blob: d3f000e39ca7e3e66426a8098d635bcd5c0ec656 [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 Vigier383b2a22012-08-27 14:20:05 -040071 Handler callbackHandler;
Emeric Vigier9701e032012-09-12 12:38:01 -040072 private Manager manager;
Emeric Vigier78a53e92012-08-29 18:15:34 -040073 /* default callID */
74 static String callID = "007";
Emeric Vigiere89b48b2012-08-30 14:16:46 -040075 static boolean callOnGoing = false;
Emeric Vigiera6bdb702012-08-31 15:34:53 -040076 private String incomingCallID = "";
Alexandre Savard1bcaf532012-09-05 16:23:02 -040077 private static final int REQUEST_CODE_PREFERENCES = 1;
Emeric Vigier090ab602012-09-17 11:09:46 -040078 ImageButton buttonCall, buttonHangup;
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);
Alexandre Savarddef3bfa2012-09-12 16:20:06 -040097 // final ActionBar actionBar = getActionBar();
Adrien Béraudffd32412012-08-07 18:39:23 -040098
99 // Set up the ViewPager with the sections adapter.
100 mViewPager = (ViewPager) findViewById(R.id.pager);
101 mViewPager.setAdapter(mSectionsPagerAdapter);
102
103 // When swiping between different sections, select the corresponding tab.
104 // We can also use ActionBar.Tab#select() to do this if we have a reference to the
105 // Tab.
106 mViewPager.setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener()
107 {
108 @Override
109 public void onPageSelected(int position)
110 {
111 actionBar.setSelectedNavigationItem(position);
112 }
113 });
114
115 // For each of the sections in the app, add a tab to the action bar.
116 for (int i = 0; i < mSectionsPagerAdapter.getCount(); i++) {
117 // Create a tab with text corresponding to the page title defined by the adapter.
118 // Also specify this Activity object, which implements the TabListener interface, as the
119 // listener for when this tab is selected.
120 actionBar.addTab(actionBar.newTab().setIcon(icon_res_id[i]).setText(mSectionsPagerAdapter.getPageTitle(i)).setTabListener(this));
121 }
Emeric Vigier383b2a22012-08-27 14:20:05 -0400122
123 // FIXME
124 callbackHandler = new Handler() {
125 public void handleMessage(Message msg) {
126 Bundle b = msg.getData();
127 TextView callVoidText;
128
Emeric Vigierc411ce32012-08-29 09:28:28 -0400129 Log.i(TAG, "handleMessage");
Emeric Vigier383b2a22012-08-27 14:20:05 -0400130
131 callVoidText = buttonFragment.getcallVoidText();
132 if (callVoidText == null)
133 Log.e(TAG, "SFLPhoneHome: callVoidText is " + callVoidText);
134 callVoidText.setText(b.getString("callback_string"));
135
Emeric Vigierc411ce32012-08-29 09:28:28 -0400136 Log.i(TAG, "handleMessage: " + b.getString("callback_string"));
Emeric Vigier383b2a22012-08-27 14:20:05 -0400137 }
138 };
Emeric Vigiera6bdb702012-08-31 15:34:53 -0400139
140 buttonCall = (ImageButton) findViewById(R.id.buttonCall);
141 buttonHangup = (ImageButton) findViewById(R.id.buttonHangUp);
Alexandre Savarddef3bfa2012-09-12 16:20:06 -0400142
Emeric Vigier9701e032012-09-12 12:38:01 -0400143 manager = new Manager(callbackHandler);
144 Log.i(TAG, "ManagerImpl::instance() = " + manager.managerImpl);
145 manager.setActivity(this);
146 /* set static AppPath before calling manager.init */
147 manager.managerImpl.setPath(getAppPath());
148 Log.i(TAG, "manager created with callbackHandler " + callbackHandler);
Emeric Vigiera6bdb702012-08-31 15:34:53 -0400149
Adrien Béraudffd32412012-08-07 18:39:23 -0400150 }
151
Emeric Vigier62ca14d2012-08-24 11:05:09 -0400152 // FIXME
153 static {
154 System.loadLibrary("gnustl_shared");
155 System.loadLibrary("expat");
156 System.loadLibrary("yaml");
157 System.loadLibrary("ccgnu2");
158 System.loadLibrary("crypto");
159 System.loadLibrary("ssl");
160 System.loadLibrary("ccrtp1");
161 System.loadLibrary("dbus");
162 System.loadLibrary("dbus-c++-1");
163 System.loadLibrary("samplerate");
164 System.loadLibrary("codec_ulaw");
165 System.loadLibrary("codec_alaw");
166 System.loadLibrary("speexresampler");
167 System.loadLibrary("sflphone");
168 }
Alexandre Savard1bcaf532012-09-05 16:23:02 -0400169
170 @Override
171 public boolean onOptionsItemSelected(MenuItem item) {
Alexandre Savardebc94432012-09-13 14:30:55 -0400172 Log.i("SFLphone", "onOptionsItemSelected " + item.getItemId());
Alexandre Savard1bcaf532012-09-05 16:23:02 -0400173
Alexandre Savardebc94432012-09-13 14:30:55 -0400174 if(item.getItemId() != 0) {
175 // When the button is clicked, launch an activity through this intent
176 Intent launchPreferencesIntent = new Intent().setClass(this, SFLPhonePreferenceActivity.class);
Alexandre Savard1bcaf532012-09-05 16:23:02 -0400177
Alexandre Savardebc94432012-09-13 14:30:55 -0400178 // Make it a subactivity so we know when it returns
179 startActivityForResult(launchPreferencesIntent, REQUEST_CODE_PREFERENCES);
180 }
Alexandre Savard1bcaf532012-09-05 16:23:02 -0400181
182 return super.onOptionsItemSelected(item);
183 }
Emeric Vigier62ca14d2012-08-24 11:05:09 -0400184
Adrien Béraudffd32412012-08-07 18:39:23 -0400185 @Override
186 public boolean onCreateOptionsMenu(Menu menu)
187 {
188 getMenuInflater().inflate(R.menu.activity_sflphone_home, menu);
189 return true;
190 }
191
192 @Override
193 public void onTabUnselected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction)
194 {
195 }
196
197 @Override
198 public void onTabSelected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction)
199 {
200 // When the given tab is selected, switch to the corresponding page in the ViewPager.
201 mViewPager.setCurrentItem(tab.getPosition());
202 }
203
204 @Override
205 public void onTabReselected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction)
206 {
Emeric Vigier05e894e2012-08-20 13:53:02 -0400207// Log.d(TAG, "onTabReselected");
Adrien Béraudffd32412012-08-07 18:39:23 -0400208 }
209
Emeric Vigiera6bdb702012-08-31 15:34:53 -0400210 public void setIncomingCallID(String id) {
211 incomingCallID = id;
212 }
213
Adrien Béraudffd32412012-08-07 18:39:23 -0400214 /**
215 * A {@link FragmentStatePagerAdapter} that returns a fragment corresponding to
216 * one of the primary sections of the app.
217 */
218 public class SectionsPagerAdapter extends FragmentStatePagerAdapter
219 {
220
221 public SectionsPagerAdapter(FragmentManager fm)
222 {
223 super(fm);
224 }
225
226 @Override
227 public Fragment getItem(int i)
228 {
229 Fragment fragment;
Emeric Vigier62ca14d2012-08-24 11:05:09 -0400230
Emeric Vigier05e894e2012-08-20 13:53:02 -0400231 switch (i) {
232 case 0:
Adrien Béraudffd32412012-08-07 18:39:23 -0400233 fragment = new CallElementList();
Emeric Vigier05e894e2012-08-20 13:53:02 -0400234 break;
235 case 1:
Adrien Béraudffd32412012-08-07 18:39:23 -0400236 fragment = new DummySectionFragment();
Emeric Vigier05e894e2012-08-20 13:53:02 -0400237 break;
238 case 2:
Alexandre Savarddef3bfa2012-09-12 16:20:06 -0400239 fragment = new ButtonSectionFragment();
240 Log.i(TAG, "getItem: fragment is " + fragment);
Emeric Vigier05e894e2012-08-20 13:53:02 -0400241 break;
242 default:
243 Log.e(TAG, "getItem: unknown tab position " + i);
244 return null;
245 }
246
Adrien Béraudffd32412012-08-07 18:39:23 -0400247 Bundle args = new Bundle();
248 args.putInt(DummySectionFragment.ARG_SECTION_NUMBER, i + 1);
249 fragment.setArguments(args);
250 return fragment;
251 }
252
253 @Override
254 public int getCount()
255 {
Emeric Vigier05e894e2012-08-20 13:53:02 -0400256 return 3;
Adrien Béraudffd32412012-08-07 18:39:23 -0400257 }
258
259 @Override
260 public CharSequence getPageTitle(int position)
261 {
262 switch (position) {
263 case 0:
264 return getString(R.string.title_section1).toUpperCase();
265 case 1:
266 return getString(R.string.title_section2).toUpperCase();
Emeric Vigier05e894e2012-08-20 13:53:02 -0400267 case 2:
268 return getString(R.string.title_section3).toUpperCase();
269 default:
270 Log.e(TAG, "getPageTitle: unknown tab position " + position);
271 break;
Adrien Béraudffd32412012-08-07 18:39:23 -0400272 }
273 return null;
274 }
275 }
276
277 /**
278 * A dummy fragment representing a section of the app, but that simply
279 * displays dummy text.
280 */
281 public static class DummySectionFragment extends Fragment
282 {
283 public DummySectionFragment()
284 {
285 setRetainInstance(true);
286 }
287
288 public static final String ARG_SECTION_NUMBER = "section_number";
289
290 @Override
Emeric Vigier05e894e2012-08-20 13:53:02 -0400291 public View onCreateView(LayoutInflater inflater, ViewGroup parent, Bundle savedInstanceState)
Adrien Béraudffd32412012-08-07 18:39:23 -0400292 {
293 TextView textView = new TextView(getActivity());
294 textView.setGravity(Gravity.CENTER);
295 Bundle args = getArguments();
296 textView.setText(Integer.toString(args.getInt(ARG_SECTION_NUMBER)));
Emeric Vigier05e894e2012-08-20 13:53:02 -0400297 textView.setText("java sucks");
Adrien Béraudffd32412012-08-07 18:39:23 -0400298 return textView;
299 }
Adrien Béraudffd32412012-08-07 18:39:23 -0400300 }
Emeric Vigier05e894e2012-08-20 13:53:02 -0400301
Emeric Vigier7a255aa2012-08-29 09:49:18 -0400302 public String getAppPath() {
303 PackageManager pkgMng = getPackageManager();
304 String pkgName = getPackageName();
305
306 try {
307 PackageInfo pkgInfo = pkgMng.getPackageInfo(pkgName, 0);
308 pkgName = pkgInfo.applicationInfo.dataDir;
309 } catch (NameNotFoundException e) {
310 Log.w(TAG, "Error Package name not found ", e);
311 }
312
313 Log.d(TAG, "Application path: " + pkgName);
314 return pkgName;
Emeric Vigier62ca14d2012-08-24 11:05:09 -0400315 }
Emeric Vigier05e894e2012-08-20 13:53:02 -0400316
Emeric Vigier62ca14d2012-08-24 11:05:09 -0400317 @Override
318 public void onClick(View view)
Emeric Vigiere89b48b2012-08-30 14:16:46 -0400319 {
Emeric Vigier62ca14d2012-08-24 11:05:09 -0400320 switch (view.getId()) {
Emeric Vigierd5c17f52012-08-29 09:29:33 -0400321 case R.id.buttonCall:
Emeric Vigier39a11fa2012-08-29 15:25:09 -0400322 TextView textView = (TextView) findViewById(R.id.editAccountID);
323 String accountID = textView.getText().toString();
324 EditText editText;
Emeric Vigier78a53e92012-08-29 18:15:34 -0400325 Random random = new Random();
Emeric Vigiere89b48b2012-08-30 14:16:46 -0400326
Emeric Vigiera6bdb702012-08-31 15:34:53 -0400327 if (incomingCallID != "") {
328 buttonCall.clearAnimation();
Emeric Vigier9380ae52012-09-14 17:40:39 -0400329// manager.managerImpl.answerCall(incomingCallID);
330 manager.callmanagerJNI.accept(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
Emeric Vigiera6bdb702012-08-31 15:34:53 -0400345 callID = Integer.toString(random.nextInt());
346
Emeric Vigier9701e032012-09-12 12:38:01 -0400347 Log.d(TAG, "manager.managerImpl.placeCall(" + accountID + ", " + callID + ", " + to + ");");
Emeric Vigier9380ae52012-09-14 17:40:39 -0400348// manager.managerImpl.outgoingCall(accountID, callID, to);
349 manager.callmanagerJNI.placeCall(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 Vigier9380ae52012-09-14 17:40:39 -0400359// manager.managerImpl.refuseCall(incomingCallID);
360 manager.callmanagerJNI.refuse(incomingCallID);
Emeric Vigiera6bdb702012-08-31 15:34:53 -0400361 incomingCallID="";
362 buttonCall.setEnabled(true);
363 buttonHangup.setEnabled(true);
364 } else {
365 if (callOnGoing == true) {
Emeric Vigier9701e032012-09-12 12:38:01 -0400366 Log.d(TAG, "manager.managerImpl.hangUp(" + callID + ");");
Emeric Vigier9380ae52012-09-14 17:40:39 -0400367// manager.managerImpl.hangupCall(callID);
368 manager.callmanagerJNI.hangUp(callID);
Emeric Vigiera6bdb702012-08-31 15:34:53 -0400369 callOnGoing = false;
370 buttonCall.setEnabled(true);
371 buttonHangup.setEnabled(false);
372 }
Emeric Vigiere89b48b2012-08-30 14:16:46 -0400373 }
Emeric Vigiera6bdb702012-08-31 15:34:53 -0400374
375 buttonCall.setImageResource(R.drawable.ic_call);
Emeric Vigier78a53e92012-08-29 18:15:34 -0400376 break;
Emeric Vigier62ca14d2012-08-24 11:05:09 -0400377 case R.id.buttonInit:
Emeric Vigier9701e032012-09-12 12:38:01 -0400378 Manager.managerImpl.setPath("");
379 Manager.managerImpl.init("");
Emeric Vigier62ca14d2012-08-24 11:05:09 -0400380 break;
Emeric Vigier62ca14d2012-08-24 11:05:09 -0400381 case R.id.buttonCallVoid:
Emeric Vigier9701e032012-09-12 12:38:01 -0400382 Manager.callVoid();
Emeric Vigier62ca14d2012-08-24 11:05:09 -0400383 break;
384 case R.id.buttonGetNewData:
Emeric Vigier9701e032012-09-12 12:38:01 -0400385 Data d = Manager.getNewData(42, "foo");
Emeric Vigier62ca14d2012-08-24 11:05:09 -0400386 if (d != null)
387 buttonFragment.getNewDataText().setText("getNewData(42, \"foo\") == Data(" + d.i + ", \"" + d.s + "\")");
388 break;
389 case R.id.buttonGetDataString:
Emeric Vigier383b2a22012-08-27 14:20:05 -0400390 Data daita = new Data(43, "bar");
Emeric Vigier9701e032012-09-12 12:38:01 -0400391 String s = Manager.getDataString(daita);
Emeric Vigier62ca14d2012-08-24 11:05:09 -0400392 if (s != "") {
Emeric Vigier383b2a22012-08-27 14:20:05 -0400393 buttonFragment.getDataStringText().setText("getDataString(Data(43, \"bar\")) == \"" + s + "\"");
Emeric Vigier62ca14d2012-08-24 11:05:09 -0400394 }
395 break;
396 default:
397 Log.w(TAG, "unknown button " + view.getId());
398 break;
Emeric Vigier05e894e2012-08-20 13:53:02 -0400399 }
Emeric Vigier05e894e2012-08-20 13:53:02 -0400400 }
Adrien Béraudffd32412012-08-07 18:39:23 -0400401}