blob: 7013e2303858da6289a343495d7d3b1cdc2b618d [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
Alexandre Lision064e1e02013-10-01 16:18:42 -040033package org.sflphone.model;
alisionfe9cf712013-05-03 17:26:08 -040034
Adrien Béraud0c9bd8f2013-05-30 16:16:57 +100035import java.util.List;
36
Alexandre Lision064e1e02013-10-01 16:18:42 -040037import org.sflphone.R;
Alexandre Lision064e1e02013-10-01 16:18:42 -040038import org.sflphone.fragments.CallFragment;
39
alisionfe9cf712013-05-03 17:26:08 -040040import android.content.Context;
Alexandre Lision940a9db2013-11-19 17:12:02 -050041import android.graphics.Bitmap;
42import android.graphics.BitmapFactory;
alisionfe9cf712013-05-03 17:26:08 -040043import android.graphics.Canvas;
Adrien Béraud33268882013-05-18 03:41:15 +100044import android.graphics.Paint;
Adrien Béraud6bbce912013-05-24 00:48:13 +100045import android.graphics.Paint.Align;
Alexandre Lision23628c12013-09-24 11:17:05 -040046import android.graphics.Paint.Style;
Alexandre Lision68855472013-10-10 16:20:46 -040047import android.graphics.PixelFormat;
Alexandre Lision940a9db2013-11-19 17:12:02 -050048import android.graphics.PorterDuff.Mode;
49import android.graphics.PorterDuffXfermode;
Alexandre Lision0edf18c2013-09-23 17:35:50 -040050import android.graphics.RectF;
Adrien Béraud04463092013-05-06 14:17:22 +100051import android.os.Handler;
52import android.os.Message;
Alexandre Lision68855472013-10-10 16:20:46 -040053import android.os.RemoteException;
alisionfe9cf712013-05-03 17:26:08 -040054import android.util.AttributeSet;
55import android.util.Log;
alision58356b72013-06-03 17:13:36 -040056import android.view.GestureDetector;
57import android.view.GestureDetector.OnGestureListener;
alisionfe9cf712013-05-03 17:26:08 -040058import android.view.MotionEvent;
Adrien Béraud04463092013-05-06 14:17:22 +100059import android.view.SurfaceHolder;
60import android.view.SurfaceView;
alisionfe9cf712013-05-03 17:26:08 -040061import android.view.View;
Adrien Béraud04463092013-05-06 14:17:22 +100062import android.view.View.OnTouchListener;
alisionfe9cf712013-05-03 17:26:08 -040063
alision2cb99562013-05-30 17:02:20 -040064public class BubblesView extends SurfaceView implements SurfaceHolder.Callback, OnTouchListener {
65 private static final String TAG = BubblesView.class.getSimpleName();
alisionfe9cf712013-05-03 17:26:08 -040066
alision2cb99562013-05-30 17:02:20 -040067 private BubblesThread thread = null;
68 private BubbleModel model;
alisionfe9cf712013-05-03 17:26:08 -040069
alision34673e62013-06-25 14:40:07 -040070 private Paint black_name_paint = new Paint(Paint.ANTI_ALIAS_FLAG);
71 private Paint white_name_paint = new Paint(Paint.ANTI_ALIAS_FLAG);
Adrien Béraud6bbce912013-05-24 00:48:13 +100072
alisione38001f2013-06-04 14:14:39 -040073 private GestureDetector gDetector;
74
alision2cb99562013-05-30 17:02:20 -040075 private float density;
76 private float textDensity;
Alexandre Lision8ad39592013-11-21 13:23:58 -050077
Alexandre Lision940a9db2013-11-19 17:12:02 -050078 private Bitmap mBackground;
Adrien Béraud33268882013-05-18 03:41:15 +100079
alision2cb99562013-05-30 17:02:20 -040080 private boolean dragging_bubble = false;
Adrien Béraudc9c424d2013-05-30 17:47:35 +100081
alision34673e62013-06-25 14:40:07 -040082 private CallFragment callback;
83
alision2cb99562013-05-30 17:02:20 -040084 public BubblesView(Context context, AttributeSet attrs) {
85 super(context, attrs);
alisionfe9cf712013-05-03 17:26:08 -040086
Alexandre Lision940a9db2013-11-19 17:12:02 -050087 Bitmap tmp = BitmapFactory.decodeResource(getResources(), R.drawable.bg_72);
Alexandre Lision8ad39592013-11-21 13:23:58 -050088 mBackground = Bitmap.createScaledBitmap(tmp, getResources().getDisplayMetrics().widthPixels, getResources().getDisplayMetrics().heightPixels,
89 false);
alision2cb99562013-05-30 17:02:20 -040090 density = getResources().getDisplayMetrics().density;
91 textDensity = getResources().getDisplayMetrics().scaledDensity;
Adrien Béraud6bbce912013-05-24 00:48:13 +100092
alision2cb99562013-05-30 17:02:20 -040093 SurfaceHolder holder = getHolder();
94 holder.addCallback(this);
alisionfe9cf712013-05-03 17:26:08 -040095
Alexandre Lision68855472013-10-10 16:20:46 -040096 this.setZOrderOnTop(true); // necessary
97 this.getHolder().setFormat(PixelFormat.TRANSLUCENT);
alision2cb99562013-05-30 17:02:20 -040098 // create thread only; it's started in surfaceCreated()
99 createThread();
alisionfe9cf712013-05-03 17:26:08 -0400100
alision2cb99562013-05-30 17:02:20 -0400101 setOnTouchListener(this);
102 setFocusable(true);
Adrien Béraud33268882013-05-18 03:41:15 +1000103
alision34673e62013-06-25 14:40:07 -0400104 black_name_paint.setTextSize(18 * textDensity);
105 black_name_paint.setColor(0xFF303030);
106 black_name_paint.setTextAlign(Align.CENTER);
107
108 white_name_paint.setTextSize(18 * textDensity);
109 white_name_paint.setColor(0xFFEEEEEE);
110 white_name_paint.setTextAlign(Align.CENTER);
alision58356b72013-06-03 17:13:36 -0400111
112 gDetector = new GestureDetector(getContext(), new MyOnGestureListener());
alision2cb99562013-05-30 17:02:20 -0400113 }
alisionfe9cf712013-05-03 17:26:08 -0400114
alision2cb99562013-05-30 17:02:20 -0400115 private void createThread() {
116 if (thread != null)
117 return;
118 thread = new BubblesThread(getHolder(), getContext(), new Handler() {
119 @Override
120 public void handleMessage(Message m) {
121 /*
122 * mStatusText.setVisibility(m.getData().getInt("viz")); mStatusText.setText(m.getData().getString("text"));
123 */
124 }
125 });
126 if (model != null)
127 thread.setModel(model);
128 }
alisionfe9cf712013-05-03 17:26:08 -0400129
alision2cb99562013-05-30 17:02:20 -0400130 public void setModel(BubbleModel model) {
131 this.model = model;
132 thread.setModel(model);
133 }
alisionfe9cf712013-05-03 17:26:08 -0400134
alision2cb99562013-05-30 17:02:20 -0400135 /*
136 * @Override public void onWindowFocusChanged(boolean hasWindowFocus) { if (!hasWindowFocus) { thread.pause(); } }
137 */
alisionfe9cf712013-05-03 17:26:08 -0400138
alision2cb99562013-05-30 17:02:20 -0400139 @Override
140 public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
141 Log.w(TAG, "surfaceChanged " + width + "-" + height);
Alexandre Lisiond588bff2013-10-08 12:43:01 -0400142 if (height < model.height) // probably showing the keyboard, don't move!
Alexandre Lision84208a32013-09-25 13:18:37 -0400143 return;
144
alision2cb99562013-05-30 17:02:20 -0400145 thread.setSurfaceSize(width, height);
146 }
alisionfe9cf712013-05-03 17:26:08 -0400147
alision2cb99562013-05-30 17:02:20 -0400148 /*
149 * Callback invoked when the Surface has been created and is ready to be used.
150 */
151 @Override
152 public void surfaceCreated(SurfaceHolder holder) {
153 // start the thread here so that we don't busy-wait in run()
154 // waiting for the surface to be created
155 createThread();
alisionfe9cf712013-05-03 17:26:08 -0400156
alision2cb99562013-05-30 17:02:20 -0400157 Log.w(TAG, "surfaceCreated");
158 thread.setRunning(true);
159 thread.start();
160 }
alisionfe9cf712013-05-03 17:26:08 -0400161
alision2cb99562013-05-30 17:02:20 -0400162 /*
163 * Callback invoked when the Surface has been destroyed and must no longer be touched. WARNING: after this method returns, the Surface/Canvas must
164 * never be touched again!
165 */
166 @Override
167 public void surfaceDestroyed(SurfaceHolder holder) {
168 // we have to tell thread to shut down & wait for it to finish, or else
169 // it might touch the Surface after we return and explode
170 Log.w(TAG, "surfaceDestroyed");
171 boolean retry = true;
172 thread.setRunning(false);
alision1005ba12013-06-19 13:52:44 -0400173 thread.setPaused(false);
alision2cb99562013-05-30 17:02:20 -0400174 while (retry) {
175 try {
alision1005ba12013-06-19 13:52:44 -0400176 Log.w(TAG, "joining...");
alision2cb99562013-05-30 17:02:20 -0400177 thread.join();
178 retry = false;
179 } catch (InterruptedException e) {
180 }
181 }
alision1005ba12013-06-19 13:52:44 -0400182 Log.w(TAG, "done");
alision2cb99562013-05-30 17:02:20 -0400183 thread = null;
184 }
alisionfe9cf712013-05-03 17:26:08 -0400185
alision2cb99562013-05-30 17:02:20 -0400186 public boolean isDraggingBubble() {
187 return dragging_bubble;
188 }
Adrien Béraudc9c424d2013-05-30 17:47:35 +1000189
alision2cb99562013-05-30 17:02:20 -0400190 class BubblesThread extends Thread {
191 private boolean running = false;
192 private SurfaceHolder surfaceHolder;
alisione38001f2013-06-04 14:14:39 -0400193 public Boolean suspendFlag = false;
alisionfe9cf712013-05-03 17:26:08 -0400194
alision2cb99562013-05-30 17:02:20 -0400195 BubbleModel model = null;
alisionfe9cf712013-05-03 17:26:08 -0400196
alision2cb99562013-05-30 17:02:20 -0400197 public BubblesThread(SurfaceHolder holder, Context context, Handler handler) {
198 surfaceHolder = holder;
199 }
alisionfe9cf712013-05-03 17:26:08 -0400200
alision2cb99562013-05-30 17:02:20 -0400201 public void setModel(BubbleModel model) {
202 this.model = model;
203 }
alisionfe9cf712013-05-03 17:26:08 -0400204
alision2cb99562013-05-30 17:02:20 -0400205 @Override
206 public void run() {
207 while (running) {
208 Canvas c = null;
209 try {
Adrien Béraud7ed23dc2013-05-06 16:27:24 +1000210
alisione38001f2013-06-04 14:14:39 -0400211 if (suspendFlag) {
212 synchronized (this) {
213 while (suspendFlag) {
214 try {
215 wait();
216 } catch (InterruptedException e) {
217 // TODO Auto-generated catch block
218 e.printStackTrace();
219 }
220 }
221 }
222 } else {
alisione38001f2013-06-04 14:14:39 -0400223 c = surfaceHolder.lockCanvas(null);
Adrien Béraud7ed23dc2013-05-06 16:27:24 +1000224
alisione38001f2013-06-04 14:14:39 -0400225 // for the case the surface is destroyed while already in the loop
226 if (c == null || model == null)
227 continue;
228
229 synchronized (surfaceHolder) {
230 // Log.w(TAG, "Thread doDraw");
231 model.update();
232 doDraw(c);
233 }
alision2cb99562013-05-30 17:02:20 -0400234 }
alisione38001f2013-06-04 14:14:39 -0400235
alision2cb99562013-05-30 17:02:20 -0400236 } finally {
237 if (c != null)
238 surfaceHolder.unlockCanvasAndPost(c);
239 }
240 }
241 }
Adrien Béraud04463092013-05-06 14:17:22 +1000242
alisione38001f2013-06-04 14:14:39 -0400243 public void setPaused(boolean wantToPause) {
244 synchronized (this) {
245 suspendFlag = wantToPause;
246 notify();
247 }
248 }
249
alision2cb99562013-05-30 17:02:20 -0400250 public void setRunning(boolean b) {
251 running = b;
252 }
Adrien Béraud04463092013-05-06 14:17:22 +1000253
alision2cb99562013-05-30 17:02:20 -0400254 public void setSurfaceSize(int width, int height) {
255 synchronized (surfaceHolder) {
256 if (model != null) {
257 model.width = width;
258 model.height = height;
259 }
260 }
261 }
Adrien Béraud04463092013-05-06 14:17:22 +1000262
alision2cb99562013-05-30 17:02:20 -0400263 /**
Alexandre Lision8ad39592013-11-21 13:23:58 -0500264 * got multiple IndexOutOfBoundsException, when switching calls. //FIXME
alision2cb99562013-05-30 17:02:20 -0400265 *
266 * @param canvas
267 */
268 private void doDraw(Canvas canvas) {
Adrien Béraud04463092013-05-06 14:17:22 +1000269
alision2cb99562013-05-30 17:02:20 -0400270 synchronized (model) {
271 List<Bubble> bubbles = model.getBubbles();
272 List<Attractor> attractors = model.getAttractors();
Alexandre Lision0edf18c2013-09-23 17:35:50 -0400273
274 Paint tryMe = new Paint();
Alexandre Lision8ad39592013-11-21 13:23:58 -0500275
Alexandre Lision940a9db2013-11-19 17:12:02 -0500276 Paint paint = new Paint();
277 paint.setXfermode(new PorterDuffXfermode(Mode.CLEAR));
278 canvas.drawPaint(paint);
279 paint.setXfermode(new PorterDuffXfermode(Mode.SRC));
Alexandre Lision0edf18c2013-09-23 17:35:50 -0400280
Alexandre Lision8ad39592013-11-21 13:23:58 -0500281 // canvas.drawColor(Color.LTGRAY);
Alexandre Lision0edf18c2013-09-23 17:35:50 -0400282
283 if (dragging_bubble) {
Alexandre Lision0edf18c2013-09-23 17:35:50 -0400284 Paint p = new Paint();
285 p.setDither(true);
Alexandre Lision23628c12013-09-24 11:17:05 -0400286 p.setColor(getResources().getColor(R.color.holo_red_light));
Alexandre Lision23628c12013-09-24 11:17:05 -0400287 p.setStyle(Style.STROKE);
Alexandre Lision23628c12013-09-24 11:17:05 -0400288 p.setStrokeWidth(20);
Alexandre Lision23628c12013-09-24 11:17:05 -0400289 canvas.drawRect(new RectF(10, 10, model.width - 10, model.height - 10), p);
Alexandre Lision0edf18c2013-09-23 17:35:50 -0400290 }
291
292 tryMe.setStyle(Paint.Style.STROKE);
293 tryMe.setColor(getResources().getColor(R.color.darker_gray));
294 tryMe.setXfermode(null);
295 canvas.drawCircle(model.width / 2, model.height / 2, model.width / 2 - getResources().getDimension(R.dimen.bubble_size), tryMe);
296
alision2cb99562013-05-30 17:02:20 -0400297 try {
Adrien Béraud0c9bd8f2013-05-30 16:16:57 +1000298
alision2cb99562013-05-30 17:02:20 -0400299 for (int i = 0, n = attractors.size(); i < n; i++) {
300 Attractor a = attractors.get(i);
301 canvas.drawBitmap(a.getBitmap(), null, a.getBounds(), null);
302 }
Adrien Béraud33268882013-05-18 03:41:15 +1000303
alision2cb99562013-05-30 17:02:20 -0400304 for (int i = 0, n = bubbles.size(); i < n; i++) {
305 Bubble b = bubbles.get(i);
Alexandre Lision0edf18c2013-09-23 17:35:50 -0400306 if (b.expanded) {
alision465ceba2013-07-04 09:24:30 -0400307 continue;
308 }
alision2cb99562013-05-30 17:02:20 -0400309 canvas.drawBitmap(b.getBitmap(), null, b.getBounds(), null);
Alexandre Lision68855472013-10-10 16:20:46 -0400310 canvas.drawText(b.getName(), b.getPosX(), (float) (b.getPosY() - b.getRetractedRadius() * 1.2 * density), getNamePaint(b));
alision34673e62013-06-25 14:40:07 -0400311 }
Alexandre Lision0edf18c2013-09-23 17:35:50 -0400312
alision34673e62013-06-25 14:40:07 -0400313 Bubble first_plan = getExpandedBubble();
314 if (first_plan != null) {
Alexandre Lisiond588bff2013-10-08 12:43:01 -0400315
Alexandre Lision40954dc2013-10-09 15:24:03 -0400316 if (first_plan.getDrawerBitmap() != null) {
317 canvas.drawBitmap(first_plan.getDrawerBitmap(), null, first_plan.getDrawerBounds(), null);
Alexandre Lision68855472013-10-10 16:20:46 -0400318 }
alision34673e62013-06-25 14:40:07 -0400319 canvas.drawBitmap(first_plan.getBitmap(), null, first_plan.getBounds(), null);
Alexandre Lision40954dc2013-10-09 15:24:03 -0400320 // canvas.drawText(first_plan.associated_call.getContact().getmDisplayName(), first_plan.getPosX(),
321 // (float) (first_plan.getPosY() - first_plan.getRetractedRadius() * 1.2 * density), getNamePaint(first_plan));
alisiondf1dac92013-06-27 17:35:53 -0400322
alision2cb99562013-05-30 17:02:20 -0400323 }
324
325 } catch (IndexOutOfBoundsException e) {
326 Log.e(TAG, e.toString());
327 }
328 }
329 }
alision34673e62013-06-25 14:40:07 -0400330
331 }
332
333 private Paint getNamePaint(Bubble b) {
alision50fa0722013-06-25 17:29:44 -0400334 if (b.expanded) {
335 white_name_paint.setTextSize(15 * b.target_scale * textDensity);
alision34673e62013-06-25 14:40:07 -0400336 return white_name_paint;
alision50fa0722013-06-25 17:29:44 -0400337 }
338 black_name_paint.setTextSize(18 * b.target_scale * textDensity);
alision34673e62013-06-25 14:40:07 -0400339 return black_name_paint;
alision2cb99562013-05-30 17:02:20 -0400340 }
Adrien Béraud04463092013-05-06 14:17:22 +1000341
alisione38001f2013-06-04 14:14:39 -0400342 @Override
343 public boolean onTouch(View v, MotionEvent event) {
Alexandre Lision6deda412013-09-25 13:21:22 -0400344 // Log.w(TAG, "onTouch " + event.getAction());
alisione38001f2013-06-04 14:14:39 -0400345
346 int action = event.getActionMasked();
347
Alexandre Lision1888bf32013-10-31 09:51:41 -0400348 if (gDetector.onTouchEvent(event))
349 return true;
350
alisione38001f2013-06-04 14:14:39 -0400351 if (action == MotionEvent.ACTION_UP) {
352 if (thread.suspendFlag) {
353 Log.i(TAG, "Relaunch drawing thread");
354 thread.setPaused(false);
355 }
356
Alexandre Lision5cd659e2013-10-29 13:18:23 -0400357 Bubble expand = getExpandedBubble();
358 if (expand != null) {
Alexandre Lision5cd659e2013-10-29 13:18:23 -0400359 switch (expand.getDrawer().getAction(event.getX(), event.getY())) {
360 case Bubble.actions.OUT_OF_BOUNDS:
361 expand.retract();
362 break;
363 case Bubble.actions.HOLD:
364
365 try {
366 if (expand.getHoldStatus()) {
367
368 if (expand.isConference())
369 callback.mCallbacks.getService().unholdConference(expand.getCallID());
370 else
371 callback.mCallbacks.getService().unhold(expand.getCallID());
372 } else {
373 if (expand.isConference())
374 callback.mCallbacks.getService().holdConference(expand.getCallID());
375 else
376 callback.mCallbacks.getService().hold(expand.getCallID());
377
378 }
379 } catch (Exception e) {
380 e.printStackTrace();
381 }
382
383 return true;
384 case Bubble.actions.RECORD:
385 try {
Alexandre Lision3874e552013-11-04 17:20:12 -0500386 boolean isRecording = callback.mCallbacks.getService().toggleRecordingCall(expand.getCallID());
387 ((BubbleUser) expand).associated_call.setRecording(isRecording);
Alexandre Lision5cd659e2013-10-29 13:18:23 -0400388 } catch (RemoteException e1) {
389 e1.printStackTrace();
390 }
391 return true;
392 case Bubble.actions.MESSAGE:
393 // TODO
394 return true;
Alexandre Lisiond9c3f7f2013-10-30 15:20:55 -0400395 case Bubble.actions.MUTE:
Alexandre Lisionc9fca9e2013-11-08 15:21:18 -0500396 try {
397 callback.mCallbacks.getService().setMuted(!((BubbleUser) expand).getMute());
398 ((BubbleUser) expand).toggleMute();
399 } catch (RemoteException e1) {
400 e1.printStackTrace();
401 }
Alexandre Lisiond9c3f7f2013-10-30 15:20:55 -0400402 return true;
Alexandre Lision5cd659e2013-10-29 13:18:23 -0400403 case Bubble.actions.HANGUP:
404 try {
405 if (expand.isConference())
406 callback.mCallbacks.getService().hangUpConference(expand.getCallID());
407 else
408 callback.mCallbacks.getService().hangUp(expand.getCallID());
409 } catch (RemoteException e) {
410 e.printStackTrace();
411 }
412 return true;
413
414 case Bubble.actions.TRANSFER:
415 callback.makeTransfer((BubbleContact) expand);
416 return true;
417 case Bubble.actions.NOTHING:
418 break;
419 }
Alexandre Lision5cd659e2013-10-29 13:18:23 -0400420
Alexandre Lisiond9c3f7f2013-10-30 15:20:55 -0400421 }
Alexandre Lision5cd659e2013-10-29 13:18:23 -0400422
alisione38001f2013-06-04 14:14:39 -0400423 List<Bubble> bubbles = model.getBubbles();
424 final int n_bubbles = bubbles.size();
425 for (int i = 0; i < n_bubbles; i++) {
426 Bubble b = bubbles.get(i);
427 if (b.dragged) {
428 b.dragged = false;
429 b.target_scale = 1.f;
Alexandre Lisionfdef8852013-09-24 13:32:38 -0400430 if (b.isOnBorder(model.width, model.height) && !b.expanded) {
Alexandre Lision0edf18c2013-09-23 17:35:50 -0400431 b.markedToDie = true;
Alexandre Lision68855472013-10-10 16:20:46 -0400432
433 try {
434 if (b.isConference())
435 callback.mCallbacks.getService().hangUpConference(b.getCallID());
436 else
437 callback.mCallbacks.getService().hangUp(b.getCallID());
438
439 } catch (RemoteException e) {
440 e.printStackTrace();
441 }
Alexandre Lision0edf18c2013-09-23 17:35:50 -0400442 }
alisione38001f2013-06-04 14:14:39 -0400443 }
444 }
445 dragging_bubble = false;
446 } else if (action != MotionEvent.ACTION_DOWN && !isDraggingBubble() && !thread.suspendFlag) {
447
448 Log.i(TAG, "Not dragging thread should be stopped");
449 thread.setPaused(true);
450 // thread.holdDrawing();
451 }
452
Alexandre Lision1888bf32013-10-31 09:51:41 -0400453 return true;
alisione38001f2013-06-04 14:14:39 -0400454 }
455
alision34673e62013-06-25 14:40:07 -0400456 private Bubble getExpandedBubble() {
457 List<Bubble> bubbles = model.getBubbles();
458 final int n_bubbles = bubbles.size();
459 for (int i = 0; i < n_bubbles; i++) {
460 Bubble b = bubbles.get(i);
461 if (b.expanded) {
462 return b;
463 }
464 }
465 return null;
466 }
alision2ec64f92013-06-17 17:28:58 -0400467
alisiondf1dac92013-06-27 17:35:53 -0400468 public void restartDrawing() {
469 if (thread != null && thread.suspendFlag) {
470 Log.i(TAG, "Relaunch drawing thread");
471 thread.setPaused(false);
472 }
473 }
474
475 public void setFragment(CallFragment callFragment) {
476 callback = callFragment;
477
478 }
479
480 public void stopThread() {
481 if (thread != null && thread.suspendFlag) {
482 Log.i(TAG, "Stop drawing thread");
Alexandre Lision666b3772013-10-28 17:42:48 -0400483 thread.setPaused(true);
alisiondf1dac92013-06-27 17:35:53 -0400484 }
485
486 }
487
alision58356b72013-06-03 17:13:36 -0400488 class MyOnGestureListener implements OnGestureListener {
489 @Override
490 public boolean onDown(MotionEvent event) {
491 List<Bubble> bubbles = model.getBubbles();
Alexandre Lision1888bf32013-10-31 09:51:41 -0400492
Alexandre Lision8fdcf2b2013-10-30 16:06:51 -0400493 Bubble target = getExpandedBubble();
Alexandre Lision1888bf32013-10-31 09:51:41 -0400494 if (target != null) {
Alexandre Lision8fdcf2b2013-10-30 16:06:51 -0400495 target.onDown(event);
496 return true;
497 }
Alexandre Lision68855472013-10-10 16:20:46 -0400498
Alexandre Lisiond9c3f7f2013-10-30 15:20:55 -0400499 for (int i = 0; i < bubbles.size(); i++) {
alision58356b72013-06-03 17:13:36 -0400500 Bubble b = bubbles.get(i);
Alexandre Lision1888bf32013-10-31 09:51:41 -0400501 if (b.onDown(event))
502 dragging_bubble = true;
alision58356b72013-06-03 17:13:36 -0400503 }
Alexandre Lisiond9c3f7f2013-10-30 15:20:55 -0400504
alision58356b72013-06-03 17:13:36 -0400505 return true;
506 }
alision2ec64f92013-06-17 17:28:58 -0400507
alision58356b72013-06-03 17:13:36 -0400508 @Override
509 public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
Alexandre Lision6deda412013-09-25 13:21:22 -0400510 // Log.d("Main", "onFling");
Alexandre Lision1888bf32013-10-31 09:51:41 -0400511 return false;
alision58356b72013-06-03 17:13:36 -0400512 }
513
514 @Override
515 public void onLongPress(MotionEvent e) {
Alexandre Lision6deda412013-09-25 13:21:22 -0400516 // Log.d("Main", "onLongPress");
Alexandre Lision1888bf32013-10-31 09:51:41 -0400517
alision2ec64f92013-06-17 17:28:58 -0400518 }
519
alision2ec64f92013-06-17 17:28:58 -0400520 private Bubble getDraggedBubble(MotionEvent e) {
521 List<Bubble> bubbles = model.getBubbles();
522 final int n_bubbles = bubbles.size();
523 for (int i = 0; i < n_bubbles; i++) {
524 Bubble b = bubbles.get(i);
525 if (b.intersects(e.getX(), e.getY())) {
526 return b;
527 }
528 }
529 return null;
alision58356b72013-06-03 17:13:36 -0400530 }
531
532 @Override
533 public boolean onScroll(MotionEvent e1, MotionEvent event, float distanceX, float distanceY) {
Alexandre Lision6deda412013-09-25 13:21:22 -0400534 // Log.d("Main", "onScroll");
alision58356b72013-06-03 17:13:36 -0400535 List<Bubble> bubbles = model.getBubbles();
Alexandre Lisiond9c3f7f2013-10-30 15:20:55 -0400536
alision58356b72013-06-03 17:13:36 -0400537 long now = System.nanoTime();
Alexandre Lisiond9c3f7f2013-10-30 15:20:55 -0400538 for (int i = 0; i < bubbles.size(); i++) {
alision58356b72013-06-03 17:13:36 -0400539 Bubble b = bubbles.get(i);
540 if (b.dragged) {
541 float x = event.getX(), y = event.getY();
542 float dt = (float) ((now - b.last_drag) / 1000000000.);
543 float dx = x - b.getPosX(), dy = y - b.getPosY();
544 b.last_drag = now;
alision58356b72013-06-03 17:13:36 -0400545 b.setPos(event.getX(), event.getY());
alision58356b72013-06-03 17:13:36 -0400546 b.speed.x = dx / dt;
547 b.speed.y = dy / dt;
Alexandre Lisiond9c3f7f2013-10-30 15:20:55 -0400548
Alexandre Lision1888bf32013-10-31 09:51:41 -0400549 return false;
alision58356b72013-06-03 17:13:36 -0400550 }
551 }
552 return true;
553 }
554
555 @Override
556 public void onShowPress(MotionEvent e) {
Alexandre Lision6deda412013-09-25 13:21:22 -0400557 // Log.d("Main", "onShowPress");
alision58356b72013-06-03 17:13:36 -0400558
559 }
560
561 @Override
562 public boolean onSingleTapUp(MotionEvent e) {
Alexandre Lision1888bf32013-10-31 09:51:41 -0400563 if (isDraggingBubble() && callback.getConference().isOnGoing()) {
564 Bubble b = getDraggedBubble(e);
565 b.expand(model.width, model.height);
Alexandre Lision1a682572013-11-01 11:21:17 -0400566 dragging_bubble = false;
Alexandre Lision1888bf32013-10-31 09:51:41 -0400567 }
568 return false;
569
alision58356b72013-06-03 17:13:36 -0400570 }
571 }
alisionfe9cf712013-05-03 17:26:08 -0400572}