Java tutorial
/* Copyright 2014 Peter Siegmund This file is part of Toaster. Foobar 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. Foobar 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 Foobar. If not, see <http://www.gnu.org/licenses/>. */ package org.mars3142.android.toaster.fragment; import android.app.ActionBar; import android.app.Activity; import android.app.ListFragment; import android.app.LoaderManager; import android.content.CursorLoader; import android.content.Loader; import android.content.SharedPreferences; import android.content.res.Configuration; import android.database.Cursor; import android.os.Bundle; import android.preference.PreferenceManager; import android.support.v4.app.ActionBarDrawerToggle; import android.support.v4.view.GravityCompat; import android.support.v4.widget.DrawerLayout; import android.view.LayoutInflater; import android.view.Menu; import android.view.MenuInflater; import android.view.MenuItem; import android.view.View; import android.view.ViewGroup; import android.widget.ListView; import org.mars3142.android.toaster.R; import org.mars3142.android.toaster.adapter.ToastArrayAdapter; import org.mars3142.android.toaster.card.ToastCard; import org.mars3142.android.toaster.comparator.ToastCardComparator; import org.mars3142.android.toaster.table.ToasterTable; import java.util.ArrayList; import java.util.Collections; public class NavigationDrawerFragment extends ListFragment implements LoaderManager.LoaderCallbacks<Cursor> { private final static String TAG = NavigationDrawerFragment.class.getSimpleName(); private final static String STATE_SELECTED_POSITION = "selected_navigation_drawer_position"; private final static String PREF_USER_LEARNED_DRAWER = "navigation_drawer_learned"; private static final int DATA_LOADER = 0; private NavigationDrawerCallbacks mCallbacks; private ActionBarDrawerToggle mDrawerToggle; private DrawerLayout mDrawerLayout; private ListView mDrawerListView; private View mFragmentContainerView; private ArrayList<ToastCard> mNavList; private int mCurrentSelectedPosition = 0; private boolean mFromSavedInstanceState; private boolean mUserLearnedDrawer; public NavigationDrawerFragment() { } @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(getActivity()); mUserLearnedDrawer = sp.getBoolean(PREF_USER_LEARNED_DRAWER, false); if (savedInstanceState != null) { mCurrentSelectedPosition = savedInstanceState.getInt(STATE_SELECTED_POSITION); mFromSavedInstanceState = true; } } @Override public void onActivityCreated(Bundle savedInstanceState) { super.onActivityCreated(savedInstanceState); setHasOptionsMenu(true); } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { mDrawerListView = (ListView) inflater.inflate(R.layout.navigation_drawer, container, false); getLoaderManager().restartLoader(DATA_LOADER, null, NavigationDrawerFragment.this); return mDrawerListView; } @Override public void onListItemClick(ListView l, View v, int position, long id) { selectItem(position); } @Override public void onViewCreated(View view, Bundle savedInstanceState) { super.onViewCreated(view, savedInstanceState); selectItem(mCurrentSelectedPosition); } @Override public void onAttach(Activity activity) { super.onAttach(activity); try { mCallbacks = (NavigationDrawerCallbacks) activity; } catch (ClassCastException e) { throw new ClassCastException("Activity must implement NavigationDrawerCallbacks."); } } @Override public void onDetach() { super.onDetach(); mCallbacks = null; } @Override public void onSaveInstanceState(Bundle outState) { super.onSaveInstanceState(outState); outState.putInt(STATE_SELECTED_POSITION, mCurrentSelectedPosition); } @Override public void onConfigurationChanged(Configuration newConfig) { super.onConfigurationChanged(newConfig); mDrawerToggle.onConfigurationChanged(newConfig); } @Override public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) { if (mDrawerLayout != null && isDrawerOpen()) { showGlobalContextActionBar(); } super.onCreateOptionsMenu(menu, inflater); } @Override public boolean onOptionsItemSelected(MenuItem item) { return mDrawerToggle.onOptionsItemSelected(item) || super.onOptionsItemSelected(item); } @Override public Loader<Cursor> onCreateLoader(int loaderId, Bundle args) { switch (loaderId) { case DATA_LOADER: return new CursorLoader(getActivity(), ToasterTable.PACKAGE_URI, null, null, null, null); default: throw new IllegalArgumentException(); } } @Override public void onLoadFinished(Loader<Cursor> loader, Cursor data) { switch (loader.getId()) { case DATA_LOADER: mNavList = new ArrayList<ToastCard>(); ToastCard emptyCard = new ToastCard(getActionBar().getThemedContext()); emptyCard.appName = getString(R.string.all_data); mNavList.add(emptyCard); if (data.moveToFirst()) { do { ToastCard packageCard = new ToastCard(getActionBar().getThemedContext()); packageCard.packageName = data.getString(data.getColumnIndex(ToasterTable.PACKAGE)); if (packageCard.packageName != null) { packageCard.loadData(); mNavList.add(packageCard); } } while (data.moveToNext()); } Collections.sort(mNavList, new ToastCardComparator()); Collections.swap(mNavList, mNavList.indexOf(emptyCard), 0); mDrawerListView.setAdapter(new ToastArrayAdapter(getActionBar().getThemedContext(), mNavList)); mDrawerListView.setItemChecked(mCurrentSelectedPosition, true); break; default: throw new IllegalArgumentException(); } } @Override public void onLoaderReset(Loader<Cursor> loader) { switch (loader.getId()) { case DATA_LOADER: mDrawerListView.setAdapter(null); break; default: throw new IllegalArgumentException(); } } private void showGlobalContextActionBar() { ActionBar actionBar = getActionBar(); actionBar.setDisplayShowTitleEnabled(true); actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_STANDARD); actionBar.setTitle(R.string.app_name); } private ActionBar getActionBar() { return getActivity().getActionBar(); } public boolean isDrawerOpen() { return mDrawerLayout != null && mDrawerLayout.isDrawerOpen(mFragmentContainerView); } public void setUp(int fragmentId, DrawerLayout drawerLayout) { mFragmentContainerView = getActivity().findViewById(fragmentId); mDrawerLayout = drawerLayout; mDrawerLayout.setDrawerShadow(R.drawable.drawer_shadow, GravityCompat.START); ActionBar actionBar = getActionBar(); actionBar.setDisplayHomeAsUpEnabled(true); actionBar.setHomeButtonEnabled(true); mDrawerToggle = new ActionBarDrawerToggle(getActivity(), mDrawerLayout, R.drawable.ic_navigation_drawer, R.string.navigation_drawer_open, R.string.navigation_drawer_close) { @Override public void onDrawerClosed(View drawerView) { super.onDrawerClosed(drawerView); if (!isAdded()) { return; } getLoaderManager().restartLoader(DATA_LOADER, null, NavigationDrawerFragment.this); getActivity().invalidateOptionsMenu(); } @Override public void onDrawerOpened(View drawerView) { super.onDrawerOpened(drawerView); if (!isAdded()) { return; } if (!mUserLearnedDrawer) { mUserLearnedDrawer = true; SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(getActivity()); sp.edit().putBoolean(PREF_USER_LEARNED_DRAWER, true).apply(); } getLoaderManager().restartLoader(DATA_LOADER, null, NavigationDrawerFragment.this); getActivity().invalidateOptionsMenu(); } }; if (!mUserLearnedDrawer && !mFromSavedInstanceState) { mDrawerLayout.openDrawer(mFragmentContainerView); } mDrawerLayout.post(new Runnable() { @Override public void run() { mDrawerToggle.syncState(); } }); mDrawerLayout.setDrawerListener(mDrawerToggle); } private void selectItem(int position) { if (mDrawerListView != null) { mDrawerListView.setItemChecked(position, true); } if (mDrawerLayout != null) { mDrawerLayout.closeDrawer(mFragmentContainerView); } mCurrentSelectedPosition = position; if (mCallbacks != null && mNavList != null && position < mNavList.size()) { mCallbacks.onNavigationDrawerItemSelected(mNavList.get(position).packageName); } } public static interface NavigationDrawerCallbacks { void onNavigationDrawerItemSelected(String packageName); } }