blob: 47ab996e59bb3593ee23a9eb75d5adb7f91a6d5a [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;
55import org.sflphone.interfaces.CallInterface;
Alexandre Lision064e1e02013-10-01 16:18:42 -040056import org.sflphone.model.CallContact;
57import org.sflphone.model.Conference;
58import org.sflphone.model.SipCall;
59import org.sflphone.receivers.CallReceiver;
60import org.sflphone.service.CallManagerCallBack;
61import 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;
alision84813a12013-05-27 17:40:39 -040070import android.content.IntentFilter;
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 Lisionfed2a642014-01-10 12:05:47 -0500104 private 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 Lisionf02190d2013-12-12 17:26:12 -0500113 private Fragment fContent;
114
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);
126
alision907bde72013-06-20 14:40:37 -0400127
Alexandre Lision269e6042013-12-11 16:15:13 -0500128 setContentView(R.layout.activity_home);
alision58356b72013-06-03 17:13:36 -0400129
alision9f7a6ec2013-05-24 16:26:26 -0400130 // Bind to LocalService
131 if (!mBound) {
132 Log.i(TAG, "onStart: Binding service...");
133 Intent intent = new Intent(this, SipService.class);
Alexandre Lision0f550ee2013-10-25 15:34:38 -0400134 startService(intent);
alision9f7a6ec2013-05-24 16:26:26 -0400135 bindService(intent, mConnection, Context.BIND_AUTO_CREATE);
136 }
Alexandre Lision8b9d8e82013-10-04 09:21:27 -0400137
138 if (savedInstanceState != null) {
Alexandre Lision23628c12013-09-24 11:17:05 -0400139 mContactsFragment = (ContactListFragment) getFragmentManager().getFragment(savedInstanceState, "ContactsListFragment");
140 }
alision9f7a6ec2013-05-24 16:26:26 -0400141 if (mContactsFragment == null) {
142 mContactsFragment = new ContactListFragment();
alision9f7a6ec2013-05-24 16:26:26 -0400143 getFragmentManager().beginTransaction().replace(R.id.contacts_frame, mContactsFragment).commit();
144 }
alision2ec64f92013-06-17 17:28:58 -0400145
Alexandre Lision450458a2013-11-22 11:33:12 -0500146 mContactDrawer = (SlidingUpPanelLayout) findViewById(R.id.contact_panel);
147 // mContactDrawer.setShadowDrawable(getResources().getDrawable(R.drawable.above_shadow));
148 mContactDrawer.setAnchorPoint(0.3f);
149 mContactDrawer.setDragView(findViewById(R.id.contacts_frame));
150 mContactDrawer.setEnableDragViewTouchEvents(true);
Alexandre Lision450458a2013-11-22 11:33:12 -0500151 mContactDrawer.setPanelSlideListener(new PanelSlideListener() {
Alexandre Lision8b9d8e82013-10-04 09:21:27 -0400152
153 @Override
Alexandre Lision450458a2013-11-22 11:33:12 -0500154 public void onPanelSlide(View panel, float slideOffset) {
155 if (slideOffset < 0.2) {
156 if (getActionBar().isShowing()) {
157 getActionBar().hide();
158 }
159 } else {
160 if (!getActionBar().isShowing()) {
161 getActionBar().show();
162 }
Alexandre Lision8b9d8e82013-10-04 09:21:27 -0400163 }
164 }
Alexandre Lision450458a2013-11-22 11:33:12 -0500165
166 @Override
167 public void onPanelExpanded(View panel) {
168
169 }
170
171 @Override
172 public void onPanelCollapsed(View panel) {
173
174 }
175
176 @Override
177 public void onPanelAnchored(View panel) {
178
179 }
Alexandre Lision8b9d8e82013-10-04 09:21:27 -0400180 });
Alexandre Lisiond588bff2013-10-08 12:43:01 -0400181
Alexandre Lision68900052013-11-01 11:34:56 -0400182 mNavigationDrawer = (DrawerLayout) findViewById(R.id.drawer_layout);
alisionf76de3b2013-04-16 15:35:22 -0400183
alision0b553832013-05-30 09:31:06 -0400184 // set a custom shadow that overlays the main content when the drawer opens
Alexandre Lision68900052013-11-01 11:34:56 -0400185 mNavigationDrawer.setDrawerShadow(R.drawable.drawer_shadow, GravityCompat.START);
alisionf76de3b2013-04-16 15:35:22 -0400186
alision9f7a6ec2013-05-24 16:26:26 -0400187 getActionBar().setDisplayHomeAsUpEnabled(true);
188 getActionBar().setHomeButtonEnabled(true);
alisionf76de3b2013-04-16 15:35:22 -0400189
alision9f7a6ec2013-05-24 16:26:26 -0400190 mDrawerToggle = new ActionBarDrawerToggle(this, /* host Activity */
Alexandre Lisionfed2a642014-01-10 12:05:47 -0500191 mNavigationDrawer, /* DrawerLayout object */
192 R.drawable.ic_drawer, /* nav drawer image to replace 'Up' caret */
193 R.string.drawer_open, /* "open drawer" description for accessibility */
194 R.string.drawer_close /* "close drawer" description for accessibility */
alision9f7a6ec2013-05-24 16:26:26 -0400195 ) {
alision0b553832013-05-30 09:31:06 -0400196 @Override
alision9f7a6ec2013-05-24 16:26:26 -0400197 public void onDrawerClosed(View view) {
alision9f7a6ec2013-05-24 16:26:26 -0400198 invalidateOptionsMenu(); // creates call to onPrepareOptionsMenu()
199 }
alisionf76de3b2013-04-16 15:35:22 -0400200
alision0b553832013-05-30 09:31:06 -0400201 @Override
alision9f7a6ec2013-05-24 16:26:26 -0400202 public void onDrawerOpened(View drawerView) {
Alexandre Lision4d20c812013-12-12 17:44:50 -0500203 // getActionBar().setTitle(mDrawerTitle);
alision9f7a6ec2013-05-24 16:26:26 -0400204 invalidateOptionsMenu(); // creates call to onPrepareOptionsMenu()
205 }
206 };
alisionf76de3b2013-04-16 15:35:22 -0400207
Alexandre Lision68900052013-11-01 11:34:56 -0400208 mNavigationDrawer.setDrawerListener(mDrawerToggle);
alisionf76de3b2013-04-16 15:35:22 -0400209
alision9f7a6ec2013-05-24 16:26:26 -0400210 }
alisionf76de3b2013-04-16 15:35:22 -0400211
alisionf57d8a62013-07-02 09:37:12 -0400212 @Override
213 protected void onPostCreate(Bundle savedInstanceState) {
214 super.onPostCreate(savedInstanceState);
215 // Sync the toggle state after onRestoreInstanceState has occurred.
216 mDrawerToggle.syncState();
Alexandre Lision450458a2013-11-22 11:33:12 -0500217 if (mContactDrawer.isExpanded()) {
Alexandre Lisiond16bad92013-10-09 17:16:20 -0400218 getActionBar().hide();
219 }
alisionf57d8a62013-07-02 09:37:12 -0400220 }
alision465ceba2013-07-04 09:24:30 -0400221
alisionf57d8a62013-07-02 09:37:12 -0400222 @Override
223 public void onConfigurationChanged(Configuration newConfig) {
224 super.onConfigurationChanged(newConfig);
225 mDrawerToggle.onConfigurationChanged(newConfig);
226 }
alision3c5c2a92013-05-30 09:50:47 -0400227
alision9f7a6ec2013-05-24 16:26:26 -0400228 @Override
229 protected void onStart() {
230 Log.i(TAG, "onStart");
Alexandre Lision35577132013-12-06 15:21:15 -0500231
232 if (!PreferenceManager.getDefaultSharedPreferences(getApplicationContext()).getBoolean("installed", false)) {
233 PreferenceManager.getDefaultSharedPreferences(getApplicationContext()).edit().putBoolean("installed", true).commit();
234
235 copyAssetFolder(getAssets(), "ringtones", getFilesDir().getAbsolutePath() + "/ringtones");
236 }
237
alision9f7a6ec2013-05-24 16:26:26 -0400238 super.onStart();
Alexandre Lisionc51ccb12013-09-11 16:00:30 -0400239
alision9f7a6ec2013-05-24 16:26:26 -0400240 }
alisionf76de3b2013-04-16 15:35:22 -0400241
Alexandre Lision35577132013-12-06 15:21:15 -0500242 private static boolean copyAssetFolder(AssetManager assetManager, String fromAssetPath, String toPath) {
243 try {
244 String[] files = assetManager.list(fromAssetPath);
245 new File(toPath).mkdirs();
246 Log.i(TAG, "Creating :" + toPath);
247 boolean res = true;
248 for (String file : files)
249 if (file.contains(".")) {
250 Log.i(TAG, "Copying file :" + fromAssetPath + "/" + file + " to " + toPath + "/" + file);
251 res &= copyAsset(assetManager, fromAssetPath + "/" + file, toPath + "/" + file);
252 } else {
253 Log.i(TAG, "Copying folder :" + fromAssetPath + "/" + file + " to " + toPath + "/" + file);
254 res &= copyAssetFolder(assetManager, fromAssetPath + "/" + file, toPath + "/" + file);
255 }
256 return res;
257 } catch (Exception e) {
258 e.printStackTrace();
259 return false;
260 }
261 }
262
263 private static boolean copyAsset(AssetManager assetManager, String fromAssetPath, String toPath) {
264 InputStream in = null;
265 OutputStream out = null;
266 try {
267 in = assetManager.open(fromAssetPath);
268 new File(toPath).createNewFile();
269 out = new FileOutputStream(toPath);
270 copyFile(in, out);
271 in.close();
272 in = null;
273 out.flush();
274 out.close();
275 out = null;
276 return true;
277 } catch (Exception e) {
278 e.printStackTrace();
279 return false;
280 }
281 }
282
283 private static void copyFile(InputStream in, OutputStream out) throws IOException {
284 byte[] buffer = new byte[1024];
285 int read;
286 while ((read = in.read(buffer)) != -1) {
287 out.write(buffer, 0, read);
288 }
289 }
290
alision9f7a6ec2013-05-24 16:26:26 -0400291 /* user gets back to the activity, e.g. through task manager */
292 @Override
293 protected void onRestart() {
294 super.onRestart();
295 }
alisionf76de3b2013-04-16 15:35:22 -0400296
alision9f7a6ec2013-05-24 16:26:26 -0400297 /* activity gets back to the foreground and user input */
298 @Override
299 protected void onResume() {
alision58356b72013-06-03 17:13:36 -0400300 super.onResume();
alision9f7a6ec2013-05-24 16:26:26 -0400301 }
alision907bde72013-06-20 14:40:37 -0400302
alisionbd45a822013-06-06 17:35:35 -0400303 @Override
304 public void onBackPressed() {
Alexandre Lisiond588bff2013-10-08 12:43:01 -0400305
Alexandre Lision68900052013-11-01 11:34:56 -0400306 if (mNavigationDrawer.isDrawerVisible(Gravity.LEFT)) {
307 mNavigationDrawer.closeDrawer(Gravity.LEFT);
Alexandre Lision68855472013-10-10 16:20:46 -0400308 return;
Alexandre Lision3ccccf02013-10-07 14:10:46 -0400309 }
alision907bde72013-06-20 14:40:37 -0400310
Alexandre Lision450458a2013-11-22 11:33:12 -0500311 if (mContactDrawer.isExpanded() || mContactDrawer.isAnchored()) {
312 mContactDrawer.collapsePane();
alisionbd45a822013-06-06 17:35:35 -0400313 return;
314 }
alision2ec64f92013-06-17 17:28:58 -0400315
Alexandre Lisionb4e60612014-01-14 17:47:23 -0500316 if (getFragmentManager().getBackStackEntryCount() > 1) {
Alexandre Lisiond5934b52014-01-07 17:17:37 -0500317 popCustomBackStack();
Alexandre Lisionfc9b4e72013-12-13 14:55:25 -0500318 fMenu.backToHome();
Alexandre Lisionf02190d2013-12-12 17:26:12 -0500319 return;
320 }
321
alisionbd45a822013-06-06 17:35:35 -0400322 if (isClosing) {
323 super.onBackPressed();
324 t.cancel();
325 finish();
326 } else {
alisionbd45a822013-06-06 17:35:35 -0400327 t.schedule(new TimerTask() {
328 @Override
329 public void run() {
330 isClosing = false;
331 }
332 }, 3000);
333 Toast.makeText(this, getResources().getString(R.string.close_msg), Toast.LENGTH_SHORT).show();
334 isClosing = true;
335 }
336 }
alisione2a38e12013-04-25 14:20:20 -0400337
Alexandre Lisiond5934b52014-01-07 17:17:37 -0500338 private void popCustomBackStack() {
Alexandre Lisionb4e60612014-01-14 17:47:23 -0500339 FragmentManager.BackStackEntry entry = getFragmentManager().getBackStackEntryAt(0);
340 fContent = getFragmentManager().findFragmentByTag(entry.getName());
341 FragmentManager fm = getFragmentManager();
Alexandre Lisionfed2a642014-01-10 12:05:47 -0500342 for (int i = 0; i < fm.getBackStackEntryCount() - 1; ++i) {
Alexandre Lisiond5934b52014-01-07 17:17:37 -0500343 fm.popBackStack();
344 }
345 }
346
alision9f7a6ec2013-05-24 16:26:26 -0400347 /* activity no more in foreground */
348 @Override
349 protected void onPause() {
350 super.onPause();
Alexandre Lisiona9ee4eb2014-01-15 16:20:35 -0500351
Alexandre Lisionb4a9e392013-10-01 14:39:43 -0400352 if (mBound) {
353 unbindService(mConnection);
354 mBound = false;
355 }
alision9f7a6ec2013-05-24 16:26:26 -0400356 }
alisionf76de3b2013-04-16 15:35:22 -0400357
alision9f7a6ec2013-05-24 16:26:26 -0400358 /* activity finishes itself or is being killed by the system */
359 @Override
360 protected void onDestroy() {
alision9f7a6ec2013-05-24 16:26:26 -0400361 super.onDestroy();
Alexandre Lisionb4a9e392013-10-01 14:39:43 -0400362 Log.i(TAG, "onDestroy: destroying service...");
363 Intent sipServiceIntent = new Intent(this, SipService.class);
364 stopService(sipServiceIntent);
alision9f7a6ec2013-05-24 16:26:26 -0400365 }
alision73424b62013-04-26 11:49:18 -0400366
alision55c36cb2013-06-14 14:57:38 -0400367 public void launchCallActivity(SipCall infos) {
Alexandre Lisionc59685a2013-10-04 16:36:36 -0400368
alision9f7a6ec2013-05-24 16:26:26 -0400369 Bundle bundle = new Bundle();
alisiondf1dac92013-06-27 17:35:53 -0400370 Conference tmp = new Conference("-1");
alision465ceba2013-07-04 09:24:30 -0400371
alisiondf1dac92013-06-27 17:35:53 -0400372 tmp.getParticipants().add(infos);
alision465ceba2013-07-04 09:24:30 -0400373
alisiondf1dac92013-06-27 17:35:53 -0400374 bundle.putParcelable("conference", tmp);
alision9f7a6ec2013-05-24 16:26:26 -0400375 Intent intent = new Intent().setClass(this, CallActivity.class);
alisiondf1dac92013-06-27 17:35:53 -0400376 intent.putExtra("resuming", false);
alision9f7a6ec2013-05-24 16:26:26 -0400377 intent.putExtras(bundle);
378 startActivityForResult(intent, REQUEST_CODE_CALL);
Alexandre Lisiond588bff2013-10-08 12:43:01 -0400379
Alexandre Lision8b9d8e82013-10-04 09:21:27 -0400380 // overridePendingTransition(R.anim.slide_down, R.anim.slide_up);
alision9f7a6ec2013-05-24 16:26:26 -0400381 }
alisionf76de3b2013-04-16 15:35:22 -0400382
Alexandre Lisionfed2a642014-01-10 12:05:47 -0500383 /**
384 * Defines callbacks for service binding, passed to bindService()
385 */
alision9f7a6ec2013-05-24 16:26:26 -0400386 private ServiceConnection mConnection = new ServiceConnection() {
alision371b77e2013-04-23 14:51:26 -0400387
alision9f7a6ec2013-05-24 16:26:26 -0400388 @Override
389 public void onServiceConnected(ComponentName className, IBinder binder) {
390 service = ISipService.Stub.asInterface(binder);
alisione2a38e12013-04-25 14:20:20 -0400391
alision2cb99562013-05-30 17:02:20 -0400392 try {
alision58356b72013-06-03 17:13:36 -0400393
alision907bde72013-06-20 14:40:37 -0400394 fMenu = new MenuFragment();
Alexandre Lisionf02190d2013-12-12 17:26:12 -0500395 fContent = new HomeFragment();
Alexandre Lisionb4e60612014-01-14 17:47:23 -0500396 getFragmentManager().beginTransaction().replace(R.id.left_drawer, fMenu).commit();
397 getFragmentManager().beginTransaction().replace(R.id.main_frame, fContent, "Home").addToBackStack("Home").commit();
Alexandre Lisionc51ccb12013-09-11 16:00:30 -0400398
alision2cb99562013-05-30 17:02:20 -0400399 service.destroyNotification();
400 } catch (RemoteException e) {
401 Log.e(TAG, e.toString());
402 }
alision9f7a6ec2013-05-24 16:26:26 -0400403 mBound = true;
alision9f7a6ec2013-05-24 16:26:26 -0400404 Log.d(TAG, "Service connected service=" + service);
alision371b77e2013-04-23 14:51:26 -0400405
alision9f7a6ec2013-05-24 16:26:26 -0400406 }
alision5f899632013-04-22 17:26:56 -0400407
alision9f7a6ec2013-05-24 16:26:26 -0400408 @Override
409 public void onServiceDisconnected(ComponentName arg0) {
alision5f899632013-04-22 17:26:56 -0400410
alision9f7a6ec2013-05-24 16:26:26 -0400411 mBound = false;
412 Log.d(TAG, "Service disconnected service=" + service);
413 }
414 };
alision5f899632013-04-22 17:26:56 -0400415
alision9f7a6ec2013-05-24 16:26:26 -0400416 @Override
417 public boolean onOptionsItemSelected(MenuItem item) {
418 Log.i(TAG, "onOptionsItemSelected " + item.getItemId());
alision5f899632013-04-22 17:26:56 -0400419
alision9f7a6ec2013-05-24 16:26:26 -0400420 if (mDrawerToggle.onOptionsItemSelected(item)) {
421 return true;
422 }
alision5f899632013-04-22 17:26:56 -0400423
alision9f7a6ec2013-05-24 16:26:26 -0400424 return super.onOptionsItemSelected(item);
425 }
alisionf76de3b2013-04-16 15:35:22 -0400426
alision9f7a6ec2013-05-24 16:26:26 -0400427 @Override
428 public void onActivityResult(int requestCode, int resultCode, Intent data) {
429 super.onActivityResult(requestCode, resultCode, data);
alision84813a12013-05-27 17:40:39 -0400430
431 switch (requestCode) {
Alexandre Lisionfed2a642014-01-10 12:05:47 -0500432 case REQUEST_CODE_PREFERENCES:
433 case AccountsManagementFragment.ACCOUNT_EDIT_REQUEST:
434 if (fMenu != null)
435 fMenu.updateAllAccounts();
436 break;
437 case REQUEST_CODE_CALL:
438 if (resultCode == CallActivity.RESULT_FAILURE) {
439 Log.w(TAG, "Call Failed");
440 }
441 break;
alision9f7a6ec2013-05-24 16:26:26 -0400442 }
alisionf76de3b2013-04-16 15:35:22 -0400443
alision9f7a6ec2013-05-24 16:26:26 -0400444 }
alisionf76de3b2013-04-16 15:35:22 -0400445
alision9f7a6ec2013-05-24 16:26:26 -0400446 @Override
alision9f7a6ec2013-05-24 16:26:26 -0400447 public ISipService getService() {
448 return service;
449 }
alisionf76de3b2013-04-16 15:35:22 -0400450
Alexandre Lisione1c96db2013-10-04 14:34:21 -0400451 @Override
452 public void onTextContact(final CallContact c) {
453 // TODO
454 }
Alexandre Lisiond588bff2013-10-08 12:43:01 -0400455
Alexandre Lisionc59685a2013-10-04 16:36:36 -0400456 @Override
457 public void onEditContact(final CallContact c) {
458 Intent intent = new Intent(Intent.ACTION_VIEW);
459 Uri uri = Uri.withAppendedPath(ContactsContract.Contacts.CONTENT_URI, String.valueOf(c.getId()));
460 intent.setData(uri);
461 startActivity(intent);
462 }
alision84813a12013-05-27 17:40:39 -0400463
464 @Override
Alexandre Lisione1c96db2013-10-04 14:34:21 -0400465 public void onCallContact(final CallContact c) {
Alexandre Lision8b9d8e82013-10-04 09:21:27 -0400466
Alexandre Lisionf0b993d2013-09-12 13:25:10 -0400467 if (fMenu.getSelectedAccount() == null) {
Alexandre Lisionf0b993d2013-09-12 13:25:10 -0400468 createAccountDialog().show();
469 return;
470 }
alisionfde875f2013-05-28 17:01:54 -0400471
Alexandre Lisiond588bff2013-10-08 12:43:01 -0400472 if (!fMenu.getSelectedAccount().isRegistered()) {
473 createNotRegisteredDialog().show();
474 return;
475 }
476
Alexandre Lisiond16bad92013-10-09 17:16:20 -0400477 getActionBar().show();
alision55c36cb2013-06-14 14:57:38 -0400478 Thread launcher = new Thread(new Runnable() {
alision907bde72013-06-20 14:40:37 -0400479
Alexandre Lisionfed2a642014-01-10 12:05:47 -0500480 final String[] CONTACTS_PHONES_PROJECTION = new String[]{Phone.NUMBER, Phone.TYPE};
481 final String[] CONTACTS_SIP_PROJECTION = new String[]{SipAddress.SIP_ADDRESS, SipAddress.TYPE};
alision907bde72013-06-20 14:40:37 -0400482
alision55c36cb2013-06-14 14:57:38 -0400483 @Override
484 public void run() {
485 SipCall.SipCallBuilder callBuilder = SipCall.SipCallBuilder.getInstance();
486 try {
Alexandre Lisiond588bff2013-10-08 12:43:01 -0400487 callBuilder.startCallCreation().setAccount(fMenu.getSelectedAccount()).setCallType(SipCall.state.CALL_TYPE_OUTGOING);
alision907bde72013-06-20 14:40:37 -0400488 Cursor cPhones = getContentResolver().query(Phone.CONTENT_URI, CONTACTS_PHONES_PROJECTION, Phone.CONTACT_ID + " =" + c.getId(),
489 null, null);
alision55c36cb2013-06-14 14:57:38 -0400490
alision907bde72013-06-20 14:40:37 -0400491 while (cPhones.moveToNext()) {
492 c.addPhoneNumber(cPhones.getString(cPhones.getColumnIndex(Phone.NUMBER)), cPhones.getInt(cPhones.getColumnIndex(Phone.TYPE)));
493 }
494 cPhones.close();
495
496 Cursor cSip = getContentResolver().query(Phone.CONTENT_URI, CONTACTS_SIP_PROJECTION, Phone.CONTACT_ID + "=" + c.getId(), null,
497 null);
498
499 while (cSip.moveToNext()) {
500 c.addSipNumber(cSip.getString(cSip.getColumnIndex(SipAddress.SIP_ADDRESS)), cSip.getInt(cSip.getColumnIndex(SipAddress.TYPE)));
501 }
502 cSip.close();
503 callBuilder.setContact(c);
alision55c36cb2013-06-14 14:57:38 -0400504 launchCallActivity(callBuilder.build());
Alexandre Lision5ed2c972013-10-11 15:36:33 -0400505 } catch (InvalidObjectException e) {
506 e.printStackTrace();
alision55c36cb2013-06-14 14:57:38 -0400507 }
alision907bde72013-06-20 14:40:37 -0400508
alision55c36cb2013-06-14 14:57:38 -0400509 }
510 });
511 launcher.start();
Alexandre Lision450458a2013-11-22 11:33:12 -0500512 mContactDrawer.collapsePane();
alision84813a12013-05-27 17:40:39 -0400513
514 }
515
516 @Override
alision907bde72013-06-20 14:40:37 -0400517 public void onCallDialed(String to) {
alision84813a12013-05-27 17:40:39 -0400518
alisiondf1dac92013-06-27 17:35:53 -0400519 if (fMenu.getSelectedAccount() == null) {
Alexandre Lisionc51ccb12013-09-11 16:00:30 -0400520 createAccountDialog().show();
alisiondf1dac92013-06-27 17:35:53 -0400521 return;
522 }
523
Alexandre Lisiond588bff2013-10-08 12:43:01 -0400524 if (fMenu.getSelectedAccount().isRegistered()) {
525 SipCall.SipCallBuilder callBuilder = SipCall.SipCallBuilder.getInstance();
526 callBuilder.startCallCreation().setAccount(fMenu.getSelectedAccount()).setCallType(SipCall.state.CALL_TYPE_OUTGOING);
527 callBuilder.setContact(CallContact.ContactBuilder.buildUnknownContact(to));
alision84813a12013-05-27 17:40:39 -0400528
Alexandre Lisiond588bff2013-10-08 12:43:01 -0400529 try {
530 launchCallActivity(callBuilder.build());
531 } catch (Exception e) {
532 Log.e(TAG, e.toString());
533 }
534 } else {
Alexandre Lision5ed2c972013-10-11 15:36:33 -0400535 createNotRegisteredDialog().show();
alisionfde875f2013-05-28 17:01:54 -0400536 }
alision84813a12013-05-27 17:40:39 -0400537
alision9f7a6ec2013-05-24 16:26:26 -0400538 }
alision371b77e2013-04-23 14:51:26 -0400539
Alexandre Lisiond588bff2013-10-08 12:43:01 -0400540 private AlertDialog createNotRegisteredDialog() {
541 final Activity ownerActivity = this;
542 AlertDialog.Builder builder = new AlertDialog.Builder(ownerActivity);
543
544 builder.setMessage(getResources().getString(R.string.cannot_pass_sipcall))
545 .setTitle(getResources().getString(R.string.cannot_pass_sipcall_title))
546 .setPositiveButton(getResources().getString(android.R.string.ok), new DialogInterface.OnClickListener() {
547 public void onClick(DialogInterface dialog, int whichButton) {
Alexandre Lision5ed2c972013-10-11 15:36:33 -0400548
Alexandre Lisiond588bff2013-10-08 12:43:01 -0400549 }
550 });
551
552 AlertDialog alertDialog = builder.create();
553 alertDialog.setOwnerActivity(ownerActivity);
554
555 return alertDialog;
556 }
557
Alexandre Lisionc51ccb12013-09-11 16:00:30 -0400558 private AlertDialog createAccountDialog() {
559 final Activity ownerActivity = this;
560 AlertDialog.Builder builder = new AlertDialog.Builder(ownerActivity);
561
562 builder.setMessage(getResources().getString(R.string.create_new_account_dialog))
563 .setTitle(getResources().getString(R.string.create_new_account_dialog_title))
564 .setPositiveButton(getResources().getString(android.R.string.ok), new DialogInterface.OnClickListener() {
565 public void onClick(DialogInterface dialog, int whichButton) {
566 Intent in = new Intent();
Alexandre Lision40954dc2013-10-09 15:24:03 -0400567 in.setClass(ownerActivity, AccountWizard.class);
Alexandre Lision4ab53972013-11-04 16:59:18 -0500568 ownerActivity.startActivityForResult(in, HomeActivity.REQUEST_CODE_PREFERENCES);
Alexandre Lisionc51ccb12013-09-11 16:00:30 -0400569 }
570 }).setNegativeButton(getResources().getString(android.R.string.cancel), new DialogInterface.OnClickListener() {
Alexandre Lisionfed2a642014-01-10 12:05:47 -0500571 public void onClick(DialogInterface dialog, int whichButton) {
572 dialog.dismiss();
573 }
574 });
Alexandre Lisionc51ccb12013-09-11 16:00:30 -0400575
576 AlertDialog alertDialog = builder.create();
577 alertDialog.setOwnerActivity(ownerActivity);
578
579 return alertDialog;
580 }
581
alisiond45da712013-05-30 09:18:49 -0400582 @Override
583 public void onContactDragged() {
Alexandre Lision450458a2013-11-22 11:33:12 -0500584 mContactDrawer.collapsePane();
alisiond45da712013-05-30 09:18:49 -0400585 }
586
alision58356b72013-06-03 17:13:36 -0400587 @Override
Alexandre Lision0936ba62013-11-25 10:04:56 -0500588 public void toggleDrawer() {
Alexandre Lisionc985b362013-12-10 17:45:03 -0500589 if (mContactDrawer.isAnchored())
Alexandre Lision0936ba62013-11-25 10:04:56 -0500590 mContactDrawer.expandPane();
Alexandre Lisionc985b362013-12-10 17:45:03 -0500591 else if (!mContactDrawer.isExpanded())
592 mContactDrawer.expandPane(0.3f);
Alexandre Lisionc9b4e9e2013-11-27 10:27:28 -0500593 else
594 mContactDrawer.collapsePane();
alision58356b72013-06-03 17:13:36 -0400595 }
596
alision806e18e2013-06-21 15:30:17 -0400597 @Override
Alexandre Lisiona458ba22013-12-11 15:04:11 -0500598 public void toggleForSearchDrawer() {
599 if (mContactDrawer.isExpanded()) {
600 mContactDrawer.collapsePane();
601 } else
602 mContactDrawer.expandPane();
603 }
604
605 @Override
Alexandre Lision1a9e3b12013-09-16 11:06:07 -0400606 public void selectedCall(Conference c) {
alisiondf1dac92013-06-27 17:35:53 -0400607 Intent intent = new Intent().setClass(this, CallActivity.class);
608 intent.putExtra("resuming", true);
Alexandre Lision1a9e3b12013-09-16 11:06:07 -0400609 intent.putExtra("conference", c);
alisiondf1dac92013-06-27 17:35:53 -0400610 startActivityForResult(intent, REQUEST_CODE_CALL);
alision806e18e2013-06-21 15:30:17 -0400611 }
Alexandre Lision8b9d8e82013-10-04 09:21:27 -0400612
Alexandre Lision0936ba62013-11-25 10:04:56 -0500613 @Override
614 public void setDragView(RelativeLayout relativeLayout) {
615 mContactDrawer.setDragView(relativeLayout);
616 }
617
Alexandre Lision269e6042013-12-11 16:15:13 -0500618 @Override
619 public void onSectionSelected(int pos) {
Alexandre Lisionf02190d2013-12-12 17:26:12 -0500620
621 mNavigationDrawer.closeDrawers();
Alexandre Lisionfed2a642014-01-10 12:05:47 -0500622
623 switch (pos) {
624 case 0:
625
626 if (fContent instanceof HomeFragment)
627 break;
628
Alexandre Lisionb4e60612014-01-14 17:47:23 -0500629 if (getFragmentManager().getBackStackEntryCount() == 1)
Alexandre Lisionfed2a642014-01-10 12:05:47 -0500630 break;
631
632 popCustomBackStack();
633
634 break;
635 case 1:
636 if (fContent instanceof AccountsManagementFragment)
637 break;
638 fContent = new AccountsManagementFragment();
Alexandre Lisionb4e60612014-01-14 17:47:23 -0500639 getFragmentManager().beginTransaction().setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE).replace(R.id.main_frame, fContent, "Accounts").addToBackStack("Accounts").commit();
Alexandre Lisionfed2a642014-01-10 12:05:47 -0500640 break;
641 case 2:
642 if (fContent instanceof AboutFragment)
643 break;
644 fContent = new AboutFragment();
Alexandre Lisionb4e60612014-01-14 17:47:23 -0500645 getFragmentManager().beginTransaction().setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE).replace(R.id.main_frame, fContent, "About").addToBackStack("About").commit();
Alexandre Lisionfed2a642014-01-10 12:05:47 -0500646 break;
647 }
648
649
Alexandre Lision269e6042013-12-11 16:15:13 -0500650 }
651
alisionf76de3b2013-04-16 15:35:22 -0400652}