UserManager.java revision 92d6aea86e5fbe59b6dd1dcc157118b1c57d7a57
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 95 public static final String DISALLOW_SHARE_LOCATION = "no_share_location"; 96 97 /** 98 * Key for user restrictions. Specifies if a user is disallowed from enabling the 99 * "Unknown Sources" setting, that allows installation of apps from unknown sources. 100 * The default value is <code>false</code>. 101 * <p/> 102 * Type: Boolean 103 * @see #setUserRestrictions(Bundle) 104 * @see #getUserRestrictions() 105 */ 106 public static final String DISALLOW_INSTALL_UNKNOWN_SOURCES = "no_install_unknown_sources"; 107 108 /** 109 * Key for user restrictions. Specifies if a user is disallowed from configuring bluetooth. 110 * The default value is <code>false</code>. 111 * <p/> 112 * Type: Boolean 113 * @see #setUserRestrictions(Bundle) 114 * @see #getUserRestrictions() 115 */ 116 public static final String DISALLOW_CONFIG_BLUETOOTH = "no_config_bluetooth"; 117 118 /** 119 * Key for user restrictions. Specifies if a user is disallowed from transferring files over 120 * USB. The default value is <code>false</code>. 121 * <p/> 122 * Type: Boolean 123 * @see #setUserRestrictions(Bundle) 124 * @see #getUserRestrictions() 125 */ 126 public static final String DISALLOW_USB_FILE_TRANSFER = "no_usb_file_transfer"; 127 128 /** 129 * Key for user restrictions. Specifies if a user is disallowed from configuring user 130 * credentials. The default value is <code>false</code>. 131 * <p/> 132 * Type: Boolean 133 * @see #setUserRestrictions(Bundle) 134 * @see #getUserRestrictions() 135 */ 136 public static final String DISALLOW_CONFIG_CREDENTIALS = "no_config_credentials"; 137 138 /** 139 * Key for user restrictions. Specifies if a user is disallowed from removing users. 140 * The default value is <code>false</code>. 141 * <p/> 142 * Type: Boolean 143 * @see #setUserRestrictions(Bundle) 144 * @see #getUserRestrictions() 145 */ 146 public static final String DISALLOW_REMOVE_USER = "no_remove_user"; 147 148 /** @hide */ 149 public static final int PIN_VERIFICATION_FAILED_INCORRECT = -3; 150 /** @hide */ 151 public static final int PIN_VERIFICATION_FAILED_NOT_SET = -2; 152 /** @hide */ 153 public static final int PIN_VERIFICATION_SUCCESS = -1; 154 155 private static UserManager sInstance = null; 156 157 /** @hide */ 158 public synchronized static UserManager get(Context context) { 159 if (sInstance == null) { 160 sInstance = (UserManager) context.getSystemService(Context.USER_SERVICE); 161 } 162 return sInstance; 163 } 164 165 /** @hide */ 166 public UserManager(Context context, IUserManager service) { 167 mService = service; 168 mContext = context; 169 } 170 171 /** 172 * Returns whether the system supports multiple users. 173 * @return true if multiple users can be created by user, false if it is a single user device. 174 * @hide 175 */ 176 public static boolean supportsMultipleUsers() { 177 return getMaxSupportedUsers() > 1 178 && SystemProperties.getBoolean("fw.show_multiuserui", 179 Resources.getSystem().getBoolean(R.bool.config_enableMultiUserUI)); 180 } 181 182 /** 183 * Returns the user handle for the user that this application is running for. 184 * @return the user handle of the user making this call. 185 * @hide 186 */ 187 public int getUserHandle() { 188 return UserHandle.myUserId(); 189 } 190 191 /** 192 * Returns the user name of the user making this call. This call is only 193 * available to applications on the system image; it requires the 194 * MANAGE_USERS permission. 195 * @return the user name 196 */ 197 public String getUserName() { 198 try { 199 return mService.getUserInfo(getUserHandle()).name; 200 } catch (RemoteException re) { 201 Log.w(TAG, "Could not get user name", re); 202 return ""; 203 } 204 } 205 206 /** 207 * Used to determine whether the user making this call is subject to 208 * teleportations. 209 * @return whether the user making this call is a goat 210 */ 211 public boolean isUserAGoat() { 212 return false; 213 } 214 215 /** 216 * Used to check if the user making this call is linked to another user. Linked users may have 217 * a reduced number of available apps, app restrictions and account restrictions. 218 * @return whether the user making this call is a linked user 219 * @hide 220 */ 221 public boolean isLinkedUser() { 222 try { 223 return mService.isRestricted(); 224 } catch (RemoteException re) { 225 Log.w(TAG, "Could not check if user is limited ", re); 226 return false; 227 } 228 } 229 230 /** 231 * Return whether the given user is actively running. This means that 232 * the user is in the "started" state, not "stopped" -- it is currently 233 * allowed to run code through scheduled alarms, receiving broadcasts, 234 * etc. A started user may be either the current foreground user or a 235 * background user; the result here does not distinguish between the two. 236 * @param user The user to retrieve the running state for. 237 */ 238 public boolean isUserRunning(UserHandle user) { 239 try { 240 return ActivityManagerNative.getDefault().isUserRunning( 241 user.getIdentifier(), false); 242 } catch (RemoteException e) { 243 return false; 244 } 245 } 246 247 /** 248 * Return whether the given user is actively running <em>or</em> stopping. 249 * This is like {@link #isUserRunning(UserHandle)}, but will also return 250 * true if the user had been running but is in the process of being stopped 251 * (but is not yet fully stopped, and still running some code). 252 * @param user The user to retrieve the running state for. 253 */ 254 public boolean isUserRunningOrStopping(UserHandle user) { 255 try { 256 return ActivityManagerNative.getDefault().isUserRunning( 257 user.getIdentifier(), true); 258 } catch (RemoteException e) { 259 return false; 260 } 261 } 262 263 /** 264 * Returns the UserInfo object describing a specific user. 265 * Requires {@link android.Manifest.permission#MANAGE_USERS} permission. 266 * @param userHandle the user handle of the user whose information is being requested. 267 * @return the UserInfo object for a specific user. 268 * @hide 269 */ 270 public UserInfo getUserInfo(int userHandle) { 271 try { 272 return mService.getUserInfo(userHandle); 273 } catch (RemoteException re) { 274 Log.w(TAG, "Could not get user info", re); 275 return null; 276 } 277 } 278 279 /** 280 * Returns the user-wide restrictions imposed on this user. 281 * @return a Bundle containing all the restrictions. 282 */ 283 public Bundle getUserRestrictions() { 284 return getUserRestrictions(Process.myUserHandle()); 285 } 286 287 /** 288 * Returns the user-wide restrictions imposed on the user specified by <code>userHandle</code>. 289 * @param userHandle the UserHandle of the user for whom to retrieve the restrictions. 290 * @return a Bundle containing all the restrictions. 291 */ 292 public Bundle getUserRestrictions(UserHandle userHandle) { 293 try { 294 return mService.getUserRestrictions(userHandle.getIdentifier()); 295 } catch (RemoteException re) { 296 Log.w(TAG, "Could not get user restrictions", re); 297 return Bundle.EMPTY; 298 } 299 } 300 301 /** 302 * Sets all the user-wide restrictions for this user. 303 * Requires the MANAGE_USERS permission or profile/device owner 304 * privileges. 305 * @param restrictions the Bundle containing all the restrictions. 306 */ 307 public void setUserRestrictions(Bundle restrictions) { 308 setUserRestrictions(restrictions, Process.myUserHandle()); 309 } 310 311 /** 312 * Sets all the user-wide restrictions for the specified user. 313 * Requires the MANAGE_USERS permission or profile/device owner 314 * privileges. 315 * @param restrictions the Bundle containing all the restrictions. 316 * @param userHandle the UserHandle of the user for whom to set the restrictions. 317 */ 318 public void setUserRestrictions(Bundle restrictions, UserHandle userHandle) { 319 try { 320 mService.setUserRestrictions(restrictions, userHandle.getIdentifier()); 321 } catch (RemoteException re) { 322 Log.w(TAG, "Could not set user restrictions", re); 323 } 324 } 325 326 /** 327 * Sets the value of a specific restriction. 328 * Requires the MANAGE_USERS permission or profile/device owner 329 * privileges. 330 * @param key the key of the restriction 331 * @param value the value for the restriction 332 */ 333 public void setUserRestriction(String key, boolean value) { 334 Bundle bundle = getUserRestrictions(); 335 bundle.putBoolean(key, value); 336 setUserRestrictions(bundle); 337 } 338 339 /** 340 * @hide 341 * Sets the value of a specific restriction on a specific user. 342 * Requires the {@link android.Manifest.permission#MANAGE_USERS} permission or profile/device owner 343 * privileges. 344 * @param key the key of the restriction 345 * @param value the value for the restriction 346 * @param userHandle the user whose restriction is to be changed. 347 */ 348 public void setUserRestriction(String key, boolean value, UserHandle userHandle) { 349 Bundle bundle = getUserRestrictions(userHandle); 350 bundle.putBoolean(key, value); 351 setUserRestrictions(bundle, userHandle); 352 } 353 354 /** 355 * @hide 356 * Returns whether the current user has been disallowed from performing certain actions 357 * or setting certain settings. 358 * @param restrictionKey the string key representing the restriction 359 */ 360 public boolean hasUserRestriction(String restrictionKey) { 361 return hasUserRestriction(restrictionKey, Process.myUserHandle()); 362 } 363 364 /** 365 * @hide 366 * Returns whether the given user has been disallowed from performing certain actions 367 * or setting certain settings. 368 * @param restrictionKey the string key representing the restriction 369 * @param userHandle the UserHandle of the user for whom to retrieve the restrictions. 370 */ 371 public boolean hasUserRestriction(String restrictionKey, UserHandle userHandle) { 372 return getUserRestrictions(userHandle).getBoolean(restrictionKey, false); 373 } 374 375 /** 376 * Return the serial number for a user. This is a device-unique 377 * number assigned to that user; if the user is deleted and then a new 378 * user created, the new users will not be given the same serial number. 379 * @param user The user whose serial number is to be retrieved. 380 * @return The serial number of the given user; returns -1 if the 381 * given UserHandle does not exist. 382 * @see #getUserForSerialNumber(long) 383 */ 384 public long getSerialNumberForUser(UserHandle user) { 385 return getUserSerialNumber(user.getIdentifier()); 386 } 387 388 /** 389 * Return the user associated with a serial number previously 390 * returned by {@link #getSerialNumberForUser(UserHandle)}. 391 * @param serialNumber The serial number of the user that is being 392 * retrieved. 393 * @return Return the user associated with the serial number, or null 394 * if there is not one. 395 * @see #getSerialNumberForUser(UserHandle) 396 */ 397 public UserHandle getUserForSerialNumber(long serialNumber) { 398 int ident = getUserHandle((int)serialNumber); 399 return ident >= 0 ? new UserHandle(ident) : null; 400 } 401 402 /** 403 * Creates a user with the specified name and options. 404 * Requires {@link android.Manifest.permission#MANAGE_USERS} permission. 405 * 406 * @param name the user's name 407 * @param flags flags that identify the type of user and other properties. 408 * @see UserInfo 409 * 410 * @return the UserInfo object for the created user, or null if the user could not be created. 411 * @hide 412 */ 413 public UserInfo createUser(String name, int flags) { 414 try { 415 return mService.createUser(name, flags); 416 } catch (RemoteException re) { 417 Log.w(TAG, "Could not create a user", re); 418 return null; 419 } 420 } 421 422 /** 423 * Creates a user with the specified name and options as a profile of another user. 424 * Requires {@link android.Manifest.permission#MANAGE_USERS} permission. 425 * 426 * @param name the user's name 427 * @param flags flags that identify the type of user and other properties. 428 * @see UserInfo 429 * @param userHandle new user will be a profile of this use. 430 * 431 * @return the UserInfo object for the created user, or null if the user could not be created. 432 * @hide 433 */ 434 public UserInfo createProfileForUser(String name, int flags, int userHandle) { 435 try { 436 return mService.createProfileForUser(name, flags, userHandle); 437 } catch (RemoteException re) { 438 Log.w(TAG, "Could not create a user", re); 439 return null; 440 } 441 } 442 443 /** 444 * Return the number of users currently created on the device. 445 */ 446 public int getUserCount() { 447 List<UserInfo> users = getUsers(); 448 return users != null ? users.size() : 1; 449 } 450 451 /** 452 * Returns information for all users on this device. 453 * Requires {@link android.Manifest.permission#MANAGE_USERS} permission. 454 * @return the list of users that were created. 455 * @hide 456 */ 457 public List<UserInfo> getUsers() { 458 try { 459 return mService.getUsers(false); 460 } catch (RemoteException re) { 461 Log.w(TAG, "Could not get user list", re); 462 return null; 463 } 464 } 465 466 /** 467 * Returns list of the profiles of userHandle including 468 * userHandle itself. 469 * Note that it this returns both enabled and not enabled profiles. See 470 * {@link #getUserProfiles()} if you need only the enabled ones. 471 * 472 * Requires {@link android.Manifest.permission#MANAGE_USERS} permission. 473 * @param userHandle profiles of this user will be returned. 474 * @return the list of profiles. 475 * @hide 476 */ 477 public List<UserInfo> getProfiles(int userHandle) { 478 try { 479 return mService.getProfiles(userHandle, false /* enabledOnly */); 480 } catch (RemoteException re) { 481 Log.w(TAG, "Could not get user list", re); 482 return null; 483 } 484 } 485 486 /** 487 * Returns a list of UserHandles for profiles associated with this user, including this user. 488 * 489 * @return A non-empty list of UserHandles associated with the calling user. 490 */ 491 public List<UserHandle> getUserProfiles() { 492 ArrayList<UserHandle> profiles = new ArrayList<UserHandle>(); 493 List<UserInfo> users = new ArrayList<UserInfo>(); 494 try { 495 // TODO: Switch enabledOnly to true once client apps are updated 496 users = mService.getProfiles(UserHandle.myUserId(), false /* enabledOnly */); 497 } catch (RemoteException re) { 498 Log.w(TAG, "Could not get user list", re); 499 return null; 500 } 501 for (UserInfo info : users) { 502 UserHandle userHandle = new UserHandle(info.id); 503 profiles.add(userHandle); 504 } 505 return profiles; 506 } 507 508 /** 509 * If the target user is a managed profile of the calling user or the caller 510 * is itself a managed profile, then this returns a badged copy of the given 511 * icon to be able to distinguish it from the original icon. 512 * <P> 513 * If the original drawable is not a BitmapDrawable, then the original 514 * drawable is returned. 515 * </P> 516 * 517 * @param icon The icon to badge. 518 * @param user The target user. 519 * @return A drawable that combines the original icon and a badge as 520 * determined by the system. 521 */ 522 public Drawable getBadgedDrawableForUser(Drawable icon, UserHandle user) { 523 int badgeResId = getBadgeResIdForUser(user.getIdentifier()); 524 if (badgeResId == 0) { 525 return icon; 526 } else { 527 Drawable badgeIcon = mContext.getPackageManager() 528 .getDrawable("system", badgeResId, null); 529 return getMergedDrawable(icon, badgeIcon); 530 } 531 } 532 533 private int getBadgeResIdForUser(int userHandle) { 534 // Return the framework-provided badge. 535 List<UserInfo> userProfiles = getProfiles(UserHandle.myUserId()); 536 for (UserInfo user : userProfiles) { 537 if (user.id == userHandle 538 && user.isManagedProfile()) { 539 return com.android.internal.R.drawable.ic_corp_badge; 540 } 541 } 542 return 0; 543 } 544 545 private Drawable getMergedDrawable(Drawable icon, Drawable badge) { 546 final int width = icon.getIntrinsicWidth(); 547 final int height = icon.getIntrinsicHeight(); 548 Bitmap bitmap = Bitmap.createBitmap(width, height, Config.ARGB_8888); 549 Canvas canvas = new Canvas(bitmap); 550 icon.setBounds(0, 0, width, height); 551 icon.draw(canvas); 552 badge.setBounds(0, 0, width, height); 553 badge.draw(canvas); 554 BitmapDrawable merged = new BitmapDrawable(bitmap); 555 if (icon instanceof BitmapDrawable) { 556 merged.setTargetDensity(((BitmapDrawable) icon).getBitmap().getDensity()); 557 } 558 return merged; 559 } 560 561 /** 562 * Returns information for all users on this device. Requires 563 * {@link android.Manifest.permission#MANAGE_USERS} permission. 564 * 565 * @param excludeDying specify if the list should exclude users being 566 * removed. 567 * @return the list of users that were created. 568 * @hide 569 */ 570 public List<UserInfo> getUsers(boolean excludeDying) { 571 try { 572 return mService.getUsers(excludeDying); 573 } catch (RemoteException re) { 574 Log.w(TAG, "Could not get user list", re); 575 return null; 576 } 577 } 578 579 /** 580 * Removes a user and all associated data. 581 * Requires {@link android.Manifest.permission#MANAGE_USERS} permission. 582 * @param userHandle the integer handle of the user, where 0 is the primary user. 583 * @hide 584 */ 585 public boolean removeUser(int userHandle) { 586 try { 587 return mService.removeUser(userHandle); 588 } catch (RemoteException re) { 589 Log.w(TAG, "Could not remove user ", re); 590 return false; 591 } 592 } 593 594 /** 595 * Updates the user's name. 596 * Requires {@link android.Manifest.permission#MANAGE_USERS} permission. 597 * 598 * @param userHandle the user's integer handle 599 * @param name the new name for the user 600 * @hide 601 */ 602 public void setUserName(int userHandle, String name) { 603 try { 604 mService.setUserName(userHandle, name); 605 } catch (RemoteException re) { 606 Log.w(TAG, "Could not set the user name ", re); 607 } 608 } 609 610 /** 611 * Sets the user's photo. 612 * @param userHandle the user for whom to change the photo. 613 * @param icon the bitmap to set as the photo. 614 * @hide 615 */ 616 public void setUserIcon(int userHandle, Bitmap icon) { 617 try { 618 mService.setUserIcon(userHandle, icon); 619 } catch (RemoteException re) { 620 Log.w(TAG, "Could not set the user icon ", re); 621 } 622 } 623 624 /** 625 * Returns a file descriptor for the user's photo. PNG data can be read from this file. 626 * @param userHandle the user whose photo we want to read. 627 * @return a {@link Bitmap} of the user's photo, or null if there's no photo. 628 * @hide 629 */ 630 public Bitmap getUserIcon(int userHandle) { 631 try { 632 return mService.getUserIcon(userHandle); 633 } catch (RemoteException re) { 634 Log.w(TAG, "Could not get the user icon ", re); 635 return null; 636 } 637 } 638 639 /** 640 * Enable or disable the use of a guest account. If disabled, the existing guest account 641 * will be wiped. 642 * Requires {@link android.Manifest.permission#MANAGE_USERS} permission. 643 * @param enable whether to enable a guest account. 644 * @hide 645 */ 646 public void setGuestEnabled(boolean enable) { 647 try { 648 mService.setGuestEnabled(enable); 649 } catch (RemoteException re) { 650 Log.w(TAG, "Could not change guest account availability to " + enable); 651 } 652 } 653 654 /** 655 * Checks if a guest user is enabled for this device. 656 * Requires {@link android.Manifest.permission#MANAGE_USERS} permission. 657 * @return whether a guest user is enabled 658 * @hide 659 */ 660 public boolean isGuestEnabled() { 661 try { 662 return mService.isGuestEnabled(); 663 } catch (RemoteException re) { 664 Log.w(TAG, "Could not retrieve guest enabled state"); 665 return false; 666 } 667 } 668 669 /** 670 * Wipes all the data for a user, but doesn't remove the user. 671 * Requires {@link android.Manifest.permission#MANAGE_USERS} permission. 672 * @param userHandle 673 * @hide 674 */ 675 public void wipeUser(int userHandle) { 676 try { 677 mService.wipeUser(userHandle); 678 } catch (RemoteException re) { 679 Log.w(TAG, "Could not wipe user " + userHandle); 680 } 681 } 682 683 /** 684 * Returns the maximum number of users that can be created on this device. A return value 685 * of 1 means that it is a single user device. 686 * @hide 687 * @return a value greater than or equal to 1 688 */ 689 public static int getMaxSupportedUsers() { 690 // Don't allow multiple users on certain builds 691 if (android.os.Build.ID.startsWith("JVP")) return 1; 692 return SystemProperties.getInt("fw.max_users", 693 Resources.getSystem().getInteger(R.integer.config_multiuserMaximumUsers)); 694 } 695 696 /** 697 * Returns true if the user switcher should be shown, this will be if there 698 * are multiple users that aren't managed profiles. 699 * @hide 700 * @return true if user switcher should be shown. 701 */ 702 public boolean isUserSwitcherEnabled() { 703 List<UserInfo> users = getUsers(true); 704 if (users == null) { 705 return false; 706 } 707 int switchableUserCount = 0; 708 for (UserInfo user : users) { 709 if (user.supportsSwitchTo()) { 710 ++switchableUserCount; 711 } 712 } 713 return switchableUserCount > 1; 714 } 715 716 /** 717 * Returns a serial number on this device for a given userHandle. User handles can be recycled 718 * when deleting and creating users, but serial numbers are not reused until the device is wiped. 719 * @param userHandle 720 * @return a serial number associated with that user, or -1 if the userHandle is not valid. 721 * @hide 722 */ 723 public int getUserSerialNumber(int userHandle) { 724 try { 725 return mService.getUserSerialNumber(userHandle); 726 } catch (RemoteException re) { 727 Log.w(TAG, "Could not get serial number for user " + userHandle); 728 } 729 return -1; 730 } 731 732 /** 733 * Returns a userHandle on this device for a given user serial number. User handles can be 734 * recycled when deleting and creating users, but serial numbers are not reused until the device 735 * is wiped. 736 * @param userSerialNumber 737 * @return the userHandle associated with that user serial number, or -1 if the serial number 738 * is not valid. 739 * @hide 740 */ 741 public int getUserHandle(int userSerialNumber) { 742 try { 743 return mService.getUserHandle(userSerialNumber); 744 } catch (RemoteException re) { 745 Log.w(TAG, "Could not get userHandle for user " + userSerialNumber); 746 } 747 return -1; 748 } 749 750 /** 751 * Returns a Bundle containing any saved application restrictions for this user, for the 752 * given package name. Only an application with this package name can call this method. 753 * @param packageName the package name of the calling application 754 * @return a Bundle with the restrictions as key/value pairs, or null if there are no 755 * saved restrictions. The values can be of type Boolean, String or String[], depending 756 * on the restriction type, as defined by the application. 757 */ 758 public Bundle getApplicationRestrictions(String packageName) { 759 try { 760 return mService.getApplicationRestrictions(packageName); 761 } catch (RemoteException re) { 762 Log.w(TAG, "Could not get application restrictions for package " + packageName); 763 } 764 return null; 765 } 766 767 /** 768 * @hide 769 */ 770 public Bundle getApplicationRestrictions(String packageName, UserHandle user) { 771 try { 772 return mService.getApplicationRestrictionsForUser(packageName, user.getIdentifier()); 773 } catch (RemoteException re) { 774 Log.w(TAG, "Could not get application restrictions for user " + user.getIdentifier()); 775 } 776 return null; 777 } 778 779 /** 780 * @hide 781 */ 782 public void setApplicationRestrictions(String packageName, Bundle restrictions, 783 UserHandle user) { 784 try { 785 mService.setApplicationRestrictions(packageName, restrictions, user.getIdentifier()); 786 } catch (RemoteException re) { 787 Log.w(TAG, "Could not set application restrictions for user " + user.getIdentifier()); 788 } 789 } 790 791 /** 792 * Sets a new challenge PIN for restrictions. This is only for use by pre-installed 793 * apps and requires the MANAGE_USERS permission. 794 * @param newPin the PIN to use for challenge dialogs. 795 * @return Returns true if the challenge PIN was set successfully. 796 */ 797 public boolean setRestrictionsChallenge(String newPin) { 798 try { 799 return mService.setRestrictionsChallenge(newPin); 800 } catch (RemoteException re) { 801 Log.w(TAG, "Could not change restrictions pin"); 802 } 803 return false; 804 } 805 806 /** 807 * @hide 808 * @param pin The PIN to verify, or null to get the number of milliseconds to wait for before 809 * allowing the user to enter the PIN. 810 * @return Returns a positive number (including zero) for how many milliseconds before 811 * you can accept another PIN, when the input is null or the input doesn't match the saved PIN. 812 * Returns {@link #PIN_VERIFICATION_SUCCESS} if the input matches the saved PIN. Returns 813 * {@link #PIN_VERIFICATION_FAILED_NOT_SET} if there is no PIN set. 814 */ 815 public int checkRestrictionsChallenge(String pin) { 816 try { 817 return mService.checkRestrictionsChallenge(pin); 818 } catch (RemoteException re) { 819 Log.w(TAG, "Could not check restrictions pin"); 820 } 821 return PIN_VERIFICATION_FAILED_INCORRECT; 822 } 823 824 /** 825 * @hide 826 * Checks whether the user has restrictions that are PIN-protected. An application that 827 * participates in restrictions can check if the owner has requested a PIN challenge for 828 * any restricted operations. If there is a PIN in effect, the application should launch 829 * the PIN challenge activity {@link android.content.Intent#ACTION_RESTRICTIONS_CHALLENGE}. 830 * @see android.content.Intent#ACTION_RESTRICTIONS_CHALLENGE 831 * @return whether a restrictions PIN is in effect. 832 */ 833 public boolean hasRestrictionsChallenge() { 834 try { 835 return mService.hasRestrictionsChallenge(); 836 } catch (RemoteException re) { 837 Log.w(TAG, "Could not change restrictions pin"); 838 } 839 return false; 840 } 841 842 /** @hide */ 843 public void removeRestrictions() { 844 try { 845 mService.removeRestrictions(); 846 } catch (RemoteException re) { 847 Log.w(TAG, "Could not change restrictions pin"); 848 } 849 } 850} 851