UserManager.java revision 298f98fa234f353267ddc9c75d58e8cc542c25f1
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.annotation.SystemApi; 19import android.app.ActivityManager; 20import android.app.ActivityManagerNative; 21import android.content.Context; 22import android.content.pm.UserInfo; 23import android.content.res.Resources; 24import android.graphics.Bitmap; 25import android.graphics.BitmapFactory; 26import android.graphics.Rect; 27import android.graphics.drawable.Drawable; 28import android.provider.Settings; 29import android.util.Log; 30import android.view.WindowManager.LayoutParams; 31 32import com.android.internal.R; 33 34import java.io.IOException; 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 * Specifies if a user is disallowed from adding and removing accounts. 49 * The default value is <code>false</code>. 50 * 51 * <p/>Key for user restrictions. 52 * <p/>Type: Boolean 53 * @see #setUserRestrictions(Bundle) 54 * @see #getUserRestrictions() 55 */ 56 public static final String DISALLOW_MODIFY_ACCOUNTS = "no_modify_accounts"; 57 58 /** 59 * Specifies if a user is disallowed from changing Wi-Fi 60 * access points. The default value is <code>false</code>. 61 * <p/>This restriction has no effect in a managed profile. 62 * 63 * <p/>Key for user restrictions. 64 * <p/>Type: Boolean 65 * @see #setUserRestrictions(Bundle) 66 * @see #getUserRestrictions() 67 */ 68 public static final String DISALLOW_CONFIG_WIFI = "no_config_wifi"; 69 70 /** 71 * Specifies if a user is disallowed from installing applications. 72 * The default value is <code>false</code>. 73 * 74 * <p/>Key for user restrictions. 75 * <p/>Type: Boolean 76 * @see #setUserRestrictions(Bundle) 77 * @see #getUserRestrictions() 78 */ 79 public static final String DISALLOW_INSTALL_APPS = "no_install_apps"; 80 81 /** 82 * Specifies if a user is disallowed from uninstalling applications. 83 * The default value is <code>false</code>. 84 * 85 * <p/>Key for user restrictions. 86 * <p/>Type: Boolean 87 * @see #setUserRestrictions(Bundle) 88 * @see #getUserRestrictions() 89 */ 90 public static final String DISALLOW_UNINSTALL_APPS = "no_uninstall_apps"; 91 92 /** 93 * Specifies if a user is disallowed from turning on location sharing. 94 * The default value is <code>false</code>. 95 * <p/>In a managed profile, location sharing always reflects the primary user's setting, but 96 * can be overridden and forced off by setting this restriction to true in the managed profile. 97 * 98 * <p/>Key for user restrictions. 99 * <p/>Type: Boolean 100 * @see #setUserRestrictions(Bundle) 101 * @see #getUserRestrictions() 102 */ 103 public static final String DISALLOW_SHARE_LOCATION = "no_share_location"; 104 105 /** 106 * Specifies if a user is disallowed from enabling the 107 * "Unknown Sources" setting, that allows installation of apps from unknown sources. 108 * The default value is <code>false</code>. 109 * 110 * <p/>Key for user restrictions. 111 * <p/>Type: Boolean 112 * @see #setUserRestrictions(Bundle) 113 * @see #getUserRestrictions() 114 */ 115 public static final String DISALLOW_INSTALL_UNKNOWN_SOURCES = "no_install_unknown_sources"; 116 117 /** 118 * Specifies if a user is disallowed from configuring bluetooth. 119 * This does <em>not</em> restrict the user from turning bluetooth on or off. 120 * The default value is <code>false</code>. 121 * <p/>This restriction has no effect in a managed profile. 122 * 123 * <p/>Key for user restrictions. 124 * <p/>Type: Boolean 125 * @see #setUserRestrictions(Bundle) 126 * @see #getUserRestrictions() 127 */ 128 public static final String DISALLOW_CONFIG_BLUETOOTH = "no_config_bluetooth"; 129 130 /** 131 * Specifies if a user is disallowed from transferring files over 132 * USB. This can only be set by device owners and profile owners on the primary user. 133 * The default value is <code>false</code>. 134 * 135 * <p/>Key for user restrictions. 136 * <p/>Type: Boolean 137 * @see #setUserRestrictions(Bundle) 138 * @see #getUserRestrictions() 139 */ 140 public static final String DISALLOW_USB_FILE_TRANSFER = "no_usb_file_transfer"; 141 142 /** 143 * Specifies if a user is disallowed from configuring user 144 * credentials. The default value is <code>false</code>. 145 * 146 * <p/>Key for user restrictions. 147 * <p/>Type: Boolean 148 * @see #setUserRestrictions(Bundle) 149 * @see #getUserRestrictions() 150 */ 151 public static final String DISALLOW_CONFIG_CREDENTIALS = "no_config_credentials"; 152 153 /** 154 * When set on the primary user this specifies if the user can remove other users. 155 * When set on a secondary user, this specifies if the user can remove itself. 156 * This restriction has no effect on managed profiles. 157 * The default value is <code>false</code>. 158 * 159 * <p/>Key for user restrictions. 160 * <p/>Type: Boolean 161 * @see #setUserRestrictions(Bundle) 162 * @see #getUserRestrictions() 163 */ 164 public static final String DISALLOW_REMOVE_USER = "no_remove_user"; 165 166 /** 167 * Specifies if a user is disallowed from enabling or 168 * accessing debugging features. The default value is <code>false</code>. 169 * 170 * <p/>Key for user restrictions. 171 * <p/>Type: Boolean 172 * @see #setUserRestrictions(Bundle) 173 * @see #getUserRestrictions() 174 */ 175 public static final String DISALLOW_DEBUGGING_FEATURES = "no_debugging_features"; 176 177 /** 178 * Specifies if a user is disallowed from configuring VPN. 179 * The default value is <code>false</code>. 180 * This restriction has an effect in a managed profile only from 181 * {@link android.os.Build.VERSION_CODES#MNC} 182 * 183 * <p/>Key for user restrictions. 184 * <p/>Type: Boolean 185 * @see #setUserRestrictions(Bundle) 186 * @see #getUserRestrictions() 187 */ 188 public static final String DISALLOW_CONFIG_VPN = "no_config_vpn"; 189 190 /** 191 * Specifies if a user is disallowed from configuring Tethering 192 * & portable hotspots. This can only be set by device owners and profile owners on the 193 * primary user. The default value is <code>false</code>. 194 * 195 * <p/>Key for user restrictions. 196 * <p/>Type: Boolean 197 * @see #setUserRestrictions(Bundle) 198 * @see #getUserRestrictions() 199 */ 200 public static final String DISALLOW_CONFIG_TETHERING = "no_config_tethering"; 201 202 /** 203 * Specifies if a user is disallowed from factory resetting 204 * from Settings. This can only be set by device owners and profile owners on the primary user. 205 * The default value is <code>false</code>. 206 * <p/>This restriction has no effect on secondary users and managed profiles since only the 207 * primary user can factory reset the device. 208 * 209 * <p/>Key for user restrictions. 210 * <p/>Type: Boolean 211 * @see #setUserRestrictions(Bundle) 212 * @see #getUserRestrictions() 213 */ 214 public static final String DISALLOW_FACTORY_RESET = "no_factory_reset"; 215 216 /** 217 * Specifies if a user is disallowed from adding new users and 218 * profiles. This can only be set by device owners and profile owners on the primary user. 219 * The default value is <code>false</code>. 220 * <p/>This restriction has no effect on secondary users and managed profiles since only the 221 * primary user can add other users. 222 * 223 * <p/>Key for user restrictions. 224 * <p/>Type: Boolean 225 * @see #setUserRestrictions(Bundle) 226 * @see #getUserRestrictions() 227 */ 228 public static final String DISALLOW_ADD_USER = "no_add_user"; 229 230 /** 231 * Specifies if a user is disallowed from disabling application 232 * verification. The default value is <code>false</code>. 233 * 234 * <p/>Key for user restrictions. 235 * <p/>Type: Boolean 236 * @see #setUserRestrictions(Bundle) 237 * @see #getUserRestrictions() 238 */ 239 public static final String ENSURE_VERIFY_APPS = "ensure_verify_apps"; 240 241 /** 242 * Specifies if a user is disallowed from configuring cell 243 * broadcasts. This can only be set by device owners and profile owners on the primary user. 244 * The default value is <code>false</code>. 245 * <p/>This restriction has no effect on secondary users and managed profiles since only the 246 * primary user can configure cell broadcasts. 247 * 248 * <p/>Key for user restrictions. 249 * <p/>Type: Boolean 250 * @see #setUserRestrictions(Bundle) 251 * @see #getUserRestrictions() 252 */ 253 public static final String DISALLOW_CONFIG_CELL_BROADCASTS = "no_config_cell_broadcasts"; 254 255 /** 256 * Specifies if a user is disallowed from configuring mobile 257 * networks. This can only be set by device owners and profile owners on the primary user. 258 * The default value is <code>false</code>. 259 * <p/>This restriction has no effect on secondary users and managed profiles since only the 260 * primary user can configure mobile networks. 261 * 262 * <p/>Key for user restrictions. 263 * <p/>Type: Boolean 264 * @see #setUserRestrictions(Bundle) 265 * @see #getUserRestrictions() 266 */ 267 public static final String DISALLOW_CONFIG_MOBILE_NETWORKS = "no_config_mobile_networks"; 268 269 /** 270 * Specifies if a user is disallowed from modifying 271 * applications in Settings or launchers. The following actions will not be allowed when this 272 * restriction is enabled: 273 * <li>uninstalling apps</li> 274 * <li>disabling apps</li> 275 * <li>clearing app caches</li> 276 * <li>clearing app data</li> 277 * <li>force stopping apps</li> 278 * <li>clearing app defaults</li> 279 * <p> 280 * The default value is <code>false</code>. 281 * 282 * <p/>Key for user restrictions. 283 * <p/>Type: Boolean 284 * @see #setUserRestrictions(Bundle) 285 * @see #getUserRestrictions() 286 */ 287 public static final String DISALLOW_APPS_CONTROL = "no_control_apps"; 288 289 /** 290 * Specifies if a user is disallowed from mounting 291 * physical external media. This can only be set by device owners and profile owners on the 292 * primary user. The default value is <code>false</code>. 293 * 294 * <p/>Key for user restrictions. 295 * <p/>Type: Boolean 296 * @see #setUserRestrictions(Bundle) 297 * @see #getUserRestrictions() 298 */ 299 public static final String DISALLOW_MOUNT_PHYSICAL_MEDIA = "no_physical_media"; 300 301 /** 302 * Specifies if a user is disallowed from adjusting microphone 303 * volume. If set, the microphone will be muted. This can only be set by device owners 304 * and profile owners on the primary user. The default value is <code>false</code>. 305 * 306 * <p/>Key for user restrictions. 307 * <p/>Type: Boolean 308 * @see #setUserRestrictions(Bundle) 309 * @see #getUserRestrictions() 310 */ 311 public static final String DISALLOW_UNMUTE_MICROPHONE = "no_unmute_microphone"; 312 313 /** 314 * Specifies if a user is disallowed from adjusting the master 315 * volume. If set, the master volume will be muted. This can only be set by device owners 316 * and profile owners on the primary user. The default value is <code>false</code>. 317 * 318 * <p/>Key for user restrictions. 319 * <p/>Type: Boolean 320 * @see #setUserRestrictions(Bundle) 321 * @see #getUserRestrictions() 322 */ 323 public static final String DISALLOW_ADJUST_VOLUME = "no_adjust_volume"; 324 325 /** 326 * Specifies that the user is not allowed to make outgoing 327 * phone calls. Emergency calls are still permitted. 328 * The default value is <code>false</code>. 329 * <p/>This restriction has no effect on managed profiles since call intents are normally 330 * forwarded to the primary user. 331 * 332 * <p/>Key for user restrictions. 333 * <p/>Type: Boolean 334 * @see #setUserRestrictions(Bundle) 335 * @see #getUserRestrictions() 336 */ 337 public static final String DISALLOW_OUTGOING_CALLS = "no_outgoing_calls"; 338 339 /** 340 * Specifies that the user is not allowed to send or receive 341 * SMS messages. The default value is <code>false</code>. 342 * 343 * <p/>Key for user restrictions. 344 * <p/>Type: Boolean 345 * @see #setUserRestrictions(Bundle) 346 * @see #getUserRestrictions() 347 */ 348 public static final String DISALLOW_SMS = "no_sms"; 349 350 /** 351 * Specifies that windows besides app windows should not be 352 * created. This will block the creation of the following types of windows. 353 * <li>{@link LayoutParams#TYPE_TOAST}</li> 354 * <li>{@link LayoutParams#TYPE_PHONE}</li> 355 * <li>{@link LayoutParams#TYPE_PRIORITY_PHONE}</li> 356 * <li>{@link LayoutParams#TYPE_SYSTEM_ALERT}</li> 357 * <li>{@link LayoutParams#TYPE_SYSTEM_ERROR}</li> 358 * <li>{@link LayoutParams#TYPE_SYSTEM_OVERLAY}</li> 359 * 360 * <p>This can only be set by device owners and profile owners on the primary user. 361 * The default value is <code>false</code>. 362 * 363 * <p/>Key for user restrictions. 364 * <p/>Type: Boolean 365 * @see #setUserRestrictions(Bundle) 366 * @see #getUserRestrictions() 367 */ 368 public static final String DISALLOW_CREATE_WINDOWS = "no_create_windows"; 369 370 /** 371 * Specifies if what is copied in the clipboard of this profile can 372 * be pasted in related profiles. Does not restrict if the clipboard of related profiles can be 373 * pasted in this profile. 374 * The default value is <code>false</code>. 375 * 376 * <p/>Key for user restrictions. 377 * <p/>Type: Boolean 378 * @see #setUserRestrictions(Bundle) 379 * @see #getUserRestrictions() 380 */ 381 public static final String DISALLOW_CROSS_PROFILE_COPY_PASTE = "no_cross_profile_copy_paste"; 382 383 /** 384 * Specifies if the user is not allowed to use NFC to beam out data from apps. 385 * The default value is <code>false</code>. 386 * 387 * <p/>Key for user restrictions. 388 * <p/>Type: Boolean 389 * @see #setUserRestrictions(Bundle) 390 * @see #getUserRestrictions() 391 */ 392 public static final String DISALLOW_OUTGOING_BEAM = "no_outgoing_beam"; 393 394 /** 395 * Hidden user restriction to disallow access to wallpaper manager APIs. This user restriction 396 * is always set for managed profiles. 397 * @hide 398 * @see #setUserRestrictions(Bundle) 399 * @see #getUserRestrictions() 400 */ 401 public static final String DISALLOW_WALLPAPER = "no_wallpaper"; 402 403 /** 404 * Specifies if the user is not allowed to reboot the device into safe boot mode. 405 * This can only be set by device owners and profile owners on the primary user. 406 * The default value is <code>false</code>. 407 * 408 * <p/>Key for user restrictions. 409 * <p/>Type: Boolean 410 * @see #setUserRestrictions(Bundle) 411 * @see #getUserRestrictions() 412 */ 413 public static final String DISALLOW_SAFE_BOOT = "no_safe_boot"; 414 415 /** 416 * Specifies if a user is not allowed to record audio. This restriction is always enabled for 417 * background users. The default value is <code>false</code>. 418 * 419 * @see #setUserRestrictions(Bundle) 420 * @see #getUserRestrictions() 421 * @hide 422 */ 423 public static final String DISALLOW_RECORD_AUDIO = "no_record_audio"; 424 425 /** 426 * Application restriction key that is used to indicate the pending arrival 427 * of real restrictions for the app. 428 * 429 * <p> 430 * Applications that support restrictions should check for the presence of this key. 431 * A <code>true</code> value indicates that restrictions may be applied in the near 432 * future but are not available yet. It is the responsibility of any 433 * management application that sets this flag to update it when the final 434 * restrictions are enforced. 435 * 436 * <p/>Key for application restrictions. 437 * <p/>Type: Boolean 438 * @see android.app.admin.DevicePolicyManager#setApplicationRestrictions( 439 * android.content.ComponentName, String, Bundle) 440 * @see android.app.admin.DevicePolicyManager#getApplicationRestrictions( 441 * android.content.ComponentName, String) 442 */ 443 public static final String KEY_RESTRICTIONS_PENDING = "restrictions_pending"; 444 445 /** @hide */ 446 public static final int PIN_VERIFICATION_FAILED_INCORRECT = -3; 447 /** @hide */ 448 public static final int PIN_VERIFICATION_FAILED_NOT_SET = -2; 449 /** @hide */ 450 public static final int PIN_VERIFICATION_SUCCESS = -1; 451 452 private static UserManager sInstance = null; 453 454 /** @hide */ 455 public synchronized static UserManager get(Context context) { 456 if (sInstance == null) { 457 sInstance = (UserManager) context.getSystemService(Context.USER_SERVICE); 458 } 459 return sInstance; 460 } 461 462 /** @hide */ 463 public UserManager(Context context, IUserManager service) { 464 mService = service; 465 mContext = context; 466 } 467 468 /** 469 * Returns whether the system supports multiple users. 470 * @return true if multiple users can be created by user, false if it is a single user device. 471 * @hide 472 */ 473 public static boolean supportsMultipleUsers() { 474 return getMaxSupportedUsers() > 1 475 && SystemProperties.getBoolean("fw.show_multiuserui", 476 Resources.getSystem().getBoolean(R.bool.config_enableMultiUserUI)); 477 } 478 479 /** 480 * Returns the user handle for the user that this process is running under. 481 * 482 * @return the user handle of this process. 483 * @hide 484 */ 485 public int getUserHandle() { 486 return UserHandle.myUserId(); 487 } 488 489 /** 490 * Returns the user name of the user making this call. This call is only 491 * available to applications on the system image; it requires the 492 * MANAGE_USERS permission. 493 * @return the user name 494 */ 495 public String getUserName() { 496 try { 497 return mService.getUserInfo(getUserHandle()).name; 498 } catch (RemoteException re) { 499 Log.w(TAG, "Could not get user name", re); 500 return ""; 501 } 502 } 503 504 /** 505 * Used to determine whether the user making this call is subject to 506 * teleportations. 507 * 508 * <p>As of {@link android.os.Build.VERSION_CODES#LOLLIPOP}, this method can 509 * now automatically identify goats using advanced goat recognition technology.</p> 510 * 511 * @return Returns true if the user making this call is a goat. 512 */ 513 public boolean isUserAGoat() { 514 return mContext.getPackageManager() 515 .isPackageAvailable("com.coffeestainstudios.goatsimulator"); 516 } 517 518 /** 519 * Used to check if this process is running under the system user. The system user 520 * is the initial user that is implicitly created on first boot and hosts most of the 521 * system services. 522 * 523 * @return whether this process is running under the system user. 524 */ 525 public boolean isSystemUser() { 526 return UserHandle.myUserId() == UserHandle.USER_OWNER; 527 } 528 529 /** 530 * Used to check if the user making this call is linked to another user. Linked users may have 531 * a reduced number of available apps, app restrictions and account restrictions. 532 * @return whether the user making this call is a linked user 533 * @hide 534 */ 535 public boolean isLinkedUser() { 536 try { 537 return mService.isRestricted(); 538 } catch (RemoteException re) { 539 Log.w(TAG, "Could not check if user is limited ", re); 540 return false; 541 } 542 } 543 544 /** 545 * Checks if the calling app is running as a guest user. 546 * @return whether the caller is a guest user. 547 * @hide 548 */ 549 public boolean isGuestUser() { 550 UserInfo user = getUserInfo(UserHandle.myUserId()); 551 return user != null ? user.isGuest() : false; 552 } 553 554 /** 555 * Checks if the calling app is running in a managed profile. 556 * Requires {@link android.Manifest.permission#MANAGE_USERS} permission. 557 * 558 * @return whether the caller is in a managed profile. 559 * @hide 560 */ 561 @SystemApi 562 public boolean isManagedProfile() { 563 UserInfo user = getUserInfo(UserHandle.myUserId()); 564 return user != null ? user.isManagedProfile() : false; 565 } 566 567 /** 568 * Return whether the given user is actively running. This means that 569 * the user is in the "started" state, not "stopped" -- it is currently 570 * allowed to run code through scheduled alarms, receiving broadcasts, 571 * etc. A started user may be either the current foreground user or a 572 * background user; the result here does not distinguish between the two. 573 * @param user The user to retrieve the running state for. 574 */ 575 public boolean isUserRunning(UserHandle user) { 576 try { 577 return ActivityManagerNative.getDefault().isUserRunning( 578 user.getIdentifier(), false); 579 } catch (RemoteException e) { 580 return false; 581 } 582 } 583 584 /** 585 * Return whether the given user is actively running <em>or</em> stopping. 586 * This is like {@link #isUserRunning(UserHandle)}, but will also return 587 * true if the user had been running but is in the process of being stopped 588 * (but is not yet fully stopped, and still running some code). 589 * @param user The user to retrieve the running state for. 590 */ 591 public boolean isUserRunningOrStopping(UserHandle user) { 592 try { 593 return ActivityManagerNative.getDefault().isUserRunning( 594 user.getIdentifier(), true); 595 } catch (RemoteException e) { 596 return false; 597 } 598 } 599 600 /** 601 * Returns the UserInfo object describing a specific user. 602 * Requires {@link android.Manifest.permission#MANAGE_USERS} permission. 603 * @param userHandle the user handle of the user whose information is being requested. 604 * @return the UserInfo object for a specific user. 605 * @hide 606 */ 607 public UserInfo getUserInfo(int userHandle) { 608 try { 609 return mService.getUserInfo(userHandle); 610 } catch (RemoteException re) { 611 Log.w(TAG, "Could not get user info", re); 612 return null; 613 } 614 } 615 616 /** 617 * Returns the user-wide restrictions imposed on this user. 618 * @return a Bundle containing all the restrictions. 619 */ 620 public Bundle getUserRestrictions() { 621 return getUserRestrictions(Process.myUserHandle()); 622 } 623 624 /** 625 * Returns the user-wide restrictions imposed on the user specified by <code>userHandle</code>. 626 * @param userHandle the UserHandle of the user for whom to retrieve the restrictions. 627 * @return a Bundle containing all the restrictions. 628 */ 629 public Bundle getUserRestrictions(UserHandle userHandle) { 630 try { 631 return mService.getUserRestrictions(userHandle.getIdentifier()); 632 } catch (RemoteException re) { 633 Log.w(TAG, "Could not get user restrictions", re); 634 return Bundle.EMPTY; 635 } 636 } 637 638 /** 639 * Sets all the user-wide restrictions for this user. 640 * Requires the MANAGE_USERS permission. 641 * @param restrictions the Bundle containing all the restrictions. 642 * @deprecated use {@link android.app.admin.DevicePolicyManager#addUserRestriction( 643 * android.content.ComponentName, String)} or 644 * {@link android.app.admin.DevicePolicyManager#clearUserRestriction( 645 * android.content.ComponentName, String)} instead. 646 */ 647 @Deprecated 648 public void setUserRestrictions(Bundle restrictions) { 649 setUserRestrictions(restrictions, Process.myUserHandle()); 650 } 651 652 /** 653 * Sets all the user-wide restrictions for the specified user. 654 * Requires the MANAGE_USERS permission. 655 * @param restrictions the Bundle containing all the restrictions. 656 * @param userHandle the UserHandle of the user for whom to set the restrictions. 657 * @deprecated use {@link android.app.admin.DevicePolicyManager#addUserRestriction( 658 * android.content.ComponentName, String)} or 659 * {@link android.app.admin.DevicePolicyManager#clearUserRestriction( 660 * android.content.ComponentName, String)} instead. 661 */ 662 @Deprecated 663 public void setUserRestrictions(Bundle restrictions, UserHandle userHandle) { 664 try { 665 mService.setUserRestrictions(restrictions, userHandle.getIdentifier()); 666 } catch (RemoteException re) { 667 Log.w(TAG, "Could not set user restrictions", re); 668 } 669 } 670 671 /** 672 * Sets the value of a specific restriction. 673 * Requires the MANAGE_USERS permission. 674 * @param key the key of the restriction 675 * @param value the value for the restriction 676 * @deprecated use {@link android.app.admin.DevicePolicyManager#addUserRestriction( 677 * android.content.ComponentName, String)} or 678 * {@link android.app.admin.DevicePolicyManager#clearUserRestriction( 679 * android.content.ComponentName, String)} instead. 680 */ 681 @Deprecated 682 public void setUserRestriction(String key, boolean value) { 683 Bundle bundle = getUserRestrictions(); 684 bundle.putBoolean(key, value); 685 setUserRestrictions(bundle); 686 } 687 688 /** 689 * @hide 690 * Sets the value of a specific restriction on a specific user. 691 * Requires the MANAGE_USERS permission. 692 * @param key the key of the restriction 693 * @param value the value for the restriction 694 * @param userHandle the user whose restriction is to be changed. 695 * @deprecated use {@link android.app.admin.DevicePolicyManager#addUserRestriction( 696 * android.content.ComponentName, String)} or 697 * {@link android.app.admin.DevicePolicyManager#clearUserRestriction( 698 * android.content.ComponentName, String)} instead. 699 */ 700 @Deprecated 701 public void setUserRestriction(String key, boolean value, UserHandle userHandle) { 702 try { 703 mService.setUserRestriction(key, value, userHandle.getIdentifier()); 704 } catch (RemoteException re) { 705 Log.w(TAG, "Could not set user restriction", re); 706 } 707 } 708 709 /** 710 * Returns whether the current user has been disallowed from performing certain actions 711 * or setting certain settings. 712 * 713 * @param restrictionKey The string key representing the restriction. 714 * @return {@code true} if the current user has the given restriction, {@code false} otherwise. 715 */ 716 public boolean hasUserRestriction(String restrictionKey) { 717 return hasUserRestriction(restrictionKey, Process.myUserHandle()); 718 } 719 720 /** 721 * @hide 722 * Returns whether the given user has been disallowed from performing certain actions 723 * or setting certain settings. 724 * @param restrictionKey the string key representing the restriction 725 * @param userHandle the UserHandle of the user for whom to retrieve the restrictions. 726 */ 727 public boolean hasUserRestriction(String restrictionKey, UserHandle userHandle) { 728 try { 729 return mService.hasUserRestriction(restrictionKey, 730 userHandle.getIdentifier()); 731 } catch (RemoteException re) { 732 Log.w(TAG, "Could not check user restrictions", re); 733 return false; 734 } 735 } 736 737 /** 738 * Return the serial number for a user. This is a device-unique 739 * number assigned to that user; if the user is deleted and then a new 740 * user created, the new users will not be given the same serial number. 741 * @param user The user whose serial number is to be retrieved. 742 * @return The serial number of the given user; returns -1 if the 743 * given UserHandle does not exist. 744 * @see #getUserForSerialNumber(long) 745 */ 746 public long getSerialNumberForUser(UserHandle user) { 747 return getUserSerialNumber(user.getIdentifier()); 748 } 749 750 /** 751 * Return the user associated with a serial number previously 752 * returned by {@link #getSerialNumberForUser(UserHandle)}. 753 * @param serialNumber The serial number of the user that is being 754 * retrieved. 755 * @return Return the user associated with the serial number, or null 756 * if there is not one. 757 * @see #getSerialNumberForUser(UserHandle) 758 */ 759 public UserHandle getUserForSerialNumber(long serialNumber) { 760 int ident = getUserHandle((int) serialNumber); 761 return ident >= 0 ? new UserHandle(ident) : null; 762 } 763 764 /** 765 * Creates a user with the specified name and options. 766 * Requires {@link android.Manifest.permission#MANAGE_USERS} permission. 767 * 768 * @param name the user's name 769 * @param flags flags that identify the type of user and other properties. 770 * @see UserInfo 771 * 772 * @return the UserInfo object for the created user, or null if the user could not be created. 773 * @hide 774 */ 775 public UserInfo createUser(String name, int flags) { 776 try { 777 return mService.createUser(name, flags); 778 } catch (RemoteException re) { 779 Log.w(TAG, "Could not create a user", re); 780 return null; 781 } 782 } 783 784 /** 785 * Creates a guest user and configures it. 786 * @param context an application context 787 * @param name the name to set for the user 788 * @hide 789 */ 790 public UserInfo createGuest(Context context, String name) { 791 UserInfo guest = createUser(name, UserInfo.FLAG_GUEST); 792 if (guest != null) { 793 Settings.Secure.putStringForUser(context.getContentResolver(), 794 Settings.Secure.SKIP_FIRST_USE_HINTS, "1", guest.id); 795 try { 796 Bundle guestRestrictions = mService.getDefaultGuestRestrictions(); 797 guestRestrictions.putBoolean(DISALLOW_SMS, true); 798 guestRestrictions.putBoolean(DISALLOW_INSTALL_UNKNOWN_SOURCES, true); 799 mService.setUserRestrictions(guestRestrictions, guest.id); 800 } catch (RemoteException re) { 801 Log.w(TAG, "Could not update guest restrictions"); 802 } 803 } 804 return guest; 805 } 806 807 /** 808 * Creates a secondary user with the specified name and options and configures it with default 809 * restrictions. 810 * Requires {@link android.Manifest.permission#MANAGE_USERS} permission. 811 * 812 * @param name the user's name 813 * @param flags flags that identify the type of user and other properties. 814 * @see UserInfo 815 * 816 * @return the UserInfo object for the created user, or null if the user could not be created. 817 * @hide 818 */ 819 public UserInfo createSecondaryUser(String name, int flags) { 820 try { 821 UserInfo user = mService.createUser(name, flags); 822 if (user == null) { 823 return null; 824 } 825 Bundle userRestrictions = mService.getUserRestrictions(user.id); 826 addDefaultUserRestrictions(userRestrictions); 827 mService.setUserRestrictions(userRestrictions, user.id); 828 return user; 829 } catch (RemoteException re) { 830 Log.w(TAG, "Could not create a user", re); 831 return null; 832 } 833 } 834 835 private static void addDefaultUserRestrictions(Bundle restrictions) { 836 restrictions.putBoolean(DISALLOW_OUTGOING_CALLS, true); 837 restrictions.putBoolean(DISALLOW_SMS, true); 838 } 839 840 /** 841 * Creates a user with the specified name and options as a profile of another user. 842 * Requires {@link android.Manifest.permission#MANAGE_USERS} permission. 843 * 844 * @param name the user's name 845 * @param flags flags that identify the type of user and other properties. 846 * @see UserInfo 847 * @param userHandle new user will be a profile of this use. 848 * 849 * @return the UserInfo object for the created user, or null if the user could not be created. 850 * @hide 851 */ 852 public UserInfo createProfileForUser(String name, int flags, int userHandle) { 853 try { 854 return mService.createProfileForUser(name, flags, userHandle); 855 } catch (RemoteException re) { 856 Log.w(TAG, "Could not create a user", re); 857 return null; 858 } 859 } 860 861 /** 862 * @hide 863 * Marks the guest user for deletion to allow a new guest to be created before deleting 864 * the current user who is a guest. 865 * @param userHandle 866 * @return 867 */ 868 public boolean markGuestForDeletion(int userHandle) { 869 try { 870 return mService.markGuestForDeletion(userHandle); 871 } catch (RemoteException re) { 872 Log.w(TAG, "Could not mark guest for deletion", re); 873 return false; 874 } 875 } 876 877 /** 878 * Sets the user as enabled, if such an user exists. 879 * Requires {@link android.Manifest.permission#MANAGE_USERS} permission. 880 * Note that the default is true, it's only that managed profiles might not be enabled. 881 * 882 * @param userHandle the id of the profile to enable 883 * @hide 884 */ 885 public void setUserEnabled(int userHandle) { 886 try { 887 mService.setUserEnabled(userHandle); 888 } catch (RemoteException e) { 889 Log.w(TAG, "Could not enable the profile", e); 890 } 891 } 892 893 /** 894 * Return the number of users currently created on the device. 895 */ 896 public int getUserCount() { 897 List<UserInfo> users = getUsers(); 898 return users != null ? users.size() : 1; 899 } 900 901 /** 902 * Returns information for all users on this device. 903 * Requires {@link android.Manifest.permission#MANAGE_USERS} permission. 904 * @return the list of users that were created. 905 * @hide 906 */ 907 public List<UserInfo> getUsers() { 908 try { 909 return mService.getUsers(false); 910 } catch (RemoteException re) { 911 Log.w(TAG, "Could not get user list", re); 912 return null; 913 } 914 } 915 916 /** 917 * Checks whether it's possible to add more users. Caller must hold the MANAGE_USERS 918 * permission. 919 * 920 * @return true if more users can be added, false if limit has been reached. 921 * @hide 922 */ 923 public boolean canAddMoreUsers() { 924 final List<UserInfo> users = getUsers(true); 925 final int totalUserCount = users.size(); 926 int aliveUserCount = 0; 927 for (int i = 0; i < totalUserCount; i++) { 928 UserInfo user = users.get(i); 929 if (!user.isGuest()) { 930 aliveUserCount++; 931 } 932 } 933 return aliveUserCount < getMaxSupportedUsers(); 934 } 935 936 /** 937 * Returns list of the profiles of userHandle including 938 * userHandle itself. 939 * Note that this returns both enabled and not enabled profiles. See 940 * {@link #getEnabledProfiles(int)} if you need only the enabled ones. 941 * 942 * Requires {@link android.Manifest.permission#MANAGE_USERS} permission. 943 * @param userHandle profiles of this user will be returned. 944 * @return the list of profiles. 945 * @hide 946 */ 947 public List<UserInfo> getProfiles(int userHandle) { 948 try { 949 return mService.getProfiles(userHandle, false /* enabledOnly */); 950 } catch (RemoteException re) { 951 Log.w(TAG, "Could not get user list", re); 952 return null; 953 } 954 } 955 956 /** 957 * Returns list of the profiles of userHandle including 958 * userHandle itself. 959 * Note that this returns only enabled. 960 * 961 * Requires {@link android.Manifest.permission#MANAGE_USERS} permission. 962 * @param userHandle profiles of this user will be returned. 963 * @return the list of profiles. 964 * @hide 965 */ 966 public List<UserInfo> getEnabledProfiles(int userHandle) { 967 try { 968 return mService.getProfiles(userHandle, true /* enabledOnly */); 969 } catch (RemoteException re) { 970 Log.w(TAG, "Could not get user list", re); 971 return null; 972 } 973 } 974 975 /** 976 * Returns a list of UserHandles for profiles associated with the user that the calling process 977 * is running on, including the user itself. 978 * 979 * @return A non-empty list of UserHandles associated with the calling user. 980 */ 981 public List<UserHandle> getUserProfiles() { 982 ArrayList<UserHandle> profiles = new ArrayList<UserHandle>(); 983 List<UserInfo> users = new ArrayList<UserInfo>(); 984 try { 985 users = mService.getProfiles(UserHandle.myUserId(), true /* enabledOnly */); 986 } catch (RemoteException re) { 987 Log.w(TAG, "Could not get user list", re); 988 return null; 989 } 990 for (UserInfo info : users) { 991 UserHandle userHandle = new UserHandle(info.id); 992 profiles.add(userHandle); 993 } 994 return profiles; 995 } 996 997 /** 998 * Returns the parent of the profile which this method is called from 999 * or null if called from a user that is not a profile. 1000 * 1001 * @hide 1002 */ 1003 public UserInfo getProfileParent(int userHandle) { 1004 try { 1005 return mService.getProfileParent(userHandle); 1006 } catch (RemoteException re) { 1007 Log.w(TAG, "Could not get profile parent", re); 1008 return null; 1009 } 1010 } 1011 1012 /** 1013 * If the target user is a managed profile of the calling user or the caller 1014 * is itself a managed profile, then this returns a badged copy of the given 1015 * icon to be able to distinguish it from the original icon. For badging an 1016 * arbitrary drawable use {@link #getBadgedDrawableForUser( 1017 * android.graphics.drawable.Drawable, UserHandle, android.graphics.Rect, int)}. 1018 * <p> 1019 * If the original drawable is a BitmapDrawable and the backing bitmap is 1020 * mutable as per {@link android.graphics.Bitmap#isMutable()}, the bading 1021 * is performed in place and the original drawable is returned. 1022 * </p> 1023 * 1024 * @param icon The icon to badge. 1025 * @param user The target user. 1026 * @return A drawable that combines the original icon and a badge as 1027 * determined by the system. 1028 * @removed 1029 */ 1030 public Drawable getBadgedIconForUser(Drawable icon, UserHandle user) { 1031 return mContext.getPackageManager().getUserBadgedIcon(icon, user); 1032 } 1033 1034 /** 1035 * If the target user is a managed profile of the calling user or the caller 1036 * is itself a managed profile, then this returns a badged copy of the given 1037 * drawable allowing the user to distinguish it from the original drawable. 1038 * The caller can specify the location in the bounds of the drawable to be 1039 * badged where the badge should be applied as well as the density of the 1040 * badge to be used. 1041 * <p> 1042 * If the original drawable is a BitmapDrawable and the backing bitmap is 1043 * mutable as per {@link android.graphics.Bitmap#isMutable()}, the bading 1044 * is performed in place and the original drawable is returned. 1045 * </p> 1046 * 1047 * @param badgedDrawable The drawable to badge. 1048 * @param user The target user. 1049 * @param badgeLocation Where in the bounds of the badged drawable to place 1050 * the badge. If not provided, the badge is applied on top of the entire 1051 * drawable being badged. 1052 * @param badgeDensity The optional desired density for the badge as per 1053 * {@link android.util.DisplayMetrics#densityDpi}. If not provided, 1054 * the density of the display is used. 1055 * @return A drawable that combines the original drawable and a badge as 1056 * determined by the system. 1057 * @removed 1058 */ 1059 public Drawable getBadgedDrawableForUser(Drawable badgedDrawable, UserHandle user, 1060 Rect badgeLocation, int badgeDensity) { 1061 return mContext.getPackageManager().getUserBadgedDrawableForDensity(badgedDrawable, user, 1062 badgeLocation, badgeDensity); 1063 } 1064 1065 /** 1066 * If the target user is a managed profile of the calling user or the caller 1067 * is itself a managed profile, then this returns a copy of the label with 1068 * badging for accessibility services like talkback. E.g. passing in "Email" 1069 * and it might return "Work Email" for Email in the work profile. 1070 * 1071 * @param label The label to change. 1072 * @param user The target user. 1073 * @return A label that combines the original label and a badge as 1074 * determined by the system. 1075 * @removed 1076 */ 1077 public CharSequence getBadgedLabelForUser(CharSequence label, UserHandle user) { 1078 return mContext.getPackageManager().getUserBadgedLabel(label, user); 1079 } 1080 1081 /** 1082 * Returns information for all users on this device. Requires 1083 * {@link android.Manifest.permission#MANAGE_USERS} permission. 1084 * 1085 * @param excludeDying specify if the list should exclude users being 1086 * removed. 1087 * @return the list of users that were created. 1088 * @hide 1089 */ 1090 public List<UserInfo> getUsers(boolean excludeDying) { 1091 try { 1092 return mService.getUsers(excludeDying); 1093 } catch (RemoteException re) { 1094 Log.w(TAG, "Could not get user list", re); 1095 return null; 1096 } 1097 } 1098 1099 /** 1100 * Removes a user and all associated data. 1101 * Requires {@link android.Manifest.permission#MANAGE_USERS} permission. 1102 * @param userHandle the integer handle of the user, where 0 is the primary user. 1103 * @hide 1104 */ 1105 public boolean removeUser(int userHandle) { 1106 try { 1107 return mService.removeUser(userHandle); 1108 } catch (RemoteException re) { 1109 Log.w(TAG, "Could not remove user ", re); 1110 return false; 1111 } 1112 } 1113 1114 /** 1115 * Updates the user's name. 1116 * Requires {@link android.Manifest.permission#MANAGE_USERS} permission. 1117 * 1118 * @param userHandle the user's integer handle 1119 * @param name the new name for the user 1120 * @hide 1121 */ 1122 public void setUserName(int userHandle, String name) { 1123 try { 1124 mService.setUserName(userHandle, name); 1125 } catch (RemoteException re) { 1126 Log.w(TAG, "Could not set the user name ", re); 1127 } 1128 } 1129 1130 /** 1131 * Sets the user's photo. 1132 * @param userHandle the user for whom to change the photo. 1133 * @param icon the bitmap to set as the photo. 1134 * @hide 1135 */ 1136 public void setUserIcon(int userHandle, Bitmap icon) { 1137 try { 1138 mService.setUserIcon(userHandle, icon); 1139 } catch (RemoteException re) { 1140 Log.w(TAG, "Could not set the user icon ", re); 1141 } 1142 } 1143 1144 /** 1145 * Returns a file descriptor for the user's photo. PNG data can be read from this file. 1146 * @param userHandle the user whose photo we want to read. 1147 * @return a {@link Bitmap} of the user's photo, or null if there's no photo. 1148 * @see com.android.internal.util.UserIcons#getDefaultUserIcon for a default. 1149 * @hide 1150 */ 1151 public Bitmap getUserIcon(int userHandle) { 1152 try { 1153 ParcelFileDescriptor fd = mService.getUserIcon(userHandle); 1154 if (fd != null) { 1155 try { 1156 return BitmapFactory.decodeFileDescriptor(fd.getFileDescriptor()); 1157 } finally { 1158 try { 1159 fd.close(); 1160 } catch (IOException e) { 1161 } 1162 } 1163 } 1164 } catch (RemoteException re) { 1165 Log.w(TAG, "Could not get the user icon ", re); 1166 } 1167 return null; 1168 } 1169 1170 /** 1171 * Returns the maximum number of users that can be created on this device. A return value 1172 * of 1 means that it is a single user device. 1173 * @hide 1174 * @return a value greater than or equal to 1 1175 */ 1176 public static int getMaxSupportedUsers() { 1177 // Don't allow multiple users on certain builds 1178 if (android.os.Build.ID.startsWith("JVP")) return 1; 1179 // Svelte devices don't get multi-user. 1180 if (ActivityManager.isLowRamDeviceStatic()) return 1; 1181 return SystemProperties.getInt("fw.max_users", 1182 Resources.getSystem().getInteger(R.integer.config_multiuserMaximumUsers)); 1183 } 1184 1185 /** 1186 * Returns true if the user switcher should be shown, this will be if there 1187 * are multiple users that aren't managed profiles. 1188 * @hide 1189 * @return true if user switcher should be shown. 1190 */ 1191 public boolean isUserSwitcherEnabled() { 1192 List<UserInfo> users = getUsers(true); 1193 if (users == null) { 1194 return false; 1195 } 1196 int switchableUserCount = 0; 1197 for (UserInfo user : users) { 1198 if (user.supportsSwitchTo()) { 1199 ++switchableUserCount; 1200 } 1201 } 1202 final boolean guestEnabled = Settings.Global.getInt(mContext.getContentResolver(), 1203 Settings.Global.GUEST_USER_ENABLED, 0) == 1; 1204 return switchableUserCount > 1 || guestEnabled; 1205 } 1206 1207 /** 1208 * Returns a serial number on this device for a given userHandle. User handles can be recycled 1209 * when deleting and creating users, but serial numbers are not reused until the device is wiped. 1210 * @param userHandle 1211 * @return a serial number associated with that user, or -1 if the userHandle is not valid. 1212 * @hide 1213 */ 1214 public int getUserSerialNumber(int userHandle) { 1215 try { 1216 return mService.getUserSerialNumber(userHandle); 1217 } catch (RemoteException re) { 1218 Log.w(TAG, "Could not get serial number for user " + userHandle); 1219 } 1220 return -1; 1221 } 1222 1223 /** 1224 * Returns a userHandle on this device for a given user serial number. User handles can be 1225 * recycled when deleting and creating users, but serial numbers are not reused until the device 1226 * is wiped. 1227 * @param userSerialNumber 1228 * @return the userHandle associated with that user serial number, or -1 if the serial number 1229 * is not valid. 1230 * @hide 1231 */ 1232 public int getUserHandle(int userSerialNumber) { 1233 try { 1234 return mService.getUserHandle(userSerialNumber); 1235 } catch (RemoteException re) { 1236 Log.w(TAG, "Could not get userHandle for user " + userSerialNumber); 1237 } 1238 return -1; 1239 } 1240 1241 /** 1242 * Returns a Bundle containing any saved application restrictions for this user, for the 1243 * given package name. Only an application with this package name can call this method. 1244 * @param packageName the package name of the calling application 1245 * @return a Bundle with the restrictions as key/value pairs, or null if there are no 1246 * saved restrictions. The values can be of type Boolean, String or String[], depending 1247 * on the restriction type, as defined by the application. 1248 */ 1249 public Bundle getApplicationRestrictions(String packageName) { 1250 try { 1251 return mService.getApplicationRestrictions(packageName); 1252 } catch (RemoteException re) { 1253 Log.w(TAG, "Could not get application restrictions for package " + packageName); 1254 } 1255 return null; 1256 } 1257 1258 /** 1259 * @hide 1260 */ 1261 public Bundle getApplicationRestrictions(String packageName, UserHandle user) { 1262 try { 1263 return mService.getApplicationRestrictionsForUser(packageName, user.getIdentifier()); 1264 } catch (RemoteException re) { 1265 Log.w(TAG, "Could not get application restrictions for user " + user.getIdentifier()); 1266 } 1267 return null; 1268 } 1269 1270 /** 1271 * @hide 1272 */ 1273 public void setApplicationRestrictions(String packageName, Bundle restrictions, 1274 UserHandle user) { 1275 try { 1276 mService.setApplicationRestrictions(packageName, restrictions, user.getIdentifier()); 1277 } catch (RemoteException re) { 1278 Log.w(TAG, "Could not set application restrictions for user " + user.getIdentifier()); 1279 } 1280 } 1281 1282 /** 1283 * Sets a new challenge PIN for restrictions. This is only for use by pre-installed 1284 * apps and requires the MANAGE_USERS permission. 1285 * @param newPin the PIN to use for challenge dialogs. 1286 * @return Returns true if the challenge PIN was set successfully. 1287 * @deprecated The restrictions PIN functionality is no longer provided by the system. 1288 * This method is preserved for backwards compatibility reasons and always returns false. 1289 */ 1290 public boolean setRestrictionsChallenge(String newPin) { 1291 return false; 1292 } 1293 1294 /** @hide */ 1295 public void removeRestrictions() { 1296 try { 1297 mService.removeRestrictions(); 1298 } catch (RemoteException re) { 1299 Log.w(TAG, "Could not change restrictions pin"); 1300 } 1301 } 1302 1303 /** 1304 * @hide 1305 * Set restrictions that should apply to any future guest user that's created. 1306 */ 1307 public void setDefaultGuestRestrictions(Bundle restrictions) { 1308 try { 1309 mService.setDefaultGuestRestrictions(restrictions); 1310 } catch (RemoteException re) { 1311 Log.w(TAG, "Could not set guest restrictions"); 1312 } 1313 } 1314 1315 /** 1316 * @hide 1317 * Gets the default guest restrictions. 1318 */ 1319 public Bundle getDefaultGuestRestrictions() { 1320 try { 1321 return mService.getDefaultGuestRestrictions(); 1322 } catch (RemoteException re) { 1323 Log.w(TAG, "Could not set guest restrictions"); 1324 } 1325 return new Bundle(); 1326 } 1327 1328 /** 1329 * Returns creation time of the user or of a managed profile associated with the calling user. 1330 * @param userHandle user handle of the user or a managed profile associated with the 1331 * calling user. 1332 * @return creation time in milliseconds since Epoch time. 1333 */ 1334 public long getUserCreationTime(UserHandle userHandle) { 1335 try { 1336 return mService.getUserCreationTime(userHandle.getIdentifier()); 1337 } catch (RemoteException re) { 1338 Log.w(TAG, "Could not get user creation time", re); 1339 return 0; 1340 } 1341 } 1342} 1343