blob: 194e560e0f2289f503da75183fad78cb8b137a72 [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;
alision55c36cb2013-06-14 14:57:38 -040046import android.net.Uri;
Alexandre Savard14323be2012-10-24 10:02:13 -040047import android.os.Bundle;
alision85992112013-05-29 12:18:08 -040048import android.os.Environment;
Alexandre Savard6d54bbc2012-10-24 11:04:23 -040049import android.os.IBinder;
alision85992112013-05-29 12:18:08 -040050import android.os.RemoteException;
alisionfde875f2013-05-28 17:01:54 -040051import android.support.v4.widget.SlidingPaneLayout;
Alexandre Savard6d54bbc2012-10-24 11:04:23 -040052import android.util.Log;
alisionfde875f2013-05-28 17:01:54 -040053import android.view.View;
Adrien Béraud33268882013-05-18 03:41:15 +100054import android.widget.Toast;
Alexandre Savard14323be2012-10-24 10:02:13 -040055
56import com.savoirfairelinux.sflphone.R;
alision84813a12013-05-27 17:40:39 -040057import 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;
alision55c36cb2013-06-14 14:57:38 -040060import com.savoirfairelinux.sflphone.model.CallContact;
alisionf76de3b2013-04-16 15:35:22 -040061import com.savoirfairelinux.sflphone.model.SipCall;
alision85992112013-05-29 12:18:08 -040062import com.savoirfairelinux.sflphone.model.SipCall.state;
alisioncc7bb422013-06-06 15:31:39 -040063import com.savoirfairelinux.sflphone.receivers.CallReceiver;
alision84813a12013-05-27 17:40:39 -040064import com.savoirfairelinux.sflphone.service.CallManagerCallBack;
Alexandre Savard6d54bbc2012-10-24 11:04:23 -040065import com.savoirfairelinux.sflphone.service.ISipService;
66import com.savoirfairelinux.sflphone.service.SipService;
Adrien Béraudc9c424d2013-05-30 17:47:35 +100067import com.savoirfairelinux.sflphone.views.CallPaneLayout;
Alexandre Savard14323be2012-10-24 10:02:13 -040068
alisionfde875f2013-05-28 17:01:54 -040069public class CallActivity extends Activity implements CallInterface, CallFragment.Callbacks, CallListFragment.Callbacks {
alision55c36cb2013-06-14 14:57:38 -040070 static final String TAG = "CallActivity";
71 private ISipService service;
alision84813a12013-05-27 17:40:39 -040072
alision55c36cb2013-06-14 14:57:38 -040073 private ExecutorService infos_fetcher = Executors.newCachedThreadPool();
74 CallReceiver receiver;
alision85992112013-05-29 12:18:08 -040075
alision55c36cb2013-06-14 14:57:38 -040076 CallPaneLayout slidingPaneLayout;
Adrien Béraud33268882013-05-18 03:41:15 +100077
alision55c36cb2013-06-14 14:57:38 -040078 CallListFragment mCallsFragment;
79 CallFragment mCurrentCallFragment;
Alexandre Savard4f42ade2012-10-24 18:03:31 -040080
alision55c36cb2013-06-14 14:57:38 -040081 @Override
82 protected void onCreate(Bundle savedInstanceState) {
83 super.onCreate(savedInstanceState);
84 setContentView(R.layout.activity_call_layout);
Alexandre Savard4f42ade2012-10-24 18:03:31 -040085
alision55c36cb2013-06-14 14:57:38 -040086 receiver = new CallReceiver(this);
Adrien Béraud33268882013-05-18 03:41:15 +100087
alision55c36cb2013-06-14 14:57:38 -040088 mCallsFragment = new CallListFragment();
Adrien Béraud33268882013-05-18 03:41:15 +100089
alision55c36cb2013-06-14 14:57:38 -040090 getFragmentManager().beginTransaction().replace(R.id.calllist_pane, mCallsFragment).commit();
alision85992112013-05-29 12:18:08 -040091
alision55c36cb2013-06-14 14:57:38 -040092 slidingPaneLayout = (CallPaneLayout) findViewById(R.id.slidingpanelayout);
93 // slidingPaneLayout.
94 // slidingPaneLayout.requestDisallowInterceptTouchEvent(disallowIntercept)
95 // Toast.makeText(this, getIntent().getData().toString(), Toast.LENGTH_LONG).show();
96 slidingPaneLayout.setPanelSlideListener(new SlidingPaneLayout.PanelSlideListener() {
alision84813a12013-05-27 17:40:39 -040097
alision55c36cb2013-06-14 14:57:38 -040098 @Override
99 public void onPanelSlide(View view, float offSet) {
100 }
alisionfde875f2013-05-28 17:01:54 -0400101
alision55c36cb2013-06-14 14:57:38 -0400102 @Override
103 public void onPanelOpened(View view) {
alisionfde875f2013-05-28 17:01:54 -0400104
alision55c36cb2013-06-14 14:57:38 -0400105 switch (view.getId()) {
106 case R.id.calllist_pane:
107 // getFragmentManager().findFragmentById(R.id.calllist_pane).setHasOptionsMenu(true);
108 // getFragmentManager().findFragmentById(R.id.ongoingcall_pane).setHasOptionsMenu(false);
109 break;
110 default:
111 break;
112 }
113 }
alisionfde875f2013-05-28 17:01:54 -0400114
alision55c36cb2013-06-14 14:57:38 -0400115 @Override
116 public void onPanelClosed(View view) {
alisionfde875f2013-05-28 17:01:54 -0400117
alision55c36cb2013-06-14 14:57:38 -0400118 switch (view.getId()) {
119 case R.id.ongoingcall_pane:
120 Log.i(TAG, "PANEL CLOSED DRAWING SHOULD RESTART");
121 mCurrentCallFragment.getBubbleView().restartDrawing();
122 break;
123 default:
124 break;
125 }
126 }
127 });
alisionfde875f2013-05-28 17:01:54 -0400128
alision55c36cb2013-06-14 14:57:38 -0400129 Intent intent = new Intent(this, SipService.class);
130 bindService(intent, mConnection, Context.BIND_AUTO_CREATE);
Adrien Béraud33268882013-05-18 03:41:15 +1000131
alision55c36cb2013-06-14 14:57:38 -0400132 }
alisiond45da712013-05-30 09:18:49 -0400133
alision55c36cb2013-06-14 14:57:38 -0400134 /* activity gets back to the foreground and user input */
135 @Override
136 protected void onResume() {
137 Log.i(TAG, "onResume");
138 IntentFilter intentFilter = new IntentFilter();
139 intentFilter.addAction(CallManagerCallBack.INCOMING_CALL);
140 intentFilter.addAction(CallManagerCallBack.INCOMING_TEXT);
141 intentFilter.addAction(CallManagerCallBack.CALL_STATE_CHANGED);
142 registerReceiver(receiver, intentFilter);
143 super.onResume();
144 }
alisiond45da712013-05-30 09:18:49 -0400145
alision55c36cb2013-06-14 14:57:38 -0400146 /* activity no more in foreground */
147 @Override
148 protected void onPause() {
149 super.onPause();
alisiond45da712013-05-30 09:18:49 -0400150
alision55c36cb2013-06-14 14:57:38 -0400151 }
Alexandre Savard6d54bbc2012-10-24 11:04:23 -0400152
alision55c36cb2013-06-14 14:57:38 -0400153 @Override
154 protected void onDestroy() {
Alexandre Savard6d54bbc2012-10-24 11:04:23 -0400155
alision55c36cb2013-06-14 14:57:38 -0400156 unregisterReceiver(receiver);
157
158 try {
alision2cb99562013-05-30 17:02:20 -0400159 service.createNotification();
160 } catch (RemoteException e) {
161 Log.e(TAG, e.toString());
162 }
alision55c36cb2013-06-14 14:57:38 -0400163 // Log.i(TAG, "Destroying Call Activity for call " + mCall.getCallId());
164 // LocalBroadcastManager.getInstance(this).unregisterReceiver(mMessageReceiver);
165 try {
alision2cb99562013-05-30 17:02:20 -0400166 service.destroyNotification();
167 } catch (RemoteException e) {
168 Log.e(TAG, e.toString());
169 }
alision55c36cb2013-06-14 14:57:38 -0400170 unbindService(mConnection);
Adrien Béraud33268882013-05-18 03:41:15 +1000171
alision55c36cb2013-06-14 14:57:38 -0400172 super.onDestroy();
173 }
Adrien Béraud33268882013-05-18 03:41:15 +1000174
alision55c36cb2013-06-14 14:57:38 -0400175 /** 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 Uri u = getIntent().getData();
183 if (u != null) {
184 CallContact c = CallContact.ContactBuilder.buildUnknownContact(u.getSchemeSpecificPart());
185 try {
186 service.destroyNotification();
187 SipCall call = SipCall.SipCallBuilder.getInstance().startCallCreation().addContact(c)
188 .setAccountID(service.getAccountList().get(1).toString()).setCallType(SipCall.state.CALL_TYPE_OUTGOING).build();
189 Bundle b = new Bundle();
190 b.putParcelable("CallInfo", call);
191 mCurrentCallFragment.setArguments(b);
192 } catch (RemoteException e) {
193 // TODO Auto-generated catch block
194 e.printStackTrace();
195 } catch (Exception e) {
196 // TODO Auto-generated catch block
197 e.printStackTrace();
198 }
Adrien Béraud33268882013-05-18 03:41:15 +1000199
alision55c36cb2013-06-14 14:57:38 -0400200 } else {
201 mCurrentCallFragment.setArguments(getIntent().getExtras());
202 }
Alexandre Savard6d54bbc2012-10-24 11:04:23 -0400203
alision55c36cb2013-06-14 14:57:38 -0400204 slidingPaneLayout.setCurFragment(mCurrentCallFragment);
205 getIntent().getExtras();
206 mCallsFragment.update();
207 getFragmentManager().beginTransaction().replace(R.id.ongoingcall_pane, mCurrentCallFragment).commit();
alisiond8c83882013-05-17 17:00:42 -0400208
alision55c36cb2013-06-14 14:57:38 -0400209 }
Adrien Béraud6bbce912013-05-24 00:48:13 +1000210
alision55c36cb2013-06-14 14:57:38 -0400211 @Override
212 public void onServiceDisconnected(ComponentName arg0) {
213 }
214 };
Adrien Béraud6bbce912013-05-24 00:48:13 +1000215
alision55c36cb2013-06-14 14:57:38 -0400216 @Override
217 public void incomingCall(Intent call) {
218 Toast.makeText(this, "New Call incoming", Toast.LENGTH_LONG).show();
Alexandre Savarde41f5212012-10-26 14:23:50 -0400219
alision55c36cb2013-06-14 14:57:38 -0400220 mCallsFragment.update();
Alexandre Savarddf544262012-10-25 14:24:08 -0400221
alision55c36cb2013-06-14 14:57:38 -0400222 }
alision84813a12013-05-27 17:40:39 -0400223
alision55c36cb2013-06-14 14:57:38 -0400224 @Override
225 public void callStateChanged(Intent callState) {
alision84813a12013-05-27 17:40:39 -0400226
alision55c36cb2013-06-14 14:57:38 -0400227 Bundle b = callState.getBundleExtra("com.savoirfairelinux.sflphone.service.newstate");
228 processCallStateChangedSignal(b.getString("CallID"), b.getString("State"));
alision84813a12013-05-27 17:40:39 -0400229
alision55c36cb2013-06-14 14:57:38 -0400230 }
alision84813a12013-05-27 17:40:39 -0400231
alision55c36cb2013-06-14 14:57:38 -0400232 public void processCallStateChangedSignal(String callID, String newState) {
233 /*
234 * Bundle bundle = intent.getBundleExtra("com.savoirfairelinux.sflphone.service.newstate"); String callID = bundle.getString("CallID"); String
235 * newState = bundle.getString("State");
236 */
237 // CallFragment fr = mCurrentCallFragment;
alision84813a12013-05-27 17:40:39 -0400238
alision55c36cb2013-06-14 14:57:38 -0400239 mCallsFragment.update();
alision84813a12013-05-27 17:40:39 -0400240
alision55c36cb2013-06-14 14:57:38 -0400241 mCurrentCallFragment.changeCallState(callID, newState);
Adrien Béraud29556042013-04-26 17:35:43 +1000242
alision55c36cb2013-06-14 14:57:38 -0400243 HashMap<String, SipCall> map;
244 try {
245 map = (HashMap<String, SipCall>) service.getCallList();
246 if (map.size() == 0) {
247 finish();
248 }
249 } catch (RemoteException e) {
250 Log.e(TAG, e.toString());
251 }
Adrien Béraud29556042013-04-26 17:35:43 +1000252
alision55c36cb2013-06-14 14:57:38 -0400253 Log.w(TAG, "processCallStateChangedSignal " + newState);
Adrien Béraud71b2f812013-04-26 18:51:02 +1000254
alision55c36cb2013-06-14 14:57:38 -0400255 }
256
257 @Override
258 public void incomingText(Intent msg) {
259 Bundle b = msg.getBundleExtra("com.savoirfairelinux.sflphone.service.newtext");
260
alisiona2a2da12013-06-05 15:58:39 -0400261 Toast.makeText(this, b.getString("From") + " : " + b.getString("Msg"), Toast.LENGTH_LONG).show();
Adrien Béraud71b2f812013-04-26 18:51:02 +1000262
alision55c36cb2013-06-14 14:57:38 -0400263 }
alision7f18fc82013-05-01 09:37:33 -0400264
alision55c36cb2013-06-14 14:57:38 -0400265 @Override
266 public ISipService getService() {
267 return service;
268 }
alision04a00182013-05-10 17:05:29 -0400269
alision55c36cb2013-06-14 14:57:38 -0400270 @Override
271 public void onCallSelected(SipCall call) {
alision85992112013-05-29 12:18:08 -0400272
alision55c36cb2013-06-14 14:57:38 -0400273 mCurrentCallFragment.getBubbleView().restartDrawing();
274 mCurrentCallFragment = new CallFragment();
275 Bundle b = new Bundle();
276 b.putParcelable("CallInfo", call);
277 mCurrentCallFragment.setArguments(b);
278 getFragmentManager().beginTransaction().replace(R.id.ongoingcall_pane, mCurrentCallFragment).commit();
alision85992112013-05-29 12:18:08 -0400279
alision55c36cb2013-06-14 14:57:38 -0400280 onCallResumed(call);
281 slidingPaneLayout.setCurFragment(mCurrentCallFragment);
282 slidingPaneLayout.closePane();
alision85992112013-05-29 12:18:08 -0400283
alision55c36cb2013-06-14 14:57:38 -0400284 }
alision85992112013-05-29 12:18:08 -0400285
alision55c36cb2013-06-14 14:57:38 -0400286 @Override
287 public void callContact(SipCall call) {
288 try {
289 service.placeCall(call);
290 } catch (RemoteException e) {
291 Log.e(TAG, "Cannot call service method", e);
292 }
alision85992112013-05-29 12:18:08 -0400293
alision55c36cb2013-06-14 14:57:38 -0400294 }
alision85992112013-05-29 12:18:08 -0400295
alision55c36cb2013-06-14 14:57:38 -0400296 @Override
297 public void onCallAccepted(SipCall call) {
298 int callState = call.getCallStateInt();
299 if (callState != state.CALL_STATE_RINGING && callState != state.CALL_STATE_NONE) {
300 return;
301 }
alision85992112013-05-29 12:18:08 -0400302
alision55c36cb2013-06-14 14:57:38 -0400303 try {
304 service.accept(call.getCallId());
305 } catch (RemoteException e) {
306 Log.e(TAG, "Cannot call service method", e);
307 }
alision85992112013-05-29 12:18:08 -0400308
alision55c36cb2013-06-14 14:57:38 -0400309 }
alision85992112013-05-29 12:18:08 -0400310
alision55c36cb2013-06-14 14:57:38 -0400311 @Override
312 public void onCallRejected(SipCall call) {
313 try {
314 if (call.getCallStateInt() == state.CALL_STATE_RINGING) {
315 service.refuse(call.getCallId());
316 return;
317 }
318 } catch (RemoteException e) {
319 Log.e(TAG, "Cannot call service method", e);
320 }
321 }
alision85992112013-05-29 12:18:08 -0400322
alision55c36cb2013-06-14 14:57:38 -0400323 @Override
324 public void onCallEnded(SipCall call) {
325 try {
326 if (call.getCallStateInt() == state.CALL_STATE_NONE || call.getCallStateInt() == state.CALL_STATE_CURRENT
327 || call.getCallStateInt() == state.CALL_STATE_HOLD) {
328 service.hangUp(call.getCallId());
329 return;
alision85992112013-05-29 12:18:08 -0400330
alision55c36cb2013-06-14 14:57:38 -0400331 } else if (call.getCallStateInt() == state.CALL_STATE_RINGING) {
332 if (call.getCallType() == state.CALL_TYPE_INCOMING) {
333 service.refuse(call.getCallId());
334 return;
335 } else if (call.getCallType() == state.CALL_TYPE_OUTGOING) {
336 service.hangUp(call.getCallId());
337 return;
338 }
339 }
340 } catch (RemoteException e) {
341 Log.e(TAG, "Cannot call service method", e);
342 }
343 }
alision85992112013-05-29 12:18:08 -0400344
alision55c36cb2013-06-14 14:57:38 -0400345 @Override
346 public void onCallSuspended(SipCall call) {
347 try {
348 if (call.getCallStateInt() == state.CALL_STATE_CURRENT) {
349 service.hold(call.getCallId());
350 return;
351 }
352 } catch (RemoteException e) {
353 Log.e(TAG, "Cannot call service method", e);
354 }
355 }
alision85992112013-05-29 12:18:08 -0400356
alision55c36cb2013-06-14 14:57:38 -0400357 @Override
358 public void onCallResumed(SipCall call) {
359 try {
360 if (call.getCallStateInt() == state.CALL_STATE_HOLD) {
361 service.unhold(call.getCallId());
362 return;
363 }
364 } catch (RemoteException e) {
365 Log.e(TAG, "Cannot call service method", e);
366 }
alision85992112013-05-29 12:18:08 -0400367
alision55c36cb2013-06-14 14:57:38 -0400368 }
alision85992112013-05-29 12:18:08 -0400369
alision55c36cb2013-06-14 14:57:38 -0400370 @Override
371 public void onCalltransfered(SipCall call, String to) {
372 try {
373 if (call.getCallStateInt() == state.CALL_STATE_CURRENT) {
374 service.transfer(call.getCallId(), to);
375 }
376 } catch (RemoteException e) {
377 Log.e(TAG, "Cannot call service method", e);
378 }
alision85992112013-05-29 12:18:08 -0400379
alision55c36cb2013-06-14 14:57:38 -0400380 }
alision85992112013-05-29 12:18:08 -0400381
alision55c36cb2013-06-14 14:57:38 -0400382 @Override
383 public void onRecordCall(SipCall call) {
384 try {
385 if (call.getCallStateInt() == state.CALL_STATE_CURRENT) {
386 service.setRecordPath(Environment.getExternalStorageDirectory().getAbsolutePath());
387 Log.w(TAG, "Recording path" + service.getRecordPath());
388 service.setRecordingCall(call.getCallId());
389 }
390 } catch (RemoteException e) {
391 Log.e(TAG, "Cannot call service method", e);
392 }
alision85992112013-05-29 12:18:08 -0400393
alision55c36cb2013-06-14 14:57:38 -0400394 }
alision85992112013-05-29 12:18:08 -0400395
alision55c36cb2013-06-14 14:57:38 -0400396 @Override
397 public void onBackPressed() {
398 super.onBackPressed();
399 Intent launchHome = new Intent(this, SFLPhoneHomeActivity.class);
400 launchHome.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
401 startActivity(launchHome);
402 }
alision85992112013-05-29 12:18:08 -0400403
alision55c36cb2013-06-14 14:57:38 -0400404 @Override
405 public void onSendMessage(SipCall call, String msg) {
406 try {
407 if (call.getCallStateInt() == state.CALL_STATE_CURRENT) {
408 service.sendTextMessage(call.getCallId(), msg, "Me");
409 }
410 } catch (RemoteException e) {
411 Log.e(TAG, "Cannot call service method", e);
412 }
alisiond45da712013-05-30 09:18:49 -0400413
alision55c36cb2013-06-14 14:57:38 -0400414 }
alisiond45da712013-05-30 09:18:49 -0400415
Alexandre Savard14323be2012-10-24 10:02:13 -0400416}