blob: 09b77eb9ac224c6891eb435f9a16daa362d49e05 [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;
Alexandre Lisiona7ab2e32014-02-14 15:33:33 -050040import java.util.Random;
alisionbd45a822013-06-06 17:35:35 -040041import java.util.Timer;
42import java.util.TimerTask;
alision84813a12013-05-27 17:40:39 -040043
Alexandre Lisionbe54c342014-01-21 17:10:33 -050044import android.app.Activity;
45import android.app.AlertDialog;
46import android.support.v4.app.*;
Alexandre Lision064e1e02013-10-01 16:18:42 -040047import org.sflphone.R;
Alexandre Lisionf02190d2013-12-12 17:26:12 -050048import org.sflphone.fragments.AboutFragment;
49import org.sflphone.fragments.AccountsManagementFragment;
50import org.sflphone.fragments.CallListFragment;
Alexandre Lision064e1e02013-10-01 16:18:42 -040051import org.sflphone.fragments.ContactListFragment;
52import org.sflphone.fragments.DialingFragment;
53import org.sflphone.fragments.HistoryFragment;
54import org.sflphone.fragments.HomeFragment;
55import org.sflphone.fragments.MenuFragment;
Alexandre Lisione796a872014-01-17 17:08:13 -050056import org.sflphone.history.HistoryEntry;
57import org.sflphone.model.Account;
Alexandre Lision064e1e02013-10-01 16:18:42 -040058import org.sflphone.model.CallContact;
59import org.sflphone.model.Conference;
60import org.sflphone.model.SipCall;
Alexandre Lision064e1e02013-10-01 16:18:42 -040061import org.sflphone.service.ISipService;
62import org.sflphone.service.SipService;
Alexandre Lision450458a2013-11-22 11:33:12 -050063import org.sflphone.views.SlidingUpPanelLayout;
64import org.sflphone.views.SlidingUpPanelLayout.PanelSlideListener;
Alexandre Lision064e1e02013-10-01 16:18:42 -040065
alisionf76de3b2013-04-16 15:35:22 -040066import android.content.ComponentName;
67import android.content.Context;
Alexandre Lisionc51ccb12013-09-11 16:00:30 -040068import android.content.DialogInterface;
alisionf76de3b2013-04-16 15:35:22 -040069import android.content.Intent;
alisionf76de3b2013-04-16 15:35:22 -040070import android.content.ServiceConnection;
Alexandre Lision35577132013-12-06 15:21:15 -050071import android.content.res.AssetManager;
alisionf57d8a62013-07-02 09:37:12 -040072import android.content.res.Configuration;
alision55c36cb2013-06-14 14:57:38 -040073import android.database.Cursor;
Alexandre Lisionc59685a2013-10-04 16:36:36 -040074import android.net.Uri;
alisionf76de3b2013-04-16 15:35:22 -040075import android.os.Bundle;
76import android.os.IBinder;
alision5f899632013-04-22 17:26:56 -040077import android.os.RemoteException;
Alexandre Lision35577132013-12-06 15:21:15 -050078import android.preference.PreferenceManager;
Alexandre Lisionc59685a2013-10-04 16:36:36 -040079import android.provider.ContactsContract;
alision55c36cb2013-06-14 14:57:38 -040080import android.provider.ContactsContract.CommonDataKinds.Phone;
81import android.provider.ContactsContract.CommonDataKinds.SipAddress;
alision9f7a6ec2013-05-24 16:26:26 -040082import android.support.v4.view.GravityCompat;
alision9f7a6ec2013-05-24 16:26:26 -040083import android.support.v4.widget.DrawerLayout;
alisionf76de3b2013-04-16 15:35:22 -040084import android.util.Log;
Alexandre Lision3ccccf02013-10-07 14:10:46 -040085import android.view.Gravity;
alision9f7a6ec2013-05-24 16:26:26 -040086import android.view.MenuItem;
87import android.view.View;
alision7297bdb2013-05-21 11:56:55 -040088import android.widget.RelativeLayout;
alision04a00182013-05-10 17:05:29 -040089import android.widget.Toast;
alisionf76de3b2013-04-16 15:35:22 -040090
Alexandre Lisiona8b78722013-12-13 10:18:33 -050091public class HomeActivity extends FragmentActivity implements DialingFragment.Callbacks, AccountsManagementFragment.Callbacks,
Alexandre Lisiona9ee4eb2014-01-15 16:20:35 -050092 ContactListFragment.Callbacks, CallListFragment.Callbacks, HistoryFragment.Callbacks, MenuFragment.Callbacks {
alisionfde875f2013-05-28 17:01:54 -040093
Alexandre Lisionf02190d2013-12-12 17:26:12 -050094 static final String TAG = HomeActivity.class.getSimpleName();
alisionf76de3b2013-04-16 15:35:22 -040095
alision9f7a6ec2013-05-24 16:26:26 -040096 private ContactListFragment mContactsFragment = null;
alision907bde72013-06-20 14:40:37 -040097 private MenuFragment fMenu;
alisionf76de3b2013-04-16 15:35:22 -040098
alision9f7a6ec2013-05-24 16:26:26 -040099 private boolean mBound = false;
100 private ISipService service;
alisionf76de3b2013-04-16 15:35:22 -0400101
alision2cb99562013-05-30 17:02:20 -0400102 public static final int REQUEST_CODE_PREFERENCES = 1;
Alexandre Lision183bf452014-01-17 11:21:59 -0500103 public static final int REQUEST_CODE_CALL = 3;
alisionf76de3b2013-04-16 15:35:22 -0400104
Alexandre Lision450458a2013-11-22 11:33:12 -0500105 SlidingUpPanelLayout mContactDrawer;
Alexandre Lision68900052013-11-01 11:34:56 -0400106 private DrawerLayout mNavigationDrawer;
alision9f7a6ec2013-05-24 16:26:26 -0400107 private ActionBarDrawerToggle mDrawerToggle;
alisionf76de3b2013-04-16 15:35:22 -0400108
Alexandre Lision9d052a12013-11-06 15:13:34 -0500109 private boolean isClosing = false;
110 private Timer t = new Timer();
alision84813a12013-05-27 17:40:39 -0400111
Alexandre Lision9b56a292014-01-23 15:20:15 -0500112 protected Fragment fContent;
Alexandre Lisionf02190d2013-12-12 17:26:12 -0500113
alision9f7a6ec2013-05-24 16:26:26 -0400114 /* called before activity is killed, e.g. rotation */
115 @Override
116 protected void onSaveInstanceState(Bundle bundle) {
117 super.onSaveInstanceState(bundle);
alision9f7a6ec2013-05-24 16:26:26 -0400118 getFragmentManager().putFragment(bundle, "ContactsListFragment", mContactsFragment);
119 Log.w(TAG, "onSaveInstanceState()");
120 }
121
122 @Override
123 public void onCreate(Bundle savedInstanceState) {
124 super.onCreate(savedInstanceState);
125
alision907bde72013-06-20 14:40:37 -0400126
Alexandre Lision269e6042013-12-11 16:15:13 -0500127 setContentView(R.layout.activity_home);
alision58356b72013-06-03 17:13:36 -0400128
alision9f7a6ec2013-05-24 16:26:26 -0400129 // Bind to LocalService
130 if (!mBound) {
131 Log.i(TAG, "onStart: Binding service...");
132 Intent intent = new Intent(this, SipService.class);
Alexandre Lision0f550ee2013-10-25 15:34:38 -0400133 startService(intent);
alision9f7a6ec2013-05-24 16:26:26 -0400134 bindService(intent, mConnection, Context.BIND_AUTO_CREATE);
135 }
Alexandre Lision8b9d8e82013-10-04 09:21:27 -0400136
137 if (savedInstanceState != null) {
Alexandre Lision23628c12013-09-24 11:17:05 -0400138 mContactsFragment = (ContactListFragment) getFragmentManager().getFragment(savedInstanceState, "ContactsListFragment");
139 }
alision9f7a6ec2013-05-24 16:26:26 -0400140 if (mContactsFragment == null) {
141 mContactsFragment = new ContactListFragment();
alision9f7a6ec2013-05-24 16:26:26 -0400142 getFragmentManager().beginTransaction().replace(R.id.contacts_frame, mContactsFragment).commit();
143 }
alision2ec64f92013-06-17 17:28:58 -0400144
Alexandre Lision450458a2013-11-22 11:33:12 -0500145 mContactDrawer = (SlidingUpPanelLayout) findViewById(R.id.contact_panel);
146 // mContactDrawer.setShadowDrawable(getResources().getDrawable(R.drawable.above_shadow));
147 mContactDrawer.setAnchorPoint(0.3f);
148 mContactDrawer.setDragView(findViewById(R.id.contacts_frame));
149 mContactDrawer.setEnableDragViewTouchEvents(true);
Alexandre Lision450458a2013-11-22 11:33:12 -0500150 mContactDrawer.setPanelSlideListener(new PanelSlideListener() {
Alexandre Lision8b9d8e82013-10-04 09:21:27 -0400151
152 @Override
Alexandre Lision450458a2013-11-22 11:33:12 -0500153 public void onPanelSlide(View panel, float slideOffset) {
154 if (slideOffset < 0.2) {
155 if (getActionBar().isShowing()) {
156 getActionBar().hide();
157 }
158 } else {
159 if (!getActionBar().isShowing()) {
160 getActionBar().show();
161 }
Alexandre Lision8b9d8e82013-10-04 09:21:27 -0400162 }
163 }
Alexandre Lision450458a2013-11-22 11:33:12 -0500164
165 @Override
166 public void onPanelExpanded(View panel) {
167
168 }
169
170 @Override
171 public void onPanelCollapsed(View panel) {
172
173 }
174
175 @Override
176 public void onPanelAnchored(View panel) {
177
178 }
Alexandre Lision8b9d8e82013-10-04 09:21:27 -0400179 });
Alexandre Lisiond588bff2013-10-08 12:43:01 -0400180
Alexandre Lision68900052013-11-01 11:34:56 -0400181 mNavigationDrawer = (DrawerLayout) findViewById(R.id.drawer_layout);
alisionf76de3b2013-04-16 15:35:22 -0400182
alision0b553832013-05-30 09:31:06 -0400183 // set a custom shadow that overlays the main content when the drawer opens
Alexandre Lision68900052013-11-01 11:34:56 -0400184 mNavigationDrawer.setDrawerShadow(R.drawable.drawer_shadow, GravityCompat.START);
alisionf76de3b2013-04-16 15:35:22 -0400185
alision9f7a6ec2013-05-24 16:26:26 -0400186 getActionBar().setDisplayHomeAsUpEnabled(true);
187 getActionBar().setHomeButtonEnabled(true);
alisionf76de3b2013-04-16 15:35:22 -0400188
alision9f7a6ec2013-05-24 16:26:26 -0400189 mDrawerToggle = new ActionBarDrawerToggle(this, /* host Activity */
Alexandre Lisionfed2a642014-01-10 12:05:47 -0500190 mNavigationDrawer, /* DrawerLayout object */
191 R.drawable.ic_drawer, /* nav drawer image to replace 'Up' caret */
192 R.string.drawer_open, /* "open drawer" description for accessibility */
193 R.string.drawer_close /* "close drawer" description for accessibility */
alision9f7a6ec2013-05-24 16:26:26 -0400194 ) {
alision0b553832013-05-30 09:31:06 -0400195 @Override
alision9f7a6ec2013-05-24 16:26:26 -0400196 public void onDrawerClosed(View view) {
alision9f7a6ec2013-05-24 16:26:26 -0400197 invalidateOptionsMenu(); // creates call to onPrepareOptionsMenu()
198 }
alisionf76de3b2013-04-16 15:35:22 -0400199
alision0b553832013-05-30 09:31:06 -0400200 @Override
alision9f7a6ec2013-05-24 16:26:26 -0400201 public void onDrawerOpened(View drawerView) {
Alexandre Lision4d20c812013-12-12 17:44:50 -0500202 // getActionBar().setTitle(mDrawerTitle);
alision9f7a6ec2013-05-24 16:26:26 -0400203 invalidateOptionsMenu(); // creates call to onPrepareOptionsMenu()
204 }
205 };
alisionf76de3b2013-04-16 15:35:22 -0400206
Alexandre Lision68900052013-11-01 11:34:56 -0400207 mNavigationDrawer.setDrawerListener(mDrawerToggle);
alisionf76de3b2013-04-16 15:35:22 -0400208
alision9f7a6ec2013-05-24 16:26:26 -0400209 }
alisionf76de3b2013-04-16 15:35:22 -0400210
alisionf57d8a62013-07-02 09:37:12 -0400211 @Override
212 protected void onPostCreate(Bundle savedInstanceState) {
213 super.onPostCreate(savedInstanceState);
214 // Sync the toggle state after onRestoreInstanceState has occurred.
215 mDrawerToggle.syncState();
Alexandre Lision450458a2013-11-22 11:33:12 -0500216 if (mContactDrawer.isExpanded()) {
Alexandre Lisiond16bad92013-10-09 17:16:20 -0400217 getActionBar().hide();
218 }
alisionf57d8a62013-07-02 09:37:12 -0400219 }
alision465ceba2013-07-04 09:24:30 -0400220
alisionf57d8a62013-07-02 09:37:12 -0400221 @Override
222 public void onConfigurationChanged(Configuration newConfig) {
223 super.onConfigurationChanged(newConfig);
224 mDrawerToggle.onConfigurationChanged(newConfig);
225 }
alision3c5c2a92013-05-30 09:50:47 -0400226
alision9f7a6ec2013-05-24 16:26:26 -0400227 @Override
228 protected void onStart() {
229 Log.i(TAG, "onStart");
Alexandre Lision35577132013-12-06 15:21:15 -0500230
231 if (!PreferenceManager.getDefaultSharedPreferences(getApplicationContext()).getBoolean("installed", false)) {
232 PreferenceManager.getDefaultSharedPreferences(getApplicationContext()).edit().putBoolean("installed", true).commit();
233
234 copyAssetFolder(getAssets(), "ringtones", getFilesDir().getAbsolutePath() + "/ringtones");
235 }
236
alision9f7a6ec2013-05-24 16:26:26 -0400237 super.onStart();
Alexandre Lisionc51ccb12013-09-11 16:00:30 -0400238
alision9f7a6ec2013-05-24 16:26:26 -0400239 }
alisionf76de3b2013-04-16 15:35:22 -0400240
Alexandre Lision35577132013-12-06 15:21:15 -0500241 private static boolean copyAssetFolder(AssetManager assetManager, String fromAssetPath, String toPath) {
242 try {
243 String[] files = assetManager.list(fromAssetPath);
244 new File(toPath).mkdirs();
245 Log.i(TAG, "Creating :" + toPath);
246 boolean res = true;
247 for (String file : files)
248 if (file.contains(".")) {
249 Log.i(TAG, "Copying file :" + fromAssetPath + "/" + file + " to " + toPath + "/" + file);
250 res &= copyAsset(assetManager, fromAssetPath + "/" + file, toPath + "/" + file);
251 } else {
252 Log.i(TAG, "Copying folder :" + fromAssetPath + "/" + file + " to " + toPath + "/" + file);
253 res &= copyAssetFolder(assetManager, fromAssetPath + "/" + file, toPath + "/" + file);
254 }
255 return res;
256 } catch (Exception e) {
257 e.printStackTrace();
258 return false;
259 }
260 }
261
262 private static boolean copyAsset(AssetManager assetManager, String fromAssetPath, String toPath) {
263 InputStream in = null;
264 OutputStream out = null;
265 try {
266 in = assetManager.open(fromAssetPath);
267 new File(toPath).createNewFile();
268 out = new FileOutputStream(toPath);
269 copyFile(in, out);
270 in.close();
271 in = null;
272 out.flush();
273 out.close();
274 out = null;
275 return true;
276 } catch (Exception e) {
277 e.printStackTrace();
278 return false;
279 }
280 }
281
282 private static void copyFile(InputStream in, OutputStream out) throws IOException {
283 byte[] buffer = new byte[1024];
284 int read;
285 while ((read = in.read(buffer)) != -1) {
286 out.write(buffer, 0, read);
287 }
288 }
289
alision9f7a6ec2013-05-24 16:26:26 -0400290 /* user gets back to the activity, e.g. through task manager */
291 @Override
292 protected void onRestart() {
293 super.onRestart();
294 }
alisionf76de3b2013-04-16 15:35:22 -0400295
alision9f7a6ec2013-05-24 16:26:26 -0400296 /* activity gets back to the foreground and user input */
297 @Override
298 protected void onResume() {
alision58356b72013-06-03 17:13:36 -0400299 super.onResume();
alision9f7a6ec2013-05-24 16:26:26 -0400300 }
alision907bde72013-06-20 14:40:37 -0400301
alisionbd45a822013-06-06 17:35:35 -0400302 @Override
303 public void onBackPressed() {
Alexandre Lisiond588bff2013-10-08 12:43:01 -0400304
Alexandre Lision68900052013-11-01 11:34:56 -0400305 if (mNavigationDrawer.isDrawerVisible(Gravity.LEFT)) {
306 mNavigationDrawer.closeDrawer(Gravity.LEFT);
Alexandre Lision68855472013-10-10 16:20:46 -0400307 return;
Alexandre Lision3ccccf02013-10-07 14:10:46 -0400308 }
alision907bde72013-06-20 14:40:37 -0400309
Alexandre Lision450458a2013-11-22 11:33:12 -0500310 if (mContactDrawer.isExpanded() || mContactDrawer.isAnchored()) {
311 mContactDrawer.collapsePane();
alisionbd45a822013-06-06 17:35:35 -0400312 return;
313 }
alision2ec64f92013-06-17 17:28:58 -0400314
Alexandre Lisionbe54c342014-01-21 17:10:33 -0500315 if (getSupportFragmentManager().getBackStackEntryCount() > 1) {
Alexandre Lisiond5934b52014-01-07 17:17:37 -0500316 popCustomBackStack();
Alexandre Lisionfc9b4e72013-12-13 14:55:25 -0500317 fMenu.backToHome();
Alexandre Lisionf02190d2013-12-12 17:26:12 -0500318 return;
319 }
320
alisionbd45a822013-06-06 17:35:35 -0400321 if (isClosing) {
322 super.onBackPressed();
323 t.cancel();
324 finish();
325 } else {
alisionbd45a822013-06-06 17:35:35 -0400326 t.schedule(new TimerTask() {
327 @Override
328 public void run() {
329 isClosing = false;
330 }
331 }, 3000);
332 Toast.makeText(this, getResources().getString(R.string.close_msg), Toast.LENGTH_SHORT).show();
333 isClosing = true;
334 }
335 }
alisione2a38e12013-04-25 14:20:20 -0400336
Alexandre Lisiond5934b52014-01-07 17:17:37 -0500337 private void popCustomBackStack() {
Alexandre Lisionbe54c342014-01-21 17:10:33 -0500338 FragmentManager fm = getSupportFragmentManager();
339 FragmentManager.BackStackEntry entry = fm.getBackStackEntryAt(0);
340 fContent = fm.findFragmentByTag(entry.getName());
Alexandre Lisionfed2a642014-01-10 12:05:47 -0500341 for (int i = 0; i < fm.getBackStackEntryCount() - 1; ++i) {
Alexandre Lisiond5934b52014-01-07 17:17:37 -0500342 fm.popBackStack();
343 }
344 }
345
alision9f7a6ec2013-05-24 16:26:26 -0400346 /* activity no more in foreground */
347 @Override
348 protected void onPause() {
349 super.onPause();
Alexandre Lisiona9ee4eb2014-01-15 16:20:35 -0500350
Alexandre Lisionb4a9e392013-10-01 14:39:43 -0400351 if (mBound) {
352 unbindService(mConnection);
353 mBound = false;
354 }
alision9f7a6ec2013-05-24 16:26:26 -0400355 }
alisionf76de3b2013-04-16 15:35:22 -0400356
alision9f7a6ec2013-05-24 16:26:26 -0400357 /* activity finishes itself or is being killed by the system */
358 @Override
359 protected void onDestroy() {
alision9f7a6ec2013-05-24 16:26:26 -0400360 super.onDestroy();
Alexandre Lisionb4a9e392013-10-01 14:39:43 -0400361 Log.i(TAG, "onDestroy: destroying service...");
362 Intent sipServiceIntent = new Intent(this, SipService.class);
363 stopService(sipServiceIntent);
alision9f7a6ec2013-05-24 16:26:26 -0400364 }
alision73424b62013-04-26 11:49:18 -0400365
alision55c36cb2013-06-14 14:57:38 -0400366 public void launchCallActivity(SipCall infos) {
Alexandre Lision67c70b42014-01-16 13:57:15 -0500367 Conference tmp = new Conference(Conference.DEFAULT_ID);
alision465ceba2013-07-04 09:24:30 -0400368
alisiondf1dac92013-06-27 17:35:53 -0400369 tmp.getParticipants().add(infos);
alision9f7a6ec2013-05-24 16:26:26 -0400370 Intent intent = new Intent().setClass(this, CallActivity.class);
Alexandre Lision48b49eb2014-02-11 13:37:33 -0500371 intent.putExtra("conference", tmp);
alisiondf1dac92013-06-27 17:35:53 -0400372 intent.putExtra("resuming", false);
alision9f7a6ec2013-05-24 16:26:26 -0400373 startActivityForResult(intent, REQUEST_CODE_CALL);
Alexandre Lisiond588bff2013-10-08 12:43:01 -0400374
Alexandre Lision8b9d8e82013-10-04 09:21:27 -0400375 // overridePendingTransition(R.anim.slide_down, R.anim.slide_up);
alision9f7a6ec2013-05-24 16:26:26 -0400376 }
alisionf76de3b2013-04-16 15:35:22 -0400377
Alexandre Lisionfed2a642014-01-10 12:05:47 -0500378 /**
379 * Defines callbacks for service binding, passed to bindService()
380 */
alision9f7a6ec2013-05-24 16:26:26 -0400381 private ServiceConnection mConnection = new ServiceConnection() {
alision371b77e2013-04-23 14:51:26 -0400382
alision9f7a6ec2013-05-24 16:26:26 -0400383 @Override
384 public void onServiceConnected(ComponentName className, IBinder binder) {
385 service = ISipService.Stub.asInterface(binder);
alisione2a38e12013-04-25 14:20:20 -0400386
alision2cb99562013-05-30 17:02:20 -0400387 try {
alision58356b72013-06-03 17:13:36 -0400388
alision907bde72013-06-20 14:40:37 -0400389 fMenu = new MenuFragment();
Alexandre Lisionf02190d2013-12-12 17:26:12 -0500390 fContent = new HomeFragment();
Alexandre Lisionbe54c342014-01-21 17:10:33 -0500391 getSupportFragmentManager().beginTransaction().replace(R.id.left_drawer, fMenu).replace(R.id.main_frame, fContent, "Home").addToBackStack("Home").commit();
Alexandre Lisionc51ccb12013-09-11 16:00:30 -0400392
alision2cb99562013-05-30 17:02:20 -0400393 service.destroyNotification();
394 } catch (RemoteException e) {
395 Log.e(TAG, e.toString());
396 }
alision9f7a6ec2013-05-24 16:26:26 -0400397 mBound = true;
alision9f7a6ec2013-05-24 16:26:26 -0400398 Log.d(TAG, "Service connected service=" + service);
alision371b77e2013-04-23 14:51:26 -0400399
alision9f7a6ec2013-05-24 16:26:26 -0400400 }
alision5f899632013-04-22 17:26:56 -0400401
alision9f7a6ec2013-05-24 16:26:26 -0400402 @Override
403 public void onServiceDisconnected(ComponentName arg0) {
alision5f899632013-04-22 17:26:56 -0400404
alision9f7a6ec2013-05-24 16:26:26 -0400405 mBound = false;
406 Log.d(TAG, "Service disconnected service=" + service);
407 }
408 };
alision5f899632013-04-22 17:26:56 -0400409
alision9f7a6ec2013-05-24 16:26:26 -0400410 @Override
411 public boolean onOptionsItemSelected(MenuItem item) {
412 Log.i(TAG, "onOptionsItemSelected " + item.getItemId());
alision5f899632013-04-22 17:26:56 -0400413
alision9f7a6ec2013-05-24 16:26:26 -0400414 if (mDrawerToggle.onOptionsItemSelected(item)) {
415 return true;
416 }
alision5f899632013-04-22 17:26:56 -0400417
alision9f7a6ec2013-05-24 16:26:26 -0400418 return super.onOptionsItemSelected(item);
419 }
alisionf76de3b2013-04-16 15:35:22 -0400420
alision9f7a6ec2013-05-24 16:26:26 -0400421 @Override
422 public void onActivityResult(int requestCode, int resultCode, Intent data) {
423 super.onActivityResult(requestCode, resultCode, data);
alision84813a12013-05-27 17:40:39 -0400424
425 switch (requestCode) {
Alexandre Lisionfed2a642014-01-10 12:05:47 -0500426 case REQUEST_CODE_PREFERENCES:
427 case AccountsManagementFragment.ACCOUNT_EDIT_REQUEST:
428 if (fMenu != null)
429 fMenu.updateAllAccounts();
430 break;
431 case REQUEST_CODE_CALL:
432 if (resultCode == CallActivity.RESULT_FAILURE) {
433 Log.w(TAG, "Call Failed");
434 }
435 break;
alision9f7a6ec2013-05-24 16:26:26 -0400436 }
alisionf76de3b2013-04-16 15:35:22 -0400437
alision9f7a6ec2013-05-24 16:26:26 -0400438 }
alisionf76de3b2013-04-16 15:35:22 -0400439
alision9f7a6ec2013-05-24 16:26:26 -0400440 @Override
alision9f7a6ec2013-05-24 16:26:26 -0400441 public ISipService getService() {
442 return service;
443 }
alisionf76de3b2013-04-16 15:35:22 -0400444
Alexandre Lisione1c96db2013-10-04 14:34:21 -0400445 @Override
446 public void onTextContact(final CallContact c) {
447 // TODO
448 }
Alexandre Lisiond588bff2013-10-08 12:43:01 -0400449
Alexandre Lisionc59685a2013-10-04 16:36:36 -0400450 @Override
451 public void onEditContact(final CallContact c) {
452 Intent intent = new Intent(Intent.ACTION_VIEW);
453 Uri uri = Uri.withAppendedPath(ContactsContract.Contacts.CONTENT_URI, String.valueOf(c.getId()));
454 intent.setData(uri);
455 startActivity(intent);
456 }
alision84813a12013-05-27 17:40:39 -0400457
458 @Override
Alexandre Lisione1c96db2013-10-04 14:34:21 -0400459 public void onCallContact(final CallContact c) {
Alexandre Lision8b9d8e82013-10-04 09:21:27 -0400460
Alexandre Lisionf0b993d2013-09-12 13:25:10 -0400461 if (fMenu.getSelectedAccount() == null) {
Alexandre Lisionf0b993d2013-09-12 13:25:10 -0400462 createAccountDialog().show();
463 return;
464 }
alisionfde875f2013-05-28 17:01:54 -0400465
Alexandre Lisiond588bff2013-10-08 12:43:01 -0400466 if (!fMenu.getSelectedAccount().isRegistered()) {
467 createNotRegisteredDialog().show();
468 return;
469 }
470
Alexandre Lisiond16bad92013-10-09 17:16:20 -0400471 getActionBar().show();
alision55c36cb2013-06-14 14:57:38 -0400472 Thread launcher = new Thread(new Runnable() {
alision907bde72013-06-20 14:40:37 -0400473
Alexandre Lisionfed2a642014-01-10 12:05:47 -0500474 final String[] CONTACTS_PHONES_PROJECTION = new String[]{Phone.NUMBER, Phone.TYPE};
475 final String[] CONTACTS_SIP_PROJECTION = new String[]{SipAddress.SIP_ADDRESS, SipAddress.TYPE};
alision907bde72013-06-20 14:40:37 -0400476
alision55c36cb2013-06-14 14:57:38 -0400477 @Override
478 public void run() {
alision55c36cb2013-06-14 14:57:38 -0400479
Alexandre Lisiona7ab2e32014-02-14 15:33:33 -0500480 Bundle args = new Bundle();
481 args.putString(SipCall.ID, Integer.toString(Math.abs(new Random().nextInt())));
482 args.putParcelable(SipCall.ACCOUNT, fMenu.getSelectedAccount());
483 args.putInt(SipCall.STATE, SipCall.state.CALL_STATE_RINGING);
484 args.putInt(SipCall.TYPE, SipCall.direction.CALL_TYPE_OUTGOING);
alision907bde72013-06-20 14:40:37 -0400485
Alexandre Lisiona7ab2e32014-02-14 15:33:33 -0500486 Cursor cPhones = getContentResolver().query(Phone.CONTENT_URI, CONTACTS_PHONES_PROJECTION, Phone.CONTACT_ID + " =" + c.getId(),
487 null, null);
alision907bde72013-06-20 14:40:37 -0400488
Alexandre Lisiona7ab2e32014-02-14 15:33:33 -0500489 while (cPhones.moveToNext()) {
490 c.addPhoneNumber(cPhones.getString(cPhones.getColumnIndex(Phone.NUMBER)), cPhones.getInt(cPhones.getColumnIndex(Phone.TYPE)));
alision55c36cb2013-06-14 14:57:38 -0400491 }
Alexandre Lisiona7ab2e32014-02-14 15:33:33 -0500492 cPhones.close();
alision907bde72013-06-20 14:40:37 -0400493
Alexandre Lisiona7ab2e32014-02-14 15:33:33 -0500494 Cursor cSip = getContentResolver().query(Phone.CONTENT_URI, CONTACTS_SIP_PROJECTION, Phone.CONTACT_ID + "=" + c.getId(), null,
495 null);
496
497 while (cSip.moveToNext()) {
498 c.addSipNumber(cSip.getString(cSip.getColumnIndex(SipAddress.SIP_ADDRESS)), cSip.getInt(cSip.getColumnIndex(SipAddress.TYPE)));
499 }
500 cSip.close();
501
502 args.putParcelable(SipCall.CONTACT, c);
503
504 launchCallActivity(new SipCall(args));
alision55c36cb2013-06-14 14:57:38 -0400505 }
506 });
507 launcher.start();
Alexandre Lision450458a2013-11-22 11:33:12 -0500508 mContactDrawer.collapsePane();
alision84813a12013-05-27 17:40:39 -0400509
510 }
511
512 @Override
Alexandre Lisione796a872014-01-17 17:08:13 -0500513 public void onCallHistory(HistoryEntry to) {
514
515 Account usedAccount = fMenu.retrieveAccountById(to.getAccountID());
516
517 if (usedAccount == null) {
518 createAccountDialog().show();
519 return;
520 }
521
522 if (usedAccount.isRegistered()) {
Alexandre Lisiona7ab2e32014-02-14 15:33:33 -0500523 Bundle args = new Bundle();
524 args.putString(SipCall.ID, Integer.toString(Math.abs(new Random().nextInt())));
525 args.putParcelable(SipCall.ACCOUNT, usedAccount);
526 args.putInt(SipCall.STATE, SipCall.state.CALL_STATE_RINGING);
527 args.putInt(SipCall.TYPE, SipCall.direction.CALL_TYPE_OUTGOING);
528 args.putParcelable(SipCall.CONTACT, to.getContact());
529
Alexandre Lisione796a872014-01-17 17:08:13 -0500530 try {
Alexandre Lisiona7ab2e32014-02-14 15:33:33 -0500531 launchCallActivity(new SipCall(args));
Alexandre Lisione796a872014-01-17 17:08:13 -0500532 } catch (Exception e) {
533 Log.e(TAG, e.toString());
534 }
535 } else {
536 createNotRegisteredDialog().show();
537 }
538 }
539
540 @Override
alision907bde72013-06-20 14:40:37 -0400541 public void onCallDialed(String to) {
Alexandre Lisione796a872014-01-17 17:08:13 -0500542 Account usedAccount = fMenu.getSelectedAccount();
543
544 if (usedAccount == null) {
Alexandre Lisionc51ccb12013-09-11 16:00:30 -0400545 createAccountDialog().show();
alisiondf1dac92013-06-27 17:35:53 -0400546 return;
547 }
548
Alexandre Lisiond588bff2013-10-08 12:43:01 -0400549 if (fMenu.getSelectedAccount().isRegistered()) {
Alexandre Lisiona7ab2e32014-02-14 15:33:33 -0500550 Bundle args = new Bundle();
551 args.putString(SipCall.ID, Integer.toString(Math.abs(new Random().nextInt())));
552 args.putParcelable(SipCall.ACCOUNT, usedAccount);
553 args.putInt(SipCall.STATE, SipCall.state.CALL_STATE_RINGING);
554 args.putInt(SipCall.TYPE, SipCall.direction.CALL_TYPE_OUTGOING);
555 args.putParcelable(SipCall.CONTACT, CallContact.ContactBuilder.buildUnknownContact(to));
alision84813a12013-05-27 17:40:39 -0400556
Alexandre Lisiond588bff2013-10-08 12:43:01 -0400557 try {
Alexandre Lisiona7ab2e32014-02-14 15:33:33 -0500558 launchCallActivity(new SipCall(args));
Alexandre Lisiond588bff2013-10-08 12:43:01 -0400559 } catch (Exception e) {
560 Log.e(TAG, e.toString());
561 }
562 } else {
Alexandre Lision5ed2c972013-10-11 15:36:33 -0400563 createNotRegisteredDialog().show();
alisionfde875f2013-05-28 17:01:54 -0400564 }
alision9f7a6ec2013-05-24 16:26:26 -0400565 }
alision371b77e2013-04-23 14:51:26 -0400566
Alexandre Lisiond588bff2013-10-08 12:43:01 -0400567 private AlertDialog createNotRegisteredDialog() {
568 final Activity ownerActivity = this;
569 AlertDialog.Builder builder = new AlertDialog.Builder(ownerActivity);
570
571 builder.setMessage(getResources().getString(R.string.cannot_pass_sipcall))
572 .setTitle(getResources().getString(R.string.cannot_pass_sipcall_title))
573 .setPositiveButton(getResources().getString(android.R.string.ok), new DialogInterface.OnClickListener() {
574 public void onClick(DialogInterface dialog, int whichButton) {
Alexandre Lision5ed2c972013-10-11 15:36:33 -0400575
Alexandre Lisiond588bff2013-10-08 12:43:01 -0400576 }
577 });
578
579 AlertDialog alertDialog = builder.create();
580 alertDialog.setOwnerActivity(ownerActivity);
581
582 return alertDialog;
583 }
584
Alexandre Lisionc51ccb12013-09-11 16:00:30 -0400585 private AlertDialog createAccountDialog() {
586 final Activity ownerActivity = this;
587 AlertDialog.Builder builder = new AlertDialog.Builder(ownerActivity);
588
589 builder.setMessage(getResources().getString(R.string.create_new_account_dialog))
590 .setTitle(getResources().getString(R.string.create_new_account_dialog_title))
591 .setPositiveButton(getResources().getString(android.R.string.ok), new DialogInterface.OnClickListener() {
592 public void onClick(DialogInterface dialog, int whichButton) {
593 Intent in = new Intent();
Alexandre Lision40954dc2013-10-09 15:24:03 -0400594 in.setClass(ownerActivity, AccountWizard.class);
Alexandre Lision4ab53972013-11-04 16:59:18 -0500595 ownerActivity.startActivityForResult(in, HomeActivity.REQUEST_CODE_PREFERENCES);
Alexandre Lisionc51ccb12013-09-11 16:00:30 -0400596 }
597 }).setNegativeButton(getResources().getString(android.R.string.cancel), new DialogInterface.OnClickListener() {
Alexandre Lisionfed2a642014-01-10 12:05:47 -0500598 public void onClick(DialogInterface dialog, int whichButton) {
599 dialog.dismiss();
600 }
601 });
Alexandre Lisionc51ccb12013-09-11 16:00:30 -0400602
603 AlertDialog alertDialog = builder.create();
604 alertDialog.setOwnerActivity(ownerActivity);
605
606 return alertDialog;
607 }
608
alisiond45da712013-05-30 09:18:49 -0400609 @Override
610 public void onContactDragged() {
Alexandre Lision450458a2013-11-22 11:33:12 -0500611 mContactDrawer.collapsePane();
alisiond45da712013-05-30 09:18:49 -0400612 }
613
alision58356b72013-06-03 17:13:36 -0400614 @Override
Alexandre Lision0936ba62013-11-25 10:04:56 -0500615 public void toggleDrawer() {
Alexandre Lisionc985b362013-12-10 17:45:03 -0500616 if (mContactDrawer.isAnchored())
Alexandre Lision0936ba62013-11-25 10:04:56 -0500617 mContactDrawer.expandPane();
Alexandre Lisionc985b362013-12-10 17:45:03 -0500618 else if (!mContactDrawer.isExpanded())
619 mContactDrawer.expandPane(0.3f);
Alexandre Lisionc9b4e9e2013-11-27 10:27:28 -0500620 else
621 mContactDrawer.collapsePane();
alision58356b72013-06-03 17:13:36 -0400622 }
623
alision806e18e2013-06-21 15:30:17 -0400624 @Override
Alexandre Lisiona458ba22013-12-11 15:04:11 -0500625 public void toggleForSearchDrawer() {
626 if (mContactDrawer.isExpanded()) {
627 mContactDrawer.collapsePane();
628 } else
629 mContactDrawer.expandPane();
630 }
631
632 @Override
Alexandre Lision0936ba62013-11-25 10:04:56 -0500633 public void setDragView(RelativeLayout relativeLayout) {
634 mContactDrawer.setDragView(relativeLayout);
635 }
636
Alexandre Lision269e6042013-12-11 16:15:13 -0500637 @Override
638 public void onSectionSelected(int pos) {
Alexandre Lisionf02190d2013-12-12 17:26:12 -0500639
640 mNavigationDrawer.closeDrawers();
Alexandre Lisionfed2a642014-01-10 12:05:47 -0500641
642 switch (pos) {
643 case 0:
644
645 if (fContent instanceof HomeFragment)
646 break;
647
Alexandre Lisionbe54c342014-01-21 17:10:33 -0500648 if (getSupportFragmentManager().getBackStackEntryCount() == 1)
Alexandre Lisionfed2a642014-01-10 12:05:47 -0500649 break;
650
651 popCustomBackStack();
652
653 break;
654 case 1:
655 if (fContent instanceof AccountsManagementFragment)
656 break;
657 fContent = new AccountsManagementFragment();
Alexandre Lisionbe54c342014-01-21 17:10:33 -0500658 getSupportFragmentManager().beginTransaction().setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE).replace(R.id.main_frame, fContent, "Accounts").addToBackStack("Accounts").commit();
Alexandre Lisionfed2a642014-01-10 12:05:47 -0500659 break;
660 case 2:
661 if (fContent instanceof AboutFragment)
662 break;
663 fContent = new AboutFragment();
Alexandre Lisionbe54c342014-01-21 17:10:33 -0500664 getSupportFragmentManager().beginTransaction().setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE).replace(R.id.main_frame, fContent, "About").addToBackStack("About").commit();
Alexandre Lisionfed2a642014-01-10 12:05:47 -0500665 break;
666 }
667
668
Alexandre Lision269e6042013-12-11 16:15:13 -0500669 }
670
alisionf76de3b2013-04-16 15:35:22 -0400671}