blob: 080c6e11692782e01c7b436efbfaadb121b16376 [file] [log] [blame]
Adrien Béraudffd32412012-08-07 18:39:23 -04001/*
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 */
31package com.savoirfairelinux.sflphone.client;
32
Emeric Vigiere89b48b2012-08-30 14:16:46 -040033import java.util.Random;
34
Adrien Béraudffd32412012-08-07 18:39:23 -040035import android.app.ActionBar;
36import android.app.Activity;
Emeric Vigier12d61d82012-09-19 15:08:18 -040037import android.app.AlertDialog;
Adrien Béraudffd32412012-08-07 18:39:23 -040038import android.app.Fragment;
39import android.app.FragmentManager;
40import android.app.FragmentTransaction;
Emeric Vigier6119d782012-09-21 18:04:14 -040041import android.content.ComponentName;
42import android.content.Context;
Alexandre Savard1bcaf532012-09-05 16:23:02 -040043import android.content.Intent;
Alexandre Savard74c1cad2012-10-24 16:39:00 -040044import android.content.IntentFilter;
Emeric Vigier6119d782012-09-21 18:04:14 -040045import android.content.ServiceConnection;
Adrien Béraudffd32412012-08-07 18:39:23 -040046import android.os.Bundle;
Emeric Vigier383b2a22012-08-27 14:20:05 -040047import android.os.Handler;
Emeric Vigier6119d782012-09-21 18:04:14 -040048import android.os.IBinder;
Emeric Vigier383b2a22012-08-27 14:20:05 -040049import android.os.Message;
Emeric Vigier6119d782012-09-21 18:04:14 -040050import android.os.RemoteException;
Adrien Béraudffd32412012-08-07 18:39:23 -040051import android.support.v13.app.FragmentStatePagerAdapter;
52import android.support.v4.view.ViewPager;
Alexandre Savard74c1cad2012-10-24 16:39:00 -040053import android.support.v4.content.LocalBroadcastManager;
Emeric Vigier05e894e2012-08-20 13:53:02 -040054import android.util.Log;
Adrien Béraudffd32412012-08-07 18:39:23 -040055import android.view.Gravity;
56import android.view.LayoutInflater;
57import android.view.Menu;
Alexandre Savard1bcaf532012-09-05 16:23:02 -040058import android.view.MenuItem;
Adrien Béraudffd32412012-08-07 18:39:23 -040059import android.view.View;
60import android.view.ViewGroup;
Alexandre Savarda949eec2012-10-25 17:30:49 -040061import android.view.View.OnClickListener;
Emeric Vigiera6bdb702012-08-31 15:34:53 -040062import android.view.animation.AlphaAnimation;
63import android.view.animation.Animation;
64import android.view.animation.LinearInterpolator;
Emeric Vigiereaf2c492012-09-19 14:38:20 -040065import android.widget.Button;
Emeric Vigierd5c17f52012-08-29 09:29:33 -040066import android.widget.EditText;
Emeric Vigiere89b48b2012-08-30 14:16:46 -040067import android.widget.ImageButton;
Adrien Béraudffd32412012-08-07 18:39:23 -040068import android.widget.TextView;
69
70import com.savoirfairelinux.sflphone.R;
Emeric Vigier6119d782012-09-21 18:04:14 -040071import com.savoirfairelinux.sflphone.service.ISipService;
Emeric Vigiereaf2c492012-09-19 14:38:20 -040072import com.savoirfairelinux.sflphone.service.SipService;
Alexandre Savard6b8d1df2012-10-23 16:44:43 -040073import com.savoirfairelinux.sflphone.utils.AccountList;
Alexandre Savard74c1cad2012-10-24 16:39:00 -040074import com.savoirfairelinux.sflphone.utils.CallList;
Adrien Béraudffd32412012-08-07 18:39:23 -040075
Alexandre Savard7a902bc2012-10-04 16:32:35 -040076import java.util.HashMap;
77
Emeric Vigier62ca14d2012-08-24 11:05:09 -040078public class SFLPhoneHome extends Activity implements ActionBar.TabListener, OnClickListener
Adrien Béraudffd32412012-08-07 18:39:23 -040079{
Emeric Vigierf24aca92012-11-06 19:11:24 -050080 SectionsPagerAdapter mSectionsPagerAdapter = null;
Alexandre Savard6c0584e2012-09-20 09:42:59 -040081 static final String TAG = "SFLPhoneHome";
Alexandre Savard6c0584e2012-09-20 09:42:59 -040082 private static final int REQUEST_CODE_PREFERENCES = 1;
83 ImageButton buttonCall, buttonHangup;
Emeric Vigier419ba7b2012-09-19 14:55:14 -040084 static Animation animation;
Emeric Vigierf24aca92012-11-06 19:11:24 -050085 private ContactListFragment mContactListFragment = null;
86 private CallElementList mCallElementList = null;
87 private HistorySectionFragment mHistorySectionFragment = null;
88 private ButtonSectionFragment mButtonSectionFragment = null;
Emeric Vigier6119d782012-09-21 18:04:14 -040089 private boolean mBound = false;
90 private ISipService service;
Emeric Vigiercc0785a2012-11-06 19:07:48 -050091 public AccountList mAccountList;
Alexandre Savard73bc56f2012-10-25 13:35:54 -040092 public CallList mCallList = new CallList(this);
Emeric Vigier1f1ced32012-11-02 16:56:32 -040093 private SFLphoneApplication mApplication;
Adrien Béraudffd32412012-08-07 18:39:23 -040094
Alexandre Savard48240e82012-10-26 10:36:03 -040095 private static final int ACTION_BAR_TAB_CONTACT = 0;
96 private static final int ACTION_BAR_TAB_CALL = 1;
97 private static final int ACTION_BAR_TAB_HISTORY = 2;
Emeric Vigierf24aca92012-11-06 19:11:24 -050098 private static final int ACTION_BAR_TAB_TEST = 3;
Alexandre Savard48240e82012-10-26 10:36:03 -040099
Alexandre Savard6c0584e2012-09-20 09:42:59 -0400100 /**
101 * The {@link ViewPager} that will host the section contents.
102 */
103 ViewPager mViewPager;
Emeric Vigierbbde1ae2012-09-21 18:13:56 -0400104
Alexandre Savard6c0584e2012-09-20 09:42:59 -0400105 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éraudffd32412012-08-07 18:39:23 -0400106
Alexandre Savard817dc502012-10-22 11:47:29 -0400107 // public SFLPhoneHome extends Activity implements ActionBar.TabListener, OnClickListener
108
Emeric Vigierf24aca92012-11-06 19:11:24 -0500109 /* called before activity is killed, e.g. rotation */
110 @Override
111 protected void onSaveInstanceState(Bundle bundle) {
112 super.onSaveInstanceState(bundle);
113 for (int i = 0; i < mSectionsPagerAdapter.getCount(); i++) {
114 try {
115 /* putFragment (Bundle bundle, String key, Fragment fragment) */
116 getFragmentManager().putFragment(bundle, mSectionsPagerAdapter.getClassName(i), mSectionsPagerAdapter.getFragment(i));
117 } catch (IllegalStateException e) {
118 Log.e(TAG, "IllegalStateException: fragment=" + mSectionsPagerAdapter.getFragment(i));
119 }
120 }
121 Log.w(TAG, "onSaveInstanceState()");
122 }
123
Alexandre Savard6c0584e2012-09-20 09:42:59 -0400124 @Override
125 public void onCreate(Bundle savedInstanceState)
126 {
127 super.onCreate(savedInstanceState);
Alexandre Savard817dc502012-10-22 11:47:29 -0400128
Emeric Vigier1f1ced32012-11-02 16:56:32 -0400129 mApplication = (SFLphoneApplication) getApplication();
130
Alexandre Savard817dc502012-10-22 11:47:29 -0400131 // Bind to LocalService
132 if (!mBound) {
133 Log.i(TAG, "onStart: Binding service...");
134 Intent intent = new Intent(this, SipService.class);
135 bindService(intent, mConnection, Context.BIND_AUTO_CREATE);
136 }
137
Alexandre Savard6c0584e2012-09-20 09:42:59 -0400138 setContentView(R.layout.activity_sflphone_home);
Adrien Béraudffd32412012-08-07 18:39:23 -0400139
Emeric Vigierf24aca92012-11-06 19:11:24 -0500140 if (mSectionsPagerAdapter == null) {
141 mSectionsPagerAdapter = new SectionsPagerAdapter(getFragmentManager());
142 }
143
144 /* getFragment(Bundle, String) */
145 if (savedInstanceState != null) {
146 Log.w(TAG, "Activity restarted, recreating PagerAdapter...");
147 /* getFragment (Bundle bundle, String key) */
148 mContactListFragment = (ContactListFragment) getFragmentManager().getFragment(
149 savedInstanceState, mSectionsPagerAdapter.getClassName(ACTION_BAR_TAB_CONTACT));
150 mCallElementList = (CallElementList) getFragmentManager().getFragment(
151 savedInstanceState, mSectionsPagerAdapter.getClassName(ACTION_BAR_TAB_CALL));
152 mHistorySectionFragment = (HistorySectionFragment) getFragmentManager().getFragment(
153 savedInstanceState, mSectionsPagerAdapter.getClassName(ACTION_BAR_TAB_HISTORY));
154 mButtonSectionFragment = (ButtonSectionFragment) getFragmentManager().getFragment(
155 savedInstanceState, mSectionsPagerAdapter.getClassName(ACTION_BAR_TAB_TEST));
156 }
157
158 if (mContactListFragment == null) {
159 mContactListFragment = new ContactListFragment();
160 Log.w(TAG, "Recreated mContactListFragment=" + mContactListFragment);
161 }
162 if (mCallElementList == null) {
163 mCallElementList = new CallElementList();
164 Log.w(TAG, "Recreated mCallElementList=" + mCallElementList);
165 }
166 if (mHistorySectionFragment == null) {
167 mHistorySectionFragment = new HistorySectionFragment();
168 Log.w(TAG, "Recreated mHistorySectionFragment=" + mHistorySectionFragment);
169 }
170 if (mButtonSectionFragment == null) {
171 mButtonSectionFragment = new ButtonSectionFragment();
172 Log.w(TAG, "Recreated mButtonSectionFragment=" + mButtonSectionFragment);
173 }
Adrien Béraudffd32412012-08-07 18:39:23 -0400174
Alexandre Savard6c0584e2012-09-20 09:42:59 -0400175 final ActionBar actionBar = getActionBar();
176 actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
177 // final ActionBar actionBar = getActionBar();
Adrien Béraudffd32412012-08-07 18:39:23 -0400178
Alexandre Savard6c0584e2012-09-20 09:42:59 -0400179 // Set up the ViewPager with the sections adapter.
180 mViewPager = (ViewPager) findViewById(R.id.pager);
181 mViewPager.setAdapter(mSectionsPagerAdapter);
Adrien Béraudffd32412012-08-07 18:39:23 -0400182
Alexandre Savard6c0584e2012-09-20 09:42:59 -0400183 // When swiping between different sections, select the corresponding tab.
184 // We can also use ActionBar.Tab#select() to do this if we have a reference to the
185 // Tab.
186 mViewPager.setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener()
187 {
188 @Override
189 public void onPageSelected(int position)
190 {
191 actionBar.setSelectedNavigationItem(position);
192 }
193 });
Emeric Vigier383b2a22012-08-27 14:20:05 -0400194
Alexandre Savard6c0584e2012-09-20 09:42:59 -0400195 // For each of the sections in the app, add a tab to the action bar.
196 for (int i = 0; i < mSectionsPagerAdapter.getCount(); i++) {
197 // Create a tab with text corresponding to the page title defined by the adapter.
198 // Also specify this Activity object, which implements the TabListener interface, as the
199 // listener for when this tab is selected.
200 Log.i(TAG, "adding tab: " + i);
201 actionBar.addTab(actionBar.newTab().setIcon(icon_res_id[i]).setText(mSectionsPagerAdapter.getPageTitle(i)).setTabListener(this));
202 }
Emeric Vigier383b2a22012-08-27 14:20:05 -0400203
Alexandre Savard48240e82012-10-26 10:36:03 -0400204 actionBar.setSelectedNavigationItem(ACTION_BAR_TAB_CALL);
Alexandre Savardcb66fa42012-10-26 10:06:16 -0400205
Alexandre Savard6c0584e2012-09-20 09:42:59 -0400206 buttonCall = (ImageButton) findViewById(R.id.buttonCall);
207 buttonHangup = (ImageButton) findViewById(R.id.buttonHangUp);
Alexandre Savarddef3bfa2012-09-12 16:20:06 -0400208
Emeric Vigier419ba7b2012-09-19 14:55:14 -0400209 // Change alpha from fully visible to invisible
210 animation = new AlphaAnimation(1, 0);
211 // duration - half a second
212 animation.setDuration(500);
213 // do not alter animation rate
214 animation.setInterpolator(new LinearInterpolator());
215 // Repeat animation infinitely
216 animation.setRepeatCount(Animation.INFINITE);
217 // Reverse
218 animation.setRepeatMode(Animation.REVERSE);
Alexandre Savard74c1cad2012-10-24 16:39:00 -0400219
220 LocalBroadcastManager.getInstance(this).registerReceiver(mCallList, new IntentFilter("new-call-created"));
221 LocalBroadcastManager.getInstance(this).registerReceiver(mCallList, new IntentFilter("call-state-changed"));
222 LocalBroadcastManager.getInstance(this).registerReceiver(mCallList, new IntentFilter("incoming-call"));
Alexandre Savarda949eec2012-10-25 17:30:49 -0400223
Emeric Vigiercc0785a2012-11-06 19:07:48 -0500224 mAccountList = mApplication.getAccountList();
225
Alexandre Savardbb6f1872012-11-01 17:25:55 -0400226 LocalBroadcastManager.getInstance(this).registerReceiver(mAccountList, new IntentFilter("accounts-changed"));
227 LocalBroadcastManager.getInstance(this).registerReceiver(mAccountList, new IntentFilter("account-state-changed"));
228
Alexandre Savarda949eec2012-10-25 17:30:49 -0400229 SipCall.setSFLPhoneHomeContext(this);
Alexandre Savard6c0584e2012-09-20 09:42:59 -0400230 }
Alexandre Savard1bcaf532012-09-05 16:23:02 -0400231
Emeric Vigier12d61d82012-09-19 15:08:18 -0400232 @Override
233 protected void onStart() {
234 Log.i(TAG, "onStart");
235 super.onStart();
236 }
Alexandre Savard1bcaf532012-09-05 16:23:02 -0400237
Emeric Vigier6119d782012-09-21 18:04:14 -0400238 /* user gets back to the activity, e.g. through task manager */
Emeric Vigier12d61d82012-09-19 15:08:18 -0400239 @Override
240 protected void onRestart() {
241 super.onRestart();
242 }
243
Emeric Vigier6119d782012-09-21 18:04:14 -0400244 /* activity gets back to the foreground and user input */
Emeric Vigier12d61d82012-09-19 15:08:18 -0400245 @Override
246 protected void onResume() {
247 Log.i(TAG, "onResume");
248 super.onResume();
Emeric Vigier12d61d82012-09-19 15:08:18 -0400249 }
250
Emeric Vigier6119d782012-09-21 18:04:14 -0400251 /* activity no more in foreground */
Emeric Vigier12d61d82012-09-19 15:08:18 -0400252 @Override
253 protected void onPause() {
Emeric Vigier12d61d82012-09-19 15:08:18 -0400254 super.onPause();
255 }
256
Emeric Vigier6119d782012-09-21 18:04:14 -0400257 /* activity is no longer visible */
Emeric Vigier12d61d82012-09-19 15:08:18 -0400258 @Override
259 protected void onStop() {
260 super.onStop();
Alexandre Savard817dc502012-10-22 11:47:29 -0400261 }
262
263 /* activity finishes itself or is being killed by the system */
264 @Override
265 protected void onDestroy() {
Emeric Vigier6119d782012-09-21 18:04:14 -0400266 /* stop the service, if no other bound user, no need to check if it is running */
267 if (mBound) {
Emeric Vigierfae29bd2012-09-24 11:32:03 -0400268 Log.i(TAG, "onStop: Unbinding service...");
Emeric Vigier6119d782012-09-21 18:04:14 -0400269 unbindService(mConnection);
270 mBound = false;
271 }
Emeric Vigiercc0785a2012-11-06 19:07:48 -0500272// Log.i(TAG, "onDestroy: stopping SipService...");
273// stopService(new Intent(this, SipService.class));
274// mApplication.setServiceRunning(false);
Emeric Vigiere2729af2012-11-05 11:24:02 -0500275
276 /* unregister broadcast receiver */
277 LocalBroadcastManager.getInstance(this).unregisterReceiver(mCallList);
278 LocalBroadcastManager.getInstance(this).unregisterReceiver(mAccountList);
279
Emeric Vigier12d61d82012-09-19 15:08:18 -0400280 super.onDestroy();
281 }
Emeric Vigiercc0785a2012-11-06 19:07:48 -0500282
Emeric Vigier6119d782012-09-21 18:04:14 -0400283 /** Defines callbacks for service binding, passed to bindService() */
284 private ServiceConnection mConnection = new ServiceConnection() {
285
286 @Override
287 public void onServiceConnected(ComponentName className,
288 IBinder binder) {
289 service = ISipService.Stub.asInterface(binder);
Emeric Vigier1f1ced32012-11-02 16:56:32 -0400290 mApplication.setSipService(service);
Emeric Vigier6119d782012-09-21 18:04:14 -0400291 mBound = true;
Alexandre Savardbb6f1872012-11-01 17:25:55 -0400292 mAccountList.setSipService(service);
Emeric Vigiercc0785a2012-11-06 19:07:48 -0500293 Log.d(TAG, "Service connected service=" + service);
Emeric Vigier6119d782012-09-21 18:04:14 -0400294 }
295
296 @Override
297 public void onServiceDisconnected(ComponentName arg0) {
Emeric Vigier1f1ced32012-11-02 16:56:32 -0400298 mApplication.setSipService(null);
Emeric Vigier6119d782012-09-21 18:04:14 -0400299 mBound = false;
Emeric Vigiercc0785a2012-11-06 19:07:48 -0500300 Log.w(TAG, "Service disconnected service=" + service);
Emeric Vigier6119d782012-09-21 18:04:14 -0400301 }
302 };
303
Emeric Vigiercc0785a2012-11-06 19:07:48 -0500304// private void startSipService() {
305// Thread thread = new Thread("StartSFLphoneService") {
306// public void run() {
307// Intent sipServiceIntent = new Intent(SFLPhoneHome.this, SipService.class);
308// //sipServiceIntent.putExtra(ServiceConstants.EXTRA_OUTGOING_ACTIVITY, new ComponentName(SFLPhoneHome.this, SFLPhoneHome.class));
309// startService(sipServiceIntent);
310// mApplication.setServiceRunning(true);
311// };
312// };
313// try {
314// thread.start();
315// } catch (IllegalThreadStateException e) {
316// AlertDialog.Builder builder = new AlertDialog.Builder(this);
317// builder.setMessage("Cannot start SFLPhone SipService!");
318// AlertDialog alert = builder.create();
319// alert.show();
320// finish();
321// }
322// }
Emeric Vigier12d61d82012-09-19 15:08:18 -0400323
324 @Override
325 public boolean onOptionsItemSelected(MenuItem item) {
326 Log.i("SFLphone", "onOptionsItemSelected " + item.getItemId());
Emeric Vigierbbde1ae2012-09-21 18:13:56 -0400327 if(item.getItemId() != 0) {
328 // When the button is clicked, launch an activity through this intent
329 Intent launchPreferencesIntent = new Intent().setClass(this, SFLPhonePreferenceActivity.class);
Alexandre Savard1bcaf532012-09-05 16:23:02 -0400330
Emeric Vigierbbde1ae2012-09-21 18:13:56 -0400331 // Make it a subactivity so we know when it returns
332 startActivityForResult(launchPreferencesIntent, REQUEST_CODE_PREFERENCES);
Alexandre Savard1bcaf532012-09-05 16:23:02 -0400333 }
Adrien Béraudffd32412012-08-07 18:39:23 -0400334
Emeric Vigierbbde1ae2012-09-21 18:13:56 -0400335 return super.onOptionsItemSelected(item);
336 }
Adrien Béraudffd32412012-08-07 18:39:23 -0400337
Emeric Vigierbbde1ae2012-09-21 18:13:56 -0400338 @Override
339 public boolean onCreateOptionsMenu(Menu menu)
340 {
341 getMenuInflater().inflate(R.menu.activity_sflphone_home, menu);
342 return true;
343 }
Adrien Béraudffd32412012-08-07 18:39:23 -0400344
Emeric Vigierbbde1ae2012-09-21 18:13:56 -0400345 @Override
346 public void onTabUnselected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction)
347 {
348 }
Adrien Béraudffd32412012-08-07 18:39:23 -0400349
Emeric Vigierbbde1ae2012-09-21 18:13:56 -0400350 @Override
351 public void onTabSelected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction)
352 {
353 // When the given tab is selected, switch to the corresponding page in the ViewPager.
354 mViewPager.setCurrentItem(tab.getPosition());
355 }
356
357 @Override
358 public void onTabReselected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction)
359 {
360 // Log.d(TAG, "onTabReselected");
361 }
362
Alexandre Savardf17e3172012-10-25 16:09:09 -0400363 public void onSelectedCallAction(SipCall call) {
Alexandre Savard27a51132012-10-25 18:35:14 -0400364 int callState = call.getCallStateInt();
365
366 if((callState == SipCall.CALL_STATE_NONE) ||
367 (callState == SipCall.CALL_STATE_CURRENT)) {
Alexandre Savardf17e3172012-10-25 16:09:09 -0400368 buttonCall.setEnabled(false);
369 buttonHangup.setEnabled(true);
370 }
Alexandre Savard27a51132012-10-25 18:35:14 -0400371 else {
372 buttonCall.setEnabled(true);
373 buttonHangup.setEnabled(false);
374 }
Alexandre Savardf17e3172012-10-25 16:09:09 -0400375
376 buttonCall.setTag(call);
377 buttonHangup.setTag(call);
378 }
379
380 public void onUnselectedCallAction() {
381 buttonCall.setTag(null);
382 buttonCall.setTag(null);
Alexandre Savarda949eec2012-10-25 17:30:49 -0400383
384 buttonCall.setEnabled(true);
385 buttonHangup.setEnabled(false);
Alexandre Savardf17e3172012-10-25 16:09:09 -0400386 }
387
Emeric Vigierbbde1ae2012-09-21 18:13:56 -0400388 public void setIncomingCallID(String accountID, String callID, String from) {
Emeric Vigier419ba7b2012-09-19 14:55:14 -0400389 Log.i(TAG, "incomingCall(" + accountID + ", " + callID + ", " + from + ")");
Emeric Vigier419ba7b2012-09-19 14:55:14 -0400390 buttonCall.startAnimation(animation);
391 buttonCall.setImageResource(R.drawable.ic_incomingcall);
Emeric Vigierbbde1ae2012-09-21 18:13:56 -0400392 }
Adrien Béraudffd32412012-08-07 18:39:23 -0400393
Emeric Vigierbbde1ae2012-09-21 18:13:56 -0400394 /**
395 * A {@link FragmentStatePagerAdapter} that returns a fragment corresponding to
396 * one of the primary sections of the app.
397 */
398 public class SectionsPagerAdapter extends FragmentStatePagerAdapter
399 {
Adrien Béraudffd32412012-08-07 18:39:23 -0400400
Emeric Vigierbbde1ae2012-09-21 18:13:56 -0400401 public SectionsPagerAdapter(FragmentManager fm)
402 {
403 super(fm);
404 }
Emeric Vigier05e894e2012-08-20 13:53:02 -0400405
Emeric Vigierbbde1ae2012-09-21 18:13:56 -0400406 @Override
407 public Fragment getItem(int i)
408 {
409 Fragment fragment;
Adrien Béraudffd32412012-08-07 18:39:23 -0400410
Emeric Vigierbbde1ae2012-09-21 18:13:56 -0400411 switch (i) {
412 case 0:
Emeric Vigier1f1ced32012-11-02 16:56:32 -0400413 mContactListFragment = new ContactListFragment();
Emeric Vigierbbde1ae2012-09-21 18:13:56 -0400414 fragment = mContactListFragment;
415 break;
416 case 1:
Emeric Vigier1f1ced32012-11-02 16:56:32 -0400417 mCallElementList = new CallElementList();
Emeric Vigierbbde1ae2012-09-21 18:13:56 -0400418 SipCall.setCallElementList(mCallElementList);
Alexandre Savardbb6f1872012-11-01 17:25:55 -0400419 mCallElementList.setAccountList(mAccountList);
Emeric Vigierbbde1ae2012-09-21 18:13:56 -0400420 fragment = mCallElementList;
421 break;
422 case 2:
Emeric Vigierb16eed32012-11-06 19:12:54 -0500423 fragment = new HistorySectionFragment();
Emeric Vigierbbde1ae2012-09-21 18:13:56 -0400424 break;
425 case 3:
426 fragment = new ButtonSectionFragment();
427 Log.i(TAG, "getItem: fragment is " + fragment);
428 break;
429 default:
430 Log.e(TAG, "getItem: unknown tab position " + i);
431 return null;
432 }
Adrien Béraudffd32412012-08-07 18:39:23 -0400433
Emeric Vigierbbde1ae2012-09-21 18:13:56 -0400434 Bundle args = new Bundle();
Emeric Vigierb16eed32012-11-06 19:12:54 -0500435 args.putInt(HistorySectionFragment.ARG_SECTION_NUMBER, i + 1);
Emeric Vigierbbde1ae2012-09-21 18:13:56 -0400436 fragment.setArguments(args);
437 return fragment;
438 }
Adrien Béraudffd32412012-08-07 18:39:23 -0400439
Emeric Vigierf24aca92012-11-06 19:11:24 -0500440 public Fragment getFragment(int i)
441 {
442 Fragment fragment;
443
444 switch (i) {
445 case 0:
446 fragment = mContactListFragment;
447 break;
448 case 1:
449 fragment = mCallElementList;
450 break;
451 case 2:
452 fragment = mHistorySectionFragment;
453 break;
454 case 3:
455 fragment = mButtonSectionFragment;
456 break;
457 default:
458 Log.e(TAG, "getClassName: unknown fragment position " + i);
459 fragment = null;
460 }
461
462// Log.w(TAG, "getFragment: fragment=" + fragment);
463 return fragment;
464 }
465
466 public String getClassName(int i)
467 {
468 String name;
469
470 switch (i) {
471 case 0:
472 name = ContactListFragment.class.getName();
473 break;
474 case 1:
475 name = CallElementList.class.getName();
476 break;
477 case 2:
478 name = HistorySectionFragment.class.getName();
479 break;
480 case 3:
481 name = ButtonSectionFragment.class.getName();
482 break;
483 default:
484 Log.e(TAG, "getClassName: unknown fragment position " + i);
485 return null;
486 }
487
488// Log.w(TAG, "getClassName: name=" + name);
489 return name;
490 }
491
Emeric Vigierbbde1ae2012-09-21 18:13:56 -0400492 @Override
493 public int getCount()
494 {
495 return 4;
496 }
Adrien Béraudffd32412012-08-07 18:39:23 -0400497
Emeric Vigierbbde1ae2012-09-21 18:13:56 -0400498 @Override
499 public CharSequence getPageTitle(int position)
500 {
501 switch (position) {
502 case 0:
503 return getString(R.string.title_section0).toUpperCase();
504 case 1:
505 return getString(R.string.title_section1).toUpperCase();
506 case 2:
507 return getString(R.string.title_section2).toUpperCase();
508 case 3:
509 return getString(R.string.title_section3).toUpperCase();
510 default:
511 Log.e(TAG, "getPageTitle: unknown tab position " + position);
512 break;
513 }
514 return null;
515 }
516 }
Adrien Béraudffd32412012-08-07 18:39:23 -0400517
Emeric Vigierbbde1ae2012-09-21 18:13:56 -0400518 /**
519 * A dummy fragment representing a section of the app, but that simply
520 * displays dummy text.
521 */
Emeric Vigierb16eed32012-11-06 19:12:54 -0500522 public static class HistorySectionFragment extends Fragment
Emeric Vigierbbde1ae2012-09-21 18:13:56 -0400523 {
Emeric Vigierb16eed32012-11-06 19:12:54 -0500524 public HistorySectionFragment()
Emeric Vigierbbde1ae2012-09-21 18:13:56 -0400525 {
526 setRetainInstance(true);
527 }
Emeric Vigier05e894e2012-08-20 13:53:02 -0400528
Emeric Vigierbbde1ae2012-09-21 18:13:56 -0400529 public static final String ARG_SECTION_NUMBER = "section_number";
530
531 @Override
532 public View onCreateView(LayoutInflater inflater, ViewGroup parent, Bundle savedInstanceState)
533 {
534 TextView textView = new TextView(getActivity());
535 textView.setGravity(Gravity.CENTER);
536 Bundle args = getArguments();
537 textView.setText(Integer.toString(args.getInt(ARG_SECTION_NUMBER)));
538 textView.setText("java sucks");
539 return textView;
540 }
541 }
542
543 @Override
Emeric Vigier62ca14d2012-08-24 11:05:09 -0400544 public void onClick(View view)
Emeric Vigiere89b48b2012-08-30 14:16:46 -0400545 {
Alexandre Savard6b8d1df2012-10-23 16:44:43 -0400546 Log.i(TAG, "onClic from SFLPhoneHome");
Alexandre Savard91052442012-10-25 16:14:26 -0400547 switch (view.getId()) {
Emeric Vigierbbde1ae2012-09-21 18:13:56 -0400548 case R.id.buttonCall:
Alexandre Savard6b8d1df2012-10-23 16:44:43 -0400549 processingNewCallAction();
Emeric Vigierbbde1ae2012-09-21 18:13:56 -0400550 break;
551 case R.id.buttonHangUp:
Alexandre Savard6b8d1df2012-10-23 16:44:43 -0400552 processingHangUpAction();
Emeric Vigierbbde1ae2012-09-21 18:13:56 -0400553 break;
Emeric Vigierbbde1ae2012-09-21 18:13:56 -0400554 default:
555 Log.w(TAG, "unknown button " + view.getId());
556 break;
Emeric Vigier6119d782012-09-21 18:04:14 -0400557 }
Emeric Vigierbbde1ae2012-09-21 18:13:56 -0400558 }
Alexandre Savard6b8d1df2012-10-23 16:44:43 -0400559
560 public void processingNewCallAction() {
Alexandre Savard60f0f1c2012-11-01 09:40:34 -0400561 // String accountID = mAccountList.currentAccountID;
562 String accountID = mCallElementList.getSelectedAccount();
Alexandre Savardcb66fa42012-10-26 10:06:16 -0400563 EditText editText = (EditText) findViewById(R.id.phoneNumberTextEntry);
564 String to = editText.getText().toString();
Alexandre Savard27a51132012-10-25 18:35:14 -0400565
Alexandre Savard6b8d1df2012-10-23 16:44:43 -0400566 Random random = new Random();
Alexandre Savard27a51132012-10-25 18:35:14 -0400567 String callID = Integer.toString(random.nextInt());
568 SipCall.CallInfo info = new SipCall.CallInfo();
Alexandre Savard6b8d1df2012-10-23 16:44:43 -0400569
Alexandre Savard27a51132012-10-25 18:35:14 -0400570 info.mCallID = callID;
571 info.mAccountID = accountID;
572 info.mDisplayName = "Cool Guy!";
573 info.mPhone = to;
574 info.mEmail = "coolGuy@coolGuy.com";
Alexandre Savard2f1ae542012-10-26 17:05:00 -0400575 info.mCallType = SipCall.CALL_TYPE_OUTGOING;
Alexandre Savard6b8d1df2012-10-23 16:44:43 -0400576
Alexandre Savard27a51132012-10-25 18:35:14 -0400577 SipCall call = CallList.getCallInstance(info);
578 call.launchCallActivity(this);
579 call.placeCallUpdateUi();
580 call.notifyServicePlaceCall(service);
581
582 onSelectedCallAction(call);
Alexandre Savard6b8d1df2012-10-23 16:44:43 -0400583 }
584
585 public void processingHangUpAction() {
Alexandre Savardf17e3172012-10-25 16:09:09 -0400586 SipCall call = (SipCall)buttonHangup.getTag();
587 if(call != null)
588 call.notifyServiceHangup(service);
Alexandre Savard6b8d1df2012-10-23 16:44:43 -0400589 }
Adrien Béraudffd32412012-08-07 18:39:23 -0400590}