blob: 1e5060ee79c298aa67ce433604b4abf8f33161d6 [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
Alexandre Lision064e1e02013-10-01 16:18:42 -040032package org.sflphone.fragments;
alision84813a12013-05-27 17:40:39 -040033
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
Alexandre Lision064e1e02013-10-01 16:18:42 -040037import org.sflphone.R;
38import org.sflphone.model.Attractor;
39import org.sflphone.model.Bubble;
Alexandre Lision40954dc2013-10-09 15:24:03 -040040import org.sflphone.model.BubbleContact;
Alexandre Lision064e1e02013-10-01 16:18:42 -040041import org.sflphone.model.BubbleModel;
Alexandre Lision40954dc2013-10-09 15:24:03 -040042import org.sflphone.model.BubbleUser;
Alexandre Lision064e1e02013-10-01 16:18:42 -040043import org.sflphone.model.BubblesView;
44import org.sflphone.model.Conference;
45import org.sflphone.model.SipCall;
46import org.sflphone.service.ISipService;
47
alision84813a12013-05-27 17:40:39 -040048import android.app.Activity;
49import android.app.Fragment;
Alexandre Lision2b237922013-09-09 16:23:02 -040050import android.app.FragmentManager;
Alexandre Lisionf1850c02013-09-23 14:19:34 -040051import android.content.Context;
Alexandre Lision2b237922013-09-09 16:23:02 -040052import android.content.Intent;
alision84813a12013-05-27 17:40:39 -040053import android.graphics.Bitmap;
Adrien Béraud0c9bd8f2013-05-30 16:16:57 +100054import android.graphics.BitmapFactory;
alision84813a12013-05-27 17:40:39 -040055import android.graphics.PointF;
Alexandre Lisionf1850c02013-09-23 14:19:34 -040056import android.hardware.Sensor;
57import android.hardware.SensorEvent;
58import android.hardware.SensorEventListener;
59import android.hardware.SensorManager;
alision84813a12013-05-27 17:40:39 -040060import android.os.Bundle;
61import android.os.RemoteException;
alision84813a12013-05-27 17:40:39 -040062import android.util.Log;
Alexandre Lision64dc8c02013-09-25 15:32:25 -040063import android.view.KeyEvent;
alision84813a12013-05-27 17:40:39 -040064import android.view.LayoutInflater;
Alexandre Lisiond588bff2013-10-08 12:43:01 -040065import android.view.Menu;
66import android.view.MenuInflater;
67import android.view.MenuItem;
Adrien Béraud13cde0b2013-05-30 20:27:20 +100068import android.view.SurfaceHolder;
69import android.view.SurfaceHolder.Callback;
alision84813a12013-05-27 17:40:39 -040070import android.view.View;
Alexandre Lision84208a32013-09-25 13:18:37 -040071import android.view.View.OnClickListener;
alision84813a12013-05-27 17:40:39 -040072import android.view.ViewGroup;
Alexandre Lisionc30e8412013-09-26 15:12:59 -040073import android.view.WindowManager;
Alexandre Lision84208a32013-09-25 13:18:37 -040074import android.view.inputmethod.InputMethodManager;
75import android.widget.ImageButton;
Alexandre Lision3c6b7102013-09-16 16:56:46 -040076import android.widget.TextView;
alision84813a12013-05-27 17:40:39 -040077
Alexandre Lisionf1850c02013-09-23 14:19:34 -040078public class CallFragment extends Fragment implements Callback, SensorEventListener {
alision84813a12013-05-27 17:40:39 -040079
alision1005ba12013-06-19 13:52:44 -040080 static final String TAG = "CallFragment";
alision84813a12013-05-27 17:40:39 -040081
alisionf57d8a62013-07-02 09:37:12 -040082 float BUBBLE_SIZE = 75;
alision1005ba12013-06-19 13:52:44 -040083 static final float ATTRACTOR_SIZE = 40;
alision84813a12013-05-27 17:40:39 -040084
Alexandre Lision2b237922013-09-09 16:23:02 -040085 public static final int REQUEST_TRANSFER = 10;
86
alisiondf1dac92013-06-27 17:35:53 -040087 private Conference conf;
alision84813a12013-05-27 17:40:39 -040088
Alexandre Lision3c6b7102013-09-16 16:56:46 -040089 private TextView callStatusTxt;
alision1005ba12013-06-19 13:52:44 -040090 private BubblesView view;
91 private BubbleModel model;
alision84813a12013-05-27 17:40:39 -040092
alision1005ba12013-06-19 13:52:44 -040093 private Callbacks mCallbacks = sDummyCallbacks;
alision84813a12013-05-27 17:40:39 -040094
alision806e18e2013-06-21 15:30:17 -040095 private SipCall myself;
alision85704182013-05-29 15:23:03 -040096
Alexandre Lisiona764c682013-09-09 10:02:07 -040097 boolean accepted = false;
alision1005ba12013-06-19 13:52:44 -040098 private Bitmap call_icon;
alision85704182013-05-29 15:23:03 -040099
Alexandre Lisionf1850c02013-09-23 14:19:34 -0400100 private SensorManager mSensorManager;
101
102 private Sensor mSensor;
Alexandre Lision0eb02032013-09-27 16:32:40 -0400103
104 TransferDFragment editName;
Alexandre Lisionf1850c02013-09-23 14:19:34 -0400105
alision1005ba12013-06-19 13:52:44 -0400106 @Override
107 public void onCreate(Bundle savedBundle) {
108 super.onCreate(savedBundle);
alision1005ba12013-06-19 13:52:44 -0400109 Bundle b = getArguments();
alisioncd8fb912013-06-28 14:43:51 -0400110 conf = new Conference((Conference) b.getParcelable("conference"));
alisiondf1dac92013-06-27 17:35:53 -0400111 model = new BubbleModel(getResources().getDisplayMetrics().density);
alision729b0a22013-07-02 11:57:33 -0400112 BUBBLE_SIZE = getResources().getDimension(R.dimen.bubble_size);
Alexandre Lisiona764c682013-09-09 10:02:07 -0400113 Log.e(TAG, "BUBBLE_SIZE " + BUBBLE_SIZE);
Alexandre Lisionf1850c02013-09-23 14:19:34 -0400114 mSensorManager = (SensorManager) getActivity().getSystemService(Context.SENSOR_SERVICE);
115 mSensor = mSensorManager.getDefaultSensor(Sensor.TYPE_PROXIMITY);
Alexandre Lisiond588bff2013-10-08 12:43:01 -0400116 this.setHasOptionsMenu(true);
alision729b0a22013-07-02 11:57:33 -0400117
alision1005ba12013-06-19 13:52:44 -0400118 }
alision85992112013-05-29 12:18:08 -0400119
alision1005ba12013-06-19 13:52:44 -0400120 /**
121 * A dummy implementation of the {@link Callbacks} interface that does nothing. Used only when this fragment is not attached to an activity.
122 */
123 private static Callbacks sDummyCallbacks = new Callbacks() {
124 @Override
125 public void onSendMessage(SipCall call, String msg) {
126 }
alision85992112013-05-29 12:18:08 -0400127
alision1005ba12013-06-19 13:52:44 -0400128 @Override
129 public void callContact(SipCall call) {
130 }
alision85992112013-05-29 12:18:08 -0400131
alision1005ba12013-06-19 13:52:44 -0400132 @Override
133 public void onCallAccepted(SipCall call) {
134 }
alision85704182013-05-29 15:23:03 -0400135
alision1005ba12013-06-19 13:52:44 -0400136 @Override
137 public void onCallRejected(SipCall call) {
138 }
alision85992112013-05-29 12:18:08 -0400139
alision1005ba12013-06-19 13:52:44 -0400140 @Override
141 public void onCallEnded(SipCall call) {
142 }
alision85704182013-05-29 15:23:03 -0400143
alision1005ba12013-06-19 13:52:44 -0400144 @Override
145 public void onCallSuspended(SipCall call) {
146 }
alision85992112013-05-29 12:18:08 -0400147
alision1005ba12013-06-19 13:52:44 -0400148 @Override
149 public void onCallResumed(SipCall call) {
150 }
alision85704182013-05-29 15:23:03 -0400151
alision1005ba12013-06-19 13:52:44 -0400152 @Override
153 public void onCalltransfered(SipCall call, String to) {
154 }
alision85992112013-05-29 12:18:08 -0400155
alision1005ba12013-06-19 13:52:44 -0400156 @Override
157 public void onRecordCall(SipCall call) {
158 }
alision85704182013-05-29 15:23:03 -0400159
alision1005ba12013-06-19 13:52:44 -0400160 @Override
161 public ISipService getService() {
162 return null;
163 }
alisiondf1dac92013-06-27 17:35:53 -0400164
165 @Override
166 public void replaceCurrentCallDisplayed() {
167 }
Alexandre Lision0c384512013-09-17 17:15:57 -0400168
169 @Override
Adrien Béraude78d06f2013-09-19 13:33:44 +1000170 public void startTimer() {
Alexandre Lision0c384512013-09-17 17:15:57 -0400171 }
alision1005ba12013-06-19 13:52:44 -0400172 };
alision907bde72013-06-20 14:40:37 -0400173
alision1005ba12013-06-19 13:52:44 -0400174 /**
175 * The Activity calling this fragment has to implement this interface
176 *
177 */
178 public interface Callbacks {
alision85992112013-05-29 12:18:08 -0400179
alision1005ba12013-06-19 13:52:44 -0400180 public ISipService getService();
alision85704182013-05-29 15:23:03 -0400181
alision1005ba12013-06-19 13:52:44 -0400182 public void callContact(SipCall call);
alision85992112013-05-29 12:18:08 -0400183
alision1005ba12013-06-19 13:52:44 -0400184 public void onCallAccepted(SipCall call);
alision85704182013-05-29 15:23:03 -0400185
alision1005ba12013-06-19 13:52:44 -0400186 public void onCallRejected(SipCall call);
alision85992112013-05-29 12:18:08 -0400187
alision1005ba12013-06-19 13:52:44 -0400188 public void onCallEnded(SipCall call);
alision85704182013-05-29 15:23:03 -0400189
alision1005ba12013-06-19 13:52:44 -0400190 public void onCallSuspended(SipCall call);
alision85704182013-05-29 15:23:03 -0400191
alision1005ba12013-06-19 13:52:44 -0400192 public void onCallResumed(SipCall call);
alision84813a12013-05-27 17:40:39 -0400193
alision1005ba12013-06-19 13:52:44 -0400194 public void onCalltransfered(SipCall call, String to);
alision84813a12013-05-27 17:40:39 -0400195
alision1005ba12013-06-19 13:52:44 -0400196 public void onRecordCall(SipCall call);
alision85704182013-05-29 15:23:03 -0400197
alision1005ba12013-06-19 13:52:44 -0400198 public void onSendMessage(SipCall call, String msg);
alisiondf1dac92013-06-27 17:35:53 -0400199
200 public void replaceCurrentCallDisplayed();
Alexandre Lision0c384512013-09-17 17:15:57 -0400201
202 public void startTimer();
alision1005ba12013-06-19 13:52:44 -0400203 }
alision85992112013-05-29 12:18:08 -0400204
alision1005ba12013-06-19 13:52:44 -0400205 @Override
206 public void onAttach(Activity activity) {
207 super.onAttach(activity);
alision85992112013-05-29 12:18:08 -0400208
alision1005ba12013-06-19 13:52:44 -0400209 if (!(activity instanceof Callbacks)) {
210 throw new IllegalStateException("Activity must implement fragment's callbacks.");
211 }
alision85992112013-05-29 12:18:08 -0400212
alision1005ba12013-06-19 13:52:44 -0400213 // rootView.requestDisallowInterceptTouchEvent(true);
alision85992112013-05-29 12:18:08 -0400214
alision1005ba12013-06-19 13:52:44 -0400215 mCallbacks = (Callbacks) activity;
alision50fa0722013-06-25 17:29:44 -0400216 myself = SipCall.SipCallBuilder.buildMyselfCall(activity.getContentResolver(), "Me");
Adrien Béraude78d06f2013-09-19 13:33:44 +1000217
alision1005ba12013-06-19 13:52:44 -0400218 }
Alexandre Lisiond588bff2013-10-08 12:43:01 -0400219
220 @Override
221 public void onCreateOptionsMenu(Menu m, MenuInflater inf) {
222 super.onCreateOptionsMenu(m, inf);
223 inf.inflate(R.menu.account_creation, m);
224 }
225
226 @Override
227 public boolean onOptionsItemSelected(MenuItem item) {
228 super.onOptionsItemSelected(item);
229 switch (item.getItemId()) {
230 case R.id.menuitem_create:
231// Intent intent = new Intent().setClass(getActivity(), AccountWizard.class);
232// startActivityForResult(intent, ACCOUNT_CREATE_REQUEST);
233 break;
234 }
235
236 return true;
237 }
alision85992112013-05-29 12:18:08 -0400238
alision1005ba12013-06-19 13:52:44 -0400239 @Override
240 public void onDetach() {
241 super.onDetach();
242 mCallbacks = sDummyCallbacks;
Alexandre Lisionf1850c02013-09-23 14:19:34 -0400243 mSensorManager.unregisterListener(this);
alision1005ba12013-06-19 13:52:44 -0400244 }
alision907bde72013-06-20 14:40:37 -0400245
alision1005ba12013-06-19 13:52:44 -0400246 @Override
alision907bde72013-06-20 14:40:37 -0400247 public void onStop() {
alision1005ba12013-06-19 13:52:44 -0400248 super.onStop();
249 }
alision85992112013-05-29 12:18:08 -0400250
alision1005ba12013-06-19 13:52:44 -0400251 @Override
Alexandre Lisionf1850c02013-09-23 14:19:34 -0400252 public void onResume() {
253 super.onResume();
254 mSensorManager.registerListener(this, mSensor, SensorManager.SENSOR_DELAY_NORMAL);
255 }
256
257 @Override
258 public void onPause() {
259 super.onPause();
260 mSensorManager.unregisterListener(this);
261 }
262
263 @Override
Alexandre Lision2b237922013-09-09 16:23:02 -0400264 public void onActivityResult(int requestCode, int resultCode, Intent data) {
265 super.onActivityResult(requestCode, resultCode, data);
266 SipCall transfer = null;
267 if (requestCode == REQUEST_TRANSFER) {
268 switch (resultCode) {
Adrien Béraude78d06f2013-09-19 13:33:44 +1000269 case TransferDFragment.RESULT_TRANSFER_CONF:
Alexandre Lision2b237922013-09-09 16:23:02 -0400270 Conference c = data.getParcelableExtra("target");
271 transfer = data.getParcelableExtra("transfer");
272 try {
273
274 mCallbacks.getService().attendedTransfer(transfer.getCallId(), c.getParticipants().get(0).getCallId());
275
276 } catch (RemoteException e) {
Alexandre Lision2b237922013-09-09 16:23:02 -0400277 e.printStackTrace();
278 }
Alexandre Lision2b237922013-09-09 16:23:02 -0400279 break;
280
Adrien Béraude78d06f2013-09-19 13:33:44 +1000281 case TransferDFragment.RESULT_TRANSFER_NUMBER:
Alexandre Lision2b237922013-09-09 16:23:02 -0400282 String to = data.getStringExtra("to_number");
283 transfer = data.getParcelableExtra("transfer");
284 try {
Alexandre Lision2b237922013-09-09 16:23:02 -0400285 mCallbacks.getService().transfer(transfer.getCallId(), to);
286 mCallbacks.getService().hangUp(transfer.getCallId());
287 } catch (RemoteException e) {
Alexandre Lision2b237922013-09-09 16:23:02 -0400288 e.printStackTrace();
289 }
290 break;
Adrien Béraud97512842013-09-19 13:37:19 +1000291 case Activity.RESULT_CANCELED:
Alexandre Lision2b237922013-09-09 16:23:02 -0400292 default:
Adrien Béraude78d06f2013-09-19 13:33:44 +1000293 model.clear();
294 initNormalStateDisplay();
Alexandre Lision2b237922013-09-09 16:23:02 -0400295 break;
296 }
297 }
298 }
299
300 @Override
alision1005ba12013-06-19 13:52:44 -0400301 public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
Alexandre Lision84208a32013-09-25 13:18:37 -0400302 final ViewGroup rootView = (ViewGroup) inflater.inflate(R.layout.frag_call, container, false);
alision85992112013-05-29 12:18:08 -0400303
alision1005ba12013-06-19 13:52:44 -0400304 view = (BubblesView) rootView.findViewById(R.id.main_view);
alision34673e62013-06-25 14:40:07 -0400305 view.setFragment(this);
alision1005ba12013-06-19 13:52:44 -0400306 view.setModel(model);
307 view.getHolder().addCallback(this);
alision84813a12013-05-27 17:40:39 -0400308
Alexandre Lision3c6b7102013-09-16 16:56:46 -0400309 callStatusTxt = (TextView) rootView.findViewById(R.id.call_status_txt);
alision1005ba12013-06-19 13:52:44 -0400310 call_icon = BitmapFactory.decodeResource(getResources(), R.drawable.ic_call);
alision84813a12013-05-27 17:40:39 -0400311
Alexandre Lision84208a32013-09-25 13:18:37 -0400312 ((ImageButton) rootView.findViewById(R.id.dialpad_btn)).setOnClickListener(new OnClickListener() {
313
314 @Override
315 public void onClick(View v) {
316 InputMethodManager lManager = (InputMethodManager) getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
317 lManager.toggleSoftInput(InputMethodManager.SHOW_IMPLICIT, InputMethodManager.HIDE_IMPLICIT_ONLY);
318 }
319 });
320
alision1005ba12013-06-19 13:52:44 -0400321 return rootView;
322 }
Alexandre Lisionc30e8412013-09-26 15:12:59 -0400323
alision1005ba12013-06-19 13:52:44 -0400324 private void initNormalStateDisplay() {
325 Log.i(TAG, "Start normal display");
alision84813a12013-05-27 17:40:39 -0400326
Alexandre Lision0c384512013-09-17 17:15:57 -0400327 mCallbacks.startTimer();
alision84813a12013-05-27 17:40:39 -0400328
Alexandre Lisionee2494d2013-10-09 17:14:00 -0400329 getBubbleForUser(conf, model.width / 2, model.height / 2);
alision907bde72013-06-20 14:40:37 -0400330
alisiondf1dac92013-06-27 17:35:53 -0400331 int angle_part = 360 / conf.getParticipants().size();
alision806e18e2013-06-21 15:30:17 -0400332 double dX = 0;
333 double dY = 0;
alision465ceba2013-07-04 09:24:30 -0400334 int radiusCalls = (int) (model.width / 2 - BUBBLE_SIZE);
alisiondf1dac92013-06-27 17:35:53 -0400335 for (int i = 0; i < conf.getParticipants().size(); ++i) {
336
337 if (conf.getParticipants().get(i) == null) {
338 Log.i(TAG, i + " null ");
339 continue;
340 }
alision34673e62013-06-25 14:40:07 -0400341 dX = Math.cos(Math.toRadians(angle_part * i - 90)) * radiusCalls;
342 dY = Math.sin(Math.toRadians(angle_part * i - 90)) * radiusCalls;
alisiondf1dac92013-06-27 17:35:53 -0400343 getBubbleFor(conf.getParticipants().get(i), (int) (model.width / 2 + dX), (int) (model.height / 2 + dY));
alision806e18e2013-06-21 15:30:17 -0400344 }
alision1005ba12013-06-19 13:52:44 -0400345
346 model.clearAttractors();
alision1005ba12013-06-19 13:52:44 -0400347 }
alision85704182013-05-29 15:23:03 -0400348
alision1005ba12013-06-19 13:52:44 -0400349 private void initIncomingCallDisplay() {
350 Log.i(TAG, "Start incoming display");
alision84813a12013-05-27 17:40:39 -0400351
Alexandre Lision0c384512013-09-17 17:15:57 -0400352 mCallbacks.startTimer();
alision84813a12013-05-27 17:40:39 -0400353
Alexandre Lision23628c12013-09-24 11:17:05 -0400354 int radiusCalls = (int) (model.width / 2 - BUBBLE_SIZE);
Alexandre Lisionee2494d2013-10-09 17:14:00 -0400355 getBubbleForUser(conf, model.width / 2, model.height / 2 + radiusCalls);
Alexandre Lision23628c12013-09-24 11:17:05 -0400356 getBubbleFor(conf.getParticipants().get(0), model.width / 2, model.height / 2 - radiusCalls);
alision85704182013-05-29 15:23:03 -0400357
alision1005ba12013-06-19 13:52:44 -0400358 model.clearAttractors();
Alexandre Lision23628c12013-09-24 11:17:05 -0400359 model.addAttractor(new Attractor(new PointF(model.width / 2, model.height / 2), ATTRACTOR_SIZE, new Attractor.Callback() {
alision1005ba12013-06-19 13:52:44 -0400360 @Override
361 public boolean onBubbleSucked(Bubble b) {
Alexandre Lisiona764c682013-09-09 10:02:07 -0400362
363 if (!accepted) {
364 mCallbacks.onCallAccepted(conf.getParticipants().get(0));
365 accepted = true;
366 }
alision1005ba12013-06-19 13:52:44 -0400367 return false;
368 }
369 }, call_icon));
alision1005ba12013-06-19 13:52:44 -0400370 }
alision85704182013-05-29 15:23:03 -0400371
alision1005ba12013-06-19 13:52:44 -0400372 private void initOutGoingCallDisplay() {
373 Log.i(TAG, "Start outgoing display");
alision85704182013-05-29 15:23:03 -0400374
Alexandre Lision0c384512013-09-17 17:15:57 -0400375 mCallbacks.startTimer();
alision85704182013-05-29 15:23:03 -0400376
Alexandre Lisionee2494d2013-10-09 17:14:00 -0400377 getBubbleForUser(conf, model.width / 2, model.height / 2);
alision85704182013-05-29 15:23:03 -0400378
alisiondf1dac92013-06-27 17:35:53 -0400379 // TODO off-thread image loading
380 int angle_part = 360 / conf.getParticipants().size();
381 double dX = 0;
382 double dY = 0;
Adrien Béraude78d06f2013-09-19 13:33:44 +1000383 int radiusCalls = (int) (model.width / 2 - BUBBLE_SIZE);
alisiondf1dac92013-06-27 17:35:53 -0400384 for (int i = 0; i < conf.getParticipants().size(); ++i) {
385 dX = Math.cos(Math.toRadians(angle_part * i - 90)) * radiusCalls;
386 dY = Math.sin(Math.toRadians(angle_part * i - 90)) * radiusCalls;
387 getBubbleFor(conf.getParticipants().get(i), (int) (model.width / 2 + dX), (int) (model.height / 2 + dY));
388 }
alision907bde72013-06-20 14:40:37 -0400389
alision1005ba12013-06-19 13:52:44 -0400390 model.clearAttractors();
alision1005ba12013-06-19 13:52:44 -0400391 }
alision85704182013-05-29 15:23:03 -0400392
alision1005ba12013-06-19 13:52:44 -0400393 /**
394 * Retrieves or create a bubble for a given contact. If the bubble exists, it is moved to the new location.
395 *
alision806e18e2013-06-21 15:30:17 -0400396 * @param call
397 * The call associated to a contact
alision1005ba12013-06-19 13:52:44 -0400398 * @param x
399 * Initial or new x position.
400 * @param y
401 * Initial or new y position.
402 * @return Bubble corresponding to the contact.
403 */
alision806e18e2013-06-21 15:30:17 -0400404 private Bubble getBubbleFor(SipCall call, float x, float y) {
Alexandre Lisionee2494d2013-10-09 17:14:00 -0400405 Bubble contact_bubble = model.getBubble(call.getCallId());
alision1005ba12013-06-19 13:52:44 -0400406 if (contact_bubble != null) {
Alexandre Lisionee2494d2013-10-09 17:14:00 -0400407 contact_bubble.setCall(call);
alision1005ba12013-06-19 13:52:44 -0400408 contact_bubble.attractor.set(x, y);
409 return contact_bubble;
410 }
alision84813a12013-05-27 17:40:39 -0400411
Alexandre Lision40954dc2013-10-09 15:24:03 -0400412 contact_bubble = new BubbleContact(getActivity(), call, x, y, BUBBLE_SIZE);
413
414 model.addBubble(contact_bubble);
415 return contact_bubble;
416 }
417
Alexandre Lisionee2494d2013-10-09 17:14:00 -0400418 private Bubble getBubbleForUser(Conference conf, float x, float y) {
419 Bubble contact_bubble = model.getBubble(myself.getCallId());
Alexandre Lision40954dc2013-10-09 15:24:03 -0400420 if (contact_bubble != null) {
421 contact_bubble.attractor.set(x, y);
Alexandre Lisionee2494d2013-10-09 17:14:00 -0400422 contact_bubble.setConference(conf);
Alexandre Lision40954dc2013-10-09 15:24:03 -0400423 return contact_bubble;
424 }
425
426 contact_bubble = new BubbleUser(getActivity(), myself, conf, x, y, BUBBLE_SIZE);
alision84813a12013-05-27 17:40:39 -0400427
alision1005ba12013-06-19 13:52:44 -0400428 model.addBubble(contact_bubble);
alision1005ba12013-06-19 13:52:44 -0400429 return contact_bubble;
430 }
Adrien Béraud0c9bd8f2013-05-30 16:16:57 +1000431
alision1005ba12013-06-19 13:52:44 -0400432 /**
433 * Should be called when a bubble is removed from the model
434 */
435 void bubbleRemoved(Bubble b) {
Alexandre Lision40954dc2013-10-09 15:24:03 -0400436// if (b.associated_call == null) {
437// return;
438// }
alision1005ba12013-06-19 13:52:44 -0400439 }
Adrien Béraudc9c424d2013-05-30 17:47:35 +1000440
alision1005ba12013-06-19 13:52:44 -0400441 public void changeCallState(String callID, String newState) {
Alexandre Lisionf1850c02013-09-23 14:19:34 -0400442 Log.w(TAG, "Call :" + callID + " " + newState);
alisiondf1dac92013-06-27 17:35:53 -0400443 if (newState.contentEquals("FAILURE")) {
444 try {
445 mCallbacks.getService().hangUp(callID);
446 } catch (RemoteException e) {
447 e.printStackTrace();
alision806e18e2013-06-21 15:30:17 -0400448 }
alision1005ba12013-06-19 13:52:44 -0400449 }
alisiondf1dac92013-06-27 17:35:53 -0400450 for (int i = 0; i < conf.getParticipants().size(); ++i) {
alisiondf1dac92013-06-27 17:35:53 -0400451 if (callID.equals(conf.getParticipants().get(i).getCallId())) {
452 if (newState.contentEquals("HUNGUP")) {
alisiondf1dac92013-06-27 17:35:53 -0400453 model.removeBubble(conf.getParticipants().get(i));
454 conf.getParticipants().remove(i);
455 } else {
456 conf.getParticipants().get(i).setCallState(newState);
457 }
458 }
459 }
460
461 if (conf.isOnGoing())
462 initNormalStateDisplay();
463
464 if (conf.getParticipants().size() == 0) {
465 mCallbacks.replaceCurrentCallDisplayed();
466 }
alision1005ba12013-06-19 13:52:44 -0400467 }
alision84813a12013-05-27 17:40:39 -0400468
alision1005ba12013-06-19 13:52:44 -0400469 public boolean draggingBubble() {
470 return view == null ? false : view.isDraggingBubble();
471 }
Adrien Béraudc9c424d2013-05-30 17:47:35 +1000472
alision1005ba12013-06-19 13:52:44 -0400473 @Override
474 public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
Adrien Béraud13cde0b2013-05-30 20:27:20 +1000475
alisiondf1dac92013-06-27 17:35:53 -0400476 if (conf.getParticipants().size() == 1) {
alisiondf1dac92013-06-27 17:35:53 -0400477 if (conf.getParticipants().get(0).isIncoming() && conf.getParticipants().get(0).isRinging()) {
alision806e18e2013-06-21 15:30:17 -0400478 initIncomingCallDisplay();
479 } else {
alisiondf1dac92013-06-27 17:35:53 -0400480 if (conf.getParticipants().get(0).isRinging()) {
alision1005ba12013-06-19 13:52:44 -0400481 initOutGoingCallDisplay();
482 }
alision806e18e2013-06-21 15:30:17 -0400483 try {
alisiondf1dac92013-06-27 17:35:53 -0400484 if (conf.getParticipants().get(0).isOutGoing()
485 && mCallbacks.getService().getCall(conf.getParticipants().get(0).getCallId()) == null) {
486 mCallbacks.getService().placeCall(conf.getParticipants().get(0));
alision806e18e2013-06-21 15:30:17 -0400487 initOutGoingCallDisplay();
alisiondf1dac92013-06-27 17:35:53 -0400488 } else if (conf.getParticipants().get(0).isOutGoing() && conf.getParticipants().get(0).isRinging()) {
alision806e18e2013-06-21 15:30:17 -0400489 initOutGoingCallDisplay();
490 }
491 } catch (RemoteException e) {
492 Log.e(TAG, e.toString());
493 }
alision1005ba12013-06-19 13:52:44 -0400494 }
alisiondf1dac92013-06-27 17:35:53 -0400495 if (conf.getParticipants().get(0).isOngoing()) {
alision806e18e2013-06-21 15:30:17 -0400496 initNormalStateDisplay();
497 }
alisiondf1dac92013-06-27 17:35:53 -0400498 } else if (conf.getParticipants().size() > 1) {
alision1005ba12013-06-19 13:52:44 -0400499 initNormalStateDisplay();
500 }
alision1005ba12013-06-19 13:52:44 -0400501 }
Adrien Béraud13cde0b2013-05-30 20:27:20 +1000502
Alexandre Lision40954dc2013-10-09 15:24:03 -0400503 public void makeTransfer(BubbleContact contact) {
Alexandre Lision2b237922013-09-09 16:23:02 -0400504 FragmentManager fm = getFragmentManager();
Alexandre Lision0eb02032013-09-27 16:32:40 -0400505 editName = TransferDFragment.newInstance();
Alexandre Lision2b237922013-09-09 16:23:02 -0400506 Bundle b = new Bundle();
Alexandre Lision6ae652d2013-09-26 16:39:20 -0400507 try {
508 b.putParcelableArrayList("calls", (ArrayList<Conference>)mCallbacks.getService().getConcurrentCalls());
509 b.putParcelable("call_selected", contact.associated_call);
510 editName.setArguments(b);
511 editName.setTargetFragment(this, REQUEST_TRANSFER);
512 editName.show(fm, "");
513 } catch (RemoteException e) {
514 Log.e(TAG, e.toString());
515 }
516
Alexandre Lision2b237922013-09-09 16:23:02 -0400517 }
518
alision1005ba12013-06-19 13:52:44 -0400519 @Override
520 public void surfaceCreated(SurfaceHolder holder) {
Alexandre Lisionc30e8412013-09-26 15:12:59 -0400521
alision1005ba12013-06-19 13:52:44 -0400522 }
Adrien Béraud13cde0b2013-05-30 20:27:20 +1000523
alision1005ba12013-06-19 13:52:44 -0400524 @Override
525 public void surfaceDestroyed(SurfaceHolder holder) {
Alexandre Lision84208a32013-09-25 13:18:37 -0400526 // check that soft input is hidden
527 InputMethodManager lManager = (InputMethodManager) getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
528 lManager.hideSoftInputFromWindow(view.getWindowToken(), 0);
Alexandre Lision0eb02032013-09-27 16:32:40 -0400529 if(editName != null && editName.isVisible()){
530 editName.dismiss();
531 }
alision1005ba12013-06-19 13:52:44 -0400532 }
Adrien Béraud13cde0b2013-05-30 20:27:20 +1000533
alisione38001f2013-06-04 14:14:39 -0400534 public BubblesView getBubbleView() {
535 return view;
alision1005ba12013-06-19 13:52:44 -0400536 }
Alexandre Lision0c384512013-09-17 17:15:57 -0400537
538 public void updateTime() {
Adrien Béraude78d06f2013-09-19 13:33:44 +1000539 long duration = System.currentTimeMillis() / 1000 - this.conf.getParticipants().get(0).getTimestamp_start();
Alexandre Lisionf1850c02013-09-23 14:19:34 -0400540 callStatusTxt.setText(String.format("%d:%02d:%02d", duration / 3600, duration % 3600 / 60, duration % 60));
Alexandre Lisionf1850c02013-09-23 14:19:34 -0400541 }
542
543 @Override
544 public void onAccuracyChanged(Sensor sensor, int accuracy) {
Alexandre Lisionf1850c02013-09-23 14:19:34 -0400545
546 }
547
548 @Override
549 public void onSensorChanged(SensorEvent event) {
Alexandre Lisionc30e8412013-09-26 15:12:59 -0400550 if (event.values[0] == 0) {
551 WindowManager.LayoutParams params = getActivity().getWindow().getAttributes();
552 params.flags |= WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON;
553 params.flags = WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE;
554 params.screenBrightness = 0.004f;
555 getActivity().getWindow().setAttributes(params);
Adrien Béraude78d06f2013-09-19 13:33:44 +1000556
Alexandre Lisionc30e8412013-09-26 15:12:59 -0400557 } else {
558 WindowManager.LayoutParams params = getActivity().getWindow().getAttributes();
559 getActivity().getWindow().clearFlags(WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE);
560 params.flags = WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON;
561 params.screenBrightness = 1.0f;
562 getActivity().getWindow().setAttributes(params);
563 }
Alexandre Lision0c384512013-09-17 17:15:57 -0400564 }
Alexandre Lision0edf18c2013-09-23 17:35:50 -0400565
566 public Conference getConference() {
567 return conf;
568 }
Alexandre Lision64dc8c02013-09-25 15:32:25 -0400569
570 public void onKeyUp(int keyCode, KeyEvent event) {
571 try {
Alexandre Lision80f0a7e2013-09-26 12:27:51 -0400572 String toSend = Character.toString(event.getDisplayLabel());
Alexandre Lision31f46fc2013-09-26 11:19:54 -0400573 toSend.toUpperCase(Locale.getDefault());
Alexandre Lisionc30e8412013-09-26 15:12:59 -0400574 Log.d(TAG, "toSend " + toSend);
Alexandre Lision64dc8c02013-09-25 15:32:25 -0400575 mCallbacks.getService().playDtmf(toSend);
576 } catch (RemoteException e) {
577 e.printStackTrace();
578 }
579 }
alision84813a12013-05-27 17:40:39 -0400580}