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