blob: 12188f709074e630424ce0001b662180429f59b5 [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 Lisiona7ab2e32014-02-14 15:33:33 -050036import java.util.*;
Adrien Béraud33268882013-05-18 03:41:15 +100037
Alexandre Lision5f144b82014-02-11 09:59:36 -050038import android.support.v4.app.FragmentActivity;
Alexandre Lisiona7ab2e32014-02-14 15:33:33 -050039import android.util.Log;
Alexandre Lision064e1e02013-10-01 16:18:42 -040040import org.sflphone.R;
41import org.sflphone.fragments.CallFragment;
Alexandre Lision666b3772013-10-28 17:42:48 -040042import org.sflphone.fragments.IMFragment;
Alexandre Lisiond588bff2013-10-08 12:43:01 -040043import org.sflphone.model.Account;
Alexandre Lision064e1e02013-10-01 16:18:42 -040044import org.sflphone.model.CallContact;
45import org.sflphone.model.Conference;
46import org.sflphone.model.SipCall;
Alexandre Lision666b3772013-10-28 17:42:48 -040047import org.sflphone.model.SipMessage;
Alexandre Lision064e1e02013-10-01 16:18:42 -040048import org.sflphone.service.ISipService;
49import org.sflphone.service.SipService;
Alexandre Lisionb8add812013-10-24 11:42:42 -040050import org.sflphone.utils.CallProximityManager;
51import org.sflphone.utils.CallProximityManager.ProximityDirector;
Alexandre Lision064e1e02013-10-01 16:18:42 -040052import org.sflphone.views.CallPaneLayout;
53
Alexandre Savard6d54bbc2012-10-24 11:04:23 -040054import android.content.ComponentName;
alision17052d42013-04-22 10:39:38 -040055import android.content.Context;
Alexandre Savard6d54bbc2012-10-24 11:04:23 -040056import android.content.Intent;
57import android.content.ServiceConnection;
Alexandre Lision93ed33d2013-10-29 15:56:59 -040058import android.graphics.Color;
Alexandre Lision0edf18c2013-09-23 17:35:50 -040059import android.graphics.PixelFormat;
alision55c36cb2013-06-14 14:57:38 -040060import android.net.Uri;
Alexandre Savard14323be2012-10-24 10:02:13 -040061import android.os.Bundle;
Alexandre Lision0c384512013-09-17 17:15:57 -040062import android.os.Handler;
Alexandre Savard6d54bbc2012-10-24 11:04:23 -040063import android.os.IBinder;
alision85992112013-05-29 12:18:08 -040064import android.os.RemoteException;
Alexandre Lision0c384512013-09-17 17:15:57 -040065import android.os.SystemClock;
alisionfde875f2013-05-28 17:01:54 -040066import android.support.v4.widget.SlidingPaneLayout;
Alexandre Lision64dc8c02013-09-25 15:32:25 -040067import android.view.KeyEvent;
alisionfde875f2013-05-28 17:01:54 -040068import android.view.View;
Alexandre Lision0edf18c2013-09-23 17:35:50 -040069import android.view.Window;
Alexandre Lision93ed33d2013-10-29 15:56:59 -040070import android.view.WindowManager;
Alexandre Savard14323be2012-10-24 10:02:13 -040071
Alexandre Lision5f144b82014-02-11 09:59:36 -050072public class CallActivity extends FragmentActivity implements IMFragment.Callbacks, CallFragment.Callbacks, ProximityDirector {
73
74 @SuppressWarnings("unused")
alision55c36cb2013-06-14 14:57:38 -040075 static final String TAG = "CallActivity";
Alexandre Lision47a72042013-12-10 10:43:02 -050076 private ISipService mService;
Alexandre Lision47a72042013-12-10 10:43:02 -050077 CallPaneLayout mSlidingPaneLayout;
Adrien Béraud33268882013-05-18 03:41:15 +100078
Alexandre Lision666b3772013-10-28 17:42:48 -040079 IMFragment mIMFragment;
alision55c36cb2013-06-14 14:57:38 -040080 CallFragment mCurrentCallFragment;
Alexandre Lision48b49eb2014-02-11 13:37:33 -050081 private Conference mDisplayedConference;
Alexandre Lisionf02190d2013-12-12 17:26:12 -050082
Alexandre Lisionf1850c02013-09-23 14:19:34 -040083 /* result code sent in case of call failure */
Alexandre Lisionc51ccb12013-09-11 16:00:30 -040084 public static int RESULT_FAILURE = -10;
Alexandre Lision47a72042013-12-10 10:43:02 -050085 private CallProximityManager mProximityManager;
Alexandre Savard4f42ade2012-10-24 18:03:31 -040086
alision55c36cb2013-06-14 14:57:38 -040087 @Override
88 protected void onCreate(Bundle savedInstanceState) {
89 super.onCreate(savedInstanceState);
90 setContentView(R.layout.activity_call_layout);
Alexandre Savard4f42ade2012-10-24 18:03:31 -040091
Alexandre Lision47a72042013-12-10 10:43:02 -050092 mProximityManager = new CallProximityManager(this, this);
alision85992112013-05-29 12:18:08 -040093
Alexandre Lision47a72042013-12-10 10:43:02 -050094 mSlidingPaneLayout = (CallPaneLayout) findViewById(R.id.slidingpanelayout);
95 mSlidingPaneLayout.setParallaxDistance(500);
96 mSlidingPaneLayout.setSliderFadeColor(Color.TRANSPARENT);
Alexandre Lision3b7148e2013-11-13 17:23:06 -050097
Alexandre Lisionf02190d2013-12-12 17:26:12 -050098 Window w = getWindow();
99 w.setFlags(WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED, WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED);
100
Alexandre Lision47a72042013-12-10 10:43:02 -0500101 mSlidingPaneLayout.setPanelSlideListener(new SlidingPaneLayout.PanelSlideListener() {
alision84813a12013-05-27 17:40:39 -0400102
alision55c36cb2013-06-14 14:57:38 -0400103 @Override
104 public void onPanelSlide(View view, float offSet) {
105 }
alisionfde875f2013-05-28 17:01:54 -0400106
alision55c36cb2013-06-14 14:57:38 -0400107 @Override
108 public void onPanelOpened(View view) {
Alexandre Lision93ed33d2013-10-29 15:56:59 -0400109 getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN);
alision55c36cb2013-06-14 14:57:38 -0400110 }
alisionfde875f2013-05-28 17:01:54 -0400111
alision55c36cb2013-06-14 14:57:38 -0400112 @Override
113 public void onPanelClosed(View view) {
Alexandre Lision666b3772013-10-28 17:42:48 -0400114 mCurrentCallFragment.getBubbleView().restartDrawing();
Alexandre Lision93ed33d2013-10-29 15:56:59 -0400115 getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_NOTHING);
alision55c36cb2013-06-14 14:57:38 -0400116 }
117 });
alisionfde875f2013-05-28 17:01:54 -0400118
Alexandre Lision47a72042013-12-10 10:43:02 -0500119 mProximityManager.startTracking();
alision55c36cb2013-06-14 14:57:38 -0400120 Intent intent = new Intent(this, SipService.class);
121 bindService(intent, mConnection, Context.BIND_AUTO_CREATE);
alision55c36cb2013-06-14 14:57:38 -0400122 }
alisiond45da712013-05-30 09:18:49 -0400123
Alexandre Lision0edf18c2013-09-23 17:35:50 -0400124 @Override
125 public void onAttachedToWindow() {
126 super.onAttachedToWindow();
127 Window window = getWindow();
128 window.setFormat(PixelFormat.RGBA_8888);
129 }
130
Alexandre Lision0c384512013-09-17 17:15:57 -0400131 private Handler mHandler = new Handler();
132 private Runnable mUpdateTimeTask = new Runnable() {
Adrien Béraud9360f242013-09-19 11:07:42 +1000133 @Override
Alexandre Lision0c384512013-09-17 17:15:57 -0400134 public void run() {
Alexandre Lision0edf18c2013-09-23 17:35:50 -0400135 if (mCurrentCallFragment != null)
Alexandre Lisionfd7a88f2013-09-18 10:03:17 -0400136 mCurrentCallFragment.updateTime();
Adrien Béraud9360f242013-09-19 11:07:42 +1000137 mHandler.postAtTime(this, SystemClock.uptimeMillis() + 1000);
Alexandre Lision0c384512013-09-17 17:15:57 -0400138 }
139 };
140
alision55c36cb2013-06-14 14:57:38 -0400141 /* activity no more in foreground */
142 @Override
143 protected void onPause() {
144 super.onPause();
Alexandre Lision0c384512013-09-17 17:15:57 -0400145 mHandler.removeCallbacks(mUpdateTimeTask);
alision55c36cb2013-06-14 14:57:38 -0400146 }
Alexandre Lision40954dc2013-10-09 15:24:03 -0400147
Alexandre Lision64dc8c02013-09-25 15:32:25 -0400148 @Override
149 public boolean onKeyUp(int keyCode, KeyEvent event) {
Alexandre Lision40954dc2013-10-09 15:24:03 -0400150
151 if (keyCode == KeyEvent.KEYCODE_BACK) {
Alexandre Lision31f46fc2013-09-26 11:19:54 -0400152 return super.onKeyUp(keyCode, event);
153 }
Alexandre Lision64dc8c02013-09-25 15:32:25 -0400154 mCurrentCallFragment.onKeyUp(keyCode, event);
155 return true;
156 }
Alexandre Savard6d54bbc2012-10-24 11:04:23 -0400157
alision55c36cb2013-06-14 14:57:38 -0400158 @Override
159 protected void onDestroy() {
Alexandre Lision5f144b82014-02-11 09:59:36 -0500160
alision55c36cb2013-06-14 14:57:38 -0400161 unbindService(mConnection);
Alexandre Lisionf02190d2013-12-12 17:26:12 -0500162
Alexandre Lision47a72042013-12-10 10:43:02 -0500163 mProximityManager.stopTracking();
164 mProximityManager.release(0);
Alexandre Lisionf02190d2013-12-12 17:26:12 -0500165
alision55c36cb2013-06-14 14:57:38 -0400166 super.onDestroy();
167 }
Adrien Béraud33268882013-05-18 03:41:15 +1000168
Alexandre Lision707f9082014-01-16 15:09:07 -0500169 /**
170 * Defines callbacks for service binding, passed to bindService()
171 */
alision55c36cb2013-06-14 14:57:38 -0400172 private ServiceConnection mConnection = new ServiceConnection() {
Alexandre Lisiona8b78722013-12-13 10:18:33 -0500173 @SuppressWarnings("unchecked")
alision55c36cb2013-06-14 14:57:38 -0400174 @Override
175 public void onServiceConnected(ComponentName className, IBinder binder) {
Alexandre Lision47a72042013-12-10 10:43:02 -0500176 mService = ISipService.Stub.asInterface(binder);
alisiondf1dac92013-06-27 17:35:53 -0400177
alision55c36cb2013-06-14 14:57:38 -0400178 mCurrentCallFragment = new CallFragment();
Alexandre Lisiond5686032013-10-29 11:09:21 -0400179 mIMFragment = new IMFragment();
Adrien Béraud9360f242013-09-19 11:07:42 +1000180
alision55c36cb2013-06-14 14:57:38 -0400181 Uri u = getIntent().getData();
182 if (u != null) {
183 CallContact c = CallContact.ContactBuilder.buildUnknownContact(u.getSchemeSpecificPart());
184 try {
Alexandre Lision47a72042013-12-10 10:43:02 -0500185 mService.destroyNotification();
Alexandre Lision666b3772013-10-28 17:42:48 -0400186
Alexandre Lision47a72042013-12-10 10:43:02 -0500187 String accountID = (String) mService.getAccountList().get(1); // We use the first account to place outgoing calls
188 HashMap<String, String> details = (HashMap<String, String>) mService.getAccountDetails(accountID);
189 ArrayList<HashMap<String, String>> credentials = (ArrayList<HashMap<String, String>>) mService.getCredentials(accountID);
Alexandre Lision3b7148e2013-11-13 17:23:06 -0500190 Account acc = new Account(accountID, details, credentials);
Alexandre Lision666b3772013-10-28 17:42:48 -0400191
Alexandre Lisiona7ab2e32014-02-14 15:33:33 -0500192 Bundle args = new Bundle();
193 args.putString(SipCall.ID, Integer.toString(Math.abs(new Random().nextInt())));
194 args.putParcelable(SipCall.ACCOUNT, acc);
195 args.putInt(SipCall.STATE, SipCall.state.CALL_STATE_RINGING);
196 args.putInt(SipCall.TYPE, SipCall.direction.CALL_TYPE_OUTGOING);
197 args.putParcelable(SipCall.CONTACT, c);
198
Alexandre Lision48b49eb2014-02-11 13:37:33 -0500199 mDisplayedConference = new Conference(Conference.DEFAULT_ID);
Alexandre Lisiona7ab2e32014-02-14 15:33:33 -0500200 mDisplayedConference.getParticipants().add(new SipCall(args));
alision55c36cb2013-06-14 14:57:38 -0400201 } catch (RemoteException e) {
alision55c36cb2013-06-14 14:57:38 -0400202 e.printStackTrace();
203 } catch (Exception e) {
alision55c36cb2013-06-14 14:57:38 -0400204 e.printStackTrace();
205 }
alision55c36cb2013-06-14 14:57:38 -0400206 } else {
Alexandre Lisiona7ab2e32014-02-14 15:33:33 -0500207 mDisplayedConference = getIntent().getParcelableExtra("conference");
alisiondf1dac92013-06-27 17:35:53 -0400208 if (getIntent().getBooleanExtra("resuming", false)) {
Alexandre Lisiond5686032013-10-29 11:09:21 -0400209 Bundle IMBundle = new Bundle();
Alexandre Lision48b49eb2014-02-11 13:37:33 -0500210 IMBundle.putParcelableArrayList("messages", mDisplayedConference.getMessages());
Alexandre Lisiond5686032013-10-29 11:09:21 -0400211 mIMFragment.setArguments(IMBundle);
alisiondf1dac92013-06-27 17:35:53 -0400212 } else {
Alexandre Lisiond5686032013-10-29 11:09:21 -0400213 Bundle IMBundle = new Bundle();
Alexandre Lisiona7ab2e32014-02-14 15:33:33 -0500214 try {
215 mService.placeCall(mDisplayedConference.getParticipants().get(0));
216 } catch (RemoteException e) {
217 e.printStackTrace();
218 }
Alexandre Lisiond5686032013-10-29 11:09:21 -0400219 IMBundle.putParcelableArrayList("messages", new ArrayList<SipMessage>());
220 mIMFragment.setArguments(IMBundle);
alisiondf1dac92013-06-27 17:35:53 -0400221 }
222
alision55c36cb2013-06-14 14:57:38 -0400223 }
Alexandre Lisiond5686032013-10-29 11:09:21 -0400224
Alexandre Lision47a72042013-12-10 10:43:02 -0500225 mSlidingPaneLayout.setCurFragment(mCurrentCallFragment);
Alexandre Lision5f144b82014-02-11 09:59:36 -0500226 getSupportFragmentManager().beginTransaction().replace(R.id.ongoingcall_pane, mCurrentCallFragment)
Alexandre Lision3e1e06f2014-02-10 12:13:25 -0500227 .replace(R.id.message_list_frame, mIMFragment).commit();
alisiond8c83882013-05-17 17:00:42 -0400228
alision55c36cb2013-06-14 14:57:38 -0400229 }
Adrien Béraud6bbce912013-05-24 00:48:13 +1000230
alision55c36cb2013-06-14 14:57:38 -0400231 @Override
232 public void onServiceDisconnected(ComponentName arg0) {
233 }
234 };
Adrien Béraud6bbce912013-05-24 00:48:13 +1000235
alision7f18fc82013-05-01 09:37:33 -0400236
alision55c36cb2013-06-14 14:57:38 -0400237 @Override
238 public ISipService getService() {
Alexandre Lision47a72042013-12-10 10:43:02 -0500239 return mService;
alision55c36cb2013-06-14 14:57:38 -0400240 }
alision04a00182013-05-10 17:05:29 -0400241
alision55c36cb2013-06-14 14:57:38 -0400242 @Override
Alexandre Lision48b49eb2014-02-11 13:37:33 -0500243 public Conference getDisplayedConference() {
244 return mDisplayedConference;
245 }
246
247 @Override
Alexandre Lisiona7ab2e32014-02-14 15:33:33 -0500248 public void updateDisplayedConference(Conference c) {
Alexandre Lisiona7ab2e32014-02-14 15:33:33 -0500249 if(mDisplayedConference.equals(c)){
Alexandre Lisiona7ab2e32014-02-14 15:33:33 -0500250 mDisplayedConference = c;
251 }
252 }
253
254 @Override
alision55c36cb2013-06-14 14:57:38 -0400255 public void onBackPressed() {
256 super.onBackPressed();
Alexandre Lision4ab53972013-11-04 16:59:18 -0500257 Intent launchHome = new Intent(this, HomeActivity.class);
alision55c36cb2013-06-14 14:57:38 -0400258 launchHome.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
Alexandre Lision5ed2c972013-10-11 15:36:33 -0400259 launchHome.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
alision55c36cb2013-06-14 14:57:38 -0400260 startActivity(launchHome);
261 }
alision85992112013-05-29 12:18:08 -0400262
alision806e18e2013-06-21 15:30:17 -0400263 @Override
Alexandre Lisiondafe9512013-10-28 16:31:41 -0400264 public void terminateCall() {
Alexandre Lisionccf0c932013-10-10 16:24:41 -0400265 mHandler.removeCallbacks(mUpdateTimeTask);
266 mCurrentCallFragment.getBubbleView().stopThread();
Alexandre Lisione4b95842013-11-12 10:36:51 -0500267 TimerTask quit = new TimerTask() {
Alexandre Lision3b7148e2013-11-13 17:23:06 -0500268
Alexandre Lisione4b95842013-11-12 10:36:51 -0500269 @Override
270 public void run() {
Alexandre Lision2cd17d32014-02-27 15:57:20 -0500271 /*try {
Alexandre Lision3c37dca2014-02-21 14:13:26 -0500272 // We hang it up again to avoid infinite failure tone
273 mService.hangUp(mDisplayedConference.getId());
274 } catch (RemoteException e) {
275 e.printStackTrace();
Alexandre Lision2cd17d32014-02-27 15:57:20 -0500276 }*/
Alexandre Lisione4b95842013-11-12 10:36:51 -0500277 finish();
278 }
279 };
Alexandre Lision3b7148e2013-11-13 17:23:06 -0500280
Alexandre Lision681d0292013-11-22 12:47:34 -0500281 new Timer().schedule(quit, 1000);
Alexandre Lisionccf0c932013-10-10 16:24:41 -0400282 }
alision806e18e2013-06-21 15:30:17 -0400283
Alexandre Lision0c384512013-09-17 17:15:57 -0400284 @Override
Alexandre Lisiond5686032013-10-29 11:09:21 -0400285 public boolean sendIM(SipMessage msg) {
286
287 try {
Alexandre Lisiona7ab2e32014-02-14 15:33:33 -0500288 Log.i(TAG, "Sending:"+msg.comment+"to"+mDisplayedConference.getId());
289 mService.sendTextMessage(mDisplayedConference.getId(), msg);
Alexandre Lisiond5686032013-10-29 11:09:21 -0400290 } catch (RemoteException e) {
291 e.printStackTrace();
292 return false;
293 }
294
295 return true;
296 }
297
298 @Override
Alexandre Lision0c384512013-09-17 17:15:57 -0400299 public void startTimer() {
300 mHandler.postDelayed(mUpdateTimeTask, 0);
301 }
302
Alexandre Lision68855472013-10-10 16:20:46 -0400303 @Override
304 public void slideChatScreen() {
Alexandre Lision666b3772013-10-28 17:42:48 -0400305
Alexandre Lision47a72042013-12-10 10:43:02 -0500306 if (mSlidingPaneLayout.isOpen()) {
307 mSlidingPaneLayout.closePane();
Alexandre Lision666b3772013-10-28 17:42:48 -0400308 } else {
309 mCurrentCallFragment.getBubbleView().stopThread();
Alexandre Lision47a72042013-12-10 10:43:02 -0500310 mSlidingPaneLayout.openPane();
Alexandre Lision666b3772013-10-28 17:42:48 -0400311 }
Alexandre Lision68855472013-10-10 16:20:46 -0400312 }
Alexandre Lisionb8add812013-10-24 11:42:42 -0400313
314 @Override
315 public boolean shouldActivateProximity() {
316 return true;
317 }
318
319 @Override
320 public void onProximityTrackingChanged(boolean acquired) {
321 // TODO Stub de la méthode généré automatiquement
Alexandre Lision666b3772013-10-28 17:42:48 -0400322
Alexandre Lisionb8add812013-10-24 11:42:42 -0400323 }
Alexandre Savard14323be2012-10-24 10:02:13 -0400324}