blob: 1bd426756f10cfb49a2557a3cac768217cc15ad5 [file] [log] [blame]
alisionf76de3b2013-04-16 15:35:22 -04001/*
alision2ec64f92013-06-17 17:28:58 -04002 * Copyright (C) 2004-2013 Savoir-Faire Linux Inc.
alisionf76de3b2013-04-16 15:35:22 -04003 *
4 * Author: Adrien Beraud <adrien.beraud@gmail.com>
alision11e8e162013-05-28 10:33:14 -04005 * Alexandre Lision <alexandre.lision@savoirfairelinux.com>
alisionf76de3b2013-04-16 15:35:22 -04006 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 *
21 * Additional permission under GNU GPL version 3 section 7:
22 *
23 * If you modify this program, or any covered work, by linking or
24 * combining it with the OpenSSL project's OpenSSL library (or a
25 * modified version of that library), containing parts covered by the
26 * terms of the OpenSSL or SSLeay licenses, Savoir-Faire Linux Inc.
27 * grants you additional permission to convey the resulting work.
28 * Corresponding Source for a non-source form of such a combination
29 * shall include the source code for the parts of OpenSSL used as well
30 * as that of the covered work.
31 */
Alexandre Lision064e1e02013-10-01 16:18:42 -040032package org.sflphone.client;
alisionf76de3b2013-04-16 15:35:22 -040033
Alexandre Lision5ed2c972013-10-11 15:36:33 -040034import java.io.InvalidObjectException;
alisionbd45a822013-06-06 17:35:35 -040035import java.util.Timer;
36import java.util.TimerTask;
alision84813a12013-05-27 17:40:39 -040037
Alexandre Lision064e1e02013-10-01 16:18:42 -040038import org.sflphone.R;
39import org.sflphone.adapters.SectionsPagerAdapter;
40import org.sflphone.fragments.ContactListFragment;
41import org.sflphone.fragments.DialingFragment;
42import org.sflphone.fragments.HistoryFragment;
43import org.sflphone.fragments.HomeFragment;
44import org.sflphone.fragments.MenuFragment;
45import org.sflphone.interfaces.CallInterface;
Alexandre Lision064e1e02013-10-01 16:18:42 -040046import org.sflphone.model.CallContact;
47import org.sflphone.model.Conference;
48import org.sflphone.model.SipCall;
49import org.sflphone.receivers.CallReceiver;
50import org.sflphone.service.CallManagerCallBack;
51import org.sflphone.service.ISipService;
52import org.sflphone.service.SipService;
Alexandre Lisione5b66022013-10-30 11:34:15 -040053import org.sflphone.views.PagerSlidingTabStrip;
Alexandre Lision450458a2013-11-22 11:33:12 -050054import org.sflphone.views.SlidingUpPanelLayout;
55import org.sflphone.views.SlidingUpPanelLayout.PanelSlideListener;
Alexandre Lision064e1e02013-10-01 16:18:42 -040056
alisionf76de3b2013-04-16 15:35:22 -040057import android.app.Activity;
Alexandre Lisionc51ccb12013-09-11 16:00:30 -040058import android.app.AlertDialog;
alisionf76de3b2013-04-16 15:35:22 -040059import android.content.ComponentName;
60import android.content.Context;
Alexandre Lisionc51ccb12013-09-11 16:00:30 -040061import android.content.DialogInterface;
alisionf76de3b2013-04-16 15:35:22 -040062import android.content.Intent;
alision84813a12013-05-27 17:40:39 -040063import android.content.IntentFilter;
alisionf76de3b2013-04-16 15:35:22 -040064import android.content.ServiceConnection;
alisionf57d8a62013-07-02 09:37:12 -040065import android.content.res.Configuration;
alision55c36cb2013-06-14 14:57:38 -040066import android.database.Cursor;
Alexandre Lisionc59685a2013-10-04 16:36:36 -040067import android.net.Uri;
alisionf76de3b2013-04-16 15:35:22 -040068import android.os.Bundle;
69import android.os.IBinder;
alision5f899632013-04-22 17:26:56 -040070import android.os.RemoteException;
Alexandre Lisionc59685a2013-10-04 16:36:36 -040071import android.provider.ContactsContract;
alision55c36cb2013-06-14 14:57:38 -040072import android.provider.ContactsContract.CommonDataKinds.Phone;
73import android.provider.ContactsContract.CommonDataKinds.SipAddress;
alision9f7a6ec2013-05-24 16:26:26 -040074import android.support.v4.app.ActionBarDrawerToggle;
75import android.support.v4.view.GravityCompat;
alisionf76de3b2013-04-16 15:35:22 -040076import android.support.v4.view.ViewPager;
alision9f7a6ec2013-05-24 16:26:26 -040077import android.support.v4.widget.DrawerLayout;
alisionf76de3b2013-04-16 15:35:22 -040078import android.util.Log;
Alexandre Lision3ccccf02013-10-07 14:10:46 -040079import android.view.Gravity;
alision9f7a6ec2013-05-24 16:26:26 -040080import android.view.MenuItem;
81import android.view.View;
Alexandre Lision68900052013-11-01 11:34:56 -040082import android.widget.ImageView;
alision7297bdb2013-05-21 11:56:55 -040083import android.widget.RelativeLayout;
alision04a00182013-05-10 17:05:29 -040084import android.widget.Toast;
alisionf76de3b2013-04-16 15:35:22 -040085
Alexandre Lision4ab53972013-11-04 16:59:18 -050086public class HomeActivity extends Activity implements DialingFragment.Callbacks, ContactListFragment.Callbacks, HomeFragment.Callbacks,
alision907bde72013-06-20 14:40:37 -040087 HistoryFragment.Callbacks, CallInterface, MenuFragment.Callbacks {
alisionfde875f2013-05-28 17:01:54 -040088
alision9f7a6ec2013-05-24 16:26:26 -040089 SectionsPagerAdapter mSectionsPagerAdapter = null;
Alexandre Lision9d052a12013-11-06 15:13:34 -050090 static final String TAG = "HomeActivity";
alisionf76de3b2013-04-16 15:35:22 -040091
alision9f7a6ec2013-05-24 16:26:26 -040092 private ContactListFragment mContactsFragment = null;
alision907bde72013-06-20 14:40:37 -040093 private MenuFragment fMenu;
alisionf76de3b2013-04-16 15:35:22 -040094
alision9f7a6ec2013-05-24 16:26:26 -040095 private boolean mBound = false;
96 private ISipService service;
alisionf76de3b2013-04-16 15:35:22 -040097
alision9f7a6ec2013-05-24 16:26:26 -040098 private CharSequence mDrawerTitle;
99 private CharSequence mTitle;
alisionf76de3b2013-04-16 15:35:22 -0400100
alision2cb99562013-05-30 17:02:20 -0400101 public static final int REQUEST_CODE_PREFERENCES = 1;
alision9f7a6ec2013-05-24 16:26:26 -0400102 private static final int REQUEST_CODE_CALL = 2;
alisionf76de3b2013-04-16 15:35:22 -0400103
alision0b553832013-05-30 09:31:06 -0400104 RelativeLayout mSliderButton;
Alexandre Lision450458a2013-11-22 11:33:12 -0500105 SlidingUpPanelLayout mContactDrawer;
Alexandre Lision68900052013-11-01 11:34:56 -0400106 private DrawerLayout mNavigationDrawer;
alision9f7a6ec2013-05-24 16:26:26 -0400107 private ActionBarDrawerToggle mDrawerToggle;
Alexandre Lisionc87763a2013-11-01 12:53:38 -0400108 ImageView mShadow;
alision9f7a6ec2013-05-24 16:26:26 -0400109 /**
110 * The {@link ViewPager} that will host the section contents.
111 */
112 ViewPager mViewPager;
alisionf76de3b2013-04-16 15:35:22 -0400113
alision58356b72013-06-03 17:13:36 -0400114 CallReceiver callReceiver;
Alexandre Lision9d052a12013-11-06 15:13:34 -0500115 private boolean isClosing = false;
116 private Timer t = new Timer();
alision84813a12013-05-27 17:40:39 -0400117
alision9f7a6ec2013-05-24 16:26:26 -0400118 /* called before activity is killed, e.g. rotation */
119 @Override
120 protected void onSaveInstanceState(Bundle bundle) {
121 super.onSaveInstanceState(bundle);
alision9f7a6ec2013-05-24 16:26:26 -0400122 getFragmentManager().putFragment(bundle, "ContactsListFragment", mContactsFragment);
123 Log.w(TAG, "onSaveInstanceState()");
124 }
125
126 @Override
127 public void onCreate(Bundle savedInstanceState) {
128 super.onCreate(savedInstanceState);
129
alision58356b72013-06-03 17:13:36 -0400130 callReceiver = new CallReceiver(this);
alision907bde72013-06-20 14:40:37 -0400131
alision58356b72013-06-03 17:13:36 -0400132 setContentView(R.layout.activity_sflphone_home);
133
alision9f7a6ec2013-05-24 16:26:26 -0400134 // Bind to LocalService
135 if (!mBound) {
136 Log.i(TAG, "onStart: Binding service...");
137 Intent intent = new Intent(this, SipService.class);
Alexandre Lision0f550ee2013-10-25 15:34:38 -0400138 startService(intent);
alision9f7a6ec2013-05-24 16:26:26 -0400139 bindService(intent, mConnection, Context.BIND_AUTO_CREATE);
140 }
Alexandre Lision8b9d8e82013-10-04 09:21:27 -0400141
142 if (savedInstanceState != null) {
Alexandre Lision23628c12013-09-24 11:17:05 -0400143 mContactsFragment = (ContactListFragment) getFragmentManager().getFragment(savedInstanceState, "ContactsListFragment");
144 }
alision9f7a6ec2013-05-24 16:26:26 -0400145 if (mContactsFragment == null) {
146 mContactsFragment = new ContactListFragment();
alision9f7a6ec2013-05-24 16:26:26 -0400147 getFragmentManager().beginTransaction().replace(R.id.contacts_frame, mContactsFragment).commit();
148 }
alision2ec64f92013-06-17 17:28:58 -0400149
Alexandre Lision450458a2013-11-22 11:33:12 -0500150 mContactDrawer = (SlidingUpPanelLayout) findViewById(R.id.contact_panel);
151 // mContactDrawer.setShadowDrawable(getResources().getDrawable(R.drawable.above_shadow));
152 mContactDrawer.setAnchorPoint(0.3f);
153 mContactDrawer.setDragView(findViewById(R.id.contacts_frame));
154 mContactDrawer.setEnableDragViewTouchEvents(true);
Alexandre Lision450458a2013-11-22 11:33:12 -0500155 mContactDrawer.setPanelSlideListener(new PanelSlideListener() {
Alexandre Lision8b9d8e82013-10-04 09:21:27 -0400156
157 @Override
Alexandre Lision450458a2013-11-22 11:33:12 -0500158 public void onPanelSlide(View panel, float slideOffset) {
159 if (slideOffset < 0.2) {
160 if (getActionBar().isShowing()) {
161 getActionBar().hide();
162 }
163 } else {
164 if (!getActionBar().isShowing()) {
165 getActionBar().show();
166 }
Alexandre Lision8b9d8e82013-10-04 09:21:27 -0400167 }
168 }
Alexandre Lision450458a2013-11-22 11:33:12 -0500169
170 @Override
171 public void onPanelExpanded(View panel) {
172
173 }
174
175 @Override
176 public void onPanelCollapsed(View panel) {
177
178 }
179
180 @Override
181 public void onPanelAnchored(View panel) {
182
183 }
Alexandre Lision8b9d8e82013-10-04 09:21:27 -0400184 });
Alexandre Lisiond588bff2013-10-08 12:43:01 -0400185
Alexandre Lisionc87763a2013-11-01 12:53:38 -0400186 mShadow = (ImageView) findViewById(R.id.overall_shadow);
alisionf76de3b2013-04-16 15:35:22 -0400187
alision9f7a6ec2013-05-24 16:26:26 -0400188 // Set up the ViewPager with the sections adapter.
189 mViewPager = (ViewPager) findViewById(R.id.pager);
alision55c36cb2013-06-14 14:57:38 -0400190 mViewPager.setPageTransformer(true, new ZoomOutPageTransformer(0.7f));
alisionf76de3b2013-04-16 15:35:22 -0400191
alision9f7a6ec2013-05-24 16:26:26 -0400192 mTitle = mDrawerTitle = getTitle();
Alexandre Lision68900052013-11-01 11:34:56 -0400193 mNavigationDrawer = (DrawerLayout) findViewById(R.id.drawer_layout);
alisionf76de3b2013-04-16 15:35:22 -0400194
alision0b553832013-05-30 09:31:06 -0400195 // set a custom shadow that overlays the main content when the drawer opens
Alexandre Lision68900052013-11-01 11:34:56 -0400196 mNavigationDrawer.setDrawerShadow(R.drawable.drawer_shadow, GravityCompat.START);
alisionf76de3b2013-04-16 15:35:22 -0400197
alision9f7a6ec2013-05-24 16:26:26 -0400198 getActionBar().setDisplayHomeAsUpEnabled(true);
199 getActionBar().setHomeButtonEnabled(true);
alisionf76de3b2013-04-16 15:35:22 -0400200
alision9f7a6ec2013-05-24 16:26:26 -0400201 mDrawerToggle = new ActionBarDrawerToggle(this, /* host Activity */
Alexandre Lision68900052013-11-01 11:34:56 -0400202 mNavigationDrawer, /* DrawerLayout object */
alision9f7a6ec2013-05-24 16:26:26 -0400203 R.drawable.ic_drawer, /* nav drawer image to replace 'Up' caret */
204 R.string.drawer_open, /* "open drawer" description for accessibility */
205 R.string.drawer_close /* "close drawer" description for accessibility */
206 ) {
alision0b553832013-05-30 09:31:06 -0400207 @Override
alision9f7a6ec2013-05-24 16:26:26 -0400208 public void onDrawerClosed(View view) {
209 getActionBar().setTitle(mTitle);
210 invalidateOptionsMenu(); // creates call to onPrepareOptionsMenu()
211 }
alisionf76de3b2013-04-16 15:35:22 -0400212
alision0b553832013-05-30 09:31:06 -0400213 @Override
alision9f7a6ec2013-05-24 16:26:26 -0400214 public void onDrawerOpened(View drawerView) {
215 getActionBar().setTitle(mDrawerTitle);
216 invalidateOptionsMenu(); // creates call to onPrepareOptionsMenu()
217 }
218 };
alisionf76de3b2013-04-16 15:35:22 -0400219
Alexandre Lision68900052013-11-01 11:34:56 -0400220 mNavigationDrawer.setDrawerListener(mDrawerToggle);
alisionf76de3b2013-04-16 15:35:22 -0400221
alision9f7a6ec2013-05-24 16:26:26 -0400222 }
alisionf76de3b2013-04-16 15:35:22 -0400223
alisionf57d8a62013-07-02 09:37:12 -0400224 @Override
225 protected void onPostCreate(Bundle savedInstanceState) {
226 super.onPostCreate(savedInstanceState);
227 // Sync the toggle state after onRestoreInstanceState has occurred.
228 mDrawerToggle.syncState();
Alexandre Lision450458a2013-11-22 11:33:12 -0500229 if (mContactDrawer.isExpanded()) {
Alexandre Lisiond16bad92013-10-09 17:16:20 -0400230 getActionBar().hide();
231 }
alisionf57d8a62013-07-02 09:37:12 -0400232 }
alision465ceba2013-07-04 09:24:30 -0400233
alisionf57d8a62013-07-02 09:37:12 -0400234 @Override
235 public void onConfigurationChanged(Configuration newConfig) {
236 super.onConfigurationChanged(newConfig);
237 mDrawerToggle.onConfigurationChanged(newConfig);
238 }
alision3c5c2a92013-05-30 09:50:47 -0400239
alision9f7a6ec2013-05-24 16:26:26 -0400240 @Override
241 protected void onStart() {
242 Log.i(TAG, "onStart");
243 super.onStart();
Alexandre Lisionc51ccb12013-09-11 16:00:30 -0400244
alision9f7a6ec2013-05-24 16:26:26 -0400245 }
alisionf76de3b2013-04-16 15:35:22 -0400246
alision9f7a6ec2013-05-24 16:26:26 -0400247 /* user gets back to the activity, e.g. through task manager */
248 @Override
249 protected void onRestart() {
250 super.onRestart();
251 }
alisionf76de3b2013-04-16 15:35:22 -0400252
alision9f7a6ec2013-05-24 16:26:26 -0400253 /* activity gets back to the foreground and user input */
254 @Override
255 protected void onResume() {
alision58356b72013-06-03 17:13:36 -0400256 super.onResume();
alision84813a12013-05-27 17:40:39 -0400257 IntentFilter intentFilter = new IntentFilter();
258 intentFilter.addAction(CallManagerCallBack.INCOMING_CALL);
259 intentFilter.addAction(CallManagerCallBack.INCOMING_TEXT);
260 intentFilter.addAction(CallManagerCallBack.CALL_STATE_CHANGED);
alision58356b72013-06-03 17:13:36 -0400261 registerReceiver(callReceiver, intentFilter);
alision907bde72013-06-20 14:40:37 -0400262
alision9f7a6ec2013-05-24 16:26:26 -0400263 }
alision907bde72013-06-20 14:40:37 -0400264
alisionbd45a822013-06-06 17:35:35 -0400265 @Override
266 public void onBackPressed() {
Alexandre Lisiond588bff2013-10-08 12:43:01 -0400267
Alexandre Lision68900052013-11-01 11:34:56 -0400268 if (mNavigationDrawer.isDrawerVisible(Gravity.LEFT)) {
269 mNavigationDrawer.closeDrawer(Gravity.LEFT);
Alexandre Lision68855472013-10-10 16:20:46 -0400270 return;
Alexandre Lision3ccccf02013-10-07 14:10:46 -0400271 }
alision907bde72013-06-20 14:40:37 -0400272
Alexandre Lision450458a2013-11-22 11:33:12 -0500273 if (mContactDrawer.isExpanded() || mContactDrawer.isAnchored()) {
274 mContactDrawer.collapsePane();
alisionbd45a822013-06-06 17:35:35 -0400275 return;
276 }
alision2ec64f92013-06-17 17:28:58 -0400277
alisionbd45a822013-06-06 17:35:35 -0400278 if (isClosing) {
279 super.onBackPressed();
280 t.cancel();
281 finish();
282 } else {
283
284 t.schedule(new TimerTask() {
285 @Override
286 public void run() {
287 isClosing = false;
288 }
289 }, 3000);
290 Toast.makeText(this, getResources().getString(R.string.close_msg), Toast.LENGTH_SHORT).show();
291 isClosing = true;
292 }
293 }
alisione2a38e12013-04-25 14:20:20 -0400294
alision9f7a6ec2013-05-24 16:26:26 -0400295 /* activity no more in foreground */
296 @Override
297 protected void onPause() {
298 super.onPause();
alision58356b72013-06-03 17:13:36 -0400299 unregisterReceiver(callReceiver);
Alexandre Lisionb4a9e392013-10-01 14:39:43 -0400300 if (mBound) {
301 unbindService(mConnection);
302 mBound = false;
303 }
alision9f7a6ec2013-05-24 16:26:26 -0400304 }
alisionf76de3b2013-04-16 15:35:22 -0400305
alision9f7a6ec2013-05-24 16:26:26 -0400306 /* activity finishes itself or is being killed by the system */
307 @Override
308 protected void onDestroy() {
alision9f7a6ec2013-05-24 16:26:26 -0400309 super.onDestroy();
Alexandre Lisionb4a9e392013-10-01 14:39:43 -0400310 Log.i(TAG, "onDestroy: destroying service...");
311 Intent sipServiceIntent = new Intent(this, SipService.class);
312 stopService(sipServiceIntent);
alision9f7a6ec2013-05-24 16:26:26 -0400313 }
alision73424b62013-04-26 11:49:18 -0400314
alision55c36cb2013-06-14 14:57:38 -0400315 public void launchCallActivity(SipCall infos) {
Alexandre Lisionc59685a2013-10-04 16:36:36 -0400316
alision9f7a6ec2013-05-24 16:26:26 -0400317 Bundle bundle = new Bundle();
alisiondf1dac92013-06-27 17:35:53 -0400318 Conference tmp = new Conference("-1");
alision465ceba2013-07-04 09:24:30 -0400319
alisiondf1dac92013-06-27 17:35:53 -0400320 tmp.getParticipants().add(infos);
alision465ceba2013-07-04 09:24:30 -0400321
alisiondf1dac92013-06-27 17:35:53 -0400322 bundle.putParcelable("conference", tmp);
alision9f7a6ec2013-05-24 16:26:26 -0400323 Intent intent = new Intent().setClass(this, CallActivity.class);
alisiondf1dac92013-06-27 17:35:53 -0400324 intent.putExtra("resuming", false);
alision9f7a6ec2013-05-24 16:26:26 -0400325 intent.putExtras(bundle);
326 startActivityForResult(intent, REQUEST_CODE_CALL);
Alexandre Lisiond588bff2013-10-08 12:43:01 -0400327
Alexandre Lision8b9d8e82013-10-04 09:21:27 -0400328 // overridePendingTransition(R.anim.slide_down, R.anim.slide_up);
alision9f7a6ec2013-05-24 16:26:26 -0400329 }
alisionf76de3b2013-04-16 15:35:22 -0400330
alision9f7a6ec2013-05-24 16:26:26 -0400331 /** Defines callbacks for service binding, passed to bindService() */
332 private ServiceConnection mConnection = new ServiceConnection() {
alision371b77e2013-04-23 14:51:26 -0400333
alision9f7a6ec2013-05-24 16:26:26 -0400334 @Override
335 public void onServiceConnected(ComponentName className, IBinder binder) {
336 service = ISipService.Stub.asInterface(binder);
alisione2a38e12013-04-25 14:20:20 -0400337
alision2cb99562013-05-30 17:02:20 -0400338 try {
alision58356b72013-06-03 17:13:36 -0400339
alision907bde72013-06-20 14:40:37 -0400340 fMenu = new MenuFragment();
341 getFragmentManager().beginTransaction().replace(R.id.left_drawer, fMenu).commit();
Alexandre Lision4ab53972013-11-04 16:59:18 -0500342 mSectionsPagerAdapter = new SectionsPagerAdapter(HomeActivity.this, getFragmentManager());
Alexandre Lisionc51ccb12013-09-11 16:00:30 -0400343
alisiondf1dac92013-06-27 17:35:53 -0400344 mViewPager.setOffscreenPageLimit(2);
alision58356b72013-06-03 17:13:36 -0400345 mViewPager.setAdapter(mSectionsPagerAdapter);
Alexandre Lisionc51ccb12013-09-11 16:00:30 -0400346 mViewPager.setCurrentItem(1);
Alexandre Lisione5b66022013-10-30 11:34:15 -0400347
348 final PagerSlidingTabStrip strip = PagerSlidingTabStrip.class.cast(findViewById(R.id.pts_main));
349
Alexandre Lisione5b66022013-10-30 11:34:15 -0400350 strip.setViewPager(mViewPager);
alision2cb99562013-05-30 17:02:20 -0400351 service.destroyNotification();
352 } catch (RemoteException e) {
353 Log.e(TAG, e.toString());
354 }
alision9f7a6ec2013-05-24 16:26:26 -0400355 mBound = true;
alision9f7a6ec2013-05-24 16:26:26 -0400356 Log.d(TAG, "Service connected service=" + service);
alision371b77e2013-04-23 14:51:26 -0400357
alision9f7a6ec2013-05-24 16:26:26 -0400358 }
alision5f899632013-04-22 17:26:56 -0400359
alision9f7a6ec2013-05-24 16:26:26 -0400360 @Override
361 public void onServiceDisconnected(ComponentName arg0) {
alision5f899632013-04-22 17:26:56 -0400362
alision9f7a6ec2013-05-24 16:26:26 -0400363 mBound = false;
364 Log.d(TAG, "Service disconnected service=" + service);
365 }
366 };
alision5f899632013-04-22 17:26:56 -0400367
alision9f7a6ec2013-05-24 16:26:26 -0400368 @Override
369 public boolean onOptionsItemSelected(MenuItem item) {
370 Log.i(TAG, "onOptionsItemSelected " + item.getItemId());
alision5f899632013-04-22 17:26:56 -0400371
alision9f7a6ec2013-05-24 16:26:26 -0400372 if (mDrawerToggle.onOptionsItemSelected(item)) {
373 return true;
374 }
alision5f899632013-04-22 17:26:56 -0400375
alision9f7a6ec2013-05-24 16:26:26 -0400376 return super.onOptionsItemSelected(item);
377 }
alisionf76de3b2013-04-16 15:35:22 -0400378
alision9f7a6ec2013-05-24 16:26:26 -0400379 @Override
380 public void onActivityResult(int requestCode, int resultCode, Intent data) {
381 super.onActivityResult(requestCode, resultCode, data);
alision84813a12013-05-27 17:40:39 -0400382
383 switch (requestCode) {
alision9f7a6ec2013-05-24 16:26:26 -0400384 case REQUEST_CODE_PREFERENCES:
Alexandre Lisione5b66022013-10-30 11:34:15 -0400385 if (fMenu != null)
Alexandre Lision933ef0a2013-10-15 17:28:40 -0400386 fMenu.updateAllAccounts();
alision9f7a6ec2013-05-24 16:26:26 -0400387 break;
388 case REQUEST_CODE_CALL:
Alexandre Lisionc51ccb12013-09-11 16:00:30 -0400389 if (resultCode == CallActivity.RESULT_FAILURE) {
Alexandre Lision8b9d8e82013-10-04 09:21:27 -0400390 Log.w(TAG, "Call Failed");
Alexandre Lisionc51ccb12013-09-11 16:00:30 -0400391 }
392 // if (mSectionsPagerAdapter != null && mSectionsPagerAdapter.getItem(2) != null)
393 // getLoaderManager().restartLoader(LoaderConstants.HISTORY_LOADER, null, (HistoryFragment) mSectionsPagerAdapter.getItem(2));
alision9f7a6ec2013-05-24 16:26:26 -0400394 break;
395 }
alisionf76de3b2013-04-16 15:35:22 -0400396
alision9f7a6ec2013-05-24 16:26:26 -0400397 }
alisionf76de3b2013-04-16 15:35:22 -0400398
alision9f7a6ec2013-05-24 16:26:26 -0400399 @Override
alision9f7a6ec2013-05-24 16:26:26 -0400400 public ISipService getService() {
401 return service;
402 }
alisionf76de3b2013-04-16 15:35:22 -0400403
alision84813a12013-05-27 17:40:39 -0400404 /**
405 * Interface implemented to handle incoming events
406 */
alision9f7a6ec2013-05-24 16:26:26 -0400407 @Override
alision84813a12013-05-27 17:40:39 -0400408 public void incomingCall(Intent call) {
alisionfde875f2013-05-28 17:01:54 -0400409 SipCall infos = call.getParcelableExtra("newcall");
alision84813a12013-05-27 17:40:39 -0400410
alision58356b72013-06-03 17:13:36 -0400411 // mCallElementList.addCall(infos);
alisionfde875f2013-05-28 17:01:54 -0400412
alision55c36cb2013-06-14 14:57:38 -0400413 launchCallActivity(infos);
alision84813a12013-05-27 17:40:39 -0400414
415 }
416
417 @Override
418 public void callStateChanged(Intent callState) {
419 Bundle b = callState.getBundleExtra("com.savoirfairelinux.sflphone.service.newstate");
420 String cID = b.getString("CallID");
421 String state = b.getString("State");
422 Log.i(TAG, "callStateChanged" + cID + " " + state);
Alexandre Lision1a9e3b12013-09-16 11:06:07 -0400423 mSectionsPagerAdapter.updateHome();
alision84813a12013-05-27 17:40:39 -0400424
425 }
426
427 @Override
428 public void incomingText(Intent msg) {
429 Bundle b = msg.getBundleExtra("com.savoirfairelinux.sflphone.service.newtext");
430 b.getString("CallID");
431 String from = b.getString("From");
432 String mess = b.getString("Msg");
433 Toast.makeText(getApplicationContext(), "text from " + from + " : " + mess, Toast.LENGTH_LONG).show();
434 }
Alexandre Lisiond588bff2013-10-08 12:43:01 -0400435
Alexandre Lisione1c96db2013-10-04 14:34:21 -0400436 @Override
437 public void onTextContact(final CallContact c) {
438 // TODO
439 }
Alexandre Lisiond588bff2013-10-08 12:43:01 -0400440
Alexandre Lisionc59685a2013-10-04 16:36:36 -0400441 @Override
442 public void onEditContact(final CallContact c) {
443 Intent intent = new Intent(Intent.ACTION_VIEW);
444 Uri uri = Uri.withAppendedPath(ContactsContract.Contacts.CONTENT_URI, String.valueOf(c.getId()));
445 intent.setData(uri);
446 startActivity(intent);
447 }
alision84813a12013-05-27 17:40:39 -0400448
449 @Override
Alexandre Lisione1c96db2013-10-04 14:34:21 -0400450 public void onCallContact(final CallContact c) {
Alexandre Lision8b9d8e82013-10-04 09:21:27 -0400451
Alexandre Lisionf0b993d2013-09-12 13:25:10 -0400452 if (fMenu.getSelectedAccount() == null) {
Alexandre Lisionf0b993d2013-09-12 13:25:10 -0400453 createAccountDialog().show();
454 return;
455 }
alisionfde875f2013-05-28 17:01:54 -0400456
Alexandre Lisiond588bff2013-10-08 12:43:01 -0400457 if (!fMenu.getSelectedAccount().isRegistered()) {
458 createNotRegisteredDialog().show();
459 return;
460 }
461
Alexandre Lisiond16bad92013-10-09 17:16:20 -0400462 getActionBar().show();
alision55c36cb2013-06-14 14:57:38 -0400463 Thread launcher = new Thread(new Runnable() {
alision907bde72013-06-20 14:40:37 -0400464
alision55c36cb2013-06-14 14:57:38 -0400465 final String[] CONTACTS_PHONES_PROJECTION = new String[] { Phone.NUMBER, Phone.TYPE };
466 final String[] CONTACTS_SIP_PROJECTION = new String[] { SipAddress.SIP_ADDRESS, SipAddress.TYPE };
alision907bde72013-06-20 14:40:37 -0400467
alision55c36cb2013-06-14 14:57:38 -0400468 @Override
469 public void run() {
470 SipCall.SipCallBuilder callBuilder = SipCall.SipCallBuilder.getInstance();
471 try {
Alexandre Lisiond588bff2013-10-08 12:43:01 -0400472 callBuilder.startCallCreation().setAccount(fMenu.getSelectedAccount()).setCallType(SipCall.state.CALL_TYPE_OUTGOING);
alision907bde72013-06-20 14:40:37 -0400473 Cursor cPhones = getContentResolver().query(Phone.CONTENT_URI, CONTACTS_PHONES_PROJECTION, Phone.CONTACT_ID + " =" + c.getId(),
474 null, null);
alision55c36cb2013-06-14 14:57:38 -0400475
alision907bde72013-06-20 14:40:37 -0400476 while (cPhones.moveToNext()) {
477 c.addPhoneNumber(cPhones.getString(cPhones.getColumnIndex(Phone.NUMBER)), cPhones.getInt(cPhones.getColumnIndex(Phone.TYPE)));
478 }
479 cPhones.close();
480
481 Cursor cSip = getContentResolver().query(Phone.CONTENT_URI, CONTACTS_SIP_PROJECTION, Phone.CONTACT_ID + "=" + c.getId(), null,
482 null);
483
484 while (cSip.moveToNext()) {
485 c.addSipNumber(cSip.getString(cSip.getColumnIndex(SipAddress.SIP_ADDRESS)), cSip.getInt(cSip.getColumnIndex(SipAddress.TYPE)));
486 }
487 cSip.close();
488 callBuilder.setContact(c);
alision55c36cb2013-06-14 14:57:38 -0400489 launchCallActivity(callBuilder.build());
Alexandre Lision5ed2c972013-10-11 15:36:33 -0400490 } catch (InvalidObjectException e) {
491 e.printStackTrace();
alision55c36cb2013-06-14 14:57:38 -0400492 }
alision907bde72013-06-20 14:40:37 -0400493
alision55c36cb2013-06-14 14:57:38 -0400494 }
495 });
496 launcher.start();
Alexandre Lision450458a2013-11-22 11:33:12 -0500497 mContactDrawer.collapsePane();
alision84813a12013-05-27 17:40:39 -0400498
499 }
500
501 @Override
alision907bde72013-06-20 14:40:37 -0400502 public void onCallDialed(String to) {
alision84813a12013-05-27 17:40:39 -0400503
alisiondf1dac92013-06-27 17:35:53 -0400504 if (fMenu.getSelectedAccount() == null) {
Alexandre Lisionc51ccb12013-09-11 16:00:30 -0400505 createAccountDialog().show();
alisiondf1dac92013-06-27 17:35:53 -0400506 return;
507 }
508
Alexandre Lisiond588bff2013-10-08 12:43:01 -0400509 if (fMenu.getSelectedAccount().isRegistered()) {
510 SipCall.SipCallBuilder callBuilder = SipCall.SipCallBuilder.getInstance();
511 callBuilder.startCallCreation().setAccount(fMenu.getSelectedAccount()).setCallType(SipCall.state.CALL_TYPE_OUTGOING);
512 callBuilder.setContact(CallContact.ContactBuilder.buildUnknownContact(to));
alision84813a12013-05-27 17:40:39 -0400513
Alexandre Lisiond588bff2013-10-08 12:43:01 -0400514 try {
515 launchCallActivity(callBuilder.build());
516 } catch (Exception e) {
517 Log.e(TAG, e.toString());
518 }
519 } else {
Alexandre Lision5ed2c972013-10-11 15:36:33 -0400520 createNotRegisteredDialog().show();
521 ;
alisionfde875f2013-05-28 17:01:54 -0400522 }
alision84813a12013-05-27 17:40:39 -0400523
alision9f7a6ec2013-05-24 16:26:26 -0400524 }
alision371b77e2013-04-23 14:51:26 -0400525
Alexandre Lisiond588bff2013-10-08 12:43:01 -0400526 private AlertDialog createNotRegisteredDialog() {
527 final Activity ownerActivity = this;
528 AlertDialog.Builder builder = new AlertDialog.Builder(ownerActivity);
529
530 builder.setMessage(getResources().getString(R.string.cannot_pass_sipcall))
531 .setTitle(getResources().getString(R.string.cannot_pass_sipcall_title))
532 .setPositiveButton(getResources().getString(android.R.string.ok), new DialogInterface.OnClickListener() {
533 public void onClick(DialogInterface dialog, int whichButton) {
Alexandre Lision5ed2c972013-10-11 15:36:33 -0400534
Alexandre Lisiond588bff2013-10-08 12:43:01 -0400535 }
536 });
537
538 AlertDialog alertDialog = builder.create();
539 alertDialog.setOwnerActivity(ownerActivity);
540
541 return alertDialog;
542 }
543
Alexandre Lisionc51ccb12013-09-11 16:00:30 -0400544 private AlertDialog createAccountDialog() {
545 final Activity ownerActivity = this;
546 AlertDialog.Builder builder = new AlertDialog.Builder(ownerActivity);
547
548 builder.setMessage(getResources().getString(R.string.create_new_account_dialog))
549 .setTitle(getResources().getString(R.string.create_new_account_dialog_title))
550 .setPositiveButton(getResources().getString(android.R.string.ok), new DialogInterface.OnClickListener() {
551 public void onClick(DialogInterface dialog, int whichButton) {
552 Intent in = new Intent();
Alexandre Lision40954dc2013-10-09 15:24:03 -0400553 in.setClass(ownerActivity, AccountWizard.class);
Alexandre Lision4ab53972013-11-04 16:59:18 -0500554 ownerActivity.startActivityForResult(in, HomeActivity.REQUEST_CODE_PREFERENCES);
Alexandre Lisionc51ccb12013-09-11 16:00:30 -0400555 }
556 }).setNegativeButton(getResources().getString(android.R.string.cancel), new DialogInterface.OnClickListener() {
557 public void onClick(DialogInterface dialog, int whichButton) {
558 dialog.dismiss();
559 }
560 });
561
562 AlertDialog alertDialog = builder.create();
563 alertDialog.setOwnerActivity(ownerActivity);
564
565 return alertDialog;
566 }
567
alisiond45da712013-05-30 09:18:49 -0400568 @Override
569 public void onContactDragged() {
Alexandre Lision450458a2013-11-22 11:33:12 -0500570 mContactDrawer.collapsePane();
alisiond45da712013-05-30 09:18:49 -0400571 }
572
alision58356b72013-06-03 17:13:36 -0400573 @Override
Alexandre Lision0936ba62013-11-25 10:04:56 -0500574 public void toggleDrawer() {
575 if (mContactDrawer.isExpanded())
576 mContactDrawer.collapsePane();
577 else
578 mContactDrawer.expandPane();
alision58356b72013-06-03 17:13:36 -0400579 }
580
alision806e18e2013-06-21 15:30:17 -0400581 @Override
582 public void confCreated(Intent intent) {
583 // TODO Auto-generated method stub
alisiondf1dac92013-06-27 17:35:53 -0400584
alision806e18e2013-06-21 15:30:17 -0400585 }
586
587 @Override
588 public void confRemoved(Intent intent) {
589 // TODO Auto-generated method stub
alisiondf1dac92013-06-27 17:35:53 -0400590
alision806e18e2013-06-21 15:30:17 -0400591 }
592
593 @Override
594 public void confChanged(Intent intent) {
595 // TODO Auto-generated method stub
alisiondf1dac92013-06-27 17:35:53 -0400596
597 }
598
599 @Override
600 public void recordingChanged(Intent intent) {
601 // TODO Auto-generated method stub
602
603 }
604
605 @Override
Alexandre Lision1a9e3b12013-09-16 11:06:07 -0400606 public void selectedCall(Conference c) {
alisiondf1dac92013-06-27 17:35:53 -0400607 Intent intent = new Intent().setClass(this, CallActivity.class);
608 intent.putExtra("resuming", true);
Alexandre Lision1a9e3b12013-09-16 11:06:07 -0400609 intent.putExtra("conference", c);
alisiondf1dac92013-06-27 17:35:53 -0400610 startActivityForResult(intent, REQUEST_CODE_CALL);
alision806e18e2013-06-21 15:30:17 -0400611 }
Alexandre Lision8b9d8e82013-10-04 09:21:27 -0400612
Alexandre Lision3f1aa682013-11-18 10:49:28 -0500613 public class ZoomOutPageTransformer implements ViewPager.PageTransformer {
Alexandre Lisionee37f7c2013-09-25 17:36:39 -0400614 private static final float MIN_ALPHA = .6f;
Alexandre Lision8b9d8e82013-10-04 09:21:27 -0400615
Alexandre Lisionee37f7c2013-09-25 17:36:39 -0400616 public ZoomOutPageTransformer(float scalingStart) {
617 super();
Alexandre Lisionee37f7c2013-09-25 17:36:39 -0400618 }
619
620 @Override
621 public void transformPage(View page, float position) {
Alexandre Lisionee37f7c2013-09-25 17:36:39 -0400622 final float normalizedposition = Math.abs(Math.abs(position) - 1);
Alexandre Lision8b9d8e82013-10-04 09:21:27 -0400623 page.setAlpha(MIN_ALPHA + (1.f - MIN_ALPHA) * normalizedposition);
Alexandre Lisionee37f7c2013-09-25 17:36:39 -0400624 }
625 }
alision806e18e2013-06-21 15:30:17 -0400626
Alexandre Lision0936ba62013-11-25 10:04:56 -0500627 @Override
628 public void setDragView(RelativeLayout relativeLayout) {
629 mContactDrawer.setDragView(relativeLayout);
630 }
631
alisionf76de3b2013-04-16 15:35:22 -0400632}