blob: f454d99958e9d3d18a797e499a7a58db75c3e8d7 [file] [log] [blame]
alisionfde875f2013-05-28 17:01:54 -04001/*
alision2ec64f92013-06-17 17:28:58 -04002 * Copyright (C) 2004-2013 Savoir-Faire Linux Inc.
alisionfde875f2013-05-28 17:01:54 -04003 *
4 * Author: Alexandre Lision <alexandre.lision@savoirfairelinux.com>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 3 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19 *
20 * Additional permission under GNU GPL version 3 section 7:
21 *
22 * If you modify this program, or any covered work, by linking or
23 * combining it with the OpenSSL project's OpenSSL library (or a
24 * modified version of that library), containing parts covered by the
25 * terms of the OpenSSL or SSLeay licenses, Savoir-Faire Linux Inc.
26 * grants you additional permission to convey the resulting work.
27 * Corresponding Source for a non-source form of such a combination
28 * shall include the source code for the parts of OpenSSL used as well
29 * as that of the covered work.
30 */
31
alision84813a12013-05-27 17:40:39 -040032package com.savoirfairelinux.sflphone.fragments;
33
34import java.util.HashMap;
35
36import android.app.Activity;
37import android.app.Fragment;
38import android.graphics.Bitmap;
Adrien Béraud0c9bd8f2013-05-30 16:16:57 +100039import android.graphics.BitmapFactory;
alision84813a12013-05-27 17:40:39 -040040import android.graphics.PointF;
41import android.os.Bundle;
42import android.os.RemoteException;
alision84813a12013-05-27 17:40:39 -040043import android.util.Log;
44import android.view.LayoutInflater;
Adrien Béraud13cde0b2013-05-30 20:27:20 +100045import android.view.SurfaceHolder;
46import android.view.SurfaceHolder.Callback;
alision84813a12013-05-27 17:40:39 -040047import android.view.View;
48import android.view.ViewGroup;
49import android.widget.TextView;
50
51import com.savoirfairelinux.sflphone.R;
52import com.savoirfairelinux.sflphone.adapters.ContactPictureLoader;
53import com.savoirfairelinux.sflphone.model.Attractor;
54import com.savoirfairelinux.sflphone.model.Bubble;
55import com.savoirfairelinux.sflphone.model.BubbleModel;
56import com.savoirfairelinux.sflphone.model.BubblesView;
57import com.savoirfairelinux.sflphone.model.CallContact;
58import com.savoirfairelinux.sflphone.model.SipCall;
alision84813a12013-05-27 17:40:39 -040059import com.savoirfairelinux.sflphone.service.ISipService;
60
Adrien Béraud13cde0b2013-05-30 20:27:20 +100061public class CallFragment extends Fragment implements Callback {
alision84813a12013-05-27 17:40:39 -040062
alision1005ba12013-06-19 13:52:44 -040063 static final String TAG = "CallFragment";
alision84813a12013-05-27 17:40:39 -040064
alision1005ba12013-06-19 13:52:44 -040065 static final float BUBBLE_SIZE = 75;
66 static final float ATTRACTOR_SIZE = 40;
alision84813a12013-05-27 17:40:39 -040067
alision1005ba12013-06-19 13:52:44 -040068 private SipCall mCall;
alision84813a12013-05-27 17:40:39 -040069
alision1005ba12013-06-19 13:52:44 -040070 private TextView callStatusTxt;
71 private BubblesView view;
72 private BubbleModel model;
alision84813a12013-05-27 17:40:39 -040073
alision1005ba12013-06-19 13:52:44 -040074 private Callbacks mCallbacks = sDummyCallbacks;
alision84813a12013-05-27 17:40:39 -040075
alision1005ba12013-06-19 13:52:44 -040076 private HashMap<CallContact, Bubble> contacts = new HashMap<CallContact, Bubble>();
alision85704182013-05-29 15:23:03 -040077
alision1005ba12013-06-19 13:52:44 -040078 private CallContact myself;
alision85704182013-05-29 15:23:03 -040079
alision1005ba12013-06-19 13:52:44 -040080 private Bitmap hangup_icon;
81 private Bitmap call_icon;
alision85704182013-05-29 15:23:03 -040082
alision1005ba12013-06-19 13:52:44 -040083 @Override
84 public void onCreate(Bundle savedBundle) {
85 super.onCreate(savedBundle);
86 model = new BubbleModel(getResources().getDisplayMetrics().density);
87 Bundle b = getArguments();
alision84813a12013-05-27 17:40:39 -040088
alision1005ba12013-06-19 13:52:44 -040089 mCall = b.getParcelable("CallInfo");
alision84813a12013-05-27 17:40:39 -040090
alision1005ba12013-06-19 13:52:44 -040091 }
alision85992112013-05-29 12:18:08 -040092
alision1005ba12013-06-19 13:52:44 -040093 /**
94 * A dummy implementation of the {@link Callbacks} interface that does nothing. Used only when this fragment is not attached to an activity.
95 */
96 private static Callbacks sDummyCallbacks = new Callbacks() {
97 @Override
98 public void onSendMessage(SipCall call, String msg) {
99 }
alision85992112013-05-29 12:18:08 -0400100
alision1005ba12013-06-19 13:52:44 -0400101 @Override
102 public void callContact(SipCall call) {
103 }
alision85992112013-05-29 12:18:08 -0400104
alision1005ba12013-06-19 13:52:44 -0400105 @Override
106 public void onCallAccepted(SipCall call) {
107 }
alision85704182013-05-29 15:23:03 -0400108
alision1005ba12013-06-19 13:52:44 -0400109 @Override
110 public void onCallRejected(SipCall call) {
111 }
alision85992112013-05-29 12:18:08 -0400112
alision1005ba12013-06-19 13:52:44 -0400113 @Override
114 public void onCallEnded(SipCall call) {
115 }
alision85704182013-05-29 15:23:03 -0400116
alision1005ba12013-06-19 13:52:44 -0400117 @Override
118 public void onCallSuspended(SipCall call) {
119 }
alision85992112013-05-29 12:18:08 -0400120
alision1005ba12013-06-19 13:52:44 -0400121 @Override
122 public void onCallResumed(SipCall call) {
123 }
alision85704182013-05-29 15:23:03 -0400124
alision1005ba12013-06-19 13:52:44 -0400125 @Override
126 public void onCalltransfered(SipCall call, String to) {
127 }
alision85992112013-05-29 12:18:08 -0400128
alision1005ba12013-06-19 13:52:44 -0400129 @Override
130 public void onRecordCall(SipCall call) {
131 }
alision85704182013-05-29 15:23:03 -0400132
alision1005ba12013-06-19 13:52:44 -0400133 @Override
134 public ISipService getService() {
135 return null;
136 }
137 };
alision907bde72013-06-20 14:40:37 -0400138
alision1005ba12013-06-19 13:52:44 -0400139 /**
140 * The Activity calling this fragment has to implement this interface
141 *
142 */
143 public interface Callbacks {
alision85992112013-05-29 12:18:08 -0400144
alision1005ba12013-06-19 13:52:44 -0400145 public ISipService getService();
alision85704182013-05-29 15:23:03 -0400146
alision1005ba12013-06-19 13:52:44 -0400147 public void callContact(SipCall call);
alision85992112013-05-29 12:18:08 -0400148
alision1005ba12013-06-19 13:52:44 -0400149 public void onCallAccepted(SipCall call);
alision85704182013-05-29 15:23:03 -0400150
alision1005ba12013-06-19 13:52:44 -0400151 public void onCallRejected(SipCall call);
alision85992112013-05-29 12:18:08 -0400152
alision1005ba12013-06-19 13:52:44 -0400153 public void onCallEnded(SipCall call);
alision85704182013-05-29 15:23:03 -0400154
alision1005ba12013-06-19 13:52:44 -0400155 public void onCallSuspended(SipCall call);
alision85704182013-05-29 15:23:03 -0400156
alision1005ba12013-06-19 13:52:44 -0400157 public void onCallResumed(SipCall call);
alision84813a12013-05-27 17:40:39 -0400158
alision1005ba12013-06-19 13:52:44 -0400159 public void onCalltransfered(SipCall call, String to);
alision84813a12013-05-27 17:40:39 -0400160
alision1005ba12013-06-19 13:52:44 -0400161 public void onRecordCall(SipCall call);
alision85704182013-05-29 15:23:03 -0400162
alision1005ba12013-06-19 13:52:44 -0400163 public void onSendMessage(SipCall call, String msg);
164 }
alision85992112013-05-29 12:18:08 -0400165
alision1005ba12013-06-19 13:52:44 -0400166 @Override
167 public void onAttach(Activity activity) {
168 super.onAttach(activity);
alision85992112013-05-29 12:18:08 -0400169
alision1005ba12013-06-19 13:52:44 -0400170 if (!(activity instanceof Callbacks)) {
171 throw new IllegalStateException("Activity must implement fragment's callbacks.");
172 }
alision85992112013-05-29 12:18:08 -0400173
alision1005ba12013-06-19 13:52:44 -0400174 // rootView.requestDisallowInterceptTouchEvent(true);
alision85992112013-05-29 12:18:08 -0400175
alision1005ba12013-06-19 13:52:44 -0400176 mCallbacks = (Callbacks) activity;
177 myself = CallContact.ContactBuilder.buildUserContact(activity.getContentResolver(), "");
alision85992112013-05-29 12:18:08 -0400178
alision1005ba12013-06-19 13:52:44 -0400179 }
alision85992112013-05-29 12:18:08 -0400180
alision1005ba12013-06-19 13:52:44 -0400181 @Override
182 public void onDetach() {
183 super.onDetach();
184 mCallbacks = sDummyCallbacks;
185 // rootView.requestDisallowInterceptTouchEvent(false);
186 }
alision907bde72013-06-20 14:40:37 -0400187
alision1005ba12013-06-19 13:52:44 -0400188 @Override
alision907bde72013-06-20 14:40:37 -0400189 public void onStop() {
alision1005ba12013-06-19 13:52:44 -0400190 super.onStop();
191 }
alision85992112013-05-29 12:18:08 -0400192
alision1005ba12013-06-19 13:52:44 -0400193 @Override
194 public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
195 ViewGroup rootView = (ViewGroup) inflater.inflate(R.layout.frag_call, container, false);
alision85992112013-05-29 12:18:08 -0400196
alision1005ba12013-06-19 13:52:44 -0400197 view = (BubblesView) rootView.findViewById(R.id.main_view);
198 view.setModel(model);
199 view.getHolder().addCallback(this);
alision84813a12013-05-27 17:40:39 -0400200
alision1005ba12013-06-19 13:52:44 -0400201 callStatusTxt = (TextView) rootView.findViewById(R.id.call_status_txt);
alision84813a12013-05-27 17:40:39 -0400202
alision1005ba12013-06-19 13:52:44 -0400203 hangup_icon = BitmapFactory.decodeResource(getResources(), R.drawable.ic_hangup);
204 call_icon = BitmapFactory.decodeResource(getResources(), R.drawable.ic_call);
alision84813a12013-05-27 17:40:39 -0400205
alision1005ba12013-06-19 13:52:44 -0400206 // Do nothing here, the view is not initialized yet.
207 return rootView;
208 }
alision85992112013-05-29 12:18:08 -0400209
alision1005ba12013-06-19 13:52:44 -0400210 private void initNormalStateDisplay() {
211 Log.i(TAG, "Start normal display");
alision84813a12013-05-27 17:40:39 -0400212
alision1005ba12013-06-19 13:52:44 -0400213 callStatusTxt.setText("0 min");
alision84813a12013-05-27 17:40:39 -0400214
alision907bde72013-06-20 14:40:37 -0400215
alision84813a12013-05-27 17:40:39 -0400216
alision1005ba12013-06-19 13:52:44 -0400217 getBubbleFor(myself, model.width / 2, model.height / 2);
alision907bde72013-06-20 14:40:37 -0400218
219 getBubbleFor(mCall.getContact(), (int) (model.width / 2), (int) (model.height / 3));
220
alision1005ba12013-06-19 13:52:44 -0400221
222 model.clearAttractors();
223 model.addAttractor(new Attractor(new PointF(model.width / 2, model.height * .8f), ATTRACTOR_SIZE, new Attractor.Callback() {
224 @Override
225 public boolean onBubbleSucked(Bubble b) {
226 Log.w(TAG, "Bubble sucked ! ");
227 mCallbacks.onCallEnded(mCall);
228 bubbleRemoved(b);
229 return true;
230 }
231 }, hangup_icon));
alision84813a12013-05-27 17:40:39 -0400232
alision1005ba12013-06-19 13:52:44 -0400233 }
alision85704182013-05-29 15:23:03 -0400234
alision1005ba12013-06-19 13:52:44 -0400235 private void initIncomingCallDisplay() {
236 Log.i(TAG, "Start incoming display");
alision84813a12013-05-27 17:40:39 -0400237
alision1005ba12013-06-19 13:52:44 -0400238 callStatusTxt.setText("Incomming call");
alision84813a12013-05-27 17:40:39 -0400239
alision907bde72013-06-20 14:40:37 -0400240 Bubble contact_bubble = getBubbleFor(mCall.getContact(), model.width / 2, model.height / 2);
241 contacts.put(mCall.getContact(), contact_bubble);
alision85704182013-05-29 15:23:03 -0400242
alision1005ba12013-06-19 13:52:44 -0400243 model.clearAttractors();
244 model.addAttractor(new Attractor(new PointF(4 * model.width / 5, model.height / 2), ATTRACTOR_SIZE, new Attractor.Callback() {
245 @Override
246 public boolean onBubbleSucked(Bubble b) {
247 mCallbacks.onCallAccepted(mCall);
248 return false;
249 }
250 }, call_icon));
251 model.addAttractor(new Attractor(new PointF(model.width / 5, model.height / 2), ATTRACTOR_SIZE, new Attractor.Callback() {
252 @Override
253 public boolean onBubbleSucked(Bubble b) {
254 mCallbacks.onCallRejected(mCall);
255 bubbleRemoved(b);
256 return true;
257 }
258 }, hangup_icon));
259 }
alision85704182013-05-29 15:23:03 -0400260
alision1005ba12013-06-19 13:52:44 -0400261 private void initOutGoingCallDisplay() {
262 Log.i(TAG, "Start outgoing display");
alision85704182013-05-29 15:23:03 -0400263
alision1005ba12013-06-19 13:52:44 -0400264 callStatusTxt.setText("Calling...");
alision85704182013-05-29 15:23:03 -0400265
alision1005ba12013-06-19 13:52:44 -0400266 // TODO off-thread image loading
alision1005ba12013-06-19 13:52:44 -0400267 getBubbleFor(myself, model.width / 2, model.height / 2);
alision85704182013-05-29 15:23:03 -0400268
alision907bde72013-06-20 14:40:37 -0400269 getBubbleFor(mCall.getContact(), (int) (model.width / 2), (int) (model.height / 3 ));
270
alision1005ba12013-06-19 13:52:44 -0400271 model.clearAttractors();
272 model.addAttractor(new Attractor(new PointF(model.width / 2, model.height * .8f), 40, new Attractor.Callback() {
273 @Override
274 public boolean onBubbleSucked(Bubble b) {
275 Log.w(TAG, "Bubble sucked ! ");
276 mCallbacks.onCallEnded(mCall);
277 bubbleRemoved(b);
278 return true;
279 }
280 }, hangup_icon));
281 }
alision85704182013-05-29 15:23:03 -0400282
alision1005ba12013-06-19 13:52:44 -0400283 /**
284 * Retrieves or create a bubble for a given contact. If the bubble exists, it is moved to the new location.
285 *
286 * @param contact
287 * The contact
288 * @param x
289 * Initial or new x position.
290 * @param y
291 * Initial or new y position.
292 * @return Bubble corresponding to the contact.
293 */
294 private Bubble getBubbleFor(CallContact contact, float x, float y) {
295 Bubble contact_bubble = contacts.get(contact);
296 if (contact_bubble != null) {
297 contact_bubble.attractor.set(x, y);
298 return contact_bubble;
299 }
alision84813a12013-05-27 17:40:39 -0400300
alision1005ba12013-06-19 13:52:44 -0400301 // TODO off-thread image loading
302 if (contact.getPhoto_id() > 0) {
303 Bitmap photo = ContactPictureLoader.loadContactPhoto(getActivity().getContentResolver(), contact.getId());
304 contact_bubble = new Bubble(x, y, BUBBLE_SIZE, photo);
305 } else {
306 contact_bubble = new Bubble(x, y, BUBBLE_SIZE, getActivity(), R.drawable.ic_contact_picture);
307 }
308 contact_bubble.contact = contact;
alision84813a12013-05-27 17:40:39 -0400309
alision1005ba12013-06-19 13:52:44 -0400310 model.addBubble(contact_bubble);
311 contacts.put(contact, contact_bubble);
alision84813a12013-05-27 17:40:39 -0400312
alision1005ba12013-06-19 13:52:44 -0400313 return contact_bubble;
314 }
Adrien Béraud0c9bd8f2013-05-30 16:16:57 +1000315
alision1005ba12013-06-19 13:52:44 -0400316 /**
317 * Should be called when a bubble is removed from the model
318 */
319 void bubbleRemoved(Bubble b) {
320 if (b.contact == null) {
321 return;
322 }
Adrien Béraudc9c424d2013-05-30 17:47:35 +1000323
alision1005ba12013-06-19 13:52:44 -0400324 contacts.remove(b.contact);
325 }
Adrien Béraudc9c424d2013-05-30 17:47:35 +1000326
alision1005ba12013-06-19 13:52:44 -0400327 public void changeCallState(String callID, String newState) {
Adrien Béraud0c9bd8f2013-05-30 16:16:57 +1000328
alision1005ba12013-06-19 13:52:44 -0400329 Log.w(TAG, "Changing call state of " + callID);
330 mCall.printCallInfo();
331 if (!callID.equals(mCall.getCallId()))
332 return;
Adrien Béraud0c9bd8f2013-05-30 16:16:57 +1000333
alision1005ba12013-06-19 13:52:44 -0400334 mCall.setCallState(newState);
335 if (mCall.isOngoing()) {
336 initNormalStateDisplay();
337 }
338 }
alision84813a12013-05-27 17:40:39 -0400339
alision1005ba12013-06-19 13:52:44 -0400340 public boolean draggingBubble() {
341 return view == null ? false : view.isDraggingBubble();
342 }
Adrien Béraudc9c424d2013-05-30 17:47:35 +1000343
alision1005ba12013-06-19 13:52:44 -0400344 @Override
345 public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
346 Log.i(TAG, "Init fragment " + mCall.getCallId());
Adrien Béraud13cde0b2013-05-30 20:27:20 +1000347
alision1005ba12013-06-19 13:52:44 -0400348 mCall.printCallInfo();
Adrien Béraud13cde0b2013-05-30 20:27:20 +1000349
alision1005ba12013-06-19 13:52:44 -0400350 if (mCall.isIncoming() && mCall.isRinging()) {
351 initIncomingCallDisplay();
352 } else {
353 if (mCall.isRinging()) {
354 initOutGoingCallDisplay();
355 }
356 try {
357 if (mCall.isOutGoing() && mCallbacks.getService().getCall(mCall.getCallId()) == null) {
358 mCallbacks.getService().placeCall(mCall);
359 initOutGoingCallDisplay();
360 } else if (mCall.isOutGoing() && mCall.isRinging()) {
361 initOutGoingCallDisplay();
362 }
363 } catch (RemoteException e) {
364 Log.e(TAG, e.toString());
365 }
366 }
Adrien Béraud13cde0b2013-05-30 20:27:20 +1000367
alision1005ba12013-06-19 13:52:44 -0400368 if (mCall.isOngoing()) {
369 initNormalStateDisplay();
370 }
Adrien Béraud13cde0b2013-05-30 20:27:20 +1000371
alision1005ba12013-06-19 13:52:44 -0400372 }
Adrien Béraud13cde0b2013-05-30 20:27:20 +1000373
alision1005ba12013-06-19 13:52:44 -0400374 @Override
375 public void surfaceCreated(SurfaceHolder holder) {
376 }
Adrien Béraud13cde0b2013-05-30 20:27:20 +1000377
alision1005ba12013-06-19 13:52:44 -0400378 @Override
379 public void surfaceDestroyed(SurfaceHolder holder) {
380 }
Adrien Béraud13cde0b2013-05-30 20:27:20 +1000381
alisione38001f2013-06-04 14:14:39 -0400382 public BubblesView getBubbleView() {
383 return view;
alisione38001f2013-06-04 14:14:39 -0400384
alision1005ba12013-06-19 13:52:44 -0400385 }
alision84813a12013-05-27 17:40:39 -0400386
alision84813a12013-05-27 17:40:39 -0400387}