blob: f9f12c907cf5dd76a19e19df6b01b36f070d0384 [file] [log] [blame]
alisionf76de3b2013-04-16 15:35:22 -04001/*
alision2ec64f92013-06-17 17:28:58 -04002 * Copyright (C) 2004-2013 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 Lision064e1e02013-10-01 16:18:42 -040043import org.sflphone.R;
Alexandre Lisionf02190d2013-12-12 17:26:12 -050044import org.sflphone.fragments.AboutFragment;
45import org.sflphone.fragments.AccountsManagementFragment;
46import org.sflphone.fragments.CallListFragment;
Alexandre Lision064e1e02013-10-01 16:18:42 -040047import org.sflphone.fragments.ContactListFragment;
48import org.sflphone.fragments.DialingFragment;
49import org.sflphone.fragments.HistoryFragment;
50import org.sflphone.fragments.HomeFragment;
51import org.sflphone.fragments.MenuFragment;
52import org.sflphone.interfaces.CallInterface;
Alexandre Lision064e1e02013-10-01 16:18:42 -040053import org.sflphone.model.CallContact;
54import org.sflphone.model.Conference;
55import org.sflphone.model.SipCall;
56import org.sflphone.receivers.CallReceiver;
57import org.sflphone.service.CallManagerCallBack;
58import org.sflphone.service.ISipService;
59import org.sflphone.service.SipService;
Alexandre Lision450458a2013-11-22 11:33:12 -050060import org.sflphone.views.SlidingUpPanelLayout;
61import org.sflphone.views.SlidingUpPanelLayout.PanelSlideListener;
Alexandre Lision064e1e02013-10-01 16:18:42 -040062
alisionf76de3b2013-04-16 15:35:22 -040063import android.app.Activity;
Alexandre Lisionc51ccb12013-09-11 16:00:30 -040064import android.app.AlertDialog;
Alexandre Lisionf02190d2013-12-12 17:26:12 -050065import android.app.Fragment;
66import android.app.FragmentManager.BackStackEntry;
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;
alision84813a12013-05-27 17:40:39 -040071import android.content.IntentFilter;
alisionf76de3b2013-04-16 15:35:22 -040072import android.content.ServiceConnection;
Alexandre Lision35577132013-12-06 15:21:15 -050073import android.content.res.AssetManager;
alisionf57d8a62013-07-02 09:37:12 -040074import android.content.res.Configuration;
alision55c36cb2013-06-14 14:57:38 -040075import android.database.Cursor;
Alexandre Lisionc59685a2013-10-04 16:36:36 -040076import android.net.Uri;
alisionf76de3b2013-04-16 15:35:22 -040077import android.os.Bundle;
78import android.os.IBinder;
alision5f899632013-04-22 17:26:56 -040079import android.os.RemoteException;
Alexandre Lision35577132013-12-06 15:21:15 -050080import android.preference.PreferenceManager;
Alexandre Lisionc59685a2013-10-04 16:36:36 -040081import android.provider.ContactsContract;
alision55c36cb2013-06-14 14:57:38 -040082import android.provider.ContactsContract.CommonDataKinds.Phone;
83import android.provider.ContactsContract.CommonDataKinds.SipAddress;
alision9f7a6ec2013-05-24 16:26:26 -040084import android.support.v4.app.ActionBarDrawerToggle;
85import android.support.v4.view.GravityCompat;
alision9f7a6ec2013-05-24 16:26:26 -040086import android.support.v4.widget.DrawerLayout;
alisionf76de3b2013-04-16 15:35:22 -040087import android.util.Log;
Alexandre Lision3ccccf02013-10-07 14:10:46 -040088import android.view.Gravity;
alision9f7a6ec2013-05-24 16:26:26 -040089import android.view.MenuItem;
90import android.view.View;
alision7297bdb2013-05-21 11:56:55 -040091import android.widget.RelativeLayout;
alision04a00182013-05-10 17:05:29 -040092import android.widget.Toast;
alisionf76de3b2013-04-16 15:35:22 -040093
Alexandre Lisionf02190d2013-12-12 17:26:12 -050094public class HomeActivity extends Activity implements DialingFragment.Callbacks, AccountsManagementFragment.Callbacks, ContactListFragment.Callbacks,
95 CallListFragment.Callbacks, HistoryFragment.Callbacks, CallInterface, MenuFragment.Callbacks {
alisionfde875f2013-05-28 17:01:54 -040096
Alexandre Lisionf02190d2013-12-12 17:26:12 -050097 static final String TAG = HomeActivity.class.getSimpleName();
alisionf76de3b2013-04-16 15:35:22 -040098
alision9f7a6ec2013-05-24 16:26:26 -040099 private ContactListFragment mContactsFragment = null;
alision907bde72013-06-20 14:40:37 -0400100 private MenuFragment fMenu;
alisionf76de3b2013-04-16 15:35:22 -0400101
alision9f7a6ec2013-05-24 16:26:26 -0400102 private boolean mBound = false;
103 private ISipService service;
alisionf76de3b2013-04-16 15:35:22 -0400104
alision2cb99562013-05-30 17:02:20 -0400105 public static final int REQUEST_CODE_PREFERENCES = 1;
alision9f7a6ec2013-05-24 16:26:26 -0400106 private static final int REQUEST_CODE_CALL = 2;
alisionf76de3b2013-04-16 15:35:22 -0400107
alision0b553832013-05-30 09:31:06 -0400108 RelativeLayout mSliderButton;
Alexandre Lision450458a2013-11-22 11:33:12 -0500109 SlidingUpPanelLayout mContactDrawer;
Alexandre Lision68900052013-11-01 11:34:56 -0400110 private DrawerLayout mNavigationDrawer;
alision9f7a6ec2013-05-24 16:26:26 -0400111 private ActionBarDrawerToggle mDrawerToggle;
alisionf76de3b2013-04-16 15:35:22 -0400112
alision58356b72013-06-03 17:13:36 -0400113 CallReceiver callReceiver;
Alexandre Lision9d052a12013-11-06 15:13:34 -0500114 private boolean isClosing = false;
115 private Timer t = new Timer();
alision84813a12013-05-27 17:40:39 -0400116
Alexandre Lisionf02190d2013-12-12 17:26:12 -0500117 private Fragment fContent;
118
alision9f7a6ec2013-05-24 16:26:26 -0400119 /* called before activity is killed, e.g. rotation */
120 @Override
121 protected void onSaveInstanceState(Bundle bundle) {
122 super.onSaveInstanceState(bundle);
alision9f7a6ec2013-05-24 16:26:26 -0400123 getFragmentManager().putFragment(bundle, "ContactsListFragment", mContactsFragment);
124 Log.w(TAG, "onSaveInstanceState()");
125 }
126
127 @Override
128 public void onCreate(Bundle savedInstanceState) {
129 super.onCreate(savedInstanceState);
130
alision58356b72013-06-03 17:13:36 -0400131 callReceiver = new CallReceiver(this);
alision907bde72013-06-20 14:40:37 -0400132
Alexandre Lision269e6042013-12-11 16:15:13 -0500133 setContentView(R.layout.activity_home);
alision58356b72013-06-03 17:13:36 -0400134
alision9f7a6ec2013-05-24 16:26:26 -0400135 // Bind to LocalService
136 if (!mBound) {
137 Log.i(TAG, "onStart: Binding service...");
138 Intent intent = new Intent(this, SipService.class);
Alexandre Lision0f550ee2013-10-25 15:34:38 -0400139 startService(intent);
alision9f7a6ec2013-05-24 16:26:26 -0400140 bindService(intent, mConnection, Context.BIND_AUTO_CREATE);
141 }
Alexandre Lision8b9d8e82013-10-04 09:21:27 -0400142
143 if (savedInstanceState != null) {
Alexandre Lision23628c12013-09-24 11:17:05 -0400144 mContactsFragment = (ContactListFragment) getFragmentManager().getFragment(savedInstanceState, "ContactsListFragment");
145 }
alision9f7a6ec2013-05-24 16:26:26 -0400146 if (mContactsFragment == null) {
147 mContactsFragment = new ContactListFragment();
alision9f7a6ec2013-05-24 16:26:26 -0400148 getFragmentManager().beginTransaction().replace(R.id.contacts_frame, mContactsFragment).commit();
149 }
alision2ec64f92013-06-17 17:28:58 -0400150
Alexandre Lision450458a2013-11-22 11:33:12 -0500151 mContactDrawer = (SlidingUpPanelLayout) findViewById(R.id.contact_panel);
152 // mContactDrawer.setShadowDrawable(getResources().getDrawable(R.drawable.above_shadow));
153 mContactDrawer.setAnchorPoint(0.3f);
154 mContactDrawer.setDragView(findViewById(R.id.contacts_frame));
155 mContactDrawer.setEnableDragViewTouchEvents(true);
Alexandre Lision450458a2013-11-22 11:33:12 -0500156 mContactDrawer.setPanelSlideListener(new PanelSlideListener() {
Alexandre Lision8b9d8e82013-10-04 09:21:27 -0400157
158 @Override
Alexandre Lision450458a2013-11-22 11:33:12 -0500159 public void onPanelSlide(View panel, float slideOffset) {
160 if (slideOffset < 0.2) {
161 if (getActionBar().isShowing()) {
162 getActionBar().hide();
163 }
164 } else {
165 if (!getActionBar().isShowing()) {
166 getActionBar().show();
167 }
Alexandre Lision8b9d8e82013-10-04 09:21:27 -0400168 }
169 }
Alexandre Lision450458a2013-11-22 11:33:12 -0500170
171 @Override
172 public void onPanelExpanded(View panel) {
173
174 }
175
176 @Override
177 public void onPanelCollapsed(View panel) {
178
179 }
180
181 @Override
182 public void onPanelAnchored(View panel) {
183
184 }
Alexandre Lision8b9d8e82013-10-04 09:21:27 -0400185 });
Alexandre Lisiond588bff2013-10-08 12:43:01 -0400186
Alexandre Lision68900052013-11-01 11:34:56 -0400187 mNavigationDrawer = (DrawerLayout) findViewById(R.id.drawer_layout);
alisionf76de3b2013-04-16 15:35:22 -0400188
alision0b553832013-05-30 09:31:06 -0400189 // set a custom shadow that overlays the main content when the drawer opens
Alexandre Lision68900052013-11-01 11:34:56 -0400190 mNavigationDrawer.setDrawerShadow(R.drawable.drawer_shadow, GravityCompat.START);
alisionf76de3b2013-04-16 15:35:22 -0400191
alision9f7a6ec2013-05-24 16:26:26 -0400192 getActionBar().setDisplayHomeAsUpEnabled(true);
193 getActionBar().setHomeButtonEnabled(true);
alisionf76de3b2013-04-16 15:35:22 -0400194
alision9f7a6ec2013-05-24 16:26:26 -0400195 mDrawerToggle = new ActionBarDrawerToggle(this, /* host Activity */
Alexandre Lision68900052013-11-01 11:34:56 -0400196 mNavigationDrawer, /* DrawerLayout object */
alision9f7a6ec2013-05-24 16:26:26 -0400197 R.drawable.ic_drawer, /* nav drawer image to replace 'Up' caret */
198 R.string.drawer_open, /* "open drawer" description for accessibility */
199 R.string.drawer_close /* "close drawer" description for accessibility */
200 ) {
alision0b553832013-05-30 09:31:06 -0400201 @Override
alision9f7a6ec2013-05-24 16:26:26 -0400202 public void onDrawerClosed(View view) {
alision9f7a6ec2013-05-24 16:26:26 -0400203 invalidateOptionsMenu(); // creates call to onPrepareOptionsMenu()
204 }
alisionf76de3b2013-04-16 15:35:22 -0400205
alision0b553832013-05-30 09:31:06 -0400206 @Override
alision9f7a6ec2013-05-24 16:26:26 -0400207 public void onDrawerOpened(View drawerView) {
Alexandre Lision4d20c812013-12-12 17:44:50 -0500208 // getActionBar().setTitle(mDrawerTitle);
alision9f7a6ec2013-05-24 16:26:26 -0400209 invalidateOptionsMenu(); // creates call to onPrepareOptionsMenu()
210 }
211 };
alisionf76de3b2013-04-16 15:35:22 -0400212
Alexandre Lision68900052013-11-01 11:34:56 -0400213 mNavigationDrawer.setDrawerListener(mDrawerToggle);
alisionf76de3b2013-04-16 15:35:22 -0400214
alision9f7a6ec2013-05-24 16:26:26 -0400215 }
alisionf76de3b2013-04-16 15:35:22 -0400216
alisionf57d8a62013-07-02 09:37:12 -0400217 @Override
218 protected void onPostCreate(Bundle savedInstanceState) {
219 super.onPostCreate(savedInstanceState);
220 // Sync the toggle state after onRestoreInstanceState has occurred.
221 mDrawerToggle.syncState();
Alexandre Lision450458a2013-11-22 11:33:12 -0500222 if (mContactDrawer.isExpanded()) {
Alexandre Lisiond16bad92013-10-09 17:16:20 -0400223 getActionBar().hide();
224 }
alisionf57d8a62013-07-02 09:37:12 -0400225 }
alision465ceba2013-07-04 09:24:30 -0400226
alisionf57d8a62013-07-02 09:37:12 -0400227 @Override
228 public void onConfigurationChanged(Configuration newConfig) {
229 super.onConfigurationChanged(newConfig);
230 mDrawerToggle.onConfigurationChanged(newConfig);
231 }
alision3c5c2a92013-05-30 09:50:47 -0400232
alision9f7a6ec2013-05-24 16:26:26 -0400233 @Override
234 protected void onStart() {
235 Log.i(TAG, "onStart");
Alexandre Lision35577132013-12-06 15:21:15 -0500236
237 if (!PreferenceManager.getDefaultSharedPreferences(getApplicationContext()).getBoolean("installed", false)) {
238 PreferenceManager.getDefaultSharedPreferences(getApplicationContext()).edit().putBoolean("installed", true).commit();
239
240 copyAssetFolder(getAssets(), "ringtones", getFilesDir().getAbsolutePath() + "/ringtones");
241 }
242
alision9f7a6ec2013-05-24 16:26:26 -0400243 super.onStart();
Alexandre Lisionc51ccb12013-09-11 16:00:30 -0400244
alision9f7a6ec2013-05-24 16:26:26 -0400245 }
alisionf76de3b2013-04-16 15:35:22 -0400246
Alexandre Lision35577132013-12-06 15:21:15 -0500247 private static boolean copyAssetFolder(AssetManager assetManager, String fromAssetPath, String toPath) {
248 try {
249 String[] files = assetManager.list(fromAssetPath);
250 new File(toPath).mkdirs();
251 Log.i(TAG, "Creating :" + toPath);
252 boolean res = true;
253 for (String file : files)
254 if (file.contains(".")) {
255 Log.i(TAG, "Copying file :" + fromAssetPath + "/" + file + " to " + toPath + "/" + file);
256 res &= copyAsset(assetManager, fromAssetPath + "/" + file, toPath + "/" + file);
257 } else {
258 Log.i(TAG, "Copying folder :" + fromAssetPath + "/" + file + " to " + toPath + "/" + file);
259 res &= copyAssetFolder(assetManager, fromAssetPath + "/" + file, toPath + "/" + file);
260 }
261 return res;
262 } catch (Exception e) {
263 e.printStackTrace();
264 return false;
265 }
266 }
267
268 private static boolean copyAsset(AssetManager assetManager, String fromAssetPath, String toPath) {
269 InputStream in = null;
270 OutputStream out = null;
271 try {
272 in = assetManager.open(fromAssetPath);
273 new File(toPath).createNewFile();
274 out = new FileOutputStream(toPath);
275 copyFile(in, out);
276 in.close();
277 in = null;
278 out.flush();
279 out.close();
280 out = null;
281 return true;
282 } catch (Exception e) {
283 e.printStackTrace();
284 return false;
285 }
286 }
287
288 private static void copyFile(InputStream in, OutputStream out) throws IOException {
289 byte[] buffer = new byte[1024];
290 int read;
291 while ((read = in.read(buffer)) != -1) {
292 out.write(buffer, 0, read);
293 }
294 }
295
alision9f7a6ec2013-05-24 16:26:26 -0400296 /* user gets back to the activity, e.g. through task manager */
297 @Override
298 protected void onRestart() {
299 super.onRestart();
300 }
alisionf76de3b2013-04-16 15:35:22 -0400301
alision9f7a6ec2013-05-24 16:26:26 -0400302 /* activity gets back to the foreground and user input */
303 @Override
304 protected void onResume() {
alision58356b72013-06-03 17:13:36 -0400305 super.onResume();
alision84813a12013-05-27 17:40:39 -0400306 IntentFilter intentFilter = new IntentFilter();
307 intentFilter.addAction(CallManagerCallBack.INCOMING_CALL);
308 intentFilter.addAction(CallManagerCallBack.INCOMING_TEXT);
309 intentFilter.addAction(CallManagerCallBack.CALL_STATE_CHANGED);
alision58356b72013-06-03 17:13:36 -0400310 registerReceiver(callReceiver, intentFilter);
alision907bde72013-06-20 14:40:37 -0400311
alision9f7a6ec2013-05-24 16:26:26 -0400312 }
alision907bde72013-06-20 14:40:37 -0400313
alisionbd45a822013-06-06 17:35:35 -0400314 @Override
315 public void onBackPressed() {
Alexandre Lisiond588bff2013-10-08 12:43:01 -0400316
Alexandre Lision68900052013-11-01 11:34:56 -0400317 if (mNavigationDrawer.isDrawerVisible(Gravity.LEFT)) {
318 mNavigationDrawer.closeDrawer(Gravity.LEFT);
Alexandre Lision68855472013-10-10 16:20:46 -0400319 return;
Alexandre Lision3ccccf02013-10-07 14:10:46 -0400320 }
alision907bde72013-06-20 14:40:37 -0400321
Alexandre Lision450458a2013-11-22 11:33:12 -0500322 if (mContactDrawer.isExpanded() || mContactDrawer.isAnchored()) {
323 mContactDrawer.collapsePane();
alisionbd45a822013-06-06 17:35:35 -0400324 return;
325 }
alision2ec64f92013-06-17 17:28:58 -0400326
Alexandre Lisionf02190d2013-12-12 17:26:12 -0500327 if (getFragmentManager().getBackStackEntryCount() > 0) {
328 BackStackEntry entry = getFragmentManager().getBackStackEntryAt(getFragmentManager().getBackStackEntryCount() - 1);
329
330 fContent = getFragmentManager().findFragmentByTag(entry.getName());
331
332 getFragmentManager().popBackStack();
333 return;
334 }
335
alisionbd45a822013-06-06 17:35:35 -0400336 if (isClosing) {
337 super.onBackPressed();
338 t.cancel();
339 finish();
340 } else {
341
342 t.schedule(new TimerTask() {
343 @Override
344 public void run() {
345 isClosing = false;
346 }
347 }, 3000);
348 Toast.makeText(this, getResources().getString(R.string.close_msg), Toast.LENGTH_SHORT).show();
349 isClosing = true;
350 }
351 }
alisione2a38e12013-04-25 14:20:20 -0400352
alision9f7a6ec2013-05-24 16:26:26 -0400353 /* activity no more in foreground */
354 @Override
355 protected void onPause() {
356 super.onPause();
alision58356b72013-06-03 17:13:36 -0400357 unregisterReceiver(callReceiver);
Alexandre Lisionb4a9e392013-10-01 14:39:43 -0400358 if (mBound) {
359 unbindService(mConnection);
360 mBound = false;
361 }
alision9f7a6ec2013-05-24 16:26:26 -0400362 }
alisionf76de3b2013-04-16 15:35:22 -0400363
alision9f7a6ec2013-05-24 16:26:26 -0400364 /* activity finishes itself or is being killed by the system */
365 @Override
366 protected void onDestroy() {
alision9f7a6ec2013-05-24 16:26:26 -0400367 super.onDestroy();
Alexandre Lisionb4a9e392013-10-01 14:39:43 -0400368 Log.i(TAG, "onDestroy: destroying service...");
369 Intent sipServiceIntent = new Intent(this, SipService.class);
370 stopService(sipServiceIntent);
alision9f7a6ec2013-05-24 16:26:26 -0400371 }
alision73424b62013-04-26 11:49:18 -0400372
alision55c36cb2013-06-14 14:57:38 -0400373 public void launchCallActivity(SipCall infos) {
Alexandre Lisionc59685a2013-10-04 16:36:36 -0400374
alision9f7a6ec2013-05-24 16:26:26 -0400375 Bundle bundle = new Bundle();
alisiondf1dac92013-06-27 17:35:53 -0400376 Conference tmp = new Conference("-1");
alision465ceba2013-07-04 09:24:30 -0400377
alisiondf1dac92013-06-27 17:35:53 -0400378 tmp.getParticipants().add(infos);
alision465ceba2013-07-04 09:24:30 -0400379
alisiondf1dac92013-06-27 17:35:53 -0400380 bundle.putParcelable("conference", tmp);
alision9f7a6ec2013-05-24 16:26:26 -0400381 Intent intent = new Intent().setClass(this, CallActivity.class);
alisiondf1dac92013-06-27 17:35:53 -0400382 intent.putExtra("resuming", false);
alision9f7a6ec2013-05-24 16:26:26 -0400383 intent.putExtras(bundle);
384 startActivityForResult(intent, REQUEST_CODE_CALL);
Alexandre Lisiond588bff2013-10-08 12:43:01 -0400385
Alexandre Lision8b9d8e82013-10-04 09:21:27 -0400386 // overridePendingTransition(R.anim.slide_down, R.anim.slide_up);
alision9f7a6ec2013-05-24 16:26:26 -0400387 }
alisionf76de3b2013-04-16 15:35:22 -0400388
alision9f7a6ec2013-05-24 16:26:26 -0400389 /** Defines callbacks for service binding, passed to bindService() */
390 private ServiceConnection mConnection = new ServiceConnection() {
alision371b77e2013-04-23 14:51:26 -0400391
alision9f7a6ec2013-05-24 16:26:26 -0400392 @Override
393 public void onServiceConnected(ComponentName className, IBinder binder) {
394 service = ISipService.Stub.asInterface(binder);
alisione2a38e12013-04-25 14:20:20 -0400395
alision2cb99562013-05-30 17:02:20 -0400396 try {
alision58356b72013-06-03 17:13:36 -0400397
alision907bde72013-06-20 14:40:37 -0400398 fMenu = new MenuFragment();
Alexandre Lisionf02190d2013-12-12 17:26:12 -0500399 fContent = new HomeFragment();
400 getFragmentManager().beginTransaction().replace(R.id.left_drawer, fMenu).replace(R.id.main_frame, fContent).commit();
Alexandre Lisionc51ccb12013-09-11 16:00:30 -0400401
alision2cb99562013-05-30 17:02:20 -0400402 service.destroyNotification();
403 } catch (RemoteException e) {
404 Log.e(TAG, e.toString());
405 }
alision9f7a6ec2013-05-24 16:26:26 -0400406 mBound = true;
alision9f7a6ec2013-05-24 16:26:26 -0400407 Log.d(TAG, "Service connected service=" + service);
alision371b77e2013-04-23 14:51:26 -0400408
alision9f7a6ec2013-05-24 16:26:26 -0400409 }
alision5f899632013-04-22 17:26:56 -0400410
alision9f7a6ec2013-05-24 16:26:26 -0400411 @Override
412 public void onServiceDisconnected(ComponentName arg0) {
alision5f899632013-04-22 17:26:56 -0400413
alision9f7a6ec2013-05-24 16:26:26 -0400414 mBound = false;
415 Log.d(TAG, "Service disconnected service=" + service);
416 }
417 };
alision5f899632013-04-22 17:26:56 -0400418
alision9f7a6ec2013-05-24 16:26:26 -0400419 @Override
420 public boolean onOptionsItemSelected(MenuItem item) {
421 Log.i(TAG, "onOptionsItemSelected " + item.getItemId());
alision5f899632013-04-22 17:26:56 -0400422
alision9f7a6ec2013-05-24 16:26:26 -0400423 if (mDrawerToggle.onOptionsItemSelected(item)) {
424 return true;
425 }
alision5f899632013-04-22 17:26:56 -0400426
alision9f7a6ec2013-05-24 16:26:26 -0400427 return super.onOptionsItemSelected(item);
428 }
alisionf76de3b2013-04-16 15:35:22 -0400429
alision9f7a6ec2013-05-24 16:26:26 -0400430 @Override
431 public void onActivityResult(int requestCode, int resultCode, Intent data) {
432 super.onActivityResult(requestCode, resultCode, data);
alision84813a12013-05-27 17:40:39 -0400433
434 switch (requestCode) {
alision9f7a6ec2013-05-24 16:26:26 -0400435 case REQUEST_CODE_PREFERENCES:
Alexandre Lisione5b66022013-10-30 11:34:15 -0400436 if (fMenu != null)
Alexandre Lision933ef0a2013-10-15 17:28:40 -0400437 fMenu.updateAllAccounts();
alision9f7a6ec2013-05-24 16:26:26 -0400438 break;
439 case REQUEST_CODE_CALL:
Alexandre Lisionc51ccb12013-09-11 16:00:30 -0400440 if (resultCode == CallActivity.RESULT_FAILURE) {
Alexandre Lision8b9d8e82013-10-04 09:21:27 -0400441 Log.w(TAG, "Call Failed");
Alexandre Lisionc51ccb12013-09-11 16:00:30 -0400442 }
443 // if (mSectionsPagerAdapter != null && mSectionsPagerAdapter.getItem(2) != null)
444 // getLoaderManager().restartLoader(LoaderConstants.HISTORY_LOADER, null, (HistoryFragment) mSectionsPagerAdapter.getItem(2));
alision9f7a6ec2013-05-24 16:26:26 -0400445 break;
446 }
alisionf76de3b2013-04-16 15:35:22 -0400447
alision9f7a6ec2013-05-24 16:26:26 -0400448 }
alisionf76de3b2013-04-16 15:35:22 -0400449
alision9f7a6ec2013-05-24 16:26:26 -0400450 @Override
alision9f7a6ec2013-05-24 16:26:26 -0400451 public ISipService getService() {
452 return service;
453 }
alisionf76de3b2013-04-16 15:35:22 -0400454
alision84813a12013-05-27 17:40:39 -0400455 /**
456 * Interface implemented to handle incoming events
457 */
alision9f7a6ec2013-05-24 16:26:26 -0400458 @Override
alision84813a12013-05-27 17:40:39 -0400459 public void incomingCall(Intent call) {
alisionfde875f2013-05-28 17:01:54 -0400460 SipCall infos = call.getParcelableExtra("newcall");
alision84813a12013-05-27 17:40:39 -0400461
alision55c36cb2013-06-14 14:57:38 -0400462 launchCallActivity(infos);
alision84813a12013-05-27 17:40:39 -0400463
464 }
465
466 @Override
467 public void callStateChanged(Intent callState) {
468 Bundle b = callState.getBundleExtra("com.savoirfairelinux.sflphone.service.newstate");
469 String cID = b.getString("CallID");
470 String state = b.getString("State");
471 Log.i(TAG, "callStateChanged" + cID + " " + state);
Alexandre Lisionf02190d2013-12-12 17:26:12 -0500472 // mSectionsPagerAdapter.updateHome();
alision84813a12013-05-27 17:40:39 -0400473
474 }
475
476 @Override
477 public void incomingText(Intent msg) {
478 Bundle b = msg.getBundleExtra("com.savoirfairelinux.sflphone.service.newtext");
479 b.getString("CallID");
480 String from = b.getString("From");
481 String mess = b.getString("Msg");
482 Toast.makeText(getApplicationContext(), "text from " + from + " : " + mess, Toast.LENGTH_LONG).show();
483 }
Alexandre Lisiond588bff2013-10-08 12:43:01 -0400484
Alexandre Lisione1c96db2013-10-04 14:34:21 -0400485 @Override
486 public void onTextContact(final CallContact c) {
487 // TODO
488 }
Alexandre Lisiond588bff2013-10-08 12:43:01 -0400489
Alexandre Lisionc59685a2013-10-04 16:36:36 -0400490 @Override
491 public void onEditContact(final CallContact c) {
492 Intent intent = new Intent(Intent.ACTION_VIEW);
493 Uri uri = Uri.withAppendedPath(ContactsContract.Contacts.CONTENT_URI, String.valueOf(c.getId()));
494 intent.setData(uri);
495 startActivity(intent);
496 }
alision84813a12013-05-27 17:40:39 -0400497
498 @Override
Alexandre Lisione1c96db2013-10-04 14:34:21 -0400499 public void onCallContact(final CallContact c) {
Alexandre Lision8b9d8e82013-10-04 09:21:27 -0400500
Alexandre Lisionf0b993d2013-09-12 13:25:10 -0400501 if (fMenu.getSelectedAccount() == null) {
Alexandre Lisionf0b993d2013-09-12 13:25:10 -0400502 createAccountDialog().show();
503 return;
504 }
alisionfde875f2013-05-28 17:01:54 -0400505
Alexandre Lisiond588bff2013-10-08 12:43:01 -0400506 if (!fMenu.getSelectedAccount().isRegistered()) {
507 createNotRegisteredDialog().show();
508 return;
509 }
510
Alexandre Lisiond16bad92013-10-09 17:16:20 -0400511 getActionBar().show();
alision55c36cb2013-06-14 14:57:38 -0400512 Thread launcher = new Thread(new Runnable() {
alision907bde72013-06-20 14:40:37 -0400513
alision55c36cb2013-06-14 14:57:38 -0400514 final String[] CONTACTS_PHONES_PROJECTION = new String[] { Phone.NUMBER, Phone.TYPE };
515 final String[] CONTACTS_SIP_PROJECTION = new String[] { SipAddress.SIP_ADDRESS, SipAddress.TYPE };
alision907bde72013-06-20 14:40:37 -0400516
alision55c36cb2013-06-14 14:57:38 -0400517 @Override
518 public void run() {
519 SipCall.SipCallBuilder callBuilder = SipCall.SipCallBuilder.getInstance();
520 try {
Alexandre Lisiond588bff2013-10-08 12:43:01 -0400521 callBuilder.startCallCreation().setAccount(fMenu.getSelectedAccount()).setCallType(SipCall.state.CALL_TYPE_OUTGOING);
alision907bde72013-06-20 14:40:37 -0400522 Cursor cPhones = getContentResolver().query(Phone.CONTENT_URI, CONTACTS_PHONES_PROJECTION, Phone.CONTACT_ID + " =" + c.getId(),
523 null, null);
alision55c36cb2013-06-14 14:57:38 -0400524
alision907bde72013-06-20 14:40:37 -0400525 while (cPhones.moveToNext()) {
526 c.addPhoneNumber(cPhones.getString(cPhones.getColumnIndex(Phone.NUMBER)), cPhones.getInt(cPhones.getColumnIndex(Phone.TYPE)));
527 }
528 cPhones.close();
529
530 Cursor cSip = getContentResolver().query(Phone.CONTENT_URI, CONTACTS_SIP_PROJECTION, Phone.CONTACT_ID + "=" + c.getId(), null,
531 null);
532
533 while (cSip.moveToNext()) {
534 c.addSipNumber(cSip.getString(cSip.getColumnIndex(SipAddress.SIP_ADDRESS)), cSip.getInt(cSip.getColumnIndex(SipAddress.TYPE)));
535 }
536 cSip.close();
537 callBuilder.setContact(c);
alision55c36cb2013-06-14 14:57:38 -0400538 launchCallActivity(callBuilder.build());
Alexandre Lision5ed2c972013-10-11 15:36:33 -0400539 } catch (InvalidObjectException e) {
540 e.printStackTrace();
alision55c36cb2013-06-14 14:57:38 -0400541 }
alision907bde72013-06-20 14:40:37 -0400542
alision55c36cb2013-06-14 14:57:38 -0400543 }
544 });
545 launcher.start();
Alexandre Lision450458a2013-11-22 11:33:12 -0500546 mContactDrawer.collapsePane();
alision84813a12013-05-27 17:40:39 -0400547
548 }
549
550 @Override
alision907bde72013-06-20 14:40:37 -0400551 public void onCallDialed(String to) {
alision84813a12013-05-27 17:40:39 -0400552
alisiondf1dac92013-06-27 17:35:53 -0400553 if (fMenu.getSelectedAccount() == null) {
Alexandre Lisionc51ccb12013-09-11 16:00:30 -0400554 createAccountDialog().show();
alisiondf1dac92013-06-27 17:35:53 -0400555 return;
556 }
557
Alexandre Lisiond588bff2013-10-08 12:43:01 -0400558 if (fMenu.getSelectedAccount().isRegistered()) {
559 SipCall.SipCallBuilder callBuilder = SipCall.SipCallBuilder.getInstance();
560 callBuilder.startCallCreation().setAccount(fMenu.getSelectedAccount()).setCallType(SipCall.state.CALL_TYPE_OUTGOING);
561 callBuilder.setContact(CallContact.ContactBuilder.buildUnknownContact(to));
alision84813a12013-05-27 17:40:39 -0400562
Alexandre Lisiond588bff2013-10-08 12:43:01 -0400563 try {
564 launchCallActivity(callBuilder.build());
565 } catch (Exception e) {
566 Log.e(TAG, e.toString());
567 }
568 } else {
Alexandre Lision5ed2c972013-10-11 15:36:33 -0400569 createNotRegisteredDialog().show();
alisionfde875f2013-05-28 17:01:54 -0400570 }
alision84813a12013-05-27 17:40:39 -0400571
alision9f7a6ec2013-05-24 16:26:26 -0400572 }
alision371b77e2013-04-23 14:51:26 -0400573
Alexandre Lisiond588bff2013-10-08 12:43:01 -0400574 private AlertDialog createNotRegisteredDialog() {
575 final Activity ownerActivity = this;
576 AlertDialog.Builder builder = new AlertDialog.Builder(ownerActivity);
577
578 builder.setMessage(getResources().getString(R.string.cannot_pass_sipcall))
579 .setTitle(getResources().getString(R.string.cannot_pass_sipcall_title))
580 .setPositiveButton(getResources().getString(android.R.string.ok), new DialogInterface.OnClickListener() {
581 public void onClick(DialogInterface dialog, int whichButton) {
Alexandre Lision5ed2c972013-10-11 15:36:33 -0400582
Alexandre Lisiond588bff2013-10-08 12:43:01 -0400583 }
584 });
585
586 AlertDialog alertDialog = builder.create();
587 alertDialog.setOwnerActivity(ownerActivity);
588
589 return alertDialog;
590 }
591
Alexandre Lisionc51ccb12013-09-11 16:00:30 -0400592 private AlertDialog createAccountDialog() {
593 final Activity ownerActivity = this;
594 AlertDialog.Builder builder = new AlertDialog.Builder(ownerActivity);
595
596 builder.setMessage(getResources().getString(R.string.create_new_account_dialog))
597 .setTitle(getResources().getString(R.string.create_new_account_dialog_title))
598 .setPositiveButton(getResources().getString(android.R.string.ok), new DialogInterface.OnClickListener() {
599 public void onClick(DialogInterface dialog, int whichButton) {
600 Intent in = new Intent();
Alexandre Lision40954dc2013-10-09 15:24:03 -0400601 in.setClass(ownerActivity, AccountWizard.class);
Alexandre Lision4ab53972013-11-04 16:59:18 -0500602 ownerActivity.startActivityForResult(in, HomeActivity.REQUEST_CODE_PREFERENCES);
Alexandre Lisionc51ccb12013-09-11 16:00:30 -0400603 }
604 }).setNegativeButton(getResources().getString(android.R.string.cancel), new DialogInterface.OnClickListener() {
605 public void onClick(DialogInterface dialog, int whichButton) {
606 dialog.dismiss();
607 }
608 });
609
610 AlertDialog alertDialog = builder.create();
611 alertDialog.setOwnerActivity(ownerActivity);
612
613 return alertDialog;
614 }
615
alisiond45da712013-05-30 09:18:49 -0400616 @Override
617 public void onContactDragged() {
Alexandre Lision450458a2013-11-22 11:33:12 -0500618 mContactDrawer.collapsePane();
alisiond45da712013-05-30 09:18:49 -0400619 }
620
alision58356b72013-06-03 17:13:36 -0400621 @Override
Alexandre Lision0936ba62013-11-25 10:04:56 -0500622 public void toggleDrawer() {
Alexandre Lisionc985b362013-12-10 17:45:03 -0500623 if (mContactDrawer.isAnchored())
Alexandre Lision0936ba62013-11-25 10:04:56 -0500624 mContactDrawer.expandPane();
Alexandre Lisionc985b362013-12-10 17:45:03 -0500625 else if (!mContactDrawer.isExpanded())
626 mContactDrawer.expandPane(0.3f);
Alexandre Lisionc9b4e9e2013-11-27 10:27:28 -0500627 else
628 mContactDrawer.collapsePane();
alision58356b72013-06-03 17:13:36 -0400629 }
630
alision806e18e2013-06-21 15:30:17 -0400631 @Override
Alexandre Lisiona458ba22013-12-11 15:04:11 -0500632 public void toggleForSearchDrawer() {
633 if (mContactDrawer.isExpanded()) {
634 mContactDrawer.collapsePane();
635 } else
636 mContactDrawer.expandPane();
637 }
638
639 @Override
alision806e18e2013-06-21 15:30:17 -0400640 public void confCreated(Intent intent) {
641 // TODO Auto-generated method stub
alisiondf1dac92013-06-27 17:35:53 -0400642
alision806e18e2013-06-21 15:30:17 -0400643 }
644
645 @Override
646 public void confRemoved(Intent intent) {
647 // TODO Auto-generated method stub
alisiondf1dac92013-06-27 17:35:53 -0400648
alision806e18e2013-06-21 15:30:17 -0400649 }
650
651 @Override
652 public void confChanged(Intent intent) {
653 // TODO Auto-generated method stub
alisiondf1dac92013-06-27 17:35:53 -0400654
655 }
656
657 @Override
658 public void recordingChanged(Intent intent) {
659 // TODO Auto-generated method stub
660
661 }
662
663 @Override
Alexandre Lision1a9e3b12013-09-16 11:06:07 -0400664 public void selectedCall(Conference c) {
alisiondf1dac92013-06-27 17:35:53 -0400665 Intent intent = new Intent().setClass(this, CallActivity.class);
666 intent.putExtra("resuming", true);
Alexandre Lision1a9e3b12013-09-16 11:06:07 -0400667 intent.putExtra("conference", c);
alisiondf1dac92013-06-27 17:35:53 -0400668 startActivityForResult(intent, REQUEST_CODE_CALL);
alision806e18e2013-06-21 15:30:17 -0400669 }
Alexandre Lision8b9d8e82013-10-04 09:21:27 -0400670
Alexandre Lision0936ba62013-11-25 10:04:56 -0500671 @Override
672 public void setDragView(RelativeLayout relativeLayout) {
673 mContactDrawer.setDragView(relativeLayout);
674 }
675
Alexandre Lision269e6042013-12-11 16:15:13 -0500676 @Override
677 public void onSectionSelected(int pos) {
678 Intent in = new Intent();
679
680 switch (pos) {
681 case 0:
Alexandre Lisionf02190d2013-12-12 17:26:12 -0500682 fContent = new HomeFragment();
683 getFragmentManager().beginTransaction().replace(R.id.main_frame, fContent).addToBackStack("HomeTransac").commit();
Alexandre Lision269e6042013-12-11 16:15:13 -0500684 break;
685 case 1:
Alexandre Lisionf02190d2013-12-12 17:26:12 -0500686 fContent = new AccountsManagementFragment();
687 getFragmentManager().beginTransaction().replace(R.id.main_frame, fContent).addToBackStack("AccountsTransac").commit();
688 break;
689 case 2:
690 fContent = new AboutFragment();
691 getFragmentManager().beginTransaction().replace(R.id.main_frame, fContent).addToBackStack("AboutTransac").commit();
Alexandre Lision269e6042013-12-11 16:15:13 -0500692 break;
693 }
Alexandre Lisionf02190d2013-12-12 17:26:12 -0500694
695 mNavigationDrawer.closeDrawers();
Alexandre Lision269e6042013-12-11 16:15:13 -0500696 }
697
alisionf76de3b2013-04-16 15:35:22 -0400698}