blob: 9c04440d3f6fa15605c6ce70f669d4cbf164f01c [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;
Alexandre Lision31f46fc2013-09-26 11:19:54 -040035import java.util.Locale;
Alexandre Lision2b237922013-09-09 16:23:02 -040036
alision84813a12013-05-27 17:40:39 -040037import android.app.Activity;
38import android.app.Fragment;
Alexandre Lision2b237922013-09-09 16:23:02 -040039import android.app.FragmentManager;
Alexandre Lisionf1850c02013-09-23 14:19:34 -040040import android.content.Context;
Alexandre Lision2b237922013-09-09 16:23:02 -040041import android.content.Intent;
alision84813a12013-05-27 17:40:39 -040042import android.graphics.Bitmap;
Adrien Béraud0c9bd8f2013-05-30 16:16:57 +100043import android.graphics.BitmapFactory;
alision84813a12013-05-27 17:40:39 -040044import android.graphics.PointF;
Alexandre Lisionf1850c02013-09-23 14:19:34 -040045import android.hardware.Sensor;
46import android.hardware.SensorEvent;
47import android.hardware.SensorEventListener;
48import android.hardware.SensorManager;
alision84813a12013-05-27 17:40:39 -040049import android.os.Bundle;
50import android.os.RemoteException;
alision84813a12013-05-27 17:40:39 -040051import android.util.Log;
Alexandre Lision64dc8c02013-09-25 15:32:25 -040052import android.view.KeyEvent;
alision84813a12013-05-27 17:40:39 -040053import android.view.LayoutInflater;
Adrien Béraud13cde0b2013-05-30 20:27:20 +100054import android.view.SurfaceHolder;
55import android.view.SurfaceHolder.Callback;
alision84813a12013-05-27 17:40:39 -040056import android.view.View;
Alexandre Lision84208a32013-09-25 13:18:37 -040057import android.view.View.OnClickListener;
alision84813a12013-05-27 17:40:39 -040058import android.view.ViewGroup;
Alexandre Lisionc30e8412013-09-26 15:12:59 -040059import android.view.WindowManager;
Alexandre Lision84208a32013-09-25 13:18:37 -040060import android.view.inputmethod.InputMethodManager;
61import android.widget.ImageButton;
Alexandre Lision3c6b7102013-09-16 16:56:46 -040062import android.widget.TextView;
alision84813a12013-05-27 17:40:39 -040063
64import com.savoirfairelinux.sflphone.R;
alision84813a12013-05-27 17:40:39 -040065import com.savoirfairelinux.sflphone.model.Attractor;
66import com.savoirfairelinux.sflphone.model.Bubble;
67import com.savoirfairelinux.sflphone.model.BubbleModel;
68import com.savoirfairelinux.sflphone.model.BubblesView;
alisiondf1dac92013-06-27 17:35:53 -040069import com.savoirfairelinux.sflphone.model.Conference;
alision84813a12013-05-27 17:40:39 -040070import com.savoirfairelinux.sflphone.model.SipCall;
alision84813a12013-05-27 17:40:39 -040071import com.savoirfairelinux.sflphone.service.ISipService;
72
Alexandre Lisionf1850c02013-09-23 14:19:34 -040073public class CallFragment extends Fragment implements Callback, SensorEventListener {
alision84813a12013-05-27 17:40:39 -040074
alision1005ba12013-06-19 13:52:44 -040075 static final String TAG = "CallFragment";
alision84813a12013-05-27 17:40:39 -040076
alisionf57d8a62013-07-02 09:37:12 -040077 float BUBBLE_SIZE = 75;
alision1005ba12013-06-19 13:52:44 -040078 static final float ATTRACTOR_SIZE = 40;
alision84813a12013-05-27 17:40:39 -040079
Alexandre Lision2b237922013-09-09 16:23:02 -040080 public static final int REQUEST_TRANSFER = 10;
81
alisiondf1dac92013-06-27 17:35:53 -040082 private Conference conf;
alision84813a12013-05-27 17:40:39 -040083
Alexandre Lision3c6b7102013-09-16 16:56:46 -040084 private TextView callStatusTxt;
alision1005ba12013-06-19 13:52:44 -040085 private BubblesView view;
86 private BubbleModel model;
alision84813a12013-05-27 17:40:39 -040087
alision1005ba12013-06-19 13:52:44 -040088 private Callbacks mCallbacks = sDummyCallbacks;
alision84813a12013-05-27 17:40:39 -040089
alision806e18e2013-06-21 15:30:17 -040090 private SipCall myself;
alision85704182013-05-29 15:23:03 -040091
Alexandre Lisiona764c682013-09-09 10:02:07 -040092 boolean accepted = false;
alision1005ba12013-06-19 13:52:44 -040093 private Bitmap call_icon;
alision85704182013-05-29 15:23:03 -040094
Alexandre Lisionf1850c02013-09-23 14:19:34 -040095 private SensorManager mSensorManager;
96
97 private Sensor mSensor;
Alexandre Lision0eb02032013-09-27 16:32:40 -040098
99 TransferDFragment editName;
Alexandre Lisionf1850c02013-09-23 14:19:34 -0400100
alision1005ba12013-06-19 13:52:44 -0400101 @Override
102 public void onCreate(Bundle savedBundle) {
103 super.onCreate(savedBundle);
alision1005ba12013-06-19 13:52:44 -0400104 Bundle b = getArguments();
alisioncd8fb912013-06-28 14:43:51 -0400105 conf = new Conference((Conference) b.getParcelable("conference"));
alisiondf1dac92013-06-27 17:35:53 -0400106 model = new BubbleModel(getResources().getDisplayMetrics().density);
alision729b0a22013-07-02 11:57:33 -0400107 BUBBLE_SIZE = getResources().getDimension(R.dimen.bubble_size);
Alexandre Lisiona764c682013-09-09 10:02:07 -0400108 Log.e(TAG, "BUBBLE_SIZE " + BUBBLE_SIZE);
Alexandre Lisionf1850c02013-09-23 14:19:34 -0400109 mSensorManager = (SensorManager) getActivity().getSystemService(Context.SENSOR_SERVICE);
110 mSensor = mSensorManager.getDefaultSensor(Sensor.TYPE_PROXIMITY);
alision729b0a22013-07-02 11:57:33 -0400111
alision1005ba12013-06-19 13:52:44 -0400112 }
alision85992112013-05-29 12:18:08 -0400113
alision1005ba12013-06-19 13:52:44 -0400114 /**
115 * A dummy implementation of the {@link Callbacks} interface that does nothing. Used only when this fragment is not attached to an activity.
116 */
117 private static Callbacks sDummyCallbacks = new Callbacks() {
118 @Override
119 public void onSendMessage(SipCall call, String msg) {
120 }
alision85992112013-05-29 12:18:08 -0400121
alision1005ba12013-06-19 13:52:44 -0400122 @Override
123 public void callContact(SipCall call) {
124 }
alision85992112013-05-29 12:18:08 -0400125
alision1005ba12013-06-19 13:52:44 -0400126 @Override
127 public void onCallAccepted(SipCall call) {
128 }
alision85704182013-05-29 15:23:03 -0400129
alision1005ba12013-06-19 13:52:44 -0400130 @Override
131 public void onCallRejected(SipCall call) {
132 }
alision85992112013-05-29 12:18:08 -0400133
alision1005ba12013-06-19 13:52:44 -0400134 @Override
135 public void onCallEnded(SipCall call) {
136 }
alision85704182013-05-29 15:23:03 -0400137
alision1005ba12013-06-19 13:52:44 -0400138 @Override
139 public void onCallSuspended(SipCall call) {
140 }
alision85992112013-05-29 12:18:08 -0400141
alision1005ba12013-06-19 13:52:44 -0400142 @Override
143 public void onCallResumed(SipCall call) {
144 }
alision85704182013-05-29 15:23:03 -0400145
alision1005ba12013-06-19 13:52:44 -0400146 @Override
147 public void onCalltransfered(SipCall call, String to) {
148 }
alision85992112013-05-29 12:18:08 -0400149
alision1005ba12013-06-19 13:52:44 -0400150 @Override
151 public void onRecordCall(SipCall call) {
152 }
alision85704182013-05-29 15:23:03 -0400153
alision1005ba12013-06-19 13:52:44 -0400154 @Override
155 public ISipService getService() {
156 return null;
157 }
alisiondf1dac92013-06-27 17:35:53 -0400158
159 @Override
160 public void replaceCurrentCallDisplayed() {
161 }
Alexandre Lision0c384512013-09-17 17:15:57 -0400162
163 @Override
Adrien Béraude78d06f2013-09-19 13:33:44 +1000164 public void startTimer() {
Alexandre Lision0c384512013-09-17 17:15:57 -0400165 }
alision1005ba12013-06-19 13:52:44 -0400166 };
alision907bde72013-06-20 14:40:37 -0400167
alision1005ba12013-06-19 13:52:44 -0400168 /**
169 * The Activity calling this fragment has to implement this interface
170 *
171 */
172 public interface Callbacks {
alision85992112013-05-29 12:18:08 -0400173
alision1005ba12013-06-19 13:52:44 -0400174 public ISipService getService();
alision85704182013-05-29 15:23:03 -0400175
alision1005ba12013-06-19 13:52:44 -0400176 public void callContact(SipCall call);
alision85992112013-05-29 12:18:08 -0400177
alision1005ba12013-06-19 13:52:44 -0400178 public void onCallAccepted(SipCall call);
alision85704182013-05-29 15:23:03 -0400179
alision1005ba12013-06-19 13:52:44 -0400180 public void onCallRejected(SipCall call);
alision85992112013-05-29 12:18:08 -0400181
alision1005ba12013-06-19 13:52:44 -0400182 public void onCallEnded(SipCall call);
alision85704182013-05-29 15:23:03 -0400183
alision1005ba12013-06-19 13:52:44 -0400184 public void onCallSuspended(SipCall call);
alision85704182013-05-29 15:23:03 -0400185
alision1005ba12013-06-19 13:52:44 -0400186 public void onCallResumed(SipCall call);
alision84813a12013-05-27 17:40:39 -0400187
alision1005ba12013-06-19 13:52:44 -0400188 public void onCalltransfered(SipCall call, String to);
alision84813a12013-05-27 17:40:39 -0400189
alision1005ba12013-06-19 13:52:44 -0400190 public void onRecordCall(SipCall call);
alision85704182013-05-29 15:23:03 -0400191
alision1005ba12013-06-19 13:52:44 -0400192 public void onSendMessage(SipCall call, String msg);
alisiondf1dac92013-06-27 17:35:53 -0400193
194 public void replaceCurrentCallDisplayed();
Alexandre Lision0c384512013-09-17 17:15:57 -0400195
196 public void startTimer();
alision1005ba12013-06-19 13:52:44 -0400197 }
alision85992112013-05-29 12:18:08 -0400198
alision1005ba12013-06-19 13:52:44 -0400199 @Override
200 public void onAttach(Activity activity) {
201 super.onAttach(activity);
alision85992112013-05-29 12:18:08 -0400202
alision1005ba12013-06-19 13:52:44 -0400203 if (!(activity instanceof Callbacks)) {
204 throw new IllegalStateException("Activity must implement fragment's callbacks.");
205 }
alision85992112013-05-29 12:18:08 -0400206
alision1005ba12013-06-19 13:52:44 -0400207 // rootView.requestDisallowInterceptTouchEvent(true);
alision85992112013-05-29 12:18:08 -0400208
alision1005ba12013-06-19 13:52:44 -0400209 mCallbacks = (Callbacks) activity;
alision50fa0722013-06-25 17:29:44 -0400210 myself = SipCall.SipCallBuilder.buildMyselfCall(activity.getContentResolver(), "Me");
Adrien Béraude78d06f2013-09-19 13:33:44 +1000211
alision1005ba12013-06-19 13:52:44 -0400212 }
alision85992112013-05-29 12:18:08 -0400213
alision1005ba12013-06-19 13:52:44 -0400214 @Override
215 public void onDetach() {
216 super.onDetach();
217 mCallbacks = sDummyCallbacks;
Alexandre Lisionf1850c02013-09-23 14:19:34 -0400218 mSensorManager.unregisterListener(this);
alision1005ba12013-06-19 13:52:44 -0400219 }
alision907bde72013-06-20 14:40:37 -0400220
alision1005ba12013-06-19 13:52:44 -0400221 @Override
alision907bde72013-06-20 14:40:37 -0400222 public void onStop() {
alision1005ba12013-06-19 13:52:44 -0400223 super.onStop();
224 }
alision85992112013-05-29 12:18:08 -0400225
alision1005ba12013-06-19 13:52:44 -0400226 @Override
Alexandre Lisionf1850c02013-09-23 14:19:34 -0400227 public void onResume() {
228 super.onResume();
229 mSensorManager.registerListener(this, mSensor, SensorManager.SENSOR_DELAY_NORMAL);
230 }
231
232 @Override
233 public void onPause() {
234 super.onPause();
235 mSensorManager.unregisterListener(this);
236 }
237
238 @Override
Alexandre Lision2b237922013-09-09 16:23:02 -0400239 public void onActivityResult(int requestCode, int resultCode, Intent data) {
240 super.onActivityResult(requestCode, resultCode, data);
241 SipCall transfer = null;
242 if (requestCode == REQUEST_TRANSFER) {
243 switch (resultCode) {
Adrien Béraude78d06f2013-09-19 13:33:44 +1000244 case TransferDFragment.RESULT_TRANSFER_CONF:
Alexandre Lision2b237922013-09-09 16:23:02 -0400245 Conference c = data.getParcelableExtra("target");
246 transfer = data.getParcelableExtra("transfer");
247 try {
248
249 mCallbacks.getService().attendedTransfer(transfer.getCallId(), c.getParticipants().get(0).getCallId());
250
251 } catch (RemoteException e) {
252 // TODO Auto-generated catch block
253 e.printStackTrace();
254 }
Alexandre Lision6deda412013-09-25 13:21:22 -0400255 // Toast.makeText(getActivity(), "Transfer complete", Toast.LENGTH_LONG).show();
Alexandre Lision2b237922013-09-09 16:23:02 -0400256 break;
257
Adrien Béraude78d06f2013-09-19 13:33:44 +1000258 case TransferDFragment.RESULT_TRANSFER_NUMBER:
Alexandre Lision2b237922013-09-09 16:23:02 -0400259 String to = data.getStringExtra("to_number");
260 transfer = data.getParcelableExtra("transfer");
261 try {
Alexandre Lision6deda412013-09-25 13:21:22 -0400262 // Toast.makeText(getActivity(), "Transferring " + transfer.getContact().getmDisplayName() + " to " + to,
263 // Toast.LENGTH_SHORT).show();
Alexandre Lision2b237922013-09-09 16:23:02 -0400264 mCallbacks.getService().transfer(transfer.getCallId(), to);
265 mCallbacks.getService().hangUp(transfer.getCallId());
266 } catch (RemoteException e) {
267 // TODO Auto-generated catch block
268 e.printStackTrace();
269 }
270 break;
Adrien Béraud97512842013-09-19 13:37:19 +1000271 case Activity.RESULT_CANCELED:
Alexandre Lision2b237922013-09-09 16:23:02 -0400272 default:
Adrien Béraude78d06f2013-09-19 13:33:44 +1000273 model.clear();
274 initNormalStateDisplay();
Alexandre Lision2b237922013-09-09 16:23:02 -0400275 break;
276 }
277 }
278 }
279
280 @Override
alision1005ba12013-06-19 13:52:44 -0400281 public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
Alexandre Lision84208a32013-09-25 13:18:37 -0400282 final ViewGroup rootView = (ViewGroup) inflater.inflate(R.layout.frag_call, container, false);
alision85992112013-05-29 12:18:08 -0400283
alision1005ba12013-06-19 13:52:44 -0400284 view = (BubblesView) rootView.findViewById(R.id.main_view);
alision34673e62013-06-25 14:40:07 -0400285 view.setFragment(this);
alision1005ba12013-06-19 13:52:44 -0400286 view.setModel(model);
287 view.getHolder().addCallback(this);
alision84813a12013-05-27 17:40:39 -0400288
Alexandre Lision3c6b7102013-09-16 16:56:46 -0400289 callStatusTxt = (TextView) rootView.findViewById(R.id.call_status_txt);
alision1005ba12013-06-19 13:52:44 -0400290 call_icon = BitmapFactory.decodeResource(getResources(), R.drawable.ic_call);
alision84813a12013-05-27 17:40:39 -0400291
Alexandre Lision84208a32013-09-25 13:18:37 -0400292 ((ImageButton) rootView.findViewById(R.id.dialpad_btn)).setOnClickListener(new OnClickListener() {
293
294 @Override
295 public void onClick(View v) {
296 InputMethodManager lManager = (InputMethodManager) getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
297 lManager.toggleSoftInput(InputMethodManager.SHOW_IMPLICIT, InputMethodManager.HIDE_IMPLICIT_ONLY);
298 }
299 });
300
alision1005ba12013-06-19 13:52:44 -0400301 return rootView;
302 }
Alexandre Lisionc30e8412013-09-26 15:12:59 -0400303
alision1005ba12013-06-19 13:52:44 -0400304 private void initNormalStateDisplay() {
305 Log.i(TAG, "Start normal display");
alision84813a12013-05-27 17:40:39 -0400306
Alexandre Lision0c384512013-09-17 17:15:57 -0400307 mCallbacks.startTimer();
alision84813a12013-05-27 17:40:39 -0400308
alision1005ba12013-06-19 13:52:44 -0400309 getBubbleFor(myself, model.width / 2, model.height / 2);
alision907bde72013-06-20 14:40:37 -0400310
alisiondf1dac92013-06-27 17:35:53 -0400311 int angle_part = 360 / conf.getParticipants().size();
alision806e18e2013-06-21 15:30:17 -0400312 double dX = 0;
313 double dY = 0;
alision465ceba2013-07-04 09:24:30 -0400314 int radiusCalls = (int) (model.width / 2 - BUBBLE_SIZE);
alisiondf1dac92013-06-27 17:35:53 -0400315 for (int i = 0; i < conf.getParticipants().size(); ++i) {
316
317 if (conf.getParticipants().get(i) == null) {
318 Log.i(TAG, i + " null ");
319 continue;
320 }
alision34673e62013-06-25 14:40:07 -0400321 dX = Math.cos(Math.toRadians(angle_part * i - 90)) * radiusCalls;
322 dY = Math.sin(Math.toRadians(angle_part * i - 90)) * radiusCalls;
alisiondf1dac92013-06-27 17:35:53 -0400323 getBubbleFor(conf.getParticipants().get(i), (int) (model.width / 2 + dX), (int) (model.height / 2 + dY));
alision806e18e2013-06-21 15:30:17 -0400324 }
alision1005ba12013-06-19 13:52:44 -0400325
326 model.clearAttractors();
alision1005ba12013-06-19 13:52:44 -0400327 }
alision85704182013-05-29 15:23:03 -0400328
alision1005ba12013-06-19 13:52:44 -0400329 private void initIncomingCallDisplay() {
330 Log.i(TAG, "Start incoming display");
alision84813a12013-05-27 17:40:39 -0400331
Alexandre Lision0c384512013-09-17 17:15:57 -0400332 mCallbacks.startTimer();
alision84813a12013-05-27 17:40:39 -0400333
Alexandre Lision23628c12013-09-24 11:17:05 -0400334 int radiusCalls = (int) (model.width / 2 - BUBBLE_SIZE);
335 getBubbleFor(myself, model.width / 2, model.height / 2 + radiusCalls);
336 getBubbleFor(conf.getParticipants().get(0), model.width / 2, model.height / 2 - radiusCalls);
alision85704182013-05-29 15:23:03 -0400337
alision1005ba12013-06-19 13:52:44 -0400338 model.clearAttractors();
Alexandre Lision23628c12013-09-24 11:17:05 -0400339 model.addAttractor(new Attractor(new PointF(model.width / 2, model.height / 2), ATTRACTOR_SIZE, new Attractor.Callback() {
alision1005ba12013-06-19 13:52:44 -0400340 @Override
341 public boolean onBubbleSucked(Bubble b) {
Alexandre Lisiona764c682013-09-09 10:02:07 -0400342
343 if (!accepted) {
344 mCallbacks.onCallAccepted(conf.getParticipants().get(0));
345 accepted = true;
346 }
alision1005ba12013-06-19 13:52:44 -0400347 return false;
348 }
349 }, call_icon));
alision1005ba12013-06-19 13:52:44 -0400350 }
alision85704182013-05-29 15:23:03 -0400351
alision1005ba12013-06-19 13:52:44 -0400352 private void initOutGoingCallDisplay() {
353 Log.i(TAG, "Start outgoing display");
alision85704182013-05-29 15:23:03 -0400354
Alexandre Lision0c384512013-09-17 17:15:57 -0400355 mCallbacks.startTimer();
alision85704182013-05-29 15:23:03 -0400356
Adrien Béraude78d06f2013-09-19 13:33:44 +1000357 getBubbleFor(myself, model.width / 2, model.height / 2);
alision85704182013-05-29 15:23:03 -0400358
alisiondf1dac92013-06-27 17:35:53 -0400359 // TODO off-thread image loading
360 int angle_part = 360 / conf.getParticipants().size();
361 double dX = 0;
362 double dY = 0;
Adrien Béraude78d06f2013-09-19 13:33:44 +1000363 int radiusCalls = (int) (model.width / 2 - BUBBLE_SIZE);
alisiondf1dac92013-06-27 17:35:53 -0400364 for (int i = 0; i < conf.getParticipants().size(); ++i) {
365 dX = Math.cos(Math.toRadians(angle_part * i - 90)) * radiusCalls;
366 dY = Math.sin(Math.toRadians(angle_part * i - 90)) * radiusCalls;
367 getBubbleFor(conf.getParticipants().get(i), (int) (model.width / 2 + dX), (int) (model.height / 2 + dY));
368 }
alision907bde72013-06-20 14:40:37 -0400369
alision1005ba12013-06-19 13:52:44 -0400370 model.clearAttractors();
alision1005ba12013-06-19 13:52:44 -0400371 }
alision85704182013-05-29 15:23:03 -0400372
alision1005ba12013-06-19 13:52:44 -0400373 /**
374 * Retrieves or create a bubble for a given contact. If the bubble exists, it is moved to the new location.
375 *
alision806e18e2013-06-21 15:30:17 -0400376 * @param call
377 * The call associated to a contact
alision1005ba12013-06-19 13:52:44 -0400378 * @param x
379 * Initial or new x position.
380 * @param y
381 * Initial or new y position.
382 * @return Bubble corresponding to the contact.
383 */
alision806e18e2013-06-21 15:30:17 -0400384 private Bubble getBubbleFor(SipCall call, float x, float y) {
alisiondf1dac92013-06-27 17:35:53 -0400385 Bubble contact_bubble = model.getBubble(call);
alision1005ba12013-06-19 13:52:44 -0400386 if (contact_bubble != null) {
387 contact_bubble.attractor.set(x, y);
388 return contact_bubble;
389 }
alision84813a12013-05-27 17:40:39 -0400390
alision34673e62013-06-25 14:40:07 -0400391 contact_bubble = new Bubble(getActivity(), call, x, y, BUBBLE_SIZE);
alision84813a12013-05-27 17:40:39 -0400392
alision1005ba12013-06-19 13:52:44 -0400393 model.addBubble(contact_bubble);
alision1005ba12013-06-19 13:52:44 -0400394 return contact_bubble;
395 }
Adrien Béraud0c9bd8f2013-05-30 16:16:57 +1000396
alision1005ba12013-06-19 13:52:44 -0400397 /**
398 * Should be called when a bubble is removed from the model
399 */
400 void bubbleRemoved(Bubble b) {
alision806e18e2013-06-21 15:30:17 -0400401 if (b.associated_call == null) {
alision1005ba12013-06-19 13:52:44 -0400402 return;
403 }
alision1005ba12013-06-19 13:52:44 -0400404 }
Adrien Béraudc9c424d2013-05-30 17:47:35 +1000405
alision1005ba12013-06-19 13:52:44 -0400406 public void changeCallState(String callID, String newState) {
Alexandre Lisionf1850c02013-09-23 14:19:34 -0400407 Log.w(TAG, "Call :" + callID + " " + newState);
alisiondf1dac92013-06-27 17:35:53 -0400408 if (newState.contentEquals("FAILURE")) {
409 try {
410 mCallbacks.getService().hangUp(callID);
411 } catch (RemoteException e) {
412 e.printStackTrace();
alision806e18e2013-06-21 15:30:17 -0400413 }
alision1005ba12013-06-19 13:52:44 -0400414 }
alisiondf1dac92013-06-27 17:35:53 -0400415 for (int i = 0; i < conf.getParticipants().size(); ++i) {
alisiondf1dac92013-06-27 17:35:53 -0400416 if (callID.equals(conf.getParticipants().get(i).getCallId())) {
417 if (newState.contentEquals("HUNGUP")) {
alisiondf1dac92013-06-27 17:35:53 -0400418 model.removeBubble(conf.getParticipants().get(i));
419 conf.getParticipants().remove(i);
420 } else {
421 conf.getParticipants().get(i).setCallState(newState);
422 }
423 }
424 }
425
426 if (conf.isOnGoing())
427 initNormalStateDisplay();
428
429 if (conf.getParticipants().size() == 0) {
430 mCallbacks.replaceCurrentCallDisplayed();
431 }
alision1005ba12013-06-19 13:52:44 -0400432 }
alision84813a12013-05-27 17:40:39 -0400433
alision1005ba12013-06-19 13:52:44 -0400434 public boolean draggingBubble() {
435 return view == null ? false : view.isDraggingBubble();
436 }
Adrien Béraudc9c424d2013-05-30 17:47:35 +1000437
alision1005ba12013-06-19 13:52:44 -0400438 @Override
439 public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
Adrien Béraud13cde0b2013-05-30 20:27:20 +1000440
alisiondf1dac92013-06-27 17:35:53 -0400441 if (conf.getParticipants().size() == 1) {
alisiondf1dac92013-06-27 17:35:53 -0400442 if (conf.getParticipants().get(0).isIncoming() && conf.getParticipants().get(0).isRinging()) {
alision806e18e2013-06-21 15:30:17 -0400443 initIncomingCallDisplay();
444 } else {
alisiondf1dac92013-06-27 17:35:53 -0400445 if (conf.getParticipants().get(0).isRinging()) {
alision1005ba12013-06-19 13:52:44 -0400446 initOutGoingCallDisplay();
447 }
alision806e18e2013-06-21 15:30:17 -0400448 try {
alisiondf1dac92013-06-27 17:35:53 -0400449 if (conf.getParticipants().get(0).isOutGoing()
450 && mCallbacks.getService().getCall(conf.getParticipants().get(0).getCallId()) == null) {
451 mCallbacks.getService().placeCall(conf.getParticipants().get(0));
alision806e18e2013-06-21 15:30:17 -0400452 initOutGoingCallDisplay();
alisiondf1dac92013-06-27 17:35:53 -0400453 } else if (conf.getParticipants().get(0).isOutGoing() && conf.getParticipants().get(0).isRinging()) {
alision806e18e2013-06-21 15:30:17 -0400454 initOutGoingCallDisplay();
455 }
456 } catch (RemoteException e) {
457 Log.e(TAG, e.toString());
458 }
alision1005ba12013-06-19 13:52:44 -0400459 }
alisiondf1dac92013-06-27 17:35:53 -0400460 if (conf.getParticipants().get(0).isOngoing()) {
alision806e18e2013-06-21 15:30:17 -0400461 initNormalStateDisplay();
462 }
alisiondf1dac92013-06-27 17:35:53 -0400463 } else if (conf.getParticipants().size() > 1) {
alision1005ba12013-06-19 13:52:44 -0400464 initNormalStateDisplay();
465 }
alision1005ba12013-06-19 13:52:44 -0400466 }
Adrien Béraud13cde0b2013-05-30 20:27:20 +1000467
Alexandre Lision2b237922013-09-09 16:23:02 -0400468 public void makeTransfer(Bubble contact) {
469 FragmentManager fm = getFragmentManager();
Alexandre Lision0eb02032013-09-27 16:32:40 -0400470 editName = TransferDFragment.newInstance();
Alexandre Lision2b237922013-09-09 16:23:02 -0400471 Bundle b = new Bundle();
Alexandre Lision6ae652d2013-09-26 16:39:20 -0400472 try {
473 b.putParcelableArrayList("calls", (ArrayList<Conference>)mCallbacks.getService().getConcurrentCalls());
474 b.putParcelable("call_selected", contact.associated_call);
475 editName.setArguments(b);
476 editName.setTargetFragment(this, REQUEST_TRANSFER);
477 editName.show(fm, "");
478 } catch (RemoteException e) {
479 Log.e(TAG, e.toString());
480 }
481
Alexandre Lision2b237922013-09-09 16:23:02 -0400482 }
483
alision1005ba12013-06-19 13:52:44 -0400484 @Override
485 public void surfaceCreated(SurfaceHolder holder) {
Alexandre Lisionc30e8412013-09-26 15:12:59 -0400486
alision1005ba12013-06-19 13:52:44 -0400487 }
Adrien Béraud13cde0b2013-05-30 20:27:20 +1000488
alision1005ba12013-06-19 13:52:44 -0400489 @Override
490 public void surfaceDestroyed(SurfaceHolder holder) {
Alexandre Lision84208a32013-09-25 13:18:37 -0400491 // check that soft input is hidden
492 InputMethodManager lManager = (InputMethodManager) getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
493 lManager.hideSoftInputFromWindow(view.getWindowToken(), 0);
Alexandre Lision0eb02032013-09-27 16:32:40 -0400494 if(editName != null && editName.isVisible()){
495 editName.dismiss();
496 }
alision1005ba12013-06-19 13:52:44 -0400497 }
Adrien Béraud13cde0b2013-05-30 20:27:20 +1000498
alisione38001f2013-06-04 14:14:39 -0400499 public BubblesView getBubbleView() {
500 return view;
alision1005ba12013-06-19 13:52:44 -0400501 }
Alexandre Lision0c384512013-09-17 17:15:57 -0400502
503 public void updateTime() {
Adrien Béraude78d06f2013-09-19 13:33:44 +1000504 long duration = System.currentTimeMillis() / 1000 - this.conf.getParticipants().get(0).getTimestamp_start();
Alexandre Lisionf1850c02013-09-23 14:19:34 -0400505 callStatusTxt.setText(String.format("%d:%02d:%02d", duration / 3600, duration % 3600 / 60, duration % 60));
Alexandre Lisionf1850c02013-09-23 14:19:34 -0400506 }
507
508 @Override
509 public void onAccuracyChanged(Sensor sensor, int accuracy) {
Alexandre Lisionf1850c02013-09-23 14:19:34 -0400510
511 }
512
513 @Override
514 public void onSensorChanged(SensorEvent event) {
Alexandre Lisionc30e8412013-09-26 15:12:59 -0400515 if (event.values[0] == 0) {
516 WindowManager.LayoutParams params = getActivity().getWindow().getAttributes();
517 params.flags |= WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON;
518 params.flags = WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE;
519 params.screenBrightness = 0.004f;
520 getActivity().getWindow().setAttributes(params);
Adrien Béraude78d06f2013-09-19 13:33:44 +1000521
Alexandre Lisionc30e8412013-09-26 15:12:59 -0400522 } else {
523 WindowManager.LayoutParams params = getActivity().getWindow().getAttributes();
524 getActivity().getWindow().clearFlags(WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE);
525 params.flags = WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON;
526 params.screenBrightness = 1.0f;
527 getActivity().getWindow().setAttributes(params);
528 }
Alexandre Lision0c384512013-09-17 17:15:57 -0400529 }
Alexandre Lision0edf18c2013-09-23 17:35:50 -0400530
531 public Conference getConference() {
532 return conf;
533 }
Alexandre Lision64dc8c02013-09-25 15:32:25 -0400534
535 public void onKeyUp(int keyCode, KeyEvent event) {
536 try {
Alexandre Lision80f0a7e2013-09-26 12:27:51 -0400537 String toSend = Character.toString(event.getDisplayLabel());
Alexandre Lision31f46fc2013-09-26 11:19:54 -0400538 toSend.toUpperCase(Locale.getDefault());
Alexandre Lisionc30e8412013-09-26 15:12:59 -0400539 Log.d(TAG, "toSend " + toSend);
Alexandre Lision64dc8c02013-09-25 15:32:25 -0400540 mCallbacks.getService().playDtmf(toSend);
541 } catch (RemoteException e) {
542 e.printStackTrace();
543 }
544 }
alision84813a12013-05-27 17:40:39 -0400545}