blob: 61dc41c7e9e4e2dc0b22df3894f994ec68248ea2 [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;
73 static ManagerImpl managerImpl;
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 Vigier7a255aa2012-08-29 09:49:18 -0400138 ManagerImpl.setAppPath(getAppPath());
Emeric Vigier383b2a22012-08-27 14:20:05 -0400139 managerImpl = new ManagerImpl(callbackHandler);
Emeric Vigiera6bdb702012-08-31 15:34:53 -0400140 managerImpl.setActivity(this);
Emeric Vigier383b2a22012-08-27 14:20:05 -0400141 Log.i(TAG, "managerImpl created with callbackHandler " + callbackHandler);
Emeric Vigiera6bdb702012-08-31 15:34:53 -0400142
143 buttonCall = (ImageButton) findViewById(R.id.buttonCall);
144 buttonHangup = (ImageButton) findViewById(R.id.buttonHangUp);
145// buttonIncomingCall = (ImageButton) findViewById(R.id.buttonIncomingCall);
Adrien Béraudffd32412012-08-07 18:39:23 -0400146 }
147
Emeric Vigier62ca14d2012-08-24 11:05:09 -0400148 // FIXME
149 static {
150 System.loadLibrary("gnustl_shared");
151 System.loadLibrary("expat");
152 System.loadLibrary("yaml");
153 System.loadLibrary("ccgnu2");
154 System.loadLibrary("crypto");
155 System.loadLibrary("ssl");
156 System.loadLibrary("ccrtp1");
157 System.loadLibrary("dbus");
158 System.loadLibrary("dbus-c++-1");
159 System.loadLibrary("samplerate");
160 System.loadLibrary("codec_ulaw");
161 System.loadLibrary("codec_alaw");
162 System.loadLibrary("speexresampler");
163 System.loadLibrary("sflphone");
164 }
Alexandre Savard1bcaf532012-09-05 16:23:02 -0400165
166 @Override
167 public boolean onOptionsItemSelected(MenuItem item) {
168 Log.i("SFLphone", "onOptionsItemSelected");
169
170 // When the button is clicked, launch an activity through this intent
171 Intent launchPreferencesIntent = new Intent().setClass(this, SFLPhonePreferenceActivity.class);
172
173 // Make it a subactivity so we know when it returns
174 startActivityForResult(launchPreferencesIntent, REQUEST_CODE_PREFERENCES);
175
176 return super.onOptionsItemSelected(item);
177 }
Emeric Vigier62ca14d2012-08-24 11:05:09 -0400178
Adrien Béraudffd32412012-08-07 18:39:23 -0400179 @Override
180 public boolean onCreateOptionsMenu(Menu menu)
181 {
182 getMenuInflater().inflate(R.menu.activity_sflphone_home, menu);
183 return true;
184 }
185
186 @Override
187 public void onTabUnselected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction)
188 {
189 }
190
191 @Override
192 public void onTabSelected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction)
193 {
194 // When the given tab is selected, switch to the corresponding page in the ViewPager.
195 mViewPager.setCurrentItem(tab.getPosition());
196 }
197
198 @Override
199 public void onTabReselected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction)
200 {
Emeric Vigier05e894e2012-08-20 13:53:02 -0400201// Log.d(TAG, "onTabReselected");
202// ManagerImpl.initN("");
Adrien Béraudffd32412012-08-07 18:39:23 -0400203 }
204
Emeric Vigiera6bdb702012-08-31 15:34:53 -0400205 public void setIncomingCallID(String id) {
206 incomingCallID = id;
207 }
208
Adrien Béraudffd32412012-08-07 18:39:23 -0400209 /**
210 * A {@link FragmentStatePagerAdapter} that returns a fragment corresponding to
211 * one of the primary sections of the app.
212 */
213 public class SectionsPagerAdapter extends FragmentStatePagerAdapter
214 {
215
216 public SectionsPagerAdapter(FragmentManager fm)
217 {
218 super(fm);
219 }
220
221 @Override
222 public Fragment getItem(int i)
223 {
224 Fragment fragment;
Emeric Vigier62ca14d2012-08-24 11:05:09 -0400225
Emeric Vigier05e894e2012-08-20 13:53:02 -0400226 switch (i) {
227 case 0:
Adrien Béraudffd32412012-08-07 18:39:23 -0400228 fragment = new CallElementList();
Emeric Vigier05e894e2012-08-20 13:53:02 -0400229 break;
230 case 1:
Adrien Béraudffd32412012-08-07 18:39:23 -0400231 fragment = new DummySectionFragment();
Emeric Vigier05e894e2012-08-20 13:53:02 -0400232 break;
233 case 2:
Emeric Vigier62ca14d2012-08-24 11:05:09 -0400234 buttonFragment = new ButtonSectionFragment();
235 Log.i(TAG, "getItem: fragment is " + buttonFragment);
236 fragment = buttonFragment;
Emeric Vigiera6bdb702012-08-31 15:34:53 -0400237 managerImpl.setButtonFragment(buttonFragment);
Emeric Vigier05e894e2012-08-20 13:53:02 -0400238 break;
239 default:
240 Log.e(TAG, "getItem: unknown tab position " + i);
241 return null;
242 }
243
Adrien Béraudffd32412012-08-07 18:39:23 -0400244 Bundle args = new Bundle();
245 args.putInt(DummySectionFragment.ARG_SECTION_NUMBER, i + 1);
246 fragment.setArguments(args);
247 return fragment;
248 }
249
250 @Override
251 public int getCount()
252 {
Emeric Vigier05e894e2012-08-20 13:53:02 -0400253 return 3;
Adrien Béraudffd32412012-08-07 18:39:23 -0400254 }
255
256 @Override
257 public CharSequence getPageTitle(int position)
258 {
259 switch (position) {
260 case 0:
261 return getString(R.string.title_section1).toUpperCase();
262 case 1:
263 return getString(R.string.title_section2).toUpperCase();
Emeric Vigier05e894e2012-08-20 13:53:02 -0400264 case 2:
265 return getString(R.string.title_section3).toUpperCase();
266 default:
267 Log.e(TAG, "getPageTitle: unknown tab position " + position);
268 break;
Adrien Béraudffd32412012-08-07 18:39:23 -0400269 }
270 return null;
271 }
272 }
273
274 /**
275 * A dummy fragment representing a section of the app, but that simply
276 * displays dummy text.
277 */
278 public static class DummySectionFragment extends Fragment
279 {
280 public DummySectionFragment()
281 {
282 setRetainInstance(true);
283 }
284
285 public static final String ARG_SECTION_NUMBER = "section_number";
286
287 @Override
Emeric Vigier05e894e2012-08-20 13:53:02 -0400288 public View onCreateView(LayoutInflater inflater, ViewGroup parent, Bundle savedInstanceState)
Adrien Béraudffd32412012-08-07 18:39:23 -0400289 {
290 TextView textView = new TextView(getActivity());
291 textView.setGravity(Gravity.CENTER);
292 Bundle args = getArguments();
293 textView.setText(Integer.toString(args.getInt(ARG_SECTION_NUMBER)));
Emeric Vigier05e894e2012-08-20 13:53:02 -0400294 textView.setText("java sucks");
Adrien Béraudffd32412012-08-07 18:39:23 -0400295 return textView;
296 }
Adrien Béraudffd32412012-08-07 18:39:23 -0400297 }
Emeric Vigier05e894e2012-08-20 13:53:02 -0400298
Emeric Vigier7a255aa2012-08-29 09:49:18 -0400299 public String getAppPath() {
300 PackageManager pkgMng = getPackageManager();
301 String pkgName = getPackageName();
302
303 try {
304 PackageInfo pkgInfo = pkgMng.getPackageInfo(pkgName, 0);
305 pkgName = pkgInfo.applicationInfo.dataDir;
306 } catch (NameNotFoundException e) {
307 Log.w(TAG, "Error Package name not found ", e);
308 }
309
310 Log.d(TAG, "Application path: " + pkgName);
311 return pkgName;
Emeric Vigier62ca14d2012-08-24 11:05:09 -0400312 }
Emeric Vigier05e894e2012-08-20 13:53:02 -0400313
Emeric Vigier62ca14d2012-08-24 11:05:09 -0400314 @Override
315 public void onClick(View view)
Emeric Vigiere89b48b2012-08-30 14:16:46 -0400316 {
Emeric Vigiera6bdb702012-08-31 15:34:53 -0400317 buttonCall = buttonFragment.getCallButton();
318 buttonHangup = buttonFragment.getHangUpButton();
319
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();
329 ManagerImpl.answerCall(incomingCallID);
330 callID = incomingCallID;
331 incomingCallID="";
Emeric Vigiere89b48b2012-08-30 14:16:46 -0400332 callOnGoing = true;
Emeric Vigiera6bdb702012-08-31 15:34:53 -0400333 buttonCall.setEnabled(false);
334 buttonHangup.setEnabled(true);
335 } else {
336 if (callOnGoing == false) {
337 editText = (EditText) findViewById(R.id.editTo);
338 String to = editText.getText().toString();
339 if (to == null) {
340 Log.e(TAG, "to string is " + to);
341 break;
342 }
343
344 /* new random callID */
345 callID = Integer.toString(random.nextInt());
346
347 Log.d(TAG, "ManagerImpl.placeCall(" + accountID + ", " + callID + ", " + to + ");");
348 ManagerImpl.placeCall(accountID, callID, to);
349 callOnGoing = true;
350 buttonCall.setEnabled(false);
351 buttonHangup.setEnabled(true);
352 }
Emeric Vigierd5c17f52012-08-29 09:29:33 -0400353 }
Emeric Vigierd5c17f52012-08-29 09:29:33 -0400354 break;
Emeric Vigier78a53e92012-08-29 18:15:34 -0400355 case R.id.buttonHangUp:
Emeric Vigiera6bdb702012-08-31 15:34:53 -0400356 if (incomingCallID != "") {
357 buttonCall.clearAnimation();
358 ManagerImpl.refuseCall(incomingCallID);
359 incomingCallID="";
360 buttonCall.setEnabled(true);
361 buttonHangup.setEnabled(true);
362 } else {
363 if (callOnGoing == true) {
364 Log.d(TAG, "ManagerImpl.hangUp(" + callID + ");");
365 ManagerImpl.hangUp(callID);
366 callOnGoing = false;
367 buttonCall.setEnabled(true);
368 buttonHangup.setEnabled(false);
369 }
Emeric Vigiere89b48b2012-08-30 14:16:46 -0400370 }
Emeric Vigiera6bdb702012-08-31 15:34:53 -0400371
372 buttonCall.setImageResource(R.drawable.ic_call);
Emeric Vigier78a53e92012-08-29 18:15:34 -0400373 break;
Emeric Vigier62ca14d2012-08-24 11:05:09 -0400374 case R.id.buttonInit:
375 ManagerImpl.initN("");
376 break;
Emeric Vigier62ca14d2012-08-24 11:05:09 -0400377 case R.id.buttonCallVoid:
378 ManagerImpl.callVoid();
379 break;
380 case R.id.buttonGetNewData:
381 Data d = ManagerImpl.getNewData(42, "foo");
382 if (d != null)
383 buttonFragment.getNewDataText().setText("getNewData(42, \"foo\") == Data(" + d.i + ", \"" + d.s + "\")");
384 break;
385 case R.id.buttonGetDataString:
Emeric Vigier383b2a22012-08-27 14:20:05 -0400386 Data daita = new Data(43, "bar");
387 String s = ManagerImpl.getDataString(daita);
Emeric Vigier62ca14d2012-08-24 11:05:09 -0400388 if (s != "") {
Emeric Vigier383b2a22012-08-27 14:20:05 -0400389 buttonFragment.getDataStringText().setText("getDataString(Data(43, \"bar\")) == \"" + s + "\"");
Emeric Vigier62ca14d2012-08-24 11:05:09 -0400390 }
391 break;
392 default:
393 Log.w(TAG, "unknown button " + view.getId());
394 break;
Emeric Vigier05e894e2012-08-20 13:53:02 -0400395 }
Emeric Vigier05e894e2012-08-20 13:53:02 -0400396 }
Adrien Béraudffd32412012-08-07 18:39:23 -0400397}