From 6fa883e146e96da4f446907b0781eb3194c4c9a1 Mon Sep 17 00:00:00 2001 From: Ashwini Oruganti Date: Mon, 5 Oct 2020 11:50:03 -0700 Subject: [PATCH] Relax the PI mutability flag requirement for apps under instrumentation This gives us some buffer time to help transition all the test apps. See go/immutable-pendingintents for more context. Bug: 160794467 Test: TH Change-Id: I2265dc8a5e86ac76e615e659169f8891e8a7ed75 --- core/java/android/app/Instrumentation.java | 14 ++++++++++++++ core/java/android/app/PendingIntent.java | 13 ++++++++++++- 2 files changed, 26 insertions(+), 1 deletion(-) diff --git a/core/java/android/app/Instrumentation.java b/core/java/android/app/Instrumentation.java index e9d63d2bc7882..8d1076ea12779 100644 --- a/core/java/android/app/Instrumentation.java +++ b/core/java/android/app/Instrumentation.java @@ -138,6 +138,20 @@ public class Instrumentation { } } + /** + * Returns if it is being called in an instrumentation environment. + * + * @hide + */ + public boolean isInstrumenting() { + // Check if we have an instrumentation context, as init should only get called by + // the system in startup processes that are being instrumented. + if (mInstrContext == null) { + return false; + } + return true; + } + /** * Called when the instrumentation is starting, before any application code * has been loaded. Usually this will be implemented to simply call diff --git a/core/java/android/app/PendingIntent.java b/core/java/android/app/PendingIntent.java index 269ccb169774c..b055c6768e7ba 100644 --- a/core/java/android/app/PendingIntent.java +++ b/core/java/android/app/PendingIntent.java @@ -40,6 +40,7 @@ import android.os.RemoteException; import android.os.UserHandle; import android.util.AndroidException; import android.util.ArraySet; +import android.util.Log; import android.util.proto.ProtoOutputStream; import com.android.internal.os.IResultReceiver; @@ -107,6 +108,7 @@ import java.lang.annotation.RetentionPolicy; * FLAG_ONE_SHOT, both FLAG_ONE_SHOT and FLAG_NO_CREATE need to be supplied. */ public final class PendingIntent implements Parcelable { + private static final String TAG = "PendingIntent"; private final IIntentSender mTarget; private IResultReceiver mCancelReceiver; private IBinder mWhitelistToken; @@ -350,9 +352,18 @@ public final class PendingIntent implements Parcelable { "Cannot set both FLAG_IMMUTABLE and FLAG_MUTABLE for PendingIntent"); } + // TODO(b/178092897) Remove the below instrumentation check and enforce + // the explicit mutability requirement for apps under instrumentation. + ActivityThread thread = ActivityThread.currentActivityThread(); + Instrumentation mInstrumentation = thread.getInstrumentation(); + if (Compatibility.isChangeEnabled(PENDING_INTENT_EXPLICIT_MUTABILITY_REQUIRED) && !flagImmutableSet && !flagMutableSet) { - throw new IllegalArgumentException(msg); + if (!mInstrumentation.isInstrumenting()) { + throw new IllegalArgumentException(msg); + } else { + Log.e(TAG, msg); + } } }