| 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 | */ |
| 16 | |
| 17 | package android.os; |
| 18 | |
| 19 | import static android.app.admin.DevicePolicyResources.Strings.Core.WORK_PROFILE_BADGED_LABEL; |
| 20 | import static android.app.admin.DevicePolicyResources.UNDEFINED; |
| 21 | |
| 22 | import android.Manifest; |
| 23 | import android.accounts.AccountManager; |
| 24 | import android.annotation.ColorInt; |
| 25 | import android.annotation.DrawableRes; |
| 26 | import android.annotation.IntDef; |
| 27 | import android.annotation.NonNull; |
| 28 | import android.annotation.Nullable; |
| 29 | import android.annotation.RequiresPermission; |
| 30 | import android.annotation.StringDef; |
| 31 | import android.annotation.SuppressAutoDoc; |
| 32 | import android.annotation.SuppressLint; |
| 33 | import android.annotation.SystemApi; |
| 34 | import android.annotation.SystemService; |
| 35 | import android.annotation.TestApi; |
| 36 | import android.annotation.UserHandleAware; |
| 37 | import android.annotation.UserIdInt; |
| 38 | import android.annotation.WorkerThread; |
| 39 | import android.app.Activity; |
| 40 | import android.app.ActivityManager; |
| 41 | import android.app.PropertyInvalidatedCache; |
| 42 | import android.app.admin.DevicePolicyManager; |
| 43 | import android.app.compat.CompatChanges; |
| 44 | import android.compat.annotation.ChangeId; |
| 45 | import android.compat.annotation.EnabledSince; |
| 46 | import android.compat.annotation.UnsupportedAppUsage; |
| 47 | import android.content.ComponentName; |
| 48 | import android.content.Context; |
| 49 | import android.content.Intent; |
| 50 | import android.content.IntentFilter; |
| 51 | import android.content.IntentSender; |
| 52 | import android.content.pm.UserInfo; |
| 53 | import android.content.pm.UserInfo.UserInfoFlag; |
| 54 | import android.content.res.Resources; |
| 55 | import android.graphics.Bitmap; |
| 56 | import android.graphics.BitmapFactory; |
| 57 | import android.graphics.Rect; |
| 58 | import android.graphics.drawable.Drawable; |
| 59 | import android.location.LocationManager; |
| 60 | import android.provider.Settings; |
| 61 | import android.telephony.TelephonyManager; |
| 62 | import android.util.AndroidException; |
| 63 | import android.util.ArraySet; |
| 64 | import android.util.Log; |
| 65 | import android.view.WindowManager.LayoutParams; |
| 66 | |
| 67 | import com.android.internal.R; |
| 68 | import com.android.internal.os.RoSystemProperties; |
| 69 | import com.android.internal.util.FrameworkStatsLog; |
| 70 | |
| 71 | import java.io.IOException; |
| 72 | import java.lang.annotation.Retention; |
| 73 | import java.lang.annotation.RetentionPolicy; |
| 74 | import java.util.ArrayList; |
| 75 | import java.util.List; |
| 76 | import java.util.Objects; |
| 77 | import java.util.Set; |
| 78 | |
| 79 | /** |
| 80 | * Manages users and user details on a multi-user system. There are two major categories of |
| 81 | * users: fully customizable users with their own login, and profiles that share a workspace |
| 82 | * with a related user. |
| 83 | * <p> |
| 84 | * Users are different from accounts, which are managed by |
| 85 | * {@link AccountManager}. Each user can have their own set of accounts. |
| 86 | * <p> |
| 87 | * See {@link DevicePolicyManager#ACTION_PROVISION_MANAGED_PROFILE} for more on managed profiles. |
| 88 | */ |
| 89 | @SystemService(Context.USER_SERVICE) |
| 90 | public class UserManager { |
| 91 | |
| 92 | private static final String TAG = "UserManager"; |
| 93 | @UnsupportedAppUsage |
| 94 | private final IUserManager mService; |
| 95 | /** Holding the Application context (not constructor param context). */ |
| 96 | private final Context mContext; |
| 97 | |
| 98 | /** The userId of the constructor param context. To be used instead of mContext.getUserId(). */ |
| 99 | private final @UserIdInt int mUserId; |
| 100 | |
| 101 | /** The userType of UserHandle.myUserId(); empty string if not a profile; null until cached. */ |
| 102 | private String mProfileTypeOfProcessUser = null; |
| 103 | |
| 104 | /** |
| 105 | * User type representing a {@link UserHandle#USER_SYSTEM system} user that is a human user. |
| 106 | * This type of user cannot be created; it can only pre-exist on first boot. |
| 107 | * @hide |
| 108 | */ |
| 109 | @SystemApi |
| 110 | public static final String USER_TYPE_FULL_SYSTEM = "android.os.usertype.full.SYSTEM"; |
| 111 | |
| 112 | /** |
| 113 | * User type representing a regular non-profile non-{@link UserHandle#USER_SYSTEM system} human |
| 114 | * user. |
| 115 | * This is sometimes called an ordinary 'secondary user'. |
| 116 | * @hide |
| 117 | */ |
| 118 | @SystemApi |
| 119 | public static final String USER_TYPE_FULL_SECONDARY = "android.os.usertype.full.SECONDARY"; |
| 120 | |
| 121 | /** |
| 122 | * User type representing a guest user that may be transient. |
| 123 | * @hide |
| 124 | */ |
| 125 | @SystemApi |
| 126 | public static final String USER_TYPE_FULL_GUEST = "android.os.usertype.full.GUEST"; |
| 127 | |
| 128 | /** |
| 129 | * User type representing a user for demo purposes only, which can be removed at any time. |
| 130 | * @hide |
| 131 | */ |
| 132 | public static final String USER_TYPE_FULL_DEMO = "android.os.usertype.full.DEMO"; |
| 133 | |
| 134 | /** |
| 135 | * User type representing a "restricted profile" user, which is a full user that is subject to |
| 136 | * certain restrictions from a parent user. Note, however, that it is NOT technically a profile. |
| 137 | * @hide |
| 138 | */ |
| 139 | public static final String USER_TYPE_FULL_RESTRICTED = "android.os.usertype.full.RESTRICTED"; |
| 140 | |
| 141 | /** |
| 142 | * User type representing a managed profile, which is a profile that is to be managed by a |
| 143 | * device policy controller (DPC). |
| 144 | * The intended purpose is for work profiles, which are managed by a corporate entity. |
| 145 | * @hide |
| 146 | */ |
| 147 | @SystemApi |
| 148 | public static final String USER_TYPE_PROFILE_MANAGED = "android.os.usertype.profile.MANAGED"; |
| 149 | |
| 150 | /** |
| 151 | * User type representing a clone profile. Clone profile is a user profile type used to run |
| 152 | * second instance of an otherwise single user App (eg, messengers). Only the primary user |
| 153 | * is allowed to have a clone profile. |
| 154 | * |
| 155 | * @hide |
| 156 | */ |
| 157 | @SystemApi |
| 158 | public static final String USER_TYPE_PROFILE_CLONE = "android.os.usertype.profile.CLONE"; |
| 159 | |
| 160 | /** |
| 161 | * User type representing a generic profile for testing purposes. Only on debuggable builds. |
| 162 | * @hide |
| 163 | */ |
| 164 | public static final String USER_TYPE_PROFILE_TEST = "android.os.usertype.profile.TEST"; |
| 165 | |
| 166 | /** |
| 167 | * User type representing a {@link UserHandle#USER_SYSTEM system} user that is <b>not</b> a |
| 168 | * human user. |
| 169 | * This type of user cannot be created; it can only pre-exist on first boot. |
| 170 | * @hide |
| 171 | */ |
| 172 | @SystemApi |
| 173 | public static final String USER_TYPE_SYSTEM_HEADLESS = "android.os.usertype.system.HEADLESS"; |
| 174 | |
| 175 | /** |
| 176 | * Flag passed to {@link #requestQuietModeEnabled} to request disabling quiet mode only if |
| 177 | * there is no need to confirm the user credentials. If credentials are required to disable |
| 178 | * quiet mode, {@link #requestQuietModeEnabled} will do nothing and return {@code false}. |
| 179 | */ |
| 180 | public static final int QUIET_MODE_DISABLE_ONLY_IF_CREDENTIAL_NOT_REQUIRED = 0x1; |
| 181 | |
| 182 | /** |
| 183 | * Flag passed to {@link #requestQuietModeEnabled} to request disabling quiet mode without |
| 184 | * asking for credentials. This is used when managed profile password is forgotten. It starts |
| 185 | * the user in locked state so that a direct boot aware DPC could reset the password. |
| 186 | * Should not be used together with |
| 187 | * {@link #QUIET_MODE_DISABLE_ONLY_IF_CREDENTIAL_NOT_REQUIRED} or an exception will be thrown. |
| 188 | * @hide |
| 189 | */ |
| 190 | public static final int QUIET_MODE_DISABLE_DONT_ASK_CREDENTIAL = 0x2; |
| 191 | |
| 192 | /** |
| 193 | * List of flags available for the {@link #requestQuietModeEnabled} method. |
| 194 | * @hide |
| 195 | */ |
| 196 | @Retention(RetentionPolicy.SOURCE) |
| 197 | @IntDef(flag = true, prefix = { "QUIET_MODE_" }, value = { |
| 198 | QUIET_MODE_DISABLE_ONLY_IF_CREDENTIAL_NOT_REQUIRED, |
| 199 | QUIET_MODE_DISABLE_DONT_ASK_CREDENTIAL}) |
| 200 | public @interface QuietModeFlag {} |
| 201 | |
| 202 | /** |
| 203 | * @hide |
| 204 | * No user restriction. |
| 205 | */ |
| 206 | @SystemApi |
| 207 | public static final int RESTRICTION_NOT_SET = 0x0; |
| 208 | |
| 209 | /** |
| 210 | * @hide |
| 211 | * User restriction set by system/user. |
| 212 | */ |
| 213 | @SystemApi |
| 214 | public static final int RESTRICTION_SOURCE_SYSTEM = 0x1; |
| 215 | |
| 216 | /** |
| 217 | * @hide |
| 218 | * User restriction set by a device owner. |
| 219 | */ |
| 220 | @SystemApi |
| 221 | public static final int RESTRICTION_SOURCE_DEVICE_OWNER = 0x2; |
| 222 | |
| 223 | /** |
| 224 | * @hide |
| 225 | * User restriction set by a profile owner. |
| 226 | */ |
| 227 | @SystemApi |
| 228 | public static final int RESTRICTION_SOURCE_PROFILE_OWNER = 0x4; |
| 229 | |
| 230 | /** @hide */ |
| 231 | @Retention(RetentionPolicy.SOURCE) |
| 232 | @IntDef(flag = true, prefix = { "RESTRICTION_" }, value = { |
| 233 | RESTRICTION_NOT_SET, |
| 234 | RESTRICTION_SOURCE_SYSTEM, |
| 235 | RESTRICTION_SOURCE_DEVICE_OWNER, |
| 236 | RESTRICTION_SOURCE_PROFILE_OWNER |
| 237 | }) |
| 238 | @SystemApi |
| 239 | public @interface UserRestrictionSource {} |
| 240 | |
| 241 | /** |
| 242 | * Specifies if a user is disallowed from adding and removing accounts, unless they are |
| 243 | * {@link android.accounts.AccountManager#addAccountExplicitly programmatically} added by |
| 244 | * Authenticator. |
| 245 | * The default value is <code>false</code>. |
| 246 | * |
| 247 | * <p>From {@link android.os.Build.VERSION_CODES#N} a profile or device owner app can still |
| 248 | * use {@link android.accounts.AccountManager} APIs to add or remove accounts when account |
| 249 | * management is disallowed. |
| 250 | * |
| 251 | * <p>Key for user restrictions. |
| 252 | * <p>Type: Boolean |
| 253 | * @see DevicePolicyManager#addUserRestriction(ComponentName, String) |
| 254 | * @see DevicePolicyManager#clearUserRestriction(ComponentName, String) |
| 255 | * @see #getUserRestrictions() |
| 256 | */ |
| 257 | public static final String DISALLOW_MODIFY_ACCOUNTS = "no_modify_accounts"; |
| 258 | |
| 259 | /** |
| 260 | * Specifies if a user is disallowed from changing Wi-Fi access points via Settings. This |
| 261 | * restriction does not affect Wi-Fi tethering settings. |
| 262 | * |
| 263 | * <p>A device owner and a profile owner can set this restriction, although the restriction has |
| 264 | * no effect in a managed profile. When it is set by a device owner, a profile owner on the |
| 265 | * primary user or by a profile owner of an organization-owned managed profile on the parent |
| 266 | * profile, it disallows the primary user from changing Wi-Fi access points. |
| 267 | * |
| 268 | * <p>The default value is <code>false</code>. |
| 269 | * |
| 270 | * <p>Key for user restrictions. |
| 271 | * <p>Type: Boolean |
| 272 | * @see DevicePolicyManager#addUserRestriction(ComponentName, String) |
| 273 | * @see DevicePolicyManager#clearUserRestriction(ComponentName, String) |
| 274 | * @see #getUserRestrictions() |
| 275 | */ |
| 276 | public static final String DISALLOW_CONFIG_WIFI = "no_config_wifi"; |
| 277 | |
| 278 | /** |
| 279 | * Specifies if a user is disallowed from enabling/disabling Wi-Fi. |
| 280 | * |
| 281 | * <p>This restriction can only be set by a device owner, |
| 282 | * a profile owner of an organization-owned managed profile on the parent profile. |
| 283 | * When it is set by any of these owners, it applies globally - i.e., it disables airplane mode |
| 284 | * from changing Wi-Fi state. |
| 285 | * |
| 286 | * <p>The default value is <code>false</code>. |
| 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_CHANGE_WIFI_STATE = "no_change_wifi_state"; |
| 295 | |
| 296 | /** |
| 297 | * Specifies if a user is disallowed from using Wi-Fi tethering. |
| 298 | * |
| 299 | * <p>This restriction does not limit the user's ability to modify or connect to regular |
| 300 | * Wi-Fi networks, which is separately controlled by {@link #DISALLOW_CONFIG_WIFI}. |
| 301 | * |
| 302 | * <p>This restriction can only be set by a device owner, |
| 303 | * a profile owner of an organization-owned managed profile on the parent profile. |
| 304 | * When it is set by any of these owners, it prevents all users from using |
| 305 | * Wi-Fi tethering. Other forms of tethering are not affected. |
| 306 | * |
| 307 | * This user restriction disables only Wi-Fi tethering. |
| 308 | * Use {@link #DISALLOW_CONFIG_TETHERING} to limit all forms of tethering. |
| 309 | * When {@link #DISALLOW_CONFIG_TETHERING} is set, this user restriction becomes obsolete. |
| 310 | * |
| 311 | * <p>The default value is <code>false</code>. |
| 312 | * |
| 313 | * <p>Key for user restrictions. |
| 314 | * <p>Type: Boolean |
| 315 | * @see DevicePolicyManager#addUserRestriction(ComponentName, String) |
| 316 | * @see DevicePolicyManager#clearUserRestriction(ComponentName, String) |
| 317 | * @see #getUserRestrictions() |
| 318 | */ |
| 319 | public static final String DISALLOW_WIFI_TETHERING = "no_wifi_tethering"; |
| 320 | |
| 321 | /** |
| 322 | * Specifies if users are disallowed from sharing Wi-Fi for admin configured networks. |
| 323 | * |
| 324 | * <p>Device owner and profile owner can set this restriction. |
| 325 | * When it is set by any of these owners, it prevents all users from |
| 326 | * sharing Wi-Fi for networks configured by these owners. |
| 327 | * Other networks not configured by these owners are not affected. |
| 328 | * |
| 329 | * <p>The default value is <code>false</code>. |
| 330 | * |
| 331 | * <p>Key for user restrictions. |
| 332 | * <p>Type: Boolean |
| 333 | * @see DevicePolicyManager#addUserRestriction(ComponentName, String) |
| 334 | * @see DevicePolicyManager#clearUserRestriction(ComponentName, String) |
| 335 | * @see #getUserRestrictions() |
| 336 | */ |
| 337 | public static final String DISALLOW_SHARING_ADMIN_CONFIGURED_WIFI = |
| 338 | "no_sharing_admin_configured_wifi"; |
| 339 | |
| 340 | /** |
| 341 | * Specifies if a user is disallowed from using Wi-Fi Direct. |
| 342 | * |
| 343 | * <p>This restriction can only be set by a device owner, |
| 344 | * a profile owner of an organization-owned managed profile on the parent profile. |
| 345 | * When it is set by any of these owners, it prevents all users from using |
| 346 | * Wi-Fi Direct. |
| 347 | * |
| 348 | * <p>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_WIFI_DIRECT = "no_wifi_direct"; |
| 357 | |
| 358 | /** |
| 359 | * Specifies if a user is disallowed from adding a new Wi-Fi configuration. |
| 360 | * |
| 361 | * <p>This restriction can only be set by a device owner, |
| 362 | * a profile owner of an organization-owned managed profile on the parent profile. |
| 363 | * When it is set by any of these owners, it prevents all users from adding |
| 364 | * a new Wi-Fi configuration. This does not limit the owner and carrier's ability |
| 365 | * to add a new configuration. |
| 366 | * |
| 367 | * <p>The default value is <code>false</code>. |
| 368 | * |
| 369 | * <p>Key for user restrictions. |
| 370 | * <p>Type: Boolean |
| 371 | * @see DevicePolicyManager#addUserRestriction(ComponentName, String) |
| 372 | * @see DevicePolicyManager#clearUserRestriction(ComponentName, String) |
| 373 | * @see #getUserRestrictions() |
| 374 | */ |
| 375 | public static final String DISALLOW_ADD_WIFI_CONFIG = "no_add_wifi_config"; |
| 376 | |
| 377 | /** |
| 378 | * Specifies if a user is disallowed from changing the device |
| 379 | * language. The default value is <code>false</code>. |
| 380 | * |
| 381 | * <p>Key for user restrictions. |
| 382 | * <p>Type: Boolean |
| 383 | * @see DevicePolicyManager#addUserRestriction(ComponentName, String) |
| 384 | * @see DevicePolicyManager#clearUserRestriction(ComponentName, String) |
| 385 | * @see #getUserRestrictions() |
| 386 | */ |
| 387 | public static final String DISALLOW_CONFIG_LOCALE = "no_config_locale"; |
| 388 | |
| 389 | /** |
| 390 | * Specifies if a user is disallowed from installing applications. This user restriction also |
| 391 | * prevents device owners and profile owners installing apps. The default value is |
| 392 | * {@code false}. |
| 393 | * |
| 394 | * <p>Key for user restrictions. |
| 395 | * <p>Type: Boolean |
| 396 | * @see DevicePolicyManager#addUserRestriction(ComponentName, String) |
| 397 | * @see DevicePolicyManager#clearUserRestriction(ComponentName, String) |
| 398 | * @see #getUserRestrictions() |
| 399 | */ |
| 400 | public static final String DISALLOW_INSTALL_APPS = "no_install_apps"; |
| 401 | |
| 402 | /** |
| 403 | * Specifies if a user is disallowed from uninstalling applications. |
| 404 | * The default value is <code>false</code>. |
| 405 | * |
| 406 | * <p>Key for user restrictions. |
| 407 | * <p>Type: Boolean |
| 408 | * @see DevicePolicyManager#addUserRestriction(ComponentName, String) |
| 409 | * @see DevicePolicyManager#clearUserRestriction(ComponentName, String) |
| 410 | * @see #getUserRestrictions() |
| 411 | */ |
| 412 | public static final String DISALLOW_UNINSTALL_APPS = "no_uninstall_apps"; |
| 413 | |
| 414 | /** |
| 415 | * Specifies if a user is disallowed from turning on location sharing. |
| 416 | * |
| 417 | * <p>In a managed profile, location sharing by default reflects the primary user's setting, but |
| 418 | * can be overridden and forced off by setting this restriction to true in the managed profile. |
| 419 | * |
| 420 | * <p>A device owner and a profile owner can set this restriction. When it is set by a device |
| 421 | * owner, a profile owner on the primary user or by a profile owner of an organization-owned |
| 422 | * managed profile on the parent profile, it prevents the primary user from turning on |
| 423 | * location sharing. |
| 424 | * |
| 425 | * <p>The default value is <code>false</code>. |
| 426 | * |
| 427 | * <p>Key for user restrictions. |
| 428 | * <p>Type: Boolean |
| 429 | * @see DevicePolicyManager#addUserRestriction(ComponentName, String) |
| 430 | * @see DevicePolicyManager#clearUserRestriction(ComponentName, String) |
| 431 | * @see #getUserRestrictions() |
| 432 | */ |
| 433 | public static final String DISALLOW_SHARE_LOCATION = "no_share_location"; |
| 434 | |
| 435 | /** |
| 436 | * Specifies if airplane mode is disallowed on the device. |
| 437 | * |
| 438 | * <p>This restriction can only be set by a device owner, a profile owner on the primary |
| 439 | * user or a profile owner of an organization-owned managed profile on the parent profile. |
| 440 | * When it is set by any of these owners, it applies globally - i.e., it disables airplane mode |
| 441 | * on the entire device. |
| 442 | * |
| 443 | * <p>The default value is <code>false</code>. |
| 444 | * |
| 445 | * <p>Key for user restrictions. |
| 446 | * <p>Type: Boolean |
| 447 | * @see DevicePolicyManager#addUserRestriction(ComponentName, String) |
| 448 | * @see DevicePolicyManager#clearUserRestriction(ComponentName, String) |
| 449 | * @see #getUserRestrictions() |
| 450 | */ |
| 451 | public static final String DISALLOW_AIRPLANE_MODE = "no_airplane_mode"; |
| 452 | |
| 453 | /** |
| 454 | * Specifies if a user is disallowed from configuring brightness. When device owner sets it, |
| 455 | * it'll only be applied on the target(system) user. |
| 456 | * |
| 457 | * <p>The default value is <code>false</code>. |
| 458 | * |
| 459 | * <p>This user restriction has no effect on managed profiles. |
| 460 | * <p>Key for user restrictions. |
| 461 | * <p>Type: Boolean |
| 462 | * @see DevicePolicyManager#addUserRestriction(ComponentName, String) |
| 463 | * @see DevicePolicyManager#clearUserRestriction(ComponentName, String) |
| 464 | * @see #getUserRestrictions() |
| 465 | */ |
| 466 | public static final String DISALLOW_CONFIG_BRIGHTNESS = "no_config_brightness"; |
| 467 | |
| 468 | /** |
| 469 | * Specifies if ambient display is disallowed for the user. |
| 470 | * |
| 471 | * <p>The default value is <code>false</code>. |
| 472 | * |
| 473 | * <p>This user restriction has no effect on managed profiles. |
| 474 | * <p>Key for user restrictions. |
| 475 | * <p>Type: Boolean |
| 476 | * @see DevicePolicyManager#addUserRestriction(ComponentName, String) |
| 477 | * @see DevicePolicyManager#clearUserRestriction(ComponentName, String) |
| 478 | * @see #getUserRestrictions() |
| 479 | */ |
| 480 | public static final String DISALLOW_AMBIENT_DISPLAY = "no_ambient_display"; |
| 481 | |
| 482 | /** |
| 483 | * Specifies if a user is disallowed from changing screen off timeout. |
| 484 | * |
| 485 | * <p>The default value is <code>false</code>. |
| 486 | * |
| 487 | * <p>This user restriction has no effect on managed profiles. |
| 488 | * <p>Key for user restrictions. |
| 489 | * <p>Type: Boolean |
| 490 | * @see DevicePolicyManager#addUserRestriction(ComponentName, String) |
| 491 | * @see DevicePolicyManager#clearUserRestriction(ComponentName, String) |
| 492 | * @see #getUserRestrictions() |
| 493 | */ |
| 494 | public static final String DISALLOW_CONFIG_SCREEN_TIMEOUT = "no_config_screen_timeout"; |
| 495 | |
| 496 | /** |
| 497 | * Specifies if a user is disallowed from enabling the |
| 498 | * "Unknown Sources" setting, that allows installation of apps from unknown sources. |
| 499 | * Unknown sources exclude adb and special apps such as trusted app stores. |
| 500 | * The default value is <code>false</code>. |
| 501 | * |
| 502 | * <p>Key for user restrictions. |
| 503 | * <p>Type: Boolean |
| 504 | * @see DevicePolicyManager#addUserRestriction(ComponentName, String) |
| 505 | * @see DevicePolicyManager#clearUserRestriction(ComponentName, String) |
| 506 | * @see #getUserRestrictions() |
| 507 | */ |
| 508 | public static final String DISALLOW_INSTALL_UNKNOWN_SOURCES = "no_install_unknown_sources"; |
| 509 | |
| 510 | /** |
| 511 | * This restriction is a device-wide version of {@link #DISALLOW_INSTALL_UNKNOWN_SOURCES}. |
| 512 | * |
| 513 | * Specifies if all users on the device are disallowed from enabling the |
| 514 | * "Unknown Sources" setting, that allows installation of apps from unknown sources. |
| 515 | * |
| 516 | * This restriction can be enabled by the profile owner, in which case all accounts and |
| 517 | * profiles will be affected. |
| 518 | * |
| 519 | * The default value is <code>false</code>. |
| 520 | * |
| 521 | * <p>Key for user restrictions. |
| 522 | * <p>Type: Boolean |
| 523 | * @see DevicePolicyManager#addUserRestriction(ComponentName, String) |
| 524 | * @see DevicePolicyManager#clearUserRestriction(ComponentName, String) |
| 525 | * @see #getUserRestrictions() |
| 526 | */ |
| 527 | public static final String DISALLOW_INSTALL_UNKNOWN_SOURCES_GLOBALLY = |
| 528 | "no_install_unknown_sources_globally"; |
| 529 | |
| 530 | /** |
| 531 | * Specifies if a user is disallowed from configuring bluetooth via Settings. This does |
| 532 | * <em>not</em> restrict the user from turning bluetooth on or off. |
| 533 | * |
| 534 | * <p>This restriction doesn't prevent the user from using bluetooth. For disallowing usage of |
| 535 | * bluetooth completely on the device, use {@link #DISALLOW_BLUETOOTH}. |
| 536 | * |
| 537 | * <p>A device owner and a profile owner can set this restriction, although the restriction has |
| 538 | * no effect in a managed profile. When it is set by a device owner, a profile owner on the |
| 539 | * primary user or by a profile owner of an organization-owned managed profile on the parent |
| 540 | * profile, it disallows the primary user from configuring bluetooth. |
| 541 | * |
| 542 | * <p>The default value is <code>false</code>. |
| 543 | * |
| 544 | * <p>Key for user restrictions. |
| 545 | * <p>Type: Boolean |
| 546 | * @see DevicePolicyManager#addUserRestriction(ComponentName, String) |
| 547 | * @see DevicePolicyManager#clearUserRestriction(ComponentName, String) |
| 548 | * @see #getUserRestrictions() |
| 549 | */ |
| 550 | public static final String DISALLOW_CONFIG_BLUETOOTH = "no_config_bluetooth"; |
| 551 | |
| 552 | /** |
| 553 | * Specifies if bluetooth is disallowed on the device. If bluetooth is disallowed on the device, |
| 554 | * bluetooth cannot be turned on or configured via Settings. |
| 555 | * |
| 556 | * <p>This restriction can only be set by a device owner, a profile owner on the primary |
| 557 | * user or a profile owner of an organization-owned managed profile on the parent profile. |
| 558 | * When it is set by a device owner, it applies globally - i.e., it disables bluetooth on |
| 559 | * the entire device and all users will be affected. When it is set by a profile owner on the |
| 560 | * primary user or by a profile owner of an organization-owned managed profile on the parent |
| 561 | * profile, it disables the primary user from using bluetooth and configuring bluetooth |
| 562 | * in Settings. |
| 563 | * |
| 564 | * <p>The default value is <code>false</code>. |
| 565 | * |
| 566 | * <p>Key for user restrictions. |
| 567 | * <p>Type: Boolean |
| 568 | * @see DevicePolicyManager#addUserRestriction(ComponentName, String) |
| 569 | * @see DevicePolicyManager#clearUserRestriction(ComponentName, String) |
| 570 | * @see #getUserRestrictions() |
| 571 | */ |
| 572 | public static final String DISALLOW_BLUETOOTH = "no_bluetooth"; |
| 573 | |
| 574 | /** |
| 575 | * Specifies if outgoing bluetooth sharing is disallowed. |
| 576 | * |
| 577 | * <p>A device owner and a profile owner can set this restriction. When it is set by a device |
| 578 | * owner, it applies globally. When it is set by a profile owner on the primary user or by a |
| 579 | * profile owner of an organization-owned managed profile on the parent profile, it disables |
| 580 | * the primary user from any outgoing bluetooth sharing. |
| 581 | * |
| 582 | * <p>Default is <code>true</code> for managed profiles and false otherwise. |
| 583 | * |
| 584 | * <p>When a device upgrades to {@link android.os.Build.VERSION_CODES#O}, the system sets it |
| 585 | * for all existing managed profiles. |
| 586 | * |
| 587 | * <p>Key for user restrictions. |
| 588 | * <p>Type: Boolean |
| 589 | * @see DevicePolicyManager#addUserRestriction(ComponentName, String) |
| 590 | * @see DevicePolicyManager#clearUserRestriction(ComponentName, String) |
| 591 | * @see #getUserRestrictions() |
| 592 | */ |
| 593 | public static final String DISALLOW_BLUETOOTH_SHARING = "no_bluetooth_sharing"; |
| 594 | |
| 595 | /** |
| 596 | * Specifies if a user is disallowed from transferring files over USB. |
| 597 | * |
| 598 | * <p>This restriction can only be set by a device owner, a profile owner on the primary |
| 599 | * user or a profile owner of an organization-owned managed profile on the parent profile. |
| 600 | * When it is set by a device owner, it applies globally. When it is set by a profile owner |
| 601 | * on the primary user or by a profile owner of an organization-owned managed profile on |
| 602 | * the parent profile, it disables the primary user from transferring files over USB. No other |
| 603 | * user on the device is able to use file transfer over USB because the UI for file transfer |
| 604 | * is always associated with the primary user. |
| 605 | * |
| 606 | * <p>The default value is <code>false</code>. |
| 607 | * |
| 608 | * <p>Key for user restrictions. |
| 609 | * <p>Type: Boolean |
| 610 | * @see DevicePolicyManager#addUserRestriction(ComponentName, String) |
| 611 | * @see DevicePolicyManager#clearUserRestriction(ComponentName, String) |
| 612 | * @see #getUserRestrictions() |
| 613 | */ |
| 614 | public static final String DISALLOW_USB_FILE_TRANSFER = "no_usb_file_transfer"; |
| 615 | |
| 616 | /** |
| 617 | * Specifies if a user is disallowed from configuring user |
| 618 | * credentials. The default value is <code>false</code>. |
| 619 | * |
| 620 | * <p>Key for user restrictions. |
| 621 | * <p>Type: Boolean |
| 622 | * @see DevicePolicyManager#addUserRestriction(ComponentName, String) |
| 623 | * @see DevicePolicyManager#clearUserRestriction(ComponentName, String) |
| 624 | * @see #getUserRestrictions() |
| 625 | */ |
| 626 | public static final String DISALLOW_CONFIG_CREDENTIALS = "no_config_credentials"; |
| 627 | |
| 628 | /** |
| 629 | * When set on the admin user this specifies if the user can remove users. |
| 630 | * When set on a non-admin secondary user, this specifies if the user can remove itself. |
| 631 | * This restriction has no effect on managed profiles. |
| 632 | * The default value is <code>false</code>. |
| 633 | * |
| 634 | * <p>Key for user restrictions. |
| 635 | * <p>Type: Boolean |
| 636 | * @see DevicePolicyManager#addUserRestriction(ComponentName, String) |
| 637 | * @see DevicePolicyManager#clearUserRestriction(ComponentName, String) |
| 638 | * @see #getUserRestrictions() |
| 639 | */ |
| 640 | public static final String DISALLOW_REMOVE_USER = "no_remove_user"; |
| 641 | |
| 642 | /** |
| 643 | * Specifies if managed profiles of this user can be removed, other than by its profile owner. |
| 644 | * The default value is <code>false</code>. |
| 645 | * <p> |
| 646 | * This restriction has no effect on managed profiles. |
| 647 | * |
| 648 | * <p>Key for user restrictions. |
| 649 | * <p>Type: Boolean |
| 650 | * @see DevicePolicyManager#addUserRestriction(ComponentName, String) |
| 651 | * @see DevicePolicyManager#clearUserRestriction(ComponentName, String) |
| 652 | * @see #getUserRestrictions() |
| 653 | * @deprecated As the ability to have a managed profile on a fully-managed device has been |
| 654 | * removed from the platform, this restriction will be silently ignored when applied by the |
| 655 | * device owner. |
| 656 | * When the device is provisioned with a managed profile on an organization-owned device, |
| 657 | * the managed profile could not be removed anyway. |
| 658 | */ |
| 659 | @Deprecated |
| 660 | public static final String DISALLOW_REMOVE_MANAGED_PROFILE = "no_remove_managed_profile"; |
| 661 | |
| 662 | /** |
| 663 | * Specifies if a user is disallowed from enabling or accessing debugging features. |
| 664 | * |
| 665 | * <p>A device owner and a profile owner can set this restriction. When it is set by a device |
| 666 | * owner, a profile owner on the primary user or by a profile owner of an organization-owned |
| 667 | * managed profile on the parent profile, it disables debugging features altogether, including |
| 668 | * USB debugging. When set on a managed profile or a secondary user, it blocks debugging for |
| 669 | * that user only, including starting activities, making service calls, accessing content |
| 670 | * providers, sending broadcasts, installing/uninstalling packages, clearing user data, etc. |
| 671 | * |
| 672 | * <p>The default value is <code>false</code>. |
| 673 | * |
| 674 | * <p>Key for user restrictions. |
| 675 | * <p>Type: Boolean |
| 676 | * @see DevicePolicyManager#addUserRestriction(ComponentName, String) |
| 677 | * @see DevicePolicyManager#clearUserRestriction(ComponentName, String) |
| 678 | * @see #getUserRestrictions() |
| 679 | */ |
| 680 | public static final String DISALLOW_DEBUGGING_FEATURES = "no_debugging_features"; |
| 681 | |
| 682 | /** |
| 683 | * Specifies if a user is disallowed from configuring a VPN. The default value is |
| 684 | * <code>false</code>. This restriction has an effect when set by device owners and, in Android |
| 685 | * 6.0 ({@linkplain android.os.Build.VERSION_CODES#M API level 23}) or higher, profile owners. |
| 686 | * <p>This restriction also prevents VPNs from starting. However, in Android 7.0 |
| 687 | * ({@linkplain android.os.Build.VERSION_CODES#N API level 24}) or higher, the system does |
| 688 | * start always-on VPNs created by the device or profile owner. |
| 689 | * <p>From Android 12 ({@linkplain android.os.Build.VERSION_CODES#S API level 31}) enforcing |
| 690 | * this restriction clears currently active VPN if it was configured by the user. |
| 691 | * |
| 692 | * <p>Key for user restrictions. |
| 693 | * <p>Type: Boolean |
| 694 | * @see DevicePolicyManager#addUserRestriction(ComponentName, String) |
| 695 | * @see DevicePolicyManager#clearUserRestriction(ComponentName, String) |
| 696 | * @see #getUserRestrictions() |
| 697 | */ |
| 698 | public static final String DISALLOW_CONFIG_VPN = "no_config_vpn"; |
| 699 | |
| 700 | /** |
| 701 | * Specifies if a user is disallowed from enabling or disabling location providers. As a |
| 702 | * result, user is disallowed from turning on or off location via Settings. |
| 703 | * |
| 704 | * <p>A device owner and a profile owner can set this restriction. When it is set by a device |
| 705 | * owner, a profile owner on the primary user or by a profile owner of an organization-owned |
| 706 | * managed profile on the parent profile, it disallows the primary user from turning location |
| 707 | * on or off. |
| 708 | * |
| 709 | * <p>The default value is <code>false</code>. |
| 710 | * |
| 711 | * <p>This user restriction is different from {@link #DISALLOW_SHARE_LOCATION}, |
| 712 | * as a device owner or a profile owner can still enable or disable location mode via |
| 713 | * {@link DevicePolicyManager#setLocationEnabled} when this restriction is on. |
| 714 | * |
| 715 | * <p>Key for user restrictions. |
| 716 | * <p>Type: Boolean |
| 717 | * @see LocationManager#isLocationEnabled() |
| 718 | * @see DevicePolicyManager#addUserRestriction(ComponentName, String) |
| 719 | * @see DevicePolicyManager#clearUserRestriction(ComponentName, String) |
| 720 | * @see #getUserRestrictions() |
| 721 | */ |
| 722 | public static final String DISALLOW_CONFIG_LOCATION = "no_config_location"; |
| 723 | |
| 724 | /** |
| 725 | * Specifies configuring date, time and timezone is disallowed via Settings. |
| 726 | * |
| 727 | * <p>A device owner and a profile owner can set this restriction, although the restriction has |
| 728 | * no effect in a managed profile. When it is set by a device owner or by a profile owner of an |
| 729 | * organization-owned managed profile on the parent profile, it applies globally - i.e., |
| 730 | * it disables date, time and timezone setting on the entire device and all users are affected. |
| 731 | * When it is set by a profile owner on the primary user, it disables the primary user |
| 732 | * from configuring date, time and timezone and disables all configuring of date, time and |
| 733 | * timezone in Settings. |
| 734 | * |
| 735 | * <p>The default value is <code>false</code>. |
| 736 | * |
| 737 | * <p>Key for user restrictions. |
| 738 | * <p>Type: Boolean |
| 739 | * @see DevicePolicyManager#addUserRestriction(ComponentName, String) |
| 740 | * @see DevicePolicyManager#clearUserRestriction(ComponentName, String) |
| 741 | * @see #getUserRestrictions() |
| 742 | */ |
| 743 | public static final String DISALLOW_CONFIG_DATE_TIME = "no_config_date_time"; |
| 744 | |
| 745 | /** |
| 746 | * Specifies if a user is disallowed from using and configuring Tethering and portable hotspots |
| 747 | * via Settings. |
| 748 | * |
| 749 | * <p>This restriction can only be set by a device owner, a profile owner on the primary |
| 750 | * user or a profile owner of an organization-owned managed profile on the parent profile. |
| 751 | * When it is set by a device owner, it applies globally. When it is set by a profile owner |
| 752 | * on the primary user or by a profile owner of an organization-owned managed profile on |
| 753 | * the parent profile, it disables the primary user from using Tethering and hotspots and |
| 754 | * disables all configuring of Tethering and hotspots in Settings. |
| 755 | * |
| 756 | * <p>The default value is <code>false</code>. |
| 757 | * |
| 758 | * <p>In Android 9.0 or higher, if tethering is enabled when this restriction is set, |
| 759 | * tethering will be automatically turned off. |
| 760 | * |
| 761 | * <p>Key for user restrictions. |
| 762 | * <p>Type: Boolean |
| 763 | * @see DevicePolicyManager#addUserRestriction(ComponentName, String) |
| 764 | * @see DevicePolicyManager#clearUserRestriction(ComponentName, String) |
| 765 | * @see #getUserRestrictions() |
| 766 | */ |
| 767 | public static final String DISALLOW_CONFIG_TETHERING = "no_config_tethering"; |
| 768 | |
| 769 | /** |
| 770 | * Specifies if a user is disallowed from resetting network settings |
| 771 | * from Settings. This can only be set by device owners and profile owners on the primary user. |
| 772 | * The default value is <code>false</code>. |
| 773 | * <p>This restriction has no effect on secondary users and managed profiles since only the |
| 774 | * primary user can reset the network settings of the device. |
| 775 | * |
| 776 | * <p>Key for user restrictions. |
| 777 | * <p>Type: Boolean |
| 778 | * @see DevicePolicyManager#addUserRestriction(ComponentName, String) |
| 779 | * @see DevicePolicyManager#clearUserRestriction(ComponentName, String) |
| 780 | * @see #getUserRestrictions() |
| 781 | */ |
| 782 | public static final String DISALLOW_NETWORK_RESET = "no_network_reset"; |
| 783 | |
| 784 | /** |
| 785 | * Specifies if a user is disallowed from factory resetting from Settings. |
| 786 | * This can only be set by device owners and profile owners on an admin user. |
| 787 | * The default value is <code>false</code>. |
| 788 | * <p>This restriction has no effect on non-admin users since they cannot factory reset the |
| 789 | * device. |
| 790 | * |
| 791 | * <p>Key for user restrictions. |
| 792 | * <p>Type: Boolean |
| 793 | * @see DevicePolicyManager#addUserRestriction(ComponentName, String) |
| 794 | * @see DevicePolicyManager#clearUserRestriction(ComponentName, String) |
| 795 | * @see #getUserRestrictions() |
| 796 | */ |
| 797 | public static final String DISALLOW_FACTORY_RESET = "no_factory_reset"; |
| 798 | |
| 799 | /** |
| 800 | * Specifies if a user is disallowed from adding new users. This can only be set by device |
| 801 | * owners or profile owners on the primary user. The default value is <code>false</code>. |
| 802 | * <p>This restriction has no effect on secondary users and managed profiles since only the |
| 803 | * primary user can add other users. |
| 804 | * <p> When the device is an organization-owned device provisioned with a managed profile, |
| 805 | * this restriction will be set as a base restriction which cannot be removed by any admin. |
| 806 | * |
| 807 | * <p>Key for user restrictions. |
| 808 | * <p>Type: Boolean |
| 809 | * @see DevicePolicyManager#addUserRestriction(ComponentName, String) |
| 810 | * @see DevicePolicyManager#clearUserRestriction(ComponentName, String) |
| 811 | * @see #getUserRestrictions() |
| 812 | */ |
| 813 | public static final String DISALLOW_ADD_USER = "no_add_user"; |
| 814 | |
| 815 | /** |
| 816 | * Specifies if a user is disallowed from adding managed profiles. |
| 817 | * <p>The default value for an unmanaged user is <code>false</code>. |
| 818 | * For users with a device owner set, the default is <code>true</code>. |
| 819 | * <p>This restriction has no effect on managed profiles. |
| 820 | * |
| 821 | * <p>Key for user restrictions. |
| 822 | * <p>Type: Boolean |
| 823 | * @see DevicePolicyManager#addUserRestriction(ComponentName, String) |
| 824 | * @see DevicePolicyManager#clearUserRestriction(ComponentName, String) |
| 825 | * @see #getUserRestrictions() |
| 826 | * @deprecated As the ability to have a managed profile on a fully-managed device has been |
| 827 | * removed from the platform, this restriction will be silently ignored when applied by the |
| 828 | * device owner. |
| 829 | */ |
| 830 | @Deprecated |
| 831 | public static final String DISALLOW_ADD_MANAGED_PROFILE = "no_add_managed_profile"; |
| 832 | |
| 833 | /** |
| 834 | * Specifies if a user is disallowed from creating clone profile. |
| 835 | * <p>The default value for an unmanaged user is <code>false</code>. |
| 836 | * For users with a device owner set, the default is <code>true</code>. |
| 837 | * |
| 838 | * <p>Key for user restrictions. |
| 839 | * <p>Type: Boolean |
| 840 | * @see DevicePolicyManager#addUserRestriction(ComponentName, String) |
| 841 | * @see DevicePolicyManager#clearUserRestriction(ComponentName, String) |
| 842 | * @see #getUserRestrictions() |
| 843 | * @hide |
| 844 | */ |
| 845 | public static final String DISALLOW_ADD_CLONE_PROFILE = "no_add_clone_profile"; |
| 846 | |
| 847 | /** |
| 848 | * Specifies if a user is disallowed from disabling application verification. The default |
| 849 | * value is <code>false</code>. |
| 850 | * |
| 851 | * <p>In Android 8.0 ({@linkplain android.os.Build.VERSION_CODES#O API level 26}) and higher, |
| 852 | * this is a global user restriction. If a device owner or profile owner sets this restriction, |
| 853 | * the system enforces app verification across all users on the device. Running in earlier |
| 854 | * Android versions, this restriction affects only the profile that sets it. |
| 855 | * |
| 856 | * <p>Key for user restrictions. |
| 857 | * <p>Type: Boolean |
| 858 | * @see DevicePolicyManager#addUserRestriction(ComponentName, String) |
| 859 | * @see DevicePolicyManager#clearUserRestriction(ComponentName, String) |
| 860 | * @see #getUserRestrictions() |
| 861 | */ |
| 862 | public static final String ENSURE_VERIFY_APPS = "ensure_verify_apps"; |
| 863 | |
| 864 | /** |
| 865 | * Specifies if a user is disallowed from configuring cell broadcasts. |
| 866 | * |
| 867 | * <p>This restriction can only be set by a device owner, a profile owner on the primary |
| 868 | * user or a profile owner of an organization-owned managed profile on the parent profile. |
| 869 | * When it is set by a device owner, it applies globally. When it is set by a profile owner |
| 870 | * on the primary user or by a profile owner of an organization-owned managed profile on |
| 871 | * the parent profile, it disables the primary user from configuring cell broadcasts. |
| 872 | * |
| 873 | * <p>The default value is <code>false</code>. |
| 874 | * |
| 875 | * <p>This restriction has no effect on secondary users and managed profiles since only the |
| 876 | * primary user can configure cell broadcasts. |
| 877 | * |
| 878 | * <p>Key for user restrictions. |
| 879 | * <p>Type: Boolean |
| 880 | * @see DevicePolicyManager#addUserRestriction(ComponentName, String) |
| 881 | * @see DevicePolicyManager#clearUserRestriction(ComponentName, String) |
| 882 | * @see #getUserRestrictions() |
| 883 | */ |
| 884 | public static final String DISALLOW_CONFIG_CELL_BROADCASTS = "no_config_cell_broadcasts"; |
| 885 | |
| 886 | /** |
| 887 | * Specifies if a user is disallowed from configuring mobile networks. |
| 888 | * |
| 889 | * <p>This restriction can only be set by a device owner, a profile owner on the primary |
| 890 | * user or a profile owner of an organization-owned managed profile on the parent profile. |
| 891 | * When it is set by a device owner, it applies globally. When it is set by a profile owner |
| 892 | * on the primary user or by a profile owner of an organization-owned managed profile on |
| 893 | * the parent profile, it disables the primary user from configuring mobile networks. |
| 894 | * |
| 895 | * <p>The default value is <code>false</code>. |
| 896 | * |
| 897 | * <p>This restriction has no effect on secondary users and managed profiles since only the |
| 898 | * primary user can configure mobile networks. |
| 899 | * |
| 900 | * <p>Key for user restrictions. |
| 901 | * <p>Type: Boolean |
| 902 | * @see DevicePolicyManager#addUserRestriction(ComponentName, String) |
| 903 | * @see DevicePolicyManager#clearUserRestriction(ComponentName, String) |
| 904 | * @see #getUserRestrictions() |
| 905 | */ |
| 906 | public static final String DISALLOW_CONFIG_MOBILE_NETWORKS = "no_config_mobile_networks"; |
| 907 | |
| 908 | /** |
| 909 | * Specifies if a user is disallowed from modifying |
| 910 | * applications in Settings or launchers. The following actions will not be allowed when this |
| 911 | * restriction is enabled: |
| 912 | * <li>uninstalling apps</li> |
| 913 | * <li>disabling apps</li> |
| 914 | * <li>clearing app caches</li> |
| 915 | * <li>clearing app data</li> |
| 916 | * <li>force stopping apps</li> |
| 917 | * <li>clearing app defaults</li> |
| 918 | * <p> |
| 919 | * The default value is <code>false</code>. |
| 920 | * |
| 921 | * <p><strong>Note:</strong> The user will still be able to perform those actions via other |
| 922 | * means (such as adb). Third party apps will also be able to uninstall apps via the |
| 923 | * {@link android.content.pm.PackageInstaller}. {@link #DISALLOW_UNINSTALL_APPS} or |
| 924 | * {@link DevicePolicyManager#setUninstallBlocked(ComponentName, String, boolean)} should be |
| 925 | * used to prevent the user from uninstalling apps completely, and |
| 926 | * {@link DevicePolicyManager#addPersistentPreferredActivity(ComponentName, IntentFilter, ComponentName)} |
| 927 | * to add a default intent handler for a given intent filter. |
| 928 | * |
| 929 | * <p>Key for user restrictions. |
| 930 | * <p>Type: Boolean |
| 931 | * @see DevicePolicyManager#addUserRestriction(ComponentName, String) |
| 932 | * @see DevicePolicyManager#clearUserRestriction(ComponentName, String) |
| 933 | * @see #getUserRestrictions() |
| 934 | */ |
| 935 | public static final String DISALLOW_APPS_CONTROL = "no_control_apps"; |
| 936 | |
| 937 | /** |
| 938 | * Specifies if a user is disallowed from mounting physical external media. |
| 939 | * |
| 940 | * <p>This restriction can only be set by a device owner, a profile owner on the primary |
| 941 | * user or a profile owner of an organization-owned managed profile on the parent profile. |
| 942 | * When it is set by a device owner, it applies globally. When it is set by a profile owner |
| 943 | * on the primary user or by a profile owner of an organization-owned managed profile on |
| 944 | * the parent profile, it disables the primary user from mounting physical external media. |
| 945 | * |
| 946 | * <p>The default value is <code>false</code>. |
| 947 | * |
| 948 | * <p>Key for user restrictions. |
| 949 | * <p>Type: Boolean |
| 950 | * @see DevicePolicyManager#addUserRestriction(ComponentName, String) |
| 951 | * @see DevicePolicyManager#clearUserRestriction(ComponentName, String) |
| 952 | * @see #getUserRestrictions() |
| 953 | */ |
| 954 | public static final String DISALLOW_MOUNT_PHYSICAL_MEDIA = "no_physical_media"; |
| 955 | |
| 956 | /** |
| 957 | * Specifies if a user is disallowed from adjusting microphone volume. If set, the microphone |
| 958 | * will be muted. |
| 959 | * |
| 960 | * <p>A device owner and a profile owner can set this restriction, although the restriction has |
| 961 | * no effect in a managed profile. When it is set by a device owner, it applies globally. When |
| 962 | * it is set by a profile owner on the primary user or by a profile owner of an |
| 963 | * organization-owned managed profile on the parent profile, it will disallow the primary user |
| 964 | * from adjusting the microphone volume. |
| 965 | * |
| 966 | * <p>The default value is <code>false</code>. |
| 967 | * |
| 968 | * <p>Key for user restrictions. |
| 969 | * <p>Type: Boolean |
| 970 | * @see DevicePolicyManager#addUserRestriction(ComponentName, String) |
| 971 | * @see DevicePolicyManager#clearUserRestriction(ComponentName, String) |
| 972 | * @see #getUserRestrictions() |
| 973 | */ |
| 974 | public static final String DISALLOW_UNMUTE_MICROPHONE = "no_unmute_microphone"; |
| 975 | |
| 976 | /** |
| 977 | * Specifies if a user is disallowed from adjusting the global volume. If set, the global volume |
| 978 | * will be muted. This can be set by device owners from API 21 and profile owners from API 24. |
| 979 | * The default value is <code>false</code>. |
| 980 | * |
| 981 | * <p>When the restriction is set by profile owners, then it only applies to relevant |
| 982 | * profiles. |
| 983 | * |
| 984 | * <p>This restriction has no effect on managed profiles. |
| 985 | * <p>Key for user restrictions. |
| 986 | * <p>Type: Boolean |
| 987 | * @see DevicePolicyManager#addUserRestriction(ComponentName, String) |
| 988 | * @see DevicePolicyManager#clearUserRestriction(ComponentName, String) |
| 989 | * @see #getUserRestrictions() |
| 990 | */ |
| 991 | public static final String DISALLOW_ADJUST_VOLUME = "no_adjust_volume"; |
| 992 | |
| 993 | /** |
| 994 | * Specifies that the user is not allowed to make outgoing phone calls. Emergency calls are |
| 995 | * still permitted. |
| 996 | * |
| 997 | * <p>A device owner and a profile owner can set this restriction, although the restriction has |
| 998 | * no effect in a managed profile. When it is set by a device owner, a profile owner on the |
| 999 | * primary user or by a profile owner of an organization-owned managed profile on the parent |
| 1000 | * profile, it disallows the primary user from making outgoing phone calls. |
| 1001 | * |
| 1002 | * <p>The default value is <code>false</code>. |
| 1003 | * |
| 1004 | * <p>Key for user restrictions. |
| 1005 | * <p>Type: Boolean |
| 1006 | * @see DevicePolicyManager#addUserRestriction(ComponentName, String) |
| 1007 | * @see DevicePolicyManager#clearUserRestriction(ComponentName, String) |
| 1008 | * @see #getUserRestrictions() |
| 1009 | */ |
| 1010 | public static final String DISALLOW_OUTGOING_CALLS = "no_outgoing_calls"; |
| 1011 | |
| 1012 | /** |
| 1013 | * Specifies that the user is not allowed to send or receive SMS messages. |
| 1014 | * |
| 1015 | * <p>This restriction can only be set by a device owner, a profile owner on the primary |
| 1016 | * user or a profile owner of an organization-owned managed profile on the parent profile. |
| 1017 | * When it is set by a device owner, it applies globally. When it is set by a profile owner |
| 1018 | * on the primary user or by a profile owner of an organization-owned managed profile on |
| 1019 | * the parent profile, it disables the primary user from sending or receiving SMS messages. |
| 1020 | * |
| 1021 | * <p>The default value is <code>false</code>. |
| 1022 | * |
| 1023 | * <p>Key for user restrictions. |
| 1024 | * <p>Type: Boolean |
| 1025 | * @see DevicePolicyManager#addUserRestriction(ComponentName, String) |
| 1026 | * @see DevicePolicyManager#clearUserRestriction(ComponentName, String) |
| 1027 | * @see #getUserRestrictions() |
| 1028 | */ |
| 1029 | public static final String DISALLOW_SMS = "no_sms"; |
| 1030 | |
| 1031 | /** |
| 1032 | * Specifies if the user is not allowed to have fun. In some cases, the |
| 1033 | * device owner may wish to prevent the user from experiencing amusement or |
| 1034 | * joy while using the device. The default value is <code>false</code>. |
| 1035 | * |
| 1036 | * <p>Key for user restrictions. |
| 1037 | * <p>Type: Boolean |
| 1038 | * @see DevicePolicyManager#addUserRestriction(ComponentName, String) |
| 1039 | * @see DevicePolicyManager#clearUserRestriction(ComponentName, String) |
| 1040 | * @see #getUserRestrictions() |
| 1041 | */ |
| 1042 | public static final String DISALLOW_FUN = "no_fun"; |
| 1043 | |
| 1044 | /** |
| 1045 | * Specifies that windows besides app windows should not be |
| 1046 | * created. This will block the creation of the following types of windows. |
| 1047 | * <li>{@link LayoutParams#TYPE_TOAST}</li> |
| 1048 | * <li>{@link LayoutParams#TYPE_PHONE}</li> |
| 1049 | * <li>{@link LayoutParams#TYPE_PRIORITY_PHONE}</li> |
| 1050 | * <li>{@link LayoutParams#TYPE_SYSTEM_ALERT}</li> |
| 1051 | * <li>{@link LayoutParams#TYPE_SYSTEM_ERROR}</li> |
| 1052 | * <li>{@link LayoutParams#TYPE_SYSTEM_OVERLAY}</li> |
| 1053 | * <li>{@link LayoutParams#TYPE_APPLICATION_OVERLAY}</li> |
| 1054 | * |
| 1055 | * <p>This can only be set by device owners and profile owners on the primary user. |
| 1056 | * The default value is <code>false</code>. |
| 1057 | * |
| 1058 | * <p>Key for user restrictions. |
| 1059 | * <p>Type: Boolean |
| 1060 | * @see DevicePolicyManager#addUserRestriction(ComponentName, String) |
| 1061 | * @see DevicePolicyManager#clearUserRestriction(ComponentName, String) |
| 1062 | * @see #getUserRestrictions() |
| 1063 | */ |
| 1064 | public static final String DISALLOW_CREATE_WINDOWS = "no_create_windows"; |
| 1065 | |
| 1066 | /** |
| 1067 | * Specifies that system error dialogs for crashed or unresponsive apps should not be shown. |
| 1068 | * In this case, the system will force-stop the app as if the user chooses the "close app" |
| 1069 | * option on the UI. A feedback report isn't collected as there is no way for the user to |
| 1070 | * provide explicit consent. The default value is <code>false</code>. |
| 1071 | * |
| 1072 | * <p>When this user restriction is set by device owners, it's applied to all users. When set by |
| 1073 | * the profile owner of the primary user or a secondary user, the restriction affects only the |
| 1074 | * calling user. This user restriction has no effect on managed profiles. |
| 1075 | * |
| 1076 | * <p>Key for user restrictions. |
| 1077 | * <p>Type: Boolean |
| 1078 | * @see DevicePolicyManager#addUserRestriction(ComponentName, String) |
| 1079 | * @see DevicePolicyManager#clearUserRestriction(ComponentName, String) |
| 1080 | * @see #getUserRestrictions() |
| 1081 | */ |
| 1082 | public static final String DISALLOW_SYSTEM_ERROR_DIALOGS = "no_system_error_dialogs"; |
| 1083 | |
| 1084 | /** |
| 1085 | * Specifies if the clipboard contents can be exported by pasting the data into other users or |
| 1086 | * profiles. This restriction doesn't prevent import, such as someone pasting clipboard data |
| 1087 | * from other profiles or users. The default value is {@code false}. |
| 1088 | * |
| 1089 | * <p><strong>Note</strong>: Because it's possible to extract data from screenshots using |
| 1090 | * optical character recognition (OCR), we strongly recommend combining this user restriction |
| 1091 | * with {@link DevicePolicyManager#setScreenCaptureDisabled(ComponentName, boolean)}. |
| 1092 | * |
| 1093 | * <p>Key for user restrictions. |
| 1094 | * <p>Type: Boolean |
| 1095 | * @see DevicePolicyManager#addUserRestriction(ComponentName, String) |
| 1096 | * @see DevicePolicyManager#clearUserRestriction(ComponentName, String) |
| 1097 | * @see #getUserRestrictions() |
| 1098 | */ |
| 1099 | public static final String DISALLOW_CROSS_PROFILE_COPY_PASTE = "no_cross_profile_copy_paste"; |
| 1100 | |
| 1101 | /** |
| 1102 | * Specifies if the user is not allowed to use NFC to beam out data from apps. |
| 1103 | * The default value is <code>false</code>. |
| 1104 | * |
| 1105 | * <p>Key for user restrictions. |
| 1106 | * <p>Type: Boolean |
| 1107 | * @see DevicePolicyManager#addUserRestriction(ComponentName, String) |
| 1108 | * @see DevicePolicyManager#clearUserRestriction(ComponentName, String) |
| 1109 | * @see #getUserRestrictions() |
| 1110 | */ |
| 1111 | public static final String DISALLOW_OUTGOING_BEAM = "no_outgoing_beam"; |
| 1112 | |
| 1113 | /** |
| 1114 | * Hidden user restriction to disallow access to wallpaper manager APIs. This restriction |
| 1115 | * generally means that wallpapers are not supported for the particular user. This user |
| 1116 | * restriction is always set for managed profiles, because such profiles don't have wallpapers. |
| 1117 | * @hide |
| 1118 | * @see #DISALLOW_SET_WALLPAPER |
| 1119 | * @see DevicePolicyManager#addUserRestriction(ComponentName, String) |
| 1120 | * @see DevicePolicyManager#clearUserRestriction(ComponentName, String) |
| 1121 | * @see #getUserRestrictions() |
| 1122 | */ |
| 1123 | public static final String DISALLOW_WALLPAPER = "no_wallpaper"; |
| 1124 | |
| 1125 | /** |
| 1126 | * User restriction to disallow setting a wallpaper. Profile owner and device owner |
| 1127 | * are able to set wallpaper regardless of this restriction. |
| 1128 | * The default value is <code>false</code>. |
| 1129 | * |
| 1130 | * <p>Key for user restrictions. |
| 1131 | * <p>Type: Boolean |
| 1132 | * @see DevicePolicyManager#addUserRestriction(ComponentName, String) |
| 1133 | * @see DevicePolicyManager#clearUserRestriction(ComponentName, String) |
| 1134 | * @see #getUserRestrictions() |
| 1135 | */ |
| 1136 | public static final String DISALLOW_SET_WALLPAPER = "no_set_wallpaper"; |
| 1137 | |
| 1138 | /** |
| 1139 | * Specifies if the user is not allowed to reboot the device into safe boot mode. |
| 1140 | * |
| 1141 | * <p>This restriction can only be set by a device owner, a profile owner on the primary |
| 1142 | * user or a profile owner of an organization-owned managed profile on the parent profile. |
| 1143 | * When it is set by a device owner, it applies globally. When it is set by a profile owner |
| 1144 | * on the primary user or by a profile owner of an organization-owned managed profile on |
| 1145 | * the parent profile, it disables the primary user from rebooting the device into safe |
| 1146 | * boot mode. |
| 1147 | * |
| 1148 | * <p>The default value is <code>false</code>. |
| 1149 | * |
| 1150 | * <p>Key for user restrictions. |
| 1151 | * <p>Type: Boolean |
| 1152 | * @see DevicePolicyManager#addUserRestriction(ComponentName, String) |
| 1153 | * @see DevicePolicyManager#clearUserRestriction(ComponentName, String) |
| 1154 | * @see #getUserRestrictions() |
| 1155 | */ |
| 1156 | public static final String DISALLOW_SAFE_BOOT = "no_safe_boot"; |
| 1157 | |
| 1158 | /** |
| 1159 | * Specifies if a user is not allowed to record audio. This restriction is always enabled for |
| 1160 | * background users. The default value is <code>false</code>. |
| 1161 | * |
| 1162 | * @see DevicePolicyManager#addUserRestriction(ComponentName, String) |
| 1163 | * @see DevicePolicyManager#clearUserRestriction(ComponentName, String) |
| 1164 | * @see #getUserRestrictions() |
| 1165 | * @hide |
| 1166 | */ |
| 1167 | @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.R, trackingBug = 170729553) |
| 1168 | public static final String DISALLOW_RECORD_AUDIO = "no_record_audio"; |
| 1169 | |
| 1170 | /** |
| 1171 | * Specifies if a user is not allowed to run in the background and should be stopped during |
| 1172 | * user switch. The default value is <code>false</code>. |
| 1173 | * |
| 1174 | * <p>This restriction can be set by device owners and profile owners. |
| 1175 | * |
| 1176 | * @see DevicePolicyManager#addUserRestriction(ComponentName, String) |
| 1177 | * @see DevicePolicyManager#clearUserRestriction(ComponentName, String) |
| 1178 | * @see #getUserRestrictions() |
| 1179 | * @hide |
| 1180 | */ |
| 1181 | @SystemApi |
| 1182 | public static final String DISALLOW_RUN_IN_BACKGROUND = "no_run_in_background"; |
| 1183 | |
| 1184 | /** |
| 1185 | * Specifies if a user is not allowed to use the camera. |
| 1186 | * |
| 1187 | * <p>A device owner and a profile owner can set this restriction. When it is set by a |
| 1188 | * device owner, it applies globally - i.e., it disables the use of camera on the entire device |
| 1189 | * and all users are affected. When it is set by a profile owner on the primary user or by a |
| 1190 | * profile owner of an organization-owned managed profile on the parent profile, it disables |
| 1191 | * the primary user from using camera. |
| 1192 | * |
| 1193 | * <p>The default value is <code>false</code>. |
| 1194 | * |
| 1195 | * @see DevicePolicyManager#addUserRestriction(ComponentName, String) |
| 1196 | * @see DevicePolicyManager#clearUserRestriction(ComponentName, String) |
| 1197 | * @see #getUserRestrictions() |
| 1198 | * @hide |
| 1199 | */ |
| 1200 | public static final String DISALLOW_CAMERA = "no_camera"; |
| 1201 | |
| 1202 | /** |
| 1203 | * Specifies if a user is not allowed to unmute the device's global volume. |
| 1204 | * |
| 1205 | * @see DevicePolicyManager#setMasterVolumeMuted(ComponentName, boolean) |
| 1206 | * @see DevicePolicyManager#clearUserRestriction(ComponentName, String) |
| 1207 | * @see #getUserRestrictions() |
| 1208 | * @hide |
| 1209 | */ |
| 1210 | public static final String DISALLOW_UNMUTE_DEVICE = "disallow_unmute_device"; |
| 1211 | |
| 1212 | /** |
| 1213 | * Specifies if a user is not allowed to use cellular data when roaming. |
| 1214 | * |
| 1215 | * <p>This restriction can only be set by a device owner, a profile owner on the primary |
| 1216 | * user or a profile owner of an organization-owned managed profile on the parent profile. |
| 1217 | * When it is set by a device owner, it applies globally. When it is set by a profile owner |
| 1218 | * on the primary user or by a profile owner of an organization-owned managed profile on |
| 1219 | * the parent profile, it disables the primary user from using cellular data when roaming. |
| 1220 | * |
| 1221 | * <p>The default value is <code>false</code>. |
| 1222 | * |
| 1223 | * @see DevicePolicyManager#addUserRestriction(ComponentName, String) |
| 1224 | * @see DevicePolicyManager#clearUserRestriction(ComponentName, String) |
| 1225 | * @see #getUserRestrictions() |
| 1226 | */ |
| 1227 | public static final String DISALLOW_DATA_ROAMING = "no_data_roaming"; |
| 1228 | |
| 1229 | /** |
| 1230 | * Specifies if a user is not allowed to change their icon. Device owner and profile owner |
| 1231 | * can set this restriction. When it is set by device owner, only the target user will be |
| 1232 | * affected. The default value is <code>false</code>. |
| 1233 | * |
| 1234 | * <p>Key for user restrictions. |
| 1235 | * <p>Type: Boolean |
| 1236 | * @see DevicePolicyManager#addUserRestriction(ComponentName, String) |
| 1237 | * @see DevicePolicyManager#clearUserRestriction(ComponentName, String) |
| 1238 | * @see #getUserRestrictions() |
| 1239 | */ |
| 1240 | public static final String DISALLOW_SET_USER_ICON = "no_set_user_icon"; |
| 1241 | |
| 1242 | /** |
| 1243 | * Specifies if a user is not allowed to enable the oem unlock setting. The default value is |
| 1244 | * <code>false</code>. Setting this restriction has no effect if the bootloader is already |
| 1245 | * unlocked. |
| 1246 | * |
| 1247 | * <p>Not for use by third-party applications. |
| 1248 | * |
| 1249 | * @see DevicePolicyManager#addUserRestriction(ComponentName, String) |
| 1250 | * @see DevicePolicyManager#clearUserRestriction(ComponentName, String) |
| 1251 | * @see #getUserRestrictions() |
| 1252 | * @deprecated use {@link OemLockManager#setOemUnlockAllowedByCarrier(boolean, byte[])} instead. |
| 1253 | * @hide |
| 1254 | */ |
| 1255 | @Deprecated |
| 1256 | @SystemApi |
| 1257 | public static final String DISALLOW_OEM_UNLOCK = "no_oem_unlock"; |
| 1258 | |
| 1259 | /** |
| 1260 | * Specifies that the managed profile is not allowed to have unified lock screen challenge with |
| 1261 | * the primary user. |
| 1262 | * |
| 1263 | * <p><strong>Note:</strong> Setting this restriction alone doesn't automatically set a |
| 1264 | * separate challenge. Profile owner can ask the user to set a new password using |
| 1265 | * {@link DevicePolicyManager#ACTION_SET_NEW_PASSWORD} and verify it using |
| 1266 | * {@link DevicePolicyManager#isUsingUnifiedPassword(ComponentName)}. |
| 1267 | * |
| 1268 | * <p>Can be set by profile owners. It only has effect on managed profiles when set by managed |
| 1269 | * profile owner. Has no effect on non-managed profiles or users. |
| 1270 | * <p>Key for user restrictions. |
| 1271 | * <p>Type: Boolean |
| 1272 | * @see DevicePolicyManager#addUserRestriction(ComponentName, String) |
| 1273 | * @see DevicePolicyManager#clearUserRestriction(ComponentName, String) |
| 1274 | * @see #getUserRestrictions() |
| 1275 | */ |
| 1276 | public static final String DISALLOW_UNIFIED_PASSWORD = "no_unified_password"; |
| 1277 | |
| 1278 | /** |
| 1279 | * Allows apps in the parent profile to handle web links from the managed profile. |
| 1280 | * |
| 1281 | * This user restriction has an effect only in a managed profile. |
| 1282 | * If set: |
| 1283 | * Intent filters of activities in the parent profile with action |
| 1284 | * {@link android.content.Intent#ACTION_VIEW}, |
| 1285 | * category {@link android.content.Intent#CATEGORY_BROWSABLE}, scheme http or https, and which |
| 1286 | * define a host can handle intents from the managed profile. |
| 1287 | * The default value is <code>false</code>. |
| 1288 | * |
| 1289 | * <p>Key for user restrictions. |
| 1290 | * <p>Type: Boolean |
| 1291 | * @see DevicePolicyManager#addUserRestriction(ComponentName, String) |
| 1292 | * @see DevicePolicyManager#clearUserRestriction(ComponentName, String) |
| 1293 | * @see #getUserRestrictions() |
| 1294 | */ |
| 1295 | public static final String ALLOW_PARENT_PROFILE_APP_LINKING |
| 1296 | = "allow_parent_profile_app_linking"; |
| 1297 | |
| 1298 | /** |
| 1299 | * Specifies if a user is not allowed to use Autofill Services. |
| 1300 | * |
| 1301 | * <p>Device owner and profile owner can set this restriction. When it is set by device owner, |
| 1302 | * only the target user will be affected. |
| 1303 | * |
| 1304 | * <p>The default value is <code>false</code>. |
| 1305 | * |
| 1306 | * @see DevicePolicyManager#addUserRestriction(ComponentName, String) |
| 1307 | * @see DevicePolicyManager#clearUserRestriction(ComponentName, String) |
| 1308 | * @see #getUserRestrictions() |
| 1309 | */ |
| 1310 | public static final String DISALLOW_AUTOFILL = "no_autofill"; |
| 1311 | |
| 1312 | /** |
| 1313 | * Specifies if the contents of a user's screen is not allowed to be captured for artificial |
| 1314 | * intelligence purposes. |
| 1315 | * |
| 1316 | * <p>A device owner and a profile owner can set this restriction. When it is set by a device |
| 1317 | * owner, a profile owner on the primary user or by a profile owner of an organization-owned |
| 1318 | * managed profile on the parent profile, it disables the primary user's screen from being |
| 1319 | * captured for artificial intelligence purposes. |
| 1320 | * |
| 1321 | * <p>The default value is <code>false</code>. |
| 1322 | * |
| 1323 | * @see DevicePolicyManager#addUserRestriction(ComponentName, String) |
| 1324 | * @see DevicePolicyManager#clearUserRestriction(ComponentName, String) |
| 1325 | * @see #getUserRestrictions() |
| 1326 | */ |
| 1327 | public static final String DISALLOW_CONTENT_CAPTURE = "no_content_capture"; |
| 1328 | |
| 1329 | /** |
| 1330 | * Specifies if the current user is able to receive content suggestions for selections based on |
| 1331 | * the contents of their screen. |
| 1332 | * |
| 1333 | * <p>A device owner and a profile owner can set this restriction. When it is set by a device |
| 1334 | * owner, a profile owner on the primary user or by a profile owner of an organization-owned |
| 1335 | * managed profile on the parent profile, it disables the primary user from receiving content |
| 1336 | * suggestions for selections based on the contents of their screen. |
| 1337 | * |
| 1338 | * <p>The default value is <code>false</code>. |
| 1339 | * |
| 1340 | * @see DevicePolicyManager#addUserRestriction(ComponentName, String) |
| 1341 | * @see DevicePolicyManager#clearUserRestriction(ComponentName, String) |
| 1342 | * @see #getUserRestrictions() |
| 1343 | */ |
| 1344 | public static final String DISALLOW_CONTENT_SUGGESTIONS = "no_content_suggestions"; |
| 1345 | |
| 1346 | /** |
| 1347 | * Specifies if user switching is blocked on the current user. |
| 1348 | * |
| 1349 | * <p> This restriction can only be set by the device owner, it will be applied to all users. |
| 1350 | * Device owner can still switch user via |
| 1351 | * {@link DevicePolicyManager#switchUser(ComponentName, UserHandle)} when this restriction is |
| 1352 | * set. |
| 1353 | * |
| 1354 | * <p>The default value is <code>false</code>. |
| 1355 | * |
| 1356 | * @see DevicePolicyManager#addUserRestriction(ComponentName, String) |
| 1357 | * @see DevicePolicyManager#clearUserRestriction(ComponentName, String) |
| 1358 | * @see #getUserRestrictions() |
| 1359 | */ |
| 1360 | public static final String DISALLOW_USER_SWITCH = "no_user_switch"; |
| 1361 | |
| 1362 | /** |
| 1363 | * Specifies whether the user can share file / picture / data from the primary user into the |
| 1364 | * managed profile, either by sending them from the primary side, or by picking up data within |
| 1365 | * an app in the managed profile. |
| 1366 | * <p> |
| 1367 | * When a managed profile is created, the system allows the user to send data from the primary |
| 1368 | * side to the profile by setting up certain default cross profile intent filters. If |
| 1369 | * this is undesired, this restriction can be set to disallow it. Note that this restriction |
| 1370 | * will not block any sharing allowed by explicit |
| 1371 | * {@link DevicePolicyManager#addCrossProfileIntentFilter} calls by the profile owner. |
| 1372 | * <p> |
| 1373 | * This restriction is only meaningful when set by profile owner. When it is set by device |
| 1374 | * owner, it does not have any effect. |
| 1375 | * <p> |
| 1376 | * The default value is <code>false</code>. |
| 1377 | * |
| 1378 | * @see DevicePolicyManager#addUserRestriction(ComponentName, String) |
| 1379 | * @see DevicePolicyManager#clearUserRestriction(ComponentName, String) |
| 1380 | * @see #getUserRestrictions() |
| 1381 | */ |
| 1382 | public static final String DISALLOW_SHARE_INTO_MANAGED_PROFILE = "no_sharing_into_profile"; |
| 1383 | |
| 1384 | /** |
| 1385 | * Specifies whether the user is allowed to print. |
| 1386 | * |
| 1387 | * This restriction can be set by device or profile owner. |
| 1388 | * |
| 1389 | * The default value is {@code false}. |
| 1390 | * |
| 1391 | * @see DevicePolicyManager#addUserRestriction(ComponentName, String) |
| 1392 | * @see DevicePolicyManager#clearUserRestriction(ComponentName, String) |
| 1393 | * @see #getUserRestrictions() |
| 1394 | */ |
| 1395 | public static final String DISALLOW_PRINTING = "no_printing"; |
| 1396 | |
| 1397 | /** |
| 1398 | * Specifies whether the user is allowed to modify private DNS settings. |
| 1399 | * |
| 1400 | * <p>This restriction can only be set by a device owner or a profile owner of an |
| 1401 | * organization-owned managed profile on the parent profile. When it is set by either of these |
| 1402 | * owners, it applies globally. |
| 1403 | * |
| 1404 | * <p>The default value is <code>false</code>. |
| 1405 | * |
| 1406 | * <p>Key for user restrictions. |
| 1407 | * <p>Type: Boolean |
| 1408 | * @see DevicePolicyManager#addUserRestriction(ComponentName, String) |
| 1409 | * @see DevicePolicyManager#clearUserRestriction(ComponentName, String) |
| 1410 | * @see #getUserRestrictions() |
| 1411 | */ |
| 1412 | public static final String DISALLOW_CONFIG_PRIVATE_DNS = |
| 1413 | "disallow_config_private_dns"; |
| 1414 | |
| 1415 | /** |
| 1416 | * Specifies whether the microphone toggle is available to the user. If this restriction is set, |
| 1417 | * the user will not be able to block microphone access via the system toggle. If microphone |
| 1418 | * access is blocked when the restriction is added, it will be automatically re-enabled. |
| 1419 | * |
| 1420 | * This restriction can only be set by a device owner. |
| 1421 | * |
| 1422 | * <p>The default value is <code>false</code>. |
| 1423 | * |
| 1424 | * @see android.hardware.SensorPrivacyManager |
| 1425 | * @see DevicePolicyManager#addUserRestriction(ComponentName, String) |
| 1426 | * @see DevicePolicyManager#clearUserRestriction(ComponentName, String) |
| 1427 | * @see #getUserRestrictions() |
| 1428 | */ |
| 1429 | public static final String DISALLOW_MICROPHONE_TOGGLE = |
| 1430 | "disallow_microphone_toggle"; |
| 1431 | |
| 1432 | /** |
| 1433 | * Specifies whether the camera toggle is available to the user. If this restriction is set, |
| 1434 | * the user will not be able to block camera access via the system toggle. If camera |
| 1435 | * access is blocked when the restriction is added, it will be automatically re-enabled. |
| 1436 | * |
| 1437 | * This restriction can only be set by a device owner. |
| 1438 | * |
| 1439 | * <p>The default value is <code>false</code>. |
| 1440 | * |
| 1441 | * @see android.hardware.SensorPrivacyManager |
| 1442 | * @see DevicePolicyManager#addUserRestriction(ComponentName, String) |
| 1443 | * @see DevicePolicyManager#clearUserRestriction(ComponentName, String) |
| 1444 | * @see #getUserRestrictions() |
| 1445 | */ |
| 1446 | public static final String DISALLOW_CAMERA_TOGGLE = |
| 1447 | "disallow_camera_toggle"; |
| 1448 | |
| 1449 | /** |
| 1450 | * This is really not a user restriction in the normal sense. This can't be set to a user, |
| 1451 | * via UserManager nor via DevicePolicyManager. This is not even set in UserSettingsUtils. |
| 1452 | * This is defined here purely for convenience within the settings app. |
| 1453 | * |
| 1454 | * TODO(b/191306258): Refactor the Settings app to remove the need for this field, and delete it |
| 1455 | * |
| 1456 | * Specifies whether biometrics are available to the user. This is used internally only, |
| 1457 | * as a means of communications between biometric settings and |
| 1458 | * {@link com.android.settingslib.enterprise.ActionDisabledByAdminControllerFactory}. |
| 1459 | * |
| 1460 | * @see {@link android.hardware.biometrics.ParentalControlsUtilsInternal} |
| 1461 | * @see {@link com.android.settings.biometrics.ParentalControlsUtils} |
| 1462 | * |
| 1463 | * @hide |
| 1464 | */ |
| 1465 | public static final String DISALLOW_BIOMETRIC = "disallow_biometric"; |
| 1466 | |
| 1467 | /** |
| 1468 | * Application restriction key that is used to indicate the pending arrival |
| 1469 | * of real restrictions for the app. |
| 1470 | * |
| 1471 | * <p> |
| 1472 | * Applications that support restrictions should check for the presence of this key. |
| 1473 | * A <code>true</code> value indicates that restrictions may be applied in the near |
| 1474 | * future but are not available yet. It is the responsibility of any |
| 1475 | * management application that sets this flag to update it when the final |
| 1476 | * restrictions are enforced. |
| 1477 | * |
| 1478 | * <p>Key for application restrictions. |
| 1479 | * <p>Type: Boolean |
| 1480 | * @see android.app.admin.DevicePolicyManager#setApplicationRestrictions( |
| 1481 | * android.content.ComponentName, String, Bundle) |
| 1482 | * @see android.app.admin.DevicePolicyManager#getApplicationRestrictions( |
| 1483 | * android.content.ComponentName, String) |
| 1484 | */ |
| 1485 | public static final String KEY_RESTRICTIONS_PENDING = "restrictions_pending"; |
| 1486 | |
| 1487 | /** |
| 1488 | * Specifies if a user is not allowed to use 2g networks. |
| 1489 | * |
| 1490 | * <p>This restriction can only be set by a device owner or a profile owner of an |
| 1491 | * organization-owned managed profile on the parent profile. |
| 1492 | * In all cases, the setting applies globally on the device and will prevent the device from |
| 1493 | * scanning for or connecting to 2g networks, except in the case of an emergency. |
| 1494 | * |
| 1495 | * <p>The default value is <code>false</code>. |
| 1496 | * |
| 1497 | * @see DevicePolicyManager#addUserRestriction(ComponentName, String) |
| 1498 | * @see DevicePolicyManager#clearUserRestriction(ComponentName, String) |
| 1499 | * @see #getUserRestrictions() |
| 1500 | */ |
| 1501 | public static final String DISALLOW_CELLULAR_2G = "no_cellular_2g"; |
| 1502 | |
| 1503 | /** |
| 1504 | * List of key values that can be passed into the various user restriction related methods |
| 1505 | * in {@link UserManager} & {@link DevicePolicyManager}. |
| 1506 | * Note: This is slightly different from the real set of user restrictions listed in {@link |
| 1507 | * com.android.server.pm.UserRestrictionsUtils#USER_RESTRICTIONS}. For example |
| 1508 | * {@link #KEY_RESTRICTIONS_PENDING} is not a real user restriction, but is a legitimate |
| 1509 | * value that can be passed into {@link #hasUserRestriction(String)}. |
| 1510 | * @hide |
| 1511 | */ |
| 1512 | @StringDef(value = { |
| 1513 | DISALLOW_MODIFY_ACCOUNTS, |
| 1514 | DISALLOW_CONFIG_WIFI, |
| 1515 | DISALLOW_CONFIG_LOCALE, |
| 1516 | DISALLOW_INSTALL_APPS, |
| 1517 | DISALLOW_UNINSTALL_APPS, |
| 1518 | DISALLOW_SHARE_LOCATION, |
| 1519 | DISALLOW_AIRPLANE_MODE, |
| 1520 | DISALLOW_CONFIG_BRIGHTNESS, |
| 1521 | DISALLOW_AMBIENT_DISPLAY, |
| 1522 | DISALLOW_CONFIG_SCREEN_TIMEOUT, |
| 1523 | DISALLOW_INSTALL_UNKNOWN_SOURCES, |
| 1524 | DISALLOW_INSTALL_UNKNOWN_SOURCES_GLOBALLY, |
| 1525 | DISALLOW_CONFIG_BLUETOOTH, |
| 1526 | DISALLOW_BLUETOOTH, |
| 1527 | DISALLOW_BLUETOOTH_SHARING, |
| 1528 | DISALLOW_USB_FILE_TRANSFER, |
| 1529 | DISALLOW_CONFIG_CREDENTIALS, |
| 1530 | DISALLOW_REMOVE_USER, |
| 1531 | DISALLOW_REMOVE_MANAGED_PROFILE, |
| 1532 | DISALLOW_DEBUGGING_FEATURES, |
| 1533 | DISALLOW_CONFIG_VPN, |
| 1534 | DISALLOW_CONFIG_LOCATION, |
| 1535 | DISALLOW_CONFIG_DATE_TIME, |
| 1536 | DISALLOW_CONFIG_TETHERING, |
| 1537 | DISALLOW_NETWORK_RESET, |
| 1538 | DISALLOW_FACTORY_RESET, |
| 1539 | DISALLOW_ADD_USER, |
| 1540 | DISALLOW_ADD_MANAGED_PROFILE, |
| 1541 | DISALLOW_ADD_CLONE_PROFILE, |
| 1542 | ENSURE_VERIFY_APPS, |
| 1543 | DISALLOW_CONFIG_CELL_BROADCASTS, |
| 1544 | DISALLOW_CONFIG_MOBILE_NETWORKS, |
| 1545 | DISALLOW_APPS_CONTROL, |
| 1546 | DISALLOW_MOUNT_PHYSICAL_MEDIA, |
| 1547 | DISALLOW_UNMUTE_MICROPHONE, |
| 1548 | DISALLOW_ADJUST_VOLUME, |
| 1549 | DISALLOW_OUTGOING_CALLS, |
| 1550 | DISALLOW_SMS, |
| 1551 | DISALLOW_FUN, |
| 1552 | DISALLOW_CREATE_WINDOWS, |
| 1553 | DISALLOW_SYSTEM_ERROR_DIALOGS, |
| 1554 | DISALLOW_CROSS_PROFILE_COPY_PASTE, |
| 1555 | DISALLOW_OUTGOING_BEAM, |
| 1556 | DISALLOW_WALLPAPER, |
| 1557 | DISALLOW_SET_WALLPAPER, |
| 1558 | DISALLOW_SAFE_BOOT, |
| 1559 | DISALLOW_RECORD_AUDIO, |
| 1560 | DISALLOW_RUN_IN_BACKGROUND, |
| 1561 | DISALLOW_CAMERA, |
| 1562 | DISALLOW_UNMUTE_DEVICE, |
| 1563 | DISALLOW_DATA_ROAMING, |
| 1564 | DISALLOW_SET_USER_ICON, |
| 1565 | DISALLOW_OEM_UNLOCK, |
| 1566 | DISALLOW_UNIFIED_PASSWORD, |
| 1567 | ALLOW_PARENT_PROFILE_APP_LINKING, |
| 1568 | DISALLOW_AUTOFILL, |
| 1569 | DISALLOW_CONTENT_CAPTURE, |
| 1570 | DISALLOW_CONTENT_SUGGESTIONS, |
| 1571 | DISALLOW_USER_SWITCH, |
| 1572 | DISALLOW_SHARE_INTO_MANAGED_PROFILE, |
| 1573 | DISALLOW_PRINTING, |
| 1574 | DISALLOW_CONFIG_PRIVATE_DNS, |
| 1575 | DISALLOW_MICROPHONE_TOGGLE, |
| 1576 | DISALLOW_CAMERA_TOGGLE, |
| 1577 | KEY_RESTRICTIONS_PENDING, |
| 1578 | DISALLOW_BIOMETRIC, |
| 1579 | DISALLOW_CHANGE_WIFI_STATE, |
| 1580 | DISALLOW_WIFI_TETHERING, |
| 1581 | DISALLOW_SHARING_ADMIN_CONFIGURED_WIFI, |
| 1582 | DISALLOW_WIFI_DIRECT, |
| 1583 | DISALLOW_ADD_WIFI_CONFIG, |
| 1584 | DISALLOW_CELLULAR_2G, |
| 1585 | }) |
| 1586 | @Retention(RetentionPolicy.SOURCE) |
| 1587 | public @interface UserRestrictionKey {} |
| 1588 | |
| 1589 | private static final String ACTION_CREATE_USER = "android.os.action.CREATE_USER"; |
| 1590 | |
| 1591 | /** |
| 1592 | * Action to start an activity to create a supervised user. |
| 1593 | * Only devices with non-empty config_supervisedUserCreationPackage support this. |
| 1594 | * |
| 1595 | * @hide |
| 1596 | */ |
| 1597 | @SystemApi |
| 1598 | @RequiresPermission(Manifest.permission.MANAGE_USERS) |
| 1599 | public static final String ACTION_CREATE_SUPERVISED_USER = |
| 1600 | "android.os.action.CREATE_SUPERVISED_USER"; |
| 1601 | |
| 1602 | /** |
| 1603 | * Extra containing a name for the user being created. Optional parameter passed to |
| 1604 | * ACTION_CREATE_USER activity. |
| 1605 | * @hide |
| 1606 | */ |
| 1607 | public static final String EXTRA_USER_NAME = "android.os.extra.USER_NAME"; |
| 1608 | |
| 1609 | /** |
| 1610 | * Extra containing account name for the user being created. Optional parameter passed to |
| 1611 | * ACTION_CREATE_USER activity. |
| 1612 | * @hide |
| 1613 | */ |
| 1614 | public static final String EXTRA_USER_ACCOUNT_NAME = "android.os.extra.USER_ACCOUNT_NAME"; |
| 1615 | |
| 1616 | /** |
| 1617 | * Extra containing account type for the user being created. Optional parameter passed to |
| 1618 | * ACTION_CREATE_USER activity. |
| 1619 | * @hide |
| 1620 | */ |
| 1621 | public static final String EXTRA_USER_ACCOUNT_TYPE = "android.os.extra.USER_ACCOUNT_TYPE"; |
| 1622 | |
| 1623 | /** |
| 1624 | * Extra containing account-specific data for the user being created. Optional parameter passed |
| 1625 | * to ACTION_CREATE_USER activity. |
| 1626 | * @hide |
| 1627 | */ |
| 1628 | public static final String EXTRA_USER_ACCOUNT_OPTIONS |
| 1629 | = "android.os.extra.USER_ACCOUNT_OPTIONS"; |
| 1630 | |
| 1631 | /** @hide */ |
| 1632 | public static final int PIN_VERIFICATION_FAILED_INCORRECT = -3; |
| 1633 | /** @hide */ |
| 1634 | public static final int PIN_VERIFICATION_FAILED_NOT_SET = -2; |
| 1635 | /** @hide */ |
| 1636 | public static final int PIN_VERIFICATION_SUCCESS = -1; |
| 1637 | |
| 1638 | /** |
| 1639 | * Sent when user restrictions have changed. |
| 1640 | * |
| 1641 | * @hide |
| 1642 | */ |
| 1643 | @SystemApi // To allow seeing it from CTS. |
| 1644 | public static final String ACTION_USER_RESTRICTIONS_CHANGED = |
| 1645 | "android.os.action.USER_RESTRICTIONS_CHANGED"; |
| 1646 | |
| 1647 | /** |
| 1648 | * Error result indicating that this user is not allowed to add other users on this device. |
| 1649 | * This is a result code returned from the activity created by the intent |
| 1650 | * {@link #createUserCreationIntent(String, String, String, PersistableBundle)}. |
| 1651 | */ |
| 1652 | public static final int USER_CREATION_FAILED_NOT_PERMITTED = Activity.RESULT_FIRST_USER; |
| 1653 | |
| 1654 | /** |
| 1655 | * Error result indicating that no more users can be created on this device. |
| 1656 | * This is a result code returned from the activity created by the intent |
| 1657 | * {@link #createUserCreationIntent(String, String, String, PersistableBundle)}. |
| 1658 | */ |
| 1659 | public static final int USER_CREATION_FAILED_NO_MORE_USERS = Activity.RESULT_FIRST_USER + 1; |
| 1660 | |
| 1661 | /** |
| 1662 | * Indicates that users are switchable. |
| 1663 | * @hide |
| 1664 | */ |
| 1665 | @SystemApi |
| 1666 | public static final int SWITCHABILITY_STATUS_OK = 0; |
| 1667 | |
| 1668 | /** |
| 1669 | * Indicated that the user is in a phone call. |
| 1670 | * @hide |
| 1671 | */ |
| 1672 | @SystemApi |
| 1673 | public static final int SWITCHABILITY_STATUS_USER_IN_CALL = 1 << 0; |
| 1674 | |
| 1675 | /** |
| 1676 | * Indicates that user switching is disallowed ({@link #DISALLOW_USER_SWITCH} is set). |
| 1677 | * @hide |
| 1678 | */ |
| 1679 | @SystemApi |
| 1680 | public static final int SWITCHABILITY_STATUS_USER_SWITCH_DISALLOWED = 1 << 1; |
| 1681 | |
| 1682 | /** |
| 1683 | * Indicates that the system user is locked and user switching is not allowed. |
| 1684 | * @hide |
| 1685 | */ |
| 1686 | @SystemApi |
| 1687 | public static final int SWITCHABILITY_STATUS_SYSTEM_USER_LOCKED = 1 << 2; |
| 1688 | |
| 1689 | /** |
| 1690 | * Result returned in {@link #getUserSwitchability()} indicating user swichability. |
| 1691 | * @hide |
| 1692 | */ |
| 1693 | @Retention(RetentionPolicy.SOURCE) |
| 1694 | @IntDef(flag = true, prefix = { "SWITCHABILITY_STATUS_" }, value = { |
| 1695 | SWITCHABILITY_STATUS_OK, |
| 1696 | SWITCHABILITY_STATUS_USER_IN_CALL, |
| 1697 | SWITCHABILITY_STATUS_USER_SWITCH_DISALLOWED, |
| 1698 | SWITCHABILITY_STATUS_SYSTEM_USER_LOCKED |
| 1699 | }) |
| 1700 | public @interface UserSwitchabilityResult {} |
| 1701 | |
| 1702 | /** |
| 1703 | * A response code from {@link #removeUserWhenPossible(UserHandle, boolean)} indicating that |
| 1704 | * the specified user has been successfully removed. |
| 1705 | * |
| 1706 | * @hide |
| 1707 | */ |
| 1708 | @SystemApi |
| 1709 | public static final int REMOVE_RESULT_REMOVED = 0; |
| 1710 | |
| 1711 | /** |
| 1712 | * A response code from {@link #removeUserWhenPossible(UserHandle, boolean)} indicating that |
| 1713 | * the specified user is marked so that it will be removed when the user is stopped or on boot. |
| 1714 | * |
| 1715 | * @hide |
| 1716 | */ |
| 1717 | @SystemApi |
| 1718 | public static final int REMOVE_RESULT_DEFERRED = 1; |
| 1719 | |
| 1720 | /** |
| 1721 | * A response code from {@link #removeUserWhenPossible(UserHandle, boolean)} indicating that |
| 1722 | * the specified user is already in the process of being removed. |
| 1723 | * |
| 1724 | * @hide |
| 1725 | */ |
| 1726 | @SystemApi |
| 1727 | public static final int REMOVE_RESULT_ALREADY_BEING_REMOVED = 2; |
| 1728 | |
| 1729 | /** |
| 1730 | * A response code from {@link #removeUserWhenPossible(UserHandle, boolean)} indicating that |
| 1731 | * an unknown error occurred that prevented the user from being removed or set as ephemeral. |
| 1732 | * |
| 1733 | * @hide |
| 1734 | */ |
| 1735 | @SystemApi |
| 1736 | public static final int REMOVE_RESULT_ERROR_UNKNOWN = -1; |
| 1737 | |
| 1738 | /** |
| 1739 | * A response code from {@link #removeUserWhenPossible(UserHandle, boolean)} indicating that |
| 1740 | * the user could not be removed due to a {@link #DISALLOW_REMOVE_MANAGED_PROFILE} or |
| 1741 | * {@link #DISALLOW_REMOVE_USER} user restriction. |
| 1742 | * |
| 1743 | * @hide |
| 1744 | */ |
| 1745 | @SystemApi |
| 1746 | public static final int REMOVE_RESULT_ERROR_USER_RESTRICTION = -2; |
| 1747 | |
| 1748 | /** |
| 1749 | * A response code from {@link #removeUserWhenPossible(UserHandle, boolean)} indicating that |
| 1750 | * user being removed does not exist. |
| 1751 | * |
| 1752 | * @hide |
| 1753 | */ |
| 1754 | @SystemApi |
| 1755 | public static final int REMOVE_RESULT_ERROR_USER_NOT_FOUND = -3; |
| 1756 | |
| 1757 | /** |
| 1758 | * A response code from {@link #removeUserWhenPossible(UserHandle, boolean)} indicating that |
| 1759 | * user being removed is a {@link UserHandle#SYSTEM} user which can't be removed. |
| 1760 | * |
| 1761 | * @hide |
| 1762 | */ |
| 1763 | @SystemApi |
| 1764 | public static final int REMOVE_RESULT_ERROR_SYSTEM_USER = -4; |
| 1765 | |
| 1766 | /** |
| 1767 | * Possible response codes from {@link #removeUserWhenPossible(UserHandle, boolean)}. |
| 1768 | * |
| 1769 | * @hide |
| 1770 | */ |
| 1771 | @IntDef(prefix = { "REMOVE_RESULT_" }, value = { |
| 1772 | REMOVE_RESULT_REMOVED, |
| 1773 | REMOVE_RESULT_DEFERRED, |
| 1774 | REMOVE_RESULT_ALREADY_BEING_REMOVED, |
| 1775 | REMOVE_RESULT_ERROR_USER_RESTRICTION, |
| 1776 | REMOVE_RESULT_ERROR_USER_NOT_FOUND, |
| 1777 | REMOVE_RESULT_ERROR_SYSTEM_USER, |
| 1778 | REMOVE_RESULT_ERROR_UNKNOWN, |
| 1779 | }) |
| 1780 | @Retention(RetentionPolicy.SOURCE) |
| 1781 | public @interface RemoveResult {} |
| 1782 | |
| 1783 | /** |
| 1784 | * Indicates user operation is successful. |
| 1785 | */ |
| 1786 | public static final int USER_OPERATION_SUCCESS = 0; |
| 1787 | |
| 1788 | /** |
| 1789 | * Indicates user operation failed for unknown reason. |
| 1790 | */ |
| 1791 | public static final int USER_OPERATION_ERROR_UNKNOWN = 1; |
| 1792 | |
| 1793 | /** |
| 1794 | * Indicates user operation failed because target user is a managed profile. |
| 1795 | */ |
| 1796 | public static final int USER_OPERATION_ERROR_MANAGED_PROFILE = 2; |
| 1797 | |
| 1798 | /** |
| 1799 | * Indicates user operation failed because maximum running user limit has been reached. |
| 1800 | */ |
| 1801 | public static final int USER_OPERATION_ERROR_MAX_RUNNING_USERS = 3; |
| 1802 | |
| 1803 | /** |
| 1804 | * Indicates user operation failed because the target user is in the foreground. |
| 1805 | */ |
| 1806 | public static final int USER_OPERATION_ERROR_CURRENT_USER = 4; |
| 1807 | |
| 1808 | /** |
| 1809 | * Indicates user operation failed because device has low data storage. |
| 1810 | */ |
| 1811 | public static final int USER_OPERATION_ERROR_LOW_STORAGE = 5; |
| 1812 | |
| 1813 | /** |
| 1814 | * Indicates user operation failed because maximum user limit has been reached. |
| 1815 | */ |
| 1816 | public static final int USER_OPERATION_ERROR_MAX_USERS = 6; |
| 1817 | |
| 1818 | /** |
| 1819 | * Indicates user operation failed because a user with that account already exists. |
| 1820 | * |
| 1821 | * @hide |
| 1822 | */ |
| 1823 | @SystemApi |
| 1824 | public static final int USER_OPERATION_ERROR_USER_ACCOUNT_ALREADY_EXISTS = 7; |
| 1825 | |
| 1826 | /** |
| 1827 | * Result returned from various user operations. |
| 1828 | * |
| 1829 | * @hide |
| 1830 | */ |
| 1831 | @Retention(RetentionPolicy.SOURCE) |
| 1832 | @IntDef(prefix = { "USER_OPERATION_" }, value = { |
| 1833 | USER_OPERATION_SUCCESS, |
| 1834 | USER_OPERATION_ERROR_UNKNOWN, |
| 1835 | USER_OPERATION_ERROR_MANAGED_PROFILE, |
| 1836 | USER_OPERATION_ERROR_MAX_RUNNING_USERS, |
| 1837 | USER_OPERATION_ERROR_CURRENT_USER, |
| 1838 | USER_OPERATION_ERROR_LOW_STORAGE, |
| 1839 | USER_OPERATION_ERROR_MAX_USERS, |
| 1840 | USER_OPERATION_ERROR_USER_ACCOUNT_ALREADY_EXISTS |
| 1841 | }) |
| 1842 | public @interface UserOperationResult {} |
| 1843 | |
| 1844 | /** |
| 1845 | * Thrown to indicate user operation failed. |
| 1846 | */ |
| 1847 | public static class UserOperationException extends RuntimeException { |
| 1848 | private final @UserOperationResult int mUserOperationResult; |
| 1849 | |
| 1850 | /** |
| 1851 | * Constructs a UserOperationException with specific result code. |
| 1852 | * |
| 1853 | * @param message the detail message |
| 1854 | * @param userOperationResult the result code |
| 1855 | * @hide |
| 1856 | */ |
| 1857 | public UserOperationException(String message, |
| 1858 | @UserOperationResult int userOperationResult) { |
| 1859 | super(message); |
| 1860 | mUserOperationResult = userOperationResult; |
| 1861 | } |
| 1862 | |
| 1863 | /** |
| 1864 | * Returns the operation result code. |
| 1865 | */ |
| 1866 | public @UserOperationResult int getUserOperationResult() { |
| 1867 | return mUserOperationResult; |
| 1868 | } |
| 1869 | |
| 1870 | /** |
| 1871 | * Returns a UserOperationException containing the same message and error code. |
| 1872 | * @hide |
| 1873 | */ |
| 1874 | public static UserOperationException from(ServiceSpecificException exception) { |
| 1875 | return new UserOperationException(exception.getMessage(), exception.errorCode); |
| 1876 | } |
| 1877 | } |
| 1878 | |
| 1879 | /** |
| 1880 | * Converts the ServiceSpecificException into a UserOperationException or throws null; |
| 1881 | * |
| 1882 | * @param exception exception to convert. |
| 1883 | * @param throwInsteadOfNull if an exception should be thrown or null returned. |
| 1884 | * @return null if chosen not to throw exception. |
| 1885 | * @throws UserOperationException |
| 1886 | */ |
| 1887 | private <T> T returnNullOrThrowUserOperationException(ServiceSpecificException exception, |
| 1888 | boolean throwInsteadOfNull) throws UserOperationException { |
| 1889 | if (throwInsteadOfNull) { |
| 1890 | throw UserOperationException.from(exception); |
| 1891 | } else { |
| 1892 | return null; |
| 1893 | } |
| 1894 | } |
| 1895 | |
| 1896 | /** |
| 1897 | * Thrown to indicate user operation failed. (Checked exception) |
| 1898 | * @hide |
| 1899 | */ |
| 1900 | public static class CheckedUserOperationException extends AndroidException { |
| 1901 | private final @UserOperationResult int mUserOperationResult; |
| 1902 | |
| 1903 | /** |
| 1904 | * Constructs a CheckedUserOperationException with specific result code. |
| 1905 | * |
| 1906 | * @param message the detail message |
| 1907 | * @param userOperationResult the result code |
| 1908 | * @hide |
| 1909 | */ |
| 1910 | public CheckedUserOperationException(String message, |
| 1911 | @UserOperationResult int userOperationResult) { |
| 1912 | super(message); |
| 1913 | mUserOperationResult = userOperationResult; |
| 1914 | } |
| 1915 | |
| 1916 | /** Returns the operation result code. */ |
| 1917 | public @UserOperationResult int getUserOperationResult() { |
| 1918 | return mUserOperationResult; |
| 1919 | } |
| 1920 | |
| 1921 | /** Return a ServiceSpecificException containing the same message and error code. */ |
| 1922 | public ServiceSpecificException toServiceSpecificException() { |
| 1923 | return new ServiceSpecificException(mUserOperationResult, getMessage()); |
| 1924 | } |
| 1925 | } |
| 1926 | |
| 1927 | /** |
| 1928 | * For apps targeting {@link Build.VERSION_CODES#TIRAMISU} and above, any UserManager API marked |
| 1929 | * as {@link android.annotation.UserHandleAware @UserHandleAware} will use the context user |
| 1930 | * (rather than the calling user). |
| 1931 | * For apps targeting an SDK version <em>below</em> this, the behaviour |
| 1932 | * depends on the particular method and when it was first introduced: |
| 1933 | * <ul> |
| 1934 | * <li> |
| 1935 | * if the {@literal @}UserHandleAware specifies a |
| 1936 | * {@link android.annotation.UserHandleAware#enabledSinceTargetSdkVersion} of |
| 1937 | * {@link Build.VERSION_CODES#TIRAMISU} the <em>calling</em> user is used. |
| 1938 | * </li> |
| 1939 | * <li> |
| 1940 | * if the {@literal @}UserHandleAware doesn't specify a |
| 1941 | * {@link android.annotation.UserHandleAware#enabledSinceTargetSdkVersion}, the |
| 1942 | * <em>context</em> user is used. |
| 1943 | * </li> |
| 1944 | * <li>there should currently be no other values used by UserManager for |
| 1945 | * {@link android.annotation.UserHandleAware#enabledSinceTargetSdkVersion}, since all |
| 1946 | * old implicitly user-dependant APIs were updated in that version and anything |
| 1947 | * introduced more recently should already be {@literal @}UserHandleAware. |
| 1948 | * </li> |
| 1949 | * </ul> |
| 1950 | * |
| 1951 | * Note that when an API marked with |
| 1952 | * {@link android.annotation.UserHandleAware#enabledSinceTargetSdkVersion} is run |
| 1953 | * on a device whose OS predates that version, the calling user will be used, since on such a |
| 1954 | * device, the API is not {@literal @}UserHandleAware yet. |
| 1955 | * |
| 1956 | * @hide |
| 1957 | */ |
| 1958 | @ChangeId |
| 1959 | @EnabledSince(targetSdkVersion = Build.VERSION_CODES.TIRAMISU) |
| 1960 | public static final long ALWAYS_USE_CONTEXT_USER = 183155436L; |
| 1961 | |
| 1962 | /** |
| 1963 | * Returns the context user or the calling user, depending on the target SDK. |
| 1964 | * New APIs do not require such gating and therefore should always use mUserId instead. |
| 1965 | * @see #ALWAYS_USE_CONTEXT_USER |
| 1966 | */ |
| 1967 | private @UserIdInt int getContextUserIfAppropriate() { |
| 1968 | if (CompatChanges.isChangeEnabled(ALWAYS_USE_CONTEXT_USER)) { |
| 1969 | return mUserId; |
| 1970 | } else { |
| 1971 | final int callingUser = UserHandle.myUserId(); |
| 1972 | if (callingUser != mUserId) { |
| 1973 | Log.w(TAG, "Using the calling user " + callingUser |
| 1974 | + ", rather than the specified context user " + mUserId |
| 1975 | + ", because API is only UserHandleAware on higher targetSdkVersions.", |
| 1976 | new Throwable()); |
| 1977 | } |
| 1978 | return callingUser; |
| 1979 | } |
| 1980 | } |
| 1981 | |
| 1982 | /** @hide */ |
| 1983 | @UnsupportedAppUsage |
| 1984 | public static UserManager get(Context context) { |
| 1985 | return (UserManager) context.getSystemService(Context.USER_SERVICE); |
| 1986 | } |
| 1987 | |
| 1988 | /** @hide */ |
| 1989 | public UserManager(Context context, IUserManager service) { |
| 1990 | mService = service; |
| 1991 | Context appContext = context.getApplicationContext(); |
| 1992 | mContext = (appContext == null ? context : appContext); |
| 1993 | mUserId = context.getUserId(); |
| 1994 | } |
| 1995 | |
| 1996 | /** |
| 1997 | * Returns whether this device supports multiple users with their own login and customizable |
| 1998 | * space. |
| 1999 | * @return whether the device supports multiple users. |
| 2000 | */ |
| 2001 | public static boolean supportsMultipleUsers() { |
| 2002 | return getMaxSupportedUsers() > 1 |
| 2003 | && SystemProperties.getBoolean("fw.show_multiuserui", |
| 2004 | Resources.getSystem().getBoolean(R.bool.config_enableMultiUserUI)); |
| 2005 | } |
| 2006 | |
| 2007 | /** |
| 2008 | * @hide |
| 2009 | * @return Whether the device is running with split system user. It means the system user and |
| 2010 | * primary user are two separate users. Previously system user and primary user are combined as |
| 2011 | * a single owner user. see @link {android.os.UserHandle#USER_OWNER} |
| 2012 | */ |
| 2013 | @TestApi |
| 2014 | public static boolean isSplitSystemUser() { |
| 2015 | return RoSystemProperties.FW_SYSTEM_USER_SPLIT; |
| 2016 | } |
| 2017 | |
| 2018 | /** |
| 2019 | * @return Whether guest user is always ephemeral |
| 2020 | * @hide |
| 2021 | */ |
| 2022 | public static boolean isGuestUserAlwaysEphemeral() { |
| 2023 | return Resources.getSystem() |
| 2024 | .getBoolean(com.android.internal.R.bool.config_guestUserEphemeral); |
| 2025 | } |
| 2026 | |
| 2027 | /** |
| 2028 | * @return true, when we want to enable user manager API and UX to allow |
| 2029 | * guest user ephemeral state change based on user input |
| 2030 | * @hide |
| 2031 | */ |
| 2032 | public static boolean isGuestUserAllowEphemeralStateChange() { |
| 2033 | return Resources.getSystem() |
| 2034 | .getBoolean(com.android.internal.R.bool.config_guestUserAllowEphemeralStateChange); |
| 2035 | } |
| 2036 | |
| 2037 | /** |
| 2038 | * Checks whether the device is running in a headless system user mode. |
| 2039 | * |
| 2040 | * <p>Headless system user mode means the {@link #isSystemUser() system user} runs system |
| 2041 | * services and some system UI, but it is not associated with any real person and additional |
| 2042 | * users must be created to be associated with real persons. |
| 2043 | * |
| 2044 | * @return whether the device is running in a headless system user mode. |
| 2045 | */ |
| 2046 | public static boolean isHeadlessSystemUserMode() { |
| 2047 | return RoSystemProperties.MULTIUSER_HEADLESS_SYSTEM_USER; |
| 2048 | } |
| 2049 | |
| 2050 | /** |
| 2051 | * @deprecated use {@link #getUserSwitchability()} instead. |
| 2052 | * |
| 2053 | * @removed |
| 2054 | * @hide |
| 2055 | */ |
| 2056 | @Deprecated |
| 2057 | @RequiresPermission(allOf = { |
| 2058 | Manifest.permission.READ_PHONE_STATE, |
| 2059 | Manifest.permission.MANAGE_USERS}, // Can be INTERACT_ACROSS_USERS instead. |
| 2060 | conditional = true) |
| 2061 | @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.R, trackingBug = 170729553) |
| 2062 | @UserHandleAware |
| 2063 | public boolean canSwitchUsers() { |
| 2064 | boolean allowUserSwitchingWhenSystemUserLocked = Settings.Global.getInt( |
| 2065 | mContext.getContentResolver(), |
| 2066 | Settings.Global.ALLOW_USER_SWITCHING_WHEN_SYSTEM_USER_LOCKED, 0) != 0; |
| 2067 | boolean isSystemUserUnlocked = isUserUnlocked(UserHandle.SYSTEM); |
| 2068 | boolean inCall = false; |
| 2069 | TelephonyManager telephonyManager = mContext.getSystemService(TelephonyManager.class); |
| 2070 | if (telephonyManager != null) { |
| 2071 | inCall = telephonyManager.getCallState() != TelephonyManager.CALL_STATE_IDLE; |
| 2072 | } |
| 2073 | boolean isUserSwitchDisallowed = hasUserRestrictionForUser(DISALLOW_USER_SWITCH, mUserId); |
| 2074 | return (allowUserSwitchingWhenSystemUserLocked || isSystemUserUnlocked) && !inCall |
| 2075 | && !isUserSwitchDisallowed; |
| 2076 | } |
| 2077 | |
| 2078 | /** |
| 2079 | * Returns whether switching users is currently allowed for the context user. |
| 2080 | * <p> |
| 2081 | * Switching users is not allowed in the following cases: |
| 2082 | * <li>the user is in a phone call</li> |
| 2083 | * <li>{@link #DISALLOW_USER_SWITCH} is set</li> |
| 2084 | * <li>system user hasn't been unlocked yet</li> |
| 2085 | * |
| 2086 | * @return A {@link UserSwitchabilityResult} flag indicating if the user is switchable. |
| 2087 | * @hide |
| 2088 | */ |
| 2089 | @SystemApi |
| 2090 | @RequiresPermission(allOf = {Manifest.permission.READ_PHONE_STATE, |
| 2091 | android.Manifest.permission.MANAGE_USERS, |
| 2092 | android.Manifest.permission.INTERACT_ACROSS_USERS}, conditional = true) |
| 2093 | @UserHandleAware(enabledSinceTargetSdkVersion = Build.VERSION_CODES.TIRAMISU) |
| 2094 | public @UserSwitchabilityResult int getUserSwitchability() { |
| 2095 | return getUserSwitchability(UserHandle.of(getContextUserIfAppropriate())); |
| 2096 | } |
| 2097 | |
| 2098 | /** |
| 2099 | * Returns whether switching users is currently allowed for the provided user. |
| 2100 | * <p> |
| 2101 | * Switching users is not allowed in the following cases: |
| 2102 | * <li>the user is in a phone call</li> |
| 2103 | * <li>{@link #DISALLOW_USER_SWITCH} is set</li> |
| 2104 | * <li>system user hasn't been unlocked yet</li> |
| 2105 | * |
| 2106 | * @return A {@link UserSwitchabilityResult} flag indicating if the user is switchable. |
| 2107 | * @hide |
| 2108 | */ |
| 2109 | @RequiresPermission(allOf = {Manifest.permission.READ_PHONE_STATE, |
| 2110 | android.Manifest.permission.MANAGE_USERS, |
| 2111 | android.Manifest.permission.INTERACT_ACROSS_USERS}, conditional = true) |
| 2112 | public @UserSwitchabilityResult int getUserSwitchability(UserHandle userHandle) { |
| 2113 | final TelephonyManager tm = |
| 2114 | (TelephonyManager) mContext.getSystemService(Context.TELEPHONY_SERVICE); |
| 2115 | |
| 2116 | int flags = SWITCHABILITY_STATUS_OK; |
| 2117 | if (tm.getCallState() != TelephonyManager.CALL_STATE_IDLE) { |
| 2118 | flags |= SWITCHABILITY_STATUS_USER_IN_CALL; |
| 2119 | } |
| 2120 | if (hasUserRestrictionForUser(DISALLOW_USER_SWITCH, userHandle)) { |
| 2121 | flags |= SWITCHABILITY_STATUS_USER_SWITCH_DISALLOWED; |
| 2122 | } |
| 2123 | |
| 2124 | // System User is always unlocked in Headless System User Mode, so ignore this flag |
| 2125 | if (!isHeadlessSystemUserMode()) { |
| 2126 | final boolean allowUserSwitchingWhenSystemUserLocked = Settings.Global.getInt( |
| 2127 | mContext.getContentResolver(), |
| 2128 | Settings.Global.ALLOW_USER_SWITCHING_WHEN_SYSTEM_USER_LOCKED, 0) != 0; |
| 2129 | final boolean systemUserUnlocked = isUserUnlocked(UserHandle.SYSTEM); |
| 2130 | |
| 2131 | if (!allowUserSwitchingWhenSystemUserLocked && !systemUserUnlocked) { |
| 2132 | flags |= SWITCHABILITY_STATUS_SYSTEM_USER_LOCKED; |
| 2133 | } |
| 2134 | } |
| 2135 | |
| 2136 | return flags; |
| 2137 | } |
| 2138 | |
| 2139 | /** |
| 2140 | * Returns the userId for the context user. |
| 2141 | * |
| 2142 | * @return the userId of the context user. |
| 2143 | * |
| 2144 | * @deprecated To get the <em>calling</em> user, use {@link UserHandle#myUserId()}. |
| 2145 | * To get the <em>context</em> user, get it directly from the context. |
| 2146 | * |
| 2147 | * @hide |
| 2148 | */ |
| 2149 | @Deprecated |
| 2150 | @UnsupportedAppUsage |
| 2151 | @UserHandleAware(enabledSinceTargetSdkVersion = Build.VERSION_CODES.TIRAMISU) |
| 2152 | // *** Do NOT use this in UserManager. Instead always use mUserId. *** |
| 2153 | public @UserIdInt int getUserHandle() { |
| 2154 | return getContextUserIfAppropriate(); |
| 2155 | } |
| 2156 | |
| 2157 | /** |
| 2158 | * Returns the userId for the user that this process is running under |
| 2159 | * (<em>not</em> the context user). |
| 2160 | * |
| 2161 | * @return the userId of <em>this process</em>. |
| 2162 | * |
| 2163 | * @deprecated Use {@link UserHandle#myUserId()} |
| 2164 | * @hide |
| 2165 | */ |
| 2166 | @Deprecated |
| 2167 | // NOT @UserHandleAware |
| 2168 | public @UserIdInt int getProcessUserId() { |
| 2169 | return UserHandle.myUserId(); |
| 2170 | } |
| 2171 | |
| 2172 | /** |
| 2173 | * @return the user type of the context user. |
| 2174 | * @hide |
| 2175 | */ |
| 2176 | @TestApi |
| 2177 | @RequiresPermission(anyOf = { |
| 2178 | android.Manifest.permission.MANAGE_USERS, |
| 2179 | android.Manifest.permission.CREATE_USERS, |
| 2180 | android.Manifest.permission.QUERY_USERS}) |
| 2181 | @UserHandleAware |
| 2182 | public @NonNull String getUserType() { |
| 2183 | UserInfo userInfo = getUserInfo(mUserId); |
| 2184 | return userInfo == null ? "" : userInfo.userType; |
| 2185 | } |
| 2186 | |
| 2187 | /** |
| 2188 | * Returns the user name of the context user. This call is only available to applications on |
| 2189 | * the system image. |
| 2190 | * |
| 2191 | * @return the user name |
| 2192 | */ |
| 2193 | @RequiresPermission(anyOf = { |
| 2194 | android.Manifest.permission.MANAGE_USERS, |
| 2195 | android.Manifest.permission.CREATE_USERS, |
| 2196 | android.Manifest.permission.QUERY_USERS, |
| 2197 | android.Manifest.permission.GET_ACCOUNTS_PRIVILEGED}) |
| 2198 | |
| 2199 | @UserHandleAware( |
| 2200 | requiresAnyOfPermissionsIfNotCaller = { |
| 2201 | android.Manifest.permission.MANAGE_USERS, |
| 2202 | android.Manifest.permission.CREATE_USERS, |
| 2203 | android.Manifest.permission.QUERY_USERS}) |
| 2204 | public @NonNull String getUserName() { |
| 2205 | if (UserHandle.myUserId() == mUserId) { |
| 2206 | try { |
| 2207 | return mService.getUserName(); |
| 2208 | } catch (RemoteException re) { |
| 2209 | throw re.rethrowFromSystemServer(); |
| 2210 | } |
| 2211 | } else { |
| 2212 | UserInfo userInfo = getUserInfo(mUserId); |
| 2213 | if (userInfo != null && userInfo.name != null) { |
| 2214 | return userInfo.name; |
| 2215 | } |
| 2216 | return ""; |
| 2217 | } |
| 2218 | } |
| 2219 | |
| 2220 | /** |
| 2221 | * Returns whether user name has been set. |
| 2222 | * <p>This method can be used to check that the value returned by {@link #getUserName()} was |
| 2223 | * set by the user and is not a placeholder string provided by the system. |
| 2224 | * @hide |
| 2225 | */ |
| 2226 | @SystemApi |
| 2227 | @RequiresPermission(anyOf = { |
| 2228 | android.Manifest.permission.MANAGE_USERS, |
| 2229 | android.Manifest.permission.CREATE_USERS, |
| 2230 | android.Manifest.permission.QUERY_USERS, |
| 2231 | android.Manifest.permission.GET_ACCOUNTS_PRIVILEGED}) |
| 2232 | @UserHandleAware( |
| 2233 | enabledSinceTargetSdkVersion = Build.VERSION_CODES.TIRAMISU, |
| 2234 | requiresAnyOfPermissionsIfNotCaller = { |
| 2235 | android.Manifest.permission.MANAGE_USERS, |
| 2236 | android.Manifest.permission.CREATE_USERS, |
| 2237 | android.Manifest.permission.QUERY_USERS}) |
| 2238 | public boolean isUserNameSet() { |
| 2239 | try { |
| 2240 | return mService.isUserNameSet(getContextUserIfAppropriate()); |
| 2241 | } catch (RemoteException re) { |
| 2242 | throw re.rethrowFromSystemServer(); |
| 2243 | } |
| 2244 | } |
| 2245 | |
| 2246 | /** |
| 2247 | * Used to determine whether the user making this call is subject to |
| 2248 | * teleportations. |
| 2249 | * |
| 2250 | * <p>As of {@link android.os.Build.VERSION_CODES#LOLLIPOP}, this method can |
| 2251 | * now automatically identify goats using advanced goat recognition technology.</p> |
| 2252 | * |
| 2253 | * <p>As of {@link android.os.Build.VERSION_CODES#R}, this method always returns |
| 2254 | * {@code false} in order to protect goat privacy.</p> |
| 2255 | * |
| 2256 | * @return Returns whether the user making this call is a goat. |
| 2257 | */ |
| 2258 | @UserHandleAware(enabledSinceTargetSdkVersion = Build.VERSION_CODES.TIRAMISU) |
| 2259 | public boolean isUserAGoat() { |
| 2260 | if (mContext.getApplicationInfo().targetSdkVersion >= Build.VERSION_CODES.R) { |
| 2261 | return false; |
| 2262 | } |
| 2263 | // Caution: This is NOT @UserHandleAware (because mContext is getApplicationContext and |
| 2264 | // can hold a different userId), but for R+ it returns false, so it doesn't matter anyway. |
| 2265 | return mContext.getPackageManager() |
| 2266 | .isPackageAvailable("com.coffeestainstudios.goatsimulator"); |
| 2267 | } |
| 2268 | |
| 2269 | /** |
| 2270 | * Used to check if the context user is the primary user. The primary user |
| 2271 | * is the first human user on a device. This is not supported in headless system user mode. |
| 2272 | * |
| 2273 | * @return whether the context user is the primary user. |
| 2274 | * @hide |
| 2275 | */ |
| 2276 | @SystemApi |
| 2277 | @RequiresPermission(anyOf = { |
| 2278 | Manifest.permission.MANAGE_USERS, |
| 2279 | Manifest.permission.CREATE_USERS, |
| 2280 | Manifest.permission.QUERY_USERS}) |
| 2281 | @UserHandleAware(enabledSinceTargetSdkVersion = Build.VERSION_CODES.TIRAMISU) |
| 2282 | public boolean isPrimaryUser() { |
| 2283 | final UserInfo user = getUserInfo(getContextUserIfAppropriate()); |
| 2284 | return user != null && user.isPrimary(); |
| 2285 | } |
| 2286 | |
| 2287 | /** |
| 2288 | * Used to check if the context user is the system user. The system user |
| 2289 | * is the initial user that is implicitly created on first boot and hosts most of the |
| 2290 | * system services. |
| 2291 | * |
| 2292 | * @return whether the context user is the system user. |
| 2293 | */ |
| 2294 | @UserHandleAware(enabledSinceTargetSdkVersion = Build.VERSION_CODES.TIRAMISU) |
| 2295 | public boolean isSystemUser() { |
| 2296 | return getContextUserIfAppropriate() == UserHandle.USER_SYSTEM; |
| 2297 | } |
| 2298 | |
| 2299 | /** |
| 2300 | * Used to check if the context user is an admin user. An admin user is allowed to |
| 2301 | * modify or configure certain settings that aren't available to non-admin users, |
| 2302 | * create and delete additional users, etc. There can be more than one admin users. |
| 2303 | * |
| 2304 | * @return whether the context user is an admin user. |
| 2305 | * @hide |
| 2306 | */ |
| 2307 | @SystemApi |
| 2308 | @RequiresPermission(anyOf = { |
| 2309 | Manifest.permission.MANAGE_USERS, |
| 2310 | Manifest.permission.CREATE_USERS, |
| 2311 | Manifest.permission.QUERY_USERS}) |
| 2312 | @UserHandleAware(enabledSinceTargetSdkVersion = Build.VERSION_CODES.TIRAMISU) |
| 2313 | public boolean isAdminUser() { |
| 2314 | return isUserAdmin(getContextUserIfAppropriate()); |
| 2315 | } |
| 2316 | |
| 2317 | /** |
| 2318 | * @hide |
| 2319 | * Returns whether the provided user is an admin user. There can be more than one admin |
| 2320 | * user. |
| 2321 | */ |
| 2322 | @UnsupportedAppUsage |
| 2323 | @RequiresPermission(anyOf = { |
| 2324 | Manifest.permission.MANAGE_USERS, |
| 2325 | Manifest.permission.CREATE_USERS, |
| 2326 | Manifest.permission.QUERY_USERS}) |
| 2327 | public boolean isUserAdmin(@UserIdInt int userId) { |
| 2328 | UserInfo user = getUserInfo(userId); |
| 2329 | return user != null && user.isAdmin(); |
| 2330 | } |
| 2331 | |
| 2332 | /** |
| 2333 | * Returns whether the context user is of the given user type. |
| 2334 | * |
| 2335 | * @param userType the name of the user's user type, e.g. |
| 2336 | * {@link UserManager#USER_TYPE_PROFILE_MANAGED}. |
| 2337 | * @return true if the user is of the given user type. |
| 2338 | * @hide |
| 2339 | */ |
| 2340 | @SystemApi |
| 2341 | @RequiresPermission(anyOf = { |
| 2342 | android.Manifest.permission.MANAGE_USERS, |
| 2343 | android.Manifest.permission.CREATE_USERS, |
| 2344 | android.Manifest.permission.QUERY_USERS}) |
| 2345 | @UserHandleAware |
| 2346 | public boolean isUserOfType(@NonNull String userType) { |
| 2347 | try { |
| 2348 | return mService.isUserOfType(mUserId, userType); |
| 2349 | } catch (RemoteException re) { |
| 2350 | throw re.rethrowFromSystemServer(); |
| 2351 | } |
| 2352 | } |
| 2353 | |
| 2354 | /** |
| 2355 | * Returns whether the user type is a |
| 2356 | * {@link UserManager#USER_TYPE_PROFILE_MANAGED managed profile}. |
| 2357 | * @hide |
| 2358 | */ |
| 2359 | public static boolean isUserTypeManagedProfile(@Nullable String userType) { |
| 2360 | return USER_TYPE_PROFILE_MANAGED.equals(userType); |
| 2361 | } |
| 2362 | |
| 2363 | /** |
| 2364 | * Returns whether the user type is a {@link UserManager#USER_TYPE_FULL_GUEST guest user}. |
| 2365 | * @hide |
| 2366 | */ |
| 2367 | public static boolean isUserTypeGuest(@Nullable String userType) { |
| 2368 | return USER_TYPE_FULL_GUEST.equals(userType); |
| 2369 | } |
| 2370 | |
| 2371 | /** |
| 2372 | * Returns whether the user type is a |
| 2373 | * {@link UserManager#USER_TYPE_FULL_RESTRICTED restricted user}. |
| 2374 | * @hide |
| 2375 | */ |
| 2376 | public static boolean isUserTypeRestricted(@Nullable String userType) { |
| 2377 | return USER_TYPE_FULL_RESTRICTED.equals(userType); |
| 2378 | } |
| 2379 | |
| 2380 | /** |
| 2381 | * Returns whether the user type is a {@link UserManager#USER_TYPE_FULL_DEMO demo user}. |
| 2382 | * @hide |
| 2383 | */ |
| 2384 | public static boolean isUserTypeDemo(@Nullable String userType) { |
| 2385 | return USER_TYPE_FULL_DEMO.equals(userType); |
| 2386 | } |
| 2387 | |
| 2388 | /** |
| 2389 | * Returns whether the user type is a {@link UserManager#USER_TYPE_PROFILE_CLONE clone user}. |
| 2390 | * @hide |
| 2391 | */ |
| 2392 | public static boolean isUserTypeCloneProfile(@Nullable String userType) { |
| 2393 | return USER_TYPE_PROFILE_CLONE.equals(userType); |
| 2394 | } |
| 2395 | |
| 2396 | /** |
| 2397 | * Returns the enum defined in the statsd UserLifecycleJourneyReported atom corresponding to the |
| 2398 | * user type. |
| 2399 | * @hide |
| 2400 | */ |
| 2401 | public static int getUserTypeForStatsd(@NonNull String userType) { |
| 2402 | switch (userType) { |
| 2403 | case USER_TYPE_FULL_SYSTEM: |
| 2404 | return FrameworkStatsLog.USER_LIFECYCLE_JOURNEY_REPORTED__USER_TYPE__FULL_SYSTEM; |
| 2405 | case USER_TYPE_FULL_SECONDARY: |
| 2406 | return FrameworkStatsLog.USER_LIFECYCLE_JOURNEY_REPORTED__USER_TYPE__FULL_SECONDARY; |
| 2407 | case USER_TYPE_FULL_GUEST: |
| 2408 | return FrameworkStatsLog.USER_LIFECYCLE_JOURNEY_REPORTED__USER_TYPE__FULL_GUEST; |
| 2409 | case USER_TYPE_FULL_DEMO: |
| 2410 | return FrameworkStatsLog.USER_LIFECYCLE_JOURNEY_REPORTED__USER_TYPE__FULL_DEMO; |
| 2411 | case USER_TYPE_FULL_RESTRICTED: |
| 2412 | return FrameworkStatsLog |
| 2413 | .USER_LIFECYCLE_JOURNEY_REPORTED__USER_TYPE__FULL_RESTRICTED; |
| 2414 | case USER_TYPE_PROFILE_MANAGED: |
| 2415 | return FrameworkStatsLog |
| 2416 | .USER_LIFECYCLE_JOURNEY_REPORTED__USER_TYPE__PROFILE_MANAGED; |
| 2417 | case USER_TYPE_SYSTEM_HEADLESS: |
| 2418 | return FrameworkStatsLog |
| 2419 | .USER_LIFECYCLE_JOURNEY_REPORTED__USER_TYPE__SYSTEM_HEADLESS; |
| 2420 | case USER_TYPE_PROFILE_CLONE: |
| 2421 | return FrameworkStatsLog |
| 2422 | .USER_LIFECYCLE_JOURNEY_REPORTED__USER_TYPE__PROFILE_CLONE; |
| 2423 | default: |
| 2424 | return FrameworkStatsLog.USER_LIFECYCLE_JOURNEY_REPORTED__USER_TYPE__TYPE_UNKNOWN; |
| 2425 | } |
| 2426 | } |
| 2427 | |
| 2428 | /** |
| 2429 | * @hide |
| 2430 | * @deprecated Use {@link #isRestrictedProfile()} |
| 2431 | */ |
| 2432 | @UnsupportedAppUsage |
| 2433 | @Deprecated |
| 2434 | @UserHandleAware( |
| 2435 | enabledSinceTargetSdkVersion = Build.VERSION_CODES.TIRAMISU, |
| 2436 | requiresAnyOfPermissionsIfNotCaller = { |
| 2437 | android.Manifest.permission.MANAGE_USERS, |
| 2438 | android.Manifest.permission.CREATE_USERS} |
| 2439 | ) |
| 2440 | public boolean isLinkedUser() { |
| 2441 | return isRestrictedProfile(); |
| 2442 | } |
| 2443 | |
| 2444 | /** |
| 2445 | * Used to check if the context user is a restricted profile. Restricted profiles |
| 2446 | * may have a reduced number of available apps, app restrictions, and account restrictions. |
| 2447 | * |
| 2448 | * @return whether the context user is a restricted profile. |
| 2449 | * @hide |
| 2450 | */ |
| 2451 | @SystemApi |
| 2452 | @UserHandleAware( |
| 2453 | enabledSinceTargetSdkVersion = Build.VERSION_CODES.TIRAMISU, |
| 2454 | requiresAnyOfPermissionsIfNotCaller = { |
| 2455 | android.Manifest.permission.MANAGE_USERS, |
| 2456 | android.Manifest.permission.CREATE_USERS} |
| 2457 | ) |
| 2458 | public boolean isRestrictedProfile() { |
| 2459 | try { |
| 2460 | return mService.isRestricted(getContextUserIfAppropriate()); |
| 2461 | } catch (RemoteException re) { |
| 2462 | throw re.rethrowFromSystemServer(); |
| 2463 | } |
| 2464 | } |
| 2465 | |
| 2466 | /** |
| 2467 | * Check if a user is a restricted profile. Restricted profiles may have a reduced number of |
| 2468 | * available apps, app restrictions, and account restrictions. |
| 2469 | * |
| 2470 | * @param user the user to check |
| 2471 | * @return whether the user is a restricted profile. |
| 2472 | * @hide |
| 2473 | */ |
| 2474 | @SystemApi |
| 2475 | @RequiresPermission(anyOf = { |
| 2476 | Manifest.permission.MANAGE_USERS, |
| 2477 | Manifest.permission.CREATE_USERS}, |
| 2478 | conditional = true) |
| 2479 | public boolean isRestrictedProfile(@NonNull UserHandle user) { |
| 2480 | try { |
| 2481 | return mService.isRestricted(user.getIdentifier()); |
| 2482 | } catch (RemoteException re) { |
| 2483 | throw re.rethrowFromSystemServer(); |
| 2484 | } |
| 2485 | } |
| 2486 | |
| 2487 | /** |
| 2488 | * Checks if the calling context user can have a restricted profile. |
| 2489 | * @return whether the context user can have a restricted profile. |
| 2490 | * @hide |
| 2491 | */ |
| 2492 | @SystemApi |
| 2493 | @RequiresPermission(android.Manifest.permission.MANAGE_USERS) |
| 2494 | @UserHandleAware |
| 2495 | public boolean canHaveRestrictedProfile() { |
| 2496 | try { |
| 2497 | return mService.canHaveRestrictedProfile(mUserId); |
| 2498 | } catch (RemoteException re) { |
| 2499 | throw re.rethrowFromSystemServer(); |
| 2500 | } |
| 2501 | } |
| 2502 | |
| 2503 | /** |
| 2504 | * Returns whether the context user has at least one restricted profile associated with it. |
| 2505 | * @return whether the user has a restricted profile associated with it |
| 2506 | * @hide |
| 2507 | */ |
| 2508 | @SystemApi |
| 2509 | @RequiresPermission(android.Manifest.permission.MANAGE_USERS) |
| 2510 | @UserHandleAware(enabledSinceTargetSdkVersion = Build.VERSION_CODES.TIRAMISU) |
| 2511 | public boolean hasRestrictedProfiles() { |
| 2512 | try { |
| 2513 | return mService.hasRestrictedProfiles(getContextUserIfAppropriate()); |
| 2514 | } catch (RemoteException re) { |
| 2515 | throw re.rethrowFromSystemServer(); |
| 2516 | } |
| 2517 | } |
| 2518 | |
| 2519 | /** |
| 2520 | * Get the parent of a restricted profile. |
| 2521 | * |
| 2522 | * @return the parent of the user or {@code null} if the user is not restricted profile |
| 2523 | * @hide |
| 2524 | */ |
| 2525 | @SystemApi |
| 2526 | @RequiresPermission(anyOf = { |
| 2527 | Manifest.permission.MANAGE_USERS, |
| 2528 | Manifest.permission.CREATE_USERS, |
| 2529 | Manifest.permission.QUERY_USERS}) |
| 2530 | @UserHandleAware |
| 2531 | public @Nullable UserHandle getRestrictedProfileParent() { |
| 2532 | final UserInfo info = getUserInfo(mUserId); |
| 2533 | if (info == null) return null; |
| 2534 | if (!info.isRestricted()) return null; |
| 2535 | final int parent = info.restrictedProfileParentId; |
| 2536 | if (parent == UserHandle.USER_NULL) return null; |
| 2537 | return UserHandle.of(parent); |
| 2538 | } |
| 2539 | |
| 2540 | /** |
| 2541 | * Checks if a user is a guest user. |
| 2542 | * @return whether user is a guest user. |
| 2543 | * @hide |
| 2544 | */ |
| 2545 | @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.R, trackingBug = 170729553) |
| 2546 | @RequiresPermission(anyOf = { |
| 2547 | Manifest.permission.MANAGE_USERS, |
| 2548 | Manifest.permission.CREATE_USERS, |
| 2549 | Manifest.permission.QUERY_USERS}) |
| 2550 | public boolean isGuestUser(@UserIdInt int userId) { |
| 2551 | UserInfo user = getUserInfo(userId); |
| 2552 | return user != null && user.isGuest(); |
| 2553 | } |
| 2554 | |
| 2555 | /** |
| 2556 | * Used to check if the context user is a guest user. A guest user may be transient. |
| 2557 | * |
| 2558 | * @return whether the context user is a guest user. |
| 2559 | * @hide |
| 2560 | */ |
| 2561 | @SystemApi |
| 2562 | @RequiresPermission(anyOf = { |
| 2563 | Manifest.permission.MANAGE_USERS, |
| 2564 | Manifest.permission.CREATE_USERS, |
| 2565 | Manifest.permission.QUERY_USERS}) |
| 2566 | @UserHandleAware(enabledSinceTargetSdkVersion = Build.VERSION_CODES.TIRAMISU) |
| 2567 | public boolean isGuestUser() { |
| 2568 | UserInfo user = getUserInfo(getContextUserIfAppropriate()); |
| 2569 | return user != null && user.isGuest(); |
| 2570 | } |
| 2571 | |
| 2572 | |
| 2573 | /** |
| 2574 | * Checks if the context user is a demo user. When running in a demo user, |
| 2575 | * apps can be more helpful to the user, or explain their features in more detail. |
| 2576 | * |
| 2577 | * @return whether the context user is a demo user. |
| 2578 | */ |
| 2579 | @UserHandleAware( |
| 2580 | enabledSinceTargetSdkVersion = Build.VERSION_CODES.TIRAMISU, |
| 2581 | requiresPermissionIfNotCaller = android.Manifest.permission.MANAGE_USERS |
| 2582 | ) |
| 2583 | public boolean isDemoUser() { |
| 2584 | try { |
| 2585 | return mService.isDemoUser(getContextUserIfAppropriate()); |
| 2586 | } catch (RemoteException re) { |
| 2587 | throw re.rethrowFromSystemServer(); |
| 2588 | } |
| 2589 | } |
| 2590 | |
| 2591 | /** |
| 2592 | * Checks if the calling context user is running in a profile. A profile is a user that |
| 2593 | * typically has its own separate data but shares its UI with some parent user. For example, a |
| 2594 | * {@link #isManagedProfile() managed profile} is a type of profile. |
| 2595 | * |
| 2596 | * @return whether the caller is in a profile. |
| 2597 | */ |
| 2598 | @UserHandleAware( |
| 2599 | requiresAnyOfPermissionsIfNotCallerProfileGroup = { |
| 2600 | android.Manifest.permission.MANAGE_USERS, |
| 2601 | android.Manifest.permission.QUERY_USERS, |
| 2602 | android.Manifest.permission.INTERACT_ACROSS_USERS}) |
| 2603 | public boolean isProfile() { |
| 2604 | return isProfile(mUserId); |
| 2605 | } |
| 2606 | |
| 2607 | private boolean isProfile(@UserIdInt int userId) { |
| 2608 | final String profileType = getProfileType(userId); |
| 2609 | return profileType != null && !profileType.equals(""); |
| 2610 | } |
| 2611 | |
| 2612 | /** |
| 2613 | * Returns the user type of the context user if it is a profile. |
| 2614 | * |
| 2615 | * This is a more specific form of {@link #getUserType()} with relaxed permission requirements. |
| 2616 | * |
| 2617 | * @return the user type of the context user if it is a {@link #isProfile() profile}, |
| 2618 | * an empty string if it is not a profile, |
| 2619 | * or null if the user doesn't exist. |
| 2620 | */ |
| 2621 | @UserHandleAware( |
| 2622 | requiresAnyOfPermissionsIfNotCallerProfileGroup = { |
| 2623 | android.Manifest.permission.MANAGE_USERS, |
| 2624 | android.Manifest.permission.QUERY_USERS, |
| 2625 | android.Manifest.permission.INTERACT_ACROSS_USERS}) |
| 2626 | private @Nullable String getProfileType() { |
| 2627 | return getProfileType(mUserId); |
| 2628 | } |
| 2629 | |
| 2630 | /** @see #getProfileType() */ |
| 2631 | private @Nullable String getProfileType(@UserIdInt int userId) { |
| 2632 | // First, the typical case (i.e. the *process* user, not necessarily the context user). |
| 2633 | // This cache cannot be become invalidated since it's about the calling process itself. |
| 2634 | if (userId == UserHandle.myUserId()) { |
| 2635 | // No need for synchronization. Once it becomes non-null, it'll be non-null forever. |
| 2636 | // Worst case we might end up calling the AIDL method multiple times but that's fine. |
| 2637 | if (mProfileTypeOfProcessUser != null) { |
| 2638 | return mProfileTypeOfProcessUser; |
| 2639 | } |
| 2640 | try { |
| 2641 | final String profileType = mService.getProfileType(userId); |
| 2642 | if (profileType != null) { |
| 2643 | return mProfileTypeOfProcessUser = profileType.intern(); |
| 2644 | } |
| 2645 | } catch (RemoteException re) { |
| 2646 | throw re.rethrowFromSystemServer(); |
| 2647 | } |
| 2648 | } |
| 2649 | |
| 2650 | // The userId is not for the process's user. Use a slower cache that handles invalidation. |
| 2651 | return mProfileTypeCache.query(userId); |
| 2652 | } |
| 2653 | |
| 2654 | /** |
| 2655 | * Checks if the context user is a managed profile. |
| 2656 | * |
| 2657 | * Note that this applies specifically to <em>managed</em> profiles. For profiles in general, |
| 2658 | * use {@link #isProfile()} instead. |
| 2659 | * |
| 2660 | * @return whether the context user is a managed profile. |
| 2661 | */ |
| 2662 | @UserHandleAware( |
| 2663 | enabledSinceTargetSdkVersion = Build.VERSION_CODES.TIRAMISU, |
| 2664 | requiresAnyOfPermissionsIfNotCallerProfileGroup = { |
| 2665 | android.Manifest.permission.MANAGE_USERS, |
| 2666 | android.Manifest.permission.QUERY_USERS, |
| 2667 | android.Manifest.permission.INTERACT_ACROSS_USERS}) |
| 2668 | public boolean isManagedProfile() { |
| 2669 | return isManagedProfile(getContextUserIfAppropriate()); |
| 2670 | } |
| 2671 | |
| 2672 | /** |
| 2673 | * Checks if the specified user is a managed profile. |
| 2674 | * Requires {@link android.Manifest.permission#MANAGE_USERS} or |
| 2675 | * {@link android.Manifest.permission#INTERACT_ACROSS_USERS} or |
| 2676 | * {@link android.Manifest.permission#QUERY_USERS} permission, otherwise the caller |
| 2677 | * must be in the same profile group of specified user. |
| 2678 | * |
| 2679 | * Note that this applies specifically to <em>managed</em> profiles. For profiles in general, |
| 2680 | * use {@link #isProfile()} instead. |
| 2681 | * |
| 2682 | * @return whether the specified user is a managed profile. |
| 2683 | * @hide |
| 2684 | */ |
| 2685 | @SystemApi |
| 2686 | @RequiresPermission(anyOf = { |
| 2687 | android.Manifest.permission.MANAGE_USERS, |
| 2688 | android.Manifest.permission.QUERY_USERS, |
| 2689 | android.Manifest.permission.INTERACT_ACROSS_USERS}, conditional = true) |
| 2690 | public boolean isManagedProfile(@UserIdInt int userId) { |
| 2691 | return isUserTypeManagedProfile(getProfileType(userId)); |
| 2692 | } |
| 2693 | |
| 2694 | /** |
| 2695 | * Checks if the context user is a clone profile. |
| 2696 | * |
| 2697 | * @return whether the context user is a clone profile. |
| 2698 | * |
| 2699 | * @see android.os.UserManager#USER_TYPE_PROFILE_CLONE |
| 2700 | * @hide |
| 2701 | */ |
| 2702 | @SystemApi |
| 2703 | @UserHandleAware( |
| 2704 | requiresAnyOfPermissionsIfNotCallerProfileGroup = { |
| 2705 | android.Manifest.permission.MANAGE_USERS, |
| 2706 | android.Manifest.permission.QUERY_USERS, |
| 2707 | android.Manifest.permission.INTERACT_ACROSS_USERS}) |
| 2708 | @SuppressAutoDoc |
| 2709 | public boolean isCloneProfile() { |
| 2710 | return isUserTypeCloneProfile(getProfileType()); |
| 2711 | } |
| 2712 | |
| 2713 | /** |
| 2714 | * Checks if the context user is an ephemeral user. |
| 2715 | * |
| 2716 | * @return whether the context user is an ephemeral user. |
| 2717 | * @hide |
| 2718 | */ |
| 2719 | @RequiresPermission(anyOf = { |
| 2720 | Manifest.permission.MANAGE_USERS, |
| 2721 | Manifest.permission.CREATE_USERS, |
| 2722 | Manifest.permission.QUERY_USERS}) |
| 2723 | @UserHandleAware |
| 2724 | public boolean isEphemeralUser() { |
| 2725 | return isUserEphemeral(mUserId); |
| 2726 | } |
| 2727 | |
| 2728 | /** |
| 2729 | * Returns whether the specified user is ephemeral. |
| 2730 | * @hide |
| 2731 | */ |
| 2732 | @RequiresPermission(anyOf = { |
| 2733 | Manifest.permission.MANAGE_USERS, |
| 2734 | Manifest.permission.CREATE_USERS, |
| 2735 | Manifest.permission.QUERY_USERS}) |
| 2736 | public boolean isUserEphemeral(@UserIdInt int userId) { |
| 2737 | final UserInfo user = getUserInfo(userId); |
| 2738 | return user != null && user.isEphemeral(); |
| 2739 | } |
| 2740 | |
| 2741 | /** |
| 2742 | * Return whether the given user is actively running. This means that |
| 2743 | * the user is in the "started" state, not "stopped" -- it is currently |
| 2744 | * allowed to run code through scheduled alarms, receiving broadcasts, |
| 2745 | * etc. A started user may be either the current foreground user or a |
| 2746 | * background user; the result here does not distinguish between the two. |
| 2747 | * |
| 2748 | * <p>Note prior to Android Nougat MR1 (SDK version <= 24; |
| 2749 | * {@link android.os.Build.VERSION_CODES#N}, this API required a system permission |
| 2750 | * in order to check other profile's status. |
| 2751 | * Since Android Nougat MR1 (SDK version >= 25; |
| 2752 | * {@link android.os.Build.VERSION_CODES#N_MR1}), the restriction has been relaxed, and now |
| 2753 | * it'll accept any {@link android.os.UserHandle} within the same profile group as the caller. |
| 2754 | * |
| 2755 | * @param user The user to retrieve the running state for. |
| 2756 | */ |
| 2757 | @RequiresPermission(anyOf = {Manifest.permission.MANAGE_USERS, |
| 2758 | Manifest.permission.INTERACT_ACROSS_USERS}, conditional = true) |
| 2759 | public boolean isUserRunning(UserHandle user) { |
| 2760 | return isUserRunning(user.getIdentifier()); |
| 2761 | } |
| 2762 | |
| 2763 | /** {@hide} */ |
| 2764 | @RequiresPermission(anyOf = {Manifest.permission.MANAGE_USERS, |
| 2765 | Manifest.permission.INTERACT_ACROSS_USERS}, conditional = true) |
| 2766 | public boolean isUserRunning(@UserIdInt int userId) { |
| 2767 | try { |
| 2768 | return mService.isUserRunning(userId); |
| 2769 | } catch (RemoteException re) { |
| 2770 | throw re.rethrowFromSystemServer(); |
| 2771 | } |
| 2772 | } |
| 2773 | |
| 2774 | /** |
| 2775 | * Return whether the given user is actively running <em>or</em> stopping. |
| 2776 | * This is like {@link #isUserRunning(UserHandle)}, but will also return |
| 2777 | * true if the user had been running but is in the process of being stopped |
| 2778 | * (but is not yet fully stopped, and still running some code). |
| 2779 | * |
| 2780 | * <p>Note prior to Android Nougat MR1 (SDK version <= 24; |
| 2781 | * {@link android.os.Build.VERSION_CODES#N}, this API required a system permission |
| 2782 | * in order to check other profile's status. |
| 2783 | * Since Android Nougat MR1 (SDK version >= 25; |
| 2784 | * {@link android.os.Build.VERSION_CODES#N_MR1}), the restriction has been relaxed, and now |
| 2785 | * it'll accept any {@link android.os.UserHandle} within the same profile group as the caller. |
| 2786 | * |
| 2787 | * @param user The user to retrieve the running state for. |
| 2788 | */ |
| 2789 | @RequiresPermission(anyOf = {Manifest.permission.MANAGE_USERS, |
| 2790 | Manifest.permission.INTERACT_ACROSS_USERS}, conditional = true) |
| 2791 | public boolean isUserRunningOrStopping(UserHandle user) { |
| 2792 | try { |
| 2793 | // TODO: reconcile stopped vs stopping? |
| 2794 | return ActivityManager.getService().isUserRunning( |
| 2795 | user.getIdentifier(), ActivityManager.FLAG_OR_STOPPED); |
| 2796 | } catch (RemoteException re) { |
| 2797 | throw re.rethrowFromSystemServer(); |
| 2798 | } |
| 2799 | } |
| 2800 | |
| 2801 | /** |
| 2802 | * Checks if the context user is running in the foreground. |
| 2803 | * |
| 2804 | * @return whether the context user is running in the foreground. |
| 2805 | */ |
| 2806 | @UserHandleAware |
| 2807 | public boolean isUserForeground() { |
| 2808 | try { |
| 2809 | return mService.isUserForeground(mUserId); |
| 2810 | } catch (RemoteException re) { |
| 2811 | throw re.rethrowFromSystemServer(); |
| 2812 | } |
| 2813 | } |
| 2814 | |
| 2815 | /** |
| 2816 | * Return whether the context user is running in an "unlocked" state. |
| 2817 | * <p> |
| 2818 | * On devices with direct boot, a user is unlocked only after they've |
| 2819 | * entered their credentials (such as a lock pattern or PIN). On devices |
| 2820 | * without direct boot, a user is unlocked as soon as it starts. |
| 2821 | * <p> |
| 2822 | * When a user is locked, only device-protected data storage is available. |
| 2823 | * When a user is unlocked, both device-protected and credential-protected |
| 2824 | * private app data storage is available. |
| 2825 | * |
| 2826 | * @see Intent#ACTION_USER_UNLOCKED |
| 2827 | * @see Context#createDeviceProtectedStorageContext() |
| 2828 | */ |
| 2829 | @UserHandleAware( |
| 2830 | enabledSinceTargetSdkVersion = Build.VERSION_CODES.TIRAMISU, |
| 2831 | requiresAnyOfPermissionsIfNotCallerProfileGroup = { |
| 2832 | android.Manifest.permission.MANAGE_USERS, |
| 2833 | android.Manifest.permission.INTERACT_ACROSS_USERS} |
| 2834 | ) |
| 2835 | public boolean isUserUnlocked() { |
| 2836 | return isUserUnlocked(getContextUserIfAppropriate()); |
| 2837 | } |
| 2838 | |
| 2839 | /** |
| 2840 | * Return whether the given user is running in an "unlocked" state. |
| 2841 | * <p> |
| 2842 | * On devices with direct boot, a user is unlocked only after they've |
| 2843 | * entered their credentials (such as a lock pattern or PIN). On devices |
| 2844 | * without direct boot, a user is unlocked as soon as it starts. |
| 2845 | * <p> |
| 2846 | * When a user is locked, only device-protected data storage is available. |
| 2847 | * When a user is unlocked, both device-protected and credential-protected |
| 2848 | * private app data storage is available. |
| 2849 | * <p>Requires {@code android.permission.MANAGE_USERS} or |
| 2850 | * {@code android.permission.INTERACT_ACROSS_USERS}, otherwise specified {@link UserHandle user} |
| 2851 | * must be the calling user or a profile associated with it. |
| 2852 | * |
| 2853 | * @param user to retrieve the unlocked state for. |
| 2854 | * @see Intent#ACTION_USER_UNLOCKED |
| 2855 | * @see Context#createDeviceProtectedStorageContext() |
| 2856 | */ |
| 2857 | @RequiresPermission(anyOf = {Manifest.permission.MANAGE_USERS, |
| 2858 | Manifest.permission.INTERACT_ACROSS_USERS}, conditional = true) |
| 2859 | public boolean isUserUnlocked(UserHandle user) { |
| 2860 | return isUserUnlocked(user.getIdentifier()); |
| 2861 | } |
| 2862 | |
| 2863 | private static final String CACHE_KEY_IS_USER_UNLOCKED_PROPERTY = |
| 2864 | "cache_key.is_user_unlocked"; |
| 2865 | |
| 2866 | private final PropertyInvalidatedCache<Integer, Boolean> mIsUserUnlockedCache = |
| 2867 | new PropertyInvalidatedCache<Integer, Boolean>( |
| 2868 | 32, CACHE_KEY_IS_USER_UNLOCKED_PROPERTY) { |
| 2869 | @Override |
| 2870 | public Boolean recompute(Integer query) { |
| 2871 | try { |
| 2872 | return mService.isUserUnlocked(query); |
| 2873 | } catch (RemoteException re) { |
| 2874 | throw re.rethrowFromSystemServer(); |
| 2875 | } |
| 2876 | } |
| 2877 | @Override |
| 2878 | public boolean bypass(Integer query) { |
| 2879 | return query < 0; |
| 2880 | } |
| 2881 | }; |
| 2882 | |
| 2883 | // Uses IS_USER_UNLOCKED_PROPERTY for invalidation as the APIs have the same dependencies. |
| 2884 | private final PropertyInvalidatedCache<Integer, Boolean> mIsUserUnlockingOrUnlockedCache = |
| 2885 | new PropertyInvalidatedCache<Integer, Boolean>( |
| 2886 | 32, CACHE_KEY_IS_USER_UNLOCKED_PROPERTY) { |
| 2887 | @Override |
| 2888 | public Boolean recompute(Integer query) { |
| 2889 | try { |
| 2890 | return mService.isUserUnlockingOrUnlocked(query); |
| 2891 | } catch (RemoteException re) { |
| 2892 | throw re.rethrowFromSystemServer(); |
| 2893 | } |
| 2894 | } |
| 2895 | @Override |
| 2896 | public boolean bypass(Integer query) { |
| 2897 | return query < 0; |
| 2898 | } |
| 2899 | }; |
| 2900 | |
| 2901 | /** {@hide} */ |
| 2902 | @UnsupportedAppUsage |
| 2903 | @RequiresPermission(anyOf = {Manifest.permission.MANAGE_USERS, |
| 2904 | Manifest.permission.INTERACT_ACROSS_USERS}, conditional = true) |
| 2905 | public boolean isUserUnlocked(@UserIdInt int userId) { |
| 2906 | return mIsUserUnlockedCache.query(userId); |
| 2907 | } |
| 2908 | |
| 2909 | /** {@hide} */ |
| 2910 | public void disableIsUserUnlockedCache() { |
| 2911 | mIsUserUnlockedCache.disableLocal(); |
| 2912 | mIsUserUnlockingOrUnlockedCache.disableLocal(); |
| 2913 | } |
| 2914 | |
| 2915 | /** {@hide} */ |
| 2916 | public static final void invalidateIsUserUnlockedCache() { |
| 2917 | PropertyInvalidatedCache.invalidateCache(CACHE_KEY_IS_USER_UNLOCKED_PROPERTY); |
| 2918 | } |
| 2919 | |
| 2920 | /** |
| 2921 | * Return whether the provided user is already running in an |
| 2922 | * "unlocked" state or in the process of unlocking. |
| 2923 | * <p> |
| 2924 | * On devices with direct boot, a user is unlocked only after they've |
| 2925 | * entered their credentials (such as a lock pattern or PIN). On devices |
| 2926 | * without direct boot, a user is unlocked as soon as it starts. |
| 2927 | * <p> |
| 2928 | * When a user is locked, only device-protected data storage is available. |
| 2929 | * When a user is unlocked, both device-protected and credential-protected |
| 2930 | * private app data storage is available. |
| 2931 | * |
| 2932 | * <p>Requires {@code android.permission.MANAGE_USERS} or |
| 2933 | * {@code android.permission.INTERACT_ACROSS_USERS}, otherwise specified {@link UserHandle user} |
| 2934 | * must be the calling user or a profile associated with it. |
| 2935 | * |
| 2936 | * @hide |
| 2937 | */ |
| 2938 | @SystemApi |
| 2939 | @RequiresPermission(anyOf = {Manifest.permission.MANAGE_USERS, |
| 2940 | Manifest.permission.INTERACT_ACROSS_USERS}, conditional = true) |
| 2941 | public boolean isUserUnlockingOrUnlocked(@NonNull UserHandle user) { |
| 2942 | return isUserUnlockingOrUnlocked(user.getIdentifier()); |
| 2943 | } |
| 2944 | |
| 2945 | /** {@hide} */ |
| 2946 | @RequiresPermission(anyOf = {Manifest.permission.MANAGE_USERS, |
| 2947 | Manifest.permission.INTERACT_ACROSS_USERS}, conditional = true) |
| 2948 | public boolean isUserUnlockingOrUnlocked(@UserIdInt int userId) { |
| 2949 | return mIsUserUnlockingOrUnlockedCache.query(userId); |
| 2950 | } |
| 2951 | |
| 2952 | /** |
| 2953 | * Return the time when the calling user started in elapsed milliseconds since boot, |
| 2954 | * or 0 if not started. |
| 2955 | * |
| 2956 | * @hide |
| 2957 | */ |
| 2958 | @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.R, trackingBug = 170729553) |
| 2959 | // NOT @UserHandleAware |
| 2960 | public long getUserStartRealtime() { |
| 2961 | if (getContextUserIfAppropriate() != UserHandle.myUserId()) { |
| 2962 | // Note: If we want to support this in the future, also annotate with |
| 2963 | // @UserHandleAware(enabledSinceTargetSdkVersion = Build.VERSION_CODES.TIRAMISU) |
| 2964 | throw new IllegalArgumentException("Calling from a context differing from the calling " |
| 2965 | + "user is not currently supported."); |
| 2966 | } |
| 2967 | try { |
| 2968 | return mService.getUserStartRealtime(); |
| 2969 | } catch (RemoteException re) { |
| 2970 | throw re.rethrowFromSystemServer(); |
| 2971 | } |
| 2972 | } |
| 2973 | |
| 2974 | /** |
| 2975 | * Return the time when the context user was unlocked elapsed milliseconds since boot, |
| 2976 | * or 0 if not unlocked. |
| 2977 | * |
| 2978 | * @hide |
| 2979 | */ |
| 2980 | @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.R, trackingBug = 170729553) |
| 2981 | // NOT @UserHandleAware |
| 2982 | public long getUserUnlockRealtime() { |
| 2983 | if (getContextUserIfAppropriate() != UserHandle.myUserId()) { |
| 2984 | // Note: If we want to support this in the future, also annotate with |
| 2985 | // @UserHandleAware(enabledSinceTargetSdkVersion = Build.VERSION_CODES.TIRAMISU) |
| 2986 | throw new IllegalArgumentException("Calling from a context differing from the calling " |
| 2987 | + "user is not currently supported."); |
| 2988 | } |
| 2989 | try { |
| 2990 | return mService.getUserUnlockRealtime(); |
| 2991 | } catch (RemoteException re) { |
| 2992 | throw re.rethrowFromSystemServer(); |
| 2993 | } |
| 2994 | } |
| 2995 | |
| 2996 | /** |
| 2997 | * Returns the UserInfo object describing a specific user. |
| 2998 | * @param userId the user handle of the user whose information is being requested. |
| 2999 | * @return the UserInfo object for a specific user. |
| 3000 | * @hide |
| 3001 | */ |
| 3002 | @UnsupportedAppUsage |
| 3003 | @RequiresPermission(anyOf = { |
| 3004 | Manifest.permission.MANAGE_USERS, |
| 3005 | Manifest.permission.CREATE_USERS, |
| 3006 | Manifest.permission.QUERY_USERS}) |
| 3007 | public UserInfo getUserInfo(@UserIdInt int userId) { |
| 3008 | try { |
| 3009 | return mService.getUserInfo(userId); |
| 3010 | } catch (RemoteException re) { |
| 3011 | throw re.rethrowFromSystemServer(); |
| 3012 | } |
| 3013 | } |
| 3014 | |
| 3015 | /** |
| 3016 | * @hide |
| 3017 | * |
| 3018 | * Returns who set a user restriction on a user. |
| 3019 | * @param restrictionKey the string key representing the restriction |
| 3020 | * @param userHandle the UserHandle of the user for whom to retrieve the restrictions. |
| 3021 | * @return The source of user restriction. Any combination of {@link #RESTRICTION_NOT_SET}, |
| 3022 | * {@link #RESTRICTION_SOURCE_SYSTEM}, {@link #RESTRICTION_SOURCE_DEVICE_OWNER} |
| 3023 | * and {@link #RESTRICTION_SOURCE_PROFILE_OWNER} |
| 3024 | * @deprecated use {@link #getUserRestrictionSources(String, int)} instead. |
| 3025 | */ |
| 3026 | @Deprecated |
| 3027 | @SystemApi |
| 3028 | @UserRestrictionSource |
| 3029 | @RequiresPermission(anyOf = { |
| 3030 | Manifest.permission.MANAGE_USERS, |
| 3031 | Manifest.permission.QUERY_USERS}) |
| 3032 | public int getUserRestrictionSource(@UserRestrictionKey String restrictionKey, |
| 3033 | UserHandle userHandle) { |
| 3034 | try { |
| 3035 | return mService.getUserRestrictionSource(restrictionKey, userHandle.getIdentifier()); |
| 3036 | } catch (RemoteException re) { |
| 3037 | throw re.rethrowFromSystemServer(); |
| 3038 | } |
| 3039 | } |
| 3040 | |
| 3041 | /** |
| 3042 | * @hide |
| 3043 | * |
| 3044 | * Returns a list of users who set a user restriction on a given user. |
| 3045 | * @param restrictionKey the string key representing the restriction |
| 3046 | * @param userHandle the UserHandle of the user for whom to retrieve the restrictions. |
| 3047 | * @return a list of user ids enforcing this restriction. |
| 3048 | */ |
| 3049 | @SystemApi |
| 3050 | @RequiresPermission(anyOf = { |
| 3051 | android.Manifest.permission.MANAGE_USERS, |
| 3052 | android.Manifest.permission.QUERY_USERS}) |
| 3053 | public List<EnforcingUser> getUserRestrictionSources( |
| 3054 | @UserRestrictionKey String restrictionKey, UserHandle userHandle) { |
| 3055 | try { |
| 3056 | return mService.getUserRestrictionSources(restrictionKey, userHandle.getIdentifier()); |
| 3057 | } catch (RemoteException re) { |
| 3058 | throw re.rethrowFromSystemServer(); |
| 3059 | } |
| 3060 | } |
| 3061 | |
| 3062 | /** |
| 3063 | * Returns the user-wide restrictions imposed on the context user. |
| 3064 | * @return a Bundle containing all the restrictions. |
| 3065 | */ |
| 3066 | @UserHandleAware( |
| 3067 | enabledSinceTargetSdkVersion = Build.VERSION_CODES.TIRAMISU, |
| 3068 | requiresAnyOfPermissionsIfNotCallerProfileGroup = { |
| 3069 | android.Manifest.permission.MANAGE_USERS, |
| 3070 | android.Manifest.permission.INTERACT_ACROSS_USERS} |
| 3071 | ) |
| 3072 | public Bundle getUserRestrictions() { |
| 3073 | try { |
| 3074 | return mService.getUserRestrictions(getContextUserIfAppropriate()); |
| 3075 | } catch (RemoteException re) { |
| 3076 | throw re.rethrowFromSystemServer(); |
| 3077 | } |
| 3078 | } |
| 3079 | |
| 3080 | /** |
| 3081 | * Returns the user-wide restrictions imposed on the user specified by <code>userHandle</code>. |
| 3082 | * @param userHandle the UserHandle of the user for whom to retrieve the restrictions. |
| 3083 | * @return a Bundle containing all the restrictions. |
| 3084 | * |
| 3085 | * <p>Requires {@code android.permission.MANAGE_USERS} or |
| 3086 | * {@code android.permission.INTERACT_ACROSS_USERS}, otherwise specified {@link UserHandle user} |
| 3087 | * must be the calling user or a profile associated with it. |
| 3088 | */ |
| 3089 | @RequiresPermission(anyOf = { |
| 3090 | android.Manifest.permission.MANAGE_USERS, |
| 3091 | android.Manifest.permission.INTERACT_ACROSS_USERS}, conditional = true) |
| 3092 | public Bundle getUserRestrictions(UserHandle userHandle) { |
| 3093 | try { |
| 3094 | return mService.getUserRestrictions(userHandle.getIdentifier()); |
| 3095 | } catch (RemoteException re) { |
| 3096 | throw re.rethrowFromSystemServer(); |
| 3097 | } |
| 3098 | } |
| 3099 | |
| 3100 | /** |
| 3101 | * @hide |
| 3102 | * Returns whether the given user has been disallowed from performing certain actions |
| 3103 | * or setting certain settings through UserManager (e.g. this type of restriction would prevent |
| 3104 | * the guest user from doing certain things, such as making calls). This method disregards |
| 3105 | * restrictions set by device policy. |
| 3106 | * @param restrictionKey the string key representing the restriction |
| 3107 | * @param userHandle the UserHandle of the user for whom to retrieve the restrictions. |
| 3108 | */ |
| 3109 | @TestApi |
| 3110 | @UnsupportedAppUsage |
| 3111 | @RequiresPermission(anyOf = {Manifest.permission.MANAGE_USERS, |
| 3112 | Manifest.permission.CREATE_USERS}) |
| 3113 | public boolean hasBaseUserRestriction(@UserRestrictionKey @NonNull String restrictionKey, |
| 3114 | @NonNull UserHandle userHandle) { |
| 3115 | try { |
| 3116 | return mService.hasBaseUserRestriction(restrictionKey, userHandle.getIdentifier()); |
| 3117 | } catch (RemoteException re) { |
| 3118 | throw re.rethrowFromSystemServer(); |
| 3119 | } |
| 3120 | } |
| 3121 | |
| 3122 | /** |
| 3123 | * This will no longer work. Device owners and profile owners should use |
| 3124 | * {@link DevicePolicyManager#addUserRestriction(ComponentName, String)} instead. |
| 3125 | */ |
| 3126 | // System apps should use UserManager.setUserRestriction() instead. |
| 3127 | @Deprecated |
| 3128 | public void setUserRestrictions(Bundle restrictions) { |
| 3129 | throw new UnsupportedOperationException("This method is no longer supported"); |
| 3130 | } |
| 3131 | |
| 3132 | /** |
| 3133 | * This will no longer work. Device owners and profile owners should use |
| 3134 | * {@link DevicePolicyManager#addUserRestriction(ComponentName, String)} instead. |
| 3135 | */ |
| 3136 | // System apps should use UserManager.setUserRestriction() instead. |
| 3137 | @Deprecated |
| 3138 | public void setUserRestrictions(Bundle restrictions, UserHandle userHandle) { |
| 3139 | throw new UnsupportedOperationException("This method is no longer supported"); |
| 3140 | } |
| 3141 | |
| 3142 | /** |
| 3143 | * Sets the value of a specific restriction on the context user. |
| 3144 | * Requires the MANAGE_USERS permission. |
| 3145 | * @param key the key of the restriction |
| 3146 | * @param value the value for the restriction |
| 3147 | * @deprecated use {@link android.app.admin.DevicePolicyManager#addUserRestriction( |
| 3148 | * android.content.ComponentName, String)} or |
| 3149 | * {@link android.app.admin.DevicePolicyManager#clearUserRestriction( |
| 3150 | * android.content.ComponentName, String)} instead. |
| 3151 | */ |
| 3152 | @Deprecated |
| 3153 | @RequiresPermission(Manifest.permission.MANAGE_USERS) |
| 3154 | @UserHandleAware(enabledSinceTargetSdkVersion = Build.VERSION_CODES.TIRAMISU) |
| 3155 | public void setUserRestriction(String key, boolean value) { |
| 3156 | try { |
| 3157 | mService.setUserRestriction(key, value, getContextUserIfAppropriate()); |
| 3158 | } catch (RemoteException re) { |
| 3159 | throw re.rethrowFromSystemServer(); |
| 3160 | } |
| 3161 | } |
| 3162 | |
| 3163 | /** |
| 3164 | * @hide |
| 3165 | * Sets the value of a specific restriction on a specific user. |
| 3166 | * @param key the key of the restriction |
| 3167 | * @param value the value for the restriction |
| 3168 | * @param userHandle the user whose restriction is to be changed. |
| 3169 | * @deprecated use {@link android.app.admin.DevicePolicyManager#addUserRestriction( |
| 3170 | * android.content.ComponentName, String)} or |
| 3171 | * {@link android.app.admin.DevicePolicyManager#clearUserRestriction( |
| 3172 | * android.content.ComponentName, String)} instead. |
| 3173 | */ |
| 3174 | @Deprecated |
| 3175 | @RequiresPermission(Manifest.permission.MANAGE_USERS) |
| 3176 | public void setUserRestriction(String key, boolean value, UserHandle userHandle) { |
| 3177 | try { |
| 3178 | mService.setUserRestriction(key, value, userHandle.getIdentifier()); |
| 3179 | } catch (RemoteException re) { |
| 3180 | throw re.rethrowFromSystemServer(); |
| 3181 | } |
| 3182 | } |
| 3183 | |
| 3184 | /** |
| 3185 | * Returns whether the context user has been disallowed from performing certain actions |
| 3186 | * or setting certain settings. |
| 3187 | * |
| 3188 | * @param restrictionKey The string key representing the restriction. |
| 3189 | * @return {@code true} if the context user has the given restriction, {@code false} otherwise. |
| 3190 | */ |
| 3191 | @UserHandleAware( |
| 3192 | enabledSinceTargetSdkVersion = Build.VERSION_CODES.TIRAMISU, |
| 3193 | requiresAnyOfPermissionsIfNotCallerProfileGroup = { |
| 3194 | android.Manifest.permission.MANAGE_USERS, |
| 3195 | android.Manifest.permission.INTERACT_ACROSS_USERS} |
| 3196 | ) |
| 3197 | public boolean hasUserRestriction(@UserRestrictionKey String restrictionKey) { |
| 3198 | return hasUserRestrictionForUser(restrictionKey, getContextUserIfAppropriate()); |
| 3199 | } |
| 3200 | |
| 3201 | /** |
| 3202 | * @hide |
| 3203 | * Returns whether the given user has been disallowed from performing certain actions |
| 3204 | * or setting certain settings. |
| 3205 | * @param restrictionKey the string key representing the restriction |
| 3206 | * @param userHandle the UserHandle of the user for whom to retrieve the restrictions. |
| 3207 | * @deprecated Use {@link #hasUserRestrictionForUser(String, UserHandle)} instead. |
| 3208 | */ |
| 3209 | @UnsupportedAppUsage |
| 3210 | @RequiresPermission(anyOf = { |
| 3211 | android.Manifest.permission.MANAGE_USERS, |
| 3212 | android.Manifest.permission.INTERACT_ACROSS_USERS}, conditional = true) |
| 3213 | @Deprecated |
| 3214 | public boolean hasUserRestriction(@UserRestrictionKey String restrictionKey, |
| 3215 | UserHandle userHandle) { |
| 3216 | return hasUserRestrictionForUser(restrictionKey, userHandle); |
| 3217 | } |
| 3218 | |
| 3219 | /** |
| 3220 | * Returns whether the given user has been disallowed from performing certain actions |
| 3221 | * or setting certain settings. |
| 3222 | * @param restrictionKey the string key representing the restriction |
| 3223 | * @param userHandle the UserHandle of the user for whom to retrieve the restrictions. |
| 3224 | * |
| 3225 | * <p>Requires {@code android.permission.MANAGE_USERS} or |
| 3226 | * {@code android.permission.INTERACT_ACROSS_USERS}, otherwise specified {@link UserHandle user} |
| 3227 | * must be the calling user or a profile associated with it. |
| 3228 | * |
| 3229 | * @hide |
| 3230 | */ |
| 3231 | @SystemApi |
| 3232 | @RequiresPermission(anyOf = { |
| 3233 | android.Manifest.permission.MANAGE_USERS, |
| 3234 | android.Manifest.permission.INTERACT_ACROSS_USERS}, conditional = true) |
| 3235 | public boolean hasUserRestrictionForUser(@NonNull @UserRestrictionKey String restrictionKey, |
| 3236 | @NonNull UserHandle userHandle) { |
| 3237 | return hasUserRestrictionForUser(restrictionKey, userHandle.getIdentifier()); |
| 3238 | } |
| 3239 | |
| 3240 | @RequiresPermission(anyOf = { |
| 3241 | android.Manifest.permission.MANAGE_USERS, |
| 3242 | android.Manifest.permission.INTERACT_ACROSS_USERS}, conditional = true) |
| 3243 | private boolean hasUserRestrictionForUser(@NonNull @UserRestrictionKey String restrictionKey, |
| 3244 | @UserIdInt int userId) { |
| 3245 | try { |
| 3246 | return mService.hasUserRestriction(restrictionKey, userId); |
| 3247 | } catch (RemoteException re) { |
| 3248 | throw re.rethrowFromSystemServer(); |
| 3249 | } |
| 3250 | } |
| 3251 | |
| 3252 | /** |
| 3253 | * @hide |
| 3254 | * Returns whether any user on the device has the given user restriction set. |
| 3255 | */ |
| 3256 | public boolean hasUserRestrictionOnAnyUser(@UserRestrictionKey String restrictionKey) { |
| 3257 | try { |
| 3258 | return mService.hasUserRestrictionOnAnyUser(restrictionKey); |
| 3259 | } catch (RemoteException re) { |
| 3260 | throw re.rethrowFromSystemServer(); |
| 3261 | } |
| 3262 | } |
| 3263 | |
| 3264 | /** |
| 3265 | * @hide |
| 3266 | * |
| 3267 | * Checks whether changing the given setting to the given value is prohibited |
| 3268 | * by the corresponding user restriction in the given user. |
| 3269 | * |
| 3270 | * May only be called by the OS itself. |
| 3271 | * |
| 3272 | * @return {@code true} if the change is prohibited, {@code false} if the change is allowed. |
| 3273 | */ |
| 3274 | public boolean isSettingRestrictedForUser(String setting, @UserIdInt int userId, |
| 3275 | String value, int callingUid) { |
| 3276 | try { |
| 3277 | return mService.isSettingRestrictedForUser(setting, userId, value, callingUid); |
| 3278 | } catch (RemoteException re) { |
| 3279 | throw re.rethrowFromSystemServer(); |
| 3280 | } |
| 3281 | } |
| 3282 | |
| 3283 | /** |
| 3284 | * @hide |
| 3285 | * Register a binder callback for user restrictions changes. |
| 3286 | * May only be called by the OS itself. |
| 3287 | */ |
| 3288 | public void addUserRestrictionsListener(final IUserRestrictionsListener listener) { |
| 3289 | try { |
| 3290 | mService.addUserRestrictionsListener(listener); |
| 3291 | } catch (RemoteException re) { |
| 3292 | throw re.rethrowFromSystemServer(); |
| 3293 | } |
| 3294 | } |
| 3295 | |
| 3296 | /** |
| 3297 | * Return the serial number for a user. This is a device-unique |
| 3298 | * number assigned to that user; if the user is deleted and then a new |
| 3299 | * user created, the new users will not be given the same serial number. |
| 3300 | * @param user The user whose serial number is to be retrieved. |
| 3301 | * @return The serial number of the given user; returns -1 if the |
| 3302 | * given UserHandle does not exist. |
| 3303 | * @see #getUserForSerialNumber(long) |
| 3304 | */ |
| 3305 | public long getSerialNumberForUser(UserHandle user) { |
| 3306 | return getUserSerialNumber(user.getIdentifier()); |
| 3307 | } |
| 3308 | |
| 3309 | /** |
| 3310 | * Return the user associated with a serial number previously |
| 3311 | * returned by {@link #getSerialNumberForUser(UserHandle)}. |
| 3312 | * @param serialNumber The serial number of the user that is being |
| 3313 | * retrieved. |
| 3314 | * @return Return the user associated with the serial number, or null |
| 3315 | * if there is not one. |
| 3316 | * @see #getSerialNumberForUser(UserHandle) |
| 3317 | */ |
| 3318 | public UserHandle getUserForSerialNumber(long serialNumber) { |
| 3319 | int ident = getUserHandle((int) serialNumber); |
| 3320 | return ident >= 0 ? new UserHandle(ident) : null; |
| 3321 | } |
| 3322 | |
| 3323 | /** |
| 3324 | * Creates a user with the specified name and options. |
| 3325 | * Default user restrictions will be applied. |
| 3326 | * Requires {@link android.Manifest.permission#MANAGE_USERS} permission. |
| 3327 | * |
| 3328 | * @param name the user's name |
| 3329 | * @param flags UserInfo flags that identify the type of user and other properties. |
| 3330 | * @see UserInfo |
| 3331 | * |
| 3332 | * @return the UserInfo object for the created user, or null if the user could not be created. |
| 3333 | * @throws IllegalArgumentException if flags do not correspond to a valid user type. |
| 3334 | * @deprecated Use {@link #createUser(String, String, int)} instead. |
| 3335 | * @hide |
| 3336 | */ |
| 3337 | @UnsupportedAppUsage |
| 3338 | @Deprecated |
| 3339 | public @Nullable UserInfo createUser(@Nullable String name, @UserInfoFlag int flags) { |
| 3340 | return createUser(name, UserInfo.getDefaultUserType(flags), flags); |
| 3341 | } |
| 3342 | |
| 3343 | /** |
| 3344 | * Creates a user with the specified name and options. |
| 3345 | * Default user restrictions will be applied. |
| 3346 | * |
| 3347 | * <p>Requires {@link android.Manifest.permission#MANAGE_USERS}. |
| 3348 | * {@link android.Manifest.permission#CREATE_USERS} suffices if flags are in |
| 3349 | * com.android.server.pm.UserManagerService#ALLOWED_FLAGS_FOR_CREATE_USERS_PERMISSION}. |
| 3350 | * |
| 3351 | * @param name the user's name |
| 3352 | * @param userType the type of user, such as {@link UserManager#USER_TYPE_FULL_GUEST}. |
| 3353 | * @param flags UserInfo flags that specify user properties. |
| 3354 | * @return the {@link UserInfo} object for the created user, or {@code null} if the user |
| 3355 | * could not be created. |
| 3356 | * |
| 3357 | * @see UserInfo |
| 3358 | * |
| 3359 | * @hide |
| 3360 | */ |
| 3361 | @RequiresPermission(anyOf = {Manifest.permission.MANAGE_USERS, |
| 3362 | Manifest.permission.CREATE_USERS}) |
| 3363 | @TestApi |
| 3364 | public @Nullable UserInfo createUser(@Nullable String name, @NonNull String userType, |
| 3365 | @UserInfoFlag int flags) { |
| 3366 | try { |
| 3367 | return mService.createUserWithThrow(name, userType, flags); |
| 3368 | } catch (ServiceSpecificException e) { |
| 3369 | return null; |
| 3370 | } catch (RemoteException re) { |
| 3371 | throw re.rethrowFromSystemServer(); |
| 3372 | } |
| 3373 | } |
| 3374 | |
| 3375 | /** |
| 3376 | * Creates a user with the specified {@link NewUserRequest}. |
| 3377 | * |
| 3378 | * @param newUserRequest specify the user information |
| 3379 | * |
| 3380 | * @hide |
| 3381 | */ |
| 3382 | @SystemApi |
| 3383 | @RequiresPermission(anyOf = {Manifest.permission.MANAGE_USERS, |
| 3384 | Manifest.permission.CREATE_USERS}) |
| 3385 | public @NonNull NewUserResponse createUser(@NonNull NewUserRequest newUserRequest) { |
| 3386 | try { |
| 3387 | final UserHandle userHandle = mService.createUserWithAttributes( |
| 3388 | newUserRequest.getName(), |
| 3389 | newUserRequest.getUserType(), |
| 3390 | newUserRequest.getFlags(), |
| 3391 | newUserRequest.getUserIcon(), |
| 3392 | newUserRequest.getAccountName(), |
| 3393 | newUserRequest.getAccountType(), |
| 3394 | newUserRequest.getAccountOptions()); |
| 3395 | |
| 3396 | return new NewUserResponse(userHandle, USER_OPERATION_SUCCESS); |
| 3397 | |
| 3398 | } catch (ServiceSpecificException e) { |
| 3399 | Log.w(TAG, "Exception while creating user " + newUserRequest, e); |
| 3400 | return new NewUserResponse(null, e.errorCode); |
| 3401 | } catch (RemoteException re) { |
| 3402 | throw re.rethrowFromSystemServer(); |
| 3403 | } |
| 3404 | } |
| 3405 | |
| 3406 | /** |
| 3407 | * Pre-creates a user of the specified type. Default user restrictions will be applied. |
| 3408 | * |
| 3409 | * <p>This method can be used by OEMs to "warm" up the user creation by pre-creating some users |
| 3410 | * at the first boot, so they when the "real" user is created (for example, |
| 3411 | * by {@link #createUser(String, String, int)} or {@link #createGuest(Context)}), it |
| 3412 | * takes less time. |
| 3413 | * |
| 3414 | * <p>This method completes the majority of work necessary for user creation: it |
| 3415 | * creates user data, CE and DE encryption keys, app data directories, initializes the user and |
| 3416 | * grants default permissions. When pre-created users become "real" users, only then are |
| 3417 | * components notified of new user creation by firing user creation broadcasts. |
| 3418 | * |
| 3419 | * <p>All pre-created users are removed during system upgrade. |
| 3420 | * |
| 3421 | * <p>Requires {@link android.Manifest.permission#MANAGE_USERS}. |
| 3422 | * {@link android.Manifest.permission#CREATE_USERS} suffices if flags are in |
| 3423 | * com.android.server.pm.UserManagerService#ALLOWED_FLAGS_FOR_CREATE_USERS_PERMISSION}. |
| 3424 | * |
| 3425 | * @param userType the type of user, such as {@link UserManager#USER_TYPE_FULL_GUEST}. |
| 3426 | * @return the {@link UserInfo} object for the created user. |
| 3427 | * |
| 3428 | * @throws UserOperationException if the user could not be created. |
| 3429 | * @hide |
| 3430 | */ |
| 3431 | @TestApi |
| 3432 | @RequiresPermission(anyOf = {Manifest.permission.MANAGE_USERS, |
| 3433 | Manifest.permission.CREATE_USERS}) |
| 3434 | public @NonNull UserInfo preCreateUser(@NonNull String userType) |
| 3435 | throws UserOperationException { |
| 3436 | try { |
| 3437 | return mService.preCreateUserWithThrow(userType); |
| 3438 | } catch (ServiceSpecificException e) { |
| 3439 | throw UserOperationException.from(e); |
| 3440 | } catch (RemoteException re) { |
| 3441 | throw re.rethrowFromSystemServer(); |
| 3442 | } |
| 3443 | } |
| 3444 | |
| 3445 | /** |
| 3446 | * Creates a guest user and configures it. |
| 3447 | * @param context an application context |
| 3448 | * @return the {@link UserInfo} object for the created user, or {@code null} if the user |
| 3449 | * could not be created. |
| 3450 | * |
| 3451 | * @hide |
| 3452 | */ |
| 3453 | @RequiresPermission(anyOf = {Manifest.permission.MANAGE_USERS, |
| 3454 | Manifest.permission.CREATE_USERS}) |
| 3455 | public UserInfo createGuest(Context context) { |
| 3456 | try { |
| 3457 | final UserInfo guest = mService.createUserWithThrow(null, USER_TYPE_FULL_GUEST, 0); |
| 3458 | if (guest != null) { |
| 3459 | Settings.Secure.putStringForUser(context.getContentResolver(), |
| 3460 | Settings.Secure.SKIP_FIRST_USE_HINTS, "1", guest.id); |
| 3461 | |
| 3462 | if (UserManager.isGuestUserAllowEphemeralStateChange()) { |
| 3463 | // Mark guest as (changeably) ephemeral if REMOVE_GUEST_ON_EXIT is 1 |
| 3464 | // This is done so that a user via a UI controller can choose to |
| 3465 | // make a guest as ephemeral or not. |
| 3466 | // Settings.Global.REMOVE_GUEST_ON_EXIT holds the choice on what the guest state |
| 3467 | // should be, with default being ephemeral. |
| 3468 | boolean resetGuestOnExit = Settings.Global.getInt(context.getContentResolver(), |
| 3469 | Settings.Global.REMOVE_GUEST_ON_EXIT, 1) == 1; |
| 3470 | |
| 3471 | if (resetGuestOnExit && !guest.isEphemeral()) { |
| 3472 | setUserEphemeral(guest.id, true); |
| 3473 | } |
| 3474 | } |
| 3475 | } |
| 3476 | return guest; |
| 3477 | } catch (ServiceSpecificException e) { |
| 3478 | return null; |
| 3479 | } catch (RemoteException re) { |
| 3480 | throw re.rethrowFromSystemServer(); |
| 3481 | } |
| 3482 | } |
| 3483 | |
| 3484 | /** |
| 3485 | * Gets the existing guest user if it exists. This does not include guest users that are dying. |
| 3486 | * @return The existing guest user if it exists. Null otherwise. |
| 3487 | * @hide |
| 3488 | */ |
| 3489 | @RequiresPermission(android.Manifest.permission.MANAGE_USERS) |
| 3490 | public UserInfo findCurrentGuestUser() { |
| 3491 | try { |
| 3492 | return mService.findCurrentGuestUser(); |
| 3493 | } catch (RemoteException re) { |
| 3494 | throw re.rethrowFromSystemServer(); |
| 3495 | } |
| 3496 | } |
| 3497 | |
| 3498 | /** |
| 3499 | * Creates a user with the specified name and options as a profile of the context's user. |
| 3500 | * |
| 3501 | * @param name the user's name. |
| 3502 | * @param userType the type of user, such as {@link UserManager#USER_TYPE_PROFILE_MANAGED}. |
| 3503 | * @param disallowedPackages packages to not install for this profile. |
| 3504 | * |
| 3505 | * @return the {@link android.os.UserHandle} object for the created user, |
| 3506 | * or throws {@link UserOperationException} if the user could not be created |
| 3507 | * and calling app is targeting {@link android.os.Build.VERSION_CODES#R} or above |
| 3508 | * (otherwise returns {@code null}). |
| 3509 | * |
| 3510 | * @throws UserOperationException if the user could not be created and the calling app is |
| 3511 | * targeting {@link android.os.Build.VERSION_CODES#R} or above. |
| 3512 | * |
| 3513 | * @hide |
| 3514 | */ |
| 3515 | @SystemApi |
| 3516 | @RequiresPermission(anyOf = {Manifest.permission.MANAGE_USERS, |
| 3517 | Manifest.permission.CREATE_USERS}) |
| 3518 | @UserHandleAware |
| 3519 | public @Nullable UserHandle createProfile(@NonNull String name, @NonNull String userType, |
| 3520 | @NonNull Set<String> disallowedPackages) throws UserOperationException { |
| 3521 | try { |
| 3522 | return mService.createProfileForUserWithThrow(name, userType, 0, |
| 3523 | mUserId, disallowedPackages.toArray( |
| 3524 | new String[disallowedPackages.size()])).getUserHandle(); |
| 3525 | } catch (ServiceSpecificException e) { |
| 3526 | return returnNullOrThrowUserOperationException(e, |
| 3527 | mContext.getApplicationInfo().targetSdkVersion >= Build.VERSION_CODES.R); |
| 3528 | } catch (RemoteException re) { |
| 3529 | throw re.rethrowFromSystemServer(); |
| 3530 | } |
| 3531 | } |
| 3532 | |
| 3533 | /** |
| 3534 | * Creates a user with the specified name and options as a profile of another user. |
| 3535 | * <p>Requires MANAGE_USERS. CREATE_USERS suffices for ALLOWED_FLAGS_FOR_CREATE_USERS_PERMISSION |
| 3536 | * |
| 3537 | * @param name the user's name |
| 3538 | * @param flags flags that identify the type of user and other properties. |
| 3539 | * @param userId new user will be a profile of this user. |
| 3540 | * |
| 3541 | * @return the {@link UserInfo} object for the created user, or null if the user |
| 3542 | * could not be created. |
| 3543 | * @throws IllegalArgumentException if flags do not correspond to a valid user type. |
| 3544 | * @deprecated Use {@link #createProfileForUser(String, String, int, int)} instead. |
| 3545 | * @hide |
| 3546 | */ |
| 3547 | @UnsupportedAppUsage |
| 3548 | @RequiresPermission(anyOf = {Manifest.permission.MANAGE_USERS, |
| 3549 | Manifest.permission.CREATE_USERS}) |
| 3550 | @Deprecated |
| 3551 | public UserInfo createProfileForUser(String name, @UserInfoFlag int flags, |
| 3552 | @UserIdInt int userId) { |
| 3553 | return createProfileForUser(name, UserInfo.getDefaultUserType(flags), flags, |
| 3554 | userId, null); |
| 3555 | } |
| 3556 | |
| 3557 | /** |
| 3558 | * Creates a user with the specified name and options as a profile of another user. |
| 3559 | * |
| 3560 | * @param name the user's name |
| 3561 | * @param userType the type of user, such as {@link UserManager#USER_TYPE_PROFILE_MANAGED}. |
| 3562 | * @param flags UserInfo flags that specify user properties. |
| 3563 | * @param userId new user will be a profile of this user. |
| 3564 | * |
| 3565 | * @return the {@link UserInfo} object for the created user, or null if the user |
| 3566 | * could not be created. |
| 3567 | * @hide |
| 3568 | */ |
| 3569 | @RequiresPermission(anyOf = {Manifest.permission.MANAGE_USERS, |
| 3570 | Manifest.permission.CREATE_USERS}) |
| 3571 | public UserInfo createProfileForUser(String name, @NonNull String userType, |
| 3572 | @UserInfoFlag int flags, @UserIdInt int userId) { |
| 3573 | return createProfileForUser(name, userType, flags, userId, null); |
| 3574 | } |
| 3575 | |
| 3576 | /** |
| 3577 | * Version of {@link #createProfileForUser(String, String, int, int)} that allows you to specify |
| 3578 | * any packages that should not be installed in the new profile by default, these packages can |
| 3579 | * still be installed later by the user if needed. |
| 3580 | * |
| 3581 | * @param name the user's name |
| 3582 | * @param userType the type of user, such as {@link UserManager#USER_TYPE_PROFILE_MANAGED}. |
| 3583 | * @param flags UserInfo flags that specify user properties. |
| 3584 | * @param userId new user will be a profile of this user. |
| 3585 | * @param disallowedPackages packages that will not be installed in the profile being created. |
| 3586 | * |
| 3587 | * @return the {@link UserInfo} object for the created user, or {@code null} if the user could |
| 3588 | * not be created. |
| 3589 | * |
| 3590 | * @hide |
| 3591 | */ |
| 3592 | @TestApi |
| 3593 | @RequiresPermission(anyOf = {Manifest.permission.MANAGE_USERS, |
| 3594 | Manifest.permission.CREATE_USERS}) |
| 3595 | public @Nullable UserInfo createProfileForUser(@Nullable String name, @NonNull String userType, |
| 3596 | @UserInfoFlag int flags, @UserIdInt int userId, @Nullable String[] disallowedPackages) { |
| 3597 | try { |
| 3598 | return mService.createProfileForUserWithThrow(name, userType, flags, userId, |
| 3599 | disallowedPackages); |
| 3600 | } catch (ServiceSpecificException e) { |
| 3601 | return null; |
| 3602 | } catch (RemoteException re) { |
| 3603 | throw re.rethrowFromSystemServer(); |
| 3604 | } |
| 3605 | } |
| 3606 | |
| 3607 | /** |
| 3608 | * Similar to {@link #createProfileForUser(String, String, int, int, String[])} |
| 3609 | * except bypassing the checking of {@link UserManager#DISALLOW_ADD_MANAGED_PROFILE}. |
| 3610 | * |
| 3611 | * @see #createProfileForUser(String, String, int, int, String[]) |
| 3612 | * @hide |
| 3613 | */ |
| 3614 | @RequiresPermission(anyOf = {Manifest.permission.MANAGE_USERS, |
| 3615 | Manifest.permission.CREATE_USERS}) |
| 3616 | public UserInfo createProfileForUserEvenWhenDisallowed(String name, |
| 3617 | @NonNull String userType, @UserInfoFlag int flags, @UserIdInt int userId, |
| 3618 | String[] disallowedPackages) { |
| 3619 | try { |
| 3620 | return mService.createProfileForUserEvenWhenDisallowedWithThrow(name, userType, flags, |
| 3621 | userId, disallowedPackages); |
| 3622 | } catch (ServiceSpecificException e) { |
| 3623 | return null; |
| 3624 | } catch (RemoteException re) { |
| 3625 | throw re.rethrowFromSystemServer(); |
| 3626 | } |
| 3627 | } |
| 3628 | |
| 3629 | /** |
| 3630 | * Creates a restricted profile with the specified name. This method also sets necessary |
| 3631 | * restrictions and adds shared accounts (with the context user). |
| 3632 | * |
| 3633 | * @param name profile's name |
| 3634 | * @return the {@link UserInfo} object for the created user, or {@code null} if the user |
| 3635 | * could not be created. |
| 3636 | * |
| 3637 | * @hide |
| 3638 | */ |
| 3639 | @TestApi |
| 3640 | @RequiresPermission(anyOf = {Manifest.permission.MANAGE_USERS, |
| 3641 | Manifest.permission.CREATE_USERS}) |
| 3642 | @UserHandleAware |
| 3643 | public @Nullable UserInfo createRestrictedProfile(@Nullable String name) { |
| 3644 | try { |
| 3645 | final int parentUserId = mUserId; |
| 3646 | final UserInfo profile = mService.createRestrictedProfileWithThrow(name, parentUserId); |
| 3647 | if (profile != null) { |
| 3648 | final UserHandle parentUserHandle = UserHandle.of(parentUserId); |
| 3649 | AccountManager.get(mContext).addSharedAccountsFromParentUser(parentUserHandle, |
| 3650 | UserHandle.of(profile.id)); |
| 3651 | } |
| 3652 | return profile; |
| 3653 | } catch (ServiceSpecificException e) { |
| 3654 | return null; |
| 3655 | } catch (RemoteException re) { |
| 3656 | throw re.rethrowFromSystemServer(); |
| 3657 | } |
| 3658 | } |
| 3659 | |
| 3660 | /** |
| 3661 | * Returns an intent to create a user for the provided name and account name. The name |
| 3662 | * and account name will be used when the setup process for the new user is started. |
| 3663 | * <p> |
| 3664 | * The intent should be launched using startActivityForResult and the return result will |
| 3665 | * indicate if the user consented to adding a new user and if the operation succeeded. Any |
| 3666 | * errors in creating the user will be returned in the result code. If the user cancels the |
| 3667 | * request, the return result will be {@link Activity#RESULT_CANCELED}. On success, the |
| 3668 | * result code will be {@link Activity#RESULT_OK}. |
| 3669 | * <p> |
| 3670 | * Use {@link #supportsMultipleUsers()} to first check if the device supports this operation |
| 3671 | * at all. |
| 3672 | * <p> |
| 3673 | * The new user is created but not initialized. After switching into the user for the first |
| 3674 | * time, the preferred user name and account information are used by the setup process for that |
| 3675 | * user. |
| 3676 | * |
| 3677 | * @param userName Optional name to assign to the user. |
| 3678 | * @param accountName Optional account name that will be used by the setup wizard to initialize |
| 3679 | * the user. |
| 3680 | * @param accountType Optional account type for the account to be created. This is required |
| 3681 | * if the account name is specified. |
| 3682 | * @param accountOptions Optional bundle of data to be passed in during account creation in the |
| 3683 | * new user via {@link AccountManager#addAccount(String, String, String[], |
| 3684 | * Bundle, android.app.Activity, android.accounts.AccountManagerCallback, |
| 3685 | * Handler)}. |
| 3686 | * @return An Intent that can be launched from an Activity. |
| 3687 | * @see #USER_CREATION_FAILED_NOT_PERMITTED |
| 3688 | * @see #USER_CREATION_FAILED_NO_MORE_USERS |
| 3689 | * @see #supportsMultipleUsers |
| 3690 | */ |
| 3691 | public static Intent createUserCreationIntent(@Nullable String userName, |
| 3692 | @Nullable String accountName, |
| 3693 | @Nullable String accountType, @Nullable PersistableBundle accountOptions) { |
| 3694 | Intent intent = new Intent(ACTION_CREATE_USER); |
| 3695 | if (userName != null) { |
| 3696 | intent.putExtra(EXTRA_USER_NAME, userName); |
| 3697 | } |
| 3698 | if (accountName != null && accountType == null) { |
| 3699 | throw new IllegalArgumentException("accountType must be specified if accountName is " |
| 3700 | + "specified"); |
| 3701 | } |
| 3702 | if (accountName != null) { |
| 3703 | intent.putExtra(EXTRA_USER_ACCOUNT_NAME, accountName); |
| 3704 | } |
| 3705 | if (accountType != null) { |
| 3706 | intent.putExtra(EXTRA_USER_ACCOUNT_TYPE, accountType); |
| 3707 | } |
| 3708 | if (accountOptions != null) { |
| 3709 | intent.putExtra(EXTRA_USER_ACCOUNT_OPTIONS, accountOptions); |
| 3710 | } |
| 3711 | return intent; |
| 3712 | } |
| 3713 | |
| 3714 | /** |
| 3715 | * Returns the list of the system packages that would be installed on this type of user upon |
| 3716 | * its creation. |
| 3717 | * |
| 3718 | * Returns {@code null} if all system packages would be installed. |
| 3719 | * |
| 3720 | * @hide |
| 3721 | */ |
| 3722 | @TestApi |
| 3723 | @SuppressLint("NullableCollection") |
| 3724 | @RequiresPermission(anyOf = { |
| 3725 | android.Manifest.permission.MANAGE_USERS, |
| 3726 | android.Manifest.permission.CREATE_USERS |
| 3727 | }) |
| 3728 | public @Nullable Set<String> getPreInstallableSystemPackages(@NonNull String userType) { |
| 3729 | try { |
| 3730 | final String[] installableSystemPackages |
| 3731 | = mService.getPreInstallableSystemPackages(userType); |
| 3732 | if (installableSystemPackages == null) { |
| 3733 | return null; |
| 3734 | } |
| 3735 | return new ArraySet<>(installableSystemPackages); |
| 3736 | } catch (RemoteException re) { |
| 3737 | throw re.rethrowFromSystemServer(); |
| 3738 | } |
| 3739 | } |
| 3740 | |
| 3741 | /** |
| 3742 | * @hide |
| 3743 | * |
| 3744 | * Returns the preferred account name for the context user's creation. |
| 3745 | */ |
| 3746 | @SystemApi |
| 3747 | @RequiresPermission(android.Manifest.permission.MANAGE_USERS) |
| 3748 | @UserHandleAware(enabledSinceTargetSdkVersion = Build.VERSION_CODES.TIRAMISU) |
| 3749 | public String getSeedAccountName() { |
| 3750 | try { |
| 3751 | return mService.getSeedAccountName(getContextUserIfAppropriate()); |
| 3752 | } catch (RemoteException re) { |
| 3753 | throw re.rethrowFromSystemServer(); |
| 3754 | } |
| 3755 | } |
| 3756 | |
| 3757 | /** |
| 3758 | * @hide |
| 3759 | * |
| 3760 | * Returns the preferred account type for the context user's creation. |
| 3761 | */ |
| 3762 | @SystemApi |
| 3763 | @RequiresPermission(android.Manifest.permission.MANAGE_USERS) |
| 3764 | @UserHandleAware(enabledSinceTargetSdkVersion = Build.VERSION_CODES.TIRAMISU) |
| 3765 | public String getSeedAccountType() { |
| 3766 | try { |
| 3767 | return mService.getSeedAccountType(getContextUserIfAppropriate()); |
| 3768 | } catch (RemoteException re) { |
| 3769 | throw re.rethrowFromSystemServer(); |
| 3770 | } |
| 3771 | } |
| 3772 | |
| 3773 | /** |
| 3774 | * @hide |
| 3775 | * |
| 3776 | * Returns the preferred account's options bundle for user creation. |
| 3777 | * @return Any options set by the requestor that created the context user. |
| 3778 | */ |
| 3779 | @SystemApi |
| 3780 | @RequiresPermission(android.Manifest.permission.MANAGE_USERS) |
| 3781 | @UserHandleAware(enabledSinceTargetSdkVersion = Build.VERSION_CODES.TIRAMISU) |
| 3782 | public PersistableBundle getSeedAccountOptions() { |
| 3783 | try { |
| 3784 | return mService.getSeedAccountOptions(getContextUserIfAppropriate()); |
| 3785 | } catch (RemoteException re) { |
| 3786 | throw re.rethrowFromSystemServer(); |
| 3787 | } |
| 3788 | } |
| 3789 | |
| 3790 | /** |
| 3791 | * @hide |
| 3792 | * |
| 3793 | * Called by a system activity to set the seed account information of a user created |
| 3794 | * through the user creation intent. |
| 3795 | * @param userId |
| 3796 | * @param accountName |
| 3797 | * @param accountType |
| 3798 | * @param accountOptions |
| 3799 | * @see #createUserCreationIntent(String, String, String, PersistableBundle) |
| 3800 | */ |
| 3801 | @RequiresPermission(android.Manifest.permission.MANAGE_USERS) |
| 3802 | public void setSeedAccountData(int userId, String accountName, String accountType, |
| 3803 | PersistableBundle accountOptions) { |
| 3804 | try { |
| 3805 | mService.setSeedAccountData(userId, accountName, accountType, accountOptions, |
| 3806 | /* persist= */ true); |
| 3807 | } catch (RemoteException re) { |
| 3808 | throw re.rethrowFromSystemServer(); |
| 3809 | } |
| 3810 | } |
| 3811 | |
| 3812 | /** |
| 3813 | * @hide |
| 3814 | * Clears the seed information used to create the context user. |
| 3815 | */ |
| 3816 | @SystemApi |
| 3817 | @RequiresPermission(android.Manifest.permission.MANAGE_USERS) |
| 3818 | @UserHandleAware(enabledSinceTargetSdkVersion = Build.VERSION_CODES.TIRAMISU) |
| 3819 | public void clearSeedAccountData() { |
| 3820 | try { |
| 3821 | mService.clearSeedAccountData(getContextUserIfAppropriate()); |
| 3822 | } catch (RemoteException re) { |
| 3823 | throw re.rethrowFromSystemServer(); |
| 3824 | } |
| 3825 | } |
| 3826 | |
| 3827 | /** |
| 3828 | * @hide |
| 3829 | * Marks the guest user for deletion to allow a new guest to be created before deleting |
| 3830 | * the current user who is a guest. |
| 3831 | * @param userId |
| 3832 | * @return |
| 3833 | */ |
| 3834 | @RequiresPermission(android.Manifest.permission.MANAGE_USERS) |
| 3835 | public boolean markGuestForDeletion(@UserIdInt int userId) { |
| 3836 | try { |
| 3837 | return mService.markGuestForDeletion(userId); |
| 3838 | } catch (RemoteException re) { |
| 3839 | throw re.rethrowFromSystemServer(); |
| 3840 | } |
| 3841 | } |
| 3842 | |
| 3843 | /** |
| 3844 | * Sets the user as enabled, if such an user exists. |
| 3845 | * |
| 3846 | * <p>Note that the default is true, it's only that managed profiles might not be enabled. |
| 3847 | * Also ephemeral users can be disabled to indicate that their removal is in progress and they |
| 3848 | * shouldn't be re-entered. Therefore ephemeral users should not be re-enabled once disabled. |
| 3849 | * |
| 3850 | * @param userId the id of the profile to enable |
| 3851 | * @hide |
| 3852 | */ |
| 3853 | @RequiresPermission(android.Manifest.permission.MANAGE_USERS) |
| 3854 | public void setUserEnabled(@UserIdInt int userId) { |
| 3855 | try { |
| 3856 | mService.setUserEnabled(userId); |
| 3857 | } catch (RemoteException re) { |
| 3858 | throw re.rethrowFromSystemServer(); |
| 3859 | } |
| 3860 | } |
| 3861 | |
| 3862 | /** |
| 3863 | * Assigns admin privileges to the user, if such a user exists. |
| 3864 | * |
| 3865 | * <p>Note that this does not alter the user's pre-existing user restrictions. |
| 3866 | * |
| 3867 | * @param userId the id of the user to become admin |
| 3868 | * @hide |
| 3869 | */ |
| 3870 | @RequiresPermission(allOf = { |
| 3871 | Manifest.permission.INTERACT_ACROSS_USERS_FULL, |
| 3872 | Manifest.permission.MANAGE_USERS |
| 3873 | }) |
| 3874 | public void setUserAdmin(@UserIdInt int userId) { |
| 3875 | try { |
| 3876 | mService.setUserAdmin(userId); |
| 3877 | } catch (RemoteException re) { |
| 3878 | throw re.rethrowFromSystemServer(); |
| 3879 | } |
| 3880 | } |
| 3881 | |
| 3882 | /** |
| 3883 | * Evicts the user's credential encryption key from memory by stopping and restarting the user. |
| 3884 | * |
| 3885 | * @hide |
| 3886 | */ |
| 3887 | @RequiresPermission(android.Manifest.permission.MANAGE_USERS) |
| 3888 | public void evictCredentialEncryptionKey(@UserIdInt int userId) { |
| 3889 | try { |
| 3890 | mService.evictCredentialEncryptionKey(userId); |
| 3891 | } catch (RemoteException re) { |
| 3892 | throw re.rethrowFromSystemServer(); |
| 3893 | } |
| 3894 | } |
| 3895 | |
| 3896 | /** |
| 3897 | * Return the number of users currently created on the device. |
| 3898 | */ |
| 3899 | @RequiresPermission(anyOf = { |
| 3900 | android.Manifest.permission.MANAGE_USERS, |
| 3901 | android.Manifest.permission.CREATE_USERS |
| 3902 | }) |
| 3903 | public int getUserCount() { |
| 3904 | List<UserInfo> users = getUsers(); |
| 3905 | return users != null ? users.size() : 1; |
| 3906 | } |
| 3907 | |
| 3908 | /** |
| 3909 | * Returns information for all fully-created users on this device, including ones marked for |
| 3910 | * deletion. |
| 3911 | * |
| 3912 | * <p>To retrieve only users that are not marked for deletion, use {@link #getAliveUsers()}. |
| 3913 | * |
| 3914 | * <p>To retrieve *all* users (including partial and pre-created users), use |
| 3915 | * {@link #getUsers(boolean, boolean, boolean)) getUsers(false, false, false)}. |
| 3916 | * |
| 3917 | * <p>To retrieve a more specific list of users, use |
| 3918 | * {@link #getUsers(boolean, boolean, boolean)}. |
| 3919 | * |
| 3920 | * @return the list of users that were created. |
| 3921 | * |
| 3922 | * @hide |
| 3923 | */ |
| 3924 | @UnsupportedAppUsage |
| 3925 | @RequiresPermission(anyOf = { |
| 3926 | android.Manifest.permission.MANAGE_USERS, |
| 3927 | android.Manifest.permission.CREATE_USERS |
| 3928 | }) |
| 3929 | public List<UserInfo> getUsers() { |
| 3930 | return getUsers(/*excludePartial= */ true, /* excludeDying= */ false, |
| 3931 | /* excludePreCreated= */ true); |
| 3932 | } |
| 3933 | |
| 3934 | /** |
| 3935 | * Returns information for all "usable" users on this device (i.e, it excludes users that are |
| 3936 | * marked for deletion, pre-created users, etc...). |
| 3937 | * |
| 3938 | * <p>To retrieve all fully-created users, use {@link #getUsers()}. |
| 3939 | * |
| 3940 | * <p>To retrieve a more specific list of users, use |
| 3941 | * {@link #getUsers(boolean, boolean, boolean)}. |
| 3942 | * |
| 3943 | * @return the list of users that were created. |
| 3944 | * @hide |
| 3945 | */ |
| 3946 | @RequiresPermission(anyOf = { |
| 3947 | android.Manifest.permission.MANAGE_USERS, |
| 3948 | android.Manifest.permission.CREATE_USERS |
| 3949 | }) |
| 3950 | public @NonNull List<UserInfo> getAliveUsers() { |
| 3951 | return getUsers(/*excludePartial= */ true, /* excludeDying= */ true, |
| 3952 | /* excludePreCreated= */ true); |
| 3953 | } |
| 3954 | |
| 3955 | /** |
| 3956 | * @deprecated use {@link #getAliveUsers()} for {@code getUsers(true)}, or |
| 3957 | * {@link #getUsers()} for @code getUsers(false)}. |
| 3958 | * |
| 3959 | * @hide |
| 3960 | */ |
| 3961 | @Deprecated |
| 3962 | @UnsupportedAppUsage |
| 3963 | @RequiresPermission(anyOf = { |
| 3964 | android.Manifest.permission.MANAGE_USERS, |
| 3965 | android.Manifest.permission.CREATE_USERS |
| 3966 | }) |
| 3967 | public @NonNull List<UserInfo> getUsers(boolean excludeDying) { |
| 3968 | return getUsers(/*excludePartial= */ true, excludeDying, |
| 3969 | /* excludePreCreated= */ true); |
| 3970 | } |
| 3971 | |
| 3972 | /** |
| 3973 | * Returns information for all users on this device, based on the filtering parameters. |
| 3974 | * |
| 3975 | * @hide |
| 3976 | */ |
| 3977 | @TestApi |
| 3978 | @RequiresPermission(anyOf = { |
| 3979 | android.Manifest.permission.MANAGE_USERS, |
| 3980 | android.Manifest.permission.CREATE_USERS |
| 3981 | }) |
| 3982 | public @NonNull List<UserInfo> getUsers(boolean excludePartial, boolean excludeDying, |
| 3983 | boolean excludePreCreated) { |
| 3984 | try { |
| 3985 | return mService.getUsers(excludePartial, excludeDying, excludePreCreated); |
| 3986 | } catch (RemoteException re) { |
| 3987 | throw re.rethrowFromSystemServer(); |
| 3988 | } |
| 3989 | } |
| 3990 | |
| 3991 | /** |
| 3992 | * Returns the user handles for all users on this device, based on the filtering parameters. |
| 3993 | * |
| 3994 | * @param excludeDying specify if the list should exclude users being removed. |
| 3995 | * @return the list of user handles. |
| 3996 | * @hide |
| 3997 | */ |
| 3998 | @SystemApi |
| 3999 | @RequiresPermission(anyOf = { |
| 4000 | android.Manifest.permission.MANAGE_USERS, |
| 4001 | android.Manifest.permission.CREATE_USERS |
| 4002 | }) |
| 4003 | public @NonNull List<UserHandle> getUserHandles(boolean excludeDying) { |
| 4004 | List<UserInfo> users = getUsers(/* excludePartial= */ true, excludeDying, |
| 4005 | /* excludePreCreated= */ true); |
| 4006 | List<UserHandle> result = new ArrayList<>(users.size()); |
| 4007 | for (UserInfo user : users) { |
| 4008 | result.add(user.getUserHandle()); |
| 4009 | } |
| 4010 | return result; |
| 4011 | } |
| 4012 | |
| 4013 | /** |
| 4014 | * Returns serial numbers of all users on this device. |
| 4015 | * |
| 4016 | * @param excludeDying specify if the list should exclude users being removed. |
| 4017 | * @return the list of serial numbers of users that exist on the device. |
| 4018 | * @hide |
| 4019 | */ |
| 4020 | @SystemApi |
| 4021 | @RequiresPermission(anyOf = { |
| 4022 | android.Manifest.permission.MANAGE_USERS, |
| 4023 | android.Manifest.permission.CREATE_USERS |
| 4024 | }) |
| 4025 | public long[] getSerialNumbersOfUsers(boolean excludeDying) { |
| 4026 | List<UserInfo> users = getUsers(/* excludePartial= */ true, excludeDying, |
| 4027 | /* excludePreCreated= */ true); |
| 4028 | long[] result = new long[users.size()]; |
| 4029 | for (int i = 0; i < result.length; i++) { |
| 4030 | result[i] = users.get(i).serialNumber; |
| 4031 | } |
| 4032 | return result; |
| 4033 | } |
| 4034 | |
| 4035 | /** |
| 4036 | * @return the user's account name, null if not found. |
| 4037 | * @hide |
| 4038 | */ |
| 4039 | @RequiresPermission( allOf = { |
| 4040 | Manifest.permission.INTERACT_ACROSS_USERS_FULL, |
| 4041 | Manifest.permission.MANAGE_USERS |
| 4042 | }) |
| 4043 | public @Nullable String getUserAccount(@UserIdInt int userId) { |
| 4044 | try { |
| 4045 | return mService.getUserAccount(userId); |
| 4046 | } catch (RemoteException re) { |
| 4047 | throw re.rethrowFromSystemServer(); |
| 4048 | } |
| 4049 | } |
| 4050 | |
| 4051 | /** |
| 4052 | * Set account name for the given user. |
| 4053 | * @hide |
| 4054 | */ |
| 4055 | @RequiresPermission( allOf = { |
| 4056 | Manifest.permission.INTERACT_ACROSS_USERS_FULL, |
| 4057 | Manifest.permission.MANAGE_USERS |
| 4058 | }) |
| 4059 | public void setUserAccount(@UserIdInt int userId, @Nullable String accountName) { |
| 4060 | try { |
| 4061 | mService.setUserAccount(userId, accountName); |
| 4062 | } catch (RemoteException re) { |
| 4063 | throw re.rethrowFromSystemServer(); |
| 4064 | } |
| 4065 | } |
| 4066 | |
| 4067 | /** |
| 4068 | * Returns information for Primary user. |
| 4069 | * |
| 4070 | * @return the Primary user, null if not found. |
| 4071 | * @hide |
| 4072 | */ |
| 4073 | @RequiresPermission(android.Manifest.permission.MANAGE_USERS) |
| 4074 | public @Nullable UserInfo getPrimaryUser() { |
| 4075 | try { |
| 4076 | return mService.getPrimaryUser(); |
| 4077 | } catch (RemoteException re) { |
| 4078 | throw re.rethrowFromSystemServer(); |
| 4079 | } |
| 4080 | } |
| 4081 | |
| 4082 | /** |
| 4083 | * Checks whether it's possible to add more users. |
| 4084 | * |
| 4085 | * @return true if more users can be added, false if limit has been reached. |
| 4086 | * |
| 4087 | * @deprecated use {@link #canAddMoreUsers(String)} instead. |
| 4088 | * |
| 4089 | * @hide |
| 4090 | */ |
| 4091 | @Deprecated |
| 4092 | @RequiresPermission(anyOf = { |
| 4093 | android.Manifest.permission.MANAGE_USERS, |
| 4094 | android.Manifest.permission.CREATE_USERS |
| 4095 | }) |
| 4096 | public boolean canAddMoreUsers() { |
| 4097 | // TODO(b/142482943): UMS has different logic, excluding Demo and Profile from counting. Why |
| 4098 | // not here? The logic is inconsistent. See UMS.canAddMoreManagedProfiles |
| 4099 | final List<UserInfo> users = getAliveUsers(); |
| 4100 | final int totalUserCount = users.size(); |
| 4101 | int aliveUserCount = 0; |
| 4102 | for (int i = 0; i < totalUserCount; i++) { |
| 4103 | UserInfo user = users.get(i); |
| 4104 | if (!user.isGuest()) { |
| 4105 | aliveUserCount++; |
| 4106 | } |
| 4107 | } |
| 4108 | return aliveUserCount < getMaxSupportedUsers(); |
| 4109 | } |
| 4110 | |
| 4111 | /** |
| 4112 | * Checks whether it is possible to add more users of the given user type. |
| 4113 | * |
| 4114 | * @param userType the type of user, such as {@link UserManager#USER_TYPE_FULL_SECONDARY}. |
| 4115 | * @return true if more users of the given type can be added, otherwise false. |
| 4116 | * @hide |
| 4117 | */ |
| 4118 | @RequiresPermission(anyOf = { |
| 4119 | android.Manifest.permission.MANAGE_USERS, |
| 4120 | android.Manifest.permission.CREATE_USERS |
| 4121 | }) |
| 4122 | public boolean canAddMoreUsers(@NonNull String userType) { |
| 4123 | try { |
| 4124 | return canAddMoreUsers() && mService.canAddMoreUsersOfType(userType); |
| 4125 | } catch (RemoteException re) { |
| 4126 | throw re.rethrowFromSystemServer(); |
| 4127 | } |
| 4128 | } |
| 4129 | |
| 4130 | /** |
| 4131 | * Returns the remaining number of users of the given type that can be created. |
| 4132 | * |
| 4133 | * @param userType the type of user, such as {@link UserManager#USER_TYPE_FULL_SECONDARY}. |
| 4134 | * @return how many additional users can be created. |
| 4135 | * @hide |
| 4136 | */ |
| 4137 | @SystemApi |
| 4138 | @RequiresPermission(anyOf = { |
| 4139 | android.Manifest.permission.MANAGE_USERS, |
| 4140 | android.Manifest.permission.CREATE_USERS, |
| 4141 | android.Manifest.permission.QUERY_USERS |
| 4142 | }) |
| 4143 | public int getRemainingCreatableUserCount(@NonNull String userType) { |
| 4144 | Objects.requireNonNull(userType, "userType must not be null"); |
| 4145 | try { |
| 4146 | return mService.getRemainingCreatableUserCount(userType); |
| 4147 | } catch (RemoteException re) { |
| 4148 | throw re.rethrowFromSystemServer(); |
| 4149 | } |
| 4150 | } |
| 4151 | |
| 4152 | /** |
| 4153 | * Returns the remaining number of profiles that can be added to the context user. |
| 4154 | * <p>Note that is applicable to any profile type (currently not including Restricted profiles). |
| 4155 | * |
| 4156 | * @param userType the type of profile, such as {@link UserManager#USER_TYPE_PROFILE_MANAGED}. |
| 4157 | * @return how many additional profiles can be created. |
| 4158 | * @hide |
| 4159 | */ |
| 4160 | @SystemApi |
| 4161 | @RequiresPermission(anyOf = { |
| 4162 | android.Manifest.permission.MANAGE_USERS, |
| 4163 | android.Manifest.permission.CREATE_USERS, |
| 4164 | android.Manifest.permission.QUERY_USERS |
| 4165 | }) |
| 4166 | @UserHandleAware |
| 4167 | public int getRemainingCreatableProfileCount(@NonNull String userType) { |
| 4168 | Objects.requireNonNull(userType, "userType must not be null"); |
| 4169 | try { |
| 4170 | // TODO(b/142482943): Perhaps let the following code apply to restricted users too. |
| 4171 | return mService.getRemainingCreatableProfileCount(userType, mUserId); |
| 4172 | } catch (RemoteException re) { |
| 4173 | throw re.rethrowFromSystemServer(); |
| 4174 | } |
| 4175 | } |
| 4176 | |
| 4177 | /** |
| 4178 | * Checks whether it's possible to add more managed profiles. |
| 4179 | * if allowedToRemoveOne is true and if the user already has a managed profile, then return if |
| 4180 | * we could add a new managed profile to this user after removing the existing one. |
| 4181 | * |
| 4182 | * @return true if more managed profiles can be added, false if limit has been reached. |
| 4183 | * @hide |
| 4184 | */ |
| 4185 | @RequiresPermission(anyOf = { |
| 4186 | android.Manifest.permission.MANAGE_USERS, |
| 4187 | android.Manifest.permission.CREATE_USERS, |
| 4188 | android.Manifest.permission.QUERY_USERS |
| 4189 | }) |
| 4190 | public boolean canAddMoreManagedProfiles(@UserIdInt int userId, boolean allowedToRemoveOne) { |
| 4191 | try { |
| 4192 | return mService.canAddMoreManagedProfiles(userId, allowedToRemoveOne); |
| 4193 | } catch (RemoteException re) { |
| 4194 | throw re.rethrowFromSystemServer(); |
| 4195 | } |
| 4196 | } |
| 4197 | |
| 4198 | /** |
| 4199 | * Checks whether it's possible to add more profiles of the given type to the given user. |
| 4200 | * |
| 4201 | * @param userType the type of user, such as {@link UserManager#USER_TYPE_PROFILE_MANAGED}. |
| 4202 | * @return true if more profiles can be added, false if limit has been reached. |
| 4203 | * @hide |
| 4204 | */ |
| 4205 | @RequiresPermission(anyOf = { |
| 4206 | android.Manifest.permission.MANAGE_USERS, |
| 4207 | android.Manifest.permission.CREATE_USERS, |
| 4208 | android.Manifest.permission.QUERY_USERS |
| 4209 | }) |
| 4210 | public boolean canAddMoreProfilesToUser(@NonNull String userType, @UserIdInt int userId) { |
| 4211 | try { |
| 4212 | return mService.canAddMoreProfilesToUser(userType, userId, false); |
| 4213 | } catch (RemoteException re) { |
| 4214 | throw re.rethrowFromSystemServer(); |
| 4215 | } |
| 4216 | } |
| 4217 | |
| 4218 | /** |
| 4219 | * Checks whether this device supports users of the given user type. |
| 4220 | * |
| 4221 | * @param userType the type of user, such as {@link UserManager#USER_TYPE_FULL_SECONDARY}. |
| 4222 | * @return true if the creation of users of the given user type is enabled on this device. |
| 4223 | * @hide |
| 4224 | */ |
| 4225 | @RequiresPermission(anyOf = { |
| 4226 | android.Manifest.permission.MANAGE_USERS, |
| 4227 | android.Manifest.permission.CREATE_USERS |
| 4228 | }) |
| 4229 | public boolean isUserTypeEnabled(@NonNull String userType) { |
| 4230 | try { |
| 4231 | return mService.isUserTypeEnabled(userType); |
| 4232 | } catch (RemoteException re) { |
| 4233 | throw re.rethrowFromSystemServer(); |
| 4234 | } |
| 4235 | } |
| 4236 | |
| 4237 | /** |
| 4238 | * Returns list of the profiles of userId including userId itself. |
| 4239 | * Note that this returns both enabled and not enabled profiles. See |
| 4240 | * {@link #getEnabledProfiles(int)} if you need only the enabled ones. |
| 4241 | * <p>Note that this includes all profile types (not including Restricted profiles). |
| 4242 | * |
| 4243 | * <p>Requires {@link android.Manifest.permission#MANAGE_USERS} or |
| 4244 | * {@link android.Manifest.permission#CREATE_USERS} or |
| 4245 | * {@link android.Manifest.permission#QUERY_USERS} if userId is not the calling user. |
| 4246 | * @param userId profiles of this user will be returned. |
| 4247 | * @return the list of profiles. |
| 4248 | * @hide |
| 4249 | */ |
| 4250 | @UnsupportedAppUsage |
| 4251 | @RequiresPermission(anyOf = { |
| 4252 | Manifest.permission.MANAGE_USERS, |
| 4253 | Manifest.permission.CREATE_USERS, |
| 4254 | Manifest.permission.QUERY_USERS}, conditional = true) |
| 4255 | public List<UserInfo> getProfiles(@UserIdInt int userId) { |
| 4256 | try { |
| 4257 | return mService.getProfiles(userId, false /* enabledOnly */); |
| 4258 | } catch (RemoteException re) { |
| 4259 | throw re.rethrowFromSystemServer(); |
| 4260 | } |
| 4261 | } |
| 4262 | |
| 4263 | /** |
| 4264 | * Checks if the 2 provided user handles belong to the same profile group. |
| 4265 | * |
| 4266 | * @param user one of the two user handles to check. |
| 4267 | * @param otherUser one of the two user handles to check. |
| 4268 | * @return true if the two users are in the same profile group. |
| 4269 | * |
| 4270 | * @hide |
| 4271 | */ |
| 4272 | @SystemApi |
| 4273 | @RequiresPermission(anyOf = { |
| 4274 | android.Manifest.permission.MANAGE_USERS, |
| 4275 | android.Manifest.permission.QUERY_USERS}) |
| 4276 | public boolean isSameProfileGroup(@NonNull UserHandle user, @NonNull UserHandle otherUser) { |
| 4277 | return isSameProfileGroup(user.getIdentifier(), otherUser.getIdentifier()); |
| 4278 | } |
| 4279 | |
| 4280 | /** |
| 4281 | * Checks if the 2 provided user ids belong to the same profile group. |
| 4282 | * @param userId one of the two user ids to check. |
| 4283 | * @param otherUserId one of the two user ids to check. |
| 4284 | * @return true if the two user ids are in the same profile group. |
| 4285 | * @hide |
| 4286 | */ |
| 4287 | @RequiresPermission(anyOf = { |
| 4288 | android.Manifest.permission.MANAGE_USERS, |
| 4289 | android.Manifest.permission.QUERY_USERS}) |
| 4290 | public boolean isSameProfileGroup(@UserIdInt int userId, int otherUserId) { |
| 4291 | try { |
| 4292 | return mService.isSameProfileGroup(userId, otherUserId); |
| 4293 | } catch (RemoteException re) { |
| 4294 | throw re.rethrowFromSystemServer(); |
| 4295 | } |
| 4296 | } |
| 4297 | |
| 4298 | /** |
| 4299 | * Returns list of the profiles of userId including userId itself. |
| 4300 | * Note that this returns only enabled. |
| 4301 | * <p>Note that this includes all profile types (not including Restricted profiles). |
| 4302 | * |
| 4303 | * <p>Requires {@link android.Manifest.permission#MANAGE_USERS} or |
| 4304 | * {@link android.Manifest.permission#CREATE_USERS} or |
| 4305 | * {@link android.Manifest.permission#QUERY_USERS} if userId is not the calling user. |
| 4306 | * @param userId profiles of this user will be returned. |
| 4307 | * @return the list of profiles. |
| 4308 | * @deprecated use {@link #getUserProfiles()} instead. |
| 4309 | * |
| 4310 | * @hide |
| 4311 | */ |
| 4312 | @Deprecated |
| 4313 | @UnsupportedAppUsage |
| 4314 | @RequiresPermission(anyOf = { |
| 4315 | Manifest.permission.MANAGE_USERS, |
| 4316 | Manifest.permission.CREATE_USERS, |
| 4317 | Manifest.permission.QUERY_USERS}, conditional = true) |
| 4318 | public List<UserInfo> getEnabledProfiles(@UserIdInt int userId) { |
| 4319 | try { |
| 4320 | return mService.getProfiles(userId, true /* enabledOnly */); |
| 4321 | } catch (RemoteException re) { |
| 4322 | throw re.rethrowFromSystemServer(); |
| 4323 | } |
| 4324 | } |
| 4325 | |
| 4326 | /** |
| 4327 | * Returns a list of UserHandles for profiles associated with the context user, including the |
| 4328 | * user itself. |
| 4329 | * <p>Note that this includes all profile types (not including Restricted profiles). |
| 4330 | * |
| 4331 | * @return A non-empty list of UserHandles associated with the context user. |
| 4332 | */ |
| 4333 | @UserHandleAware( |
| 4334 | enabledSinceTargetSdkVersion = Build.VERSION_CODES.TIRAMISU, |
| 4335 | requiresAnyOfPermissionsIfNotCaller = { |
| 4336 | Manifest.permission.MANAGE_USERS, |
| 4337 | Manifest.permission.CREATE_USERS, |
| 4338 | Manifest.permission.QUERY_USERS}) |
| 4339 | public List<UserHandle> getUserProfiles() { |
| 4340 | int[] userIds = getProfileIds(getContextUserIfAppropriate(), true /* enabledOnly */); |
| 4341 | return convertUserIdsToUserHandles(userIds); |
| 4342 | } |
| 4343 | |
| 4344 | /** |
| 4345 | * Returns a list of ids for enabled profiles associated with the context user including the |
| 4346 | * user itself. |
| 4347 | * <p>Note that this includes all profile types (not including Restricted profiles). |
| 4348 | * |
| 4349 | * @return A non-empty list of UserHandles associated with the context user. |
| 4350 | * @hide |
| 4351 | */ |
| 4352 | @SystemApi |
| 4353 | @UserHandleAware( |
| 4354 | requiresAnyOfPermissionsIfNotCaller = { |
| 4355 | Manifest.permission.MANAGE_USERS, |
| 4356 | Manifest.permission.CREATE_USERS, |
| 4357 | Manifest.permission.QUERY_USERS}) |
| 4358 | public @NonNull List<UserHandle> getEnabledProfiles() { |
| 4359 | return getProfiles(true); |
| 4360 | } |
| 4361 | |
| 4362 | /** |
| 4363 | * Returns a list of ids for all profiles associated with the context user including the user |
| 4364 | * itself. |
| 4365 | * <p>Note that this includes all profile types (not including Restricted profiles). |
| 4366 | * |
| 4367 | * @return A non-empty list of UserHandles associated with the context user. |
| 4368 | * @hide |
| 4369 | */ |
| 4370 | @SystemApi |
| 4371 | @UserHandleAware( |
| 4372 | requiresAnyOfPermissionsIfNotCaller = { |
| 4373 | Manifest.permission.MANAGE_USERS, |
| 4374 | Manifest.permission.CREATE_USERS, |
| 4375 | Manifest.permission.QUERY_USERS}) |
| 4376 | public @NonNull List<UserHandle> getAllProfiles() { |
| 4377 | return getProfiles(false); |
| 4378 | } |
| 4379 | |
| 4380 | /** |
| 4381 | * Returns a list of ids for profiles associated with the context user including the user |
| 4382 | * itself. |
| 4383 | * <p>Note that this includes all profile types (not including Restricted profiles). |
| 4384 | * |
| 4385 | * @param enabledOnly whether to return only {@link UserInfo#isEnabled() enabled} profiles |
| 4386 | * @return A non-empty list of UserHandles associated with the context user. |
| 4387 | */ |
| 4388 | @UserHandleAware( |
| 4389 | requiresAnyOfPermissionsIfNotCaller = { |
| 4390 | Manifest.permission.MANAGE_USERS, |
| 4391 | Manifest.permission.CREATE_USERS, |
| 4392 | Manifest.permission.QUERY_USERS}) |
| 4393 | private @NonNull List<UserHandle> getProfiles(boolean enabledOnly) { |
| 4394 | final int[] userIds = getProfileIds(mUserId, enabledOnly); |
| 4395 | return convertUserIdsToUserHandles(userIds); |
| 4396 | } |
| 4397 | |
| 4398 | /** Converts the given array of userIds to a List of UserHandles. */ |
| 4399 | private @NonNull List<UserHandle> convertUserIdsToUserHandles(@NonNull int[] userIds) { |
| 4400 | final List<UserHandle> result = new ArrayList<>(userIds.length); |
| 4401 | for (int userId : userIds) { |
| 4402 | result.add(UserHandle.of(userId)); |
| 4403 | } |
| 4404 | return result; |
| 4405 | } |
| 4406 | |
| 4407 | /** |
| 4408 | * Returns a list of ids for profiles associated with the specified user including the user |
| 4409 | * itself. |
| 4410 | * <p>Note that this includes all profile types (not including Restricted profiles). |
| 4411 | * |
| 4412 | * @param userId id of the user to return profiles for |
| 4413 | * @param enabledOnly whether return only {@link UserInfo#isEnabled() enabled} profiles |
| 4414 | * @return A non-empty list of ids of profiles associated with the specified user. |
| 4415 | * |
| 4416 | * @hide |
| 4417 | */ |
| 4418 | @RequiresPermission(anyOf = { |
| 4419 | Manifest.permission.MANAGE_USERS, |
| 4420 | Manifest.permission.CREATE_USERS, |
| 4421 | Manifest.permission.QUERY_USERS}, conditional = true) |
| 4422 | public @NonNull int[] getProfileIds(@UserIdInt int userId, boolean enabledOnly) { |
| 4423 | try { |
| 4424 | return mService.getProfileIds(userId, enabledOnly); |
| 4425 | } catch (RemoteException re) { |
| 4426 | throw re.rethrowFromSystemServer(); |
| 4427 | } |
| 4428 | } |
| 4429 | |
| 4430 | /** |
| 4431 | * @see #getProfileIds(int, boolean) |
| 4432 | * @hide |
| 4433 | */ |
| 4434 | @UnsupportedAppUsage |
| 4435 | @RequiresPermission(anyOf = { |
| 4436 | Manifest.permission.MANAGE_USERS, |
| 4437 | Manifest.permission.CREATE_USERS, |
| 4438 | Manifest.permission.QUERY_USERS}, conditional = true) |
| 4439 | public int[] getProfileIdsWithDisabled(@UserIdInt int userId) { |
| 4440 | return getProfileIds(userId, false /* enabledOnly */); |
| 4441 | } |
| 4442 | |
| 4443 | /** |
| 4444 | * @see #getProfileIds(int, boolean) |
| 4445 | * @hide |
| 4446 | */ |
| 4447 | @RequiresPermission(anyOf = { |
| 4448 | Manifest.permission.MANAGE_USERS, |
| 4449 | Manifest.permission.CREATE_USERS, |
| 4450 | Manifest.permission.QUERY_USERS}, conditional = true) |
| 4451 | public int[] getEnabledProfileIds(@UserIdInt int userId) { |
| 4452 | return getProfileIds(userId, true /* enabledOnly */); |
| 4453 | } |
| 4454 | |
| 4455 | /** |
| 4456 | * Returns the device credential owner id of the profile from |
| 4457 | * which this method is called, or userId if called from a user that |
| 4458 | * is not a profile. |
| 4459 | * |
| 4460 | * @hide |
| 4461 | */ |
| 4462 | @RequiresPermission(Manifest.permission.MANAGE_USERS) |
| 4463 | public int getCredentialOwnerProfile(@UserIdInt int userId) { |
| 4464 | try { |
| 4465 | return mService.getCredentialOwnerProfile(userId); |
| 4466 | } catch (RemoteException re) { |
| 4467 | throw re.rethrowFromSystemServer(); |
| 4468 | } |
| 4469 | } |
| 4470 | |
| 4471 | /** |
| 4472 | * Returns the parent of the profile which this method is called from |
| 4473 | * or null if called from a user that is not a profile. |
| 4474 | * |
| 4475 | * @hide |
| 4476 | */ |
| 4477 | @UnsupportedAppUsage |
| 4478 | @RequiresPermission(anyOf = { |
| 4479 | android.Manifest.permission.MANAGE_USERS, |
| 4480 | android.Manifest.permission.INTERACT_ACROSS_USERS |
| 4481 | }) |
| 4482 | public UserInfo getProfileParent(@UserIdInt int userId) { |
| 4483 | try { |
| 4484 | return mService.getProfileParent(userId); |
| 4485 | } catch (RemoteException re) { |
| 4486 | throw re.rethrowFromSystemServer(); |
| 4487 | } |
| 4488 | } |
| 4489 | |
| 4490 | /** |
| 4491 | * Get the parent of a user profile. |
| 4492 | * |
| 4493 | * @param user the handle of the user profile |
| 4494 | * |
| 4495 | * @return the parent of the user or {@code null} if the user is not profile |
| 4496 | * |
| 4497 | * @hide |
| 4498 | */ |
| 4499 | @SystemApi |
| 4500 | @RequiresPermission(anyOf = { |
| 4501 | android.Manifest.permission.MANAGE_USERS, |
| 4502 | android.Manifest.permission.INTERACT_ACROSS_USERS |
| 4503 | }) |
| 4504 | public @Nullable UserHandle getProfileParent(@NonNull UserHandle user) { |
| 4505 | UserInfo info = getProfileParent(user.getIdentifier()); |
| 4506 | |
| 4507 | if (info == null) { |
| 4508 | return null; |
| 4509 | } |
| 4510 | |
| 4511 | return UserHandle.of(info.id); |
| 4512 | } |
| 4513 | |
| 4514 | /** |
| 4515 | * Enables or disables quiet mode for a managed profile. If quiet mode is enabled, apps in a |
| 4516 | * managed profile don't run, generate notifications, or consume data or battery. |
| 4517 | * <p> |
| 4518 | * If a user's credential is needed to turn off quiet mode, a confirm credential screen will be |
| 4519 | * shown to the user. |
| 4520 | * <p> |
| 4521 | * The change may not happen instantly, however apps can listen for |
| 4522 | * {@link Intent#ACTION_MANAGED_PROFILE_AVAILABLE} and |
| 4523 | * {@link Intent#ACTION_MANAGED_PROFILE_UNAVAILABLE} broadcasts in order to be notified of |
| 4524 | * the change of the quiet mode. Apps can also check the current state of quiet mode by |
| 4525 | * calling {@link #isQuietModeEnabled(UserHandle)}. |
| 4526 | * <p> |
| 4527 | * The caller must either be the foreground default launcher or have one of these permissions: |
| 4528 | * {@code MANAGE_USERS} or {@code MODIFY_QUIET_MODE}. |
| 4529 | * |
| 4530 | * @param enableQuietMode whether quiet mode should be enabled or disabled |
| 4531 | * @param userHandle user handle of the profile |
| 4532 | * @return {@code false} if user's credential is needed in order to turn off quiet mode, |
| 4533 | * {@code true} otherwise |
| 4534 | * @throws SecurityException if the caller is invalid |
| 4535 | * @throws IllegalArgumentException if {@code userHandle} is not a managed profile |
| 4536 | * |
| 4537 | * @see #isQuietModeEnabled(UserHandle) |
| 4538 | */ |
| 4539 | @RequiresPermission(anyOf = {android.Manifest.permission.MANAGE_USERS, |
| 4540 | Manifest.permission.MODIFY_QUIET_MODE}, conditional = true) |
| 4541 | public boolean requestQuietModeEnabled(boolean enableQuietMode, @NonNull UserHandle userHandle) { |
| 4542 | return requestQuietModeEnabled(enableQuietMode, userHandle, null); |
| 4543 | } |
| 4544 | |
| 4545 | /** |
| 4546 | * Perform the same operation as {@link #requestQuietModeEnabled(boolean, UserHandle)}, but |
| 4547 | * with a flag to tweak the behavior of the request. |
| 4548 | * |
| 4549 | * @param enableQuietMode whether quiet mode should be enabled or disabled |
| 4550 | * @param userHandle user handle of the profile |
| 4551 | * @param flags Can be 0 or {@link #QUIET_MODE_DISABLE_ONLY_IF_CREDENTIAL_NOT_REQUIRED}. |
| 4552 | * @return {@code false} if user's credential is needed in order to turn off quiet mode, |
| 4553 | * {@code true} otherwise |
| 4554 | * @throws SecurityException if the caller is invalid |
| 4555 | * @throws IllegalArgumentException if {@code userHandle} is not a managed profile |
| 4556 | * |
| 4557 | * @see #isQuietModeEnabled(UserHandle) |
| 4558 | */ |
| 4559 | public boolean requestQuietModeEnabled(boolean enableQuietMode, @NonNull UserHandle userHandle, |
| 4560 | @QuietModeFlag int flags) { |
| 4561 | return requestQuietModeEnabled(enableQuietMode, userHandle, null, flags); |
| 4562 | } |
| 4563 | |
| 4564 | /** |
| 4565 | * Similar to {@link #requestQuietModeEnabled(boolean, UserHandle)}, except you can specify |
| 4566 | * a target to start when user is unlocked. If {@code target} is specified, caller must have |
| 4567 | * the {@link android.Manifest.permission#MANAGE_USERS} permission. |
| 4568 | * |
| 4569 | * @see {@link #requestQuietModeEnabled(boolean, UserHandle)} |
| 4570 | * @hide |
| 4571 | */ |
| 4572 | @RequiresPermission(android.Manifest.permission.MANAGE_USERS) |
| 4573 | public boolean requestQuietModeEnabled( |
| 4574 | boolean enableQuietMode, @NonNull UserHandle userHandle, IntentSender target) { |
| 4575 | return requestQuietModeEnabled(enableQuietMode, userHandle, target, 0); |
| 4576 | } |
| 4577 | |
| 4578 | /** |
| 4579 | * Similar to {@link #requestQuietModeEnabled(boolean, UserHandle)}, except you can specify |
| 4580 | * a target to start when user is unlocked. If {@code target} is specified, caller must have |
| 4581 | * the {@link android.Manifest.permission#MANAGE_USERS} permission. |
| 4582 | * |
| 4583 | * @see #requestQuietModeEnabled(boolean, UserHandle) |
| 4584 | * @hide |
| 4585 | */ |
| 4586 | public boolean requestQuietModeEnabled( |
| 4587 | boolean enableQuietMode, @NonNull UserHandle userHandle, IntentSender target, |
| 4588 | int flags) { |
| 4589 | try { |
| 4590 | return mService.requestQuietModeEnabled( |
| 4591 | mContext.getPackageName(), enableQuietMode, userHandle.getIdentifier(), target, |
| 4592 | flags); |
| 4593 | } catch (RemoteException re) { |
| 4594 | throw re.rethrowFromSystemServer(); |
| 4595 | } |
| 4596 | } |
| 4597 | |
| 4598 | /** |
| 4599 | * Returns whether the given profile is in quiet mode or not. |
| 4600 | * Notes: Quiet mode is only supported for managed profiles. |
| 4601 | * |
| 4602 | * @param userHandle The user handle of the profile to be queried. |
| 4603 | * @return true if the profile is in quiet mode, false otherwise. |
| 4604 | */ |
| 4605 | public boolean isQuietModeEnabled(UserHandle userHandle) { |
| 4606 | try { |
| 4607 | return mService.isQuietModeEnabled(userHandle.getIdentifier()); |
| 4608 | } catch (RemoteException re) { |
| 4609 | throw re.rethrowFromSystemServer(); |
| 4610 | } |
| 4611 | } |
| 4612 | |
| 4613 | /** |
| 4614 | * Returns whether the given user has a badge (generally to put on profiles' icons). |
| 4615 | * |
| 4616 | * @param userId userId of the user in question |
| 4617 | * @return true if the user's icons should display a badge; false otherwise. |
| 4618 | * |
| 4619 | * @see #getBadgedIconForUser more information about badging in general |
| 4620 | * @hide |
| 4621 | */ |
| 4622 | public boolean hasBadge(@UserIdInt int userId) { |
| 4623 | if (!isProfile(userId)) { |
| 4624 | // Since currently only profiles actually have badges, we can do this optimization. |
| 4625 | return false; |
| 4626 | } |
| 4627 | try { |
| 4628 | return mService.hasBadge(userId); |
| 4629 | } catch (RemoteException re) { |
| 4630 | throw re.rethrowFromSystemServer(); |
| 4631 | } |
| 4632 | } |
| 4633 | |
| 4634 | /** |
| 4635 | * Returns whether the user associated with the context has a badge (generally to put on |
| 4636 | * profiles' icons). |
| 4637 | * |
| 4638 | * @return true if the user's icons should display a badge; false otherwise. |
| 4639 | * @see #getBadgedIconForUser more information about badging in general |
| 4640 | * @hide |
| 4641 | */ |
| 4642 | @UserHandleAware |
| 4643 | public boolean hasBadge() { |
| 4644 | return hasBadge(mUserId); |
| 4645 | } |
| 4646 | |
| 4647 | /** |
| 4648 | * Returns the light theme badge color for the given user (generally to color a profile's |
| 4649 | * icon's badge). |
| 4650 | * |
| 4651 | * <p>To check whether a badge color is expected for the user, first call {@link #hasBadge}. |
| 4652 | * |
| 4653 | * @return the color (not the resource ID) to be used for the user's badge |
| 4654 | * @throws Resources.NotFoundException if no valid badge color exists for this user |
| 4655 | * |
| 4656 | * @see #getBadgedIconForUser more information about badging in general |
| 4657 | * @hide |
| 4658 | */ |
| 4659 | public @ColorInt int getUserBadgeColor(@UserIdInt int userId) { |
| 4660 | try { |
| 4661 | final int resourceId = mService.getUserBadgeColorResId(userId); |
| 4662 | return Resources.getSystem().getColor(resourceId, null); |
| 4663 | } catch (RemoteException re) { |
| 4664 | throw re.rethrowFromSystemServer(); |
| 4665 | } |
| 4666 | } |
| 4667 | |
| 4668 | /** |
| 4669 | * Returns the dark theme badge color for the given user (generally to color a profile's icon's |
| 4670 | * badge). |
| 4671 | * |
| 4672 | * <p>To check whether a badge color is expected for the user, first call {@link #hasBadge}. |
| 4673 | * |
| 4674 | * @return the color (not the resource ID) to be used for the user's badge |
| 4675 | * @throws Resources.NotFoundException if no valid badge color exists for this user |
| 4676 | * |
| 4677 | * @see #getBadgedIconForUser more information about badging in general |
| 4678 | * @hide |
| 4679 | */ |
| 4680 | public @ColorInt int getUserBadgeDarkColor(@UserIdInt int userId) { |
| 4681 | try { |
| 4682 | final int resourceId = mService.getUserBadgeDarkColorResId(userId); |
| 4683 | return Resources.getSystem().getColor(resourceId, null); |
| 4684 | } catch (RemoteException re) { |
| 4685 | throw re.rethrowFromSystemServer(); |
| 4686 | } |
| 4687 | } |
| 4688 | |
| 4689 | /** |
| 4690 | * Returns the Resource ID of the user's icon badge. |
| 4691 | * |
| 4692 | * @return the Resource ID of the user's icon badge if it has one; otherwise |
| 4693 | * {@link Resources#ID_NULL}. |
| 4694 | * |
| 4695 | * @see #getBadgedIconForUser more information about badging in general |
| 4696 | * @hide |
| 4697 | */ |
| 4698 | public @DrawableRes int getUserIconBadgeResId(@UserIdInt int userId) { |
| 4699 | try { |
| 4700 | return mService.getUserIconBadgeResId(userId); |
| 4701 | } catch (RemoteException re) { |
| 4702 | throw re.rethrowFromSystemServer(); |
| 4703 | } |
| 4704 | } |
| 4705 | |
| 4706 | /** |
| 4707 | * Returns the Resource ID of the user's badge. |
| 4708 | * |
| 4709 | * @return the Resource ID of the user's badge if it has one; otherwise |
| 4710 | * {@link Resources#ID_NULL}. |
| 4711 | * |
| 4712 | * @see #getBadgedIconForUser more information about badging in general |
| 4713 | * @hide |
| 4714 | */ |
| 4715 | public @DrawableRes int getUserBadgeResId(@UserIdInt int userId) { |
| 4716 | try { |
| 4717 | return mService.getUserBadgeResId(userId); |
| 4718 | } catch (RemoteException re) { |
| 4719 | throw re.rethrowFromSystemServer(); |
| 4720 | } |
| 4721 | } |
| 4722 | |
| 4723 | /** |
| 4724 | * Returns the Resource ID of the user's badge without a background. |
| 4725 | * |
| 4726 | * @return the Resource ID of the user's no-background badge if it has one; otherwise |
| 4727 | * {@link Resources#ID_NULL}. |
| 4728 | * |
| 4729 | * @see #getBadgedIconForUser more information about badging in general |
| 4730 | * @hide |
| 4731 | */ |
| 4732 | public @DrawableRes int getUserBadgeNoBackgroundResId(@UserIdInt int userId) { |
| 4733 | try { |
| 4734 | return mService.getUserBadgeNoBackgroundResId(userId); |
| 4735 | } catch (RemoteException re) { |
| 4736 | throw re.rethrowFromSystemServer(); |
| 4737 | } |
| 4738 | } |
| 4739 | |
| 4740 | /** |
| 4741 | * If the target user is a profile of the calling user or the caller |
| 4742 | * is itself a profile, then this returns a badged copy of the given |
| 4743 | * icon to be able to distinguish it from the original icon. For badging an |
| 4744 | * arbitrary drawable use {@link #getBadgedDrawableForUser( |
| 4745 | * android.graphics.drawable.Drawable, UserHandle, android.graphics.Rect, int)}. |
| 4746 | * <p> |
| 4747 | * If the original drawable is a BitmapDrawable and the backing bitmap is |
| 4748 | * mutable as per {@link android.graphics.Bitmap#isMutable()}, the badging |
| 4749 | * is performed in place and the original drawable is returned. |
| 4750 | * </p> |
| 4751 | * |
| 4752 | * @param icon The icon to badge. |
| 4753 | * @param user The target user. |
| 4754 | * @return A drawable that combines the original icon and a badge as |
| 4755 | * determined by the system. |
| 4756 | * @removed |
| 4757 | */ |
| 4758 | public Drawable getBadgedIconForUser(Drawable icon, UserHandle user) { |
| 4759 | return mContext.getPackageManager().getUserBadgedIcon(icon, user); |
| 4760 | } |
| 4761 | |
| 4762 | /** |
| 4763 | * If the target user is a profile of the calling user or the caller |
| 4764 | * is itself a profile, then this returns a badged copy of the given |
| 4765 | * drawable allowing the user to distinguish it from the original drawable. |
| 4766 | * The caller can specify the location in the bounds of the drawable to be |
| 4767 | * badged where the badge should be applied as well as the density of the |
| 4768 | * badge to be used. |
| 4769 | * <p> |
| 4770 | * If the original drawable is a BitmapDrawable and the backing bitmap is |
| 4771 | * mutable as per {@link android.graphics.Bitmap#isMutable()}, the badging |
| 4772 | * is performed in place and the original drawable is returned. |
| 4773 | * </p> |
| 4774 | * |
| 4775 | * @param badgedDrawable The drawable to badge. |
| 4776 | * @param user The target user. |
| 4777 | * @param badgeLocation Where in the bounds of the badged drawable to place |
| 4778 | * the badge. If it's {@code null}, the badge is applied on top of the entire |
| 4779 | * drawable being badged. |
| 4780 | * @param badgeDensity The optional desired density for the badge as per |
| 4781 | * {@link android.util.DisplayMetrics#densityDpi}. If it's not positive, |
| 4782 | * the density of the display is used. |
| 4783 | * @return A drawable that combines the original drawable and a badge as |
| 4784 | * determined by the system. |
| 4785 | * @removed |
| 4786 | */ |
| 4787 | public Drawable getBadgedDrawableForUser(Drawable badgedDrawable, UserHandle user, |
| 4788 | Rect badgeLocation, int badgeDensity) { |
| 4789 | return mContext.getPackageManager().getUserBadgedDrawableForDensity(badgedDrawable, user, |
| 4790 | badgeLocation, badgeDensity); |
| 4791 | } |
| 4792 | |
| 4793 | /** |
| 4794 | * If the target user is a profile of the calling user or the caller |
| 4795 | * is itself a profile, then this returns a copy of the label with |
| 4796 | * badging for accessibility services like talkback. E.g. passing in "Email" |
| 4797 | * and it might return "Work Email" for Email in the work profile. |
| 4798 | * |
| 4799 | * <p>Requires {@link android.Manifest.permission#MANAGE_USERS} or |
| 4800 | * {@link android.Manifest.permission#INTERACT_ACROSS_USERS} permission, otherwise the caller |
| 4801 | * must be in the same profile group of specified user. |
| 4802 | * |
| 4803 | * @param label The label to change. |
| 4804 | * @param user The target user. |
| 4805 | * @return A label that combines the original label and a badge as |
| 4806 | * determined by the system. |
| 4807 | * @removed |
| 4808 | */ |
| 4809 | public CharSequence getBadgedLabelForUser(CharSequence label, UserHandle user) { |
| 4810 | final int userId = user.getIdentifier(); |
| 4811 | if (!hasBadge(userId)) { |
| 4812 | return label; |
| 4813 | } |
| 4814 | DevicePolicyManager dpm = mContext.getSystemService(DevicePolicyManager.class); |
| 4815 | return dpm.getResources().getString( |
| 4816 | getUpdatableUserBadgedLabelId(userId), |
| 4817 | () -> getDefaultUserBadgedLabel(label, userId), |
| 4818 | /* formatArgs= */ label); |
| 4819 | } |
| 4820 | |
| 4821 | private String getUpdatableUserBadgedLabelId(int userId) { |
| 4822 | return isManagedProfile(userId) ? WORK_PROFILE_BADGED_LABEL : UNDEFINED; |
| 4823 | } |
| 4824 | |
| 4825 | private String getDefaultUserBadgedLabel(CharSequence label, int userId) { |
| 4826 | try { |
| 4827 | final int resourceId = mService.getUserBadgeLabelResId(userId); |
| 4828 | return Resources.getSystem().getString(resourceId, label); |
| 4829 | } catch (RemoteException re) { |
| 4830 | throw re.rethrowFromSystemServer(); |
| 4831 | } |
| 4832 | } |
| 4833 | |
| 4834 | /** |
| 4835 | * If the user is a {@link UserManager#isProfile profile}, checks if the user |
| 4836 | * shares media with its parent user (the user that created this profile). |
| 4837 | * Returns false for any other type of user. |
| 4838 | * |
| 4839 | * <p>Requires {@link android.Manifest.permission#MANAGE_USERS} or |
| 4840 | * {@link android.Manifest.permission#INTERACT_ACROSS_USERS} permission, otherwise the |
| 4841 | * caller must be in the same profile group as the user. |
| 4842 | * |
| 4843 | * @return true if the user shares media with its parent user, false otherwise. |
| 4844 | * @hide |
| 4845 | */ |
| 4846 | @SystemApi |
| 4847 | @RequiresPermission(anyOf = {android.Manifest.permission.MANAGE_USERS, |
| 4848 | Manifest.permission.INTERACT_ACROSS_USERS}, conditional = true) |
| 4849 | @UserHandleAware |
| 4850 | @SuppressAutoDoc |
| 4851 | public boolean isMediaSharedWithParent() { |
| 4852 | try { |
| 4853 | return mService.isMediaSharedWithParent(mUserId); |
| 4854 | } catch (RemoteException re) { |
| 4855 | throw re.rethrowFromSystemServer(); |
| 4856 | } |
| 4857 | } |
| 4858 | |
| 4859 | /** |
| 4860 | * Returns whether the user can have shared lockscreen credential with its parent user. |
| 4861 | * |
| 4862 | * This API only works for {@link UserManager#isProfile() profiles} |
| 4863 | * and will always return false for any other user type. |
| 4864 | * |
| 4865 | * @hide |
| 4866 | */ |
| 4867 | @SystemApi |
| 4868 | @UserHandleAware( |
| 4869 | requiresAnyOfPermissionsIfNotCallerProfileGroup = { |
| 4870 | Manifest.permission.MANAGE_USERS, |
| 4871 | Manifest.permission.INTERACT_ACROSS_USERS}) |
| 4872 | @SuppressAutoDoc |
| 4873 | public boolean isCredentialSharableWithParent() { |
| 4874 | try { |
| 4875 | return mService.isCredentialSharableWithParent(mUserId); |
| 4876 | } catch (RemoteException re) { |
| 4877 | throw re.rethrowFromSystemServer(); |
| 4878 | } |
| 4879 | } |
| 4880 | |
| 4881 | /** |
| 4882 | * Removes a user and all associated data. |
| 4883 | * @param userId the integer handle of the user. |
| 4884 | * @hide |
| 4885 | */ |
| 4886 | @UnsupportedAppUsage |
| 4887 | @RequiresPermission(anyOf = {Manifest.permission.MANAGE_USERS, |
| 4888 | Manifest.permission.CREATE_USERS}) |
| 4889 | public boolean removeUser(@UserIdInt int userId) { |
| 4890 | try { |
| 4891 | return mService.removeUser(userId); |
| 4892 | } catch (RemoteException re) { |
| 4893 | throw re.rethrowFromSystemServer(); |
| 4894 | } |
| 4895 | } |
| 4896 | |
| 4897 | /** |
| 4898 | * Removes a user and all associated data. |
| 4899 | * |
| 4900 | * @param user the user that needs to be removed. |
| 4901 | * @return {@code true} if the user was successfully removed, {@code false} otherwise. |
| 4902 | * @throws IllegalArgumentException if {@code user} is {@code null} |
| 4903 | * @hide |
| 4904 | */ |
| 4905 | @SystemApi |
| 4906 | @RequiresPermission(anyOf = {Manifest.permission.MANAGE_USERS, |
| 4907 | Manifest.permission.CREATE_USERS}) |
| 4908 | public boolean removeUser(@NonNull UserHandle user) { |
| 4909 | if (user == null) { |
| 4910 | throw new IllegalArgumentException("user cannot be null"); |
| 4911 | } |
| 4912 | return removeUser(user.getIdentifier()); |
| 4913 | } |
| 4914 | |
| 4915 | |
| 4916 | /** |
| 4917 | * Similar to {@link #removeUser(int)} except bypassing the checking of |
| 4918 | * {@link UserManager#DISALLOW_REMOVE_USER} |
| 4919 | * or {@link UserManager#DISALLOW_REMOVE_MANAGED_PROFILE}. |
| 4920 | * |
| 4921 | * @see {@link #removeUser(int)} |
| 4922 | * @hide |
| 4923 | */ |
| 4924 | @RequiresPermission(anyOf = {Manifest.permission.MANAGE_USERS, |
| 4925 | Manifest.permission.CREATE_USERS}) |
| 4926 | public boolean removeUserEvenWhenDisallowed(@UserIdInt int userId) { |
| 4927 | try { |
| 4928 | return mService.removeUserEvenWhenDisallowed(userId); |
| 4929 | } catch (RemoteException re) { |
| 4930 | throw re.rethrowFromSystemServer(); |
| 4931 | } |
| 4932 | } |
| 4933 | |
| 4934 | /** |
| 4935 | * Immediately removes the user or, if the user cannot be removed, such as when the user is |
| 4936 | * the current user, then set the user as ephemeral so that it will be removed when it is |
| 4937 | * stopped. |
| 4938 | * |
| 4939 | * @param overrideDevicePolicy when {@code true}, user is removed even if the caller has |
| 4940 | * the {@link #DISALLOW_REMOVE_USER} or {@link #DISALLOW_REMOVE_MANAGED_PROFILE} restriction |
| 4941 | * |
| 4942 | * @return the {@link RemoveResult} code: {@link #REMOVE_RESULT_REMOVED}, |
| 4943 | * {@link #REMOVE_RESULT_DEFERRED}, {@link #REMOVE_RESULT_ALREADY_BEING_REMOVED}, |
| 4944 | * {@link #REMOVE_RESULT_ERROR_USER_RESTRICTION}, {@link #REMOVE_RESULT_ERROR_USER_NOT_FOUND}, |
| 4945 | * {@link #REMOVE_RESULT_ERROR_SYSTEM_USER}, or {@link #REMOVE_RESULT_ERROR_UNKNOWN}. All error |
| 4946 | * codes have negative values. |
| 4947 | * |
| 4948 | * @hide |
| 4949 | */ |
| 4950 | @SystemApi |
| 4951 | @RequiresPermission(anyOf = {Manifest.permission.MANAGE_USERS, |
| 4952 | Manifest.permission.CREATE_USERS}) |
| 4953 | public @RemoveResult int removeUserWhenPossible(@NonNull UserHandle user, |
| 4954 | boolean overrideDevicePolicy) { |
| 4955 | try { |
| 4956 | return mService.removeUserWhenPossible(user.getIdentifier(), overrideDevicePolicy); |
| 4957 | } catch (RemoteException re) { |
| 4958 | throw re.rethrowFromSystemServer(); |
| 4959 | } |
| 4960 | } |
| 4961 | |
| 4962 | /** |
| 4963 | * Check if {@link #removeUserWhenPossible} returned a success code which means that the user |
| 4964 | * has been removed or is slated for removal. |
| 4965 | * |
| 4966 | * @param result is {@link #RemoveResult} code return by {@link #removeUserWhenPossible}. |
| 4967 | * @return {@code true} if it is a success code. |
| 4968 | * @hide |
| 4969 | */ |
| 4970 | @SystemApi |
| 4971 | public static boolean isRemoveResultSuccessful(@RemoveResult int result) { |
| 4972 | return result >= 0; |
| 4973 | } |
| 4974 | |
| 4975 | /** |
| 4976 | * Updates the user's name. |
| 4977 | * |
| 4978 | * @param userId the user's integer id |
| 4979 | * @param name the new name for the user |
| 4980 | * @hide |
| 4981 | */ |
| 4982 | @RequiresPermission(android.Manifest.permission.MANAGE_USERS) |
| 4983 | public void setUserName(@UserIdInt int userId, String name) { |
| 4984 | try { |
| 4985 | mService.setUserName(userId, name); |
| 4986 | } catch (RemoteException re) { |
| 4987 | throw re.rethrowFromSystemServer(); |
| 4988 | } |
| 4989 | } |
| 4990 | |
| 4991 | /** |
| 4992 | * Set the user as ephemeral or non-ephemeral. |
| 4993 | * |
| 4994 | * If the user was initially created as ephemeral then this |
| 4995 | * method has no effect and false is returned. |
| 4996 | * |
| 4997 | * @param userId the user's integer id |
| 4998 | * @param enableEphemeral true: change user state to ephemeral, |
| 4999 | * false: change user state to non-ephemeral |
| 5000 | * @return true: user now has the desired ephemeral state, |
| 5001 | * false: desired user ephemeral state could not be set |
| 5002 | * |
| 5003 | * @hide |
| 5004 | */ |
| 5005 | @RequiresPermission(anyOf = { |
| 5006 | android.Manifest.permission.MANAGE_USERS, |
| 5007 | android.Manifest.permission.CREATE_USERS}) |
| 5008 | public boolean setUserEphemeral(@UserIdInt int userId, boolean enableEphemeral) { |
| 5009 | try { |
| 5010 | return mService.setUserEphemeral(userId, enableEphemeral); |
| 5011 | } catch (RemoteException re) { |
| 5012 | throw re.rethrowFromSystemServer(); |
| 5013 | } |
| 5014 | } |
| 5015 | |
| 5016 | /** |
| 5017 | * Updates the context user's name. |
| 5018 | * |
| 5019 | * @param name the new name for the user |
| 5020 | * @hide |
| 5021 | */ |
| 5022 | @SystemApi |
| 5023 | @RequiresPermission(android.Manifest.permission.MANAGE_USERS) |
| 5024 | @UserHandleAware |
| 5025 | public void setUserName(@Nullable String name) { |
| 5026 | setUserName(mUserId, name); |
| 5027 | } |
| 5028 | |
| 5029 | /** |
| 5030 | * Sets the user's photo. |
| 5031 | * @param userId the user for whom to change the photo. |
| 5032 | * @param icon the bitmap to set as the photo. |
| 5033 | * |
| 5034 | * @hide |
| 5035 | */ |
| 5036 | @RequiresPermission(android.Manifest.permission.MANAGE_USERS) |
| 5037 | public void setUserIcon(@UserIdInt int userId, @NonNull Bitmap icon) { |
| 5038 | try { |
| 5039 | mService.setUserIcon(userId, icon); |
| 5040 | } catch (ServiceSpecificException e) { |
| 5041 | return; |
| 5042 | } catch (RemoteException re) { |
| 5043 | throw re.rethrowFromSystemServer(); |
| 5044 | } |
| 5045 | } |
| 5046 | |
| 5047 | /** |
| 5048 | * Sets the context user's photo. |
| 5049 | * |
| 5050 | * @param icon the bitmap to set as the photo. |
| 5051 | * |
| 5052 | * @throws UserOperationException according to the function signature, but may not actually |
| 5053 | * throw it in practice. Catch RuntimeException instead. |
| 5054 | * |
| 5055 | * @hide |
| 5056 | */ |
| 5057 | @SystemApi |
| 5058 | @RequiresPermission(android.Manifest.permission.MANAGE_USERS) |
| 5059 | @UserHandleAware |
| 5060 | public void setUserIcon(@NonNull Bitmap icon) throws UserOperationException { |
| 5061 | setUserIcon(mUserId, icon); |
| 5062 | } |
| 5063 | |
| 5064 | /** |
| 5065 | * Returns a bitmap of the user's photo |
| 5066 | * @param userId the user whose photo we want to read. |
| 5067 | * @return a {@link Bitmap} of the user's photo, or null if there's no photo. |
| 5068 | * @see com.android.internal.util.UserIcons#getDefaultUserIcon for a default. |
| 5069 | * @hide |
| 5070 | */ |
| 5071 | @UnsupportedAppUsage |
| 5072 | @RequiresPermission(anyOf = {android.Manifest.permission.MANAGE_USERS, |
| 5073 | android.Manifest.permission.GET_ACCOUNTS_PRIVILEGED}) |
| 5074 | public Bitmap getUserIcon(@UserIdInt int userId) { |
| 5075 | try { |
| 5076 | ParcelFileDescriptor fd = mService.getUserIcon(userId); |
| 5077 | if (fd != null) { |
| 5078 | try { |
| 5079 | return BitmapFactory.decodeFileDescriptor(fd.getFileDescriptor()); |
| 5080 | } finally { |
| 5081 | try { |
| 5082 | fd.close(); |
| 5083 | } catch (IOException e) { |
| 5084 | } |
| 5085 | } |
| 5086 | } |
| 5087 | } catch (RemoteException re) { |
| 5088 | throw re.rethrowFromSystemServer(); |
| 5089 | } |
| 5090 | return null; |
| 5091 | } |
| 5092 | |
| 5093 | /** |
| 5094 | * Returns a Bitmap for the context user's photo. |
| 5095 | * |
| 5096 | * @return a {@link Bitmap} of the user's photo, or null if there's no photo. |
| 5097 | * @see com.android.internal.util.UserIcons#getDefaultUserIcon for a default. |
| 5098 | * @hide |
| 5099 | */ |
| 5100 | @SystemApi |
| 5101 | @RequiresPermission(anyOf = {android.Manifest.permission.MANAGE_USERS, |
| 5102 | android.Manifest.permission.GET_ACCOUNTS_PRIVILEGED}) |
| 5103 | @UserHandleAware |
| 5104 | public @Nullable Bitmap getUserIcon() { |
| 5105 | return getUserIcon(mUserId); |
| 5106 | } |
| 5107 | |
| 5108 | /** |
| 5109 | * Returns the maximum number of users that can be created on this device. A return value |
| 5110 | * of 1 means that it is a single user device. |
| 5111 | * @hide |
| 5112 | * @return a value greater than or equal to 1 |
| 5113 | */ |
| 5114 | @UnsupportedAppUsage |
| 5115 | public static int getMaxSupportedUsers() { |
| 5116 | // Don't allow multiple users on certain builds |
| 5117 | if (android.os.Build.ID.startsWith("JVP")) return 1; |
| 5118 | return Math.max(1, SystemProperties.getInt("fw.max_users", |
| 5119 | Resources.getSystem().getInteger(R.integer.config_multiuserMaximumUsers))); |
| 5120 | } |
| 5121 | |
| 5122 | /** |
| 5123 | * Returns true if the user switcher is enabled (regardless of whether there is anything |
| 5124 | * interesting for it to show). |
| 5125 | * |
| 5126 | * @return true if user switcher is enabled |
| 5127 | * @hide |
| 5128 | */ |
| 5129 | @RequiresPermission(anyOf = { |
| 5130 | android.Manifest.permission.MANAGE_USERS, |
| 5131 | android.Manifest.permission.CREATE_USERS // And INTERACT_ if diff profile group |
| 5132 | }) |
| 5133 | @UserHandleAware |
| 5134 | public boolean isUserSwitcherEnabled() { |
| 5135 | return isUserSwitcherEnabled(true); |
| 5136 | } |
| 5137 | |
| 5138 | /** |
| 5139 | * Returns true if the user switcher should be shown. |
| 5140 | * |
| 5141 | * @param showEvenIfNotActionable value to return if the feature is enabled but there is nothing |
| 5142 | * actionable for the user to do anyway |
| 5143 | * @return true if user switcher should be shown. |
| 5144 | * @hide |
| 5145 | */ |
| 5146 | @RequiresPermission(anyOf = { |
| 5147 | android.Manifest.permission.MANAGE_USERS, |
| 5148 | android.Manifest.permission.CREATE_USERS // And INTERACT_ if diff profile group |
| 5149 | }) |
| 5150 | @UserHandleAware |
| 5151 | public boolean isUserSwitcherEnabled(boolean showEvenIfNotActionable) { |
| 5152 | |
| 5153 | try { |
| 5154 | if (!mService.isUserSwitcherEnabled(mUserId)) { |
| 5155 | return false; |
| 5156 | } |
| 5157 | } catch (RemoteException re) { |
| 5158 | throw re.rethrowFromSystemServer(); |
| 5159 | } |
| 5160 | |
| 5161 | // The feature is enabled. But is it worth showing? |
| 5162 | return showEvenIfNotActionable |
| 5163 | || areThereUsersToWhichToSwitch() // There are switchable users. |
| 5164 | || !hasUserRestrictionForUser(DISALLOW_ADD_USER, mUserId); // New users can be added |
| 5165 | } |
| 5166 | |
| 5167 | /** Returns whether there are any users (other than the current user) to which to switch. */ |
| 5168 | @RequiresPermission(anyOf = { |
| 5169 | android.Manifest.permission.MANAGE_USERS, |
| 5170 | android.Manifest.permission.CREATE_USERS |
| 5171 | }) |
| 5172 | private boolean areThereUsersToWhichToSwitch() { |
| 5173 | final List<UserInfo> users = getAliveUsers(); |
| 5174 | if (users == null) { |
| 5175 | return false; |
| 5176 | } |
| 5177 | int switchableUserCount = 0; |
| 5178 | for (UserInfo user : users) { |
| 5179 | if (user.supportsSwitchToByUser()) { |
| 5180 | ++switchableUserCount; |
| 5181 | } |
| 5182 | } |
| 5183 | return switchableUserCount > 1; |
| 5184 | } |
| 5185 | |
| 5186 | /** |
| 5187 | * @hide |
| 5188 | */ |
| 5189 | @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.R, trackingBug = 170729553) |
| 5190 | public static boolean isDeviceInDemoMode(Context context) { |
| 5191 | return Settings.Global.getInt(context.getContentResolver(), |
| 5192 | Settings.Global.DEVICE_DEMO_MODE, 0) > 0; |
| 5193 | } |
| 5194 | |
| 5195 | /** |
| 5196 | * Returns a serial number on this device for a given userId. User handles can be recycled |
| 5197 | * when deleting and creating users, but serial numbers are not reused until the device is wiped. |
| 5198 | * @param userId |
| 5199 | * @return a serial number associated with that user, or -1 if the userId is not valid. |
| 5200 | * @hide |
| 5201 | */ |
| 5202 | @UnsupportedAppUsage |
| 5203 | public int getUserSerialNumber(@UserIdInt int userId) { |
| 5204 | try { |
| 5205 | return mService.getUserSerialNumber(userId); |
| 5206 | } catch (RemoteException re) { |
| 5207 | throw re.rethrowFromSystemServer(); |
| 5208 | } |
| 5209 | } |
| 5210 | |
| 5211 | /** |
| 5212 | * Returns a userId on this device for a given user serial number. User handles can be |
| 5213 | * recycled when deleting and creating users, but serial numbers are not reused until the device |
| 5214 | * is wiped. |
| 5215 | * @param userSerialNumber |
| 5216 | * @return the userId associated with that user serial number, or -1 if the serial number |
| 5217 | * is not valid. |
| 5218 | * @hide |
| 5219 | */ |
| 5220 | @UnsupportedAppUsage |
| 5221 | public @UserIdInt int getUserHandle(int userSerialNumber) { |
| 5222 | try { |
| 5223 | return mService.getUserHandle(userSerialNumber); |
| 5224 | } catch (RemoteException re) { |
| 5225 | throw re.rethrowFromSystemServer(); |
| 5226 | } |
| 5227 | } |
| 5228 | |
| 5229 | /** |
| 5230 | * Returns a {@link Bundle} containing any saved application restrictions for the context user, |
| 5231 | * for the given package name. Only an application with this package name can call this method. |
| 5232 | * |
| 5233 | * <p>The returned {@link Bundle} consists of key-value pairs, as defined by the application, |
| 5234 | * where the types of values may be: |
| 5235 | * <ul> |
| 5236 | * <li>{@code boolean} |
| 5237 | * <li>{@code int} |
| 5238 | * <li>{@code String} or {@code String[]} |
| 5239 | * <li>From {@link android.os.Build.VERSION_CODES#M}, {@code Bundle} or {@code Bundle[]} |
| 5240 | * </ul> |
| 5241 | * |
| 5242 | * <p>NOTE: The method performs disk I/O and shouldn't be called on the main thread |
| 5243 | * |
| 5244 | * @param packageName the package name of the calling application |
| 5245 | * @return a {@link Bundle} with the restrictions for that package, or an empty {@link Bundle} |
| 5246 | * if there are no saved restrictions. |
| 5247 | * |
| 5248 | * @see #KEY_RESTRICTIONS_PENDING |
| 5249 | */ |
| 5250 | @WorkerThread |
| 5251 | @UserHandleAware(enabledSinceTargetSdkVersion = Build.VERSION_CODES.TIRAMISU) |
| 5252 | public Bundle getApplicationRestrictions(String packageName) { |
| 5253 | try { |
| 5254 | return mService.getApplicationRestrictionsForUser(packageName, |
| 5255 | getContextUserIfAppropriate()); |
| 5256 | } catch (RemoteException re) { |
| 5257 | throw re.rethrowFromSystemServer(); |
| 5258 | } |
| 5259 | } |
| 5260 | |
| 5261 | /** |
| 5262 | * @hide |
| 5263 | */ |
| 5264 | @WorkerThread |
| 5265 | public Bundle getApplicationRestrictions(String packageName, UserHandle user) { |
| 5266 | try { |
| 5267 | return mService.getApplicationRestrictionsForUser(packageName, user.getIdentifier()); |
| 5268 | } catch (RemoteException re) { |
| 5269 | throw re.rethrowFromSystemServer(); |
| 5270 | } |
| 5271 | } |
| 5272 | |
| 5273 | /** |
| 5274 | * @hide |
| 5275 | */ |
| 5276 | @WorkerThread |
| 5277 | public void setApplicationRestrictions(String packageName, Bundle restrictions, |
| 5278 | UserHandle user) { |
| 5279 | try { |
| 5280 | mService.setApplicationRestrictions(packageName, restrictions, user.getIdentifier()); |
| 5281 | } catch (RemoteException re) { |
| 5282 | throw re.rethrowFromSystemServer(); |
| 5283 | } |
| 5284 | } |
| 5285 | |
| 5286 | /** |
| 5287 | * Sets a new challenge PIN for restrictions. This is only for use by pre-installed |
| 5288 | * apps and requires the MANAGE_USERS permission. |
| 5289 | * @param newPin the PIN to use for challenge dialogs. |
| 5290 | * @return Returns true if the challenge PIN was set successfully. |
| 5291 | * @deprecated The restrictions PIN functionality is no longer provided by the system. |
| 5292 | * This method is preserved for backwards compatibility reasons and always returns false. |
| 5293 | */ |
| 5294 | @Deprecated |
| 5295 | public boolean setRestrictionsChallenge(String newPin) { |
| 5296 | return false; |
| 5297 | } |
| 5298 | |
| 5299 | /** |
| 5300 | * @hide |
| 5301 | * Set restrictions that should apply to any future guest user that's created. |
| 5302 | */ |
| 5303 | @RequiresPermission(android.Manifest.permission.MANAGE_USERS) |
| 5304 | public void setDefaultGuestRestrictions(Bundle restrictions) { |
| 5305 | try { |
| 5306 | mService.setDefaultGuestRestrictions(restrictions); |
| 5307 | } catch (RemoteException re) { |
| 5308 | throw re.rethrowFromSystemServer(); |
| 5309 | } |
| 5310 | } |
| 5311 | |
| 5312 | /** |
| 5313 | * @hide |
| 5314 | * Gets the default guest restrictions. |
| 5315 | */ |
| 5316 | @RequiresPermission(android.Manifest.permission.MANAGE_USERS) |
| 5317 | public Bundle getDefaultGuestRestrictions() { |
| 5318 | try { |
| 5319 | return mService.getDefaultGuestRestrictions(); |
| 5320 | } catch (RemoteException re) { |
| 5321 | throw re.rethrowFromSystemServer(); |
| 5322 | } |
| 5323 | } |
| 5324 | |
| 5325 | /** |
| 5326 | * Returns creation time of the given user. The given user must be the calling user or |
| 5327 | * a profile associated with it. |
| 5328 | * @param userHandle user handle of the calling user or a profile associated with the |
| 5329 | * calling user. |
| 5330 | * @return creation time in milliseconds since Epoch time. |
| 5331 | */ |
| 5332 | public long getUserCreationTime(UserHandle userHandle) { |
| 5333 | try { |
| 5334 | return mService.getUserCreationTime(userHandle.getIdentifier()); |
| 5335 | } catch (RemoteException re) { |
| 5336 | throw re.rethrowFromSystemServer(); |
| 5337 | } |
| 5338 | } |
| 5339 | |
| 5340 | /** |
| 5341 | * Checks if any uninitialized user has the specific seed account name and type. |
| 5342 | * |
| 5343 | * @param accountName The account name to check for |
| 5344 | * @param accountType The account type of the account to check for |
| 5345 | * @return whether the seed account was found |
| 5346 | * @hide |
| 5347 | */ |
| 5348 | @RequiresPermission(android.Manifest.permission.MANAGE_USERS) |
| 5349 | public boolean someUserHasSeedAccount(String accountName, String accountType) { |
| 5350 | try { |
| 5351 | return mService.someUserHasSeedAccount(accountName, accountType); |
| 5352 | } catch (RemoteException re) { |
| 5353 | throw re.rethrowFromSystemServer(); |
| 5354 | } |
| 5355 | } |
| 5356 | |
| 5357 | /** |
| 5358 | * Checks if any initialized or uninitialized user has the specific account name and type. |
| 5359 | * |
| 5360 | * @param accountName The account name to check for |
| 5361 | * @param accountType The account type of the account to check for |
| 5362 | * @return whether the account was found |
| 5363 | * @hide |
| 5364 | */ |
| 5365 | @SystemApi |
| 5366 | @RequiresPermission(anyOf = {Manifest.permission.MANAGE_USERS, |
| 5367 | Manifest.permission.CREATE_USERS}) |
| 5368 | public boolean someUserHasAccount( |
| 5369 | @NonNull String accountName, @NonNull String accountType) { |
| 5370 | Objects.requireNonNull(accountName, "accountName must not be null"); |
| 5371 | Objects.requireNonNull(accountType, "accountType must not be null"); |
| 5372 | |
| 5373 | try { |
| 5374 | return mService.someUserHasAccount(accountName, accountType); |
| 5375 | } catch (RemoteException re) { |
| 5376 | throw re.rethrowFromSystemServer(); |
| 5377 | } |
| 5378 | } |
| 5379 | |
| 5380 | /* Cache key for anything that assumes that userIds cannot be re-used without rebooting. */ |
| 5381 | private static final String CACHE_KEY_STATIC_USER_PROPERTIES = "cache_key.static_user_props"; |
| 5382 | |
| 5383 | private final PropertyInvalidatedCache<Integer, String> mProfileTypeCache = |
| 5384 | new PropertyInvalidatedCache<Integer, String>(32, CACHE_KEY_STATIC_USER_PROPERTIES) { |
| 5385 | @Override |
| 5386 | public String recompute(Integer query) { |
| 5387 | try { |
| 5388 | // Will be null (and not cached) if invalid user; otherwise cache the type. |
| 5389 | String profileType = mService.getProfileType(query); |
| 5390 | if (profileType != null) profileType = profileType.intern(); |
| 5391 | return profileType; |
| 5392 | } catch (RemoteException re) { |
| 5393 | throw re.rethrowFromSystemServer(); |
| 5394 | } |
| 5395 | } |
| 5396 | @Override |
| 5397 | public boolean bypass(Integer query) { |
| 5398 | return query < 0; |
| 5399 | } |
| 5400 | }; |
| 5401 | |
| 5402 | /** {@hide} */ |
| 5403 | public static final void invalidateStaticUserProperties() { |
| 5404 | PropertyInvalidatedCache.invalidateCache(CACHE_KEY_STATIC_USER_PROPERTIES); |
| 5405 | } |
| 5406 | |
| 5407 | /** |
| 5408 | * @hide |
| 5409 | * User that enforces a restriction. |
| 5410 | * |
| 5411 | * @see #getUserRestrictionSources(String, UserHandle) |
| 5412 | */ |
| 5413 | @SystemApi |
| 5414 | public static final class EnforcingUser implements Parcelable { |
| 5415 | private final @UserIdInt int userId; |
| 5416 | private final @UserRestrictionSource int userRestrictionSource; |
| 5417 | |
| 5418 | /** |
| 5419 | * @hide |
| 5420 | */ |
| 5421 | public EnforcingUser( |
| 5422 | @UserIdInt int userId, @UserRestrictionSource int userRestrictionSource) { |
| 5423 | this.userId = userId; |
| 5424 | this.userRestrictionSource = userRestrictionSource; |
| 5425 | } |
| 5426 | |
| 5427 | private EnforcingUser(Parcel in) { |
| 5428 | userId = in.readInt(); |
| 5429 | userRestrictionSource = in.readInt(); |
| 5430 | } |
| 5431 | |
| 5432 | public static final @android.annotation.NonNull Creator<EnforcingUser> CREATOR = new Creator<EnforcingUser>() { |
| 5433 | @Override |
| 5434 | public EnforcingUser createFromParcel(Parcel in) { |
| 5435 | return new EnforcingUser(in); |
| 5436 | } |
| 5437 | |
| 5438 | @Override |
| 5439 | public EnforcingUser[] newArray(int size) { |
| 5440 | return new EnforcingUser[size]; |
| 5441 | } |
| 5442 | }; |
| 5443 | |
| 5444 | @Override |
| 5445 | public int describeContents() { |
| 5446 | return 0; |
| 5447 | } |
| 5448 | |
| 5449 | @Override |
| 5450 | public void writeToParcel(Parcel dest, int flags) { |
| 5451 | dest.writeInt(userId); |
| 5452 | dest.writeInt(userRestrictionSource); |
| 5453 | } |
| 5454 | |
| 5455 | /** |
| 5456 | * Returns an id of the enforcing user. |
| 5457 | * |
| 5458 | * <p> Will be UserHandle.USER_NULL when restriction is set by the system. |
| 5459 | */ |
| 5460 | public UserHandle getUserHandle() { |
| 5461 | return UserHandle.of(userId); |
| 5462 | } |
| 5463 | |
| 5464 | /** |
| 5465 | * Returns the status of the enforcing user. |
| 5466 | * |
| 5467 | * <p> One of {@link #RESTRICTION_SOURCE_SYSTEM}, |
| 5468 | * {@link #RESTRICTION_SOURCE_DEVICE_OWNER} and |
| 5469 | * {@link #RESTRICTION_SOURCE_PROFILE_OWNER} |
| 5470 | */ |
| 5471 | public @UserRestrictionSource int getUserRestrictionSource() { |
| 5472 | return userRestrictionSource; |
| 5473 | } |
| 5474 | } |
| 5475 | } |