From 184bfe029ec7053418696517987b164a7a224e36 Mon Sep 17 00:00:00 2001 From: Adrian Roos Date: Thu, 3 Mar 2016 13:41:44 -0800 Subject: [PATCH] Fix and optimize stripForDelivery Makes it work for style-less notifications. Also prevents an unnecessary clone if no stripping is needed. Bug: 27386126 Change-Id: Ia1a193a39a9b0764890813d7edf68b1924eb799f --- core/java/android/app/Notification.java | 58 ++++++++++++------- .../java/android/app/NotificationManager.java | 3 +- 2 files changed, 38 insertions(+), 23 deletions(-) diff --git a/core/java/android/app/Notification.java b/core/java/android/app/Notification.java index 2c34371a12c1b..333c781f7c6be 100644 --- a/core/java/android/app/Notification.java +++ b/core/java/android/app/Notification.java @@ -3597,37 +3597,53 @@ public class Notification implements Parcelable } /** + * Removes RemoteViews that were created for compatibility from {@param n}, if they did not + * change. + * + * @return {@param n}, if no stripping is needed, otherwise a stripped clone of {@param n}. + * * @hide */ - public static void stripForDelivery(Notification n) { + public static Notification maybeCloneStrippedForDelivery(Notification n) { String templateClass = n.extras.getString(EXTRA_TEMPLATE); - if (TextUtils.isEmpty(templateClass)) { - return; - } + // Only strip views for known Styles because we won't know how to // re-create them otherwise. - if (getNotificationStyleClass(templateClass) == null) { - return; + if (!TextUtils.isEmpty(templateClass) + && getNotificationStyleClass(templateClass) == null) { + return n; } - // Get rid of unmodified BuilderRemoteViews. - if (n.contentView instanceof BuilderRemoteViews && + + // Only strip unmodified BuilderRemoteViews. + boolean stripContentView = n.contentView instanceof BuilderRemoteViews && n.extras.getInt(EXTRA_REBUILD_CONTENT_VIEW_ACTION_COUNT, -1) == - n.contentView.getSequenceNumber()) { - n.contentView = null; - n.extras.remove(EXTRA_REBUILD_CONTENT_VIEW_ACTION_COUNT); - } - if (n.bigContentView instanceof BuilderRemoteViews && + n.contentView.getSequenceNumber(); + boolean stripBigContentView = n.bigContentView instanceof BuilderRemoteViews && n.extras.getInt(EXTRA_REBUILD_BIG_CONTENT_VIEW_ACTION_COUNT, -1) == - n.bigContentView.getSequenceNumber()) { - n.bigContentView = null; - n.extras.remove(EXTRA_REBUILD_BIG_CONTENT_VIEW_ACTION_COUNT); - } - if (n.headsUpContentView instanceof BuilderRemoteViews && + n.bigContentView.getSequenceNumber(); + boolean stripHeadsUpContentView = n.headsUpContentView instanceof BuilderRemoteViews && n.extras.getInt(EXTRA_REBUILD_HEADS_UP_CONTENT_VIEW_ACTION_COUNT, -1) == - n.headsUpContentView.getSequenceNumber()) { - n.headsUpContentView = null; - n.extras.remove(EXTRA_REBUILD_HEADS_UP_CONTENT_VIEW_ACTION_COUNT); + n.headsUpContentView.getSequenceNumber(); + + // Nothing to do here, no need to clone. + if (!stripContentView && !stripBigContentView && !stripHeadsUpContentView) { + return n; } + + Notification clone = n.clone(); + if (stripContentView) { + clone.contentView = null; + clone.extras.remove(EXTRA_REBUILD_CONTENT_VIEW_ACTION_COUNT); + } + if (stripBigContentView) { + clone.bigContentView = null; + clone.extras.remove(EXTRA_REBUILD_BIG_CONTENT_VIEW_ACTION_COUNT); + } + if (stripHeadsUpContentView) { + clone.headsUpContentView = null; + clone.extras.remove(EXTRA_REBUILD_HEADS_UP_CONTENT_VIEW_ACTION_COUNT); + } + return clone; } private int getBaseLayoutResource() { diff --git a/core/java/android/app/NotificationManager.java b/core/java/android/app/NotificationManager.java index 057a4e9439710..9687984ad8048 100644 --- a/core/java/android/app/NotificationManager.java +++ b/core/java/android/app/NotificationManager.java @@ -259,8 +259,7 @@ public class NotificationManager } } if (localLOGV) Log.v(TAG, pkg + ": notify(" + id + ", " + notification + ")"); - final Notification copy = notification.clone(); - Builder.stripForDelivery(copy); + final Notification copy = Builder.maybeCloneStrippedForDelivery(notification); try { service.enqueueNotificationWithTag(pkg, mContext.getOpPackageName(), tag, id, copy, idOut, user.getIdentifier());