blob: c738e6d141ed6f3665c8eaad2501fc73b8a71cb1 [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;
71import android.widget.ImageButton;
Alexandre Lision3c6b7102013-09-16 16:56:46 -040072import android.widget.TextView;
alision84813a12013-05-27 17:40:39 -040073
Alexandre Lisionfda4d1d2013-10-25 15:29:53 -040074public class CallFragment extends Fragment implements Callback {
alision84813a12013-05-27 17:40:39 -040075
alision1005ba12013-06-19 13:52:44 -040076 static final String TAG = "CallFragment";
alision84813a12013-05-27 17:40:39 -040077
alisionf57d8a62013-07-02 09:37:12 -040078 float BUBBLE_SIZE = 75;
alision1005ba12013-06-19 13:52:44 -040079 static final float ATTRACTOR_SIZE = 40;
alision84813a12013-05-27 17:40:39 -040080
Alexandre Lision2b237922013-09-09 16:23:02 -040081 public static final int REQUEST_TRANSFER = 10;
82
alisiondf1dac92013-06-27 17:35:53 -040083 private Conference conf;
alision84813a12013-05-27 17:40:39 -040084
Alexandre Lision3c6b7102013-09-16 16:56:46 -040085 private TextView callStatusTxt;
Alexandre Lisionb8add812013-10-24 11:42:42 -040086 private TextView codecNameTxt;
87
alision1005ba12013-06-19 13:52:44 -040088 private BubblesView view;
89 private BubbleModel model;
alision84813a12013-05-27 17:40:39 -040090
Alexandre Lision68855472013-10-10 16:20:46 -040091 public Callbacks mCallbacks = sDummyCallbacks;
Alexandre Lisiona764c682013-09-09 10:02:07 -040092 boolean accepted = false;
alision1005ba12013-06-19 13:52:44 -040093 private Bitmap call_icon;
alision85704182013-05-29 15:23:03 -040094
Alexandre Lision0eb02032013-09-27 16:32:40 -040095 TransferDFragment editName;
Alexandre Lisionf1850c02013-09-23 14:19:34 -040096
alision1005ba12013-06-19 13:52:44 -040097 @Override
98 public void onCreate(Bundle savedBundle) {
99 super.onCreate(savedBundle);
Alexandre Lisiondafe9512013-10-28 16:31:41 -0400100 conf = new Conference((Conference) getArguments().getParcelable("conference"));
alisiondf1dac92013-06-27 17:35:53 -0400101 model = new BubbleModel(getResources().getDisplayMetrics().density);
alision729b0a22013-07-02 11:57:33 -0400102 BUBBLE_SIZE = getResources().getDimension(R.dimen.bubble_size);
Alexandre Lisiona764c682013-09-09 10:02:07 -0400103 Log.e(TAG, "BUBBLE_SIZE " + BUBBLE_SIZE);
Alexandre Lisiond588bff2013-10-08 12:43:01 -0400104 this.setHasOptionsMenu(true);
alision729b0a22013-07-02 11:57:33 -0400105
alision1005ba12013-06-19 13:52:44 -0400106 }
alision85992112013-05-29 12:18:08 -0400107
alision1005ba12013-06-19 13:52:44 -0400108 /**
109 * A dummy implementation of the {@link Callbacks} interface that does nothing. Used only when this fragment is not attached to an activity.
110 */
111 private static Callbacks sDummyCallbacks = new Callbacks() {
alision85704182013-05-29 15:23:03 -0400112
alision1005ba12013-06-19 13:52:44 -0400113 @Override
114 public ISipService getService() {
115 return null;
116 }
alisiondf1dac92013-06-27 17:35:53 -0400117
Alexandre Lisionccf0c932013-10-10 16:24:41 -0400118 @Override
Alexandre Lisiondafe9512013-10-28 16:31:41 -0400119 public void terminateCall() {
Alexandre Lisionccf0c932013-10-10 16:24:41 -0400120 }
Alexandre Lision0c384512013-09-17 17:15:57 -0400121
122 @Override
Adrien Béraude78d06f2013-09-19 13:33:44 +1000123 public void startTimer() {
Alexandre Lision0c384512013-09-17 17:15:57 -0400124 }
Alexandre Lision68855472013-10-10 16:20:46 -0400125
126 @Override
Alexandre Lisionb8add812013-10-24 11:42:42 -0400127 public void slideChatScreen() {
Alexandre Lision68855472013-10-10 16:20:46 -0400128 }
alision1005ba12013-06-19 13:52:44 -0400129 };
alision907bde72013-06-20 14:40:37 -0400130
alision1005ba12013-06-19 13:52:44 -0400131 /**
132 * The Activity calling this fragment has to implement this interface
133 *
134 */
135 public interface Callbacks {
alision85992112013-05-29 12:18:08 -0400136
alision1005ba12013-06-19 13:52:44 -0400137 public ISipService getService();
alision85704182013-05-29 15:23:03 -0400138
Alexandre Lision0c384512013-09-17 17:15:57 -0400139 public void startTimer();
Alexandre Lision68855472013-10-10 16:20:46 -0400140
141 public void slideChatScreen();
Alexandre Lisionb8add812013-10-24 11:42:42 -0400142
Alexandre Lisiondafe9512013-10-28 16:31:41 -0400143 public void terminateCall();
alision1005ba12013-06-19 13:52:44 -0400144 }
alision85992112013-05-29 12:18:08 -0400145
alision1005ba12013-06-19 13:52:44 -0400146 @Override
147 public void onAttach(Activity activity) {
148 super.onAttach(activity);
alision85992112013-05-29 12:18:08 -0400149
alision1005ba12013-06-19 13:52:44 -0400150 if (!(activity instanceof Callbacks)) {
151 throw new IllegalStateException("Activity must implement fragment's callbacks.");
152 }
alision85992112013-05-29 12:18:08 -0400153
alision1005ba12013-06-19 13:52:44 -0400154 // rootView.requestDisallowInterceptTouchEvent(true);
alision85992112013-05-29 12:18:08 -0400155
alision1005ba12013-06-19 13:52:44 -0400156 mCallbacks = (Callbacks) activity;
Alexandre Lisionb8add812013-10-24 11:42:42 -0400157 // myself = SipCall.SipCallBuilder.buildMyselfCall(activity.getContentResolver(), "Me");
Adrien Béraude78d06f2013-09-19 13:33:44 +1000158
alision1005ba12013-06-19 13:52:44 -0400159 }
Alexandre Lisionb8add812013-10-24 11:42:42 -0400160
Alexandre Lisiond588bff2013-10-08 12:43:01 -0400161 @Override
162 public void onCreateOptionsMenu(Menu m, MenuInflater inf) {
163 super.onCreateOptionsMenu(m, inf);
Alexandre Lision68855472013-10-10 16:20:46 -0400164 inf.inflate(R.menu.ac_call, m);
Alexandre Lisiond588bff2013-10-08 12:43:01 -0400165 }
166
167 @Override
168 public boolean onOptionsItemSelected(MenuItem item) {
169 super.onOptionsItemSelected(item);
Alexandre Lision666b3772013-10-28 17:42:48 -0400170 switch (item.getItemId()) {
171 case R.id.menuitem_chat:
172 mCallbacks.slideChatScreen();
173 break;
174 }
Alexandre Lisiond588bff2013-10-08 12:43:01 -0400175
176 return true;
177 }
alision85992112013-05-29 12:18:08 -0400178
alision1005ba12013-06-19 13:52:44 -0400179 @Override
180 public void onDetach() {
181 super.onDetach();
182 mCallbacks = sDummyCallbacks;
alision1005ba12013-06-19 13:52:44 -0400183 }
alision907bde72013-06-20 14:40:37 -0400184
alision1005ba12013-06-19 13:52:44 -0400185 @Override
alision907bde72013-06-20 14:40:37 -0400186 public void onStop() {
alision1005ba12013-06-19 13:52:44 -0400187 super.onStop();
188 }
alision85992112013-05-29 12:18:08 -0400189
alision1005ba12013-06-19 13:52:44 -0400190 @Override
Alexandre Lisionf1850c02013-09-23 14:19:34 -0400191 public void onResume() {
192 super.onResume();
Alexandre Lisionf1850c02013-09-23 14:19:34 -0400193 }
194
195 @Override
196 public void onPause() {
197 super.onPause();
Alexandre Lisionf1850c02013-09-23 14:19:34 -0400198 }
199
200 @Override
Alexandre Lision2b237922013-09-09 16:23:02 -0400201 public void onActivityResult(int requestCode, int resultCode, Intent data) {
202 super.onActivityResult(requestCode, resultCode, data);
203 SipCall transfer = null;
204 if (requestCode == REQUEST_TRANSFER) {
205 switch (resultCode) {
Adrien Béraude78d06f2013-09-19 13:33:44 +1000206 case TransferDFragment.RESULT_TRANSFER_CONF:
Alexandre Lision2b237922013-09-09 16:23:02 -0400207 Conference c = data.getParcelableExtra("target");
208 transfer = data.getParcelableExtra("transfer");
209 try {
210
211 mCallbacks.getService().attendedTransfer(transfer.getCallId(), c.getParticipants().get(0).getCallId());
212
213 } catch (RemoteException e) {
Alexandre Lision2b237922013-09-09 16:23:02 -0400214 e.printStackTrace();
215 }
Alexandre Lision2b237922013-09-09 16:23:02 -0400216 break;
217
Adrien Béraude78d06f2013-09-19 13:33:44 +1000218 case TransferDFragment.RESULT_TRANSFER_NUMBER:
Alexandre Lision2b237922013-09-09 16:23:02 -0400219 String to = data.getStringExtra("to_number");
220 transfer = data.getParcelableExtra("transfer");
221 try {
Alexandre Lision2b237922013-09-09 16:23:02 -0400222 mCallbacks.getService().transfer(transfer.getCallId(), to);
223 mCallbacks.getService().hangUp(transfer.getCallId());
224 } catch (RemoteException e) {
Alexandre Lision2b237922013-09-09 16:23:02 -0400225 e.printStackTrace();
226 }
227 break;
Adrien Béraud97512842013-09-19 13:37:19 +1000228 case Activity.RESULT_CANCELED:
Alexandre Lision2b237922013-09-09 16:23:02 -0400229 default:
Adrien Béraude78d06f2013-09-19 13:33:44 +1000230 model.clear();
231 initNormalStateDisplay();
Alexandre Lision2b237922013-09-09 16:23:02 -0400232 break;
233 }
234 }
235 }
236
237 @Override
alision1005ba12013-06-19 13:52:44 -0400238 public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
Alexandre Lision84208a32013-09-25 13:18:37 -0400239 final ViewGroup rootView = (ViewGroup) inflater.inflate(R.layout.frag_call, container, false);
alision85992112013-05-29 12:18:08 -0400240
alision1005ba12013-06-19 13:52:44 -0400241 view = (BubblesView) rootView.findViewById(R.id.main_view);
alision34673e62013-06-25 14:40:07 -0400242 view.setFragment(this);
alision1005ba12013-06-19 13:52:44 -0400243 view.setModel(model);
244 view.getHolder().addCallback(this);
alision84813a12013-05-27 17:40:39 -0400245
Alexandre Lision4fb22622013-10-21 16:26:33 -0400246 codecNameTxt = (TextView) rootView.findViewById(R.id.codec_name_txt);
Alexandre Lision3c6b7102013-09-16 16:56:46 -0400247 callStatusTxt = (TextView) rootView.findViewById(R.id.call_status_txt);
Alexandre Lision68855472013-10-10 16:20:46 -0400248 call_icon = BitmapFactory.decodeResource(getResources(), R.drawable.ic_action_call);
alision84813a12013-05-27 17:40:39 -0400249
Alexandre Lision84208a32013-09-25 13:18:37 -0400250 ((ImageButton) rootView.findViewById(R.id.dialpad_btn)).setOnClickListener(new OnClickListener() {
251
252 @Override
253 public void onClick(View v) {
254 InputMethodManager lManager = (InputMethodManager) getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
255 lManager.toggleSoftInput(InputMethodManager.SHOW_IMPLICIT, InputMethodManager.HIDE_IMPLICIT_ONLY);
256 }
257 });
258
alision1005ba12013-06-19 13:52:44 -0400259 return rootView;
260 }
Alexandre Lisionc30e8412013-09-26 15:12:59 -0400261
alision1005ba12013-06-19 13:52:44 -0400262 private void initNormalStateDisplay() {
263 Log.i(TAG, "Start normal display");
alision84813a12013-05-27 17:40:39 -0400264
Alexandre Lision0c384512013-09-17 17:15:57 -0400265 mCallbacks.startTimer();
alision84813a12013-05-27 17:40:39 -0400266
Alexandre Lisionee2494d2013-10-09 17:14:00 -0400267 getBubbleForUser(conf, model.width / 2, model.height / 2);
alision907bde72013-06-20 14:40:37 -0400268
alisiondf1dac92013-06-27 17:35:53 -0400269 int angle_part = 360 / conf.getParticipants().size();
alision806e18e2013-06-21 15:30:17 -0400270 double dX = 0;
271 double dY = 0;
alision465ceba2013-07-04 09:24:30 -0400272 int radiusCalls = (int) (model.width / 2 - BUBBLE_SIZE);
alisiondf1dac92013-06-27 17:35:53 -0400273 for (int i = 0; i < conf.getParticipants().size(); ++i) {
274
275 if (conf.getParticipants().get(i) == null) {
alisiondf1dac92013-06-27 17:35:53 -0400276 continue;
277 }
alision34673e62013-06-25 14:40:07 -0400278 dX = Math.cos(Math.toRadians(angle_part * i - 90)) * radiusCalls;
279 dY = Math.sin(Math.toRadians(angle_part * i - 90)) * radiusCalls;
alisiondf1dac92013-06-27 17:35:53 -0400280 getBubbleFor(conf.getParticipants().get(i), (int) (model.width / 2 + dX), (int) (model.height / 2 + dY));
alision806e18e2013-06-21 15:30:17 -0400281 }
alision1005ba12013-06-19 13:52:44 -0400282
283 model.clearAttractors();
alision1005ba12013-06-19 13:52:44 -0400284 }
alision85704182013-05-29 15:23:03 -0400285
alision1005ba12013-06-19 13:52:44 -0400286 private void initIncomingCallDisplay() {
287 Log.i(TAG, "Start incoming display");
alision84813a12013-05-27 17:40:39 -0400288
Alexandre Lision0c384512013-09-17 17:15:57 -0400289 mCallbacks.startTimer();
alision84813a12013-05-27 17:40:39 -0400290
Alexandre Lision23628c12013-09-24 11:17:05 -0400291 int radiusCalls = (int) (model.width / 2 - BUBBLE_SIZE);
Alexandre Lisionee2494d2013-10-09 17:14:00 -0400292 getBubbleForUser(conf, model.width / 2, model.height / 2 + radiusCalls);
Alexandre Lision23628c12013-09-24 11:17:05 -0400293 getBubbleFor(conf.getParticipants().get(0), model.width / 2, model.height / 2 - radiusCalls);
alision85704182013-05-29 15:23:03 -0400294
alision1005ba12013-06-19 13:52:44 -0400295 model.clearAttractors();
Alexandre Lision23628c12013-09-24 11:17:05 -0400296 model.addAttractor(new Attractor(new PointF(model.width / 2, model.height / 2), ATTRACTOR_SIZE, new Attractor.Callback() {
alision1005ba12013-06-19 13:52:44 -0400297 @Override
298 public boolean onBubbleSucked(Bubble b) {
Alexandre Lisiona764c682013-09-09 10:02:07 -0400299
300 if (!accepted) {
Alexandre Lision68855472013-10-10 16:20:46 -0400301 try {
302 mCallbacks.getService().accept(b.getCallID());
303 } catch (RemoteException e) {
304 e.printStackTrace();
305 }
Alexandre Lisiona764c682013-09-09 10:02:07 -0400306 accepted = true;
307 }
alision1005ba12013-06-19 13:52:44 -0400308 return false;
309 }
310 }, call_icon));
alision1005ba12013-06-19 13:52:44 -0400311 }
alision85704182013-05-29 15:23:03 -0400312
alision1005ba12013-06-19 13:52:44 -0400313 private void initOutGoingCallDisplay() {
314 Log.i(TAG, "Start outgoing display");
alision85704182013-05-29 15:23:03 -0400315
Alexandre Lision0c384512013-09-17 17:15:57 -0400316 mCallbacks.startTimer();
alision85704182013-05-29 15:23:03 -0400317
Alexandre Lisionee2494d2013-10-09 17:14:00 -0400318 getBubbleForUser(conf, model.width / 2, model.height / 2);
alision85704182013-05-29 15:23:03 -0400319
alisiondf1dac92013-06-27 17:35:53 -0400320 // TODO off-thread image loading
321 int angle_part = 360 / conf.getParticipants().size();
322 double dX = 0;
323 double dY = 0;
Adrien Béraude78d06f2013-09-19 13:33:44 +1000324 int radiusCalls = (int) (model.width / 2 - BUBBLE_SIZE);
alisiondf1dac92013-06-27 17:35:53 -0400325 for (int i = 0; i < conf.getParticipants().size(); ++i) {
326 dX = Math.cos(Math.toRadians(angle_part * i - 90)) * radiusCalls;
327 dY = Math.sin(Math.toRadians(angle_part * i - 90)) * radiusCalls;
328 getBubbleFor(conf.getParticipants().get(i), (int) (model.width / 2 + dX), (int) (model.height / 2 + dY));
329 }
alision907bde72013-06-20 14:40:37 -0400330
alision1005ba12013-06-19 13:52:44 -0400331 model.clearAttractors();
alision1005ba12013-06-19 13:52:44 -0400332 }
alision85704182013-05-29 15:23:03 -0400333
alision1005ba12013-06-19 13:52:44 -0400334 /**
335 * Retrieves or create a bubble for a given contact. If the bubble exists, it is moved to the new location.
336 *
alision806e18e2013-06-21 15:30:17 -0400337 * @param call
338 * The call associated to a contact
alision1005ba12013-06-19 13:52:44 -0400339 * @param x
340 * Initial or new x position.
341 * @param y
342 * Initial or new y position.
343 * @return Bubble corresponding to the contact.
344 */
alision806e18e2013-06-21 15:30:17 -0400345 private Bubble getBubbleFor(SipCall call, float x, float y) {
Alexandre Lisionee2494d2013-10-09 17:14:00 -0400346 Bubble contact_bubble = model.getBubble(call.getCallId());
alision1005ba12013-06-19 13:52:44 -0400347 if (contact_bubble != null) {
Alexandre Lisionb8add812013-10-24 11:42:42 -0400348 ((BubbleContact) contact_bubble).setCall(call);
alision1005ba12013-06-19 13:52:44 -0400349 contact_bubble.attractor.set(x, y);
350 return contact_bubble;
351 }
alision84813a12013-05-27 17:40:39 -0400352
Alexandre Lision40954dc2013-10-09 15:24:03 -0400353 contact_bubble = new BubbleContact(getActivity(), call, x, y, BUBBLE_SIZE);
354
355 model.addBubble(contact_bubble);
356 return contact_bubble;
357 }
Alexandre Lisionb8add812013-10-24 11:42:42 -0400358
Alexandre Lisionee2494d2013-10-09 17:14:00 -0400359 private Bubble getBubbleForUser(Conference conf, float x, float y) {
Alexandre Lision68855472013-10-10 16:20:46 -0400360 Bubble contact_bubble = model.getBubble(conf.getId());
Alexandre Lision40954dc2013-10-09 15:24:03 -0400361 if (contact_bubble != null) {
362 contact_bubble.attractor.set(x, y);
Alexandre Lisionb8add812013-10-24 11:42:42 -0400363 ((BubbleUser) contact_bubble).setConference(conf);
Alexandre Lision40954dc2013-10-09 15:24:03 -0400364 return contact_bubble;
365 }
366
Alexandre Lisionb8add812013-10-24 11:42:42 -0400367 contact_bubble = new BubbleUser(getActivity(), CallContact.ContactBuilder.buildUserContact(getActivity().getContentResolver(), getResources()
368 .getString(R.string.me)), conf, x, y, BUBBLE_SIZE);
alision84813a12013-05-27 17:40:39 -0400369
alision1005ba12013-06-19 13:52:44 -0400370 model.addBubble(contact_bubble);
alision1005ba12013-06-19 13:52:44 -0400371 return contact_bubble;
372 }
Adrien Béraud0c9bd8f2013-05-30 16:16:57 +1000373
alision1005ba12013-06-19 13:52:44 -0400374 /**
375 * Should be called when a bubble is removed from the model
376 */
377 void bubbleRemoved(Bubble b) {
Alexandre Lisionb8add812013-10-24 11:42:42 -0400378 // if (b.associated_call == null) {
379 // return;
380 // }
alision1005ba12013-06-19 13:52:44 -0400381 }
Adrien Béraudc9c424d2013-05-30 17:47:35 +1000382
alision1005ba12013-06-19 13:52:44 -0400383 public void changeCallState(String callID, String newState) {
Alexandre Lisionf1850c02013-09-23 14:19:34 -0400384 Log.w(TAG, "Call :" + callID + " " + newState);
alisiondf1dac92013-06-27 17:35:53 -0400385 if (newState.contentEquals("FAILURE")) {
386 try {
387 mCallbacks.getService().hangUp(callID);
388 } catch (RemoteException e) {
389 e.printStackTrace();
alision806e18e2013-06-21 15:30:17 -0400390 }
alision1005ba12013-06-19 13:52:44 -0400391 }
alisiondf1dac92013-06-27 17:35:53 -0400392 for (int i = 0; i < conf.getParticipants().size(); ++i) {
alisiondf1dac92013-06-27 17:35:53 -0400393 if (callID.equals(conf.getParticipants().get(i).getCallId())) {
394 if (newState.contentEquals("HUNGUP")) {
alisiondf1dac92013-06-27 17:35:53 -0400395 model.removeBubble(conf.getParticipants().get(i));
396 conf.getParticipants().remove(i);
397 } else {
398 conf.getParticipants().get(i).setCallState(newState);
399 }
400 }
401 }
402
Alexandre Lision4fb22622013-10-21 16:26:33 -0400403 if (conf.isOnGoing()) {
alisiondf1dac92013-06-27 17:35:53 -0400404 initNormalStateDisplay();
Alexandre Lision4fb22622013-10-21 16:26:33 -0400405 try {
406 updateCodecName(mCallbacks.getService().getCurrentAudioCodecName(callID));
407 } catch (RemoteException e) {
408 e.printStackTrace();
409 }
410 }
alisiondf1dac92013-06-27 17:35:53 -0400411
Alexandre Lisionccf0c932013-10-10 16:24:41 -0400412 if (conf.getParticipants().size() == 0) {
Alexandre Lisiondafe9512013-10-28 16:31:41 -0400413 mCallbacks.terminateCall();
Alexandre Lisionccf0c932013-10-10 16:24:41 -0400414 }
alision1005ba12013-06-19 13:52:44 -0400415 }
alision84813a12013-05-27 17:40:39 -0400416
alision1005ba12013-06-19 13:52:44 -0400417 public boolean draggingBubble() {
418 return view == null ? false : view.isDraggingBubble();
419 }
Adrien Béraudc9c424d2013-05-30 17:47:35 +1000420
alision1005ba12013-06-19 13:52:44 -0400421 @Override
422 public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
Adrien Béraud13cde0b2013-05-30 20:27:20 +1000423
alisiondf1dac92013-06-27 17:35:53 -0400424 if (conf.getParticipants().size() == 1) {
alisiondf1dac92013-06-27 17:35:53 -0400425 if (conf.getParticipants().get(0).isIncoming() && conf.getParticipants().get(0).isRinging()) {
alision806e18e2013-06-21 15:30:17 -0400426 initIncomingCallDisplay();
427 } else {
alisiondf1dac92013-06-27 17:35:53 -0400428 if (conf.getParticipants().get(0).isRinging()) {
alision1005ba12013-06-19 13:52:44 -0400429 initOutGoingCallDisplay();
430 }
alision806e18e2013-06-21 15:30:17 -0400431 try {
alisiondf1dac92013-06-27 17:35:53 -0400432 if (conf.getParticipants().get(0).isOutGoing()
433 && mCallbacks.getService().getCall(conf.getParticipants().get(0).getCallId()) == null) {
434 mCallbacks.getService().placeCall(conf.getParticipants().get(0));
alision806e18e2013-06-21 15:30:17 -0400435 initOutGoingCallDisplay();
alisiondf1dac92013-06-27 17:35:53 -0400436 } else if (conf.getParticipants().get(0).isOutGoing() && conf.getParticipants().get(0).isRinging()) {
alision806e18e2013-06-21 15:30:17 -0400437 initOutGoingCallDisplay();
438 }
439 } catch (RemoteException e) {
440 Log.e(TAG, e.toString());
441 }
alision1005ba12013-06-19 13:52:44 -0400442 }
alisiondf1dac92013-06-27 17:35:53 -0400443 if (conf.getParticipants().get(0).isOngoing()) {
alision806e18e2013-06-21 15:30:17 -0400444 initNormalStateDisplay();
445 }
alisiondf1dac92013-06-27 17:35:53 -0400446 } else if (conf.getParticipants().size() > 1) {
alision1005ba12013-06-19 13:52:44 -0400447 initNormalStateDisplay();
448 }
alision1005ba12013-06-19 13:52:44 -0400449 }
Adrien Béraud13cde0b2013-05-30 20:27:20 +1000450
Alexandre Lision40954dc2013-10-09 15:24:03 -0400451 public void makeTransfer(BubbleContact contact) {
Alexandre Lision2b237922013-09-09 16:23:02 -0400452 FragmentManager fm = getFragmentManager();
Alexandre Lision0eb02032013-09-27 16:32:40 -0400453 editName = TransferDFragment.newInstance();
Alexandre Lision2b237922013-09-09 16:23:02 -0400454 Bundle b = new Bundle();
Alexandre Lision6ae652d2013-09-26 16:39:20 -0400455 try {
Alexandre Lisionb8add812013-10-24 11:42:42 -0400456 b.putParcelableArrayList("calls", (ArrayList<Conference>) mCallbacks.getService().getConcurrentCalls());
Alexandre Lision6ae652d2013-09-26 16:39:20 -0400457 b.putParcelable("call_selected", contact.associated_call);
458 editName.setArguments(b);
459 editName.setTargetFragment(this, REQUEST_TRANSFER);
460 editName.show(fm, "");
461 } catch (RemoteException e) {
462 Log.e(TAG, e.toString());
463 }
464
Alexandre Lision2b237922013-09-09 16:23:02 -0400465 }
466
alision1005ba12013-06-19 13:52:44 -0400467 @Override
468 public void surfaceCreated(SurfaceHolder holder) {
Alexandre Lisionc30e8412013-09-26 15:12:59 -0400469
alision1005ba12013-06-19 13:52:44 -0400470 }
Adrien Béraud13cde0b2013-05-30 20:27:20 +1000471
alision1005ba12013-06-19 13:52:44 -0400472 @Override
473 public void surfaceDestroyed(SurfaceHolder holder) {
Alexandre Lision84208a32013-09-25 13:18:37 -0400474 // check that soft input is hidden
475 InputMethodManager lManager = (InputMethodManager) getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
476 lManager.hideSoftInputFromWindow(view.getWindowToken(), 0);
Alexandre Lisionb8add812013-10-24 11:42:42 -0400477 if (editName != null && editName.isVisible()) {
Alexandre Lision0eb02032013-09-27 16:32:40 -0400478 editName.dismiss();
479 }
alision1005ba12013-06-19 13:52:44 -0400480 }
Adrien Béraud13cde0b2013-05-30 20:27:20 +1000481
alisione38001f2013-06-04 14:14:39 -0400482 public BubblesView getBubbleView() {
483 return view;
alision1005ba12013-06-19 13:52:44 -0400484 }
Alexandre Lision0c384512013-09-17 17:15:57 -0400485
486 public void updateTime() {
Adrien Béraude78d06f2013-09-19 13:33:44 +1000487 long duration = System.currentTimeMillis() / 1000 - this.conf.getParticipants().get(0).getTimestamp_start();
Alexandre Lisionf1850c02013-09-23 14:19:34 -0400488 callStatusTxt.setText(String.format("%d:%02d:%02d", duration / 3600, duration % 3600 / 60, duration % 60));
Alexandre Lisionf1850c02013-09-23 14:19:34 -0400489 }
490
Alexandre Lision0edf18c2013-09-23 17:35:50 -0400491 public Conference getConference() {
492 return conf;
493 }
Alexandre Lision64dc8c02013-09-25 15:32:25 -0400494
495 public void onKeyUp(int keyCode, KeyEvent event) {
496 try {
Alexandre Lisionb8add812013-10-24 11:42:42 -0400497
Alexandre Lisionfda4d1d2013-10-25 15:29:53 -0400498 switch (keyCode) {
499 case KeyEvent.KEYCODE_VOLUME_DOWN:
500 case KeyEvent.KEYCODE_VOLUME_UP:
501 break;
502 default:
503 String toSend = Character.toString(event.getDisplayLabel());
504 toSend.toUpperCase(Locale.getDefault());
505 Log.d(TAG, "toSend " + toSend);
506 mCallbacks.getService().playDtmf(toSend);
507 break;
508 }
Alexandre Lision64dc8c02013-09-25 15:32:25 -0400509 } catch (RemoteException e) {
510 e.printStackTrace();
511 }
512 }
Alexandre Lision4fb22622013-10-21 16:26:33 -0400513
514 public void updateCodecName(String currentAudioCodecName) {
515 codecNameTxt.setText(currentAudioCodecName);
516 }
alision84813a12013-05-27 17:40:39 -0400517}