blob: c131016e47b9bf480d845c56b394bc7f9a08507b [file] [log] [blame]
Adrien Béraud04463092013-05-06 14:17:22 +10001package com.savoirfairelinux.sflphone.model;
alisionfe9cf712013-05-03 17:26:08 -04002
Adrien Béraud0c9bd8f2013-05-30 16:16:57 +10003import java.util.List;
4
alisionfe9cf712013-05-03 17:26:08 -04005import android.content.Context;
6import android.graphics.Canvas;
7import android.graphics.Color;
Adrien Béraud33268882013-05-18 03:41:15 +10008import android.graphics.Paint;
Adrien Béraud6bbce912013-05-24 00:48:13 +10009import android.graphics.Paint.Align;
Adrien Béraud04463092013-05-06 14:17:22 +100010import android.os.Handler;
11import android.os.Message;
alisionfe9cf712013-05-03 17:26:08 -040012import android.util.AttributeSet;
13import android.util.Log;
14import android.view.MotionEvent;
Adrien Béraud04463092013-05-06 14:17:22 +100015import android.view.SurfaceHolder;
16import android.view.SurfaceView;
alisionfe9cf712013-05-03 17:26:08 -040017import android.view.View;
Adrien Béraud04463092013-05-06 14:17:22 +100018import android.view.View.OnTouchListener;
alisionfe9cf712013-05-03 17:26:08 -040019
alision2cb99562013-05-30 17:02:20 -040020public class BubblesView extends SurfaceView implements SurfaceHolder.Callback, OnTouchListener {
21 private static final String TAG = BubblesView.class.getSimpleName();
alisionfe9cf712013-05-03 17:26:08 -040022
alision2cb99562013-05-30 17:02:20 -040023 private BubblesThread thread = null;
24 private BubbleModel model;
alisionfe9cf712013-05-03 17:26:08 -040025
alision2cb99562013-05-30 17:02:20 -040026 private Paint attractor_paint = new Paint();
27 private Paint name_paint = new Paint(Paint.ANTI_ALIAS_FLAG);
Adrien Béraud6bbce912013-05-24 00:48:13 +100028
alision2cb99562013-05-30 17:02:20 -040029 private float density;
30 private float textDensity;
Adrien Béraud33268882013-05-18 03:41:15 +100031
alision2cb99562013-05-30 17:02:20 -040032 private boolean dragging_bubble = false;
Adrien Béraudc9c424d2013-05-30 17:47:35 +100033
alision2cb99562013-05-30 17:02:20 -040034 public BubblesView(Context context, AttributeSet attrs) {
35 super(context, attrs);
alisionfe9cf712013-05-03 17:26:08 -040036
alision2cb99562013-05-30 17:02:20 -040037 density = getResources().getDisplayMetrics().density;
38 textDensity = getResources().getDisplayMetrics().scaledDensity;
Adrien Béraud6bbce912013-05-24 00:48:13 +100039
alision2cb99562013-05-30 17:02:20 -040040 SurfaceHolder holder = getHolder();
41 holder.addCallback(this);
alisionfe9cf712013-05-03 17:26:08 -040042
alision2cb99562013-05-30 17:02:20 -040043 // create thread only; it's started in surfaceCreated()
44 createThread();
alisionfe9cf712013-05-03 17:26:08 -040045
alision2cb99562013-05-30 17:02:20 -040046 setOnTouchListener(this);
47 setFocusable(true);
Adrien Béraud33268882013-05-18 03:41:15 +100048
alision2cb99562013-05-30 17:02:20 -040049 attractor_paint.setColor(Color.RED);
50 // attractor_paint.set
51 name_paint.setTextSize(18 * textDensity);
52 name_paint.setColor(0xFF303030);
53 name_paint.setTextAlign(Align.CENTER);
54 }
alisionfe9cf712013-05-03 17:26:08 -040055
alision2cb99562013-05-30 17:02:20 -040056 private void createThread() {
57 if (thread != null)
58 return;
59 thread = new BubblesThread(getHolder(), getContext(), new Handler() {
60 @Override
61 public void handleMessage(Message m) {
62 /*
63 * mStatusText.setVisibility(m.getData().getInt("viz")); mStatusText.setText(m.getData().getString("text"));
64 */
65 }
66 });
67 if (model != null)
68 thread.setModel(model);
69 }
alisionfe9cf712013-05-03 17:26:08 -040070
alision2cb99562013-05-30 17:02:20 -040071 public void setModel(BubbleModel model) {
72 this.model = model;
73 thread.setModel(model);
74 }
alisionfe9cf712013-05-03 17:26:08 -040075
alision2cb99562013-05-30 17:02:20 -040076 /*
77 * @Override public void onWindowFocusChanged(boolean hasWindowFocus) { if (!hasWindowFocus) { thread.pause(); } }
78 */
alisionfe9cf712013-05-03 17:26:08 -040079
alision2cb99562013-05-30 17:02:20 -040080 @Override
81 public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
82 Log.w(TAG, "surfaceChanged " + width + "-" + height);
83 thread.setSurfaceSize(width, height);
84 }
alisionfe9cf712013-05-03 17:26:08 -040085
alision2cb99562013-05-30 17:02:20 -040086 /*
87 * Callback invoked when the Surface has been created and is ready to be used.
88 */
89 @Override
90 public void surfaceCreated(SurfaceHolder holder) {
91 // start the thread here so that we don't busy-wait in run()
92 // waiting for the surface to be created
93 createThread();
alisionfe9cf712013-05-03 17:26:08 -040094
alision2cb99562013-05-30 17:02:20 -040095 Log.w(TAG, "surfaceCreated");
96 thread.setRunning(true);
97 thread.start();
98 }
alisionfe9cf712013-05-03 17:26:08 -040099
alision2cb99562013-05-30 17:02:20 -0400100 /*
101 * Callback invoked when the Surface has been destroyed and must no longer be touched. WARNING: after this method returns, the Surface/Canvas must
102 * never be touched again!
103 */
104 @Override
105 public void surfaceDestroyed(SurfaceHolder holder) {
106 // we have to tell thread to shut down & wait for it to finish, or else
107 // it might touch the Surface after we return and explode
108 Log.w(TAG, "surfaceDestroyed");
109 boolean retry = true;
110 thread.setRunning(false);
111 while (retry) {
112 try {
113 thread.join();
114 retry = false;
115 } catch (InterruptedException e) {
116 }
117 }
118 thread = null;
119 }
alisionfe9cf712013-05-03 17:26:08 -0400120
alision2cb99562013-05-30 17:02:20 -0400121 @Override
122 public boolean onTouch(View v, MotionEvent event) {
123 Log.w(TAG, "onTouch " + event.getAction());
Adrien Béraud25fc4092013-05-06 15:28:39 +1000124
alision2cb99562013-05-30 17:02:20 -0400125 int action = event.getActionMasked();
Adrien Béraud25fc4092013-05-06 15:28:39 +1000126
alision2cb99562013-05-30 17:02:20 -0400127 synchronized (model) {
128 List<Bubble> bubbles = model.getBubbles();
129 final int n_bubbles = bubbles.size();
Adrien Béraude0ef0c22013-05-18 01:56:27 +1000130
alision2cb99562013-05-30 17:02:20 -0400131 if (action == MotionEvent.ACTION_DOWN) {
132 for (int i = 0; i < n_bubbles; i++) {
133 Bubble b = bubbles.get(i);
134 if (b.intersects(event.getX(), event.getY())) {
135 b.dragged = true;
136 b.last_drag = System.nanoTime();
137 b.setPos(event.getX(), event.getY());
138 b.target_scale = .8f;
139 dragging_bubble = true;
140 }
141 }
142 } else if (action == MotionEvent.ACTION_MOVE) {
143 long now = System.nanoTime();
144 for (int i = 0; i < n_bubbles; i++) {
145 Bubble b = bubbles.get(i);
146 if (b.dragged) {
147 float x = event.getX(), y = event.getY();
148 float dt = (float) ((now - b.last_drag) / 1000000000.);
149 float dx = x - b.getPosX(), dy = y - b.getPosY();
150 b.last_drag = now;
Adrien Béraud0c9bd8f2013-05-30 16:16:57 +1000151
alision2cb99562013-05-30 17:02:20 -0400152 b.setPos(event.getX(), event.getY());
153 /*
154 * int hn = event.getHistorySize() - 2; Log.w(TAG, "event.getHistorySize() : " + event.getHistorySize()); if(hn > 0) { float
155 * dx = x-event.getHistoricalX(hn); float dy = y-event.getHistoricalY(hn); float dt = event.getHistoricalEventTime(hn)/1000.f;
156 */
157 b.speed.x = dx / dt;
158 b.speed.y = dy / dt;
159 // Log.w(TAG, "onTouch dx:" + b.speed.x + " dy:" + b.speed.y);
160 // }
161 return true;
162 }
163 }
164 } else if (action == MotionEvent.ACTION_UP || action == MotionEvent.ACTION_CANCEL) {
165 for (int i = 0; i < n_bubbles; i++) {
166 Bubble b = bubbles.get(i);
167 if (b.dragged) {
168 b.dragged = false;
169 b.target_scale = 1.f;
170 }
171 }
172 dragging_bubble = false;
173 }
174 }
Adrien Béraud0c9bd8f2013-05-30 16:16:57 +1000175
alision2cb99562013-05-30 17:02:20 -0400176 return true;
177 }
alisionfe9cf712013-05-03 17:26:08 -0400178
alision2cb99562013-05-30 17:02:20 -0400179 public boolean isDraggingBubble() {
180 return dragging_bubble;
181 }
Adrien Béraudc9c424d2013-05-30 17:47:35 +1000182
alision2cb99562013-05-30 17:02:20 -0400183 class BubblesThread extends Thread {
184 private boolean running = false;
185 private SurfaceHolder surfaceHolder;
alisionfe9cf712013-05-03 17:26:08 -0400186
alision2cb99562013-05-30 17:02:20 -0400187 BubbleModel model = null;
alisionfe9cf712013-05-03 17:26:08 -0400188
alision2cb99562013-05-30 17:02:20 -0400189 public BubblesThread(SurfaceHolder holder, Context context, Handler handler) {
190 surfaceHolder = holder;
191 }
alisionfe9cf712013-05-03 17:26:08 -0400192
alision2cb99562013-05-30 17:02:20 -0400193 public void setModel(BubbleModel model) {
194 this.model = model;
195 }
alisionfe9cf712013-05-03 17:26:08 -0400196
alision2cb99562013-05-30 17:02:20 -0400197 @Override
198 public void run() {
199 while (running) {
200 Canvas c = null;
201 try {
202 c = surfaceHolder.lockCanvas(null);
Adrien Béraud7ed23dc2013-05-06 16:27:24 +1000203
alision2cb99562013-05-30 17:02:20 -0400204 // for the case the surface is destroyed while already in the loop
205 if (c == null || model == null)
206 continue;
Adrien Béraud7ed23dc2013-05-06 16:27:24 +1000207
alision2cb99562013-05-30 17:02:20 -0400208 synchronized (surfaceHolder) {
209 // Log.w(TAG, "Thread doDraw");
210 model.update();
211 doDraw(c);
212 }
213 } finally {
214 if (c != null)
215 surfaceHolder.unlockCanvasAndPost(c);
216 }
217 }
218 }
Adrien Béraud04463092013-05-06 14:17:22 +1000219
alision2cb99562013-05-30 17:02:20 -0400220 public void setRunning(boolean b) {
221 running = b;
222 }
Adrien Béraud04463092013-05-06 14:17:22 +1000223
alision2cb99562013-05-30 17:02:20 -0400224 public void setSurfaceSize(int width, int height) {
225 synchronized (surfaceHolder) {
226 if (model != null) {
227 model.width = width;
228 model.height = height;
229 }
230 }
231 }
Adrien Béraud04463092013-05-06 14:17:22 +1000232
alision2cb99562013-05-30 17:02:20 -0400233 /**
234 * I got multiple IndexOutOfBoundsException, when switching calls. //FIXME
235 *
236 * @param canvas
237 */
238 private void doDraw(Canvas canvas) {
239 canvas.drawColor(Color.WHITE);
Adrien Béraud04463092013-05-06 14:17:22 +1000240
alision2cb99562013-05-30 17:02:20 -0400241 synchronized (model) {
242 List<Bubble> bubbles = model.getBubbles();
243 List<Attractor> attractors = model.getAttractors();
244 try {
Adrien Béraud0c9bd8f2013-05-30 16:16:57 +1000245
alision2cb99562013-05-30 17:02:20 -0400246 for (int i = 0, n = attractors.size(); i < n; i++) {
247 Attractor a = attractors.get(i);
248 canvas.drawBitmap(a.getBitmap(), null, a.getBounds(), null);
249 }
Adrien Béraud33268882013-05-18 03:41:15 +1000250
alision2cb99562013-05-30 17:02:20 -0400251 for (int i = 0, n = bubbles.size(); i < n; i++) {
252 Bubble b = bubbles.get(i);
253 canvas.drawBitmap(b.getBitmap(), null, b.getBounds(), null);
254 canvas.drawText(b.contact.getmDisplayName(), b.getPosX(), b.getPosY() - 50 * density, name_paint);
255 }
256
257 } catch (IndexOutOfBoundsException e) {
258 Log.e(TAG, e.toString());
259 }
260 }
261 }
262 }
Adrien Béraud04463092013-05-06 14:17:22 +1000263
alisionfe9cf712013-05-03 17:26:08 -0400264}