blob: 150ea49c86cd76ab5f3c30dca8c16e8d2f410f4b [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);
Alexandre Lision8162f4c2014-02-14 16:45:49 -0500286 SipCall call = getConference().getCallById(securedCallID);
Alexandre Lisiona7ab2e32014-02-14 15:33:33 -0500287
Alexandre Lision8162f4c2014-02-14 16:45:49 -0500288 if (call != null && call instanceof SecureSipCall) {
289 SecureSipCall display = (SecureSipCall) call;
Alexandre Lisiona7ab2e32014-02-14 15:33:33 -0500290 if (!display.isConfirmedSAS()) {
291 final Button sas = (Button) getView().findViewById(R.id.confirm_sas);
292 sas.setText("Confirm SAS: " + display.getSAS());
293 sas.setVisibility(View.VISIBLE);
294 sas.setOnClickListener(new OnClickListener() {
295 @Override
296 public void onClick(View v) {
297 try {
298 mCallbacks.getService().confirmSAS(securedCallID);
299 sas.setVisibility(View.INVISIBLE);
300 } catch (RemoteException e) {
301 e.printStackTrace();
302 }
303 }
304 });
Alexandre Lision48b49eb2014-02-11 13:37:33 -0500305 }
Alexandre Lisiona7ab2e32014-02-14 15:33:33 -0500306 }
307
Alexandre Lision5f144b82014-02-11 09:59:36 -0500308 }
309
310 @Override
Alexandre Lision2b237922013-09-09 16:23:02 -0400311 public void onActivityResult(int requestCode, int resultCode, Intent data) {
312 super.onActivityResult(requestCode, resultCode, data);
Alexandre Lision5f144b82014-02-11 09:59:36 -0500313 SipCall transfer;
Alexandre Lision2b237922013-09-09 16:23:02 -0400314 if (requestCode == REQUEST_TRANSFER) {
315 switch (resultCode) {
Alexandre Lisiona7ab2e32014-02-14 15:33:33 -0500316 case TransferDFragment.RESULT_TRANSFER_CONF:
317 Conference c = data.getParcelableExtra("target");
318 transfer = data.getParcelableExtra("transfer");
319 try {
Alexandre Lision2b237922013-09-09 16:23:02 -0400320
Alexandre Lisiona7ab2e32014-02-14 15:33:33 -0500321 mCallbacks.getService().attendedTransfer(transfer.getCallId(), c.getParticipants().get(0).getCallId());
Alexandre Lision2b237922013-09-09 16:23:02 -0400322
Alexandre Lisiona7ab2e32014-02-14 15:33:33 -0500323 } catch (RemoteException e) {
324 e.printStackTrace();
325 }
326 break;
Alexandre Lision2b237922013-09-09 16:23:02 -0400327
Alexandre Lisiona7ab2e32014-02-14 15:33:33 -0500328 case TransferDFragment.RESULT_TRANSFER_NUMBER:
329 String to = data.getStringExtra("to_number");
330 transfer = data.getParcelableExtra("transfer");
331 try {
332 mCallbacks.getService().transfer(transfer.getCallId(), to);
333 mCallbacks.getService().hangUp(transfer.getCallId());
334 } catch (RemoteException e) {
335 e.printStackTrace();
336 }
337 break;
338 case Activity.RESULT_CANCELED:
339 default:
340 model.clear();
341 initNormalStateDisplay();
342 break;
Alexandre Lision2b237922013-09-09 16:23:02 -0400343 }
344 }
345 }
346
347 @Override
alision1005ba12013-06-19 13:52:44 -0400348 public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
Alexandre Lision84208a32013-09-25 13:18:37 -0400349 final ViewGroup rootView = (ViewGroup) inflater.inflate(R.layout.frag_call, container, false);
alision85992112013-05-29 12:18:08 -0400350
alision1005ba12013-06-19 13:52:44 -0400351 view = (BubblesView) rootView.findViewById(R.id.main_view);
alision34673e62013-06-25 14:40:07 -0400352 view.setFragment(this);
alision1005ba12013-06-19 13:52:44 -0400353 view.setModel(model);
354 view.getHolder().addCallback(this);
alision84813a12013-05-27 17:40:39 -0400355
Alexandre Lision3c6b7102013-09-16 16:56:46 -0400356 callStatusTxt = (TextView) rootView.findViewById(R.id.call_status_txt);
Alexandre Lision68855472013-10-10 16:20:46 -0400357 call_icon = BitmapFactory.decodeResource(getResources(), R.drawable.ic_action_call);
Alexandre Lisioncb2345c2013-12-09 15:39:13 -0500358
Alexandre Lisionbaeeb212013-12-09 12:54:47 -0500359 speakers = (ToggleButton) rootView.findViewById(R.id.speaker_toggle);
Alexandre Lisioncb2345c2013-12-09 15:39:13 -0500360
Alexandre Lision35577132013-12-06 15:21:15 -0500361 speakers.setOnCheckedChangeListener(new OnCheckedChangeListener() {
Alexandre Lisioncb2345c2013-12-09 15:39:13 -0500362
Alexandre Lision35577132013-12-06 15:21:15 -0500363 @Override
364 public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
365 try {
366 mCallbacks.getService().toggleSpeakerPhone(isChecked);
367 } catch (RemoteException e) {
368 e.printStackTrace();
369 }
Alexandre Lisioncb2345c2013-12-09 15:39:13 -0500370
Alexandre Lision35577132013-12-06 15:21:15 -0500371 }
372 });
alision84813a12013-05-27 17:40:39 -0400373
Alexandre Lision5f144b82014-02-11 09:59:36 -0500374 rootView.findViewById(R.id.dialpad_btn).setOnClickListener(new OnClickListener() {
Alexandre Lision84208a32013-09-25 13:18:37 -0400375
376 @Override
377 public void onClick(View v) {
378 InputMethodManager lManager = (InputMethodManager) getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
379 lManager.toggleSoftInput(InputMethodManager.SHOW_IMPLICIT, InputMethodManager.HIDE_IMPLICIT_ONLY);
380 }
381 });
382
alision1005ba12013-06-19 13:52:44 -0400383 return rootView;
384 }
Alexandre Lisionc30e8412013-09-26 15:12:59 -0400385
Alexandre Lisiona7ab2e32014-02-14 15:33:33 -0500386 public Conference getConference() {
Alexandre Lision48b49eb2014-02-11 13:37:33 -0500387 return mCallbacks.getDisplayedConference();
388 }
389
alision1005ba12013-06-19 13:52:44 -0400390 private void initNormalStateDisplay() {
391 Log.i(TAG, "Start normal display");
alision84813a12013-05-27 17:40:39 -0400392
Alexandre Lision0c384512013-09-17 17:15:57 -0400393 mCallbacks.startTimer();
alision84813a12013-05-27 17:40:39 -0400394
Alexandre Lision48b49eb2014-02-11 13:37:33 -0500395 getBubbleForUser(getConference(), model.width / 2, model.height / 2);
alision907bde72013-06-20 14:40:37 -0400396
Alexandre Lision48b49eb2014-02-11 13:37:33 -0500397 int angle_part = 360 / getConference().getParticipants().size();
Alexandre Lision5f144b82014-02-11 09:59:36 -0500398 double dX, dY;
alision465ceba2013-07-04 09:24:30 -0400399 int radiusCalls = (int) (model.width / 2 - BUBBLE_SIZE);
Alexandre Lision48b49eb2014-02-11 13:37:33 -0500400 for (int i = 0; i < getConference().getParticipants().size(); ++i) {
alisiondf1dac92013-06-27 17:35:53 -0400401
Alexandre Lision48b49eb2014-02-11 13:37:33 -0500402 if (getConference().getParticipants().get(i) == null) {
alisiondf1dac92013-06-27 17:35:53 -0400403 continue;
404 }
alision34673e62013-06-25 14:40:07 -0400405 dX = Math.cos(Math.toRadians(angle_part * i - 90)) * radiusCalls;
406 dY = Math.sin(Math.toRadians(angle_part * i - 90)) * radiusCalls;
Alexandre Lision48b49eb2014-02-11 13:37:33 -0500407 getBubbleFor(getConference().getParticipants().get(i), (int) (model.width / 2 + dX), (int) (model.height / 2 + dY));
alision806e18e2013-06-21 15:30:17 -0400408 }
alision1005ba12013-06-19 13:52:44 -0400409
410 model.clearAttractors();
alision1005ba12013-06-19 13:52:44 -0400411 }
alision85704182013-05-29 15:23:03 -0400412
alision1005ba12013-06-19 13:52:44 -0400413 private void initIncomingCallDisplay() {
414 Log.i(TAG, "Start incoming display");
alision84813a12013-05-27 17:40:39 -0400415
Alexandre Lision23628c12013-09-24 11:17:05 -0400416 int radiusCalls = (int) (model.width / 2 - BUBBLE_SIZE);
Alexandre Lision48b49eb2014-02-11 13:37:33 -0500417 getBubbleForUser(getConference(), model.width / 2, model.height / 2 + radiusCalls);
418 getBubbleFor(getConference().getParticipants().get(0), model.width / 2, model.height / 2 - radiusCalls);
alision85704182013-05-29 15:23:03 -0400419
alision1005ba12013-06-19 13:52:44 -0400420 model.clearAttractors();
Alexandre Lision23628c12013-09-24 11:17:05 -0400421 model.addAttractor(new Attractor(new PointF(model.width / 2, model.height / 2), ATTRACTOR_SIZE, new Attractor.Callback() {
alision1005ba12013-06-19 13:52:44 -0400422 @Override
423 public boolean onBubbleSucked(Bubble b) {
Alexandre Lisiona764c682013-09-09 10:02:07 -0400424 if (!accepted) {
Alexandre Lision68855472013-10-10 16:20:46 -0400425 try {
426 mCallbacks.getService().accept(b.getCallID());
427 } catch (RemoteException e) {
428 e.printStackTrace();
429 }
Alexandre Lisiona764c682013-09-09 10:02:07 -0400430 accepted = true;
431 }
alision1005ba12013-06-19 13:52:44 -0400432 return false;
433 }
434 }, call_icon));
Alexandre Lisioncb2345c2013-12-09 15:39:13 -0500435
Alexandre Lision48b49eb2014-02-11 13:37:33 -0500436 if (getConference().getParticipants().get(0).getAccount().isAutoanswerEnabled()) {
Alexandre Lision332f4912013-11-12 13:04:32 -0500437 try {
Alexandre Lision48b49eb2014-02-11 13:37:33 -0500438 mCallbacks.getService().accept(getConference().getParticipants().get(0).getCallId());
Alexandre Lision332f4912013-11-12 13:04:32 -0500439 } catch (RemoteException e) {
440 e.printStackTrace();
441 }
442 }
alision1005ba12013-06-19 13:52:44 -0400443 }
alision85704182013-05-29 15:23:03 -0400444
alision1005ba12013-06-19 13:52:44 -0400445 private void initOutGoingCallDisplay() {
446 Log.i(TAG, "Start outgoing display");
alision85704182013-05-29 15:23:03 -0400447
Alexandre Lision48b49eb2014-02-11 13:37:33 -0500448 getBubbleForUser(getConference(), model.width / 2, model.height / 2);
alision85704182013-05-29 15:23:03 -0400449
alisiondf1dac92013-06-27 17:35:53 -0400450 // TODO off-thread image loading
Alexandre Lision48b49eb2014-02-11 13:37:33 -0500451 int angle_part = 360 / getConference().getParticipants().size();
Alexandre Lision5f144b82014-02-11 09:59:36 -0500452 double dX, dY;
Adrien Béraude78d06f2013-09-19 13:33:44 +1000453 int radiusCalls = (int) (model.width / 2 - BUBBLE_SIZE);
Alexandre Lision48b49eb2014-02-11 13:37:33 -0500454 for (int i = 0; i < getConference().getParticipants().size(); ++i) {
alisiondf1dac92013-06-27 17:35:53 -0400455 dX = Math.cos(Math.toRadians(angle_part * i - 90)) * radiusCalls;
456 dY = Math.sin(Math.toRadians(angle_part * i - 90)) * radiusCalls;
Alexandre Lision48b49eb2014-02-11 13:37:33 -0500457 getBubbleFor(getConference().getParticipants().get(i), (int) (model.width / 2 + dX), (int) (model.height / 2 + dY));
alisiondf1dac92013-06-27 17:35:53 -0400458 }
alision907bde72013-06-20 14:40:37 -0400459
alision1005ba12013-06-19 13:52:44 -0400460 model.clearAttractors();
alision1005ba12013-06-19 13:52:44 -0400461 }
alision85704182013-05-29 15:23:03 -0400462
alision1005ba12013-06-19 13:52:44 -0400463 /**
464 * 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 -0500465 *
466 * @param call The call associated to a contact
467 * @param x Initial or new x position.
468 * @param y Initial or new y position.
alision1005ba12013-06-19 13:52:44 -0400469 * @return Bubble corresponding to the contact.
470 */
alision806e18e2013-06-21 15:30:17 -0400471 private Bubble getBubbleFor(SipCall call, float x, float y) {
Alexandre Lisionee2494d2013-10-09 17:14:00 -0400472 Bubble contact_bubble = model.getBubble(call.getCallId());
alision1005ba12013-06-19 13:52:44 -0400473 if (contact_bubble != null) {
Alexandre Lisionb8add812013-10-24 11:42:42 -0400474 ((BubbleContact) contact_bubble).setCall(call);
alision1005ba12013-06-19 13:52:44 -0400475 contact_bubble.attractor.set(x, y);
476 return contact_bubble;
477 }
alision84813a12013-05-27 17:40:39 -0400478
Alexandre Lision40954dc2013-10-09 15:24:03 -0400479 contact_bubble = new BubbleContact(getActivity(), call, x, y, BUBBLE_SIZE);
480
481 model.addBubble(contact_bubble);
482 return contact_bubble;
483 }
Alexandre Lisionb8add812013-10-24 11:42:42 -0400484
Alexandre Lisionee2494d2013-10-09 17:14:00 -0400485 private Bubble getBubbleForUser(Conference conf, float x, float y) {
Alexandre Lisiond5686032013-10-29 11:09:21 -0400486 Bubble contact_bubble = model.getUser();
Alexandre Lision40954dc2013-10-09 15:24:03 -0400487 if (contact_bubble != null) {
488 contact_bubble.attractor.set(x, y);
Alexandre Lisionb8add812013-10-24 11:42:42 -0400489 ((BubbleUser) contact_bubble).setConference(conf);
Alexandre Lisioncb2345c2013-12-09 15:39:13 -0500490
Alexandre Lision40954dc2013-10-09 15:24:03 -0400491 return contact_bubble;
492 }
493
Alexandre Lisioncb2345c2013-12-09 15:39:13 -0500494 contact_bubble = new BubbleUser(getActivity(), CallContact.ContactBuilder.buildUserContact(getActivity().getContentResolver()), conf, x, y,
495 BUBBLE_SIZE * 1.3f);
alision84813a12013-05-27 17:40:39 -0400496
Alexandre Lisionc9fca9e2013-11-08 15:21:18 -0500497 try {
498 ((BubbleUser) contact_bubble).setMute(mCallbacks.getService().isCaptureMuted());
499 } catch (RemoteException e) {
500 e.printStackTrace();
Alexandre Lisioncb2345c2013-12-09 15:39:13 -0500501 } catch (NullPointerException e1) {
Alexandre Lisionc9fca9e2013-11-08 15:21:18 -0500502 e1.printStackTrace();
503 }
alision1005ba12013-06-19 13:52:44 -0400504 model.addBubble(contact_bubble);
alision1005ba12013-06-19 13:52:44 -0400505 return contact_bubble;
506 }
Adrien Béraud0c9bd8f2013-05-30 16:16:57 +1000507
alision1005ba12013-06-19 13:52:44 -0400508 public boolean draggingBubble() {
509 return view == null ? false : view.isDraggingBubble();
510 }
Adrien Béraudc9c424d2013-05-30 17:47:35 +1000511
alision1005ba12013-06-19 13:52:44 -0400512 @Override
513 public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
Adrien Béraud13cde0b2013-05-30 20:27:20 +1000514
Alexandre Lision48b49eb2014-02-11 13:37:33 -0500515 if (getConference().getParticipants().size() == 1) {
516 if (getConference().getParticipants().get(0).isIncoming() && getConference().getParticipants().get(0).isRinging()) {
alision806e18e2013-06-21 15:30:17 -0400517 initIncomingCallDisplay();
Alexandre Lisiona7ab2e32014-02-14 15:33:33 -0500518 } else if (getConference().getParticipants().get(0).isRinging()) {
519 initOutGoingCallDisplay();
520 } else if (getConference().getParticipants().get(0).isOngoing()) {
alision806e18e2013-06-21 15:30:17 -0400521 initNormalStateDisplay();
522 }
Alexandre Lision48b49eb2014-02-11 13:37:33 -0500523 } else if (getConference().getParticipants().size() > 1) {
alision1005ba12013-06-19 13:52:44 -0400524 initNormalStateDisplay();
525 }
alision1005ba12013-06-19 13:52:44 -0400526 }
Adrien Béraud13cde0b2013-05-30 20:27:20 +1000527
Alexandre Lision40954dc2013-10-09 15:24:03 -0400528 public void makeTransfer(BubbleContact contact) {
Alexandre Lision2b237922013-09-09 16:23:02 -0400529 FragmentManager fm = getFragmentManager();
Alexandre Lision0eb02032013-09-27 16:32:40 -0400530 editName = TransferDFragment.newInstance();
Alexandre Lision2b237922013-09-09 16:23:02 -0400531 Bundle b = new Bundle();
Alexandre Lision6ae652d2013-09-26 16:39:20 -0400532 try {
Alexandre Lisionb8add812013-10-24 11:42:42 -0400533 b.putParcelableArrayList("calls", (ArrayList<Conference>) mCallbacks.getService().getConcurrentCalls());
Alexandre Lision6ae652d2013-09-26 16:39:20 -0400534 b.putParcelable("call_selected", contact.associated_call);
535 editName.setArguments(b);
536 editName.setTargetFragment(this, REQUEST_TRANSFER);
537 editName.show(fm, "");
538 } catch (RemoteException e) {
539 Log.e(TAG, e.toString());
540 }
541
Alexandre Lision2b237922013-09-09 16:23:02 -0400542 }
543
alision1005ba12013-06-19 13:52:44 -0400544 @Override
545 public void surfaceCreated(SurfaceHolder holder) {
Alexandre Lisionc30e8412013-09-26 15:12:59 -0400546
alision1005ba12013-06-19 13:52:44 -0400547 }
Adrien Béraud13cde0b2013-05-30 20:27:20 +1000548
alision1005ba12013-06-19 13:52:44 -0400549 @Override
550 public void surfaceDestroyed(SurfaceHolder holder) {
Alexandre Lision84208a32013-09-25 13:18:37 -0400551 // check that soft input is hidden
552 InputMethodManager lManager = (InputMethodManager) getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
553 lManager.hideSoftInputFromWindow(view.getWindowToken(), 0);
Alexandre Lisionb8add812013-10-24 11:42:42 -0400554 if (editName != null && editName.isVisible()) {
Alexandre Lision0eb02032013-09-27 16:32:40 -0400555 editName.dismiss();
556 }
alision1005ba12013-06-19 13:52:44 -0400557 }
Adrien Béraud13cde0b2013-05-30 20:27:20 +1000558
alisione38001f2013-06-04 14:14:39 -0400559 public BubblesView getBubbleView() {
560 return view;
alision1005ba12013-06-19 13:52:44 -0400561 }
Alexandre Lision0c384512013-09-17 17:15:57 -0400562
563 public void updateTime() {
Alexandre Lisiona7ab2e32014-02-14 15:33:33 -0500564 if (getConference() != null) {
565 long duration = System.currentTimeMillis() - getConference().getParticipants().get(0).getTimestampStart_();
566 duration = duration / 1000;
567 if (getConference().isOnGoing())
568 callStatusTxt.setText(String.format("%d:%02d:%02d", duration / 3600, duration % 3600 / 60, duration % 60));
569 }
570
Alexandre Lisionf1850c02013-09-23 14:19:34 -0400571 }
572
Alexandre Lision64dc8c02013-09-25 15:32:25 -0400573 public void onKeyUp(int keyCode, KeyEvent event) {
574 try {
Alexandre Lisionb8add812013-10-24 11:42:42 -0400575
Alexandre Lisionfda4d1d2013-10-25 15:29:53 -0400576 switch (keyCode) {
Alexandre Lisiona7ab2e32014-02-14 15:33:33 -0500577 case KeyEvent.KEYCODE_VOLUME_DOWN:
578 case KeyEvent.KEYCODE_VOLUME_UP:
579 break;
580 default:
581 String toSend = Character.toString(event.getDisplayLabel());
582 toSend.toUpperCase(Locale.getDefault());
583 Log.d(TAG, "toSend " + toSend);
584 mCallbacks.getService().playDtmf(toSend);
585 break;
Alexandre Lisionfda4d1d2013-10-25 15:29:53 -0400586 }
Alexandre Lision64dc8c02013-09-25 15:32:25 -0400587 } catch (RemoteException e) {
588 e.printStackTrace();
Alexandre Lision3e1e06f2014-02-10 12:13:25 -0500589 } catch (NullPointerException e) {
590 e.printStackTrace();
Alexandre Lision64dc8c02013-09-25 15:32:25 -0400591 }
592 }
alision84813a12013-05-27 17:40:39 -0400593}