Adrien Béraud | ffd3241 | 2012-08-07 18:39:23 -0400 | [diff] [blame] | 1 | /* |
| 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 | */ |
| 31 | package com.savoirfairelinux.sflphone.client; |
| 32 | |
| 33 | import android.app.ActionBar; |
| 34 | import android.app.Activity; |
| 35 | import android.app.Fragment; |
| 36 | import android.app.FragmentManager; |
| 37 | import android.app.FragmentTransaction; |
Emeric Vigier | 7a255aa | 2012-08-29 09:49:18 -0400 | [diff] [blame] | 38 | import android.content.pm.PackageInfo; |
| 39 | import android.content.pm.PackageManager; |
| 40 | import android.content.pm.PackageManager.NameNotFoundException; |
Adrien Béraud | ffd3241 | 2012-08-07 18:39:23 -0400 | [diff] [blame] | 41 | import android.os.Bundle; |
Emeric Vigier | 383b2a2 | 2012-08-27 14:20:05 -0400 | [diff] [blame] | 42 | import android.os.Handler; |
| 43 | import android.os.Message; |
Adrien Béraud | ffd3241 | 2012-08-07 18:39:23 -0400 | [diff] [blame] | 44 | import android.support.v13.app.FragmentStatePagerAdapter; |
| 45 | import android.support.v4.view.ViewPager; |
Emeric Vigier | 05e894e | 2012-08-20 13:53:02 -0400 | [diff] [blame] | 46 | import android.util.Log; |
Adrien Béraud | ffd3241 | 2012-08-07 18:39:23 -0400 | [diff] [blame] | 47 | import android.view.Gravity; |
| 48 | import android.view.LayoutInflater; |
| 49 | import android.view.Menu; |
| 50 | import android.view.View; |
Emeric Vigier | 05e894e | 2012-08-20 13:53:02 -0400 | [diff] [blame] | 51 | import android.view.View.OnClickListener; |
Adrien Béraud | ffd3241 | 2012-08-07 18:39:23 -0400 | [diff] [blame] | 52 | import android.view.ViewGroup; |
Emeric Vigier | d5c17f5 | 2012-08-29 09:29:33 -0400 | [diff] [blame] | 53 | import android.widget.EditText; |
Adrien Béraud | ffd3241 | 2012-08-07 18:39:23 -0400 | [diff] [blame] | 54 | import android.widget.TextView; |
Emeric Vigier | 78a53e9 | 2012-08-29 18:15:34 -0400 | [diff] [blame^] | 55 | import java.util.Random; |
Adrien Béraud | ffd3241 | 2012-08-07 18:39:23 -0400 | [diff] [blame] | 56 | |
| 57 | import com.savoirfairelinux.sflphone.R; |
| 58 | |
Emeric Vigier | 62ca14d | 2012-08-24 11:05:09 -0400 | [diff] [blame] | 59 | public class SFLPhoneHome extends Activity implements ActionBar.TabListener, OnClickListener |
Adrien Béraud | ffd3241 | 2012-08-07 18:39:23 -0400 | [diff] [blame] | 60 | { |
| 61 | SectionsPagerAdapter mSectionsPagerAdapter; |
Emeric Vigier | 62ca14d | 2012-08-24 11:05:09 -0400 | [diff] [blame] | 62 | static final String TAG = "SFLPhoneHome"; |
| 63 | ButtonSectionFragment buttonFragment; |
Emeric Vigier | 383b2a2 | 2012-08-27 14:20:05 -0400 | [diff] [blame] | 64 | Handler callbackHandler; |
| 65 | static ManagerImpl managerImpl; |
Emeric Vigier | 78a53e9 | 2012-08-29 18:15:34 -0400 | [diff] [blame^] | 66 | /* default callID */ |
| 67 | static String callID = "007"; |
Adrien Béraud | ffd3241 | 2012-08-07 18:39:23 -0400 | [diff] [blame] | 68 | |
| 69 | /** |
| 70 | * The {@link ViewPager} that will host the section contents. |
| 71 | */ |
| 72 | ViewPager mViewPager; |
| 73 | |
Emeric Vigier | 05e894e | 2012-08-20 13:53:02 -0400 | [diff] [blame] | 74 | final private int[] icon_res_id = {R.drawable.ic_tab_call, R.drawable.ic_tab_history, R.drawable.ic_tab_play_selected}; |
Adrien Béraud | ffd3241 | 2012-08-07 18:39:23 -0400 | [diff] [blame] | 75 | |
| 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 Vigier | 383b2a2 | 2012-08-27 14:20:05 -0400 | [diff] [blame] | 110 | |
| 111 | // FIXME |
| 112 | callbackHandler = new Handler() { |
| 113 | public void handleMessage(Message msg) { |
| 114 | Bundle b = msg.getData(); |
| 115 | TextView callVoidText; |
| 116 | |
Emeric Vigier | c411ce3 | 2012-08-29 09:28:28 -0400 | [diff] [blame] | 117 | Log.i(TAG, "handleMessage"); |
Emeric Vigier | 383b2a2 | 2012-08-27 14:20:05 -0400 | [diff] [blame] | 118 | |
| 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 Vigier | c411ce3 | 2012-08-29 09:28:28 -0400 | [diff] [blame] | 124 | Log.i(TAG, "handleMessage: " + b.getString("callback_string")); |
Emeric Vigier | 383b2a2 | 2012-08-27 14:20:05 -0400 | [diff] [blame] | 125 | } |
| 126 | }; |
Emeric Vigier | 7a255aa | 2012-08-29 09:49:18 -0400 | [diff] [blame] | 127 | ManagerImpl.setAppPath(getAppPath()); |
Emeric Vigier | 383b2a2 | 2012-08-27 14:20:05 -0400 | [diff] [blame] | 128 | managerImpl = new ManagerImpl(callbackHandler); |
| 129 | Log.i(TAG, "managerImpl created with callbackHandler " + callbackHandler); |
Adrien Béraud | ffd3241 | 2012-08-07 18:39:23 -0400 | [diff] [blame] | 130 | } |
| 131 | |
Emeric Vigier | 62ca14d | 2012-08-24 11:05:09 -0400 | [diff] [blame] | 132 | // 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éraud | ffd3241 | 2012-08-07 18:39:23 -0400 | [diff] [blame] | 150 | @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 Vigier | 05e894e | 2012-08-20 13:53:02 -0400 | [diff] [blame] | 172 | // Log.d(TAG, "onTabReselected"); |
| 173 | // ManagerImpl.initN(""); |
Adrien Béraud | ffd3241 | 2012-08-07 18:39:23 -0400 | [diff] [blame] | 174 | } |
| 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 Vigier | 62ca14d | 2012-08-24 11:05:09 -0400 | [diff] [blame] | 192 | |
Emeric Vigier | 05e894e | 2012-08-20 13:53:02 -0400 | [diff] [blame] | 193 | switch (i) { |
| 194 | case 0: |
Adrien Béraud | ffd3241 | 2012-08-07 18:39:23 -0400 | [diff] [blame] | 195 | fragment = new CallElementList(); |
Emeric Vigier | 05e894e | 2012-08-20 13:53:02 -0400 | [diff] [blame] | 196 | break; |
| 197 | case 1: |
Adrien Béraud | ffd3241 | 2012-08-07 18:39:23 -0400 | [diff] [blame] | 198 | fragment = new DummySectionFragment(); |
Emeric Vigier | 05e894e | 2012-08-20 13:53:02 -0400 | [diff] [blame] | 199 | break; |
| 200 | case 2: |
Emeric Vigier | 62ca14d | 2012-08-24 11:05:09 -0400 | [diff] [blame] | 201 | buttonFragment = new ButtonSectionFragment(); |
| 202 | Log.i(TAG, "getItem: fragment is " + buttonFragment); |
| 203 | fragment = buttonFragment; |
Emeric Vigier | 05e894e | 2012-08-20 13:53:02 -0400 | [diff] [blame] | 204 | break; |
| 205 | default: |
| 206 | Log.e(TAG, "getItem: unknown tab position " + i); |
| 207 | return null; |
| 208 | } |
| 209 | |
Adrien Béraud | ffd3241 | 2012-08-07 18:39:23 -0400 | [diff] [blame] | 210 | 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 Vigier | 05e894e | 2012-08-20 13:53:02 -0400 | [diff] [blame] | 219 | return 3; |
Adrien Béraud | ffd3241 | 2012-08-07 18:39:23 -0400 | [diff] [blame] | 220 | } |
| 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 Vigier | 05e894e | 2012-08-20 13:53:02 -0400 | [diff] [blame] | 230 | 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éraud | ffd3241 | 2012-08-07 18:39:23 -0400 | [diff] [blame] | 235 | } |
| 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 Vigier | 05e894e | 2012-08-20 13:53:02 -0400 | [diff] [blame] | 254 | public View onCreateView(LayoutInflater inflater, ViewGroup parent, Bundle savedInstanceState) |
Adrien Béraud | ffd3241 | 2012-08-07 18:39:23 -0400 | [diff] [blame] | 255 | { |
| 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 Vigier | 05e894e | 2012-08-20 13:53:02 -0400 | [diff] [blame] | 260 | textView.setText("java sucks"); |
Adrien Béraud | ffd3241 | 2012-08-07 18:39:23 -0400 | [diff] [blame] | 261 | return textView; |
| 262 | } |
Adrien Béraud | ffd3241 | 2012-08-07 18:39:23 -0400 | [diff] [blame] | 263 | } |
Emeric Vigier | 05e894e | 2012-08-20 13:53:02 -0400 | [diff] [blame] | 264 | |
Emeric Vigier | 7a255aa | 2012-08-29 09:49:18 -0400 | [diff] [blame] | 265 | 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 Vigier | 62ca14d | 2012-08-24 11:05:09 -0400 | [diff] [blame] | 278 | } |
Emeric Vigier | 05e894e | 2012-08-20 13:53:02 -0400 | [diff] [blame] | 279 | |
Emeric Vigier | 62ca14d | 2012-08-24 11:05:09 -0400 | [diff] [blame] | 280 | @Override |
| 281 | public void onClick(View view) |
Emeric Vigier | 78a53e9 | 2012-08-29 18:15:34 -0400 | [diff] [blame^] | 282 | { |
Emeric Vigier | 62ca14d | 2012-08-24 11:05:09 -0400 | [diff] [blame] | 283 | switch (view.getId()) { |
Emeric Vigier | d5c17f5 | 2012-08-29 09:29:33 -0400 | [diff] [blame] | 284 | case R.id.buttonCall: |
Emeric Vigier | 39a11fa | 2012-08-29 15:25:09 -0400 | [diff] [blame] | 285 | TextView textView = (TextView) findViewById(R.id.editAccountID); |
| 286 | String accountID = textView.getText().toString(); |
| 287 | EditText editText; |
Emeric Vigier | 78a53e9 | 2012-08-29 18:15:34 -0400 | [diff] [blame^] | 288 | Random random = new Random(); |
Emeric Vigier | d5c17f5 | 2012-08-29 09:29:33 -0400 | [diff] [blame] | 289 | |
| 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 Vigier | 78a53e9 | 2012-08-29 18:15:34 -0400 | [diff] [blame^] | 297 | /* new random callID */ |
| 298 | callID = Integer.toString(random.nextInt()); |
| 299 | |
Emeric Vigier | d5c17f5 | 2012-08-29 09:29:33 -0400 | [diff] [blame] | 300 | Log.d(TAG, "ManagerImpl.placeCall(" + accountID + ", " + callID + ", " + to + ");"); |
| 301 | ManagerImpl.placeCall(accountID, callID, to); |
| 302 | break; |
Emeric Vigier | 78a53e9 | 2012-08-29 18:15:34 -0400 | [diff] [blame^] | 303 | case R.id.buttonHangUp: |
| 304 | Log.d(TAG, "ManagerImpl.hangUp(" + callID + ");"); |
| 305 | ManagerImpl.hangUp(callID); |
| 306 | break; |
Emeric Vigier | 62ca14d | 2012-08-24 11:05:09 -0400 | [diff] [blame] | 307 | case R.id.buttonInit: |
| 308 | ManagerImpl.initN(""); |
| 309 | break; |
Emeric Vigier | 62ca14d | 2012-08-24 11:05:09 -0400 | [diff] [blame] | 310 | 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 Vigier | 383b2a2 | 2012-08-27 14:20:05 -0400 | [diff] [blame] | 319 | Data daita = new Data(43, "bar"); |
| 320 | String s = ManagerImpl.getDataString(daita); |
Emeric Vigier | 62ca14d | 2012-08-24 11:05:09 -0400 | [diff] [blame] | 321 | if (s != "") { |
Emeric Vigier | 383b2a2 | 2012-08-27 14:20:05 -0400 | [diff] [blame] | 322 | buttonFragment.getDataStringText().setText("getDataString(Data(43, \"bar\")) == \"" + s + "\""); |
Emeric Vigier | 62ca14d | 2012-08-24 11:05:09 -0400 | [diff] [blame] | 323 | } |
| 324 | break; |
| 325 | default: |
| 326 | Log.w(TAG, "unknown button " + view.getId()); |
| 327 | break; |
Emeric Vigier | 05e894e | 2012-08-20 13:53:02 -0400 | [diff] [blame] | 328 | } |
Emeric Vigier | 05e894e | 2012-08-20 13:53:02 -0400 | [diff] [blame] | 329 | } |
Adrien Béraud | ffd3241 | 2012-08-07 18:39:23 -0400 | [diff] [blame] | 330 | } |