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); + } } }