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

Commit 06bceb95 authored by Ioana Alexandru's avatar Ioana Alexandru
Browse files

Avoid using recoverBuilder in contentDescForNotif

Also remove unnecessary permission check (since the substitute app name
extra is being stripped in NotificationManagerService already).

Fix: 281629927
Test: NotificationContentDescriptionTest + verified with TalkBack
Flag: NONE
Change-Id: Ib6ac67f8016b19f8a948ea852ee4dd52db3371ac
parent 7a92cfa3
Loading
Loading
Loading
Loading
+38 −28
Original line number Diff line number Diff line
@@ -3018,6 +3018,43 @@ public class Notification implements Parcelable
        }
    }
    /**
     * @hide
     */
    public String loadHeaderAppName(Context context) {
        CharSequence name = null;
        // Check if there is a non-empty substitute app name and return that.
        if (extras.containsKey(EXTRA_SUBSTITUTE_APP_NAME)) {
            name = extras.getString(EXTRA_SUBSTITUTE_APP_NAME);
            if (!TextUtils.isEmpty(name)) {
                return name.toString();
            }
        }
        // If not, try getting the app info from extras.
        if (context == null) {
            return null;
        }
        final PackageManager pm = context.getPackageManager();
        if (TextUtils.isEmpty(name)) {
            if (extras.containsKey(EXTRA_BUILDER_APPLICATION_INFO)) {
                final ApplicationInfo info = extras.getParcelable(EXTRA_BUILDER_APPLICATION_INFO,
                        ApplicationInfo.class);
                if (info != null) {
                    name = pm.getApplicationLabel(info);
                }
            }
        }
        // If that's still empty, use the one from the context directly.
        if (TextUtils.isEmpty(name)) {
            name = pm.getApplicationLabel(context.getApplicationInfo());
        }
        // If there's still nothing, ¯\_(ツ)_/¯
        if (TextUtils.isEmpty(name)) {
            return null;
        }
        return name.toString();
    }
    /**
     * Removes heavyweight parts of the Notification object for archival or for sending to
     * listeners when the full contents are not necessary.
@@ -5760,34 +5797,7 @@ public class Notification implements Parcelable
         */
        @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.R, trackingBug = 170729553)
        public String loadHeaderAppName() {
            CharSequence name = null;
            final PackageManager pm = mContext.getPackageManager();
            if (mN.extras.containsKey(EXTRA_SUBSTITUTE_APP_NAME)) {
                // only system packages which lump together a bunch of unrelated stuff
                // may substitute a different name to make the purpose of the
                // notification more clear. the correct package label should always
                // be accessible via SystemUI.
                final String pkg = mContext.getPackageName();
                final String subName = mN.extras.getString(EXTRA_SUBSTITUTE_APP_NAME);
                if (PackageManager.PERMISSION_GRANTED == pm.checkPermission(
                        android.Manifest.permission.SUBSTITUTE_NOTIFICATION_APP_NAME, pkg)) {
                    name = subName;
                } else {
                    Log.w(TAG, "warning: pkg "
                            + pkg + " attempting to substitute app name '" + subName
                            + "' without holding perm "
                            + android.Manifest.permission.SUBSTITUTE_NOTIFICATION_APP_NAME);
                }
            }
            if (TextUtils.isEmpty(name)) {
                name = pm.getApplicationLabel(mContext.getApplicationInfo());
            }
            if (TextUtils.isEmpty(name)) {
                // still nothing?
                return null;
            }
            return String.valueOf(name);
            return mN.loadHeaderAppName(mContext);
        }
        /**
+2 −23
Original line number Diff line number Diff line
@@ -20,35 +20,14 @@ package com.android.systemui.statusbar.notification

import android.app.Notification
import android.content.Context
import android.content.pm.ApplicationInfo
import android.text.TextUtils
import android.util.Log
import androidx.annotation.MainThread
import com.android.systemui.res.R

/**
 * Returns accessibility content description for a given notification.
 *
 * NOTE: This is a relatively slow call.
 */
/** Returns accessibility content description for a given notification. */
@MainThread
fun contentDescForNotification(c: Context, n: Notification?): CharSequence {
    var appName = ""
    try {
        val builder = Notification.Builder.recoverBuilder(c, n)
        appName = builder.loadHeaderAppName()
    } catch (e: RuntimeException) {
        Log.e("ContentDescription", "Unable to recover builder", e)
        // Trying to get the app name from the app info instead.
        val appInfo =
            n?.extras?.getParcelable(
                Notification.EXTRA_BUILDER_APPLICATION_INFO,
                ApplicationInfo::class.java
            )
        if (appInfo != null) {
            appName = appInfo.loadLabel(c.packageManager).toString()
        }
    }
    val appName = n?.loadHeaderAppName(c) ?: ""
    val title = n?.extras?.getCharSequence(Notification.EXTRA_TITLE)
    val text = n?.extras?.getCharSequence(Notification.EXTRA_TEXT)
    val ticker = n?.tickerText