UserManager.java revision 5b0e5207e1b2de9fd533fa444b8d9a0acadd1513
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.Manifest; 19import android.accounts.AccountManager; 20import android.annotation.Nullable; 21import android.annotation.RequiresPermission; 22import android.annotation.SystemApi; 23import android.app.ActivityManager; 24import android.app.ActivityManagerNative; 25import android.app.admin.DevicePolicyManager; 26import android.content.ComponentName; 27import android.content.Context; 28import android.content.pm.UserInfo; 29import android.content.res.Resources; 30import android.graphics.Bitmap; 31import android.graphics.BitmapFactory; 32import android.graphics.Rect; 33import android.graphics.drawable.Drawable; 34import android.os.storage.StorageManager; 35import android.provider.Settings; 36import android.util.Log; 37import android.view.WindowManager.LayoutParams; 38 39import com.android.internal.R; 40 41import java.io.IOException; 42import java.util.ArrayList; 43import java.util.List; 44 45/** 46 * Manages users and user details on a multi-user system. 47 */ 48public class UserManager { 49 50 private static String TAG = "UserManager"; 51 private final IUserManager mService; 52 private final Context mContext; 53 54 /** 55 * Specifies if a user is disallowed from adding and removing accounts, unless they are 56 * {@link android.accounts.AccountManager#addAccountExplicitly programmatically} added by 57 * Authenticator. 58 * The default value is <code>false</code>. 59 * 60 * <p/>Key for user restrictions. 61 * <p/>Type: Boolean 62 * @see DevicePolicyManager#addUserRestriction(ComponentName, String) 63 * @see DevicePolicyManager#clearUserRestriction(ComponentName, String) 64 * @see #getUserRestrictions() 65 */ 66 public static final String DISALLOW_MODIFY_ACCOUNTS = "no_modify_accounts"; 67 68 /** 69 * Specifies if a user is disallowed from changing Wi-Fi 70 * access points. The default value is <code>false</code>. 71 * <p/>This restriction has no effect in a managed profile. 72 * 73 * <p/>Key for user restrictions. 74 * <p/>Type: Boolean 75 * @see DevicePolicyManager#addUserRestriction(ComponentName, String) 76 * @see DevicePolicyManager#clearUserRestriction(ComponentName, String) 77 * @see #getUserRestrictions() 78 */ 79 public static final String DISALLOW_CONFIG_WIFI = "no_config_wifi"; 80 81 /** 82 * Specifies if a user is disallowed from installing applications. 83 * The default value is <code>false</code>. 84 * 85 * <p/>Key for user restrictions. 86 * <p/>Type: Boolean 87 * @see DevicePolicyManager#addUserRestriction(ComponentName, String) 88 * @see DevicePolicyManager#clearUserRestriction(ComponentName, String) 89 * @see #getUserRestrictions() 90 */ 91 public static final String DISALLOW_INSTALL_APPS = "no_install_apps"; 92 93 /** 94 * Specifies if a user is disallowed from uninstalling applications. 95 * The default value is <code>false</code>. 96 * 97 * <p/>Key for user restrictions. 98 * <p/>Type: Boolean 99 * @see DevicePolicyManager#addUserRestriction(ComponentName, String) 100 * @see DevicePolicyManager#clearUserRestriction(ComponentName, String) 101 * @see #getUserRestrictions() 102 */ 103 public static final String DISALLOW_UNINSTALL_APPS = "no_uninstall_apps"; 104 105 /** 106 * Specifies if a user is disallowed from turning on location sharing. 107 * The default value is <code>false</code>. 108 * <p/>In a managed profile, location sharing always reflects the primary user's setting, but 109 * can be overridden and forced off by setting this restriction to true in the managed profile. 110 * 111 * <p/>Key for user restrictions. 112 * <p/>Type: Boolean 113 * @see DevicePolicyManager#addUserRestriction(ComponentName, String) 114 * @see DevicePolicyManager#clearUserRestriction(ComponentName, String) 115 * @see #getUserRestrictions() 116 */ 117 public static final String DISALLOW_SHARE_LOCATION = "no_share_location"; 118 119 /** 120 * Specifies if a user is disallowed from enabling the 121 * "Unknown Sources" setting, that allows installation of apps from unknown sources. 122 * The default value is <code>false</code>. 123 * 124 * <p/>Key for user restrictions. 125 * <p/>Type: Boolean 126 * @see DevicePolicyManager#addUserRestriction(ComponentName, String) 127 * @see DevicePolicyManager#clearUserRestriction(ComponentName, String) 128 * @see #getUserRestrictions() 129 */ 130 public static final String DISALLOW_INSTALL_UNKNOWN_SOURCES = "no_install_unknown_sources"; 131 132 /** 133 * Specifies if a user is disallowed from configuring bluetooth. 134 * This does <em>not</em> restrict the user from turning bluetooth on or off. 135 * The default value is <code>false</code>. 136 * <p/>This restriction has no effect in a managed profile. 137 * 138 * <p/>Key for user restrictions. 139 * <p/>Type: Boolean 140 * @see DevicePolicyManager#addUserRestriction(ComponentName, String) 141 * @see DevicePolicyManager#clearUserRestriction(ComponentName, String) 142 * @see #getUserRestrictions() 143 */ 144 public static final String DISALLOW_CONFIG_BLUETOOTH = "no_config_bluetooth"; 145 146 /** 147 * Specifies if a user is disallowed from transferring files over 148 * USB. This can only be set by device owners and profile owners on the primary user. 149 * The default value is <code>false</code>. 150 * 151 * <p/>Key for user restrictions. 152 * <p/>Type: Boolean 153 * @see DevicePolicyManager#addUserRestriction(ComponentName, String) 154 * @see DevicePolicyManager#clearUserRestriction(ComponentName, String) 155 * @see #getUserRestrictions() 156 */ 157 public static final String DISALLOW_USB_FILE_TRANSFER = "no_usb_file_transfer"; 158 159 /** 160 * Specifies if a user is disallowed from configuring user 161 * credentials. The default value is <code>false</code>. 162 * 163 * <p/>Key for user restrictions. 164 * <p/>Type: Boolean 165 * @see DevicePolicyManager#addUserRestriction(ComponentName, String) 166 * @see DevicePolicyManager#clearUserRestriction(ComponentName, String) 167 * @see #getUserRestrictions() 168 */ 169 public static final String DISALLOW_CONFIG_CREDENTIALS = "no_config_credentials"; 170 171 /** 172 * When set on the primary user this specifies if the user can remove other users. 173 * When set on a secondary user, this specifies if the user can remove itself. 174 * This restriction has no effect on managed profiles. 175 * The default value is <code>false</code>. 176 * 177 * <p/>Key for user restrictions. 178 * <p/>Type: Boolean 179 * @see DevicePolicyManager#addUserRestriction(ComponentName, String) 180 * @see DevicePolicyManager#clearUserRestriction(ComponentName, String) 181 * @see #getUserRestrictions() 182 */ 183 public static final String DISALLOW_REMOVE_USER = "no_remove_user"; 184 185 /** 186 * Specifies if a user is disallowed from enabling or 187 * accessing debugging features. The default value is <code>false</code>. 188 * 189 * <p/>Key for user restrictions. 190 * <p/>Type: Boolean 191 * @see DevicePolicyManager#addUserRestriction(ComponentName, String) 192 * @see DevicePolicyManager#clearUserRestriction(ComponentName, String) 193 * @see #getUserRestrictions() 194 */ 195 public static final String DISALLOW_DEBUGGING_FEATURES = "no_debugging_features"; 196 197 /** 198 * Specifies if a user is disallowed from configuring VPN. 199 * The default value is <code>false</code>. 200 * This restriction has an effect in a managed profile only from 201 * {@link android.os.Build.VERSION_CODES#M} 202 * 203 * <p/>Key for user restrictions. 204 * <p/>Type: Boolean 205 * @see DevicePolicyManager#addUserRestriction(ComponentName, String) 206 * @see DevicePolicyManager#clearUserRestriction(ComponentName, String) 207 * @see #getUserRestrictions() 208 */ 209 public static final String DISALLOW_CONFIG_VPN = "no_config_vpn"; 210 211 /** 212 * Specifies if a user is disallowed from configuring Tethering 213 * & portable hotspots. This can only be set by device owners and profile owners on the 214 * primary user. The default value is <code>false</code>. 215 * 216 * <p/>Key for user restrictions. 217 * <p/>Type: Boolean 218 * @see DevicePolicyManager#addUserRestriction(ComponentName, String) 219 * @see DevicePolicyManager#clearUserRestriction(ComponentName, String) 220 * @see #getUserRestrictions() 221 */ 222 public static final String DISALLOW_CONFIG_TETHERING = "no_config_tethering"; 223 224 /** 225 * Specifies if a user is disallowed from resetting network settings 226 * from Settings. This can only be set by device owners and profile owners on the primary user. 227 * The default value is <code>false</code>. 228 * <p/>This restriction has no effect on secondary users and managed profiles since only the 229 * primary user can reset the network settings of the device. 230 * 231 * <p/>Key for user restrictions. 232 * <p/>Type: Boolean 233 * @see DevicePolicyManager#addUserRestriction(ComponentName, String) 234 * @see DevicePolicyManager#clearUserRestriction(ComponentName, String) 235 * @see #getUserRestrictions() 236 */ 237 public static final String DISALLOW_NETWORK_RESET = "no_network_reset"; 238 239 /** 240 * Specifies if a user is disallowed from factory resetting 241 * from Settings. This can only be set by device owners and profile owners on the primary user. 242 * The default value is <code>false</code>. 243 * <p/>This restriction has no effect on secondary users and managed profiles since only the 244 * primary user can factory reset the device. 245 * 246 * <p/>Key for user restrictions. 247 * <p/>Type: Boolean 248 * @see DevicePolicyManager#addUserRestriction(ComponentName, String) 249 * @see DevicePolicyManager#clearUserRestriction(ComponentName, String) 250 * @see #getUserRestrictions() 251 */ 252 public static final String DISALLOW_FACTORY_RESET = "no_factory_reset"; 253 254 /** 255 * Specifies if a user is disallowed from adding new users and 256 * profiles. This can only be set by device owners and profile owners on the primary user. 257 * The default value is <code>false</code>. 258 * <p/>This restriction has no effect on secondary users and managed profiles since only the 259 * primary user can add other users. 260 * 261 * <p/>Key for user restrictions. 262 * <p/>Type: Boolean 263 * @see DevicePolicyManager#addUserRestriction(ComponentName, String) 264 * @see DevicePolicyManager#clearUserRestriction(ComponentName, String) 265 * @see #getUserRestrictions() 266 */ 267 public static final String DISALLOW_ADD_USER = "no_add_user"; 268 269 /** 270 * Specifies if a user is disallowed from disabling application 271 * verification. The default value is <code>false</code>. 272 * 273 * <p/>Key for user restrictions. 274 * <p/>Type: Boolean 275 * @see DevicePolicyManager#addUserRestriction(ComponentName, String) 276 * @see DevicePolicyManager#clearUserRestriction(ComponentName, String) 277 * @see #getUserRestrictions() 278 */ 279 public static final String ENSURE_VERIFY_APPS = "ensure_verify_apps"; 280 281 /** 282 * Specifies if a user is disallowed from configuring cell 283 * broadcasts. This can only be set by device owners and profile owners on the primary user. 284 * The default value is <code>false</code>. 285 * <p/>This restriction has no effect on secondary users and managed profiles since only the 286 * primary user can configure cell broadcasts. 287 * 288 * <p/>Key for user restrictions. 289 * <p/>Type: Boolean 290 * @see DevicePolicyManager#addUserRestriction(ComponentName, String) 291 * @see DevicePolicyManager#clearUserRestriction(ComponentName, String) 292 * @see #getUserRestrictions() 293 */ 294 public static final String DISALLOW_CONFIG_CELL_BROADCASTS = "no_config_cell_broadcasts"; 295 296 /** 297 * Specifies if a user is disallowed from configuring mobile 298 * networks. This can only be set by device owners and profile owners on the primary user. 299 * The default value is <code>false</code>. 300 * <p/>This restriction has no effect on secondary users and managed profiles since only the 301 * primary user can configure mobile networks. 302 * 303 * <p/>Key for user restrictions. 304 * <p/>Type: Boolean 305 * @see DevicePolicyManager#addUserRestriction(ComponentName, String) 306 * @see DevicePolicyManager#clearUserRestriction(ComponentName, String) 307 * @see #getUserRestrictions() 308 */ 309 public static final String DISALLOW_CONFIG_MOBILE_NETWORKS = "no_config_mobile_networks"; 310 311 /** 312 * Specifies if a user is disallowed from modifying 313 * applications in Settings or launchers. The following actions will not be allowed when this 314 * restriction is enabled: 315 * <li>uninstalling apps</li> 316 * <li>disabling apps</li> 317 * <li>clearing app caches</li> 318 * <li>clearing app data</li> 319 * <li>force stopping apps</li> 320 * <li>clearing app defaults</li> 321 * <p> 322 * The default value is <code>false</code>. 323 * 324 * <p/>Key for user restrictions. 325 * <p/>Type: Boolean 326 * @see DevicePolicyManager#addUserRestriction(ComponentName, String) 327 * @see DevicePolicyManager#clearUserRestriction(ComponentName, String) 328 * @see #getUserRestrictions() 329 */ 330 public static final String DISALLOW_APPS_CONTROL = "no_control_apps"; 331 332 /** 333 * Specifies if a user is disallowed from mounting 334 * physical external media. This can only be set by device owners and profile owners on the 335 * primary user. The default value is <code>false</code>. 336 * 337 * <p/>Key for user restrictions. 338 * <p/>Type: Boolean 339 * @see DevicePolicyManager#addUserRestriction(ComponentName, String) 340 * @see DevicePolicyManager#clearUserRestriction(ComponentName, String) 341 * @see #getUserRestrictions() 342 */ 343 public static final String DISALLOW_MOUNT_PHYSICAL_MEDIA = "no_physical_media"; 344 345 /** 346 * Specifies if a user is disallowed from adjusting microphone 347 * volume. If set, the microphone will be muted. This can only be set by device owners 348 * and profile owners on the primary user. The default value is <code>false</code>. 349 * 350 * <p/>Key for user restrictions. 351 * <p/>Type: Boolean 352 * @see DevicePolicyManager#addUserRestriction(ComponentName, String) 353 * @see DevicePolicyManager#clearUserRestriction(ComponentName, String) 354 * @see #getUserRestrictions() 355 */ 356 public static final String DISALLOW_UNMUTE_MICROPHONE = "no_unmute_microphone"; 357 358 /** 359 * Specifies if a user is disallowed from adjusting the master 360 * volume. If set, the master volume will be muted. This can only be set by device owners 361 * and profile owners on the primary user. The default value is <code>false</code>. 362 * 363 * <p/>Key for user restrictions. 364 * <p/>Type: Boolean 365 * @see DevicePolicyManager#addUserRestriction(ComponentName, String) 366 * @see DevicePolicyManager#clearUserRestriction(ComponentName, String) 367 * @see #getUserRestrictions() 368 */ 369 public static final String DISALLOW_ADJUST_VOLUME = "no_adjust_volume"; 370 371 /** 372 * Specifies that the user is not allowed to make outgoing 373 * phone calls. Emergency calls are still permitted. 374 * The default value is <code>false</code>. 375 * <p/>This restriction has no effect on managed profiles since call intents are normally 376 * forwarded to the primary user. 377 * 378 * <p/>Key for user restrictions. 379 * <p/>Type: Boolean 380 * @see DevicePolicyManager#addUserRestriction(ComponentName, String) 381 * @see DevicePolicyManager#clearUserRestriction(ComponentName, String) 382 * @see #getUserRestrictions() 383 */ 384 public static final String DISALLOW_OUTGOING_CALLS = "no_outgoing_calls"; 385 386 /** 387 * Specifies that the user is not allowed to send or receive 388 * SMS messages. The default value is <code>false</code>. 389 * 390 * <p/>Key for user restrictions. 391 * <p/>Type: Boolean 392 * @see DevicePolicyManager#addUserRestriction(ComponentName, String) 393 * @see DevicePolicyManager#clearUserRestriction(ComponentName, String) 394 * @see #getUserRestrictions() 395 */ 396 public static final String DISALLOW_SMS = "no_sms"; 397 398 /** 399 * Specifies if the user is not allowed to have fun. In some cases, the 400 * device owner may wish to prevent the user from experiencing amusement or 401 * joy while using the device. The default value is <code>false</code>. 402 * 403 * <p/>Key for user restrictions. 404 * <p/>Type: Boolean 405 * @see DevicePolicyManager#addUserRestriction(ComponentName, String) 406 * @see DevicePolicyManager#clearUserRestriction(ComponentName, String) 407 * @see #getUserRestrictions() 408 */ 409 public static final String DISALLOW_FUN = "no_fun"; 410 411 /** 412 * Specifies that windows besides app windows should not be 413 * created. This will block the creation of the following types of windows. 414 * <li>{@link LayoutParams#TYPE_TOAST}</li> 415 * <li>{@link LayoutParams#TYPE_PHONE}</li> 416 * <li>{@link LayoutParams#TYPE_PRIORITY_PHONE}</li> 417 * <li>{@link LayoutParams#TYPE_SYSTEM_ALERT}</li> 418 * <li>{@link LayoutParams#TYPE_SYSTEM_ERROR}</li> 419 * <li>{@link LayoutParams#TYPE_SYSTEM_OVERLAY}</li> 420 * 421 * <p>This can only be set by device owners and profile owners on the primary user. 422 * The default value is <code>false</code>. 423 * 424 * <p/>Key for user restrictions. 425 * <p/>Type: Boolean 426 * @see DevicePolicyManager#addUserRestriction(ComponentName, String) 427 * @see DevicePolicyManager#clearUserRestriction(ComponentName, String) 428 * @see #getUserRestrictions() 429 */ 430 public static final String DISALLOW_CREATE_WINDOWS = "no_create_windows"; 431 432 /** 433 * Specifies if what is copied in the clipboard of this profile can 434 * be pasted in related profiles. Does not restrict if the clipboard of related profiles can be 435 * pasted in this profile. 436 * The default value is <code>false</code>. 437 * 438 * <p/>Key for user restrictions. 439 * <p/>Type: Boolean 440 * @see DevicePolicyManager#addUserRestriction(ComponentName, String) 441 * @see DevicePolicyManager#clearUserRestriction(ComponentName, String) 442 * @see #getUserRestrictions() 443 */ 444 public static final String DISALLOW_CROSS_PROFILE_COPY_PASTE = "no_cross_profile_copy_paste"; 445 446 /** 447 * Specifies if the user is not allowed to use NFC to beam out data from apps. 448 * The default value is <code>false</code>. 449 * 450 * <p/>Key for user restrictions. 451 * <p/>Type: Boolean 452 * @see DevicePolicyManager#addUserRestriction(ComponentName, String) 453 * @see DevicePolicyManager#clearUserRestriction(ComponentName, String) 454 * @see #getUserRestrictions() 455 */ 456 public static final String DISALLOW_OUTGOING_BEAM = "no_outgoing_beam"; 457 458 /** 459 * Hidden user restriction to disallow access to wallpaper manager APIs. This user restriction 460 * is always set for managed profiles. 461 * @hide 462 * @see DevicePolicyManager#addUserRestriction(ComponentName, String) 463 * @see DevicePolicyManager#clearUserRestriction(ComponentName, String) 464 * @see #getUserRestrictions() 465 */ 466 public static final String DISALLOW_WALLPAPER = "no_wallpaper"; 467 468 /** 469 * Specifies if the user is not allowed to reboot the device into safe boot mode. 470 * This can only be set by device owners and profile owners on the primary user. 471 * The default value is <code>false</code>. 472 * 473 * <p/>Key for user restrictions. 474 * <p/>Type: Boolean 475 * @see DevicePolicyManager#addUserRestriction(ComponentName, String) 476 * @see DevicePolicyManager#clearUserRestriction(ComponentName, String) 477 * @see #getUserRestrictions() 478 */ 479 public static final String DISALLOW_SAFE_BOOT = "no_safe_boot"; 480 481 /** 482 * Specifies if a user is not allowed to record audio. This restriction is always enabled for 483 * background users. The default value is <code>false</code>. 484 * 485 * @see DevicePolicyManager#addUserRestriction(ComponentName, String) 486 * @see DevicePolicyManager#clearUserRestriction(ComponentName, String) 487 * @see #getUserRestrictions() 488 * @hide 489 */ 490 public static final String DISALLOW_RECORD_AUDIO = "no_record_audio"; 491 492 /** 493 * Specifies if a user is not allowed to run in the background and should be stopped during 494 * user switch. The default value is <code>false</code>. 495 * 496 * <p>This restriction can be set by device owners and profile owners. 497 * 498 * @see DevicePolicyManager#addUserRestriction(ComponentName, String) 499 * @see DevicePolicyManager#clearUserRestriction(ComponentName, String) 500 * @see #getUserRestrictions() 501 * @hide 502 */ 503 public static final String DISALLOW_RUN_IN_BACKGROUND = "no_run_in_background"; 504 505 /** 506 * Specifies if a user is not allowed to use the camera. 507 * 508 * @see DevicePolicyManager#addUserRestriction(ComponentName, String) 509 * @see DevicePolicyManager#clearUserRestriction(ComponentName, String) 510 * @see #getUserRestrictions() 511 * @hide 512 */ 513 public static final String DISALLOW_CAMERA = "no_camera"; 514 515 /** 516 * Allows apps in the parent profile to handle web links from the managed profile. 517 * 518 * This user restriction has an effect only in a managed profile. 519 * If set: 520 * Intent filters of activities in the parent profile with action 521 * {@link android.content.Intent#ACTION_VIEW}, 522 * category {@link android.content.Intent#CATEGORY_BROWSABLE}, scheme http or https, and which 523 * define a host can handle intents from the managed profile. 524 * The default value is <code>false</code>. 525 * 526 * <p/>Key for user restrictions. 527 * <p/>Type: Boolean 528 * @see DevicePolicyManager#addUserRestriction(ComponentName, String) 529 * @see DevicePolicyManager#clearUserRestriction(ComponentName, String) 530 * @see #getUserRestrictions() 531 */ 532 public static final String ALLOW_PARENT_PROFILE_APP_LINKING 533 = "allow_parent_profile_app_linking"; 534 535 /** 536 * Application restriction key that is used to indicate the pending arrival 537 * of real restrictions for the app. 538 * 539 * <p> 540 * Applications that support restrictions should check for the presence of this key. 541 * A <code>true</code> value indicates that restrictions may be applied in the near 542 * future but are not available yet. It is the responsibility of any 543 * management application that sets this flag to update it when the final 544 * restrictions are enforced. 545 * 546 * <p/>Key for application restrictions. 547 * <p/>Type: Boolean 548 * @see android.app.admin.DevicePolicyManager#setApplicationRestrictions( 549 * android.content.ComponentName, String, Bundle) 550 * @see android.app.admin.DevicePolicyManager#getApplicationRestrictions( 551 * android.content.ComponentName, String) 552 */ 553 public static final String KEY_RESTRICTIONS_PENDING = "restrictions_pending"; 554 555 /** @hide */ 556 public static final int PIN_VERIFICATION_FAILED_INCORRECT = -3; 557 /** @hide */ 558 public static final int PIN_VERIFICATION_FAILED_NOT_SET = -2; 559 /** @hide */ 560 public static final int PIN_VERIFICATION_SUCCESS = -1; 561 562 /** @hide */ 563 public static UserManager get(Context context) { 564 return (UserManager) context.getSystemService(Context.USER_SERVICE); 565 } 566 567 /** @hide */ 568 public UserManager(Context context, IUserManager service) { 569 mService = service; 570 mContext = context; 571 } 572 573 /** 574 * Returns whether the system supports multiple users. 575 * @return true if multiple users can be created by user, false if it is a single user device. 576 * @hide 577 */ 578 public static boolean supportsMultipleUsers() { 579 return getMaxSupportedUsers() > 1 580 && SystemProperties.getBoolean("fw.show_multiuserui", 581 Resources.getSystem().getBoolean(R.bool.config_enableMultiUserUI)); 582 } 583 584 /** 585 * @hide 586 * @return Whether the device is running with split system user. It means the system user and 587 * primary user are two separate users. Previously system user and primary user are combined as 588 * a single owner user. see @link {android.os.UserHandle#USER_OWNER} 589 */ 590 public static boolean isSplitSystemUser() { 591 return SystemProperties.getBoolean("ro.fw.system_user_split", false); 592 } 593 594 /** 595 * Returns the user handle for the user that this process is running under. 596 * 597 * @return the user handle of this process. 598 * @hide 599 */ 600 public int getUserHandle() { 601 return UserHandle.myUserId(); 602 } 603 604 /** 605 * Returns the user name of the user making this call. This call is only 606 * available to applications on the system image; it requires the 607 * MANAGE_USERS permission. 608 * @return the user name 609 */ 610 public String getUserName() { 611 try { 612 return mService.getUserInfo(getUserHandle()).name; 613 } catch (RemoteException re) { 614 Log.w(TAG, "Could not get user name", re); 615 return ""; 616 } 617 } 618 619 /** 620 * Used to determine whether the user making this call is subject to 621 * teleportations. 622 * 623 * <p>As of {@link android.os.Build.VERSION_CODES#LOLLIPOP}, this method can 624 * now automatically identify goats using advanced goat recognition technology.</p> 625 * 626 * @return Returns true if the user making this call is a goat. 627 */ 628 public boolean isUserAGoat() { 629 return mContext.getPackageManager() 630 .isPackageAvailable("com.coffeestainstudios.goatsimulator"); 631 } 632 633 /** 634 * Used to check if this process is running under the primary user. The primary user 635 * is the first human user on a device. 636 * 637 * @return whether this process is running under the primary user. 638 * @hide 639 */ 640 public boolean isPrimaryUser() { 641 UserInfo user = getUserInfo(UserHandle.myUserId()); 642 return user != null ? user.isPrimary() : false; 643 } 644 645 /** 646 * Used to check if this process is running under the system user. The system user 647 * is the initial user that is implicitly created on first boot and hosts most of the 648 * system services. 649 * 650 * @return whether this process is running under the system user. 651 */ 652 public boolean isSystemUser() { 653 return UserHandle.myUserId() == UserHandle.USER_SYSTEM; 654 } 655 656 /** 657 * @hide 658 * Returns whether the caller is running as an admin user. There can be more than one admin 659 * user. 660 */ 661 public boolean isAdminUser() { 662 return isUserAdmin(UserHandle.myUserId()); 663 } 664 665 /** 666 * @hide 667 * Returns whether the provided user is an admin user. There can be more than one admin 668 * user. 669 */ 670 public boolean isUserAdmin(int userId) { 671 UserInfo user = getUserInfo(userId); 672 return user != null && user.isAdmin(); 673 } 674 675 /** 676 * Used to check if the user making this call is linked to another user. Linked users may have 677 * a reduced number of available apps, app restrictions and account restrictions. 678 * @return whether the user making this call is a linked user 679 * @hide 680 */ 681 public boolean isLinkedUser() { 682 try { 683 return mService.isRestricted(); 684 } catch (RemoteException re) { 685 Log.w(TAG, "Could not check if user is limited ", re); 686 return false; 687 } 688 } 689 690 /** 691 * Checks if specified user can have restricted profile. 692 * @hide 693 */ 694 public boolean canHaveRestrictedProfile(int userId) { 695 try { 696 return mService.canHaveRestrictedProfile(userId); 697 } catch (RemoteException re) { 698 Log.w(TAG, "Could not check if user can have restricted profile", re); 699 return false; 700 } 701 } 702 703 /** 704 * Checks if the calling app is running as a guest user. 705 * @return whether the caller is a guest user. 706 * @hide 707 */ 708 public boolean isGuestUser() { 709 UserInfo user = getUserInfo(UserHandle.myUserId()); 710 return user != null ? user.isGuest() : false; 711 } 712 713 /** 714 * Checks if the calling app is running in a managed profile. 715 * Requires {@link android.Manifest.permission#MANAGE_USERS} permission. 716 * 717 * @return whether the caller is in a managed profile. 718 * @hide 719 */ 720 @SystemApi 721 public boolean isManagedProfile() { 722 UserInfo user = getUserInfo(UserHandle.myUserId()); 723 return user != null ? user.isManagedProfile() : false; 724 } 725 726 /** 727 * Checks if the calling app is running as an ephemeral user. 728 * 729 * @return whether the caller is an ephemeral user. 730 * @hide 731 */ 732 public boolean isEphemeralUser() { 733 return isUserEphemeral(UserHandle.myUserId()); 734 } 735 736 /** 737 * Returns whether the specified user is ephemeral. 738 * @hide 739 */ 740 public boolean isUserEphemeral(int userId) { 741 final UserInfo user = getUserInfo(userId); 742 return user != null && user.isEphemeral(); 743 } 744 745 /** 746 * Return whether the given user is actively running. This means that 747 * the user is in the "started" state, not "stopped" -- it is currently 748 * allowed to run code through scheduled alarms, receiving broadcasts, 749 * etc. A started user may be either the current foreground user or a 750 * background user; the result here does not distinguish between the two. 751 * @param user The user to retrieve the running state for. 752 */ 753 public boolean isUserRunning(UserHandle user) { 754 try { 755 return ActivityManagerNative.getDefault().isUserRunning( 756 user.getIdentifier(), 0); 757 } catch (RemoteException e) { 758 return false; 759 } 760 } 761 762 /** 763 * Return whether the given user is actively running <em>or</em> stopping. 764 * This is like {@link #isUserRunning(UserHandle)}, but will also return 765 * true if the user had been running but is in the process of being stopped 766 * (but is not yet fully stopped, and still running some code). 767 * @param user The user to retrieve the running state for. 768 */ 769 public boolean isUserRunningOrStopping(UserHandle user) { 770 try { 771 // TODO: reconcile stopped vs stopping? 772 return ActivityManagerNative.getDefault().isUserRunning( 773 user.getIdentifier(), ActivityManager.FLAG_OR_STOPPED); 774 } catch (RemoteException e) { 775 return false; 776 } 777 } 778 779 /** 780 * Return whether the calling user is running in a "locked" state. A user is 781 * unlocked only after they've entered their credentials (such as a lock 782 * pattern or PIN), and credential-encrypted private app data storage is 783 * available. 784 */ 785 @Deprecated 786 public boolean isUserRunningAndLocked() { 787 return isUserRunningAndLocked(Process.myUserHandle()); 788 } 789 790 /** 791 * Return whether the given user is running in a "locked" state. A user 792 * is unlocked only after they've entered their credentials (such as a lock 793 * pattern or PIN), and credential-encrypted private app data storage is 794 * available. 795 * 796 * @param user to retrieve the unlocked state for. 797 */ 798 @Deprecated 799 public boolean isUserRunningAndLocked(UserHandle user) { 800 try { 801 return ActivityManagerNative.getDefault().isUserRunning( 802 user.getIdentifier(), ActivityManager.FLAG_AND_LOCKED); 803 } catch (RemoteException e) { 804 return false; 805 } 806 } 807 808 /** 809 * Return whether the calling user is running in an "unlocked" state. A user 810 * is unlocked only after they've entered their credentials (such as a lock 811 * pattern or PIN), and credential-encrypted private app data storage is 812 * available. 813 */ 814 @Deprecated 815 public boolean isUserRunningAndUnlocked() { 816 return isUserRunningAndUnlocked(Process.myUserHandle()); 817 } 818 819 /** 820 * Return whether the given user is running in an "unlocked" state. A user 821 * is unlocked only after they've entered their credentials (such as a lock 822 * pattern or PIN), and credential-encrypted private app data storage is 823 * available. 824 * 825 * @param user to retrieve the unlocked state for. 826 */ 827 @Deprecated 828 public boolean isUserRunningAndUnlocked(UserHandle user) { 829 try { 830 return ActivityManagerNative.getDefault().isUserRunning( 831 user.getIdentifier(), ActivityManager.FLAG_AND_UNLOCKED); 832 } catch (RemoteException e) { 833 return false; 834 } 835 } 836 837 /** 838 * Return whether the calling user is running in an "unlocked" state. A user 839 * is unlocked only after they've entered their credentials (such as a lock 840 * pattern or PIN), and credential-encrypted private app data storage is 841 * available. 842 */ 843 public boolean isUserUnlocked() { 844 return isUserUnlocked(Process.myUserHandle()); 845 } 846 847 /** 848 * Return whether the given user is running in an "unlocked" state. A user 849 * is unlocked only after they've entered their credentials (such as a lock 850 * pattern or PIN), and credential-encrypted private app data storage is 851 * available. 852 * 853 * @param user to retrieve the unlocked state for. 854 */ 855 public boolean isUserUnlocked(UserHandle user) { 856 return isUserUnlocked(user.getIdentifier()); 857 } 858 859 /** {@hide} */ 860 public boolean isUserUnlocked(int userId) { 861 // TODO: eventually pivot this back to look at ActivityManager state, 862 // but there is race where we can start a non-encryption-aware launcher 863 // before that lifecycle has entered the running unlocked state. 864 return mContext.getSystemService(StorageManager.class).isUserKeyUnlocked(userId); 865 } 866 867 /** 868 * Returns the UserInfo object describing a specific user. 869 * Requires {@link android.Manifest.permission#MANAGE_USERS} permission. 870 * @param userHandle the user handle of the user whose information is being requested. 871 * @return the UserInfo object for a specific user. 872 * @hide 873 */ 874 public UserInfo getUserInfo(int userHandle) { 875 try { 876 return mService.getUserInfo(userHandle); 877 } catch (RemoteException re) { 878 Log.w(TAG, "Could not get user info", re); 879 return null; 880 } 881 } 882 883 /** 884 * Returns the user-wide restrictions imposed on this user. 885 * @return a Bundle containing all the restrictions. 886 */ 887 public Bundle getUserRestrictions() { 888 return getUserRestrictions(Process.myUserHandle()); 889 } 890 891 /** 892 * Returns the user-wide restrictions imposed on the user specified by <code>userHandle</code>. 893 * @param userHandle the UserHandle of the user for whom to retrieve the restrictions. 894 * @return a Bundle containing all the restrictions. 895 */ 896 public Bundle getUserRestrictions(UserHandle userHandle) { 897 try { 898 return mService.getUserRestrictions(userHandle.getIdentifier()); 899 } catch (RemoteException re) { 900 Log.w(TAG, "Could not get user restrictions", re); 901 return Bundle.EMPTY; 902 } 903 } 904 905 /** 906 * @hide 907 * Returns whether the given user has been disallowed from performing certain actions 908 * or setting certain settings through UserManager. This method disregards restrictions 909 * set by device policy. 910 * @param restrictionKey the string key representing the restriction 911 * @param userHandle the UserHandle of the user for whom to retrieve the restrictions. 912 */ 913 public boolean hasBaseUserRestriction(String restrictionKey, UserHandle userHandle) { 914 try { 915 return mService.hasBaseUserRestriction(restrictionKey, userHandle.getIdentifier()); 916 } catch (RemoteException re) { 917 Log.w(TAG, "Could not get base user restrictions for user " + 918 userHandle.getIdentifier(), re); 919 return false; 920 } 921 } 922 923 /** 924 * This will no longer work. Device owners and profile owners should use 925 * {@link DevicePolicyManager#addUserRestriction(ComponentName, String)} instead. 926 */ 927 // System apps should use UserManager.setUserRestriction() instead. 928 @Deprecated 929 public void setUserRestrictions(Bundle restrictions) { 930 throw new UnsupportedOperationException("This method is no longer supported"); 931 } 932 933 /** 934 * This will no longer work. Device owners and profile owners should use 935 * {@link DevicePolicyManager#addUserRestriction(ComponentName, String)} instead. 936 */ 937 // System apps should use UserManager.setUserRestriction() instead. 938 @Deprecated 939 public void setUserRestrictions(Bundle restrictions, UserHandle userHandle) { 940 throw new UnsupportedOperationException("This method is no longer supported"); 941 } 942 943 /** 944 * Sets the value of a specific restriction. 945 * Requires the MANAGE_USERS permission. 946 * @param key the key of the restriction 947 * @param value the value for the restriction 948 * @deprecated use {@link android.app.admin.DevicePolicyManager#addUserRestriction( 949 * android.content.ComponentName, String)} or 950 * {@link android.app.admin.DevicePolicyManager#clearUserRestriction( 951 * android.content.ComponentName, String)} instead. 952 */ 953 @Deprecated 954 public void setUserRestriction(String key, boolean value) { 955 setUserRestriction(key, value, Process.myUserHandle()); 956 } 957 958 /** 959 * @hide 960 * Sets the value of a specific restriction on a specific user. 961 * Requires the MANAGE_USERS permission. 962 * @param key the key of the restriction 963 * @param value the value for the restriction 964 * @param userHandle the user whose restriction is to be changed. 965 * @deprecated use {@link android.app.admin.DevicePolicyManager#addUserRestriction( 966 * android.content.ComponentName, String)} or 967 * {@link android.app.admin.DevicePolicyManager#clearUserRestriction( 968 * android.content.ComponentName, String)} instead. 969 */ 970 @Deprecated 971 public void setUserRestriction(String key, boolean value, UserHandle userHandle) { 972 try { 973 mService.setUserRestriction(key, value, userHandle.getIdentifier()); 974 } catch (RemoteException re) { 975 Log.w(TAG, "Could not set user restriction", re); 976 } 977 } 978 979 /** 980 * Returns whether the current user has been disallowed from performing certain actions 981 * or setting certain settings. 982 * 983 * @param restrictionKey The string key representing the restriction. 984 * @return {@code true} if the current user has the given restriction, {@code false} otherwise. 985 */ 986 public boolean hasUserRestriction(String restrictionKey) { 987 return hasUserRestriction(restrictionKey, Process.myUserHandle()); 988 } 989 990 /** 991 * @hide 992 * Returns whether the given user has been disallowed from performing certain actions 993 * or setting certain settings. 994 * @param restrictionKey the string key representing the restriction 995 * @param userHandle the UserHandle of the user for whom to retrieve the restrictions. 996 */ 997 public boolean hasUserRestriction(String restrictionKey, UserHandle userHandle) { 998 try { 999 return mService.hasUserRestriction(restrictionKey, 1000 userHandle.getIdentifier()); 1001 } catch (RemoteException re) { 1002 Log.w(TAG, "Could not check user restrictions", re); 1003 return false; 1004 } 1005 } 1006 1007 /** 1008 * Return the serial number for a user. This is a device-unique 1009 * number assigned to that user; if the user is deleted and then a new 1010 * user created, the new users will not be given the same serial number. 1011 * @param user The user whose serial number is to be retrieved. 1012 * @return The serial number of the given user; returns -1 if the 1013 * given UserHandle does not exist. 1014 * @see #getUserForSerialNumber(long) 1015 */ 1016 public long getSerialNumberForUser(UserHandle user) { 1017 return getUserSerialNumber(user.getIdentifier()); 1018 } 1019 1020 /** 1021 * Return the user associated with a serial number previously 1022 * returned by {@link #getSerialNumberForUser(UserHandle)}. 1023 * @param serialNumber The serial number of the user that is being 1024 * retrieved. 1025 * @return Return the user associated with the serial number, or null 1026 * if there is not one. 1027 * @see #getSerialNumberForUser(UserHandle) 1028 */ 1029 public UserHandle getUserForSerialNumber(long serialNumber) { 1030 int ident = getUserHandle((int) serialNumber); 1031 return ident >= 0 ? new UserHandle(ident) : null; 1032 } 1033 1034 /** 1035 * Creates a user with the specified name and options. For non-admin users, default user 1036 * restrictions are going to be applied. 1037 * Requires {@link android.Manifest.permission#MANAGE_USERS} permission. 1038 * 1039 * @param name the user's name 1040 * @param flags flags that identify the type of user and other properties. 1041 * @see UserInfo 1042 * 1043 * @return the UserInfo object for the created user, or null if the user could not be created. 1044 * @hide 1045 */ 1046 public UserInfo createUser(String name, int flags) { 1047 UserInfo user = null; 1048 try { 1049 user = mService.createUser(name, flags); 1050 if (user != null && !user.isAdmin()) { 1051 mService.setUserRestriction(DISALLOW_SMS, true, user.id); 1052 mService.setUserRestriction(DISALLOW_OUTGOING_CALLS, true, user.id); 1053 } 1054 } catch (RemoteException re) { 1055 Log.w(TAG, "Could not create a user", re); 1056 } 1057 return user; 1058 } 1059 1060 /** 1061 * Creates a guest user and configures it. 1062 * @param context an application context 1063 * @param name the name to set for the user 1064 * @hide 1065 */ 1066 public UserInfo createGuest(Context context, String name) { 1067 UserInfo guest = null; 1068 try { 1069 guest = mService.createUser(name, UserInfo.FLAG_GUEST); 1070 if (guest != null) { 1071 Settings.Secure.putStringForUser(context.getContentResolver(), 1072 Settings.Secure.SKIP_FIRST_USE_HINTS, "1", guest.id); 1073 } 1074 } catch (RemoteException re) { 1075 Log.w(TAG, "Could not create a user", re); 1076 } 1077 return guest; 1078 } 1079 1080 /** 1081 * Creates a user with the specified name and options as a profile of another user. 1082 * Requires {@link android.Manifest.permission#MANAGE_USERS} permission. 1083 * 1084 * @param name the user's name 1085 * @param flags flags that identify the type of user and other properties. 1086 * @see UserInfo 1087 * @param userHandle new user will be a profile of this use. 1088 * 1089 * @return the UserInfo object for the created user, or null if the user could not be created. 1090 * @hide 1091 */ 1092 public UserInfo createProfileForUser(String name, int flags, int userHandle) { 1093 try { 1094 return mService.createProfileForUser(name, flags, userHandle); 1095 } catch (RemoteException re) { 1096 Log.w(TAG, "Could not create a user", re); 1097 return null; 1098 } 1099 } 1100 1101 /** 1102 * Creates a restricted profile with the specified name. This method also sets necessary 1103 * restrictions and adds shared accounts. 1104 * 1105 * @param name profile's name 1106 * @return UserInfo object for the created user, or null if the user could not be created. 1107 * @hide 1108 */ 1109 public UserInfo createRestrictedProfile(String name) { 1110 try { 1111 UserHandle parentUserHandle = Process.myUserHandle(); 1112 UserInfo user = mService.createRestrictedProfile(name, 1113 parentUserHandle.getIdentifier()); 1114 if (user != null) { 1115 AccountManager.get(mContext).addSharedAccountsFromParentUser(parentUserHandle, 1116 UserHandle.of(user.id)); 1117 } 1118 return user; 1119 } catch (RemoteException e) { 1120 Log.w(TAG, "Could not create a restricted profile", e); 1121 } 1122 return null; 1123 } 1124 1125 /** 1126 * @hide 1127 * Marks the guest user for deletion to allow a new guest to be created before deleting 1128 * the current user who is a guest. 1129 * @param userHandle 1130 * @return 1131 */ 1132 public boolean markGuestForDeletion(int userHandle) { 1133 try { 1134 return mService.markGuestForDeletion(userHandle); 1135 } catch (RemoteException re) { 1136 Log.w(TAG, "Could not mark guest for deletion", re); 1137 return false; 1138 } 1139 } 1140 1141 /** 1142 * Sets the user as enabled, if such an user exists. 1143 * Requires {@link android.Manifest.permission#MANAGE_USERS} permission. 1144 * Note that the default is true, it's only that managed profiles might not be enabled. 1145 * 1146 * @param userHandle the id of the profile to enable 1147 * @hide 1148 */ 1149 public void setUserEnabled(int userHandle) { 1150 try { 1151 mService.setUserEnabled(userHandle); 1152 } catch (RemoteException e) { 1153 Log.w(TAG, "Could not enable the profile", e); 1154 } 1155 } 1156 1157 /** 1158 * Return the number of users currently created on the device. 1159 */ 1160 public int getUserCount() { 1161 List<UserInfo> users = getUsers(); 1162 return users != null ? users.size() : 1; 1163 } 1164 1165 /** 1166 * Returns information for all users on this device. 1167 * Requires {@link android.Manifest.permission#MANAGE_USERS} permission. 1168 * @return the list of users that were created. 1169 * @hide 1170 */ 1171 public List<UserInfo> getUsers() { 1172 try { 1173 return mService.getUsers(false); 1174 } catch (RemoteException re) { 1175 Log.w(TAG, "Could not get user list", re); 1176 return null; 1177 } 1178 } 1179 1180 /** 1181 * @return the user's account name, null if not found. 1182 * @hide 1183 */ 1184 @RequiresPermission( allOf = { 1185 Manifest.permission.INTERACT_ACROSS_USERS_FULL, 1186 Manifest.permission.MANAGE_USERS 1187 }) 1188 public @Nullable String getUserAccount(int userHandle) { 1189 try { 1190 return mService.getUserAccount(userHandle); 1191 } catch (RemoteException re) { 1192 Log.w(TAG, "Could not get user account", re); 1193 return null; 1194 } 1195 } 1196 1197 /** 1198 * Set account name for the given user. 1199 * @hide 1200 */ 1201 @RequiresPermission( allOf = { 1202 Manifest.permission.INTERACT_ACROSS_USERS_FULL, 1203 Manifest.permission.MANAGE_USERS 1204 }) 1205 public void setUserAccount(int userHandle, @Nullable String accountName) { 1206 try { 1207 mService.setUserAccount(userHandle, accountName); 1208 } catch (RemoteException re) { 1209 Log.w(TAG, "Could not set user account", re); 1210 } 1211 } 1212 1213 /** 1214 * Returns information for Primary user. 1215 * Requires {@link android.Manifest.permission#MANAGE_USERS} permission. 1216 * 1217 * @return the Primary user, null if not found. 1218 * @hide 1219 */ 1220 public @Nullable UserInfo getPrimaryUser() { 1221 try { 1222 return mService.getPrimaryUser(); 1223 } catch (RemoteException re) { 1224 Log.w(TAG, "Could not get Primary user", re); 1225 return null; 1226 } 1227 } 1228 1229 /** 1230 * Checks whether it's possible to add more users. Caller must hold the MANAGE_USERS 1231 * permission. 1232 * 1233 * @return true if more users can be added, false if limit has been reached. 1234 * @hide 1235 */ 1236 public boolean canAddMoreUsers() { 1237 final List<UserInfo> users = getUsers(true); 1238 final int totalUserCount = users.size(); 1239 int aliveUserCount = 0; 1240 for (int i = 0; i < totalUserCount; i++) { 1241 UserInfo user = users.get(i); 1242 if (!user.isGuest()) { 1243 aliveUserCount++; 1244 } 1245 } 1246 return aliveUserCount < getMaxSupportedUsers(); 1247 } 1248 1249 /** 1250 * Checks whether it's possible to add more managed profiles. Caller must hold the MANAGE_USERS 1251 * permission. 1252 * if allowedToRemoveOne is true and if the user already has a managed profile, then return if 1253 * we could add a new managed profile to this user after removing the existing one. 1254 * 1255 * @return true if more managed profiles can be added, false if limit has been reached. 1256 * @hide 1257 */ 1258 public boolean canAddMoreManagedProfiles(int userId, boolean allowedToRemoveOne) { 1259 try { 1260 return mService.canAddMoreManagedProfiles(userId, allowedToRemoveOne); 1261 } catch (RemoteException re) { 1262 Log.w(TAG, "Could not check if we can add more managed profiles", re); 1263 return false; 1264 } 1265 } 1266 1267 /** 1268 * Returns list of the profiles of userHandle including 1269 * userHandle itself. 1270 * Note that this returns both enabled and not enabled profiles. See 1271 * {@link #getEnabledProfiles(int)} if you need only the enabled ones. 1272 * 1273 * Requires {@link android.Manifest.permission#MANAGE_USERS} permission. 1274 * @param userHandle profiles of this user will be returned. 1275 * @return the list of profiles. 1276 * @hide 1277 */ 1278 public List<UserInfo> getProfiles(int userHandle) { 1279 try { 1280 return mService.getProfiles(userHandle, false /* enabledOnly */); 1281 } catch (RemoteException re) { 1282 Log.w(TAG, "Could not get user list", re); 1283 return null; 1284 } 1285 } 1286 1287 /** 1288 * Requires {@link android.Manifest.permission#MANAGE_USERS} permission. 1289 * @param userId one of the two user ids to check. 1290 * @param otherUserId one of the two user ids to check. 1291 * @return true if the two user ids are in the same profile group. 1292 * @hide 1293 */ 1294 public boolean isSameProfileGroup(int userId, int otherUserId) { 1295 try { 1296 return mService.isSameProfileGroup(userId, otherUserId); 1297 } catch (RemoteException re) { 1298 Log.w(TAG, "Could not get user list", re); 1299 return false; 1300 } 1301 } 1302 1303 /** 1304 * Returns list of the profiles of userHandle including 1305 * userHandle itself. 1306 * Note that this returns only enabled. 1307 * 1308 * Requires {@link android.Manifest.permission#MANAGE_USERS} permission. 1309 * @param userHandle profiles of this user will be returned. 1310 * @return the list of profiles. 1311 * @hide 1312 */ 1313 public List<UserInfo> getEnabledProfiles(int userHandle) { 1314 try { 1315 return mService.getProfiles(userHandle, true /* enabledOnly */); 1316 } catch (RemoteException re) { 1317 Log.w(TAG, "Could not get user list", re); 1318 return null; 1319 } 1320 } 1321 1322 /** 1323 * Returns a list of UserHandles for profiles associated with the user that the calling process 1324 * is running on, including the user itself. 1325 * 1326 * @return A non-empty list of UserHandles associated with the calling user. 1327 */ 1328 public List<UserHandle> getUserProfiles() { 1329 ArrayList<UserHandle> profiles = new ArrayList<UserHandle>(); 1330 List<UserInfo> users; 1331 try { 1332 users = mService.getProfiles(UserHandle.myUserId(), true /* enabledOnly */); 1333 } catch (RemoteException re) { 1334 Log.w(TAG, "Could not get user list", re); 1335 return null; 1336 } 1337 for (UserInfo info : users) { 1338 UserHandle userHandle = new UserHandle(info.id); 1339 profiles.add(userHandle); 1340 } 1341 return profiles; 1342 } 1343 1344 /** 1345 * Returns the device credential owner id of the profile from 1346 * which this method is called, or userHandle if called from a user that 1347 * is not a profile. 1348 * 1349 * @hide 1350 */ 1351 public int getCredentialOwnerProfile(int userHandle) { 1352 try { 1353 return mService.getCredentialOwnerProfile(userHandle); 1354 } catch (RemoteException re) { 1355 Log.w(TAG, "Could not get credential owner", re); 1356 return -1; 1357 } 1358 } 1359 1360 /** 1361 * Returns the parent of the profile which this method is called from 1362 * or null if called from a user that is not a profile. 1363 * 1364 * @hide 1365 */ 1366 public UserInfo getProfileParent(int userHandle) { 1367 try { 1368 return mService.getProfileParent(userHandle); 1369 } catch (RemoteException re) { 1370 Log.w(TAG, "Could not get profile parent", re); 1371 return null; 1372 } 1373 } 1374 1375 /** 1376 * Set quiet mode of a managed profile. 1377 * 1378 * @param userHandle The user handle of the profile. 1379 * @param enableQuietMode Whether quiet mode should be enabled or disabled. 1380 * @hide 1381 */ 1382 public void setQuietModeEnabled(int userHandle, boolean enableQuietMode) { 1383 try { 1384 mService.setQuietModeEnabled(userHandle, enableQuietMode); 1385 } catch (RemoteException e) { 1386 Log.w(TAG, "Could not change the profile's quiet mode", e); 1387 } 1388 } 1389 1390 /** 1391 * Returns whether the given profile is in quiet mode or not. 1392 * 1393 * @param userHandle The user handle of the profile to be queried. 1394 * @return true if the profile is in quiet mode, false otherwise. 1395 */ 1396 public boolean isQuietModeEnabled(UserHandle userHandle) { 1397 try { 1398 return mService.isQuietModeEnabled(userHandle.getIdentifier()); 1399 } catch (RemoteException e) { 1400 Log.w(TAG, "Could not query the profile's quiet mode", e); 1401 } 1402 return false; 1403 } 1404 1405 /** 1406 * If the target user is a managed profile of the calling user or the caller 1407 * is itself a managed profile, then this returns a badged copy of the given 1408 * icon to be able to distinguish it from the original icon. For badging an 1409 * arbitrary drawable use {@link #getBadgedDrawableForUser( 1410 * android.graphics.drawable.Drawable, UserHandle, android.graphics.Rect, int)}. 1411 * <p> 1412 * If the original drawable is a BitmapDrawable and the backing bitmap is 1413 * mutable as per {@link android.graphics.Bitmap#isMutable()}, the bading 1414 * is performed in place and the original drawable is returned. 1415 * </p> 1416 * 1417 * @param icon The icon to badge. 1418 * @param user The target user. 1419 * @return A drawable that combines the original icon and a badge as 1420 * determined by the system. 1421 * @removed 1422 */ 1423 public Drawable getBadgedIconForUser(Drawable icon, UserHandle user) { 1424 return mContext.getPackageManager().getUserBadgedIcon(icon, user); 1425 } 1426 1427 /** 1428 * If the target user is a managed profile of the calling user or the caller 1429 * is itself a managed profile, then this returns a badged copy of the given 1430 * drawable allowing the user to distinguish it from the original drawable. 1431 * The caller can specify the location in the bounds of the drawable to be 1432 * badged where the badge should be applied as well as the density of the 1433 * badge to be used. 1434 * <p> 1435 * If the original drawable is a BitmapDrawable and the backing bitmap is 1436 * mutable as per {@link android.graphics.Bitmap#isMutable()}, the bading 1437 * is performed in place and the original drawable is returned. 1438 * </p> 1439 * 1440 * @param badgedDrawable The drawable to badge. 1441 * @param user The target user. 1442 * @param badgeLocation Where in the bounds of the badged drawable to place 1443 * the badge. If not provided, the badge is applied on top of the entire 1444 * drawable being badged. 1445 * @param badgeDensity The optional desired density for the badge as per 1446 * {@link android.util.DisplayMetrics#densityDpi}. If not provided, 1447 * the density of the display is used. 1448 * @return A drawable that combines the original drawable and a badge as 1449 * determined by the system. 1450 * @removed 1451 */ 1452 public Drawable getBadgedDrawableForUser(Drawable badgedDrawable, UserHandle user, 1453 Rect badgeLocation, int badgeDensity) { 1454 return mContext.getPackageManager().getUserBadgedDrawableForDensity(badgedDrawable, user, 1455 badgeLocation, badgeDensity); 1456 } 1457 1458 /** 1459 * If the target user is a managed profile of the calling user or the caller 1460 * is itself a managed profile, then this returns a copy of the label with 1461 * badging for accessibility services like talkback. E.g. passing in "Email" 1462 * and it might return "Work Email" for Email in the work profile. 1463 * 1464 * @param label The label to change. 1465 * @param user The target user. 1466 * @return A label that combines the original label and a badge as 1467 * determined by the system. 1468 * @removed 1469 */ 1470 public CharSequence getBadgedLabelForUser(CharSequence label, UserHandle user) { 1471 return mContext.getPackageManager().getUserBadgedLabel(label, user); 1472 } 1473 1474 /** 1475 * Returns information for all users on this device. Requires 1476 * {@link android.Manifest.permission#MANAGE_USERS} permission. 1477 * 1478 * @param excludeDying specify if the list should exclude users being 1479 * removed. 1480 * @return the list of users that were created. 1481 * @hide 1482 */ 1483 public List<UserInfo> getUsers(boolean excludeDying) { 1484 try { 1485 return mService.getUsers(excludeDying); 1486 } catch (RemoteException re) { 1487 Log.w(TAG, "Could not get user list", re); 1488 return null; 1489 } 1490 } 1491 1492 /** 1493 * Removes a user and all associated data. 1494 * Requires {@link android.Manifest.permission#MANAGE_USERS} permission. 1495 * @param userHandle the integer handle of the user, where 0 is the primary user. 1496 * @hide 1497 */ 1498 public boolean removeUser(int userHandle) { 1499 try { 1500 return mService.removeUser(userHandle); 1501 } catch (RemoteException re) { 1502 Log.w(TAG, "Could not remove user ", re); 1503 return false; 1504 } 1505 } 1506 1507 /** 1508 * Updates the user's name. 1509 * Requires {@link android.Manifest.permission#MANAGE_USERS} permission. 1510 * 1511 * @param userHandle the user's integer handle 1512 * @param name the new name for the user 1513 * @hide 1514 */ 1515 public void setUserName(int userHandle, String name) { 1516 try { 1517 mService.setUserName(userHandle, name); 1518 } catch (RemoteException re) { 1519 Log.w(TAG, "Could not set the user name ", re); 1520 } 1521 } 1522 1523 /** 1524 * Sets the user's photo. 1525 * @param userHandle the user for whom to change the photo. 1526 * @param icon the bitmap to set as the photo. 1527 * @hide 1528 */ 1529 public void setUserIcon(int userHandle, Bitmap icon) { 1530 try { 1531 mService.setUserIcon(userHandle, icon); 1532 } catch (RemoteException re) { 1533 Log.w(TAG, "Could not set the user icon ", re); 1534 } 1535 } 1536 1537 /** 1538 * Returns a file descriptor for the user's photo. PNG data can be read from this file. 1539 * @param userHandle the user whose photo we want to read. 1540 * @return a {@link Bitmap} of the user's photo, or null if there's no photo. 1541 * @see com.android.internal.util.UserIcons#getDefaultUserIcon for a default. 1542 * @hide 1543 */ 1544 public Bitmap getUserIcon(int userHandle) { 1545 try { 1546 ParcelFileDescriptor fd = mService.getUserIcon(userHandle); 1547 if (fd != null) { 1548 try { 1549 return BitmapFactory.decodeFileDescriptor(fd.getFileDescriptor()); 1550 } finally { 1551 try { 1552 fd.close(); 1553 } catch (IOException e) { 1554 } 1555 } 1556 } 1557 } catch (RemoteException re) { 1558 Log.w(TAG, "Could not get the user icon ", re); 1559 } 1560 return null; 1561 } 1562 1563 /** 1564 * Returns the maximum number of users that can be created on this device. A return value 1565 * of 1 means that it is a single user device. 1566 * @hide 1567 * @return a value greater than or equal to 1 1568 */ 1569 public static int getMaxSupportedUsers() { 1570 // Don't allow multiple users on certain builds 1571 if (android.os.Build.ID.startsWith("JVP")) return 1; 1572 // Svelte devices don't get multi-user. 1573 if (ActivityManager.isLowRamDeviceStatic()) return 1; 1574 return SystemProperties.getInt("fw.max_users", 1575 Resources.getSystem().getInteger(R.integer.config_multiuserMaximumUsers)); 1576 } 1577 1578 /** 1579 * Returns true if the user switcher should be shown, this will be if device supports multi-user 1580 * and there are at least 2 users available that are not managed profiles. 1581 * @hide 1582 * @return true if user switcher should be shown. 1583 */ 1584 public boolean isUserSwitcherEnabled() { 1585 if (!supportsMultipleUsers()) { 1586 return false; 1587 } 1588 List<UserInfo> users = getUsers(true); 1589 if (users == null) { 1590 return false; 1591 } 1592 int switchableUserCount = 0; 1593 for (UserInfo user : users) { 1594 if (user.supportsSwitchToByUser()) { 1595 ++switchableUserCount; 1596 } 1597 } 1598 final boolean guestEnabled = !mContext.getSystemService(DevicePolicyManager.class) 1599 .getGuestUserDisabled(null); 1600 return switchableUserCount > 1 || guestEnabled; 1601 } 1602 1603 /** 1604 * Returns a serial number on this device for a given userHandle. User handles can be recycled 1605 * when deleting and creating users, but serial numbers are not reused until the device is wiped. 1606 * @param userHandle 1607 * @return a serial number associated with that user, or -1 if the userHandle is not valid. 1608 * @hide 1609 */ 1610 public int getUserSerialNumber(int userHandle) { 1611 try { 1612 return mService.getUserSerialNumber(userHandle); 1613 } catch (RemoteException re) { 1614 Log.w(TAG, "Could not get serial number for user " + userHandle); 1615 } 1616 return -1; 1617 } 1618 1619 /** 1620 * Returns a userHandle on this device for a given user serial number. User handles can be 1621 * recycled when deleting and creating users, but serial numbers are not reused until the device 1622 * is wiped. 1623 * @param userSerialNumber 1624 * @return the userHandle associated with that user serial number, or -1 if the serial number 1625 * is not valid. 1626 * @hide 1627 */ 1628 public int getUserHandle(int userSerialNumber) { 1629 try { 1630 return mService.getUserHandle(userSerialNumber); 1631 } catch (RemoteException re) { 1632 Log.w(TAG, "Could not get userHandle for user " + userSerialNumber); 1633 } 1634 return -1; 1635 } 1636 1637 /** 1638 * Returns a {@code Bundle} containing any saved application restrictions for this user, for the 1639 * given package name. Only an application with this package name can call this method. 1640 * 1641 * <p>The returned {@link Bundle} consists of key-value pairs, as defined by the application, 1642 * where the types of values may be: 1643 * <ul> 1644 * <li>{@code boolean} 1645 * <li>{@code int} 1646 * <li>{@code String} or {@code String[]} 1647 * <li>From {@link android.os.Build.VERSION_CODES#M}, {@code Bundle} or {@code Bundle[]} 1648 * </ul> 1649 * 1650 * @param packageName the package name of the calling application 1651 * @return a {@code Bundle} with the restrictions for that package, or {@code null} if there 1652 * are no saved restrictions. 1653 * 1654 * @see #KEY_RESTRICTIONS_PENDING 1655 */ 1656 public Bundle getApplicationRestrictions(String packageName) { 1657 try { 1658 return mService.getApplicationRestrictions(packageName); 1659 } catch (RemoteException re) { 1660 Log.w(TAG, "Could not get application restrictions for package " + packageName); 1661 } 1662 return null; 1663 } 1664 1665 /** 1666 * @hide 1667 */ 1668 public Bundle getApplicationRestrictions(String packageName, UserHandle user) { 1669 try { 1670 return mService.getApplicationRestrictionsForUser(packageName, user.getIdentifier()); 1671 } catch (RemoteException re) { 1672 Log.w(TAG, "Could not get application restrictions for user " + user.getIdentifier()); 1673 } 1674 return null; 1675 } 1676 1677 /** 1678 * @hide 1679 */ 1680 public void setApplicationRestrictions(String packageName, Bundle restrictions, 1681 UserHandle user) { 1682 try { 1683 mService.setApplicationRestrictions(packageName, restrictions, user.getIdentifier()); 1684 } catch (RemoteException re) { 1685 Log.w(TAG, "Could not set application restrictions for user " + user.getIdentifier()); 1686 } 1687 } 1688 1689 /** 1690 * Sets a new challenge PIN for restrictions. This is only for use by pre-installed 1691 * apps and requires the MANAGE_USERS permission. 1692 * @param newPin the PIN to use for challenge dialogs. 1693 * @return Returns true if the challenge PIN was set successfully. 1694 * @deprecated The restrictions PIN functionality is no longer provided by the system. 1695 * This method is preserved for backwards compatibility reasons and always returns false. 1696 */ 1697 public boolean setRestrictionsChallenge(String newPin) { 1698 return false; 1699 } 1700 1701 /** 1702 * @hide 1703 * Set restrictions that should apply to any future guest user that's created. 1704 */ 1705 public void setDefaultGuestRestrictions(Bundle restrictions) { 1706 try { 1707 mService.setDefaultGuestRestrictions(restrictions); 1708 } catch (RemoteException re) { 1709 Log.w(TAG, "Could not set guest restrictions"); 1710 } 1711 } 1712 1713 /** 1714 * @hide 1715 * Gets the default guest restrictions. 1716 */ 1717 public Bundle getDefaultGuestRestrictions() { 1718 try { 1719 return mService.getDefaultGuestRestrictions(); 1720 } catch (RemoteException re) { 1721 Log.w(TAG, "Could not set guest restrictions"); 1722 } 1723 return new Bundle(); 1724 } 1725 1726 /** 1727 * Returns creation time of the user or of a managed profile associated with the calling user. 1728 * @param userHandle user handle of the user or a managed profile associated with the 1729 * calling user. 1730 * @return creation time in milliseconds since Epoch time. 1731 */ 1732 public long getUserCreationTime(UserHandle userHandle) { 1733 try { 1734 return mService.getUserCreationTime(userHandle.getIdentifier()); 1735 } catch (RemoteException re) { 1736 Log.w(TAG, "Could not get user creation time", re); 1737 return 0; 1738 } 1739 } 1740} 1741