Java tutorial
/**************************************************************************************** * Copyright (c) 2014 Timothy Rae <perceptualchaos2@gmail.com> * * * * This program 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 program 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 * * this program. If not, see <http://www.gnu.org/licenses/>. * ****************************************************************************************/ package website.openeng.anki; import android.content.Intent; import android.content.SharedPreferences; import android.content.res.Configuration; import android.net.Uri; import android.os.Bundle; import android.support.design.widget.NavigationView; import android.support.v4.view.GravityCompat; import android.support.v4.widget.DrawerLayout; import android.support.v7.app.ActionBarDrawerToggle; import android.support.v7.widget.Toolbar; import android.view.Menu; import android.view.MenuItem; import android.view.View; import website.openeng.anim.ActivityTransitionAnimation; import website.openeng.compat.CompatHelper; import timber.log.Timber; public class NavigationDrawerActivity extends AnkiActivity implements NavigationView.OnNavigationItemSelectedListener { /** Navigation Drawer */ protected CharSequence mTitle; protected Boolean mFragmented = false; // Preselection for DeckDropDownAdapter protected static boolean sIsWholeCollection = true; private DrawerLayout mDrawerLayout; private NavigationView mNavigationView; private ActionBarDrawerToggle mDrawerToggle; // Other members private String mOldColPath; // Intent request codes public static final int REQUEST_PREFERENCES_UPDATE = 100; public static final int REQUEST_BROWSE_CARDS = 101; public static final int REQUEST_STATISTICS = 102; // Navigation drawer initialisation protected void initNavigationDrawer(View mainView) { // Create inherited navigation drawer layout here so that it can be used by parent class mDrawerLayout = (DrawerLayout) mainView.findViewById(R.id.drawer_layout); // set a custom shadow that overlays the main content when the drawer opens mDrawerLayout.setDrawerShadow(R.drawable.drawer_shadow, GravityCompat.START); mNavigationView = (NavigationView) mDrawerLayout.findViewById(R.id.navdrawer_items_container); mNavigationView.setNavigationItemSelectedListener(this); Toolbar toolbar = (Toolbar) mainView.findViewById(R.id.toolbar); if (toolbar != null) { setSupportActionBar(toolbar); } // enable ActionBar app icon to behave as action to toggle nav drawer getSupportActionBar().setDisplayHomeAsUpEnabled(true); getSupportActionBar().setHomeButtonEnabled(true); // ActionBarDrawerToggle ties together the the proper interactions // between the sliding drawer and the action bar app icon mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout, 0, 0) { @Override public void onDrawerClosed(View drawerView) { super.onDrawerClosed(drawerView); supportInvalidateOptionsMenu(); } @Override public void onDrawerOpened(View drawerView) { super.onDrawerOpened(drawerView); setNightModeIcon(); supportInvalidateOptionsMenu(); } }; mDrawerLayout.setDrawerListener(mDrawerToggle); } private void setNightModeIcon() { SharedPreferences preferences = KanjiDroidApp.getSharedPrefs(NavigationDrawerActivity.this); if (preferences.getBoolean("invertedColors", false)) { mNavigationView.getMenu().findItem(R.id.nav_night_mode).setIcon(R.drawable.ic_check_box_black_24dp); } else { mNavigationView.getMenu().findItem(R.id.nav_night_mode) .setIcon(R.drawable.ic_check_box_outline_blank_black_24dp); } } /** Sets selected navigation drawer item */ protected void selectNavigationItem(int itemId) { Menu menu = mNavigationView.getMenu(); MenuItem item = menu.findItem(itemId); if (item != null) { item.setChecked(true); } else { Timber.e("Could not find item %d", itemId); } } public boolean onNavigationItemSelected(MenuItem item) { // Don't do anything if user selects already selected position if (item.isChecked()) { return true; } // Take action if a different item selected switch (item.getItemId()) { case R.id.nav_decks: Intent deckPicker = new Intent(this, DeckPicker.class); deckPicker.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK); // opening DeckPicker should clear back history startActivityWithAnimation(deckPicker, ActivityTransitionAnimation.RIGHT); break; case R.id.nav_browser: Intent cardBrowser = new Intent(this, CardBrowser.class); cardBrowser.putExtra("selectedDeck", getCol().getDecks().selected()); startActivityForResultWithAnimation(cardBrowser, REQUEST_BROWSE_CARDS, ActivityTransitionAnimation.LEFT); break; case R.id.nav_stats: Intent intent = new Intent(this, Statistics.class); intent.putExtra("selectedDeck", getCol().getDecks().selected()); startActivityForResultWithAnimation(intent, REQUEST_STATISTICS, ActivityTransitionAnimation.LEFT); break; case R.id.nav_night_mode: SharedPreferences preferences = KanjiDroidApp.getSharedPrefs(this); if (preferences.getBoolean("invertedColors", false)) { Timber.i("StudyOptionsFragment:: Night mode was disabled"); preferences.edit().putBoolean("invertedColors", false).commit(); } else { Timber.i("StudyOptionsFragment:: Night mode was enabled"); preferences.edit().putBoolean("invertedColors", true).commit(); } setNightModeIcon(); CompatHelper.getCompat().restartActivityInvalidateBackstack(this); return true; case R.id.nav_settings: mOldColPath = CollectionHelper.getCurrentKanjiDroidDirectory(this); startActivityForResultWithAnimation(new Intent(this, Preferences.class), REQUEST_PREFERENCES_UPDATE, ActivityTransitionAnimation.FADE); break; case R.id.nav_help: Intent helpIntent = new Intent("android.intent.action.VIEW", Uri.parse(KanjiDroidApp.getManualUrl())); startActivityWithoutAnimation(helpIntent); break; case R.id.nav_feedback: Intent feedbackIntent = new Intent("android.intent.action.VIEW", Uri.parse(KanjiDroidApp.getFeedbackUrl())); startActivityWithoutAnimation(feedbackIntent); break; default: return false; } mDrawerLayout.closeDrawers(); return true; } @Override public void setTitle(CharSequence title) { mTitle = title; getSupportActionBar().setTitle(mTitle); } /** * When using the ActionBarDrawerToggle, you must call it during * onPostCreate() and onConfigurationChanged()... */ @Override protected void onPostCreate(Bundle savedInstanceState) { super.onPostCreate(savedInstanceState); // Sync the toggle state after onRestoreInstanceState has occurred. mDrawerToggle.syncState(); } @Override public void onConfigurationChanged(Configuration newConfig) { super.onConfigurationChanged(newConfig); // Pass any configuration change to the drawer toggles mDrawerToggle.onConfigurationChanged(newConfig); } public ActionBarDrawerToggle getDrawerToggle() { return mDrawerToggle; } /** * This function locks the navigation drawer closed in regards to swipes, * but continues to allowed it to be opened via it's indicator button. This * function in a noop if the drawer hasn't been initialized. */ protected void disableDrawerSwipe() { if (mDrawerLayout != null) { mDrawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_CLOSED); } } /** * This function allows swipes to open the navigation drawer. This * function in a noop if the drawer hasn't been initialized. */ protected void enableDrawerSwipe() { if (mDrawerLayout != null) { mDrawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_UNLOCKED); } } @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { SharedPreferences preferences = KanjiDroidApp.getSharedPrefs(this); // Update language KanjiDroidApp.setLanguage(preferences.getString(Preferences.LANGUAGE, "")); // Restart the activity on preference change if (requestCode == REQUEST_PREFERENCES_UPDATE) { if (mOldColPath != null && CollectionHelper.getCurrentKanjiDroidDirectory(this).equals(mOldColPath)) { // collection path hasn't been changed so just restart the current activity if ((this instanceof Reviewer) && preferences.getBoolean("tts", false)) { // Workaround to kick user back to StudyOptions after opening settings from Reviewer // because onDestroy() of old Activity interferes with TTS in new Activity finishWithoutAnimation(); } else { restartActivity(); } } else { // collection path has changed so kick the user back to the DeckPicker CollectionHelper.getInstance().closeCollection(true); CompatHelper.getCompat().restartActivityInvalidateBackstack(this); } } else { super.onActivityResult(requestCode, resultCode, data); } } public static void setIsWholeCollection(boolean isWholeCollection) { sIsWholeCollection = isWholeCollection; } public static boolean isWholeCollection() { return sIsWholeCollection; } /** * Get the drawer layout. * * The drawer layout is the parent layout for activities that use the Navigation Drawer. */ public DrawerLayout getDrawerLayout() { return mDrawerLayout; } }