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