Back to project page sensor-supervisor.
The source code is released under:
GNU General Public License
If you think the Android project sensor-supervisor 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.thirdi.sensorsupervisor; // w w w . j a va 2 s. c o m import android.app.ActionBar; import android.app.Activity; import android.app.Fragment; import android.app.FragmentManager; import android.app.FragmentTransaction; import android.content.Context; import android.hardware.SensorManager; import android.os.Bundle; import android.support.v4.widget.DrawerLayout; import android.view.LayoutInflater; import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.view.ViewGroup; public class MainActivity extends Activity implements NavigationDrawerFragment.NavigationDrawerCallbacks { /** * Main activity of our app. It is being invoked when the app starts. * * mTitle : Holds the value of title part. * mSensorFragment, mGPSFragment, mNetworkFragment, mMediaFragment, mSettingsFragment : Fragments * that will be replaced with the container when needed. */ private NavigationDrawerFragment mNavigationDrawerFragment; private CharSequence mTitle; private FragmentManager mFragmentManager; private SensorFragment mSensorFragment; private GPSFragment mGPSFragment; private NetworkFragment mNetworkFragment; private CameraFragment mCameraFragment; private AudioFragment mAudioFragment; private FragmentTransaction mFragmentTransaction; private DatabaseFragment mDatabaseFragment; private SettingsFragment mSettingsFragment; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mNavigationDrawerFragment = (NavigationDrawerFragment) getFragmentManager().findFragmentById(R.id.navigation_drawer); mTitle = getTitle(); //Initialize member variables when app is first started. mFragmentManager = getFragmentManager(); mSensorFragment = new SensorFragment(); mGPSFragment = new GPSFragment(); mNetworkFragment = new NetworkFragment(); mCameraFragment = new CameraFragment(); mAudioFragment = new AudioFragment(); mSettingsFragment = new SettingsFragment(); mDatabaseFragment = new DatabaseFragment(); // Set up the drawer. mNavigationDrawerFragment.setUp( R.id.navigation_drawer, (DrawerLayout) findViewById(R.id.drawer_layout)); } @Override public void onNavigationDrawerItemSelected(int position) { // update the main content by replacing fragments FragmentManager fragmentManager = getFragmentManager(); fragmentManager.beginTransaction() .replace(R.id.container, PlaceholderFragment.newInstance(position + 1)) .commit(); } public void onSectionAttached(int number) { // An element from NavigationDrawer is selected, replace the old fragment with the new one. // Basically, start a fragment transaction, define it, then commit it. // Camera and Audio Fragment Changed // Camera and Audio added switch (number) { case 1: mTitle = getString(R.string.title_section1); mFragmentTransaction = mFragmentManager.beginTransaction(); mFragmentTransaction.replace(R.id.container, mSensorFragment); mFragmentTransaction.commit(); break; case 2: mTitle = getString(R.string.title_section2); mFragmentTransaction = mFragmentManager.beginTransaction(); mFragmentTransaction.replace(R.id.container, mGPSFragment); mFragmentTransaction.commit(); break; case 3: mTitle = getString(R.string.title_section3); mFragmentTransaction = mFragmentManager.beginTransaction(); mFragmentTransaction.replace(R.id.container, mNetworkFragment); mFragmentTransaction.commit(); break; case 4: mTitle = getString(R.string.title_section4); mFragmentTransaction = mFragmentManager.beginTransaction(); mFragmentTransaction.replace(R.id.container, mCameraFragment); mFragmentTransaction.commit(); break; case 5: mTitle = getString(R.string.title_section5); mFragmentTransaction = mFragmentManager.beginTransaction(); mFragmentTransaction.replace(R.id.container, mAudioFragment); mFragmentTransaction.commit(); break; case 6: mTitle = getString(R.string.title_section6); mFragmentTransaction = mFragmentManager.beginTransaction(); mFragmentTransaction.replace(R.id.container, mDatabaseFragment); mFragmentTransaction.commit(); break; case 7: mTitle = getString(R.string.action_settings); mFragmentTransaction = mFragmentManager.beginTransaction(); mFragmentTransaction.replace(R.id.container, mSettingsFragment); mFragmentTransaction.commit(); break; } } public void restoreActionBar() { ActionBar actionBar = getActionBar(); actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_STANDARD); actionBar.setDisplayShowTitleEnabled(true); actionBar.setTitle(mTitle); } @Override public boolean onCreateOptionsMenu(Menu menu) { if (!mNavigationDrawerFragment.isDrawerOpen()) { // Only show items in the action bar relevant to this screen // if the drawer is not showing. Otherwise, let the drawer // decide what to show in the action bar. getMenuInflater().inflate(R.menu.main, menu); restoreActionBar(); return true; } return super.onCreateOptionsMenu(menu); } @Override public boolean onOptionsItemSelected(MenuItem item) { // Handle action bar item clicks here. The action bar will // automatically handle clicks on the Home/Up button, so long // as you specify a parent activity in AndroidManifest.xml. int id = item.getItemId(); if (id == R.id.action_settings) { //Settings is selected, bring settings in front. mTitle = getString(R.string.action_settings); mFragmentTransaction = mFragmentManager.beginTransaction(); mFragmentTransaction.replace(R.id.container, mSettingsFragment); mFragmentTransaction.commit(); return true; } return super.onOptionsItemSelected(item); } /** * A placeholder fragment containing a simple view. */ public static class PlaceholderFragment extends Fragment { /** * The fragment argument representing the section number for this * fragment. */ private static final String ARG_SECTION_NUMBER = "section_number"; /** * Returns a new instance of this fragment for the given section * number. */ public static PlaceholderFragment newInstance(int sectionNumber) { PlaceholderFragment fragment = new PlaceholderFragment(); Bundle args = new Bundle(); args.putInt(ARG_SECTION_NUMBER, sectionNumber); fragment.setArguments(args); return fragment; } public PlaceholderFragment() { } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View rootView = inflater.inflate(R.layout.fragment_main, container, false); return rootView; } @Override public void onAttach(Activity activity) { super.onAttach(activity); ((MainActivity) activity).onSectionAttached( getArguments().getInt(ARG_SECTION_NUMBER)); } } }