Bug 1312114 - Post: centralise AppCompatDrawableManager calls to allow easy replacement r?sebastian draft
authorAndrzej Hunt <ahunt@mozilla.com>
Thu, 03 Nov 2016 11:45:42 +0100
changeset 433298 51b51e42e6aee2c8afe668c0f001693e920fbfb8
parent 433297 4bb14434fce255fea585072e83e3d3723a72611b
child 433299 bb1ebce095a755a6704ecf1331d780b3d1133786
push id34532
push userahunt@mozilla.com
push dateThu, 03 Nov 2016 10:57:55 +0000
reviewerssebastian
bugs1312114
milestone52.0a1
Bug 1312114 - Post: centralise AppCompatDrawableManager calls to allow easy replacement r?sebastian We'll want to switch to AppCompatResources as soon as it's available (i.e. support library 24.2). Keeping this call in one central location should make that upgrade slightly easier. (Especially since there is a good chance we will expand VectorDrawable usage to other parts of the app before we get round to upgrading the support library.) MozReview-Commit-ID: CvnLzE0mbwz
mobile/android/base/java/org/mozilla/gecko/menu/GeckoMenuItem.java
mobile/android/base/java/org/mozilla/gecko/menu/MenuItemActionBar.java
mobile/android/base/java/org/mozilla/gecko/menu/MenuItemDefault.java
mobile/android/base/java/org/mozilla/gecko/util/DrawableUtil.java
mobile/android/base/java/org/mozilla/gecko/util/ResourceDrawableUtils.java
--- a/mobile/android/base/java/org/mozilla/gecko/menu/GeckoMenuItem.java
+++ b/mobile/android/base/java/org/mozilla/gecko/menu/GeckoMenuItem.java
@@ -1,20 +1,20 @@
 /* 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.ResourceDrawableUtils;
 import org.mozilla.gecko.widget.GeckoActionProvider;
 
 import android.content.Intent;
 import android.graphics.drawable.Drawable;
-import android.support.v7.widget.AppCompatDrawableManager;
 import android.text.TextUtils;
 import android.view.ActionProvider;
 import android.view.ContextMenu;
 import android.view.MenuItem;
 import android.view.SubMenu;
 import android.view.View;
 
 public class GeckoMenuItem implements MenuItem {
@@ -151,17 +151,17 @@ public class GeckoMenuItem implements Me
     public int getGroupId() {
         return 0;
     }
 
     @Override
     public Drawable getIcon() {
         if (mIcon == null) {
             if (mIconRes != 0)
-                return AppCompatDrawableManager.get().getDrawable(mMenu.getContext(), mIconRes);
+                return ResourceDrawableUtils.getDrawable(mMenu.getContext(), mIconRes);
             else
                 return null;
         } else {
             return mIcon;
         }
     }
 
     @Override
--- a/mobile/android/base/java/org/mozilla/gecko/menu/MenuItemActionBar.java
+++ b/mobile/android/base/java/org/mozilla/gecko/menu/MenuItemActionBar.java
@@ -1,20 +1,20 @@
 /* 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.ResourceDrawableUtils;
 import org.mozilla.gecko.widget.themed.ThemedImageButton;
 
 import android.content.Context;
 import android.graphics.drawable.Drawable;
-import android.support.v7.widget.AppCompatDrawableManager;
 import android.util.AttributeSet;
 
 public class MenuItemActionBar extends ThemedImageButton
                                implements GeckoMenuItem.Layout {
     private static final String LOGTAG = "GeckoMenuItemActionBar";
 
     public MenuItemActionBar(Context context) {
         this(context, null);
@@ -44,17 +44,17 @@ public class MenuItemActionBar extends T
             setVisibility(GONE);
         } else {
             setVisibility(VISIBLE);
             setImageDrawable(icon);
         }
     }
 
     void setIcon(int icon) {
-        setIcon((icon == 0) ? null : AppCompatDrawableManager.get().getDrawable(getContext(), icon));
+        setIcon((icon == 0) ? null : ResourceDrawableUtils.getDrawable(getContext(), icon));
     }
 
     void setTitle(CharSequence title) {
         // set accessibility contentDescription here
         setContentDescription(title);
     }
 
     @Override
--- a/mobile/android/base/java/org/mozilla/gecko/menu/MenuItemDefault.java
+++ b/mobile/android/base/java/org/mozilla/gecko/menu/MenuItemDefault.java
@@ -1,21 +1,21 @@
 /* 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.ResourceDrawableUtils;
 
 import android.content.Context;
 import android.content.res.Resources;
 import android.graphics.Rect;
 import android.graphics.drawable.Drawable;
-import android.support.v7.widget.AppCompatDrawableManager;
 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 };
@@ -98,17 +98,17 @@ public class MenuItemDefault extends Tex
             mIcon.setBounds(sIconBounds);
             mIcon.setAlpha(isEnabled() ? 255 : 99);
         }
 
         refreshIcon();
     }
 
     void setIcon(int icon) {
-        setIcon((icon == 0) ? null : AppCompatDrawableManager.get().getDrawable(getContext(), icon));
+        setIcon((icon == 0) ? null : ResourceDrawableUtils.getDrawable(getContext(), icon));
     }
 
     void setTitle(CharSequence title) {
         setText(title);
     }
 
     @Override
     public void setEnabled(boolean enabled) {
--- a/mobile/android/base/java/org/mozilla/gecko/util/DrawableUtil.java
+++ b/mobile/android/base/java/org/mozilla/gecko/util/DrawableUtil.java
@@ -2,40 +2,38 @@
 /* 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.Color;
 import android.graphics.drawable.Drawable;
 import android.support.annotation.CheckResult;
 import android.support.annotation.ColorInt;
 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;
-import android.support.v7.widget.AppCompatDrawableManager;
 
 import org.mozilla.gecko.AppConstants;
 
 public class DrawableUtil {
 
     /**
      * Tints the given drawable with the given color and returns it.
      */
     @CheckResult
     public static Drawable tintDrawable(@NonNull final Context context,
                                         @DrawableRes final int drawableID,
                                         @ColorInt final int color) {
-        final Drawable icon = DrawableCompat.wrap(
-                AppCompatDrawableManager.get().getDrawable(context, drawableID).mutate());
+        final Drawable icon = DrawableCompat.wrap(ResourceDrawableUtils.getDrawable(context, drawableID)
+                .mutate());
         DrawableCompat.setTint(icon, color);
         return icon;
     }
 
     /**
      * Tints the given drawable with the given color and returns it.
      */
     @CheckResult
