[Downloads/Phone] Add button to icon row for offlining pages

* Generalizes the AppMenu[Adapter/PropertiesDelegate] code to support
  up to five items.

* Adds an icon to the phone/compressed tablet menu that is for
  offlining a page.

* Hides it behind a flag called: --enable-offline-page-downloading

* Removes an unused icon.

- Not hooked up to anything yet.

- The icon is off-center because the asset provided by go/icons is
  off-center.  Waiting for a new one from UX.

BUG=616324

Review-Url: https://codereview.chromium.org/2240263002
Cr-Commit-Position: refs/heads/master@{#411736}
diff --git a/chrome/android/java/res/drawable-hdpi/bookmark_filter_offline_pages.png b/chrome/android/java/res/drawable-hdpi/bookmark_filter_offline_pages.png
deleted file mode 100644
index d9aacea..0000000
--- a/chrome/android/java/res/drawable-hdpi/bookmark_filter_offline_pages.png
+++ /dev/null
Binary files differ
diff --git a/chrome/android/java/res/drawable-mdpi/bookmark_filter_offline_pages.png b/chrome/android/java/res/drawable-mdpi/bookmark_filter_offline_pages.png
deleted file mode 100644
index c2c845e8..0000000
--- a/chrome/android/java/res/drawable-mdpi/bookmark_filter_offline_pages.png
+++ /dev/null
Binary files differ
diff --git a/chrome/android/java/res/drawable-xhdpi/bookmark_filter_offline_pages.png b/chrome/android/java/res/drawable-xhdpi/bookmark_filter_offline_pages.png
deleted file mode 100644
index 9495f288..0000000
--- a/chrome/android/java/res/drawable-xhdpi/bookmark_filter_offline_pages.png
+++ /dev/null
Binary files differ
diff --git a/chrome/android/java/res/drawable-xxhdpi/bookmark_filter_offline_pages.png b/chrome/android/java/res/drawable-xxhdpi/bookmark_filter_offline_pages.png
deleted file mode 100644
index ce97c85..0000000
--- a/chrome/android/java/res/drawable-xxhdpi/bookmark_filter_offline_pages.png
+++ /dev/null
Binary files differ
diff --git a/chrome/android/java/res/drawable-xxxhdpi/bookmark_filter_offline_pages.png b/chrome/android/java/res/drawable-xxxhdpi/bookmark_filter_offline_pages.png
deleted file mode 100644
index 8c83bff..0000000
--- a/chrome/android/java/res/drawable-xxxhdpi/bookmark_filter_offline_pages.png
+++ /dev/null
Binary files differ
diff --git a/chrome/android/java/res/layout/four_button_menu_item.xml b/chrome/android/java/res/layout/four_button_menu_item.xml
deleted file mode 100644
index bbec6abb..0000000
--- a/chrome/android/java/res/layout/four_button_menu_item.xml
+++ /dev/null
@@ -1,44 +0,0 @@
-<?xml version="1.0" encoding="utf-8"?>
-<!-- Copyright 2014 The Chromium Authors. All rights reserved.
-
-     Use of this source code is governed by a BSD-style license that can be
-     found in the LICENSE file.
--->
-<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
-    android:layout_width="match_parent"
-    android:layout_height="?android:attr/listPreferredItemHeightSmall"
-    android:layout_gravity="top|start"
-    android:orientation="horizontal">
-
-    <org.chromium.chrome.browser.widget.TintedImageButton
-        android:id="@+id/button_one"
-        android:layout_width="0dp"
-        android:layout_height="match_parent"
-        android:layout_weight="1"
-        android:background="?attr/listChoiceBackgroundIndicator"
-        android:scaleType="center" />
-
-    <org.chromium.chrome.browser.widget.TintedImageButton
-        android:id="@+id/button_two"
-        android:layout_width="0dp"
-        android:layout_height="match_parent"
-        android:layout_weight="1"
-        android:background="?attr/listChoiceBackgroundIndicator"
-        android:scaleType="center" />
-
-    <org.chromium.chrome.browser.widget.TintedImageButton
-        android:id="@+id/button_three"
-        android:layout_width="0dp"
-        android:layout_height="match_parent"
-        android:layout_weight="1"
-        android:background="?attr/listChoiceBackgroundIndicator"
-        android:scaleType="center" />
-
-    <org.chromium.chrome.browser.widget.TintedImageButton
-        android:id="@+id/button_four"
-        android:layout_width="0dp"
-        android:layout_height="match_parent"
-        android:layout_weight="1"
-        android:background="?attr/listChoiceBackgroundIndicator"
-        android:scaleType="center" />
-</LinearLayout>
diff --git a/chrome/android/java/res/layout/icon_row_menu_item.xml b/chrome/android/java/res/layout/icon_row_menu_item.xml
new file mode 100644
index 0000000..f1f9e96
--- /dev/null
+++ b/chrome/android/java/res/layout/icon_row_menu_item.xml
@@ -0,0 +1,32 @@
+<?xml version="1.0" encoding="utf-8"?>
+<!-- Copyright 2016 The Chromium Authors. All rights reserved.
+
+     Use of this source code is governed by a BSD-style license that can be
+     found in the LICENSE file.
+-->
+<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
+    android:layout_width="match_parent"
+    android:layout_height="?android:attr/listPreferredItemHeightSmall"
+    android:layout_gravity="top|start"
+    android:orientation="horizontal">
+
+    <org.chromium.chrome.browser.widget.TintedImageButton
+        android:id="@+id/button_one"
+        style="@style/OverflowMenuButton" />
+
+    <org.chromium.chrome.browser.widget.TintedImageButton
+        android:id="@+id/button_two"
+        style="@style/OverflowMenuButton" />
+
+    <org.chromium.chrome.browser.widget.TintedImageButton
+        android:id="@+id/button_three"
+        style="@style/OverflowMenuButton" />
+
+    <org.chromium.chrome.browser.widget.TintedImageButton
+        android:id="@+id/button_four"
+        style="@style/OverflowMenuButton" />
+
+    <org.chromium.chrome.browser.widget.TintedImageButton
+        android:id="@+id/button_five"
+        style="@style/OverflowMenuButton" />
+</LinearLayout>
diff --git a/chrome/android/java/res/layout/three_button_menu_item.xml b/chrome/android/java/res/layout/three_button_menu_item.xml
deleted file mode 100644
index 6ec9040f..0000000
--- a/chrome/android/java/res/layout/three_button_menu_item.xml
+++ /dev/null
@@ -1,36 +0,0 @@
-<?xml version="1.0" encoding="utf-8"?>
-<!-- Copyright 2015 The Chromium Authors. All rights reserved.
-
-     Use of this source code is governed by a BSD-style license that can be
-     found in the LICENSE file.
--->
-<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
-    android:layout_width="match_parent"
-    android:layout_height="?android:attr/listPreferredItemHeightSmall"
-    android:layout_gravity="top|start"
-    android:orientation="horizontal">
-
-    <org.chromium.chrome.browser.widget.TintedImageButton
-        android:id="@+id/button_one"
-        android:layout_width="0dp"
-        android:layout_height="match_parent"
-        android:layout_weight="1"
-        android:background="?attr/listChoiceBackgroundIndicator"
-        android:scaleType="center" />
-
-    <org.chromium.chrome.browser.widget.TintedImageButton
-        android:id="@+id/button_two"
-        android:layout_width="0dp"
-        android:layout_height="match_parent"
-        android:layout_weight="1"
-        android:background="?attr/listChoiceBackgroundIndicator"
-        android:scaleType="center" />
-
-    <org.chromium.chrome.browser.widget.TintedImageButton
-        android:id="@+id/button_three"
-        android:layout_width="0dp"
-        android:layout_height="match_parent"
-        android:layout_weight="1"
-        android:background="?attr/listChoiceBackgroundIndicator"
-        android:scaleType="center" />
-</LinearLayout>
diff --git a/chrome/android/java/res/menu/main_menu.xml b/chrome/android/java/res/menu/main_menu.xml
index 216206b..ab351d8 100644
--- a/chrome/android/java/res/menu/main_menu.xml
+++ b/chrome/android/java/res/menu/main_menu.xml
@@ -16,6 +16,9 @@
               <item android:id="@+id/bookmark_this_page_id"
                 android:title="@string/accessibility_menu_bookmark"
                 android:icon="@drawable/btn_star"/>
+              <item android:id="@+id/offline_page_id"
+                android:title="@string/contextmenu_save_offline"
+                android:icon="@drawable/ic_get_app_white_24dp"/>
               <item android:id="@+id/info_menu_id"
                 android:title="@string/accessibility_menu_info"
                 android:icon="@drawable/btn_info" />
diff --git a/chrome/android/java/res/values-v17/styles.xml b/chrome/android/java/res/values-v17/styles.xml
index d3a61503..02bc471a 100644
--- a/chrome/android/java/res/values-v17/styles.xml
+++ b/chrome/android/java/res/values-v17/styles.xml
@@ -221,6 +221,13 @@
         <item name="android:listPreferredItemHeightSmall">48dp</item>
         <item name="android:textSize">16sp</item>
     </style>
+    <style name="OverflowMenuButton">
+        <item name="android:layout_width">0dp</item>
+        <item name="android:layout_height">match_parent</item>
+        <item name="android:layout_weight">1</item>
+        <item name="android:background">?attr/listChoiceBackgroundIndicator</item>
+        <item name="android:scaleType">center</item>
+    </style>
     <style name="OverflowMenuAnim">
         <item name="android:windowEnterAnimation">@anim/menu_enter</item>
         <item name="android:windowExitAnimation">@anim/menu_exit</item>
diff --git a/chrome/android/java/src/org/chromium/chrome/browser/ChromeActivity.java b/chrome/android/java/src/org/chromium/chrome/browser/ChromeActivity.java
index 22553bd..ccdc945 100644
--- a/chrome/android/java/src/org/chromium/chrome/browser/ChromeActivity.java
+++ b/chrome/android/java/src/org/chromium/chrome/browser/ChromeActivity.java
@@ -1531,6 +1531,8 @@
         } else if (id == R.id.bookmark_this_page_id) {
             addOrEditBookmark(currentTab);
             RecordUserAction.record("MobileMenuAddToBookmarks");
+        } else if (id == R.id.offline_page_id) {
+            // TODO(dfalcantara): Hook up offline page downloading.
         } else if (id == R.id.reload_menu_id) {
             if (currentTab.isLoading()) {
                 currentTab.stopLoading();
diff --git a/chrome/android/java/src/org/chromium/chrome/browser/ChromeSwitches.java b/chrome/android/java/src/org/chromium/chrome/browser/ChromeSwitches.java
index 6f4e886cd..e07f6c0 100644
--- a/chrome/android/java/src/org/chromium/chrome/browser/ChromeSwitches.java
+++ b/chrome/android/java/src/org/chromium/chrome/browser/ChromeSwitches.java
@@ -86,6 +86,12 @@
      */
     public static final String MARKET_URL_FOR_TESTING = "market-url-for-testing";
 
+
+    /**
+     * Show the button for saving an offline page.
+     */
+    public static final String ENABLE_OFFLINE_PAGE_DOWNLOADING = "enable-offline-page-downloading";
+
     ///////////////////////////////////////////////////////////////////////////////////////////////
     // Native Switches
     ///////////////////////////////////////////////////////////////////////////////////////////////
diff --git a/chrome/android/java/src/org/chromium/chrome/browser/appmenu/AppMenuAdapter.java b/chrome/android/java/src/org/chromium/chrome/browser/appmenu/AppMenuAdapter.java
index 3484671..2518f4b0 100644
--- a/chrome/android/java/src/org/chromium/chrome/browser/appmenu/AppMenuAdapter.java
+++ b/chrome/android/java/src/org/chromium/chrome/browser/appmenu/AppMenuAdapter.java
@@ -31,6 +31,14 @@
 
 /**
  * ListAdapter to customize the view of items in the list.
+ *
+ * The icon row in the menu is a special case of a MenuItem that displays multiple buttons in a row.
+ * If, for some unfathomable reason, you need to add yet another icon to the row (the current max
+ * is five), then you will need to:
+ *
+ * 1) Update icon_row_menu_item.xml to have as many buttons as you need.
+ * 2) Edit the {@link BUTTON_IDS} to reference your new button.
+ * 3) Hope that the icon row still fits on small phones.
  */
 class AppMenuAdapter extends BaseAdapter {
     /**
@@ -45,7 +53,7 @@
     private static final int TITLE_BUTTON_MENU_ITEM = 1;
 
     /**
-     * Menu item that has four buttons. Every one of these buttons is displayed as an icon.
+     * Menu item that has three buttons. Every one of these buttons is displayed as an icon.
      */
     private static final int THREE_BUTTON_MENU_ITEM = 2;
 
@@ -55,14 +63,28 @@
     private static final int FOUR_BUTTON_MENU_ITEM = 3;
 
     /**
+     * Menu item that has five buttons. Every one of these buttons is displayed as an icon.
+     */
+    private static final int FIVE_BUTTON_MENU_ITEM = 4;
+
+    /**
      * Menu item for updating Chrome; uses a custom layout.
      */
-    private static final int UPDATE_MENU_ITEM = 4;
+    private static final int UPDATE_MENU_ITEM = 5;
 
     /**
      * The number of view types specified above.  If you add a view type you MUST increment this.
      */
-    private static final int VIEW_TYPE_COUNT = 5;
+    private static final int VIEW_TYPE_COUNT = 6;
+
+    /** IDs of all of the buttons in icon_row_menu_item.xml. */
+    private static final int[] BUTTON_IDS = {
+        R.id.button_one,
+        R.id.button_two,
+        R.id.button_three,
+        R.id.button_four,
+        R.id.button_five
+    };
 
     /** MenuItem Animation Constants */
     private static final int ENTER_ITEM_DURATION_MS = 350;
@@ -102,6 +124,8 @@
 
         if (item.getItemId() == R.id.update_menu_id) {
             return UPDATE_MENU_ITEM;
+        } else if (viewCount == 5) {
+            return FIVE_BUTTON_MENU_ITEM;
         } else if (viewCount == 4) {
             return FOUR_BUTTON_MENU_ITEM;
         } else if (viewCount == 3) {
@@ -175,57 +199,15 @@
                 break;
             }
             case THREE_BUTTON_MENU_ITEM: {
-                ThreeButtonMenuItemViewHolder holder = null;
-                if (convertView == null
-                        || !(convertView.getTag() instanceof ThreeButtonMenuItemViewHolder)) {
-                    holder = new ThreeButtonMenuItemViewHolder();
-                    convertView = mInflater.inflate(R.layout.three_button_menu_item, parent, false);
-                    holder.buttons[0] =
-                            (TintedImageButton) convertView.findViewById(R.id.button_one);
-                    holder.buttons[1] =
-                            (TintedImageButton) convertView.findViewById(R.id.button_two);
-                    holder.buttons[2] =
-                            (TintedImageButton) convertView.findViewById(R.id.button_three);
-                    convertView.setTag(holder);
-                    convertView.setTag(R.id.menu_item_enter_anim_id,
-                            buildIconItemEnterAnimator(holder.buttons));
-                } else {
-                    holder = (ThreeButtonMenuItemViewHolder) convertView.getTag();
-                }
-                for (int i = 0; i < 3; i++) {
-                    setupImageButton(holder.buttons[i], item.getSubMenu().getItem(i));
-                }
-
-                convertView.setFocusable(false);
-                convertView.setEnabled(false);
+                convertView = createMenuItemRow(convertView, parent, item, 3);
                 break;
             }
             case FOUR_BUTTON_MENU_ITEM: {
-                FourButtonMenuItemViewHolder holder = null;
-                if (convertView == null
-                        || !(convertView.getTag() instanceof FourButtonMenuItemViewHolder)) {
-                    holder = new FourButtonMenuItemViewHolder();
-                    convertView = mInflater.inflate(R.layout.four_button_menu_item, parent, false);
-                    holder.buttons[0] =
-                            (TintedImageButton) convertView.findViewById(R.id.button_one);
-                    holder.buttons[1] =
-                            (TintedImageButton) convertView.findViewById(R.id.button_two);
-                    holder.buttons[2] =
-                            (TintedImageButton) convertView.findViewById(R.id.button_three);
-                    holder.buttons[3] =
-                            (TintedImageButton) convertView.findViewById(R.id.button_four);
-                    convertView.setTag(holder);
-                    convertView.setTag(R.id.menu_item_enter_anim_id,
-                            buildIconItemEnterAnimator(holder.buttons));
-                } else {
-                    holder = (FourButtonMenuItemViewHolder) convertView.getTag();
-                }
-                for (int i = 0; i < 4; i++) {
-                    setupImageButton(holder.buttons[i], item.getSubMenu().getItem(i));
-                }
-
-                convertView.setFocusable(false);
-                convertView.setEnabled(false);
+                convertView = createMenuItemRow(convertView, parent, item, 4);
+                break;
+            }
+            case FIVE_BUTTON_MENU_ITEM: {
+                convertView = createMenuItemRow(convertView, parent, item, 5);
                 break;
             }
             case TITLE_BUTTON_MENU_ITEM: {
@@ -292,6 +274,9 @@
                 mAppMenu.onItemClick(item);
             }
         });
+
+        // Menu items may be hidden by command line flags before they get to this point.
+        button.setVisibility(item.isVisible() ? View.VISIBLE : View.GONE);
     }
 
     private void setupStandardMenuItemViewHolder(StandardMenuItemViewHolder holder,
@@ -392,6 +377,41 @@
         return animation;
     }
 
