Bug 1179479 - Tint menuItemActionBar buttons via tint list. r=mhaigh, a=sledru
authorMichael Comella <michael.l.comella@gmail.com>
Wed, 05 Aug 2015 15:13:38 -0700
changeset 288787 7c092142102fac5312234bfc9beea307f6910cca
parent 288786 9032008d56251514e7462f7aae2b48f519220926
child 288788 33c0b0175e5fd925f3c7e6a28de0753958e72d25
push id5067
push userraliiev@mozilla.com
push dateMon, 21 Sep 2015 14:04:52 +0000
treeherdermozilla-beta@14221ffe5b2f [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersmhaigh, sledru
bugs1179479
milestone42.0a2
Bug 1179479 - Tint menuItemActionBar buttons via tint list. r=mhaigh, a=sledru Note that this undoes a small change I made previously in this series to set the color of the action bar icons via setColorFilter.
mobile/android/base/menu/MenuItemActionBar.java
mobile/android/base/resources/color-large-v11/action_bar_menu_item_colors.xml
mobile/android/base/resources/values-large-v11/styles.xml
mobile/android/base/resources/values/attrs.xml
mobile/android/base/resources/values/styles.xml
mobile/android/base/toolbar/BrowserToolbarTabletBase.java
mobile/android/base/util/DrawableUtil.java
--- a/mobile/android/base/menu/MenuItemActionBar.java
+++ b/mobile/android/base/menu/MenuItemActionBar.java
@@ -1,35 +1,44 @@
 /* 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 org.mozilla.gecko.R;
+import org.mozilla.gecko.util.DrawableUtil;
 import org.mozilla.gecko.widget.ThemedImageButton;
 
 import android.content.Context;
+import android.content.res.ColorStateList;
+import android.content.res.TypedArray;
 import android.graphics.drawable.Drawable;
 import android.util.AttributeSet;
 
 public class MenuItemActionBar extends ThemedImageButton
                                implements GeckoMenuItem.Layout {
     private static final String LOGTAG = "GeckoMenuItemActionBar";
 
+    private final ColorStateList drawableColors;
+
     public MenuItemActionBar(Context context) {
         this(context, null);
     }
 
     public MenuItemActionBar(Context context, AttributeSet attrs) {
         this(context, attrs, R.attr.menuItemActionBarStyle);
     }
 
     public MenuItemActionBar(Context context, AttributeSet attrs, int defStyle) {
         super(context, attrs, defStyle);
+
+        final TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.MenuItemActionBar, defStyle, 0);
+        drawableColors = ta.getColorStateList(R.styleable.MenuItemActionBar_drawableTintList);
+        ta.recycle();
     }
 
     @Override
     public void initialize(GeckoMenuItem item) {
         if (item == null)
             return;
 
         setIcon(item.getIcon());
@@ -38,32 +47,27 @@ public class MenuItemActionBar extends T
         setId(item.getItemId());
     }
 
     void setIcon(Drawable icon) {
         if (icon == null) {
             setVisibility(GONE);
         } else {
             setVisibility(VISIBLE);
+            DrawableUtil.tintDrawableWithStateList(icon, drawableColors);
             setImageDrawable(icon);
         }
     }
 
     void setIcon(int icon) {
         setIcon((icon == 0) ? null : getResources().getDrawable(icon));
     }
 
     void setTitle(CharSequence title) {
         // set accessibility contentDescription here
         setContentDescription(title);
     }
 
     @Override
-    public void setEnabled(boolean enabled) {
-        super.setEnabled(enabled);
-        setColorFilter(enabled ? 0 : 0xFF999999);
-    }
-
-    @Override
     public void setShowIcon(boolean show) {
         // Do nothing.
     }
 }
new file mode 100644
--- /dev/null
+++ b/mobile/android/base/resources/color-large-v11/action_bar_menu_item_colors.xml
@@ -0,0 +1,26 @@
+<?xml version="1.0" encoding="utf-8"?>
+<!-- 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/. -->
+
+<selector xmlns:android="http://schemas.android.com/apk/res/android"
+          xmlns:gecko="http://schemas.android.com/apk/res-auto">
+
+    <item gecko:state_private="true"
+          android:state_enabled="false"
+          android:color="@color/toolbar_icon_grey"
+          />
+
+    <item gecko:state_private="true"
+          android:state_enabled="true"
+          android:color="@color/tabs_tray_icon_grey"
+          />
+
+    <item gecko:state_private="false"
+          android:state_enabled="false"
+          android:color="@color/disabled_grey"
+          />
+
+    <item android:color="@color/toolbar_icon_grey" />
+
+</selector>
--- a/mobile/android/base/resources/values-large-v11/styles.xml
+++ b/mobile/android/base/resources/values-large-v11/styles.xml
@@ -73,16 +73,17 @@
         <!-- Same as pre-19 Toast style, but with no left and right margins.
              They're removed since large tablets are never going to be only 400dp wide. -->
     </style>
 
     <style name="Widget.MenuItemActionBar">
         <item name="android:layout_width">wrap_content</item>
         <item name="android:layout_height">wrap_content</item>
         <item name="android:background">@drawable/new_tablet_action_bar_button</item>
+        <item name="drawableTintList">@color/action_bar_menu_item_colors</item>
         <item name="android:scaleType">center</item>
 
         <!-- layout_width/height doesn't work here, likely because it's
              an ImageButton, so we use padding instead.
 
              Notes:
                  * The bookmarks star is larger than the reload button
                  * The reload button contains whitespace at the top of the image to lower it -->
--- a/mobile/android/base/resources/values/attrs.xml
+++ b/mobile/android/base/resources/values/attrs.xml
@@ -191,10 +191,16 @@
     </declare-styleable>
 
     <declare-styleable name="OverlayDialogButton">
         <attr name="drawable" format="reference" />
         <attr name="enabledText" format="string" />
         <attr name="disabledText" format="string" />
     </declare-styleable>
 
+    <declare-styleable name="MenuItemActionBar">
+        <!-- We reimplement tint list XML attrs here because it is
+             only available in Android 21+. -->
+        <attr name="drawableTintList" format="color" />
+    </declare-styleable>
+
 </resources>
 
--- a/mobile/android/base/resources/values/styles.xml
+++ b/mobile/android/base/resources/values/styles.xml
@@ -89,16 +89,17 @@
         <item name="android:background">@drawable/action_bar_button</item>
         <item name="android:scaleType">fitCenter</item>
     </style>
 
     <style name="Widget.MenuItemSecondaryActionBar">
         <item name="android:padding">8dip</item>
         <item name="android:background">@drawable/action_bar_button</item>
         <item name="android:scaleType">centerInside</item>
+        <item name="drawableTintList">@color/secondary_menu_item_colors</item>
     </style>
 
     <style name="Widget.MenuItemActionView">
         <item name="android:gravity">left</item>
     </style>
 
     <style name="Widget.MenuItemDefault">
         <item name="android:paddingLeft">10dip</item>
--- a/mobile/android/base/toolbar/BrowserToolbarTabletBase.java
+++ b/mobile/android/base/toolbar/BrowserToolbarTabletBase.java
@@ -140,17 +140,16 @@ abstract class BrowserToolbarTabletBase 
         setTabsCounterPrivateMode(isPrivate, colorFilter);
         menuIcon.setColorFilter(colorFilter);
 
         backButton.setPrivateMode(isPrivate);
         forwardButton.setPrivateMode(isPrivate);
         for (int i = 0; i < actionItemBar.getChildCount(); ++i) {
             final MenuItemActionBar child = (MenuItemActionBar) actionItemBar.getChildAt(i);
             child.setPrivateMode(isPrivate);
-            child.setColorFilter(colorFilter);
         }
     }
 
     private void setTabsCounterPrivateMode(final boolean isPrivate, final PorterDuffColorFilter colorFilter) {
         // The TabsCounter is a TextSwitcher which cycles two views
         // to provide animations, hence looping over these two children.
         for (int i = 0; i < 2; ++i) {
             final ThemedTextView view = (ThemedTextView) tabsCounter.getChildAt(i);
--- a/mobile/android/base/util/DrawableUtil.java
+++ b/mobile/android/base/util/DrawableUtil.java
@@ -1,25 +1,33 @@
 /* -*- Mode: Java; c-basic-offset: 4; tab-width: 20; indent-tabs-mode: nil; -*-
 /* 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.util;
 
 import android.content.Context;
+import android.content.res.ColorStateList;
 import android.graphics.drawable.Drawable;
 import android.support.annotation.ColorRes;
 import android.support.annotation.DrawableRes;
 import android.support.annotation.NonNull;
 import android.support.v4.content.ContextCompat;
 import android.support.v4.graphics.drawable.DrawableCompat;
 
 public class DrawableUtil {
 
     public static Drawable tintDrawable(@NonNull final Context context, @DrawableRes final int drawableID,
                 @ColorRes final int colorID) {
         final Drawable icon = DrawableCompat.wrap(
                 ContextCompat.getDrawable(context, drawableID).mutate());
         DrawableCompat.setTint(icon, context.getResources().getColor(colorID));
         return icon;
     }
+
+    public static Drawable tintDrawableWithStateList(@NonNull final Drawable drawable,
+            @NonNull final ColorStateList colorList) {
+        final Drawable wrappedDrawable = DrawableCompat.wrap(drawable.mutate());
+        DrawableCompat.setTintList(wrappedDrawable, colorList);
+        return wrappedDrawable;
+    }
 }