blob: 7920bbbc3cc5044b8097f267311410be2e1e33f5 [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;
38import org.sflphone.client.CallActivity;
39import org.sflphone.fragments.CallFragment;
40
alisionfe9cf712013-05-03 17:26:08 -040041import android.content.Context;
42import android.graphics.Canvas;
43import android.graphics.Color;
Adrien Béraud33268882013-05-18 03:41:15 +100044import android.graphics.Paint;
Adrien Béraud6bbce912013-05-24 00:48:13 +100045import android.graphics.Paint.Align;
Alexandre Lision23628c12013-09-24 11:17:05 -040046import android.graphics.Paint.Style;
Alexandre Lision0edf18c2013-09-23 17:35:50 -040047import android.graphics.RectF;
Adrien Béraud04463092013-05-06 14:17:22 +100048import android.os.Handler;
49import android.os.Message;
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;
alisionfe9cf712013-05-03 17:26:08 -040059
alision2cb99562013-05-30 17:02:20 -040060public class BubblesView extends SurfaceView implements SurfaceHolder.Callback, OnTouchListener {
61 private static final String TAG = BubblesView.class.getSimpleName();
alisionfe9cf712013-05-03 17:26:08 -040062
alision2cb99562013-05-30 17:02:20 -040063 private BubblesThread thread = null;
64 private BubbleModel model;
alisionfe9cf712013-05-03 17:26:08 -040065
alision2cb99562013-05-30 17:02:20 -040066 private Paint attractor_paint = new Paint();
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
alision2cb99562013-05-30 17:02:20 -040088 // create thread only; it's started in surfaceCreated()
89 createThread();
alisionfe9cf712013-05-03 17:26:08 -040090
alision2cb99562013-05-30 17:02:20 -040091 setOnTouchListener(this);
92 setFocusable(true);
Adrien Béraud33268882013-05-18 03:41:15 +100093
alision2cb99562013-05-30 17:02:20 -040094 attractor_paint.setColor(Color.RED);
95 // attractor_paint.set
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 Lision0edf18c2013-09-23 17:35:50 -0400276
Alexandre Lision23628c12013-09-24 11:17:05 -0400277 canvas.drawRect(new RectF(10, 10, model.width - 10, model.height - 10), p);
Alexandre Lision0edf18c2013-09-23 17:35:50 -0400278 }
279
280 tryMe.setStyle(Paint.Style.STROKE);
281 tryMe.setColor(getResources().getColor(R.color.darker_gray));
282 tryMe.setXfermode(null);
283 canvas.drawCircle(model.width / 2, model.height / 2, model.width / 2 - getResources().getDimension(R.dimen.bubble_size), tryMe);
284
alision2cb99562013-05-30 17:02:20 -0400285 try {
Adrien Béraud0c9bd8f2013-05-30 16:16:57 +1000286
alision2cb99562013-05-30 17:02:20 -0400287 for (int i = 0, n = attractors.size(); i < n; i++) {
288 Attractor a = attractors.get(i);
289 canvas.drawBitmap(a.getBitmap(), null, a.getBounds(), null);
290 }
Adrien Béraud33268882013-05-18 03:41:15 +1000291
alision2cb99562013-05-30 17:02:20 -0400292 for (int i = 0, n = bubbles.size(); i < n; i++) {
293 Bubble b = bubbles.get(i);
Alexandre Lision0edf18c2013-09-23 17:35:50 -0400294 if (b.expanded) {
alision465ceba2013-07-04 09:24:30 -0400295 continue;
296 }
alision2cb99562013-05-30 17:02:20 -0400297 canvas.drawBitmap(b.getBitmap(), null, b.getBounds(), null);
Alexandre Lision40954dc2013-10-09 15:24:03 -0400298 canvas.drawText(b.getCall().getContact().getmDisplayName(), b.getPosX(), (float) (b.getPosY() - b.getRetractedRadius() * 1.2
299 * density), getNamePaint(b));
alision34673e62013-06-25 14:40:07 -0400300 }
Alexandre Lision0edf18c2013-09-23 17:35:50 -0400301
alision34673e62013-06-25 14:40:07 -0400302 Bubble first_plan = getExpandedBubble();
303 if (first_plan != null) {
Alexandre Lisiond588bff2013-10-08 12:43:01 -0400304
Alexandre Lision40954dc2013-10-09 15:24:03 -0400305 if (first_plan.getDrawerBitmap() != null) {
306 canvas.drawBitmap(first_plan.getDrawerBitmap(), null, first_plan.getDrawerBounds(), null);
307 }
alision34673e62013-06-25 14:40:07 -0400308 canvas.drawBitmap(first_plan.getBitmap(), null, first_plan.getBounds(), null);
Alexandre Lision40954dc2013-10-09 15:24:03 -0400309 // canvas.drawText(first_plan.associated_call.getContact().getmDisplayName(), first_plan.getPosX(),
310 // (float) (first_plan.getPosY() - first_plan.getRetractedRadius() * 1.2 * density), getNamePaint(first_plan));
alisiondf1dac92013-06-27 17:35:53 -0400311
alision2cb99562013-05-30 17:02:20 -0400312 }
313
314 } catch (IndexOutOfBoundsException e) {
315 Log.e(TAG, e.toString());
316 }
317 }
318 }
alision34673e62013-06-25 14:40:07 -0400319
320 }
321
322 private Paint getNamePaint(Bubble b) {
alision50fa0722013-06-25 17:29:44 -0400323 if (b.expanded) {
324 white_name_paint.setTextSize(15 * b.target_scale * textDensity);
alision34673e62013-06-25 14:40:07 -0400325 return white_name_paint;
alision50fa0722013-06-25 17:29:44 -0400326 }
327 black_name_paint.setTextSize(18 * b.target_scale * textDensity);
alision34673e62013-06-25 14:40:07 -0400328 return black_name_paint;
alision2cb99562013-05-30 17:02:20 -0400329 }
Adrien Béraud04463092013-05-06 14:17:22 +1000330
alisione38001f2013-06-04 14:14:39 -0400331 @Override
332 public boolean onTouch(View v, MotionEvent event) {
Alexandre Lision6deda412013-09-25 13:21:22 -0400333 // Log.w(TAG, "onTouch " + event.getAction());
alisione38001f2013-06-04 14:14:39 -0400334
335 int action = event.getActionMasked();
336
337 if (action == MotionEvent.ACTION_UP) {
338 if (thread.suspendFlag) {
339 Log.i(TAG, "Relaunch drawing thread");
340 thread.setPaused(false);
341 }
342
343 List<Bubble> bubbles = model.getBubbles();
344 final int n_bubbles = bubbles.size();
345 for (int i = 0; i < n_bubbles; i++) {
346 Bubble b = bubbles.get(i);
347 if (b.dragged) {
348 b.dragged = false;
349 b.target_scale = 1.f;
Alexandre Lisionfdef8852013-09-24 13:32:38 -0400350 if (b.isOnBorder(model.width, model.height) && !b.expanded) {
Alexandre Lision0edf18c2013-09-23 17:35:50 -0400351 b.markedToDie = true;
Alexandre Lision40954dc2013-10-09 15:24:03 -0400352 ((CallActivity) callback.getActivity()).onCallEnded(b.getCall());
Alexandre Lision0edf18c2013-09-23 17:35:50 -0400353 }
alisione38001f2013-06-04 14:14:39 -0400354 }
355 }
356 dragging_bubble = false;
357 } else if (action != MotionEvent.ACTION_DOWN && !isDraggingBubble() && !thread.suspendFlag) {
358
359 Log.i(TAG, "Not dragging thread should be stopped");
360 thread.setPaused(true);
361 // thread.holdDrawing();
362 }
363
364 return gDetector.onTouchEvent(event);
365 }
366
alision34673e62013-06-25 14:40:07 -0400367 private Bubble getExpandedBubble() {
368 List<Bubble> bubbles = model.getBubbles();
369 final int n_bubbles = bubbles.size();
370 for (int i = 0; i < n_bubbles; i++) {
371 Bubble b = bubbles.get(i);
372 if (b.expanded) {
373 return b;
374 }
375 }
376 return null;
377 }
alision2ec64f92013-06-17 17:28:58 -0400378
alisiondf1dac92013-06-27 17:35:53 -0400379 public void restartDrawing() {
380 if (thread != null && thread.suspendFlag) {
381 Log.i(TAG, "Relaunch drawing thread");
382 thread.setPaused(false);
383 }
384 }
385
386 public void setFragment(CallFragment callFragment) {
387 callback = callFragment;
388
389 }
390
391 public void stopThread() {
392 if (thread != null && thread.suspendFlag) {
393 Log.i(TAG, "Stop drawing thread");
394 thread.setRunning(false);
395 thread.setPaused(false);
396 }
397
398 }
399
alision58356b72013-06-03 17:13:36 -0400400 class MyOnGestureListener implements OnGestureListener {
401 @Override
402 public boolean onDown(MotionEvent event) {
403 List<Bubble> bubbles = model.getBubbles();
404 final int n_bubbles = bubbles.size();
alision2ec64f92013-06-17 17:28:58 -0400405 Bubble expand = getExpandedBubble();
Alexandre Lision40954dc2013-10-09 15:24:03 -0400406 Log.d("Main", "onDown");
alision34673e62013-06-25 14:40:07 -0400407 if (expand != null) {
Alexandre Lision40954dc2013-10-09 15:24:03 -0400408 Log.d("Main", "getAction");
409 switch (expand.getDrawer().getAction(event.getX(), event.getY())) {
410 case Bubble.actions.NOTHING:
alision34673e62013-06-25 14:40:07 -0400411 expand.retract();
Alexandre Lision40954dc2013-10-09 15:24:03 -0400412 break;
413 case Bubble.actions.HOLD:
414 // if(expand.isUser){
415 // ((CallActivity) callback.getActivity()).onCallSuspended();
416 // }
417 if (expand.getHoldStatus()) {
418 ((CallActivity) callback.getActivity()).onCallResumed(expand.getCall());
419 } else {
420 ((CallActivity) callback.getActivity()).onCallSuspended(expand.getCall());
alision34673e62013-06-25 14:40:07 -0400421 }
Alexandre Lision40954dc2013-10-09 15:24:03 -0400422
423 break;
424 case Bubble.actions.RECORD:
425 ((CallActivity) callback.getActivity()).onRecordCall(expand.getCall());
426 break;
427 case Bubble.actions.MESSAGE:
428 // TODO
429 break;
430
431 case Bubble.actions.HANGUP:
432 ((CallActivity) callback.getActivity()).onCallEnded(expand.getCall());
433 break;
434
435 case Bubble.actions.TRANSFER:
436 callback.makeTransfer((BubbleContact) expand);
437 break;
alision34673e62013-06-25 14:40:07 -0400438 }
439 return true;
alision2ec64f92013-06-17 17:28:58 -0400440 }
Alexandre Lision40954dc2013-10-09 15:24:03 -0400441
Alexandre Lision6deda412013-09-25 13:21:22 -0400442 // Log.d("Main", "onDown");
alision58356b72013-06-03 17:13:36 -0400443 for (int i = 0; i < n_bubbles; i++) {
444 Bubble b = bubbles.get(i);
alision34673e62013-06-25 14:40:07 -0400445 if (b.intersects(event.getX(), event.getY()) && !b.expanded) {
alision58356b72013-06-03 17:13:36 -0400446 b.dragged = true;
447 b.last_drag = System.nanoTime();
448 b.setPos(event.getX(), event.getY());
449 b.target_scale = .8f;
450 dragging_bubble = true;
451 }
452 }
453 return true;
454 }
alision2ec64f92013-06-17 17:28:58 -0400455
alision58356b72013-06-03 17:13:36 -0400456 @Override
457 public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
Alexandre Lision6deda412013-09-25 13:21:22 -0400458 // Log.d("Main", "onFling");
alision58356b72013-06-03 17:13:36 -0400459 return true;
460 }
461
462 @Override
463 public void onLongPress(MotionEvent e) {
Alexandre Lision6deda412013-09-25 13:21:22 -0400464 // Log.d("Main", "onLongPress");
alision2ec64f92013-06-17 17:28:58 -0400465 if (isDraggingBubble()) {
466 Bubble b = getDraggedBubble(e);
Alexandre Lisiond588bff2013-10-08 12:43:01 -0400467 b.expand(model.width, model.height);
alision2ec64f92013-06-17 17:28:58 -0400468 }
469 }
470
alision2ec64f92013-06-17 17:28:58 -0400471 private Bubble getDraggedBubble(MotionEvent e) {
472 List<Bubble> bubbles = model.getBubbles();
473 final int n_bubbles = bubbles.size();
474 for (int i = 0; i < n_bubbles; i++) {
475 Bubble b = bubbles.get(i);
476 if (b.intersects(e.getX(), e.getY())) {
477 return b;
478 }
479 }
480 return null;
alision58356b72013-06-03 17:13:36 -0400481 }
482
483 @Override
484 public boolean onScroll(MotionEvent e1, MotionEvent event, float distanceX, float distanceY) {
Alexandre Lision6deda412013-09-25 13:21:22 -0400485 // Log.d("Main", "onScroll");
alision58356b72013-06-03 17:13:36 -0400486 List<Bubble> bubbles = model.getBubbles();
487 final int n_bubbles = bubbles.size();
488 long now = System.nanoTime();
489 for (int i = 0; i < n_bubbles; i++) {
490 Bubble b = bubbles.get(i);
491 if (b.dragged) {
492 float x = event.getX(), y = event.getY();
493 float dt = (float) ((now - b.last_drag) / 1000000000.);
494 float dx = x - b.getPosX(), dy = y - b.getPosY();
495 b.last_drag = now;
alision58356b72013-06-03 17:13:36 -0400496 b.setPos(event.getX(), event.getY());
alision58356b72013-06-03 17:13:36 -0400497 b.speed.x = dx / dt;
498 b.speed.y = dy / dt;
alision58356b72013-06-03 17:13:36 -0400499 // }
500 return true;
501 }
502 }
503 return true;
504 }
505
506 @Override
507 public void onShowPress(MotionEvent e) {
Alexandre Lision6deda412013-09-25 13:21:22 -0400508 // Log.d("Main", "onShowPress");
alision58356b72013-06-03 17:13:36 -0400509
510 }
511
512 @Override
513 public boolean onSingleTapUp(MotionEvent e) {
Alexandre Lision6deda412013-09-25 13:21:22 -0400514 // Log.d("Main", "onSingleTapUp");
alision58356b72013-06-03 17:13:36 -0400515 return true;
516 }
517 }
alisionfe9cf712013-05-03 17:26:08 -0400518}