/*
* Copyright (C) 2012 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package android.os;
import android.Manifest;
import android.accounts.AccountManager;
import android.annotation.Nullable;
import android.annotation.RequiresPermission;
import android.annotation.SystemApi;
import android.annotation.UserIdInt;
import android.app.ActivityManager;
import android.app.ActivityManagerNative;
import android.app.admin.DevicePolicyManager;
import android.content.ComponentName;
import android.content.Context;
import android.content.pm.UserInfo;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Rect;
import android.graphics.drawable.Drawable;
import android.os.storage.StorageManager;
import android.provider.Settings;
import android.util.Log;
import android.view.WindowManager.LayoutParams;
import com.android.internal.R;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
/**
* Manages users and user details on a multi-user system.
*/
public class UserManager {
private static String TAG = "UserManager";
private final IUserManager mService;
private final Context mContext;
/**
* Specifies if a user is disallowed from adding and removing accounts, unless they are
* {@link android.accounts.AccountManager#addAccountExplicitly programmatically} added by
* Authenticator.
* The default value is false
.
*
*
From {@link android.os.Build.VERSION_CODES#N} a profile or device owner app can still * use {@link android.accounts.AccountManager} APIs to add or remove accounts when account * management is disallowed. * *
Key for user restrictions. * Type: Boolean * @see DevicePolicyManager#addUserRestriction(ComponentName, String) * @see DevicePolicyManager#clearUserRestriction(ComponentName, String) * @see #getUserRestrictions() */ public static final String DISALLOW_MODIFY_ACCOUNTS = "no_modify_accounts"; /** * Specifies if a user is disallowed from changing Wi-Fi * access points. The default value isfalse
.
* This restriction has no effect in a managed profile.
*
* Key for user restrictions.
* Type: Boolean
* @see DevicePolicyManager#addUserRestriction(ComponentName, String)
* @see DevicePolicyManager#clearUserRestriction(ComponentName, String)
* @see #getUserRestrictions()
*/
public static final String DISALLOW_CONFIG_WIFI = "no_config_wifi";
/**
* Specifies if a user is disallowed from installing applications.
* The default value is false
.
*
* Key for user restrictions.
* Type: Boolean
* @see DevicePolicyManager#addUserRestriction(ComponentName, String)
* @see DevicePolicyManager#clearUserRestriction(ComponentName, String)
* @see #getUserRestrictions()
*/
public static final String DISALLOW_INSTALL_APPS = "no_install_apps";
/**
* Specifies if a user is disallowed from uninstalling applications.
* The default value is false
.
*
* Key for user restrictions.
* Type: Boolean
* @see DevicePolicyManager#addUserRestriction(ComponentName, String)
* @see DevicePolicyManager#clearUserRestriction(ComponentName, String)
* @see #getUserRestrictions()
*/
public static final String DISALLOW_UNINSTALL_APPS = "no_uninstall_apps";
/**
* Specifies if a user is disallowed from turning on location sharing.
* The default value is false
.
* In a managed profile, location sharing always reflects the primary user's setting, but
* can be overridden and forced off by setting this restriction to true in the managed profile.
*
* Key for user restrictions.
* Type: Boolean
* @see DevicePolicyManager#addUserRestriction(ComponentName, String)
* @see DevicePolicyManager#clearUserRestriction(ComponentName, String)
* @see #getUserRestrictions()
*/
public static final String DISALLOW_SHARE_LOCATION = "no_share_location";
/**
* Specifies if a user is disallowed from enabling the
* "Unknown Sources" setting, that allows installation of apps from unknown sources.
* The default value is false
.
*
* Key for user restrictions.
* Type: Boolean
* @see DevicePolicyManager#addUserRestriction(ComponentName, String)
* @see DevicePolicyManager#clearUserRestriction(ComponentName, String)
* @see #getUserRestrictions()
*/
public static final String DISALLOW_INSTALL_UNKNOWN_SOURCES = "no_install_unknown_sources";
/**
* Specifies if a user is disallowed from configuring bluetooth.
* This does not restrict the user from turning bluetooth on or off.
* The default value is false
.
* This restriction has no effect in a managed profile.
*
* Key for user restrictions.
* Type: Boolean
* @see DevicePolicyManager#addUserRestriction(ComponentName, String)
* @see DevicePolicyManager#clearUserRestriction(ComponentName, String)
* @see #getUserRestrictions()
*/
public static final String DISALLOW_CONFIG_BLUETOOTH = "no_config_bluetooth";
/**
* Specifies if a user is disallowed from transferring files over
* USB. This can only be set by device owners and profile owners on the primary user.
* The default value is false
.
*
* Key for user restrictions.
* Type: Boolean
* @see DevicePolicyManager#addUserRestriction(ComponentName, String)
* @see DevicePolicyManager#clearUserRestriction(ComponentName, String)
* @see #getUserRestrictions()
*/
public static final String DISALLOW_USB_FILE_TRANSFER = "no_usb_file_transfer";
/**
* Specifies if a user is disallowed from configuring user
* credentials. The default value is false
.
*
* Key for user restrictions.
* Type: Boolean
* @see DevicePolicyManager#addUserRestriction(ComponentName, String)
* @see DevicePolicyManager#clearUserRestriction(ComponentName, String)
* @see #getUserRestrictions()
*/
public static final String DISALLOW_CONFIG_CREDENTIALS = "no_config_credentials";
/**
* When set on the primary user this specifies if the user can remove other users.
* When set on a secondary user, this specifies if the user can remove itself.
* This restriction has no effect on managed profiles.
* The default value is false
.
*
* Key for user restrictions.
* Type: Boolean
* @see DevicePolicyManager#addUserRestriction(ComponentName, String)
* @see DevicePolicyManager#clearUserRestriction(ComponentName, String)
* @see #getUserRestrictions()
*/
public static final String DISALLOW_REMOVE_USER = "no_remove_user";
/**
* Specifies if a user is disallowed from enabling or
* accessing debugging features. The default value is false
.
*
* Key for user restrictions.
* Type: Boolean
* @see DevicePolicyManager#addUserRestriction(ComponentName, String)
* @see DevicePolicyManager#clearUserRestriction(ComponentName, String)
* @see #getUserRestrictions()
*/
public static final String DISALLOW_DEBUGGING_FEATURES = "no_debugging_features";
/**
* Specifies if a user is disallowed from configuring VPN.
* The default value is false
.
* This restriction has an effect in a managed profile only from
* {@link android.os.Build.VERSION_CODES#M}
*
* Key for user restrictions.
* Type: Boolean
* @see DevicePolicyManager#addUserRestriction(ComponentName, String)
* @see DevicePolicyManager#clearUserRestriction(ComponentName, String)
* @see #getUserRestrictions()
*/
public static final String DISALLOW_CONFIG_VPN = "no_config_vpn";
/**
* Specifies if a user is disallowed from configuring Tethering
* & portable hotspots. This can only be set by device owners and profile owners on the
* primary user. The default value is false
.
*
* Key for user restrictions.
* Type: Boolean
* @see DevicePolicyManager#addUserRestriction(ComponentName, String)
* @see DevicePolicyManager#clearUserRestriction(ComponentName, String)
* @see #getUserRestrictions()
*/
public static final String DISALLOW_CONFIG_TETHERING = "no_config_tethering";
/**
* Specifies if a user is disallowed from resetting network settings
* from Settings. This can only be set by device owners and profile owners on the primary user.
* The default value is false
.
* This restriction has no effect on secondary users and managed profiles since only the
* primary user can reset the network settings of the device.
*
* Key for user restrictions.
* Type: Boolean
* @see DevicePolicyManager#addUserRestriction(ComponentName, String)
* @see DevicePolicyManager#clearUserRestriction(ComponentName, String)
* @see #getUserRestrictions()
*/
public static final String DISALLOW_NETWORK_RESET = "no_network_reset";
/**
* Specifies if a user is disallowed from factory resetting
* from Settings. This can only be set by device owners and profile owners on the primary user.
* The default value is false
.
* This restriction has no effect on secondary users and managed profiles since only the
* primary user can factory reset the device.
*
* Key for user restrictions.
* Type: Boolean
* @see DevicePolicyManager#addUserRestriction(ComponentName, String)
* @see DevicePolicyManager#clearUserRestriction(ComponentName, String)
* @see #getUserRestrictions()
*/
public static final String DISALLOW_FACTORY_RESET = "no_factory_reset";
/**
* Specifies if a user is disallowed from adding new users and
* profiles. This can only be set by device owners and profile owners on the primary user.
* The default value is false
.
* This restriction has no effect on secondary users and managed profiles since only the
* primary user can add other users.
*
* Key for user restrictions.
* Type: Boolean
* @see DevicePolicyManager#addUserRestriction(ComponentName, String)
* @see DevicePolicyManager#clearUserRestriction(ComponentName, String)
* @see #getUserRestrictions()
*/
public static final String DISALLOW_ADD_USER = "no_add_user";
/**
* Specifies if a user is disallowed from disabling application
* verification. The default value is false
.
*
* Key for user restrictions.
* Type: Boolean
* @see DevicePolicyManager#addUserRestriction(ComponentName, String)
* @see DevicePolicyManager#clearUserRestriction(ComponentName, String)
* @see #getUserRestrictions()
*/
public static final String ENSURE_VERIFY_APPS = "ensure_verify_apps";
/**
* Specifies if a user is disallowed from configuring cell
* broadcasts. This can only be set by device owners and profile owners on the primary user.
* The default value is false
.
* This restriction has no effect on secondary users and managed profiles since only the
* primary user can configure cell broadcasts.
*
* Key for user restrictions.
* Type: Boolean
* @see DevicePolicyManager#addUserRestriction(ComponentName, String)
* @see DevicePolicyManager#clearUserRestriction(ComponentName, String)
* @see #getUserRestrictions()
*/
public static final String DISALLOW_CONFIG_CELL_BROADCASTS = "no_config_cell_broadcasts";
/**
* Specifies if a user is disallowed from configuring mobile
* networks. This can only be set by device owners and profile owners on the primary user.
* The default value is false
.
* This restriction has no effect on secondary users and managed profiles since only the
* primary user can configure mobile networks.
*
* Key for user restrictions.
* Type: Boolean
* @see DevicePolicyManager#addUserRestriction(ComponentName, String)
* @see DevicePolicyManager#clearUserRestriction(ComponentName, String)
* @see #getUserRestrictions()
*/
public static final String DISALLOW_CONFIG_MOBILE_NETWORKS = "no_config_mobile_networks";
/**
* Specifies if a user is disallowed from modifying
* applications in Settings or launchers. The following actions will not be allowed when this
* restriction is enabled:
*
* The default value is false
.
*
*
false
.
*
* Key for user restrictions.
* Type: Boolean
* @see DevicePolicyManager#addUserRestriction(ComponentName, String)
* @see DevicePolicyManager#clearUserRestriction(ComponentName, String)
* @see #getUserRestrictions()
*/
public static final String DISALLOW_MOUNT_PHYSICAL_MEDIA = "no_physical_media";
/**
* Specifies if a user is disallowed from adjusting microphone
* volume. If set, the microphone will be muted. This can only be set by device owners
* and profile owners on the primary user. The default value is false
.
*
* Key for user restrictions.
* Type: Boolean
* @see DevicePolicyManager#addUserRestriction(ComponentName, String)
* @see DevicePolicyManager#clearUserRestriction(ComponentName, String)
* @see #getUserRestrictions()
*/
public static final String DISALLOW_UNMUTE_MICROPHONE = "no_unmute_microphone";
/**
* Specifies if a user is disallowed from adjusting the master
* volume. If set, the master volume will be muted. This can only be set by device owners
* and profile owners on the primary user. The default value is false
.
*
* Key for user restrictions.
* Type: Boolean
* @see DevicePolicyManager#addUserRestriction(ComponentName, String)
* @see DevicePolicyManager#clearUserRestriction(ComponentName, String)
* @see #getUserRestrictions()
*/
public static final String DISALLOW_ADJUST_VOLUME = "no_adjust_volume";
/**
* Specifies that the user is not allowed to make outgoing
* phone calls. Emergency calls are still permitted.
* The default value is false
.
* This restriction has no effect on managed profiles since call intents are normally
* forwarded to the primary user.
*
* Key for user restrictions.
* Type: Boolean
* @see DevicePolicyManager#addUserRestriction(ComponentName, String)
* @see DevicePolicyManager#clearUserRestriction(ComponentName, String)
* @see #getUserRestrictions()
*/
public static final String DISALLOW_OUTGOING_CALLS = "no_outgoing_calls";
/**
* Specifies that the user is not allowed to send or receive
* SMS messages. The default value is false
.
*
* Key for user restrictions.
* Type: Boolean
* @see DevicePolicyManager#addUserRestriction(ComponentName, String)
* @see DevicePolicyManager#clearUserRestriction(ComponentName, String)
* @see #getUserRestrictions()
*/
public static final String DISALLOW_SMS = "no_sms";
/**
* Specifies if the user is not allowed to have fun. In some cases, the
* device owner may wish to prevent the user from experiencing amusement or
* joy while using the device. The default value is false
.
*
* Key for user restrictions.
* Type: Boolean
* @see DevicePolicyManager#addUserRestriction(ComponentName, String)
* @see DevicePolicyManager#clearUserRestriction(ComponentName, String)
* @see #getUserRestrictions()
*/
public static final String DISALLOW_FUN = "no_fun";
/**
* Specifies that windows besides app windows should not be
* created. This will block the creation of the following types of windows.
* This can only be set by device owners and profile owners on the primary user.
* The default value is false
.
*
*
false
.
*
* Key for user restrictions.
* Type: Boolean
* @see DevicePolicyManager#addUserRestriction(ComponentName, String)
* @see DevicePolicyManager#clearUserRestriction(ComponentName, String)
* @see #getUserRestrictions()
*/
public static final String DISALLOW_CROSS_PROFILE_COPY_PASTE = "no_cross_profile_copy_paste";
/**
* Specifies if the user is not allowed to use NFC to beam out data from apps.
* The default value is false
.
*
* Key for user restrictions.
* Type: Boolean
* @see DevicePolicyManager#addUserRestriction(ComponentName, String)
* @see DevicePolicyManager#clearUserRestriction(ComponentName, String)
* @see #getUserRestrictions()
*/
public static final String DISALLOW_OUTGOING_BEAM = "no_outgoing_beam";
/**
* Hidden user restriction to disallow access to wallpaper manager APIs. This user restriction
* is always set for managed profiles.
* @hide
* @see DevicePolicyManager#addUserRestriction(ComponentName, String)
* @see DevicePolicyManager#clearUserRestriction(ComponentName, String)
* @see #getUserRestrictions()
*/
public static final String DISALLOW_WALLPAPER = "no_wallpaper";
/**
* Specifies if the user is not allowed to reboot the device into safe boot mode.
* This can only be set by device owners and profile owners on the primary user.
* The default value is false
.
*
* Key for user restrictions.
* Type: Boolean
* @see DevicePolicyManager#addUserRestriction(ComponentName, String)
* @see DevicePolicyManager#clearUserRestriction(ComponentName, String)
* @see #getUserRestrictions()
*/
public static final String DISALLOW_SAFE_BOOT = "no_safe_boot";
/**
* Specifies if a user is not allowed to record audio. This restriction is always enabled for
* background users. The default value is false
.
*
* @see DevicePolicyManager#addUserRestriction(ComponentName, String)
* @see DevicePolicyManager#clearUserRestriction(ComponentName, String)
* @see #getUserRestrictions()
* @hide
*/
public static final String DISALLOW_RECORD_AUDIO = "no_record_audio";
/**
* Specifies if a user is not allowed to run in the background and should be stopped during
* user switch. The default value is false
.
*
* This restriction can be set by device owners and profile owners.
*
* @see DevicePolicyManager#addUserRestriction(ComponentName, String)
* @see DevicePolicyManager#clearUserRestriction(ComponentName, String)
* @see #getUserRestrictions()
* @hide
*/
public static final String DISALLOW_RUN_IN_BACKGROUND = "no_run_in_background";
/**
* Specifies if a user is not allowed to use the camera.
*
* @see DevicePolicyManager#addUserRestriction(ComponentName, String)
* @see DevicePolicyManager#clearUserRestriction(ComponentName, String)
* @see #getUserRestrictions()
* @hide
*/
public static final String DISALLOW_CAMERA = "no_camera";
/**
* Specifies if a user is not allowed to use cellular data when roaming. This can only be set by
* device owners. The default value is false
.
*
* @see DevicePolicyManager#addUserRestriction(ComponentName, String)
* @see DevicePolicyManager#clearUserRestriction(ComponentName, String)
* @see #getUserRestrictions()
*/
public static final String DISALLOW_DATA_ROAMING = "no_data_roaming";
/**
* Allows apps in the parent profile to handle web links from the managed profile.
*
* This user restriction has an effect only in a managed profile.
* If set:
* Intent filters of activities in the parent profile with action
* {@link android.content.Intent#ACTION_VIEW},
* category {@link android.content.Intent#CATEGORY_BROWSABLE}, scheme http or https, and which
* define a host can handle intents from the managed profile.
* The default value is false
.
*
*
* Applications that support restrictions should check for the presence of this key.
* A true
value indicates that restrictions may be applied in the near
* future but are not available yet. It is the responsibility of any
* management application that sets this flag to update it when the final
* restrictions are enforced.
*
*
As of {@link android.os.Build.VERSION_CODES#LOLLIPOP}, this method can * now automatically identify goats using advanced goat recognition technology.
* * @return Returns true if the user making this call is a goat. */ public boolean isUserAGoat() { return mContext.getPackageManager() .isPackageAvailable("com.coffeestainstudios.goatsimulator"); } /** * Used to check if this process is running under the primary user. The primary user * is the first human user on a device. * * @return whether this process is running under the primary user. * @hide */ public boolean isPrimaryUser() { UserInfo user = getUserInfo(UserHandle.myUserId()); return user != null ? user.isPrimary() : false; } /** * Used to check if this process is running under the system user. The system user * is the initial user that is implicitly created on first boot and hosts most of the * system services. * * @return whether this process is running under the system user. */ public boolean isSystemUser() { return UserHandle.myUserId() == UserHandle.USER_SYSTEM; } /** * @hide * Returns whether the caller is running as an admin user. There can be more than one admin * user. */ public boolean isAdminUser() { return isUserAdmin(UserHandle.myUserId()); } /** * @hide * Returns whether the provided user is an admin user. There can be more than one admin * user. */ public boolean isUserAdmin(@UserIdInt int userId) { UserInfo user = getUserInfo(userId); return user != null && user.isAdmin(); } /** * Used to check if the user making this call is linked to another user. Linked users may have * a reduced number of available apps, app restrictions and account restrictions. * @return whether the user making this call is a linked user * @hide */ public boolean isLinkedUser() { try { return mService.isRestricted(); } catch (RemoteException re) { Log.w(TAG, "Could not check if user is limited ", re); return false; } } /** * Checks if specified user can have restricted profile. * @hide */ public boolean canHaveRestrictedProfile(@UserIdInt int userId) { try { return mService.canHaveRestrictedProfile(userId); } catch (RemoteException re) { Log.w(TAG, "Could not check if user can have restricted profile", re); return false; } } /** * Checks if the calling app is running as a guest user. * @return whether the caller is a guest user. * @hide */ public boolean isGuestUser() { UserInfo user = getUserInfo(UserHandle.myUserId()); return user != null ? user.isGuest() : false; } /** * Checks if the calling app is running in a managed profile. * Requires {@link android.Manifest.permission#MANAGE_USERS} permission. * * @return whether the caller is in a managed profile. * @hide */ @SystemApi public boolean isManagedProfile() { UserInfo user = getUserInfo(UserHandle.myUserId()); return user != null ? user.isManagedProfile() : false; } /** * Checks if the calling app is running as an ephemeral user. * * @return whether the caller is an ephemeral user. * @hide */ public boolean isEphemeralUser() { return isUserEphemeral(UserHandle.myUserId()); } /** * Returns whether the specified user is ephemeral. * @hide */ public boolean isUserEphemeral(@UserIdInt int userId) { final UserInfo user = getUserInfo(userId); return user != null && user.isEphemeral(); } /** * Return whether the given user is actively running. This means that * the user is in the "started" state, not "stopped" -- it is currently * allowed to run code through scheduled alarms, receiving broadcasts, * etc. A started user may be either the current foreground user or a * background user; the result here does not distinguish between the two. * @param user The user to retrieve the running state for. */ public boolean isUserRunning(UserHandle user) { return isUserRunning(user.getIdentifier()); } /** {@hide} */ public boolean isUserRunning(int userId) { try { return ActivityManagerNative.getDefault().isUserRunning(userId, 0); } catch (RemoteException e) { return false; } } /** * Return whether the given user is actively running or stopping. * This is like {@link #isUserRunning(UserHandle)}, but will also return * true if the user had been running but is in the process of being stopped * (but is not yet fully stopped, and still running some code). * @param user The user to retrieve the running state for. */ public boolean isUserRunningOrStopping(UserHandle user) { try { // TODO: reconcile stopped vs stopping? return ActivityManagerNative.getDefault().isUserRunning( user.getIdentifier(), ActivityManager.FLAG_OR_STOPPED); } catch (RemoteException e) { return false; } } /** * Return whether the calling user is running in a "locked" state. A user is * unlocked only after they've entered their credentials (such as a lock * pattern or PIN), and credential-encrypted private app data storage is * available. */ @Deprecated public boolean isUserRunningAndLocked() { return isUserRunningAndLocked(Process.myUserHandle()); } /** * Return whether the given user is running in a "locked" state. A user * is unlocked only after they've entered their credentials (such as a lock * pattern or PIN), and credential-encrypted private app data storage is * available. * * @param user to retrieve the unlocked state for. */ @Deprecated public boolean isUserRunningAndLocked(UserHandle user) { try { return ActivityManagerNative.getDefault().isUserRunning( user.getIdentifier(), ActivityManager.FLAG_AND_LOCKED); } catch (RemoteException e) { return false; } } /** * Return whether the calling user is running in an "unlocked" state. A user * is unlocked only after they've entered their credentials (such as a lock * pattern or PIN), and credential-encrypted private app data storage is * available. */ @Deprecated public boolean isUserRunningAndUnlocked() { return isUserRunningAndUnlocked(Process.myUserHandle()); } /** * Return whether the given user is running in an "unlocked" state. A user * is unlocked only after they've entered their credentials (such as a lock * pattern or PIN), and credential-encrypted private app data storage is * available. * * @param user to retrieve the unlocked state for. */ @Deprecated public boolean isUserRunningAndUnlocked(UserHandle user) { try { return ActivityManagerNative.getDefault().isUserRunning( user.getIdentifier(), ActivityManager.FLAG_AND_UNLOCKED); } catch (RemoteException e) { return false; } } /** * Return whether the calling user is running in an "unlocked" state. A user * is unlocked only after they've entered their credentials (such as a lock * pattern or PIN), and credential-encrypted private app data storage is * available. */ public boolean isUserUnlocked() { return isUserUnlocked(Process.myUserHandle()); } /** * Return whether the given user is running in an "unlocked" state. A user * is unlocked only after they've entered their credentials (such as a lock * pattern or PIN), and credential-encrypted private app data storage is * available. * * @param user to retrieve the unlocked state for. */ public boolean isUserUnlocked(UserHandle user) { return isUserUnlocked(user.getIdentifier()); } /** {@hide} */ public boolean isUserUnlocked(@UserIdInt int userId) { // TODO: eventually pivot this back to look at ActivityManager state, // but there is race where we can start a non-encryption-aware launcher // before that lifecycle has entered the running unlocked state. return mContext.getSystemService(StorageManager.class).isUserKeyUnlocked(userId); } /** * Returns the UserInfo object describing a specific user. * Requires {@link android.Manifest.permission#MANAGE_USERS} permission. * @param userHandle the user handle of the user whose information is being requested. * @return the UserInfo object for a specific user. * @hide */ public UserInfo getUserInfo(@UserIdInt int userHandle) { try { return mService.getUserInfo(userHandle); } catch (RemoteException re) { Log.w(TAG, "Could not get user info", re); return null; } } /** * Returns the user-wide restrictions imposed on this user. * @return a Bundle containing all the restrictions. */ public Bundle getUserRestrictions() { return getUserRestrictions(Process.myUserHandle()); } /** * Returns the user-wide restrictions imposed on the user specified byuserHandle
.
* @param userHandle the UserHandle of the user for whom to retrieve the restrictions.
* @return a Bundle containing all the restrictions.
*/
public Bundle getUserRestrictions(UserHandle userHandle) {
try {
return mService.getUserRestrictions(userHandle.getIdentifier());
} catch (RemoteException re) {
Log.w(TAG, "Could not get user restrictions", re);
return Bundle.EMPTY;
}
}
/**
* @hide
* Returns whether the given user has been disallowed from performing certain actions
* or setting certain settings through UserManager. This method disregards restrictions
* set by device policy.
* @param restrictionKey the string key representing the restriction
* @param userHandle the UserHandle of the user for whom to retrieve the restrictions.
*/
public boolean hasBaseUserRestriction(String restrictionKey, UserHandle userHandle) {
try {
return mService.hasBaseUserRestriction(restrictionKey, userHandle.getIdentifier());
} catch (RemoteException re) {
Log.w(TAG, "Could not get base user restrictions for user " +
userHandle.getIdentifier(), re);
return false;
}
}
/**
* This will no longer work. Device owners and profile owners should use
* {@link DevicePolicyManager#addUserRestriction(ComponentName, String)} instead.
*/
// System apps should use UserManager.setUserRestriction() instead.
@Deprecated
public void setUserRestrictions(Bundle restrictions) {
throw new UnsupportedOperationException("This method is no longer supported");
}
/**
* This will no longer work. Device owners and profile owners should use
* {@link DevicePolicyManager#addUserRestriction(ComponentName, String)} instead.
*/
// System apps should use UserManager.setUserRestriction() instead.
@Deprecated
public void setUserRestrictions(Bundle restrictions, UserHandle userHandle) {
throw new UnsupportedOperationException("This method is no longer supported");
}
/**
* Sets the value of a specific restriction.
* Requires the MANAGE_USERS permission.
* @param key the key of the restriction
* @param value the value for the restriction
* @deprecated use {@link android.app.admin.DevicePolicyManager#addUserRestriction(
* android.content.ComponentName, String)} or
* {@link android.app.admin.DevicePolicyManager#clearUserRestriction(
* android.content.ComponentName, String)} instead.
*/
@Deprecated
public void setUserRestriction(String key, boolean value) {
setUserRestriction(key, value, Process.myUserHandle());
}
/**
* @hide
* Sets the value of a specific restriction on a specific user.
* Requires the MANAGE_USERS permission.
* @param key the key of the restriction
* @param value the value for the restriction
* @param userHandle the user whose restriction is to be changed.
* @deprecated use {@link android.app.admin.DevicePolicyManager#addUserRestriction(
* android.content.ComponentName, String)} or
* {@link android.app.admin.DevicePolicyManager#clearUserRestriction(
* android.content.ComponentName, String)} instead.
*/
@Deprecated
public void setUserRestriction(String key, boolean value, UserHandle userHandle) {
try {
mService.setUserRestriction(key, value, userHandle.getIdentifier());
} catch (RemoteException re) {
Log.w(TAG, "Could not set user restriction", re);
}
}
/**
* Returns whether the current user has been disallowed from performing certain actions
* or setting certain settings.
*
* @param restrictionKey The string key representing the restriction.
* @return {@code true} if the current user has the given restriction, {@code false} otherwise.
*/
public boolean hasUserRestriction(String restrictionKey) {
return hasUserRestriction(restrictionKey, Process.myUserHandle());
}
/**
* @hide
* Returns whether the given user has been disallowed from performing certain actions
* or setting certain settings.
* @param restrictionKey the string key representing the restriction
* @param userHandle the UserHandle of the user for whom to retrieve the restrictions.
*/
public boolean hasUserRestriction(String restrictionKey, UserHandle userHandle) {
try {
return mService.hasUserRestriction(restrictionKey,
userHandle.getIdentifier());
} catch (RemoteException re) {
Log.w(TAG, "Could not check user restrictions", re);
return false;
}
}
/**
* Return the serial number for a user. This is a device-unique
* number assigned to that user; if the user is deleted and then a new
* user created, the new users will not be given the same serial number.
* @param user The user whose serial number is to be retrieved.
* @return The serial number of the given user; returns -1 if the
* given UserHandle does not exist.
* @see #getUserForSerialNumber(long)
*/
public long getSerialNumberForUser(UserHandle user) {
return getUserSerialNumber(user.getIdentifier());
}
/**
* Return the user associated with a serial number previously
* returned by {@link #getSerialNumberForUser(UserHandle)}.
* @param serialNumber The serial number of the user that is being
* retrieved.
* @return Return the user associated with the serial number, or null
* if there is not one.
* @see #getSerialNumberForUser(UserHandle)
*/
public UserHandle getUserForSerialNumber(long serialNumber) {
int ident = getUserHandle((int) serialNumber);
return ident >= 0 ? new UserHandle(ident) : null;
}
/**
* Creates a user with the specified name and options. For non-admin users, default user
* restrictions are going to be applied.
* Requires {@link android.Manifest.permission#MANAGE_USERS} permission.
*
* @param name the user's name
* @param flags flags that identify the type of user and other properties.
* @see UserInfo
*
* @return the UserInfo object for the created user, or null if the user could not be created.
* @hide
*/
public UserInfo createUser(String name, int flags) {
UserInfo user = null;
try {
user = mService.createUser(name, flags);
if (user != null && !user.isAdmin()) {
mService.setUserRestriction(DISALLOW_SMS, true, user.id);
mService.setUserRestriction(DISALLOW_OUTGOING_CALLS, true, user.id);
}
} catch (RemoteException re) {
Log.w(TAG, "Could not create a user", re);
}
return user;
}
/**
* Creates a guest user and configures it.
* @param context an application context
* @param name the name to set for the user
* @hide
*/
public UserInfo createGuest(Context context, String name) {
UserInfo guest = null;
try {
guest = mService.createUser(name, UserInfo.FLAG_GUEST);
if (guest != null) {
Settings.Secure.putStringForUser(context.getContentResolver(),
Settings.Secure.SKIP_FIRST_USE_HINTS, "1", guest.id);
}
} catch (RemoteException re) {
Log.w(TAG, "Could not create a user", re);
}
return guest;
}
/**
* Creates a user with the specified name and options as a profile of another user.
* Requires {@link android.Manifest.permission#MANAGE_USERS} permission.
*
* @param name the user's name
* @param flags flags that identify the type of user and other properties.
* @see UserInfo
* @param userHandle new user will be a profile of this use.
*
* @return the UserInfo object for the created user, or null if the user could not be created.
* @hide
*/
public UserInfo createProfileForUser(String name, int flags, @UserIdInt int userHandle) {
try {
return mService.createProfileForUser(name, flags, userHandle);
} catch (RemoteException re) {
Log.w(TAG, "Could not create a user", re);
return null;
}
}
/**
* Creates a restricted profile with the specified name. This method also sets necessary
* restrictions and adds shared accounts.
*
* @param name profile's name
* @return UserInfo object for the created user, or null if the user could not be created.
* @hide
*/
public UserInfo createRestrictedProfile(String name) {
try {
UserHandle parentUserHandle = Process.myUserHandle();
UserInfo user = mService.createRestrictedProfile(name,
parentUserHandle.getIdentifier());
if (user != null) {
AccountManager.get(mContext).addSharedAccountsFromParentUser(parentUserHandle,
UserHandle.of(user.id));
}
return user;
} catch (RemoteException e) {
Log.w(TAG, "Could not create a restricted profile", e);
}
return null;
}
/**
* @hide
* Marks the guest user for deletion to allow a new guest to be created before deleting
* the current user who is a guest.
* @param userHandle
* @return
*/
public boolean markGuestForDeletion(@UserIdInt int userHandle) {
try {
return mService.markGuestForDeletion(userHandle);
} catch (RemoteException re) {
Log.w(TAG, "Could not mark guest for deletion", re);
return false;
}
}
/**
* Sets the user as enabled, if such an user exists.
* Requires {@link android.Manifest.permission#MANAGE_USERS} permission.
* Note that the default is true, it's only that managed profiles might not be enabled.
*
* @param userHandle the id of the profile to enable
* @hide
*/
public void setUserEnabled(@UserIdInt int userHandle) {
try {
mService.setUserEnabled(userHandle);
} catch (RemoteException e) {
Log.w(TAG, "Could not enable the profile", e);
}
}
/**
* Return the number of users currently created on the device.
*/
public int getUserCount() {
List* If the original drawable is a BitmapDrawable and the backing bitmap is * mutable as per {@link android.graphics.Bitmap#isMutable()}, the bading * is performed in place and the original drawable is returned. *
* * @param icon The icon to badge. * @param user The target user. * @return A drawable that combines the original icon and a badge as * determined by the system. * @removed */ public Drawable getBadgedIconForUser(Drawable icon, UserHandle user) { return mContext.getPackageManager().getUserBadgedIcon(icon, user); } /** * If the target user is a managed profile of the calling user or the caller * is itself a managed profile, then this returns a badged copy of the given * drawable allowing the user to distinguish it from the original drawable. * The caller can specify the location in the bounds of the drawable to be * badged where the badge should be applied as well as the density of the * badge to be used. ** If the original drawable is a BitmapDrawable and the backing bitmap is * mutable as per {@link android.graphics.Bitmap#isMutable()}, the bading * is performed in place and the original drawable is returned. *
* * @param badgedDrawable The drawable to badge. * @param user The target user. * @param badgeLocation Where in the bounds of the badged drawable to place * the badge. If not provided, the badge is applied on top of the entire * drawable being badged. * @param badgeDensity The optional desired density for the badge as per * {@link android.util.DisplayMetrics#densityDpi}. If not provided, * the density of the display is used. * @return A drawable that combines the original drawable and a badge as * determined by the system. * @removed */ public Drawable getBadgedDrawableForUser(Drawable badgedDrawable, UserHandle user, Rect badgeLocation, int badgeDensity) { return mContext.getPackageManager().getUserBadgedDrawableForDensity(badgedDrawable, user, badgeLocation, badgeDensity); } /** * If the target user is a managed profile of the calling user or the caller * is itself a managed profile, then this returns a copy of the label with * badging for accessibility services like talkback. E.g. passing in "Email" * and it might return "Work Email" for Email in the work profile. * * @param label The label to change. * @param user The target user. * @return A label that combines the original label and a badge as * determined by the system. * @removed */ public CharSequence getBadgedLabelForUser(CharSequence label, UserHandle user) { return mContext.getPackageManager().getUserBadgedLabel(label, user); } /** * Returns information for all users on this device. Requires * {@link android.Manifest.permission#MANAGE_USERS} permission. * * @param excludeDying specify if the list should exclude users being * removed. * @return the list of users that were created. * @hide */ public ListThe returned {@link Bundle} consists of key-value pairs, as defined by the application, * where the types of values may be: *