Bug 1391579 Part 2: Add MenuItemIcon r=jwu,sebastian
authorJulian_Chu <walkingice0204@gmail.com>
Thu, 24 Aug 2017 01:15:22 -0700
changeset 430421 7048637ef80c14b230d7cde182dbc8d3c93e1554
parent 430420 bb3a37d321e22313ce8d9d8cd71274569fc5121c
child 430422 f25a9201e1eabaa6a85bca571fbd6274c66ca4c3
push id7761
push userjlund@mozilla.com
push dateFri, 15 Sep 2017 00:19:52 +0000
treeherdermozilla-beta@c38455951db4 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersjwu, sebastian
bugs1391579
milestone57.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 1391579 Part 2: Add MenuItemIcon r=jwu,sebastian GeckoMenu always create view from MenuItemDefault. Now lets adding a new type for MenuItem which will display a Drawable in right side. MozReview-Commit-ID: F7zVDze0RaP
mobile/android/app/src/main/res/values/attrs.xml
mobile/android/base/java/org/mozilla/gecko/menu/GeckoMenu.java
mobile/android/base/java/org/mozilla/gecko/menu/GeckoMenuItem.java
mobile/android/base/java/org/mozilla/gecko/menu/MenuItemDefault.java
mobile/android/base/java/org/mozilla/gecko/menu/MenuItemIcon.java
mobile/android/base/moz.build
--- a/mobile/android/app/src/main/res/values/attrs.xml
+++ b/mobile/android/app/src/main/res/values/attrs.xml
@@ -52,16 +52,17 @@
         <attr name="android:icon"/>
         <attr name="android:checkable"/>
         <attr name="android:checked"/>
         <attr name="android:visible"/>
         <attr name="android:enabled"/>
         <attr name="android:showAsAction"/>
         <attr name="itemType" type="enum">
             <enum name="default_menu_item" value="0x00" />
+            <enum name="icon_menu_item" value="0x01" />
         </attr>
     </declare-styleable>
 
     <declare-styleable name="MenuItemDefault">
         <attr name="state_more" format="boolean"/>
     </declare-styleable>
 
     <declare-styleable name="TabThumbnailWrapper">
