blob: 71be78c7d95eb1e50d9d1ba4f3a7b576d3d6a086 [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
Alexandre Lision2b237922013-09-09 16:23:02 -040034import java.util.ArrayList;
Alexandre Lision31f46fc2013-09-26 11:19:54 -040035import java.util.Locale;
Alexandre Lision2b237922013-09-09 16:23:02 -040036
Alexandre Lisiona3c4f5f2014-01-07 17:22:22 -050037import android.content.BroadcastReceiver;
38import android.content.IntentFilter;
39import android.net.wifi.WifiInfo;
40import android.net.wifi.WifiManager;
Alexandre Lision064e1e02013-10-01 16:18:42 -040041import org.sflphone.R;
42import org.sflphone.model.Attractor;
43import org.sflphone.model.Bubble;
Alexandre Lision40954dc2013-10-09 15:24:03 -040044import org.sflphone.model.BubbleContact;
Alexandre Lision064e1e02013-10-01 16:18:42 -040045import org.sflphone.model.BubbleModel;
Alexandre Lision40954dc2013-10-09 15:24:03 -040046import org.sflphone.model.BubbleUser;
Alexandre Lision064e1e02013-10-01 16:18:42 -040047import org.sflphone.model.BubblesView;
Alexandre Lision68855472013-10-10 16:20:46 -040048import org.sflphone.model.CallContact;
Alexandre Lision064e1e02013-10-01 16:18:42 -040049import org.sflphone.model.Conference;
50import org.sflphone.model.SipCall;
51import org.sflphone.service.ISipService;
52
alision84813a12013-05-27 17:40:39 -040053import android.app.Activity;
54import android.app.Fragment;
Alexandre Lision2b237922013-09-09 16:23:02 -040055import android.app.FragmentManager;
Alexandre Lisionf1850c02013-09-23 14:19:34 -040056import android.content.Context;
Alexandre Lision2b237922013-09-09 16:23:02 -040057import android.content.Intent;
alision84813a12013-05-27 17:40:39 -040058import android.graphics.Bitmap;
Adrien Béraud0c9bd8f2013-05-30 16:16:57 +100059import android.graphics.BitmapFactory;
alision84813a12013-05-27 17:40:39 -040060import android.graphics.PointF;
61import android.os.Bundle;
Alexandre Lisionf02190d2013-12-12 17:26:12 -050062import android.os.PowerManager;
alision84813a12013-05-27 17:40:39 -040063import android.os.RemoteException;
Alexandre Lisionf02190d2013-12-12 17:26:12 -050064import android.os.PowerManager.WakeLock;
alision84813a12013-05-27 17:40:39 -040065import android.util.Log;
Alexandre Lision64dc8c02013-09-25 15:32:25 -040066import android.view.KeyEvent;
alision84813a12013-05-27 17:40:39 -040067import android.view.LayoutInflater;
Alexandre Lisiond588bff2013-10-08 12:43:01 -040068import android.view.Menu;
69import android.view.MenuInflater;
70import android.view.MenuItem;
Adrien Béraud13cde0b2013-05-30 20:27:20 +100071import android.view.SurfaceHolder;
72import android.view.SurfaceHolder.Callback;
alision84813a12013-05-27 17:40:39 -040073import android.view.View;
Alexandre Lision84208a32013-09-25 13:18:37 -040074import android.view.View.OnClickListener;
alision84813a12013-05-27 17:40:39 -040075import android.view.ViewGroup;
Alexandre Lision84208a32013-09-25 13:18:37 -040076import android.view.inputmethod.InputMethodManager;
Alexandre Lision35577132013-12-06 15:21:15 -050077import android.widget.CompoundButton;
78import android.widget.CompoundButton.OnCheckedChangeListener;
Alexandre Lision84208a32013-09-25 13:18:37 -040079import android.widget.ImageButton;
Alexandre Lision3c6b7102013-09-16 16:56:46 -040080import android.widget.TextView;
Alexandre Lision35577132013-12-06 15:21:15 -050081import android.widget.ToggleButton;
alision84813a12013-05-27 17:40:39 -040082
Alexandre Lisionfda4d1d2013-10-25 15:29:53 -040083public class CallFragment extends Fragment implements Callback {
alision84813a12013-05-27 17:40:39 -040084
alision1005ba12013-06-19 13:52:44 -040085 static final String TAG = "CallFragment";
alision84813a12013-05-27 17:40:39 -040086
alisionf57d8a62013-07-02 09:37:12 -040087 float BUBBLE_SIZE = 75;
alision1005ba12013-06-19 13:52:44 -040088 static final float ATTRACTOR_SIZE = 40;
alision84813a12013-05-27 17:40:39 -040089
Alexandre Lision2b237922013-09-09 16:23:02 -040090 public static final int REQUEST_TRANSFER = 10;
91
alisiondf1dac92013-06-27 17:35:53 -040092 private Conference conf;
alision84813a12013-05-27 17:40:39 -040093
Alexandre Lision3c6b7102013-09-16 16:56:46 -040094 private TextView callStatusTxt;
Alexandre Lisionb8add812013-10-24 11:42:42 -040095 private TextView codecNameTxt;
Alexandre Lisioncb2345c2013-12-09 15:39:13 -050096
Alexandre Lision35577132013-12-06 15:21:15 -050097 private ToggleButton speakers;
Alexandre Lisionb8add812013-10-24 11:42:42 -040098
Alexandre Lisionf02190d2013-12-12 17:26:12 -050099 private PowerManager powerManager;
100 // Screen wake lock for incoming call
101 private WakeLock wakeLock;
102
alision1005ba12013-06-19 13:52:44 -0400103 private BubblesView view;
104 private BubbleModel model;
alision84813a12013-05-27 17:40:39 -0400105
Alexandre Lision68855472013-10-10 16:20:46 -0400106 public Callbacks mCallbacks = sDummyCallbacks;
Alexandre Lisiona764c682013-09-09 10:02:07 -0400107 boolean accepted = false;
alision1005ba12013-06-19 13:52:44 -0400108 private Bitmap call_icon;
alision85704182013-05-29 15:23:03 -0400109
Alexandre Lision0eb02032013-09-27 16:32:40 -0400110 TransferDFragment editName;
Alexandre Lisiona3c4f5f2014-01-07 17:22:22 -0500111 private WifiManager wifiManager;
112 private BroadcastReceiver wifiReceiver = new BroadcastReceiver() {
113
114 @Override
115 public void onReceive(Context context, Intent intent) {
116 WifiInfo info = wifiManager.getConnectionInfo();
117 Log.i(TAG, "Level of wifi " +info.getRssi());
118 }
119
120 };
Alexandre Lisionf1850c02013-09-23 14:19:34 -0400121
alision1005ba12013-06-19 13:52:44 -0400122 @Override
123 public void onCreate(Bundle savedBundle) {
124 super.onCreate(savedBundle);
Alexandre Lisiondafe9512013-10-28 16:31:41 -0400125 conf = new Conference((Conference) getArguments().getParcelable("conference"));
alisiondf1dac92013-06-27 17:35:53 -0400126 model = new BubbleModel(getResources().getDisplayMetrics().density);
alision729b0a22013-07-02 11:57:33 -0400127 BUBBLE_SIZE = getResources().getDimension(R.dimen.bubble_size);
Alexandre Lisiona764c682013-09-09 10:02:07 -0400128 Log.e(TAG, "BUBBLE_SIZE " + BUBBLE_SIZE);
Alexandre Lisiond588bff2013-10-08 12:43:01 -0400129 this.setHasOptionsMenu(true);
alision729b0a22013-07-02 11:57:33 -0400130
Alexandre Lisionf02190d2013-12-12 17:26:12 -0500131 powerManager = (PowerManager) getActivity().getSystemService(Context.POWER_SERVICE);
132 wakeLock = powerManager.newWakeLock(PowerManager.SCREEN_DIM_WAKE_LOCK | PowerManager.ACQUIRE_CAUSES_WAKEUP | PowerManager.ON_AFTER_RELEASE,
133 "org.sflphone.onIncomingCall");
134 wakeLock.setReferenceCounted(false);
135
136 Log.d(TAG, "Acquire wake up lock");
137 if (wakeLock != null && !wakeLock.isHeld()) {
138 wakeLock.acquire();
139 }
Alexandre Lisiona3c4f5f2014-01-07 17:22:22 -0500140 }
Alexandre Lisionf02190d2013-12-12 17:26:12 -0500141
Alexandre Lisiona3c4f5f2014-01-07 17:22:22 -0500142 private void initializeWiFiListener(){
143 Log.i(TAG, "executing initializeWiFiListener");
144
145 String connectivity_context = Context.WIFI_SERVICE;
146 wifiManager = (WifiManager) getActivity().getSystemService(connectivity_context);
147
148 if(!wifiManager.isWifiEnabled()){
149 if(wifiManager.getWifiState() != WifiManager.WIFI_STATE_ENABLING){
150 wifiManager.setWifiEnabled(true);
151 }
152 }
153
154 getActivity().registerReceiver(wifiReceiver, new IntentFilter(WifiManager.RSSI_CHANGED_ACTION));
alision1005ba12013-06-19 13:52:44 -0400155 }
alision85992112013-05-29 12:18:08 -0400156
alision1005ba12013-06-19 13:52:44 -0400157 /**
158 * A dummy implementation of the {@link Callbacks} interface that does nothing. Used only when this fragment is not attached to an activity.
159 */
160 private static Callbacks sDummyCallbacks = new Callbacks() {
alision85704182013-05-29 15:23:03 -0400161
alision1005ba12013-06-19 13:52:44 -0400162 @Override
163 public ISipService getService() {
164 return null;
165 }
alisiondf1dac92013-06-27 17:35:53 -0400166
Alexandre Lisionccf0c932013-10-10 16:24:41 -0400167 @Override
Alexandre Lisiondafe9512013-10-28 16:31:41 -0400168 public void terminateCall() {
Alexandre Lisionccf0c932013-10-10 16:24:41 -0400169 }
Alexandre Lision0c384512013-09-17 17:15:57 -0400170
171 @Override
Adrien Béraude78d06f2013-09-19 13:33:44 +1000172 public void startTimer() {
Alexandre Lision0c384512013-09-17 17:15:57 -0400173 }
Alexandre Lision68855472013-10-10 16:20:46 -0400174
175 @Override
Alexandre Lisionb8add812013-10-24 11:42:42 -0400176 public void slideChatScreen() {
Alexandre Lision68855472013-10-10 16:20:46 -0400177 }
alision1005ba12013-06-19 13:52:44 -0400178 };
alision907bde72013-06-20 14:40:37 -0400179
alision1005ba12013-06-19 13:52:44 -0400180 /**
181 * The Activity calling this fragment has to implement this interface
182 *
183 */
184 public interface Callbacks {
alision85992112013-05-29 12:18:08 -0400185
alision1005ba12013-06-19 13:52:44 -0400186 public ISipService getService();
alision85704182013-05-29 15:23:03 -0400187
Alexandre Lision0c384512013-09-17 17:15:57 -0400188 public void startTimer();
Alexandre Lision68855472013-10-10 16:20:46 -0400189
190 public void slideChatScreen();
Alexandre Lisionb8add812013-10-24 11:42:42 -0400191
Alexandre Lisiondafe9512013-10-28 16:31:41 -0400192 public void terminateCall();
alision1005ba12013-06-19 13:52:44 -0400193 }
alision85992112013-05-29 12:18:08 -0400194
alision1005ba12013-06-19 13:52:44 -0400195 @Override
196 public void onAttach(Activity activity) {
197 super.onAttach(activity);
alision85992112013-05-29 12:18:08 -0400198
alision1005ba12013-06-19 13:52:44 -0400199 if (!(activity instanceof Callbacks)) {
200 throw new IllegalStateException("Activity must implement fragment's callbacks.");
201 }
alision85992112013-05-29 12:18:08 -0400202
alision1005ba12013-06-19 13:52:44 -0400203 // rootView.requestDisallowInterceptTouchEvent(true);
alision85992112013-05-29 12:18:08 -0400204
alision1005ba12013-06-19 13:52:44 -0400205 mCallbacks = (Callbacks) activity;
Alexandre Lisionb8add812013-10-24 11:42:42 -0400206 // myself = SipCall.SipCallBuilder.buildMyselfCall(activity.getContentResolver(), "Me");
Adrien Béraude78d06f2013-09-19 13:33:44 +1000207
alision1005ba12013-06-19 13:52:44 -0400208 }
Alexandre Lisionb8add812013-10-24 11:42:42 -0400209
Alexandre Lisiond588bff2013-10-08 12:43:01 -0400210 @Override
211 public void onCreateOptionsMenu(Menu m, MenuInflater inf) {
212 super.onCreateOptionsMenu(m, inf);
Alexandre Lision68855472013-10-10 16:20:46 -0400213 inf.inflate(R.menu.ac_call, m);
Alexandre Lisiond588bff2013-10-08 12:43:01 -0400214 }
215
216 @Override
217 public boolean onOptionsItemSelected(MenuItem item) {
218 super.onOptionsItemSelected(item);
Alexandre Lision666b3772013-10-28 17:42:48 -0400219 switch (item.getItemId()) {
220 case R.id.menuitem_chat:
221 mCallbacks.slideChatScreen();
222 break;
223 }
Alexandre Lisiond588bff2013-10-08 12:43:01 -0400224
225 return true;
226 }
alision85992112013-05-29 12:18:08 -0400227
alision1005ba12013-06-19 13:52:44 -0400228 @Override
229 public void onDetach() {
230 super.onDetach();
231 mCallbacks = sDummyCallbacks;
alision1005ba12013-06-19 13:52:44 -0400232 }
alision907bde72013-06-20 14:40:37 -0400233
alision1005ba12013-06-19 13:52:44 -0400234 @Override
alision907bde72013-06-20 14:40:37 -0400235 public void onStop() {
alision1005ba12013-06-19 13:52:44 -0400236 super.onStop();
237 }
alision85992112013-05-29 12:18:08 -0400238
alision1005ba12013-06-19 13:52:44 -0400239 @Override
Alexandre Lisionf1850c02013-09-23 14:19:34 -0400240 public void onResume() {
241 super.onResume();
Alexandre Lisiona3c4f5f2014-01-07 17:22:22 -0500242 initializeWiFiListener();
Alexandre Lisionf1850c02013-09-23 14:19:34 -0400243 }
244
245 @Override
246 public void onPause() {
247 super.onPause();
Alexandre Lisiona3c4f5f2014-01-07 17:22:22 -0500248 getActivity().unregisterReceiver(wifiReceiver);
Alexandre Lisionf02190d2013-12-12 17:26:12 -0500249 if (wakeLock != null && wakeLock.isHeld()) {
250 wakeLock.release();
251 }
252
Alexandre Lisionf1850c02013-09-23 14:19:34 -0400253 }
254
255 @Override
Alexandre Lision2b237922013-09-09 16:23:02 -0400256 public void onActivityResult(int requestCode, int resultCode, Intent data) {
257 super.onActivityResult(requestCode, resultCode, data);
258 SipCall transfer = null;
259 if (requestCode == REQUEST_TRANSFER) {
260 switch (resultCode) {
Adrien Béraude78d06f2013-09-19 13:33:44 +1000261 case TransferDFragment.RESULT_TRANSFER_CONF:
Alexandre Lision2b237922013-09-09 16:23:02 -0400262 Conference c = data.getParcelableExtra("target");
263 transfer = data.getParcelableExtra("transfer");
264 try {
265
266 mCallbacks.getService().attendedTransfer(transfer.getCallId(), c.getParticipants().get(0).getCallId());
267
268 } catch (RemoteException e) {
Alexandre Lision2b237922013-09-09 16:23:02 -0400269 e.printStackTrace();
270 }
Alexandre Lision2b237922013-09-09 16:23:02 -0400271 break;
272
Adrien Béraude78d06f2013-09-19 13:33:44 +1000273 case TransferDFragment.RESULT_TRANSFER_NUMBER:
Alexandre Lision2b237922013-09-09 16:23:02 -0400274 String to = data.getStringExtra("to_number");
275 transfer = data.getParcelableExtra("transfer");
276 try {
Alexandre Lision2b237922013-09-09 16:23:02 -0400277 mCallbacks.getService().transfer(transfer.getCallId(), to);
278 mCallbacks.getService().hangUp(transfer.getCallId());
279 } catch (RemoteException e) {
Alexandre Lision2b237922013-09-09 16:23:02 -0400280 e.printStackTrace();
281 }
282 break;
Adrien Béraud97512842013-09-19 13:37:19 +1000283 case Activity.RESULT_CANCELED:
Alexandre Lision2b237922013-09-09 16:23:02 -0400284 default:
Adrien Béraude78d06f2013-09-19 13:33:44 +1000285 model.clear();
286 initNormalStateDisplay();
Alexandre Lision2b237922013-09-09 16:23:02 -0400287 break;
288 }
289 }
290 }
291
292 @Override
alision1005ba12013-06-19 13:52:44 -0400293 public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
Alexandre Lision84208a32013-09-25 13:18:37 -0400294 final ViewGroup rootView = (ViewGroup) inflater.inflate(R.layout.frag_call, container, false);
alision85992112013-05-29 12:18:08 -0400295
alision1005ba12013-06-19 13:52:44 -0400296 view = (BubblesView) rootView.findViewById(R.id.main_view);
alision34673e62013-06-25 14:40:07 -0400297 view.setFragment(this);
alision1005ba12013-06-19 13:52:44 -0400298 view.setModel(model);
299 view.getHolder().addCallback(this);
alision84813a12013-05-27 17:40:39 -0400300
Alexandre Lision4fb22622013-10-21 16:26:33 -0400301 codecNameTxt = (TextView) rootView.findViewById(R.id.codec_name_txt);
Alexandre Lision3c6b7102013-09-16 16:56:46 -0400302 callStatusTxt = (TextView) rootView.findViewById(R.id.call_status_txt);
Alexandre Lision68855472013-10-10 16:20:46 -0400303 call_icon = BitmapFactory.decodeResource(getResources(), R.drawable.ic_action_call);
Alexandre Lisioncb2345c2013-12-09 15:39:13 -0500304
Alexandre Lisionbaeeb212013-12-09 12:54:47 -0500305 speakers = (ToggleButton) rootView.findViewById(R.id.speaker_toggle);
Alexandre Lisioncb2345c2013-12-09 15:39:13 -0500306
Alexandre Lision35577132013-12-06 15:21:15 -0500307 speakers.setOnCheckedChangeListener(new OnCheckedChangeListener() {
Alexandre Lisioncb2345c2013-12-09 15:39:13 -0500308
Alexandre Lision35577132013-12-06 15:21:15 -0500309 @Override
310 public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
311 try {
312 mCallbacks.getService().toggleSpeakerPhone(isChecked);
313 } catch (RemoteException e) {
314 e.printStackTrace();
315 }
Alexandre Lisioncb2345c2013-12-09 15:39:13 -0500316
Alexandre Lision35577132013-12-06 15:21:15 -0500317 }
318 });
alision84813a12013-05-27 17:40:39 -0400319
Alexandre Lision84208a32013-09-25 13:18:37 -0400320 ((ImageButton) rootView.findViewById(R.id.dialpad_btn)).setOnClickListener(new OnClickListener() {
321
322 @Override
323 public void onClick(View v) {
324 InputMethodManager lManager = (InputMethodManager) getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
325 lManager.toggleSoftInput(InputMethodManager.SHOW_IMPLICIT, InputMethodManager.HIDE_IMPLICIT_ONLY);
326 }
327 });
328
alision1005ba12013-06-19 13:52:44 -0400329 return rootView;
330 }
Alexandre Lisionc30e8412013-09-26 15:12:59 -0400331
alision1005ba12013-06-19 13:52:44 -0400332 private void initNormalStateDisplay() {
333 Log.i(TAG, "Start normal display");
alision84813a12013-05-27 17:40:39 -0400334
Alexandre Lision0c384512013-09-17 17:15:57 -0400335 mCallbacks.startTimer();
alision84813a12013-05-27 17:40:39 -0400336
Alexandre Lisionee2494d2013-10-09 17:14:00 -0400337 getBubbleForUser(conf, model.width / 2, model.height / 2);
alision907bde72013-06-20 14:40:37 -0400338
alisiondf1dac92013-06-27 17:35:53 -0400339 int angle_part = 360 / conf.getParticipants().size();
alision806e18e2013-06-21 15:30:17 -0400340 double dX = 0;
341 double dY = 0;
alision465ceba2013-07-04 09:24:30 -0400342 int radiusCalls = (int) (model.width / 2 - BUBBLE_SIZE);
alisiondf1dac92013-06-27 17:35:53 -0400343 for (int i = 0; i < conf.getParticipants().size(); ++i) {
344
345 if (conf.getParticipants().get(i) == null) {
alisiondf1dac92013-06-27 17:35:53 -0400346 continue;
347 }
alision34673e62013-06-25 14:40:07 -0400348 dX = Math.cos(Math.toRadians(angle_part * i - 90)) * radiusCalls;
349 dY = Math.sin(Math.toRadians(angle_part * i - 90)) * radiusCalls;
alisiondf1dac92013-06-27 17:35:53 -0400350 getBubbleFor(conf.getParticipants().get(i), (int) (model.width / 2 + dX), (int) (model.height / 2 + dY));
alision806e18e2013-06-21 15:30:17 -0400351 }
alision1005ba12013-06-19 13:52:44 -0400352
353 model.clearAttractors();
alision1005ba12013-06-19 13:52:44 -0400354 }
alision85704182013-05-29 15:23:03 -0400355
alision1005ba12013-06-19 13:52:44 -0400356 private void initIncomingCallDisplay() {
357 Log.i(TAG, "Start incoming display");
alision84813a12013-05-27 17:40:39 -0400358
Alexandre Lision0c384512013-09-17 17:15:57 -0400359 mCallbacks.startTimer();
alision84813a12013-05-27 17:40:39 -0400360
Alexandre Lision23628c12013-09-24 11:17:05 -0400361 int radiusCalls = (int) (model.width / 2 - BUBBLE_SIZE);
Alexandre Lisionee2494d2013-10-09 17:14:00 -0400362 getBubbleForUser(conf, model.width / 2, model.height / 2 + radiusCalls);
Alexandre Lision23628c12013-09-24 11:17:05 -0400363 getBubbleFor(conf.getParticipants().get(0), model.width / 2, model.height / 2 - radiusCalls);
alision85704182013-05-29 15:23:03 -0400364
alision1005ba12013-06-19 13:52:44 -0400365 model.clearAttractors();
Alexandre Lision23628c12013-09-24 11:17:05 -0400366 model.addAttractor(new Attractor(new PointF(model.width / 2, model.height / 2), ATTRACTOR_SIZE, new Attractor.Callback() {
alision1005ba12013-06-19 13:52:44 -0400367 @Override
368 public boolean onBubbleSucked(Bubble b) {
Alexandre Lisiona764c682013-09-09 10:02:07 -0400369
370 if (!accepted) {
Alexandre Lision68855472013-10-10 16:20:46 -0400371 try {
372 mCallbacks.getService().accept(b.getCallID());
373 } catch (RemoteException e) {
374 e.printStackTrace();
375 }
Alexandre Lisiona764c682013-09-09 10:02:07 -0400376 accepted = true;
377 }
alision1005ba12013-06-19 13:52:44 -0400378 return false;
379 }
380 }, call_icon));
Alexandre Lisioncb2345c2013-12-09 15:39:13 -0500381
382 if (conf.getParticipants().get(0).getAccount().isAutoanswerEnabled()) {
Alexandre Lision332f4912013-11-12 13:04:32 -0500383 try {
384 mCallbacks.getService().accept(conf.getParticipants().get(0).getCallId());
385 } catch (RemoteException e) {
386 e.printStackTrace();
387 }
388 }
alision1005ba12013-06-19 13:52:44 -0400389 }
alision85704182013-05-29 15:23:03 -0400390
alision1005ba12013-06-19 13:52:44 -0400391 private void initOutGoingCallDisplay() {
392 Log.i(TAG, "Start outgoing display");
alision85704182013-05-29 15:23:03 -0400393
Alexandre Lision0c384512013-09-17 17:15:57 -0400394 mCallbacks.startTimer();
alision85704182013-05-29 15:23:03 -0400395
Alexandre Lisionee2494d2013-10-09 17:14:00 -0400396 getBubbleForUser(conf, model.width / 2, model.height / 2);
alision85704182013-05-29 15:23:03 -0400397
alisiondf1dac92013-06-27 17:35:53 -0400398 // TODO off-thread image loading
399 int angle_part = 360 / conf.getParticipants().size();
400 double dX = 0;
401 double dY = 0;
Adrien Béraude78d06f2013-09-19 13:33:44 +1000402 int radiusCalls = (int) (model.width / 2 - BUBBLE_SIZE);
alisiondf1dac92013-06-27 17:35:53 -0400403 for (int i = 0; i < conf.getParticipants().size(); ++i) {
404 dX = Math.cos(Math.toRadians(angle_part * i - 90)) * radiusCalls;
405 dY = Math.sin(Math.toRadians(angle_part * i - 90)) * radiusCalls;
406 getBubbleFor(conf.getParticipants().get(i), (int) (model.width / 2 + dX), (int) (model.height / 2 + dY));
407 }
alision907bde72013-06-20 14:40:37 -0400408
alision1005ba12013-06-19 13:52:44 -0400409 model.clearAttractors();
alision1005ba12013-06-19 13:52:44 -0400410 }
alision85704182013-05-29 15:23:03 -0400411
alision1005ba12013-06-19 13:52:44 -0400412 /**
413 * Retrieves or create a bubble for a given contact. If the bubble exists, it is moved to the new location.
414 *
alision806e18e2013-06-21 15:30:17 -0400415 * @param call
416 * The call associated to a contact
alision1005ba12013-06-19 13:52:44 -0400417 * @param x
418 * Initial or new x position.
419 * @param y
420 * Initial or new y position.
421 * @return Bubble corresponding to the contact.
422 */
alision806e18e2013-06-21 15:30:17 -0400423 private Bubble getBubbleFor(SipCall call, float x, float y) {
Alexandre Lisionee2494d2013-10-09 17:14:00 -0400424 Bubble contact_bubble = model.getBubble(call.getCallId());
alision1005ba12013-06-19 13:52:44 -0400425 if (contact_bubble != null) {
Alexandre Lisionb8add812013-10-24 11:42:42 -0400426 ((BubbleContact) contact_bubble).setCall(call);
alision1005ba12013-06-19 13:52:44 -0400427 contact_bubble.attractor.set(x, y);
428 return contact_bubble;
429 }
alision84813a12013-05-27 17:40:39 -0400430
Alexandre Lision40954dc2013-10-09 15:24:03 -0400431 contact_bubble = new BubbleContact(getActivity(), call, x, y, BUBBLE_SIZE);
432
433 model.addBubble(contact_bubble);
434 return contact_bubble;
435 }
Alexandre Lisionb8add812013-10-24 11:42:42 -0400436
Alexandre Lisionee2494d2013-10-09 17:14:00 -0400437 private Bubble getBubbleForUser(Conference conf, float x, float y) {
Alexandre Lisiond5686032013-10-29 11:09:21 -0400438 Bubble contact_bubble = model.getUser();
Alexandre Lision40954dc2013-10-09 15:24:03 -0400439 if (contact_bubble != null) {
440 contact_bubble.attractor.set(x, y);
Alexandre Lisionb8add812013-10-24 11:42:42 -0400441 ((BubbleUser) contact_bubble).setConference(conf);
Alexandre Lisioncb2345c2013-12-09 15:39:13 -0500442
Alexandre Lision40954dc2013-10-09 15:24:03 -0400443 return contact_bubble;
444 }
445
Alexandre Lisioncb2345c2013-12-09 15:39:13 -0500446 contact_bubble = new BubbleUser(getActivity(), CallContact.ContactBuilder.buildUserContact(getActivity().getContentResolver()), conf, x, y,
447 BUBBLE_SIZE * 1.3f);
alision84813a12013-05-27 17:40:39 -0400448
Alexandre Lisionc9fca9e2013-11-08 15:21:18 -0500449 try {
450 ((BubbleUser) contact_bubble).setMute(mCallbacks.getService().isCaptureMuted());
451 } catch (RemoteException e) {
452 e.printStackTrace();
Alexandre Lisioncb2345c2013-12-09 15:39:13 -0500453 } catch (NullPointerException e1) {
Alexandre Lisionc9fca9e2013-11-08 15:21:18 -0500454 e1.printStackTrace();
455 }
alision1005ba12013-06-19 13:52:44 -0400456 model.addBubble(contact_bubble);
alision1005ba12013-06-19 13:52:44 -0400457 return contact_bubble;
458 }
Adrien Béraud0c9bd8f2013-05-30 16:16:57 +1000459
alision1005ba12013-06-19 13:52:44 -0400460 public void changeCallState(String callID, String newState) {
Alexandre Lisione4b95842013-11-12 10:36:51 -0500461 Log.i(TAG, "Call :" + callID + " " + newState);
alisiondf1dac92013-06-27 17:35:53 -0400462 if (newState.contentEquals("FAILURE")) {
463 try {
464 mCallbacks.getService().hangUp(callID);
465 } catch (RemoteException e) {
466 e.printStackTrace();
alision806e18e2013-06-21 15:30:17 -0400467 }
alision1005ba12013-06-19 13:52:44 -0400468 }
Alexandre Lisioncb2345c2013-12-09 15:39:13 -0500469 if (conf == null) {
Alexandre Lisione4b95842013-11-12 10:36:51 -0500470 return;
471 }
alisiondf1dac92013-06-27 17:35:53 -0400472 for (int i = 0; i < conf.getParticipants().size(); ++i) {
alisiondf1dac92013-06-27 17:35:53 -0400473 if (callID.equals(conf.getParticipants().get(i).getCallId())) {
474 if (newState.contentEquals("HUNGUP")) {
alisiondf1dac92013-06-27 17:35:53 -0400475 model.removeBubble(conf.getParticipants().get(i));
476 conf.getParticipants().remove(i);
477 } else {
478 conf.getParticipants().get(i).setCallState(newState);
479 }
480 }
481 }
482
Alexandre Lision4fb22622013-10-21 16:26:33 -0400483 if (conf.isOnGoing()) {
alisiondf1dac92013-06-27 17:35:53 -0400484 initNormalStateDisplay();
Alexandre Lision4fb22622013-10-21 16:26:33 -0400485 try {
486 updateCodecName(mCallbacks.getService().getCurrentAudioCodecName(callID));
487 } catch (RemoteException e) {
488 e.printStackTrace();
489 }
490 }
alisiondf1dac92013-06-27 17:35:53 -0400491
Alexandre Lisionccf0c932013-10-10 16:24:41 -0400492 if (conf.getParticipants().size() == 0) {
Alexandre Lisione4b95842013-11-12 10:36:51 -0500493 callStatusTxt.setText(newState);
Alexandre Lisiondafe9512013-10-28 16:31:41 -0400494 mCallbacks.terminateCall();
Alexandre Lisionccf0c932013-10-10 16:24:41 -0400495 }
alision1005ba12013-06-19 13:52:44 -0400496 }
alision84813a12013-05-27 17:40:39 -0400497
alision1005ba12013-06-19 13:52:44 -0400498 public boolean draggingBubble() {
499 return view == null ? false : view.isDraggingBubble();
500 }
Adrien Béraudc9c424d2013-05-30 17:47:35 +1000501
alision1005ba12013-06-19 13:52:44 -0400502 @Override
503 public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
Adrien Béraud13cde0b2013-05-30 20:27:20 +1000504
alisiondf1dac92013-06-27 17:35:53 -0400505 if (conf.getParticipants().size() == 1) {
alisiondf1dac92013-06-27 17:35:53 -0400506 if (conf.getParticipants().get(0).isIncoming() && conf.getParticipants().get(0).isRinging()) {
alision806e18e2013-06-21 15:30:17 -0400507 initIncomingCallDisplay();
508 } else {
alisiondf1dac92013-06-27 17:35:53 -0400509 if (conf.getParticipants().get(0).isRinging()) {
alision1005ba12013-06-19 13:52:44 -0400510 initOutGoingCallDisplay();
511 }
alision806e18e2013-06-21 15:30:17 -0400512 try {
alisiondf1dac92013-06-27 17:35:53 -0400513 if (conf.getParticipants().get(0).isOutGoing()
Alexandre Lisiona9ee4eb2014-01-15 16:20:35 -0500514 && mCallbacks.getService().getConference(conf.getId()) == null) {
alisiondf1dac92013-06-27 17:35:53 -0400515 mCallbacks.getService().placeCall(conf.getParticipants().get(0));
alision806e18e2013-06-21 15:30:17 -0400516 initOutGoingCallDisplay();
alisiondf1dac92013-06-27 17:35:53 -0400517 } else if (conf.getParticipants().get(0).isOutGoing() && conf.getParticipants().get(0).isRinging()) {
alision806e18e2013-06-21 15:30:17 -0400518 initOutGoingCallDisplay();
519 }
520 } catch (RemoteException e) {
521 Log.e(TAG, e.toString());
522 }
alision1005ba12013-06-19 13:52:44 -0400523 }
alisiondf1dac92013-06-27 17:35:53 -0400524 if (conf.getParticipants().get(0).isOngoing()) {
alision806e18e2013-06-21 15:30:17 -0400525 initNormalStateDisplay();
526 }
alisiondf1dac92013-06-27 17:35:53 -0400527 } else if (conf.getParticipants().size() > 1) {
alision1005ba12013-06-19 13:52:44 -0400528 initNormalStateDisplay();
529 }
alision1005ba12013-06-19 13:52:44 -0400530 }
Adrien Béraud13cde0b2013-05-30 20:27:20 +1000531
Alexandre Lision40954dc2013-10-09 15:24:03 -0400532 public void makeTransfer(BubbleContact contact) {
Alexandre Lision2b237922013-09-09 16:23:02 -0400533 FragmentManager fm = getFragmentManager();
Alexandre Lision0eb02032013-09-27 16:32:40 -0400534 editName = TransferDFragment.newInstance();
Alexandre Lision2b237922013-09-09 16:23:02 -0400535 Bundle b = new Bundle();
Alexandre Lision6ae652d2013-09-26 16:39:20 -0400536 try {
Alexandre Lisionb8add812013-10-24 11:42:42 -0400537 b.putParcelableArrayList("calls", (ArrayList<Conference>) mCallbacks.getService().getConcurrentCalls());
Alexandre Lision6ae652d2013-09-26 16:39:20 -0400538 b.putParcelable("call_selected", contact.associated_call);
539 editName.setArguments(b);
540 editName.setTargetFragment(this, REQUEST_TRANSFER);
541 editName.show(fm, "");
542 } catch (RemoteException e) {
543 Log.e(TAG, e.toString());
544 }
545
Alexandre Lision2b237922013-09-09 16:23:02 -0400546 }
547
alision1005ba12013-06-19 13:52:44 -0400548 @Override
549 public void surfaceCreated(SurfaceHolder holder) {
Alexandre Lisionc30e8412013-09-26 15:12:59 -0400550
alision1005ba12013-06-19 13:52:44 -0400551 }
Adrien Béraud13cde0b2013-05-30 20:27:20 +1000552
alision1005ba12013-06-19 13:52:44 -0400553 @Override
554 public void surfaceDestroyed(SurfaceHolder holder) {
Alexandre Lision84208a32013-09-25 13:18:37 -0400555 // check that soft input is hidden
556 InputMethodManager lManager = (InputMethodManager) getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
557 lManager.hideSoftInputFromWindow(view.getWindowToken(), 0);
Alexandre Lisionb8add812013-10-24 11:42:42 -0400558 if (editName != null && editName.isVisible()) {
Alexandre Lision0eb02032013-09-27 16:32:40 -0400559 editName.dismiss();
560 }
alision1005ba12013-06-19 13:52:44 -0400561 }
Adrien Béraud13cde0b2013-05-30 20:27:20 +1000562
alisione38001f2013-06-04 14:14:39 -0400563 public BubblesView getBubbleView() {
564 return view;
alision1005ba12013-06-19 13:52:44 -0400565 }
Alexandre Lision0c384512013-09-17 17:15:57 -0400566
567 public void updateTime() {
Alexandre Lision945e4612014-01-15 17:40:31 -0500568 long duration = System.currentTimeMillis() / 1000 - this.conf.getParticipants().get(0).getTimestampStart_();
Alexandre Lisioncb2345c2013-12-09 15:39:13 -0500569 if (conf.isOnGoing())
Alexandre Lisione4b95842013-11-12 10:36:51 -0500570 callStatusTxt.setText(String.format("%d:%02d:%02d", duration / 3600, duration % 3600 / 60, duration % 60));
Alexandre Lisionf1850c02013-09-23 14:19:34 -0400571 }
572
Alexandre Lision0edf18c2013-09-23 17:35:50 -0400573 public Conference getConference() {
574 return conf;
575 }
Alexandre Lision64dc8c02013-09-25 15:32:25 -0400576
577 public void onKeyUp(int keyCode, KeyEvent event) {
578 try {
Alexandre Lisionb8add812013-10-24 11:42:42 -0400579
Alexandre Lisionfda4d1d2013-10-25 15:29:53 -0400580 switch (keyCode) {
581 case KeyEvent.KEYCODE_VOLUME_DOWN:
582 case KeyEvent.KEYCODE_VOLUME_UP:
583 break;
584 default:
585 String toSend = Character.toString(event.getDisplayLabel());
586 toSend.toUpperCase(Locale.getDefault());
587 Log.d(TAG, "toSend " + toSend);
588 mCallbacks.getService().playDtmf(toSend);
589 break;
590 }
Alexandre Lision64dc8c02013-09-25 15:32:25 -0400591 } catch (RemoteException e) {
592 e.printStackTrace();
593 }
594 }
Alexandre Lision4fb22622013-10-21 16:26:33 -0400595
596 public void updateCodecName(String currentAudioCodecName) {
Alexandre Lisionf30ff852013-12-09 17:08:40 -0500597 // In case of multiple codec in the String
Alexandre Lisioncb2345c2013-12-09 15:39:13 -0500598 if (currentAudioCodecName.indexOf(' ') > 0)
599 codecNameTxt.setText(currentAudioCodecName.subSequence(0, currentAudioCodecName.indexOf(' ')));
600 else
601 codecNameTxt.setText(currentAudioCodecName);
Alexandre Lision4fb22622013-10-21 16:26:33 -0400602 }
alision84813a12013-05-27 17:40:39 -0400603}