+    private View createMenuItemRow(
+            View convertView, ViewGroup parent, MenuItem item, int numItems) {
+        RowItemViewHolder holder = null;
+        if (convertView == null
+                || !(convertView.getTag() instanceof RowItemViewHolder)
+                || ((RowItemViewHolder) convertView.getTag()).buttons.length != numItems) {
+            holder = new RowItemViewHolder(numItems);
+            convertView = mInflater.inflate(R.layout.icon_row_menu_item, parent, false);
+
+            // Save references to all the buttons.
+            for (int i = 0; i < numItems; i++) {
+                holder.buttons[i] =
+                        (TintedImageButton) convertView.findViewById(BUTTON_IDS[i]);
+            }
+
+            // Remove unused menu items.
+            for (int j = numItems; j < 5; j++) {
+                ((ViewGroup) convertView).removeView(convertView.findViewById(BUTTON_IDS[j]));
+            }
+
+            convertView.setTag(holder);
+            convertView.setTag(R.id.menu_item_enter_anim_id,
+                    buildIconItemEnterAnimator(holder.buttons));
+        } else {
+            holder = (RowItemViewHolder) convertView.getTag();
+        }
+
+        for (int i = 0; i < numItems; i++) {
+            setupImageButton(holder.buttons[i], item.getSubMenu().getItem(i));
+        }
+        convertView.setFocusable(false);
+        convertView.setEnabled(false);
+        return convertView;
+    }
+
     static class StandardMenuItemViewHolder {
         public TextView text;
         public AppMenuItemIcon image;
@@ -401,12 +421,12 @@
         public TextView summary;
     }
 
