blob: 382b1a613a46340485580eb9d0d0cef6ca1f825d [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
alision84813a12013-05-27 17:40:39 -040076
Alexandre Lision3c6b7102013-09-16 16:56:46 -040077 private TextView callStatusTxt;
Alexandre Lision35577132013-12-06 15:21:15 -050078 private ToggleButton speakers;
Alexandre Lisionb8add812013-10-24 11:42:42 -040079
Alexandre Lision5f144b82014-02-11 09:59:36 -050080
Alexandre Lisionf02190d2013-12-12 17:26:12 -050081 private PowerManager powerManager;
82 // Screen wake lock for incoming call
83 private WakeLock wakeLock;
84
alision1005ba12013-06-19 13:52:44 -040085 private BubblesView view;
86 private BubbleModel model;
alision84813a12013-05-27 17:40:39 -040087
Alexandre Lision68855472013-10-10 16:20:46 -040088 public Callbacks mCallbacks = sDummyCallbacks;
Alexandre Lisiona764c682013-09-09 10:02:07 -040089 boolean accepted = false;
alision1005ba12013-06-19 13:52:44 -040090 private Bitmap call_icon;
alision85704182013-05-29 15:23:03 -040091
Alexandre Lision0eb02032013-09-27 16:32:40 -040092 TransferDFragment editName;
Alexandre Lisiona3c4f5f2014-01-07 17:22:22 -050093 private WifiManager wifiManager;
94 private BroadcastReceiver wifiReceiver = new BroadcastReceiver() {
95
96 @Override
97 public void onReceive(Context context, Intent intent) {
98 WifiInfo info = wifiManager.getConnectionInfo();
99 Log.i(TAG, "Level of wifi " +info.getRssi());
100 }
101
102 };
Alexandre Lisionf1850c02013-09-23 14:19:34 -0400103
alision1005ba12013-06-19 13:52:44 -0400104 @Override
105 public void onCreate(Bundle savedBundle) {
106 super.onCreate(savedBundle);
alisiondf1dac92013-06-27 17:35:53 -0400107 model = new BubbleModel(getResources().getDisplayMetrics().density);
alision729b0a22013-07-02 11:57:33 -0400108 BUBBLE_SIZE = getResources().getDimension(R.dimen.bubble_size);
Alexandre Lisiona764c682013-09-09 10:02:07 -0400109 Log.e(TAG, "BUBBLE_SIZE " + BUBBLE_SIZE);
Alexandre Lisiond588bff2013-10-08 12:43:01 -0400110 this.setHasOptionsMenu(true);
alision729b0a22013-07-02 11:57:33 -0400111
Alexandre Lisionf02190d2013-12-12 17:26:12 -0500112 powerManager = (PowerManager) getActivity().getSystemService(Context.POWER_SERVICE);
113 wakeLock = powerManager.newWakeLock(PowerManager.SCREEN_DIM_WAKE_LOCK | PowerManager.ACQUIRE_CAUSES_WAKEUP | PowerManager.ON_AFTER_RELEASE,
114 "org.sflphone.onIncomingCall");
115 wakeLock.setReferenceCounted(false);
116
Alexandre Lision5f144b82014-02-11 09:59:36 -0500117
Alexandre Lisionf02190d2013-12-12 17:26:12 -0500118 Log.d(TAG, "Acquire wake up lock");
119 if (wakeLock != null && !wakeLock.isHeld()) {
120 wakeLock.acquire();
121 }
Alexandre Lisiona3c4f5f2014-01-07 17:22:22 -0500122 }
Alexandre Lisionf02190d2013-12-12 17:26:12 -0500123
Alexandre Lisiona3c4f5f2014-01-07 17:22:22 -0500124 private void initializeWiFiListener(){
125 Log.i(TAG, "executing initializeWiFiListener");
126
127 String connectivity_context = Context.WIFI_SERVICE;
128 wifiManager = (WifiManager) getActivity().getSystemService(connectivity_context);
129
130 if(!wifiManager.isWifiEnabled()){
131 if(wifiManager.getWifiState() != WifiManager.WIFI_STATE_ENABLING){
132 wifiManager.setWifiEnabled(true);
133 }
134 }
135
136 getActivity().registerReceiver(wifiReceiver, new IntentFilter(WifiManager.RSSI_CHANGED_ACTION));
alision1005ba12013-06-19 13:52:44 -0400137 }
alision85992112013-05-29 12:18:08 -0400138
alision1005ba12013-06-19 13:52:44 -0400139 /**
140 * A dummy implementation of the {@link Callbacks} interface that does nothing. Used only when this fragment is not attached to an activity.
141 */
142 private static Callbacks sDummyCallbacks = new Callbacks() {
alision85704182013-05-29 15:23:03 -0400143
alision1005ba12013-06-19 13:52:44 -0400144 @Override
145 public ISipService getService() {
146 return null;
147 }
alisiondf1dac92013-06-27 17:35:53 -0400148
Alexandre Lisionccf0c932013-10-10 16:24:41 -0400149 @Override
Alexandre Lisiondafe9512013-10-28 16:31:41 -0400150 public void terminateCall() {
Alexandre Lisionccf0c932013-10-10 16:24:41 -0400151 }
Alexandre Lision0c384512013-09-17 17:15:57 -0400152
153 @Override
Alexandre Lision48b49eb2014-02-11 13:37:33 -0500154 public Conference getDisplayedConference() {
155 return null;
156 }
157
158 @Override
Adrien Béraude78d06f2013-09-19 13:33:44 +1000159 public void startTimer() {
Alexandre Lision0c384512013-09-17 17:15:57 -0400160 }
Alexandre Lision68855472013-10-10 16:20:46 -0400161
162 @Override
Alexandre Lisionb8add812013-10-24 11:42:42 -0400163 public void slideChatScreen() {
Alexandre Lision68855472013-10-10 16:20:46 -0400164 }
alision1005ba12013-06-19 13:52:44 -0400165 };
alision907bde72013-06-20 14:40:37 -0400166
alision1005ba12013-06-19 13:52:44 -0400167 /**
168 * The Activity calling this fragment has to implement this interface
169 *
170 */
171 public interface Callbacks {
alision85992112013-05-29 12:18:08 -0400172
alision1005ba12013-06-19 13:52:44 -0400173 public ISipService getService();
alision85704182013-05-29 15:23:03 -0400174
Alexandre Lision0c384512013-09-17 17:15:57 -0400175 public void startTimer();
Alexandre Lision68855472013-10-10 16:20:46 -0400176
177 public void slideChatScreen();
Alexandre Lisionb8add812013-10-24 11:42:42 -0400178
Alexandre Lisiondafe9512013-10-28 16:31:41 -0400179 public void terminateCall();
Alexandre Lision48b49eb2014-02-11 13:37:33 -0500180
181 public Conference getDisplayedConference();
alision1005ba12013-06-19 13:52:44 -0400182 }
alision85992112013-05-29 12:18:08 -0400183
alision1005ba12013-06-19 13:52:44 -0400184 @Override
185 public void onAttach(Activity activity) {
186 super.onAttach(activity);
alision85992112013-05-29 12:18:08 -0400187
alision1005ba12013-06-19 13:52:44 -0400188 if (!(activity instanceof Callbacks)) {
189 throw new IllegalStateException("Activity must implement fragment's callbacks.");
190 }
alision85992112013-05-29 12:18:08 -0400191
alision1005ba12013-06-19 13:52:44 -0400192 // rootView.requestDisallowInterceptTouchEvent(true);
alision85992112013-05-29 12:18:08 -0400193
alision1005ba12013-06-19 13:52:44 -0400194 mCallbacks = (Callbacks) activity;
Alexandre Lisionb8add812013-10-24 11:42:42 -0400195 // myself = SipCall.SipCallBuilder.buildMyselfCall(activity.getContentResolver(), "Me");
Adrien Béraude78d06f2013-09-19 13:33:44 +1000196
alision1005ba12013-06-19 13:52:44 -0400197 }
Alexandre Lisionb8add812013-10-24 11:42:42 -0400198
Alexandre Lisiond588bff2013-10-08 12:43:01 -0400199 @Override
200 public void onCreateOptionsMenu(Menu m, MenuInflater inf) {
201 super.onCreateOptionsMenu(m, inf);
Alexandre Lision68855472013-10-10 16:20:46 -0400202 inf.inflate(R.menu.ac_call, m);
Alexandre Lisiond588bff2013-10-08 12:43:01 -0400203 }
204
205 @Override
206 public boolean onOptionsItemSelected(MenuItem item) {
207 super.onOptionsItemSelected(item);
Alexandre Lision666b3772013-10-28 17:42:48 -0400208 switch (item.getItemId()) {
209 case R.id.menuitem_chat:
210 mCallbacks.slideChatScreen();
211 break;
212 }
Alexandre Lisiond588bff2013-10-08 12:43:01 -0400213
214 return true;
215 }
alision85992112013-05-29 12:18:08 -0400216
alision1005ba12013-06-19 13:52:44 -0400217 @Override
218 public void onDetach() {
219 super.onDetach();
220 mCallbacks = sDummyCallbacks;
alision1005ba12013-06-19 13:52:44 -0400221 }
alision907bde72013-06-20 14:40:37 -0400222
alision1005ba12013-06-19 13:52:44 -0400223 @Override
alision907bde72013-06-20 14:40:37 -0400224 public void onStop() {
alision1005ba12013-06-19 13:52:44 -0400225 super.onStop();
226 }
alision85992112013-05-29 12:18:08 -0400227
alision1005ba12013-06-19 13:52:44 -0400228 @Override
Alexandre Lisionf1850c02013-09-23 14:19:34 -0400229 public void onResume() {
230 super.onResume();
Alexandre Lisiona3c4f5f2014-01-07 17:22:22 -0500231 initializeWiFiListener();
Alexandre Lisionf1850c02013-09-23 14:19:34 -0400232 }
233
234 @Override
235 public void onPause() {
236 super.onPause();
Alexandre Lisiona3c4f5f2014-01-07 17:22:22 -0500237 getActivity().unregisterReceiver(wifiReceiver);
Alexandre Lisionf02190d2013-12-12 17:26:12 -0500238 if (wakeLock != null && wakeLock.isHeld()) {
239 wakeLock.release();
240 }
241
Alexandre Lisionf1850c02013-09-23 14:19:34 -0400242 }
243
244 @Override
Alexandre Lision48b49eb2014-02-11 13:37:33 -0500245 public void callStateChanged(String callID, String state) {
246 changeCallState(callID, state);
Alexandre Lision5f144b82014-02-11 09:59:36 -0500247 }
248
249 @Override
Alexandre Lision48b49eb2014-02-11 13:37:33 -0500250 public void recordingChanged(String callID, String filename) {
Alexandre Lision5f144b82014-02-11 09:59:36 -0500251
252 }
253
254 @Override
Alexandre Lision48b49eb2014-02-11 13:37:33 -0500255 public void secureZrtpOn(String id) {
Alexandre Lision5f144b82014-02-11 09:59:36 -0500256 Log.i(TAG, "secureZrtpOn");
257 //enableSASButton();
258 }
259
260 @Override
Alexandre Lision48b49eb2014-02-11 13:37:33 -0500261 public void secureZrtpOff(String id) {
Alexandre Lision5f144b82014-02-11 09:59:36 -0500262 Log.i(TAG, "secureZrtpOff");
263 }
264
265 @Override
Alexandre Lision48b49eb2014-02-11 13:37:33 -0500266 public void displaySAS(final String callID, String SAS, boolean verified) {
Alexandre Lision5f144b82014-02-11 09:59:36 -0500267 Log.i(TAG, "displaySAS");
Alexandre Lision48b49eb2014-02-11 13:37:33 -0500268 final Button sas = (Button) getView().findViewById(R.id.confirm_sas);
269 sas.setText("Confirm SAS: " + SAS);
270 sas.setVisibility(View.VISIBLE);
271 sas.setOnClickListener(new OnClickListener() {
272 @Override
273 public void onClick(View v) {
274 try {
275 mCallbacks.getService().confirmSAS(callID);
276 sas.setVisibility(View.INVISIBLE);
277 } catch (RemoteException e) {
278 e.printStackTrace();
279 }
280 }
281 });
Alexandre Lision5f144b82014-02-11 09:59:36 -0500282 }
283
284 @Override
Alexandre Lision2b237922013-09-09 16:23:02 -0400285 public void onActivityResult(int requestCode, int resultCode, Intent data) {
286 super.onActivityResult(requestCode, resultCode, data);
Alexandre Lision5f144b82014-02-11 09:59:36 -0500287 SipCall transfer;
Alexandre Lision2b237922013-09-09 16:23:02 -0400288 if (requestCode == REQUEST_TRANSFER) {
289 switch (resultCode) {
Adrien Béraude78d06f2013-09-19 13:33:44 +1000290 case TransferDFragment.RESULT_TRANSFER_CONF:
Alexandre Lision2b237922013-09-09 16:23:02 -0400291 Conference c = data.getParcelableExtra("target");
292 transfer = data.getParcelableExtra("transfer");
293 try {
294
295 mCallbacks.getService().attendedTransfer(transfer.getCallId(), c.getParticipants().get(0).getCallId());
296
297 } catch (RemoteException e) {
Alexandre Lision2b237922013-09-09 16:23:02 -0400298 e.printStackTrace();
299 }
Alexandre Lision2b237922013-09-09 16:23:02 -0400300 break;
301
Adrien Béraude78d06f2013-09-19 13:33:44 +1000302 case TransferDFragment.RESULT_TRANSFER_NUMBER:
Alexandre Lision2b237922013-09-09 16:23:02 -0400303 String to = data.getStringExtra("to_number");
304 transfer = data.getParcelableExtra("transfer");
305 try {
Alexandre Lision2b237922013-09-09 16:23:02 -0400306 mCallbacks.getService().transfer(transfer.getCallId(), to);
307 mCallbacks.getService().hangUp(transfer.getCallId());
308 } catch (RemoteException e) {
Alexandre Lision2b237922013-09-09 16:23:02 -0400309 e.printStackTrace();
310 }
311 break;
Adrien Béraud97512842013-09-19 13:37:19 +1000312 case Activity.RESULT_CANCELED:
Alexandre Lision2b237922013-09-09 16:23:02 -0400313 default:
Adrien Béraude78d06f2013-09-19 13:33:44 +1000314 model.clear();
315 initNormalStateDisplay();
Alexandre Lision2b237922013-09-09 16:23:02 -0400316 break;
317 }
318 }
319 }
320
321 @Override
alision1005ba12013-06-19 13:52:44 -0400322 public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
Alexandre Lision84208a32013-09-25 13:18:37 -0400323 final ViewGroup rootView = (ViewGroup) inflater.inflate(R.layout.frag_call, container, false);
alision85992112013-05-29 12:18:08 -0400324
alision1005ba12013-06-19 13:52:44 -0400325 view = (BubblesView) rootView.findViewById(R.id.main_view);
alision34673e62013-06-25 14:40:07 -0400326 view.setFragment(this);
alision1005ba12013-06-19 13:52:44 -0400327 view.setModel(model);
328 view.getHolder().addCallback(this);
alision84813a12013-05-27 17:40:39 -0400329
Alexandre Lision3c6b7102013-09-16 16:56:46 -0400330 callStatusTxt = (TextView) rootView.findViewById(R.id.call_status_txt);
Alexandre Lision68855472013-10-10 16:20:46 -0400331 call_icon = BitmapFactory.decodeResource(getResources(), R.drawable.ic_action_call);
Alexandre Lisioncb2345c2013-12-09 15:39:13 -0500332
Alexandre Lisionbaeeb212013-12-09 12:54:47 -0500333 speakers = (ToggleButton) rootView.findViewById(R.id.speaker_toggle);
Alexandre Lisioncb2345c2013-12-09 15:39:13 -0500334
Alexandre Lision35577132013-12-06 15:21:15 -0500335 speakers.setOnCheckedChangeListener(new OnCheckedChangeListener() {
Alexandre Lisioncb2345c2013-12-09 15:39:13 -0500336
Alexandre Lision35577132013-12-06 15:21:15 -0500337 @Override
338 public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
339 try {
340 mCallbacks.getService().toggleSpeakerPhone(isChecked);
341 } catch (RemoteException e) {
342 e.printStackTrace();
343 }
Alexandre Lisioncb2345c2013-12-09 15:39:13 -0500344
Alexandre Lision35577132013-12-06 15:21:15 -0500345 }
346 });
alision84813a12013-05-27 17:40:39 -0400347
Alexandre Lision5f144b82014-02-11 09:59:36 -0500348 rootView.findViewById(R.id.dialpad_btn).setOnClickListener(new OnClickListener() {
Alexandre Lision84208a32013-09-25 13:18:37 -0400349
350 @Override
351 public void onClick(View v) {
352 InputMethodManager lManager = (InputMethodManager) getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
353 lManager.toggleSoftInput(InputMethodManager.SHOW_IMPLICIT, InputMethodManager.HIDE_IMPLICIT_ONLY);
354 }
355 });
356
alision1005ba12013-06-19 13:52:44 -0400357 return rootView;
358 }
Alexandre Lisionc30e8412013-09-26 15:12:59 -0400359
Alexandre Lision48b49eb2014-02-11 13:37:33 -0500360 public Conference getConference(){
361 return mCallbacks.getDisplayedConference();
362 }
363
alision1005ba12013-06-19 13:52:44 -0400364 private void initNormalStateDisplay() {
365 Log.i(TAG, "Start normal display");
alision84813a12013-05-27 17:40:39 -0400366
Alexandre Lision0c384512013-09-17 17:15:57 -0400367 mCallbacks.startTimer();
alision84813a12013-05-27 17:40:39 -0400368
Alexandre Lision48b49eb2014-02-11 13:37:33 -0500369 getBubbleForUser(getConference(), model.width / 2, model.height / 2);
alision907bde72013-06-20 14:40:37 -0400370
Alexandre Lision48b49eb2014-02-11 13:37:33 -0500371 int angle_part = 360 / getConference().getParticipants().size();
Alexandre Lision5f144b82014-02-11 09:59:36 -0500372 double dX, dY;
alision465ceba2013-07-04 09:24:30 -0400373 int radiusCalls = (int) (model.width / 2 - BUBBLE_SIZE);
Alexandre Lision48b49eb2014-02-11 13:37:33 -0500374 for (int i = 0; i < getConference().getParticipants().size(); ++i) {
alisiondf1dac92013-06-27 17:35:53 -0400375
Alexandre Lision48b49eb2014-02-11 13:37:33 -0500376 if (getConference().getParticipants().get(i) == null) {
alisiondf1dac92013-06-27 17:35:53 -0400377 continue;
378 }
alision34673e62013-06-25 14:40:07 -0400379 dX = Math.cos(Math.toRadians(angle_part * i - 90)) * radiusCalls;
380 dY = Math.sin(Math.toRadians(angle_part * i - 90)) * radiusCalls;
Alexandre Lision48b49eb2014-02-11 13:37:33 -0500381 getBubbleFor(getConference().getParticipants().get(i), (int) (model.width / 2 + dX), (int) (model.height / 2 + dY));
alision806e18e2013-06-21 15:30:17 -0400382 }
alision1005ba12013-06-19 13:52:44 -0400383
384 model.clearAttractors();
alision1005ba12013-06-19 13:52:44 -0400385 }
alision85704182013-05-29 15:23:03 -0400386
alision1005ba12013-06-19 13:52:44 -0400387 private void initIncomingCallDisplay() {
388 Log.i(TAG, "Start incoming display");
alision84813a12013-05-27 17:40:39 -0400389
Alexandre Lision0c384512013-09-17 17:15:57 -0400390 mCallbacks.startTimer();
alision84813a12013-05-27 17:40:39 -0400391
Alexandre Lision23628c12013-09-24 11:17:05 -0400392 int radiusCalls = (int) (model.width / 2 - BUBBLE_SIZE);
Alexandre Lision48b49eb2014-02-11 13:37:33 -0500393 getBubbleForUser(getConference(), model.width / 2, model.height / 2 + radiusCalls);
394 getBubbleFor(getConference().getParticipants().get(0), model.width / 2, model.height / 2 - radiusCalls);
alision85704182013-05-29 15:23:03 -0400395
alision1005ba12013-06-19 13:52:44 -0400396 model.clearAttractors();
Alexandre Lision23628c12013-09-24 11:17:05 -0400397 model.addAttractor(new Attractor(new PointF(model.width / 2, model.height / 2), ATTRACTOR_SIZE, new Attractor.Callback() {
alision1005ba12013-06-19 13:52:44 -0400398 @Override
399 public boolean onBubbleSucked(Bubble b) {
Alexandre Lisiona764c682013-09-09 10:02:07 -0400400
401 if (!accepted) {
Alexandre Lision68855472013-10-10 16:20:46 -0400402 try {
403 mCallbacks.getService().accept(b.getCallID());
404 } catch (RemoteException e) {
405 e.printStackTrace();
406 }
Alexandre Lisiona764c682013-09-09 10:02:07 -0400407 accepted = true;
408 }
alision1005ba12013-06-19 13:52:44 -0400409 return false;
410 }
411 }, call_icon));
Alexandre Lisioncb2345c2013-12-09 15:39:13 -0500412
Alexandre Lision48b49eb2014-02-11 13:37:33 -0500413 if (getConference().getParticipants().get(0).getAccount().isAutoanswerEnabled()) {
Alexandre Lision332f4912013-11-12 13:04:32 -0500414 try {
Alexandre Lision48b49eb2014-02-11 13:37:33 -0500415 mCallbacks.getService().accept(getConference().getParticipants().get(0).getCallId());
Alexandre Lision332f4912013-11-12 13:04:32 -0500416 } catch (RemoteException e) {
417 e.printStackTrace();
418 }
419 }
alision1005ba12013-06-19 13:52:44 -0400420 }
alision85704182013-05-29 15:23:03 -0400421
alision1005ba12013-06-19 13:52:44 -0400422 private void initOutGoingCallDisplay() {
423 Log.i(TAG, "Start outgoing display");
alision85704182013-05-29 15:23:03 -0400424
Alexandre Lision0c384512013-09-17 17:15:57 -0400425 mCallbacks.startTimer();
alision85704182013-05-29 15:23:03 -0400426
Alexandre Lision48b49eb2014-02-11 13:37:33 -0500427 getBubbleForUser(getConference(), model.width / 2, model.height / 2);
alision85704182013-05-29 15:23:03 -0400428
alisiondf1dac92013-06-27 17:35:53 -0400429 // TODO off-thread image loading
Alexandre Lision48b49eb2014-02-11 13:37:33 -0500430 int angle_part = 360 / getConference().getParticipants().size();
Alexandre Lision5f144b82014-02-11 09:59:36 -0500431 double dX, dY;
Adrien Béraude78d06f2013-09-19 13:33:44 +1000432 int radiusCalls = (int) (model.width / 2 - BUBBLE_SIZE);
Alexandre Lision48b49eb2014-02-11 13:37:33 -0500433 for (int i = 0; i < getConference().getParticipants().size(); ++i) {
alisiondf1dac92013-06-27 17:35:53 -0400434 dX = Math.cos(Math.toRadians(angle_part * i - 90)) * radiusCalls;
435 dY = Math.sin(Math.toRadians(angle_part * i - 90)) * radiusCalls;
Alexandre Lision48b49eb2014-02-11 13:37:33 -0500436 getBubbleFor(getConference().getParticipants().get(i), (int) (model.width / 2 + dX), (int) (model.height / 2 + dY));
alisiondf1dac92013-06-27 17:35:53 -0400437 }
alision907bde72013-06-20 14:40:37 -0400438
alision1005ba12013-06-19 13:52:44 -0400439 model.clearAttractors();
alision1005ba12013-06-19 13:52:44 -0400440 }
alision85704182013-05-29 15:23:03 -0400441
alision1005ba12013-06-19 13:52:44 -0400442 /**
443 * Retrieves or create a bubble for a given contact. If the bubble exists, it is moved to the new location.
444 *
alision806e18e2013-06-21 15:30:17 -0400445 * @param call
446 * The call associated to a contact
alision1005ba12013-06-19 13:52:44 -0400447 * @param x
448 * Initial or new x position.
449 * @param y
450 * Initial or new y position.
451 * @return Bubble corresponding to the contact.
452 */
alision806e18e2013-06-21 15:30:17 -0400453 private Bubble getBubbleFor(SipCall call, float x, float y) {
Alexandre Lisionee2494d2013-10-09 17:14:00 -0400454 Bubble contact_bubble = model.getBubble(call.getCallId());
alision1005ba12013-06-19 13:52:44 -0400455 if (contact_bubble != null) {
Alexandre Lisionb8add812013-10-24 11:42:42 -0400456 ((BubbleContact) contact_bubble).setCall(call);
alision1005ba12013-06-19 13:52:44 -0400457 contact_bubble.attractor.set(x, y);
458 return contact_bubble;
459 }
alision84813a12013-05-27 17:40:39 -0400460
Alexandre Lision40954dc2013-10-09 15:24:03 -0400461 contact_bubble = new BubbleContact(getActivity(), call, x, y, BUBBLE_SIZE);
462
463 model.addBubble(contact_bubble);
464 return contact_bubble;
465 }
Alexandre Lisionb8add812013-10-24 11:42:42 -0400466
Alexandre Lisionee2494d2013-10-09 17:14:00 -0400467 private Bubble getBubbleForUser(Conference conf, float x, float y) {
Alexandre Lisiond5686032013-10-29 11:09:21 -0400468 Bubble contact_bubble = model.getUser();
Alexandre Lision40954dc2013-10-09 15:24:03 -0400469 if (contact_bubble != null) {
470 contact_bubble.attractor.set(x, y);
Alexandre Lisionb8add812013-10-24 11:42:42 -0400471 ((BubbleUser) contact_bubble).setConference(conf);
Alexandre Lisioncb2345c2013-12-09 15:39:13 -0500472
Alexandre Lision40954dc2013-10-09 15:24:03 -0400473 return contact_bubble;
474 }
475
Alexandre Lisioncb2345c2013-12-09 15:39:13 -0500476 contact_bubble = new BubbleUser(getActivity(), CallContact.ContactBuilder.buildUserContact(getActivity().getContentResolver()), conf, x, y,
477 BUBBLE_SIZE * 1.3f);
alision84813a12013-05-27 17:40:39 -0400478
Alexandre Lisionc9fca9e2013-11-08 15:21:18 -0500479 try {
480 ((BubbleUser) contact_bubble).setMute(mCallbacks.getService().isCaptureMuted());
481 } catch (RemoteException e) {
482 e.printStackTrace();
Alexandre Lisioncb2345c2013-12-09 15:39:13 -0500483 } catch (NullPointerException e1) {
Alexandre Lisionc9fca9e2013-11-08 15:21:18 -0500484 e1.printStackTrace();
485 }
alision1005ba12013-06-19 13:52:44 -0400486 model.addBubble(contact_bubble);
alision1005ba12013-06-19 13:52:44 -0400487 return contact_bubble;
488 }
Adrien Béraud0c9bd8f2013-05-30 16:16:57 +1000489
alision1005ba12013-06-19 13:52:44 -0400490 public void changeCallState(String callID, String newState) {
Alexandre Lisione4b95842013-11-12 10:36:51 -0500491 Log.i(TAG, "Call :" + callID + " " + newState);
alisiondf1dac92013-06-27 17:35:53 -0400492 if (newState.contentEquals("FAILURE")) {
493 try {
494 mCallbacks.getService().hangUp(callID);
495 } catch (RemoteException e) {
496 e.printStackTrace();
alision806e18e2013-06-21 15:30:17 -0400497 }
alision1005ba12013-06-19 13:52:44 -0400498 }
Alexandre Lision48b49eb2014-02-11 13:37:33 -0500499 if (getConference() == null) {
Alexandre Lisione4b95842013-11-12 10:36:51 -0500500 return;
501 }
Alexandre Lision48b49eb2014-02-11 13:37:33 -0500502 for (int i = 0; i < getConference().getParticipants().size(); ++i) {
503 if (callID.equals(getConference().getParticipants().get(i).getCallId())) {
alisiondf1dac92013-06-27 17:35:53 -0400504 if (newState.contentEquals("HUNGUP")) {
Alexandre Lision48b49eb2014-02-11 13:37:33 -0500505 model.removeBubble(getConference().getParticipants().get(i));
506 getConference().getParticipants().remove(i);
alisiondf1dac92013-06-27 17:35:53 -0400507 } else {
Alexandre Lision48b49eb2014-02-11 13:37:33 -0500508 getConference().getParticipants().get(i).setCallState(newState);
alisiondf1dac92013-06-27 17:35:53 -0400509 }
510 }
511 }
512
Alexandre Lision48b49eb2014-02-11 13:37:33 -0500513 if (getConference().isOnGoing()) {
alisiondf1dac92013-06-27 17:35:53 -0400514 initNormalStateDisplay();
Alexandre Lision4fb22622013-10-21 16:26:33 -0400515 }
alisiondf1dac92013-06-27 17:35:53 -0400516
Alexandre Lision48b49eb2014-02-11 13:37:33 -0500517 if (getConference().getParticipants().size() == 0) {
Alexandre Lisione4b95842013-11-12 10:36:51 -0500518 callStatusTxt.setText(newState);
Alexandre Lisiondafe9512013-10-28 16:31:41 -0400519 mCallbacks.terminateCall();
Alexandre Lisionccf0c932013-10-10 16:24:41 -0400520 }
alision1005ba12013-06-19 13:52:44 -0400521 }
alision84813a12013-05-27 17:40:39 -0400522
alision1005ba12013-06-19 13:52:44 -0400523 public boolean draggingBubble() {
524 return view == null ? false : view.isDraggingBubble();
525 }
Adrien Béraudc9c424d2013-05-30 17:47:35 +1000526
alision1005ba12013-06-19 13:52:44 -0400527 @Override
528 public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
Adrien Béraud13cde0b2013-05-30 20:27:20 +1000529
Alexandre Lision48b49eb2014-02-11 13:37:33 -0500530 if (getConference().getParticipants().size() == 1) {
531 if (getConference().getParticipants().get(0).isIncoming() && getConference().getParticipants().get(0).isRinging()) {
alision806e18e2013-06-21 15:30:17 -0400532 initIncomingCallDisplay();
533 } else {
Alexandre Lision48b49eb2014-02-11 13:37:33 -0500534 if (getConference().getParticipants().get(0).isRinging()) {
alision1005ba12013-06-19 13:52:44 -0400535 initOutGoingCallDisplay();
536 }
alision806e18e2013-06-21 15:30:17 -0400537 try {
Alexandre Lision48b49eb2014-02-11 13:37:33 -0500538 if (getConference().getParticipants().get(0).isOutGoing()
539 && mCallbacks.getService().getConference(getConference().getId()) == null) {
540 mCallbacks.getService().placeCall(getConference().getParticipants().get(0));
alision806e18e2013-06-21 15:30:17 -0400541 initOutGoingCallDisplay();
Alexandre Lision48b49eb2014-02-11 13:37:33 -0500542 } else if (getConference().getParticipants().get(0).isOutGoing() && getConference().getParticipants().get(0).isRinging()) {
alision806e18e2013-06-21 15:30:17 -0400543 initOutGoingCallDisplay();
544 }
545 } catch (RemoteException e) {
546 Log.e(TAG, e.toString());
547 }
alision1005ba12013-06-19 13:52:44 -0400548 }
Alexandre Lision48b49eb2014-02-11 13:37:33 -0500549 if (getConference().getParticipants().get(0).isOngoing()) {
alision806e18e2013-06-21 15:30:17 -0400550 initNormalStateDisplay();
551 }
Alexandre Lision48b49eb2014-02-11 13:37:33 -0500552 } else if (getConference().getParticipants().size() > 1) {
alision1005ba12013-06-19 13:52:44 -0400553 initNormalStateDisplay();
554 }
alision1005ba12013-06-19 13:52:44 -0400555 }
Adrien Béraud13cde0b2013-05-30 20:27:20 +1000556
Alexandre Lision40954dc2013-10-09 15:24:03 -0400557 public void makeTransfer(BubbleContact contact) {
Alexandre Lision2b237922013-09-09 16:23:02 -0400558 FragmentManager fm = getFragmentManager();
Alexandre Lision0eb02032013-09-27 16:32:40 -0400559 editName = TransferDFragment.newInstance();
Alexandre Lision2b237922013-09-09 16:23:02 -0400560 Bundle b = new Bundle();
Alexandre Lision6ae652d2013-09-26 16:39:20 -0400561 try {
Alexandre Lisionb8add812013-10-24 11:42:42 -0400562 b.putParcelableArrayList("calls", (ArrayList<Conference>) mCallbacks.getService().getConcurrentCalls());
Alexandre Lision6ae652d2013-09-26 16:39:20 -0400563 b.putParcelable("call_selected", contact.associated_call);
564 editName.setArguments(b);
565 editName.setTargetFragment(this, REQUEST_TRANSFER);
566 editName.show(fm, "");
567 } catch (RemoteException e) {
568 Log.e(TAG, e.toString());
569 }
570
Alexandre Lision2b237922013-09-09 16:23:02 -0400571 }
572
alision1005ba12013-06-19 13:52:44 -0400573 @Override
574 public void surfaceCreated(SurfaceHolder holder) {
Alexandre Lisionc30e8412013-09-26 15:12:59 -0400575
alision1005ba12013-06-19 13:52:44 -0400576 }
Adrien Béraud13cde0b2013-05-30 20:27:20 +1000577
alision1005ba12013-06-19 13:52:44 -0400578 @Override
579 public void surfaceDestroyed(SurfaceHolder holder) {
Alexandre Lision84208a32013-09-25 13:18:37 -0400580 // check that soft input is hidden
581 InputMethodManager lManager = (InputMethodManager) getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
582 lManager.hideSoftInputFromWindow(view.getWindowToken(), 0);
Alexandre Lisionb8add812013-10-24 11:42:42 -0400583 if (editName != null && editName.isVisible()) {
Alexandre Lision0eb02032013-09-27 16:32:40 -0400584 editName.dismiss();
585 }
alision1005ba12013-06-19 13:52:44 -0400586 }
Adrien Béraud13cde0b2013-05-30 20:27:20 +1000587
alisione38001f2013-06-04 14:14:39 -0400588 public BubblesView getBubbleView() {
589 return view;
alision1005ba12013-06-19 13:52:44 -0400590 }
Alexandre Lision0c384512013-09-17 17:15:57 -0400591
592 public void updateTime() {
Alexandre Lision48b49eb2014-02-11 13:37:33 -0500593 long duration = System.currentTimeMillis() / 1000 - this.getConference().getParticipants().get(0).getTimestampStart_();
594 if (getConference().isOnGoing())
Alexandre Lisione4b95842013-11-12 10:36:51 -0500595 callStatusTxt.setText(String.format("%d:%02d:%02d", duration / 3600, duration % 3600 / 60, duration % 60));
Alexandre Lisionf1850c02013-09-23 14:19:34 -0400596 }
597
Alexandre Lision64dc8c02013-09-25 15:32:25 -0400598 public void onKeyUp(int keyCode, KeyEvent event) {
599 try {
Alexandre Lisionb8add812013-10-24 11:42:42 -0400600
Alexandre Lisionfda4d1d2013-10-25 15:29:53 -0400601 switch (keyCode) {
602 case KeyEvent.KEYCODE_VOLUME_DOWN:
603 case KeyEvent.KEYCODE_VOLUME_UP:
604 break;
605 default:
606 String toSend = Character.toString(event.getDisplayLabel());
607 toSend.toUpperCase(Locale.getDefault());
608 Log.d(TAG, "toSend " + toSend);
609 mCallbacks.getService().playDtmf(toSend);
610 break;
611 }
Alexandre Lision64dc8c02013-09-25 15:32:25 -0400612 } catch (RemoteException e) {
613 e.printStackTrace();
Alexandre Lision3e1e06f2014-02-10 12:13:25 -0500614 } catch (NullPointerException e) {
615 e.printStackTrace();
Alexandre Lision64dc8c02013-09-25 15:32:25 -0400616 }
617 }
alision84813a12013-05-27 17:40:39 -0400618}