blob: 2c306a747dc0e9e9e9fb0369fd5de1165fde737f [file] [log] [blame]
alisionfde875f2013-05-28 17:01:54 -04001/*
Alexandre Lisionc1024c02014-01-06 11:12:53 -05002 * Copyright (C) 2004-2014 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
alision84813a12013-05-27 17:40:39 -040034import android.app.Activity;
Alexandre Lision5f144b82014-02-11 09:59:36 -050035import android.content.BroadcastReceiver;
Alexandre Lisionf1850c02013-09-23 14:19:34 -040036import android.content.Context;
Alexandre Lision2b237922013-09-09 16:23:02 -040037import android.content.Intent;
Alexandre Lision5f144b82014-02-11 09:59:36 -050038import android.content.IntentFilter;
alision84813a12013-05-27 17:40:39 -040039import android.graphics.Bitmap;
Adrien Béraud0c9bd8f2013-05-30 16:16:57 +100040import android.graphics.BitmapFactory;
alision84813a12013-05-27 17:40:39 -040041import android.graphics.PointF;
Alexandre Lision5f144b82014-02-11 09:59:36 -050042import android.net.wifi.WifiInfo;
43import android.net.wifi.WifiManager;
alision84813a12013-05-27 17:40:39 -040044import android.os.Bundle;
Alexandre Lisionf02190d2013-12-12 17:26:12 -050045import android.os.PowerManager;
Alexandre Lisionf02190d2013-12-12 17:26:12 -050046import android.os.PowerManager.WakeLock;
Alexandre Lision5f144b82014-02-11 09:59:36 -050047import android.os.RemoteException;
48import android.support.v4.app.FragmentManager;
alision84813a12013-05-27 17:40:39 -040049import android.util.Log;
Alexandre Lision5f144b82014-02-11 09:59:36 -050050import android.view.*;
Adrien Béraud13cde0b2013-05-30 20:27:20 +100051import android.view.SurfaceHolder.Callback;
Alexandre Lision84208a32013-09-25 13:18:37 -040052import android.view.View.OnClickListener;
Alexandre Lision84208a32013-09-25 13:18:37 -040053import android.view.inputmethod.InputMethodManager;
Alexandre Lision48b49eb2014-02-11 13:37:33 -050054import android.widget.Button;
Alexandre Lision35577132013-12-06 15:21:15 -050055import android.widget.CompoundButton;
56import android.widget.CompoundButton.OnCheckedChangeListener;
Alexandre Lision3c6b7102013-09-16 16:56:46 -040057import android.widget.TextView;
Alexandre Lision35577132013-12-06 15:21:15 -050058import android.widget.ToggleButton;
Alexandre Lision5f144b82014-02-11 09:59:36 -050059import org.sflphone.R;
60import org.sflphone.interfaces.CallInterface;
61import org.sflphone.model.*;
62import org.sflphone.service.ISipService;
alision84813a12013-05-27 17:40:39 -040063
Alexandre Lision5f144b82014-02-11 09:59:36 -050064import java.util.ArrayList;
65import java.util.Locale;
66
67public class CallFragment extends CallableWrapperFragment implements CallInterface, Callback {
alision84813a12013-05-27 17:40:39 -040068
alision1005ba12013-06-19 13:52:44 -040069 static final String TAG = "CallFragment";
alision84813a12013-05-27 17:40:39 -040070
alisionf57d8a62013-07-02 09:37:12 -040071 float BUBBLE_SIZE = 75;
alision1005ba12013-06-19 13:52:44 -040072 static final float ATTRACTOR_SIZE = 40;
Alexandre Lision2b237922013-09-09 16:23:02 -040073 public static final int REQUEST_TRANSFER = 10;
74
Alexandre Lision48b49eb2014-02-11 13:37:33 -050075
Alexandre Lision3c6b7102013-09-16 16:56:46 -040076 private TextView callStatusTxt;
Alexandre Lision35577132013-12-06 15:21:15 -050077 private ToggleButton speakers;
Alexandre Lisionb8add812013-10-24 11:42:42 -040078
Alexandre Lision5f144b82014-02-11 09:59:36 -050079
Alexandre Lisionf02190d2013-12-12 17:26:12 -050080 private PowerManager powerManager;
81 // Screen wake lock for incoming call
82 private WakeLock wakeLock;
83
alision1005ba12013-06-19 13:52:44 -040084 private BubblesView view;
85 private BubbleModel model;
alision84813a12013-05-27 17:40:39 -040086
Alexandre Lision68855472013-10-10 16:20:46 -040087 public Callbacks mCallbacks = sDummyCallbacks;
Alexandre Lisiona764c682013-09-09 10:02:07 -040088 boolean accepted = false;
alision1005ba12013-06-19 13:52:44 -040089 private Bitmap call_icon;
alision85704182013-05-29 15:23:03 -040090
Alexandre Lision0eb02032013-09-27 16:32:40 -040091 TransferDFragment editName;
Alexandre Lisiona3c4f5f2014-01-07 17:22:22 -050092 private WifiManager wifiManager;
93 private BroadcastReceiver wifiReceiver = new BroadcastReceiver() {
94
95 @Override
96 public void onReceive(Context context, Intent intent) {
97 WifiInfo info = wifiManager.getConnectionInfo();
Alexandre Lisiona7ab2e32014-02-14 15:33:33 -050098 Log.i(TAG, "Level of wifi " + info.getRssi());
Alexandre Lisiona3c4f5f2014-01-07 17:22:22 -050099 }
100
101 };
Alexandre Lisionf1850c02013-09-23 14:19:34 -0400102
alision1005ba12013-06-19 13:52:44 -0400103 @Override
104 public void onCreate(Bundle savedBundle) {
105 super.onCreate(savedBundle);
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 Lisiond588bff2013-10-08 12:43:01 -0400109 this.setHasOptionsMenu(true);
alision729b0a22013-07-02 11:57:33 -0400110
Alexandre Lisionf02190d2013-12-12 17:26:12 -0500111 powerManager = (PowerManager) getActivity().getSystemService(Context.POWER_SERVICE);
112 wakeLock = powerManager.newWakeLock(PowerManager.SCREEN_DIM_WAKE_LOCK | PowerManager.ACQUIRE_CAUSES_WAKEUP | PowerManager.ON_AFTER_RELEASE,
113 "org.sflphone.onIncomingCall");
114 wakeLock.setReferenceCounted(false);
115
Alexandre Lision5f144b82014-02-11 09:59:36 -0500116
Alexandre Lisionf02190d2013-12-12 17:26:12 -0500117 Log.d(TAG, "Acquire wake up lock");
118 if (wakeLock != null && !wakeLock.isHeld()) {
119 wakeLock.acquire();
120 }
Alexandre Lisiona3c4f5f2014-01-07 17:22:22 -0500121 }
Alexandre Lisionf02190d2013-12-12 17:26:12 -0500122
Alexandre Lisiona7ab2e32014-02-14 15:33:33 -0500123 private void initializeWiFiListener() {
Alexandre Lisiona3c4f5f2014-01-07 17:22:22 -0500124 Log.i(TAG, "executing initializeWiFiListener");
125
126 String connectivity_context = Context.WIFI_SERVICE;
127 wifiManager = (WifiManager) getActivity().getSystemService(connectivity_context);
128
Alexandre Lisiona7ab2e32014-02-14 15:33:33 -0500129 if (!wifiManager.isWifiEnabled()) {
130 if (wifiManager.getWifiState() != WifiManager.WIFI_STATE_ENABLING) {
Alexandre Lisiona3c4f5f2014-01-07 17:22:22 -0500131 wifiManager.setWifiEnabled(true);
132 }
133 }
134
135 getActivity().registerReceiver(wifiReceiver, new IntentFilter(WifiManager.RSSI_CHANGED_ACTION));
alision1005ba12013-06-19 13:52:44 -0400136 }
alision85992112013-05-29 12:18:08 -0400137
alision1005ba12013-06-19 13:52:44 -0400138 /**
139 * A dummy implementation of the {@link Callbacks} interface that does nothing. Used only when this fragment is not attached to an activity.
140 */
141 private static Callbacks sDummyCallbacks = new Callbacks() {
alision85704182013-05-29 15:23:03 -0400142
alision1005ba12013-06-19 13:52:44 -0400143 @Override
144 public ISipService getService() {
145 return null;
146 }
alisiondf1dac92013-06-27 17:35:53 -0400147
Alexandre Lisionccf0c932013-10-10 16:24:41 -0400148 @Override
Alexandre Lisiondafe9512013-10-28 16:31:41 -0400149 public void terminateCall() {
Alexandre Lisionccf0c932013-10-10 16:24:41 -0400150 }
Alexandre Lision0c384512013-09-17 17:15:57 -0400151
152 @Override
Alexandre Lision48b49eb2014-02-11 13:37:33 -0500153 public Conference getDisplayedConference() {
154 return null;
155 }
156
157 @Override
Alexandre Lisiona7ab2e32014-02-14 15:33:33 -0500158 public void updateDisplayedConference(Conference c) {
159 }
160
161 @Override
Adrien Béraude78d06f2013-09-19 13:33:44 +1000162 public void startTimer() {
Alexandre Lision0c384512013-09-17 17:15:57 -0400163 }
Alexandre Lision68855472013-10-10 16:20:46 -0400164
165 @Override
Alexandre Lisionb8add812013-10-24 11:42:42 -0400166 public void slideChatScreen() {
Alexandre Lision68855472013-10-10 16:20:46 -0400167 }
Alexandre Lisiona7ab2e32014-02-14 15:33:33 -0500168
alision1005ba12013-06-19 13:52:44 -0400169 };
alision907bde72013-06-20 14:40:37 -0400170
alision1005ba12013-06-19 13:52:44 -0400171 /**
172 * The Activity calling this fragment has to implement this interface
alision1005ba12013-06-19 13:52:44 -0400173 */
174 public interface Callbacks {
alision85992112013-05-29 12:18:08 -0400175
alision1005ba12013-06-19 13:52:44 -0400176 public ISipService getService();
alision85704182013-05-29 15:23:03 -0400177
Alexandre Lision0c384512013-09-17 17:15:57 -0400178 public void startTimer();
Alexandre Lision68855472013-10-10 16:20:46 -0400179
180 public void slideChatScreen();
Alexandre Lisionb8add812013-10-24 11:42:42 -0400181
Alexandre Lisiondafe9512013-10-28 16:31:41 -0400182 public void terminateCall();
Alexandre Lision48b49eb2014-02-11 13:37:33 -0500183
184 public Conference getDisplayedConference();
Alexandre Lisiona7ab2e32014-02-14 15:33:33 -0500185
186 public void updateDisplayedConference(Conference c);
alision1005ba12013-06-19 13:52:44 -0400187 }
alision85992112013-05-29 12:18:08 -0400188
alision1005ba12013-06-19 13:52:44 -0400189 @Override
190 public void onAttach(Activity activity) {
191 super.onAttach(activity);
alision85992112013-05-29 12:18:08 -0400192
alision1005ba12013-06-19 13:52:44 -0400193 if (!(activity instanceof Callbacks)) {
194 throw new IllegalStateException("Activity must implement fragment's callbacks.");
195 }
alision85992112013-05-29 12:18:08 -0400196
alision1005ba12013-06-19 13:52:44 -0400197 // rootView.requestDisallowInterceptTouchEvent(true);
alision85992112013-05-29 12:18:08 -0400198
alision1005ba12013-06-19 13:52:44 -0400199 mCallbacks = (Callbacks) activity;
Alexandre Lisionb8add812013-10-24 11:42:42 -0400200 // myself = SipCall.SipCallBuilder.buildMyselfCall(activity.getContentResolver(), "Me");
Adrien Béraude78d06f2013-09-19 13:33:44 +1000201
alision1005ba12013-06-19 13:52:44 -0400202 }
Alexandre Lisionb8add812013-10-24 11:42:42 -0400203
Alexandre Lisiond588bff2013-10-08 12:43:01 -0400204 @Override
205 public void onCreateOptionsMenu(Menu m, MenuInflater inf) {
206 super.onCreateOptionsMenu(m, inf);
Alexandre Lision68855472013-10-10 16:20:46 -0400207 inf.inflate(R.menu.ac_call, m);
Alexandre Lisiond588bff2013-10-08 12:43:01 -0400208 }
209
210 @Override
211 public boolean onOptionsItemSelected(MenuItem item) {
212 super.onOptionsItemSelected(item);
Alexandre Lision666b3772013-10-28 17:42:48 -0400213 switch (item.getItemId()) {
Alexandre Lisiona7ab2e32014-02-14 15:33:33 -0500214 case R.id.menuitem_chat:
215 mCallbacks.slideChatScreen();
216 break;
Alexandre Lision666b3772013-10-28 17:42:48 -0400217 }
Alexandre Lisiond588bff2013-10-08 12:43:01 -0400218
219 return true;
220 }
alision85992112013-05-29 12:18:08 -0400221
alision1005ba12013-06-19 13:52:44 -0400222 @Override
223 public void onDetach() {
224 super.onDetach();
225 mCallbacks = sDummyCallbacks;
alision1005ba12013-06-19 13:52:44 -0400226 }
alision907bde72013-06-20 14:40:37 -0400227
alision1005ba12013-06-19 13:52:44 -0400228 @Override
alision907bde72013-06-20 14:40:37 -0400229 public void onStop() {
alision1005ba12013-06-19 13:52:44 -0400230 super.onStop();
231 }
alision85992112013-05-29 12:18:08 -0400232
alision1005ba12013-06-19 13:52:44 -0400233 @Override
Alexandre Lisionf1850c02013-09-23 14:19:34 -0400234 public void onResume() {
235 super.onResume();
Alexandre Lisiona3c4f5f2014-01-07 17:22:22 -0500236 initializeWiFiListener();
Alexandre Lisionf1850c02013-09-23 14:19:34 -0400237 }
238
239 @Override
240 public void onPause() {
241 super.onPause();
Alexandre Lisiona3c4f5f2014-01-07 17:22:22 -0500242 getActivity().unregisterReceiver(wifiReceiver);
Alexandre Lisionf02190d2013-12-12 17:26:12 -0500243 if (wakeLock != null && wakeLock.isHeld()) {
244 wakeLock.release();
245 }
246
Alexandre Lisionf1850c02013-09-23 14:19:34 -0400247 }
248
249 @Override
Alexandre Lisiona7ab2e32014-02-14 15:33:33 -0500250 public void callStateChanged(Conference updated, String callID, String newState) {
251 mCallbacks.updateDisplayedConference(updated);
252 Log.i(TAG, "Call :" + callID + " " + newState);
253
254 if (getConference().isOnGoing()) {
255 initNormalStateDisplay();
256 } else if (getConference().isRinging()) {
257 callStatusTxt.setText(newState);
258
259 if (getConference().isIncoming()) {
260 initIncomingCallDisplay();
261 } else
262 initOutGoingCallDisplay();
263 } else {
264 callStatusTxt.setText(newState);
265 mCallbacks.terminateCall();
266 }
Alexandre Lision5f144b82014-02-11 09:59:36 -0500267 }
268
269 @Override
Alexandre Lisiona7ab2e32014-02-14 15:33:33 -0500270 public void secureZrtpOn(Conference updated, String id) {
Alexandre Lision5f144b82014-02-11 09:59:36 -0500271 Log.i(TAG, "secureZrtpOn");
Alexandre Lisiona7ab2e32014-02-14 15:33:33 -0500272 mCallbacks.updateDisplayedConference(updated);
Alexandre Lision5f144b82014-02-11 09:59:36 -0500273 //enableSASButton();
274 }
275
276 @Override
Alexandre Lisiona7ab2e32014-02-14 15:33:33 -0500277 public void secureZrtpOff(Conference updated, String id) {
Alexandre Lision5f144b82014-02-11 09:59:36 -0500278 Log.i(TAG, "secureZrtpOff");
Alexandre Lisiona7ab2e32014-02-14 15:33:33 -0500279 mCallbacks.updateDisplayedConference(updated);
Alexandre Lision5f144b82014-02-11 09:59:36 -0500280 }
281
282 @Override
Alexandre Lisiona7ab2e32014-02-14 15:33:33 -0500283 public void displaySAS(Conference updated, final String securedCallID) {
Alexandre Lision5f144b82014-02-11 09:59:36 -0500284 Log.i(TAG, "displaySAS");
Alexandre Lisiona7ab2e32014-02-14 15:33:33 -0500285 mCallbacks.updateDisplayedConference(updated);
286 SecureSipCall display = (SecureSipCall) getConference().getCallById(securedCallID);
287
288 if (display != null) {
289 if (!display.isConfirmedSAS()) {
290 final Button sas = (Button) getView().findViewById(R.id.confirm_sas);
291 sas.setText("Confirm SAS: " + display.getSAS());
292 sas.setVisibility(View.VISIBLE);
293 sas.setOnClickListener(new OnClickListener() {
294 @Override
295 public void onClick(View v) {
296 try {
297 mCallbacks.getService().confirmSAS(securedCallID);
298 sas.setVisibility(View.INVISIBLE);
299 } catch (RemoteException e) {
300 e.printStackTrace();
301 }
302 }
303 });
Alexandre Lision48b49eb2014-02-11 13:37:33 -0500304 }
Alexandre Lisiona7ab2e32014-02-14 15:33:33 -0500305 }
306
Alexandre Lision5f144b82014-02-11 09:59:36 -0500307 }
308
309 @Override
Alexandre Lision2b237922013-09-09 16:23:02 -0400310 public void onActivityResult(int requestCode, int resultCode, Intent data) {
311 super.onActivityResult(requestCode, resultCode, data);
Alexandre Lision5f144b82014-02-11 09:59:36 -0500312 SipCall transfer;
Alexandre Lision2b237922013-09-09 16:23:02 -0400313 if (requestCode == REQUEST_TRANSFER) {
314 switch (resultCode) {
Alexandre Lisiona7ab2e32014-02-14 15:33:33 -0500315 case TransferDFragment.RESULT_TRANSFER_CONF:
316 Conference c = data.getParcelableExtra("target");
317 transfer = data.getParcelableExtra("transfer");
318 try {
Alexandre Lision2b237922013-09-09 16:23:02 -0400319
Alexandre Lisiona7ab2e32014-02-14 15:33:33 -0500320 mCallbacks.getService().attendedTransfer(transfer.getCallId(), c.getParticipants().get(0).getCallId());
Alexandre Lision2b237922013-09-09 16:23:02 -0400321
Alexandre Lisiona7ab2e32014-02-14 15:33:33 -0500322 } catch (RemoteException e) {
323 e.printStackTrace();
324 }
325 break;
Alexandre Lision2b237922013-09-09 16:23:02 -0400326
Alexandre Lisiona7ab2e32014-02-14 15:33:33 -0500327 case TransferDFragment.RESULT_TRANSFER_NUMBER:
328 String to = data.getStringExtra("to_number");
329 transfer = data.getParcelableExtra("transfer");
330 try {
331 mCallbacks.getService().transfer(transfer.getCallId(), to);
332 mCallbacks.getService().hangUp(transfer.getCallId());
333 } catch (RemoteException e) {
334 e.printStackTrace();
335 }
336 break;
337 case Activity.RESULT_CANCELED:
338 default:
339 model.clear();
340 initNormalStateDisplay();
341 break;
Alexandre Lision2b237922013-09-09 16:23:02 -0400342 }
343 }
344 }
345
346 @Override
alision1005ba12013-06-19 13:52:44 -0400347 public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
Alexandre Lision84208a32013-09-25 13:18:37 -0400348 final ViewGroup rootView = (ViewGroup) inflater.inflate(R.layout.frag_call, container, false);
alision85992112013-05-29 12:18:08 -0400349
alision1005ba12013-06-19 13:52:44 -0400350 view = (BubblesView) rootView.findViewById(R.id.main_view);
alision34673e62013-06-25 14:40:07 -0400351 view.setFragment(this);
alision1005ba12013-06-19 13:52:44 -0400352 view.setModel(model);
353 view.getHolder().addCallback(this);
alision84813a12013-05-27 17:40:39 -0400354
Alexandre Lision3c6b7102013-09-16 16:56:46 -0400355 callStatusTxt = (TextView) rootView.findViewById(R.id.call_status_txt);
Alexandre Lision68855472013-10-10 16:20:46 -0400356 call_icon = BitmapFactory.decodeResource(getResources(), R.drawable.ic_action_call);
Alexandre Lisioncb2345c2013-12-09 15:39:13 -0500357
Alexandre Lisionbaeeb212013-12-09 12:54:47 -0500358 speakers = (ToggleButton) rootView.findViewById(R.id.speaker_toggle);
Alexandre Lisioncb2345c2013-12-09 15:39:13 -0500359
Alexandre Lision35577132013-12-06 15:21:15 -0500360 speakers.setOnCheckedChangeListener(new OnCheckedChangeListener() {
Alexandre Lisioncb2345c2013-12-09 15:39:13 -0500361
Alexandre Lision35577132013-12-06 15:21:15 -0500362 @Override
363 public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
364 try {
365 mCallbacks.getService().toggleSpeakerPhone(isChecked);
366 } catch (RemoteException e) {
367 e.printStackTrace();
368 }
Alexandre Lisioncb2345c2013-12-09 15:39:13 -0500369
Alexandre Lision35577132013-12-06 15:21:15 -0500370 }
371 });
alision84813a12013-05-27 17:40:39 -0400372
Alexandre Lision5f144b82014-02-11 09:59:36 -0500373 rootView.findViewById(R.id.dialpad_btn).setOnClickListener(new OnClickListener() {
Alexandre Lision84208a32013-09-25 13:18:37 -0400374
375 @Override
376 public void onClick(View v) {
377 InputMethodManager lManager = (InputMethodManager) getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
378 lManager.toggleSoftInput(InputMethodManager.SHOW_IMPLICIT, InputMethodManager.HIDE_IMPLICIT_ONLY);
379 }
380 });
381
alision1005ba12013-06-19 13:52:44 -0400382 return rootView;
383 }
Alexandre Lisionc30e8412013-09-26 15:12:59 -0400384
Alexandre Lisiona7ab2e32014-02-14 15:33:33 -0500385 public Conference getConference() {
Alexandre Lision48b49eb2014-02-11 13:37:33 -0500386 return mCallbacks.getDisplayedConference();
387 }
388
alision1005ba12013-06-19 13:52:44 -0400389 private void initNormalStateDisplay() {
390 Log.i(TAG, "Start normal display");
alision84813a12013-05-27 17:40:39 -0400391
Alexandre Lision0c384512013-09-17 17:15:57 -0400392 mCallbacks.startTimer();
alision84813a12013-05-27 17:40:39 -0400393
Alexandre Lision48b49eb2014-02-11 13:37:33 -0500394 getBubbleForUser(getConference(), model.width / 2, model.height / 2);
alision907bde72013-06-20 14:40:37 -0400395
Alexandre Lision48b49eb2014-02-11 13:37:33 -0500396 int angle_part = 360 / getConference().getParticipants().size();
Alexandre Lision5f144b82014-02-11 09:59:36 -0500397 double dX, dY;
alision465ceba2013-07-04 09:24:30 -0400398 int radiusCalls = (int) (model.width / 2 - BUBBLE_SIZE);
Alexandre Lision48b49eb2014-02-11 13:37:33 -0500399 for (int i = 0; i < getConference().getParticipants().size(); ++i) {
alisiondf1dac92013-06-27 17:35:53 -0400400
Alexandre Lision48b49eb2014-02-11 13:37:33 -0500401 if (getConference().getParticipants().get(i) == null) {
alisiondf1dac92013-06-27 17:35:53 -0400402 continue;
403 }
alision34673e62013-06-25 14:40:07 -0400404 dX = Math.cos(Math.toRadians(angle_part * i - 90)) * radiusCalls;
405 dY = Math.sin(Math.toRadians(angle_part * i - 90)) * radiusCalls;
Alexandre Lision48b49eb2014-02-11 13:37:33 -0500406 getBubbleFor(getConference().getParticipants().get(i), (int) (model.width / 2 + dX), (int) (model.height / 2 + dY));
alision806e18e2013-06-21 15:30:17 -0400407 }
alision1005ba12013-06-19 13:52:44 -0400408
409 model.clearAttractors();
alision1005ba12013-06-19 13:52:44 -0400410 }
alision85704182013-05-29 15:23:03 -0400411
alision1005ba12013-06-19 13:52:44 -0400412 private void initIncomingCallDisplay() {
413 Log.i(TAG, "Start incoming display");
alision84813a12013-05-27 17:40:39 -0400414
Alexandre Lision23628c12013-09-24 11:17:05 -0400415 int radiusCalls = (int) (model.width / 2 - BUBBLE_SIZE);
Alexandre Lision48b49eb2014-02-11 13:37:33 -0500416 getBubbleForUser(getConference(), model.width / 2, model.height / 2 + radiusCalls);
417 getBubbleFor(getConference().getParticipants().get(0), model.width / 2, model.height / 2 - radiusCalls);
alision85704182013-05-29 15:23:03 -0400418
alision1005ba12013-06-19 13:52:44 -0400419 model.clearAttractors();
Alexandre Lision23628c12013-09-24 11:17:05 -0400420 model.addAttractor(new Attractor(new PointF(model.width / 2, model.height / 2), ATTRACTOR_SIZE, new Attractor.Callback() {
alision1005ba12013-06-19 13:52:44 -0400421 @Override
422 public boolean onBubbleSucked(Bubble b) {
Alexandre Lisiona764c682013-09-09 10:02:07 -0400423 if (!accepted) {
Alexandre Lision68855472013-10-10 16:20:46 -0400424 try {
425 mCallbacks.getService().accept(b.getCallID());
426 } catch (RemoteException e) {
427 e.printStackTrace();
428 }
Alexandre Lisiona764c682013-09-09 10:02:07 -0400429 accepted = true;
430 }
alision1005ba12013-06-19 13:52:44 -0400431 return false;
432 }
433 }, call_icon));
Alexandre Lisioncb2345c2013-12-09 15:39:13 -0500434
Alexandre Lision48b49eb2014-02-11 13:37:33 -0500435 if (getConference().getParticipants().get(0).getAccount().isAutoanswerEnabled()) {
Alexandre Lision332f4912013-11-12 13:04:32 -0500436 try {
Alexandre Lision48b49eb2014-02-11 13:37:33 -0500437 mCallbacks.getService().accept(getConference().getParticipants().get(0).getCallId());
Alexandre Lision332f4912013-11-12 13:04:32 -0500438 } catch (RemoteException e) {
439 e.printStackTrace();
440 }
441 }
alision1005ba12013-06-19 13:52:44 -0400442 }
alision85704182013-05-29 15:23:03 -0400443
alision1005ba12013-06-19 13:52:44 -0400444 private void initOutGoingCallDisplay() {
445 Log.i(TAG, "Start outgoing display");
alision85704182013-05-29 15:23:03 -0400446
Alexandre Lision48b49eb2014-02-11 13:37:33 -0500447 getBubbleForUser(getConference(), model.width / 2, model.height / 2);
alision85704182013-05-29 15:23:03 -0400448
alisiondf1dac92013-06-27 17:35:53 -0400449 // TODO off-thread image loading
Alexandre Lision48b49eb2014-02-11 13:37:33 -0500450 int angle_part = 360 / getConference().getParticipants().size();
Alexandre Lision5f144b82014-02-11 09:59:36 -0500451 double dX, dY;
Adrien Béraude78d06f2013-09-19 13:33:44 +1000452 int radiusCalls = (int) (model.width / 2 - BUBBLE_SIZE);
Alexandre Lision48b49eb2014-02-11 13:37:33 -0500453 for (int i = 0; i < getConference().getParticipants().size(); ++i) {
alisiondf1dac92013-06-27 17:35:53 -0400454 dX = Math.cos(Math.toRadians(angle_part * i - 90)) * radiusCalls;
455 dY = Math.sin(Math.toRadians(angle_part * i - 90)) * radiusCalls;
Alexandre Lision48b49eb2014-02-11 13:37:33 -0500456 getBubbleFor(getConference().getParticipants().get(i), (int) (model.width / 2 + dX), (int) (model.height / 2 + dY));
alisiondf1dac92013-06-27 17:35:53 -0400457 }
alision907bde72013-06-20 14:40:37 -0400458
alision1005ba12013-06-19 13:52:44 -0400459 model.clearAttractors();
alision1005ba12013-06-19 13:52:44 -0400460 }
alision85704182013-05-29 15:23:03 -0400461
alision1005ba12013-06-19 13:52:44 -0400462 /**
463 * Retrieves or create a bubble for a given contact. If the bubble exists, it is moved to the new location.
Alexandre Lisiona7ab2e32014-02-14 15:33:33 -0500464 *
465 * @param call The call associated to a contact
466 * @param x Initial or new x position.
467 * @param y Initial or new y position.
alision1005ba12013-06-19 13:52:44 -0400468 * @return Bubble corresponding to the contact.
469 */
alision806e18e2013-06-21 15:30:17 -0400470 private Bubble getBubbleFor(SipCall call, float x, float y) {
Alexandre Lisionee2494d2013-10-09 17:14:00 -0400471 Bubble contact_bubble = model.getBubble(call.getCallId());
alision1005ba12013-06-19 13:52:44 -0400472 if (contact_bubble != null) {
Alexandre Lisionb8add812013-10-24 11:42:42 -0400473 ((BubbleContact) contact_bubble).setCall(call);
alision1005ba12013-06-19 13:52:44 -0400474 contact_bubble.attractor.set(x, y);
475 return contact_bubble;
476 }
alision84813a12013-05-27 17:40:39 -0400477
Alexandre Lision40954dc2013-10-09 15:24:03 -0400478 contact_bubble = new BubbleContact(getActivity(), call, x, y, BUBBLE_SIZE);
479
480 model.addBubble(contact_bubble);
481 return contact_bubble;
482 }
Alexandre Lisionb8add812013-10-24 11:42:42 -0400483
Alexandre Lisionee2494d2013-10-09 17:14:00 -0400484 private Bubble getBubbleForUser(Conference conf, float x, float y) {
Alexandre Lisiond5686032013-10-29 11:09:21 -0400485 Bubble contact_bubble = model.getUser();
Alexandre Lision40954dc2013-10-09 15:24:03 -0400486 if (contact_bubble != null) {
487 contact_bubble.attractor.set(x, y);
Alexandre Lisionb8add812013-10-24 11:42:42 -0400488 ((BubbleUser) contact_bubble).setConference(conf);
Alexandre Lisioncb2345c2013-12-09 15:39:13 -0500489
Alexandre Lision40954dc2013-10-09 15:24:03 -0400490 return contact_bubble;
491 }
492
Alexandre Lisioncb2345c2013-12-09 15:39:13 -0500493 contact_bubble = new BubbleUser(getActivity(), CallContact.ContactBuilder.buildUserContact(getActivity().getContentResolver()), conf, x, y,
494 BUBBLE_SIZE * 1.3f);
alision84813a12013-05-27 17:40:39 -0400495
Alexandre Lisionc9fca9e2013-11-08 15:21:18 -0500496 try {
497 ((BubbleUser) contact_bubble).setMute(mCallbacks.getService().isCaptureMuted());
498 } catch (RemoteException e) {
499 e.printStackTrace();
Alexandre Lisioncb2345c2013-12-09 15:39:13 -0500500 } catch (NullPointerException e1) {
Alexandre Lisionc9fca9e2013-11-08 15:21:18 -0500501 e1.printStackTrace();
502 }
alision1005ba12013-06-19 13:52:44 -0400503 model.addBubble(contact_bubble);
alision1005ba12013-06-19 13:52:44 -0400504 return contact_bubble;
505 }
Adrien Béraud0c9bd8f2013-05-30 16:16:57 +1000506
alision1005ba12013-06-19 13:52:44 -0400507 public boolean draggingBubble() {
508 return view == null ? false : view.isDraggingBubble();
509 }
Adrien Béraudc9c424d2013-05-30 17:47:35 +1000510
alision1005ba12013-06-19 13:52:44 -0400511 @Override
512 public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
Adrien Béraud13cde0b2013-05-30 20:27:20 +1000513
Alexandre Lision48b49eb2014-02-11 13:37:33 -0500514 if (getConference().getParticipants().size() == 1) {
515 if (getConference().getParticipants().get(0).isIncoming() && getConference().getParticipants().get(0).isRinging()) {
alision806e18e2013-06-21 15:30:17 -0400516 initIncomingCallDisplay();
Alexandre Lisiona7ab2e32014-02-14 15:33:33 -0500517 } else if (getConference().getParticipants().get(0).isRinging()) {
518 initOutGoingCallDisplay();
519 } else if (getConference().getParticipants().get(0).isOngoing()) {
alision806e18e2013-06-21 15:30:17 -0400520 initNormalStateDisplay();
521 }
Alexandre Lision48b49eb2014-02-11 13:37:33 -0500522 } else if (getConference().getParticipants().size() > 1) {
alision1005ba12013-06-19 13:52:44 -0400523 initNormalStateDisplay();
524 }
alision1005ba12013-06-19 13:52:44 -0400525 }
Adrien Béraud13cde0b2013-05-30 20:27:20 +1000526
Alexandre Lision40954dc2013-10-09 15:24:03 -0400527 public void makeTransfer(BubbleContact contact) {
Alexandre Lision2b237922013-09-09 16:23:02 -0400528 FragmentManager fm = getFragmentManager();
Alexandre Lision0eb02032013-09-27 16:32:40 -0400529 editName = TransferDFragment.newInstance();
Alexandre Lision2b237922013-09-09 16:23:02 -0400530 Bundle b = new Bundle();
Alexandre Lision6ae652d2013-09-26 16:39:20 -0400531 try {
Alexandre Lisionb8add812013-10-24 11:42:42 -0400532 b.putParcelableArrayList("calls", (ArrayList<Conference>) mCallbacks.getService().getConcurrentCalls());
Alexandre Lision6ae652d2013-09-26 16:39:20 -0400533 b.putParcelable("call_selected", contact.associated_call);
534 editName.setArguments(b);
535 editName.setTargetFragment(this, REQUEST_TRANSFER);
536 editName.show(fm, "");
537 } catch (RemoteException e) {
538 Log.e(TAG, e.toString());
539 }
540
Alexandre Lision2b237922013-09-09 16:23:02 -0400541 }
542
alision1005ba12013-06-19 13:52:44 -0400543 @Override
544 public void surfaceCreated(SurfaceHolder holder) {
Alexandre Lisionc30e8412013-09-26 15:12:59 -0400545
alision1005ba12013-06-19 13:52:44 -0400546 }
Adrien Béraud13cde0b2013-05-30 20:27:20 +1000547
alision1005ba12013-06-19 13:52:44 -0400548 @Override
549 public void surfaceDestroyed(SurfaceHolder holder) {
Alexandre Lision84208a32013-09-25 13:18:37 -0400550 // check that soft input is hidden
551 InputMethodManager lManager = (InputMethodManager) getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
552 lManager.hideSoftInputFromWindow(view.getWindowToken(), 0);
Alexandre Lisionb8add812013-10-24 11:42:42 -0400553 if (editName != null && editName.isVisible()) {
Alexandre Lision0eb02032013-09-27 16:32:40 -0400554 editName.dismiss();
555 }
alision1005ba12013-06-19 13:52:44 -0400556 }
Adrien Béraud13cde0b2013-05-30 20:27:20 +1000557
alisione38001f2013-06-04 14:14:39 -0400558 public BubblesView getBubbleView() {
559 return view;
alision1005ba12013-06-19 13:52:44 -0400560 }
Alexandre Lision0c384512013-09-17 17:15:57 -0400561
562 public void updateTime() {
Alexandre Lisiona7ab2e32014-02-14 15:33:33 -0500563 if (getConference() != null) {
564 long duration = System.currentTimeMillis() - getConference().getParticipants().get(0).getTimestampStart_();
565 duration = duration / 1000;
566 if (getConference().isOnGoing())
567 callStatusTxt.setText(String.format("%d:%02d:%02d", duration / 3600, duration % 3600 / 60, duration % 60));
568 }
569
Alexandre Lisionf1850c02013-09-23 14:19:34 -0400570 }
571
Alexandre Lision64dc8c02013-09-25 15:32:25 -0400572 public void onKeyUp(int keyCode, KeyEvent event) {
573 try {
Alexandre Lisionb8add812013-10-24 11:42:42 -0400574
Alexandre Lisionfda4d1d2013-10-25 15:29:53 -0400575 switch (keyCode) {
Alexandre Lisiona7ab2e32014-02-14 15:33:33 -0500576 case KeyEvent.KEYCODE_VOLUME_DOWN:
577 case KeyEvent.KEYCODE_VOLUME_UP:
578 break;
579 default:
580 String toSend = Character.toString(event.getDisplayLabel());
581 toSend.toUpperCase(Locale.getDefault());
582 Log.d(TAG, "toSend " + toSend);
583 mCallbacks.getService().playDtmf(toSend);
584 break;
Alexandre Lisionfda4d1d2013-10-25 15:29:53 -0400585 }
Alexandre Lision64dc8c02013-09-25 15:32:25 -0400586 } catch (RemoteException e) {
587 e.printStackTrace();
Alexandre Lision3e1e06f2014-02-10 12:13:25 -0500588 } catch (NullPointerException e) {
589 e.printStackTrace();
Alexandre Lision64dc8c02013-09-25 15:32:25 -0400590 }
591 }
alision84813a12013-05-27 17:40:39 -0400592}