--- a/mobile/android/base/java/org/mozilla/gecko/menu/GeckoMenu.java
+++ b/mobile/android/base/java/org/mozilla/gecko/menu/GeckoMenu.java
@@ -772,16 +772,17 @@ public class GeckoMenu extends ListView
             }
         }
     }
 
     // Adapter to bind menu items to the list.
     private class MenuItemsAdapter extends BaseAdapter {
         private static final int VIEW_TYPE_DEFAULT = 0;
         private static final int VIEW_TYPE_ACTION_MODE = 1;
+        private static final int VIEW_TYPE_ICON = 1;
 
         private final List<GeckoMenuItem> mItems;
 
         public MenuItemsAdapter() {
             mItems = new ArrayList<GeckoMenuItem>();
         }
 
         @Override
@@ -818,18 +819,23 @@ public class GeckoMenu extends ListView
         }
 
         @Override
         public View getView(int position, View convertView, ViewGroup parent) {
             GeckoMenuItem item = getItem(position);
             GeckoMenuItem.Layout view = null;
 
             // Try to re-use the view.
-            if (convertView == null && getItemViewType(position) == VIEW_TYPE_DEFAULT) {
-                view = new MenuItemDefault(parent.getContext(), null);
+            if (convertView == null) {
+                final int type = getItemViewType(position);
+                if (type == VIEW_TYPE_ICON) {
+                    view = new MenuItemIcon(parent.getContext(), null);
+                } else if (type == VIEW_TYPE_DEFAULT) {
+                    view = new MenuItemDefault(parent.getContext(), null);
+                }
             } else {
                 view = (GeckoMenuItem.Layout) convertView;
             }
 
             if (view == null || view instanceof MenuItemSwitcherLayout) {
                 // Always get from the menu item.
                 // This will ensure that the default activity is refreshed.
                 view = (MenuItemSwitcherLayout) item.getActionView();
@@ -851,16 +857,20 @@ public class GeckoMenu extends ListView
             // Initialize the view.
             view.setShowIcon(mShowIcons);
             view.initialize(item);
             return (View) view;
         }
 
         @Override
         public int getItemViewType(int position) {
+            final GeckoMenuItem item = getItem(position);
+            if (item.getItemType() == GeckoMenuItem.ITEM_TYPE_ICON) {
+                return VIEW_TYPE_ICON;
+            }
             return getItem(position).getGeckoActionProvider() == null ? VIEW_TYPE_DEFAULT : VIEW_TYPE_ACTION_MODE;
         }
 
         @Override
         public int getViewTypeCount() {
             return 2;
         }
 
--- a/mobile/android/base/java/org/mozilla/gecko/menu/GeckoMenuItem.java
+++ b/mobile/android/base/java/org/mozilla/gecko/menu/GeckoMenuItem.java
@@ -23,16 +23,17 @@ public class GeckoMenuItem implements Me
     // These values mirror MenuItem values that are only available on API >= 11.
     public static final int SHOW_AS_ACTION_NEVER = 0;
     public static final int SHOW_AS_ACTION_IF_ROOM = 1;
     public static final int SHOW_AS_ACTION_ALWAYS = 2;
     public static final int SHOW_AS_ACTION_WITH_TEXT = 4;
     public static final int SHOW_AS_ACTION_COLLAPSE_ACTION_VIEW = 8;
 
     public static final int ITEM_TYPE_DEFAULT = 0x00;
+    public static final int ITEM_TYPE_ICON = 0x01;
 
     // A View that can show a MenuItem should be able to initialize from
     // the properties of the MenuItem.
     public static interface Layout {
         public void initialize(GeckoMenuItem item);
 
         public void setShowIcon(boolean show);
     }
--- a/mobile/android/base/java/org/mozilla/gecko/menu/MenuItemDefault.java
+++ b/mobile/android/base/java/org/mozilla/gecko/menu/MenuItemDefault.java
@@ -16,24 +16,25 @@ import android.util.AttributeSet;
 import android.widget.TextView;
 
 public class MenuItemDefault extends TextView
                              implements GeckoMenuItem.Layout {
     private static final int[] STATE_MORE = new int[] { R.attr.state_more };
     private static final int[] STATE_CHECKED = new int[] { android.R.attr.state_checkable, android.R.attr.state_checked };
     private static final int[] STATE_UNCHECKED = new int[] { android.R.attr.state_checkable };
 
-    private Drawable mIcon;
+    protected Drawable mIcon;
+    protected boolean mShowIcon;
+
     private final Drawable mState;
     private static Rect sIconBounds;
 
     private boolean mCheckable;
     private boolean mChecked;
     private boolean mHasSubMenu;
-    private boolean mShowIcon;
 
     public MenuItemDefault(Context context) {
         this(context, null);
     }
 
     public MenuItemDefault(Context context, AttributeSet attrs) {
         this(context, attrs, R.attr.menuItemDefaultStyle);
     }
@@ -83,17 +84,17 @@ public class MenuItemDefault extends Tex
         setTitle(item.getTitle());
         setIcon(item.getIcon());
         setEnabled(item.isEnabled());
         setCheckable(item.isCheckable());
         setChecked(item.isChecked());
         setSubMenuIndicator(item.hasSubMenu());
     }
 
-    private void refreshIcon() {
+    protected void refreshIcon() {
         TextViewCompat.setCompoundDrawablesRelative(this, mShowIcon ? mIcon : null, null, mState, null);
     }
 
     void setIcon(Drawable icon) {
         mIcon = icon;
 
         if (mIcon != null) {
             mIcon.setBounds(sIconBounds);
new file mode 100644
--- /dev/null
+++ b/mobile/android/base/java/org/mozilla/gecko/menu/MenuItemIcon.java
@@ -0,0 +1,32 @@
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this file,
+ * You can obtain one at http://mozilla.org/MPL/2.0/. */
+
+package org.mozilla.gecko.menu;
+
+import android.content.Context;
+import android.support.v4.widget.TextViewCompat;
+import android.util.AttributeSet;
+
+import org.mozilla.gecko.R;
+
+public class MenuItemIcon extends MenuItemDefault {
+
+    public MenuItemIcon(Context context) {
+        this(context, null);
+    }
+
+    public MenuItemIcon(Context context, AttributeSet attrs) {
+        this(context, attrs, R.attr.menuItemDefaultStyle);
+    }
+
+    public MenuItemIcon(Context context, AttributeSet attrs, int defStyle) {
+        super(context, attrs, defStyle);
+    }
+
+    @Override
+    protected void refreshIcon() {
+        // always show icon, if any
+        TextViewCompat.setCompoundDrawablesRelative(this, null, null, super.mIcon, null);
+    }
+}
--- a/mobile/android/base/moz.build
+++ b/mobile/android/base/moz.build
@@ -767,16 +767,17 @@ gbjar.sources += ['java/org/mozilla/geck
     'MediaCastingBar.java',
     'MemoryMonitor.java',
     'menu/GeckoMenu.java',
     'menu/GeckoMenuInflater.java',
     'menu/GeckoMenuItem.java',
     'menu/GeckoSubMenu.java',
     'menu/MenuItemActionBar.java',
     'menu/MenuItemDefault.java',
+    'menu/MenuItemIcon.java',
     'menu/MenuItemSwitcherLayout.java',
     'menu/MenuPanel.java',
     'menu/MenuPopup.java',
     'mma/MmaDelegate.java',
     'MotionEventInterceptor.java',
     'notifications/NotificationClient.java',
     'notifications/NotificationHelper.java',
     'notifications/NotificationReceiver.java',