blob: 56cbba16fdd3b3afd14f6d8fba2b848d791c086b [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 Lision064e1e02013-10-01 16:18:42 -040037import org.sflphone.R;
38import org.sflphone.model.Attractor;
39import org.sflphone.model.Bubble;
Alexandre Lision40954dc2013-10-09 15:24:03 -040040import org.sflphone.model.BubbleContact;
Alexandre Lision064e1e02013-10-01 16:18:42 -040041import org.sflphone.model.BubbleModel;
Alexandre Lision40954dc2013-10-09 15:24:03 -040042import org.sflphone.model.BubbleUser;
Alexandre Lision064e1e02013-10-01 16:18:42 -040043import org.sflphone.model.BubblesView;
Alexandre Lision68855472013-10-10 16:20:46 -040044import org.sflphone.model.CallContact;
Alexandre Lision064e1e02013-10-01 16:18:42 -040045import org.sflphone.model.Conference;
46import org.sflphone.model.SipCall;
47import org.sflphone.service.ISipService;
48
alision84813a12013-05-27 17:40:39 -040049import android.app.Activity;
50import android.app.Fragment;
Alexandre Lision2b237922013-09-09 16:23:02 -040051import android.app.FragmentManager;
Alexandre Lisionf1850c02013-09-23 14:19:34 -040052import android.content.Context;
Alexandre Lision2b237922013-09-09 16:23:02 -040053import android.content.Intent;
alision84813a12013-05-27 17:40:39 -040054import android.graphics.Bitmap;
Adrien Béraud0c9bd8f2013-05-30 16:16:57 +100055import android.graphics.BitmapFactory;
alision84813a12013-05-27 17:40:39 -040056import android.graphics.PointF;
57import android.os.Bundle;
Alexandre Lisionf02190d2013-12-12 17:26:12 -050058import android.os.PowerManager;
alision84813a12013-05-27 17:40:39 -040059import android.os.RemoteException;
Alexandre Lisionf02190d2013-12-12 17:26:12 -050060import android.os.PowerManager.WakeLock;
alision84813a12013-05-27 17:40:39 -040061import android.util.Log;
Alexandre Lision64dc8c02013-09-25 15:32:25 -040062import android.view.KeyEvent;
alision84813a12013-05-27 17:40:39 -040063import android.view.LayoutInflater;
Alexandre Lisiond588bff2013-10-08 12:43:01 -040064import android.view.Menu;
65import android.view.MenuInflater;
66import android.view.MenuItem;
Adrien Béraud13cde0b2013-05-30 20:27:20 +100067import android.view.SurfaceHolder;
68import android.view.SurfaceHolder.Callback;
alision84813a12013-05-27 17:40:39 -040069import android.view.View;
Alexandre Lision84208a32013-09-25 13:18:37 -040070import android.view.View.OnClickListener;
alision84813a12013-05-27 17:40:39 -040071import android.view.ViewGroup;
Alexandre Lision84208a32013-09-25 13:18:37 -040072import android.view.inputmethod.InputMethodManager;
Alexandre Lision35577132013-12-06 15:21:15 -050073import android.widget.CompoundButton;
74import android.widget.CompoundButton.OnCheckedChangeListener;
Alexandre Lision84208a32013-09-25 13:18:37 -040075import android.widget.ImageButton;
Alexandre Lision3c6b7102013-09-16 16:56:46 -040076import android.widget.TextView;
Alexandre Lision35577132013-12-06 15:21:15 -050077import android.widget.ToggleButton;
alision84813a12013-05-27 17:40:39 -040078
Alexandre Lisionfda4d1d2013-10-25 15:29:53 -040079public class CallFragment extends Fragment implements Callback {
alision84813a12013-05-27 17:40:39 -040080
alision1005ba12013-06-19 13:52:44 -040081 static final String TAG = "CallFragment";
alision84813a12013-05-27 17:40:39 -040082
alisionf57d8a62013-07-02 09:37:12 -040083 float BUBBLE_SIZE = 75;
alision1005ba12013-06-19 13:52:44 -040084 static final float ATTRACTOR_SIZE = 40;
alision84813a12013-05-27 17:40:39 -040085
Alexandre Lision2b237922013-09-09 16:23:02 -040086 public static final int REQUEST_TRANSFER = 10;
87
alisiondf1dac92013-06-27 17:35:53 -040088 private Conference conf;
alision84813a12013-05-27 17:40:39 -040089
Alexandre Lision3c6b7102013-09-16 16:56:46 -040090 private TextView callStatusTxt;
Alexandre Lisionb8add812013-10-24 11:42:42 -040091 private TextView codecNameTxt;
Alexandre Lisioncb2345c2013-12-09 15:39:13 -050092
Alexandre Lision35577132013-12-06 15:21:15 -050093 private ToggleButton speakers;
Alexandre Lisionb8add812013-10-24 11:42:42 -040094
Alexandre Lisionf02190d2013-12-12 17:26:12 -050095 private PowerManager powerManager;
96 // Screen wake lock for incoming call
97 private WakeLock wakeLock;
98
alision1005ba12013-06-19 13:52:44 -040099 private BubblesView view;
100 private BubbleModel model;
alision84813a12013-05-27 17:40:39 -0400101
Alexandre Lision68855472013-10-10 16:20:46 -0400102 public Callbacks mCallbacks = sDummyCallbacks;
Alexandre Lisiona764c682013-09-09 10:02:07 -0400103 boolean accepted = false;
alision1005ba12013-06-19 13:52:44 -0400104 private Bitmap call_icon;
alision85704182013-05-29 15:23:03 -0400105
Alexandre Lision0eb02032013-09-27 16:32:40 -0400106 TransferDFragment editName;
Alexandre Lisionf1850c02013-09-23 14:19:34 -0400107
alision1005ba12013-06-19 13:52:44 -0400108 @Override
109 public void onCreate(Bundle savedBundle) {
110 super.onCreate(savedBundle);
Alexandre Lisiondafe9512013-10-28 16:31:41 -0400111 conf = new Conference((Conference) getArguments().getParcelable("conference"));
alisiondf1dac92013-06-27 17:35:53 -0400112 model = new BubbleModel(getResources().getDisplayMetrics().density);
alision729b0a22013-07-02 11:57:33 -0400113 BUBBLE_SIZE = getResources().getDimension(R.dimen.bubble_size);
Alexandre Lisiona764c682013-09-09 10:02:07 -0400114 Log.e(TAG, "BUBBLE_SIZE " + BUBBLE_SIZE);
Alexandre Lisiond588bff2013-10-08 12:43:01 -0400115 this.setHasOptionsMenu(true);
alision729b0a22013-07-02 11:57:33 -0400116
Alexandre Lisionf02190d2013-12-12 17:26:12 -0500117 powerManager = (PowerManager) getActivity().getSystemService(Context.POWER_SERVICE);
118 wakeLock = powerManager.newWakeLock(PowerManager.SCREEN_DIM_WAKE_LOCK | PowerManager.ACQUIRE_CAUSES_WAKEUP | PowerManager.ON_AFTER_RELEASE,
119 "org.sflphone.onIncomingCall");
120 wakeLock.setReferenceCounted(false);
121
122 Log.d(TAG, "Acquire wake up lock");
123 if (wakeLock != null && !wakeLock.isHeld()) {
124 wakeLock.acquire();
125 }
126
alision1005ba12013-06-19 13:52:44 -0400127 }
alision85992112013-05-29 12:18:08 -0400128
alision1005ba12013-06-19 13:52:44 -0400129 /**
130 * A dummy implementation of the {@link Callbacks} interface that does nothing. Used only when this fragment is not attached to an activity.
131 */
132 private static Callbacks sDummyCallbacks = new Callbacks() {
alision85704182013-05-29 15:23:03 -0400133
alision1005ba12013-06-19 13:52:44 -0400134 @Override
135 public ISipService getService() {
136 return null;
137 }
alisiondf1dac92013-06-27 17:35:53 -0400138
Alexandre Lisionccf0c932013-10-10 16:24:41 -0400139 @Override
Alexandre Lisiondafe9512013-10-28 16:31:41 -0400140 public void terminateCall() {
Alexandre Lisionccf0c932013-10-10 16:24:41 -0400141 }
Alexandre Lision0c384512013-09-17 17:15:57 -0400142
143 @Override
Adrien Béraude78d06f2013-09-19 13:33:44 +1000144 public void startTimer() {
Alexandre Lision0c384512013-09-17 17:15:57 -0400145 }
Alexandre Lision68855472013-10-10 16:20:46 -0400146
147 @Override
Alexandre Lisionb8add812013-10-24 11:42:42 -0400148 public void slideChatScreen() {
Alexandre Lision68855472013-10-10 16:20:46 -0400149 }
alision1005ba12013-06-19 13:52:44 -0400150 };
alision907bde72013-06-20 14:40:37 -0400151
alision1005ba12013-06-19 13:52:44 -0400152 /**
153 * The Activity calling this fragment has to implement this interface
154 *
155 */
156 public interface Callbacks {
alision85992112013-05-29 12:18:08 -0400157
alision1005ba12013-06-19 13:52:44 -0400158 public ISipService getService();
alision85704182013-05-29 15:23:03 -0400159
Alexandre Lision0c384512013-09-17 17:15:57 -0400160 public void startTimer();
Alexandre Lision68855472013-10-10 16:20:46 -0400161
162 public void slideChatScreen();
Alexandre Lisionb8add812013-10-24 11:42:42 -0400163
Alexandre Lisiondafe9512013-10-28 16:31:41 -0400164 public void terminateCall();
alision1005ba12013-06-19 13:52:44 -0400165 }
alision85992112013-05-29 12:18:08 -0400166
alision1005ba12013-06-19 13:52:44 -0400167 @Override
168 public void onAttach(Activity activity) {
169 super.onAttach(activity);
alision85992112013-05-29 12:18:08 -0400170
alision1005ba12013-06-19 13:52:44 -0400171 if (!(activity instanceof Callbacks)) {
172 throw new IllegalStateException("Activity must implement fragment's callbacks.");
173 }
alision85992112013-05-29 12:18:08 -0400174
alision1005ba12013-06-19 13:52:44 -0400175 // rootView.requestDisallowInterceptTouchEvent(true);
alision85992112013-05-29 12:18:08 -0400176
alision1005ba12013-06-19 13:52:44 -0400177 mCallbacks = (Callbacks) activity;
Alexandre Lisionb8add812013-10-24 11:42:42 -0400178 // myself = SipCall.SipCallBuilder.buildMyselfCall(activity.getContentResolver(), "Me");
Adrien Béraude78d06f2013-09-19 13:33:44 +1000179
alision1005ba12013-06-19 13:52:44 -0400180 }
Alexandre Lisionb8add812013-10-24 11:42:42 -0400181
Alexandre Lisiond588bff2013-10-08 12:43:01 -0400182 @Override
183 public void onCreateOptionsMenu(Menu m, MenuInflater inf) {
184 super.onCreateOptionsMenu(m, inf);
Alexandre Lision68855472013-10-10 16:20:46 -0400185 inf.inflate(R.menu.ac_call, m);
Alexandre Lisiond588bff2013-10-08 12:43:01 -0400186 }
187
188 @Override
189 public boolean onOptionsItemSelected(MenuItem item) {
190 super.onOptionsItemSelected(item);
Alexandre Lision666b3772013-10-28 17:42:48 -0400191 switch (item.getItemId()) {
192 case R.id.menuitem_chat:
193 mCallbacks.slideChatScreen();
194 break;
195 }
Alexandre Lisiond588bff2013-10-08 12:43:01 -0400196
197 return true;
198 }
alision85992112013-05-29 12:18:08 -0400199
alision1005ba12013-06-19 13:52:44 -0400200 @Override
201 public void onDetach() {
202 super.onDetach();
203 mCallbacks = sDummyCallbacks;
alision1005ba12013-06-19 13:52:44 -0400204 }
alision907bde72013-06-20 14:40:37 -0400205
alision1005ba12013-06-19 13:52:44 -0400206 @Override
alision907bde72013-06-20 14:40:37 -0400207 public void onStop() {
alision1005ba12013-06-19 13:52:44 -0400208 super.onStop();
209 }
alision85992112013-05-29 12:18:08 -0400210
alision1005ba12013-06-19 13:52:44 -0400211 @Override
Alexandre Lisionf1850c02013-09-23 14:19:34 -0400212 public void onResume() {
213 super.onResume();
Alexandre Lisionf1850c02013-09-23 14:19:34 -0400214 }
215
216 @Override
217 public void onPause() {
218 super.onPause();
Alexandre Lisionf02190d2013-12-12 17:26:12 -0500219 if (wakeLock != null && wakeLock.isHeld()) {
220 wakeLock.release();
221 }
222
Alexandre Lisionf1850c02013-09-23 14:19:34 -0400223 }
224
225 @Override
Alexandre Lision2b237922013-09-09 16:23:02 -0400226 public void onActivityResult(int requestCode, int resultCode, Intent data) {
227 super.onActivityResult(requestCode, resultCode, data);
228 SipCall transfer = null;
229 if (requestCode == REQUEST_TRANSFER) {
230 switch (resultCode) {
Adrien Béraude78d06f2013-09-19 13:33:44 +1000231 case TransferDFragment.RESULT_TRANSFER_CONF:
Alexandre Lision2b237922013-09-09 16:23:02 -0400232 Conference c = data.getParcelableExtra("target");
233 transfer = data.getParcelableExtra("transfer");
234 try {
235
236 mCallbacks.getService().attendedTransfer(transfer.getCallId(), c.getParticipants().get(0).getCallId());
237
238 } catch (RemoteException e) {
Alexandre Lision2b237922013-09-09 16:23:02 -0400239 e.printStackTrace();
240 }
Alexandre Lision2b237922013-09-09 16:23:02 -0400241 break;
242
Adrien Béraude78d06f2013-09-19 13:33:44 +1000243 case TransferDFragment.RESULT_TRANSFER_NUMBER:
Alexandre Lision2b237922013-09-09 16:23:02 -0400244 String to = data.getStringExtra("to_number");
245 transfer = data.getParcelableExtra("transfer");
246 try {
Alexandre Lision2b237922013-09-09 16:23:02 -0400247 mCallbacks.getService().transfer(transfer.getCallId(), to);
248 mCallbacks.getService().hangUp(transfer.getCallId());
249 } catch (RemoteException e) {
Alexandre Lision2b237922013-09-09 16:23:02 -0400250 e.printStackTrace();
251 }
252 break;
Adrien Béraud97512842013-09-19 13:37:19 +1000253 case Activity.RESULT_CANCELED:
Alexandre Lision2b237922013-09-09 16:23:02 -0400254 default:
Adrien Béraude78d06f2013-09-19 13:33:44 +1000255 model.clear();
256 initNormalStateDisplay();
Alexandre Lision2b237922013-09-09 16:23:02 -0400257 break;
258 }
259 }
260 }
261
262 @Override
alision1005ba12013-06-19 13:52:44 -0400263 public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
Alexandre Lision84208a32013-09-25 13:18:37 -0400264 final ViewGroup rootView = (ViewGroup) inflater.inflate(R.layout.frag_call, container, false);
alision85992112013-05-29 12:18:08 -0400265
alision1005ba12013-06-19 13:52:44 -0400266 view = (BubblesView) rootView.findViewById(R.id.main_view);
alision34673e62013-06-25 14:40:07 -0400267 view.setFragment(this);
alision1005ba12013-06-19 13:52:44 -0400268 view.setModel(model);
269 view.getHolder().addCallback(this);
alision84813a12013-05-27 17:40:39 -0400270
Alexandre Lision4fb22622013-10-21 16:26:33 -0400271 codecNameTxt = (TextView) rootView.findViewById(R.id.codec_name_txt);
Alexandre Lision3c6b7102013-09-16 16:56:46 -0400272 callStatusTxt = (TextView) rootView.findViewById(R.id.call_status_txt);
Alexandre Lision68855472013-10-10 16:20:46 -0400273 call_icon = BitmapFactory.decodeResource(getResources(), R.drawable.ic_action_call);
Alexandre Lisioncb2345c2013-12-09 15:39:13 -0500274
Alexandre Lisionbaeeb212013-12-09 12:54:47 -0500275 speakers = (ToggleButton) rootView.findViewById(R.id.speaker_toggle);
Alexandre Lisioncb2345c2013-12-09 15:39:13 -0500276
Alexandre Lision35577132013-12-06 15:21:15 -0500277 speakers.setOnCheckedChangeListener(new OnCheckedChangeListener() {
Alexandre Lisioncb2345c2013-12-09 15:39:13 -0500278
Alexandre Lision35577132013-12-06 15:21:15 -0500279 @Override
280 public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
281 try {
282 mCallbacks.getService().toggleSpeakerPhone(isChecked);
283 } catch (RemoteException e) {
284 e.printStackTrace();
285 }
Alexandre Lisioncb2345c2013-12-09 15:39:13 -0500286
Alexandre Lision35577132013-12-06 15:21:15 -0500287 }
288 });
alision84813a12013-05-27 17:40:39 -0400289
Alexandre Lision84208a32013-09-25 13:18:37 -0400290 ((ImageButton) rootView.findViewById(R.id.dialpad_btn)).setOnClickListener(new OnClickListener() {
291
292 @Override
293 public void onClick(View v) {
294 InputMethodManager lManager = (InputMethodManager) getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
295 lManager.toggleSoftInput(InputMethodManager.SHOW_IMPLICIT, InputMethodManager.HIDE_IMPLICIT_ONLY);
296 }
297 });
298
alision1005ba12013-06-19 13:52:44 -0400299 return rootView;
300 }
Alexandre Lisionc30e8412013-09-26 15:12:59 -0400301
alision1005ba12013-06-19 13:52:44 -0400302 private void initNormalStateDisplay() {
303 Log.i(TAG, "Start normal display");
alision84813a12013-05-27 17:40:39 -0400304
Alexandre Lision0c384512013-09-17 17:15:57 -0400305 mCallbacks.startTimer();
alision84813a12013-05-27 17:40:39 -0400306
Alexandre Lisionee2494d2013-10-09 17:14:00 -0400307 getBubbleForUser(conf, model.width / 2, model.height / 2);
alision907bde72013-06-20 14:40:37 -0400308
alisiondf1dac92013-06-27 17:35:53 -0400309 int angle_part = 360 / conf.getParticipants().size();
alision806e18e2013-06-21 15:30:17 -0400310 double dX = 0;
311 double dY = 0;
alision465ceba2013-07-04 09:24:30 -0400312 int radiusCalls = (int) (model.width / 2 - BUBBLE_SIZE);
alisiondf1dac92013-06-27 17:35:53 -0400313 for (int i = 0; i < conf.getParticipants().size(); ++i) {
314
315 if (conf.getParticipants().get(i) == null) {
alisiondf1dac92013-06-27 17:35:53 -0400316 continue;
317 }
alision34673e62013-06-25 14:40:07 -0400318 dX = Math.cos(Math.toRadians(angle_part * i - 90)) * radiusCalls;
319 dY = Math.sin(Math.toRadians(angle_part * i - 90)) * radiusCalls;
alisiondf1dac92013-06-27 17:35:53 -0400320 getBubbleFor(conf.getParticipants().get(i), (int) (model.width / 2 + dX), (int) (model.height / 2 + dY));
alision806e18e2013-06-21 15:30:17 -0400321 }
alision1005ba12013-06-19 13:52:44 -0400322
323 model.clearAttractors();
alision1005ba12013-06-19 13:52:44 -0400324 }
alision85704182013-05-29 15:23:03 -0400325
alision1005ba12013-06-19 13:52:44 -0400326 private void initIncomingCallDisplay() {
327 Log.i(TAG, "Start incoming display");
alision84813a12013-05-27 17:40:39 -0400328
Alexandre Lision0c384512013-09-17 17:15:57 -0400329 mCallbacks.startTimer();
alision84813a12013-05-27 17:40:39 -0400330
Alexandre Lision23628c12013-09-24 11:17:05 -0400331 int radiusCalls = (int) (model.width / 2 - BUBBLE_SIZE);
Alexandre Lisionee2494d2013-10-09 17:14:00 -0400332 getBubbleForUser(conf, model.width / 2, model.height / 2 + radiusCalls);
Alexandre Lision23628c12013-09-24 11:17:05 -0400333 getBubbleFor(conf.getParticipants().get(0), model.width / 2, model.height / 2 - radiusCalls);
alision85704182013-05-29 15:23:03 -0400334
alision1005ba12013-06-19 13:52:44 -0400335 model.clearAttractors();
Alexandre Lision23628c12013-09-24 11:17:05 -0400336 model.addAttractor(new Attractor(new PointF(model.width / 2, model.height / 2), ATTRACTOR_SIZE, new Attractor.Callback() {
alision1005ba12013-06-19 13:52:44 -0400337 @Override
338 public boolean onBubbleSucked(Bubble b) {
Alexandre Lisiona764c682013-09-09 10:02:07 -0400339
340 if (!accepted) {
Alexandre Lision68855472013-10-10 16:20:46 -0400341 try {
342 mCallbacks.getService().accept(b.getCallID());
343 } catch (RemoteException e) {
344 e.printStackTrace();
345 }
Alexandre Lisiona764c682013-09-09 10:02:07 -0400346 accepted = true;
347 }
alision1005ba12013-06-19 13:52:44 -0400348 return false;
349 }
350 }, call_icon));
Alexandre Lisioncb2345c2013-12-09 15:39:13 -0500351
352 if (conf.getParticipants().get(0).getAccount().isAutoanswerEnabled()) {
Alexandre Lision332f4912013-11-12 13:04:32 -0500353 try {
354 mCallbacks.getService().accept(conf.getParticipants().get(0).getCallId());
355 } catch (RemoteException e) {
356 e.printStackTrace();
357 }
358 }
alision1005ba12013-06-19 13:52:44 -0400359 }
alision85704182013-05-29 15:23:03 -0400360
alision1005ba12013-06-19 13:52:44 -0400361 private void initOutGoingCallDisplay() {
362 Log.i(TAG, "Start outgoing display");
alision85704182013-05-29 15:23:03 -0400363
Alexandre Lision0c384512013-09-17 17:15:57 -0400364 mCallbacks.startTimer();
alision85704182013-05-29 15:23:03 -0400365
Alexandre Lisionee2494d2013-10-09 17:14:00 -0400366 getBubbleForUser(conf, model.width / 2, model.height / 2);
alision85704182013-05-29 15:23:03 -0400367
alisiondf1dac92013-06-27 17:35:53 -0400368 // TODO off-thread image loading
369 int angle_part = 360 / conf.getParticipants().size();
370 double dX = 0;
371 double dY = 0;
Adrien Béraude78d06f2013-09-19 13:33:44 +1000372 int radiusCalls = (int) (model.width / 2 - BUBBLE_SIZE);
alisiondf1dac92013-06-27 17:35:53 -0400373 for (int i = 0; i < conf.getParticipants().size(); ++i) {
374 dX = Math.cos(Math.toRadians(angle_part * i - 90)) * radiusCalls;
375 dY = Math.sin(Math.toRadians(angle_part * i - 90)) * radiusCalls;
376 getBubbleFor(conf.getParticipants().get(i), (int) (model.width / 2 + dX), (int) (model.height / 2 + dY));
377 }
alision907bde72013-06-20 14:40:37 -0400378
alision1005ba12013-06-19 13:52:44 -0400379 model.clearAttractors();
alision1005ba12013-06-19 13:52:44 -0400380 }
alision85704182013-05-29 15:23:03 -0400381
alision1005ba12013-06-19 13:52:44 -0400382 /**
383 * Retrieves or create a bubble for a given contact. If the bubble exists, it is moved to the new location.
384 *
alision806e18e2013-06-21 15:30:17 -0400385 * @param call
386 * The call associated to a contact
alision1005ba12013-06-19 13:52:44 -0400387 * @param x
388 * Initial or new x position.
389 * @param y
390 * Initial or new y position.
391 * @return Bubble corresponding to the contact.
392 */
alision806e18e2013-06-21 15:30:17 -0400393 private Bubble getBubbleFor(SipCall call, float x, float y) {
Alexandre Lisionee2494d2013-10-09 17:14:00 -0400394 Bubble contact_bubble = model.getBubble(call.getCallId());
alision1005ba12013-06-19 13:52:44 -0400395 if (contact_bubble != null) {
Alexandre Lisionb8add812013-10-24 11:42:42 -0400396 ((BubbleContact) contact_bubble).setCall(call);
alision1005ba12013-06-19 13:52:44 -0400397 contact_bubble.attractor.set(x, y);
398 return contact_bubble;
399 }
alision84813a12013-05-27 17:40:39 -0400400
Alexandre Lision40954dc2013-10-09 15:24:03 -0400401 contact_bubble = new BubbleContact(getActivity(), call, x, y, BUBBLE_SIZE);
402
403 model.addBubble(contact_bubble);
404 return contact_bubble;
405 }
Alexandre Lisionb8add812013-10-24 11:42:42 -0400406
Alexandre Lisionee2494d2013-10-09 17:14:00 -0400407 private Bubble getBubbleForUser(Conference conf, float x, float y) {
Alexandre Lisiond5686032013-10-29 11:09:21 -0400408 Bubble contact_bubble = model.getUser();
Alexandre Lision40954dc2013-10-09 15:24:03 -0400409 if (contact_bubble != null) {
410 contact_bubble.attractor.set(x, y);
Alexandre Lisionb8add812013-10-24 11:42:42 -0400411 ((BubbleUser) contact_bubble).setConference(conf);
Alexandre Lisioncb2345c2013-12-09 15:39:13 -0500412
Alexandre Lision40954dc2013-10-09 15:24:03 -0400413 return contact_bubble;
414 }
415
Alexandre Lisioncb2345c2013-12-09 15:39:13 -0500416 contact_bubble = new BubbleUser(getActivity(), CallContact.ContactBuilder.buildUserContact(getActivity().getContentResolver()), conf, x, y,
417 BUBBLE_SIZE * 1.3f);
alision84813a12013-05-27 17:40:39 -0400418
Alexandre Lisionc9fca9e2013-11-08 15:21:18 -0500419 try {
420 ((BubbleUser) contact_bubble).setMute(mCallbacks.getService().isCaptureMuted());
421 } catch (RemoteException e) {
422 e.printStackTrace();
Alexandre Lisioncb2345c2013-12-09 15:39:13 -0500423 } catch (NullPointerException e1) {
Alexandre Lisionc9fca9e2013-11-08 15:21:18 -0500424 e1.printStackTrace();
425 }
alision1005ba12013-06-19 13:52:44 -0400426 model.addBubble(contact_bubble);
alision1005ba12013-06-19 13:52:44 -0400427 return contact_bubble;
428 }
Adrien Béraud0c9bd8f2013-05-30 16:16:57 +1000429
alision1005ba12013-06-19 13:52:44 -0400430 public void changeCallState(String callID, String newState) {
Alexandre Lisione4b95842013-11-12 10:36:51 -0500431 Log.i(TAG, "Call :" + callID + " " + newState);
alisiondf1dac92013-06-27 17:35:53 -0400432 if (newState.contentEquals("FAILURE")) {
433 try {
434 mCallbacks.getService().hangUp(callID);
435 } catch (RemoteException e) {
436 e.printStackTrace();
alision806e18e2013-06-21 15:30:17 -0400437 }
alision1005ba12013-06-19 13:52:44 -0400438 }
Alexandre Lisioncb2345c2013-12-09 15:39:13 -0500439 if (conf == null) {
Alexandre Lisione4b95842013-11-12 10:36:51 -0500440 return;
441 }
alisiondf1dac92013-06-27 17:35:53 -0400442 for (int i = 0; i < conf.getParticipants().size(); ++i) {
alisiondf1dac92013-06-27 17:35:53 -0400443 if (callID.equals(conf.getParticipants().get(i).getCallId())) {
444 if (newState.contentEquals("HUNGUP")) {
alisiondf1dac92013-06-27 17:35:53 -0400445 model.removeBubble(conf.getParticipants().get(i));
446 conf.getParticipants().remove(i);
447 } else {
448 conf.getParticipants().get(i).setCallState(newState);
449 }
450 }
451 }
452
Alexandre Lision4fb22622013-10-21 16:26:33 -0400453 if (conf.isOnGoing()) {
alisiondf1dac92013-06-27 17:35:53 -0400454 initNormalStateDisplay();
Alexandre Lision4fb22622013-10-21 16:26:33 -0400455 try {
456 updateCodecName(mCallbacks.getService().getCurrentAudioCodecName(callID));
457 } catch (RemoteException e) {
458 e.printStackTrace();
459 }
460 }
alisiondf1dac92013-06-27 17:35:53 -0400461
Alexandre Lisionccf0c932013-10-10 16:24:41 -0400462 if (conf.getParticipants().size() == 0) {
Alexandre Lisione4b95842013-11-12 10:36:51 -0500463 callStatusTxt.setText(newState);
Alexandre Lisiondafe9512013-10-28 16:31:41 -0400464 mCallbacks.terminateCall();
Alexandre Lisionccf0c932013-10-10 16:24:41 -0400465 }
alision1005ba12013-06-19 13:52:44 -0400466 }
alision84813a12013-05-27 17:40:39 -0400467
alision1005ba12013-06-19 13:52:44 -0400468 public boolean draggingBubble() {
469 return view == null ? false : view.isDraggingBubble();
470 }
Adrien Béraudc9c424d2013-05-30 17:47:35 +1000471
alision1005ba12013-06-19 13:52:44 -0400472 @Override
473 public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
Adrien Béraud13cde0b2013-05-30 20:27:20 +1000474
alisiondf1dac92013-06-27 17:35:53 -0400475 if (conf.getParticipants().size() == 1) {
alisiondf1dac92013-06-27 17:35:53 -0400476 if (conf.getParticipants().get(0).isIncoming() && conf.getParticipants().get(0).isRinging()) {
alision806e18e2013-06-21 15:30:17 -0400477 initIncomingCallDisplay();
478 } else {
alisiondf1dac92013-06-27 17:35:53 -0400479 if (conf.getParticipants().get(0).isRinging()) {
alision1005ba12013-06-19 13:52:44 -0400480 initOutGoingCallDisplay();
481 }
alision806e18e2013-06-21 15:30:17 -0400482 try {
alisiondf1dac92013-06-27 17:35:53 -0400483 if (conf.getParticipants().get(0).isOutGoing()
484 && mCallbacks.getService().getCall(conf.getParticipants().get(0).getCallId()) == null) {
485 mCallbacks.getService().placeCall(conf.getParticipants().get(0));
alision806e18e2013-06-21 15:30:17 -0400486 initOutGoingCallDisplay();
alisiondf1dac92013-06-27 17:35:53 -0400487 } else if (conf.getParticipants().get(0).isOutGoing() && conf.getParticipants().get(0).isRinging()) {
alision806e18e2013-06-21 15:30:17 -0400488 initOutGoingCallDisplay();
489 }
490 } catch (RemoteException e) {
491 Log.e(TAG, e.toString());
492 }
alision1005ba12013-06-19 13:52:44 -0400493 }
alisiondf1dac92013-06-27 17:35:53 -0400494 if (conf.getParticipants().get(0).isOngoing()) {
alision806e18e2013-06-21 15:30:17 -0400495 initNormalStateDisplay();
496 }
alisiondf1dac92013-06-27 17:35:53 -0400497 } else if (conf.getParticipants().size() > 1) {
alision1005ba12013-06-19 13:52:44 -0400498 initNormalStateDisplay();
499 }
alision1005ba12013-06-19 13:52:44 -0400500 }
Adrien Béraud13cde0b2013-05-30 20:27:20 +1000501
Alexandre Lision40954dc2013-10-09 15:24:03 -0400502 public void makeTransfer(BubbleContact contact) {
Alexandre Lision2b237922013-09-09 16:23:02 -0400503 FragmentManager fm = getFragmentManager();
Alexandre Lision0eb02032013-09-27 16:32:40 -0400504 editName = TransferDFragment.newInstance();
Alexandre Lision2b237922013-09-09 16:23:02 -0400505 Bundle b = new Bundle();
Alexandre Lision6ae652d2013-09-26 16:39:20 -0400506 try {
Alexandre Lisionb8add812013-10-24 11:42:42 -0400507 b.putParcelableArrayList("calls", (ArrayList<Conference>) mCallbacks.getService().getConcurrentCalls());
Alexandre Lision6ae652d2013-09-26 16:39:20 -0400508 b.putParcelable("call_selected", contact.associated_call);
509 editName.setArguments(b);
510 editName.setTargetFragment(this, REQUEST_TRANSFER);
511 editName.show(fm, "");
512 } catch (RemoteException e) {
513 Log.e(TAG, e.toString());
514 }
515
Alexandre Lision2b237922013-09-09 16:23:02 -0400516 }
517
alision1005ba12013-06-19 13:52:44 -0400518 @Override
519 public void surfaceCreated(SurfaceHolder holder) {
Alexandre Lisionc30e8412013-09-26 15:12:59 -0400520
alision1005ba12013-06-19 13:52:44 -0400521 }
Adrien Béraud13cde0b2013-05-30 20:27:20 +1000522
alision1005ba12013-06-19 13:52:44 -0400523 @Override
524 public void surfaceDestroyed(SurfaceHolder holder) {
Alexandre Lision84208a32013-09-25 13:18:37 -0400525 // check that soft input is hidden
526 InputMethodManager lManager = (InputMethodManager) getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
527 lManager.hideSoftInputFromWindow(view.getWindowToken(), 0);
Alexandre Lisionb8add812013-10-24 11:42:42 -0400528 if (editName != null && editName.isVisible()) {
Alexandre Lision0eb02032013-09-27 16:32:40 -0400529 editName.dismiss();
530 }
alision1005ba12013-06-19 13:52:44 -0400531 }
Adrien Béraud13cde0b2013-05-30 20:27:20 +1000532
alisione38001f2013-06-04 14:14:39 -0400533 public BubblesView getBubbleView() {
534 return view;
alision1005ba12013-06-19 13:52:44 -0400535 }
Alexandre Lision0c384512013-09-17 17:15:57 -0400536
537 public void updateTime() {
Adrien Béraude78d06f2013-09-19 13:33:44 +1000538 long duration = System.currentTimeMillis() / 1000 - this.conf.getParticipants().get(0).getTimestamp_start();
Alexandre Lisioncb2345c2013-12-09 15:39:13 -0500539 if (conf.isOnGoing())
Alexandre Lisione4b95842013-11-12 10:36:51 -0500540 callStatusTxt.setText(String.format("%d:%02d:%02d", duration / 3600, duration % 3600 / 60, duration % 60));
Alexandre Lisionf1850c02013-09-23 14:19:34 -0400541 }
542
Alexandre Lision0edf18c2013-09-23 17:35:50 -0400543 public Conference getConference() {
544 return conf;
545 }
Alexandre Lision64dc8c02013-09-25 15:32:25 -0400546
547 public void onKeyUp(int keyCode, KeyEvent event) {
548 try {
Alexandre Lisionb8add812013-10-24 11:42:42 -0400549
Alexandre Lisionfda4d1d2013-10-25 15:29:53 -0400550 switch (keyCode) {
551 case KeyEvent.KEYCODE_VOLUME_DOWN:
552 case KeyEvent.KEYCODE_VOLUME_UP:
553 break;
554 default:
555 String toSend = Character.toString(event.getDisplayLabel());
556 toSend.toUpperCase(Locale.getDefault());
557 Log.d(TAG, "toSend " + toSend);
558 mCallbacks.getService().playDtmf(toSend);
559 break;
560 }
Alexandre Lision64dc8c02013-09-25 15:32:25 -0400561 } catch (RemoteException e) {
562 e.printStackTrace();
563 }
564 }
Alexandre Lision4fb22622013-10-21 16:26:33 -0400565
566 public void updateCodecName(String currentAudioCodecName) {
Alexandre Lisionf30ff852013-12-09 17:08:40 -0500567 // In case of multiple codec in the String
Alexandre Lisioncb2345c2013-12-09 15:39:13 -0500568 if (currentAudioCodecName.indexOf(' ') > 0)
569 codecNameTxt.setText(currentAudioCodecName.subSequence(0, currentAudioCodecName.indexOf(' ')));
570 else
571 codecNameTxt.setText(currentAudioCodecName);
Alexandre Lision4fb22622013-10-21 16:26:33 -0400572 }
alision84813a12013-05-27 17:40:39 -0400573}