From f465ea0d6a7f610009e55f7453af257228398b99 Mon Sep 17 00:00:00 2001 From: Bryce Lee Date: Tue, 2 May 2017 16:58:22 -0700 Subject: [PATCH] Disallow starting activities without new task from non-activity context. A change to the conditions to allow such behavior when a task id was specified now allows for all non-activity contexts to start activities as long as no options bundle is specified. This changelist corrects the condition. Change-Id: Ifaff4fc6d875ba74ec532b3e3ce91c6aa3ca3362 Fixes: 32625589 Test: atest CtsActivityManagerDeviceTestCases:StartActivityTests --- core/java/android/app/ContextImpl.java | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/core/java/android/app/ContextImpl.java b/core/java/android/app/ContextImpl.java index 165343058a2aa..4914ffaf89124 100644 --- a/core/java/android/app/ContextImpl.java +++ b/core/java/android/app/ContextImpl.java @@ -872,13 +872,19 @@ class ContextImpl extends Context { // Calling start activity from outside an activity without FLAG_ACTIVITY_NEW_TASK is // generally not allowed, except if the caller specifies the task id the activity should - // be launched in. - if ((intent.getFlags()&Intent.FLAG_ACTIVITY_NEW_TASK) == 0 - && options != null && ActivityOptions.fromBundle(options).getLaunchTaskId() == -1) { + // be launched in. A bug was existed between N and O-MR1 which allowed this to work. We + // maintain this for backwards compatibility. + final int targetSdkVersion = getApplicationInfo().targetSdkVersion; + + if ((intent.getFlags() & Intent.FLAG_ACTIVITY_NEW_TASK) == 0 + && (targetSdkVersion < Build.VERSION_CODES.N + || targetSdkVersion >= Build.VERSION_CODES.P) + && (options == null + || ActivityOptions.fromBundle(options).getLaunchTaskId() == -1)) { throw new AndroidRuntimeException( "Calling startActivity() from outside of an Activity " - + " context requires the FLAG_ACTIVITY_NEW_TASK flag." - + " Is this really what you want?"); + + " context requires the FLAG_ACTIVITY_NEW_TASK flag." + + " Is this really what you want?"); } mMainThread.getInstrumentation().execStartActivity( getOuterContext(), mMainThread.getApplicationThread(), null,