Back to project page LCFR-Mobile-Android.
The source code is released under:
Apache License
If you think the Android project LCFR-Mobile-Android 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 org.dizon.lcfrmobile; //from w w w .ja v a 2s .c o m import java.io.IOException; import java.io.InputStream; import java.util.Locale; import org.dizon.lcfr.ProtocolContent; import org.dizon.lcfr.ProtocolXmlParser; import org.dizon.lcfrmobile.MainDisplayFragment.OnFragmentInteractionListener; import org.xmlpull.v1.XmlPullParserException; import android.app.ActionBar; import android.app.FragmentTransaction; import android.content.Intent; import android.net.Uri; import android.os.Bundle; 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.app.FragmentStatePagerAdapter; import android.support.v4.view.ViewPager; import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.widget.Toast; public class MainActivity extends FragmentActivity implements ActionBar.TabListener, IncidentListFragment.Callbacks, ProtocolListFragment.Callbacks, OnFragmentInteractionListener, OnDownloadNotify{ private boolean mTwoPane; private String item1; private String protocolFile = "protocolindex.xml"; /** * The {@link android.support.v4.view.PagerAdapter} that will provide * fragments for each of the sections. We use a * {@link android.support.v4.app.FragmentPagerAdapter} derivative, which * will keep every loaded fragment in memory. If this becomes too memory * intensive, it may be best to switch to a * {@link android.support.v4.app.FragmentStatePagerAdapter}. */ SectionsPagerAdapter mSectionsPagerAdapter; /** * The {@link ViewPager} that will host the section contents. */ ViewPager mViewPager; private ProtocolContent content; private S3IndexChecker s3checker; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); //TODO: Add logic to check for updated file item1="foo"; GetS3File s3 = new GetS3File(this.getBaseContext(), this); s3.execute(new String[]{protocolFile}); mTwoPane=false; // Set up the action bar. final ActionBar actionBar = getActionBar(); actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS); setContentView(R.layout.activity_main); // Create the adapter that will return a fragment for each of the three // primary sections of the app. mSectionsPagerAdapter = new SectionsPagerAdapter( getSupportFragmentManager()); // Set up the ViewPager with the sections adapter. mViewPager = (ViewPager) findViewById(R.id.pager); mViewPager.setAdapter(mSectionsPagerAdapter); // When swiping between different sections, select the corresponding // tab. We can also use ActionBar.Tab#select() to do this if we have // a reference to the Tab. mViewPager .setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() { @Override public void onPageSelected(int position) { actionBar.setSelectedNavigationItem(position); } }); // For each of the sections in the app, add a tab to the action bar. for (int i = 0; i < mSectionsPagerAdapter.getCount(); i++) { // Create a tab with text corresponding to the page title defined by // the adapter. Also specify this Activity object, which implements // the TabListener interface, as the callback (listener) for when // this tab is selected. actionBar.addTab(actionBar.newTab() .setText(mSectionsPagerAdapter.getPageTitle(i)) .setTabListener(this)); } } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.main, menu); return true; } @Override public boolean onOptionsItemSelected(MenuItem item){ switch (item.getItemId()) { case R.id.action_settings: Intent intent = new Intent(this, SettingsActivity.class); startActivity(intent); Toast.makeText(this, "Settings selected", Toast.LENGTH_SHORT) .show(); break; case R.id.action_refresh: Toast.makeText(this, "Refresh selected", Toast.LENGTH_SHORT) .show(); break; case R.id.action_about: Toast.makeText(this, "About selected", Toast.LENGTH_SHORT) .show(); break; default: break; } return true; } @Override public void onTabSelected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) { // When the given tab is selected, switch to the corresponding page in // the ViewPager. mViewPager.setCurrentItem(tab.getPosition()); } @Override public void onTabUnselected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) { } @Override public void onTabReselected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) { } /** * A {@link FragmentPagerAdapter} that returns a fragment corresponding to * one of the sections/tabs/pages. */ public class SectionsPagerAdapter extends FragmentStatePagerAdapter { public SectionsPagerAdapter(FragmentManager fm) { super(fm); } @Override public int getItemPosition(Object object) { // TODO Auto-generated method stub //return super.getItemPosition(object); return POSITION_NONE; } @Override public Fragment getItem(int position) { // getItem is called to instantiate the fragment for the given page. // Return a DummySectionFragment (defined as a static inner class // below) with the page number as its lone argument. if(position == 1) { Fragment fragment = new IncidentListFragment(); return fragment; } else if(position == 2) { ProtocolListFragment fragment = new ProtocolListFragment(); ProtocolListFragment.ProtoContent = content; return fragment; } else { MainDisplayFragment fragment = new MainDisplayFragment(); Bundle args = new Bundle(); args.putString("param1", item1); args.putString("param2", "I actually did something"); fragment.setArguments(args); return fragment; } } @Override public int getCount() { // Show 3 total pages. return 3; } @Override public CharSequence getPageTitle(int position) { Locale l = Locale.getDefault(); switch (position) { case 0: return getString(R.string.title_section1).toUpperCase(l); case 1: return getString(R.string.title_section2).toUpperCase(l); case 2: return getString(R.string.title_section3).toUpperCase(l); } return null; } } @Override public void onItemSelected(String id) { // TODO Auto-generated method stub if (mTwoPane) { // In two-pane mode, show the detail view in this activity by // adding or replacing the detail fragment using a // fragment transaction. Bundle arguments = new Bundle(); arguments.putString(IncidentDetailFragment.ARG_ITEM_ID, id); IncidentDetailFragment fragment = new IncidentDetailFragment(); fragment.setArguments(arguments); getSupportFragmentManager().beginTransaction() .replace(R.id.incident_detail_container, fragment).commit(); } else { // In single-pane mode, simply start the detail activity // for the selected item ID. Intent detailIntent = new Intent(this, IncidentDetailActivity.class); detailIntent.putExtra(IncidentDetailFragment.ARG_ITEM_ID, id); startActivity(detailIntent); } } @Override public void onProtocolItemSelected(String id) { // TODO Auto-generated method stub if (mTwoPane) { // In two-pane mode, show the detail view in this activity by // adding or replacing the detail fragment using a // fragment transaction. Bundle arguments = new Bundle(); arguments.putString(ProtocolDetailFragment.ARG_ITEM_ID, id); ProtocolDetailFragment fragment = new ProtocolDetailFragment(); fragment.setArguments(arguments); getSupportFragmentManager().beginTransaction() .replace(R.id.protocol_detail_container, fragment).commit(); } else { // In single-pane mode, simply start the detail activity // for the selected item ID. Intent detailIntent = new Intent(this, ProtocolDetailActivity.class); detailIntent.putExtra(ProtocolDetailFragment.ARG_ITEM_ID, id); startActivity(detailIntent); } } @Override public void onFragmentInteraction(Uri uri) { // TODO Auto-generated method stub } public void onButtonPressed(View V) { } private void onIndexDownload(){ ProtocolXmlParser xml = new ProtocolXmlParser(); InputStream in = null; content = new ProtocolContent(); try { in=openFileInput("protocolindex.xml"); } catch (IOException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } //in = ctx.getResources().openRawResource(R.raw.protocolindex); try { ProtocolContent.ITEMS = xml.parse(in); } catch (XmlPullParserException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } try { in.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } s3checker = new S3IndexChecker(this.getBaseContext(), this); s3checker.execute(new String[]{}); } @Override public void DownloadComplete(String filename) { if(filename==protocolFile){ onIndexDownload(); } } @Override public void UpdateProgress(Integer... values) { MainDisplayFragment fragment = (MainDisplayFragment)mSectionsPagerAdapter.getItem(0); int percent = values[0]; int file = values[1]; int totalfiles=values[2]; StringBuilder sb = new StringBuilder(); sb.append("Download Percent: "); sb.append(percent); sb.append("\tFile #: "); sb.append(file); sb.append("\tTotal Files: "); sb.append(totalfiles); Bundle args = new Bundle(); args.putString("param1", sb.toString()); args.putString("param2", "I actually did something"); fragment.setArguments(args); item1=sb.toString(); mSectionsPagerAdapter.notifyDataSetChanged(); //getSupportFragmentManager().beginTransaction() //.replace(R.id.main_fragment, fragment).commit(); } }