blob: f03f80b232954ee255f4775bd32ace1407b37540 [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 Lision35577132013-12-06 15:21:15 -050034import java.io.File;
35import java.io.FileOutputStream;
36import java.io.IOException;
37import java.io.InputStream;
Alexandre Lision5ed2c972013-10-11 15:36:33 -040038import java.io.InvalidObjectException;
Alexandre Lision35577132013-12-06 15:21:15 -050039import java.io.OutputStream;
alisionbd45a822013-06-06 17:35:35 -040040import java.util.Timer;
41import java.util.TimerTask;
alision84813a12013-05-27 17:40:39 -040042
Alexandre Lision064e1e02013-10-01 16:18:42 -040043import org.sflphone.R;
44import org.sflphone.adapters.SectionsPagerAdapter;
45import org.sflphone.fragments.ContactListFragment;
46import org.sflphone.fragments.DialingFragment;
47import org.sflphone.fragments.HistoryFragment;
48import org.sflphone.fragments.HomeFragment;
49import org.sflphone.fragments.MenuFragment;
50import org.sflphone.interfaces.CallInterface;
Alexandre Lision064e1e02013-10-01 16:18:42 -040051import org.sflphone.model.CallContact;
52import org.sflphone.model.Conference;
53import org.sflphone.model.SipCall;
54import org.sflphone.receivers.CallReceiver;
55import org.sflphone.service.CallManagerCallBack;
56import org.sflphone.service.ISipService;
57import org.sflphone.service.SipService;
Alexandre Lisione5b66022013-10-30 11:34:15 -040058import org.sflphone.views.PagerSlidingTabStrip;
Alexandre Lision450458a2013-11-22 11:33:12 -050059import org.sflphone.views.SlidingUpPanelLayout;
60import org.sflphone.views.SlidingUpPanelLayout.PanelSlideListener;
Alexandre Lision064e1e02013-10-01 16:18:42 -040061
alisionf76de3b2013-04-16 15:35:22 -040062import android.app.Activity;
Alexandre Lisionc51ccb12013-09-11 16:00:30 -040063import android.app.AlertDialog;
alisionf76de3b2013-04-16 15:35:22 -040064import android.content.ComponentName;
65import android.content.Context;
Alexandre Lisionc51ccb12013-09-11 16:00:30 -040066import android.content.DialogInterface;
alisionf76de3b2013-04-16 15:35:22 -040067import android.content.Intent;
alision84813a12013-05-27 17:40:39 -040068import android.content.IntentFilter;
alisionf76de3b2013-04-16 15:35:22 -040069import android.content.ServiceConnection;
Alexandre Lision35577132013-12-06 15:21:15 -050070import android.content.res.AssetManager;
alisionf57d8a62013-07-02 09:37:12 -040071import android.content.res.Configuration;
alision55c36cb2013-06-14 14:57:38 -040072import android.database.Cursor;
Alexandre Lisionc59685a2013-10-04 16:36:36 -040073import android.net.Uri;
alisionf76de3b2013-04-16 15:35:22 -040074import android.os.Bundle;
75import android.os.IBinder;
alision5f899632013-04-22 17:26:56 -040076import android.os.RemoteException;
Alexandre Lision35577132013-12-06 15:21:15 -050077import android.preference.PreferenceManager;
Alexandre Lisionc59685a2013-10-04 16:36:36 -040078import android.provider.ContactsContract;
alision55c36cb2013-06-14 14:57:38 -040079import android.provider.ContactsContract.CommonDataKinds.Phone;
80import android.provider.ContactsContract.CommonDataKinds.SipAddress;
alision9f7a6ec2013-05-24 16:26:26 -040081import android.support.v4.app.ActionBarDrawerToggle;
82import android.support.v4.view.GravityCompat;
alisionf76de3b2013-04-16 15:35:22 -040083import android.support.v4.view.ViewPager;
alision9f7a6ec2013-05-24 16:26:26 -040084import android.support.v4.widget.DrawerLayout;
alisionf76de3b2013-04-16 15:35:22 -040085import android.util.Log;
Alexandre Lision3ccccf02013-10-07 14:10:46 -040086import android.view.Gravity;
alision9f7a6ec2013-05-24 16:26:26 -040087import android.view.MenuItem;
88import android.view.View;
Alexandre Lisiona458ba22013-12-11 15:04:11 -050089import android.view.WindowManager;
alision7297bdb2013-05-21 11:56:55 -040090import android.widget.RelativeLayout;
alision04a00182013-05-10 17:05:29 -040091import android.widget.Toast;
alisionf76de3b2013-04-16 15:35:22 -040092
Alexandre Lision4ab53972013-11-04 16:59:18 -050093public class HomeActivity extends Activity implements DialingFragment.Callbacks, ContactListFragment.Callbacks, HomeFragment.Callbacks,
alision907bde72013-06-20 14:40:37 -040094 HistoryFragment.Callbacks, CallInterface, MenuFragment.Callbacks {
alisionfde875f2013-05-28 17:01:54 -040095
alision9f7a6ec2013-05-24 16:26:26 -040096 SectionsPagerAdapter mSectionsPagerAdapter = null;
Alexandre Lision9d052a12013-11-06 15:13:34 -050097 static final String TAG = "HomeActivity";
alisionf76de3b2013-04-16 15:35:22 -040098
alision9f7a6ec2013-05-24 16:26:26 -040099 private ContactListFragment mContactsFragment = null;
alision907bde72013-06-20 14:40:37 -0400100 private MenuFragment fMenu;
alisionf76de3b2013-04-16 15:35:22 -0400101
alision9f7a6ec2013-05-24 16:26:26 -0400102 private boolean mBound = false;
103 private ISipService service;
alisionf76de3b2013-04-16 15:35:22 -0400104
alision9f7a6ec2013-05-24 16:26:26 -0400105 private CharSequence mDrawerTitle;
106 private CharSequence mTitle;
alisionf76de3b2013-04-16 15:35:22 -0400107
alision2cb99562013-05-30 17:02:20 -0400108 public static final int REQUEST_CODE_PREFERENCES = 1;
alision9f7a6ec2013-05-24 16:26:26 -0400109 private static final int REQUEST_CODE_CALL = 2;
alisionf76de3b2013-04-16 15:35:22 -0400110
alision0b553832013-05-30 09:31:06 -0400111 RelativeLayout mSliderButton;
Alexandre Lision450458a2013-11-22 11:33:12 -0500112 SlidingUpPanelLayout mContactDrawer;
Alexandre Lision68900052013-11-01 11:34:56 -0400113 private DrawerLayout mNavigationDrawer;
alision9f7a6ec2013-05-24 16:26:26 -0400114 private ActionBarDrawerToggle mDrawerToggle;
115 /**
116 * The {@link ViewPager} that will host the section contents.
117 */
118 ViewPager mViewPager;
alisionf76de3b2013-04-16 15:35:22 -0400119
alision58356b72013-06-03 17:13:36 -0400120 CallReceiver callReceiver;
Alexandre Lision9d052a12013-11-06 15:13:34 -0500121 private boolean isClosing = false;
122 private Timer t = new Timer();
alision84813a12013-05-27 17:40:39 -0400123
alision9f7a6ec2013-05-24 16:26:26 -0400124 /* called before activity is killed, e.g. rotation */
125 @Override
126 protected void onSaveInstanceState(Bundle bundle) {
127 super.onSaveInstanceState(bundle);
alision9f7a6ec2013-05-24 16:26:26 -0400128 getFragmentManager().putFragment(bundle, "ContactsListFragment", mContactsFragment);
129 Log.w(TAG, "onSaveInstanceState()");
130 }
131
132 @Override
133 public void onCreate(Bundle savedInstanceState) {
134 super.onCreate(savedInstanceState);
135
alision58356b72013-06-03 17:13:36 -0400136 callReceiver = new CallReceiver(this);
alision907bde72013-06-20 14:40:37 -0400137
alision58356b72013-06-03 17:13:36 -0400138 setContentView(R.layout.activity_sflphone_home);
139
alision9f7a6ec2013-05-24 16:26:26 -0400140 // Bind to LocalService
141 if (!mBound) {
142 Log.i(TAG, "onStart: Binding service...");
143 Intent intent = new Intent(this, SipService.class);
Alexandre Lision0f550ee2013-10-25 15:34:38 -0400144 startService(intent);
alision9f7a6ec2013-05-24 16:26:26 -0400145 bindService(intent, mConnection, Context.BIND_AUTO_CREATE);
146 }
Alexandre Lision8b9d8e82013-10-04 09:21:27 -0400147
148 if (savedInstanceState != null) {
Alexandre Lision23628c12013-09-24 11:17:05 -0400149 mContactsFragment = (ContactListFragment) getFragmentManager().getFragment(savedInstanceState, "ContactsListFragment");
150 }
alision9f7a6ec2013-05-24 16:26:26 -0400151 if (mContactsFragment == null) {
152 mContactsFragment = new ContactListFragment();
alision9f7a6ec2013-05-24 16:26:26 -0400153 getFragmentManager().beginTransaction().replace(R.id.contacts_frame, mContactsFragment).commit();
154 }
alision2ec64f92013-06-17 17:28:58 -0400155
Alexandre Lision450458a2013-11-22 11:33:12 -0500156 mContactDrawer = (SlidingUpPanelLayout) findViewById(R.id.contact_panel);
157 // mContactDrawer.setShadowDrawable(getResources().getDrawable(R.drawable.above_shadow));
158 mContactDrawer.setAnchorPoint(0.3f);
159 mContactDrawer.setDragView(findViewById(R.id.contacts_frame));
160 mContactDrawer.setEnableDragViewTouchEvents(true);
Alexandre Lision450458a2013-11-22 11:33:12 -0500161 mContactDrawer.setPanelSlideListener(new PanelSlideListener() {
Alexandre Lision8b9d8e82013-10-04 09:21:27 -0400162
163 @Override
Alexandre Lision450458a2013-11-22 11:33:12 -0500164 public void onPanelSlide(View panel, float slideOffset) {
165 if (slideOffset < 0.2) {
166 if (getActionBar().isShowing()) {
167 getActionBar().hide();
168 }
169 } else {
170 if (!getActionBar().isShowing()) {
171 getActionBar().show();
172 }
Alexandre Lision8b9d8e82013-10-04 09:21:27 -0400173 }
174 }
Alexandre Lision450458a2013-11-22 11:33:12 -0500175
176 @Override
177 public void onPanelExpanded(View panel) {
178
179 }
180
181 @Override
182 public void onPanelCollapsed(View panel) {
183
184 }
185
186 @Override
187 public void onPanelAnchored(View panel) {
188
189 }
Alexandre Lision8b9d8e82013-10-04 09:21:27 -0400190 });
Alexandre Lisiond588bff2013-10-08 12:43:01 -0400191
alision9f7a6ec2013-05-24 16:26:26 -0400192 // Set up the ViewPager with the sections adapter.
193 mViewPager = (ViewPager) findViewById(R.id.pager);
alision55c36cb2013-06-14 14:57:38 -0400194 mViewPager.setPageTransformer(true, new ZoomOutPageTransformer(0.7f));
alisionf76de3b2013-04-16 15:35:22 -0400195
alision9f7a6ec2013-05-24 16:26:26 -0400196 mTitle = mDrawerTitle = getTitle();
Alexandre Lision68900052013-11-01 11:34:56 -0400197 mNavigationDrawer = (DrawerLayout) findViewById(R.id.drawer_layout);
alisionf76de3b2013-04-16 15:35:22 -0400198
alision0b553832013-05-30 09:31:06 -0400199 // set a custom shadow that overlays the main content when the drawer opens
Alexandre Lision68900052013-11-01 11:34:56 -0400200 mNavigationDrawer.setDrawerShadow(R.drawable.drawer_shadow, GravityCompat.START);
alisionf76de3b2013-04-16 15:35:22 -0400201
alision9f7a6ec2013-05-24 16:26:26 -0400202 getActionBar().setDisplayHomeAsUpEnabled(true);
203 getActionBar().setHomeButtonEnabled(true);
alisionf76de3b2013-04-16 15:35:22 -0400204
alision9f7a6ec2013-05-24 16:26:26 -0400205 mDrawerToggle = new ActionBarDrawerToggle(this, /* host Activity */
Alexandre Lision68900052013-11-01 11:34:56 -0400206 mNavigationDrawer, /* DrawerLayout object */
alision9f7a6ec2013-05-24 16:26:26 -0400207 R.drawable.ic_drawer, /* nav drawer image to replace 'Up' caret */
208 R.string.drawer_open, /* "open drawer" description for accessibility */
209 R.string.drawer_close /* "close drawer" description for accessibility */
210 ) {
alision0b553832013-05-30 09:31:06 -0400211 @Override
alision9f7a6ec2013-05-24 16:26:26 -0400212 public void onDrawerClosed(View view) {
213 getActionBar().setTitle(mTitle);
214 invalidateOptionsMenu(); // creates call to onPrepareOptionsMenu()
215 }
alisionf76de3b2013-04-16 15:35:22 -0400216
alision0b553832013-05-30 09:31:06 -0400217 @Override
alision9f7a6ec2013-05-24 16:26:26 -0400218 public void onDrawerOpened(View drawerView) {
219 getActionBar().setTitle(mDrawerTitle);
220 invalidateOptionsMenu(); // creates call to onPrepareOptionsMenu()
221 }
222 };
alisionf76de3b2013-04-16 15:35:22 -0400223
Alexandre Lision68900052013-11-01 11:34:56 -0400224 mNavigationDrawer.setDrawerListener(mDrawerToggle);
alisionf76de3b2013-04-16 15:35:22 -0400225
alision9f7a6ec2013-05-24 16:26:26 -0400226 }
alisionf76de3b2013-04-16 15:35:22 -0400227
alisionf57d8a62013-07-02 09:37:12 -0400228 @Override
229 protected void onPostCreate(Bundle savedInstanceState) {
230 super.onPostCreate(savedInstanceState);
231 // Sync the toggle state after onRestoreInstanceState has occurred.
232 mDrawerToggle.syncState();
Alexandre Lision450458a2013-11-22 11:33:12 -0500233 if (mContactDrawer.isExpanded()) {
Alexandre Lisiond16bad92013-10-09 17:16:20 -0400234 getActionBar().hide();
235 }
alisionf57d8a62013-07-02 09:37:12 -0400236 }
alision465ceba2013-07-04 09:24:30 -0400237
alisionf57d8a62013-07-02 09:37:12 -0400238 @Override
239 public void onConfigurationChanged(Configuration newConfig) {
240 super.onConfigurationChanged(newConfig);
241 mDrawerToggle.onConfigurationChanged(newConfig);
242 }
alision3c5c2a92013-05-30 09:50:47 -0400243
alision9f7a6ec2013-05-24 16:26:26 -0400244 @Override
245 protected void onStart() {
246 Log.i(TAG, "onStart");
Alexandre Lision35577132013-12-06 15:21:15 -0500247
248 if (!PreferenceManager.getDefaultSharedPreferences(getApplicationContext()).getBoolean("installed", false)) {
249 PreferenceManager.getDefaultSharedPreferences(getApplicationContext()).edit().putBoolean("installed", true).commit();
250
251 copyAssetFolder(getAssets(), "ringtones", getFilesDir().getAbsolutePath() + "/ringtones");
252 }
253
alision9f7a6ec2013-05-24 16:26:26 -0400254 super.onStart();
Alexandre Lisionc51ccb12013-09-11 16:00:30 -0400255
alision9f7a6ec2013-05-24 16:26:26 -0400256 }
alisionf76de3b2013-04-16 15:35:22 -0400257
Alexandre Lision35577132013-12-06 15:21:15 -0500258 private static boolean copyAssetFolder(AssetManager assetManager, String fromAssetPath, String toPath) {
259 try {
260 String[] files = assetManager.list(fromAssetPath);
261 new File(toPath).mkdirs();
262 Log.i(TAG, "Creating :" + toPath);
263 boolean res = true;
264 for (String file : files)
265 if (file.contains(".")) {
266 Log.i(TAG, "Copying file :" + fromAssetPath + "/" + file + " to " + toPath + "/" + file);
267 res &= copyAsset(assetManager, fromAssetPath + "/" + file, toPath + "/" + file);
268 } else {
269 Log.i(TAG, "Copying folder :" + fromAssetPath + "/" + file + " to " + toPath + "/" + file);
270 res &= copyAssetFolder(assetManager, fromAssetPath + "/" + file, toPath + "/" + file);
271 }
272 return res;
273 } catch (Exception e) {
274 e.printStackTrace();
275 return false;
276 }
277 }
278
279 private static boolean copyAsset(AssetManager assetManager, String fromAssetPath, String toPath) {
280 InputStream in = null;
281 OutputStream out = null;
282 try {
283 in = assetManager.open(fromAssetPath);
284 new File(toPath).createNewFile();
285 out = new FileOutputStream(toPath);
286 copyFile(in, out);
287 in.close();
288 in = null;
289 out.flush();
290 out.close();
291 out = null;
292 return true;
293 } catch (Exception e) {
294 e.printStackTrace();
295 return false;
296 }
297 }
298
299 private static void copyFile(InputStream in, OutputStream out) throws IOException {
300 byte[] buffer = new byte[1024];
301 int read;
302 while ((read = in.read(buffer)) != -1) {
303 out.write(buffer, 0, read);
304 }
305 }
306
alision9f7a6ec2013-05-24 16:26:26 -0400307 /* user gets back to the activity, e.g. through task manager */
308 @Override
309 protected void onRestart() {
310 super.onRestart();
311 }
alisionf76de3b2013-04-16 15:35:22 -0400312
alision9f7a6ec2013-05-24 16:26:26 -0400313 /* activity gets back to the foreground and user input */
314 @Override
315 protected void onResume() {
alision58356b72013-06-03 17:13:36 -0400316 super.onResume();
alision84813a12013-05-27 17:40:39 -0400317 IntentFilter intentFilter = new IntentFilter();
318 intentFilter.addAction(CallManagerCallBack.INCOMING_CALL);
319 intentFilter.addAction(CallManagerCallBack.INCOMING_TEXT);
320 intentFilter.addAction(CallManagerCallBack.CALL_STATE_CHANGED);
alision58356b72013-06-03 17:13:36 -0400321 registerReceiver(callReceiver, intentFilter);
alision907bde72013-06-20 14:40:37 -0400322
alision9f7a6ec2013-05-24 16:26:26 -0400323 }
alision907bde72013-06-20 14:40:37 -0400324
alisionbd45a822013-06-06 17:35:35 -0400325 @Override
326 public void onBackPressed() {
Alexandre Lisiond588bff2013-10-08 12:43:01 -0400327
Alexandre Lision68900052013-11-01 11:34:56 -0400328 if (mNavigationDrawer.isDrawerVisible(Gravity.LEFT)) {
329 mNavigationDrawer.closeDrawer(Gravity.LEFT);
Alexandre Lision68855472013-10-10 16:20:46 -0400330 return;
Alexandre Lision3ccccf02013-10-07 14:10:46 -0400331 }
alision907bde72013-06-20 14:40:37 -0400332
Alexandre Lision450458a2013-11-22 11:33:12 -0500333 if (mContactDrawer.isExpanded() || mContactDrawer.isAnchored()) {
334 mContactDrawer.collapsePane();
alisionbd45a822013-06-06 17:35:35 -0400335 return;
336 }
alision2ec64f92013-06-17 17:28:58 -0400337
alisionbd45a822013-06-06 17:35:35 -0400338 if (isClosing) {
339 super.onBackPressed();
340 t.cancel();
341 finish();
342 } else {
343
344 t.schedule(new TimerTask() {
345 @Override
346 public void run() {
347 isClosing = false;
348 }
349 }, 3000);
350 Toast.makeText(this, getResources().getString(R.string.close_msg), Toast.LENGTH_SHORT).show();
351 isClosing = true;
352 }
353 }
alisione2a38e12013-04-25 14:20:20 -0400354
alision9f7a6ec2013-05-24 16:26:26 -0400355 /* activity no more in foreground */
356 @Override
357 protected void onPause() {
358 super.onPause();
alision58356b72013-06-03 17:13:36 -0400359 unregisterReceiver(callReceiver);
Alexandre Lisionb4a9e392013-10-01 14:39:43 -0400360 if (mBound) {
361 unbindService(mConnection);
362 mBound = false;
363 }
alision9f7a6ec2013-05-24 16:26:26 -0400364 }
alisionf76de3b2013-04-16 15:35:22 -0400365
alision9f7a6ec2013-05-24 16:26:26 -0400366 /* activity finishes itself or is being killed by the system */
367 @Override
368 protected void onDestroy() {
alision9f7a6ec2013-05-24 16:26:26 -0400369 super.onDestroy();
Alexandre Lisionb4a9e392013-10-01 14:39:43 -0400370 Log.i(TAG, "onDestroy: destroying service...");
371 Intent sipServiceIntent = new Intent(this, SipService.class);
372 stopService(sipServiceIntent);
alision9f7a6ec2013-05-24 16:26:26 -0400373 }
alision73424b62013-04-26 11:49:18 -0400374
alision55c36cb2013-06-14 14:57:38 -0400375 public void launchCallActivity(SipCall infos) {
Alexandre Lisionc59685a2013-10-04 16:36:36 -0400376
alision9f7a6ec2013-05-24 16:26:26 -0400377 Bundle bundle = new Bundle();
alisiondf1dac92013-06-27 17:35:53 -0400378 Conference tmp = new Conference("-1");
alision465ceba2013-07-04 09:24:30 -0400379
alisiondf1dac92013-06-27 17:35:53 -0400380 tmp.getParticipants().add(infos);
alision465ceba2013-07-04 09:24:30 -0400381
alisiondf1dac92013-06-27 17:35:53 -0400382 bundle.putParcelable("conference", tmp);
alision9f7a6ec2013-05-24 16:26:26 -0400383 Intent intent = new Intent().setClass(this, CallActivity.class);
alisiondf1dac92013-06-27 17:35:53 -0400384 intent.putExtra("resuming", false);
alision9f7a6ec2013-05-24 16:26:26 -0400385 intent.putExtras(bundle);
386 startActivityForResult(intent, REQUEST_CODE_CALL);
Alexandre Lisiond588bff2013-10-08 12:43:01 -0400387
Alexandre Lision8b9d8e82013-10-04 09:21:27 -0400388 // overridePendingTransition(R.anim.slide_down, R.anim.slide_up);
alision9f7a6ec2013-05-24 16:26:26 -0400389 }
alisionf76de3b2013-04-16 15:35:22 -0400390
alision9f7a6ec2013-05-24 16:26:26 -0400391 /** Defines callbacks for service binding, passed to bindService() */
392 private ServiceConnection mConnection = new ServiceConnection() {
alision371b77e2013-04-23 14:51:26 -0400393
alision9f7a6ec2013-05-24 16:26:26 -0400394 @Override
395 public void onServiceConnected(ComponentName className, IBinder binder) {
396 service = ISipService.Stub.asInterface(binder);
alisione2a38e12013-04-25 14:20:20 -0400397
alision2cb99562013-05-30 17:02:20 -0400398 try {
alision58356b72013-06-03 17:13:36 -0400399
alision907bde72013-06-20 14:40:37 -0400400 fMenu = new MenuFragment();
401 getFragmentManager().beginTransaction().replace(R.id.left_drawer, fMenu).commit();
Alexandre Lision4ab53972013-11-04 16:59:18 -0500402 mSectionsPagerAdapter = new SectionsPagerAdapter(HomeActivity.this, getFragmentManager());
Alexandre Lisionc51ccb12013-09-11 16:00:30 -0400403
alisiondf1dac92013-06-27 17:35:53 -0400404 mViewPager.setOffscreenPageLimit(2);
alision58356b72013-06-03 17:13:36 -0400405 mViewPager.setAdapter(mSectionsPagerAdapter);
Alexandre Lisionc51ccb12013-09-11 16:00:30 -0400406 mViewPager.setCurrentItem(1);
Alexandre Lisione5b66022013-10-30 11:34:15 -0400407
408 final PagerSlidingTabStrip strip = PagerSlidingTabStrip.class.cast(findViewById(R.id.pts_main));
409
Alexandre Lisione5b66022013-10-30 11:34:15 -0400410 strip.setViewPager(mViewPager);
alision2cb99562013-05-30 17:02:20 -0400411 service.destroyNotification();
412 } catch (RemoteException e) {
413 Log.e(TAG, e.toString());
414 }
alision9f7a6ec2013-05-24 16:26:26 -0400415 mBound = true;
alision9f7a6ec2013-05-24 16:26:26 -0400416 Log.d(TAG, "Service connected service=" + service);
alision371b77e2013-04-23 14:51:26 -0400417
alision9f7a6ec2013-05-24 16:26:26 -0400418 }
alision5f899632013-04-22 17:26:56 -0400419
alision9f7a6ec2013-05-24 16:26:26 -0400420 @Override
421 public void onServiceDisconnected(ComponentName arg0) {
alision5f899632013-04-22 17:26:56 -0400422
alision9f7a6ec2013-05-24 16:26:26 -0400423 mBound = false;
424 Log.d(TAG, "Service disconnected service=" + service);
425 }
426 };
alision5f899632013-04-22 17:26:56 -0400427
alision9f7a6ec2013-05-24 16:26:26 -0400428 @Override
429 public boolean onOptionsItemSelected(MenuItem item) {
430 Log.i(TAG, "onOptionsItemSelected " + item.getItemId());
alision5f899632013-04-22 17:26:56 -0400431
alision9f7a6ec2013-05-24 16:26:26 -0400432 if (mDrawerToggle.onOptionsItemSelected(item)) {
433 return true;
434 }
alision5f899632013-04-22 17:26:56 -0400435
alision9f7a6ec2013-05-24 16:26:26 -0400436 return super.onOptionsItemSelected(item);
437 }
alisionf76de3b2013-04-16 15:35:22 -0400438
alision9f7a6ec2013-05-24 16:26:26 -0400439 @Override
440 public void onActivityResult(int requestCode, int resultCode, Intent data) {
441 super.onActivityResult(requestCode, resultCode, data);
alision84813a12013-05-27 17:40:39 -0400442
443 switch (requestCode) {
alision9f7a6ec2013-05-24 16:26:26 -0400444 case REQUEST_CODE_PREFERENCES:
Alexandre Lisione5b66022013-10-30 11:34:15 -0400445 if (fMenu != null)
Alexandre Lision933ef0a2013-10-15 17:28:40 -0400446 fMenu.updateAllAccounts();
alision9f7a6ec2013-05-24 16:26:26 -0400447 break;
448 case REQUEST_CODE_CALL:
Alexandre Lisionc51ccb12013-09-11 16:00:30 -0400449 if (resultCode == CallActivity.RESULT_FAILURE) {
Alexandre Lision8b9d8e82013-10-04 09:21:27 -0400450 Log.w(TAG, "Call Failed");
Alexandre Lisionc51ccb12013-09-11 16:00:30 -0400451 }
452 // if (mSectionsPagerAdapter != null && mSectionsPagerAdapter.getItem(2) != null)
453 // getLoaderManager().restartLoader(LoaderConstants.HISTORY_LOADER, null, (HistoryFragment) mSectionsPagerAdapter.getItem(2));
alision9f7a6ec2013-05-24 16:26:26 -0400454 break;
455 }
alisionf76de3b2013-04-16 15:35:22 -0400456
alision9f7a6ec2013-05-24 16:26:26 -0400457 }
alisionf76de3b2013-04-16 15:35:22 -0400458
alision9f7a6ec2013-05-24 16:26:26 -0400459 @Override
alision9f7a6ec2013-05-24 16:26:26 -0400460 public ISipService getService() {
461 return service;
462 }
alisionf76de3b2013-04-16 15:35:22 -0400463
alision84813a12013-05-27 17:40:39 -0400464 /**
465 * Interface implemented to handle incoming events
466 */
alision9f7a6ec2013-05-24 16:26:26 -0400467 @Override
alision84813a12013-05-27 17:40:39 -0400468 public void incomingCall(Intent call) {
alisionfde875f2013-05-28 17:01:54 -0400469 SipCall infos = call.getParcelableExtra("newcall");
alision84813a12013-05-27 17:40:39 -0400470
alision55c36cb2013-06-14 14:57:38 -0400471 launchCallActivity(infos);
alision84813a12013-05-27 17:40:39 -0400472
473 }
474
475 @Override
476 public void callStateChanged(Intent callState) {
477 Bundle b = callState.getBundleExtra("com.savoirfairelinux.sflphone.service.newstate");
478 String cID = b.getString("CallID");
479 String state = b.getString("State");
480 Log.i(TAG, "callStateChanged" + cID + " " + state);
Alexandre Lision1a9e3b12013-09-16 11:06:07 -0400481 mSectionsPagerAdapter.updateHome();
alision84813a12013-05-27 17:40:39 -0400482
483 }
484
485 @Override
486 public void incomingText(Intent msg) {
487 Bundle b = msg.getBundleExtra("com.savoirfairelinux.sflphone.service.newtext");
488 b.getString("CallID");
489 String from = b.getString("From");
490 String mess = b.getString("Msg");
491 Toast.makeText(getApplicationContext(), "text from " + from + " : " + mess, Toast.LENGTH_LONG).show();
492 }
Alexandre Lisiond588bff2013-10-08 12:43:01 -0400493
Alexandre Lisione1c96db2013-10-04 14:34:21 -0400494 @Override
495 public void onTextContact(final CallContact c) {
496 // TODO
497 }
Alexandre Lisiond588bff2013-10-08 12:43:01 -0400498
Alexandre Lisionc59685a2013-10-04 16:36:36 -0400499 @Override
500 public void onEditContact(final CallContact c) {
501 Intent intent = new Intent(Intent.ACTION_VIEW);
502 Uri uri = Uri.withAppendedPath(ContactsContract.Contacts.CONTENT_URI, String.valueOf(c.getId()));
503 intent.setData(uri);
504 startActivity(intent);
505 }
alision84813a12013-05-27 17:40:39 -0400506
507 @Override
Alexandre Lisione1c96db2013-10-04 14:34:21 -0400508 public void onCallContact(final CallContact c) {
Alexandre Lision8b9d8e82013-10-04 09:21:27 -0400509
Alexandre Lisionf0b993d2013-09-12 13:25:10 -0400510 if (fMenu.getSelectedAccount() == null) {
Alexandre Lisionf0b993d2013-09-12 13:25:10 -0400511 createAccountDialog().show();
512 return;
513 }
alisionfde875f2013-05-28 17:01:54 -0400514
Alexandre Lisiond588bff2013-10-08 12:43:01 -0400515 if (!fMenu.getSelectedAccount().isRegistered()) {
516 createNotRegisteredDialog().show();
517 return;
518 }
519
Alexandre Lisiond16bad92013-10-09 17:16:20 -0400520 getActionBar().show();
alision55c36cb2013-06-14 14:57:38 -0400521 Thread launcher = new Thread(new Runnable() {
alision907bde72013-06-20 14:40:37 -0400522
alision55c36cb2013-06-14 14:57:38 -0400523 final String[] CONTACTS_PHONES_PROJECTION = new String[] { Phone.NUMBER, Phone.TYPE };
524 final String[] CONTACTS_SIP_PROJECTION = new String[] { SipAddress.SIP_ADDRESS, SipAddress.TYPE };
alision907bde72013-06-20 14:40:37 -0400525
alision55c36cb2013-06-14 14:57:38 -0400526 @Override
527 public void run() {
528 SipCall.SipCallBuilder callBuilder = SipCall.SipCallBuilder.getInstance();
529 try {
Alexandre Lisiond588bff2013-10-08 12:43:01 -0400530 callBuilder.startCallCreation().setAccount(fMenu.getSelectedAccount()).setCallType(SipCall.state.CALL_TYPE_OUTGOING);
alision907bde72013-06-20 14:40:37 -0400531 Cursor cPhones = getContentResolver().query(Phone.CONTENT_URI, CONTACTS_PHONES_PROJECTION, Phone.CONTACT_ID + " =" + c.getId(),
532 null, null);
alision55c36cb2013-06-14 14:57:38 -0400533
alision907bde72013-06-20 14:40:37 -0400534 while (cPhones.moveToNext()) {
535 c.addPhoneNumber(cPhones.getString(cPhones.getColumnIndex(Phone.NUMBER)), cPhones.getInt(cPhones.getColumnIndex(Phone.TYPE)));
536 }
537 cPhones.close();
538
539 Cursor cSip = getContentResolver().query(Phone.CONTENT_URI, CONTACTS_SIP_PROJECTION, Phone.CONTACT_ID + "=" + c.getId(), null,
540 null);
541
542 while (cSip.moveToNext()) {
543 c.addSipNumber(cSip.getString(cSip.getColumnIndex(SipAddress.SIP_ADDRESS)), cSip.getInt(cSip.getColumnIndex(SipAddress.TYPE)));
544 }
545 cSip.close();
546 callBuilder.setContact(c);
alision55c36cb2013-06-14 14:57:38 -0400547 launchCallActivity(callBuilder.build());
Alexandre Lision5ed2c972013-10-11 15:36:33 -0400548 } catch (InvalidObjectException e) {
549 e.printStackTrace();
alision55c36cb2013-06-14 14:57:38 -0400550 }
alision907bde72013-06-20 14:40:37 -0400551
alision55c36cb2013-06-14 14:57:38 -0400552 }
553 });
554 launcher.start();
Alexandre Lision450458a2013-11-22 11:33:12 -0500555 mContactDrawer.collapsePane();
alision84813a12013-05-27 17:40:39 -0400556
557 }
558
559 @Override
alision907bde72013-06-20 14:40:37 -0400560 public void onCallDialed(String to) {
alision84813a12013-05-27 17:40:39 -0400561
alisiondf1dac92013-06-27 17:35:53 -0400562 if (fMenu.getSelectedAccount() == null) {
Alexandre Lisionc51ccb12013-09-11 16:00:30 -0400563 createAccountDialog().show();
alisiondf1dac92013-06-27 17:35:53 -0400564 return;
565 }
566
Alexandre Lisiond588bff2013-10-08 12:43:01 -0400567 if (fMenu.getSelectedAccount().isRegistered()) {
568 SipCall.SipCallBuilder callBuilder = SipCall.SipCallBuilder.getInstance();
569 callBuilder.startCallCreation().setAccount(fMenu.getSelectedAccount()).setCallType(SipCall.state.CALL_TYPE_OUTGOING);
570 callBuilder.setContact(CallContact.ContactBuilder.buildUnknownContact(to));
alision84813a12013-05-27 17:40:39 -0400571
Alexandre Lisiond588bff2013-10-08 12:43:01 -0400572 try {
573 launchCallActivity(callBuilder.build());
574 } catch (Exception e) {
575 Log.e(TAG, e.toString());
576 }
577 } else {
Alexandre Lision5ed2c972013-10-11 15:36:33 -0400578 createNotRegisteredDialog().show();
alisionfde875f2013-05-28 17:01:54 -0400579 }
alision84813a12013-05-27 17:40:39 -0400580
alision9f7a6ec2013-05-24 16:26:26 -0400581 }
alision371b77e2013-04-23 14:51:26 -0400582
Alexandre Lisiond588bff2013-10-08 12:43:01 -0400583 private AlertDialog createNotRegisteredDialog() {
584 final Activity ownerActivity = this;
585 AlertDialog.Builder builder = new AlertDialog.Builder(ownerActivity);
586
587 builder.setMessage(getResources().getString(R.string.cannot_pass_sipcall))
588 .setTitle(getResources().getString(R.string.cannot_pass_sipcall_title))
589 .setPositiveButton(getResources().getString(android.R.string.ok), new DialogInterface.OnClickListener() {
590 public void onClick(DialogInterface dialog, int whichButton) {
Alexandre Lision5ed2c972013-10-11 15:36:33 -0400591
Alexandre Lisiond588bff2013-10-08 12:43:01 -0400592 }
593 });
594
595 AlertDialog alertDialog = builder.create();
596 alertDialog.setOwnerActivity(ownerActivity);
597
598 return alertDialog;
599 }
600
Alexandre Lisionc51ccb12013-09-11 16:00:30 -0400601 private AlertDialog createAccountDialog() {
602 final Activity ownerActivity = this;
603 AlertDialog.Builder builder = new AlertDialog.Builder(ownerActivity);
604
605 builder.setMessage(getResources().getString(R.string.create_new_account_dialog))
606 .setTitle(getResources().getString(R.string.create_new_account_dialog_title))
607 .setPositiveButton(getResources().getString(android.R.string.ok), new DialogInterface.OnClickListener() {
608 public void onClick(DialogInterface dialog, int whichButton) {
609 Intent in = new Intent();
Alexandre Lision40954dc2013-10-09 15:24:03 -0400610 in.setClass(ownerActivity, AccountWizard.class);
Alexandre Lision4ab53972013-11-04 16:59:18 -0500611 ownerActivity.startActivityForResult(in, HomeActivity.REQUEST_CODE_PREFERENCES);
Alexandre Lisionc51ccb12013-09-11 16:00:30 -0400612 }
613 }).setNegativeButton(getResources().getString(android.R.string.cancel), new DialogInterface.OnClickListener() {
614 public void onClick(DialogInterface dialog, int whichButton) {
615 dialog.dismiss();
616 }
617 });
618
619 AlertDialog alertDialog = builder.create();
620 alertDialog.setOwnerActivity(ownerActivity);
621
622 return alertDialog;
623 }
624
alisiond45da712013-05-30 09:18:49 -0400625 @Override
626 public void onContactDragged() {
Alexandre Lision450458a2013-11-22 11:33:12 -0500627 mContactDrawer.collapsePane();
alisiond45da712013-05-30 09:18:49 -0400628 }
629
alision58356b72013-06-03 17:13:36 -0400630 @Override
Alexandre Lision0936ba62013-11-25 10:04:56 -0500631 public void toggleDrawer() {
Alexandre Lisionc985b362013-12-10 17:45:03 -0500632 if (mContactDrawer.isAnchored())
Alexandre Lision0936ba62013-11-25 10:04:56 -0500633 mContactDrawer.expandPane();
Alexandre Lisionc985b362013-12-10 17:45:03 -0500634 else if (!mContactDrawer.isExpanded())
635 mContactDrawer.expandPane(0.3f);
Alexandre Lisionc9b4e9e2013-11-27 10:27:28 -0500636 else
637 mContactDrawer.collapsePane();
alision58356b72013-06-03 17:13:36 -0400638 }
639
alision806e18e2013-06-21 15:30:17 -0400640 @Override
Alexandre Lisiona458ba22013-12-11 15:04:11 -0500641 public void toggleForSearchDrawer() {
642 if (mContactDrawer.isExpanded()) {
643 mContactDrawer.collapsePane();
644 } else
645 mContactDrawer.expandPane();
646 }
647
648 @Override
alision806e18e2013-06-21 15:30:17 -0400649 public void confCreated(Intent intent) {
650 // TODO Auto-generated method stub
alisiondf1dac92013-06-27 17:35:53 -0400651
alision806e18e2013-06-21 15:30:17 -0400652 }
653
654 @Override
655 public void confRemoved(Intent intent) {
656 // TODO Auto-generated method stub
alisiondf1dac92013-06-27 17:35:53 -0400657
alision806e18e2013-06-21 15:30:17 -0400658 }
659
660 @Override
661 public void confChanged(Intent intent) {
662 // TODO Auto-generated method stub
alisiondf1dac92013-06-27 17:35:53 -0400663
664 }
665
666 @Override
667 public void recordingChanged(Intent intent) {
668 // TODO Auto-generated method stub
669
670 }
671
672 @Override
Alexandre Lision1a9e3b12013-09-16 11:06:07 -0400673 public void selectedCall(Conference c) {
alisiondf1dac92013-06-27 17:35:53 -0400674 Intent intent = new Intent().setClass(this, CallActivity.class);
675 intent.putExtra("resuming", true);
Alexandre Lision1a9e3b12013-09-16 11:06:07 -0400676 intent.putExtra("conference", c);
alisiondf1dac92013-06-27 17:35:53 -0400677 startActivityForResult(intent, REQUEST_CODE_CALL);
alision806e18e2013-06-21 15:30:17 -0400678 }
Alexandre Lision8b9d8e82013-10-04 09:21:27 -0400679
Alexandre Lision3f1aa682013-11-18 10:49:28 -0500680 public class ZoomOutPageTransformer implements ViewPager.PageTransformer {
Alexandre Lisionee37f7c2013-09-25 17:36:39 -0400681 private static final float MIN_ALPHA = .6f;
Alexandre Lision8b9d8e82013-10-04 09:21:27 -0400682
Alexandre Lisionee37f7c2013-09-25 17:36:39 -0400683 public ZoomOutPageTransformer(float scalingStart) {
684 super();
Alexandre Lisionee37f7c2013-09-25 17:36:39 -0400685 }
686
687 @Override
688 public void transformPage(View page, float position) {
Alexandre Lisionee37f7c2013-09-25 17:36:39 -0400689 final float normalizedposition = Math.abs(Math.abs(position) - 1);
Alexandre Lision8b9d8e82013-10-04 09:21:27 -0400690 page.setAlpha(MIN_ALPHA + (1.f - MIN_ALPHA) * normalizedposition);
Alexandre Lisionee37f7c2013-09-25 17:36:39 -0400691 }
692 }
alision806e18e2013-06-21 15:30:17 -0400693
Alexandre Lision0936ba62013-11-25 10:04:56 -0500694 @Override
695 public void setDragView(RelativeLayout relativeLayout) {
696 mContactDrawer.setDragView(relativeLayout);
697 }
698
alisionf76de3b2013-04-16 15:35:22 -0400699}