blob: 8e9c51147b86c2234268bf747cb2bda9856c1f7d [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 Lision4ecfd812014-02-21 15:53:32 -050054import android.widget.*;
Alexandre Lision35577132013-12-06 15:21:15 -050055import android.widget.CompoundButton.OnCheckedChangeListener;
Alexandre Lision5f144b82014-02-11 09:59:36 -050056import org.sflphone.R;
57import org.sflphone.interfaces.CallInterface;
58import org.sflphone.model.*;
59import org.sflphone.service.ISipService;
alision84813a12013-05-27 17:40:39 -040060
Alexandre Lision5f144b82014-02-11 09:59:36 -050061import java.util.ArrayList;
62import java.util.Locale;
63
64public class CallFragment extends CallableWrapperFragment implements CallInterface, Callback {
alision84813a12013-05-27 17:40:39 -040065
alision1005ba12013-06-19 13:52:44 -040066 static final String TAG = "CallFragment";
alision84813a12013-05-27 17:40:39 -040067
alisionf57d8a62013-07-02 09:37:12 -040068 float BUBBLE_SIZE = 75;
alision1005ba12013-06-19 13:52:44 -040069 static final float ATTRACTOR_SIZE = 40;
Alexandre Lision2b237922013-09-09 16:23:02 -040070 public static final int REQUEST_TRANSFER = 10;
71
Alexandre Lisionf02190d2013-12-12 17:26:12 -050072 // Screen wake lock for incoming call
Alexandre Lision4ecfd812014-02-21 15:53:32 -050073 private WakeLock mScreenWakeLock;
Alexandre Lisionf02190d2013-12-12 17:26:12 -050074
Alexandre Lision4ecfd812014-02-21 15:53:32 -050075 private BubblesView mBubbleView;
Alexandre Lision651b5262014-02-21 17:14:14 -050076 private BubbleModel mBubbleModel;
alision84813a12013-05-27 17:40:39 -040077
Alexandre Lision4ecfd812014-02-21 15:53:32 -050078 ViewSwitcher mSecuritySwitch;
79 private TextView mCallStatusTxt;
80 private ToggleButton mToggleSpeakers;
81
Alexandre Lision68855472013-10-10 16:20:46 -040082 public Callbacks mCallbacks = sDummyCallbacks;
Alexandre Lisiona764c682013-09-09 10:02:07 -040083 boolean accepted = false;
alision85704182013-05-29 15:23:03 -040084
Alexandre Lision0eb02032013-09-27 16:32:40 -040085 TransferDFragment editName;
Alexandre Lisiona3c4f5f2014-01-07 17:22:22 -050086 private WifiManager wifiManager;
87 private BroadcastReceiver wifiReceiver = new BroadcastReceiver() {
88
89 @Override
90 public void onReceive(Context context, Intent intent) {
91 WifiInfo info = wifiManager.getConnectionInfo();
Alexandre Lisiona7ab2e32014-02-14 15:33:33 -050092 Log.i(TAG, "Level of wifi " + info.getRssi());
Alexandre Lisiona3c4f5f2014-01-07 17:22:22 -050093 }
94
95 };
Alexandre Lisionf1850c02013-09-23 14:19:34 -040096
alision1005ba12013-06-19 13:52:44 -040097 @Override
98 public void onCreate(Bundle savedBundle) {
99 super.onCreate(savedBundle);
Alexandre Lisiona764c682013-09-09 10:02:07 -0400100 Log.e(TAG, "BUBBLE_SIZE " + BUBBLE_SIZE);
Alexandre Lision651b5262014-02-21 17:14:14 -0500101
102 mBubbleModel = new BubbleModel(getResources().getDisplayMetrics().density);
103 BUBBLE_SIZE = getResources().getDimension(R.dimen.bubble_size);
104
105 setHasOptionsMenu(true);
Alexandre Lision4ecfd812014-02-21 15:53:32 -0500106 PowerManager powerManager = (PowerManager) getActivity().getSystemService(Context.POWER_SERVICE);
107 mScreenWakeLock = powerManager.newWakeLock(PowerManager.SCREEN_DIM_WAKE_LOCK | PowerManager.ACQUIRE_CAUSES_WAKEUP | PowerManager.ON_AFTER_RELEASE,
Alexandre Lisionf02190d2013-12-12 17:26:12 -0500108 "org.sflphone.onIncomingCall");
Alexandre Lision4ecfd812014-02-21 15:53:32 -0500109 mScreenWakeLock.setReferenceCounted(false);
Alexandre Lision5f144b82014-02-11 09:59:36 -0500110
Alexandre Lisionf02190d2013-12-12 17:26:12 -0500111 Log.d(TAG, "Acquire wake up lock");
Alexandre Lision4ecfd812014-02-21 15:53:32 -0500112 if (mScreenWakeLock != null && !mScreenWakeLock.isHeld()) {
113 mScreenWakeLock.acquire();
Alexandre Lisionf02190d2013-12-12 17:26:12 -0500114 }
Alexandre Lisiona3c4f5f2014-01-07 17:22:22 -0500115 }
Alexandre Lisionf02190d2013-12-12 17:26:12 -0500116
Alexandre Lisiona7ab2e32014-02-14 15:33:33 -0500117 private void initializeWiFiListener() {
Alexandre Lisiona3c4f5f2014-01-07 17:22:22 -0500118 Log.i(TAG, "executing initializeWiFiListener");
119
120 String connectivity_context = Context.WIFI_SERVICE;
121 wifiManager = (WifiManager) getActivity().getSystemService(connectivity_context);
122
Alexandre Lisiona7ab2e32014-02-14 15:33:33 -0500123 if (!wifiManager.isWifiEnabled()) {
124 if (wifiManager.getWifiState() != WifiManager.WIFI_STATE_ENABLING) {
Alexandre Lisiona3c4f5f2014-01-07 17:22:22 -0500125 wifiManager.setWifiEnabled(true);
126 }
127 }
128
129 getActivity().registerReceiver(wifiReceiver, new IntentFilter(WifiManager.RSSI_CHANGED_ACTION));
alision1005ba12013-06-19 13:52:44 -0400130 }
alision85992112013-05-29 12:18:08 -0400131
alision1005ba12013-06-19 13:52:44 -0400132 /**
133 * A dummy implementation of the {@link Callbacks} interface that does nothing. Used only when this fragment is not attached to an activity.
134 */
135 private static Callbacks sDummyCallbacks = new Callbacks() {
alision85704182013-05-29 15:23:03 -0400136
alision1005ba12013-06-19 13:52:44 -0400137 @Override
138 public ISipService getService() {
139 return null;
140 }
alisiondf1dac92013-06-27 17:35:53 -0400141
Alexandre Lisionccf0c932013-10-10 16:24:41 -0400142 @Override
Alexandre Lisiondafe9512013-10-28 16:31:41 -0400143 public void terminateCall() {
Alexandre Lisionccf0c932013-10-10 16:24:41 -0400144 }
Alexandre Lision0c384512013-09-17 17:15:57 -0400145
146 @Override
Alexandre Lision48b49eb2014-02-11 13:37:33 -0500147 public Conference getDisplayedConference() {
148 return null;
149 }
150
151 @Override
Alexandre Lisiona7ab2e32014-02-14 15:33:33 -0500152 public void updateDisplayedConference(Conference c) {
153 }
154
155 @Override
Adrien Béraude78d06f2013-09-19 13:33:44 +1000156 public void startTimer() {
Alexandre Lision0c384512013-09-17 17:15:57 -0400157 }
Alexandre Lision68855472013-10-10 16:20:46 -0400158
159 @Override
Alexandre Lisionb8add812013-10-24 11:42:42 -0400160 public void slideChatScreen() {
Alexandre Lision68855472013-10-10 16:20:46 -0400161 }
Alexandre Lisiona7ab2e32014-02-14 15:33:33 -0500162
alision1005ba12013-06-19 13:52:44 -0400163 };
alision907bde72013-06-20 14:40:37 -0400164
alision1005ba12013-06-19 13:52:44 -0400165 /**
166 * The Activity calling this fragment has to implement this interface
alision1005ba12013-06-19 13:52:44 -0400167 */
168 public interface Callbacks {
alision85992112013-05-29 12:18:08 -0400169
alision1005ba12013-06-19 13:52:44 -0400170 public ISipService getService();
alision85704182013-05-29 15:23:03 -0400171
Alexandre Lision0c384512013-09-17 17:15:57 -0400172 public void startTimer();
Alexandre Lision68855472013-10-10 16:20:46 -0400173
174 public void slideChatScreen();
Alexandre Lisionb8add812013-10-24 11:42:42 -0400175
Alexandre Lisiondafe9512013-10-28 16:31:41 -0400176 public void terminateCall();
Alexandre Lision48b49eb2014-02-11 13:37:33 -0500177
178 public Conference getDisplayedConference();
Alexandre Lisiona7ab2e32014-02-14 15:33:33 -0500179
180 public void updateDisplayedConference(Conference c);
alision1005ba12013-06-19 13:52:44 -0400181 }
alision85992112013-05-29 12:18:08 -0400182
alision1005ba12013-06-19 13:52:44 -0400183 @Override
184 public void onAttach(Activity activity) {
185 super.onAttach(activity);
alision85992112013-05-29 12:18:08 -0400186
alision1005ba12013-06-19 13:52:44 -0400187 if (!(activity instanceof Callbacks)) {
188 throw new IllegalStateException("Activity must implement fragment's callbacks.");
189 }
alision85992112013-05-29 12:18:08 -0400190
alision1005ba12013-06-19 13:52:44 -0400191 // rootView.requestDisallowInterceptTouchEvent(true);
alision85992112013-05-29 12:18:08 -0400192
alision1005ba12013-06-19 13:52:44 -0400193 mCallbacks = (Callbacks) activity;
Alexandre Lisionb8add812013-10-24 11:42:42 -0400194 // myself = SipCall.SipCallBuilder.buildMyselfCall(activity.getContentResolver(), "Me");
Adrien Béraude78d06f2013-09-19 13:33:44 +1000195
alision1005ba12013-06-19 13:52:44 -0400196 }
Alexandre Lisionb8add812013-10-24 11:42:42 -0400197
Alexandre Lisiond588bff2013-10-08 12:43:01 -0400198 @Override
199 public void onCreateOptionsMenu(Menu m, MenuInflater inf) {
200 super.onCreateOptionsMenu(m, inf);
Alexandre Lision68855472013-10-10 16:20:46 -0400201 inf.inflate(R.menu.ac_call, m);
Alexandre Lisiond588bff2013-10-08 12:43:01 -0400202 }
203
204 @Override
205 public boolean onOptionsItemSelected(MenuItem item) {
206 super.onOptionsItemSelected(item);
Alexandre Lision666b3772013-10-28 17:42:48 -0400207 switch (item.getItemId()) {
Alexandre Lisiona7ab2e32014-02-14 15:33:33 -0500208 case R.id.menuitem_chat:
209 mCallbacks.slideChatScreen();
210 break;
Alexandre Lision666b3772013-10-28 17:42:48 -0400211 }
Alexandre Lisiond588bff2013-10-08 12:43:01 -0400212
213 return true;
214 }
alision85992112013-05-29 12:18:08 -0400215
alision1005ba12013-06-19 13:52:44 -0400216 @Override
217 public void onDetach() {
218 super.onDetach();
219 mCallbacks = sDummyCallbacks;
alision1005ba12013-06-19 13:52:44 -0400220 }
alision907bde72013-06-20 14:40:37 -0400221
alision1005ba12013-06-19 13:52:44 -0400222 @Override
alision907bde72013-06-20 14:40:37 -0400223 public void onStop() {
alision1005ba12013-06-19 13:52:44 -0400224 super.onStop();
225 }
alision85992112013-05-29 12:18:08 -0400226
alision1005ba12013-06-19 13:52:44 -0400227 @Override
Alexandre Lisionf1850c02013-09-23 14:19:34 -0400228 public void onResume() {
229 super.onResume();
Alexandre Lisiona3c4f5f2014-01-07 17:22:22 -0500230 initializeWiFiListener();
Alexandre Lisionf1850c02013-09-23 14:19:34 -0400231 }
232
233 @Override
234 public void onPause() {
235 super.onPause();
Alexandre Lisiona3c4f5f2014-01-07 17:22:22 -0500236 getActivity().unregisterReceiver(wifiReceiver);
Alexandre Lision4ecfd812014-02-21 15:53:32 -0500237 if (mScreenWakeLock != null && mScreenWakeLock.isHeld()) {
238 mScreenWakeLock.release();
Alexandre Lisionf02190d2013-12-12 17:26:12 -0500239 }
Alexandre Lisionf1850c02013-09-23 14:19:34 -0400240 }
241
242 @Override
Alexandre Lisiona7ab2e32014-02-14 15:33:33 -0500243 public void callStateChanged(Conference updated, String callID, String newState) {
244 mCallbacks.updateDisplayedConference(updated);
245 Log.i(TAG, "Call :" + callID + " " + newState);
246
247 if (getConference().isOnGoing()) {
248 initNormalStateDisplay();
249 } else if (getConference().isRinging()) {
Alexandre Lision4ecfd812014-02-21 15:53:32 -0500250 mCallStatusTxt.setText(newState);
Alexandre Lisiona7ab2e32014-02-14 15:33:33 -0500251
252 if (getConference().isIncoming()) {
253 initIncomingCallDisplay();
254 } else
255 initOutGoingCallDisplay();
256 } else {
Alexandre Lision4ecfd812014-02-21 15:53:32 -0500257 mCallStatusTxt.setText(newState);
Alexandre Lisiona7ab2e32014-02-14 15:33:33 -0500258 mCallbacks.terminateCall();
259 }
Alexandre Lision5f144b82014-02-11 09:59:36 -0500260 }
261
262 @Override
Alexandre Lisiona7ab2e32014-02-14 15:33:33 -0500263 public void secureZrtpOn(Conference updated, String id) {
Alexandre Lision5f144b82014-02-11 09:59:36 -0500264 Log.i(TAG, "secureZrtpOn");
Alexandre Lisiona7ab2e32014-02-14 15:33:33 -0500265 mCallbacks.updateDisplayedConference(updated);
Alexandre Lisione1b7f162014-02-26 16:45:32 -0500266 updateSecurityDisplay();
Alexandre Lision5f144b82014-02-11 09:59:36 -0500267 }
268
269 @Override
Alexandre Lisiona7ab2e32014-02-14 15:33:33 -0500270 public void secureZrtpOff(Conference updated, String id) {
Alexandre Lision5f144b82014-02-11 09:59:36 -0500271 Log.i(TAG, "secureZrtpOff");
Alexandre Lisiona7ab2e32014-02-14 15:33:33 -0500272 mCallbacks.updateDisplayedConference(updated);
Alexandre Lisione1b7f162014-02-26 16:45:32 -0500273 updateSecurityDisplay();
Alexandre Lision5f144b82014-02-11 09:59:36 -0500274 }
275
276 @Override
Alexandre Lisiona7ab2e32014-02-14 15:33:33 -0500277 public void displaySAS(Conference updated, final String securedCallID) {
Alexandre Lision5f144b82014-02-11 09:59:36 -0500278 Log.i(TAG, "displaySAS");
Alexandre Lisiona7ab2e32014-02-14 15:33:33 -0500279 mCallbacks.updateDisplayedConference(updated);
Alexandre Lisione1b7f162014-02-26 16:45:32 -0500280 updateSecurityDisplay();
Alexandre Lision5f144b82014-02-11 09:59:36 -0500281 }
282
283 @Override
Alexandre Lision651b5262014-02-21 17:14:14 -0500284 public void zrtpNegotiationFailed(Conference c, String securedCallID) {
285 mCallbacks.updateDisplayedConference(c);
Alexandre Lisione1b7f162014-02-26 16:45:32 -0500286 updateSecurityDisplay();
Alexandre Lision651b5262014-02-21 17:14:14 -0500287 }
288
289 @Override
290 public void zrtpNotSupported(Conference c, String securedCallID) {
291 mCallbacks.updateDisplayedConference(c);
Alexandre Lisione1b7f162014-02-26 16:45:32 -0500292 updateSecurityDisplay();
Alexandre Lision651b5262014-02-21 17:14:14 -0500293 }
294
295 @Override
Alexandre Lision2b237922013-09-09 16:23:02 -0400296 public void onActivityResult(int requestCode, int resultCode, Intent data) {
297 super.onActivityResult(requestCode, resultCode, data);
Alexandre Lision5f144b82014-02-11 09:59:36 -0500298 SipCall transfer;
Alexandre Lision2b237922013-09-09 16:23:02 -0400299 if (requestCode == REQUEST_TRANSFER) {
300 switch (resultCode) {
Alexandre Lisiona7ab2e32014-02-14 15:33:33 -0500301 case TransferDFragment.RESULT_TRANSFER_CONF:
302 Conference c = data.getParcelableExtra("target");
303 transfer = data.getParcelableExtra("transfer");
304 try {
Alexandre Lision2b237922013-09-09 16:23:02 -0400305
Alexandre Lisiona7ab2e32014-02-14 15:33:33 -0500306 mCallbacks.getService().attendedTransfer(transfer.getCallId(), c.getParticipants().get(0).getCallId());
Alexandre Lision2b237922013-09-09 16:23:02 -0400307
Alexandre Lisiona7ab2e32014-02-14 15:33:33 -0500308 } catch (RemoteException e) {
309 e.printStackTrace();
310 }
311 break;
Alexandre Lision2b237922013-09-09 16:23:02 -0400312
Alexandre Lisiona7ab2e32014-02-14 15:33:33 -0500313 case TransferDFragment.RESULT_TRANSFER_NUMBER:
314 String to = data.getStringExtra("to_number");
315 transfer = data.getParcelableExtra("transfer");
316 try {
317 mCallbacks.getService().transfer(transfer.getCallId(), to);
318 mCallbacks.getService().hangUp(transfer.getCallId());
319 } catch (RemoteException e) {
320 e.printStackTrace();
321 }
322 break;
323 case Activity.RESULT_CANCELED:
324 default:
Alexandre Lision651b5262014-02-21 17:14:14 -0500325 mBubbleModel.clear();
Alexandre Lisiona7ab2e32014-02-14 15:33:33 -0500326 initNormalStateDisplay();
327 break;
Alexandre Lision2b237922013-09-09 16:23:02 -0400328 }
329 }
330 }
331
332 @Override
alision1005ba12013-06-19 13:52:44 -0400333 public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
Alexandre Lision84208a32013-09-25 13:18:37 -0400334 final ViewGroup rootView = (ViewGroup) inflater.inflate(R.layout.frag_call, container, false);
alision85992112013-05-29 12:18:08 -0400335
Alexandre Lision4ecfd812014-02-21 15:53:32 -0500336 mBubbleView = (BubblesView) rootView.findViewById(R.id.main_view);
337 mBubbleView.setFragment(this);
Alexandre Lision651b5262014-02-21 17:14:14 -0500338 mBubbleView.setModel(mBubbleModel);
Alexandre Lision4ecfd812014-02-21 15:53:32 -0500339 mBubbleView.getHolder().addCallback(this);
alision84813a12013-05-27 17:40:39 -0400340
Alexandre Lision4ecfd812014-02-21 15:53:32 -0500341 mCallStatusTxt = (TextView) rootView.findViewById(R.id.call_status_txt);
Alexandre Lisioncb2345c2013-12-09 15:39:13 -0500342
Alexandre Lision651b5262014-02-21 17:14:14 -0500343 mSecuritySwitch = (ViewSwitcher) rootView.findViewById(R.id.security_switcher);
Alexandre Lision4ecfd812014-02-21 15:53:32 -0500344 mToggleSpeakers = (ToggleButton) rootView.findViewById(R.id.speaker_toggle);
345
346 mToggleSpeakers.setOnCheckedChangeListener(new OnCheckedChangeListener() {
Alexandre Lisioncb2345c2013-12-09 15:39:13 -0500347
Alexandre Lision35577132013-12-06 15:21:15 -0500348 @Override
349 public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
350 try {
351 mCallbacks.getService().toggleSpeakerPhone(isChecked);
352 } catch (RemoteException e) {
353 e.printStackTrace();
354 }
Alexandre Lisioncb2345c2013-12-09 15:39:13 -0500355
Alexandre Lision35577132013-12-06 15:21:15 -0500356 }
357 });
alision84813a12013-05-27 17:40:39 -0400358
Alexandre Lision5f144b82014-02-11 09:59:36 -0500359 rootView.findViewById(R.id.dialpad_btn).setOnClickListener(new OnClickListener() {
Alexandre Lision84208a32013-09-25 13:18:37 -0400360
361 @Override
362 public void onClick(View v) {
363 InputMethodManager lManager = (InputMethodManager) getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
364 lManager.toggleSoftInput(InputMethodManager.SHOW_IMPLICIT, InputMethodManager.HIDE_IMPLICIT_ONLY);
365 }
366 });
367
alision1005ba12013-06-19 13:52:44 -0400368 return rootView;
369 }
Alexandre Lisionc30e8412013-09-26 15:12:59 -0400370
Alexandre Lisiona7ab2e32014-02-14 15:33:33 -0500371 public Conference getConference() {
Alexandre Lision48b49eb2014-02-11 13:37:33 -0500372 return mCallbacks.getDisplayedConference();
373 }
374
alision1005ba12013-06-19 13:52:44 -0400375 private void initNormalStateDisplay() {
376 Log.i(TAG, "Start normal display");
alision84813a12013-05-27 17:40:39 -0400377
Alexandre Lision0c384512013-09-17 17:15:57 -0400378 mCallbacks.startTimer();
alision84813a12013-05-27 17:40:39 -0400379
Alexandre Lision651b5262014-02-21 17:14:14 -0500380 getBubbleForUser(getConference(), mBubbleModel.width / 2, mBubbleModel.height / 2);
alision907bde72013-06-20 14:40:37 -0400381
Alexandre Lision48b49eb2014-02-11 13:37:33 -0500382 int angle_part = 360 / getConference().getParticipants().size();
Alexandre Lision5f144b82014-02-11 09:59:36 -0500383 double dX, dY;
Alexandre Lision651b5262014-02-21 17:14:14 -0500384 int radiusCalls = (int) (mBubbleModel.width / 2 - BUBBLE_SIZE);
Alexandre Lision48b49eb2014-02-11 13:37:33 -0500385 for (int i = 0; i < getConference().getParticipants().size(); ++i) {
alisiondf1dac92013-06-27 17:35:53 -0400386
Alexandre Lision3c37dca2014-02-21 14:13:26 -0500387 SipCall partee = getConference().getParticipants().get(i);
388 if (partee == null) {
alisiondf1dac92013-06-27 17:35:53 -0400389 continue;
390 }
alision34673e62013-06-25 14:40:07 -0400391 dX = Math.cos(Math.toRadians(angle_part * i - 90)) * radiusCalls;
392 dY = Math.sin(Math.toRadians(angle_part * i - 90)) * radiusCalls;
Alexandre Lision651b5262014-02-21 17:14:14 -0500393 getBubbleFor(partee, (int) (mBubbleModel.width / 2 + dX), (int) (mBubbleModel.height / 2 + dY));
alision806e18e2013-06-21 15:30:17 -0400394 }
Alexandre Lisione1b7f162014-02-26 16:45:32 -0500395 updateSecurityDisplay();
Alexandre Lision651b5262014-02-21 17:14:14 -0500396 mBubbleModel.clearAttractors();
alision1005ba12013-06-19 13:52:44 -0400397 }
alision85704182013-05-29 15:23:03 -0400398
Alexandre Lisione1b7f162014-02-26 16:45:32 -0500399 private void updateSecurityDisplay() {
400
401 //First we check if at least one participan use a security layer.
402 if (!getConference().useSecureLayer())
403 return;
404
405 Log.i(TAG, "Enable security display");
406 if (getConference().hasMultipleParticipants()) {
407 //TODO What layout should we put?
408 } else {
409 final SecureSipCall secured = (SecureSipCall) getConference().getParticipants().get(0);
410 switch (secured.displayModule()) {
411 case SecureSipCall.DISPLAY_GREEN_LOCK:
412 Log.i(TAG, "DISPLAY_GREEN_LOCK");
413 showLock(R.drawable.green_lock);
414 break;
415 case SecureSipCall.DISPLAY_RED_LOCK:
416 Log.i(TAG, "DISPLAY_RED_LOCK");
417 showLock(R.drawable.red_lock);
418 break;
419 case SecureSipCall.DISPLAY_CONFIRM_SAS:
420 final Button sas = (Button) mSecuritySwitch.findViewById(R.id.confirm_sas);
421 Log.i(TAG, "Confirm SAS: " + secured.getSAS());
422 sas.setText("Confirm SAS: " + secured.getSAS());
423 sas.setOnClickListener(new View.OnClickListener() {
424 @Override
425 public void onClick(View v) {
426 try {
427 mCallbacks.getService().confirmSAS(secured.getCallId());
428 showLock(R.drawable.green_lock);
429 } catch (RemoteException e) {
430 e.printStackTrace();
431 }
Alexandre Lision651b5262014-02-21 17:14:14 -0500432 }
Alexandre Lisione1b7f162014-02-26 16:45:32 -0500433 });
434 mSecuritySwitch.setDisplayedChild(0);
435 mSecuritySwitch.setVisibility(View.VISIBLE);
436 break;
437 case SecureSipCall.DISPLAY_NONE:
438 break;
439 }
Alexandre Lisiona0e45822014-02-14 17:36:11 -0500440 }
441 }
442
Alexandre Lisiondbe396d2014-02-24 16:53:25 -0500443 private void showLock(int resId) {
444 ImageView lock = (ImageView) mSecuritySwitch.findViewById(R.id.lock_image);
445 lock.setImageDrawable(getResources().getDrawable(resId));
Alexandre Lisione1b7f162014-02-26 16:45:32 -0500446 mSecuritySwitch.setDisplayedChild(1);
Alexandre Lisiondbe396d2014-02-24 16:53:25 -0500447 mSecuritySwitch.setVisibility(View.VISIBLE);
448 }
449
alision1005ba12013-06-19 13:52:44 -0400450 private void initIncomingCallDisplay() {
451 Log.i(TAG, "Start incoming display");
alision84813a12013-05-27 17:40:39 -0400452
Alexandre Lision651b5262014-02-21 17:14:14 -0500453 int radiusCalls = (int) (mBubbleModel.width / 2 - BUBBLE_SIZE);
454 getBubbleForUser(getConference(), mBubbleModel.width / 2, mBubbleModel.height / 2 + radiusCalls);
455 getBubbleFor(getConference().getParticipants().get(0), mBubbleModel.width / 2, mBubbleModel.height / 2 - radiusCalls);
Alexandre Lision4ecfd812014-02-21 15:53:32 -0500456 Bitmap call_icon = BitmapFactory.decodeResource(getResources(), R.drawable.ic_action_call);
Alexandre Lision651b5262014-02-21 17:14:14 -0500457 mBubbleModel.clearAttractors();
458 mBubbleModel.addAttractor(new Attractor(new PointF(mBubbleModel.width / 2, mBubbleModel.height / 2), ATTRACTOR_SIZE, new Attractor.Callback() {
alision1005ba12013-06-19 13:52:44 -0400459 @Override
460 public boolean onBubbleSucked(Bubble b) {
Alexandre Lisiona764c682013-09-09 10:02:07 -0400461 if (!accepted) {
Alexandre Lision68855472013-10-10 16:20:46 -0400462 try {
463 mCallbacks.getService().accept(b.getCallID());
464 } catch (RemoteException e) {
465 e.printStackTrace();
466 }
Alexandre Lisiona764c682013-09-09 10:02:07 -0400467 accepted = true;
468 }
alision1005ba12013-06-19 13:52:44 -0400469 return false;
470 }
471 }, call_icon));
Alexandre Lisioncb2345c2013-12-09 15:39:13 -0500472
Alexandre Lision48b49eb2014-02-11 13:37:33 -0500473 if (getConference().getParticipants().get(0).getAccount().isAutoanswerEnabled()) {
Alexandre Lision332f4912013-11-12 13:04:32 -0500474 try {
Alexandre Lision48b49eb2014-02-11 13:37:33 -0500475 mCallbacks.getService().accept(getConference().getParticipants().get(0).getCallId());
Alexandre Lision332f4912013-11-12 13:04:32 -0500476 } catch (RemoteException e) {
477 e.printStackTrace();
478 }
479 }
alision1005ba12013-06-19 13:52:44 -0400480 }
alision85704182013-05-29 15:23:03 -0400481
alision1005ba12013-06-19 13:52:44 -0400482 private void initOutGoingCallDisplay() {
483 Log.i(TAG, "Start outgoing display");
Alexandre Lision651b5262014-02-21 17:14:14 -0500484 getBubbleForUser(getConference(), mBubbleModel.width / 2, mBubbleModel.height / 2);
alision85704182013-05-29 15:23:03 -0400485
Alexandre Lision48b49eb2014-02-11 13:37:33 -0500486 int angle_part = 360 / getConference().getParticipants().size();
Alexandre Lision5f144b82014-02-11 09:59:36 -0500487 double dX, dY;
Alexandre Lision651b5262014-02-21 17:14:14 -0500488 int radiusCalls = (int) (mBubbleModel.width / 2 - BUBBLE_SIZE);
Alexandre Lision48b49eb2014-02-11 13:37:33 -0500489 for (int i = 0; i < getConference().getParticipants().size(); ++i) {
alisiondf1dac92013-06-27 17:35:53 -0400490 dX = Math.cos(Math.toRadians(angle_part * i - 90)) * radiusCalls;
491 dY = Math.sin(Math.toRadians(angle_part * i - 90)) * radiusCalls;
Alexandre Lision651b5262014-02-21 17:14:14 -0500492 getBubbleFor(getConference().getParticipants().get(i), (int) (mBubbleModel.width / 2 + dX), (int) (mBubbleModel.height / 2 + dY));
alisiondf1dac92013-06-27 17:35:53 -0400493 }
Alexandre Lision651b5262014-02-21 17:14:14 -0500494 mBubbleModel.clearAttractors();
alision1005ba12013-06-19 13:52:44 -0400495 }
alision85704182013-05-29 15:23:03 -0400496
alision1005ba12013-06-19 13:52:44 -0400497 /**
498 * 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 -0500499 *
500 * @param call The call associated to a contact
501 * @param x Initial or new x position.
502 * @param y Initial or new y position.
alision1005ba12013-06-19 13:52:44 -0400503 * @return Bubble corresponding to the contact.
504 */
alision806e18e2013-06-21 15:30:17 -0400505 private Bubble getBubbleFor(SipCall call, float x, float y) {
Alexandre Lision651b5262014-02-21 17:14:14 -0500506 Bubble contact_bubble = mBubbleModel.getBubble(call.getCallId());
alision1005ba12013-06-19 13:52:44 -0400507 if (contact_bubble != null) {
Alexandre Lisionb8add812013-10-24 11:42:42 -0400508 ((BubbleContact) contact_bubble).setCall(call);
alision1005ba12013-06-19 13:52:44 -0400509 contact_bubble.attractor.set(x, y);
510 return contact_bubble;
511 }
alision84813a12013-05-27 17:40:39 -0400512
Alexandre Lision40954dc2013-10-09 15:24:03 -0400513 contact_bubble = new BubbleContact(getActivity(), call, x, y, BUBBLE_SIZE);
514
Alexandre Lision651b5262014-02-21 17:14:14 -0500515 mBubbleModel.addBubble(contact_bubble);
Alexandre Lision40954dc2013-10-09 15:24:03 -0400516 return contact_bubble;
517 }
Alexandre Lisionb8add812013-10-24 11:42:42 -0400518
Alexandre Lisionee2494d2013-10-09 17:14:00 -0400519 private Bubble getBubbleForUser(Conference conf, float x, float y) {
Alexandre Lision651b5262014-02-21 17:14:14 -0500520 Bubble contact_bubble = mBubbleModel.getUser();
Alexandre Lision40954dc2013-10-09 15:24:03 -0400521 if (contact_bubble != null) {
522 contact_bubble.attractor.set(x, y);
Alexandre Lisionb8add812013-10-24 11:42:42 -0400523 ((BubbleUser) contact_bubble).setConference(conf);
Alexandre Lisioncb2345c2013-12-09 15:39:13 -0500524
Alexandre Lision40954dc2013-10-09 15:24:03 -0400525 return contact_bubble;
526 }
527
Alexandre Lisioncb2345c2013-12-09 15:39:13 -0500528 contact_bubble = new BubbleUser(getActivity(), CallContact.ContactBuilder.buildUserContact(getActivity().getContentResolver()), conf, x, y,
529 BUBBLE_SIZE * 1.3f);
alision84813a12013-05-27 17:40:39 -0400530
Alexandre Lisionc9fca9e2013-11-08 15:21:18 -0500531 try {
532 ((BubbleUser) contact_bubble).setMute(mCallbacks.getService().isCaptureMuted());
533 } catch (RemoteException e) {
534 e.printStackTrace();
Alexandre Lisioncb2345c2013-12-09 15:39:13 -0500535 } catch (NullPointerException e1) {
Alexandre Lisionc9fca9e2013-11-08 15:21:18 -0500536 e1.printStackTrace();
537 }
Alexandre Lision651b5262014-02-21 17:14:14 -0500538 mBubbleModel.addBubble(contact_bubble);
alision1005ba12013-06-19 13:52:44 -0400539 return contact_bubble;
540 }
Adrien Béraud0c9bd8f2013-05-30 16:16:57 +1000541
alision1005ba12013-06-19 13:52:44 -0400542 public boolean draggingBubble() {
Alexandre Lision4ecfd812014-02-21 15:53:32 -0500543 return mBubbleView == null ? false : mBubbleView.isDraggingBubble();
alision1005ba12013-06-19 13:52:44 -0400544 }
Adrien Béraudc9c424d2013-05-30 17:47:35 +1000545
alision1005ba12013-06-19 13:52:44 -0400546 @Override
547 public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
Adrien Béraud13cde0b2013-05-30 20:27:20 +1000548
Alexandre Lision48b49eb2014-02-11 13:37:33 -0500549 if (getConference().getParticipants().size() == 1) {
550 if (getConference().getParticipants().get(0).isIncoming() && getConference().getParticipants().get(0).isRinging()) {
alision806e18e2013-06-21 15:30:17 -0400551 initIncomingCallDisplay();
Alexandre Lisiona7ab2e32014-02-14 15:33:33 -0500552 } else if (getConference().getParticipants().get(0).isRinging()) {
553 initOutGoingCallDisplay();
554 } else if (getConference().getParticipants().get(0).isOngoing()) {
alision806e18e2013-06-21 15:30:17 -0400555 initNormalStateDisplay();
556 }
Alexandre Lision48b49eb2014-02-11 13:37:33 -0500557 } else if (getConference().getParticipants().size() > 1) {
alision1005ba12013-06-19 13:52:44 -0400558 initNormalStateDisplay();
559 }
alision1005ba12013-06-19 13:52:44 -0400560 }
Adrien Béraud13cde0b2013-05-30 20:27:20 +1000561
Alexandre Lision40954dc2013-10-09 15:24:03 -0400562 public void makeTransfer(BubbleContact contact) {
Alexandre Lision2b237922013-09-09 16:23:02 -0400563 FragmentManager fm = getFragmentManager();
Alexandre Lision0eb02032013-09-27 16:32:40 -0400564 editName = TransferDFragment.newInstance();
Alexandre Lision2b237922013-09-09 16:23:02 -0400565 Bundle b = new Bundle();
Alexandre Lision6ae652d2013-09-26 16:39:20 -0400566 try {
Alexandre Lisionb8add812013-10-24 11:42:42 -0400567 b.putParcelableArrayList("calls", (ArrayList<Conference>) mCallbacks.getService().getConcurrentCalls());
Alexandre Lision6ae652d2013-09-26 16:39:20 -0400568 b.putParcelable("call_selected", contact.associated_call);
569 editName.setArguments(b);
570 editName.setTargetFragment(this, REQUEST_TRANSFER);
571 editName.show(fm, "");
572 } catch (RemoteException e) {
573 Log.e(TAG, e.toString());
574 }
575
Alexandre Lision2b237922013-09-09 16:23:02 -0400576 }
577
alision1005ba12013-06-19 13:52:44 -0400578 @Override
579 public void surfaceCreated(SurfaceHolder holder) {
Alexandre Lisionc30e8412013-09-26 15:12:59 -0400580
alision1005ba12013-06-19 13:52:44 -0400581 }
Adrien Béraud13cde0b2013-05-30 20:27:20 +1000582
alision1005ba12013-06-19 13:52:44 -0400583 @Override
584 public void surfaceDestroyed(SurfaceHolder holder) {
Alexandre Lision84208a32013-09-25 13:18:37 -0400585 // check that soft input is hidden
586 InputMethodManager lManager = (InputMethodManager) getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
Alexandre Lision4ecfd812014-02-21 15:53:32 -0500587 lManager.hideSoftInputFromWindow(mBubbleView.getWindowToken(), 0);
Alexandre Lisionb8add812013-10-24 11:42:42 -0400588 if (editName != null && editName.isVisible()) {
Alexandre Lision0eb02032013-09-27 16:32:40 -0400589 editName.dismiss();
590 }
alision1005ba12013-06-19 13:52:44 -0400591 }
Adrien Béraud13cde0b2013-05-30 20:27:20 +1000592
alisione38001f2013-06-04 14:14:39 -0400593 public BubblesView getBubbleView() {
Alexandre Lision4ecfd812014-02-21 15:53:32 -0500594 return mBubbleView;
alision1005ba12013-06-19 13:52:44 -0400595 }
Alexandre Lision0c384512013-09-17 17:15:57 -0400596
597 public void updateTime() {
Alexandre Lisiona7ab2e32014-02-14 15:33:33 -0500598 if (getConference() != null) {
599 long duration = System.currentTimeMillis() - getConference().getParticipants().get(0).getTimestampStart_();
600 duration = duration / 1000;
601 if (getConference().isOnGoing())
Alexandre Lision4ecfd812014-02-21 15:53:32 -0500602 mCallStatusTxt.setText(String.format("%d:%02d:%02d", duration / 3600, duration % 3600 / 60, duration % 60));
Alexandre Lisiona7ab2e32014-02-14 15:33:33 -0500603 }
604
Alexandre Lisionf1850c02013-09-23 14:19:34 -0400605 }
606
Alexandre Lision64dc8c02013-09-25 15:32:25 -0400607 public void onKeyUp(int keyCode, KeyEvent event) {
608 try {
Alexandre Lisionb8add812013-10-24 11:42:42 -0400609
Alexandre Lisionfda4d1d2013-10-25 15:29:53 -0400610 switch (keyCode) {
Alexandre Lisiona7ab2e32014-02-14 15:33:33 -0500611 case KeyEvent.KEYCODE_VOLUME_DOWN:
612 case KeyEvent.KEYCODE_VOLUME_UP:
613 break;
614 default:
615 String toSend = Character.toString(event.getDisplayLabel());
616 toSend.toUpperCase(Locale.getDefault());
617 Log.d(TAG, "toSend " + toSend);
618 mCallbacks.getService().playDtmf(toSend);
619 break;
Alexandre Lisionfda4d1d2013-10-25 15:29:53 -0400620 }
Alexandre Lision64dc8c02013-09-25 15:32:25 -0400621 } catch (RemoteException e) {
622 e.printStackTrace();
Alexandre Lision3e1e06f2014-02-10 12:13:25 -0500623 } catch (NullPointerException e) {
624 e.printStackTrace();
Alexandre Lision64dc8c02013-09-25 15:32:25 -0400625 }
626 }
alision84813a12013-05-27 17:40:39 -0400627}