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

Commit e05f8625 authored by Jacky Wang's avatar Jacky Wang
Browse files

[SettingsLib Tile] Icon is not tinted correctly when injected setting is disabled

Fix: 417564147
Flag: EXEMPT bugfix
Test: manual
Change-Id: I204c12269c9b2802b7b4f27b514a4f67e9c5e9d4
parent 6e9fc6c7
Loading
Loading
Loading
Loading
+18 −6
Original line number Diff line number Diff line
@@ -36,8 +36,10 @@ import android.content.Context;
import android.content.Intent;
import android.content.pm.ComponentInfo;
import android.content.pm.PackageManager;
import android.content.res.ColorStateList;
import android.content.res.Resources;
import android.content.res.TypedArray;
import android.graphics.Color;
import android.graphics.drawable.Icon;
import android.os.Bundle;
import android.os.Parcel;
@@ -303,12 +305,7 @@ public abstract class Tile implements Parcelable {
        if (iconResId != 0 && iconResId != android.R.color.transparent) {
            final Icon icon = Icon.createWithResource(componentInfo.packageName, iconResId);
            if (isIconTintable(context)) {
                final TypedArray a =
                        context.obtainStyledAttributes(
                                new int[] {android.R.attr.colorControlNormal});
                final int tintColor = a.getColor(0, 0);
                a.recycle();
                icon.setTint(tintColor);
                icon.setTintList(getIconTintColorStateList(context));
            }
            return icon;
        } else {
@@ -316,6 +313,21 @@ public abstract class Tile implements Parcelable {
        }
    }

    /**
     * Returns {@link ColorStateList} for the icon with enabled/disabled state.
     */
    public static ColorStateList getIconTintColorStateList(@NonNull Context context) {
        TypedArray typedArray = context.obtainStyledAttributes(
                new int[]{android.R.attr.colorControlNormal, android.R.attr.disabledAlpha});
        int tintColor = typedArray.getColor(0, 0);
        float disabledAlpha = typedArray.getFloat(1, 1f);
        int disabledTintColor = Color.argb((int) (Color.alpha(tintColor) * disabledAlpha),
                Color.red(tintColor), Color.green(tintColor), Color.blue(tintColor));
        typedArray.recycle();
        return new ColorStateList(new int[][]{{android.R.attr.state_enabled}, {}},
                new int[]{tintColor, disabledTintColor});
    }

    /**
     * Whether the icon can be tinted. This is true when icon needs to be monochrome (single-color)
     */