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 | |
Emeric Vigier | e89b48b | 2012-08-30 14:16:46 -0400 | [diff] [blame] | 33 | import java.util.Random; |
| 34 | |
Adrien Béraud | ffd3241 | 2012-08-07 18:39:23 -0400 | [diff] [blame] | 35 | import android.app.ActionBar; |
| 36 | import android.app.Activity; |
| 37 | import android.app.Fragment; |
| 38 | import android.app.FragmentManager; |
| 39 | import android.app.FragmentTransaction; |
Emeric Vigier | 7a255aa | 2012-08-29 09:49:18 -0400 | [diff] [blame] | 40 | import android.content.pm.PackageInfo; |
| 41 | import android.content.pm.PackageManager; |
| 42 | import android.content.pm.PackageManager.NameNotFoundException; |
Adrien Béraud | ffd3241 | 2012-08-07 18:39:23 -0400 | [diff] [blame] | 43 | import android.os.Bundle; |
Emeric Vigier | 383b2a2 | 2012-08-27 14:20:05 -0400 | [diff] [blame] | 44 | import android.os.Handler; |
| 45 | import android.os.Message; |
Adrien Béraud | ffd3241 | 2012-08-07 18:39:23 -0400 | [diff] [blame] | 46 | import android.support.v13.app.FragmentStatePagerAdapter; |
| 47 | import android.support.v4.view.ViewPager; |
Emeric Vigier | 05e894e | 2012-08-20 13:53:02 -0400 | [diff] [blame] | 48 | import android.util.Log; |
Adrien Béraud | ffd3241 | 2012-08-07 18:39:23 -0400 | [diff] [blame] | 49 | import android.view.Gravity; |
| 50 | import android.view.LayoutInflater; |
| 51 | import android.view.Menu; |
| 52 | import android.view.View; |
Emeric Vigier | 05e894e | 2012-08-20 13:53:02 -0400 | [diff] [blame] | 53 | import android.view.View.OnClickListener; |
Adrien Béraud | ffd3241 | 2012-08-07 18:39:23 -0400 | [diff] [blame] | 54 | import android.view.ViewGroup; |
Emeric Vigier | e89b48b | 2012-08-30 14:16:46 -0400 | [diff] [blame] | 55 | import android.widget.Button; |
Emeric Vigier | d5c17f5 | 2012-08-29 09:29:33 -0400 | [diff] [blame] | 56 | import android.widget.EditText; |
Emeric Vigier | e89b48b | 2012-08-30 14:16:46 -0400 | [diff] [blame] | 57 | import android.widget.ImageButton; |
Adrien Béraud | ffd3241 | 2012-08-07 18:39:23 -0400 | [diff] [blame] | 58 | import android.widget.TextView; |
| 59 | |
| 60 | import com.savoirfairelinux.sflphone.R; |
| 61 | |
Emeric Vigier | 62ca14d | 2012-08-24 11:05:09 -0400 | [diff] [blame] | 62 | public class SFLPhoneHome extends Activity implements ActionBar.TabListener, OnClickListener |
Adrien Béraud | ffd3241 | 2012-08-07 18:39:23 -0400 | [diff] [blame] | 63 | { |
| 64 | SectionsPagerAdapter mSectionsPagerAdapter; |
Emeric Vigier | 62ca14d | 2012-08-24 11:05:09 -0400 | [diff] [blame] | 65 | static final String TAG = "SFLPhoneHome"; |
| 66 | ButtonSectionFragment buttonFragment; |
Emeric Vigier | e89b48b | 2012-08-30 14:16:46 -0400 | [diff] [blame] | 67 | ImageButton buttonCall, buttonHangup; |
Emeric Vigier | 383b2a2 | 2012-08-27 14:20:05 -0400 | [diff] [blame] | 68 | Handler callbackHandler; |
| 69 | static ManagerImpl managerImpl; |
Emeric Vigier | 78a53e9 | 2012-08-29 18:15:34 -0400 | [diff] [blame] | 70 | /* default callID */ |
| 71 | static String callID = "007"; |
Emeric Vigier | e89b48b | 2012-08-30 14:16:46 -0400 | [diff] [blame] | 72 | static boolean callOnGoing = false; |
Adrien Béraud | ffd3241 | 2012-08-07 18:39:23 -0400 | [diff] [blame] | 73 | |
| 74 | /** |
| 75 | * The {@link ViewPager} that will host the section contents. |
| 76 | */ |
| 77 | ViewPager mViewPager; |
| 78 | |
Emeric Vigier | 05e894e | 2012-08-20 13:53:02 -0400 | [diff] [blame] | 79 | 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] | 80 | |
| 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 Vigier | 383b2a2 | 2012-08-27 14:20:05 -0400 | [diff] [blame] | 115 | |
| 116 | // FIXME |
| 117 | callbackHandler = new Handler() { |
| 118 | public void handleMessage(Message msg) { |
| 119 | Bundle b = msg.getData(); |
| 120 | TextView callVoidText; |
| 121 | |
Emeric Vigier | c411ce3 | 2012-08-29 09:28:28 -0400 | [diff] [blame] | 122 | Log.i(TAG, "handleMessage"); |
Emeric Vigier | 383b2a2 | 2012-08-27 14:20:05 -0400 | [diff] [blame] | 123 | |
| 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 Vigier | c411ce3 | 2012-08-29 09:28:28 -0400 | [diff] [blame] | 129 | Log.i(TAG, "handleMessage: " + b.getString("callback_string")); |
Emeric Vigier | 383b2a2 | 2012-08-27 14:20:05 -0400 | [diff] [blame] | 130 | } |
| 131 | }; |
Emeric Vigier | 7a255aa | 2012-08-29 09:49:18 -0400 | [diff] [blame] | 132 | ManagerImpl.setAppPath(getAppPath()); |
Emeric Vigier | 383b2a2 | 2012-08-27 14:20:05 -0400 | [diff] [blame] | 133 | managerImpl = new ManagerImpl(callbackHandler); |
| 134 | Log.i(TAG, "managerImpl created with callbackHandler " + callbackHandler); |
Adrien Béraud | ffd3241 | 2012-08-07 18:39:23 -0400 | [diff] [blame] | 135 | } |
| 136 | |
Emeric Vigier | 62ca14d | 2012-08-24 11:05:09 -0400 | [diff] [blame] | 137 | // 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éraud | ffd3241 | 2012-08-07 18:39:23 -0400 | [diff] [blame] | 155 | @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 Vigier | 05e894e | 2012-08-20 13:53:02 -0400 | [diff] [blame] | 177 | // Log.d(TAG, "onTabReselected"); |
| 178 | // ManagerImpl.initN(""); |
Adrien Béraud | ffd3241 | 2012-08-07 18:39:23 -0400 | [diff] [blame] | 179 | } |
| 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 Vigier | 62ca14d | 2012-08-24 11:05:09 -0400 | [diff] [blame] | 197 | |
Emeric Vigier | 05e894e | 2012-08-20 13:53:02 -0400 | [diff] [blame] | 198 | switch (i) { |
| 199 | case 0: |
Adrien Béraud | ffd3241 | 2012-08-07 18:39:23 -0400 | [diff] [blame] | 200 | fragment = new CallElementList(); |
Emeric Vigier | 05e894e | 2012-08-20 13:53:02 -0400 | [diff] [blame] | 201 | break; |
| 202 | case 1: |
Adrien Béraud | ffd3241 | 2012-08-07 18:39:23 -0400 | [diff] [blame] | 203 | fragment = new DummySectionFragment(); |
Emeric Vigier | 05e894e | 2012-08-20 13:53:02 -0400 | [diff] [blame] | 204 | break; |
| 205 | case 2: |
Emeric Vigier | 62ca14d | 2012-08-24 11:05:09 -0400 | [diff] [blame] | 206 | buttonFragment = new ButtonSectionFragment(); |
| 207 | Log.i(TAG, "getItem: fragment is " + buttonFragment); |
| 208 | fragment = buttonFragment; |
Emeric Vigier | 05e894e | 2012-08-20 13:53:02 -0400 | [diff] [blame] | 209 | break; |
| 210 | default: |
| 211 | Log.e(TAG, "getItem: unknown tab position " + i); |
| 212 | return null; |
| 213 | } |
| 214 | |
Adrien Béraud | ffd3241 | 2012-08-07 18:39:23 -0400 | [diff] [blame] | 215 | 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 Vigier | 05e894e | 2012-08-20 13:53:02 -0400 | [diff] [blame] | 224 | return 3; |
Adrien Béraud | ffd3241 | 2012-08-07 18:39:23 -0400 | [diff] [blame] | 225 | } |
| 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 Vigier | 05e894e | 2012-08-20 13:53:02 -0400 | [diff] [blame] | 235 | 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éraud | ffd3241 | 2012-08-07 18:39:23 -0400 | [diff] [blame] | 240 | } |
| 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 Vigier | 05e894e | 2012-08-20 13:53:02 -0400 | [diff] [blame] | 259 | public View onCreateView(LayoutInflater inflater, ViewGroup parent, Bundle savedInstanceState) |
Adrien Béraud | ffd3241 | 2012-08-07 18:39:23 -0400 | [diff] [blame] | 260 | { |
| 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 Vigier | 05e894e | 2012-08-20 13:53:02 -0400 | [diff] [blame] | 265 | textView.setText("java sucks"); |
Adrien Béraud | ffd3241 | 2012-08-07 18:39:23 -0400 | [diff] [blame] | 266 | return textView; |
| 267 | } |
Adrien Béraud | ffd3241 | 2012-08-07 18:39:23 -0400 | [diff] [blame] | 268 | } |
Emeric Vigier | 05e894e | 2012-08-20 13:53:02 -0400 | [diff] [blame] | 269 | |
Emeric Vigier | 7a255aa | 2012-08-29 09:49:18 -0400 | [diff] [blame] | 270 | 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 Vigier | 62ca14d | 2012-08-24 11:05:09 -0400 | [diff] [blame] | 283 | } |
Emeric Vigier | 05e894e | 2012-08-20 13:53:02 -0400 | [diff] [blame] | 284 | |
Emeric Vigier | 62ca14d | 2012-08-24 11:05:09 -0400 | [diff] [blame] | 285 | @Override |
| 286 | public void onClick(View view) |
Emeric Vigier | e89b48b | 2012-08-30 14:16:46 -0400 | [diff] [blame] | 287 | { |
| 288 | buttonCall = (ImageButton) findViewById(R.id.buttonCall); |
| 289 | buttonHangup = (ImageButton) findViewById(R.id.buttonHangUp); |
| 290 | |
Emeric Vigier | 62ca14d | 2012-08-24 11:05:09 -0400 | [diff] [blame] | 291 | switch (view.getId()) { |
Emeric Vigier | d5c17f5 | 2012-08-29 09:29:33 -0400 | [diff] [blame] | 292 | case R.id.buttonCall: |
Emeric Vigier | 39a11fa | 2012-08-29 15:25:09 -0400 | [diff] [blame] | 293 | TextView textView = (TextView) findViewById(R.id.editAccountID); |
| 294 | String accountID = textView.getText().toString(); |
| 295 | EditText editText; |
Emeric Vigier | 78a53e9 | 2012-08-29 18:15:34 -0400 | [diff] [blame] | 296 | Random random = new Random(); |
Emeric Vigier | e89b48b | 2012-08-30 14:16:46 -0400 | [diff] [blame] | 297 | |
| 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 Vigier | d5c17f5 | 2012-08-29 09:29:33 -0400 | [diff] [blame] | 314 | } |
Emeric Vigier | d5c17f5 | 2012-08-29 09:29:33 -0400 | [diff] [blame] | 315 | break; |
Emeric Vigier | 78a53e9 | 2012-08-29 18:15:34 -0400 | [diff] [blame] | 316 | case R.id.buttonHangUp: |
Emeric Vigier | e89b48b | 2012-08-30 14:16:46 -0400 | [diff] [blame] | 317 | 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 Vigier | 78a53e9 | 2012-08-29 18:15:34 -0400 | [diff] [blame] | 324 | break; |
Emeric Vigier | 62ca14d | 2012-08-24 11:05:09 -0400 | [diff] [blame] | 325 | case R.id.buttonInit: |
| 326 | ManagerImpl.initN(""); |
| 327 | break; |
Emeric Vigier | 62ca14d | 2012-08-24 11:05:09 -0400 | [diff] [blame] | 328 | 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 Vigier | 383b2a2 | 2012-08-27 14:20:05 -0400 | [diff] [blame] | 337 | Data daita = new Data(43, "bar"); |
| 338 | String s = ManagerImpl.getDataString(daita); |
Emeric Vigier | 62ca14d | 2012-08-24 11:05:09 -0400 | [diff] [blame] | 339 | if (s != "") { |
Emeric Vigier | 383b2a2 | 2012-08-27 14:20:05 -0400 | [diff] [blame] | 340 | buttonFragment.getDataStringText().setText("getDataString(Data(43, \"bar\")) == \"" + s + "\""); |
Emeric Vigier | 62ca14d | 2012-08-24 11:05:09 -0400 | [diff] [blame] | 341 | } |
| 342 | break; |
| 343 | default: |
| 344 | Log.w(TAG, "unknown button " + view.getId()); |
| 345 | break; |
Emeric Vigier | 05e894e | 2012-08-20 13:53:02 -0400 | [diff] [blame] | 346 | } |
Emeric Vigier | 05e894e | 2012-08-20 13:53:02 -0400 | [diff] [blame] | 347 | } |
Adrien Béraud | ffd3241 | 2012-08-07 18:39:23 -0400 | [diff] [blame] | 348 | } |