alision | cc7bb42 | 2013-06-06 15:31:39 -0400 | [diff] [blame] | 1 | /*
|
| 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éraud | 0446309 | 2013-05-06 14:17:22 +1000 | [diff] [blame] | 33 | package com.savoirfairelinux.sflphone.model;
|
alision | fe9cf71 | 2013-05-03 17:26:08 -0400 | [diff] [blame] | 34 |
|
Adrien Béraud | 0c9bd8f | 2013-05-30 16:16:57 +1000 | [diff] [blame] | 35 | import java.util.List;
|
| 36 |
|
alision | fe9cf71 | 2013-05-03 17:26:08 -0400 | [diff] [blame] | 37 | import android.content.Context;
|
| 38 | import android.graphics.Canvas;
|
| 39 | import android.graphics.Color;
|
Adrien Béraud | 3326888 | 2013-05-18 03:41:15 +1000 | [diff] [blame] | 40 | import android.graphics.Paint;
|
Adrien Béraud | 6bbce91 | 2013-05-24 00:48:13 +1000 | [diff] [blame] | 41 | import android.graphics.Paint.Align;
|
Adrien Béraud | 0446309 | 2013-05-06 14:17:22 +1000 | [diff] [blame] | 42 | import android.os.Handler;
|
| 43 | import android.os.Message;
|
alision | fe9cf71 | 2013-05-03 17:26:08 -0400 | [diff] [blame] | 44 | import android.util.AttributeSet;
|
| 45 | import android.util.Log;
|
alision | 58356b7 | 2013-06-03 17:13:36 -0400 | [diff] [blame] | 46 | import android.view.GestureDetector;
|
| 47 | import android.view.GestureDetector.OnGestureListener;
|
alision | fe9cf71 | 2013-05-03 17:26:08 -0400 | [diff] [blame] | 48 | import android.view.MotionEvent;
|
Adrien Béraud | 0446309 | 2013-05-06 14:17:22 +1000 | [diff] [blame] | 49 | import android.view.SurfaceHolder;
|
| 50 | import android.view.SurfaceView;
|
alision | fe9cf71 | 2013-05-03 17:26:08 -0400 | [diff] [blame] | 51 | import android.view.View;
|
Adrien Béraud | 0446309 | 2013-05-06 14:17:22 +1000 | [diff] [blame] | 52 | import android.view.View.OnTouchListener;
|
alision | 34673e6 | 2013-06-25 14:40:07 -0400 | [diff] [blame] | 53 | import android.widget.Toast;
|
| 54 |
|
| 55 | import com.savoirfairelinux.sflphone.client.CallActivity;
|
| 56 | import com.savoirfairelinux.sflphone.fragments.CallFragment;
|
alision | fe9cf71 | 2013-05-03 17:26:08 -0400 | [diff] [blame] | 57 |
|
alision | 2cb9956 | 2013-05-30 17:02:20 -0400 | [diff] [blame] | 58 | public class BubblesView extends SurfaceView implements SurfaceHolder.Callback, OnTouchListener {
|
| 59 | private static final String TAG = BubblesView.class.getSimpleName();
|
alision | fe9cf71 | 2013-05-03 17:26:08 -0400 | [diff] [blame] | 60 |
|
alision | 2cb9956 | 2013-05-30 17:02:20 -0400 | [diff] [blame] | 61 | private BubblesThread thread = null;
|
| 62 | private BubbleModel model;
|
alision | fe9cf71 | 2013-05-03 17:26:08 -0400 | [diff] [blame] | 63 |
|
alision | 2cb9956 | 2013-05-30 17:02:20 -0400 | [diff] [blame] | 64 | private Paint attractor_paint = new Paint();
|
alision | 34673e6 | 2013-06-25 14:40:07 -0400 | [diff] [blame] | 65 | private Paint black_name_paint = new Paint(Paint.ANTI_ALIAS_FLAG);
|
| 66 | private Paint white_name_paint = new Paint(Paint.ANTI_ALIAS_FLAG);
|
Adrien Béraud | 6bbce91 | 2013-05-24 00:48:13 +1000 | [diff] [blame] | 67 |
|
alision | e38001f | 2013-06-04 14:14:39 -0400 | [diff] [blame] | 68 | private GestureDetector gDetector;
|
| 69 |
|
alision | 2cb9956 | 2013-05-30 17:02:20 -0400 | [diff] [blame] | 70 | private float density;
|
| 71 | private float textDensity;
|
Adrien Béraud | 3326888 | 2013-05-18 03:41:15 +1000 | [diff] [blame] | 72 |
|
alision | 2cb9956 | 2013-05-30 17:02:20 -0400 | [diff] [blame] | 73 | private boolean dragging_bubble = false;
|
Adrien Béraud | c9c424d | 2013-05-30 17:47:35 +1000 | [diff] [blame] | 74 |
|
alision | 34673e6 | 2013-06-25 14:40:07 -0400 | [diff] [blame] | 75 | private CallFragment callback;
|
| 76 |
|
alision | 2cb9956 | 2013-05-30 17:02:20 -0400 | [diff] [blame] | 77 | public BubblesView(Context context, AttributeSet attrs) {
|
| 78 | super(context, attrs);
|
alision | fe9cf71 | 2013-05-03 17:26:08 -0400 | [diff] [blame] | 79 |
|
alision | 2cb9956 | 2013-05-30 17:02:20 -0400 | [diff] [blame] | 80 | density = getResources().getDisplayMetrics().density;
|
| 81 | textDensity = getResources().getDisplayMetrics().scaledDensity;
|
Adrien Béraud | 6bbce91 | 2013-05-24 00:48:13 +1000 | [diff] [blame] | 82 |
|
alision | 2cb9956 | 2013-05-30 17:02:20 -0400 | [diff] [blame] | 83 | SurfaceHolder holder = getHolder();
|
| 84 | holder.addCallback(this);
|
alision | fe9cf71 | 2013-05-03 17:26:08 -0400 | [diff] [blame] | 85 |
|
alision | 2cb9956 | 2013-05-30 17:02:20 -0400 | [diff] [blame] | 86 | // create thread only; it's started in surfaceCreated()
|
| 87 | createThread();
|
alision | fe9cf71 | 2013-05-03 17:26:08 -0400 | [diff] [blame] | 88 |
|
alision | 2cb9956 | 2013-05-30 17:02:20 -0400 | [diff] [blame] | 89 | setOnTouchListener(this);
|
| 90 | setFocusable(true);
|
Adrien Béraud | 3326888 | 2013-05-18 03:41:15 +1000 | [diff] [blame] | 91 |
|
alision | 2cb9956 | 2013-05-30 17:02:20 -0400 | [diff] [blame] | 92 | attractor_paint.setColor(Color.RED);
|
| 93 | // attractor_paint.set
|
alision | 34673e6 | 2013-06-25 14:40:07 -0400 | [diff] [blame] | 94 | black_name_paint.setTextSize(18 * textDensity);
|
| 95 | black_name_paint.setColor(0xFF303030);
|
| 96 | black_name_paint.setTextAlign(Align.CENTER);
|
| 97 |
|
| 98 | white_name_paint.setTextSize(18 * textDensity);
|
| 99 | white_name_paint.setColor(0xFFEEEEEE);
|
| 100 | white_name_paint.setTextAlign(Align.CENTER);
|
alision | 58356b7 | 2013-06-03 17:13:36 -0400 | [diff] [blame] | 101 |
|
| 102 | gDetector = new GestureDetector(getContext(), new MyOnGestureListener());
|
alision | 2cb9956 | 2013-05-30 17:02:20 -0400 | [diff] [blame] | 103 | }
|
alision | fe9cf71 | 2013-05-03 17:26:08 -0400 | [diff] [blame] | 104 |
|
alision | 2cb9956 | 2013-05-30 17:02:20 -0400 | [diff] [blame] | 105 | private void createThread() {
|
| 106 | if (thread != null)
|
| 107 | return;
|
| 108 | thread = new BubblesThread(getHolder(), getContext(), new Handler() {
|
| 109 | @Override
|
| 110 | public void handleMessage(Message m) {
|
| 111 | /*
|
| 112 | * mStatusText.setVisibility(m.getData().getInt("viz")); mStatusText.setText(m.getData().getString("text"));
|
| 113 | */
|
| 114 | }
|
| 115 | });
|
| 116 | if (model != null)
|
| 117 | thread.setModel(model);
|
| 118 | }
|
alision | fe9cf71 | 2013-05-03 17:26:08 -0400 | [diff] [blame] | 119 |
|
alision | 2cb9956 | 2013-05-30 17:02:20 -0400 | [diff] [blame] | 120 | public void setModel(BubbleModel model) {
|
| 121 | this.model = model;
|
| 122 | thread.setModel(model);
|
| 123 | }
|
alision | fe9cf71 | 2013-05-03 17:26:08 -0400 | [diff] [blame] | 124 |
|
alision | 2cb9956 | 2013-05-30 17:02:20 -0400 | [diff] [blame] | 125 | /*
|
| 126 | * @Override public void onWindowFocusChanged(boolean hasWindowFocus) { if (!hasWindowFocus) { thread.pause(); } }
|
| 127 | */
|
alision | fe9cf71 | 2013-05-03 17:26:08 -0400 | [diff] [blame] | 128 |
|
alision | 2cb9956 | 2013-05-30 17:02:20 -0400 | [diff] [blame] | 129 | @Override
|
| 130 | public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
|
| 131 | Log.w(TAG, "surfaceChanged " + width + "-" + height);
|
| 132 | thread.setSurfaceSize(width, height);
|
| 133 | }
|
alision | fe9cf71 | 2013-05-03 17:26:08 -0400 | [diff] [blame] | 134 |
|
alision | 2cb9956 | 2013-05-30 17:02:20 -0400 | [diff] [blame] | 135 | /*
|
| 136 | * Callback invoked when the Surface has been created and is ready to be used.
|
| 137 | */
|
| 138 | @Override
|
| 139 | public void surfaceCreated(SurfaceHolder holder) {
|
| 140 | // start the thread here so that we don't busy-wait in run()
|
| 141 | // waiting for the surface to be created
|
| 142 | createThread();
|
alision | fe9cf71 | 2013-05-03 17:26:08 -0400 | [diff] [blame] | 143 |
|
alision | 2cb9956 | 2013-05-30 17:02:20 -0400 | [diff] [blame] | 144 | Log.w(TAG, "surfaceCreated");
|
| 145 | thread.setRunning(true);
|
| 146 | thread.start();
|
| 147 | }
|
alision | fe9cf71 | 2013-05-03 17:26:08 -0400 | [diff] [blame] | 148 |
|
alision | 2cb9956 | 2013-05-30 17:02:20 -0400 | [diff] [blame] | 149 | /*
|
| 150 | * Callback invoked when the Surface has been destroyed and must no longer be touched. WARNING: after this method returns, the Surface/Canvas must
|
| 151 | * never be touched again!
|
| 152 | */
|
| 153 | @Override
|
| 154 | public void surfaceDestroyed(SurfaceHolder holder) {
|
| 155 | // we have to tell thread to shut down & wait for it to finish, or else
|
| 156 | // it might touch the Surface after we return and explode
|
| 157 | Log.w(TAG, "surfaceDestroyed");
|
| 158 | boolean retry = true;
|
| 159 | thread.setRunning(false);
|
alision | 1005ba1 | 2013-06-19 13:52:44 -0400 | [diff] [blame] | 160 | thread.setPaused(false);
|
alision | 2cb9956 | 2013-05-30 17:02:20 -0400 | [diff] [blame] | 161 | while (retry) {
|
| 162 | try {
|
alision | 1005ba1 | 2013-06-19 13:52:44 -0400 | [diff] [blame] | 163 | Log.w(TAG, "joining...");
|
alision | 2cb9956 | 2013-05-30 17:02:20 -0400 | [diff] [blame] | 164 | thread.join();
|
| 165 | retry = false;
|
| 166 | } catch (InterruptedException e) {
|
| 167 | }
|
| 168 | }
|
alision | 1005ba1 | 2013-06-19 13:52:44 -0400 | [diff] [blame] | 169 | Log.w(TAG, "done");
|
alision | 2cb9956 | 2013-05-30 17:02:20 -0400 | [diff] [blame] | 170 | thread = null;
|
| 171 | }
|
alision | fe9cf71 | 2013-05-03 17:26:08 -0400 | [diff] [blame] | 172 |
|
alision | 2cb9956 | 2013-05-30 17:02:20 -0400 | [diff] [blame] | 173 | public boolean isDraggingBubble() {
|
| 174 | return dragging_bubble;
|
| 175 | }
|
Adrien Béraud | c9c424d | 2013-05-30 17:47:35 +1000 | [diff] [blame] | 176 |
|
alision | 2cb9956 | 2013-05-30 17:02:20 -0400 | [diff] [blame] | 177 | class BubblesThread extends Thread {
|
| 178 | private boolean running = false;
|
| 179 | private SurfaceHolder surfaceHolder;
|
alision | e38001f | 2013-06-04 14:14:39 -0400 | [diff] [blame] | 180 | public Boolean suspendFlag = false;
|
alision | fe9cf71 | 2013-05-03 17:26:08 -0400 | [diff] [blame] | 181 |
|
alision | 2cb9956 | 2013-05-30 17:02:20 -0400 | [diff] [blame] | 182 | BubbleModel model = null;
|
alision | fe9cf71 | 2013-05-03 17:26:08 -0400 | [diff] [blame] | 183 |
|
alision | 2cb9956 | 2013-05-30 17:02:20 -0400 | [diff] [blame] | 184 | public BubblesThread(SurfaceHolder holder, Context context, Handler handler) {
|
| 185 | surfaceHolder = holder;
|
| 186 | }
|
alision | fe9cf71 | 2013-05-03 17:26:08 -0400 | [diff] [blame] | 187 |
|
alision | 2cb9956 | 2013-05-30 17:02:20 -0400 | [diff] [blame] | 188 | public void setModel(BubbleModel model) {
|
| 189 | this.model = model;
|
| 190 | }
|
alision | fe9cf71 | 2013-05-03 17:26:08 -0400 | [diff] [blame] | 191 |
|
alision | 2cb9956 | 2013-05-30 17:02:20 -0400 | [diff] [blame] | 192 | @Override
|
| 193 | public void run() {
|
| 194 | while (running) {
|
| 195 | Canvas c = null;
|
| 196 | try {
|
Adrien Béraud | 7ed23dc | 2013-05-06 16:27:24 +1000 | [diff] [blame] | 197 |
|
alision | e38001f | 2013-06-04 14:14:39 -0400 | [diff] [blame] | 198 | if (suspendFlag) {
|
| 199 | synchronized (this) {
|
| 200 | while (suspendFlag) {
|
| 201 | try {
|
| 202 | wait();
|
| 203 | } catch (InterruptedException e) {
|
| 204 | // TODO Auto-generated catch block
|
| 205 | e.printStackTrace();
|
| 206 | }
|
| 207 | }
|
| 208 | }
|
| 209 | } else {
|
alision | e38001f | 2013-06-04 14:14:39 -0400 | [diff] [blame] | 210 | c = surfaceHolder.lockCanvas(null);
|
Adrien Béraud | 7ed23dc | 2013-05-06 16:27:24 +1000 | [diff] [blame] | 211 |
|
alision | e38001f | 2013-06-04 14:14:39 -0400 | [diff] [blame] | 212 | // for the case the surface is destroyed while already in the loop
|
| 213 | if (c == null || model == null)
|
| 214 | continue;
|
| 215 |
|
| 216 | synchronized (surfaceHolder) {
|
| 217 | // Log.w(TAG, "Thread doDraw");
|
| 218 | model.update();
|
| 219 | doDraw(c);
|
| 220 | }
|
alision | 2cb9956 | 2013-05-30 17:02:20 -0400 | [diff] [blame] | 221 | }
|
alision | e38001f | 2013-06-04 14:14:39 -0400 | [diff] [blame] | 222 |
|
alision | 2cb9956 | 2013-05-30 17:02:20 -0400 | [diff] [blame] | 223 | } finally {
|
| 224 | if (c != null)
|
| 225 | surfaceHolder.unlockCanvasAndPost(c);
|
| 226 | }
|
| 227 | }
|
| 228 | }
|
Adrien Béraud | 0446309 | 2013-05-06 14:17:22 +1000 | [diff] [blame] | 229 |
|
alision | e38001f | 2013-06-04 14:14:39 -0400 | [diff] [blame] | 230 | public void setPaused(boolean wantToPause) {
|
| 231 | synchronized (this) {
|
| 232 | suspendFlag = wantToPause;
|
| 233 | notify();
|
| 234 | }
|
| 235 | }
|
| 236 |
|
alision | 2cb9956 | 2013-05-30 17:02:20 -0400 | [diff] [blame] | 237 | public void setRunning(boolean b) {
|
| 238 | running = b;
|
| 239 | }
|
Adrien Béraud | 0446309 | 2013-05-06 14:17:22 +1000 | [diff] [blame] | 240 |
|
alision | 2cb9956 | 2013-05-30 17:02:20 -0400 | [diff] [blame] | 241 | public void setSurfaceSize(int width, int height) {
|
| 242 | synchronized (surfaceHolder) {
|
| 243 | if (model != null) {
|
| 244 | model.width = width;
|
| 245 | model.height = height;
|
| 246 | }
|
| 247 | }
|
| 248 | }
|
Adrien Béraud | 0446309 | 2013-05-06 14:17:22 +1000 | [diff] [blame] | 249 |
|
alision | 2cb9956 | 2013-05-30 17:02:20 -0400 | [diff] [blame] | 250 | /**
|
| 251 | * I got multiple IndexOutOfBoundsException, when switching calls. //FIXME
|
| 252 | *
|
| 253 | * @param canvas
|
| 254 | */
|
| 255 | private void doDraw(Canvas canvas) {
|
| 256 | canvas.drawColor(Color.WHITE);
|
Adrien Béraud | 0446309 | 2013-05-06 14:17:22 +1000 | [diff] [blame] | 257 |
|
alision | 2cb9956 | 2013-05-30 17:02:20 -0400 | [diff] [blame] | 258 | synchronized (model) {
|
| 259 | List<Bubble> bubbles = model.getBubbles();
|
| 260 | List<Attractor> attractors = model.getAttractors();
|
| 261 | try {
|
Adrien Béraud | 0c9bd8f | 2013-05-30 16:16:57 +1000 | [diff] [blame] | 262 |
|
alision | 2cb9956 | 2013-05-30 17:02:20 -0400 | [diff] [blame] | 263 | for (int i = 0, n = attractors.size(); i < n; i++) {
|
| 264 | Attractor a = attractors.get(i);
|
| 265 | canvas.drawBitmap(a.getBitmap(), null, a.getBounds(), null);
|
| 266 | }
|
Adrien Béraud | 3326888 | 2013-05-18 03:41:15 +1000 | [diff] [blame] | 267 |
|
alision | 2cb9956 | 2013-05-30 17:02:20 -0400 | [diff] [blame] | 268 | for (int i = 0, n = bubbles.size(); i < n; i++) {
|
| 269 | Bubble b = bubbles.get(i);
|
| 270 | canvas.drawBitmap(b.getBitmap(), null, b.getBounds(), null);
|
alision | 729b0a2 | 2013-07-02 11:57:33 -0400 | [diff] [blame] | 271 | canvas.drawText(b.associated_call.getContact().getmDisplayName(), b.getPosX(),
|
| 272 | (float) (b.getPosY() - b.getRetractedRadius() * 1.5 * density), getNamePaint(b));
|
alision | 34673e6 | 2013-06-25 14:40:07 -0400 | [diff] [blame] | 273 | }
|
| 274 | Bubble first_plan = getExpandedBubble();
|
| 275 | if (first_plan != null) {
|
| 276 | canvas.drawBitmap(first_plan.getBitmap(), null, first_plan.getBounds(), null);
|
alision | 729b0a2 | 2013-07-02 11:57:33 -0400 | [diff] [blame] | 277 |
|
| 278 | canvas.drawText(first_plan.associated_call.getContact().getmDisplayName(), first_plan.getPosX(),
|
| 279 | (float) (first_plan.getPosY() - first_plan.getRetractedRadius() * 1.5 * density), getNamePaint(first_plan));
|
| 280 |
|
| 281 | canvas.drawText("Transfer", first_plan.getPosX(), (float) (first_plan.getPosY() + first_plan.getRetractedRadius() * 1.5
|
| 282 | * density), getNamePaint(first_plan));
|
alision | df1dac9 | 2013-06-27 17:35:53 -0400 | [diff] [blame] | 283 |
|
alision | 729b0a2 | 2013-07-02 11:57:33 -0400 | [diff] [blame] | 284 | canvas.drawText(getResources().getString(first_plan.getHoldStatus()),
|
| 285 | (float) (first_plan.getPosX() - first_plan.getRetractedRadius() * 1.5 * density), first_plan.getPosY(),
|
| 286 | getNamePaint(first_plan));
|
| 287 |
|
| 288 | canvas.drawText(getResources().getString(first_plan.getRecordStatus()),
|
| 289 | (float) (first_plan.getPosX() + first_plan.getRetractedRadius() * 1.5 * density), first_plan.getPosY(),
|
| 290 | getNamePaint(first_plan));
|
alision | df1dac9 | 2013-06-27 17:35:53 -0400 | [diff] [blame] | 291 |
|
alision | 2cb9956 | 2013-05-30 17:02:20 -0400 | [diff] [blame] | 292 | }
|
| 293 |
|
| 294 | } catch (IndexOutOfBoundsException e) {
|
| 295 | Log.e(TAG, e.toString());
|
| 296 | }
|
| 297 | }
|
| 298 | }
|
alision | 34673e6 | 2013-06-25 14:40:07 -0400 | [diff] [blame] | 299 |
|
| 300 | }
|
| 301 |
|
| 302 | private Paint getNamePaint(Bubble b) {
|
alision | 50fa072 | 2013-06-25 17:29:44 -0400 | [diff] [blame] | 303 | if (b.expanded) {
|
| 304 | white_name_paint.setTextSize(15 * b.target_scale * textDensity);
|
alision | 34673e6 | 2013-06-25 14:40:07 -0400 | [diff] [blame] | 305 | return white_name_paint;
|
alision | 50fa072 | 2013-06-25 17:29:44 -0400 | [diff] [blame] | 306 | }
|
| 307 | black_name_paint.setTextSize(18 * b.target_scale * textDensity);
|
alision | 34673e6 | 2013-06-25 14:40:07 -0400 | [diff] [blame] | 308 | return black_name_paint;
|
alision | 2cb9956 | 2013-05-30 17:02:20 -0400 | [diff] [blame] | 309 | }
|
Adrien Béraud | 0446309 | 2013-05-06 14:17:22 +1000 | [diff] [blame] | 310 |
|
alision | e38001f | 2013-06-04 14:14:39 -0400 | [diff] [blame] | 311 | @Override
|
| 312 | public boolean onTouch(View v, MotionEvent event) {
|
| 313 | Log.w(TAG, "onTouch " + event.getAction());
|
| 314 |
|
| 315 | int action = event.getActionMasked();
|
| 316 |
|
| 317 | if (action == MotionEvent.ACTION_UP) {
|
| 318 | if (thread.suspendFlag) {
|
| 319 | Log.i(TAG, "Relaunch drawing thread");
|
| 320 | thread.setPaused(false);
|
| 321 | }
|
| 322 |
|
| 323 | List<Bubble> bubbles = model.getBubbles();
|
| 324 | final int n_bubbles = bubbles.size();
|
| 325 | for (int i = 0; i < n_bubbles; i++) {
|
| 326 | Bubble b = bubbles.get(i);
|
| 327 | if (b.dragged) {
|
| 328 | b.dragged = false;
|
| 329 | b.target_scale = 1.f;
|
| 330 | }
|
| 331 | }
|
| 332 | dragging_bubble = false;
|
| 333 | } else if (action != MotionEvent.ACTION_DOWN && !isDraggingBubble() && !thread.suspendFlag) {
|
| 334 |
|
| 335 | Log.i(TAG, "Not dragging thread should be stopped");
|
| 336 | thread.setPaused(true);
|
| 337 | // thread.holdDrawing();
|
| 338 | }
|
| 339 |
|
| 340 | return gDetector.onTouchEvent(event);
|
| 341 | }
|
| 342 |
|
alision | 34673e6 | 2013-06-25 14:40:07 -0400 | [diff] [blame] | 343 | private Bubble getExpandedBubble() {
|
| 344 | List<Bubble> bubbles = model.getBubbles();
|
| 345 | final int n_bubbles = bubbles.size();
|
| 346 | for (int i = 0; i < n_bubbles; i++) {
|
| 347 | Bubble b = bubbles.get(i);
|
| 348 | if (b.expanded) {
|
| 349 | return b;
|
| 350 | }
|
| 351 | }
|
| 352 | return null;
|
| 353 | }
|
alision | 2ec64f9 | 2013-06-17 17:28:58 -0400 | [diff] [blame] | 354 |
|
alision | df1dac9 | 2013-06-27 17:35:53 -0400 | [diff] [blame] | 355 | public void restartDrawing() {
|
| 356 | if (thread != null && thread.suspendFlag) {
|
| 357 | Log.i(TAG, "Relaunch drawing thread");
|
| 358 | thread.setPaused(false);
|
| 359 | }
|
| 360 | }
|
| 361 |
|
| 362 | public void setFragment(CallFragment callFragment) {
|
| 363 | callback = callFragment;
|
| 364 |
|
| 365 | }
|
| 366 |
|
| 367 | public void stopThread() {
|
| 368 | if (thread != null && thread.suspendFlag) {
|
| 369 | Log.i(TAG, "Stop drawing thread");
|
| 370 | thread.setRunning(false);
|
| 371 | thread.setPaused(false);
|
| 372 | }
|
| 373 |
|
| 374 | }
|
| 375 |
|
alision | 58356b7 | 2013-06-03 17:13:36 -0400 | [diff] [blame] | 376 | class MyOnGestureListener implements OnGestureListener {
|
| 377 | @Override
|
| 378 | public boolean onDown(MotionEvent event) {
|
| 379 | List<Bubble> bubbles = model.getBubbles();
|
| 380 | final int n_bubbles = bubbles.size();
|
alision | 2ec64f9 | 2013-06-17 17:28:58 -0400 | [diff] [blame] | 381 | Bubble expand = getExpandedBubble();
|
alision | 34673e6 | 2013-06-25 14:40:07 -0400 | [diff] [blame] | 382 | if (expand != null) {
|
| 383 | if (!expand.intersects(event.getX(), event.getY())) {
|
| 384 | expand.retract();
|
| 385 | } else {
|
| 386 | Log.d("Main", "getAction");
|
alision | 50fa072 | 2013-06-25 17:29:44 -0400 | [diff] [blame] | 387 | switch (expand.getAction(event.getX(), event.getY())) {
|
| 388 | case 0:
|
| 389 | expand.retract();
|
| 390 | break;
|
| 391 | case 1:
|
alision | df1dac9 | 2013-06-27 17:35:53 -0400 | [diff] [blame] | 392 | if (expand.associated_call.isOnHold()) {
|
| 393 | ((CallActivity) callback.getActivity()).onCallResumed(expand.associated_call);
|
| 394 | } else {
|
| 395 | ((CallActivity) callback.getActivity()).onCallSuspended(expand.associated_call);
|
| 396 | }
|
| 397 |
|
alision | 50fa072 | 2013-06-25 17:29:44 -0400 | [diff] [blame] | 398 | break;
|
| 399 | case 2:
|
| 400 | Log.d("Main", "onRecordCall");
|
| 401 | ((CallActivity) callback.getActivity()).onRecordCall(expand.associated_call);
|
| 402 | break;
|
| 403 | case 3:
|
| 404 | Toast.makeText(getContext(), "Not implemented here", Toast.LENGTH_SHORT).show();
|
| 405 | break;
|
alision | 34673e6 | 2013-06-25 14:40:07 -0400 | [diff] [blame] | 406 | }
|
| 407 | }
|
| 408 | return true;
|
alision | 2ec64f9 | 2013-06-17 17:28:58 -0400 | [diff] [blame] | 409 | }
|
alision | 58356b7 | 2013-06-03 17:13:36 -0400 | [diff] [blame] | 410 | Log.d("Main", "onDown");
|
| 411 | for (int i = 0; i < n_bubbles; i++) {
|
| 412 | Bubble b = bubbles.get(i);
|
alision | 34673e6 | 2013-06-25 14:40:07 -0400 | [diff] [blame] | 413 | if (b.intersects(event.getX(), event.getY()) && !b.expanded) {
|
alision | 58356b7 | 2013-06-03 17:13:36 -0400 | [diff] [blame] | 414 | b.dragged = true;
|
| 415 | b.last_drag = System.nanoTime();
|
| 416 | b.setPos(event.getX(), event.getY());
|
| 417 | b.target_scale = .8f;
|
| 418 | dragging_bubble = true;
|
| 419 | }
|
| 420 | }
|
| 421 | return true;
|
| 422 | }
|
alision | 2ec64f9 | 2013-06-17 17:28:58 -0400 | [diff] [blame] | 423 |
|
alision | 58356b7 | 2013-06-03 17:13:36 -0400 | [diff] [blame] | 424 | @Override
|
| 425 | public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
|
| 426 | Log.d("Main", "onFling");
|
| 427 | return true;
|
| 428 | }
|
| 429 |
|
| 430 | @Override
|
| 431 | public void onLongPress(MotionEvent e) {
|
| 432 | Log.d("Main", "onLongPress");
|
alision | 2ec64f9 | 2013-06-17 17:28:58 -0400 | [diff] [blame] | 433 | if (isDraggingBubble()) {
|
| 434 | Bubble b = getDraggedBubble(e);
|
alision | 729b0a2 | 2013-07-02 11:57:33 -0400 | [diff] [blame] | 435 | b.expand();
|
alision | 2ec64f9 | 2013-06-17 17:28:58 -0400 | [diff] [blame] | 436 | }
|
| 437 | }
|
| 438 |
|
alision | 2ec64f9 | 2013-06-17 17:28:58 -0400 | [diff] [blame] | 439 | private Bubble getDraggedBubble(MotionEvent e) {
|
| 440 | List<Bubble> bubbles = model.getBubbles();
|
| 441 | final int n_bubbles = bubbles.size();
|
| 442 | for (int i = 0; i < n_bubbles; i++) {
|
| 443 | Bubble b = bubbles.get(i);
|
| 444 | if (b.intersects(e.getX(), e.getY())) {
|
| 445 | return b;
|
| 446 | }
|
| 447 | }
|
| 448 | return null;
|
alision | 58356b7 | 2013-06-03 17:13:36 -0400 | [diff] [blame] | 449 | }
|
| 450 |
|
| 451 | @Override
|
| 452 | public boolean onScroll(MotionEvent e1, MotionEvent event, float distanceX, float distanceY) {
|
| 453 | Log.d("Main", "onScroll");
|
| 454 | List<Bubble> bubbles = model.getBubbles();
|
| 455 | final int n_bubbles = bubbles.size();
|
| 456 | long now = System.nanoTime();
|
| 457 | for (int i = 0; i < n_bubbles; i++) {
|
| 458 | Bubble b = bubbles.get(i);
|
| 459 | if (b.dragged) {
|
| 460 | float x = event.getX(), y = event.getY();
|
| 461 | float dt = (float) ((now - b.last_drag) / 1000000000.);
|
| 462 | float dx = x - b.getPosX(), dy = y - b.getPosY();
|
| 463 | b.last_drag = now;
|
alision | 58356b7 | 2013-06-03 17:13:36 -0400 | [diff] [blame] | 464 | b.setPos(event.getX(), event.getY());
|
alision | 58356b7 | 2013-06-03 17:13:36 -0400 | [diff] [blame] | 465 | b.speed.x = dx / dt;
|
| 466 | b.speed.y = dy / dt;
|
alision | 58356b7 | 2013-06-03 17:13:36 -0400 | [diff] [blame] | 467 | // }
|
| 468 | return true;
|
| 469 | }
|
| 470 | }
|
| 471 | return true;
|
| 472 | }
|
| 473 |
|
| 474 | @Override
|
| 475 | public void onShowPress(MotionEvent e) {
|
| 476 | Log.d("Main", "onShowPress");
|
| 477 |
|
| 478 | }
|
| 479 |
|
| 480 | @Override
|
| 481 | public boolean onSingleTapUp(MotionEvent e) {
|
| 482 | Log.d("Main", "onSingleTapUp");
|
| 483 | return true;
|
| 484 | }
|
| 485 | }
|
alision | fe9cf71 | 2013-05-03 17:26:08 -0400 | [diff] [blame] | 486 | }
|