UserManager.java revision bf3a9465483976dcd5692b619b47132c2b95f73e
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.ActivityManager; 19import android.app.ActivityManagerNative; 20import android.content.Context; 21import android.content.pm.UserInfo; 22import android.content.res.Resources; 23import android.graphics.Bitmap; 24import android.graphics.Canvas; 25import android.graphics.Bitmap.Config; 26import android.graphics.Rect; 27import android.graphics.drawable.BitmapDrawable; 28import android.graphics.drawable.Drawable; 29import android.provider.Settings; 30import android.util.Log; 31import android.view.WindowManager.LayoutParams; 32 33import com.android.internal.R; 34 35import java.util.ArrayList; 36import java.util.List; 37 38/** 39 * Manages users and user details on a multi-user system. 40 */ 41public class UserManager { 42 43 private static String TAG = "UserManager"; 44 private final IUserManager mService; 45 private final Context mContext; 46 47 /** 48 * Key for user restrictions. Specifies if a user is disallowed from adding and removing 49 * accounts. 50 * The default value is <code>false</code>. 51 * <p/> 52 * Type: Boolean 53 * @see #setUserRestrictions(Bundle) 54 * @see #getUserRestrictions() 55 */ 56 public static final String DISALLOW_MODIFY_ACCOUNTS = "no_modify_accounts"; 57 58 /** 59 * Key for user restrictions. Specifies if a user is disallowed from changing Wi-Fi 60 * access points. 61 * 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. 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. 143 * The default value is <code>false</code>. 144 * <p/> 145 * Type: Boolean 146 * @see #setUserRestrictions(Bundle) 147 * @see #getUserRestrictions() 148 */ 149 public static final String DISALLOW_REMOVE_USER = "no_remove_user"; 150 151 /** 152 * Key for user restrictions. Specifies if a user is disallowed from enabling or 153 * accessing debugging features. The default value is <code>false</code>. 154 * <p/> 155 * Type: Boolean 156 * @see #setUserRestrictions(Bundle) 157 * @see #getUserRestrictions() 158 */ 159 public static final String DISALLOW_DEBUGGING_FEATURES = "no_debugging_features"; 160 161 /** 162 * Key for user restrictions. Specifies if a user is disallowed from configuring VPN. 163 * The default value is <code>false</code>. 164 * <p/> 165 * Type: Boolean 166 * @see #setUserRestrictions(Bundle) 167 * @see #getUserRestrictions() 168 */ 169 public static final String DISALLOW_CONFIG_VPN = "no_config_vpn"; 170 171 /** 172 * Key for user restrictions. Specifies if a user is disallowed from configuring Tethering 173 * & portable hotspots. The default value is <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. 184 * The default value is <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. 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. 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. 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 controlling 233 * applications in Settings. The default value is <code>false</code>. 234 * <p> 235 * Type: Boolean 236 * @see #setUserRestrictions(Bundle) 237 * @see #getUserRestrictions() 238 */ 239 public static final String DISALLOW_APPS_CONTROL = "no_control_apps"; 240 241 /** 242 * Key for user restrictions. Specifies if a user is disallowed from mounting 243 * physical external media. The default value is <code>false</code>. 244 * <p/> 245 * Type: Boolean 246 * @see #setUserRestrictions(Bundle) 247 * @see #getUserRestrictions() 248 */ 249 public static final String DISALLOW_MOUNT_PHYSICAL_MEDIA = "no_physical_media"; 250 251 /** 252 * Key for user restrictions. Specifies if a user is disallowed from adjusting microphone 253 * volume. 254 * The default value is <code>false</code>. 255 * <p/> 256 * Type: Boolean 257 * @see #setUserRestrictions(Bundle) 258 * @see #getUserRestrictions() 259 */ 260 public static final String DISALLOW_UNMUTE_MICROPHONE = "no_unmute_microphone"; 261 262 /** 263 * Key for user restrictions. Specifies if a user is disallowed from adjusting the master 264 * volume. 265 * The default value is <code>false</code>. 266 * <p/> 267 * Type: Boolean 268 * @see #setUserRestrictions(Bundle) 269 * @see #getUserRestrictions() 270 */ 271 public static final String DISALLOW_ADJUST_VOLUME = "no_adjust_volume"; 272 273 /** 274 * Key for user restrictions. Specifies that the user is not allowed to make outgoing 275 * phone calls. Emergency calls are still permitted. 276 * The default value is <code>false</code>. 277 * <p/> 278 * Type: Boolean 279 * @see #setUserRestrictions(Bundle) 280 * @see #getUserRestrictions() 281 */ 282 public static final String DISALLOW_OUTGOING_CALLS = "no_outgoing_calls"; 283 284 /** 285 * Key for user restrictions. Specifies that the user is not allowed to send or receive 286 * SMS messages. 287 * The default value is <code>false</code>. 288 * <p/> 289 * Type: Boolean 290 * @see #setUserRestrictions(Bundle) 291 * @see #getUserRestrictions() 292 */ 293 public static final String DISALLOW_SMS = "no_sms"; 294 295 /** 296 * Key for user restrictions. Specifies that windows besides app windows should not be 297 * created. This will block the creation of the following types of windows. 298 * <li>{@link LayoutParams#TYPE_TOAST}</li> 299 * <li>{@link LayoutParams#TYPE_PHONE}</li> 300 * <li>{@link LayoutParams#TYPE_PRIORITY_PHONE}</li> 301 * <li>{@link LayoutParams#TYPE_SYSTEM_ALERT}</li> 302 * <li>{@link LayoutParams#TYPE_SYSTEM_ERROR}</li> 303 * <li>{@link LayoutParams#TYPE_SYSTEM_OVERLAY}</li> 304 * 305 * <p>The default value is <code>false</code>. 306 * <p/> 307 * Type: Boolean 308 * @see #setUserRestrictions(Bundle) 309 * @see #getUserRestrictions() 310 */ 311 public static final String DISALLOW_CREATE_WINDOWS = "no_create_windows"; 312 313 /** 314 * Key for user restrictions. Specifies if what is copied in the clipboard of this profile can 315 * be pasted in related profiles. Does not restrict if the clipboard of related profiles can be 316 * pasted in this profile. 317 * The default value is <code>false</code>. 318 * <p/> 319 * Type: Boolean 320 * @see #setUserRestrictions(Bundle) 321 * @see #getUserRestrictions() 322 */ 323 public static final String DISALLOW_CROSS_PROFILE_COPY_PASTE = "no_cross_profile_copy_paste"; 324 325 /** @hide */ 326 public static final int PIN_VERIFICATION_FAILED_INCORRECT = -3; 327 /** @hide */ 328 public static final int PIN_VERIFICATION_FAILED_NOT_SET = -2; 329 /** @hide */ 330 public static final int PIN_VERIFICATION_SUCCESS = -1; 331 332 private static UserManager sInstance = null; 333 334 /** @hide */ 335 public synchronized static UserManager get(Context context) { 336 if (sInstance == null) { 337 sInstance = (UserManager) context.getSystemService(Context.USER_SERVICE); 338 } 339 return sInstance; 340 } 341 342 /** @hide */ 343 public UserManager(Context context, IUserManager service) { 344 mService = service; 345 mContext = context; 346 } 347 348 /** 349 * Returns whether the system supports multiple users. 350 * @return true if multiple users can be created by user, false if it is a single user device. 351 * @hide 352 */ 353 public static boolean supportsMultipleUsers() { 354 return getMaxSupportedUsers() > 1 355 && SystemProperties.getBoolean("fw.show_multiuserui", 356 Resources.getSystem().getBoolean(R.bool.config_enableMultiUserUI)); 357 } 358 359 /** 360 * Returns the user handle for the user that the calling process is running on. 361 * 362 * @return the user handle of the user making this call. 363 * @hide 364 */ 365 public int getUserHandle() { 366 return UserHandle.myUserId(); 367 } 368 369 /** 370 * Returns the user name of the user making this call. This call is only 371 * available to applications on the system image; it requires the 372 * MANAGE_USERS permission. 373 * @return the user name 374 */ 375 public String getUserName() { 376 try { 377 return mService.getUserInfo(getUserHandle()).name; 378 } catch (RemoteException re) { 379 Log.w(TAG, "Could not get user name", re); 380 return ""; 381 } 382 } 383 384 /** 385 * Used to determine whether the user making this call is subject to 386 * teleportations. 387 * @return whether the user making this call is a goat 388 */ 389 public boolean isUserAGoat() { 390 return false; 391 } 392 393 /** 394 * Used to check if the user making this call is linked to another user. Linked users may have 395 * a reduced number of available apps, app restrictions and account restrictions. 396 * @return whether the user making this call is a linked user 397 * @hide 398 */ 399 public boolean isLinkedUser() { 400 try { 401 return mService.isRestricted(); 402 } catch (RemoteException re) { 403 Log.w(TAG, "Could not check if user is limited ", re); 404 return false; 405 } 406 } 407 408 /** 409 * Checks if the calling app is running as a guest user. 410 * @return whether the caller is a guest user. 411 * @hide 412 */ 413 public boolean isGuestUser() { 414 UserInfo user = getUserInfo(UserHandle.myUserId()); 415 return user != null ? user.isGuest() : false; 416 } 417 418 /** 419 * Return whether the given user is actively running. This means that 420 * the user is in the "started" state, not "stopped" -- it is currently 421 * allowed to run code through scheduled alarms, receiving broadcasts, 422 * etc. A started user may be either the current foreground user or a 423 * background user; the result here does not distinguish between the two. 424 * @param user The user to retrieve the running state for. 425 */ 426 public boolean isUserRunning(UserHandle user) { 427 try { 428 return ActivityManagerNative.getDefault().isUserRunning( 429 user.getIdentifier(), false); 430 } catch (RemoteException e) { 431 return false; 432 } 433 } 434 435 /** 436 * Return whether the given user is actively running <em>or</em> stopping. 437 * This is like {@link #isUserRunning(UserHandle)}, but will also return 438 * true if the user had been running but is in the process of being stopped 439 * (but is not yet fully stopped, and still running some code). 440 * @param user The user to retrieve the running state for. 441 */ 442 public boolean isUserRunningOrStopping(UserHandle user) { 443 try { 444 return ActivityManagerNative.getDefault().isUserRunning( 445 user.getIdentifier(), true); 446 } catch (RemoteException e) { 447 return false; 448 } 449 } 450 451 /** 452 * Returns the UserInfo object describing a specific user. 453 * Requires {@link android.Manifest.permission#MANAGE_USERS} permission. 454 * @param userHandle the user handle of the user whose information is being requested. 455 * @return the UserInfo object for a specific user. 456 * @hide 457 */ 458 public UserInfo getUserInfo(int userHandle) { 459 try { 460 return mService.getUserInfo(userHandle); 461 } catch (RemoteException re) { 462 Log.w(TAG, "Could not get user info", re); 463 return null; 464 } 465 } 466 467 /** 468 * Returns the user-wide restrictions imposed on this user. 469 * @return a Bundle containing all the restrictions. 470 */ 471 public Bundle getUserRestrictions() { 472 return getUserRestrictions(Process.myUserHandle()); 473 } 474 475 /** 476 * Returns the user-wide restrictions imposed on the user specified by <code>userHandle</code>. 477 * @param userHandle the UserHandle of the user for whom to retrieve the restrictions. 478 * @return a Bundle containing all the restrictions. 479 */ 480 public Bundle getUserRestrictions(UserHandle userHandle) { 481 try { 482 return mService.getUserRestrictions(userHandle.getIdentifier()); 483 } catch (RemoteException re) { 484 Log.w(TAG, "Could not get user restrictions", re); 485 return Bundle.EMPTY; 486 } 487 } 488 489 /** 490 * Sets all the user-wide restrictions for this user. 491 * Requires the MANAGE_USERS permission. 492 * @param restrictions the Bundle containing all the restrictions. 493 */ 494 public void setUserRestrictions(Bundle restrictions) { 495 setUserRestrictions(restrictions, Process.myUserHandle()); 496 } 497 498 /** 499 * Sets all the user-wide restrictions for the specified user. 500 * Requires the MANAGE_USERS permission. 501 * @param restrictions the Bundle containing all the restrictions. 502 * @param userHandle the UserHandle of the user for whom to set the restrictions. 503 */ 504 public void setUserRestrictions(Bundle restrictions, UserHandle userHandle) { 505 try { 506 mService.setUserRestrictions(restrictions, userHandle.getIdentifier()); 507 } catch (RemoteException re) { 508 Log.w(TAG, "Could not set user restrictions", re); 509 } 510 } 511 512 /** 513 * Sets the value of a specific restriction. 514 * Requires the MANAGE_USERS permission. 515 * @param key the key of the restriction 516 * @param value the value for the restriction 517 */ 518 public void setUserRestriction(String key, boolean value) { 519 Bundle bundle = getUserRestrictions(); 520 bundle.putBoolean(key, value); 521 setUserRestrictions(bundle); 522 } 523 524 /** 525 * @hide 526 * Sets the value of a specific restriction on a specific user. 527 * Requires the MANAGE_USERS permission. 528 * @param key the key of the restriction 529 * @param value the value for the restriction 530 * @param userHandle the user whose restriction is to be changed. 531 */ 532 public void setUserRestriction(String key, boolean value, UserHandle userHandle) { 533 Bundle bundle = getUserRestrictions(userHandle); 534 bundle.putBoolean(key, value); 535 setUserRestrictions(bundle, userHandle); 536 } 537 538 /** 539 * Returns whether the current user has been disallowed from performing certain actions 540 * or setting certain settings. 541 * 542 * @param restrictionKey The string key representing the restriction. 543 * @return {@code true} if the current user has the given restriction, {@code false} otherwise. 544 */ 545 public boolean hasUserRestriction(String restrictionKey) { 546 return hasUserRestriction(restrictionKey, Process.myUserHandle()); 547 } 548 549 /** 550 * @hide 551 * Returns whether the given user has been disallowed from performing certain actions 552 * or setting certain settings. 553 * @param restrictionKey the string key representing the restriction 554 * @param userHandle the UserHandle of the user for whom to retrieve the restrictions. 555 */ 556 public boolean hasUserRestriction(String restrictionKey, UserHandle userHandle) { 557 return getUserRestrictions(userHandle).getBoolean(restrictionKey, false); 558 } 559 560 /** 561 * Return the serial number for a user. This is a device-unique 562 * number assigned to that user; if the user is deleted and then a new 563 * user created, the new users will not be given the same serial number. 564 * @param user The user whose serial number is to be retrieved. 565 * @return The serial number of the given user; returns -1 if the 566 * given UserHandle does not exist. 567 * @see #getUserForSerialNumber(long) 568 */ 569 public long getSerialNumberForUser(UserHandle user) { 570 return getUserSerialNumber(user.getIdentifier()); 571 } 572 573 /** 574 * Return the user associated with a serial number previously 575 * returned by {@link #getSerialNumberForUser(UserHandle)}. 576 * @param serialNumber The serial number of the user that is being 577 * retrieved. 578 * @return Return the user associated with the serial number, or null 579 * if there is not one. 580 * @see #getSerialNumberForUser(UserHandle) 581 */ 582 public UserHandle getUserForSerialNumber(long serialNumber) { 583 int ident = getUserHandle((int)serialNumber); 584 return ident >= 0 ? new UserHandle(ident) : null; 585 } 586 587 /** 588 * Creates a user with the specified name and options. 589 * Requires {@link android.Manifest.permission#MANAGE_USERS} permission. 590 * 591 * @param name the user's name 592 * @param flags flags that identify the type of user and other properties. 593 * @see UserInfo 594 * 595 * @return the UserInfo object for the created user, or null if the user could not be created. 596 * @hide 597 */ 598 public UserInfo createUser(String name, int flags) { 599 try { 600 return mService.createUser(name, flags); 601 } catch (RemoteException re) { 602 Log.w(TAG, "Could not create a user", re); 603 return null; 604 } 605 } 606 607 /** 608 * Creates a guest user and configures it. 609 * @param context an application context 610 * @param name the name to set for the user 611 * @hide 612 */ 613 public UserInfo createGuest(Context context, String name) { 614 UserInfo guest = createUser(name, UserInfo.FLAG_GUEST); 615 if (guest != null) { 616 Settings.Secure.putStringForUser(context.getContentResolver(), 617 Settings.Secure.SKIP_FIRST_USE_HINTS, "1", guest.id); 618 try { 619 mService.setUserRestrictions( 620 mService.getDefaultGuestRestrictions(), guest.id); 621 } catch (RemoteException re) { 622 Log.w(TAG, "Could not update guest restrictions"); 623 } 624 } 625 return guest; 626 } 627 628 /** 629 * Creates a user with the specified name and options as a profile of another user. 630 * Requires {@link android.Manifest.permission#MANAGE_USERS} permission. 631 * 632 * @param name the user's name 633 * @param flags flags that identify the type of user and other properties. 634 * @see UserInfo 635 * @param userHandle new user will be a profile of this use. 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 createProfileForUser(String name, int flags, int userHandle) { 641 try { 642 return mService.createProfileForUser(name, flags, userHandle); 643 } catch (RemoteException re) { 644 Log.w(TAG, "Could not create a user", re); 645 return null; 646 } 647 } 648 649 /** 650 * Sets the user as enabled, if such an user exists. 651 * Requires {@link android.Manifest.permission#MANAGE_USERS} permission. 652 * Note that the default is true, it's only that managed profiles might not be enabled. 653 * 654 * @param userHandle the id of the profile to enable 655 * @hide 656 */ 657 public void setUserEnabled(int userHandle) { 658 try { 659 mService.setUserEnabled(userHandle); 660 } catch (RemoteException e) { 661 Log.w(TAG, "Could not enable the profile", e); 662 } 663 } 664 665 /** 666 * Return the number of users currently created on the device. 667 */ 668 public int getUserCount() { 669 List<UserInfo> users = getUsers(); 670 return users != null ? users.size() : 1; 671 } 672 673 /** 674 * Returns information for all users on this device. 675 * Requires {@link android.Manifest.permission#MANAGE_USERS} permission. 676 * @return the list of users that were created. 677 * @hide 678 */ 679 public List<UserInfo> getUsers() { 680 try { 681 return mService.getUsers(false); 682 } catch (RemoteException re) { 683 Log.w(TAG, "Could not get user list", re); 684 return null; 685 } 686 } 687 688 /** 689 * Returns list of the profiles of userHandle including 690 * userHandle itself. 691 * Note that it this returns both enabled and not enabled profiles. See 692 * {@link #getUserProfiles()} if you need only the enabled ones. 693 * 694 * Requires {@link android.Manifest.permission#MANAGE_USERS} permission. 695 * @param userHandle profiles of this user will be returned. 696 * @return the list of profiles. 697 * @hide 698 */ 699 public List<UserInfo> getProfiles(int userHandle) { 700 try { 701 return mService.getProfiles(userHandle, false /* enabledOnly */); 702 } catch (RemoteException re) { 703 Log.w(TAG, "Could not get user list", re); 704 return null; 705 } 706 } 707 708 /** 709 * Returns a list of UserHandles for profiles associated with the user that the calling process 710 * is running on, including the user itself. 711 * 712 * @return A non-empty list of UserHandles associated with the calling user. 713 */ 714 public List<UserHandle> getUserProfiles() { 715 ArrayList<UserHandle> profiles = new ArrayList<UserHandle>(); 716 List<UserInfo> users = new ArrayList<UserInfo>(); 717 try { 718 users = mService.getProfiles(UserHandle.myUserId(), true /* enabledOnly */); 719 } catch (RemoteException re) { 720 Log.w(TAG, "Could not get user list", re); 721 return null; 722 } 723 for (UserInfo info : users) { 724 UserHandle userHandle = new UserHandle(info.id); 725 profiles.add(userHandle); 726 } 727 return profiles; 728 } 729 730 /** 731 * Returns the parent of the profile which this method is called from 732 * or null if called from a user that is not a profile. 733 * 734 * @hide 735 */ 736 public UserInfo getProfileParent(int userHandle) { 737 try { 738 return mService.getProfileParent(userHandle); 739 } catch (RemoteException re) { 740 Log.w(TAG, "Could not get profile parent", re); 741 return null; 742 } 743 } 744 745 /** 746 * If the target user is a managed profile of the calling user or the caller 747 * is itself a managed profile, then this returns a badged copy of the given 748 * icon to be able to distinguish it from the original icon. 749 * <P> 750 * If the original drawable is not a BitmapDrawable, then the original 751 * drawable is returned. 752 * </P> 753 * 754 * @param icon The icon to badge. 755 * @param user The target user. 756 * @return A drawable that combines the original icon and a badge as 757 * determined by the system. 758 */ 759 public Drawable getBadgedDrawableForUser(Drawable icon, UserHandle user) { 760 int badgeResId = getBadgeResIdForUser(user.getIdentifier()); 761 if (badgeResId == 0) { 762 return icon; 763 } else { 764 Drawable badgeIcon = mContext.getPackageManager() 765 .getDrawable("system", badgeResId, null); 766 return getMergedDrawable(icon, badgeIcon); 767 } 768 } 769 770 /** 771 * If the target user is a managed profile of the calling user or the caller 772 * is itself a managed profile, then this returns a copy of the label with 773 * badging for accessibility services like talkback. E.g. passing in "Email" 774 * and it might return "Work Email" for Email in the work profile. 775 * 776 * @param label The label to change. 777 * @param user The target user. 778 * @return A label that combines the original label and a badge as 779 * determined by the system. 780 */ 781 public CharSequence getBadgedLabelForUser(CharSequence label, UserHandle user) { 782 UserInfo userInfo = getUserIfProfile(user.getIdentifier()); 783 if (userInfo != null && userInfo.isManagedProfile()) { 784 return Resources.getSystem().getString( 785 R.string.managed_profile_label_badge, label); 786 } 787 return label; 788 } 789 790 /** 791 * Kept during L development to simplify updating unbundled apps. 792 * TODO: Remove after 2014-08-04 793 * @hide 794 */ 795 public String getBadgedLabelForUser(String label, UserHandle user) { 796 return (String) getBadgedLabelForUser((CharSequence) label, user); 797 } 798 799 /** 800 * If the target user is a managed profile of the calling user or the caller 801 * is itself a managed profile, then this returns a drawable to use as a small 802 * icon to include in a view to distinguish it from the original icon. 803 * 804 * @param user The target user. 805 * @return the drawable or null if no drawable is required. 806 * @hide 807 */ 808 public Drawable getBadgeForUser(UserHandle user) { 809 UserInfo userInfo = getUserIfProfile(user.getIdentifier()); 810 if (userInfo != null && userInfo.isManagedProfile()) { 811 return Resources.getSystem().getDrawable( 812 com.android.internal.R.drawable.ic_corp_badge); 813 } 814 return null; 815 } 816 817 private int getBadgeResIdForUser(int userHandle) { 818 // Return the framework-provided badge. 819 UserInfo userInfo = getUserIfProfile(userHandle); 820 if (userInfo != null && userInfo.isManagedProfile()) { 821 return com.android.internal.R.drawable.ic_corp_icon_badge; 822 } 823 return 0; 824 } 825 826 /** 827 * @return UserInfo for userHandle if it exists and is a profile of the current 828 * user or null. 829 */ 830 private UserInfo getUserIfProfile(int userHandle) { 831 List<UserInfo> userProfiles = getProfiles(getUserHandle()); 832 for (UserInfo user : userProfiles) { 833 if (user.id == userHandle) { 834 return user; 835 } 836 } 837 return null; 838 } 839 840 private Drawable getMergedDrawable(Drawable icon, Drawable badge) { 841 final int width = icon.getIntrinsicWidth(); 842 final int height = icon.getIntrinsicHeight(); 843 Bitmap bitmap = Bitmap.createBitmap(width, height, Config.ARGB_8888); 844 Canvas canvas = new Canvas(bitmap); 845 icon.setBounds(0, 0, width, height); 846 icon.draw(canvas); 847 badge.setBounds(0, 0, width, height); 848 badge.draw(canvas); 849 BitmapDrawable merged = new BitmapDrawable(bitmap); 850 if (icon instanceof BitmapDrawable) { 851 merged.setTargetDensity(((BitmapDrawable) icon).getBitmap().getDensity()); 852 } 853 return merged; 854 } 855 856 /** 857 * Returns information for all users on this device. Requires 858 * {@link android.Manifest.permission#MANAGE_USERS} permission. 859 * 860 * @param excludeDying specify if the list should exclude users being 861 * removed. 862 * @return the list of users that were created. 863 * @hide 864 */ 865 public List<UserInfo> getUsers(boolean excludeDying) { 866 try { 867 return mService.getUsers(excludeDying); 868 } catch (RemoteException re) { 869 Log.w(TAG, "Could not get user list", re); 870 return null; 871 } 872 } 873 874 /** 875 * Removes a user and all associated data. 876 * Requires {@link android.Manifest.permission#MANAGE_USERS} permission. 877 * @param userHandle the integer handle of the user, where 0 is the primary user. 878 * @hide 879 */ 880 public boolean removeUser(int userHandle) { 881 try { 882 return mService.removeUser(userHandle); 883 } catch (RemoteException re) { 884 Log.w(TAG, "Could not remove user ", re); 885 return false; 886 } 887 } 888 889 /** 890 * Updates the user's name. 891 * Requires {@link android.Manifest.permission#MANAGE_USERS} permission. 892 * 893 * @param userHandle the user's integer handle 894 * @param name the new name for the user 895 * @hide 896 */ 897 public void setUserName(int userHandle, String name) { 898 try { 899 mService.setUserName(userHandle, name); 900 } catch (RemoteException re) { 901 Log.w(TAG, "Could not set the user name ", re); 902 } 903 } 904 905 /** 906 * Sets the user's photo. 907 * @param userHandle the user for whom to change the photo. 908 * @param icon the bitmap to set as the photo. 909 * @hide 910 */ 911 public void setUserIcon(int userHandle, Bitmap icon) { 912 try { 913 mService.setUserIcon(userHandle, icon); 914 } catch (RemoteException re) { 915 Log.w(TAG, "Could not set the user icon ", re); 916 } 917 } 918 919 /** 920 * Returns a file descriptor for the user's photo. PNG data can be read from this file. 921 * @param userHandle the user whose photo we want to read. 922 * @return a {@link Bitmap} of the user's photo, or null if there's no photo. 923 * @hide 924 */ 925 public Bitmap getUserIcon(int userHandle) { 926 try { 927 return mService.getUserIcon(userHandle); 928 } catch (RemoteException re) { 929 Log.w(TAG, "Could not get the user icon ", re); 930 return null; 931 } 932 } 933 934 /** 935 * Returns the maximum number of users that can be created on this device. A return value 936 * of 1 means that it is a single user device. 937 * @hide 938 * @return a value greater than or equal to 1 939 */ 940 public static int getMaxSupportedUsers() { 941 // Don't allow multiple users on certain builds 942 if (android.os.Build.ID.startsWith("JVP")) return 1; 943 // Svelte devices don't get multi-user. 944 if (ActivityManager.isLowRamDeviceStatic()) return 1; 945 return SystemProperties.getInt("fw.max_users", 946 Resources.getSystem().getInteger(R.integer.config_multiuserMaximumUsers)); 947 } 948 949 /** 950 * Returns true if the user switcher should be shown, this will be if there 951 * are multiple users that aren't managed profiles. 952 * @hide 953 * @return true if user switcher should be shown. 954 */ 955 public boolean isUserSwitcherEnabled() { 956 List<UserInfo> users = getUsers(true); 957 if (users == null) { 958 return false; 959 } 960 int switchableUserCount = 0; 961 for (UserInfo user : users) { 962 if (user.supportsSwitchTo()) { 963 ++switchableUserCount; 964 } 965 } 966 final boolean guestEnabled = Settings.Global.getInt(mContext.getContentResolver(), 967 Settings.Global.GUEST_USER_ENABLED, 0) == 1; 968 return switchableUserCount > 1 || guestEnabled; 969 } 970 971 /** 972 * Returns a serial number on this device for a given userHandle. User handles can be recycled 973 * when deleting and creating users, but serial numbers are not reused until the device is wiped. 974 * @param userHandle 975 * @return a serial number associated with that user, or -1 if the userHandle is not valid. 976 * @hide 977 */ 978 public int getUserSerialNumber(int userHandle) { 979 try { 980 return mService.getUserSerialNumber(userHandle); 981 } catch (RemoteException re) { 982 Log.w(TAG, "Could not get serial number for user " + userHandle); 983 } 984 return -1; 985 } 986 987 /** 988 * Returns a userHandle on this device for a given user serial number. User handles can be 989 * recycled when deleting and creating users, but serial numbers are not reused until the device 990 * is wiped. 991 * @param userSerialNumber 992 * @return the userHandle associated with that user serial number, or -1 if the serial number 993 * is not valid. 994 * @hide 995 */ 996 public int getUserHandle(int userSerialNumber) { 997 try { 998 return mService.getUserHandle(userSerialNumber); 999 } catch (RemoteException re) { 1000 Log.w(TAG, "Could not get userHandle for user " + userSerialNumber); 1001 } 1002 return -1; 1003 } 1004 1005 /** 1006 * Returns a Bundle containing any saved application restrictions for this user, for the 1007 * given package name. Only an application with this package name can call this method. 1008 * @param packageName the package name of the calling application 1009 * @return a Bundle with the restrictions as key/value pairs, or null if there are no 1010 * saved restrictions. The values can be of type Boolean, String or String[], depending 1011 * on the restriction type, as defined by the application. 1012 */ 1013 public Bundle getApplicationRestrictions(String packageName) { 1014 try { 1015 return mService.getApplicationRestrictions(packageName); 1016 } catch (RemoteException re) { 1017 Log.w(TAG, "Could not get application restrictions for package " + packageName); 1018 } 1019 return null; 1020 } 1021 1022 /** 1023 * @hide 1024 */ 1025 public Bundle getApplicationRestrictions(String packageName, UserHandle user) { 1026 try { 1027 return mService.getApplicationRestrictionsForUser(packageName, user.getIdentifier()); 1028 } catch (RemoteException re) { 1029 Log.w(TAG, "Could not get application restrictions for user " + user.getIdentifier()); 1030 } 1031 return null; 1032 } 1033 1034 /** 1035 * @hide 1036 */ 1037 public void setApplicationRestrictions(String packageName, Bundle restrictions, 1038 UserHandle user) { 1039 try { 1040 mService.setApplicationRestrictions(packageName, restrictions, user.getIdentifier()); 1041 } catch (RemoteException re) { 1042 Log.w(TAG, "Could not set application restrictions for user " + user.getIdentifier()); 1043 } 1044 } 1045 1046 /** 1047 * Sets a new challenge PIN for restrictions. This is only for use by pre-installed 1048 * apps and requires the MANAGE_USERS permission. 1049 * @param newPin the PIN to use for challenge dialogs. 1050 * @return Returns true if the challenge PIN was set successfully. 1051 */ 1052 public boolean setRestrictionsChallenge(String newPin) { 1053 try { 1054 return mService.setRestrictionsChallenge(newPin); 1055 } catch (RemoteException re) { 1056 Log.w(TAG, "Could not change restrictions pin"); 1057 } 1058 return false; 1059 } 1060 1061 /** 1062 * @hide 1063 * @param pin The PIN to verify, or null to get the number of milliseconds to wait for before 1064 * allowing the user to enter the PIN. 1065 * @return Returns a positive number (including zero) for how many milliseconds before 1066 * you can accept another PIN, when the input is null or the input doesn't match the saved PIN. 1067 * Returns {@link #PIN_VERIFICATION_SUCCESS} if the input matches the saved PIN. Returns 1068 * {@link #PIN_VERIFICATION_FAILED_NOT_SET} if there is no PIN set. 1069 */ 1070 public int checkRestrictionsChallenge(String pin) { 1071 try { 1072 return mService.checkRestrictionsChallenge(pin); 1073 } catch (RemoteException re) { 1074 Log.w(TAG, "Could not check restrictions pin"); 1075 } 1076 return PIN_VERIFICATION_FAILED_INCORRECT; 1077 } 1078 1079 /** 1080 * @hide 1081 * Checks whether the user has restrictions that are PIN-protected. An application that 1082 * participates in restrictions can check if the owner has requested a PIN challenge for 1083 * any restricted operations. If there is a PIN in effect, the application should launch 1084 * the PIN challenge activity {@link android.content.Intent#ACTION_RESTRICTIONS_CHALLENGE}. 1085 * @see android.content.Intent#ACTION_RESTRICTIONS_CHALLENGE 1086 * @return whether a restrictions PIN is in effect. 1087 */ 1088 public boolean hasRestrictionsChallenge() { 1089 try { 1090 return mService.hasRestrictionsChallenge(); 1091 } catch (RemoteException re) { 1092 Log.w(TAG, "Could not change restrictions pin"); 1093 } 1094 return false; 1095 } 1096 1097 /** @hide */ 1098 public void removeRestrictions() { 1099 try { 1100 mService.removeRestrictions(); 1101 } catch (RemoteException re) { 1102 Log.w(TAG, "Could not change restrictions pin"); 1103 } 1104 } 1105 1106 /** 1107 * @hide 1108 * Set restrictions that should apply to any future guest user that's created. 1109 */ 1110 public void setDefaultGuestRestrictions(Bundle restrictions) { 1111 try { 1112 mService.setDefaultGuestRestrictions(restrictions); 1113 } catch (RemoteException re) { 1114 Log.w(TAG, "Could not set guest restrictions"); 1115 } 1116 } 1117 1118 /** 1119 * @hide 1120 * Gets the default guest restrictions. 1121 */ 1122 public Bundle getDefaultGuestRestrictions() { 1123 try { 1124 return mService.getDefaultGuestRestrictions(); 1125 } catch (RemoteException re) { 1126 Log.w(TAG, "Could not set guest restrictions"); 1127 } 1128 return new Bundle(); 1129 } 1130} 1131