blob: 2f5de06db9d01f4c2cfa46b26ff0a7f2449b6eec [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;
alision84813a12013-05-27 17:40:39 -040079
Alexandre Lision5f144b82014-02-11 09:59:36 -050080
alision85992112013-05-29 12:18:08 -040081
Alexandre Lision47a72042013-12-10 10:43:02 -050082 CallPaneLayout mSlidingPaneLayout;
Adrien Béraud33268882013-05-18 03:41:15 +100083
Alexandre Lision666b3772013-10-28 17:42:48 -040084 IMFragment mIMFragment;
alision55c36cb2013-06-14 14:57:38 -040085 CallFragment mCurrentCallFragment;
Alexandre Lision1a9e3b12013-09-16 11:06:07 -040086
Alexandre Lisionf02190d2013-12-12 17:26:12 -050087
Alexandre Lisionf1850c02013-09-23 14:19:34 -040088 /* result code sent in case of call failure */
Alexandre Lisionc51ccb12013-09-11 16:00:30 -040089 public static int RESULT_FAILURE = -10;
Alexandre Lision47a72042013-12-10 10:43:02 -050090 private CallProximityManager mProximityManager;
Alexandre Savard4f42ade2012-10-24 18:03:31 -040091
alision55c36cb2013-06-14 14:57:38 -040092 @Override
93 protected void onCreate(Bundle savedInstanceState) {
94 super.onCreate(savedInstanceState);
95 setContentView(R.layout.activity_call_layout);
Alexandre Savard4f42ade2012-10-24 18:03:31 -040096
Alexandre Lision5f144b82014-02-11 09:59:36 -050097
Adrien Béraud33268882013-05-18 03:41:15 +100098
Alexandre Lision47a72042013-12-10 10:43:02 -050099 mProximityManager = new CallProximityManager(this, this);
alision85992112013-05-29 12:18:08 -0400100
Alexandre Lision47a72042013-12-10 10:43:02 -0500101 mSlidingPaneLayout = (CallPaneLayout) findViewById(R.id.slidingpanelayout);
102 mSlidingPaneLayout.setParallaxDistance(500);
103 mSlidingPaneLayout.setSliderFadeColor(Color.TRANSPARENT);
Alexandre Lision3b7148e2013-11-13 17:23:06 -0500104
Alexandre Lisionf02190d2013-12-12 17:26:12 -0500105 Window w = getWindow();
106 w.setFlags(WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED, WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED);
107
Alexandre Lision47a72042013-12-10 10:43:02 -0500108 mSlidingPaneLayout.setPanelSlideListener(new SlidingPaneLayout.PanelSlideListener() {
alision84813a12013-05-27 17:40:39 -0400109
alision55c36cb2013-06-14 14:57:38 -0400110 @Override
111 public void onPanelSlide(View view, float offSet) {
112 }
alisionfde875f2013-05-28 17:01:54 -0400113
alision55c36cb2013-06-14 14:57:38 -0400114 @Override
115 public void onPanelOpened(View view) {
Alexandre Lision93ed33d2013-10-29 15:56:59 -0400116 getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN);
alision55c36cb2013-06-14 14:57:38 -0400117 }
alisionfde875f2013-05-28 17:01:54 -0400118
alision55c36cb2013-06-14 14:57:38 -0400119 @Override
120 public void onPanelClosed(View view) {
Alexandre Lision666b3772013-10-28 17:42:48 -0400121 mCurrentCallFragment.getBubbleView().restartDrawing();
Alexandre Lision93ed33d2013-10-29 15:56:59 -0400122 getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_NOTHING);
alision55c36cb2013-06-14 14:57:38 -0400123 }
124 });
alisionfde875f2013-05-28 17:01:54 -0400125
Alexandre Lision47a72042013-12-10 10:43:02 -0500126 mProximityManager.startTracking();
alision55c36cb2013-06-14 14:57:38 -0400127 Intent intent = new Intent(this, SipService.class);
128 bindService(intent, mConnection, Context.BIND_AUTO_CREATE);
alision55c36cb2013-06-14 14:57:38 -0400129 }
alisiond45da712013-05-30 09:18:49 -0400130
Alexandre Lision0edf18c2013-09-23 17:35:50 -0400131 @Override
132 public void onAttachedToWindow() {
133 super.onAttachedToWindow();
134 Window window = getWindow();
135 window.setFormat(PixelFormat.RGBA_8888);
136 }
137
Alexandre Lision0c384512013-09-17 17:15:57 -0400138 private Handler mHandler = new Handler();
139 private Runnable mUpdateTimeTask = new Runnable() {
Adrien Béraud9360f242013-09-19 11:07:42 +1000140 @Override
Alexandre Lision0c384512013-09-17 17:15:57 -0400141 public void run() {
Alexandre Lision0edf18c2013-09-23 17:35:50 -0400142 if (mCurrentCallFragment != null)
Alexandre Lisionfd7a88f2013-09-18 10:03:17 -0400143 mCurrentCallFragment.updateTime();
Adrien Béraud9360f242013-09-19 11:07:42 +1000144 mHandler.postAtTime(this, SystemClock.uptimeMillis() + 1000);
Alexandre Lision0c384512013-09-17 17:15:57 -0400145 }
146 };
147
alision55c36cb2013-06-14 14:57:38 -0400148 /* activity no more in foreground */
149 @Override
150 protected void onPause() {
151 super.onPause();
Alexandre Lision0c384512013-09-17 17:15:57 -0400152 mHandler.removeCallbacks(mUpdateTimeTask);
alision55c36cb2013-06-14 14:57:38 -0400153 }
Alexandre Lision40954dc2013-10-09 15:24:03 -0400154
Alexandre Lision64dc8c02013-09-25 15:32:25 -0400155 @Override
156 public boolean onKeyUp(int keyCode, KeyEvent event) {
Alexandre Lision40954dc2013-10-09 15:24:03 -0400157
158 if (keyCode == KeyEvent.KEYCODE_BACK) {
Alexandre Lision31f46fc2013-09-26 11:19:54 -0400159 return super.onKeyUp(keyCode, event);
160 }
Alexandre Lision64dc8c02013-09-25 15:32:25 -0400161 mCurrentCallFragment.onKeyUp(keyCode, event);
162 return true;
163 }
Alexandre Savard6d54bbc2012-10-24 11:04:23 -0400164
alision55c36cb2013-06-14 14:57:38 -0400165 @Override
166 protected void onDestroy() {
Alexandre Lision5f144b82014-02-11 09:59:36 -0500167
alision55c36cb2013-06-14 14:57:38 -0400168 unbindService(mConnection);
Alexandre Lisionf02190d2013-12-12 17:26:12 -0500169
Alexandre Lision47a72042013-12-10 10:43:02 -0500170 mProximityManager.stopTracking();
171 mProximityManager.release(0);
Alexandre Lisionf02190d2013-12-12 17:26:12 -0500172
alision55c36cb2013-06-14 14:57:38 -0400173 super.onDestroy();
174 }
Adrien Béraud33268882013-05-18 03:41:15 +1000175
Alexandre Lision707f9082014-01-16 15:09:07 -0500176 /**
177 * Defines callbacks for service binding, passed to bindService()
178 */
alision55c36cb2013-06-14 14:57:38 -0400179 private ServiceConnection mConnection = new ServiceConnection() {
Alexandre Lisiona8b78722013-12-13 10:18:33 -0500180 @SuppressWarnings("unchecked")
alision55c36cb2013-06-14 14:57:38 -0400181 @Override
182 public void onServiceConnected(ComponentName className, IBinder binder) {
Alexandre Lision47a72042013-12-10 10:43:02 -0500183 mService = ISipService.Stub.asInterface(binder);
alisiondf1dac92013-06-27 17:35:53 -0400184
alision55c36cb2013-06-14 14:57:38 -0400185 mCurrentCallFragment = new CallFragment();
Alexandre Lisiond5686032013-10-29 11:09:21 -0400186 mIMFragment = new IMFragment();
Adrien Béraud9360f242013-09-19 11:07:42 +1000187
alision55c36cb2013-06-14 14:57:38 -0400188 Uri u = getIntent().getData();
189 if (u != null) {
190 CallContact c = CallContact.ContactBuilder.buildUnknownContact(u.getSchemeSpecificPart());
191 try {
Alexandre Lision47a72042013-12-10 10:43:02 -0500192 mService.destroyNotification();
Alexandre Lision666b3772013-10-28 17:42:48 -0400193
Alexandre Lision47a72042013-12-10 10:43:02 -0500194 String accountID = (String) mService.getAccountList().get(1); // We use the first account to place outgoing calls
195 HashMap<String, String> details = (HashMap<String, String>) mService.getAccountDetails(accountID);
196 ArrayList<HashMap<String, String>> credentials = (ArrayList<HashMap<String, String>>) mService.getCredentials(accountID);
Alexandre Lision3b7148e2013-11-13 17:23:06 -0500197 Account acc = new Account(accountID, details, credentials);
Alexandre Lision666b3772013-10-28 17:42:48 -0400198
199 SipCall call = SipCall.SipCallBuilder.getInstance().startCallCreation().setContact(c).setAccount(acc)
Alexandre Lision67c70b42014-01-16 13:57:15 -0500200 .setCallType(SipCall.direction.CALL_TYPE_OUTGOING).build();
201 Conference tmp = new Conference(Conference.DEFAULT_ID);
alisiondf1dac92013-06-27 17:35:53 -0400202 tmp.getParticipants().add(call);
alision55c36cb2013-06-14 14:57:38 -0400203 Bundle b = new Bundle();
alisiondf1dac92013-06-27 17:35:53 -0400204 b.putParcelable("conference", tmp);
alision55c36cb2013-06-14 14:57:38 -0400205 mCurrentCallFragment.setArguments(b);
206 } catch (RemoteException e) {
alision55c36cb2013-06-14 14:57:38 -0400207 e.printStackTrace();
208 } catch (Exception e) {
alision55c36cb2013-06-14 14:57:38 -0400209 e.printStackTrace();
210 }
alision55c36cb2013-06-14 14:57:38 -0400211 } else {
alisiondf1dac92013-06-27 17:35:53 -0400212 if (getIntent().getBooleanExtra("resuming", false)) {
213
214 Bundle b = new Bundle();
Alexandre Lisiond5686032013-10-29 11:09:21 -0400215 Conference resumed = getIntent().getParcelableExtra("conference");
Adrien Béraud9360f242013-09-19 11:07:42 +1000216 b.putParcelable("conference", getIntent().getParcelableExtra("conference"));
Alexandre Lision1a9e3b12013-09-16 11:06:07 -0400217 mCurrentCallFragment.setArguments(b);
218
Alexandre Lisiond5686032013-10-29 11:09:21 -0400219 Bundle IMBundle = new Bundle();
220 IMBundle.putParcelableArrayList("messages", resumed.getMessages());
221 mIMFragment.setArguments(IMBundle);
222
alisiondf1dac92013-06-27 17:35:53 -0400223 } else {
224 mCurrentCallFragment.setArguments(getIntent().getExtras());
Alexandre Lision3b7148e2013-11-13 17:23:06 -0500225
Alexandre Lisiond5686032013-10-29 11:09:21 -0400226 Bundle IMBundle = new Bundle();
227 IMBundle.putParcelableArrayList("messages", new ArrayList<SipMessage>());
228 mIMFragment.setArguments(IMBundle);
alisiondf1dac92013-06-27 17:35:53 -0400229 }
230
alision55c36cb2013-06-14 14:57:38 -0400231 }
Alexandre Lisiond5686032013-10-29 11:09:21 -0400232
Alexandre Lision47a72042013-12-10 10:43:02 -0500233 mSlidingPaneLayout.setCurFragment(mCurrentCallFragment);
Alexandre Lision5f144b82014-02-11 09:59:36 -0500234 getSupportFragmentManager().beginTransaction().replace(R.id.ongoingcall_pane, mCurrentCallFragment)
Alexandre Lision3e1e06f2014-02-10 12:13:25 -0500235 .replace(R.id.message_list_frame, mIMFragment).commit();
alisiond8c83882013-05-17 17:00:42 -0400236
alision55c36cb2013-06-14 14:57:38 -0400237 }
Adrien Béraud6bbce912013-05-24 00:48:13 +1000238
alision55c36cb2013-06-14 14:57:38 -0400239 @Override
240 public void onServiceDisconnected(ComponentName arg0) {
241 }
242 };
Adrien Béraud6bbce912013-05-24 00:48:13 +1000243
alision7f18fc82013-05-01 09:37:33 -0400244
alision55c36cb2013-06-14 14:57:38 -0400245 @Override
246 public ISipService getService() {
Alexandre Lision47a72042013-12-10 10:43:02 -0500247 return mService;
alision55c36cb2013-06-14 14:57:38 -0400248 }
alision04a00182013-05-10 17:05:29 -0400249
alision55c36cb2013-06-14 14:57:38 -0400250 @Override
251 public void onBackPressed() {
252 super.onBackPressed();
Alexandre Lision4ab53972013-11-04 16:59:18 -0500253 Intent launchHome = new Intent(this, HomeActivity.class);
alision55c36cb2013-06-14 14:57:38 -0400254 launchHome.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
Alexandre Lision5ed2c972013-10-11 15:36:33 -0400255 launchHome.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
alision55c36cb2013-06-14 14:57:38 -0400256 startActivity(launchHome);
257 }
alision85992112013-05-29 12:18:08 -0400258
alision806e18e2013-06-21 15:30:17 -0400259 @Override
Alexandre Lisiondafe9512013-10-28 16:31:41 -0400260 public void terminateCall() {
Alexandre Lisionccf0c932013-10-10 16:24:41 -0400261 mHandler.removeCallbacks(mUpdateTimeTask);
262 mCurrentCallFragment.getBubbleView().stopThread();
Alexandre Lisione4b95842013-11-12 10:36:51 -0500263 TimerTask quit = new TimerTask() {
Alexandre Lision3b7148e2013-11-13 17:23:06 -0500264
Alexandre Lisione4b95842013-11-12 10:36:51 -0500265 @Override
266 public void run() {
267 finish();
268 }
269 };
Alexandre Lision3b7148e2013-11-13 17:23:06 -0500270
Alexandre Lision681d0292013-11-22 12:47:34 -0500271 new Timer().schedule(quit, 1000);
Alexandre Lisionccf0c932013-10-10 16:24:41 -0400272 }
alision806e18e2013-06-21 15:30:17 -0400273
Alexandre Lision0c384512013-09-17 17:15:57 -0400274 @Override
Alexandre Lisiond5686032013-10-29 11:09:21 -0400275 public boolean sendIM(SipMessage msg) {
276
277 try {
Alexandre Lision47a72042013-12-10 10:43:02 -0500278 mService.sendTextMessage(mCurrentCallFragment.getConference().getId(), msg);
Alexandre Lisiond5686032013-10-29 11:09:21 -0400279 } catch (RemoteException e) {
280 e.printStackTrace();
281 return false;
282 }
283
284 return true;
285 }
286
287 @Override
Alexandre Lision0c384512013-09-17 17:15:57 -0400288 public void startTimer() {
289 mHandler.postDelayed(mUpdateTimeTask, 0);
290 }
291
Alexandre Lision68855472013-10-10 16:20:46 -0400292 @Override
293 public void slideChatScreen() {
Alexandre Lision666b3772013-10-28 17:42:48 -0400294
Alexandre Lision47a72042013-12-10 10:43:02 -0500295 if (mSlidingPaneLayout.isOpen()) {
296 mSlidingPaneLayout.closePane();
Alexandre Lision666b3772013-10-28 17:42:48 -0400297 } else {
298 mCurrentCallFragment.getBubbleView().stopThread();
Alexandre Lision47a72042013-12-10 10:43:02 -0500299 mSlidingPaneLayout.openPane();
Alexandre Lision666b3772013-10-28 17:42:48 -0400300 }
Alexandre Lision68855472013-10-10 16:20:46 -0400301 }
Alexandre Lisionb8add812013-10-24 11:42:42 -0400302
303 @Override
304 public boolean shouldActivateProximity() {
305 return true;
306 }
307
308 @Override
309 public void onProximityTrackingChanged(boolean acquired) {
310 // TODO Stub de la méthode généré automatiquement
Alexandre Lision666b3772013-10-28 17:42:48 -0400311
Alexandre Lisionb8add812013-10-24 11:42:42 -0400312 }
Alexandre Savard14323be2012-10-24 10:02:13 -0400313}