blob: cac929641417f23eaf8c3ee9809b46188884b147 [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 */
32package com.savoirfairelinux.sflphone.client;
33
alisiond45da712013-05-30 09:18:49 -040034import java.util.HashMap;
alisionbd45a822013-06-06 17:35:35 -040035import java.util.Timer;
36import java.util.TimerTask;
alision84813a12013-05-27 17:40:39 -040037
alisionf76de3b2013-04-16 15:35:22 -040038import android.app.Activity;
alisionf76de3b2013-04-16 15:35:22 -040039import android.content.ComponentName;
40import android.content.Context;
41import android.content.Intent;
alision84813a12013-05-27 17:40:39 -040042import android.content.IntentFilter;
alisionf76de3b2013-04-16 15:35:22 -040043import android.content.ServiceConnection;
alisionf57d8a62013-07-02 09:37:12 -040044import android.content.res.Configuration;
alision55c36cb2013-06-14 14:57:38 -040045import android.database.Cursor;
alisionf76de3b2013-04-16 15:35:22 -040046import android.os.Bundle;
47import android.os.IBinder;
alision5f899632013-04-22 17:26:56 -040048import android.os.RemoteException;
alision55c36cb2013-06-14 14:57:38 -040049import android.provider.ContactsContract.CommonDataKinds.Phone;
50import android.provider.ContactsContract.CommonDataKinds.SipAddress;
alision9f7a6ec2013-05-24 16:26:26 -040051import android.support.v4.app.ActionBarDrawerToggle;
52import android.support.v4.view.GravityCompat;
alisionf76de3b2013-04-16 15:35:22 -040053import android.support.v4.view.ViewPager;
alision9f7a6ec2013-05-24 16:26:26 -040054import android.support.v4.widget.DrawerLayout;
alisionf76de3b2013-04-16 15:35:22 -040055import android.util.Log;
alision9f7a6ec2013-05-24 16:26:26 -040056import android.view.MenuItem;
57import android.view.View;
alision7297bdb2013-05-21 11:56:55 -040058import android.widget.RelativeLayout;
alisiond45da712013-05-30 09:18:49 -040059import android.widget.TabHost;
alision3c5c2a92013-05-30 09:50:47 -040060import android.widget.TabHost.OnTabChangeListener;
alisiond45da712013-05-30 09:18:49 -040061import android.widget.TabHost.TabContentFactory;
alision04a00182013-05-10 17:05:29 -040062import android.widget.Toast;
alisionf76de3b2013-04-16 15:35:22 -040063
64import com.savoirfairelinux.sflphone.R;
alision9f7a6ec2013-05-24 16:26:26 -040065import com.savoirfairelinux.sflphone.adapters.SectionsPagerAdapter;
alisionf76de3b2013-04-16 15:35:22 -040066import com.savoirfairelinux.sflphone.fragments.ContactListFragment;
alision7297bdb2013-05-21 11:56:55 -040067import com.savoirfairelinux.sflphone.fragments.DialingFragment;
alisiond9e29442013-04-17 16:10:18 -040068import com.savoirfairelinux.sflphone.fragments.HistoryFragment;
alision55c36cb2013-06-14 14:57:38 -040069import com.savoirfairelinux.sflphone.fragments.HomeFragment;
alision9f7a6ec2013-05-24 16:26:26 -040070import com.savoirfairelinux.sflphone.fragments.MenuFragment;
alision84813a12013-05-27 17:40:39 -040071import com.savoirfairelinux.sflphone.interfaces.CallInterface;
alision2ec64f92013-06-17 17:28:58 -040072import com.savoirfairelinux.sflphone.loaders.LoaderConstants;
alision84813a12013-05-27 17:40:39 -040073import com.savoirfairelinux.sflphone.model.CallContact;
alisiondf1dac92013-06-27 17:35:53 -040074import com.savoirfairelinux.sflphone.model.Conference;
alisionf76de3b2013-04-16 15:35:22 -040075import com.savoirfairelinux.sflphone.model.SipCall;
alisioncc7bb422013-06-06 15:31:39 -040076import com.savoirfairelinux.sflphone.receivers.CallReceiver;
alision84813a12013-05-27 17:40:39 -040077import com.savoirfairelinux.sflphone.service.CallManagerCallBack;
alisionf76de3b2013-04-16 15:35:22 -040078import com.savoirfairelinux.sflphone.service.ISipService;
79import com.savoirfairelinux.sflphone.service.SipService;
alision7297bdb2013-05-21 11:56:55 -040080import com.savoirfairelinux.sflphone.views.CustomSlidingDrawer;
alisionf76de3b2013-04-16 15:35:22 -040081
alision907bde72013-06-20 14:40:37 -040082public class SFLPhoneHomeActivity extends Activity implements DialingFragment.Callbacks, ContactListFragment.Callbacks, HomeFragment.Callbacks,
83 HistoryFragment.Callbacks, CallInterface, MenuFragment.Callbacks {
alisionfde875f2013-05-28 17:01:54 -040084
alision9f7a6ec2013-05-24 16:26:26 -040085 SectionsPagerAdapter mSectionsPagerAdapter = null;
alision84813a12013-05-27 17:40:39 -040086 static final String TAG = "SFLPhoneHomeActivity";
alisionf76de3b2013-04-16 15:35:22 -040087
alision2cb99562013-05-30 17:02:20 -040088 /**
89 * Fragments used
90 */
alision9f7a6ec2013-05-24 16:26:26 -040091 private ContactListFragment mContactsFragment = null;
alision58356b72013-06-03 17:13:36 -040092 // private DialingFragment mDialingFragment = null;
93 // private CallElementListFragment mCallElementList = null;
94 // private HistoryFragment mHistorySectionFragment = null;
alision907bde72013-06-20 14:40:37 -040095 private MenuFragment fMenu;
alisionf76de3b2013-04-16 15:35:22 -040096
alision9f7a6ec2013-05-24 16:26:26 -040097 private boolean mBound = false;
98 private ISipService service;
alisionf76de3b2013-04-16 15:35:22 -040099
alision9f7a6ec2013-05-24 16:26:26 -0400100 private CharSequence mDrawerTitle;
101 private CharSequence mTitle;
alisionf76de3b2013-04-16 15:35:22 -0400102
alision907bde72013-06-20 14:40:37 -0400103 // AccountSelectionAdapter mAdapter;
104 // private Spinner spinnerAccounts;
alision58356b72013-06-03 17:13:36 -0400105
alision2cb99562013-05-30 17:02:20 -0400106 public static final int REQUEST_CODE_PREFERENCES = 1;
alision9f7a6ec2013-05-24 16:26:26 -0400107 private static final int REQUEST_CODE_CALL = 2;
alisionf76de3b2013-04-16 15:35:22 -0400108
alision0b553832013-05-30 09:31:06 -0400109 RelativeLayout mSliderButton;
alision9f7a6ec2013-05-24 16:26:26 -0400110 CustomSlidingDrawer mDrawer;
alision9f7a6ec2013-05-24 16:26:26 -0400111 private DrawerLayout mDrawerLayout;
alision9f7a6ec2013-05-24 16:26:26 -0400112 private ActionBarDrawerToggle mDrawerToggle;
113 /**
114 * The {@link ViewPager} that will host the section contents.
115 */
116 ViewPager mViewPager;
alisionf76de3b2013-04-16 15:35:22 -0400117
alision58356b72013-06-03 17:13:36 -0400118 CallReceiver callReceiver;
alision84813a12013-05-27 17:40:39 -0400119
alision3c5c2a92013-05-30 09:50:47 -0400120 private TabHost mTabHost;
alisionf76de3b2013-04-16 15:35:22 -0400121
alision9f7a6ec2013-05-24 16:26:26 -0400122 // public SFLPhoneHome extends Activity implements ActionBar.TabListener, OnClickListener
123
124 /* called before activity is killed, e.g. rotation */
125 @Override
126 protected void onSaveInstanceState(Bundle bundle) {
127 super.onSaveInstanceState(bundle);
128 for (int i = 0; i < mSectionsPagerAdapter.getCount(); i++) {
129 try {
130 getFragmentManager().putFragment(bundle, mSectionsPagerAdapter.getClassName(i), mSectionsPagerAdapter.getItem(i));
131 } catch (IllegalStateException e) {
132 Log.e(TAG, "fragment=" + mSectionsPagerAdapter.getItem(i));
133 }
134 }
135
136 getFragmentManager().putFragment(bundle, "ContactsListFragment", mContactsFragment);
137 Log.w(TAG, "onSaveInstanceState()");
138 }
139
140 @Override
141 public void onCreate(Bundle savedInstanceState) {
142 super.onCreate(savedInstanceState);
143
alision58356b72013-06-03 17:13:36 -0400144 callReceiver = new CallReceiver(this);
alision907bde72013-06-20 14:40:37 -0400145
alision2cb99562013-05-30 17:02:20 -0400146 // String libraryPath = getApplicationInfo().dataDir + "/lib";
147 // Log.i(TAG, libraryPath);
alision9f7a6ec2013-05-24 16:26:26 -0400148
alision58356b72013-06-03 17:13:36 -0400149 setContentView(R.layout.activity_sflphone_home);
150
alision9f7a6ec2013-05-24 16:26:26 -0400151 // Bind to LocalService
152 if (!mBound) {
153 Log.i(TAG, "onStart: Binding service...");
154 Intent intent = new Intent(this, SipService.class);
155 bindService(intent, mConnection, Context.BIND_AUTO_CREATE);
156 }
157
alision9f7a6ec2013-05-24 16:26:26 -0400158 if (mContactsFragment == null) {
159 mContactsFragment = new ContactListFragment();
160 Log.w(TAG, "Recreated mContactListFragment=" + mContactsFragment);
161 getFragmentManager().beginTransaction().replace(R.id.contacts_frame, mContactsFragment).commit();
162 }
alision2ec64f92013-06-17 17:28:58 -0400163
alision9f7a6ec2013-05-24 16:26:26 -0400164 mDrawer = (CustomSlidingDrawer) findViewById(R.id.custom_sliding_drawer);
alision7297bdb2013-05-21 11:56:55 -0400165
alision84813a12013-05-27 17:40:39 -0400166 mContactsFragment.setHandleView((RelativeLayout) findViewById(R.id.slider_button));
alision7297bdb2013-05-21 11:56:55 -0400167
168 mDrawer.setmTrackHandle(findViewById(R.id.handle_title));
alisionf76de3b2013-04-16 15:35:22 -0400169
alision9f7a6ec2013-05-24 16:26:26 -0400170 // Set up the ViewPager with the sections adapter.
171 mViewPager = (ViewPager) findViewById(R.id.pager);
alision55c36cb2013-06-14 14:57:38 -0400172 mViewPager.setPageTransformer(true, new ZoomOutPageTransformer(0.7f));
alisionf76de3b2013-04-16 15:35:22 -0400173
alision9f7a6ec2013-05-24 16:26:26 -0400174 mTitle = mDrawerTitle = getTitle();
175 mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
alisionf76de3b2013-04-16 15:35:22 -0400176
alision0b553832013-05-30 09:31:06 -0400177 // set a custom shadow that overlays the main content when the drawer opens
alision9f7a6ec2013-05-24 16:26:26 -0400178 mDrawerLayout.setDrawerShadow(R.drawable.drawer_shadow, GravityCompat.START);
alisionf76de3b2013-04-16 15:35:22 -0400179
alision9f7a6ec2013-05-24 16:26:26 -0400180 getActionBar().setDisplayHomeAsUpEnabled(true);
181 getActionBar().setHomeButtonEnabled(true);
alisionf76de3b2013-04-16 15:35:22 -0400182
alision9f7a6ec2013-05-24 16:26:26 -0400183 mDrawerToggle = new ActionBarDrawerToggle(this, /* host Activity */
184 mDrawerLayout, /* DrawerLayout object */
185 R.drawable.ic_drawer, /* nav drawer image to replace 'Up' caret */
186 R.string.drawer_open, /* "open drawer" description for accessibility */
187 R.string.drawer_close /* "close drawer" description for accessibility */
188 ) {
alision0b553832013-05-30 09:31:06 -0400189 @Override
alision9f7a6ec2013-05-24 16:26:26 -0400190 public void onDrawerClosed(View view) {
191 getActionBar().setTitle(mTitle);
192 invalidateOptionsMenu(); // creates call to onPrepareOptionsMenu()
193 }
alisionf76de3b2013-04-16 15:35:22 -0400194
alision0b553832013-05-30 09:31:06 -0400195 @Override
alision9f7a6ec2013-05-24 16:26:26 -0400196 public void onDrawerOpened(View drawerView) {
197 getActionBar().setTitle(mDrawerTitle);
198 invalidateOptionsMenu(); // creates call to onPrepareOptionsMenu()
199 }
200 };
alisionf76de3b2013-04-16 15:35:22 -0400201
alision3c5c2a92013-05-30 09:50:47 -0400202 mTabHost = (TabHost) findViewById(android.R.id.tabhost);
alision9f7a6ec2013-05-24 16:26:26 -0400203 mDrawerLayout.setDrawerListener(mDrawerToggle);
alisionf76de3b2013-04-16 15:35:22 -0400204
alision9f7a6ec2013-05-24 16:26:26 -0400205 mViewPager.setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
206 @Override
207 public void onPageSelected(int position) {
alision3c5c2a92013-05-30 09:50:47 -0400208 mTabHost.setCurrentTab(position);
alision9f7a6ec2013-05-24 16:26:26 -0400209 }
210 });
alisionf76de3b2013-04-16 15:35:22 -0400211
alision9f7a6ec2013-05-24 16:26:26 -0400212 }
alisionf76de3b2013-04-16 15:35:22 -0400213
alision3c5c2a92013-05-30 09:50:47 -0400214 private void initialiseTabHost(Bundle args) {
215
alision3c5c2a92013-05-30 09:50:47 -0400216 mTabHost.setup();
217 TabInfo tabInfo = null;
alision2cb99562013-05-30 17:02:20 -0400218 SFLPhoneHomeActivity
219 .AddTab(this,
220 this.mTabHost,
221 this.mTabHost.newTabSpec("Tab1").setIndicator(mSectionsPagerAdapter.getPageTitle(0),
222 getResources().getDrawable(mSectionsPagerAdapter.getIconOf(0))), (tabInfo = new TabInfo("Tab1",
223 DialingFragment.class, args)));
alision3c5c2a92013-05-30 09:50:47 -0400224 this.mapTabInfo.put(tabInfo.tag, tabInfo);
alision2cb99562013-05-30 17:02:20 -0400225 SFLPhoneHomeActivity.AddTab(
226 this,
227 this.mTabHost,
228 this.mTabHost.newTabSpec("Tab2").setIndicator(mSectionsPagerAdapter.getPageTitle(1),
alision907bde72013-06-20 14:40:37 -0400229 getResources().getDrawable(mSectionsPagerAdapter.getIconOf(1))), (tabInfo = new TabInfo("Tab2", HomeFragment.class, args)));
alision3c5c2a92013-05-30 09:50:47 -0400230 this.mapTabInfo.put(tabInfo.tag, tabInfo);
alision2cb99562013-05-30 17:02:20 -0400231 SFLPhoneHomeActivity
232 .AddTab(this,
233 this.mTabHost,
234 this.mTabHost.newTabSpec("Tab3").setIndicator(mSectionsPagerAdapter.getPageTitle(2),
235 getResources().getDrawable(mSectionsPagerAdapter.getIconOf(2))), (tabInfo = new TabInfo("Tab3",
236 HistoryFragment.class, args)));
alision3c5c2a92013-05-30 09:50:47 -0400237 this.mapTabInfo.put(tabInfo.tag, tabInfo);
238
239 mTabHost.setOnTabChangedListener(new OnTabChangeListener() {
240
241 @Override
242 public void onTabChanged(String tabId) {
243 int pos = mTabHost.getCurrentTab();
244 mViewPager.setCurrentItem(pos);
245
246 }
247 });
248 }
alision465ceba2013-07-04 09:24:30 -0400249
alisionf57d8a62013-07-02 09:37:12 -0400250 @Override
251 protected void onPostCreate(Bundle savedInstanceState) {
252 super.onPostCreate(savedInstanceState);
253 // Sync the toggle state after onRestoreInstanceState has occurred.
254 mDrawerToggle.syncState();
255 }
alision465ceba2013-07-04 09:24:30 -0400256
alisionf57d8a62013-07-02 09:37:12 -0400257 @Override
258 public void onConfigurationChanged(Configuration newConfig) {
259 super.onConfigurationChanged(newConfig);
260 mDrawerToggle.onConfigurationChanged(newConfig);
261 }
alision3c5c2a92013-05-30 09:50:47 -0400262
alision9f7a6ec2013-05-24 16:26:26 -0400263 @Override
264 protected void onStart() {
265 Log.i(TAG, "onStart");
266 super.onStart();
267 }
alisionf76de3b2013-04-16 15:35:22 -0400268
alision9f7a6ec2013-05-24 16:26:26 -0400269 /* user gets back to the activity, e.g. through task manager */
270 @Override
271 protected void onRestart() {
272 super.onRestart();
273 }
alisionf76de3b2013-04-16 15:35:22 -0400274
alision9f7a6ec2013-05-24 16:26:26 -0400275 /* activity gets back to the foreground and user input */
276 @Override
277 protected void onResume() {
alision58356b72013-06-03 17:13:36 -0400278 super.onResume();
alision84813a12013-05-27 17:40:39 -0400279 IntentFilter intentFilter = new IntentFilter();
280 intentFilter.addAction(CallManagerCallBack.INCOMING_CALL);
281 intentFilter.addAction(CallManagerCallBack.INCOMING_TEXT);
282 intentFilter.addAction(CallManagerCallBack.CALL_STATE_CHANGED);
alision58356b72013-06-03 17:13:36 -0400283 registerReceiver(callReceiver, intentFilter);
alision907bde72013-06-20 14:40:37 -0400284
alision9f7a6ec2013-05-24 16:26:26 -0400285 }
alision907bde72013-06-20 14:40:37 -0400286
alisionbd45a822013-06-06 17:35:35 -0400287 private boolean isClosing = false;
288 private Timer t = new Timer();
alision907bde72013-06-20 14:40:37 -0400289
alisionbd45a822013-06-06 17:35:35 -0400290 @Override
291 public void onBackPressed() {
alision907bde72013-06-20 14:40:37 -0400292
293 if (getActionBar().getCustomView() != null) {
alision2ec64f92013-06-17 17:28:58 -0400294 getActionBar().setDisplayShowCustomEnabled(false);
295 getActionBar().setCustomView(null);
296 // Display all the contacts again
297 getLoaderManager().restartLoader(LoaderConstants.CONTACT_LOADER, null, mContactsFragment);
298 return;
299 }
alision907bde72013-06-20 14:40:37 -0400300
alisionbd45a822013-06-06 17:35:35 -0400301 if (mDrawer.isOpened()) {
alision55c36cb2013-06-14 14:57:38 -0400302 mDrawer.animateClose();
alisionbd45a822013-06-06 17:35:35 -0400303 return;
304 }
alision2ec64f92013-06-17 17:28:58 -0400305
alisionbd45a822013-06-06 17:35:35 -0400306 if (isClosing) {
307 super.onBackPressed();
308 t.cancel();
309 finish();
310 } else {
311
312 t.schedule(new TimerTask() {
313 @Override
314 public void run() {
315 isClosing = false;
316 }
317 }, 3000);
318 Toast.makeText(this, getResources().getString(R.string.close_msg), Toast.LENGTH_SHORT).show();
319 isClosing = true;
320 }
321 }
alisione2a38e12013-04-25 14:20:20 -0400322
alision9f7a6ec2013-05-24 16:26:26 -0400323 /* activity no more in foreground */
324 @Override
325 protected void onPause() {
326 super.onPause();
alision58356b72013-06-03 17:13:36 -0400327 unregisterReceiver(callReceiver);
alision55c36cb2013-06-14 14:57:38 -0400328 try {
329 service.createNotification();
330
331 } catch (RemoteException e) {
332 Log.e(TAG, e.toString());
333 }
alision2cb99562013-05-30 17:02:20 -0400334
alision9f7a6ec2013-05-24 16:26:26 -0400335 }
alisionf76de3b2013-04-16 15:35:22 -0400336
alision9f7a6ec2013-05-24 16:26:26 -0400337 /* activity is no longer visible */
338 @Override
339 protected void onStop() {
340 super.onStop();
alision2cb99562013-05-30 17:02:20 -0400341
alision9f7a6ec2013-05-24 16:26:26 -0400342 }
alisionf76de3b2013-04-16 15:35:22 -0400343
alision9f7a6ec2013-05-24 16:26:26 -0400344 /* activity finishes itself or is being killed by the system */
345 @Override
346 protected void onDestroy() {
347 /* stop the service, if no other bound user, no need to check if it is running */
348 if (mBound) {
349 Log.i(TAG, "onDestroy: Unbinding service...");
alision907bde72013-06-20 14:40:37 -0400350
alision9f7a6ec2013-05-24 16:26:26 -0400351 unbindService(mConnection);
352 mBound = false;
alision2cb99562013-05-30 17:02:20 -0400353
alision9f7a6ec2013-05-24 16:26:26 -0400354 }
alision371b77e2013-04-23 14:51:26 -0400355
alision9f7a6ec2013-05-24 16:26:26 -0400356 super.onDestroy();
357 }
alision73424b62013-04-26 11:49:18 -0400358
alision55c36cb2013-06-14 14:57:38 -0400359 public void launchCallActivity(SipCall infos) {
alision9f7a6ec2013-05-24 16:26:26 -0400360 Log.i(TAG, "Launch Call Activity");
361 Bundle bundle = new Bundle();
alisiondf1dac92013-06-27 17:35:53 -0400362 Conference tmp = new Conference("-1");
alision465ceba2013-07-04 09:24:30 -0400363
alisiondf1dac92013-06-27 17:35:53 -0400364 tmp.getParticipants().add(infos);
alision465ceba2013-07-04 09:24:30 -0400365
alisiondf1dac92013-06-27 17:35:53 -0400366 bundle.putParcelable("conference", tmp);
alision9f7a6ec2013-05-24 16:26:26 -0400367 Intent intent = new Intent().setClass(this, CallActivity.class);
alisiondf1dac92013-06-27 17:35:53 -0400368 intent.putExtra("resuming", false);
alision9f7a6ec2013-05-24 16:26:26 -0400369 intent.putExtras(bundle);
370 startActivityForResult(intent, REQUEST_CODE_CALL);
alision465ceba2013-07-04 09:24:30 -0400371 overridePendingTransition(R.anim.slide_down, R.anim.slide_up);
alision9f7a6ec2013-05-24 16:26:26 -0400372 }
alisionf76de3b2013-04-16 15:35:22 -0400373
alision9f7a6ec2013-05-24 16:26:26 -0400374 /** Defines callbacks for service binding, passed to bindService() */
375 private ServiceConnection mConnection = new ServiceConnection() {
alision371b77e2013-04-23 14:51:26 -0400376
alision9f7a6ec2013-05-24 16:26:26 -0400377 @Override
378 public void onServiceConnected(ComponentName className, IBinder binder) {
379 service = ISipService.Stub.asInterface(binder);
alisione2a38e12013-04-25 14:20:20 -0400380
alision2cb99562013-05-30 17:02:20 -0400381 try {
alision58356b72013-06-03 17:13:36 -0400382
alision907bde72013-06-20 14:40:37 -0400383 fMenu = new MenuFragment();
384 getFragmentManager().beginTransaction().replace(R.id.left_drawer, fMenu).commit();
alision58356b72013-06-03 17:13:36 -0400385 mSectionsPagerAdapter = new SectionsPagerAdapter(SFLPhoneHomeActivity.this, getFragmentManager());
386 initialiseTabHost(null);
alisiondf1dac92013-06-27 17:35:53 -0400387 mViewPager.setOffscreenPageLimit(2);
alision58356b72013-06-03 17:13:36 -0400388 mViewPager.setAdapter(mSectionsPagerAdapter);
389 mTabHost.setCurrentTab(1);
alision2cb99562013-05-30 17:02:20 -0400390 service.destroyNotification();
alision58356b72013-06-03 17:13:36 -0400391 // mAdapter = new AccountSelectionAdapter(SFLPhoneHomeActivity.this, service, new ArrayList<Account>());
392 // spinnerAccounts.setAdapter(mAdapter);
alision2cb99562013-05-30 17:02:20 -0400393 } catch (RemoteException e) {
394 Log.e(TAG, e.toString());
395 }
alision9f7a6ec2013-05-24 16:26:26 -0400396 mBound = true;
alision9f7a6ec2013-05-24 16:26:26 -0400397 Log.d(TAG, "Service connected service=" + service);
alision371b77e2013-04-23 14:51:26 -0400398
alision9f7a6ec2013-05-24 16:26:26 -0400399 }
alision5f899632013-04-22 17:26:56 -0400400
alision9f7a6ec2013-05-24 16:26:26 -0400401 @Override
402 public void onServiceDisconnected(ComponentName arg0) {
alision5f899632013-04-22 17:26:56 -0400403
alision9f7a6ec2013-05-24 16:26:26 -0400404 mBound = false;
405 Log.d(TAG, "Service disconnected service=" + service);
406 }
407 };
alision5f899632013-04-22 17:26:56 -0400408
alision9f7a6ec2013-05-24 16:26:26 -0400409 @Override
410 public boolean onOptionsItemSelected(MenuItem item) {
411 Log.i(TAG, "onOptionsItemSelected " + item.getItemId());
alision5f899632013-04-22 17:26:56 -0400412
alision9f7a6ec2013-05-24 16:26:26 -0400413 if (mDrawerToggle.onOptionsItemSelected(item)) {
414 return true;
415 }
alision5f899632013-04-22 17:26:56 -0400416
alision9f7a6ec2013-05-24 16:26:26 -0400417 return super.onOptionsItemSelected(item);
418 }
alisionf76de3b2013-04-16 15:35:22 -0400419
alision9f7a6ec2013-05-24 16:26:26 -0400420 @Override
421 public void onActivityResult(int requestCode, int resultCode, Intent data) {
422 super.onActivityResult(requestCode, resultCode, data);
alision84813a12013-05-27 17:40:39 -0400423
424 switch (requestCode) {
alision9f7a6ec2013-05-24 16:26:26 -0400425 case REQUEST_CODE_PREFERENCES:
alision907bde72013-06-20 14:40:37 -0400426 Log.w(TAG, "In Activity");
427 fMenu.updateAllAccounts();
alision9f7a6ec2013-05-24 16:26:26 -0400428 break;
429 case REQUEST_CODE_CALL:
alision2cb99562013-05-30 17:02:20 -0400430 Log.w(TAG, "Result out of CallActivity");
alision465ceba2013-07-04 09:24:30 -0400431 if (mSectionsPagerAdapter.getItem(2) != null)
432 getLoaderManager().restartLoader(LoaderConstants.HISTORY_LOADER, null, (HistoryFragment) mSectionsPagerAdapter.getItem(2));
alision9f7a6ec2013-05-24 16:26:26 -0400433 break;
434 }
alisionf76de3b2013-04-16 15:35:22 -0400435
alision9f7a6ec2013-05-24 16:26:26 -0400436 }
alisionf76de3b2013-04-16 15:35:22 -0400437
alision9f7a6ec2013-05-24 16:26:26 -0400438 @Override
alision9f7a6ec2013-05-24 16:26:26 -0400439 public ISipService getService() {
440 return service;
441 }
alisionf76de3b2013-04-16 15:35:22 -0400442
alision84813a12013-05-27 17:40:39 -0400443 /**
444 * Interface implemented to handle incoming events
445 */
alision9f7a6ec2013-05-24 16:26:26 -0400446 @Override
alision84813a12013-05-27 17:40:39 -0400447 public void incomingCall(Intent call) {
448 Toast.makeText(this, "New Call incoming", Toast.LENGTH_LONG).show();
alisionfde875f2013-05-28 17:01:54 -0400449 SipCall infos = call.getParcelableExtra("newcall");
alision84813a12013-05-27 17:40:39 -0400450
alision58356b72013-06-03 17:13:36 -0400451 // mCallElementList.addCall(infos);
alisionfde875f2013-05-28 17:01:54 -0400452
alision55c36cb2013-06-14 14:57:38 -0400453 launchCallActivity(infos);
alision84813a12013-05-27 17:40:39 -0400454
455 }
456
457 @Override
458 public void callStateChanged(Intent callState) {
459 Bundle b = callState.getBundleExtra("com.savoirfairelinux.sflphone.service.newstate");
460 String cID = b.getString("CallID");
461 String state = b.getString("State");
462 Log.i(TAG, "callStateChanged" + cID + " " + state);
alision58356b72013-06-03 17:13:36 -0400463 // mCallElementList.updateCall(cID, state);
alision84813a12013-05-27 17:40:39 -0400464
465 }
466
467 @Override
468 public void incomingText(Intent msg) {
469 Bundle b = msg.getBundleExtra("com.savoirfairelinux.sflphone.service.newtext");
470 b.getString("CallID");
471 String from = b.getString("From");
472 String mess = b.getString("Msg");
473 Toast.makeText(getApplicationContext(), "text from " + from + " : " + mess, Toast.LENGTH_LONG).show();
474 }
475
476 @Override
alision55c36cb2013-06-14 14:57:38 -0400477 public void onContactSelected(final CallContact c) {
alisionfde875f2013-05-28 17:01:54 -0400478
alision55c36cb2013-06-14 14:57:38 -0400479 Thread launcher = new Thread(new Runnable() {
alision907bde72013-06-20 14:40:37 -0400480
alision55c36cb2013-06-14 14:57:38 -0400481 final String[] CONTACTS_PHONES_PROJECTION = new String[] { Phone.NUMBER, Phone.TYPE };
482 final String[] CONTACTS_SIP_PROJECTION = new String[] { SipAddress.SIP_ADDRESS, SipAddress.TYPE };
alision907bde72013-06-20 14:40:37 -0400483
alision55c36cb2013-06-14 14:57:38 -0400484 @Override
485 public void run() {
486 SipCall.SipCallBuilder callBuilder = SipCall.SipCallBuilder.getInstance();
487 try {
alision907bde72013-06-20 14:40:37 -0400488 callBuilder.startCallCreation().setAccountID(service.getAccountList().get(1).toString())
489 .setCallType(SipCall.state.CALL_TYPE_OUTGOING);
490 Cursor cPhones = getContentResolver().query(Phone.CONTENT_URI, CONTACTS_PHONES_PROJECTION, Phone.CONTACT_ID + " =" + c.getId(),
491 null, null);
alision55c36cb2013-06-14 14:57:38 -0400492
alision907bde72013-06-20 14:40:37 -0400493 while (cPhones.moveToNext()) {
494 c.addPhoneNumber(cPhones.getString(cPhones.getColumnIndex(Phone.NUMBER)), cPhones.getInt(cPhones.getColumnIndex(Phone.TYPE)));
495 }
496 cPhones.close();
497
498 Cursor cSip = getContentResolver().query(Phone.CONTENT_URI, CONTACTS_SIP_PROJECTION, Phone.CONTACT_ID + "=" + c.getId(), null,
499 null);
500
501 while (cSip.moveToNext()) {
502 c.addSipNumber(cSip.getString(cSip.getColumnIndex(SipAddress.SIP_ADDRESS)), cSip.getInt(cSip.getColumnIndex(SipAddress.TYPE)));
503 }
504 cSip.close();
505 callBuilder.setContact(c);
alision55c36cb2013-06-14 14:57:38 -0400506 launchCallActivity(callBuilder.build());
507 } catch (RemoteException e1) {
508 Log.e(TAG, e1.toString());
509 } catch (Exception e) {
510 Log.e(TAG, e.toString());
511 }
alision907bde72013-06-20 14:40:37 -0400512
alision55c36cb2013-06-14 14:57:38 -0400513 }
514 });
515 launcher.start();
516 mDrawer.close();
alision84813a12013-05-27 17:40:39 -0400517
518 }
519
520 @Override
alision907bde72013-06-20 14:40:37 -0400521 public void onCallDialed(String to) {
alision84813a12013-05-27 17:40:39 -0400522
alisiondf1dac92013-06-27 17:35:53 -0400523 if (fMenu.getSelectedAccount() == null) {
524 Toast.makeText(this, "No Account Selected", Toast.LENGTH_SHORT).show();
525 return;
526 }
527
alisionfde875f2013-05-28 17:01:54 -0400528 SipCall.SipCallBuilder callBuilder = SipCall.SipCallBuilder.getInstance();
alision907bde72013-06-20 14:40:37 -0400529 callBuilder.startCallCreation().setAccountID(fMenu.getSelectedAccount().getAccountID()).setCallType(SipCall.state.CALL_TYPE_OUTGOING);
530 callBuilder.setContact(CallContact.ContactBuilder.buildUnknownContact(to));
alision84813a12013-05-27 17:40:39 -0400531
alisionfde875f2013-05-28 17:01:54 -0400532 try {
alision55c36cb2013-06-14 14:57:38 -0400533 launchCallActivity(callBuilder.build());
alisionfde875f2013-05-28 17:01:54 -0400534 } catch (Exception e) {
535 Log.e(TAG, e.toString());
536 }
alision84813a12013-05-27 17:40:39 -0400537
alision9f7a6ec2013-05-24 16:26:26 -0400538 }
alision371b77e2013-04-23 14:51:26 -0400539
alisiond45da712013-05-30 09:18:49 -0400540 @Override
541 public void onContactDragged() {
542
543 mDrawer.close();
alision2cb99562013-05-30 17:02:20 -0400544 mTabHost.setCurrentTab(1);
alisiond45da712013-05-30 09:18:49 -0400545
546 }
547
548 private HashMap<String, TabInfo> mapTabInfo = new HashMap<String, SFLPhoneHomeActivity.TabInfo>();
549
550 /**
551 * A simple factory that returns dummy views to the Tabhost
552 *
553 * @author mwho
554 */
555 class TabFactory implements TabContentFactory {
556
557 private final Context mContext;
558
559 /**
560 * @param context
561 */
562 public TabFactory(Context context) {
563 mContext = context;
564 }
565
566 /**
567 * (non-Javadoc)
568 *
569 * @see android.widget.TabHost.TabContentFactory#createTabContent(java.lang.String)
570 */
571 public View createTabContent(String tag) {
572 View v = new View(mContext);
573 v.setMinimumWidth(0);
574 v.setMinimumHeight(0);
575 return v;
576 }
577
578 }
579
580 /**
581 *
582 * @author mwho Maintains extrinsic info of a tab's construct
583 */
584 private class TabInfo {
585 private String tag;
alisiond45da712013-05-30 09:18:49 -0400586
587 TabInfo(String tag, Class<?> clazz, Bundle args) {
588 this.tag = tag;
alisiond45da712013-05-30 09:18:49 -0400589 }
590
591 }
592
593 private static void AddTab(SFLPhoneHomeActivity activity, TabHost tabHost, TabHost.TabSpec tabSpec, TabInfo tabInfo) {
594 // Attach a Tab view factory to the spec
595 tabSpec.setContent(activity.new TabFactory(activity));
596 tabHost.addTab(tabSpec);
597 }
598
alision58356b72013-06-03 17:13:36 -0400599 @Override
600 public void openDrawer() {
alision55c36cb2013-06-14 14:57:38 -0400601 mDrawer.animateOpen();
alision58356b72013-06-03 17:13:36 -0400602 }
603
alision806e18e2013-06-21 15:30:17 -0400604 @Override
605 public void confCreated(Intent intent) {
606 // TODO Auto-generated method stub
alisiondf1dac92013-06-27 17:35:53 -0400607
alision806e18e2013-06-21 15:30:17 -0400608 }
609
610 @Override
611 public void confRemoved(Intent intent) {
612 // TODO Auto-generated method stub
alisiondf1dac92013-06-27 17:35:53 -0400613
alision806e18e2013-06-21 15:30:17 -0400614 }
615
616 @Override
617 public void confChanged(Intent intent) {
618 // TODO Auto-generated method stub
alisiondf1dac92013-06-27 17:35:53 -0400619
620 }
621
622 @Override
623 public void recordingChanged(Intent intent) {
624 // TODO Auto-generated method stub
625
626 }
627
628 @Override
629 public void resumeCallActivity() {
630 Intent intent = new Intent().setClass(this, CallActivity.class);
631 intent.putExtra("resuming", true);
632 startActivityForResult(intent, REQUEST_CODE_CALL);
alision806e18e2013-06-21 15:30:17 -0400633 }
634
alisionf76de3b2013-04-16 15:35:22 -0400635}