blob: d0eac58b94ae1d05e9f089c2d1e7a06345c4420d [file] [log] [blame]
Alexandre Savard14323be2012-10-24 10:02:13 -04001/*
2 * Copyright (C) 2004-2012 Savoir-Faire Linux Inc.
3 *
4 * Author: Alexandre Savard <alexandre.savard@savoirfairelinux.com>
Adrien Béraud71b2f812013-04-26 18:51:02 +10005 * Author: Adrien Béraud <adrien.beraud@savoirfairelinux.com>
alisionfde875f2013-05-28 17:01:54 -04006 * Author: Alexandre Lision <alexandre.lision@savoirfairelinux.com>
Alexandre Savard14323be2012-10-24 10:02:13 -04007 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 3 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 *
22 * Additional permission under GNU GPL version 3 section 7:
23 *
24 * If you modify this program, or any covered work, by linking or
25 * combining it with the OpenSSL project's OpenSSL library (or a
26 * modified version of that library), containing parts covered by the
27 * terms of the OpenSSL or SSLeay licenses, Savoir-Faire Linux Inc.
28 * grants you additional permission to convey the resulting work.
29 * Corresponding Source for a non-source form of such a combination
30 * shall include the source code for the parts of OpenSSL used as well
31 * as that of the covered work.
32 */
33
34package com.savoirfairelinux.sflphone.client;
35
alision85704182013-05-29 15:23:03 -040036import java.util.HashMap;
Adrien Béraud33268882013-05-18 03:41:15 +100037import java.util.concurrent.ExecutorService;
38import java.util.concurrent.Executors;
39
Alexandre Savard14323be2012-10-24 10:02:13 -040040import android.app.Activity;
Alexandre Savard6d54bbc2012-10-24 11:04:23 -040041import android.content.ComponentName;
alision17052d42013-04-22 10:39:38 -040042import android.content.Context;
Alexandre Savard6d54bbc2012-10-24 11:04:23 -040043import android.content.Intent;
alision84813a12013-05-27 17:40:39 -040044import android.content.IntentFilter;
Alexandre Savard6d54bbc2012-10-24 11:04:23 -040045import android.content.ServiceConnection;
Alexandre Savard14323be2012-10-24 10:02:13 -040046import android.os.Bundle;
alision85992112013-05-29 12:18:08 -040047import android.os.Environment;
Alexandre Savard6d54bbc2012-10-24 11:04:23 -040048import android.os.IBinder;
alision85992112013-05-29 12:18:08 -040049import android.os.RemoteException;
alisionfde875f2013-05-28 17:01:54 -040050import android.support.v4.widget.SlidingPaneLayout;
Alexandre Savard6d54bbc2012-10-24 11:04:23 -040051import android.util.Log;
alisionfde875f2013-05-28 17:01:54 -040052import android.view.View;
Adrien Béraud33268882013-05-18 03:41:15 +100053import android.widget.Toast;
Alexandre Savard14323be2012-10-24 10:02:13 -040054
55import com.savoirfairelinux.sflphone.R;
alision84813a12013-05-27 17:40:39 -040056import com.savoirfairelinux.sflphone.client.receiver.CallReceiver;
57import com.savoirfairelinux.sflphone.fragments.CallFragment;
alisionfde875f2013-05-28 17:01:54 -040058import com.savoirfairelinux.sflphone.fragments.CallListFragment;
alision84813a12013-05-27 17:40:39 -040059import com.savoirfairelinux.sflphone.interfaces.CallInterface;
alisionf76de3b2013-04-16 15:35:22 -040060import com.savoirfairelinux.sflphone.model.SipCall;
alision85992112013-05-29 12:18:08 -040061import com.savoirfairelinux.sflphone.model.SipCall.state;
alision84813a12013-05-27 17:40:39 -040062import com.savoirfairelinux.sflphone.service.CallManagerCallBack;
Alexandre Savard6d54bbc2012-10-24 11:04:23 -040063import com.savoirfairelinux.sflphone.service.ISipService;
64import com.savoirfairelinux.sflphone.service.SipService;
Adrien Béraudc9c424d2013-05-30 17:47:35 +100065import com.savoirfairelinux.sflphone.views.CallPaneLayout;
Alexandre Savard14323be2012-10-24 10:02:13 -040066
alisionfde875f2013-05-28 17:01:54 -040067public class CallActivity extends Activity implements CallInterface, CallFragment.Callbacks, CallListFragment.Callbacks {
Adrien Béraudc9c424d2013-05-30 17:47:35 +100068 static final String TAG = "CallActivity";
69 private ISipService service;
alision84813a12013-05-27 17:40:39 -040070
Adrien Béraudc9c424d2013-05-30 17:47:35 +100071 private ExecutorService infos_fetcher = Executors.newCachedThreadPool();
72 CallReceiver receiver;
alision85992112013-05-29 12:18:08 -040073
Adrien Béraudc9c424d2013-05-30 17:47:35 +100074 CallPaneLayout slidingPaneLayout;
Adrien Béraud33268882013-05-18 03:41:15 +100075
Adrien Béraudc9c424d2013-05-30 17:47:35 +100076 CallListFragment mCallsFragment;
77 CallFragment mCurrentCallFragment;
Alexandre Savard4f42ade2012-10-24 18:03:31 -040078
Adrien Béraudc9c424d2013-05-30 17:47:35 +100079 @Override
80 protected void onCreate(Bundle savedInstanceState) {
81 super.onCreate(savedInstanceState);
82 setContentView(R.layout.activity_call_layout);
Alexandre Savard4f42ade2012-10-24 18:03:31 -040083
Adrien Béraudc9c424d2013-05-30 17:47:35 +100084 receiver = new CallReceiver(this);
Adrien Béraud33268882013-05-18 03:41:15 +100085
Adrien Béraudc9c424d2013-05-30 17:47:35 +100086 mCallsFragment = new CallListFragment();
Adrien Béraud33268882013-05-18 03:41:15 +100087
Adrien Béraudc9c424d2013-05-30 17:47:35 +100088 getFragmentManager().beginTransaction().replace(R.id.calllist_pane, mCallsFragment).commit();
alision85992112013-05-29 12:18:08 -040089
Adrien Béraudc9c424d2013-05-30 17:47:35 +100090 slidingPaneLayout = (CallPaneLayout) findViewById(R.id.slidingpanelayout);
Adrien Béraud13cde0b2013-05-30 20:27:20 +100091 //slidingPaneLayout.
Adrien Béraudc9c424d2013-05-30 17:47:35 +100092 // slidingPaneLayout.requestDisallowInterceptTouchEvent(disallowIntercept)
93 slidingPaneLayout.setPanelSlideListener(new SlidingPaneLayout.PanelSlideListener() {
alision84813a12013-05-27 17:40:39 -040094
Adrien Béraudc9c424d2013-05-30 17:47:35 +100095 @Override
96 public void onPanelSlide(View view, float offSet) {
97 }
alisionfde875f2013-05-28 17:01:54 -040098
Adrien Béraudc9c424d2013-05-30 17:47:35 +100099 @Override
100 public void onPanelOpened(View view) {
alisionfde875f2013-05-28 17:01:54 -0400101
Adrien Béraudc9c424d2013-05-30 17:47:35 +1000102 switch (view.getId()) {
103 case R.id.calllist_pane:
104 // getFragmentManager().findFragmentById(R.id.calllist_pane).setHasOptionsMenu(true);
105 // getFragmentManager().findFragmentById(R.id.ongoingcall_pane).setHasOptionsMenu(false);
106 break;
107 default:
108 break;
109 }
110 }
alisionfde875f2013-05-28 17:01:54 -0400111
Adrien Béraudc9c424d2013-05-30 17:47:35 +1000112 @Override
113 public void onPanelClosed(View view) {
alisionfde875f2013-05-28 17:01:54 -0400114
Adrien Béraudc9c424d2013-05-30 17:47:35 +1000115 switch (view.getId()) {
116 case R.id.ongoingcall_pane:
117 // getFragmentManager().findFragmentById(R.id.calllist_pane).setHasOptionsMenu(false);
118 // getFragmentManager().findFragmentById(R.id.ongoingcall_pane).setHasOptionsMenu(true);
119 break;
120 default:
121 break;
122 }
123 }
124 });
alisionfde875f2013-05-28 17:01:54 -0400125
Adrien Béraudc9c424d2013-05-30 17:47:35 +1000126 Bundle b = getIntent().getExtras();
Adrien Béraud33268882013-05-18 03:41:15 +1000127
Adrien Béraudc9c424d2013-05-30 17:47:35 +1000128 Intent intent = new Intent(this, SipService.class);
alisiond45da712013-05-30 09:18:49 -0400129
Adrien Béraudc9c424d2013-05-30 17:47:35 +1000130 // setCallStateDisplay(mCall.getCallStateString());
alisiond45da712013-05-30 09:18:49 -0400131
Adrien Béraudc9c424d2013-05-30 17:47:35 +1000132 bindService(intent, mConnection, Context.BIND_AUTO_CREATE);
alisiond45da712013-05-30 09:18:49 -0400133
Adrien Béraudc9c424d2013-05-30 17:47:35 +1000134 }
Alexandre Savard6d54bbc2012-10-24 11:04:23 -0400135
Adrien Béraudc9c424d2013-05-30 17:47:35 +1000136 /* activity gets back to the foreground and user input */
137 @Override
138 protected void onResume() {
139 Log.i(TAG, "onResume");
140 IntentFilter intentFilter = new IntentFilter();
141 intentFilter.addAction(CallManagerCallBack.INCOMING_CALL);
142 intentFilter.addAction(CallManagerCallBack.INCOMING_TEXT);
143 intentFilter.addAction(CallManagerCallBack.CALL_STATE_CHANGED);
144 registerReceiver(receiver, intentFilter);
145 super.onResume();
146 }
Alexandre Savard6d54bbc2012-10-24 11:04:23 -0400147
Adrien Béraudc9c424d2013-05-30 17:47:35 +1000148 /* activity no more in foreground */
149 @Override
150 protected void onPause() {
151 super.onPause();
152 unregisterReceiver(receiver);
alision2cb99562013-05-30 17:02:20 -0400153 try {
154 service.createNotification();
155 } catch (RemoteException e) {
156 Log.e(TAG, e.toString());
157 }
Adrien Béraudc9c424d2013-05-30 17:47:35 +1000158 }
Adrien Béraud6bbce912013-05-24 00:48:13 +1000159
Adrien Béraudc9c424d2013-05-30 17:47:35 +1000160 @Override
161 protected void onDestroy() {
162 // Log.i(TAG, "Destroying Call Activity for call " + mCall.getCallId());
163 // LocalBroadcastManager.getInstance(this).unregisterReceiver(mMessageReceiver);
alision2cb99562013-05-30 17:02:20 -0400164 try {
165 service.destroyNotification();
166 } catch (RemoteException e) {
167 Log.e(TAG, e.toString());
168 }
Adrien Béraudc9c424d2013-05-30 17:47:35 +1000169 unbindService(mConnection);
Adrien Béraud33268882013-05-18 03:41:15 +1000170
Adrien Béraudc9c424d2013-05-30 17:47:35 +1000171 super.onDestroy();
172 }
Adrien Béraud33268882013-05-18 03:41:15 +1000173
Adrien Béraudc9c424d2013-05-30 17:47:35 +1000174 /** Defines callbacks for service binding, passed to bindService() */
175 private ServiceConnection mConnection = new ServiceConnection() {
176 @Override
177 public void onServiceConnected(ComponentName className, IBinder binder) {
178 service = ISipService.Stub.asInterface(binder);
179 Log.i(TAG, "Placing call");
180 mCurrentCallFragment = new CallFragment();
181 mCurrentCallFragment.setArguments(getIntent().getExtras());
182 slidingPaneLayout.curFragment = mCurrentCallFragment;
183 getIntent().getExtras();
alision2cb99562013-05-30 17:02:20 -0400184 mCallsFragment.update();
Adrien Béraudc9c424d2013-05-30 17:47:35 +1000185 getFragmentManager().beginTransaction().replace(R.id.ongoingcall_pane, mCurrentCallFragment).commit();
Adrien Béraud33268882013-05-18 03:41:15 +1000186
Adrien Béraudc9c424d2013-05-30 17:47:35 +1000187 }
Alexandre Savard6d54bbc2012-10-24 11:04:23 -0400188
Adrien Béraudc9c424d2013-05-30 17:47:35 +1000189 @Override
190 public void onServiceDisconnected(ComponentName arg0) {
191 }
192 };
alisiond8c83882013-05-17 17:00:42 -0400193
Adrien Béraudc9c424d2013-05-30 17:47:35 +1000194 @Override
195 public void incomingCall(Intent call) {
196 Toast.makeText(this, "New Call incoming", Toast.LENGTH_LONG).show();
Adrien Béraud6bbce912013-05-24 00:48:13 +1000197
Adrien Béraudc9c424d2013-05-30 17:47:35 +1000198 mCallsFragment.update();
Adrien Béraud6bbce912013-05-24 00:48:13 +1000199
Adrien Béraudc9c424d2013-05-30 17:47:35 +1000200 }
Alexandre Savarde41f5212012-10-26 14:23:50 -0400201
Adrien Béraudc9c424d2013-05-30 17:47:35 +1000202 @Override
203 public void callStateChanged(Intent callState) {
Alexandre Savarddf544262012-10-25 14:24:08 -0400204
Adrien Béraudc9c424d2013-05-30 17:47:35 +1000205 Bundle b = callState.getBundleExtra("com.savoirfairelinux.sflphone.service.newstate");
206 processCallStateChangedSignal(b.getString("CallID"), b.getString("State"));
alision84813a12013-05-27 17:40:39 -0400207
Adrien Béraudc9c424d2013-05-30 17:47:35 +1000208 }
alision84813a12013-05-27 17:40:39 -0400209
Adrien Béraudc9c424d2013-05-30 17:47:35 +1000210 public void processCallStateChangedSignal(String callID, String newState) {
211 /*
212 * Bundle bundle = intent.getBundleExtra("com.savoirfairelinux.sflphone.service.newstate"); String callID = bundle.getString("CallID"); String
213 * newState = bundle.getString("State");
214 */
215 // CallFragment fr = mCurrentCallFragment;
alision84813a12013-05-27 17:40:39 -0400216
Adrien Béraudc9c424d2013-05-30 17:47:35 +1000217 mCallsFragment.update();
alision84813a12013-05-27 17:40:39 -0400218
Adrien Béraudc9c424d2013-05-30 17:47:35 +1000219 mCurrentCallFragment.changeCallState(callID, newState);
alision84813a12013-05-27 17:40:39 -0400220
Adrien Béraudc9c424d2013-05-30 17:47:35 +1000221 HashMap<String, SipCall> map;
222 try {
223 map = (HashMap<String, SipCall>) service.getCallList();
224 if(map.size() == 0){
225 finish();
226 }
227 } catch (RemoteException e) {
228 Log.e(TAG, e.toString());
229 }
alision84813a12013-05-27 17:40:39 -0400230
Adrien Béraud29556042013-04-26 17:35:43 +1000231
Adrien Béraudc9c424d2013-05-30 17:47:35 +1000232 Log.w(TAG, "processCallStateChangedSignal " + newState);
Adrien Béraud29556042013-04-26 17:35:43 +1000233
Adrien Béraudc9c424d2013-05-30 17:47:35 +1000234 }
Adrien Béraud71b2f812013-04-26 18:51:02 +1000235
Adrien Béraudc9c424d2013-05-30 17:47:35 +1000236 @Override
237 public void incomingText(Intent msg) {
238 Toast.makeText(this, "New Call incoming", Toast.LENGTH_LONG).show();
Adrien Béraud29556042013-04-26 17:35:43 +1000239
Adrien Béraudc9c424d2013-05-30 17:47:35 +1000240 // TODO link text message to associate call and display it at the right place
Adrien Béraud71b2f812013-04-26 18:51:02 +1000241
Adrien Béraudc9c424d2013-05-30 17:47:35 +1000242 }
alision7f18fc82013-05-01 09:37:33 -0400243
Adrien Béraudc9c424d2013-05-30 17:47:35 +1000244 @Override
245 public ISipService getService() {
246 return service;
247 }
alision04a00182013-05-10 17:05:29 -0400248
Adrien Béraudc9c424d2013-05-30 17:47:35 +1000249 @Override
250 public void onCallSelected(SipCall call) {
251 mCurrentCallFragment = new CallFragment();
252 Bundle b = new Bundle();
253 b.putParcelable("CallInfo", call);
254 mCurrentCallFragment.setArguments(b);
255 getFragmentManager().beginTransaction().replace(R.id.ongoingcall_pane, mCurrentCallFragment).commit();
alision85992112013-05-29 12:18:08 -0400256
Adrien Béraudc9c424d2013-05-30 17:47:35 +1000257 slidingPaneLayout.curFragment = mCurrentCallFragment;
alision2cb99562013-05-30 17:02:20 -0400258 slidingPaneLayout.closePane();
alision85992112013-05-29 12:18:08 -0400259
Adrien Béraudc9c424d2013-05-30 17:47:35 +1000260 }
alision85992112013-05-29 12:18:08 -0400261
Adrien Béraudc9c424d2013-05-30 17:47:35 +1000262 @Override
263 public void callContact(SipCall call) {
264 try {
265 service.placeCall(call);
266 } catch (RemoteException e) {
267 Log.e(TAG, "Cannot call service method", e);
268 }
alision85992112013-05-29 12:18:08 -0400269
Adrien Béraudc9c424d2013-05-30 17:47:35 +1000270 }
alision85992112013-05-29 12:18:08 -0400271
Adrien Béraudc9c424d2013-05-30 17:47:35 +1000272 @Override
273 public void onCallAccepted(SipCall call) {
274 int callState = call.getCallStateInt();
275 if (callState != state.CALL_STATE_RINGING && callState != state.CALL_STATE_NONE) {
276 return;
277 }
alision85992112013-05-29 12:18:08 -0400278
Adrien Béraudc9c424d2013-05-30 17:47:35 +1000279 try {
280 service.accept(call.getCallId());
281 } catch (RemoteException e) {
282 Log.e(TAG, "Cannot call service method", e);
283 }
alision85992112013-05-29 12:18:08 -0400284
Adrien Béraudc9c424d2013-05-30 17:47:35 +1000285 }
alision85992112013-05-29 12:18:08 -0400286
Adrien Béraudc9c424d2013-05-30 17:47:35 +1000287 @Override
288 public void onCallRejected(SipCall call) {
289 try {
290 if (call.getCallStateInt() == state.CALL_STATE_RINGING) {
291 service.refuse(call.getCallId());
292 return;
293 }
294 } catch (RemoteException e) {
295 Log.e(TAG, "Cannot call service method", e);
296 }
297 }
alision85992112013-05-29 12:18:08 -0400298
Adrien Béraudc9c424d2013-05-30 17:47:35 +1000299 @Override
300 public void onCallEnded(SipCall call) {
301 try {
302 if (call.getCallStateInt() == state.CALL_STATE_NONE || call.getCallStateInt() == state.CALL_STATE_CURRENT
303 || call.getCallStateInt() == state.CALL_STATE_HOLD) {
304 service.hangUp(call.getCallId());
305 return;
alision85992112013-05-29 12:18:08 -0400306
Adrien Béraudc9c424d2013-05-30 17:47:35 +1000307 } else if (call.getCallStateInt() == state.CALL_STATE_RINGING) {
308 if (call.getCallType() == state.CALL_TYPE_INCOMING) {
309 service.refuse(call.getCallId());
310 return;
311 } else if (call.getCallType() == state.CALL_TYPE_OUTGOING) {
312 service.hangUp(call.getCallId());
313 return;
314 }
315 }
316 } catch (RemoteException e) {
317 Log.e(TAG, "Cannot call service method", e);
318 }
319 }
alision85992112013-05-29 12:18:08 -0400320
Adrien Béraudc9c424d2013-05-30 17:47:35 +1000321 @Override
322 public void onCallSuspended(SipCall call) {
323 try {
324 if (call.getCallStateInt() == state.CALL_STATE_CURRENT) {
325 service.hold(call.getCallId());
326 return;
327 }
328 } catch (RemoteException e) {
329 Log.e(TAG, "Cannot call service method", e);
330 }
331 }
alision85992112013-05-29 12:18:08 -0400332
Adrien Béraudc9c424d2013-05-30 17:47:35 +1000333 @Override
334 public void onCallResumed(SipCall call) {
335 try {
336 if (call.getCallStateInt() == state.CALL_STATE_HOLD) {
337 service.unhold(call.getCallId());
338 return;
339 }
340 } catch (RemoteException e) {
341 Log.e(TAG, "Cannot call service method", e);
342 }
alision85992112013-05-29 12:18:08 -0400343
alision85992112013-05-29 12:18:08 -0400344
Adrien Béraudc9c424d2013-05-30 17:47:35 +1000345 }
alision85992112013-05-29 12:18:08 -0400346
Adrien Béraudc9c424d2013-05-30 17:47:35 +1000347 @Override
348 public void onCalltransfered(SipCall call,String to) {
349 try {
350 if (call.getCallStateInt() == state.CALL_STATE_CURRENT) {
351 service.transfer(call.getCallId(), to);
352 }
353 } catch (RemoteException e) {
354 Log.e(TAG, "Cannot call service method", e);
355 }
alision85992112013-05-29 12:18:08 -0400356
Adrien Béraudc9c424d2013-05-30 17:47:35 +1000357 }
alision85992112013-05-29 12:18:08 -0400358
Adrien Béraudc9c424d2013-05-30 17:47:35 +1000359 @Override
360 public void onRecordCall(SipCall call) {
361 try {
362 if (call.getCallStateInt() == state.CALL_STATE_CURRENT) {
363 service.setRecordPath(Environment.getExternalStorageDirectory().getAbsolutePath());
364 Log.w(TAG, "Recording path" + service.getRecordPath());
365 service.setRecordingCall(call.getCallId());
366 }
367 } catch (RemoteException e) {
368 Log.e(TAG, "Cannot call service method", e);
369 }
alision85992112013-05-29 12:18:08 -0400370
Adrien Béraudc9c424d2013-05-30 17:47:35 +1000371 }
alision85992112013-05-29 12:18:08 -0400372
Adrien Béraudc9c424d2013-05-30 17:47:35 +1000373 @Override
374 public void onSendMessage(SipCall call, String msg) {
375 try {
376 if (call.getCallStateInt() == state.CALL_STATE_CURRENT) {
377 service.sendTextMessage(call.getCallId(), msg, "Me");
378 }
379 } catch (RemoteException e) {
380 Log.e(TAG, "Cannot call service method", e);
381 }
alision85992112013-05-29 12:18:08 -0400382
Adrien Béraudc9c424d2013-05-30 17:47:35 +1000383 }
alisiond45da712013-05-30 09:18:49 -0400384
alisiond45da712013-05-30 09:18:49 -0400385
Alexandre Savard14323be2012-10-24 10:02:13 -0400386}