UserManager.java revision f1939901d2ed0480069f0b23be64f122fce93995
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 String getBadgedLabelForUser(String 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 * If the target user is a managed profile of the calling user or the caller 786 * is itself a managed profile, then this returns a drawable to use as a small 787 * icon to include in a view to distinguish it from the original icon. 788 * 789 * @param user The target user. 790 * @return the drawable or null if no drawable is required. 791 * @hide 792 */ 793 public Drawable getBadgeForUser(UserHandle user) { 794 UserInfo userInfo = getUserIfProfile(user.getIdentifier()); 795 if (userInfo != null && userInfo.isManagedProfile()) { 796 return Resources.getSystem().getDrawable( 797 com.android.internal.R.drawable.ic_corp_badge); 798 } 799 return null; 800 } 801 802 private int getBadgeResIdForUser(int userHandle) { 803 // Return the framework-provided badge. 804 UserInfo userInfo = getUserIfProfile(userHandle); 805 if (userInfo != null && userInfo.isManagedProfile()) { 806 return com.android.internal.R.drawable.ic_corp_icon_badge; 807 } 808 return 0; 809 } 810 811 /** 812 * @return UserInfo for userHandle if it exists and is a profile of the current 813 * user or null. 814 */ 815 private UserInfo getUserIfProfile(int userHandle) { 816 List<UserInfo> userProfiles = getProfiles(getUserHandle()); 817 for (UserInfo user : userProfiles) { 818 if (user.id == userHandle) { 819 return user; 820 } 821 } 822 return null; 823 } 824 825 private Drawable getMergedDrawable(Drawable icon, Drawable badge) { 826 final int width = icon.getIntrinsicWidth(); 827 final int height = icon.getIntrinsicHeight(); 828 Bitmap bitmap = Bitmap.createBitmap(width, height, Config.ARGB_8888); 829 Canvas canvas = new Canvas(bitmap); 830 icon.setBounds(0, 0, width, height); 831 icon.draw(canvas); 832 badge.setBounds(0, 0, width, height); 833 badge.draw(canvas); 834 BitmapDrawable merged = new BitmapDrawable(bitmap); 835 if (icon instanceof BitmapDrawable) { 836 merged.setTargetDensity(((BitmapDrawable) icon).getBitmap().getDensity()); 837 } 838 return merged; 839 } 840 841 /** 842 * Returns information for all users on this device. Requires 843 * {@link android.Manifest.permission#MANAGE_USERS} permission. 844 * 845 * @param excludeDying specify if the list should exclude users being 846 * removed. 847 * @return the list of users that were created. 848 * @hide 849 */ 850 public List<UserInfo> getUsers(boolean excludeDying) { 851 try { 852 return mService.getUsers(excludeDying); 853 } catch (RemoteException re) { 854 Log.w(TAG, "Could not get user list", re); 855 return null; 856 } 857 } 858 859 /** 860 * Removes a user and all associated data. 861 * Requires {@link android.Manifest.permission#MANAGE_USERS} permission. 862 * @param userHandle the integer handle of the user, where 0 is the primary user. 863 * @hide 864 */ 865 public boolean removeUser(int userHandle) { 866 try { 867 return mService.removeUser(userHandle); 868 } catch (RemoteException re) { 869 Log.w(TAG, "Could not remove user ", re); 870 return false; 871 } 872 } 873 874 /** 875 * Updates the user's name. 876 * Requires {@link android.Manifest.permission#MANAGE_USERS} permission. 877 * 878 * @param userHandle the user's integer handle 879 * @param name the new name for the user 880 * @hide 881 */ 882 public void setUserName(int userHandle, String name) { 883 try { 884 mService.setUserName(userHandle, name); 885 } catch (RemoteException re) { 886 Log.w(TAG, "Could not set the user name ", re); 887 } 888 } 889 890 /** 891 * Sets the user's photo. 892 * @param userHandle the user for whom to change the photo. 893 * @param icon the bitmap to set as the photo. 894 * @hide 895 */ 896 public void setUserIcon(int userHandle, Bitmap icon) { 897 try { 898 mService.setUserIcon(userHandle, icon); 899 } catch (RemoteException re) { 900 Log.w(TAG, "Could not set the user icon ", re); 901 } 902 } 903 904 /** 905 * Returns a file descriptor for the user's photo. PNG data can be read from this file. 906 * @param userHandle the user whose photo we want to read. 907 * @return a {@link Bitmap} of the user's photo, or null if there's no photo. 908 * @hide 909 */ 910 public Bitmap getUserIcon(int userHandle) { 911 try { 912 return mService.getUserIcon(userHandle); 913 } catch (RemoteException re) { 914 Log.w(TAG, "Could not get the user icon ", re); 915 return null; 916 } 917 } 918 919 /** 920 * Returns the maximum number of users that can be created on this device. A return value 921 * of 1 means that it is a single user device. 922 * @hide 923 * @return a value greater than or equal to 1 924 */ 925 public static int getMaxSupportedUsers() { 926 // Don't allow multiple users on certain builds 927 if (android.os.Build.ID.startsWith("JVP")) return 1; 928 // Svelte devices don't get multi-user. 929 if (ActivityManager.isLowRamDeviceStatic()) return 1; 930 return SystemProperties.getInt("fw.max_users", 931 Resources.getSystem().getInteger(R.integer.config_multiuserMaximumUsers)); 932 } 933 934 /** 935 * Returns true if the user switcher should be shown, this will be if there 936 * are multiple users that aren't managed profiles. 937 * @hide 938 * @return true if user switcher should be shown. 939 */ 940 public boolean isUserSwitcherEnabled() { 941 List<UserInfo> users = getUsers(true); 942 if (users == null) { 943 return false; 944 } 945 int switchableUserCount = 0; 946 for (UserInfo user : users) { 947 if (user.supportsSwitchTo()) { 948 ++switchableUserCount; 949 } 950 } 951 final boolean guestEnabled = Settings.Global.getInt(mContext.getContentResolver(), 952 Settings.Global.GUEST_USER_ENABLED, 0) == 1; 953 return switchableUserCount > 1 || guestEnabled; 954 } 955 956 /** 957 * Returns a serial number on this device for a given userHandle. User handles can be recycled 958 * when deleting and creating users, but serial numbers are not reused until the device is wiped. 959 * @param userHandle 960 * @return a serial number associated with that user, or -1 if the userHandle is not valid. 961 * @hide 962 */ 963 public int getUserSerialNumber(int userHandle) { 964 try { 965 return mService.getUserSerialNumber(userHandle); 966 } catch (RemoteException re) { 967 Log.w(TAG, "Could not get serial number for user " + userHandle); 968 } 969 return -1; 970 } 971 972 /** 973 * Returns a userHandle on this device for a given user serial number. User handles can be 974 * recycled when deleting and creating users, but serial numbers are not reused until the device 975 * is wiped. 976 * @param userSerialNumber 977 * @return the userHandle associated with that user serial number, or -1 if the serial number 978 * is not valid. 979 * @hide 980 */ 981 public int getUserHandle(int userSerialNumber) { 982 try { 983 return mService.getUserHandle(userSerialNumber); 984 } catch (RemoteException re) { 985 Log.w(TAG, "Could not get userHandle for user " + userSerialNumber); 986 } 987 return -1; 988 } 989 990 /** 991 * Returns a Bundle containing any saved application restrictions for this user, for the 992 * given package name. Only an application with this package name can call this method. 993 * @param packageName the package name of the calling application 994 * @return a Bundle with the restrictions as key/value pairs, or null if there are no 995 * saved restrictions. The values can be of type Boolean, String or String[], depending 996 * on the restriction type, as defined by the application. 997 */ 998 public Bundle getApplicationRestrictions(String packageName) { 999 try { 1000 return mService.getApplicationRestrictions(packageName); 1001 } catch (RemoteException re) { 1002 Log.w(TAG, "Could not get application restrictions for package " + packageName); 1003 } 1004 return null; 1005 } 1006 1007 /** 1008 * @hide 1009 */ 1010 public Bundle getApplicationRestrictions(String packageName, UserHandle user) { 1011 try { 1012 return mService.getApplicationRestrictionsForUser(packageName, user.getIdentifier()); 1013 } catch (RemoteException re) { 1014 Log.w(TAG, "Could not get application restrictions for user " + user.getIdentifier()); 1015 } 1016 return null; 1017 } 1018 1019 /** 1020 * @hide 1021 */ 1022 public void setApplicationRestrictions(String packageName, Bundle restrictions, 1023 UserHandle user) { 1024 try { 1025 mService.setApplicationRestrictions(packageName, restrictions, user.getIdentifier()); 1026 } catch (RemoteException re) { 1027 Log.w(TAG, "Could not set application restrictions for user " + user.getIdentifier()); 1028 } 1029 } 1030 1031 /** 1032 * Sets a new challenge PIN for restrictions. This is only for use by pre-installed 1033 * apps and requires the MANAGE_USERS permission. 1034 * @param newPin the PIN to use for challenge dialogs. 1035 * @return Returns true if the challenge PIN was set successfully. 1036 */ 1037 public boolean setRestrictionsChallenge(String newPin) { 1038 try { 1039 return mService.setRestrictionsChallenge(newPin); 1040 } catch (RemoteException re) { 1041 Log.w(TAG, "Could not change restrictions pin"); 1042 } 1043 return false; 1044 } 1045 1046 /** 1047 * @hide 1048 * @param pin The PIN to verify, or null to get the number of milliseconds to wait for before 1049 * allowing the user to enter the PIN. 1050 * @return Returns a positive number (including zero) for how many milliseconds before 1051 * you can accept another PIN, when the input is null or the input doesn't match the saved PIN. 1052 * Returns {@link #PIN_VERIFICATION_SUCCESS} if the input matches the saved PIN. Returns 1053 * {@link #PIN_VERIFICATION_FAILED_NOT_SET} if there is no PIN set. 1054 */ 1055 public int checkRestrictionsChallenge(String pin) { 1056 try { 1057 return mService.checkRestrictionsChallenge(pin); 1058 } catch (RemoteException re) { 1059 Log.w(TAG, "Could not check restrictions pin"); 1060 } 1061 return PIN_VERIFICATION_FAILED_INCORRECT; 1062 } 1063 1064 /** 1065 * @hide 1066 * Checks whether the user has restrictions that are PIN-protected. An application that 1067 * participates in restrictions can check if the owner has requested a PIN challenge for 1068 * any restricted operations. If there is a PIN in effect, the application should launch 1069 * the PIN challenge activity {@link android.content.Intent#ACTION_RESTRICTIONS_CHALLENGE}. 1070 * @see android.content.Intent#ACTION_RESTRICTIONS_CHALLENGE 1071 * @return whether a restrictions PIN is in effect. 1072 */ 1073 public boolean hasRestrictionsChallenge() { 1074 try { 1075 return mService.hasRestrictionsChallenge(); 1076 } catch (RemoteException re) { 1077 Log.w(TAG, "Could not change restrictions pin"); 1078 } 1079 return false; 1080 } 1081 1082 /** @hide */ 1083 public void removeRestrictions() { 1084 try { 1085 mService.removeRestrictions(); 1086 } catch (RemoteException re) { 1087 Log.w(TAG, "Could not change restrictions pin"); 1088 } 1089 } 1090 1091 /** 1092 * @hide 1093 * Set restrictions that should apply to any future guest user that's created. 1094 */ 1095 public void setDefaultGuestRestrictions(Bundle restrictions) { 1096 try { 1097 mService.setDefaultGuestRestrictions(restrictions); 1098 } catch (RemoteException re) { 1099 Log.w(TAG, "Could not set guest restrictions"); 1100 } 1101 } 1102 1103 /** 1104 * @hide 1105 * Gets the default guest restrictions. 1106 */ 1107 public Bundle getDefaultGuestRestrictions() { 1108 try { 1109 return mService.getDefaultGuestRestrictions(); 1110 } catch (RemoteException re) { 1111 Log.w(TAG, "Could not set guest restrictions"); 1112 } 1113 return new Bundle(); 1114 } 1115} 1116