blob: 1beb7613107f0d1e358f1455a6b135b0a875713a [file] [log] [blame]
alisioncc7bb422013-06-06 15:31:39 -04001/*
2 * Copyright (C) 2004-2013 Savoir-Faire Linux Inc.
3 *
4 * Author: Alexandre Lision <alexandre.lision@savoirfairelinux.com>
5 * Adrien Béraud <adrien.beraud@gmail.com>
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 *
21 * Additional permission under GNU GPL version 3 section 7:
22 *
23 * If you modify this program, or any covered work, by linking or
24 * combining it with the OpenSSL project's OpenSSL library (or a
25 * modified version of that library), containing parts covered by the
26 * terms of the OpenSSL or SSLeay licenses, Savoir-Faire Linux Inc.
27 * grants you additional permission to convey the resulting work.
28 * Corresponding Source for a non-source form of such a combination
29 * shall include the source code for the parts of OpenSSL used as well
30 * as that of the covered work.
31 */
32
Adrien Béraud04463092013-05-06 14:17:22 +100033package com.savoirfairelinux.sflphone.model;
alisionfe9cf712013-05-03 17:26:08 -040034
Adrien Béraud0c9bd8f2013-05-30 16:16:57 +100035import java.util.List;
36
alisionfe9cf712013-05-03 17:26:08 -040037import android.content.Context;
38import android.graphics.Canvas;
39import android.graphics.Color;
Adrien Béraud33268882013-05-18 03:41:15 +100040import android.graphics.Paint;
Adrien Béraud6bbce912013-05-24 00:48:13 +100041import android.graphics.Paint.Align;
alision2ec64f92013-06-17 17:28:58 -040042import android.graphics.PointF;
43import android.graphics.RectF;
Adrien Béraud04463092013-05-06 14:17:22 +100044import android.os.Handler;
45import android.os.Message;
alisionfe9cf712013-05-03 17:26:08 -040046import android.util.AttributeSet;
47import android.util.Log;
alision58356b72013-06-03 17:13:36 -040048import android.view.GestureDetector;
49import android.view.GestureDetector.OnGestureListener;
alisionfe9cf712013-05-03 17:26:08 -040050import android.view.MotionEvent;
Adrien Béraud04463092013-05-06 14:17:22 +100051import android.view.SurfaceHolder;
52import android.view.SurfaceView;
alisionfe9cf712013-05-03 17:26:08 -040053import android.view.View;
Adrien Béraud04463092013-05-06 14:17:22 +100054import android.view.View.OnTouchListener;
alisionfe9cf712013-05-03 17:26:08 -040055
alision2cb99562013-05-30 17:02:20 -040056public class BubblesView extends SurfaceView implements SurfaceHolder.Callback, OnTouchListener {
57 private static final String TAG = BubblesView.class.getSimpleName();
alisionfe9cf712013-05-03 17:26:08 -040058
alision2cb99562013-05-30 17:02:20 -040059 private BubblesThread thread = null;
60 private BubbleModel model;
alisionfe9cf712013-05-03 17:26:08 -040061
alision2cb99562013-05-30 17:02:20 -040062 private Paint attractor_paint = new Paint();
63 private Paint name_paint = new Paint(Paint.ANTI_ALIAS_FLAG);
Adrien Béraud6bbce912013-05-24 00:48:13 +100064
alisione38001f2013-06-04 14:14:39 -040065 private GestureDetector gDetector;
66
alision2cb99562013-05-30 17:02:20 -040067 private float density;
68 private float textDensity;
Adrien Béraud33268882013-05-18 03:41:15 +100069
alision2cb99562013-05-30 17:02:20 -040070 private boolean dragging_bubble = false;
Adrien Béraudc9c424d2013-05-30 17:47:35 +100071
alision2cb99562013-05-30 17:02:20 -040072 public BubblesView(Context context, AttributeSet attrs) {
73 super(context, attrs);
alisionfe9cf712013-05-03 17:26:08 -040074
alision2cb99562013-05-30 17:02:20 -040075 density = getResources().getDisplayMetrics().density;
76 textDensity = getResources().getDisplayMetrics().scaledDensity;
Adrien Béraud6bbce912013-05-24 00:48:13 +100077
alision2cb99562013-05-30 17:02:20 -040078 SurfaceHolder holder = getHolder();
79 holder.addCallback(this);
alisionfe9cf712013-05-03 17:26:08 -040080
alision2cb99562013-05-30 17:02:20 -040081 // create thread only; it's started in surfaceCreated()
82 createThread();
alisionfe9cf712013-05-03 17:26:08 -040083
alision2cb99562013-05-30 17:02:20 -040084 setOnTouchListener(this);
85 setFocusable(true);
Adrien Béraud33268882013-05-18 03:41:15 +100086
alision2cb99562013-05-30 17:02:20 -040087 attractor_paint.setColor(Color.RED);
88 // attractor_paint.set
89 name_paint.setTextSize(18 * textDensity);
90 name_paint.setColor(0xFF303030);
91 name_paint.setTextAlign(Align.CENTER);
alision58356b72013-06-03 17:13:36 -040092
93 gDetector = new GestureDetector(getContext(), new MyOnGestureListener());
alision2cb99562013-05-30 17:02:20 -040094 }
alisionfe9cf712013-05-03 17:26:08 -040095
alision2cb99562013-05-30 17:02:20 -040096 private void createThread() {
97 if (thread != null)
98 return;
99 thread = new BubblesThread(getHolder(), getContext(), new Handler() {
100 @Override
101 public void handleMessage(Message m) {
102 /*
103 * mStatusText.setVisibility(m.getData().getInt("viz")); mStatusText.setText(m.getData().getString("text"));
104 */
105 }
106 });
107 if (model != null)
108 thread.setModel(model);
109 }
alisionfe9cf712013-05-03 17:26:08 -0400110
alision2cb99562013-05-30 17:02:20 -0400111 public void setModel(BubbleModel model) {
112 this.model = model;
113 thread.setModel(model);
114 }
alisionfe9cf712013-05-03 17:26:08 -0400115
alision2cb99562013-05-30 17:02:20 -0400116 /*
117 * @Override public void onWindowFocusChanged(boolean hasWindowFocus) { if (!hasWindowFocus) { thread.pause(); } }
118 */
alisionfe9cf712013-05-03 17:26:08 -0400119
alision2cb99562013-05-30 17:02:20 -0400120 @Override
121 public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
122 Log.w(TAG, "surfaceChanged " + width + "-" + height);
123 thread.setSurfaceSize(width, height);
124 }
alisionfe9cf712013-05-03 17:26:08 -0400125
alision2cb99562013-05-30 17:02:20 -0400126 /*
127 * Callback invoked when the Surface has been created and is ready to be used.
128 */
129 @Override
130 public void surfaceCreated(SurfaceHolder holder) {
131 // start the thread here so that we don't busy-wait in run()
132 // waiting for the surface to be created
133 createThread();
alisionfe9cf712013-05-03 17:26:08 -0400134
alision2cb99562013-05-30 17:02:20 -0400135 Log.w(TAG, "surfaceCreated");
136 thread.setRunning(true);
137 thread.start();
138 }
alisionfe9cf712013-05-03 17:26:08 -0400139
alision2cb99562013-05-30 17:02:20 -0400140 /*
141 * Callback invoked when the Surface has been destroyed and must no longer be touched. WARNING: after this method returns, the Surface/Canvas must
142 * never be touched again!
143 */
144 @Override
145 public void surfaceDestroyed(SurfaceHolder holder) {
146 // we have to tell thread to shut down & wait for it to finish, or else
147 // it might touch the Surface after we return and explode
148 Log.w(TAG, "surfaceDestroyed");
149 boolean retry = true;
150 thread.setRunning(false);
151 while (retry) {
152 try {
153 thread.join();
154 retry = false;
155 } catch (InterruptedException e) {
156 }
157 }
158 thread = null;
159 }
alisionfe9cf712013-05-03 17:26:08 -0400160
alision2cb99562013-05-30 17:02:20 -0400161 public boolean isDraggingBubble() {
162 return dragging_bubble;
163 }
Adrien Béraudc9c424d2013-05-30 17:47:35 +1000164
alision2cb99562013-05-30 17:02:20 -0400165 class BubblesThread extends Thread {
166 private boolean running = false;
167 private SurfaceHolder surfaceHolder;
alisione38001f2013-06-04 14:14:39 -0400168 public Boolean suspendFlag = false;
alisionfe9cf712013-05-03 17:26:08 -0400169
alision2cb99562013-05-30 17:02:20 -0400170 BubbleModel model = null;
alisionfe9cf712013-05-03 17:26:08 -0400171
alision2cb99562013-05-30 17:02:20 -0400172 public BubblesThread(SurfaceHolder holder, Context context, Handler handler) {
173 surfaceHolder = holder;
174 }
alisionfe9cf712013-05-03 17:26:08 -0400175
alision2cb99562013-05-30 17:02:20 -0400176 public void setModel(BubbleModel model) {
177 this.model = model;
178 }
alisionfe9cf712013-05-03 17:26:08 -0400179
alision2cb99562013-05-30 17:02:20 -0400180 @Override
181 public void run() {
182 while (running) {
183 Canvas c = null;
184 try {
Adrien Béraud7ed23dc2013-05-06 16:27:24 +1000185
alisione38001f2013-06-04 14:14:39 -0400186 if (suspendFlag) {
187 synchronized (this) {
188 while (suspendFlag) {
189 try {
190 wait();
191 } catch (InterruptedException e) {
192 // TODO Auto-generated catch block
193 e.printStackTrace();
194 }
195 }
196 }
197 } else {
alisione38001f2013-06-04 14:14:39 -0400198 c = surfaceHolder.lockCanvas(null);
Adrien Béraud7ed23dc2013-05-06 16:27:24 +1000199
alisione38001f2013-06-04 14:14:39 -0400200 // for the case the surface is destroyed while already in the loop
201 if (c == null || model == null)
202 continue;
203
204 synchronized (surfaceHolder) {
205 // Log.w(TAG, "Thread doDraw");
206 model.update();
207 doDraw(c);
208 }
alision2cb99562013-05-30 17:02:20 -0400209 }
alisione38001f2013-06-04 14:14:39 -0400210
alision2cb99562013-05-30 17:02:20 -0400211 } finally {
212 if (c != null)
213 surfaceHolder.unlockCanvasAndPost(c);
214 }
215 }
216 }
Adrien Béraud04463092013-05-06 14:17:22 +1000217
alisione38001f2013-06-04 14:14:39 -0400218 public void setPaused(boolean wantToPause) {
219 synchronized (this) {
220 suspendFlag = wantToPause;
221 notify();
222 }
223 }
224
alision2cb99562013-05-30 17:02:20 -0400225 public void setRunning(boolean b) {
226 running = b;
227 }
Adrien Béraud04463092013-05-06 14:17:22 +1000228
alision2cb99562013-05-30 17:02:20 -0400229 public void setSurfaceSize(int width, int height) {
230 synchronized (surfaceHolder) {
231 if (model != null) {
232 model.width = width;
233 model.height = height;
234 }
235 }
236 }
Adrien Béraud04463092013-05-06 14:17:22 +1000237
alision2cb99562013-05-30 17:02:20 -0400238 /**
239 * I got multiple IndexOutOfBoundsException, when switching calls. //FIXME
240 *
241 * @param canvas
242 */
243 private void doDraw(Canvas canvas) {
244 canvas.drawColor(Color.WHITE);
Adrien Béraud04463092013-05-06 14:17:22 +1000245
alision2cb99562013-05-30 17:02:20 -0400246 synchronized (model) {
247 List<Bubble> bubbles = model.getBubbles();
248 List<Attractor> attractors = model.getAttractors();
249 try {
Adrien Béraud0c9bd8f2013-05-30 16:16:57 +1000250
alision2cb99562013-05-30 17:02:20 -0400251 for (int i = 0, n = attractors.size(); i < n; i++) {
252 Attractor a = attractors.get(i);
253 canvas.drawBitmap(a.getBitmap(), null, a.getBounds(), null);
254 }
Adrien Béraud33268882013-05-18 03:41:15 +1000255
alision2cb99562013-05-30 17:02:20 -0400256 for (int i = 0, n = bubbles.size(); i < n; i++) {
257 Bubble b = bubbles.get(i);
258 canvas.drawBitmap(b.getBitmap(), null, b.getBounds(), null);
259 canvas.drawText(b.contact.getmDisplayName(), b.getPosX(), b.getPosY() - 50 * density, name_paint);
alision2ec64f92013-06-17 17:28:58 -0400260
alision2cb99562013-05-30 17:02:20 -0400261 }
262
263 } catch (IndexOutOfBoundsException e) {
264 Log.e(TAG, e.toString());
265 }
266 }
267 }
268 }
Adrien Béraud04463092013-05-06 14:17:22 +1000269
alisione38001f2013-06-04 14:14:39 -0400270 @Override
271 public boolean onTouch(View v, MotionEvent event) {
272 Log.w(TAG, "onTouch " + event.getAction());
273
274 int action = event.getActionMasked();
275
276 if (action == MotionEvent.ACTION_UP) {
277 if (thread.suspendFlag) {
278 Log.i(TAG, "Relaunch drawing thread");
279 thread.setPaused(false);
280 }
281
282 List<Bubble> bubbles = model.getBubbles();
283 final int n_bubbles = bubbles.size();
284 for (int i = 0; i < n_bubbles; i++) {
285 Bubble b = bubbles.get(i);
286 if (b.dragged) {
287 b.dragged = false;
288 b.target_scale = 1.f;
289 }
290 }
291 dragging_bubble = false;
292 } else if (action != MotionEvent.ACTION_DOWN && !isDraggingBubble() && !thread.suspendFlag) {
293
294 Log.i(TAG, "Not dragging thread should be stopped");
295 thread.setPaused(true);
296 // thread.holdDrawing();
297 }
298
299 return gDetector.onTouchEvent(event);
300 }
301
alision2ec64f92013-06-17 17:28:58 -0400302 RectF intern;
303 RectF external;
304
alision58356b72013-06-03 17:13:36 -0400305 class MyOnGestureListener implements OnGestureListener {
306 @Override
307 public boolean onDown(MotionEvent event) {
308 List<Bubble> bubbles = model.getBubbles();
309 final int n_bubbles = bubbles.size();
alision2ec64f92013-06-17 17:28:58 -0400310 Bubble expand = getExpandedBubble();
311 if (expand != null && !expand.intersects(event.getX(), event.getY())) {
312
313 expand.retract();
314 }
alision58356b72013-06-03 17:13:36 -0400315 Log.d("Main", "onDown");
316 for (int i = 0; i < n_bubbles; i++) {
317 Bubble b = bubbles.get(i);
318 if (b.intersects(event.getX(), event.getY())) {
319 b.dragged = true;
320 b.last_drag = System.nanoTime();
321 b.setPos(event.getX(), event.getY());
322 b.target_scale = .8f;
323 dragging_bubble = true;
324 }
325 }
326 return true;
327 }
328
alision2ec64f92013-06-17 17:28:58 -0400329 private Bubble getExpandedBubble() {
330 List<Bubble> bubbles = model.getBubbles();
331 final int n_bubbles = bubbles.size();
332 for (int i = 0; i < n_bubbles; i++) {
333 Bubble b = bubbles.get(i);
334 if (b.expanded) {
335 return b;
336 }
337 }
338 return null;
339 }
340
alision58356b72013-06-03 17:13:36 -0400341 @Override
342 public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
343 Log.d("Main", "onFling");
344 return true;
345 }
346
347 @Override
348 public void onLongPress(MotionEvent e) {
349 Log.d("Main", "onLongPress");
alision2ec64f92013-06-17 17:28:58 -0400350 if (isDraggingBubble()) {
351 Bubble b = getDraggedBubble(e);
352
353 calculateBounds(b.getPos(), b.getRadius());
354
355 b.expand(100);
356 }
357 }
358
359 private void calculateBounds(PointF pointF, float radius) {
360 // intern = new RectF();
361 // float half_square = (float) (radius / Math.sqrt(2));
362 // intern.set(pointF.x - half_square, pointF.y - half_square, pointF.x + half_square, pointF.y + half_square);
363
364 external = new RectF();
365 float large_half_square = 200;
366 external.set(pointF.x - large_half_square, pointF.y - large_half_square, pointF.x + large_half_square, pointF.y + large_half_square);
367
368 }
369
370 private Bubble getDraggedBubble(MotionEvent e) {
371 List<Bubble> bubbles = model.getBubbles();
372 final int n_bubbles = bubbles.size();
373 for (int i = 0; i < n_bubbles; i++) {
374 Bubble b = bubbles.get(i);
375 if (b.intersects(e.getX(), e.getY())) {
376 return b;
377 }
378 }
379 return null;
alision58356b72013-06-03 17:13:36 -0400380 }
381
382 @Override
383 public boolean onScroll(MotionEvent e1, MotionEvent event, float distanceX, float distanceY) {
384 Log.d("Main", "onScroll");
385 List<Bubble> bubbles = model.getBubbles();
386 final int n_bubbles = bubbles.size();
387 long now = System.nanoTime();
388 for (int i = 0; i < n_bubbles; i++) {
389 Bubble b = bubbles.get(i);
390 if (b.dragged) {
391 float x = event.getX(), y = event.getY();
392 float dt = (float) ((now - b.last_drag) / 1000000000.);
393 float dx = x - b.getPosX(), dy = y - b.getPosY();
394 b.last_drag = now;
395
396 b.setPos(event.getX(), event.getY());
397 /*
398 * int hn = event.getHistorySize() - 2; Log.w(TAG, "event.getHistorySize() : " + event.getHistorySize()); if(hn > 0) { float dx =
399 * x-event.getHistoricalX(hn); float dy = y-event.getHistoricalY(hn); float dt = event.getHistoricalEventTime(hn)/1000.f;
400 */
401 b.speed.x = dx / dt;
402 b.speed.y = dy / dt;
403 // Log.w(TAG, "onTouch dx:" + b.speed.x + " dy:" + b.speed.y);
404 // }
405 return true;
406 }
407 }
408 return true;
409 }
410
411 @Override
412 public void onShowPress(MotionEvent e) {
413 Log.d("Main", "onShowPress");
414
415 }
416
417 @Override
418 public boolean onSingleTapUp(MotionEvent e) {
419 Log.d("Main", "onSingleTapUp");
420 return true;
421 }
422 }
423
alisione38001f2013-06-04 14:14:39 -0400424 public void restartDrawing() {
425 if (thread != null && thread.suspendFlag) {
426 Log.i(TAG, "Relaunch drawing thread");
427 thread.setPaused(false);
428 }
429 }
430
alisionfe9cf712013-05-03 17:26:08 -0400431}