Back to project page Android-Apps.
The source code is released under:
Apache License
If you think the Android project Android-Apps listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.
package com.kniezrec.voiceremote2; /*from w w w. jav a2 s . c om*/ import com.kniezrec.voiceremote2.R; import android.app.ActionBar; import android.app.FragmentTransaction; import android.content.Context; import android.content.Intent; import android.os.Bundle; import android.os.Vibrator; import android.support.v4.app.Fragment; import android.support.v4.app.FragmentActivity; import android.support.v4.app.FragmentManager; import android.support.v4.app.FragmentPagerAdapter; import android.support.v4.view.ViewPager; import android.util.Log; import android.view.KeyEvent; import android.view.Menu; import android.view.MenuItem; import android.view.WindowManager; public class MainActivity extends FragmentActivity implements ActionBar.TabListener { public static final int VOICE_RECOGNITION_REQUEST_CODE = 1001; public static final String PREFS_SERVER_HOST_KEY = "serverHost"; public static final String PREFS_TIMEOUT = "5"; public static final String PREFS_SERVER_HOST_DEFAULT = "Please change the IP address in settings"; public static final String PREFS_SERVER_PORT_KEY = "serverPort"; public static final String PREFS_SERVER_PORT_DEFAULT = "2345"; public static final String PREFS_SENDER_FACTORY_KEY = "keyCodeSenderFactory"; public static final String PREFS_LANGUAGE = "LanguageSet"; public static final String PREFS_TIMEOUT_KEY = "timeoutSec"; public static final String PREFS_PAUSE_KEY = "pauseSec"; public static final String PREFS_FAVOURITES = "favr"; public static final String PREFS_TO_LOUD_KEY = "toLoudKeys"; public static final String PREFS_TO_QUIET_KEY = "toQuietKey"; public static final String PREFS_SENDER_FACTORY_DEFAULT = CSeriesKeyCodeSenderFactory.class .getCanonicalName(); private static ListActionsFragment listActionsFragment; private static MainFragment mainFragment; ViewPager mViewPager; static String[] arrTabs; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON); setContentView(R.layout.activity_fragment_main); final AppSectionsPagerAdapter mAppSectionsPagerAdapter = new AppSectionsPagerAdapter( getSupportFragmentManager()); final ActionBar actionBar = getActionBar(); // /actionBar.setDisplayShowHomeEnabled(false); // actionBar.setDisplayShowTitleEnabled(false); actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS); actionBar.hide(); mViewPager = (ViewPager) findViewById(R.id.pager); mViewPager.setAdapter(mAppSectionsPagerAdapter); mViewPager .setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() { @Override public void onPageSelected(int position) { actionBar.setSelectedNavigationItem(position); } }); arrTabs = getResources().getStringArray(R.array.tabs); for (int i = 0; i < mAppSectionsPagerAdapter.getCount(); i++) { actionBar.addTab(actionBar.newTab() .setText(mAppSectionsPagerAdapter.getPageTitle(i)) .setTabListener(this)); } actionBar.setSelectedNavigationItem(1); // create fragments to use if (savedInstanceState != null) { mainFragment = (MainFragment) getSupportFragmentManager() .getFragment(savedInstanceState, MainFragment.class.getName()); listActionsFragment = (ListActionsFragment) getSupportFragmentManager() .getFragment(savedInstanceState, ListActionsFragment.class.getName()); } else { mainFragment = null; listActionsFragment = null; } } @Override public void onTabUnselected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) { } @Override public void onTabSelected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) { mViewPager.setCurrentItem(tab.getPosition()); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.activity_main, menu); return true; } public boolean onOptionsItemSelected(MenuItem item) { // Handle item selection Intent intent; switch (item.getItemId()) { case R.id.help: getActionBar().setSelectedNavigationItem(3); return true; case R.id.menu_settings: intent = new Intent(this, SettingsActivity.class); startActivity(intent); this.overridePendingTransition(R.anim.slide_x, R.anim.slidebackx); return true; default: return super.onOptionsItemSelected(item); } } @Override public void onTabReselected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) { } public static class AppSectionsPagerAdapter extends FragmentPagerAdapter { public AppSectionsPagerAdapter(FragmentManager fm) { super(fm); } @Override public Fragment getItem(int i) { switch (i) { case 0: return new CommandsFragment(); case 1: { mainFragment = null; mainFragment = new MainFragment(); return mainFragment; } case 2: { listActionsFragment = new ListActionsFragment(); return listActionsFragment; } case 3: return new HelpFragment(); default: return new MainFragment(); } } @Override public int getCount() { return 4; } @Override public CharSequence getPageTitle(int position) { return arrTabs[position]; } } @Override protected void onSaveInstanceState(Bundle outState) { super.onSaveInstanceState(outState); getSupportFragmentManager().putFragment(outState, MainFragment.class.getName(), mainFragment); getSupportFragmentManager().putFragment(outState, ListActionsFragment.class.getName(), listActionsFragment); } @Override public boolean onKeyDown(int keyCode, KeyEvent event) { if (keyCode == KeyEvent.KEYCODE_BACK) { if (mainFragment != null && mainFragment.isListening) { mainFragment.StopListening(); } else { Log.v("", Boolean.toString(mainFragment.isListening)); super.onBackPressed(); } return true; } return super.onKeyDown(keyCode, event); } public void refreshList() { if (listActionsFragment != null) { listActionsFragment.onResume(); } } public static void vibrate(Context context, long millis) { if (millis == 0) return; Vibrator v = (Vibrator) context .getSystemService(Context.VIBRATOR_SERVICE); if (v != null) { v.vibrate(millis); } } }