blob: 9f61622ec64f9225b9a0ad9e29781da51096314a [file] [log] [blame]
alisioncc7bb422013-06-06 15:31:39 -04001/*
2 * Copyright (C) 2004-2013 Savoir-Faire Linux Inc.
3 *
4 * Author: Alexandre Lision <alexandre.lision@savoirfairelinux.com>
5 * Adrien Béraud <adrien.beraud@gmail.com>
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 *
21 * Additional permission under GNU GPL version 3 section 7:
22 *
23 * If you modify this program, or any covered work, by linking or
24 * combining it with the OpenSSL project's OpenSSL library (or a
25 * modified version of that library), containing parts covered by the
26 * terms of the OpenSSL or SSLeay licenses, Savoir-Faire Linux Inc.
27 * grants you additional permission to convey the resulting work.
28 * Corresponding Source for a non-source form of such a combination
29 * shall include the source code for the parts of OpenSSL used as well
30 * as that of the covered work.
31 */
32
Adrien Béraud04463092013-05-06 14:17:22 +100033package com.savoirfairelinux.sflphone.model;
alisionfe9cf712013-05-03 17:26:08 -040034
Adrien Béraud0c9bd8f2013-05-30 16:16:57 +100035import java.util.List;
36
alisionfe9cf712013-05-03 17:26:08 -040037import android.content.Context;
38import android.graphics.Canvas;
39import android.graphics.Color;
Alexandre Lision0edf18c2013-09-23 17:35:50 -040040import android.graphics.LinearGradient;
Adrien Béraud33268882013-05-18 03:41:15 +100041import android.graphics.Paint;
Adrien Béraud6bbce912013-05-24 00:48:13 +100042import android.graphics.Paint.Align;
Alexandre Lision0edf18c2013-09-23 17:35:50 -040043import android.graphics.RectF;
44import android.graphics.Shader.TileMode;
Adrien Béraud04463092013-05-06 14:17:22 +100045import android.os.Handler;
46import android.os.Message;
alisionfe9cf712013-05-03 17:26:08 -040047import android.util.AttributeSet;
48import android.util.Log;
alision58356b72013-06-03 17:13:36 -040049import android.view.GestureDetector;
50import android.view.GestureDetector.OnGestureListener;
alisionfe9cf712013-05-03 17:26:08 -040051import android.view.MotionEvent;
Adrien Béraud04463092013-05-06 14:17:22 +100052import android.view.SurfaceHolder;
53import android.view.SurfaceView;
alisionfe9cf712013-05-03 17:26:08 -040054import android.view.View;
Adrien Béraud04463092013-05-06 14:17:22 +100055import android.view.View.OnTouchListener;
alision34673e62013-06-25 14:40:07 -040056import android.widget.Toast;
57
Alexandre Lision0edf18c2013-09-23 17:35:50 -040058import com.savoirfairelinux.sflphone.R;
alision34673e62013-06-25 14:40:07 -040059import com.savoirfairelinux.sflphone.client.CallActivity;
60import com.savoirfairelinux.sflphone.fragments.CallFragment;
alisionfe9cf712013-05-03 17:26:08 -040061
alision2cb99562013-05-30 17:02:20 -040062public class BubblesView extends SurfaceView implements SurfaceHolder.Callback, OnTouchListener {
63 private static final String TAG = BubblesView.class.getSimpleName();
alisionfe9cf712013-05-03 17:26:08 -040064
alision2cb99562013-05-30 17:02:20 -040065 private BubblesThread thread = null;
66 private BubbleModel model;
alisionfe9cf712013-05-03 17:26:08 -040067
alision2cb99562013-05-30 17:02:20 -040068 private Paint attractor_paint = new Paint();
alision34673e62013-06-25 14:40:07 -040069 private Paint black_name_paint = new Paint(Paint.ANTI_ALIAS_FLAG);
70 private Paint white_name_paint = new Paint(Paint.ANTI_ALIAS_FLAG);
Adrien Béraud6bbce912013-05-24 00:48:13 +100071
alisione38001f2013-06-04 14:14:39 -040072 private GestureDetector gDetector;
73
alision2cb99562013-05-30 17:02:20 -040074 private float density;
75 private float textDensity;
Adrien Béraud33268882013-05-18 03:41:15 +100076
alision2cb99562013-05-30 17:02:20 -040077 private boolean dragging_bubble = false;
Adrien Béraudc9c424d2013-05-30 17:47:35 +100078
alision34673e62013-06-25 14:40:07 -040079 private CallFragment callback;
80
alision2cb99562013-05-30 17:02:20 -040081 public BubblesView(Context context, AttributeSet attrs) {
82 super(context, attrs);
alisionfe9cf712013-05-03 17:26:08 -040083
alision2cb99562013-05-30 17:02:20 -040084 density = getResources().getDisplayMetrics().density;
85 textDensity = getResources().getDisplayMetrics().scaledDensity;
Adrien Béraud6bbce912013-05-24 00:48:13 +100086
alision2cb99562013-05-30 17:02:20 -040087 SurfaceHolder holder = getHolder();
88 holder.addCallback(this);
alisionfe9cf712013-05-03 17:26:08 -040089
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
alision2cb99562013-05-30 17:02:20 -040096 attractor_paint.setColor(Color.RED);
97 // attractor_paint.set
alision34673e62013-06-25 14:40:07 -040098 black_name_paint.setTextSize(18 * textDensity);
99 black_name_paint.setColor(0xFF303030);
100 black_name_paint.setTextAlign(Align.CENTER);
101
102 white_name_paint.setTextSize(18 * textDensity);
103 white_name_paint.setColor(0xFFEEEEEE);
104 white_name_paint.setTextAlign(Align.CENTER);
alision58356b72013-06-03 17:13:36 -0400105
106 gDetector = new GestureDetector(getContext(), new MyOnGestureListener());
alision2cb99562013-05-30 17:02:20 -0400107 }
alisionfe9cf712013-05-03 17:26:08 -0400108
alision2cb99562013-05-30 17:02:20 -0400109 private void createThread() {
110 if (thread != null)
111 return;
112 thread = new BubblesThread(getHolder(), getContext(), new Handler() {
113 @Override
114 public void handleMessage(Message m) {
115 /*
116 * mStatusText.setVisibility(m.getData().getInt("viz")); mStatusText.setText(m.getData().getString("text"));
117 */
118 }
119 });
120 if (model != null)
121 thread.setModel(model);
122 }
alisionfe9cf712013-05-03 17:26:08 -0400123
alision2cb99562013-05-30 17:02:20 -0400124 public void setModel(BubbleModel model) {
125 this.model = model;
126 thread.setModel(model);
127 }
alisionfe9cf712013-05-03 17:26:08 -0400128
alision2cb99562013-05-30 17:02:20 -0400129 /*
130 * @Override public void onWindowFocusChanged(boolean hasWindowFocus) { if (!hasWindowFocus) { thread.pause(); } }
131 */
alisionfe9cf712013-05-03 17:26:08 -0400132
alision2cb99562013-05-30 17:02:20 -0400133 @Override
134 public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
135 Log.w(TAG, "surfaceChanged " + width + "-" + height);
136 thread.setSurfaceSize(width, height);
137 }
alisionfe9cf712013-05-03 17:26:08 -0400138
alision2cb99562013-05-30 17:02:20 -0400139 /*
140 * Callback invoked when the Surface has been created and is ready to be used.
141 */
142 @Override
143 public void surfaceCreated(SurfaceHolder holder) {
144 // start the thread here so that we don't busy-wait in run()
145 // waiting for the surface to be created
146 createThread();
alisionfe9cf712013-05-03 17:26:08 -0400147
alision2cb99562013-05-30 17:02:20 -0400148 Log.w(TAG, "surfaceCreated");
149 thread.setRunning(true);
150 thread.start();
151 }
alisionfe9cf712013-05-03 17:26:08 -0400152
alision2cb99562013-05-30 17:02:20 -0400153 /*
154 * Callback invoked when the Surface has been destroyed and must no longer be touched. WARNING: after this method returns, the Surface/Canvas must
155 * never be touched again!
156 */
157 @Override
158 public void surfaceDestroyed(SurfaceHolder holder) {
159 // we have to tell thread to shut down & wait for it to finish, or else
160 // it might touch the Surface after we return and explode
161 Log.w(TAG, "surfaceDestroyed");
162 boolean retry = true;
163 thread.setRunning(false);
alision1005ba12013-06-19 13:52:44 -0400164 thread.setPaused(false);
alision2cb99562013-05-30 17:02:20 -0400165 while (retry) {
166 try {
alision1005ba12013-06-19 13:52:44 -0400167 Log.w(TAG, "joining...");
alision2cb99562013-05-30 17:02:20 -0400168 thread.join();
169 retry = false;
170 } catch (InterruptedException e) {
171 }
172 }
alision1005ba12013-06-19 13:52:44 -0400173 Log.w(TAG, "done");
alision2cb99562013-05-30 17:02:20 -0400174 thread = null;
175 }
alisionfe9cf712013-05-03 17:26:08 -0400176
alision2cb99562013-05-30 17:02:20 -0400177 public boolean isDraggingBubble() {
178 return dragging_bubble;
179 }
Adrien Béraudc9c424d2013-05-30 17:47:35 +1000180
alision2cb99562013-05-30 17:02:20 -0400181 class BubblesThread extends Thread {
182 private boolean running = false;
183 private SurfaceHolder surfaceHolder;
alisione38001f2013-06-04 14:14:39 -0400184 public Boolean suspendFlag = false;
alisionfe9cf712013-05-03 17:26:08 -0400185
alision2cb99562013-05-30 17:02:20 -0400186 BubbleModel model = null;
alisionfe9cf712013-05-03 17:26:08 -0400187
alision2cb99562013-05-30 17:02:20 -0400188 public BubblesThread(SurfaceHolder holder, Context context, Handler handler) {
189 surfaceHolder = holder;
190 }
alisionfe9cf712013-05-03 17:26:08 -0400191
alision2cb99562013-05-30 17:02:20 -0400192 public void setModel(BubbleModel model) {
193 this.model = model;
194 }
alisionfe9cf712013-05-03 17:26:08 -0400195
alision2cb99562013-05-30 17:02:20 -0400196 @Override
197 public void run() {
198 while (running) {
199 Canvas c = null;
200 try {
Adrien Béraud7ed23dc2013-05-06 16:27:24 +1000201
alisione38001f2013-06-04 14:14:39 -0400202 if (suspendFlag) {
203 synchronized (this) {
204 while (suspendFlag) {
205 try {
206 wait();
207 } catch (InterruptedException e) {
208 // TODO Auto-generated catch block
209 e.printStackTrace();
210 }
211 }
212 }
213 } else {
alisione38001f2013-06-04 14:14:39 -0400214 c = surfaceHolder.lockCanvas(null);
Adrien Béraud7ed23dc2013-05-06 16:27:24 +1000215
alisione38001f2013-06-04 14:14:39 -0400216 // for the case the surface is destroyed while already in the loop
217 if (c == null || model == null)
218 continue;
219
220 synchronized (surfaceHolder) {
221 // Log.w(TAG, "Thread doDraw");
222 model.update();
223 doDraw(c);
224 }
alision2cb99562013-05-30 17:02:20 -0400225 }
alisione38001f2013-06-04 14:14:39 -0400226
alision2cb99562013-05-30 17:02:20 -0400227 } finally {
228 if (c != null)
229 surfaceHolder.unlockCanvasAndPost(c);
230 }
231 }
232 }
Adrien Béraud04463092013-05-06 14:17:22 +1000233
alisione38001f2013-06-04 14:14:39 -0400234 public void setPaused(boolean wantToPause) {
235 synchronized (this) {
236 suspendFlag = wantToPause;
237 notify();
238 }
239 }
240
alision2cb99562013-05-30 17:02:20 -0400241 public void setRunning(boolean b) {
242 running = b;
243 }
Adrien Béraud04463092013-05-06 14:17:22 +1000244
alision2cb99562013-05-30 17:02:20 -0400245 public void setSurfaceSize(int width, int height) {
246 synchronized (surfaceHolder) {
247 if (model != null) {
248 model.width = width;
249 model.height = height;
250 }
251 }
252 }
Adrien Béraud04463092013-05-06 14:17:22 +1000253
alision2cb99562013-05-30 17:02:20 -0400254 /**
255 * I got multiple IndexOutOfBoundsException, when switching calls. //FIXME
256 *
257 * @param canvas
258 */
259 private void doDraw(Canvas canvas) {
Adrien Béraud04463092013-05-06 14:17:22 +1000260
alision2cb99562013-05-30 17:02:20 -0400261 synchronized (model) {
262 List<Bubble> bubbles = model.getBubbles();
263 List<Attractor> attractors = model.getAttractors();
Alexandre Lision0edf18c2013-09-23 17:35:50 -0400264
265 Paint tryMe = new Paint();
266
267 canvas.drawColor(Color.WHITE);
268
269 if (dragging_bubble) {
270 // Draw red gradient around to hang up call
271 // canvas.drawColor(Color.RED);
272
273 LinearGradient grTop = new LinearGradient(0, 0, 0, 40, Color.RED, Color.WHITE, TileMode.CLAMP);
274 Paint p = new Paint();
275 p.setDither(true);
276 p.setShader(grTop);
277 canvas.drawRect(new RectF(0, 0, model.width, 40), p);
278
279 LinearGradient grBottom = new LinearGradient(0, model.height, 0, model.height - 40, Color.RED, Color.WHITE, TileMode.CLAMP);
280 p.setDither(true);
281 p.setShader(grBottom);
282 canvas.drawRect(new RectF(0, model.height - 40, model.width, model.height), p);
283
284 LinearGradient grLeft = new LinearGradient(0, 0, 40, 0, Color.RED, Color.WHITE, TileMode.CLAMP);
285 p.setDither(true);
286 p.setShader(grLeft);
287 canvas.drawRect(new RectF(0, 0, 40, model.height), p);
288
289 LinearGradient grRight = new LinearGradient(model.width, 0, model.width - 40, 0, Color.RED, Color.WHITE, TileMode.CLAMP);
290 p.setDither(true);
291 p.setShader(grRight);
292 canvas.drawRect(new RectF(model.width - 40, 0, model.width, model.height), p);
293
294 // tryMe.setColor(getResources().getColor(R.color.lighter_gray));
295 // tryMe.setStyle(Paint.Style.FILL);
296 // tryMe.setXfermode(new PorterDuffXfermode(Mode.SRC_OUT));
297 // canvas.drawArc(new RectF(15, 30, model.width - 15, model.height - 30), 0, 360, false, tryMe);
298 }
299
300 tryMe.setStyle(Paint.Style.STROKE);
301 tryMe.setColor(getResources().getColor(R.color.darker_gray));
302 tryMe.setXfermode(null);
303 canvas.drawCircle(model.width / 2, model.height / 2, model.width / 2 - getResources().getDimension(R.dimen.bubble_size), tryMe);
304
alision2cb99562013-05-30 17:02:20 -0400305 try {
Adrien Béraud0c9bd8f2013-05-30 16:16:57 +1000306
alision2cb99562013-05-30 17:02:20 -0400307 for (int i = 0, n = attractors.size(); i < n; i++) {
308 Attractor a = attractors.get(i);
309 canvas.drawBitmap(a.getBitmap(), null, a.getBounds(), null);
310 }
Adrien Béraud33268882013-05-18 03:41:15 +1000311
alision2cb99562013-05-30 17:02:20 -0400312 for (int i = 0, n = bubbles.size(); i < n; i++) {
313 Bubble b = bubbles.get(i);
Alexandre Lision0edf18c2013-09-23 17:35:50 -0400314 if (b.expanded) {
alision465ceba2013-07-04 09:24:30 -0400315 continue;
316 }
alision2cb99562013-05-30 17:02:20 -0400317 canvas.drawBitmap(b.getBitmap(), null, b.getBounds(), null);
Alexandre Lision0edf18c2013-09-23 17:35:50 -0400318 canvas.drawText(b.associated_call.getContact().getmDisplayName(), b.getPosX(), (float) (b.getPosY() - b.getRetractedRadius()
319 * 1.2 * density), getNamePaint(b));
alision34673e62013-06-25 14:40:07 -0400320 }
Alexandre Lision0edf18c2013-09-23 17:35:50 -0400321
alision34673e62013-06-25 14:40:07 -0400322 Bubble first_plan = getExpandedBubble();
323 if (first_plan != null) {
324 canvas.drawBitmap(first_plan.getBitmap(), null, first_plan.getBounds(), null);
Alexandre Lision0edf18c2013-09-23 17:35:50 -0400325
alision729b0a22013-07-02 11:57:33 -0400326 canvas.drawText(first_plan.associated_call.getContact().getmDisplayName(), first_plan.getPosX(),
alision465ceba2013-07-04 09:24:30 -0400327 (float) (first_plan.getPosY() - first_plan.getRetractedRadius() * 1.2 * density), getNamePaint(first_plan));
Alexandre Lision0edf18c2013-09-23 17:35:50 -0400328
alision729b0a22013-07-02 11:57:33 -0400329 canvas.drawText("Transfer", first_plan.getPosX(), (float) (first_plan.getPosY() + first_plan.getRetractedRadius() * 1.5
330 * density), getNamePaint(first_plan));
alisiondf1dac92013-06-27 17:35:53 -0400331
alision729b0a22013-07-02 11:57:33 -0400332 canvas.drawText(getResources().getString(first_plan.getHoldStatus()),
Alexandre Lisionc51ccb12013-09-11 16:00:30 -0400333 (float) (first_plan.getPosX() - first_plan.getRetractedRadius() * 1.5 * density - 15), first_plan.getPosY(),
alision729b0a22013-07-02 11:57:33 -0400334 getNamePaint(first_plan));
335
336 canvas.drawText(getResources().getString(first_plan.getRecordStatus()),
Alexandre Lisionc51ccb12013-09-11 16:00:30 -0400337 (float) (first_plan.getPosX() + first_plan.getRetractedRadius() * 1.5 * density + 15), first_plan.getPosY(),
alision729b0a22013-07-02 11:57:33 -0400338 getNamePaint(first_plan));
alisiondf1dac92013-06-27 17:35:53 -0400339
alision2cb99562013-05-30 17:02:20 -0400340 }
341
342 } catch (IndexOutOfBoundsException e) {
343 Log.e(TAG, e.toString());
344 }
345 }
346 }
alision34673e62013-06-25 14:40:07 -0400347
348 }
349
350 private Paint getNamePaint(Bubble b) {
alision50fa0722013-06-25 17:29:44 -0400351 if (b.expanded) {
352 white_name_paint.setTextSize(15 * b.target_scale * textDensity);
alision34673e62013-06-25 14:40:07 -0400353 return white_name_paint;
alision50fa0722013-06-25 17:29:44 -0400354 }
355 black_name_paint.setTextSize(18 * b.target_scale * textDensity);
alision34673e62013-06-25 14:40:07 -0400356 return black_name_paint;
alision2cb99562013-05-30 17:02:20 -0400357 }
Adrien Béraud04463092013-05-06 14:17:22 +1000358
alisione38001f2013-06-04 14:14:39 -0400359 @Override
360 public boolean onTouch(View v, MotionEvent event) {
361 Log.w(TAG, "onTouch " + event.getAction());
362
363 int action = event.getActionMasked();
364
365 if (action == MotionEvent.ACTION_UP) {
366 if (thread.suspendFlag) {
367 Log.i(TAG, "Relaunch drawing thread");
368 thread.setPaused(false);
369 }
370
371 List<Bubble> bubbles = model.getBubbles();
372 final int n_bubbles = bubbles.size();
373 for (int i = 0; i < n_bubbles; i++) {
374 Bubble b = bubbles.get(i);
375 if (b.dragged) {
376 b.dragged = false;
377 b.target_scale = 1.f;
Alexandre Lision0edf18c2013-09-23 17:35:50 -0400378 if (b.isOnBorder(model.width, model.height)){
379 b.markedToDie = true;
380 ((CallActivity) callback.getActivity()).onCallEnded(b.associated_call);
381 }
alisione38001f2013-06-04 14:14:39 -0400382 }
383 }
384 dragging_bubble = false;
385 } else if (action != MotionEvent.ACTION_DOWN && !isDraggingBubble() && !thread.suspendFlag) {
386
387 Log.i(TAG, "Not dragging thread should be stopped");
388 thread.setPaused(true);
389 // thread.holdDrawing();
390 }
391
392 return gDetector.onTouchEvent(event);
393 }
394
alision34673e62013-06-25 14:40:07 -0400395 private Bubble getExpandedBubble() {
396 List<Bubble> bubbles = model.getBubbles();
397 final int n_bubbles = bubbles.size();
398 for (int i = 0; i < n_bubbles; i++) {
399 Bubble b = bubbles.get(i);
400 if (b.expanded) {
401 return b;
402 }
403 }
404 return null;
405 }
alision2ec64f92013-06-17 17:28:58 -0400406
alisiondf1dac92013-06-27 17:35:53 -0400407 public void restartDrawing() {
408 if (thread != null && thread.suspendFlag) {
409 Log.i(TAG, "Relaunch drawing thread");
410 thread.setPaused(false);
411 }
412 }
413
414 public void setFragment(CallFragment callFragment) {
415 callback = callFragment;
416
417 }
418
419 public void stopThread() {
420 if (thread != null && thread.suspendFlag) {
421 Log.i(TAG, "Stop drawing thread");
422 thread.setRunning(false);
423 thread.setPaused(false);
424 }
425
426 }
427
alision58356b72013-06-03 17:13:36 -0400428 class MyOnGestureListener implements OnGestureListener {
429 @Override
430 public boolean onDown(MotionEvent event) {
431 List<Bubble> bubbles = model.getBubbles();
432 final int n_bubbles = bubbles.size();
alision2ec64f92013-06-17 17:28:58 -0400433 Bubble expand = getExpandedBubble();
alision34673e62013-06-25 14:40:07 -0400434 if (expand != null) {
435 if (!expand.intersects(event.getX(), event.getY())) {
436 expand.retract();
437 } else {
438 Log.d("Main", "getAction");
alision50fa0722013-06-25 17:29:44 -0400439 switch (expand.getAction(event.getX(), event.getY())) {
440 case 0:
441 expand.retract();
442 break;
443 case 1:
alisiondf1dac92013-06-27 17:35:53 -0400444 if (expand.associated_call.isOnHold()) {
445 ((CallActivity) callback.getActivity()).onCallResumed(expand.associated_call);
446 } else {
447 ((CallActivity) callback.getActivity()).onCallSuspended(expand.associated_call);
448 }
449
alision50fa0722013-06-25 17:29:44 -0400450 break;
451 case 2:
452 Log.d("Main", "onRecordCall");
453 ((CallActivity) callback.getActivity()).onRecordCall(expand.associated_call);
454 break;
455 case 3:
Alexandre Lision2b237922013-09-09 16:23:02 -0400456 callback.makeTransfer(expand);
alision50fa0722013-06-25 17:29:44 -0400457 Toast.makeText(getContext(), "Not implemented here", Toast.LENGTH_SHORT).show();
458 break;
alision34673e62013-06-25 14:40:07 -0400459 }
460 }
461 return true;
alision2ec64f92013-06-17 17:28:58 -0400462 }
alision58356b72013-06-03 17:13:36 -0400463 Log.d("Main", "onDown");
464 for (int i = 0; i < n_bubbles; i++) {
465 Bubble b = bubbles.get(i);
alision34673e62013-06-25 14:40:07 -0400466 if (b.intersects(event.getX(), event.getY()) && !b.expanded) {
alision58356b72013-06-03 17:13:36 -0400467 b.dragged = true;
468 b.last_drag = System.nanoTime();
469 b.setPos(event.getX(), event.getY());
470 b.target_scale = .8f;
471 dragging_bubble = true;
472 }
473 }
474 return true;
475 }
alision2ec64f92013-06-17 17:28:58 -0400476
alision58356b72013-06-03 17:13:36 -0400477 @Override
478 public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
479 Log.d("Main", "onFling");
480 return true;
481 }
482
483 @Override
484 public void onLongPress(MotionEvent e) {
485 Log.d("Main", "onLongPress");
alision2ec64f92013-06-17 17:28:58 -0400486 if (isDraggingBubble()) {
487 Bubble b = getDraggedBubble(e);
alision729b0a22013-07-02 11:57:33 -0400488 b.expand();
alision2ec64f92013-06-17 17:28:58 -0400489 }
490 }
491
alision2ec64f92013-06-17 17:28:58 -0400492 private Bubble getDraggedBubble(MotionEvent e) {
493 List<Bubble> bubbles = model.getBubbles();
494 final int n_bubbles = bubbles.size();
495 for (int i = 0; i < n_bubbles; i++) {
496 Bubble b = bubbles.get(i);
497 if (b.intersects(e.getX(), e.getY())) {
498 return b;
499 }
500 }
501 return null;
alision58356b72013-06-03 17:13:36 -0400502 }
503
504 @Override
505 public boolean onScroll(MotionEvent e1, MotionEvent event, float distanceX, float distanceY) {
506 Log.d("Main", "onScroll");
507 List<Bubble> bubbles = model.getBubbles();
508 final int n_bubbles = bubbles.size();
509 long now = System.nanoTime();
510 for (int i = 0; i < n_bubbles; i++) {
511 Bubble b = bubbles.get(i);
512 if (b.dragged) {
513 float x = event.getX(), y = event.getY();
514 float dt = (float) ((now - b.last_drag) / 1000000000.);
515 float dx = x - b.getPosX(), dy = y - b.getPosY();
516 b.last_drag = now;
alision58356b72013-06-03 17:13:36 -0400517 b.setPos(event.getX(), event.getY());
alision58356b72013-06-03 17:13:36 -0400518 b.speed.x = dx / dt;
519 b.speed.y = dy / dt;
alision58356b72013-06-03 17:13:36 -0400520 // }
521 return true;
522 }
523 }
524 return true;
525 }
526
527 @Override
528 public void onShowPress(MotionEvent e) {
529 Log.d("Main", "onShowPress");
530
531 }
532
533 @Override
534 public boolean onSingleTapUp(MotionEvent e) {
535 Log.d("Main", "onSingleTapUp");
536 return true;
537 }
538 }
alisionfe9cf712013-05-03 17:26:08 -0400539}