blob: 27172fafa36931c09814d54da329bdd3352ac236 [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 Vigiera6bdb702012-08-31 15:34:53 -040055import android.view.animation.AlphaAnimation;
56import android.view.animation.Animation;
57import android.view.animation.LinearInterpolator;
Emeric Vigierd5c17f52012-08-29 09:29:33 -040058import android.widget.EditText;
Emeric Vigiere89b48b2012-08-30 14:16:46 -040059import android.widget.ImageButton;
Adrien Béraudffd32412012-08-07 18:39:23 -040060import android.widget.TextView;
61
62import com.savoirfairelinux.sflphone.R;
63
Emeric Vigier62ca14d2012-08-24 11:05:09 -040064public class SFLPhoneHome extends Activity implements ActionBar.TabListener, OnClickListener
Adrien Béraudffd32412012-08-07 18:39:23 -040065{
66 SectionsPagerAdapter mSectionsPagerAdapter;
Emeric Vigier62ca14d2012-08-24 11:05:09 -040067 static final String TAG = "SFLPhoneHome";
Emeric Vigiera6bdb702012-08-31 15:34:53 -040068 private ButtonSectionFragment buttonFragment;
Emeric Vigiere89b48b2012-08-30 14:16:46 -040069 ImageButton buttonCall, buttonHangup;
Emeric Vigier383b2a22012-08-27 14:20:05 -040070 Handler callbackHandler;
71 static ManagerImpl managerImpl;
Emeric Vigier78a53e92012-08-29 18:15:34 -040072 /* default callID */
73 static String callID = "007";
Emeric Vigiere89b48b2012-08-30 14:16:46 -040074 static boolean callOnGoing = false;
Emeric Vigiera6bdb702012-08-31 15:34:53 -040075 private String incomingCallID = "";
Adrien Béraudffd32412012-08-07 18:39:23 -040076
77 /**
78 * The {@link ViewPager} that will host the section contents.
79 */
80 ViewPager mViewPager;
81
Emeric Vigier05e894e2012-08-20 13:53:02 -040082 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 -040083
84 @Override
85 public void onCreate(Bundle savedInstanceState)
86 {
87 super.onCreate(savedInstanceState);
88 setContentView(R.layout.activity_sflphone_home);
89
90 mSectionsPagerAdapter = new SectionsPagerAdapter(getFragmentManager());
91
92 final ActionBar actionBar = getActionBar();
93 actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
94
95 // Set up the ViewPager with the sections adapter.
96 mViewPager = (ViewPager) findViewById(R.id.pager);
97 mViewPager.setAdapter(mSectionsPagerAdapter);
98
99 // When swiping between different sections, select the corresponding tab.
100 // We can also use ActionBar.Tab#select() to do this if we have a reference to the
101 // Tab.
102 mViewPager.setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener()
103 {
104 @Override
105 public void onPageSelected(int position)
106 {
107 actionBar.setSelectedNavigationItem(position);
108 }
109 });
110
111 // For each of the sections in the app, add a tab to the action bar.
112 for (int i = 0; i < mSectionsPagerAdapter.getCount(); i++) {
113 // Create a tab with text corresponding to the page title defined by the adapter.
114 // Also specify this Activity object, which implements the TabListener interface, as the
115 // listener for when this tab is selected.
116 actionBar.addTab(actionBar.newTab().setIcon(icon_res_id[i]).setText(mSectionsPagerAdapter.getPageTitle(i)).setTabListener(this));
117 }
Emeric Vigier383b2a22012-08-27 14:20:05 -0400118
119 // FIXME
120 callbackHandler = new Handler() {
121 public void handleMessage(Message msg) {
122 Bundle b = msg.getData();
123 TextView callVoidText;
124
Emeric Vigierc411ce32012-08-29 09:28:28 -0400125 Log.i(TAG, "handleMessage");
Emeric Vigier383b2a22012-08-27 14:20:05 -0400126
127 callVoidText = buttonFragment.getcallVoidText();
128 if (callVoidText == null)
129 Log.e(TAG, "SFLPhoneHome: callVoidText is " + callVoidText);
130 callVoidText.setText(b.getString("callback_string"));
131
Emeric Vigierc411ce32012-08-29 09:28:28 -0400132 Log.i(TAG, "handleMessage: " + b.getString("callback_string"));
Emeric Vigier383b2a22012-08-27 14:20:05 -0400133 }
134 };
Emeric Vigier7a255aa2012-08-29 09:49:18 -0400135 ManagerImpl.setAppPath(getAppPath());
Emeric Vigier383b2a22012-08-27 14:20:05 -0400136 managerImpl = new ManagerImpl(callbackHandler);
Emeric Vigiera6bdb702012-08-31 15:34:53 -0400137 managerImpl.setActivity(this);
Emeric Vigier383b2a22012-08-27 14:20:05 -0400138 Log.i(TAG, "managerImpl created with callbackHandler " + callbackHandler);
Emeric Vigiera6bdb702012-08-31 15:34:53 -0400139
140 buttonCall = (ImageButton) findViewById(R.id.buttonCall);
141 buttonHangup = (ImageButton) findViewById(R.id.buttonHangUp);
142// buttonIncomingCall = (ImageButton) findViewById(R.id.buttonIncomingCall);
Adrien Béraudffd32412012-08-07 18:39:23 -0400143 }
144
Emeric Vigier62ca14d2012-08-24 11:05:09 -0400145 // FIXME
146 static {
147 System.loadLibrary("gnustl_shared");
148 System.loadLibrary("expat");
149 System.loadLibrary("yaml");
150 System.loadLibrary("ccgnu2");
151 System.loadLibrary("crypto");
152 System.loadLibrary("ssl");
153 System.loadLibrary("ccrtp1");
154 System.loadLibrary("dbus");
155 System.loadLibrary("dbus-c++-1");
156 System.loadLibrary("samplerate");
157 System.loadLibrary("codec_ulaw");
158 System.loadLibrary("codec_alaw");
159 System.loadLibrary("speexresampler");
160 System.loadLibrary("sflphone");
161 }
162
Adrien Béraudffd32412012-08-07 18:39:23 -0400163 @Override
164 public boolean onCreateOptionsMenu(Menu menu)
165 {
166 getMenuInflater().inflate(R.menu.activity_sflphone_home, menu);
167 return true;
168 }
169
170 @Override
171 public void onTabUnselected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction)
172 {
173 }
174
175 @Override
176 public void onTabSelected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction)
177 {
178 // When the given tab is selected, switch to the corresponding page in the ViewPager.
179 mViewPager.setCurrentItem(tab.getPosition());
180 }
181
182 @Override
183 public void onTabReselected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction)
184 {
Emeric Vigier05e894e2012-08-20 13:53:02 -0400185// Log.d(TAG, "onTabReselected");
186// ManagerImpl.initN("");
Adrien Béraudffd32412012-08-07 18:39:23 -0400187 }
188
Emeric Vigiera6bdb702012-08-31 15:34:53 -0400189 public void setIncomingCallID(String id) {
190 incomingCallID = id;
191 }
192
Adrien Béraudffd32412012-08-07 18:39:23 -0400193 /**
194 * A {@link FragmentStatePagerAdapter} that returns a fragment corresponding to
195 * one of the primary sections of the app.
196 */
197 public class SectionsPagerAdapter extends FragmentStatePagerAdapter
198 {
199
200 public SectionsPagerAdapter(FragmentManager fm)
201 {
202 super(fm);
203 }
204
205 @Override
206 public Fragment getItem(int i)
207 {
208 Fragment fragment;
Emeric Vigier62ca14d2012-08-24 11:05:09 -0400209
Emeric Vigier05e894e2012-08-20 13:53:02 -0400210 switch (i) {
211 case 0:
Adrien Béraudffd32412012-08-07 18:39:23 -0400212 fragment = new CallElementList();
Emeric Vigier05e894e2012-08-20 13:53:02 -0400213 break;
214 case 1:
Adrien Béraudffd32412012-08-07 18:39:23 -0400215 fragment = new DummySectionFragment();
Emeric Vigier05e894e2012-08-20 13:53:02 -0400216 break;
217 case 2:
Emeric Vigier62ca14d2012-08-24 11:05:09 -0400218 buttonFragment = new ButtonSectionFragment();
219 Log.i(TAG, "getItem: fragment is " + buttonFragment);
220 fragment = buttonFragment;
Emeric Vigiera6bdb702012-08-31 15:34:53 -0400221 managerImpl.setButtonFragment(buttonFragment);
Emeric Vigier05e894e2012-08-20 13:53:02 -0400222 break;
223 default:
224 Log.e(TAG, "getItem: unknown tab position " + i);
225 return null;
226 }
227
Adrien Béraudffd32412012-08-07 18:39:23 -0400228 Bundle args = new Bundle();
229 args.putInt(DummySectionFragment.ARG_SECTION_NUMBER, i + 1);
230 fragment.setArguments(args);
231 return fragment;
232 }
233
234 @Override
235 public int getCount()
236 {
Emeric Vigier05e894e2012-08-20 13:53:02 -0400237 return 3;
Adrien Béraudffd32412012-08-07 18:39:23 -0400238 }
239
240 @Override
241 public CharSequence getPageTitle(int position)
242 {
243 switch (position) {
244 case 0:
245 return getString(R.string.title_section1).toUpperCase();
246 case 1:
247 return getString(R.string.title_section2).toUpperCase();
Emeric Vigier05e894e2012-08-20 13:53:02 -0400248 case 2:
249 return getString(R.string.title_section3).toUpperCase();
250 default:
251 Log.e(TAG, "getPageTitle: unknown tab position " + position);
252 break;
Adrien Béraudffd32412012-08-07 18:39:23 -0400253 }
254 return null;
255 }
256 }
257
258 /**
259 * A dummy fragment representing a section of the app, but that simply
260 * displays dummy text.
261 */
262 public static class DummySectionFragment extends Fragment
263 {
264 public DummySectionFragment()
265 {
266 setRetainInstance(true);
267 }
268
269 public static final String ARG_SECTION_NUMBER = "section_number";
270
271 @Override
Emeric Vigier05e894e2012-08-20 13:53:02 -0400272 public View onCreateView(LayoutInflater inflater, ViewGroup parent, Bundle savedInstanceState)
Adrien Béraudffd32412012-08-07 18:39:23 -0400273 {
274 TextView textView = new TextView(getActivity());
275 textView.setGravity(Gravity.CENTER);
276 Bundle args = getArguments();
277 textView.setText(Integer.toString(args.getInt(ARG_SECTION_NUMBER)));
Emeric Vigier05e894e2012-08-20 13:53:02 -0400278 textView.setText("java sucks");
Adrien Béraudffd32412012-08-07 18:39:23 -0400279 return textView;
280 }
Adrien Béraudffd32412012-08-07 18:39:23 -0400281 }
Emeric Vigier05e894e2012-08-20 13:53:02 -0400282
Emeric Vigier7a255aa2012-08-29 09:49:18 -0400283 public String getAppPath() {
284 PackageManager pkgMng = getPackageManager();
285 String pkgName = getPackageName();
286
287 try {
288 PackageInfo pkgInfo = pkgMng.getPackageInfo(pkgName, 0);
289 pkgName = pkgInfo.applicationInfo.dataDir;
290 } catch (NameNotFoundException e) {
291 Log.w(TAG, "Error Package name not found ", e);
292 }
293
294 Log.d(TAG, "Application path: " + pkgName);
295 return pkgName;
Emeric Vigier62ca14d2012-08-24 11:05:09 -0400296 }
Emeric Vigier05e894e2012-08-20 13:53:02 -0400297
Emeric Vigier62ca14d2012-08-24 11:05:09 -0400298 @Override
299 public void onClick(View view)
Emeric Vigiere89b48b2012-08-30 14:16:46 -0400300 {
Emeric Vigiera6bdb702012-08-31 15:34:53 -0400301 buttonCall = buttonFragment.getCallButton();
302 buttonHangup = buttonFragment.getHangUpButton();
303
Emeric Vigier62ca14d2012-08-24 11:05:09 -0400304 switch (view.getId()) {
Emeric Vigierd5c17f52012-08-29 09:29:33 -0400305 case R.id.buttonCall:
Emeric Vigier39a11fa2012-08-29 15:25:09 -0400306 TextView textView = (TextView) findViewById(R.id.editAccountID);
307 String accountID = textView.getText().toString();
308 EditText editText;
Emeric Vigier78a53e92012-08-29 18:15:34 -0400309 Random random = new Random();
Emeric Vigiere89b48b2012-08-30 14:16:46 -0400310
Emeric Vigiera6bdb702012-08-31 15:34:53 -0400311 if (incomingCallID != "") {
312 buttonCall.clearAnimation();
313 ManagerImpl.answerCall(incomingCallID);
314 callID = incomingCallID;
315 incomingCallID="";
Emeric Vigiere89b48b2012-08-30 14:16:46 -0400316 callOnGoing = true;
Emeric Vigiera6bdb702012-08-31 15:34:53 -0400317 buttonCall.setEnabled(false);
318 buttonHangup.setEnabled(true);
319 } else {
320 if (callOnGoing == false) {
321 editText = (EditText) findViewById(R.id.editTo);
322 String to = editText.getText().toString();
323 if (to == null) {
324 Log.e(TAG, "to string is " + to);
325 break;
326 }
327
328 /* new random callID */
329 callID = Integer.toString(random.nextInt());
330
331 Log.d(TAG, "ManagerImpl.placeCall(" + accountID + ", " + callID + ", " + to + ");");
332 ManagerImpl.placeCall(accountID, callID, to);
333 callOnGoing = true;
334 buttonCall.setEnabled(false);
335 buttonHangup.setEnabled(true);
336 }
Emeric Vigierd5c17f52012-08-29 09:29:33 -0400337 }
Emeric Vigierd5c17f52012-08-29 09:29:33 -0400338 break;
Emeric Vigier78a53e92012-08-29 18:15:34 -0400339 case R.id.buttonHangUp:
Emeric Vigiera6bdb702012-08-31 15:34:53 -0400340 if (incomingCallID != "") {
341 buttonCall.clearAnimation();
342 ManagerImpl.refuseCall(incomingCallID);
343 incomingCallID="";
344 buttonCall.setEnabled(true);
345 buttonHangup.setEnabled(true);
346 } else {
347 if (callOnGoing == true) {
348 Log.d(TAG, "ManagerImpl.hangUp(" + callID + ");");
349 ManagerImpl.hangUp(callID);
350 callOnGoing = false;
351 buttonCall.setEnabled(true);
352 buttonHangup.setEnabled(false);
353 }
Emeric Vigiere89b48b2012-08-30 14:16:46 -0400354 }
Emeric Vigiera6bdb702012-08-31 15:34:53 -0400355
356 buttonCall.setImageResource(R.drawable.ic_call);
Emeric Vigier78a53e92012-08-29 18:15:34 -0400357 break;
Emeric Vigier62ca14d2012-08-24 11:05:09 -0400358 case R.id.buttonInit:
359 ManagerImpl.initN("");
360 break;
Emeric Vigier62ca14d2012-08-24 11:05:09 -0400361 case R.id.buttonCallVoid:
362 ManagerImpl.callVoid();
363 break;
364 case R.id.buttonGetNewData:
365 Data d = ManagerImpl.getNewData(42, "foo");
366 if (d != null)
367 buttonFragment.getNewDataText().setText("getNewData(42, \"foo\") == Data(" + d.i + ", \"" + d.s + "\")");
368 break;
369 case R.id.buttonGetDataString:
Emeric Vigier383b2a22012-08-27 14:20:05 -0400370 Data daita = new Data(43, "bar");
371 String s = ManagerImpl.getDataString(daita);
Emeric Vigier62ca14d2012-08-24 11:05:09 -0400372 if (s != "") {
Emeric Vigier383b2a22012-08-27 14:20:05 -0400373 buttonFragment.getDataStringText().setText("getDataString(Data(43, \"bar\")) == \"" + s + "\"");
Emeric Vigier62ca14d2012-08-24 11:05:09 -0400374 }
375 break;
376 default:
377 Log.w(TAG, "unknown button " + view.getId());
378 break;
Emeric Vigier05e894e2012-08-20 13:53:02 -0400379 }
Emeric Vigier05e894e2012-08-20 13:53:02 -0400380 }
Adrien Béraudffd32412012-08-07 18:39:23 -0400381}