blob: 7e479088cfcfcd3354f40665c920d760ca03dfc0 [file] [log] [blame]
Alexandre Lision40954dc2013-10-09 15:24:03 -04001package org.sflphone.model;
2
3import org.sflphone.R;
4
5import android.content.Context;
6import android.graphics.Bitmap;
7import android.graphics.Bitmap.Config;
Alexandre Lision8fdcf2b2013-10-30 16:06:51 -04008import android.graphics.Paint.Style;
Alexandre Lision40954dc2013-10-09 15:24:03 -04009import android.graphics.BitmapFactory;
10import android.graphics.Canvas;
11import android.graphics.Paint;
Alexandre Lision40954dc2013-10-09 15:24:03 -040012import android.graphics.RectF;
13import android.util.Log;
Alexandre Lisiond9c3f7f2013-10-30 15:20:55 -040014import android.view.MotionEvent;
Alexandre Lision40954dc2013-10-09 15:24:03 -040015
16public class BubbleContact extends Bubble {
17
18 public SipCall associated_call;
Alexandre Lisionee2494d2013-10-09 17:14:00 -040019 Bitmap buttonMsg, buttonUnhold, buttonHold, buttonTransfer, buttonHangUp;
Alexandre Lision40954dc2013-10-09 15:24:03 -040020
Alexandre Lision68855472013-10-10 16:20:46 -040021 public interface drawerPosition {
22 int UNDEFINED = -1;
23 int TOP = 0;
24 int RIGHT = 1;
25 int BOTTOM = 2;
26 int LEFT = 3;
27 }
28
Alexandre Lision40954dc2013-10-09 15:24:03 -040029 public BubbleContact(Context context, SipCall call, float x, float y, float size) {
30 super(context, call.getContact(), x, y, size);
31 associated_call = call;
Alexandre Lision8fdcf2b2013-10-30 16:06:51 -040032
Alexandre Lision40954dc2013-10-09 15:24:03 -040033 buttonMsg = BitmapFactory.decodeResource(mContext.getResources(), R.drawable.ic_action_chat);
34 buttonHold = BitmapFactory.decodeResource(mContext.getResources(), R.drawable.ic_action_pause_over_video);
Alexandre Lisionee2494d2013-10-09 17:14:00 -040035 buttonUnhold = BitmapFactory.decodeResource(mContext.getResources(), R.drawable.ic_action_play_over_video);
Alexandre Lision40954dc2013-10-09 15:24:03 -040036 buttonTransfer = BitmapFactory.decodeResource(mContext.getResources(), R.drawable.ic_action_forward);
37 buttonHangUp = BitmapFactory.decodeResource(mContext.getResources(), R.drawable.ic_action_end_call);
Alexandre Lision8fdcf2b2013-10-30 16:06:51 -040038
39 setDrawer(new ActionDrawer(0, 0, drawerPosition.UNDEFINED));
Alexandre Lision40954dc2013-10-09 15:24:03 -040040
41 }
42
43 @Override
44 public void expand(int width, int height) {
45
46 expanded = true;
47 generateBitmap();
48 if (pos.x < width / 3) {
49
Alexandre Lision68855472013-10-10 16:20:46 -040050 // Left
51 act = new ActionDrawer(width * 2 / 3, (int) (getRadius() * 1.5f), drawerPosition.LEFT);
52 act.adjustBounds(pos.x, pos.y);
Alexandre Lisiond9c3f7f2013-10-30 15:20:55 -040053 act.generateBitmap(actions.NOTHING);
Alexandre Lision40954dc2013-10-09 15:24:03 -040054
Alexandre Lision40954dc2013-10-09 15:24:03 -040055 } else if (pos.x > 2 * width / 3) {
Alexandre Lision68855472013-10-10 16:20:46 -040056 // Right
57 act = new ActionDrawer(width * 2 / 3, (int) (getRadius() * 1.5f), drawerPosition.RIGHT);
58 act.adjustBounds(pos.x, pos.y);
Alexandre Lisiond9c3f7f2013-10-30 15:20:55 -040059 act.generateBitmap(actions.NOTHING);
Alexandre Lision40954dc2013-10-09 15:24:03 -040060
61 } else {
62 // Middle of the screen
63 if (pos.y < height / 3) {
64 // Middle Top
65
Alexandre Lision68855472013-10-10 16:20:46 -040066 act = new ActionDrawer((int) (getRadius() * 1.5f), height / 2, drawerPosition.TOP);
67 act.adjustBounds(pos.x, pos.y);
Alexandre Lisiond9c3f7f2013-10-30 15:20:55 -040068 act.generateBitmap(actions.NOTHING);
Alexandre Lision40954dc2013-10-09 15:24:03 -040069
70 } else if (pos.y > 2 * height / 3) {
71 // Middle Bottom
72
Alexandre Lision68855472013-10-10 16:20:46 -040073 act = new ActionDrawer((int) (getRadius() * 1.5f), height / 2, drawerPosition.BOTTOM);
74 act.adjustBounds(pos.x, pos.y);
Alexandre Lisiond9c3f7f2013-10-30 15:20:55 -040075 act.generateBitmap(actions.NOTHING);
Alexandre Lision40954dc2013-10-09 15:24:03 -040076
Alexandre Lision40954dc2013-10-09 15:24:03 -040077 }
78 }
79
80 }
81
Alexandre Lisionee2494d2013-10-09 17:14:00 -040082 protected class ActionDrawer extends Bubble.ActionDrawer {
Alexandre Lision40954dc2013-10-09 15:24:03 -040083
Alexandre Lision68855472013-10-10 16:20:46 -040084 int direction;
Alexandre Lision40954dc2013-10-09 15:24:03 -040085 RectF boundsHoldButton, boundsMsgButton, boundsTransferButton, boundsHangUpButton;
Alexandre Lision5cd659e2013-10-29 13:18:23 -040086 private String TAG = ActionDrawer.class.getSimpleName();
Alexandre Lision8fdcf2b2013-10-30 16:06:51 -040087 Paint mBackgroundPaint;
88 Paint mSelector;
89 Paint mButtonPaint;
90
91 int wHang, hHang;
92 int wHold, hHold;
93 int wMsg, hMsg;
94 int wTrans, hTrans;
95 private RectF boundsTransferIcon;
96 private RectF boundsMsgIcon;
97 private RectF boundsHangIcon;
98 private RectF boundsHoldIcon;
99
100 Paint pButtons = new Paint();
Alexandre Lision40954dc2013-10-09 15:24:03 -0400101
Alexandre Lision68855472013-10-10 16:20:46 -0400102 public ActionDrawer(int w, int h, int dir) {
Alexandre Lision40954dc2013-10-09 15:24:03 -0400103 super(w, h);
Alexandre Lision68855472013-10-10 16:20:46 -0400104 direction = dir;
Alexandre Lision8fdcf2b2013-10-30 16:06:51 -0400105
106 mBackgroundPaint = new Paint();
107 mBackgroundPaint.setColor(mContext.getResources().getColor(R.color.sfl_blue_9));
108 mBackgroundPaint.setDither(true);
109
110 mSelector = new Paint();
111 mSelector.setStyle(Style.FILL);
112 mSelector.setColor(mContext.getResources().getColor(R.color.sfl_light_blue));
113
114 mButtonPaint = new Paint();
115
116 wHang = buttonHangUp.getWidth();
117 hHang = buttonHangUp.getHeight();
118
119 wMsg = buttonMsg.getWidth();
120 hMsg = buttonMsg.getHeight();
121
122 wHold = buttonHold.getWidth();
123 hHold = buttonHold.getHeight();
124
125 wTrans = buttonTransfer.getWidth();
126 hTrans = buttonTransfer.getHeight();
Alexandre Lision40954dc2013-10-09 15:24:03 -0400127 }
Alexandre Lisionee2494d2013-10-09 17:14:00 -0400128
Alexandre Lision40954dc2013-10-09 15:24:03 -0400129 @Override
130 public int getAction(float x, float y) {
131
Alexandre Lision5cd659e2013-10-29 13:18:23 -0400132 float relativeX = x - getDrawerBounds().left;
133 float relativeY = y - getDrawerBounds().top;
Alexandre Lision8fdcf2b2013-10-30 16:06:51 -0400134
Alexandre Lision5cd659e2013-10-29 13:18:23 -0400135 int result = actions.NOTHING;
Alexandre Lision8fdcf2b2013-10-30 16:06:51 -0400136
137 if (!getDrawerBounds().contains(x, y) && !getBounds().contains(x, y)) {
Alexandre Lision5cd659e2013-10-29 13:18:23 -0400138 return actions.OUT_OF_BOUNDS;
139 }
Alexandre Lision40954dc2013-10-09 15:24:03 -0400140
141 if (boundsHoldButton.contains(relativeX, relativeY)) {
142 Log.i("Bubble", "Holding");
Alexandre Lision8fdcf2b2013-10-30 16:06:51 -0400143 result = actions.HOLD;
Alexandre Lision40954dc2013-10-09 15:24:03 -0400144 }
145
146 if (boundsMsgButton.contains(relativeX, relativeY)) {
147 Log.i("Bubble", "Msg");
Alexandre Lision8fdcf2b2013-10-30 16:06:51 -0400148 result = actions.MESSAGE;
Alexandre Lision40954dc2013-10-09 15:24:03 -0400149 }
150
151 if (boundsHangUpButton.contains(relativeX, relativeY)) {
152 Log.i("Bubble", "hangUp");
Alexandre Lision8fdcf2b2013-10-30 16:06:51 -0400153 result = actions.HANGUP;
Alexandre Lision40954dc2013-10-09 15:24:03 -0400154 }
155
156 if (boundsTransferButton.contains(relativeX, relativeY)) {
157 Log.i("Bubble", "Transfer");
Alexandre Lision5cd659e2013-10-29 13:18:23 -0400158 result = actions.TRANSFER;
Alexandre Lision40954dc2013-10-09 15:24:03 -0400159 }
Alexandre Lision8fdcf2b2013-10-30 16:06:51 -0400160
Alexandre Lision5cd659e2013-10-29 13:18:23 -0400161 return result;
Alexandre Lision40954dc2013-10-09 15:24:03 -0400162
163 }
164
Alexandre Lisiond9c3f7f2013-10-30 15:20:55 -0400165 public void generateBitmap(int action) {
Alexandre Lision40954dc2013-10-09 15:24:03 -0400166
167 img = Bitmap.createBitmap(mWidth, mHeight, Config.ARGB_8888);
168 Paint paint = new Paint();
Alexandre Lisione5b66022013-10-30 11:34:15 -0400169 paint.setColor(mContext.getResources().getColor(R.color.sfl_blue_9));
Alexandre Lision40954dc2013-10-09 15:24:03 -0400170 Canvas c = new Canvas(img);
171 c.drawRect(new RectF(0, 0, mWidth, mHeight), paint);
Alexandre Lision68855472013-10-10 16:20:46 -0400172 float rHeight, rWidth;
Alexandre Lision40954dc2013-10-09 15:24:03 -0400173
Alexandre Lision68855472013-10-10 16:20:46 -0400174 switch (direction) {
175 case drawerPosition.TOP:
176 rHeight = bounds.height() - getRadius();
Alexandre Lision40954dc2013-10-09 15:24:03 -0400177 boundsHoldButton = new RectF(0, getRadius(), mWidth, getRadius() + rHeight / 4);
178 boundsMsgButton = new RectF(0, getRadius() + rHeight / 4, mWidth, getRadius() + 2 * rHeight / 4);
179 boundsTransferButton = new RectF(0, getRadius() + 2 * rHeight / 4, mWidth, getRadius() + 3 * rHeight / 4);
180 boundsHangUpButton = new RectF(0, getRadius() + 3 * rHeight / 4, mWidth, getRadius() + rHeight);
181
Alexandre Lision8fdcf2b2013-10-30 16:06:51 -0400182 calculateIconBounds();
183 draw(c, action);
Alexandre Lision68855472013-10-10 16:20:46 -0400184 break;
185 case drawerPosition.BOTTOM:
186 rHeight = bounds.height() - getRadius();
187 boundsHoldButton = new RectF(0, 0, mWidth, rHeight / 4);
188 boundsMsgButton = new RectF(0, rHeight / 4, mWidth, 2 * rHeight / 4);
189 boundsTransferButton = new RectF(0, 2 * rHeight / 4, mWidth, 3 * rHeight / 4);
190 boundsHangUpButton = new RectF(0, 3 * rHeight / 4, mWidth, rHeight);
191
Alexandre Lision8fdcf2b2013-10-30 16:06:51 -0400192 calculateIconBounds();
193 draw(c, action);
Alexandre Lision68855472013-10-10 16:20:46 -0400194 break;
195 case drawerPosition.RIGHT:
196 rWidth = bounds.width() - getRadius();
197 boundsHoldButton = new RectF(0, 0, rWidth / 4, mHeight);
198 boundsMsgButton = new RectF(rWidth / 4, 0, 2 * rWidth / 4, mHeight);
199 boundsTransferButton = new RectF(2 * rWidth / 4, 0, 3 * rWidth / 4, mHeight);
200 boundsHangUpButton = new RectF(3 * rWidth / 4, 0, rWidth, mHeight);
201
Alexandre Lision8fdcf2b2013-10-30 16:06:51 -0400202 calculateIconBounds();
203 draw(c, action);
Alexandre Lision68855472013-10-10 16:20:46 -0400204 break;
205 case drawerPosition.LEFT:
Alexandre Lision8fdcf2b2013-10-30 16:06:51 -0400206
Alexandre Lision68855472013-10-10 16:20:46 -0400207 rWidth = bounds.width() - getRadius();
208 boundsHoldButton = new RectF(getRadius(), 0, getRadius() + rWidth / 4, mHeight);
209 boundsMsgButton = new RectF(getRadius() + rWidth / 4, 0, getRadius() + 2 * rWidth / 4, mHeight);
210 boundsTransferButton = new RectF(getRadius() + 2 * rWidth / 4, 0, getRadius() + 3 * rWidth / 4, mHeight);
211 boundsHangUpButton = new RectF(getRadius() + 3 * rWidth / 4, 0, getRadius() + rWidth, mHeight);
212
Alexandre Lision8fdcf2b2013-10-30 16:06:51 -0400213 calculateIconBounds();
214 draw(c, action);
Alexandre Lision68855472013-10-10 16:20:46 -0400215 break;
Alexandre Lision40954dc2013-10-09 15:24:03 -0400216 }
217
218 }
219
Alexandre Lision8fdcf2b2013-10-30 16:06:51 -0400220 private void draw(Canvas c, int action) {
221 if (action == actions.HANGUP) {
222 c.drawCircle(boundsHangUpButton.centerX(), boundsHangUpButton.centerY(), boundsHangUpButton.width() / 2, mSelector);
223 }
224 c.drawBitmap(buttonHangUp, null, boundsHangIcon, pButtons);
225
226 if (action == actions.HOLD) {
227 c.drawCircle(boundsHoldButton.centerX(), boundsHoldButton.centerY(), boundsHoldButton.width() / 2, mSelector);
228 }
229 if (associated_call.isOnHold()) {
230 c.drawBitmap(buttonUnhold, null, boundsHoldIcon, pButtons);
231 } else {
232 c.drawBitmap(buttonHold, null, boundsHoldIcon, pButtons);
233 }
234 if (action == actions.MESSAGE) {
235 c.drawCircle(boundsMsgButton.centerX(), boundsMsgButton.centerY(), boundsMsgButton.width() / 2, mSelector);
236 }
237 c.drawBitmap(buttonMsg, null, boundsMsgIcon, pButtons);
238
239 if (action == actions.TRANSFER) {
240 c.drawCircle(boundsTransferButton.centerX(), boundsTransferButton.centerY(), boundsTransferButton.width() / 2, mSelector);
241 }
242 c.drawBitmap(buttonTransfer, null, boundsTransferIcon, pButtons);
243 }
244
245 private void calculateIconBounds() {
246 boundsHoldIcon = new RectF((int) boundsHoldButton.centerX() - wHold / 2, (int) boundsHoldButton.centerY() - hHold / 2,
247 (int) boundsHoldButton.centerX() + wHold / 2, (int) boundsHoldButton.centerY() + hHold / 2);
248 boundsHangIcon = new RectF((int) boundsHangUpButton.centerX() - wHang / 2, (int) boundsHangUpButton.centerY() - hHang / 2,
249 (int) boundsHangUpButton.centerX() + wHang / 2, (int) boundsHangUpButton.centerY() + hHang / 2);
250 boundsMsgIcon = new RectF((int) boundsMsgButton.centerX() - wMsg / 2, (int) boundsMsgButton.centerY() - hMsg / 2,
251 (int) boundsMsgButton.centerX() + wMsg / 2, (int) boundsMsgButton.centerY() + hMsg / 2);
252 boundsTransferIcon = new RectF((int) boundsTransferButton.centerX() - wTrans / 2, (int) boundsTransferButton.centerY() - hTrans / 2,
253 (int) boundsTransferButton.centerX() + wTrans / 2, (int) boundsTransferButton.centerY() + hTrans / 2);
254 }
255
Alexandre Lision68855472013-10-10 16:20:46 -0400256 public void adjustBounds(float x, float y) {
257 switch (direction) {
258 case drawerPosition.TOP:
259 setBounds(x - getRadius(), y, x + getRadius(), y + getHeight());
260 break;
261 case drawerPosition.BOTTOM:
262 setBounds(x - getRadius(), y - getHeight(), x + getRadius(), y);
263 break;
264 case drawerPosition.RIGHT:
265 setBounds(x - getWidth(), y - getRadius(), x, y + +getRadius());
266 break;
267 case drawerPosition.LEFT:
268 setBounds(x, y - getRadius(), x + getWidth(), y + getRadius());
269 break;
270 }
271
272 }
273
274 @Override
275 public void setBounds(float left, float top, float right, float bottom) {
276 int margin = (int) (0.5f * getRadius()) / 2;
277 switch (direction) {
278 case drawerPosition.TOP:
279 case drawerPosition.BOTTOM:
280 super.setBounds(left + margin, top, right - margin, bottom);
281 break;
282 case drawerPosition.RIGHT:
283 case drawerPosition.LEFT:
284 super.setBounds(left, top + margin, right, bottom - margin);
285 break;
286 }
287
288 }
Alexandre Lision40954dc2013-10-09 15:24:03 -0400289 }
290
291 public Bitmap getDrawerBitmap() {
292 return act.getBitmap();
293 }
294
295 public RectF getDrawerBounds() {
Alexandre Lision5cd659e2013-10-29 13:18:23 -0400296 return act.getDrawerBounds();
Alexandre Lision40954dc2013-10-09 15:24:03 -0400297 }
298
299 @Override
300 public void set(float x, float y, float s) {
301 scale = s;
302 pos.x = x;
303 pos.y = y;
304 if (!expanded) {
305 bounds.set(pos.x - getRadius(), pos.y - getRadius(), pos.x + getRadius(), pos.y + getRadius());
306 } else {
307 bounds.set(pos.x - getRadius(), pos.y - getRadius(), pos.x + getRadius(), pos.y + getRadius());
Alexandre Lision68855472013-10-10 16:20:46 -0400308 act.adjustBounds(pos.x, pos.y);
Alexandre Lision40954dc2013-10-09 15:24:03 -0400309 }
310 }
311
312 @Override
313 public int getRadius() {
Alexandre Lision8fdcf2b2013-10-30 16:06:51 -0400314 if (expanded)
Alexandre Lisiona9485b82013-10-25 10:00:57 -0400315 return (int) (radius * density);
Alexandre Lision40954dc2013-10-09 15:24:03 -0400316 return (int) (radius * scale * density);
317 }
318
Alexandre Lision40954dc2013-10-09 15:24:03 -0400319 @Override
320 public boolean getHoldStatus() {
321 if (associated_call.isOnHold())
322 return true;
323 else
324 return false;
325 }
326
327 @Override
328 public boolean getRecordStatus() {
329 if (associated_call.isRecording())
330 return true;
331 else
332 return false;
333 }
334
Alexandre Lision40954dc2013-10-09 15:24:03 -0400335 public SipCall getCall() {
336 return associated_call;
337 }
338
Alexandre Lisionee2494d2013-10-09 17:14:00 -0400339 public void setCall(SipCall call) {
340 associated_call = call;
Alexandre Lision68855472013-10-10 16:20:46 -0400341 if (expanded) {
Alexandre Lisiond9c3f7f2013-10-30 15:20:55 -0400342 act.generateBitmap(actions.NOTHING);
Alexandre Lisionee2494d2013-10-09 17:14:00 -0400343 }
Alexandre Lision68855472013-10-10 16:20:46 -0400344
Alexandre Lisionee2494d2013-10-09 17:14:00 -0400345 }
346
347 @Override
Alexandre Lision68855472013-10-10 16:20:46 -0400348 public String getName() {
349 return associated_call.getContact().getmDisplayName();
350 }
351
352 @Override
353 public boolean callIDEquals(String call) {
354 return associated_call.getCallId().contentEquals(call);
355 }
356
357 @Override
358 public String getCallID() {
359 return associated_call.getCallId();
Alexandre Lisionee2494d2013-10-09 17:14:00 -0400360 }
Alexandre Lision8fdcf2b2013-10-30 16:06:51 -0400361
Alexandre Lisiond9c3f7f2013-10-30 15:20:55 -0400362 @Override
363 public boolean onDown(MotionEvent event) {
Alexandre Lision8fdcf2b2013-10-30 16:06:51 -0400364 if (expanded) {
365 act.generateBitmap(act.getAction(event.getX(), event.getY()));
366 return false;
367 }
368 if (intersects(event.getX(), event.getY())) {
Alexandre Lisiond9c3f7f2013-10-30 15:20:55 -0400369 dragged = true;
370 last_drag = System.nanoTime();
371 setPos(event.getX(), event.getY());
372 target_scale = .8f;
373 return true;
374 }
375 return false;
376 }
Alexandre Lisionee2494d2013-10-09 17:14:00 -0400377
Alexandre Lision40954dc2013-10-09 15:24:03 -0400378}