Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit e08c9b76 authored by Fabian Kozynski's avatar Fabian Kozynski
Browse files

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
parent 21107584
Loading
Loading
Loading
Loading
+5 −17
Original line number Diff line number Diff line
@@ -20,9 +20,9 @@
        android:gravity="center_vertical|fill_horizontal">
        <layer-list >
            <item>
                <shape android:shape="rectangle"
                <shape
                    android:tint="?android:attr/colorControlActivated"
                    android:alpha="0.5">
                    android:alpha="?android:attr/disabledAlpha">
                    <size android:height="48dp" />
                    <solid android:color="@color/white_disabled" />
                    <corners android:radius="24dp" />
@@ -31,21 +31,9 @@
            <item
                android:gravity="center_vertical|start"
                android:start="32dp">
                <!-- Copied from drawables/ic_brightness -->
            <vector
                android:width="24dp"
                android:height="24dp"
                android:viewportWidth="24.0"
                android:viewportHeight="24.0">

                <path
                    android:pathData="M18,14.48V18h-3.52L12,20.48L9.52,18H6v-3.52L3.52,12L6,9.52V6h3.52L12,3.52L14.48,6H18v3.52L20.48,12L18,14.48z"
                    />

                <path
                    android:pathData=" M20,8.69 V4h-4.69L12,0.69L8.69,4H4v4.69L0.69,12L4,15.31V20h4.69L12,23.31L15.31,20H20v-4.69L23.31,12L20,8.69z M18,14.48V18h-3.52L12,20.48L9.52,18H6v-3.52L3.52,12L6,9.52V6h3.52L12,3.52L14.48,6H18v3.52L20.48,12L18,14.48z M12,7c-2.76,0 -5,2.24 -5,5s2.24,5 5,5s5,-2.24 5,-5S14.76,7 12,7z"
                    android:fillColor="?android:attr/colorControlActivated" />
            </vector>
                <com.android.systemui.util.AlphaTintDrawableWrapper
                    android:drawable="@drawable/ic_brightness"
                    android:tint="?android:attr/colorControlActivated" />
            </item>
        </layer-list>
    </item>
+6 −17
Original line number Diff line number Diff line
@@ -16,7 +16,7 @@
  -->

<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item>
    <item android:id="@+id/slider_foreground">
        <shape>
            <size android:height="48dp" />
            <solid android:color="?android:attr/colorControlActivated" />
@@ -24,23 +24,12 @@
        </shape>
    </item>
    <item
        android:id="@+id/slider_icon"
        android:gravity="center_vertical|start"
        android:start="32dp">
        <!-- Copied from drawables/ic_brightness -->
        <vector
            android:width="24dp"
            android:height="24dp"
            android:viewportWidth="24.0"
            android:viewportHeight="24.0"
            android:alpha="0.8">

            <path
                android:pathData="M18,14.48V18h-3.52L12,20.48L9.52,18H6v-3.52L3.52,12L6,9.52V6h3.52L12,3.52L14.48,6H18v3.52L20.48,12L18,14.48z"
                />

            <path
                android:pathData=" M20,8.69 V4h-4.69L12,0.69L8.69,4H4v4.69L0.69,12L4,15.31V20h4.69L12,23.31L15.31,20H20v-4.69L23.31,12L20,8.69z M18,14.48V18h-3.52L12,20.48L9.52,18H6v-3.52L3.52,12L6,9.52V6h3.52L12,3.52L14.48,6H18v3.52L20.48,12L18,14.48z M12,7c-2.76,0 -5,2.24 -5,5s2.24,5 5,5s5,-2.24 5,-5S14.76,7 12,7z"
                android:fillColor="?android:attr/colorBackground" />
        </vector>
        <com.android.systemui.util.AlphaTintDrawableWrapper
            android:drawable="@drawable/ic_brightness"
            android:tint="?android:attr/colorBackground"
            android:alpha="0.8"/>
    </item>
</layer-list>
 No newline at end of file
+6 −0
Original line number Diff line number Diff line
@@ -161,5 +161,11 @@
        <attr name="sensorPressureCoefficient" format="float"/>
        <attr name="sensorTouchAreaCoefficient" format="float"/>
    </declare-styleable>

    <declare-styleable name="AlphaTintDrawableWrapper">
        <attr name="android:tint" />
        <attr name="android:drawable" />
        <attr name="android:alpha" />
    </declare-styleable>
</resources>
+35 −0
Original line number Diff line number Diff line
@@ -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));
            }
        }
    }
}
+112 −0
Original line number Diff line number Diff line
/*
 * 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);
        }
    }
}