alision | cc7bb42 | 2013-06-06 15:31:39 -0400 | [diff] [blame] | 1 | /*
|
Alexandre Lision | c1024c0 | 2014-01-06 11:12:53 -0500 | [diff] [blame] | 2 | * Copyright (C) 2004-2014 Savoir-Faire Linux Inc.
|
alision | cc7bb42 | 2013-06-06 15:31:39 -0400 | [diff] [blame] | 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 Lision | 064e1e0 | 2013-10-01 16:18:42 -0400 | [diff] [blame] | 33 | package org.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 |
|
Alexandre Lision | 064e1e0 | 2013-10-01 16:18:42 -0400 | [diff] [blame] | 37 | import org.sflphone.R;
|
Alexandre Lision | 064e1e0 | 2013-10-01 16:18:42 -0400 | [diff] [blame] | 38 | import org.sflphone.fragments.CallFragment;
|
| 39 |
|
alision | fe9cf71 | 2013-05-03 17:26:08 -0400 | [diff] [blame] | 40 | import android.content.Context;
|
| 41 | import android.graphics.Canvas;
|
Adrien Béraud | 3326888 | 2013-05-18 03:41:15 +1000 | [diff] [blame] | 42 | import android.graphics.Paint;
|
Adrien Béraud | 6bbce91 | 2013-05-24 00:48:13 +1000 | [diff] [blame] | 43 | import android.graphics.Paint.Align;
|
Alexandre Lision | 23628c1 | 2013-09-24 11:17:05 -0400 | [diff] [blame] | 44 | import android.graphics.Paint.Style;
|
Alexandre Lision | 6885547 | 2013-10-10 16:20:46 -0400 | [diff] [blame] | 45 | import android.graphics.PixelFormat;
|
Alexandre Lision | 940a9db | 2013-11-19 17:12:02 -0500 | [diff] [blame] | 46 | import android.graphics.PorterDuff.Mode;
|
| 47 | import android.graphics.PorterDuffXfermode;
|
Alexandre Lision | 0edf18c | 2013-09-23 17:35:50 -0400 | [diff] [blame] | 48 | import android.graphics.RectF;
|
Alexandre Lision | 6885547 | 2013-10-10 16:20:46 -0400 | [diff] [blame] | 49 | import android.os.RemoteException;
|
alision | fe9cf71 | 2013-05-03 17:26:08 -0400 | [diff] [blame] | 50 | import android.util.AttributeSet;
|
| 51 | import android.util.Log;
|
alision | 58356b7 | 2013-06-03 17:13:36 -0400 | [diff] [blame] | 52 | import android.view.GestureDetector;
|
| 53 | import android.view.GestureDetector.OnGestureListener;
|
alision | fe9cf71 | 2013-05-03 17:26:08 -0400 | [diff] [blame] | 54 | import android.view.MotionEvent;
|
Adrien Béraud | 0446309 | 2013-05-06 14:17:22 +1000 | [diff] [blame] | 55 | import android.view.SurfaceHolder;
|
| 56 | import android.view.SurfaceView;
|
alision | fe9cf71 | 2013-05-03 17:26:08 -0400 | [diff] [blame] | 57 | import android.view.View;
|
Adrien Béraud | 0446309 | 2013-05-06 14:17:22 +1000 | [diff] [blame] | 58 | import android.view.View.OnTouchListener;
|
alision | fe9cf71 | 2013-05-03 17:26:08 -0400 | [diff] [blame] | 59 |
|
alision | 2cb9956 | 2013-05-30 17:02:20 -0400 | [diff] [blame] | 60 | public class BubblesView extends SurfaceView implements SurfaceHolder.Callback, OnTouchListener {
|
| 61 | private static final String TAG = BubblesView.class.getSimpleName();
|
alision | fe9cf71 | 2013-05-03 17:26:08 -0400 | [diff] [blame] | 62 |
|
alision | 2cb9956 | 2013-05-30 17:02:20 -0400 | [diff] [blame] | 63 | private BubblesThread thread = null;
|
| 64 | private BubbleModel model;
|
alision | fe9cf71 | 2013-05-03 17:26:08 -0400 | [diff] [blame] | 65 |
|
alision | 34673e6 | 2013-06-25 14:40:07 -0400 | [diff] [blame] | 66 | private Paint black_name_paint = new Paint(Paint.ANTI_ALIAS_FLAG);
|
| 67 | private Paint white_name_paint = new Paint(Paint.ANTI_ALIAS_FLAG);
|
Adrien Béraud | 6bbce91 | 2013-05-24 00:48:13 +1000 | [diff] [blame] | 68 |
|
alision | e38001f | 2013-06-04 14:14:39 -0400 | [diff] [blame] | 69 | private GestureDetector gDetector;
|
| 70 |
|
alision | 2cb9956 | 2013-05-30 17:02:20 -0400 | [diff] [blame] | 71 | private float density;
|
| 72 | private float textDensity;
|
Alexandre Lision | 8ad3959 | 2013-11-21 13:23:58 -0500 | [diff] [blame] | 73 |
|
alision | 2cb9956 | 2013-05-30 17:02:20 -0400 | [diff] [blame] | 74 | private boolean dragging_bubble = false;
|
Adrien Béraud | c9c424d | 2013-05-30 17:47:35 +1000 | [diff] [blame] | 75 |
|
alision | 34673e6 | 2013-06-25 14:40:07 -0400 | [diff] [blame] | 76 | private CallFragment callback;
|
| 77 |
|
alision | 2cb9956 | 2013-05-30 17:02:20 -0400 | [diff] [blame] | 78 | public BubblesView(Context context, AttributeSet attrs) {
|
| 79 | super(context, attrs);
|
alision | fe9cf71 | 2013-05-03 17:26:08 -0400 | [diff] [blame] | 80 |
|
alision | 2cb9956 | 2013-05-30 17:02:20 -0400 | [diff] [blame] | 81 | density = getResources().getDisplayMetrics().density;
|
| 82 | textDensity = getResources().getDisplayMetrics().scaledDensity;
|
Adrien Béraud | 6bbce91 | 2013-05-24 00:48:13 +1000 | [diff] [blame] | 83 |
|
alision | 2cb9956 | 2013-05-30 17:02:20 -0400 | [diff] [blame] | 84 | SurfaceHolder holder = getHolder();
|
| 85 | holder.addCallback(this);
|
alision | fe9cf71 | 2013-05-03 17:26:08 -0400 | [diff] [blame] | 86 |
|
Alexandre Lision | 6885547 | 2013-10-10 16:20:46 -0400 | [diff] [blame] | 87 | this.setZOrderOnTop(true); // necessary
|
| 88 | this.getHolder().setFormat(PixelFormat.TRANSLUCENT);
|
alision | 2cb9956 | 2013-05-30 17:02:20 -0400 | [diff] [blame] | 89 | // create thread only; it's started in surfaceCreated()
|
| 90 | createThread();
|
alision | fe9cf71 | 2013-05-03 17:26:08 -0400 | [diff] [blame] | 91 |
|
alision | 2cb9956 | 2013-05-30 17:02:20 -0400 | [diff] [blame] | 92 | setOnTouchListener(this);
|
| 93 | setFocusable(true);
|
Adrien Béraud | 3326888 | 2013-05-18 03:41:15 +1000 | [diff] [blame] | 94 |
|
alision | 34673e6 | 2013-06-25 14:40:07 -0400 | [diff] [blame] | 95 | black_name_paint.setTextSize(18 * textDensity);
|
| 96 | black_name_paint.setColor(0xFF303030);
|
| 97 | black_name_paint.setTextAlign(Align.CENTER);
|
| 98 |
|
| 99 | white_name_paint.setTextSize(18 * textDensity);
|
| 100 | white_name_paint.setColor(0xFFEEEEEE);
|
| 101 | white_name_paint.setTextAlign(Align.CENTER);
|
alision | 58356b7 | 2013-06-03 17:13:36 -0400 | [diff] [blame] | 102 |
|
| 103 | gDetector = new GestureDetector(getContext(), new MyOnGestureListener());
|
alision | 2cb9956 | 2013-05-30 17:02:20 -0400 | [diff] [blame] | 104 | }
|
alision | fe9cf71 | 2013-05-03 17:26:08 -0400 | [diff] [blame] | 105 |
|
alision | 2cb9956 | 2013-05-30 17:02:20 -0400 | [diff] [blame] | 106 | private void createThread() {
|
| 107 | if (thread != null)
|
| 108 | return;
|
Alexandre Lision | f30ff85 | 2013-12-09 17:08:40 -0500 | [diff] [blame] | 109 | thread = new BubblesThread(getHolder(), getContext());
|
alision | 2cb9956 | 2013-05-30 17:02:20 -0400 | [diff] [blame] | 110 | if (model != null)
|
| 111 | thread.setModel(model);
|
| 112 | }
|
alision | fe9cf71 | 2013-05-03 17:26:08 -0400 | [diff] [blame] | 113 |
|
alision | 2cb9956 | 2013-05-30 17:02:20 -0400 | [diff] [blame] | 114 | public void setModel(BubbleModel model) {
|
| 115 | this.model = model;
|
| 116 | thread.setModel(model);
|
| 117 | }
|
alision | fe9cf71 | 2013-05-03 17:26:08 -0400 | [diff] [blame] | 118 |
|
alision | 2cb9956 | 2013-05-30 17:02:20 -0400 | [diff] [blame] | 119 | /*
|
| 120 | * @Override public void onWindowFocusChanged(boolean hasWindowFocus) { if (!hasWindowFocus) { thread.pause(); } }
|
| 121 | */
|
alision | fe9cf71 | 2013-05-03 17:26:08 -0400 | [diff] [blame] | 122 |
|
alision | 2cb9956 | 2013-05-30 17:02:20 -0400 | [diff] [blame] | 123 | @Override
|
| 124 | public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
|
| 125 | Log.w(TAG, "surfaceChanged " + width + "-" + height);
|
Alexandre Lision | d588bff | 2013-10-08 12:43:01 -0400 | [diff] [blame] | 126 | if (height < model.height) // probably showing the keyboard, don't move!
|
Alexandre Lision | 84208a3 | 2013-09-25 13:18:37 -0400 | [diff] [blame] | 127 | return;
|
| 128 |
|
alision | 2cb9956 | 2013-05-30 17:02:20 -0400 | [diff] [blame] | 129 | thread.setSurfaceSize(width, height);
|
| 130 | }
|
alision | fe9cf71 | 2013-05-03 17:26:08 -0400 | [diff] [blame] | 131 |
|
alision | 2cb9956 | 2013-05-30 17:02:20 -0400 | [diff] [blame] | 132 | /*
|
| 133 | * Callback invoked when the Surface has been created and is ready to be used.
|
| 134 | */
|
| 135 | @Override
|
| 136 | public void surfaceCreated(SurfaceHolder holder) {
|
| 137 | // start the thread here so that we don't busy-wait in run()
|
| 138 | // waiting for the surface to be created
|
| 139 | createThread();
|
alision | fe9cf71 | 2013-05-03 17:26:08 -0400 | [diff] [blame] | 140 |
|
alision | 2cb9956 | 2013-05-30 17:02:20 -0400 | [diff] [blame] | 141 | Log.w(TAG, "surfaceCreated");
|
| 142 | thread.setRunning(true);
|
| 143 | thread.start();
|
| 144 | }
|
alision | fe9cf71 | 2013-05-03 17:26:08 -0400 | [diff] [blame] | 145 |
|
alision | 2cb9956 | 2013-05-30 17:02:20 -0400 | [diff] [blame] | 146 | /*
|
| 147 | * Callback invoked when the Surface has been destroyed and must no longer be touched. WARNING: after this method returns, the Surface/Canvas must
|
| 148 | * never be touched again!
|
| 149 | */
|
| 150 | @Override
|
| 151 | public void surfaceDestroyed(SurfaceHolder holder) {
|
| 152 | // we have to tell thread to shut down & wait for it to finish, or else
|
| 153 | // it might touch the Surface after we return and explode
|
| 154 | Log.w(TAG, "surfaceDestroyed");
|
| 155 | boolean retry = true;
|
| 156 | thread.setRunning(false);
|
alision | 1005ba1 | 2013-06-19 13:52:44 -0400 | [diff] [blame] | 157 | thread.setPaused(false);
|
alision | 2cb9956 | 2013-05-30 17:02:20 -0400 | [diff] [blame] | 158 | while (retry) {
|
| 159 | try {
|
alision | 1005ba1 | 2013-06-19 13:52:44 -0400 | [diff] [blame] | 160 | Log.w(TAG, "joining...");
|
alision | 2cb9956 | 2013-05-30 17:02:20 -0400 | [diff] [blame] | 161 | thread.join();
|
| 162 | retry = false;
|
| 163 | } catch (InterruptedException e) {
|
| 164 | }
|
| 165 | }
|
alision | 1005ba1 | 2013-06-19 13:52:44 -0400 | [diff] [blame] | 166 | Log.w(TAG, "done");
|
alision | 2cb9956 | 2013-05-30 17:02:20 -0400 | [diff] [blame] | 167 | thread = null;
|
| 168 | }
|
alision | fe9cf71 | 2013-05-03 17:26:08 -0400 | [diff] [blame] | 169 |
|
alision | 2cb9956 | 2013-05-30 17:02:20 -0400 | [diff] [blame] | 170 | public boolean isDraggingBubble() {
|
| 171 | return dragging_bubble;
|
| 172 | }
|
Adrien Béraud | c9c424d | 2013-05-30 17:47:35 +1000 | [diff] [blame] | 173 |
|
alision | 2cb9956 | 2013-05-30 17:02:20 -0400 | [diff] [blame] | 174 | class BubblesThread extends Thread {
|
| 175 | private boolean running = false;
|
| 176 | private SurfaceHolder surfaceHolder;
|
alision | e38001f | 2013-06-04 14:14:39 -0400 | [diff] [blame] | 177 | public Boolean suspendFlag = false;
|
alision | fe9cf71 | 2013-05-03 17:26:08 -0400 | [diff] [blame] | 178 |
|
alision | 2cb9956 | 2013-05-30 17:02:20 -0400 | [diff] [blame] | 179 | BubbleModel model = null;
|
alision | fe9cf71 | 2013-05-03 17:26:08 -0400 | [diff] [blame] | 180 |
|
Alexandre Lision | f30ff85 | 2013-12-09 17:08:40 -0500 | [diff] [blame] | 181 | public BubblesThread(SurfaceHolder holder, Context context) {
|
alision | 2cb9956 | 2013-05-30 17:02:20 -0400 | [diff] [blame] | 182 | surfaceHolder = holder;
|
| 183 | }
|
alision | fe9cf71 | 2013-05-03 17:26:08 -0400 | [diff] [blame] | 184 |
|
alision | 2cb9956 | 2013-05-30 17:02:20 -0400 | [diff] [blame] | 185 | public void setModel(BubbleModel model) {
|
| 186 | this.model = model;
|
| 187 | }
|
alision | fe9cf71 | 2013-05-03 17:26:08 -0400 | [diff] [blame] | 188 |
|
alision | 2cb9956 | 2013-05-30 17:02:20 -0400 | [diff] [blame] | 189 | @Override
|
| 190 | public void run() {
|
| 191 | while (running) {
|
| 192 | Canvas c = null;
|
| 193 | try {
|
Adrien Béraud | 7ed23dc | 2013-05-06 16:27:24 +1000 | [diff] [blame] | 194 |
|
alision | e38001f | 2013-06-04 14:14:39 -0400 | [diff] [blame] | 195 | if (suspendFlag) {
|
| 196 | synchronized (this) {
|
| 197 | while (suspendFlag) {
|
| 198 | try {
|
| 199 | wait();
|
| 200 | } catch (InterruptedException e) {
|
| 201 | // TODO Auto-generated catch block
|
| 202 | e.printStackTrace();
|
| 203 | }
|
| 204 | }
|
| 205 | }
|
| 206 | } else {
|
alision | e38001f | 2013-06-04 14:14:39 -0400 | [diff] [blame] | 207 | c = surfaceHolder.lockCanvas(null);
|
Adrien Béraud | 7ed23dc | 2013-05-06 16:27:24 +1000 | [diff] [blame] | 208 |
|
alision | e38001f | 2013-06-04 14:14:39 -0400 | [diff] [blame] | 209 | // for the case the surface is destroyed while already in the loop
|
| 210 | if (c == null || model == null)
|
| 211 | continue;
|
| 212 |
|
| 213 | synchronized (surfaceHolder) {
|
| 214 | // Log.w(TAG, "Thread doDraw");
|
| 215 | model.update();
|
| 216 | doDraw(c);
|
| 217 | }
|
alision | 2cb9956 | 2013-05-30 17:02:20 -0400 | [diff] [blame] | 218 | }
|
alision | e38001f | 2013-06-04 14:14:39 -0400 | [diff] [blame] | 219 |
|
alision | 2cb9956 | 2013-05-30 17:02:20 -0400 | [diff] [blame] | 220 | } finally {
|
| 221 | if (c != null)
|
| 222 | surfaceHolder.unlockCanvasAndPost(c);
|
| 223 | }
|
| 224 | }
|
| 225 | }
|
Adrien Béraud | 0446309 | 2013-05-06 14:17:22 +1000 | [diff] [blame] | 226 |
|
alision | e38001f | 2013-06-04 14:14:39 -0400 | [diff] [blame] | 227 | public void setPaused(boolean wantToPause) {
|
| 228 | synchronized (this) {
|
| 229 | suspendFlag = wantToPause;
|
| 230 | notify();
|
| 231 | }
|
| 232 | }
|
| 233 |
|
alision | 2cb9956 | 2013-05-30 17:02:20 -0400 | [diff] [blame] | 234 | public void setRunning(boolean b) {
|
| 235 | running = b;
|
| 236 | }
|
Adrien Béraud | 0446309 | 2013-05-06 14:17:22 +1000 | [diff] [blame] | 237 |
|
alision | 2cb9956 | 2013-05-30 17:02:20 -0400 | [diff] [blame] | 238 | public void setSurfaceSize(int width, int height) {
|
| 239 | synchronized (surfaceHolder) {
|
| 240 | if (model != null) {
|
| 241 | model.width = width;
|
| 242 | model.height = height;
|
| 243 | }
|
| 244 | }
|
| 245 | }
|
Adrien Béraud | 0446309 | 2013-05-06 14:17:22 +1000 | [diff] [blame] | 246 |
|
alision | 2cb9956 | 2013-05-30 17:02:20 -0400 | [diff] [blame] | 247 | /**
|
Alexandre Lision | 8ad3959 | 2013-11-21 13:23:58 -0500 | [diff] [blame] | 248 | * got multiple IndexOutOfBoundsException, when switching calls. //FIXME
|
alision | 2cb9956 | 2013-05-30 17:02:20 -0400 | [diff] [blame] | 249 | *
|
| 250 | * @param canvas
|
| 251 | */
|
| 252 | private void doDraw(Canvas canvas) {
|
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 | synchronized (model) {
|
| 255 | List<Bubble> bubbles = model.getBubbles();
|
| 256 | List<Attractor> attractors = model.getAttractors();
|
Alexandre Lision | 0edf18c | 2013-09-23 17:35:50 -0400 | [diff] [blame] | 257 |
|
| 258 | Paint tryMe = new Paint();
|
Alexandre Lision | 8ad3959 | 2013-11-21 13:23:58 -0500 | [diff] [blame] | 259 |
|
Alexandre Lision | 940a9db | 2013-11-19 17:12:02 -0500 | [diff] [blame] | 260 | Paint paint = new Paint();
|
| 261 | paint.setXfermode(new PorterDuffXfermode(Mode.CLEAR));
|
| 262 | canvas.drawPaint(paint);
|
| 263 | paint.setXfermode(new PorterDuffXfermode(Mode.SRC));
|
Alexandre Lision | 0edf18c | 2013-09-23 17:35:50 -0400 | [diff] [blame] | 264 |
|
Alexandre Lision | 8ad3959 | 2013-11-21 13:23:58 -0500 | [diff] [blame] | 265 | // canvas.drawColor(Color.LTGRAY);
|
Alexandre Lision | 0edf18c | 2013-09-23 17:35:50 -0400 | [diff] [blame] | 266 |
|
| 267 | if (dragging_bubble) {
|
Alexandre Lision | 0edf18c | 2013-09-23 17:35:50 -0400 | [diff] [blame] | 268 | Paint p = new Paint();
|
| 269 | p.setDither(true);
|
Alexandre Lision | 23628c1 | 2013-09-24 11:17:05 -0400 | [diff] [blame] | 270 | p.setColor(getResources().getColor(R.color.holo_red_light));
|
Alexandre Lision | 23628c1 | 2013-09-24 11:17:05 -0400 | [diff] [blame] | 271 | p.setStyle(Style.STROKE);
|
Alexandre Lision | 23628c1 | 2013-09-24 11:17:05 -0400 | [diff] [blame] | 272 | p.setStrokeWidth(20);
|
Alexandre Lision | 23628c1 | 2013-09-24 11:17:05 -0400 | [diff] [blame] | 273 | canvas.drawRect(new RectF(10, 10, model.width - 10, model.height - 10), p);
|
Alexandre Lision | 0edf18c | 2013-09-23 17:35:50 -0400 | [diff] [blame] | 274 | }
|
| 275 |
|
| 276 | tryMe.setStyle(Paint.Style.STROKE);
|
| 277 | tryMe.setColor(getResources().getColor(R.color.darker_gray));
|
| 278 | tryMe.setXfermode(null);
|
| 279 | canvas.drawCircle(model.width / 2, model.height / 2, model.width / 2 - getResources().getDimension(R.dimen.bubble_size), tryMe);
|
| 280 |
|
alision | 2cb9956 | 2013-05-30 17:02:20 -0400 | [diff] [blame] | 281 | try {
|
Adrien Béraud | 0c9bd8f | 2013-05-30 16:16:57 +1000 | [diff] [blame] | 282 |
|
alision | 2cb9956 | 2013-05-30 17:02:20 -0400 | [diff] [blame] | 283 | for (int i = 0, n = attractors.size(); i < n; i++) {
|
| 284 | Attractor a = attractors.get(i);
|
| 285 | canvas.drawBitmap(a.getBitmap(), null, a.getBounds(), null);
|
| 286 | }
|
Adrien Béraud | 3326888 | 2013-05-18 03:41:15 +1000 | [diff] [blame] | 287 |
|
alision | 2cb9956 | 2013-05-30 17:02:20 -0400 | [diff] [blame] | 288 | for (int i = 0, n = bubbles.size(); i < n; i++) {
|
| 289 | Bubble b = bubbles.get(i);
|
Alexandre Lision | 0edf18c | 2013-09-23 17:35:50 -0400 | [diff] [blame] | 290 | if (b.expanded) {
|
alision | 465ceba | 2013-07-04 09:24:30 -0400 | [diff] [blame] | 291 | continue;
|
| 292 | }
|
alision | 2cb9956 | 2013-05-30 17:02:20 -0400 | [diff] [blame] | 293 | canvas.drawBitmap(b.getBitmap(), null, b.getBounds(), null);
|
Alexandre Lision | 6885547 | 2013-10-10 16:20:46 -0400 | [diff] [blame] | 294 | canvas.drawText(b.getName(), b.getPosX(), (float) (b.getPosY() - b.getRetractedRadius() * 1.2 * density), getNamePaint(b));
|
alision | 34673e6 | 2013-06-25 14:40:07 -0400 | [diff] [blame] | 295 | }
|
Alexandre Lision | 0edf18c | 2013-09-23 17:35:50 -0400 | [diff] [blame] | 296 |
|
alision | 34673e6 | 2013-06-25 14:40:07 -0400 | [diff] [blame] | 297 | Bubble first_plan = getExpandedBubble();
|
| 298 | if (first_plan != null) {
|
Alexandre Lision | d588bff | 2013-10-08 12:43:01 -0400 | [diff] [blame] | 299 |
|
Alexandre Lision | 40954dc | 2013-10-09 15:24:03 -0400 | [diff] [blame] | 300 | if (first_plan.getDrawerBitmap() != null) {
|
| 301 | canvas.drawBitmap(first_plan.getDrawerBitmap(), null, first_plan.getDrawerBounds(), null);
|
Alexandre Lision | 6885547 | 2013-10-10 16:20:46 -0400 | [diff] [blame] | 302 | }
|
alision | 34673e6 | 2013-06-25 14:40:07 -0400 | [diff] [blame] | 303 | canvas.drawBitmap(first_plan.getBitmap(), null, first_plan.getBounds(), null);
|
Alexandre Lision | 40954dc | 2013-10-09 15:24:03 -0400 | [diff] [blame] | 304 | // canvas.drawText(first_plan.associated_call.getContact().getmDisplayName(), first_plan.getPosX(),
|
| 305 | // (float) (first_plan.getPosY() - first_plan.getRetractedRadius() * 1.2 * density), getNamePaint(first_plan));
|
alision | df1dac9 | 2013-06-27 17:35:53 -0400 | [diff] [blame] | 306 |
|
alision | 2cb9956 | 2013-05-30 17:02:20 -0400 | [diff] [blame] | 307 | }
|
| 308 |
|
| 309 | } catch (IndexOutOfBoundsException e) {
|
| 310 | Log.e(TAG, e.toString());
|
| 311 | }
|
| 312 | }
|
| 313 | }
|
alision | 34673e6 | 2013-06-25 14:40:07 -0400 | [diff] [blame] | 314 |
|
| 315 | }
|
| 316 |
|
| 317 | private Paint getNamePaint(Bubble b) {
|
alision | 50fa072 | 2013-06-25 17:29:44 -0400 | [diff] [blame] | 318 | if (b.expanded) {
|
| 319 | white_name_paint.setTextSize(15 * b.target_scale * textDensity);
|
alision | 34673e6 | 2013-06-25 14:40:07 -0400 | [diff] [blame] | 320 | return white_name_paint;
|
alision | 50fa072 | 2013-06-25 17:29:44 -0400 | [diff] [blame] | 321 | }
|
| 322 | black_name_paint.setTextSize(18 * b.target_scale * textDensity);
|
alision | 34673e6 | 2013-06-25 14:40:07 -0400 | [diff] [blame] | 323 | return black_name_paint;
|
alision | 2cb9956 | 2013-05-30 17:02:20 -0400 | [diff] [blame] | 324 | }
|
Adrien Béraud | 0446309 | 2013-05-06 14:17:22 +1000 | [diff] [blame] | 325 |
|
alision | e38001f | 2013-06-04 14:14:39 -0400 | [diff] [blame] | 326 | @Override
|
| 327 | public boolean onTouch(View v, MotionEvent event) {
|
Alexandre Lision | 6deda41 | 2013-09-25 13:21:22 -0400 | [diff] [blame] | 328 | // Log.w(TAG, "onTouch " + event.getAction());
|
alision | e38001f | 2013-06-04 14:14:39 -0400 | [diff] [blame] | 329 |
|
| 330 | int action = event.getActionMasked();
|
| 331 |
|
Alexandre Lision | 1888bf3 | 2013-10-31 09:51:41 -0400 | [diff] [blame] | 332 | if (gDetector.onTouchEvent(event))
|
| 333 | return true;
|
| 334 |
|
alision | e38001f | 2013-06-04 14:14:39 -0400 | [diff] [blame] | 335 | if (action == MotionEvent.ACTION_UP) {
|
| 336 | if (thread.suspendFlag) {
|
| 337 | Log.i(TAG, "Relaunch drawing thread");
|
| 338 | thread.setPaused(false);
|
| 339 | }
|
| 340 |
|
Alexandre Lision | 5cd659e | 2013-10-29 13:18:23 -0400 | [diff] [blame] | 341 | Bubble expand = getExpandedBubble();
|
| 342 | if (expand != null) {
|
Alexandre Lision | 5cd659e | 2013-10-29 13:18:23 -0400 | [diff] [blame] | 343 | switch (expand.getDrawer().getAction(event.getX(), event.getY())) {
|
| 344 | case Bubble.actions.OUT_OF_BOUNDS:
|
| 345 | expand.retract();
|
| 346 | break;
|
| 347 | case Bubble.actions.HOLD:
|
| 348 |
|
| 349 | try {
|
| 350 | if (expand.getHoldStatus()) {
|
| 351 |
|
| 352 | if (expand.isConference())
|
| 353 | callback.mCallbacks.getService().unholdConference(expand.getCallID());
|
| 354 | else
|
| 355 | callback.mCallbacks.getService().unhold(expand.getCallID());
|
| 356 | } else {
|
| 357 | if (expand.isConference())
|
| 358 | callback.mCallbacks.getService().holdConference(expand.getCallID());
|
| 359 | else
|
| 360 | callback.mCallbacks.getService().hold(expand.getCallID());
|
| 361 |
|
| 362 | }
|
| 363 | } catch (Exception e) {
|
| 364 | e.printStackTrace();
|
| 365 | }
|
| 366 |
|
| 367 | return true;
|
| 368 | case Bubble.actions.RECORD:
|
| 369 | try {
|
Alexandre Lision | 3874e55 | 2013-11-04 17:20:12 -0500 | [diff] [blame] | 370 | boolean isRecording = callback.mCallbacks.getService().toggleRecordingCall(expand.getCallID());
|
| 371 | ((BubbleUser) expand).associated_call.setRecording(isRecording);
|
Alexandre Lision | 5cd659e | 2013-10-29 13:18:23 -0400 | [diff] [blame] | 372 | } catch (RemoteException e1) {
|
| 373 | e1.printStackTrace();
|
| 374 | }
|
| 375 | return true;
|
| 376 | case Bubble.actions.MESSAGE:
|
| 377 | // TODO
|
| 378 | return true;
|
Alexandre Lision | d9c3f7f | 2013-10-30 15:20:55 -0400 | [diff] [blame] | 379 | case Bubble.actions.MUTE:
|
Alexandre Lision | c9fca9e | 2013-11-08 15:21:18 -0500 | [diff] [blame] | 380 | try {
|
| 381 | callback.mCallbacks.getService().setMuted(!((BubbleUser) expand).getMute());
|
| 382 | ((BubbleUser) expand).toggleMute();
|
| 383 | } catch (RemoteException e1) {
|
| 384 | e1.printStackTrace();
|
| 385 | }
|
Alexandre Lision | d9c3f7f | 2013-10-30 15:20:55 -0400 | [diff] [blame] | 386 | return true;
|
Alexandre Lision | 5cd659e | 2013-10-29 13:18:23 -0400 | [diff] [blame] | 387 | case Bubble.actions.HANGUP:
|
| 388 | try {
|
| 389 | if (expand.isConference())
|
| 390 | callback.mCallbacks.getService().hangUpConference(expand.getCallID());
|
| 391 | else
|
| 392 | callback.mCallbacks.getService().hangUp(expand.getCallID());
|
| 393 | } catch (RemoteException e) {
|
| 394 | e.printStackTrace();
|
| 395 | }
|
| 396 | return true;
|
| 397 |
|
| 398 | case Bubble.actions.TRANSFER:
|
| 399 | callback.makeTransfer((BubbleContact) expand);
|
| 400 | return true;
|
| 401 | case Bubble.actions.NOTHING:
|
| 402 | break;
|
| 403 | }
|
Alexandre Lision | 5cd659e | 2013-10-29 13:18:23 -0400 | [diff] [blame] | 404 |
|
Alexandre Lision | d9c3f7f | 2013-10-30 15:20:55 -0400 | [diff] [blame] | 405 | }
|
Alexandre Lision | 5cd659e | 2013-10-29 13:18:23 -0400 | [diff] [blame] | 406 |
|
alision | e38001f | 2013-06-04 14:14:39 -0400 | [diff] [blame] | 407 | List<Bubble> bubbles = model.getBubbles();
|
| 408 | final int n_bubbles = bubbles.size();
|
| 409 | for (int i = 0; i < n_bubbles; i++) {
|
| 410 | Bubble b = bubbles.get(i);
|
| 411 | if (b.dragged) {
|
| 412 | b.dragged = false;
|
| 413 | b.target_scale = 1.f;
|
Alexandre Lision | fdef885 | 2013-09-24 13:32:38 -0400 | [diff] [blame] | 414 | if (b.isOnBorder(model.width, model.height) && !b.expanded) {
|
Alexandre Lision | 0edf18c | 2013-09-23 17:35:50 -0400 | [diff] [blame] | 415 | b.markedToDie = true;
|
Alexandre Lision | 6885547 | 2013-10-10 16:20:46 -0400 | [diff] [blame] | 416 |
|
| 417 | try {
|
| 418 | if (b.isConference())
|
| 419 | callback.mCallbacks.getService().hangUpConference(b.getCallID());
|
| 420 | else
|
| 421 | callback.mCallbacks.getService().hangUp(b.getCallID());
|
| 422 |
|
| 423 | } catch (RemoteException e) {
|
| 424 | e.printStackTrace();
|
| 425 | }
|
Alexandre Lision | 0edf18c | 2013-09-23 17:35:50 -0400 | [diff] [blame] | 426 | }
|
alision | e38001f | 2013-06-04 14:14:39 -0400 | [diff] [blame] | 427 | }
|
| 428 | }
|
| 429 | dragging_bubble = false;
|
| 430 | } else if (action != MotionEvent.ACTION_DOWN && !isDraggingBubble() && !thread.suspendFlag) {
|
| 431 |
|
| 432 | Log.i(TAG, "Not dragging thread should be stopped");
|
| 433 | thread.setPaused(true);
|
| 434 | // thread.holdDrawing();
|
| 435 | }
|
| 436 |
|
Alexandre Lision | 1888bf3 | 2013-10-31 09:51:41 -0400 | [diff] [blame] | 437 | return true;
|
alision | e38001f | 2013-06-04 14:14:39 -0400 | [diff] [blame] | 438 | }
|
| 439 |
|
alision | 34673e6 | 2013-06-25 14:40:07 -0400 | [diff] [blame] | 440 | private Bubble getExpandedBubble() {
|
| 441 | List<Bubble> bubbles = model.getBubbles();
|
| 442 | final int n_bubbles = bubbles.size();
|
| 443 | for (int i = 0; i < n_bubbles; i++) {
|
| 444 | Bubble b = bubbles.get(i);
|
| 445 | if (b.expanded) {
|
| 446 | return b;
|
| 447 | }
|
| 448 | }
|
| 449 | return null;
|
| 450 | }
|
alision | 2ec64f9 | 2013-06-17 17:28:58 -0400 | [diff] [blame] | 451 |
|
alision | df1dac9 | 2013-06-27 17:35:53 -0400 | [diff] [blame] | 452 | public void restartDrawing() {
|
| 453 | if (thread != null && thread.suspendFlag) {
|
| 454 | Log.i(TAG, "Relaunch drawing thread");
|
| 455 | thread.setPaused(false);
|
| 456 | }
|
| 457 | }
|
| 458 |
|
| 459 | public void setFragment(CallFragment callFragment) {
|
| 460 | callback = callFragment;
|
| 461 |
|
| 462 | }
|
| 463 |
|
| 464 | public void stopThread() {
|
| 465 | if (thread != null && thread.suspendFlag) {
|
| 466 | Log.i(TAG, "Stop drawing thread");
|
Alexandre Lision | 666b377 | 2013-10-28 17:42:48 -0400 | [diff] [blame] | 467 | thread.setPaused(true);
|
alision | df1dac9 | 2013-06-27 17:35:53 -0400 | [diff] [blame] | 468 | }
|
| 469 |
|
| 470 | }
|
| 471 |
|
alision | 58356b7 | 2013-06-03 17:13:36 -0400 | [diff] [blame] | 472 | class MyOnGestureListener implements OnGestureListener {
|
| 473 | @Override
|
| 474 | public boolean onDown(MotionEvent event) {
|
| 475 | List<Bubble> bubbles = model.getBubbles();
|
Alexandre Lision | 1888bf3 | 2013-10-31 09:51:41 -0400 | [diff] [blame] | 476 |
|
Alexandre Lision | 8fdcf2b | 2013-10-30 16:06:51 -0400 | [diff] [blame] | 477 | Bubble target = getExpandedBubble();
|
Alexandre Lision | 1888bf3 | 2013-10-31 09:51:41 -0400 | [diff] [blame] | 478 | if (target != null) {
|
Alexandre Lision | 8fdcf2b | 2013-10-30 16:06:51 -0400 | [diff] [blame] | 479 | target.onDown(event);
|
| 480 | return true;
|
| 481 | }
|
Alexandre Lision | 6885547 | 2013-10-10 16:20:46 -0400 | [diff] [blame] | 482 |
|
Alexandre Lision | d9c3f7f | 2013-10-30 15:20:55 -0400 | [diff] [blame] | 483 | for (int i = 0; i < bubbles.size(); i++) {
|
alision | 58356b7 | 2013-06-03 17:13:36 -0400 | [diff] [blame] | 484 | Bubble b = bubbles.get(i);
|
Alexandre Lision | 1888bf3 | 2013-10-31 09:51:41 -0400 | [diff] [blame] | 485 | if (b.onDown(event))
|
| 486 | dragging_bubble = true;
|
alision | 58356b7 | 2013-06-03 17:13:36 -0400 | [diff] [blame] | 487 | }
|
Alexandre Lision | d9c3f7f | 2013-10-30 15:20:55 -0400 | [diff] [blame] | 488 |
|
alision | 58356b7 | 2013-06-03 17:13:36 -0400 | [diff] [blame] | 489 | return true;
|
| 490 | }
|
alision | 2ec64f9 | 2013-06-17 17:28:58 -0400 | [diff] [blame] | 491 |
|
alision | 58356b7 | 2013-06-03 17:13:36 -0400 | [diff] [blame] | 492 | @Override
|
| 493 | public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
|
Alexandre Lision | 6deda41 | 2013-09-25 13:21:22 -0400 | [diff] [blame] | 494 | // Log.d("Main", "onFling");
|
Alexandre Lision | 1888bf3 | 2013-10-31 09:51:41 -0400 | [diff] [blame] | 495 | return false;
|
alision | 58356b7 | 2013-06-03 17:13:36 -0400 | [diff] [blame] | 496 | }
|
| 497 |
|
| 498 | @Override
|
| 499 | public void onLongPress(MotionEvent e) {
|
Alexandre Lision | 6deda41 | 2013-09-25 13:21:22 -0400 | [diff] [blame] | 500 | // Log.d("Main", "onLongPress");
|
Alexandre Lision | 1888bf3 | 2013-10-31 09:51:41 -0400 | [diff] [blame] | 501 |
|
alision | 2ec64f9 | 2013-06-17 17:28:58 -0400 | [diff] [blame] | 502 | }
|
| 503 |
|
alision | 2ec64f9 | 2013-06-17 17:28:58 -0400 | [diff] [blame] | 504 | private Bubble getDraggedBubble(MotionEvent e) {
|
| 505 | List<Bubble> bubbles = model.getBubbles();
|
| 506 | final int n_bubbles = bubbles.size();
|
| 507 | for (int i = 0; i < n_bubbles; i++) {
|
| 508 | Bubble b = bubbles.get(i);
|
| 509 | if (b.intersects(e.getX(), e.getY())) {
|
| 510 | return b;
|
| 511 | }
|
| 512 | }
|
| 513 | return null;
|
alision | 58356b7 | 2013-06-03 17:13:36 -0400 | [diff] [blame] | 514 | }
|
| 515 |
|
| 516 | @Override
|
| 517 | public boolean onScroll(MotionEvent e1, MotionEvent event, float distanceX, float distanceY) {
|
Alexandre Lision | 6deda41 | 2013-09-25 13:21:22 -0400 | [diff] [blame] | 518 | // Log.d("Main", "onScroll");
|
alision | 58356b7 | 2013-06-03 17:13:36 -0400 | [diff] [blame] | 519 | List<Bubble> bubbles = model.getBubbles();
|
Alexandre Lision | d9c3f7f | 2013-10-30 15:20:55 -0400 | [diff] [blame] | 520 |
|
alision | 58356b7 | 2013-06-03 17:13:36 -0400 | [diff] [blame] | 521 | long now = System.nanoTime();
|
Alexandre Lision | d9c3f7f | 2013-10-30 15:20:55 -0400 | [diff] [blame] | 522 | for (int i = 0; i < bubbles.size(); i++) {
|
alision | 58356b7 | 2013-06-03 17:13:36 -0400 | [diff] [blame] | 523 | Bubble b = bubbles.get(i);
|
| 524 | if (b.dragged) {
|
| 525 | float x = event.getX(), y = event.getY();
|
| 526 | float dt = (float) ((now - b.last_drag) / 1000000000.);
|
| 527 | float dx = x - b.getPosX(), dy = y - b.getPosY();
|
| 528 | b.last_drag = now;
|
alision | 58356b7 | 2013-06-03 17:13:36 -0400 | [diff] [blame] | 529 | b.setPos(event.getX(), event.getY());
|
alision | 58356b7 | 2013-06-03 17:13:36 -0400 | [diff] [blame] | 530 | b.speed.x = dx / dt;
|
| 531 | b.speed.y = dy / dt;
|
Alexandre Lision | d9c3f7f | 2013-10-30 15:20:55 -0400 | [diff] [blame] | 532 |
|
Alexandre Lision | 1888bf3 | 2013-10-31 09:51:41 -0400 | [diff] [blame] | 533 | return false;
|
alision | 58356b7 | 2013-06-03 17:13:36 -0400 | [diff] [blame] | 534 | }
|
| 535 | }
|
| 536 | return true;
|
| 537 | }
|
| 538 |
|
| 539 | @Override
|
| 540 | public void onShowPress(MotionEvent e) {
|
Alexandre Lision | 6deda41 | 2013-09-25 13:21:22 -0400 | [diff] [blame] | 541 | // Log.d("Main", "onShowPress");
|
alision | 58356b7 | 2013-06-03 17:13:36 -0400 | [diff] [blame] | 542 |
|
| 543 | }
|
| 544 |
|
| 545 | @Override
|
| 546 | public boolean onSingleTapUp(MotionEvent e) {
|
Alexandre Lision | 1888bf3 | 2013-10-31 09:51:41 -0400 | [diff] [blame] | 547 | if (isDraggingBubble() && callback.getConference().isOnGoing()) {
|
| 548 | Bubble b = getDraggedBubble(e);
|
| 549 | b.expand(model.width, model.height);
|
Alexandre Lision | 1a68257 | 2013-11-01 11:21:17 -0400 | [diff] [blame] | 550 | dragging_bubble = false;
|
Alexandre Lision | 1888bf3 | 2013-10-31 09:51:41 -0400 | [diff] [blame] | 551 | }
|
| 552 | return false;
|
| 553 |
|
alision | 58356b7 | 2013-06-03 17:13:36 -0400 | [diff] [blame] | 554 | }
|
| 555 | }
|
alision | fe9cf71 | 2013-05-03 17:26:08 -0400 | [diff] [blame] | 556 | }
|