blob: f40af91fa9a553ffff6b2d5c930957dd9a033b8c [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
alision1005ba12013-06-19 13:52:44 -0400104 @Override
105 public void onCreate(Bundle savedBundle) {
106 super.onCreate(savedBundle);
alision1005ba12013-06-19 13:52:44 -0400107 Bundle b = getArguments();
alisioncd8fb912013-06-28 14:43:51 -0400108 conf = new Conference((Conference) b.getParcelable("conference"));
alisiondf1dac92013-06-27 17:35:53 -0400109 model = new BubbleModel(getResources().getDisplayMetrics().density);
alision729b0a22013-07-02 11:57:33 -0400110 BUBBLE_SIZE = getResources().getDimension(R.dimen.bubble_size);
Alexandre Lisiona764c682013-09-09 10:02:07 -0400111 Log.e(TAG, "BUBBLE_SIZE " + BUBBLE_SIZE);
Alexandre Lisionf1850c02013-09-23 14:19:34 -0400112 mSensorManager = (SensorManager) getActivity().getSystemService(Context.SENSOR_SERVICE);
113 mSensor = mSensorManager.getDefaultSensor(Sensor.TYPE_PROXIMITY);
Alexandre Lisiond588bff2013-10-08 12:43:01 -0400114 this.setHasOptionsMenu(true);
alision729b0a22013-07-02 11:57:33 -0400115
alision1005ba12013-06-19 13:52:44 -0400116 }
alision85992112013-05-29 12:18:08 -0400117
alision1005ba12013-06-19 13:52:44 -0400118 /**
119 * A dummy implementation of the {@link Callbacks} interface that does nothing. Used only when this fragment is not attached to an activity.
120 */
121 private static Callbacks sDummyCallbacks = new Callbacks() {
alision85704182013-05-29 15:23:03 -0400122
Alexandre Lisionccf0c932013-10-10 16:24:41 -0400123
alision1005ba12013-06-19 13:52:44 -0400124 @Override
125 public ISipService getService() {
126 return null;
127 }
alisiondf1dac92013-06-27 17:35:53 -0400128
Alexandre Lisionccf0c932013-10-10 16:24:41 -0400129 @Override
130 public void replaceCurrentCallDisplayed() {
131 }
Alexandre Lision0c384512013-09-17 17:15:57 -0400132
133 @Override
Adrien Béraude78d06f2013-09-19 13:33:44 +1000134 public void startTimer() {
Alexandre Lision0c384512013-09-17 17:15:57 -0400135 }
Alexandre Lision68855472013-10-10 16:20:46 -0400136
137 @Override
138 public void slideChatScreen() {
139 }
alision1005ba12013-06-19 13:52:44 -0400140 };
alision907bde72013-06-20 14:40:37 -0400141
alision1005ba12013-06-19 13:52:44 -0400142 /**
143 * The Activity calling this fragment has to implement this interface
144 *
145 */
146 public interface Callbacks {
alision85992112013-05-29 12:18:08 -0400147
alision1005ba12013-06-19 13:52:44 -0400148 public ISipService getService();
alision85704182013-05-29 15:23:03 -0400149
Alexandre Lision0c384512013-09-17 17:15:57 -0400150 public void startTimer();
Alexandre Lision68855472013-10-10 16:20:46 -0400151
152 public void slideChatScreen();
Alexandre Lisionccf0c932013-10-10 16:24:41 -0400153
154 public void replaceCurrentCallDisplayed();
alision1005ba12013-06-19 13:52:44 -0400155 }
alision85992112013-05-29 12:18:08 -0400156
alision1005ba12013-06-19 13:52:44 -0400157 @Override
158 public void onAttach(Activity activity) {
159 super.onAttach(activity);
alision85992112013-05-29 12:18:08 -0400160
alision1005ba12013-06-19 13:52:44 -0400161 if (!(activity instanceof Callbacks)) {
162 throw new IllegalStateException("Activity must implement fragment's callbacks.");
163 }
alision85992112013-05-29 12:18:08 -0400164
alision1005ba12013-06-19 13:52:44 -0400165 // rootView.requestDisallowInterceptTouchEvent(true);
alision85992112013-05-29 12:18:08 -0400166
alision1005ba12013-06-19 13:52:44 -0400167 mCallbacks = (Callbacks) activity;
Alexandre Lision68855472013-10-10 16:20:46 -0400168// myself = SipCall.SipCallBuilder.buildMyselfCall(activity.getContentResolver(), "Me");
Adrien Béraude78d06f2013-09-19 13:33:44 +1000169
alision1005ba12013-06-19 13:52:44 -0400170 }
Alexandre Lisiond588bff2013-10-08 12:43:01 -0400171
172 @Override
173 public void onCreateOptionsMenu(Menu m, MenuInflater inf) {
174 super.onCreateOptionsMenu(m, inf);
Alexandre Lision68855472013-10-10 16:20:46 -0400175 inf.inflate(R.menu.ac_call, m);
Alexandre Lisiond588bff2013-10-08 12:43:01 -0400176 }
177
178 @Override
179 public boolean onOptionsItemSelected(MenuItem item) {
180 super.onOptionsItemSelected(item);
181 switch (item.getItemId()) {
Alexandre Lision68855472013-10-10 16:20:46 -0400182 case R.id.menuitem_chat:
183 mCallbacks.slideChatScreen();
Alexandre Lisiond588bff2013-10-08 12:43:01 -0400184 break;
185 }
186
187 return true;
188 }
alision85992112013-05-29 12:18:08 -0400189
alision1005ba12013-06-19 13:52:44 -0400190 @Override
191 public void onDetach() {
192 super.onDetach();
193 mCallbacks = sDummyCallbacks;
Alexandre Lisionf1850c02013-09-23 14:19:34 -0400194 mSensorManager.unregisterListener(this);
alision1005ba12013-06-19 13:52:44 -0400195 }
alision907bde72013-06-20 14:40:37 -0400196
alision1005ba12013-06-19 13:52:44 -0400197 @Override
alision907bde72013-06-20 14:40:37 -0400198 public void onStop() {
alision1005ba12013-06-19 13:52:44 -0400199 super.onStop();
200 }
alision85992112013-05-29 12:18:08 -0400201
alision1005ba12013-06-19 13:52:44 -0400202 @Override
Alexandre Lisionf1850c02013-09-23 14:19:34 -0400203 public void onResume() {
204 super.onResume();
205 mSensorManager.registerListener(this, mSensor, SensorManager.SENSOR_DELAY_NORMAL);
206 }
207
208 @Override
209 public void onPause() {
210 super.onPause();
211 mSensorManager.unregisterListener(this);
212 }
213
214 @Override
Alexandre Lision2b237922013-09-09 16:23:02 -0400215 public void onActivityResult(int requestCode, int resultCode, Intent data) {
216 super.onActivityResult(requestCode, resultCode, data);
217 SipCall transfer = null;
218 if (requestCode == REQUEST_TRANSFER) {
219 switch (resultCode) {
Adrien Béraude78d06f2013-09-19 13:33:44 +1000220 case TransferDFragment.RESULT_TRANSFER_CONF:
Alexandre Lision2b237922013-09-09 16:23:02 -0400221 Conference c = data.getParcelableExtra("target");
222 transfer = data.getParcelableExtra("transfer");
223 try {
224
225 mCallbacks.getService().attendedTransfer(transfer.getCallId(), c.getParticipants().get(0).getCallId());
226
227 } catch (RemoteException e) {
Alexandre Lision2b237922013-09-09 16:23:02 -0400228 e.printStackTrace();
229 }
Alexandre Lision2b237922013-09-09 16:23:02 -0400230 break;
231
Adrien Béraude78d06f2013-09-19 13:33:44 +1000232 case TransferDFragment.RESULT_TRANSFER_NUMBER:
Alexandre Lision2b237922013-09-09 16:23:02 -0400233 String to = data.getStringExtra("to_number");
234 transfer = data.getParcelableExtra("transfer");
235 try {
Alexandre Lision2b237922013-09-09 16:23:02 -0400236 mCallbacks.getService().transfer(transfer.getCallId(), to);
237 mCallbacks.getService().hangUp(transfer.getCallId());
238 } catch (RemoteException e) {
Alexandre Lision2b237922013-09-09 16:23:02 -0400239 e.printStackTrace();
240 }
241 break;
Adrien Béraud97512842013-09-19 13:37:19 +1000242 case Activity.RESULT_CANCELED:
Alexandre Lision2b237922013-09-09 16:23:02 -0400243 default:
Adrien Béraude78d06f2013-09-19 13:33:44 +1000244 model.clear();
245 initNormalStateDisplay();
Alexandre Lision2b237922013-09-09 16:23:02 -0400246 break;
247 }
248 }
249 }
250
251 @Override
alision1005ba12013-06-19 13:52:44 -0400252 public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
Alexandre Lision84208a32013-09-25 13:18:37 -0400253 final ViewGroup rootView = (ViewGroup) inflater.inflate(R.layout.frag_call, container, false);
alision85992112013-05-29 12:18:08 -0400254
alision1005ba12013-06-19 13:52:44 -0400255 view = (BubblesView) rootView.findViewById(R.id.main_view);
alision34673e62013-06-25 14:40:07 -0400256 view.setFragment(this);
alision1005ba12013-06-19 13:52:44 -0400257 view.setModel(model);
258 view.getHolder().addCallback(this);
alision84813a12013-05-27 17:40:39 -0400259
Alexandre Lision3c6b7102013-09-16 16:56:46 -0400260 callStatusTxt = (TextView) rootView.findViewById(R.id.call_status_txt);
Alexandre Lision68855472013-10-10 16:20:46 -0400261 call_icon = BitmapFactory.decodeResource(getResources(), R.drawable.ic_action_call);
alision84813a12013-05-27 17:40:39 -0400262
Alexandre Lision84208a32013-09-25 13:18:37 -0400263 ((ImageButton) rootView.findViewById(R.id.dialpad_btn)).setOnClickListener(new OnClickListener() {
264
265 @Override
266 public void onClick(View v) {
267 InputMethodManager lManager = (InputMethodManager) getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
268 lManager.toggleSoftInput(InputMethodManager.SHOW_IMPLICIT, InputMethodManager.HIDE_IMPLICIT_ONLY);
269 }
270 });
271
alision1005ba12013-06-19 13:52:44 -0400272 return rootView;
273 }
Alexandre Lisionc30e8412013-09-26 15:12:59 -0400274
alision1005ba12013-06-19 13:52:44 -0400275 private void initNormalStateDisplay() {
276 Log.i(TAG, "Start normal display");
alision84813a12013-05-27 17:40:39 -0400277
Alexandre Lision0c384512013-09-17 17:15:57 -0400278 mCallbacks.startTimer();
alision84813a12013-05-27 17:40:39 -0400279
Alexandre Lisionee2494d2013-10-09 17:14:00 -0400280 getBubbleForUser(conf, model.width / 2, model.height / 2);
alision907bde72013-06-20 14:40:37 -0400281
alisiondf1dac92013-06-27 17:35:53 -0400282 int angle_part = 360 / conf.getParticipants().size();
alision806e18e2013-06-21 15:30:17 -0400283 double dX = 0;
284 double dY = 0;
alision465ceba2013-07-04 09:24:30 -0400285 int radiusCalls = (int) (model.width / 2 - BUBBLE_SIZE);
alisiondf1dac92013-06-27 17:35:53 -0400286 for (int i = 0; i < conf.getParticipants().size(); ++i) {
287
288 if (conf.getParticipants().get(i) == null) {
alisiondf1dac92013-06-27 17:35:53 -0400289 continue;
290 }
alision34673e62013-06-25 14:40:07 -0400291 dX = Math.cos(Math.toRadians(angle_part * i - 90)) * radiusCalls;
292 dY = Math.sin(Math.toRadians(angle_part * i - 90)) * radiusCalls;
alisiondf1dac92013-06-27 17:35:53 -0400293 getBubbleFor(conf.getParticipants().get(i), (int) (model.width / 2 + dX), (int) (model.height / 2 + dY));
alision806e18e2013-06-21 15:30:17 -0400294 }
alision1005ba12013-06-19 13:52:44 -0400295
296 model.clearAttractors();
alision1005ba12013-06-19 13:52:44 -0400297 }
alision85704182013-05-29 15:23:03 -0400298
alision1005ba12013-06-19 13:52:44 -0400299 private void initIncomingCallDisplay() {
300 Log.i(TAG, "Start incoming display");
alision84813a12013-05-27 17:40:39 -0400301
Alexandre Lision0c384512013-09-17 17:15:57 -0400302 mCallbacks.startTimer();
alision84813a12013-05-27 17:40:39 -0400303
Alexandre Lision23628c12013-09-24 11:17:05 -0400304 int radiusCalls = (int) (model.width / 2 - BUBBLE_SIZE);
Alexandre Lisionee2494d2013-10-09 17:14:00 -0400305 getBubbleForUser(conf, model.width / 2, model.height / 2 + radiusCalls);
Alexandre Lision23628c12013-09-24 11:17:05 -0400306 getBubbleFor(conf.getParticipants().get(0), model.width / 2, model.height / 2 - radiusCalls);
alision85704182013-05-29 15:23:03 -0400307
alision1005ba12013-06-19 13:52:44 -0400308 model.clearAttractors();
Alexandre Lision23628c12013-09-24 11:17:05 -0400309 model.addAttractor(new Attractor(new PointF(model.width / 2, model.height / 2), ATTRACTOR_SIZE, new Attractor.Callback() {
alision1005ba12013-06-19 13:52:44 -0400310 @Override
311 public boolean onBubbleSucked(Bubble b) {
Alexandre Lisiona764c682013-09-09 10:02:07 -0400312
313 if (!accepted) {
Alexandre Lision68855472013-10-10 16:20:46 -0400314 try {
315 mCallbacks.getService().accept(b.getCallID());
316 } catch (RemoteException e) {
317 e.printStackTrace();
318 }
Alexandre Lisiona764c682013-09-09 10:02:07 -0400319 accepted = true;
320 }
alision1005ba12013-06-19 13:52:44 -0400321 return false;
322 }
323 }, call_icon));
alision1005ba12013-06-19 13:52:44 -0400324 }
alision85704182013-05-29 15:23:03 -0400325
alision1005ba12013-06-19 13:52:44 -0400326 private void initOutGoingCallDisplay() {
327 Log.i(TAG, "Start outgoing display");
alision85704182013-05-29 15:23:03 -0400328
Alexandre Lision0c384512013-09-17 17:15:57 -0400329 mCallbacks.startTimer();
alision85704182013-05-29 15:23:03 -0400330
Alexandre Lisionee2494d2013-10-09 17:14:00 -0400331 getBubbleForUser(conf, model.width / 2, model.height / 2);
alision85704182013-05-29 15:23:03 -0400332
alisiondf1dac92013-06-27 17:35:53 -0400333 // TODO off-thread image loading
334 int angle_part = 360 / conf.getParticipants().size();
335 double dX = 0;
336 double dY = 0;
Adrien Béraude78d06f2013-09-19 13:33:44 +1000337 int radiusCalls = (int) (model.width / 2 - BUBBLE_SIZE);
alisiondf1dac92013-06-27 17:35:53 -0400338 for (int i = 0; i < conf.getParticipants().size(); ++i) {
339 dX = Math.cos(Math.toRadians(angle_part * i - 90)) * radiusCalls;
340 dY = Math.sin(Math.toRadians(angle_part * i - 90)) * radiusCalls;
341 getBubbleFor(conf.getParticipants().get(i), (int) (model.width / 2 + dX), (int) (model.height / 2 + dY));
342 }
alision907bde72013-06-20 14:40:37 -0400343
alision1005ba12013-06-19 13:52:44 -0400344 model.clearAttractors();
alision1005ba12013-06-19 13:52:44 -0400345 }
alision85704182013-05-29 15:23:03 -0400346
alision1005ba12013-06-19 13:52:44 -0400347 /**
348 * Retrieves or create a bubble for a given contact. If the bubble exists, it is moved to the new location.
349 *
alision806e18e2013-06-21 15:30:17 -0400350 * @param call
351 * The call associated to a contact
alision1005ba12013-06-19 13:52:44 -0400352 * @param x
353 * Initial or new x position.
354 * @param y
355 * Initial or new y position.
356 * @return Bubble corresponding to the contact.
357 */
alision806e18e2013-06-21 15:30:17 -0400358 private Bubble getBubbleFor(SipCall call, float x, float y) {
Alexandre Lisionee2494d2013-10-09 17:14:00 -0400359 Bubble contact_bubble = model.getBubble(call.getCallId());
alision1005ba12013-06-19 13:52:44 -0400360 if (contact_bubble != null) {
Alexandre Lision68855472013-10-10 16:20:46 -0400361 ((BubbleContact)contact_bubble).setCall(call);
alision1005ba12013-06-19 13:52:44 -0400362 contact_bubble.attractor.set(x, y);
363 return contact_bubble;
364 }
alision84813a12013-05-27 17:40:39 -0400365
Alexandre Lision40954dc2013-10-09 15:24:03 -0400366 contact_bubble = new BubbleContact(getActivity(), call, x, y, BUBBLE_SIZE);
367
368 model.addBubble(contact_bubble);
369 return contact_bubble;
370 }
371
Alexandre Lisionee2494d2013-10-09 17:14:00 -0400372 private Bubble getBubbleForUser(Conference conf, float x, float y) {
Alexandre Lision68855472013-10-10 16:20:46 -0400373 Bubble contact_bubble = model.getBubble(conf.getId());
Alexandre Lision40954dc2013-10-09 15:24:03 -0400374 if (contact_bubble != null) {
375 contact_bubble.attractor.set(x, y);
Alexandre Lision68855472013-10-10 16:20:46 -0400376 ((BubbleUser)contact_bubble).setConference(conf);
Alexandre Lision40954dc2013-10-09 15:24:03 -0400377 return contact_bubble;
378 }
379
Alexandre Lision68855472013-10-10 16:20:46 -0400380 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 -0400381
alision1005ba12013-06-19 13:52:44 -0400382 model.addBubble(contact_bubble);
alision1005ba12013-06-19 13:52:44 -0400383 return contact_bubble;
384 }
Adrien Béraud0c9bd8f2013-05-30 16:16:57 +1000385
alision1005ba12013-06-19 13:52:44 -0400386 /**
387 * Should be called when a bubble is removed from the model
388 */
389 void bubbleRemoved(Bubble b) {
Alexandre Lision40954dc2013-10-09 15:24:03 -0400390// if (b.associated_call == null) {
391// return;
392// }
alision1005ba12013-06-19 13:52:44 -0400393 }
Adrien Béraudc9c424d2013-05-30 17:47:35 +1000394
alision1005ba12013-06-19 13:52:44 -0400395 public void changeCallState(String callID, String newState) {
Alexandre Lisionf1850c02013-09-23 14:19:34 -0400396 Log.w(TAG, "Call :" + callID + " " + newState);
alisiondf1dac92013-06-27 17:35:53 -0400397 if (newState.contentEquals("FAILURE")) {
398 try {
399 mCallbacks.getService().hangUp(callID);
400 } catch (RemoteException e) {
401 e.printStackTrace();
alision806e18e2013-06-21 15:30:17 -0400402 }
alision1005ba12013-06-19 13:52:44 -0400403 }
alisiondf1dac92013-06-27 17:35:53 -0400404 for (int i = 0; i < conf.getParticipants().size(); ++i) {
alisiondf1dac92013-06-27 17:35:53 -0400405 if (callID.equals(conf.getParticipants().get(i).getCallId())) {
406 if (newState.contentEquals("HUNGUP")) {
alisiondf1dac92013-06-27 17:35:53 -0400407 model.removeBubble(conf.getParticipants().get(i));
408 conf.getParticipants().remove(i);
409 } else {
410 conf.getParticipants().get(i).setCallState(newState);
411 }
412 }
413 }
414
415 if (conf.isOnGoing())
416 initNormalStateDisplay();
417
Alexandre Lisionccf0c932013-10-10 16:24:41 -0400418 if (conf.getParticipants().size() == 0) {
419 mCallbacks.replaceCurrentCallDisplayed();
420 }
alision1005ba12013-06-19 13:52:44 -0400421 }
alision84813a12013-05-27 17:40:39 -0400422
alision1005ba12013-06-19 13:52:44 -0400423 public boolean draggingBubble() {
424 return view == null ? false : view.isDraggingBubble();
425 }
Adrien Béraudc9c424d2013-05-30 17:47:35 +1000426
alision1005ba12013-06-19 13:52:44 -0400427 @Override
428 public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
Adrien Béraud13cde0b2013-05-30 20:27:20 +1000429
alisiondf1dac92013-06-27 17:35:53 -0400430 if (conf.getParticipants().size() == 1) {
alisiondf1dac92013-06-27 17:35:53 -0400431 if (conf.getParticipants().get(0).isIncoming() && conf.getParticipants().get(0).isRinging()) {
alision806e18e2013-06-21 15:30:17 -0400432 initIncomingCallDisplay();
433 } else {
alisiondf1dac92013-06-27 17:35:53 -0400434 if (conf.getParticipants().get(0).isRinging()) {
alision1005ba12013-06-19 13:52:44 -0400435 initOutGoingCallDisplay();
436 }
alision806e18e2013-06-21 15:30:17 -0400437 try {
alisiondf1dac92013-06-27 17:35:53 -0400438 if (conf.getParticipants().get(0).isOutGoing()
439 && mCallbacks.getService().getCall(conf.getParticipants().get(0).getCallId()) == null) {
440 mCallbacks.getService().placeCall(conf.getParticipants().get(0));
alision806e18e2013-06-21 15:30:17 -0400441 initOutGoingCallDisplay();
alisiondf1dac92013-06-27 17:35:53 -0400442 } else if (conf.getParticipants().get(0).isOutGoing() && conf.getParticipants().get(0).isRinging()) {
alision806e18e2013-06-21 15:30:17 -0400443 initOutGoingCallDisplay();
444 }
445 } catch (RemoteException e) {
446 Log.e(TAG, e.toString());
447 }
alision1005ba12013-06-19 13:52:44 -0400448 }
alisiondf1dac92013-06-27 17:35:53 -0400449 if (conf.getParticipants().get(0).isOngoing()) {
alision806e18e2013-06-21 15:30:17 -0400450 initNormalStateDisplay();
451 }
alisiondf1dac92013-06-27 17:35:53 -0400452 } else if (conf.getParticipants().size() > 1) {
alision1005ba12013-06-19 13:52:44 -0400453 initNormalStateDisplay();
454 }
alision1005ba12013-06-19 13:52:44 -0400455 }
Adrien Béraud13cde0b2013-05-30 20:27:20 +1000456
Alexandre Lision40954dc2013-10-09 15:24:03 -0400457 public void makeTransfer(BubbleContact contact) {
Alexandre Lision2b237922013-09-09 16:23:02 -0400458 FragmentManager fm = getFragmentManager();
Alexandre Lision0eb02032013-09-27 16:32:40 -0400459 editName = TransferDFragment.newInstance();
Alexandre Lision2b237922013-09-09 16:23:02 -0400460 Bundle b = new Bundle();
Alexandre Lision6ae652d2013-09-26 16:39:20 -0400461 try {
462 b.putParcelableArrayList("calls", (ArrayList<Conference>)mCallbacks.getService().getConcurrentCalls());
463 b.putParcelable("call_selected", contact.associated_call);
464 editName.setArguments(b);
465 editName.setTargetFragment(this, REQUEST_TRANSFER);
466 editName.show(fm, "");
467 } catch (RemoteException e) {
468 Log.e(TAG, e.toString());
469 }
470
Alexandre Lision2b237922013-09-09 16:23:02 -0400471 }
472
alision1005ba12013-06-19 13:52:44 -0400473 @Override
474 public void surfaceCreated(SurfaceHolder holder) {
Alexandre Lisionc30e8412013-09-26 15:12:59 -0400475
alision1005ba12013-06-19 13:52:44 -0400476 }
Adrien Béraud13cde0b2013-05-30 20:27:20 +1000477
alision1005ba12013-06-19 13:52:44 -0400478 @Override
479 public void surfaceDestroyed(SurfaceHolder holder) {
Alexandre Lision84208a32013-09-25 13:18:37 -0400480 // check that soft input is hidden
481 InputMethodManager lManager = (InputMethodManager) getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
482 lManager.hideSoftInputFromWindow(view.getWindowToken(), 0);
Alexandre Lision0eb02032013-09-27 16:32:40 -0400483 if(editName != null && editName.isVisible()){
484 editName.dismiss();
485 }
alision1005ba12013-06-19 13:52:44 -0400486 }
Adrien Béraud13cde0b2013-05-30 20:27:20 +1000487
alisione38001f2013-06-04 14:14:39 -0400488 public BubblesView getBubbleView() {
489 return view;
alision1005ba12013-06-19 13:52:44 -0400490 }
Alexandre Lision0c384512013-09-17 17:15:57 -0400491
492 public void updateTime() {
Adrien Béraude78d06f2013-09-19 13:33:44 +1000493 long duration = System.currentTimeMillis() / 1000 - this.conf.getParticipants().get(0).getTimestamp_start();
Alexandre Lisionf1850c02013-09-23 14:19:34 -0400494 callStatusTxt.setText(String.format("%d:%02d:%02d", duration / 3600, duration % 3600 / 60, duration % 60));
Alexandre Lisionf1850c02013-09-23 14:19:34 -0400495 }
496
497 @Override
498 public void onAccuracyChanged(Sensor sensor, int accuracy) {
Alexandre Lisionf1850c02013-09-23 14:19:34 -0400499
500 }
501
502 @Override
503 public void onSensorChanged(SensorEvent event) {
Alexandre Lisionc30e8412013-09-26 15:12:59 -0400504 if (event.values[0] == 0) {
505 WindowManager.LayoutParams params = getActivity().getWindow().getAttributes();
506 params.flags |= WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON;
507 params.flags = WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE;
508 params.screenBrightness = 0.004f;
509 getActivity().getWindow().setAttributes(params);
Adrien Béraude78d06f2013-09-19 13:33:44 +1000510
Alexandre Lisionc30e8412013-09-26 15:12:59 -0400511 } else {
512 WindowManager.LayoutParams params = getActivity().getWindow().getAttributes();
513 getActivity().getWindow().clearFlags(WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE);
514 params.flags = WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON;
515 params.screenBrightness = 1.0f;
516 getActivity().getWindow().setAttributes(params);
517 }
Alexandre Lision0c384512013-09-17 17:15:57 -0400518 }
Alexandre Lision0edf18c2013-09-23 17:35:50 -0400519
520 public Conference getConference() {
521 return conf;
522 }
Alexandre Lision64dc8c02013-09-25 15:32:25 -0400523
524 public void onKeyUp(int keyCode, KeyEvent event) {
525 try {
Alexandre Lision80f0a7e2013-09-26 12:27:51 -0400526 String toSend = Character.toString(event.getDisplayLabel());
Alexandre Lision31f46fc2013-09-26 11:19:54 -0400527 toSend.toUpperCase(Locale.getDefault());
Alexandre Lisionc30e8412013-09-26 15:12:59 -0400528 Log.d(TAG, "toSend " + toSend);
Alexandre Lision64dc8c02013-09-25 15:32:25 -0400529 mCallbacks.getService().playDtmf(toSend);
530 } catch (RemoteException e) {
531 e.printStackTrace();
532 }
533 }
alision84813a12013-05-27 17:40:39 -0400534}