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