--- a/mobile/android/base/java/org/mozilla/gecko/util/ResourceDrawableUtils.java
+++ b/mobile/android/base/java/org/mozilla/gecko/util/ResourceDrawableUtils.java
@@ -3,16 +3,18 @@
  * 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.graphics.drawable.BitmapDrawable;
 import android.graphics.drawable.Drawable;
 import android.net.Uri;
+import android.support.annotation.DrawableRes;
+import android.support.annotation.NonNull;
 import android.support.v7.widget.AppCompatDrawableManager;
 import android.text.TextUtils;
 import android.util.Log;
 
 import org.mozilla.gecko.util.GeckoJarReader;
 import org.mozilla.gecko.util.ThreadUtils;
 import org.mozilla.gecko.util.UIAsyncTask;
 
@@ -20,16 +22,23 @@ import java.io.InputStream;
 import java.net.URL;
 
 import static org.mozilla.gecko.gfx.BitmapUtils.getBitmapFromDataURI;
 import static org.mozilla.gecko.gfx.BitmapUtils.getResource;
 
 public class ResourceDrawableUtils {
     private static final String LOGTAG = "ResourceDrawableUtils";
 
+    public static Drawable getDrawable(@NonNull final Context context,
+                                       @DrawableRes final int drawableID) {
+        // TODO: upgrade this call to use AppCompatResources when upgrading to support library >= 24.2
+        // https://developer.android.com/reference/android/support/v7/content/res/AppCompatResources.html#getDrawable(android.content.Context,%20int)
+        return AppCompatDrawableManager.get().getDrawable(context, drawableID);
+    }
+
     public interface BitmapLoader {
         public void onBitmapFound(Drawable d);
     }
 
     public static void runOnBitmapFoundOnUiThread(final BitmapLoader loader, final Drawable d) {
         if (ThreadUtils.isOnUiThread()) {
             loader.onBitmapFound(d);
             return;
@@ -110,17 +119,17 @@ public class ResourceDrawableUtils {
             } catch (Exception ex) { }
 
             return;
         }
 
         if (data.startsWith("drawable://")) {
             final Uri imageUri = Uri.parse(data);
             final int id = getResource(context, imageUri);
-            final Drawable d = AppCompatDrawableManager.get().getDrawable(context, id);
+            final Drawable d = getDrawable(context, id);
 
             runOnBitmapFoundOnUiThread(loader, d);
             return;
         }
 
         runOnBitmapFoundOnUiThread(loader, null);
     }