blob: c5b0933661c442236115ff915e5bff99d548d427 [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 Lision23628c12013-09-24 11:17:05 -040043import android.graphics.Paint.Style;
44import android.graphics.PorterDuffXfermode;
45import android.graphics.RadialGradient;
Alexandre Lision0edf18c2013-09-23 17:35:50 -040046import android.graphics.RectF;
47import android.graphics.Shader.TileMode;
Alexandre Lision23628c12013-09-24 11:17:05 -040048import android.graphics.Xfermode;
Adrien Béraud04463092013-05-06 14:17:22 +100049import android.os.Handler;
50import android.os.Message;
alisionfe9cf712013-05-03 17:26:08 -040051import android.util.AttributeSet;
52import android.util.Log;
alision58356b72013-06-03 17:13:36 -040053import android.view.GestureDetector;
54import android.view.GestureDetector.OnGestureListener;
alisionfe9cf712013-05-03 17:26:08 -040055import android.view.MotionEvent;
Adrien Béraud04463092013-05-06 14:17:22 +100056import android.view.SurfaceHolder;
57import android.view.SurfaceView;
alisionfe9cf712013-05-03 17:26:08 -040058import android.view.View;
Adrien Béraud04463092013-05-06 14:17:22 +100059import android.view.View.OnTouchListener;
alision34673e62013-06-25 14:40:07 -040060import android.widget.Toast;
61
Alexandre Lision0edf18c2013-09-23 17:35:50 -040062import com.savoirfairelinux.sflphone.R;
alision34673e62013-06-25 14:40:07 -040063import com.savoirfairelinux.sflphone.client.CallActivity;
64import com.savoirfairelinux.sflphone.fragments.CallFragment;
alisionfe9cf712013-05-03 17:26:08 -040065
alision2cb99562013-05-30 17:02:20 -040066public class BubblesView extends SurfaceView implements SurfaceHolder.Callback, OnTouchListener {
67 private static final String TAG = BubblesView.class.getSimpleName();
alisionfe9cf712013-05-03 17:26:08 -040068
alision2cb99562013-05-30 17:02:20 -040069 private BubblesThread thread = null;
70 private BubbleModel model;
alisionfe9cf712013-05-03 17:26:08 -040071
alision2cb99562013-05-30 17:02:20 -040072 private Paint attractor_paint = new Paint();
alision34673e62013-06-25 14:40:07 -040073 private Paint black_name_paint = new Paint(Paint.ANTI_ALIAS_FLAG);
74 private Paint white_name_paint = new Paint(Paint.ANTI_ALIAS_FLAG);
Adrien Béraud6bbce912013-05-24 00:48:13 +100075
alisione38001f2013-06-04 14:14:39 -040076 private GestureDetector gDetector;
77
alision2cb99562013-05-30 17:02:20 -040078 private float density;
79 private float textDensity;
Adrien Béraud33268882013-05-18 03:41:15 +100080
alision2cb99562013-05-30 17:02:20 -040081 private boolean dragging_bubble = false;
Adrien Béraudc9c424d2013-05-30 17:47:35 +100082
alision34673e62013-06-25 14:40:07 -040083 private CallFragment callback;
84
alision2cb99562013-05-30 17:02:20 -040085 public BubblesView(Context context, AttributeSet attrs) {
86 super(context, attrs);
alisionfe9cf712013-05-03 17:26:08 -040087
alision2cb99562013-05-30 17:02:20 -040088 density = getResources().getDisplayMetrics().density;
89 textDensity = getResources().getDisplayMetrics().scaledDensity;
Adrien Béraud6bbce912013-05-24 00:48:13 +100090
alision2cb99562013-05-30 17:02:20 -040091 SurfaceHolder holder = getHolder();
92 holder.addCallback(this);
alisionfe9cf712013-05-03 17:26:08 -040093
alision2cb99562013-05-30 17:02:20 -040094 // create thread only; it's started in surfaceCreated()
95 createThread();
alisionfe9cf712013-05-03 17:26:08 -040096
alision2cb99562013-05-30 17:02:20 -040097 setOnTouchListener(this);
98 setFocusable(true);
Adrien Béraud33268882013-05-18 03:41:15 +100099
alision2cb99562013-05-30 17:02:20 -0400100 attractor_paint.setColor(Color.RED);
101 // attractor_paint.set
alision34673e62013-06-25 14:40:07 -0400102 black_name_paint.setTextSize(18 * textDensity);
103 black_name_paint.setColor(0xFF303030);
104 black_name_paint.setTextAlign(Align.CENTER);
105
106 white_name_paint.setTextSize(18 * textDensity);
107 white_name_paint.setColor(0xFFEEEEEE);
108 white_name_paint.setTextAlign(Align.CENTER);
alision58356b72013-06-03 17:13:36 -0400109
110 gDetector = new GestureDetector(getContext(), new MyOnGestureListener());
alision2cb99562013-05-30 17:02:20 -0400111 }
alisionfe9cf712013-05-03 17:26:08 -0400112
alision2cb99562013-05-30 17:02:20 -0400113 private void createThread() {
114 if (thread != null)
115 return;
116 thread = new BubblesThread(getHolder(), getContext(), new Handler() {
117 @Override
118 public void handleMessage(Message m) {
119 /*
120 * mStatusText.setVisibility(m.getData().getInt("viz")); mStatusText.setText(m.getData().getString("text"));
121 */
122 }
123 });
124 if (model != null)
125 thread.setModel(model);
126 }
alisionfe9cf712013-05-03 17:26:08 -0400127
alision2cb99562013-05-30 17:02:20 -0400128 public void setModel(BubbleModel model) {
129 this.model = model;
130 thread.setModel(model);
131 }
alisionfe9cf712013-05-03 17:26:08 -0400132
alision2cb99562013-05-30 17:02:20 -0400133 /*
134 * @Override public void onWindowFocusChanged(boolean hasWindowFocus) { if (!hasWindowFocus) { thread.pause(); } }
135 */
alisionfe9cf712013-05-03 17:26:08 -0400136
alision2cb99562013-05-30 17:02:20 -0400137 @Override
138 public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
139 Log.w(TAG, "surfaceChanged " + width + "-" + height);
140 thread.setSurfaceSize(width, height);
141 }
alisionfe9cf712013-05-03 17:26:08 -0400142
alision2cb99562013-05-30 17:02:20 -0400143 /*
144 * Callback invoked when the Surface has been created and is ready to be used.
145 */
146 @Override
147 public void surfaceCreated(SurfaceHolder holder) {
148 // start the thread here so that we don't busy-wait in run()
149 // waiting for the surface to be created
150 createThread();
alisionfe9cf712013-05-03 17:26:08 -0400151
alision2cb99562013-05-30 17:02:20 -0400152 Log.w(TAG, "surfaceCreated");
153 thread.setRunning(true);
154 thread.start();
155 }
alisionfe9cf712013-05-03 17:26:08 -0400156
alision2cb99562013-05-30 17:02:20 -0400157 /*
158 * Callback invoked when the Surface has been destroyed and must no longer be touched. WARNING: after this method returns, the Surface/Canvas must
159 * never be touched again!
160 */
161 @Override
162 public void surfaceDestroyed(SurfaceHolder holder) {
163 // we have to tell thread to shut down & wait for it to finish, or else
164 // it might touch the Surface after we return and explode
165 Log.w(TAG, "surfaceDestroyed");
166 boolean retry = true;
167 thread.setRunning(false);
alision1005ba12013-06-19 13:52:44 -0400168 thread.setPaused(false);
alision2cb99562013-05-30 17:02:20 -0400169 while (retry) {
170 try {
alision1005ba12013-06-19 13:52:44 -0400171 Log.w(TAG, "joining...");
alision2cb99562013-05-30 17:02:20 -0400172 thread.join();
173 retry = false;
174 } catch (InterruptedException e) {
175 }
176 }
alision1005ba12013-06-19 13:52:44 -0400177 Log.w(TAG, "done");
alision2cb99562013-05-30 17:02:20 -0400178 thread = null;
179 }
alisionfe9cf712013-05-03 17:26:08 -0400180
alision2cb99562013-05-30 17:02:20 -0400181 public boolean isDraggingBubble() {
182 return dragging_bubble;
183 }
Adrien Béraudc9c424d2013-05-30 17:47:35 +1000184
alision2cb99562013-05-30 17:02:20 -0400185 class BubblesThread extends Thread {
186 private boolean running = false;
187 private SurfaceHolder surfaceHolder;
alisione38001f2013-06-04 14:14:39 -0400188 public Boolean suspendFlag = false;
alisionfe9cf712013-05-03 17:26:08 -0400189
alision2cb99562013-05-30 17:02:20 -0400190 BubbleModel model = null;
alisionfe9cf712013-05-03 17:26:08 -0400191
alision2cb99562013-05-30 17:02:20 -0400192 public BubblesThread(SurfaceHolder holder, Context context, Handler handler) {
193 surfaceHolder = holder;
194 }
alisionfe9cf712013-05-03 17:26:08 -0400195
alision2cb99562013-05-30 17:02:20 -0400196 public void setModel(BubbleModel model) {
197 this.model = model;
198 }
alisionfe9cf712013-05-03 17:26:08 -0400199
alision2cb99562013-05-30 17:02:20 -0400200 @Override
201 public void run() {
202 while (running) {
203 Canvas c = null;
204 try {
Adrien Béraud7ed23dc2013-05-06 16:27:24 +1000205
alisione38001f2013-06-04 14:14:39 -0400206 if (suspendFlag) {
207 synchronized (this) {
208 while (suspendFlag) {
209 try {
210 wait();
211 } catch (InterruptedException e) {
212 // TODO Auto-generated catch block
213 e.printStackTrace();
214 }
215 }
216 }
217 } else {
alisione38001f2013-06-04 14:14:39 -0400218 c = surfaceHolder.lockCanvas(null);
Adrien Béraud7ed23dc2013-05-06 16:27:24 +1000219
alisione38001f2013-06-04 14:14:39 -0400220 // for the case the surface is destroyed while already in the loop
221 if (c == null || model == null)
222 continue;
223
224 synchronized (surfaceHolder) {
225 // Log.w(TAG, "Thread doDraw");
226 model.update();
227 doDraw(c);
228 }
alision2cb99562013-05-30 17:02:20 -0400229 }
alisione38001f2013-06-04 14:14:39 -0400230
alision2cb99562013-05-30 17:02:20 -0400231 } finally {
232 if (c != null)
233 surfaceHolder.unlockCanvasAndPost(c);
234 }
235 }
236 }
Adrien Béraud04463092013-05-06 14:17:22 +1000237
alisione38001f2013-06-04 14:14:39 -0400238 public void setPaused(boolean wantToPause) {
239 synchronized (this) {
240 suspendFlag = wantToPause;
241 notify();
242 }
243 }
244
alision2cb99562013-05-30 17:02:20 -0400245 public void setRunning(boolean b) {
246 running = b;
247 }
Adrien Béraud04463092013-05-06 14:17:22 +1000248
alision2cb99562013-05-30 17:02:20 -0400249 public void setSurfaceSize(int width, int height) {
250 synchronized (surfaceHolder) {
251 if (model != null) {
252 model.width = width;
253 model.height = height;
254 }
255 }
256 }
Adrien Béraud04463092013-05-06 14:17:22 +1000257
alision2cb99562013-05-30 17:02:20 -0400258 /**
259 * I got multiple IndexOutOfBoundsException, when switching calls. //FIXME
260 *
261 * @param canvas
262 */
263 private void doDraw(Canvas canvas) {
Adrien Béraud04463092013-05-06 14:17:22 +1000264
alision2cb99562013-05-30 17:02:20 -0400265 synchronized (model) {
266 List<Bubble> bubbles = model.getBubbles();
267 List<Attractor> attractors = model.getAttractors();
Alexandre Lision0edf18c2013-09-23 17:35:50 -0400268
269 Paint tryMe = new Paint();
270
271 canvas.drawColor(Color.WHITE);
272
273 if (dragging_bubble) {
274 // Draw red gradient around to hang up call
275 // canvas.drawColor(Color.RED);
276
Alexandre Lision23628c12013-09-24 11:17:05 -0400277 // LinearGradient grTop = new LinearGradient(0, 0, 0, 40, Color.RED, Color.WHITE, TileMode.CLAMP);
278 // RadialGradient gr = new RadialGradient(model.width/2, model.height/2, model.width/2, Color.WHITE, Color.RED, TileMode.CLAMP);
Alexandre Lision0edf18c2013-09-23 17:35:50 -0400279 Paint p = new Paint();
280 p.setDither(true);
Alexandre Lision23628c12013-09-24 11:17:05 -0400281 // p.setShader(gr);
282 p.setColor(getResources().getColor(R.color.holo_red_light));
283 // p.setXfermode(new PorterDuffXfermode(Mode.))
284 p.setStyle(Style.STROKE);
285 // canvas.drawRect(new RectF(0, 0, model.width, 40), p);
286 p.setStrokeWidth(20);
Alexandre Lision0edf18c2013-09-23 17:35:50 -0400287
Alexandre Lision23628c12013-09-24 11:17:05 -0400288 canvas.drawRect(new RectF(10, 10, model.width - 10, model.height - 10), p);
Alexandre Lision0edf18c2013-09-23 17:35:50 -0400289
Alexandre Lision23628c12013-09-24 11:17:05 -0400290 // canvas.drawRoundRect(new RectF(0,0,model.width, model.height), 200, 200, p);
Alexandre Lision0edf18c2013-09-23 17:35:50 -0400291
Alexandre Lision23628c12013-09-24 11:17:05 -0400292 // LinearGradient grBottom = new LinearGradient(0, model.height, 0, model.height - 40, Color.RED, Color.WHITE, TileMode.CLAMP);
293 // p.setDither(true);
294 // p.setShader(grBottom);
295 // canvas.drawRect(new RectF(0, model.height - 40, model.width, model.height), p);
296 //
297 // LinearGradient grLeft = new LinearGradient(0, 0, 40, 0, Color.RED, Color.WHITE, TileMode.CLAMP);
298 // p.setDither(true);
299 // p.setShader(grLeft);
300 // canvas.drawRect(new RectF(0, 0, 40, model.height), p);
301 //
302 // LinearGradient grRight = new LinearGradient(model.width, 0, model.width - 40, 0, Color.RED, Color.WHITE, TileMode.CLAMP);
303 // p.setDither(true);
304 // p.setShader(grRight);
305 // canvas.drawRect(new RectF(model.width - 40, 0, model.width, model.height), p);
Alexandre Lision0edf18c2013-09-23 17:35:50 -0400306
307 // tryMe.setColor(getResources().getColor(R.color.lighter_gray));
308 // tryMe.setStyle(Paint.Style.FILL);
309 // tryMe.setXfermode(new PorterDuffXfermode(Mode.SRC_OUT));
310 // canvas.drawArc(new RectF(15, 30, model.width - 15, model.height - 30), 0, 360, false, tryMe);
311 }
312
313 tryMe.setStyle(Paint.Style.STROKE);
314 tryMe.setColor(getResources().getColor(R.color.darker_gray));
315 tryMe.setXfermode(null);
316 canvas.drawCircle(model.width / 2, model.height / 2, model.width / 2 - getResources().getDimension(R.dimen.bubble_size), tryMe);
317
alision2cb99562013-05-30 17:02:20 -0400318 try {
Adrien Béraud0c9bd8f2013-05-30 16:16:57 +1000319
alision2cb99562013-05-30 17:02:20 -0400320 for (int i = 0, n = attractors.size(); i < n; i++) {
321 Attractor a = attractors.get(i);
322 canvas.drawBitmap(a.getBitmap(), null, a.getBounds(), null);
323 }
Adrien Béraud33268882013-05-18 03:41:15 +1000324
alision2cb99562013-05-30 17:02:20 -0400325 for (int i = 0, n = bubbles.size(); i < n; i++) {
326 Bubble b = bubbles.get(i);
Alexandre Lision0edf18c2013-09-23 17:35:50 -0400327 if (b.expanded) {
alision465ceba2013-07-04 09:24:30 -0400328 continue;
329 }
alision2cb99562013-05-30 17:02:20 -0400330 canvas.drawBitmap(b.getBitmap(), null, b.getBounds(), null);
Alexandre Lision0edf18c2013-09-23 17:35:50 -0400331 canvas.drawText(b.associated_call.getContact().getmDisplayName(), b.getPosX(), (float) (b.getPosY() - b.getRetractedRadius()
332 * 1.2 * density), getNamePaint(b));
alision34673e62013-06-25 14:40:07 -0400333 }
Alexandre Lision0edf18c2013-09-23 17:35:50 -0400334
alision34673e62013-06-25 14:40:07 -0400335 Bubble first_plan = getExpandedBubble();
336 if (first_plan != null) {
337 canvas.drawBitmap(first_plan.getBitmap(), null, first_plan.getBounds(), null);
Alexandre Lision0edf18c2013-09-23 17:35:50 -0400338
alision729b0a22013-07-02 11:57:33 -0400339 canvas.drawText(first_plan.associated_call.getContact().getmDisplayName(), first_plan.getPosX(),
alision465ceba2013-07-04 09:24:30 -0400340 (float) (first_plan.getPosY() - first_plan.getRetractedRadius() * 1.2 * density), getNamePaint(first_plan));
Alexandre Lision0edf18c2013-09-23 17:35:50 -0400341
alision729b0a22013-07-02 11:57:33 -0400342 canvas.drawText("Transfer", first_plan.getPosX(), (float) (first_plan.getPosY() + first_plan.getRetractedRadius() * 1.5
343 * density), getNamePaint(first_plan));
alisiondf1dac92013-06-27 17:35:53 -0400344
alision729b0a22013-07-02 11:57:33 -0400345 canvas.drawText(getResources().getString(first_plan.getHoldStatus()),
Alexandre Lisionc51ccb12013-09-11 16:00:30 -0400346 (float) (first_plan.getPosX() - first_plan.getRetractedRadius() * 1.5 * density - 15), first_plan.getPosY(),
alision729b0a22013-07-02 11:57:33 -0400347 getNamePaint(first_plan));
348
349 canvas.drawText(getResources().getString(first_plan.getRecordStatus()),
Alexandre Lisionc51ccb12013-09-11 16:00:30 -0400350 (float) (first_plan.getPosX() + first_plan.getRetractedRadius() * 1.5 * density + 15), first_plan.getPosY(),
alision729b0a22013-07-02 11:57:33 -0400351 getNamePaint(first_plan));
alisiondf1dac92013-06-27 17:35:53 -0400352
alision2cb99562013-05-30 17:02:20 -0400353 }
354
355 } catch (IndexOutOfBoundsException e) {
356 Log.e(TAG, e.toString());
357 }
358 }
359 }
alision34673e62013-06-25 14:40:07 -0400360
361 }
362
363 private Paint getNamePaint(Bubble b) {
alision50fa0722013-06-25 17:29:44 -0400364 if (b.expanded) {
365 white_name_paint.setTextSize(15 * b.target_scale * textDensity);
alision34673e62013-06-25 14:40:07 -0400366 return white_name_paint;
alision50fa0722013-06-25 17:29:44 -0400367 }
368 black_name_paint.setTextSize(18 * b.target_scale * textDensity);
alision34673e62013-06-25 14:40:07 -0400369 return black_name_paint;
alision2cb99562013-05-30 17:02:20 -0400370 }
Adrien Béraud04463092013-05-06 14:17:22 +1000371
alisione38001f2013-06-04 14:14:39 -0400372 @Override
373 public boolean onTouch(View v, MotionEvent event) {
Alexandre Lision23628c12013-09-24 11:17:05 -0400374// Log.w(TAG, "onTouch " + event.getAction());
alisione38001f2013-06-04 14:14:39 -0400375
376 int action = event.getActionMasked();
377
378 if (action == MotionEvent.ACTION_UP) {
379 if (thread.suspendFlag) {
380 Log.i(TAG, "Relaunch drawing thread");
381 thread.setPaused(false);
382 }
383
384 List<Bubble> bubbles = model.getBubbles();
385 final int n_bubbles = bubbles.size();
386 for (int i = 0; i < n_bubbles; i++) {
387 Bubble b = bubbles.get(i);
388 if (b.dragged) {
389 b.dragged = false;
390 b.target_scale = 1.f;
Alexandre Lisionfdef8852013-09-24 13:32:38 -0400391 if (b.isOnBorder(model.width, model.height) && !b.expanded) {
Alexandre Lision0edf18c2013-09-23 17:35:50 -0400392 b.markedToDie = true;
393 ((CallActivity) callback.getActivity()).onCallEnded(b.associated_call);
394 }
alisione38001f2013-06-04 14:14:39 -0400395 }
396 }
397 dragging_bubble = false;
398 } else if (action != MotionEvent.ACTION_DOWN && !isDraggingBubble() && !thread.suspendFlag) {
399
400 Log.i(TAG, "Not dragging thread should be stopped");
401 thread.setPaused(true);
402 // thread.holdDrawing();
403 }
404
405 return gDetector.onTouchEvent(event);
406 }
407
alision34673e62013-06-25 14:40:07 -0400408 private Bubble getExpandedBubble() {
409 List<Bubble> bubbles = model.getBubbles();
410 final int n_bubbles = bubbles.size();
411 for (int i = 0; i < n_bubbles; i++) {
412 Bubble b = bubbles.get(i);
413 if (b.expanded) {
414 return b;
415 }
416 }
417 return null;
418 }
alision2ec64f92013-06-17 17:28:58 -0400419
alisiondf1dac92013-06-27 17:35:53 -0400420 public void restartDrawing() {
421 if (thread != null && thread.suspendFlag) {
422 Log.i(TAG, "Relaunch drawing thread");
423 thread.setPaused(false);
424 }
425 }
426
427 public void setFragment(CallFragment callFragment) {
428 callback = callFragment;
429
430 }
431
432 public void stopThread() {
433 if (thread != null && thread.suspendFlag) {
434 Log.i(TAG, "Stop drawing thread");
435 thread.setRunning(false);
436 thread.setPaused(false);
437 }
438
439 }
440
alision58356b72013-06-03 17:13:36 -0400441 class MyOnGestureListener implements OnGestureListener {
442 @Override
443 public boolean onDown(MotionEvent event) {
444 List<Bubble> bubbles = model.getBubbles();
445 final int n_bubbles = bubbles.size();
alision2ec64f92013-06-17 17:28:58 -0400446 Bubble expand = getExpandedBubble();
alision34673e62013-06-25 14:40:07 -0400447 if (expand != null) {
448 if (!expand.intersects(event.getX(), event.getY())) {
449 expand.retract();
450 } else {
Alexandre Lision23628c12013-09-24 11:17:05 -0400451// Log.d("Main", "getAction");
alision50fa0722013-06-25 17:29:44 -0400452 switch (expand.getAction(event.getX(), event.getY())) {
453 case 0:
454 expand.retract();
455 break;
456 case 1:
alisiondf1dac92013-06-27 17:35:53 -0400457 if (expand.associated_call.isOnHold()) {
458 ((CallActivity) callback.getActivity()).onCallResumed(expand.associated_call);
459 } else {
460 ((CallActivity) callback.getActivity()).onCallSuspended(expand.associated_call);
461 }
462
alision50fa0722013-06-25 17:29:44 -0400463 break;
464 case 2:
465 Log.d("Main", "onRecordCall");
466 ((CallActivity) callback.getActivity()).onRecordCall(expand.associated_call);
467 break;
468 case 3:
Alexandre Lision2b237922013-09-09 16:23:02 -0400469 callback.makeTransfer(expand);
alision50fa0722013-06-25 17:29:44 -0400470 Toast.makeText(getContext(), "Not implemented here", Toast.LENGTH_SHORT).show();
471 break;
alision34673e62013-06-25 14:40:07 -0400472 }
473 }
474 return true;
alision2ec64f92013-06-17 17:28:58 -0400475 }
Alexandre Lision23628c12013-09-24 11:17:05 -0400476// Log.d("Main", "onDown");
alision58356b72013-06-03 17:13:36 -0400477 for (int i = 0; i < n_bubbles; i++) {
478 Bubble b = bubbles.get(i);
alision34673e62013-06-25 14:40:07 -0400479 if (b.intersects(event.getX(), event.getY()) && !b.expanded) {
alision58356b72013-06-03 17:13:36 -0400480 b.dragged = true;
481 b.last_drag = System.nanoTime();
482 b.setPos(event.getX(), event.getY());
483 b.target_scale = .8f;
484 dragging_bubble = true;
485 }
486 }
487 return true;
488 }
alision2ec64f92013-06-17 17:28:58 -0400489
alision58356b72013-06-03 17:13:36 -0400490 @Override
491 public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
Alexandre Lision23628c12013-09-24 11:17:05 -0400492// Log.d("Main", "onFling");
alision58356b72013-06-03 17:13:36 -0400493 return true;
494 }
495
496 @Override
497 public void onLongPress(MotionEvent e) {
Alexandre Lision23628c12013-09-24 11:17:05 -0400498// Log.d("Main", "onLongPress");
alision2ec64f92013-06-17 17:28:58 -0400499 if (isDraggingBubble()) {
500 Bubble b = getDraggedBubble(e);
alision729b0a22013-07-02 11:57:33 -0400501 b.expand();
alision2ec64f92013-06-17 17:28:58 -0400502 }
503 }
504
alision2ec64f92013-06-17 17:28:58 -0400505 private Bubble getDraggedBubble(MotionEvent e) {
506 List<Bubble> bubbles = model.getBubbles();
507 final int n_bubbles = bubbles.size();
508 for (int i = 0; i < n_bubbles; i++) {
509 Bubble b = bubbles.get(i);
510 if (b.intersects(e.getX(), e.getY())) {
511 return b;
512 }
513 }
514 return null;
alision58356b72013-06-03 17:13:36 -0400515 }
516
517 @Override
518 public boolean onScroll(MotionEvent e1, MotionEvent event, float distanceX, float distanceY) {
Alexandre Lision23628c12013-09-24 11:17:05 -0400519// Log.d("Main", "onScroll");
alision58356b72013-06-03 17:13:36 -0400520 List<Bubble> bubbles = model.getBubbles();
521 final int n_bubbles = bubbles.size();
522 long now = System.nanoTime();
523 for (int i = 0; i < n_bubbles; i++) {
524 Bubble b = bubbles.get(i);
525 if (b.dragged) {
526 float x = event.getX(), y = event.getY();
527 float dt = (float) ((now - b.last_drag) / 1000000000.);
528 float dx = x - b.getPosX(), dy = y - b.getPosY();
529 b.last_drag = now;
alision58356b72013-06-03 17:13:36 -0400530 b.setPos(event.getX(), event.getY());
alision58356b72013-06-03 17:13:36 -0400531 b.speed.x = dx / dt;
532 b.speed.y = dy / dt;
alision58356b72013-06-03 17:13:36 -0400533 // }
534 return true;
535 }
536 }
537 return true;
538 }
539
540 @Override
541 public void onShowPress(MotionEvent e) {
Alexandre Lision23628c12013-09-24 11:17:05 -0400542// Log.d("Main", "onShowPress");
alision58356b72013-06-03 17:13:36 -0400543
544 }
545
546 @Override
547 public boolean onSingleTapUp(MotionEvent e) {
Alexandre Lision23628c12013-09-24 11:17:05 -0400548// Log.d("Main", "onSingleTapUp");
alision58356b72013-06-03 17:13:36 -0400549 return true;
550 }
551 }
alisionfe9cf712013-05-03 17:26:08 -0400552}