blob: cb5ed4f2c15dcb738c38cecce0546bbd0469c93c [file] [log] [blame]
Amith Yamasani258848d2012-08-10 17:06:33 -07001/*
2 * Copyright (C) 2012 The Android Open Source Project
3 *
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
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
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.
15 */
16package android.os;
17
Dianne Hackborna8a9bd62012-10-09 15:36:59 -070018import android.app.ActivityManagerNative;
Amith Yamasani258848d2012-08-10 17:06:33 -070019import android.content.Context;
Amith Yamasanidf2e92a2013-03-01 17:04:38 -080020import android.content.RestrictionEntry;
Amith Yamasani258848d2012-08-10 17:06:33 -070021import android.content.pm.UserInfo;
Jeff Sharkey27bd34d2012-09-16 12:49:00 -070022import android.content.res.Resources;
Maggie Benthall67944582013-02-22 14:58:27 -050023import android.graphics.Bitmap;
Amith Yamasani258848d2012-08-10 17:06:33 -070024import android.util.Log;
25
Maggie Benthall67944582013-02-22 14:58:27 -050026import com.android.internal.R;
27
Amith Yamasani258848d2012-08-10 17:06:33 -070028import java.util.List;
29
30/**
31 * Manages users and user details on a multi-user system.
32 */
33public class UserManager {
34
35 private static String TAG = "UserManager";
36 private final IUserManager mService;
37 private final Context mContext;
38
Amith Yamasanie4cf7342012-12-17 11:12:09 -080039 /**
Amith Yamasani71e6c692013-03-24 17:39:28 -070040 * Key for user restrictions. Specifies if a user is disallowed from adding and removing
41 * accounts.
42 * The default value is <code>false</code>.
43 * <p/>
Amith Yamasanie4cf7342012-12-17 11:12:09 -080044 * Type: Boolean
45 * @see #setUserRestrictions(Bundle)
46 * @see #getUserRestrictions()
47 */
Amith Yamasani71e6c692013-03-24 17:39:28 -070048 public static final String DISALLOW_MODIFY_ACCOUNTS = "no_modify_accounts";
Amith Yamasanie4cf7342012-12-17 11:12:09 -080049
50 /**
Amith Yamasani71e6c692013-03-24 17:39:28 -070051 * Key for user restrictions. Specifies if a user is disallowed from changing Wi-Fi
52 * access points.
53 * The default value is <code>false</code>.
54 * <p/>
Amith Yamasanie4cf7342012-12-17 11:12:09 -080055 * Type: Boolean
56 * @see #setUserRestrictions(Bundle)
57 * @see #getUserRestrictions()
58 */
Amith Yamasani71e6c692013-03-24 17:39:28 -070059 public static final String DISALLOW_CONFIG_WIFI = "no_config_wifi";
Amith Yamasanie4cf7342012-12-17 11:12:09 -080060
61 /**
Amith Yamasani71e6c692013-03-24 17:39:28 -070062 * Key for user restrictions. Specifies if a user is disallowed from installing applications.
63 * The default value is <code>false</code>.
64 * <p/>
Amith Yamasanie4cf7342012-12-17 11:12:09 -080065 * Type: Boolean
66 * @see #setUserRestrictions(Bundle)
67 * @see #getUserRestrictions()
68 */
Amith Yamasani71e6c692013-03-24 17:39:28 -070069 public static final String DISALLOW_INSTALL_APPS = "no_install_apps";
Amith Yamasanie4cf7342012-12-17 11:12:09 -080070
71 /**
Amith Yamasani71e6c692013-03-24 17:39:28 -070072 * Key for user restrictions. Specifies if a user is disallowed from uninstalling applications.
73 * The default value is <code>false</code>.
74 * <p/>
Amith Yamasanie4cf7342012-12-17 11:12:09 -080075 * Type: Boolean
76 * @see #setUserRestrictions(Bundle)
77 * @see #getUserRestrictions()
78 */
Amith Yamasani71e6c692013-03-24 17:39:28 -070079 public static final String DISALLOW_UNINSTALL_APPS = "no_uninstall_apps";
Amith Yamasanie4cf7342012-12-17 11:12:09 -080080
Amith Yamasani71e6c692013-03-24 17:39:28 -070081 /**
82 * Key for user restrictions. Specifies if a user is disallowed from toggling location sharing.
83 * The default value is <code>false</code>.
84 * <p/>
Maggie Benthall67944582013-02-22 14:58:27 -050085 * Type: Boolean
86 * @see #setUserRestrictions(Bundle)
87 * @see #getUserRestrictions()
88 */
Maggie Benthalla12fccf2013-03-14 18:02:12 -040089
Amith Yamasani71e6c692013-03-24 17:39:28 -070090 public static final String DISALLOW_SHARE_LOCATION = "no_share_location";
Maggie Benthall67944582013-02-22 14:58:27 -050091
Maggie Benthalla12fccf2013-03-14 18:02:12 -040092 /**
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>.
96 * <p/>
97 * Type: Boolean
98 * @see #setUserRestrictions(Bundle)
99 * @see #getUserRestrictions()
100 */
101 public static final String DISALLOW_INSTALL_UNKNOWN_SOURCES = "no_install_unknown_sources";
102
103 /**
104 * Key for user restrictions. Specifies if a user is disallowed from configuring bluetooth.
105 * The default value is <code>false</code>.
106 * <p/>
107 * Type: Boolean
108 * @see #setUserRestrictions(Bundle)
109 * @see #getUserRestrictions()
110 */
111 public static final String DISALLOW_CONFIG_BLUETOOTH = "no_config_bluetooth";
112
Maggie Benthalla12fccf2013-03-14 18:02:12 -0400113 /**
114 * Key for user restrictions. Specifies if a user is disallowed from transferring files over
115 * USB. The default value is <code>false</code>.
116 * <p/>
117 * Type: Boolean
118 * @see #setUserRestrictions(Bundle)
119 * @see #getUserRestrictions()
120 */
121 public static final String DISALLOW_USB_FILE_TRANSFER = "no_usb_file_transfer";
122
Emily Bernierb223f732013-04-11 15:46:36 -0400123 /**
124 * Key for user restrictions. Specifies if a user is disallowed from configuring user
125 * credentials. The default value is <code>false</code>.
126 * <p/>
127 * Type: Boolean
128 * @see #setUserRestrictions(Bundle)
129 * @see #getUserRestrictions()
130 */
131 public static final String DISALLOW_CONFIG_CREDENTIALS = "no_config_credentials";
132
133 /**
134 * Key for user restrictions. Specifies if a user is disallowed from removing users.
135 * The default value is <code>false</code>.
136 * <p/>
137 * Type: Boolean
138 * @see #setUserRestrictions(Bundle)
139 * @see #getUserRestrictions()
140 */
141 public static final String DISALLOW_REMOVE_USER = "no_remove_user";
142
Amith Yamasani27db4682013-03-30 17:07:47 -0700143 private static UserManager sInstance = null;
144
Amith Yamasani7e99bc02013-04-16 18:24:51 -0700145 /** @hide */
Amith Yamasani27db4682013-03-30 17:07:47 -0700146 public synchronized static UserManager get(Context context) {
147 if (sInstance == null) {
148 sInstance = (UserManager) context.getSystemService(Context.USER_SERVICE);
149 }
150 return sInstance;
151 }
Maggie Benthalla12fccf2013-03-14 18:02:12 -0400152
Amith Yamasani258848d2012-08-10 17:06:33 -0700153 /** @hide */
154 public UserManager(Context context, IUserManager service) {
155 mService = service;
156 mContext = context;
157 }
158
159 /**
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.
Amith Yamasanie928d7d2012-09-17 21:46:51 -0700162 * @hide
Amith Yamasani258848d2012-08-10 17:06:33 -0700163 */
Jeff Sharkey4673e7e2012-09-19 13:14:30 -0700164 public static boolean supportsMultipleUsers() {
Amith Yamasani258848d2012-08-10 17:06:33 -0700165 return getMaxSupportedUsers() > 1;
166 }
167
Maggie Benthall67944582013-02-22 14:58:27 -0500168 /**
Amith Yamasani258848d2012-08-10 17:06:33 -0700169 * Returns the user handle for the user that this application is running for.
170 * @return the user handle of the user making this call.
171 * @hide
Maggie Benthall67944582013-02-22 14:58:27 -0500172 */
Amith Yamasani258848d2012-08-10 17:06:33 -0700173 public int getUserHandle() {
Dianne Hackborn79af1dd2012-08-16 16:42:52 -0700174 return UserHandle.myUserId();
Amith Yamasani258848d2012-08-10 17:06:33 -0700175 }
176
177 /**
Dianne Hackborn8832c182012-09-17 17:20:24 -0700178 * 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.
Amith Yamasani258848d2012-08-10 17:06:33 -0700181 * @return the user name
182 */
183 public String getUserName() {
184 try {
185 return mService.getUserInfo(getUserHandle()).name;
186 } catch (RemoteException re) {
187 Log.w(TAG, "Could not get user name", re);
188 return "";
189 }
190 }
191
Dan Morrille4ab16a2012-09-20 20:25:55 -0700192 /**
193 * Used to determine whether the user making this call is subject to
194 * teleportations.
Maggie Benthalla12fccf2013-03-14 18:02:12 -0400195 * @return whether the user making this call is a goat
Dan Morrille4ab16a2012-09-20 20:25:55 -0700196 */
197 public boolean isUserAGoat() {
198 return false;
199 }
Amith Yamasanidf2e92a2013-03-01 17:04:38 -0800200
201 /**
Amith Yamasanie1375902013-04-13 16:48:35 -0700202 * Used to check if the user making this call is linked to another user. Linked users may have
Amith Yamasani46bc4eb2013-04-12 13:26:50 -0700203 * a reduced number of available apps, app restrictions and account restrictions.
Amith Yamasanie1375902013-04-13 16:48:35 -0700204 * @return whether the user making this call is a linked user
Amith Yamasani2555daf2013-04-25 13:39:27 -0700205 * @hide
Amith Yamasanidf2e92a2013-03-01 17:04:38 -0800206 */
Amith Yamasanie1375902013-04-13 16:48:35 -0700207 public boolean isLinkedUser() {
Amith Yamasanidf2e92a2013-03-01 17:04:38 -0800208 try {
Amith Yamasani71e6c692013-03-24 17:39:28 -0700209 return mService.isRestricted();
Amith Yamasanidf2e92a2013-03-01 17:04:38 -0800210 } catch (RemoteException re) {
Amith Yamasani46bc4eb2013-04-12 13:26:50 -0700211 Log.w(TAG, "Could not check if user is limited ", re);
Amith Yamasanidf2e92a2013-03-01 17:04:38 -0800212 return false;
213 }
214 }
215
Amith Yamasani258848d2012-08-10 17:06:33 -0700216 /**
Dianne Hackborna8a9bd62012-10-09 15:36:59 -0700217 * 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.
223 */
224 public boolean isUserRunning(UserHandle user) {
225 try {
226 return ActivityManagerNative.getDefault().isUserRunning(
227 user.getIdentifier(), false);
228 } catch (RemoteException e) {
229 return false;
230 }
231 }
232
233 /**
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.
239 */
240 public boolean isUserRunningOrStopping(UserHandle user) {
241 try {
242 return ActivityManagerNative.getDefault().isUserRunning(
243 user.getIdentifier(), true);
244 } catch (RemoteException e) {
245 return false;
246 }
247 }
248
249 /**
Amith Yamasani258848d2012-08-10 17:06:33 -0700250 * Returns the UserInfo object describing a specific user.
Amith Yamasani195263742012-08-21 15:40:12 -0700251 * Requires {@link android.Manifest.permission#MANAGE_USERS} permission.
Amith Yamasani258848d2012-08-10 17:06:33 -0700252 * @param userHandle the user handle of the user whose information is being requested.
253 * @return the UserInfo object for a specific user.
254 * @hide
Dianne Hackbornb26306a2012-10-24 15:22:21 -0700255 */
Amith Yamasani258848d2012-08-10 17:06:33 -0700256 public UserInfo getUserInfo(int userHandle) {
257 try {
258 return mService.getUserInfo(userHandle);
259 } catch (RemoteException re) {
260 Log.w(TAG, "Could not get user info", re);
261 return null;
262 }
263 }
264
Amith Yamasani71e6c692013-03-24 17:39:28 -0700265 /**
266 * Returns the user-wide restrictions imposed on this user.
267 * @return a Bundle containing all the restrictions.
268 */
Amith Yamasanie4cf7342012-12-17 11:12:09 -0800269 public Bundle getUserRestrictions() {
270 return getUserRestrictions(Process.myUserHandle());
271 }
272
Amith Yamasani71e6c692013-03-24 17:39:28 -0700273 /**
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.
277 */
Amith Yamasanie4cf7342012-12-17 11:12:09 -0800278 public Bundle getUserRestrictions(UserHandle userHandle) {
279 try {
280 return mService.getUserRestrictions(userHandle.getIdentifier());
281 } catch (RemoteException re) {
282 Log.w(TAG, "Could not get user restrictions", re);
283 return Bundle.EMPTY;
284 }
285 }
286
Amith Yamasani71e6c692013-03-24 17:39:28 -0700287 /**
288 * Sets all the user-wide restrictions for this user.
Amith Yamasanica050ad2013-03-26 16:15:43 -0700289 * Requires the MANAGE_USERS permission.
Amith Yamasani71e6c692013-03-24 17:39:28 -0700290 * @param restrictions the Bundle containing all the restrictions.
291 */
Amith Yamasanie4cf7342012-12-17 11:12:09 -0800292 public void setUserRestrictions(Bundle restrictions) {
293 setUserRestrictions(restrictions, Process.myUserHandle());
294 }
295
Amith Yamasani71e6c692013-03-24 17:39:28 -0700296 /**
297 * Sets all the user-wide restrictions for the specified user.
Amith Yamasanica050ad2013-03-26 16:15:43 -0700298 * Requires the MANAGE_USERS permission.
Amith Yamasani71e6c692013-03-24 17:39:28 -0700299 * @param restrictions the Bundle containing all the restrictions.
300 * @param userHandle the UserHandle of the user for whom to set the restrictions.
301 */
Amith Yamasanie4cf7342012-12-17 11:12:09 -0800302 public void setUserRestrictions(Bundle restrictions, UserHandle userHandle) {
303 try {
304 mService.setUserRestrictions(restrictions, userHandle.getIdentifier());
305 } catch (RemoteException re) {
306 Log.w(TAG, "Could not set user restrictions", re);
307 }
308 }
309
Amith Yamasani71e6c692013-03-24 17:39:28 -0700310 /**
311 * Sets the value of a specific restriction.
Amith Yamasanica050ad2013-03-26 16:15:43 -0700312 * Requires the MANAGE_USERS permission.
Amith Yamasani71e6c692013-03-24 17:39:28 -0700313 * @param key the key of the restriction
314 * @param value the value for the restriction
Amith Yamasani71e6c692013-03-24 17:39:28 -0700315 */
316 public void setUserRestriction(String key, boolean value) {
317 Bundle bundle = getUserRestrictions();
318 bundle.putBoolean(key, value);
319 setUserRestrictions(bundle);
320 }
321
322 /**
323 * @hide
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.
329 */
Maggie Benthall67944582013-02-22 14:58:27 -0500330 public void setUserRestriction(String key, boolean value, UserHandle userHandle) {
331 Bundle bundle = getUserRestrictions(userHandle);
332 bundle.putBoolean(key, value);
333 setUserRestrictions(bundle, userHandle);
334 }
335
Amith Yamasani258848d2012-08-10 17:06:33 -0700336 /**
Maggie Benthalla12fccf2013-03-14 18:02:12 -0400337 * @hide
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
341 */
342 public boolean hasUserRestriction(String restrictionKey) {
343 return getUserRestrictions().getBoolean(restrictionKey, false);
344 }
345
346 /**
Dianne Hackborn33f9cb82012-10-04 17:15:10 -0700347 * Return the serial number for a user. This is a device-unique
Dianne Hackbornb26306a2012-10-24 15:22:21 -0700348 * 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.
Dianne Hackborn33f9cb82012-10-04 17:15:10 -0700350 * @param user The user whose serial number is to be retrieved.
Dianne Hackbornb26306a2012-10-24 15:22:21 -0700351 * @return The serial number of the given user; returns -1 if the
352 * given UserHandle does not exist.
Dianne Hackborn33f9cb82012-10-04 17:15:10 -0700353 * @see #getUserForSerialNumber(long)
354 */
355 public long getSerialNumberForUser(UserHandle user) {
356 return getUserSerialNumber(user.getIdentifier());
357 }
358
359 /**
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
363 * retrieved.
364 * @return Return the user associated with the serial number, or null
365 * if there is not one.
366 * @see #getSerialNumberForUser(UserHandle)
367 */
368 public UserHandle getUserForSerialNumber(long serialNumber) {
369 int ident = getUserHandle((int)serialNumber);
370 return ident >= 0 ? new UserHandle(ident) : null;
371 }
372
373 /**
Amith Yamasani258848d2012-08-10 17:06:33 -0700374 * Creates a user with the specified name and options.
Amith Yamasani195263742012-08-21 15:40:12 -0700375 * Requires {@link android.Manifest.permission#MANAGE_USERS} permission.
Amith Yamasani258848d2012-08-10 17:06:33 -0700376 *
377 * @param name the user's name
378 * @param flags flags that identify the type of user and other properties.
379 * @see UserInfo
380 *
381 * @return the UserInfo object for the created user, or null if the user could not be created.
382 * @hide
383 */
384 public UserInfo createUser(String name, int flags) {
385 try {
386 return mService.createUser(name, flags);
387 } catch (RemoteException re) {
388 Log.w(TAG, "Could not create a user", re);
389 return null;
390 }
391 }
392
393 /**
Dianne Hackbornb26306a2012-10-24 15:22:21 -0700394 * Return the number of users currently created on the device.
395 */
396 public int getUserCount() {
397 List<UserInfo> users = getUsers();
398 return users != null ? users.size() : 1;
399 }
400
401 /**
Amith Yamasani258848d2012-08-10 17:06:33 -0700402 * Returns information for all users on this device.
Amith Yamasani195263742012-08-21 15:40:12 -0700403 * Requires {@link android.Manifest.permission#MANAGE_USERS} permission.
Amith Yamasani258848d2012-08-10 17:06:33 -0700404 * @return the list of users that were created.
405 * @hide
406 */
407 public List<UserInfo> getUsers() {
408 try {
Amith Yamasani920ace02012-09-20 22:15:37 -0700409 return mService.getUsers(false);
410 } catch (RemoteException re) {
411 Log.w(TAG, "Could not get user list", re);
412 return null;
413 }
414 }
415
416 /**
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.
421 * @hide
422 */
423 public List<UserInfo> getUsers(boolean excludeDying) {
424 try {
425 return mService.getUsers(excludeDying);
Amith Yamasani258848d2012-08-10 17:06:33 -0700426 } catch (RemoteException re) {
427 Log.w(TAG, "Could not get user list", re);
428 return null;
429 }
430 }
431
432 /**
433 * Removes a user and all associated data.
Amith Yamasani195263742012-08-21 15:40:12 -0700434 * Requires {@link android.Manifest.permission#MANAGE_USERS} permission.
Amith Yamasani258848d2012-08-10 17:06:33 -0700435 * @param userHandle the integer handle of the user, where 0 is the primary user.
436 * @hide
437 */
438 public boolean removeUser(int userHandle) {
439 try {
440 return mService.removeUser(userHandle);
441 } catch (RemoteException re) {
442 Log.w(TAG, "Could not remove user ", re);
443 return false;
444 }
445 }
446
447 /**
448 * Updates the user's name.
Amith Yamasani195263742012-08-21 15:40:12 -0700449 * Requires {@link android.Manifest.permission#MANAGE_USERS} permission.
Amith Yamasani258848d2012-08-10 17:06:33 -0700450 *
451 * @param userHandle the user's integer handle
452 * @param name the new name for the user
453 * @hide
454 */
455 public void setUserName(int userHandle, String name) {
456 try {
457 mService.setUserName(userHandle, name);
458 } catch (RemoteException re) {
459 Log.w(TAG, "Could not set the user name ", re);
460 }
461 }
462
463 /**
Amith Yamasanie928d7d2012-09-17 21:46:51 -0700464 * Sets the user's photo.
Amith Yamasani258848d2012-08-10 17:06:33 -0700465 * @param userHandle the user for whom to change the photo.
Amith Yamasanie928d7d2012-09-17 21:46:51 -0700466 * @param icon the bitmap to set as the photo.
Amith Yamasani258848d2012-08-10 17:06:33 -0700467 * @hide
468 */
Amith Yamasanie928d7d2012-09-17 21:46:51 -0700469 public void setUserIcon(int userHandle, Bitmap icon) {
Amith Yamasani258848d2012-08-10 17:06:33 -0700470 try {
Amith Yamasanie928d7d2012-09-17 21:46:51 -0700471 mService.setUserIcon(userHandle, icon);
Amith Yamasani258848d2012-08-10 17:06:33 -0700472 } catch (RemoteException re) {
473 Log.w(TAG, "Could not set the user icon ", re);
Amith Yamasani258848d2012-08-10 17:06:33 -0700474 }
475 }
476
477 /**
Amith Yamasani3b49f072012-09-17 10:21:43 -0700478 * 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.
Amith Yamasanie928d7d2012-09-17 21:46:51 -0700480 * @return a {@link Bitmap} of the user's photo, or null if there's no photo.
Amith Yamasani3b49f072012-09-17 10:21:43 -0700481 * @hide
482 */
Amith Yamasanie928d7d2012-09-17 21:46:51 -0700483 public Bitmap getUserIcon(int userHandle) {
Amith Yamasani3b49f072012-09-17 10:21:43 -0700484 try {
485 return mService.getUserIcon(userHandle);
486 } catch (RemoteException re) {
Amith Yamasanie928d7d2012-09-17 21:46:51 -0700487 Log.w(TAG, "Could not get the user icon ", re);
Amith Yamasani3b49f072012-09-17 10:21:43 -0700488 return null;
489 }
490 }
491
492 /**
Amith Yamasani258848d2012-08-10 17:06:33 -0700493 * Enable or disable the use of a guest account. If disabled, the existing guest account
494 * will be wiped.
Amith Yamasani195263742012-08-21 15:40:12 -0700495 * Requires {@link android.Manifest.permission#MANAGE_USERS} permission.
Amith Yamasani258848d2012-08-10 17:06:33 -0700496 * @param enable whether to enable a guest account.
497 * @hide
498 */
499 public void setGuestEnabled(boolean enable) {
500 try {
501 mService.setGuestEnabled(enable);
502 } catch (RemoteException re) {
503 Log.w(TAG, "Could not change guest account availability to " + enable);
504 }
505 }
506
507 /**
508 * Checks if a guest user is enabled for this device.
Amith Yamasani195263742012-08-21 15:40:12 -0700509 * Requires {@link android.Manifest.permission#MANAGE_USERS} permission.
Amith Yamasani258848d2012-08-10 17:06:33 -0700510 * @return whether a guest user is enabled
511 * @hide
512 */
513 public boolean isGuestEnabled() {
514 try {
515 return mService.isGuestEnabled();
516 } catch (RemoteException re) {
517 Log.w(TAG, "Could not retrieve guest enabled state");
518 return false;
519 }
520 }
521
522 /**
523 * Wipes all the data for a user, but doesn't remove the user.
Amith Yamasani195263742012-08-21 15:40:12 -0700524 * Requires {@link android.Manifest.permission#MANAGE_USERS} permission.
Amith Yamasani258848d2012-08-10 17:06:33 -0700525 * @param userHandle
526 * @hide
527 */
528 public void wipeUser(int userHandle) {
529 try {
530 mService.wipeUser(userHandle);
531 } catch (RemoteException re) {
532 Log.w(TAG, "Could not wipe user " + userHandle);
533 }
534 }
535
536 /**
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.
539 * @hide
Maggie Benthalla12fccf2013-03-14 18:02:12 -0400540 * @return a value greater than or equal to 1
Amith Yamasani258848d2012-08-10 17:06:33 -0700541 */
Jeff Sharkey27bd34d2012-09-16 12:49:00 -0700542 public static int getMaxSupportedUsers() {
Amith Yamasaniff549202012-10-12 12:44:49 -0700543 // Don't allow multiple users on certain builds
544 if (android.os.Build.ID.startsWith("JVP")) return 1;
Jeff Sharkey27bd34d2012-09-16 12:49:00 -0700545 return SystemProperties.getInt("fw.max_users",
546 Resources.getSystem().getInteger(R.integer.config_multiuserMaximumUsers));
Amith Yamasani258848d2012-08-10 17:06:33 -0700547 }
Amith Yamasani2a003292012-08-14 18:25:45 -0700548
549 /**
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.
552 * @param userHandle
553 * @return a serial number associated with that user, or -1 if the userHandle is not valid.
554 * @hide
555 */
556 public int getUserSerialNumber(int userHandle) {
557 try {
558 return mService.getUserSerialNumber(userHandle);
559 } catch (RemoteException re) {
560 Log.w(TAG, "Could not get serial number for user " + userHandle);
561 }
562 return -1;
563 }
564
565 /**
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
568 * is wiped.
569 * @param userSerialNumber
570 * @return the userHandle associated with that user serial number, or -1 if the serial number
571 * is not valid.
572 * @hide
573 */
574 public int getUserHandle(int userSerialNumber) {
575 try {
576 return mService.getUserHandle(userSerialNumber);
577 } catch (RemoteException re) {
578 Log.w(TAG, "Could not get userHandle for user " + userSerialNumber);
579 }
580 return -1;
581 }
Maggie Benthall67944582013-02-22 14:58:27 -0500582
Amith Yamasani7e99bc02013-04-16 18:24:51 -0700583 /**
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.
590 */
591 public Bundle getApplicationRestrictions(String packageName) {
592 try {
593 return mService.getApplicationRestrictions(packageName);
594 } catch (RemoteException re) {
595 Log.w(TAG, "Could not get application restrictions for package " + packageName);
596 }
597 return null;
598 }
Amith Yamasanidf2e92a2013-03-01 17:04:38 -0800599
600 /**
601 * @hide
602 */
Amith Yamasani7e99bc02013-04-16 18:24:51 -0700603 public Bundle getApplicationRestrictions(String packageName, UserHandle user) {
Amith Yamasanidf2e92a2013-03-01 17:04:38 -0800604 try {
Amith Yamasani7e99bc02013-04-16 18:24:51 -0700605 return mService.getApplicationRestrictionsForUser(packageName, user.getIdentifier());
Amith Yamasanidf2e92a2013-03-01 17:04:38 -0800606 } catch (RemoteException re) {
607 Log.w(TAG, "Could not get application restrictions for user " + user.getIdentifier());
608 }
609 return null;
610 }
611
612 /**
613 * @hide
614 */
Amith Yamasani7e99bc02013-04-16 18:24:51 -0700615 public void setApplicationRestrictions(String packageName, Bundle restrictions,
Amith Yamasanidf2e92a2013-03-01 17:04:38 -0800616 UserHandle user) {
617 try {
Amith Yamasani7e99bc02013-04-16 18:24:51 -0700618 mService.setApplicationRestrictions(packageName, restrictions, user.getIdentifier());
Amith Yamasanidf2e92a2013-03-01 17:04:38 -0800619 } catch (RemoteException re) {
620 Log.w(TAG, "Could not set application restrictions for user " + user.getIdentifier());
621 }
622 }
Amith Yamasani258848d2012-08-10 17:06:33 -0700623}