/*
* 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.app.ActivityManagerNative;
import android.content.Context;
import android.content.pm.UserInfo;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Bitmap.Config;
import android.graphics.Rect;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.provider.Settings;
import android.util.Log;
import android.view.WindowManager.LayoutParams;
import com.android.internal.R;
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;
/**
* Key for user restrictions. Specifies if a user is disallowed from adding and removing
* accounts.
* The default value is false
.
*
false
.
*
* Type: Boolean
* @see #setUserRestrictions(Bundle)
* @see #getUserRestrictions()
*/
public static final String DISALLOW_CONFIG_WIFI = "no_config_wifi";
/**
* Key for user restrictions. Specifies if a user is disallowed from installing applications.
* The default value is false
.
*
* Type: Boolean
* @see #setUserRestrictions(Bundle)
* @see #getUserRestrictions()
*/
public static final String DISALLOW_INSTALL_APPS = "no_install_apps";
/**
* Key for user restrictions. Specifies if a user is disallowed from uninstalling applications.
* The default value is false
.
*
* Type: Boolean
* @see #setUserRestrictions(Bundle)
* @see #getUserRestrictions()
*/
public static final String DISALLOW_UNINSTALL_APPS = "no_uninstall_apps";
/**
* Key for user restrictions. Specifies if a user is disallowed from toggling location sharing.
* The default value is false
.
*
* Type: Boolean
* @see #setUserRestrictions(Bundle)
* @see #getUserRestrictions()
*/
public static final String DISALLOW_SHARE_LOCATION = "no_share_location";
/**
* Key for user restrictions. 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
.
*
* Type: Boolean
* @see #setUserRestrictions(Bundle)
* @see #getUserRestrictions()
*/
public static final String DISALLOW_INSTALL_UNKNOWN_SOURCES = "no_install_unknown_sources";
/**
* Key for user restrictions. Specifies if a user is disallowed from configuring bluetooth.
* The default value is false
.
*
* Type: Boolean
* @see #setUserRestrictions(Bundle)
* @see #getUserRestrictions()
*/
public static final String DISALLOW_CONFIG_BLUETOOTH = "no_config_bluetooth";
/**
* Key for user restrictions. Specifies if a user is disallowed from transferring files over
* USB. The default value is false
.
*
* Type: Boolean
* @see #setUserRestrictions(Bundle)
* @see #getUserRestrictions()
*/
public static final String DISALLOW_USB_FILE_TRANSFER = "no_usb_file_transfer";
/**
* Key for user restrictions. Specifies if a user is disallowed from configuring user
* credentials. The default value is false
.
*
* Type: Boolean
* @see #setUserRestrictions(Bundle)
* @see #getUserRestrictions()
*/
public static final String DISALLOW_CONFIG_CREDENTIALS = "no_config_credentials";
/**
* Key for user restrictions. Specifies if a user is disallowed from removing users.
* The default value is false
.
*
* Type: Boolean
* @see #setUserRestrictions(Bundle)
* @see #getUserRestrictions()
*/
public static final String DISALLOW_REMOVE_USER = "no_remove_user";
/**
* Key for user restrictions. Specifies if a user is disallowed from enabling or
* accessing debugging features. The default value is false
.
*
* Type: Boolean
* @see #setUserRestrictions(Bundle)
* @see #getUserRestrictions()
*/
public static final String DISALLOW_DEBUGGING_FEATURES = "no_debugging_features";
/**
* Key for user restrictions. Specifies if a user is disallowed from configuring VPN.
* The default value is false
.
*
* Type: Boolean
* @see #setUserRestrictions(Bundle)
* @see #getUserRestrictions()
*/
public static final String DISALLOW_CONFIG_VPN = "no_config_vpn";
/**
* Key for user restrictions. Specifies if a user is disallowed from configuring Tethering
* & portable hotspots. The default value is false
.
*
* Type: Boolean
* @see #setUserRestrictions(Bundle)
* @see #getUserRestrictions()
*/
public static final String DISALLOW_CONFIG_TETHERING = "no_config_tethering";
/**
* Key for user restrictions. Specifies if a user is disallowed from factory resetting
* from Settings.
* The default value is false
.
*
* @see #setUserRestrictions(Bundle)
* @see #getUserRestrictions()
*/
public static final String DISALLOW_FACTORY_RESET = "no_factory_reset";
/**
* Key for user restrictions. Specifies if a user is disallowed from adding new users and
* profiles. The default value is false
.
*
* Type: Boolean
* @see #setUserRestrictions(Bundle)
* @see #getUserRestrictions()
*/
public static final String DISALLOW_ADD_USER = "no_add_user";
/**
* Key for user restrictions. Specifies if a user is disallowed from disabling application
* verification. The default value is false
.
*
* Type: Boolean
* @see #setUserRestrictions(Bundle)
* @see #getUserRestrictions()
*/
public static final String ENSURE_VERIFY_APPS = "ensure_verify_apps";
/**
* Key for user restrictions. Specifies if a user is disallowed from configuring cell
* broadcasts. The default value is false
.
*
* Type: Boolean
* @see #setUserRestrictions(Bundle)
* @see #getUserRestrictions()
*/
public static final String DISALLOW_CONFIG_CELL_BROADCASTS = "no_config_cell_broadcasts";
/**
* Key for user restrictions. Specifies if a user is disallowed from configuring mobile
* networks. The default value is false
.
*
* Type: Boolean
* @see #setUserRestrictions(Bundle)
* @see #getUserRestrictions()
*/
public static final String DISALLOW_CONFIG_MOBILE_NETWORKS = "no_config_mobile_networks";
/**
* Key for user restrictions. Specifies if a user is disallowed from controlling
* applications in Settings. The default value is false
.
*
* Type: Boolean
* @see #setUserRestrictions(Bundle)
* @see #getUserRestrictions()
*/
public static final String DISALLOW_APPS_CONTROL = "no_control_apps";
/**
* Key for user restrictions. Specifies if a user is disallowed from mounting
* physical external media. The default value is false
.
*
false
.
*
* Type: Boolean
* @see #setUserRestrictions(Bundle)
* @see #getUserRestrictions()
*/
public static final String DISALLOW_UNMUTE_MICROPHONE = "no_unmute_microphone";
/**
* Key for user restrictions. Specifies if a user is disallowed from adjusting the master
* volume.
* The default value is false
.
*
* Type: Boolean
* @see #setUserRestrictions(Bundle)
* @see #getUserRestrictions()
*/
public static final String DISALLOW_ADJUST_VOLUME = "no_adjust_volume";
/**
* Key for user restrictions. Specifies that the user is not allowed to send or receive
* phone calls or text messages. Emergency calls may still be permitted.
* The default value is false
.
*
* Type: Boolean
* @see #setUserRestrictions(Bundle)
* @see #getUserRestrictions()
*/
public static final String DISALLOW_TELEPHONY = "no_telephony";
/**
* Key for user restrictions. Specifies that windows besides app windows should not be
* created. This will block the creation of the following types of windows.
* The default value is false
.
*
userHandle
.
* @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;
}
}
/**
* Sets all the user-wide restrictions for this user.
* Requires the MANAGE_USERS permission.
* @param restrictions the Bundle containing all the restrictions.
*/
public void setUserRestrictions(Bundle restrictions) {
setUserRestrictions(restrictions, Process.myUserHandle());
}
/**
* Sets all the user-wide restrictions for the specified user.
* Requires the MANAGE_USERS permission.
* @param restrictions the Bundle containing all the restrictions.
* @param userHandle the UserHandle of the user for whom to set the restrictions.
*/
public void setUserRestrictions(Bundle restrictions, UserHandle userHandle) {
try {
mService.setUserRestrictions(restrictions, userHandle.getIdentifier());
} catch (RemoteException re) {
Log.w(TAG, "Could not set user restrictions", re);
}
}
/**
* 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
*/
public void setUserRestriction(String key, boolean value) {
Bundle bundle = getUserRestrictions();
bundle.putBoolean(key, value);
setUserRestrictions(bundle);
}
/**
* @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.
*/
public void setUserRestriction(String key, boolean value, UserHandle userHandle) {
Bundle bundle = getUserRestrictions(userHandle);
bundle.putBoolean(key, value);
setUserRestrictions(bundle, userHandle);
}
/**
* 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) {
return getUserRestrictions(userHandle).getBoolean(restrictionKey, 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.
* 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) {
try {
return mService.createUser(name, flags);
} catch (RemoteException re) {
Log.w(TAG, "Could not create a user", re);
return null;
}
}
/**
* 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 = createUser(name, UserInfo.FLAG_GUEST);
if (guest != null) {
Settings.Secure.putStringForUser(context.getContentResolver(),
Settings.Secure.SKIP_FIRST_USE_HINTS, "1", guest.id);
}
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, int userHandle) {
try {
return mService.createProfileForUser(name, flags, userHandle);
} catch (RemoteException re) {
Log.w(TAG, "Could not create a user", re);
return null;
}
}
/**
* 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(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 not a BitmapDrawable, then 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. */ public Drawable getBadgedDrawableForUser(Drawable icon, UserHandle user) { int badgeResId = getBadgeResIdForUser(user.getIdentifier()); if (badgeResId == 0) { return icon; } else { Drawable badgeIcon = mContext.getPackageManager() .getDrawable("system", badgeResId, null); return getMergedDrawable(icon, badgeIcon); } } /** * 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. */ public String getBadgedLabelForUser(String label, UserHandle user) { UserInfo userInfo = getUserIfProfile(user.getIdentifier()); if (userInfo != null && userInfo.isManagedProfile()) { return Resources.getSystem().getString( R.string.managed_profile_label_badge, label); } return label; } /** * If the target user is a managed profile of the calling user or the caller * is itself a managed profile, then this returns a drawable to use as a small * icon to include in a view to distinguish it from the original icon. * * @param user The target user. * @return the drawable or null if no drawable is required. * @hide */ public Drawable getBadgeForUser(UserHandle user) { UserInfo userInfo = getUserIfProfile(user.getIdentifier()); if (userInfo != null && userInfo.isManagedProfile()) { return Resources.getSystem().getDrawable( com.android.internal.R.drawable.ic_corp_badge); } return null; } private int getBadgeResIdForUser(int userHandle) { // Return the framework-provided badge. UserInfo userInfo = getUserIfProfile(userHandle); if (userInfo != null && userInfo.isManagedProfile()) { return com.android.internal.R.drawable.ic_corp_icon_badge; } return 0; } /** * @return UserInfo for userHandle if it exists and is a profile of the current * user or null. */ private UserInfo getUserIfProfile(int userHandle) { List