blob: 8945f4fe84ed38426b1cbf900f5055eb469dfb53 [file] [log] [blame]
alisionfde875f2013-05-28 17:01:54 -04001/*
2 * Copyright (C) 2004-2012 Savoir-Faire Linux Inc.
3 *
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
Adrien Béraud0c9bd8f2013-05-30 16:16:57 +100063 static final String TAG = "CallFragment";
alision84813a12013-05-27 17:40:39 -040064
Adrien Béraudc9c424d2013-05-30 17:47:35 +100065 static final float BUBBLE_SIZE = 75;
66 static final float ATTRACTOR_SIZE = 40;
alision84813a12013-05-27 17:40:39 -040067
Adrien Béraud0c9bd8f2013-05-30 16:16:57 +100068 private SipCall mCall;
alision84813a12013-05-27 17:40:39 -040069
Adrien Béraud13cde0b2013-05-30 20:27:20 +100070 private TextView callStatusTxt;
Adrien Béraud0c9bd8f2013-05-30 16:16:57 +100071 private BubblesView view;
72 private BubbleModel model;
alision84813a12013-05-27 17:40:39 -040073
Adrien Béraud0c9bd8f2013-05-30 16:16:57 +100074 private Callbacks mCallbacks = sDummyCallbacks;
alision84813a12013-05-27 17:40:39 -040075
Adrien Béraud0c9bd8f2013-05-30 16:16:57 +100076 private HashMap<CallContact, Bubble> contacts = new HashMap<CallContact, Bubble>();
alision85704182013-05-29 15:23:03 -040077
Adrien Béraud0c9bd8f2013-05-30 16:16:57 +100078 private CallContact myself = CallContact.ContactBuilder.buildUserContact("Me");
alision85704182013-05-29 15:23:03 -040079
Adrien Béraud0c9bd8f2013-05-30 16:16:57 +100080 private Bitmap hangup_icon;
81 private Bitmap call_icon;
alision85704182013-05-29 15:23:03 -040082
Adrien Béraud0c9bd8f2013-05-30 16:16:57 +100083 @Override
84 public void onCreate(Bundle savedBundle) {
85 super.onCreate(savedBundle);
86 model = new BubbleModel(getResources().getDisplayMetrics().density);
Adrien Béraud13cde0b2013-05-30 20:27:20 +100087 //metrics = getResources().getDisplayMetrics();
88 //screenCenter = new PointF(metrics.widthPixels / 2, metrics.heightPixels / 3);
alision84813a12013-05-27 17:40:39 -040089
Adrien Béraud0c9bd8f2013-05-30 16:16:57 +100090 Bundle b = getArguments();
alision84813a12013-05-27 17:40:39 -040091
Adrien Béraud0c9bd8f2013-05-30 16:16:57 +100092 mCall = b.getParcelable("CallInfo");
93 //mCall.
94 }
alision85992112013-05-29 12:18:08 -040095
Adrien Béraud0c9bd8f2013-05-30 16:16:57 +100096 /**
97 * A dummy implementation of the {@link Callbacks} interface that does nothing. Used only when this fragment is not attached to an activity.
98 */
99 private static Callbacks sDummyCallbacks = new Callbacks() {
100 @Override
101 public void onSendMessage(SipCall call, String msg) {
102 }
alision85992112013-05-29 12:18:08 -0400103
Adrien Béraud0c9bd8f2013-05-30 16:16:57 +1000104 @Override
105 public void callContact(SipCall call) {
106 }
alision85992112013-05-29 12:18:08 -0400107
Adrien Béraud0c9bd8f2013-05-30 16:16:57 +1000108 @Override
109 public void onCallAccepted(SipCall call) {
110 }
alision85704182013-05-29 15:23:03 -0400111
Adrien Béraud0c9bd8f2013-05-30 16:16:57 +1000112 @Override
113 public void onCallRejected(SipCall call) {
114 }
alision85992112013-05-29 12:18:08 -0400115
Adrien Béraud0c9bd8f2013-05-30 16:16:57 +1000116 @Override
117 public void onCallEnded(SipCall call) {
118 }
alision85704182013-05-29 15:23:03 -0400119
Adrien Béraud0c9bd8f2013-05-30 16:16:57 +1000120 @Override
121 public void onCallSuspended(SipCall call) {
122 }
alision85992112013-05-29 12:18:08 -0400123
Adrien Béraud0c9bd8f2013-05-30 16:16:57 +1000124 @Override
125 public void onCallResumed(SipCall call) {
126 }
alision85704182013-05-29 15:23:03 -0400127
Adrien Béraud0c9bd8f2013-05-30 16:16:57 +1000128 @Override
129 public void onCalltransfered(SipCall call, String to) {
130 }
alision85992112013-05-29 12:18:08 -0400131
Adrien Béraud0c9bd8f2013-05-30 16:16:57 +1000132 @Override
133 public void onRecordCall(SipCall call) {
134 }
alision85704182013-05-29 15:23:03 -0400135
Adrien Béraud0c9bd8f2013-05-30 16:16:57 +1000136 @Override
137 public ISipService getService() {
138 return null;
139 }
140 };
alision85992112013-05-29 12:18:08 -0400141
Adrien Béraud0c9bd8f2013-05-30 16:16:57 +1000142 /**
143 * The Activity calling this fragment has to implement this interface
144 *
145 */
146 public interface Callbacks {
alision85704182013-05-29 15:23:03 -0400147
Adrien Béraud0c9bd8f2013-05-30 16:16:57 +1000148 public ISipService getService();
alision85992112013-05-29 12:18:08 -0400149
Adrien Béraud0c9bd8f2013-05-30 16:16:57 +1000150 public void callContact(SipCall call);
alision85704182013-05-29 15:23:03 -0400151
Adrien Béraud0c9bd8f2013-05-30 16:16:57 +1000152 public void onCallAccepted(SipCall call);
alision85992112013-05-29 12:18:08 -0400153
Adrien Béraud0c9bd8f2013-05-30 16:16:57 +1000154 public void onCallRejected(SipCall call);
alision85704182013-05-29 15:23:03 -0400155
Adrien Béraud0c9bd8f2013-05-30 16:16:57 +1000156 public void onCallEnded(SipCall call);
alision85704182013-05-29 15:23:03 -0400157
Adrien Béraud0c9bd8f2013-05-30 16:16:57 +1000158 public void onCallSuspended(SipCall call);
alision84813a12013-05-27 17:40:39 -0400159
Adrien Béraud0c9bd8f2013-05-30 16:16:57 +1000160 public void onCallResumed(SipCall call);
alision84813a12013-05-27 17:40:39 -0400161
Adrien Béraud0c9bd8f2013-05-30 16:16:57 +1000162 public void onCalltransfered(SipCall call, String to);
alision85704182013-05-29 15:23:03 -0400163
Adrien Béraud0c9bd8f2013-05-30 16:16:57 +1000164 public void onRecordCall(SipCall call);
alision85992112013-05-29 12:18:08 -0400165
Adrien Béraud0c9bd8f2013-05-30 16:16:57 +1000166 public void onSendMessage(SipCall call, String msg);
167 }
alision85992112013-05-29 12:18:08 -0400168
Adrien Béraud0c9bd8f2013-05-30 16:16:57 +1000169 @Override
170 public void onAttach(Activity activity) {
171 super.onAttach(activity);
alision85992112013-05-29 12:18:08 -0400172
Adrien Béraud0c9bd8f2013-05-30 16:16:57 +1000173 if (!(activity instanceof Callbacks)) {
174 throw new IllegalStateException("Activity must implement fragment's callbacks.");
175 }
alision85992112013-05-29 12:18:08 -0400176
Adrien Béraudc9c424d2013-05-30 17:47:35 +1000177 //rootView.requestDisallowInterceptTouchEvent(true);
alision85992112013-05-29 12:18:08 -0400178
Adrien Béraud0c9bd8f2013-05-30 16:16:57 +1000179 mCallbacks = (Callbacks) activity;
180 }
alision85992112013-05-29 12:18:08 -0400181
Adrien Béraud0c9bd8f2013-05-30 16:16:57 +1000182 @Override
183 public void onDetach() {
184 super.onDetach();
185 mCallbacks = sDummyCallbacks;
Adrien Béraudc9c424d2013-05-30 17:47:35 +1000186 //rootView.requestDisallowInterceptTouchEvent(false);
Adrien Béraud0c9bd8f2013-05-30 16:16:57 +1000187 }
alision85992112013-05-29 12:18:08 -0400188
Adrien Béraud0c9bd8f2013-05-30 16:16:57 +1000189 @Override
190 public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
Adrien Béraud13cde0b2013-05-30 20:27:20 +1000191 ViewGroup rootView = (ViewGroup) inflater.inflate(R.layout.frag_call, container, false);
alision85992112013-05-29 12:18:08 -0400192
Adrien Béraud0c9bd8f2013-05-30 16:16:57 +1000193 view = (BubblesView) rootView.findViewById(R.id.main_view);
194 view.setModel(model);
Adrien Béraud13cde0b2013-05-30 20:27:20 +1000195 view.getHolder().addCallback(this);
alision84813a12013-05-27 17:40:39 -0400196
Adrien Béraud13cde0b2013-05-30 20:27:20 +1000197 callStatusTxt = (TextView) rootView.findViewById(R.id.call_status_txt);
alision84813a12013-05-27 17:40:39 -0400198
Adrien Béraud0c9bd8f2013-05-30 16:16:57 +1000199 hangup_icon = BitmapFactory.decodeResource(getResources(), R.drawable.ic_hangup);
200 call_icon = BitmapFactory.decodeResource(getResources(), R.drawable.ic_call);
alision84813a12013-05-27 17:40:39 -0400201
Adrien Béraud13cde0b2013-05-30 20:27:20 +1000202 // Do nothing here, the view is not initialized yet.
Adrien Béraud0c9bd8f2013-05-30 16:16:57 +1000203 return rootView;
204 }
alision85992112013-05-29 12:18:08 -0400205
Adrien Béraud0c9bd8f2013-05-30 16:16:57 +1000206 private void initNormalStateDisplay() {
207 Log.i(TAG, "Start normal display");
alision84813a12013-05-27 17:40:39 -0400208
Adrien Béraud13cde0b2013-05-30 20:27:20 +1000209 callStatusTxt.setText("0 min");
alision84813a12013-05-27 17:40:39 -0400210
Adrien Béraud13cde0b2013-05-30 20:27:20 +1000211 getBubbleFor(mCall.getContacts().get(0), model.width/2, model.height/2);
212 getBubbleFor(myself, model.width/2, model.height/3);
alision84813a12013-05-27 17:40:39 -0400213
Adrien Béraud0c9bd8f2013-05-30 16:16:57 +1000214 model.clearAttractors();
Adrien Béraud13cde0b2013-05-30 20:27:20 +1000215 model.addAttractor(new Attractor(new PointF(model.width / 2, model.height * .8f), ATTRACTOR_SIZE, new Attractor.Callback() {
Adrien Béraud0c9bd8f2013-05-30 16:16:57 +1000216 @Override
Adrien Béraudc9c424d2013-05-30 17:47:35 +1000217 public boolean onBubbleSucked(Bubble b) {
Adrien Béraud0c9bd8f2013-05-30 16:16:57 +1000218 Log.w(TAG, "Bubble sucked ! ");
219 mCallbacks.onCallEnded(mCall);
Adrien Béraudc9c424d2013-05-30 17:47:35 +1000220 bubbleRemoved(b);
221 return true;
Adrien Béraud0c9bd8f2013-05-30 16:16:57 +1000222 }
223 }, hangup_icon));
alision84813a12013-05-27 17:40:39 -0400224
Adrien Béraud0c9bd8f2013-05-30 16:16:57 +1000225 }
alision85704182013-05-29 15:23:03 -0400226
Adrien Béraud0c9bd8f2013-05-30 16:16:57 +1000227 private void initIncomingCallDisplay() {
228 Log.i(TAG, "Start incoming display");
alision84813a12013-05-27 17:40:39 -0400229
Adrien Béraud13cde0b2013-05-30 20:27:20 +1000230 callStatusTxt.setText("Incomming call");
alision84813a12013-05-27 17:40:39 -0400231
Adrien Béraud13cde0b2013-05-30 20:27:20 +1000232 Bubble contact_bubble = getBubbleFor(mCall.getContacts().get(0), model.width/2, model.height/2);
Adrien Béraud0c9bd8f2013-05-30 16:16:57 +1000233 contacts.put(mCall.getContacts().get(0), contact_bubble);
alision85704182013-05-29 15:23:03 -0400234
Adrien Béraud0c9bd8f2013-05-30 16:16:57 +1000235 model.clearAttractors();
Adrien Béraud13cde0b2013-05-30 20:27:20 +1000236 model.addAttractor(new Attractor(new PointF(4 * model.width / 5, model.height / 2), ATTRACTOR_SIZE, new Attractor.Callback() {
Adrien Béraud0c9bd8f2013-05-30 16:16:57 +1000237 @Override
Adrien Béraudc9c424d2013-05-30 17:47:35 +1000238 public boolean onBubbleSucked(Bubble b) {
Adrien Béraud0c9bd8f2013-05-30 16:16:57 +1000239 mCallbacks.onCallAccepted(mCall);
Adrien Béraudc9c424d2013-05-30 17:47:35 +1000240 return false;
Adrien Béraud0c9bd8f2013-05-30 16:16:57 +1000241 }
242 }, call_icon));
Adrien Béraud13cde0b2013-05-30 20:27:20 +1000243 model.addAttractor(new Attractor(new PointF(model.width / 5, model.height / 2), ATTRACTOR_SIZE, new Attractor.Callback() {
Adrien Béraud0c9bd8f2013-05-30 16:16:57 +1000244 @Override
Adrien Béraudc9c424d2013-05-30 17:47:35 +1000245 public boolean onBubbleSucked(Bubble b) {
Adrien Béraud0c9bd8f2013-05-30 16:16:57 +1000246 mCallbacks.onCallRejected(mCall);
Adrien Béraudc9c424d2013-05-30 17:47:35 +1000247 bubbleRemoved(b);
248 return true;
Adrien Béraud0c9bd8f2013-05-30 16:16:57 +1000249 }
250 }, hangup_icon));
251 }
alision85704182013-05-29 15:23:03 -0400252
Adrien Béraud0c9bd8f2013-05-30 16:16:57 +1000253 private void initOutGoingCallDisplay() {
254 Log.i(TAG, "Start outgoing display");
alision85704182013-05-29 15:23:03 -0400255
Adrien Béraud13cde0b2013-05-30 20:27:20 +1000256 callStatusTxt.setText("Calling...");
alision85704182013-05-29 15:23:03 -0400257
Adrien Béraud0c9bd8f2013-05-30 16:16:57 +1000258 // TODO off-thread image loading
Adrien Béraud13cde0b2013-05-30 20:27:20 +1000259 getBubbleFor(mCall.getContacts().get(0), model.width/2, model.height/2);
alision85704182013-05-29 15:23:03 -0400260
Adrien Béraud0c9bd8f2013-05-30 16:16:57 +1000261 model.clearAttractors();
Adrien Béraud13cde0b2013-05-30 20:27:20 +1000262 model.addAttractor(new Attractor(new PointF(model.width / 2, model.height * .8f), 40, new Attractor.Callback() {
Adrien Béraud0c9bd8f2013-05-30 16:16:57 +1000263 @Override
Adrien Béraudc9c424d2013-05-30 17:47:35 +1000264 public boolean onBubbleSucked(Bubble b) {
Adrien Béraud0c9bd8f2013-05-30 16:16:57 +1000265 Log.w(TAG, "Bubble sucked ! ");
266 mCallbacks.onCallEnded(mCall);
Adrien Béraudc9c424d2013-05-30 17:47:35 +1000267 bubbleRemoved(b);
268 return true;
Adrien Béraud0c9bd8f2013-05-30 16:16:57 +1000269 }
270 }, hangup_icon));
Adrien Béraud0c9bd8f2013-05-30 16:16:57 +1000271 }
alision85704182013-05-29 15:23:03 -0400272
Adrien Béraud0c9bd8f2013-05-30 16:16:57 +1000273 /**
274 * Retrieves or create a bubble for a given contact.
275 * If the bubble exists, it is moved to the new location.
276 *
277 * @param contact The contact
278 * @param x Initial or new x position.
279 * @param y Initial or new y position.
280 * @return Bubble corresponding to the contact.
281 */
282 private Bubble getBubbleFor(CallContact contact, float x, float y) {
283 Bubble contact_bubble = contacts.get(contact);
284 if(contact_bubble != null) {
285 contact_bubble.attractor.set(x, y);
286 return contact_bubble;
287 }
alision84813a12013-05-27 17:40:39 -0400288
Adrien Béraudc9c424d2013-05-30 17:47:35 +1000289 // TODO off-thread image loading
Adrien Béraud0c9bd8f2013-05-30 16:16:57 +1000290 if (contact.getPhoto_id() > 0) {
291 Bitmap photo = ContactPictureLoader.loadContactPhoto(getActivity().getContentResolver(), mCall.getContacts().get(0).getId());
292 contact_bubble = new Bubble(x, y, BUBBLE_SIZE, photo);
293 } else {
294 contact_bubble = new Bubble(x, y, BUBBLE_SIZE, getActivity(), R.drawable.ic_contact_picture);
295 }
296 contact_bubble.contact = contact;
alision84813a12013-05-27 17:40:39 -0400297
Adrien Béraud0c9bd8f2013-05-30 16:16:57 +1000298 model.addBubble(contact_bubble);
299 contacts.put(contact, contact_bubble);
alision84813a12013-05-27 17:40:39 -0400300
Adrien Béraud0c9bd8f2013-05-30 16:16:57 +1000301 return contact_bubble;
302 }
303
Adrien Béraudc9c424d2013-05-30 17:47:35 +1000304 /**
305 * Should be called when a bubble is removed from the model
306 */
307 void bubbleRemoved(Bubble b) {
308 if(b.contact == null) {
309 return;
310 }
311
312 contacts.remove(b.contact);
313 }
314
Adrien Béraud0c9bd8f2013-05-30 16:16:57 +1000315 public void changeCallState(String callID, String newState) {
316
317 Log.w(TAG, "Changing call state of "+callID);
318 mCall.printCallInfo();
319 if(!callID.equals(mCall.getCallId()))
320 return;
321
322 mCall.setCallState(newState);
323 if(mCall.isOngoing()){
324 initNormalStateDisplay();
325 }
326 }
alision84813a12013-05-27 17:40:39 -0400327
Adrien Béraudc9c424d2013-05-30 17:47:35 +1000328 public boolean draggingBubble()
329 {
330 return view == null ? false : view.isDraggingBubble();
331 }
332
Adrien Béraud13cde0b2013-05-30 20:27:20 +1000333 @Override
334 public void surfaceChanged(SurfaceHolder holder, int format, int width, int height)
335 {
336 Log.i(TAG, "Init fragment " + mCall.getCallId());
337
338 mCall.printCallInfo();
339
340 if (mCall.isIncoming() && mCall.isRinging()) {
341 initIncomingCallDisplay();
342 } else {
343 if (mCall.isRinging()) {
344 initOutGoingCallDisplay();
345 }
346 try {
347 if (mCall.isOutGoing() && mCallbacks.getService().getCall(mCall.getCallId()) == null) {
348 mCallbacks.getService().placeCall(mCall);
349 initOutGoingCallDisplay();
350 } else if(mCall.isOutGoing() && mCall.isRinging()){
351 initOutGoingCallDisplay();
352 }
353 } catch (RemoteException e) {
354 Log.e(TAG, e.toString());
355 }
356 }
357
358 if(mCall.isOngoing()){
359 initNormalStateDisplay();
360 }
361
362 }
363
364 @Override
365 public void surfaceCreated(SurfaceHolder holder)
366 {
367 }
368
369 @Override
370 public void surfaceDestroyed(SurfaceHolder holder)
371 {
372 }
373
alision84813a12013-05-27 17:40:39 -0400374
alision84813a12013-05-27 17:40:39 -0400375}