UserManager.java revision c7d62f02b8acfd0a6b31f8544ec2c07e780fe4bb
1/* 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 18import android.annotation.SystemApi; 19import android.app.ActivityManager; 20import android.app.ActivityManagerNative; 21import android.content.Context; 22import android.content.pm.UserInfo; 23import android.content.res.Resources; 24import android.graphics.Bitmap; 25import android.graphics.Rect; 26import android.graphics.drawable.Drawable; 27import android.provider.Settings; 28import android.util.Log; 29import android.view.WindowManager.LayoutParams; 30 31import com.android.internal.R; 32 33import java.util.ArrayList; 34import java.util.List; 35 36/** 37 * Manages users and user details on a multi-user system. 38 */ 39public class UserManager { 40 41 private static String TAG = "UserManager"; 42 private final IUserManager mService; 43 private final Context mContext; 44 45 /** 46 * Key for user restrictions. Specifies if a user is disallowed from adding and removing 47 * accounts. 48 * The default value is <code>false</code>. 49 * <p/> 50 * Type: Boolean 51 * @see #setUserRestrictions(Bundle) 52 * @see #getUserRestrictions() 53 */ 54 public static final String DISALLOW_MODIFY_ACCOUNTS = "no_modify_accounts"; 55 56 /** 57 * Key for user restrictions. Specifies if a user is disallowed from changing Wi-Fi 58 * access points. The default value is <code>false</code>. 59 * <p/> 60 * Type: Boolean 61 * @see #setUserRestrictions(Bundle) 62 * @see #getUserRestrictions() 63 */ 64 public static final String DISALLOW_CONFIG_WIFI = "no_config_wifi"; 65 66 /** 67 * Key for user restrictions. Specifies if a user is disallowed from installing applications. 68 * The default value is <code>false</code>. 69 * <p/> 70 * Type: Boolean 71 * @see #setUserRestrictions(Bundle) 72 * @see #getUserRestrictions() 73 */ 74 public static final String DISALLOW_INSTALL_APPS = "no_install_apps"; 75 76 /** 77 * Key for user restrictions. Specifies if a user is disallowed from uninstalling applications. 78 * The default value is <code>false</code>. 79 * <p/> 80 * Type: Boolean 81 * @see #setUserRestrictions(Bundle) 82 * @see #getUserRestrictions() 83 */ 84 public static final String DISALLOW_UNINSTALL_APPS = "no_uninstall_apps"; 85 86 /** 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/> 90 * Type: Boolean 91 * @see #setUserRestrictions(Bundle) 92 * @see #getUserRestrictions() 93 */ 94 public static final String DISALLOW_SHARE_LOCATION = "no_share_location"; 95 96 /** 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 117 /** 118 * Key for user restrictions. Specifies if a user is disallowed from transferring files over 119 * USB. This can only be set by device owners. 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 127 /** 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 itself and other 139 * users. 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 147 /** 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. This can only be set by device owners. The default value is 170 * <code>false</code>. 171 * <p/> 172 * Type: Boolean 173 * @see #setUserRestrictions(Bundle) 174 * @see #getUserRestrictions() 175 */ 176 public static final String DISALLOW_CONFIG_TETHERING = "no_config_tethering"; 177 178 /** 179 * Key for user restrictions. Specifies if a user is disallowed from factory resetting 180 * from Settings. This can only be set by device owners. The default value is 181 * <code>false</code>. 182 * <p> 183 * @see #setUserRestrictions(Bundle) 184 * @see #getUserRestrictions() 185 */ 186 public static final String DISALLOW_FACTORY_RESET = "no_factory_reset"; 187 188 /** 189 * Key for user restrictions. Specifies if a user is disallowed from adding new users and 190 * profiles. This can only be set by device owners. The default value is <code>false</code>. 191 * <p> 192 * Type: Boolean 193 * @see #setUserRestrictions(Bundle) 194 * @see #getUserRestrictions() 195 */ 196 public static final String DISALLOW_ADD_USER = "no_add_user"; 197 198 /** 199 * Key for user restrictions. Specifies if a user is disallowed from disabling application 200 * verification. The default value is <code>false</code>. 201 * <p> 202 * Type: Boolean 203 * @see #setUserRestrictions(Bundle) 204 * @see #getUserRestrictions() 205 */ 206 public static final String ENSURE_VERIFY_APPS = "ensure_verify_apps"; 207 208 /** 209 * Key for user restrictions. Specifies if a user is disallowed from configuring cell 210 * broadcasts. This can only be set by device owners. The default value is <code>false</code>. 211 * <p> 212 * Type: Boolean 213 * @see #setUserRestrictions(Bundle) 214 * @see #getUserRestrictions() 215 */ 216 public static final String DISALLOW_CONFIG_CELL_BROADCASTS = "no_config_cell_broadcasts"; 217 218 /** 219 * Key for user restrictions. Specifies if a user is disallowed from configuring mobile 220 * networks. This can only be set by device owners. The default value is <code>false</code>. 221 * <p> 222 * Type: Boolean 223 * @see #setUserRestrictions(Bundle) 224 * @see #getUserRestrictions() 225 */ 226 public static final String DISALLOW_CONFIG_MOBILE_NETWORKS = "no_config_mobile_networks"; 227 228 /** 229 * Key for user restrictions. Specifies if a user is disallowed from modifying 230 * applications in Settings or launchers. The following actions will not be allowed when this 231 * restriction is enabled: 232 * <li>uninstalling apps</li> 233 * <li>disabling apps</li> 234 * <li>clearing app caches</li> 235 * <li>clearing app data</li> 236 * <li>force stopping apps</li> 237 * <li>clearing app defaults</li> 238 * <p> 239 * 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_APPS_CONTROL = "no_control_apps"; 246 247 /** 248 * Key for user restrictions. Specifies if a user is disallowed from mounting 249 * physical external media. This can only be set by device owners. The default value is 250 * <code>false</code>. 251 * <p/> 252 * Type: Boolean 253 * @see #setUserRestrictions(Bundle) 254 * @see #getUserRestrictions() 255 */ 256 public static final String DISALLOW_MOUNT_PHYSICAL_MEDIA = "no_physical_media"; 257 258 /** 259 * Key for user restrictions. Specifies if a user is disallowed from adjusting microphone 260 * volume. If set, the microphone will be muted. This can only be set by device owners. 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_UNMUTE_MICROPHONE = "no_unmute_microphone"; 268 269 /** 270 * Key for user restrictions. Specifies if a user is disallowed from adjusting the master 271 * volume. If set, the master volume will be muted. This can only be set by device owners. 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_ADJUST_VOLUME = "no_adjust_volume"; 279 280 /** 281 * Key for user restrictions. Specifies that the user is not allowed to make outgoing 282 * phone calls. Emergency calls are still permitted. 283 * The default value is <code>false</code>. 284 * <p/> 285 * Type: Boolean 286 * @see #setUserRestrictions(Bundle) 287 * @see #getUserRestrictions() 288 */ 289 public static final String DISALLOW_OUTGOING_CALLS = "no_outgoing_calls"; 290 291 /** 292 * Key for user restrictions. Specifies that the user is not allowed to send or receive 293 * SMS messages. This can only be set by device owners. The default value is <code>false</code>. 294 * <p/> 295 * Type: Boolean 296 * @see #setUserRestrictions(Bundle) 297 * @see #getUserRestrictions() 298 */ 299 public static final String DISALLOW_SMS = "no_sms"; 300 301 /** 302 * Key for user restrictions. Specifies that windows besides app windows should not be 303 * created. This will block the creation of the following types of windows. 304 * <li>{@link LayoutParams#TYPE_TOAST}</li> 305 * <li>{@link LayoutParams#TYPE_PHONE}</li> 306 * <li>{@link LayoutParams#TYPE_PRIORITY_PHONE}</li> 307 * <li>{@link LayoutParams#TYPE_SYSTEM_ALERT}</li> 308 * <li>{@link LayoutParams#TYPE_SYSTEM_ERROR}</li> 309 * <li>{@link LayoutParams#TYPE_SYSTEM_OVERLAY}</li> 310 * 311 * <p>This can only be set by device owners. The default value is <code>false</code>. 312 * <p/> 313 * Type: Boolean 314 * @see #setUserRestrictions(Bundle) 315 * @see #getUserRestrictions() 316 */ 317 public static final String DISALLOW_CREATE_WINDOWS = "no_create_windows"; 318 319 /** 320 * Key for user restrictions. Specifies if what is copied in the clipboard of this profile can 321 * be pasted in related profiles. Does not restrict if the clipboard of related profiles can be 322 * pasted in this profile. 323 * The default value is <code>false</code>. 324 * <p/> 325 * Type: Boolean 326 * @see #setUserRestrictions(Bundle) 327 * @see #getUserRestrictions() 328 */ 329 public static final String DISALLOW_CROSS_PROFILE_COPY_PASTE = "no_cross_profile_copy_paste"; 330 331 /** @hide */ 332 public static final int PIN_VERIFICATION_FAILED_INCORRECT = -3; 333 /** @hide */ 334 public static final int PIN_VERIFICATION_FAILED_NOT_SET = -2; 335 /** @hide */ 336 public static final int PIN_VERIFICATION_SUCCESS = -1; 337 338 private static UserManager sInstance = null; 339 340 /** @hide */ 341 public synchronized static UserManager get(Context context) { 342 if (sInstance == null) { 343 sInstance = (UserManager) context.getSystemService(Context.USER_SERVICE); 344 } 345 return sInstance; 346 } 347 348 /** @hide */ 349 public UserManager(Context context, IUserManager service) { 350 mService = service; 351 mContext = context; 352 } 353 354 /** 355 * Returns whether the system supports multiple users. 356 * @return true if multiple users can be created by user, false if it is a single user device. 357 * @hide 358 */ 359 public static boolean supportsMultipleUsers() { 360 return getMaxSupportedUsers() > 1 361 && SystemProperties.getBoolean("fw.show_multiuserui", 362 Resources.getSystem().getBoolean(R.bool.config_enableMultiUserUI)); 363 } 364 365 /** 366 * Returns the user handle for the user that the calling process is running on. 367 * 368 * @return the user handle of the user making this call. 369 * @hide 370 */ 371 public int getUserHandle() { 372 return UserHandle.myUserId(); 373 } 374 375 /** 376 * Returns the user name of the user making this call. This call is only 377 * available to applications on the system image; it requires the 378 * MANAGE_USERS permission. 379 * @return the user name 380 */ 381 public String getUserName() { 382 try { 383 return mService.getUserInfo(getUserHandle()).name; 384 } catch (RemoteException re) { 385 Log.w(TAG, "Could not get user name", re); 386 return ""; 387 } 388 } 389 390 /** 391 * Used to determine whether the user making this call is subject to 392 * teleportations. 393 * @return whether the user making this call is a goat 394 */ 395 public boolean isUserAGoat() { 396 return false; 397 } 398 399 /** 400 * Used to check if the user making this call is linked to another user. Linked users may have 401 * a reduced number of available apps, app restrictions and account restrictions. 402 * @return whether the user making this call is a linked user 403 * @hide 404 */ 405 public boolean isLinkedUser() { 406 try { 407 return mService.isRestricted(); 408 } catch (RemoteException re) { 409 Log.w(TAG, "Could not check if user is limited ", re); 410 return false; 411 } 412 } 413 414 /** 415 * Checks if the calling app is running as a guest user. 416 * @return whether the caller is a guest user. 417 * @hide 418 */ 419 public boolean isGuestUser() { 420 UserInfo user = getUserInfo(UserHandle.myUserId()); 421 return user != null ? user.isGuest() : false; 422 } 423 424 /** 425 * Checks if the calling app is running in a managed profile. 426 * Requires {@link android.Manifest.permission#MANAGE_USERS} permission. 427 * 428 * @return whether the caller is in a managed profile. 429 * @hide 430 */ 431 @SystemApi 432 public boolean isManagedProfile() { 433 UserInfo user = getUserInfo(UserHandle.myUserId()); 434 return user != null ? user.isManagedProfile() : false; 435 } 436 437 /** 438 * Return whether the given user is actively running. This means that 439 * the user is in the "started" state, not "stopped" -- it is currently 440 * allowed to run code through scheduled alarms, receiving broadcasts, 441 * etc. A started user may be either the current foreground user or a 442 * background user; the result here does not distinguish between the two. 443 * @param user The user to retrieve the running state for. 444 */ 445 public boolean isUserRunning(UserHandle user) { 446 try { 447 return ActivityManagerNative.getDefault().isUserRunning( 448 user.getIdentifier(), false); 449 } catch (RemoteException e) { 450 return false; 451 } 452 } 453 454 /** 455 * Return whether the given user is actively running <em>or</em> stopping. 456 * This is like {@link #isUserRunning(UserHandle)}, but will also return 457 * true if the user had been running but is in the process of being stopped 458 * (but is not yet fully stopped, and still running some code). 459 * @param user The user to retrieve the running state for. 460 */ 461 public boolean isUserRunningOrStopping(UserHandle user) { 462 try { 463 return ActivityManagerNative.getDefault().isUserRunning( 464 user.getIdentifier(), true); 465 } catch (RemoteException e) { 466 return false; 467 } 468 } 469 470 /** 471 * Returns the UserInfo object describing a specific user. 472 * Requires {@link android.Manifest.permission#MANAGE_USERS} permission. 473 * @param userHandle the user handle of the user whose information is being requested. 474 * @return the UserInfo object for a specific user. 475 * @hide 476 */ 477 public UserInfo getUserInfo(int userHandle) { 478 try { 479 return mService.getUserInfo(userHandle); 480 } catch (RemoteException re) { 481 Log.w(TAG, "Could not get user info", re); 482 return null; 483 } 484 } 485 486 /** 487 * Returns the user-wide restrictions imposed on this user. 488 * @return a Bundle containing all the restrictions. 489 */ 490 public Bundle getUserRestrictions() { 491 return getUserRestrictions(Process.myUserHandle()); 492 } 493 494 /** 495 * Returns the user-wide restrictions imposed on the user specified by <code>userHandle</code>. 496 * @param userHandle the UserHandle of the user for whom to retrieve the restrictions. 497 * @return a Bundle containing all the restrictions. 498 */ 499 public Bundle getUserRestrictions(UserHandle userHandle) { 500 try { 501 return mService.getUserRestrictions(userHandle.getIdentifier()); 502 } catch (RemoteException re) { 503 Log.w(TAG, "Could not get user restrictions", re); 504 return Bundle.EMPTY; 505 } 506 } 507 508 /** 509 * Sets all the user-wide restrictions for this user. 510 * Requires the MANAGE_USERS permission. 511 * @param restrictions the Bundle containing all the restrictions. 512 * @deprecated use {@link android.app.admin.DevicePolicyManager#addUserRestriction( 513 * android.content.ComponentName, String)} or 514 * {@link android.app.admin.DevicePolicyManager#clearUserRestriction( 515 * android.content.ComponentName, String)} instead. 516 */ 517 @Deprecated 518 public void setUserRestrictions(Bundle restrictions) { 519 setUserRestrictions(restrictions, Process.myUserHandle()); 520 } 521 522 /** 523 * Sets all the user-wide restrictions for the specified user. 524 * Requires the MANAGE_USERS permission. 525 * @param restrictions the Bundle containing all the restrictions. 526 * @param userHandle the UserHandle of the user for whom to set the restrictions. 527 * @deprecated use {@link android.app.admin.DevicePolicyManager#addUserRestriction( 528 * android.content.ComponentName, String)} or 529 * {@link android.app.admin.DevicePolicyManager#clearUserRestriction( 530 * android.content.ComponentName, String)} instead. 531 */ 532 @Deprecated 533 public void setUserRestrictions(Bundle restrictions, UserHandle userHandle) { 534 try { 535 mService.setUserRestrictions(restrictions, userHandle.getIdentifier()); 536 } catch (RemoteException re) { 537 Log.w(TAG, "Could not set user restrictions", re); 538 } 539 } 540 541 /** 542 * Sets the value of a specific restriction. 543 * Requires the MANAGE_USERS permission. 544 * @param key the key of the restriction 545 * @param value the value for the restriction 546 * @deprecated use {@link android.app.admin.DevicePolicyManager#addUserRestriction( 547 * android.content.ComponentName, String)} or 548 * {@link android.app.admin.DevicePolicyManager#clearUserRestriction( 549 * android.content.ComponentName, String)} instead. 550 */ 551 @Deprecated 552 public void setUserRestriction(String key, boolean value) { 553 Bundle bundle = getUserRestrictions(); 554 bundle.putBoolean(key, value); 555 setUserRestrictions(bundle); 556 } 557 558 /** 559 * @hide 560 * Sets the value of a specific restriction on a specific user. 561 * Requires the MANAGE_USERS permission. 562 * @param key the key of the restriction 563 * @param value the value for the restriction 564 * @param userHandle the user whose restriction is to be changed. 565 * @deprecated use {@link android.app.admin.DevicePolicyManager#addUserRestriction( 566 * android.content.ComponentName, String)} or 567 * {@link android.app.admin.DevicePolicyManager#clearUserRestriction( 568 * android.content.ComponentName, String)} instead. 569 */ 570 @Deprecated 571 public void setUserRestriction(String key, boolean value, UserHandle userHandle) { 572 Bundle bundle = getUserRestrictions(userHandle); 573 bundle.putBoolean(key, value); 574 setUserRestrictions(bundle, userHandle); 575 } 576 577 /** 578 * Returns whether the current user has been disallowed from performing certain actions 579 * or setting certain settings. 580 * 581 * @param restrictionKey The string key representing the restriction. 582 * @return {@code true} if the current user has the given restriction, {@code false} otherwise. 583 */ 584 public boolean hasUserRestriction(String restrictionKey) { 585 return hasUserRestriction(restrictionKey, Process.myUserHandle()); 586 } 587 588 /** 589 * @hide 590 * Returns whether the given user has been disallowed from performing certain actions 591 * or setting certain settings. 592 * @param restrictionKey the string key representing the restriction 593 * @param userHandle the UserHandle of the user for whom to retrieve the restrictions. 594 */ 595 public boolean hasUserRestriction(String restrictionKey, UserHandle userHandle) { 596 return getUserRestrictions(userHandle).getBoolean(restrictionKey, false); 597 } 598 599 /** 600 * Return the serial number for a user. This is a device-unique 601 * number assigned to that user; if the user is deleted and then a new 602 * user created, the new users will not be given the same serial number. 603 * @param user The user whose serial number is to be retrieved. 604 * @return The serial number of the given user; returns -1 if the 605 * given UserHandle does not exist. 606 * @see #getUserForSerialNumber(long) 607 */ 608 public long getSerialNumberForUser(UserHandle user) { 609 return getUserSerialNumber(user.getIdentifier()); 610 } 611 612 /** 613 * Return the user associated with a serial number previously 614 * returned by {@link #getSerialNumberForUser(UserHandle)}. 615 * @param serialNumber The serial number of the user that is being 616 * retrieved. 617 * @return Return the user associated with the serial number, or null 618 * if there is not one. 619 * @see #getSerialNumberForUser(UserHandle) 620 */ 621 public UserHandle getUserForSerialNumber(long serialNumber) { 622 int ident = getUserHandle((int)serialNumber); 623 return ident >= 0 ? new UserHandle(ident) : null; 624 } 625 626 /** 627 * Creates a user with the specified name and options. 628 * Requires {@link android.Manifest.permission#MANAGE_USERS} permission. 629 * 630 * @param name the user's name 631 * @param flags flags that identify the type of user and other properties. 632 * @see UserInfo 633 * 634 * @return the UserInfo object for the created user, or null if the user could not be created. 635 * @hide 636 */ 637 public UserInfo createUser(String name, int flags) { 638 try { 639 return mService.createUser(name, flags); 640 } catch (RemoteException re) { 641 Log.w(TAG, "Could not create a user", re); 642 return null; 643 } 644 } 645 646 /** 647 * Creates a guest user and configures it. 648 * @param context an application context 649 * @param name the name to set for the user 650 * @hide 651 */ 652 public UserInfo createGuest(Context context, String name) { 653 UserInfo guest = createUser(name, UserInfo.FLAG_GUEST); 654 if (guest != null) { 655 Settings.Secure.putStringForUser(context.getContentResolver(), 656 Settings.Secure.SKIP_FIRST_USE_HINTS, "1", guest.id); 657 try { 658 Bundle guestRestrictions = mService.getDefaultGuestRestrictions(); 659 guestRestrictions.putBoolean(DISALLOW_SMS, true); 660 guestRestrictions.putBoolean(DISALLOW_INSTALL_UNKNOWN_SOURCES, true); 661 mService.setUserRestrictions(guestRestrictions, guest.id); 662 } catch (RemoteException re) { 663 Log.w(TAG, "Could not update guest restrictions"); 664 } 665 } 666 return guest; 667 } 668 669 /** 670 * Creates a user with the specified name and options as a profile of another user. 671 * Requires {@link android.Manifest.permission#MANAGE_USERS} permission. 672 * 673 * @param name the user's name 674 * @param flags flags that identify the type of user and other properties. 675 * @see UserInfo 676 * @param userHandle new user will be a profile of this use. 677 * 678 * @return the UserInfo object for the created user, or null if the user could not be created. 679 * @hide 680 */ 681 public UserInfo createProfileForUser(String name, int flags, int userHandle) { 682 try { 683 return mService.createProfileForUser(name, flags, userHandle); 684 } catch (RemoteException re) { 685 Log.w(TAG, "Could not create a user", re); 686 return null; 687 } 688 } 689 690 /** 691 * @hide 692 * Marks the guest user for deletion to allow a new guest to be created before deleting 693 * the current user who is a guest. 694 * @param userHandle 695 * @return 696 */ 697 public boolean markGuestForDeletion(int userHandle) { 698 try { 699 return mService.markGuestForDeletion(userHandle); 700 } catch (RemoteException re) { 701 Log.w(TAG, "Could not mark guest for deletion", re); 702 return false; 703 } 704 } 705 706 /** 707 * Sets the user as enabled, if such an user exists. 708 * Requires {@link android.Manifest.permission#MANAGE_USERS} permission. 709 * Note that the default is true, it's only that managed profiles might not be enabled. 710 * 711 * @param userHandle the id of the profile to enable 712 * @hide 713 */ 714 public void setUserEnabled(int userHandle) { 715 try { 716 mService.setUserEnabled(userHandle); 717 } catch (RemoteException e) { 718 Log.w(TAG, "Could not enable the profile", e); 719 } 720 } 721 722 /** 723 * Return the number of users currently created on the device. 724 */ 725 public int getUserCount() { 726 List<UserInfo> users = getUsers(); 727 return users != null ? users.size() : 1; 728 } 729 730 /** 731 * Returns information for all users on this device. 732 * Requires {@link android.Manifest.permission#MANAGE_USERS} permission. 733 * @return the list of users that were created. 734 * @hide 735 */ 736 public List<UserInfo> getUsers() { 737 try { 738 return mService.getUsers(false); 739 } catch (RemoteException re) { 740 Log.w(TAG, "Could not get user list", re); 741 return null; 742 } 743 } 744 745 /** 746 * Checks whether it's possible to add more users. Caller must hold the MANAGE_USERS 747 * permission. 748 * 749 * @return true if more users can be added, false if limit has been reached. 750 * @hide 751 */ 752 public boolean canAddMoreUsers() { 753 final List<UserInfo> users = getUsers(true); 754 final int totalUserCount = users.size(); 755 int aliveUserCount = 0; 756 for (int i = 0; i < totalUserCount; i++) { 757 UserInfo user = users.get(i); 758 if (!user.isGuest()) { 759 aliveUserCount++; 760 } 761 } 762 return aliveUserCount < getMaxSupportedUsers(); 763 } 764 765 /** 766 * Returns list of the profiles of userHandle including 767 * userHandle itself. 768 * Note that this returns both enabled and not enabled profiles. See 769 * {@link #getUserProfiles()} if you need only the enabled ones. 770 * 771 * Requires {@link android.Manifest.permission#MANAGE_USERS} permission. 772 * @param userHandle profiles of this user will be returned. 773 * @return the list of profiles. 774 * @hide 775 */ 776 public List<UserInfo> getProfiles(int userHandle) { 777 try { 778 return mService.getProfiles(userHandle, false /* enabledOnly */); 779 } catch (RemoteException re) { 780 Log.w(TAG, "Could not get user list", re); 781 return null; 782 } 783 } 784 785 /** 786 * Returns a list of UserHandles for profiles associated with the user that the calling process 787 * is running on, including the user itself. 788 * 789 * @return A non-empty list of UserHandles associated with the calling user. 790 */ 791 public List<UserHandle> getUserProfiles() { 792 ArrayList<UserHandle> profiles = new ArrayList<UserHandle>(); 793 List<UserInfo> users = new ArrayList<UserInfo>(); 794 try { 795 users = mService.getProfiles(UserHandle.myUserId(), true /* enabledOnly */); 796 } catch (RemoteException re) { 797 Log.w(TAG, "Could not get user list", re); 798 return null; 799 } 800 for (UserInfo info : users) { 801 UserHandle userHandle = new UserHandle(info.id); 802 profiles.add(userHandle); 803 } 804 return profiles; 805 } 806 807 /** 808 * Returns the parent of the profile which this method is called from 809 * or null if called from a user that is not a profile. 810 * 811 * @hide 812 */ 813 public UserInfo getProfileParent(int userHandle) { 814 try { 815 return mService.getProfileParent(userHandle); 816 } catch (RemoteException re) { 817 Log.w(TAG, "Could not get profile parent", re); 818 return null; 819 } 820 } 821 822 /** 823 * If the target user is a managed profile of the calling user or the caller 824 * is itself a managed profile, then this returns a badged copy of the given 825 * icon to be able to distinguish it from the original icon. For badging an 826 * arbitrary drawable use {@link #getBadgedDrawableForUser( 827 * android.graphics.drawable.Drawable, UserHandle, android.graphics.Rect, int)}. 828 * <p> 829 * If the original drawable is a BitmapDrawable and the backing bitmap is 830 * mutable as per {@link android.graphics.Bitmap#isMutable()}, the bading 831 * is performed in place and the original drawable is returned. 832 * </p> 833 * 834 * @param icon The icon to badge. 835 * @param user The target user. 836 * @return A drawable that combines the original icon and a badge as 837 * determined by the system. 838 * @removed 839 */ 840 public Drawable getBadgedIconForUser(Drawable icon, UserHandle user) { 841 return mContext.getPackageManager().getUserBadgedIcon(icon, user); 842 } 843 844 /** 845 * If the target user is a managed profile of the calling user or the caller 846 * is itself a managed profile, then this returns a badged copy of the given 847 * drawable allowing the user to distinguish it from the original drawable. 848 * The caller can specify the location in the bounds of the drawable to be 849 * badged where the badge should be applied as well as the density of the 850 * badge to be used. 851 * <p> 852 * If the original drawable is a BitmapDrawable and the backing bitmap is 853 * mutable as per {@link android.graphics.Bitmap#isMutable()}, the bading 854 * is performed in place and the original drawable is returned. 855 * </p> 856 * 857 * @param badgedDrawable The drawable to badge. 858 * @param user The target user. 859 * @param badgeLocation Where in the bounds of the badged drawable to place 860 * the badge. If not provided, the badge is applied on top of the entire 861 * drawable being badged. 862 * @param badgeDensity The optional desired density for the badge as per 863 * {@link android.util.DisplayMetrics#densityDpi}. If not provided, 864 * the density of the display is used. 865 * @return A drawable that combines the original drawable and a badge as 866 * determined by the system. 867 * @removed 868 */ 869 public Drawable getBadgedDrawableForUser(Drawable badgedDrawable, UserHandle user, 870 Rect badgeLocation, int badgeDensity) { 871 return mContext.getPackageManager().getUserBadgedDrawableForDensity(badgedDrawable, user, 872 badgeLocation, badgeDensity); 873 } 874 875 /** 876 * If the target user is a managed profile of the calling user or the caller 877 * is itself a managed profile, then this returns a copy of the label with 878 * badging for accessibility services like talkback. E.g. passing in "Email" 879 * and it might return "Work Email" for Email in the work profile. 880 * 881 * @param label The label to change. 882 * @param user The target user. 883 * @return A label that combines the original label and a badge as 884 * determined by the system. 885 * @removed 886 */ 887 public CharSequence getBadgedLabelForUser(CharSequence label, UserHandle user) { 888 return mContext.getPackageManager().getUserBadgedLabel(label, user); 889 } 890 891 /** 892 * Returns information for all users on this device. Requires 893 * {@link android.Manifest.permission#MANAGE_USERS} permission. 894 * 895 * @param excludeDying specify if the list should exclude users being 896 * removed. 897 * @return the list of users that were created. 898 * @hide 899 */ 900 public List<UserInfo> getUsers(boolean excludeDying) { 901 try { 902 return mService.getUsers(excludeDying); 903 } catch (RemoteException re) { 904 Log.w(TAG, "Could not get user list", re); 905 return null; 906 } 907 } 908 909 /** 910 * Removes a user and all associated data. 911 * Requires {@link android.Manifest.permission#MANAGE_USERS} permission. 912 * @param userHandle the integer handle of the user, where 0 is the primary user. 913 * @hide 914 */ 915 public boolean removeUser(int userHandle) { 916 try { 917 return mService.removeUser(userHandle); 918 } catch (RemoteException re) { 919 Log.w(TAG, "Could not remove user ", re); 920 return false; 921 } 922 } 923 924 /** 925 * Updates the user's name. 926 * Requires {@link android.Manifest.permission#MANAGE_USERS} permission. 927 * 928 * @param userHandle the user's integer handle 929 * @param name the new name for the user 930 * @hide 931 */ 932 public void setUserName(int userHandle, String name) { 933 try { 934 mService.setUserName(userHandle, name); 935 } catch (RemoteException re) { 936 Log.w(TAG, "Could not set the user name ", re); 937 } 938 } 939 940 /** 941 * Sets the user's photo. 942 * @param userHandle the user for whom to change the photo. 943 * @param icon the bitmap to set as the photo. 944 * @hide 945 */ 946 public void setUserIcon(int userHandle, Bitmap icon) { 947 try { 948 mService.setUserIcon(userHandle, icon); 949 } catch (RemoteException re) { 950 Log.w(TAG, "Could not set the user icon ", re); 951 } 952 } 953 954 /** 955 * Returns a file descriptor for the user's photo. PNG data can be read from this file. 956 * @param userHandle the user whose photo we want to read. 957 * @return a {@link Bitmap} of the user's photo, or null if there's no photo. 958 * @hide 959 */ 960 public Bitmap getUserIcon(int userHandle) { 961 try { 962 return mService.getUserIcon(userHandle); 963 } catch (RemoteException re) { 964 Log.w(TAG, "Could not get the user icon ", re); 965 return null; 966 } 967 } 968 969 /** 970 * Returns the maximum number of users that can be created on this device. A return value 971 * of 1 means that it is a single user device. 972 * @hide 973 * @return a value greater than or equal to 1 974 */ 975 public static int getMaxSupportedUsers() { 976 // Don't allow multiple users on certain builds 977 if (android.os.Build.ID.startsWith("JVP")) return 1; 978 // Svelte devices don't get multi-user. 979 if (ActivityManager.isLowRamDeviceStatic()) return 1; 980 return SystemProperties.getInt("fw.max_users", 981 Resources.getSystem().getInteger(R.integer.config_multiuserMaximumUsers)); 982 } 983 984 /** 985 * Returns true if the user switcher should be shown, this will be if there 986 * are multiple users that aren't managed profiles. 987 * @hide 988 * @return true if user switcher should be shown. 989 */ 990 public boolean isUserSwitcherEnabled() { 991 List<UserInfo> users = getUsers(true); 992 if (users == null) { 993 return false; 994 } 995 int switchableUserCount = 0; 996 for (UserInfo user : users) { 997 if (user.supportsSwitchTo()) { 998 ++switchableUserCount; 999 } 1000 } 1001 final boolean guestEnabled = Settings.Global.getInt(mContext.getContentResolver(), 1002 Settings.Global.GUEST_USER_ENABLED, 0) == 1; 1003 return switchableUserCount > 1 || guestEnabled; 1004 } 1005 1006 /** 1007 * Returns a serial number on this device for a given userHandle. User handles can be recycled 1008 * when deleting and creating users, but serial numbers are not reused until the device is wiped. 1009 * @param userHandle 1010 * @return a serial number associated with that user, or -1 if the userHandle is not valid. 1011 * @hide 1012 */ 1013 public int getUserSerialNumber(int userHandle) { 1014 try { 1015 return mService.getUserSerialNumber(userHandle); 1016 } catch (RemoteException re) { 1017 Log.w(TAG, "Could not get serial number for user " + userHandle); 1018 } 1019 return -1; 1020 } 1021 1022 /** 1023 * Returns a userHandle on this device for a given user serial number. User handles can be 1024 * recycled when deleting and creating users, but serial numbers are not reused until the device 1025 * is wiped. 1026 * @param userSerialNumber 1027 * @return the userHandle associated with that user serial number, or -1 if the serial number 1028 * is not valid. 1029 * @hide 1030 */ 1031 public int getUserHandle(int userSerialNumber) { 1032 try { 1033 return mService.getUserHandle(userSerialNumber); 1034 } catch (RemoteException re) { 1035 Log.w(TAG, "Could not get userHandle for user " + userSerialNumber); 1036 } 1037 return -1; 1038 } 1039 1040 /** 1041 * Returns a Bundle containing any saved application restrictions for this user, for the 1042 * given package name. Only an application with this package name can call this method. 1043 * @param packageName the package name of the calling application 1044 * @return a Bundle with the restrictions as key/value pairs, or null if there are no 1045 * saved restrictions. The values can be of type Boolean, String or String[], depending 1046 * on the restriction type, as defined by the application. 1047 */ 1048 public Bundle getApplicationRestrictions(String packageName) { 1049 try { 1050 return mService.getApplicationRestrictions(packageName); 1051 } catch (RemoteException re) { 1052 Log.w(TAG, "Could not get application restrictions for package " + packageName); 1053 } 1054 return null; 1055 } 1056 1057 /** 1058 * @hide 1059 */ 1060 public Bundle getApplicationRestrictions(String packageName, UserHandle user) { 1061 try { 1062 return mService.getApplicationRestrictionsForUser(packageName, user.getIdentifier()); 1063 } catch (RemoteException re) { 1064 Log.w(TAG, "Could not get application restrictions for user " + user.getIdentifier()); 1065 } 1066 return null; 1067 } 1068 1069 /** 1070 * @hide 1071 */ 1072 public void setApplicationRestrictions(String packageName, Bundle restrictions, 1073 UserHandle user) { 1074 try { 1075 mService.setApplicationRestrictions(packageName, restrictions, user.getIdentifier()); 1076 } catch (RemoteException re) { 1077 Log.w(TAG, "Could not set application restrictions for user " + user.getIdentifier()); 1078 } 1079 } 1080 1081 /** 1082 * Sets a new challenge PIN for restrictions. This is only for use by pre-installed 1083 * apps and requires the MANAGE_USERS permission. 1084 * @param newPin the PIN to use for challenge dialogs. 1085 * @return Returns true if the challenge PIN was set successfully. 1086 */ 1087 public boolean setRestrictionsChallenge(String newPin) { 1088 try { 1089 return mService.setRestrictionsChallenge(newPin); 1090 } catch (RemoteException re) { 1091 Log.w(TAG, "Could not change restrictions pin"); 1092 } 1093 return false; 1094 } 1095 1096 /** 1097 * @hide 1098 * @param pin The PIN to verify, or null to get the number of milliseconds to wait for before 1099 * allowing the user to enter the PIN. 1100 * @return Returns a positive number (including zero) for how many milliseconds before 1101 * you can accept another PIN, when the input is null or the input doesn't match the saved PIN. 1102 * Returns {@link #PIN_VERIFICATION_SUCCESS} if the input matches the saved PIN. Returns 1103 * {@link #PIN_VERIFICATION_FAILED_NOT_SET} if there is no PIN set. 1104 */ 1105 public int checkRestrictionsChallenge(String pin) { 1106 try { 1107 return mService.checkRestrictionsChallenge(pin); 1108 } catch (RemoteException re) { 1109 Log.w(TAG, "Could not check restrictions pin"); 1110 } 1111 return PIN_VERIFICATION_FAILED_INCORRECT; 1112 } 1113 1114 /** 1115 * @hide 1116 * Checks whether the user has restrictions that are PIN-protected. An application that 1117 * participates in restrictions can check if the owner has requested a PIN challenge for 1118 * any restricted operations. If there is a PIN in effect, the application should launch 1119 * the PIN challenge activity {@link android.content.Intent#ACTION_RESTRICTIONS_CHALLENGE}. 1120 * @see android.content.Intent#ACTION_RESTRICTIONS_CHALLENGE 1121 * @return whether a restrictions PIN is in effect. 1122 */ 1123 public boolean hasRestrictionsChallenge() { 1124 try { 1125 return mService.hasRestrictionsChallenge(); 1126 } catch (RemoteException re) { 1127 Log.w(TAG, "Could not change restrictions pin"); 1128 } 1129 return false; 1130 } 1131 1132 /** @hide */ 1133 public void removeRestrictions() { 1134 try { 1135 mService.removeRestrictions(); 1136 } catch (RemoteException re) { 1137 Log.w(TAG, "Could not change restrictions pin"); 1138 } 1139 } 1140 1141 /** 1142 * @hide 1143 * Set restrictions that should apply to any future guest user that's created. 1144 */ 1145 public void setDefaultGuestRestrictions(Bundle restrictions) { 1146 try { 1147 mService.setDefaultGuestRestrictions(restrictions); 1148 } catch (RemoteException re) { 1149 Log.w(TAG, "Could not set guest restrictions"); 1150 } 1151 } 1152 1153 /** 1154 * @hide 1155 * Gets the default guest restrictions. 1156 */ 1157 public Bundle getDefaultGuestRestrictions() { 1158 try { 1159 return mService.getDefaultGuestRestrictions(); 1160 } catch (RemoteException re) { 1161 Log.w(TAG, "Could not set guest restrictions"); 1162 } 1163 return new Bundle(); 1164 } 1165} 1166