blob: df85f60784fbc06f3c32c0d7b037c025e294c8f6 [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
335 if (action == MotionEvent.ACTION_UP) {
336 if (thread.suspendFlag) {
337 Log.i(TAG, "Relaunch drawing thread");
338 thread.setPaused(false);
339 }
340
Alexandre Lision5cd659e2013-10-29 13:18:23 -0400341 Bubble expand = getExpandedBubble();
342 if (expand != null) {
343
344 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 {
371 callback.mCallbacks.getService().toggleRecordingCall(expand.getCallID());
372 } catch (RemoteException e1) {
373 e1.printStackTrace();
374 }
375 return true;
376 case Bubble.actions.MESSAGE:
377 // TODO
378 return true;
379
380 case Bubble.actions.HANGUP:
381 try {
382 if (expand.isConference())
383 callback.mCallbacks.getService().hangUpConference(expand.getCallID());
384 else
385 callback.mCallbacks.getService().hangUp(expand.getCallID());
386 } catch (RemoteException e) {
387 e.printStackTrace();
388 }
389 return true;
390
391 case Bubble.actions.TRANSFER:
392 callback.makeTransfer((BubbleContact) expand);
393 return true;
394 case Bubble.actions.NOTHING:
395 break;
396 }
397
398
399 }
400
401
402
alisione38001f2013-06-04 14:14:39 -0400403 List<Bubble> bubbles = model.getBubbles();
404 final int n_bubbles = bubbles.size();
405 for (int i = 0; i < n_bubbles; i++) {
406 Bubble b = bubbles.get(i);
407 if (b.dragged) {
408 b.dragged = false;
409 b.target_scale = 1.f;
Alexandre Lisionfdef8852013-09-24 13:32:38 -0400410 if (b.isOnBorder(model.width, model.height) && !b.expanded) {
Alexandre Lision0edf18c2013-09-23 17:35:50 -0400411 b.markedToDie = true;
Alexandre Lision68855472013-10-10 16:20:46 -0400412
413 try {
414 if (b.isConference())
415 callback.mCallbacks.getService().hangUpConference(b.getCallID());
416 else
417 callback.mCallbacks.getService().hangUp(b.getCallID());
418
419 } catch (RemoteException e) {
420 e.printStackTrace();
421 }
Alexandre Lision0edf18c2013-09-23 17:35:50 -0400422 }
alisione38001f2013-06-04 14:14:39 -0400423 }
424 }
425 dragging_bubble = false;
426 } else if (action != MotionEvent.ACTION_DOWN && !isDraggingBubble() && !thread.suspendFlag) {
427
428 Log.i(TAG, "Not dragging thread should be stopped");
429 thread.setPaused(true);
430 // thread.holdDrawing();
431 }
432
433 return gDetector.onTouchEvent(event);
434 }
435
alision34673e62013-06-25 14:40:07 -0400436 private Bubble getExpandedBubble() {
437 List<Bubble> bubbles = model.getBubbles();
438 final int n_bubbles = bubbles.size();
439 for (int i = 0; i < n_bubbles; i++) {
440 Bubble b = bubbles.get(i);
441 if (b.expanded) {
442 return b;
443 }
444 }
445 return null;
446 }
alision2ec64f92013-06-17 17:28:58 -0400447
alisiondf1dac92013-06-27 17:35:53 -0400448 public void restartDrawing() {
449 if (thread != null && thread.suspendFlag) {
450 Log.i(TAG, "Relaunch drawing thread");
451 thread.setPaused(false);
452 }
453 }
454
455 public void setFragment(CallFragment callFragment) {
456 callback = callFragment;
457
458 }
459
460 public void stopThread() {
461 if (thread != null && thread.suspendFlag) {
462 Log.i(TAG, "Stop drawing thread");
Alexandre Lision666b3772013-10-28 17:42:48 -0400463 thread.setPaused(true);
alisiondf1dac92013-06-27 17:35:53 -0400464 }
465
466 }
467
alision58356b72013-06-03 17:13:36 -0400468 class MyOnGestureListener implements OnGestureListener {
469 @Override
470 public boolean onDown(MotionEvent event) {
471 List<Bubble> bubbles = model.getBubbles();
472 final int n_bubbles = bubbles.size();
Alexandre Lision68855472013-10-10 16:20:46 -0400473
alision58356b72013-06-03 17:13:36 -0400474 for (int i = 0; i < n_bubbles; i++) {
475 Bubble b = bubbles.get(i);
alision34673e62013-06-25 14:40:07 -0400476 if (b.intersects(event.getX(), event.getY()) && !b.expanded) {
alision58356b72013-06-03 17:13:36 -0400477 b.dragged = true;
478 b.last_drag = System.nanoTime();
479 b.setPos(event.getX(), event.getY());
480 b.target_scale = .8f;
481 dragging_bubble = true;
482 }
483 }
484 return true;
485 }
alision2ec64f92013-06-17 17:28:58 -0400486
alision58356b72013-06-03 17:13:36 -0400487 @Override
488 public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
Alexandre Lision6deda412013-09-25 13:21:22 -0400489 // Log.d("Main", "onFling");
alision58356b72013-06-03 17:13:36 -0400490 return true;
491 }
492
493 @Override
494 public void onLongPress(MotionEvent e) {
Alexandre Lision6deda412013-09-25 13:21:22 -0400495 // Log.d("Main", "onLongPress");
alision2ec64f92013-06-17 17:28:58 -0400496 if (isDraggingBubble()) {
497 Bubble b = getDraggedBubble(e);
Alexandre Lisiond588bff2013-10-08 12:43:01 -0400498 b.expand(model.width, model.height);
alision2ec64f92013-06-17 17:28:58 -0400499 }
500 }
501
alision2ec64f92013-06-17 17:28:58 -0400502 private Bubble getDraggedBubble(MotionEvent e) {
503 List<Bubble> bubbles = model.getBubbles();
504 final int n_bubbles = bubbles.size();
505 for (int i = 0; i < n_bubbles; i++) {
506 Bubble b = bubbles.get(i);
507 if (b.intersects(e.getX(), e.getY())) {
508 return b;
509 }
510 }
511 return null;
alision58356b72013-06-03 17:13:36 -0400512 }
513
514 @Override
515 public boolean onScroll(MotionEvent e1, MotionEvent event, float distanceX, float distanceY) {
Alexandre Lision6deda412013-09-25 13:21:22 -0400516 // Log.d("Main", "onScroll");
alision58356b72013-06-03 17:13:36 -0400517 List<Bubble> bubbles = model.getBubbles();
518 final int n_bubbles = bubbles.size();
519 long now = System.nanoTime();
520 for (int i = 0; i < n_bubbles; i++) {
521 Bubble b = bubbles.get(i);
522 if (b.dragged) {
523 float x = event.getX(), y = event.getY();
524 float dt = (float) ((now - b.last_drag) / 1000000000.);
525 float dx = x - b.getPosX(), dy = y - b.getPosY();
526 b.last_drag = now;
alision58356b72013-06-03 17:13:36 -0400527 b.setPos(event.getX(), event.getY());
alision58356b72013-06-03 17:13:36 -0400528 b.speed.x = dx / dt;
529 b.speed.y = dy / dt;
alision58356b72013-06-03 17:13:36 -0400530 // }
531 return true;
532 }
533 }
534 return true;
535 }
536
537 @Override
538 public void onShowPress(MotionEvent e) {
Alexandre Lision6deda412013-09-25 13:21:22 -0400539 // Log.d("Main", "onShowPress");
alision58356b72013-06-03 17:13:36 -0400540
541 }
542
543 @Override
544 public boolean onSingleTapUp(MotionEvent e) {
Alexandre Lision6deda412013-09-25 13:21:22 -0400545 // Log.d("Main", "onSingleTapUp");
alision58356b72013-06-03 17:13:36 -0400546 return true;
547 }
548 }
alisionfe9cf712013-05-03 17:26:08 -0400549}