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

Commit 5081c0de authored by Adrian Roos's avatar Adrian Roos
Browse files

Fix crash with decorated custom notifications

Clones notifications before adding them to remote views so
the original stays parcelable on its own.

Also prevents the compatibility inflation from triggering when
an app uses a decorating style to prevent recursive wrapping.
Those styles only exist on N and later anyways.

Also fixes the compatibility inflation in listeners.

Bug: 27368615
Change-Id: Iedf3036bf315dd9c7b476c7e8bcce57de5b5c9c8
parent d23947e1
Loading
Loading
Loading
Loading
+12 −1
Original line number Original line Diff line number Diff line
@@ -3525,7 +3525,8 @@ public class Notification implements Parcelable
                mStyle.buildStyled(mN);
                mStyle.buildStyled(mN);
            }
            }


            if (mContext.getApplicationInfo().targetSdkVersion < Build.VERSION_CODES.N) {
            if (mContext.getApplicationInfo().targetSdkVersion < Build.VERSION_CODES.N
                    && (mStyle == null || !mStyle.displayCustomViewInline())) {
                if (mN.contentView == null) {
                if (mN.contentView == null) {
                    mN.contentView = createContentView();
                    mN.contentView = createContentView();
                    mN.extras.putInt(EXTRA_REBUILD_CONTENT_VIEW_ACTION_COUNT,
                    mN.extras.putInt(EXTRA_REBUILD_CONTENT_VIEW_ACTION_COUNT,
@@ -4539,6 +4540,11 @@ public class Notification implements Parcelable
        private void buildIntoRemoteViewContent(RemoteViews remoteViews,
        private void buildIntoRemoteViewContent(RemoteViews remoteViews,
                RemoteViews customContent) {
                RemoteViews customContent) {
            remoteViews.removeAllViews(R.id.notification_main_column);
            remoteViews.removeAllViews(R.id.notification_main_column);
            // Need to clone customContent before adding, because otherwise it can no longer be
            // parceled independently of remoteViews.
            if (customContent != null) {
                customContent = customContent.clone();
            }
            remoteViews.addView(R.id.notification_main_column, customContent);
            remoteViews.addView(R.id.notification_main_column, customContent);
            // also update the end margin if there is an image
            // also update the end margin if there is an image
            int endMargin = mBuilder.mContext.getResources().getDimensionPixelSize(
            int endMargin = mBuilder.mContext.getResources().getDimensionPixelSize(
@@ -4643,6 +4649,11 @@ public class Notification implements Parcelable
        private RemoteViews buildIntoRemoteView(RemoteViews remoteViews, int id,
        private RemoteViews buildIntoRemoteView(RemoteViews remoteViews, int id,
                RemoteViews customContent) {
                RemoteViews customContent) {
            remoteViews.removeAllViews(id);
            remoteViews.removeAllViews(id);
            // Need to clone customContent before adding, because otherwise it can no longer be
            // parceled independently of remoteViews.
            if (customContent != null) {
                customContent = customContent.clone();
            }
            remoteViews.addView(id, customContent);
            remoteViews.addView(id, customContent);
            return remoteViews;
            return remoteViews;
        }
        }
+11 −3
Original line number Original line Diff line number Diff line
@@ -42,6 +42,7 @@ import android.os.ServiceManager;
import android.util.ArrayMap;
import android.util.ArrayMap;
import android.util.ArraySet;
import android.util.ArraySet;
import android.util.Log;
import android.util.Log;
import android.widget.RemoteViews;


import java.lang.annotation.Retention;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.RetentionPolicy;
@@ -754,9 +755,16 @@ public abstract class NotificationListenerService extends Service {
    private void maybePopulateRemoteViews(Notification notification) {
    private void maybePopulateRemoteViews(Notification notification) {
        if (getContext().getApplicationInfo().targetSdkVersion < Build.VERSION_CODES.N) {
        if (getContext().getApplicationInfo().targetSdkVersion < Build.VERSION_CODES.N) {
            Builder builder = Builder.recoverBuilder(getContext(), notification);
            Builder builder = Builder.recoverBuilder(getContext(), notification);
            notification.contentView = builder.createContentView();

            notification.bigContentView = builder.createBigContentView();
            // Some styles wrap Notification's contentView, bigContentView and headsUpContentView.
            notification.headsUpContentView = builder.createHeadsUpContentView();
            // First inflate them all, only then set them to avoid recursive wrapping.
            RemoteViews content = builder.createContentView();
            RemoteViews big = builder.createBigContentView();
            RemoteViews headsUp = builder.createHeadsUpContentView();

            notification.contentView = content;
            notification.bigContentView = big;
            notification.headsUpContentView = headsUp;
        }
        }
    }
    }