blob: 42423902a2367d54113484cd440a812e9de797a4 [file] [log] [blame]
Alexandre Savard14323be2012-10-24 10:02:13 -04001/*
Alexandre Lisionc1024c02014-01-06 11:12:53 -05002 * Copyright (C) 2004-2014 Savoir-Faire Linux Inc.
Alexandre Savard14323be2012-10-24 10:02:13 -04003 *
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
Alexandre Lision064e1e02013-10-01 16:18:42 -040034package org.sflphone.client;
Alexandre Savard14323be2012-10-24 10:02:13 -040035
Alexandre Lisiond5686032013-10-29 11:09:21 -040036import java.util.ArrayList;
alision85704182013-05-29 15:23:03 -040037import java.util.HashMap;
Alexandre Lisione4b95842013-11-12 10:36:51 -050038import java.util.Timer;
39import java.util.TimerTask;
Adrien Béraud33268882013-05-18 03:41:15 +100040
Alexandre Lision5f144b82014-02-11 09:59:36 -050041import android.support.v4.app.FragmentActivity;
Alexandre Lision064e1e02013-10-01 16:18:42 -040042import org.sflphone.R;
43import org.sflphone.fragments.CallFragment;
Alexandre Lision666b3772013-10-28 17:42:48 -040044import org.sflphone.fragments.IMFragment;
Alexandre Lisiond588bff2013-10-08 12:43:01 -040045import org.sflphone.model.Account;
Alexandre Lision064e1e02013-10-01 16:18:42 -040046import org.sflphone.model.CallContact;
47import org.sflphone.model.Conference;
48import org.sflphone.model.SipCall;
Alexandre Lision666b3772013-10-28 17:42:48 -040049import org.sflphone.model.SipMessage;
Alexandre Lision064e1e02013-10-01 16:18:42 -040050import org.sflphone.service.ISipService;
51import org.sflphone.service.SipService;
Alexandre Lisionb8add812013-10-24 11:42:42 -040052import org.sflphone.utils.CallProximityManager;
53import org.sflphone.utils.CallProximityManager.ProximityDirector;
Alexandre Lision064e1e02013-10-01 16:18:42 -040054import org.sflphone.views.CallPaneLayout;
55
Alexandre Savard6d54bbc2012-10-24 11:04:23 -040056import android.content.ComponentName;
alision17052d42013-04-22 10:39:38 -040057import android.content.Context;
Alexandre Savard6d54bbc2012-10-24 11:04:23 -040058import android.content.Intent;
59import android.content.ServiceConnection;
Alexandre Lision93ed33d2013-10-29 15:56:59 -040060import android.graphics.Color;
Alexandre Lision0edf18c2013-09-23 17:35:50 -040061import android.graphics.PixelFormat;
alision55c36cb2013-06-14 14:57:38 -040062import android.net.Uri;
Alexandre Savard14323be2012-10-24 10:02:13 -040063import android.os.Bundle;
Alexandre Lision0c384512013-09-17 17:15:57 -040064import android.os.Handler;
Alexandre Savard6d54bbc2012-10-24 11:04:23 -040065import android.os.IBinder;
alision85992112013-05-29 12:18:08 -040066import android.os.RemoteException;
Alexandre Lision0c384512013-09-17 17:15:57 -040067import android.os.SystemClock;
alisionfde875f2013-05-28 17:01:54 -040068import android.support.v4.widget.SlidingPaneLayout;
Alexandre Lision64dc8c02013-09-25 15:32:25 -040069import android.view.KeyEvent;
alisionfde875f2013-05-28 17:01:54 -040070import android.view.View;
Alexandre Lision0edf18c2013-09-23 17:35:50 -040071import android.view.Window;
Alexandre Lision93ed33d2013-10-29 15:56:59 -040072import android.view.WindowManager;
Alexandre Savard14323be2012-10-24 10:02:13 -040073
Alexandre Lision5f144b82014-02-11 09:59:36 -050074public class CallActivity extends FragmentActivity implements IMFragment.Callbacks, CallFragment.Callbacks, ProximityDirector {
75
76 @SuppressWarnings("unused")
alision55c36cb2013-06-14 14:57:38 -040077 static final String TAG = "CallActivity";
Alexandre Lision47a72042013-12-10 10:43:02 -050078 private ISipService mService;
Alexandre Lision47a72042013-12-10 10:43:02 -050079 CallPaneLayout mSlidingPaneLayout;
Adrien Béraud33268882013-05-18 03:41:15 +100080
Alexandre Lision666b3772013-10-28 17:42:48 -040081 IMFragment mIMFragment;
alision55c36cb2013-06-14 14:57:38 -040082 CallFragment mCurrentCallFragment;
Alexandre Lision48b49eb2014-02-11 13:37:33 -050083 private Conference mDisplayedConference;
Alexandre Lisionf02190d2013-12-12 17:26:12 -050084
Alexandre Lisionf1850c02013-09-23 14:19:34 -040085 /* result code sent in case of call failure */
Alexandre Lisionc51ccb12013-09-11 16:00:30 -040086 public static int RESULT_FAILURE = -10;
Alexandre Lision47a72042013-12-10 10:43:02 -050087 private CallProximityManager mProximityManager;
Alexandre Savard4f42ade2012-10-24 18:03:31 -040088
alision55c36cb2013-06-14 14:57:38 -040089 @Override
90 protected void onCreate(Bundle savedInstanceState) {
91 super.onCreate(savedInstanceState);
92 setContentView(R.layout.activity_call_layout);
Alexandre Savard4f42ade2012-10-24 18:03:31 -040093
Alexandre Lision47a72042013-12-10 10:43:02 -050094 mProximityManager = new CallProximityManager(this, this);
alision85992112013-05-29 12:18:08 -040095
Alexandre Lision47a72042013-12-10 10:43:02 -050096 mSlidingPaneLayout = (CallPaneLayout) findViewById(R.id.slidingpanelayout);
97 mSlidingPaneLayout.setParallaxDistance(500);
98 mSlidingPaneLayout.setSliderFadeColor(Color.TRANSPARENT);
Alexandre Lision3b7148e2013-11-13 17:23:06 -050099
Alexandre Lisionf02190d2013-12-12 17:26:12 -0500100 Window w = getWindow();
101 w.setFlags(WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED, WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED);
102
Alexandre Lision47a72042013-12-10 10:43:02 -0500103 mSlidingPaneLayout.setPanelSlideListener(new SlidingPaneLayout.PanelSlideListener() {
alision84813a12013-05-27 17:40:39 -0400104
alision55c36cb2013-06-14 14:57:38 -0400105 @Override
106 public void onPanelSlide(View view, float offSet) {
107 }
alisionfde875f2013-05-28 17:01:54 -0400108
alision55c36cb2013-06-14 14:57:38 -0400109 @Override
110 public void onPanelOpened(View view) {
Alexandre Lision93ed33d2013-10-29 15:56:59 -0400111 getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN);
alision55c36cb2013-06-14 14:57:38 -0400112 }
alisionfde875f2013-05-28 17:01:54 -0400113
alision55c36cb2013-06-14 14:57:38 -0400114 @Override
115 public void onPanelClosed(View view) {
Alexandre Lision666b3772013-10-28 17:42:48 -0400116 mCurrentCallFragment.getBubbleView().restartDrawing();
Alexandre Lision93ed33d2013-10-29 15:56:59 -0400117 getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_NOTHING);
alision55c36cb2013-06-14 14:57:38 -0400118 }
119 });
alisionfde875f2013-05-28 17:01:54 -0400120
Alexandre Lision47a72042013-12-10 10:43:02 -0500121 mProximityManager.startTracking();
alision55c36cb2013-06-14 14:57:38 -0400122 Intent intent = new Intent(this, SipService.class);
123 bindService(intent, mConnection, Context.BIND_AUTO_CREATE);
alision55c36cb2013-06-14 14:57:38 -0400124 }
alisiond45da712013-05-30 09:18:49 -0400125
Alexandre Lision0edf18c2013-09-23 17:35:50 -0400126 @Override
127 public void onAttachedToWindow() {
128 super.onAttachedToWindow();
129 Window window = getWindow();
130 window.setFormat(PixelFormat.RGBA_8888);
131 }
132
Alexandre Lision0c384512013-09-17 17:15:57 -0400133 private Handler mHandler = new Handler();
134 private Runnable mUpdateTimeTask = new Runnable() {
Adrien Béraud9360f242013-09-19 11:07:42 +1000135 @Override
Alexandre Lision0c384512013-09-17 17:15:57 -0400136 public void run() {
Alexandre Lision0edf18c2013-09-23 17:35:50 -0400137 if (mCurrentCallFragment != null)
Alexandre Lisionfd7a88f2013-09-18 10:03:17 -0400138 mCurrentCallFragment.updateTime();
Adrien Béraud9360f242013-09-19 11:07:42 +1000139 mHandler.postAtTime(this, SystemClock.uptimeMillis() + 1000);
Alexandre Lision0c384512013-09-17 17:15:57 -0400140 }
141 };
142
alision55c36cb2013-06-14 14:57:38 -0400143 /* activity no more in foreground */
144 @Override
145 protected void onPause() {
146 super.onPause();
Alexandre Lision0c384512013-09-17 17:15:57 -0400147 mHandler.removeCallbacks(mUpdateTimeTask);
alision55c36cb2013-06-14 14:57:38 -0400148 }
Alexandre Lision40954dc2013-10-09 15:24:03 -0400149
Alexandre Lision64dc8c02013-09-25 15:32:25 -0400150 @Override
151 public boolean onKeyUp(int keyCode, KeyEvent event) {
Alexandre Lision40954dc2013-10-09 15:24:03 -0400152
153 if (keyCode == KeyEvent.KEYCODE_BACK) {
Alexandre Lision31f46fc2013-09-26 11:19:54 -0400154 return super.onKeyUp(keyCode, event);
155 }
Alexandre Lision64dc8c02013-09-25 15:32:25 -0400156 mCurrentCallFragment.onKeyUp(keyCode, event);
157 return true;
158 }
Alexandre Savard6d54bbc2012-10-24 11:04:23 -0400159
alision55c36cb2013-06-14 14:57:38 -0400160 @Override
161 protected void onDestroy() {
Alexandre Lision5f144b82014-02-11 09:59:36 -0500162
alision55c36cb2013-06-14 14:57:38 -0400163 unbindService(mConnection);
Alexandre Lisionf02190d2013-12-12 17:26:12 -0500164
Alexandre Lision47a72042013-12-10 10:43:02 -0500165 mProximityManager.stopTracking();
166 mProximityManager.release(0);
Alexandre Lisionf02190d2013-12-12 17:26:12 -0500167
alision55c36cb2013-06-14 14:57:38 -0400168 super.onDestroy();
169 }
Adrien Béraud33268882013-05-18 03:41:15 +1000170
Alexandre Lision707f9082014-01-16 15:09:07 -0500171 /**
172 * Defines callbacks for service binding, passed to bindService()
173 */
alision55c36cb2013-06-14 14:57:38 -0400174 private ServiceConnection mConnection = new ServiceConnection() {
Alexandre Lisiona8b78722013-12-13 10:18:33 -0500175 @SuppressWarnings("unchecked")
alision55c36cb2013-06-14 14:57:38 -0400176 @Override
177 public void onServiceConnected(ComponentName className, IBinder binder) {
Alexandre Lision47a72042013-12-10 10:43:02 -0500178 mService = ISipService.Stub.asInterface(binder);
alisiondf1dac92013-06-27 17:35:53 -0400179
alision55c36cb2013-06-14 14:57:38 -0400180 mCurrentCallFragment = new CallFragment();
Alexandre Lisiond5686032013-10-29 11:09:21 -0400181 mIMFragment = new IMFragment();
Adrien Béraud9360f242013-09-19 11:07:42 +1000182
alision55c36cb2013-06-14 14:57:38 -0400183 Uri u = getIntent().getData();
184 if (u != null) {
185 CallContact c = CallContact.ContactBuilder.buildUnknownContact(u.getSchemeSpecificPart());
186 try {
Alexandre Lision47a72042013-12-10 10:43:02 -0500187 mService.destroyNotification();
Alexandre Lision666b3772013-10-28 17:42:48 -0400188
Alexandre Lision47a72042013-12-10 10:43:02 -0500189 String accountID = (String) mService.getAccountList().get(1); // We use the first account to place outgoing calls
190 HashMap<String, String> details = (HashMap<String, String>) mService.getAccountDetails(accountID);
191 ArrayList<HashMap<String, String>> credentials = (ArrayList<HashMap<String, String>>) mService.getCredentials(accountID);
Alexandre Lision3b7148e2013-11-13 17:23:06 -0500192 Account acc = new Account(accountID, details, credentials);
Alexandre Lision666b3772013-10-28 17:42:48 -0400193
194 SipCall call = SipCall.SipCallBuilder.getInstance().startCallCreation().setContact(c).setAccount(acc)
Alexandre Lision67c70b42014-01-16 13:57:15 -0500195 .setCallType(SipCall.direction.CALL_TYPE_OUTGOING).build();
Alexandre Lision48b49eb2014-02-11 13:37:33 -0500196 mDisplayedConference = new Conference(Conference.DEFAULT_ID);
197 mDisplayedConference.getParticipants().add(call);
alision55c36cb2013-06-14 14:57:38 -0400198 } catch (RemoteException e) {
alision55c36cb2013-06-14 14:57:38 -0400199 e.printStackTrace();
200 } catch (Exception e) {
alision55c36cb2013-06-14 14:57:38 -0400201 e.printStackTrace();
202 }
alision55c36cb2013-06-14 14:57:38 -0400203 } else {
alisiondf1dac92013-06-27 17:35:53 -0400204 if (getIntent().getBooleanExtra("resuming", false)) {
205
Alexandre Lision48b49eb2014-02-11 13:37:33 -0500206 mDisplayedConference = getIntent().getParcelableExtra("conference");
Alexandre Lision1a9e3b12013-09-16 11:06:07 -0400207
Alexandre Lisiond5686032013-10-29 11:09:21 -0400208 Bundle IMBundle = new Bundle();
Alexandre Lision48b49eb2014-02-11 13:37:33 -0500209 IMBundle.putParcelableArrayList("messages", mDisplayedConference.getMessages());
Alexandre Lisiond5686032013-10-29 11:09:21 -0400210 mIMFragment.setArguments(IMBundle);
211
alisiondf1dac92013-06-27 17:35:53 -0400212 } else {
Alexandre Lision48b49eb2014-02-11 13:37:33 -0500213 mDisplayedConference = getIntent().getParcelableExtra("conference");
Alexandre Lisiond5686032013-10-29 11:09:21 -0400214 Bundle IMBundle = new Bundle();
215 IMBundle.putParcelableArrayList("messages", new ArrayList<SipMessage>());
216 mIMFragment.setArguments(IMBundle);
alisiondf1dac92013-06-27 17:35:53 -0400217 }
218
alision55c36cb2013-06-14 14:57:38 -0400219 }
Alexandre Lisiond5686032013-10-29 11:09:21 -0400220
Alexandre Lision47a72042013-12-10 10:43:02 -0500221 mSlidingPaneLayout.setCurFragment(mCurrentCallFragment);
Alexandre Lision5f144b82014-02-11 09:59:36 -0500222 getSupportFragmentManager().beginTransaction().replace(R.id.ongoingcall_pane, mCurrentCallFragment)
Alexandre Lision3e1e06f2014-02-10 12:13:25 -0500223 .replace(R.id.message_list_frame, mIMFragment).commit();
alisiond8c83882013-05-17 17:00:42 -0400224
alision55c36cb2013-06-14 14:57:38 -0400225 }
Adrien Béraud6bbce912013-05-24 00:48:13 +1000226
alision55c36cb2013-06-14 14:57:38 -0400227 @Override
228 public void onServiceDisconnected(ComponentName arg0) {
229 }
230 };
Adrien Béraud6bbce912013-05-24 00:48:13 +1000231
alision7f18fc82013-05-01 09:37:33 -0400232
alision55c36cb2013-06-14 14:57:38 -0400233 @Override
234 public ISipService getService() {
Alexandre Lision47a72042013-12-10 10:43:02 -0500235 return mService;
alision55c36cb2013-06-14 14:57:38 -0400236 }
alision04a00182013-05-10 17:05:29 -0400237
alision55c36cb2013-06-14 14:57:38 -0400238 @Override
Alexandre Lision48b49eb2014-02-11 13:37:33 -0500239 public Conference getDisplayedConference() {
240 return mDisplayedConference;
241 }
242
243 @Override
alision55c36cb2013-06-14 14:57:38 -0400244 public void onBackPressed() {
245 super.onBackPressed();
Alexandre Lision4ab53972013-11-04 16:59:18 -0500246 Intent launchHome = new Intent(this, HomeActivity.class);
alision55c36cb2013-06-14 14:57:38 -0400247 launchHome.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
Alexandre Lision5ed2c972013-10-11 15:36:33 -0400248 launchHome.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
alision55c36cb2013-06-14 14:57:38 -0400249 startActivity(launchHome);
250 }
alision85992112013-05-29 12:18:08 -0400251
alision806e18e2013-06-21 15:30:17 -0400252 @Override
Alexandre Lisiondafe9512013-10-28 16:31:41 -0400253 public void terminateCall() {
Alexandre Lisionccf0c932013-10-10 16:24:41 -0400254 mHandler.removeCallbacks(mUpdateTimeTask);
255 mCurrentCallFragment.getBubbleView().stopThread();
Alexandre Lisione4b95842013-11-12 10:36:51 -0500256 TimerTask quit = new TimerTask() {
Alexandre Lision3b7148e2013-11-13 17:23:06 -0500257
Alexandre Lisione4b95842013-11-12 10:36:51 -0500258 @Override
259 public void run() {
260 finish();
261 }
262 };
Alexandre Lision3b7148e2013-11-13 17:23:06 -0500263
Alexandre Lision681d0292013-11-22 12:47:34 -0500264 new Timer().schedule(quit, 1000);
Alexandre Lisionccf0c932013-10-10 16:24:41 -0400265 }
alision806e18e2013-06-21 15:30:17 -0400266
Alexandre Lision0c384512013-09-17 17:15:57 -0400267 @Override
Alexandre Lisiond5686032013-10-29 11:09:21 -0400268 public boolean sendIM(SipMessage msg) {
269
270 try {
Alexandre Lision47a72042013-12-10 10:43:02 -0500271 mService.sendTextMessage(mCurrentCallFragment.getConference().getId(), msg);
Alexandre Lisiond5686032013-10-29 11:09:21 -0400272 } catch (RemoteException e) {
273 e.printStackTrace();
274 return false;
275 }
276
277 return true;
278 }
279
280 @Override
Alexandre Lision0c384512013-09-17 17:15:57 -0400281 public void startTimer() {
282 mHandler.postDelayed(mUpdateTimeTask, 0);
283 }
284
Alexandre Lision68855472013-10-10 16:20:46 -0400285 @Override
286 public void slideChatScreen() {
Alexandre Lision666b3772013-10-28 17:42:48 -0400287
Alexandre Lision47a72042013-12-10 10:43:02 -0500288 if (mSlidingPaneLayout.isOpen()) {
289 mSlidingPaneLayout.closePane();
Alexandre Lision666b3772013-10-28 17:42:48 -0400290 } else {
291 mCurrentCallFragment.getBubbleView().stopThread();
Alexandre Lision47a72042013-12-10 10:43:02 -0500292 mSlidingPaneLayout.openPane();
Alexandre Lision666b3772013-10-28 17:42:48 -0400293 }
Alexandre Lision68855472013-10-10 16:20:46 -0400294 }
Alexandre Lisionb8add812013-10-24 11:42:42 -0400295
296 @Override
297 public boolean shouldActivateProximity() {
298 return true;
299 }
300
301 @Override
302 public void onProximityTrackingChanged(boolean acquired) {
303 // TODO Stub de la méthode généré automatiquement
Alexandre Lision666b3772013-10-28 17:42:48 -0400304
Alexandre Lisionb8add812013-10-24 11:42:42 -0400305 }
Alexandre Savard14323be2012-10-24 10:02:13 -0400306}