Adrien Béraud | 0446309 | 2013-05-06 14:17:22 +1000 | [diff] [blame] | 1 | package com.savoirfairelinux.sflphone.model;
|
alision | fe9cf71 | 2013-05-03 17:26:08 -0400 | [diff] [blame] | 2 |
|
| 3 | import android.content.Context;
|
| 4 | import android.graphics.Canvas;
|
| 5 | import android.graphics.Color;
|
Adrien Béraud | 0446309 | 2013-05-06 14:17:22 +1000 | [diff] [blame] | 6 | import android.os.Handler;
|
| 7 | import android.os.Message;
|
alision | fe9cf71 | 2013-05-03 17:26:08 -0400 | [diff] [blame] | 8 | import android.util.AttributeSet;
|
| 9 | import android.util.Log;
|
| 10 | import android.view.MotionEvent;
|
Adrien Béraud | 0446309 | 2013-05-06 14:17:22 +1000 | [diff] [blame] | 11 | import android.view.SurfaceHolder;
|
| 12 | import android.view.SurfaceView;
|
alision | fe9cf71 | 2013-05-03 17:26:08 -0400 | [diff] [blame] | 13 | import android.view.View;
|
Adrien Béraud | 0446309 | 2013-05-06 14:17:22 +1000 | [diff] [blame] | 14 | import android.view.View.OnTouchListener;
|
alision | fe9cf71 | 2013-05-03 17:26:08 -0400 | [diff] [blame] | 15 |
|
Adrien Béraud | 0446309 | 2013-05-06 14:17:22 +1000 | [diff] [blame] | 16 | public class BubblesView extends SurfaceView implements SurfaceHolder.Callback, OnTouchListener
|
| 17 | {
|
| 18 | private static final String TAG = BubblesView.class.getSimpleName();
|
alision | fe9cf71 | 2013-05-03 17:26:08 -0400 | [diff] [blame] | 19 |
|
Adrien Béraud | 0446309 | 2013-05-06 14:17:22 +1000 | [diff] [blame] | 20 | private BubblesThread thread = null;
|
| 21 | private BubbleModel model;
|
alision | fe9cf71 | 2013-05-03 17:26:08 -0400 | [diff] [blame] | 22 |
|
Adrien Béraud | 0446309 | 2013-05-06 14:17:22 +1000 | [diff] [blame] | 23 | public BubblesView(Context context, AttributeSet attrs)
|
| 24 | {
|
| 25 | super(context, attrs);
|
alision | fe9cf71 | 2013-05-03 17:26:08 -0400 | [diff] [blame] | 26 |
|
Adrien Béraud | 0446309 | 2013-05-06 14:17:22 +1000 | [diff] [blame] | 27 | SurfaceHolder holder = getHolder();
|
| 28 | holder.addCallback(this);
|
alision | fe9cf71 | 2013-05-03 17:26:08 -0400 | [diff] [blame] | 29 |
|
Adrien Béraud | 0446309 | 2013-05-06 14:17:22 +1000 | [diff] [blame] | 30 | // create thread only; it's started in surfaceCreated()
|
| 31 | createThread();
|
alision | fe9cf71 | 2013-05-03 17:26:08 -0400 | [diff] [blame] | 32 |
|
Adrien Béraud | 0446309 | 2013-05-06 14:17:22 +1000 | [diff] [blame] | 33 | setOnTouchListener(this);
|
| 34 | setFocusable(true);
|
| 35 | }
|
alision | fe9cf71 | 2013-05-03 17:26:08 -0400 | [diff] [blame] | 36 |
|
Adrien Béraud | 0446309 | 2013-05-06 14:17:22 +1000 | [diff] [blame] | 37 | private void createThread() {
|
| 38 | if(thread != null) return;
|
| 39 | thread = new BubblesThread(getHolder(), getContext(), new Handler() {
|
| 40 | @Override
|
| 41 | public void handleMessage(Message m)
|
| 42 | {
|
| 43 | /* mStatusText.setVisibility(m.getData().getInt("viz"));
|
| 44 | mStatusText.setText(m.getData().getString("text"));*/
|
| 45 | }
|
| 46 | });
|
Adrien Béraud | 65ba5b1 | 2013-05-06 15:42:50 +1000 | [diff] [blame] | 47 | if(model != null)
|
| 48 | thread.setModel(model);
|
Adrien Béraud | 0446309 | 2013-05-06 14:17:22 +1000 | [diff] [blame] | 49 | }
|
alision | fe9cf71 | 2013-05-03 17:26:08 -0400 | [diff] [blame] | 50 |
|
Adrien Béraud | 0446309 | 2013-05-06 14:17:22 +1000 | [diff] [blame] | 51 | public void setModel(BubbleModel model)
|
| 52 | {
|
| 53 | this.model = model;
|
| 54 | thread.setModel(model);
|
| 55 | }
|
alision | fe9cf71 | 2013-05-03 17:26:08 -0400 | [diff] [blame] | 56 |
|
Adrien Béraud | 0446309 | 2013-05-06 14:17:22 +1000 | [diff] [blame] | 57 | /*@Override
|
| 58 | public void onWindowFocusChanged(boolean hasWindowFocus) {
|
| 59 | if (!hasWindowFocus) {
|
| 60 | thread.pause();
|
| 61 | }
|
| 62 | }*/
|
alision | fe9cf71 | 2013-05-03 17:26:08 -0400 | [diff] [blame] | 63 |
|
Adrien Béraud | 0446309 | 2013-05-06 14:17:22 +1000 | [diff] [blame] | 64 | @Override
|
| 65 | public void surfaceChanged(SurfaceHolder holder, int format, int width, int height)
|
| 66 | {
|
| 67 | Log.w(TAG, "surfaceChanged");
|
| 68 | thread.setSurfaceSize(width, height);
|
| 69 | }
|
alision | fe9cf71 | 2013-05-03 17:26:08 -0400 | [diff] [blame] | 70 |
|
Adrien Béraud | 0446309 | 2013-05-06 14:17:22 +1000 | [diff] [blame] | 71 | /*
|
| 72 | * Callback invoked when the Surface has been created and is ready to be
|
| 73 | * used.
|
| 74 | */
|
| 75 | @Override
|
| 76 | public void surfaceCreated(SurfaceHolder holder)
|
| 77 | {
|
| 78 | // start the thread here so that we don't busy-wait in run()
|
| 79 | // waiting for the surface to be created
|
| 80 | createThread();
|
alision | fe9cf71 | 2013-05-03 17:26:08 -0400 | [diff] [blame] | 81 |
|
Adrien Béraud | 0446309 | 2013-05-06 14:17:22 +1000 | [diff] [blame] | 82 | Log.w(TAG, "surfaceCreated");
|
| 83 | thread.setRunning(true);
|
| 84 | thread.start();
|
| 85 | }
|
alision | fe9cf71 | 2013-05-03 17:26:08 -0400 | [diff] [blame] | 86 |
|
Adrien Béraud | 0446309 | 2013-05-06 14:17:22 +1000 | [diff] [blame] | 87 | /*
|
| 88 | * Callback invoked when the Surface has been destroyed and must no longer
|
| 89 | * be touched. WARNING: after this method returns, the Surface/Canvas must
|
| 90 | * never be touched again!
|
| 91 | */
|
| 92 | @Override
|
| 93 | public void surfaceDestroyed(SurfaceHolder holder)
|
| 94 | {
|
| 95 | // we have to tell thread to shut down & wait for it to finish, or else
|
| 96 | // it might touch the Surface after we return and explode
|
| 97 | Log.w(TAG, "surfaceDestroyed");
|
| 98 | boolean retry = true;
|
| 99 | thread.setRunning(false);
|
Adrien Béraud | 25fc409 | 2013-05-06 15:28:39 +1000 | [diff] [blame] | 100 | while (retry) {
|
Adrien Béraud | 0446309 | 2013-05-06 14:17:22 +1000 | [diff] [blame] | 101 | try {
|
| 102 | thread.join();
|
| 103 | retry = false;
|
| 104 | } catch (InterruptedException e) {
|
| 105 | }
|
Adrien Béraud | 25fc409 | 2013-05-06 15:28:39 +1000 | [diff] [blame] | 106 | }
|
Adrien Béraud | 0446309 | 2013-05-06 14:17:22 +1000 | [diff] [blame] | 107 | thread = null;
|
| 108 | }
|
alision | fe9cf71 | 2013-05-03 17:26:08 -0400 | [diff] [blame] | 109 |
|
Adrien Béraud | 0446309 | 2013-05-06 14:17:22 +1000 | [diff] [blame] | 110 | @Override
|
| 111 | public boolean onTouch(View v, MotionEvent event)
|
| 112 | {
|
| 113 | Log.w(TAG, "onTouch " + event.getAction());
|
Adrien Béraud | 25fc409 | 2013-05-06 15:28:39 +1000 | [diff] [blame] | 114 |
|
| 115 | int action = event.getActionMasked();
|
| 116 |
|
| 117 | if(action == MotionEvent.ACTION_DOWN) {
|
Adrien Béraud | 0446309 | 2013-05-06 14:17:22 +1000 | [diff] [blame] | 118 | for (Bubble b : model.listBubbles) {
|
Adrien Béraud | 25fc409 | 2013-05-06 15:28:39 +1000 | [diff] [blame] | 119 | if(b.intersects(event.getX(), event.getY())) {
|
| 120 | b.dragged = true;
|
| 121 | }
|
| 122 | }
|
| 123 | } else if (action == MotionEvent.ACTION_MOVE) {
|
| 124 | for (Bubble b : model.listBubbles) {
|
| 125 | if(b.dragged) {
|
| 126 | b.setPos(event.getX(), event.getY());
|
Adrien Béraud | 0446309 | 2013-05-06 14:17:22 +1000 | [diff] [blame] | 127 | return true;
|
| 128 | }
|
| 129 | }
|
Adrien Béraud | 25fc409 | 2013-05-06 15:28:39 +1000 | [diff] [blame] | 130 | } else if(action == MotionEvent.ACTION_UP || action == MotionEvent.ACTION_CANCEL) {
|
| 131 | for (Bubble b : model.listBubbles) {
|
| 132 | if(b.dragged) {
|
| 133 | b.dragged = false;
|
| 134 | }
|
| 135 | }
|
| 136 | }
|
Adrien Béraud | 0446309 | 2013-05-06 14:17:22 +1000 | [diff] [blame] | 137 | return true;
|
| 138 | }
|
alision | fe9cf71 | 2013-05-03 17:26:08 -0400 | [diff] [blame] | 139 |
|
Adrien Béraud | 0446309 | 2013-05-06 14:17:22 +1000 | [diff] [blame] | 140 | class BubblesThread extends Thread
|
| 141 | {
|
| 142 | private boolean running = false;
|
| 143 | private SurfaceHolder surfaceHolder;
|
alision | fe9cf71 | 2013-05-03 17:26:08 -0400 | [diff] [blame] | 144 |
|
Adrien Béraud | 0446309 | 2013-05-06 14:17:22 +1000 | [diff] [blame] | 145 | BubbleModel model = null;
|
alision | fe9cf71 | 2013-05-03 17:26:08 -0400 | [diff] [blame] | 146 |
|
Adrien Béraud | 0446309 | 2013-05-06 14:17:22 +1000 | [diff] [blame] | 147 | public BubblesThread(SurfaceHolder holder, Context context, Handler handler)
|
| 148 | {
|
| 149 | surfaceHolder = holder;
|
| 150 | }
|
alision | fe9cf71 | 2013-05-03 17:26:08 -0400 | [diff] [blame] | 151 |
|
Adrien Béraud | 0446309 | 2013-05-06 14:17:22 +1000 | [diff] [blame] | 152 | public void setModel(BubbleModel model)
|
| 153 | {
|
| 154 | this.model = model;
|
| 155 | }
|
alision | fe9cf71 | 2013-05-03 17:26:08 -0400 | [diff] [blame] | 156 |
|
Adrien Béraud | 0446309 | 2013-05-06 14:17:22 +1000 | [diff] [blame] | 157 | @Override
|
| 158 | public void run()
|
| 159 | {
|
| 160 | while (running) {
|
| 161 | Canvas c = null;
|
| 162 | try {
|
| 163 | c = surfaceHolder.lockCanvas(null);
|
Adrien Béraud | 7ed23dc | 2013-05-06 16:27:24 +1000 | [diff] [blame] | 164 |
|
| 165 | // for the case the surface is destroyed while already in the loop
|
| 166 | if(c == null || model == null) continue;
|
| 167 |
|
Adrien Béraud | 0446309 | 2013-05-06 14:17:22 +1000 | [diff] [blame] | 168 | synchronized (surfaceHolder) {
|
Adrien Béraud | 25fc409 | 2013-05-06 15:28:39 +1000 | [diff] [blame] | 169 | //Log.w(TAG, "Thread doDraw");
|
| 170 | model.update();
|
Adrien Béraud | 0446309 | 2013-05-06 14:17:22 +1000 | [diff] [blame] | 171 | doDraw(c);
|
| 172 | }
|
| 173 | } finally {
|
| 174 | // do this in a finally so that if an exception is thrown
|
| 175 | // during the above, we don't leave the Surface in an
|
| 176 | // inconsistent state
|
| 177 | if (c != null)
|
| 178 | surfaceHolder.unlockCanvasAndPost(c);
|
| 179 | }
|
| 180 | }
|
| 181 | }
|
| 182 |
|
| 183 | public void setRunning(boolean b)
|
| 184 | {
|
| 185 | running = b;
|
| 186 | }
|
| 187 |
|
| 188 | public void setSurfaceSize(int width, int height)
|
| 189 | {
|
| 190 | synchronized (surfaceHolder) {
|
Adrien Béraud | 25fc409 | 2013-05-06 15:28:39 +1000 | [diff] [blame] | 191 | if(model != null) {
|
| 192 | model.width = width;
|
| 193 | model.height = height;
|
| 194 | }
|
Adrien Béraud | 0446309 | 2013-05-06 14:17:22 +1000 | [diff] [blame] | 195 |
|
| 196 | // don't forget to resize the background image
|
| 197 | // mBackgroundImage = Bitmap.createScaledBitmap(mBackgroundImage, width, height, true);
|
| 198 | }
|
| 199 | }
|
| 200 |
|
Adrien Béraud | 0446309 | 2013-05-06 14:17:22 +1000 | [diff] [blame] | 201 | private void doDraw(Canvas canvas)
|
| 202 | {
|
| 203 | canvas.drawColor(Color.WHITE);
|
| 204 |
|
alision | e124ff4 | 2013-05-09 10:48:05 -0400 | [diff] [blame] | 205 | for(int i=0 ; i<model.listBubbles.size(); i++) {
|
| 206 | synchronized (model) {
|
| 207 | Bubble b = model.listBubbles.get(i);
|
| 208 | canvas.drawBitmap(b.getBitmap(), null, b.getBounds(), null);
|
| 209 | }
|
Adrien Béraud | 25fc409 | 2013-05-06 15:28:39 +1000 | [diff] [blame] | 210 | }
|
Adrien Béraud | 0446309 | 2013-05-06 14:17:22 +1000 | [diff] [blame] | 211 | }
|
| 212 | }
|
| 213 |
|
alision | fe9cf71 | 2013-05-03 17:26:08 -0400 | [diff] [blame] | 214 | }
|