Adrien Béraud | ffd3241 | 2012-08-07 18:39:23 -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 | |
Emeric Vigier | e89b48b | 2012-08-30 14:16:46 -0400 | [diff] [blame] | 33 | import java.util.Random; |
| 34 | |
Adrien Béraud | ffd3241 | 2012-08-07 18:39:23 -0400 | [diff] [blame] | 35 | import android.app.ActionBar; |
| 36 | import android.app.Activity; |
Emeric Vigier | 12d61d8 | 2012-09-19 15:08:18 -0400 | [diff] [blame] | 37 | import android.app.AlertDialog; |
Adrien Béraud | ffd3241 | 2012-08-07 18:39:23 -0400 | [diff] [blame] | 38 | import android.app.Fragment; |
| 39 | import android.app.FragmentManager; |
| 40 | import android.app.FragmentTransaction; |
Emeric Vigier | 6119d78 | 2012-09-21 18:04:14 -0400 | [diff] [blame] | 41 | import android.content.ComponentName; |
| 42 | import android.content.Context; |
Alexandre Savard | 1bcaf53 | 2012-09-05 16:23:02 -0400 | [diff] [blame] | 43 | import android.content.Intent; |
Emeric Vigier | 6119d78 | 2012-09-21 18:04:14 -0400 | [diff] [blame] | 44 | import android.content.ServiceConnection; |
Adrien Béraud | ffd3241 | 2012-08-07 18:39:23 -0400 | [diff] [blame] | 45 | import android.os.Bundle; |
Emeric Vigier | 383b2a2 | 2012-08-27 14:20:05 -0400 | [diff] [blame] | 46 | import android.os.Handler; |
Emeric Vigier | 6119d78 | 2012-09-21 18:04:14 -0400 | [diff] [blame] | 47 | import android.os.IBinder; |
Emeric Vigier | 383b2a2 | 2012-08-27 14:20:05 -0400 | [diff] [blame] | 48 | import android.os.Message; |
Emeric Vigier | 6119d78 | 2012-09-21 18:04:14 -0400 | [diff] [blame] | 49 | import android.os.RemoteException; |
Adrien Béraud | ffd3241 | 2012-08-07 18:39:23 -0400 | [diff] [blame] | 50 | import android.support.v13.app.FragmentStatePagerAdapter; |
| 51 | import android.support.v4.view.ViewPager; |
Emeric Vigier | 05e894e | 2012-08-20 13:53:02 -0400 | [diff] [blame] | 52 | import android.util.Log; |
Adrien Béraud | ffd3241 | 2012-08-07 18:39:23 -0400 | [diff] [blame] | 53 | import android.view.Gravity; |
| 54 | import android.view.LayoutInflater; |
| 55 | import android.view.Menu; |
Alexandre Savard | 1bcaf53 | 2012-09-05 16:23:02 -0400 | [diff] [blame] | 56 | import android.view.MenuItem; |
Adrien Béraud | ffd3241 | 2012-08-07 18:39:23 -0400 | [diff] [blame] | 57 | import android.view.View; |
Emeric Vigier | 05e894e | 2012-08-20 13:53:02 -0400 | [diff] [blame] | 58 | import android.view.View.OnClickListener; |
Adrien Béraud | ffd3241 | 2012-08-07 18:39:23 -0400 | [diff] [blame] | 59 | import android.view.ViewGroup; |
Emeric Vigier | a6bdb70 | 2012-08-31 15:34:53 -0400 | [diff] [blame] | 60 | import android.view.animation.AlphaAnimation; |
| 61 | import android.view.animation.Animation; |
| 62 | import android.view.animation.LinearInterpolator; |
Emeric Vigier | eaf2c49 | 2012-09-19 14:38:20 -0400 | [diff] [blame] | 63 | import android.widget.Button; |
Emeric Vigier | d5c17f5 | 2012-08-29 09:29:33 -0400 | [diff] [blame] | 64 | import android.widget.EditText; |
Emeric Vigier | e89b48b | 2012-08-30 14:16:46 -0400 | [diff] [blame] | 65 | import android.widget.ImageButton; |
Adrien Béraud | ffd3241 | 2012-08-07 18:39:23 -0400 | [diff] [blame] | 66 | import android.widget.TextView; |
| 67 | |
| 68 | import com.savoirfairelinux.sflphone.R; |
Emeric Vigier | 6119d78 | 2012-09-21 18:04:14 -0400 | [diff] [blame] | 69 | import com.savoirfairelinux.sflphone.service.ISipService; |
Emeric Vigier | eaf2c49 | 2012-09-19 14:38:20 -0400 | [diff] [blame] | 70 | import com.savoirfairelinux.sflphone.service.SipService; |
Adrien Béraud | ffd3241 | 2012-08-07 18:39:23 -0400 | [diff] [blame] | 71 | |
Alexandre Savard | 7a902bc | 2012-10-04 16:32:35 -0400 | [diff] [blame] | 72 | import java.util.HashMap; |
| 73 | |
Emeric Vigier | 62ca14d | 2012-08-24 11:05:09 -0400 | [diff] [blame] | 74 | public class SFLPhoneHome extends Activity implements ActionBar.TabListener, OnClickListener |
Adrien Béraud | ffd3241 | 2012-08-07 18:39:23 -0400 | [diff] [blame] | 75 | { |
Alexandre Savard | 6c0584e | 2012-09-20 09:42:59 -0400 | [diff] [blame] | 76 | SectionsPagerAdapter mSectionsPagerAdapter; |
| 77 | static final String TAG = "SFLPhoneHome"; |
| 78 | private ButtonSectionFragment buttonFragment; |
Alexandre Savard | 6c0584e | 2012-09-20 09:42:59 -0400 | [diff] [blame] | 79 | /* default callID */ |
| 80 | static String callID = "007"; |
| 81 | static boolean callOnGoing = false; |
Emeric Vigier | eaf2c49 | 2012-09-19 14:38:20 -0400 | [diff] [blame] | 82 | static boolean serviceIsOn = false; |
Alexandre Savard | 6c0584e | 2012-09-20 09:42:59 -0400 | [diff] [blame] | 83 | private String incomingCallID = ""; |
| 84 | private static final int REQUEST_CODE_PREFERENCES = 1; |
| 85 | ImageButton buttonCall, buttonHangup; |
| 86 | Button buttonService; |
Emeric Vigier | 419ba7b | 2012-09-19 14:55:14 -0400 | [diff] [blame] | 87 | static Animation animation; |
Alexandre Savard | 6c0584e | 2012-09-20 09:42:59 -0400 | [diff] [blame] | 88 | ContactListFragment mContactListFragment; |
| 89 | CallElementList mCallElementList; |
Emeric Vigier | 6119d78 | 2012-09-21 18:04:14 -0400 | [diff] [blame] | 90 | private boolean mBound = false; |
| 91 | private ISipService service; |
Adrien Béraud | ffd3241 | 2012-08-07 18:39:23 -0400 | [diff] [blame] | 92 | |
Alexandre Savard | 6c0584e | 2012-09-20 09:42:59 -0400 | [diff] [blame] | 93 | /** |
| 94 | * The {@link ViewPager} that will host the section contents. |
| 95 | */ |
| 96 | ViewPager mViewPager; |
Emeric Vigier | bbde1ae | 2012-09-21 18:13:56 -0400 | [diff] [blame] | 97 | |
Alexandre Savard | 6c0584e | 2012-09-20 09:42:59 -0400 | [diff] [blame] | 98 | final private int[] icon_res_id = {R.drawable.ic_tab_call, R.drawable.ic_tab_call, R.drawable.ic_tab_history, R.drawable.ic_tab_play_selected}; |
Adrien Béraud | ffd3241 | 2012-08-07 18:39:23 -0400 | [diff] [blame] | 99 | |
Alexandre Savard | 817dc50 | 2012-10-22 11:47:29 -0400 | [diff] [blame^] | 100 | // public SFLPhoneHome extends Activity implements ActionBar.TabListener, OnClickListener |
| 101 | |
Alexandre Savard | 6c0584e | 2012-09-20 09:42:59 -0400 | [diff] [blame] | 102 | @Override |
| 103 | public void onCreate(Bundle savedInstanceState) |
| 104 | { |
| 105 | super.onCreate(savedInstanceState); |
Alexandre Savard | 817dc50 | 2012-10-22 11:47:29 -0400 | [diff] [blame^] | 106 | |
| 107 | if (!serviceIsOn) { |
| 108 | Log.i(TAG, "starting SipService"); |
| 109 | startSipService(); |
| 110 | } |
| 111 | |
| 112 | // Bind to LocalService |
| 113 | if (!mBound) { |
| 114 | Log.i(TAG, "onStart: Binding service..."); |
| 115 | Intent intent = new Intent(this, SipService.class); |
| 116 | bindService(intent, mConnection, Context.BIND_AUTO_CREATE); |
| 117 | } |
| 118 | |
Alexandre Savard | 6c0584e | 2012-09-20 09:42:59 -0400 | [diff] [blame] | 119 | setContentView(R.layout.activity_sflphone_home); |
Adrien Béraud | ffd3241 | 2012-08-07 18:39:23 -0400 | [diff] [blame] | 120 | |
Alexandre Savard | 6c0584e | 2012-09-20 09:42:59 -0400 | [diff] [blame] | 121 | mSectionsPagerAdapter = new SectionsPagerAdapter(getFragmentManager()); |
Adrien Béraud | ffd3241 | 2012-08-07 18:39:23 -0400 | [diff] [blame] | 122 | |
Alexandre Savard | 6c0584e | 2012-09-20 09:42:59 -0400 | [diff] [blame] | 123 | final ActionBar actionBar = getActionBar(); |
| 124 | actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS); |
| 125 | // final ActionBar actionBar = getActionBar(); |
Adrien Béraud | ffd3241 | 2012-08-07 18:39:23 -0400 | [diff] [blame] | 126 | |
Alexandre Savard | 6c0584e | 2012-09-20 09:42:59 -0400 | [diff] [blame] | 127 | // Set up the ViewPager with the sections adapter. |
| 128 | mViewPager = (ViewPager) findViewById(R.id.pager); |
| 129 | mViewPager.setAdapter(mSectionsPagerAdapter); |
Adrien Béraud | ffd3241 | 2012-08-07 18:39:23 -0400 | [diff] [blame] | 130 | |
Alexandre Savard | 6c0584e | 2012-09-20 09:42:59 -0400 | [diff] [blame] | 131 | // When swiping between different sections, select the corresponding tab. |
| 132 | // We can also use ActionBar.Tab#select() to do this if we have a reference to the |
| 133 | // Tab. |
| 134 | mViewPager.setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() |
| 135 | { |
| 136 | @Override |
| 137 | public void onPageSelected(int position) |
| 138 | { |
| 139 | actionBar.setSelectedNavigationItem(position); |
| 140 | } |
| 141 | }); |
Emeric Vigier | 383b2a2 | 2012-08-27 14:20:05 -0400 | [diff] [blame] | 142 | |
Alexandre Savard | 6c0584e | 2012-09-20 09:42:59 -0400 | [diff] [blame] | 143 | // For each of the sections in the app, add a tab to the action bar. |
| 144 | for (int i = 0; i < mSectionsPagerAdapter.getCount(); i++) { |
| 145 | // Create a tab with text corresponding to the page title defined by the adapter. |
| 146 | // Also specify this Activity object, which implements the TabListener interface, as the |
| 147 | // listener for when this tab is selected. |
| 148 | Log.i(TAG, "adding tab: " + i); |
| 149 | actionBar.addTab(actionBar.newTab().setIcon(icon_res_id[i]).setText(mSectionsPagerAdapter.getPageTitle(i)).setTabListener(this)); |
| 150 | } |
Emeric Vigier | 383b2a2 | 2012-08-27 14:20:05 -0400 | [diff] [blame] | 151 | |
Alexandre Savard | 6c0584e | 2012-09-20 09:42:59 -0400 | [diff] [blame] | 152 | buttonCall = (ImageButton) findViewById(R.id.buttonCall); |
| 153 | buttonHangup = (ImageButton) findViewById(R.id.buttonHangUp); |
Alexandre Savard | def3bfa | 2012-09-12 16:20:06 -0400 | [diff] [blame] | 154 | |
Emeric Vigier | 419ba7b | 2012-09-19 14:55:14 -0400 | [diff] [blame] | 155 | // Change alpha from fully visible to invisible |
| 156 | animation = new AlphaAnimation(1, 0); |
| 157 | // duration - half a second |
| 158 | animation.setDuration(500); |
| 159 | // do not alter animation rate |
| 160 | animation.setInterpolator(new LinearInterpolator()); |
| 161 | // Repeat animation infinitely |
| 162 | animation.setRepeatCount(Animation.INFINITE); |
| 163 | // Reverse |
| 164 | animation.setRepeatMode(Animation.REVERSE); |
Alexandre Savard | 6c0584e | 2012-09-20 09:42:59 -0400 | [diff] [blame] | 165 | } |
Alexandre Savard | 1bcaf53 | 2012-09-05 16:23:02 -0400 | [diff] [blame] | 166 | |
Emeric Vigier | 12d61d8 | 2012-09-19 15:08:18 -0400 | [diff] [blame] | 167 | @Override |
| 168 | protected void onStart() { |
| 169 | Log.i(TAG, "onStart"); |
| 170 | super.onStart(); |
| 171 | } |
Alexandre Savard | 1bcaf53 | 2012-09-05 16:23:02 -0400 | [diff] [blame] | 172 | |
Emeric Vigier | 6119d78 | 2012-09-21 18:04:14 -0400 | [diff] [blame] | 173 | /* user gets back to the activity, e.g. through task manager */ |
Emeric Vigier | 12d61d8 | 2012-09-19 15:08:18 -0400 | [diff] [blame] | 174 | @Override |
| 175 | protected void onRestart() { |
| 176 | super.onRestart(); |
| 177 | } |
| 178 | |
Emeric Vigier | 6119d78 | 2012-09-21 18:04:14 -0400 | [diff] [blame] | 179 | /* activity gets back to the foreground and user input */ |
Emeric Vigier | 12d61d8 | 2012-09-19 15:08:18 -0400 | [diff] [blame] | 180 | @Override |
| 181 | protected void onResume() { |
| 182 | Log.i(TAG, "onResume"); |
| 183 | super.onResume(); |
Emeric Vigier | 12d61d8 | 2012-09-19 15:08:18 -0400 | [diff] [blame] | 184 | } |
| 185 | |
Emeric Vigier | 6119d78 | 2012-09-21 18:04:14 -0400 | [diff] [blame] | 186 | /* activity no more in foreground */ |
Emeric Vigier | 12d61d8 | 2012-09-19 15:08:18 -0400 | [diff] [blame] | 187 | @Override |
| 188 | protected void onPause() { |
Emeric Vigier | 12d61d8 | 2012-09-19 15:08:18 -0400 | [diff] [blame] | 189 | super.onPause(); |
| 190 | } |
| 191 | |
Emeric Vigier | 6119d78 | 2012-09-21 18:04:14 -0400 | [diff] [blame] | 192 | /* activity is no longer visible */ |
Emeric Vigier | 12d61d8 | 2012-09-19 15:08:18 -0400 | [diff] [blame] | 193 | @Override |
| 194 | protected void onStop() { |
| 195 | super.onStop(); |
Alexandre Savard | 817dc50 | 2012-10-22 11:47:29 -0400 | [diff] [blame^] | 196 | } |
| 197 | |
| 198 | /* activity finishes itself or is being killed by the system */ |
| 199 | @Override |
| 200 | protected void onDestroy() { |
Emeric Vigier | 6119d78 | 2012-09-21 18:04:14 -0400 | [diff] [blame] | 201 | /* stop the service, if no other bound user, no need to check if it is running */ |
| 202 | if (mBound) { |
Emeric Vigier | fae29bd | 2012-09-24 11:32:03 -0400 | [diff] [blame] | 203 | Log.i(TAG, "onStop: Unbinding service..."); |
Emeric Vigier | 6119d78 | 2012-09-21 18:04:14 -0400 | [diff] [blame] | 204 | unbindService(mConnection); |
| 205 | mBound = false; |
| 206 | } |
Emeric Vigier | fae29bd | 2012-09-24 11:32:03 -0400 | [diff] [blame] | 207 | Log.i(TAG, "onDestroy: stopping SipService..."); |
| 208 | stopService(new Intent(this, SipService.class)); |
Emeric Vigier | 6119d78 | 2012-09-21 18:04:14 -0400 | [diff] [blame] | 209 | serviceIsOn = false; |
Emeric Vigier | 12d61d8 | 2012-09-19 15:08:18 -0400 | [diff] [blame] | 210 | super.onDestroy(); |
| 211 | } |
| 212 | |
Emeric Vigier | 6119d78 | 2012-09-21 18:04:14 -0400 | [diff] [blame] | 213 | /** Defines callbacks for service binding, passed to bindService() */ |
| 214 | private ServiceConnection mConnection = new ServiceConnection() { |
| 215 | |
| 216 | @Override |
| 217 | public void onServiceConnected(ComponentName className, |
| 218 | IBinder binder) { |
| 219 | service = ISipService.Stub.asInterface(binder); |
| 220 | mBound = true; |
Alexandre Savard | 817dc50 | 2012-10-22 11:47:29 -0400 | [diff] [blame^] | 221 | mContactListFragment.setService(service); |
| 222 | mCallElementList.setService(service); |
Emeric Vigier | 6119d78 | 2012-09-21 18:04:14 -0400 | [diff] [blame] | 223 | Log.d(TAG, "Service connected"); |
| 224 | } |
| 225 | |
| 226 | @Override |
| 227 | public void onServiceDisconnected(ComponentName arg0) { |
| 228 | mBound = false; |
| 229 | Log.d(TAG, "Service disconnected"); |
| 230 | } |
| 231 | }; |
| 232 | |
Emeric Vigier | 12d61d8 | 2012-09-19 15:08:18 -0400 | [diff] [blame] | 233 | private void startSipService() { |
| 234 | Thread thread = new Thread("StartSFLphoneService") { |
| 235 | public void run() { |
| 236 | Intent sipServiceIntent = new Intent(SFLPhoneHome.this, SipService.class); |
| 237 | //sipServiceIntent.putExtra(ServiceConstants.EXTRA_OUTGOING_ACTIVITY, new ComponentName(SFLPhoneHome.this, SFLPhoneHome.class)); |
| 238 | startService(sipServiceIntent); |
Emeric Vigier | fae29bd | 2012-09-24 11:32:03 -0400 | [diff] [blame] | 239 | serviceIsOn = true; |
Emeric Vigier | 12d61d8 | 2012-09-19 15:08:18 -0400 | [diff] [blame] | 240 | }; |
| 241 | }; |
| 242 | try { |
| 243 | thread.start(); |
| 244 | } catch (IllegalThreadStateException e) { |
| 245 | AlertDialog.Builder builder = new AlertDialog.Builder(this); |
| 246 | builder.setMessage("Cannot start SFLPhone SipService!"); |
| 247 | AlertDialog alert = builder.create(); |
| 248 | alert.show(); |
| 249 | finish(); |
| 250 | } |
Emeric Vigier | 12d61d8 | 2012-09-19 15:08:18 -0400 | [diff] [blame] | 251 | } |
| 252 | |
| 253 | @Override |
| 254 | public boolean onOptionsItemSelected(MenuItem item) { |
| 255 | Log.i("SFLphone", "onOptionsItemSelected " + item.getItemId()); |
Emeric Vigier | bbde1ae | 2012-09-21 18:13:56 -0400 | [diff] [blame] | 256 | if(item.getItemId() != 0) { |
| 257 | // When the button is clicked, launch an activity through this intent |
| 258 | Intent launchPreferencesIntent = new Intent().setClass(this, SFLPhonePreferenceActivity.class); |
Alexandre Savard | 1bcaf53 | 2012-09-05 16:23:02 -0400 | [diff] [blame] | 259 | |
Emeric Vigier | bbde1ae | 2012-09-21 18:13:56 -0400 | [diff] [blame] | 260 | // Make it a subactivity so we know when it returns |
| 261 | startActivityForResult(launchPreferencesIntent, REQUEST_CODE_PREFERENCES); |
Alexandre Savard | 1bcaf53 | 2012-09-05 16:23:02 -0400 | [diff] [blame] | 262 | } |
Adrien Béraud | ffd3241 | 2012-08-07 18:39:23 -0400 | [diff] [blame] | 263 | |
Emeric Vigier | bbde1ae | 2012-09-21 18:13:56 -0400 | [diff] [blame] | 264 | return super.onOptionsItemSelected(item); |
| 265 | } |
Adrien Béraud | ffd3241 | 2012-08-07 18:39:23 -0400 | [diff] [blame] | 266 | |
Emeric Vigier | bbde1ae | 2012-09-21 18:13:56 -0400 | [diff] [blame] | 267 | @Override |
| 268 | public boolean onCreateOptionsMenu(Menu menu) |
| 269 | { |
| 270 | getMenuInflater().inflate(R.menu.activity_sflphone_home, menu); |
| 271 | return true; |
| 272 | } |
Adrien Béraud | ffd3241 | 2012-08-07 18:39:23 -0400 | [diff] [blame] | 273 | |
Emeric Vigier | bbde1ae | 2012-09-21 18:13:56 -0400 | [diff] [blame] | 274 | @Override |
| 275 | public void onTabUnselected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) |
| 276 | { |
| 277 | } |
Adrien Béraud | ffd3241 | 2012-08-07 18:39:23 -0400 | [diff] [blame] | 278 | |
Emeric Vigier | bbde1ae | 2012-09-21 18:13:56 -0400 | [diff] [blame] | 279 | @Override |
| 280 | public void onTabSelected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) |
| 281 | { |
| 282 | // When the given tab is selected, switch to the corresponding page in the ViewPager. |
| 283 | mViewPager.setCurrentItem(tab.getPosition()); |
| 284 | } |
| 285 | |
| 286 | @Override |
| 287 | public void onTabReselected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) |
| 288 | { |
| 289 | // Log.d(TAG, "onTabReselected"); |
| 290 | } |
| 291 | |
| 292 | public void setIncomingCallID(String accountID, String callID, String from) { |
Emeric Vigier | 419ba7b | 2012-09-19 14:55:14 -0400 | [diff] [blame] | 293 | Log.i(TAG, "incomingCall(" + accountID + ", " + callID + ", " + from + ")"); |
Emeric Vigier | bbde1ae | 2012-09-21 18:13:56 -0400 | [diff] [blame] | 294 | incomingCallID = callID; |
Emeric Vigier | 419ba7b | 2012-09-19 14:55:14 -0400 | [diff] [blame] | 295 | buttonCall.startAnimation(animation); |
| 296 | buttonCall.setImageResource(R.drawable.ic_incomingcall); |
Emeric Vigier | bbde1ae | 2012-09-21 18:13:56 -0400 | [diff] [blame] | 297 | } |
Adrien Béraud | ffd3241 | 2012-08-07 18:39:23 -0400 | [diff] [blame] | 298 | |
Emeric Vigier | bbde1ae | 2012-09-21 18:13:56 -0400 | [diff] [blame] | 299 | /** |
| 300 | * A {@link FragmentStatePagerAdapter} that returns a fragment corresponding to |
| 301 | * one of the primary sections of the app. |
| 302 | */ |
| 303 | public class SectionsPagerAdapter extends FragmentStatePagerAdapter |
| 304 | { |
Adrien Béraud | ffd3241 | 2012-08-07 18:39:23 -0400 | [diff] [blame] | 305 | |
Emeric Vigier | bbde1ae | 2012-09-21 18:13:56 -0400 | [diff] [blame] | 306 | public SectionsPagerAdapter(FragmentManager fm) |
| 307 | { |
| 308 | super(fm); |
| 309 | } |
Emeric Vigier | 05e894e | 2012-08-20 13:53:02 -0400 | [diff] [blame] | 310 | |
Emeric Vigier | bbde1ae | 2012-09-21 18:13:56 -0400 | [diff] [blame] | 311 | @Override |
| 312 | public Fragment getItem(int i) |
| 313 | { |
| 314 | Fragment fragment; |
Adrien Béraud | ffd3241 | 2012-08-07 18:39:23 -0400 | [diff] [blame] | 315 | |
Emeric Vigier | bbde1ae | 2012-09-21 18:13:56 -0400 | [diff] [blame] | 316 | switch (i) { |
| 317 | case 0: |
Alexandre Savard | 817dc50 | 2012-10-22 11:47:29 -0400 | [diff] [blame^] | 318 | mContactListFragment = new ContactListFragment(service); |
Emeric Vigier | bbde1ae | 2012-09-21 18:13:56 -0400 | [diff] [blame] | 319 | fragment = mContactListFragment; |
| 320 | break; |
| 321 | case 1: |
Alexandre Savard | 817dc50 | 2012-10-22 11:47:29 -0400 | [diff] [blame^] | 322 | mCallElementList = new CallElementList(service); |
Emeric Vigier | bbde1ae | 2012-09-21 18:13:56 -0400 | [diff] [blame] | 323 | SipCall.setCallElementList(mCallElementList); |
| 324 | fragment = mCallElementList; |
| 325 | break; |
| 326 | case 2: |
| 327 | fragment = new DummySectionFragment(); |
| 328 | break; |
| 329 | case 3: |
| 330 | fragment = new ButtonSectionFragment(); |
| 331 | Log.i(TAG, "getItem: fragment is " + fragment); |
| 332 | break; |
| 333 | default: |
| 334 | Log.e(TAG, "getItem: unknown tab position " + i); |
| 335 | return null; |
| 336 | } |
Adrien Béraud | ffd3241 | 2012-08-07 18:39:23 -0400 | [diff] [blame] | 337 | |
Emeric Vigier | bbde1ae | 2012-09-21 18:13:56 -0400 | [diff] [blame] | 338 | Bundle args = new Bundle(); |
| 339 | args.putInt(DummySectionFragment.ARG_SECTION_NUMBER, i + 1); |
| 340 | fragment.setArguments(args); |
| 341 | return fragment; |
| 342 | } |
Adrien Béraud | ffd3241 | 2012-08-07 18:39:23 -0400 | [diff] [blame] | 343 | |
Emeric Vigier | bbde1ae | 2012-09-21 18:13:56 -0400 | [diff] [blame] | 344 | @Override |
| 345 | public int getCount() |
| 346 | { |
| 347 | return 4; |
| 348 | } |
Adrien Béraud | ffd3241 | 2012-08-07 18:39:23 -0400 | [diff] [blame] | 349 | |
Emeric Vigier | bbde1ae | 2012-09-21 18:13:56 -0400 | [diff] [blame] | 350 | @Override |
| 351 | public CharSequence getPageTitle(int position) |
| 352 | { |
| 353 | switch (position) { |
| 354 | case 0: |
| 355 | return getString(R.string.title_section0).toUpperCase(); |
| 356 | case 1: |
| 357 | return getString(R.string.title_section1).toUpperCase(); |
| 358 | case 2: |
| 359 | return getString(R.string.title_section2).toUpperCase(); |
| 360 | case 3: |
| 361 | return getString(R.string.title_section3).toUpperCase(); |
| 362 | default: |
| 363 | Log.e(TAG, "getPageTitle: unknown tab position " + position); |
| 364 | break; |
| 365 | } |
| 366 | return null; |
| 367 | } |
| 368 | } |
Adrien Béraud | ffd3241 | 2012-08-07 18:39:23 -0400 | [diff] [blame] | 369 | |
Emeric Vigier | bbde1ae | 2012-09-21 18:13:56 -0400 | [diff] [blame] | 370 | /** |
| 371 | * A dummy fragment representing a section of the app, but that simply |
| 372 | * displays dummy text. |
| 373 | */ |
| 374 | public static class DummySectionFragment extends Fragment |
| 375 | { |
| 376 | public DummySectionFragment() |
| 377 | { |
| 378 | setRetainInstance(true); |
| 379 | } |
Emeric Vigier | 05e894e | 2012-08-20 13:53:02 -0400 | [diff] [blame] | 380 | |
Emeric Vigier | bbde1ae | 2012-09-21 18:13:56 -0400 | [diff] [blame] | 381 | public static final String ARG_SECTION_NUMBER = "section_number"; |
| 382 | |
| 383 | @Override |
| 384 | public View onCreateView(LayoutInflater inflater, ViewGroup parent, Bundle savedInstanceState) |
| 385 | { |
| 386 | TextView textView = new TextView(getActivity()); |
| 387 | textView.setGravity(Gravity.CENTER); |
| 388 | Bundle args = getArguments(); |
| 389 | textView.setText(Integer.toString(args.getInt(ARG_SECTION_NUMBER))); |
| 390 | textView.setText("java sucks"); |
| 391 | return textView; |
| 392 | } |
| 393 | } |
| 394 | |
| 395 | @Override |
Emeric Vigier | 62ca14d | 2012-08-24 11:05:09 -0400 | [diff] [blame] | 396 | public void onClick(View view) |
Emeric Vigier | e89b48b | 2012-08-30 14:16:46 -0400 | [diff] [blame] | 397 | { |
Alexandre Savard | 2c50e69 | 2012-09-24 11:18:31 -0400 | [diff] [blame] | 398 | Log.i("SFLPhoneHome", "onClic from SFLPhoneHome"); |
| 399 | |
Emeric Vigier | eaf2c49 | 2012-09-19 14:38:20 -0400 | [diff] [blame] | 400 | buttonService = (Button) findViewById(R.id.buttonService); |
Emeric Vigier | 6119d78 | 2012-09-21 18:04:14 -0400 | [diff] [blame] | 401 | |
| 402 | try { |
Emeric Vigier | bbde1ae | 2012-09-21 18:13:56 -0400 | [diff] [blame] | 403 | switch (view.getId()) { |
| 404 | case R.id.buttonCall: |
| 405 | TextView textView = (TextView) findViewById(R.id.editAccountID); |
| 406 | String accountID = textView.getText().toString(); |
| 407 | EditText editText; |
| 408 | Random random = new Random(); |
Emeric Vigier | e89b48b | 2012-08-30 14:16:46 -0400 | [diff] [blame] | 409 | |
Emeric Vigier | bbde1ae | 2012-09-21 18:13:56 -0400 | [diff] [blame] | 410 | if (incomingCallID != "") { |
| 411 | buttonCall.clearAnimation(); |
| 412 | service.accept(incomingCallID); |
| 413 | callID = incomingCallID; |
| 414 | incomingCallID=""; |
| 415 | callOnGoing = true; |
| 416 | buttonCall.setEnabled(false); |
| 417 | buttonHangup.setEnabled(true); |
| 418 | } else { |
| 419 | if (callOnGoing == false) { |
| 420 | editText = (EditText) findViewById(R.id.editTo); |
| 421 | String to = editText.getText().toString(); |
| 422 | if (to == null) { |
| 423 | Log.e(TAG, "to string is " + to); |
| 424 | break; |
| 425 | } |
Emeric Vigier | a6bdb70 | 2012-08-31 15:34:53 -0400 | [diff] [blame] | 426 | |
Emeric Vigier | bbde1ae | 2012-09-21 18:13:56 -0400 | [diff] [blame] | 427 | callID = Integer.toString(random.nextInt()); |
Emeric Vigier | a6bdb70 | 2012-08-31 15:34:53 -0400 | [diff] [blame] | 428 | |
Emeric Vigier | bbde1ae | 2012-09-21 18:13:56 -0400 | [diff] [blame] | 429 | Log.d(TAG, "service.placeCall(" + accountID + ", " + callID + ", " + to + ");"); |
| 430 | service.placeCall(accountID, callID, to); |
| 431 | callOnGoing = true; |
| 432 | buttonCall.setEnabled(false); |
| 433 | buttonHangup.setEnabled(true); |
| 434 | } |
| 435 | } |
| 436 | break; |
| 437 | case R.id.buttonHangUp: |
| 438 | if (incomingCallID != "") { |
| 439 | buttonCall.clearAnimation(); |
| 440 | Log.d(TAG, "service.refuse(" + incomingCallID + ");"); |
| 441 | service.refuse(incomingCallID); |
| 442 | incomingCallID=""; |
| 443 | buttonCall.setEnabled(true); |
| 444 | buttonHangup.setEnabled(true); |
| 445 | } else { |
| 446 | if (callOnGoing == true) { |
| 447 | Log.d(TAG, "service.hangUp(" + callID + ");"); |
| 448 | service.hangUp(callID); |
| 449 | callOnGoing = false; |
| 450 | buttonCall.setEnabled(true); |
| 451 | buttonHangup.setEnabled(false); |
| 452 | } |
| 453 | } |
Emeric Vigier | a6bdb70 | 2012-08-31 15:34:53 -0400 | [diff] [blame] | 454 | |
Emeric Vigier | bbde1ae | 2012-09-21 18:13:56 -0400 | [diff] [blame] | 455 | buttonCall.setImageResource(R.drawable.ic_call); |
| 456 | break; |
| 457 | case R.id.buttonInit: |
| 458 | Log.i(TAG, "R.id.buttonInit"); |
Alexandre Savard | 7a902bc | 2012-10-04 16:32:35 -0400 | [diff] [blame] | 459 | // String audioPlugin = service.getCurrentAudioOutputPlugin(); |
| 460 | HashMap<String, String> map = (HashMap<String, String>)service.getAccountDetails("Account:1345153770"); |
Emeric Vigier | bbde1ae | 2012-09-21 18:13:56 -0400 | [diff] [blame] | 461 | break; |
| 462 | case R.id.buttonService: |
| 463 | if (!serviceIsOn) { |
| 464 | startService(new Intent(this, SipService.class)); |
| 465 | serviceIsOn = true; |
| 466 | buttonService.setText("disable Service"); |
| 467 | } |
| 468 | else { |
| 469 | stopService(new Intent(this, SipService.class)); |
| 470 | serviceIsOn = false; |
| 471 | buttonService.setText("enable Service"); |
| 472 | } |
| 473 | break; |
Emeric Vigier | bbde1ae | 2012-09-21 18:13:56 -0400 | [diff] [blame] | 474 | default: |
| 475 | Log.w(TAG, "unknown button " + view.getId()); |
| 476 | break; |
Emeric Vigier | eaf2c49 | 2012-09-19 14:38:20 -0400 | [diff] [blame] | 477 | } |
Emeric Vigier | 6119d78 | 2012-09-21 18:04:14 -0400 | [diff] [blame] | 478 | } catch (RemoteException e) { |
| 479 | Log.e(TAG, "Cannot call service method", e); |
| 480 | } |
Emeric Vigier | bbde1ae | 2012-09-21 18:13:56 -0400 | [diff] [blame] | 481 | } |
Adrien Béraud | ffd3241 | 2012-08-07 18:39:23 -0400 | [diff] [blame] | 482 | } |