blob: 1f8c405649e48402f49ac204d31c083c8c039b08 [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);
Alexandre Lisiond5686032013-10-29 11:09:21 -040065 isUser = false;
Alexandre Lision40954dc2013-10-09 15:24:03 -040066 }
Alexandre Lisiond588bff2013-10-08 12:43:01 -040067
Alexandre Lision40954dc2013-10-09 15:24:03 -040068 protected void generateBitmap() {
Alexandre Lision68855472013-10-10 16:20:46 -040069
Alexandre Lisiond588bff2013-10-08 12:43:01 -040070 int w = saved_photo.getWidth(), h = saved_photo.getHeight();
71 if (w > h) {
72 w = h;
73 } else if (h > w) {
74 h = w;
75 }
Alexandre Lision68855472013-10-10 16:20:46 -040076 externalBMP = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888);
Alexandre Lisiond588bff2013-10-08 12:43:01 -040077 BitmapShader shader;
78 shader = new BitmapShader(saved_photo, Shader.TileMode.CLAMP, Shader.TileMode.CLAMP);
79
80 Paint paint = new Paint();
Alexandre Lision68855472013-10-10 16:20:46 -040081 paint.setDither(true);
Alexandre Lisiond588bff2013-10-08 12:43:01 -040082 paint.setAntiAlias(true);
83 paint.setShader(shader);
84 Canvas internalCanvas = new Canvas(externalBMP);
Alexandre Lision68855472013-10-10 16:20:46 -040085 internalCanvas.drawCircle(w / 2, h / 2, w / 2, paint);
Alexandre Lision40954dc2013-10-09 15:24:03 -040086
87 Paint whiteStroke = new Paint();
88 whiteStroke.setStyle(Style.STROKE);
Alexandre Lision68855472013-10-10 16:20:46 -040089 whiteStroke.setStrokeWidth(8);
90 if (expanded) {
91 whiteStroke.setColor(mContext.getResources().getColor(R.color.sfl_action_blue));
92 } else {
93 whiteStroke.setColor(Color.WHITE);
94 }
95 whiteStroke.setDither(true);
96 whiteStroke.setAntiAlias(true);
97 internalCanvas.drawCircle(w / 2, h / 2, w / 2 - 4, whiteStroke);
Alexandre Lisiond588bff2013-10-08 12:43:01 -040098
99 bounds = new RectF(pos.x - getRadius(), pos.y - getRadius(), pos.x + getRadius(), pos.y + getRadius());
Alexandre Lision68855472013-10-10 16:20:46 -0400100
Alexandre Lisiond588bff2013-10-08 12:43:01 -0400101 }
alisionfe9cf712013-05-03 17:26:08 -0400102
Alexandre Lision40954dc2013-10-09 15:24:03 -0400103 protected Bitmap getContactPhoto(Context context, CallContact contact, int size) {
104 if (contact.getPhoto_id() > 0) {
105 return ContactPictureTask.loadContactPhoto(context.getContentResolver(), contact.getId());
Alexandre Lisiond588bff2013-10-08 12:43:01 -0400106 } else {
107 return ContactPictureTask.decodeSampledBitmapFromResource(context.getResources(), R.drawable.ic_contact_picture, (int) size, (int) size);
108 }
alision2ec64f92013-06-17 17:28:58 -0400109 }
Adrien Béraud25fc4092013-05-06 15:28:39 +1000110
alision2ec64f92013-06-17 17:28:58 -0400111 public Bitmap getBitmap() {
112 return externalBMP;
113 }
alisionfe9cf712013-05-03 17:26:08 -0400114
alision2ec64f92013-06-17 17:28:58 -0400115 public RectF getBounds() {
116 return bounds;
117 }
Adrien Béraud33268882013-05-18 03:41:15 +1000118
Alexandre Lision40954dc2013-10-09 15:24:03 -0400119 public abstract void set(float x, float y, float s);
Adrien Béraud25fc4092013-05-06 15:28:39 +1000120
alision2ec64f92013-06-17 17:28:58 -0400121 public float getPosX() {
122 return pos.x;
123 }
Adrien Béraud25fc4092013-05-06 15:28:39 +1000124
alision2ec64f92013-06-17 17:28:58 -0400125 public float getPosY() {
126 return pos.y;
127 }
Adrien Béraud6bbce912013-05-24 00:48:13 +1000128
alision2ec64f92013-06-17 17:28:58 -0400129 public void setPos(float x, float y) {
130 set(x, y, scale);
131 }
alisionfe9cf712013-05-03 17:26:08 -0400132
alision2ec64f92013-06-17 17:28:58 -0400133 public PointF getPos() {
134 return pos;
135 }
Adrien Béraud25fc4092013-05-06 15:28:39 +1000136
alision2ec64f92013-06-17 17:28:58 -0400137 public float getScale() {
138 return scale;
139 }
Adrien Béraud25fc4092013-05-06 15:28:39 +1000140
alision2ec64f92013-06-17 17:28:58 -0400141 public void setScale(float s) {
142 set(pos.x, pos.y, s);
143 }
Adrien Béraud25fc4092013-05-06 15:28:39 +1000144
Alexandre Lision40954dc2013-10-09 15:24:03 -0400145 public abstract int getRadius();
Adrien Béraud6bbce912013-05-24 00:48:13 +1000146
alision2ec64f92013-06-17 17:28:58 -0400147 /**
148 * Point intersection test.
149 */
150 boolean intersects(float x, float y) {
alision34673e62013-06-25 14:40:07 -0400151 float dx = x - pos.x;
152 float dy = y - pos.y;
alision50fa0722013-06-25 17:29:44 -0400153
Alexandre Lision5ed2c972013-10-11 15:36:33 -0400154 return dx * dx + dy * dy < getRadius() * getRadius();
alision2ec64f92013-06-17 17:28:58 -0400155 }
Adrien Béraud6bbce912013-05-24 00:48:13 +1000156
alision2ec64f92013-06-17 17:28:58 -0400157 /**
158 * Other circle intersection test.
159 */
160 boolean intersects(float x, float y, float radius) {
161 float dx = x - pos.x, dy = y - pos.y;
162 float tot_radius = getRadius() + radius;
163 return dx * dx + dy * dy < tot_radius * tot_radius;
164 }
Adrien Béraud6bbce912013-05-24 00:48:13 +1000165
alision2ec64f92013-06-17 17:28:58 -0400166 public void setDensity(float density) {
167 this.density = density;
168 }
Adrien Béraud25fc4092013-05-06 15:28:39 +1000169
Alexandre Lision40954dc2013-10-09 15:24:03 -0400170 public abstract void expand(int width, int height);
alision2ec64f92013-06-17 17:28:58 -0400171
172 public void retract() {
173 expanded = false;
Alexandre Lision40954dc2013-10-09 15:24:03 -0400174 generateBitmap();
alision2ec64f92013-06-17 17:28:58 -0400175 }
alisiondf1dac92013-06-27 17:35:53 -0400176
Alexandre Lision0edf18c2013-09-23 17:35:50 -0400177 public boolean isOnBorder(float w, float h) {
178 return (bounds.left < 0 || bounds.right > w || bounds.top < 0 || bounds.bottom > h);
179 }
180
alision729b0a22013-07-02 11:57:33 -0400181 /**
182 * Always return the normal radius of the bubble
alision91d28592013-07-02 14:33:53 -0400183 *
alision729b0a22013-07-02 11:57:33 -0400184 * @return
185 */
186 public float getRetractedRadius() {
187 return radius;
188 }
189
Alexandre Lision40954dc2013-10-09 15:24:03 -0400190 public abstract boolean getHoldStatus();
alision729b0a22013-07-02 11:57:33 -0400191
Alexandre Lision40954dc2013-10-09 15:24:03 -0400192 public abstract boolean getRecordStatus();
Alexandre Lisiond588bff2013-10-08 12:43:01 -0400193
Alexandre Lision40954dc2013-10-09 15:24:03 -0400194 public abstract Bitmap getDrawerBitmap();
Alexandre Lisiond588bff2013-10-08 12:43:01 -0400195
Alexandre Lision40954dc2013-10-09 15:24:03 -0400196 public abstract RectF getDrawerBounds();
Alexandre Lision68855472013-10-10 16:20:46 -0400197
198 protected abstract class ActionDrawer {
199
Alexandre Lisiond588bff2013-10-08 12:43:01 -0400200 int mWidth, mHeight;
Alexandre Lisiond588bff2013-10-08 12:43:01 -0400201 RectF bounds;
Alexandre Lision40954dc2013-10-09 15:24:03 -0400202 Bitmap img;
Alexandre Lisiond588bff2013-10-08 12:43:01 -0400203
Alexandre Lision40954dc2013-10-09 15:24:03 -0400204 public ActionDrawer(int w, int h) {
205
Alexandre Lisiond588bff2013-10-08 12:43:01 -0400206 mWidth = w;
207 mHeight = h;
208 bounds = new RectF(0, 0, 0, 0);
Alexandre Lisiond588bff2013-10-08 12:43:01 -0400209
Alexandre Lision40954dc2013-10-09 15:24:03 -0400210 }
Alexandre Lision68855472013-10-10 16:20:46 -0400211
Alexandre Lision40954dc2013-10-09 15:24:03 -0400212 /**
213 * When the bubble is expanded we need to check on wich action button the user tap
214 *
215 * @param x
216 * @param y
217 * @return
218 */
219 public abstract int getAction(float x, float y);
Alexandre Lision68855472013-10-10 16:20:46 -0400220
Alexandre Lision40954dc2013-10-09 15:24:03 -0400221 public void setBounds(float f, float y, float g, float h) {
222 bounds.set(f, y, g, h);
223 }
Alexandre Lision68855472013-10-10 16:20:46 -0400224
Alexandre Lision40954dc2013-10-09 15:24:03 -0400225 public abstract void generateBitmap();
Alexandre Lision68855472013-10-10 16:20:46 -0400226
Alexandre Lisiond588bff2013-10-08 12:43:01 -0400227 public RectF getBounds() {
228 return bounds;
229 }
230
231 public void setBounds(RectF bounds) {
232 this.bounds = bounds;
233 }
234
Alexandre Lision40954dc2013-10-09 15:24:03 -0400235 public Bitmap getBitmap() {
236 return img;
237 }
238
Alexandre Lisiond588bff2013-10-08 12:43:01 -0400239 public int getWidth() {
240 return mWidth;
241 }
242
243 public int getHeight() {
244 return mHeight;
245 }
Alexandre Lision68855472013-10-10 16:20:46 -0400246
247 public void adjustBounds(float x, float y) {
248
249 }
250
Alexandre Lisiond588bff2013-10-08 12:43:01 -0400251 }
Alexandre Lision40954dc2013-10-09 15:24:03 -0400252
253 public ActionDrawer getDrawer() {
254 return act;
255 }
Alexandre Lision68855472013-10-10 16:20:46 -0400256
Alexandre Lision40954dc2013-10-09 15:24:03 -0400257 public void setDrawer(ActionDrawer a) {
258 act = a;
259 }
260
Alexandre Lision68855472013-10-10 16:20:46 -0400261 public abstract String getName();
262
263 public abstract boolean callIDEquals(String call);
264
265 public abstract String getCallID();
266
267 public boolean isConference() {
268 return false;
269 }
Alexandre Lisionee2494d2013-10-09 17:14:00 -0400270
alisionfe9cf712013-05-03 17:26:08 -0400271}