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

Commit 9153396a authored by d34d's avatar d34d
Browse files

SysUI: Don't let rogue themes ruin notifications

If we encounter an exception when inflating a notification's views,
and a theme is applied, we should make a second attempt at inflating
the notification without a theme applied in case the theme cause the
issue.  If the exception still occurs, we treat it like we normally
do and allow the app to be killed for posting a bad notification.

Change-Id: I444cf6c78ee43e2978201880957c53eb08e6966d
TICKET: CYNGNOS-2892
parent 5f8d3f81
Loading
Loading
Loading
Loading
+43 −5
Original line number Diff line number Diff line
@@ -1296,6 +1296,20 @@ public abstract class BaseStatusBar extends SystemUI implements
    }

    protected boolean inflateViews(Entry entry, ViewGroup parent) {
        final StatusBarNotification sbn = entry.notification;
        String themePackageName = mCurrentTheme != null
                ? mCurrentTheme.getOverlayPkgNameForApp(sbn.getPackageName()) : null;
        boolean inflated = inflateViews(entry, parent, true);
        if (!inflated && themePackageName != null
                && !ThemeConfig.SYSTEM_DEFAULT.equals(themePackageName)) {
            Log.w(TAG, "Couldn't expand themed RemoteViews, trying unthemed for: " + sbn);
            inflated = inflateViews(entry, mStackScroller, false);
        }

        return inflated;
    }

    protected boolean inflateViews(Entry entry, ViewGroup parent, boolean isThemeable) {
        PackageManager pmUser = getPackageManagerForUser(
                entry.notification.getUser().getIdentifier());

@@ -1372,10 +1386,12 @@ public abstract class BaseStatusBar extends SystemUI implements
        View contentViewLocal = null;
        View bigContentViewLocal = null;
        View headsUpContentViewLocal = null;
        String themePackageName = mCurrentTheme != null
                ? mCurrentTheme.getOverlayPkgNameForApp(sbn.getPackageName()) : null;
        String statusBarThemePackageName = mCurrentTheme != null
                ? mCurrentTheme.getOverlayForStatusBar() : null;
        String themePackageName = (isThemeable && mCurrentTheme != null)
                ? mCurrentTheme.getOverlayPkgNameForApp(sbn.getPackageName())
                : ThemeConfig.SYSTEM_DEFAULT;
        String statusBarThemePackageName = (isThemeable && mCurrentTheme != null)
                ? mCurrentTheme.getOverlayForStatusBar()
                : ThemeConfig.SYSTEM_DEFAULT;

        try {
            contentViewLocal = contentView.apply(
@@ -1466,8 +1482,10 @@ public abstract class BaseStatusBar extends SystemUI implements
        }

        if (publicViewLocal == null) {
            final Context layoutContext = isThemeable ? mContext
                    : maybeGetThemedContext(mContext, ThemeConfig.SYSTEM_DEFAULT);
            // Add a basic notification template
            publicViewLocal = LayoutInflater.from(mContext).inflate(
            publicViewLocal = LayoutInflater.from(layoutContext).inflate(
                    R.layout.notification_public_default,
                    contentContainerPublic, false);
            publicViewLocal.setIsRootNamespace(true);
@@ -2324,4 +2342,24 @@ public abstract class BaseStatusBar extends SystemUI implements
            mAssistManager.startAssist(args);
        }
    }

    /**
     * Returns a context with the given theme applied or the original context if we fail to get a
     * themed context.
     */
    private Context maybeGetThemedContext(Context context, String themePkg) {
        Context themedContext;
        try {
            ApplicationInfo ai = context.getPackageManager().getApplicationInfo(
                    context.getPackageName(), 0);
            themedContext = context.createApplicationContext(ai, themePkg,
                    0);
        } catch (PackageManager.NameNotFoundException e) {
            themedContext = null;
        }
        if (themedContext == null) {
            themedContext = context;
        }
        return themedContext;
    }
}