blob: a07421f84be259d09978de556a97eae7ed4bb9c1 [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.fragments.CallFragment;
alisionfde875f2013-05-28 17:01:54 -040057import com.savoirfairelinux.sflphone.fragments.CallListFragment;
alision84813a12013-05-27 17:40:39 -040058import com.savoirfairelinux.sflphone.interfaces.CallInterface;
alisionf76de3b2013-04-16 15:35:22 -040059import com.savoirfairelinux.sflphone.model.SipCall;
alision85992112013-05-29 12:18:08 -040060import com.savoirfairelinux.sflphone.model.SipCall.state;
alisioncc7bb422013-06-06 15:31:39 -040061import com.savoirfairelinux.sflphone.receivers.CallReceiver;
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)
alisioncc7bb422013-06-06 15:31:39 -040093// Toast.makeText(this, getIntent().getData().toString(), Toast.LENGTH_LONG).show();
Adrien Béraudc9c424d2013-05-30 17:47:35 +100094 slidingPaneLayout.setPanelSlideListener(new SlidingPaneLayout.PanelSlideListener() {
alision84813a12013-05-27 17:40:39 -040095
Adrien Béraudc9c424d2013-05-30 17:47:35 +100096 @Override
97 public void onPanelSlide(View view, float offSet) {
98 }
alisionfde875f2013-05-28 17:01:54 -040099
Adrien Béraudc9c424d2013-05-30 17:47:35 +1000100 @Override
101 public void onPanelOpened(View view) {
alisionfde875f2013-05-28 17:01:54 -0400102
Adrien Béraudc9c424d2013-05-30 17:47:35 +1000103 switch (view.getId()) {
104 case R.id.calllist_pane:
105 // getFragmentManager().findFragmentById(R.id.calllist_pane).setHasOptionsMenu(true);
106 // getFragmentManager().findFragmentById(R.id.ongoingcall_pane).setHasOptionsMenu(false);
107 break;
108 default:
109 break;
110 }
111 }
alisionfde875f2013-05-28 17:01:54 -0400112
Adrien Béraudc9c424d2013-05-30 17:47:35 +1000113 @Override
114 public void onPanelClosed(View view) {
alisionfde875f2013-05-28 17:01:54 -0400115
Adrien Béraudc9c424d2013-05-30 17:47:35 +1000116 switch (view.getId()) {
117 case R.id.ongoingcall_pane:
alisione38001f2013-06-04 14:14:39 -0400118 Log.i(TAG,"PANEL CLOSED DRAWING SHOULD RESTART");
119 mCurrentCallFragment.getBubbleView().restartDrawing();
Adrien Béraudc9c424d2013-05-30 17:47:35 +1000120 break;
121 default:
122 break;
123 }
124 }
125 });
alisionfde875f2013-05-28 17:01:54 -0400126
Adrien Béraudc9c424d2013-05-30 17:47:35 +1000127 Bundle b = getIntent().getExtras();
Adrien Béraud33268882013-05-18 03:41:15 +1000128
Adrien Béraudc9c424d2013-05-30 17:47:35 +1000129 Intent intent = new Intent(this, SipService.class);
alisiond45da712013-05-30 09:18:49 -0400130
Adrien Béraudc9c424d2013-05-30 17:47:35 +1000131 // setCallStateDisplay(mCall.getCallStateString());
alisiond45da712013-05-30 09:18:49 -0400132
Adrien Béraudc9c424d2013-05-30 17:47:35 +1000133 bindService(intent, mConnection, Context.BIND_AUTO_CREATE);
alisiond45da712013-05-30 09:18:49 -0400134
Adrien Béraudc9c424d2013-05-30 17:47:35 +1000135 }
Alexandre Savard6d54bbc2012-10-24 11:04:23 -0400136
Adrien Béraudc9c424d2013-05-30 17:47:35 +1000137 /* activity gets back to the foreground and user input */
138 @Override
139 protected void onResume() {
140 Log.i(TAG, "onResume");
141 IntentFilter intentFilter = new IntentFilter();
142 intentFilter.addAction(CallManagerCallBack.INCOMING_CALL);
143 intentFilter.addAction(CallManagerCallBack.INCOMING_TEXT);
144 intentFilter.addAction(CallManagerCallBack.CALL_STATE_CHANGED);
145 registerReceiver(receiver, intentFilter);
146 super.onResume();
147 }
Alexandre Savard6d54bbc2012-10-24 11:04:23 -0400148
Adrien Béraudc9c424d2013-05-30 17:47:35 +1000149 /* activity no more in foreground */
150 @Override
151 protected void onPause() {
152 super.onPause();
153 unregisterReceiver(receiver);
alision2cb99562013-05-30 17:02:20 -0400154 try {
155 service.createNotification();
156 } catch (RemoteException e) {
157 Log.e(TAG, e.toString());
158 }
Adrien Béraudc9c424d2013-05-30 17:47:35 +1000159 }
Adrien Béraud6bbce912013-05-24 00:48:13 +1000160
Adrien Béraudc9c424d2013-05-30 17:47:35 +1000161 @Override
162 protected void onDestroy() {
163 // Log.i(TAG, "Destroying Call Activity for call " + mCall.getCallId());
164 // LocalBroadcastManager.getInstance(this).unregisterReceiver(mMessageReceiver);
alision2cb99562013-05-30 17:02:20 -0400165 try {
166 service.destroyNotification();
167 } catch (RemoteException e) {
168 Log.e(TAG, e.toString());
169 }
Adrien Béraudc9c424d2013-05-30 17:47:35 +1000170 unbindService(mConnection);
Adrien Béraud33268882013-05-18 03:41:15 +1000171
Adrien Béraudc9c424d2013-05-30 17:47:35 +1000172 super.onDestroy();
173 }
Adrien Béraud33268882013-05-18 03:41:15 +1000174
Adrien Béraudc9c424d2013-05-30 17:47:35 +1000175 /** Defines callbacks for service binding, passed to bindService() */
176 private ServiceConnection mConnection = new ServiceConnection() {
177 @Override
178 public void onServiceConnected(ComponentName className, IBinder binder) {
179 service = ISipService.Stub.asInterface(binder);
180 Log.i(TAG, "Placing call");
181 mCurrentCallFragment = new CallFragment();
182 mCurrentCallFragment.setArguments(getIntent().getExtras());
alision58356b72013-06-03 17:13:36 -0400183 slidingPaneLayout.setCurFragment(mCurrentCallFragment);
Adrien Béraudc9c424d2013-05-30 17:47:35 +1000184 getIntent().getExtras();
alision2cb99562013-05-30 17:02:20 -0400185 mCallsFragment.update();
Adrien Béraudc9c424d2013-05-30 17:47:35 +1000186 getFragmentManager().beginTransaction().replace(R.id.ongoingcall_pane, mCurrentCallFragment).commit();
Adrien Béraud33268882013-05-18 03:41:15 +1000187
Adrien Béraudc9c424d2013-05-30 17:47:35 +1000188 }
Alexandre Savard6d54bbc2012-10-24 11:04:23 -0400189
Adrien Béraudc9c424d2013-05-30 17:47:35 +1000190 @Override
191 public void onServiceDisconnected(ComponentName arg0) {
192 }
193 };
alisiond8c83882013-05-17 17:00:42 -0400194
Adrien Béraudc9c424d2013-05-30 17:47:35 +1000195 @Override
196 public void incomingCall(Intent call) {
197 Toast.makeText(this, "New Call incoming", Toast.LENGTH_LONG).show();
Adrien Béraud6bbce912013-05-24 00:48:13 +1000198
Adrien Béraudc9c424d2013-05-30 17:47:35 +1000199 mCallsFragment.update();
Adrien Béraud6bbce912013-05-24 00:48:13 +1000200
Adrien Béraudc9c424d2013-05-30 17:47:35 +1000201 }
Alexandre Savarde41f5212012-10-26 14:23:50 -0400202
Adrien Béraudc9c424d2013-05-30 17:47:35 +1000203 @Override
204 public void callStateChanged(Intent callState) {
Alexandre Savarddf544262012-10-25 14:24:08 -0400205
Adrien Béraudc9c424d2013-05-30 17:47:35 +1000206 Bundle b = callState.getBundleExtra("com.savoirfairelinux.sflphone.service.newstate");
207 processCallStateChangedSignal(b.getString("CallID"), b.getString("State"));
alision84813a12013-05-27 17:40:39 -0400208
Adrien Béraudc9c424d2013-05-30 17:47:35 +1000209 }
alision84813a12013-05-27 17:40:39 -0400210
Adrien Béraudc9c424d2013-05-30 17:47:35 +1000211 public void processCallStateChangedSignal(String callID, String newState) {
212 /*
213 * Bundle bundle = intent.getBundleExtra("com.savoirfairelinux.sflphone.service.newstate"); String callID = bundle.getString("CallID"); String
214 * newState = bundle.getString("State");
215 */
216 // CallFragment fr = mCurrentCallFragment;
alision84813a12013-05-27 17:40:39 -0400217
Adrien Béraudc9c424d2013-05-30 17:47:35 +1000218 mCallsFragment.update();
alision84813a12013-05-27 17:40:39 -0400219
Adrien Béraudc9c424d2013-05-30 17:47:35 +1000220 mCurrentCallFragment.changeCallState(callID, newState);
alision84813a12013-05-27 17:40:39 -0400221
Adrien Béraudc9c424d2013-05-30 17:47:35 +1000222 HashMap<String, SipCall> map;
223 try {
224 map = (HashMap<String, SipCall>) service.getCallList();
225 if(map.size() == 0){
226 finish();
227 }
228 } catch (RemoteException e) {
229 Log.e(TAG, e.toString());
230 }
alision84813a12013-05-27 17:40:39 -0400231
Adrien Béraud29556042013-04-26 17:35:43 +1000232
Adrien Béraudc9c424d2013-05-30 17:47:35 +1000233 Log.w(TAG, "processCallStateChangedSignal " + newState);
Adrien Béraud29556042013-04-26 17:35:43 +1000234
Adrien Béraudc9c424d2013-05-30 17:47:35 +1000235 }
Adrien Béraud71b2f812013-04-26 18:51:02 +1000236
Adrien Béraudc9c424d2013-05-30 17:47:35 +1000237 @Override
238 public void incomingText(Intent msg) {
alisiona2a2da12013-06-05 15:58:39 -0400239 Bundle b = msg.getBundleExtra("com.savoirfairelinux.sflphone.service.newtext");
240
241 Toast.makeText(this, b.getString("From") + " : " + b.getString("Msg"), Toast.LENGTH_LONG).show();
Adrien Béraud71b2f812013-04-26 18:51:02 +1000242
Adrien Béraudc9c424d2013-05-30 17:47:35 +1000243 }
alision7f18fc82013-05-01 09:37:33 -0400244
Adrien Béraudc9c424d2013-05-30 17:47:35 +1000245 @Override
246 public ISipService getService() {
247 return service;
248 }
alision04a00182013-05-10 17:05:29 -0400249
Adrien Béraudc9c424d2013-05-30 17:47:35 +1000250 @Override
251 public void onCallSelected(SipCall call) {
alisiona2a2da12013-06-05 15:58:39 -0400252
253 mCurrentCallFragment.getBubbleView().restartDrawing();
Adrien Béraudc9c424d2013-05-30 17:47:35 +1000254 mCurrentCallFragment = new CallFragment();
255 Bundle b = new Bundle();
256 b.putParcelable("CallInfo", call);
257 mCurrentCallFragment.setArguments(b);
258 getFragmentManager().beginTransaction().replace(R.id.ongoingcall_pane, mCurrentCallFragment).commit();
alision85992112013-05-29 12:18:08 -0400259
alision58356b72013-06-03 17:13:36 -0400260 onCallResumed(call);
261 slidingPaneLayout.setCurFragment(mCurrentCallFragment);
alision2cb99562013-05-30 17:02:20 -0400262 slidingPaneLayout.closePane();
alision85992112013-05-29 12:18:08 -0400263
Adrien Béraudc9c424d2013-05-30 17:47:35 +1000264 }
alision85992112013-05-29 12:18:08 -0400265
Adrien Béraudc9c424d2013-05-30 17:47:35 +1000266 @Override
267 public void callContact(SipCall call) {
268 try {
269 service.placeCall(call);
270 } catch (RemoteException e) {
271 Log.e(TAG, "Cannot call service method", e);
272 }
alision85992112013-05-29 12:18:08 -0400273
Adrien Béraudc9c424d2013-05-30 17:47:35 +1000274 }
alision85992112013-05-29 12:18:08 -0400275
Adrien Béraudc9c424d2013-05-30 17:47:35 +1000276 @Override
277 public void onCallAccepted(SipCall call) {
278 int callState = call.getCallStateInt();
279 if (callState != state.CALL_STATE_RINGING && callState != state.CALL_STATE_NONE) {
280 return;
281 }
alision85992112013-05-29 12:18:08 -0400282
Adrien Béraudc9c424d2013-05-30 17:47:35 +1000283 try {
284 service.accept(call.getCallId());
285 } catch (RemoteException e) {
286 Log.e(TAG, "Cannot call service method", e);
287 }
alision85992112013-05-29 12:18:08 -0400288
Adrien Béraudc9c424d2013-05-30 17:47:35 +1000289 }
alision85992112013-05-29 12:18:08 -0400290
Adrien Béraudc9c424d2013-05-30 17:47:35 +1000291 @Override
292 public void onCallRejected(SipCall call) {
293 try {
294 if (call.getCallStateInt() == state.CALL_STATE_RINGING) {
295 service.refuse(call.getCallId());
296 return;
297 }
298 } catch (RemoteException e) {
299 Log.e(TAG, "Cannot call service method", e);
300 }
301 }
alision85992112013-05-29 12:18:08 -0400302
Adrien Béraudc9c424d2013-05-30 17:47:35 +1000303 @Override
304 public void onCallEnded(SipCall call) {
305 try {
306 if (call.getCallStateInt() == state.CALL_STATE_NONE || call.getCallStateInt() == state.CALL_STATE_CURRENT
307 || call.getCallStateInt() == state.CALL_STATE_HOLD) {
308 service.hangUp(call.getCallId());
309 return;
alision85992112013-05-29 12:18:08 -0400310
Adrien Béraudc9c424d2013-05-30 17:47:35 +1000311 } else if (call.getCallStateInt() == state.CALL_STATE_RINGING) {
312 if (call.getCallType() == state.CALL_TYPE_INCOMING) {
313 service.refuse(call.getCallId());
314 return;
315 } else if (call.getCallType() == state.CALL_TYPE_OUTGOING) {
316 service.hangUp(call.getCallId());
317 return;
318 }
319 }
320 } catch (RemoteException e) {
321 Log.e(TAG, "Cannot call service method", e);
322 }
323 }
alision85992112013-05-29 12:18:08 -0400324
Adrien Béraudc9c424d2013-05-30 17:47:35 +1000325 @Override
326 public void onCallSuspended(SipCall call) {
327 try {
328 if (call.getCallStateInt() == state.CALL_STATE_CURRENT) {
329 service.hold(call.getCallId());
330 return;
331 }
332 } catch (RemoteException e) {
333 Log.e(TAG, "Cannot call service method", e);
334 }
335 }
alision85992112013-05-29 12:18:08 -0400336
Adrien Béraudc9c424d2013-05-30 17:47:35 +1000337 @Override
338 public void onCallResumed(SipCall call) {
339 try {
340 if (call.getCallStateInt() == state.CALL_STATE_HOLD) {
341 service.unhold(call.getCallId());
342 return;
343 }
344 } catch (RemoteException e) {
345 Log.e(TAG, "Cannot call service method", e);
346 }
alision85992112013-05-29 12:18:08 -0400347
alision85992112013-05-29 12:18:08 -0400348
Adrien Béraudc9c424d2013-05-30 17:47:35 +1000349 }
alision85992112013-05-29 12:18:08 -0400350
Adrien Béraudc9c424d2013-05-30 17:47:35 +1000351 @Override
352 public void onCalltransfered(SipCall call,String to) {
353 try {
354 if (call.getCallStateInt() == state.CALL_STATE_CURRENT) {
355 service.transfer(call.getCallId(), to);
356 }
357 } catch (RemoteException e) {
358 Log.e(TAG, "Cannot call service method", e);
359 }
alision85992112013-05-29 12:18:08 -0400360
Adrien Béraudc9c424d2013-05-30 17:47:35 +1000361 }
alision85992112013-05-29 12:18:08 -0400362
Adrien Béraudc9c424d2013-05-30 17:47:35 +1000363 @Override
364 public void onRecordCall(SipCall call) {
365 try {
366 if (call.getCallStateInt() == state.CALL_STATE_CURRENT) {
367 service.setRecordPath(Environment.getExternalStorageDirectory().getAbsolutePath());
368 Log.w(TAG, "Recording path" + service.getRecordPath());
369 service.setRecordingCall(call.getCallId());
370 }
371 } catch (RemoteException e) {
372 Log.e(TAG, "Cannot call service method", e);
373 }
alision85992112013-05-29 12:18:08 -0400374
Adrien Béraudc9c424d2013-05-30 17:47:35 +1000375 }
alision85992112013-05-29 12:18:08 -0400376
Adrien Béraudc9c424d2013-05-30 17:47:35 +1000377 @Override
378 public void onSendMessage(SipCall call, String msg) {
379 try {
380 if (call.getCallStateInt() == state.CALL_STATE_CURRENT) {
381 service.sendTextMessage(call.getCallId(), msg, "Me");
382 }
383 } catch (RemoteException e) {
384 Log.e(TAG, "Cannot call service method", e);
385 }
alision85992112013-05-29 12:18:08 -0400386
Adrien Béraudc9c424d2013-05-30 17:47:35 +1000387 }
alisiond45da712013-05-30 09:18:49 -0400388
alisiond45da712013-05-30 09:18:49 -0400389
Alexandre Savard14323be2012-10-24 10:02:13 -0400390}