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

Commit 4e22af12 authored by Ioana Alexandru's avatar Ioana Alexandru
Browse files

Introduce StatusBarIcon.Type

Let's have a type that tells us what kind of SBI we're working with, it
can make some pieces of logic more readable.

Bug: 335211019
Test: builds
Flag: EXEMPT trivial change
Change-Id: Ie82cd6e7a3d97fbffc638aa3972d918db8ea2c9c
parent e89f7097
Loading
Loading
Loading
Loading
+14 −4
Original line number Diff line number Diff line
@@ -22,7 +22,11 @@ import android.os.Parcelable;
import android.os.UserHandle;
import android.text.TextUtils;

import androidx.annotation.NonNull;

public class StatusBarIcon implements Parcelable {
    public enum Type {PeopleAvatar, MonochromeAppIcon, NotifSmallIcon, SystemIcon}

    public UserHandle user;
    public String pkg;
    public Icon icon;
@@ -30,9 +34,10 @@ public class StatusBarIcon implements Parcelable {
    public boolean visible = true;
    public int number;
    public CharSequence contentDescription;
    public Type type;

    public StatusBarIcon(UserHandle user, String resPackage, Icon icon, int iconLevel, int number,
            CharSequence contentDescription) {
            CharSequence contentDescription, Type type) {
        if (icon.getType() == Icon.TYPE_RESOURCE
                && TextUtils.isEmpty(icon.getResPackage())) {
            // This is an odd situation where someone's managed to hand us an icon without a
@@ -46,15 +51,17 @@ public class StatusBarIcon implements Parcelable {
        this.iconLevel = iconLevel;
        this.number = number;
        this.contentDescription = contentDescription;
        this.type = type;
    }

    public StatusBarIcon(String iconPackage, UserHandle user,
            int iconId, int iconLevel, int number,
            CharSequence contentDescription) {
            CharSequence contentDescription, Type type) {
        this(user, iconPackage, Icon.createWithResource(iconPackage, iconId),
                iconLevel, number, contentDescription);
                iconLevel, number, contentDescription, type);
    }

    @NonNull
    @Override
    public String toString() {
        return "StatusBarIcon(icon=" + icon
@@ -65,10 +72,11 @@ public class StatusBarIcon implements Parcelable {
                + " )";
    }

    @NonNull
    @Override
    public StatusBarIcon clone() {
        StatusBarIcon that = new StatusBarIcon(this.user, this.pkg, this.icon,
                this.iconLevel, this.number, this.contentDescription);
                this.iconLevel, this.number, this.contentDescription, this.type);
        that.visible = this.visible;
        return that;
    }
@@ -88,6 +96,7 @@ public class StatusBarIcon implements Parcelable {
        this.visible = in.readInt() != 0;
        this.number = in.readInt();
        this.contentDescription = in.readCharSequence();
        this.type = Type.valueOf(in.readString());
    }

    public void writeToParcel(Parcel out, int flags) {
@@ -98,6 +107,7 @@ public class StatusBarIcon implements Parcelable {
        out.writeInt(this.visible ? 1 : 0);
        out.writeInt(this.number);
        out.writeCharSequence(this.contentDescription);
        out.writeString(this.type.name());
    }

    public int describeContents() {
+2 −1
Original line number Diff line number Diff line
@@ -48,7 +48,8 @@ public class RegisterStatusBarResultTest {
        final String dumyIconKey = "dummyIcon1";
        final ArrayMap<String, StatusBarIcon> iconMap = new ArrayMap<>();
        iconMap.put(dumyIconKey, new StatusBarIcon("com.android.internal.statusbar.test",
                UserHandle.of(100), 123, 1, 2, "dummyIconDescription"));
                UserHandle.of(100), 123, 1, 2, "dummyIconDescription",
                StatusBarIcon.Type.SystemIcon));
        final LetterboxDetails letterboxDetails = new LetterboxDetails(
                /* letterboxInnerBounds= */ new Rect(1, 2, 3, 4),
                /* letterboxFullBounds= */ new Rect(5, 6, 7, 8),
+2 −1
Original line number Diff line number Diff line
@@ -44,7 +44,8 @@ public class StatusBarIconTest {
        final int dummyIconNumber = 2;
        final CharSequence dummyIconContentDescription = "dummyIcon";
        final StatusBarIcon original = new StatusBarIcon(dummyIconPackageName, dummyUserHandle,
                dummyIconId, dummyIconLevel, dummyIconNumber, dummyIconContentDescription);
                dummyIconId, dummyIconLevel, dummyIconNumber, dummyIconContentDescription,
                StatusBarIcon.Type.SystemIcon);

        final StatusBarIcon copy = clone(original);

+1 −1
Original line number Diff line number Diff line
@@ -332,7 +332,7 @@ public class TileServices extends IQSService.Stub {
                if (info.applicationInfo.isSystemApp()) {
                    final StatusBarIcon statusIcon = icon != null
                            ? new StatusBarIcon(userHandle, packageName, icon, 0, 0,
                            contentDescription)
                            contentDescription, StatusBarIcon.Type.SystemIcon)
                            : null;
                    final String slot = getStatusBarIconSlotName(componentName);
                    mMainHandler.post(new Runnable() {
+32 −34
Original line number Diff line number Diff line
@@ -226,28 +226,27 @@ constructor(
        }

        val n = entry.sbn.notification
        var usingMonochromeAppIcon = false
        val icon: Icon?
        val (icon: Icon?, type: StatusBarIcon.Type) =
            if (showPeopleAvatar) {
            icon = createPeopleAvatar(entry)
        } else if (android.app.Flags.notificationsUseMonochromeAppIcon()) {
            if (n.shouldUseAppIcon()) {
                icon =
                    getMonochromeAppIcon(entry)?.also { usingMonochromeAppIcon = true }
                        ?: n.smallIcon
                createPeopleAvatar(entry) to StatusBarIcon.Type.PeopleAvatar
            } else if (
                android.app.Flags.notificationsUseMonochromeAppIcon() && n.shouldUseAppIcon()
            ) {
                val monochrome = getMonochromeAppIcon(entry)
                if (monochrome != null) {
                    monochrome to StatusBarIcon.Type.MonochromeAppIcon
                } else {
                icon = n.smallIcon
                    n.smallIcon to StatusBarIcon.Type.NotifSmallIcon
                }
            } else {
            icon = n.smallIcon
                n.smallIcon to StatusBarIcon.Type.NotifSmallIcon
            }

        if (icon == null) {
            throw InflationException("No icon in notification from ${entry.sbn.packageName}")
        }

        val sbi = icon.toStatusBarIcon(entry)
        cacheIconDescriptor(entry, sbi, showPeopleAvatar, usingMonochromeAppIcon)
        val sbi = icon.toStatusBarIcon(entry, type)
        cacheIconDescriptor(entry, sbi)
        return sbi
    }

@@ -269,29 +268,23 @@ constructor(
        }
    }

    private fun cacheIconDescriptor(
        entry: NotificationEntry,
        descriptor: StatusBarIcon,
        showPeopleAvatar: Boolean,
        usingMonochromeAppIcon: Boolean
    ) {
        if (android.app.Flags.notificationsUseAppIcon() ||
    private fun cacheIconDescriptor(entry: NotificationEntry, descriptor: StatusBarIcon) {
        if (
            android.app.Flags.notificationsUseAppIcon() ||
                android.app.Flags.notificationsUseMonochromeAppIcon()
        ) {
            // If either of the new icon flags is enabled, we cache the icon all the time.
            if (showPeopleAvatar) {
                entry.icons.peopleAvatarDescriptor = descriptor
            } else if (usingMonochromeAppIcon) {
            when (descriptor.type) {
                StatusBarIcon.Type.PeopleAvatar -> entry.icons.peopleAvatarDescriptor = descriptor
                // When notificationsUseMonochromeAppIcon is enabled, we use the appIconDescriptor.
                entry.icons.appIconDescriptor = descriptor
            } else {
                StatusBarIcon.Type.MonochromeAppIcon -> entry.icons.appIconDescriptor = descriptor
                // When notificationsUseAppIcon is enabled, the app icon overrides the small icon.
                // But either way, it's a good idea to cache the descriptor.
                entry.icons.smallIconDescriptor = descriptor
                else -> entry.icons.smallIconDescriptor = descriptor
            }
        } else if (isImportantConversation(entry)) {
            // Old approach: cache only if important conversation.
            if (showPeopleAvatar) {
            if (descriptor.type == StatusBarIcon.Type.PeopleAvatar) {
                entry.icons.peopleAvatarDescriptor = descriptor
            } else {
                entry.icons.smallIconDescriptor = descriptor
@@ -312,7 +305,10 @@ constructor(
        }
    }

    private fun Icon.toStatusBarIcon(entry: NotificationEntry): StatusBarIcon {
    private fun Icon.toStatusBarIcon(
        entry: NotificationEntry,
        type: StatusBarIcon.Type
    ): StatusBarIcon {
        val n = entry.sbn.notification
        return StatusBarIcon(
            entry.sbn.user,
@@ -320,7 +316,8 @@ constructor(
            /* icon = */ this,
            n.iconLevel,
            n.number,
            iconBuilder.getIconContentDescription(n)
            iconBuilder.getIconContentDescription(n),
            type
        )
    }

@@ -365,7 +362,8 @@ constructor(
            // Once we have the icon, updating it should happen on the main thread.
            if (icon != null) {
                withContext(mainCoroutineContext) {
                    val iconDescriptor = icon.toStatusBarIcon(entry)
                    val iconDescriptor =
                        icon.toStatusBarIcon(entry, StatusBarIcon.Type.PeopleAvatar)

                    // Cache the value
                    entry.icons.peopleAvatarDescriptor = iconDescriptor
Loading