blob: b56dce6256366f5f2ece71ec633a4edd85d2e111 [file] [log] [blame]
alisionf76de3b2013-04-16 15:35:22 -04001/*
Alexandre Lisionc1024c02014-01-06 11:12:53 -05002 * Copyright (C) 2004-2014 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 Lisionb4e60612014-01-14 17:47:23 -050043import android.app.*;
44import android.support.v4.app.ActionBarDrawerToggle;
45import android.support.v4.app.FragmentActivity;
Alexandre Lision064e1e02013-10-01 16:18:42 -040046import org.sflphone.R;
Alexandre Lisionf02190d2013-12-12 17:26:12 -050047import org.sflphone.fragments.AboutFragment;
48import org.sflphone.fragments.AccountsManagementFragment;
49import org.sflphone.fragments.CallListFragment;
Alexandre Lision064e1e02013-10-01 16:18:42 -040050import org.sflphone.fragments.ContactListFragment;
51import org.sflphone.fragments.DialingFragment;
52import org.sflphone.fragments.HistoryFragment;
53import org.sflphone.fragments.HomeFragment;
54import org.sflphone.fragments.MenuFragment;
Alexandre Lision064e1e02013-10-01 16:18:42 -040055import org.sflphone.model.CallContact;
56import org.sflphone.model.Conference;
57import org.sflphone.model.SipCall;
Alexandre Lision064e1e02013-10-01 16:18:42 -040058import org.sflphone.service.ISipService;
59import org.sflphone.service.SipService;
Alexandre Lision450458a2013-11-22 11:33:12 -050060import org.sflphone.views.SlidingUpPanelLayout;
61import org.sflphone.views.SlidingUpPanelLayout.PanelSlideListener;
Alexandre Lision064e1e02013-10-01 16:18:42 -040062
alisionf76de3b2013-04-16 15:35:22 -040063import android.content.ComponentName;
64import android.content.Context;
Alexandre Lisionc51ccb12013-09-11 16:00:30 -040065import android.content.DialogInterface;
alisionf76de3b2013-04-16 15:35:22 -040066import android.content.Intent;
alisionf76de3b2013-04-16 15:35:22 -040067import android.content.ServiceConnection;
Alexandre Lision35577132013-12-06 15:21:15 -050068import android.content.res.AssetManager;
alisionf57d8a62013-07-02 09:37:12 -040069import android.content.res.Configuration;
alision55c36cb2013-06-14 14:57:38 -040070import android.database.Cursor;
Alexandre Lisionc59685a2013-10-04 16:36:36 -040071import android.net.Uri;
alisionf76de3b2013-04-16 15:35:22 -040072import android.os.Bundle;
73import android.os.IBinder;
alision5f899632013-04-22 17:26:56 -040074import android.os.RemoteException;
Alexandre Lision35577132013-12-06 15:21:15 -050075import android.preference.PreferenceManager;
Alexandre Lisionc59685a2013-10-04 16:36:36 -040076import android.provider.ContactsContract;
alision55c36cb2013-06-14 14:57:38 -040077import android.provider.ContactsContract.CommonDataKinds.Phone;
78import android.provider.ContactsContract.CommonDataKinds.SipAddress;
alision9f7a6ec2013-05-24 16:26:26 -040079import android.support.v4.view.GravityCompat;
alision9f7a6ec2013-05-24 16:26:26 -040080import android.support.v4.widget.DrawerLayout;
alisionf76de3b2013-04-16 15:35:22 -040081import android.util.Log;
Alexandre Lision3ccccf02013-10-07 14:10:46 -040082import android.view.Gravity;
alision9f7a6ec2013-05-24 16:26:26 -040083import android.view.MenuItem;
84import android.view.View;
alision7297bdb2013-05-21 11:56:55 -040085import android.widget.RelativeLayout;
alision04a00182013-05-10 17:05:29 -040086import android.widget.Toast;
alisionf76de3b2013-04-16 15:35:22 -040087
Alexandre Lisiona8b78722013-12-13 10:18:33 -050088public class HomeActivity extends FragmentActivity implements DialingFragment.Callbacks, AccountsManagementFragment.Callbacks,
Alexandre Lisiona9ee4eb2014-01-15 16:20:35 -050089 ContactListFragment.Callbacks, CallListFragment.Callbacks, HistoryFragment.Callbacks, MenuFragment.Callbacks {
alisionfde875f2013-05-28 17:01:54 -040090
Alexandre Lisionf02190d2013-12-12 17:26:12 -050091 static final String TAG = HomeActivity.class.getSimpleName();
alisionf76de3b2013-04-16 15:35:22 -040092
alision9f7a6ec2013-05-24 16:26:26 -040093 private ContactListFragment mContactsFragment = null;
alision907bde72013-06-20 14:40:37 -040094 private MenuFragment fMenu;
alisionf76de3b2013-04-16 15:35:22 -040095
alision9f7a6ec2013-05-24 16:26:26 -040096 private boolean mBound = false;
97 private ISipService service;
alisionf76de3b2013-04-16 15:35:22 -040098
alision2cb99562013-05-30 17:02:20 -040099 public static final int REQUEST_CODE_PREFERENCES = 1;
Alexandre Lisionfed2a642014-01-10 12:05:47 -0500100 private static final int REQUEST_CODE_CALL = 3;
alisionf76de3b2013-04-16 15:35:22 -0400101
Alexandre Lision450458a2013-11-22 11:33:12 -0500102 SlidingUpPanelLayout mContactDrawer;
Alexandre Lision68900052013-11-01 11:34:56 -0400103 private DrawerLayout mNavigationDrawer;
alision9f7a6ec2013-05-24 16:26:26 -0400104 private ActionBarDrawerToggle mDrawerToggle;
alisionf76de3b2013-04-16 15:35:22 -0400105
Alexandre Lision9d052a12013-11-06 15:13:34 -0500106 private boolean isClosing = false;
107 private Timer t = new Timer();
alision84813a12013-05-27 17:40:39 -0400108
Alexandre Lisionf02190d2013-12-12 17:26:12 -0500109 private Fragment fContent;
110
alision9f7a6ec2013-05-24 16:26:26 -0400111 /* called before activity is killed, e.g. rotation */
112 @Override
113 protected void onSaveInstanceState(Bundle bundle) {
114 super.onSaveInstanceState(bundle);
alision9f7a6ec2013-05-24 16:26:26 -0400115 getFragmentManager().putFragment(bundle, "ContactsListFragment", mContactsFragment);
116 Log.w(TAG, "onSaveInstanceState()");
117 }
118
119 @Override
120 public void onCreate(Bundle savedInstanceState) {
121 super.onCreate(savedInstanceState);
122
alision907bde72013-06-20 14:40:37 -0400123
Alexandre Lision269e6042013-12-11 16:15:13 -0500124 setContentView(R.layout.activity_home);
alision58356b72013-06-03 17:13:36 -0400125
alision9f7a6ec2013-05-24 16:26:26 -0400126 // Bind to LocalService
127 if (!mBound) {
128 Log.i(TAG, "onStart: Binding service...");
129 Intent intent = new Intent(this, SipService.class);
Alexandre Lision0f550ee2013-10-25 15:34:38 -0400130 startService(intent);
alision9f7a6ec2013-05-24 16:26:26 -0400131 bindService(intent, mConnection, Context.BIND_AUTO_CREATE);
132 }
Alexandre Lision8b9d8e82013-10-04 09:21:27 -0400133
134 if (savedInstanceState != null) {
Alexandre Lision23628c12013-09-24 11:17:05 -0400135 mContactsFragment = (ContactListFragment) getFragmentManager().getFragment(savedInstanceState, "ContactsListFragment");
136 }
alision9f7a6ec2013-05-24 16:26:26 -0400137 if (mContactsFragment == null) {
138 mContactsFragment = new ContactListFragment();
alision9f7a6ec2013-05-24 16:26:26 -0400139 getFragmentManager().beginTransaction().replace(R.id.contacts_frame, mContactsFragment).commit();
140 }
alision2ec64f92013-06-17 17:28:58 -0400141
Alexandre Lision450458a2013-11-22 11:33:12 -0500142 mContactDrawer = (SlidingUpPanelLayout) findViewById(R.id.contact_panel);
143 // mContactDrawer.setShadowDrawable(getResources().getDrawable(R.drawable.above_shadow));
144 mContactDrawer.setAnchorPoint(0.3f);
145 mContactDrawer.setDragView(findViewById(R.id.contacts_frame));
146 mContactDrawer.setEnableDragViewTouchEvents(true);
Alexandre Lision450458a2013-11-22 11:33:12 -0500147 mContactDrawer.setPanelSlideListener(new PanelSlideListener() {
Alexandre Lision8b9d8e82013-10-04 09:21:27 -0400148
149 @Override
Alexandre Lision450458a2013-11-22 11:33:12 -0500150 public void onPanelSlide(View panel, float slideOffset) {
151 if (slideOffset < 0.2) {
152 if (getActionBar().isShowing()) {
153 getActionBar().hide();
154 }
155 } else {
156 if (!getActionBar().isShowing()) {
157 getActionBar().show();
158 }
Alexandre Lision8b9d8e82013-10-04 09:21:27 -0400159 }
160 }
Alexandre Lision450458a2013-11-22 11:33:12 -0500161
162 @Override
163 public void onPanelExpanded(View panel) {
164
165 }
166
167 @Override
168 public void onPanelCollapsed(View panel) {
169
170 }
171
172 @Override
173 public void onPanelAnchored(View panel) {
174
175 }
Alexandre Lision8b9d8e82013-10-04 09:21:27 -0400176 });
Alexandre Lisiond588bff2013-10-08 12:43:01 -0400177
Alexandre Lision68900052013-11-01 11:34:56 -0400178 mNavigationDrawer = (DrawerLayout) findViewById(R.id.drawer_layout);
alisionf76de3b2013-04-16 15:35:22 -0400179
alision0b553832013-05-30 09:31:06 -0400180 // set a custom shadow that overlays the main content when the drawer opens
Alexandre Lision68900052013-11-01 11:34:56 -0400181 mNavigationDrawer.setDrawerShadow(R.drawable.drawer_shadow, GravityCompat.START);
alisionf76de3b2013-04-16 15:35:22 -0400182
alision9f7a6ec2013-05-24 16:26:26 -0400183 getActionBar().setDisplayHomeAsUpEnabled(true);
184 getActionBar().setHomeButtonEnabled(true);
alisionf76de3b2013-04-16 15:35:22 -0400185
alision9f7a6ec2013-05-24 16:26:26 -0400186 mDrawerToggle = new ActionBarDrawerToggle(this, /* host Activity */
Alexandre Lisionfed2a642014-01-10 12:05:47 -0500187 mNavigationDrawer, /* DrawerLayout object */
188 R.drawable.ic_drawer, /* nav drawer image to replace 'Up' caret */
189 R.string.drawer_open, /* "open drawer" description for accessibility */
190 R.string.drawer_close /* "close drawer" description for accessibility */
alision9f7a6ec2013-05-24 16:26:26 -0400191 ) {
alision0b553832013-05-30 09:31:06 -0400192 @Override
alision9f7a6ec2013-05-24 16:26:26 -0400193 public void onDrawerClosed(View view) {
alision9f7a6ec2013-05-24 16:26:26 -0400194 invalidateOptionsMenu(); // creates call to onPrepareOptionsMenu()
195 }
alisionf76de3b2013-04-16 15:35:22 -0400196
alision0b553832013-05-30 09:31:06 -0400197 @Override
alision9f7a6ec2013-05-24 16:26:26 -0400198 public void onDrawerOpened(View drawerView) {
Alexandre Lision4d20c812013-12-12 17:44:50 -0500199 // getActionBar().setTitle(mDrawerTitle);
alision9f7a6ec2013-05-24 16:26:26 -0400200 invalidateOptionsMenu(); // creates call to onPrepareOptionsMenu()
201 }
202 };
alisionf76de3b2013-04-16 15:35:22 -0400203
Alexandre Lision68900052013-11-01 11:34:56 -0400204 mNavigationDrawer.setDrawerListener(mDrawerToggle);
alisionf76de3b2013-04-16 15:35:22 -0400205
alision9f7a6ec2013-05-24 16:26:26 -0400206 }
alisionf76de3b2013-04-16 15:35:22 -0400207
alisionf57d8a62013-07-02 09:37:12 -0400208 @Override
209 protected void onPostCreate(Bundle savedInstanceState) {
210 super.onPostCreate(savedInstanceState);
211 // Sync the toggle state after onRestoreInstanceState has occurred.
212 mDrawerToggle.syncState();
Alexandre Lision450458a2013-11-22 11:33:12 -0500213 if (mContactDrawer.isExpanded()) {
Alexandre Lisiond16bad92013-10-09 17:16:20 -0400214 getActionBar().hide();
215 }
alisionf57d8a62013-07-02 09:37:12 -0400216 }
alision465ceba2013-07-04 09:24:30 -0400217
alisionf57d8a62013-07-02 09:37:12 -0400218 @Override
219 public void onConfigurationChanged(Configuration newConfig) {
220 super.onConfigurationChanged(newConfig);
221 mDrawerToggle.onConfigurationChanged(newConfig);
222 }
alision3c5c2a92013-05-30 09:50:47 -0400223
alision9f7a6ec2013-05-24 16:26:26 -0400224 @Override
225 protected void onStart() {
226 Log.i(TAG, "onStart");
Alexandre Lision35577132013-12-06 15:21:15 -0500227
228 if (!PreferenceManager.getDefaultSharedPreferences(getApplicationContext()).getBoolean("installed", false)) {
229 PreferenceManager.getDefaultSharedPreferences(getApplicationContext()).edit().putBoolean("installed", true).commit();
230
231 copyAssetFolder(getAssets(), "ringtones", getFilesDir().getAbsolutePath() + "/ringtones");
232 }
233
alision9f7a6ec2013-05-24 16:26:26 -0400234 super.onStart();
Alexandre Lisionc51ccb12013-09-11 16:00:30 -0400235
alision9f7a6ec2013-05-24 16:26:26 -0400236 }
alisionf76de3b2013-04-16 15:35:22 -0400237
Alexandre Lision35577132013-12-06 15:21:15 -0500238 private static boolean copyAssetFolder(AssetManager assetManager, String fromAssetPath, String toPath) {
239 try {
240 String[] files = assetManager.list(fromAssetPath);
241 new File(toPath).mkdirs();
242 Log.i(TAG, "Creating :" + toPath);
243 boolean res = true;
244 for (String file : files)
245 if (file.contains(".")) {
246 Log.i(TAG, "Copying file :" + fromAssetPath + "/" + file + " to " + toPath + "/" + file);
247 res &= copyAsset(assetManager, fromAssetPath + "/" + file, toPath + "/" + file);
248 } else {
249 Log.i(TAG, "Copying folder :" + fromAssetPath + "/" + file + " to " + toPath + "/" + file);
250 res &= copyAssetFolder(assetManager, fromAssetPath + "/" + file, toPath + "/" + file);
251 }
252 return res;
253 } catch (Exception e) {
254 e.printStackTrace();
255 return false;
256 }
257 }
258
259 private static boolean copyAsset(AssetManager assetManager, String fromAssetPath, String toPath) {
260 InputStream in = null;
261 OutputStream out = null;
262 try {
263 in = assetManager.open(fromAssetPath);
264 new File(toPath).createNewFile();
265 out = new FileOutputStream(toPath);
266 copyFile(in, out);
267 in.close();
268 in = null;
269 out.flush();
270 out.close();
271 out = null;
272 return true;
273 } catch (Exception e) {
274 e.printStackTrace();
275 return false;
276 }
277 }
278
279 private static void copyFile(InputStream in, OutputStream out) throws IOException {
280 byte[] buffer = new byte[1024];
281 int read;
282 while ((read = in.read(buffer)) != -1) {
283 out.write(buffer, 0, read);
284 }
285 }
286
alision9f7a6ec2013-05-24 16:26:26 -0400287 /* user gets back to the activity, e.g. through task manager */
288 @Override
289 protected void onRestart() {
290 super.onRestart();
291 }
alisionf76de3b2013-04-16 15:35:22 -0400292
alision9f7a6ec2013-05-24 16:26:26 -0400293 /* activity gets back to the foreground and user input */
294 @Override
295 protected void onResume() {
alision58356b72013-06-03 17:13:36 -0400296 super.onResume();
alision9f7a6ec2013-05-24 16:26:26 -0400297 }
alision907bde72013-06-20 14:40:37 -0400298
alisionbd45a822013-06-06 17:35:35 -0400299 @Override
300 public void onBackPressed() {
Alexandre Lisiond588bff2013-10-08 12:43:01 -0400301
Alexandre Lision68900052013-11-01 11:34:56 -0400302 if (mNavigationDrawer.isDrawerVisible(Gravity.LEFT)) {
303 mNavigationDrawer.closeDrawer(Gravity.LEFT);
Alexandre Lision68855472013-10-10 16:20:46 -0400304 return;
Alexandre Lision3ccccf02013-10-07 14:10:46 -0400305 }
alision907bde72013-06-20 14:40:37 -0400306
Alexandre Lision450458a2013-11-22 11:33:12 -0500307 if (mContactDrawer.isExpanded() || mContactDrawer.isAnchored()) {
308 mContactDrawer.collapsePane();
alisionbd45a822013-06-06 17:35:35 -0400309 return;
310 }
alision2ec64f92013-06-17 17:28:58 -0400311
Alexandre Lisionb4e60612014-01-14 17:47:23 -0500312 if (getFragmentManager().getBackStackEntryCount() > 1) {
Alexandre Lisiond5934b52014-01-07 17:17:37 -0500313 popCustomBackStack();
Alexandre Lisionfc9b4e72013-12-13 14:55:25 -0500314 fMenu.backToHome();
Alexandre Lisionf02190d2013-12-12 17:26:12 -0500315 return;
316 }
317
alisionbd45a822013-06-06 17:35:35 -0400318 if (isClosing) {
319 super.onBackPressed();
320 t.cancel();
321 finish();
322 } else {
alisionbd45a822013-06-06 17:35:35 -0400323 t.schedule(new TimerTask() {
324 @Override
325 public void run() {
326 isClosing = false;
327 }
328 }, 3000);
329 Toast.makeText(this, getResources().getString(R.string.close_msg), Toast.LENGTH_SHORT).show();
330 isClosing = true;
331 }
332 }
alisione2a38e12013-04-25 14:20:20 -0400333
Alexandre Lisiond5934b52014-01-07 17:17:37 -0500334 private void popCustomBackStack() {
Alexandre Lisionb4e60612014-01-14 17:47:23 -0500335 FragmentManager.BackStackEntry entry = getFragmentManager().getBackStackEntryAt(0);
336 fContent = getFragmentManager().findFragmentByTag(entry.getName());
337 FragmentManager fm = getFragmentManager();
Alexandre Lisionfed2a642014-01-10 12:05:47 -0500338 for (int i = 0; i < fm.getBackStackEntryCount() - 1; ++i) {
Alexandre Lisiond5934b52014-01-07 17:17:37 -0500339 fm.popBackStack();
340 }
341 }
342
alision9f7a6ec2013-05-24 16:26:26 -0400343 /* activity no more in foreground */
344 @Override
345 protected void onPause() {
346 super.onPause();
Alexandre Lisiona9ee4eb2014-01-15 16:20:35 -0500347
Alexandre Lisionb4a9e392013-10-01 14:39:43 -0400348 if (mBound) {
349 unbindService(mConnection);
350 mBound = false;
351 }
alision9f7a6ec2013-05-24 16:26:26 -0400352 }
alisionf76de3b2013-04-16 15:35:22 -0400353
alision9f7a6ec2013-05-24 16:26:26 -0400354 /* activity finishes itself or is being killed by the system */
355 @Override
356 protected void onDestroy() {
alision9f7a6ec2013-05-24 16:26:26 -0400357 super.onDestroy();
Alexandre Lisionb4a9e392013-10-01 14:39:43 -0400358 Log.i(TAG, "onDestroy: destroying service...");
359 Intent sipServiceIntent = new Intent(this, SipService.class);
360 stopService(sipServiceIntent);
alision9f7a6ec2013-05-24 16:26:26 -0400361 }
alision73424b62013-04-26 11:49:18 -0400362
alision55c36cb2013-06-14 14:57:38 -0400363 public void launchCallActivity(SipCall infos) {
Alexandre Lisionc59685a2013-10-04 16:36:36 -0400364
alision9f7a6ec2013-05-24 16:26:26 -0400365 Bundle bundle = new Bundle();
Alexandre Lision67c70b42014-01-16 13:57:15 -0500366 Conference tmp = new Conference(Conference.DEFAULT_ID);
alision465ceba2013-07-04 09:24:30 -0400367
alisiondf1dac92013-06-27 17:35:53 -0400368 tmp.getParticipants().add(infos);
alision465ceba2013-07-04 09:24:30 -0400369
alisiondf1dac92013-06-27 17:35:53 -0400370 bundle.putParcelable("conference", tmp);
alision9f7a6ec2013-05-24 16:26:26 -0400371 Intent intent = new Intent().setClass(this, CallActivity.class);
alisiondf1dac92013-06-27 17:35:53 -0400372 intent.putExtra("resuming", false);
alision9f7a6ec2013-05-24 16:26:26 -0400373 intent.putExtras(bundle);
374 startActivityForResult(intent, REQUEST_CODE_CALL);
Alexandre Lisiond588bff2013-10-08 12:43:01 -0400375
Alexandre Lision8b9d8e82013-10-04 09:21:27 -0400376 // overridePendingTransition(R.anim.slide_down, R.anim.slide_up);
alision9f7a6ec2013-05-24 16:26:26 -0400377 }
alisionf76de3b2013-04-16 15:35:22 -0400378
Alexandre Lisionfed2a642014-01-10 12:05:47 -0500379 /**
380 * Defines callbacks for service binding, passed to bindService()
381 */
alision9f7a6ec2013-05-24 16:26:26 -0400382 private ServiceConnection mConnection = new ServiceConnection() {
alision371b77e2013-04-23 14:51:26 -0400383
alision9f7a6ec2013-05-24 16:26:26 -0400384 @Override
385 public void onServiceConnected(ComponentName className, IBinder binder) {
386 service = ISipService.Stub.asInterface(binder);
alisione2a38e12013-04-25 14:20:20 -0400387
alision2cb99562013-05-30 17:02:20 -0400388 try {
alision58356b72013-06-03 17:13:36 -0400389
alision907bde72013-06-20 14:40:37 -0400390 fMenu = new MenuFragment();
Alexandre Lisionf02190d2013-12-12 17:26:12 -0500391 fContent = new HomeFragment();
Alexandre Lision67c70b42014-01-16 13:57:15 -0500392 getFragmentManager().beginTransaction().replace(R.id.left_drawer, fMenu).replace(R.id.main_frame, fContent, "Home").addToBackStack("Home").commit();
Alexandre Lisionc51ccb12013-09-11 16:00:30 -0400393
alision2cb99562013-05-30 17:02:20 -0400394 service.destroyNotification();
395 } catch (RemoteException e) {
396 Log.e(TAG, e.toString());
397 }
alision9f7a6ec2013-05-24 16:26:26 -0400398 mBound = true;
alision9f7a6ec2013-05-24 16:26:26 -0400399 Log.d(TAG, "Service connected service=" + service);
alision371b77e2013-04-23 14:51:26 -0400400
alision9f7a6ec2013-05-24 16:26:26 -0400401 }
alision5f899632013-04-22 17:26:56 -0400402
alision9f7a6ec2013-05-24 16:26:26 -0400403 @Override
404 public void onServiceDisconnected(ComponentName arg0) {
alision5f899632013-04-22 17:26:56 -0400405
alision9f7a6ec2013-05-24 16:26:26 -0400406 mBound = false;
407 Log.d(TAG, "Service disconnected service=" + service);
408 }
409 };
alision5f899632013-04-22 17:26:56 -0400410
alision9f7a6ec2013-05-24 16:26:26 -0400411 @Override
412 public boolean onOptionsItemSelected(MenuItem item) {
413 Log.i(TAG, "onOptionsItemSelected " + item.getItemId());
alision5f899632013-04-22 17:26:56 -0400414
alision9f7a6ec2013-05-24 16:26:26 -0400415 if (mDrawerToggle.onOptionsItemSelected(item)) {
416 return true;
417 }
alision5f899632013-04-22 17:26:56 -0400418
alision9f7a6ec2013-05-24 16:26:26 -0400419 return super.onOptionsItemSelected(item);
420 }
alisionf76de3b2013-04-16 15:35:22 -0400421
alision9f7a6ec2013-05-24 16:26:26 -0400422 @Override
423 public void onActivityResult(int requestCode, int resultCode, Intent data) {
424 super.onActivityResult(requestCode, resultCode, data);
alision84813a12013-05-27 17:40:39 -0400425
426 switch (requestCode) {
Alexandre Lisionfed2a642014-01-10 12:05:47 -0500427 case REQUEST_CODE_PREFERENCES:
428 case AccountsManagementFragment.ACCOUNT_EDIT_REQUEST:
429 if (fMenu != null)
430 fMenu.updateAllAccounts();
431 break;
432 case REQUEST_CODE_CALL:
433 if (resultCode == CallActivity.RESULT_FAILURE) {
434 Log.w(TAG, "Call Failed");
435 }
436 break;
alision9f7a6ec2013-05-24 16:26:26 -0400437 }
alisionf76de3b2013-04-16 15:35:22 -0400438
alision9f7a6ec2013-05-24 16:26:26 -0400439 }
alisionf76de3b2013-04-16 15:35:22 -0400440
alision9f7a6ec2013-05-24 16:26:26 -0400441 @Override
alision9f7a6ec2013-05-24 16:26:26 -0400442 public ISipService getService() {
443 return service;
444 }
alisionf76de3b2013-04-16 15:35:22 -0400445
Alexandre Lisione1c96db2013-10-04 14:34:21 -0400446 @Override
447 public void onTextContact(final CallContact c) {
448 // TODO
449 }
Alexandre Lisiond588bff2013-10-08 12:43:01 -0400450
Alexandre Lisionc59685a2013-10-04 16:36:36 -0400451 @Override
452 public void onEditContact(final CallContact c) {
453 Intent intent = new Intent(Intent.ACTION_VIEW);
454 Uri uri = Uri.withAppendedPath(ContactsContract.Contacts.CONTENT_URI, String.valueOf(c.getId()));
455 intent.setData(uri);
456 startActivity(intent);
457 }
alision84813a12013-05-27 17:40:39 -0400458
459 @Override
Alexandre Lisione1c96db2013-10-04 14:34:21 -0400460 public void onCallContact(final CallContact c) {
Alexandre Lision8b9d8e82013-10-04 09:21:27 -0400461
Alexandre Lisionf0b993d2013-09-12 13:25:10 -0400462 if (fMenu.getSelectedAccount() == null) {
Alexandre Lisionf0b993d2013-09-12 13:25:10 -0400463 createAccountDialog().show();
464 return;
465 }
alisionfde875f2013-05-28 17:01:54 -0400466
Alexandre Lisiond588bff2013-10-08 12:43:01 -0400467 if (!fMenu.getSelectedAccount().isRegistered()) {
468 createNotRegisteredDialog().show();
469 return;
470 }
471
Alexandre Lisiond16bad92013-10-09 17:16:20 -0400472 getActionBar().show();
alision55c36cb2013-06-14 14:57:38 -0400473 Thread launcher = new Thread(new Runnable() {
alision907bde72013-06-20 14:40:37 -0400474
Alexandre Lisionfed2a642014-01-10 12:05:47 -0500475 final String[] CONTACTS_PHONES_PROJECTION = new String[]{Phone.NUMBER, Phone.TYPE};
476 final String[] CONTACTS_SIP_PROJECTION = new String[]{SipAddress.SIP_ADDRESS, SipAddress.TYPE};
alision907bde72013-06-20 14:40:37 -0400477
alision55c36cb2013-06-14 14:57:38 -0400478 @Override
479 public void run() {
480 SipCall.SipCallBuilder callBuilder = SipCall.SipCallBuilder.getInstance();
481 try {
Alexandre Lision67c70b42014-01-16 13:57:15 -0500482 callBuilder.startCallCreation().setAccount(fMenu.getSelectedAccount()).setCallType(SipCall.direction.CALL_TYPE_OUTGOING);
alision907bde72013-06-20 14:40:37 -0400483 Cursor cPhones = getContentResolver().query(Phone.CONTENT_URI, CONTACTS_PHONES_PROJECTION, Phone.CONTACT_ID + " =" + c.getId(),
484 null, null);
alision55c36cb2013-06-14 14:57:38 -0400485
alision907bde72013-06-20 14:40:37 -0400486 while (cPhones.moveToNext()) {
487 c.addPhoneNumber(cPhones.getString(cPhones.getColumnIndex(Phone.NUMBER)), cPhones.getInt(cPhones.getColumnIndex(Phone.TYPE)));
488 }
489 cPhones.close();
490
491 Cursor cSip = getContentResolver().query(Phone.CONTENT_URI, CONTACTS_SIP_PROJECTION, Phone.CONTACT_ID + "=" + c.getId(), null,
492 null);
493
494 while (cSip.moveToNext()) {
495 c.addSipNumber(cSip.getString(cSip.getColumnIndex(SipAddress.SIP_ADDRESS)), cSip.getInt(cSip.getColumnIndex(SipAddress.TYPE)));
496 }
497 cSip.close();
498 callBuilder.setContact(c);
alision55c36cb2013-06-14 14:57:38 -0400499 launchCallActivity(callBuilder.build());
Alexandre Lision5ed2c972013-10-11 15:36:33 -0400500 } catch (InvalidObjectException e) {
501 e.printStackTrace();
alision55c36cb2013-06-14 14:57:38 -0400502 }
alision907bde72013-06-20 14:40:37 -0400503
alision55c36cb2013-06-14 14:57:38 -0400504 }
505 });
506 launcher.start();
Alexandre Lision450458a2013-11-22 11:33:12 -0500507 mContactDrawer.collapsePane();
alision84813a12013-05-27 17:40:39 -0400508
509 }
510
511 @Override
alision907bde72013-06-20 14:40:37 -0400512 public void onCallDialed(String to) {
alision84813a12013-05-27 17:40:39 -0400513
alisiondf1dac92013-06-27 17:35:53 -0400514 if (fMenu.getSelectedAccount() == null) {
Alexandre Lisionc51ccb12013-09-11 16:00:30 -0400515 createAccountDialog().show();
alisiondf1dac92013-06-27 17:35:53 -0400516 return;
517 }
518
Alexandre Lisiond588bff2013-10-08 12:43:01 -0400519 if (fMenu.getSelectedAccount().isRegistered()) {
520 SipCall.SipCallBuilder callBuilder = SipCall.SipCallBuilder.getInstance();
Alexandre Lision67c70b42014-01-16 13:57:15 -0500521 callBuilder.startCallCreation().setAccount(fMenu.getSelectedAccount()).setCallType(SipCall.direction.CALL_TYPE_OUTGOING);
Alexandre Lisiond588bff2013-10-08 12:43:01 -0400522 callBuilder.setContact(CallContact.ContactBuilder.buildUnknownContact(to));
alision84813a12013-05-27 17:40:39 -0400523
Alexandre Lisiond588bff2013-10-08 12:43:01 -0400524 try {
525 launchCallActivity(callBuilder.build());
526 } catch (Exception e) {
527 Log.e(TAG, e.toString());
528 }
529 } else {
Alexandre Lision5ed2c972013-10-11 15:36:33 -0400530 createNotRegisteredDialog().show();
alisionfde875f2013-05-28 17:01:54 -0400531 }
alision84813a12013-05-27 17:40:39 -0400532
alision9f7a6ec2013-05-24 16:26:26 -0400533 }
alision371b77e2013-04-23 14:51:26 -0400534
Alexandre Lisiond588bff2013-10-08 12:43:01 -0400535 private AlertDialog createNotRegisteredDialog() {
536 final Activity ownerActivity = this;
537 AlertDialog.Builder builder = new AlertDialog.Builder(ownerActivity);
538
539 builder.setMessage(getResources().getString(R.string.cannot_pass_sipcall))
540 .setTitle(getResources().getString(R.string.cannot_pass_sipcall_title))
541 .setPositiveButton(getResources().getString(android.R.string.ok), new DialogInterface.OnClickListener() {
542 public void onClick(DialogInterface dialog, int whichButton) {
Alexandre Lision5ed2c972013-10-11 15:36:33 -0400543
Alexandre Lisiond588bff2013-10-08 12:43:01 -0400544 }
545 });
546
547 AlertDialog alertDialog = builder.create();
548 alertDialog.setOwnerActivity(ownerActivity);
549
550 return alertDialog;
551 }
552
Alexandre Lisionc51ccb12013-09-11 16:00:30 -0400553 private AlertDialog createAccountDialog() {
554 final Activity ownerActivity = this;
555 AlertDialog.Builder builder = new AlertDialog.Builder(ownerActivity);
556
557 builder.setMessage(getResources().getString(R.string.create_new_account_dialog))
558 .setTitle(getResources().getString(R.string.create_new_account_dialog_title))
559 .setPositiveButton(getResources().getString(android.R.string.ok), new DialogInterface.OnClickListener() {
560 public void onClick(DialogInterface dialog, int whichButton) {
561 Intent in = new Intent();
Alexandre Lision40954dc2013-10-09 15:24:03 -0400562 in.setClass(ownerActivity, AccountWizard.class);
Alexandre Lision4ab53972013-11-04 16:59:18 -0500563 ownerActivity.startActivityForResult(in, HomeActivity.REQUEST_CODE_PREFERENCES);
Alexandre Lisionc51ccb12013-09-11 16:00:30 -0400564 }
565 }).setNegativeButton(getResources().getString(android.R.string.cancel), new DialogInterface.OnClickListener() {
Alexandre Lisionfed2a642014-01-10 12:05:47 -0500566 public void onClick(DialogInterface dialog, int whichButton) {
567 dialog.dismiss();
568 }
569 });
Alexandre Lisionc51ccb12013-09-11 16:00:30 -0400570
571 AlertDialog alertDialog = builder.create();
572 alertDialog.setOwnerActivity(ownerActivity);
573
574 return alertDialog;
575 }
576
alisiond45da712013-05-30 09:18:49 -0400577 @Override
578 public void onContactDragged() {
Alexandre Lision450458a2013-11-22 11:33:12 -0500579 mContactDrawer.collapsePane();
alisiond45da712013-05-30 09:18:49 -0400580 }
581
alision58356b72013-06-03 17:13:36 -0400582 @Override
Alexandre Lision0936ba62013-11-25 10:04:56 -0500583 public void toggleDrawer() {
Alexandre Lisionc985b362013-12-10 17:45:03 -0500584 if (mContactDrawer.isAnchored())
Alexandre Lision0936ba62013-11-25 10:04:56 -0500585 mContactDrawer.expandPane();
Alexandre Lisionc985b362013-12-10 17:45:03 -0500586 else if (!mContactDrawer.isExpanded())
587 mContactDrawer.expandPane(0.3f);
Alexandre Lisionc9b4e9e2013-11-27 10:27:28 -0500588 else
589 mContactDrawer.collapsePane();
alision58356b72013-06-03 17:13:36 -0400590 }
591
alision806e18e2013-06-21 15:30:17 -0400592 @Override
Alexandre Lisiona458ba22013-12-11 15:04:11 -0500593 public void toggleForSearchDrawer() {
594 if (mContactDrawer.isExpanded()) {
595 mContactDrawer.collapsePane();
596 } else
597 mContactDrawer.expandPane();
598 }
599
600 @Override
Alexandre Lision1a9e3b12013-09-16 11:06:07 -0400601 public void selectedCall(Conference c) {
alisiondf1dac92013-06-27 17:35:53 -0400602 Intent intent = new Intent().setClass(this, CallActivity.class);
603 intent.putExtra("resuming", true);
Alexandre Lision1a9e3b12013-09-16 11:06:07 -0400604 intent.putExtra("conference", c);
alisiondf1dac92013-06-27 17:35:53 -0400605 startActivityForResult(intent, REQUEST_CODE_CALL);
alision806e18e2013-06-21 15:30:17 -0400606 }
Alexandre Lision8b9d8e82013-10-04 09:21:27 -0400607
Alexandre Lision0936ba62013-11-25 10:04:56 -0500608 @Override
609 public void setDragView(RelativeLayout relativeLayout) {
610 mContactDrawer.setDragView(relativeLayout);
611 }
612
Alexandre Lision269e6042013-12-11 16:15:13 -0500613 @Override
614 public void onSectionSelected(int pos) {
Alexandre Lisionf02190d2013-12-12 17:26:12 -0500615
616 mNavigationDrawer.closeDrawers();
Alexandre Lisionfed2a642014-01-10 12:05:47 -0500617
618 switch (pos) {
619 case 0:
620
621 if (fContent instanceof HomeFragment)
622 break;
623
Alexandre Lisionb4e60612014-01-14 17:47:23 -0500624 if (getFragmentManager().getBackStackEntryCount() == 1)
Alexandre Lisionfed2a642014-01-10 12:05:47 -0500625 break;
626
627 popCustomBackStack();
628
629 break;
630 case 1:
631 if (fContent instanceof AccountsManagementFragment)
632 break;
633 fContent = new AccountsManagementFragment();
Alexandre Lisionb4e60612014-01-14 17:47:23 -0500634 getFragmentManager().beginTransaction().setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE).replace(R.id.main_frame, fContent, "Accounts").addToBackStack("Accounts").commit();
Alexandre Lisionfed2a642014-01-10 12:05:47 -0500635 break;
636 case 2:
637 if (fContent instanceof AboutFragment)
638 break;
639 fContent = new AboutFragment();
Alexandre Lisionb4e60612014-01-14 17:47:23 -0500640 getFragmentManager().beginTransaction().setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE).replace(R.id.main_frame, fContent, "About").addToBackStack("About").commit();
Alexandre Lisionfed2a642014-01-10 12:05:47 -0500641 break;
642 }
643
644
Alexandre Lision269e6042013-12-11 16:15:13 -0500645 }
646
alisionf76de3b2013-04-16 15:35:22 -0400647}