blob: df124b0d1cb75d8b9ece3a5dcda9b357c6916813 [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;
41import android.graphics.Canvas;
Adrien Béraud33268882013-05-18 03:41:15 +100042import android.graphics.Paint;
Adrien Béraud6bbce912013-05-24 00:48:13 +100043import android.graphics.Paint.Align;
Alexandre Lision23628c12013-09-24 11:17:05 -040044import android.graphics.Paint.Style;
Alexandre Lision68855472013-10-10 16:20:46 -040045import android.graphics.PixelFormat;
Alexandre Lision0edf18c2013-09-23 17:35:50 -040046import android.graphics.RectF;
Adrien Béraud04463092013-05-06 14:17:22 +100047import android.os.Handler;
48import android.os.Message;
Alexandre Lision68855472013-10-10 16:20:46 -040049import android.os.RemoteException;
alisionfe9cf712013-05-03 17:26:08 -040050import android.util.AttributeSet;
51import android.util.Log;
alision58356b72013-06-03 17:13:36 -040052import android.view.GestureDetector;
53import android.view.GestureDetector.OnGestureListener;
alisionfe9cf712013-05-03 17:26:08 -040054import android.view.MotionEvent;
Adrien Béraud04463092013-05-06 14:17:22 +100055import android.view.SurfaceHolder;
56import android.view.SurfaceView;
alisionfe9cf712013-05-03 17:26:08 -040057import android.view.View;
Adrien Béraud04463092013-05-06 14:17:22 +100058import android.view.View.OnTouchListener;
Alexandre Lision5cd659e2013-10-29 13:18:23 -040059import android.widget.Toast;
alisionfe9cf712013-05-03 17:26:08 -040060
alision2cb99562013-05-30 17:02:20 -040061public class BubblesView extends SurfaceView implements SurfaceHolder.Callback, OnTouchListener {
62 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;
Adrien Béraud33268882013-05-18 03:41:15 +100074
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;
110 thread = new BubblesThread(getHolder(), getContext(), new Handler() {
111 @Override
112 public void handleMessage(Message m) {
113 /*
114 * mStatusText.setVisibility(m.getData().getInt("viz")); mStatusText.setText(m.getData().getString("text"));
115 */
116 }
117 });
118 if (model != null)
119 thread.setModel(model);
120 }
alisionfe9cf712013-05-03 17:26:08 -0400121
alision2cb99562013-05-30 17:02:20 -0400122 public void setModel(BubbleModel model) {
123 this.model = model;
124 thread.setModel(model);
125 }
alisionfe9cf712013-05-03 17:26:08 -0400126
alision2cb99562013-05-30 17:02:20 -0400127 /*
128 * @Override public void onWindowFocusChanged(boolean hasWindowFocus) { if (!hasWindowFocus) { thread.pause(); } }
129 */
alisionfe9cf712013-05-03 17:26:08 -0400130
alision2cb99562013-05-30 17:02:20 -0400131 @Override
132 public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
133 Log.w(TAG, "surfaceChanged " + width + "-" + height);
Alexandre Lisiond588bff2013-10-08 12:43:01 -0400134 if (height < model.height) // probably showing the keyboard, don't move!
Alexandre Lision84208a32013-09-25 13:18:37 -0400135 return;
136
alision2cb99562013-05-30 17:02:20 -0400137 thread.setSurfaceSize(width, height);
138 }
alisionfe9cf712013-05-03 17:26:08 -0400139
alision2cb99562013-05-30 17:02:20 -0400140 /*
141 * Callback invoked when the Surface has been created and is ready to be used.
142 */
143 @Override
144 public void surfaceCreated(SurfaceHolder holder) {
145 // start the thread here so that we don't busy-wait in run()
146 // waiting for the surface to be created
147 createThread();
alisionfe9cf712013-05-03 17:26:08 -0400148
alision2cb99562013-05-30 17:02:20 -0400149 Log.w(TAG, "surfaceCreated");
150 thread.setRunning(true);
151 thread.start();
152 }
alisionfe9cf712013-05-03 17:26:08 -0400153
alision2cb99562013-05-30 17:02:20 -0400154 /*
155 * Callback invoked when the Surface has been destroyed and must no longer be touched. WARNING: after this method returns, the Surface/Canvas must
156 * never be touched again!
157 */
158 @Override
159 public void surfaceDestroyed(SurfaceHolder holder) {
160 // we have to tell thread to shut down & wait for it to finish, or else
161 // it might touch the Surface after we return and explode
162 Log.w(TAG, "surfaceDestroyed");
163 boolean retry = true;
164 thread.setRunning(false);
alision1005ba12013-06-19 13:52:44 -0400165 thread.setPaused(false);
alision2cb99562013-05-30 17:02:20 -0400166 while (retry) {
167 try {
alision1005ba12013-06-19 13:52:44 -0400168 Log.w(TAG, "joining...");
alision2cb99562013-05-30 17:02:20 -0400169 thread.join();
170 retry = false;
171 } catch (InterruptedException e) {
172 }
173 }
alision1005ba12013-06-19 13:52:44 -0400174 Log.w(TAG, "done");
alision2cb99562013-05-30 17:02:20 -0400175 thread = null;
176 }
alisionfe9cf712013-05-03 17:26:08 -0400177
alision2cb99562013-05-30 17:02:20 -0400178 public boolean isDraggingBubble() {
179 return dragging_bubble;
180 }
Adrien Béraudc9c424d2013-05-30 17:47:35 +1000181
alision2cb99562013-05-30 17:02:20 -0400182 class BubblesThread extends Thread {
183 private boolean running = false;
184 private SurfaceHolder surfaceHolder;
alisione38001f2013-06-04 14:14:39 -0400185 public Boolean suspendFlag = false;
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 {
Adrien Béraud7ed23dc2013-05-06 16:27:24 +1000202
alisione38001f2013-06-04 14:14:39 -0400203 if (suspendFlag) {
204 synchronized (this) {
205 while (suspendFlag) {
206 try {
207 wait();
208 } catch (InterruptedException e) {
209 // TODO Auto-generated catch block
210 e.printStackTrace();
211 }
212 }
213 }
214 } else {
alisione38001f2013-06-04 14:14:39 -0400215 c = surfaceHolder.lockCanvas(null);
Adrien Béraud7ed23dc2013-05-06 16:27:24 +1000216
alisione38001f2013-06-04 14:14:39 -0400217 // for the case the surface is destroyed while already in the loop
218 if (c == null || model == null)
219 continue;
220
221 synchronized (surfaceHolder) {
222 // Log.w(TAG, "Thread doDraw");
223 model.update();
224 doDraw(c);
225 }
alision2cb99562013-05-30 17:02:20 -0400226 }
alisione38001f2013-06-04 14:14:39 -0400227
alision2cb99562013-05-30 17:02:20 -0400228 } finally {
229 if (c != null)
230 surfaceHolder.unlockCanvasAndPost(c);
231 }
232 }
233 }
Adrien Béraud04463092013-05-06 14:17:22 +1000234
alisione38001f2013-06-04 14:14:39 -0400235 public void setPaused(boolean wantToPause) {
236 synchronized (this) {
237 suspendFlag = wantToPause;
238 notify();
239 }
240 }
241
alision2cb99562013-05-30 17:02:20 -0400242 public void setRunning(boolean b) {
243 running = b;
244 }
Adrien Béraud04463092013-05-06 14:17:22 +1000245
alision2cb99562013-05-30 17:02:20 -0400246 public void setSurfaceSize(int width, int height) {
247 synchronized (surfaceHolder) {
248 if (model != null) {
249 model.width = width;
250 model.height = height;
251 }
252 }
253 }
Adrien Béraud04463092013-05-06 14:17:22 +1000254
alision2cb99562013-05-30 17:02:20 -0400255 /**
256 * I got multiple IndexOutOfBoundsException, when switching calls. //FIXME
257 *
258 * @param canvas
259 */
260 private void doDraw(Canvas canvas) {
Adrien Béraud04463092013-05-06 14:17:22 +1000261
alision2cb99562013-05-30 17:02:20 -0400262 synchronized (model) {
263 List<Bubble> bubbles = model.getBubbles();
264 List<Attractor> attractors = model.getAttractors();
Alexandre Lision0edf18c2013-09-23 17:35:50 -0400265
266 Paint tryMe = new Paint();
267
Alexandre Lision40954dc2013-10-09 15:24:03 -0400268 canvas.drawColor(getResources().getColor(R.color.sfl_light_blue));
Alexandre Lision0edf18c2013-09-23 17:35:50 -0400269
270 if (dragging_bubble) {
Alexandre Lision0edf18c2013-09-23 17:35:50 -0400271 Paint p = new Paint();
272 p.setDither(true);
Alexandre Lision23628c12013-09-24 11:17:05 -0400273 p.setColor(getResources().getColor(R.color.holo_red_light));
Alexandre Lision23628c12013-09-24 11:17:05 -0400274 p.setStyle(Style.STROKE);
Alexandre Lision23628c12013-09-24 11:17:05 -0400275 p.setStrokeWidth(20);
Alexandre Lision23628c12013-09-24 11:17:05 -0400276 canvas.drawRect(new RectF(10, 10, model.width - 10, model.height - 10), p);
Alexandre Lision0edf18c2013-09-23 17:35:50 -0400277 }
278
279 tryMe.setStyle(Paint.Style.STROKE);
280 tryMe.setColor(getResources().getColor(R.color.darker_gray));
281 tryMe.setXfermode(null);
282 canvas.drawCircle(model.width / 2, model.height / 2, model.width / 2 - getResources().getDimension(R.dimen.bubble_size), tryMe);
283
alision2cb99562013-05-30 17:02:20 -0400284 try {
Adrien Béraud0c9bd8f2013-05-30 16:16:57 +1000285
alision2cb99562013-05-30 17:02:20 -0400286 for (int i = 0, n = attractors.size(); i < n; i++) {
287 Attractor a = attractors.get(i);
288 canvas.drawBitmap(a.getBitmap(), null, a.getBounds(), null);
289 }
Adrien Béraud33268882013-05-18 03:41:15 +1000290
alision2cb99562013-05-30 17:02:20 -0400291 for (int i = 0, n = bubbles.size(); i < n; i++) {
292 Bubble b = bubbles.get(i);
Alexandre Lision0edf18c2013-09-23 17:35:50 -0400293 if (b.expanded) {
alision465ceba2013-07-04 09:24:30 -0400294 continue;
295 }
alision2cb99562013-05-30 17:02:20 -0400296 canvas.drawBitmap(b.getBitmap(), null, b.getBounds(), null);
Alexandre Lision68855472013-10-10 16:20:46 -0400297 canvas.drawText(b.getName(), b.getPosX(), (float) (b.getPosY() - b.getRetractedRadius() * 1.2 * density), getNamePaint(b));
alision34673e62013-06-25 14:40:07 -0400298 }
Alexandre Lision0edf18c2013-09-23 17:35:50 -0400299
alision34673e62013-06-25 14:40:07 -0400300 Bubble first_plan = getExpandedBubble();
301 if (first_plan != null) {
Alexandre Lisiond588bff2013-10-08 12:43:01 -0400302
Alexandre Lision40954dc2013-10-09 15:24:03 -0400303 if (first_plan.getDrawerBitmap() != null) {
304 canvas.drawBitmap(first_plan.getDrawerBitmap(), null, first_plan.getDrawerBounds(), null);
Alexandre Lision68855472013-10-10 16:20:46 -0400305 }
alision34673e62013-06-25 14:40:07 -0400306 canvas.drawBitmap(first_plan.getBitmap(), null, first_plan.getBounds(), null);
Alexandre Lision40954dc2013-10-09 15:24:03 -0400307 // canvas.drawText(first_plan.associated_call.getContact().getmDisplayName(), first_plan.getPosX(),
308 // (float) (first_plan.getPosY() - first_plan.getRetractedRadius() * 1.2 * density), getNamePaint(first_plan));
alisiondf1dac92013-06-27 17:35:53 -0400309
alision2cb99562013-05-30 17:02:20 -0400310 }
311
312 } catch (IndexOutOfBoundsException e) {
313 Log.e(TAG, e.toString());
314 }
315 }
316 }
alision34673e62013-06-25 14:40:07 -0400317
318 }
319
320 private Paint getNamePaint(Bubble b) {
alision50fa0722013-06-25 17:29:44 -0400321 if (b.expanded) {
322 white_name_paint.setTextSize(15 * b.target_scale * textDensity);
alision34673e62013-06-25 14:40:07 -0400323 return white_name_paint;
alision50fa0722013-06-25 17:29:44 -0400324 }
325 black_name_paint.setTextSize(18 * b.target_scale * textDensity);
alision34673e62013-06-25 14:40:07 -0400326 return black_name_paint;
alision2cb99562013-05-30 17:02:20 -0400327 }
Adrien Béraud04463092013-05-06 14:17:22 +1000328
alisione38001f2013-06-04 14:14:39 -0400329 @Override
330 public boolean onTouch(View v, MotionEvent event) {
Alexandre Lision6deda412013-09-25 13:21:22 -0400331 // Log.w(TAG, "onTouch " + event.getAction());
alisione38001f2013-06-04 14:14:39 -0400332
333 int action = event.getActionMasked();
334
Alexandre Lision1888bf32013-10-31 09:51:41 -0400335 if (gDetector.onTouchEvent(event))
336 return true;
337
alisione38001f2013-06-04 14:14:39 -0400338 if (action == MotionEvent.ACTION_UP) {
339 if (thread.suspendFlag) {
340 Log.i(TAG, "Relaunch drawing thread");
341 thread.setPaused(false);
342 }
343
Alexandre Lision5cd659e2013-10-29 13:18:23 -0400344 Bubble expand = getExpandedBubble();
345 if (expand != null) {
Alexandre Lision5cd659e2013-10-29 13:18:23 -0400346 switch (expand.getDrawer().getAction(event.getX(), event.getY())) {
347 case Bubble.actions.OUT_OF_BOUNDS:
348 expand.retract();
349 break;
350 case Bubble.actions.HOLD:
351
352 try {
353 if (expand.getHoldStatus()) {
354
355 if (expand.isConference())
356 callback.mCallbacks.getService().unholdConference(expand.getCallID());
357 else
358 callback.mCallbacks.getService().unhold(expand.getCallID());
359 } else {
360 if (expand.isConference())
361 callback.mCallbacks.getService().holdConference(expand.getCallID());
362 else
363 callback.mCallbacks.getService().hold(expand.getCallID());
364
365 }
366 } catch (Exception e) {
367 e.printStackTrace();
368 }
369
370 return true;
371 case Bubble.actions.RECORD:
372 try {
Alexandre Lision3874e552013-11-04 17:20:12 -0500373 boolean isRecording = callback.mCallbacks.getService().toggleRecordingCall(expand.getCallID());
374 ((BubbleUser) expand).associated_call.setRecording(isRecording);
Alexandre Lision5cd659e2013-10-29 13:18:23 -0400375 } catch (RemoteException e1) {
376 e1.printStackTrace();
377 }
378 return true;
379 case Bubble.actions.MESSAGE:
380 // TODO
381 return true;
Alexandre Lisiond9c3f7f2013-10-30 15:20:55 -0400382 case Bubble.actions.MUTE:
Alexandre Lisionc9fca9e2013-11-08 15:21:18 -0500383 try {
384 callback.mCallbacks.getService().setMuted(!((BubbleUser) expand).getMute());
385 ((BubbleUser) expand).toggleMute();
386 } catch (RemoteException e1) {
387 e1.printStackTrace();
388 }
Alexandre Lisiond9c3f7f2013-10-30 15:20:55 -0400389 return true;
Alexandre Lision5cd659e2013-10-29 13:18:23 -0400390 case Bubble.actions.HANGUP:
391 try {
392 if (expand.isConference())
393 callback.mCallbacks.getService().hangUpConference(expand.getCallID());
394 else
395 callback.mCallbacks.getService().hangUp(expand.getCallID());
396 } catch (RemoteException e) {
397 e.printStackTrace();
398 }
399 return true;
400
401 case Bubble.actions.TRANSFER:
402 callback.makeTransfer((BubbleContact) expand);
403 return true;
404 case Bubble.actions.NOTHING:
405 break;
406 }
Alexandre Lision5cd659e2013-10-29 13:18:23 -0400407
Alexandre Lisiond9c3f7f2013-10-30 15:20:55 -0400408 }
Alexandre Lision5cd659e2013-10-29 13:18:23 -0400409
alisione38001f2013-06-04 14:14:39 -0400410 List<Bubble> bubbles = model.getBubbles();
411 final int n_bubbles = bubbles.size();
412 for (int i = 0; i < n_bubbles; i++) {
413 Bubble b = bubbles.get(i);
414 if (b.dragged) {
415 b.dragged = false;
416 b.target_scale = 1.f;
Alexandre Lisionfdef8852013-09-24 13:32:38 -0400417 if (b.isOnBorder(model.width, model.height) && !b.expanded) {
Alexandre Lision0edf18c2013-09-23 17:35:50 -0400418 b.markedToDie = true;
Alexandre Lision68855472013-10-10 16:20:46 -0400419
420 try {
421 if (b.isConference())
422 callback.mCallbacks.getService().hangUpConference(b.getCallID());
423 else
424 callback.mCallbacks.getService().hangUp(b.getCallID());
425
426 } catch (RemoteException e) {
427 e.printStackTrace();
428 }
Alexandre Lision0edf18c2013-09-23 17:35:50 -0400429 }
alisione38001f2013-06-04 14:14:39 -0400430 }
431 }
432 dragging_bubble = false;
433 } else if (action != MotionEvent.ACTION_DOWN && !isDraggingBubble() && !thread.suspendFlag) {
434
435 Log.i(TAG, "Not dragging thread should be stopped");
436 thread.setPaused(true);
437 // thread.holdDrawing();
438 }
439
Alexandre Lision1888bf32013-10-31 09:51:41 -0400440 return true;
alisione38001f2013-06-04 14:14:39 -0400441 }
442
alision34673e62013-06-25 14:40:07 -0400443 private Bubble getExpandedBubble() {
444 List<Bubble> bubbles = model.getBubbles();
445 final int n_bubbles = bubbles.size();
446 for (int i = 0; i < n_bubbles; i++) {
447 Bubble b = bubbles.get(i);
448 if (b.expanded) {
449 return b;
450 }
451 }
452 return null;
453 }
alision2ec64f92013-06-17 17:28:58 -0400454
alisiondf1dac92013-06-27 17:35:53 -0400455 public void restartDrawing() {
456 if (thread != null && thread.suspendFlag) {
457 Log.i(TAG, "Relaunch drawing thread");
458 thread.setPaused(false);
459 }
460 }
461
462 public void setFragment(CallFragment callFragment) {
463 callback = callFragment;
464
465 }
466
467 public void stopThread() {
468 if (thread != null && thread.suspendFlag) {
469 Log.i(TAG, "Stop drawing thread");
Alexandre Lision666b3772013-10-28 17:42:48 -0400470 thread.setPaused(true);
alisiondf1dac92013-06-27 17:35:53 -0400471 }
472
473 }
474
alision58356b72013-06-03 17:13:36 -0400475 class MyOnGestureListener implements OnGestureListener {
476 @Override
477 public boolean onDown(MotionEvent event) {
478 List<Bubble> bubbles = model.getBubbles();
Alexandre Lision1888bf32013-10-31 09:51:41 -0400479
Alexandre Lision8fdcf2b2013-10-30 16:06:51 -0400480 Bubble target = getExpandedBubble();
Alexandre Lision1888bf32013-10-31 09:51:41 -0400481 if (target != null) {
Alexandre Lision8fdcf2b2013-10-30 16:06:51 -0400482 target.onDown(event);
483 return true;
484 }
Alexandre Lision68855472013-10-10 16:20:46 -0400485
Alexandre Lisiond9c3f7f2013-10-30 15:20:55 -0400486 for (int i = 0; i < bubbles.size(); i++) {
alision58356b72013-06-03 17:13:36 -0400487 Bubble b = bubbles.get(i);
Alexandre Lision1888bf32013-10-31 09:51:41 -0400488 if (b.onDown(event))
489 dragging_bubble = true;
alision58356b72013-06-03 17:13:36 -0400490 }
Alexandre Lisiond9c3f7f2013-10-30 15:20:55 -0400491
alision58356b72013-06-03 17:13:36 -0400492 return true;
493 }
alision2ec64f92013-06-17 17:28:58 -0400494
alision58356b72013-06-03 17:13:36 -0400495 @Override
496 public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
Alexandre Lision6deda412013-09-25 13:21:22 -0400497 // Log.d("Main", "onFling");
Alexandre Lision1888bf32013-10-31 09:51:41 -0400498 return false;
alision58356b72013-06-03 17:13:36 -0400499 }
500
501 @Override
502 public void onLongPress(MotionEvent e) {
Alexandre Lision6deda412013-09-25 13:21:22 -0400503 // Log.d("Main", "onLongPress");
Alexandre Lision1888bf32013-10-31 09:51:41 -0400504
alision2ec64f92013-06-17 17:28:58 -0400505 }
506
alision2ec64f92013-06-17 17:28:58 -0400507 private Bubble getDraggedBubble(MotionEvent e) {
508 List<Bubble> bubbles = model.getBubbles();
509 final int n_bubbles = bubbles.size();
510 for (int i = 0; i < n_bubbles; i++) {
511 Bubble b = bubbles.get(i);
512 if (b.intersects(e.getX(), e.getY())) {
513 return b;
514 }
515 }
516 return null;
alision58356b72013-06-03 17:13:36 -0400517 }
518
519 @Override
520 public boolean onScroll(MotionEvent e1, MotionEvent event, float distanceX, float distanceY) {
Alexandre Lision6deda412013-09-25 13:21:22 -0400521 // Log.d("Main", "onScroll");
alision58356b72013-06-03 17:13:36 -0400522 List<Bubble> bubbles = model.getBubbles();
Alexandre Lisiond9c3f7f2013-10-30 15:20:55 -0400523
alision58356b72013-06-03 17:13:36 -0400524 long now = System.nanoTime();
Alexandre Lisiond9c3f7f2013-10-30 15:20:55 -0400525 for (int i = 0; i < bubbles.size(); i++) {
alision58356b72013-06-03 17:13:36 -0400526 Bubble b = bubbles.get(i);
527 if (b.dragged) {
528 float x = event.getX(), y = event.getY();
529 float dt = (float) ((now - b.last_drag) / 1000000000.);
530 float dx = x - b.getPosX(), dy = y - b.getPosY();
531 b.last_drag = now;
alision58356b72013-06-03 17:13:36 -0400532 b.setPos(event.getX(), event.getY());
alision58356b72013-06-03 17:13:36 -0400533 b.speed.x = dx / dt;
534 b.speed.y = dy / dt;
Alexandre Lisiond9c3f7f2013-10-30 15:20:55 -0400535
Alexandre Lision1888bf32013-10-31 09:51:41 -0400536 return false;
alision58356b72013-06-03 17:13:36 -0400537 }
538 }
539 return true;
540 }
541
542 @Override
543 public void onShowPress(MotionEvent e) {
Alexandre Lision6deda412013-09-25 13:21:22 -0400544 // Log.d("Main", "onShowPress");
alision58356b72013-06-03 17:13:36 -0400545
546 }
547
548 @Override
549 public boolean onSingleTapUp(MotionEvent e) {
Alexandre Lision1888bf32013-10-31 09:51:41 -0400550 if (isDraggingBubble() && callback.getConference().isOnGoing()) {
551 Bubble b = getDraggedBubble(e);
552 b.expand(model.width, model.height);
Alexandre Lision1a682572013-11-01 11:21:17 -0400553 dragging_bubble = false;
Alexandre Lision1888bf32013-10-31 09:51:41 -0400554 }
555 return false;
556
alision58356b72013-06-03 17:13:36 -0400557 }
558 }
alisionfe9cf712013-05-03 17:26:08 -0400559}