blob: 3564589e35a40e8f5826afc86ec3c343be2a2adb [file] [log] [blame]
Amith Yamasani258848d2012-08-10 17:06:33 -07001/*
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 */
Jeff Sharkey8a372a02016-03-16 16:25:45 -060016
Amith Yamasani258848d2012-08-10 17:06:33 -070017package android.os;
18
Xiaohui Chenb3b92582015-12-07 11:22:13 -080019import android.Manifest;
Fyodor Kupolov02cb6e72015-09-18 18:20:55 -070020import android.accounts.AccountManager;
Bookatz029832a2019-10-04 16:50:22 -070021import android.annotation.ColorInt;
22import android.annotation.DrawableRes;
Zoltan Szatmary-Bane7834602016-04-08 18:41:11 +010023import android.annotation.IntDef;
Adam Lesinskiada8deb2017-05-12 13:50:42 -070024import android.annotation.NonNull;
Xiaohui Chen7cb69df2015-07-13 16:01:01 -070025import android.annotation.Nullable;
Xiaohui Chenb3b92582015-12-07 11:22:13 -080026import android.annotation.RequiresPermission;
Roshan Pius3eac0b92019-12-05 09:35:36 -080027import android.annotation.StringDef;
Amith Yamasani0e8d7d62014-09-03 13:17:28 -070028import android.annotation.SystemApi;
Jeff Sharkeyd86b8fe2017-06-02 17:36:26 -060029import android.annotation.SystemService;
Makoto Onukie72f81b2017-03-16 14:08:19 -070030import android.annotation.TestApi;
Valentin Iftime89df4c82019-08-23 13:02:50 +020031import android.annotation.UserHandleAware;
Jeff Sharkey8588bc12016-01-06 16:47:42 -070032import android.annotation.UserIdInt;
Fyodor Kupolov4e9af062016-07-18 16:59:11 -070033import android.annotation.WorkerThread;
Amith Yamasani37ed8d12016-01-27 14:40:16 -080034import android.app.Activity;
Dianne Hackborn409297d2014-07-10 17:39:20 -070035import android.app.ActivityManager;
Fyodor Kupolovcd86ebf2015-09-29 17:06:50 -070036import android.app.admin.DevicePolicyManager;
Artur Satayevafdb23a2019-12-10 17:47:53 +000037import android.compat.annotation.UnsupportedAppUsage;
Makoto Onuki068c54a2015-10-13 14:34:03 -070038import android.content.ComponentName;
Amith Yamasani258848d2012-08-10 17:06:33 -070039import android.content.Context;
Amith Yamasani12747872015-12-07 14:19:49 -080040import android.content.Intent;
Esteban Talavera8bd7c522017-02-13 12:35:04 +000041import android.content.IntentFilter;
Benjamin Franzf02420c2016-04-04 18:52:21 +010042import android.content.IntentSender;
Amith Yamasani258848d2012-08-10 17:06:33 -070043import android.content.pm.UserInfo;
Felipe Lemec1ca4412019-09-11 09:23:26 -070044import android.content.pm.UserInfo.UserInfoFlag;
Robin Lee83c79312018-02-09 19:27:18 +010045import android.content.res.Configuration;
Jeff Sharkey27bd34d2012-09-16 12:49:00 -070046import android.content.res.Resources;
Maggie Benthall67944582013-02-22 14:58:27 -050047import android.graphics.Bitmap;
Adrian Roos1bdff912015-02-17 15:51:35 +010048import android.graphics.BitmapFactory;
Amith Yamasani4f582632014-02-19 14:31:52 -080049import android.graphics.Rect;
Amith Yamasani4f582632014-02-19 14:31:52 -080050import android.graphics.drawable.Drawable;
Soonil Nagarkardc08e3a2019-12-02 12:31:45 -080051import android.location.LocationManager;
Amith Yamasani1e9c2182014-06-11 17:25:51 -070052import android.provider.Settings;
Fyodor Kupolov523c4042016-02-24 15:03:13 -080053import android.telephony.TelephonyManager;
Valentin Iftime89df4c82019-08-23 13:02:50 +020054import android.util.AndroidException;
Jason Monk1c7c3192014-06-26 12:52:18 -040055import android.view.WindowManager.LayoutParams;
Amith Yamasani258848d2012-08-10 17:06:33 -070056
Maggie Benthall67944582013-02-22 14:58:27 -050057import com.android.internal.R;
John Reckaa67f682016-09-20 14:24:21 -070058import com.android.internal.os.RoSystemProperties;
Maggie Benthall67944582013-02-22 14:58:27 -050059
Adrian Roos1bdff912015-02-17 15:51:35 +010060import java.io.IOException;
Zoltan Szatmary-Bane7834602016-04-08 18:41:11 +010061import java.lang.annotation.Retention;
62import java.lang.annotation.RetentionPolicy;
Amith Yamasani4f582632014-02-19 14:31:52 -080063import java.util.ArrayList;
Amith Yamasani258848d2012-08-10 17:06:33 -070064import java.util.List;
65
66/**
Amith Yamasani06964342016-04-15 13:55:01 -070067 * Manages users and user details on a multi-user system. There are two major categories of
68 * users: fully customizable users with their own login, and managed profiles that share a workspace
69 * with a related user.
70 * <p>
71 * Users are different from accounts, which are managed by
72 * {@link AccountManager}. Each user can have their own set of accounts.
73 * <p>
74 * See {@link DevicePolicyManager#ACTION_PROVISION_MANAGED_PROFILE} for more on managed profiles.
Amith Yamasani258848d2012-08-10 17:06:33 -070075 */
Jeff Sharkeyd86b8fe2017-06-02 17:36:26 -060076@SystemService(Context.USER_SERVICE)
Amith Yamasani258848d2012-08-10 17:06:33 -070077public class UserManager {
78
Pavel Grafov4f4f6f82017-03-28 13:44:04 +010079 private static final String TAG = "UserManager";
Andrei Onea24ec3212019-03-15 17:35:05 +000080 @UnsupportedAppUsage
Amith Yamasani258848d2012-08-10 17:06:33 -070081 private final IUserManager mService;
Valentin Iftime89df4c82019-08-23 13:02:50 +020082 /** Holding the Application context (not constructor param context). */
Amith Yamasani258848d2012-08-10 17:06:33 -070083 private final Context mContext;
84
Valentin Iftime89df4c82019-08-23 13:02:50 +020085 /** The userId of the constructor param context. To be used instead of mContext.getUserId(). */
86 private final @UserIdInt int mUserId;
87
Makoto Onukid49f3fa2017-01-25 14:09:48 -080088 private Boolean mIsManagedProfileCached;
Bookatz029832a2019-10-04 16:50:22 -070089 private Boolean mIsProfileCached;
90
91 /**
92 * User type representing a {@link UserHandle#USER_SYSTEM system} user that is a human user.
93 * This type of user cannot be created; it can only pre-exist on first boot.
94 * @hide
95 */
Valentin Iftime89df4c82019-08-23 13:02:50 +020096 @SystemApi
Bookatz029832a2019-10-04 16:50:22 -070097 public static final String USER_TYPE_FULL_SYSTEM = "android.os.usertype.full.SYSTEM";
98
99 /**
100 * User type representing a regular non-profile non-{@link UserHandle#USER_SYSTEM system} human
101 * user.
102 * This is sometimes called an ordinary 'secondary user'.
103 * @hide
104 */
Valentin Iftime89df4c82019-08-23 13:02:50 +0200105 @SystemApi
Bookatz029832a2019-10-04 16:50:22 -0700106 public static final String USER_TYPE_FULL_SECONDARY = "android.os.usertype.full.SECONDARY";
107
108 /**
109 * User type representing a guest user that may be transient.
110 * @hide
111 */
112 public static final String USER_TYPE_FULL_GUEST = "android.os.usertype.full.GUEST";
113
114 /**
115 * User type representing a user for demo purposes only, which can be removed at any time.
116 * @hide
117 */
118 public static final String USER_TYPE_FULL_DEMO = "android.os.usertype.full.DEMO";
119
120 /**
121 * User type representing a "restricted profile" user, which is a full user that is subject to
122 * certain restrictions from a parent user. Note, however, that it is NOT technically a profile.
123 * @hide
124 */
125 public static final String USER_TYPE_FULL_RESTRICTED = "android.os.usertype.full.RESTRICTED";
126
127 /**
128 * User type representing a managed profile, which is a profile that is to be managed by a
129 * device policy controller (DPC).
130 * The intended purpose is for work profiles, which are managed by a corporate entity.
131 * @hide
132 */
Valentin Iftime89df4c82019-08-23 13:02:50 +0200133 @SystemApi
Bookatz029832a2019-10-04 16:50:22 -0700134 public static final String USER_TYPE_PROFILE_MANAGED = "android.os.usertype.profile.MANAGED";
135
136 /**
137 * User type representing a {@link UserHandle#USER_SYSTEM system} user that is <b>not</b> a
138 * human user.
139 * This type of user cannot be created; it can only pre-exist on first boot.
140 * @hide
141 */
Valentin Iftime89df4c82019-08-23 13:02:50 +0200142 @SystemApi
Bookatz029832a2019-10-04 16:50:22 -0700143 public static final String USER_TYPE_SYSTEM_HEADLESS = "android.os.usertype.system.HEADLESS";
Makoto Onukid49f3fa2017-01-25 14:09:48 -0800144
Amith Yamasanie4cf7342012-12-17 11:12:09 -0800145 /**
Pierre Barbier de Reuille17eaf742020-01-10 17:49:45 +0000146 * Flag passed to {@link #requestQuietModeEnabled} to request disabling quiet mode only if
147 * there is no need to confirm the user credentials. If credentials are required to disable
148 * quiet mode, {@link #requestQuietModeEnabled} will do nothing and return {@code false}.
149 */
150 public static final int QUIET_MODE_DISABLE_ONLY_IF_CREDENTIAL_NOT_REQUIRED = 0x1;
151
152 /**
153 * List of flags available for the {@link #requestQuietModeEnabled} method.
154 * @hide
155 */
156 @Retention(RetentionPolicy.SOURCE)
157 @IntDef(flag = true, prefix = { "QUIET_MODE_" }, value = {
158 QUIET_MODE_DISABLE_ONLY_IF_CREDENTIAL_NOT_REQUIRED })
159 public @interface QuietModeFlag {}
160
161 /**
Zoltan Szatmary-Bane7834602016-04-08 18:41:11 +0100162 * @hide
163 * No user restriction.
164 */
165 @SystemApi
166 public static final int RESTRICTION_NOT_SET = 0x0;
167
168 /**
169 * @hide
170 * User restriction set by system/user.
171 */
172 @SystemApi
173 public static final int RESTRICTION_SOURCE_SYSTEM = 0x1;
174
175 /**
176 * @hide
177 * User restriction set by a device owner.
178 */
179 @SystemApi
180 public static final int RESTRICTION_SOURCE_DEVICE_OWNER = 0x2;
181
182 /**
183 * @hide
184 * User restriction set by a profile owner.
185 */
186 @SystemApi
187 public static final int RESTRICTION_SOURCE_PROFILE_OWNER = 0x4;
188
189 /** @hide */
190 @Retention(RetentionPolicy.SOURCE)
Jeff Sharkeyce8db992017-12-13 20:05:05 -0700191 @IntDef(flag = true, prefix = { "RESTRICTION_" }, value = {
192 RESTRICTION_NOT_SET,
193 RESTRICTION_SOURCE_SYSTEM,
194 RESTRICTION_SOURCE_DEVICE_OWNER,
195 RESTRICTION_SOURCE_PROFILE_OWNER
196 })
Zoltan Szatmary-Bane7834602016-04-08 18:41:11 +0100197 @SystemApi
198 public @interface UserRestrictionSource {}
199
200 /**
Fyodor Kupolov53019282015-07-21 11:48:18 -0700201 * Specifies if a user is disallowed from adding and removing accounts, unless they are
202 * {@link android.accounts.AccountManager#addAccountExplicitly programmatically} added by
203 * Authenticator.
Amith Yamasani71e6c692013-03-24 17:39:28 -0700204 * The default value is <code>false</code>.
Amith Yamasani26af8292014-09-09 09:57:27 -0700205 *
Benjamin Franzb6c0ce42015-11-05 10:06:51 +0000206 * <p>From {@link android.os.Build.VERSION_CODES#N} a profile or device owner app can still
207 * use {@link android.accounts.AccountManager} APIs to add or remove accounts when account
208 * management is disallowed.
209 *
Fyodor Kupolov0ffbfea2016-01-19 10:07:54 -0800210 * <p>Key for user restrictions.
211 * <p>Type: Boolean
Makoto Onuki068c54a2015-10-13 14:34:03 -0700212 * @see DevicePolicyManager#addUserRestriction(ComponentName, String)
213 * @see DevicePolicyManager#clearUserRestriction(ComponentName, String)
Amith Yamasanie4cf7342012-12-17 11:12:09 -0800214 * @see #getUserRestrictions()
215 */
Amith Yamasani71e6c692013-03-24 17:39:28 -0700216 public static final String DISALLOW_MODIFY_ACCOUNTS = "no_modify_accounts";
Amith Yamasanie4cf7342012-12-17 11:12:09 -0800217
218 /**
Amith Yamasani26af8292014-09-09 09:57:27 -0700219 * Specifies if a user is disallowed from changing Wi-Fi
Julia Reynolds2cb384f2014-08-13 15:15:55 -0400220 * access points. The default value is <code>false</code>.
Alex Johnston7c3d7e22020-01-08 12:57:14 +0000221 * <p>
222 * Device owner and profile owner can set this restriction, although the restriction has no
223 * effect in a managed profile. When it is set by the profile owner of an organization-owned
224 * managed profile on the parent profile, it will disallow the personal user from changing
225 * Wi-Fi access points.
Amith Yamasani26af8292014-09-09 09:57:27 -0700226 *
Fyodor Kupolov0ffbfea2016-01-19 10:07:54 -0800227 * <p>Key for user restrictions.
228 * <p>Type: Boolean
Makoto Onuki068c54a2015-10-13 14:34:03 -0700229 * @see DevicePolicyManager#addUserRestriction(ComponentName, String)
230 * @see DevicePolicyManager#clearUserRestriction(ComponentName, String)
Amith Yamasanie4cf7342012-12-17 11:12:09 -0800231 * @see #getUserRestrictions()
232 */
Amith Yamasani71e6c692013-03-24 17:39:28 -0700233 public static final String DISALLOW_CONFIG_WIFI = "no_config_wifi";
Amith Yamasanie4cf7342012-12-17 11:12:09 -0800234
235 /**
Christine Franks1bade5d2017-10-10 15:41:50 -0700236 * Specifies if a user is disallowed from changing the device
237 * language. The default value is <code>false</code>.
238 *
239 * <p>Key for user restrictions.
240 * <p>Type: Boolean
241 * @see DevicePolicyManager#addUserRestriction(ComponentName, String)
242 * @see DevicePolicyManager#clearUserRestriction(ComponentName, String)
243 * @see #getUserRestrictions()
244 */
245 public static final String DISALLOW_CONFIG_LOCALE = "no_config_locale";
246
247 /**
Benjamin Millerf1c3ed12018-05-03 10:56:28 +0000248 * Specifies if a user is disallowed from installing applications. This user restriction also
249 * prevents device owners and profile owners installing apps. The default value is
250 * {@code false}.
Amith Yamasani26af8292014-09-09 09:57:27 -0700251 *
Fyodor Kupolov0ffbfea2016-01-19 10:07:54 -0800252 * <p>Key for user restrictions.
253 * <p>Type: Boolean
Makoto Onuki068c54a2015-10-13 14:34:03 -0700254 * @see DevicePolicyManager#addUserRestriction(ComponentName, String)
255 * @see DevicePolicyManager#clearUserRestriction(ComponentName, String)
Amith Yamasanie4cf7342012-12-17 11:12:09 -0800256 * @see #getUserRestrictions()
257 */
Amith Yamasani71e6c692013-03-24 17:39:28 -0700258 public static final String DISALLOW_INSTALL_APPS = "no_install_apps";
Amith Yamasanie4cf7342012-12-17 11:12:09 -0800259
260 /**
Amith Yamasani26af8292014-09-09 09:57:27 -0700261 * Specifies if a user is disallowed from uninstalling applications.
Amith Yamasani71e6c692013-03-24 17:39:28 -0700262 * The default value is <code>false</code>.
Amith Yamasani26af8292014-09-09 09:57:27 -0700263 *
Fyodor Kupolov0ffbfea2016-01-19 10:07:54 -0800264 * <p>Key for user restrictions.
265 * <p>Type: Boolean
Makoto Onuki068c54a2015-10-13 14:34:03 -0700266 * @see DevicePolicyManager#addUserRestriction(ComponentName, String)
267 * @see DevicePolicyManager#clearUserRestriction(ComponentName, String)
Amith Yamasanie4cf7342012-12-17 11:12:09 -0800268 * @see #getUserRestrictions()
269 */
Amith Yamasani71e6c692013-03-24 17:39:28 -0700270 public static final String DISALLOW_UNINSTALL_APPS = "no_uninstall_apps";
Amith Yamasanie4cf7342012-12-17 11:12:09 -0800271
Amith Yamasani71e6c692013-03-24 17:39:28 -0700272 /**
Amith Yamasani150514b2015-01-07 16:05:05 -0800273 * Specifies if a user is disallowed from turning on location sharing.
Amith Yamasani71e6c692013-03-24 17:39:28 -0700274 * The default value is <code>false</code>.
Alex Johnston7c3d7e22020-01-08 12:57:14 +0000275 * <p>
276 * In a managed profile, location sharing always reflects the primary user's setting, but
Amith Yamasani150514b2015-01-07 16:05:05 -0800277 * can be overridden and forced off by setting this restriction to true in the managed profile.
Alex Johnston7c3d7e22020-01-08 12:57:14 +0000278 * <p>
279 * Device owner and profile owner can set this restriction. When it is set by the profile
280 * owner of an organization-owned managed profile on the parent profile, it will prevent the
281 * user from turning on location sharing in the personal profile.
Amith Yamasani26af8292014-09-09 09:57:27 -0700282 *
Fyodor Kupolov0ffbfea2016-01-19 10:07:54 -0800283 * <p>Key for user restrictions.
284 * <p>Type: Boolean
Makoto Onuki068c54a2015-10-13 14:34:03 -0700285 * @see DevicePolicyManager#addUserRestriction(ComponentName, String)
286 * @see DevicePolicyManager#clearUserRestriction(ComponentName, String)
Maggie Benthall67944582013-02-22 14:58:27 -0500287 * @see #getUserRestrictions()
288 */
Amith Yamasani71e6c692013-03-24 17:39:28 -0700289 public static final String DISALLOW_SHARE_LOCATION = "no_share_location";
Maggie Benthall67944582013-02-22 14:58:27 -0500290
Maggie Benthalla12fccf2013-03-14 18:02:12 -0400291 /**
yuemingw5fe75dc2017-11-29 15:52:56 +0000292 * Specifies if airplane mode is disallowed on the device.
Alex Johnston9722abe2020-01-28 16:15:55 +0000293 * <p>
294 * This restriction can only be set by the device owner, the profile owner on the primary user
295 * or the profile owner of an organization-owned managed profile on the parent profile, and it
296 * applies globally - i.e. it disables airplane mode on the entire device.
297 * <p>
298 * The default value is <code>false</code>.
yuemingw5fe75dc2017-11-29 15:52:56 +0000299 *
300 * <p>Key for user restrictions.
301 * <p>Type: Boolean
302 * @see DevicePolicyManager#addUserRestriction(ComponentName, String)
303 * @see DevicePolicyManager#clearUserRestriction(ComponentName, String)
304 * @see #getUserRestrictions()
305 */
306 public static final String DISALLOW_AIRPLANE_MODE = "no_airplane_mode";
307
308 /**
yuemingwc6ac29d2018-01-10 16:54:08 +0000309 * Specifies if a user is disallowed from configuring brightness. When device owner sets it,
310 * it'll only be applied on the target(system) user.
311 *
312 * <p>The default value is <code>false</code>.
313 *
314 * <p>This user restriction has no effect on managed profiles.
315 * <p>Key for user restrictions.
316 * <p>Type: Boolean
317 * @see DevicePolicyManager#addUserRestriction(ComponentName, String)
318 * @see DevicePolicyManager#clearUserRestriction(ComponentName, String)
319 * @see #getUserRestrictions()
320 */
321 public static final String DISALLOW_CONFIG_BRIGHTNESS = "no_config_brightness";
322
323 /**
yuemingwa1781442018-01-10 18:59:43 +0000324 * Specifies if ambient display is disallowed for the user.
325 *
326 * <p>The default value is <code>false</code>.
327 *
328 * <p>This user restriction has no effect on managed profiles.
329 * <p>Key for user restrictions.
330 * <p>Type: Boolean
331 * @see DevicePolicyManager#addUserRestriction(ComponentName, String)
332 * @see DevicePolicyManager#clearUserRestriction(ComponentName, String)
333 * @see #getUserRestrictions()
334 */
335 public static final String DISALLOW_AMBIENT_DISPLAY = "no_ambient_display";
336
337 /**
yuemingw5cda3ae2018-01-15 10:26:19 +0000338 * Specifies if a user is disallowed from changing screen off timeout.
339 *
340 * <p>The default value is <code>false</code>.
341 *
342 * <p>This user restriction has no effect on managed profiles.
343 * <p>Key for user restrictions.
344 * <p>Type: Boolean
345 * @see DevicePolicyManager#addUserRestriction(ComponentName, String)
346 * @see DevicePolicyManager#clearUserRestriction(ComponentName, String)
347 * @see #getUserRestrictions()
348 */
349 public static final String DISALLOW_CONFIG_SCREEN_TIMEOUT = "no_config_screen_timeout";
350
351 /**
Amith Yamasani26af8292014-09-09 09:57:27 -0700352 * Specifies if a user is disallowed from enabling the
Maggie Benthalla12fccf2013-03-14 18:02:12 -0400353 * "Unknown Sources" setting, that allows installation of apps from unknown sources.
Irina Dumitrescu4638edd2018-09-05 14:08:33 +0100354 * Unknown sources exclude adb and special apps such as trusted app stores.
Maggie Benthalla12fccf2013-03-14 18:02:12 -0400355 * The default value is <code>false</code>.
Amith Yamasani26af8292014-09-09 09:57:27 -0700356 *
Fyodor Kupolov0ffbfea2016-01-19 10:07:54 -0800357 * <p>Key for user restrictions.
358 * <p>Type: Boolean
Makoto Onuki068c54a2015-10-13 14:34:03 -0700359 * @see DevicePolicyManager#addUserRestriction(ComponentName, String)
360 * @see DevicePolicyManager#clearUserRestriction(ComponentName, String)
Maggie Benthalla12fccf2013-03-14 18:02:12 -0400361 * @see #getUserRestrictions()
362 */
363 public static final String DISALLOW_INSTALL_UNKNOWN_SOURCES = "no_install_unknown_sources";
364
365 /**
Tor Norbye33ca9122018-10-03 09:28:21 +0200366 * This restriction is a device-wide version of {@link #DISALLOW_INSTALL_UNKNOWN_SOURCES}.
Irina Dumitrescu4638edd2018-09-05 14:08:33 +0100367 *
368 * Specifies if all users on the device are disallowed from enabling the
369 * "Unknown Sources" setting, that allows installation of apps from unknown sources.
Irina Dumitrescu0501eee2018-10-19 23:50:18 +0100370 *
371 * This restriction can be enabled by the profile owner, in which case all accounts and
372 * profiles will be affected.
373 *
Irina Dumitrescu4638edd2018-09-05 14:08:33 +0100374 * The default value is <code>false</code>.
375 *
376 * <p>Key for user restrictions.
377 * <p>Type: Boolean
378 * @see DevicePolicyManager#addUserRestriction(ComponentName, String)
379 * @see DevicePolicyManager#clearUserRestriction(ComponentName, String)
380 * @see #getUserRestrictions()
381 */
382 public static final String DISALLOW_INSTALL_UNKNOWN_SOURCES_GLOBALLY =
383 "no_install_unknown_sources_globally";
384
385 /**
Amith Yamasani26af8292014-09-09 09:57:27 -0700386 * Specifies if a user is disallowed from configuring bluetooth.
Nicolas Prevot1c4c4422015-02-16 11:32:21 +0000387 * This does <em>not</em> restrict the user from turning bluetooth on or off.
Maggie Benthalla12fccf2013-03-14 18:02:12 -0400388 * The default value is <code>false</code>.
Alex Johnston7c3d7e22020-01-08 12:57:14 +0000389 * <p>
390 * This restriction doesn't prevent the user from using bluetooth. For disallowing usage of
Lenka Trochtova63d5e4a72016-12-02 12:19:39 +0100391 * bluetooth completely on the device, use {@link #DISALLOW_BLUETOOTH}.
Alex Johnston7c3d7e22020-01-08 12:57:14 +0000392 * <p>
393 * Device owner and profile owner can set this restriction, although the restriction has no
394 * effect in a managed profile. When it is set by the profile owner of an organization-owned
395 * managed profile on the parent profile, it will disallow the personal user from configuring
396 * bluetooth.
Amith Yamasani26af8292014-09-09 09:57:27 -0700397 *
Fyodor Kupolov0ffbfea2016-01-19 10:07:54 -0800398 * <p>Key for user restrictions.
399 * <p>Type: Boolean
Makoto Onuki068c54a2015-10-13 14:34:03 -0700400 * @see DevicePolicyManager#addUserRestriction(ComponentName, String)
401 * @see DevicePolicyManager#clearUserRestriction(ComponentName, String)
Maggie Benthalla12fccf2013-03-14 18:02:12 -0400402 * @see #getUserRestrictions()
403 */
404 public static final String DISALLOW_CONFIG_BLUETOOTH = "no_config_bluetooth";
405
Maggie Benthalla12fccf2013-03-14 18:02:12 -0400406 /**
Lenka Trochtova63d5e4a72016-12-02 12:19:39 +0100407 * Specifies if bluetooth is disallowed on the device.
408 *
Alex Johnston7c3d7e22020-01-08 12:57:14 +0000409 * <p> This restriction can only be set by the device owner, the profile owner on the
410 * primary user or the profile owner of an organization-owned managed profile on the
411 * parent profile and it applies globally - i.e. it disables bluetooth on the entire
412 * device.
Lenka Trochtova63d5e4a72016-12-02 12:19:39 +0100413 * <p>The default value is <code>false</code>.
414 * <p>Key for user restrictions.
415 * <p>Type: Boolean
416 * @see DevicePolicyManager#addUserRestriction(ComponentName, String)
417 * @see DevicePolicyManager#clearUserRestriction(ComponentName, String)
418 * @see #getUserRestrictions()
419 */
420 public static final String DISALLOW_BLUETOOTH = "no_bluetooth";
421
422 /**
Pavel Grafov4f4f6f82017-03-28 13:44:04 +0100423 * Specifies if outgoing bluetooth sharing is disallowed on the device. Device owner and profile
Alex Johnston7c3d7e22020-01-08 12:57:14 +0000424 * owner can set this restriction. When it is set by device owner or the profile owner of an
425 * organization-owned managed profile on the parent profile, all users on this device will be
426 * affected.
Pavel Grafov4f4f6f82017-03-28 13:44:04 +0100427 *
428 * <p>Default is <code>true</code> for managed profiles and false for otherwise. When a device
429 * upgrades to {@link android.os.Build.VERSION_CODES#O}, the system sets it for all existing
430 * managed profiles.
431 *
432 * <p>Key for user restrictions.
433 * <p>Type: Boolean
434 * @see DevicePolicyManager#addUserRestriction(ComponentName, String)
435 * @see DevicePolicyManager#clearUserRestriction(ComponentName, String)
436 * @see #getUserRestrictions()
437 */
438 public static final String DISALLOW_BLUETOOTH_SHARING = "no_bluetooth_sharing";
439
440 /**
Amith Yamasani26af8292014-09-09 09:57:27 -0700441 * Specifies if a user is disallowed from transferring files over
Alex Johnston7c3d7e22020-01-08 12:57:14 +0000442 * USB. This can only be set by device owners, profile owners on the primary user or
443 * profile owners of organization-owned managed profiles on the parent profile.
Amith Yamasanic34dc7c2014-09-18 09:42:42 -0700444 * The default value is <code>false</code>.
Amith Yamasani26af8292014-09-09 09:57:27 -0700445 *
Fyodor Kupolov0ffbfea2016-01-19 10:07:54 -0800446 * <p>Key for user restrictions.
447 * <p>Type: Boolean
Makoto Onuki068c54a2015-10-13 14:34:03 -0700448 * @see DevicePolicyManager#addUserRestriction(ComponentName, String)
449 * @see DevicePolicyManager#clearUserRestriction(ComponentName, String)
Maggie Benthalla12fccf2013-03-14 18:02:12 -0400450 * @see #getUserRestrictions()
451 */
452 public static final String DISALLOW_USB_FILE_TRANSFER = "no_usb_file_transfer";
453
Emily Bernierb223f732013-04-11 15:46:36 -0400454 /**
Amith Yamasani26af8292014-09-09 09:57:27 -0700455 * Specifies if a user is disallowed from configuring user
Emily Bernierb223f732013-04-11 15:46:36 -0400456 * credentials. The default value is <code>false</code>.
Amith Yamasani26af8292014-09-09 09:57:27 -0700457 *
Fyodor Kupolov0ffbfea2016-01-19 10:07:54 -0800458 * <p>Key for user restrictions.
459 * <p>Type: Boolean
Makoto Onuki068c54a2015-10-13 14:34:03 -0700460 * @see DevicePolicyManager#addUserRestriction(ComponentName, String)
461 * @see DevicePolicyManager#clearUserRestriction(ComponentName, String)
Emily Bernierb223f732013-04-11 15:46:36 -0400462 * @see #getUserRestrictions()
463 */
464 public static final String DISALLOW_CONFIG_CREDENTIALS = "no_config_credentials";
465
466 /**
Amith Yamasani150514b2015-01-07 16:05:05 -0800467 * When set on the primary user this specifies if the user can remove other users.
468 * When set on a secondary user, this specifies if the user can remove itself.
469 * This restriction has no effect on managed profiles.
470 * The default value is <code>false</code>.
Amith Yamasani26af8292014-09-09 09:57:27 -0700471 *
Fyodor Kupolov0ffbfea2016-01-19 10:07:54 -0800472 * <p>Key for user restrictions.
473 * <p>Type: Boolean
Makoto Onuki068c54a2015-10-13 14:34:03 -0700474 * @see DevicePolicyManager#addUserRestriction(ComponentName, String)
475 * @see DevicePolicyManager#clearUserRestriction(ComponentName, String)
Emily Bernierb223f732013-04-11 15:46:36 -0400476 * @see #getUserRestrictions()
477 */
478 public static final String DISALLOW_REMOVE_USER = "no_remove_user";
479
Julia Reynoldsd46d0f92014-04-23 15:23:24 -0400480 /**
Esteban Talavera6c9116a2016-11-24 16:12:44 +0000481 * Specifies if managed profiles of this user can be removed, other than by its profile owner.
482 * The default value is <code>false</code>.
483 * <p>
Nicolas Prevot2ea46fe2017-01-05 10:29:34 +0000484 * This restriction has no effect on managed profiles.
Esteban Talavera6c9116a2016-11-24 16:12:44 +0000485 *
486 * <p>Key for user restrictions.
487 * <p>Type: Boolean
488 * @see DevicePolicyManager#addUserRestriction(ComponentName, String)
489 * @see DevicePolicyManager#clearUserRestriction(ComponentName, String)
490 * @see #getUserRestrictions()
Eran Messerice273df2019-12-17 13:14:45 +0000491 * @deprecated As the ability to have a managed profile on a fully-managed device has been
492 * removed from the platform, this restriction will be silently ignored when applied by the
493 * device owner.
494 * When the device is provisioned with a managed profile on an organization-owned device,
495 * the managed profile could not be removed anyway.
Esteban Talavera6c9116a2016-11-24 16:12:44 +0000496 */
Eran Messerice273df2019-12-17 13:14:45 +0000497 @Deprecated
Esteban Talavera6c9116a2016-11-24 16:12:44 +0000498 public static final String DISALLOW_REMOVE_MANAGED_PROFILE = "no_remove_managed_profile";
499
500 /**
Pavel Grafov9fb706c2018-02-27 14:07:52 +0000501 * Specifies if a user is disallowed from enabling or accessing debugging features. When set on
Alex Johnston7c3d7e22020-01-08 12:57:14 +0000502 * the primary user or by the profile owner of an organization-owned managed profile on the
503 * parent profile, disables debugging features altogether, including USB debugging. When set on
504 * a managed profile or a secondary user, blocks debugging for that user only, including
Pavel Grafov9fb706c2018-02-27 14:07:52 +0000505 * starting activities, making service calls, accessing content providers, sending broadcasts,
506 * installing/uninstalling packages, clearing user data, etc.
507 * The default value is <code>false</code>.
Amith Yamasani26af8292014-09-09 09:57:27 -0700508 *
Fyodor Kupolov0ffbfea2016-01-19 10:07:54 -0800509 * <p>Key for user restrictions.
510 * <p>Type: Boolean
Makoto Onuki068c54a2015-10-13 14:34:03 -0700511 * @see DevicePolicyManager#addUserRestriction(ComponentName, String)
512 * @see DevicePolicyManager#clearUserRestriction(ComponentName, String)
Julia Reynoldsd46d0f92014-04-23 15:23:24 -0400513 * @see #getUserRestrictions()
514 */
515 public static final String DISALLOW_DEBUGGING_FEATURES = "no_debugging_features";
516
517 /**
Benjamin Miller05fef7e2017-07-24 10:11:39 +0200518 * Specifies if a user is disallowed from configuring a VPN. The default value is
519 * <code>false</code>. This restriction has an effect when set by device owners and, in Android
520 * 6.0 ({@linkplain android.os.Build.VERSION_CODES#M API level 23}) or higher, profile owners.
521 * <p>This restriction also prevents VPNs from starting. However, in Android 7.0
522 * ({@linkplain android.os.Build.VERSION_CODES#N API level 24}) or higher, the system does
523 * start always-on VPNs created by the device or profile owner.
Amith Yamasani26af8292014-09-09 09:57:27 -0700524 *
Fyodor Kupolov0ffbfea2016-01-19 10:07:54 -0800525 * <p>Key for user restrictions.
526 * <p>Type: Boolean
Makoto Onuki068c54a2015-10-13 14:34:03 -0700527 * @see DevicePolicyManager#addUserRestriction(ComponentName, String)
528 * @see DevicePolicyManager#clearUserRestriction(ComponentName, String)
Julia Reynoldsd46d0f92014-04-23 15:23:24 -0400529 * @see #getUserRestrictions()
530 */
531 public static final String DISALLOW_CONFIG_VPN = "no_config_vpn";
532
533 /**
yuemingw7810b8b2018-02-01 17:32:25 +0000534 * Specifies if a user is disallowed from enabling or disabling location providers. As a
Alex Johnston7c3d7e22020-01-08 12:57:14 +0000535 * result, user is disallowed from turning on or off location.
yuemingw7cc2c4c2017-11-28 17:20:01 +0000536 *
Alex Johnston7c3d7e22020-01-08 12:57:14 +0000537 * <p>
538 * In a managed profile, location sharing is forced off when it is turned off on the primary
539 * user or by the profile owner of an organization-owned managed profile on the parent profile.
540 * The user can still turn off location sharing on a managed profile when the restriction is
541 * set by the profile owner on a managed profile.
542 * <p>
543 * This user restriction is different from {@link #DISALLOW_SHARE_LOCATION},
yuemingw7cc2c4c2017-11-28 17:20:01 +0000544 * as the device owner or profile owner can still enable or disable location mode via
Soonil Nagarkardc08e3a2019-12-02 12:31:45 -0800545 * {@link DevicePolicyManager#setLocationEnabled} when this restriction is on.
Alex Johnston7c3d7e22020-01-08 12:57:14 +0000546 * <p>
547 * The default value is <code>false</code>.
yuemingw7cc2c4c2017-11-28 17:20:01 +0000548 *
549 * <p>Key for user restrictions.
550 * <p>Type: Boolean
Soonil Nagarkardc08e3a2019-12-02 12:31:45 -0800551 * @see LocationManager#isLocationEnabled()
yuemingw7cc2c4c2017-11-28 17:20:01 +0000552 * @see DevicePolicyManager#addUserRestriction(ComponentName, String)
553 * @see DevicePolicyManager#clearUserRestriction(ComponentName, String)
554 * @see #getUserRestrictions()
555 */
yuemingw7810b8b2018-02-01 17:32:25 +0000556 public static final String DISALLOW_CONFIG_LOCATION = "no_config_location";
yuemingw7cc2c4c2017-11-28 17:20:01 +0000557
558 /**
yuemingwa9772f362017-10-23 18:34:35 +0100559 * Specifies if date, time and timezone configuring is disallowed.
560 *
Alex Johnston7c3d7e22020-01-08 12:57:14 +0000561 * <p>When restriction is set by device owners or profile owners of organization-owned
562 * managed profiles on the parent profile, it applies globally - i.e., it disables date,
yuemingwa9772f362017-10-23 18:34:35 +0100563 * time and timezone setting on the entire device and all users will be affected. When it's set
564 * by profile owners, it's only applied to the managed user.
565 * <p>The default value is <code>false</code>.
566 *
567 * <p>This user restriction has no effect on managed profiles.
568 * <p>Key for user restrictions.
569 * <p>Type: Boolean
570 * @see DevicePolicyManager#addUserRestriction(ComponentName, String)
571 * @see DevicePolicyManager#clearUserRestriction(ComponentName, String)
572 * @see #getUserRestrictions()
573 */
574 public static final String DISALLOW_CONFIG_DATE_TIME = "no_config_date_time";
575
576 /**
Amith Yamasani26af8292014-09-09 09:57:27 -0700577 * Specifies if a user is disallowed from configuring Tethering
Alex Johnston7c3d7e22020-01-08 12:57:14 +0000578 * & portable hotspots. This can only be set by device owners, profile owners on the
579 * primary user or profile owners of organization-owned managed profiles on the parent profile.
580 * The default value is <code>false</code>.
Rubin Xu1faf1442017-08-23 15:48:12 +0100581 * <p>In Android 9.0 or higher, if tethering is enabled when this restriction is set,
582 * tethering will be automatically turned off.
Amith Yamasani26af8292014-09-09 09:57:27 -0700583 *
Fyodor Kupolov0ffbfea2016-01-19 10:07:54 -0800584 * <p>Key for user restrictions.
585 * <p>Type: Boolean
Makoto Onuki068c54a2015-10-13 14:34:03 -0700586 * @see DevicePolicyManager#addUserRestriction(ComponentName, String)
587 * @see DevicePolicyManager#clearUserRestriction(ComponentName, String)
Julia Reynoldsd46d0f92014-04-23 15:23:24 -0400588 * @see #getUserRestrictions()
589 */
590 public static final String DISALLOW_CONFIG_TETHERING = "no_config_tethering";
591
592 /**
Stuart Scotte3e314d2015-04-20 14:07:45 -0700593 * Specifies if a user is disallowed from resetting network settings
594 * from Settings. This can only be set by device owners and profile owners on the primary user.
595 * The default value is <code>false</code>.
Fyodor Kupolov0ffbfea2016-01-19 10:07:54 -0800596 * <p>This restriction has no effect on secondary users and managed profiles since only the
Stuart Scotte3e314d2015-04-20 14:07:45 -0700597 * primary user can reset the network settings of the device.
598 *
Fyodor Kupolov0ffbfea2016-01-19 10:07:54 -0800599 * <p>Key for user restrictions.
600 * <p>Type: Boolean
Makoto Onuki068c54a2015-10-13 14:34:03 -0700601 * @see DevicePolicyManager#addUserRestriction(ComponentName, String)
602 * @see DevicePolicyManager#clearUserRestriction(ComponentName, String)
Stuart Scotte3e314d2015-04-20 14:07:45 -0700603 * @see #getUserRestrictions()
604 */
605 public static final String DISALLOW_NETWORK_RESET = "no_network_reset";
606
607 /**
Amith Yamasani26af8292014-09-09 09:57:27 -0700608 * Specifies if a user is disallowed from factory resetting
Amith Yamasanic34dc7c2014-09-18 09:42:42 -0700609 * from Settings. This can only be set by device owners and profile owners on the primary user.
610 * The default value is <code>false</code>.
Fyodor Kupolov0ffbfea2016-01-19 10:07:54 -0800611 * <p>This restriction has no effect on secondary users and managed profiles since only the
Amith Yamasani150514b2015-01-07 16:05:05 -0800612 * primary user can factory reset the device.
Amith Yamasani26af8292014-09-09 09:57:27 -0700613 *
Fyodor Kupolov0ffbfea2016-01-19 10:07:54 -0800614 * <p>Key for user restrictions.
615 * <p>Type: Boolean
Makoto Onuki068c54a2015-10-13 14:34:03 -0700616 * @see DevicePolicyManager#addUserRestriction(ComponentName, String)
617 * @see DevicePolicyManager#clearUserRestriction(ComponentName, String)
Julia Reynoldsd46d0f92014-04-23 15:23:24 -0400618 * @see #getUserRestrictions()
619 */
620 public static final String DISALLOW_FACTORY_RESET = "no_factory_reset";
621
622 /**
Esteban Talavera6c9116a2016-11-24 16:12:44 +0000623 * Specifies if a user is disallowed from adding new users. This can only be set by device
Alex Johnston7c3d7e22020-01-08 12:57:14 +0000624 * owners, profile owners on the primary user or profile owners of organization-owned managed
625 * profiles on the parent profile. The default value is <code>false</code>.
Fyodor Kupolov0ffbfea2016-01-19 10:07:54 -0800626 * <p>This restriction has no effect on secondary users and managed profiles since only the
Amith Yamasani150514b2015-01-07 16:05:05 -0800627 * primary user can add other users.
Amith Yamasani26af8292014-09-09 09:57:27 -0700628 *
Fyodor Kupolov0ffbfea2016-01-19 10:07:54 -0800629 * <p>Key for user restrictions.
630 * <p>Type: Boolean
Makoto Onuki068c54a2015-10-13 14:34:03 -0700631 * @see DevicePolicyManager#addUserRestriction(ComponentName, String)
632 * @see DevicePolicyManager#clearUserRestriction(ComponentName, String)
Julia Reynoldsd46d0f92014-04-23 15:23:24 -0400633 * @see #getUserRestrictions()
634 */
635 public static final String DISALLOW_ADD_USER = "no_add_user";
636
637 /**
Esteban Talavera6c9116a2016-11-24 16:12:44 +0000638 * Specifies if a user is disallowed from adding managed profiles.
639 * <p>The default value for an unmanaged user is <code>false</code>.
Nicolas Prevot2ea46fe2017-01-05 10:29:34 +0000640 * For users with a device owner set, the default is <code>true</code>.
641 * <p>This restriction has no effect on managed profiles.
Esteban Talavera6c9116a2016-11-24 16:12:44 +0000642 *
643 * <p>Key for user restrictions.
644 * <p>Type: Boolean
645 * @see DevicePolicyManager#addUserRestriction(ComponentName, String)
646 * @see DevicePolicyManager#clearUserRestriction(ComponentName, String)
647 * @see #getUserRestrictions()
Eran Messerice273df2019-12-17 13:14:45 +0000648 * @deprecated As the ability to have a managed profile on a fully-managed device has been
649 * removed from the platform, this restriction will be silently ignored when applied by the
650 * device owner.
Esteban Talavera6c9116a2016-11-24 16:12:44 +0000651 */
Eran Messerice273df2019-12-17 13:14:45 +0000652 @Deprecated
Esteban Talavera6c9116a2016-11-24 16:12:44 +0000653 public static final String DISALLOW_ADD_MANAGED_PROFILE = "no_add_managed_profile";
654
655 /**
Benjamin Millerd41a9fc2017-07-17 17:24:44 +0200656 * Specifies if a user is disallowed from disabling application verification. The default
657 * value is <code>false</code>.
658 *
659 * <p>In Android 8.0 ({@linkplain android.os.Build.VERSION_CODES#O API level 26}) and higher,
660 * this is a global user restriction. If a device owner or profile owner sets this restriction,
661 * the system enforces app verification across all users on the device. Running in earlier
662 * Android versions, this restriction affects only the profile that sets it.
Amith Yamasani26af8292014-09-09 09:57:27 -0700663 *
Fyodor Kupolov0ffbfea2016-01-19 10:07:54 -0800664 * <p>Key for user restrictions.
665 * <p>Type: Boolean
Makoto Onuki068c54a2015-10-13 14:34:03 -0700666 * @see DevicePolicyManager#addUserRestriction(ComponentName, String)
667 * @see DevicePolicyManager#clearUserRestriction(ComponentName, String)
Julia Reynoldsd46d0f92014-04-23 15:23:24 -0400668 * @see #getUserRestrictions()
669 */
670 public static final String ENSURE_VERIFY_APPS = "ensure_verify_apps";
671
672 /**
Amith Yamasani26af8292014-09-09 09:57:27 -0700673 * Specifies if a user is disallowed from configuring cell
Alex Johnston7c3d7e22020-01-08 12:57:14 +0000674 * broadcasts. This can only be set by device owners, profile owners on the primary user or
675 * profile owners of organization-owned managed profiles on the parent profile.
Amith Yamasanic34dc7c2014-09-18 09:42:42 -0700676 * The default value is <code>false</code>.
Fyodor Kupolov0ffbfea2016-01-19 10:07:54 -0800677 * <p>This restriction has no effect on secondary users and managed profiles since only the
Amith Yamasani150514b2015-01-07 16:05:05 -0800678 * primary user can configure cell broadcasts.
Amith Yamasani26af8292014-09-09 09:57:27 -0700679 *
Fyodor Kupolov0ffbfea2016-01-19 10:07:54 -0800680 * <p>Key for user restrictions.
681 * <p>Type: Boolean
Makoto Onuki068c54a2015-10-13 14:34:03 -0700682 * @see DevicePolicyManager#addUserRestriction(ComponentName, String)
683 * @see DevicePolicyManager#clearUserRestriction(ComponentName, String)
Julia Reynoldsd46d0f92014-04-23 15:23:24 -0400684 * @see #getUserRestrictions()
685 */
686 public static final String DISALLOW_CONFIG_CELL_BROADCASTS = "no_config_cell_broadcasts";
687
688 /**
Amith Yamasani26af8292014-09-09 09:57:27 -0700689 * Specifies if a user is disallowed from configuring mobile
Alex Johnston7c3d7e22020-01-08 12:57:14 +0000690 * networks. This can only be set by device owners, profile owners on the primary user or
691 * profile owners of organization-owned managed profiles on the parent profile.
Amith Yamasanic34dc7c2014-09-18 09:42:42 -0700692 * The default value is <code>false</code>.
Fyodor Kupolov0ffbfea2016-01-19 10:07:54 -0800693 * <p>This restriction has no effect on secondary users and managed profiles since only the
Amith Yamasani150514b2015-01-07 16:05:05 -0800694 * primary user can configure mobile networks.
Amith Yamasani26af8292014-09-09 09:57:27 -0700695 *
Fyodor Kupolov0ffbfea2016-01-19 10:07:54 -0800696 * <p>Key for user restrictions.
697 * <p>Type: Boolean
Makoto Onuki068c54a2015-10-13 14:34:03 -0700698 * @see DevicePolicyManager#addUserRestriction(ComponentName, String)
699 * @see DevicePolicyManager#clearUserRestriction(ComponentName, String)
Julia Reynoldsd46d0f92014-04-23 15:23:24 -0400700 * @see #getUserRestrictions()
701 */
702 public static final String DISALLOW_CONFIG_MOBILE_NETWORKS = "no_config_mobile_networks";
703
704 /**
Amith Yamasani26af8292014-09-09 09:57:27 -0700705 * Specifies if a user is disallowed from modifying
Julia Reynoldsc617f812014-07-25 16:32:27 -0400706 * applications in Settings or launchers. The following actions will not be allowed when this
707 * restriction is enabled:
708 * <li>uninstalling apps</li>
709 * <li>disabling apps</li>
710 * <li>clearing app caches</li>
711 * <li>clearing app data</li>
712 * <li>force stopping apps</li>
713 * <li>clearing app defaults</li>
714 * <p>
715 * The default value is <code>false</code>.
Amith Yamasani26af8292014-09-09 09:57:27 -0700716 *
Esteban Talavera8bd7c522017-02-13 12:35:04 +0000717 * <p><strong>Note:</strong> The user will still be able to perform those actions via other
718 * means (such as adb). Third party apps will also be able to uninstall apps via the
719 * {@link android.content.pm.PackageInstaller}. {@link #DISALLOW_UNINSTALL_APPS} or
720 * {@link DevicePolicyManager#setUninstallBlocked(ComponentName, String, boolean)} should be
721 * used to prevent the user from uninstalling apps completely, and
722 * {@link DevicePolicyManager#addPersistentPreferredActivity(ComponentName, IntentFilter, ComponentName)}
723 * to add a default intent handler for a given intent filter.
724 *
Fyodor Kupolov0ffbfea2016-01-19 10:07:54 -0800725 * <p>Key for user restrictions.
726 * <p>Type: Boolean
Makoto Onuki068c54a2015-10-13 14:34:03 -0700727 * @see DevicePolicyManager#addUserRestriction(ComponentName, String)
728 * @see DevicePolicyManager#clearUserRestriction(ComponentName, String)
Julia Reynoldsd46d0f92014-04-23 15:23:24 -0400729 * @see #getUserRestrictions()
730 */
Julia Reynolds36fbc8d2014-06-18 09:26:30 -0400731 public static final String DISALLOW_APPS_CONTROL = "no_control_apps";
Julia Reynoldsd46d0f92014-04-23 15:23:24 -0400732
Emily Bernier394a6cd2014-05-07 12:49:20 -0400733 /**
Alex Johnston9722abe2020-01-28 16:15:55 +0000734 * Specifies if a user is disallowed from mounting physical external media.
735 * <p>
736 * This restriction can only be set by the device owner, the profile owner on the primary user
737 * or the profile owner of an organization-owned managed profile on the parent profile.
738 * <p>
739 * The default value is <code>false</code>.
Amith Yamasani26af8292014-09-09 09:57:27 -0700740 *
Fyodor Kupolov0ffbfea2016-01-19 10:07:54 -0800741 * <p>Key for user restrictions.
742 * <p>Type: Boolean
Makoto Onuki068c54a2015-10-13 14:34:03 -0700743 * @see DevicePolicyManager#addUserRestriction(ComponentName, String)
744 * @see DevicePolicyManager#clearUserRestriction(ComponentName, String)
Emily Bernier394a6cd2014-05-07 12:49:20 -0400745 * @see #getUserRestrictions()
746 */
747 public static final String DISALLOW_MOUNT_PHYSICAL_MEDIA = "no_physical_media";
748
749 /**
Pavel Grafovce3e1a32017-04-21 14:48:21 +0100750 * Specifies if a user is disallowed from adjusting microphone volume. If set, the microphone
Alex Johnston9722abe2020-01-28 16:15:55 +0000751 * will be muted.
752 * <p>
753 * The default value is <code>false</code>.
754 * <p>
755 * Device owner and profile owner can set this restriction, although the restriction has no
756 * effect in a managed profile. When it is set by the profile owner of an organization-owned
757 * managed profile on the parent profile, it will disallow the personal user from adjusting the
758 * microphone volume.
Amith Yamasani26af8292014-09-09 09:57:27 -0700759 *
Fyodor Kupolov0ffbfea2016-01-19 10:07:54 -0800760 * <p>Key for user restrictions.
761 * <p>Type: Boolean
Makoto Onuki068c54a2015-10-13 14:34:03 -0700762 * @see DevicePolicyManager#addUserRestriction(ComponentName, String)
763 * @see DevicePolicyManager#clearUserRestriction(ComponentName, String)
Emily Bernier394a6cd2014-05-07 12:49:20 -0400764 * @see #getUserRestrictions()
765 */
766 public static final String DISALLOW_UNMUTE_MICROPHONE = "no_unmute_microphone";
767
768 /**
Pavel Grafovce3e1a32017-04-21 14:48:21 +0100769 * Specifies if a user is disallowed from adjusting the master volume. If set, the master volume
Wen ZHANG61ed0dc2017-08-23 14:27:02 +0100770 * will be muted. This can be set by device owners from API 21 and profile owners from API 24.
771 * The default value is <code>false</code>.
772 *
773 * <p>When the restriction is set by profile owners, then it only applies to relevant
774 * profiles.
Amith Yamasani26af8292014-09-09 09:57:27 -0700775 *
Pavel Grafovce3e1a32017-04-21 14:48:21 +0100776 * <p>This restriction has no effect on managed profiles.
Fyodor Kupolov0ffbfea2016-01-19 10:07:54 -0800777 * <p>Key for user restrictions.
778 * <p>Type: Boolean
Makoto Onuki068c54a2015-10-13 14:34:03 -0700779 * @see DevicePolicyManager#addUserRestriction(ComponentName, String)
780 * @see DevicePolicyManager#clearUserRestriction(ComponentName, String)
Emily Bernier394a6cd2014-05-07 12:49:20 -0400781 * @see #getUserRestrictions()
782 */
783 public static final String DISALLOW_ADJUST_VOLUME = "no_adjust_volume";
784
Amith Yamasani9f6c25f2014-05-16 14:49:15 -0700785 /**
Alex Johnston9722abe2020-01-28 16:15:55 +0000786 * Specifies that the user is not allowed to make outgoing phone calls. Emergency calls are
787 * still permitted.
788 * <p>
Amith Yamasani9f6c25f2014-05-16 14:49:15 -0700789 * The default value is <code>false</code>.
Alex Johnston9722abe2020-01-28 16:15:55 +0000790 * <p>
791 * Device owner and profile owner can set this restriction, although the restriction has no
792 * effect in a managed profile. When it is set by the profile owner of an organization-owned
793 * managed profile on the parent profile, it will disallow the personal user from making
794 * outgoing phone calls.
Amith Yamasani26af8292014-09-09 09:57:27 -0700795 *
Fyodor Kupolov0ffbfea2016-01-19 10:07:54 -0800796 * <p>Key for user restrictions.
797 * <p>Type: Boolean
Makoto Onuki068c54a2015-10-13 14:34:03 -0700798 * @see DevicePolicyManager#addUserRestriction(ComponentName, String)
799 * @see DevicePolicyManager#clearUserRestriction(ComponentName, String)
Amith Yamasani9f6c25f2014-05-16 14:49:15 -0700800 * @see #getUserRestrictions()
801 */
Amith Yamasani390989d2014-07-17 10:52:03 -0700802 public static final String DISALLOW_OUTGOING_CALLS = "no_outgoing_calls";
803
804 /**
Amith Yamasani26af8292014-09-09 09:57:27 -0700805 * Specifies that the user is not allowed to send or receive
Amith Yamasanic34dc7c2014-09-18 09:42:42 -0700806 * SMS messages. The default value is <code>false</code>.
Alex Johnston7c3d7e22020-01-08 12:57:14 +0000807 * <p>
808 * Device owner and profile owner can set this restriction. When it is set by the
809 * profile owner of an organization-owned managed profile on the parent profile,
810 * it will disable SMS in the personal profile.
Amith Yamasani26af8292014-09-09 09:57:27 -0700811 *
Fyodor Kupolov0ffbfea2016-01-19 10:07:54 -0800812 * <p>Key for user restrictions.
813 * <p>Type: Boolean
Makoto Onuki068c54a2015-10-13 14:34:03 -0700814 * @see DevicePolicyManager#addUserRestriction(ComponentName, String)
815 * @see DevicePolicyManager#clearUserRestriction(ComponentName, String)
Amith Yamasani390989d2014-07-17 10:52:03 -0700816 * @see #getUserRestrictions()
817 */
818 public static final String DISALLOW_SMS = "no_sms";
Amith Yamasani9f6c25f2014-05-16 14:49:15 -0700819
Jason Monk1c7c3192014-06-26 12:52:18 -0400820 /**
Jeff Sharkey2cc03e52015-03-20 11:24:04 -0700821 * Specifies if the user is not allowed to have fun. In some cases, the
822 * device owner may wish to prevent the user from experiencing amusement or
823 * joy while using the device. The default value is <code>false</code>.
824 *
Fyodor Kupolov0ffbfea2016-01-19 10:07:54 -0800825 * <p>Key for user restrictions.
826 * <p>Type: Boolean
Makoto Onuki068c54a2015-10-13 14:34:03 -0700827 * @see DevicePolicyManager#addUserRestriction(ComponentName, String)
828 * @see DevicePolicyManager#clearUserRestriction(ComponentName, String)
Jeff Sharkey2cc03e52015-03-20 11:24:04 -0700829 * @see #getUserRestrictions()
830 */
831 public static final String DISALLOW_FUN = "no_fun";
832
833 /**
Amith Yamasani26af8292014-09-09 09:57:27 -0700834 * Specifies that windows besides app windows should not be
Jason Monk1c7c3192014-06-26 12:52:18 -0400835 * created. This will block the creation of the following types of windows.
836 * <li>{@link LayoutParams#TYPE_TOAST}</li>
837 * <li>{@link LayoutParams#TYPE_PHONE}</li>
838 * <li>{@link LayoutParams#TYPE_PRIORITY_PHONE}</li>
839 * <li>{@link LayoutParams#TYPE_SYSTEM_ALERT}</li>
840 * <li>{@link LayoutParams#TYPE_SYSTEM_ERROR}</li>
841 * <li>{@link LayoutParams#TYPE_SYSTEM_OVERLAY}</li>
Wale Ogunwale5cd907d2017-01-26 14:14:08 -0800842 * <li>{@link LayoutParams#TYPE_APPLICATION_OVERLAY}</li>
Jason Monk1c7c3192014-06-26 12:52:18 -0400843 *
Amith Yamasanic34dc7c2014-09-18 09:42:42 -0700844 * <p>This can only be set by device owners and profile owners on the primary user.
845 * The default value is <code>false</code>.
Amith Yamasani26af8292014-09-09 09:57:27 -0700846 *
Fyodor Kupolov0ffbfea2016-01-19 10:07:54 -0800847 * <p>Key for user restrictions.
848 * <p>Type: Boolean
Makoto Onuki068c54a2015-10-13 14:34:03 -0700849 * @see DevicePolicyManager#addUserRestriction(ComponentName, String)
850 * @see DevicePolicyManager#clearUserRestriction(ComponentName, String)
Jason Monk1c7c3192014-06-26 12:52:18 -0400851 * @see #getUserRestrictions()
852 */
853 public static final String DISALLOW_CREATE_WINDOWS = "no_create_windows";
854
Nicolas Prevotf1939902014-06-25 09:29:02 +0100855 /**
Charles He22ff6f9d2017-10-05 21:28:55 +0100856 * Specifies that system error dialogs for crashed or unresponsive apps should not be shown.
857 * In this case, the system will force-stop the app as if the user chooses the "close app"
Benjamin Franz0c86fe12018-02-28 09:49:17 +0000858 * option on the UI. A feedback report isn't collected as there is no way for the user to
859 * provide explicit consent. The default value is <code>false</code>.
Charles He22ff6f9d2017-10-05 21:28:55 +0100860 *
Benjamin Franz0c86fe12018-02-28 09:49:17 +0000861 * <p>When this user restriction is set by device owners, it's applied to all users. When set by
862 * the profile owner of the primary user or a secondary user, the restriction affects only the
863 * calling user. This user restriction has no effect on managed profiles.
Charles He22ff6f9d2017-10-05 21:28:55 +0100864 *
Charles He22ff6f9d2017-10-05 21:28:55 +0100865 * <p>Key for user restrictions.
866 * <p>Type: Boolean
867 * @see DevicePolicyManager#addUserRestriction(ComponentName, String)
868 * @see DevicePolicyManager#clearUserRestriction(ComponentName, String)
869 * @see #getUserRestrictions()
870 */
871 public static final String DISALLOW_SYSTEM_ERROR_DIALOGS = "no_system_error_dialogs";
872
873 /**
Benjamin Miller8cc730d2019-01-18 19:03:40 +0000874 * Specifies if the clipboard contents can be exported by pasting the data into other users or
875 * profiles. This restriction doesn't prevent import, such as someone pasting clipboard data
876 * from other profiles or users. The default value is {@code false}.
877 *
878 * <p><strong>Note</strong>: Because it's possible to extract data from screenshots using
879 * optical character recognition (OCR), we strongly recommend combining this user restriction
880 * with {@link DevicePolicyManager#setScreenCaptureDisabled(ComponentName, boolean)}.
Amith Yamasani26af8292014-09-09 09:57:27 -0700881 *
Fyodor Kupolov0ffbfea2016-01-19 10:07:54 -0800882 * <p>Key for user restrictions.
883 * <p>Type: Boolean
Makoto Onuki068c54a2015-10-13 14:34:03 -0700884 * @see DevicePolicyManager#addUserRestriction(ComponentName, String)
885 * @see DevicePolicyManager#clearUserRestriction(ComponentName, String)
Nicolas Prevotf1939902014-06-25 09:29:02 +0100886 * @see #getUserRestrictions()
887 */
888 public static final String DISALLOW_CROSS_PROFILE_COPY_PASTE = "no_cross_profile_copy_paste";
889
Amith Yamasani26af8292014-09-09 09:57:27 -0700890 /**
891 * Specifies if the user is not allowed to use NFC to beam out data from apps.
892 * The default value is <code>false</code>.
893 *
Fyodor Kupolov0ffbfea2016-01-19 10:07:54 -0800894 * <p>Key for user restrictions.
895 * <p>Type: Boolean
Makoto Onuki068c54a2015-10-13 14:34:03 -0700896 * @see DevicePolicyManager#addUserRestriction(ComponentName, String)
897 * @see DevicePolicyManager#clearUserRestriction(ComponentName, String)
Amith Yamasani26af8292014-09-09 09:57:27 -0700898 * @see #getUserRestrictions()
899 */
900 public static final String DISALLOW_OUTGOING_BEAM = "no_outgoing_beam";
901
Sander Alewijnse53d63dc2014-11-07 21:43:00 +0000902 /**
Oleksandr Peletskyif2519812016-01-26 20:16:06 +0100903 * Hidden user restriction to disallow access to wallpaper manager APIs. This restriction
904 * generally means that wallpapers are not supported for the particular user. This user
905 * restriction is always set for managed profiles, because such profiles don't have wallpapers.
Benjamin Franzf3ece362015-02-11 10:51:10 +0000906 * @hide
Oleksandr Peletskyif2519812016-01-26 20:16:06 +0100907 * @see #DISALLOW_SET_WALLPAPER
Makoto Onuki068c54a2015-10-13 14:34:03 -0700908 * @see DevicePolicyManager#addUserRestriction(ComponentName, String)
909 * @see DevicePolicyManager#clearUserRestriction(ComponentName, String)
Benjamin Franzf3ece362015-02-11 10:51:10 +0000910 * @see #getUserRestrictions()
911 */
912 public static final String DISALLOW_WALLPAPER = "no_wallpaper";
913
914 /**
Oleksandr Peletskyif2519812016-01-26 20:16:06 +0100915 * User restriction to disallow setting a wallpaper. Profile owner and device owner
916 * are able to set wallpaper regardless of this restriction.
917 * The default value is <code>false</code>.
918 *
919 * <p>Key for user restrictions.
920 * <p>Type: Boolean
921 * @see DevicePolicyManager#addUserRestriction(ComponentName, String)
922 * @see DevicePolicyManager#clearUserRestriction(ComponentName, String)
923 * @see #getUserRestrictions()
924 */
925 public static final String DISALLOW_SET_WALLPAPER = "no_set_wallpaper";
926
927 /**
Benjamin Franzbff46ba2015-03-05 18:33:51 +0000928 * Specifies if the user is not allowed to reboot the device into safe boot mode.
Alex Johnston7c3d7e22020-01-08 12:57:14 +0000929 * This can only be set by device owners, profile owners on the primary user or profile
930 * owners of organization-owned managed profiles on the parent profile.
Benjamin Franzbff46ba2015-03-05 18:33:51 +0000931 * The default value is <code>false</code>.
932 *
Fyodor Kupolov0ffbfea2016-01-19 10:07:54 -0800933 * <p>Key for user restrictions.
934 * <p>Type: Boolean
Makoto Onuki068c54a2015-10-13 14:34:03 -0700935 * @see DevicePolicyManager#addUserRestriction(ComponentName, String)
936 * @see DevicePolicyManager#clearUserRestriction(ComponentName, String)
Benjamin Franzbff46ba2015-03-05 18:33:51 +0000937 * @see #getUserRestrictions()
938 */
939 public static final String DISALLOW_SAFE_BOOT = "no_safe_boot";
940
941 /**
Fyodor Kupolovb5013302015-04-17 17:59:14 -0700942 * Specifies if a user is not allowed to record audio. This restriction is always enabled for
943 * background users. The default value is <code>false</code>.
944 *
Makoto Onuki068c54a2015-10-13 14:34:03 -0700945 * @see DevicePolicyManager#addUserRestriction(ComponentName, String)
946 * @see DevicePolicyManager#clearUserRestriction(ComponentName, String)
Fyodor Kupolovb5013302015-04-17 17:59:14 -0700947 * @see #getUserRestrictions()
948 * @hide
949 */
Andrei Onea24ec3212019-03-15 17:35:05 +0000950 @UnsupportedAppUsage
Fyodor Kupolovb5013302015-04-17 17:59:14 -0700951 public static final String DISALLOW_RECORD_AUDIO = "no_record_audio";
952
953 /**
Fyodor Kupolov9cbfc9e2015-10-07 15:52:33 -0700954 * Specifies if a user is not allowed to run in the background and should be stopped during
955 * user switch. The default value is <code>false</code>.
956 *
957 * <p>This restriction can be set by device owners and profile owners.
958 *
959 * @see DevicePolicyManager#addUserRestriction(ComponentName, String)
960 * @see DevicePolicyManager#clearUserRestriction(ComponentName, String)
961 * @see #getUserRestrictions()
962 * @hide
963 */
Robin Lee6cfb7a92017-12-18 15:14:46 +0100964 @SystemApi
Fyodor Kupolov9cbfc9e2015-10-07 15:52:33 -0700965 public static final String DISALLOW_RUN_IN_BACKGROUND = "no_run_in_background";
966
967 /**
Makoto Onuki759a7632015-10-28 16:43:10 -0700968 * Specifies if a user is not allowed to use the camera.
Alex Johnston7c3d7e22020-01-08 12:57:14 +0000969 * <p>
970 * Device owner and profile owner can set this restriction. When the restriction is set by
971 * the device owner or the profile owner of an organization-owned managed profile on the
972 * parent profile, it is applied globally.
973 * <p>
974 * The default value is <code>false</code>.
Makoto Onuki759a7632015-10-28 16:43:10 -0700975 *
976 * @see DevicePolicyManager#addUserRestriction(ComponentName, String)
977 * @see DevicePolicyManager#clearUserRestriction(ComponentName, String)
978 * @see #getUserRestrictions()
979 * @hide
980 */
981 public static final String DISALLOW_CAMERA = "no_camera";
982
983 /**
Tony Makc1205112016-07-22 16:02:59 +0100984 * Specifies if a user is not allowed to unmute the device's master volume.
985 *
986 * @see DevicePolicyManager#setMasterVolumeMuted(ComponentName, boolean)
987 * @see DevicePolicyManager#clearUserRestriction(ComponentName, String)
988 * @see #getUserRestrictions()
989 * @hide
990 */
Esteban Talavera492b4722017-02-13 14:59:45 +0000991 public static final String DISALLOW_UNMUTE_DEVICE = "disallow_unmute_device";
Tony Makc1205112016-07-22 16:02:59 +0100992
993 /**
Mahaver Chopradea471e2015-12-17 11:02:37 +0000994 * Specifies if a user is not allowed to use cellular data when roaming. This can only be set by
Alex Johnston7c3d7e22020-01-08 12:57:14 +0000995 * device owners or profile owners of organization-owned managed profiles on the parent profile.
996 * The default value is <code>false</code>.
Mahaver Chopradea471e2015-12-17 11:02:37 +0000997 *
998 * @see DevicePolicyManager#addUserRestriction(ComponentName, String)
999 * @see DevicePolicyManager#clearUserRestriction(ComponentName, String)
1000 * @see #getUserRestrictions()
1001 */
1002 public static final String DISALLOW_DATA_ROAMING = "no_data_roaming";
1003
1004 /**
Oleksandr Peletskyi7f1f1df2016-01-18 15:40:21 +01001005 * Specifies if a user is not allowed to change their icon. Device owner and profile owner
1006 * can set this restriction. When it is set by device owner, only the target user will be
1007 * affected. The default value is <code>false</code>.
1008 *
1009 * <p>Key for user restrictions.
Oleksandr Peletskyi7f1f1df2016-01-18 15:40:21 +01001010 * <p>Type: Boolean
Oleksandr Peletskyi7f1f1df2016-01-18 15:40:21 +01001011 * @see DevicePolicyManager#addUserRestriction(ComponentName, String)
1012 * @see DevicePolicyManager#clearUserRestriction(ComponentName, String)
1013 * @see #getUserRestrictions()
1014 */
1015 public static final String DISALLOW_SET_USER_ICON = "no_set_user_icon";
1016
1017 /**
Mahaver Chopra3d9805d2016-07-07 16:25:05 +01001018 * Specifies if a user is not allowed to enable the oem unlock setting. The default value is
Esteban Talaverac48b20f2016-08-11 11:23:40 +01001019 * <code>false</code>. Setting this restriction has no effect if the bootloader is already
1020 * unlocked.
Mahaver Chopra3d9805d2016-07-07 16:25:05 +01001021 *
Lenka Trochtova12b04962016-11-29 21:00:12 +01001022 * <p>Not for use by third-party applications.
1023 *
Mahaver Chopra3d9805d2016-07-07 16:25:05 +01001024 * @see DevicePolicyManager#addUserRestriction(ComponentName, String)
1025 * @see DevicePolicyManager#clearUserRestriction(ComponentName, String)
1026 * @see #getUserRestrictions()
Andrew Scull3b8b46f2017-02-13 18:12:15 +00001027 * @deprecated use {@link OemLockManager#setOemUnlockAllowedByCarrier(boolean, byte[])} instead.
Mahaver Chopra3d9805d2016-07-07 16:25:05 +01001028 * @hide
1029 */
Andrew Scull3b8b46f2017-02-13 18:12:15 +00001030 @Deprecated
Lenka Trochtova12b04962016-11-29 21:00:12 +01001031 @SystemApi
Mahaver Chopra3d9805d2016-07-07 16:25:05 +01001032 public static final String DISALLOW_OEM_UNLOCK = "no_oem_unlock";
1033
1034 /**
Pavel Grafovc4f87e92017-10-26 16:34:25 +01001035 * Specifies that the managed profile is not allowed to have unified lock screen challenge with
1036 * the primary user.
1037 *
1038 * <p><strong>Note:</strong> Setting this restriction alone doesn't automatically set a
1039 * separate challenge. Profile owner can ask the user to set a new password using
1040 * {@link DevicePolicyManager#ACTION_SET_NEW_PASSWORD} and verify it using
1041 * {@link DevicePolicyManager#isUsingUnifiedPassword(ComponentName)}.
1042 *
1043 * <p>Can be set by profile owners. It only has effect on managed profiles when set by managed
1044 * profile owner. Has no effect on non-managed profiles or users.
1045 * <p>Key for user restrictions.
1046 * <p>Type: Boolean
1047 * @see DevicePolicyManager#addUserRestriction(ComponentName, String)
1048 * @see DevicePolicyManager#clearUserRestriction(ComponentName, String)
1049 * @see #getUserRestrictions()
1050 */
1051 public static final String DISALLOW_UNIFIED_PASSWORD = "no_unified_password";
1052
1053 /**
Nicolas Prevotf0029c12015-06-25 14:25:41 -07001054 * Allows apps in the parent profile to handle web links from the managed profile.
1055 *
Nicolas Prevot9edbda12015-06-17 11:09:48 -07001056 * This user restriction has an effect only in a managed profile.
1057 * If set:
1058 * Intent filters of activities in the parent profile with action
1059 * {@link android.content.Intent#ACTION_VIEW},
1060 * category {@link android.content.Intent#CATEGORY_BROWSABLE}, scheme http or https, and which
1061 * define a host can handle intents from the managed profile.
1062 * The default value is <code>false</code>.
1063 *
Fyodor Kupolov0ffbfea2016-01-19 10:07:54 -08001064 * <p>Key for user restrictions.
1065 * <p>Type: Boolean
Makoto Onuki068c54a2015-10-13 14:34:03 -07001066 * @see DevicePolicyManager#addUserRestriction(ComponentName, String)
1067 * @see DevicePolicyManager#clearUserRestriction(ComponentName, String)
Nicolas Prevot9edbda12015-06-17 11:09:48 -07001068 * @see #getUserRestrictions()
1069 */
Nicolas Prevotf0029c12015-06-25 14:25:41 -07001070 public static final String ALLOW_PARENT_PROFILE_APP_LINKING
1071 = "allow_parent_profile_app_linking";
Nicolas Prevot9edbda12015-06-17 11:09:48 -07001072
1073 /**
Felipe Leme24d58932017-03-21 14:13:58 -07001074 * Specifies if a user is not allowed to use Autofill Services.
1075 *
1076 * <p>Device owner and profile owner can set this restriction. When it is set by device owner,
1077 * only the target user will be affected.
1078 *
1079 * <p>The default value is <code>false</code>.
1080 *
1081 * @see DevicePolicyManager#addUserRestriction(ComponentName, String)
1082 * @see DevicePolicyManager#clearUserRestriction(ComponentName, String)
1083 * @see #getUserRestrictions()
1084 */
1085 public static final String DISALLOW_AUTOFILL = "no_autofill";
1086
1087 /**
Felipe Leme1dfa9a02018-10-17 17:24:37 -07001088 * Specifies if the contents of a user's screen is not allowed to be captured for artificial
1089 * intelligence purposes.
1090 *
Alex Johnston7c3d7e22020-01-08 12:57:14 +00001091 * <p>Device owner and profile owner can set this restriction. When it is set by the
1092 * device owner or the profile owner of an organization-owned managed profile on the parent
1093 * profile, only the target user will be affected.
Felipe Leme1dfa9a02018-10-17 17:24:37 -07001094 *
1095 * <p>The default value is <code>false</code>.
1096 *
1097 * @see DevicePolicyManager#addUserRestriction(ComponentName, String)
1098 * @see DevicePolicyManager#clearUserRestriction(ComponentName, String)
1099 * @see #getUserRestrictions()
1100 */
Felipe Leme749b8892018-12-03 16:30:30 -08001101 public static final String DISALLOW_CONTENT_CAPTURE = "no_content_capture";
Felipe Leme1dfa9a02018-10-17 17:24:37 -07001102
1103 /**
Zak Cohen3e12ac72019-03-05 16:50:39 -08001104 * Specifies if the current user is able to receive content suggestions for selections based on
1105 * the contents of their screen.
1106 *
Alex Johnston7c3d7e22020-01-08 12:57:14 +00001107 * <p>Device owner and profile owner can set this restriction. When it is set by the
1108 * device owner or the profile owner of an organization-owned managed profile on the parent
1109 * profile, only the target user will be affected.
Zak Cohen3e12ac72019-03-05 16:50:39 -08001110 *
1111 * <p>The default value is <code>false</code>.
1112 *
1113 * @see DevicePolicyManager#addUserRestriction(ComponentName, String)
1114 * @see DevicePolicyManager#clearUserRestriction(ComponentName, String)
1115 * @see #getUserRestrictions()
1116 */
1117 public static final String DISALLOW_CONTENT_SUGGESTIONS = "no_content_suggestions";
1118
1119 /**
Benjamin Franzff66fa92017-08-10 10:39:44 +01001120 * Specifies if user switching is blocked on the current user.
1121 *
1122 * <p> This restriction can only be set by the device owner, it will be applied to all users.
Alex Chau71ada442018-02-22 12:56:59 +08001123 * Device owner can still switch user via
1124 * {@link DevicePolicyManager#switchUser(ComponentName, UserHandle)} when this restriction is
1125 * set.
Benjamin Franzff66fa92017-08-10 10:39:44 +01001126 *
1127 * <p>The default value is <code>false</code>.
1128 *
1129 * @see DevicePolicyManager#addUserRestriction(ComponentName, String)
1130 * @see DevicePolicyManager#clearUserRestriction(ComponentName, String)
1131 * @see #getUserRestrictions()
1132 */
1133 public static final String DISALLOW_USER_SWITCH = "no_user_switch";
1134
1135 /**
Rubin Xucc391c22018-01-02 20:37:35 +00001136 * Specifies whether the user can share file / picture / data from the primary user into the
1137 * managed profile, either by sending them from the primary side, or by picking up data within
1138 * an app in the managed profile.
1139 * <p>
1140 * When a managed profile is created, the system allows the user to send data from the primary
1141 * side to the profile by setting up certain default cross profile intent filters. If
1142 * this is undesired, this restriction can be set to disallow it. Note that this restriction
1143 * will not block any sharing allowed by explicit
1144 * {@link DevicePolicyManager#addCrossProfileIntentFilter} calls by the profile owner.
1145 * <p>
1146 * This restriction is only meaningful when set by profile owner. When it is set by device
1147 * owner, it does not have any effect.
1148 * <p>
1149 * The default value is <code>false</code>.
1150 *
1151 * @see DevicePolicyManager#addUserRestriction(ComponentName, String)
1152 * @see DevicePolicyManager#clearUserRestriction(ComponentName, String)
1153 * @see #getUserRestrictions()
1154 */
1155 public static final String DISALLOW_SHARE_INTO_MANAGED_PROFILE = "no_sharing_into_profile";
Vladislav Kuzkokov622b9f92018-01-25 16:33:05 +01001156
1157 /**
1158 * Specifies whether the user is allowed to print.
1159 *
1160 * This restriction can be set by device or profile owner.
1161 *
1162 * The default value is {@code false}.
1163 *
1164 * @see DevicePolicyManager#addUserRestriction(ComponentName, String)
1165 * @see DevicePolicyManager#clearUserRestriction(ComponentName, String)
1166 * @see #getUserRestrictions()
1167 */
1168 public static final String DISALLOW_PRINTING = "no_printing";
1169
Rubin Xucc391c22018-01-02 20:37:35 +00001170 /**
Eran Messeri09b122da2018-10-05 15:33:53 +01001171 * Specifies whether the user is allowed to modify private DNS settings.
1172 *
1173 * <p>The default value is <code>false</code>.
1174 *
Alex Johnston7c3d7e22020-01-08 12:57:14 +00001175 * <p>This user restriction can only be applied by the device owner or the profile owner
1176 * of an organization-owned managed profile on the parent profile.
1177 *
Eran Messeri09b122da2018-10-05 15:33:53 +01001178 * <p>Key for user restrictions.
1179 * <p>Type: Boolean
1180 * @see DevicePolicyManager#addUserRestriction(ComponentName, String)
1181 * @see DevicePolicyManager#clearUserRestriction(ComponentName, String)
1182 * @see #getUserRestrictions()
1183 */
1184 public static final String DISALLOW_CONFIG_PRIVATE_DNS =
1185 "disallow_config_private_dns";
1186
1187 /**
Sander Alewijnse53d63dc2014-11-07 21:43:00 +00001188 * Application restriction key that is used to indicate the pending arrival
1189 * of real restrictions for the app.
1190 *
1191 * <p>
1192 * Applications that support restrictions should check for the presence of this key.
1193 * A <code>true</code> value indicates that restrictions may be applied in the near
1194 * future but are not available yet. It is the responsibility of any
1195 * management application that sets this flag to update it when the final
1196 * restrictions are enforced.
1197 *
Fyodor Kupolov0ffbfea2016-01-19 10:07:54 -08001198 * <p>Key for application restrictions.
1199 * <p>Type: Boolean
Nicolas Prevotb14ed952015-01-13 14:23:53 +00001200 * @see android.app.admin.DevicePolicyManager#setApplicationRestrictions(
1201 * android.content.ComponentName, String, Bundle)
1202 * @see android.app.admin.DevicePolicyManager#getApplicationRestrictions(
1203 * android.content.ComponentName, String)
Sander Alewijnse53d63dc2014-11-07 21:43:00 +00001204 */
1205 public static final String KEY_RESTRICTIONS_PENDING = "restrictions_pending";
1206
Roshan Pius3eac0b92019-12-05 09:35:36 -08001207 /**
1208 * List of key values that can be passed into the various user restriction related methods
1209 * in {@link UserManager} & {@link DevicePolicyManager}.
1210 * Note: This is slightly different from the real set of user restrictions listed in {@link
1211 * com.android.server.pm.UserRestrictionsUtils#USER_RESTRICTIONS}. For example
1212 * {@link #KEY_RESTRICTIONS_PENDING} is not a real user restriction, but is a a legitimate
1213 * value that can be passed into {@link #hasUserRestriction(String)}.
1214 * @hide
1215 */
1216 @StringDef(value = {
1217 DISALLOW_MODIFY_ACCOUNTS,
1218 DISALLOW_CONFIG_WIFI,
1219 DISALLOW_CONFIG_LOCALE,
1220 DISALLOW_INSTALL_APPS,
1221 DISALLOW_UNINSTALL_APPS,
1222 DISALLOW_SHARE_LOCATION,
1223 DISALLOW_AIRPLANE_MODE,
1224 DISALLOW_CONFIG_BRIGHTNESS,
1225 DISALLOW_AMBIENT_DISPLAY,
1226 DISALLOW_CONFIG_SCREEN_TIMEOUT,
1227 DISALLOW_INSTALL_UNKNOWN_SOURCES,
1228 DISALLOW_INSTALL_UNKNOWN_SOURCES_GLOBALLY,
1229 DISALLOW_CONFIG_BLUETOOTH,
1230 DISALLOW_BLUETOOTH,
1231 DISALLOW_BLUETOOTH_SHARING,
1232 DISALLOW_USB_FILE_TRANSFER,
1233 DISALLOW_CONFIG_CREDENTIALS,
1234 DISALLOW_REMOVE_USER,
1235 DISALLOW_REMOVE_MANAGED_PROFILE,
1236 DISALLOW_DEBUGGING_FEATURES,
1237 DISALLOW_CONFIG_VPN,
1238 DISALLOW_CONFIG_LOCATION,
1239 DISALLOW_CONFIG_DATE_TIME,
1240 DISALLOW_CONFIG_TETHERING,
1241 DISALLOW_NETWORK_RESET,
1242 DISALLOW_FACTORY_RESET,
1243 DISALLOW_ADD_USER,
1244 DISALLOW_ADD_MANAGED_PROFILE,
1245 ENSURE_VERIFY_APPS,
1246 DISALLOW_CONFIG_CELL_BROADCASTS,
1247 DISALLOW_CONFIG_MOBILE_NETWORKS,
1248 DISALLOW_APPS_CONTROL,
1249 DISALLOW_MOUNT_PHYSICAL_MEDIA,
1250 DISALLOW_UNMUTE_MICROPHONE,
1251 DISALLOW_ADJUST_VOLUME,
1252 DISALLOW_OUTGOING_CALLS,
1253 DISALLOW_SMS,
1254 DISALLOW_FUN,
1255 DISALLOW_CREATE_WINDOWS,
1256 DISALLOW_SYSTEM_ERROR_DIALOGS,
1257 DISALLOW_CROSS_PROFILE_COPY_PASTE,
1258 DISALLOW_OUTGOING_BEAM,
1259 DISALLOW_WALLPAPER,
1260 DISALLOW_SET_WALLPAPER,
1261 DISALLOW_SAFE_BOOT,
1262 DISALLOW_RECORD_AUDIO,
1263 DISALLOW_RUN_IN_BACKGROUND,
1264 DISALLOW_CAMERA,
1265 DISALLOW_UNMUTE_DEVICE,
1266 DISALLOW_DATA_ROAMING,
1267 DISALLOW_SET_USER_ICON,
1268 DISALLOW_OEM_UNLOCK,
1269 DISALLOW_UNIFIED_PASSWORD,
1270 ALLOW_PARENT_PROFILE_APP_LINKING,
1271 DISALLOW_AUTOFILL,
1272 DISALLOW_CONTENT_CAPTURE,
1273 DISALLOW_CONTENT_SUGGESTIONS,
1274 DISALLOW_USER_SWITCH,
1275 DISALLOW_SHARE_INTO_MANAGED_PROFILE,
1276 DISALLOW_PRINTING,
1277 DISALLOW_CONFIG_PRIVATE_DNS,
1278 KEY_RESTRICTIONS_PENDING,
1279 })
1280 @Retention(RetentionPolicy.SOURCE)
1281 public @interface UserRestrictionKey {}
1282
Amith Yamasani12747872015-12-07 14:19:49 -08001283 private static final String ACTION_CREATE_USER = "android.os.action.CREATE_USER";
1284
1285 /**
1286 * Extra containing a name for the user being created. Optional parameter passed to
1287 * ACTION_CREATE_USER activity.
1288 * @hide
1289 */
1290 public static final String EXTRA_USER_NAME = "android.os.extra.USER_NAME";
1291
1292 /**
1293 * Extra containing account name for the user being created. Optional parameter passed to
1294 * ACTION_CREATE_USER activity.
1295 * @hide
1296 */
1297 public static final String EXTRA_USER_ACCOUNT_NAME = "android.os.extra.USER_ACCOUNT_NAME";
1298
1299 /**
1300 * Extra containing account type for the user being created. Optional parameter passed to
1301 * ACTION_CREATE_USER activity.
1302 * @hide
1303 */
1304 public static final String EXTRA_USER_ACCOUNT_TYPE = "android.os.extra.USER_ACCOUNT_TYPE";
1305
1306 /**
1307 * Extra containing account-specific data for the user being created. Optional parameter passed
1308 * to ACTION_CREATE_USER activity.
1309 * @hide
1310 */
1311 public static final String EXTRA_USER_ACCOUNT_OPTIONS
1312 = "android.os.extra.USER_ACCOUNT_OPTIONS";
1313
Amith Yamasani655d0e22013-06-12 14:19:10 -07001314 /** @hide */
1315 public static final int PIN_VERIFICATION_FAILED_INCORRECT = -3;
1316 /** @hide */
1317 public static final int PIN_VERIFICATION_FAILED_NOT_SET = -2;
1318 /** @hide */
1319 public static final int PIN_VERIFICATION_SUCCESS = -1;
1320
Amith Yamasani37ed8d12016-01-27 14:40:16 -08001321 /**
Makoto Onukie72f81b2017-03-16 14:08:19 -07001322 * Sent when user restrictions have changed.
1323 *
1324 * @hide
1325 */
1326 @SystemApi
1327 @TestApi // To allow seeing it from CTS.
1328 public static final String ACTION_USER_RESTRICTIONS_CHANGED =
1329 "android.os.action.USER_RESTRICTIONS_CHANGED";
1330
1331 /**
Amith Yamasani37ed8d12016-01-27 14:40:16 -08001332 * Error result indicating that this user is not allowed to add other users on this device.
1333 * This is a result code returned from the activity created by the intent
1334 * {@link #createUserCreationIntent(String, String, String, PersistableBundle)}.
1335 */
1336 public static final int USER_CREATION_FAILED_NOT_PERMITTED = Activity.RESULT_FIRST_USER;
1337
1338 /**
1339 * Error result indicating that no more users can be created on this device.
1340 * This is a result code returned from the activity created by the intent
1341 * {@link #createUserCreationIntent(String, String, String, PersistableBundle)}.
1342 */
1343 public static final int USER_CREATION_FAILED_NO_MORE_USERS = Activity.RESULT_FIRST_USER + 1;
1344
Alex Chau89386ba2018-02-13 11:09:17 +08001345 /**
Varun Shah50ef2002019-03-04 16:41:12 -08001346 * Indicates that users are switchable.
1347 * @hide
1348 */
1349 @SystemApi
1350 public static final int SWITCHABILITY_STATUS_OK = 0;
1351
1352 /**
1353 * Indicated that the user is in a phone call.
1354 * @hide
1355 */
1356 @SystemApi
1357 public static final int SWITCHABILITY_STATUS_USER_IN_CALL = 1 << 0;
1358
1359 /**
1360 * Indicates that user switching is disallowed ({@link #DISALLOW_USER_SWITCH} is set).
1361 * @hide
1362 */
1363 @SystemApi
1364 public static final int SWITCHABILITY_STATUS_USER_SWITCH_DISALLOWED = 1 << 1;
1365
1366 /**
1367 * Indicates that the system user is locked and user switching is not allowed.
1368 * @hide
1369 */
1370 @SystemApi
1371 public static final int SWITCHABILITY_STATUS_SYSTEM_USER_LOCKED = 1 << 2;
1372
1373 /**
1374 * Result returned in {@link #getUserSwitchability()} indicating user swichability.
1375 * @hide
1376 */
1377 @Retention(RetentionPolicy.SOURCE)
1378 @IntDef(flag = true, prefix = { "SWITCHABILITY_STATUS_" }, value = {
1379 SWITCHABILITY_STATUS_OK,
1380 SWITCHABILITY_STATUS_USER_IN_CALL,
1381 SWITCHABILITY_STATUS_USER_SWITCH_DISALLOWED,
1382 SWITCHABILITY_STATUS_SYSTEM_USER_LOCKED
1383 })
1384 public @interface UserSwitchabilityResult {}
1385
1386 /**
Alex Chau89386ba2018-02-13 11:09:17 +08001387 * Indicates user operation is successful.
1388 */
1389 public static final int USER_OPERATION_SUCCESS = 0;
1390
1391 /**
1392 * Indicates user operation failed for unknown reason.
1393 */
1394 public static final int USER_OPERATION_ERROR_UNKNOWN = 1;
1395
1396 /**
1397 * Indicates user operation failed because target user is a managed profile.
1398 */
1399 public static final int USER_OPERATION_ERROR_MANAGED_PROFILE = 2;
1400
1401 /**
1402 * Indicates user operation failed because maximum running user limit has been reached.
1403 */
1404 public static final int USER_OPERATION_ERROR_MAX_RUNNING_USERS = 3;
1405
1406 /**
1407 * Indicates user operation failed because the target user is in the foreground.
1408 */
1409 public static final int USER_OPERATION_ERROR_CURRENT_USER = 4;
1410
1411 /**
1412 * Indicates user operation failed because device has low data storage.
1413 */
1414 public static final int USER_OPERATION_ERROR_LOW_STORAGE = 5;
1415
1416 /**
1417 * Indicates user operation failed because maximum user limit has been reached.
1418 */
1419 public static final int USER_OPERATION_ERROR_MAX_USERS = 6;
1420
1421 /**
1422 * Result returned from various user operations.
1423 *
1424 * @hide
1425 */
1426 @Retention(RetentionPolicy.SOURCE)
1427 @IntDef(prefix = { "USER_OPERATION_" }, value = {
1428 USER_OPERATION_SUCCESS,
1429 USER_OPERATION_ERROR_UNKNOWN,
1430 USER_OPERATION_ERROR_MANAGED_PROFILE,
1431 USER_OPERATION_ERROR_MAX_RUNNING_USERS,
1432 USER_OPERATION_ERROR_CURRENT_USER,
1433 USER_OPERATION_ERROR_LOW_STORAGE,
1434 USER_OPERATION_ERROR_MAX_USERS
1435 })
1436 public @interface UserOperationResult {}
1437
1438 /**
1439 * Thrown to indicate user operation failed.
1440 */
1441 public static class UserOperationException extends RuntimeException {
1442 private final @UserOperationResult int mUserOperationResult;
1443
1444 /**
1445 * Constructs a UserOperationException with specific result code.
1446 *
1447 * @param message the detail message
1448 * @param userOperationResult the result code
1449 * @hide
1450 */
1451 public UserOperationException(String message,
1452 @UserOperationResult int userOperationResult) {
1453 super(message);
1454 mUserOperationResult = userOperationResult;
1455 }
1456
1457 /**
1458 * Returns the operation result code.
1459 */
1460 public @UserOperationResult int getUserOperationResult() {
1461 return mUserOperationResult;
1462 }
Valentin Iftime89df4c82019-08-23 13:02:50 +02001463
1464 /**
1465 * Returns a UserOperationException containing the same message and error code.
1466 * @hide
1467 */
1468 public static UserOperationException from(ServiceSpecificException exception) {
1469 return new UserOperationException(exception.getMessage(), exception.errorCode);
1470 }
1471 }
1472
1473 /**
1474 * Converts the ServiceSpecificException into a UserOperationException or throws null;
1475 *
1476 * @param exception exception to convert.
1477 * @param throwInsteadOfNull if an exception should be thrown or null returned.
1478 * @return null if chosen not to throw exception.
1479 * @throws UserOperationException
1480 */
1481 private <T> T returnNullOrThrowUserOperationException(ServiceSpecificException exception,
1482 boolean throwInsteadOfNull) throws UserOperationException {
1483 if (throwInsteadOfNull) {
1484 throw UserOperationException.from(exception);
1485 } else {
1486 return null;
1487 }
1488 }
1489
1490 /**
1491 * Thrown to indicate user operation failed. (Checked exception)
1492 * @hide
1493 */
1494 public static class CheckedUserOperationException extends AndroidException {
1495 private final @UserOperationResult int mUserOperationResult;
1496
1497 /**
1498 * Constructs a CheckedUserOperationException with specific result code.
1499 *
1500 * @param message the detail message
1501 * @param userOperationResult the result code
1502 * @hide
1503 */
1504 public CheckedUserOperationException(String message,
1505 @UserOperationResult int userOperationResult) {
1506 super(message);
1507 mUserOperationResult = userOperationResult;
1508 }
1509
1510 /** Returns the operation result code. */
1511 public @UserOperationResult int getUserOperationResult() {
1512 return mUserOperationResult;
1513 }
1514
1515 /** Return a ServiceSpecificException containing the same message and error code. */
1516 public ServiceSpecificException toServiceSpecificException() {
1517 return new ServiceSpecificException(mUserOperationResult, getMessage());
1518 }
Alex Chau89386ba2018-02-13 11:09:17 +08001519 }
1520
Amith Yamasani7e99bc02013-04-16 18:24:51 -07001521 /** @hide */
Andrei Onea24ec3212019-03-15 17:35:05 +00001522 @UnsupportedAppUsage
Amith Yamasanic0688302015-10-30 10:40:03 -07001523 public static UserManager get(Context context) {
1524 return (UserManager) context.getSystemService(Context.USER_SERVICE);
Amith Yamasani27db4682013-03-30 17:07:47 -07001525 }
Maggie Benthalla12fccf2013-03-14 18:02:12 -04001526
Amith Yamasani258848d2012-08-10 17:06:33 -07001527 /** @hide */
1528 public UserManager(Context context, IUserManager service) {
1529 mService = service;
Fyodor Kupolov5200e1c2016-10-17 18:46:16 -07001530 mContext = context.getApplicationContext();
Valentin Iftime89df4c82019-08-23 13:02:50 +02001531 mUserId = context.getUserId();
Amith Yamasani258848d2012-08-10 17:06:33 -07001532 }
1533
1534 /**
Amith Yamasani06964342016-04-15 13:55:01 -07001535 * Returns whether this device supports multiple users with their own login and customizable
1536 * space.
1537 * @return whether the device supports multiple users.
Amith Yamasani258848d2012-08-10 17:06:33 -07001538 */
Jeff Sharkey4673e7e2012-09-19 13:14:30 -07001539 public static boolean supportsMultipleUsers() {
Kenny Guy1a447532014-02-20 21:55:32 +00001540 return getMaxSupportedUsers() > 1
1541 && SystemProperties.getBoolean("fw.show_multiuserui",
1542 Resources.getSystem().getBoolean(R.bool.config_enableMultiUserUI));
Amith Yamasani258848d2012-08-10 17:06:33 -07001543 }
1544
Maggie Benthall67944582013-02-22 14:58:27 -05001545 /**
Xiaohui Chen7cb69df2015-07-13 16:01:01 -07001546 * @hide
1547 * @return Whether the device is running with split system user. It means the system user and
1548 * primary user are two separate users. Previously system user and primary user are combined as
1549 * a single owner user. see @link {android.os.UserHandle#USER_OWNER}
1550 */
Philip P. Moltmannf80809f2018-04-04 11:20:44 -07001551 @TestApi
Xiaohui Chen7cb69df2015-07-13 16:01:01 -07001552 public static boolean isSplitSystemUser() {
John Reckaa67f682016-09-20 14:24:21 -07001553 return RoSystemProperties.FW_SYSTEM_USER_SPLIT;
Xiaohui Chen7cb69df2015-07-13 16:01:01 -07001554 }
1555
1556 /**
Evan Rosky13a58a92016-07-27 15:51:09 -07001557 * @return Whether guest user is always ephemeral
1558 * @hide
1559 */
1560 public static boolean isGuestUserEphemeral() {
1561 return Resources.getSystem()
1562 .getBoolean(com.android.internal.R.bool.config_guestUserEphemeral);
1563 }
1564
1565 /**
Eric Jeonge7ead1b2019-07-18 09:37:08 -07001566 * @hide
1567 * @return Whether the device is running in a headless system user mode. It means the headless
1568 * user (system user) runs system services and system UI, but is not associated with any real
1569 * person. Secondary users can be created to be associated with real person.
1570 */
1571 public static boolean isHeadlessSystemUserMode() {
1572 return RoSystemProperties.MULTIUSER_HEADLESS_SYSTEM_USER;
1573 }
1574
1575 /**
Varun Shah50ef2002019-03-04 16:41:12 -08001576 * @deprecated use {@link #getUserSwitchability()} instead.
1577 *
1578 * @removed
Fyodor Kupolov523c4042016-02-24 15:03:13 -08001579 * @hide
1580 */
Varun Shah50ef2002019-03-04 16:41:12 -08001581 @Deprecated
1582 @UnsupportedAppUsage
Fyodor Kupolov523c4042016-02-24 15:03:13 -08001583 public boolean canSwitchUsers() {
1584 boolean allowUserSwitchingWhenSystemUserLocked = Settings.Global.getInt(
1585 mContext.getContentResolver(),
1586 Settings.Global.ALLOW_USER_SWITCHING_WHEN_SYSTEM_USER_LOCKED, 0) != 0;
1587 boolean isSystemUserUnlocked = isUserUnlocked(UserHandle.SYSTEM);
Jayachandran C16dce222019-11-15 15:42:01 -08001588 boolean inCall = false;
1589 TelephonyManager telephonyManager = mContext.getSystemService(TelephonyManager.class);
1590 if (telephonyManager != null) {
1591 inCall = telephonyManager.getCallState() != TelephonyManager.CALL_STATE_IDLE;
1592 }
Benjamin Franzff66fa92017-08-10 10:39:44 +01001593 boolean isUserSwitchDisallowed = hasUserRestriction(DISALLOW_USER_SWITCH);
1594 return (allowUserSwitchingWhenSystemUserLocked || isSystemUserUnlocked) && !inCall
1595 && !isUserSwitchDisallowed;
Fyodor Kupolov523c4042016-02-24 15:03:13 -08001596 }
1597
1598 /**
Anthony Hugh6acda5e2019-10-24 14:49:52 -07001599 * Returns whether switching users is currently allowed for the user this process is running
1600 * under.
Varun Shah50ef2002019-03-04 16:41:12 -08001601 * <p>
1602 * Switching users is not allowed in the following cases:
1603 * <li>the user is in a phone call</li>
1604 * <li>{@link #DISALLOW_USER_SWITCH} is set</li>
1605 * <li>system user hasn't been unlocked yet</li>
1606 *
1607 * @return A {@link UserSwitchabilityResult} flag indicating if the user is switchable.
1608 * @hide
1609 */
1610 @SystemApi
1611 @RequiresPermission(allOf = {Manifest.permission.READ_PHONE_STATE,
1612 android.Manifest.permission.MANAGE_USERS,
1613 android.Manifest.permission.INTERACT_ACROSS_USERS}, conditional = true)
1614 public @UserSwitchabilityResult int getUserSwitchability() {
Anthony Hugh6acda5e2019-10-24 14:49:52 -07001615 return getUserSwitchability(Process.myUserHandle());
1616 }
1617
1618 /**
1619 * Returns whether switching users is currently allowed for the provided user.
1620 * <p>
1621 * Switching users is not allowed in the following cases:
1622 * <li>the user is in a phone call</li>
1623 * <li>{@link #DISALLOW_USER_SWITCH} is set</li>
1624 * <li>system user hasn't been unlocked yet</li>
1625 *
1626 * @return A {@link UserSwitchabilityResult} flag indicating if the user is switchable.
1627 * @hide
1628 */
1629 @RequiresPermission(allOf = {Manifest.permission.READ_PHONE_STATE,
1630 android.Manifest.permission.MANAGE_USERS,
1631 android.Manifest.permission.INTERACT_ACROSS_USERS}, conditional = true)
1632 public @UserSwitchabilityResult int getUserSwitchability(UserHandle userHandle) {
Varun Shah50ef2002019-03-04 16:41:12 -08001633 final TelephonyManager tm =
1634 (TelephonyManager) mContext.getSystemService(Context.TELEPHONY_SERVICE);
1635
1636 int flags = SWITCHABILITY_STATUS_OK;
1637 if (tm.getCallState() != TelephonyManager.CALL_STATE_IDLE) {
1638 flags |= SWITCHABILITY_STATUS_USER_IN_CALL;
1639 }
Anthony Hugh6acda5e2019-10-24 14:49:52 -07001640 if (hasUserRestriction(DISALLOW_USER_SWITCH, userHandle)) {
Varun Shah50ef2002019-03-04 16:41:12 -08001641 flags |= SWITCHABILITY_STATUS_USER_SWITCH_DISALLOWED;
1642 }
Anthony Hugh6acda5e2019-10-24 14:49:52 -07001643
1644 // System User is always unlocked in Headless System User Mode, so ignore this flag
1645 if (!isHeadlessSystemUserMode()) {
1646 final boolean allowUserSwitchingWhenSystemUserLocked = Settings.Global.getInt(
1647 mContext.getContentResolver(),
1648 Settings.Global.ALLOW_USER_SWITCHING_WHEN_SYSTEM_USER_LOCKED, 0) != 0;
1649 final boolean systemUserUnlocked = isUserUnlocked(UserHandle.SYSTEM);
1650
1651 if (!allowUserSwitchingWhenSystemUserLocked && !systemUserUnlocked) {
1652 flags |= SWITCHABILITY_STATUS_SYSTEM_USER_LOCKED;
1653 }
Varun Shah50ef2002019-03-04 16:41:12 -08001654 }
Anthony Hugh6acda5e2019-10-24 14:49:52 -07001655
Varun Shah50ef2002019-03-04 16:41:12 -08001656 return flags;
1657 }
1658
1659 /**
Amith Yamasani5760e172015-04-17 18:42:41 -07001660 * Returns the user handle for the user that this process is running under.
Jessica Hummelbe81c802014-04-22 15:49:22 +01001661 *
Amith Yamasani5760e172015-04-17 18:42:41 -07001662 * @return the user handle of this process.
Amith Yamasani258848d2012-08-10 17:06:33 -07001663 * @hide
Maggie Benthall67944582013-02-22 14:58:27 -05001664 */
Andrei Onea24ec3212019-03-15 17:35:05 +00001665 @UnsupportedAppUsage
Jeff Sharkey8588bc12016-01-06 16:47:42 -07001666 public @UserIdInt int getUserHandle() {
Dianne Hackborn79af1dd2012-08-16 16:42:52 -07001667 return UserHandle.myUserId();
Amith Yamasani258848d2012-08-10 17:06:33 -07001668 }
1669
1670 /**
Valentin Iftime89df4c82019-08-23 13:02:50 +02001671 * Returns the user name of the context user. This call is only available to applications on
1672 * the system image; it requires the {@code android.permission.MANAGE_USERS} or {@code
1673 * android.permission.GET_ACCOUNTS_PRIVILEGED} permissions.
1674 *
Amith Yamasani258848d2012-08-10 17:06:33 -07001675 * @return the user name
1676 */
Valentin Iftime89df4c82019-08-23 13:02:50 +02001677 @RequiresPermission(anyOf = {android.Manifest.permission.MANAGE_USERS,
1678 android.Manifest.permission.GET_ACCOUNTS_PRIVILEGED}, conditional = true)
1679 @UserHandleAware
1680 public @NonNull String getUserName() {
1681 if (UserHandle.myUserId() == mUserId) {
1682 try {
1683 return mService.getUserName();
1684 } catch (RemoteException re) {
1685 throw re.rethrowFromSystemServer();
1686 }
1687 } else {
1688 UserInfo userInfo = getUserInfo(mUserId);
1689 return userInfo == null ? "" : userInfo.name;
Bookatzcde3d922019-03-08 14:30:00 -08001690 }
Amith Yamasani258848d2012-08-10 17:06:33 -07001691 }
1692
Dianne Hackborn67a101a2014-10-02 12:42:18 -07001693 /**
Fyodor Kupolov605b12a2017-05-10 15:58:09 -07001694 * Returns whether user name has been set.
1695 * <p>This method can be used to check that the value returned by {@link #getUserName()} was
1696 * set by the user and is not a placeholder string provided by the system.
1697 * @hide
1698 */
Bookatzfe4eb2b2019-08-22 18:26:49 -07001699 @SystemApi
1700 @RequiresPermission(anyOf = {Manifest.permission.MANAGE_USERS,
1701 Manifest.permission.GET_ACCOUNTS_PRIVILEGED})
Fyodor Kupolov605b12a2017-05-10 15:58:09 -07001702 public boolean isUserNameSet() {
1703 try {
1704 return mService.isUserNameSet(getUserHandle());
1705 } catch (RemoteException re) {
1706 throw re.rethrowFromSystemServer();
1707 }
1708 }
1709
1710 /**
Dan Morrille4ab16a2012-09-20 20:25:55 -07001711 * Used to determine whether the user making this call is subject to
1712 * teleportations.
Dianne Hackborn67a101a2014-10-02 12:42:18 -07001713 *
Dianne Hackborn955d8d62014-10-07 20:17:19 -07001714 * <p>As of {@link android.os.Build.VERSION_CODES#LOLLIPOP}, this method can
Dianne Hackborn67a101a2014-10-02 12:42:18 -07001715 * now automatically identify goats using advanced goat recognition technology.</p>
1716 *
1717 * @return Returns true if the user making this call is a goat.
Dan Morrille4ab16a2012-09-20 20:25:55 -07001718 */
1719 public boolean isUserAGoat() {
Adam Powell988ae302014-09-17 17:58:33 -07001720 return mContext.getPackageManager()
1721 .isPackageAvailable("com.coffeestainstudios.goatsimulator");
Dan Morrille4ab16a2012-09-20 20:25:55 -07001722 }
Amith Yamasanidf2e92a2013-03-01 17:04:38 -08001723
1724 /**
Xiaohui Chen70f6c382015-04-28 14:21:43 -07001725 * Used to check if this process is running under the primary user. The primary user
Ying Zheng215116f2019-09-11 12:50:09 -07001726 * is the first human user on a device. This is not supported in headless system user mode.
Xiaohui Chen70f6c382015-04-28 14:21:43 -07001727 *
1728 * @return whether this process is running under the primary user.
1729 * @hide
1730 */
Varun Shahe142e332018-10-22 15:52:56 -07001731 @SystemApi
Valentin Iftime89df4c82019-08-23 13:02:50 +02001732 @RequiresPermission(anyOf = {Manifest.permission.MANAGE_USERS,
1733 Manifest.permission.CREATE_USERS})
Xiaohui Chen70f6c382015-04-28 14:21:43 -07001734 public boolean isPrimaryUser() {
1735 UserInfo user = getUserInfo(UserHandle.myUserId());
Amith Yamasanid35a89c2016-05-26 16:58:43 -07001736 return user != null && user.isPrimary();
Xiaohui Chen70f6c382015-04-28 14:21:43 -07001737 }
1738
1739 /**
Amith Yamasani5760e172015-04-17 18:42:41 -07001740 * Used to check if this process is running under the system user. The system user
1741 * is the initial user that is implicitly created on first boot and hosts most of the
1742 * system services.
1743 *
1744 * @return whether this process is running under the system user.
1745 */
1746 public boolean isSystemUser() {
Xiaohui Chen70f6c382015-04-28 14:21:43 -07001747 return UserHandle.myUserId() == UserHandle.USER_SYSTEM;
Amith Yamasani5760e172015-04-17 18:42:41 -07001748 }
Xiaohui Chen2b45f8a2015-08-04 15:12:23 -07001749
Amith Yamasani5760e172015-04-17 18:42:41 -07001750 /**
Varun Shahe142e332018-10-22 15:52:56 -07001751 * Used to check if this process is running as an admin user. An admin user is allowed to
1752 * modify or configure certain settings that aren't available to non-admin users,
1753 * create and delete additional users, etc. There can be more than one admin users.
1754 *
1755 * @return whether this process is running under an admin user.
Amith Yamasani462ac3a2015-06-30 14:21:01 -07001756 * @hide
Amith Yamasani462ac3a2015-06-30 14:21:01 -07001757 */
Varun Shahe142e332018-10-22 15:52:56 -07001758 @SystemApi
1759 @RequiresPermission(android.Manifest.permission.MANAGE_USERS)
Amith Yamasani462ac3a2015-06-30 14:21:01 -07001760 public boolean isAdminUser() {
Xiaohui Chen2b45f8a2015-08-04 15:12:23 -07001761 return isUserAdmin(UserHandle.myUserId());
1762 }
1763
1764 /**
1765 * @hide
1766 * Returns whether the provided user is an admin user. There can be more than one admin
1767 * user.
1768 */
Andrei Onea24ec3212019-03-15 17:35:05 +00001769 @UnsupportedAppUsage
Valentin Iftime89df4c82019-08-23 13:02:50 +02001770 @RequiresPermission(anyOf = {Manifest.permission.MANAGE_USERS,
1771 Manifest.permission.CREATE_USERS})
Jeff Sharkey8588bc12016-01-06 16:47:42 -07001772 public boolean isUserAdmin(@UserIdInt int userId) {
Xiaohui Chen2b45f8a2015-08-04 15:12:23 -07001773 UserInfo user = getUserInfo(userId);
1774 return user != null && user.isAdmin();
Amith Yamasani462ac3a2015-06-30 14:21:01 -07001775 }
1776
1777 /**
Valentin Iftime89df4c82019-08-23 13:02:50 +02001778 * Returns whether the context user's user is of the given user type, such as
Adam Bookatz351a8fb2019-12-23 10:09:14 -08001779 * {@link UserManager#USER_TYPE_FULL_GUEST}.
Bookatz029832a2019-10-04 16:50:22 -07001780 *
Adam Bookatz351a8fb2019-12-23 10:09:14 -08001781 * @return true if the user is of the given user type.
Bookatz029832a2019-10-04 16:50:22 -07001782 * @hide
1783 */
Valentin Iftime89df4c82019-08-23 13:02:50 +02001784 @SystemApi
1785 @UserHandleAware
Adam Bookatz351a8fb2019-12-23 10:09:14 -08001786 @RequiresPermission(android.Manifest.permission.MANAGE_USERS)
1787 public boolean isUserOfType(@NonNull String userType) {
Bookatz029832a2019-10-04 16:50:22 -07001788 try {
Valentin Iftime89df4c82019-08-23 13:02:50 +02001789 return mService.isUserOfType(mUserId, userType);
Bookatz029832a2019-10-04 16:50:22 -07001790 } catch (RemoteException re) {
1791 throw re.rethrowFromSystemServer();
1792 }
1793 }
1794
1795 /**
Adam Bookatz351a8fb2019-12-23 10:09:14 -08001796 * Returns whether the given user is of the given user type, such as
1797 * {@link UserManager#USER_TYPE_FULL_GUEST}.
Bookatz029832a2019-10-04 16:50:22 -07001798 *
1799 * @param userHandle the user handle of the user whose type is being requested.
Adam Bookatz351a8fb2019-12-23 10:09:14 -08001800 * @param userType the name of the user's user type, e.g.
1801 * {@link UserManager#USER_TYPE_PROFILE_MANAGED}.
1802 * @return true if the userHandle user is of type userType
Bookatz029832a2019-10-04 16:50:22 -07001803 * @hide
1804 */
Valentin Iftime89df4c82019-08-23 13:02:50 +02001805 @SystemApi
Adam Bookatz351a8fb2019-12-23 10:09:14 -08001806 @RequiresPermission(android.Manifest.permission.MANAGE_USERS)
1807 public boolean isUserOfType(@NonNull UserHandle userHandle, @NonNull String userType) {
Bookatz029832a2019-10-04 16:50:22 -07001808 try {
Adam Bookatz351a8fb2019-12-23 10:09:14 -08001809 return mService.isUserOfType(userHandle.getIdentifier(), userType);
Bookatz029832a2019-10-04 16:50:22 -07001810 } catch (RemoteException re) {
1811 throw re.rethrowFromSystemServer();
1812 }
1813 }
1814
1815 /**
1816 * Returns whether the user type is a
1817 * {@link UserManager#USER_TYPE_PROFILE_MANAGED managed profile}.
1818 * @hide
1819 */
1820 public static boolean isUserTypeManagedProfile(String userType) {
1821 return USER_TYPE_PROFILE_MANAGED.equals(userType);
1822 }
1823
1824 /**
1825 * Returns whether the user type is a {@link UserManager#USER_TYPE_FULL_GUEST guest user}.
1826 * @hide
1827 */
1828 public static boolean isUserTypeGuest(String userType) {
1829 return USER_TYPE_FULL_GUEST.equals(userType);
1830 }
1831
1832 /**
1833 * Returns whether the user type is a
1834 * {@link UserManager#USER_TYPE_FULL_RESTRICTED restricted user}.
1835 * @hide
1836 */
1837 public static boolean isUserTypeRestricted(String userType) {
1838 return USER_TYPE_FULL_RESTRICTED.equals(userType);
1839 }
1840
1841 /**
1842 * Returns whether the user type is a {@link UserManager#USER_TYPE_FULL_DEMO demo user}.
1843 * @hide
1844 */
1845 public static boolean isUserTypeDemo(String userType) {
1846 return USER_TYPE_FULL_DEMO.equals(userType);
1847 }
1848
1849 /**
Fyodor Kupolovca177562017-11-09 17:43:01 -08001850 * @hide
1851 * @deprecated Use {@link #isRestrictedProfile()}
1852 */
Andrei Onea24ec3212019-03-15 17:35:05 +00001853 @UnsupportedAppUsage
Fyodor Kupolovca177562017-11-09 17:43:01 -08001854 @Deprecated
1855 public boolean isLinkedUser() {
1856 return isRestrictedProfile();
1857 }
1858
1859 /**
Varun Shahe142e332018-10-22 15:52:56 -07001860 * Used to check if this process is running under a restricted profile. Restricted profiles
1861 * may have a reduced number of available apps, app restrictions, and account restrictions.
1862 *
1863 * @return whether this process is running under a restricted profile.
Amith Yamasani2555daf2013-04-25 13:39:27 -07001864 * @hide
Amith Yamasanidf2e92a2013-03-01 17:04:38 -08001865 */
Fyodor Kupolovca177562017-11-09 17:43:01 -08001866 @SystemApi
Varun Shahe142e332018-10-22 15:52:56 -07001867 @RequiresPermission(android.Manifest.permission.MANAGE_USERS)
Fyodor Kupolovca177562017-11-09 17:43:01 -08001868 public boolean isRestrictedProfile() {
Amith Yamasanidf2e92a2013-03-01 17:04:38 -08001869 try {
Amith Yamasani71e6c692013-03-24 17:39:28 -07001870 return mService.isRestricted();
Amith Yamasanidf2e92a2013-03-01 17:04:38 -08001871 } catch (RemoteException re) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001872 throw re.rethrowFromSystemServer();
Amith Yamasanidf2e92a2013-03-01 17:04:38 -08001873 }
1874 }
1875
Amith Yamasani258848d2012-08-10 17:06:33 -07001876 /**
Hai Zhang7c064ca2019-03-18 18:21:38 -07001877 * Check if a user is a restricted profile. Restricted profiles may have a reduced number of
1878 * available apps, app restrictions, and account restrictions.
1879 *
1880 * @param user the user to check
1881 * @return whether the user is a restricted profile.
1882 * @hide
1883 */
1884 @SystemApi
Valentin Iftime89df4c82019-08-23 13:02:50 +02001885 @RequiresPermission(anyOf = {Manifest.permission.MANAGE_USERS,
1886 Manifest.permission.CREATE_USERS})
Hai Zhang7c064ca2019-03-18 18:21:38 -07001887 public boolean isRestrictedProfile(@NonNull UserHandle user) {
1888 try {
1889 return mService.getUserInfo(user.getIdentifier()).isRestricted();
1890 } catch (RemoteException re) {
1891 throw re.rethrowFromSystemServer();
1892 }
1893 }
1894
1895 /**
Fyodor Kupolov1c363152015-09-02 13:27:21 -07001896 * Checks if specified user can have restricted profile.
1897 * @hide
1898 */
Valentin Iftime89df4c82019-08-23 13:02:50 +02001899 @RequiresPermission(android.Manifest.permission.MANAGE_USERS)
Jeff Sharkey8588bc12016-01-06 16:47:42 -07001900 public boolean canHaveRestrictedProfile(@UserIdInt int userId) {
Fyodor Kupolov1c363152015-09-02 13:27:21 -07001901 try {
1902 return mService.canHaveRestrictedProfile(userId);
1903 } catch (RemoteException re) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001904 throw re.rethrowFromSystemServer();
Fyodor Kupolov1c363152015-09-02 13:27:21 -07001905 }
1906 }
1907
1908 /**
Fyodor Kupolovca177562017-11-09 17:43:01 -08001909 * Returns whether the calling user has at least one restricted profile associated with it.
1910 * @return
1911 * @hide
1912 */
1913 @SystemApi
Valentin Iftime89df4c82019-08-23 13:02:50 +02001914 @RequiresPermission(android.Manifest.permission.MANAGE_USERS)
Fyodor Kupolovca177562017-11-09 17:43:01 -08001915 public boolean hasRestrictedProfiles() {
1916 try {
1917 return mService.hasRestrictedProfiles();
1918 } catch (RemoteException re) {
1919 throw re.rethrowFromSystemServer();
1920 }
1921 }
1922
1923 /**
Evan Rosky13a58a92016-07-27 15:51:09 -07001924 * Checks if a user is a guest user.
1925 * @return whether user is a guest user.
1926 * @hide
1927 */
Andrei Onea24ec3212019-03-15 17:35:05 +00001928 @UnsupportedAppUsage
Valentin Iftime89df4c82019-08-23 13:02:50 +02001929 @RequiresPermission(anyOf = {Manifest.permission.MANAGE_USERS,
1930 Manifest.permission.CREATE_USERS})
Bookatz07758e32019-11-19 17:16:08 -08001931 public boolean isGuestUser(@UserIdInt int userId) {
1932 UserInfo user = getUserInfo(userId);
Evan Rosky13a58a92016-07-27 15:51:09 -07001933 return user != null && user.isGuest();
1934 }
1935
1936 /**
Varun Shahe142e332018-10-22 15:52:56 -07001937 * Used to check if this process is running under a guest user. A guest user may be transient.
1938 *
1939 * @return whether this process is running under a guest user.
Amith Yamasani1e9c2182014-06-11 17:25:51 -07001940 * @hide
1941 */
Varun Shahe142e332018-10-22 15:52:56 -07001942 @SystemApi
Valentin Iftime89df4c82019-08-23 13:02:50 +02001943 @RequiresPermission(anyOf = {Manifest.permission.MANAGE_USERS,
1944 Manifest.permission.CREATE_USERS})
Amith Yamasani1e9c2182014-06-11 17:25:51 -07001945 public boolean isGuestUser() {
1946 UserInfo user = getUserInfo(UserHandle.myUserId());
Amith Yamasanid35a89c2016-05-26 16:58:43 -07001947 return user != null && user.isGuest();
1948 }
1949
Fyodor Kupolovca177562017-11-09 17:43:01 -08001950
Amith Yamasanid35a89c2016-05-26 16:58:43 -07001951 /**
Amith Yamasani1c41dc82016-06-28 16:13:15 -07001952 * Checks if the calling app is running in a demo user. When running in a demo user,
1953 * apps can be more helpful to the user, or explain their features in more detail.
1954 *
Amith Yamasanid35a89c2016-05-26 16:58:43 -07001955 * @return whether the caller is a demo user.
Amith Yamasanid35a89c2016-05-26 16:58:43 -07001956 */
1957 public boolean isDemoUser() {
Amith Yamasani1c41dc82016-06-28 16:13:15 -07001958 try {
1959 return mService.isDemoUser(UserHandle.myUserId());
1960 } catch (RemoteException re) {
1961 throw re.rethrowFromSystemServer();
1962 }
Amith Yamasani1e9c2182014-06-11 17:25:51 -07001963 }
1964
1965 /**
Valentin Iftime89df4c82019-08-23 13:02:50 +02001966 * Checks if the calling context user is running in a profile.
1967 *
1968 * Requires {@link android.Manifest.permission#MANAGE_USERS} or
1969 * {@link android.Manifest.permission#INTERACT_ACROSS_USERS} permission, otherwise the
1970 * caller must be in the same profile group of specified user.
Bookatz029832a2019-10-04 16:50:22 -07001971 *
1972 * @return whether the caller is in a profile.
1973 * @hide
1974 */
Valentin Iftime89df4c82019-08-23 13:02:50 +02001975 @SystemApi
1976 @RequiresPermission(anyOf = {android.Manifest.permission.MANAGE_USERS,
1977 android.Manifest.permission.INTERACT_ACROSS_USERS}, conditional = true)
1978 @UserHandleAware
Bookatz029832a2019-10-04 16:50:22 -07001979 public boolean isProfile() {
Valentin Iftime89df4c82019-08-23 13:02:50 +02001980 return isProfile(mUserId);
1981 }
1982
1983 private boolean isProfile(@UserIdInt int userId) {
1984 if (userId == UserHandle.myUserId()) {
1985 // No need for synchronization. Once it becomes non-null, it'll be non-null forever.
1986 // Worst case we might end up calling the AIDL method multiple times but that's fine.
1987 if (mIsProfileCached != null) {
1988 return mIsProfileCached;
1989 }
1990 try {
1991 mIsProfileCached = mService.isProfile(userId);
1992 return mIsProfileCached;
1993 } catch (RemoteException re) {
1994 throw re.rethrowFromSystemServer();
1995 }
1996 } else {
1997 try {
1998 return mService.isProfile(userId);
1999 } catch (RemoteException re) {
2000 throw re.rethrowFromSystemServer();
2001 }
Bookatz029832a2019-10-04 16:50:22 -07002002 }
2003 }
2004
2005 /**
Amith Yamasani0e8d7d62014-09-03 13:17:28 -07002006 * Checks if the calling app is running in a managed profile.
Amith Yamasani0e8d7d62014-09-03 13:17:28 -07002007 *
2008 * @return whether the caller is in a managed profile.
Amith Yamasani0e8d7d62014-09-03 13:17:28 -07002009 */
Amith Yamasani0e8d7d62014-09-03 13:17:28 -07002010 public boolean isManagedProfile() {
Makoto Onukid49f3fa2017-01-25 14:09:48 -08002011 // No need for synchronization. Once it becomes non-null, it'll be non-null forever.
2012 // Worst case we might end up calling the AIDL method multiple times but that's fine.
2013 if (mIsManagedProfileCached != null) {
2014 return mIsManagedProfileCached;
2015 }
Tony Mak8673b282016-03-21 21:10:59 +00002016 try {
Makoto Onukid49f3fa2017-01-25 14:09:48 -08002017 mIsManagedProfileCached = mService.isManagedProfile(UserHandle.myUserId());
2018 return mIsManagedProfileCached;
Tony Mak8673b282016-03-21 21:10:59 +00002019 } catch (RemoteException re) {
2020 throw re.rethrowFromSystemServer();
2021 }
2022 }
2023
2024 /**
2025 * Checks if the specified user is a managed profile.
Bookatz029832a2019-10-04 16:50:22 -07002026 * Requires {@link android.Manifest.permission#MANAGE_USERS} or
2027 * {@link android.Manifest.permission#INTERACT_ACROSS_USERS} permission, otherwise the caller
Tony Mak8673b282016-03-21 21:10:59 +00002028 * must be in the same profile group of specified user.
2029 *
2030 * @return whether the specified user is a managed profile.
2031 * @hide
2032 */
2033 @SystemApi
Valentin Iftime89df4c82019-08-23 13:02:50 +02002034 @RequiresPermission(anyOf = {android.Manifest.permission.MANAGE_USERS,
2035 Manifest.permission.INTERACT_ACROSS_USERS}, conditional = true)
Tony Mak8673b282016-03-21 21:10:59 +00002036 public boolean isManagedProfile(@UserIdInt int userId) {
Makoto Onukid49f3fa2017-01-25 14:09:48 -08002037 if (userId == UserHandle.myUserId()) {
2038 return isManagedProfile();
2039 }
Tony Mak8673b282016-03-21 21:10:59 +00002040 try {
2041 return mService.isManagedProfile(userId);
2042 } catch (RemoteException re) {
2043 throw re.rethrowFromSystemServer();
2044 }
Amith Yamasani0e8d7d62014-09-03 13:17:28 -07002045 }
2046
2047 /**
Lenka Trochtovac4dd0212015-11-18 12:22:06 +01002048 * Checks if the calling app is running as an ephemeral user.
2049 *
2050 * @return whether the caller is an ephemeral user.
2051 * @hide
2052 */
Valentin Iftime89df4c82019-08-23 13:02:50 +02002053 @RequiresPermission(anyOf = {Manifest.permission.MANAGE_USERS,
2054 Manifest.permission.CREATE_USERS})
Lenka Trochtovac4dd0212015-11-18 12:22:06 +01002055 public boolean isEphemeralUser() {
2056 return isUserEphemeral(UserHandle.myUserId());
2057 }
2058
2059 /**
2060 * Returns whether the specified user is ephemeral.
2061 * @hide
2062 */
Valentin Iftime89df4c82019-08-23 13:02:50 +02002063 @RequiresPermission(anyOf = {Manifest.permission.MANAGE_USERS,
2064 Manifest.permission.CREATE_USERS})
Jeff Sharkey8588bc12016-01-06 16:47:42 -07002065 public boolean isUserEphemeral(@UserIdInt int userId) {
Lenka Trochtovac4dd0212015-11-18 12:22:06 +01002066 final UserInfo user = getUserInfo(userId);
2067 return user != null && user.isEphemeral();
2068 }
2069
2070 /**
Dianne Hackborna8a9bd62012-10-09 15:36:59 -07002071 * Return whether the given user is actively running. This means that
2072 * the user is in the "started" state, not "stopped" -- it is currently
2073 * allowed to run code through scheduled alarms, receiving broadcasts,
2074 * etc. A started user may be either the current foreground user or a
2075 * background user; the result here does not distinguish between the two.
Makoto Onuki5eef50d2017-03-02 16:38:45 -08002076 *
2077 * <p>Note prior to Android Nougat MR1 (SDK version <= 24;
Andrew Solovay8a788f42018-12-12 14:25:47 -08002078 * {@link android.os.Build.VERSION_CODES#N}, this API required a system permission
Makoto Onuki5eef50d2017-03-02 16:38:45 -08002079 * in order to check other profile's status.
2080 * Since Android Nougat MR1 (SDK version >= 25;
Andrew Solovay8a788f42018-12-12 14:25:47 -08002081 * {@link android.os.Build.VERSION_CODES#N_MR1}), the restriction has been relaxed, and now
2082 * it'll accept any {@link android.os.UserHandle} within the same profile group as the caller.
Fyodor Kupolovcdb3c2f2017-02-10 11:48:32 -08002083 *
Dianne Hackborna8a9bd62012-10-09 15:36:59 -07002084 * @param user The user to retrieve the running state for.
2085 */
Valentin Iftime89df4c82019-08-23 13:02:50 +02002086 @RequiresPermission(anyOf = {Manifest.permission.MANAGE_USERS,
2087 Manifest.permission.INTERACT_ACROSS_USERS}, conditional = true)
Dianne Hackborna8a9bd62012-10-09 15:36:59 -07002088 public boolean isUserRunning(UserHandle user) {
Jeff Sharkey0e62384c2016-01-13 18:52:55 -07002089 return isUserRunning(user.getIdentifier());
2090 }
2091
2092 /** {@hide} */
Valentin Iftime89df4c82019-08-23 13:02:50 +02002093 @RequiresPermission(anyOf = {Manifest.permission.MANAGE_USERS,
2094 Manifest.permission.INTERACT_ACROSS_USERS}, conditional = true)
Fyodor Kupolov2e7e0962016-12-01 18:09:17 -08002095 public boolean isUserRunning(@UserIdInt int userId) {
Dianne Hackborna8a9bd62012-10-09 15:36:59 -07002096 try {
Fyodor Kupolov2e7e0962016-12-01 18:09:17 -08002097 return mService.isUserRunning(userId);
Jeff Sharkey27b2e692016-02-25 17:40:12 -07002098 } catch (RemoteException re) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07002099 throw re.rethrowFromSystemServer();
Dianne Hackborna8a9bd62012-10-09 15:36:59 -07002100 }
2101 }
2102
2103 /**
2104 * Return whether the given user is actively running <em>or</em> stopping.
2105 * This is like {@link #isUserRunning(UserHandle)}, but will also return
2106 * true if the user had been running but is in the process of being stopped
2107 * (but is not yet fully stopped, and still running some code).
Makoto Onuki5eef50d2017-03-02 16:38:45 -08002108 *
2109 * <p>Note prior to Android Nougat MR1 (SDK version <= 24;
Andrew Solovay8a788f42018-12-12 14:25:47 -08002110 * {@link android.os.Build.VERSION_CODES#N}, this API required a system permission
Makoto Onuki5eef50d2017-03-02 16:38:45 -08002111 * in order to check other profile's status.
2112 * Since Android Nougat MR1 (SDK version >= 25;
Andrew Solovay8a788f42018-12-12 14:25:47 -08002113 * {@link android.os.Build.VERSION_CODES#N_MR1}), the restriction has been relaxed, and now
2114 * it'll accept any {@link android.os.UserHandle} within the same profile group as the caller.
Fyodor Kupolovcdb3c2f2017-02-10 11:48:32 -08002115 *
Dianne Hackborna8a9bd62012-10-09 15:36:59 -07002116 * @param user The user to retrieve the running state for.
2117 */
Valentin Iftime89df4c82019-08-23 13:02:50 +02002118 @RequiresPermission(anyOf = {Manifest.permission.MANAGE_USERS,
2119 Manifest.permission.INTERACT_ACROSS_USERS}, conditional = true)
Dianne Hackborna8a9bd62012-10-09 15:36:59 -07002120 public boolean isUserRunningOrStopping(UserHandle user) {
2121 try {
Jeff Sharkeye17ac152015-11-06 22:40:29 -08002122 // TODO: reconcile stopped vs stopping?
Sudheer Shankadc589ac2016-11-10 15:30:17 -08002123 return ActivityManager.getService().isUserRunning(
Jeff Sharkeye17ac152015-11-06 22:40:29 -08002124 user.getIdentifier(), ActivityManager.FLAG_OR_STOPPED);
Jeff Sharkey27b2e692016-02-25 17:40:12 -07002125 } catch (RemoteException re) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07002126 throw re.rethrowFromSystemServer();
Dianne Hackborna8a9bd62012-10-09 15:36:59 -07002127 }
2128 }
2129
Jeff Sharkey0825ab22015-12-02 13:04:49 -07002130 /**
Jeff Sharkeycf3f0a12016-03-17 19:57:58 -06002131 * Return whether the calling user is running in an "unlocked" state.
2132 * <p>
2133 * On devices with direct boot, a user is unlocked only after they've
2134 * entered their credentials (such as a lock pattern or PIN). On devices
2135 * without direct boot, a user is unlocked as soon as it starts.
2136 * <p>
2137 * When a user is locked, only device-protected data storage is available.
2138 * When a user is unlocked, both device-protected and credential-protected
2139 * private app data storage is available.
Jeff Sharkey8a372a02016-03-16 16:25:45 -06002140 *
2141 * @see Intent#ACTION_USER_UNLOCKED
2142 * @see Context#createDeviceProtectedStorageContext()
Jeff Sharkeyb6423872015-12-11 11:11:58 -07002143 */
2144 public boolean isUserUnlocked() {
2145 return isUserUnlocked(Process.myUserHandle());
2146 }
2147
2148 /**
Jeff Sharkeycf3f0a12016-03-17 19:57:58 -06002149 * Return whether the given user is running in an "unlocked" state.
2150 * <p>
2151 * On devices with direct boot, a user is unlocked only after they've
2152 * entered their credentials (such as a lock pattern or PIN). On devices
2153 * without direct boot, a user is unlocked as soon as it starts.
2154 * <p>
2155 * When a user is locked, only device-protected data storage is available.
2156 * When a user is unlocked, both device-protected and credential-protected
2157 * private app data storage is available.
Fyodor Kupolovcdb3c2f2017-02-10 11:48:32 -08002158 * <p>Requires {@code android.permission.MANAGE_USERS} or
2159 * {@code android.permission.INTERACT_ACROSS_USERS}, otherwise specified {@link UserHandle user}
2160 * must be the calling user or a managed profile associated with it.
Jeff Sharkeyb6423872015-12-11 11:11:58 -07002161 *
2162 * @param user to retrieve the unlocked state for.
Jeff Sharkey8a372a02016-03-16 16:25:45 -06002163 * @see Intent#ACTION_USER_UNLOCKED
2164 * @see Context#createDeviceProtectedStorageContext()
Jeff Sharkeyb6423872015-12-11 11:11:58 -07002165 */
Valentin Iftime89df4c82019-08-23 13:02:50 +02002166 @RequiresPermission(anyOf = {Manifest.permission.MANAGE_USERS,
2167 Manifest.permission.INTERACT_ACROSS_USERS}, conditional = true)
Jeff Sharkeyb6423872015-12-11 11:11:58 -07002168 public boolean isUserUnlocked(UserHandle user) {
Jeff Sharkey0999c0d2015-12-17 15:12:22 -07002169 return isUserUnlocked(user.getIdentifier());
2170 }
2171
2172 /** {@hide} */
Andrei Onea24ec3212019-03-15 17:35:05 +00002173 @UnsupportedAppUsage
Valentin Iftime89df4c82019-08-23 13:02:50 +02002174 @RequiresPermission(anyOf = {Manifest.permission.MANAGE_USERS,
2175 Manifest.permission.INTERACT_ACROSS_USERS}, conditional = true)
Jeff Sharkey8588bc12016-01-06 16:47:42 -07002176 public boolean isUserUnlocked(@UserIdInt int userId) {
Jeff Sharkeyce18c812016-04-27 16:00:41 -06002177 try {
Fyodor Kupolov2e7e0962016-12-01 18:09:17 -08002178 return mService.isUserUnlocked(userId);
Jeff Sharkeyce18c812016-04-27 16:00:41 -06002179 } catch (RemoteException re) {
2180 throw re.rethrowFromSystemServer();
2181 }
2182 }
2183
Roshan Piusf6c8cd32019-10-17 13:03:18 -07002184 /**
2185 * Return whether the provided user is already running in an
2186 * "unlocked" state or in the process of unlocking.
2187 * <p>
2188 * On devices with direct boot, a user is unlocked only after they've
2189 * entered their credentials (such as a lock pattern or PIN). On devices
2190 * without direct boot, a user is unlocked as soon as it starts.
2191 * <p>
2192 * When a user is locked, only device-protected data storage is available.
2193 * When a user is unlocked, both device-protected and credential-protected
2194 * private app data storage is available.
2195 *
2196 * <p>Requires {@code android.permission.MANAGE_USERS} or
2197 * {@code android.permission.INTERACT_ACROSS_USERS}, otherwise specified {@link UserHandle user}
2198 * must be the calling user or a profile associated with it.
2199 *
2200 * @hide
2201 */
2202 @SystemApi
Valentin Iftime89df4c82019-08-23 13:02:50 +02002203 @RequiresPermission(anyOf = {Manifest.permission.MANAGE_USERS,
2204 Manifest.permission.INTERACT_ACROSS_USERS}, conditional = true)
Roshan Piusf6c8cd32019-10-17 13:03:18 -07002205 public boolean isUserUnlockingOrUnlocked(@NonNull UserHandle user) {
Jeff Sharkeyce18c812016-04-27 16:00:41 -06002206 return isUserUnlockingOrUnlocked(user.getIdentifier());
2207 }
2208
2209 /** {@hide} */
Valentin Iftime89df4c82019-08-23 13:02:50 +02002210 @RequiresPermission(anyOf = {Manifest.permission.MANAGE_USERS,
2211 Manifest.permission.INTERACT_ACROSS_USERS}, conditional = true)
Jeff Sharkeyce18c812016-04-27 16:00:41 -06002212 public boolean isUserUnlockingOrUnlocked(@UserIdInt int userId) {
2213 try {
Fyodor Kupolovc413f702016-10-06 17:11:14 -07002214 return mService.isUserUnlockingOrUnlocked(userId);
Jeff Sharkeyce18c812016-04-27 16:00:41 -06002215 } catch (RemoteException re) {
2216 throw re.rethrowFromSystemServer();
2217 }
Jeff Sharkeyb6423872015-12-11 11:11:58 -07002218 }
2219
2220 /**
Makoto Onuki73dded22017-12-20 13:14:48 +09002221 * Return the time when the calling user started in elapsed milliseconds since boot,
2222 * or 0 if not started.
2223 *
2224 * @hide
2225 */
Andrei Onea24ec3212019-03-15 17:35:05 +00002226 @UnsupportedAppUsage
Makoto Onuki73dded22017-12-20 13:14:48 +09002227 public long getUserStartRealtime() {
2228 try {
2229 return mService.getUserStartRealtime();
2230 } catch (RemoteException re) {
2231 throw re.rethrowFromSystemServer();
2232 }
2233 }
2234
2235 /**
2236 * Return the time when the calling user was unlocked elapsed milliseconds since boot,
2237 * or 0 if not unlocked.
2238 *
2239 * @hide
2240 */
Andrei Onea24ec3212019-03-15 17:35:05 +00002241 @UnsupportedAppUsage
Makoto Onuki73dded22017-12-20 13:14:48 +09002242 public long getUserUnlockRealtime() {
2243 try {
2244 return mService.getUserUnlockRealtime();
2245 } catch (RemoteException re) {
2246 throw re.rethrowFromSystemServer();
2247 }
2248 }
2249
2250 /**
Amith Yamasani258848d2012-08-10 17:06:33 -07002251 * Returns the UserInfo object describing a specific user.
Bookatzf56f2582019-09-04 16:06:41 -07002252 * @param userId the user handle of the user whose information is being requested.
Amith Yamasani258848d2012-08-10 17:06:33 -07002253 * @return the UserInfo object for a specific user.
2254 * @hide
Dianne Hackbornb26306a2012-10-24 15:22:21 -07002255 */
Andrei Onea24ec3212019-03-15 17:35:05 +00002256 @UnsupportedAppUsage
Valentin Iftime89df4c82019-08-23 13:02:50 +02002257 @RequiresPermission(anyOf = {Manifest.permission.MANAGE_USERS,
2258 Manifest.permission.CREATE_USERS})
Bookatzf56f2582019-09-04 16:06:41 -07002259 public UserInfo getUserInfo(@UserIdInt int userId) {
Amith Yamasani258848d2012-08-10 17:06:33 -07002260 try {
Bookatzf56f2582019-09-04 16:06:41 -07002261 return mService.getUserInfo(userId);
Amith Yamasani258848d2012-08-10 17:06:33 -07002262 } catch (RemoteException re) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07002263 throw re.rethrowFromSystemServer();
Amith Yamasani258848d2012-08-10 17:06:33 -07002264 }
2265 }
2266
Amith Yamasani71e6c692013-03-24 17:39:28 -07002267 /**
Zoltan Szatmary-Bane7834602016-04-08 18:41:11 +01002268 * @hide
2269 *
2270 * Returns who set a user restriction on a user.
Zoltan Szatmary-Bane7834602016-04-08 18:41:11 +01002271 * @param restrictionKey the string key representing the restriction
2272 * @param userHandle the UserHandle of the user for whom to retrieve the restrictions.
2273 * @return The source of user restriction. Any combination of {@link #RESTRICTION_NOT_SET},
2274 * {@link #RESTRICTION_SOURCE_SYSTEM}, {@link #RESTRICTION_SOURCE_DEVICE_OWNER}
2275 * and {@link #RESTRICTION_SOURCE_PROFILE_OWNER}
Pavel Grafov6a40f092016-10-25 15:46:51 +01002276 * @deprecated use {@link #getUserRestrictionSources(String, int)} instead.
Zoltan Szatmary-Bane7834602016-04-08 18:41:11 +01002277 */
Pavel Grafov6a40f092016-10-25 15:46:51 +01002278 @Deprecated
Zoltan Szatmary-Bane7834602016-04-08 18:41:11 +01002279 @SystemApi
2280 @UserRestrictionSource
Jeff Sharkeyd86b8fe2017-06-02 17:36:26 -06002281 @RequiresPermission(android.Manifest.permission.MANAGE_USERS)
Roshan Pius3eac0b92019-12-05 09:35:36 -08002282 public int getUserRestrictionSource(@UserRestrictionKey String restrictionKey,
2283 UserHandle userHandle) {
Zoltan Szatmary-Bane7834602016-04-08 18:41:11 +01002284 try {
2285 return mService.getUserRestrictionSource(restrictionKey, userHandle.getIdentifier());
2286 } catch (RemoteException re) {
2287 throw re.rethrowFromSystemServer();
2288 }
2289 }
2290
2291 /**
Pavel Grafov6a40f092016-10-25 15:46:51 +01002292 * @hide
2293 *
2294 * Returns a list of users who set a user restriction on a given user.
Pavel Grafov6a40f092016-10-25 15:46:51 +01002295 * @param restrictionKey the string key representing the restriction
2296 * @param userHandle the UserHandle of the user for whom to retrieve the restrictions.
2297 * @return a list of user ids enforcing this restriction.
2298 */
2299 @SystemApi
Jeff Sharkeyd86b8fe2017-06-02 17:36:26 -06002300 @RequiresPermission(android.Manifest.permission.MANAGE_USERS)
Pavel Grafov6a40f092016-10-25 15:46:51 +01002301 public List<EnforcingUser> getUserRestrictionSources(
Roshan Pius3eac0b92019-12-05 09:35:36 -08002302 @UserRestrictionKey String restrictionKey, UserHandle userHandle) {
Pavel Grafov6a40f092016-10-25 15:46:51 +01002303 try {
2304 return mService.getUserRestrictionSources(restrictionKey, userHandle.getIdentifier());
2305 } catch (RemoteException re) {
2306 throw re.rethrowFromSystemServer();
2307 }
2308 }
2309
2310 /**
Amith Yamasani71e6c692013-03-24 17:39:28 -07002311 * Returns the user-wide restrictions imposed on this user.
2312 * @return a Bundle containing all the restrictions.
2313 */
Amith Yamasanie4cf7342012-12-17 11:12:09 -08002314 public Bundle getUserRestrictions() {
2315 return getUserRestrictions(Process.myUserHandle());
2316 }
2317
Amith Yamasani71e6c692013-03-24 17:39:28 -07002318 /**
2319 * Returns the user-wide restrictions imposed on the user specified by <code>userHandle</code>.
2320 * @param userHandle the UserHandle of the user for whom to retrieve the restrictions.
2321 * @return a Bundle containing all the restrictions.
Roshan Pius7969c802019-10-21 19:58:57 -07002322 *
2323 * <p>Requires {@code android.permission.MANAGE_USERS} or
2324 * {@code android.permission.INTERACT_ACROSS_USERS}, otherwise specified {@link UserHandle user}
2325 * must be the calling user or a managed profile associated with it.
Amith Yamasani71e6c692013-03-24 17:39:28 -07002326 */
Roshan Pius7969c802019-10-21 19:58:57 -07002327 @RequiresPermission(anyOf = {
2328 android.Manifest.permission.MANAGE_USERS,
2329 android.Manifest.permission.INTERACT_ACROSS_USERS}, conditional = true)
Amith Yamasanie4cf7342012-12-17 11:12:09 -08002330 public Bundle getUserRestrictions(UserHandle userHandle) {
2331 try {
2332 return mService.getUserRestrictions(userHandle.getIdentifier());
2333 } catch (RemoteException re) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07002334 throw re.rethrowFromSystemServer();
Amith Yamasanie4cf7342012-12-17 11:12:09 -08002335 }
2336 }
2337
Zoltan Szatmary-Ban3bbcedd2015-11-26 13:45:51 +00002338 /**
2339 * @hide
2340 * Returns whether the given user has been disallowed from performing certain actions
Irina Dumitrescu4638edd2018-09-05 14:08:33 +01002341 * or setting certain settings through UserManager (e.g. this type of restriction would prevent
2342 * the guest user from doing certain things, such as making calls). This method disregards
2343 * restrictions set by device policy.
Zoltan Szatmary-Ban3bbcedd2015-11-26 13:45:51 +00002344 * @param restrictionKey the string key representing the restriction
2345 * @param userHandle the UserHandle of the user for whom to retrieve the restrictions.
2346 */
Andrei Onea24ec3212019-03-15 17:35:05 +00002347 @UnsupportedAppUsage
Valentin Iftime89df4c82019-08-23 13:02:50 +02002348 @RequiresPermission(Manifest.permission.MANAGE_USERS)
Roshan Pius3eac0b92019-12-05 09:35:36 -08002349 public boolean hasBaseUserRestriction(@UserRestrictionKey String restrictionKey,
2350 UserHandle userHandle) {
Zoltan Szatmary-Ban3bbcedd2015-11-26 13:45:51 +00002351 try {
2352 return mService.hasBaseUserRestriction(restrictionKey, userHandle.getIdentifier());
2353 } catch (RemoteException re) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07002354 throw re.rethrowFromSystemServer();
Zoltan Szatmary-Ban3bbcedd2015-11-26 13:45:51 +00002355 }
2356 }
2357
Amith Yamasani71e6c692013-03-24 17:39:28 -07002358 /**
Makoto Onukia3c12502015-10-28 10:18:32 -07002359 * This will no longer work. Device owners and profile owners should use
2360 * {@link DevicePolicyManager#addUserRestriction(ComponentName, String)} instead.
Amith Yamasani71e6c692013-03-24 17:39:28 -07002361 */
Makoto Onukia3c12502015-10-28 10:18:32 -07002362 // System apps should use UserManager.setUserRestriction() instead.
Julia Reynolds3d9eb782014-08-11 16:40:08 -04002363 @Deprecated
Amith Yamasanie4cf7342012-12-17 11:12:09 -08002364 public void setUserRestrictions(Bundle restrictions) {
Makoto Onuki068c54a2015-10-13 14:34:03 -07002365 throw new UnsupportedOperationException("This method is no longer supported");
Amith Yamasanie4cf7342012-12-17 11:12:09 -08002366 }
2367
Amith Yamasani71e6c692013-03-24 17:39:28 -07002368 /**
Makoto Onukia3c12502015-10-28 10:18:32 -07002369 * This will no longer work. Device owners and profile owners should use
2370 * {@link DevicePolicyManager#addUserRestriction(ComponentName, String)} instead.
Amith Yamasani71e6c692013-03-24 17:39:28 -07002371 */
Makoto Onukia3c12502015-10-28 10:18:32 -07002372 // System apps should use UserManager.setUserRestriction() instead.
Julia Reynolds3d9eb782014-08-11 16:40:08 -04002373 @Deprecated
Amith Yamasanie4cf7342012-12-17 11:12:09 -08002374 public void setUserRestrictions(Bundle restrictions, UserHandle userHandle) {
Makoto Onuki068c54a2015-10-13 14:34:03 -07002375 throw new UnsupportedOperationException("This method is no longer supported");
Amith Yamasanie4cf7342012-12-17 11:12:09 -08002376 }
2377
Amith Yamasani71e6c692013-03-24 17:39:28 -07002378 /**
2379 * Sets the value of a specific restriction.
Amith Yamasanibe465322014-04-24 13:45:17 -07002380 * Requires the MANAGE_USERS permission.
Amith Yamasani71e6c692013-03-24 17:39:28 -07002381 * @param key the key of the restriction
2382 * @param value the value for the restriction
Julia Reynolds3d9eb782014-08-11 16:40:08 -04002383 * @deprecated use {@link android.app.admin.DevicePolicyManager#addUserRestriction(
2384 * android.content.ComponentName, String)} or
2385 * {@link android.app.admin.DevicePolicyManager#clearUserRestriction(
2386 * android.content.ComponentName, String)} instead.
Amith Yamasani71e6c692013-03-24 17:39:28 -07002387 */
Julia Reynolds3d9eb782014-08-11 16:40:08 -04002388 @Deprecated
Valentin Iftime89df4c82019-08-23 13:02:50 +02002389 @RequiresPermission(Manifest.permission.MANAGE_USERS)
Amith Yamasani71e6c692013-03-24 17:39:28 -07002390 public void setUserRestriction(String key, boolean value) {
Makoto Onuki068c54a2015-10-13 14:34:03 -07002391 setUserRestriction(key, value, Process.myUserHandle());
Amith Yamasani71e6c692013-03-24 17:39:28 -07002392 }
2393
2394 /**
2395 * @hide
2396 * Sets the value of a specific restriction on a specific user.
Amith Yamasani71e6c692013-03-24 17:39:28 -07002397 * @param key the key of the restriction
2398 * @param value the value for the restriction
2399 * @param userHandle the user whose restriction is to be changed.
Julia Reynolds3d9eb782014-08-11 16:40:08 -04002400 * @deprecated use {@link android.app.admin.DevicePolicyManager#addUserRestriction(
2401 * android.content.ComponentName, String)} or
2402 * {@link android.app.admin.DevicePolicyManager#clearUserRestriction(
2403 * android.content.ComponentName, String)} instead.
Amith Yamasani71e6c692013-03-24 17:39:28 -07002404 */
Julia Reynolds3d9eb782014-08-11 16:40:08 -04002405 @Deprecated
Valentin Iftime89df4c82019-08-23 13:02:50 +02002406 @RequiresPermission(Manifest.permission.MANAGE_USERS)
Maggie Benthall67944582013-02-22 14:58:27 -05002407 public void setUserRestriction(String key, boolean value, UserHandle userHandle) {
Fyodor Kupolovb5013302015-04-17 17:59:14 -07002408 try {
2409 mService.setUserRestriction(key, value, userHandle.getIdentifier());
2410 } catch (RemoteException re) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07002411 throw re.rethrowFromSystemServer();
Fyodor Kupolovb5013302015-04-17 17:59:14 -07002412 }
Maggie Benthall67944582013-02-22 14:58:27 -05002413 }
2414
Amith Yamasani258848d2012-08-10 17:06:33 -07002415 /**
Maggie Benthalla12fccf2013-03-14 18:02:12 -04002416 * Returns whether the current user has been disallowed from performing certain actions
2417 * or setting certain settings.
Julia Reynolds2b2cf722014-06-06 11:41:04 -04002418 *
2419 * @param restrictionKey The string key representing the restriction.
2420 * @return {@code true} if the current user has the given restriction, {@code false} otherwise.
Maggie Benthalla12fccf2013-03-14 18:02:12 -04002421 */
Roshan Pius3eac0b92019-12-05 09:35:36 -08002422 public boolean hasUserRestriction(@UserRestrictionKey String restrictionKey) {
Roshan Pius7969c802019-10-21 19:58:57 -07002423 return hasUserRestrictionForUser(restrictionKey, Process.myUserHandle());
David Christieb12ba932013-09-03 17:15:28 -07002424 }
2425
2426 /**
2427 * @hide
2428 * Returns whether the given user has been disallowed from performing certain actions
2429 * or setting certain settings.
2430 * @param restrictionKey the string key representing the restriction
2431 * @param userHandle the UserHandle of the user for whom to retrieve the restrictions.
2432 */
Andrei Onea24ec3212019-03-15 17:35:05 +00002433 @UnsupportedAppUsage
Roshan Pius3eac0b92019-12-05 09:35:36 -08002434 public boolean hasUserRestriction(@UserRestrictionKey String restrictionKey,
2435 UserHandle userHandle) {
Roshan Pius7969c802019-10-21 19:58:57 -07002436 return hasUserRestrictionForUser(restrictionKey, userHandle);
2437 }
2438
2439 /**
2440 * Returns whether the given user has been disallowed from performing certain actions
2441 * or setting certain settings.
2442 * @param restrictionKey the string key representing the restriction
2443 * @param userHandle the UserHandle of the user for whom to retrieve the restrictions.
2444 *
2445 * <p>Requires {@code android.permission.MANAGE_USERS} or
2446 * {@code android.permission.INTERACT_ACROSS_USERS}, otherwise specified {@link UserHandle user}
2447 * must be the calling user or a managed profile associated with it.
2448 *
2449 * @hide
2450 */
2451 @SystemApi
2452 @RequiresPermission(anyOf = {
2453 android.Manifest.permission.MANAGE_USERS,
2454 android.Manifest.permission.INTERACT_ACROSS_USERS}, conditional = true)
Roshan Pius3eac0b92019-12-05 09:35:36 -08002455 public boolean hasUserRestrictionForUser(@NonNull @UserRestrictionKey String restrictionKey,
Roshan Pius7969c802019-10-21 19:58:57 -07002456 @NonNull UserHandle userHandle) {
Amith Yamasani8cd28b52014-06-08 17:54:27 -07002457 try {
Roshan Pius7969c802019-10-21 19:58:57 -07002458 return mService.hasUserRestriction(restrictionKey, userHandle.getIdentifier());
Amith Yamasani8cd28b52014-06-08 17:54:27 -07002459 } catch (RemoteException re) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07002460 throw re.rethrowFromSystemServer();
Amith Yamasani8cd28b52014-06-08 17:54:27 -07002461 }
Maggie Benthalla12fccf2013-03-14 18:02:12 -04002462 }
2463
2464 /**
Makoto Onukiacc50462018-02-14 14:13:49 -08002465 * @hide
2466 * Returns whether any user on the device has the given user restriction set.
2467 */
Roshan Pius3eac0b92019-12-05 09:35:36 -08002468 public boolean hasUserRestrictionOnAnyUser(@UserRestrictionKey String restrictionKey) {
Makoto Onukiacc50462018-02-14 14:13:49 -08002469 try {
2470 return mService.hasUserRestrictionOnAnyUser(restrictionKey);
2471 } catch (RemoteException re) {
2472 throw re.rethrowFromSystemServer();
2473 }
2474 }
2475
2476 /**
Christopher Tate65fb2e42019-10-11 17:00:23 -07002477 * @hide
2478 *
2479 * Checks whether changing the given setting to the given value is prohibited
2480 * by the corresponding user restriction in the given user.
2481 *
2482 * May only be called by the OS itself.
2483 *
2484 * @return {@code true} if the change is prohibited, {@code false} if the change is allowed.
2485 */
2486 public boolean isSettingRestrictedForUser(String setting, @UserIdInt int userId,
2487 String value, int callingUid) {
2488 try {
2489 return mService.isSettingRestrictedForUser(setting, userId, value, callingUid);
2490 } catch (RemoteException re) {
2491 throw re.rethrowFromSystemServer();
2492 }
2493 }
2494
2495 /**
2496 * @hide
2497 * Register a binder callback for user restrictions changes.
2498 * May only be called by the OS itself.
2499 */
2500 public void addUserRestrictionsListener(final IUserRestrictionsListener listener) {
2501 try {
2502 mService.addUserRestrictionsListener(listener);
2503 } catch (RemoteException re) {
2504 throw re.rethrowFromSystemServer();
2505 }
2506 }
2507
2508 /**
Dianne Hackborn33f9cb82012-10-04 17:15:10 -07002509 * Return the serial number for a user. This is a device-unique
Dianne Hackbornb26306a2012-10-24 15:22:21 -07002510 * number assigned to that user; if the user is deleted and then a new
2511 * user created, the new users will not be given the same serial number.
Dianne Hackborn33f9cb82012-10-04 17:15:10 -07002512 * @param user The user whose serial number is to be retrieved.
Dianne Hackbornb26306a2012-10-24 15:22:21 -07002513 * @return The serial number of the given user; returns -1 if the
2514 * given UserHandle does not exist.
Dianne Hackborn33f9cb82012-10-04 17:15:10 -07002515 * @see #getUserForSerialNumber(long)
2516 */
2517 public long getSerialNumberForUser(UserHandle user) {
2518 return getUserSerialNumber(user.getIdentifier());
2519 }
2520
2521 /**
2522 * Return the user associated with a serial number previously
2523 * returned by {@link #getSerialNumberForUser(UserHandle)}.
2524 * @param serialNumber The serial number of the user that is being
2525 * retrieved.
2526 * @return Return the user associated with the serial number, or null
2527 * if there is not one.
2528 * @see #getSerialNumberForUser(UserHandle)
2529 */
2530 public UserHandle getUserForSerialNumber(long serialNumber) {
Fyodor Kupolovef249092015-05-06 13:18:46 -07002531 int ident = getUserHandle((int) serialNumber);
Dianne Hackborn33f9cb82012-10-04 17:15:10 -07002532 return ident >= 0 ? new UserHandle(ident) : null;
2533 }
2534
2535 /**
Xiaohui Chencfe64c82015-07-16 14:30:50 -07002536 * Creates a user with the specified name and options. For non-admin users, default user
Bookatz029832a2019-10-04 16:50:22 -07002537 * restrictions are going to be applied.
2538 * Requires {@link android.Manifest.permission#MANAGE_USERS} permission.
2539 *
2540 * @param name the user's name
2541 * @param flags UserInfo flags that identify the type of user and other properties.
2542 * @see UserInfo
2543 *
2544 * @return the UserInfo object for the created user, or null if the user could not be created.
2545 * @throws IllegalArgumentException if flags do not correspond to a valid user type.
2546 * @deprecated Use {@link #createUser(String, String, int)} instead.
2547 * @hide
2548 */
2549 @UnsupportedAppUsage
2550 @Deprecated
2551 public @Nullable UserInfo createUser(@Nullable String name, @UserInfoFlag int flags) {
2552 return createUser(name, UserInfo.getDefaultUserType(flags), flags);
2553 }
2554
2555 /**
2556 * Creates a user with the specified name and options. For non-admin users, default user
Felipe Lemec1ca4412019-09-11 09:23:26 -07002557 * restrictions will be applied.
2558 *
Valentin Iftime89df4c82019-08-23 13:02:50 +02002559 * <p>Requires {@link android.Manifest.permission#MANAGE_USERS}.
2560 * {@link android.Manifest.permission#CREATE_USERS} suffices if flags are in
2561 * com.android.server.pm.UserManagerService#ALLOWED_FLAGS_FOR_CREATE_USERS_PERMISSION}.
Amith Yamasani258848d2012-08-10 17:06:33 -07002562 *
Valentin Iftime89df4c82019-08-23 13:02:50 +02002563 * @param name the user's name
Bookatz029832a2019-10-04 16:50:22 -07002564 * @param userType the type of user, such as {@link UserManager#USER_TYPE_FULL_GUEST}.
Valentin Iftime89df4c82019-08-23 13:02:50 +02002565 * @param flags UserInfo flags that specify user properties.
2566 * @return the {@link UserInfo} object for the created user,
2567 * or throws {@link UserOperationException} if the user could not be created
2568 * and calling app is targeting {@link android.os.Build.VERSION_CODES#R} or above
2569 * (otherwise returns {@code null}).
Amith Yamasani258848d2012-08-10 17:06:33 -07002570 *
Valentin Iftime89df4c82019-08-23 13:02:50 +02002571 * @throws UserOperationException if the user could not be created and the calling app is
2572 * targeting {@link android.os.Build.VERSION_CODES#R} or above.
Amith Yamasani258848d2012-08-10 17:06:33 -07002573 * @hide
Valentin Iftime89df4c82019-08-23 13:02:50 +02002574 * @see UserInfo
Amith Yamasani258848d2012-08-10 17:06:33 -07002575 */
Valentin Iftime89df4c82019-08-23 13:02:50 +02002576 @RequiresPermission(anyOf = {Manifest.permission.MANAGE_USERS,
2577 Manifest.permission.CREATE_USERS})
Bookatz029832a2019-10-04 16:50:22 -07002578 public @Nullable UserInfo createUser(@Nullable String name, @NonNull String userType,
2579 @UserInfoFlag int flags) {
Amith Yamasani258848d2012-08-10 17:06:33 -07002580 try {
Valentin Iftime89df4c82019-08-23 13:02:50 +02002581 return mService.createUserWithThrow(name, userType, flags);
2582 } catch (ServiceSpecificException e) {
2583 return returnNullOrThrowUserOperationException(e,
2584 mContext.getApplicationInfo().targetSdkVersion >= Build.VERSION_CODES.R);
Amith Yamasani258848d2012-08-10 17:06:33 -07002585 } catch (RemoteException re) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07002586 throw re.rethrowFromSystemServer();
Amith Yamasani258848d2012-08-10 17:06:33 -07002587 }
2588 }
2589
2590 /**
Bookatz029832a2019-10-04 16:50:22 -07002591 * Pre-creates a user of the specified type. For non-admin users, default user
Felipe Lemec1ca4412019-09-11 09:23:26 -07002592 * restrictions will be applied.
2593 *
2594 * <p>This method can be used by OEMs to "warm" up the user creation by pre-creating some users
2595 * at the first boot, so they when the "real" user is created (for example,
Bookatz029832a2019-10-04 16:50:22 -07002596 * by {@link #createUser(String, String, int)} or {@link #createGuest(Context, String)}), it
2597 * takes less time.
Felipe Lemec1ca4412019-09-11 09:23:26 -07002598 *
jovanak8508f2f2019-10-28 09:56:29 -07002599 * <p>This method completes the majority of work necessary for user creation: it
2600 * creates user data, CE and DE encryption keys, app data directories, initializes the user and
2601 * grants default permissions. When pre-created users become "real" users, only then are
2602 * components notified of new user creation by firing user creation broadcasts.
2603 *
2604 * <p>All pre-created users are removed during system upgrade.
2605 *
Valentin Iftime89df4c82019-08-23 13:02:50 +02002606 * <p>Requires {@link android.Manifest.permission#MANAGE_USERS}.
2607 * {@link android.Manifest.permission#CREATE_USERS} suffices if flags are in
2608 * com.android.server.pm.UserManagerService#ALLOWED_FLAGS_FOR_CREATE_USERS_PERMISSION}.
Felipe Lemec1ca4412019-09-11 09:23:26 -07002609 *
Bookatz029832a2019-10-04 16:50:22 -07002610 * @param userType the type of user, such as {@link UserManager#USER_TYPE_FULL_GUEST}.
Valentin Iftime89df4c82019-08-23 13:02:50 +02002611 * @return the {@link UserInfo} object for the created user,
2612 * or throws {@link UserOperationException} if the user could not be created
2613 * and calling app is targeting {@link android.os.Build.VERSION_CODES#R} or above
2614 * (otherwise returns {@code null}).
Felipe Lemec1ca4412019-09-11 09:23:26 -07002615 *
Valentin Iftime89df4c82019-08-23 13:02:50 +02002616 * @throws UserOperationException if the user could not be created and the calling app is
2617 * targeting {@link android.os.Build.VERSION_CODES#R} or above.
Felipe Lemec1ca4412019-09-11 09:23:26 -07002618 *
2619 * @hide
2620 */
Valentin Iftime89df4c82019-08-23 13:02:50 +02002621 @RequiresPermission(anyOf = {Manifest.permission.MANAGE_USERS,
2622 Manifest.permission.CREATE_USERS})
2623 public @Nullable UserInfo preCreateUser(@NonNull String userType)
2624 throws UserOperationException {
Felipe Lemec1ca4412019-09-11 09:23:26 -07002625 try {
Valentin Iftime89df4c82019-08-23 13:02:50 +02002626 return mService.preCreateUserWithThrow(userType);
2627 } catch (ServiceSpecificException e) {
2628 return returnNullOrThrowUserOperationException(e,
2629 mContext.getApplicationInfo().targetSdkVersion >= Build.VERSION_CODES.R);
Felipe Lemec1ca4412019-09-11 09:23:26 -07002630 } catch (RemoteException re) {
2631 throw re.rethrowFromSystemServer();
2632 }
2633 }
2634
2635 /**
Amith Yamasani1e9c2182014-06-11 17:25:51 -07002636 * Creates a guest user and configures it.
2637 * @param context an application context
2638 * @param name the name to set for the user
Valentin Iftime89df4c82019-08-23 13:02:50 +02002639 * @return the {@link UserInfo} object for the created user,
2640 * or throws {@link UserOperationException} if the user could not be created
2641 * and calling app is targeting {@link android.os.Build.VERSION_CODES#R} or above
2642 * (otherwise returns {@code null}).
2643 *
2644 * @throws UserOperationException if the user could not be created and the calling app is
2645 * targeting {@link android.os.Build.VERSION_CODES#R} or above.
Amith Yamasani1e9c2182014-06-11 17:25:51 -07002646 * @hide
2647 */
Valentin Iftime89df4c82019-08-23 13:02:50 +02002648 @RequiresPermission(anyOf = {Manifest.permission.MANAGE_USERS,
2649 Manifest.permission.CREATE_USERS})
2650 public UserInfo createGuest(Context context, String name) throws UserOperationException {
Makoto Onuki068c54a2015-10-13 14:34:03 -07002651 UserInfo guest = null;
2652 try {
Valentin Iftime89df4c82019-08-23 13:02:50 +02002653 guest = mService.createUserWithThrow(name, USER_TYPE_FULL_GUEST, 0);
Makoto Onuki068c54a2015-10-13 14:34:03 -07002654 if (guest != null) {
2655 Settings.Secure.putStringForUser(context.getContentResolver(),
2656 Settings.Secure.SKIP_FIRST_USE_HINTS, "1", guest.id);
Amith Yamasanibf3a9462014-07-28 14:26:42 -07002657 }
Valentin Iftime89df4c82019-08-23 13:02:50 +02002658 } catch (ServiceSpecificException e) {
2659 return returnNullOrThrowUserOperationException(e,
2660 context.getApplicationInfo().targetSdkVersion >= Build.VERSION_CODES.R);
Makoto Onuki068c54a2015-10-13 14:34:03 -07002661 } catch (RemoteException re) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07002662 throw re.rethrowFromSystemServer();
Amith Yamasani1e9c2182014-06-11 17:25:51 -07002663 }
2664 return guest;
2665 }
2666
Amith Yamasaniaa6634e2014-10-06 14:20:28 -07002667 /**
Anthony Hugh5d7f9052019-11-13 16:48:28 -08002668 * Gets the existing guest user if it exists. This does not include guest users that are dying.
2669 * @return The existing guest user if it exists. Null otherwise.
2670 * @hide
2671 */
2672 @RequiresPermission(android.Manifest.permission.MANAGE_USERS)
2673 public UserInfo findCurrentGuestUser() {
2674 try {
2675 return mService.findCurrentGuestUser();
2676 } catch (RemoteException re) {
2677 throw re.rethrowFromSystemServer();
2678 }
2679 }
2680
2681 /**
Valentin Iftime89df4c82019-08-23 13:02:50 +02002682 * Creates a user with the specified name and options as a profile of the context's user.
2683 *
2684 * @param name the user's name.
2685 * @param userType the type of user, such as {@link UserManager#USER_TYPE_PROFILE_MANAGED}.
2686 * @param disallowedPackages packages to not install for this profile.
2687 *
2688 * @return the {@link android.os.UserHandle} object for the created user,
2689 * or throws {@link UserOperationException} if the user could not be created
2690 * and calling app is targeting {@link android.os.Build.VERSION_CODES#R} or above
2691 * (otherwise returns {@code null}).
2692 *
2693 * @throws UserOperationException if the user could not be created and the calling app is
2694 * targeting {@link android.os.Build.VERSION_CODES#R} or above.
2695 *
2696 * @hide
2697 */
2698 @SystemApi
2699 @RequiresPermission(anyOf = {Manifest.permission.MANAGE_USERS,
2700 Manifest.permission.CREATE_USERS})
2701 @UserHandleAware
2702 public @Nullable UserHandle createProfile(@NonNull String name, @NonNull String userType,
2703 @Nullable String[] disallowedPackages) throws UserOperationException {
2704 try {
2705 return mService.createProfileForUserWithThrow(name, userType, 0,
2706 mUserId, disallowedPackages).getUserHandle();
2707 } catch (ServiceSpecificException e) {
2708 return returnNullOrThrowUserOperationException(e,
2709 mContext.getApplicationInfo().targetSdkVersion >= Build.VERSION_CODES.R);
2710 } catch (RemoteException re) {
2711 throw re.rethrowFromSystemServer();
2712 }
2713 }
2714
2715 /**
Kenny Guy2a764942014-04-02 13:29:20 +01002716 * Creates a user with the specified name and options as a profile of another user.
Valentin Iftime89df4c82019-08-23 13:02:50 +02002717 * <p>Requires MANAGE_USERS. CREATE_USERS suffices for ALLOWED_FLAGS_FOR_CREATE_USERS_PERMISSION
Kenny Guya52dc3e2014-02-11 15:33:14 +00002718 *
2719 * @param name the user's name
2720 * @param flags flags that identify the type of user and other properties.
Bookatzf56f2582019-09-04 16:06:41 -07002721 * @param userId new user will be a profile of this user.
Kenny Guya52dc3e2014-02-11 15:33:14 +00002722 *
Sudheer Shanka7cb54a32016-09-16 12:59:05 -07002723 * @return the {@link UserInfo} object for the created user, or null if the user
2724 * could not be created.
Bookatz029832a2019-10-04 16:50:22 -07002725 * @throws IllegalArgumentException if flags do not correspond to a valid user type.
2726 * @deprecated Use {@link #createProfileForUser(String, String, int, int)} instead.
Kenny Guya52dc3e2014-02-11 15:33:14 +00002727 * @hide
2728 */
Andrei Onea24ec3212019-03-15 17:35:05 +00002729 @UnsupportedAppUsage
Valentin Iftime89df4c82019-08-23 13:02:50 +02002730 @RequiresPermission(anyOf = {Manifest.permission.MANAGE_USERS,
2731 Manifest.permission.CREATE_USERS})
Bookatz029832a2019-10-04 16:50:22 -07002732 @Deprecated
2733 public UserInfo createProfileForUser(String name, @UserInfoFlag int flags,
2734 @UserIdInt int userId) {
2735 return createProfileForUser(name, UserInfo.getDefaultUserType(flags), flags,
2736 userId, null);
Sudheer Shanka7cb54a32016-09-16 12:59:05 -07002737 }
2738
2739 /**
Bookatz029832a2019-10-04 16:50:22 -07002740 * Creates a user with the specified name and options as a profile of another user.
Bookatz029832a2019-10-04 16:50:22 -07002741 *
2742 * @param name the user's name
2743 * @param userType the type of user, such as {@link UserManager#USER_TYPE_PROFILE_MANAGED}.
2744 * @param flags UserInfo flags that specify user properties.
2745 * @param userId new user will be a profile of this user.
2746 *
2747 * @return the {@link UserInfo} object for the created user, or null if the user
2748 * could not be created.
2749 * @hide
2750 */
Valentin Iftime89df4c82019-08-23 13:02:50 +02002751 @RequiresPermission(anyOf = {Manifest.permission.MANAGE_USERS,
2752 Manifest.permission.CREATE_USERS})
Bookatz029832a2019-10-04 16:50:22 -07002753 public UserInfo createProfileForUser(String name, @NonNull String userType,
2754 @UserInfoFlag int flags, @UserIdInt int userId) {
2755 return createProfileForUser(name, userType, flags, userId, null);
2756 }
2757
2758 /**
2759 * Version of {@link #createProfileForUser(String, String, int, int)} that allows you to specify
Sudheer Shanka7cb54a32016-09-16 12:59:05 -07002760 * any packages that should not be installed in the new profile by default, these packages can
2761 * still be installed later by the user if needed.
2762 *
2763 * @param name the user's name
Bookatz029832a2019-10-04 16:50:22 -07002764 * @param userType the type of user, such as {@link UserManager#USER_TYPE_PROFILE_MANAGED}.
2765 * @param flags UserInfo flags that specify user properties.
Bookatzf56f2582019-09-04 16:06:41 -07002766 * @param userId new user will be a profile of this user.
Sudheer Shanka7cb54a32016-09-16 12:59:05 -07002767 * @param disallowedPackages packages that will not be installed in the profile being created.
2768 *
Valentin Iftime89df4c82019-08-23 13:02:50 +02002769 * @return the {@link UserInfo} object for the created user,
2770 * or throws {@link UserOperationException} if the user could not be created
2771 * and calling app is targeting {@link android.os.Build.VERSION_CODES#R} or above
2772 * (otherwise returns {@code null}).
2773 *
2774 * @throws UserOperationException if the user could not be created and the calling app is
2775 * targeting {@link android.os.Build.VERSION_CODES#R} or above.
Sudheer Shanka7cb54a32016-09-16 12:59:05 -07002776 * @hide
2777 */
Valentin Iftime89df4c82019-08-23 13:02:50 +02002778 @RequiresPermission(anyOf = {Manifest.permission.MANAGE_USERS,
2779 Manifest.permission.CREATE_USERS})
Bookatz029832a2019-10-04 16:50:22 -07002780 public UserInfo createProfileForUser(String name, @NonNull String userType,
Valentin Iftime89df4c82019-08-23 13:02:50 +02002781 @UserInfoFlag int flags, @UserIdInt int userId, String[] disallowedPackages)
2782 throws UserOperationException {
Kenny Guya52dc3e2014-02-11 15:33:14 +00002783 try {
Valentin Iftime89df4c82019-08-23 13:02:50 +02002784 return mService.createProfileForUserWithThrow(name, userType, flags, userId,
2785 disallowedPackages);
2786 } catch (ServiceSpecificException e) {
2787 return returnNullOrThrowUserOperationException(e,
2788 mContext.getApplicationInfo().targetSdkVersion >= Build.VERSION_CODES.R);
Kenny Guya52dc3e2014-02-11 15:33:14 +00002789 } catch (RemoteException re) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07002790 throw re.rethrowFromSystemServer();
Kenny Guya52dc3e2014-02-11 15:33:14 +00002791 }
2792 }
2793
2794 /**
Bookatz029832a2019-10-04 16:50:22 -07002795 * Similar to {@link #createProfileForUser(String, String, int, int, String[])}
Esteban Talavera6c9116a2016-11-24 16:12:44 +00002796 * except bypassing the checking of {@link UserManager#DISALLOW_ADD_MANAGED_PROFILE}.
Tony Mak6dc428f2016-10-10 15:48:27 +01002797 *
Bookatz029832a2019-10-04 16:50:22 -07002798 * @see #createProfileForUser(String, String, int, int, String[])
Tony Mak6dc428f2016-10-10 15:48:27 +01002799 * @hide
2800 */
Valentin Iftime89df4c82019-08-23 13:02:50 +02002801 @RequiresPermission(anyOf = {Manifest.permission.MANAGE_USERS,
2802 Manifest.permission.CREATE_USERS})
Bookatz029832a2019-10-04 16:50:22 -07002803 public UserInfo createProfileForUserEvenWhenDisallowed(String name,
2804 @NonNull String userType, @UserInfoFlag int flags, @UserIdInt int userId,
Valentin Iftime89df4c82019-08-23 13:02:50 +02002805 String[] disallowedPackages) throws UserOperationException {
Tony Mak6dc428f2016-10-10 15:48:27 +01002806 try {
Valentin Iftime89df4c82019-08-23 13:02:50 +02002807 return mService.createProfileForUserEvenWhenDisallowedWithThrow(name, userType, flags,
Bookatz029832a2019-10-04 16:50:22 -07002808 userId, disallowedPackages);
Valentin Iftime89df4c82019-08-23 13:02:50 +02002809 } catch (ServiceSpecificException e) {
2810 return returnNullOrThrowUserOperationException(e,
2811 mContext.getApplicationInfo().targetSdkVersion >= Build.VERSION_CODES.R);
Tony Mak6dc428f2016-10-10 15:48:27 +01002812 } catch (RemoteException re) {
2813 throw re.rethrowFromSystemServer();
2814 }
2815 }
2816
2817 /**
Fyodor Kupolov02cb6e72015-09-18 18:20:55 -07002818 * Creates a restricted profile with the specified name. This method also sets necessary
2819 * restrictions and adds shared accounts.
Fyodor Kupolov06a484a2015-08-21 16:33:20 -07002820 *
2821 * @param name profile's name
Valentin Iftime89df4c82019-08-23 13:02:50 +02002822 * @return the {@link UserInfo} object for the created user,
2823 * or throws {@link UserOperationException} if the user could not be created
2824 * and calling app is targeting {@link android.os.Build.VERSION_CODES#R} or above
2825 * (otherwise returns {@code null}).
2826 *
2827 * @throws UserOperationException if the user could not be created and the calling app is
2828 * targeting {@link android.os.Build.VERSION_CODES#R} or above.
Fyodor Kupolov06a484a2015-08-21 16:33:20 -07002829 * @hide
2830 */
Valentin Iftime89df4c82019-08-23 13:02:50 +02002831 @RequiresPermission(anyOf = {Manifest.permission.MANAGE_USERS,
2832 Manifest.permission.CREATE_USERS})
2833 public UserInfo createRestrictedProfile(String name) throws UserOperationException {
Fyodor Kupolov06a484a2015-08-21 16:33:20 -07002834 try {
Fyodor Kupolov02cb6e72015-09-18 18:20:55 -07002835 UserHandle parentUserHandle = Process.myUserHandle();
Valentin Iftime89df4c82019-08-23 13:02:50 +02002836 UserInfo user = mService.createRestrictedProfileWithThrow(name,
Fyodor Kupolov02cb6e72015-09-18 18:20:55 -07002837 parentUserHandle.getIdentifier());
2838 if (user != null) {
2839 AccountManager.get(mContext).addSharedAccountsFromParentUser(parentUserHandle,
2840 UserHandle.of(user.id));
Fyodor Kupolov06a484a2015-08-21 16:33:20 -07002841 }
Fyodor Kupolov02cb6e72015-09-18 18:20:55 -07002842 return user;
Valentin Iftime89df4c82019-08-23 13:02:50 +02002843 } catch (ServiceSpecificException e) {
2844 return returnNullOrThrowUserOperationException(e,
2845 mContext.getApplicationInfo().targetSdkVersion >= Build.VERSION_CODES.R);
Jeff Sharkey27b2e692016-02-25 17:40:12 -07002846 } catch (RemoteException re) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07002847 throw re.rethrowFromSystemServer();
Fyodor Kupolov06a484a2015-08-21 16:33:20 -07002848 }
Fyodor Kupolov06a484a2015-08-21 16:33:20 -07002849 }
2850
2851 /**
Amith Yamasani06964342016-04-15 13:55:01 -07002852 * Returns an intent to create a user for the provided name and account name. The name
2853 * and account name will be used when the setup process for the new user is started.
2854 * <p>
Amith Yamasani12747872015-12-07 14:19:49 -08002855 * The intent should be launched using startActivityForResult and the return result will
Amith Yamasani37ed8d12016-01-27 14:40:16 -08002856 * indicate if the user consented to adding a new user and if the operation succeeded. Any
2857 * errors in creating the user will be returned in the result code. If the user cancels the
2858 * request, the return result will be {@link Activity#RESULT_CANCELED}. On success, the
2859 * result code will be {@link Activity#RESULT_OK}.
Amith Yamasani06964342016-04-15 13:55:01 -07002860 * <p>
2861 * Use {@link #supportsMultipleUsers()} to first check if the device supports this operation
2862 * at all.
2863 * <p>
Amith Yamasani12747872015-12-07 14:19:49 -08002864 * The new user is created but not initialized. After switching into the user for the first
2865 * time, the preferred user name and account information are used by the setup process for that
2866 * user.
2867 *
2868 * @param userName Optional name to assign to the user.
Amith Yamasani06964342016-04-15 13:55:01 -07002869 * @param accountName Optional account name that will be used by the setup wizard to initialize
Amith Yamasani12747872015-12-07 14:19:49 -08002870 * the user.
2871 * @param accountType Optional account type for the account to be created. This is required
2872 * if the account name is specified.
2873 * @param accountOptions Optional bundle of data to be passed in during account creation in the
2874 * new user via {@link AccountManager#addAccount(String, String, String[],
2875 * Bundle, android.app.Activity, android.accounts.AccountManagerCallback,
2876 * Handler)}.
Amith Yamasani06964342016-04-15 13:55:01 -07002877 * @return An Intent that can be launched from an Activity.
Amith Yamasani37ed8d12016-01-27 14:40:16 -08002878 * @see #USER_CREATION_FAILED_NOT_PERMITTED
2879 * @see #USER_CREATION_FAILED_NO_MORE_USERS
Amith Yamasani06964342016-04-15 13:55:01 -07002880 * @see #supportsMultipleUsers
Amith Yamasani12747872015-12-07 14:19:49 -08002881 */
2882 public static Intent createUserCreationIntent(@Nullable String userName,
2883 @Nullable String accountName,
2884 @Nullable String accountType, @Nullable PersistableBundle accountOptions) {
Amith Yamasani12747872015-12-07 14:19:49 -08002885 Intent intent = new Intent(ACTION_CREATE_USER);
2886 if (userName != null) {
2887 intent.putExtra(EXTRA_USER_NAME, userName);
2888 }
2889 if (accountName != null && accountType == null) {
2890 throw new IllegalArgumentException("accountType must be specified if accountName is "
2891 + "specified");
2892 }
2893 if (accountName != null) {
2894 intent.putExtra(EXTRA_USER_ACCOUNT_NAME, accountName);
2895 }
2896 if (accountType != null) {
2897 intent.putExtra(EXTRA_USER_ACCOUNT_TYPE, accountType);
2898 }
2899 if (accountOptions != null) {
2900 intent.putExtra(EXTRA_USER_ACCOUNT_OPTIONS, accountOptions);
2901 }
2902 return intent;
2903 }
2904
2905 /**
2906 * @hide
2907 *
Jeff Sharkeyd86b8fe2017-06-02 17:36:26 -06002908 * Returns the preferred account name for user creation.
Amith Yamasani12747872015-12-07 14:19:49 -08002909 */
2910 @SystemApi
Jeff Sharkeyd86b8fe2017-06-02 17:36:26 -06002911 @RequiresPermission(android.Manifest.permission.MANAGE_USERS)
Amith Yamasani12747872015-12-07 14:19:49 -08002912 public String getSeedAccountName() {
2913 try {
2914 return mService.getSeedAccountName();
2915 } catch (RemoteException re) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07002916 throw re.rethrowFromSystemServer();
Amith Yamasani12747872015-12-07 14:19:49 -08002917 }
2918 }
2919
2920 /**
2921 * @hide
2922 *
Jeff Sharkeyd86b8fe2017-06-02 17:36:26 -06002923 * Returns the preferred account type for user creation.
Amith Yamasani12747872015-12-07 14:19:49 -08002924 */
2925 @SystemApi
Jeff Sharkeyd86b8fe2017-06-02 17:36:26 -06002926 @RequiresPermission(android.Manifest.permission.MANAGE_USERS)
Amith Yamasani12747872015-12-07 14:19:49 -08002927 public String getSeedAccountType() {
2928 try {
2929 return mService.getSeedAccountType();
2930 } catch (RemoteException re) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07002931 throw re.rethrowFromSystemServer();
Amith Yamasani12747872015-12-07 14:19:49 -08002932 }
2933 }
2934
2935 /**
2936 * @hide
2937 *
Jeff Sharkeyd86b8fe2017-06-02 17:36:26 -06002938 * Returns the preferred account's options bundle for user creation.
Amith Yamasani12747872015-12-07 14:19:49 -08002939 * @return Any options set by the requestor that created the user.
2940 */
2941 @SystemApi
Jeff Sharkeyd86b8fe2017-06-02 17:36:26 -06002942 @RequiresPermission(android.Manifest.permission.MANAGE_USERS)
Amith Yamasani12747872015-12-07 14:19:49 -08002943 public PersistableBundle getSeedAccountOptions() {
2944 try {
2945 return mService.getSeedAccountOptions();
2946 } catch (RemoteException re) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07002947 throw re.rethrowFromSystemServer();
Amith Yamasani12747872015-12-07 14:19:49 -08002948 }
2949 }
2950
2951 /**
2952 * @hide
2953 *
2954 * Called by a system activity to set the seed account information of a user created
2955 * through the user creation intent.
2956 * @param userId
2957 * @param accountName
2958 * @param accountType
2959 * @param accountOptions
2960 * @see #createUserCreationIntent(String, String, String, PersistableBundle)
2961 */
Valentin Iftime89df4c82019-08-23 13:02:50 +02002962 @RequiresPermission(android.Manifest.permission.MANAGE_USERS)
Amith Yamasani12747872015-12-07 14:19:49 -08002963 public void setSeedAccountData(int userId, String accountName, String accountType,
2964 PersistableBundle accountOptions) {
2965 try {
2966 mService.setSeedAccountData(userId, accountName, accountType, accountOptions,
2967 /* persist= */ true);
2968 } catch (RemoteException re) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07002969 throw re.rethrowFromSystemServer();
Amith Yamasani12747872015-12-07 14:19:49 -08002970 }
2971 }
2972
2973 /**
2974 * @hide
Jeff Sharkeyd86b8fe2017-06-02 17:36:26 -06002975 * Clears the seed information used to create this user.
Amith Yamasani12747872015-12-07 14:19:49 -08002976 */
2977 @SystemApi
Jeff Sharkeyd86b8fe2017-06-02 17:36:26 -06002978 @RequiresPermission(android.Manifest.permission.MANAGE_USERS)
Amith Yamasani12747872015-12-07 14:19:49 -08002979 public void clearSeedAccountData() {
2980 try {
2981 mService.clearSeedAccountData();
2982 } catch (RemoteException re) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07002983 throw re.rethrowFromSystemServer();
Amith Yamasani12747872015-12-07 14:19:49 -08002984 }
2985 }
2986
2987 /**
Amith Yamasani1df14732014-08-29 21:37:27 -07002988 * @hide
2989 * Marks the guest user for deletion to allow a new guest to be created before deleting
2990 * the current user who is a guest.
Bookatzf56f2582019-09-04 16:06:41 -07002991 * @param userId
Amith Yamasani1df14732014-08-29 21:37:27 -07002992 * @return
2993 */
Valentin Iftime89df4c82019-08-23 13:02:50 +02002994 @RequiresPermission(android.Manifest.permission.MANAGE_USERS)
Bookatzf56f2582019-09-04 16:06:41 -07002995 public boolean markGuestForDeletion(@UserIdInt int userId) {
Amith Yamasani1df14732014-08-29 21:37:27 -07002996 try {
Bookatzf56f2582019-09-04 16:06:41 -07002997 return mService.markGuestForDeletion(userId);
Amith Yamasani1df14732014-08-29 21:37:27 -07002998 } catch (RemoteException re) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07002999 throw re.rethrowFromSystemServer();
Amith Yamasani1df14732014-08-29 21:37:27 -07003000 }
3001 }
3002
3003 /**
Alexandra Gherghinadf35d572014-04-09 13:54:39 +01003004 * Sets the user as enabled, if such an user exists.
Lenka Trochtova1ddda472016-02-12 10:42:12 +01003005 *
Lenka Trochtova1ddda472016-02-12 10:42:12 +01003006 * <p>Note that the default is true, it's only that managed profiles might not be enabled.
3007 * Also ephemeral users can be disabled to indicate that their removal is in progress and they
3008 * shouldn't be re-entered. Therefore ephemeral users should not be re-enabled once disabled.
Alexandra Gherghinadf35d572014-04-09 13:54:39 +01003009 *
jovanakf24ad492018-05-18 12:15:59 -07003010 * @param userId the id of the profile to enable
Alexandra Gherghinadf35d572014-04-09 13:54:39 +01003011 * @hide
3012 */
Valentin Iftime89df4c82019-08-23 13:02:50 +02003013 @RequiresPermission(android.Manifest.permission.MANAGE_USERS)
jovanakf24ad492018-05-18 12:15:59 -07003014 public void setUserEnabled(@UserIdInt int userId) {
Alexandra Gherghinadf35d572014-04-09 13:54:39 +01003015 try {
jovanakf24ad492018-05-18 12:15:59 -07003016 mService.setUserEnabled(userId);
3017 } catch (RemoteException re) {
3018 throw re.rethrowFromSystemServer();
3019 }
3020 }
3021
3022 /**
3023 * Assigns admin privileges to the user, if such a user exists.
3024 *
Bookatzd15b2f72019-11-26 16:58:57 -08003025 * <p>Note that this does not alter the user's pre-existing user restrictions.
jovanakf24ad492018-05-18 12:15:59 -07003026 *
Bookatzf56f2582019-09-04 16:06:41 -07003027 * @param userId the id of the user to become admin
jovanakf24ad492018-05-18 12:15:59 -07003028 * @hide
3029 */
3030 @RequiresPermission(allOf = {
3031 Manifest.permission.INTERACT_ACROSS_USERS_FULL,
3032 Manifest.permission.MANAGE_USERS
3033 })
Bookatzf56f2582019-09-04 16:06:41 -07003034 public void setUserAdmin(@UserIdInt int userId) {
jovanakf24ad492018-05-18 12:15:59 -07003035 try {
Bookatzf56f2582019-09-04 16:06:41 -07003036 mService.setUserAdmin(userId);
Jeff Sharkey27b2e692016-02-25 17:40:12 -07003037 } catch (RemoteException re) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07003038 throw re.rethrowFromSystemServer();
Alexandra Gherghinadf35d572014-04-09 13:54:39 +01003039 }
3040 }
3041
3042 /**
Andrew Scull85a63bc2016-10-24 13:47:47 +01003043 * Evicts the user's credential encryption key from memory by stopping and restarting the user.
3044 *
3045 * @hide
3046 */
Valentin Iftime89df4c82019-08-23 13:02:50 +02003047 @RequiresPermission(android.Manifest.permission.MANAGE_USERS)
Bookatzf56f2582019-09-04 16:06:41 -07003048 public void evictCredentialEncryptionKey(@UserIdInt int userId) {
Andrew Scull85a63bc2016-10-24 13:47:47 +01003049 try {
Bookatzf56f2582019-09-04 16:06:41 -07003050 mService.evictCredentialEncryptionKey(userId);
Andrew Scull85a63bc2016-10-24 13:47:47 +01003051 } catch (RemoteException re) {
3052 throw re.rethrowFromSystemServer();
3053 }
3054 }
3055
3056 /**
Dianne Hackbornb26306a2012-10-24 15:22:21 -07003057 * Return the number of users currently created on the device.
Kevin Hufnagle5813a8c2019-09-30 16:25:02 +00003058 * <p>This API is not for use by third-party apps. It requires the {@code MANAGE_USERS}
3059 * permission.</p>
Dianne Hackbornb26306a2012-10-24 15:22:21 -07003060 */
Valentin Iftime89df4c82019-08-23 13:02:50 +02003061 @RequiresPermission(android.Manifest.permission.MANAGE_USERS)
Dianne Hackbornb26306a2012-10-24 15:22:21 -07003062 public int getUserCount() {
3063 List<UserInfo> users = getUsers();
3064 return users != null ? users.size() : 1;
3065 }
3066
3067 /**
Amith Yamasanid04aaa32016-06-13 12:09:36 -07003068 * Returns information for all users on this device, including ones marked for deletion.
3069 * To retrieve only users that are alive, use {@link #getUsers(boolean)}.
Felipe Leme09a7f2d2019-10-02 16:27:46 -07003070 *
Fyodor Kupolov940e8572016-01-26 12:03:51 -08003071 * @return the list of users that exist on the device.
Amith Yamasani258848d2012-08-10 17:06:33 -07003072 * @hide
3073 */
Andrei Onea24ec3212019-03-15 17:35:05 +00003074 @UnsupportedAppUsage
Felipe Leme09a7f2d2019-10-02 16:27:46 -07003075 @RequiresPermission(android.Manifest.permission.MANAGE_USERS)
Amith Yamasani258848d2012-08-10 17:06:33 -07003076 public List<UserInfo> getUsers() {
Felipe Leme09a7f2d2019-10-02 16:27:46 -07003077 return getUsers(/* excludeDying= */ false);
3078 }
3079
3080 /**
Jeffrey Huang91027f82019-11-26 10:34:28 -08003081 * Returns information for all users on this device. Requires
3082 * {@link android.Manifest.permission#MANAGE_USERS} permission.
3083 *
3084 * @param excludeDying specify if the list should exclude users being
3085 * removed.
3086 * @return the list of users that were created.
3087 * @hide
3088 */
3089 @UnsupportedAppUsage
3090 public @NonNull List<UserInfo> getUsers(boolean excludeDying) {
3091 return getUsers(/*excludePartial= */ true, excludeDying,
3092 /* excludePreCreated= */ true);
3093 }
3094
3095 /**
Felipe Leme09a7f2d2019-10-02 16:27:46 -07003096 * Returns information for all users on this device, based on the filtering parameters.
3097 *
3098 * @hide
3099 */
3100 @RequiresPermission(android.Manifest.permission.MANAGE_USERS)
3101 public List<UserInfo> getUsers(boolean excludePartial, boolean excludeDying,
3102 boolean excludePreCreated) {
Amith Yamasani258848d2012-08-10 17:06:33 -07003103 try {
Felipe Leme09a7f2d2019-10-02 16:27:46 -07003104 return mService.getUsers(excludePartial, excludeDying, excludePreCreated);
Amith Yamasani920ace02012-09-20 22:15:37 -07003105 } catch (RemoteException re) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07003106 throw re.rethrowFromSystemServer();
Amith Yamasani920ace02012-09-20 22:15:37 -07003107 }
3108 }
3109
3110 /**
Jeffrey Huang91027f82019-11-26 10:34:28 -08003111 * Returns the user handles for all users on this device, based on the filtering parameters.
3112 *
3113 * @param excludeDying specify if the list should exclude users being removed.
3114 * @return the list of user handles.
3115 * @hide
3116 */
3117 @SystemApi
3118 @RequiresPermission(android.Manifest.permission.MANAGE_USERS)
3119 public @NonNull List<UserHandle> getUserHandles(boolean excludeDying) {
3120 List<UserInfo> users = getUsers(excludeDying);
3121 List<UserHandle> result = new ArrayList<>(users.size());
3122 for (UserInfo user : users) {
3123 result.add(user.getUserHandle());
3124 }
3125 return result;
3126 }
3127
3128 /**
Fyodor Kupolov940e8572016-01-26 12:03:51 -08003129 * Returns serial numbers of all users on this device.
Fyodor Kupolov940e8572016-01-26 12:03:51 -08003130 *
3131 * @param excludeDying specify if the list should exclude users being removed.
3132 * @return the list of serial numbers of users that exist on the device.
3133 * @hide
3134 */
3135 @SystemApi
Jeff Sharkeyd86b8fe2017-06-02 17:36:26 -06003136 @RequiresPermission(android.Manifest.permission.MANAGE_USERS)
Fyodor Kupolov940e8572016-01-26 12:03:51 -08003137 public long[] getSerialNumbersOfUsers(boolean excludeDying) {
Felipe Leme09a7f2d2019-10-02 16:27:46 -07003138 List<UserInfo> users = getUsers(excludeDying);
3139 long[] result = new long[users.size()];
3140 for (int i = 0; i < result.length; i++) {
3141 result[i] = users.get(i).serialNumber;
Fyodor Kupolov940e8572016-01-26 12:03:51 -08003142 }
Felipe Leme09a7f2d2019-10-02 16:27:46 -07003143 return result;
Fyodor Kupolov940e8572016-01-26 12:03:51 -08003144 }
3145
3146 /**
Xiaohui Chenb3b92582015-12-07 11:22:13 -08003147 * @return the user's account name, null if not found.
3148 * @hide
3149 */
3150 @RequiresPermission( allOf = {
3151 Manifest.permission.INTERACT_ACROSS_USERS_FULL,
3152 Manifest.permission.MANAGE_USERS
3153 })
Bookatzf56f2582019-09-04 16:06:41 -07003154 public @Nullable String getUserAccount(@UserIdInt int userId) {
Xiaohui Chenb3b92582015-12-07 11:22:13 -08003155 try {
Bookatzf56f2582019-09-04 16:06:41 -07003156 return mService.getUserAccount(userId);
Xiaohui Chenb3b92582015-12-07 11:22:13 -08003157 } catch (RemoteException re) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07003158 throw re.rethrowFromSystemServer();
Xiaohui Chenb3b92582015-12-07 11:22:13 -08003159 }
3160 }
3161
3162 /**
3163 * Set account name for the given user.
3164 * @hide
3165 */
3166 @RequiresPermission( allOf = {
3167 Manifest.permission.INTERACT_ACROSS_USERS_FULL,
3168 Manifest.permission.MANAGE_USERS
3169 })
Bookatzf56f2582019-09-04 16:06:41 -07003170 public void setUserAccount(@UserIdInt int userId, @Nullable String accountName) {
Xiaohui Chenb3b92582015-12-07 11:22:13 -08003171 try {
Bookatzf56f2582019-09-04 16:06:41 -07003172 mService.setUserAccount(userId, accountName);
Xiaohui Chenb3b92582015-12-07 11:22:13 -08003173 } catch (RemoteException re) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07003174 throw re.rethrowFromSystemServer();
Xiaohui Chenb3b92582015-12-07 11:22:13 -08003175 }
3176 }
3177
3178 /**
Xiaohui Chen70f6c382015-04-28 14:21:43 -07003179 * Returns information for Primary user.
Xiaohui Chen70f6c382015-04-28 14:21:43 -07003180 *
3181 * @return the Primary user, null if not found.
3182 * @hide
3183 */
Valentin Iftime89df4c82019-08-23 13:02:50 +02003184 @RequiresPermission(android.Manifest.permission.MANAGE_USERS)
Xiaohui Chen7cb69df2015-07-13 16:01:01 -07003185 public @Nullable UserInfo getPrimaryUser() {
Xiaohui Chen70f6c382015-04-28 14:21:43 -07003186 try {
3187 return mService.getPrimaryUser();
3188 } catch (RemoteException re) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07003189 throw re.rethrowFromSystemServer();
Xiaohui Chen70f6c382015-04-28 14:21:43 -07003190 }
3191 }
3192
3193 /**
Amith Yamasani95ab7842014-08-11 17:09:26 -07003194 * Checks whether it's possible to add more users. Caller must hold the MANAGE_USERS
3195 * permission.
3196 *
3197 * @return true if more users can be added, false if limit has been reached.
3198 * @hide
3199 */
Valentin Iftime89df4c82019-08-23 13:02:50 +02003200 @RequiresPermission(android.Manifest.permission.MANAGE_USERS)
Amith Yamasani95ab7842014-08-11 17:09:26 -07003201 public boolean canAddMoreUsers() {
Bookatz029832a2019-10-04 16:50:22 -07003202 // TODO(b/142482943): UMS has different logic, excluding Demo and Profile from counting. Why
3203 // not here? The logic is inconsistent. See UMS.canAddMoreManagedProfiles
Amith Yamasani95ab7842014-08-11 17:09:26 -07003204 final List<UserInfo> users = getUsers(true);
3205 final int totalUserCount = users.size();
3206 int aliveUserCount = 0;
3207 for (int i = 0; i < totalUserCount; i++) {
3208 UserInfo user = users.get(i);
3209 if (!user.isGuest()) {
3210 aliveUserCount++;
3211 }
3212 }
3213 return aliveUserCount < getMaxSupportedUsers();
3214 }
3215
3216 /**
Nicolas Prevot72434b72015-05-13 12:15:03 -07003217 * Checks whether it's possible to add more managed profiles. Caller must hold the MANAGE_USERS
3218 * permission.
Nicolas Prevot07387fe2015-10-30 17:53:30 +00003219 * if allowedToRemoveOne is true and if the user already has a managed profile, then return if
3220 * we could add a new managed profile to this user after removing the existing one.
Nicolas Prevot72434b72015-05-13 12:15:03 -07003221 *
3222 * @return true if more managed profiles can be added, false if limit has been reached.
3223 * @hide
3224 */
Valentin Iftime89df4c82019-08-23 13:02:50 +02003225 @RequiresPermission(android.Manifest.permission.MANAGE_USERS)
Jeff Sharkey8588bc12016-01-06 16:47:42 -07003226 public boolean canAddMoreManagedProfiles(@UserIdInt int userId, boolean allowedToRemoveOne) {
Nicolas Prevot72434b72015-05-13 12:15:03 -07003227 try {
Nicolas Prevot07387fe2015-10-30 17:53:30 +00003228 return mService.canAddMoreManagedProfiles(userId, allowedToRemoveOne);
Nicolas Prevot72434b72015-05-13 12:15:03 -07003229 } catch (RemoteException re) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07003230 throw re.rethrowFromSystemServer();
Nicolas Prevot72434b72015-05-13 12:15:03 -07003231 }
3232 }
3233
3234 /**
Bookatz029832a2019-10-04 16:50:22 -07003235 * Checks whether it's possible to add more profiles of the given type to the given user.
3236 *
3237 * @param userType the type of user, such as {@link UserManager#USER_TYPE_PROFILE_MANAGED}.
3238 * @return true if more profiles can be added, false if limit has been reached.
3239 * @hide
3240 */
3241 @RequiresPermission(android.Manifest.permission.MANAGE_USERS)
3242 public boolean canAddMoreProfilesToUser(@NonNull String userType, @UserIdInt int userId) {
3243 try {
3244 return mService.canAddMoreProfilesToUser(userType, userId, false);
3245 } catch (RemoteException re) {
3246 throw re.rethrowFromSystemServer();
3247 }
3248 }
3249
3250 /**
Bookatzf56f2582019-09-04 16:06:41 -07003251 * Returns list of the profiles of userId including userId itself.
Amith Yamasani4f7e2e32014-08-14 18:49:48 -07003252 * Note that this returns both enabled and not enabled profiles. See
Ruben Brunk7f75da22015-04-30 17:46:30 -07003253 * {@link #getEnabledProfiles(int)} if you need only the enabled ones.
Amith Yamasani4f582632014-02-19 14:31:52 -08003254 *
Valentin Iftime89df4c82019-08-23 13:02:50 +02003255 * <p>Requires {@link android.Manifest.permission#MANAGE_USERS}.
3256 * {@link android.Manifest.permission#CREATE_USERS} suffices if userId is the calling user.
Bookatzf56f2582019-09-04 16:06:41 -07003257 * @param userId profiles of this user will be returned.
Kenny Guy2a764942014-04-02 13:29:20 +01003258 * @return the list of profiles.
3259 * @hide
3260 */
Andrei Onea24ec3212019-03-15 17:35:05 +00003261 @UnsupportedAppUsage
Valentin Iftime89df4c82019-08-23 13:02:50 +02003262 @RequiresPermission(anyOf = {Manifest.permission.MANAGE_USERS,
3263 Manifest.permission.CREATE_USERS}, conditional = true)
Bookatzf56f2582019-09-04 16:06:41 -07003264 public List<UserInfo> getProfiles(@UserIdInt int userId) {
Kenny Guya52dc3e2014-02-11 15:33:14 +00003265 try {
Bookatzf56f2582019-09-04 16:06:41 -07003266 return mService.getProfiles(userId, false /* enabledOnly */);
Kenny Guya52dc3e2014-02-11 15:33:14 +00003267 } catch (RemoteException re) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07003268 throw re.rethrowFromSystemServer();
Kenny Guya52dc3e2014-02-11 15:33:14 +00003269 }
3270 }
3271
3272 /**
Roshan Piusf6c8cd32019-10-17 13:03:18 -07003273 * Checks if the 2 provided user handles belong to the same profile group.
3274 *
3275 * @param user one of the two user handles to check.
3276 * @param otherUser one of the two user handles to check.
3277 * @return true if the two users are in the same profile group.
3278 *
Roshan Piusf6c8cd32019-10-17 13:03:18 -07003279 * @hide
3280 */
3281 @SystemApi
3282 @RequiresPermission(android.Manifest.permission.MANAGE_USERS)
3283 public boolean isSameProfileGroup(@NonNull UserHandle user, @NonNull UserHandle otherUser) {
3284 return isSameProfileGroup(user.getIdentifier(), otherUser.getIdentifier());
3285 }
3286
3287 /**
Valentin Iftime89df4c82019-08-23 13:02:50 +02003288 * Checks if the 2 provided user ids belong to the same profile group.
Xiaohui Chenfd5b7742015-10-14 15:47:04 -07003289 * @param userId one of the two user ids to check.
3290 * @param otherUserId one of the two user ids to check.
3291 * @return true if the two user ids are in the same profile group.
3292 * @hide
3293 */
Valentin Iftime89df4c82019-08-23 13:02:50 +02003294 @RequiresPermission(android.Manifest.permission.MANAGE_USERS)
Jeff Sharkey8588bc12016-01-06 16:47:42 -07003295 public boolean isSameProfileGroup(@UserIdInt int userId, int otherUserId) {
Xiaohui Chenfd5b7742015-10-14 15:47:04 -07003296 try {
3297 return mService.isSameProfileGroup(userId, otherUserId);
3298 } catch (RemoteException re) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07003299 throw re.rethrowFromSystemServer();
Xiaohui Chenfd5b7742015-10-14 15:47:04 -07003300 }
3301 }
3302
3303 /**
Bookatzf56f2582019-09-04 16:06:41 -07003304 * Returns list of the profiles of userId including userId itself.
Ruben Brunk7f75da22015-04-30 17:46:30 -07003305 * Note that this returns only enabled.
3306 *
Valentin Iftime89df4c82019-08-23 13:02:50 +02003307 * <p>Requires {@link android.Manifest.permission#MANAGE_USERS}.
3308 * {@link android.Manifest.permission#CREATE_USERS} suffices if userId is the calling user.
Bookatzf56f2582019-09-04 16:06:41 -07003309 * @param userId profiles of this user will be returned.
Ruben Brunk7f75da22015-04-30 17:46:30 -07003310 * @return the list of profiles.
3311 * @hide
3312 */
Andrei Onea24ec3212019-03-15 17:35:05 +00003313 @UnsupportedAppUsage
Valentin Iftime89df4c82019-08-23 13:02:50 +02003314 @RequiresPermission(anyOf = {Manifest.permission.MANAGE_USERS,
3315 Manifest.permission.CREATE_USERS}, conditional = true)
Bookatzf56f2582019-09-04 16:06:41 -07003316 public List<UserInfo> getEnabledProfiles(@UserIdInt int userId) {
Ruben Brunk7f75da22015-04-30 17:46:30 -07003317 try {
Bookatzf56f2582019-09-04 16:06:41 -07003318 return mService.getProfiles(userId, true /* enabledOnly */);
Ruben Brunk7f75da22015-04-30 17:46:30 -07003319 } catch (RemoteException re) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07003320 throw re.rethrowFromSystemServer();
Ruben Brunk7f75da22015-04-30 17:46:30 -07003321 }
3322 }
3323
3324 /**
Jessica Hummelbe81c802014-04-22 15:49:22 +01003325 * Returns a list of UserHandles for profiles associated with the user that the calling process
3326 * is running on, including the user itself.
Amith Yamasani4f582632014-02-19 14:31:52 -08003327 *
3328 * @return A non-empty list of UserHandles associated with the calling user.
3329 */
3330 public List<UserHandle> getUserProfiles() {
Fyodor Kupolov7f98aa42016-04-07 14:56:25 -07003331 int[] userIds = getProfileIds(UserHandle.myUserId(), true /* enabledOnly */);
3332 List<UserHandle> result = new ArrayList<>(userIds.length);
3333 for (int userId : userIds) {
3334 result.add(UserHandle.of(userId));
3335 }
3336 return result;
3337 }
3338
3339 /**
Valentin Iftime89df4c82019-08-23 13:02:50 +02003340 * Returns a list of ids for profiles associated with the context user including the user
3341 * itself.
3342 *
3343 * @param enabledOnly whether to return only {@link UserInfo#isEnabled() enabled} profiles
3344 * @return A non-empty list of UserHandles associated with the calling user.
3345 *
3346 * @hide
3347 */
3348 @SystemApi
3349 @RequiresPermission(anyOf = {Manifest.permission.MANAGE_USERS,
3350 Manifest.permission.CREATE_USERS}, conditional = true)
3351 @UserHandleAware
3352 public @NonNull List<UserHandle> getUserProfiles(boolean enabledOnly) {
3353 final int[] userIds = getProfileIds(mUserId, enabledOnly);
3354 final List<UserHandle> result = new ArrayList<>(userIds.length);
3355 for (int userId : userIds) {
3356 result.add(UserHandle.of(userId));
3357 }
3358 return result;
3359 }
3360
3361 /**
Fyodor Kupolov7f98aa42016-04-07 14:56:25 -07003362 * Returns a list of ids for profiles associated with the specified user including the user
3363 * itself.
3364 *
3365 * @param userId id of the user to return profiles for
3366 * @param enabledOnly whether return only {@link UserInfo#isEnabled() enabled} profiles
3367 * @return A non-empty list of ids of profiles associated with the specified user.
3368 *
3369 * @hide
3370 */
Philip P. Moltmann4e615e62018-08-28 14:57:49 -07003371 @RequiresPermission(anyOf = {Manifest.permission.MANAGE_USERS,
3372 Manifest.permission.CREATE_USERS}, conditional = true)
Philip P. Moltmann4e615e62018-08-28 14:57:49 -07003373 public @NonNull int[] getProfileIds(@UserIdInt int userId, boolean enabledOnly) {
Alexandra Gherghina385124d2014-04-03 13:37:39 +01003374 try {
Fyodor Kupolov7f98aa42016-04-07 14:56:25 -07003375 return mService.getProfileIds(userId, enabledOnly);
Alexandra Gherghina385124d2014-04-03 13:37:39 +01003376 } catch (RemoteException re) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07003377 throw re.rethrowFromSystemServer();
Alexandra Gherghina385124d2014-04-03 13:37:39 +01003378 }
Fyodor Kupolov7f98aa42016-04-07 14:56:25 -07003379 }
3380
3381 /**
3382 * @see #getProfileIds(int, boolean)
3383 * @hide
3384 */
Andrei Onea24ec3212019-03-15 17:35:05 +00003385 @UnsupportedAppUsage
Valentin Iftime89df4c82019-08-23 13:02:50 +02003386 @RequiresPermission(anyOf = {Manifest.permission.MANAGE_USERS,
3387 Manifest.permission.CREATE_USERS}, conditional = true)
Fyodor Kupolov7f98aa42016-04-07 14:56:25 -07003388 public int[] getProfileIdsWithDisabled(@UserIdInt int userId) {
3389 return getProfileIds(userId, false /* enabledOnly */);
3390 }
3391
3392 /**
3393 * @see #getProfileIds(int, boolean)
3394 * @hide
3395 */
Valentin Iftime89df4c82019-08-23 13:02:50 +02003396 @RequiresPermission(anyOf = {Manifest.permission.MANAGE_USERS,
3397 Manifest.permission.CREATE_USERS}, conditional = true)
Fyodor Kupolov7f98aa42016-04-07 14:56:25 -07003398 public int[] getEnabledProfileIds(@UserIdInt int userId) {
3399 return getProfileIds(userId, true /* enabledOnly */);
Amith Yamasani4f582632014-02-19 14:31:52 -08003400 }
3401
Amith Yamasani7dda2652014-04-11 14:57:12 -07003402 /**
Andres Moralesc5548c02015-08-05 10:23:12 -07003403 * Returns the device credential owner id of the profile from
Bookatzf56f2582019-09-04 16:06:41 -07003404 * which this method is called, or userId if called from a user that
Andres Moralesc5548c02015-08-05 10:23:12 -07003405 * is not a profile.
3406 *
3407 * @hide
3408 */
Valentin Iftime89df4c82019-08-23 13:02:50 +02003409 @RequiresPermission(Manifest.permission.MANAGE_USERS)
Bookatzf56f2582019-09-04 16:06:41 -07003410 public int getCredentialOwnerProfile(@UserIdInt int userId) {
Andres Moralesc5548c02015-08-05 10:23:12 -07003411 try {
Bookatzf56f2582019-09-04 16:06:41 -07003412 return mService.getCredentialOwnerProfile(userId);
Andres Moralesc5548c02015-08-05 10:23:12 -07003413 } catch (RemoteException re) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07003414 throw re.rethrowFromSystemServer();
Andres Moralesc5548c02015-08-05 10:23:12 -07003415 }
3416 }
3417
3418 /**
Jessica Hummelbe81c802014-04-22 15:49:22 +01003419 * Returns the parent of the profile which this method is called from
3420 * or null if called from a user that is not a profile.
3421 *
3422 * @hide
3423 */
Andrei Onea24ec3212019-03-15 17:35:05 +00003424 @UnsupportedAppUsage
Valentin Iftime89df4c82019-08-23 13:02:50 +02003425 @RequiresPermission(android.Manifest.permission.MANAGE_USERS)
Bookatzf56f2582019-09-04 16:06:41 -07003426 public UserInfo getProfileParent(@UserIdInt int userId) {
Jessica Hummelbe81c802014-04-22 15:49:22 +01003427 try {
Bookatzf56f2582019-09-04 16:06:41 -07003428 return mService.getProfileParent(userId);
Jessica Hummelbe81c802014-04-22 15:49:22 +01003429 } catch (RemoteException re) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07003430 throw re.rethrowFromSystemServer();
Jessica Hummelbe81c802014-04-22 15:49:22 +01003431 }
3432 }
3433
3434 /**
Philip P. Moltmanna3e358c2018-11-21 12:45:50 -08003435 * Get the parent of a user profile.
3436 *
3437 * @param user the handle of the user profile
3438 *
3439 * @return the parent of the user or {@code null} if the user is not profile
3440 *
3441 * @hide
3442 */
3443 @SystemApi
3444 @RequiresPermission(android.Manifest.permission.MANAGE_USERS)
3445 public @Nullable UserHandle getProfileParent(@NonNull UserHandle user) {
3446 UserInfo info = getProfileParent(user.getIdentifier());
3447
3448 if (info == null) {
3449 return null;
3450 }
3451
3452 return UserHandle.of(info.id);
3453 }
3454
3455 /**
Tony Make3d1f652017-12-12 11:00:37 +00003456 * Enables or disables quiet mode for a managed profile. If quiet mode is enabled, apps in a
3457 * managed profile don't run, generate notifications, or consume data or battery.
3458 * <p>
3459 * If a user's credential is needed to turn off quiet mode, a confirm credential screen will be
3460 * shown to the user.
3461 * <p>
3462 * The change may not happen instantly, however apps can listen for
3463 * {@link Intent#ACTION_MANAGED_PROFILE_AVAILABLE} and
3464 * {@link Intent#ACTION_MANAGED_PROFILE_UNAVAILABLE} broadcasts in order to be notified of
3465 * the change of the quiet mode. Apps can also check the current state of quiet mode by
3466 * calling {@link #isQuietModeEnabled(UserHandle)}.
3467 * <p>
3468 * The caller must either be the foreground default launcher or have one of these permissions:
3469 * {@code MANAGE_USERS} or {@code MODIFY_QUIET_MODE}.
Rubin Xu0a29ecd2015-11-04 15:11:48 +00003470 *
Tony Make3d1f652017-12-12 11:00:37 +00003471 * @param enableQuietMode whether quiet mode should be enabled or disabled
3472 * @param userHandle user handle of the profile
3473 * @return {@code false} if user's credential is needed in order to turn off quiet mode,
3474 * {@code true} otherwise
3475 * @throws SecurityException if the caller is invalid
3476 * @throws IllegalArgumentException if {@code userHandle} is not a managed profile
3477 *
3478 * @see #isQuietModeEnabled(UserHandle)
Rubin Xu0a29ecd2015-11-04 15:11:48 +00003479 */
Valentin Iftime89df4c82019-08-23 13:02:50 +02003480 @RequiresPermission(anyOf = {android.Manifest.permission.MANAGE_USERS,
3481 Manifest.permission.MODIFY_QUIET_MODE}, conditional = true)
Tony Makbece85d2018-01-12 12:10:17 +00003482 public boolean requestQuietModeEnabled(boolean enableQuietMode, @NonNull UserHandle userHandle) {
3483 return requestQuietModeEnabled(enableQuietMode, userHandle, null);
Tony Makb7e6fd42017-12-05 19:40:28 +00003484 }
3485
3486 /**
Pierre Barbier de Reuille17eaf742020-01-10 17:49:45 +00003487 * Perform the same operation as {@link #requestQuietModeEnabled(boolean, UserHandle)}, but
3488 * with a flag to tweak the behavior of the request.
3489 *
3490 * @param enableQuietMode whether quiet mode should be enabled or disabled
3491 * @param userHandle user handle of the profile
3492 * @param flags Can be 0 or {@link #QUIET_MODE_DISABLE_ONLY_IF_CREDENTIAL_NOT_REQUIRED}.
3493 * @return {@code false} if user's credential is needed in order to turn off quiet mode,
3494 * {@code true} otherwise
3495 * @throws SecurityException if the caller is invalid
3496 * @throws IllegalArgumentException if {@code userHandle} is not a managed profile
3497 *
3498 * @see #isQuietModeEnabled(UserHandle)
3499 */
3500 public boolean requestQuietModeEnabled(boolean enableQuietMode, @NonNull UserHandle userHandle,
3501 @QuietModeFlag int flags) {
3502 return requestQuietModeEnabled(enableQuietMode, userHandle, null, flags);
3503 }
3504
3505 /**
Tony Makbece85d2018-01-12 12:10:17 +00003506 * Similar to {@link #requestQuietModeEnabled(boolean, UserHandle)}, except you can specify
Tony Makd390ae92017-12-28 13:23:10 +00003507 * a target to start when user is unlocked. If {@code target} is specified, caller must have
3508 * the {@link android.Manifest.permission#MANAGE_USERS} permission.
Tony Makb7e6fd42017-12-05 19:40:28 +00003509 *
Tony Makbece85d2018-01-12 12:10:17 +00003510 * @see {@link #requestQuietModeEnabled(boolean, UserHandle)}
Rubin Xu0a29ecd2015-11-04 15:11:48 +00003511 * @hide
3512 */
Valentin Iftime89df4c82019-08-23 13:02:50 +02003513 @RequiresPermission(android.Manifest.permission.MANAGE_USERS)
Tony Makbece85d2018-01-12 12:10:17 +00003514 public boolean requestQuietModeEnabled(
Tony Makb7e6fd42017-12-05 19:40:28 +00003515 boolean enableQuietMode, @NonNull UserHandle userHandle, IntentSender target) {
Pierre Barbier de Reuille17eaf742020-01-10 17:49:45 +00003516 return requestQuietModeEnabled(enableQuietMode, userHandle, target, 0);
3517 }
3518 /**
3519 * Similar to {@link #requestQuietModeEnabled(boolean, UserHandle)}, except you can specify
3520 * a target to start when user is unlocked. If {@code target} is specified, caller must have
3521 * the {@link android.Manifest.permission#MANAGE_USERS} permission.
3522 *
3523 * @see {@link #requestQuietModeEnabled(boolean, UserHandle)}
3524 * @hide
3525 */
3526 public boolean requestQuietModeEnabled(
3527 boolean enableQuietMode, @NonNull UserHandle userHandle, IntentSender target,
3528 int flags) {
Rubin Xu0a29ecd2015-11-04 15:11:48 +00003529 try {
Tony Makbece85d2018-01-12 12:10:17 +00003530 return mService.requestQuietModeEnabled(
Pierre Barbier de Reuille17eaf742020-01-10 17:49:45 +00003531 mContext.getPackageName(), enableQuietMode, userHandle.getIdentifier(), target,
3532 flags);
Jeff Sharkey27b2e692016-02-25 17:40:12 -07003533 } catch (RemoteException re) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07003534 throw re.rethrowFromSystemServer();
Rubin Xu0a29ecd2015-11-04 15:11:48 +00003535 }
3536 }
3537
3538 /**
3539 * Returns whether the given profile is in quiet mode or not.
Ricky Wai7881cf82016-04-15 17:20:12 +01003540 * Notes: Quiet mode is only supported for managed profiles.
Rubin Xu0a29ecd2015-11-04 15:11:48 +00003541 *
3542 * @param userHandle The user handle of the profile to be queried.
3543 * @return true if the profile is in quiet mode, false otherwise.
3544 */
3545 public boolean isQuietModeEnabled(UserHandle userHandle) {
3546 try {
3547 return mService.isQuietModeEnabled(userHandle.getIdentifier());
Jeff Sharkey27b2e692016-02-25 17:40:12 -07003548 } catch (RemoteException re) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07003549 throw re.rethrowFromSystemServer();
Rubin Xu0a29ecd2015-11-04 15:11:48 +00003550 }
Rubin Xu0a29ecd2015-11-04 15:11:48 +00003551 }
3552
3553 /**
Bookatz029832a2019-10-04 16:50:22 -07003554 * Returns whether the given user has a badge (generally to put on profiles' icons).
3555 *
3556 * @param userId userId of the user in question
3557 * @return true if the user's icons should display a badge; false otherwise.
3558 *
3559 * @see #getBadgedIconForUser more information about badging in general
3560 * @hide
3561 */
3562 public boolean hasBadge(@UserIdInt int userId) {
3563 if (!isProfile(userId)) {
3564 // Since currently only profiles actually have badges, we can do this optimization.
3565 return false;
3566 }
3567 try {
3568 return mService.hasBadge(userId);
3569 } catch (RemoteException re) {
3570 throw re.rethrowFromSystemServer();
3571 }
3572 }
3573
3574 /**
Valentin Iftime89df4c82019-08-23 13:02:50 +02003575 * Returns whether the user associated with the context has a badge (generally to put on
3576 * profiles' icons).
Bookatz029832a2019-10-04 16:50:22 -07003577 *
3578 * @return true if the user's icons should display a badge; false otherwise.
Bookatz029832a2019-10-04 16:50:22 -07003579 * @see #getBadgedIconForUser more information about badging in general
3580 * @hide
3581 */
Valentin Iftime89df4c82019-08-23 13:02:50 +02003582 @UserHandleAware
Bookatz029832a2019-10-04 16:50:22 -07003583 public boolean hasBadge() {
Valentin Iftime89df4c82019-08-23 13:02:50 +02003584 return hasBadge(mUserId);
Bookatz029832a2019-10-04 16:50:22 -07003585 }
3586
3587 /**
3588 * Returns the badge color for the given user (generally to color a profile's icon's badge).
3589 *
3590 * <p>To check whether a badge color is expected for the user, first call {@link #hasBadge}.
3591 *
3592 * @return the color (not the resource ID) to be used for the user's badge
3593 * @throws Resources.NotFoundException if no valid badge color exists for this user
3594 *
3595 * @see #getBadgedIconForUser more information about badging in general
3596 * @hide
3597 */
3598 public @ColorInt int getUserBadgeColor(@UserIdInt int userId) {
3599 try {
3600 final int resourceId = mService.getUserBadgeColorResId(userId);
3601 return Resources.getSystem().getColor(resourceId, null);
3602 } catch (RemoteException re) {
3603 throw re.rethrowFromSystemServer();
3604 }
3605 }
3606
3607 /**
3608 * Returns the Resource ID of the user's icon badge.
3609 *
3610 * @return the Resource ID of the user's icon badge if it has one; otherwise
3611 * {@link Resources#ID_NULL}.
3612 *
3613 * @see #getBadgedIconForUser more information about badging in general
3614 * @hide
3615 */
3616 public @DrawableRes int getUserIconBadgeResId(@UserIdInt int userId) {
3617 try {
3618 return mService.getUserIconBadgeResId(userId);
3619 } catch (RemoteException re) {
3620 throw re.rethrowFromSystemServer();
3621 }
3622 }
3623
3624 /**
3625 * Returns the Resource ID of the user's badge.
3626 *
3627 * @return the Resource ID of the user's badge if it has one; otherwise
3628 * {@link Resources#ID_NULL}.
3629 *
3630 * @see #getBadgedIconForUser more information about badging in general
3631 * @hide
3632 */
3633 public @DrawableRes int getUserBadgeResId(@UserIdInt int userId) {
3634 try {
3635 return mService.getUserBadgeResId(userId);
3636 } catch (RemoteException re) {
3637 throw re.rethrowFromSystemServer();
3638 }
3639 }
3640
3641 /**
3642 * Returns the Resource ID of the user's badge without a background.
3643 *
3644 * @return the Resource ID of the user's no-background badge if it has one; otherwise
3645 * {@link Resources#ID_NULL}.
3646 *
3647 * @see #getBadgedIconForUser more information about badging in general
3648 * @hide
3649 */
3650 public @DrawableRes int getUserBadgeNoBackgroundResId(@UserIdInt int userId) {
3651 try {
3652 return mService.getUserBadgeNoBackgroundResId(userId);
3653 } catch (RemoteException re) {
3654 throw re.rethrowFromSystemServer();
3655 }
3656 }
3657
3658 /**
3659 * If the target user is a profile of the calling user or the caller
3660 * is itself a profile, then this returns a badged copy of the given
Svetoslavc71c42f2014-08-05 18:57:05 -07003661 * icon to be able to distinguish it from the original icon. For badging an
3662 * arbitrary drawable use {@link #getBadgedDrawableForUser(
3663 * android.graphics.drawable.Drawable, UserHandle, android.graphics.Rect, int)}.
3664 * <p>
3665 * If the original drawable is a BitmapDrawable and the backing bitmap is
Vadim Tryshev66ae66a2016-02-18 15:41:21 -08003666 * mutable as per {@link android.graphics.Bitmap#isMutable()}, the badging
Svetoslavc71c42f2014-08-05 18:57:05 -07003667 * is performed in place and the original drawable is returned.
3668 * </p>
Amith Yamasani7dda2652014-04-11 14:57:12 -07003669 *
3670 * @param icon The icon to badge.
3671 * @param user The target user.
3672 * @return A drawable that combines the original icon and a badge as
3673 * determined by the system.
Svetoslavc7d62f02014-09-04 15:39:54 -07003674 * @removed
Amith Yamasani7dda2652014-04-11 14:57:12 -07003675 */
Svetoslavc71c42f2014-08-05 18:57:05 -07003676 public Drawable getBadgedIconForUser(Drawable icon, UserHandle user) {
Svetoslavc7d62f02014-09-04 15:39:54 -07003677 return mContext.getPackageManager().getUserBadgedIcon(icon, user);
Amith Yamasani4f582632014-02-19 14:31:52 -08003678 }
3679
Kenny Guy701ea7c2014-05-08 23:34:12 +01003680 /**
Bookatz029832a2019-10-04 16:50:22 -07003681 * If the target user is a profile of the calling user or the caller
3682 * is itself a profile, then this returns a badged copy of the given
Svetoslavc71c42f2014-08-05 18:57:05 -07003683 * drawable allowing the user to distinguish it from the original drawable.
3684 * The caller can specify the location in the bounds of the drawable to be
3685 * badged where the badge should be applied as well as the density of the
3686 * badge to be used.
3687 * <p>
3688 * If the original drawable is a BitmapDrawable and the backing bitmap is
Vadim Tryshev66ae66a2016-02-18 15:41:21 -08003689 * mutable as per {@link android.graphics.Bitmap#isMutable()}, the badging
Svetoslavc71c42f2014-08-05 18:57:05 -07003690 * is performed in place and the original drawable is returned.
3691 * </p>
3692 *
3693 * @param badgedDrawable The drawable to badge.
3694 * @param user The target user.
3695 * @param badgeLocation Where in the bounds of the badged drawable to place
Vadim Tryshev66ae66a2016-02-18 15:41:21 -08003696 * the badge. If it's {@code null}, the badge is applied on top of the entire
Svetoslavc71c42f2014-08-05 18:57:05 -07003697 * drawable being badged.
3698 * @param badgeDensity The optional desired density for the badge as per
Vadim Tryshev66ae66a2016-02-18 15:41:21 -08003699 * {@link android.util.DisplayMetrics#densityDpi}. If it's not positive,
Svetoslavc71c42f2014-08-05 18:57:05 -07003700 * the density of the display is used.
3701 * @return A drawable that combines the original drawable and a badge as
3702 * determined by the system.
Svetoslavc7d62f02014-09-04 15:39:54 -07003703 * @removed
Svetoslavc71c42f2014-08-05 18:57:05 -07003704 */
3705 public Drawable getBadgedDrawableForUser(Drawable badgedDrawable, UserHandle user,
3706 Rect badgeLocation, int badgeDensity) {
Svetoslavc7d62f02014-09-04 15:39:54 -07003707 return mContext.getPackageManager().getUserBadgedDrawableForDensity(badgedDrawable, user,
3708 badgeLocation, badgeDensity);
Svetoslavc71c42f2014-08-05 18:57:05 -07003709 }
3710
3711 /**
Bookatz029832a2019-10-04 16:50:22 -07003712 * If the target user is a profile of the calling user or the caller
3713 * is itself a profile, then this returns a copy of the label with
Kenny Guyf7ecf7c2014-06-18 11:32:05 +01003714 * badging for accessibility services like talkback. E.g. passing in "Email"
3715 * and it might return "Work Email" for Email in the work profile.
3716 *
Bookatz029832a2019-10-04 16:50:22 -07003717 * <p>Requires {@link android.Manifest.permission#MANAGE_USERS} or
3718 * {@link android.Manifest.permission#INTERACT_ACROSS_USERS} permission, otherwise the caller
3719 * must be in the same profile group of specified user.
3720 *
Kenny Guyf7ecf7c2014-06-18 11:32:05 +01003721 * @param label The label to change.
3722 * @param user The target user.
3723 * @return A label that combines the original label and a badge as
3724 * determined by the system.
Svetoslavc7d62f02014-09-04 15:39:54 -07003725 * @removed
Kenny Guyf7ecf7c2014-06-18 11:32:05 +01003726 */
Kenny Guy237aecd2014-07-21 14:06:09 +01003727 public CharSequence getBadgedLabelForUser(CharSequence label, UserHandle user) {
Bookatz029832a2019-10-04 16:50:22 -07003728 final int userId = user.getIdentifier();
3729 if (!hasBadge(userId)) {
3730 return label;
3731 }
3732 try {
3733 final int resourceId = mService.getUserBadgeLabelResId(userId);
3734 return Resources.getSystem().getString(resourceId, label);
3735 } catch (RemoteException re) {
3736 throw re.rethrowFromSystemServer();
3737 }
Amith Yamasani4f582632014-02-19 14:31:52 -08003738 }
3739
3740 /**
Amith Yamasani258848d2012-08-10 17:06:33 -07003741 * Removes a user and all associated data.
Bookatzf56f2582019-09-04 16:06:41 -07003742 * @param userId the integer handle of the user.
Amith Yamasani258848d2012-08-10 17:06:33 -07003743 * @hide
3744 */
Andrei Onea24ec3212019-03-15 17:35:05 +00003745 @UnsupportedAppUsage
Valentin Iftime89df4c82019-08-23 13:02:50 +02003746 @RequiresPermission(anyOf = {Manifest.permission.MANAGE_USERS,
3747 Manifest.permission.CREATE_USERS})
Bookatzf56f2582019-09-04 16:06:41 -07003748 public boolean removeUser(@UserIdInt int userId) {
Amith Yamasani258848d2012-08-10 17:06:33 -07003749 try {
Bookatzf56f2582019-09-04 16:06:41 -07003750 return mService.removeUser(userId);
Amith Yamasani258848d2012-08-10 17:06:33 -07003751 } catch (RemoteException re) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07003752 throw re.rethrowFromSystemServer();
Amith Yamasani258848d2012-08-10 17:06:33 -07003753 }
3754 }
3755
3756 /**
Varun Shahe38c6782018-12-04 16:57:49 -08003757 * Removes a user and all associated data.
3758 *
3759 * @param user the user that needs to be removed.
Varun Shah9533e622019-02-27 15:26:56 -08003760 * @return {@code true} if the user was successfully removed, {@code false} otherwise.
3761 * @throws IllegalArgumentException if {@code user} is {@code null}
Varun Shahe38c6782018-12-04 16:57:49 -08003762 * @hide
3763 */
3764 @SystemApi
Valentin Iftime89df4c82019-08-23 13:02:50 +02003765 @RequiresPermission(anyOf = {Manifest.permission.MANAGE_USERS,
3766 Manifest.permission.CREATE_USERS})
Varun Shah9533e622019-02-27 15:26:56 -08003767 public boolean removeUser(@NonNull UserHandle user) {
3768 if (user == null) {
3769 throw new IllegalArgumentException("user cannot be null");
3770 }
Varun Shahe38c6782018-12-04 16:57:49 -08003771 return removeUser(user.getIdentifier());
3772 }
3773
3774
3775 /**
Nicolas Prevotd37c4a92017-01-23 11:56:00 +00003776 * Similar to {@link #removeUser(int)} except bypassing the checking of
3777 * {@link UserManager#DISALLOW_REMOVE_USER}
3778 * or {@link UserManager#DISALLOW_REMOVE_MANAGED_PROFILE}.
3779 *
3780 * @see {@link #removeUser(int)}
3781 * @hide
3782 */
Valentin Iftime89df4c82019-08-23 13:02:50 +02003783 @RequiresPermission(anyOf = {Manifest.permission.MANAGE_USERS,
3784 Manifest.permission.CREATE_USERS})
Bookatzf56f2582019-09-04 16:06:41 -07003785 public boolean removeUserEvenWhenDisallowed(@UserIdInt int userId) {
Nicolas Prevotd37c4a92017-01-23 11:56:00 +00003786 try {
Bookatzf56f2582019-09-04 16:06:41 -07003787 return mService.removeUserEvenWhenDisallowed(userId);
Nicolas Prevotd37c4a92017-01-23 11:56:00 +00003788 } catch (RemoteException re) {
3789 throw re.rethrowFromSystemServer();
3790 }
3791 }
3792
3793 /**
Amith Yamasani258848d2012-08-10 17:06:33 -07003794 * Updates the user's name.
3795 *
Bookatzf56f2582019-09-04 16:06:41 -07003796 * @param userId the user's integer id
Amith Yamasani258848d2012-08-10 17:06:33 -07003797 * @param name the new name for the user
3798 * @hide
3799 */
Valentin Iftime89df4c82019-08-23 13:02:50 +02003800 @RequiresPermission(android.Manifest.permission.MANAGE_USERS)
Bookatzf56f2582019-09-04 16:06:41 -07003801 public void setUserName(@UserIdInt int userId, String name) {
Amith Yamasani258848d2012-08-10 17:06:33 -07003802 try {
Bookatzf56f2582019-09-04 16:06:41 -07003803 mService.setUserName(userId, name);
Amith Yamasani258848d2012-08-10 17:06:33 -07003804 } catch (RemoteException re) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07003805 throw re.rethrowFromSystemServer();
Amith Yamasani258848d2012-08-10 17:06:33 -07003806 }
3807 }
3808
3809 /**
Valentin Iftime89df4c82019-08-23 13:02:50 +02003810 * Updates the context user's name.
Leo Hsuab28fff2019-01-30 15:40:56 +08003811 *
3812 * @param name the new name for the user
3813 * @hide
3814 */
3815 @SystemApi
3816 @RequiresPermission(android.Manifest.permission.MANAGE_USERS)
Valentin Iftime89df4c82019-08-23 13:02:50 +02003817 @UserHandleAware
Leo Hsu51839332019-03-05 16:59:04 +08003818 public void setUserName(@Nullable String name) {
Valentin Iftime89df4c82019-08-23 13:02:50 +02003819 setUserName(mUserId, name);
Leo Hsuab28fff2019-01-30 15:40:56 +08003820 }
3821
3822 /**
Amith Yamasanie928d7d2012-09-17 21:46:51 -07003823 * Sets the user's photo.
Bookatzf56f2582019-09-04 16:06:41 -07003824 * @param userId the user for whom to change the photo.
Amith Yamasanie928d7d2012-09-17 21:46:51 -07003825 * @param icon the bitmap to set as the photo.
Amith Yamasani258848d2012-08-10 17:06:33 -07003826 * @hide
3827 */
Valentin Iftime89df4c82019-08-23 13:02:50 +02003828 @RequiresPermission(android.Manifest.permission.MANAGE_USERS)
3829 public void setUserIcon(@UserIdInt int userId, @NonNull Bitmap icon)
3830 throws UserOperationException {
Amith Yamasani258848d2012-08-10 17:06:33 -07003831 try {
Bookatzf56f2582019-09-04 16:06:41 -07003832 mService.setUserIcon(userId, icon);
Valentin Iftime89df4c82019-08-23 13:02:50 +02003833 } catch (ServiceSpecificException e) {
3834 throw UserOperationException.from(e);
Amith Yamasani258848d2012-08-10 17:06:33 -07003835 } catch (RemoteException re) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07003836 throw re.rethrowFromSystemServer();
Amith Yamasani258848d2012-08-10 17:06:33 -07003837 }
3838 }
3839
3840 /**
Valentin Iftime89df4c82019-08-23 13:02:50 +02003841 * Sets the context user's photo.
Leo Hsuab28fff2019-01-30 15:40:56 +08003842 *
3843 * @param icon the bitmap to set as the photo.
3844 * @hide
3845 */
3846 @SystemApi
3847 @RequiresPermission(android.Manifest.permission.MANAGE_USERS)
Valentin Iftime89df4c82019-08-23 13:02:50 +02003848 @UserHandleAware
3849 public void setUserIcon(@NonNull Bitmap icon) throws UserOperationException {
3850 setUserIcon(mUserId, icon);
Leo Hsuab28fff2019-01-30 15:40:56 +08003851 }
3852
3853 /**
Valentin Iftime89df4c82019-08-23 13:02:50 +02003854 * Returns a bitmap of the user's photo
Bookatzf56f2582019-09-04 16:06:41 -07003855 * @param userId the user whose photo we want to read.
Amith Yamasanie928d7d2012-09-17 21:46:51 -07003856 * @return a {@link Bitmap} of the user's photo, or null if there's no photo.
Alexandra Gherghina64d4dca2014-08-28 18:26:56 +01003857 * @see com.android.internal.util.UserIcons#getDefaultUserIcon for a default.
Amith Yamasani3b49f072012-09-17 10:21:43 -07003858 * @hide
3859 */
Andrei Onea24ec3212019-03-15 17:35:05 +00003860 @UnsupportedAppUsage
Valentin Iftime89df4c82019-08-23 13:02:50 +02003861 @RequiresPermission(anyOf = {android.Manifest.permission.MANAGE_USERS,
3862 android.Manifest.permission.GET_ACCOUNTS_PRIVILEGED})
Bookatzf56f2582019-09-04 16:06:41 -07003863 public Bitmap getUserIcon(@UserIdInt int userId) {
Amith Yamasani3b49f072012-09-17 10:21:43 -07003864 try {
Bookatzf56f2582019-09-04 16:06:41 -07003865 ParcelFileDescriptor fd = mService.getUserIcon(userId);
Adrian Roos1bdff912015-02-17 15:51:35 +01003866 if (fd != null) {
3867 try {
3868 return BitmapFactory.decodeFileDescriptor(fd.getFileDescriptor());
3869 } finally {
3870 try {
3871 fd.close();
3872 } catch (IOException e) {
3873 }
3874 }
3875 }
Amith Yamasani3b49f072012-09-17 10:21:43 -07003876 } catch (RemoteException re) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07003877 throw re.rethrowFromSystemServer();
Amith Yamasani3b49f072012-09-17 10:21:43 -07003878 }
Adrian Roos1bdff912015-02-17 15:51:35 +01003879 return null;
Amith Yamasani3b49f072012-09-17 10:21:43 -07003880 }
3881
3882 /**
Valentin Iftime89df4c82019-08-23 13:02:50 +02003883 * Returns a Bitmap for the context user's photo.
Leo Hsuab28fff2019-01-30 15:40:56 +08003884 *
3885 * @return a {@link Bitmap} of the user's photo, or null if there's no photo.
3886 * @see com.android.internal.util.UserIcons#getDefaultUserIcon for a default.
3887 * @hide
3888 */
3889 @SystemApi
Bookatzcde3d922019-03-08 14:30:00 -08003890 @RequiresPermission(anyOf = {android.Manifest.permission.MANAGE_USERS,
3891 android.Manifest.permission.GET_ACCOUNTS_PRIVILEGED})
Valentin Iftime89df4c82019-08-23 13:02:50 +02003892 @UserHandleAware
Leo Hsu51839332019-03-05 16:59:04 +08003893 public @Nullable Bitmap getUserIcon() {
Valentin Iftime89df4c82019-08-23 13:02:50 +02003894 return getUserIcon(mUserId);
Leo Hsuab28fff2019-01-30 15:40:56 +08003895 }
3896
3897 /**
Amith Yamasani258848d2012-08-10 17:06:33 -07003898 * Returns the maximum number of users that can be created on this device. A return value
3899 * of 1 means that it is a single user device.
3900 * @hide
Maggie Benthalla12fccf2013-03-14 18:02:12 -04003901 * @return a value greater than or equal to 1
Amith Yamasani258848d2012-08-10 17:06:33 -07003902 */
Andrei Onea24ec3212019-03-15 17:35:05 +00003903 @UnsupportedAppUsage
Jeff Sharkey27bd34d2012-09-16 12:49:00 -07003904 public static int getMaxSupportedUsers() {
Amith Yamasaniff549202012-10-12 12:44:49 -07003905 // Don't allow multiple users on certain builds
3906 if (android.os.Build.ID.startsWith("JVP")) return 1;
Robin Lee83c79312018-02-09 19:27:18 +01003907 if (ActivityManager.isLowRamDeviceStatic()) {
3908 // Low-ram devices are Svelte. Most of the time they don't get multi-user.
3909 if ((Resources.getSystem().getConfiguration().uiMode & Configuration.UI_MODE_TYPE_MASK)
3910 != Configuration.UI_MODE_TYPE_TELEVISION) {
3911 return 1;
3912 }
3913 }
Jeff Sharkey27bd34d2012-09-16 12:49:00 -07003914 return SystemProperties.getInt("fw.max_users",
3915 Resources.getSystem().getInteger(R.integer.config_multiuserMaximumUsers));
Amith Yamasani258848d2012-08-10 17:06:33 -07003916 }
Amith Yamasani2a003292012-08-14 18:25:45 -07003917
3918 /**
Fyodor Kupolovcd86ebf2015-09-29 17:06:50 -07003919 * Returns true if the user switcher should be shown, this will be if device supports multi-user
3920 * and there are at least 2 users available that are not managed profiles.
Kenny Guy1a447532014-02-20 21:55:32 +00003921 * @hide
3922 * @return true if user switcher should be shown.
3923 */
3924 public boolean isUserSwitcherEnabled() {
Fyodor Kupolovcd86ebf2015-09-29 17:06:50 -07003925 if (!supportsMultipleUsers()) {
3926 return false;
3927 }
Benjamin Franzff66fa92017-08-10 10:39:44 +01003928 if (hasUserRestriction(DISALLOW_USER_SWITCH)) {
3929 return false;
3930 }
Amith Yamasanieb437d42016-04-29 09:31:25 -07003931 // If Demo Mode is on, don't show user switcher
3932 if (isDeviceInDemoMode(mContext)) {
3933 return false;
3934 }
Fan Zhangc69c8b62018-06-05 13:30:11 -07003935 // If user disabled this feature, don't show switcher
3936 final boolean userSwitcherEnabled = Settings.Global.getInt(mContext.getContentResolver(),
3937 Settings.Global.USER_SWITCHER_ENABLED, 1) != 0;
3938 if (!userSwitcherEnabled) {
3939 return false;
3940 }
Kenny Guy1a447532014-02-20 21:55:32 +00003941 List<UserInfo> users = getUsers(true);
3942 if (users == null) {
3943 return false;
3944 }
3945 int switchableUserCount = 0;
3946 for (UserInfo user : users) {
Xiaohui Chen7cb69df2015-07-13 16:01:01 -07003947 if (user.supportsSwitchToByUser()) {
Kenny Guy1a447532014-02-20 21:55:32 +00003948 ++switchableUserCount;
3949 }
3950 }
Fyodor Kupolovcd86ebf2015-09-29 17:06:50 -07003951 final boolean guestEnabled = !mContext.getSystemService(DevicePolicyManager.class)
3952 .getGuestUserDisabled(null);
Amith Yamasania596ff82014-06-12 18:12:38 -07003953 return switchableUserCount > 1 || guestEnabled;
Kenny Guy1a447532014-02-20 21:55:32 +00003954 }
3955
3956 /**
Amith Yamasanieb437d42016-04-29 09:31:25 -07003957 * @hide
3958 */
Andrei Onea24ec3212019-03-15 17:35:05 +00003959 @UnsupportedAppUsage
Amith Yamasanieb437d42016-04-29 09:31:25 -07003960 public static boolean isDeviceInDemoMode(Context context) {
3961 return Settings.Global.getInt(context.getContentResolver(),
3962 Settings.Global.DEVICE_DEMO_MODE, 0) > 0;
3963 }
3964
3965 /**
Bookatzf56f2582019-09-04 16:06:41 -07003966 * Returns a serial number on this device for a given userId. User handles can be recycled
Amith Yamasani2a003292012-08-14 18:25:45 -07003967 * when deleting and creating users, but serial numbers are not reused until the device is wiped.
Bookatzf56f2582019-09-04 16:06:41 -07003968 * @param userId
3969 * @return a serial number associated with that user, or -1 if the userId is not valid.
Amith Yamasani2a003292012-08-14 18:25:45 -07003970 * @hide
3971 */
Andrei Onea24ec3212019-03-15 17:35:05 +00003972 @UnsupportedAppUsage
Bookatzf56f2582019-09-04 16:06:41 -07003973 public int getUserSerialNumber(@UserIdInt int userId) {
Amith Yamasani2a003292012-08-14 18:25:45 -07003974 try {
Bookatzf56f2582019-09-04 16:06:41 -07003975 return mService.getUserSerialNumber(userId);
Amith Yamasani2a003292012-08-14 18:25:45 -07003976 } catch (RemoteException re) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07003977 throw re.rethrowFromSystemServer();
Amith Yamasani2a003292012-08-14 18:25:45 -07003978 }
Amith Yamasani2a003292012-08-14 18:25:45 -07003979 }
3980
3981 /**
Bookatzf56f2582019-09-04 16:06:41 -07003982 * Returns a userId on this device for a given user serial number. User handles can be
Amith Yamasani2a003292012-08-14 18:25:45 -07003983 * recycled when deleting and creating users, but serial numbers are not reused until the device
3984 * is wiped.
3985 * @param userSerialNumber
Bookatzf56f2582019-09-04 16:06:41 -07003986 * @return the userId associated with that user serial number, or -1 if the serial number
Amith Yamasani2a003292012-08-14 18:25:45 -07003987 * is not valid.
3988 * @hide
3989 */
Andrei Onea24ec3212019-03-15 17:35:05 +00003990 @UnsupportedAppUsage
Jeff Sharkey8588bc12016-01-06 16:47:42 -07003991 public @UserIdInt int getUserHandle(int userSerialNumber) {
Amith Yamasani2a003292012-08-14 18:25:45 -07003992 try {
3993 return mService.getUserHandle(userSerialNumber);
3994 } catch (RemoteException re) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07003995 throw re.rethrowFromSystemServer();
Amith Yamasani2a003292012-08-14 18:25:45 -07003996 }
Amith Yamasani2a003292012-08-14 18:25:45 -07003997 }
Maggie Benthall67944582013-02-22 14:58:27 -05003998
Amith Yamasani7e99bc02013-04-16 18:24:51 -07003999 /**
Esteban Talavera953fe482016-06-07 15:25:20 +01004000 * Returns a {@link Bundle} containing any saved application restrictions for this user, for the
Amith Yamasani7e99bc02013-04-16 18:24:51 -07004001 * given package name. Only an application with this package name can call this method.
Esteban Talavera5b9f1672015-12-11 15:22:34 +00004002 *
4003 * <p>The returned {@link Bundle} consists of key-value pairs, as defined by the application,
4004 * where the types of values may be:
4005 * <ul>
4006 * <li>{@code boolean}
4007 * <li>{@code int}
4008 * <li>{@code String} or {@code String[]}
4009 * <li>From {@link android.os.Build.VERSION_CODES#M}, {@code Bundle} or {@code Bundle[]}
4010 * </ul>
4011 *
Fyodor Kupolov4e9af062016-07-18 16:59:11 -07004012 * <p>NOTE: The method performs disk I/O and shouldn't be called on the main thread
4013 *
Amith Yamasani7e99bc02013-04-16 18:24:51 -07004014 * @param packageName the package name of the calling application
Esteban Talavera953fe482016-06-07 15:25:20 +01004015 * @return a {@link Bundle} with the restrictions for that package, or an empty {@link Bundle}
4016 * if there are no saved restrictions.
Esteban Talavera5b9f1672015-12-11 15:22:34 +00004017 *
4018 * @see #KEY_RESTRICTIONS_PENDING
Amith Yamasani7e99bc02013-04-16 18:24:51 -07004019 */
Fyodor Kupolov4e9af062016-07-18 16:59:11 -07004020 @WorkerThread
Amith Yamasani7e99bc02013-04-16 18:24:51 -07004021 public Bundle getApplicationRestrictions(String packageName) {
4022 try {
4023 return mService.getApplicationRestrictions(packageName);
4024 } catch (RemoteException re) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07004025 throw re.rethrowFromSystemServer();
Amith Yamasani7e99bc02013-04-16 18:24:51 -07004026 }
Amith Yamasani7e99bc02013-04-16 18:24:51 -07004027 }
Amith Yamasanidf2e92a2013-03-01 17:04:38 -08004028
4029 /**
4030 * @hide
4031 */
Fyodor Kupolov4e9af062016-07-18 16:59:11 -07004032 @WorkerThread
Amith Yamasani7e99bc02013-04-16 18:24:51 -07004033 public Bundle getApplicationRestrictions(String packageName, UserHandle user) {
Amith Yamasanidf2e92a2013-03-01 17:04:38 -08004034 try {
Amith Yamasani7e99bc02013-04-16 18:24:51 -07004035 return mService.getApplicationRestrictionsForUser(packageName, user.getIdentifier());
Amith Yamasanidf2e92a2013-03-01 17:04:38 -08004036 } catch (RemoteException re) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07004037 throw re.rethrowFromSystemServer();
Amith Yamasanidf2e92a2013-03-01 17:04:38 -08004038 }
Amith Yamasanidf2e92a2013-03-01 17:04:38 -08004039 }
4040
4041 /**
4042 * @hide
4043 */
Fyodor Kupolov4e9af062016-07-18 16:59:11 -07004044 @WorkerThread
Amith Yamasani7e99bc02013-04-16 18:24:51 -07004045 public void setApplicationRestrictions(String packageName, Bundle restrictions,
Amith Yamasanidf2e92a2013-03-01 17:04:38 -08004046 UserHandle user) {
4047 try {
Amith Yamasani7e99bc02013-04-16 18:24:51 -07004048 mService.setApplicationRestrictions(packageName, restrictions, user.getIdentifier());
Amith Yamasanidf2e92a2013-03-01 17:04:38 -08004049 } catch (RemoteException re) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07004050 throw re.rethrowFromSystemServer();
Amith Yamasanidf2e92a2013-03-01 17:04:38 -08004051 }
4052 }
Amith Yamasani655d0e22013-06-12 14:19:10 -07004053
4054 /**
Amith Yamasanid304af62013-09-05 09:30:23 -07004055 * Sets a new challenge PIN for restrictions. This is only for use by pre-installed
4056 * apps and requires the MANAGE_USERS permission.
4057 * @param newPin the PIN to use for challenge dialogs.
4058 * @return Returns true if the challenge PIN was set successfully.
Fyodor Kupolovef249092015-05-06 13:18:46 -07004059 * @deprecated The restrictions PIN functionality is no longer provided by the system.
4060 * This method is preserved for backwards compatibility reasons and always returns false.
Amith Yamasani655d0e22013-06-12 14:19:10 -07004061 */
Aurimas Liutikas514c5ef2016-05-24 15:22:55 -07004062 @Deprecated
Amith Yamasanid304af62013-09-05 09:30:23 -07004063 public boolean setRestrictionsChallenge(String newPin) {
Amith Yamasani655d0e22013-06-12 14:19:10 -07004064 return false;
4065 }
Amith Yamasani1a7472e2013-07-02 11:17:30 -07004066
Amith Yamasanie4afaa32014-06-30 14:55:07 +05304067 /**
4068 * @hide
4069 * Set restrictions that should apply to any future guest user that's created.
4070 */
Valentin Iftime89df4c82019-08-23 13:02:50 +02004071 @RequiresPermission(android.Manifest.permission.MANAGE_USERS)
Amith Yamasanie4afaa32014-06-30 14:55:07 +05304072 public void setDefaultGuestRestrictions(Bundle restrictions) {
4073 try {
4074 mService.setDefaultGuestRestrictions(restrictions);
4075 } catch (RemoteException re) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07004076 throw re.rethrowFromSystemServer();
Amith Yamasanie4afaa32014-06-30 14:55:07 +05304077 }
4078 }
4079
4080 /**
4081 * @hide
4082 * Gets the default guest restrictions.
4083 */
Valentin Iftime89df4c82019-08-23 13:02:50 +02004084 @RequiresPermission(android.Manifest.permission.MANAGE_USERS)
Amith Yamasanie4afaa32014-06-30 14:55:07 +05304085 public Bundle getDefaultGuestRestrictions() {
4086 try {
4087 return mService.getDefaultGuestRestrictions();
4088 } catch (RemoteException re) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07004089 throw re.rethrowFromSystemServer();
Amith Yamasanie4afaa32014-06-30 14:55:07 +05304090 }
Amith Yamasanie4afaa32014-06-30 14:55:07 +05304091 }
Fyodor Kupolovff7233e2015-04-08 11:28:52 -07004092
4093 /**
4094 * Returns creation time of the user or of a managed profile associated with the calling user.
4095 * @param userHandle user handle of the user or a managed profile associated with the
4096 * calling user.
4097 * @return creation time in milliseconds since Epoch time.
4098 */
Fyodor Kupolov385de622015-04-10 18:00:19 -07004099 public long getUserCreationTime(UserHandle userHandle) {
Fyodor Kupolovff7233e2015-04-08 11:28:52 -07004100 try {
Fyodor Kupolov385de622015-04-10 18:00:19 -07004101 return mService.getUserCreationTime(userHandle.getIdentifier());
Fyodor Kupolovff7233e2015-04-08 11:28:52 -07004102 } catch (RemoteException re) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07004103 throw re.rethrowFromSystemServer();
Fyodor Kupolovff7233e2015-04-08 11:28:52 -07004104 }
4105 }
Amith Yamasani12747872015-12-07 14:19:49 -08004106
4107 /**
4108 * @hide
4109 * Checks if any uninitialized user has the specific seed account name and type.
4110 *
Pavel Grafov6a40f092016-10-25 15:46:51 +01004111 * @param accountName The account name to check for
4112 * @param accountType The account type of the account to check for
Amith Yamasani12747872015-12-07 14:19:49 -08004113 * @return whether the seed account was found
4114 */
Valentin Iftime89df4c82019-08-23 13:02:50 +02004115 @RequiresPermission(android.Manifest.permission.MANAGE_USERS)
Amith Yamasani12747872015-12-07 14:19:49 -08004116 public boolean someUserHasSeedAccount(String accountName, String accountType) {
4117 try {
4118 return mService.someUserHasSeedAccount(accountName, accountType);
4119 } catch (RemoteException re) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07004120 throw re.rethrowFromSystemServer();
Amith Yamasani12747872015-12-07 14:19:49 -08004121 }
4122 }
Pavel Grafov6a40f092016-10-25 15:46:51 +01004123
4124 /**
4125 * @hide
4126 * User that enforces a restriction.
4127 *
4128 * @see #getUserRestrictionSources(String, UserHandle)
4129 */
4130 @SystemApi
4131 public static final class EnforcingUser implements Parcelable {
4132 private final @UserIdInt int userId;
4133 private final @UserRestrictionSource int userRestrictionSource;
4134
4135 /**
4136 * @hide
4137 */
4138 public EnforcingUser(
4139 @UserIdInt int userId, @UserRestrictionSource int userRestrictionSource) {
4140 this.userId = userId;
4141 this.userRestrictionSource = userRestrictionSource;
4142 }
4143
4144 private EnforcingUser(Parcel in) {
4145 userId = in.readInt();
4146 userRestrictionSource = in.readInt();
4147 }
4148
Jeff Sharkey9e8f83d2019-02-28 12:06:45 -07004149 public static final @android.annotation.NonNull Creator<EnforcingUser> CREATOR = new Creator<EnforcingUser>() {
Pavel Grafov6a40f092016-10-25 15:46:51 +01004150 @Override
4151 public EnforcingUser createFromParcel(Parcel in) {
4152 return new EnforcingUser(in);
4153 }
4154
4155 @Override
4156 public EnforcingUser[] newArray(int size) {
4157 return new EnforcingUser[size];
4158 }
4159 };
4160
4161 @Override
4162 public int describeContents() {
4163 return 0;
4164 }
4165
4166 @Override
4167 public void writeToParcel(Parcel dest, int flags) {
4168 dest.writeInt(userId);
4169 dest.writeInt(userRestrictionSource);
4170 }
4171
4172 /**
4173 * Returns an id of the enforcing user.
4174 *
4175 * <p> Will be UserHandle.USER_NULL when restriction is set by the system.
4176 */
4177 public UserHandle getUserHandle() {
4178 return UserHandle.of(userId);
4179 }
4180
4181 /**
4182 * Returns the status of the enforcing user.
4183 *
4184 * <p> One of {@link #RESTRICTION_SOURCE_SYSTEM},
4185 * {@link #RESTRICTION_SOURCE_DEVICE_OWNER} and
4186 * {@link #RESTRICTION_SOURCE_PROFILE_OWNER}
4187 */
4188 public @UserRestrictionSource int getUserRestrictionSource() {
4189 return userRestrictionSource;
4190 }
4191 }
Amith Yamasani258848d2012-08-10 17:06:33 -07004192}