blob: 7aa8d980e76e5d559aa6520ae81552a517a675fc [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;
37import android.app.Fragment;
38import android.app.FragmentManager;
39import android.app.FragmentTransaction;
Alexandre Savard1bcaf532012-09-05 16:23:02 -040040import android.content.Intent;
Emeric Vigier7a255aa2012-08-29 09:49:18 -040041import android.content.pm.PackageInfo;
42import android.content.pm.PackageManager;
43import android.content.pm.PackageManager.NameNotFoundException;
Adrien Béraudffd32412012-08-07 18:39:23 -040044import android.os.Bundle;
Emeric Vigier383b2a22012-08-27 14:20:05 -040045import android.os.Handler;
46import android.os.Message;
Adrien Béraudffd32412012-08-07 18:39:23 -040047import android.support.v13.app.FragmentStatePagerAdapter;
48import android.support.v4.view.ViewPager;
Emeric Vigier05e894e2012-08-20 13:53:02 -040049import android.util.Log;
Adrien Béraudffd32412012-08-07 18:39:23 -040050import android.view.Gravity;
51import android.view.LayoutInflater;
52import android.view.Menu;
Alexandre Savard1bcaf532012-09-05 16:23:02 -040053import android.view.MenuItem;
Adrien Béraudffd32412012-08-07 18:39:23 -040054import android.view.View;
Emeric Vigier05e894e2012-08-20 13:53:02 -040055import android.view.View.OnClickListener;
Adrien Béraudffd32412012-08-07 18:39:23 -040056import android.view.ViewGroup;
Emeric Vigiera6bdb702012-08-31 15:34:53 -040057import android.view.animation.AlphaAnimation;
58import android.view.animation.Animation;
59import android.view.animation.LinearInterpolator;
Emeric Vigierd5c17f52012-08-29 09:29:33 -040060import android.widget.EditText;
Emeric Vigiere89b48b2012-08-30 14:16:46 -040061import android.widget.ImageButton;
Adrien Béraudffd32412012-08-07 18:39:23 -040062import android.widget.TextView;
63
64import com.savoirfairelinux.sflphone.R;
65
Emeric Vigier62ca14d2012-08-24 11:05:09 -040066public class SFLPhoneHome extends Activity implements ActionBar.TabListener, OnClickListener
Adrien Béraudffd32412012-08-07 18:39:23 -040067{
68 SectionsPagerAdapter mSectionsPagerAdapter;
Emeric Vigier62ca14d2012-08-24 11:05:09 -040069 static final String TAG = "SFLPhoneHome";
Emeric Vigiera6bdb702012-08-31 15:34:53 -040070 private ButtonSectionFragment buttonFragment;
Emeric Vigiere89b48b2012-08-30 14:16:46 -040071 ImageButton buttonCall, buttonHangup;
Emeric Vigier383b2a22012-08-27 14:20:05 -040072 Handler callbackHandler;
Emeric Vigier9701e032012-09-12 12:38:01 -040073 private Manager manager;
Emeric Vigier78a53e92012-08-29 18:15:34 -040074 /* default callID */
75 static String callID = "007";
Emeric Vigiere89b48b2012-08-30 14:16:46 -040076 static boolean callOnGoing = false;
Emeric Vigiera6bdb702012-08-31 15:34:53 -040077 private String incomingCallID = "";
Alexandre Savard1bcaf532012-09-05 16:23:02 -040078 private static final int REQUEST_CODE_PREFERENCES = 1;
Adrien Béraudffd32412012-08-07 18:39:23 -040079
80 /**
81 * The {@link ViewPager} that will host the section contents.
82 */
83 ViewPager mViewPager;
84
Emeric Vigier05e894e2012-08-20 13:53:02 -040085 final private int[] icon_res_id = {R.drawable.ic_tab_call, R.drawable.ic_tab_history, R.drawable.ic_tab_play_selected};
Adrien Béraudffd32412012-08-07 18:39:23 -040086
87 @Override
88 public void onCreate(Bundle savedInstanceState)
89 {
90 super.onCreate(savedInstanceState);
91 setContentView(R.layout.activity_sflphone_home);
92
93 mSectionsPagerAdapter = new SectionsPagerAdapter(getFragmentManager());
94
95 final ActionBar actionBar = getActionBar();
96 actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
97
98 // Set up the ViewPager with the sections adapter.
99 mViewPager = (ViewPager) findViewById(R.id.pager);
100 mViewPager.setAdapter(mSectionsPagerAdapter);
101
102 // When swiping between different sections, select the corresponding tab.
103 // We can also use ActionBar.Tab#select() to do this if we have a reference to the
104 // Tab.
105 mViewPager.setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener()
106 {
107 @Override
108 public void onPageSelected(int position)
109 {
110 actionBar.setSelectedNavigationItem(position);
111 }
112 });
113
114 // For each of the sections in the app, add a tab to the action bar.
115 for (int i = 0; i < mSectionsPagerAdapter.getCount(); i++) {
116 // Create a tab with text corresponding to the page title defined by the adapter.
117 // Also specify this Activity object, which implements the TabListener interface, as the
118 // listener for when this tab is selected.
119 actionBar.addTab(actionBar.newTab().setIcon(icon_res_id[i]).setText(mSectionsPagerAdapter.getPageTitle(i)).setTabListener(this));
120 }
Emeric Vigier383b2a22012-08-27 14:20:05 -0400121
122 // FIXME
123 callbackHandler = new Handler() {
124 public void handleMessage(Message msg) {
125 Bundle b = msg.getData();
126 TextView callVoidText;
127
Emeric Vigierc411ce32012-08-29 09:28:28 -0400128 Log.i(TAG, "handleMessage");
Emeric Vigier383b2a22012-08-27 14:20:05 -0400129
130 callVoidText = buttonFragment.getcallVoidText();
131 if (callVoidText == null)
132 Log.e(TAG, "SFLPhoneHome: callVoidText is " + callVoidText);
133 callVoidText.setText(b.getString("callback_string"));
134
Emeric Vigierc411ce32012-08-29 09:28:28 -0400135 Log.i(TAG, "handleMessage: " + b.getString("callback_string"));
Emeric Vigier383b2a22012-08-27 14:20:05 -0400136 }
137 };
Emeric Vigier9701e032012-09-12 12:38:01 -0400138 manager = new Manager(callbackHandler);
139 Log.i(TAG, "ManagerImpl::instance() = " + manager.managerImpl);
140 manager.setActivity(this);
141 /* set static AppPath before calling manager.init */
142 manager.managerImpl.setPath(getAppPath());
143 Log.i(TAG, "manager created with callbackHandler " + callbackHandler);
Emeric Vigiera6bdb702012-08-31 15:34:53 -0400144
145 buttonCall = (ImageButton) findViewById(R.id.buttonCall);
146 buttonHangup = (ImageButton) findViewById(R.id.buttonHangUp);
147// buttonIncomingCall = (ImageButton) findViewById(R.id.buttonIncomingCall);
Adrien Béraudffd32412012-08-07 18:39:23 -0400148 }
149
Emeric Vigier62ca14d2012-08-24 11:05:09 -0400150 // FIXME
151 static {
152 System.loadLibrary("gnustl_shared");
153 System.loadLibrary("expat");
154 System.loadLibrary("yaml");
155 System.loadLibrary("ccgnu2");
156 System.loadLibrary("crypto");
157 System.loadLibrary("ssl");
158 System.loadLibrary("ccrtp1");
159 System.loadLibrary("dbus");
160 System.loadLibrary("dbus-c++-1");
161 System.loadLibrary("samplerate");
162 System.loadLibrary("codec_ulaw");
163 System.loadLibrary("codec_alaw");
164 System.loadLibrary("speexresampler");
165 System.loadLibrary("sflphone");
166 }
Alexandre Savard1bcaf532012-09-05 16:23:02 -0400167
168 @Override
169 public boolean onOptionsItemSelected(MenuItem item) {
170 Log.i("SFLphone", "onOptionsItemSelected");
171
172 // When the button is clicked, launch an activity through this intent
173 Intent launchPreferencesIntent = new Intent().setClass(this, SFLPhonePreferenceActivity.class);
174
175 // Make it a subactivity so we know when it returns
176 startActivityForResult(launchPreferencesIntent, REQUEST_CODE_PREFERENCES);
177
178 return super.onOptionsItemSelected(item);
179 }
Emeric Vigier62ca14d2012-08-24 11:05:09 -0400180
Adrien Béraudffd32412012-08-07 18:39:23 -0400181 @Override
182 public boolean onCreateOptionsMenu(Menu menu)
183 {
184 getMenuInflater().inflate(R.menu.activity_sflphone_home, menu);
185 return true;
186 }
187
188 @Override
189 public void onTabUnselected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction)
190 {
191 }
192
193 @Override
194 public void onTabSelected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction)
195 {
196 // When the given tab is selected, switch to the corresponding page in the ViewPager.
197 mViewPager.setCurrentItem(tab.getPosition());
198 }
199
200 @Override
201 public void onTabReselected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction)
202 {
Emeric Vigier05e894e2012-08-20 13:53:02 -0400203// Log.d(TAG, "onTabReselected");
Adrien Béraudffd32412012-08-07 18:39:23 -0400204 }
205
Emeric Vigiera6bdb702012-08-31 15:34:53 -0400206 public void setIncomingCallID(String id) {
207 incomingCallID = id;
208 }
209
Adrien Béraudffd32412012-08-07 18:39:23 -0400210 /**
211 * A {@link FragmentStatePagerAdapter} that returns a fragment corresponding to
212 * one of the primary sections of the app.
213 */
214 public class SectionsPagerAdapter extends FragmentStatePagerAdapter
215 {
216
217 public SectionsPagerAdapter(FragmentManager fm)
218 {
219 super(fm);
220 }
221
222 @Override
223 public Fragment getItem(int i)
224 {
225 Fragment fragment;
Emeric Vigier62ca14d2012-08-24 11:05:09 -0400226
Emeric Vigier05e894e2012-08-20 13:53:02 -0400227 switch (i) {
228 case 0:
Adrien Béraudffd32412012-08-07 18:39:23 -0400229 fragment = new CallElementList();
Emeric Vigier05e894e2012-08-20 13:53:02 -0400230 break;
231 case 1:
Adrien Béraudffd32412012-08-07 18:39:23 -0400232 fragment = new DummySectionFragment();
Emeric Vigier05e894e2012-08-20 13:53:02 -0400233 break;
234 case 2:
Emeric Vigier62ca14d2012-08-24 11:05:09 -0400235 buttonFragment = new ButtonSectionFragment();
236 Log.i(TAG, "getItem: fragment is " + buttonFragment);
237 fragment = buttonFragment;
Emeric Vigier9701e032012-09-12 12:38:01 -0400238 manager.setButtonFragment(buttonFragment);
Emeric Vigier05e894e2012-08-20 13:53:02 -0400239 break;
240 default:
241 Log.e(TAG, "getItem: unknown tab position " + i);
242 return null;
243 }
244
Adrien Béraudffd32412012-08-07 18:39:23 -0400245 Bundle args = new Bundle();
246 args.putInt(DummySectionFragment.ARG_SECTION_NUMBER, i + 1);
247 fragment.setArguments(args);
248 return fragment;
249 }
250
251 @Override
252 public int getCount()
253 {
Emeric Vigier05e894e2012-08-20 13:53:02 -0400254 return 3;
Adrien Béraudffd32412012-08-07 18:39:23 -0400255 }
256
257 @Override
258 public CharSequence getPageTitle(int position)
259 {
260 switch (position) {
261 case 0:
262 return getString(R.string.title_section1).toUpperCase();
263 case 1:
264 return getString(R.string.title_section2).toUpperCase();
Emeric Vigier05e894e2012-08-20 13:53:02 -0400265 case 2:
266 return getString(R.string.title_section3).toUpperCase();
267 default:
268 Log.e(TAG, "getPageTitle: unknown tab position " + position);
269 break;
Adrien Béraudffd32412012-08-07 18:39:23 -0400270 }
271 return null;
272 }
273 }
274
275 /**
276 * A dummy fragment representing a section of the app, but that simply
277 * displays dummy text.
278 */
279 public static class DummySectionFragment extends Fragment
280 {
281 public DummySectionFragment()
282 {
283 setRetainInstance(true);
284 }
285
286 public static final String ARG_SECTION_NUMBER = "section_number";
287
288 @Override
Emeric Vigier05e894e2012-08-20 13:53:02 -0400289 public View onCreateView(LayoutInflater inflater, ViewGroup parent, Bundle savedInstanceState)
Adrien Béraudffd32412012-08-07 18:39:23 -0400290 {
291 TextView textView = new TextView(getActivity());
292 textView.setGravity(Gravity.CENTER);
293 Bundle args = getArguments();
294 textView.setText(Integer.toString(args.getInt(ARG_SECTION_NUMBER)));
Emeric Vigier05e894e2012-08-20 13:53:02 -0400295 textView.setText("java sucks");
Adrien Béraudffd32412012-08-07 18:39:23 -0400296 return textView;
297 }
Adrien Béraudffd32412012-08-07 18:39:23 -0400298 }
Emeric Vigier05e894e2012-08-20 13:53:02 -0400299
Emeric Vigier7a255aa2012-08-29 09:49:18 -0400300 public String getAppPath() {
301 PackageManager pkgMng = getPackageManager();
302 String pkgName = getPackageName();
303
304 try {
305 PackageInfo pkgInfo = pkgMng.getPackageInfo(pkgName, 0);
306 pkgName = pkgInfo.applicationInfo.dataDir;
307 } catch (NameNotFoundException e) {
308 Log.w(TAG, "Error Package name not found ", e);
309 }
310
311 Log.d(TAG, "Application path: " + pkgName);
312 return pkgName;
Emeric Vigier62ca14d2012-08-24 11:05:09 -0400313 }
Emeric Vigier05e894e2012-08-20 13:53:02 -0400314
Emeric Vigier62ca14d2012-08-24 11:05:09 -0400315 @Override
316 public void onClick(View view)
Emeric Vigiere89b48b2012-08-30 14:16:46 -0400317 {
Emeric Vigiera6bdb702012-08-31 15:34:53 -0400318 buttonCall = buttonFragment.getCallButton();
319 buttonHangup = buttonFragment.getHangUpButton();
320
Emeric Vigier62ca14d2012-08-24 11:05:09 -0400321 switch (view.getId()) {
Emeric Vigierd5c17f52012-08-29 09:29:33 -0400322 case R.id.buttonCall:
Emeric Vigier39a11fa2012-08-29 15:25:09 -0400323 TextView textView = (TextView) findViewById(R.id.editAccountID);
324 String accountID = textView.getText().toString();
325 EditText editText;
Emeric Vigier78a53e92012-08-29 18:15:34 -0400326 Random random = new Random();
Emeric Vigiere89b48b2012-08-30 14:16:46 -0400327
Emeric Vigiera6bdb702012-08-31 15:34:53 -0400328 if (incomingCallID != "") {
329 buttonCall.clearAnimation();
Emeric Vigier9701e032012-09-12 12:38:01 -0400330 manager.managerImpl.answerCall(incomingCallID);
Emeric Vigiera6bdb702012-08-31 15:34:53 -0400331 callID = incomingCallID;
332 incomingCallID="";
Emeric Vigiere89b48b2012-08-30 14:16:46 -0400333 callOnGoing = true;
Emeric Vigiera6bdb702012-08-31 15:34:53 -0400334 buttonCall.setEnabled(false);
335 buttonHangup.setEnabled(true);
336 } else {
337 if (callOnGoing == false) {
338 editText = (EditText) findViewById(R.id.editTo);
339 String to = editText.getText().toString();
340 if (to == null) {
341 Log.e(TAG, "to string is " + to);
342 break;
343 }
344
345 /* new random callID */
346 callID = Integer.toString(random.nextInt());
347
Emeric Vigier9701e032012-09-12 12:38:01 -0400348 Log.d(TAG, "manager.managerImpl.placeCall(" + accountID + ", " + callID + ", " + to + ");");
349 manager.managerImpl.outgoingCall(accountID, callID, to);
Emeric Vigiera6bdb702012-08-31 15:34:53 -0400350 callOnGoing = true;
351 buttonCall.setEnabled(false);
352 buttonHangup.setEnabled(true);
353 }
Emeric Vigierd5c17f52012-08-29 09:29:33 -0400354 }
Emeric Vigierd5c17f52012-08-29 09:29:33 -0400355 break;
Emeric Vigier78a53e92012-08-29 18:15:34 -0400356 case R.id.buttonHangUp:
Emeric Vigiera6bdb702012-08-31 15:34:53 -0400357 if (incomingCallID != "") {
358 buttonCall.clearAnimation();
Emeric Vigier9701e032012-09-12 12:38:01 -0400359 manager.managerImpl.refuseCall(incomingCallID);
Emeric Vigiera6bdb702012-08-31 15:34:53 -0400360 incomingCallID="";
361 buttonCall.setEnabled(true);
362 buttonHangup.setEnabled(true);
363 } else {
364 if (callOnGoing == true) {
Emeric Vigier9701e032012-09-12 12:38:01 -0400365 Log.d(TAG, "manager.managerImpl.hangUp(" + callID + ");");
366 manager.managerImpl.hangupCall(callID);
Emeric Vigiera6bdb702012-08-31 15:34:53 -0400367 callOnGoing = false;
368 buttonCall.setEnabled(true);
369 buttonHangup.setEnabled(false);
370 }
Emeric Vigiere89b48b2012-08-30 14:16:46 -0400371 }
Emeric Vigiera6bdb702012-08-31 15:34:53 -0400372
373 buttonCall.setImageResource(R.drawable.ic_call);
Emeric Vigier78a53e92012-08-29 18:15:34 -0400374 break;
Emeric Vigier62ca14d2012-08-24 11:05:09 -0400375 case R.id.buttonInit:
Emeric Vigier9701e032012-09-12 12:38:01 -0400376 Manager.managerImpl.setPath("");
377 Manager.managerImpl.init("");
Emeric Vigier62ca14d2012-08-24 11:05:09 -0400378 break;
Emeric Vigier62ca14d2012-08-24 11:05:09 -0400379 case R.id.buttonCallVoid:
Emeric Vigier9701e032012-09-12 12:38:01 -0400380 Manager.callVoid();
Emeric Vigier62ca14d2012-08-24 11:05:09 -0400381 break;
382 case R.id.buttonGetNewData:
Emeric Vigier9701e032012-09-12 12:38:01 -0400383 Data d = Manager.getNewData(42, "foo");
Emeric Vigier62ca14d2012-08-24 11:05:09 -0400384 if (d != null)
385 buttonFragment.getNewDataText().setText("getNewData(42, \"foo\") == Data(" + d.i + ", \"" + d.s + "\")");
386 break;
387 case R.id.buttonGetDataString:
Emeric Vigier383b2a22012-08-27 14:20:05 -0400388 Data daita = new Data(43, "bar");
Emeric Vigier9701e032012-09-12 12:38:01 -0400389 String s = Manager.getDataString(daita);
Emeric Vigier62ca14d2012-08-24 11:05:09 -0400390 if (s != "") {
Emeric Vigier383b2a22012-08-27 14:20:05 -0400391 buttonFragment.getDataStringText().setText("getDataString(Data(43, \"bar\")) == \"" + s + "\"");
Emeric Vigier62ca14d2012-08-24 11:05:09 -0400392 }
393 break;
394 default:
395 Log.w(TAG, "unknown button " + view.getId());
396 break;
Emeric Vigier05e894e2012-08-20 13:53:02 -0400397 }
Emeric Vigier05e894e2012-08-20 13:53:02 -0400398 }
Adrien Béraudffd32412012-08-07 18:39:23 -0400399}