Mholloway has uploaded a new change for review. https://gerrit.wikimedia.org/r/226721
Change subject: Use NavigationView from android.support.design library for nav drawer ...................................................................... Use NavigationView from android.support.design library for nav drawer Bug: T101723 Change-Id: I4bd1b138065bfd0be7f35852881d374393013cb5 --- A wikipedia/res/color/nav_item_state_list M wikipedia/res/layout/activity_page.xml D wikipedia/res/layout/fragment_navdrawer.xml A wikipedia/res/layout/inflate_header_nav_drawer.xml A wikipedia/res/menu/menu_nav_drawer.xml D wikipedia/src/main/java/org/wikipedia/NavDrawerFragment.java M wikipedia/src/main/java/org/wikipedia/page/PageActivity.java M wikipedia/src/main/java/org/wikipedia/random/RandomHandler.java 8 files changed, 286 insertions(+), 562 deletions(-) git pull ssh://gerrit.wikimedia.org:29418/apps/android/wikipedia refs/changes/21/226721/1 diff --git a/wikipedia/res/color/nav_item_state_list b/wikipedia/res/color/nav_item_state_list new file mode 100644 index 0000000..532adff --- /dev/null +++ b/wikipedia/res/color/nav_item_state_list @@ -0,0 +1,8 @@ +<?xml version="1.0" encoding="utf-8"?> + +<selector xmlns:android="http://schemas.android.com/apk/res/android"> + <!-- This is used when the Navigation Item is checked --> + <item android:color="@color/nav_item_highlight" android:state_checked="true" /> + <!-- This is the default text color --> + <item android:color="@color/nav_background" /> +</selector> \ No newline at end of file diff --git a/wikipedia/res/layout/activity_page.xml b/wikipedia/res/layout/activity_page.xml index 303b41f..73a57c5 100644 --- a/wikipedia/res/layout/activity_page.xml +++ b/wikipedia/res/layout/activity_page.xml @@ -160,10 +160,15 @@ </FrameLayout> <!-- The navigation drawer --> - <fragment android:layout_width="288dp" android:layout_height="match_parent" + <android.support.design.widget.NavigationView android:id="@+id/navdrawer" - android:name="org.wikipedia.NavDrawerFragment" + android:layout_width="wrap_content" + android:layout_height="match_parent" android:layout_gravity="start" - tools:layout="@layout/fragment_navdrawer"/> + android:background="@color/nav_background" + app:menu="@menu/menu_nav_drawer" + app:itemBackground="@color/nav_item_state_list" + app:itemTextColor="@color/nav_text" + app:itemIconTint="@color/nav_text" /> </org.wikipedia.views.WikiDrawerLayout> diff --git a/wikipedia/res/layout/fragment_navdrawer.xml b/wikipedia/res/layout/fragment_navdrawer.xml deleted file mode 100644 index 98d6a76..0000000 --- a/wikipedia/res/layout/fragment_navdrawer.xml +++ /dev/null @@ -1,262 +0,0 @@ -<?xml version="1.0" encoding="utf-8"?> - -<ScrollView xmlns:android="http://schemas.android.com/apk/res/android" - xmlns:tools="http://schemas.android.com/tools" - android:layout_width="match_parent" - android:layout_height="match_parent" - android:fillViewport="true"> - - <LinearLayout android:layout_width="match_parent" - android:layout_height="wrap_content" - android:orientation="horizontal" - > - - <LinearLayout android:id="@+id/nav_item_container" - android:layout_width="0dp" - android:layout_height="wrap_content" - android:orientation="vertical" - android:paddingLeft="4dp" - android:paddingRight="4dp" - android:paddingBottom="16dp" - android:layout_weight="1" - android:background="@color/nav_background" - > - - <ImageView - android:layout_width="150dp" - android:layout_height="wrap_content" - android:layout_marginLeft="12dp" - android:layout_marginRight="12dp" - android:layout_marginTop="42dp" - android:layout_marginBottom="34dp" - android:src="@drawable/nav_drawer_wordmark"/> - - <LinearLayout android:id="@+id/nav_item_login" - android:layout_width="match_parent" - android:layout_height="wrap_content" - android:orientation="horizontal" - android:background="@drawable/nav_item_background" - > - <ImageView android:layout_width="24dp" android:layout_height="24dp" - android:layout_margin="12dp" - android:padding="2dp" - android:layout_gravity="top" - android:src="@drawable/face_sleep_white" - android:contentDescription="@null" - /> - <LinearLayout android:layout_width="match_parent" - android:layout_height="wrap_content" - android:orientation="vertical" - > - <TextView - android:layout_width="match_parent" - android:layout_height="48dp" - android:gravity="center_vertical" - android:layout_gravity="center_vertical" - android:text="@string/nav_item_login" - android:textColor="@color/nav_text" - style="?android:textAppearanceMedium" - /> - </LinearLayout> - </LinearLayout> - - <LinearLayout android:id="@+id/nav_item_username" - android:layout_width="match_parent" - android:layout_height="wrap_content" - android:orientation="horizontal" - android:paddingTop="8dp" - android:paddingBottom="8dp" - android:visibility="gone" - tools:visibility="visible" - > - <ImageView android:layout_width="24dp" android:layout_height="24dp" - android:layout_margin="12dp" - android:padding="2dp" - android:layout_gravity="top" - android:src="@drawable/face_smile_white" - android:contentDescription="@null" - /> - <LinearLayout android:layout_width="match_parent" - android:layout_height="wrap_content" - android:orientation="vertical" - > - <TextView - android:id="@+id/nav_item_username_primary_text" - android:layout_width="wrap_content" - android:layout_height="48dp" - android:gravity="center_vertical" - android:textColor="@color/nav_text" - tools:text="Sample user name" - style="?android:textAppearanceMedium" - /> - </LinearLayout> - </LinearLayout> - - <LinearLayout android:id="@+id/nav_item_today" - android:layout_width="match_parent" - android:layout_height="wrap_content" - android:orientation="horizontal" - android:background="@drawable/nav_item_background" - > - <ImageView android:layout_width="32dp" android:layout_height="32dp" - android:layout_margin="8dp" - android:padding="2dp" - android:layout_gravity="center_vertical" - android:src="@drawable/ic_today" - android:contentDescription="@null" - /> - <TextView - android:layout_width="match_parent" - android:layout_height="wrap_content" - android:layout_gravity="center_vertical" - android:text="@string/nav_item_today" - android:textColor="@color/nav_text" - style="?android:textAppearanceMedium" - /> - </LinearLayout> - - <LinearLayout android:id="@+id/nav_item_history" - android:layout_width="match_parent" - android:layout_height="wrap_content" - android:orientation="horizontal" - android:background="@drawable/nav_item_background" - > - <ImageView android:layout_width="24dp" android:layout_height="24dp" - android:layout_margin="12dp" - android:padding="2dp" - android:layout_gravity="center_vertical" - android:src="@drawable/clock_white" - android:contentDescription="@null" - /> - <TextView - android:layout_width="match_parent" - android:layout_height="wrap_content" - android:layout_gravity="center_vertical" - android:text="@string/nav_item_history" - android:textColor="@color/nav_text" - style="?android:textAppearanceMedium" - /> - </LinearLayout> - - <LinearLayout android:id="@+id/nav_item_saved_pages" - android:layout_width="match_parent" - android:layout_height="wrap_content" - android:orientation="horizontal" - android:background="@drawable/nav_item_background" - > - <ImageView android:layout_width="24dp" android:layout_height="24dp" - android:layout_margin="12dp" - android:padding="2dp" - android:layout_gravity="center_vertical" - android:src="@drawable/bookmark_white" - android:contentDescription="@null" - /> - <TextView - android:layout_width="match_parent" - android:layout_height="wrap_content" - android:layout_gravity="center_vertical" - android:text="@string/nav_item_saved_pages" - android:textColor="@color/nav_text" - style="?android:textAppearanceMedium" - /> - </LinearLayout> - - <LinearLayout android:id="@+id/nav_item_nearby" - android:layout_width="match_parent" - android:layout_height="wrap_content" - android:orientation="horizontal" - android:background="@drawable/nav_item_background" - > - <ImageView android:layout_width="32dp" android:layout_height="32dp" - android:layout_margin="8dp" - android:padding="2dp" - android:layout_gravity="center_vertical" - android:src="@drawable/ic_place_dark" - android:contentDescription="@null" - /> - <TextView - android:layout_width="match_parent" - android:layout_height="wrap_content" - android:layout_gravity="center_vertical" - android:text="@string/nav_item_nearby" - android:textColor="@color/nav_text" - style="?android:textAppearanceMedium" - /> - </LinearLayout> - - <LinearLayout android:id="@+id/nav_item_random" - android:layout_width="match_parent" - android:layout_height="wrap_content" - android:orientation="horizontal" - android:background="@drawable/nav_item_background" - > - <FrameLayout android:layout_width="24dp" android:layout_height="24dp" - android:layout_margin="12dp" - android:padding="2dp" - android:layout_gravity="center_vertical"> - <ImageView android:id="@+id/nav_item_random_icon" - android:layout_width="match_parent" - android:layout_height="match_parent" - android:src="@drawable/dice_white" - android:contentDescription="@null" /> - <ProgressBar android:id="@+id/nav_item_random_progressbar" - android:layout_width="match_parent" - android:layout_height="match_parent" - /> - </FrameLayout> - <TextView - android:layout_width="match_parent" - android:layout_height="wrap_content" - android:layout_gravity="center_vertical" - android:text="@string/nav_item_random" - android:textColor="@color/nav_text" - style="?android:textAppearanceMedium" - /> - </LinearLayout> - - <TextView - android:id="@+id/nav_item_zero" - android:layout_width="match_parent" - android:layout_height="wrap_content" - style="@style/RtlAwareTextView" - android:layout_gravity="center_vertical" - android:layout_margin="11dp" - android:text="" - tools:text="W0 sample carrier message" - android:textColor="@color/nav_text" - android:textAppearance="?android:attr/textAppearanceSmall" - /> - - <!-- Separator view for providing gap between top items and bottom ones --> - <View android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1" /> - - <LinearLayout android:id="@+id/nav_item_more" - android:layout_width="match_parent" - android:layout_height="wrap_content" - android:orientation="horizontal" - android:background="@drawable/nav_item_background" - > - <ImageView android:layout_width="24dp" - android:layout_height="24dp" - android:layout_margin="12dp" - android:padding="2dp" - android:layout_gravity="center_vertical" - android:src="@drawable/gear_white" - android:contentDescription="@null" - /> - <TextView - android:layout_width="match_parent" - android:layout_height="wrap_content" - android:layout_gravity="center_vertical" - android:text="@string/more_item_preferences" - android:textColor="@color/nav_text" - style="?android:textAppearanceMedium" - /> - </LinearLayout> - - </LinearLayout> - - <View android:layout_width="4dp" android:layout_height="match_parent" android:background="@drawable/nav_drawer_shadow" /> - - </LinearLayout> -</ScrollView> diff --git a/wikipedia/res/layout/inflate_header_nav_drawer.xml b/wikipedia/res/layout/inflate_header_nav_drawer.xml new file mode 100644 index 0000000..71e742e --- /dev/null +++ b/wikipedia/res/layout/inflate_header_nav_drawer.xml @@ -0,0 +1,17 @@ +<?xml version="1.0" encoding="utf-8"?> + +<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" + android:id="@+id/nav_bar_header" + android:layout_width="0dp" + android:layout_height="wrap_content" + android:orientation="vertical" + android:layout_weight="1"> + <ImageView + android:layout_width="175dp" + android:layout_height="wrap_content" + android:paddingLeft="12dp" + android:paddingRight="12dp" + android:paddingTop="42dp" + android:paddingBottom="24dp" + android:src="@drawable/nav_drawer_wordmark" /> +</LinearLayout> \ No newline at end of file diff --git a/wikipedia/res/menu/menu_nav_drawer.xml b/wikipedia/res/menu/menu_nav_drawer.xml new file mode 100644 index 0000000..e304109 --- /dev/null +++ b/wikipedia/res/menu/menu_nav_drawer.xml @@ -0,0 +1,41 @@ +<?xml version="1.0" encoding="utf-8"?> + +<menu xmlns:android="http://schemas.android.com/apk/res/android" + android:id="@+id/nav_menu"> + <item android:id="@+id/nav_item_login" + android:title="@string/nav_item_login" + android:icon="@drawable/face_sleep_white" + /> + <item android:id="@+id/nav_item_username" + android:icon="@drawable/face_smile_white" + android:title="Sample user name" + /> + <item android:id="@+id/nav_item_today" + android:title="@string/nav_item_today" + android:icon="@drawable/ic_today" + /> + <item android:id="@+id/nav_item_history" + android:title="@string/nav_item_history" + android:icon="@drawable/clock_white" + /> + <item android:id="@+id/nav_item_saved_pages" + android:title="@string/nav_item_saved_pages" + android:icon="@drawable/bookmark_white" + /> + <item android:id="@+id/nav_item_nearby" + android:title="@string/nav_item_nearby" + android:icon="@drawable/ic_place_dark" + /> + <item android:id="@+id/nav_item_random" + android:title="@string/nav_item_random" + android:icon="@drawable/dice_white" + /> + <item android:id="@+id/nav_item_zero" + android:title="W0 sample carrier message" + /> + <group android:id="@+id/group_more"> + <item android:id="@+id/nav_item_more" + android:title="@string/more_item_preferences" + android:icon="@drawable/gear_white" /> + </group> +</menu> \ No newline at end of file diff --git a/wikipedia/src/main/java/org/wikipedia/NavDrawerFragment.java b/wikipedia/src/main/java/org/wikipedia/NavDrawerFragment.java deleted file mode 100644 index 0e725ff..0000000 --- a/wikipedia/src/main/java/org/wikipedia/NavDrawerFragment.java +++ /dev/null @@ -1,257 +0,0 @@ -package org.wikipedia; - -import android.appwidget.AppWidgetManager; -import android.content.ComponentName; -import android.content.Intent; -import android.os.Bundle; -import android.os.Handler; -import android.os.Looper; -import android.support.annotation.Nullable; -import android.support.v4.app.Fragment; -import android.text.format.DateUtils; -import android.view.LayoutInflater; -import android.view.View; -import android.view.ViewGroup; -import android.widget.TextView; -import android.widget.Toast; -import org.wikipedia.analytics.LoginFunnel; -import org.wikipedia.history.HistoryEntry; -import org.wikipedia.history.HistoryFragment; -import org.wikipedia.login.LoginActivity; -import org.wikipedia.nearby.NearbyFragment; -import org.wikipedia.page.PageActivity; -import org.wikipedia.page.PageTitle; -import org.wikipedia.page.PageViewFragmentInternal; -import org.wikipedia.random.RandomHandler; -import org.wikipedia.savedpages.SavedPagesFragment; -import org.wikipedia.settings.SettingsActivity; -import org.wikipedia.settings.SettingsActivityGB; -import org.wikipedia.util.ApiUtil; -import org.wikipedia.widgets.WidgetProviderFeaturedPage; - -public class NavDrawerFragment extends Fragment implements View.OnClickListener { - private static final int[] ACTION_ITEMS_ALL = { - R.id.nav_item_today, - R.id.nav_item_history, - R.id.nav_item_saved_pages, - R.id.nav_item_nearby, - R.id.nav_item_more, - R.id.nav_item_login, - R.id.nav_item_random - // We don't actually need R.id.nav_item_zero here because we add it programmatically - // below, and we don't need an on-tap as of now - }; - - private static final int[] ACTION_ITEMS_LOGGED_IN_ONLY = { - R.id.nav_item_username - }; - - private View[] actionViews = new View[ACTION_ITEMS_ALL.length]; - private View[] loggedInOnyActionViews = new View[ACTION_ITEMS_LOGGED_IN_ONLY.length]; - private WikipediaApp app; - private RandomHandler randomHandler; - - @Override - public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { - return inflater.inflate(R.layout.fragment_navdrawer, container, false); - } - - @Override - public void onResume() { - super.onResume(); - - // Ensure that Login / Logout and Wikipedia Zero status is accurate - setupDynamicItems(); - } - - private TextView wikipediaZeroText; - @Override - public void onActivityCreated(Bundle savedInstanceState) { - super.onActivityCreated(savedInstanceState); - app = (WikipediaApp)getActivity().getApplicationContext(); - - for (int i = 0; i < ACTION_ITEMS_ALL.length; i++) { - actionViews[i] = getView().findViewById(ACTION_ITEMS_ALL[i]); - actionViews[i].setOnClickListener(this); - } - - for (int i = 0; i < ACTION_ITEMS_LOGGED_IN_ONLY.length; i++) { - loggedInOnyActionViews[i] = getView().findViewById(ACTION_ITEMS_LOGGED_IN_ONLY[i]); - } - - wikipediaZeroText = (TextView) getView().findViewById(R.id.nav_item_zero); - - randomHandler = new RandomHandler(getView().findViewById(R.id.nav_item_random), - getView().findViewById(R.id.nav_item_random_icon), - getView().findViewById(R.id.nav_item_random_progressbar), - new RandomHandler.RandomListener() { - @Override - public void onRandomPageReceived(@Nullable PageTitle title) { - if (!isAdded()) { - return; - } - if (title == null) { - - // TODO: show a SnackBar if we fail to fetch a random page. - // (next patch, I promise) - - } else { - HistoryEntry historyEntry = new HistoryEntry(title, HistoryEntry.SOURCE_RANDOM); - ((PageActivity) getActivity()).displayNewPage(title, historyEntry, true); - } - } - }); - } - - private View usernameContainer; - private View loginContainer; - private TextView usernamePrimaryText; - public void setupDynamicItems() { - if (usernameContainer == null) { - usernameContainer = getView().findViewById(R.id.nav_item_username); - usernamePrimaryText = (TextView) usernameContainer.findViewById(R.id.nav_item_username_primary_text); - loginContainer = getView().findViewById(R.id.nav_item_login); - } - // Do login / logout swap - if (app.getUserInfoStorage().isLoggedIn()) { - loginContainer.setVisibility(View.GONE); - for (View loggedInOnyActionView : loggedInOnyActionViews) { - loggedInOnyActionView.setVisibility(View.VISIBLE); - } - usernamePrimaryText.setText(app.getUserInfoStorage().getUser().getUsername()); - } else { - loginContainer.setVisibility(View.VISIBLE); - for (View loggedInOnyActionView : loggedInOnyActionViews) { - loggedInOnyActionView.setVisibility(View.GONE); - } - } - - // Show Wikipedia Zero if ON, otherwise hide it - if (app.getWikipediaZeroHandler().isZeroEnabled()) { - wikipediaZeroText.setText(app.getWikipediaZeroHandler().getCarrierMessage().getMsg()); - wikipediaZeroText.setVisibility(View.VISIBLE); - } else { - wikipediaZeroText.setVisibility(View.GONE); - } - - // highlight the correct list item based on which fragment is on top. - // un-highlight all of them first... - for (int id : ACTION_ITEMS_ALL) { - getView().findViewById(id).setBackgroundResource(R.drawable.nav_item_background); - } - PageActivity activity = (PageActivity)getActivity(); - int highlightItem = -1; - if (activity.getTopFragment() instanceof HistoryFragment) { - highlightItem = R.id.nav_item_history; - } else if (activity.getTopFragment() instanceof SavedPagesFragment) { - highlightItem = R.id.nav_item_saved_pages; - } else if (activity.getTopFragment() instanceof NearbyFragment) { - highlightItem = R.id.nav_item_nearby; - } else if (activity.getTopFragment() instanceof PageViewFragmentInternal) { - PageViewFragmentInternal fragment = (PageViewFragmentInternal)activity.getTopFragment(); - if (fragment.getHistoryEntry() != null) { - if (fragment.getHistoryEntry().getSource() == HistoryEntry.SOURCE_MAIN_PAGE) { - highlightItem = R.id.nav_item_today; - } else if (fragment.getHistoryEntry().getSource() == HistoryEntry.SOURCE_RANDOM) { - highlightItem = R.id.nav_item_random; - } - } - } - if (highlightItem != -1) { - getView().findViewById(highlightItem).setBackgroundColor(getResources().getColor(R.color.nav_item_highlight)); - } - } - - private void doLogout() { - boolean doUpdate = true; - if (app == null) { - // we haven't yet been attached to our Activity, so don't worry about - // updating dynamic items - doUpdate = false; - app = WikipediaApp.getInstance(); - } - app.getEditTokenStorage().clearAllTokens(); - app.getCookieManager().clearAllCookies(); - app.getUserInfoStorage().clearUser(); - Toast.makeText(app, R.string.toast_logout_complete, Toast.LENGTH_LONG).show(); - if (doUpdate) { - setupDynamicItems(); - } - } - - @Override - public void onClick(View view) { - Intent intent = new Intent(); - switch (view.getId()) { - case R.id.nav_item_today: - ((PageActivity)getActivity()).displayMainPage(); - break; - case R.id.nav_item_history: - ((PageActivity)getActivity()).pushFragment(new HistoryFragment()); - break; - case R.id.nav_item_saved_pages: - ((PageActivity)getActivity()).pushFragment(new SavedPagesFragment()); - break; - case R.id.nav_item_nearby: - ((PageActivity)getActivity()).pushFragment(new NearbyFragment()); - break; - case R.id.nav_item_more: - if (ApiUtil.hasHoneyComb()) { - intent.setClass(this.getActivity(), SettingsActivity.class); - } else { - intent.setClass(this.getActivity(), SettingsActivityGB.class); - } - startActivityForResult(intent, SettingsActivity.ACTIVITY_REQUEST_SHOW_SETTINGS); - break; - case R.id.nav_item_login: - intent.setClass(this.getActivity(), LoginActivity.class); - intent.putExtra(LoginActivity.LOGIN_REQUEST_SOURCE, LoginFunnel.SOURCE_NAV); - startActivity(intent); - break; - case R.id.nav_item_random: - randomHandler.doVisitRandomArticle(); - break; - default: - throw new RuntimeException("Unknown ID clicked!"); - } - } - - @Override - public void onActivityResult(int requestCode, int resultCode, Intent data) { - if (requestCode == SettingsActivity.ACTIVITY_REQUEST_SHOW_SETTINGS) { - if (resultCode == SettingsActivity.ACTIVITY_RESULT_LANGUAGE_CHANGED) { - // Run the code a second later, to: - // - Make sure that onStart in PageActivity gets called, thus - // registering the activity for the bus. - // - The 1s delay ensures a smoother transition, otherwise was - // very jarring - Handler uiThread = new Handler(Looper.getMainLooper()); - uiThread.postDelayed(new Runnable() { - @Override - public void run() { - if (!isAdded()) { - return; - } - ((PageActivity)getActivity()).displayMainPage(true); - // and update any instances of our Featured Page widget, since it will - // change with the currently selected language. - Intent widgetIntent = new Intent(getActivity(), WidgetProviderFeaturedPage.class); - widgetIntent.setAction(AppWidgetManager.ACTION_APPWIDGET_UPDATE); - int[] ids = AppWidgetManager.getInstance(getActivity().getApplication()).getAppWidgetIds( - new ComponentName(getActivity().getApplication(), WidgetProviderFeaturedPage.class)); - widgetIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS, ids); - getActivity().sendBroadcast(widgetIntent); - } - }, DateUtils.SECOND_IN_MILLIS); - } else if (resultCode == SettingsActivity.ACTIVITY_RESULT_LOGOUT) { - doLogout(); - } - } - } - - @Override - public void onDetach() { - super.onDetach(); - randomHandler.onStop(); - } -} diff --git a/wikipedia/src/main/java/org/wikipedia/page/PageActivity.java b/wikipedia/src/main/java/org/wikipedia/page/PageActivity.java index a335c41..42b13e1 100644 --- a/wikipedia/src/main/java/org/wikipedia/page/PageActivity.java +++ b/wikipedia/src/main/java/org/wikipedia/page/PageActivity.java @@ -2,7 +2,6 @@ import org.acra.ACRA; import org.wikipedia.BackPressedHandler; -import org.wikipedia.NavDrawerFragment; import org.wikipedia.R; import org.wikipedia.Site; import org.wikipedia.activity.ActivityUtil; @@ -10,16 +9,24 @@ import org.wikipedia.Utils; import org.wikipedia.ViewAnimations; import org.wikipedia.WikipediaApp; +import org.wikipedia.analytics.LoginFunnel; import org.wikipedia.analytics.WidgetsFunnel; import org.wikipedia.events.ChangeTextSizeEvent; import org.wikipedia.events.ThemeChangeEvent; import org.wikipedia.events.WikipediaZeroStateChangeEvent; import org.wikipedia.history.HistoryEntry; +import org.wikipedia.history.HistoryFragment; import org.wikipedia.interlanguage.LangLinksActivity; +import org.wikipedia.login.LoginActivity; +import org.wikipedia.nearby.NearbyFragment; import org.wikipedia.page.gallery.GalleryActivity; +import org.wikipedia.random.RandomHandler; import org.wikipedia.recurring.RecurringTasksExecutor; +import org.wikipedia.savedpages.SavedPagesFragment; import org.wikipedia.search.SearchArticlesFragment; import org.wikipedia.search.SearchBarHideHandler; +import org.wikipedia.settings.SettingsActivity; +import org.wikipedia.settings.SettingsActivityGB; import org.wikipedia.staticdata.MainPageNameData; import org.wikipedia.theme.ThemeChooserDialog; import org.wikipedia.tooltip.ToolTipUtil; @@ -28,6 +35,7 @@ import org.wikipedia.util.log.L; import org.wikipedia.views.ViewUtil; import org.wikipedia.views.WikiDrawerLayout; +import org.wikipedia.widgets.WidgetProviderFeaturedPage; import org.wikipedia.zero.ZeroMessage; import com.squareup.otto.Bus; @@ -36,6 +44,8 @@ import de.keyboardsurfer.android.widget.crouton.Style; import android.app.SearchManager; +import android.appwidget.AppWidgetManager; +import android.content.ComponentName; import android.content.DialogInterface; import android.content.Intent; import android.content.SharedPreferences; @@ -43,9 +53,12 @@ import android.net.Uri; import android.os.BadParcelableException; import android.os.Bundle; +import android.os.Handler; +import android.os.Looper; import android.preference.PreferenceManager; import android.support.annotation.NonNull; import android.support.annotation.Nullable; +import android.support.design.widget.NavigationView; import android.support.v4.app.Fragment; import android.support.v4.app.FragmentTransaction; import android.support.v4.view.GravityCompat; @@ -56,13 +69,18 @@ import android.support.v7.view.ActionMode; import android.text.Html; import android.text.TextUtils; +import android.text.format.DateUtils; import android.util.Log; import android.view.Gravity; import android.view.KeyEvent; +import android.view.LayoutInflater; +import android.view.Menu; import android.view.MenuItem; import android.view.View; +import android.widget.LinearLayout; import android.widget.ProgressBar; import android.widget.TextView; +import android.widget.Toast; public class PageActivity extends ThemedActionBarActivity { public static final String ACTION_PAGE_FOR_TITLE = "org.wikipedia.page_for_title"; @@ -81,37 +99,61 @@ public static final int ACTIVITY_REQUEST_EDIT_SECTION = 1; public static final int ACTIVITY_REQUEST_GALLERY = 2; + public static final int PROGRESS_BAR_MAX_VALUE = 10000; + + private static final int[] NAV_DRAWER_ACTION_ITEMS_ALL = { + R.id.nav_item_today, + R.id.nav_item_history, + R.id.nav_item_saved_pages, + R.id.nav_item_nearby, + R.id.nav_item_more, + R.id.nav_item_login, + R.id.nav_item_random + // We don't actually need R.id.nav_item_zero here because we add it programmatically + // below, and we don't need an on-tap as of now + }; + + private static final int[] NAV_DRAWER_ACTION_ITEMS_LOGGED_IN_ONLY = { + R.id.nav_item_username + }; + private Bus bus; private EventBusMethods busMethods; private WikipediaApp app; - private View fragmentContainerView; + private View tabsContainerView; + private MenuItem usernameContainer; + private MenuItem loginContainer; + private WikiDrawerLayout drawerLayout; + private NavigationView navDrawer; + private Menu navMenu; + private SearchArticlesFragment searchFragment; + private TextView searchHintText; + private MenuItem wikipediaZeroText; + private MenuItem[] actionViews = new MenuItem[NAV_DRAWER_ACTION_ITEMS_ALL.length]; + private MenuItem[] loggedInOnyActionViews = new MenuItem[NAV_DRAWER_ACTION_ITEMS_LOGGED_IN_ONLY.length]; + private ProgressBar progressBar; + private View toolbarContainer; + private ActionMode currentActionMode; + private ActionBarDrawerToggle mDrawerToggle; + private SearchBarHideHandler searchBarHideHandler; + private boolean pausedStateOfZero; + private ZeroMessage pausedMessageOfZero; + private ThemeChooserDialog themeChooser; + private RandomHandler randomHandler; + public View getContentView() { return fragmentContainerView; } - private View tabsContainerView; public View getTabsContainerView() { return tabsContainerView; } - private WikiDrawerLayout drawerLayout; - private NavDrawerFragment fragmentNavdrawer; - private SearchArticlesFragment searchFragment; - private TextView searchHintText; - - public static final int PROGRESS_BAR_MAX_VALUE = 10000; - private ProgressBar progressBar; - - private View toolbarContainer; - private ActionMode currentActionMode; - - private ActionBarDrawerToggle mDrawerToggle; public ActionBarDrawerToggle getDrawerToggle() { return mDrawerToggle; } - private SearchBarHideHandler searchBarHideHandler; public SearchBarHideHandler getSearchBarHideHandler() { return searchBarHideHandler; } @@ -143,11 +185,6 @@ } } - private boolean pausedStateOfZero; - private ZeroMessage pausedMessageOfZero; - - private ThemeChooserDialog themeChooser; - @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); @@ -165,8 +202,65 @@ registerBus(); drawerLayout = (WikiDrawerLayout) findViewById(R.id.drawer_layout); - fragmentNavdrawer = (NavDrawerFragment) getSupportFragmentManager().findFragmentById( - R.id.navdrawer); + navDrawer = (NavigationView) findViewById(R.id.navdrawer); + LinearLayout navHeaderView = (LinearLayout) LayoutInflater.from(this).inflate(R.layout.inflate_header_nav_drawer, null); + navDrawer.addHeaderView(navHeaderView); + navMenu = navDrawer.getMenu(); + + randomHandler = new RandomHandler(navMenu.findItem(R.id.nav_item_random), + new RandomHandler.RandomListener() { + @Override + public void onRandomPageReceived(@Nullable PageTitle title) { + if (title == null) { + // TODO: show a SnackBar if we fail to fetch a random page. + // (next patch, I promise) + } else { + HistoryEntry historyEntry = new HistoryEntry(title, HistoryEntry.SOURCE_RANDOM); + displayNewPage(title, historyEntry, true); + } + } + }); + + navDrawer.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() { + @Override + public boolean onNavigationItemSelected(MenuItem menuItem) { + Intent intent = new Intent(); + switch (menuItem.getItemId()) { + case R.id.nav_item_today: + displayMainPage(); + break; + case R.id.nav_item_history: + pushFragment(new HistoryFragment()); + break; + case R.id.nav_item_saved_pages: + pushFragment(new SavedPagesFragment()); + break; + case R.id.nav_item_nearby: + pushFragment(new NearbyFragment()); + break; + case R.id.nav_item_more: + if (ApiUtil.hasHoneyComb()) { + intent.setClass(getApplicationContext(), SettingsActivity.class); + } else { + intent.setClass(getApplicationContext(), SettingsActivityGB.class); + } + startActivityForResult(intent, SettingsActivity.ACTIVITY_REQUEST_SHOW_SETTINGS); + break; + case R.id.nav_item_login: + intent.setClass(app, LoginActivity.class); + intent.putExtra(LoginActivity.LOGIN_REQUEST_SOURCE, LoginFunnel.SOURCE_NAV); + startActivity(intent); + break; + case R.id.nav_item_random: + randomHandler.doVisitRandomArticle(); + break; + default: + throw new RuntimeException("Unknown ID clicked!"); + } + return false; + } + }); + searchFragment = (SearchArticlesFragment) getSupportFragmentManager().findFragmentById(R.id.search_fragment); searchHintText = (TextView) findViewById(R.id.main_search_bar_text); @@ -324,7 +418,7 @@ getCurPageFragment().toggleToC(PageViewFragmentInternal.TOC_ACTION_HIDE); } //and make sure to update dynamic items and highlights - fragmentNavdrawer.setupDynamicItems(); + setupDynamicNavDrawerItems(); oncePerSlideLock = true; } // and make sure the Toolbar is showing @@ -581,9 +675,9 @@ } //is the new title the same as what's already being displayed? if (!frag.getCurrentTab().getBackStack().isEmpty() - && frag.getCurrentTab().getBackStack() - .get(frag.getCurrentTab().getBackStack().size() - 1).getTitle() - .equals(title)) { + && frag.getCurrentTab().getBackStack() + .get(frag.getCurrentTab().getBackStack().size() - 1).getTitle() + .equals(title)) { //if we have a section to scroll to, then pass it to the fragment if (!TextUtils.isEmpty(title.getFragment())) { frag.scrollToSection(title.getFragment()); @@ -710,9 +804,9 @@ String title = getString(R.string.zero_charged_verbiage); String verbiage = getString(R.string.zero_charged_verbiage_extended); makeWikipediaZeroCrouton(getResources().getColor(R.color.holo_red_dark), - getResources().getColor(android.R.color.white), - title); - fragmentNavdrawer.setupDynamicItems(); + getResources().getColor(android.R.color.white), + title); + setupDynamicNavDrawerItems(); showDialogAboutZero(null, title, verbiage); } else if ((!pausedStateOfZero || !pausedMessageOfZero.equals(latestCarrierMessage)) && latestWikipediaZeroDisposition) { @@ -721,7 +815,7 @@ int bg = latestCarrierMessage.getBg(); String verbiage = getString(R.string.zero_learn_more); makeWikipediaZeroCrouton(bg, fg, title); - fragmentNavdrawer.setupDynamicItems(); + setupDynamicNavDrawerItems(); showDialogAboutZero(ZERO_ON_NOTICE_PRESENTED, title, verbiage); } pausedStateOfZero = latestWikipediaZeroDisposition; @@ -792,6 +886,7 @@ if (pausedStateOfZero && !latestWikipediaZeroDisposition) { bus.post(new WikipediaZeroStateChangeEvent()); } + setupDynamicNavDrawerItems(); } @Override @@ -821,6 +916,32 @@ public void onActivityResult(int requestCode, int resultCode, final Intent data) { if (bus == null) { registerBus(); + } + if (requestCode == SettingsActivity.ACTIVITY_REQUEST_SHOW_SETTINGS) { + if (resultCode == SettingsActivity.ACTIVITY_RESULT_LANGUAGE_CHANGED) { + // Run the code a second later, to: + // - Make sure that onStart in PageActivity gets called, thus + // registering the activity for the bus. + // - The 1s delay ensures a smoother transition, otherwise was + // very jarring + Handler uiThread = new Handler(Looper.getMainLooper()); + uiThread.postDelayed(new Runnable() { + @Override + public void run() { + displayMainPage(true); + // and update any instances of our Featured Page widget, since it will + // change with the currently selected language. + Intent widgetIntent = new Intent(getApplicationContext(), WidgetProviderFeaturedPage.class); + widgetIntent.setAction(AppWidgetManager.ACTION_APPWIDGET_UPDATE); + int[] ids = AppWidgetManager.getInstance(getApplication()).getAppWidgetIds( + new ComponentName(getApplication(), WidgetProviderFeaturedPage.class)); + widgetIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS, ids); + sendBroadcast(widgetIntent); + } + }, DateUtils.SECOND_IN_MILLIS); + } else if (resultCode == SettingsActivity.ACTIVITY_RESULT_LOGOUT) { + doLogout(); + } } if ((requestCode == ACTIVITY_REQUEST_LANGLINKS && resultCode == LangLinksActivity.ACTIVITY_RESULT_LANGLINK_SELECT)) { fragmentContainerView.post(new Runnable() { @@ -901,4 +1022,60 @@ bus = null; L.d("Unregistered bus."); } -} + + private void setupDynamicNavDrawerItems() { + wikipediaZeroText = navMenu.findItem(R.id.nav_item_zero); + + for (int i = 0; i < NAV_DRAWER_ACTION_ITEMS_ALL.length; i++) { + actionViews[i] = navMenu.findItem(NAV_DRAWER_ACTION_ITEMS_ALL[i]); + } + for (int i = 0; i < NAV_DRAWER_ACTION_ITEMS_LOGGED_IN_ONLY.length; i++) { + loggedInOnyActionViews[i] = navMenu.findItem(NAV_DRAWER_ACTION_ITEMS_LOGGED_IN_ONLY[i]); + } + + if (usernameContainer == null) { + usernameContainer = navMenu.findItem(R.id.nav_item_username); + loginContainer = navMenu.findItem(R.id.nav_item_login); + } + + // Do login / logout swap + if (app.getUserInfoStorage().isLoggedIn()) { + loginContainer.setVisible(false); + for (MenuItem loggedInOnyActionView : loggedInOnyActionViews) { + loggedInOnyActionView.setVisible(true); + } + usernameContainer.setTitle(app.getUserInfoStorage().getUser().getUsername()); + } else { + loginContainer.setVisible(true); + for (MenuItem loggedInOnyActionView : loggedInOnyActionViews) { + loggedInOnyActionView.setVisible(false); + } + } + + // Show Wikipedia Zero if ON, otherwise hide it + if (app.getWikipediaZeroHandler().isZeroEnabled()) { + wikipediaZeroText.setTitle(app.getWikipediaZeroHandler().getCarrierMessage().getMsg()); + wikipediaZeroText.setVisible(true); + } else { + wikipediaZeroText.setVisible(false); + } + + } + + private void doLogout() { + boolean doUpdate = true; + if (app == null) { + // we haven't yet been attached to our Activity, so don't worry about + // updating dynamic items + doUpdate = false; + app = WikipediaApp.getInstance(); + } + app.getEditTokenStorage().clearAllTokens(); + app.getCookieManager().clearAllCookies(); + app.getUserInfoStorage().clearUser(); + Toast.makeText(app, R.string.toast_logout_complete, Toast.LENGTH_LONG).show(); + if (doUpdate) { + setupDynamicNavDrawerItems(); + } + } +} \ No newline at end of file diff --git a/wikipedia/src/main/java/org/wikipedia/random/RandomHandler.java b/wikipedia/src/main/java/org/wikipedia/random/RandomHandler.java index 5c99697..ef38e27 100644 --- a/wikipedia/src/main/java/org/wikipedia/random/RandomHandler.java +++ b/wikipedia/src/main/java/org/wikipedia/random/RandomHandler.java @@ -3,6 +3,7 @@ import android.os.Handler; import android.os.Message; import android.util.Log; +import android.view.MenuItem; import android.view.View; import org.wikipedia.page.PageTitle; import org.wikipedia.WikipediaApp; @@ -12,9 +13,7 @@ private RandomArticleIdTask curRandomArticleIdTask; private static final int MESSAGE_RND = 1; - private View randomMenuItem; - private View randomIcon; - private View randomProgressBar; + private MenuItem randomMenuItem; private boolean isClosed; private RandomListener randomListener; @@ -22,10 +21,8 @@ void onRandomPageReceived(PageTitle title); } - public RandomHandler(View menuItem, View icon, View progressBar, RandomListener listener) { + public RandomHandler(MenuItem menuItem, RandomListener listener) { randomMenuItem = menuItem; - randomIcon = icon; - randomProgressBar = progressBar; randomListener = listener; this.app = WikipediaApp.getInstance(); isClosed = false; @@ -36,8 +33,6 @@ private void setState(boolean busy) { randomMenuItem.setEnabled(!busy); - randomProgressBar.setVisibility(busy ? View.VISIBLE : View.GONE); - randomIcon.setVisibility(busy ? View.GONE : View.VISIBLE); } public void onStop() { -- To view, visit https://gerrit.wikimedia.org/r/226721 To unsubscribe, visit https://gerrit.wikimedia.org/r/settings Gerrit-MessageType: newchange Gerrit-Change-Id: I4bd1b138065bfd0be7f35852881d374393013cb5 Gerrit-PatchSet: 1 Gerrit-Project: apps/android/wikipedia Gerrit-Branch: master Gerrit-Owner: Mholloway <[email protected]> _______________________________________________ MediaWiki-commits mailing list [email protected] https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits
