blob: e1b73e62981db67081e41df52a98d9220d669ebb [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 Lisiond5ee3962013-11-08 15:37:28 -050032
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 Lisiond5ee3962013-11-08 15:37:28 -050038
Alexandre Lision8fdcf2b2013-10-30 16:06:51 -040039 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
88 int wHang, hHang;
89 int wHold, hHold;
90 int wMsg, hMsg;
91 int wTrans, hTrans;
Alexandre Lisiond5ee3962013-11-08 15:37:28 -050092 private RectF boundsTransferIcon,boundsMsgIcon, boundsHangIcon, boundsHoldIcon;
93
94 private int LINE_PADDING = 25;
95
Alexandre Lision8fdcf2b2013-10-30 16:06:51 -040096
97 Paint pButtons = new Paint();
Alexandre Lision40954dc2013-10-09 15:24:03 -040098
Alexandre Lision68855472013-10-10 16:20:46 -040099 public ActionDrawer(int w, int h, int dir) {
Alexandre Lision40954dc2013-10-09 15:24:03 -0400100 super(w, h);
Alexandre Lision68855472013-10-10 16:20:46 -0400101 direction = dir;
Alexandre Lision8fdcf2b2013-10-30 16:06:51 -0400102
Alexandre Lision8fdcf2b2013-10-30 16:06:51 -0400103 wHang = buttonHangUp.getWidth();
104 hHang = buttonHangUp.getHeight();
105
106 wMsg = buttonMsg.getWidth();
107 hMsg = buttonMsg.getHeight();
108
109 wHold = buttonHold.getWidth();
110 hHold = buttonHold.getHeight();
111
112 wTrans = buttonTransfer.getWidth();
113 hTrans = buttonTransfer.getHeight();
Alexandre Lision40954dc2013-10-09 15:24:03 -0400114 }
Alexandre Lisionee2494d2013-10-09 17:14:00 -0400115
Alexandre Lision40954dc2013-10-09 15:24:03 -0400116 @Override
117 public int getAction(float x, float y) {
118
Alexandre Lision5cd659e2013-10-29 13:18:23 -0400119 float relativeX = x - getDrawerBounds().left;
120 float relativeY = y - getDrawerBounds().top;
Alexandre Lision8fdcf2b2013-10-30 16:06:51 -0400121
Alexandre Lision5cd659e2013-10-29 13:18:23 -0400122 int result = actions.NOTHING;
Alexandre Lision8fdcf2b2013-10-30 16:06:51 -0400123
124 if (!getDrawerBounds().contains(x, y) && !getBounds().contains(x, y)) {
Alexandre Lision5cd659e2013-10-29 13:18:23 -0400125 return actions.OUT_OF_BOUNDS;
126 }
Alexandre Lision40954dc2013-10-09 15:24:03 -0400127
128 if (boundsHoldButton.contains(relativeX, relativeY)) {
129 Log.i("Bubble", "Holding");
Alexandre Lision8fdcf2b2013-10-30 16:06:51 -0400130 result = actions.HOLD;
Alexandre Lision40954dc2013-10-09 15:24:03 -0400131 }
132
133 if (boundsMsgButton.contains(relativeX, relativeY)) {
134 Log.i("Bubble", "Msg");
Alexandre Lision8fdcf2b2013-10-30 16:06:51 -0400135 result = actions.MESSAGE;
Alexandre Lision40954dc2013-10-09 15:24:03 -0400136 }
137
138 if (boundsHangUpButton.contains(relativeX, relativeY)) {
139 Log.i("Bubble", "hangUp");
Alexandre Lision8fdcf2b2013-10-30 16:06:51 -0400140 result = actions.HANGUP;
Alexandre Lision40954dc2013-10-09 15:24:03 -0400141 }
142
143 if (boundsTransferButton.contains(relativeX, relativeY)) {
144 Log.i("Bubble", "Transfer");
Alexandre Lision5cd659e2013-10-29 13:18:23 -0400145 result = actions.TRANSFER;
Alexandre Lision40954dc2013-10-09 15:24:03 -0400146 }
Alexandre Lision8fdcf2b2013-10-30 16:06:51 -0400147
Alexandre Lision5cd659e2013-10-29 13:18:23 -0400148 return result;
Alexandre Lision40954dc2013-10-09 15:24:03 -0400149
150 }
151
Alexandre Lisiond9c3f7f2013-10-30 15:20:55 -0400152 public void generateBitmap(int action) {
Alexandre Lision40954dc2013-10-09 15:24:03 -0400153
154 img = Bitmap.createBitmap(mWidth, mHeight, Config.ARGB_8888);
Alexandre Lision40954dc2013-10-09 15:24:03 -0400155 Canvas c = new Canvas(img);
Alexandre Lisiond5ee3962013-11-08 15:37:28 -0500156 c.drawRect(new RectF(0, 0, mWidth, mHeight), mBackgroundPaint);
Alexandre Lision68855472013-10-10 16:20:46 -0400157 float rHeight, rWidth;
Alexandre Lision40954dc2013-10-09 15:24:03 -0400158
Alexandre Lision68855472013-10-10 16:20:46 -0400159 switch (direction) {
160 case drawerPosition.TOP:
161 rHeight = bounds.height() - getRadius();
Alexandre Lision40954dc2013-10-09 15:24:03 -0400162 boundsHoldButton = new RectF(0, getRadius(), mWidth, getRadius() + rHeight / 4);
163 boundsMsgButton = new RectF(0, getRadius() + rHeight / 4, mWidth, getRadius() + 2 * rHeight / 4);
164 boundsTransferButton = new RectF(0, getRadius() + 2 * rHeight / 4, mWidth, getRadius() + 3 * rHeight / 4);
165 boundsHangUpButton = new RectF(0, getRadius() + 3 * rHeight / 4, mWidth, getRadius() + rHeight);
166
Alexandre Lision8fdcf2b2013-10-30 16:06:51 -0400167 calculateIconBounds();
168 draw(c, action);
Alexandre Lisiond5ee3962013-11-08 15:37:28 -0500169
170 c.drawLine(LINE_PADDING, boundsHoldButton.bottom, mWidth - LINE_PADDING, boundsHoldButton.bottom, mLines);
171 c.drawLine(LINE_PADDING, boundsMsgButton.bottom, mWidth - LINE_PADDING, boundsMsgButton.bottom, mLines);
172 c.drawLine(LINE_PADDING, boundsTransferButton.bottom, mWidth - LINE_PADDING, boundsTransferButton.bottom, mLines);
173
Alexandre Lision68855472013-10-10 16:20:46 -0400174 break;
175 case drawerPosition.BOTTOM:
176 rHeight = bounds.height() - getRadius();
Alexandre Lisiond5ee3962013-11-08 15:37:28 -0500177 boundsHangUpButton = new RectF(0, 0, mWidth, rHeight / 4);
178 boundsTransferButton = new RectF(0, rHeight / 4, mWidth, 2 * rHeight / 4);
179 boundsMsgButton = new RectF(0, 2 * rHeight / 4, mWidth, 3 * rHeight / 4);
180 boundsHoldButton = new RectF(0, 3 * rHeight / 4, mWidth, rHeight);
Alexandre Lision68855472013-10-10 16:20:46 -0400181
Alexandre Lision8fdcf2b2013-10-30 16:06:51 -0400182 calculateIconBounds();
183 draw(c, action);
Alexandre Lisiond5ee3962013-11-08 15:37:28 -0500184
185 c.drawLine(LINE_PADDING, boundsHangUpButton.bottom, mWidth - LINE_PADDING, boundsHangUpButton.bottom, mLines);
186 c.drawLine(LINE_PADDING, boundsTransferButton.bottom, mWidth - LINE_PADDING, boundsTransferButton.bottom, mLines);
187 c.drawLine(LINE_PADDING, boundsMsgButton.bottom, mWidth - LINE_PADDING,boundsMsgButton.bottom, mLines);
188
Alexandre Lision68855472013-10-10 16:20:46 -0400189 break;
190 case drawerPosition.RIGHT:
191 rWidth = bounds.width() - getRadius();
192 boundsHoldButton = new RectF(0, 0, rWidth / 4, mHeight);
193 boundsMsgButton = new RectF(rWidth / 4, 0, 2 * rWidth / 4, mHeight);
194 boundsTransferButton = new RectF(2 * rWidth / 4, 0, 3 * rWidth / 4, mHeight);
195 boundsHangUpButton = new RectF(3 * rWidth / 4, 0, rWidth, mHeight);
196
Alexandre Lision8fdcf2b2013-10-30 16:06:51 -0400197 calculateIconBounds();
198 draw(c, action);
Alexandre Lisiond5ee3962013-11-08 15:37:28 -0500199
200 c.drawLine(boundsHoldButton.right, LINE_PADDING, boundsHoldButton.right, mHeight - LINE_PADDING, mLines);
201 c.drawLine(boundsMsgButton.right, LINE_PADDING, boundsMsgButton.right, mHeight - LINE_PADDING, mLines);
202 c.drawLine(boundsTransferButton.right, LINE_PADDING, boundsTransferButton.right, mHeight - LINE_PADDING, mLines);
203
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();
Alexandre Lisiond5ee3962013-11-08 15:37:28 -0500208 boundsHangUpButton = new RectF(getRadius(), 0, getRadius() + rWidth / 4, mHeight);
209 boundsTransferButton = new RectF(getRadius() + rWidth / 4, 0, getRadius() + 2 * rWidth / 4, mHeight);
210 boundsMsgButton = new RectF(getRadius() + 2 * rWidth / 4, 0, getRadius() + 3 * rWidth / 4, mHeight);
211 boundsHoldButton = new RectF(getRadius() + 3 * rWidth / 4, 0, getRadius() + rWidth, mHeight);
Alexandre Lision68855472013-10-10 16:20:46 -0400212
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 Lisiond5ee3962013-11-08 15:37:28 -0500220
221
Alexandre Lision8fdcf2b2013-10-30 16:06:51 -0400222 private void draw(Canvas c, int action) {
223 if (action == actions.HANGUP) {
Alexandre Lisiond5ee3962013-11-08 15:37:28 -0500224 c.drawRect(boundsHangUpButton, mSelector);
Alexandre Lision8fdcf2b2013-10-30 16:06:51 -0400225 }
226 c.drawBitmap(buttonHangUp, null, boundsHangIcon, pButtons);
227
228 if (action == actions.HOLD) {
Alexandre Lisiond5ee3962013-11-08 15:37:28 -0500229 c.drawRect(boundsHoldButton, mSelector);
Alexandre Lision8fdcf2b2013-10-30 16:06:51 -0400230 }
231 if (associated_call.isOnHold()) {
232 c.drawBitmap(buttonUnhold, null, boundsHoldIcon, pButtons);
233 } else {
234 c.drawBitmap(buttonHold, null, boundsHoldIcon, pButtons);
235 }
236 if (action == actions.MESSAGE) {
Alexandre Lisiond5ee3962013-11-08 15:37:28 -0500237 c.drawRect(boundsMsgButton, mSelector);
Alexandre Lision8fdcf2b2013-10-30 16:06:51 -0400238 }
239 c.drawBitmap(buttonMsg, null, boundsMsgIcon, pButtons);
240
241 if (action == actions.TRANSFER) {
Alexandre Lisiond5ee3962013-11-08 15:37:28 -0500242 c.drawRect(boundsTransferButton, mSelector);
Alexandre Lision8fdcf2b2013-10-30 16:06:51 -0400243 }
244 c.drawBitmap(buttonTransfer, null, boundsTransferIcon, pButtons);
245 }
246
247 private void calculateIconBounds() {
248 boundsHoldIcon = new RectF((int) boundsHoldButton.centerX() - wHold / 2, (int) boundsHoldButton.centerY() - hHold / 2,
249 (int) boundsHoldButton.centerX() + wHold / 2, (int) boundsHoldButton.centerY() + hHold / 2);
250 boundsHangIcon = new RectF((int) boundsHangUpButton.centerX() - wHang / 2, (int) boundsHangUpButton.centerY() - hHang / 2,
251 (int) boundsHangUpButton.centerX() + wHang / 2, (int) boundsHangUpButton.centerY() + hHang / 2);
252 boundsMsgIcon = new RectF((int) boundsMsgButton.centerX() - wMsg / 2, (int) boundsMsgButton.centerY() - hMsg / 2,
253 (int) boundsMsgButton.centerX() + wMsg / 2, (int) boundsMsgButton.centerY() + hMsg / 2);
254 boundsTransferIcon = new RectF((int) boundsTransferButton.centerX() - wTrans / 2, (int) boundsTransferButton.centerY() - hTrans / 2,
255 (int) boundsTransferButton.centerX() + wTrans / 2, (int) boundsTransferButton.centerY() + hTrans / 2);
256 }
257
Alexandre Lision68855472013-10-10 16:20:46 -0400258 public void adjustBounds(float x, float y) {
259 switch (direction) {
260 case drawerPosition.TOP:
261 setBounds(x - getRadius(), y, x + getRadius(), y + getHeight());
262 break;
263 case drawerPosition.BOTTOM:
264 setBounds(x - getRadius(), y - getHeight(), x + getRadius(), y);
265 break;
266 case drawerPosition.RIGHT:
267 setBounds(x - getWidth(), y - getRadius(), x, y + +getRadius());
268 break;
269 case drawerPosition.LEFT:
270 setBounds(x, y - getRadius(), x + getWidth(), y + getRadius());
271 break;
272 }
273
274 }
275
276 @Override
277 public void setBounds(float left, float top, float right, float bottom) {
278 int margin = (int) (0.5f * getRadius()) / 2;
279 switch (direction) {
280 case drawerPosition.TOP:
281 case drawerPosition.BOTTOM:
282 super.setBounds(left + margin, top, right - margin, bottom);
283 break;
284 case drawerPosition.RIGHT:
285 case drawerPosition.LEFT:
286 super.setBounds(left, top + margin, right, bottom - margin);
287 break;
288 }
289
290 }
Alexandre Lision40954dc2013-10-09 15:24:03 -0400291 }
292
293 public Bitmap getDrawerBitmap() {
294 return act.getBitmap();
295 }
296
297 public RectF getDrawerBounds() {
Alexandre Lision5cd659e2013-10-29 13:18:23 -0400298 return act.getDrawerBounds();
Alexandre Lision40954dc2013-10-09 15:24:03 -0400299 }
300
301 @Override
302 public void set(float x, float y, float s) {
303 scale = s;
304 pos.x = x;
305 pos.y = y;
306 if (!expanded) {
307 bounds.set(pos.x - getRadius(), pos.y - getRadius(), pos.x + getRadius(), pos.y + getRadius());
308 } else {
309 bounds.set(pos.x - getRadius(), pos.y - getRadius(), pos.x + getRadius(), pos.y + getRadius());
Alexandre Lision68855472013-10-10 16:20:46 -0400310 act.adjustBounds(pos.x, pos.y);
Alexandre Lision40954dc2013-10-09 15:24:03 -0400311 }
312 }
313
314 @Override
315 public int getRadius() {
Alexandre Lision8fdcf2b2013-10-30 16:06:51 -0400316 if (expanded)
Alexandre Lisiona9485b82013-10-25 10:00:57 -0400317 return (int) (radius * density);
Alexandre Lision40954dc2013-10-09 15:24:03 -0400318 return (int) (radius * scale * density);
319 }
320
Alexandre Lision40954dc2013-10-09 15:24:03 -0400321 @Override
322 public boolean getHoldStatus() {
323 if (associated_call.isOnHold())
324 return true;
325 else
326 return false;
327 }
328
329 @Override
330 public boolean getRecordStatus() {
331 if (associated_call.isRecording())
332 return true;
333 else
334 return false;
335 }
336
Alexandre Lision40954dc2013-10-09 15:24:03 -0400337 public SipCall getCall() {
338 return associated_call;
339 }
340
Alexandre Lisionee2494d2013-10-09 17:14:00 -0400341 public void setCall(SipCall call) {
342 associated_call = call;
Alexandre Lision68855472013-10-10 16:20:46 -0400343 if (expanded) {
Alexandre Lisiond9c3f7f2013-10-30 15:20:55 -0400344 act.generateBitmap(actions.NOTHING);
Alexandre Lisionee2494d2013-10-09 17:14:00 -0400345 }
Alexandre Lision68855472013-10-10 16:20:46 -0400346
Alexandre Lisionee2494d2013-10-09 17:14:00 -0400347 }
348
349 @Override
Alexandre Lision68855472013-10-10 16:20:46 -0400350 public String getName() {
351 return associated_call.getContact().getmDisplayName();
352 }
353
354 @Override
355 public boolean callIDEquals(String call) {
356 return associated_call.getCallId().contentEquals(call);
357 }
358
359 @Override
360 public String getCallID() {
361 return associated_call.getCallId();
Alexandre Lisionee2494d2013-10-09 17:14:00 -0400362 }
Alexandre Lision8fdcf2b2013-10-30 16:06:51 -0400363
Alexandre Lisiond9c3f7f2013-10-30 15:20:55 -0400364 @Override
365 public boolean onDown(MotionEvent event) {
Alexandre Lision8fdcf2b2013-10-30 16:06:51 -0400366 if (expanded) {
367 act.generateBitmap(act.getAction(event.getX(), event.getY()));
368 return false;
369 }
370 if (intersects(event.getX(), event.getY())) {
Alexandre Lisiond9c3f7f2013-10-30 15:20:55 -0400371 dragged = true;
372 last_drag = System.nanoTime();
373 setPos(event.getX(), event.getY());
374 target_scale = .8f;
375 return true;
376 }
377 return false;
378 }
Alexandre Lisionee2494d2013-10-09 17:14:00 -0400379
Alexandre Lision40954dc2013-10-09 15:24:03 -0400380}