blob: 514abf907ddc4a54a35d2fca45c91ca196e670d4 [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;
alision1005ba12013-06-19 13:52:44 -040076 private BubbleModel model;
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);
alisiondf1dac92013-06-27 17:35:53 -0400100 model = new BubbleModel(getResources().getDisplayMetrics().density);
alision729b0a22013-07-02 11:57:33 -0400101 BUBBLE_SIZE = getResources().getDimension(R.dimen.bubble_size);
Alexandre Lisiona764c682013-09-09 10:02:07 -0400102 Log.e(TAG, "BUBBLE_SIZE " + BUBBLE_SIZE);
Alexandre Lisiond588bff2013-10-08 12:43:01 -0400103 this.setHasOptionsMenu(true);
Alexandre Lision4ecfd812014-02-21 15:53:32 -0500104 PowerManager powerManager = (PowerManager) getActivity().getSystemService(Context.POWER_SERVICE);
105 mScreenWakeLock = powerManager.newWakeLock(PowerManager.SCREEN_DIM_WAKE_LOCK | PowerManager.ACQUIRE_CAUSES_WAKEUP | PowerManager.ON_AFTER_RELEASE,
Alexandre Lisionf02190d2013-12-12 17:26:12 -0500106 "org.sflphone.onIncomingCall");
Alexandre Lision4ecfd812014-02-21 15:53:32 -0500107 mScreenWakeLock.setReferenceCounted(false);
Alexandre Lision5f144b82014-02-11 09:59:36 -0500108
Alexandre Lisionf02190d2013-12-12 17:26:12 -0500109 Log.d(TAG, "Acquire wake up lock");
Alexandre Lision4ecfd812014-02-21 15:53:32 -0500110 if (mScreenWakeLock != null && !mScreenWakeLock.isHeld()) {
111 mScreenWakeLock.acquire();
Alexandre Lisionf02190d2013-12-12 17:26:12 -0500112 }
Alexandre Lisiona3c4f5f2014-01-07 17:22:22 -0500113 }
Alexandre Lisionf02190d2013-12-12 17:26:12 -0500114
Alexandre Lisiona7ab2e32014-02-14 15:33:33 -0500115 private void initializeWiFiListener() {
Alexandre Lisiona3c4f5f2014-01-07 17:22:22 -0500116 Log.i(TAG, "executing initializeWiFiListener");
117
118 String connectivity_context = Context.WIFI_SERVICE;
119 wifiManager = (WifiManager) getActivity().getSystemService(connectivity_context);
120
Alexandre Lisiona7ab2e32014-02-14 15:33:33 -0500121 if (!wifiManager.isWifiEnabled()) {
122 if (wifiManager.getWifiState() != WifiManager.WIFI_STATE_ENABLING) {
Alexandre Lisiona3c4f5f2014-01-07 17:22:22 -0500123 wifiManager.setWifiEnabled(true);
124 }
125 }
126
127 getActivity().registerReceiver(wifiReceiver, new IntentFilter(WifiManager.RSSI_CHANGED_ACTION));
alision1005ba12013-06-19 13:52:44 -0400128 }
alision85992112013-05-29 12:18:08 -0400129
alision1005ba12013-06-19 13:52:44 -0400130 /**
131 * A dummy implementation of the {@link Callbacks} interface that does nothing. Used only when this fragment is not attached to an activity.
132 */
133 private static Callbacks sDummyCallbacks = new Callbacks() {
alision85704182013-05-29 15:23:03 -0400134
alision1005ba12013-06-19 13:52:44 -0400135 @Override
136 public ISipService getService() {
137 return null;
138 }
alisiondf1dac92013-06-27 17:35:53 -0400139
Alexandre Lisionccf0c932013-10-10 16:24:41 -0400140 @Override
Alexandre Lisiondafe9512013-10-28 16:31:41 -0400141 public void terminateCall() {
Alexandre Lisionccf0c932013-10-10 16:24:41 -0400142 }
Alexandre Lision0c384512013-09-17 17:15:57 -0400143
144 @Override
Alexandre Lision48b49eb2014-02-11 13:37:33 -0500145 public Conference getDisplayedConference() {
146 return null;
147 }
148
149 @Override
Alexandre Lisiona7ab2e32014-02-14 15:33:33 -0500150 public void updateDisplayedConference(Conference c) {
151 }
152
153 @Override
Adrien Béraude78d06f2013-09-19 13:33:44 +1000154 public void startTimer() {
Alexandre Lision0c384512013-09-17 17:15:57 -0400155 }
Alexandre Lision68855472013-10-10 16:20:46 -0400156
157 @Override
Alexandre Lisionb8add812013-10-24 11:42:42 -0400158 public void slideChatScreen() {
Alexandre Lision68855472013-10-10 16:20:46 -0400159 }
Alexandre Lisiona7ab2e32014-02-14 15:33:33 -0500160
alision1005ba12013-06-19 13:52:44 -0400161 };
alision907bde72013-06-20 14:40:37 -0400162
alision1005ba12013-06-19 13:52:44 -0400163 /**
164 * The Activity calling this fragment has to implement this interface
alision1005ba12013-06-19 13:52:44 -0400165 */
166 public interface Callbacks {
alision85992112013-05-29 12:18:08 -0400167
alision1005ba12013-06-19 13:52:44 -0400168 public ISipService getService();
alision85704182013-05-29 15:23:03 -0400169
Alexandre Lision0c384512013-09-17 17:15:57 -0400170 public void startTimer();
Alexandre Lision68855472013-10-10 16:20:46 -0400171
172 public void slideChatScreen();
Alexandre Lisionb8add812013-10-24 11:42:42 -0400173
Alexandre Lisiondafe9512013-10-28 16:31:41 -0400174 public void terminateCall();
Alexandre Lision48b49eb2014-02-11 13:37:33 -0500175
176 public Conference getDisplayedConference();
Alexandre Lisiona7ab2e32014-02-14 15:33:33 -0500177
178 public void updateDisplayedConference(Conference c);
alision1005ba12013-06-19 13:52:44 -0400179 }
alision85992112013-05-29 12:18:08 -0400180
alision1005ba12013-06-19 13:52:44 -0400181 @Override
182 public void onAttach(Activity activity) {
183 super.onAttach(activity);
alision85992112013-05-29 12:18:08 -0400184
alision1005ba12013-06-19 13:52:44 -0400185 if (!(activity instanceof Callbacks)) {
186 throw new IllegalStateException("Activity must implement fragment's callbacks.");
187 }
alision85992112013-05-29 12:18:08 -0400188
alision1005ba12013-06-19 13:52:44 -0400189 // rootView.requestDisallowInterceptTouchEvent(true);
alision85992112013-05-29 12:18:08 -0400190
alision1005ba12013-06-19 13:52:44 -0400191 mCallbacks = (Callbacks) activity;
Alexandre Lisionb8add812013-10-24 11:42:42 -0400192 // myself = SipCall.SipCallBuilder.buildMyselfCall(activity.getContentResolver(), "Me");
Adrien Béraude78d06f2013-09-19 13:33:44 +1000193
alision1005ba12013-06-19 13:52:44 -0400194 }
Alexandre Lisionb8add812013-10-24 11:42:42 -0400195
Alexandre Lisiond588bff2013-10-08 12:43:01 -0400196 @Override
197 public void onCreateOptionsMenu(Menu m, MenuInflater inf) {
198 super.onCreateOptionsMenu(m, inf);
Alexandre Lision68855472013-10-10 16:20:46 -0400199 inf.inflate(R.menu.ac_call, m);
Alexandre Lisiond588bff2013-10-08 12:43:01 -0400200 }
201
202 @Override
203 public boolean onOptionsItemSelected(MenuItem item) {
204 super.onOptionsItemSelected(item);
Alexandre Lision666b3772013-10-28 17:42:48 -0400205 switch (item.getItemId()) {
Alexandre Lisiona7ab2e32014-02-14 15:33:33 -0500206 case R.id.menuitem_chat:
207 mCallbacks.slideChatScreen();
208 break;
Alexandre Lision666b3772013-10-28 17:42:48 -0400209 }
Alexandre Lisiond588bff2013-10-08 12:43:01 -0400210
211 return true;
212 }
alision85992112013-05-29 12:18:08 -0400213
alision1005ba12013-06-19 13:52:44 -0400214 @Override
215 public void onDetach() {
216 super.onDetach();
217 mCallbacks = sDummyCallbacks;
alision1005ba12013-06-19 13:52:44 -0400218 }
alision907bde72013-06-20 14:40:37 -0400219
alision1005ba12013-06-19 13:52:44 -0400220 @Override
alision907bde72013-06-20 14:40:37 -0400221 public void onStop() {
alision1005ba12013-06-19 13:52:44 -0400222 super.onStop();
223 }
alision85992112013-05-29 12:18:08 -0400224
alision1005ba12013-06-19 13:52:44 -0400225 @Override
Alexandre Lisionf1850c02013-09-23 14:19:34 -0400226 public void onResume() {
227 super.onResume();
Alexandre Lisiona3c4f5f2014-01-07 17:22:22 -0500228 initializeWiFiListener();
Alexandre Lisionf1850c02013-09-23 14:19:34 -0400229 }
230
231 @Override
232 public void onPause() {
233 super.onPause();
Alexandre Lisiona3c4f5f2014-01-07 17:22:22 -0500234 getActivity().unregisterReceiver(wifiReceiver);
Alexandre Lision4ecfd812014-02-21 15:53:32 -0500235 if (mScreenWakeLock != null && mScreenWakeLock.isHeld()) {
236 mScreenWakeLock.release();
Alexandre Lisionf02190d2013-12-12 17:26:12 -0500237 }
238
Alexandre Lisionf1850c02013-09-23 14:19:34 -0400239 }
240
241 @Override
Alexandre Lisiona7ab2e32014-02-14 15:33:33 -0500242 public void callStateChanged(Conference updated, String callID, String newState) {
243 mCallbacks.updateDisplayedConference(updated);
244 Log.i(TAG, "Call :" + callID + " " + newState);
245
246 if (getConference().isOnGoing()) {
247 initNormalStateDisplay();
248 } else if (getConference().isRinging()) {
Alexandre Lision4ecfd812014-02-21 15:53:32 -0500249 mCallStatusTxt.setText(newState);
Alexandre Lisiona7ab2e32014-02-14 15:33:33 -0500250
251 if (getConference().isIncoming()) {
252 initIncomingCallDisplay();
253 } else
254 initOutGoingCallDisplay();
255 } else {
Alexandre Lision4ecfd812014-02-21 15:53:32 -0500256 mCallStatusTxt.setText(newState);
Alexandre Lisiona7ab2e32014-02-14 15:33:33 -0500257 mCallbacks.terminateCall();
258 }
Alexandre Lision5f144b82014-02-11 09:59:36 -0500259 }
260
261 @Override
Alexandre Lisiona7ab2e32014-02-14 15:33:33 -0500262 public void secureZrtpOn(Conference updated, String id) {
Alexandre Lision5f144b82014-02-11 09:59:36 -0500263 Log.i(TAG, "secureZrtpOn");
Alexandre Lisiona7ab2e32014-02-14 15:33:33 -0500264 mCallbacks.updateDisplayedConference(updated);
Alexandre Lision5f144b82014-02-11 09:59:36 -0500265 }
266
267 @Override
Alexandre Lisiona7ab2e32014-02-14 15:33:33 -0500268 public void secureZrtpOff(Conference updated, String id) {
Alexandre Lision5f144b82014-02-11 09:59:36 -0500269 Log.i(TAG, "secureZrtpOff");
Alexandre Lisiona7ab2e32014-02-14 15:33:33 -0500270 mCallbacks.updateDisplayedConference(updated);
Alexandre Lision5f144b82014-02-11 09:59:36 -0500271 }
272
273 @Override
Alexandre Lisiona7ab2e32014-02-14 15:33:33 -0500274 public void displaySAS(Conference updated, final String securedCallID) {
Alexandre Lision5f144b82014-02-11 09:59:36 -0500275 Log.i(TAG, "displaySAS");
Alexandre Lisiona7ab2e32014-02-14 15:33:33 -0500276 mCallbacks.updateDisplayedConference(updated);
Alexandre Lisiona0e45822014-02-14 17:36:11 -0500277 SecureSipCall display = (SecureSipCall) getConference().getCallById(securedCallID);
Alexandre Lision4ecfd812014-02-21 15:53:32 -0500278 enableZRTP(display);
Alexandre Lision5f144b82014-02-11 09:59:36 -0500279 }
280
281 @Override
Alexandre Lision2b237922013-09-09 16:23:02 -0400282 public void onActivityResult(int requestCode, int resultCode, Intent data) {
283 super.onActivityResult(requestCode, resultCode, data);
Alexandre Lision5f144b82014-02-11 09:59:36 -0500284 SipCall transfer;
Alexandre Lision2b237922013-09-09 16:23:02 -0400285 if (requestCode == REQUEST_TRANSFER) {
286 switch (resultCode) {
Alexandre Lisiona7ab2e32014-02-14 15:33:33 -0500287 case TransferDFragment.RESULT_TRANSFER_CONF:
288 Conference c = data.getParcelableExtra("target");
289 transfer = data.getParcelableExtra("transfer");
290 try {
Alexandre Lision2b237922013-09-09 16:23:02 -0400291
Alexandre Lisiona7ab2e32014-02-14 15:33:33 -0500292 mCallbacks.getService().attendedTransfer(transfer.getCallId(), c.getParticipants().get(0).getCallId());
Alexandre Lision2b237922013-09-09 16:23:02 -0400293
Alexandre Lisiona7ab2e32014-02-14 15:33:33 -0500294 } catch (RemoteException e) {
295 e.printStackTrace();
296 }
297 break;
Alexandre Lision2b237922013-09-09 16:23:02 -0400298
Alexandre Lisiona7ab2e32014-02-14 15:33:33 -0500299 case TransferDFragment.RESULT_TRANSFER_NUMBER:
300 String to = data.getStringExtra("to_number");
301 transfer = data.getParcelableExtra("transfer");
302 try {
303 mCallbacks.getService().transfer(transfer.getCallId(), to);
304 mCallbacks.getService().hangUp(transfer.getCallId());
305 } catch (RemoteException e) {
306 e.printStackTrace();
307 }
308 break;
309 case Activity.RESULT_CANCELED:
310 default:
311 model.clear();
312 initNormalStateDisplay();
313 break;
Alexandre Lision2b237922013-09-09 16:23:02 -0400314 }
315 }
316 }
317
318 @Override
alision1005ba12013-06-19 13:52:44 -0400319 public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
Alexandre Lision84208a32013-09-25 13:18:37 -0400320 final ViewGroup rootView = (ViewGroup) inflater.inflate(R.layout.frag_call, container, false);
alision85992112013-05-29 12:18:08 -0400321
Alexandre Lision4ecfd812014-02-21 15:53:32 -0500322 mBubbleView = (BubblesView) rootView.findViewById(R.id.main_view);
323 mBubbleView.setFragment(this);
324 mBubbleView.setModel(model);
325 mBubbleView.getHolder().addCallback(this);
alision84813a12013-05-27 17:40:39 -0400326
Alexandre Lision4ecfd812014-02-21 15:53:32 -0500327 mCallStatusTxt = (TextView) rootView.findViewById(R.id.call_status_txt);
Alexandre Lisioncb2345c2013-12-09 15:39:13 -0500328
Alexandre Lisioncb2345c2013-12-09 15:39:13 -0500329
Alexandre Lision4ecfd812014-02-21 15:53:32 -0500330 mToggleSpeakers = (ToggleButton) rootView.findViewById(R.id.speaker_toggle);
331
332 mToggleSpeakers.setOnCheckedChangeListener(new OnCheckedChangeListener() {
Alexandre Lisioncb2345c2013-12-09 15:39:13 -0500333
Alexandre Lision35577132013-12-06 15:21:15 -0500334 @Override
335 public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
336 try {
337 mCallbacks.getService().toggleSpeakerPhone(isChecked);
338 } catch (RemoteException e) {
339 e.printStackTrace();
340 }
Alexandre Lisioncb2345c2013-12-09 15:39:13 -0500341
Alexandre Lision35577132013-12-06 15:21:15 -0500342 }
343 });
alision84813a12013-05-27 17:40:39 -0400344
Alexandre Lision5f144b82014-02-11 09:59:36 -0500345 rootView.findViewById(R.id.dialpad_btn).setOnClickListener(new OnClickListener() {
Alexandre Lision84208a32013-09-25 13:18:37 -0400346
347 @Override
348 public void onClick(View v) {
349 InputMethodManager lManager = (InputMethodManager) getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
350 lManager.toggleSoftInput(InputMethodManager.SHOW_IMPLICIT, InputMethodManager.HIDE_IMPLICIT_ONLY);
351 }
352 });
353
alision1005ba12013-06-19 13:52:44 -0400354 return rootView;
355 }
Alexandre Lisionc30e8412013-09-26 15:12:59 -0400356
Alexandre Lisiona7ab2e32014-02-14 15:33:33 -0500357 public Conference getConference() {
Alexandre Lision48b49eb2014-02-11 13:37:33 -0500358 return mCallbacks.getDisplayedConference();
359 }
360
alision1005ba12013-06-19 13:52:44 -0400361 private void initNormalStateDisplay() {
362 Log.i(TAG, "Start normal display");
alision84813a12013-05-27 17:40:39 -0400363
Alexandre Lision0c384512013-09-17 17:15:57 -0400364 mCallbacks.startTimer();
alision84813a12013-05-27 17:40:39 -0400365
Alexandre Lision48b49eb2014-02-11 13:37:33 -0500366 getBubbleForUser(getConference(), model.width / 2, model.height / 2);
alision907bde72013-06-20 14:40:37 -0400367
Alexandre Lision48b49eb2014-02-11 13:37:33 -0500368 int angle_part = 360 / getConference().getParticipants().size();
Alexandre Lision5f144b82014-02-11 09:59:36 -0500369 double dX, dY;
alision465ceba2013-07-04 09:24:30 -0400370 int radiusCalls = (int) (model.width / 2 - BUBBLE_SIZE);
Alexandre Lision48b49eb2014-02-11 13:37:33 -0500371 for (int i = 0; i < getConference().getParticipants().size(); ++i) {
alisiondf1dac92013-06-27 17:35:53 -0400372
Alexandre Lision3c37dca2014-02-21 14:13:26 -0500373 SipCall partee = getConference().getParticipants().get(i);
374 if (partee == null) {
alisiondf1dac92013-06-27 17:35:53 -0400375 continue;
376 }
alision34673e62013-06-25 14:40:07 -0400377 dX = Math.cos(Math.toRadians(angle_part * i - 90)) * radiusCalls;
378 dY = Math.sin(Math.toRadians(angle_part * i - 90)) * radiusCalls;
Alexandre Lision3c37dca2014-02-21 14:13:26 -0500379 getBubbleFor(partee, (int) (model.width / 2 + dX), (int) (model.height / 2 + dY));
380 if (partee instanceof SecureSipCall)
381 enableZRTP((SecureSipCall) partee);
alision806e18e2013-06-21 15:30:17 -0400382 }
alision1005ba12013-06-19 13:52:44 -0400383 model.clearAttractors();
alision1005ba12013-06-19 13:52:44 -0400384 }
alision85704182013-05-29 15:23:03 -0400385
Alexandre Lision3c37dca2014-02-21 14:13:26 -0500386 private void enableZRTP(final SecureSipCall secured) {
387 if (!secured.isConfirmedSAS()) {
388 final Button sas = (Button) getView().findViewById(R.id.confirm_sas);
389 sas.setText("Confirm SAS: " + secured.getSAS());
390 sas.setVisibility(View.VISIBLE);
391 sas.setOnClickListener(new OnClickListener() {
392 @Override
393 public void onClick(View v) {
394 try {
395 mCallbacks.getService().confirmSAS(secured.getCallId());
396 sas.setVisibility(View.INVISIBLE);
397 } catch (RemoteException e) {
398 e.printStackTrace();
Alexandre Lisiona0e45822014-02-14 17:36:11 -0500399 }
Alexandre Lision3c37dca2014-02-21 14:13:26 -0500400 }
401 });
402 } else {
Alexandre Lision1b932d82014-02-21 10:03:19 -0500403
Alexandre Lisiona0e45822014-02-14 17:36:11 -0500404 }
405 }
406
alision1005ba12013-06-19 13:52:44 -0400407 private void initIncomingCallDisplay() {
408 Log.i(TAG, "Start incoming display");
alision84813a12013-05-27 17:40:39 -0400409
Alexandre Lision23628c12013-09-24 11:17:05 -0400410 int radiusCalls = (int) (model.width / 2 - BUBBLE_SIZE);
Alexandre Lision48b49eb2014-02-11 13:37:33 -0500411 getBubbleForUser(getConference(), model.width / 2, model.height / 2 + radiusCalls);
412 getBubbleFor(getConference().getParticipants().get(0), model.width / 2, model.height / 2 - radiusCalls);
Alexandre Lision4ecfd812014-02-21 15:53:32 -0500413 Bitmap call_icon = BitmapFactory.decodeResource(getResources(), R.drawable.ic_action_call);
alision1005ba12013-06-19 13:52:44 -0400414 model.clearAttractors();
Alexandre Lision23628c12013-09-24 11:17:05 -0400415 model.addAttractor(new Attractor(new PointF(model.width / 2, model.height / 2), ATTRACTOR_SIZE, new Attractor.Callback() {
alision1005ba12013-06-19 13:52:44 -0400416 @Override
417 public boolean onBubbleSucked(Bubble b) {
Alexandre Lisiona764c682013-09-09 10:02:07 -0400418 if (!accepted) {
Alexandre Lision68855472013-10-10 16:20:46 -0400419 try {
420 mCallbacks.getService().accept(b.getCallID());
421 } catch (RemoteException e) {
422 e.printStackTrace();
423 }
Alexandre Lisiona764c682013-09-09 10:02:07 -0400424 accepted = true;
425 }
alision1005ba12013-06-19 13:52:44 -0400426 return false;
427 }
428 }, call_icon));
Alexandre Lisioncb2345c2013-12-09 15:39:13 -0500429
Alexandre Lision48b49eb2014-02-11 13:37:33 -0500430 if (getConference().getParticipants().get(0).getAccount().isAutoanswerEnabled()) {
Alexandre Lision332f4912013-11-12 13:04:32 -0500431 try {
Alexandre Lision48b49eb2014-02-11 13:37:33 -0500432 mCallbacks.getService().accept(getConference().getParticipants().get(0).getCallId());
Alexandre Lision332f4912013-11-12 13:04:32 -0500433 } catch (RemoteException e) {
434 e.printStackTrace();
435 }
436 }
alision1005ba12013-06-19 13:52:44 -0400437 }
alision85704182013-05-29 15:23:03 -0400438
alision1005ba12013-06-19 13:52:44 -0400439 private void initOutGoingCallDisplay() {
440 Log.i(TAG, "Start outgoing display");
alision85704182013-05-29 15:23:03 -0400441
Alexandre Lision48b49eb2014-02-11 13:37:33 -0500442 getBubbleForUser(getConference(), model.width / 2, model.height / 2);
alision85704182013-05-29 15:23:03 -0400443
alisiondf1dac92013-06-27 17:35:53 -0400444 // TODO off-thread image loading
Alexandre Lision48b49eb2014-02-11 13:37:33 -0500445 int angle_part = 360 / getConference().getParticipants().size();
Alexandre Lision5f144b82014-02-11 09:59:36 -0500446 double dX, dY;
Adrien Béraude78d06f2013-09-19 13:33:44 +1000447 int radiusCalls = (int) (model.width / 2 - BUBBLE_SIZE);
Alexandre Lision48b49eb2014-02-11 13:37:33 -0500448 for (int i = 0; i < getConference().getParticipants().size(); ++i) {
alisiondf1dac92013-06-27 17:35:53 -0400449 dX = Math.cos(Math.toRadians(angle_part * i - 90)) * radiusCalls;
450 dY = Math.sin(Math.toRadians(angle_part * i - 90)) * radiusCalls;
Alexandre Lision48b49eb2014-02-11 13:37:33 -0500451 getBubbleFor(getConference().getParticipants().get(i), (int) (model.width / 2 + dX), (int) (model.height / 2 + dY));
alisiondf1dac92013-06-27 17:35:53 -0400452 }
alision907bde72013-06-20 14:40:37 -0400453
alision1005ba12013-06-19 13:52:44 -0400454 model.clearAttractors();
alision1005ba12013-06-19 13:52:44 -0400455 }
alision85704182013-05-29 15:23:03 -0400456
alision1005ba12013-06-19 13:52:44 -0400457 /**
458 * 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 -0500459 *
460 * @param call The call associated to a contact
461 * @param x Initial or new x position.
462 * @param y Initial or new y position.
alision1005ba12013-06-19 13:52:44 -0400463 * @return Bubble corresponding to the contact.
464 */
alision806e18e2013-06-21 15:30:17 -0400465 private Bubble getBubbleFor(SipCall call, float x, float y) {
Alexandre Lisionee2494d2013-10-09 17:14:00 -0400466 Bubble contact_bubble = model.getBubble(call.getCallId());
alision1005ba12013-06-19 13:52:44 -0400467 if (contact_bubble != null) {
Alexandre Lisionb8add812013-10-24 11:42:42 -0400468 ((BubbleContact) contact_bubble).setCall(call);
alision1005ba12013-06-19 13:52:44 -0400469 contact_bubble.attractor.set(x, y);
470 return contact_bubble;
471 }
alision84813a12013-05-27 17:40:39 -0400472
Alexandre Lision40954dc2013-10-09 15:24:03 -0400473 contact_bubble = new BubbleContact(getActivity(), call, x, y, BUBBLE_SIZE);
474
475 model.addBubble(contact_bubble);
476 return contact_bubble;
477 }
Alexandre Lisionb8add812013-10-24 11:42:42 -0400478
Alexandre Lisionee2494d2013-10-09 17:14:00 -0400479 private Bubble getBubbleForUser(Conference conf, float x, float y) {
Alexandre Lisiond5686032013-10-29 11:09:21 -0400480 Bubble contact_bubble = model.getUser();
Alexandre Lision40954dc2013-10-09 15:24:03 -0400481 if (contact_bubble != null) {
482 contact_bubble.attractor.set(x, y);
Alexandre Lisionb8add812013-10-24 11:42:42 -0400483 ((BubbleUser) contact_bubble).setConference(conf);
Alexandre Lisioncb2345c2013-12-09 15:39:13 -0500484
Alexandre Lision40954dc2013-10-09 15:24:03 -0400485 return contact_bubble;
486 }
487
Alexandre Lisioncb2345c2013-12-09 15:39:13 -0500488 contact_bubble = new BubbleUser(getActivity(), CallContact.ContactBuilder.buildUserContact(getActivity().getContentResolver()), conf, x, y,
489 BUBBLE_SIZE * 1.3f);
alision84813a12013-05-27 17:40:39 -0400490
Alexandre Lisionc9fca9e2013-11-08 15:21:18 -0500491 try {
492 ((BubbleUser) contact_bubble).setMute(mCallbacks.getService().isCaptureMuted());
493 } catch (RemoteException e) {
494 e.printStackTrace();
Alexandre Lisioncb2345c2013-12-09 15:39:13 -0500495 } catch (NullPointerException e1) {
Alexandre Lisionc9fca9e2013-11-08 15:21:18 -0500496 e1.printStackTrace();
497 }
alision1005ba12013-06-19 13:52:44 -0400498 model.addBubble(contact_bubble);
alision1005ba12013-06-19 13:52:44 -0400499 return contact_bubble;
500 }
Adrien Béraud0c9bd8f2013-05-30 16:16:57 +1000501
alision1005ba12013-06-19 13:52:44 -0400502 public boolean draggingBubble() {
Alexandre Lision4ecfd812014-02-21 15:53:32 -0500503 return mBubbleView == null ? false : mBubbleView.isDraggingBubble();
alision1005ba12013-06-19 13:52:44 -0400504 }
Adrien Béraudc9c424d2013-05-30 17:47:35 +1000505
alision1005ba12013-06-19 13:52:44 -0400506 @Override
507 public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
Adrien Béraud13cde0b2013-05-30 20:27:20 +1000508
Alexandre Lision48b49eb2014-02-11 13:37:33 -0500509 if (getConference().getParticipants().size() == 1) {
510 if (getConference().getParticipants().get(0).isIncoming() && getConference().getParticipants().get(0).isRinging()) {
alision806e18e2013-06-21 15:30:17 -0400511 initIncomingCallDisplay();
Alexandre Lisiona7ab2e32014-02-14 15:33:33 -0500512 } else if (getConference().getParticipants().get(0).isRinging()) {
513 initOutGoingCallDisplay();
514 } else if (getConference().getParticipants().get(0).isOngoing()) {
alision806e18e2013-06-21 15:30:17 -0400515 initNormalStateDisplay();
516 }
Alexandre Lision48b49eb2014-02-11 13:37:33 -0500517 } else if (getConference().getParticipants().size() > 1) {
alision1005ba12013-06-19 13:52:44 -0400518 initNormalStateDisplay();
519 }
alision1005ba12013-06-19 13:52:44 -0400520 }
Adrien Béraud13cde0b2013-05-30 20:27:20 +1000521
Alexandre Lision40954dc2013-10-09 15:24:03 -0400522 public void makeTransfer(BubbleContact contact) {
Alexandre Lision2b237922013-09-09 16:23:02 -0400523 FragmentManager fm = getFragmentManager();
Alexandre Lision0eb02032013-09-27 16:32:40 -0400524 editName = TransferDFragment.newInstance();
Alexandre Lision2b237922013-09-09 16:23:02 -0400525 Bundle b = new Bundle();
Alexandre Lision6ae652d2013-09-26 16:39:20 -0400526 try {
Alexandre Lisionb8add812013-10-24 11:42:42 -0400527 b.putParcelableArrayList("calls", (ArrayList<Conference>) mCallbacks.getService().getConcurrentCalls());
Alexandre Lision6ae652d2013-09-26 16:39:20 -0400528 b.putParcelable("call_selected", contact.associated_call);
529 editName.setArguments(b);
530 editName.setTargetFragment(this, REQUEST_TRANSFER);
531 editName.show(fm, "");
532 } catch (RemoteException e) {
533 Log.e(TAG, e.toString());
534 }
535
Alexandre Lision2b237922013-09-09 16:23:02 -0400536 }
537
alision1005ba12013-06-19 13:52:44 -0400538 @Override
539 public void surfaceCreated(SurfaceHolder holder) {
Alexandre Lisionc30e8412013-09-26 15:12:59 -0400540
alision1005ba12013-06-19 13:52:44 -0400541 }
Adrien Béraud13cde0b2013-05-30 20:27:20 +1000542
alision1005ba12013-06-19 13:52:44 -0400543 @Override
544 public void surfaceDestroyed(SurfaceHolder holder) {
Alexandre Lision84208a32013-09-25 13:18:37 -0400545 // check that soft input is hidden
546 InputMethodManager lManager = (InputMethodManager) getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
Alexandre Lision4ecfd812014-02-21 15:53:32 -0500547 lManager.hideSoftInputFromWindow(mBubbleView.getWindowToken(), 0);
Alexandre Lisionb8add812013-10-24 11:42:42 -0400548 if (editName != null && editName.isVisible()) {
Alexandre Lision0eb02032013-09-27 16:32:40 -0400549 editName.dismiss();
550 }
alision1005ba12013-06-19 13:52:44 -0400551 }
Adrien Béraud13cde0b2013-05-30 20:27:20 +1000552
alisione38001f2013-06-04 14:14:39 -0400553 public BubblesView getBubbleView() {
Alexandre Lision4ecfd812014-02-21 15:53:32 -0500554 return mBubbleView;
alision1005ba12013-06-19 13:52:44 -0400555 }
Alexandre Lision0c384512013-09-17 17:15:57 -0400556
557 public void updateTime() {
Alexandre Lisiona7ab2e32014-02-14 15:33:33 -0500558 if (getConference() != null) {
559 long duration = System.currentTimeMillis() - getConference().getParticipants().get(0).getTimestampStart_();
560 duration = duration / 1000;
561 if (getConference().isOnGoing())
Alexandre Lision4ecfd812014-02-21 15:53:32 -0500562 mCallStatusTxt.setText(String.format("%d:%02d:%02d", duration / 3600, duration % 3600 / 60, duration % 60));
Alexandre Lisiona7ab2e32014-02-14 15:33:33 -0500563 }
564
Alexandre Lisionf1850c02013-09-23 14:19:34 -0400565 }
566
Alexandre Lision64dc8c02013-09-25 15:32:25 -0400567 public void onKeyUp(int keyCode, KeyEvent event) {
568 try {
Alexandre Lisionb8add812013-10-24 11:42:42 -0400569
Alexandre Lisionfda4d1d2013-10-25 15:29:53 -0400570 switch (keyCode) {
Alexandre Lisiona7ab2e32014-02-14 15:33:33 -0500571 case KeyEvent.KEYCODE_VOLUME_DOWN:
572 case KeyEvent.KEYCODE_VOLUME_UP:
573 break;
574 default:
575 String toSend = Character.toString(event.getDisplayLabel());
576 toSend.toUpperCase(Locale.getDefault());
577 Log.d(TAG, "toSend " + toSend);
578 mCallbacks.getService().playDtmf(toSend);
579 break;
Alexandre Lisionfda4d1d2013-10-25 15:29:53 -0400580 }
Alexandre Lision64dc8c02013-09-25 15:32:25 -0400581 } catch (RemoteException e) {
582 e.printStackTrace();
Alexandre Lision3e1e06f2014-02-10 12:13:25 -0500583 } catch (NullPointerException e) {
584 e.printStackTrace();
Alexandre Lision64dc8c02013-09-25 15:32:25 -0400585 }
586 }
alision84813a12013-05-27 17:40:39 -0400587}