2 * Copyright (C) 2012 The Android Open Source Project
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
18 import android.app.ActivityManagerNative;
19 import android.content.Context;
20 import android.content.RestrictionEntry;
21 import android.content.pm.UserInfo;
22 import android.content.res.Resources;
23 import android.graphics.Bitmap;
24 import android.util.Log;
26 import com.android.internal.R;
28 import java.util.List;
31 * Manages users and user details on a multi-user system.
33 public class UserManager {
35 private static String TAG = "UserManager";
36 private final IUserManager mService;
37 private final Context mContext;
40 * Key for user restrictions. Specifies if a user is disallowed from adding and removing
42 * The default value is <code>false</code>.
45 * @see #setUserRestrictions(Bundle)
46 * @see #getUserRestrictions()
48 public static final String DISALLOW_MODIFY_ACCOUNTS = "no_modify_accounts";
51 * Key for user restrictions. Specifies if a user is disallowed from changing Wi-Fi
53 * The default value is <code>false</code>.
56 * @see #setUserRestrictions(Bundle)
57 * @see #getUserRestrictions()
59 public static final String DISALLOW_CONFIG_WIFI = "no_config_wifi";
62 * Key for user restrictions. Specifies if a user is disallowed from installing applications.
63 * The default value is <code>false</code>.
66 * @see #setUserRestrictions(Bundle)
67 * @see #getUserRestrictions()
69 public static final String DISALLOW_INSTALL_APPS = "no_install_apps";
72 * Key for user restrictions. Specifies if a user is disallowed from uninstalling applications.
73 * The default value is <code>false</code>.
76 * @see #setUserRestrictions(Bundle)
77 * @see #getUserRestrictions()
79 public static final String DISALLOW_UNINSTALL_APPS = "no_uninstall_apps";
82 * Key for user restrictions. Specifies if a user is disallowed from toggling location sharing.
83 * The default value is <code>false</code>.
86 * @see #setUserRestrictions(Bundle)
87 * @see #getUserRestrictions()
90 public static final String DISALLOW_SHARE_LOCATION = "no_share_location";
93 * Key for user restrictions. Specifies if a user is disallowed from enabling the
94 * "Unknown Sources" setting, that allows installation of apps from unknown sources.
95 * The default value is <code>false</code>.
98 * @see #setUserRestrictions(Bundle)
99 * @see #getUserRestrictions()
101 public static final String DISALLOW_INSTALL_UNKNOWN_SOURCES = "no_install_unknown_sources";
104 * Key for user restrictions. Specifies if a user is disallowed from configuring bluetooth.
105 * The default value is <code>false</code>.
108 * @see #setUserRestrictions(Bundle)
109 * @see #getUserRestrictions()
111 public static final String DISALLOW_CONFIG_BLUETOOTH = "no_config_bluetooth";
114 * Key for user restrictions. Specifies if a user is disallowed from transferring files over
115 * USB. The default value is <code>false</code>.
118 * @see #setUserRestrictions(Bundle)
119 * @see #getUserRestrictions()
121 public static final String DISALLOW_USB_FILE_TRANSFER = "no_usb_file_transfer";
124 * Key for user restrictions. Specifies if a user is disallowed from configuring user
125 * credentials. The default value is <code>false</code>.
128 * @see #setUserRestrictions(Bundle)
129 * @see #getUserRestrictions()
131 public static final String DISALLOW_CONFIG_CREDENTIALS = "no_config_credentials";
134 * Key for user restrictions. Specifies if a user is disallowed from removing users.
135 * The default value is <code>false</code>.
138 * @see #setUserRestrictions(Bundle)
139 * @see #getUserRestrictions()
141 public static final String DISALLOW_REMOVE_USER = "no_remove_user";
143 private static UserManager sInstance = null;
146 public synchronized static UserManager get(Context context) {
147 if (sInstance == null) {
148 sInstance = (UserManager) context.getSystemService(Context.USER_SERVICE);
154 public UserManager(Context context, IUserManager service) {
160 * Returns whether the system supports multiple users.
161 * @return true if multiple users can be created, false if it is a single user device.
164 public static boolean supportsMultipleUsers() {
165 return getMaxSupportedUsers() > 1;
169 * Returns the user handle for the user that this application is running for.
170 * @return the user handle of the user making this call.
173 public int getUserHandle() {
174 return UserHandle.myUserId();
178 * Returns the user name of the user making this call. This call is only
179 * available to applications on the system image; it requires the
180 * MANAGE_USERS permission.
181 * @return the user name
183 public String getUserName() {
185 return mService.getUserInfo(getUserHandle()).name;
186 } catch (RemoteException re) {
187 Log.w(TAG, "Could not get user name", re);
193 * Used to determine whether the user making this call is subject to
195 * @return whether the user making this call is a goat
197 public boolean isUserAGoat() {
202 * Used to check if the user making this call is linked to another user. Linked users may have
203 * a reduced number of available apps, app restrictions and account restrictions.
204 * @return whether the user making this call is a linked user
207 public boolean isLinkedUser() {
209 return mService.isRestricted();
210 } catch (RemoteException re) {
211 Log.w(TAG, "Could not check if user is limited ", re);
217 * Return whether the given user is actively running. This means that
218 * the user is in the "started" state, not "stopped" -- it is currently
219 * allowed to run code through scheduled alarms, receiving broadcasts,
220 * etc. A started user may be either the current foreground user or a
221 * background user; the result here does not distinguish between the two.
222 * @param user The user to retrieve the running state for.
224 public boolean isUserRunning(UserHandle user) {
226 return ActivityManagerNative.getDefault().isUserRunning(
227 user.getIdentifier(), false);
228 } catch (RemoteException e) {
234 * Return whether the given user is actively running <em>or</em> stopping.
235 * This is like {@link #isUserRunning(UserHandle)}, but will also return
236 * true if the user had been running but is in the process of being stopped
237 * (but is not yet fully stopped, and still running some code).
238 * @param user The user to retrieve the running state for.
240 public boolean isUserRunningOrStopping(UserHandle user) {
242 return ActivityManagerNative.getDefault().isUserRunning(
243 user.getIdentifier(), true);
244 } catch (RemoteException e) {
250 * Returns the UserInfo object describing a specific user.
251 * Requires {@link android.Manifest.permission#MANAGE_USERS} permission.
252 * @param userHandle the user handle of the user whose information is being requested.
253 * @return the UserInfo object for a specific user.
256 public UserInfo getUserInfo(int userHandle) {
258 return mService.getUserInfo(userHandle);
259 } catch (RemoteException re) {
260 Log.w(TAG, "Could not get user info", re);
266 * Returns the user-wide restrictions imposed on this user.
267 * @return a Bundle containing all the restrictions.
269 public Bundle getUserRestrictions() {
270 return getUserRestrictions(Process.myUserHandle());
274 * Returns the user-wide restrictions imposed on the user specified by <code>userHandle</code>.
275 * @param userHandle the UserHandle of the user for whom to retrieve the restrictions.
276 * @return a Bundle containing all the restrictions.
278 public Bundle getUserRestrictions(UserHandle userHandle) {
280 return mService.getUserRestrictions(userHandle.getIdentifier());
281 } catch (RemoteException re) {
282 Log.w(TAG, "Could not get user restrictions", re);
288 * Sets all the user-wide restrictions for this user.
289 * Requires the MANAGE_USERS permission.
290 * @param restrictions the Bundle containing all the restrictions.
292 public void setUserRestrictions(Bundle restrictions) {
293 setUserRestrictions(restrictions, Process.myUserHandle());
297 * Sets all the user-wide restrictions for the specified user.
298 * Requires the MANAGE_USERS permission.
299 * @param restrictions the Bundle containing all the restrictions.
300 * @param userHandle the UserHandle of the user for whom to set the restrictions.
302 public void setUserRestrictions(Bundle restrictions, UserHandle userHandle) {
304 mService.setUserRestrictions(restrictions, userHandle.getIdentifier());
305 } catch (RemoteException re) {
306 Log.w(TAG, "Could not set user restrictions", re);
311 * Sets the value of a specific restriction.
312 * Requires the MANAGE_USERS permission.
313 * @param key the key of the restriction
314 * @param value the value for the restriction
316 public void setUserRestriction(String key, boolean value) {
317 Bundle bundle = getUserRestrictions();
318 bundle.putBoolean(key, value);
319 setUserRestrictions(bundle);
324 * Sets the value of a specific restriction on a specific user.
325 * Requires the {@link android.Manifest.permission#MANAGE_USERS} permission.
326 * @param key the key of the restriction
327 * @param value the value for the restriction
328 * @param userHandle the user whose restriction is to be changed.
330 public void setUserRestriction(String key, boolean value, UserHandle userHandle) {
331 Bundle bundle = getUserRestrictions(userHandle);
332 bundle.putBoolean(key, value);
333 setUserRestrictions(bundle, userHandle);
338 * Returns whether the current user has been disallowed from performing certain actions
339 * or setting certain settings.
340 * @param restrictionKey the string key representing the restriction
342 public boolean hasUserRestriction(String restrictionKey) {
343 return getUserRestrictions().getBoolean(restrictionKey, false);
347 * Return the serial number for a user. This is a device-unique
348 * number assigned to that user; if the user is deleted and then a new
349 * user created, the new users will not be given the same serial number.
350 * @param user The user whose serial number is to be retrieved.
351 * @return The serial number of the given user; returns -1 if the
352 * given UserHandle does not exist.
353 * @see #getUserForSerialNumber(long)
355 public long getSerialNumberForUser(UserHandle user) {
356 return getUserSerialNumber(user.getIdentifier());
360 * Return the user associated with a serial number previously
361 * returned by {@link #getSerialNumberForUser(UserHandle)}.
362 * @param serialNumber The serial number of the user that is being
364 * @return Return the user associated with the serial number, or null
365 * if there is not one.
366 * @see #getSerialNumberForUser(UserHandle)
368 public UserHandle getUserForSerialNumber(long serialNumber) {
369 int ident = getUserHandle((int)serialNumber);
370 return ident >= 0 ? new UserHandle(ident) : null;
374 * Creates a user with the specified name and options.
375 * Requires {@link android.Manifest.permission#MANAGE_USERS} permission.
377 * @param name the user's name
378 * @param flags flags that identify the type of user and other properties.
381 * @return the UserInfo object for the created user, or null if the user could not be created.
384 public UserInfo createUser(String name, int flags) {
386 return mService.createUser(name, flags);
387 } catch (RemoteException re) {
388 Log.w(TAG, "Could not create a user", re);
394 * Return the number of users currently created on the device.
396 public int getUserCount() {
397 List<UserInfo> users = getUsers();
398 return users != null ? users.size() : 1;
402 * Returns information for all users on this device.
403 * Requires {@link android.Manifest.permission#MANAGE_USERS} permission.
404 * @return the list of users that were created.
407 public List<UserInfo> getUsers() {
409 return mService.getUsers(false);
410 } catch (RemoteException re) {
411 Log.w(TAG, "Could not get user list", re);
417 * Returns information for all users on this device.
418 * Requires {@link android.Manifest.permission#MANAGE_USERS} permission.
419 * @param excludeDying specify if the list should exclude users being removed.
420 * @return the list of users that were created.
423 public List<UserInfo> getUsers(boolean excludeDying) {
425 return mService.getUsers(excludeDying);
426 } catch (RemoteException re) {
427 Log.w(TAG, "Could not get user list", re);
433 * Removes a user and all associated data.
434 * Requires {@link android.Manifest.permission#MANAGE_USERS} permission.
435 * @param userHandle the integer handle of the user, where 0 is the primary user.
438 public boolean removeUser(int userHandle) {
440 return mService.removeUser(userHandle);
441 } catch (RemoteException re) {
442 Log.w(TAG, "Could not remove user ", re);
448 * Updates the user's name.
449 * Requires {@link android.Manifest.permission#MANAGE_USERS} permission.
451 * @param userHandle the user's integer handle
452 * @param name the new name for the user
455 public void setUserName(int userHandle, String name) {
457 mService.setUserName(userHandle, name);
458 } catch (RemoteException re) {
459 Log.w(TAG, "Could not set the user name ", re);
464 * Sets the user's photo.
465 * @param userHandle the user for whom to change the photo.
466 * @param icon the bitmap to set as the photo.
469 public void setUserIcon(int userHandle, Bitmap icon) {
471 mService.setUserIcon(userHandle, icon);
472 } catch (RemoteException re) {
473 Log.w(TAG, "Could not set the user icon ", re);
478 * Returns a file descriptor for the user's photo. PNG data can be read from this file.
479 * @param userHandle the user whose photo we want to read.
480 * @return a {@link Bitmap} of the user's photo, or null if there's no photo.
483 public Bitmap getUserIcon(int userHandle) {
485 return mService.getUserIcon(userHandle);
486 } catch (RemoteException re) {
487 Log.w(TAG, "Could not get the user icon ", re);
493 * Enable or disable the use of a guest account. If disabled, the existing guest account
495 * Requires {@link android.Manifest.permission#MANAGE_USERS} permission.
496 * @param enable whether to enable a guest account.
499 public void setGuestEnabled(boolean enable) {
501 mService.setGuestEnabled(enable);
502 } catch (RemoteException re) {
503 Log.w(TAG, "Could not change guest account availability to " + enable);
508 * Checks if a guest user is enabled for this device.
509 * Requires {@link android.Manifest.permission#MANAGE_USERS} permission.
510 * @return whether a guest user is enabled
513 public boolean isGuestEnabled() {
515 return mService.isGuestEnabled();
516 } catch (RemoteException re) {
517 Log.w(TAG, "Could not retrieve guest enabled state");
523 * Wipes all the data for a user, but doesn't remove the user.
524 * Requires {@link android.Manifest.permission#MANAGE_USERS} permission.
528 public void wipeUser(int userHandle) {
530 mService.wipeUser(userHandle);
531 } catch (RemoteException re) {
532 Log.w(TAG, "Could not wipe user " + userHandle);
537 * Returns the maximum number of users that can be created on this device. A return value
538 * of 1 means that it is a single user device.
540 * @return a value greater than or equal to 1
542 public static int getMaxSupportedUsers() {
543 // Don't allow multiple users on certain builds
544 if (android.os.Build.ID.startsWith("JVP")) return 1;
545 return SystemProperties.getInt("fw.max_users",
546 Resources.getSystem().getInteger(R.integer.config_multiuserMaximumUsers));
550 * Returns a serial number on this device for a given userHandle. User handles can be recycled
551 * when deleting and creating users, but serial numbers are not reused until the device is wiped.
553 * @return a serial number associated with that user, or -1 if the userHandle is not valid.
556 public int getUserSerialNumber(int userHandle) {
558 return mService.getUserSerialNumber(userHandle);
559 } catch (RemoteException re) {
560 Log.w(TAG, "Could not get serial number for user " + userHandle);
566 * Returns a userHandle on this device for a given user serial number. User handles can be
567 * recycled when deleting and creating users, but serial numbers are not reused until the device
569 * @param userSerialNumber
570 * @return the userHandle associated with that user serial number, or -1 if the serial number
574 public int getUserHandle(int userSerialNumber) {
576 return mService.getUserHandle(userSerialNumber);
577 } catch (RemoteException re) {
578 Log.w(TAG, "Could not get userHandle for user " + userSerialNumber);
584 * Returns a Bundle containing any saved application restrictions for this user, for the
585 * given package name. Only an application with this package name can call this method.
586 * @param packageName the package name of the calling application
587 * @return a Bundle with the restrictions as key/value pairs, or null if there are no
588 * saved restrictions. The values can be of type Boolean, String or String[], depending
589 * on the restriction type, as defined by the application.
591 public Bundle getApplicationRestrictions(String packageName) {
593 return mService.getApplicationRestrictions(packageName);
594 } catch (RemoteException re) {
595 Log.w(TAG, "Could not get application restrictions for package " + packageName);
603 public Bundle getApplicationRestrictions(String packageName, UserHandle user) {
605 return mService.getApplicationRestrictionsForUser(packageName, user.getIdentifier());
606 } catch (RemoteException re) {
607 Log.w(TAG, "Could not get application restrictions for user " + user.getIdentifier());
615 public void setApplicationRestrictions(String packageName, Bundle restrictions,
618 mService.setApplicationRestrictions(packageName, restrictions, user.getIdentifier());
619 } catch (RemoteException re) {
620 Log.w(TAG, "Could not set application restrictions for user " + user.getIdentifier());