Alexandre Lision | 064e1e0 | 2013-10-01 16:18:42 -0400 | [diff] [blame] | 1 | package org.sflphone.model; |
| 2 | |
Alexandre Lision | d588bff | 2013-10-08 12:43:01 -0400 | [diff] [blame] | 3 | import org.sflphone.R; |
Alexandre Lision | 064e1e0 | 2013-10-01 16:18:42 -0400 | [diff] [blame] | 4 | import org.sflphone.adapters.ContactPictureTask; |
alision | fe9cf71 | 2013-05-03 17:26:08 -0400 | [diff] [blame] | 5 | |
| 6 | import android.content.Context; |
| 7 | import android.graphics.Bitmap; |
Alexandre Lision | d588bff | 2013-10-08 12:43:01 -0400 | [diff] [blame] | 8 | import android.graphics.BitmapShader; |
alision | fe9cf71 | 2013-05-03 17:26:08 -0400 | [diff] [blame] | 9 | import android.graphics.Canvas; |
alision | 2ec64f9 | 2013-06-17 17:28:58 -0400 | [diff] [blame] | 10 | import android.graphics.Color; |
alision | fe9cf71 | 2013-05-03 17:26:08 -0400 | [diff] [blame] | 11 | import android.graphics.Paint; |
Alexandre Lision | d588bff | 2013-10-08 12:43:01 -0400 | [diff] [blame] | 12 | import android.graphics.Paint.Style; |
Adrien Béraud | 25fc409 | 2013-05-06 15:28:39 +1000 | [diff] [blame] | 13 | import android.graphics.PointF; |
alision | fe9cf71 | 2013-05-03 17:26:08 -0400 | [diff] [blame] | 14 | import android.graphics.RectF; |
Alexandre Lision | d588bff | 2013-10-08 12:43:01 -0400 | [diff] [blame] | 15 | import android.graphics.Shader; |
alision | fe9cf71 | 2013-05-03 17:26:08 -0400 | [diff] [blame] | 16 | |
Alexandre Lision | 40954dc | 2013-10-09 15:24:03 -0400 | [diff] [blame] | 17 | public abstract class Bubble { |
alision | 806e18e | 2013-06-21 15:30:17 -0400 | [diff] [blame] | 18 | |
alision | 2ec64f9 | 2013-06-17 17:28:58 -0400 | [diff] [blame] | 19 | // A Bitmap object that is going to be passed to the BitmapShader |
Alexandre Lision | 40954dc | 2013-10-09 15:24:03 -0400 | [diff] [blame] | 20 | protected Bitmap externalBMP; |
alision | fe9cf71 | 2013-05-03 17:26:08 -0400 | [diff] [blame] | 21 | |
Alexandre Lision | 40954dc | 2013-10-09 15:24:03 -0400 | [diff] [blame] | 22 | protected PointF pos = new PointF(); |
| 23 | protected RectF bounds; |
alision | 2ec64f9 | 2013-06-17 17:28:58 -0400 | [diff] [blame] | 24 | public float target_scale = 1.f; |
Alexandre Lision | 40954dc | 2013-10-09 15:24:03 -0400 | [diff] [blame] | 25 | protected float radius; |
| 26 | protected float scale = 1.f; |
| 27 | protected float density = 1.f; |
alision | 2ec64f9 | 2013-06-17 17:28:58 -0400 | [diff] [blame] | 28 | public PointF speed = new PointF(0, 0); |
| 29 | public PointF last_speed = new PointF(); |
| 30 | public PointF attractor = null; |
Alexandre Lision | 40954dc | 2013-10-09 15:24:03 -0400 | [diff] [blame] | 31 | ActionDrawer act; |
Alexandre Lision | 40954dc | 2013-10-09 15:24:03 -0400 | [diff] [blame] | 32 | |
Alexandre Lision | 6885547 | 2013-10-10 16:20:46 -0400 | [diff] [blame] | 33 | public boolean isUser; |
alision | fe9cf71 | 2013-05-03 17:26:08 -0400 | [diff] [blame] | 34 | |
alision | 2ec64f9 | 2013-06-17 17:28:58 -0400 | [diff] [blame] | 35 | public boolean dragged = false; |
Alexandre Lision | 6deda41 | 2013-09-25 13:21:22 -0400 | [diff] [blame] | 36 | |
Alexandre Lision | 0edf18c | 2013-09-23 17:35:50 -0400 | [diff] [blame] | 37 | public boolean markedToDie = false; |
alision | 2ec64f9 | 2013-06-17 17:28:58 -0400 | [diff] [blame] | 38 | public long last_drag; |
| 39 | public boolean expanded; // determine if we draw the buttons around the bubble |
Alexandre Lision | 40954dc | 2013-10-09 15:24:03 -0400 | [diff] [blame] | 40 | protected Bitmap saved_photo; |
alision | 4a0eb09 | 2013-05-07 13:52:03 -0400 | [diff] [blame] | 41 | |
Alexandre Lision | 40954dc | 2013-10-09 15:24:03 -0400 | [diff] [blame] | 42 | public interface actions { |
| 43 | int NOTHING = 0; |
| 44 | int HOLD = 1; |
| 45 | int RECORD = 2; |
| 46 | int HANGUP = 3; |
| 47 | int MESSAGE = 4; |
| 48 | int TRANSFER = 5; |
| 49 | int MUTE = 6; |
| 50 | } |
| 51 | |
| 52 | protected Context mContext; |
Alexandre Lision | d588bff | 2013-10-08 12:43:01 -0400 | [diff] [blame] | 53 | |
alision | 2ec64f9 | 2013-06-17 17:28:58 -0400 | [diff] [blame] | 54 | public void setAttractor(PointF attractor) { |
| 55 | this.attractor = attractor; |
| 56 | } |
Adrien Béraud | e0ef0c2 | 2013-05-18 01:56:27 +1000 | [diff] [blame] | 57 | |
Alexandre Lision | 40954dc | 2013-10-09 15:24:03 -0400 | [diff] [blame] | 58 | public Bubble(Context context, CallContact contact, float x, float y, float size) { |
| 59 | mContext = context; |
alision | 2ec64f9 | 2013-06-17 17:28:58 -0400 | [diff] [blame] | 60 | pos.set(x, y); |
Alexandre Lision | 6885547 | 2013-10-10 16:20:46 -0400 | [diff] [blame] | 61 | radius = size / 2; // 10 is the white stroke |
Alexandre Lision | 40954dc | 2013-10-09 15:24:03 -0400 | [diff] [blame] | 62 | saved_photo = getContactPhoto(context, contact, (int) size); |
| 63 | generateBitmap(); |
| 64 | attractor = new PointF(x, y); |
| 65 | } |
Alexandre Lision | d588bff | 2013-10-08 12:43:01 -0400 | [diff] [blame] | 66 | |
Alexandre Lision | 40954dc | 2013-10-09 15:24:03 -0400 | [diff] [blame] | 67 | protected void generateBitmap() { |
Alexandre Lision | 6885547 | 2013-10-10 16:20:46 -0400 | [diff] [blame] | 68 | |
Alexandre Lision | d588bff | 2013-10-08 12:43:01 -0400 | [diff] [blame] | 69 | int w = saved_photo.getWidth(), h = saved_photo.getHeight(); |
| 70 | if (w > h) { |
| 71 | w = h; |
| 72 | } else if (h > w) { |
| 73 | h = w; |
| 74 | } |
Alexandre Lision | 6885547 | 2013-10-10 16:20:46 -0400 | [diff] [blame] | 75 | externalBMP = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888); |
Alexandre Lision | d588bff | 2013-10-08 12:43:01 -0400 | [diff] [blame] | 76 | BitmapShader shader; |
| 77 | shader = new BitmapShader(saved_photo, Shader.TileMode.CLAMP, Shader.TileMode.CLAMP); |
| 78 | |
| 79 | Paint paint = new Paint(); |
Alexandre Lision | 6885547 | 2013-10-10 16:20:46 -0400 | [diff] [blame] | 80 | paint.setDither(true); |
Alexandre Lision | d588bff | 2013-10-08 12:43:01 -0400 | [diff] [blame] | 81 | paint.setAntiAlias(true); |
| 82 | paint.setShader(shader); |
| 83 | Canvas internalCanvas = new Canvas(externalBMP); |
Alexandre Lision | 6885547 | 2013-10-10 16:20:46 -0400 | [diff] [blame] | 84 | internalCanvas.drawCircle(w / 2, h / 2, w / 2, paint); |
Alexandre Lision | 40954dc | 2013-10-09 15:24:03 -0400 | [diff] [blame] | 85 | |
| 86 | Paint whiteStroke = new Paint(); |
| 87 | whiteStroke.setStyle(Style.STROKE); |
Alexandre Lision | 6885547 | 2013-10-10 16:20:46 -0400 | [diff] [blame] | 88 | whiteStroke.setStrokeWidth(8); |
| 89 | if (expanded) { |
| 90 | whiteStroke.setColor(mContext.getResources().getColor(R.color.sfl_action_blue)); |
| 91 | } else { |
| 92 | whiteStroke.setColor(Color.WHITE); |
| 93 | } |
| 94 | whiteStroke.setDither(true); |
| 95 | whiteStroke.setAntiAlias(true); |
| 96 | internalCanvas.drawCircle(w / 2, h / 2, w / 2 - 4, whiteStroke); |
Alexandre Lision | d588bff | 2013-10-08 12:43:01 -0400 | [diff] [blame] | 97 | |
| 98 | bounds = new RectF(pos.x - getRadius(), pos.y - getRadius(), pos.x + getRadius(), pos.y + getRadius()); |
Alexandre Lision | 6885547 | 2013-10-10 16:20:46 -0400 | [diff] [blame] | 99 | |
Alexandre Lision | d588bff | 2013-10-08 12:43:01 -0400 | [diff] [blame] | 100 | } |
alision | fe9cf71 | 2013-05-03 17:26:08 -0400 | [diff] [blame] | 101 | |
Alexandre Lision | 40954dc | 2013-10-09 15:24:03 -0400 | [diff] [blame] | 102 | protected Bitmap getContactPhoto(Context context, CallContact contact, int size) { |
| 103 | if (contact.getPhoto_id() > 0) { |
| 104 | return ContactPictureTask.loadContactPhoto(context.getContentResolver(), contact.getId()); |
Alexandre Lision | d588bff | 2013-10-08 12:43:01 -0400 | [diff] [blame] | 105 | } else { |
| 106 | return ContactPictureTask.decodeSampledBitmapFromResource(context.getResources(), R.drawable.ic_contact_picture, (int) size, (int) size); |
| 107 | } |
alision | 2ec64f9 | 2013-06-17 17:28:58 -0400 | [diff] [blame] | 108 | } |
Adrien Béraud | 25fc409 | 2013-05-06 15:28:39 +1000 | [diff] [blame] | 109 | |
alision | 2ec64f9 | 2013-06-17 17:28:58 -0400 | [diff] [blame] | 110 | public Bitmap getBitmap() { |
| 111 | return externalBMP; |
| 112 | } |
alision | fe9cf71 | 2013-05-03 17:26:08 -0400 | [diff] [blame] | 113 | |
alision | 2ec64f9 | 2013-06-17 17:28:58 -0400 | [diff] [blame] | 114 | public RectF getBounds() { |
| 115 | return bounds; |
| 116 | } |
Adrien Béraud | 3326888 | 2013-05-18 03:41:15 +1000 | [diff] [blame] | 117 | |
Alexandre Lision | 40954dc | 2013-10-09 15:24:03 -0400 | [diff] [blame] | 118 | public abstract void set(float x, float y, float s); |
Adrien Béraud | 25fc409 | 2013-05-06 15:28:39 +1000 | [diff] [blame] | 119 | |
alision | 2ec64f9 | 2013-06-17 17:28:58 -0400 | [diff] [blame] | 120 | public float getPosX() { |
| 121 | return pos.x; |
| 122 | } |
Adrien Béraud | 25fc409 | 2013-05-06 15:28:39 +1000 | [diff] [blame] | 123 | |
alision | 2ec64f9 | 2013-06-17 17:28:58 -0400 | [diff] [blame] | 124 | public float getPosY() { |
| 125 | return pos.y; |
| 126 | } |
Adrien Béraud | 6bbce91 | 2013-05-24 00:48:13 +1000 | [diff] [blame] | 127 | |
alision | 2ec64f9 | 2013-06-17 17:28:58 -0400 | [diff] [blame] | 128 | public void setPos(float x, float y) { |
| 129 | set(x, y, scale); |
| 130 | } |
alision | fe9cf71 | 2013-05-03 17:26:08 -0400 | [diff] [blame] | 131 | |
alision | 2ec64f9 | 2013-06-17 17:28:58 -0400 | [diff] [blame] | 132 | public PointF getPos() { |
| 133 | return pos; |
| 134 | } |
Adrien Béraud | 25fc409 | 2013-05-06 15:28:39 +1000 | [diff] [blame] | 135 | |
alision | 2ec64f9 | 2013-06-17 17:28:58 -0400 | [diff] [blame] | 136 | public float getScale() { |
| 137 | return scale; |
| 138 | } |
Adrien Béraud | 25fc409 | 2013-05-06 15:28:39 +1000 | [diff] [blame] | 139 | |
alision | 2ec64f9 | 2013-06-17 17:28:58 -0400 | [diff] [blame] | 140 | public void setScale(float s) { |
| 141 | set(pos.x, pos.y, s); |
| 142 | } |
Adrien Béraud | 25fc409 | 2013-05-06 15:28:39 +1000 | [diff] [blame] | 143 | |
Alexandre Lision | 40954dc | 2013-10-09 15:24:03 -0400 | [diff] [blame] | 144 | public abstract int getRadius(); |
Adrien Béraud | 6bbce91 | 2013-05-24 00:48:13 +1000 | [diff] [blame] | 145 | |
alision | 2ec64f9 | 2013-06-17 17:28:58 -0400 | [diff] [blame] | 146 | /** |
| 147 | * Point intersection test. |
| 148 | */ |
| 149 | boolean intersects(float x, float y) { |
alision | 34673e6 | 2013-06-25 14:40:07 -0400 | [diff] [blame] | 150 | float dx = x - pos.x; |
| 151 | float dy = y - pos.y; |
alision | 50fa072 | 2013-06-25 17:29:44 -0400 | [diff] [blame] | 152 | |
Alexandre Lision | 5ed2c97 | 2013-10-11 15:36:33 -0400 | [diff] [blame] | 153 | return dx * dx + dy * dy < getRadius() * getRadius(); |
alision | 2ec64f9 | 2013-06-17 17:28:58 -0400 | [diff] [blame] | 154 | } |
Adrien Béraud | 6bbce91 | 2013-05-24 00:48:13 +1000 | [diff] [blame] | 155 | |
alision | 2ec64f9 | 2013-06-17 17:28:58 -0400 | [diff] [blame] | 156 | /** |
| 157 | * Other circle intersection test. |
| 158 | */ |
| 159 | boolean intersects(float x, float y, float radius) { |
| 160 | float dx = x - pos.x, dy = y - pos.y; |
| 161 | float tot_radius = getRadius() + radius; |
| 162 | return dx * dx + dy * dy < tot_radius * tot_radius; |
| 163 | } |
Adrien Béraud | 6bbce91 | 2013-05-24 00:48:13 +1000 | [diff] [blame] | 164 | |
alision | 2ec64f9 | 2013-06-17 17:28:58 -0400 | [diff] [blame] | 165 | public void setDensity(float density) { |
| 166 | this.density = density; |
| 167 | } |
Adrien Béraud | 25fc409 | 2013-05-06 15:28:39 +1000 | [diff] [blame] | 168 | |
Alexandre Lision | 40954dc | 2013-10-09 15:24:03 -0400 | [diff] [blame] | 169 | public abstract void expand(int width, int height); |
alision | 2ec64f9 | 2013-06-17 17:28:58 -0400 | [diff] [blame] | 170 | |
| 171 | public void retract() { |
| 172 | expanded = false; |
Alexandre Lision | 40954dc | 2013-10-09 15:24:03 -0400 | [diff] [blame] | 173 | generateBitmap(); |
alision | 2ec64f9 | 2013-06-17 17:28:58 -0400 | [diff] [blame] | 174 | } |
alision | df1dac9 | 2013-06-27 17:35:53 -0400 | [diff] [blame] | 175 | |
Alexandre Lision | 0edf18c | 2013-09-23 17:35:50 -0400 | [diff] [blame] | 176 | public boolean isOnBorder(float w, float h) { |
| 177 | return (bounds.left < 0 || bounds.right > w || bounds.top < 0 || bounds.bottom > h); |
| 178 | } |
| 179 | |
alision | 729b0a2 | 2013-07-02 11:57:33 -0400 | [diff] [blame] | 180 | /** |
| 181 | * Always return the normal radius of the bubble |
alision | 91d2859 | 2013-07-02 14:33:53 -0400 | [diff] [blame] | 182 | * |
alision | 729b0a2 | 2013-07-02 11:57:33 -0400 | [diff] [blame] | 183 | * @return |
| 184 | */ |
| 185 | public float getRetractedRadius() { |
| 186 | return radius; |
| 187 | } |
| 188 | |
Alexandre Lision | 40954dc | 2013-10-09 15:24:03 -0400 | [diff] [blame] | 189 | public abstract boolean getHoldStatus(); |
alision | 729b0a2 | 2013-07-02 11:57:33 -0400 | [diff] [blame] | 190 | |
Alexandre Lision | 40954dc | 2013-10-09 15:24:03 -0400 | [diff] [blame] | 191 | public abstract boolean getRecordStatus(); |
Alexandre Lision | d588bff | 2013-10-08 12:43:01 -0400 | [diff] [blame] | 192 | |
Alexandre Lision | 40954dc | 2013-10-09 15:24:03 -0400 | [diff] [blame] | 193 | public abstract Bitmap getDrawerBitmap(); |
Alexandre Lision | d588bff | 2013-10-08 12:43:01 -0400 | [diff] [blame] | 194 | |
Alexandre Lision | 40954dc | 2013-10-09 15:24:03 -0400 | [diff] [blame] | 195 | public abstract RectF getDrawerBounds(); |
Alexandre Lision | 6885547 | 2013-10-10 16:20:46 -0400 | [diff] [blame] | 196 | |
| 197 | protected abstract class ActionDrawer { |
| 198 | |
Alexandre Lision | d588bff | 2013-10-08 12:43:01 -0400 | [diff] [blame] | 199 | int mWidth, mHeight; |
Alexandre Lision | d588bff | 2013-10-08 12:43:01 -0400 | [diff] [blame] | 200 | RectF bounds; |
Alexandre Lision | 40954dc | 2013-10-09 15:24:03 -0400 | [diff] [blame] | 201 | Bitmap img; |
Alexandre Lision | d588bff | 2013-10-08 12:43:01 -0400 | [diff] [blame] | 202 | |
Alexandre Lision | 40954dc | 2013-10-09 15:24:03 -0400 | [diff] [blame] | 203 | public ActionDrawer(int w, int h) { |
| 204 | |
Alexandre Lision | d588bff | 2013-10-08 12:43:01 -0400 | [diff] [blame] | 205 | mWidth = w; |
| 206 | mHeight = h; |
| 207 | bounds = new RectF(0, 0, 0, 0); |
Alexandre Lision | d588bff | 2013-10-08 12:43:01 -0400 | [diff] [blame] | 208 | |
Alexandre Lision | 40954dc | 2013-10-09 15:24:03 -0400 | [diff] [blame] | 209 | } |
Alexandre Lision | 6885547 | 2013-10-10 16:20:46 -0400 | [diff] [blame] | 210 | |
Alexandre Lision | 40954dc | 2013-10-09 15:24:03 -0400 | [diff] [blame] | 211 | /** |
| 212 | * When the bubble is expanded we need to check on wich action button the user tap |
| 213 | * |
| 214 | * @param x |
| 215 | * @param y |
| 216 | * @return |
| 217 | */ |
| 218 | public abstract int getAction(float x, float y); |
Alexandre Lision | 6885547 | 2013-10-10 16:20:46 -0400 | [diff] [blame] | 219 | |
Alexandre Lision | 40954dc | 2013-10-09 15:24:03 -0400 | [diff] [blame] | 220 | public void setBounds(float f, float y, float g, float h) { |
| 221 | bounds.set(f, y, g, h); |
| 222 | } |
Alexandre Lision | 6885547 | 2013-10-10 16:20:46 -0400 | [diff] [blame] | 223 | |
Alexandre Lision | 40954dc | 2013-10-09 15:24:03 -0400 | [diff] [blame] | 224 | public abstract void generateBitmap(); |
Alexandre Lision | 6885547 | 2013-10-10 16:20:46 -0400 | [diff] [blame] | 225 | |
Alexandre Lision | d588bff | 2013-10-08 12:43:01 -0400 | [diff] [blame] | 226 | public RectF getBounds() { |
| 227 | return bounds; |
| 228 | } |
| 229 | |
| 230 | public void setBounds(RectF bounds) { |
| 231 | this.bounds = bounds; |
| 232 | } |
| 233 | |
Alexandre Lision | 40954dc | 2013-10-09 15:24:03 -0400 | [diff] [blame] | 234 | public Bitmap getBitmap() { |
| 235 | return img; |
| 236 | } |
| 237 | |
Alexandre Lision | d588bff | 2013-10-08 12:43:01 -0400 | [diff] [blame] | 238 | public int getWidth() { |
| 239 | return mWidth; |
| 240 | } |
| 241 | |
| 242 | public int getHeight() { |
| 243 | return mHeight; |
| 244 | } |
Alexandre Lision | 6885547 | 2013-10-10 16:20:46 -0400 | [diff] [blame] | 245 | |
| 246 | public void adjustBounds(float x, float y) { |
| 247 | |
| 248 | } |
| 249 | |
Alexandre Lision | d588bff | 2013-10-08 12:43:01 -0400 | [diff] [blame] | 250 | } |
Alexandre Lision | 40954dc | 2013-10-09 15:24:03 -0400 | [diff] [blame] | 251 | |
| 252 | public ActionDrawer getDrawer() { |
| 253 | return act; |
| 254 | } |
Alexandre Lision | 6885547 | 2013-10-10 16:20:46 -0400 | [diff] [blame] | 255 | |
Alexandre Lision | 40954dc | 2013-10-09 15:24:03 -0400 | [diff] [blame] | 256 | public void setDrawer(ActionDrawer a) { |
| 257 | act = a; |
| 258 | } |
| 259 | |
Alexandre Lision | 6885547 | 2013-10-10 16:20:46 -0400 | [diff] [blame] | 260 | public abstract String getName(); |
| 261 | |
| 262 | public abstract boolean callIDEquals(String call); |
| 263 | |
| 264 | public abstract String getCallID(); |
| 265 | |
| 266 | public boolean isConference() { |
| 267 | return false; |
| 268 | } |
Alexandre Lision | ee2494d | 2013-10-09 17:14:00 -0400 | [diff] [blame] | 269 | |
alision | fe9cf71 | 2013-05-03 17:26:08 -0400 | [diff] [blame] | 270 | } |