blob: 900dfde3c1ce5084de10fb185975242e7c0689f1 [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
Alexandre Savarda04c5202012-09-18 17:19:53 -040085 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 -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.
Alexandre Savarda04c5202012-09-18 17:19:53 -0400120 Log.i(TAG, "adding tab: " + i);
Adrien Béraudffd32412012-08-07 18:39:23 -0400121 actionBar.addTab(actionBar.newTab().setIcon(icon_res_id[i]).setText(mSectionsPagerAdapter.getPageTitle(i)).setTabListener(this));
122 }
Emeric Vigier383b2a22012-08-27 14:20:05 -0400123
124 // FIXME
125 callbackHandler = new Handler() {
126 public void handleMessage(Message msg) {
127 Bundle b = msg.getData();
128 TextView callVoidText;
129
Emeric Vigierc411ce32012-08-29 09:28:28 -0400130 Log.i(TAG, "handleMessage");
Emeric Vigier383b2a22012-08-27 14:20:05 -0400131
132 callVoidText = buttonFragment.getcallVoidText();
133 if (callVoidText == null)
134 Log.e(TAG, "SFLPhoneHome: callVoidText is " + callVoidText);
135 callVoidText.setText(b.getString("callback_string"));
136
Emeric Vigierc411ce32012-08-29 09:28:28 -0400137 Log.i(TAG, "handleMessage: " + b.getString("callback_string"));
Emeric Vigier383b2a22012-08-27 14:20:05 -0400138 }
139 };
Emeric Vigiera6bdb702012-08-31 15:34:53 -0400140
141 buttonCall = (ImageButton) findViewById(R.id.buttonCall);
142 buttonHangup = (ImageButton) findViewById(R.id.buttonHangUp);
Alexandre Savarddef3bfa2012-09-12 16:20:06 -0400143
Emeric Vigier9701e032012-09-12 12:38:01 -0400144 manager = new Manager(callbackHandler);
145 Log.i(TAG, "ManagerImpl::instance() = " + manager.managerImpl);
146 manager.setActivity(this);
147 /* set static AppPath before calling manager.init */
148 manager.managerImpl.setPath(getAppPath());
149 Log.i(TAG, "manager created with callbackHandler " + callbackHandler);
Emeric Vigiera6bdb702012-08-31 15:34:53 -0400150
Adrien Béraudffd32412012-08-07 18:39:23 -0400151 }
152
Emeric Vigier62ca14d2012-08-24 11:05:09 -0400153 // FIXME
154 static {
155 System.loadLibrary("gnustl_shared");
156 System.loadLibrary("expat");
157 System.loadLibrary("yaml");
158 System.loadLibrary("ccgnu2");
159 System.loadLibrary("crypto");
160 System.loadLibrary("ssl");
161 System.loadLibrary("ccrtp1");
162 System.loadLibrary("dbus");
163 System.loadLibrary("dbus-c++-1");
164 System.loadLibrary("samplerate");
165 System.loadLibrary("codec_ulaw");
166 System.loadLibrary("codec_alaw");
167 System.loadLibrary("speexresampler");
168 System.loadLibrary("sflphone");
169 }
Alexandre Savard1bcaf532012-09-05 16:23:02 -0400170
171 @Override
172 public boolean onOptionsItemSelected(MenuItem item) {
Alexandre Savardebc94432012-09-13 14:30:55 -0400173 Log.i("SFLphone", "onOptionsItemSelected " + item.getItemId());
Alexandre Savard1bcaf532012-09-05 16:23:02 -0400174
Alexandre Savardebc94432012-09-13 14:30:55 -0400175 if(item.getItemId() != 0) {
176 // When the button is clicked, launch an activity through this intent
177 Intent launchPreferencesIntent = new Intent().setClass(this, SFLPhonePreferenceActivity.class);
Alexandre Savard1bcaf532012-09-05 16:23:02 -0400178
Alexandre Savardebc94432012-09-13 14:30:55 -0400179 // Make it a subactivity so we know when it returns
180 startActivityForResult(launchPreferencesIntent, REQUEST_CODE_PREFERENCES);
181 }
Alexandre Savard1bcaf532012-09-05 16:23:02 -0400182
183 return super.onOptionsItemSelected(item);
184 }
Emeric Vigier62ca14d2012-08-24 11:05:09 -0400185
Adrien Béraudffd32412012-08-07 18:39:23 -0400186 @Override
187 public boolean onCreateOptionsMenu(Menu menu)
188 {
189 getMenuInflater().inflate(R.menu.activity_sflphone_home, menu);
190 return true;
191 }
192
193 @Override
194 public void onTabUnselected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction)
195 {
196 }
197
198 @Override
199 public void onTabSelected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction)
200 {
201 // When the given tab is selected, switch to the corresponding page in the ViewPager.
202 mViewPager.setCurrentItem(tab.getPosition());
203 }
204
205 @Override
206 public void onTabReselected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction)
207 {
Emeric Vigier05e894e2012-08-20 13:53:02 -0400208// Log.d(TAG, "onTabReselected");
Adrien Béraudffd32412012-08-07 18:39:23 -0400209 }
210
Emeric Vigiera6bdb702012-08-31 15:34:53 -0400211 public void setIncomingCallID(String id) {
212 incomingCallID = id;
213 }
214
Adrien Béraudffd32412012-08-07 18:39:23 -0400215 /**
216 * A {@link FragmentStatePagerAdapter} that returns a fragment corresponding to
217 * one of the primary sections of the app.
218 */
219 public class SectionsPagerAdapter extends FragmentStatePagerAdapter
220 {
221
222 public SectionsPagerAdapter(FragmentManager fm)
223 {
224 super(fm);
225 }
226
227 @Override
228 public Fragment getItem(int i)
229 {
230 Fragment fragment;
Emeric Vigier62ca14d2012-08-24 11:05:09 -0400231
Emeric Vigier05e894e2012-08-20 13:53:02 -0400232 switch (i) {
Alexandre Savarda04c5202012-09-18 17:19:53 -0400233 case 0:
234 fragment = new ContactListFragment();
235 break;
236 case 1:
Adrien Béraudffd32412012-08-07 18:39:23 -0400237 fragment = new CallElementList();
Emeric Vigier05e894e2012-08-20 13:53:02 -0400238 break;
Alexandre Savarda04c5202012-09-18 17:19:53 -0400239 case 2:
Adrien Béraudffd32412012-08-07 18:39:23 -0400240 fragment = new DummySectionFragment();
Emeric Vigier05e894e2012-08-20 13:53:02 -0400241 break;
Alexandre Savarda04c5202012-09-18 17:19:53 -0400242 case 3:
Alexandre Savarddef3bfa2012-09-12 16:20:06 -0400243 fragment = new ButtonSectionFragment();
244 Log.i(TAG, "getItem: fragment is " + fragment);
Emeric Vigier05e894e2012-08-20 13:53:02 -0400245 break;
246 default:
247 Log.e(TAG, "getItem: unknown tab position " + i);
248 return null;
249 }
250
Adrien Béraudffd32412012-08-07 18:39:23 -0400251 Bundle args = new Bundle();
252 args.putInt(DummySectionFragment.ARG_SECTION_NUMBER, i + 1);
253 fragment.setArguments(args);
254 return fragment;
255 }
256
257 @Override
258 public int getCount()
259 {
Alexandre Savarda04c5202012-09-18 17:19:53 -0400260 return 4;
Adrien Béraudffd32412012-08-07 18:39:23 -0400261 }
262
263 @Override
264 public CharSequence getPageTitle(int position)
265 {
266 switch (position) {
Alexandre Savarda04c5202012-09-18 17:19:53 -0400267 case 0:
268 return getString(R.string.title_section0).toUpperCase();
Adrien Béraudffd32412012-08-07 18:39:23 -0400269 case 1:
Alexandre Savarda04c5202012-09-18 17:19:53 -0400270 return getString(R.string.title_section1).toUpperCase();
Emeric Vigier05e894e2012-08-20 13:53:02 -0400271 case 2:
Alexandre Savarda04c5202012-09-18 17:19:53 -0400272 return getString(R.string.title_section2).toUpperCase();
273 case 3:
Emeric Vigier05e894e2012-08-20 13:53:02 -0400274 return getString(R.string.title_section3).toUpperCase();
275 default:
276 Log.e(TAG, "getPageTitle: unknown tab position " + position);
277 break;
Adrien Béraudffd32412012-08-07 18:39:23 -0400278 }
279 return null;
280 }
281 }
282
283 /**
284 * A dummy fragment representing a section of the app, but that simply
285 * displays dummy text.
286 */
287 public static class DummySectionFragment extends Fragment
288 {
289 public DummySectionFragment()
290 {
291 setRetainInstance(true);
292 }
293
294 public static final String ARG_SECTION_NUMBER = "section_number";
295
296 @Override
Emeric Vigier05e894e2012-08-20 13:53:02 -0400297 public View onCreateView(LayoutInflater inflater, ViewGroup parent, Bundle savedInstanceState)
Adrien Béraudffd32412012-08-07 18:39:23 -0400298 {
299 TextView textView = new TextView(getActivity());
300 textView.setGravity(Gravity.CENTER);
301 Bundle args = getArguments();
302 textView.setText(Integer.toString(args.getInt(ARG_SECTION_NUMBER)));
Emeric Vigier05e894e2012-08-20 13:53:02 -0400303 textView.setText("java sucks");
Adrien Béraudffd32412012-08-07 18:39:23 -0400304 return textView;
305 }
Adrien Béraudffd32412012-08-07 18:39:23 -0400306 }
Emeric Vigier05e894e2012-08-20 13:53:02 -0400307
Emeric Vigier7a255aa2012-08-29 09:49:18 -0400308 public String getAppPath() {
309 PackageManager pkgMng = getPackageManager();
310 String pkgName = getPackageName();
311
312 try {
313 PackageInfo pkgInfo = pkgMng.getPackageInfo(pkgName, 0);
314 pkgName = pkgInfo.applicationInfo.dataDir;
315 } catch (NameNotFoundException e) {
316 Log.w(TAG, "Error Package name not found ", e);
317 }
318
319 Log.d(TAG, "Application path: " + pkgName);
320 return pkgName;
Emeric Vigier62ca14d2012-08-24 11:05:09 -0400321 }
Emeric Vigier05e894e2012-08-20 13:53:02 -0400322
Emeric Vigier62ca14d2012-08-24 11:05:09 -0400323 @Override
324 public void onClick(View view)
Emeric Vigiere89b48b2012-08-30 14:16:46 -0400325 {
Emeric Vigier62ca14d2012-08-24 11:05:09 -0400326 switch (view.getId()) {
Emeric Vigierd5c17f52012-08-29 09:29:33 -0400327 case R.id.buttonCall:
Emeric Vigier39a11fa2012-08-29 15:25:09 -0400328 TextView textView = (TextView) findViewById(R.id.editAccountID);
329 String accountID = textView.getText().toString();
330 EditText editText;
Emeric Vigier78a53e92012-08-29 18:15:34 -0400331 Random random = new Random();
Emeric Vigiere89b48b2012-08-30 14:16:46 -0400332
Emeric Vigiera6bdb702012-08-31 15:34:53 -0400333 if (incomingCallID != "") {
334 buttonCall.clearAnimation();
Emeric Vigier9380ae52012-09-14 17:40:39 -0400335// manager.managerImpl.answerCall(incomingCallID);
336 manager.callmanagerJNI.accept(incomingCallID);
Emeric Vigiera6bdb702012-08-31 15:34:53 -0400337 callID = incomingCallID;
338 incomingCallID="";
Emeric Vigiere89b48b2012-08-30 14:16:46 -0400339 callOnGoing = true;
Emeric Vigiera6bdb702012-08-31 15:34:53 -0400340 buttonCall.setEnabled(false);
341 buttonHangup.setEnabled(true);
342 } else {
343 if (callOnGoing == false) {
344 editText = (EditText) findViewById(R.id.editTo);
345 String to = editText.getText().toString();
346 if (to == null) {
347 Log.e(TAG, "to string is " + to);
348 break;
349 }
350
Emeric Vigiera6bdb702012-08-31 15:34:53 -0400351 callID = Integer.toString(random.nextInt());
352
Emeric Vigier9701e032012-09-12 12:38:01 -0400353 Log.d(TAG, "manager.managerImpl.placeCall(" + accountID + ", " + callID + ", " + to + ");");
Emeric Vigier9380ae52012-09-14 17:40:39 -0400354// manager.managerImpl.outgoingCall(accountID, callID, to);
355 manager.callmanagerJNI.placeCall(accountID, callID, to);
Emeric Vigiera6bdb702012-08-31 15:34:53 -0400356 callOnGoing = true;
357 buttonCall.setEnabled(false);
358 buttonHangup.setEnabled(true);
359 }
Emeric Vigierd5c17f52012-08-29 09:29:33 -0400360 }
Emeric Vigierd5c17f52012-08-29 09:29:33 -0400361 break;
Emeric Vigier78a53e92012-08-29 18:15:34 -0400362 case R.id.buttonHangUp:
Emeric Vigiera6bdb702012-08-31 15:34:53 -0400363 if (incomingCallID != "") {
364 buttonCall.clearAnimation();
Emeric Vigier9380ae52012-09-14 17:40:39 -0400365// manager.managerImpl.refuseCall(incomingCallID);
366 manager.callmanagerJNI.refuse(incomingCallID);
Emeric Vigiera6bdb702012-08-31 15:34:53 -0400367 incomingCallID="";
368 buttonCall.setEnabled(true);
369 buttonHangup.setEnabled(true);
370 } else {
371 if (callOnGoing == true) {
Emeric Vigier9701e032012-09-12 12:38:01 -0400372 Log.d(TAG, "manager.managerImpl.hangUp(" + callID + ");");
Emeric Vigier9380ae52012-09-14 17:40:39 -0400373// manager.managerImpl.hangupCall(callID);
374 manager.callmanagerJNI.hangUp(callID);
Emeric Vigiera6bdb702012-08-31 15:34:53 -0400375 callOnGoing = false;
376 buttonCall.setEnabled(true);
377 buttonHangup.setEnabled(false);
378 }
Emeric Vigiere89b48b2012-08-30 14:16:46 -0400379 }
Emeric Vigiera6bdb702012-08-31 15:34:53 -0400380
381 buttonCall.setImageResource(R.drawable.ic_call);
Emeric Vigier78a53e92012-08-29 18:15:34 -0400382 break;
Emeric Vigier62ca14d2012-08-24 11:05:09 -0400383 case R.id.buttonInit:
Emeric Vigier9701e032012-09-12 12:38:01 -0400384 Manager.managerImpl.setPath("");
385 Manager.managerImpl.init("");
Emeric Vigier62ca14d2012-08-24 11:05:09 -0400386 break;
Emeric Vigier62ca14d2012-08-24 11:05:09 -0400387 case R.id.buttonCallVoid:
Emeric Vigier9701e032012-09-12 12:38:01 -0400388 Manager.callVoid();
Emeric Vigier62ca14d2012-08-24 11:05:09 -0400389 break;
390 case R.id.buttonGetNewData:
Emeric Vigier9701e032012-09-12 12:38:01 -0400391 Data d = Manager.getNewData(42, "foo");
Emeric Vigier62ca14d2012-08-24 11:05:09 -0400392 if (d != null)
393 buttonFragment.getNewDataText().setText("getNewData(42, \"foo\") == Data(" + d.i + ", \"" + d.s + "\")");
394 break;
395 case R.id.buttonGetDataString:
Emeric Vigier383b2a22012-08-27 14:20:05 -0400396 Data daita = new Data(43, "bar");
Emeric Vigier9701e032012-09-12 12:38:01 -0400397 String s = Manager.getDataString(daita);
Emeric Vigier62ca14d2012-08-24 11:05:09 -0400398 if (s != "") {
Emeric Vigier383b2a22012-08-27 14:20:05 -0400399 buttonFragment.getDataStringText().setText("getDataString(Data(43, \"bar\")) == \"" + s + "\"");
Emeric Vigier62ca14d2012-08-24 11:05:09 -0400400 }
401 break;
402 default:
403 Log.w(TAG, "unknown button " + view.getId());
404 break;
Emeric Vigier05e894e2012-08-20 13:53:02 -0400405 }
Emeric Vigier05e894e2012-08-20 13:53:02 -0400406 }
Adrien Béraudffd32412012-08-07 18:39:23 -0400407}