blob: f479a615d430349eb70e7b17d56db3df6275099c [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;
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;
alision84813a12013-05-27 17:40:39 -040069import android.content.IntentFilter;
alisionf76de3b2013-04-16 15:35:22 -040070import android.content.ServiceConnection;
Alexandre Lision35577132013-12-06 15:21:15 -050071import android.content.res.AssetManager;
alisionf57d8a62013-07-02 09:37:12 -040072import android.content.res.Configuration;
alision55c36cb2013-06-14 14:57:38 -040073import android.database.Cursor;
Alexandre Lisionc59685a2013-10-04 16:36:36 -040074import android.net.Uri;
alisionf76de3b2013-04-16 15:35:22 -040075import android.os.Bundle;
76import android.os.IBinder;
alision5f899632013-04-22 17:26:56 -040077import android.os.RemoteException;
Alexandre Lision35577132013-12-06 15:21:15 -050078import android.preference.PreferenceManager;
Alexandre Lisionc59685a2013-10-04 16:36:36 -040079import android.provider.ContactsContract;
alision55c36cb2013-06-14 14:57:38 -040080import android.provider.ContactsContract.CommonDataKinds.Phone;
81import android.provider.ContactsContract.CommonDataKinds.SipAddress;
alision9f7a6ec2013-05-24 16:26:26 -040082import android.support.v4.app.ActionBarDrawerToggle;
Alexandre Lisiona8b78722013-12-13 10:18:33 -050083import android.support.v4.app.Fragment;
84import android.support.v4.app.FragmentActivity;
Alexandre Lision18e30542013-12-13 11:32:07 -050085import android.support.v4.app.FragmentManager;
Alexandre Lisiona8b78722013-12-13 10:18:33 -050086import android.support.v4.app.FragmentManager.BackStackEntry;
alision9f7a6ec2013-05-24 16:26:26 -040087import android.support.v4.view.GravityCompat;
alision9f7a6ec2013-05-24 16:26:26 -040088import android.support.v4.widget.DrawerLayout;
alisionf76de3b2013-04-16 15:35:22 -040089import android.util.Log;
Alexandre Lision3ccccf02013-10-07 14:10:46 -040090import android.view.Gravity;
alision9f7a6ec2013-05-24 16:26:26 -040091import android.view.MenuItem;
92import android.view.View;
alision7297bdb2013-05-21 11:56:55 -040093import android.widget.RelativeLayout;
alision04a00182013-05-10 17:05:29 -040094import android.widget.Toast;
alisionf76de3b2013-04-16 15:35:22 -040095
Alexandre Lisiona8b78722013-12-13 10:18:33 -050096public class HomeActivity extends FragmentActivity implements DialingFragment.Callbacks, AccountsManagementFragment.Callbacks,
97 ContactListFragment.Callbacks, CallListFragment.Callbacks, HistoryFragment.Callbacks, CallInterface, MenuFragment.Callbacks {
alisionfde875f2013-05-28 17:01:54 -040098
Alexandre Lisionf02190d2013-12-12 17:26:12 -050099 static final String TAG = HomeActivity.class.getSimpleName();
alisionf76de3b2013-04-16 15:35:22 -0400100
alision9f7a6ec2013-05-24 16:26:26 -0400101 private ContactListFragment mContactsFragment = null;
alision907bde72013-06-20 14:40:37 -0400102 private MenuFragment fMenu;
alisionf76de3b2013-04-16 15:35:22 -0400103
alision9f7a6ec2013-05-24 16:26:26 -0400104 private boolean mBound = false;
105 private ISipService service;
alisionf76de3b2013-04-16 15:35:22 -0400106
alision2cb99562013-05-30 17:02:20 -0400107 public static final int REQUEST_CODE_PREFERENCES = 1;
alision9f7a6ec2013-05-24 16:26:26 -0400108 private static final int REQUEST_CODE_CALL = 2;
alisionf76de3b2013-04-16 15:35:22 -0400109
alision0b553832013-05-30 09:31:06 -0400110 RelativeLayout mSliderButton;
Alexandre Lision450458a2013-11-22 11:33:12 -0500111 SlidingUpPanelLayout mContactDrawer;
Alexandre Lision68900052013-11-01 11:34:56 -0400112 private DrawerLayout mNavigationDrawer;
alision9f7a6ec2013-05-24 16:26:26 -0400113 private ActionBarDrawerToggle mDrawerToggle;
alisionf76de3b2013-04-16 15:35:22 -0400114
alision58356b72013-06-03 17:13:36 -0400115 CallReceiver callReceiver;
Alexandre Lision9d052a12013-11-06 15:13:34 -0500116 private boolean isClosing = false;
117 private Timer t = new Timer();
alision84813a12013-05-27 17:40:39 -0400118
Alexandre Lisionf02190d2013-12-12 17:26:12 -0500119 private Fragment fContent;
120
alision9f7a6ec2013-05-24 16:26:26 -0400121 /* called before activity is killed, e.g. rotation */
122 @Override
123 protected void onSaveInstanceState(Bundle bundle) {
124 super.onSaveInstanceState(bundle);
alision9f7a6ec2013-05-24 16:26:26 -0400125 getFragmentManager().putFragment(bundle, "ContactsListFragment", mContactsFragment);
126 Log.w(TAG, "onSaveInstanceState()");
127 }
128
129 @Override
130 public void onCreate(Bundle savedInstanceState) {
131 super.onCreate(savedInstanceState);
132
alision58356b72013-06-03 17:13:36 -0400133 callReceiver = new CallReceiver(this);
alision907bde72013-06-20 14:40:37 -0400134
Alexandre Lision269e6042013-12-11 16:15:13 -0500135 setContentView(R.layout.activity_home);
alision58356b72013-06-03 17:13:36 -0400136
alision9f7a6ec2013-05-24 16:26:26 -0400137 // Bind to LocalService
138 if (!mBound) {
139 Log.i(TAG, "onStart: Binding service...");
140 Intent intent = new Intent(this, SipService.class);
Alexandre Lision0f550ee2013-10-25 15:34:38 -0400141 startService(intent);
alision9f7a6ec2013-05-24 16:26:26 -0400142 bindService(intent, mConnection, Context.BIND_AUTO_CREATE);
143 }
Alexandre Lision8b9d8e82013-10-04 09:21:27 -0400144
145 if (savedInstanceState != null) {
Alexandre Lision23628c12013-09-24 11:17:05 -0400146 mContactsFragment = (ContactListFragment) getFragmentManager().getFragment(savedInstanceState, "ContactsListFragment");
147 }
alision9f7a6ec2013-05-24 16:26:26 -0400148 if (mContactsFragment == null) {
149 mContactsFragment = new ContactListFragment();
alision9f7a6ec2013-05-24 16:26:26 -0400150 getFragmentManager().beginTransaction().replace(R.id.contacts_frame, mContactsFragment).commit();
151 }
alision2ec64f92013-06-17 17:28:58 -0400152
Alexandre Lision450458a2013-11-22 11:33:12 -0500153 mContactDrawer = (SlidingUpPanelLayout) findViewById(R.id.contact_panel);
154 // mContactDrawer.setShadowDrawable(getResources().getDrawable(R.drawable.above_shadow));
155 mContactDrawer.setAnchorPoint(0.3f);
156 mContactDrawer.setDragView(findViewById(R.id.contacts_frame));
157 mContactDrawer.setEnableDragViewTouchEvents(true);
Alexandre Lision450458a2013-11-22 11:33:12 -0500158 mContactDrawer.setPanelSlideListener(new PanelSlideListener() {
Alexandre Lision8b9d8e82013-10-04 09:21:27 -0400159
160 @Override
Alexandre Lision450458a2013-11-22 11:33:12 -0500161 public void onPanelSlide(View panel, float slideOffset) {
162 if (slideOffset < 0.2) {
163 if (getActionBar().isShowing()) {
164 getActionBar().hide();
165 }
166 } else {
167 if (!getActionBar().isShowing()) {
168 getActionBar().show();
169 }
Alexandre Lision8b9d8e82013-10-04 09:21:27 -0400170 }
171 }
Alexandre Lision450458a2013-11-22 11:33:12 -0500172
173 @Override
174 public void onPanelExpanded(View panel) {
175
176 }
177
178 @Override
179 public void onPanelCollapsed(View panel) {
180
181 }
182
183 @Override
184 public void onPanelAnchored(View panel) {
185
186 }
Alexandre Lision8b9d8e82013-10-04 09:21:27 -0400187 });
Alexandre Lisiond588bff2013-10-08 12:43:01 -0400188
Alexandre Lision68900052013-11-01 11:34:56 -0400189 mNavigationDrawer = (DrawerLayout) findViewById(R.id.drawer_layout);
alisionf76de3b2013-04-16 15:35:22 -0400190
alision0b553832013-05-30 09:31:06 -0400191 // set a custom shadow that overlays the main content when the drawer opens
Alexandre Lision68900052013-11-01 11:34:56 -0400192 mNavigationDrawer.setDrawerShadow(R.drawable.drawer_shadow, GravityCompat.START);
alisionf76de3b2013-04-16 15:35:22 -0400193
alision9f7a6ec2013-05-24 16:26:26 -0400194 getActionBar().setDisplayHomeAsUpEnabled(true);
195 getActionBar().setHomeButtonEnabled(true);
alisionf76de3b2013-04-16 15:35:22 -0400196
alision9f7a6ec2013-05-24 16:26:26 -0400197 mDrawerToggle = new ActionBarDrawerToggle(this, /* host Activity */
Alexandre Lision68900052013-11-01 11:34:56 -0400198 mNavigationDrawer, /* DrawerLayout object */
alision9f7a6ec2013-05-24 16:26:26 -0400199 R.drawable.ic_drawer, /* nav drawer image to replace 'Up' caret */
200 R.string.drawer_open, /* "open drawer" description for accessibility */
201 R.string.drawer_close /* "close drawer" description for accessibility */
202 ) {
alision0b553832013-05-30 09:31:06 -0400203 @Override
alision9f7a6ec2013-05-24 16:26:26 -0400204 public void onDrawerClosed(View view) {
alision9f7a6ec2013-05-24 16:26:26 -0400205 invalidateOptionsMenu(); // creates call to onPrepareOptionsMenu()
206 }
alisionf76de3b2013-04-16 15:35:22 -0400207
alision0b553832013-05-30 09:31:06 -0400208 @Override
alision9f7a6ec2013-05-24 16:26:26 -0400209 public void onDrawerOpened(View drawerView) {
Alexandre Lision4d20c812013-12-12 17:44:50 -0500210 // getActionBar().setTitle(mDrawerTitle);
alision9f7a6ec2013-05-24 16:26:26 -0400211 invalidateOptionsMenu(); // creates call to onPrepareOptionsMenu()
212 }
213 };
alisionf76de3b2013-04-16 15:35:22 -0400214
Alexandre Lision68900052013-11-01 11:34:56 -0400215 mNavigationDrawer.setDrawerListener(mDrawerToggle);
alisionf76de3b2013-04-16 15:35:22 -0400216
alision9f7a6ec2013-05-24 16:26:26 -0400217 }
alisionf76de3b2013-04-16 15:35:22 -0400218
alisionf57d8a62013-07-02 09:37:12 -0400219 @Override
220 protected void onPostCreate(Bundle savedInstanceState) {
221 super.onPostCreate(savedInstanceState);
222 // Sync the toggle state after onRestoreInstanceState has occurred.
223 mDrawerToggle.syncState();
Alexandre Lision450458a2013-11-22 11:33:12 -0500224 if (mContactDrawer.isExpanded()) {
Alexandre Lisiond16bad92013-10-09 17:16:20 -0400225 getActionBar().hide();
226 }
alisionf57d8a62013-07-02 09:37:12 -0400227 }
alision465ceba2013-07-04 09:24:30 -0400228
alisionf57d8a62013-07-02 09:37:12 -0400229 @Override
230 public void onConfigurationChanged(Configuration newConfig) {
231 super.onConfigurationChanged(newConfig);
232 mDrawerToggle.onConfigurationChanged(newConfig);
233 }
alision3c5c2a92013-05-30 09:50:47 -0400234
alision9f7a6ec2013-05-24 16:26:26 -0400235 @Override
236 protected void onStart() {
237 Log.i(TAG, "onStart");
Alexandre Lision35577132013-12-06 15:21:15 -0500238
239 if (!PreferenceManager.getDefaultSharedPreferences(getApplicationContext()).getBoolean("installed", false)) {
240 PreferenceManager.getDefaultSharedPreferences(getApplicationContext()).edit().putBoolean("installed", true).commit();
241
242 copyAssetFolder(getAssets(), "ringtones", getFilesDir().getAbsolutePath() + "/ringtones");
243 }
244
alision9f7a6ec2013-05-24 16:26:26 -0400245 super.onStart();
Alexandre Lisionc51ccb12013-09-11 16:00:30 -0400246
alision9f7a6ec2013-05-24 16:26:26 -0400247 }
alisionf76de3b2013-04-16 15:35:22 -0400248
Alexandre Lision35577132013-12-06 15:21:15 -0500249 private static boolean copyAssetFolder(AssetManager assetManager, String fromAssetPath, String toPath) {
250 try {
251 String[] files = assetManager.list(fromAssetPath);
252 new File(toPath).mkdirs();
253 Log.i(TAG, "Creating :" + toPath);
254 boolean res = true;
255 for (String file : files)
256 if (file.contains(".")) {
257 Log.i(TAG, "Copying file :" + fromAssetPath + "/" + file + " to " + toPath + "/" + file);
258 res &= copyAsset(assetManager, fromAssetPath + "/" + file, toPath + "/" + file);
259 } else {
260 Log.i(TAG, "Copying folder :" + fromAssetPath + "/" + file + " to " + toPath + "/" + file);
261 res &= copyAssetFolder(assetManager, fromAssetPath + "/" + file, toPath + "/" + file);
262 }
263 return res;
264 } catch (Exception e) {
265 e.printStackTrace();
266 return false;
267 }
268 }
269
270 private static boolean copyAsset(AssetManager assetManager, String fromAssetPath, String toPath) {
271 InputStream in = null;
272 OutputStream out = null;
273 try {
274 in = assetManager.open(fromAssetPath);
275 new File(toPath).createNewFile();
276 out = new FileOutputStream(toPath);
277 copyFile(in, out);
278 in.close();
279 in = null;
280 out.flush();
281 out.close();
282 out = null;
283 return true;
284 } catch (Exception e) {
285 e.printStackTrace();
286 return false;
287 }
288 }
289
290 private static void copyFile(InputStream in, OutputStream out) throws IOException {
291 byte[] buffer = new byte[1024];
292 int read;
293 while ((read = in.read(buffer)) != -1) {
294 out.write(buffer, 0, read);
295 }
296 }
297
alision9f7a6ec2013-05-24 16:26:26 -0400298 /* user gets back to the activity, e.g. through task manager */
299 @Override
300 protected void onRestart() {
301 super.onRestart();
302 }
alisionf76de3b2013-04-16 15:35:22 -0400303
alision9f7a6ec2013-05-24 16:26:26 -0400304 /* activity gets back to the foreground and user input */
305 @Override
306 protected void onResume() {
alision58356b72013-06-03 17:13:36 -0400307 super.onResume();
alision84813a12013-05-27 17:40:39 -0400308 IntentFilter intentFilter = new IntentFilter();
309 intentFilter.addAction(CallManagerCallBack.INCOMING_CALL);
310 intentFilter.addAction(CallManagerCallBack.INCOMING_TEXT);
311 intentFilter.addAction(CallManagerCallBack.CALL_STATE_CHANGED);
alision58356b72013-06-03 17:13:36 -0400312 registerReceiver(callReceiver, intentFilter);
alision907bde72013-06-20 14:40:37 -0400313
alision9f7a6ec2013-05-24 16:26:26 -0400314 }
alision907bde72013-06-20 14:40:37 -0400315
alisionbd45a822013-06-06 17:35:35 -0400316 @Override
317 public void onBackPressed() {
Alexandre Lisiond588bff2013-10-08 12:43:01 -0400318
Alexandre Lision68900052013-11-01 11:34:56 -0400319 if (mNavigationDrawer.isDrawerVisible(Gravity.LEFT)) {
320 mNavigationDrawer.closeDrawer(Gravity.LEFT);
Alexandre Lision68855472013-10-10 16:20:46 -0400321 return;
Alexandre Lision3ccccf02013-10-07 14:10:46 -0400322 }
alision907bde72013-06-20 14:40:37 -0400323
Alexandre Lision450458a2013-11-22 11:33:12 -0500324 if (mContactDrawer.isExpanded() || mContactDrawer.isAnchored()) {
325 mContactDrawer.collapsePane();
alisionbd45a822013-06-06 17:35:35 -0400326 return;
327 }
alision2ec64f92013-06-17 17:28:58 -0400328
Alexandre Lisiona8b78722013-12-13 10:18:33 -0500329 if (getSupportFragmentManager().getBackStackEntryCount() > 0) {
330 BackStackEntry entry = getSupportFragmentManager().getBackStackEntryAt(getSupportFragmentManager().getBackStackEntryCount() - 1);
Alexandre Lision18e30542013-12-13 11:32:07 -0500331 Log.i(TAG, "Popping:"+ getSupportFragmentManager().getBackStackEntryCount());
332 Log.i(TAG, "Name:"+ entry.getName());
Alexandre Lisiona8b78722013-12-13 10:18:33 -0500333 fContent = getSupportFragmentManager().findFragmentByTag(entry.getName());
Alexandre Lision18e30542013-12-13 11:32:07 -0500334 if(fContent == null)
335 Log.i(TAG, "Null frag");
Alexandre Lisiona8b78722013-12-13 10:18:33 -0500336 getSupportFragmentManager().popBackStack();
Alexandre Lisionf02190d2013-12-12 17:26:12 -0500337 return;
338 }
339
alisionbd45a822013-06-06 17:35:35 -0400340 if (isClosing) {
341 super.onBackPressed();
342 t.cancel();
343 finish();
344 } else {
345
346 t.schedule(new TimerTask() {
347 @Override
348 public void run() {
349 isClosing = false;
350 }
351 }, 3000);
352 Toast.makeText(this, getResources().getString(R.string.close_msg), Toast.LENGTH_SHORT).show();
353 isClosing = true;
354 }
355 }
alisione2a38e12013-04-25 14:20:20 -0400356
alision9f7a6ec2013-05-24 16:26:26 -0400357 /* activity no more in foreground */
358 @Override
359 protected void onPause() {
360 super.onPause();
alision58356b72013-06-03 17:13:36 -0400361 unregisterReceiver(callReceiver);
Alexandre Lisionb4a9e392013-10-01 14:39:43 -0400362 if (mBound) {
363 unbindService(mConnection);
364 mBound = false;
365 }
alision9f7a6ec2013-05-24 16:26:26 -0400366 }
alisionf76de3b2013-04-16 15:35:22 -0400367
alision9f7a6ec2013-05-24 16:26:26 -0400368 /* activity finishes itself or is being killed by the system */
369 @Override
370 protected void onDestroy() {
alision9f7a6ec2013-05-24 16:26:26 -0400371 super.onDestroy();
Alexandre Lisionb4a9e392013-10-01 14:39:43 -0400372 Log.i(TAG, "onDestroy: destroying service...");
373 Intent sipServiceIntent = new Intent(this, SipService.class);
374 stopService(sipServiceIntent);
alision9f7a6ec2013-05-24 16:26:26 -0400375 }
alision73424b62013-04-26 11:49:18 -0400376
alision55c36cb2013-06-14 14:57:38 -0400377 public void launchCallActivity(SipCall infos) {
Alexandre Lisionc59685a2013-10-04 16:36:36 -0400378
alision9f7a6ec2013-05-24 16:26:26 -0400379 Bundle bundle = new Bundle();
alisiondf1dac92013-06-27 17:35:53 -0400380 Conference tmp = new Conference("-1");
alision465ceba2013-07-04 09:24:30 -0400381
alisiondf1dac92013-06-27 17:35:53 -0400382 tmp.getParticipants().add(infos);
alision465ceba2013-07-04 09:24:30 -0400383
alisiondf1dac92013-06-27 17:35:53 -0400384 bundle.putParcelable("conference", tmp);
alision9f7a6ec2013-05-24 16:26:26 -0400385 Intent intent = new Intent().setClass(this, CallActivity.class);
alisiondf1dac92013-06-27 17:35:53 -0400386 intent.putExtra("resuming", false);
alision9f7a6ec2013-05-24 16:26:26 -0400387 intent.putExtras(bundle);
388 startActivityForResult(intent, REQUEST_CODE_CALL);
Alexandre Lisiond588bff2013-10-08 12:43:01 -0400389
Alexandre Lision8b9d8e82013-10-04 09:21:27 -0400390 // overridePendingTransition(R.anim.slide_down, R.anim.slide_up);
alision9f7a6ec2013-05-24 16:26:26 -0400391 }
alisionf76de3b2013-04-16 15:35:22 -0400392
alision9f7a6ec2013-05-24 16:26:26 -0400393 /** Defines callbacks for service binding, passed to bindService() */
394 private ServiceConnection mConnection = new ServiceConnection() {
alision371b77e2013-04-23 14:51:26 -0400395
alision9f7a6ec2013-05-24 16:26:26 -0400396 @Override
397 public void onServiceConnected(ComponentName className, IBinder binder) {
398 service = ISipService.Stub.asInterface(binder);
alisione2a38e12013-04-25 14:20:20 -0400399
alision2cb99562013-05-30 17:02:20 -0400400 try {
alision58356b72013-06-03 17:13:36 -0400401
alision907bde72013-06-20 14:40:37 -0400402 fMenu = new MenuFragment();
Alexandre Lisionf02190d2013-12-12 17:26:12 -0500403 fContent = new HomeFragment();
Alexandre Lision18e30542013-12-13 11:32:07 -0500404 getSupportFragmentManager().beginTransaction().replace(R.id.left_drawer, fMenu).commit();
405 getSupportFragmentManager().beginTransaction().replace(R.id.main_frame, fContent, "Home").commit();
Alexandre Lisionc51ccb12013-09-11 16:00:30 -0400406
alision2cb99562013-05-30 17:02:20 -0400407 service.destroyNotification();
408 } catch (RemoteException e) {
409 Log.e(TAG, e.toString());
410 }
alision9f7a6ec2013-05-24 16:26:26 -0400411 mBound = true;
alision9f7a6ec2013-05-24 16:26:26 -0400412 Log.d(TAG, "Service connected service=" + service);
alision371b77e2013-04-23 14:51:26 -0400413
alision9f7a6ec2013-05-24 16:26:26 -0400414 }
alision5f899632013-04-22 17:26:56 -0400415
alision9f7a6ec2013-05-24 16:26:26 -0400416 @Override
417 public void onServiceDisconnected(ComponentName arg0) {
alision5f899632013-04-22 17:26:56 -0400418
alision9f7a6ec2013-05-24 16:26:26 -0400419 mBound = false;
420 Log.d(TAG, "Service disconnected service=" + service);
421 }
422 };
alision5f899632013-04-22 17:26:56 -0400423
alision9f7a6ec2013-05-24 16:26:26 -0400424 @Override
425 public boolean onOptionsItemSelected(MenuItem item) {
426 Log.i(TAG, "onOptionsItemSelected " + item.getItemId());
alision5f899632013-04-22 17:26:56 -0400427
alision9f7a6ec2013-05-24 16:26:26 -0400428 if (mDrawerToggle.onOptionsItemSelected(item)) {
429 return true;
430 }
alision5f899632013-04-22 17:26:56 -0400431
alision9f7a6ec2013-05-24 16:26:26 -0400432 return super.onOptionsItemSelected(item);
433 }
alisionf76de3b2013-04-16 15:35:22 -0400434
alision9f7a6ec2013-05-24 16:26:26 -0400435 @Override
436 public void onActivityResult(int requestCode, int resultCode, Intent data) {
437 super.onActivityResult(requestCode, resultCode, data);
alision84813a12013-05-27 17:40:39 -0400438
439 switch (requestCode) {
alision9f7a6ec2013-05-24 16:26:26 -0400440 case REQUEST_CODE_PREFERENCES:
Alexandre Lisione5b66022013-10-30 11:34:15 -0400441 if (fMenu != null)
Alexandre Lision933ef0a2013-10-15 17:28:40 -0400442 fMenu.updateAllAccounts();
alision9f7a6ec2013-05-24 16:26:26 -0400443 break;
444 case REQUEST_CODE_CALL:
Alexandre Lisionc51ccb12013-09-11 16:00:30 -0400445 if (resultCode == CallActivity.RESULT_FAILURE) {
Alexandre Lision8b9d8e82013-10-04 09:21:27 -0400446 Log.w(TAG, "Call Failed");
Alexandre Lisionc51ccb12013-09-11 16:00:30 -0400447 }
448 // if (mSectionsPagerAdapter != null && mSectionsPagerAdapter.getItem(2) != null)
449 // getLoaderManager().restartLoader(LoaderConstants.HISTORY_LOADER, null, (HistoryFragment) mSectionsPagerAdapter.getItem(2));
alision9f7a6ec2013-05-24 16:26:26 -0400450 break;
451 }
alisionf76de3b2013-04-16 15:35:22 -0400452
alision9f7a6ec2013-05-24 16:26:26 -0400453 }
alisionf76de3b2013-04-16 15:35:22 -0400454
alision9f7a6ec2013-05-24 16:26:26 -0400455 @Override
alision9f7a6ec2013-05-24 16:26:26 -0400456 public ISipService getService() {
457 return service;
458 }
alisionf76de3b2013-04-16 15:35:22 -0400459
alision84813a12013-05-27 17:40:39 -0400460 /**
461 * Interface implemented to handle incoming events
462 */
alision9f7a6ec2013-05-24 16:26:26 -0400463 @Override
alision84813a12013-05-27 17:40:39 -0400464 public void incomingCall(Intent call) {
alisionfde875f2013-05-28 17:01:54 -0400465 SipCall infos = call.getParcelableExtra("newcall");
alision84813a12013-05-27 17:40:39 -0400466
alision55c36cb2013-06-14 14:57:38 -0400467 launchCallActivity(infos);
alision84813a12013-05-27 17:40:39 -0400468
469 }
470
471 @Override
472 public void callStateChanged(Intent callState) {
473 Bundle b = callState.getBundleExtra("com.savoirfairelinux.sflphone.service.newstate");
474 String cID = b.getString("CallID");
475 String state = b.getString("State");
476 Log.i(TAG, "callStateChanged" + cID + " " + state);
Alexandre Lisionf02190d2013-12-12 17:26:12 -0500477 // mSectionsPagerAdapter.updateHome();
alision84813a12013-05-27 17:40:39 -0400478
479 }
480
481 @Override
482 public void incomingText(Intent msg) {
483 Bundle b = msg.getBundleExtra("com.savoirfairelinux.sflphone.service.newtext");
484 b.getString("CallID");
485 String from = b.getString("From");
486 String mess = b.getString("Msg");
487 Toast.makeText(getApplicationContext(), "text from " + from + " : " + mess, Toast.LENGTH_LONG).show();
488 }
Alexandre Lisiond588bff2013-10-08 12:43:01 -0400489
Alexandre Lisione1c96db2013-10-04 14:34:21 -0400490 @Override
491 public void onTextContact(final CallContact c) {
492 // TODO
493 }
Alexandre Lisiond588bff2013-10-08 12:43:01 -0400494
Alexandre Lisionc59685a2013-10-04 16:36:36 -0400495 @Override
496 public void onEditContact(final CallContact c) {
497 Intent intent = new Intent(Intent.ACTION_VIEW);
498 Uri uri = Uri.withAppendedPath(ContactsContract.Contacts.CONTENT_URI, String.valueOf(c.getId()));
499 intent.setData(uri);
500 startActivity(intent);
501 }
alision84813a12013-05-27 17:40:39 -0400502
503 @Override
Alexandre Lisione1c96db2013-10-04 14:34:21 -0400504 public void onCallContact(final CallContact c) {
Alexandre Lision8b9d8e82013-10-04 09:21:27 -0400505
Alexandre Lisionf0b993d2013-09-12 13:25:10 -0400506 if (fMenu.getSelectedAccount() == null) {
Alexandre Lisionf0b993d2013-09-12 13:25:10 -0400507 createAccountDialog().show();
508 return;
509 }
alisionfde875f2013-05-28 17:01:54 -0400510
Alexandre Lisiond588bff2013-10-08 12:43:01 -0400511 if (!fMenu.getSelectedAccount().isRegistered()) {
512 createNotRegisteredDialog().show();
513 return;
514 }
515
Alexandre Lisiond16bad92013-10-09 17:16:20 -0400516 getActionBar().show();
alision55c36cb2013-06-14 14:57:38 -0400517 Thread launcher = new Thread(new Runnable() {
alision907bde72013-06-20 14:40:37 -0400518
alision55c36cb2013-06-14 14:57:38 -0400519 final String[] CONTACTS_PHONES_PROJECTION = new String[] { Phone.NUMBER, Phone.TYPE };
520 final String[] CONTACTS_SIP_PROJECTION = new String[] { SipAddress.SIP_ADDRESS, SipAddress.TYPE };
alision907bde72013-06-20 14:40:37 -0400521
alision55c36cb2013-06-14 14:57:38 -0400522 @Override
523 public void run() {
524 SipCall.SipCallBuilder callBuilder = SipCall.SipCallBuilder.getInstance();
525 try {
Alexandre Lisiond588bff2013-10-08 12:43:01 -0400526 callBuilder.startCallCreation().setAccount(fMenu.getSelectedAccount()).setCallType(SipCall.state.CALL_TYPE_OUTGOING);
alision907bde72013-06-20 14:40:37 -0400527 Cursor cPhones = getContentResolver().query(Phone.CONTENT_URI, CONTACTS_PHONES_PROJECTION, Phone.CONTACT_ID + " =" + c.getId(),
528 null, null);
alision55c36cb2013-06-14 14:57:38 -0400529
alision907bde72013-06-20 14:40:37 -0400530 while (cPhones.moveToNext()) {
531 c.addPhoneNumber(cPhones.getString(cPhones.getColumnIndex(Phone.NUMBER)), cPhones.getInt(cPhones.getColumnIndex(Phone.TYPE)));
532 }
533 cPhones.close();
534
535 Cursor cSip = getContentResolver().query(Phone.CONTENT_URI, CONTACTS_SIP_PROJECTION, Phone.CONTACT_ID + "=" + c.getId(), null,
536 null);
537
538 while (cSip.moveToNext()) {
539 c.addSipNumber(cSip.getString(cSip.getColumnIndex(SipAddress.SIP_ADDRESS)), cSip.getInt(cSip.getColumnIndex(SipAddress.TYPE)));
540 }
541 cSip.close();
542 callBuilder.setContact(c);
alision55c36cb2013-06-14 14:57:38 -0400543 launchCallActivity(callBuilder.build());
Alexandre Lision5ed2c972013-10-11 15:36:33 -0400544 } catch (InvalidObjectException e) {
545 e.printStackTrace();
alision55c36cb2013-06-14 14:57:38 -0400546 }
alision907bde72013-06-20 14:40:37 -0400547
alision55c36cb2013-06-14 14:57:38 -0400548 }
549 });
550 launcher.start();
Alexandre Lision450458a2013-11-22 11:33:12 -0500551 mContactDrawer.collapsePane();
alision84813a12013-05-27 17:40:39 -0400552
553 }
554
555 @Override
alision907bde72013-06-20 14:40:37 -0400556 public void onCallDialed(String to) {
alision84813a12013-05-27 17:40:39 -0400557
alisiondf1dac92013-06-27 17:35:53 -0400558 if (fMenu.getSelectedAccount() == null) {
Alexandre Lisionc51ccb12013-09-11 16:00:30 -0400559 createAccountDialog().show();
alisiondf1dac92013-06-27 17:35:53 -0400560 return;
561 }
562
Alexandre Lisiond588bff2013-10-08 12:43:01 -0400563 if (fMenu.getSelectedAccount().isRegistered()) {
564 SipCall.SipCallBuilder callBuilder = SipCall.SipCallBuilder.getInstance();
565 callBuilder.startCallCreation().setAccount(fMenu.getSelectedAccount()).setCallType(SipCall.state.CALL_TYPE_OUTGOING);
566 callBuilder.setContact(CallContact.ContactBuilder.buildUnknownContact(to));
alision84813a12013-05-27 17:40:39 -0400567
Alexandre Lisiond588bff2013-10-08 12:43:01 -0400568 try {
569 launchCallActivity(callBuilder.build());
570 } catch (Exception e) {
571 Log.e(TAG, e.toString());
572 }
573 } else {
Alexandre Lision5ed2c972013-10-11 15:36:33 -0400574 createNotRegisteredDialog().show();
alisionfde875f2013-05-28 17:01:54 -0400575 }
alision84813a12013-05-27 17:40:39 -0400576
alision9f7a6ec2013-05-24 16:26:26 -0400577 }
alision371b77e2013-04-23 14:51:26 -0400578
Alexandre Lisiond588bff2013-10-08 12:43:01 -0400579 private AlertDialog createNotRegisteredDialog() {
580 final Activity ownerActivity = this;
581 AlertDialog.Builder builder = new AlertDialog.Builder(ownerActivity);
582
583 builder.setMessage(getResources().getString(R.string.cannot_pass_sipcall))
584 .setTitle(getResources().getString(R.string.cannot_pass_sipcall_title))
585 .setPositiveButton(getResources().getString(android.R.string.ok), new DialogInterface.OnClickListener() {
586 public void onClick(DialogInterface dialog, int whichButton) {
Alexandre Lision5ed2c972013-10-11 15:36:33 -0400587
Alexandre Lisiond588bff2013-10-08 12:43:01 -0400588 }
589 });
590
591 AlertDialog alertDialog = builder.create();
592 alertDialog.setOwnerActivity(ownerActivity);
593
594 return alertDialog;
595 }
596
Alexandre Lisionc51ccb12013-09-11 16:00:30 -0400597 private AlertDialog createAccountDialog() {
598 final Activity ownerActivity = this;
599 AlertDialog.Builder builder = new AlertDialog.Builder(ownerActivity);
600
601 builder.setMessage(getResources().getString(R.string.create_new_account_dialog))
602 .setTitle(getResources().getString(R.string.create_new_account_dialog_title))
603 .setPositiveButton(getResources().getString(android.R.string.ok), new DialogInterface.OnClickListener() {
604 public void onClick(DialogInterface dialog, int whichButton) {
605 Intent in = new Intent();
Alexandre Lision40954dc2013-10-09 15:24:03 -0400606 in.setClass(ownerActivity, AccountWizard.class);
Alexandre Lision4ab53972013-11-04 16:59:18 -0500607 ownerActivity.startActivityForResult(in, HomeActivity.REQUEST_CODE_PREFERENCES);
Alexandre Lisionc51ccb12013-09-11 16:00:30 -0400608 }
609 }).setNegativeButton(getResources().getString(android.R.string.cancel), new DialogInterface.OnClickListener() {
610 public void onClick(DialogInterface dialog, int whichButton) {
611 dialog.dismiss();
612 }
613 });
614
615 AlertDialog alertDialog = builder.create();
616 alertDialog.setOwnerActivity(ownerActivity);
617
618 return alertDialog;
619 }
620
alisiond45da712013-05-30 09:18:49 -0400621 @Override
622 public void onContactDragged() {
Alexandre Lision450458a2013-11-22 11:33:12 -0500623 mContactDrawer.collapsePane();
alisiond45da712013-05-30 09:18:49 -0400624 }
625
alision58356b72013-06-03 17:13:36 -0400626 @Override
Alexandre Lision0936ba62013-11-25 10:04:56 -0500627 public void toggleDrawer() {
Alexandre Lisionc985b362013-12-10 17:45:03 -0500628 if (mContactDrawer.isAnchored())
Alexandre Lision0936ba62013-11-25 10:04:56 -0500629 mContactDrawer.expandPane();
Alexandre Lisionc985b362013-12-10 17:45:03 -0500630 else if (!mContactDrawer.isExpanded())
631 mContactDrawer.expandPane(0.3f);
Alexandre Lisionc9b4e9e2013-11-27 10:27:28 -0500632 else
633 mContactDrawer.collapsePane();
alision58356b72013-06-03 17:13:36 -0400634 }
635
alision806e18e2013-06-21 15:30:17 -0400636 @Override
Alexandre Lisiona458ba22013-12-11 15:04:11 -0500637 public void toggleForSearchDrawer() {
638 if (mContactDrawer.isExpanded()) {
639 mContactDrawer.collapsePane();
640 } else
641 mContactDrawer.expandPane();
642 }
643
644 @Override
alision806e18e2013-06-21 15:30:17 -0400645 public void confCreated(Intent intent) {
646 // TODO Auto-generated method stub
alisiondf1dac92013-06-27 17:35:53 -0400647
alision806e18e2013-06-21 15:30:17 -0400648 }
649
650 @Override
651 public void confRemoved(Intent intent) {
652 // TODO Auto-generated method stub
alisiondf1dac92013-06-27 17:35:53 -0400653
alision806e18e2013-06-21 15:30:17 -0400654 }
655
656 @Override
657 public void confChanged(Intent intent) {
658 // TODO Auto-generated method stub
alisiondf1dac92013-06-27 17:35:53 -0400659
660 }
661
662 @Override
663 public void recordingChanged(Intent intent) {
664 // TODO Auto-generated method stub
665
666 }
667
668 @Override
Alexandre Lision1a9e3b12013-09-16 11:06:07 -0400669 public void selectedCall(Conference c) {
alisiondf1dac92013-06-27 17:35:53 -0400670 Intent intent = new Intent().setClass(this, CallActivity.class);
671 intent.putExtra("resuming", true);
Alexandre Lision1a9e3b12013-09-16 11:06:07 -0400672 intent.putExtra("conference", c);
alisiondf1dac92013-06-27 17:35:53 -0400673 startActivityForResult(intent, REQUEST_CODE_CALL);
alision806e18e2013-06-21 15:30:17 -0400674 }
Alexandre Lision8b9d8e82013-10-04 09:21:27 -0400675
Alexandre Lision0936ba62013-11-25 10:04:56 -0500676 @Override
677 public void setDragView(RelativeLayout relativeLayout) {
678 mContactDrawer.setDragView(relativeLayout);
679 }
680
Alexandre Lision269e6042013-12-11 16:15:13 -0500681 @Override
682 public void onSectionSelected(int pos) {
Alexandre Lision269e6042013-12-11 16:15:13 -0500683 switch (pos) {
684 case 0:
Alexandre Lision18e30542013-12-13 11:32:07 -0500685
686 if (fContent instanceof HomeFragment)
687 break;
688
689 BackStackEntry entry = getSupportFragmentManager().getBackStackEntryAt(0);
690 fContent = getSupportFragmentManager().findFragmentByTag(entry.getName());
691 getSupportFragmentManager().popBackStack(null, FragmentManager.POP_BACK_STACK_INCLUSIVE);
692
Alexandre Lision269e6042013-12-11 16:15:13 -0500693 break;
694 case 1:
Alexandre Lision18e30542013-12-13 11:32:07 -0500695 if (fContent instanceof AccountsManagementFragment)
696 break;
697 Log.i(TAG, "BackStackEntries: " + getSupportFragmentManager().getBackStackEntryCount());
Alexandre Lisionf02190d2013-12-12 17:26:12 -0500698 fContent = new AccountsManagementFragment();
Alexandre Lision18e30542013-12-13 11:32:07 -0500699 getSupportFragmentManager().beginTransaction().replace(R.id.main_frame, fContent, "Home").addToBackStack("Home").commit();
Alexandre Lisionf02190d2013-12-12 17:26:12 -0500700 break;
701 case 2:
Alexandre Lision18e30542013-12-13 11:32:07 -0500702 if (fContent instanceof AboutFragment)
703 break;
Alexandre Lisionf02190d2013-12-12 17:26:12 -0500704 fContent = new AboutFragment();
Alexandre Lision18e30542013-12-13 11:32:07 -0500705 getSupportFragmentManager().beginTransaction().replace(R.id.main_frame, fContent, "Home").addToBackStack("Home").commit();
Alexandre Lision269e6042013-12-11 16:15:13 -0500706 break;
707 }
Alexandre Lisionf02190d2013-12-12 17:26:12 -0500708
709 mNavigationDrawer.closeDrawers();
Alexandre Lision269e6042013-12-11 16:15:13 -0500710 }
711
alisionf76de3b2013-04-16 15:35:22 -0400712}