From bf06fa047954e85cf9df554d0b3d4eb4b19a950e Mon Sep 17 00:00:00 2001 From: Adam Powell Date: Fri, 30 May 2014 12:32:18 -0700 Subject: Compatibility fix for apps that construct Notification.Builders early MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Some apps in the wild are known to construct a Notification.Builder from within their Activity constructor, passing said Activity instance as the Context parameter. However at this point in the Activity's lifecycle the ContextWrapper's base context has not yet been populated and most calls to Context methods will fail. Even though this was technically undefined behavior in the past, as apps have little to no way to guess at these internal implementation details it's probably best to preserve old expectations. Add a note in Notification.Builder for future framework developers and remove the offending call - the associated field was never used anyway. ಠ_ಠ Change-Id: I4cf3df10be14739babc8ae9a969d261a7ecf8beb --- core/java/android/app/Notification.java | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) (limited to 'core') diff --git a/core/java/android/app/Notification.java b/core/java/android/app/Notification.java index 90aeaae..8dba1dc 100644 --- a/core/java/android/app/Notification.java +++ b/core/java/android/app/Notification.java @@ -1671,7 +1671,6 @@ public class Notification implements Parcelable private Notification mPublicVersion = null; private final NotificationColorUtil mColorUtil; private ArrayList mPeople; - private boolean mPreQuantum; private int mColor = COLOR_DEFAULT; /** @@ -1694,6 +1693,15 @@ public class Notification implements Parcelable * object. */ public Builder(Context context) { + /* + * Important compatibility note! + * Some apps out in the wild create a Notification.Builder in their Activity subclass + * constructor for later use. At this point Activities - themselves subclasses of + * ContextWrapper - do not have their inner Context populated yet. This means that + * any calls to Context methods from within this constructor can cause NPEs in existing + * apps. Any data populated from mContext should therefore be populated lazily to + * preserve compatibility. + */ mContext = context; // Set defaults to match the defaults of a Notification @@ -1702,7 +1710,6 @@ public class Notification implements Parcelable mPriority = PRIORITY_DEFAULT; mPeople = new ArrayList(); - mPreQuantum = context.getApplicationInfo().targetSdkVersion < Build.VERSION_CODES.L; mColorUtil = NotificationColorUtil.getInstance(); } -- cgit v1.1