blob: f7a75fd4341395d8548be0150577ad6a0a41e509 [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
Alexandre Lision2b237922013-09-09 16:23:02 -040034import java.util.ArrayList;
35
alision84813a12013-05-27 17:40:39 -040036import android.app.Activity;
37import android.app.Fragment;
Alexandre Lision2b237922013-09-09 16:23:02 -040038import android.app.FragmentManager;
Alexandre Lisionf1850c02013-09-23 14:19:34 -040039import android.content.Context;
Alexandre Lision2b237922013-09-09 16:23:02 -040040import android.content.Intent;
alision84813a12013-05-27 17:40:39 -040041import android.graphics.Bitmap;
Adrien Béraud0c9bd8f2013-05-30 16:16:57 +100042import android.graphics.BitmapFactory;
alision84813a12013-05-27 17:40:39 -040043import android.graphics.PointF;
Alexandre Lisionf1850c02013-09-23 14:19:34 -040044import android.hardware.Sensor;
45import android.hardware.SensorEvent;
46import android.hardware.SensorEventListener;
47import android.hardware.SensorManager;
alision84813a12013-05-27 17:40:39 -040048import android.os.Bundle;
49import android.os.RemoteException;
alision84813a12013-05-27 17:40:39 -040050import android.util.Log;
51import android.view.LayoutInflater;
Adrien Béraud13cde0b2013-05-30 20:27:20 +100052import android.view.SurfaceHolder;
53import android.view.SurfaceHolder.Callback;
alision84813a12013-05-27 17:40:39 -040054import android.view.View;
55import android.view.ViewGroup;
Alexandre Lisionf1850c02013-09-23 14:19:34 -040056import android.view.WindowManager;
Alexandre Lision3c6b7102013-09-16 16:56:46 -040057import android.widget.TextView;
Alexandre Lisiona764c682013-09-09 10:02:07 -040058import android.widget.Toast;
alision84813a12013-05-27 17:40:39 -040059
60import com.savoirfairelinux.sflphone.R;
alision84813a12013-05-27 17:40:39 -040061import com.savoirfairelinux.sflphone.model.Attractor;
62import com.savoirfairelinux.sflphone.model.Bubble;
63import com.savoirfairelinux.sflphone.model.BubbleModel;
64import com.savoirfairelinux.sflphone.model.BubblesView;
alisiondf1dac92013-06-27 17:35:53 -040065import com.savoirfairelinux.sflphone.model.Conference;
alision84813a12013-05-27 17:40:39 -040066import com.savoirfairelinux.sflphone.model.SipCall;
alision84813a12013-05-27 17:40:39 -040067import com.savoirfairelinux.sflphone.service.ISipService;
68
Alexandre Lisionf1850c02013-09-23 14:19:34 -040069public class CallFragment extends Fragment implements Callback, SensorEventListener {
alision84813a12013-05-27 17:40:39 -040070
alision1005ba12013-06-19 13:52:44 -040071 static final String TAG = "CallFragment";
alision84813a12013-05-27 17:40:39 -040072
alisionf57d8a62013-07-02 09:37:12 -040073 float BUBBLE_SIZE = 75;
alision1005ba12013-06-19 13:52:44 -040074 static final float ATTRACTOR_SIZE = 40;
alision84813a12013-05-27 17:40:39 -040075
Alexandre Lision2b237922013-09-09 16:23:02 -040076 public static final int REQUEST_TRANSFER = 10;
77
alisiondf1dac92013-06-27 17:35:53 -040078 private Conference conf;
alision84813a12013-05-27 17:40:39 -040079
Alexandre Lision3c6b7102013-09-16 16:56:46 -040080 private TextView callStatusTxt;
alision1005ba12013-06-19 13:52:44 -040081 private BubblesView view;
82 private BubbleModel model;
alision84813a12013-05-27 17:40:39 -040083
alision1005ba12013-06-19 13:52:44 -040084 private Callbacks mCallbacks = sDummyCallbacks;
alision84813a12013-05-27 17:40:39 -040085
alision806e18e2013-06-21 15:30:17 -040086 private SipCall myself;
alision85704182013-05-29 15:23:03 -040087
Alexandre Lisiona764c682013-09-09 10:02:07 -040088 boolean accepted = false;
89
Alexandre Lision2b237922013-09-09 16:23:02 -040090 private Bitmap hangup_icon, transfer_icon;
alision1005ba12013-06-19 13:52:44 -040091 private Bitmap call_icon;
alision85704182013-05-29 15:23:03 -040092
Alexandre Lisionf1850c02013-09-23 14:19:34 -040093 private SensorManager mSensorManager;
94
95 private Sensor mSensor;
96
alision1005ba12013-06-19 13:52:44 -040097 @Override
98 public void onCreate(Bundle savedBundle) {
99 super.onCreate(savedBundle);
alision1005ba12013-06-19 13:52:44 -0400100 Bundle b = getArguments();
alisioncd8fb912013-06-28 14:43:51 -0400101 conf = new Conference((Conference) b.getParcelable("conference"));
alisiondf1dac92013-06-27 17:35:53 -0400102 model = new BubbleModel(getResources().getDisplayMetrics().density);
alision729b0a22013-07-02 11:57:33 -0400103 BUBBLE_SIZE = getResources().getDimension(R.dimen.bubble_size);
Alexandre Lisiona764c682013-09-09 10:02:07 -0400104 Log.e(TAG, "BUBBLE_SIZE " + BUBBLE_SIZE);
Alexandre Lisionf1850c02013-09-23 14:19:34 -0400105 mSensorManager = (SensorManager) getActivity().getSystemService(Context.SENSOR_SERVICE);
106 mSensor = mSensorManager.getDefaultSensor(Sensor.TYPE_PROXIMITY);
alision729b0a22013-07-02 11:57:33 -0400107
alision1005ba12013-06-19 13:52:44 -0400108 }
alision85992112013-05-29 12:18:08 -0400109
alision1005ba12013-06-19 13:52:44 -0400110 /**
111 * A dummy implementation of the {@link Callbacks} interface that does nothing. Used only when this fragment is not attached to an activity.
112 */
113 private static Callbacks sDummyCallbacks = new Callbacks() {
114 @Override
115 public void onSendMessage(SipCall call, String msg) {
116 }
alision85992112013-05-29 12:18:08 -0400117
alision1005ba12013-06-19 13:52:44 -0400118 @Override
119 public void callContact(SipCall call) {
120 }
alision85992112013-05-29 12:18:08 -0400121
alision1005ba12013-06-19 13:52:44 -0400122 @Override
123 public void onCallAccepted(SipCall call) {
124 }
alision85704182013-05-29 15:23:03 -0400125
alision1005ba12013-06-19 13:52:44 -0400126 @Override
127 public void onCallRejected(SipCall call) {
128 }
alision85992112013-05-29 12:18:08 -0400129
alision1005ba12013-06-19 13:52:44 -0400130 @Override
131 public void onCallEnded(SipCall call) {
132 }
alision85704182013-05-29 15:23:03 -0400133
alision1005ba12013-06-19 13:52:44 -0400134 @Override
135 public void onCallSuspended(SipCall call) {
136 }
alision85992112013-05-29 12:18:08 -0400137
alision1005ba12013-06-19 13:52:44 -0400138 @Override
139 public void onCallResumed(SipCall call) {
140 }
alision85704182013-05-29 15:23:03 -0400141
alision1005ba12013-06-19 13:52:44 -0400142 @Override
143 public void onCalltransfered(SipCall call, String to) {
144 }
alision85992112013-05-29 12:18:08 -0400145
alision1005ba12013-06-19 13:52:44 -0400146 @Override
147 public void onRecordCall(SipCall call) {
148 }
alision85704182013-05-29 15:23:03 -0400149
alision1005ba12013-06-19 13:52:44 -0400150 @Override
151 public ISipService getService() {
152 return null;
153 }
alisiondf1dac92013-06-27 17:35:53 -0400154
155 @Override
156 public void replaceCurrentCallDisplayed() {
157 }
Alexandre Lision0c384512013-09-17 17:15:57 -0400158
159 @Override
Adrien Béraude78d06f2013-09-19 13:33:44 +1000160 public void startTimer() {
Alexandre Lision0c384512013-09-17 17:15:57 -0400161 }
alision1005ba12013-06-19 13:52:44 -0400162 };
alision907bde72013-06-20 14:40:37 -0400163
alision1005ba12013-06-19 13:52:44 -0400164 /**
165 * The Activity calling this fragment has to implement this interface
166 *
167 */
168 public interface Callbacks {
alision85992112013-05-29 12:18:08 -0400169
alision1005ba12013-06-19 13:52:44 -0400170 public ISipService getService();
alision85704182013-05-29 15:23:03 -0400171
alision1005ba12013-06-19 13:52:44 -0400172 public void callContact(SipCall call);
alision85992112013-05-29 12:18:08 -0400173
alision1005ba12013-06-19 13:52:44 -0400174 public void onCallAccepted(SipCall call);
alision85704182013-05-29 15:23:03 -0400175
alision1005ba12013-06-19 13:52:44 -0400176 public void onCallRejected(SipCall call);
alision85992112013-05-29 12:18:08 -0400177
alision1005ba12013-06-19 13:52:44 -0400178 public void onCallEnded(SipCall call);
alision85704182013-05-29 15:23:03 -0400179
alision1005ba12013-06-19 13:52:44 -0400180 public void onCallSuspended(SipCall call);
alision85704182013-05-29 15:23:03 -0400181
alision1005ba12013-06-19 13:52:44 -0400182 public void onCallResumed(SipCall call);
alision84813a12013-05-27 17:40:39 -0400183
alision1005ba12013-06-19 13:52:44 -0400184 public void onCalltransfered(SipCall call, String to);
alision84813a12013-05-27 17:40:39 -0400185
alision1005ba12013-06-19 13:52:44 -0400186 public void onRecordCall(SipCall call);
alision85704182013-05-29 15:23:03 -0400187
alision1005ba12013-06-19 13:52:44 -0400188 public void onSendMessage(SipCall call, String msg);
alisiondf1dac92013-06-27 17:35:53 -0400189
190 public void replaceCurrentCallDisplayed();
Alexandre Lision0c384512013-09-17 17:15:57 -0400191
192 public void startTimer();
alision1005ba12013-06-19 13:52:44 -0400193 }
alision85992112013-05-29 12:18:08 -0400194
alision1005ba12013-06-19 13:52:44 -0400195 @Override
196 public void onAttach(Activity activity) {
197 super.onAttach(activity);
alision85992112013-05-29 12:18:08 -0400198
alision1005ba12013-06-19 13:52:44 -0400199 if (!(activity instanceof Callbacks)) {
200 throw new IllegalStateException("Activity must implement fragment's callbacks.");
201 }
alision85992112013-05-29 12:18:08 -0400202
alision1005ba12013-06-19 13:52:44 -0400203 // rootView.requestDisallowInterceptTouchEvent(true);
alision85992112013-05-29 12:18:08 -0400204
alision1005ba12013-06-19 13:52:44 -0400205 mCallbacks = (Callbacks) activity;
alision50fa0722013-06-25 17:29:44 -0400206 myself = SipCall.SipCallBuilder.buildMyselfCall(activity.getContentResolver(), "Me");
Adrien Béraude78d06f2013-09-19 13:33:44 +1000207
alision1005ba12013-06-19 13:52:44 -0400208 }
alision85992112013-05-29 12:18:08 -0400209
alision1005ba12013-06-19 13:52:44 -0400210 @Override
211 public void onDetach() {
212 super.onDetach();
213 mCallbacks = sDummyCallbacks;
Alexandre Lisionf1850c02013-09-23 14:19:34 -0400214 mSensorManager.unregisterListener(this);
alision1005ba12013-06-19 13:52:44 -0400215 // rootView.requestDisallowInterceptTouchEvent(false);
216 }
alision907bde72013-06-20 14:40:37 -0400217
alision1005ba12013-06-19 13:52:44 -0400218 @Override
alision907bde72013-06-20 14:40:37 -0400219 public void onStop() {
alision1005ba12013-06-19 13:52:44 -0400220 super.onStop();
221 }
alision85992112013-05-29 12:18:08 -0400222
alision1005ba12013-06-19 13:52:44 -0400223 @Override
Alexandre Lisionf1850c02013-09-23 14:19:34 -0400224 public void onResume() {
225 super.onResume();
226 mSensorManager.registerListener(this, mSensor, SensorManager.SENSOR_DELAY_NORMAL);
227 }
228
229 @Override
230 public void onPause() {
231 super.onPause();
232 mSensorManager.unregisterListener(this);
233 }
234
235 @Override
Alexandre Lision2b237922013-09-09 16:23:02 -0400236 public void onActivityResult(int requestCode, int resultCode, Intent data) {
237 super.onActivityResult(requestCode, resultCode, data);
238 SipCall transfer = null;
239 if (requestCode == REQUEST_TRANSFER) {
240 switch (resultCode) {
Adrien Béraude78d06f2013-09-19 13:33:44 +1000241 case TransferDFragment.RESULT_TRANSFER_CONF:
Alexandre Lision2b237922013-09-09 16:23:02 -0400242 Conference c = data.getParcelableExtra("target");
243 transfer = data.getParcelableExtra("transfer");
244 try {
245
246 mCallbacks.getService().attendedTransfer(transfer.getCallId(), c.getParticipants().get(0).getCallId());
247
248 } catch (RemoteException e) {
249 // TODO Auto-generated catch block
250 e.printStackTrace();
251 }
Alexandre Lision0edf18c2013-09-23 17:35:50 -0400252// Toast.makeText(getActivity(), "Transfer complete", Toast.LENGTH_LONG).show();
Alexandre Lision2b237922013-09-09 16:23:02 -0400253 break;
254
Adrien Béraude78d06f2013-09-19 13:33:44 +1000255 case TransferDFragment.RESULT_TRANSFER_NUMBER:
Alexandre Lision2b237922013-09-09 16:23:02 -0400256 String to = data.getStringExtra("to_number");
257 transfer = data.getParcelableExtra("transfer");
258 try {
Alexandre Lision0edf18c2013-09-23 17:35:50 -0400259// Toast.makeText(getActivity(), "Transferring " + transfer.getContact().getmDisplayName() + " to " + to, Toast.LENGTH_SHORT).show();
Alexandre Lision2b237922013-09-09 16:23:02 -0400260 mCallbacks.getService().transfer(transfer.getCallId(), to);
261 mCallbacks.getService().hangUp(transfer.getCallId());
262 } catch (RemoteException e) {
263 // TODO Auto-generated catch block
264 e.printStackTrace();
265 }
266 break;
Adrien Béraud97512842013-09-19 13:37:19 +1000267 case Activity.RESULT_CANCELED:
Alexandre Lision2b237922013-09-09 16:23:02 -0400268 default:
Adrien Béraude78d06f2013-09-19 13:33:44 +1000269 model.clear();
270 initNormalStateDisplay();
Alexandre Lision2b237922013-09-09 16:23:02 -0400271 break;
272 }
273 }
274 }
275
276 @Override
alision1005ba12013-06-19 13:52:44 -0400277 public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
278 ViewGroup rootView = (ViewGroup) inflater.inflate(R.layout.frag_call, container, false);
alision85992112013-05-29 12:18:08 -0400279
alision1005ba12013-06-19 13:52:44 -0400280 view = (BubblesView) rootView.findViewById(R.id.main_view);
alision34673e62013-06-25 14:40:07 -0400281 view.setFragment(this);
alision1005ba12013-06-19 13:52:44 -0400282 view.setModel(model);
283 view.getHolder().addCallback(this);
alision84813a12013-05-27 17:40:39 -0400284
Alexandre Lision3c6b7102013-09-16 16:56:46 -0400285 callStatusTxt = (TextView) rootView.findViewById(R.id.call_status_txt);
alision84813a12013-05-27 17:40:39 -0400286
alision1005ba12013-06-19 13:52:44 -0400287 hangup_icon = BitmapFactory.decodeResource(getResources(), R.drawable.ic_hangup);
288 call_icon = BitmapFactory.decodeResource(getResources(), R.drawable.ic_call);
Alexandre Lision2b237922013-09-09 16:23:02 -0400289 transfer_icon = BitmapFactory.decodeResource(getResources(), R.drawable.ic_phones_call_transfer_icon);
alision84813a12013-05-27 17:40:39 -0400290
alision1005ba12013-06-19 13:52:44 -0400291 // Do nothing here, the view is not initialized yet.
292 return rootView;
293 }
alision85992112013-05-29 12:18:08 -0400294
alision1005ba12013-06-19 13:52:44 -0400295 private void initNormalStateDisplay() {
296 Log.i(TAG, "Start normal display");
alision84813a12013-05-27 17:40:39 -0400297
Alexandre Lision0c384512013-09-17 17:15:57 -0400298 mCallbacks.startTimer();
alision84813a12013-05-27 17:40:39 -0400299
alision1005ba12013-06-19 13:52:44 -0400300 getBubbleFor(myself, model.width / 2, model.height / 2);
alision907bde72013-06-20 14:40:37 -0400301
alisiondf1dac92013-06-27 17:35:53 -0400302 int angle_part = 360 / conf.getParticipants().size();
alision806e18e2013-06-21 15:30:17 -0400303 double dX = 0;
304 double dY = 0;
alision465ceba2013-07-04 09:24:30 -0400305 int radiusCalls = (int) (model.width / 2 - BUBBLE_SIZE);
alisiondf1dac92013-06-27 17:35:53 -0400306 for (int i = 0; i < conf.getParticipants().size(); ++i) {
307
308 if (conf.getParticipants().get(i) == null) {
309 Log.i(TAG, i + " null ");
310 continue;
311 }
alision34673e62013-06-25 14:40:07 -0400312 dX = Math.cos(Math.toRadians(angle_part * i - 90)) * radiusCalls;
313 dY = Math.sin(Math.toRadians(angle_part * i - 90)) * radiusCalls;
alisiondf1dac92013-06-27 17:35:53 -0400314 getBubbleFor(conf.getParticipants().get(i), (int) (model.width / 2 + dX), (int) (model.height / 2 + dY));
alision806e18e2013-06-21 15:30:17 -0400315 }
alision1005ba12013-06-19 13:52:44 -0400316
317 model.clearAttractors();
Alexandre Lision23628c12013-09-24 11:17:05 -0400318// model.addAttractor(new Attractor(new PointF(model.width * 0.9f, model.height * 0.1f), ATTRACTOR_SIZE, new Attractor.Callback() {
Alexandre Lision0edf18c2013-09-23 17:35:50 -0400319// @Override
320// public boolean onBubbleSucked(Bubble b) {
321// Log.w(TAG, "Bubble sucked ! ");
322//
323// if (b.associated_call.getContact().isUser()) {
324//
325// try {
326// if (conf.hasMultipleParticipants())
327// mCallbacks.getService().hangUpConference(conf.getId());
328// else
329// mCallbacks.onCallEnded(conf.getParticipants().get(0));
330//
331// model.clearAttractors();
332// } catch (RemoteException e) {
333// e.printStackTrace();
334// }
335//
336// } else {
337// mCallbacks.onCallEnded(b.associated_call);
338// }
339// bubbleRemoved(b);
340// return true;
341// }
342// }, hangup_icon));
alisiondf1dac92013-06-27 17:35:53 -0400343
Alexandre Lision0edf18c2013-09-23 17:35:50 -0400344// model.addAttractor(new Attractor(new PointF(model.width * .8f, model.height * 0.9f), ATTRACTOR_SIZE, new Attractor.Callback() {
345// @Override
346// public boolean onBubbleSucked(Bubble b) {
347// Log.w(TAG, "Bubble sucked ! ");
348// makeTransfer(b);
349// return true;
350// }
351// }, transfer_icon));
Alexandre Lision2b237922013-09-09 16:23:02 -0400352
alisiondf1dac92013-06-27 17:35:53 -0400353 // if (conf.hasMultipleParticipants()) {
354 // model.addAttractor(new Attractor(new PointF(model.width / 1.1f, model.height * .9f), ATTRACTOR_SIZE, new Attractor.Callback() {
355 // @Override
356 // public boolean onBubbleSucked(Bubble b) {
357 //
358 // try {
359 // mCallbacks.getService().detachParticipant(b.associated_call.getCallId());
360 // } catch (RemoteException e) {
361 // e.printStackTrace();
362 // }
363 //
364 // bubbleRemoved(b);
365 // return true;
366 // }
367 // }, separate_icon));
368 // }
369
370 // if(mCalls.size() == 1 && mCalls.get(0).isOnHold()){
371 // mCallbacks.onCallResumed(mCalls.get(0));
372 // }
alision84813a12013-05-27 17:40:39 -0400373
alision1005ba12013-06-19 13:52:44 -0400374 }
alision85704182013-05-29 15:23:03 -0400375
alision1005ba12013-06-19 13:52:44 -0400376 private void initIncomingCallDisplay() {
377 Log.i(TAG, "Start incoming display");
alision84813a12013-05-27 17:40:39 -0400378
Alexandre Lision0c384512013-09-17 17:15:57 -0400379 mCallbacks.startTimer();
alision84813a12013-05-27 17:40:39 -0400380
Alexandre Lision23628c12013-09-24 11:17:05 -0400381 int radiusCalls = (int) (model.width / 2 - BUBBLE_SIZE);
382 getBubbleFor(myself, model.width / 2, model.height / 2 + radiusCalls);
383 getBubbleFor(conf.getParticipants().get(0), model.width / 2, model.height / 2 - radiusCalls);
384
alision85704182013-05-29 15:23:03 -0400385
alision1005ba12013-06-19 13:52:44 -0400386 model.clearAttractors();
Alexandre Lision23628c12013-09-24 11:17:05 -0400387 model.addAttractor(new Attractor(new PointF(model.width / 2, model.height / 2), ATTRACTOR_SIZE, new Attractor.Callback() {
alision1005ba12013-06-19 13:52:44 -0400388 @Override
389 public boolean onBubbleSucked(Bubble b) {
Alexandre Lisiona764c682013-09-09 10:02:07 -0400390
391 if (!accepted) {
392 mCallbacks.onCallAccepted(conf.getParticipants().get(0));
393 accepted = true;
394 }
alision1005ba12013-06-19 13:52:44 -0400395 return false;
396 }
397 }, call_icon));
alision1005ba12013-06-19 13:52:44 -0400398 }
alision85704182013-05-29 15:23:03 -0400399
alision1005ba12013-06-19 13:52:44 -0400400 private void initOutGoingCallDisplay() {
401 Log.i(TAG, "Start outgoing display");
alision85704182013-05-29 15:23:03 -0400402
Alexandre Lision0c384512013-09-17 17:15:57 -0400403 mCallbacks.startTimer();
alision85704182013-05-29 15:23:03 -0400404
Adrien Béraude78d06f2013-09-19 13:33:44 +1000405 getBubbleFor(myself, model.width / 2, model.height / 2);
alision85704182013-05-29 15:23:03 -0400406
alisiondf1dac92013-06-27 17:35:53 -0400407 // TODO off-thread image loading
408 int angle_part = 360 / conf.getParticipants().size();
409 double dX = 0;
410 double dY = 0;
Adrien Béraude78d06f2013-09-19 13:33:44 +1000411 int radiusCalls = (int) (model.width / 2 - BUBBLE_SIZE);
alisiondf1dac92013-06-27 17:35:53 -0400412 for (int i = 0; i < conf.getParticipants().size(); ++i) {
413 dX = Math.cos(Math.toRadians(angle_part * i - 90)) * radiusCalls;
414 dY = Math.sin(Math.toRadians(angle_part * i - 90)) * radiusCalls;
415 getBubbleFor(conf.getParticipants().get(i), (int) (model.width / 2 + dX), (int) (model.height / 2 + dY));
416 }
alision907bde72013-06-20 14:40:37 -0400417
alision1005ba12013-06-19 13:52:44 -0400418 model.clearAttractors();
Alexandre Lision2b237922013-09-09 16:23:02 -0400419
Alexandre Lision0edf18c2013-09-23 17:35:50 -0400420// model.addAttractor(new Attractor(new PointF(model.width / 2f, model.height * .9f), 40, new Attractor.Callback() {
421// @Override
422// public boolean onBubbleSucked(Bubble b) {
423// Log.w(TAG, "Bubble sucked ! ");
424// mCallbacks.onCallEnded(conf.getParticipants().get(0));
425// bubbleRemoved(b);
426// return true;
427// }
428// }, hangup_icon));
alision1005ba12013-06-19 13:52:44 -0400429 }
alision85704182013-05-29 15:23:03 -0400430
alision1005ba12013-06-19 13:52:44 -0400431 /**
432 * Retrieves or create a bubble for a given contact. If the bubble exists, it is moved to the new location.
433 *
alision806e18e2013-06-21 15:30:17 -0400434 * @param call
435 * The call associated to a contact
alision1005ba12013-06-19 13:52:44 -0400436 * @param x
437 * Initial or new x position.
438 * @param y
439 * Initial or new y position.
440 * @return Bubble corresponding to the contact.
441 */
alision806e18e2013-06-21 15:30:17 -0400442 private Bubble getBubbleFor(SipCall call, float x, float y) {
alisiondf1dac92013-06-27 17:35:53 -0400443 Bubble contact_bubble = model.getBubble(call);
alision1005ba12013-06-19 13:52:44 -0400444 if (contact_bubble != null) {
445 contact_bubble.attractor.set(x, y);
446 return contact_bubble;
447 }
alision84813a12013-05-27 17:40:39 -0400448
alision34673e62013-06-25 14:40:07 -0400449 contact_bubble = new Bubble(getActivity(), call, x, y, BUBBLE_SIZE);
alision84813a12013-05-27 17:40:39 -0400450
alision1005ba12013-06-19 13:52:44 -0400451 model.addBubble(contact_bubble);
alision1005ba12013-06-19 13:52:44 -0400452 return contact_bubble;
453 }
Adrien Béraud0c9bd8f2013-05-30 16:16:57 +1000454
alision1005ba12013-06-19 13:52:44 -0400455 /**
456 * Should be called when a bubble is removed from the model
457 */
458 void bubbleRemoved(Bubble b) {
alision806e18e2013-06-21 15:30:17 -0400459 if (b.associated_call == null) {
alision1005ba12013-06-19 13:52:44 -0400460 return;
461 }
alision1005ba12013-06-19 13:52:44 -0400462 }
Adrien Béraudc9c424d2013-05-30 17:47:35 +1000463
alision1005ba12013-06-19 13:52:44 -0400464 public void changeCallState(String callID, String newState) {
Alexandre Lisionf1850c02013-09-23 14:19:34 -0400465 Log.w(TAG, "Call :" + callID + " " + newState);
alisiondf1dac92013-06-27 17:35:53 -0400466 if (newState.contentEquals("FAILURE")) {
467 try {
468 mCallbacks.getService().hangUp(callID);
469 } catch (RemoteException e) {
470 e.printStackTrace();
alision806e18e2013-06-21 15:30:17 -0400471 }
alision1005ba12013-06-19 13:52:44 -0400472 }
alisiondf1dac92013-06-27 17:35:53 -0400473
Alexandre Lisionf1850c02013-09-23 14:19:34 -0400474 // Log.w(TAG, "conf.getParticipants().size():" + conf.getParticipants().size());
alisiondf1dac92013-06-27 17:35:53 -0400475 for (int i = 0; i < conf.getParticipants().size(); ++i) {
476 // conf.getParticipants().get(i).printCallInfo();
Alexandre Lisionf1850c02013-09-23 14:19:34 -0400477 // Log.w(TAG, "Call id:" + conf.getParticipants().get(i).getCallId());
478 // Log.w(TAG, "Searching:" + callID);
alisiondf1dac92013-06-27 17:35:53 -0400479 if (callID.equals(conf.getParticipants().get(i).getCallId())) {
480 if (newState.contentEquals("HUNGUP")) {
Alexandre Lisionf1850c02013-09-23 14:19:34 -0400481 // Log.w(TAG, "Call hungup:" + conf.getParticipants().get(i).getContact().getmDisplayName());
alisiondf1dac92013-06-27 17:35:53 -0400482 model.removeBubble(conf.getParticipants().get(i));
483 conf.getParticipants().remove(i);
484 } else {
Alexandre Lisionf1850c02013-09-23 14:19:34 -0400485 // Log.w(TAG, "Call:" + conf.getParticipants().get(i).getContact().getmDisplayName() + " state:" + newState);
alisiondf1dac92013-06-27 17:35:53 -0400486 conf.getParticipants().get(i).setCallState(newState);
487 }
488 }
489 }
490
491 if (conf.isOnGoing())
492 initNormalStateDisplay();
493
494 if (conf.getParticipants().size() == 0) {
495 mCallbacks.replaceCurrentCallDisplayed();
496 }
497
alision1005ba12013-06-19 13:52:44 -0400498 }
alision84813a12013-05-27 17:40:39 -0400499
alision1005ba12013-06-19 13:52:44 -0400500 public boolean draggingBubble() {
501 return view == null ? false : view.isDraggingBubble();
502 }
Adrien Béraudc9c424d2013-05-30 17:47:35 +1000503
alision1005ba12013-06-19 13:52:44 -0400504 @Override
505 public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
Adrien Béraud13cde0b2013-05-30 20:27:20 +1000506
alisiondf1dac92013-06-27 17:35:53 -0400507 if (conf.getParticipants().size() == 1) {
Adrien Béraud13cde0b2013-05-30 20:27:20 +1000508
alisiondf1dac92013-06-27 17:35:53 -0400509 if (conf.getParticipants().get(0).isIncoming() && conf.getParticipants().get(0).isRinging()) {
alision806e18e2013-06-21 15:30:17 -0400510 initIncomingCallDisplay();
511 } else {
alisiondf1dac92013-06-27 17:35:53 -0400512 if (conf.getParticipants().get(0).isRinging()) {
alision1005ba12013-06-19 13:52:44 -0400513 initOutGoingCallDisplay();
514 }
alision806e18e2013-06-21 15:30:17 -0400515 try {
alisiondf1dac92013-06-27 17:35:53 -0400516 if (conf.getParticipants().get(0).isOutGoing()
517 && mCallbacks.getService().getCall(conf.getParticipants().get(0).getCallId()) == null) {
518 mCallbacks.getService().placeCall(conf.getParticipants().get(0));
alision806e18e2013-06-21 15:30:17 -0400519 initOutGoingCallDisplay();
alisiondf1dac92013-06-27 17:35:53 -0400520 } else if (conf.getParticipants().get(0).isOutGoing() && conf.getParticipants().get(0).isRinging()) {
alision806e18e2013-06-21 15:30:17 -0400521 initOutGoingCallDisplay();
522 }
523 } catch (RemoteException e) {
524 Log.e(TAG, e.toString());
525 }
alision1005ba12013-06-19 13:52:44 -0400526 }
alisiondf1dac92013-06-27 17:35:53 -0400527 if (conf.getParticipants().get(0).isOngoing()) {
alision806e18e2013-06-21 15:30:17 -0400528 initNormalStateDisplay();
529 }
alisiondf1dac92013-06-27 17:35:53 -0400530 } else if (conf.getParticipants().size() > 1) {
alision1005ba12013-06-19 13:52:44 -0400531 initNormalStateDisplay();
532 }
Adrien Béraud13cde0b2013-05-30 20:27:20 +1000533
alision1005ba12013-06-19 13:52:44 -0400534 }
Adrien Béraud13cde0b2013-05-30 20:27:20 +1000535
Alexandre Lision2b237922013-09-09 16:23:02 -0400536 public void makeTransfer(Bubble contact) {
537 FragmentManager fm = getFragmentManager();
538 TransferDFragment editName = new TransferDFragment();
539
540 Bundle b = new Bundle();
541 b.putParcelableArrayList("calls", new ArrayList<Conference>());
542 b.putParcelable("call_selected", contact.associated_call);
543 editName.setArguments(b);
544 editName.setTargetFragment(this, REQUEST_TRANSFER);
545 editName.show(fm, "");
Alexandre Lision2b237922013-09-09 16:23:02 -0400546 }
547
alision1005ba12013-06-19 13:52:44 -0400548 @Override
549 public void surfaceCreated(SurfaceHolder holder) {
550 }
Adrien Béraud13cde0b2013-05-30 20:27:20 +1000551
alision1005ba12013-06-19 13:52:44 -0400552 @Override
553 public void surfaceDestroyed(SurfaceHolder holder) {
554 }
Adrien Béraud13cde0b2013-05-30 20:27:20 +1000555
alisione38001f2013-06-04 14:14:39 -0400556 public BubblesView getBubbleView() {
557 return view;
alisione38001f2013-06-04 14:14:39 -0400558
alision1005ba12013-06-19 13:52:44 -0400559 }
Alexandre Lision0c384512013-09-17 17:15:57 -0400560
561 public void updateTime() {
Adrien Béraude78d06f2013-09-19 13:33:44 +1000562 long duration = System.currentTimeMillis() / 1000 - this.conf.getParticipants().get(0).getTimestamp_start();
Alexandre Lisionf1850c02013-09-23 14:19:34 -0400563 callStatusTxt.setText(String.format("%d:%02d:%02d", duration / 3600, duration % 3600 / 60, duration % 60));
564
565 }
566
567 @Override
568 public void onAccuracyChanged(Sensor sensor, int accuracy) {
569 Log.i(TAG, "onAccuracyChanged");
570
571 }
572
573 @Override
574 public void onSensorChanged(SensorEvent event) {
575 Log.i(TAG, "onSensorChanged:" + event.sensor.getName());
576 Log.i(TAG, "onSensorChanged:" + event.sensor.getType());
577 if (event.values[0] == 0) {
578// PowerManager pm = (PowerManager) getActivity().getSystemService(Context.POWER_SERVICE);
579// pm.goToSleep(SystemClock.uptimeMillis());
580 WindowManager.LayoutParams params = getActivity().getWindow().getAttributes();
581 params.flags |= WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON;
582 params.screenBrightness = 0.004f;
583 getActivity().getWindow().setAttributes(params);
584// Settings.System.putInt(getActivity().getContentResolver(), Settings.System.SCREEN_OFF_TIMEOUT, 1000);
585 } else {
586 WindowManager.LayoutParams params = getActivity().getWindow().getAttributes();
587 params.flags = WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON;
588 params.screenBrightness = 1f;
589 getActivity().getWindow().setAttributes(params);
590 }
591 // Sensor.TYPE_PROXIMITY
Adrien Béraude78d06f2013-09-19 13:33:44 +1000592
Alexandre Lision0c384512013-09-17 17:15:57 -0400593 }
Alexandre Lision0edf18c2013-09-23 17:35:50 -0400594
595 public Conference getConference() {
596 return conf;
597 }
alision84813a12013-05-27 17:40:39 -0400598}