blob: 244d7052e8bf8ecb3b659febb97a2a34bb45cd4c [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;
Alexandre Lisiondbe396d2014-02-24 16:53:25 -050046import android.content.pm.PackageManager;
Alexandre Lisionbe54c342014-01-21 17:10:33 -050047import android.support.v4.app.*;
Alexandre Lision064e1e02013-10-01 16:18:42 -040048import org.sflphone.R;
Alexandre Lisionf02190d2013-12-12 17:26:12 -050049import org.sflphone.fragments.AboutFragment;
50import org.sflphone.fragments.AccountsManagementFragment;
51import org.sflphone.fragments.CallListFragment;
Alexandre Lision064e1e02013-10-01 16:18:42 -040052import org.sflphone.fragments.ContactListFragment;
53import org.sflphone.fragments.DialingFragment;
54import org.sflphone.fragments.HistoryFragment;
55import org.sflphone.fragments.HomeFragment;
56import org.sflphone.fragments.MenuFragment;
Alexandre Lisione796a872014-01-17 17:08:13 -050057import org.sflphone.history.HistoryEntry;
58import org.sflphone.model.Account;
Alexandre Lision064e1e02013-10-01 16:18:42 -040059import org.sflphone.model.CallContact;
60import org.sflphone.model.Conference;
61import org.sflphone.model.SipCall;
Alexandre Lision064e1e02013-10-01 16:18:42 -040062import org.sflphone.service.ISipService;
63import org.sflphone.service.SipService;
Alexandre Lision450458a2013-11-22 11:33:12 -050064import org.sflphone.views.SlidingUpPanelLayout;
65import org.sflphone.views.SlidingUpPanelLayout.PanelSlideListener;
Alexandre Lision064e1e02013-10-01 16:18:42 -040066
alisionf76de3b2013-04-16 15:35:22 -040067import android.content.ComponentName;
68import android.content.Context;
Alexandre Lisionc51ccb12013-09-11 16:00:30 -040069import android.content.DialogInterface;
alisionf76de3b2013-04-16 15:35:22 -040070import android.content.Intent;
alisionf76de3b2013-04-16 15:35:22 -040071import android.content.ServiceConnection;
Alexandre Lision35577132013-12-06 15:21:15 -050072import android.content.res.AssetManager;
alisionf57d8a62013-07-02 09:37:12 -040073import android.content.res.Configuration;
alision55c36cb2013-06-14 14:57:38 -040074import android.database.Cursor;
Alexandre Lisionc59685a2013-10-04 16:36:36 -040075import android.net.Uri;
alisionf76de3b2013-04-16 15:35:22 -040076import android.os.Bundle;
77import android.os.IBinder;
alision5f899632013-04-22 17:26:56 -040078import android.os.RemoteException;
Alexandre Lision35577132013-12-06 15:21:15 -050079import android.preference.PreferenceManager;
Alexandre Lisionc59685a2013-10-04 16:36:36 -040080import android.provider.ContactsContract;
alision55c36cb2013-06-14 14:57:38 -040081import android.provider.ContactsContract.CommonDataKinds.Phone;
82import android.provider.ContactsContract.CommonDataKinds.SipAddress;
alision9f7a6ec2013-05-24 16:26:26 -040083import android.support.v4.view.GravityCompat;
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;
alision7297bdb2013-05-21 11:56:55 -040089import android.widget.RelativeLayout;
alision04a00182013-05-10 17:05:29 -040090import android.widget.Toast;
alisionf76de3b2013-04-16 15:35:22 -040091
Alexandre Lisiona8b78722013-12-13 10:18:33 -050092public class HomeActivity extends FragmentActivity implements DialingFragment.Callbacks, AccountsManagementFragment.Callbacks,
Alexandre Lisiona9ee4eb2014-01-15 16:20:35 -050093 ContactListFragment.Callbacks, CallListFragment.Callbacks, HistoryFragment.Callbacks, MenuFragment.Callbacks {
alisionfde875f2013-05-28 17:01:54 -040094
Alexandre Lisionf02190d2013-12-12 17:26:12 -050095 static final String TAG = HomeActivity.class.getSimpleName();
alisionf76de3b2013-04-16 15:35:22 -040096
alision9f7a6ec2013-05-24 16:26:26 -040097 private ContactListFragment mContactsFragment = null;
alision907bde72013-06-20 14:40:37 -040098 private MenuFragment fMenu;
alisionf76de3b2013-04-16 15:35:22 -040099
alision9f7a6ec2013-05-24 16:26:26 -0400100 private boolean mBound = false;
101 private ISipService service;
alisionf76de3b2013-04-16 15:35:22 -0400102
alision2cb99562013-05-30 17:02:20 -0400103 public static final int REQUEST_CODE_PREFERENCES = 1;
Alexandre Lision183bf452014-01-17 11:21:59 -0500104 public static final int REQUEST_CODE_CALL = 3;
alisionf76de3b2013-04-16 15:35:22 -0400105
Alexandre Lision450458a2013-11-22 11:33:12 -0500106 SlidingUpPanelLayout mContactDrawer;
Alexandre Lision68900052013-11-01 11:34:56 -0400107 private DrawerLayout mNavigationDrawer;
alision9f7a6ec2013-05-24 16:26:26 -0400108 private ActionBarDrawerToggle mDrawerToggle;
alisionf76de3b2013-04-16 15:35:22 -0400109
Alexandre Lision9d052a12013-11-06 15:13:34 -0500110 private boolean isClosing = false;
111 private Timer t = new Timer();
alision84813a12013-05-27 17:40:39 -0400112
Alexandre Lision9b56a292014-01-23 15:20:15 -0500113 protected Fragment fContent;
Alexandre Lisionf02190d2013-12-12 17:26:12 -0500114
alision9f7a6ec2013-05-24 16:26:26 -0400115 /* called before activity is killed, e.g. rotation */
116 @Override
117 protected void onSaveInstanceState(Bundle bundle) {
118 super.onSaveInstanceState(bundle);
alision9f7a6ec2013-05-24 16:26:26 -0400119 getFragmentManager().putFragment(bundle, "ContactsListFragment", mContactsFragment);
120 Log.w(TAG, "onSaveInstanceState()");
121 }
122
123 @Override
124 public void onCreate(Bundle savedInstanceState) {
125 super.onCreate(savedInstanceState);
Alexandre Lision269e6042013-12-11 16:15:13 -0500126 setContentView(R.layout.activity_home);
alision58356b72013-06-03 17:13:36 -0400127
alision9f7a6ec2013-05-24 16:26:26 -0400128 // Bind to LocalService
129 if (!mBound) {
130 Log.i(TAG, "onStart: Binding service...");
131 Intent intent = new Intent(this, SipService.class);
Alexandre Lision0f550ee2013-10-25 15:34:38 -0400132 startService(intent);
alision9f7a6ec2013-05-24 16:26:26 -0400133 bindService(intent, mConnection, Context.BIND_AUTO_CREATE);
134 }
Alexandre Lision8b9d8e82013-10-04 09:21:27 -0400135
136 if (savedInstanceState != null) {
Alexandre Lision23628c12013-09-24 11:17:05 -0400137 mContactsFragment = (ContactListFragment) getFragmentManager().getFragment(savedInstanceState, "ContactsListFragment");
138 }
alision9f7a6ec2013-05-24 16:26:26 -0400139 if (mContactsFragment == null) {
140 mContactsFragment = new ContactListFragment();
alision9f7a6ec2013-05-24 16:26:26 -0400141 getFragmentManager().beginTransaction().replace(R.id.contacts_frame, mContactsFragment).commit();
142 }
alision2ec64f92013-06-17 17:28:58 -0400143
Alexandre Lision450458a2013-11-22 11:33:12 -0500144 mContactDrawer = (SlidingUpPanelLayout) findViewById(R.id.contact_panel);
145 // mContactDrawer.setShadowDrawable(getResources().getDrawable(R.drawable.above_shadow));
146 mContactDrawer.setAnchorPoint(0.3f);
147 mContactDrawer.setDragView(findViewById(R.id.contacts_frame));
148 mContactDrawer.setEnableDragViewTouchEvents(true);
Alexandre Lision450458a2013-11-22 11:33:12 -0500149 mContactDrawer.setPanelSlideListener(new PanelSlideListener() {
Alexandre Lision8b9d8e82013-10-04 09:21:27 -0400150
151 @Override
Alexandre Lision450458a2013-11-22 11:33:12 -0500152 public void onPanelSlide(View panel, float slideOffset) {
153 if (slideOffset < 0.2) {
154 if (getActionBar().isShowing()) {
155 getActionBar().hide();
156 }
157 } else {
158 if (!getActionBar().isShowing()) {
159 getActionBar().show();
160 }
Alexandre Lision8b9d8e82013-10-04 09:21:27 -0400161 }
162 }
Alexandre Lision450458a2013-11-22 11:33:12 -0500163
164 @Override
165 public void onPanelExpanded(View panel) {
166
167 }
168
169 @Override
170 public void onPanelCollapsed(View panel) {
171
172 }
173
174 @Override
175 public void onPanelAnchored(View panel) {
176
177 }
Alexandre Lision8b9d8e82013-10-04 09:21:27 -0400178 });
Alexandre Lisiond588bff2013-10-08 12:43:01 -0400179
Alexandre Lision68900052013-11-01 11:34:56 -0400180 mNavigationDrawer = (DrawerLayout) findViewById(R.id.drawer_layout);
alisionf76de3b2013-04-16 15:35:22 -0400181
alision0b553832013-05-30 09:31:06 -0400182 // set a custom shadow that overlays the main content when the drawer opens
Alexandre Lision68900052013-11-01 11:34:56 -0400183 mNavigationDrawer.setDrawerShadow(R.drawable.drawer_shadow, GravityCompat.START);
alisionf76de3b2013-04-16 15:35:22 -0400184
alision9f7a6ec2013-05-24 16:26:26 -0400185 getActionBar().setDisplayHomeAsUpEnabled(true);
186 getActionBar().setHomeButtonEnabled(true);
alisionf76de3b2013-04-16 15:35:22 -0400187
alision9f7a6ec2013-05-24 16:26:26 -0400188 mDrawerToggle = new ActionBarDrawerToggle(this, /* host Activity */
Alexandre Lisionfed2a642014-01-10 12:05:47 -0500189 mNavigationDrawer, /* DrawerLayout object */
190 R.drawable.ic_drawer, /* nav drawer image to replace 'Up' caret */
191 R.string.drawer_open, /* "open drawer" description for accessibility */
192 R.string.drawer_close /* "close drawer" description for accessibility */
alision9f7a6ec2013-05-24 16:26:26 -0400193 ) {
alision0b553832013-05-30 09:31:06 -0400194 @Override
alision9f7a6ec2013-05-24 16:26:26 -0400195 public void onDrawerClosed(View view) {
alision9f7a6ec2013-05-24 16:26:26 -0400196 invalidateOptionsMenu(); // creates call to onPrepareOptionsMenu()
197 }
alisionf76de3b2013-04-16 15:35:22 -0400198
alision0b553832013-05-30 09:31:06 -0400199 @Override
alision9f7a6ec2013-05-24 16:26:26 -0400200 public void onDrawerOpened(View drawerView) {
Alexandre Lision4d20c812013-12-12 17:44:50 -0500201 // getActionBar().setTitle(mDrawerTitle);
alision9f7a6ec2013-05-24 16:26:26 -0400202 invalidateOptionsMenu(); // creates call to onPrepareOptionsMenu()
203 }
204 };
alisionf76de3b2013-04-16 15:35:22 -0400205
Alexandre Lision68900052013-11-01 11:34:56 -0400206 mNavigationDrawer.setDrawerListener(mDrawerToggle);
alisionf76de3b2013-04-16 15:35:22 -0400207
alision9f7a6ec2013-05-24 16:26:26 -0400208 }
alisionf76de3b2013-04-16 15:35:22 -0400209
alisionf57d8a62013-07-02 09:37:12 -0400210 @Override
211 protected void onPostCreate(Bundle savedInstanceState) {
212 super.onPostCreate(savedInstanceState);
213 // Sync the toggle state after onRestoreInstanceState has occurred.
214 mDrawerToggle.syncState();
Alexandre Lision450458a2013-11-22 11:33:12 -0500215 if (mContactDrawer.isExpanded()) {
Alexandre Lisiond16bad92013-10-09 17:16:20 -0400216 getActionBar().hide();
217 }
alisionf57d8a62013-07-02 09:37:12 -0400218 }
alision465ceba2013-07-04 09:24:30 -0400219
alisionf57d8a62013-07-02 09:37:12 -0400220 @Override
221 public void onConfigurationChanged(Configuration newConfig) {
222 super.onConfigurationChanged(newConfig);
223 mDrawerToggle.onConfigurationChanged(newConfig);
224 }
alision3c5c2a92013-05-30 09:50:47 -0400225
alision9f7a6ec2013-05-24 16:26:26 -0400226 @Override
227 protected void onStart() {
228 Log.i(TAG, "onStart");
Alexandre Lision35577132013-12-06 15:21:15 -0500229
230 if (!PreferenceManager.getDefaultSharedPreferences(getApplicationContext()).getBoolean("installed", false)) {
231 PreferenceManager.getDefaultSharedPreferences(getApplicationContext()).edit().putBoolean("installed", true).commit();
232
233 copyAssetFolder(getAssets(), "ringtones", getFilesDir().getAbsolutePath() + "/ringtones");
234 }
235
alision9f7a6ec2013-05-24 16:26:26 -0400236 super.onStart();
Alexandre Lisionc51ccb12013-09-11 16:00:30 -0400237
alision9f7a6ec2013-05-24 16:26:26 -0400238 }
alisionf76de3b2013-04-16 15:35:22 -0400239
Alexandre Lision35577132013-12-06 15:21:15 -0500240 private static boolean copyAssetFolder(AssetManager assetManager, String fromAssetPath, String toPath) {
241 try {
242 String[] files = assetManager.list(fromAssetPath);
243 new File(toPath).mkdirs();
244 Log.i(TAG, "Creating :" + toPath);
245 boolean res = true;
246 for (String file : files)
247 if (file.contains(".")) {
248 Log.i(TAG, "Copying file :" + fromAssetPath + "/" + file + " to " + toPath + "/" + file);
249 res &= copyAsset(assetManager, fromAssetPath + "/" + file, toPath + "/" + file);
250 } else {
251 Log.i(TAG, "Copying folder :" + fromAssetPath + "/" + file + " to " + toPath + "/" + file);
252 res &= copyAssetFolder(assetManager, fromAssetPath + "/" + file, toPath + "/" + file);
253 }
254 return res;
255 } catch (Exception e) {
256 e.printStackTrace();
257 return false;
258 }
259 }
260
261 private static boolean copyAsset(AssetManager assetManager, String fromAssetPath, String toPath) {
262 InputStream in = null;
263 OutputStream out = null;
264 try {
265 in = assetManager.open(fromAssetPath);
266 new File(toPath).createNewFile();
267 out = new FileOutputStream(toPath);
268 copyFile(in, out);
269 in.close();
270 in = null;
271 out.flush();
272 out.close();
273 out = null;
274 return true;
275 } catch (Exception e) {
276 e.printStackTrace();
277 return false;
278 }
279 }
280
281 private static void copyFile(InputStream in, OutputStream out) throws IOException {
282 byte[] buffer = new byte[1024];
283 int read;
284 while ((read = in.read(buffer)) != -1) {
285 out.write(buffer, 0, read);
286 }
287 }
288
alision9f7a6ec2013-05-24 16:26:26 -0400289 /* user gets back to the activity, e.g. through task manager */
290 @Override
291 protected void onRestart() {
292 super.onRestart();
293 }
alisionf76de3b2013-04-16 15:35:22 -0400294
alision9f7a6ec2013-05-24 16:26:26 -0400295 /* activity gets back to the foreground and user input */
296 @Override
297 protected void onResume() {
alision58356b72013-06-03 17:13:36 -0400298 super.onResume();
alision9f7a6ec2013-05-24 16:26:26 -0400299 }
alision907bde72013-06-20 14:40:37 -0400300
alisionbd45a822013-06-06 17:35:35 -0400301 @Override
302 public void onBackPressed() {
Alexandre Lisiond588bff2013-10-08 12:43:01 -0400303
Alexandre Lision68900052013-11-01 11:34:56 -0400304 if (mNavigationDrawer.isDrawerVisible(Gravity.LEFT)) {
305 mNavigationDrawer.closeDrawer(Gravity.LEFT);
Alexandre Lision68855472013-10-10 16:20:46 -0400306 return;
Alexandre Lision3ccccf02013-10-07 14:10:46 -0400307 }
alision907bde72013-06-20 14:40:37 -0400308
Alexandre Lision450458a2013-11-22 11:33:12 -0500309 if (mContactDrawer.isExpanded() || mContactDrawer.isAnchored()) {
310 mContactDrawer.collapsePane();
alisionbd45a822013-06-06 17:35:35 -0400311 return;
312 }
alision2ec64f92013-06-17 17:28:58 -0400313
Alexandre Lisionbe54c342014-01-21 17:10:33 -0500314 if (getSupportFragmentManager().getBackStackEntryCount() > 1) {
Alexandre Lisiond5934b52014-01-07 17:17:37 -0500315 popCustomBackStack();
Alexandre Lisionfc9b4e72013-12-13 14:55:25 -0500316 fMenu.backToHome();
Alexandre Lisionf02190d2013-12-12 17:26:12 -0500317 return;
318 }
319
alisionbd45a822013-06-06 17:35:35 -0400320 if (isClosing) {
321 super.onBackPressed();
322 t.cancel();
323 finish();
324 } else {
alisionbd45a822013-06-06 17:35:35 -0400325 t.schedule(new TimerTask() {
326 @Override
327 public void run() {
328 isClosing = false;
329 }
330 }, 3000);
331 Toast.makeText(this, getResources().getString(R.string.close_msg), Toast.LENGTH_SHORT).show();
332 isClosing = true;
333 }
334 }
alisione2a38e12013-04-25 14:20:20 -0400335
Alexandre Lisiond5934b52014-01-07 17:17:37 -0500336 private void popCustomBackStack() {
Alexandre Lisionbe54c342014-01-21 17:10:33 -0500337 FragmentManager fm = getSupportFragmentManager();
338 FragmentManager.BackStackEntry entry = fm.getBackStackEntryAt(0);
339 fContent = fm.findFragmentByTag(entry.getName());
Alexandre Lisionfed2a642014-01-10 12:05:47 -0500340 for (int i = 0; i < fm.getBackStackEntryCount() - 1; ++i) {
Alexandre Lisiond5934b52014-01-07 17:17:37 -0500341 fm.popBackStack();
342 }
343 }
344
alision9f7a6ec2013-05-24 16:26:26 -0400345 /* activity no more in foreground */
346 @Override
347 protected void onPause() {
348 super.onPause();
Alexandre Lisiona9ee4eb2014-01-15 16:20:35 -0500349
Alexandre Lisionb4a9e392013-10-01 14:39:43 -0400350 if (mBound) {
351 unbindService(mConnection);
352 mBound = false;
353 }
alision9f7a6ec2013-05-24 16:26:26 -0400354 }
alisionf76de3b2013-04-16 15:35:22 -0400355
alision9f7a6ec2013-05-24 16:26:26 -0400356 /* activity finishes itself or is being killed by the system */
357 @Override
358 protected void onDestroy() {
alision9f7a6ec2013-05-24 16:26:26 -0400359 super.onDestroy();
Alexandre Lisionb4a9e392013-10-01 14:39:43 -0400360 Log.i(TAG, "onDestroy: destroying service...");
361 Intent sipServiceIntent = new Intent(this, SipService.class);
362 stopService(sipServiceIntent);
alision9f7a6ec2013-05-24 16:26:26 -0400363 }
alision73424b62013-04-26 11:49:18 -0400364
alision55c36cb2013-06-14 14:57:38 -0400365 public void launchCallActivity(SipCall infos) {
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);
alision9f7a6ec2013-05-24 16:26:26 -0400369 Intent intent = new Intent().setClass(this, CallActivity.class);
Alexandre Lision48b49eb2014-02-11 13:37:33 -0500370 intent.putExtra("conference", tmp);
alisiondf1dac92013-06-27 17:35:53 -0400371 intent.putExtra("resuming", false);
alision9f7a6ec2013-05-24 16:26:26 -0400372 startActivityForResult(intent, REQUEST_CODE_CALL);
Alexandre Lisiond588bff2013-10-08 12:43:01 -0400373
Alexandre Lision8b9d8e82013-10-04 09:21:27 -0400374 // overridePendingTransition(R.anim.slide_down, R.anim.slide_up);
alision9f7a6ec2013-05-24 16:26:26 -0400375 }
alisionf76de3b2013-04-16 15:35:22 -0400376
Alexandre Lisionfed2a642014-01-10 12:05:47 -0500377 /**
378 * Defines callbacks for service binding, passed to bindService()
379 */
alision9f7a6ec2013-05-24 16:26:26 -0400380 private ServiceConnection mConnection = new ServiceConnection() {
alision371b77e2013-04-23 14:51:26 -0400381
alision9f7a6ec2013-05-24 16:26:26 -0400382 @Override
383 public void onServiceConnected(ComponentName className, IBinder binder) {
384 service = ISipService.Stub.asInterface(binder);
alisione2a38e12013-04-25 14:20:20 -0400385
Alexandre Lisiondbe396d2014-02-24 16:53:25 -0500386
387
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 Lisionbe54c342014-01-21 17:10:33 -0500392 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 -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() {
alision55c36cb2013-06-14 14:57:38 -0400480
Alexandre Lisiona7ab2e32014-02-14 15:33:33 -0500481 Bundle args = new Bundle();
482 args.putString(SipCall.ID, Integer.toString(Math.abs(new Random().nextInt())));
483 args.putParcelable(SipCall.ACCOUNT, fMenu.getSelectedAccount());
484 args.putInt(SipCall.STATE, SipCall.state.CALL_STATE_RINGING);
485 args.putInt(SipCall.TYPE, SipCall.direction.CALL_TYPE_OUTGOING);
alision907bde72013-06-20 14:40:37 -0400486
Alexandre Lisiona7ab2e32014-02-14 15:33:33 -0500487 Cursor cPhones = getContentResolver().query(Phone.CONTENT_URI, CONTACTS_PHONES_PROJECTION, Phone.CONTACT_ID + " =" + c.getId(),
488 null, null);
alision907bde72013-06-20 14:40:37 -0400489
Alexandre Lisiona7ab2e32014-02-14 15:33:33 -0500490 while (cPhones.moveToNext()) {
491 c.addPhoneNumber(cPhones.getString(cPhones.getColumnIndex(Phone.NUMBER)), cPhones.getInt(cPhones.getColumnIndex(Phone.TYPE)));
alision55c36cb2013-06-14 14:57:38 -0400492 }
Alexandre Lisiona7ab2e32014-02-14 15:33:33 -0500493 cPhones.close();
alision907bde72013-06-20 14:40:37 -0400494
Alexandre Lisiona7ab2e32014-02-14 15:33:33 -0500495 Cursor cSip = getContentResolver().query(Phone.CONTENT_URI, CONTACTS_SIP_PROJECTION, Phone.CONTACT_ID + "=" + c.getId(), null,
496 null);
497
498 while (cSip.moveToNext()) {
499 c.addSipNumber(cSip.getString(cSip.getColumnIndex(SipAddress.SIP_ADDRESS)), cSip.getInt(cSip.getColumnIndex(SipAddress.TYPE)));
500 }
501 cSip.close();
502
503 args.putParcelable(SipCall.CONTACT, c);
504
505 launchCallActivity(new SipCall(args));
alision55c36cb2013-06-14 14:57:38 -0400506 }
507 });
508 launcher.start();
Alexandre Lision450458a2013-11-22 11:33:12 -0500509 mContactDrawer.collapsePane();
alision84813a12013-05-27 17:40:39 -0400510
511 }
512
513 @Override
Alexandre Lisione796a872014-01-17 17:08:13 -0500514 public void onCallHistory(HistoryEntry to) {
515
516 Account usedAccount = fMenu.retrieveAccountById(to.getAccountID());
517
518 if (usedAccount == null) {
519 createAccountDialog().show();
520 return;
521 }
522
523 if (usedAccount.isRegistered()) {
Alexandre Lisiona7ab2e32014-02-14 15:33:33 -0500524 Bundle args = new Bundle();
525 args.putString(SipCall.ID, Integer.toString(Math.abs(new Random().nextInt())));
526 args.putParcelable(SipCall.ACCOUNT, usedAccount);
527 args.putInt(SipCall.STATE, SipCall.state.CALL_STATE_RINGING);
528 args.putInt(SipCall.TYPE, SipCall.direction.CALL_TYPE_OUTGOING);
529 args.putParcelable(SipCall.CONTACT, to.getContact());
530
Alexandre Lisione796a872014-01-17 17:08:13 -0500531 try {
Alexandre Lisiona7ab2e32014-02-14 15:33:33 -0500532 launchCallActivity(new SipCall(args));
Alexandre Lisione796a872014-01-17 17:08:13 -0500533 } catch (Exception e) {
534 Log.e(TAG, e.toString());
535 }
536 } else {
537 createNotRegisteredDialog().show();
538 }
539 }
540
541 @Override
alision907bde72013-06-20 14:40:37 -0400542 public void onCallDialed(String to) {
Alexandre Lisione796a872014-01-17 17:08:13 -0500543 Account usedAccount = fMenu.getSelectedAccount();
544
545 if (usedAccount == null) {
Alexandre Lisionc51ccb12013-09-11 16:00:30 -0400546 createAccountDialog().show();
alisiondf1dac92013-06-27 17:35:53 -0400547 return;
548 }
549
Alexandre Lisiond588bff2013-10-08 12:43:01 -0400550 if (fMenu.getSelectedAccount().isRegistered()) {
Alexandre Lisiona7ab2e32014-02-14 15:33:33 -0500551 Bundle args = new Bundle();
552 args.putString(SipCall.ID, Integer.toString(Math.abs(new Random().nextInt())));
553 args.putParcelable(SipCall.ACCOUNT, usedAccount);
554 args.putInt(SipCall.STATE, SipCall.state.CALL_STATE_RINGING);
555 args.putInt(SipCall.TYPE, SipCall.direction.CALL_TYPE_OUTGOING);
556 args.putParcelable(SipCall.CONTACT, CallContact.ContactBuilder.buildUnknownContact(to));
alision84813a12013-05-27 17:40:39 -0400557
Alexandre Lisiond588bff2013-10-08 12:43:01 -0400558 try {
Alexandre Lisiona7ab2e32014-02-14 15:33:33 -0500559 launchCallActivity(new SipCall(args));
Alexandre Lisiond588bff2013-10-08 12:43:01 -0400560 } catch (Exception e) {
561 Log.e(TAG, e.toString());
562 }
563 } else {
Alexandre Lision5ed2c972013-10-11 15:36:33 -0400564 createNotRegisteredDialog().show();
alisionfde875f2013-05-28 17:01:54 -0400565 }
alision9f7a6ec2013-05-24 16:26:26 -0400566 }
alision371b77e2013-04-23 14:51:26 -0400567
Alexandre Lisiond588bff2013-10-08 12:43:01 -0400568 private AlertDialog createNotRegisteredDialog() {
569 final Activity ownerActivity = this;
570 AlertDialog.Builder builder = new AlertDialog.Builder(ownerActivity);
571
572 builder.setMessage(getResources().getString(R.string.cannot_pass_sipcall))
573 .setTitle(getResources().getString(R.string.cannot_pass_sipcall_title))
574 .setPositiveButton(getResources().getString(android.R.string.ok), new DialogInterface.OnClickListener() {
575 public void onClick(DialogInterface dialog, int whichButton) {
Alexandre Lision5ed2c972013-10-11 15:36:33 -0400576
Alexandre Lisiond588bff2013-10-08 12:43:01 -0400577 }
578 });
579
580 AlertDialog alertDialog = builder.create();
581 alertDialog.setOwnerActivity(ownerActivity);
582
583 return alertDialog;
584 }
585
Alexandre Lisionc51ccb12013-09-11 16:00:30 -0400586 private AlertDialog createAccountDialog() {
587 final Activity ownerActivity = this;
588 AlertDialog.Builder builder = new AlertDialog.Builder(ownerActivity);
589
590 builder.setMessage(getResources().getString(R.string.create_new_account_dialog))
591 .setTitle(getResources().getString(R.string.create_new_account_dialog_title))
592 .setPositiveButton(getResources().getString(android.R.string.ok), new DialogInterface.OnClickListener() {
593 public void onClick(DialogInterface dialog, int whichButton) {
594 Intent in = new Intent();
Alexandre Lision40954dc2013-10-09 15:24:03 -0400595 in.setClass(ownerActivity, AccountWizard.class);
Alexandre Lision4ab53972013-11-04 16:59:18 -0500596 ownerActivity.startActivityForResult(in, HomeActivity.REQUEST_CODE_PREFERENCES);
Alexandre Lisionc51ccb12013-09-11 16:00:30 -0400597 }
598 }).setNegativeButton(getResources().getString(android.R.string.cancel), new DialogInterface.OnClickListener() {
Alexandre Lisionfed2a642014-01-10 12:05:47 -0500599 public void onClick(DialogInterface dialog, int whichButton) {
600 dialog.dismiss();
601 }
602 });
Alexandre Lisionc51ccb12013-09-11 16:00:30 -0400603
604 AlertDialog alertDialog = builder.create();
605 alertDialog.setOwnerActivity(ownerActivity);
606
607 return alertDialog;
608 }
609
alisiond45da712013-05-30 09:18:49 -0400610 @Override
611 public void onContactDragged() {
Alexandre Lision450458a2013-11-22 11:33:12 -0500612 mContactDrawer.collapsePane();
alisiond45da712013-05-30 09:18:49 -0400613 }
614
alision58356b72013-06-03 17:13:36 -0400615 @Override
Alexandre Lision0936ba62013-11-25 10:04:56 -0500616 public void toggleDrawer() {
Alexandre Lisionc985b362013-12-10 17:45:03 -0500617 if (mContactDrawer.isAnchored())
Alexandre Lision0936ba62013-11-25 10:04:56 -0500618 mContactDrawer.expandPane();
Alexandre Lisionc985b362013-12-10 17:45:03 -0500619 else if (!mContactDrawer.isExpanded())
620 mContactDrawer.expandPane(0.3f);
Alexandre Lisionc9b4e9e2013-11-27 10:27:28 -0500621 else
622 mContactDrawer.collapsePane();
alision58356b72013-06-03 17:13:36 -0400623 }
624
alision806e18e2013-06-21 15:30:17 -0400625 @Override
Alexandre Lisiona458ba22013-12-11 15:04:11 -0500626 public void toggleForSearchDrawer() {
627 if (mContactDrawer.isExpanded()) {
628 mContactDrawer.collapsePane();
629 } else
630 mContactDrawer.expandPane();
631 }
632
633 @Override
Alexandre Lision0936ba62013-11-25 10:04:56 -0500634 public void setDragView(RelativeLayout relativeLayout) {
635 mContactDrawer.setDragView(relativeLayout);
636 }
637
Alexandre Lision269e6042013-12-11 16:15:13 -0500638 @Override
639 public void onSectionSelected(int pos) {
Alexandre Lisionf02190d2013-12-12 17:26:12 -0500640
641 mNavigationDrawer.closeDrawers();
Alexandre Lisionfed2a642014-01-10 12:05:47 -0500642
643 switch (pos) {
644 case 0:
645
646 if (fContent instanceof HomeFragment)
647 break;
648
Alexandre Lisionbe54c342014-01-21 17:10:33 -0500649 if (getSupportFragmentManager().getBackStackEntryCount() == 1)
Alexandre Lisionfed2a642014-01-10 12:05:47 -0500650 break;
651
652 popCustomBackStack();
653
654 break;
655 case 1:
656 if (fContent instanceof AccountsManagementFragment)
657 break;
658 fContent = new AccountsManagementFragment();
Alexandre Lisionbe54c342014-01-21 17:10:33 -0500659 getSupportFragmentManager().beginTransaction().setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE).replace(R.id.main_frame, fContent, "Accounts").addToBackStack("Accounts").commit();
Alexandre Lisionfed2a642014-01-10 12:05:47 -0500660 break;
661 case 2:
662 if (fContent instanceof AboutFragment)
663 break;
664 fContent = new AboutFragment();
Alexandre Lisionbe54c342014-01-21 17:10:33 -0500665 getSupportFragmentManager().beginTransaction().setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE).replace(R.id.main_frame, fContent, "About").addToBackStack("About").commit();
Alexandre Lisionfed2a642014-01-10 12:05:47 -0500666 break;
667 }
668
669
Alexandre Lision269e6042013-12-11 16:15:13 -0500670 }
671
alisionf76de3b2013-04-16 15:35:22 -0400672}