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