blob: b76feca7c5d5890634a1799c503cebd3f1ebd3d7 [file] [log] [blame]
Alexandre Lision064e1e02013-10-01 16:18:42 -04001package org.sflphone.model;
2
Alexandre Lisiond588bff2013-10-08 12:43:01 -04003import org.sflphone.R;
Alexandre Lision064e1e02013-10-01 16:18:42 -04004import org.sflphone.adapters.ContactPictureTask;
alisionfe9cf712013-05-03 17:26:08 -04005
6import android.content.Context;
7import android.graphics.Bitmap;
Alexandre Lisiond588bff2013-10-08 12:43:01 -04008import android.graphics.BitmapShader;
alisionfe9cf712013-05-03 17:26:08 -04009import android.graphics.Canvas;
alision2ec64f92013-06-17 17:28:58 -040010import android.graphics.Color;
alisionfe9cf712013-05-03 17:26:08 -040011import android.graphics.Paint;
Alexandre Lisiond588bff2013-10-08 12:43:01 -040012import android.graphics.Paint.Style;
Adrien Béraud25fc4092013-05-06 15:28:39 +100013import android.graphics.PointF;
alisionfe9cf712013-05-03 17:26:08 -040014import android.graphics.RectF;
Alexandre Lisiond588bff2013-10-08 12:43:01 -040015import android.graphics.Shader;
alisionfe9cf712013-05-03 17:26:08 -040016
Alexandre Lision40954dc2013-10-09 15:24:03 -040017public abstract class Bubble {
alision806e18e2013-06-21 15:30:17 -040018
alision2ec64f92013-06-17 17:28:58 -040019 // A Bitmap object that is going to be passed to the BitmapShader
Alexandre Lision40954dc2013-10-09 15:24:03 -040020 protected Bitmap externalBMP;
alisionfe9cf712013-05-03 17:26:08 -040021
Alexandre Lision40954dc2013-10-09 15:24:03 -040022 protected PointF pos = new PointF();
23 protected RectF bounds;
alision2ec64f92013-06-17 17:28:58 -040024 public float target_scale = 1.f;
Alexandre Lision40954dc2013-10-09 15:24:03 -040025 protected float radius;
26 protected float scale = 1.f;
27 protected float density = 1.f;
alision2ec64f92013-06-17 17:28:58 -040028 public PointF speed = new PointF(0, 0);
29 public PointF last_speed = new PointF();
30 public PointF attractor = null;
Alexandre Lision40954dc2013-10-09 15:24:03 -040031 ActionDrawer act;
Alexandre Lision40954dc2013-10-09 15:24:03 -040032
Alexandre Lision68855472013-10-10 16:20:46 -040033 public boolean isUser;
alisionfe9cf712013-05-03 17:26:08 -040034
alision2ec64f92013-06-17 17:28:58 -040035 public boolean dragged = false;
Alexandre Lision6deda412013-09-25 13:21:22 -040036
Alexandre Lision0edf18c2013-09-23 17:35:50 -040037 public boolean markedToDie = false;
alision2ec64f92013-06-17 17:28:58 -040038 public long last_drag;
39 public boolean expanded; // determine if we draw the buttons around the bubble
Alexandre Lision40954dc2013-10-09 15:24:03 -040040 protected Bitmap saved_photo;
alision4a0eb092013-05-07 13:52:03 -040041
Alexandre Lision40954dc2013-10-09 15:24:03 -040042 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 Lisiond588bff2013-10-08 12:43:01 -040053
alision2ec64f92013-06-17 17:28:58 -040054 public void setAttractor(PointF attractor) {
55 this.attractor = attractor;
56 }
Adrien Béraude0ef0c22013-05-18 01:56:27 +100057
Alexandre Lision40954dc2013-10-09 15:24:03 -040058 public Bubble(Context context, CallContact contact, float x, float y, float size) {
59 mContext = context;
alision2ec64f92013-06-17 17:28:58 -040060 pos.set(x, y);
Alexandre Lision68855472013-10-10 16:20:46 -040061 radius = size / 2; // 10 is the white stroke
Alexandre Lision40954dc2013-10-09 15:24:03 -040062 saved_photo = getContactPhoto(context, contact, (int) size);
63 generateBitmap();
64 attractor = new PointF(x, y);
65 }
Alexandre Lisiond588bff2013-10-08 12:43:01 -040066
Alexandre Lision40954dc2013-10-09 15:24:03 -040067 protected void generateBitmap() {
Alexandre Lision68855472013-10-10 16:20:46 -040068
Alexandre Lisiond588bff2013-10-08 12:43:01 -040069 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 Lision68855472013-10-10 16:20:46 -040075 externalBMP = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888);
Alexandre Lisiond588bff2013-10-08 12:43:01 -040076 BitmapShader shader;
77 shader = new BitmapShader(saved_photo, Shader.TileMode.CLAMP, Shader.TileMode.CLAMP);
78
79 Paint paint = new Paint();
Alexandre Lision68855472013-10-10 16:20:46 -040080 paint.setDither(true);
Alexandre Lisiond588bff2013-10-08 12:43:01 -040081 paint.setAntiAlias(true);
82 paint.setShader(shader);
83 Canvas internalCanvas = new Canvas(externalBMP);
Alexandre Lision68855472013-10-10 16:20:46 -040084 internalCanvas.drawCircle(w / 2, h / 2, w / 2, paint);
Alexandre Lision40954dc2013-10-09 15:24:03 -040085
86 Paint whiteStroke = new Paint();
87 whiteStroke.setStyle(Style.STROKE);
Alexandre Lision68855472013-10-10 16:20:46 -040088 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 Lisiond588bff2013-10-08 12:43:01 -040097
98 bounds = new RectF(pos.x - getRadius(), pos.y - getRadius(), pos.x + getRadius(), pos.y + getRadius());
Alexandre Lision68855472013-10-10 16:20:46 -040099
Alexandre Lisiond588bff2013-10-08 12:43:01 -0400100 }
alisionfe9cf712013-05-03 17:26:08 -0400101
Alexandre Lision40954dc2013-10-09 15:24:03 -0400102 protected Bitmap getContactPhoto(Context context, CallContact contact, int size) {
103 if (contact.getPhoto_id() > 0) {
104 return ContactPictureTask.loadContactPhoto(context.getContentResolver(), contact.getId());
Alexandre Lisiond588bff2013-10-08 12:43:01 -0400105 } else {
106 return ContactPictureTask.decodeSampledBitmapFromResource(context.getResources(), R.drawable.ic_contact_picture, (int) size, (int) size);
107 }
alision2ec64f92013-06-17 17:28:58 -0400108 }
Adrien Béraud25fc4092013-05-06 15:28:39 +1000109
alision2ec64f92013-06-17 17:28:58 -0400110 public Bitmap getBitmap() {
111 return externalBMP;
112 }
alisionfe9cf712013-05-03 17:26:08 -0400113
alision2ec64f92013-06-17 17:28:58 -0400114 public RectF getBounds() {
115 return bounds;
116 }
Adrien Béraud33268882013-05-18 03:41:15 +1000117
Alexandre Lision40954dc2013-10-09 15:24:03 -0400118 public abstract void set(float x, float y, float s);
Adrien Béraud25fc4092013-05-06 15:28:39 +1000119
alision2ec64f92013-06-17 17:28:58 -0400120 public float getPosX() {
121 return pos.x;
122 }
Adrien Béraud25fc4092013-05-06 15:28:39 +1000123
alision2ec64f92013-06-17 17:28:58 -0400124 public float getPosY() {
125 return pos.y;
126 }
Adrien Béraud6bbce912013-05-24 00:48:13 +1000127
alision2ec64f92013-06-17 17:28:58 -0400128 public void setPos(float x, float y) {
129 set(x, y, scale);
130 }
alisionfe9cf712013-05-03 17:26:08 -0400131
alision2ec64f92013-06-17 17:28:58 -0400132 public PointF getPos() {
133 return pos;
134 }
Adrien Béraud25fc4092013-05-06 15:28:39 +1000135
alision2ec64f92013-06-17 17:28:58 -0400136 public float getScale() {
137 return scale;
138 }
Adrien Béraud25fc4092013-05-06 15:28:39 +1000139
alision2ec64f92013-06-17 17:28:58 -0400140 public void setScale(float s) {
141 set(pos.x, pos.y, s);
142 }
Adrien Béraud25fc4092013-05-06 15:28:39 +1000143
Alexandre Lision40954dc2013-10-09 15:24:03 -0400144 public abstract int getRadius();
Adrien Béraud6bbce912013-05-24 00:48:13 +1000145
alision2ec64f92013-06-17 17:28:58 -0400146 /**
147 * Point intersection test.
148 */
149 boolean intersects(float x, float y) {
alision34673e62013-06-25 14:40:07 -0400150 float dx = x - pos.x;
151 float dy = y - pos.y;
alision50fa0722013-06-25 17:29:44 -0400152
Alexandre Lision5ed2c972013-10-11 15:36:33 -0400153 return dx * dx + dy * dy < getRadius() * getRadius();
alision2ec64f92013-06-17 17:28:58 -0400154 }
Adrien Béraud6bbce912013-05-24 00:48:13 +1000155
alision2ec64f92013-06-17 17:28:58 -0400156 /**
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éraud6bbce912013-05-24 00:48:13 +1000164
alision2ec64f92013-06-17 17:28:58 -0400165 public void setDensity(float density) {
166 this.density = density;
167 }
Adrien Béraud25fc4092013-05-06 15:28:39 +1000168
Alexandre Lision40954dc2013-10-09 15:24:03 -0400169 public abstract void expand(int width, int height);
alision2ec64f92013-06-17 17:28:58 -0400170
171 public void retract() {
172 expanded = false;
Alexandre Lision40954dc2013-10-09 15:24:03 -0400173 generateBitmap();
alision2ec64f92013-06-17 17:28:58 -0400174 }
alisiondf1dac92013-06-27 17:35:53 -0400175
Alexandre Lision0edf18c2013-09-23 17:35:50 -0400176 public boolean isOnBorder(float w, float h) {
177 return (bounds.left < 0 || bounds.right > w || bounds.top < 0 || bounds.bottom > h);
178 }
179
alision729b0a22013-07-02 11:57:33 -0400180 /**
181 * Always return the normal radius of the bubble
alision91d28592013-07-02 14:33:53 -0400182 *
alision729b0a22013-07-02 11:57:33 -0400183 * @return
184 */
185 public float getRetractedRadius() {
186 return radius;
187 }
188
Alexandre Lision40954dc2013-10-09 15:24:03 -0400189 public abstract boolean getHoldStatus();
alision729b0a22013-07-02 11:57:33 -0400190
Alexandre Lision40954dc2013-10-09 15:24:03 -0400191 public abstract boolean getRecordStatus();
Alexandre Lisiond588bff2013-10-08 12:43:01 -0400192
Alexandre Lision40954dc2013-10-09 15:24:03 -0400193 public abstract Bitmap getDrawerBitmap();
Alexandre Lisiond588bff2013-10-08 12:43:01 -0400194
Alexandre Lision40954dc2013-10-09 15:24:03 -0400195 public abstract RectF getDrawerBounds();
Alexandre Lision68855472013-10-10 16:20:46 -0400196
197 protected abstract class ActionDrawer {
198
Alexandre Lisiond588bff2013-10-08 12:43:01 -0400199 int mWidth, mHeight;
Alexandre Lisiond588bff2013-10-08 12:43:01 -0400200 RectF bounds;
Alexandre Lision40954dc2013-10-09 15:24:03 -0400201 Bitmap img;
Alexandre Lisiond588bff2013-10-08 12:43:01 -0400202
Alexandre Lision40954dc2013-10-09 15:24:03 -0400203 public ActionDrawer(int w, int h) {
204
Alexandre Lisiond588bff2013-10-08 12:43:01 -0400205 mWidth = w;
206 mHeight = h;
207 bounds = new RectF(0, 0, 0, 0);
Alexandre Lisiond588bff2013-10-08 12:43:01 -0400208
Alexandre Lision40954dc2013-10-09 15:24:03 -0400209 }
Alexandre Lision68855472013-10-10 16:20:46 -0400210
Alexandre Lision40954dc2013-10-09 15:24:03 -0400211 /**
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 Lision68855472013-10-10 16:20:46 -0400219
Alexandre Lision40954dc2013-10-09 15:24:03 -0400220 public void setBounds(float f, float y, float g, float h) {
221 bounds.set(f, y, g, h);
222 }
Alexandre Lision68855472013-10-10 16:20:46 -0400223
Alexandre Lision40954dc2013-10-09 15:24:03 -0400224 public abstract void generateBitmap();
Alexandre Lision68855472013-10-10 16:20:46 -0400225
Alexandre Lisiond588bff2013-10-08 12:43:01 -0400226 public RectF getBounds() {
227 return bounds;
228 }
229
230 public void setBounds(RectF bounds) {
231 this.bounds = bounds;
232 }
233
Alexandre Lision40954dc2013-10-09 15:24:03 -0400234 public Bitmap getBitmap() {
235 return img;
236 }
237
Alexandre Lisiond588bff2013-10-08 12:43:01 -0400238 public int getWidth() {
239 return mWidth;
240 }
241
242 public int getHeight() {
243 return mHeight;
244 }
Alexandre Lision68855472013-10-10 16:20:46 -0400245
246 public void adjustBounds(float x, float y) {
247
248 }
249
Alexandre Lisiond588bff2013-10-08 12:43:01 -0400250 }
Alexandre Lision40954dc2013-10-09 15:24:03 -0400251
252 public ActionDrawer getDrawer() {
253 return act;
254 }
Alexandre Lision68855472013-10-10 16:20:46 -0400255
Alexandre Lision40954dc2013-10-09 15:24:03 -0400256 public void setDrawer(ActionDrawer a) {
257 act = a;
258 }
259
Alexandre Lision68855472013-10-10 16:20:46 -0400260 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 Lisionee2494d2013-10-09 17:14:00 -0400269
alisionfe9cf712013-05-03 17:26:08 -0400270}