Java tutorial
/* * Copyright 2013 Andrew Okin * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package net.forkk.autocron; import android.app.ActionBar; import android.app.Fragment; import android.app.FragmentManager; import android.app.FragmentTransaction; import android.content.Intent; import android.os.Bundle; import android.support.v13.app.FragmentPagerAdapter; import android.support.v4.app.FragmentActivity; import android.support.v4.view.ViewPager; import net.forkk.autocron.data.ComponentPointer; import net.forkk.autocron.data.Event; import java.util.Locale; public class EditAutomationActivity extends FragmentActivity implements ActionBar.TabListener { public static final String EXTRA_AUTOMATION_POINTER = "net.forkk.autocron.automation_ptr"; protected ComponentPointer mAutomationPointer; protected boolean mShowTriggerList = false; public EditAutomationActivity() { } /** * 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}. */ protected SectionsPagerAdapter mPagerAdapter; /** * The {@link ViewPager} that will host the section contents. */ protected ViewPager mViewPager; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); Intent intent = getIntent(); assert intent != null; mAutomationPointer = (ComponentPointer) intent.getSerializableExtra(EXTRA_AUTOMATION_POINTER); mShowTriggerList = mAutomationPointer instanceof Event.Pointer; setContentView(R.layout.activity_edit_automation); // Set up the action bar. final ActionBar actionBar = getActionBar(); assert actionBar != null; actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS); // Create the adapter that will return a fragment for each of the three // primary sections of the app. mPagerAdapter = new SectionsPagerAdapter(getFragmentManager()); // Set up the ViewPager with the sections adapter. mViewPager = (ViewPager) findViewById(R.id.pager); mViewPager.setAdapter(mPagerAdapter); // 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 < mPagerAdapter.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(mPagerAdapter.getPageTitle(i)).setTabListener(this)); } } @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 FragmentPagerAdapter { public SectionsPagerAdapter(FragmentManager fm) { super(fm); } @Override public Fragment getItem(int position) { if (mShowTriggerList) { if (position > 1) position--; else if (position == 1) return new AutomationComponentListFragment(mAutomationPointer, AutomationComponentListFragment.ComponentListType.Trigger); } switch (position) { case 0: return new ComponentPreferenceFragment(mAutomationPointer); case 1: return new AutomationComponentListFragment(mAutomationPointer, AutomationComponentListFragment.ComponentListType.Rule); case 2: return new AutomationComponentListFragment(mAutomationPointer, AutomationComponentListFragment.ComponentListType.Action); } return null; } @Override public int getCount() { // Show 3 total pages. return mShowTriggerList ? 4 : 3; } @Override public CharSequence getPageTitle(int position) { Locale l = Locale.getDefault(); if (mShowTriggerList) { if (position > 1) position--; else if (position == 1) return getString(R.string.title_automation_triggers).toUpperCase(l); } switch (position) { case 0: return getString(R.string.title_automation_settings).toUpperCase(l); case 1: return getString(R.string.title_automation_rules).toUpperCase(l); case 2: return getString(R.string.title_automation_actions).toUpperCase(l); } return null; } } }