blob: ca44ee367de1a0c1a84b8478d7aa8b4975feab92 [file] [log] [blame]
alisioncc7bb422013-06-06 15:31:39 -04001/*
Alexandre Lisionc1024c02014-01-06 11:12:53 -05002 * Copyright (C) 2004-2014 Savoir-Faire Linux Inc.
alisioncc7bb422013-06-06 15:31:39 -04003 *
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 Lision2cd1ce42014-01-07 17:18:58 -050037import android.opengl.GLSurfaceView;
Alexandre Lision064e1e02013-10-01 16:18:42 -040038import org.sflphone.R;
Alexandre Lision064e1e02013-10-01 16:18:42 -040039import org.sflphone.fragments.CallFragment;
40
alisionfe9cf712013-05-03 17:26:08 -040041import android.content.Context;
42import android.graphics.Canvas;
Adrien Béraud33268882013-05-18 03:41:15 +100043import android.graphics.Paint;
Adrien Béraud6bbce912013-05-24 00:48:13 +100044import android.graphics.Paint.Align;
Alexandre Lision23628c12013-09-24 11:17:05 -040045import android.graphics.Paint.Style;
Alexandre Lision68855472013-10-10 16:20:46 -040046import android.graphics.PixelFormat;
Alexandre Lision940a9db2013-11-19 17:12:02 -050047import android.graphics.PorterDuff.Mode;
48import android.graphics.PorterDuffXfermode;
Alexandre Lision0edf18c2013-09-23 17:35:50 -040049import android.graphics.RectF;
Alexandre Lision68855472013-10-10 16:20:46 -040050import android.os.RemoteException;
alisionfe9cf712013-05-03 17:26:08 -040051import android.util.AttributeSet;
52import android.util.Log;
alision58356b72013-06-03 17:13:36 -040053import android.view.GestureDetector;
54import android.view.GestureDetector.OnGestureListener;
alisionfe9cf712013-05-03 17:26:08 -040055import android.view.MotionEvent;
Adrien Béraud04463092013-05-06 14:17:22 +100056import android.view.SurfaceHolder;
57import android.view.SurfaceView;
alisionfe9cf712013-05-03 17:26:08 -040058import android.view.View;
Adrien Béraud04463092013-05-06 14:17:22 +100059import android.view.View.OnTouchListener;
alisionfe9cf712013-05-03 17:26:08 -040060
Alexandre Lision2cd1ce42014-01-07 17:18:58 -050061public class BubblesView extends GLSurfaceView implements SurfaceHolder.Callback, OnTouchListener {
alision2cb99562013-05-30 17:02:20 -040062 private static final String TAG = BubblesView.class.getSimpleName();
alisionfe9cf712013-05-03 17:26:08 -040063
alision2cb99562013-05-30 17:02:20 -040064 private BubblesThread thread = null;
65 private BubbleModel model;
alisionfe9cf712013-05-03 17:26:08 -040066
alision34673e62013-06-25 14:40:07 -040067 private Paint black_name_paint = new Paint(Paint.ANTI_ALIAS_FLAG);
68 private Paint white_name_paint = new Paint(Paint.ANTI_ALIAS_FLAG);
Adrien Béraud6bbce912013-05-24 00:48:13 +100069
alisione38001f2013-06-04 14:14:39 -040070 private GestureDetector gDetector;
71
alision2cb99562013-05-30 17:02:20 -040072 private float density;
73 private float textDensity;
Alexandre Lision8ad39592013-11-21 13:23:58 -050074
alision2cb99562013-05-30 17:02:20 -040075 private boolean dragging_bubble = false;
Adrien Béraudc9c424d2013-05-30 17:47:35 +100076
alision34673e62013-06-25 14:40:07 -040077 private CallFragment callback;
78
alision2cb99562013-05-30 17:02:20 -040079 public BubblesView(Context context, AttributeSet attrs) {
80 super(context, attrs);
alisionfe9cf712013-05-03 17:26:08 -040081
alision2cb99562013-05-30 17:02:20 -040082 density = getResources().getDisplayMetrics().density;
83 textDensity = getResources().getDisplayMetrics().scaledDensity;
Adrien Béraud6bbce912013-05-24 00:48:13 +100084
alision2cb99562013-05-30 17:02:20 -040085 SurfaceHolder holder = getHolder();
86 holder.addCallback(this);
alisionfe9cf712013-05-03 17:26:08 -040087
Alexandre Lision68855472013-10-10 16:20:46 -040088 this.setZOrderOnTop(true); // necessary
89 this.getHolder().setFormat(PixelFormat.TRANSLUCENT);
alision2cb99562013-05-30 17:02:20 -040090 // create thread only; it's started in surfaceCreated()
91 createThread();
alisionfe9cf712013-05-03 17:26:08 -040092
alision2cb99562013-05-30 17:02:20 -040093 setOnTouchListener(this);
94 setFocusable(true);
Adrien Béraud33268882013-05-18 03:41:15 +100095
alision34673e62013-06-25 14:40:07 -040096 black_name_paint.setTextSize(18 * textDensity);
97 black_name_paint.setColor(0xFF303030);
98 black_name_paint.setTextAlign(Align.CENTER);
99
100 white_name_paint.setTextSize(18 * textDensity);
101 white_name_paint.setColor(0xFFEEEEEE);
102 white_name_paint.setTextAlign(Align.CENTER);
alision58356b72013-06-03 17:13:36 -0400103
104 gDetector = new GestureDetector(getContext(), new MyOnGestureListener());
alision2cb99562013-05-30 17:02:20 -0400105 }
alisionfe9cf712013-05-03 17:26:08 -0400106
alision2cb99562013-05-30 17:02:20 -0400107 private void createThread() {
108 if (thread != null)
109 return;
Alexandre Lisionf30ff852013-12-09 17:08:40 -0500110 thread = new BubblesThread(getHolder(), getContext());
alision2cb99562013-05-30 17:02:20 -0400111 if (model != null)
112 thread.setModel(model);
113 }
alisionfe9cf712013-05-03 17:26:08 -0400114
alision2cb99562013-05-30 17:02:20 -0400115 public void setModel(BubbleModel model) {
116 this.model = model;
117 thread.setModel(model);
118 }
alisionfe9cf712013-05-03 17:26:08 -0400119
alision2cb99562013-05-30 17:02:20 -0400120 /*
121 * @Override public void onWindowFocusChanged(boolean hasWindowFocus) { if (!hasWindowFocus) { thread.pause(); } }
122 */
alisionfe9cf712013-05-03 17:26:08 -0400123
alision2cb99562013-05-30 17:02:20 -0400124 @Override
125 public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
126 Log.w(TAG, "surfaceChanged " + width + "-" + height);
Alexandre Lisiond588bff2013-10-08 12:43:01 -0400127 if (height < model.height) // probably showing the keyboard, don't move!
Alexandre Lision84208a32013-09-25 13:18:37 -0400128 return;
129
alision2cb99562013-05-30 17:02:20 -0400130 thread.setSurfaceSize(width, height);
131 }
alisionfe9cf712013-05-03 17:26:08 -0400132
alision2cb99562013-05-30 17:02:20 -0400133 /*
134 * Callback invoked when the Surface has been created and is ready to be used.
135 */
136 @Override
137 public void surfaceCreated(SurfaceHolder holder) {
138 // start the thread here so that we don't busy-wait in run()
139 // waiting for the surface to be created
140 createThread();
alisionfe9cf712013-05-03 17:26:08 -0400141
alision2cb99562013-05-30 17:02:20 -0400142 Log.w(TAG, "surfaceCreated");
143 thread.setRunning(true);
144 thread.start();
145 }
alisionfe9cf712013-05-03 17:26:08 -0400146
alision2cb99562013-05-30 17:02:20 -0400147 /*
148 * Callback invoked when the Surface has been destroyed and must no longer be touched. WARNING: after this method returns, the Surface/Canvas must
149 * never be touched again!
150 */
151 @Override
152 public void surfaceDestroyed(SurfaceHolder holder) {
153 // we have to tell thread to shut down & wait for it to finish, or else
154 // it might touch the Surface after we return and explode
155 Log.w(TAG, "surfaceDestroyed");
156 boolean retry = true;
157 thread.setRunning(false);
alision1005ba12013-06-19 13:52:44 -0400158 thread.setPaused(false);
alision2cb99562013-05-30 17:02:20 -0400159 while (retry) {
160 try {
alision1005ba12013-06-19 13:52:44 -0400161 Log.w(TAG, "joining...");
alision2cb99562013-05-30 17:02:20 -0400162 thread.join();
163 retry = false;
164 } catch (InterruptedException e) {
165 }
166 }
alision1005ba12013-06-19 13:52:44 -0400167 Log.w(TAG, "done");
alision2cb99562013-05-30 17:02:20 -0400168 thread = null;
169 }
alisionfe9cf712013-05-03 17:26:08 -0400170
alision2cb99562013-05-30 17:02:20 -0400171 public boolean isDraggingBubble() {
172 return dragging_bubble;
173 }
Adrien Béraudc9c424d2013-05-30 17:47:35 +1000174
alision2cb99562013-05-30 17:02:20 -0400175 class BubblesThread extends Thread {
176 private boolean running = false;
177 private SurfaceHolder surfaceHolder;
alisione38001f2013-06-04 14:14:39 -0400178 public Boolean suspendFlag = false;
alisionfe9cf712013-05-03 17:26:08 -0400179
alision2cb99562013-05-30 17:02:20 -0400180 BubbleModel model = null;
alisionfe9cf712013-05-03 17:26:08 -0400181
Alexandre Lisionf30ff852013-12-09 17:08:40 -0500182 public BubblesThread(SurfaceHolder holder, Context context) {
alision2cb99562013-05-30 17:02:20 -0400183 surfaceHolder = holder;
184 }
alisionfe9cf712013-05-03 17:26:08 -0400185
alision2cb99562013-05-30 17:02:20 -0400186 public void setModel(BubbleModel model) {
187 this.model = model;
188 }
alisionfe9cf712013-05-03 17:26:08 -0400189
alision2cb99562013-05-30 17:02:20 -0400190 @Override
191 public void run() {
192 while (running) {
193 Canvas c = null;
194 try {
Adrien Béraud7ed23dc2013-05-06 16:27:24 +1000195
alisione38001f2013-06-04 14:14:39 -0400196 if (suspendFlag) {
197 synchronized (this) {
198 while (suspendFlag) {
199 try {
200 wait();
201 } catch (InterruptedException e) {
202 // TODO Auto-generated catch block
203 e.printStackTrace();
204 }
205 }
206 }
207 } else {
alisione38001f2013-06-04 14:14:39 -0400208 c = surfaceHolder.lockCanvas(null);
Adrien Béraud7ed23dc2013-05-06 16:27:24 +1000209
alisione38001f2013-06-04 14:14:39 -0400210 // for the case the surface is destroyed while already in the loop
211 if (c == null || model == null)
212 continue;
213
214 synchronized (surfaceHolder) {
215 // Log.w(TAG, "Thread doDraw");
216 model.update();
217 doDraw(c);
218 }
alision2cb99562013-05-30 17:02:20 -0400219 }
alisione38001f2013-06-04 14:14:39 -0400220
alision2cb99562013-05-30 17:02:20 -0400221 } finally {
222 if (c != null)
223 surfaceHolder.unlockCanvasAndPost(c);
224 }
225 }
226 }
Adrien Béraud04463092013-05-06 14:17:22 +1000227
alisione38001f2013-06-04 14:14:39 -0400228 public void setPaused(boolean wantToPause) {
229 synchronized (this) {
230 suspendFlag = wantToPause;
231 notify();
232 }
233 }
234
alision2cb99562013-05-30 17:02:20 -0400235 public void setRunning(boolean b) {
236 running = b;
237 }
Adrien Béraud04463092013-05-06 14:17:22 +1000238
alision2cb99562013-05-30 17:02:20 -0400239 public void setSurfaceSize(int width, int height) {
240 synchronized (surfaceHolder) {
241 if (model != null) {
242 model.width = width;
243 model.height = height;
244 }
245 }
246 }
Adrien Béraud04463092013-05-06 14:17:22 +1000247
alision2cb99562013-05-30 17:02:20 -0400248 /**
Alexandre Lision8ad39592013-11-21 13:23:58 -0500249 * got multiple IndexOutOfBoundsException, when switching calls. //FIXME
alision2cb99562013-05-30 17:02:20 -0400250 *
251 * @param canvas
252 */
253 private void doDraw(Canvas canvas) {
Adrien Béraud04463092013-05-06 14:17:22 +1000254
alision2cb99562013-05-30 17:02:20 -0400255 synchronized (model) {
256 List<Bubble> bubbles = model.getBubbles();
257 List<Attractor> attractors = model.getAttractors();
Alexandre Lision0edf18c2013-09-23 17:35:50 -0400258
259 Paint tryMe = new Paint();
Alexandre Lision8ad39592013-11-21 13:23:58 -0500260
Alexandre Lision940a9db2013-11-19 17:12:02 -0500261 Paint paint = new Paint();
262 paint.setXfermode(new PorterDuffXfermode(Mode.CLEAR));
263 canvas.drawPaint(paint);
264 paint.setXfermode(new PorterDuffXfermode(Mode.SRC));
Alexandre Lision0edf18c2013-09-23 17:35:50 -0400265
Alexandre Lision8ad39592013-11-21 13:23:58 -0500266 // canvas.drawColor(Color.LTGRAY);
Alexandre Lision0edf18c2013-09-23 17:35:50 -0400267
268 if (dragging_bubble) {
Alexandre Lision0edf18c2013-09-23 17:35:50 -0400269 Paint p = new Paint();
270 p.setDither(true);
Alexandre Lision23628c12013-09-24 11:17:05 -0400271 p.setColor(getResources().getColor(R.color.holo_red_light));
Alexandre Lision23628c12013-09-24 11:17:05 -0400272 p.setStyle(Style.STROKE);
Alexandre Lision23628c12013-09-24 11:17:05 -0400273 p.setStrokeWidth(20);
Alexandre Lision23628c12013-09-24 11:17:05 -0400274 canvas.drawRect(new RectF(10, 10, model.width - 10, model.height - 10), p);
Alexandre Lision0edf18c2013-09-23 17:35:50 -0400275 }
276
277 tryMe.setStyle(Paint.Style.STROKE);
278 tryMe.setColor(getResources().getColor(R.color.darker_gray));
279 tryMe.setXfermode(null);
280 canvas.drawCircle(model.width / 2, model.height / 2, model.width / 2 - getResources().getDimension(R.dimen.bubble_size), tryMe);
281
alision2cb99562013-05-30 17:02:20 -0400282 try {
Adrien Béraud0c9bd8f2013-05-30 16:16:57 +1000283
alision2cb99562013-05-30 17:02:20 -0400284 for (int i = 0, n = attractors.size(); i < n; i++) {
285 Attractor a = attractors.get(i);
286 canvas.drawBitmap(a.getBitmap(), null, a.getBounds(), null);
287 }
Adrien Béraud33268882013-05-18 03:41:15 +1000288
alision2cb99562013-05-30 17:02:20 -0400289 for (int i = 0, n = bubbles.size(); i < n; i++) {
290 Bubble b = bubbles.get(i);
Alexandre Lision0edf18c2013-09-23 17:35:50 -0400291 if (b.expanded) {
alision465ceba2013-07-04 09:24:30 -0400292 continue;
293 }
alision2cb99562013-05-30 17:02:20 -0400294 canvas.drawBitmap(b.getBitmap(), null, b.getBounds(), null);
Alexandre Lision68855472013-10-10 16:20:46 -0400295 canvas.drawText(b.getName(), b.getPosX(), (float) (b.getPosY() - b.getRetractedRadius() * 1.2 * density), getNamePaint(b));
alision34673e62013-06-25 14:40:07 -0400296 }
Alexandre Lision0edf18c2013-09-23 17:35:50 -0400297
alision34673e62013-06-25 14:40:07 -0400298 Bubble first_plan = getExpandedBubble();
299 if (first_plan != null) {
Alexandre Lisiond588bff2013-10-08 12:43:01 -0400300
Alexandre Lision40954dc2013-10-09 15:24:03 -0400301 if (first_plan.getDrawerBitmap() != null) {
302 canvas.drawBitmap(first_plan.getDrawerBitmap(), null, first_plan.getDrawerBounds(), null);
Alexandre Lision68855472013-10-10 16:20:46 -0400303 }
alision34673e62013-06-25 14:40:07 -0400304 canvas.drawBitmap(first_plan.getBitmap(), null, first_plan.getBounds(), null);
Alexandre Lisiona7ab2e32014-02-14 15:33:33 -0500305 // canvas.drawText(first_plan.associated_call.getmContact().getmDisplayName(), first_plan.getPosX(),
Alexandre Lision40954dc2013-10-09 15:24:03 -0400306 // (float) (first_plan.getPosY() - first_plan.getRetractedRadius() * 1.2 * density), getNamePaint(first_plan));
alisiondf1dac92013-06-27 17:35:53 -0400307
alision2cb99562013-05-30 17:02:20 -0400308 }
309
310 } catch (IndexOutOfBoundsException e) {
311 Log.e(TAG, e.toString());
312 }
313 }
314 }
alision34673e62013-06-25 14:40:07 -0400315
316 }
317
318 private Paint getNamePaint(Bubble b) {
alision50fa0722013-06-25 17:29:44 -0400319 if (b.expanded) {
320 white_name_paint.setTextSize(15 * b.target_scale * textDensity);
alision34673e62013-06-25 14:40:07 -0400321 return white_name_paint;
alision50fa0722013-06-25 17:29:44 -0400322 }
323 black_name_paint.setTextSize(18 * b.target_scale * textDensity);
alision34673e62013-06-25 14:40:07 -0400324 return black_name_paint;
alision2cb99562013-05-30 17:02:20 -0400325 }
Adrien Béraud04463092013-05-06 14:17:22 +1000326
alisione38001f2013-06-04 14:14:39 -0400327 @Override
328 public boolean onTouch(View v, MotionEvent event) {
Alexandre Lision6deda412013-09-25 13:21:22 -0400329 // Log.w(TAG, "onTouch " + event.getAction());
alisione38001f2013-06-04 14:14:39 -0400330
331 int action = event.getActionMasked();
332
Alexandre Lision1888bf32013-10-31 09:51:41 -0400333 if (gDetector.onTouchEvent(event))
334 return true;
335
alisione38001f2013-06-04 14:14:39 -0400336 if (action == MotionEvent.ACTION_UP) {
337 if (thread.suspendFlag) {
338 Log.i(TAG, "Relaunch drawing thread");
339 thread.setPaused(false);
340 }
341
Alexandre Lision5cd659e2013-10-29 13:18:23 -0400342 Bubble expand = getExpandedBubble();
343 if (expand != null) {
Alexandre Lision5cd659e2013-10-29 13:18:23 -0400344 switch (expand.getDrawer().getAction(event.getX(), event.getY())) {
345 case Bubble.actions.OUT_OF_BOUNDS:
346 expand.retract();
347 break;
348 case Bubble.actions.HOLD:
349
350 try {
351 if (expand.getHoldStatus()) {
352
353 if (expand.isConference())
354 callback.mCallbacks.getService().unholdConference(expand.getCallID());
355 else
356 callback.mCallbacks.getService().unhold(expand.getCallID());
357 } else {
358 if (expand.isConference())
359 callback.mCallbacks.getService().holdConference(expand.getCallID());
360 else
361 callback.mCallbacks.getService().hold(expand.getCallID());
362
363 }
364 } catch (Exception e) {
365 e.printStackTrace();
366 }
367
368 return true;
369 case Bubble.actions.RECORD:
370 try {
Alexandre Lision3874e552013-11-04 17:20:12 -0500371 boolean isRecording = callback.mCallbacks.getService().toggleRecordingCall(expand.getCallID());
372 ((BubbleUser) expand).associated_call.setRecording(isRecording);
Alexandre Lision5cd659e2013-10-29 13:18:23 -0400373 } catch (RemoteException e1) {
374 e1.printStackTrace();
375 }
376 return true;
377 case Bubble.actions.MESSAGE:
378 // TODO
379 return true;
Alexandre Lisiond9c3f7f2013-10-30 15:20:55 -0400380 case Bubble.actions.MUTE:
Alexandre Lisionc9fca9e2013-11-08 15:21:18 -0500381 try {
382 callback.mCallbacks.getService().setMuted(!((BubbleUser) expand).getMute());
383 ((BubbleUser) expand).toggleMute();
384 } catch (RemoteException e1) {
385 e1.printStackTrace();
386 }
Alexandre Lisiond9c3f7f2013-10-30 15:20:55 -0400387 return true;
Alexandre Lision5cd659e2013-10-29 13:18:23 -0400388 case Bubble.actions.HANGUP:
389 try {
390 if (expand.isConference())
391 callback.mCallbacks.getService().hangUpConference(expand.getCallID());
392 else
393 callback.mCallbacks.getService().hangUp(expand.getCallID());
394 } catch (RemoteException e) {
395 e.printStackTrace();
396 }
397 return true;
398
399 case Bubble.actions.TRANSFER:
400 callback.makeTransfer((BubbleContact) expand);
401 return true;
402 case Bubble.actions.NOTHING:
403 break;
404 }
Alexandre Lision5cd659e2013-10-29 13:18:23 -0400405
Alexandre Lisiond9c3f7f2013-10-30 15:20:55 -0400406 }
Alexandre Lision5cd659e2013-10-29 13:18:23 -0400407
alisione38001f2013-06-04 14:14:39 -0400408 List<Bubble> bubbles = model.getBubbles();
409 final int n_bubbles = bubbles.size();
410 for (int i = 0; i < n_bubbles; i++) {
411 Bubble b = bubbles.get(i);
412 if (b.dragged) {
413 b.dragged = false;
414 b.target_scale = 1.f;
Alexandre Lisionfdef8852013-09-24 13:32:38 -0400415 if (b.isOnBorder(model.width, model.height) && !b.expanded) {
Alexandre Lision0edf18c2013-09-23 17:35:50 -0400416 b.markedToDie = true;
Alexandre Lision68855472013-10-10 16:20:46 -0400417
418 try {
419 if (b.isConference())
420 callback.mCallbacks.getService().hangUpConference(b.getCallID());
421 else
422 callback.mCallbacks.getService().hangUp(b.getCallID());
423
424 } catch (RemoteException e) {
425 e.printStackTrace();
426 }
Alexandre Lision0edf18c2013-09-23 17:35:50 -0400427 }
alisione38001f2013-06-04 14:14:39 -0400428 }
429 }
430 dragging_bubble = false;
431 } else if (action != MotionEvent.ACTION_DOWN && !isDraggingBubble() && !thread.suspendFlag) {
432
433 Log.i(TAG, "Not dragging thread should be stopped");
434 thread.setPaused(true);
435 // thread.holdDrawing();
436 }
437
Alexandre Lision1888bf32013-10-31 09:51:41 -0400438 return true;
alisione38001f2013-06-04 14:14:39 -0400439 }
440
alision34673e62013-06-25 14:40:07 -0400441 private Bubble getExpandedBubble() {
442 List<Bubble> bubbles = model.getBubbles();
443 final int n_bubbles = bubbles.size();
444 for (int i = 0; i < n_bubbles; i++) {
445 Bubble b = bubbles.get(i);
446 if (b.expanded) {
447 return b;
448 }
449 }
450 return null;
451 }
alision2ec64f92013-06-17 17:28:58 -0400452
alisiondf1dac92013-06-27 17:35:53 -0400453 public void restartDrawing() {
454 if (thread != null && thread.suspendFlag) {
455 Log.i(TAG, "Relaunch drawing thread");
456 thread.setPaused(false);
457 }
458 }
459
460 public void setFragment(CallFragment callFragment) {
461 callback = callFragment;
462
463 }
464
465 public void stopThread() {
466 if (thread != null && thread.suspendFlag) {
467 Log.i(TAG, "Stop drawing thread");
Alexandre Lision666b3772013-10-28 17:42:48 -0400468 thread.setPaused(true);
alisiondf1dac92013-06-27 17:35:53 -0400469 }
470
471 }
472
alision58356b72013-06-03 17:13:36 -0400473 class MyOnGestureListener implements OnGestureListener {
474 @Override
475 public boolean onDown(MotionEvent event) {
476 List<Bubble> bubbles = model.getBubbles();
Alexandre Lision1888bf32013-10-31 09:51:41 -0400477
Alexandre Lision8fdcf2b2013-10-30 16:06:51 -0400478 Bubble target = getExpandedBubble();
Alexandre Lision1888bf32013-10-31 09:51:41 -0400479 if (target != null) {
Alexandre Lision8fdcf2b2013-10-30 16:06:51 -0400480 target.onDown(event);
481 return true;
482 }
Alexandre Lision68855472013-10-10 16:20:46 -0400483
Alexandre Lisiond9c3f7f2013-10-30 15:20:55 -0400484 for (int i = 0; i < bubbles.size(); i++) {
alision58356b72013-06-03 17:13:36 -0400485 Bubble b = bubbles.get(i);
Alexandre Lision1888bf32013-10-31 09:51:41 -0400486 if (b.onDown(event))
487 dragging_bubble = true;
alision58356b72013-06-03 17:13:36 -0400488 }
Alexandre Lisiond9c3f7f2013-10-30 15:20:55 -0400489
alision58356b72013-06-03 17:13:36 -0400490 return true;
491 }
alision2ec64f92013-06-17 17:28:58 -0400492
alision58356b72013-06-03 17:13:36 -0400493 @Override
494 public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
Alexandre Lision6deda412013-09-25 13:21:22 -0400495 // Log.d("Main", "onFling");
Alexandre Lision1888bf32013-10-31 09:51:41 -0400496 return false;
alision58356b72013-06-03 17:13:36 -0400497 }
498
499 @Override
500 public void onLongPress(MotionEvent e) {
Alexandre Lision6deda412013-09-25 13:21:22 -0400501 // Log.d("Main", "onLongPress");
Alexandre Lision1888bf32013-10-31 09:51:41 -0400502
alision2ec64f92013-06-17 17:28:58 -0400503 }
504
alision2ec64f92013-06-17 17:28:58 -0400505 private Bubble getDraggedBubble(MotionEvent e) {
506 List<Bubble> bubbles = model.getBubbles();
507 final int n_bubbles = bubbles.size();
508 for (int i = 0; i < n_bubbles; i++) {
509 Bubble b = bubbles.get(i);
510 if (b.intersects(e.getX(), e.getY())) {
511 return b;
512 }
513 }
514 return null;
alision58356b72013-06-03 17:13:36 -0400515 }
516
517 @Override
518 public boolean onScroll(MotionEvent e1, MotionEvent event, float distanceX, float distanceY) {
Alexandre Lision6deda412013-09-25 13:21:22 -0400519 // Log.d("Main", "onScroll");
alision58356b72013-06-03 17:13:36 -0400520 List<Bubble> bubbles = model.getBubbles();
Alexandre Lisiond9c3f7f2013-10-30 15:20:55 -0400521
alision58356b72013-06-03 17:13:36 -0400522 long now = System.nanoTime();
Alexandre Lisiond9c3f7f2013-10-30 15:20:55 -0400523 for (int i = 0; i < bubbles.size(); i++) {
alision58356b72013-06-03 17:13:36 -0400524 Bubble b = bubbles.get(i);
525 if (b.dragged) {
526 float x = event.getX(), y = event.getY();
527 float dt = (float) ((now - b.last_drag) / 1000000000.);
528 float dx = x - b.getPosX(), dy = y - b.getPosY();
529 b.last_drag = now;
alision58356b72013-06-03 17:13:36 -0400530 b.setPos(event.getX(), event.getY());
alision58356b72013-06-03 17:13:36 -0400531 b.speed.x = dx / dt;
532 b.speed.y = dy / dt;
Alexandre Lisiond9c3f7f2013-10-30 15:20:55 -0400533
Alexandre Lision1888bf32013-10-31 09:51:41 -0400534 return false;
alision58356b72013-06-03 17:13:36 -0400535 }
536 }
537 return true;
538 }
539
540 @Override
541 public void onShowPress(MotionEvent e) {
Alexandre Lision6deda412013-09-25 13:21:22 -0400542 // Log.d("Main", "onShowPress");
alision58356b72013-06-03 17:13:36 -0400543
544 }
545
546 @Override
547 public boolean onSingleTapUp(MotionEvent e) {
Alexandre Lision1888bf32013-10-31 09:51:41 -0400548 if (isDraggingBubble() && callback.getConference().isOnGoing()) {
549 Bubble b = getDraggedBubble(e);
550 b.expand(model.width, model.height);
Alexandre Lision1a682572013-11-01 11:21:17 -0400551 dragging_bubble = false;
Alexandre Lision1888bf32013-10-31 09:51:41 -0400552 }
553 return false;
554
alision58356b72013-06-03 17:13:36 -0400555 }
556 }
alisionfe9cf712013-05-03 17:26:08 -0400557}