From 211075847c066d0e7a31f80f6b74c97d52b5b736 Mon Sep 17 00:00:00 2001 From: Fabian Kozynski Date: Thu, 3 Dec 2020 12:14:07 -0500 Subject: [PATCH 1/2] Fix colors of thick brightness slider More pleasant colors for the background and icon of the slider. Also, have the icon change color as the slider passes by. There doesn't seem to be an easy way to use a vector drawable as an item while at the same time changing the tint and alpha. Test: manual Fixes: 17476414 Change-Id: Ie545c3b681885ea8372808aae8804f58c85f2f67 --- .../brightness_progress_drawable_thick.xml | 42 +++++++++++++++---- .../brightness_progress_full_drawable.xml | 36 ++++++++++++---- ...quick_settings_brightness_dialog_thick.xml | 19 ++------- .../settings/brightness/BrightnessSlider.java | 2 +- 4 files changed, 67 insertions(+), 32 deletions(-) diff --git a/packages/SystemUI/res/drawable/brightness_progress_drawable_thick.xml b/packages/SystemUI/res/drawable/brightness_progress_drawable_thick.xml index ca56ec17014bf..9edf84ef70d19 100644 --- a/packages/SystemUI/res/drawable/brightness_progress_drawable_thick.xml +++ b/packages/SystemUI/res/drawable/brightness_progress_drawable_thick.xml @@ -16,6 +16,39 @@ --> + + + + + + + + + + + + + + + + + + + + - - - - - - - \ No newline at end of file diff --git a/packages/SystemUI/res/drawable/brightness_progress_full_drawable.xml b/packages/SystemUI/res/drawable/brightness_progress_full_drawable.xml index a38b8b4e62dbb..2c7ff350381c5 100644 --- a/packages/SystemUI/res/drawable/brightness_progress_full_drawable.xml +++ b/packages/SystemUI/res/drawable/brightness_progress_full_drawable.xml @@ -15,10 +15,32 @@ ~ limitations under the License. --> - - - - - \ No newline at end of file + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/packages/SystemUI/res/layout/quick_settings_brightness_dialog_thick.xml b/packages/SystemUI/res/layout/quick_settings_brightness_dialog_thick.xml index e08b44af10681..4f4b2af097990 100644 --- a/packages/SystemUI/res/layout/quick_settings_brightness_dialog_thick.xml +++ b/packages/SystemUI/res/layout/quick_settings_brightness_dialog_thick.xml @@ -14,10 +14,10 @@ limitations under the License. --> + android:layout_height="wrap_content" + android:layout_width="match_parent" + android:layout_gravity="center" + style="@style/BrightnessDialogContainer"> - - diff --git a/packages/SystemUI/src/com/android/systemui/settings/brightness/BrightnessSlider.java b/packages/SystemUI/src/com/android/systemui/settings/brightness/BrightnessSlider.java index 6c4f8083788a0..61a6ada3fc103 100644 --- a/packages/SystemUI/src/com/android/systemui/settings/brightness/BrightnessSlider.java +++ b/packages/SystemUI/src/com/android/systemui/settings/brightness/BrightnessSlider.java @@ -273,7 +273,7 @@ public class BrightnessSlider } /** Get the layout to inflate based on what slider to use */ - public int getLayout() { + private int getLayout() { return mSettings.useThickSlider() ? R.layout.quick_settings_brightness_dialog_thick : R.layout.quick_settings_brightness_dialog; From e08c9b7653369c6eb8a79c0400a078f8bfaac2ab Mon Sep 17 00:00:00 2001 From: Fabian Kozynski Date: Mon, 7 Dec 2020 11:34:50 -0500 Subject: [PATCH 2/2] Extend DrawableWrapper to set tint and alpha This way, the brightness icon can be overlaid properly and the colors will work. Test: manual Fixes: 174746414 Change-Id: I73bb98a08e89be780bd89cc48228f80b15b8b9de --- .../brightness_progress_drawable_thick.xml | 22 +--- .../brightness_progress_full_drawable.xml | 23 +--- packages/SystemUI/res/values/attrs.xml | 6 + .../settings/brightness/BrightnessSlider.java | 35 ++++++ .../util/AlphaTintDrawableWrapper.java | 112 ++++++++++++++++++ 5 files changed, 164 insertions(+), 34 deletions(-) create mode 100644 packages/SystemUI/src/com/android/systemui/util/AlphaTintDrawableWrapper.java diff --git a/packages/SystemUI/res/drawable/brightness_progress_drawable_thick.xml b/packages/SystemUI/res/drawable/brightness_progress_drawable_thick.xml index 9edf84ef70d19..108591beb05a8 100644 --- a/packages/SystemUI/res/drawable/brightness_progress_drawable_thick.xml +++ b/packages/SystemUI/res/drawable/brightness_progress_drawable_thick.xml @@ -20,9 +20,9 @@ android:gravity="center_vertical|fill_horizontal"> - + android:alpha="?android:attr/disabledAlpha"> @@ -31,21 +31,9 @@ - - - - - - - + diff --git a/packages/SystemUI/res/drawable/brightness_progress_full_drawable.xml b/packages/SystemUI/res/drawable/brightness_progress_full_drawable.xml index 2c7ff350381c5..b5def5ebf5393 100644 --- a/packages/SystemUI/res/drawable/brightness_progress_full_drawable.xml +++ b/packages/SystemUI/res/drawable/brightness_progress_full_drawable.xml @@ -16,7 +16,7 @@ --> - + @@ -24,23 +24,12 @@ - - - - - - - + \ No newline at end of file diff --git a/packages/SystemUI/res/values/attrs.xml b/packages/SystemUI/res/values/attrs.xml index 78d92c4b47e23..897e3902b55c6 100644 --- a/packages/SystemUI/res/values/attrs.xml +++ b/packages/SystemUI/res/values/attrs.xml @@ -161,5 +161,11 @@ + + + + + + diff --git a/packages/SystemUI/src/com/android/systemui/settings/brightness/BrightnessSlider.java b/packages/SystemUI/src/com/android/systemui/settings/brightness/BrightnessSlider.java index 61a6ada3fc103..53ff1dfd277b2 100644 --- a/packages/SystemUI/src/com/android/systemui/settings/brightness/BrightnessSlider.java +++ b/packages/SystemUI/src/com/android/systemui/settings/brightness/BrightnessSlider.java @@ -17,6 +17,9 @@ package com.android.systemui.settings.brightness; import android.content.Context; +import android.graphics.drawable.ClipDrawable; +import android.graphics.drawable.Drawable; +import android.graphics.drawable.LayerDrawable; import android.view.LayoutInflater; import android.view.MotionEvent; import android.view.View; @@ -27,6 +30,7 @@ import android.widget.SeekBar; import androidx.annotation.Nullable; import com.android.settingslib.RestrictedLockUtils; +import com.android.settingslib.Utils; import com.android.systemui.R; import com.android.systemui.statusbar.policy.BrightnessMirrorController; import com.android.systemui.util.ViewController; @@ -269,6 +273,10 @@ public class BrightnessSlider private BrightnessSlider fromTree(ViewGroup root, boolean useMirror) { BrightnessSliderView v = root.requireViewById(R.id.brightness_slider); + + // TODO(175026098) Workaround. Remove when b/175026098 is fixed + applyTheme(v); + return new BrightnessSlider(root, v, useMirror); } @@ -278,5 +286,32 @@ public class BrightnessSlider ? R.layout.quick_settings_brightness_dialog_thick : R.layout.quick_settings_brightness_dialog; } + + private LayerDrawable findProgressClippableDrawable(BrightnessSliderView v) { + SeekBar b = v.requireViewById(R.id.slider); + if (b.getProgressDrawable() instanceof LayerDrawable) { + Drawable progress = ((LayerDrawable) b.getProgressDrawable()) + .findDrawableByLayerId(com.android.internal.R.id.progress); + if (progress instanceof ClipDrawable) { + Drawable inner = ((ClipDrawable) progress).getDrawable(); + if (inner instanceof LayerDrawable) { + return (LayerDrawable) inner; + } + } + } + return null; + } + + private void applyTheme(BrightnessSliderView v) { + LayerDrawable layer = findProgressClippableDrawable(v); + if (layer != null) { + layer.findDrawableByLayerId(R.id.slider_foreground).setTintList( + Utils.getColorAttr(v.getContext(), + com.android.internal.R.attr.colorControlActivated)); + layer.findDrawableByLayerId(R.id.slider_icon).setTintList( + Utils.getColorAttr(v.getContext(), + com.android.internal.R.attr.colorBackground)); + } + } } } diff --git a/packages/SystemUI/src/com/android/systemui/util/AlphaTintDrawableWrapper.java b/packages/SystemUI/src/com/android/systemui/util/AlphaTintDrawableWrapper.java new file mode 100644 index 0000000000000..79a197d9d4097 --- /dev/null +++ b/packages/SystemUI/src/com/android/systemui/util/AlphaTintDrawableWrapper.java @@ -0,0 +1,112 @@ +/* + * Copyright (C) 2020 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.systemui.util; + +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 com.android.systemui.R; + +import org.xmlpull.v1.XmlPullParser; +import org.xmlpull.v1.XmlPullParserException; + +import java.io.IOException; + +/** + * An extension of {@link DrawableWrapper} that supports alpha and tint XML properties. + * + * {@link DrawableWrapper} supports setting these properties programmatically, but doesn't expose + * corresponding XML properties for some reason. This class allows to set these values in the XML, + * supporting theming. + * + * This class should only be used in XML. + * + * @attr ref android.R.styleable#DrawableWrapper_drawable + * @attr ref R.styleable#AlphaTintDrawableWrapper_tint + * @attr ref R.styleable#AlphaTintDrawableWrapper_alpha + */ +public class AlphaTintDrawableWrapper extends DrawableWrapper { + private ColorStateList mTint; + private int[] mThemeAttrs; + + /** No-arg constructor used by drawable inflation. */ + public AlphaTintDrawableWrapper() { + 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.AlphaTintDrawableWrapper); + + 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.AlphaTintDrawableWrapper); + 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.AlphaTintDrawableWrapper_android_drawable)) { + setDrawable(a.getDrawable(R.styleable.AlphaTintDrawableWrapper_android_drawable)); + } + if (a.hasValue(R.styleable.AlphaTintDrawableWrapper_android_tint)) { + mTint = a.getColorStateList(R.styleable.AlphaTintDrawableWrapper_android_tint); + } + if (a.hasValue(R.styleable.AlphaTintDrawableWrapper_android_alpha)) { + float alpha = a.getFloat(R.styleable.AlphaTintDrawableWrapper_android_alpha, 1); + setAlpha(Math.round(alpha * 255)); + } + } + + private void applyTint() { + if (getDrawable() != null && mTint != null) { + getDrawable().mutate().setTintList(mTint); + } + } +}