Java tutorial
/* * GNU GENERAL PUBLIC LICENSE * Version 3, 29 June 2007 * * Copyright (c) 2015 Joaquim Ley <me@joaquimley.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 2 * 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, write to the Free Software * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ package com.joaquimley.byinvitationonly.ui; import android.app.Activity; import android.app.Fragment; import android.content.Context; import android.content.SharedPreferences; import android.content.res.Configuration; import android.os.Bundle; import android.support.annotation.Nullable; import android.support.v4.widget.DrawerLayout; import android.support.v7.app.ActionBarActivity; import android.support.v7.app.ActionBarDrawerToggle; import android.support.v7.widget.LinearLayoutManager; import android.support.v7.widget.RecyclerView; import android.support.v7.widget.Toolbar; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.ImageView; import android.widget.TextView; import com.joaquimley.byinvitationonly.R; import java.util.ArrayList; import java.util.List; /** * Fragment which contains the navigation drawer */ public class NavigationDrawerFragment extends Fragment implements NavigationDrawerCallbacks { private static final String PREF_USER_LEARNED_DRAWER = "navigation_drawer_learned"; private static final String STATE_SELECTED_POSITION = "selected_navigation_drawer_position"; private static final String PREFERENCES_FILE = "bio_app_settings"; private NavigationDrawerCallbacks mCallbacks; private RecyclerView mDrawerList; private View mFragmentContainerView; private DrawerLayout mDrawerLayout; private ActionBarDrawerToggle mActionBarDrawerToggle; private boolean mUserLearnedDrawer; private boolean mFromSavedInstanceState; private int mCurrentSelectedPosition; private ImageView mUserAvatar; private TextView mUserName; private TextView mUserEmail; @Nullable @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View view = inflater.inflate(R.layout.fragment_navigation_google, container, false); mDrawerList = (RecyclerView) view.findViewById(R.id.drawerList); LinearLayoutManager layoutManager = new LinearLayoutManager(getActivity()); layoutManager.setOrientation(LinearLayoutManager.VERTICAL); mDrawerList.setLayoutManager(layoutManager); mDrawerList.setHasFixedSize(true); final List<NavigationItem> navigationItems = getMenu(); NavigationDrawerAdapter adapter = new NavigationDrawerAdapter(navigationItems); adapter.setNavigationDrawerCallbacks(this); mDrawerList.setAdapter(adapter); selectItem(mCurrentSelectedPosition); mUserAvatar = (ImageView) view.findViewById(R.id.imgAvatar); mUserName = (TextView) view.findViewById(R.id.txtUsername); mUserEmail = (TextView) view.findViewById(R.id.txtUserEmail); return view; } @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); mUserLearnedDrawer = Boolean.valueOf(readSharedSetting(getActivity(), PREF_USER_LEARNED_DRAWER, "false")); if (savedInstanceState != null) { mCurrentSelectedPosition = savedInstanceState.getInt(STATE_SELECTED_POSITION); mFromSavedInstanceState = true; } } @Override public void onAttach(Activity activity) { super.onAttach(activity); try { mCallbacks = (NavigationDrawerCallbacks) activity; } catch (ClassCastException e) { throw new ClassCastException("Activity must implement NavigationDrawerCallbacks."); } } public ActionBarDrawerToggle getActionBarDrawerToggle() { return mActionBarDrawerToggle; } public void setActionBarDrawerToggle(ActionBarDrawerToggle actionBarDrawerToggle) { mActionBarDrawerToggle = actionBarDrawerToggle; } public void setup(int fragmentId, DrawerLayout drawerLayout, Toolbar toolbar) { mFragmentContainerView = getActivity().findViewById(fragmentId); if (mFragmentContainerView.getParent() instanceof ScrimInsetsFrameLayout) { mFragmentContainerView = (View) mFragmentContainerView.getParent(); } mDrawerLayout = drawerLayout; mDrawerLayout.setStatusBarBackgroundColor(getResources().getColor(R.color.myPrimaryDarkColor)); mActionBarDrawerToggle = new ActionBarDrawerToggle(getActivity(), mDrawerLayout, toolbar, R.string.drawer_open, R.string.drawer_close) { @Override public void onDrawerClosed(View drawerView) { super.onDrawerClosed(drawerView); if (!isAdded()) return; getActivity().invalidateOptionsMenu(); } @Override public void onDrawerOpened(View drawerView) { super.onDrawerOpened(drawerView); if (!isAdded()) return; if (!mUserLearnedDrawer) { mUserLearnedDrawer = true; saveSharedSetting(getActivity(), PREF_USER_LEARNED_DRAWER, "true"); } getActivity().invalidateOptionsMenu(); } }; if (!mUserLearnedDrawer && !mFromSavedInstanceState) mDrawerLayout.openDrawer(mFragmentContainerView); mDrawerLayout.post(new Runnable() { @Override public void run() { mActionBarDrawerToggle.syncState(); } }); mDrawerLayout.setDrawerListener(mActionBarDrawerToggle); } public void openDrawer() { mDrawerLayout.openDrawer(mFragmentContainerView); } public void closeDrawer() { mDrawerLayout.closeDrawer(mFragmentContainerView); } @Override public void onDetach() { super.onDetach(); mCallbacks = null; } public List<NavigationItem> getMenu() { List<NavigationItem> items = new ArrayList<>(); items.add(new NavigationItem(getString(R.string.text_home), getResources().getDrawable(R.drawable.ic_action_home))); items.add(new NavigationItem(getString(R.string.text_sessions), getResources().getDrawable(R.drawable.ic_keynote))); items.add(new NavigationItem(getString(R.string.text_participants_list), getResources().getDrawable(R.drawable.ic_action_group))); items.add(new NavigationItem(getString(R.string.text_bookmark), getResources().getDrawable(R.drawable.ic_favorite_checkbox_default))); return items; } /** * Changes the icon of the drawer to back */ public void showBackButton() { if (getActivity() instanceof ActionBarActivity) { ((ActionBarActivity) getActivity()).getSupportActionBar().setDisplayHomeAsUpEnabled(true); } } /** * Changes the icon of the drawer to menu */ public void showDrawerButton() { if (getActivity() instanceof ActionBarActivity) { ((ActionBarActivity) getActivity()).getSupportActionBar().setDisplayHomeAsUpEnabled(false); } mActionBarDrawerToggle.syncState(); } void selectItem(int position) { mCurrentSelectedPosition = position; if (mDrawerLayout != null) { mDrawerLayout.closeDrawer(mFragmentContainerView); } if (mCallbacks != null) { mCallbacks.onNavigationDrawerItemSelected(position); } ((NavigationDrawerAdapter) mDrawerList.getAdapter()).selectPosition(position); } public boolean isDrawerOpen() { return mDrawerLayout != null && mDrawerLayout.isDrawerOpen(mFragmentContainerView); } @Override public void onConfigurationChanged(Configuration newConfig) { super.onConfigurationChanged(newConfig); mActionBarDrawerToggle.onConfigurationChanged(newConfig); } @Override public void onSaveInstanceState(Bundle outState) { super.onSaveInstanceState(outState); outState.putInt(STATE_SELECTED_POSITION, mCurrentSelectedPosition); } @Override public void onNavigationDrawerItemSelected(int position) { selectItem(position); } public DrawerLayout getDrawerLayout() { return mDrawerLayout; } public void setDrawerLayout(DrawerLayout drawerLayout) { mDrawerLayout = drawerLayout; } public static void saveSharedSetting(Context ctx, String settingName, String settingValue) { SharedPreferences sharedPref = ctx.getSharedPreferences(PREFERENCES_FILE, Context.MODE_PRIVATE); SharedPreferences.Editor editor = sharedPref.edit(); editor.putString(settingName, settingValue); editor.apply(); } public static String readSharedSetting(Context ctx, String settingName, String defaultValue) { SharedPreferences sharedPref = ctx.getSharedPreferences(PREFERENCES_FILE, Context.MODE_PRIVATE); return sharedPref.getString(settingName, defaultValue); } public ImageView getUserAvatar() { return mUserAvatar; } public void setUserAvatar(ImageView userAvatar) { mUserAvatar = userAvatar; } public TextView getUserName() { return mUserName; } public void setUserName(TextView userName) { mUserName = userName; } public TextView getUserEmail() { return mUserEmail; } public void setUserEmail(TextView userEmail) { mUserEmail = userEmail; } }