Adrien Béraud | 0446309 | 2013-05-06 14:17:22 +1000 | [diff] [blame] | 1 | package com.savoirfairelinux.sflphone.model;
|
alision | fe9cf71 | 2013-05-03 17:26:08 -0400 | [diff] [blame] | 2 |
|
Adrien Béraud | 0c9bd8f | 2013-05-30 16:16:57 +1000 | [diff] [blame] | 3 | import java.util.List;
|
| 4 |
|
alision | fe9cf71 | 2013-05-03 17:26:08 -0400 | [diff] [blame] | 5 | import android.content.Context;
|
| 6 | import android.graphics.Canvas;
|
| 7 | import android.graphics.Color;
|
Adrien Béraud | 3326888 | 2013-05-18 03:41:15 +1000 | [diff] [blame] | 8 | import android.graphics.Paint;
|
Adrien Béraud | 6bbce91 | 2013-05-24 00:48:13 +1000 | [diff] [blame] | 9 | import android.graphics.Paint.Align;
|
Adrien Béraud | 0446309 | 2013-05-06 14:17:22 +1000 | [diff] [blame] | 10 | import android.os.Handler;
|
| 11 | import android.os.Message;
|
alision | fe9cf71 | 2013-05-03 17:26:08 -0400 | [diff] [blame] | 12 | import android.util.AttributeSet;
|
| 13 | import android.util.Log;
|
| 14 | import android.view.MotionEvent;
|
Adrien Béraud | 0446309 | 2013-05-06 14:17:22 +1000 | [diff] [blame] | 15 | import android.view.SurfaceHolder;
|
| 16 | import android.view.SurfaceView;
|
alision | fe9cf71 | 2013-05-03 17:26:08 -0400 | [diff] [blame] | 17 | import android.view.View;
|
Adrien Béraud | 0446309 | 2013-05-06 14:17:22 +1000 | [diff] [blame] | 18 | import android.view.View.OnTouchListener;
|
alision | fe9cf71 | 2013-05-03 17:26:08 -0400 | [diff] [blame] | 19 |
|
Adrien Béraud | 0446309 | 2013-05-06 14:17:22 +1000 | [diff] [blame] | 20 | public class BubblesView extends SurfaceView implements SurfaceHolder.Callback, OnTouchListener
|
| 21 | {
|
| 22 | private static final String TAG = BubblesView.class.getSimpleName();
|
alision | fe9cf71 | 2013-05-03 17:26:08 -0400 | [diff] [blame] | 23 |
|
Adrien Béraud | 0446309 | 2013-05-06 14:17:22 +1000 | [diff] [blame] | 24 | private BubblesThread thread = null;
|
| 25 | private BubbleModel model;
|
alision | fe9cf71 | 2013-05-03 17:26:08 -0400 | [diff] [blame] | 26 |
|
Adrien Béraud | 3326888 | 2013-05-18 03:41:15 +1000 | [diff] [blame] | 27 | private Paint attractor_paint = new Paint();
|
Adrien Béraud | 6bbce91 | 2013-05-24 00:48:13 +1000 | [diff] [blame] | 28 | private Paint name_paint = new Paint(Paint.ANTI_ALIAS_FLAG);
|
| 29 |
|
| 30 | private float density;
|
| 31 | private float textDensity;
|
Adrien Béraud | 3326888 | 2013-05-18 03:41:15 +1000 | [diff] [blame] | 32 |
|
Adrien Béraud | c9c424d | 2013-05-30 17:47:35 +1000 | [diff] [blame] | 33 | private boolean dragging_bubble = false;
|
| 34 |
|
Adrien Béraud | 0446309 | 2013-05-06 14:17:22 +1000 | [diff] [blame] | 35 | public BubblesView(Context context, AttributeSet attrs)
|
| 36 | {
|
| 37 | super(context, attrs);
|
alision | fe9cf71 | 2013-05-03 17:26:08 -0400 | [diff] [blame] | 38 |
|
Adrien Béraud | 6bbce91 | 2013-05-24 00:48:13 +1000 | [diff] [blame] | 39 | density = getResources().getDisplayMetrics().density;
|
| 40 | textDensity = getResources().getDisplayMetrics().scaledDensity;
|
| 41 |
|
Adrien Béraud | 0446309 | 2013-05-06 14:17:22 +1000 | [diff] [blame] | 42 | SurfaceHolder holder = getHolder();
|
| 43 | holder.addCallback(this);
|
alision | fe9cf71 | 2013-05-03 17:26:08 -0400 | [diff] [blame] | 44 |
|
Adrien Béraud | 0446309 | 2013-05-06 14:17:22 +1000 | [diff] [blame] | 45 | // create thread only; it's started in surfaceCreated()
|
| 46 | createThread();
|
alision | fe9cf71 | 2013-05-03 17:26:08 -0400 | [diff] [blame] | 47 |
|
Adrien Béraud | 0446309 | 2013-05-06 14:17:22 +1000 | [diff] [blame] | 48 | setOnTouchListener(this);
|
| 49 | setFocusable(true);
|
Adrien Béraud | 3326888 | 2013-05-18 03:41:15 +1000 | [diff] [blame] | 50 |
|
| 51 | attractor_paint.setColor(Color.RED);
|
| 52 | //attractor_paint.set
|
Adrien Béraud | c9c424d | 2013-05-30 17:47:35 +1000 | [diff] [blame] | 53 | name_paint.setTextSize(18 * textDensity);
|
Adrien Béraud | 6bbce91 | 2013-05-24 00:48:13 +1000 | [diff] [blame] | 54 | name_paint.setColor(0xFF303030);
|
| 55 | name_paint.setTextAlign(Align.CENTER);
|
Adrien Béraud | 0446309 | 2013-05-06 14:17:22 +1000 | [diff] [blame] | 56 | }
|
alision | fe9cf71 | 2013-05-03 17:26:08 -0400 | [diff] [blame] | 57 |
|
Adrien Béraud | e0ef0c2 | 2013-05-18 01:56:27 +1000 | [diff] [blame] | 58 | private void createThread()
|
| 59 | {
|
| 60 | if (thread != null)
|
| 61 | return;
|
Adrien Béraud | 0446309 | 2013-05-06 14:17:22 +1000 | [diff] [blame] | 62 | thread = new BubblesThread(getHolder(), getContext(), new Handler() {
|
| 63 | @Override
|
| 64 | public void handleMessage(Message m)
|
| 65 | {
|
| 66 | /* mStatusText.setVisibility(m.getData().getInt("viz"));
|
| 67 | mStatusText.setText(m.getData().getString("text"));*/
|
| 68 | }
|
| 69 | });
|
Adrien Béraud | e0ef0c2 | 2013-05-18 01:56:27 +1000 | [diff] [blame] | 70 | if (model != null)
|
Adrien Béraud | 65ba5b1 | 2013-05-06 15:42:50 +1000 | [diff] [blame] | 71 | thread.setModel(model);
|
Adrien Béraud | 0446309 | 2013-05-06 14:17:22 +1000 | [diff] [blame] | 72 | }
|
alision | fe9cf71 | 2013-05-03 17:26:08 -0400 | [diff] [blame] | 73 |
|
Adrien Béraud | 0446309 | 2013-05-06 14:17:22 +1000 | [diff] [blame] | 74 | public void setModel(BubbleModel model)
|
| 75 | {
|
| 76 | this.model = model;
|
| 77 | thread.setModel(model);
|
| 78 | }
|
alision | fe9cf71 | 2013-05-03 17:26:08 -0400 | [diff] [blame] | 79 |
|
Adrien Béraud | 0446309 | 2013-05-06 14:17:22 +1000 | [diff] [blame] | 80 | /*@Override
|
| 81 | public void onWindowFocusChanged(boolean hasWindowFocus) {
|
| 82 | if (!hasWindowFocus) {
|
| 83 | thread.pause();
|
| 84 | }
|
| 85 | }*/
|
alision | fe9cf71 | 2013-05-03 17:26:08 -0400 | [diff] [blame] | 86 |
|
Adrien Béraud | 0446309 | 2013-05-06 14:17:22 +1000 | [diff] [blame] | 87 | @Override
|
| 88 | public void surfaceChanged(SurfaceHolder holder, int format, int width, int height)
|
| 89 | {
|
Adrien Béraud | 13cde0b | 2013-05-30 20:27:20 +1000 | [diff] [blame] | 90 | Log.w(TAG, "surfaceChanged "+width+"-"+height);
|
Adrien Béraud | 0446309 | 2013-05-06 14:17:22 +1000 | [diff] [blame] | 91 | thread.setSurfaceSize(width, height);
|
| 92 | }
|
alision | fe9cf71 | 2013-05-03 17:26:08 -0400 | [diff] [blame] | 93 |
|
Adrien Béraud | 0446309 | 2013-05-06 14:17:22 +1000 | [diff] [blame] | 94 | /*
|
| 95 | * Callback invoked when the Surface has been created and is ready to be
|
| 96 | * used.
|
| 97 | */
|
| 98 | @Override
|
| 99 | public void surfaceCreated(SurfaceHolder holder)
|
| 100 | {
|
| 101 | // start the thread here so that we don't busy-wait in run()
|
| 102 | // waiting for the surface to be created
|
| 103 | createThread();
|
alision | fe9cf71 | 2013-05-03 17:26:08 -0400 | [diff] [blame] | 104 |
|
Adrien Béraud | 0446309 | 2013-05-06 14:17:22 +1000 | [diff] [blame] | 105 | Log.w(TAG, "surfaceCreated");
|
| 106 | thread.setRunning(true);
|
| 107 | thread.start();
|
| 108 | }
|
alision | fe9cf71 | 2013-05-03 17:26:08 -0400 | [diff] [blame] | 109 |
|
Adrien Béraud | 0446309 | 2013-05-06 14:17:22 +1000 | [diff] [blame] | 110 | /*
|
| 111 | * Callback invoked when the Surface has been destroyed and must no longer
|
| 112 | * be touched. WARNING: after this method returns, the Surface/Canvas must
|
| 113 | * never be touched again!
|
| 114 | */
|
| 115 | @Override
|
| 116 | public void surfaceDestroyed(SurfaceHolder holder)
|
| 117 | {
|
| 118 | // we have to tell thread to shut down & wait for it to finish, or else
|
| 119 | // it might touch the Surface after we return and explode
|
| 120 | Log.w(TAG, "surfaceDestroyed");
|
| 121 | boolean retry = true;
|
| 122 | thread.setRunning(false);
|
Adrien Béraud | 25fc409 | 2013-05-06 15:28:39 +1000 | [diff] [blame] | 123 | while (retry) {
|
Adrien Béraud | 0446309 | 2013-05-06 14:17:22 +1000 | [diff] [blame] | 124 | try {
|
| 125 | thread.join();
|
| 126 | retry = false;
|
| 127 | } catch (InterruptedException e) {
|
| 128 | }
|
Adrien Béraud | 25fc409 | 2013-05-06 15:28:39 +1000 | [diff] [blame] | 129 | }
|
Adrien Béraud | 0446309 | 2013-05-06 14:17:22 +1000 | [diff] [blame] | 130 | thread = null;
|
| 131 | }
|
alision | fe9cf71 | 2013-05-03 17:26:08 -0400 | [diff] [blame] | 132 |
|
Adrien Béraud | 0446309 | 2013-05-06 14:17:22 +1000 | [diff] [blame] | 133 | @Override
|
| 134 | public boolean onTouch(View v, MotionEvent event)
|
| 135 | {
|
| 136 | Log.w(TAG, "onTouch " + event.getAction());
|
Adrien Béraud | 25fc409 | 2013-05-06 15:28:39 +1000 | [diff] [blame] | 137 |
|
| 138 | int action = event.getActionMasked();
|
| 139 |
|
Adrien Béraud | 0c9bd8f | 2013-05-30 16:16:57 +1000 | [diff] [blame] | 140 | synchronized (model) {
|
| 141 | List<Bubble> bubbles = model.getBubbles();
|
| 142 | final int n_bubbles = bubbles.size();
|
Adrien Béraud | e0ef0c2 | 2013-05-18 01:56:27 +1000 | [diff] [blame] | 143 |
|
Adrien Béraud | 0c9bd8f | 2013-05-30 16:16:57 +1000 | [diff] [blame] | 144 | if (action == MotionEvent.ACTION_DOWN) {
|
Adrien Béraud | c9c424d | 2013-05-30 17:47:35 +1000 | [diff] [blame] | 145 | for (int i = 0; i < n_bubbles; i++) {
|
Adrien Béraud | 0c9bd8f | 2013-05-30 16:16:57 +1000 | [diff] [blame] | 146 | Bubble b = bubbles.get(i);
|
| 147 | if (b.intersects(event.getX(), event.getY())) {
|
| 148 | b.dragged = true;
|
| 149 | b.last_drag = System.nanoTime();
|
| 150 | b.setPos(event.getX(), event.getY());
|
| 151 | b.target_scale = .8f;
|
Adrien Béraud | c9c424d | 2013-05-30 17:47:35 +1000 | [diff] [blame] | 152 | dragging_bubble = true;
|
Adrien Béraud | 0c9bd8f | 2013-05-30 16:16:57 +1000 | [diff] [blame] | 153 | }
|
| 154 | }
|
| 155 | } else if (action == MotionEvent.ACTION_MOVE) {
|
| 156 | long now = System.nanoTime();
|
Adrien Béraud | c9c424d | 2013-05-30 17:47:35 +1000 | [diff] [blame] | 157 | for (int i = 0; i < n_bubbles; i++) {
|
Adrien Béraud | 0c9bd8f | 2013-05-30 16:16:57 +1000 | [diff] [blame] | 158 | Bubble b = bubbles.get(i);
|
| 159 | if (b.dragged) {
|
| 160 | float x = event.getX(), y = event.getY();
|
Adrien Béraud | c9c424d | 2013-05-30 17:47:35 +1000 | [diff] [blame] | 161 | float dt = (float) ((now - b.last_drag) / 1000000000.);
|
Adrien Béraud | 0c9bd8f | 2013-05-30 16:16:57 +1000 | [diff] [blame] | 162 | float dx = x - b.getPosX(), dy = y - b.getPosY();
|
| 163 | b.last_drag = now;
|
| 164 |
|
| 165 | b.setPos(event.getX(), event.getY());
|
| 166 | /*int hn = event.getHistorySize() - 2;
|
Adrien Béraud | c9c424d | 2013-05-30 17:47:35 +1000 | [diff] [blame] | 167 | Log.w(TAG, "event.getHistorySize() : " + event.getHistorySize());
|
| 168 | if(hn > 0) {
|
Adrien Béraud | e0ef0c2 | 2013-05-18 01:56:27 +1000 | [diff] [blame] | 169 | float dx = x-event.getHistoricalX(hn);
|
| 170 | float dy = y-event.getHistoricalY(hn);
|
| 171 | float dt = event.getHistoricalEventTime(hn)/1000.f;*/
|
Adrien Béraud | c9c424d | 2013-05-30 17:47:35 +1000 | [diff] [blame] | 172 | b.speed.x = dx / dt;
|
| 173 | b.speed.y = dy / dt;
|
Adrien Béraud | 0c9bd8f | 2013-05-30 16:16:57 +1000 | [diff] [blame] | 174 | //Log.w(TAG, "onTouch dx:" + b.speed.x + " dy:" + b.speed.y);
|
| 175 | //}
|
| 176 | return true;
|
| 177 | }
|
Adrien Béraud | 0446309 | 2013-05-06 14:17:22 +1000 | [diff] [blame] | 178 | }
|
Adrien Béraud | 0c9bd8f | 2013-05-30 16:16:57 +1000 | [diff] [blame] | 179 | } else if (action == MotionEvent.ACTION_UP || action == MotionEvent.ACTION_CANCEL) {
|
Adrien Béraud | c9c424d | 2013-05-30 17:47:35 +1000 | [diff] [blame] | 180 | for (int i = 0; i < n_bubbles; i++) {
|
Adrien Béraud | 0c9bd8f | 2013-05-30 16:16:57 +1000 | [diff] [blame] | 181 | Bubble b = bubbles.get(i);
|
| 182 | if (b.dragged) {
|
| 183 | b.dragged = false;
|
| 184 | b.target_scale = 1.f;
|
| 185 | }
|
Adrien Béraud | 25fc409 | 2013-05-06 15:28:39 +1000 | [diff] [blame] | 186 | }
|
Adrien Béraud | c9c424d | 2013-05-30 17:47:35 +1000 | [diff] [blame] | 187 | dragging_bubble = false;
|
Adrien Béraud | 25fc409 | 2013-05-06 15:28:39 +1000 | [diff] [blame] | 188 | }
|
| 189 | }
|
Adrien Béraud | 0c9bd8f | 2013-05-30 16:16:57 +1000 | [diff] [blame] | 190 |
|
Adrien Béraud | 0446309 | 2013-05-06 14:17:22 +1000 | [diff] [blame] | 191 | return true;
|
| 192 | }
|
alision | fe9cf71 | 2013-05-03 17:26:08 -0400 | [diff] [blame] | 193 |
|
Adrien Béraud | c9c424d | 2013-05-30 17:47:35 +1000 | [diff] [blame] | 194 | public boolean isDraggingBubble()
|
| 195 | {
|
| 196 | return dragging_bubble;
|
| 197 | }
|
| 198 |
|
Adrien Béraud | 0446309 | 2013-05-06 14:17:22 +1000 | [diff] [blame] | 199 | class BubblesThread extends Thread
|
| 200 | {
|
| 201 | private boolean running = false;
|
| 202 | private SurfaceHolder surfaceHolder;
|
alision | fe9cf71 | 2013-05-03 17:26:08 -0400 | [diff] [blame] | 203 |
|
Adrien Béraud | 0446309 | 2013-05-06 14:17:22 +1000 | [diff] [blame] | 204 | BubbleModel model = null;
|
alision | fe9cf71 | 2013-05-03 17:26:08 -0400 | [diff] [blame] | 205 |
|
Adrien Béraud | 0446309 | 2013-05-06 14:17:22 +1000 | [diff] [blame] | 206 | public BubblesThread(SurfaceHolder holder, Context context, Handler handler)
|
| 207 | {
|
| 208 | surfaceHolder = holder;
|
| 209 | }
|
alision | fe9cf71 | 2013-05-03 17:26:08 -0400 | [diff] [blame] | 210 |
|
Adrien Béraud | 0446309 | 2013-05-06 14:17:22 +1000 | [diff] [blame] | 211 | public void setModel(BubbleModel model)
|
| 212 | {
|
| 213 | this.model = model;
|
| 214 | }
|
alision | fe9cf71 | 2013-05-03 17:26:08 -0400 | [diff] [blame] | 215 |
|
Adrien Béraud | 0446309 | 2013-05-06 14:17:22 +1000 | [diff] [blame] | 216 | @Override
|
| 217 | public void run()
|
| 218 | {
|
| 219 | while (running) {
|
| 220 | Canvas c = null;
|
| 221 | try {
|
| 222 | c = surfaceHolder.lockCanvas(null);
|
Adrien Béraud | 7ed23dc | 2013-05-06 16:27:24 +1000 | [diff] [blame] | 223 |
|
| 224 | // for the case the surface is destroyed while already in the loop
|
Adrien Béraud | e0ef0c2 | 2013-05-18 01:56:27 +1000 | [diff] [blame] | 225 | if (c == null || model == null)
|
| 226 | continue;
|
Adrien Béraud | 7ed23dc | 2013-05-06 16:27:24 +1000 | [diff] [blame] | 227 |
|
Adrien Béraud | 0446309 | 2013-05-06 14:17:22 +1000 | [diff] [blame] | 228 | synchronized (surfaceHolder) {
|
Adrien Béraud | 25fc409 | 2013-05-06 15:28:39 +1000 | [diff] [blame] | 229 | //Log.w(TAG, "Thread doDraw");
|
| 230 | model.update();
|
Adrien Béraud | 0446309 | 2013-05-06 14:17:22 +1000 | [diff] [blame] | 231 | doDraw(c);
|
| 232 | }
|
| 233 | } finally {
|
Adrien Béraud | 0446309 | 2013-05-06 14:17:22 +1000 | [diff] [blame] | 234 | if (c != null)
|
| 235 | surfaceHolder.unlockCanvasAndPost(c);
|
| 236 | }
|
| 237 | }
|
| 238 | }
|
| 239 |
|
| 240 | public void setRunning(boolean b)
|
| 241 | {
|
| 242 | running = b;
|
| 243 | }
|
| 244 |
|
| 245 | public void setSurfaceSize(int width, int height)
|
| 246 | {
|
| 247 | synchronized (surfaceHolder) {
|
Adrien Béraud | e0ef0c2 | 2013-05-18 01:56:27 +1000 | [diff] [blame] | 248 | if (model != null) {
|
Adrien Béraud | 25fc409 | 2013-05-06 15:28:39 +1000 | [diff] [blame] | 249 | model.width = width;
|
| 250 | model.height = height;
|
| 251 | }
|
Adrien Béraud | 0446309 | 2013-05-06 14:17:22 +1000 | [diff] [blame] | 252 | }
|
| 253 | }
|
| 254 |
|
Adrien Béraud | 0446309 | 2013-05-06 14:17:22 +1000 | [diff] [blame] | 255 | private void doDraw(Canvas canvas)
|
| 256 | {
|
| 257 | canvas.drawColor(Color.WHITE);
|
| 258 |
|
Adrien Béraud | e0ef0c2 | 2013-05-18 01:56:27 +1000 | [diff] [blame] | 259 | synchronized (model) {
|
Adrien Béraud | 0c9bd8f | 2013-05-30 16:16:57 +1000 | [diff] [blame] | 260 | List<Bubble> bubbles = model.getBubbles();
|
| 261 | List<Attractor> attractors = model.getAttractors();
|
| 262 |
|
Adrien Béraud | c9c424d | 2013-05-30 17:47:35 +1000 | [diff] [blame] | 263 | for (int i = 0, n = attractors.size(); i < n; i++) {
|
Adrien Béraud | 0c9bd8f | 2013-05-30 16:16:57 +1000 | [diff] [blame] | 264 | Attractor a = attractors.get(i);
|
Adrien Béraud | 0c9bd8f | 2013-05-30 16:16:57 +1000 | [diff] [blame] | 265 | canvas.drawBitmap(a.getBitmap(), null, a.getBounds(), null);
|
Adrien Béraud | 3326888 | 2013-05-18 03:41:15 +1000 | [diff] [blame] | 266 | }
|
| 267 |
|
Adrien Béraud | c9c424d | 2013-05-30 17:47:35 +1000 | [diff] [blame] | 268 | for (int i = 0, n = bubbles.size(); i < n; i++) {
|
Adrien Béraud | 0c9bd8f | 2013-05-30 16:16:57 +1000 | [diff] [blame] | 269 | Bubble b = bubbles.get(i);
|
Adrien Béraud | 0c9bd8f | 2013-05-30 16:16:57 +1000 | [diff] [blame] | 270 | canvas.drawBitmap(b.getBitmap(), null, b.getBounds(), null);
|
Adrien Béraud | c9c424d | 2013-05-30 17:47:35 +1000 | [diff] [blame] | 271 | canvas.drawText(b.contact.getmDisplayName(), b.getPosX(), b.getPosY() - 50 * density, name_paint);
|
Adrien Béraud | e0ef0c2 | 2013-05-18 01:56:27 +1000 | [diff] [blame] | 272 | }
|
Adrien Béraud | 25fc409 | 2013-05-06 15:28:39 +1000 | [diff] [blame] | 273 | }
|
Adrien Béraud | 0446309 | 2013-05-06 14:17:22 +1000 | [diff] [blame] | 274 | }
|
| 275 | }
|
| 276 |
|
alision | fe9cf71 | 2013-05-03 17:26:08 -0400 | [diff] [blame] | 277 | }
|