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;
|
Alexandre Lision | 0edf18c | 2013-09-23 17:35:50 -0400 | [diff] [blame^] | 40 | import android.graphics.LinearGradient;
|
Adrien Béraud | 3326888 | 2013-05-18 03:41:15 +1000 | [diff] [blame] | 41 | import android.graphics.Paint;
|
Adrien Béraud | 6bbce91 | 2013-05-24 00:48:13 +1000 | [diff] [blame] | 42 | import android.graphics.Paint.Align;
|
Alexandre Lision | 0edf18c | 2013-09-23 17:35:50 -0400 | [diff] [blame^] | 43 | import android.graphics.RectF;
|
| 44 | import android.graphics.Shader.TileMode;
|
Adrien Béraud | 0446309 | 2013-05-06 14:17:22 +1000 | [diff] [blame] | 45 | import android.os.Handler;
|
| 46 | import android.os.Message;
|
alision | fe9cf71 | 2013-05-03 17:26:08 -0400 | [diff] [blame] | 47 | import android.util.AttributeSet;
|
| 48 | import android.util.Log;
|
alision | 58356b7 | 2013-06-03 17:13:36 -0400 | [diff] [blame] | 49 | import android.view.GestureDetector;
|
| 50 | import android.view.GestureDetector.OnGestureListener;
|
alision | fe9cf71 | 2013-05-03 17:26:08 -0400 | [diff] [blame] | 51 | import android.view.MotionEvent;
|
Adrien Béraud | 0446309 | 2013-05-06 14:17:22 +1000 | [diff] [blame] | 52 | import android.view.SurfaceHolder;
|
| 53 | import android.view.SurfaceView;
|
alision | fe9cf71 | 2013-05-03 17:26:08 -0400 | [diff] [blame] | 54 | import android.view.View;
|
Adrien Béraud | 0446309 | 2013-05-06 14:17:22 +1000 | [diff] [blame] | 55 | import android.view.View.OnTouchListener;
|
alision | 34673e6 | 2013-06-25 14:40:07 -0400 | [diff] [blame] | 56 | import android.widget.Toast;
|
| 57 |
|
Alexandre Lision | 0edf18c | 2013-09-23 17:35:50 -0400 | [diff] [blame^] | 58 | import com.savoirfairelinux.sflphone.R;
|
alision | 34673e6 | 2013-06-25 14:40:07 -0400 | [diff] [blame] | 59 | import com.savoirfairelinux.sflphone.client.CallActivity;
|
| 60 | import com.savoirfairelinux.sflphone.fragments.CallFragment;
|
alision | fe9cf71 | 2013-05-03 17:26:08 -0400 | [diff] [blame] | 61 |
|
alision | 2cb9956 | 2013-05-30 17:02:20 -0400 | [diff] [blame] | 62 | public class BubblesView extends SurfaceView implements SurfaceHolder.Callback, OnTouchListener {
|
| 63 | private static final String TAG = BubblesView.class.getSimpleName();
|
alision | fe9cf71 | 2013-05-03 17:26:08 -0400 | [diff] [blame] | 64 |
|
alision | 2cb9956 | 2013-05-30 17:02:20 -0400 | [diff] [blame] | 65 | private BubblesThread thread = null;
|
| 66 | private BubbleModel model;
|
alision | fe9cf71 | 2013-05-03 17:26:08 -0400 | [diff] [blame] | 67 |
|
alision | 2cb9956 | 2013-05-30 17:02:20 -0400 | [diff] [blame] | 68 | private Paint attractor_paint = new Paint();
|
alision | 34673e6 | 2013-06-25 14:40:07 -0400 | [diff] [blame] | 69 | 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éraud | 6bbce91 | 2013-05-24 00:48:13 +1000 | [diff] [blame] | 71 |
|
alision | e38001f | 2013-06-04 14:14:39 -0400 | [diff] [blame] | 72 | private GestureDetector gDetector;
|
| 73 |
|
alision | 2cb9956 | 2013-05-30 17:02:20 -0400 | [diff] [blame] | 74 | private float density;
|
| 75 | private float textDensity;
|
Adrien Béraud | 3326888 | 2013-05-18 03:41:15 +1000 | [diff] [blame] | 76 |
|
alision | 2cb9956 | 2013-05-30 17:02:20 -0400 | [diff] [blame] | 77 | private boolean dragging_bubble = false;
|
Adrien Béraud | c9c424d | 2013-05-30 17:47:35 +1000 | [diff] [blame] | 78 |
|
alision | 34673e6 | 2013-06-25 14:40:07 -0400 | [diff] [blame] | 79 | private CallFragment callback;
|
| 80 |
|
alision | 2cb9956 | 2013-05-30 17:02:20 -0400 | [diff] [blame] | 81 | public BubblesView(Context context, AttributeSet attrs) {
|
| 82 | super(context, attrs);
|
alision | fe9cf71 | 2013-05-03 17:26:08 -0400 | [diff] [blame] | 83 |
|
alision | 2cb9956 | 2013-05-30 17:02:20 -0400 | [diff] [blame] | 84 | density = getResources().getDisplayMetrics().density;
|
| 85 | textDensity = getResources().getDisplayMetrics().scaledDensity;
|
Adrien Béraud | 6bbce91 | 2013-05-24 00:48:13 +1000 | [diff] [blame] | 86 |
|
alision | 2cb9956 | 2013-05-30 17:02:20 -0400 | [diff] [blame] | 87 | SurfaceHolder holder = getHolder();
|
| 88 | holder.addCallback(this);
|
alision | fe9cf71 | 2013-05-03 17:26:08 -0400 | [diff] [blame] | 89 |
|
alision | 2cb9956 | 2013-05-30 17:02:20 -0400 | [diff] [blame] | 90 | // create thread only; it's started in surfaceCreated()
|
| 91 | createThread();
|
alision | fe9cf71 | 2013-05-03 17:26:08 -0400 | [diff] [blame] | 92 |
|
alision | 2cb9956 | 2013-05-30 17:02:20 -0400 | [diff] [blame] | 93 | setOnTouchListener(this);
|
| 94 | setFocusable(true);
|
Adrien Béraud | 3326888 | 2013-05-18 03:41:15 +1000 | [diff] [blame] | 95 |
|
alision | 2cb9956 | 2013-05-30 17:02:20 -0400 | [diff] [blame] | 96 | attractor_paint.setColor(Color.RED);
|
| 97 | // attractor_paint.set
|
alision | 34673e6 | 2013-06-25 14:40:07 -0400 | [diff] [blame] | 98 | 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);
|
alision | 58356b7 | 2013-06-03 17:13:36 -0400 | [diff] [blame] | 105 |
|
| 106 | gDetector = new GestureDetector(getContext(), new MyOnGestureListener());
|
alision | 2cb9956 | 2013-05-30 17:02:20 -0400 | [diff] [blame] | 107 | }
|
alision | fe9cf71 | 2013-05-03 17:26:08 -0400 | [diff] [blame] | 108 |
|
alision | 2cb9956 | 2013-05-30 17:02:20 -0400 | [diff] [blame] | 109 | 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 | }
|
alision | fe9cf71 | 2013-05-03 17:26:08 -0400 | [diff] [blame] | 123 |
|
alision | 2cb9956 | 2013-05-30 17:02:20 -0400 | [diff] [blame] | 124 | public void setModel(BubbleModel model) {
|
| 125 | this.model = model;
|
| 126 | thread.setModel(model);
|
| 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 | /*
|
| 130 | * @Override public void onWindowFocusChanged(boolean hasWindowFocus) { if (!hasWindowFocus) { thread.pause(); } }
|
| 131 | */
|
alision | fe9cf71 | 2013-05-03 17:26:08 -0400 | [diff] [blame] | 132 |
|
alision | 2cb9956 | 2013-05-30 17:02:20 -0400 | [diff] [blame] | 133 | @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 | }
|
alision | fe9cf71 | 2013-05-03 17:26:08 -0400 | [diff] [blame] | 138 |
|
alision | 2cb9956 | 2013-05-30 17:02:20 -0400 | [diff] [blame] | 139 | /*
|
| 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();
|
alision | fe9cf71 | 2013-05-03 17:26:08 -0400 | [diff] [blame] | 147 |
|
alision | 2cb9956 | 2013-05-30 17:02:20 -0400 | [diff] [blame] | 148 | Log.w(TAG, "surfaceCreated");
|
| 149 | thread.setRunning(true);
|
| 150 | thread.start();
|
| 151 | }
|
alision | fe9cf71 | 2013-05-03 17:26:08 -0400 | [diff] [blame] | 152 |
|
alision | 2cb9956 | 2013-05-30 17:02:20 -0400 | [diff] [blame] | 153 | /*
|
| 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);
|
alision | 1005ba1 | 2013-06-19 13:52:44 -0400 | [diff] [blame] | 164 | thread.setPaused(false);
|
alision | 2cb9956 | 2013-05-30 17:02:20 -0400 | [diff] [blame] | 165 | while (retry) {
|
| 166 | try {
|
alision | 1005ba1 | 2013-06-19 13:52:44 -0400 | [diff] [blame] | 167 | Log.w(TAG, "joining...");
|
alision | 2cb9956 | 2013-05-30 17:02:20 -0400 | [diff] [blame] | 168 | thread.join();
|
| 169 | retry = false;
|
| 170 | } catch (InterruptedException e) {
|
| 171 | }
|
| 172 | }
|
alision | 1005ba1 | 2013-06-19 13:52:44 -0400 | [diff] [blame] | 173 | Log.w(TAG, "done");
|
alision | 2cb9956 | 2013-05-30 17:02:20 -0400 | [diff] [blame] | 174 | thread = null;
|
| 175 | }
|
alision | fe9cf71 | 2013-05-03 17:26:08 -0400 | [diff] [blame] | 176 |
|
alision | 2cb9956 | 2013-05-30 17:02:20 -0400 | [diff] [blame] | 177 | public boolean isDraggingBubble() {
|
| 178 | return dragging_bubble;
|
| 179 | }
|
Adrien Béraud | c9c424d | 2013-05-30 17:47:35 +1000 | [diff] [blame] | 180 |
|
alision | 2cb9956 | 2013-05-30 17:02:20 -0400 | [diff] [blame] | 181 | class BubblesThread extends Thread {
|
| 182 | private boolean running = false;
|
| 183 | private SurfaceHolder surfaceHolder;
|
alision | e38001f | 2013-06-04 14:14:39 -0400 | [diff] [blame] | 184 | public Boolean suspendFlag = false;
|
alision | fe9cf71 | 2013-05-03 17:26:08 -0400 | [diff] [blame] | 185 |
|
alision | 2cb9956 | 2013-05-30 17:02:20 -0400 | [diff] [blame] | 186 | BubbleModel model = null;
|
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 BubblesThread(SurfaceHolder holder, Context context, Handler handler) {
|
| 189 | surfaceHolder = holder;
|
| 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 | public void setModel(BubbleModel model) {
|
| 193 | this.model = model;
|
| 194 | }
|
alision | fe9cf71 | 2013-05-03 17:26:08 -0400 | [diff] [blame] | 195 |
|
alision | 2cb9956 | 2013-05-30 17:02:20 -0400 | [diff] [blame] | 196 | @Override
|
| 197 | public void run() {
|
| 198 | while (running) {
|
| 199 | Canvas c = null;
|
| 200 | try {
|
Adrien Béraud | 7ed23dc | 2013-05-06 16:27:24 +1000 | [diff] [blame] | 201 |
|
alision | e38001f | 2013-06-04 14:14:39 -0400 | [diff] [blame] | 202 | 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 {
|
alision | e38001f | 2013-06-04 14:14:39 -0400 | [diff] [blame] | 214 | c = surfaceHolder.lockCanvas(null);
|
Adrien Béraud | 7ed23dc | 2013-05-06 16:27:24 +1000 | [diff] [blame] | 215 |
|
alision | e38001f | 2013-06-04 14:14:39 -0400 | [diff] [blame] | 216 | // 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 | }
|
alision | 2cb9956 | 2013-05-30 17:02:20 -0400 | [diff] [blame] | 225 | }
|
alision | e38001f | 2013-06-04 14:14:39 -0400 | [diff] [blame] | 226 |
|
alision | 2cb9956 | 2013-05-30 17:02:20 -0400 | [diff] [blame] | 227 | } finally {
|
| 228 | if (c != null)
|
| 229 | surfaceHolder.unlockCanvasAndPost(c);
|
| 230 | }
|
| 231 | }
|
| 232 | }
|
Adrien Béraud | 0446309 | 2013-05-06 14:17:22 +1000 | [diff] [blame] | 233 |
|
alision | e38001f | 2013-06-04 14:14:39 -0400 | [diff] [blame] | 234 | public void setPaused(boolean wantToPause) {
|
| 235 | synchronized (this) {
|
| 236 | suspendFlag = wantToPause;
|
| 237 | notify();
|
| 238 | }
|
| 239 | }
|
| 240 |
|
alision | 2cb9956 | 2013-05-30 17:02:20 -0400 | [diff] [blame] | 241 | public void setRunning(boolean b) {
|
| 242 | running = b;
|
| 243 | }
|
Adrien Béraud | 0446309 | 2013-05-06 14:17:22 +1000 | [diff] [blame] | 244 |
|
alision | 2cb9956 | 2013-05-30 17:02:20 -0400 | [diff] [blame] | 245 | 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éraud | 0446309 | 2013-05-06 14:17:22 +1000 | [diff] [blame] | 253 |
|
alision | 2cb9956 | 2013-05-30 17:02:20 -0400 | [diff] [blame] | 254 | /**
|
| 255 | * I got multiple IndexOutOfBoundsException, when switching calls. //FIXME
|
| 256 | *
|
| 257 | * @param canvas
|
| 258 | */
|
| 259 | private void doDraw(Canvas canvas) {
|
Adrien Béraud | 0446309 | 2013-05-06 14:17:22 +1000 | [diff] [blame] | 260 |
|
alision | 2cb9956 | 2013-05-30 17:02:20 -0400 | [diff] [blame] | 261 | synchronized (model) {
|
| 262 | List<Bubble> bubbles = model.getBubbles();
|
| 263 | List<Attractor> attractors = model.getAttractors();
|
Alexandre Lision | 0edf18c | 2013-09-23 17:35:50 -0400 | [diff] [blame^] | 264 |
|
| 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 |
|
alision | 2cb9956 | 2013-05-30 17:02:20 -0400 | [diff] [blame] | 305 | try {
|
Adrien Béraud | 0c9bd8f | 2013-05-30 16:16:57 +1000 | [diff] [blame] | 306 |
|
alision | 2cb9956 | 2013-05-30 17:02:20 -0400 | [diff] [blame] | 307 | 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éraud | 3326888 | 2013-05-18 03:41:15 +1000 | [diff] [blame] | 311 |
|
alision | 2cb9956 | 2013-05-30 17:02:20 -0400 | [diff] [blame] | 312 | for (int i = 0, n = bubbles.size(); i < n; i++) {
|
| 313 | Bubble b = bubbles.get(i);
|
Alexandre Lision | 0edf18c | 2013-09-23 17:35:50 -0400 | [diff] [blame^] | 314 | if (b.expanded) {
|
alision | 465ceba | 2013-07-04 09:24:30 -0400 | [diff] [blame] | 315 | continue;
|
| 316 | }
|
alision | 2cb9956 | 2013-05-30 17:02:20 -0400 | [diff] [blame] | 317 | canvas.drawBitmap(b.getBitmap(), null, b.getBounds(), null);
|
Alexandre Lision | 0edf18c | 2013-09-23 17:35:50 -0400 | [diff] [blame^] | 318 | canvas.drawText(b.associated_call.getContact().getmDisplayName(), b.getPosX(), (float) (b.getPosY() - b.getRetractedRadius()
|
| 319 | * 1.2 * density), getNamePaint(b));
|
alision | 34673e6 | 2013-06-25 14:40:07 -0400 | [diff] [blame] | 320 | }
|
Alexandre Lision | 0edf18c | 2013-09-23 17:35:50 -0400 | [diff] [blame^] | 321 |
|
alision | 34673e6 | 2013-06-25 14:40:07 -0400 | [diff] [blame] | 322 | Bubble first_plan = getExpandedBubble();
|
| 323 | if (first_plan != null) {
|
| 324 | canvas.drawBitmap(first_plan.getBitmap(), null, first_plan.getBounds(), null);
|
Alexandre Lision | 0edf18c | 2013-09-23 17:35:50 -0400 | [diff] [blame^] | 325 |
|
alision | 729b0a2 | 2013-07-02 11:57:33 -0400 | [diff] [blame] | 326 | canvas.drawText(first_plan.associated_call.getContact().getmDisplayName(), first_plan.getPosX(),
|
alision | 465ceba | 2013-07-04 09:24:30 -0400 | [diff] [blame] | 327 | (float) (first_plan.getPosY() - first_plan.getRetractedRadius() * 1.2 * density), getNamePaint(first_plan));
|
Alexandre Lision | 0edf18c | 2013-09-23 17:35:50 -0400 | [diff] [blame^] | 328 |
|
alision | 729b0a2 | 2013-07-02 11:57:33 -0400 | [diff] [blame] | 329 | canvas.drawText("Transfer", first_plan.getPosX(), (float) (first_plan.getPosY() + first_plan.getRetractedRadius() * 1.5
|
| 330 | * density), getNamePaint(first_plan));
|
alision | df1dac9 | 2013-06-27 17:35:53 -0400 | [diff] [blame] | 331 |
|
alision | 729b0a2 | 2013-07-02 11:57:33 -0400 | [diff] [blame] | 332 | canvas.drawText(getResources().getString(first_plan.getHoldStatus()),
|
Alexandre Lision | c51ccb1 | 2013-09-11 16:00:30 -0400 | [diff] [blame] | 333 | (float) (first_plan.getPosX() - first_plan.getRetractedRadius() * 1.5 * density - 15), first_plan.getPosY(),
|
alision | 729b0a2 | 2013-07-02 11:57:33 -0400 | [diff] [blame] | 334 | getNamePaint(first_plan));
|
| 335 |
|
| 336 | canvas.drawText(getResources().getString(first_plan.getRecordStatus()),
|
Alexandre Lision | c51ccb1 | 2013-09-11 16:00:30 -0400 | [diff] [blame] | 337 | (float) (first_plan.getPosX() + first_plan.getRetractedRadius() * 1.5 * density + 15), first_plan.getPosY(),
|
alision | 729b0a2 | 2013-07-02 11:57:33 -0400 | [diff] [blame] | 338 | getNamePaint(first_plan));
|
alision | df1dac9 | 2013-06-27 17:35:53 -0400 | [diff] [blame] | 339 |
|
alision | 2cb9956 | 2013-05-30 17:02:20 -0400 | [diff] [blame] | 340 | }
|
| 341 |
|
| 342 | } catch (IndexOutOfBoundsException e) {
|
| 343 | Log.e(TAG, e.toString());
|
| 344 | }
|
| 345 | }
|
| 346 | }
|
alision | 34673e6 | 2013-06-25 14:40:07 -0400 | [diff] [blame] | 347 |
|
| 348 | }
|
| 349 |
|
| 350 | private Paint getNamePaint(Bubble b) {
|
alision | 50fa072 | 2013-06-25 17:29:44 -0400 | [diff] [blame] | 351 | if (b.expanded) {
|
| 352 | white_name_paint.setTextSize(15 * b.target_scale * textDensity);
|
alision | 34673e6 | 2013-06-25 14:40:07 -0400 | [diff] [blame] | 353 | return white_name_paint;
|
alision | 50fa072 | 2013-06-25 17:29:44 -0400 | [diff] [blame] | 354 | }
|
| 355 | black_name_paint.setTextSize(18 * b.target_scale * textDensity);
|
alision | 34673e6 | 2013-06-25 14:40:07 -0400 | [diff] [blame] | 356 | return black_name_paint;
|
alision | 2cb9956 | 2013-05-30 17:02:20 -0400 | [diff] [blame] | 357 | }
|
Adrien Béraud | 0446309 | 2013-05-06 14:17:22 +1000 | [diff] [blame] | 358 |
|
alision | e38001f | 2013-06-04 14:14:39 -0400 | [diff] [blame] | 359 | @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 Lision | 0edf18c | 2013-09-23 17:35:50 -0400 | [diff] [blame^] | 378 | if (b.isOnBorder(model.width, model.height)){
|
| 379 | b.markedToDie = true;
|
| 380 | ((CallActivity) callback.getActivity()).onCallEnded(b.associated_call);
|
| 381 | }
|
alision | e38001f | 2013-06-04 14:14:39 -0400 | [diff] [blame] | 382 | }
|
| 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 |
|
alision | 34673e6 | 2013-06-25 14:40:07 -0400 | [diff] [blame] | 395 | 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 | }
|
alision | 2ec64f9 | 2013-06-17 17:28:58 -0400 | [diff] [blame] | 406 |
|
alision | df1dac9 | 2013-06-27 17:35:53 -0400 | [diff] [blame] | 407 | 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 |
|
alision | 58356b7 | 2013-06-03 17:13:36 -0400 | [diff] [blame] | 428 | 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();
|
alision | 2ec64f9 | 2013-06-17 17:28:58 -0400 | [diff] [blame] | 433 | Bubble expand = getExpandedBubble();
|
alision | 34673e6 | 2013-06-25 14:40:07 -0400 | [diff] [blame] | 434 | if (expand != null) {
|
| 435 | if (!expand.intersects(event.getX(), event.getY())) {
|
| 436 | expand.retract();
|
| 437 | } else {
|
| 438 | Log.d("Main", "getAction");
|
alision | 50fa072 | 2013-06-25 17:29:44 -0400 | [diff] [blame] | 439 | switch (expand.getAction(event.getX(), event.getY())) {
|
| 440 | case 0:
|
| 441 | expand.retract();
|
| 442 | break;
|
| 443 | case 1:
|
alision | df1dac9 | 2013-06-27 17:35:53 -0400 | [diff] [blame] | 444 | 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 |
|
alision | 50fa072 | 2013-06-25 17:29:44 -0400 | [diff] [blame] | 450 | break;
|
| 451 | case 2:
|
| 452 | Log.d("Main", "onRecordCall");
|
| 453 | ((CallActivity) callback.getActivity()).onRecordCall(expand.associated_call);
|
| 454 | break;
|
| 455 | case 3:
|
Alexandre Lision | 2b23792 | 2013-09-09 16:23:02 -0400 | [diff] [blame] | 456 | callback.makeTransfer(expand);
|
alision | 50fa072 | 2013-06-25 17:29:44 -0400 | [diff] [blame] | 457 | Toast.makeText(getContext(), "Not implemented here", Toast.LENGTH_SHORT).show();
|
| 458 | break;
|
alision | 34673e6 | 2013-06-25 14:40:07 -0400 | [diff] [blame] | 459 | }
|
| 460 | }
|
| 461 | return true;
|
alision | 2ec64f9 | 2013-06-17 17:28:58 -0400 | [diff] [blame] | 462 | }
|
alision | 58356b7 | 2013-06-03 17:13:36 -0400 | [diff] [blame] | 463 | Log.d("Main", "onDown");
|
| 464 | for (int i = 0; i < n_bubbles; i++) {
|
| 465 | Bubble b = bubbles.get(i);
|
alision | 34673e6 | 2013-06-25 14:40:07 -0400 | [diff] [blame] | 466 | if (b.intersects(event.getX(), event.getY()) && !b.expanded) {
|
alision | 58356b7 | 2013-06-03 17:13:36 -0400 | [diff] [blame] | 467 | 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 | }
|
alision | 2ec64f9 | 2013-06-17 17:28:58 -0400 | [diff] [blame] | 476 |
|
alision | 58356b7 | 2013-06-03 17:13:36 -0400 | [diff] [blame] | 477 | @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");
|
alision | 2ec64f9 | 2013-06-17 17:28:58 -0400 | [diff] [blame] | 486 | if (isDraggingBubble()) {
|
| 487 | Bubble b = getDraggedBubble(e);
|
alision | 729b0a2 | 2013-07-02 11:57:33 -0400 | [diff] [blame] | 488 | b.expand();
|
alision | 2ec64f9 | 2013-06-17 17:28:58 -0400 | [diff] [blame] | 489 | }
|
| 490 | }
|
| 491 |
|
alision | 2ec64f9 | 2013-06-17 17:28:58 -0400 | [diff] [blame] | 492 | 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;
|
alision | 58356b7 | 2013-06-03 17:13:36 -0400 | [diff] [blame] | 502 | }
|
| 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;
|
alision | 58356b7 | 2013-06-03 17:13:36 -0400 | [diff] [blame] | 517 | b.setPos(event.getX(), event.getY());
|
alision | 58356b7 | 2013-06-03 17:13:36 -0400 | [diff] [blame] | 518 | b.speed.x = dx / dt;
|
| 519 | b.speed.y = dy / dt;
|
alision | 58356b7 | 2013-06-03 17:13:36 -0400 | [diff] [blame] | 520 | // }
|
| 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 | }
|
alision | fe9cf71 | 2013-05-03 17:26:08 -0400 | [diff] [blame] | 539 | }
|