summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--api/system-current.txt1
-rw-r--r--core/java/android/app/ActivityManager.java12
-rw-r--r--services/tests/servicestests/src/com/android/server/pm/UserManagerTest.java33
3 files changed, 41 insertions, 5 deletions
diff --git a/api/system-current.txt b/api/system-current.txt
index f4eb2e99b208..d488b864d1f0 100644
--- a/api/system-current.txt
+++ b/api/system-current.txt
@@ -298,6 +298,7 @@ package android.app {
method public void killUid(int, java.lang.String);
method public void removeOnUidImportanceListener(android.app.ActivityManager.OnUidImportanceListener);
method public static void setPersistentVrThread(int);
+ method public boolean switchUser(android.os.UserHandle);
}
public static abstract interface ActivityManager.OnUidImportanceListener {
diff --git a/core/java/android/app/ActivityManager.java b/core/java/android/app/ActivityManager.java
index 1cf042fd12b4..84c778502393 100644
--- a/core/java/android/app/ActivityManager.java
+++ b/core/java/android/app/ActivityManager.java
@@ -3676,6 +3676,18 @@ public class ActivityManager {
}
/**
+ * Returns whether switching to provided user was successful.
+ *
+ * @param user the user to switch to.
+ * @hide
+ */
+ @SystemApi
+ @RequiresPermission(android.Manifest.permission.MANAGE_USERS)
+ public boolean switchUser(UserHandle user) {
+ return switchUser(user.getIdentifier());
+ }
+
+ /**
* Logs out current current foreground user by switching to the system user and stopping the
* user being switched from.
* @hide
diff --git a/services/tests/servicestests/src/com/android/server/pm/UserManagerTest.java b/services/tests/servicestests/src/com/android/server/pm/UserManagerTest.java
index c639c28c5794..823b7a5ee545 100644
--- a/services/tests/servicestests/src/com/android/server/pm/UserManagerTest.java
+++ b/services/tests/servicestests/src/com/android/server/pm/UserManagerTest.java
@@ -527,9 +527,21 @@ public class UserManagerTest extends AndroidTestCase {
UserInfo user = createUser("User", 0);
assertNotNull(user);
// Switch to the user just created.
- switchUser(user.id);
+ switchUser(user.id, null, true);
// Switch back to the starting user.
- switchUser(startUser);
+ switchUser(startUser, null, true);
+ }
+
+ @LargeTest
+ public void testSwitchUserByHandle() {
+ ActivityManager am = getContext().getSystemService(ActivityManager.class);
+ final int startUser = am.getCurrentUser();
+ UserInfo user = createUser("User", 0);
+ assertNotNull(user);
+ // Switch to the user just created.
+ switchUser(-1, user.getUserHandle(), false);
+ // Switch back to the starting user.
+ switchUser(-1, UserHandle.of(startUser), false);
}
@MediumTest
@@ -567,10 +579,20 @@ public class UserManagerTest extends AndroidTestCase {
}
}
- private void switchUser(int userId) {
+ /**
+ * @param userId value will be used to call switchUser(int) only if ignoreHandle is false.
+ * @param user value will be used to call switchUser(UserHandle) only if ignoreHandle is true.
+ * @param ignoreHandle if true, switchUser(int) will be called with the provided userId,
+ * else, switchUser(UserHandle) will be called with the provided user.
+ */
+ private void switchUser(int userId, UserHandle user, boolean ignoreHandle) {
synchronized (mUserSwitchLock) {
ActivityManager am = getContext().getSystemService(ActivityManager.class);
- am.switchUser(userId);
+ if (ignoreHandle) {
+ am.switchUser(userId);
+ } else {
+ am.switchUser(user);
+ }
long time = System.currentTimeMillis();
try {
mUserSwitchLock.wait(SWITCH_USER_TIMEOUT_MILLIS);
@@ -579,7 +601,8 @@ public class UserManagerTest extends AndroidTestCase {
return;
}
if (System.currentTimeMillis() - time > SWITCH_USER_TIMEOUT_MILLIS) {
- fail("Timeout waiting for the user switch to u" + userId);
+ fail("Timeout waiting for the user switch to u"
+ + (ignoreHandle ? userId : user.getIdentifier()));
}
}
}