blob: 8c2e8e23eb69044b6cf460e077016faa2d6a6c7c [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;
Emeric Vigier7a255aa2012-08-29 09:49:18 -040040import android.content.pm.PackageInfo;
41import android.content.pm.PackageManager;
42import android.content.pm.PackageManager.NameNotFoundException;
Adrien Béraudffd32412012-08-07 18:39:23 -040043import android.os.Bundle;
Emeric Vigier383b2a22012-08-27 14:20:05 -040044import android.os.Handler;
45import android.os.Message;
Adrien Béraudffd32412012-08-07 18:39:23 -040046import android.support.v13.app.FragmentStatePagerAdapter;
47import android.support.v4.view.ViewPager;
Emeric Vigier05e894e2012-08-20 13:53:02 -040048import android.util.Log;
Adrien Béraudffd32412012-08-07 18:39:23 -040049import android.view.Gravity;
50import android.view.LayoutInflater;
51import android.view.Menu;
52import android.view.View;
Emeric Vigier05e894e2012-08-20 13:53:02 -040053import android.view.View.OnClickListener;
Adrien Béraudffd32412012-08-07 18:39:23 -040054import android.view.ViewGroup;
Emeric Vigiere89b48b2012-08-30 14:16:46 -040055import android.widget.Button;
Emeric Vigierd5c17f52012-08-29 09:29:33 -040056import android.widget.EditText;
Emeric Vigiere89b48b2012-08-30 14:16:46 -040057import android.widget.ImageButton;
Adrien Béraudffd32412012-08-07 18:39:23 -040058import android.widget.TextView;
59
60import com.savoirfairelinux.sflphone.R;
61
Emeric Vigier62ca14d2012-08-24 11:05:09 -040062public class SFLPhoneHome extends Activity implements ActionBar.TabListener, OnClickListener
Adrien Béraudffd32412012-08-07 18:39:23 -040063{
64 SectionsPagerAdapter mSectionsPagerAdapter;
Emeric Vigier62ca14d2012-08-24 11:05:09 -040065 static final String TAG = "SFLPhoneHome";
66 ButtonSectionFragment buttonFragment;
Emeric Vigiere89b48b2012-08-30 14:16:46 -040067 ImageButton buttonCall, buttonHangup;
Emeric Vigier383b2a22012-08-27 14:20:05 -040068 Handler callbackHandler;
69 static ManagerImpl managerImpl;
Emeric Vigier78a53e92012-08-29 18:15:34 -040070 /* default callID */
71 static String callID = "007";
Emeric Vigiere89b48b2012-08-30 14:16:46 -040072 static boolean callOnGoing = false;
Adrien Béraudffd32412012-08-07 18:39:23 -040073
74 /**
75 * The {@link ViewPager} that will host the section contents.
76 */
77 ViewPager mViewPager;
78
Emeric Vigier05e894e2012-08-20 13:53:02 -040079 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 -040080
81 @Override
82 public void onCreate(Bundle savedInstanceState)
83 {
84 super.onCreate(savedInstanceState);
85 setContentView(R.layout.activity_sflphone_home);
86
87 mSectionsPagerAdapter = new SectionsPagerAdapter(getFragmentManager());
88
89 final ActionBar actionBar = getActionBar();
90 actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
91
92 // Set up the ViewPager with the sections adapter.
93 mViewPager = (ViewPager) findViewById(R.id.pager);
94 mViewPager.setAdapter(mSectionsPagerAdapter);
95
96 // When swiping between different sections, select the corresponding tab.
97 // We can also use ActionBar.Tab#select() to do this if we have a reference to the
98 // Tab.
99 mViewPager.setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener()
100 {
101 @Override
102 public void onPageSelected(int position)
103 {
104 actionBar.setSelectedNavigationItem(position);
105 }
106 });
107
108 // For each of the sections in the app, add a tab to the action bar.
109 for (int i = 0; i < mSectionsPagerAdapter.getCount(); i++) {
110 // Create a tab with text corresponding to the page title defined by the adapter.
111 // Also specify this Activity object, which implements the TabListener interface, as the
112 // listener for when this tab is selected.
113 actionBar.addTab(actionBar.newTab().setIcon(icon_res_id[i]).setText(mSectionsPagerAdapter.getPageTitle(i)).setTabListener(this));
114 }
Emeric Vigier383b2a22012-08-27 14:20:05 -0400115
116 // FIXME
117 callbackHandler = new Handler() {
118 public void handleMessage(Message msg) {
119 Bundle b = msg.getData();
120 TextView callVoidText;
121
Emeric Vigierc411ce32012-08-29 09:28:28 -0400122 Log.i(TAG, "handleMessage");
Emeric Vigier383b2a22012-08-27 14:20:05 -0400123
124 callVoidText = buttonFragment.getcallVoidText();
125 if (callVoidText == null)
126 Log.e(TAG, "SFLPhoneHome: callVoidText is " + callVoidText);
127 callVoidText.setText(b.getString("callback_string"));
128
Emeric Vigierc411ce32012-08-29 09:28:28 -0400129 Log.i(TAG, "handleMessage: " + b.getString("callback_string"));
Emeric Vigier383b2a22012-08-27 14:20:05 -0400130 }
131 };
Emeric Vigier7a255aa2012-08-29 09:49:18 -0400132 ManagerImpl.setAppPath(getAppPath());
Emeric Vigier383b2a22012-08-27 14:20:05 -0400133 managerImpl = new ManagerImpl(callbackHandler);
134 Log.i(TAG, "managerImpl created with callbackHandler " + callbackHandler);
Adrien Béraudffd32412012-08-07 18:39:23 -0400135 }
136
Emeric Vigier62ca14d2012-08-24 11:05:09 -0400137 // FIXME
138 static {
139 System.loadLibrary("gnustl_shared");
140 System.loadLibrary("expat");
141 System.loadLibrary("yaml");
142 System.loadLibrary("ccgnu2");
143 System.loadLibrary("crypto");
144 System.loadLibrary("ssl");
145 System.loadLibrary("ccrtp1");
146 System.loadLibrary("dbus");
147 System.loadLibrary("dbus-c++-1");
148 System.loadLibrary("samplerate");
149 System.loadLibrary("codec_ulaw");
150 System.loadLibrary("codec_alaw");
151 System.loadLibrary("speexresampler");
152 System.loadLibrary("sflphone");
153 }
154
Adrien Béraudffd32412012-08-07 18:39:23 -0400155 @Override
156 public boolean onCreateOptionsMenu(Menu menu)
157 {
158 getMenuInflater().inflate(R.menu.activity_sflphone_home, menu);
159 return true;
160 }
161
162 @Override
163 public void onTabUnselected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction)
164 {
165 }
166
167 @Override
168 public void onTabSelected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction)
169 {
170 // When the given tab is selected, switch to the corresponding page in the ViewPager.
171 mViewPager.setCurrentItem(tab.getPosition());
172 }
173
174 @Override
175 public void onTabReselected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction)
176 {
Emeric Vigier05e894e2012-08-20 13:53:02 -0400177// Log.d(TAG, "onTabReselected");
178// ManagerImpl.initN("");
Adrien Béraudffd32412012-08-07 18:39:23 -0400179 }
180
181 /**
182 * A {@link FragmentStatePagerAdapter} that returns a fragment corresponding to
183 * one of the primary sections of the app.
184 */
185 public class SectionsPagerAdapter extends FragmentStatePagerAdapter
186 {
187
188 public SectionsPagerAdapter(FragmentManager fm)
189 {
190 super(fm);
191 }
192
193 @Override
194 public Fragment getItem(int i)
195 {
196 Fragment fragment;
Emeric Vigier62ca14d2012-08-24 11:05:09 -0400197
Emeric Vigier05e894e2012-08-20 13:53:02 -0400198 switch (i) {
199 case 0:
Adrien Béraudffd32412012-08-07 18:39:23 -0400200 fragment = new CallElementList();
Emeric Vigier05e894e2012-08-20 13:53:02 -0400201 break;
202 case 1:
Adrien Béraudffd32412012-08-07 18:39:23 -0400203 fragment = new DummySectionFragment();
Emeric Vigier05e894e2012-08-20 13:53:02 -0400204 break;
205 case 2:
Emeric Vigier62ca14d2012-08-24 11:05:09 -0400206 buttonFragment = new ButtonSectionFragment();
207 Log.i(TAG, "getItem: fragment is " + buttonFragment);
208 fragment = buttonFragment;
Emeric Vigier05e894e2012-08-20 13:53:02 -0400209 break;
210 default:
211 Log.e(TAG, "getItem: unknown tab position " + i);
212 return null;
213 }
214
Adrien Béraudffd32412012-08-07 18:39:23 -0400215 Bundle args = new Bundle();
216 args.putInt(DummySectionFragment.ARG_SECTION_NUMBER, i + 1);
217 fragment.setArguments(args);
218 return fragment;
219 }
220
221 @Override
222 public int getCount()
223 {
Emeric Vigier05e894e2012-08-20 13:53:02 -0400224 return 3;
Adrien Béraudffd32412012-08-07 18:39:23 -0400225 }
226
227 @Override
228 public CharSequence getPageTitle(int position)
229 {
230 switch (position) {
231 case 0:
232 return getString(R.string.title_section1).toUpperCase();
233 case 1:
234 return getString(R.string.title_section2).toUpperCase();
Emeric Vigier05e894e2012-08-20 13:53:02 -0400235 case 2:
236 return getString(R.string.title_section3).toUpperCase();
237 default:
238 Log.e(TAG, "getPageTitle: unknown tab position " + position);
239 break;
Adrien Béraudffd32412012-08-07 18:39:23 -0400240 }
241 return null;
242 }
243 }
244
245 /**
246 * A dummy fragment representing a section of the app, but that simply
247 * displays dummy text.
248 */
249 public static class DummySectionFragment extends Fragment
250 {
251 public DummySectionFragment()
252 {
253 setRetainInstance(true);
254 }
255
256 public static final String ARG_SECTION_NUMBER = "section_number";
257
258 @Override
Emeric Vigier05e894e2012-08-20 13:53:02 -0400259 public View onCreateView(LayoutInflater inflater, ViewGroup parent, Bundle savedInstanceState)
Adrien Béraudffd32412012-08-07 18:39:23 -0400260 {
261 TextView textView = new TextView(getActivity());
262 textView.setGravity(Gravity.CENTER);
263 Bundle args = getArguments();
264 textView.setText(Integer.toString(args.getInt(ARG_SECTION_NUMBER)));
Emeric Vigier05e894e2012-08-20 13:53:02 -0400265 textView.setText("java sucks");
Adrien Béraudffd32412012-08-07 18:39:23 -0400266 return textView;
267 }
Adrien Béraudffd32412012-08-07 18:39:23 -0400268 }
Emeric Vigier05e894e2012-08-20 13:53:02 -0400269
Emeric Vigier7a255aa2012-08-29 09:49:18 -0400270 public String getAppPath() {
271 PackageManager pkgMng = getPackageManager();
272 String pkgName = getPackageName();
273
274 try {
275 PackageInfo pkgInfo = pkgMng.getPackageInfo(pkgName, 0);
276 pkgName = pkgInfo.applicationInfo.dataDir;
277 } catch (NameNotFoundException e) {
278 Log.w(TAG, "Error Package name not found ", e);
279 }
280
281 Log.d(TAG, "Application path: " + pkgName);
282 return pkgName;
Emeric Vigier62ca14d2012-08-24 11:05:09 -0400283 }
Emeric Vigier05e894e2012-08-20 13:53:02 -0400284
Emeric Vigier62ca14d2012-08-24 11:05:09 -0400285 @Override
286 public void onClick(View view)
Emeric Vigiere89b48b2012-08-30 14:16:46 -0400287 {
288 buttonCall = (ImageButton) findViewById(R.id.buttonCall);
289 buttonHangup = (ImageButton) findViewById(R.id.buttonHangUp);
290
Emeric Vigier62ca14d2012-08-24 11:05:09 -0400291 switch (view.getId()) {
Emeric Vigierd5c17f52012-08-29 09:29:33 -0400292 case R.id.buttonCall:
Emeric Vigier39a11fa2012-08-29 15:25:09 -0400293 TextView textView = (TextView) findViewById(R.id.editAccountID);
294 String accountID = textView.getText().toString();
295 EditText editText;
Emeric Vigier78a53e92012-08-29 18:15:34 -0400296 Random random = new Random();
Emeric Vigiere89b48b2012-08-30 14:16:46 -0400297
298 if (callOnGoing == false) {
299 editText = (EditText) findViewById(R.id.editTo);
300 String to = editText.getText().toString();
301 if (to == null) {
302 Log.e(TAG, "to string is " + to);
303 break;
304 }
305
306 /* new random callID */
307 callID = Integer.toString(random.nextInt());
308
309 Log.d(TAG, "ManagerImpl.placeCall(" + accountID + ", " + callID + ", " + to + ");");
310 ManagerImpl.placeCall(accountID, callID, to);
311 callOnGoing = true;
312 buttonCall.setEnabled(false);
313 buttonHangup.setEnabled(true);
Emeric Vigierd5c17f52012-08-29 09:29:33 -0400314 }
Emeric Vigierd5c17f52012-08-29 09:29:33 -0400315 break;
Emeric Vigier78a53e92012-08-29 18:15:34 -0400316 case R.id.buttonHangUp:
Emeric Vigiere89b48b2012-08-30 14:16:46 -0400317 if (callOnGoing == true) {
318 Log.d(TAG, "ManagerImpl.hangUp(" + callID + ");");
319 ManagerImpl.hangUp(callID);
320 callOnGoing = false;
321 buttonCall.setEnabled(true);
322 buttonHangup.setEnabled(false);
323 }
Emeric Vigier78a53e92012-08-29 18:15:34 -0400324 break;
Emeric Vigier62ca14d2012-08-24 11:05:09 -0400325 case R.id.buttonInit:
326 ManagerImpl.initN("");
327 break;
Emeric Vigier62ca14d2012-08-24 11:05:09 -0400328 case R.id.buttonCallVoid:
329 ManagerImpl.callVoid();
330 break;
331 case R.id.buttonGetNewData:
332 Data d = ManagerImpl.getNewData(42, "foo");
333 if (d != null)
334 buttonFragment.getNewDataText().setText("getNewData(42, \"foo\") == Data(" + d.i + ", \"" + d.s + "\")");
335 break;
336 case R.id.buttonGetDataString:
Emeric Vigier383b2a22012-08-27 14:20:05 -0400337 Data daita = new Data(43, "bar");
338 String s = ManagerImpl.getDataString(daita);
Emeric Vigier62ca14d2012-08-24 11:05:09 -0400339 if (s != "") {
Emeric Vigier383b2a22012-08-27 14:20:05 -0400340 buttonFragment.getDataStringText().setText("getDataString(Data(43, \"bar\")) == \"" + s + "\"");
Emeric Vigier62ca14d2012-08-24 11:05:09 -0400341 }
342 break;
343 default:
344 Log.w(TAG, "unknown button " + view.getId());
345 break;
Emeric Vigier05e894e2012-08-20 13:53:02 -0400346 }
Emeric Vigier05e894e2012-08-20 13:53:02 -0400347 }
Adrien Béraudffd32412012-08-07 18:39:23 -0400348}