blob: 903873948cd47860b2819ce9ca5089126ed25a95 [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
alision2cb99562013-05-30 17:02:20 -0400103 public static final int REQUEST_CODE_PREFERENCES = 1;
alision9f7a6ec2013-05-24 16:26:26 -0400104 private static final int REQUEST_CODE_CALL = 2;
alisionf76de3b2013-04-16 15:35:22 -0400105
alision0b553832013-05-30 09:31:06 -0400106 RelativeLayout mSliderButton;
alision9f7a6ec2013-05-24 16:26:26 -0400107 CustomSlidingDrawer mDrawer;
alision9f7a6ec2013-05-24 16:26:26 -0400108 private DrawerLayout mDrawerLayout;
alision9f7a6ec2013-05-24 16:26:26 -0400109 private ActionBarDrawerToggle mDrawerToggle;
110 /**
111 * The {@link ViewPager} that will host the section contents.
112 */
113 ViewPager mViewPager;
alisionf76de3b2013-04-16 15:35:22 -0400114
alision58356b72013-06-03 17:13:36 -0400115 CallReceiver callReceiver;
alision84813a12013-05-27 17:40:39 -0400116
alision3c5c2a92013-05-30 09:50:47 -0400117 private TabHost mTabHost;
alisionf76de3b2013-04-16 15:35:22 -0400118
alision9f7a6ec2013-05-24 16:26:26 -0400119 // public SFLPhoneHome extends Activity implements ActionBar.TabListener, OnClickListener
120
121 /* called before activity is killed, e.g. rotation */
122 @Override
123 protected void onSaveInstanceState(Bundle bundle) {
124 super.onSaveInstanceState(bundle);
125 for (int i = 0; i < mSectionsPagerAdapter.getCount(); i++) {
126 try {
127 getFragmentManager().putFragment(bundle, mSectionsPagerAdapter.getClassName(i), mSectionsPagerAdapter.getItem(i));
128 } catch (IllegalStateException e) {
129 Log.e(TAG, "fragment=" + mSectionsPagerAdapter.getItem(i));
130 }
131 }
132
133 getFragmentManager().putFragment(bundle, "ContactsListFragment", mContactsFragment);
134 Log.w(TAG, "onSaveInstanceState()");
135 }
136
137 @Override
138 public void onCreate(Bundle savedInstanceState) {
139 super.onCreate(savedInstanceState);
140
alision58356b72013-06-03 17:13:36 -0400141 callReceiver = new CallReceiver(this);
alision907bde72013-06-20 14:40:37 -0400142
alision2cb99562013-05-30 17:02:20 -0400143 // String libraryPath = getApplicationInfo().dataDir + "/lib";
144 // Log.i(TAG, libraryPath);
alision9f7a6ec2013-05-24 16:26:26 -0400145
alision58356b72013-06-03 17:13:36 -0400146 setContentView(R.layout.activity_sflphone_home);
147
alision9f7a6ec2013-05-24 16:26:26 -0400148 // Bind to LocalService
149 if (!mBound) {
150 Log.i(TAG, "onStart: Binding service...");
151 Intent intent = new Intent(this, SipService.class);
152 bindService(intent, mConnection, Context.BIND_AUTO_CREATE);
153 }
154
alision9f7a6ec2013-05-24 16:26:26 -0400155 if (mContactsFragment == null) {
156 mContactsFragment = new ContactListFragment();
157 Log.w(TAG, "Recreated mContactListFragment=" + mContactsFragment);
158 getFragmentManager().beginTransaction().replace(R.id.contacts_frame, mContactsFragment).commit();
159 }
alision2ec64f92013-06-17 17:28:58 -0400160
alision9f7a6ec2013-05-24 16:26:26 -0400161 mDrawer = (CustomSlidingDrawer) findViewById(R.id.custom_sliding_drawer);
alision7297bdb2013-05-21 11:56:55 -0400162
alision84813a12013-05-27 17:40:39 -0400163 mContactsFragment.setHandleView((RelativeLayout) findViewById(R.id.slider_button));
alision7297bdb2013-05-21 11:56:55 -0400164
165 mDrawer.setmTrackHandle(findViewById(R.id.handle_title));
alisionf76de3b2013-04-16 15:35:22 -0400166
alision9f7a6ec2013-05-24 16:26:26 -0400167 // Set up the ViewPager with the sections adapter.
168 mViewPager = (ViewPager) findViewById(R.id.pager);
alision55c36cb2013-06-14 14:57:38 -0400169 mViewPager.setPageTransformer(true, new ZoomOutPageTransformer(0.7f));
alisionf76de3b2013-04-16 15:35:22 -0400170
alision9f7a6ec2013-05-24 16:26:26 -0400171 mTitle = mDrawerTitle = getTitle();
172 mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
alisionf76de3b2013-04-16 15:35:22 -0400173
alision0b553832013-05-30 09:31:06 -0400174 // set a custom shadow that overlays the main content when the drawer opens
alision9f7a6ec2013-05-24 16:26:26 -0400175 mDrawerLayout.setDrawerShadow(R.drawable.drawer_shadow, GravityCompat.START);
alisionf76de3b2013-04-16 15:35:22 -0400176
alision9f7a6ec2013-05-24 16:26:26 -0400177 getActionBar().setDisplayHomeAsUpEnabled(true);
178 getActionBar().setHomeButtonEnabled(true);
alisionf76de3b2013-04-16 15:35:22 -0400179
alision9f7a6ec2013-05-24 16:26:26 -0400180 mDrawerToggle = new ActionBarDrawerToggle(this, /* host Activity */
181 mDrawerLayout, /* DrawerLayout object */
182 R.drawable.ic_drawer, /* nav drawer image to replace 'Up' caret */
183 R.string.drawer_open, /* "open drawer" description for accessibility */
184 R.string.drawer_close /* "close drawer" description for accessibility */
185 ) {
alision0b553832013-05-30 09:31:06 -0400186 @Override
alision9f7a6ec2013-05-24 16:26:26 -0400187 public void onDrawerClosed(View view) {
188 getActionBar().setTitle(mTitle);
189 invalidateOptionsMenu(); // creates call to onPrepareOptionsMenu()
190 }
alisionf76de3b2013-04-16 15:35:22 -0400191
alision0b553832013-05-30 09:31:06 -0400192 @Override
alision9f7a6ec2013-05-24 16:26:26 -0400193 public void onDrawerOpened(View drawerView) {
194 getActionBar().setTitle(mDrawerTitle);
195 invalidateOptionsMenu(); // creates call to onPrepareOptionsMenu()
196 }
197 };
alisionf76de3b2013-04-16 15:35:22 -0400198
alision3c5c2a92013-05-30 09:50:47 -0400199 mTabHost = (TabHost) findViewById(android.R.id.tabhost);
alision9f7a6ec2013-05-24 16:26:26 -0400200 mDrawerLayout.setDrawerListener(mDrawerToggle);
alisionf76de3b2013-04-16 15:35:22 -0400201
alision9f7a6ec2013-05-24 16:26:26 -0400202 mViewPager.setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
203 @Override
204 public void onPageSelected(int position) {
alision3c5c2a92013-05-30 09:50:47 -0400205 mTabHost.setCurrentTab(position);
alision9f7a6ec2013-05-24 16:26:26 -0400206 }
207 });
alisionf76de3b2013-04-16 15:35:22 -0400208
alision9f7a6ec2013-05-24 16:26:26 -0400209 }
alisionf76de3b2013-04-16 15:35:22 -0400210
alision3c5c2a92013-05-30 09:50:47 -0400211 private void initialiseTabHost(Bundle args) {
212
alision3c5c2a92013-05-30 09:50:47 -0400213 mTabHost.setup();
214 TabInfo tabInfo = null;
alision2cb99562013-05-30 17:02:20 -0400215 SFLPhoneHomeActivity
216 .AddTab(this,
217 this.mTabHost,
218 this.mTabHost.newTabSpec("Tab1").setIndicator(mSectionsPagerAdapter.getPageTitle(0),
219 getResources().getDrawable(mSectionsPagerAdapter.getIconOf(0))), (tabInfo = new TabInfo("Tab1",
220 DialingFragment.class, args)));
alision3c5c2a92013-05-30 09:50:47 -0400221 this.mapTabInfo.put(tabInfo.tag, tabInfo);
alision2cb99562013-05-30 17:02:20 -0400222 SFLPhoneHomeActivity.AddTab(
223 this,
224 this.mTabHost,
225 this.mTabHost.newTabSpec("Tab2").setIndicator(mSectionsPagerAdapter.getPageTitle(1),
alision907bde72013-06-20 14:40:37 -0400226 getResources().getDrawable(mSectionsPagerAdapter.getIconOf(1))), (tabInfo = new TabInfo("Tab2", HomeFragment.class, args)));
alision3c5c2a92013-05-30 09:50:47 -0400227 this.mapTabInfo.put(tabInfo.tag, tabInfo);
alision2cb99562013-05-30 17:02:20 -0400228 SFLPhoneHomeActivity
229 .AddTab(this,
230 this.mTabHost,
231 this.mTabHost.newTabSpec("Tab3").setIndicator(mSectionsPagerAdapter.getPageTitle(2),
232 getResources().getDrawable(mSectionsPagerAdapter.getIconOf(2))), (tabInfo = new TabInfo("Tab3",
233 HistoryFragment.class, args)));
alision3c5c2a92013-05-30 09:50:47 -0400234 this.mapTabInfo.put(tabInfo.tag, tabInfo);
235
236 mTabHost.setOnTabChangedListener(new OnTabChangeListener() {
237
238 @Override
239 public void onTabChanged(String tabId) {
240 int pos = mTabHost.getCurrentTab();
241 mViewPager.setCurrentItem(pos);
242
243 }
244 });
245 }
alision465ceba2013-07-04 09:24:30 -0400246
alisionf57d8a62013-07-02 09:37:12 -0400247 @Override
248 protected void onPostCreate(Bundle savedInstanceState) {
249 super.onPostCreate(savedInstanceState);
250 // Sync the toggle state after onRestoreInstanceState has occurred.
251 mDrawerToggle.syncState();
252 }
alision465ceba2013-07-04 09:24:30 -0400253
alisionf57d8a62013-07-02 09:37:12 -0400254 @Override
255 public void onConfigurationChanged(Configuration newConfig) {
256 super.onConfigurationChanged(newConfig);
257 mDrawerToggle.onConfigurationChanged(newConfig);
258 }
alision3c5c2a92013-05-30 09:50:47 -0400259
alision9f7a6ec2013-05-24 16:26:26 -0400260 @Override
261 protected void onStart() {
262 Log.i(TAG, "onStart");
263 super.onStart();
264 }
alisionf76de3b2013-04-16 15:35:22 -0400265
alision9f7a6ec2013-05-24 16:26:26 -0400266 /* user gets back to the activity, e.g. through task manager */
267 @Override
268 protected void onRestart() {
269 super.onRestart();
270 }
alisionf76de3b2013-04-16 15:35:22 -0400271
alision9f7a6ec2013-05-24 16:26:26 -0400272 /* activity gets back to the foreground and user input */
273 @Override
274 protected void onResume() {
alision58356b72013-06-03 17:13:36 -0400275 super.onResume();
alision84813a12013-05-27 17:40:39 -0400276 IntentFilter intentFilter = new IntentFilter();
277 intentFilter.addAction(CallManagerCallBack.INCOMING_CALL);
278 intentFilter.addAction(CallManagerCallBack.INCOMING_TEXT);
279 intentFilter.addAction(CallManagerCallBack.CALL_STATE_CHANGED);
alision58356b72013-06-03 17:13:36 -0400280 registerReceiver(callReceiver, intentFilter);
alision907bde72013-06-20 14:40:37 -0400281
alision9f7a6ec2013-05-24 16:26:26 -0400282 }
alision907bde72013-06-20 14:40:37 -0400283
alisionbd45a822013-06-06 17:35:35 -0400284 private boolean isClosing = false;
285 private Timer t = new Timer();
alision907bde72013-06-20 14:40:37 -0400286
alisionbd45a822013-06-06 17:35:35 -0400287 @Override
288 public void onBackPressed() {
alision907bde72013-06-20 14:40:37 -0400289
290 if (getActionBar().getCustomView() != null) {
alision2ec64f92013-06-17 17:28:58 -0400291 getActionBar().setDisplayShowCustomEnabled(false);
292 getActionBar().setCustomView(null);
293 // Display all the contacts again
294 getLoaderManager().restartLoader(LoaderConstants.CONTACT_LOADER, null, mContactsFragment);
295 return;
296 }
alision907bde72013-06-20 14:40:37 -0400297
alisionbd45a822013-06-06 17:35:35 -0400298 if (mDrawer.isOpened()) {
alision55c36cb2013-06-14 14:57:38 -0400299 mDrawer.animateClose();
alisionbd45a822013-06-06 17:35:35 -0400300 return;
301 }
alision2ec64f92013-06-17 17:28:58 -0400302
alisionbd45a822013-06-06 17:35:35 -0400303 if (isClosing) {
304 super.onBackPressed();
305 t.cancel();
306 finish();
307 } else {
308
309 t.schedule(new TimerTask() {
310 @Override
311 public void run() {
312 isClosing = false;
313 }
314 }, 3000);
315 Toast.makeText(this, getResources().getString(R.string.close_msg), Toast.LENGTH_SHORT).show();
316 isClosing = true;
317 }
318 }
alisione2a38e12013-04-25 14:20:20 -0400319
alision9f7a6ec2013-05-24 16:26:26 -0400320 /* activity no more in foreground */
321 @Override
322 protected void onPause() {
323 super.onPause();
alision58356b72013-06-03 17:13:36 -0400324 unregisterReceiver(callReceiver);
alision55c36cb2013-06-14 14:57:38 -0400325 try {
326 service.createNotification();
327
328 } catch (RemoteException e) {
329 Log.e(TAG, e.toString());
330 }
alision2cb99562013-05-30 17:02:20 -0400331
alision9f7a6ec2013-05-24 16:26:26 -0400332 }
alisionf76de3b2013-04-16 15:35:22 -0400333
alision9f7a6ec2013-05-24 16:26:26 -0400334 /* activity is no longer visible */
335 @Override
336 protected void onStop() {
337 super.onStop();
alision2cb99562013-05-30 17:02:20 -0400338
alision9f7a6ec2013-05-24 16:26:26 -0400339 }
alisionf76de3b2013-04-16 15:35:22 -0400340
alision9f7a6ec2013-05-24 16:26:26 -0400341 /* activity finishes itself or is being killed by the system */
342 @Override
343 protected void onDestroy() {
344 /* stop the service, if no other bound user, no need to check if it is running */
345 if (mBound) {
346 Log.i(TAG, "onDestroy: Unbinding service...");
alision907bde72013-06-20 14:40:37 -0400347
alision9f7a6ec2013-05-24 16:26:26 -0400348 unbindService(mConnection);
349 mBound = false;
alision2cb99562013-05-30 17:02:20 -0400350
alision9f7a6ec2013-05-24 16:26:26 -0400351 }
alision371b77e2013-04-23 14:51:26 -0400352
alision9f7a6ec2013-05-24 16:26:26 -0400353 super.onDestroy();
354 }
alision73424b62013-04-26 11:49:18 -0400355
alision55c36cb2013-06-14 14:57:38 -0400356 public void launchCallActivity(SipCall infos) {
alision9f7a6ec2013-05-24 16:26:26 -0400357 Log.i(TAG, "Launch Call Activity");
358 Bundle bundle = new Bundle();
alisiondf1dac92013-06-27 17:35:53 -0400359 Conference tmp = new Conference("-1");
alision465ceba2013-07-04 09:24:30 -0400360
alisiondf1dac92013-06-27 17:35:53 -0400361 tmp.getParticipants().add(infos);
alision465ceba2013-07-04 09:24:30 -0400362
alisiondf1dac92013-06-27 17:35:53 -0400363 bundle.putParcelable("conference", tmp);
alision9f7a6ec2013-05-24 16:26:26 -0400364 Intent intent = new Intent().setClass(this, CallActivity.class);
alisiondf1dac92013-06-27 17:35:53 -0400365 intent.putExtra("resuming", false);
alision9f7a6ec2013-05-24 16:26:26 -0400366 intent.putExtras(bundle);
367 startActivityForResult(intent, REQUEST_CODE_CALL);
alision465ceba2013-07-04 09:24:30 -0400368 overridePendingTransition(R.anim.slide_down, R.anim.slide_up);
alision9f7a6ec2013-05-24 16:26:26 -0400369 }
alisionf76de3b2013-04-16 15:35:22 -0400370
alision9f7a6ec2013-05-24 16:26:26 -0400371 /** Defines callbacks for service binding, passed to bindService() */
372 private ServiceConnection mConnection = new ServiceConnection() {
alision371b77e2013-04-23 14:51:26 -0400373
alision9f7a6ec2013-05-24 16:26:26 -0400374 @Override
375 public void onServiceConnected(ComponentName className, IBinder binder) {
376 service = ISipService.Stub.asInterface(binder);
alisione2a38e12013-04-25 14:20:20 -0400377
alision2cb99562013-05-30 17:02:20 -0400378 try {
alision58356b72013-06-03 17:13:36 -0400379
alision907bde72013-06-20 14:40:37 -0400380 fMenu = new MenuFragment();
381 getFragmentManager().beginTransaction().replace(R.id.left_drawer, fMenu).commit();
alision58356b72013-06-03 17:13:36 -0400382 mSectionsPagerAdapter = new SectionsPagerAdapter(SFLPhoneHomeActivity.this, getFragmentManager());
383 initialiseTabHost(null);
alisiondf1dac92013-06-27 17:35:53 -0400384 mViewPager.setOffscreenPageLimit(2);
alision58356b72013-06-03 17:13:36 -0400385 mViewPager.setAdapter(mSectionsPagerAdapter);
386 mTabHost.setCurrentTab(1);
alision2cb99562013-05-30 17:02:20 -0400387 service.destroyNotification();
alision58356b72013-06-03 17:13:36 -0400388 // mAdapter = new AccountSelectionAdapter(SFLPhoneHomeActivity.this, service, new ArrayList<Account>());
389 // spinnerAccounts.setAdapter(mAdapter);
alision2cb99562013-05-30 17:02:20 -0400390 } catch (RemoteException e) {
391 Log.e(TAG, e.toString());
392 }
alision9f7a6ec2013-05-24 16:26:26 -0400393 mBound = true;
alision9f7a6ec2013-05-24 16:26:26 -0400394 Log.d(TAG, "Service connected service=" + service);
alision371b77e2013-04-23 14:51:26 -0400395
alision9f7a6ec2013-05-24 16:26:26 -0400396 }
alision5f899632013-04-22 17:26:56 -0400397
alision9f7a6ec2013-05-24 16:26:26 -0400398 @Override
399 public void onServiceDisconnected(ComponentName arg0) {
alision5f899632013-04-22 17:26:56 -0400400
alision9f7a6ec2013-05-24 16:26:26 -0400401 mBound = false;
402 Log.d(TAG, "Service disconnected service=" + service);
403 }
404 };
alision5f899632013-04-22 17:26:56 -0400405
alision9f7a6ec2013-05-24 16:26:26 -0400406 @Override
407 public boolean onOptionsItemSelected(MenuItem item) {
408 Log.i(TAG, "onOptionsItemSelected " + item.getItemId());
alision5f899632013-04-22 17:26:56 -0400409
alision9f7a6ec2013-05-24 16:26:26 -0400410 if (mDrawerToggle.onOptionsItemSelected(item)) {
411 return true;
412 }
alision5f899632013-04-22 17:26:56 -0400413
alision9f7a6ec2013-05-24 16:26:26 -0400414 return super.onOptionsItemSelected(item);
415 }
alisionf76de3b2013-04-16 15:35:22 -0400416
alision9f7a6ec2013-05-24 16:26:26 -0400417 @Override
418 public void onActivityResult(int requestCode, int resultCode, Intent data) {
419 super.onActivityResult(requestCode, resultCode, data);
alision84813a12013-05-27 17:40:39 -0400420
421 switch (requestCode) {
alision9f7a6ec2013-05-24 16:26:26 -0400422 case REQUEST_CODE_PREFERENCES:
alision907bde72013-06-20 14:40:37 -0400423 Log.w(TAG, "In Activity");
424 fMenu.updateAllAccounts();
alision9f7a6ec2013-05-24 16:26:26 -0400425 break;
426 case REQUEST_CODE_CALL:
alision2cb99562013-05-30 17:02:20 -0400427 Log.w(TAG, "Result out of CallActivity");
alision465ceba2013-07-04 09:24:30 -0400428 if (mSectionsPagerAdapter.getItem(2) != null)
429 getLoaderManager().restartLoader(LoaderConstants.HISTORY_LOADER, null, (HistoryFragment) mSectionsPagerAdapter.getItem(2));
alision9f7a6ec2013-05-24 16:26:26 -0400430 break;
431 }
alisionf76de3b2013-04-16 15:35:22 -0400432
alision9f7a6ec2013-05-24 16:26:26 -0400433 }
alisionf76de3b2013-04-16 15:35:22 -0400434
alision9f7a6ec2013-05-24 16:26:26 -0400435 @Override
alision9f7a6ec2013-05-24 16:26:26 -0400436 public ISipService getService() {
437 return service;
438 }
alisionf76de3b2013-04-16 15:35:22 -0400439
alision84813a12013-05-27 17:40:39 -0400440 /**
441 * Interface implemented to handle incoming events
442 */
alision9f7a6ec2013-05-24 16:26:26 -0400443 @Override
alision84813a12013-05-27 17:40:39 -0400444 public void incomingCall(Intent call) {
445 Toast.makeText(this, "New Call incoming", Toast.LENGTH_LONG).show();
alisionfde875f2013-05-28 17:01:54 -0400446 SipCall infos = call.getParcelableExtra("newcall");
alision84813a12013-05-27 17:40:39 -0400447
alision58356b72013-06-03 17:13:36 -0400448 // mCallElementList.addCall(infos);
alisionfde875f2013-05-28 17:01:54 -0400449
alision55c36cb2013-06-14 14:57:38 -0400450 launchCallActivity(infos);
alision84813a12013-05-27 17:40:39 -0400451
452 }
453
454 @Override
455 public void callStateChanged(Intent callState) {
456 Bundle b = callState.getBundleExtra("com.savoirfairelinux.sflphone.service.newstate");
457 String cID = b.getString("CallID");
458 String state = b.getString("State");
459 Log.i(TAG, "callStateChanged" + cID + " " + state);
alision58356b72013-06-03 17:13:36 -0400460 // mCallElementList.updateCall(cID, state);
alision84813a12013-05-27 17:40:39 -0400461
462 }
463
464 @Override
465 public void incomingText(Intent msg) {
466 Bundle b = msg.getBundleExtra("com.savoirfairelinux.sflphone.service.newtext");
467 b.getString("CallID");
468 String from = b.getString("From");
469 String mess = b.getString("Msg");
470 Toast.makeText(getApplicationContext(), "text from " + from + " : " + mess, Toast.LENGTH_LONG).show();
471 }
472
473 @Override
alision55c36cb2013-06-14 14:57:38 -0400474 public void onContactSelected(final CallContact c) {
alisionfde875f2013-05-28 17:01:54 -0400475
alision55c36cb2013-06-14 14:57:38 -0400476 Thread launcher = new Thread(new Runnable() {
alision907bde72013-06-20 14:40:37 -0400477
alision55c36cb2013-06-14 14:57:38 -0400478 final String[] CONTACTS_PHONES_PROJECTION = new String[] { Phone.NUMBER, Phone.TYPE };
479 final String[] CONTACTS_SIP_PROJECTION = new String[] { SipAddress.SIP_ADDRESS, SipAddress.TYPE };
alision907bde72013-06-20 14:40:37 -0400480
alision55c36cb2013-06-14 14:57:38 -0400481 @Override
482 public void run() {
483 SipCall.SipCallBuilder callBuilder = SipCall.SipCallBuilder.getInstance();
484 try {
alision907bde72013-06-20 14:40:37 -0400485 callBuilder.startCallCreation().setAccountID(service.getAccountList().get(1).toString())
486 .setCallType(SipCall.state.CALL_TYPE_OUTGOING);
487 Cursor cPhones = getContentResolver().query(Phone.CONTENT_URI, CONTACTS_PHONES_PROJECTION, Phone.CONTACT_ID + " =" + c.getId(),
488 null, null);
alision55c36cb2013-06-14 14:57:38 -0400489
alision907bde72013-06-20 14:40:37 -0400490 while (cPhones.moveToNext()) {
491 c.addPhoneNumber(cPhones.getString(cPhones.getColumnIndex(Phone.NUMBER)), cPhones.getInt(cPhones.getColumnIndex(Phone.TYPE)));
492 }
493 cPhones.close();
494
495 Cursor cSip = getContentResolver().query(Phone.CONTENT_URI, CONTACTS_SIP_PROJECTION, Phone.CONTACT_ID + "=" + c.getId(), null,
496 null);
497
498 while (cSip.moveToNext()) {
499 c.addSipNumber(cSip.getString(cSip.getColumnIndex(SipAddress.SIP_ADDRESS)), cSip.getInt(cSip.getColumnIndex(SipAddress.TYPE)));
500 }
501 cSip.close();
502 callBuilder.setContact(c);
alision55c36cb2013-06-14 14:57:38 -0400503 launchCallActivity(callBuilder.build());
504 } catch (RemoteException e1) {
505 Log.e(TAG, e1.toString());
506 } catch (Exception e) {
507 Log.e(TAG, e.toString());
508 }
alision907bde72013-06-20 14:40:37 -0400509
alision55c36cb2013-06-14 14:57:38 -0400510 }
511 });
512 launcher.start();
513 mDrawer.close();
alision84813a12013-05-27 17:40:39 -0400514
515 }
516
517 @Override
alision907bde72013-06-20 14:40:37 -0400518 public void onCallDialed(String to) {
alision84813a12013-05-27 17:40:39 -0400519
alisiondf1dac92013-06-27 17:35:53 -0400520 if (fMenu.getSelectedAccount() == null) {
521 Toast.makeText(this, "No Account Selected", Toast.LENGTH_SHORT).show();
522 return;
523 }
524
alisionfde875f2013-05-28 17:01:54 -0400525 SipCall.SipCallBuilder callBuilder = SipCall.SipCallBuilder.getInstance();
alision907bde72013-06-20 14:40:37 -0400526 callBuilder.startCallCreation().setAccountID(fMenu.getSelectedAccount().getAccountID()).setCallType(SipCall.state.CALL_TYPE_OUTGOING);
527 callBuilder.setContact(CallContact.ContactBuilder.buildUnknownContact(to));
alision84813a12013-05-27 17:40:39 -0400528
alisionfde875f2013-05-28 17:01:54 -0400529 try {
alision55c36cb2013-06-14 14:57:38 -0400530 launchCallActivity(callBuilder.build());
alisionfde875f2013-05-28 17:01:54 -0400531 } catch (Exception e) {
532 Log.e(TAG, e.toString());
533 }
alision84813a12013-05-27 17:40:39 -0400534
alision9f7a6ec2013-05-24 16:26:26 -0400535 }
alision371b77e2013-04-23 14:51:26 -0400536
alisiond45da712013-05-30 09:18:49 -0400537 @Override
538 public void onContactDragged() {
539
540 mDrawer.close();
alision2cb99562013-05-30 17:02:20 -0400541 mTabHost.setCurrentTab(1);
alisiond45da712013-05-30 09:18:49 -0400542
543 }
544
545 private HashMap<String, TabInfo> mapTabInfo = new HashMap<String, SFLPhoneHomeActivity.TabInfo>();
546
547 /**
548 * A simple factory that returns dummy views to the Tabhost
549 *
550 * @author mwho
551 */
552 class TabFactory implements TabContentFactory {
553
554 private final Context mContext;
555
556 /**
557 * @param context
558 */
559 public TabFactory(Context context) {
560 mContext = context;
561 }
562
563 /**
564 * (non-Javadoc)
565 *
566 * @see android.widget.TabHost.TabContentFactory#createTabContent(java.lang.String)
567 */
568 public View createTabContent(String tag) {
569 View v = new View(mContext);
570 v.setMinimumWidth(0);
571 v.setMinimumHeight(0);
572 return v;
573 }
574
575 }
576
577 /**
578 *
579 * @author mwho Maintains extrinsic info of a tab's construct
580 */
581 private class TabInfo {
582 private String tag;
alisiond45da712013-05-30 09:18:49 -0400583
584 TabInfo(String tag, Class<?> clazz, Bundle args) {
585 this.tag = tag;
alisiond45da712013-05-30 09:18:49 -0400586 }
587
588 }
589
590 private static void AddTab(SFLPhoneHomeActivity activity, TabHost tabHost, TabHost.TabSpec tabSpec, TabInfo tabInfo) {
591 // Attach a Tab view factory to the spec
592 tabSpec.setContent(activity.new TabFactory(activity));
593 tabHost.addTab(tabSpec);
594 }
595
alision58356b72013-06-03 17:13:36 -0400596 @Override
597 public void openDrawer() {
alision55c36cb2013-06-14 14:57:38 -0400598 mDrawer.animateOpen();
alision58356b72013-06-03 17:13:36 -0400599 }
600
alision806e18e2013-06-21 15:30:17 -0400601 @Override
602 public void confCreated(Intent intent) {
603 // TODO Auto-generated method stub
alisiondf1dac92013-06-27 17:35:53 -0400604
alision806e18e2013-06-21 15:30:17 -0400605 }
606
607 @Override
608 public void confRemoved(Intent intent) {
609 // TODO Auto-generated method stub
alisiondf1dac92013-06-27 17:35:53 -0400610
alision806e18e2013-06-21 15:30:17 -0400611 }
612
613 @Override
614 public void confChanged(Intent intent) {
615 // TODO Auto-generated method stub
alisiondf1dac92013-06-27 17:35:53 -0400616
617 }
618
619 @Override
620 public void recordingChanged(Intent intent) {
621 // TODO Auto-generated method stub
622
623 }
624
625 @Override
626 public void resumeCallActivity() {
627 Intent intent = new Intent().setClass(this, CallActivity.class);
628 intent.putExtra("resuming", true);
629 startActivityForResult(intent, REQUEST_CODE_CALL);
alision806e18e2013-06-21 15:30:17 -0400630 }
631
alisionf76de3b2013-04-16 15:35:22 -0400632}