Merge "Added UserManager#getUserSwitchability."

This commit is contained in:
Varun Shah
2019-03-12 01:39:32 +00:00
committed by Android (Google) Code Review
4 changed files with 97 additions and 9 deletions

View File

@@ -5602,7 +5602,6 @@ package android.os {
}
public class UserManager {
method @RequiresPermission(anyOf={android.Manifest.permission.MANAGE_USERS, android.Manifest.permission.INTERACT_ACROSS_USERS}, conditional=true) public boolean canSwitchUsers();
method @RequiresPermission(android.Manifest.permission.MANAGE_USERS) public void clearSeedAccountData();
method @Nullable @RequiresPermission(android.Manifest.permission.MANAGE_USERS) public android.os.UserHandle getProfileParent(@NonNull android.os.UserHandle);
method @RequiresPermission(android.Manifest.permission.MANAGE_USERS) public String getSeedAccountName();
@@ -5612,6 +5611,7 @@ package android.os {
method @Nullable @RequiresPermission(android.Manifest.permission.MANAGE_USERS) public android.graphics.Bitmap getUserIcon();
method @Deprecated @android.os.UserManager.UserRestrictionSource @RequiresPermission(android.Manifest.permission.MANAGE_USERS) public int getUserRestrictionSource(String, android.os.UserHandle);
method @RequiresPermission(android.Manifest.permission.MANAGE_USERS) public java.util.List<android.os.UserManager.EnforcingUser> getUserRestrictionSources(String, android.os.UserHandle);
method @RequiresPermission(allOf={android.Manifest.permission.READ_PHONE_STATE, android.Manifest.permission.MANAGE_USERS, android.Manifest.permission.INTERACT_ACROSS_USERS}, conditional=true) public int getUserSwitchability();
method public boolean hasRestrictedProfiles();
method @RequiresPermission(android.Manifest.permission.MANAGE_USERS) public boolean isAdminUser();
method @RequiresPermission(android.Manifest.permission.MANAGE_USERS) public boolean isGuestUser();
@@ -5629,6 +5629,10 @@ package android.os {
field public static final int RESTRICTION_SOURCE_DEVICE_OWNER = 2; // 0x2
field public static final int RESTRICTION_SOURCE_PROFILE_OWNER = 4; // 0x4
field public static final int RESTRICTION_SOURCE_SYSTEM = 1; // 0x1
field public static final int SWITCHABILITY_STATUS_OK = 0; // 0x0
field public static final int SWITCHABILITY_STATUS_SYSTEM_USER_LOCKED = 4; // 0x4
field public static final int SWITCHABILITY_STATUS_USER_IN_CALL = 1; // 0x1
field public static final int SWITCHABILITY_STATUS_USER_SWITCH_DISALLOWED = 2; // 0x2
}
public static final class UserManager.EnforcingUser implements android.os.Parcelable {

View File

@@ -25,6 +25,7 @@ import android.annotation.RequiresPermission;
import android.annotation.SystemApi;
import android.annotation.SystemService;
import android.annotation.TestApi;
import android.annotation.UnsupportedAppUsage;
import android.annotation.UserIdInt;
import android.annotation.WorkerThread;
import android.app.Activity;
@@ -1116,6 +1117,47 @@ public class UserManager {
*/
public static final int USER_CREATION_FAILED_NO_MORE_USERS = Activity.RESULT_FIRST_USER + 1;
/**
* Indicates that users are switchable.
* @hide
*/
@SystemApi
public static final int SWITCHABILITY_STATUS_OK = 0;
/**
* Indicated that the user is in a phone call.
* @hide
*/
@SystemApi
public static final int SWITCHABILITY_STATUS_USER_IN_CALL = 1 << 0;
/**
* Indicates that user switching is disallowed ({@link #DISALLOW_USER_SWITCH} is set).
* @hide
*/
@SystemApi
public static final int SWITCHABILITY_STATUS_USER_SWITCH_DISALLOWED = 1 << 1;
/**
* Indicates that the system user is locked and user switching is not allowed.
* @hide
*/
@SystemApi
public static final int SWITCHABILITY_STATUS_SYSTEM_USER_LOCKED = 1 << 2;
/**
* Result returned in {@link #getUserSwitchability()} indicating user swichability.
* @hide
*/
@Retention(RetentionPolicy.SOURCE)
@IntDef(flag = true, prefix = { "SWITCHABILITY_STATUS_" }, value = {
SWITCHABILITY_STATUS_OK,
SWITCHABILITY_STATUS_USER_IN_CALL,
SWITCHABILITY_STATUS_USER_SWITCH_DISALLOWED,
SWITCHABILITY_STATUS_SYSTEM_USER_LOCKED
})
public @interface UserSwitchabilityResult {}
/**
* Indicates user operation is successful.
*/
@@ -1238,14 +1280,13 @@ public class UserManager {
}
/**
* Returns whether switching users is currently allowed.
* <p>For instance switching users is not allowed if the current user is in a phone call,
* system user hasn't been unlocked yet, or {@link #DISALLOW_USER_SWITCH} is set.
* @deprecated use {@link #getUserSwitchability()} instead.
*
* @removed
* @hide
*/
@SystemApi
@RequiresPermission(anyOf = {android.Manifest.permission.MANAGE_USERS,
android.Manifest.permission.INTERACT_ACROSS_USERS}, conditional = true)
@Deprecated
@UnsupportedAppUsage
public boolean canSwitchUsers() {
boolean allowUserSwitchingWhenSystemUserLocked = Settings.Global.getInt(
mContext.getContentResolver(),
@@ -1258,6 +1299,42 @@ public class UserManager {
&& !isUserSwitchDisallowed;
}
/**
* Returns whether switching users is currently allowed.
* <p>
* Switching users is not allowed in the following cases:
* <li>the user is in a phone call</li>
* <li>{@link #DISALLOW_USER_SWITCH} is set</li>
* <li>system user hasn't been unlocked yet</li>
*
* @return A {@link UserSwitchabilityResult} flag indicating if the user is switchable.
* @hide
*/
@SystemApi
@RequiresPermission(allOf = {Manifest.permission.READ_PHONE_STATE,
android.Manifest.permission.MANAGE_USERS,
android.Manifest.permission.INTERACT_ACROSS_USERS}, conditional = true)
public @UserSwitchabilityResult int getUserSwitchability() {
final boolean allowUserSwitchingWhenSystemUserLocked = Settings.Global.getInt(
mContext.getContentResolver(),
Settings.Global.ALLOW_USER_SWITCHING_WHEN_SYSTEM_USER_LOCKED, 0) != 0;
final boolean systemUserUnlocked = isUserUnlocked(UserHandle.SYSTEM);
final TelephonyManager tm =
(TelephonyManager) mContext.getSystemService(Context.TELEPHONY_SERVICE);
int flags = SWITCHABILITY_STATUS_OK;
if (tm.getCallState() != TelephonyManager.CALL_STATE_IDLE) {
flags |= SWITCHABILITY_STATUS_USER_IN_CALL;
}
if (hasUserRestriction(DISALLOW_USER_SWITCH)) {
flags |= SWITCHABILITY_STATUS_USER_SWITCH_DISALLOWED;
}
if (!allowUserSwitchingWhenSystemUserLocked && !systemUserUnlocked) {
flags |= SWITCHABILITY_STATUS_SYSTEM_USER_LOCKED;
}
return flags;
}
/**
* Returns the user handle for the user that this process is running under.
*

View File

@@ -1674,8 +1674,9 @@ final class ActivityManagerShellCommand extends ShellCommand {
int runSwitchUser(PrintWriter pw) throws RemoteException {
UserManager userManager = mInternal.mContext.getSystemService(UserManager.class);
if (!userManager.canSwitchUsers()) {
getErrPrintWriter().println("Error: disallowed switching user");
final int userSwitchable = userManager.getUserSwitchability();
if (userSwitchable != UserManager.SWITCHABILITY_STATUS_OK) {
getErrPrintWriter().println("Error: " + userSwitchable);
return -1;
}
String user = getNextArgRequired();

View File

@@ -532,6 +532,12 @@ public class UserManagerTest extends AndroidTestCase {
}
}
public void testGetUserSwitchability() {
int userSwitchable = mUserManager.getUserSwitchability();
assertEquals("Expected users to be switchable", UserManager.SWITCHABILITY_STATUS_OK,
userSwitchable);
}
@LargeTest
public void testSwitchUser() {
ActivityManager am = getContext().getSystemService(ActivityManager.class);