1 /* -*- Mode: Java; c-basic-offset: 4; tab-width: 20; indent-tabs-mode: nil; -*-
2 /* This Source Code Form is subject to the terms of the Mozilla Public
3  * License, v. 2.0. If a copy of the MPL was not distributed with this file,
4  * You can obtain one at http://mozilla.org/MPL/2.0/. */
5 
6 package org.mozilla.gecko.util;
7 
8 import android.content.Context;
9 import android.content.res.ColorStateList;
10 import android.graphics.drawable.Drawable;
11 import android.support.annotation.CheckResult;
12 import android.support.annotation.ColorInt;
13 import android.support.annotation.ColorRes;
14 import android.support.annotation.DrawableRes;
15 import android.support.annotation.NonNull;
16 import android.support.v4.content.ContextCompat;
17 import android.support.v4.graphics.drawable.DrawableCompat;
18 
19 import org.mozilla.gecko.AppConstants;
20 
21 public class DrawableUtil {
22 
23     /**
24      * Tints the given drawable with the given color and returns it.
25      */
26     @CheckResult
27     public static Drawable tintDrawable(@NonNull final Context context,
28                                         @DrawableRes final int drawableID,
29                                         @ColorInt final int color) {
30         final Drawable icon = DrawableCompat.wrap(ResourceDrawableUtils.getDrawable(context, drawableID)
31                 .mutate());
32         DrawableCompat.setTint(icon, color);
33         return icon;
34     }
35 
36     /**
37      * Tints the given drawable with the given color and returns it.
38      */
39     @CheckResult
40     public static Drawable tintDrawableWithColorRes(@NonNull final Context context,
41                                                     @DrawableRes final int drawableID,
42                                                     @ColorRes final int colorID) {
43         return tintDrawable(context, drawableID, ContextCompat.getColor(context, colorID));
44     }
45 
46     /**
47      * Tints the given drawable with the given tint list and returns it. Note that you
48      * should no longer use the argument Drawable because the argument is not mutated
49      * on pre-Lollipop devices but is mutated on L+ due to differences in the Support
50      * Library implementation (bug 1193950).
51      */
52     @CheckResult
53     public static Drawable tintDrawableWithStateList(@NonNull final Drawable drawable,
54             @NonNull final ColorStateList colorList) {
55         final Drawable wrappedDrawable = DrawableCompat.wrap(drawable.mutate());
56         DrawableCompat.setTintList(wrappedDrawable, colorList);
57 
58         // DrawableCompat on pre-L doesn't handle its bounds correctly, and by default therefore won't
ToolbarProgressView(Context context, AttributeSet attrs, int defStyle)59         // be rendered - we need to manually copy the bounds as a workaround:
60         if (AppConstants.Versions.preMarshmallow) {
61             wrappedDrawable.setBounds(0, 0, wrappedDrawable.getIntrinsicHeight(), wrappedDrawable.getIntrinsicHeight());
62         }
63 
ToolbarProgressView(Context context, AttributeSet attrs)64         return wrappedDrawable;
65     }
66 }
67