blob: 854d4ce5201accd42fb5bce89b0c5c626b7f1e8b [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 Lisione796a872014-01-17 17:08:13 -050055import org.sflphone.history.HistoryEntry;
56import org.sflphone.model.Account;
Alexandre Lision064e1e02013-10-01 16:18:42 -040057import org.sflphone.model.CallContact;
58import org.sflphone.model.Conference;
59import org.sflphone.model.SipCall;
Alexandre Lision064e1e02013-10-01 16:18:42 -040060import org.sflphone.service.ISipService;
61import org.sflphone.service.SipService;
Alexandre Lision450458a2013-11-22 11:33:12 -050062import org.sflphone.views.SlidingUpPanelLayout;
63import org.sflphone.views.SlidingUpPanelLayout.PanelSlideListener;
Alexandre Lision064e1e02013-10-01 16:18:42 -040064
alisionf76de3b2013-04-16 15:35:22 -040065import android.content.ComponentName;
66import android.content.Context;
Alexandre Lisionc51ccb12013-09-11 16:00:30 -040067import android.content.DialogInterface;
alisionf76de3b2013-04-16 15:35:22 -040068import android.content.Intent;
alisionf76de3b2013-04-16 15:35:22 -040069import android.content.ServiceConnection;
Alexandre Lision35577132013-12-06 15:21:15 -050070import android.content.res.AssetManager;
alisionf57d8a62013-07-02 09:37:12 -040071import android.content.res.Configuration;
alision55c36cb2013-06-14 14:57:38 -040072import android.database.Cursor;
Alexandre Lisionc59685a2013-10-04 16:36:36 -040073import android.net.Uri;
alisionf76de3b2013-04-16 15:35:22 -040074import android.os.Bundle;
75import android.os.IBinder;
alision5f899632013-04-22 17:26:56 -040076import android.os.RemoteException;
Alexandre Lision35577132013-12-06 15:21:15 -050077import android.preference.PreferenceManager;
Alexandre Lisionc59685a2013-10-04 16:36:36 -040078import android.provider.ContactsContract;
alision55c36cb2013-06-14 14:57:38 -040079import android.provider.ContactsContract.CommonDataKinds.Phone;
80import android.provider.ContactsContract.CommonDataKinds.SipAddress;
alision9f7a6ec2013-05-24 16:26:26 -040081import android.support.v4.view.GravityCompat;
alision9f7a6ec2013-05-24 16:26:26 -040082import android.support.v4.widget.DrawerLayout;
alisionf76de3b2013-04-16 15:35:22 -040083import android.util.Log;
Alexandre Lision3ccccf02013-10-07 14:10:46 -040084import android.view.Gravity;
alision9f7a6ec2013-05-24 16:26:26 -040085import android.view.MenuItem;
86import android.view.View;
alision7297bdb2013-05-21 11:56:55 -040087import android.widget.RelativeLayout;
alision04a00182013-05-10 17:05:29 -040088import android.widget.Toast;
alisionf76de3b2013-04-16 15:35:22 -040089
Alexandre Lisiona8b78722013-12-13 10:18:33 -050090public class HomeActivity extends FragmentActivity implements DialingFragment.Callbacks, AccountsManagementFragment.Callbacks,
Alexandre Lisiona9ee4eb2014-01-15 16:20:35 -050091 ContactListFragment.Callbacks, CallListFragment.Callbacks, HistoryFragment.Callbacks, MenuFragment.Callbacks {
alisionfde875f2013-05-28 17:01:54 -040092
Alexandre Lisionf02190d2013-12-12 17:26:12 -050093 static final String TAG = HomeActivity.class.getSimpleName();
alisionf76de3b2013-04-16 15:35:22 -040094
alision9f7a6ec2013-05-24 16:26:26 -040095 private ContactListFragment mContactsFragment = null;
alision907bde72013-06-20 14:40:37 -040096 private MenuFragment fMenu;
alisionf76de3b2013-04-16 15:35:22 -040097
alision9f7a6ec2013-05-24 16:26:26 -040098 private boolean mBound = false;
99 private ISipService service;
alisionf76de3b2013-04-16 15:35:22 -0400100
alision2cb99562013-05-30 17:02:20 -0400101 public static final int REQUEST_CODE_PREFERENCES = 1;
Alexandre Lision183bf452014-01-17 11:21:59 -0500102 public static final int REQUEST_CODE_CALL = 3;
alisionf76de3b2013-04-16 15:35:22 -0400103
Alexandre Lision450458a2013-11-22 11:33:12 -0500104 SlidingUpPanelLayout mContactDrawer;
Alexandre Lision68900052013-11-01 11:34:56 -0400105 private DrawerLayout mNavigationDrawer;
alision9f7a6ec2013-05-24 16:26:26 -0400106 private ActionBarDrawerToggle mDrawerToggle;
alisionf76de3b2013-04-16 15:35:22 -0400107
Alexandre Lision9d052a12013-11-06 15:13:34 -0500108 private boolean isClosing = false;
109 private Timer t = new Timer();
alision84813a12013-05-27 17:40:39 -0400110
Alexandre Lisionf02190d2013-12-12 17:26:12 -0500111 private Fragment fContent;
112
alision9f7a6ec2013-05-24 16:26:26 -0400113 /* called before activity is killed, e.g. rotation */
114 @Override
115 protected void onSaveInstanceState(Bundle bundle) {
116 super.onSaveInstanceState(bundle);
alision9f7a6ec2013-05-24 16:26:26 -0400117 getFragmentManager().putFragment(bundle, "ContactsListFragment", mContactsFragment);
118 Log.w(TAG, "onSaveInstanceState()");
119 }
120
121 @Override
122 public void onCreate(Bundle savedInstanceState) {
123 super.onCreate(savedInstanceState);
124
alision907bde72013-06-20 14:40:37 -0400125
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 Lisionb4e60612014-01-14 17:47:23 -0500314 if (getFragmentManager().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 Lisionb4e60612014-01-14 17:47:23 -0500337 FragmentManager.BackStackEntry entry = getFragmentManager().getBackStackEntryAt(0);
338 fContent = getFragmentManager().findFragmentByTag(entry.getName());
339 FragmentManager fm = getFragmentManager();
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 Lisionc59685a2013-10-04 16:36:36 -0400366
alision9f7a6ec2013-05-24 16:26:26 -0400367 Bundle bundle = new Bundle();
Alexandre Lision67c70b42014-01-16 13:57:15 -0500368 Conference tmp = new Conference(Conference.DEFAULT_ID);
alision465ceba2013-07-04 09:24:30 -0400369
alisiondf1dac92013-06-27 17:35:53 -0400370 tmp.getParticipants().add(infos);
alision465ceba2013-07-04 09:24:30 -0400371
alisiondf1dac92013-06-27 17:35:53 -0400372 bundle.putParcelable("conference", tmp);
alision9f7a6ec2013-05-24 16:26:26 -0400373 Intent intent = new Intent().setClass(this, CallActivity.class);
alisiondf1dac92013-06-27 17:35:53 -0400374 intent.putExtra("resuming", false);
alision9f7a6ec2013-05-24 16:26:26 -0400375 intent.putExtras(bundle);
376 startActivityForResult(intent, REQUEST_CODE_CALL);
Alexandre Lisiond588bff2013-10-08 12:43:01 -0400377
Alexandre Lision8b9d8e82013-10-04 09:21:27 -0400378 // overridePendingTransition(R.anim.slide_down, R.anim.slide_up);
alision9f7a6ec2013-05-24 16:26:26 -0400379 }
alisionf76de3b2013-04-16 15:35:22 -0400380
Alexandre Lisionfed2a642014-01-10 12:05:47 -0500381 /**
382 * Defines callbacks for service binding, passed to bindService()
383 */
alision9f7a6ec2013-05-24 16:26:26 -0400384 private ServiceConnection mConnection = new ServiceConnection() {
alision371b77e2013-04-23 14:51:26 -0400385
alision9f7a6ec2013-05-24 16:26:26 -0400386 @Override
387 public void onServiceConnected(ComponentName className, IBinder binder) {
388 service = ISipService.Stub.asInterface(binder);
alisione2a38e12013-04-25 14:20:20 -0400389
alision2cb99562013-05-30 17:02:20 -0400390 try {
alision58356b72013-06-03 17:13:36 -0400391
alision907bde72013-06-20 14:40:37 -0400392 fMenu = new MenuFragment();
Alexandre Lisionf02190d2013-12-12 17:26:12 -0500393 fContent = new HomeFragment();
Alexandre Lision67c70b42014-01-16 13:57:15 -0500394 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 -0400395
alision2cb99562013-05-30 17:02:20 -0400396 service.destroyNotification();
397 } catch (RemoteException e) {
398 Log.e(TAG, e.toString());
399 }
alision9f7a6ec2013-05-24 16:26:26 -0400400 mBound = true;
alision9f7a6ec2013-05-24 16:26:26 -0400401 Log.d(TAG, "Service connected service=" + service);
alision371b77e2013-04-23 14:51:26 -0400402
alision9f7a6ec2013-05-24 16:26:26 -0400403 }
alision5f899632013-04-22 17:26:56 -0400404
alision9f7a6ec2013-05-24 16:26:26 -0400405 @Override
406 public void onServiceDisconnected(ComponentName arg0) {
alision5f899632013-04-22 17:26:56 -0400407
alision9f7a6ec2013-05-24 16:26:26 -0400408 mBound = false;
409 Log.d(TAG, "Service disconnected service=" + service);
410 }
411 };
alision5f899632013-04-22 17:26:56 -0400412
alision9f7a6ec2013-05-24 16:26:26 -0400413 @Override
414 public boolean onOptionsItemSelected(MenuItem item) {
415 Log.i(TAG, "onOptionsItemSelected " + item.getItemId());
alision5f899632013-04-22 17:26:56 -0400416
alision9f7a6ec2013-05-24 16:26:26 -0400417 if (mDrawerToggle.onOptionsItemSelected(item)) {
418 return true;
419 }
alision5f899632013-04-22 17:26:56 -0400420
alision9f7a6ec2013-05-24 16:26:26 -0400421 return super.onOptionsItemSelected(item);
422 }
alisionf76de3b2013-04-16 15:35:22 -0400423
alision9f7a6ec2013-05-24 16:26:26 -0400424 @Override
425 public void onActivityResult(int requestCode, int resultCode, Intent data) {
426 super.onActivityResult(requestCode, resultCode, data);
alision84813a12013-05-27 17:40:39 -0400427
428 switch (requestCode) {
Alexandre Lisionfed2a642014-01-10 12:05:47 -0500429 case REQUEST_CODE_PREFERENCES:
430 case AccountsManagementFragment.ACCOUNT_EDIT_REQUEST:
431 if (fMenu != null)
432 fMenu.updateAllAccounts();
433 break;
434 case REQUEST_CODE_CALL:
435 if (resultCode == CallActivity.RESULT_FAILURE) {
436 Log.w(TAG, "Call Failed");
437 }
438 break;
alision9f7a6ec2013-05-24 16:26:26 -0400439 }
alisionf76de3b2013-04-16 15:35:22 -0400440
alision9f7a6ec2013-05-24 16:26:26 -0400441 }
alisionf76de3b2013-04-16 15:35:22 -0400442
alision9f7a6ec2013-05-24 16:26:26 -0400443 @Override
alision9f7a6ec2013-05-24 16:26:26 -0400444 public ISipService getService() {
445 return service;
446 }
alisionf76de3b2013-04-16 15:35:22 -0400447
Alexandre Lisione1c96db2013-10-04 14:34:21 -0400448 @Override
449 public void onTextContact(final CallContact c) {
450 // TODO
451 }
Alexandre Lisiond588bff2013-10-08 12:43:01 -0400452
Alexandre Lisionc59685a2013-10-04 16:36:36 -0400453 @Override
454 public void onEditContact(final CallContact c) {
455 Intent intent = new Intent(Intent.ACTION_VIEW);
456 Uri uri = Uri.withAppendedPath(ContactsContract.Contacts.CONTENT_URI, String.valueOf(c.getId()));
457 intent.setData(uri);
458 startActivity(intent);
459 }
alision84813a12013-05-27 17:40:39 -0400460
461 @Override
Alexandre Lisione1c96db2013-10-04 14:34:21 -0400462 public void onCallContact(final CallContact c) {
Alexandre Lision8b9d8e82013-10-04 09:21:27 -0400463
Alexandre Lisionf0b993d2013-09-12 13:25:10 -0400464 if (fMenu.getSelectedAccount() == null) {
Alexandre Lisionf0b993d2013-09-12 13:25:10 -0400465 createAccountDialog().show();
466 return;
467 }
alisionfde875f2013-05-28 17:01:54 -0400468
Alexandre Lisiond588bff2013-10-08 12:43:01 -0400469 if (!fMenu.getSelectedAccount().isRegistered()) {
470 createNotRegisteredDialog().show();
471 return;
472 }
473
Alexandre Lisiond16bad92013-10-09 17:16:20 -0400474 getActionBar().show();
alision55c36cb2013-06-14 14:57:38 -0400475 Thread launcher = new Thread(new Runnable() {
alision907bde72013-06-20 14:40:37 -0400476
Alexandre Lisionfed2a642014-01-10 12:05:47 -0500477 final String[] CONTACTS_PHONES_PROJECTION = new String[]{Phone.NUMBER, Phone.TYPE};
478 final String[] CONTACTS_SIP_PROJECTION = new String[]{SipAddress.SIP_ADDRESS, SipAddress.TYPE};
alision907bde72013-06-20 14:40:37 -0400479
alision55c36cb2013-06-14 14:57:38 -0400480 @Override
481 public void run() {
482 SipCall.SipCallBuilder callBuilder = SipCall.SipCallBuilder.getInstance();
483 try {
Alexandre Lision67c70b42014-01-16 13:57:15 -0500484 callBuilder.startCallCreation().setAccount(fMenu.getSelectedAccount()).setCallType(SipCall.direction.CALL_TYPE_OUTGOING);
alision907bde72013-06-20 14:40:37 -0400485 Cursor cPhones = getContentResolver().query(Phone.CONTENT_URI, CONTACTS_PHONES_PROJECTION, Phone.CONTACT_ID + " =" + c.getId(),
486 null, null);
alision55c36cb2013-06-14 14:57:38 -0400487
alision907bde72013-06-20 14:40:37 -0400488 while (cPhones.moveToNext()) {
489 c.addPhoneNumber(cPhones.getString(cPhones.getColumnIndex(Phone.NUMBER)), cPhones.getInt(cPhones.getColumnIndex(Phone.TYPE)));
490 }
491 cPhones.close();
492
493 Cursor cSip = getContentResolver().query(Phone.CONTENT_URI, CONTACTS_SIP_PROJECTION, Phone.CONTACT_ID + "=" + c.getId(), null,
494 null);
495
496 while (cSip.moveToNext()) {
497 c.addSipNumber(cSip.getString(cSip.getColumnIndex(SipAddress.SIP_ADDRESS)), cSip.getInt(cSip.getColumnIndex(SipAddress.TYPE)));
498 }
499 cSip.close();
500 callBuilder.setContact(c);
alision55c36cb2013-06-14 14:57:38 -0400501 launchCallActivity(callBuilder.build());
Alexandre Lision5ed2c972013-10-11 15:36:33 -0400502 } catch (InvalidObjectException e) {
503 e.printStackTrace();
alision55c36cb2013-06-14 14:57:38 -0400504 }
alision907bde72013-06-20 14:40:37 -0400505
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()) {
524 SipCall.SipCallBuilder callBuilder = SipCall.SipCallBuilder.getInstance();
525 callBuilder.startCallCreation().setAccount(usedAccount).setCallType(SipCall.direction.CALL_TYPE_OUTGOING);
526 callBuilder.setContact(to.getContact());
527 try {
528 launchCallActivity(callBuilder.build());
529 } catch (Exception e) {
530 Log.e(TAG, e.toString());
531 }
532 } else {
533 createNotRegisteredDialog().show();
534 }
535 }
536
537 @Override
alision907bde72013-06-20 14:40:37 -0400538 public void onCallDialed(String to) {
alision84813a12013-05-27 17:40:39 -0400539
Alexandre Lisione796a872014-01-17 17:08:13 -0500540
541 Account usedAccount = fMenu.getSelectedAccount();
542
543 if (usedAccount == null) {
Alexandre Lisionc51ccb12013-09-11 16:00:30 -0400544 createAccountDialog().show();
alisiondf1dac92013-06-27 17:35:53 -0400545 return;
546 }
547
Alexandre Lisiond588bff2013-10-08 12:43:01 -0400548 if (fMenu.getSelectedAccount().isRegistered()) {
549 SipCall.SipCallBuilder callBuilder = SipCall.SipCallBuilder.getInstance();
Alexandre Lisione796a872014-01-17 17:08:13 -0500550 callBuilder.startCallCreation().setAccount(usedAccount).setCallType(SipCall.direction.CALL_TYPE_OUTGOING);
Alexandre Lisiond588bff2013-10-08 12:43:01 -0400551 callBuilder.setContact(CallContact.ContactBuilder.buildUnknownContact(to));
alision84813a12013-05-27 17:40:39 -0400552
Alexandre Lisiond588bff2013-10-08 12:43:01 -0400553 try {
554 launchCallActivity(callBuilder.build());
555 } catch (Exception e) {
556 Log.e(TAG, e.toString());
557 }
558 } else {
Alexandre Lision5ed2c972013-10-11 15:36:33 -0400559 createNotRegisteredDialog().show();
alisionfde875f2013-05-28 17:01:54 -0400560 }
alision9f7a6ec2013-05-24 16:26:26 -0400561 }
alision371b77e2013-04-23 14:51:26 -0400562
Alexandre Lisiond588bff2013-10-08 12:43:01 -0400563 private AlertDialog createNotRegisteredDialog() {
564 final Activity ownerActivity = this;
565 AlertDialog.Builder builder = new AlertDialog.Builder(ownerActivity);
566
567 builder.setMessage(getResources().getString(R.string.cannot_pass_sipcall))
568 .setTitle(getResources().getString(R.string.cannot_pass_sipcall_title))
569 .setPositiveButton(getResources().getString(android.R.string.ok), new DialogInterface.OnClickListener() {
570 public void onClick(DialogInterface dialog, int whichButton) {
Alexandre Lision5ed2c972013-10-11 15:36:33 -0400571
Alexandre Lisiond588bff2013-10-08 12:43:01 -0400572 }
573 });
574
575 AlertDialog alertDialog = builder.create();
576 alertDialog.setOwnerActivity(ownerActivity);
577
578 return alertDialog;
579 }
580
Alexandre Lisionc51ccb12013-09-11 16:00:30 -0400581 private AlertDialog createAccountDialog() {
582 final Activity ownerActivity = this;
583 AlertDialog.Builder builder = new AlertDialog.Builder(ownerActivity);
584
585 builder.setMessage(getResources().getString(R.string.create_new_account_dialog))
586 .setTitle(getResources().getString(R.string.create_new_account_dialog_title))
587 .setPositiveButton(getResources().getString(android.R.string.ok), new DialogInterface.OnClickListener() {
588 public void onClick(DialogInterface dialog, int whichButton) {
589 Intent in = new Intent();
Alexandre Lision40954dc2013-10-09 15:24:03 -0400590 in.setClass(ownerActivity, AccountWizard.class);
Alexandre Lision4ab53972013-11-04 16:59:18 -0500591 ownerActivity.startActivityForResult(in, HomeActivity.REQUEST_CODE_PREFERENCES);
Alexandre Lisionc51ccb12013-09-11 16:00:30 -0400592 }
593 }).setNegativeButton(getResources().getString(android.R.string.cancel), new DialogInterface.OnClickListener() {
Alexandre Lisionfed2a642014-01-10 12:05:47 -0500594 public void onClick(DialogInterface dialog, int whichButton) {
595 dialog.dismiss();
596 }
597 });
Alexandre Lisionc51ccb12013-09-11 16:00:30 -0400598
599 AlertDialog alertDialog = builder.create();
600 alertDialog.setOwnerActivity(ownerActivity);
601
602 return alertDialog;
603 }
604
alisiond45da712013-05-30 09:18:49 -0400605 @Override
606 public void onContactDragged() {
Alexandre Lision450458a2013-11-22 11:33:12 -0500607 mContactDrawer.collapsePane();
alisiond45da712013-05-30 09:18:49 -0400608 }
609
alision58356b72013-06-03 17:13:36 -0400610 @Override
Alexandre Lision0936ba62013-11-25 10:04:56 -0500611 public void toggleDrawer() {
Alexandre Lisionc985b362013-12-10 17:45:03 -0500612 if (mContactDrawer.isAnchored())
Alexandre Lision0936ba62013-11-25 10:04:56 -0500613 mContactDrawer.expandPane();
Alexandre Lisionc985b362013-12-10 17:45:03 -0500614 else if (!mContactDrawer.isExpanded())
615 mContactDrawer.expandPane(0.3f);
Alexandre Lisionc9b4e9e2013-11-27 10:27:28 -0500616 else
617 mContactDrawer.collapsePane();
alision58356b72013-06-03 17:13:36 -0400618 }
619
alision806e18e2013-06-21 15:30:17 -0400620 @Override
Alexandre Lisiona458ba22013-12-11 15:04:11 -0500621 public void toggleForSearchDrawer() {
622 if (mContactDrawer.isExpanded()) {
623 mContactDrawer.collapsePane();
624 } else
625 mContactDrawer.expandPane();
626 }
627
628 @Override
Alexandre Lision0936ba62013-11-25 10:04:56 -0500629 public void setDragView(RelativeLayout relativeLayout) {
630 mContactDrawer.setDragView(relativeLayout);
631 }
632
Alexandre Lision269e6042013-12-11 16:15:13 -0500633 @Override
634 public void onSectionSelected(int pos) {
Alexandre Lisionf02190d2013-12-12 17:26:12 -0500635
636 mNavigationDrawer.closeDrawers();
Alexandre Lisionfed2a642014-01-10 12:05:47 -0500637
638 switch (pos) {
639 case 0:
640
641 if (fContent instanceof HomeFragment)
642 break;
643
Alexandre Lisionb4e60612014-01-14 17:47:23 -0500644 if (getFragmentManager().getBackStackEntryCount() == 1)
Alexandre Lisionfed2a642014-01-10 12:05:47 -0500645 break;
646
647 popCustomBackStack();
648
649 break;
650 case 1:
651 if (fContent instanceof AccountsManagementFragment)
652 break;
653 fContent = new AccountsManagementFragment();
Alexandre Lisionb4e60612014-01-14 17:47:23 -0500654 getFragmentManager().beginTransaction().setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE).replace(R.id.main_frame, fContent, "Accounts").addToBackStack("Accounts").commit();
Alexandre Lisionfed2a642014-01-10 12:05:47 -0500655 break;
656 case 2:
657 if (fContent instanceof AboutFragment)
658 break;
659 fContent = new AboutFragment();
Alexandre Lisionb4e60612014-01-14 17:47:23 -0500660 getFragmentManager().beginTransaction().setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE).replace(R.id.main_frame, fContent, "About").addToBackStack("About").commit();
Alexandre Lisionfed2a642014-01-10 12:05:47 -0500661 break;
662 }
663
664
Alexandre Lision269e6042013-12-11 16:15:13 -0500665 }
666
alisionf76de3b2013-04-16 15:35:22 -0400667}