Java tutorial
/* * Copyright 2014, Hiroshi Miura <miurahr@linux.com> * Copyright 2014, BlueGnss4OSM Project * Copyright (C) 2010, 2011, 2012 BluetoothGPS4Droid Project * * This file is part of BlueGnss4OSM. * * This is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * This is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with it. If not, see <http://www.gnu.org/licenses/>. */ package org.da_cha.android.bluegnss; import org.da_cha.android.bluegnss.R; import org.da_cha.android.bluegnss.view.GnssStatusView; import android.app.AlertDialog; import android.content.Intent; import android.os.Bundle; import android.support.v7.app.ActionBarActivity; import android.support.v7.app.ActionBar; import android.support.v4.app.Fragment; import android.support.v4.app.FragmentActivity; import android.support.v4.app.FragmentManager; import android.support.v4.app.FragmentTransaction; import android.support.v4.app.FragmentPagerAdapter; import android.support.v4.view.ViewPager; import android.text.method.LinkMovementMethod; import android.widget.TextView; import android.view.View; import android.view.ViewGroup; import android.view.Menu; import android.view.MenuInflater; import android.view.MenuItem; import android.util.Log; /** * An Activity Class used to start and stop connecting BT GPS/GNSS dongle. * * @author Hiroshi Miura * */ public class MainActivity extends ActionBarActivity implements ActionBar.TabListener { private final static String LOG_TAG = "BlueGNSS"; SectionsPagerAdapter mSectionsPagerAdapter; ViewPager mViewPager; private GnssStatusView mGnssStatusView; /** Called when the activity is first created. */ @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); // Set up the action bar. final ActionBar actionBar = getSupportActionBar(); actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS); // Create the adapter that will return a fragment for each of the three // primary sections of the activity. mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager()); // Set up the ViewPager with the sections adapter. mViewPager = (ViewPager) findViewById(R.id.main_activity); 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)); } mGnssStatusView = (GnssStatusView) findViewById(R.id.GnssStatusView); } @Override protected void onSaveInstanceState(Bundle outState) { super.onSaveInstanceState(outState); } @Override protected void onResume() { super.onResume(); } @Override protected void onDestroy() { super.onDestroy(); } /* * Option Menu preparation */ @Override public boolean onCreateOptionsMenu(Menu menu) { super.onCreateOptionsMenu(menu); MenuInflater inflater = getMenuInflater(); inflater.inflate(R.menu.optionsmenu, menu); return true; } @Override public boolean onOptionsItemSelected(MenuItem item) { switch (item.getItemId()) { case R.id.menu_pref: startActivity(new Intent(this, SettingActivity.class)); return true; case R.id.menu_about: displayAboutDialog(); return true; } return super.onOptionsItemSelected(item); } @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) { } private void displayAboutDialog() { View messageView = getLayoutInflater().inflate(R.layout.about, null, false); // we need this to enable html links TextView textView = (TextView) messageView.findViewById(R.id.about_license); textView.setMovementMethod(LinkMovementMethod.getInstance()); // When linking text, force to always use default color. This works // around a pressed color state bug. int defaultColor = textView.getTextColors().getDefaultColor(); textView.setTextColor(defaultColor); textView = (TextView) messageView.findViewById(R.id.about_sources); textView.setTextColor(defaultColor); AlertDialog.Builder builder = new AlertDialog.Builder(this); builder.setTitle(R.string.about_title); builder.setIcon(R.drawable.gplv3_icon); builder.setView(messageView); builder.show(); } /** * A {@link FragmentPagerAdapter} that returns a fragment corresponding to * one of the sections/tabs/pages. */ public class SectionsPagerAdapter extends FragmentPagerAdapter { public SectionsPagerAdapter(FragmentManager fm) { super(fm); } @Override public Fragment getItem(int position) { switch (position) { case 0: return (Fragment) new MainFragment(); case 1: return (Fragment) new StatusFragment(); } return null; } @Override public int getCount() { return 2; } @Override public CharSequence getPageTitle(int position) { switch (position) { case 0: return getString(R.string.title_main); case 1: return getString(R.string.title_status); } return null; } } } // vim: tabstop=4 expandtab shiftwidth=4 softtabstop=4