blob: ee219e3d5d3681985eea72eee5746e3de10a39dd [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;
20import android.content.pm.UserInfo;
Jeff Sharkey27bd34d2012-09-16 12:49:00 -070021import android.content.res.Resources;
Maggie Benthall67944582013-02-22 14:58:27 -050022import android.graphics.Bitmap;
Amith Yamasani4f582632014-02-19 14:31:52 -080023import android.graphics.Canvas;
24import android.graphics.Bitmap.Config;
25import android.graphics.Rect;
26import android.graphics.drawable.BitmapDrawable;
27import android.graphics.drawable.Drawable;
Amith Yamasani258848d2012-08-10 17:06:33 -070028import android.util.Log;
29
Maggie Benthall67944582013-02-22 14:58:27 -050030import com.android.internal.R;
31
Amith Yamasani4f582632014-02-19 14:31:52 -080032import java.util.ArrayList;
Amith Yamasani258848d2012-08-10 17:06:33 -070033import java.util.List;
34
35/**
36 * Manages users and user details on a multi-user system.
37 */
38public class UserManager {
39
40 private static String TAG = "UserManager";
41 private final IUserManager mService;
42 private final Context mContext;
43
Amith Yamasanie4cf7342012-12-17 11:12:09 -080044 /**
Amith Yamasani71e6c692013-03-24 17:39:28 -070045 * Key for user restrictions. Specifies if a user is disallowed from adding and removing
46 * accounts.
47 * The default value is <code>false</code>.
48 * <p/>
Amith Yamasanie4cf7342012-12-17 11:12:09 -080049 * Type: Boolean
50 * @see #setUserRestrictions(Bundle)
51 * @see #getUserRestrictions()
52 */
Amith Yamasani71e6c692013-03-24 17:39:28 -070053 public static final String DISALLOW_MODIFY_ACCOUNTS = "no_modify_accounts";
Amith Yamasanie4cf7342012-12-17 11:12:09 -080054
55 /**
Amith Yamasani71e6c692013-03-24 17:39:28 -070056 * Key for user restrictions. Specifies if a user is disallowed from changing Wi-Fi
57 * access points.
58 * The default value is <code>false</code>.
59 * <p/>
Amith Yamasanie4cf7342012-12-17 11:12:09 -080060 * Type: Boolean
61 * @see #setUserRestrictions(Bundle)
62 * @see #getUserRestrictions()
63 */
Amith Yamasani71e6c692013-03-24 17:39:28 -070064 public static final String DISALLOW_CONFIG_WIFI = "no_config_wifi";
Amith Yamasanie4cf7342012-12-17 11:12:09 -080065
66 /**
Amith Yamasani71e6c692013-03-24 17:39:28 -070067 * Key for user restrictions. Specifies if a user is disallowed from installing applications.
68 * The default value is <code>false</code>.
69 * <p/>
Amith Yamasanie4cf7342012-12-17 11:12:09 -080070 * Type: Boolean
71 * @see #setUserRestrictions(Bundle)
72 * @see #getUserRestrictions()
73 */
Amith Yamasani71e6c692013-03-24 17:39:28 -070074 public static final String DISALLOW_INSTALL_APPS = "no_install_apps";
Amith Yamasanie4cf7342012-12-17 11:12:09 -080075
76 /**
Amith Yamasani71e6c692013-03-24 17:39:28 -070077 * Key for user restrictions. Specifies if a user is disallowed from uninstalling applications.
78 * The default value is <code>false</code>.
79 * <p/>
Amith Yamasanie4cf7342012-12-17 11:12:09 -080080 * Type: Boolean
81 * @see #setUserRestrictions(Bundle)
82 * @see #getUserRestrictions()
83 */
Amith Yamasani71e6c692013-03-24 17:39:28 -070084 public static final String DISALLOW_UNINSTALL_APPS = "no_uninstall_apps";
Amith Yamasanie4cf7342012-12-17 11:12:09 -080085
Amith Yamasani71e6c692013-03-24 17:39:28 -070086 /**
87 * Key for user restrictions. Specifies if a user is disallowed from toggling location sharing.
88 * The default value is <code>false</code>.
89 * <p/>
Maggie Benthall67944582013-02-22 14:58:27 -050090 * Type: Boolean
91 * @see #setUserRestrictions(Bundle)
92 * @see #getUserRestrictions()
93 */
Amith Yamasani71e6c692013-03-24 17:39:28 -070094 public static final String DISALLOW_SHARE_LOCATION = "no_share_location";
Maggie Benthall67944582013-02-22 14:58:27 -050095
Maggie Benthalla12fccf2013-03-14 18:02:12 -040096 /**
97 * Key for user restrictions. Specifies if a user is disallowed from enabling the
98 * "Unknown Sources" setting, that allows installation of apps from unknown sources.
99 * The default value is <code>false</code>.
100 * <p/>
101 * Type: Boolean
102 * @see #setUserRestrictions(Bundle)
103 * @see #getUserRestrictions()
104 */
105 public static final String DISALLOW_INSTALL_UNKNOWN_SOURCES = "no_install_unknown_sources";
106
107 /**
108 * Key for user restrictions. Specifies if a user is disallowed from configuring bluetooth.
109 * The default value is <code>false</code>.
110 * <p/>
111 * Type: Boolean
112 * @see #setUserRestrictions(Bundle)
113 * @see #getUserRestrictions()
114 */
115 public static final String DISALLOW_CONFIG_BLUETOOTH = "no_config_bluetooth";
116
Maggie Benthalla12fccf2013-03-14 18:02:12 -0400117 /**
118 * Key for user restrictions. Specifies if a user is disallowed from transferring files over
119 * USB. The default value is <code>false</code>.
120 * <p/>
121 * Type: Boolean
122 * @see #setUserRestrictions(Bundle)
123 * @see #getUserRestrictions()
124 */
125 public static final String DISALLOW_USB_FILE_TRANSFER = "no_usb_file_transfer";
126
Emily Bernierb223f732013-04-11 15:46:36 -0400127 /**
128 * Key for user restrictions. Specifies if a user is disallowed from configuring user
129 * credentials. The default value is <code>false</code>.
130 * <p/>
131 * Type: Boolean
132 * @see #setUserRestrictions(Bundle)
133 * @see #getUserRestrictions()
134 */
135 public static final String DISALLOW_CONFIG_CREDENTIALS = "no_config_credentials";
136
137 /**
138 * Key for user restrictions. Specifies if a user is disallowed from removing users.
139 * The default value is <code>false</code>.
140 * <p/>
141 * Type: Boolean
142 * @see #setUserRestrictions(Bundle)
143 * @see #getUserRestrictions()
144 */
145 public static final String DISALLOW_REMOVE_USER = "no_remove_user";
146
Julia Reynoldsd46d0f92014-04-23 15:23:24 -0400147 /**
148 * Key for user restrictions. Specifies if a user is disallowed from enabling or
149 * accessing debugging features. The default value is <code>false</code>.
150 * <p/>
151 * Type: Boolean
152 * @see #setUserRestrictions(Bundle)
153 * @see #getUserRestrictions()
154 */
155 public static final String DISALLOW_DEBUGGING_FEATURES = "no_debugging_features";
156
157 /**
158 * Key for user restrictions. Specifies if a user is disallowed from configuring VPN.
159 * The default value is <code>false</code>.
160 * <p/>
161 * Type: Boolean
162 * @see #setUserRestrictions(Bundle)
163 * @see #getUserRestrictions()
164 */
165 public static final String DISALLOW_CONFIG_VPN = "no_config_vpn";
166
167 /**
168 * Key for user restrictions. Specifies if a user is disallowed from configuring Tethering
169 * & portable hotspots. The default value is <code>false</code>.
170 * <p/>
171 * Type: Boolean
172 * @see #setUserRestrictions(Bundle)
173 * @see #getUserRestrictions()
174 */
175 public static final String DISALLOW_CONFIG_TETHERING = "no_config_tethering";
176
177 /**
178 * Key for user restrictions. Specifies if a user is disallowed from factory resetting
179 * from Settings.
180 * The default value is <code>false</code>.
181 * <p>
182 * @see #setUserRestrictions(Bundle)
183 * @see #getUserRestrictions()
184 */
185 public static final String DISALLOW_FACTORY_RESET = "no_factory_reset";
186
187 /**
188 * Key for user restrictions. Specifies if a user is disallowed from adding new users and
189 * profiles. The default value is <code>false</code>.
190 * <p>
191 * Type: Boolean
192 * @see #setUserRestrictions(Bundle)
193 * @see #getUserRestrictions()
194 */
195 public static final String DISALLOW_ADD_USER = "no_add_user";
196
197 /**
198 * Key for user restrictions. Specifies if a user is disallowed from disabling application
199 * verification. The default value is <code>false</code>.
200 * <p>
201 * Type: Boolean
202 * @see #setUserRestrictions(Bundle)
203 * @see #getUserRestrictions()
204 */
205 public static final String ENSURE_VERIFY_APPS = "ensure_verify_apps";
206
207 /**
208 * Key for user restrictions. Specifies if a user is disallowed from configuring cell
209 * broadcasts. The default value is <code>false</code>.
210 * <p>
211 * Type: Boolean
212 * @see #setUserRestrictions(Bundle)
213 * @see #getUserRestrictions()
214 */
215 public static final String DISALLOW_CONFIG_CELL_BROADCASTS = "no_config_cell_broadcasts";
216
217 /**
218 * Key for user restrictions. Specifies if a user is disallowed from configuring mobile
219 * networks. The default value is <code>false</code>.
220 * <p>
221 * Type: Boolean
222 * @see #setUserRestrictions(Bundle)
223 * @see #getUserRestrictions()
224 */
225 public static final String DISALLOW_CONFIG_MOBILE_NETWORKS = "no_config_mobile_networks";
226
227 /**
228 * Key for user restrictions. Specifies if a user is disallowed from configuring
229 * applications in Settings. The default value is <code>false</code>.
230 * <p>
231 * Type: Boolean
232 * @see #setUserRestrictions(Bundle)
233 * @see #getUserRestrictions()
234 */
235 public static final String DISALLOW_CONFIG_APPS = "no_config_apps";
236
Emily Bernier394a6cd2014-05-07 12:49:20 -0400237 /**
238 * Key for user restrictions. Specifies if a user is disallowed from mounting
239 * physical external media. The default value is <code>false</code>.
240 * <p/>
241 * Type: Boolean
242 * @see #setUserRestrictions(Bundle)
243 * @see #getUserRestrictions()
244 */
245 public static final String DISALLOW_MOUNT_PHYSICAL_MEDIA = "no_physical_media";
246
247 /**
248 * Key for user restrictions. Specifies if a user is disallowed from adjusting microphone
249 * volume.
250 * The default value is <code>false</code>.
251 * <p/>
252 * Type: Boolean
253 * @see #setUserRestrictions(Bundle)
254 * @see #getUserRestrictions()
255 */
256 public static final String DISALLOW_UNMUTE_MICROPHONE = "no_unmute_microphone";
257
258 /**
259 * Key for user restrictions. Specifies if a user is disallowed from adjusting the master
260 * volume.
261 * The default value is <code>false</code>.
262 * <p/>
263 * Type: Boolean
264 * @see #setUserRestrictions(Bundle)
265 * @see #getUserRestrictions()
266 */
267 public static final String DISALLOW_ADJUST_VOLUME = "no_adjust_volume";
268
Amith Yamasani9f6c25f2014-05-16 14:49:15 -0700269 /**
270 * Key for user restrictions. Specifies that the user is not allowed to send or receive
271 * phone calls or text messages. Emergency calls may still be permitted.
272 * The default value is <code>false</code>.
273 * <p/>
274 * Type: Boolean
275 * @see #setUserRestrictions(Bundle)
276 * @see #getUserRestrictions()
277 */
278 public static final String DISALLOW_TELEPHONY = "no_telephony";
279
Amith Yamasani655d0e22013-06-12 14:19:10 -0700280 /** @hide */
281 public static final int PIN_VERIFICATION_FAILED_INCORRECT = -3;
282 /** @hide */
283 public static final int PIN_VERIFICATION_FAILED_NOT_SET = -2;
284 /** @hide */
285 public static final int PIN_VERIFICATION_SUCCESS = -1;
286
Amith Yamasani27db4682013-03-30 17:07:47 -0700287 private static UserManager sInstance = null;
288
Amith Yamasani7e99bc02013-04-16 18:24:51 -0700289 /** @hide */
Amith Yamasani27db4682013-03-30 17:07:47 -0700290 public synchronized static UserManager get(Context context) {
291 if (sInstance == null) {
292 sInstance = (UserManager) context.getSystemService(Context.USER_SERVICE);
293 }
294 return sInstance;
295 }
Maggie Benthalla12fccf2013-03-14 18:02:12 -0400296
Amith Yamasani258848d2012-08-10 17:06:33 -0700297 /** @hide */
298 public UserManager(Context context, IUserManager service) {
299 mService = service;
300 mContext = context;
301 }
302
303 /**
304 * Returns whether the system supports multiple users.
Kenny Guy1a447532014-02-20 21:55:32 +0000305 * @return true if multiple users can be created by user, false if it is a single user device.
Amith Yamasanie928d7d2012-09-17 21:46:51 -0700306 * @hide
Amith Yamasani258848d2012-08-10 17:06:33 -0700307 */
Jeff Sharkey4673e7e2012-09-19 13:14:30 -0700308 public static boolean supportsMultipleUsers() {
Kenny Guy1a447532014-02-20 21:55:32 +0000309 return getMaxSupportedUsers() > 1
310 && SystemProperties.getBoolean("fw.show_multiuserui",
311 Resources.getSystem().getBoolean(R.bool.config_enableMultiUserUI));
Amith Yamasani258848d2012-08-10 17:06:33 -0700312 }
313
Maggie Benthall67944582013-02-22 14:58:27 -0500314 /**
Jessica Hummelbe81c802014-04-22 15:49:22 +0100315 * Returns the user handle for the user that the calling process is running on.
316 *
Amith Yamasani258848d2012-08-10 17:06:33 -0700317 * @return the user handle of the user making this call.
318 * @hide
Maggie Benthall67944582013-02-22 14:58:27 -0500319 */
Amith Yamasani258848d2012-08-10 17:06:33 -0700320 public int getUserHandle() {
Dianne Hackborn79af1dd2012-08-16 16:42:52 -0700321 return UserHandle.myUserId();
Amith Yamasani258848d2012-08-10 17:06:33 -0700322 }
323
324 /**
Dianne Hackborn8832c182012-09-17 17:20:24 -0700325 * Returns the user name of the user making this call. This call is only
326 * available to applications on the system image; it requires the
327 * MANAGE_USERS permission.
Amith Yamasani258848d2012-08-10 17:06:33 -0700328 * @return the user name
329 */
330 public String getUserName() {
331 try {
332 return mService.getUserInfo(getUserHandle()).name;
333 } catch (RemoteException re) {
334 Log.w(TAG, "Could not get user name", re);
335 return "";
336 }
337 }
338
Dan Morrille4ab16a2012-09-20 20:25:55 -0700339 /**
340 * Used to determine whether the user making this call is subject to
341 * teleportations.
Maggie Benthalla12fccf2013-03-14 18:02:12 -0400342 * @return whether the user making this call is a goat
Dan Morrille4ab16a2012-09-20 20:25:55 -0700343 */
344 public boolean isUserAGoat() {
345 return false;
346 }
Amith Yamasanidf2e92a2013-03-01 17:04:38 -0800347
348 /**
Amith Yamasanie1375902013-04-13 16:48:35 -0700349 * 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 -0700350 * a reduced number of available apps, app restrictions and account restrictions.
Amith Yamasanie1375902013-04-13 16:48:35 -0700351 * @return whether the user making this call is a linked user
Amith Yamasani2555daf2013-04-25 13:39:27 -0700352 * @hide
Amith Yamasanidf2e92a2013-03-01 17:04:38 -0800353 */
Amith Yamasanie1375902013-04-13 16:48:35 -0700354 public boolean isLinkedUser() {
Amith Yamasanidf2e92a2013-03-01 17:04:38 -0800355 try {
Amith Yamasani71e6c692013-03-24 17:39:28 -0700356 return mService.isRestricted();
Amith Yamasanidf2e92a2013-03-01 17:04:38 -0800357 } catch (RemoteException re) {
Amith Yamasani46bc4eb2013-04-12 13:26:50 -0700358 Log.w(TAG, "Could not check if user is limited ", re);
Amith Yamasanidf2e92a2013-03-01 17:04:38 -0800359 return false;
360 }
361 }
362
Amith Yamasani258848d2012-08-10 17:06:33 -0700363 /**
Dianne Hackborna8a9bd62012-10-09 15:36:59 -0700364 * Return whether the given user is actively running. This means that
365 * the user is in the "started" state, not "stopped" -- it is currently
366 * allowed to run code through scheduled alarms, receiving broadcasts,
367 * etc. A started user may be either the current foreground user or a
368 * background user; the result here does not distinguish between the two.
369 * @param user The user to retrieve the running state for.
370 */
371 public boolean isUserRunning(UserHandle user) {
372 try {
373 return ActivityManagerNative.getDefault().isUserRunning(
374 user.getIdentifier(), false);
375 } catch (RemoteException e) {
376 return false;
377 }
378 }
379
380 /**
381 * Return whether the given user is actively running <em>or</em> stopping.
382 * This is like {@link #isUserRunning(UserHandle)}, but will also return
383 * true if the user had been running but is in the process of being stopped
384 * (but is not yet fully stopped, and still running some code).
385 * @param user The user to retrieve the running state for.
386 */
387 public boolean isUserRunningOrStopping(UserHandle user) {
388 try {
389 return ActivityManagerNative.getDefault().isUserRunning(
390 user.getIdentifier(), true);
391 } catch (RemoteException e) {
392 return false;
393 }
394 }
395
396 /**
Amith Yamasani258848d2012-08-10 17:06:33 -0700397 * Returns the UserInfo object describing a specific user.
Amith Yamasani195263742012-08-21 15:40:12 -0700398 * Requires {@link android.Manifest.permission#MANAGE_USERS} permission.
Amith Yamasani258848d2012-08-10 17:06:33 -0700399 * @param userHandle the user handle of the user whose information is being requested.
400 * @return the UserInfo object for a specific user.
401 * @hide
Dianne Hackbornb26306a2012-10-24 15:22:21 -0700402 */
Amith Yamasani258848d2012-08-10 17:06:33 -0700403 public UserInfo getUserInfo(int userHandle) {
404 try {
405 return mService.getUserInfo(userHandle);
406 } catch (RemoteException re) {
407 Log.w(TAG, "Could not get user info", re);
408 return null;
409 }
410 }
411
Amith Yamasani71e6c692013-03-24 17:39:28 -0700412 /**
413 * Returns the user-wide restrictions imposed on this user.
414 * @return a Bundle containing all the restrictions.
415 */
Amith Yamasanie4cf7342012-12-17 11:12:09 -0800416 public Bundle getUserRestrictions() {
417 return getUserRestrictions(Process.myUserHandle());
418 }
419
Amith Yamasani71e6c692013-03-24 17:39:28 -0700420 /**
421 * Returns the user-wide restrictions imposed on the user specified by <code>userHandle</code>.
422 * @param userHandle the UserHandle of the user for whom to retrieve the restrictions.
423 * @return a Bundle containing all the restrictions.
424 */
Amith Yamasanie4cf7342012-12-17 11:12:09 -0800425 public Bundle getUserRestrictions(UserHandle userHandle) {
426 try {
427 return mService.getUserRestrictions(userHandle.getIdentifier());
428 } catch (RemoteException re) {
429 Log.w(TAG, "Could not get user restrictions", re);
430 return Bundle.EMPTY;
431 }
432 }
433
Amith Yamasani71e6c692013-03-24 17:39:28 -0700434 /**
435 * Sets all the user-wide restrictions for this user.
Amith Yamasanibe465322014-04-24 13:45:17 -0700436 * Requires the MANAGE_USERS permission.
Amith Yamasani71e6c692013-03-24 17:39:28 -0700437 * @param restrictions the Bundle containing all the restrictions.
438 */
Amith Yamasanie4cf7342012-12-17 11:12:09 -0800439 public void setUserRestrictions(Bundle restrictions) {
440 setUserRestrictions(restrictions, Process.myUserHandle());
441 }
442
Amith Yamasani71e6c692013-03-24 17:39:28 -0700443 /**
444 * Sets all the user-wide restrictions for the specified user.
Amith Yamasanibe465322014-04-24 13:45:17 -0700445 * Requires the MANAGE_USERS permission.
Amith Yamasani71e6c692013-03-24 17:39:28 -0700446 * @param restrictions the Bundle containing all the restrictions.
447 * @param userHandle the UserHandle of the user for whom to set the restrictions.
448 */
Amith Yamasanie4cf7342012-12-17 11:12:09 -0800449 public void setUserRestrictions(Bundle restrictions, UserHandle userHandle) {
450 try {
451 mService.setUserRestrictions(restrictions, userHandle.getIdentifier());
452 } catch (RemoteException re) {
453 Log.w(TAG, "Could not set user restrictions", re);
454 }
455 }
456
Amith Yamasani71e6c692013-03-24 17:39:28 -0700457 /**
458 * Sets the value of a specific restriction.
Amith Yamasanibe465322014-04-24 13:45:17 -0700459 * Requires the MANAGE_USERS permission.
Amith Yamasani71e6c692013-03-24 17:39:28 -0700460 * @param key the key of the restriction
461 * @param value the value for the restriction
Amith Yamasani71e6c692013-03-24 17:39:28 -0700462 */
463 public void setUserRestriction(String key, boolean value) {
464 Bundle bundle = getUserRestrictions();
465 bundle.putBoolean(key, value);
466 setUserRestrictions(bundle);
467 }
468
469 /**
470 * @hide
471 * Sets the value of a specific restriction on a specific user.
Amith Yamasanibe465322014-04-24 13:45:17 -0700472 * Requires the MANAGE_USERS permission.
Amith Yamasani71e6c692013-03-24 17:39:28 -0700473 * @param key the key of the restriction
474 * @param value the value for the restriction
475 * @param userHandle the user whose restriction is to be changed.
476 */
Maggie Benthall67944582013-02-22 14:58:27 -0500477 public void setUserRestriction(String key, boolean value, UserHandle userHandle) {
478 Bundle bundle = getUserRestrictions(userHandle);
479 bundle.putBoolean(key, value);
480 setUserRestrictions(bundle, userHandle);
481 }
482
Amith Yamasani258848d2012-08-10 17:06:33 -0700483 /**
Maggie Benthalla12fccf2013-03-14 18:02:12 -0400484 * @hide
485 * Returns whether the current user has been disallowed from performing certain actions
486 * or setting certain settings.
487 * @param restrictionKey the string key representing the restriction
488 */
489 public boolean hasUserRestriction(String restrictionKey) {
David Christieb12ba932013-09-03 17:15:28 -0700490 return hasUserRestriction(restrictionKey, Process.myUserHandle());
491 }
492
493 /**
494 * @hide
495 * Returns whether the given user has been disallowed from performing certain actions
496 * or setting certain settings.
497 * @param restrictionKey the string key representing the restriction
498 * @param userHandle the UserHandle of the user for whom to retrieve the restrictions.
499 */
500 public boolean hasUserRestriction(String restrictionKey, UserHandle userHandle) {
501 return getUserRestrictions(userHandle).getBoolean(restrictionKey, false);
Maggie Benthalla12fccf2013-03-14 18:02:12 -0400502 }
503
504 /**
Dianne Hackborn33f9cb82012-10-04 17:15:10 -0700505 * Return the serial number for a user. This is a device-unique
Dianne Hackbornb26306a2012-10-24 15:22:21 -0700506 * number assigned to that user; if the user is deleted and then a new
507 * user created, the new users will not be given the same serial number.
Dianne Hackborn33f9cb82012-10-04 17:15:10 -0700508 * @param user The user whose serial number is to be retrieved.
Dianne Hackbornb26306a2012-10-24 15:22:21 -0700509 * @return The serial number of the given user; returns -1 if the
510 * given UserHandle does not exist.
Dianne Hackborn33f9cb82012-10-04 17:15:10 -0700511 * @see #getUserForSerialNumber(long)
512 */
513 public long getSerialNumberForUser(UserHandle user) {
514 return getUserSerialNumber(user.getIdentifier());
515 }
516
517 /**
518 * Return the user associated with a serial number previously
519 * returned by {@link #getSerialNumberForUser(UserHandle)}.
520 * @param serialNumber The serial number of the user that is being
521 * retrieved.
522 * @return Return the user associated with the serial number, or null
523 * if there is not one.
524 * @see #getSerialNumberForUser(UserHandle)
525 */
526 public UserHandle getUserForSerialNumber(long serialNumber) {
527 int ident = getUserHandle((int)serialNumber);
528 return ident >= 0 ? new UserHandle(ident) : null;
529 }
530
531 /**
Amith Yamasani258848d2012-08-10 17:06:33 -0700532 * Creates a user with the specified name and options.
Amith Yamasani195263742012-08-21 15:40:12 -0700533 * Requires {@link android.Manifest.permission#MANAGE_USERS} permission.
Amith Yamasani258848d2012-08-10 17:06:33 -0700534 *
535 * @param name the user's name
536 * @param flags flags that identify the type of user and other properties.
537 * @see UserInfo
538 *
539 * @return the UserInfo object for the created user, or null if the user could not be created.
540 * @hide
541 */
542 public UserInfo createUser(String name, int flags) {
543 try {
544 return mService.createUser(name, flags);
545 } catch (RemoteException re) {
546 Log.w(TAG, "Could not create a user", re);
547 return null;
548 }
549 }
550
551 /**
Kenny Guy2a764942014-04-02 13:29:20 +0100552 * Creates a user with the specified name and options as a profile of another user.
Kenny Guya52dc3e2014-02-11 15:33:14 +0000553 * Requires {@link android.Manifest.permission#MANAGE_USERS} permission.
554 *
555 * @param name the user's name
556 * @param flags flags that identify the type of user and other properties.
557 * @see UserInfo
Kenny Guy2a764942014-04-02 13:29:20 +0100558 * @param userHandle new user will be a profile of this use.
Kenny Guya52dc3e2014-02-11 15:33:14 +0000559 *
560 * @return the UserInfo object for the created user, or null if the user could not be created.
561 * @hide
562 */
Kenny Guy2a764942014-04-02 13:29:20 +0100563 public UserInfo createProfileForUser(String name, int flags, int userHandle) {
Kenny Guya52dc3e2014-02-11 15:33:14 +0000564 try {
Kenny Guy2a764942014-04-02 13:29:20 +0100565 return mService.createProfileForUser(name, flags, userHandle);
Kenny Guya52dc3e2014-02-11 15:33:14 +0000566 } catch (RemoteException re) {
567 Log.w(TAG, "Could not create a user", re);
568 return null;
569 }
570 }
571
572 /**
Alexandra Gherghinadf35d572014-04-09 13:54:39 +0100573 * Sets the user as enabled, if such an user exists.
574 * Requires {@link android.Manifest.permission#MANAGE_USERS} permission.
575 * Note that the default is true, it's only that managed profiles might not be enabled.
576 *
577 * @param userHandle the id of the profile to enable
578 * @hide
579 */
580 public void setUserEnabled(int userHandle) {
581 try {
582 mService.setUserEnabled(userHandle);
583 } catch (RemoteException e) {
584 Log.w(TAG, "Could not enable the profile", e);
585 }
586 }
587
588 /**
Dianne Hackbornb26306a2012-10-24 15:22:21 -0700589 * Return the number of users currently created on the device.
590 */
591 public int getUserCount() {
592 List<UserInfo> users = getUsers();
593 return users != null ? users.size() : 1;
594 }
595
596 /**
Amith Yamasani258848d2012-08-10 17:06:33 -0700597 * Returns information for all users on this device.
Amith Yamasani195263742012-08-21 15:40:12 -0700598 * Requires {@link android.Manifest.permission#MANAGE_USERS} permission.
Amith Yamasani258848d2012-08-10 17:06:33 -0700599 * @return the list of users that were created.
600 * @hide
601 */
602 public List<UserInfo> getUsers() {
603 try {
Amith Yamasani920ace02012-09-20 22:15:37 -0700604 return mService.getUsers(false);
605 } catch (RemoteException re) {
606 Log.w(TAG, "Could not get user list", re);
607 return null;
608 }
609 }
610
611 /**
Kenny Guy2a764942014-04-02 13:29:20 +0100612 * Returns list of the profiles of userHandle including
613 * userHandle itself.
Alexandra Gherghina385124d2014-04-03 13:37:39 +0100614 * Note that it this returns both enabled and not enabled profiles. See
615 * {@link #getUserProfiles()} if you need only the enabled ones.
Amith Yamasani4f582632014-02-19 14:31:52 -0800616 *
Kenny Guy2a764942014-04-02 13:29:20 +0100617 * Requires {@link android.Manifest.permission#MANAGE_USERS} permission.
618 * @param userHandle profiles of this user will be returned.
619 * @return the list of profiles.
620 * @hide
621 */
622 public List<UserInfo> getProfiles(int userHandle) {
Kenny Guya52dc3e2014-02-11 15:33:14 +0000623 try {
Alexandra Gherghina385124d2014-04-03 13:37:39 +0100624 return mService.getProfiles(userHandle, false /* enabledOnly */);
Kenny Guya52dc3e2014-02-11 15:33:14 +0000625 } catch (RemoteException re) {
626 Log.w(TAG, "Could not get user list", re);
627 return null;
628 }
629 }
630
631 /**
Jessica Hummelbe81c802014-04-22 15:49:22 +0100632 * Returns a list of UserHandles for profiles associated with the user that the calling process
633 * is running on, including the user itself.
Amith Yamasani4f582632014-02-19 14:31:52 -0800634 *
635 * @return A non-empty list of UserHandles associated with the calling user.
636 */
637 public List<UserHandle> getUserProfiles() {
638 ArrayList<UserHandle> profiles = new ArrayList<UserHandle>();
Alexandra Gherghina385124d2014-04-03 13:37:39 +0100639 List<UserInfo> users = new ArrayList<UserInfo>();
640 try {
Alexandra Gherghinaf2e7b3f2014-04-30 17:01:03 +0000641 users = mService.getProfiles(UserHandle.myUserId(), true /* enabledOnly */);
Alexandra Gherghina385124d2014-04-03 13:37:39 +0100642 } catch (RemoteException re) {
643 Log.w(TAG, "Could not get user list", re);
644 return null;
645 }
Amith Yamasani4f582632014-02-19 14:31:52 -0800646 for (UserInfo info : users) {
647 UserHandle userHandle = new UserHandle(info.id);
648 profiles.add(userHandle);
649 }
650 return profiles;
651 }
652
Amith Yamasani7dda2652014-04-11 14:57:12 -0700653 /**
Jessica Hummelbe81c802014-04-22 15:49:22 +0100654 * Returns the parent of the profile which this method is called from
655 * or null if called from a user that is not a profile.
656 *
657 * @hide
658 */
659 public UserInfo getProfileParent(int userHandle) {
660 try {
661 return mService.getProfileParent(userHandle);
662 } catch (RemoteException re) {
663 Log.w(TAG, "Could not get profile parent", re);
664 return null;
665 }
666 }
667
668 /**
Amith Yamasani7dda2652014-04-11 14:57:12 -0700669 * If the target user is a managed profile of the calling user or the caller
670 * is itself a managed profile, then this returns a badged copy of the given
671 * icon to be able to distinguish it from the original icon.
672 * <P>
673 * If the original drawable is not a BitmapDrawable, then the original
674 * drawable is returned.
675 * </P>
676 *
677 * @param icon The icon to badge.
678 * @param user The target user.
679 * @return A drawable that combines the original icon and a badge as
680 * determined by the system.
681 */
Amith Yamasani4f582632014-02-19 14:31:52 -0800682 public Drawable getBadgedDrawableForUser(Drawable icon, UserHandle user) {
683 int badgeResId = getBadgeResIdForUser(user.getIdentifier());
684 if (badgeResId == 0) {
685 return icon;
686 } else {
687 Drawable badgeIcon = mContext.getPackageManager()
688 .getDrawable("system", badgeResId, null);
689 return getMergedDrawable(icon, badgeIcon);
690 }
691 }
692
693 private int getBadgeResIdForUser(int userHandle) {
694 // Return the framework-provided badge.
Jessica Hummelbe81c802014-04-22 15:49:22 +0100695 List<UserInfo> userProfiles = getProfiles(getUserHandle());
Amith Yamasani5abdbb62014-04-08 17:23:46 -0700696 for (UserInfo user : userProfiles) {
697 if (user.id == userHandle
698 && user.isManagedProfile()) {
699 return com.android.internal.R.drawable.ic_corp_badge;
Amith Yamasani4f582632014-02-19 14:31:52 -0800700 }
701 }
Amith Yamasani5abdbb62014-04-08 17:23:46 -0700702 return 0;
Amith Yamasani4f582632014-02-19 14:31:52 -0800703 }
704
705 private Drawable getMergedDrawable(Drawable icon, Drawable badge) {
706 final int width = icon.getIntrinsicWidth();
707 final int height = icon.getIntrinsicHeight();
708 Bitmap bitmap = Bitmap.createBitmap(width, height, Config.ARGB_8888);
709 Canvas canvas = new Canvas(bitmap);
710 icon.setBounds(0, 0, width, height);
711 icon.draw(canvas);
712 badge.setBounds(0, 0, width, height);
713 badge.draw(canvas);
714 BitmapDrawable merged = new BitmapDrawable(bitmap);
715 if (icon instanceof BitmapDrawable) {
716 merged.setTargetDensity(((BitmapDrawable) icon).getBitmap().getDensity());
717 }
718 return merged;
719 }
720
721 /**
722 * Returns information for all users on this device. Requires
723 * {@link android.Manifest.permission#MANAGE_USERS} permission.
Emily Bernier394a6cd2014-05-07 12:49:20 -0400724 *
Amith Yamasani4f582632014-02-19 14:31:52 -0800725 * @param excludeDying specify if the list should exclude users being
726 * removed.
Amith Yamasani920ace02012-09-20 22:15:37 -0700727 * @return the list of users that were created.
728 * @hide
729 */
730 public List<UserInfo> getUsers(boolean excludeDying) {
731 try {
732 return mService.getUsers(excludeDying);
Amith Yamasani258848d2012-08-10 17:06:33 -0700733 } catch (RemoteException re) {
734 Log.w(TAG, "Could not get user list", re);
735 return null;
736 }
737 }
738
739 /**
740 * Removes a user and all associated data.
Amith Yamasani195263742012-08-21 15:40:12 -0700741 * Requires {@link android.Manifest.permission#MANAGE_USERS} permission.
Amith Yamasani258848d2012-08-10 17:06:33 -0700742 * @param userHandle the integer handle of the user, where 0 is the primary user.
743 * @hide
744 */
745 public boolean removeUser(int userHandle) {
746 try {
747 return mService.removeUser(userHandle);
748 } catch (RemoteException re) {
749 Log.w(TAG, "Could not remove user ", re);
750 return false;
751 }
752 }
753
754 /**
755 * Updates the user's name.
Amith Yamasani195263742012-08-21 15:40:12 -0700756 * Requires {@link android.Manifest.permission#MANAGE_USERS} permission.
Amith Yamasani258848d2012-08-10 17:06:33 -0700757 *
758 * @param userHandle the user's integer handle
759 * @param name the new name for the user
760 * @hide
761 */
762 public void setUserName(int userHandle, String name) {
763 try {
764 mService.setUserName(userHandle, name);
765 } catch (RemoteException re) {
766 Log.w(TAG, "Could not set the user name ", re);
767 }
768 }
769
770 /**
Amith Yamasanie928d7d2012-09-17 21:46:51 -0700771 * Sets the user's photo.
Amith Yamasani258848d2012-08-10 17:06:33 -0700772 * @param userHandle the user for whom to change the photo.
Amith Yamasanie928d7d2012-09-17 21:46:51 -0700773 * @param icon the bitmap to set as the photo.
Amith Yamasani258848d2012-08-10 17:06:33 -0700774 * @hide
775 */
Amith Yamasanie928d7d2012-09-17 21:46:51 -0700776 public void setUserIcon(int userHandle, Bitmap icon) {
Amith Yamasani258848d2012-08-10 17:06:33 -0700777 try {
Amith Yamasanie928d7d2012-09-17 21:46:51 -0700778 mService.setUserIcon(userHandle, icon);
Amith Yamasani258848d2012-08-10 17:06:33 -0700779 } catch (RemoteException re) {
780 Log.w(TAG, "Could not set the user icon ", re);
Amith Yamasani258848d2012-08-10 17:06:33 -0700781 }
782 }
783
784 /**
Amith Yamasani3b49f072012-09-17 10:21:43 -0700785 * Returns a file descriptor for the user's photo. PNG data can be read from this file.
786 * @param userHandle the user whose photo we want to read.
Amith Yamasanie928d7d2012-09-17 21:46:51 -0700787 * @return a {@link Bitmap} of the user's photo, or null if there's no photo.
Amith Yamasani3b49f072012-09-17 10:21:43 -0700788 * @hide
789 */
Amith Yamasanie928d7d2012-09-17 21:46:51 -0700790 public Bitmap getUserIcon(int userHandle) {
Amith Yamasani3b49f072012-09-17 10:21:43 -0700791 try {
792 return mService.getUserIcon(userHandle);
793 } catch (RemoteException re) {
Amith Yamasanie928d7d2012-09-17 21:46:51 -0700794 Log.w(TAG, "Could not get the user icon ", re);
Amith Yamasani3b49f072012-09-17 10:21:43 -0700795 return null;
796 }
797 }
798
799 /**
Amith Yamasani258848d2012-08-10 17:06:33 -0700800 * Enable or disable the use of a guest account. If disabled, the existing guest account
801 * will be wiped.
Amith Yamasani195263742012-08-21 15:40:12 -0700802 * Requires {@link android.Manifest.permission#MANAGE_USERS} permission.
Amith Yamasani258848d2012-08-10 17:06:33 -0700803 * @param enable whether to enable a guest account.
804 * @hide
805 */
806 public void setGuestEnabled(boolean enable) {
807 try {
808 mService.setGuestEnabled(enable);
809 } catch (RemoteException re) {
810 Log.w(TAG, "Could not change guest account availability to " + enable);
811 }
812 }
813
814 /**
815 * Checks if a guest user is enabled for this device.
Amith Yamasani195263742012-08-21 15:40:12 -0700816 * Requires {@link android.Manifest.permission#MANAGE_USERS} permission.
Amith Yamasani258848d2012-08-10 17:06:33 -0700817 * @return whether a guest user is enabled
818 * @hide
819 */
820 public boolean isGuestEnabled() {
821 try {
822 return mService.isGuestEnabled();
823 } catch (RemoteException re) {
824 Log.w(TAG, "Could not retrieve guest enabled state");
825 return false;
826 }
827 }
828
829 /**
830 * Wipes all the data for a user, but doesn't remove the user.
Amith Yamasani195263742012-08-21 15:40:12 -0700831 * Requires {@link android.Manifest.permission#MANAGE_USERS} permission.
Amith Yamasani258848d2012-08-10 17:06:33 -0700832 * @param userHandle
833 * @hide
834 */
835 public void wipeUser(int userHandle) {
836 try {
837 mService.wipeUser(userHandle);
838 } catch (RemoteException re) {
839 Log.w(TAG, "Could not wipe user " + userHandle);
840 }
841 }
842
843 /**
844 * Returns the maximum number of users that can be created on this device. A return value
845 * of 1 means that it is a single user device.
846 * @hide
Maggie Benthalla12fccf2013-03-14 18:02:12 -0400847 * @return a value greater than or equal to 1
Amith Yamasani258848d2012-08-10 17:06:33 -0700848 */
Jeff Sharkey27bd34d2012-09-16 12:49:00 -0700849 public static int getMaxSupportedUsers() {
Amith Yamasaniff549202012-10-12 12:44:49 -0700850 // Don't allow multiple users on certain builds
851 if (android.os.Build.ID.startsWith("JVP")) return 1;
Jeff Sharkey27bd34d2012-09-16 12:49:00 -0700852 return SystemProperties.getInt("fw.max_users",
853 Resources.getSystem().getInteger(R.integer.config_multiuserMaximumUsers));
Amith Yamasani258848d2012-08-10 17:06:33 -0700854 }
Amith Yamasani2a003292012-08-14 18:25:45 -0700855
856 /**
Kenny Guy1a447532014-02-20 21:55:32 +0000857 * Returns true if the user switcher should be shown, this will be if there
858 * are multiple users that aren't managed profiles.
859 * @hide
860 * @return true if user switcher should be shown.
861 */
862 public boolean isUserSwitcherEnabled() {
863 List<UserInfo> users = getUsers(true);
864 if (users == null) {
865 return false;
866 }
867 int switchableUserCount = 0;
868 for (UserInfo user : users) {
869 if (user.supportsSwitchTo()) {
870 ++switchableUserCount;
871 }
872 }
873 return switchableUserCount > 1;
874 }
875
876 /**
Amith Yamasani2a003292012-08-14 18:25:45 -0700877 * Returns a serial number on this device for a given userHandle. User handles can be recycled
878 * when deleting and creating users, but serial numbers are not reused until the device is wiped.
879 * @param userHandle
880 * @return a serial number associated with that user, or -1 if the userHandle is not valid.
881 * @hide
882 */
883 public int getUserSerialNumber(int userHandle) {
884 try {
885 return mService.getUserSerialNumber(userHandle);
886 } catch (RemoteException re) {
887 Log.w(TAG, "Could not get serial number for user " + userHandle);
888 }
889 return -1;
890 }
891
892 /**
893 * Returns a userHandle on this device for a given user serial number. User handles can be
894 * recycled when deleting and creating users, but serial numbers are not reused until the device
895 * is wiped.
896 * @param userSerialNumber
897 * @return the userHandle associated with that user serial number, or -1 if the serial number
898 * is not valid.
899 * @hide
900 */
901 public int getUserHandle(int userSerialNumber) {
902 try {
903 return mService.getUserHandle(userSerialNumber);
904 } catch (RemoteException re) {
905 Log.w(TAG, "Could not get userHandle for user " + userSerialNumber);
906 }
907 return -1;
908 }
Maggie Benthall67944582013-02-22 14:58:27 -0500909
Amith Yamasani7e99bc02013-04-16 18:24:51 -0700910 /**
911 * Returns a Bundle containing any saved application restrictions for this user, for the
912 * given package name. Only an application with this package name can call this method.
913 * @param packageName the package name of the calling application
914 * @return a Bundle with the restrictions as key/value pairs, or null if there are no
915 * saved restrictions. The values can be of type Boolean, String or String[], depending
916 * on the restriction type, as defined by the application.
917 */
918 public Bundle getApplicationRestrictions(String packageName) {
919 try {
920 return mService.getApplicationRestrictions(packageName);
921 } catch (RemoteException re) {
922 Log.w(TAG, "Could not get application restrictions for package " + packageName);
923 }
924 return null;
925 }
Amith Yamasanidf2e92a2013-03-01 17:04:38 -0800926
927 /**
928 * @hide
929 */
Amith Yamasani7e99bc02013-04-16 18:24:51 -0700930 public Bundle getApplicationRestrictions(String packageName, UserHandle user) {
Amith Yamasanidf2e92a2013-03-01 17:04:38 -0800931 try {
Amith Yamasani7e99bc02013-04-16 18:24:51 -0700932 return mService.getApplicationRestrictionsForUser(packageName, user.getIdentifier());
Amith Yamasanidf2e92a2013-03-01 17:04:38 -0800933 } catch (RemoteException re) {
934 Log.w(TAG, "Could not get application restrictions for user " + user.getIdentifier());
935 }
936 return null;
937 }
938
939 /**
940 * @hide
941 */
Amith Yamasani7e99bc02013-04-16 18:24:51 -0700942 public void setApplicationRestrictions(String packageName, Bundle restrictions,
Amith Yamasanidf2e92a2013-03-01 17:04:38 -0800943 UserHandle user) {
944 try {
Amith Yamasani7e99bc02013-04-16 18:24:51 -0700945 mService.setApplicationRestrictions(packageName, restrictions, user.getIdentifier());
Amith Yamasanidf2e92a2013-03-01 17:04:38 -0800946 } catch (RemoteException re) {
947 Log.w(TAG, "Could not set application restrictions for user " + user.getIdentifier());
948 }
949 }
Amith Yamasani655d0e22013-06-12 14:19:10 -0700950
951 /**
Amith Yamasanid304af62013-09-05 09:30:23 -0700952 * Sets a new challenge PIN for restrictions. This is only for use by pre-installed
953 * apps and requires the MANAGE_USERS permission.
954 * @param newPin the PIN to use for challenge dialogs.
955 * @return Returns true if the challenge PIN was set successfully.
Amith Yamasani655d0e22013-06-12 14:19:10 -0700956 */
Amith Yamasanid304af62013-09-05 09:30:23 -0700957 public boolean setRestrictionsChallenge(String newPin) {
Amith Yamasani655d0e22013-06-12 14:19:10 -0700958 try {
Amith Yamasanid304af62013-09-05 09:30:23 -0700959 return mService.setRestrictionsChallenge(newPin);
Amith Yamasani655d0e22013-06-12 14:19:10 -0700960 } catch (RemoteException re) {
961 Log.w(TAG, "Could not change restrictions pin");
962 }
963 return false;
964 }
965
966 /**
967 * @hide
968 * @param pin The PIN to verify, or null to get the number of milliseconds to wait for before
969 * allowing the user to enter the PIN.
970 * @return Returns a positive number (including zero) for how many milliseconds before
971 * you can accept another PIN, when the input is null or the input doesn't match the saved PIN.
972 * Returns {@link #PIN_VERIFICATION_SUCCESS} if the input matches the saved PIN. Returns
973 * {@link #PIN_VERIFICATION_FAILED_NOT_SET} if there is no PIN set.
974 */
Amith Yamasanid304af62013-09-05 09:30:23 -0700975 public int checkRestrictionsChallenge(String pin) {
Amith Yamasani655d0e22013-06-12 14:19:10 -0700976 try {
Amith Yamasanid304af62013-09-05 09:30:23 -0700977 return mService.checkRestrictionsChallenge(pin);
Amith Yamasani655d0e22013-06-12 14:19:10 -0700978 } catch (RemoteException re) {
979 Log.w(TAG, "Could not check restrictions pin");
980 }
981 return PIN_VERIFICATION_FAILED_INCORRECT;
982 }
983
984 /**
Amith Yamasanid304af62013-09-05 09:30:23 -0700985 * @hide
Amith Yamasani655d0e22013-06-12 14:19:10 -0700986 * Checks whether the user has restrictions that are PIN-protected. An application that
987 * participates in restrictions can check if the owner has requested a PIN challenge for
988 * any restricted operations. If there is a PIN in effect, the application should launch
Amith Yamasanid304af62013-09-05 09:30:23 -0700989 * the PIN challenge activity {@link android.content.Intent#ACTION_RESTRICTIONS_CHALLENGE}.
990 * @see android.content.Intent#ACTION_RESTRICTIONS_CHALLENGE
Amith Yamasani655d0e22013-06-12 14:19:10 -0700991 * @return whether a restrictions PIN is in effect.
992 */
Amith Yamasanid304af62013-09-05 09:30:23 -0700993 public boolean hasRestrictionsChallenge() {
Amith Yamasani655d0e22013-06-12 14:19:10 -0700994 try {
Amith Yamasanid304af62013-09-05 09:30:23 -0700995 return mService.hasRestrictionsChallenge();
Amith Yamasani655d0e22013-06-12 14:19:10 -0700996 } catch (RemoteException re) {
997 Log.w(TAG, "Could not change restrictions pin");
998 }
999 return false;
1000 }
Amith Yamasani1a7472e2013-07-02 11:17:30 -07001001
1002 /** @hide */
1003 public void removeRestrictions() {
1004 try {
1005 mService.removeRestrictions();
1006 } catch (RemoteException re) {
1007 Log.w(TAG, "Could not change restrictions pin");
1008 }
1009 }
Amith Yamasani258848d2012-08-10 17:06:33 -07001010}