blob: 90342d547d72745a112a0efef35006eb6f387f6a [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 };
138
139 /**
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 }
187
188 @Override
189 public void onStop(){
190 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
alision1005ba12013-06-19 13:52:44 -0400215 int radiusCalls = model.width / 2 - 150;
216 int angle_part = 360 / mCall.getContacts().size();
217 double dX = 0;
218 double dY = 0;
alision84813a12013-05-27 17:40:39 -0400219
alision1005ba12013-06-19 13:52:44 -0400220 getBubbleFor(myself, model.width / 2, model.height / 2);
221 for (int i = 0; i < mCall.getContacts().size(); ++i) {
222 dX = Math.cos(Math.toRadians(angle_part * i)) * radiusCalls;
223 dY = Math.sin(Math.toRadians(angle_part * i)) * radiusCalls;
224
225 getBubbleFor(mCall.getContacts().get(i),(int) (model.width / 2 + dX), (int) (model.height / 2 + dY));
226 }
227
228 model.clearAttractors();
229 model.addAttractor(new Attractor(new PointF(model.width / 2, model.height * .8f), ATTRACTOR_SIZE, new Attractor.Callback() {
230 @Override
231 public boolean onBubbleSucked(Bubble b) {
232 Log.w(TAG, "Bubble sucked ! ");
233 mCallbacks.onCallEnded(mCall);
234 bubbleRemoved(b);
235 return true;
236 }
237 }, hangup_icon));
alision84813a12013-05-27 17:40:39 -0400238
alision1005ba12013-06-19 13:52:44 -0400239 }
alision85704182013-05-29 15:23:03 -0400240
alision1005ba12013-06-19 13:52:44 -0400241 private void initIncomingCallDisplay() {
242 Log.i(TAG, "Start incoming display");
alision84813a12013-05-27 17:40:39 -0400243
alision1005ba12013-06-19 13:52:44 -0400244 callStatusTxt.setText("Incomming call");
alision84813a12013-05-27 17:40:39 -0400245
alision1005ba12013-06-19 13:52:44 -0400246 Bubble contact_bubble = getBubbleFor(mCall.getContacts().get(0), model.width / 2, model.height / 2);
247 contacts.put(mCall.getContacts().get(0), contact_bubble);
alision85704182013-05-29 15:23:03 -0400248
alision1005ba12013-06-19 13:52:44 -0400249 model.clearAttractors();
250 model.addAttractor(new Attractor(new PointF(4 * model.width / 5, model.height / 2), ATTRACTOR_SIZE, new Attractor.Callback() {
251 @Override
252 public boolean onBubbleSucked(Bubble b) {
253 mCallbacks.onCallAccepted(mCall);
254 return false;
255 }
256 }, call_icon));
257 model.addAttractor(new Attractor(new PointF(model.width / 5, model.height / 2), ATTRACTOR_SIZE, new Attractor.Callback() {
258 @Override
259 public boolean onBubbleSucked(Bubble b) {
260 mCallbacks.onCallRejected(mCall);
261 bubbleRemoved(b);
262 return true;
263 }
264 }, hangup_icon));
265 }
alision85704182013-05-29 15:23:03 -0400266
alision1005ba12013-06-19 13:52:44 -0400267 private void initOutGoingCallDisplay() {
268 Log.i(TAG, "Start outgoing display");
alision85704182013-05-29 15:23:03 -0400269
alision1005ba12013-06-19 13:52:44 -0400270 callStatusTxt.setText("Calling...");
alision85704182013-05-29 15:23:03 -0400271
alision1005ba12013-06-19 13:52:44 -0400272 // TODO off-thread image loading
273 getBubbleFor(mCall.getContacts().get(0), model.width / 2, model.height / 3);
274 getBubbleFor(myself, model.width / 2, model.height / 2);
alision85704182013-05-29 15:23:03 -0400275
alision1005ba12013-06-19 13:52:44 -0400276 model.clearAttractors();
277 model.addAttractor(new Attractor(new PointF(model.width / 2, model.height * .8f), 40, new Attractor.Callback() {
278 @Override
279 public boolean onBubbleSucked(Bubble b) {
280 Log.w(TAG, "Bubble sucked ! ");
281 mCallbacks.onCallEnded(mCall);
282 bubbleRemoved(b);
283 return true;
284 }
285 }, hangup_icon));
286 }
alision85704182013-05-29 15:23:03 -0400287
alision1005ba12013-06-19 13:52:44 -0400288 /**
289 * Retrieves or create a bubble for a given contact. If the bubble exists, it is moved to the new location.
290 *
291 * @param contact
292 * The contact
293 * @param x
294 * Initial or new x position.
295 * @param y
296 * Initial or new y position.
297 * @return Bubble corresponding to the contact.
298 */
299 private Bubble getBubbleFor(CallContact contact, float x, float y) {
300 Bubble contact_bubble = contacts.get(contact);
301 if (contact_bubble != null) {
302 contact_bubble.attractor.set(x, y);
303 return contact_bubble;
304 }
alision84813a12013-05-27 17:40:39 -0400305
alision1005ba12013-06-19 13:52:44 -0400306 // TODO off-thread image loading
307 if (contact.getPhoto_id() > 0) {
308 Bitmap photo = ContactPictureLoader.loadContactPhoto(getActivity().getContentResolver(), contact.getId());
309 contact_bubble = new Bubble(x, y, BUBBLE_SIZE, photo);
310 } else {
311 contact_bubble = new Bubble(x, y, BUBBLE_SIZE, getActivity(), R.drawable.ic_contact_picture);
312 }
313 contact_bubble.contact = contact;
alision84813a12013-05-27 17:40:39 -0400314
alision1005ba12013-06-19 13:52:44 -0400315 model.addBubble(contact_bubble);
316 contacts.put(contact, contact_bubble);
alision84813a12013-05-27 17:40:39 -0400317
alision1005ba12013-06-19 13:52:44 -0400318 return contact_bubble;
319 }
Adrien Béraud0c9bd8f2013-05-30 16:16:57 +1000320
alision1005ba12013-06-19 13:52:44 -0400321 /**
322 * Should be called when a bubble is removed from the model
323 */
324 void bubbleRemoved(Bubble b) {
325 if (b.contact == null) {
326 return;
327 }
Adrien Béraudc9c424d2013-05-30 17:47:35 +1000328
alision1005ba12013-06-19 13:52:44 -0400329 contacts.remove(b.contact);
330 }
Adrien Béraudc9c424d2013-05-30 17:47:35 +1000331
alision1005ba12013-06-19 13:52:44 -0400332 public void changeCallState(String callID, String newState) {
Adrien Béraud0c9bd8f2013-05-30 16:16:57 +1000333
alision1005ba12013-06-19 13:52:44 -0400334 Log.w(TAG, "Changing call state of " + callID);
335 mCall.printCallInfo();
336 if (!callID.equals(mCall.getCallId()))
337 return;
Adrien Béraud0c9bd8f2013-05-30 16:16:57 +1000338
alision1005ba12013-06-19 13:52:44 -0400339 mCall.setCallState(newState);
340 if (mCall.isOngoing()) {
341 initNormalStateDisplay();
342 }
343 }
alision84813a12013-05-27 17:40:39 -0400344
alision1005ba12013-06-19 13:52:44 -0400345 public boolean draggingBubble() {
346 return view == null ? false : view.isDraggingBubble();
347 }
Adrien Béraudc9c424d2013-05-30 17:47:35 +1000348
alision1005ba12013-06-19 13:52:44 -0400349 @Override
350 public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
351 Log.i(TAG, "Init fragment " + mCall.getCallId());
Adrien Béraud13cde0b2013-05-30 20:27:20 +1000352
alision1005ba12013-06-19 13:52:44 -0400353 mCall.printCallInfo();
Adrien Béraud13cde0b2013-05-30 20:27:20 +1000354
alision1005ba12013-06-19 13:52:44 -0400355 if (mCall.isIncoming() && mCall.isRinging()) {
356 initIncomingCallDisplay();
357 } else {
358 if (mCall.isRinging()) {
359 initOutGoingCallDisplay();
360 }
361 try {
362 if (mCall.isOutGoing() && mCallbacks.getService().getCall(mCall.getCallId()) == null) {
363 mCallbacks.getService().placeCall(mCall);
364 initOutGoingCallDisplay();
365 } else if (mCall.isOutGoing() && mCall.isRinging()) {
366 initOutGoingCallDisplay();
367 }
368 } catch (RemoteException e) {
369 Log.e(TAG, e.toString());
370 }
371 }
Adrien Béraud13cde0b2013-05-30 20:27:20 +1000372
alision1005ba12013-06-19 13:52:44 -0400373 if (mCall.isOngoing()) {
374 initNormalStateDisplay();
375 }
Adrien Béraud13cde0b2013-05-30 20:27:20 +1000376
alision1005ba12013-06-19 13:52:44 -0400377 }
Adrien Béraud13cde0b2013-05-30 20:27:20 +1000378
alision1005ba12013-06-19 13:52:44 -0400379 @Override
380 public void surfaceCreated(SurfaceHolder holder) {
381 }
Adrien Béraud13cde0b2013-05-30 20:27:20 +1000382
alision1005ba12013-06-19 13:52:44 -0400383 @Override
384 public void surfaceDestroyed(SurfaceHolder holder) {
385 }
Adrien Béraud13cde0b2013-05-30 20:27:20 +1000386
alisione38001f2013-06-04 14:14:39 -0400387 public BubblesView getBubbleView() {
388 return view;
alisione38001f2013-06-04 14:14:39 -0400389
alision1005ba12013-06-19 13:52:44 -0400390 }
alision84813a12013-05-27 17:40:39 -0400391
alision84813a12013-05-27 17:40:39 -0400392}