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

Commit 67dc8e82 authored by Treehugger Robot's avatar Treehugger Robot Committed by Android (Google) Code Review
Browse files

Merge "[SettingsLib Tile] Icon is not tinted correctly when injected setting is disabled" into main

parents 3eb26f21 e05f8625
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)
     */