blob: 1cda9153586310b7ddb978ee6e47fc959c9de2c2 [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
33import android.app.ActionBar;
34import android.app.Activity;
35import android.app.Fragment;
36import android.app.FragmentManager;
37import android.app.FragmentTransaction;
Emeric Vigier7a255aa2012-08-29 09:49:18 -040038import android.content.pm.PackageInfo;
39import android.content.pm.PackageManager;
40import android.content.pm.PackageManager.NameNotFoundException;
Adrien Béraudffd32412012-08-07 18:39:23 -040041import android.os.Bundle;
Emeric Vigier383b2a22012-08-27 14:20:05 -040042import android.os.Handler;
43import android.os.Message;
Adrien Béraudffd32412012-08-07 18:39:23 -040044import android.support.v13.app.FragmentStatePagerAdapter;
45import android.support.v4.view.ViewPager;
Emeric Vigier05e894e2012-08-20 13:53:02 -040046import android.util.Log;
Adrien Béraudffd32412012-08-07 18:39:23 -040047import android.view.Gravity;
48import android.view.LayoutInflater;
49import android.view.Menu;
50import android.view.View;
Emeric Vigier05e894e2012-08-20 13:53:02 -040051import android.view.View.OnClickListener;
Adrien Béraudffd32412012-08-07 18:39:23 -040052import android.view.ViewGroup;
Emeric Vigierd5c17f52012-08-29 09:29:33 -040053import android.widget.EditText;
Adrien Béraudffd32412012-08-07 18:39:23 -040054import android.widget.TextView;
55
56import com.savoirfairelinux.sflphone.R;
57
Emeric Vigier62ca14d2012-08-24 11:05:09 -040058public class SFLPhoneHome extends Activity implements ActionBar.TabListener, OnClickListener
Adrien Béraudffd32412012-08-07 18:39:23 -040059{
60 SectionsPagerAdapter mSectionsPagerAdapter;
Emeric Vigier62ca14d2012-08-24 11:05:09 -040061 static final String TAG = "SFLPhoneHome";
62 ButtonSectionFragment buttonFragment;
Emeric Vigier383b2a22012-08-27 14:20:05 -040063 Handler callbackHandler;
64 static ManagerImpl managerImpl;
Adrien Béraudffd32412012-08-07 18:39:23 -040065
66 /**
67 * The {@link ViewPager} that will host the section contents.
68 */
69 ViewPager mViewPager;
70
Emeric Vigier05e894e2012-08-20 13:53:02 -040071 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 -040072
73 @Override
74 public void onCreate(Bundle savedInstanceState)
75 {
76 super.onCreate(savedInstanceState);
77 setContentView(R.layout.activity_sflphone_home);
78
79 mSectionsPagerAdapter = new SectionsPagerAdapter(getFragmentManager());
80
81 final ActionBar actionBar = getActionBar();
82 actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
83
84 // Set up the ViewPager with the sections adapter.
85 mViewPager = (ViewPager) findViewById(R.id.pager);
86 mViewPager.setAdapter(mSectionsPagerAdapter);
87
88 // When swiping between different sections, select the corresponding tab.
89 // We can also use ActionBar.Tab#select() to do this if we have a reference to the
90 // Tab.
91 mViewPager.setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener()
92 {
93 @Override
94 public void onPageSelected(int position)
95 {
96 actionBar.setSelectedNavigationItem(position);
97 }
98 });
99
100 // For each of the sections in the app, add a tab to the action bar.
101 for (int i = 0; i < mSectionsPagerAdapter.getCount(); i++) {
102 // Create a tab with text corresponding to the page title defined by the adapter.
103 // Also specify this Activity object, which implements the TabListener interface, as the
104 // listener for when this tab is selected.
105 actionBar.addTab(actionBar.newTab().setIcon(icon_res_id[i]).setText(mSectionsPagerAdapter.getPageTitle(i)).setTabListener(this));
106 }
Emeric Vigier383b2a22012-08-27 14:20:05 -0400107
108 // FIXME
109 callbackHandler = new Handler() {
110 public void handleMessage(Message msg) {
111 Bundle b = msg.getData();
112 TextView callVoidText;
113
Emeric Vigierc411ce32012-08-29 09:28:28 -0400114 Log.i(TAG, "handleMessage");
Emeric Vigier383b2a22012-08-27 14:20:05 -0400115
116 callVoidText = buttonFragment.getcallVoidText();
117 if (callVoidText == null)
118 Log.e(TAG, "SFLPhoneHome: callVoidText is " + callVoidText);
119 callVoidText.setText(b.getString("callback_string"));
120
Emeric Vigierc411ce32012-08-29 09:28:28 -0400121 Log.i(TAG, "handleMessage: " + b.getString("callback_string"));
Emeric Vigier383b2a22012-08-27 14:20:05 -0400122 }
123 };
Emeric Vigier7a255aa2012-08-29 09:49:18 -0400124 ManagerImpl.setAppPath(getAppPath());
Emeric Vigier383b2a22012-08-27 14:20:05 -0400125 managerImpl = new ManagerImpl(callbackHandler);
126 Log.i(TAG, "managerImpl created with callbackHandler " + callbackHandler);
Adrien Béraudffd32412012-08-07 18:39:23 -0400127 }
128
Emeric Vigier62ca14d2012-08-24 11:05:09 -0400129 // FIXME
130 static {
131 System.loadLibrary("gnustl_shared");
132 System.loadLibrary("expat");
133 System.loadLibrary("yaml");
134 System.loadLibrary("ccgnu2");
135 System.loadLibrary("crypto");
136 System.loadLibrary("ssl");
137 System.loadLibrary("ccrtp1");
138 System.loadLibrary("dbus");
139 System.loadLibrary("dbus-c++-1");
140 System.loadLibrary("samplerate");
141 System.loadLibrary("codec_ulaw");
142 System.loadLibrary("codec_alaw");
143 System.loadLibrary("speexresampler");
144 System.loadLibrary("sflphone");
145 }
146
Adrien Béraudffd32412012-08-07 18:39:23 -0400147 @Override
148 public boolean onCreateOptionsMenu(Menu menu)
149 {
150 getMenuInflater().inflate(R.menu.activity_sflphone_home, menu);
151 return true;
152 }
153
154 @Override
155 public void onTabUnselected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction)
156 {
157 }
158
159 @Override
160 public void onTabSelected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction)
161 {
162 // When the given tab is selected, switch to the corresponding page in the ViewPager.
163 mViewPager.setCurrentItem(tab.getPosition());
164 }
165
166 @Override
167 public void onTabReselected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction)
168 {
Emeric Vigier05e894e2012-08-20 13:53:02 -0400169// Log.d(TAG, "onTabReselected");
170// ManagerImpl.initN("");
Adrien Béraudffd32412012-08-07 18:39:23 -0400171 }
172
173 /**
174 * A {@link FragmentStatePagerAdapter} that returns a fragment corresponding to
175 * one of the primary sections of the app.
176 */
177 public class SectionsPagerAdapter extends FragmentStatePagerAdapter
178 {
179
180 public SectionsPagerAdapter(FragmentManager fm)
181 {
182 super(fm);
183 }
184
185 @Override
186 public Fragment getItem(int i)
187 {
188 Fragment fragment;
Emeric Vigier62ca14d2012-08-24 11:05:09 -0400189
Emeric Vigier05e894e2012-08-20 13:53:02 -0400190 switch (i) {
191 case 0:
Adrien Béraudffd32412012-08-07 18:39:23 -0400192 fragment = new CallElementList();
Emeric Vigier05e894e2012-08-20 13:53:02 -0400193 break;
194 case 1:
Adrien Béraudffd32412012-08-07 18:39:23 -0400195 fragment = new DummySectionFragment();
Emeric Vigier05e894e2012-08-20 13:53:02 -0400196 break;
197 case 2:
Emeric Vigier62ca14d2012-08-24 11:05:09 -0400198 buttonFragment = new ButtonSectionFragment();
199 Log.i(TAG, "getItem: fragment is " + buttonFragment);
200 fragment = buttonFragment;
Emeric Vigier05e894e2012-08-20 13:53:02 -0400201 break;
202 default:
203 Log.e(TAG, "getItem: unknown tab position " + i);
204 return null;
205 }
206
Adrien Béraudffd32412012-08-07 18:39:23 -0400207 Bundle args = new Bundle();
208 args.putInt(DummySectionFragment.ARG_SECTION_NUMBER, i + 1);
209 fragment.setArguments(args);
210 return fragment;
211 }
212
213 @Override
214 public int getCount()
215 {
Emeric Vigier05e894e2012-08-20 13:53:02 -0400216 return 3;
Adrien Béraudffd32412012-08-07 18:39:23 -0400217 }
218
219 @Override
220 public CharSequence getPageTitle(int position)
221 {
222 switch (position) {
223 case 0:
224 return getString(R.string.title_section1).toUpperCase();
225 case 1:
226 return getString(R.string.title_section2).toUpperCase();
Emeric Vigier05e894e2012-08-20 13:53:02 -0400227 case 2:
228 return getString(R.string.title_section3).toUpperCase();
229 default:
230 Log.e(TAG, "getPageTitle: unknown tab position " + position);
231 break;
Adrien Béraudffd32412012-08-07 18:39:23 -0400232 }
233 return null;
234 }
235 }
236
237 /**
238 * A dummy fragment representing a section of the app, but that simply
239 * displays dummy text.
240 */
241 public static class DummySectionFragment extends Fragment
242 {
243 public DummySectionFragment()
244 {
245 setRetainInstance(true);
246 }
247
248 public static final String ARG_SECTION_NUMBER = "section_number";
249
250 @Override
Emeric Vigier05e894e2012-08-20 13:53:02 -0400251 public View onCreateView(LayoutInflater inflater, ViewGroup parent, Bundle savedInstanceState)
Adrien Béraudffd32412012-08-07 18:39:23 -0400252 {
253 TextView textView = new TextView(getActivity());
254 textView.setGravity(Gravity.CENTER);
255 Bundle args = getArguments();
256 textView.setText(Integer.toString(args.getInt(ARG_SECTION_NUMBER)));
Emeric Vigier05e894e2012-08-20 13:53:02 -0400257 textView.setText("java sucks");
Adrien Béraudffd32412012-08-07 18:39:23 -0400258 return textView;
259 }
Adrien Béraudffd32412012-08-07 18:39:23 -0400260 }
Emeric Vigier05e894e2012-08-20 13:53:02 -0400261
Emeric Vigier7a255aa2012-08-29 09:49:18 -0400262 public String getAppPath() {
263 PackageManager pkgMng = getPackageManager();
264 String pkgName = getPackageName();
265
266 try {
267 PackageInfo pkgInfo = pkgMng.getPackageInfo(pkgName, 0);
268 pkgName = pkgInfo.applicationInfo.dataDir;
269 } catch (NameNotFoundException e) {
270 Log.w(TAG, "Error Package name not found ", e);
271 }
272
273 Log.d(TAG, "Application path: " + pkgName);
274 return pkgName;
Emeric Vigier62ca14d2012-08-24 11:05:09 -0400275 }
Emeric Vigier05e894e2012-08-20 13:53:02 -0400276
Emeric Vigier62ca14d2012-08-24 11:05:09 -0400277 @Override
278 public void onClick(View view)
279 {
280 switch (view.getId()) {
Emeric Vigierd5c17f52012-08-29 09:29:33 -0400281 case R.id.buttonCall:
Emeric Vigier39a11fa2012-08-29 15:25:09 -0400282 String callID = "007";
283 TextView textView = (TextView) findViewById(R.id.editAccountID);
284 String accountID = textView.getText().toString();
285 EditText editText;
Emeric Vigierd5c17f52012-08-29 09:29:33 -0400286
287 editText = (EditText) findViewById(R.id.editTo);
288 String to = editText.getText().toString();
289 if (to == null) {
290 Log.e(TAG, "to string is " + to);
291 break;
292 }
293
294 Log.d(TAG, "ManagerImpl.placeCall(" + accountID + ", " + callID + ", " + to + ");");
295 ManagerImpl.placeCall(accountID, callID, to);
296 break;
Emeric Vigier62ca14d2012-08-24 11:05:09 -0400297 case R.id.buttonInit:
298 ManagerImpl.initN("");
299 break;
Emeric Vigier62ca14d2012-08-24 11:05:09 -0400300 case R.id.buttonCallVoid:
301 ManagerImpl.callVoid();
302 break;
303 case R.id.buttonGetNewData:
304 Data d = ManagerImpl.getNewData(42, "foo");
305 if (d != null)
306 buttonFragment.getNewDataText().setText("getNewData(42, \"foo\") == Data(" + d.i + ", \"" + d.s + "\")");
307 break;
308 case R.id.buttonGetDataString:
Emeric Vigier383b2a22012-08-27 14:20:05 -0400309 Data daita = new Data(43, "bar");
310 String s = ManagerImpl.getDataString(daita);
Emeric Vigier62ca14d2012-08-24 11:05:09 -0400311 if (s != "") {
Emeric Vigier383b2a22012-08-27 14:20:05 -0400312 buttonFragment.getDataStringText().setText("getDataString(Data(43, \"bar\")) == \"" + s + "\"");
Emeric Vigier62ca14d2012-08-24 11:05:09 -0400313 }
314 break;
315 default:
316 Log.w(TAG, "unknown button " + view.getId());
317 break;
Emeric Vigier05e894e2012-08-20 13:53:02 -0400318 }
Emeric Vigier05e894e2012-08-20 13:53:02 -0400319 }
Adrien Béraudffd32412012-08-07 18:39:23 -0400320}