blob: 6abb5e84fae192088a711c95af9e2206d2c37b2b [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;
Emeric Vigier78a53e92012-08-29 18:15:34 -040055import java.util.Random;
Adrien Béraudffd32412012-08-07 18:39:23 -040056
57import com.savoirfairelinux.sflphone.R;
58
Emeric Vigier62ca14d2012-08-24 11:05:09 -040059public class SFLPhoneHome extends Activity implements ActionBar.TabListener, OnClickListener
Adrien Béraudffd32412012-08-07 18:39:23 -040060{
61 SectionsPagerAdapter mSectionsPagerAdapter;
Emeric Vigier62ca14d2012-08-24 11:05:09 -040062 static final String TAG = "SFLPhoneHome";
63 ButtonSectionFragment buttonFragment;
Emeric Vigier383b2a22012-08-27 14:20:05 -040064 Handler callbackHandler;
65 static ManagerImpl managerImpl;
Emeric Vigier78a53e92012-08-29 18:15:34 -040066 /* default callID */
67 static String callID = "007";
Adrien Béraudffd32412012-08-07 18:39:23 -040068
69 /**
70 * The {@link ViewPager} that will host the section contents.
71 */
72 ViewPager mViewPager;
73
Emeric Vigier05e894e2012-08-20 13:53:02 -040074 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 -040075
76 @Override
77 public void onCreate(Bundle savedInstanceState)
78 {
79 super.onCreate(savedInstanceState);
80 setContentView(R.layout.activity_sflphone_home);
81
82 mSectionsPagerAdapter = new SectionsPagerAdapter(getFragmentManager());
83
84 final ActionBar actionBar = getActionBar();
85 actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
86
87 // Set up the ViewPager with the sections adapter.
88 mViewPager = (ViewPager) findViewById(R.id.pager);
89 mViewPager.setAdapter(mSectionsPagerAdapter);
90
91 // When swiping between different sections, select the corresponding tab.
92 // We can also use ActionBar.Tab#select() to do this if we have a reference to the
93 // Tab.
94 mViewPager.setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener()
95 {
96 @Override
97 public void onPageSelected(int position)
98 {
99 actionBar.setSelectedNavigationItem(position);
100 }
101 });
102
103 // For each of the sections in the app, add a tab to the action bar.
104 for (int i = 0; i < mSectionsPagerAdapter.getCount(); i++) {
105 // Create a tab with text corresponding to the page title defined by the adapter.
106 // Also specify this Activity object, which implements the TabListener interface, as the
107 // listener for when this tab is selected.
108 actionBar.addTab(actionBar.newTab().setIcon(icon_res_id[i]).setText(mSectionsPagerAdapter.getPageTitle(i)).setTabListener(this));
109 }
Emeric Vigier383b2a22012-08-27 14:20:05 -0400110
111 // FIXME
112 callbackHandler = new Handler() {
113 public void handleMessage(Message msg) {
114 Bundle b = msg.getData();
115 TextView callVoidText;
116
Emeric Vigierc411ce32012-08-29 09:28:28 -0400117 Log.i(TAG, "handleMessage");
Emeric Vigier383b2a22012-08-27 14:20:05 -0400118
119 callVoidText = buttonFragment.getcallVoidText();
120 if (callVoidText == null)
121 Log.e(TAG, "SFLPhoneHome: callVoidText is " + callVoidText);
122 callVoidText.setText(b.getString("callback_string"));
123
Emeric Vigierc411ce32012-08-29 09:28:28 -0400124 Log.i(TAG, "handleMessage: " + b.getString("callback_string"));
Emeric Vigier383b2a22012-08-27 14:20:05 -0400125 }
126 };
Emeric Vigier7a255aa2012-08-29 09:49:18 -0400127 ManagerImpl.setAppPath(getAppPath());
Emeric Vigier383b2a22012-08-27 14:20:05 -0400128 managerImpl = new ManagerImpl(callbackHandler);
129 Log.i(TAG, "managerImpl created with callbackHandler " + callbackHandler);
Adrien Béraudffd32412012-08-07 18:39:23 -0400130 }
131
Emeric Vigier62ca14d2012-08-24 11:05:09 -0400132 // FIXME
133 static {
134 System.loadLibrary("gnustl_shared");
135 System.loadLibrary("expat");
136 System.loadLibrary("yaml");
137 System.loadLibrary("ccgnu2");
138 System.loadLibrary("crypto");
139 System.loadLibrary("ssl");
140 System.loadLibrary("ccrtp1");
141 System.loadLibrary("dbus");
142 System.loadLibrary("dbus-c++-1");
143 System.loadLibrary("samplerate");
144 System.loadLibrary("codec_ulaw");
145 System.loadLibrary("codec_alaw");
146 System.loadLibrary("speexresampler");
147 System.loadLibrary("sflphone");
148 }
149
Adrien Béraudffd32412012-08-07 18:39:23 -0400150 @Override
151 public boolean onCreateOptionsMenu(Menu menu)
152 {
153 getMenuInflater().inflate(R.menu.activity_sflphone_home, menu);
154 return true;
155 }
156
157 @Override
158 public void onTabUnselected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction)
159 {
160 }
161
162 @Override
163 public void onTabSelected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction)
164 {
165 // When the given tab is selected, switch to the corresponding page in the ViewPager.
166 mViewPager.setCurrentItem(tab.getPosition());
167 }
168
169 @Override
170 public void onTabReselected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction)
171 {
Emeric Vigier05e894e2012-08-20 13:53:02 -0400172// Log.d(TAG, "onTabReselected");
173// ManagerImpl.initN("");
Adrien Béraudffd32412012-08-07 18:39:23 -0400174 }
175
176 /**
177 * A {@link FragmentStatePagerAdapter} that returns a fragment corresponding to
178 * one of the primary sections of the app.
179 */
180 public class SectionsPagerAdapter extends FragmentStatePagerAdapter
181 {
182
183 public SectionsPagerAdapter(FragmentManager fm)
184 {
185 super(fm);
186 }
187
188 @Override
189 public Fragment getItem(int i)
190 {
191 Fragment fragment;
Emeric Vigier62ca14d2012-08-24 11:05:09 -0400192
Emeric Vigier05e894e2012-08-20 13:53:02 -0400193 switch (i) {
194 case 0:
Adrien Béraudffd32412012-08-07 18:39:23 -0400195 fragment = new CallElementList();
Emeric Vigier05e894e2012-08-20 13:53:02 -0400196 break;
197 case 1:
Adrien Béraudffd32412012-08-07 18:39:23 -0400198 fragment = new DummySectionFragment();
Emeric Vigier05e894e2012-08-20 13:53:02 -0400199 break;
200 case 2:
Emeric Vigier62ca14d2012-08-24 11:05:09 -0400201 buttonFragment = new ButtonSectionFragment();
202 Log.i(TAG, "getItem: fragment is " + buttonFragment);
203 fragment = buttonFragment;
Emeric Vigier05e894e2012-08-20 13:53:02 -0400204 break;
205 default:
206 Log.e(TAG, "getItem: unknown tab position " + i);
207 return null;
208 }
209
Adrien Béraudffd32412012-08-07 18:39:23 -0400210 Bundle args = new Bundle();
211 args.putInt(DummySectionFragment.ARG_SECTION_NUMBER, i + 1);
212 fragment.setArguments(args);
213 return fragment;
214 }
215
216 @Override
217 public int getCount()
218 {
Emeric Vigier05e894e2012-08-20 13:53:02 -0400219 return 3;
Adrien Béraudffd32412012-08-07 18:39:23 -0400220 }
221
222 @Override
223 public CharSequence getPageTitle(int position)
224 {
225 switch (position) {
226 case 0:
227 return getString(R.string.title_section1).toUpperCase();
228 case 1:
229 return getString(R.string.title_section2).toUpperCase();
Emeric Vigier05e894e2012-08-20 13:53:02 -0400230 case 2:
231 return getString(R.string.title_section3).toUpperCase();
232 default:
233 Log.e(TAG, "getPageTitle: unknown tab position " + position);
234 break;
Adrien Béraudffd32412012-08-07 18:39:23 -0400235 }
236 return null;
237 }
238 }
239
240 /**
241 * A dummy fragment representing a section of the app, but that simply
242 * displays dummy text.
243 */
244 public static class DummySectionFragment extends Fragment
245 {
246 public DummySectionFragment()
247 {
248 setRetainInstance(true);
249 }
250
251 public static final String ARG_SECTION_NUMBER = "section_number";
252
253 @Override
Emeric Vigier05e894e2012-08-20 13:53:02 -0400254 public View onCreateView(LayoutInflater inflater, ViewGroup parent, Bundle savedInstanceState)
Adrien Béraudffd32412012-08-07 18:39:23 -0400255 {
256 TextView textView = new TextView(getActivity());
257 textView.setGravity(Gravity.CENTER);
258 Bundle args = getArguments();
259 textView.setText(Integer.toString(args.getInt(ARG_SECTION_NUMBER)));
Emeric Vigier05e894e2012-08-20 13:53:02 -0400260 textView.setText("java sucks");
Adrien Béraudffd32412012-08-07 18:39:23 -0400261 return textView;
262 }
Adrien Béraudffd32412012-08-07 18:39:23 -0400263 }
Emeric Vigier05e894e2012-08-20 13:53:02 -0400264
Emeric Vigier7a255aa2012-08-29 09:49:18 -0400265 public String getAppPath() {
266 PackageManager pkgMng = getPackageManager();
267 String pkgName = getPackageName();
268
269 try {
270 PackageInfo pkgInfo = pkgMng.getPackageInfo(pkgName, 0);
271 pkgName = pkgInfo.applicationInfo.dataDir;
272 } catch (NameNotFoundException e) {
273 Log.w(TAG, "Error Package name not found ", e);
274 }
275
276 Log.d(TAG, "Application path: " + pkgName);
277 return pkgName;
Emeric Vigier62ca14d2012-08-24 11:05:09 -0400278 }
Emeric Vigier05e894e2012-08-20 13:53:02 -0400279
Emeric Vigier62ca14d2012-08-24 11:05:09 -0400280 @Override
281 public void onClick(View view)
Emeric Vigier78a53e92012-08-29 18:15:34 -0400282 {
Emeric Vigier62ca14d2012-08-24 11:05:09 -0400283 switch (view.getId()) {
Emeric Vigierd5c17f52012-08-29 09:29:33 -0400284 case R.id.buttonCall:
Emeric Vigier39a11fa2012-08-29 15:25:09 -0400285 TextView textView = (TextView) findViewById(R.id.editAccountID);
286 String accountID = textView.getText().toString();
287 EditText editText;
Emeric Vigier78a53e92012-08-29 18:15:34 -0400288 Random random = new Random();
Emeric Vigierd5c17f52012-08-29 09:29:33 -0400289
290 editText = (EditText) findViewById(R.id.editTo);
291 String to = editText.getText().toString();
292 if (to == null) {
293 Log.e(TAG, "to string is " + to);
294 break;
295 }
296
Emeric Vigier78a53e92012-08-29 18:15:34 -0400297 /* new random callID */
298 callID = Integer.toString(random.nextInt());
299
Emeric Vigierd5c17f52012-08-29 09:29:33 -0400300 Log.d(TAG, "ManagerImpl.placeCall(" + accountID + ", " + callID + ", " + to + ");");
301 ManagerImpl.placeCall(accountID, callID, to);
302 break;
Emeric Vigier78a53e92012-08-29 18:15:34 -0400303 case R.id.buttonHangUp:
304 Log.d(TAG, "ManagerImpl.hangUp(" + callID + ");");
305 ManagerImpl.hangUp(callID);
306 break;
Emeric Vigier62ca14d2012-08-24 11:05:09 -0400307 case R.id.buttonInit:
308 ManagerImpl.initN("");
309 break;
Emeric Vigier62ca14d2012-08-24 11:05:09 -0400310 case R.id.buttonCallVoid:
311 ManagerImpl.callVoid();
312 break;
313 case R.id.buttonGetNewData:
314 Data d = ManagerImpl.getNewData(42, "foo");
315 if (d != null)
316 buttonFragment.getNewDataText().setText("getNewData(42, \"foo\") == Data(" + d.i + ", \"" + d.s + "\")");
317 break;
318 case R.id.buttonGetDataString:
Emeric Vigier383b2a22012-08-27 14:20:05 -0400319 Data daita = new Data(43, "bar");
320 String s = ManagerImpl.getDataString(daita);
Emeric Vigier62ca14d2012-08-24 11:05:09 -0400321 if (s != "") {
Emeric Vigier383b2a22012-08-27 14:20:05 -0400322 buttonFragment.getDataStringText().setText("getDataString(Data(43, \"bar\")) == \"" + s + "\"");
Emeric Vigier62ca14d2012-08-24 11:05:09 -0400323 }
324 break;
325 default:
326 Log.w(TAG, "unknown button " + view.getId());
327 break;
Emeric Vigier05e894e2012-08-20 13:53:02 -0400328 }
Emeric Vigier05e894e2012-08-20 13:53:02 -0400329 }
Adrien Béraudffd32412012-08-07 18:39:23 -0400330}