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