blob: f0091c32c7f95db2605fa90833ddf1ce5b786d16 [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;
Alexandre Lision64dc8c02013-09-25 15:32:25 -040050import android.text.Editable;
51import android.text.method.KeyListener;
alision84813a12013-05-27 17:40:39 -040052import android.util.Log;
Alexandre Lision64dc8c02013-09-25 15:32:25 -040053import android.view.KeyEvent;
alision84813a12013-05-27 17:40:39 -040054import android.view.LayoutInflater;
Adrien Béraud13cde0b2013-05-30 20:27:20 +100055import android.view.SurfaceHolder;
56import android.view.SurfaceHolder.Callback;
alision84813a12013-05-27 17:40:39 -040057import android.view.View;
Alexandre Lision84208a32013-09-25 13:18:37 -040058import android.view.View.OnClickListener;
Alexandre Lision64dc8c02013-09-25 15:32:25 -040059import android.view.View.OnKeyListener;
alision84813a12013-05-27 17:40:39 -040060import android.view.ViewGroup;
Alexandre Lision84208a32013-09-25 13:18:37 -040061import android.view.inputmethod.InputMethodManager;
62import android.widget.ImageButton;
Alexandre Lision3c6b7102013-09-16 16:56:46 -040063import android.widget.TextView;
alision84813a12013-05-27 17:40:39 -040064
65import com.savoirfairelinux.sflphone.R;
alision84813a12013-05-27 17:40:39 -040066import com.savoirfairelinux.sflphone.model.Attractor;
67import com.savoirfairelinux.sflphone.model.Bubble;
68import com.savoirfairelinux.sflphone.model.BubbleModel;
69import com.savoirfairelinux.sflphone.model.BubblesView;
alisiondf1dac92013-06-27 17:35:53 -040070import com.savoirfairelinux.sflphone.model.Conference;
alision84813a12013-05-27 17:40:39 -040071import com.savoirfairelinux.sflphone.model.SipCall;
alision84813a12013-05-27 17:40:39 -040072import com.savoirfairelinux.sflphone.service.ISipService;
73
Alexandre Lisionf1850c02013-09-23 14:19:34 -040074public class CallFragment extends Fragment implements Callback, SensorEventListener {
alision84813a12013-05-27 17:40:39 -040075
alision1005ba12013-06-19 13:52:44 -040076 static final String TAG = "CallFragment";
alision84813a12013-05-27 17:40:39 -040077
alisionf57d8a62013-07-02 09:37:12 -040078 float BUBBLE_SIZE = 75;
alision1005ba12013-06-19 13:52:44 -040079 static final float ATTRACTOR_SIZE = 40;
alision84813a12013-05-27 17:40:39 -040080
Alexandre Lision2b237922013-09-09 16:23:02 -040081 public static final int REQUEST_TRANSFER = 10;
82
alisiondf1dac92013-06-27 17:35:53 -040083 private Conference conf;
alision84813a12013-05-27 17:40:39 -040084
Alexandre Lision3c6b7102013-09-16 16:56:46 -040085 private TextView callStatusTxt;
alision1005ba12013-06-19 13:52:44 -040086 private BubblesView view;
87 private BubbleModel model;
alision84813a12013-05-27 17:40:39 -040088
alision1005ba12013-06-19 13:52:44 -040089 private Callbacks mCallbacks = sDummyCallbacks;
alision84813a12013-05-27 17:40:39 -040090
alision806e18e2013-06-21 15:30:17 -040091 private SipCall myself;
alision85704182013-05-29 15:23:03 -040092
Alexandre Lisiona764c682013-09-09 10:02:07 -040093 boolean accepted = false;
alision1005ba12013-06-19 13:52:44 -040094 private Bitmap call_icon;
alision85704182013-05-29 15:23:03 -040095
Alexandre Lisionf1850c02013-09-23 14:19:34 -040096 private SensorManager mSensorManager;
97
98 private Sensor mSensor;
99
alision1005ba12013-06-19 13:52:44 -0400100 @Override
101 public void onCreate(Bundle savedBundle) {
102 super.onCreate(savedBundle);
alision1005ba12013-06-19 13:52:44 -0400103 Bundle b = getArguments();
alisioncd8fb912013-06-28 14:43:51 -0400104 conf = new Conference((Conference) b.getParcelable("conference"));
alisiondf1dac92013-06-27 17:35:53 -0400105 model = new BubbleModel(getResources().getDisplayMetrics().density);
alision729b0a22013-07-02 11:57:33 -0400106 BUBBLE_SIZE = getResources().getDimension(R.dimen.bubble_size);
Alexandre Lisiona764c682013-09-09 10:02:07 -0400107 Log.e(TAG, "BUBBLE_SIZE " + BUBBLE_SIZE);
Alexandre Lisionf1850c02013-09-23 14:19:34 -0400108 mSensorManager = (SensorManager) getActivity().getSystemService(Context.SENSOR_SERVICE);
109 mSensor = mSensorManager.getDefaultSensor(Sensor.TYPE_PROXIMITY);
alision729b0a22013-07-02 11:57:33 -0400110
alision1005ba12013-06-19 13:52:44 -0400111 }
alision85992112013-05-29 12:18:08 -0400112
alision1005ba12013-06-19 13:52:44 -0400113 /**
114 * A dummy implementation of the {@link Callbacks} interface that does nothing. Used only when this fragment is not attached to an activity.
115 */
116 private static Callbacks sDummyCallbacks = new Callbacks() {
117 @Override
118 public void onSendMessage(SipCall call, String msg) {
119 }
alision85992112013-05-29 12:18:08 -0400120
alision1005ba12013-06-19 13:52:44 -0400121 @Override
122 public void callContact(SipCall call) {
123 }
alision85992112013-05-29 12:18:08 -0400124
alision1005ba12013-06-19 13:52:44 -0400125 @Override
126 public void onCallAccepted(SipCall call) {
127 }
alision85704182013-05-29 15:23:03 -0400128
alision1005ba12013-06-19 13:52:44 -0400129 @Override
130 public void onCallRejected(SipCall call) {
131 }
alision85992112013-05-29 12:18:08 -0400132
alision1005ba12013-06-19 13:52:44 -0400133 @Override
134 public void onCallEnded(SipCall call) {
135 }
alision85704182013-05-29 15:23:03 -0400136
alision1005ba12013-06-19 13:52:44 -0400137 @Override
138 public void onCallSuspended(SipCall call) {
139 }
alision85992112013-05-29 12:18:08 -0400140
alision1005ba12013-06-19 13:52:44 -0400141 @Override
142 public void onCallResumed(SipCall call) {
143 }
alision85704182013-05-29 15:23:03 -0400144
alision1005ba12013-06-19 13:52:44 -0400145 @Override
146 public void onCalltransfered(SipCall call, String to) {
147 }
alision85992112013-05-29 12:18:08 -0400148
alision1005ba12013-06-19 13:52:44 -0400149 @Override
150 public void onRecordCall(SipCall call) {
151 }
alision85704182013-05-29 15:23:03 -0400152
alision1005ba12013-06-19 13:52:44 -0400153 @Override
154 public ISipService getService() {
155 return null;
156 }
alisiondf1dac92013-06-27 17:35:53 -0400157
158 @Override
159 public void replaceCurrentCallDisplayed() {
160 }
Alexandre Lision0c384512013-09-17 17:15:57 -0400161
162 @Override
Adrien Béraude78d06f2013-09-19 13:33:44 +1000163 public void startTimer() {
Alexandre Lision0c384512013-09-17 17:15:57 -0400164 }
alision1005ba12013-06-19 13:52:44 -0400165 };
alision907bde72013-06-20 14:40:37 -0400166
alision1005ba12013-06-19 13:52:44 -0400167 /**
168 * The Activity calling this fragment has to implement this interface
169 *
170 */
171 public interface Callbacks {
alision85992112013-05-29 12:18:08 -0400172
alision1005ba12013-06-19 13:52:44 -0400173 public ISipService getService();
alision85704182013-05-29 15:23:03 -0400174
alision1005ba12013-06-19 13:52:44 -0400175 public void callContact(SipCall call);
alision85992112013-05-29 12:18:08 -0400176
alision1005ba12013-06-19 13:52:44 -0400177 public void onCallAccepted(SipCall call);
alision85704182013-05-29 15:23:03 -0400178
alision1005ba12013-06-19 13:52:44 -0400179 public void onCallRejected(SipCall call);
alision85992112013-05-29 12:18:08 -0400180
alision1005ba12013-06-19 13:52:44 -0400181 public void onCallEnded(SipCall call);
alision85704182013-05-29 15:23:03 -0400182
alision1005ba12013-06-19 13:52:44 -0400183 public void onCallSuspended(SipCall call);
alision85704182013-05-29 15:23:03 -0400184
alision1005ba12013-06-19 13:52:44 -0400185 public void onCallResumed(SipCall call);
alision84813a12013-05-27 17:40:39 -0400186
alision1005ba12013-06-19 13:52:44 -0400187 public void onCalltransfered(SipCall call, String to);
alision84813a12013-05-27 17:40:39 -0400188
alision1005ba12013-06-19 13:52:44 -0400189 public void onRecordCall(SipCall call);
alision85704182013-05-29 15:23:03 -0400190
alision1005ba12013-06-19 13:52:44 -0400191 public void onSendMessage(SipCall call, String msg);
alisiondf1dac92013-06-27 17:35:53 -0400192
193 public void replaceCurrentCallDisplayed();
Alexandre Lision0c384512013-09-17 17:15:57 -0400194
195 public void startTimer();
alision1005ba12013-06-19 13:52:44 -0400196 }
alision85992112013-05-29 12:18:08 -0400197
alision1005ba12013-06-19 13:52:44 -0400198 @Override
199 public void onAttach(Activity activity) {
200 super.onAttach(activity);
alision85992112013-05-29 12:18:08 -0400201
alision1005ba12013-06-19 13:52:44 -0400202 if (!(activity instanceof Callbacks)) {
203 throw new IllegalStateException("Activity must implement fragment's callbacks.");
204 }
alision85992112013-05-29 12:18:08 -0400205
alision1005ba12013-06-19 13:52:44 -0400206 // rootView.requestDisallowInterceptTouchEvent(true);
alision85992112013-05-29 12:18:08 -0400207
alision1005ba12013-06-19 13:52:44 -0400208 mCallbacks = (Callbacks) activity;
alision50fa0722013-06-25 17:29:44 -0400209 myself = SipCall.SipCallBuilder.buildMyselfCall(activity.getContentResolver(), "Me");
Adrien Béraude78d06f2013-09-19 13:33:44 +1000210
alision1005ba12013-06-19 13:52:44 -0400211 }
alision85992112013-05-29 12:18:08 -0400212
alision1005ba12013-06-19 13:52:44 -0400213 @Override
214 public void onDetach() {
215 super.onDetach();
216 mCallbacks = sDummyCallbacks;
Alexandre Lisionf1850c02013-09-23 14:19:34 -0400217 mSensorManager.unregisterListener(this);
alision1005ba12013-06-19 13:52:44 -0400218 }
alision907bde72013-06-20 14:40:37 -0400219
alision1005ba12013-06-19 13:52:44 -0400220 @Override
alision907bde72013-06-20 14:40:37 -0400221 public void onStop() {
alision1005ba12013-06-19 13:52:44 -0400222 super.onStop();
223 }
alision85992112013-05-29 12:18:08 -0400224
alision1005ba12013-06-19 13:52:44 -0400225 @Override
Alexandre Lisionf1850c02013-09-23 14:19:34 -0400226 public void onResume() {
227 super.onResume();
228 mSensorManager.registerListener(this, mSensor, SensorManager.SENSOR_DELAY_NORMAL);
229 }
230
231 @Override
232 public void onPause() {
233 super.onPause();
234 mSensorManager.unregisterListener(this);
235 }
236
237 @Override
Alexandre Lision2b237922013-09-09 16:23:02 -0400238 public void onActivityResult(int requestCode, int resultCode, Intent data) {
239 super.onActivityResult(requestCode, resultCode, data);
240 SipCall transfer = null;
241 if (requestCode == REQUEST_TRANSFER) {
242 switch (resultCode) {
Adrien Béraude78d06f2013-09-19 13:33:44 +1000243 case TransferDFragment.RESULT_TRANSFER_CONF:
Alexandre Lision2b237922013-09-09 16:23:02 -0400244 Conference c = data.getParcelableExtra("target");
245 transfer = data.getParcelableExtra("transfer");
246 try {
247
248 mCallbacks.getService().attendedTransfer(transfer.getCallId(), c.getParticipants().get(0).getCallId());
249
250 } catch (RemoteException e) {
251 // TODO Auto-generated catch block
252 e.printStackTrace();
253 }
Alexandre Lision6deda412013-09-25 13:21:22 -0400254 // Toast.makeText(getActivity(), "Transfer complete", Toast.LENGTH_LONG).show();
Alexandre Lision2b237922013-09-09 16:23:02 -0400255 break;
256
Adrien Béraude78d06f2013-09-19 13:33:44 +1000257 case TransferDFragment.RESULT_TRANSFER_NUMBER:
Alexandre Lision2b237922013-09-09 16:23:02 -0400258 String to = data.getStringExtra("to_number");
259 transfer = data.getParcelableExtra("transfer");
260 try {
Alexandre Lision6deda412013-09-25 13:21:22 -0400261 // Toast.makeText(getActivity(), "Transferring " + transfer.getContact().getmDisplayName() + " to " + to,
262 // Toast.LENGTH_SHORT).show();
Alexandre Lision2b237922013-09-09 16:23:02 -0400263 mCallbacks.getService().transfer(transfer.getCallId(), to);
264 mCallbacks.getService().hangUp(transfer.getCallId());
265 } catch (RemoteException e) {
266 // TODO Auto-generated catch block
267 e.printStackTrace();
268 }
269 break;
Adrien Béraud97512842013-09-19 13:37:19 +1000270 case Activity.RESULT_CANCELED:
Alexandre Lision2b237922013-09-09 16:23:02 -0400271 default:
Adrien Béraude78d06f2013-09-19 13:33:44 +1000272 model.clear();
273 initNormalStateDisplay();
Alexandre Lision2b237922013-09-09 16:23:02 -0400274 break;
275 }
276 }
277 }
278
279 @Override
alision1005ba12013-06-19 13:52:44 -0400280 public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
Alexandre Lision84208a32013-09-25 13:18:37 -0400281 final ViewGroup rootView = (ViewGroup) inflater.inflate(R.layout.frag_call, container, false);
alision85992112013-05-29 12:18:08 -0400282
alision1005ba12013-06-19 13:52:44 -0400283 view = (BubblesView) rootView.findViewById(R.id.main_view);
alision34673e62013-06-25 14:40:07 -0400284 view.setFragment(this);
alision1005ba12013-06-19 13:52:44 -0400285 view.setModel(model);
286 view.getHolder().addCallback(this);
alision84813a12013-05-27 17:40:39 -0400287
Alexandre Lision3c6b7102013-09-16 16:56:46 -0400288 callStatusTxt = (TextView) rootView.findViewById(R.id.call_status_txt);
alision1005ba12013-06-19 13:52:44 -0400289 call_icon = BitmapFactory.decodeResource(getResources(), R.drawable.ic_call);
alision84813a12013-05-27 17:40:39 -0400290
Alexandre Lision84208a32013-09-25 13:18:37 -0400291 ((ImageButton) rootView.findViewById(R.id.dialpad_btn)).setOnClickListener(new OnClickListener() {
292
293 @Override
294 public void onClick(View v) {
295 InputMethodManager lManager = (InputMethodManager) getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
296 lManager.toggleSoftInput(InputMethodManager.SHOW_IMPLICIT, InputMethodManager.HIDE_IMPLICIT_ONLY);
297 }
298 });
299
Alexandre Lision64dc8c02013-09-25 15:32:25 -0400300
301
alision1005ba12013-06-19 13:52:44 -0400302 return rootView;
303 }
Alexandre Lision64dc8c02013-09-25 15:32:25 -0400304
alision1005ba12013-06-19 13:52:44 -0400305 private void initNormalStateDisplay() {
306 Log.i(TAG, "Start normal display");
alision84813a12013-05-27 17:40:39 -0400307
Alexandre Lision0c384512013-09-17 17:15:57 -0400308 mCallbacks.startTimer();
alision84813a12013-05-27 17:40:39 -0400309
alision1005ba12013-06-19 13:52:44 -0400310 getBubbleFor(myself, model.width / 2, model.height / 2);
alision907bde72013-06-20 14:40:37 -0400311
alisiondf1dac92013-06-27 17:35:53 -0400312 int angle_part = 360 / conf.getParticipants().size();
alision806e18e2013-06-21 15:30:17 -0400313 double dX = 0;
314 double dY = 0;
alision465ceba2013-07-04 09:24:30 -0400315 int radiusCalls = (int) (model.width / 2 - BUBBLE_SIZE);
alisiondf1dac92013-06-27 17:35:53 -0400316 for (int i = 0; i < conf.getParticipants().size(); ++i) {
317
318 if (conf.getParticipants().get(i) == null) {
319 Log.i(TAG, i + " null ");
320 continue;
321 }
alision34673e62013-06-25 14:40:07 -0400322 dX = Math.cos(Math.toRadians(angle_part * i - 90)) * radiusCalls;
323 dY = Math.sin(Math.toRadians(angle_part * i - 90)) * radiusCalls;
alisiondf1dac92013-06-27 17:35:53 -0400324 getBubbleFor(conf.getParticipants().get(i), (int) (model.width / 2 + dX), (int) (model.height / 2 + dY));
alision806e18e2013-06-21 15:30:17 -0400325 }
alision1005ba12013-06-19 13:52:44 -0400326
327 model.clearAttractors();
alision1005ba12013-06-19 13:52:44 -0400328 }
alision85704182013-05-29 15:23:03 -0400329
alision1005ba12013-06-19 13:52:44 -0400330 private void initIncomingCallDisplay() {
331 Log.i(TAG, "Start incoming display");
alision84813a12013-05-27 17:40:39 -0400332
Alexandre Lision0c384512013-09-17 17:15:57 -0400333 mCallbacks.startTimer();
alision84813a12013-05-27 17:40:39 -0400334
Alexandre Lision23628c12013-09-24 11:17:05 -0400335 int radiusCalls = (int) (model.width / 2 - BUBBLE_SIZE);
336 getBubbleFor(myself, model.width / 2, model.height / 2 + radiusCalls);
337 getBubbleFor(conf.getParticipants().get(0), model.width / 2, model.height / 2 - radiusCalls);
alision85704182013-05-29 15:23:03 -0400338
alision1005ba12013-06-19 13:52:44 -0400339 model.clearAttractors();
Alexandre Lision23628c12013-09-24 11:17:05 -0400340 model.addAttractor(new Attractor(new PointF(model.width / 2, model.height / 2), ATTRACTOR_SIZE, new Attractor.Callback() {
alision1005ba12013-06-19 13:52:44 -0400341 @Override
342 public boolean onBubbleSucked(Bubble b) {
Alexandre Lisiona764c682013-09-09 10:02:07 -0400343
344 if (!accepted) {
345 mCallbacks.onCallAccepted(conf.getParticipants().get(0));
346 accepted = true;
347 }
alision1005ba12013-06-19 13:52:44 -0400348 return false;
349 }
350 }, call_icon));
alision1005ba12013-06-19 13:52:44 -0400351 }
alision85704182013-05-29 15:23:03 -0400352
alision1005ba12013-06-19 13:52:44 -0400353 private void initOutGoingCallDisplay() {
354 Log.i(TAG, "Start outgoing display");
alision85704182013-05-29 15:23:03 -0400355
Alexandre Lision0c384512013-09-17 17:15:57 -0400356 mCallbacks.startTimer();
alision85704182013-05-29 15:23:03 -0400357
Adrien Béraude78d06f2013-09-19 13:33:44 +1000358 getBubbleFor(myself, model.width / 2, model.height / 2);
alision85704182013-05-29 15:23:03 -0400359
alisiondf1dac92013-06-27 17:35:53 -0400360 // TODO off-thread image loading
361 int angle_part = 360 / conf.getParticipants().size();
362 double dX = 0;
363 double dY = 0;
Adrien Béraude78d06f2013-09-19 13:33:44 +1000364 int radiusCalls = (int) (model.width / 2 - BUBBLE_SIZE);
alisiondf1dac92013-06-27 17:35:53 -0400365 for (int i = 0; i < conf.getParticipants().size(); ++i) {
366 dX = Math.cos(Math.toRadians(angle_part * i - 90)) * radiusCalls;
367 dY = Math.sin(Math.toRadians(angle_part * i - 90)) * radiusCalls;
368 getBubbleFor(conf.getParticipants().get(i), (int) (model.width / 2 + dX), (int) (model.height / 2 + dY));
369 }
alision907bde72013-06-20 14:40:37 -0400370
alision1005ba12013-06-19 13:52:44 -0400371 model.clearAttractors();
alision1005ba12013-06-19 13:52:44 -0400372 }
alision85704182013-05-29 15:23:03 -0400373
alision1005ba12013-06-19 13:52:44 -0400374 /**
375 * Retrieves or create a bubble for a given contact. If the bubble exists, it is moved to the new location.
376 *
alision806e18e2013-06-21 15:30:17 -0400377 * @param call
378 * The call associated to a contact
alision1005ba12013-06-19 13:52:44 -0400379 * @param x
380 * Initial or new x position.
381 * @param y
382 * Initial or new y position.
383 * @return Bubble corresponding to the contact.
384 */
alision806e18e2013-06-21 15:30:17 -0400385 private Bubble getBubbleFor(SipCall call, float x, float y) {
alisiondf1dac92013-06-27 17:35:53 -0400386 Bubble contact_bubble = model.getBubble(call);
alision1005ba12013-06-19 13:52:44 -0400387 if (contact_bubble != null) {
388 contact_bubble.attractor.set(x, y);
389 return contact_bubble;
390 }
alision84813a12013-05-27 17:40:39 -0400391
alision34673e62013-06-25 14:40:07 -0400392 contact_bubble = new Bubble(getActivity(), call, x, y, BUBBLE_SIZE);
alision84813a12013-05-27 17:40:39 -0400393
alision1005ba12013-06-19 13:52:44 -0400394 model.addBubble(contact_bubble);
alision1005ba12013-06-19 13:52:44 -0400395 return contact_bubble;
396 }
Adrien Béraud0c9bd8f2013-05-30 16:16:57 +1000397
alision1005ba12013-06-19 13:52:44 -0400398 /**
399 * Should be called when a bubble is removed from the model
400 */
401 void bubbleRemoved(Bubble b) {
alision806e18e2013-06-21 15:30:17 -0400402 if (b.associated_call == null) {
alision1005ba12013-06-19 13:52:44 -0400403 return;
404 }
alision1005ba12013-06-19 13:52:44 -0400405 }
Adrien Béraudc9c424d2013-05-30 17:47:35 +1000406
alision1005ba12013-06-19 13:52:44 -0400407 public void changeCallState(String callID, String newState) {
Alexandre Lisionf1850c02013-09-23 14:19:34 -0400408 Log.w(TAG, "Call :" + callID + " " + newState);
alisiondf1dac92013-06-27 17:35:53 -0400409 if (newState.contentEquals("FAILURE")) {
410 try {
411 mCallbacks.getService().hangUp(callID);
412 } catch (RemoteException e) {
413 e.printStackTrace();
alision806e18e2013-06-21 15:30:17 -0400414 }
alision1005ba12013-06-19 13:52:44 -0400415 }
alisiondf1dac92013-06-27 17:35:53 -0400416 for (int i = 0; i < conf.getParticipants().size(); ++i) {
alisiondf1dac92013-06-27 17:35:53 -0400417 if (callID.equals(conf.getParticipants().get(i).getCallId())) {
418 if (newState.contentEquals("HUNGUP")) {
alisiondf1dac92013-06-27 17:35:53 -0400419 model.removeBubble(conf.getParticipants().get(i));
420 conf.getParticipants().remove(i);
421 } else {
422 conf.getParticipants().get(i).setCallState(newState);
423 }
424 }
425 }
426
427 if (conf.isOnGoing())
428 initNormalStateDisplay();
429
430 if (conf.getParticipants().size() == 0) {
431 mCallbacks.replaceCurrentCallDisplayed();
432 }
433
alision1005ba12013-06-19 13:52:44 -0400434 }
alision84813a12013-05-27 17:40:39 -0400435
alision1005ba12013-06-19 13:52:44 -0400436 public boolean draggingBubble() {
437 return view == null ? false : view.isDraggingBubble();
438 }
Adrien Béraudc9c424d2013-05-30 17:47:35 +1000439
alision1005ba12013-06-19 13:52:44 -0400440 @Override
441 public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
Adrien Béraud13cde0b2013-05-30 20:27:20 +1000442
alisiondf1dac92013-06-27 17:35:53 -0400443 if (conf.getParticipants().size() == 1) {
alisiondf1dac92013-06-27 17:35:53 -0400444 if (conf.getParticipants().get(0).isIncoming() && conf.getParticipants().get(0).isRinging()) {
alision806e18e2013-06-21 15:30:17 -0400445 initIncomingCallDisplay();
446 } else {
alisiondf1dac92013-06-27 17:35:53 -0400447 if (conf.getParticipants().get(0).isRinging()) {
alision1005ba12013-06-19 13:52:44 -0400448 initOutGoingCallDisplay();
449 }
alision806e18e2013-06-21 15:30:17 -0400450 try {
alisiondf1dac92013-06-27 17:35:53 -0400451 if (conf.getParticipants().get(0).isOutGoing()
452 && mCallbacks.getService().getCall(conf.getParticipants().get(0).getCallId()) == null) {
453 mCallbacks.getService().placeCall(conf.getParticipants().get(0));
alision806e18e2013-06-21 15:30:17 -0400454 initOutGoingCallDisplay();
alisiondf1dac92013-06-27 17:35:53 -0400455 } else if (conf.getParticipants().get(0).isOutGoing() && conf.getParticipants().get(0).isRinging()) {
alision806e18e2013-06-21 15:30:17 -0400456 initOutGoingCallDisplay();
457 }
458 } catch (RemoteException e) {
459 Log.e(TAG, e.toString());
460 }
alision1005ba12013-06-19 13:52:44 -0400461 }
alisiondf1dac92013-06-27 17:35:53 -0400462 if (conf.getParticipants().get(0).isOngoing()) {
alision806e18e2013-06-21 15:30:17 -0400463 initNormalStateDisplay();
464 }
alisiondf1dac92013-06-27 17:35:53 -0400465 } else if (conf.getParticipants().size() > 1) {
alision1005ba12013-06-19 13:52:44 -0400466 initNormalStateDisplay();
467 }
Adrien Béraud13cde0b2013-05-30 20:27:20 +1000468
alision1005ba12013-06-19 13:52:44 -0400469 }
Adrien Béraud13cde0b2013-05-30 20:27:20 +1000470
Alexandre Lision2b237922013-09-09 16:23:02 -0400471 public void makeTransfer(Bubble contact) {
472 FragmentManager fm = getFragmentManager();
473 TransferDFragment editName = new TransferDFragment();
474
475 Bundle b = new Bundle();
476 b.putParcelableArrayList("calls", new ArrayList<Conference>());
477 b.putParcelable("call_selected", contact.associated_call);
478 editName.setArguments(b);
479 editName.setTargetFragment(this, REQUEST_TRANSFER);
480 editName.show(fm, "");
Alexandre Lision2b237922013-09-09 16:23:02 -0400481 }
482
alision1005ba12013-06-19 13:52:44 -0400483 @Override
484 public void surfaceCreated(SurfaceHolder holder) {
485 }
Adrien Béraud13cde0b2013-05-30 20:27:20 +1000486
alision1005ba12013-06-19 13:52:44 -0400487 @Override
488 public void surfaceDestroyed(SurfaceHolder holder) {
Alexandre Lision84208a32013-09-25 13:18:37 -0400489 // check that soft input is hidden
490 InputMethodManager lManager = (InputMethodManager) getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
491 lManager.hideSoftInputFromWindow(view.getWindowToken(), 0);
alision1005ba12013-06-19 13:52:44 -0400492 }
Adrien Béraud13cde0b2013-05-30 20:27:20 +1000493
alisione38001f2013-06-04 14:14:39 -0400494 public BubblesView getBubbleView() {
495 return view;
alisione38001f2013-06-04 14:14:39 -0400496
alision1005ba12013-06-19 13:52:44 -0400497 }
Alexandre Lision0c384512013-09-17 17:15:57 -0400498
499 public void updateTime() {
Adrien Béraude78d06f2013-09-19 13:33:44 +1000500 long duration = System.currentTimeMillis() / 1000 - this.conf.getParticipants().get(0).getTimestamp_start();
Alexandre Lisionf1850c02013-09-23 14:19:34 -0400501 callStatusTxt.setText(String.format("%d:%02d:%02d", duration / 3600, duration % 3600 / 60, duration % 60));
502
503 }
504
505 @Override
506 public void onAccuracyChanged(Sensor sensor, int accuracy) {
507 Log.i(TAG, "onAccuracyChanged");
508
509 }
510
511 @Override
512 public void onSensorChanged(SensorEvent event) {
513 Log.i(TAG, "onSensorChanged:" + event.sensor.getName());
514 Log.i(TAG, "onSensorChanged:" + event.sensor.getType());
Alexandre Lision64dc8c02013-09-25 15:32:25 -0400515// if (event.values[0] == 0) {
516// // PowerManager pm = (PowerManager) getActivity().getSystemService(Context.POWER_SERVICE);
517// // pm.goToSleep(SystemClock.uptimeMillis());
518// WindowManager.LayoutParams params = getActivity().getWindow().getAttributes();
519// params.flags |= WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON;
520// params.screenBrightness = 0.004f;
521// getActivity().getWindow().setAttributes(params);
522// // Settings.System.putInt(getActivity().getContentResolver(), Settings.System.SCREEN_OFF_TIMEOUT, 1000);
523// } else {
524// WindowManager.LayoutParams params = getActivity().getWindow().getAttributes();
525// params.flags = WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON;
526// params.screenBrightness = 1f;
527// getActivity().getWindow().setAttributes(params);
528// }
Alexandre Lisionf1850c02013-09-23 14:19:34 -0400529 // Sensor.TYPE_PROXIMITY
Adrien Béraude78d06f2013-09-19 13:33:44 +1000530
Alexandre Lision0c384512013-09-17 17:15:57 -0400531 }
Alexandre Lision0edf18c2013-09-23 17:35:50 -0400532
533 public Conference getConference() {
534 return conf;
535 }
Alexandre Lision64dc8c02013-09-25 15:32:25 -0400536
537 public void onKeyUp(int keyCode, KeyEvent event) {
538 try {
539 Log.d(TAG, "Sending "+event.getDisplayLabel());
540 String toSend = "" + event.getDisplayLabel();
541 mCallbacks.getService().playDtmf(toSend);
542 } catch (RemoteException e) {
543 e.printStackTrace();
544 }
545 }
alision84813a12013-05-27 17:40:39 -0400546}