UserManager.java revision 237aecd18d0edf56c038761f570758c310c9e6df
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 } 619 return guest; 620 } 621 622 /** 623 * Creates a user with the specified name and options as a profile of another user. 624 * Requires {@link android.Manifest.permission#MANAGE_USERS} permission. 625 * 626 * @param name the user's name 627 * @param flags flags that identify the type of user and other properties. 628 * @see UserInfo 629 * @param userHandle new user will be a profile of this use. 630 * 631 * @return the UserInfo object for the created user, or null if the user could not be created. 632 * @hide 633 */ 634 public UserInfo createProfileForUser(String name, int flags, int userHandle) { 635 try { 636 return mService.createProfileForUser(name, flags, userHandle); 637 } catch (RemoteException re) { 638 Log.w(TAG, "Could not create a user", re); 639 return null; 640 } 641 } 642 643 /** 644 * Sets the user as enabled, if such an user exists. 645 * Requires {@link android.Manifest.permission#MANAGE_USERS} permission. 646 * Note that the default is true, it's only that managed profiles might not be enabled. 647 * 648 * @param userHandle the id of the profile to enable 649 * @hide 650 */ 651 public void setUserEnabled(int userHandle) { 652 try { 653 mService.setUserEnabled(userHandle); 654 } catch (RemoteException e) { 655 Log.w(TAG, "Could not enable the profile", e); 656 } 657 } 658 659 /** 660 * Return the number of users currently created on the device. 661 */ 662 public int getUserCount() { 663 List<UserInfo> users = getUsers(); 664 return users != null ? users.size() : 1; 665 } 666 667 /** 668 * Returns information for all users on this device. 669 * Requires {@link android.Manifest.permission#MANAGE_USERS} permission. 670 * @return the list of users that were created. 671 * @hide 672 */ 673 public List<UserInfo> getUsers() { 674 try { 675 return mService.getUsers(false); 676 } catch (RemoteException re) { 677 Log.w(TAG, "Could not get user list", re); 678 return null; 679 } 680 } 681 682 /** 683 * Returns list of the profiles of userHandle including 684 * userHandle itself. 685 * Note that it this returns both enabled and not enabled profiles. See 686 * {@link #getUserProfiles()} if you need only the enabled ones. 687 * 688 * Requires {@link android.Manifest.permission#MANAGE_USERS} permission. 689 * @param userHandle profiles of this user will be returned. 690 * @return the list of profiles. 691 * @hide 692 */ 693 public List<UserInfo> getProfiles(int userHandle) { 694 try { 695 return mService.getProfiles(userHandle, false /* enabledOnly */); 696 } catch (RemoteException re) { 697 Log.w(TAG, "Could not get user list", re); 698 return null; 699 } 700 } 701 702 /** 703 * Returns a list of UserHandles for profiles associated with the user that the calling process 704 * is running on, including the user itself. 705 * 706 * @return A non-empty list of UserHandles associated with the calling user. 707 */ 708 public List<UserHandle> getUserProfiles() { 709 ArrayList<UserHandle> profiles = new ArrayList<UserHandle>(); 710 List<UserInfo> users = new ArrayList<UserInfo>(); 711 try { 712 users = mService.getProfiles(UserHandle.myUserId(), true /* enabledOnly */); 713 } catch (RemoteException re) { 714 Log.w(TAG, "Could not get user list", re); 715 return null; 716 } 717 for (UserInfo info : users) { 718 UserHandle userHandle = new UserHandle(info.id); 719 profiles.add(userHandle); 720 } 721 return profiles; 722 } 723 724 /** 725 * Returns the parent of the profile which this method is called from 726 * or null if called from a user that is not a profile. 727 * 728 * @hide 729 */ 730 public UserInfo getProfileParent(int userHandle) { 731 try { 732 return mService.getProfileParent(userHandle); 733 } catch (RemoteException re) { 734 Log.w(TAG, "Could not get profile parent", re); 735 return null; 736 } 737 } 738 739 /** 740 * If the target user is a managed profile of the calling user or the caller 741 * is itself a managed profile, then this returns a badged copy of the given 742 * icon to be able to distinguish it from the original icon. 743 * <P> 744 * If the original drawable is not a BitmapDrawable, then the original 745 * drawable is returned. 746 * </P> 747 * 748 * @param icon The icon to badge. 749 * @param user The target user. 750 * @return A drawable that combines the original icon and a badge as 751 * determined by the system. 752 */ 753 public Drawable getBadgedDrawableForUser(Drawable icon, UserHandle user) { 754 int badgeResId = getBadgeResIdForUser(user.getIdentifier()); 755 if (badgeResId == 0) { 756 return icon; 757 } else { 758 Drawable badgeIcon = mContext.getPackageManager() 759 .getDrawable("system", badgeResId, null); 760 return getMergedDrawable(icon, badgeIcon); 761 } 762 } 763 764 /** 765 * If the target user is a managed profile of the calling user or the caller 766 * is itself a managed profile, then this returns a copy of the label with 767 * badging for accessibility services like talkback. E.g. passing in "Email" 768 * and it might return "Work Email" for Email in the work profile. 769 * 770 * @param label The label to change. 771 * @param user The target user. 772 * @return A label that combines the original label and a badge as 773 * determined by the system. 774 */ 775 public CharSequence getBadgedLabelForUser(CharSequence label, UserHandle user) { 776 UserInfo userInfo = getUserIfProfile(user.getIdentifier()); 777 if (userInfo != null && userInfo.isManagedProfile()) { 778 return Resources.getSystem().getString( 779 R.string.managed_profile_label_badge, label); 780 } 781 return label; 782 } 783 784 /** 785 * Kept during L development to simplify updating unbundled apps. 786 * TODO: Remove after 2014-08-04 787 * @hide 788 */ 789 public String getBadgedLabelForUser(String label, UserHandle user) { 790 return (String) getBadgedLabelForUser((CharSequence) label, user); 791 } 792 793 /** 794 * If the target user is a managed profile of the calling user or the caller 795 * is itself a managed profile, then this returns a drawable to use as a small 796 * icon to include in a view to distinguish it from the original icon. 797 * 798 * @param user The target user. 799 * @return the drawable or null if no drawable is required. 800 * @hide 801 */ 802 public Drawable getBadgeForUser(UserHandle user) { 803 UserInfo userInfo = getUserIfProfile(user.getIdentifier()); 804 if (userInfo != null && userInfo.isManagedProfile()) { 805 return Resources.getSystem().getDrawable( 806 com.android.internal.R.drawable.ic_corp_badge); 807 } 808 return null; 809 } 810 811 private int getBadgeResIdForUser(int userHandle) { 812 // Return the framework-provided badge. 813 UserInfo userInfo = getUserIfProfile(userHandle); 814 if (userInfo != null && userInfo.isManagedProfile()) { 815 return com.android.internal.R.drawable.ic_corp_icon_badge; 816 } 817 return 0; 818 } 819 820 /** 821 * @return UserInfo for userHandle if it exists and is a profile of the current 822 * user or null. 823 */ 824 private UserInfo getUserIfProfile(int userHandle) { 825 List<UserInfo> userProfiles = getProfiles(getUserHandle()); 826 for (UserInfo user : userProfiles) { 827 if (user.id == userHandle) { 828 return user; 829 } 830 } 831 return null; 832 } 833 834 private Drawable getMergedDrawable(Drawable icon, Drawable badge) { 835 final int width = icon.getIntrinsicWidth(); 836 final int height = icon.getIntrinsicHeight(); 837 Bitmap bitmap = Bitmap.createBitmap(width, height, Config.ARGB_8888); 838 Canvas canvas = new Canvas(bitmap); 839 icon.setBounds(0, 0, width, height); 840 icon.draw(canvas); 841 badge.setBounds(0, 0, width, height); 842 badge.draw(canvas); 843 BitmapDrawable merged = new BitmapDrawable(bitmap); 844 if (icon instanceof BitmapDrawable) { 845 merged.setTargetDensity(((BitmapDrawable) icon).getBitmap().getDensity()); 846 } 847 return merged; 848 } 849 850 /** 851 * Returns information for all users on this device. Requires 852 * {@link android.Manifest.permission#MANAGE_USERS} permission. 853 * 854 * @param excludeDying specify if the list should exclude users being 855 * removed. 856 * @return the list of users that were created. 857 * @hide 858 */ 859 public List<UserInfo> getUsers(boolean excludeDying) { 860 try { 861 return mService.getUsers(excludeDying); 862 } catch (RemoteException re) { 863 Log.w(TAG, "Could not get user list", re); 864 return null; 865 } 866 } 867 868 /** 869 * Removes a user and all associated data. 870 * Requires {@link android.Manifest.permission#MANAGE_USERS} permission. 871 * @param userHandle the integer handle of the user, where 0 is the primary user. 872 * @hide 873 */ 874 public boolean removeUser(int userHandle) { 875 try { 876 return mService.removeUser(userHandle); 877 } catch (RemoteException re) { 878 Log.w(TAG, "Could not remove user ", re); 879 return false; 880 } 881 } 882 883 /** 884 * Updates the user's name. 885 * Requires {@link android.Manifest.permission#MANAGE_USERS} permission. 886 * 887 * @param userHandle the user's integer handle 888 * @param name the new name for the user 889 * @hide 890 */ 891 public void setUserName(int userHandle, String name) { 892 try { 893 mService.setUserName(userHandle, name); 894 } catch (RemoteException re) { 895 Log.w(TAG, "Could not set the user name ", re); 896 } 897 } 898 899 /** 900 * Sets the user's photo. 901 * @param userHandle the user for whom to change the photo. 902 * @param icon the bitmap to set as the photo. 903 * @hide 904 */ 905 public void setUserIcon(int userHandle, Bitmap icon) { 906 try { 907 mService.setUserIcon(userHandle, icon); 908 } catch (RemoteException re) { 909 Log.w(TAG, "Could not set the user icon ", re); 910 } 911 } 912 913 /** 914 * Returns a file descriptor for the user's photo. PNG data can be read from this file. 915 * @param userHandle the user whose photo we want to read. 916 * @return a {@link Bitmap} of the user's photo, or null if there's no photo. 917 * @hide 918 */ 919 public Bitmap getUserIcon(int userHandle) { 920 try { 921 return mService.getUserIcon(userHandle); 922 } catch (RemoteException re) { 923 Log.w(TAG, "Could not get the user icon ", re); 924 return null; 925 } 926 } 927 928 /** 929 * Returns the maximum number of users that can be created on this device. A return value 930 * of 1 means that it is a single user device. 931 * @hide 932 * @return a value greater than or equal to 1 933 */ 934 public static int getMaxSupportedUsers() { 935 // Don't allow multiple users on certain builds 936 if (android.os.Build.ID.startsWith("JVP")) return 1; 937 // Svelte devices don't get multi-user. 938 if (ActivityManager.isLowRamDeviceStatic()) return 1; 939 return SystemProperties.getInt("fw.max_users", 940 Resources.getSystem().getInteger(R.integer.config_multiuserMaximumUsers)); 941 } 942 943 /** 944 * Returns true if the user switcher should be shown, this will be if there 945 * are multiple users that aren't managed profiles. 946 * @hide 947 * @return true if user switcher should be shown. 948 */ 949 public boolean isUserSwitcherEnabled() { 950 List<UserInfo> users = getUsers(true); 951 if (users == null) { 952 return false; 953 } 954 int switchableUserCount = 0; 955 for (UserInfo user : users) { 956 if (user.supportsSwitchTo()) { 957 ++switchableUserCount; 958 } 959 } 960 final boolean guestEnabled = Settings.Global.getInt(mContext.getContentResolver(), 961 Settings.Global.GUEST_USER_ENABLED, 0) == 1; 962 return switchableUserCount > 1 || guestEnabled; 963 } 964 965 /** 966 * Returns a serial number on this device for a given userHandle. User handles can be recycled 967 * when deleting and creating users, but serial numbers are not reused until the device is wiped. 968 * @param userHandle 969 * @return a serial number associated with that user, or -1 if the userHandle is not valid. 970 * @hide 971 */ 972 public int getUserSerialNumber(int userHandle) { 973 try { 974 return mService.getUserSerialNumber(userHandle); 975 } catch (RemoteException re) { 976 Log.w(TAG, "Could not get serial number for user " + userHandle); 977 } 978 return -1; 979 } 980 981 /** 982 * Returns a userHandle on this device for a given user serial number. User handles can be 983 * recycled when deleting and creating users, but serial numbers are not reused until the device 984 * is wiped. 985 * @param userSerialNumber 986 * @return the userHandle associated with that user serial number, or -1 if the serial number 987 * is not valid. 988 * @hide 989 */ 990 public int getUserHandle(int userSerialNumber) { 991 try { 992 return mService.getUserHandle(userSerialNumber); 993 } catch (RemoteException re) { 994 Log.w(TAG, "Could not get userHandle for user " + userSerialNumber); 995 } 996 return -1; 997 } 998 999 /** 1000 * Returns a Bundle containing any saved application restrictions for this user, for the 1001 * given package name. Only an application with this package name can call this method. 1002 * @param packageName the package name of the calling application 1003 * @return a Bundle with the restrictions as key/value pairs, or null if there are no 1004 * saved restrictions. The values can be of type Boolean, String or String[], depending 1005 * on the restriction type, as defined by the application. 1006 */ 1007 public Bundle getApplicationRestrictions(String packageName) { 1008 try { 1009 return mService.getApplicationRestrictions(packageName); 1010 } catch (RemoteException re) { 1011 Log.w(TAG, "Could not get application restrictions for package " + packageName); 1012 } 1013 return null; 1014 } 1015 1016 /** 1017 * @hide 1018 */ 1019 public Bundle getApplicationRestrictions(String packageName, UserHandle user) { 1020 try { 1021 return mService.getApplicationRestrictionsForUser(packageName, user.getIdentifier()); 1022 } catch (RemoteException re) { 1023 Log.w(TAG, "Could not get application restrictions for user " + user.getIdentifier()); 1024 } 1025 return null; 1026 } 1027 1028 /** 1029 * @hide 1030 */ 1031 public void setApplicationRestrictions(String packageName, Bundle restrictions, 1032 UserHandle user) { 1033 try { 1034 mService.setApplicationRestrictions(packageName, restrictions, user.getIdentifier()); 1035 } catch (RemoteException re) { 1036 Log.w(TAG, "Could not set application restrictions for user " + user.getIdentifier()); 1037 } 1038 } 1039 1040 /** 1041 * Sets a new challenge PIN for restrictions. This is only for use by pre-installed 1042 * apps and requires the MANAGE_USERS permission. 1043 * @param newPin the PIN to use for challenge dialogs. 1044 * @return Returns true if the challenge PIN was set successfully. 1045 */ 1046 public boolean setRestrictionsChallenge(String newPin) { 1047 try { 1048 return mService.setRestrictionsChallenge(newPin); 1049 } catch (RemoteException re) { 1050 Log.w(TAG, "Could not change restrictions pin"); 1051 } 1052 return false; 1053 } 1054 1055 /** 1056 * @hide 1057 * @param pin The PIN to verify, or null to get the number of milliseconds to wait for before 1058 * allowing the user to enter the PIN. 1059 * @return Returns a positive number (including zero) for how many milliseconds before 1060 * you can accept another PIN, when the input is null or the input doesn't match the saved PIN. 1061 * Returns {@link #PIN_VERIFICATION_SUCCESS} if the input matches the saved PIN. Returns 1062 * {@link #PIN_VERIFICATION_FAILED_NOT_SET} if there is no PIN set. 1063 */ 1064 public int checkRestrictionsChallenge(String pin) { 1065 try { 1066 return mService.checkRestrictionsChallenge(pin); 1067 } catch (RemoteException re) { 1068 Log.w(TAG, "Could not check restrictions pin"); 1069 } 1070 return PIN_VERIFICATION_FAILED_INCORRECT; 1071 } 1072 1073 /** 1074 * @hide 1075 * Checks whether the user has restrictions that are PIN-protected. An application that 1076 * participates in restrictions can check if the owner has requested a PIN challenge for 1077 * any restricted operations. If there is a PIN in effect, the application should launch 1078 * the PIN challenge activity {@link android.content.Intent#ACTION_RESTRICTIONS_CHALLENGE}. 1079 * @see android.content.Intent#ACTION_RESTRICTIONS_CHALLENGE 1080 * @return whether a restrictions PIN is in effect. 1081 */ 1082 public boolean hasRestrictionsChallenge() { 1083 try { 1084 return mService.hasRestrictionsChallenge(); 1085 } catch (RemoteException re) { 1086 Log.w(TAG, "Could not change restrictions pin"); 1087 } 1088 return false; 1089 } 1090 1091 /** @hide */ 1092 public void removeRestrictions() { 1093 try { 1094 mService.removeRestrictions(); 1095 } catch (RemoteException re) { 1096 Log.w(TAG, "Could not change restrictions pin"); 1097 } 1098 } 1099 1100 /** 1101 * @hide 1102 * Set restrictions that should apply to any future guest user that's created. 1103 */ 1104 public void setDefaultGuestRestrictions(Bundle restrictions) { 1105 try { 1106 mService.setDefaultGuestRestrictions(restrictions); 1107 } catch (RemoteException re) { 1108 Log.w(TAG, "Could not set guest restrictions"); 1109 } 1110 } 1111 1112 /** 1113 * @hide 1114 * Gets the default guest restrictions. 1115 */ 1116 public Bundle getDefaultGuestRestrictions() { 1117 try { 1118 return mService.getDefaultGuestRestrictions(); 1119 } catch (RemoteException re) { 1120 Log.w(TAG, "Could not set guest restrictions"); 1121 } 1122 return new Bundle(); 1123 } 1124} 1125