alision | f76de3b | 2013-04-16 15:35:22 -0400 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2004-2012 Savoir-Faire Linux Inc. |
| 3 | * |
| 4 | * Author: Adrien Beraud <adrien.beraud@gmail.com> |
alision | 11e8e16 | 2013-05-28 10:33:14 -0400 | [diff] [blame^] | 5 | * Alexandre Lision <alexandre.lision@savoirfairelinux.com> |
alision | f76de3b | 2013-04-16 15:35:22 -0400 | [diff] [blame] | 6 | * |
| 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 | */ |
| 32 | package com.savoirfairelinux.sflphone.client; |
| 33 | |
alision | 84813a1 | 2013-05-27 17:40:39 -0400 | [diff] [blame] | 34 | import java.util.Random; |
| 35 | |
alision | f76de3b | 2013-04-16 15:35:22 -0400 | [diff] [blame] | 36 | import android.app.ActionBar; |
| 37 | import android.app.Activity; |
| 38 | import android.app.Fragment; |
alision | f76de3b | 2013-04-16 15:35:22 -0400 | [diff] [blame] | 39 | import android.app.FragmentTransaction; |
| 40 | import android.content.ComponentName; |
| 41 | import android.content.Context; |
| 42 | import android.content.Intent; |
alision | 84813a1 | 2013-05-27 17:40:39 -0400 | [diff] [blame] | 43 | import android.content.IntentFilter; |
alision | f76de3b | 2013-04-16 15:35:22 -0400 | [diff] [blame] | 44 | import android.content.ServiceConnection; |
| 45 | import android.os.Bundle; |
| 46 | import android.os.IBinder; |
alision | 5f89963 | 2013-04-22 17:26:56 -0400 | [diff] [blame] | 47 | import android.os.RemoteException; |
alision | 9f7a6ec | 2013-05-24 16:26:26 -0400 | [diff] [blame] | 48 | import android.support.v4.app.ActionBarDrawerToggle; |
| 49 | import android.support.v4.view.GravityCompat; |
alision | f76de3b | 2013-04-16 15:35:22 -0400 | [diff] [blame] | 50 | import android.support.v4.view.ViewPager; |
alision | 9f7a6ec | 2013-05-24 16:26:26 -0400 | [diff] [blame] | 51 | import android.support.v4.widget.DrawerLayout; |
alision | f76de3b | 2013-04-16 15:35:22 -0400 | [diff] [blame] | 52 | import android.util.Log; |
alision | 9f7a6ec | 2013-05-24 16:26:26 -0400 | [diff] [blame] | 53 | import android.view.MenuItem; |
| 54 | import android.view.View; |
alision | f76de3b | 2013-04-16 15:35:22 -0400 | [diff] [blame] | 55 | import android.widget.ImageButton; |
alision | 7297bdb | 2013-05-21 11:56:55 -0400 | [diff] [blame] | 56 | import android.widget.RelativeLayout; |
alision | 04a0018 | 2013-05-10 17:05:29 -0400 | [diff] [blame] | 57 | import android.widget.Toast; |
alision | f76de3b | 2013-04-16 15:35:22 -0400 | [diff] [blame] | 58 | |
| 59 | import com.savoirfairelinux.sflphone.R; |
alision | 9f7a6ec | 2013-05-24 16:26:26 -0400 | [diff] [blame] | 60 | import com.savoirfairelinux.sflphone.adapters.SectionsPagerAdapter; |
alision | 84813a1 | 2013-05-27 17:40:39 -0400 | [diff] [blame] | 61 | import com.savoirfairelinux.sflphone.client.receiver.CallReceiver; |
alision | f76de3b | 2013-04-16 15:35:22 -0400 | [diff] [blame] | 62 | import com.savoirfairelinux.sflphone.fragments.CallElementListFragment; |
| 63 | import com.savoirfairelinux.sflphone.fragments.ContactListFragment; |
alision | 7297bdb | 2013-05-21 11:56:55 -0400 | [diff] [blame] | 64 | import com.savoirfairelinux.sflphone.fragments.DialingFragment; |
alision | d9e2944 | 2013-04-17 16:10:18 -0400 | [diff] [blame] | 65 | import com.savoirfairelinux.sflphone.fragments.HistoryFragment; |
alision | 9f7a6ec | 2013-05-24 16:26:26 -0400 | [diff] [blame] | 66 | import com.savoirfairelinux.sflphone.fragments.MenuFragment; |
alision | 84813a1 | 2013-05-27 17:40:39 -0400 | [diff] [blame] | 67 | import com.savoirfairelinux.sflphone.interfaces.CallInterface; |
| 68 | import com.savoirfairelinux.sflphone.model.CallContact; |
| 69 | import com.savoirfairelinux.sflphone.model.CallContact.ContactBuilder; |
alision | f76de3b | 2013-04-16 15:35:22 -0400 | [diff] [blame] | 70 | import com.savoirfairelinux.sflphone.model.SipCall; |
alision | 84813a1 | 2013-05-27 17:40:39 -0400 | [diff] [blame] | 71 | import com.savoirfairelinux.sflphone.service.CallManagerCallBack; |
alision | f76de3b | 2013-04-16 15:35:22 -0400 | [diff] [blame] | 72 | import com.savoirfairelinux.sflphone.service.ISipService; |
| 73 | import com.savoirfairelinux.sflphone.service.SipService; |
alision | 7297bdb | 2013-05-21 11:56:55 -0400 | [diff] [blame] | 74 | import com.savoirfairelinux.sflphone.views.CustomSlidingDrawer; |
alision | f76de3b | 2013-04-16 15:35:22 -0400 | [diff] [blame] | 75 | |
alision | 84813a1 | 2013-05-27 17:40:39 -0400 | [diff] [blame] | 76 | public class SFLPhoneHomeActivity extends Activity implements ActionBar.TabListener, DialingFragment.Callbacks, ContactListFragment.Callbacks, |
| 77 | CallElementListFragment.Callbacks, HistoryFragment.Callbacks, CallInterface { |
alision | 9f7a6ec | 2013-05-24 16:26:26 -0400 | [diff] [blame] | 78 | SectionsPagerAdapter mSectionsPagerAdapter = null; |
alision | 84813a1 | 2013-05-27 17:40:39 -0400 | [diff] [blame] | 79 | static final String TAG = "SFLPhoneHomeActivity"; |
alision | f76de3b | 2013-04-16 15:35:22 -0400 | [diff] [blame] | 80 | |
alision | 9f7a6ec | 2013-05-24 16:26:26 -0400 | [diff] [blame] | 81 | ImageButton buttonCall, buttonHangup; |
| 82 | private ContactListFragment mContactsFragment = null; |
| 83 | private DialingFragment mDialingFragment = null; |
| 84 | private CallElementListFragment mCallElementList = null; |
| 85 | private HistoryFragment mHistorySectionFragment = null; |
alision | f76de3b | 2013-04-16 15:35:22 -0400 | [diff] [blame] | 86 | |
alision | 9f7a6ec | 2013-05-24 16:26:26 -0400 | [diff] [blame] | 87 | Fragment fMenu; |
alision | f76de3b | 2013-04-16 15:35:22 -0400 | [diff] [blame] | 88 | |
alision | 9f7a6ec | 2013-05-24 16:26:26 -0400 | [diff] [blame] | 89 | private boolean mBound = false; |
| 90 | private ISipService service; |
alision | f76de3b | 2013-04-16 15:35:22 -0400 | [diff] [blame] | 91 | |
alision | 9f7a6ec | 2013-05-24 16:26:26 -0400 | [diff] [blame] | 92 | private CharSequence mDrawerTitle; |
| 93 | private CharSequence mTitle; |
alision | f76de3b | 2013-04-16 15:35:22 -0400 | [diff] [blame] | 94 | |
alision | 9f7a6ec | 2013-05-24 16:26:26 -0400 | [diff] [blame] | 95 | private static final int REQUEST_CODE_PREFERENCES = 1; |
| 96 | private static final int REQUEST_CODE_CALL = 2; |
alision | f76de3b | 2013-04-16 15:35:22 -0400 | [diff] [blame] | 97 | |
alision | 9f7a6ec | 2013-05-24 16:26:26 -0400 | [diff] [blame] | 98 | private static final int ACTION_BAR_TAB_DIALING = 0; |
| 99 | private static final int ACTION_BAR_TAB_CALL = 1; |
| 100 | private static final int ACTION_BAR_TAB_HISTORY = 2; |
alision | f76de3b | 2013-04-16 15:35:22 -0400 | [diff] [blame] | 101 | |
alision | 9f7a6ec | 2013-05-24 16:26:26 -0400 | [diff] [blame] | 102 | RelativeLayout mSliderButton; |
| 103 | CustomSlidingDrawer mDrawer; |
alision | 9f7a6ec | 2013-05-24 16:26:26 -0400 | [diff] [blame] | 104 | private DrawerLayout mDrawerLayout; |
alision | 9f7a6ec | 2013-05-24 16:26:26 -0400 | [diff] [blame] | 105 | private ActionBarDrawerToggle mDrawerToggle; |
| 106 | /** |
| 107 | * The {@link ViewPager} that will host the section contents. |
| 108 | */ |
| 109 | ViewPager mViewPager; |
alision | f76de3b | 2013-04-16 15:35:22 -0400 | [diff] [blame] | 110 | |
alision | 84813a1 | 2013-05-27 17:40:39 -0400 | [diff] [blame] | 111 | CallReceiver receiver; |
| 112 | |
alision | 9f7a6ec | 2013-05-24 16:26:26 -0400 | [diff] [blame] | 113 | final private int[] icon_res_id = { R.drawable.ic_tab_call, R.drawable.ic_tab_call, R.drawable.ic_tab_history }; |
alision | f76de3b | 2013-04-16 15:35:22 -0400 | [diff] [blame] | 114 | |
alision | 9f7a6ec | 2013-05-24 16:26:26 -0400 | [diff] [blame] | 115 | // public SFLPhoneHome extends Activity implements ActionBar.TabListener, OnClickListener |
| 116 | |
| 117 | /* called before activity is killed, e.g. rotation */ |
| 118 | @Override |
| 119 | protected void onSaveInstanceState(Bundle bundle) { |
| 120 | super.onSaveInstanceState(bundle); |
| 121 | for (int i = 0; i < mSectionsPagerAdapter.getCount(); i++) { |
| 122 | try { |
| 123 | getFragmentManager().putFragment(bundle, mSectionsPagerAdapter.getClassName(i), mSectionsPagerAdapter.getItem(i)); |
| 124 | } catch (IllegalStateException e) { |
| 125 | Log.e(TAG, "fragment=" + mSectionsPagerAdapter.getItem(i)); |
| 126 | } |
| 127 | } |
| 128 | |
| 129 | getFragmentManager().putFragment(bundle, "ContactsListFragment", mContactsFragment); |
| 130 | Log.w(TAG, "onSaveInstanceState()"); |
| 131 | } |
| 132 | |
| 133 | @Override |
| 134 | public void onCreate(Bundle savedInstanceState) { |
| 135 | super.onCreate(savedInstanceState); |
| 136 | |
alision | 84813a1 | 2013-05-27 17:40:39 -0400 | [diff] [blame] | 137 | receiver = new CallReceiver(this); |
| 138 | |
alision | 9f7a6ec | 2013-05-24 16:26:26 -0400 | [diff] [blame] | 139 | String libraryPath = getApplicationInfo().dataDir + "/lib"; |
| 140 | Log.i(TAG, libraryPath); |
| 141 | |
| 142 | // Bind to LocalService |
| 143 | if (!mBound) { |
| 144 | Log.i(TAG, "onStart: Binding service..."); |
| 145 | Intent intent = new Intent(this, SipService.class); |
| 146 | bindService(intent, mConnection, Context.BIND_AUTO_CREATE); |
| 147 | } |
| 148 | |
| 149 | setContentView(R.layout.activity_sflphone_home); |
| 150 | |
| 151 | if (mSectionsPagerAdapter == null) { |
| 152 | mSectionsPagerAdapter = new SectionsPagerAdapter(this, getFragmentManager()); |
| 153 | } |
| 154 | |
| 155 | /* getFragment(Bundle, String) */ |
| 156 | if (savedInstanceState != null) { |
| 157 | Log.w(TAG, "Activity restarted, recreating PagerAdapter..."); |
| 158 | /* getFragment (Bundle bundle, String key) */ |
| 159 | mDialingFragment = (DialingFragment) getFragmentManager().getFragment(savedInstanceState, |
| 160 | mSectionsPagerAdapter.getClassName(ACTION_BAR_TAB_DIALING)); |
| 161 | mCallElementList = (CallElementListFragment) getFragmentManager().getFragment(savedInstanceState, |
| 162 | mSectionsPagerAdapter.getClassName(ACTION_BAR_TAB_CALL)); |
| 163 | mHistorySectionFragment = (HistoryFragment) getFragmentManager().getFragment(savedInstanceState, |
| 164 | mSectionsPagerAdapter.getClassName(ACTION_BAR_TAB_HISTORY)); |
| 165 | } |
| 166 | |
| 167 | if (mDialingFragment == null) { |
| 168 | mDialingFragment = new DialingFragment(); |
alision | 7297bdb | 2013-05-21 11:56:55 -0400 | [diff] [blame] | 169 | Log.w(TAG, "Recreated mDialingFragment=" + mDialingFragment); |
alision | 9f7a6ec | 2013-05-24 16:26:26 -0400 | [diff] [blame] | 170 | } |
alision | f76de3b | 2013-04-16 15:35:22 -0400 | [diff] [blame] | 171 | |
alision | 9f7a6ec | 2013-05-24 16:26:26 -0400 | [diff] [blame] | 172 | if (mContactsFragment == null) { |
| 173 | mContactsFragment = new ContactListFragment(); |
| 174 | Log.w(TAG, "Recreated mContactListFragment=" + mContactsFragment); |
| 175 | getFragmentManager().beginTransaction().replace(R.id.contacts_frame, mContactsFragment).commit(); |
| 176 | } |
| 177 | if (mCallElementList == null) { |
| 178 | mCallElementList = new CallElementListFragment(); |
| 179 | Log.w(TAG, "Recreated mCallElementList=" + mCallElementList); |
| 180 | } |
| 181 | if (mHistorySectionFragment == null) { |
| 182 | mHistorySectionFragment = new HistoryFragment(); |
| 183 | Log.w(TAG, "Recreated mHistorySectionFragment=" + mHistorySectionFragment); |
| 184 | } |
| 185 | |
| 186 | mDrawer = (CustomSlidingDrawer) findViewById(R.id.custom_sliding_drawer); |
alision | 7297bdb | 2013-05-21 11:56:55 -0400 | [diff] [blame] | 187 | |
alision | 84813a1 | 2013-05-27 17:40:39 -0400 | [diff] [blame] | 188 | mContactsFragment.setHandleView((RelativeLayout) findViewById(R.id.slider_button)); |
alision | 7297bdb | 2013-05-21 11:56:55 -0400 | [diff] [blame] | 189 | |
| 190 | mDrawer.setmTrackHandle(findViewById(R.id.handle_title)); |
alision | f76de3b | 2013-04-16 15:35:22 -0400 | [diff] [blame] | 191 | |
alision | 9f7a6ec | 2013-05-24 16:26:26 -0400 | [diff] [blame] | 192 | final ActionBar actionBar = getActionBar(); |
| 193 | actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS); |
alision | f76de3b | 2013-04-16 15:35:22 -0400 | [diff] [blame] | 194 | |
alision | 9f7a6ec | 2013-05-24 16:26:26 -0400 | [diff] [blame] | 195 | // Set up the ViewPager with the sections adapter. |
| 196 | mViewPager = (ViewPager) findViewById(R.id.pager); |
| 197 | mViewPager.setAdapter(mSectionsPagerAdapter); |
alision | f76de3b | 2013-04-16 15:35:22 -0400 | [diff] [blame] | 198 | |
alision | 9f7a6ec | 2013-05-24 16:26:26 -0400 | [diff] [blame] | 199 | mTitle = mDrawerTitle = getTitle(); |
| 200 | mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout); |
| 201 | // mDrawerList = (ListView) findViewById(R.id.left_drawer); |
alision | f76de3b | 2013-04-16 15:35:22 -0400 | [diff] [blame] | 202 | |
alision | 9f7a6ec | 2013-05-24 16:26:26 -0400 | [diff] [blame] | 203 | // set a custom shadow that overlays the main content when the drawer opens |
| 204 | mDrawerLayout.setDrawerShadow(R.drawable.drawer_shadow, GravityCompat.START); |
| 205 | // set up the drawer's list view with items and click listener |
| 206 | // mDrawerList.setAdapter(new ArrayAdapter<String>(this, R.layout.drawer_list_item, mPlanetTitles)); |
| 207 | // mDrawerList.setOnItemClickListener(new DrawerItemClickListener()); |
alision | f76de3b | 2013-04-16 15:35:22 -0400 | [diff] [blame] | 208 | |
alision | 9f7a6ec | 2013-05-24 16:26:26 -0400 | [diff] [blame] | 209 | getActionBar().setDisplayHomeAsUpEnabled(true); |
| 210 | getActionBar().setHomeButtonEnabled(true); |
alision | f76de3b | 2013-04-16 15:35:22 -0400 | [diff] [blame] | 211 | |
alision | 9f7a6ec | 2013-05-24 16:26:26 -0400 | [diff] [blame] | 212 | mDrawerToggle = new ActionBarDrawerToggle(this, /* host Activity */ |
| 213 | mDrawerLayout, /* DrawerLayout object */ |
| 214 | R.drawable.ic_drawer, /* nav drawer image to replace 'Up' caret */ |
| 215 | R.string.drawer_open, /* "open drawer" description for accessibility */ |
| 216 | R.string.drawer_close /* "close drawer" description for accessibility */ |
| 217 | ) { |
| 218 | public void onDrawerClosed(View view) { |
| 219 | getActionBar().setTitle(mTitle); |
| 220 | invalidateOptionsMenu(); // creates call to onPrepareOptionsMenu() |
| 221 | } |
alision | f76de3b | 2013-04-16 15:35:22 -0400 | [diff] [blame] | 222 | |
alision | 9f7a6ec | 2013-05-24 16:26:26 -0400 | [diff] [blame] | 223 | public void onDrawerOpened(View drawerView) { |
| 224 | getActionBar().setTitle(mDrawerTitle); |
| 225 | invalidateOptionsMenu(); // creates call to onPrepareOptionsMenu() |
| 226 | } |
| 227 | }; |
alision | f76de3b | 2013-04-16 15:35:22 -0400 | [diff] [blame] | 228 | |
alision | 9f7a6ec | 2013-05-24 16:26:26 -0400 | [diff] [blame] | 229 | mDrawerLayout.setDrawerListener(mDrawerToggle); |
alision | f76de3b | 2013-04-16 15:35:22 -0400 | [diff] [blame] | 230 | |
alision | 9f7a6ec | 2013-05-24 16:26:26 -0400 | [diff] [blame] | 231 | mViewPager.setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() { |
| 232 | @Override |
| 233 | public void onPageSelected(int position) { |
| 234 | actionBar.setSelectedNavigationItem(position); |
| 235 | } |
| 236 | }); |
alision | f76de3b | 2013-04-16 15:35:22 -0400 | [diff] [blame] | 237 | |
alision | 9f7a6ec | 2013-05-24 16:26:26 -0400 | [diff] [blame] | 238 | // For each of the sections in the app, add a tab to the action bar. |
| 239 | for (int i = 0; i < mSectionsPagerAdapter.getCount(); i++) { |
alision | f76de3b | 2013-04-16 15:35:22 -0400 | [diff] [blame] | 240 | |
alision | 9f7a6ec | 2013-05-24 16:26:26 -0400 | [diff] [blame] | 241 | actionBar.addTab(actionBar.newTab().setIcon(icon_res_id[i]).setText(mSectionsPagerAdapter.getPageTitle(i)).setTabListener(this)); |
| 242 | } |
alision | f76de3b | 2013-04-16 15:35:22 -0400 | [diff] [blame] | 243 | |
alision | 9f7a6ec | 2013-05-24 16:26:26 -0400 | [diff] [blame] | 244 | actionBar.setSelectedNavigationItem(ACTION_BAR_TAB_CALL); |
alision | f76de3b | 2013-04-16 15:35:22 -0400 | [diff] [blame] | 245 | |
alision | 9f7a6ec | 2013-05-24 16:26:26 -0400 | [diff] [blame] | 246 | fMenu = new MenuFragment(); |
| 247 | getFragmentManager().beginTransaction().replace(R.id.left_drawer, fMenu).commit(); |
| 248 | } |
alision | f76de3b | 2013-04-16 15:35:22 -0400 | [diff] [blame] | 249 | |
alision | 9f7a6ec | 2013-05-24 16:26:26 -0400 | [diff] [blame] | 250 | @Override |
| 251 | protected void onStart() { |
| 252 | Log.i(TAG, "onStart"); |
| 253 | super.onStart(); |
| 254 | } |
alision | f76de3b | 2013-04-16 15:35:22 -0400 | [diff] [blame] | 255 | |
alision | 9f7a6ec | 2013-05-24 16:26:26 -0400 | [diff] [blame] | 256 | /* user gets back to the activity, e.g. through task manager */ |
| 257 | @Override |
| 258 | protected void onRestart() { |
| 259 | super.onRestart(); |
| 260 | } |
alision | f76de3b | 2013-04-16 15:35:22 -0400 | [diff] [blame] | 261 | |
alision | 9f7a6ec | 2013-05-24 16:26:26 -0400 | [diff] [blame] | 262 | /* activity gets back to the foreground and user input */ |
| 263 | @Override |
| 264 | protected void onResume() { |
| 265 | Log.i(TAG, "onResume"); |
alision | 84813a1 | 2013-05-27 17:40:39 -0400 | [diff] [blame] | 266 | IntentFilter intentFilter = new IntentFilter(); |
| 267 | intentFilter.addAction(CallManagerCallBack.INCOMING_CALL); |
| 268 | intentFilter.addAction(CallManagerCallBack.INCOMING_TEXT); |
| 269 | intentFilter.addAction(CallManagerCallBack.CALL_STATE_CHANGED); |
| 270 | registerReceiver(receiver, intentFilter); |
alision | 9f7a6ec | 2013-05-24 16:26:26 -0400 | [diff] [blame] | 271 | super.onResume(); |
| 272 | } |
alision | e2a38e1 | 2013-04-25 14:20:20 -0400 | [diff] [blame] | 273 | |
alision | 9f7a6ec | 2013-05-24 16:26:26 -0400 | [diff] [blame] | 274 | /* activity no more in foreground */ |
| 275 | @Override |
| 276 | protected void onPause() { |
| 277 | super.onPause(); |
alision | 84813a1 | 2013-05-27 17:40:39 -0400 | [diff] [blame] | 278 | unregisterReceiver(receiver); |
alision | 9f7a6ec | 2013-05-24 16:26:26 -0400 | [diff] [blame] | 279 | } |
alision | f76de3b | 2013-04-16 15:35:22 -0400 | [diff] [blame] | 280 | |
alision | 9f7a6ec | 2013-05-24 16:26:26 -0400 | [diff] [blame] | 281 | /* activity is no longer visible */ |
| 282 | @Override |
| 283 | protected void onStop() { |
| 284 | super.onStop(); |
| 285 | } |
alision | f76de3b | 2013-04-16 15:35:22 -0400 | [diff] [blame] | 286 | |
alision | 9f7a6ec | 2013-05-24 16:26:26 -0400 | [diff] [blame] | 287 | /* activity finishes itself or is being killed by the system */ |
| 288 | @Override |
| 289 | protected void onDestroy() { |
| 290 | /* stop the service, if no other bound user, no need to check if it is running */ |
| 291 | if (mBound) { |
| 292 | Log.i(TAG, "onDestroy: Unbinding service..."); |
| 293 | unbindService(mConnection); |
| 294 | mBound = false; |
| 295 | } |
alision | 371b77e | 2013-04-23 14:51:26 -0400 | [diff] [blame] | 296 | |
alision | 9f7a6ec | 2013-05-24 16:26:26 -0400 | [diff] [blame] | 297 | super.onDestroy(); |
| 298 | } |
alision | 73424b6 | 2013-04-26 11:49:18 -0400 | [diff] [blame] | 299 | |
alision | 84813a1 | 2013-05-27 17:40:39 -0400 | [diff] [blame] | 300 | public void launchCallActivity(SipCall.CallInfo infos, String action) { |
alision | 9f7a6ec | 2013-05-24 16:26:26 -0400 | [diff] [blame] | 301 | Log.i(TAG, "Launch Call Activity"); |
| 302 | Bundle bundle = new Bundle(); |
alision | 84813a1 | 2013-05-27 17:40:39 -0400 | [diff] [blame] | 303 | bundle.putString("action", action); |
alision | 9f7a6ec | 2013-05-24 16:26:26 -0400 | [diff] [blame] | 304 | // bundle.putParcelable("CallContact", mListAdapter.getItem(pos)); |
| 305 | bundle.putParcelable("CallInfo", infos); |
| 306 | Intent intent = new Intent().setClass(this, CallActivity.class); |
| 307 | intent.putExtras(bundle); |
| 308 | startActivityForResult(intent, REQUEST_CODE_CALL); |
| 309 | } |
alision | f76de3b | 2013-04-16 15:35:22 -0400 | [diff] [blame] | 310 | |
alision | 9f7a6ec | 2013-05-24 16:26:26 -0400 | [diff] [blame] | 311 | /** Defines callbacks for service binding, passed to bindService() */ |
| 312 | private ServiceConnection mConnection = new ServiceConnection() { |
alision | 371b77e | 2013-04-23 14:51:26 -0400 | [diff] [blame] | 313 | |
alision | 9f7a6ec | 2013-05-24 16:26:26 -0400 | [diff] [blame] | 314 | @Override |
| 315 | public void onServiceConnected(ComponentName className, IBinder binder) { |
| 316 | service = ISipService.Stub.asInterface(binder); |
alision | e2a38e1 | 2013-04-25 14:20:20 -0400 | [diff] [blame] | 317 | |
alision | 9f7a6ec | 2013-05-24 16:26:26 -0400 | [diff] [blame] | 318 | mBound = true; |
| 319 | mCallElementList.onServiceSipBinded(service); |
| 320 | mHistorySectionFragment.onServiceSipBinded(service); |
| 321 | mDialingFragment.onServiceSipBinded(service); |
| 322 | Log.d(TAG, "Service connected service=" + service); |
alision | 371b77e | 2013-04-23 14:51:26 -0400 | [diff] [blame] | 323 | |
alision | 9f7a6ec | 2013-05-24 16:26:26 -0400 | [diff] [blame] | 324 | } |
alision | 5f89963 | 2013-04-22 17:26:56 -0400 | [diff] [blame] | 325 | |
alision | 9f7a6ec | 2013-05-24 16:26:26 -0400 | [diff] [blame] | 326 | @Override |
| 327 | public void onServiceDisconnected(ComponentName arg0) { |
alision | 5f89963 | 2013-04-22 17:26:56 -0400 | [diff] [blame] | 328 | |
alision | 9f7a6ec | 2013-05-24 16:26:26 -0400 | [diff] [blame] | 329 | mBound = false; |
| 330 | Log.d(TAG, "Service disconnected service=" + service); |
| 331 | } |
| 332 | }; |
alision | 5f89963 | 2013-04-22 17:26:56 -0400 | [diff] [blame] | 333 | |
alision | 9f7a6ec | 2013-05-24 16:26:26 -0400 | [diff] [blame] | 334 | @Override |
| 335 | public boolean onOptionsItemSelected(MenuItem item) { |
| 336 | Log.i(TAG, "onOptionsItemSelected " + item.getItemId()); |
alision | 5f89963 | 2013-04-22 17:26:56 -0400 | [diff] [blame] | 337 | |
alision | 9f7a6ec | 2013-05-24 16:26:26 -0400 | [diff] [blame] | 338 | if (mDrawerToggle.onOptionsItemSelected(item)) { |
| 339 | return true; |
| 340 | } |
alision | 5f89963 | 2013-04-22 17:26:56 -0400 | [diff] [blame] | 341 | |
alision | 9f7a6ec | 2013-05-24 16:26:26 -0400 | [diff] [blame] | 342 | return super.onOptionsItemSelected(item); |
| 343 | } |
alision | f76de3b | 2013-04-16 15:35:22 -0400 | [diff] [blame] | 344 | |
alision | 9f7a6ec | 2013-05-24 16:26:26 -0400 | [diff] [blame] | 345 | @Override |
| 346 | public void onActivityResult(int requestCode, int resultCode, Intent data) { |
| 347 | super.onActivityResult(requestCode, resultCode, data); |
alision | 84813a1 | 2013-05-27 17:40:39 -0400 | [diff] [blame] | 348 | |
| 349 | switch (requestCode) { |
alision | 9f7a6ec | 2013-05-24 16:26:26 -0400 | [diff] [blame] | 350 | case REQUEST_CODE_PREFERENCES: |
| 351 | mCallElementList.onServiceSipBinded(service); |
| 352 | break; |
| 353 | case REQUEST_CODE_CALL: |
alision | 84813a1 | 2013-05-27 17:40:39 -0400 | [diff] [blame] | 354 | |
alision | 9f7a6ec | 2013-05-24 16:26:26 -0400 | [diff] [blame] | 355 | break; |
| 356 | } |
alision | f76de3b | 2013-04-16 15:35:22 -0400 | [diff] [blame] | 357 | |
alision | 9f7a6ec | 2013-05-24 16:26:26 -0400 | [diff] [blame] | 358 | } |
alision | f76de3b | 2013-04-16 15:35:22 -0400 | [diff] [blame] | 359 | |
alision | 9f7a6ec | 2013-05-24 16:26:26 -0400 | [diff] [blame] | 360 | // @Override |
| 361 | // public boolean onCreateOptionsMenu(Menu menu) { |
| 362 | // getMenuInflater().inflate(R.menu.activity_sflphone_home, menu); |
| 363 | // return true; |
| 364 | // } |
alision | f76de3b | 2013-04-16 15:35:22 -0400 | [diff] [blame] | 365 | |
alision | 9f7a6ec | 2013-05-24 16:26:26 -0400 | [diff] [blame] | 366 | @Override |
| 367 | public void onTabUnselected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) { |
| 368 | } |
alision | f76de3b | 2013-04-16 15:35:22 -0400 | [diff] [blame] | 369 | |
alision | 9f7a6ec | 2013-05-24 16:26:26 -0400 | [diff] [blame] | 370 | @Override |
| 371 | public void onTabSelected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) { |
| 372 | // When the given tab is selected, switch to the corresponding page in the ViewPager. |
| 373 | mViewPager.setCurrentItem(tab.getPosition()); |
| 374 | } |
alision | f76de3b | 2013-04-16 15:35:22 -0400 | [diff] [blame] | 375 | |
alision | 9f7a6ec | 2013-05-24 16:26:26 -0400 | [diff] [blame] | 376 | @Override |
| 377 | public void onTabReselected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) { |
| 378 | // Log.d(TAG, "onTabReselected"); |
| 379 | } |
alision | f76de3b | 2013-04-16 15:35:22 -0400 | [diff] [blame] | 380 | |
alision | 9f7a6ec | 2013-05-24 16:26:26 -0400 | [diff] [blame] | 381 | public void processingHangUpAction() { |
| 382 | SipCall call = (SipCall) buttonHangup.getTag(); |
| 383 | if (call != null) |
| 384 | call.notifyServiceHangup(service); |
| 385 | } |
alision | f76de3b | 2013-04-16 15:35:22 -0400 | [diff] [blame] | 386 | |
alision | 9f7a6ec | 2013-05-24 16:26:26 -0400 | [diff] [blame] | 387 | @Override |
| 388 | public void onCallSelected(SipCall c) { |
alision | 84813a1 | 2013-05-27 17:40:39 -0400 | [diff] [blame] | 389 | // launchCallActivity(c.mCallInfo); |
alision | f76de3b | 2013-04-16 15:35:22 -0400 | [diff] [blame] | 390 | |
alision | 9f7a6ec | 2013-05-24 16:26:26 -0400 | [diff] [blame] | 391 | } |
alision | f76de3b | 2013-04-16 15:35:22 -0400 | [diff] [blame] | 392 | |
alision | 9f7a6ec | 2013-05-24 16:26:26 -0400 | [diff] [blame] | 393 | @Override |
| 394 | public ISipService getService() { |
| 395 | return service; |
| 396 | } |
alision | f76de3b | 2013-04-16 15:35:22 -0400 | [diff] [blame] | 397 | |
alision | 84813a1 | 2013-05-27 17:40:39 -0400 | [diff] [blame] | 398 | /** |
| 399 | * Interface implemented to handle incoming events |
| 400 | */ |
alision | 9f7a6ec | 2013-05-24 16:26:26 -0400 | [diff] [blame] | 401 | @Override |
alision | 84813a1 | 2013-05-27 17:40:39 -0400 | [diff] [blame] | 402 | public void incomingCall(Intent call) { |
| 403 | Toast.makeText(this, "New Call incoming", Toast.LENGTH_LONG).show(); |
| 404 | SipCall.CallInfo infos = new SipCall.CallInfo(call); |
| 405 | SipCall c = new SipCall(infos); |
alision | 9f7a6ec | 2013-05-24 16:26:26 -0400 | [diff] [blame] | 406 | mCallElementList.addCall(c); |
alision | 84813a1 | 2013-05-27 17:40:39 -0400 | [diff] [blame] | 407 | |
| 408 | CallContact.ContactBuilder builder = new ContactBuilder(); |
alision | 11e8e16 | 2013-05-28 10:33:14 -0400 | [diff] [blame^] | 409 | infos.contacts.add(builder.buildUnknownContact(infos.mPhone)); |
alision | 9f7a6ec | 2013-05-24 16:26:26 -0400 | [diff] [blame] | 410 | |
alision | 84813a1 | 2013-05-27 17:40:39 -0400 | [diff] [blame] | 411 | launchCallActivity(infos, CallManagerCallBack.INCOMING_CALL); |
| 412 | |
| 413 | } |
| 414 | |
| 415 | @Override |
| 416 | public void callStateChanged(Intent callState) { |
| 417 | Bundle b = callState.getBundleExtra("com.savoirfairelinux.sflphone.service.newstate"); |
| 418 | String cID = b.getString("CallID"); |
| 419 | String state = b.getString("State"); |
| 420 | Log.i(TAG, "callStateChanged" + cID + " " + state); |
| 421 | mCallElementList.updateCall(cID, state); |
| 422 | |
| 423 | } |
| 424 | |
| 425 | @Override |
| 426 | public void incomingText(Intent msg) { |
| 427 | Bundle b = msg.getBundleExtra("com.savoirfairelinux.sflphone.service.newtext"); |
| 428 | b.getString("CallID"); |
| 429 | String from = b.getString("From"); |
| 430 | String mess = b.getString("Msg"); |
| 431 | Toast.makeText(getApplicationContext(), "text from " + from + " : " + mess, Toast.LENGTH_LONG).show(); |
| 432 | } |
| 433 | |
| 434 | @Override |
| 435 | public void onContactSelected(CallContact c) { |
| 436 | String callID = Integer.toString(new Random().nextInt()); |
| 437 | SipCall.CallInfo info = new SipCall.CallInfo(); |
| 438 | info.mCallID = callID; |
| 439 | try { |
| 440 | info.mAccountID = service.getAccountList().get(1).toString(); |
| 441 | } catch (RemoteException e) { |
| 442 | Log.e(TAG, e.toString()); |
| 443 | } |
| 444 | info.mDisplayName = c.getmDisplayName(); |
| 445 | info.mPhone = c.getSipPhone().getNumber(); |
| 446 | info.mEmail = c.getmEmail(); |
| 447 | info.mCallType = SipCall.CALL_TYPE_OUTGOING; |
alision | 11e8e16 | 2013-05-28 10:33:14 -0400 | [diff] [blame^] | 448 | info.contacts.add(c); |
alision | 84813a1 | 2013-05-27 17:40:39 -0400 | [diff] [blame] | 449 | |
| 450 | launchCallActivity(info, "call"); |
| 451 | |
| 452 | } |
| 453 | |
| 454 | @Override |
| 455 | public void onCallDialed(String accountID, String to) { |
| 456 | Random random = new Random(); |
| 457 | String callID = Integer.toString(random.nextInt()); |
| 458 | SipCall.CallInfo info = new SipCall.CallInfo(); |
| 459 | |
| 460 | info.mCallID = callID; |
| 461 | info.mAccountID = accountID; |
| 462 | info.mDisplayName = "Username"; |
| 463 | info.mPhone = to; |
| 464 | info.mEmail = "username@xxxx.com"; |
| 465 | info.mCallType = SipCall.CALL_TYPE_OUTGOING; |
| 466 | |
| 467 | CallContact.ContactBuilder builder = new ContactBuilder(); |
| 468 | |
alision | 11e8e16 | 2013-05-28 10:33:14 -0400 | [diff] [blame^] | 469 | info.contacts.add(builder.buildUnknownContact(info.mPhone)); |
alision | 84813a1 | 2013-05-27 17:40:39 -0400 | [diff] [blame] | 470 | |
| 471 | launchCallActivity(info, "call"); |
| 472 | |
alision | 9f7a6ec | 2013-05-24 16:26:26 -0400 | [diff] [blame] | 473 | } |
alision | 371b77e | 2013-04-23 14:51:26 -0400 | [diff] [blame] | 474 | |
alision | f76de3b | 2013-04-16 15:35:22 -0400 | [diff] [blame] | 475 | } |