blob: c135b0d3cb151865b950556fb2ced1a831eee90d [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;
38import android.os.Bundle;
Emeric Vigier383b2a22012-08-27 14:20:05 -040039import android.os.Handler;
40import android.os.Message;
Adrien Béraudffd32412012-08-07 18:39:23 -040041import android.support.v13.app.FragmentStatePagerAdapter;
42import android.support.v4.view.ViewPager;
Emeric Vigier05e894e2012-08-20 13:53:02 -040043import android.util.Log;
Adrien Béraudffd32412012-08-07 18:39:23 -040044import android.view.Gravity;
45import android.view.LayoutInflater;
46import android.view.Menu;
47import android.view.View;
Emeric Vigier05e894e2012-08-20 13:53:02 -040048import android.view.View.OnClickListener;
Adrien Béraudffd32412012-08-07 18:39:23 -040049import android.view.ViewGroup;
50import android.widget.TextView;
Emeric Vigier62ca14d2012-08-24 11:05:09 -040051import com.savoirfairelinux.sflphone.client.Data;
52import com.savoirfairelinux.sflphone.client.ManagerImpl;
Adrien Béraudffd32412012-08-07 18:39:23 -040053
54import com.savoirfairelinux.sflphone.R;
55
Emeric Vigier62ca14d2012-08-24 11:05:09 -040056public class SFLPhoneHome extends Activity implements ActionBar.TabListener, OnClickListener
Adrien Béraudffd32412012-08-07 18:39:23 -040057{
58 SectionsPagerAdapter mSectionsPagerAdapter;
Emeric Vigier62ca14d2012-08-24 11:05:09 -040059 static final String TAG = "SFLPhoneHome";
60 ButtonSectionFragment buttonFragment;
Emeric Vigier383b2a22012-08-27 14:20:05 -040061 Handler callbackHandler;
62 static ManagerImpl managerImpl;
Adrien Béraudffd32412012-08-07 18:39:23 -040063
64 /**
65 * The {@link ViewPager} that will host the section contents.
66 */
67 ViewPager mViewPager;
68
Emeric Vigier05e894e2012-08-20 13:53:02 -040069 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 -040070
71 @Override
72 public void onCreate(Bundle savedInstanceState)
73 {
74 super.onCreate(savedInstanceState);
75 setContentView(R.layout.activity_sflphone_home);
76
77 mSectionsPagerAdapter = new SectionsPagerAdapter(getFragmentManager());
78
79 final ActionBar actionBar = getActionBar();
80 actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
81
82 // Set up the ViewPager with the sections adapter.
83 mViewPager = (ViewPager) findViewById(R.id.pager);
84 mViewPager.setAdapter(mSectionsPagerAdapter);
85
86 // When swiping between different sections, select the corresponding tab.
87 // We can also use ActionBar.Tab#select() to do this if we have a reference to the
88 // Tab.
89 mViewPager.setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener()
90 {
91 @Override
92 public void onPageSelected(int position)
93 {
94 actionBar.setSelectedNavigationItem(position);
95 }
96 });
97
98 // For each of the sections in the app, add a tab to the action bar.
99 for (int i = 0; i < mSectionsPagerAdapter.getCount(); i++) {
100 // Create a tab with text corresponding to the page title defined by the adapter.
101 // Also specify this Activity object, which implements the TabListener interface, as the
102 // listener for when this tab is selected.
103 actionBar.addTab(actionBar.newTab().setIcon(icon_res_id[i]).setText(mSectionsPagerAdapter.getPageTitle(i)).setTabListener(this));
104 }
Emeric Vigier383b2a22012-08-27 14:20:05 -0400105
106 // FIXME
107 callbackHandler = new Handler() {
108 public void handleMessage(Message msg) {
109 Bundle b = msg.getData();
110 TextView callVoidText;
111
112 Log.i(TAG, "handlerMessage");
113
114 callVoidText = buttonFragment.getcallVoidText();
115 if (callVoidText == null)
116 Log.e(TAG, "SFLPhoneHome: callVoidText is " + callVoidText);
117 callVoidText.setText(b.getString("callback_string"));
118
119 Log.i(TAG, "handlerMessage: " + b.getString("callback_string"));
120 }
121 };
122 managerImpl = new ManagerImpl(callbackHandler);
123 Log.i(TAG, "managerImpl created with callbackHandler " + callbackHandler);
Adrien Béraudffd32412012-08-07 18:39:23 -0400124 }
125
Emeric Vigier62ca14d2012-08-24 11:05:09 -0400126 // FIXME
127 static {
128 System.loadLibrary("gnustl_shared");
129 System.loadLibrary("expat");
130 System.loadLibrary("yaml");
131 System.loadLibrary("ccgnu2");
132 System.loadLibrary("crypto");
133 System.loadLibrary("ssl");
134 System.loadLibrary("ccrtp1");
135 System.loadLibrary("dbus");
136 System.loadLibrary("dbus-c++-1");
137 System.loadLibrary("samplerate");
138 System.loadLibrary("codec_ulaw");
139 System.loadLibrary("codec_alaw");
140 System.loadLibrary("speexresampler");
141 System.loadLibrary("sflphone");
142 }
143
Adrien Béraudffd32412012-08-07 18:39:23 -0400144 @Override
145 public boolean onCreateOptionsMenu(Menu menu)
146 {
147 getMenuInflater().inflate(R.menu.activity_sflphone_home, menu);
148 return true;
149 }
150
151 @Override
152 public void onTabUnselected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction)
153 {
154 }
155
156 @Override
157 public void onTabSelected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction)
158 {
159 // When the given tab is selected, switch to the corresponding page in the ViewPager.
160 mViewPager.setCurrentItem(tab.getPosition());
161 }
162
163 @Override
164 public void onTabReselected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction)
165 {
Emeric Vigier05e894e2012-08-20 13:53:02 -0400166// Log.d(TAG, "onTabReselected");
167// ManagerImpl.initN("");
Adrien Béraudffd32412012-08-07 18:39:23 -0400168 }
169
170 /**
171 * A {@link FragmentStatePagerAdapter} that returns a fragment corresponding to
172 * one of the primary sections of the app.
173 */
174 public class SectionsPagerAdapter extends FragmentStatePagerAdapter
175 {
176
177 public SectionsPagerAdapter(FragmentManager fm)
178 {
179 super(fm);
180 }
181
182 @Override
183 public Fragment getItem(int i)
184 {
185 Fragment fragment;
Emeric Vigier62ca14d2012-08-24 11:05:09 -0400186
Emeric Vigier05e894e2012-08-20 13:53:02 -0400187 switch (i) {
188 case 0:
Adrien Béraudffd32412012-08-07 18:39:23 -0400189 fragment = new CallElementList();
Emeric Vigier05e894e2012-08-20 13:53:02 -0400190 break;
191 case 1:
Adrien Béraudffd32412012-08-07 18:39:23 -0400192 fragment = new DummySectionFragment();
Emeric Vigier05e894e2012-08-20 13:53:02 -0400193 break;
194 case 2:
Emeric Vigier62ca14d2012-08-24 11:05:09 -0400195 buttonFragment = new ButtonSectionFragment();
196 Log.i(TAG, "getItem: fragment is " + buttonFragment);
197 fragment = buttonFragment;
Emeric Vigier05e894e2012-08-20 13:53:02 -0400198 break;
199 default:
200 Log.e(TAG, "getItem: unknown tab position " + i);
201 return null;
202 }
203
Adrien Béraudffd32412012-08-07 18:39:23 -0400204 Bundle args = new Bundle();
205 args.putInt(DummySectionFragment.ARG_SECTION_NUMBER, i + 1);
206 fragment.setArguments(args);
207 return fragment;
208 }
209
210 @Override
211 public int getCount()
212 {
Emeric Vigier05e894e2012-08-20 13:53:02 -0400213 return 3;
Adrien Béraudffd32412012-08-07 18:39:23 -0400214 }
215
216 @Override
217 public CharSequence getPageTitle(int position)
218 {
219 switch (position) {
220 case 0:
221 return getString(R.string.title_section1).toUpperCase();
222 case 1:
223 return getString(R.string.title_section2).toUpperCase();
Emeric Vigier05e894e2012-08-20 13:53:02 -0400224 case 2:
225 return getString(R.string.title_section3).toUpperCase();
226 default:
227 Log.e(TAG, "getPageTitle: unknown tab position " + position);
228 break;
Adrien Béraudffd32412012-08-07 18:39:23 -0400229 }
230 return null;
231 }
232 }
233
234 /**
235 * A dummy fragment representing a section of the app, but that simply
236 * displays dummy text.
237 */
238 public static class DummySectionFragment extends Fragment
239 {
240 public DummySectionFragment()
241 {
242 setRetainInstance(true);
243 }
244
245 public static final String ARG_SECTION_NUMBER = "section_number";
246
247 @Override
Emeric Vigier05e894e2012-08-20 13:53:02 -0400248 public View onCreateView(LayoutInflater inflater, ViewGroup parent, Bundle savedInstanceState)
Adrien Béraudffd32412012-08-07 18:39:23 -0400249 {
250 TextView textView = new TextView(getActivity());
251 textView.setGravity(Gravity.CENTER);
252 Bundle args = getArguments();
253 textView.setText(Integer.toString(args.getInt(ARG_SECTION_NUMBER)));
Emeric Vigier05e894e2012-08-20 13:53:02 -0400254 textView.setText("java sucks");
Adrien Béraudffd32412012-08-07 18:39:23 -0400255 return textView;
256 }
Adrien Béraudffd32412012-08-07 18:39:23 -0400257 }
Emeric Vigier05e894e2012-08-20 13:53:02 -0400258
Emeric Vigier62ca14d2012-08-24 11:05:09 -0400259 public static String getAppPath() {
260 return "/data/data/com.savoirfairelinux.sflphone";
261// PackageManager m = getPackageManager();
262// String s = getPackageName();
263// Log.d(TAG, "Application path: " + s);
264// try {
265// PackageInfo p = m.getPackageInfo(s, 0);
266// s = p.applicationInfo.dataDir;
267// } catch (NameNotFoundException e) {
268// Log.w(TAG, "Error Package name not found ", e);
269// }
270// return s;
271 }
Emeric Vigier05e894e2012-08-20 13:53:02 -0400272
Emeric Vigier62ca14d2012-08-24 11:05:09 -0400273 @Override
274 public void onClick(View view)
275 {
276 switch (view.getId()) {
Emeric Vigier62ca14d2012-08-24 11:05:09 -0400277 case R.id.buttonInit:
278 ManagerImpl.initN("");
279 break;
Emeric Vigier62ca14d2012-08-24 11:05:09 -0400280 case R.id.buttonCallVoid:
281 ManagerImpl.callVoid();
282 break;
283 case R.id.buttonGetNewData:
284 Data d = ManagerImpl.getNewData(42, "foo");
285 if (d != null)
286 buttonFragment.getNewDataText().setText("getNewData(42, \"foo\") == Data(" + d.i + ", \"" + d.s + "\")");
287 break;
288 case R.id.buttonGetDataString:
Emeric Vigier383b2a22012-08-27 14:20:05 -0400289 Data daita = new Data(43, "bar");
290 String s = ManagerImpl.getDataString(daita);
Emeric Vigier62ca14d2012-08-24 11:05:09 -0400291 if (s != "") {
Emeric Vigier383b2a22012-08-27 14:20:05 -0400292 buttonFragment.getDataStringText().setText("getDataString(Data(43, \"bar\")) == \"" + s + "\"");
Emeric Vigier62ca14d2012-08-24 11:05:09 -0400293 }
294 break;
295 default:
296 Log.w(TAG, "unknown button " + view.getId());
297 break;
Emeric Vigier05e894e2012-08-20 13:53:02 -0400298 }
Emeric Vigier05e894e2012-08-20 13:53:02 -0400299 }
Adrien Béraudffd32412012-08-07 18:39:23 -0400300}