Exposed some testAPIs in ActivityManager

- Added the following testAPIs to replace the usage
of adb shell commands in tests:
* stopUser and ACTION_USER_STOPPED
* waitForBroadcastIdle

- Changed the documentation for switchUser to correctly
reflect that it accepts CREATE_USERS

Test: verfied that tests can access the APIs
Bug: 181030374
Change-Id: I92b92471a18cf97a118d3f1946d6e06c277a76f1
This commit is contained in:
kholoud mohamed
2021-03-15 14:38:02 +00:00
parent 51755dc698
commit 23c36dbe21
6 changed files with 64 additions and 8 deletions

View File

@@ -407,7 +407,7 @@ package android.app {
method @RequiresPermission(android.Manifest.permission.RESTRICTED_VR_ACCESS) public static void setPersistentVrThread(int);
method @RequiresPermission(anyOf={android.Manifest.permission.MANAGE_USERS, android.Manifest.permission.INTERACT_ACROSS_USERS_FULL}) public boolean startProfile(@NonNull android.os.UserHandle);
method @RequiresPermission(anyOf={android.Manifest.permission.MANAGE_USERS, android.Manifest.permission.INTERACT_ACROSS_USERS_FULL}) public boolean stopProfile(@NonNull android.os.UserHandle);
method @RequiresPermission(android.Manifest.permission.MANAGE_USERS) public boolean switchUser(@NonNull android.os.UserHandle);
method @RequiresPermission(anyOf={android.Manifest.permission.MANAGE_USERS, android.Manifest.permission.CREATE_USERS}) public boolean switchUser(@NonNull android.os.UserHandle);
}
public static interface ActivityManager.OnUidImportanceListener {

View File

@@ -104,7 +104,9 @@ package android.app {
method @RequiresPermission(android.Manifest.permission.RESET_APP_ERRORS) public void resetAppErrors();
method public static void resumeAppSwitches() throws android.os.RemoteException;
method @RequiresPermission(android.Manifest.permission.CHANGE_CONFIGURATION) public void scheduleApplicationInfoChanged(java.util.List<java.lang.String>, int);
method @RequiresPermission(android.Manifest.permission.INTERACT_ACROSS_USERS_FULL) public boolean stopUser(int, boolean);
method @RequiresPermission(android.Manifest.permission.CHANGE_CONFIGURATION) public boolean updateMccMncConfiguration(@NonNull String, @NonNull String);
method @RequiresPermission(android.Manifest.permission.DUMP) public void waitForBroadcastIdle();
field public static final long DROP_CLOSE_SYSTEM_DIALOGS = 174664120L; // 0xa6929b8L
field public static final long LOCK_DOWN_CLOSE_SYSTEM_DIALOGS = 174664365L; // 0xa692aadL
field public static final int PROCESS_CAPABILITY_ALL = 15; // 0xf
@@ -709,6 +711,10 @@ package android.content {
method public int getDisplayId();
}
public class Intent implements java.lang.Cloneable android.os.Parcelable {
field public static final String ACTION_USER_STOPPED = "android.intent.action.USER_STOPPED";
}
public class SyncAdapterType implements android.os.Parcelable {
method @Nullable public String getPackageName();
}

View File

@@ -31,6 +31,7 @@ import android.annotation.RequiresPermission;
import android.annotation.SystemApi;
import android.annotation.SystemService;
import android.annotation.TestApi;
import android.annotation.UserIdInt;
import android.compat.annotation.ChangeId;
import android.compat.annotation.EnabledSince;
import android.compat.annotation.UnsupportedAppUsage;
@@ -4048,7 +4049,8 @@ public class ActivityManager {
* @hide
*/
@SystemApi
@RequiresPermission(android.Manifest.permission.MANAGE_USERS)
@RequiresPermission(anyOf = {android.Manifest.permission.MANAGE_USERS,
android.Manifest.permission.CREATE_USERS})
public boolean switchUser(@NonNull UserHandle user) {
if (user == null) {
throw new IllegalArgumentException("UserHandle cannot be null.");
@@ -4144,6 +4146,25 @@ public class ActivityManager {
}
}
/**
* Stops the given {@code userId}.
*
* @hide
*/
@TestApi
@RequiresPermission(android.Manifest.permission.INTERACT_ACROSS_USERS_FULL)
public boolean stopUser(@UserIdInt int userId, boolean force) {
if (userId == UserHandle.USER_SYSTEM) {
return false;
}
try {
return USER_OP_SUCCESS == getService().stopUser(
userId, force, /* callback= */ null);
} catch (RemoteException e) {
throw e.rethrowFromSystemServer();
}
}
/** {@hide} */
public static final int FLAG_OR_STOPPED = 1 << 0;
/** {@hide} */
@@ -4810,6 +4831,21 @@ public class ActivityManager {
}
}
/**
* Blocks until all broadcast queues become idle.
*
* @hide
*/
@TestApi
@RequiresPermission(android.Manifest.permission.DUMP)
public void waitForBroadcastIdle() {
try {
getService().waitForBroadcastIdle();
} catch (RemoteException e) {
e.rethrowFromSystemServer();
}
}
/**
* A subset of immutable pending intent information suitable for caching on the client side.
*

View File

@@ -709,4 +709,7 @@ interface IActivityManager {
ParceledListSlice queryIntentComponentsForIntentSender(in IIntentSender sender, int matchFlags);
int getUidProcessCapabilities(int uid, in String callingPackage);
/** Blocks until all broadcast queues become idle. */
void waitForBroadcastIdle();
}

View File

@@ -28,6 +28,7 @@ import android.annotation.SdkConstant;
import android.annotation.SdkConstant.SdkConstantType;
import android.annotation.SuppressLint;
import android.annotation.SystemApi;
import android.annotation.TestApi;
import android.app.AppGlobals;
import android.compat.annotation.UnsupportedAppUsage;
import android.content.pm.ActivityInfo;
@@ -3771,6 +3772,7 @@ public class Intent implements Parcelable, Cloneable {
* has just been stopped (which is no longer running).
* @hide
*/
@TestApi
public static final String ACTION_USER_STOPPED =
"android.intent.action.USER_STOPPED";

View File

@@ -16147,7 +16147,12 @@ public class ActivityManagerService extends IActivityManager.Stub
}
}
public void waitForBroadcastIdle(PrintWriter pw) {
@Override
public void waitForBroadcastIdle() {
waitForBroadcastIdle(/* printWriter= */ null);
}
public void waitForBroadcastIdle(@Nullable PrintWriter pw) {
enforceCallingPermission(permission.DUMP, "waitForBroadcastIdle()");
while (true) {
boolean idle = true;
@@ -16155,9 +16160,11 @@ public class ActivityManagerService extends IActivityManager.Stub
for (BroadcastQueue queue : mBroadcastQueues) {
if (!queue.isIdle()) {
final String msg = "Waiting for queue " + queue + " to become idle...";
pw.println(msg);
pw.println(queue.describeState());
pw.flush();
if (pw != null) {
pw.println(msg);
pw.println(queue.describeState());
pw.flush();
}
Slog.v(TAG, msg);
queue.cancelDeferrals();
idle = false;
@@ -16167,8 +16174,10 @@ public class ActivityManagerService extends IActivityManager.Stub
if (idle) {
final String msg = "All broadcast queues are idle!";
pw.println(msg);
pw.flush();
if (pw != null) {
pw.println(msg);
pw.flush();
}
Slog.v(TAG, msg);
return;
} else {