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; |
| 38 | import android.os.Bundle; |
Emeric Vigier | 383b2a2 | 2012-08-27 14:20:05 -0400 | [diff] [blame] | 39 | import android.os.Handler; |
| 40 | import android.os.Message; |
Adrien Béraud | ffd3241 | 2012-08-07 18:39:23 -0400 | [diff] [blame] | 41 | import android.support.v13.app.FragmentStatePagerAdapter; |
| 42 | import android.support.v4.view.ViewPager; |
Emeric Vigier | 05e894e | 2012-08-20 13:53:02 -0400 | [diff] [blame] | 43 | import android.util.Log; |
Adrien Béraud | ffd3241 | 2012-08-07 18:39:23 -0400 | [diff] [blame] | 44 | import android.view.Gravity; |
| 45 | import android.view.LayoutInflater; |
| 46 | import android.view.Menu; |
| 47 | import android.view.View; |
Emeric Vigier | 05e894e | 2012-08-20 13:53:02 -0400 | [diff] [blame] | 48 | import android.view.View.OnClickListener; |
Adrien Béraud | ffd3241 | 2012-08-07 18:39:23 -0400 | [diff] [blame] | 49 | import android.view.ViewGroup; |
| 50 | import android.widget.TextView; |
Emeric Vigier | 62ca14d | 2012-08-24 11:05:09 -0400 | [diff] [blame] | 51 | import com.savoirfairelinux.sflphone.client.Data; |
| 52 | import com.savoirfairelinux.sflphone.client.ManagerImpl; |
Adrien Béraud | ffd3241 | 2012-08-07 18:39:23 -0400 | [diff] [blame] | 53 | |
| 54 | import com.savoirfairelinux.sflphone.R; |
| 55 | |
Emeric Vigier | 62ca14d | 2012-08-24 11:05:09 -0400 | [diff] [blame] | 56 | public class SFLPhoneHome extends Activity implements ActionBar.TabListener, OnClickListener |
Adrien Béraud | ffd3241 | 2012-08-07 18:39:23 -0400 | [diff] [blame] | 57 | { |
| 58 | SectionsPagerAdapter mSectionsPagerAdapter; |
Emeric Vigier | 62ca14d | 2012-08-24 11:05:09 -0400 | [diff] [blame] | 59 | static final String TAG = "SFLPhoneHome"; |
| 60 | ButtonSectionFragment buttonFragment; |
Emeric Vigier | 383b2a2 | 2012-08-27 14:20:05 -0400 | [diff] [blame] | 61 | Handler callbackHandler; |
| 62 | static ManagerImpl managerImpl; |
Adrien Béraud | ffd3241 | 2012-08-07 18:39:23 -0400 | [diff] [blame] | 63 | |
| 64 | /** |
| 65 | * The {@link ViewPager} that will host the section contents. |
| 66 | */ |
| 67 | ViewPager mViewPager; |
| 68 | |
Emeric Vigier | 05e894e | 2012-08-20 13:53:02 -0400 | [diff] [blame] | 69 | 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] | 70 | |
| 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 Vigier | 383b2a2 | 2012-08-27 14:20:05 -0400 | [diff] [blame] | 105 | |
| 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éraud | ffd3241 | 2012-08-07 18:39:23 -0400 | [diff] [blame] | 124 | } |
| 125 | |
Emeric Vigier | 62ca14d | 2012-08-24 11:05:09 -0400 | [diff] [blame] | 126 | // 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éraud | ffd3241 | 2012-08-07 18:39:23 -0400 | [diff] [blame] | 144 | @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 Vigier | 05e894e | 2012-08-20 13:53:02 -0400 | [diff] [blame] | 166 | // Log.d(TAG, "onTabReselected"); |
| 167 | // ManagerImpl.initN(""); |
Adrien Béraud | ffd3241 | 2012-08-07 18:39:23 -0400 | [diff] [blame] | 168 | } |
| 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 Vigier | 62ca14d | 2012-08-24 11:05:09 -0400 | [diff] [blame] | 186 | |
Emeric Vigier | 05e894e | 2012-08-20 13:53:02 -0400 | [diff] [blame] | 187 | switch (i) { |
| 188 | case 0: |
Adrien Béraud | ffd3241 | 2012-08-07 18:39:23 -0400 | [diff] [blame] | 189 | fragment = new CallElementList(); |
Emeric Vigier | 05e894e | 2012-08-20 13:53:02 -0400 | [diff] [blame] | 190 | break; |
| 191 | case 1: |
Adrien Béraud | ffd3241 | 2012-08-07 18:39:23 -0400 | [diff] [blame] | 192 | fragment = new DummySectionFragment(); |
Emeric Vigier | 05e894e | 2012-08-20 13:53:02 -0400 | [diff] [blame] | 193 | break; |
| 194 | case 2: |
Emeric Vigier | 62ca14d | 2012-08-24 11:05:09 -0400 | [diff] [blame] | 195 | buttonFragment = new ButtonSectionFragment(); |
| 196 | Log.i(TAG, "getItem: fragment is " + buttonFragment); |
| 197 | fragment = buttonFragment; |
Emeric Vigier | 05e894e | 2012-08-20 13:53:02 -0400 | [diff] [blame] | 198 | break; |
| 199 | default: |
| 200 | Log.e(TAG, "getItem: unknown tab position " + i); |
| 201 | return null; |
| 202 | } |
| 203 | |
Adrien Béraud | ffd3241 | 2012-08-07 18:39:23 -0400 | [diff] [blame] | 204 | 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 Vigier | 05e894e | 2012-08-20 13:53:02 -0400 | [diff] [blame] | 213 | return 3; |
Adrien Béraud | ffd3241 | 2012-08-07 18:39:23 -0400 | [diff] [blame] | 214 | } |
| 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 Vigier | 05e894e | 2012-08-20 13:53:02 -0400 | [diff] [blame] | 224 | 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éraud | ffd3241 | 2012-08-07 18:39:23 -0400 | [diff] [blame] | 229 | } |
| 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 Vigier | 05e894e | 2012-08-20 13:53:02 -0400 | [diff] [blame] | 248 | public View onCreateView(LayoutInflater inflater, ViewGroup parent, Bundle savedInstanceState) |
Adrien Béraud | ffd3241 | 2012-08-07 18:39:23 -0400 | [diff] [blame] | 249 | { |
| 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 Vigier | 05e894e | 2012-08-20 13:53:02 -0400 | [diff] [blame] | 254 | textView.setText("java sucks"); |
Adrien Béraud | ffd3241 | 2012-08-07 18:39:23 -0400 | [diff] [blame] | 255 | return textView; |
| 256 | } |
Adrien Béraud | ffd3241 | 2012-08-07 18:39:23 -0400 | [diff] [blame] | 257 | } |
Emeric Vigier | 05e894e | 2012-08-20 13:53:02 -0400 | [diff] [blame] | 258 | |
Emeric Vigier | 62ca14d | 2012-08-24 11:05:09 -0400 | [diff] [blame] | 259 | 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 Vigier | 05e894e | 2012-08-20 13:53:02 -0400 | [diff] [blame] | 272 | |
Emeric Vigier | 62ca14d | 2012-08-24 11:05:09 -0400 | [diff] [blame] | 273 | @Override |
| 274 | public void onClick(View view) |
| 275 | { |
| 276 | switch (view.getId()) { |
Emeric Vigier | 62ca14d | 2012-08-24 11:05:09 -0400 | [diff] [blame] | 277 | case R.id.buttonInit: |
| 278 | ManagerImpl.initN(""); |
| 279 | break; |
Emeric Vigier | 62ca14d | 2012-08-24 11:05:09 -0400 | [diff] [blame] | 280 | 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 Vigier | 383b2a2 | 2012-08-27 14:20:05 -0400 | [diff] [blame] | 289 | Data daita = new Data(43, "bar"); |
| 290 | String s = ManagerImpl.getDataString(daita); |
Emeric Vigier | 62ca14d | 2012-08-24 11:05:09 -0400 | [diff] [blame] | 291 | if (s != "") { |
Emeric Vigier | 383b2a2 | 2012-08-27 14:20:05 -0400 | [diff] [blame] | 292 | buttonFragment.getDataStringText().setText("getDataString(Data(43, \"bar\")) == \"" + s + "\""); |
Emeric Vigier | 62ca14d | 2012-08-24 11:05:09 -0400 | [diff] [blame] | 293 | } |
| 294 | break; |
| 295 | default: |
| 296 | Log.w(TAG, "unknown button " + view.getId()); |
| 297 | break; |
Emeric Vigier | 05e894e | 2012-08-20 13:53:02 -0400 | [diff] [blame] | 298 | } |
Emeric Vigier | 05e894e | 2012-08-20 13:53:02 -0400 | [diff] [blame] | 299 | } |
Adrien Béraud | ffd3241 | 2012-08-07 18:39:23 -0400 | [diff] [blame] | 300 | } |