Commit d3415886 authored by Boris Sazonov's avatar Boris Sazonov Committed by Commit Bot

[Android] Add UiUtils.getTintedDrawable

This CL adds UiUtils.getTintedDrawable and removes several snippets that
were doing the same thing.

Bug: None
Change-Id: Ia32b8ca25524f4e7bf2e431bb52436ec0f852110
Reviewed-on: https://chromium-review.googlesource.com/c/1456743Reviewed-by: default avatarTheresa <twellington@chromium.org>
Commit-Queue: Boris Sazonov <bsazonov@chromium.org>
Cr-Commit-Position: refs/heads/master@{#630118}
parent 67f9e9da
...@@ -7,10 +7,8 @@ package org.chromium.chrome.browser.download.home.list.holder; ...@@ -7,10 +7,8 @@ package org.chromium.chrome.browser.download.home.list.holder;
import android.content.res.Resources; import android.content.res.Resources;
import android.graphics.drawable.Drawable; import android.graphics.drawable.Drawable;
import android.support.annotation.DrawableRes; import android.support.annotation.DrawableRes;
import android.support.v4.graphics.drawable.DrawableCompat;
import android.support.v4.graphics.drawable.RoundedBitmapDrawable; import android.support.v4.graphics.drawable.RoundedBitmapDrawable;
import android.support.v4.graphics.drawable.RoundedBitmapDrawableFactory; import android.support.v4.graphics.drawable.RoundedBitmapDrawableFactory;
import android.support.v7.content.res.AppCompatResources;
import android.view.LayoutInflater; import android.view.LayoutInflater;
import android.view.View; import android.view.View;
import android.view.ViewGroup; import android.view.ViewGroup;
...@@ -63,12 +61,8 @@ public class GenericViewHolder extends OfflineItemViewHolder { ...@@ -63,12 +61,8 @@ public class GenericViewHolder extends OfflineItemViewHolder {
if (iconId != mGenericIconId) { if (iconId != mGenericIconId) {
mGenericIconId = iconId; mGenericIconId = iconId;
Drawable drawable = DrawableCompat.wrap( Drawable drawable = org.chromium.ui.UiUtils.getTintedDrawable(
org.chromium.chrome.browser.download.home.list.view.UiUtils.getDrawable( itemView.getContext(), iconId, R.color.dark_mode_tint);
itemView.getContext(), iconId));
DrawableCompat.setTintList(drawable,
AppCompatResources.getColorStateList(
itemView.getContext(), R.color.dark_mode_tint));
mThumbnail.setUnavailableDrawable(drawable); mThumbnail.setUnavailableDrawable(drawable);
mThumbnail.setWaitingDrawable(drawable); mThumbnail.setWaitingDrawable(drawable);
......
...@@ -4,13 +4,12 @@ ...@@ -4,13 +4,12 @@
package org.chromium.chrome.browser.preferences; package org.chromium.chrome.browser.preferences;
import android.content.Context; import android.content.Context;
import android.graphics.PorterDuff;
import android.graphics.drawable.Drawable;
import android.preference.Preference; import android.preference.Preference;
import android.util.AttributeSet; import android.util.AttributeSet;
import org.chromium.base.ApiCompatibilityUtils; import org.chromium.base.ApiCompatibilityUtils;
import org.chromium.chrome.R; import org.chromium.chrome.R;
import org.chromium.ui.UiUtils;
/** /**
* A preference that displays the current sync account and status (enabled, error, needs passphrase, * A preference that displays the current sync account and status (enabled, error, needs passphrase,
...@@ -32,13 +31,8 @@ public class SyncPreference extends Preference { ...@@ -32,13 +31,8 @@ public class SyncPreference extends Preference {
setIcon(ApiCompatibilityUtils.getDrawable( setIcon(ApiCompatibilityUtils.getDrawable(
getContext().getResources(), R.drawable.sync_error)); getContext().getResources(), R.drawable.sync_error));
} else { } else {
// Sets preference icon and tints it to blue. setIcon(UiUtils.getTintedDrawable(getContext(), R.drawable.permission_background_sync,
Drawable icon = ApiCompatibilityUtils.getDrawable( R.color.default_icon_color_blue));
getContext().getResources(), R.drawable.permission_background_sync);
icon.setColorFilter(ApiCompatibilityUtils.getColor(getContext().getResources(),
R.color.default_icon_color_blue),
PorterDuff.Mode.SRC_IN);
setIcon(icon);
} }
} }
} }
...@@ -5,10 +5,8 @@ ...@@ -5,10 +5,8 @@
package org.chromium.chrome.browser.signin; package org.chromium.chrome.browser.signin;
import android.content.Context; import android.content.Context;
import android.content.res.ColorStateList;
import android.graphics.drawable.Drawable; import android.graphics.drawable.Drawable;
import android.support.annotation.Nullable; import android.support.annotation.Nullable;
import android.support.v4.graphics.drawable.DrawableCompat;
import android.support.v7.content.res.AppCompatResources; import android.support.v7.content.res.AppCompatResources;
import android.util.AttributeSet; import android.util.AttributeSet;
import android.view.View; import android.view.View;
...@@ -18,6 +16,7 @@ import android.widget.LinearLayout; ...@@ -18,6 +16,7 @@ import android.widget.LinearLayout;
import android.widget.TextView; import android.widget.TextView;
import org.chromium.chrome.R; import org.chromium.chrome.R;
import org.chromium.ui.UiUtils;
import org.chromium.ui.drawable.AnimationLooper; import org.chromium.ui.drawable.AnimationLooper;
import org.chromium.ui.widget.ButtonCompat; import org.chromium.ui.widget.ButtonCompat;
...@@ -136,13 +135,8 @@ public class SigninView extends LinearLayout { ...@@ -136,13 +135,8 @@ public class SigninView extends LinearLayout {
} }
static Drawable getExpandArrowDrawable(Context context) { static Drawable getExpandArrowDrawable(Context context) {
Drawable drawable = return UiUtils.getTintedDrawable(
AppCompatResources.getDrawable(context, R.drawable.ic_expand_more_black_24dp); context, R.drawable.ic_expand_more_black_24dp, R.color.dark_mode_tint);
assert drawable != null;
Drawable tintableDrawable = DrawableCompat.wrap(drawable).mutate();
ColorStateList tint = AppCompatResources.getColorStateList(context, R.color.dark_mode_tint);
DrawableCompat.setTintList(tintableDrawable, tint);
return tintableDrawable;
} }
static Drawable getCheckmarkDrawable(Context context) { static Drawable getCheckmarkDrawable(Context context) {
......
...@@ -11,7 +11,6 @@ import android.annotation.SuppressLint; ...@@ -11,7 +11,6 @@ import android.annotation.SuppressLint;
import android.content.Context; import android.content.Context;
import android.content.res.Resources; import android.content.res.Resources;
import android.graphics.drawable.Drawable; import android.graphics.drawable.Drawable;
import android.support.v4.graphics.drawable.DrawableCompat;
import android.support.v4.view.ViewCompat; import android.support.v4.view.ViewCompat;
import android.support.v7.content.res.AppCompatResources; import android.support.v7.content.res.AppCompatResources;
import android.util.AttributeSet; import android.util.AttributeSet;
...@@ -37,6 +36,7 @@ import org.chromium.chrome.browser.toolbar.TabCountProvider.TabCountObserver; ...@@ -37,6 +36,7 @@ import org.chromium.chrome.browser.toolbar.TabCountProvider.TabCountObserver;
import org.chromium.chrome.browser.util.AccessibilityUtil; import org.chromium.chrome.browser.util.AccessibilityUtil;
import org.chromium.chrome.browser.util.ColorUtils; import org.chromium.chrome.browser.util.ColorUtils;
import org.chromium.chrome.browser.util.FeatureUtilities; import org.chromium.chrome.browser.util.FeatureUtilities;
import org.chromium.ui.UiUtils;
import org.chromium.ui.base.DeviceFormFactor; import org.chromium.ui.base.DeviceFormFactor;
import java.util.ArrayList; import java.util.ArrayList;
...@@ -104,10 +104,8 @@ public class ToolbarTablet extends ToolbarLayout ...@@ -104,10 +104,8 @@ public class ToolbarTablet extends ToolbarLayout
mReloadButton = findViewById(R.id.refresh_button); mReloadButton = findViewById(R.id.refresh_button);
// ImageView tinting doesn't work with LevelListDrawable, use Drawable tinting instead. // ImageView tinting doesn't work with LevelListDrawable, use Drawable tinting instead.
// See https://crbug.com/891593 for details. // See https://crbug.com/891593 for details.
Drawable reloadIcon = Drawable reloadIcon = UiUtils.getTintedDrawable(
AppCompatResources.getDrawable(getContext(), R.drawable.btn_reload_stop); getContext(), R.drawable.btn_reload_stop, R.color.dark_mode_tint);
DrawableCompat.setTintList(reloadIcon,
AppCompatResources.getColorStateList(getContext(), R.color.dark_mode_tint));
mReloadButton.setImageDrawable(reloadIcon); mReloadButton.setImageDrawable(reloadIcon);
mShowTabStack = AccessibilityUtil.isAccessibilityEnabled() mShowTabStack = AccessibilityUtil.isAccessibilityEnabled()
&& isAccessibilityTabSwitcherPreferenceEnabled(); && isAccessibilityTabSwitcherPreferenceEnabled();
......
...@@ -260,6 +260,7 @@ android_library("ui_utils_java") { ...@@ -260,6 +260,7 @@ android_library("ui_utils_java") {
] ]
deps = [ deps = [
"//base:base_java", "//base:base_java",
"//third_party/android_deps:android_support_v7_appcompat_java",
] ]
} }
......
...@@ -8,9 +8,14 @@ import android.content.Context; ...@@ -8,9 +8,14 @@ import android.content.Context;
import android.graphics.Bitmap; import android.graphics.Bitmap;
import android.graphics.Canvas; import android.graphics.Canvas;
import android.graphics.Typeface; import android.graphics.Typeface;
import android.graphics.drawable.Drawable;
import android.os.Build; import android.os.Build;
import android.os.Environment; import android.os.Environment;
import android.os.StrictMode; import android.os.StrictMode;
import android.support.annotation.ColorRes;
import android.support.annotation.DrawableRes;
import android.support.v4.graphics.drawable.DrawableCompat;
import android.support.v7.content.res.AppCompatResources;
import android.text.TextUtils; import android.text.TextUtils;
import android.view.SurfaceView; import android.view.SurfaceView;
import android.view.View; import android.view.View;
...@@ -462,6 +467,22 @@ public class UiUtils { ...@@ -462,6 +467,22 @@ public class UiUtils {
return indexInParent; return indexInParent;
} }
/**
* Gets a drawable from the resources and applies the specified tint to it. Uses Support Library
* for vector drawables and tinting on older Android versions.
* @param drawableId The resource id for the drawable.
* @param tintColorId The resource id for the color or ColorStateList.
*/
public static Drawable getTintedDrawable(
Context context, @DrawableRes int drawableId, @ColorRes int tintColorId) {
Drawable drawable = AppCompatResources.getDrawable(context, drawableId);
assert drawable != null;
drawable = DrawableCompat.wrap(drawable).mutate();
DrawableCompat.setTintList(
drawable, AppCompatResources.getColorStateList(context, tintColorId));
return drawable;
}
/** /**
* @return Whether the support for theming on a particular device has been completely disabled * @return Whether the support for theming on a particular device has been completely disabled
* due to lack of support by the OEM. * due to lack of support by the OEM.
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment