blob: 919e1da3342fa78b83f5a9e956e153513779baf1 [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 Lision0edf18c2013-09-23 17:35:50 -0400318// model.addAttractor(new Attractor(new PointF(model.width * .2f, model.height * 0.9f), ATTRACTOR_SIZE, new Attractor.Callback() {
319// @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
alisiondf1dac92013-06-27 17:35:53 -0400381 getBubbleFor(conf.getParticipants().get(0), model.width / 2, model.height / 2);
alision85704182013-05-29 15:23:03 -0400382
alision1005ba12013-06-19 13:52:44 -0400383 model.clearAttractors();
384 model.addAttractor(new Attractor(new PointF(4 * model.width / 5, model.height / 2), ATTRACTOR_SIZE, new Attractor.Callback() {
385 @Override
386 public boolean onBubbleSucked(Bubble b) {
Alexandre Lisiona764c682013-09-09 10:02:07 -0400387
388 if (!accepted) {
389 mCallbacks.onCallAccepted(conf.getParticipants().get(0));
390 accepted = true;
391 }
alision1005ba12013-06-19 13:52:44 -0400392 return false;
393 }
394 }, call_icon));
395 model.addAttractor(new Attractor(new PointF(model.width / 5, model.height / 2), ATTRACTOR_SIZE, new Attractor.Callback() {
396 @Override
397 public boolean onBubbleSucked(Bubble b) {
alisiondf1dac92013-06-27 17:35:53 -0400398 mCallbacks.onCallRejected(conf.getParticipants().get(0));
alision1005ba12013-06-19 13:52:44 -0400399 bubbleRemoved(b);
400 return true;
401 }
402 }, hangup_icon));
403 }
alision85704182013-05-29 15:23:03 -0400404
alision1005ba12013-06-19 13:52:44 -0400405 private void initOutGoingCallDisplay() {
406 Log.i(TAG, "Start outgoing display");
alision85704182013-05-29 15:23:03 -0400407
Alexandre Lision0c384512013-09-17 17:15:57 -0400408 mCallbacks.startTimer();
alision85704182013-05-29 15:23:03 -0400409
Adrien Béraude78d06f2013-09-19 13:33:44 +1000410 getBubbleFor(myself, model.width / 2, model.height / 2);
alision85704182013-05-29 15:23:03 -0400411
alisiondf1dac92013-06-27 17:35:53 -0400412 // TODO off-thread image loading
413 int angle_part = 360 / conf.getParticipants().size();
414 double dX = 0;
415 double dY = 0;
Adrien Béraude78d06f2013-09-19 13:33:44 +1000416 int radiusCalls = (int) (model.width / 2 - BUBBLE_SIZE);
alisiondf1dac92013-06-27 17:35:53 -0400417 for (int i = 0; i < conf.getParticipants().size(); ++i) {
418 dX = Math.cos(Math.toRadians(angle_part * i - 90)) * radiusCalls;
419 dY = Math.sin(Math.toRadians(angle_part * i - 90)) * radiusCalls;
420 getBubbleFor(conf.getParticipants().get(i), (int) (model.width / 2 + dX), (int) (model.height / 2 + dY));
421 }
alision907bde72013-06-20 14:40:37 -0400422
alision1005ba12013-06-19 13:52:44 -0400423 model.clearAttractors();
Alexandre Lision2b237922013-09-09 16:23:02 -0400424
Alexandre Lision0edf18c2013-09-23 17:35:50 -0400425// model.addAttractor(new Attractor(new PointF(model.width / 2f, model.height * .9f), 40, new Attractor.Callback() {
426// @Override
427// public boolean onBubbleSucked(Bubble b) {
428// Log.w(TAG, "Bubble sucked ! ");
429// mCallbacks.onCallEnded(conf.getParticipants().get(0));
430// bubbleRemoved(b);
431// return true;
432// }
433// }, hangup_icon));
alision1005ba12013-06-19 13:52:44 -0400434 }
alision85704182013-05-29 15:23:03 -0400435
alision1005ba12013-06-19 13:52:44 -0400436 /**
437 * Retrieves or create a bubble for a given contact. If the bubble exists, it is moved to the new location.
438 *
alision806e18e2013-06-21 15:30:17 -0400439 * @param call
440 * The call associated to a contact
alision1005ba12013-06-19 13:52:44 -0400441 * @param x
442 * Initial or new x position.
443 * @param y
444 * Initial or new y position.
445 * @return Bubble corresponding to the contact.
446 */
alision806e18e2013-06-21 15:30:17 -0400447 private Bubble getBubbleFor(SipCall call, float x, float y) {
alisiondf1dac92013-06-27 17:35:53 -0400448 Bubble contact_bubble = model.getBubble(call);
alision1005ba12013-06-19 13:52:44 -0400449 if (contact_bubble != null) {
450 contact_bubble.attractor.set(x, y);
451 return contact_bubble;
452 }
alision84813a12013-05-27 17:40:39 -0400453
alision34673e62013-06-25 14:40:07 -0400454 contact_bubble = new Bubble(getActivity(), call, x, y, BUBBLE_SIZE);
alision84813a12013-05-27 17:40:39 -0400455
alision1005ba12013-06-19 13:52:44 -0400456 model.addBubble(contact_bubble);
alision1005ba12013-06-19 13:52:44 -0400457 return contact_bubble;
458 }
Adrien Béraud0c9bd8f2013-05-30 16:16:57 +1000459
alision1005ba12013-06-19 13:52:44 -0400460 /**
461 * Should be called when a bubble is removed from the model
462 */
463 void bubbleRemoved(Bubble b) {
alision806e18e2013-06-21 15:30:17 -0400464 if (b.associated_call == null) {
alision1005ba12013-06-19 13:52:44 -0400465 return;
466 }
alision1005ba12013-06-19 13:52:44 -0400467 }
Adrien Béraudc9c424d2013-05-30 17:47:35 +1000468
alision1005ba12013-06-19 13:52:44 -0400469 public void changeCallState(String callID, String newState) {
Alexandre Lisionf1850c02013-09-23 14:19:34 -0400470 Log.w(TAG, "Call :" + callID + " " + newState);
alisiondf1dac92013-06-27 17:35:53 -0400471 if (newState.contentEquals("FAILURE")) {
472 try {
473 mCallbacks.getService().hangUp(callID);
474 } catch (RemoteException e) {
475 e.printStackTrace();
alision806e18e2013-06-21 15:30:17 -0400476 }
alision1005ba12013-06-19 13:52:44 -0400477 }
alisiondf1dac92013-06-27 17:35:53 -0400478
Alexandre Lisionf1850c02013-09-23 14:19:34 -0400479 // Log.w(TAG, "conf.getParticipants().size():" + conf.getParticipants().size());
alisiondf1dac92013-06-27 17:35:53 -0400480 for (int i = 0; i < conf.getParticipants().size(); ++i) {
481 // conf.getParticipants().get(i).printCallInfo();
Alexandre Lisionf1850c02013-09-23 14:19:34 -0400482 // Log.w(TAG, "Call id:" + conf.getParticipants().get(i).getCallId());
483 // Log.w(TAG, "Searching:" + callID);
alisiondf1dac92013-06-27 17:35:53 -0400484 if (callID.equals(conf.getParticipants().get(i).getCallId())) {
485 if (newState.contentEquals("HUNGUP")) {
Alexandre Lisionf1850c02013-09-23 14:19:34 -0400486 // Log.w(TAG, "Call hungup:" + conf.getParticipants().get(i).getContact().getmDisplayName());
alisiondf1dac92013-06-27 17:35:53 -0400487 model.removeBubble(conf.getParticipants().get(i));
488 conf.getParticipants().remove(i);
489 } else {
Alexandre Lisionf1850c02013-09-23 14:19:34 -0400490 // Log.w(TAG, "Call:" + conf.getParticipants().get(i).getContact().getmDisplayName() + " state:" + newState);
alisiondf1dac92013-06-27 17:35:53 -0400491 conf.getParticipants().get(i).setCallState(newState);
492 }
493 }
494 }
495
496 if (conf.isOnGoing())
497 initNormalStateDisplay();
498
499 if (conf.getParticipants().size() == 0) {
500 mCallbacks.replaceCurrentCallDisplayed();
501 }
502
alision1005ba12013-06-19 13:52:44 -0400503 }
alision84813a12013-05-27 17:40:39 -0400504
alision1005ba12013-06-19 13:52:44 -0400505 public boolean draggingBubble() {
506 return view == null ? false : view.isDraggingBubble();
507 }
Adrien Béraudc9c424d2013-05-30 17:47:35 +1000508
alision1005ba12013-06-19 13:52:44 -0400509 @Override
510 public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
Adrien Béraud13cde0b2013-05-30 20:27:20 +1000511
alisiondf1dac92013-06-27 17:35:53 -0400512 if (conf.getParticipants().size() == 1) {
Adrien Béraud13cde0b2013-05-30 20:27:20 +1000513
alisiondf1dac92013-06-27 17:35:53 -0400514 if (conf.getParticipants().get(0).isIncoming() && conf.getParticipants().get(0).isRinging()) {
alision806e18e2013-06-21 15:30:17 -0400515 initIncomingCallDisplay();
516 } else {
alisiondf1dac92013-06-27 17:35:53 -0400517 if (conf.getParticipants().get(0).isRinging()) {
alision1005ba12013-06-19 13:52:44 -0400518 initOutGoingCallDisplay();
519 }
alision806e18e2013-06-21 15:30:17 -0400520 try {
alisiondf1dac92013-06-27 17:35:53 -0400521 if (conf.getParticipants().get(0).isOutGoing()
522 && mCallbacks.getService().getCall(conf.getParticipants().get(0).getCallId()) == null) {
523 mCallbacks.getService().placeCall(conf.getParticipants().get(0));
alision806e18e2013-06-21 15:30:17 -0400524 initOutGoingCallDisplay();
alisiondf1dac92013-06-27 17:35:53 -0400525 } else if (conf.getParticipants().get(0).isOutGoing() && conf.getParticipants().get(0).isRinging()) {
alision806e18e2013-06-21 15:30:17 -0400526 initOutGoingCallDisplay();
527 }
528 } catch (RemoteException e) {
529 Log.e(TAG, e.toString());
530 }
alision1005ba12013-06-19 13:52:44 -0400531 }
alisiondf1dac92013-06-27 17:35:53 -0400532 if (conf.getParticipants().get(0).isOngoing()) {
alision806e18e2013-06-21 15:30:17 -0400533 initNormalStateDisplay();
534 }
alisiondf1dac92013-06-27 17:35:53 -0400535 } else if (conf.getParticipants().size() > 1) {
alision1005ba12013-06-19 13:52:44 -0400536 initNormalStateDisplay();
537 }
Adrien Béraud13cde0b2013-05-30 20:27:20 +1000538
alision1005ba12013-06-19 13:52:44 -0400539 }
Adrien Béraud13cde0b2013-05-30 20:27:20 +1000540
Alexandre Lision2b237922013-09-09 16:23:02 -0400541 public void makeTransfer(Bubble contact) {
542 FragmentManager fm = getFragmentManager();
543 TransferDFragment editName = new TransferDFragment();
544
545 Bundle b = new Bundle();
546 b.putParcelableArrayList("calls", new ArrayList<Conference>());
547 b.putParcelable("call_selected", contact.associated_call);
548 editName.setArguments(b);
549 editName.setTargetFragment(this, REQUEST_TRANSFER);
550 editName.show(fm, "");
Alexandre Lision2b237922013-09-09 16:23:02 -0400551 }
552
alision1005ba12013-06-19 13:52:44 -0400553 @Override
554 public void surfaceCreated(SurfaceHolder holder) {
555 }
Adrien Béraud13cde0b2013-05-30 20:27:20 +1000556
alision1005ba12013-06-19 13:52:44 -0400557 @Override
558 public void surfaceDestroyed(SurfaceHolder holder) {
559 }
Adrien Béraud13cde0b2013-05-30 20:27:20 +1000560
alisione38001f2013-06-04 14:14:39 -0400561 public BubblesView getBubbleView() {
562 return view;
alisione38001f2013-06-04 14:14:39 -0400563
alision1005ba12013-06-19 13:52:44 -0400564 }
Alexandre Lision0c384512013-09-17 17:15:57 -0400565
566 public void updateTime() {
Adrien Béraude78d06f2013-09-19 13:33:44 +1000567 long duration = System.currentTimeMillis() / 1000 - this.conf.getParticipants().get(0).getTimestamp_start();
Alexandre Lisionf1850c02013-09-23 14:19:34 -0400568 callStatusTxt.setText(String.format("%d:%02d:%02d", duration / 3600, duration % 3600 / 60, duration % 60));
569
570 }
571
572 @Override
573 public void onAccuracyChanged(Sensor sensor, int accuracy) {
574 Log.i(TAG, "onAccuracyChanged");
575
576 }
577
578 @Override
579 public void onSensorChanged(SensorEvent event) {
580 Log.i(TAG, "onSensorChanged:" + event.sensor.getName());
581 Log.i(TAG, "onSensorChanged:" + event.sensor.getType());
582 if (event.values[0] == 0) {
583// PowerManager pm = (PowerManager) getActivity().getSystemService(Context.POWER_SERVICE);
584// pm.goToSleep(SystemClock.uptimeMillis());
585 WindowManager.LayoutParams params = getActivity().getWindow().getAttributes();
586 params.flags |= WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON;
587 params.screenBrightness = 0.004f;
588 getActivity().getWindow().setAttributes(params);
589// Settings.System.putInt(getActivity().getContentResolver(), Settings.System.SCREEN_OFF_TIMEOUT, 1000);
590 } else {
591 WindowManager.LayoutParams params = getActivity().getWindow().getAttributes();
592 params.flags = WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON;
593 params.screenBrightness = 1f;
594 getActivity().getWindow().setAttributes(params);
595 }
596 // Sensor.TYPE_PROXIMITY
Adrien Béraude78d06f2013-09-19 13:33:44 +1000597
Alexandre Lision0c384512013-09-17 17:15:57 -0400598 }
Alexandre Lision0edf18c2013-09-23 17:35:50 -0400599
600 public Conference getConference() {
601 return conf;
602 }
alision84813a12013-05-27 17:40:39 -0400603}