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