From 8335ce30ad94dd4e6b96b5c8b14add2191083d86 Mon Sep 17 00:00:00 2001 From: Amin Shaikh Date: Fri, 5 Apr 2019 08:06:38 -0400 Subject: [PATCH] Create TintDrawable in Settings. This allows us to specify a drawable resource in xml that is simply another drawable with a different tint. This is necessary to overlay the settings wifi icons since the tint is resolved using a theme attribute. This change also replaces a few icons other duplicate icons to demonstrate usage. Fixes: 123363812 Test: make; use class in XML and run app Change-Id: Id80f318e96dab2e1ad9db8430e8371ac6497b753 --- res/drawable/ic_battery_saver_accent_24dp.xml | 18 +--- res/drawable/ic_delete_accent.xml | 20 +--- res/drawable/ic_settings_wireless_white.xml | 13 +-- res/drawable/ic_wifi_signal_0.xml | 14 +-- res/drawable/ic_wifi_signal_1.xml | 17 +-- res/drawable/ic_wifi_signal_2.xml | 17 +-- res/drawable/ic_wifi_signal_3.xml | 17 +-- res/drawable/ic_wifi_signal_4.xml | 13 +-- res/values/attrs.xml | 5 + .../android/settings/widget/TintDrawable.java | 101 ++++++++++++++++++ 10 files changed, 138 insertions(+), 97 deletions(-) create mode 100644 src/com/android/settings/widget/TintDrawable.java diff --git a/res/drawable/ic_battery_saver_accent_24dp.xml b/res/drawable/ic_battery_saver_accent_24dp.xml index 764402de5b8..31eb1930103 100644 --- a/res/drawable/ic_battery_saver_accent_24dp.xml +++ b/res/drawable/ic_battery_saver_accent_24dp.xml @@ -13,17 +13,7 @@ See the License for the specific language governing permissions and limitations under the License. --> - - - - - + diff --git a/res/drawable/ic_delete_accent.xml b/res/drawable/ic_delete_accent.xml index 86a9ccacd40..2f087e8e341 100644 --- a/res/drawable/ic_delete_accent.xml +++ b/res/drawable/ic_delete_accent.xml @@ -13,19 +13,7 @@ See the License for the specific language governing permissions and limitations under the License. --> - - - - - + diff --git a/res/drawable/ic_settings_wireless_white.xml b/res/drawable/ic_settings_wireless_white.xml index 3271b8b4f66..63be43b290b 100644 --- a/res/drawable/ic_settings_wireless_white.xml +++ b/res/drawable/ic_settings_wireless_white.xml @@ -13,12 +13,7 @@ See the License for the specific language governing permissions and limitations under the License. --> - - - + \ No newline at end of file diff --git a/res/drawable/ic_wifi_signal_0.xml b/res/drawable/ic_wifi_signal_0.xml index 55faf64f675..4a3567afb3f 100644 --- a/res/drawable/ic_wifi_signal_0.xml +++ b/res/drawable/ic_wifi_signal_0.xml @@ -14,13 +14,7 @@ See the License for the specific language governing permissions and limitations under the License. --> - - - \ No newline at end of file + \ No newline at end of file diff --git a/res/drawable/ic_wifi_signal_1.xml b/res/drawable/ic_wifi_signal_1.xml index e0a207266d1..88a94688fe8 100644 --- a/res/drawable/ic_wifi_signal_1.xml +++ b/res/drawable/ic_wifi_signal_1.xml @@ -14,16 +14,7 @@ See the License for the specific language governing permissions and limitations under the License. --> - - - - + \ No newline at end of file diff --git a/res/drawable/ic_wifi_signal_2.xml b/res/drawable/ic_wifi_signal_2.xml index d0daa603604..b1c2859dfb7 100644 --- a/res/drawable/ic_wifi_signal_2.xml +++ b/res/drawable/ic_wifi_signal_2.xml @@ -14,16 +14,7 @@ See the License for the specific language governing permissions and limitations under the License. --> - - - - + diff --git a/res/drawable/ic_wifi_signal_3.xml b/res/drawable/ic_wifi_signal_3.xml index c542c69c520..b058eba1343 100644 --- a/res/drawable/ic_wifi_signal_3.xml +++ b/res/drawable/ic_wifi_signal_3.xml @@ -14,16 +14,7 @@ See the License for the specific language governing permissions and limitations under the License. --> - - - - + \ No newline at end of file diff --git a/res/drawable/ic_wifi_signal_4.xml b/res/drawable/ic_wifi_signal_4.xml index bb7dbd04b29..e84066a9636 100644 --- a/res/drawable/ic_wifi_signal_4.xml +++ b/res/drawable/ic_wifi_signal_4.xml @@ -14,12 +14,7 @@ See the License for the specific language governing permissions and limitations under the License. --> - - - + \ No newline at end of file diff --git a/res/values/attrs.xml b/res/values/attrs.xml index be6345bd6a8..83269cc4e04 100644 --- a/res/values/attrs.xml +++ b/res/values/attrs.xml @@ -164,6 +164,11 @@ + + + + + diff --git a/src/com/android/settings/widget/TintDrawable.java b/src/com/android/settings/widget/TintDrawable.java new file mode 100644 index 00000000000..13eb1211da7 --- /dev/null +++ b/src/com/android/settings/widget/TintDrawable.java @@ -0,0 +1,101 @@ +/* + * Copyright (C) 2019 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License + */ + +package com.android.settings.widget; + +import android.annotation.NonNull; +import android.annotation.Nullable; +import android.content.res.ColorStateList; +import android.content.res.Resources; +import android.content.res.Resources.Theme; +import android.content.res.TypedArray; +import android.graphics.drawable.DrawableWrapper; +import android.util.AttributeSet; +import android.util.Log; + +import com.android.settings.R; + +import org.xmlpull.v1.XmlPullParser; +import org.xmlpull.v1.XmlPullParserException; + +import java.io.IOException; + +/** + * A Drawable that tints a contained Drawable, overriding the existing tint specified in the + * underlying drawable. This class should only be used in XML. + * + * @attr ref android.R.styleable#DrawableWrapper_drawable + * @attr ref R.styleable#TintDrawable_tint + */ +public class TintDrawable extends DrawableWrapper { + private ColorStateList mTint; + private int[] mThemeAttrs; + + /** No-arg constructor used by drawable inflation. */ + public TintDrawable() { + super(null); + } + + @Override + public void inflate(@NonNull Resources r, @NonNull XmlPullParser parser, + @NonNull AttributeSet attrs, @Nullable Theme theme) + throws XmlPullParserException, IOException { + final TypedArray a = obtainAttributes(r, theme, attrs, R.styleable.TintDrawable); + + super.inflate(r, parser, attrs, theme); + + mThemeAttrs = a.extractThemeAttrs(); + updateStateFromTypedArray(a); + a.recycle(); + + applyTint(); + } + + @Override + public void applyTheme(Theme t) { + super.applyTheme(t); + + if (mThemeAttrs != null) { + final TypedArray a = t.resolveAttributes(mThemeAttrs, R.styleable.TintDrawable); + updateStateFromTypedArray(a); + a.recycle(); + } + + // Ensure tint is reapplied after applying the theme to ensure this drawables' + // tint overrides the underlying drawables' tint. + applyTint(); + } + + @Override + public boolean canApplyTheme() { + return (mThemeAttrs != null && mThemeAttrs.length > 0) || super.canApplyTheme(); + } + + private void updateStateFromTypedArray(@NonNull TypedArray a) { + if (a.hasValue(R.styleable.TintDrawable_android_drawable)) { + setDrawable(a.getDrawable(R.styleable.TintDrawable_android_drawable)); + } + if (a.hasValue(R.styleable.TintDrawable_android_tint)) { + mTint = a.getColorStateList(R.styleable.TintDrawable_android_tint); + } + } + + private void applyTint() { + if (getDrawable() != null && mTint != null) { + getDrawable().mutate().setTintList(mTint); + } + } +}