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;
import android.content.res.Resources;
import android.graphics.drawable.Drawable;
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.RoundedBitmapDrawableFactory;
import android.support.v7.content.res.AppCompatResources;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
......@@ -63,12 +61,8 @@ public class GenericViewHolder extends OfflineItemViewHolder {
if (iconId != mGenericIconId) {
mGenericIconId = iconId;
Drawable drawable = DrawableCompat.wrap(
org.chromium.chrome.browser.download.home.list.view.UiUtils.getDrawable(
itemView.getContext(), iconId));
DrawableCompat.setTintList(drawable,
AppCompatResources.getColorStateList(
itemView.getContext(), R.color.dark_mode_tint));
Drawable drawable = org.chromium.ui.UiUtils.getTintedDrawable(
itemView.getContext(), iconId, R.color.dark_mode_tint);
mThumbnail.setUnavailableDrawable(drawable);
mThumbnail.setWaitingDrawable(drawable);
......
......@@ -4,13 +4,12 @@
package org.chromium.chrome.browser.preferences;
import android.content.Context;
import android.graphics.PorterDuff;
import android.graphics.drawable.Drawable;
import android.preference.Preference;
import android.util.AttributeSet;
import org.chromium.base.ApiCompatibilityUtils;
import org.chromium.chrome.R;
import org.chromium.ui.UiUtils;
/**
* A preference that displays the current sync account and status (enabled, error, needs passphrase,
......@@ -32,13 +31,8 @@ public class SyncPreference extends Preference {
setIcon(ApiCompatibilityUtils.getDrawable(
getContext().getResources(), R.drawable.sync_error));
} else {
// Sets preference icon and tints it to blue.
Drawable icon = ApiCompatibilityUtils.getDrawable(
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);
setIcon(UiUtils.getTintedDrawable(getContext(), R.drawable.permission_background_sync,
R.color.default_icon_color_blue));
}
}
}
......@@ -5,10 +5,8 @@
package org.chromium.chrome.browser.signin;
import android.content.Context;
import android.content.res.ColorStateList;
import android.graphics.drawable.Drawable;
import android.support.annotation.Nullable;
import android.support.v4.graphics.drawable.DrawableCompat;
import android.support.v7.content.res.AppCompatResources;
import android.util.AttributeSet;
import android.view.View;
......@@ -18,6 +16,7 @@ import android.widget.LinearLayout;
import android.widget.TextView;
import org.chromium.chrome.R;
import org.chromium.ui.UiUtils;
import org.chromium.ui.drawable.AnimationLooper;
import org.chromium.ui.widget.ButtonCompat;
......@@ -136,13 +135,8 @@ public class SigninView extends LinearLayout {
}
static Drawable getExpandArrowDrawable(Context context) {
Drawable drawable =
AppCompatResources.getDrawable(context, R.drawable.ic_expand_more_black_24dp);
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;
return UiUtils.getTintedDrawable(
context, R.drawable.ic_expand_more_black_24dp, R.color.dark_mode_tint);
}
static Drawable getCheckmarkDrawable(Context context) {
......
......@@ -11,7 +11,6 @@ import android.annotation.SuppressLint;
import android.content.Context;
import android.content.res.Resources;
import android.graphics.drawable.Drawable;
import android.support.v4.graphics.drawable.DrawableCompat;
import android.support.v4.view.ViewCompat;
import android.support.v7.content.res.AppCompatResources;
import android.util.AttributeSet;
......@@ -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.ColorUtils;
import org.chromium.chrome.browser.util.FeatureUtilities;
import org.chromium.ui.UiUtils;
import org.chromium.ui.base.DeviceFormFactor;
import java.util.ArrayList;
......@@ -104,10 +104,8 @@ public class ToolbarTablet extends ToolbarLayout
mReloadButton = findViewById(R.id.refresh_button);
// ImageView tinting doesn't work with LevelListDrawable, use Drawable tinting instead.
// See https://crbug.com/891593 for details.
Drawable reloadIcon =
AppCompatResources.getDrawable(getContext(), R.drawable.btn_reload_stop);
DrawableCompat.setTintList(reloadIcon,
AppCompatResources.getColorStateList(getContext(), R.color.dark_mode_tint));
Drawable reloadIcon = UiUtils.getTintedDrawable(
getContext(), R.drawable.btn_reload_stop, R.color.dark_mode_tint);
mReloadButton.setImageDrawable(reloadIcon);
mShowTabStack = AccessibilityUtil.isAccessibilityEnabled()
&& isAccessibilityTabSwitcherPreferenceEnabled();
......
......@@ -260,6 +260,7 @@ android_library("ui_utils_java") {
]
deps = [
"//base:base_java",
"//third_party/android_deps:android_support_v7_appcompat_java",
]
}
......
......@@ -8,9 +8,14 @@ import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Typeface;
import android.graphics.drawable.Drawable;
import android.os.Build;
import android.os.Environment;
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.view.SurfaceView;
import android.view.View;
......@@ -462,6 +467,22 @@ public class UiUtils {
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
* 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