blob: 858be9394d6b70e7451b5efc51f08ee28ac92662 [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;
Alexandre Lisionf1850c02013-09-23 14:19:34 -040057import android.hardware.Sensor;
58import android.hardware.SensorEvent;
59import android.hardware.SensorEventListener;
60import android.hardware.SensorManager;
alision84813a12013-05-27 17:40:39 -040061import android.os.Bundle;
62import android.os.RemoteException;
alision84813a12013-05-27 17:40:39 -040063import android.util.Log;
Alexandre Lision64dc8c02013-09-25 15:32:25 -040064import android.view.KeyEvent;
alision84813a12013-05-27 17:40:39 -040065import android.view.LayoutInflater;
Alexandre Lisiond588bff2013-10-08 12:43:01 -040066import android.view.Menu;
67import android.view.MenuInflater;
68import android.view.MenuItem;
Adrien Béraud13cde0b2013-05-30 20:27:20 +100069import android.view.SurfaceHolder;
70import android.view.SurfaceHolder.Callback;
alision84813a12013-05-27 17:40:39 -040071import android.view.View;
Alexandre Lision84208a32013-09-25 13:18:37 -040072import android.view.View.OnClickListener;
alision84813a12013-05-27 17:40:39 -040073import android.view.ViewGroup;
Alexandre Lisionc30e8412013-09-26 15:12:59 -040074import android.view.WindowManager;
Alexandre Lision84208a32013-09-25 13:18:37 -040075import android.view.inputmethod.InputMethodManager;
76import android.widget.ImageButton;
Alexandre Lision3c6b7102013-09-16 16:56:46 -040077import android.widget.TextView;
alision84813a12013-05-27 17:40:39 -040078
Alexandre Lisionf1850c02013-09-23 14:19:34 -040079public class CallFragment extends Fragment implements Callback, SensorEventListener {
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;
alision1005ba12013-06-19 13:52:44 -040091 private BubblesView view;
92 private BubbleModel model;
alision84813a12013-05-27 17:40:39 -040093
Alexandre Lision68855472013-10-10 16:20:46 -040094 public Callbacks mCallbacks = sDummyCallbacks;
Alexandre Lisiona764c682013-09-09 10:02:07 -040095 boolean accepted = false;
alision1005ba12013-06-19 13:52:44 -040096 private Bitmap call_icon;
alision85704182013-05-29 15:23:03 -040097
Alexandre Lisionf1850c02013-09-23 14:19:34 -040098 private SensorManager mSensorManager;
99
100 private Sensor mSensor;
Alexandre Lision0eb02032013-09-27 16:32:40 -0400101
102 TransferDFragment editName;
Alexandre Lisionf1850c02013-09-23 14:19:34 -0400103
Alexandre Lision4fb22622013-10-21 16:26:33 -0400104 private TextView codecNameTxt;
105
alision1005ba12013-06-19 13:52:44 -0400106 @Override
107 public void onCreate(Bundle savedBundle) {
108 super.onCreate(savedBundle);
alision1005ba12013-06-19 13:52:44 -0400109 Bundle b = getArguments();
alisioncd8fb912013-06-28 14:43:51 -0400110 conf = new Conference((Conference) b.getParcelable("conference"));
alisiondf1dac92013-06-27 17:35:53 -0400111 model = new BubbleModel(getResources().getDisplayMetrics().density);
alision729b0a22013-07-02 11:57:33 -0400112 BUBBLE_SIZE = getResources().getDimension(R.dimen.bubble_size);
Alexandre Lisiona764c682013-09-09 10:02:07 -0400113 Log.e(TAG, "BUBBLE_SIZE " + BUBBLE_SIZE);
Alexandre Lisionf1850c02013-09-23 14:19:34 -0400114 mSensorManager = (SensorManager) getActivity().getSystemService(Context.SENSOR_SERVICE);
115 mSensor = mSensorManager.getDefaultSensor(Sensor.TYPE_PROXIMITY);
Alexandre Lisiond588bff2013-10-08 12:43:01 -0400116 this.setHasOptionsMenu(true);
alision729b0a22013-07-02 11:57:33 -0400117
alision1005ba12013-06-19 13:52:44 -0400118 }
alision85992112013-05-29 12:18:08 -0400119
alision1005ba12013-06-19 13:52:44 -0400120 /**
121 * A dummy implementation of the {@link Callbacks} interface that does nothing. Used only when this fragment is not attached to an activity.
122 */
123 private static Callbacks sDummyCallbacks = new Callbacks() {
alision85704182013-05-29 15:23:03 -0400124
Alexandre Lisionccf0c932013-10-10 16:24:41 -0400125
alision1005ba12013-06-19 13:52:44 -0400126 @Override
127 public ISipService getService() {
128 return null;
129 }
alisiondf1dac92013-06-27 17:35:53 -0400130
Alexandre Lisionccf0c932013-10-10 16:24:41 -0400131 @Override
132 public void replaceCurrentCallDisplayed() {
133 }
Alexandre Lision0c384512013-09-17 17:15:57 -0400134
135 @Override
Adrien Béraude78d06f2013-09-19 13:33:44 +1000136 public void startTimer() {
Alexandre Lision0c384512013-09-17 17:15:57 -0400137 }
Alexandre Lision68855472013-10-10 16:20:46 -0400138
139 @Override
140 public void slideChatScreen() {
141 }
alision1005ba12013-06-19 13:52:44 -0400142 };
alision907bde72013-06-20 14:40:37 -0400143
alision1005ba12013-06-19 13:52:44 -0400144 /**
145 * The Activity calling this fragment has to implement this interface
146 *
147 */
148 public interface Callbacks {
alision85992112013-05-29 12:18:08 -0400149
alision1005ba12013-06-19 13:52:44 -0400150 public ISipService getService();
alision85704182013-05-29 15:23:03 -0400151
Alexandre Lision0c384512013-09-17 17:15:57 -0400152 public void startTimer();
Alexandre Lision68855472013-10-10 16:20:46 -0400153
154 public void slideChatScreen();
Alexandre Lisionccf0c932013-10-10 16:24:41 -0400155
156 public void replaceCurrentCallDisplayed();
alision1005ba12013-06-19 13:52:44 -0400157 }
alision85992112013-05-29 12:18:08 -0400158
alision1005ba12013-06-19 13:52:44 -0400159 @Override
160 public void onAttach(Activity activity) {
161 super.onAttach(activity);
alision85992112013-05-29 12:18:08 -0400162
alision1005ba12013-06-19 13:52:44 -0400163 if (!(activity instanceof Callbacks)) {
164 throw new IllegalStateException("Activity must implement fragment's callbacks.");
165 }
alision85992112013-05-29 12:18:08 -0400166
alision1005ba12013-06-19 13:52:44 -0400167 // rootView.requestDisallowInterceptTouchEvent(true);
alision85992112013-05-29 12:18:08 -0400168
alision1005ba12013-06-19 13:52:44 -0400169 mCallbacks = (Callbacks) activity;
Alexandre Lision68855472013-10-10 16:20:46 -0400170// myself = SipCall.SipCallBuilder.buildMyselfCall(activity.getContentResolver(), "Me");
Adrien Béraude78d06f2013-09-19 13:33:44 +1000171
alision1005ba12013-06-19 13:52:44 -0400172 }
Alexandre Lisiond588bff2013-10-08 12:43:01 -0400173
174 @Override
175 public void onCreateOptionsMenu(Menu m, MenuInflater inf) {
176 super.onCreateOptionsMenu(m, inf);
Alexandre Lision68855472013-10-10 16:20:46 -0400177 inf.inflate(R.menu.ac_call, m);
Alexandre Lisiond588bff2013-10-08 12:43:01 -0400178 }
179
180 @Override
181 public boolean onOptionsItemSelected(MenuItem item) {
182 super.onOptionsItemSelected(item);
Alexandre Lision5ed2c972013-10-11 15:36:33 -0400183// switch (item.getItemId()) {
184// case R.id.menuitem_chat:
185// mCallbacks.slideChatScreen();
186// break;
187// }
Alexandre Lisiond588bff2013-10-08 12:43:01 -0400188
189 return true;
190 }
alision85992112013-05-29 12:18:08 -0400191
alision1005ba12013-06-19 13:52:44 -0400192 @Override
193 public void onDetach() {
194 super.onDetach();
195 mCallbacks = sDummyCallbacks;
Alexandre Lisionf1850c02013-09-23 14:19:34 -0400196 mSensorManager.unregisterListener(this);
alision1005ba12013-06-19 13:52:44 -0400197 }
alision907bde72013-06-20 14:40:37 -0400198
alision1005ba12013-06-19 13:52:44 -0400199 @Override
alision907bde72013-06-20 14:40:37 -0400200 public void onStop() {
alision1005ba12013-06-19 13:52:44 -0400201 super.onStop();
202 }
alision85992112013-05-29 12:18:08 -0400203
alision1005ba12013-06-19 13:52:44 -0400204 @Override
Alexandre Lisionf1850c02013-09-23 14:19:34 -0400205 public void onResume() {
206 super.onResume();
207 mSensorManager.registerListener(this, mSensor, SensorManager.SENSOR_DELAY_NORMAL);
208 }
209
210 @Override
211 public void onPause() {
212 super.onPause();
213 mSensorManager.unregisterListener(this);
214 }
215
216 @Override
Alexandre Lision2b237922013-09-09 16:23:02 -0400217 public void onActivityResult(int requestCode, int resultCode, Intent data) {
218 super.onActivityResult(requestCode, resultCode, data);
219 SipCall transfer = null;
220 if (requestCode == REQUEST_TRANSFER) {
221 switch (resultCode) {
Adrien Béraude78d06f2013-09-19 13:33:44 +1000222 case TransferDFragment.RESULT_TRANSFER_CONF:
Alexandre Lision2b237922013-09-09 16:23:02 -0400223 Conference c = data.getParcelableExtra("target");
224 transfer = data.getParcelableExtra("transfer");
225 try {
226
227 mCallbacks.getService().attendedTransfer(transfer.getCallId(), c.getParticipants().get(0).getCallId());
228
229 } catch (RemoteException e) {
Alexandre Lision2b237922013-09-09 16:23:02 -0400230 e.printStackTrace();
231 }
Alexandre Lision2b237922013-09-09 16:23:02 -0400232 break;
233
Adrien Béraude78d06f2013-09-19 13:33:44 +1000234 case TransferDFragment.RESULT_TRANSFER_NUMBER:
Alexandre Lision2b237922013-09-09 16:23:02 -0400235 String to = data.getStringExtra("to_number");
236 transfer = data.getParcelableExtra("transfer");
237 try {
Alexandre Lision2b237922013-09-09 16:23:02 -0400238 mCallbacks.getService().transfer(transfer.getCallId(), to);
239 mCallbacks.getService().hangUp(transfer.getCallId());
240 } catch (RemoteException e) {
Alexandre Lision2b237922013-09-09 16:23:02 -0400241 e.printStackTrace();
242 }
243 break;
Adrien Béraud97512842013-09-19 13:37:19 +1000244 case Activity.RESULT_CANCELED:
Alexandre Lision2b237922013-09-09 16:23:02 -0400245 default:
Adrien Béraude78d06f2013-09-19 13:33:44 +1000246 model.clear();
247 initNormalStateDisplay();
Alexandre Lision2b237922013-09-09 16:23:02 -0400248 break;
249 }
250 }
251 }
252
253 @Override
alision1005ba12013-06-19 13:52:44 -0400254 public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
Alexandre Lision84208a32013-09-25 13:18:37 -0400255 final ViewGroup rootView = (ViewGroup) inflater.inflate(R.layout.frag_call, container, false);
alision85992112013-05-29 12:18:08 -0400256
alision1005ba12013-06-19 13:52:44 -0400257 view = (BubblesView) rootView.findViewById(R.id.main_view);
alision34673e62013-06-25 14:40:07 -0400258 view.setFragment(this);
alision1005ba12013-06-19 13:52:44 -0400259 view.setModel(model);
260 view.getHolder().addCallback(this);
alision84813a12013-05-27 17:40:39 -0400261
Alexandre Lision4fb22622013-10-21 16:26:33 -0400262 codecNameTxt = (TextView) rootView.findViewById(R.id.codec_name_txt);
Alexandre Lision3c6b7102013-09-16 16:56:46 -0400263 callStatusTxt = (TextView) rootView.findViewById(R.id.call_status_txt);
Alexandre Lision68855472013-10-10 16:20:46 -0400264 call_icon = BitmapFactory.decodeResource(getResources(), R.drawable.ic_action_call);
alision84813a12013-05-27 17:40:39 -0400265
Alexandre Lision84208a32013-09-25 13:18:37 -0400266 ((ImageButton) rootView.findViewById(R.id.dialpad_btn)).setOnClickListener(new OnClickListener() {
267
268 @Override
269 public void onClick(View v) {
270 InputMethodManager lManager = (InputMethodManager) getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
271 lManager.toggleSoftInput(InputMethodManager.SHOW_IMPLICIT, InputMethodManager.HIDE_IMPLICIT_ONLY);
272 }
273 });
274
alision1005ba12013-06-19 13:52:44 -0400275 return rootView;
276 }
Alexandre Lisionc30e8412013-09-26 15:12:59 -0400277
alision1005ba12013-06-19 13:52:44 -0400278 private void initNormalStateDisplay() {
279 Log.i(TAG, "Start normal display");
alision84813a12013-05-27 17:40:39 -0400280
Alexandre Lision0c384512013-09-17 17:15:57 -0400281 mCallbacks.startTimer();
alision84813a12013-05-27 17:40:39 -0400282
Alexandre Lisionee2494d2013-10-09 17:14:00 -0400283 getBubbleForUser(conf, model.width / 2, model.height / 2);
alision907bde72013-06-20 14:40:37 -0400284
alisiondf1dac92013-06-27 17:35:53 -0400285 int angle_part = 360 / conf.getParticipants().size();
alision806e18e2013-06-21 15:30:17 -0400286 double dX = 0;
287 double dY = 0;
alision465ceba2013-07-04 09:24:30 -0400288 int radiusCalls = (int) (model.width / 2 - BUBBLE_SIZE);
alisiondf1dac92013-06-27 17:35:53 -0400289 for (int i = 0; i < conf.getParticipants().size(); ++i) {
290
291 if (conf.getParticipants().get(i) == null) {
alisiondf1dac92013-06-27 17:35:53 -0400292 continue;
293 }
alision34673e62013-06-25 14:40:07 -0400294 dX = Math.cos(Math.toRadians(angle_part * i - 90)) * radiusCalls;
295 dY = Math.sin(Math.toRadians(angle_part * i - 90)) * radiusCalls;
alisiondf1dac92013-06-27 17:35:53 -0400296 getBubbleFor(conf.getParticipants().get(i), (int) (model.width / 2 + dX), (int) (model.height / 2 + dY));
alision806e18e2013-06-21 15:30:17 -0400297 }
alision1005ba12013-06-19 13:52:44 -0400298
299 model.clearAttractors();
alision1005ba12013-06-19 13:52:44 -0400300 }
alision85704182013-05-29 15:23:03 -0400301
alision1005ba12013-06-19 13:52:44 -0400302 private void initIncomingCallDisplay() {
303 Log.i(TAG, "Start incoming 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 Lision23628c12013-09-24 11:17:05 -0400307 int radiusCalls = (int) (model.width / 2 - BUBBLE_SIZE);
Alexandre Lisionee2494d2013-10-09 17:14:00 -0400308 getBubbleForUser(conf, model.width / 2, model.height / 2 + radiusCalls);
Alexandre Lision23628c12013-09-24 11:17:05 -0400309 getBubbleFor(conf.getParticipants().get(0), model.width / 2, model.height / 2 - radiusCalls);
alision85704182013-05-29 15:23:03 -0400310
alision1005ba12013-06-19 13:52:44 -0400311 model.clearAttractors();
Alexandre Lision23628c12013-09-24 11:17:05 -0400312 model.addAttractor(new Attractor(new PointF(model.width / 2, model.height / 2), ATTRACTOR_SIZE, new Attractor.Callback() {
alision1005ba12013-06-19 13:52:44 -0400313 @Override
314 public boolean onBubbleSucked(Bubble b) {
Alexandre Lisiona764c682013-09-09 10:02:07 -0400315
316 if (!accepted) {
Alexandre Lision68855472013-10-10 16:20:46 -0400317 try {
318 mCallbacks.getService().accept(b.getCallID());
319 } catch (RemoteException e) {
320 e.printStackTrace();
321 }
Alexandre Lisiona764c682013-09-09 10:02:07 -0400322 accepted = true;
323 }
alision1005ba12013-06-19 13:52:44 -0400324 return false;
325 }
326 }, call_icon));
alision1005ba12013-06-19 13:52:44 -0400327 }
alision85704182013-05-29 15:23:03 -0400328
alision1005ba12013-06-19 13:52:44 -0400329 private void initOutGoingCallDisplay() {
330 Log.i(TAG, "Start outgoing display");
alision85704182013-05-29 15:23:03 -0400331
Alexandre Lision0c384512013-09-17 17:15:57 -0400332 mCallbacks.startTimer();
alision85704182013-05-29 15:23:03 -0400333
Alexandre Lisionee2494d2013-10-09 17:14:00 -0400334 getBubbleForUser(conf, model.width / 2, model.height / 2);
alision85704182013-05-29 15:23:03 -0400335
alisiondf1dac92013-06-27 17:35:53 -0400336 // TODO off-thread image loading
337 int angle_part = 360 / conf.getParticipants().size();
338 double dX = 0;
339 double dY = 0;
Adrien Béraude78d06f2013-09-19 13:33:44 +1000340 int radiusCalls = (int) (model.width / 2 - BUBBLE_SIZE);
alisiondf1dac92013-06-27 17:35:53 -0400341 for (int i = 0; i < conf.getParticipants().size(); ++i) {
342 dX = Math.cos(Math.toRadians(angle_part * i - 90)) * radiusCalls;
343 dY = Math.sin(Math.toRadians(angle_part * i - 90)) * radiusCalls;
344 getBubbleFor(conf.getParticipants().get(i), (int) (model.width / 2 + dX), (int) (model.height / 2 + dY));
345 }
alision907bde72013-06-20 14:40:37 -0400346
alision1005ba12013-06-19 13:52:44 -0400347 model.clearAttractors();
alision1005ba12013-06-19 13:52:44 -0400348 }
alision85704182013-05-29 15:23:03 -0400349
alision1005ba12013-06-19 13:52:44 -0400350 /**
351 * Retrieves or create a bubble for a given contact. If the bubble exists, it is moved to the new location.
352 *
alision806e18e2013-06-21 15:30:17 -0400353 * @param call
354 * The call associated to a contact
alision1005ba12013-06-19 13:52:44 -0400355 * @param x
356 * Initial or new x position.
357 * @param y
358 * Initial or new y position.
359 * @return Bubble corresponding to the contact.
360 */
alision806e18e2013-06-21 15:30:17 -0400361 private Bubble getBubbleFor(SipCall call, float x, float y) {
Alexandre Lisionee2494d2013-10-09 17:14:00 -0400362 Bubble contact_bubble = model.getBubble(call.getCallId());
alision1005ba12013-06-19 13:52:44 -0400363 if (contact_bubble != null) {
Alexandre Lision68855472013-10-10 16:20:46 -0400364 ((BubbleContact)contact_bubble).setCall(call);
alision1005ba12013-06-19 13:52:44 -0400365 contact_bubble.attractor.set(x, y);
366 return contact_bubble;
367 }
alision84813a12013-05-27 17:40:39 -0400368
Alexandre Lision40954dc2013-10-09 15:24:03 -0400369 contact_bubble = new BubbleContact(getActivity(), call, x, y, BUBBLE_SIZE);
370
371 model.addBubble(contact_bubble);
372 return contact_bubble;
373 }
374
Alexandre Lisionee2494d2013-10-09 17:14:00 -0400375 private Bubble getBubbleForUser(Conference conf, float x, float y) {
Alexandre Lision68855472013-10-10 16:20:46 -0400376 Bubble contact_bubble = model.getBubble(conf.getId());
Alexandre Lision40954dc2013-10-09 15:24:03 -0400377 if (contact_bubble != null) {
378 contact_bubble.attractor.set(x, y);
Alexandre Lision68855472013-10-10 16:20:46 -0400379 ((BubbleUser)contact_bubble).setConference(conf);
Alexandre Lision40954dc2013-10-09 15:24:03 -0400380 return contact_bubble;
381 }
382
Alexandre Lision68855472013-10-10 16:20:46 -0400383 contact_bubble = new BubbleUser(getActivity(), CallContact.ContactBuilder.buildUserContact(getActivity().getContentResolver(), getResources().getString(R.string.me)), conf, x, y, BUBBLE_SIZE);
alision84813a12013-05-27 17:40:39 -0400384
alision1005ba12013-06-19 13:52:44 -0400385 model.addBubble(contact_bubble);
alision1005ba12013-06-19 13:52:44 -0400386 return contact_bubble;
387 }
Adrien Béraud0c9bd8f2013-05-30 16:16:57 +1000388
alision1005ba12013-06-19 13:52:44 -0400389 /**
390 * Should be called when a bubble is removed from the model
391 */
392 void bubbleRemoved(Bubble b) {
Alexandre Lision40954dc2013-10-09 15:24:03 -0400393// if (b.associated_call == null) {
394// return;
395// }
alision1005ba12013-06-19 13:52:44 -0400396 }
Adrien Béraudc9c424d2013-05-30 17:47:35 +1000397
alision1005ba12013-06-19 13:52:44 -0400398 public void changeCallState(String callID, String newState) {
Alexandre Lisionf1850c02013-09-23 14:19:34 -0400399 Log.w(TAG, "Call :" + callID + " " + newState);
alisiondf1dac92013-06-27 17:35:53 -0400400 if (newState.contentEquals("FAILURE")) {
401 try {
402 mCallbacks.getService().hangUp(callID);
403 } catch (RemoteException e) {
404 e.printStackTrace();
alision806e18e2013-06-21 15:30:17 -0400405 }
alision1005ba12013-06-19 13:52:44 -0400406 }
alisiondf1dac92013-06-27 17:35:53 -0400407 for (int i = 0; i < conf.getParticipants().size(); ++i) {
alisiondf1dac92013-06-27 17:35:53 -0400408 if (callID.equals(conf.getParticipants().get(i).getCallId())) {
409 if (newState.contentEquals("HUNGUP")) {
alisiondf1dac92013-06-27 17:35:53 -0400410 model.removeBubble(conf.getParticipants().get(i));
411 conf.getParticipants().remove(i);
412 } else {
413 conf.getParticipants().get(i).setCallState(newState);
414 }
415 }
416 }
417
Alexandre Lision4fb22622013-10-21 16:26:33 -0400418 if (conf.isOnGoing()) {
alisiondf1dac92013-06-27 17:35:53 -0400419 initNormalStateDisplay();
Alexandre Lision4fb22622013-10-21 16:26:33 -0400420 try {
421 updateCodecName(mCallbacks.getService().getCurrentAudioCodecName(callID));
422 } catch (RemoteException e) {
423 e.printStackTrace();
424 }
425 }
alisiondf1dac92013-06-27 17:35:53 -0400426
Alexandre Lisionccf0c932013-10-10 16:24:41 -0400427 if (conf.getParticipants().size() == 0) {
428 mCallbacks.replaceCurrentCallDisplayed();
429 }
alision1005ba12013-06-19 13:52:44 -0400430 }
alision84813a12013-05-27 17:40:39 -0400431
alision1005ba12013-06-19 13:52:44 -0400432 public boolean draggingBubble() {
433 return view == null ? false : view.isDraggingBubble();
434 }
Adrien Béraudc9c424d2013-05-30 17:47:35 +1000435
alision1005ba12013-06-19 13:52:44 -0400436 @Override
437 public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
Adrien Béraud13cde0b2013-05-30 20:27:20 +1000438
alisiondf1dac92013-06-27 17:35:53 -0400439 if (conf.getParticipants().size() == 1) {
alisiondf1dac92013-06-27 17:35:53 -0400440 if (conf.getParticipants().get(0).isIncoming() && conf.getParticipants().get(0).isRinging()) {
alision806e18e2013-06-21 15:30:17 -0400441 initIncomingCallDisplay();
442 } else {
alisiondf1dac92013-06-27 17:35:53 -0400443 if (conf.getParticipants().get(0).isRinging()) {
alision1005ba12013-06-19 13:52:44 -0400444 initOutGoingCallDisplay();
445 }
alision806e18e2013-06-21 15:30:17 -0400446 try {
alisiondf1dac92013-06-27 17:35:53 -0400447 if (conf.getParticipants().get(0).isOutGoing()
448 && mCallbacks.getService().getCall(conf.getParticipants().get(0).getCallId()) == null) {
449 mCallbacks.getService().placeCall(conf.getParticipants().get(0));
alision806e18e2013-06-21 15:30:17 -0400450 initOutGoingCallDisplay();
alisiondf1dac92013-06-27 17:35:53 -0400451 } else if (conf.getParticipants().get(0).isOutGoing() && conf.getParticipants().get(0).isRinging()) {
alision806e18e2013-06-21 15:30:17 -0400452 initOutGoingCallDisplay();
453 }
454 } catch (RemoteException e) {
455 Log.e(TAG, e.toString());
456 }
alision1005ba12013-06-19 13:52:44 -0400457 }
alisiondf1dac92013-06-27 17:35:53 -0400458 if (conf.getParticipants().get(0).isOngoing()) {
alision806e18e2013-06-21 15:30:17 -0400459 initNormalStateDisplay();
460 }
alisiondf1dac92013-06-27 17:35:53 -0400461 } else if (conf.getParticipants().size() > 1) {
alision1005ba12013-06-19 13:52:44 -0400462 initNormalStateDisplay();
463 }
alision1005ba12013-06-19 13:52:44 -0400464 }
Adrien Béraud13cde0b2013-05-30 20:27:20 +1000465
Alexandre Lision40954dc2013-10-09 15:24:03 -0400466 public void makeTransfer(BubbleContact contact) {
Alexandre Lision2b237922013-09-09 16:23:02 -0400467 FragmentManager fm = getFragmentManager();
Alexandre Lision0eb02032013-09-27 16:32:40 -0400468 editName = TransferDFragment.newInstance();
Alexandre Lision2b237922013-09-09 16:23:02 -0400469 Bundle b = new Bundle();
Alexandre Lision6ae652d2013-09-26 16:39:20 -0400470 try {
471 b.putParcelableArrayList("calls", (ArrayList<Conference>)mCallbacks.getService().getConcurrentCalls());
472 b.putParcelable("call_selected", contact.associated_call);
473 editName.setArguments(b);
474 editName.setTargetFragment(this, REQUEST_TRANSFER);
475 editName.show(fm, "");
476 } catch (RemoteException e) {
477 Log.e(TAG, e.toString());
478 }
479
Alexandre Lision2b237922013-09-09 16:23:02 -0400480 }
481
alision1005ba12013-06-19 13:52:44 -0400482 @Override
483 public void surfaceCreated(SurfaceHolder holder) {
Alexandre Lisionc30e8412013-09-26 15:12:59 -0400484
alision1005ba12013-06-19 13:52:44 -0400485 }
Adrien Béraud13cde0b2013-05-30 20:27:20 +1000486
alision1005ba12013-06-19 13:52:44 -0400487 @Override
488 public void surfaceDestroyed(SurfaceHolder holder) {
Alexandre Lision84208a32013-09-25 13:18:37 -0400489 // check that soft input is hidden
490 InputMethodManager lManager = (InputMethodManager) getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
491 lManager.hideSoftInputFromWindow(view.getWindowToken(), 0);
Alexandre Lision0eb02032013-09-27 16:32:40 -0400492 if(editName != null && editName.isVisible()){
493 editName.dismiss();
494 }
alision1005ba12013-06-19 13:52:44 -0400495 }
Adrien Béraud13cde0b2013-05-30 20:27:20 +1000496
alisione38001f2013-06-04 14:14:39 -0400497 public BubblesView getBubbleView() {
498 return view;
alision1005ba12013-06-19 13:52:44 -0400499 }
Alexandre Lision0c384512013-09-17 17:15:57 -0400500
501 public void updateTime() {
Adrien Béraude78d06f2013-09-19 13:33:44 +1000502 long duration = System.currentTimeMillis() / 1000 - this.conf.getParticipants().get(0).getTimestamp_start();
Alexandre Lisionf1850c02013-09-23 14:19:34 -0400503 callStatusTxt.setText(String.format("%d:%02d:%02d", duration / 3600, duration % 3600 / 60, duration % 60));
Alexandre Lisionf1850c02013-09-23 14:19:34 -0400504 }
505
506 @Override
507 public void onAccuracyChanged(Sensor sensor, int accuracy) {
Alexandre Lisionf1850c02013-09-23 14:19:34 -0400508
509 }
510
511 @Override
512 public void onSensorChanged(SensorEvent event) {
Alexandre Lisionc30e8412013-09-26 15:12:59 -0400513 if (event.values[0] == 0) {
514 WindowManager.LayoutParams params = getActivity().getWindow().getAttributes();
515 params.flags |= WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON;
516 params.flags = WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE;
517 params.screenBrightness = 0.004f;
518 getActivity().getWindow().setAttributes(params);
Adrien Béraude78d06f2013-09-19 13:33:44 +1000519
Alexandre Lisionc30e8412013-09-26 15:12:59 -0400520 } else {
521 WindowManager.LayoutParams params = getActivity().getWindow().getAttributes();
522 getActivity().getWindow().clearFlags(WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE);
523 params.flags = WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON;
524 params.screenBrightness = 1.0f;
525 getActivity().getWindow().setAttributes(params);
526 }
Alexandre Lision0c384512013-09-17 17:15:57 -0400527 }
Alexandre Lision0edf18c2013-09-23 17:35:50 -0400528
529 public Conference getConference() {
530 return conf;
531 }
Alexandre Lision64dc8c02013-09-25 15:32:25 -0400532
533 public void onKeyUp(int keyCode, KeyEvent event) {
534 try {
Alexandre Lision11dee862013-10-17 11:53:21 -0400535
536 if(event.getKeyCode() == KeyEvent.KEYCODE_VOLUME_UP || event.getKeyCode() == KeyEvent.KEYCODE_VOLUME_DOWN)
537 return;
538
539
Alexandre Lision80f0a7e2013-09-26 12:27:51 -0400540 String toSend = Character.toString(event.getDisplayLabel());
Alexandre Lision31f46fc2013-09-26 11:19:54 -0400541 toSend.toUpperCase(Locale.getDefault());
Alexandre Lisionc30e8412013-09-26 15:12:59 -0400542 Log.d(TAG, "toSend " + toSend);
Alexandre Lision64dc8c02013-09-25 15:32:25 -0400543 mCallbacks.getService().playDtmf(toSend);
544 } catch (RemoteException e) {
545 e.printStackTrace();
546 }
547 }
Alexandre Lision4fb22622013-10-21 16:26:33 -0400548
549 public void updateCodecName(String currentAudioCodecName) {
550 codecNameTxt.setText(currentAudioCodecName);
551 }
alision84813a12013-05-27 17:40:39 -0400552}