blob: 452c218ab5083fa9081683bb60815cf032f04dbc [file] [log] [blame]
alisionfde875f2013-05-28 17:01:54 -04001/*
alision2ec64f92013-06-17 17:28:58 -04002 * Copyright (C) 2004-2013 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;
58import android.os.RemoteException;
alision84813a12013-05-27 17:40:39 -040059import android.util.Log;
Alexandre Lision64dc8c02013-09-25 15:32:25 -040060import android.view.KeyEvent;
alision84813a12013-05-27 17:40:39 -040061import android.view.LayoutInflater;
Alexandre Lisiond588bff2013-10-08 12:43:01 -040062import android.view.Menu;
63import android.view.MenuInflater;
64import android.view.MenuItem;
Adrien Béraud13cde0b2013-05-30 20:27:20 +100065import android.view.SurfaceHolder;
66import android.view.SurfaceHolder.Callback;
alision84813a12013-05-27 17:40:39 -040067import android.view.View;
Alexandre Lision84208a32013-09-25 13:18:37 -040068import android.view.View.OnClickListener;
alision84813a12013-05-27 17:40:39 -040069import android.view.ViewGroup;
Alexandre Lision84208a32013-09-25 13:18:37 -040070import android.view.inputmethod.InputMethodManager;
Alexandre Lision35577132013-12-06 15:21:15 -050071import android.widget.CompoundButton;
72import android.widget.CompoundButton.OnCheckedChangeListener;
Alexandre Lision84208a32013-09-25 13:18:37 -040073import android.widget.ImageButton;
Alexandre Lision3c6b7102013-09-16 16:56:46 -040074import android.widget.TextView;
Alexandre Lision35577132013-12-06 15:21:15 -050075import android.widget.ToggleButton;
alision84813a12013-05-27 17:40:39 -040076
Alexandre Lisionfda4d1d2013-10-25 15:29:53 -040077public class CallFragment extends Fragment implements Callback {
alision84813a12013-05-27 17:40:39 -040078
alision1005ba12013-06-19 13:52:44 -040079 static final String TAG = "CallFragment";
alision84813a12013-05-27 17:40:39 -040080
alisionf57d8a62013-07-02 09:37:12 -040081 float BUBBLE_SIZE = 75;
alision1005ba12013-06-19 13:52:44 -040082 static final float ATTRACTOR_SIZE = 40;
alision84813a12013-05-27 17:40:39 -040083
Alexandre Lision2b237922013-09-09 16:23:02 -040084 public static final int REQUEST_TRANSFER = 10;
85
alisiondf1dac92013-06-27 17:35:53 -040086 private Conference conf;
alision84813a12013-05-27 17:40:39 -040087
Alexandre Lision3c6b7102013-09-16 16:56:46 -040088 private TextView callStatusTxt;
Alexandre Lisionb8add812013-10-24 11:42:42 -040089 private TextView codecNameTxt;
Alexandre Lisioncb2345c2013-12-09 15:39:13 -050090
Alexandre Lision35577132013-12-06 15:21:15 -050091 private ToggleButton speakers;
Alexandre Lisionb8add812013-10-24 11:42:42 -040092
alision1005ba12013-06-19 13:52:44 -040093 private BubblesView view;
94 private BubbleModel model;
alision84813a12013-05-27 17:40:39 -040095
Alexandre Lision68855472013-10-10 16:20:46 -040096 public Callbacks mCallbacks = sDummyCallbacks;
Alexandre Lisiona764c682013-09-09 10:02:07 -040097 boolean accepted = false;
alision1005ba12013-06-19 13:52:44 -040098 private Bitmap call_icon;
alision85704182013-05-29 15:23:03 -040099
Alexandre Lision0eb02032013-09-27 16:32:40 -0400100 TransferDFragment editName;
Alexandre Lisionf1850c02013-09-23 14:19:34 -0400101
alision1005ba12013-06-19 13:52:44 -0400102 @Override
103 public void onCreate(Bundle savedBundle) {
104 super.onCreate(savedBundle);
Alexandre Lisiondafe9512013-10-28 16:31:41 -0400105 conf = new Conference((Conference) getArguments().getParcelable("conference"));
alisiondf1dac92013-06-27 17:35:53 -0400106 model = new BubbleModel(getResources().getDisplayMetrics().density);
alision729b0a22013-07-02 11:57:33 -0400107 BUBBLE_SIZE = getResources().getDimension(R.dimen.bubble_size);
Alexandre Lisiona764c682013-09-09 10:02:07 -0400108 Log.e(TAG, "BUBBLE_SIZE " + BUBBLE_SIZE);
Alexandre Lisiond588bff2013-10-08 12:43:01 -0400109 this.setHasOptionsMenu(true);
alision729b0a22013-07-02 11:57:33 -0400110
alision1005ba12013-06-19 13:52:44 -0400111 }
alision85992112013-05-29 12:18:08 -0400112
alision1005ba12013-06-19 13:52:44 -0400113 /**
114 * A dummy implementation of the {@link Callbacks} interface that does nothing. Used only when this fragment is not attached to an activity.
115 */
116 private static Callbacks sDummyCallbacks = new Callbacks() {
alision85704182013-05-29 15:23:03 -0400117
alision1005ba12013-06-19 13:52:44 -0400118 @Override
119 public ISipService getService() {
120 return null;
121 }
alisiondf1dac92013-06-27 17:35:53 -0400122
Alexandre Lisionccf0c932013-10-10 16:24:41 -0400123 @Override
Alexandre Lisiondafe9512013-10-28 16:31:41 -0400124 public void terminateCall() {
Alexandre Lisionccf0c932013-10-10 16:24:41 -0400125 }
Alexandre Lision0c384512013-09-17 17:15:57 -0400126
127 @Override
Adrien Béraude78d06f2013-09-19 13:33:44 +1000128 public void startTimer() {
Alexandre Lision0c384512013-09-17 17:15:57 -0400129 }
Alexandre Lision68855472013-10-10 16:20:46 -0400130
131 @Override
Alexandre Lisionb8add812013-10-24 11:42:42 -0400132 public void slideChatScreen() {
Alexandre Lision68855472013-10-10 16:20:46 -0400133 }
alision1005ba12013-06-19 13:52:44 -0400134 };
alision907bde72013-06-20 14:40:37 -0400135
alision1005ba12013-06-19 13:52:44 -0400136 /**
137 * The Activity calling this fragment has to implement this interface
138 *
139 */
140 public interface Callbacks {
alision85992112013-05-29 12:18:08 -0400141
alision1005ba12013-06-19 13:52:44 -0400142 public ISipService getService();
alision85704182013-05-29 15:23:03 -0400143
Alexandre Lision0c384512013-09-17 17:15:57 -0400144 public void startTimer();
Alexandre Lision68855472013-10-10 16:20:46 -0400145
146 public void slideChatScreen();
Alexandre Lisionb8add812013-10-24 11:42:42 -0400147
Alexandre Lisiondafe9512013-10-28 16:31:41 -0400148 public void terminateCall();
alision1005ba12013-06-19 13:52:44 -0400149 }
alision85992112013-05-29 12:18:08 -0400150
alision1005ba12013-06-19 13:52:44 -0400151 @Override
152 public void onAttach(Activity activity) {
153 super.onAttach(activity);
alision85992112013-05-29 12:18:08 -0400154
alision1005ba12013-06-19 13:52:44 -0400155 if (!(activity instanceof Callbacks)) {
156 throw new IllegalStateException("Activity must implement fragment's callbacks.");
157 }
alision85992112013-05-29 12:18:08 -0400158
alision1005ba12013-06-19 13:52:44 -0400159 // rootView.requestDisallowInterceptTouchEvent(true);
alision85992112013-05-29 12:18:08 -0400160
alision1005ba12013-06-19 13:52:44 -0400161 mCallbacks = (Callbacks) activity;
Alexandre Lisionb8add812013-10-24 11:42:42 -0400162 // myself = SipCall.SipCallBuilder.buildMyselfCall(activity.getContentResolver(), "Me");
Adrien Béraude78d06f2013-09-19 13:33:44 +1000163
alision1005ba12013-06-19 13:52:44 -0400164 }
Alexandre Lisionb8add812013-10-24 11:42:42 -0400165
Alexandre Lisiond588bff2013-10-08 12:43:01 -0400166 @Override
167 public void onCreateOptionsMenu(Menu m, MenuInflater inf) {
168 super.onCreateOptionsMenu(m, inf);
Alexandre Lision68855472013-10-10 16:20:46 -0400169 inf.inflate(R.menu.ac_call, m);
Alexandre Lisiond588bff2013-10-08 12:43:01 -0400170 }
171
172 @Override
173 public boolean onOptionsItemSelected(MenuItem item) {
174 super.onOptionsItemSelected(item);
Alexandre Lision666b3772013-10-28 17:42:48 -0400175 switch (item.getItemId()) {
176 case R.id.menuitem_chat:
177 mCallbacks.slideChatScreen();
178 break;
179 }
Alexandre Lisiond588bff2013-10-08 12:43:01 -0400180
181 return true;
182 }
alision85992112013-05-29 12:18:08 -0400183
alision1005ba12013-06-19 13:52:44 -0400184 @Override
185 public void onDetach() {
186 super.onDetach();
187 mCallbacks = sDummyCallbacks;
alision1005ba12013-06-19 13:52:44 -0400188 }
alision907bde72013-06-20 14:40:37 -0400189
alision1005ba12013-06-19 13:52:44 -0400190 @Override
alision907bde72013-06-20 14:40:37 -0400191 public void onStop() {
alision1005ba12013-06-19 13:52:44 -0400192 super.onStop();
193 }
alision85992112013-05-29 12:18:08 -0400194
alision1005ba12013-06-19 13:52:44 -0400195 @Override
Alexandre Lisionf1850c02013-09-23 14:19:34 -0400196 public void onResume() {
197 super.onResume();
Alexandre Lisionf1850c02013-09-23 14:19:34 -0400198 }
199
200 @Override
201 public void onPause() {
202 super.onPause();
Alexandre Lisionf1850c02013-09-23 14:19:34 -0400203 }
204
205 @Override
Alexandre Lision2b237922013-09-09 16:23:02 -0400206 public void onActivityResult(int requestCode, int resultCode, Intent data) {
207 super.onActivityResult(requestCode, resultCode, data);
208 SipCall transfer = null;
209 if (requestCode == REQUEST_TRANSFER) {
210 switch (resultCode) {
Adrien Béraude78d06f2013-09-19 13:33:44 +1000211 case TransferDFragment.RESULT_TRANSFER_CONF:
Alexandre Lision2b237922013-09-09 16:23:02 -0400212 Conference c = data.getParcelableExtra("target");
213 transfer = data.getParcelableExtra("transfer");
214 try {
215
216 mCallbacks.getService().attendedTransfer(transfer.getCallId(), c.getParticipants().get(0).getCallId());
217
218 } catch (RemoteException e) {
Alexandre Lision2b237922013-09-09 16:23:02 -0400219 e.printStackTrace();
220 }
Alexandre Lision2b237922013-09-09 16:23:02 -0400221 break;
222
Adrien Béraude78d06f2013-09-19 13:33:44 +1000223 case TransferDFragment.RESULT_TRANSFER_NUMBER:
Alexandre Lision2b237922013-09-09 16:23:02 -0400224 String to = data.getStringExtra("to_number");
225 transfer = data.getParcelableExtra("transfer");
226 try {
Alexandre Lision2b237922013-09-09 16:23:02 -0400227 mCallbacks.getService().transfer(transfer.getCallId(), to);
228 mCallbacks.getService().hangUp(transfer.getCallId());
229 } catch (RemoteException e) {
Alexandre Lision2b237922013-09-09 16:23:02 -0400230 e.printStackTrace();
231 }
232 break;
Adrien Béraud97512842013-09-19 13:37:19 +1000233 case Activity.RESULT_CANCELED:
Alexandre Lision2b237922013-09-09 16:23:02 -0400234 default:
Adrien Béraude78d06f2013-09-19 13:33:44 +1000235 model.clear();
236 initNormalStateDisplay();
Alexandre Lision2b237922013-09-09 16:23:02 -0400237 break;
238 }
239 }
240 }
241
242 @Override
alision1005ba12013-06-19 13:52:44 -0400243 public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
Alexandre Lision84208a32013-09-25 13:18:37 -0400244 final ViewGroup rootView = (ViewGroup) inflater.inflate(R.layout.frag_call, container, false);
alision85992112013-05-29 12:18:08 -0400245
alision1005ba12013-06-19 13:52:44 -0400246 view = (BubblesView) rootView.findViewById(R.id.main_view);
alision34673e62013-06-25 14:40:07 -0400247 view.setFragment(this);
alision1005ba12013-06-19 13:52:44 -0400248 view.setModel(model);
249 view.getHolder().addCallback(this);
alision84813a12013-05-27 17:40:39 -0400250
Alexandre Lision4fb22622013-10-21 16:26:33 -0400251 codecNameTxt = (TextView) rootView.findViewById(R.id.codec_name_txt);
Alexandre Lision3c6b7102013-09-16 16:56:46 -0400252 callStatusTxt = (TextView) rootView.findViewById(R.id.call_status_txt);
Alexandre Lision68855472013-10-10 16:20:46 -0400253 call_icon = BitmapFactory.decodeResource(getResources(), R.drawable.ic_action_call);
Alexandre Lisioncb2345c2013-12-09 15:39:13 -0500254
Alexandre Lisionbaeeb212013-12-09 12:54:47 -0500255 speakers = (ToggleButton) rootView.findViewById(R.id.speaker_toggle);
Alexandre Lisioncb2345c2013-12-09 15:39:13 -0500256
Alexandre Lision35577132013-12-06 15:21:15 -0500257 speakers.setOnCheckedChangeListener(new OnCheckedChangeListener() {
Alexandre Lisioncb2345c2013-12-09 15:39:13 -0500258
Alexandre Lision35577132013-12-06 15:21:15 -0500259 @Override
260 public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
261 try {
262 mCallbacks.getService().toggleSpeakerPhone(isChecked);
263 } catch (RemoteException e) {
264 e.printStackTrace();
265 }
Alexandre Lisioncb2345c2013-12-09 15:39:13 -0500266
Alexandre Lision35577132013-12-06 15:21:15 -0500267 }
268 });
alision84813a12013-05-27 17:40:39 -0400269
Alexandre Lision84208a32013-09-25 13:18:37 -0400270 ((ImageButton) rootView.findViewById(R.id.dialpad_btn)).setOnClickListener(new OnClickListener() {
271
272 @Override
273 public void onClick(View v) {
274 InputMethodManager lManager = (InputMethodManager) getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
275 lManager.toggleSoftInput(InputMethodManager.SHOW_IMPLICIT, InputMethodManager.HIDE_IMPLICIT_ONLY);
276 }
277 });
278
alision1005ba12013-06-19 13:52:44 -0400279 return rootView;
280 }
Alexandre Lisionc30e8412013-09-26 15:12:59 -0400281
alision1005ba12013-06-19 13:52:44 -0400282 private void initNormalStateDisplay() {
283 Log.i(TAG, "Start normal display");
alision84813a12013-05-27 17:40:39 -0400284
Alexandre Lision0c384512013-09-17 17:15:57 -0400285 mCallbacks.startTimer();
alision84813a12013-05-27 17:40:39 -0400286
Alexandre Lisionee2494d2013-10-09 17:14:00 -0400287 getBubbleForUser(conf, model.width / 2, model.height / 2);
alision907bde72013-06-20 14:40:37 -0400288
alisiondf1dac92013-06-27 17:35:53 -0400289 int angle_part = 360 / conf.getParticipants().size();
alision806e18e2013-06-21 15:30:17 -0400290 double dX = 0;
291 double dY = 0;
alision465ceba2013-07-04 09:24:30 -0400292 int radiusCalls = (int) (model.width / 2 - BUBBLE_SIZE);
alisiondf1dac92013-06-27 17:35:53 -0400293 for (int i = 0; i < conf.getParticipants().size(); ++i) {
294
295 if (conf.getParticipants().get(i) == null) {
alisiondf1dac92013-06-27 17:35:53 -0400296 continue;
297 }
alision34673e62013-06-25 14:40:07 -0400298 dX = Math.cos(Math.toRadians(angle_part * i - 90)) * radiusCalls;
299 dY = Math.sin(Math.toRadians(angle_part * i - 90)) * radiusCalls;
alisiondf1dac92013-06-27 17:35:53 -0400300 getBubbleFor(conf.getParticipants().get(i), (int) (model.width / 2 + dX), (int) (model.height / 2 + dY));
alision806e18e2013-06-21 15:30:17 -0400301 }
alision1005ba12013-06-19 13:52:44 -0400302
303 model.clearAttractors();
alision1005ba12013-06-19 13:52:44 -0400304 }
alision85704182013-05-29 15:23:03 -0400305
alision1005ba12013-06-19 13:52:44 -0400306 private void initIncomingCallDisplay() {
307 Log.i(TAG, "Start incoming display");
alision84813a12013-05-27 17:40:39 -0400308
Alexandre Lision0c384512013-09-17 17:15:57 -0400309 mCallbacks.startTimer();
alision84813a12013-05-27 17:40:39 -0400310
Alexandre Lision23628c12013-09-24 11:17:05 -0400311 int radiusCalls = (int) (model.width / 2 - BUBBLE_SIZE);
Alexandre Lisionee2494d2013-10-09 17:14:00 -0400312 getBubbleForUser(conf, model.width / 2, model.height / 2 + radiusCalls);
Alexandre Lision23628c12013-09-24 11:17:05 -0400313 getBubbleFor(conf.getParticipants().get(0), model.width / 2, model.height / 2 - radiusCalls);
alision85704182013-05-29 15:23:03 -0400314
alision1005ba12013-06-19 13:52:44 -0400315 model.clearAttractors();
Alexandre Lision23628c12013-09-24 11:17:05 -0400316 model.addAttractor(new Attractor(new PointF(model.width / 2, model.height / 2), ATTRACTOR_SIZE, new Attractor.Callback() {
alision1005ba12013-06-19 13:52:44 -0400317 @Override
318 public boolean onBubbleSucked(Bubble b) {
Alexandre Lisiona764c682013-09-09 10:02:07 -0400319
320 if (!accepted) {
Alexandre Lision68855472013-10-10 16:20:46 -0400321 try {
322 mCallbacks.getService().accept(b.getCallID());
323 } catch (RemoteException e) {
324 e.printStackTrace();
325 }
Alexandre Lisiona764c682013-09-09 10:02:07 -0400326 accepted = true;
327 }
alision1005ba12013-06-19 13:52:44 -0400328 return false;
329 }
330 }, call_icon));
Alexandre Lisioncb2345c2013-12-09 15:39:13 -0500331
332 if (conf.getParticipants().get(0).getAccount().isAutoanswerEnabled()) {
Alexandre Lision332f4912013-11-12 13:04:32 -0500333 try {
334 mCallbacks.getService().accept(conf.getParticipants().get(0).getCallId());
335 } catch (RemoteException e) {
336 e.printStackTrace();
337 }
338 }
alision1005ba12013-06-19 13:52:44 -0400339 }
alision85704182013-05-29 15:23:03 -0400340
alision1005ba12013-06-19 13:52:44 -0400341 private void initOutGoingCallDisplay() {
342 Log.i(TAG, "Start outgoing display");
alision85704182013-05-29 15:23:03 -0400343
Alexandre Lision0c384512013-09-17 17:15:57 -0400344 mCallbacks.startTimer();
alision85704182013-05-29 15:23:03 -0400345
Alexandre Lisionee2494d2013-10-09 17:14:00 -0400346 getBubbleForUser(conf, model.width / 2, model.height / 2);
alision85704182013-05-29 15:23:03 -0400347
alisiondf1dac92013-06-27 17:35:53 -0400348 // TODO off-thread image loading
349 int angle_part = 360 / conf.getParticipants().size();
350 double dX = 0;
351 double dY = 0;
Adrien Béraude78d06f2013-09-19 13:33:44 +1000352 int radiusCalls = (int) (model.width / 2 - BUBBLE_SIZE);
alisiondf1dac92013-06-27 17:35:53 -0400353 for (int i = 0; i < conf.getParticipants().size(); ++i) {
354 dX = Math.cos(Math.toRadians(angle_part * i - 90)) * radiusCalls;
355 dY = Math.sin(Math.toRadians(angle_part * i - 90)) * radiusCalls;
356 getBubbleFor(conf.getParticipants().get(i), (int) (model.width / 2 + dX), (int) (model.height / 2 + dY));
357 }
alision907bde72013-06-20 14:40:37 -0400358
alision1005ba12013-06-19 13:52:44 -0400359 model.clearAttractors();
alision1005ba12013-06-19 13:52:44 -0400360 }
alision85704182013-05-29 15:23:03 -0400361
alision1005ba12013-06-19 13:52:44 -0400362 /**
363 * Retrieves or create a bubble for a given contact. If the bubble exists, it is moved to the new location.
364 *
alision806e18e2013-06-21 15:30:17 -0400365 * @param call
366 * The call associated to a contact
alision1005ba12013-06-19 13:52:44 -0400367 * @param x
368 * Initial or new x position.
369 * @param y
370 * Initial or new y position.
371 * @return Bubble corresponding to the contact.
372 */
alision806e18e2013-06-21 15:30:17 -0400373 private Bubble getBubbleFor(SipCall call, float x, float y) {
Alexandre Lisionee2494d2013-10-09 17:14:00 -0400374 Bubble contact_bubble = model.getBubble(call.getCallId());
alision1005ba12013-06-19 13:52:44 -0400375 if (contact_bubble != null) {
Alexandre Lisionb8add812013-10-24 11:42:42 -0400376 ((BubbleContact) contact_bubble).setCall(call);
alision1005ba12013-06-19 13:52:44 -0400377 contact_bubble.attractor.set(x, y);
378 return contact_bubble;
379 }
alision84813a12013-05-27 17:40:39 -0400380
Alexandre Lision40954dc2013-10-09 15:24:03 -0400381 contact_bubble = new BubbleContact(getActivity(), call, x, y, BUBBLE_SIZE);
382
383 model.addBubble(contact_bubble);
384 return contact_bubble;
385 }
Alexandre Lisionb8add812013-10-24 11:42:42 -0400386
Alexandre Lisionee2494d2013-10-09 17:14:00 -0400387 private Bubble getBubbleForUser(Conference conf, float x, float y) {
Alexandre Lisiond5686032013-10-29 11:09:21 -0400388 Bubble contact_bubble = model.getUser();
Alexandre Lision40954dc2013-10-09 15:24:03 -0400389 if (contact_bubble != null) {
390 contact_bubble.attractor.set(x, y);
Alexandre Lisionb8add812013-10-24 11:42:42 -0400391 ((BubbleUser) contact_bubble).setConference(conf);
Alexandre Lisioncb2345c2013-12-09 15:39:13 -0500392
Alexandre Lision40954dc2013-10-09 15:24:03 -0400393 return contact_bubble;
394 }
395
Alexandre Lisioncb2345c2013-12-09 15:39:13 -0500396 contact_bubble = new BubbleUser(getActivity(), CallContact.ContactBuilder.buildUserContact(getActivity().getContentResolver()), conf, x, y,
397 BUBBLE_SIZE * 1.3f);
alision84813a12013-05-27 17:40:39 -0400398
Alexandre Lisionc9fca9e2013-11-08 15:21:18 -0500399 try {
400 ((BubbleUser) contact_bubble).setMute(mCallbacks.getService().isCaptureMuted());
401 } catch (RemoteException e) {
402 e.printStackTrace();
Alexandre Lisioncb2345c2013-12-09 15:39:13 -0500403 } catch (NullPointerException e1) {
Alexandre Lisionc9fca9e2013-11-08 15:21:18 -0500404 e1.printStackTrace();
405 }
alision1005ba12013-06-19 13:52:44 -0400406 model.addBubble(contact_bubble);
alision1005ba12013-06-19 13:52:44 -0400407 return contact_bubble;
408 }
Adrien Béraud0c9bd8f2013-05-30 16:16:57 +1000409
alision1005ba12013-06-19 13:52:44 -0400410 public void changeCallState(String callID, String newState) {
Alexandre Lisione4b95842013-11-12 10:36:51 -0500411 Log.i(TAG, "Call :" + callID + " " + newState);
alisiondf1dac92013-06-27 17:35:53 -0400412 if (newState.contentEquals("FAILURE")) {
413 try {
414 mCallbacks.getService().hangUp(callID);
415 } catch (RemoteException e) {
416 e.printStackTrace();
alision806e18e2013-06-21 15:30:17 -0400417 }
alision1005ba12013-06-19 13:52:44 -0400418 }
Alexandre Lisioncb2345c2013-12-09 15:39:13 -0500419 if (conf == null) {
Alexandre Lisione4b95842013-11-12 10:36:51 -0500420 return;
421 }
alisiondf1dac92013-06-27 17:35:53 -0400422 for (int i = 0; i < conf.getParticipants().size(); ++i) {
alisiondf1dac92013-06-27 17:35:53 -0400423 if (callID.equals(conf.getParticipants().get(i).getCallId())) {
424 if (newState.contentEquals("HUNGUP")) {
alisiondf1dac92013-06-27 17:35:53 -0400425 model.removeBubble(conf.getParticipants().get(i));
426 conf.getParticipants().remove(i);
427 } else {
428 conf.getParticipants().get(i).setCallState(newState);
429 }
430 }
431 }
432
Alexandre Lision4fb22622013-10-21 16:26:33 -0400433 if (conf.isOnGoing()) {
alisiondf1dac92013-06-27 17:35:53 -0400434 initNormalStateDisplay();
Alexandre Lision4fb22622013-10-21 16:26:33 -0400435 try {
436 updateCodecName(mCallbacks.getService().getCurrentAudioCodecName(callID));
437 } catch (RemoteException e) {
438 e.printStackTrace();
439 }
440 }
alisiondf1dac92013-06-27 17:35:53 -0400441
Alexandre Lisionccf0c932013-10-10 16:24:41 -0400442 if (conf.getParticipants().size() == 0) {
Alexandre Lisione4b95842013-11-12 10:36:51 -0500443 callStatusTxt.setText(newState);
Alexandre Lisiondafe9512013-10-28 16:31:41 -0400444 mCallbacks.terminateCall();
Alexandre Lisionccf0c932013-10-10 16:24:41 -0400445 }
alision1005ba12013-06-19 13:52:44 -0400446 }
alision84813a12013-05-27 17:40:39 -0400447
alision1005ba12013-06-19 13:52:44 -0400448 public boolean draggingBubble() {
449 return view == null ? false : view.isDraggingBubble();
450 }
Adrien Béraudc9c424d2013-05-30 17:47:35 +1000451
alision1005ba12013-06-19 13:52:44 -0400452 @Override
453 public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
Adrien Béraud13cde0b2013-05-30 20:27:20 +1000454
alisiondf1dac92013-06-27 17:35:53 -0400455 if (conf.getParticipants().size() == 1) {
alisiondf1dac92013-06-27 17:35:53 -0400456 if (conf.getParticipants().get(0).isIncoming() && conf.getParticipants().get(0).isRinging()) {
alision806e18e2013-06-21 15:30:17 -0400457 initIncomingCallDisplay();
458 } else {
alisiondf1dac92013-06-27 17:35:53 -0400459 if (conf.getParticipants().get(0).isRinging()) {
alision1005ba12013-06-19 13:52:44 -0400460 initOutGoingCallDisplay();
461 }
alision806e18e2013-06-21 15:30:17 -0400462 try {
alisiondf1dac92013-06-27 17:35:53 -0400463 if (conf.getParticipants().get(0).isOutGoing()
464 && mCallbacks.getService().getCall(conf.getParticipants().get(0).getCallId()) == null) {
465 mCallbacks.getService().placeCall(conf.getParticipants().get(0));
alision806e18e2013-06-21 15:30:17 -0400466 initOutGoingCallDisplay();
alisiondf1dac92013-06-27 17:35:53 -0400467 } else if (conf.getParticipants().get(0).isOutGoing() && conf.getParticipants().get(0).isRinging()) {
alision806e18e2013-06-21 15:30:17 -0400468 initOutGoingCallDisplay();
469 }
470 } catch (RemoteException e) {
471 Log.e(TAG, e.toString());
472 }
alision1005ba12013-06-19 13:52:44 -0400473 }
alisiondf1dac92013-06-27 17:35:53 -0400474 if (conf.getParticipants().get(0).isOngoing()) {
alision806e18e2013-06-21 15:30:17 -0400475 initNormalStateDisplay();
476 }
alisiondf1dac92013-06-27 17:35:53 -0400477 } else if (conf.getParticipants().size() > 1) {
alision1005ba12013-06-19 13:52:44 -0400478 initNormalStateDisplay();
479 }
alision1005ba12013-06-19 13:52:44 -0400480 }
Adrien Béraud13cde0b2013-05-30 20:27:20 +1000481
Alexandre Lision40954dc2013-10-09 15:24:03 -0400482 public void makeTransfer(BubbleContact contact) {
Alexandre Lision2b237922013-09-09 16:23:02 -0400483 FragmentManager fm = getFragmentManager();
Alexandre Lision0eb02032013-09-27 16:32:40 -0400484 editName = TransferDFragment.newInstance();
Alexandre Lision2b237922013-09-09 16:23:02 -0400485 Bundle b = new Bundle();
Alexandre Lision6ae652d2013-09-26 16:39:20 -0400486 try {
Alexandre Lisionb8add812013-10-24 11:42:42 -0400487 b.putParcelableArrayList("calls", (ArrayList<Conference>) mCallbacks.getService().getConcurrentCalls());
Alexandre Lision6ae652d2013-09-26 16:39:20 -0400488 b.putParcelable("call_selected", contact.associated_call);
489 editName.setArguments(b);
490 editName.setTargetFragment(this, REQUEST_TRANSFER);
491 editName.show(fm, "");
492 } catch (RemoteException e) {
493 Log.e(TAG, e.toString());
494 }
495
Alexandre Lision2b237922013-09-09 16:23:02 -0400496 }
497
alision1005ba12013-06-19 13:52:44 -0400498 @Override
499 public void surfaceCreated(SurfaceHolder holder) {
Alexandre Lisionc30e8412013-09-26 15:12:59 -0400500
alision1005ba12013-06-19 13:52:44 -0400501 }
Adrien Béraud13cde0b2013-05-30 20:27:20 +1000502
alision1005ba12013-06-19 13:52:44 -0400503 @Override
504 public void surfaceDestroyed(SurfaceHolder holder) {
Alexandre Lision84208a32013-09-25 13:18:37 -0400505 // check that soft input is hidden
506 InputMethodManager lManager = (InputMethodManager) getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
507 lManager.hideSoftInputFromWindow(view.getWindowToken(), 0);
Alexandre Lisionb8add812013-10-24 11:42:42 -0400508 if (editName != null && editName.isVisible()) {
Alexandre Lision0eb02032013-09-27 16:32:40 -0400509 editName.dismiss();
510 }
alision1005ba12013-06-19 13:52:44 -0400511 }
Adrien Béraud13cde0b2013-05-30 20:27:20 +1000512
alisione38001f2013-06-04 14:14:39 -0400513 public BubblesView getBubbleView() {
514 return view;
alision1005ba12013-06-19 13:52:44 -0400515 }
Alexandre Lision0c384512013-09-17 17:15:57 -0400516
517 public void updateTime() {
Adrien Béraude78d06f2013-09-19 13:33:44 +1000518 long duration = System.currentTimeMillis() / 1000 - this.conf.getParticipants().get(0).getTimestamp_start();
Alexandre Lisioncb2345c2013-12-09 15:39:13 -0500519 if (conf.isOnGoing())
Alexandre Lisione4b95842013-11-12 10:36:51 -0500520 callStatusTxt.setText(String.format("%d:%02d:%02d", duration / 3600, duration % 3600 / 60, duration % 60));
Alexandre Lisionf1850c02013-09-23 14:19:34 -0400521 }
522
Alexandre Lision0edf18c2013-09-23 17:35:50 -0400523 public Conference getConference() {
524 return conf;
525 }
Alexandre Lision64dc8c02013-09-25 15:32:25 -0400526
527 public void onKeyUp(int keyCode, KeyEvent event) {
528 try {
Alexandre Lisionb8add812013-10-24 11:42:42 -0400529
Alexandre Lisionfda4d1d2013-10-25 15:29:53 -0400530 switch (keyCode) {
531 case KeyEvent.KEYCODE_VOLUME_DOWN:
532 case KeyEvent.KEYCODE_VOLUME_UP:
533 break;
534 default:
535 String toSend = Character.toString(event.getDisplayLabel());
536 toSend.toUpperCase(Locale.getDefault());
537 Log.d(TAG, "toSend " + toSend);
538 mCallbacks.getService().playDtmf(toSend);
539 break;
540 }
Alexandre Lision64dc8c02013-09-25 15:32:25 -0400541 } catch (RemoteException e) {
542 e.printStackTrace();
543 }
544 }
Alexandre Lision4fb22622013-10-21 16:26:33 -0400545
546 public void updateCodecName(String currentAudioCodecName) {
Alexandre Lisionf30ff852013-12-09 17:08:40 -0500547 // In case of multiple codec in the String
Alexandre Lisioncb2345c2013-12-09 15:39:13 -0500548 if (currentAudioCodecName.indexOf(' ') > 0)
549 codecNameTxt.setText(currentAudioCodecName.subSequence(0, currentAudioCodecName.indexOf(' ')));
550 else
551 codecNameTxt.setText(currentAudioCodecName);
Alexandre Lision4fb22622013-10-21 16:26:33 -0400552 }
alision84813a12013-05-27 17:40:39 -0400553}