From c76767c21c85688335ac151eae666359981e8d3c Mon Sep 17 00:00:00 2001 From: Alan Stokes Date: Thu, 30 May 2019 09:33:47 +0100 Subject: [PATCH] Add an option to allow background starts for am broadcast. Add an option to "adb shell am broadcast" to whitelist the broadcast receiver to perform background activity starts. We could do this unconditionally, but it's useful to be able to test the behavior with and without the option. This is gated on android.permission.START_ACTIVITIES_FROM_BACKGROUND, which shell already has. Bug: 132439856 Test: Manually send broadcast with and without the flag. Change-Id: Iccd16e58c4f175bf3f5f1151dbe2bbf539489c33 --- .../server/am/ActivityManagerShellCommand.java | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/services/core/java/com/android/server/am/ActivityManagerShellCommand.java b/services/core/java/com/android/server/am/ActivityManagerShellCommand.java index cba9674d7360b..f7512f9d25e77 100644 --- a/services/core/java/com/android/server/am/ActivityManagerShellCommand.java +++ b/services/core/java/com/android/server/am/ActivityManagerShellCommand.java @@ -29,6 +29,7 @@ import android.app.ActivityManager; import android.app.ActivityOptions; import android.app.ActivityTaskManager; import android.app.AppGlobals; +import android.app.BroadcastOptions; import android.app.IActivityController; import android.app.IActivityManager; import android.app.IActivityTaskManager; @@ -148,6 +149,7 @@ final class ActivityManagerShellCommand extends ShellCommand { private int mTaskId; private boolean mIsTaskOverlay; private boolean mIsLockTask; + private BroadcastOptions mBroadcastOptions; final boolean mDumping; @@ -313,6 +315,7 @@ final class ActivityManagerShellCommand extends ShellCommand { mTaskId = INVALID_TASK_ID; mIsTaskOverlay = false; mIsLockTask = false; + mBroadcastOptions = null; return Intent.parseCommandArgs(this, new Intent.CommandOptionHandler() { @Override @@ -371,6 +374,11 @@ final class ActivityManagerShellCommand extends ShellCommand { mIsTaskOverlay = true; } else if (opt.equals("--lock-task")) { mIsLockTask = true; + } else if (opt.equals("--allow-background-activity-starts")) { + if (mBroadcastOptions == null) { + mBroadcastOptions = BroadcastOptions.makeBasic(); + } + mBroadcastOptions.setBackgroundActivityStartsAllowed(true); } else { return false; } @@ -717,8 +725,9 @@ final class ActivityManagerShellCommand extends ShellCommand { : new String[] {mReceiverPermission}; pw.println("Broadcasting: " + intent); pw.flush(); + Bundle bundle = mBroadcastOptions == null ? null : mBroadcastOptions.toBundle(); mInterface.broadcastIntent(null, intent, null, receiver, 0, null, null, requiredPermissions, - android.app.AppOpsManager.OP_NONE, null, true, false, mUserId); + android.app.AppOpsManager.OP_NONE, bundle, true, false, mUserId); receiver.waitForFinish(); return 0; } @@ -2967,6 +2976,8 @@ final class ActivityManagerShellCommand extends ShellCommand { pw.println(" --user | all | current: Specify which user to send to; if not"); pw.println(" specified then send to all users."); pw.println(" --receiver-permission : Require receiver to hold permission."); + pw.println(" --allow-background-activity-starts: The receiver may start activities"); + pw.println(" even if in the background."); pw.println(" instrument [-r] [-e ] [-p ] [-w]"); pw.println(" [--user | current] [--no-hidden-api-checks]"); pw.println(" [--no-isolated-storage]");