-    static class ThreeButtonMenuItemViewHolder {
-        public TintedImageButton[] buttons = new TintedImageButton[3];
-    }
+    private static class RowItemViewHolder {
+        public TintedImageButton[] buttons;
 
-    static class FourButtonMenuItemViewHolder {
-        public TintedImageButton[] buttons = new TintedImageButton[4];
+        RowItemViewHolder(int numButtons) {
+            buttons = new TintedImageButton[numButtons];
+        }
     }
 
     static class TitleButtonMenuItemViewHolder {
diff --git a/chrome/android/java/src/org/chromium/chrome/browser/appmenu/AppMenuPropertiesDelegate.java b/chrome/android/java/src/org/chromium/chrome/browser/appmenu/AppMenuPropertiesDelegate.java
index 6eee512..595191c 100644
--- a/chrome/android/java/src/org/chromium/chrome/browser/appmenu/AppMenuPropertiesDelegate.java
+++ b/chrome/android/java/src/org/chromium/chrome/browser/appmenu/AppMenuPropertiesDelegate.java
@@ -4,13 +4,17 @@
 
 package org.chromium.chrome.browser.appmenu;
 
+import android.graphics.PorterDuff;
+import android.graphics.drawable.Drawable;
 import android.text.TextUtils;
 import android.view.Menu;
 import android.view.MenuItem;
 
+import org.chromium.base.CommandLine;
 import org.chromium.chrome.R;
 import org.chromium.chrome.browser.ChromeActivity;
 import org.chromium.chrome.browser.ChromeFeatureList;
+import org.chromium.chrome.browser.ChromeSwitches;
 import org.chromium.chrome.browser.ShortcutHelper;
 import org.chromium.chrome.browser.UrlConstants;
 import org.chromium.chrome.browser.bookmarks.BookmarkBridge;
@@ -104,6 +108,22 @@
 
                 MenuItem bookmarkMenuItem = menu.findItem(R.id.bookmark_this_page_id);
                 updateBookmarkMenuItem(bookmarkMenuItem, currentTab);
+
+                MenuItem offlineMenuItem = menu.findItem(R.id.offline_page_id);
+                if (offlineMenuItem != null) {
+                    if (CommandLine.getInstance().hasSwitch(
+                            ChromeSwitches.ENABLE_OFFLINE_PAGE_DOWNLOADING)) {
+                        offlineMenuItem.setEnabled(!isChromeScheme);
+                        Drawable drawable = offlineMenuItem.getIcon();
+                        if (drawable != null) {
+                            int iconTint = mActivity.getColor(R.color.light_normal_color);
+                            drawable.mutate();
+                            drawable.setColorFilter(iconTint, PorterDuff.Mode.SRC_ATOP);
+                        }
+                    } else {
+                        offlineMenuItem.setVisible(false);
+                    }
+                }
             }
 
             menu.findItem(R.id.downloads_menu_id)
diff --git a/chrome/android/java/src/org/chromium/chrome/browser/download/ui/FilterAdapter.java b/chrome/android/java/src/org/chromium/chrome/browser/download/ui/FilterAdapter.java
index e2bfaff1..7b521572 100644
--- a/chrome/android/java/src/org/chromium/chrome/browser/download/ui/FilterAdapter.java
+++ b/chrome/android/java/src/org/chromium/chrome/browser/download/ui/FilterAdapter.java
@@ -53,8 +53,8 @@
                     R.layout.download_manager_ui_drawer_filter, null);
         }
 
-        int iconId = DownloadFilter.FILTER_LIST[position][0];
-        labelView.setText(DownloadFilter.FILTER_LIST[position][1]);
+        int iconId = DownloadFilter.getDrawableForFilter(position);
+        labelView.setText(DownloadFilter.getStringIdForFilter(position));
 
         Drawable iconDrawable = null;
         if (position == mSelectedIndex) {