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