blob: 42633778a3213c0191758f09179eacd00f7afbed [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;
Zoltan Szatmary-Bane7834602016-04-08 18:41:11 +010021import android.annotation.IntDef;
Adam Lesinskiada8deb2017-05-12 13:50:42 -070022import android.annotation.NonNull;
Xiaohui Chen7cb69df2015-07-13 16:01:01 -070023import android.annotation.Nullable;
Xiaohui Chenb3b92582015-12-07 11:22:13 -080024import android.annotation.RequiresPermission;
Amith Yamasani0e8d7d62014-09-03 13:17:28 -070025import android.annotation.SystemApi;
Jeff Sharkeyd86b8fe2017-06-02 17:36:26 -060026import android.annotation.SystemService;
Makoto Onukie72f81b2017-03-16 14:08:19 -070027import android.annotation.TestApi;
Jeff Sharkey8588bc12016-01-06 16:47:42 -070028import android.annotation.UserIdInt;
Fyodor Kupolov4e9af062016-07-18 16:59:11 -070029import android.annotation.WorkerThread;
Amith Yamasani37ed8d12016-01-27 14:40:16 -080030import android.app.Activity;
Dianne Hackborn409297d2014-07-10 17:39:20 -070031import android.app.ActivityManager;
Fyodor Kupolovcd86ebf2015-09-29 17:06:50 -070032import android.app.admin.DevicePolicyManager;
Makoto Onuki068c54a2015-10-13 14:34:03 -070033import android.content.ComponentName;
Amith Yamasani258848d2012-08-10 17:06:33 -070034import android.content.Context;
Amith Yamasani12747872015-12-07 14:19:49 -080035import android.content.Intent;
Esteban Talavera8bd7c522017-02-13 12:35:04 +000036import android.content.IntentFilter;
Benjamin Franzf02420c2016-04-04 18:52:21 +010037import android.content.IntentSender;
Amith Yamasani258848d2012-08-10 17:06:33 -070038import android.content.pm.UserInfo;
Robin Lee83c79312018-02-09 19:27:18 +010039import android.content.res.Configuration;
Jeff Sharkey27bd34d2012-09-16 12:49:00 -070040import android.content.res.Resources;
Maggie Benthall67944582013-02-22 14:58:27 -050041import android.graphics.Bitmap;
Adrian Roos1bdff912015-02-17 15:51:35 +010042import android.graphics.BitmapFactory;
Amith Yamasani4f582632014-02-19 14:31:52 -080043import android.graphics.Rect;
Amith Yamasani4f582632014-02-19 14:31:52 -080044import android.graphics.drawable.Drawable;
Amith Yamasani1e9c2182014-06-11 17:25:51 -070045import android.provider.Settings;
Fyodor Kupolov523c4042016-02-24 15:03:13 -080046import android.telephony.TelephonyManager;
Jason Monk1c7c3192014-06-26 12:52:18 -040047import android.view.WindowManager.LayoutParams;
Amith Yamasani258848d2012-08-10 17:06:33 -070048
Maggie Benthall67944582013-02-22 14:58:27 -050049import com.android.internal.R;
John Reckaa67f682016-09-20 14:24:21 -070050import com.android.internal.os.RoSystemProperties;
Maggie Benthall67944582013-02-22 14:58:27 -050051
Adrian Roos1bdff912015-02-17 15:51:35 +010052import java.io.IOException;
Zoltan Szatmary-Bane7834602016-04-08 18:41:11 +010053import java.lang.annotation.Retention;
54import java.lang.annotation.RetentionPolicy;
Amith Yamasani4f582632014-02-19 14:31:52 -080055import java.util.ArrayList;
Amith Yamasani258848d2012-08-10 17:06:33 -070056import java.util.List;
57
58/**
Amith Yamasani06964342016-04-15 13:55:01 -070059 * Manages users and user details on a multi-user system. There are two major categories of
60 * users: fully customizable users with their own login, and managed profiles that share a workspace
61 * with a related user.
62 * <p>
63 * Users are different from accounts, which are managed by
64 * {@link AccountManager}. Each user can have their own set of accounts.
65 * <p>
66 * See {@link DevicePolicyManager#ACTION_PROVISION_MANAGED_PROFILE} for more on managed profiles.
Amith Yamasani258848d2012-08-10 17:06:33 -070067 */
Jeff Sharkeyd86b8fe2017-06-02 17:36:26 -060068@SystemService(Context.USER_SERVICE)
Amith Yamasani258848d2012-08-10 17:06:33 -070069public class UserManager {
70
Pavel Grafov4f4f6f82017-03-28 13:44:04 +010071 private static final String TAG = "UserManager";
Amith Yamasani258848d2012-08-10 17:06:33 -070072 private final IUserManager mService;
73 private final Context mContext;
74
Makoto Onukid49f3fa2017-01-25 14:09:48 -080075 private Boolean mIsManagedProfileCached;
76
Amith Yamasanie4cf7342012-12-17 11:12:09 -080077 /**
Zoltan Szatmary-Bane7834602016-04-08 18:41:11 +010078 * @hide
79 * No user restriction.
80 */
81 @SystemApi
82 public static final int RESTRICTION_NOT_SET = 0x0;
83
84 /**
85 * @hide
86 * User restriction set by system/user.
87 */
88 @SystemApi
89 public static final int RESTRICTION_SOURCE_SYSTEM = 0x1;
90
91 /**
92 * @hide
93 * User restriction set by a device owner.
94 */
95 @SystemApi
96 public static final int RESTRICTION_SOURCE_DEVICE_OWNER = 0x2;
97
98 /**
99 * @hide
100 * User restriction set by a profile owner.
101 */
102 @SystemApi
103 public static final int RESTRICTION_SOURCE_PROFILE_OWNER = 0x4;
104
105 /** @hide */
106 @Retention(RetentionPolicy.SOURCE)
Jeff Sharkeyce8db992017-12-13 20:05:05 -0700107 @IntDef(flag = true, prefix = { "RESTRICTION_" }, value = {
108 RESTRICTION_NOT_SET,
109 RESTRICTION_SOURCE_SYSTEM,
110 RESTRICTION_SOURCE_DEVICE_OWNER,
111 RESTRICTION_SOURCE_PROFILE_OWNER
112 })
Zoltan Szatmary-Bane7834602016-04-08 18:41:11 +0100113 @SystemApi
114 public @interface UserRestrictionSource {}
115
116 /**
Fyodor Kupolov53019282015-07-21 11:48:18 -0700117 * Specifies if a user is disallowed from adding and removing accounts, unless they are
118 * {@link android.accounts.AccountManager#addAccountExplicitly programmatically} added by
119 * Authenticator.
Amith Yamasani71e6c692013-03-24 17:39:28 -0700120 * The default value is <code>false</code>.
Amith Yamasani26af8292014-09-09 09:57:27 -0700121 *
Benjamin Franzb6c0ce42015-11-05 10:06:51 +0000122 * <p>From {@link android.os.Build.VERSION_CODES#N} a profile or device owner app can still
123 * use {@link android.accounts.AccountManager} APIs to add or remove accounts when account
124 * management is disallowed.
125 *
Fyodor Kupolov0ffbfea2016-01-19 10:07:54 -0800126 * <p>Key for user restrictions.
127 * <p>Type: Boolean
Makoto Onuki068c54a2015-10-13 14:34:03 -0700128 * @see DevicePolicyManager#addUserRestriction(ComponentName, String)
129 * @see DevicePolicyManager#clearUserRestriction(ComponentName, String)
Amith Yamasanie4cf7342012-12-17 11:12:09 -0800130 * @see #getUserRestrictions()
131 */
Amith Yamasani71e6c692013-03-24 17:39:28 -0700132 public static final String DISALLOW_MODIFY_ACCOUNTS = "no_modify_accounts";
Amith Yamasanie4cf7342012-12-17 11:12:09 -0800133
134 /**
Amith Yamasani26af8292014-09-09 09:57:27 -0700135 * Specifies if a user is disallowed from changing Wi-Fi
Julia Reynolds2cb384f2014-08-13 15:15:55 -0400136 * access points. The default value is <code>false</code>.
Fyodor Kupolov0ffbfea2016-01-19 10:07:54 -0800137 * <p>This restriction has no effect in a managed profile.
Amith Yamasani26af8292014-09-09 09:57:27 -0700138 *
Fyodor Kupolov0ffbfea2016-01-19 10:07:54 -0800139 * <p>Key for user restrictions.
140 * <p>Type: Boolean
Makoto Onuki068c54a2015-10-13 14:34:03 -0700141 * @see DevicePolicyManager#addUserRestriction(ComponentName, String)
142 * @see DevicePolicyManager#clearUserRestriction(ComponentName, String)
Amith Yamasanie4cf7342012-12-17 11:12:09 -0800143 * @see #getUserRestrictions()
144 */
Amith Yamasani71e6c692013-03-24 17:39:28 -0700145 public static final String DISALLOW_CONFIG_WIFI = "no_config_wifi";
Amith Yamasanie4cf7342012-12-17 11:12:09 -0800146
147 /**
Christine Franks1bade5d2017-10-10 15:41:50 -0700148 * Specifies if a user is disallowed from changing the device
149 * language. The default value is <code>false</code>.
150 *
151 * <p>Key for user restrictions.
152 * <p>Type: Boolean
153 * @see DevicePolicyManager#addUserRestriction(ComponentName, String)
154 * @see DevicePolicyManager#clearUserRestriction(ComponentName, String)
155 * @see #getUserRestrictions()
156 */
157 public static final String DISALLOW_CONFIG_LOCALE = "no_config_locale";
158
159 /**
Benjamin Millerf1c3ed12018-05-03 10:56:28 +0000160 * Specifies if a user is disallowed from installing applications. This user restriction also
161 * prevents device owners and profile owners installing apps. The default value is
162 * {@code false}.
Amith Yamasani26af8292014-09-09 09:57:27 -0700163 *
Fyodor Kupolov0ffbfea2016-01-19 10:07:54 -0800164 * <p>Key for user restrictions.
165 * <p>Type: Boolean
Makoto Onuki068c54a2015-10-13 14:34:03 -0700166 * @see DevicePolicyManager#addUserRestriction(ComponentName, String)
167 * @see DevicePolicyManager#clearUserRestriction(ComponentName, String)
Amith Yamasanie4cf7342012-12-17 11:12:09 -0800168 * @see #getUserRestrictions()
169 */
Amith Yamasani71e6c692013-03-24 17:39:28 -0700170 public static final String DISALLOW_INSTALL_APPS = "no_install_apps";
Amith Yamasanie4cf7342012-12-17 11:12:09 -0800171
172 /**
Amith Yamasani26af8292014-09-09 09:57:27 -0700173 * Specifies if a user is disallowed from uninstalling applications.
Amith Yamasani71e6c692013-03-24 17:39:28 -0700174 * The default value is <code>false</code>.
Amith Yamasani26af8292014-09-09 09:57:27 -0700175 *
Fyodor Kupolov0ffbfea2016-01-19 10:07:54 -0800176 * <p>Key for user restrictions.
177 * <p>Type: Boolean
Makoto Onuki068c54a2015-10-13 14:34:03 -0700178 * @see DevicePolicyManager#addUserRestriction(ComponentName, String)
179 * @see DevicePolicyManager#clearUserRestriction(ComponentName, String)
Amith Yamasanie4cf7342012-12-17 11:12:09 -0800180 * @see #getUserRestrictions()
181 */
Amith Yamasani71e6c692013-03-24 17:39:28 -0700182 public static final String DISALLOW_UNINSTALL_APPS = "no_uninstall_apps";
Amith Yamasanie4cf7342012-12-17 11:12:09 -0800183
Amith Yamasani71e6c692013-03-24 17:39:28 -0700184 /**
Amith Yamasani150514b2015-01-07 16:05:05 -0800185 * Specifies if a user is disallowed from turning on location sharing.
Amith Yamasani71e6c692013-03-24 17:39:28 -0700186 * The default value is <code>false</code>.
Fyodor Kupolov0ffbfea2016-01-19 10:07:54 -0800187 * <p>In a managed profile, location sharing always reflects the primary user's setting, but
Amith Yamasani150514b2015-01-07 16:05:05 -0800188 * can be overridden and forced off by setting this restriction to true in the managed profile.
Amith Yamasani26af8292014-09-09 09:57:27 -0700189 *
Fyodor Kupolov0ffbfea2016-01-19 10:07:54 -0800190 * <p>Key for user restrictions.
191 * <p>Type: Boolean
Makoto Onuki068c54a2015-10-13 14:34:03 -0700192 * @see DevicePolicyManager#addUserRestriction(ComponentName, String)
193 * @see DevicePolicyManager#clearUserRestriction(ComponentName, String)
Maggie Benthall67944582013-02-22 14:58:27 -0500194 * @see #getUserRestrictions()
195 */
Amith Yamasani71e6c692013-03-24 17:39:28 -0700196 public static final String DISALLOW_SHARE_LOCATION = "no_share_location";
Maggie Benthall67944582013-02-22 14:58:27 -0500197
Maggie Benthalla12fccf2013-03-14 18:02:12 -0400198 /**
yuemingw5fe75dc2017-11-29 15:52:56 +0000199 * Specifies if airplane mode is disallowed on the device.
200 *
201 * <p> This restriction can only be set by the device owner and the profile owner on the
202 * primary user and it applies globally - i.e. it disables airplane mode on the entire device.
203 * <p>The default value is <code>false</code>.
204 *
205 * <p>Key for user restrictions.
206 * <p>Type: Boolean
207 * @see DevicePolicyManager#addUserRestriction(ComponentName, String)
208 * @see DevicePolicyManager#clearUserRestriction(ComponentName, String)
209 * @see #getUserRestrictions()
210 */
211 public static final String DISALLOW_AIRPLANE_MODE = "no_airplane_mode";
212
213 /**
yuemingwc6ac29d2018-01-10 16:54:08 +0000214 * Specifies if a user is disallowed from configuring brightness. When device owner sets it,
215 * it'll only be applied on the target(system) user.
216 *
217 * <p>The default value is <code>false</code>.
218 *
219 * <p>This user restriction has no effect on managed profiles.
220 * <p>Key for user restrictions.
221 * <p>Type: Boolean
222 * @see DevicePolicyManager#addUserRestriction(ComponentName, String)
223 * @see DevicePolicyManager#clearUserRestriction(ComponentName, String)
224 * @see #getUserRestrictions()
225 */
226 public static final String DISALLOW_CONFIG_BRIGHTNESS = "no_config_brightness";
227
228 /**
yuemingwa1781442018-01-10 18:59:43 +0000229 * Specifies if ambient display is disallowed for the user.
230 *
231 * <p>The default value is <code>false</code>.
232 *
233 * <p>This user restriction has no effect on managed profiles.
234 * <p>Key for user restrictions.
235 * <p>Type: Boolean
236 * @see DevicePolicyManager#addUserRestriction(ComponentName, String)
237 * @see DevicePolicyManager#clearUserRestriction(ComponentName, String)
238 * @see #getUserRestrictions()
239 */
240 public static final String DISALLOW_AMBIENT_DISPLAY = "no_ambient_display";
241
242 /**
yuemingw5cda3ae2018-01-15 10:26:19 +0000243 * Specifies if a user is disallowed from changing screen off timeout.
244 *
245 * <p>The default value is <code>false</code>.
246 *
247 * <p>This user restriction has no effect on managed profiles.
248 * <p>Key for user restrictions.
249 * <p>Type: Boolean
250 * @see DevicePolicyManager#addUserRestriction(ComponentName, String)
251 * @see DevicePolicyManager#clearUserRestriction(ComponentName, String)
252 * @see #getUserRestrictions()
253 */
254 public static final String DISALLOW_CONFIG_SCREEN_TIMEOUT = "no_config_screen_timeout";
255
256 /**
Amith Yamasani26af8292014-09-09 09:57:27 -0700257 * Specifies if a user is disallowed from enabling the
Maggie Benthalla12fccf2013-03-14 18:02:12 -0400258 * "Unknown Sources" setting, that allows installation of apps from unknown sources.
Irina Dumitrescu4638edd2018-09-05 14:08:33 +0100259 * Unknown sources exclude adb and special apps such as trusted app stores.
Maggie Benthalla12fccf2013-03-14 18:02:12 -0400260 * The default value is <code>false</code>.
Amith Yamasani26af8292014-09-09 09:57:27 -0700261 *
Fyodor Kupolov0ffbfea2016-01-19 10:07:54 -0800262 * <p>Key for user restrictions.
263 * <p>Type: Boolean
Makoto Onuki068c54a2015-10-13 14:34:03 -0700264 * @see DevicePolicyManager#addUserRestriction(ComponentName, String)
265 * @see DevicePolicyManager#clearUserRestriction(ComponentName, String)
Maggie Benthalla12fccf2013-03-14 18:02:12 -0400266 * @see #getUserRestrictions()
267 */
268 public static final String DISALLOW_INSTALL_UNKNOWN_SOURCES = "no_install_unknown_sources";
269
270 /**
Tor Norbye33ca9122018-10-03 09:28:21 +0200271 * This restriction is a device-wide version of {@link #DISALLOW_INSTALL_UNKNOWN_SOURCES}.
Irina Dumitrescu4638edd2018-09-05 14:08:33 +0100272 *
273 * Specifies if all users on the device are disallowed from enabling the
274 * "Unknown Sources" setting, that allows installation of apps from unknown sources.
Irina Dumitrescu0501eee2018-10-19 23:50:18 +0100275 *
276 * This restriction can be enabled by the profile owner, in which case all accounts and
277 * profiles will be affected.
278 *
Irina Dumitrescu4638edd2018-09-05 14:08:33 +0100279 * The default value is <code>false</code>.
280 *
281 * <p>Key for user restrictions.
282 * <p>Type: Boolean
283 * @see DevicePolicyManager#addUserRestriction(ComponentName, String)
284 * @see DevicePolicyManager#clearUserRestriction(ComponentName, String)
285 * @see #getUserRestrictions()
286 */
287 public static final String DISALLOW_INSTALL_UNKNOWN_SOURCES_GLOBALLY =
288 "no_install_unknown_sources_globally";
289
290 /**
Amith Yamasani26af8292014-09-09 09:57:27 -0700291 * Specifies if a user is disallowed from configuring bluetooth.
Nicolas Prevot1c4c4422015-02-16 11:32:21 +0000292 * This does <em>not</em> restrict the user from turning bluetooth on or off.
Maggie Benthalla12fccf2013-03-14 18:02:12 -0400293 * The default value is <code>false</code>.
Lenka Trochtova63d5e4a72016-12-02 12:19:39 +0100294 * <p>This restriction doesn't prevent the user from using bluetooth. For disallowing usage of
295 * bluetooth completely on the device, use {@link #DISALLOW_BLUETOOTH}.
Fyodor Kupolov0ffbfea2016-01-19 10:07:54 -0800296 * <p>This restriction has no effect in a managed profile.
Amith Yamasani26af8292014-09-09 09:57:27 -0700297 *
Fyodor Kupolov0ffbfea2016-01-19 10:07:54 -0800298 * <p>Key for user restrictions.
299 * <p>Type: Boolean
Makoto Onuki068c54a2015-10-13 14:34:03 -0700300 * @see DevicePolicyManager#addUserRestriction(ComponentName, String)
301 * @see DevicePolicyManager#clearUserRestriction(ComponentName, String)
Maggie Benthalla12fccf2013-03-14 18:02:12 -0400302 * @see #getUserRestrictions()
303 */
304 public static final String DISALLOW_CONFIG_BLUETOOTH = "no_config_bluetooth";
305
Maggie Benthalla12fccf2013-03-14 18:02:12 -0400306 /**
Lenka Trochtova63d5e4a72016-12-02 12:19:39 +0100307 * Specifies if bluetooth is disallowed on the device.
308 *
309 * <p> This restriction can only be set by the device owner and the profile owner on the
310 * primary user and it applies globally - i.e. it disables bluetooth on the entire device.
311 * <p>The default value is <code>false</code>.
312 * <p>Key for user restrictions.
313 * <p>Type: Boolean
314 * @see DevicePolicyManager#addUserRestriction(ComponentName, String)
315 * @see DevicePolicyManager#clearUserRestriction(ComponentName, String)
316 * @see #getUserRestrictions()
317 */
318 public static final String DISALLOW_BLUETOOTH = "no_bluetooth";
319
320 /**
Pavel Grafov4f4f6f82017-03-28 13:44:04 +0100321 * Specifies if outgoing bluetooth sharing is disallowed on the device. Device owner and profile
322 * owner can set this restriction. When it is set by device owner, all users on this device will
323 * be affected.
324 *
325 * <p>Default is <code>true</code> for managed profiles and false for otherwise. When a device
326 * upgrades to {@link android.os.Build.VERSION_CODES#O}, the system sets it for all existing
327 * managed profiles.
328 *
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_BLUETOOTH_SHARING = "no_bluetooth_sharing";
336
337 /**
Amith Yamasani26af8292014-09-09 09:57:27 -0700338 * Specifies if a user is disallowed from transferring files over
Amith Yamasanic34dc7c2014-09-18 09:42:42 -0700339 * USB. This can only be set by device owners and profile owners on the primary user.
340 * The default value is <code>false</code>.
Amith Yamasani26af8292014-09-09 09:57:27 -0700341 *
Fyodor Kupolov0ffbfea2016-01-19 10:07:54 -0800342 * <p>Key for user restrictions.
343 * <p>Type: Boolean
Makoto Onuki068c54a2015-10-13 14:34:03 -0700344 * @see DevicePolicyManager#addUserRestriction(ComponentName, String)
345 * @see DevicePolicyManager#clearUserRestriction(ComponentName, String)
Maggie Benthalla12fccf2013-03-14 18:02:12 -0400346 * @see #getUserRestrictions()
347 */
348 public static final String DISALLOW_USB_FILE_TRANSFER = "no_usb_file_transfer";
349
Emily Bernierb223f732013-04-11 15:46:36 -0400350 /**
Amith Yamasani26af8292014-09-09 09:57:27 -0700351 * Specifies if a user is disallowed from configuring user
Emily Bernierb223f732013-04-11 15:46:36 -0400352 * credentials. The default value is <code>false</code>.
Amith Yamasani26af8292014-09-09 09:57:27 -0700353 *
Fyodor Kupolov0ffbfea2016-01-19 10:07:54 -0800354 * <p>Key for user restrictions.
355 * <p>Type: Boolean
Makoto Onuki068c54a2015-10-13 14:34:03 -0700356 * @see DevicePolicyManager#addUserRestriction(ComponentName, String)
357 * @see DevicePolicyManager#clearUserRestriction(ComponentName, String)
Emily Bernierb223f732013-04-11 15:46:36 -0400358 * @see #getUserRestrictions()
359 */
360 public static final String DISALLOW_CONFIG_CREDENTIALS = "no_config_credentials";
361
362 /**
Amith Yamasani150514b2015-01-07 16:05:05 -0800363 * When set on the primary user this specifies if the user can remove other users.
364 * When set on a secondary user, this specifies if the user can remove itself.
365 * This restriction has no effect on managed profiles.
366 * The default value is <code>false</code>.
Amith Yamasani26af8292014-09-09 09:57:27 -0700367 *
Fyodor Kupolov0ffbfea2016-01-19 10:07:54 -0800368 * <p>Key for user restrictions.
369 * <p>Type: Boolean
Makoto Onuki068c54a2015-10-13 14:34:03 -0700370 * @see DevicePolicyManager#addUserRestriction(ComponentName, String)
371 * @see DevicePolicyManager#clearUserRestriction(ComponentName, String)
Emily Bernierb223f732013-04-11 15:46:36 -0400372 * @see #getUserRestrictions()
373 */
374 public static final String DISALLOW_REMOVE_USER = "no_remove_user";
375
Julia Reynoldsd46d0f92014-04-23 15:23:24 -0400376 /**
Esteban Talavera6c9116a2016-11-24 16:12:44 +0000377 * Specifies if managed profiles of this user can be removed, other than by its profile owner.
378 * The default value is <code>false</code>.
379 * <p>
Nicolas Prevot2ea46fe2017-01-05 10:29:34 +0000380 * This restriction has no effect on managed profiles.
Esteban Talavera6c9116a2016-11-24 16:12:44 +0000381 *
382 * <p>Key for user restrictions.
383 * <p>Type: Boolean
384 * @see DevicePolicyManager#addUserRestriction(ComponentName, String)
385 * @see DevicePolicyManager#clearUserRestriction(ComponentName, String)
386 * @see #getUserRestrictions()
387 */
388 public static final String DISALLOW_REMOVE_MANAGED_PROFILE = "no_remove_managed_profile";
389
390 /**
Pavel Grafov9fb706c2018-02-27 14:07:52 +0000391 * Specifies if a user is disallowed from enabling or accessing debugging features. When set on
392 * the primary user, disables debugging features altogether, including USB debugging. When set
393 * on a managed profile or a secondary user, blocks debugging for that user only, including
394 * starting activities, making service calls, accessing content providers, sending broadcasts,
395 * installing/uninstalling packages, clearing user data, etc.
396 * The default value is <code>false</code>.
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)
Julia Reynoldsd46d0f92014-04-23 15:23:24 -0400402 * @see #getUserRestrictions()
403 */
404 public static final String DISALLOW_DEBUGGING_FEATURES = "no_debugging_features";
405
406 /**
Benjamin Miller05fef7e2017-07-24 10:11:39 +0200407 * Specifies if a user is disallowed from configuring a VPN. The default value is
408 * <code>false</code>. This restriction has an effect when set by device owners and, in Android
409 * 6.0 ({@linkplain android.os.Build.VERSION_CODES#M API level 23}) or higher, profile owners.
410 * <p>This restriction also prevents VPNs from starting. However, in Android 7.0
411 * ({@linkplain android.os.Build.VERSION_CODES#N API level 24}) or higher, the system does
412 * start always-on VPNs created by the device or profile owner.
Amith Yamasani26af8292014-09-09 09:57:27 -0700413 *
Fyodor Kupolov0ffbfea2016-01-19 10:07:54 -0800414 * <p>Key for user restrictions.
415 * <p>Type: Boolean
Makoto Onuki068c54a2015-10-13 14:34:03 -0700416 * @see DevicePolicyManager#addUserRestriction(ComponentName, String)
417 * @see DevicePolicyManager#clearUserRestriction(ComponentName, String)
Julia Reynoldsd46d0f92014-04-23 15:23:24 -0400418 * @see #getUserRestrictions()
419 */
420 public static final String DISALLOW_CONFIG_VPN = "no_config_vpn";
421
422 /**
yuemingw7810b8b2018-02-01 17:32:25 +0000423 * Specifies if a user is disallowed from enabling or disabling location providers. As a
424 * result, user is disallowed from turning on or off location. Device owner and profile owners
425 * can set this restriction and it only applies on the managed user.
yuemingw7cc2c4c2017-11-28 17:20:01 +0000426 *
427 * <p>In a managed profile, location sharing is forced off when it's off on primary user, so
428 * user can still turn off location sharing on managed profile when the restriction is set by
429 * profile owner on managed profile.
430 *
431 * <p>This user restriction is different from {@link #DISALLOW_SHARE_LOCATION},
432 * as the device owner or profile owner can still enable or disable location mode via
433 * {@link DevicePolicyManager#setSecureSetting} when this restriction is on.
434 *
435 * <p>The default value is <code>false</code>.
436 *
437 * <p>Key for user restrictions.
438 * <p>Type: Boolean
yuemingw7810b8b2018-02-01 17:32:25 +0000439 * @see android.location.LocationManager#isProviderEnabled(String)
yuemingw7cc2c4c2017-11-28 17:20:01 +0000440 * @see DevicePolicyManager#addUserRestriction(ComponentName, String)
441 * @see DevicePolicyManager#clearUserRestriction(ComponentName, String)
442 * @see #getUserRestrictions()
443 */
yuemingw7810b8b2018-02-01 17:32:25 +0000444 public static final String DISALLOW_CONFIG_LOCATION = "no_config_location";
yuemingw7cc2c4c2017-11-28 17:20:01 +0000445
446 /**
yuemingwa9772f362017-10-23 18:34:35 +0100447 * Specifies if date, time and timezone configuring is disallowed.
448 *
449 * <p>When restriction is set by device owners, it applies globally - i.e., it disables date,
450 * time and timezone setting on the entire device and all users will be affected. When it's set
451 * by profile owners, it's only applied to the managed user.
452 * <p>The default value is <code>false</code>.
453 *
454 * <p>This user restriction has no effect on managed profiles.
455 * <p>Key for user restrictions.
456 * <p>Type: Boolean
457 * @see DevicePolicyManager#addUserRestriction(ComponentName, String)
458 * @see DevicePolicyManager#clearUserRestriction(ComponentName, String)
459 * @see #getUserRestrictions()
460 */
461 public static final String DISALLOW_CONFIG_DATE_TIME = "no_config_date_time";
462
463 /**
Amith Yamasani26af8292014-09-09 09:57:27 -0700464 * Specifies if a user is disallowed from configuring Tethering
Amith Yamasanic34dc7c2014-09-18 09:42:42 -0700465 * & portable hotspots. This can only be set by device owners and profile owners on the
466 * primary user. The default value is <code>false</code>.
Rubin Xu1faf1442017-08-23 15:48:12 +0100467 * <p>In Android 9.0 or higher, if tethering is enabled when this restriction is set,
468 * tethering will be automatically turned off.
Amith Yamasani26af8292014-09-09 09:57:27 -0700469 *
Fyodor Kupolov0ffbfea2016-01-19 10:07:54 -0800470 * <p>Key for user restrictions.
471 * <p>Type: Boolean
Makoto Onuki068c54a2015-10-13 14:34:03 -0700472 * @see DevicePolicyManager#addUserRestriction(ComponentName, String)
473 * @see DevicePolicyManager#clearUserRestriction(ComponentName, String)
Julia Reynoldsd46d0f92014-04-23 15:23:24 -0400474 * @see #getUserRestrictions()
475 */
476 public static final String DISALLOW_CONFIG_TETHERING = "no_config_tethering";
477
478 /**
Stuart Scotte3e314d2015-04-20 14:07:45 -0700479 * Specifies if a user is disallowed from resetting network settings
480 * from Settings. This can only be set by device owners and profile owners on the primary user.
481 * The default value is <code>false</code>.
Fyodor Kupolov0ffbfea2016-01-19 10:07:54 -0800482 * <p>This restriction has no effect on secondary users and managed profiles since only the
Stuart Scotte3e314d2015-04-20 14:07:45 -0700483 * primary user can reset the network settings of the device.
484 *
Fyodor Kupolov0ffbfea2016-01-19 10:07:54 -0800485 * <p>Key for user restrictions.
486 * <p>Type: Boolean
Makoto Onuki068c54a2015-10-13 14:34:03 -0700487 * @see DevicePolicyManager#addUserRestriction(ComponentName, String)
488 * @see DevicePolicyManager#clearUserRestriction(ComponentName, String)
Stuart Scotte3e314d2015-04-20 14:07:45 -0700489 * @see #getUserRestrictions()
490 */
491 public static final String DISALLOW_NETWORK_RESET = "no_network_reset";
492
493 /**
Amith Yamasani26af8292014-09-09 09:57:27 -0700494 * Specifies if a user is disallowed from factory resetting
Amith Yamasanic34dc7c2014-09-18 09:42:42 -0700495 * from Settings. This can only be set by device owners and profile owners on the primary user.
496 * The default value is <code>false</code>.
Fyodor Kupolov0ffbfea2016-01-19 10:07:54 -0800497 * <p>This restriction has no effect on secondary users and managed profiles since only the
Amith Yamasani150514b2015-01-07 16:05:05 -0800498 * primary user can factory reset the device.
Amith Yamasani26af8292014-09-09 09:57:27 -0700499 *
Fyodor Kupolov0ffbfea2016-01-19 10:07:54 -0800500 * <p>Key for user restrictions.
501 * <p>Type: Boolean
Makoto Onuki068c54a2015-10-13 14:34:03 -0700502 * @see DevicePolicyManager#addUserRestriction(ComponentName, String)
503 * @see DevicePolicyManager#clearUserRestriction(ComponentName, String)
Julia Reynoldsd46d0f92014-04-23 15:23:24 -0400504 * @see #getUserRestrictions()
505 */
506 public static final String DISALLOW_FACTORY_RESET = "no_factory_reset";
507
508 /**
Esteban Talavera6c9116a2016-11-24 16:12:44 +0000509 * Specifies if a user is disallowed from adding new users. This can only be set by device
510 * owners and profile owners on the primary user.
Amith Yamasanic34dc7c2014-09-18 09:42:42 -0700511 * The default value is <code>false</code>.
Fyodor Kupolov0ffbfea2016-01-19 10:07:54 -0800512 * <p>This restriction has no effect on secondary users and managed profiles since only the
Amith Yamasani150514b2015-01-07 16:05:05 -0800513 * primary user can add other users.
Amith Yamasani26af8292014-09-09 09:57:27 -0700514 *
Fyodor Kupolov0ffbfea2016-01-19 10:07:54 -0800515 * <p>Key for user restrictions.
516 * <p>Type: Boolean
Makoto Onuki068c54a2015-10-13 14:34:03 -0700517 * @see DevicePolicyManager#addUserRestriction(ComponentName, String)
518 * @see DevicePolicyManager#clearUserRestriction(ComponentName, String)
Julia Reynoldsd46d0f92014-04-23 15:23:24 -0400519 * @see #getUserRestrictions()
520 */
521 public static final String DISALLOW_ADD_USER = "no_add_user";
522
523 /**
Esteban Talavera6c9116a2016-11-24 16:12:44 +0000524 * Specifies if a user is disallowed from adding managed profiles.
525 * <p>The default value for an unmanaged user is <code>false</code>.
Nicolas Prevot2ea46fe2017-01-05 10:29:34 +0000526 * For users with a device owner set, the default is <code>true</code>.
527 * <p>This restriction has no effect on managed profiles.
Esteban Talavera6c9116a2016-11-24 16:12:44 +0000528 *
529 * <p>Key for user restrictions.
530 * <p>Type: Boolean
531 * @see DevicePolicyManager#addUserRestriction(ComponentName, String)
532 * @see DevicePolicyManager#clearUserRestriction(ComponentName, String)
533 * @see #getUserRestrictions()
534 */
535 public static final String DISALLOW_ADD_MANAGED_PROFILE = "no_add_managed_profile";
536
537 /**
Benjamin Millerd41a9fc2017-07-17 17:24:44 +0200538 * Specifies if a user is disallowed from disabling application verification. The default
539 * value is <code>false</code>.
540 *
541 * <p>In Android 8.0 ({@linkplain android.os.Build.VERSION_CODES#O API level 26}) and higher,
542 * this is a global user restriction. If a device owner or profile owner sets this restriction,
543 * the system enforces app verification across all users on the device. Running in earlier
544 * Android versions, this restriction affects only the profile that sets it.
Amith Yamasani26af8292014-09-09 09:57:27 -0700545 *
Fyodor Kupolov0ffbfea2016-01-19 10:07:54 -0800546 * <p>Key for user restrictions.
547 * <p>Type: Boolean
Makoto Onuki068c54a2015-10-13 14:34:03 -0700548 * @see DevicePolicyManager#addUserRestriction(ComponentName, String)
549 * @see DevicePolicyManager#clearUserRestriction(ComponentName, String)
Julia Reynoldsd46d0f92014-04-23 15:23:24 -0400550 * @see #getUserRestrictions()
551 */
552 public static final String ENSURE_VERIFY_APPS = "ensure_verify_apps";
553
554 /**
Amith Yamasani26af8292014-09-09 09:57:27 -0700555 * Specifies if a user is disallowed from configuring cell
Amith Yamasanic34dc7c2014-09-18 09:42:42 -0700556 * broadcasts. This can only be set by device owners and profile owners on the primary user.
557 * The default value is <code>false</code>.
Fyodor Kupolov0ffbfea2016-01-19 10:07:54 -0800558 * <p>This restriction has no effect on secondary users and managed profiles since only the
Amith Yamasani150514b2015-01-07 16:05:05 -0800559 * primary user can configure cell broadcasts.
Amith Yamasani26af8292014-09-09 09:57:27 -0700560 *
Fyodor Kupolov0ffbfea2016-01-19 10:07:54 -0800561 * <p>Key for user restrictions.
562 * <p>Type: Boolean
Makoto Onuki068c54a2015-10-13 14:34:03 -0700563 * @see DevicePolicyManager#addUserRestriction(ComponentName, String)
564 * @see DevicePolicyManager#clearUserRestriction(ComponentName, String)
Julia Reynoldsd46d0f92014-04-23 15:23:24 -0400565 * @see #getUserRestrictions()
566 */
567 public static final String DISALLOW_CONFIG_CELL_BROADCASTS = "no_config_cell_broadcasts";
568
569 /**
Amith Yamasani26af8292014-09-09 09:57:27 -0700570 * Specifies if a user is disallowed from configuring mobile
Amith Yamasanic34dc7c2014-09-18 09:42:42 -0700571 * networks. This can only be set by device owners and profile owners on the primary user.
572 * The default value is <code>false</code>.
Fyodor Kupolov0ffbfea2016-01-19 10:07:54 -0800573 * <p>This restriction has no effect on secondary users and managed profiles since only the
Amith Yamasani150514b2015-01-07 16:05:05 -0800574 * primary user can configure mobile networks.
Amith Yamasani26af8292014-09-09 09:57:27 -0700575 *
Fyodor Kupolov0ffbfea2016-01-19 10:07:54 -0800576 * <p>Key for user restrictions.
577 * <p>Type: Boolean
Makoto Onuki068c54a2015-10-13 14:34:03 -0700578 * @see DevicePolicyManager#addUserRestriction(ComponentName, String)
579 * @see DevicePolicyManager#clearUserRestriction(ComponentName, String)
Julia Reynoldsd46d0f92014-04-23 15:23:24 -0400580 * @see #getUserRestrictions()
581 */
582 public static final String DISALLOW_CONFIG_MOBILE_NETWORKS = "no_config_mobile_networks";
583
584 /**
Amith Yamasani26af8292014-09-09 09:57:27 -0700585 * Specifies if a user is disallowed from modifying
Julia Reynoldsc617f812014-07-25 16:32:27 -0400586 * applications in Settings or launchers. The following actions will not be allowed when this
587 * restriction is enabled:
588 * <li>uninstalling apps</li>
589 * <li>disabling apps</li>
590 * <li>clearing app caches</li>
591 * <li>clearing app data</li>
592 * <li>force stopping apps</li>
593 * <li>clearing app defaults</li>
594 * <p>
595 * The default value is <code>false</code>.
Amith Yamasani26af8292014-09-09 09:57:27 -0700596 *
Esteban Talavera8bd7c522017-02-13 12:35:04 +0000597 * <p><strong>Note:</strong> The user will still be able to perform those actions via other
598 * means (such as adb). Third party apps will also be able to uninstall apps via the
599 * {@link android.content.pm.PackageInstaller}. {@link #DISALLOW_UNINSTALL_APPS} or
600 * {@link DevicePolicyManager#setUninstallBlocked(ComponentName, String, boolean)} should be
601 * used to prevent the user from uninstalling apps completely, and
602 * {@link DevicePolicyManager#addPersistentPreferredActivity(ComponentName, IntentFilter, ComponentName)}
603 * to add a default intent handler for a given intent filter.
604 *
Fyodor Kupolov0ffbfea2016-01-19 10:07:54 -0800605 * <p>Key for user restrictions.
606 * <p>Type: Boolean
Makoto Onuki068c54a2015-10-13 14:34:03 -0700607 * @see DevicePolicyManager#addUserRestriction(ComponentName, String)
608 * @see DevicePolicyManager#clearUserRestriction(ComponentName, String)
Julia Reynoldsd46d0f92014-04-23 15:23:24 -0400609 * @see #getUserRestrictions()
610 */
Julia Reynolds36fbc8d2014-06-18 09:26:30 -0400611 public static final String DISALLOW_APPS_CONTROL = "no_control_apps";
Julia Reynoldsd46d0f92014-04-23 15:23:24 -0400612
Emily Bernier394a6cd2014-05-07 12:49:20 -0400613 /**
Amith Yamasani26af8292014-09-09 09:57:27 -0700614 * Specifies if a user is disallowed from mounting
Amith Yamasanic34dc7c2014-09-18 09:42:42 -0700615 * physical external media. This can only be set by device owners and profile owners on the
616 * primary user. The default value is <code>false</code>.
Amith Yamasani26af8292014-09-09 09:57:27 -0700617 *
Fyodor Kupolov0ffbfea2016-01-19 10:07:54 -0800618 * <p>Key for user restrictions.
619 * <p>Type: Boolean
Makoto Onuki068c54a2015-10-13 14:34:03 -0700620 * @see DevicePolicyManager#addUserRestriction(ComponentName, String)
621 * @see DevicePolicyManager#clearUserRestriction(ComponentName, String)
Emily Bernier394a6cd2014-05-07 12:49:20 -0400622 * @see #getUserRestrictions()
623 */
624 public static final String DISALLOW_MOUNT_PHYSICAL_MEDIA = "no_physical_media";
625
626 /**
Pavel Grafovce3e1a32017-04-21 14:48:21 +0100627 * Specifies if a user is disallowed from adjusting microphone volume. If set, the microphone
628 * will be muted. This can be set by device owners and profile owners. The default value is
629 * <code>false</code>.
Amith Yamasani26af8292014-09-09 09:57:27 -0700630 *
Pavel Grafovce3e1a32017-04-21 14:48:21 +0100631 * <p>This restriction has no effect on managed profiles.
Fyodor Kupolov0ffbfea2016-01-19 10:07:54 -0800632 * <p>Key for user restrictions.
633 * <p>Type: Boolean
Makoto Onuki068c54a2015-10-13 14:34:03 -0700634 * @see DevicePolicyManager#addUserRestriction(ComponentName, String)
635 * @see DevicePolicyManager#clearUserRestriction(ComponentName, String)
Emily Bernier394a6cd2014-05-07 12:49:20 -0400636 * @see #getUserRestrictions()
637 */
638 public static final String DISALLOW_UNMUTE_MICROPHONE = "no_unmute_microphone";
639
640 /**
Pavel Grafovce3e1a32017-04-21 14:48:21 +0100641 * Specifies if a user is disallowed from adjusting the master volume. If set, the master volume
Wen ZHANG61ed0dc2017-08-23 14:27:02 +0100642 * will be muted. This can be set by device owners from API 21 and profile owners from API 24.
643 * The default value is <code>false</code>.
644 *
645 * <p>When the restriction is set by profile owners, then it only applies to relevant
646 * profiles.
Amith Yamasani26af8292014-09-09 09:57:27 -0700647 *
Pavel Grafovce3e1a32017-04-21 14:48:21 +0100648 * <p>This restriction has no effect on managed profiles.
Fyodor Kupolov0ffbfea2016-01-19 10:07:54 -0800649 * <p>Key for user restrictions.
650 * <p>Type: Boolean
Makoto Onuki068c54a2015-10-13 14:34:03 -0700651 * @see DevicePolicyManager#addUserRestriction(ComponentName, String)
652 * @see DevicePolicyManager#clearUserRestriction(ComponentName, String)
Emily Bernier394a6cd2014-05-07 12:49:20 -0400653 * @see #getUserRestrictions()
654 */
655 public static final String DISALLOW_ADJUST_VOLUME = "no_adjust_volume";
656
Amith Yamasani9f6c25f2014-05-16 14:49:15 -0700657 /**
Amith Yamasani26af8292014-09-09 09:57:27 -0700658 * Specifies that the user is not allowed to make outgoing
Amith Yamasani390989d2014-07-17 10:52:03 -0700659 * phone calls. Emergency calls are still permitted.
Amith Yamasani9f6c25f2014-05-16 14:49:15 -0700660 * The default value is <code>false</code>.
Tony Makeb83ab52016-02-22 18:36:08 +0000661 * <p>This restriction has no effect on managed profiles.
Amith Yamasani26af8292014-09-09 09:57:27 -0700662 *
Fyodor Kupolov0ffbfea2016-01-19 10:07:54 -0800663 * <p>Key for user restrictions.
664 * <p>Type: Boolean
Makoto Onuki068c54a2015-10-13 14:34:03 -0700665 * @see DevicePolicyManager#addUserRestriction(ComponentName, String)
666 * @see DevicePolicyManager#clearUserRestriction(ComponentName, String)
Amith Yamasani9f6c25f2014-05-16 14:49:15 -0700667 * @see #getUserRestrictions()
668 */
Amith Yamasani390989d2014-07-17 10:52:03 -0700669 public static final String DISALLOW_OUTGOING_CALLS = "no_outgoing_calls";
670
671 /**
Amith Yamasani26af8292014-09-09 09:57:27 -0700672 * Specifies that the user is not allowed to send or receive
Amith Yamasanic34dc7c2014-09-18 09:42:42 -0700673 * SMS messages. The default value is <code>false</code>.
Amith Yamasani26af8292014-09-09 09:57:27 -0700674 *
Fyodor Kupolov0ffbfea2016-01-19 10:07:54 -0800675 * <p>Key for user restrictions.
676 * <p>Type: Boolean
Makoto Onuki068c54a2015-10-13 14:34:03 -0700677 * @see DevicePolicyManager#addUserRestriction(ComponentName, String)
678 * @see DevicePolicyManager#clearUserRestriction(ComponentName, String)
Amith Yamasani390989d2014-07-17 10:52:03 -0700679 * @see #getUserRestrictions()
680 */
681 public static final String DISALLOW_SMS = "no_sms";
Amith Yamasani9f6c25f2014-05-16 14:49:15 -0700682
Jason Monk1c7c3192014-06-26 12:52:18 -0400683 /**
Jeff Sharkey2cc03e52015-03-20 11:24:04 -0700684 * Specifies if the user is not allowed to have fun. In some cases, the
685 * device owner may wish to prevent the user from experiencing amusement or
686 * joy while using the device. The default value is <code>false</code>.
687 *
Fyodor Kupolov0ffbfea2016-01-19 10:07:54 -0800688 * <p>Key for user restrictions.
689 * <p>Type: Boolean
Makoto Onuki068c54a2015-10-13 14:34:03 -0700690 * @see DevicePolicyManager#addUserRestriction(ComponentName, String)
691 * @see DevicePolicyManager#clearUserRestriction(ComponentName, String)
Jeff Sharkey2cc03e52015-03-20 11:24:04 -0700692 * @see #getUserRestrictions()
693 */
694 public static final String DISALLOW_FUN = "no_fun";
695
696 /**
Amith Yamasani26af8292014-09-09 09:57:27 -0700697 * Specifies that windows besides app windows should not be
Jason Monk1c7c3192014-06-26 12:52:18 -0400698 * created. This will block the creation of the following types of windows.
699 * <li>{@link LayoutParams#TYPE_TOAST}</li>
700 * <li>{@link LayoutParams#TYPE_PHONE}</li>
701 * <li>{@link LayoutParams#TYPE_PRIORITY_PHONE}</li>
702 * <li>{@link LayoutParams#TYPE_SYSTEM_ALERT}</li>
703 * <li>{@link LayoutParams#TYPE_SYSTEM_ERROR}</li>
704 * <li>{@link LayoutParams#TYPE_SYSTEM_OVERLAY}</li>
Wale Ogunwale5cd907d2017-01-26 14:14:08 -0800705 * <li>{@link LayoutParams#TYPE_APPLICATION_OVERLAY}</li>
Jason Monk1c7c3192014-06-26 12:52:18 -0400706 *
Amith Yamasanic34dc7c2014-09-18 09:42:42 -0700707 * <p>This can only be set by device owners and profile owners on the primary user.
708 * The default value is <code>false</code>.
Amith Yamasani26af8292014-09-09 09:57:27 -0700709 *
Fyodor Kupolov0ffbfea2016-01-19 10:07:54 -0800710 * <p>Key for user restrictions.
711 * <p>Type: Boolean
Makoto Onuki068c54a2015-10-13 14:34:03 -0700712 * @see DevicePolicyManager#addUserRestriction(ComponentName, String)
713 * @see DevicePolicyManager#clearUserRestriction(ComponentName, String)
Jason Monk1c7c3192014-06-26 12:52:18 -0400714 * @see #getUserRestrictions()
715 */
716 public static final String DISALLOW_CREATE_WINDOWS = "no_create_windows";
717
Nicolas Prevotf1939902014-06-25 09:29:02 +0100718 /**
Charles He22ff6f9d2017-10-05 21:28:55 +0100719 * Specifies that system error dialogs for crashed or unresponsive apps should not be shown.
720 * 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 +0000721 * option on the UI. A feedback report isn't collected as there is no way for the user to
722 * provide explicit consent. The default value is <code>false</code>.
Charles He22ff6f9d2017-10-05 21:28:55 +0100723 *
Benjamin Franz0c86fe12018-02-28 09:49:17 +0000724 * <p>When this user restriction is set by device owners, it's applied to all users. When set by
725 * the profile owner of the primary user or a secondary user, the restriction affects only the
726 * calling user. This user restriction has no effect on managed profiles.
Charles He22ff6f9d2017-10-05 21:28:55 +0100727 *
Charles He22ff6f9d2017-10-05 21:28:55 +0100728 * <p>Key for user restrictions.
729 * <p>Type: Boolean
730 * @see DevicePolicyManager#addUserRestriction(ComponentName, String)
731 * @see DevicePolicyManager#clearUserRestriction(ComponentName, String)
732 * @see #getUserRestrictions()
733 */
734 public static final String DISALLOW_SYSTEM_ERROR_DIALOGS = "no_system_error_dialogs";
735
736 /**
Benjamin Miller8cc730d2019-01-18 19:03:40 +0000737 * Specifies if the clipboard contents can be exported by pasting the data into other users or
738 * profiles. This restriction doesn't prevent import, such as someone pasting clipboard data
739 * from other profiles or users. The default value is {@code false}.
740 *
741 * <p><strong>Note</strong>: Because it's possible to extract data from screenshots using
742 * optical character recognition (OCR), we strongly recommend combining this user restriction
743 * with {@link DevicePolicyManager#setScreenCaptureDisabled(ComponentName, boolean)}.
Amith Yamasani26af8292014-09-09 09:57:27 -0700744 *
Fyodor Kupolov0ffbfea2016-01-19 10:07:54 -0800745 * <p>Key for user restrictions.
746 * <p>Type: Boolean
Makoto Onuki068c54a2015-10-13 14:34:03 -0700747 * @see DevicePolicyManager#addUserRestriction(ComponentName, String)
748 * @see DevicePolicyManager#clearUserRestriction(ComponentName, String)
Nicolas Prevotf1939902014-06-25 09:29:02 +0100749 * @see #getUserRestrictions()
750 */
751 public static final String DISALLOW_CROSS_PROFILE_COPY_PASTE = "no_cross_profile_copy_paste";
752
Amith Yamasani26af8292014-09-09 09:57:27 -0700753 /**
754 * Specifies if the user is not allowed to use NFC to beam out data from apps.
755 * The default value is <code>false</code>.
756 *
Fyodor Kupolov0ffbfea2016-01-19 10:07:54 -0800757 * <p>Key for user restrictions.
758 * <p>Type: Boolean
Makoto Onuki068c54a2015-10-13 14:34:03 -0700759 * @see DevicePolicyManager#addUserRestriction(ComponentName, String)
760 * @see DevicePolicyManager#clearUserRestriction(ComponentName, String)
Amith Yamasani26af8292014-09-09 09:57:27 -0700761 * @see #getUserRestrictions()
762 */
763 public static final String DISALLOW_OUTGOING_BEAM = "no_outgoing_beam";
764
Sander Alewijnse53d63dc2014-11-07 21:43:00 +0000765 /**
Oleksandr Peletskyif2519812016-01-26 20:16:06 +0100766 * Hidden user restriction to disallow access to wallpaper manager APIs. This restriction
767 * generally means that wallpapers are not supported for the particular user. This user
768 * restriction is always set for managed profiles, because such profiles don't have wallpapers.
Benjamin Franzf3ece362015-02-11 10:51:10 +0000769 * @hide
Oleksandr Peletskyif2519812016-01-26 20:16:06 +0100770 * @see #DISALLOW_SET_WALLPAPER
Makoto Onuki068c54a2015-10-13 14:34:03 -0700771 * @see DevicePolicyManager#addUserRestriction(ComponentName, String)
772 * @see DevicePolicyManager#clearUserRestriction(ComponentName, String)
Benjamin Franzf3ece362015-02-11 10:51:10 +0000773 * @see #getUserRestrictions()
774 */
775 public static final String DISALLOW_WALLPAPER = "no_wallpaper";
776
777 /**
Oleksandr Peletskyif2519812016-01-26 20:16:06 +0100778 * User restriction to disallow setting a wallpaper. Profile owner and device owner
779 * are able to set wallpaper regardless of this restriction.
780 * The default value is <code>false</code>.
781 *
782 * <p>Key for user restrictions.
783 * <p>Type: Boolean
784 * @see DevicePolicyManager#addUserRestriction(ComponentName, String)
785 * @see DevicePolicyManager#clearUserRestriction(ComponentName, String)
786 * @see #getUserRestrictions()
787 */
788 public static final String DISALLOW_SET_WALLPAPER = "no_set_wallpaper";
789
790 /**
Benjamin Franzbff46ba2015-03-05 18:33:51 +0000791 * Specifies if the user is not allowed to reboot the device into safe boot mode.
792 * This can only be set by device owners and profile owners on the primary user.
793 * The default value is <code>false</code>.
794 *
Fyodor Kupolov0ffbfea2016-01-19 10:07:54 -0800795 * <p>Key for user restrictions.
796 * <p>Type: Boolean
Makoto Onuki068c54a2015-10-13 14:34:03 -0700797 * @see DevicePolicyManager#addUserRestriction(ComponentName, String)
798 * @see DevicePolicyManager#clearUserRestriction(ComponentName, String)
Benjamin Franzbff46ba2015-03-05 18:33:51 +0000799 * @see #getUserRestrictions()
800 */
801 public static final String DISALLOW_SAFE_BOOT = "no_safe_boot";
802
803 /**
Fyodor Kupolovb5013302015-04-17 17:59:14 -0700804 * Specifies if a user is not allowed to record audio. This restriction is always enabled for
805 * background users. The default value is <code>false</code>.
806 *
Makoto Onuki068c54a2015-10-13 14:34:03 -0700807 * @see DevicePolicyManager#addUserRestriction(ComponentName, String)
808 * @see DevicePolicyManager#clearUserRestriction(ComponentName, String)
Fyodor Kupolovb5013302015-04-17 17:59:14 -0700809 * @see #getUserRestrictions()
810 * @hide
811 */
812 public static final String DISALLOW_RECORD_AUDIO = "no_record_audio";
813
814 /**
Fyodor Kupolov9cbfc9e2015-10-07 15:52:33 -0700815 * Specifies if a user is not allowed to run in the background and should be stopped during
816 * user switch. The default value is <code>false</code>.
817 *
818 * <p>This restriction can be set by device owners and profile owners.
819 *
820 * @see DevicePolicyManager#addUserRestriction(ComponentName, String)
821 * @see DevicePolicyManager#clearUserRestriction(ComponentName, String)
822 * @see #getUserRestrictions()
823 * @hide
824 */
Robin Lee6cfb7a92017-12-18 15:14:46 +0100825 @SystemApi
Fyodor Kupolov9cbfc9e2015-10-07 15:52:33 -0700826 public static final String DISALLOW_RUN_IN_BACKGROUND = "no_run_in_background";
827
828 /**
Makoto Onuki759a7632015-10-28 16:43:10 -0700829 * Specifies if a user is not allowed to use the camera.
830 *
831 * @see DevicePolicyManager#addUserRestriction(ComponentName, String)
832 * @see DevicePolicyManager#clearUserRestriction(ComponentName, String)
833 * @see #getUserRestrictions()
834 * @hide
835 */
836 public static final String DISALLOW_CAMERA = "no_camera";
837
838 /**
Tony Makc1205112016-07-22 16:02:59 +0100839 * Specifies if a user is not allowed to unmute the device's master volume.
840 *
841 * @see DevicePolicyManager#setMasterVolumeMuted(ComponentName, boolean)
842 * @see DevicePolicyManager#clearUserRestriction(ComponentName, String)
843 * @see #getUserRestrictions()
844 * @hide
845 */
Esteban Talavera492b4722017-02-13 14:59:45 +0000846 public static final String DISALLOW_UNMUTE_DEVICE = "disallow_unmute_device";
Tony Makc1205112016-07-22 16:02:59 +0100847
848 /**
Mahaver Chopradea471e2015-12-17 11:02:37 +0000849 * Specifies if a user is not allowed to use cellular data when roaming. This can only be set by
850 * device owners. The default value is <code>false</code>.
851 *
852 * @see DevicePolicyManager#addUserRestriction(ComponentName, String)
853 * @see DevicePolicyManager#clearUserRestriction(ComponentName, String)
854 * @see #getUserRestrictions()
855 */
856 public static final String DISALLOW_DATA_ROAMING = "no_data_roaming";
857
858 /**
Oleksandr Peletskyi7f1f1df2016-01-18 15:40:21 +0100859 * Specifies if a user is not allowed to change their icon. Device owner and profile owner
860 * can set this restriction. When it is set by device owner, only the target user will be
861 * affected. The default value is <code>false</code>.
862 *
863 * <p>Key for user restrictions.
Oleksandr Peletskyi7f1f1df2016-01-18 15:40:21 +0100864 * <p>Type: Boolean
Oleksandr Peletskyi7f1f1df2016-01-18 15:40:21 +0100865 * @see DevicePolicyManager#addUserRestriction(ComponentName, String)
866 * @see DevicePolicyManager#clearUserRestriction(ComponentName, String)
867 * @see #getUserRestrictions()
868 */
869 public static final String DISALLOW_SET_USER_ICON = "no_set_user_icon";
870
871 /**
Mahaver Chopra3d9805d2016-07-07 16:25:05 +0100872 * Specifies if a user is not allowed to enable the oem unlock setting. The default value is
Esteban Talaverac48b20f2016-08-11 11:23:40 +0100873 * <code>false</code>. Setting this restriction has no effect if the bootloader is already
874 * unlocked.
Mahaver Chopra3d9805d2016-07-07 16:25:05 +0100875 *
Lenka Trochtova12b04962016-11-29 21:00:12 +0100876 * <p>Not for use by third-party applications.
877 *
Mahaver Chopra3d9805d2016-07-07 16:25:05 +0100878 * @see DevicePolicyManager#addUserRestriction(ComponentName, String)
879 * @see DevicePolicyManager#clearUserRestriction(ComponentName, String)
880 * @see #getUserRestrictions()
Andrew Scull3b8b46f2017-02-13 18:12:15 +0000881 * @deprecated use {@link OemLockManager#setOemUnlockAllowedByCarrier(boolean, byte[])} instead.
Mahaver Chopra3d9805d2016-07-07 16:25:05 +0100882 * @hide
883 */
Andrew Scull3b8b46f2017-02-13 18:12:15 +0000884 @Deprecated
Lenka Trochtova12b04962016-11-29 21:00:12 +0100885 @SystemApi
Mahaver Chopra3d9805d2016-07-07 16:25:05 +0100886 public static final String DISALLOW_OEM_UNLOCK = "no_oem_unlock";
887
888 /**
Pavel Grafovc4f87e92017-10-26 16:34:25 +0100889 * Specifies that the managed profile is not allowed to have unified lock screen challenge with
890 * the primary user.
891 *
892 * <p><strong>Note:</strong> Setting this restriction alone doesn't automatically set a
893 * separate challenge. Profile owner can ask the user to set a new password using
894 * {@link DevicePolicyManager#ACTION_SET_NEW_PASSWORD} and verify it using
895 * {@link DevicePolicyManager#isUsingUnifiedPassword(ComponentName)}.
896 *
897 * <p>Can be set by profile owners. It only has effect on managed profiles when set by managed
898 * profile owner. Has no effect on non-managed profiles or users.
899 * <p>Key for user restrictions.
900 * <p>Type: Boolean
901 * @see DevicePolicyManager#addUserRestriction(ComponentName, String)
902 * @see DevicePolicyManager#clearUserRestriction(ComponentName, String)
903 * @see #getUserRestrictions()
904 */
905 public static final String DISALLOW_UNIFIED_PASSWORD = "no_unified_password";
906
907 /**
Nicolas Prevotf0029c12015-06-25 14:25:41 -0700908 * Allows apps in the parent profile to handle web links from the managed profile.
909 *
Nicolas Prevot9edbda12015-06-17 11:09:48 -0700910 * This user restriction has an effect only in a managed profile.
911 * If set:
912 * Intent filters of activities in the parent profile with action
913 * {@link android.content.Intent#ACTION_VIEW},
914 * category {@link android.content.Intent#CATEGORY_BROWSABLE}, scheme http or https, and which
915 * define a host can handle intents from the managed profile.
916 * The default value is <code>false</code>.
917 *
Fyodor Kupolov0ffbfea2016-01-19 10:07:54 -0800918 * <p>Key for user restrictions.
919 * <p>Type: Boolean
Makoto Onuki068c54a2015-10-13 14:34:03 -0700920 * @see DevicePolicyManager#addUserRestriction(ComponentName, String)
921 * @see DevicePolicyManager#clearUserRestriction(ComponentName, String)
Nicolas Prevot9edbda12015-06-17 11:09:48 -0700922 * @see #getUserRestrictions()
923 */
Nicolas Prevotf0029c12015-06-25 14:25:41 -0700924 public static final String ALLOW_PARENT_PROFILE_APP_LINKING
925 = "allow_parent_profile_app_linking";
Nicolas Prevot9edbda12015-06-17 11:09:48 -0700926
927 /**
Felipe Leme24d58932017-03-21 14:13:58 -0700928 * Specifies if a user is not allowed to use Autofill Services.
929 *
930 * <p>Device owner and profile owner can set this restriction. When it is set by device owner,
931 * only the target user will be affected.
932 *
933 * <p>The default value is <code>false</code>.
934 *
935 * @see DevicePolicyManager#addUserRestriction(ComponentName, String)
936 * @see DevicePolicyManager#clearUserRestriction(ComponentName, String)
937 * @see #getUserRestrictions()
938 */
939 public static final String DISALLOW_AUTOFILL = "no_autofill";
940
941 /**
Felipe Leme1dfa9a02018-10-17 17:24:37 -0700942 * Specifies if the contents of a user's screen is not allowed to be captured for artificial
943 * intelligence purposes.
944 *
945 * <p>Device owner and profile owner can set this restriction. When it is set by device owner,
946 * only the target user will be affected.
947 *
948 * <p>The default value is <code>false</code>.
949 *
950 * @see DevicePolicyManager#addUserRestriction(ComponentName, String)
951 * @see DevicePolicyManager#clearUserRestriction(ComponentName, String)
952 * @see #getUserRestrictions()
953 */
Felipe Leme749b8892018-12-03 16:30:30 -0800954 public static final String DISALLOW_CONTENT_CAPTURE = "no_content_capture";
Felipe Leme1dfa9a02018-10-17 17:24:37 -0700955
956 /**
Benjamin Franzff66fa92017-08-10 10:39:44 +0100957 * Specifies if user switching is blocked on the current user.
958 *
959 * <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 +0800960 * Device owner can still switch user via
961 * {@link DevicePolicyManager#switchUser(ComponentName, UserHandle)} when this restriction is
962 * set.
Benjamin Franzff66fa92017-08-10 10:39:44 +0100963 *
964 * <p>The default value is <code>false</code>.
965 *
966 * @see DevicePolicyManager#addUserRestriction(ComponentName, String)
967 * @see DevicePolicyManager#clearUserRestriction(ComponentName, String)
968 * @see #getUserRestrictions()
969 */
970 public static final String DISALLOW_USER_SWITCH = "no_user_switch";
971
972 /**
Rubin Xucc391c22018-01-02 20:37:35 +0000973 * Specifies whether the user can share file / picture / data from the primary user into the
974 * managed profile, either by sending them from the primary side, or by picking up data within
975 * an app in the managed profile.
976 * <p>
977 * When a managed profile is created, the system allows the user to send data from the primary
978 * side to the profile by setting up certain default cross profile intent filters. If
979 * this is undesired, this restriction can be set to disallow it. Note that this restriction
980 * will not block any sharing allowed by explicit
981 * {@link DevicePolicyManager#addCrossProfileIntentFilter} calls by the profile owner.
982 * <p>
983 * This restriction is only meaningful when set by profile owner. When it is set by device
984 * owner, it does not have any effect.
985 * <p>
986 * The default value is <code>false</code>.
987 *
988 * @see DevicePolicyManager#addUserRestriction(ComponentName, String)
989 * @see DevicePolicyManager#clearUserRestriction(ComponentName, String)
990 * @see #getUserRestrictions()
991 */
992 public static final String DISALLOW_SHARE_INTO_MANAGED_PROFILE = "no_sharing_into_profile";
Vladislav Kuzkokov622b9f92018-01-25 16:33:05 +0100993
994 /**
995 * Specifies whether the user is allowed to print.
996 *
997 * This restriction can be set by device or profile owner.
998 *
999 * The default value is {@code false}.
1000 *
1001 * @see DevicePolicyManager#addUserRestriction(ComponentName, String)
1002 * @see DevicePolicyManager#clearUserRestriction(ComponentName, String)
1003 * @see #getUserRestrictions()
1004 */
1005 public static final String DISALLOW_PRINTING = "no_printing";
1006
Rubin Xucc391c22018-01-02 20:37:35 +00001007 /**
Eran Messeri09b122da2018-10-05 15:33:53 +01001008 * Specifies whether the user is allowed to modify private DNS settings.
1009 *
1010 * <p>The default value is <code>false</code>.
1011 *
1012 * <p>This user restriction can only be applied by the Device Owner.
1013 * <p>Key for user restrictions.
1014 * <p>Type: Boolean
1015 * @see DevicePolicyManager#addUserRestriction(ComponentName, String)
1016 * @see DevicePolicyManager#clearUserRestriction(ComponentName, String)
1017 * @see #getUserRestrictions()
1018 */
1019 public static final String DISALLOW_CONFIG_PRIVATE_DNS =
1020 "disallow_config_private_dns";
1021
1022 /**
Sander Alewijnse53d63dc2014-11-07 21:43:00 +00001023 * Application restriction key that is used to indicate the pending arrival
1024 * of real restrictions for the app.
1025 *
1026 * <p>
1027 * Applications that support restrictions should check for the presence of this key.
1028 * A <code>true</code> value indicates that restrictions may be applied in the near
1029 * future but are not available yet. It is the responsibility of any
1030 * management application that sets this flag to update it when the final
1031 * restrictions are enforced.
1032 *
Fyodor Kupolov0ffbfea2016-01-19 10:07:54 -08001033 * <p>Key for application restrictions.
1034 * <p>Type: Boolean
Nicolas Prevotb14ed952015-01-13 14:23:53 +00001035 * @see android.app.admin.DevicePolicyManager#setApplicationRestrictions(
1036 * android.content.ComponentName, String, Bundle)
1037 * @see android.app.admin.DevicePolicyManager#getApplicationRestrictions(
1038 * android.content.ComponentName, String)
Sander Alewijnse53d63dc2014-11-07 21:43:00 +00001039 */
1040 public static final String KEY_RESTRICTIONS_PENDING = "restrictions_pending";
1041
Amith Yamasani12747872015-12-07 14:19:49 -08001042 private static final String ACTION_CREATE_USER = "android.os.action.CREATE_USER";
1043
1044 /**
1045 * Extra containing a name for the user being created. Optional parameter passed to
1046 * ACTION_CREATE_USER activity.
1047 * @hide
1048 */
1049 public static final String EXTRA_USER_NAME = "android.os.extra.USER_NAME";
1050
1051 /**
1052 * Extra containing account name for the user being created. Optional parameter passed to
1053 * ACTION_CREATE_USER activity.
1054 * @hide
1055 */
1056 public static final String EXTRA_USER_ACCOUNT_NAME = "android.os.extra.USER_ACCOUNT_NAME";
1057
1058 /**
1059 * Extra containing account type for the user being created. Optional parameter passed to
1060 * ACTION_CREATE_USER activity.
1061 * @hide
1062 */
1063 public static final String EXTRA_USER_ACCOUNT_TYPE = "android.os.extra.USER_ACCOUNT_TYPE";
1064
1065 /**
1066 * Extra containing account-specific data for the user being created. Optional parameter passed
1067 * to ACTION_CREATE_USER activity.
1068 * @hide
1069 */
1070 public static final String EXTRA_USER_ACCOUNT_OPTIONS
1071 = "android.os.extra.USER_ACCOUNT_OPTIONS";
1072
Amith Yamasani655d0e22013-06-12 14:19:10 -07001073 /** @hide */
1074 public static final int PIN_VERIFICATION_FAILED_INCORRECT = -3;
1075 /** @hide */
1076 public static final int PIN_VERIFICATION_FAILED_NOT_SET = -2;
1077 /** @hide */
1078 public static final int PIN_VERIFICATION_SUCCESS = -1;
1079
Amith Yamasani37ed8d12016-01-27 14:40:16 -08001080 /**
Makoto Onukie72f81b2017-03-16 14:08:19 -07001081 * Sent when user restrictions have changed.
1082 *
1083 * @hide
1084 */
1085 @SystemApi
1086 @TestApi // To allow seeing it from CTS.
1087 public static final String ACTION_USER_RESTRICTIONS_CHANGED =
1088 "android.os.action.USER_RESTRICTIONS_CHANGED";
1089
1090 /**
Amith Yamasani37ed8d12016-01-27 14:40:16 -08001091 * Error result indicating that this user is not allowed to add other users on this device.
1092 * This is a result code returned from the activity created by the intent
1093 * {@link #createUserCreationIntent(String, String, String, PersistableBundle)}.
1094 */
1095 public static final int USER_CREATION_FAILED_NOT_PERMITTED = Activity.RESULT_FIRST_USER;
1096
1097 /**
1098 * Error result indicating that no more users can be created on this device.
1099 * This is a result code returned from the activity created by the intent
1100 * {@link #createUserCreationIntent(String, String, String, PersistableBundle)}.
1101 */
1102 public static final int USER_CREATION_FAILED_NO_MORE_USERS = Activity.RESULT_FIRST_USER + 1;
1103
Alex Chau89386ba2018-02-13 11:09:17 +08001104 /**
1105 * Indicates user operation is successful.
1106 */
1107 public static final int USER_OPERATION_SUCCESS = 0;
1108
1109 /**
1110 * Indicates user operation failed for unknown reason.
1111 */
1112 public static final int USER_OPERATION_ERROR_UNKNOWN = 1;
1113
1114 /**
1115 * Indicates user operation failed because target user is a managed profile.
1116 */
1117 public static final int USER_OPERATION_ERROR_MANAGED_PROFILE = 2;
1118
1119 /**
1120 * Indicates user operation failed because maximum running user limit has been reached.
1121 */
1122 public static final int USER_OPERATION_ERROR_MAX_RUNNING_USERS = 3;
1123
1124 /**
1125 * Indicates user operation failed because the target user is in the foreground.
1126 */
1127 public static final int USER_OPERATION_ERROR_CURRENT_USER = 4;
1128
1129 /**
1130 * Indicates user operation failed because device has low data storage.
1131 */
1132 public static final int USER_OPERATION_ERROR_LOW_STORAGE = 5;
1133
1134 /**
1135 * Indicates user operation failed because maximum user limit has been reached.
1136 */
1137 public static final int USER_OPERATION_ERROR_MAX_USERS = 6;
1138
1139 /**
1140 * Result returned from various user operations.
1141 *
1142 * @hide
1143 */
1144 @Retention(RetentionPolicy.SOURCE)
1145 @IntDef(prefix = { "USER_OPERATION_" }, value = {
1146 USER_OPERATION_SUCCESS,
1147 USER_OPERATION_ERROR_UNKNOWN,
1148 USER_OPERATION_ERROR_MANAGED_PROFILE,
1149 USER_OPERATION_ERROR_MAX_RUNNING_USERS,
1150 USER_OPERATION_ERROR_CURRENT_USER,
1151 USER_OPERATION_ERROR_LOW_STORAGE,
1152 USER_OPERATION_ERROR_MAX_USERS
1153 })
1154 public @interface UserOperationResult {}
1155
1156 /**
1157 * Thrown to indicate user operation failed.
1158 */
1159 public static class UserOperationException extends RuntimeException {
1160 private final @UserOperationResult int mUserOperationResult;
1161
1162 /**
1163 * Constructs a UserOperationException with specific result code.
1164 *
1165 * @param message the detail message
1166 * @param userOperationResult the result code
1167 * @hide
1168 */
1169 public UserOperationException(String message,
1170 @UserOperationResult int userOperationResult) {
1171 super(message);
1172 mUserOperationResult = userOperationResult;
1173 }
1174
1175 /**
1176 * Returns the operation result code.
1177 */
1178 public @UserOperationResult int getUserOperationResult() {
1179 return mUserOperationResult;
1180 }
1181 }
1182
Amith Yamasani7e99bc02013-04-16 18:24:51 -07001183 /** @hide */
Amith Yamasanic0688302015-10-30 10:40:03 -07001184 public static UserManager get(Context context) {
1185 return (UserManager) context.getSystemService(Context.USER_SERVICE);
Amith Yamasani27db4682013-03-30 17:07:47 -07001186 }
Maggie Benthalla12fccf2013-03-14 18:02:12 -04001187
Amith Yamasani258848d2012-08-10 17:06:33 -07001188 /** @hide */
1189 public UserManager(Context context, IUserManager service) {
1190 mService = service;
Fyodor Kupolov5200e1c2016-10-17 18:46:16 -07001191 mContext = context.getApplicationContext();
Amith Yamasani258848d2012-08-10 17:06:33 -07001192 }
1193
1194 /**
Amith Yamasani06964342016-04-15 13:55:01 -07001195 * Returns whether this device supports multiple users with their own login and customizable
1196 * space.
1197 * @return whether the device supports multiple users.
Amith Yamasani258848d2012-08-10 17:06:33 -07001198 */
Jeff Sharkey4673e7e2012-09-19 13:14:30 -07001199 public static boolean supportsMultipleUsers() {
Kenny Guy1a447532014-02-20 21:55:32 +00001200 return getMaxSupportedUsers() > 1
1201 && SystemProperties.getBoolean("fw.show_multiuserui",
1202 Resources.getSystem().getBoolean(R.bool.config_enableMultiUserUI));
Amith Yamasani258848d2012-08-10 17:06:33 -07001203 }
1204
Maggie Benthall67944582013-02-22 14:58:27 -05001205 /**
Xiaohui Chen7cb69df2015-07-13 16:01:01 -07001206 * @hide
1207 * @return Whether the device is running with split system user. It means the system user and
1208 * primary user are two separate users. Previously system user and primary user are combined as
1209 * a single owner user. see @link {android.os.UserHandle#USER_OWNER}
1210 */
Philip P. Moltmannf80809f2018-04-04 11:20:44 -07001211 @TestApi
Xiaohui Chen7cb69df2015-07-13 16:01:01 -07001212 public static boolean isSplitSystemUser() {
John Reckaa67f682016-09-20 14:24:21 -07001213 return RoSystemProperties.FW_SYSTEM_USER_SPLIT;
Xiaohui Chen7cb69df2015-07-13 16:01:01 -07001214 }
1215
1216 /**
Evan Rosky13a58a92016-07-27 15:51:09 -07001217 * @return Whether guest user is always ephemeral
1218 * @hide
1219 */
1220 public static boolean isGuestUserEphemeral() {
1221 return Resources.getSystem()
1222 .getBoolean(com.android.internal.R.bool.config_guestUserEphemeral);
1223 }
1224
1225 /**
Fyodor Kupolov523c4042016-02-24 15:03:13 -08001226 * Returns whether switching users is currently allowed.
1227 * <p>For instance switching users is not allowed if the current user is in a phone call,
Benjamin Franzff66fa92017-08-10 10:39:44 +01001228 * system user hasn't been unlocked yet, or {@link #DISALLOW_USER_SWITCH} is set.
Fyodor Kupolov523c4042016-02-24 15:03:13 -08001229 * @hide
1230 */
Varun Shahe38c6782018-12-04 16:57:49 -08001231 @SystemApi
1232 @RequiresPermission(anyOf = {android.Manifest.permission.MANAGE_USERS,
1233 android.Manifest.permission.INTERACT_ACROSS_USERS}, conditional = true)
Fyodor Kupolov523c4042016-02-24 15:03:13 -08001234 public boolean canSwitchUsers() {
1235 boolean allowUserSwitchingWhenSystemUserLocked = Settings.Global.getInt(
1236 mContext.getContentResolver(),
1237 Settings.Global.ALLOW_USER_SWITCHING_WHEN_SYSTEM_USER_LOCKED, 0) != 0;
1238 boolean isSystemUserUnlocked = isUserUnlocked(UserHandle.SYSTEM);
1239 boolean inCall = TelephonyManager.getDefault().getCallState()
1240 != TelephonyManager.CALL_STATE_IDLE;
Benjamin Franzff66fa92017-08-10 10:39:44 +01001241 boolean isUserSwitchDisallowed = hasUserRestriction(DISALLOW_USER_SWITCH);
1242 return (allowUserSwitchingWhenSystemUserLocked || isSystemUserUnlocked) && !inCall
1243 && !isUserSwitchDisallowed;
Fyodor Kupolov523c4042016-02-24 15:03:13 -08001244 }
1245
1246 /**
Amith Yamasani5760e172015-04-17 18:42:41 -07001247 * Returns the user handle for the user that this process is running under.
Jessica Hummelbe81c802014-04-22 15:49:22 +01001248 *
Amith Yamasani5760e172015-04-17 18:42:41 -07001249 * @return the user handle of this process.
Amith Yamasani258848d2012-08-10 17:06:33 -07001250 * @hide
Maggie Benthall67944582013-02-22 14:58:27 -05001251 */
Jeff Sharkey8588bc12016-01-06 16:47:42 -07001252 public @UserIdInt int getUserHandle() {
Dianne Hackborn79af1dd2012-08-16 16:42:52 -07001253 return UserHandle.myUserId();
Amith Yamasani258848d2012-08-10 17:06:33 -07001254 }
1255
1256 /**
Dianne Hackborn8832c182012-09-17 17:20:24 -07001257 * Returns the user name of the user making this call. This call is only
1258 * available to applications on the system image; it requires the
1259 * MANAGE_USERS permission.
Amith Yamasani258848d2012-08-10 17:06:33 -07001260 * @return the user name
1261 */
1262 public String getUserName() {
Will Harmond9dcfb12017-05-18 15:41:15 -07001263 UserInfo user = getUserInfo(getUserHandle());
1264 return user == null ? "" : user.name;
Amith Yamasani258848d2012-08-10 17:06:33 -07001265 }
1266
Dianne Hackborn67a101a2014-10-02 12:42:18 -07001267 /**
Fyodor Kupolov605b12a2017-05-10 15:58:09 -07001268 * Returns whether user name has been set.
1269 * <p>This method can be used to check that the value returned by {@link #getUserName()} was
1270 * set by the user and is not a placeholder string provided by the system.
1271 * @hide
1272 */
1273 public boolean isUserNameSet() {
1274 try {
1275 return mService.isUserNameSet(getUserHandle());
1276 } catch (RemoteException re) {
1277 throw re.rethrowFromSystemServer();
1278 }
1279 }
1280
1281 /**
Dan Morrille4ab16a2012-09-20 20:25:55 -07001282 * Used to determine whether the user making this call is subject to
1283 * teleportations.
Dianne Hackborn67a101a2014-10-02 12:42:18 -07001284 *
Dianne Hackborn955d8d62014-10-07 20:17:19 -07001285 * <p>As of {@link android.os.Build.VERSION_CODES#LOLLIPOP}, this method can
Dianne Hackborn67a101a2014-10-02 12:42:18 -07001286 * now automatically identify goats using advanced goat recognition technology.</p>
1287 *
1288 * @return Returns true if the user making this call is a goat.
Dan Morrille4ab16a2012-09-20 20:25:55 -07001289 */
1290 public boolean isUserAGoat() {
Adam Powell988ae302014-09-17 17:58:33 -07001291 return mContext.getPackageManager()
1292 .isPackageAvailable("com.coffeestainstudios.goatsimulator");
Dan Morrille4ab16a2012-09-20 20:25:55 -07001293 }
Amith Yamasanidf2e92a2013-03-01 17:04:38 -08001294
1295 /**
Xiaohui Chen70f6c382015-04-28 14:21:43 -07001296 * Used to check if this process is running under the primary user. The primary user
1297 * is the first human user on a device.
1298 *
1299 * @return whether this process is running under the primary user.
1300 * @hide
1301 */
Varun Shahe142e332018-10-22 15:52:56 -07001302 @SystemApi
1303 @RequiresPermission(android.Manifest.permission.MANAGE_USERS)
Xiaohui Chen70f6c382015-04-28 14:21:43 -07001304 public boolean isPrimaryUser() {
1305 UserInfo user = getUserInfo(UserHandle.myUserId());
Amith Yamasanid35a89c2016-05-26 16:58:43 -07001306 return user != null && user.isPrimary();
Xiaohui Chen70f6c382015-04-28 14:21:43 -07001307 }
1308
1309 /**
Amith Yamasani5760e172015-04-17 18:42:41 -07001310 * Used to check if this process is running under the system user. The system user
1311 * is the initial user that is implicitly created on first boot and hosts most of the
1312 * system services.
1313 *
1314 * @return whether this process is running under the system user.
1315 */
1316 public boolean isSystemUser() {
Xiaohui Chen70f6c382015-04-28 14:21:43 -07001317 return UserHandle.myUserId() == UserHandle.USER_SYSTEM;
Amith Yamasani5760e172015-04-17 18:42:41 -07001318 }
Xiaohui Chen2b45f8a2015-08-04 15:12:23 -07001319
Amith Yamasani5760e172015-04-17 18:42:41 -07001320 /**
Varun Shahe142e332018-10-22 15:52:56 -07001321 * Used to check if this process is running as an admin user. An admin user is allowed to
1322 * modify or configure certain settings that aren't available to non-admin users,
1323 * create and delete additional users, etc. There can be more than one admin users.
1324 *
1325 * @return whether this process is running under an admin user.
Amith Yamasani462ac3a2015-06-30 14:21:01 -07001326 * @hide
Amith Yamasani462ac3a2015-06-30 14:21:01 -07001327 */
Varun Shahe142e332018-10-22 15:52:56 -07001328 @SystemApi
1329 @RequiresPermission(android.Manifest.permission.MANAGE_USERS)
Amith Yamasani462ac3a2015-06-30 14:21:01 -07001330 public boolean isAdminUser() {
Xiaohui Chen2b45f8a2015-08-04 15:12:23 -07001331 return isUserAdmin(UserHandle.myUserId());
1332 }
1333
1334 /**
1335 * @hide
1336 * Returns whether the provided user is an admin user. There can be more than one admin
1337 * user.
1338 */
Jeff Sharkey8588bc12016-01-06 16:47:42 -07001339 public boolean isUserAdmin(@UserIdInt int userId) {
Xiaohui Chen2b45f8a2015-08-04 15:12:23 -07001340 UserInfo user = getUserInfo(userId);
1341 return user != null && user.isAdmin();
Amith Yamasani462ac3a2015-06-30 14:21:01 -07001342 }
1343
1344 /**
Fyodor Kupolovca177562017-11-09 17:43:01 -08001345 * @hide
1346 * @deprecated Use {@link #isRestrictedProfile()}
1347 */
1348 @Deprecated
1349 public boolean isLinkedUser() {
1350 return isRestrictedProfile();
1351 }
1352
1353 /**
Varun Shahe142e332018-10-22 15:52:56 -07001354 * Used to check if this process is running under a restricted profile. Restricted profiles
1355 * may have a reduced number of available apps, app restrictions, and account restrictions.
1356 *
1357 * @return whether this process is running under a restricted profile.
Amith Yamasani2555daf2013-04-25 13:39:27 -07001358 * @hide
Amith Yamasanidf2e92a2013-03-01 17:04:38 -08001359 */
Fyodor Kupolovca177562017-11-09 17:43:01 -08001360 @SystemApi
Varun Shahe142e332018-10-22 15:52:56 -07001361 @RequiresPermission(android.Manifest.permission.MANAGE_USERS)
Fyodor Kupolovca177562017-11-09 17:43:01 -08001362 public boolean isRestrictedProfile() {
Amith Yamasanidf2e92a2013-03-01 17:04:38 -08001363 try {
Amith Yamasani71e6c692013-03-24 17:39:28 -07001364 return mService.isRestricted();
Amith Yamasanidf2e92a2013-03-01 17:04:38 -08001365 } catch (RemoteException re) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001366 throw re.rethrowFromSystemServer();
Amith Yamasanidf2e92a2013-03-01 17:04:38 -08001367 }
1368 }
1369
Amith Yamasani258848d2012-08-10 17:06:33 -07001370 /**
Fyodor Kupolov1c363152015-09-02 13:27:21 -07001371 * Checks if specified user can have restricted profile.
1372 * @hide
1373 */
Jeff Sharkey8588bc12016-01-06 16:47:42 -07001374 public boolean canHaveRestrictedProfile(@UserIdInt int userId) {
Fyodor Kupolov1c363152015-09-02 13:27:21 -07001375 try {
1376 return mService.canHaveRestrictedProfile(userId);
1377 } catch (RemoteException re) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001378 throw re.rethrowFromSystemServer();
Fyodor Kupolov1c363152015-09-02 13:27:21 -07001379 }
1380 }
1381
1382 /**
Fyodor Kupolovca177562017-11-09 17:43:01 -08001383 * Returns whether the calling user has at least one restricted profile associated with it.
1384 * @return
1385 * @hide
1386 */
1387 @SystemApi
1388 public boolean hasRestrictedProfiles() {
1389 try {
1390 return mService.hasRestrictedProfiles();
1391 } catch (RemoteException re) {
1392 throw re.rethrowFromSystemServer();
1393 }
1394 }
1395
1396 /**
Evan Rosky13a58a92016-07-27 15:51:09 -07001397 * Checks if a user is a guest user.
1398 * @return whether user is a guest user.
1399 * @hide
1400 */
1401 public boolean isGuestUser(int id) {
1402 UserInfo user = getUserInfo(id);
1403 return user != null && user.isGuest();
1404 }
1405
1406 /**
Varun Shahe142e332018-10-22 15:52:56 -07001407 * Used to check if this process is running under a guest user. A guest user may be transient.
1408 *
1409 * @return whether this process is running under a guest user.
Amith Yamasani1e9c2182014-06-11 17:25:51 -07001410 * @hide
1411 */
Varun Shahe142e332018-10-22 15:52:56 -07001412 @SystemApi
1413 @RequiresPermission(android.Manifest.permission.MANAGE_USERS)
Amith Yamasani1e9c2182014-06-11 17:25:51 -07001414 public boolean isGuestUser() {
1415 UserInfo user = getUserInfo(UserHandle.myUserId());
Amith Yamasanid35a89c2016-05-26 16:58:43 -07001416 return user != null && user.isGuest();
1417 }
1418
Fyodor Kupolovca177562017-11-09 17:43:01 -08001419
Amith Yamasanid35a89c2016-05-26 16:58:43 -07001420 /**
Amith Yamasani1c41dc82016-06-28 16:13:15 -07001421 * Checks if the calling app is running in a demo user. When running in a demo user,
1422 * apps can be more helpful to the user, or explain their features in more detail.
1423 *
Amith Yamasanid35a89c2016-05-26 16:58:43 -07001424 * @return whether the caller is a demo user.
Amith Yamasanid35a89c2016-05-26 16:58:43 -07001425 */
1426 public boolean isDemoUser() {
Amith Yamasani1c41dc82016-06-28 16:13:15 -07001427 try {
1428 return mService.isDemoUser(UserHandle.myUserId());
1429 } catch (RemoteException re) {
1430 throw re.rethrowFromSystemServer();
1431 }
Amith Yamasani1e9c2182014-06-11 17:25:51 -07001432 }
1433
1434 /**
Amith Yamasani0e8d7d62014-09-03 13:17:28 -07001435 * Checks if the calling app is running in a managed profile.
Amith Yamasani0e8d7d62014-09-03 13:17:28 -07001436 *
1437 * @return whether the caller is in a managed profile.
1438 * @hide
1439 */
1440 @SystemApi
Jeff Sharkeyd86b8fe2017-06-02 17:36:26 -06001441 @RequiresPermission(android.Manifest.permission.MANAGE_USERS)
Amith Yamasani0e8d7d62014-09-03 13:17:28 -07001442 public boolean isManagedProfile() {
Makoto Onukid49f3fa2017-01-25 14:09:48 -08001443 // No need for synchronization. Once it becomes non-null, it'll be non-null forever.
1444 // Worst case we might end up calling the AIDL method multiple times but that's fine.
1445 if (mIsManagedProfileCached != null) {
1446 return mIsManagedProfileCached;
1447 }
Tony Mak8673b282016-03-21 21:10:59 +00001448 try {
Makoto Onukid49f3fa2017-01-25 14:09:48 -08001449 mIsManagedProfileCached = mService.isManagedProfile(UserHandle.myUserId());
1450 return mIsManagedProfileCached;
Tony Mak8673b282016-03-21 21:10:59 +00001451 } catch (RemoteException re) {
1452 throw re.rethrowFromSystemServer();
1453 }
1454 }
1455
1456 /**
1457 * Checks if the specified user is a managed profile.
1458 * Requires {@link android.Manifest.permission#MANAGE_USERS} permission, otherwise the caller
1459 * must be in the same profile group of specified user.
1460 *
1461 * @return whether the specified user is a managed profile.
1462 * @hide
1463 */
1464 @SystemApi
Jeff Sharkeyd86b8fe2017-06-02 17:36:26 -06001465 @RequiresPermission(android.Manifest.permission.MANAGE_USERS)
Tony Mak8673b282016-03-21 21:10:59 +00001466 public boolean isManagedProfile(@UserIdInt int userId) {
Makoto Onukid49f3fa2017-01-25 14:09:48 -08001467 if (userId == UserHandle.myUserId()) {
1468 return isManagedProfile();
1469 }
Tony Mak8673b282016-03-21 21:10:59 +00001470 try {
1471 return mService.isManagedProfile(userId);
1472 } catch (RemoteException re) {
1473 throw re.rethrowFromSystemServer();
1474 }
Amith Yamasani0e8d7d62014-09-03 13:17:28 -07001475 }
1476
1477 /**
Kenny Guy02c89902016-11-15 19:36:38 +00001478 * Gets badge for a managed profile.
1479 * Requires {@link android.Manifest.permission#MANAGE_USERS} permission, otherwise the caller
1480 * must be in the same profile group of specified user.
1481 *
1482 * @return which badge to use for the managed profile badge id will be less than
1483 * UserManagerService.getMaxManagedProfiles()
1484 * @hide
1485 */
1486 public int getManagedProfileBadge(@UserIdInt int userId) {
1487 try {
1488 return mService.getManagedProfileBadge(userId);
1489 } catch (RemoteException re) {
1490 throw re.rethrowFromSystemServer();
1491 }
1492 }
1493
1494 /**
Lenka Trochtovac4dd0212015-11-18 12:22:06 +01001495 * Checks if the calling app is running as an ephemeral user.
1496 *
1497 * @return whether the caller is an ephemeral user.
1498 * @hide
1499 */
1500 public boolean isEphemeralUser() {
1501 return isUserEphemeral(UserHandle.myUserId());
1502 }
1503
1504 /**
1505 * Returns whether the specified user is ephemeral.
1506 * @hide
1507 */
Jeff Sharkey8588bc12016-01-06 16:47:42 -07001508 public boolean isUserEphemeral(@UserIdInt int userId) {
Lenka Trochtovac4dd0212015-11-18 12:22:06 +01001509 final UserInfo user = getUserInfo(userId);
1510 return user != null && user.isEphemeral();
1511 }
1512
1513 /**
Dianne Hackborna8a9bd62012-10-09 15:36:59 -07001514 * Return whether the given user is actively running. This means that
1515 * the user is in the "started" state, not "stopped" -- it is currently
1516 * allowed to run code through scheduled alarms, receiving broadcasts,
1517 * etc. A started user may be either the current foreground user or a
1518 * background user; the result here does not distinguish between the two.
Makoto Onuki5eef50d2017-03-02 16:38:45 -08001519 *
1520 * <p>Note prior to Android Nougat MR1 (SDK version <= 24;
Andrew Solovay8a788f42018-12-12 14:25:47 -08001521 * {@link android.os.Build.VERSION_CODES#N}, this API required a system permission
Makoto Onuki5eef50d2017-03-02 16:38:45 -08001522 * in order to check other profile's status.
1523 * Since Android Nougat MR1 (SDK version >= 25;
Andrew Solovay8a788f42018-12-12 14:25:47 -08001524 * {@link android.os.Build.VERSION_CODES#N_MR1}), the restriction has been relaxed, and now
1525 * it'll accept any {@link android.os.UserHandle} within the same profile group as the caller.
Fyodor Kupolovcdb3c2f2017-02-10 11:48:32 -08001526 *
Dianne Hackborna8a9bd62012-10-09 15:36:59 -07001527 * @param user The user to retrieve the running state for.
1528 */
Makoto Onuki5eef50d2017-03-02 16:38:45 -08001529 // Note this requires either INTERACT_ACROSS_USERS or MANAGE_USERS.
Dianne Hackborna8a9bd62012-10-09 15:36:59 -07001530 public boolean isUserRunning(UserHandle user) {
Jeff Sharkey0e62384c2016-01-13 18:52:55 -07001531 return isUserRunning(user.getIdentifier());
1532 }
1533
1534 /** {@hide} */
Fyodor Kupolov2e7e0962016-12-01 18:09:17 -08001535 public boolean isUserRunning(@UserIdInt int userId) {
Dianne Hackborna8a9bd62012-10-09 15:36:59 -07001536 try {
Fyodor Kupolov2e7e0962016-12-01 18:09:17 -08001537 return mService.isUserRunning(userId);
Jeff Sharkey27b2e692016-02-25 17:40:12 -07001538 } catch (RemoteException re) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001539 throw re.rethrowFromSystemServer();
Dianne Hackborna8a9bd62012-10-09 15:36:59 -07001540 }
1541 }
1542
1543 /**
1544 * Return whether the given user is actively running <em>or</em> stopping.
1545 * This is like {@link #isUserRunning(UserHandle)}, but will also return
1546 * true if the user had been running but is in the process of being stopped
1547 * (but is not yet fully stopped, and still running some code).
Makoto Onuki5eef50d2017-03-02 16:38:45 -08001548 *
1549 * <p>Note prior to Android Nougat MR1 (SDK version <= 24;
Andrew Solovay8a788f42018-12-12 14:25:47 -08001550 * {@link android.os.Build.VERSION_CODES#N}, this API required a system permission
Makoto Onuki5eef50d2017-03-02 16:38:45 -08001551 * in order to check other profile's status.
1552 * Since Android Nougat MR1 (SDK version >= 25;
Andrew Solovay8a788f42018-12-12 14:25:47 -08001553 * {@link android.os.Build.VERSION_CODES#N_MR1}), the restriction has been relaxed, and now
1554 * it'll accept any {@link android.os.UserHandle} within the same profile group as the caller.
Fyodor Kupolovcdb3c2f2017-02-10 11:48:32 -08001555 *
Dianne Hackborna8a9bd62012-10-09 15:36:59 -07001556 * @param user The user to retrieve the running state for.
1557 */
Makoto Onuki5eef50d2017-03-02 16:38:45 -08001558 // Note this requires either INTERACT_ACROSS_USERS or MANAGE_USERS.
Dianne Hackborna8a9bd62012-10-09 15:36:59 -07001559 public boolean isUserRunningOrStopping(UserHandle user) {
1560 try {
Jeff Sharkeye17ac152015-11-06 22:40:29 -08001561 // TODO: reconcile stopped vs stopping?
Sudheer Shankadc589ac2016-11-10 15:30:17 -08001562 return ActivityManager.getService().isUserRunning(
Jeff Sharkeye17ac152015-11-06 22:40:29 -08001563 user.getIdentifier(), ActivityManager.FLAG_OR_STOPPED);
Jeff Sharkey27b2e692016-02-25 17:40:12 -07001564 } catch (RemoteException re) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001565 throw re.rethrowFromSystemServer();
Dianne Hackborna8a9bd62012-10-09 15:36:59 -07001566 }
1567 }
1568
Jeff Sharkey0825ab22015-12-02 13:04:49 -07001569 /**
Jeff Sharkeycf3f0a12016-03-17 19:57:58 -06001570 * Return whether the calling user is running in an "unlocked" state.
1571 * <p>
1572 * On devices with direct boot, a user is unlocked only after they've
1573 * entered their credentials (such as a lock pattern or PIN). On devices
1574 * without direct boot, a user is unlocked as soon as it starts.
1575 * <p>
1576 * When a user is locked, only device-protected data storage is available.
1577 * When a user is unlocked, both device-protected and credential-protected
1578 * private app data storage is available.
Jeff Sharkey8a372a02016-03-16 16:25:45 -06001579 *
1580 * @see Intent#ACTION_USER_UNLOCKED
1581 * @see Context#createDeviceProtectedStorageContext()
Jeff Sharkeyb6423872015-12-11 11:11:58 -07001582 */
1583 public boolean isUserUnlocked() {
1584 return isUserUnlocked(Process.myUserHandle());
1585 }
1586
1587 /**
Jeff Sharkeycf3f0a12016-03-17 19:57:58 -06001588 * Return whether the given user is running in an "unlocked" state.
1589 * <p>
1590 * On devices with direct boot, a user is unlocked only after they've
1591 * entered their credentials (such as a lock pattern or PIN). On devices
1592 * without direct boot, a user is unlocked as soon as it starts.
1593 * <p>
1594 * When a user is locked, only device-protected data storage is available.
1595 * When a user is unlocked, both device-protected and credential-protected
1596 * private app data storage is available.
Fyodor Kupolovcdb3c2f2017-02-10 11:48:32 -08001597 * <p>Requires {@code android.permission.MANAGE_USERS} or
1598 * {@code android.permission.INTERACT_ACROSS_USERS}, otherwise specified {@link UserHandle user}
1599 * must be the calling user or a managed profile associated with it.
Jeff Sharkeyb6423872015-12-11 11:11:58 -07001600 *
1601 * @param user to retrieve the unlocked state for.
Jeff Sharkey8a372a02016-03-16 16:25:45 -06001602 * @see Intent#ACTION_USER_UNLOCKED
1603 * @see Context#createDeviceProtectedStorageContext()
Jeff Sharkeyb6423872015-12-11 11:11:58 -07001604 */
1605 public boolean isUserUnlocked(UserHandle user) {
Jeff Sharkey0999c0d2015-12-17 15:12:22 -07001606 return isUserUnlocked(user.getIdentifier());
1607 }
1608
1609 /** {@hide} */
Jeff Sharkey8588bc12016-01-06 16:47:42 -07001610 public boolean isUserUnlocked(@UserIdInt int userId) {
Jeff Sharkeyce18c812016-04-27 16:00:41 -06001611 try {
Fyodor Kupolov2e7e0962016-12-01 18:09:17 -08001612 return mService.isUserUnlocked(userId);
Jeff Sharkeyce18c812016-04-27 16:00:41 -06001613 } catch (RemoteException re) {
1614 throw re.rethrowFromSystemServer();
1615 }
1616 }
1617
1618 /** {@hide} */
1619 public boolean isUserUnlockingOrUnlocked(UserHandle user) {
1620 return isUserUnlockingOrUnlocked(user.getIdentifier());
1621 }
1622
1623 /** {@hide} */
1624 public boolean isUserUnlockingOrUnlocked(@UserIdInt int userId) {
1625 try {
Fyodor Kupolovc413f702016-10-06 17:11:14 -07001626 return mService.isUserUnlockingOrUnlocked(userId);
Jeff Sharkeyce18c812016-04-27 16:00:41 -06001627 } catch (RemoteException re) {
1628 throw re.rethrowFromSystemServer();
1629 }
Jeff Sharkeyb6423872015-12-11 11:11:58 -07001630 }
1631
1632 /**
Makoto Onuki73dded22017-12-20 13:14:48 +09001633 * Return the time when the calling user started in elapsed milliseconds since boot,
1634 * or 0 if not started.
1635 *
1636 * @hide
1637 */
1638 public long getUserStartRealtime() {
1639 try {
1640 return mService.getUserStartRealtime();
1641 } catch (RemoteException re) {
1642 throw re.rethrowFromSystemServer();
1643 }
1644 }
1645
1646 /**
1647 * Return the time when the calling user was unlocked elapsed milliseconds since boot,
1648 * or 0 if not unlocked.
1649 *
1650 * @hide
1651 */
1652 public long getUserUnlockRealtime() {
1653 try {
1654 return mService.getUserUnlockRealtime();
1655 } catch (RemoteException re) {
1656 throw re.rethrowFromSystemServer();
1657 }
1658 }
1659
1660 /**
Amith Yamasani258848d2012-08-10 17:06:33 -07001661 * Returns the UserInfo object describing a specific user.
Tony Mak8673b282016-03-21 21:10:59 +00001662 * Requires {@link android.Manifest.permission#MANAGE_USERS} permission.
Amith Yamasani258848d2012-08-10 17:06:33 -07001663 * @param userHandle the user handle of the user whose information is being requested.
1664 * @return the UserInfo object for a specific user.
1665 * @hide
Dianne Hackbornb26306a2012-10-24 15:22:21 -07001666 */
Jeff Sharkey8588bc12016-01-06 16:47:42 -07001667 public UserInfo getUserInfo(@UserIdInt int userHandle) {
Amith Yamasani258848d2012-08-10 17:06:33 -07001668 try {
1669 return mService.getUserInfo(userHandle);
1670 } catch (RemoteException re) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001671 throw re.rethrowFromSystemServer();
Amith Yamasani258848d2012-08-10 17:06:33 -07001672 }
1673 }
1674
Amith Yamasani71e6c692013-03-24 17:39:28 -07001675 /**
Zoltan Szatmary-Bane7834602016-04-08 18:41:11 +01001676 * @hide
1677 *
1678 * Returns who set a user restriction on a user.
Zoltan Szatmary-Bane7834602016-04-08 18:41:11 +01001679 * @param restrictionKey the string key representing the restriction
1680 * @param userHandle the UserHandle of the user for whom to retrieve the restrictions.
1681 * @return The source of user restriction. Any combination of {@link #RESTRICTION_NOT_SET},
1682 * {@link #RESTRICTION_SOURCE_SYSTEM}, {@link #RESTRICTION_SOURCE_DEVICE_OWNER}
1683 * and {@link #RESTRICTION_SOURCE_PROFILE_OWNER}
Pavel Grafov6a40f092016-10-25 15:46:51 +01001684 * @deprecated use {@link #getUserRestrictionSources(String, int)} instead.
Zoltan Szatmary-Bane7834602016-04-08 18:41:11 +01001685 */
Pavel Grafov6a40f092016-10-25 15:46:51 +01001686 @Deprecated
Zoltan Szatmary-Bane7834602016-04-08 18:41:11 +01001687 @SystemApi
1688 @UserRestrictionSource
Jeff Sharkeyd86b8fe2017-06-02 17:36:26 -06001689 @RequiresPermission(android.Manifest.permission.MANAGE_USERS)
Zoltan Szatmary-Bane7834602016-04-08 18:41:11 +01001690 public int getUserRestrictionSource(String restrictionKey, UserHandle userHandle) {
1691 try {
1692 return mService.getUserRestrictionSource(restrictionKey, userHandle.getIdentifier());
1693 } catch (RemoteException re) {
1694 throw re.rethrowFromSystemServer();
1695 }
1696 }
1697
1698 /**
Pavel Grafov6a40f092016-10-25 15:46:51 +01001699 * @hide
1700 *
1701 * Returns a list of users who set a user restriction on a given user.
Pavel Grafov6a40f092016-10-25 15:46:51 +01001702 * @param restrictionKey the string key representing the restriction
1703 * @param userHandle the UserHandle of the user for whom to retrieve the restrictions.
1704 * @return a list of user ids enforcing this restriction.
1705 */
1706 @SystemApi
Jeff Sharkeyd86b8fe2017-06-02 17:36:26 -06001707 @RequiresPermission(android.Manifest.permission.MANAGE_USERS)
Pavel Grafov6a40f092016-10-25 15:46:51 +01001708 public List<EnforcingUser> getUserRestrictionSources(
1709 String restrictionKey, UserHandle userHandle) {
1710 try {
1711 return mService.getUserRestrictionSources(restrictionKey, userHandle.getIdentifier());
1712 } catch (RemoteException re) {
1713 throw re.rethrowFromSystemServer();
1714 }
1715 }
1716
1717 /**
Amith Yamasani71e6c692013-03-24 17:39:28 -07001718 * Returns the user-wide restrictions imposed on this user.
1719 * @return a Bundle containing all the restrictions.
1720 */
Amith Yamasanie4cf7342012-12-17 11:12:09 -08001721 public Bundle getUserRestrictions() {
1722 return getUserRestrictions(Process.myUserHandle());
1723 }
1724
Amith Yamasani71e6c692013-03-24 17:39:28 -07001725 /**
1726 * Returns the user-wide restrictions imposed on the user specified by <code>userHandle</code>.
1727 * @param userHandle the UserHandle of the user for whom to retrieve the restrictions.
1728 * @return a Bundle containing all the restrictions.
1729 */
Amith Yamasanie4cf7342012-12-17 11:12:09 -08001730 public Bundle getUserRestrictions(UserHandle userHandle) {
1731 try {
1732 return mService.getUserRestrictions(userHandle.getIdentifier());
1733 } catch (RemoteException re) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001734 throw re.rethrowFromSystemServer();
Amith Yamasanie4cf7342012-12-17 11:12:09 -08001735 }
1736 }
1737
Zoltan Szatmary-Ban3bbcedd2015-11-26 13:45:51 +00001738 /**
1739 * @hide
1740 * Returns whether the given user has been disallowed from performing certain actions
Irina Dumitrescu4638edd2018-09-05 14:08:33 +01001741 * or setting certain settings through UserManager (e.g. this type of restriction would prevent
1742 * the guest user from doing certain things, such as making calls). This method disregards
1743 * restrictions set by device policy.
Zoltan Szatmary-Ban3bbcedd2015-11-26 13:45:51 +00001744 * @param restrictionKey the string key representing the restriction
1745 * @param userHandle the UserHandle of the user for whom to retrieve the restrictions.
1746 */
1747 public boolean hasBaseUserRestriction(String restrictionKey, UserHandle userHandle) {
1748 try {
1749 return mService.hasBaseUserRestriction(restrictionKey, userHandle.getIdentifier());
1750 } catch (RemoteException re) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001751 throw re.rethrowFromSystemServer();
Zoltan Szatmary-Ban3bbcedd2015-11-26 13:45:51 +00001752 }
1753 }
1754
Amith Yamasani71e6c692013-03-24 17:39:28 -07001755 /**
Makoto Onukia3c12502015-10-28 10:18:32 -07001756 * This will no longer work. Device owners and profile owners should use
1757 * {@link DevicePolicyManager#addUserRestriction(ComponentName, String)} instead.
Amith Yamasani71e6c692013-03-24 17:39:28 -07001758 */
Makoto Onukia3c12502015-10-28 10:18:32 -07001759 // System apps should use UserManager.setUserRestriction() instead.
Julia Reynolds3d9eb782014-08-11 16:40:08 -04001760 @Deprecated
Amith Yamasanie4cf7342012-12-17 11:12:09 -08001761 public void setUserRestrictions(Bundle restrictions) {
Makoto Onuki068c54a2015-10-13 14:34:03 -07001762 throw new UnsupportedOperationException("This method is no longer supported");
Amith Yamasanie4cf7342012-12-17 11:12:09 -08001763 }
1764
Amith Yamasani71e6c692013-03-24 17:39:28 -07001765 /**
Makoto Onukia3c12502015-10-28 10:18:32 -07001766 * This will no longer work. Device owners and profile owners should use
1767 * {@link DevicePolicyManager#addUserRestriction(ComponentName, String)} instead.
Amith Yamasani71e6c692013-03-24 17:39:28 -07001768 */
Makoto Onukia3c12502015-10-28 10:18:32 -07001769 // System apps should use UserManager.setUserRestriction() instead.
Julia Reynolds3d9eb782014-08-11 16:40:08 -04001770 @Deprecated
Amith Yamasanie4cf7342012-12-17 11:12:09 -08001771 public void setUserRestrictions(Bundle restrictions, UserHandle userHandle) {
Makoto Onuki068c54a2015-10-13 14:34:03 -07001772 throw new UnsupportedOperationException("This method is no longer supported");
Amith Yamasanie4cf7342012-12-17 11:12:09 -08001773 }
1774
Amith Yamasani71e6c692013-03-24 17:39:28 -07001775 /**
1776 * Sets the value of a specific restriction.
Amith Yamasanibe465322014-04-24 13:45:17 -07001777 * Requires the MANAGE_USERS permission.
Amith Yamasani71e6c692013-03-24 17:39:28 -07001778 * @param key the key of the restriction
1779 * @param value the value for the restriction
Julia Reynolds3d9eb782014-08-11 16:40:08 -04001780 * @deprecated use {@link android.app.admin.DevicePolicyManager#addUserRestriction(
1781 * android.content.ComponentName, String)} or
1782 * {@link android.app.admin.DevicePolicyManager#clearUserRestriction(
1783 * android.content.ComponentName, String)} instead.
Amith Yamasani71e6c692013-03-24 17:39:28 -07001784 */
Julia Reynolds3d9eb782014-08-11 16:40:08 -04001785 @Deprecated
Amith Yamasani71e6c692013-03-24 17:39:28 -07001786 public void setUserRestriction(String key, boolean value) {
Makoto Onuki068c54a2015-10-13 14:34:03 -07001787 setUserRestriction(key, value, Process.myUserHandle());
Amith Yamasani71e6c692013-03-24 17:39:28 -07001788 }
1789
1790 /**
1791 * @hide
1792 * Sets the value of a specific restriction on a specific user.
Amith Yamasanibe465322014-04-24 13:45:17 -07001793 * Requires the MANAGE_USERS permission.
Amith Yamasani71e6c692013-03-24 17:39:28 -07001794 * @param key the key of the restriction
1795 * @param value the value for the restriction
1796 * @param userHandle the user whose restriction is to be changed.
Julia Reynolds3d9eb782014-08-11 16:40:08 -04001797 * @deprecated use {@link android.app.admin.DevicePolicyManager#addUserRestriction(
1798 * android.content.ComponentName, String)} or
1799 * {@link android.app.admin.DevicePolicyManager#clearUserRestriction(
1800 * android.content.ComponentName, String)} instead.
Amith Yamasani71e6c692013-03-24 17:39:28 -07001801 */
Julia Reynolds3d9eb782014-08-11 16:40:08 -04001802 @Deprecated
Maggie Benthall67944582013-02-22 14:58:27 -05001803 public void setUserRestriction(String key, boolean value, UserHandle userHandle) {
Fyodor Kupolovb5013302015-04-17 17:59:14 -07001804 try {
1805 mService.setUserRestriction(key, value, userHandle.getIdentifier());
1806 } catch (RemoteException re) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001807 throw re.rethrowFromSystemServer();
Fyodor Kupolovb5013302015-04-17 17:59:14 -07001808 }
Maggie Benthall67944582013-02-22 14:58:27 -05001809 }
1810
Amith Yamasani258848d2012-08-10 17:06:33 -07001811 /**
Maggie Benthalla12fccf2013-03-14 18:02:12 -04001812 * Returns whether the current user has been disallowed from performing certain actions
1813 * or setting certain settings.
Julia Reynolds2b2cf722014-06-06 11:41:04 -04001814 *
1815 * @param restrictionKey The string key representing the restriction.
1816 * @return {@code true} if the current user has the given restriction, {@code false} otherwise.
Maggie Benthalla12fccf2013-03-14 18:02:12 -04001817 */
1818 public boolean hasUserRestriction(String restrictionKey) {
David Christieb12ba932013-09-03 17:15:28 -07001819 return hasUserRestriction(restrictionKey, Process.myUserHandle());
1820 }
1821
1822 /**
1823 * @hide
1824 * Returns whether the given user has been disallowed from performing certain actions
1825 * or setting certain settings.
1826 * @param restrictionKey the string key representing the restriction
1827 * @param userHandle the UserHandle of the user for whom to retrieve the restrictions.
1828 */
1829 public boolean hasUserRestriction(String restrictionKey, UserHandle userHandle) {
Amith Yamasani8cd28b52014-06-08 17:54:27 -07001830 try {
1831 return mService.hasUserRestriction(restrictionKey,
1832 userHandle.getIdentifier());
1833 } catch (RemoteException re) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001834 throw re.rethrowFromSystemServer();
Amith Yamasani8cd28b52014-06-08 17:54:27 -07001835 }
Maggie Benthalla12fccf2013-03-14 18:02:12 -04001836 }
1837
1838 /**
Makoto Onukiacc50462018-02-14 14:13:49 -08001839 * @hide
1840 * Returns whether any user on the device has the given user restriction set.
1841 */
1842 public boolean hasUserRestrictionOnAnyUser(String restrictionKey) {
1843 try {
1844 return mService.hasUserRestrictionOnAnyUser(restrictionKey);
1845 } catch (RemoteException re) {
1846 throw re.rethrowFromSystemServer();
1847 }
1848 }
1849
1850 /**
Dianne Hackborn33f9cb82012-10-04 17:15:10 -07001851 * Return the serial number for a user. This is a device-unique
Dianne Hackbornb26306a2012-10-24 15:22:21 -07001852 * number assigned to that user; if the user is deleted and then a new
1853 * user created, the new users will not be given the same serial number.
Dianne Hackborn33f9cb82012-10-04 17:15:10 -07001854 * @param user The user whose serial number is to be retrieved.
Dianne Hackbornb26306a2012-10-24 15:22:21 -07001855 * @return The serial number of the given user; returns -1 if the
1856 * given UserHandle does not exist.
Dianne Hackborn33f9cb82012-10-04 17:15:10 -07001857 * @see #getUserForSerialNumber(long)
1858 */
1859 public long getSerialNumberForUser(UserHandle user) {
1860 return getUserSerialNumber(user.getIdentifier());
1861 }
1862
1863 /**
1864 * Return the user associated with a serial number previously
1865 * returned by {@link #getSerialNumberForUser(UserHandle)}.
1866 * @param serialNumber The serial number of the user that is being
1867 * retrieved.
1868 * @return Return the user associated with the serial number, or null
1869 * if there is not one.
1870 * @see #getSerialNumberForUser(UserHandle)
1871 */
1872 public UserHandle getUserForSerialNumber(long serialNumber) {
Fyodor Kupolovef249092015-05-06 13:18:46 -07001873 int ident = getUserHandle((int) serialNumber);
Dianne Hackborn33f9cb82012-10-04 17:15:10 -07001874 return ident >= 0 ? new UserHandle(ident) : null;
1875 }
1876
1877 /**
Xiaohui Chencfe64c82015-07-16 14:30:50 -07001878 * Creates a user with the specified name and options. For non-admin users, default user
1879 * restrictions are going to be applied.
Amith Yamasani195263742012-08-21 15:40:12 -07001880 * Requires {@link android.Manifest.permission#MANAGE_USERS} permission.
Amith Yamasani258848d2012-08-10 17:06:33 -07001881 *
1882 * @param name the user's name
1883 * @param flags flags that identify the type of user and other properties.
1884 * @see UserInfo
1885 *
1886 * @return the UserInfo object for the created user, or null if the user could not be created.
1887 * @hide
1888 */
1889 public UserInfo createUser(String name, int flags) {
Xiaohui Chencfe64c82015-07-16 14:30:50 -07001890 UserInfo user = null;
Amith Yamasani258848d2012-08-10 17:06:33 -07001891 try {
Xiaohui Chencfe64c82015-07-16 14:30:50 -07001892 user = mService.createUser(name, flags);
phweisse9c44062016-02-10 12:57:38 +01001893 // TODO: Keep this in sync with
1894 // UserManagerService.LocalService.createUserEvenWhenDisallowed
Christine Franks97a54802017-08-09 10:06:43 -07001895 if (user != null && !user.isAdmin() && !user.isDemo()) {
Makoto Onuki068c54a2015-10-13 14:34:03 -07001896 mService.setUserRestriction(DISALLOW_SMS, true, user.id);
1897 mService.setUserRestriction(DISALLOW_OUTGOING_CALLS, true, user.id);
Xiaohui Chencfe64c82015-07-16 14:30:50 -07001898 }
Amith Yamasani258848d2012-08-10 17:06:33 -07001899 } catch (RemoteException re) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001900 throw re.rethrowFromSystemServer();
Amith Yamasani258848d2012-08-10 17:06:33 -07001901 }
Xiaohui Chencfe64c82015-07-16 14:30:50 -07001902 return user;
Amith Yamasani258848d2012-08-10 17:06:33 -07001903 }
1904
1905 /**
Amith Yamasani1e9c2182014-06-11 17:25:51 -07001906 * Creates a guest user and configures it.
1907 * @param context an application context
1908 * @param name the name to set for the user
1909 * @hide
1910 */
1911 public UserInfo createGuest(Context context, String name) {
Makoto Onuki068c54a2015-10-13 14:34:03 -07001912 UserInfo guest = null;
1913 try {
1914 guest = mService.createUser(name, UserInfo.FLAG_GUEST);
1915 if (guest != null) {
1916 Settings.Secure.putStringForUser(context.getContentResolver(),
1917 Settings.Secure.SKIP_FIRST_USE_HINTS, "1", guest.id);
Amith Yamasanibf3a9462014-07-28 14:26:42 -07001918 }
Makoto Onuki068c54a2015-10-13 14:34:03 -07001919 } catch (RemoteException re) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001920 throw re.rethrowFromSystemServer();
Amith Yamasani1e9c2182014-06-11 17:25:51 -07001921 }
1922 return guest;
1923 }
1924
Amith Yamasaniaa6634e2014-10-06 14:20:28 -07001925 /**
Kenny Guy2a764942014-04-02 13:29:20 +01001926 * Creates a user with the specified name and options as a profile of another user.
Kenny Guya52dc3e2014-02-11 15:33:14 +00001927 * Requires {@link android.Manifest.permission#MANAGE_USERS} permission.
1928 *
1929 * @param name the user's name
1930 * @param flags flags that identify the type of user and other properties.
Sudheer Shanka7cb54a32016-09-16 12:59:05 -07001931 * @param userHandle new user will be a profile of this user.
Kenny Guya52dc3e2014-02-11 15:33:14 +00001932 *
Sudheer Shanka7cb54a32016-09-16 12:59:05 -07001933 * @return the {@link UserInfo} object for the created user, or null if the user
1934 * could not be created.
Kenny Guya52dc3e2014-02-11 15:33:14 +00001935 * @hide
1936 */
Jeff Sharkey8588bc12016-01-06 16:47:42 -07001937 public UserInfo createProfileForUser(String name, int flags, @UserIdInt int userHandle) {
Sudheer Shanka7cb54a32016-09-16 12:59:05 -07001938 return createProfileForUser(name, flags, userHandle, null);
1939 }
1940
1941 /**
1942 * Version of {@link #createProfileForUser(String, int, int)} that allows you to specify
1943 * any packages that should not be installed in the new profile by default, these packages can
1944 * still be installed later by the user if needed.
1945 *
1946 * @param name the user's name
1947 * @param flags flags that identify the type of user and other properties.
1948 * @param userHandle new user will be a profile of this user.
1949 * @param disallowedPackages packages that will not be installed in the profile being created.
1950 *
1951 * @return the {@link UserInfo} object for the created user, or null if the user
1952 * could not be created.
1953 * @hide
1954 */
1955 public UserInfo createProfileForUser(String name, int flags, @UserIdInt int userHandle,
1956 String[] disallowedPackages) {
Kenny Guya52dc3e2014-02-11 15:33:14 +00001957 try {
Sudheer Shanka7cb54a32016-09-16 12:59:05 -07001958 return mService.createProfileForUser(name, flags, userHandle, disallowedPackages);
Kenny Guya52dc3e2014-02-11 15:33:14 +00001959 } catch (RemoteException re) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001960 throw re.rethrowFromSystemServer();
Kenny Guya52dc3e2014-02-11 15:33:14 +00001961 }
1962 }
1963
1964 /**
Tony Mak6dc428f2016-10-10 15:48:27 +01001965 * Similar to {@link #createProfileForUser(String, int, int, String[])}
Esteban Talavera6c9116a2016-11-24 16:12:44 +00001966 * except bypassing the checking of {@link UserManager#DISALLOW_ADD_MANAGED_PROFILE}.
Tony Mak6dc428f2016-10-10 15:48:27 +01001967 * Requires {@link android.Manifest.permission#MANAGE_USERS} permission.
1968 *
1969 * @see #createProfileForUser(String, int, int, String[])
1970 * @hide
1971 */
1972 public UserInfo createProfileForUserEvenWhenDisallowed(String name, int flags,
1973 @UserIdInt int userHandle, String[] disallowedPackages) {
1974 try {
1975 return mService.createProfileForUserEvenWhenDisallowed(name, flags, userHandle,
1976 disallowedPackages);
1977 } catch (RemoteException re) {
1978 throw re.rethrowFromSystemServer();
1979 }
1980 }
1981
1982 /**
Fyodor Kupolov02cb6e72015-09-18 18:20:55 -07001983 * Creates a restricted profile with the specified name. This method also sets necessary
1984 * restrictions and adds shared accounts.
Fyodor Kupolov06a484a2015-08-21 16:33:20 -07001985 *
1986 * @param name profile's name
1987 * @return UserInfo object for the created user, or null if the user could not be created.
1988 * @hide
1989 */
1990 public UserInfo createRestrictedProfile(String name) {
1991 try {
Fyodor Kupolov02cb6e72015-09-18 18:20:55 -07001992 UserHandle parentUserHandle = Process.myUserHandle();
1993 UserInfo user = mService.createRestrictedProfile(name,
1994 parentUserHandle.getIdentifier());
1995 if (user != null) {
1996 AccountManager.get(mContext).addSharedAccountsFromParentUser(parentUserHandle,
1997 UserHandle.of(user.id));
Fyodor Kupolov06a484a2015-08-21 16:33:20 -07001998 }
Fyodor Kupolov02cb6e72015-09-18 18:20:55 -07001999 return user;
Jeff Sharkey27b2e692016-02-25 17:40:12 -07002000 } catch (RemoteException re) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07002001 throw re.rethrowFromSystemServer();
Fyodor Kupolov06a484a2015-08-21 16:33:20 -07002002 }
Fyodor Kupolov06a484a2015-08-21 16:33:20 -07002003 }
2004
2005 /**
Amith Yamasani06964342016-04-15 13:55:01 -07002006 * Returns an intent to create a user for the provided name and account name. The name
2007 * and account name will be used when the setup process for the new user is started.
2008 * <p>
Amith Yamasani12747872015-12-07 14:19:49 -08002009 * The intent should be launched using startActivityForResult and the return result will
Amith Yamasani37ed8d12016-01-27 14:40:16 -08002010 * indicate if the user consented to adding a new user and if the operation succeeded. Any
2011 * errors in creating the user will be returned in the result code. If the user cancels the
2012 * request, the return result will be {@link Activity#RESULT_CANCELED}. On success, the
2013 * result code will be {@link Activity#RESULT_OK}.
Amith Yamasani06964342016-04-15 13:55:01 -07002014 * <p>
2015 * Use {@link #supportsMultipleUsers()} to first check if the device supports this operation
2016 * at all.
2017 * <p>
Amith Yamasani12747872015-12-07 14:19:49 -08002018 * The new user is created but not initialized. After switching into the user for the first
2019 * time, the preferred user name and account information are used by the setup process for that
2020 * user.
2021 *
2022 * @param userName Optional name to assign to the user.
Amith Yamasani06964342016-04-15 13:55:01 -07002023 * @param accountName Optional account name that will be used by the setup wizard to initialize
Amith Yamasani12747872015-12-07 14:19:49 -08002024 * the user.
2025 * @param accountType Optional account type for the account to be created. This is required
2026 * if the account name is specified.
2027 * @param accountOptions Optional bundle of data to be passed in during account creation in the
2028 * new user via {@link AccountManager#addAccount(String, String, String[],
2029 * Bundle, android.app.Activity, android.accounts.AccountManagerCallback,
2030 * Handler)}.
Amith Yamasani06964342016-04-15 13:55:01 -07002031 * @return An Intent that can be launched from an Activity.
Amith Yamasani37ed8d12016-01-27 14:40:16 -08002032 * @see #USER_CREATION_FAILED_NOT_PERMITTED
2033 * @see #USER_CREATION_FAILED_NO_MORE_USERS
Amith Yamasani06964342016-04-15 13:55:01 -07002034 * @see #supportsMultipleUsers
Amith Yamasani12747872015-12-07 14:19:49 -08002035 */
2036 public static Intent createUserCreationIntent(@Nullable String userName,
2037 @Nullable String accountName,
2038 @Nullable String accountType, @Nullable PersistableBundle accountOptions) {
Amith Yamasani12747872015-12-07 14:19:49 -08002039 Intent intent = new Intent(ACTION_CREATE_USER);
2040 if (userName != null) {
2041 intent.putExtra(EXTRA_USER_NAME, userName);
2042 }
2043 if (accountName != null && accountType == null) {
2044 throw new IllegalArgumentException("accountType must be specified if accountName is "
2045 + "specified");
2046 }
2047 if (accountName != null) {
2048 intent.putExtra(EXTRA_USER_ACCOUNT_NAME, accountName);
2049 }
2050 if (accountType != null) {
2051 intent.putExtra(EXTRA_USER_ACCOUNT_TYPE, accountType);
2052 }
2053 if (accountOptions != null) {
2054 intent.putExtra(EXTRA_USER_ACCOUNT_OPTIONS, accountOptions);
2055 }
2056 return intent;
2057 }
2058
2059 /**
2060 * @hide
2061 *
Jeff Sharkeyd86b8fe2017-06-02 17:36:26 -06002062 * Returns the preferred account name for user creation.
Amith Yamasani12747872015-12-07 14:19:49 -08002063 */
2064 @SystemApi
Jeff Sharkeyd86b8fe2017-06-02 17:36:26 -06002065 @RequiresPermission(android.Manifest.permission.MANAGE_USERS)
Amith Yamasani12747872015-12-07 14:19:49 -08002066 public String getSeedAccountName() {
2067 try {
2068 return mService.getSeedAccountName();
2069 } catch (RemoteException re) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07002070 throw re.rethrowFromSystemServer();
Amith Yamasani12747872015-12-07 14:19:49 -08002071 }
2072 }
2073
2074 /**
2075 * @hide
2076 *
Jeff Sharkeyd86b8fe2017-06-02 17:36:26 -06002077 * Returns the preferred account type for user creation.
Amith Yamasani12747872015-12-07 14:19:49 -08002078 */
2079 @SystemApi
Jeff Sharkeyd86b8fe2017-06-02 17:36:26 -06002080 @RequiresPermission(android.Manifest.permission.MANAGE_USERS)
Amith Yamasani12747872015-12-07 14:19:49 -08002081 public String getSeedAccountType() {
2082 try {
2083 return mService.getSeedAccountType();
2084 } catch (RemoteException re) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07002085 throw re.rethrowFromSystemServer();
Amith Yamasani12747872015-12-07 14:19:49 -08002086 }
2087 }
2088
2089 /**
2090 * @hide
2091 *
Jeff Sharkeyd86b8fe2017-06-02 17:36:26 -06002092 * Returns the preferred account's options bundle for user creation.
Amith Yamasani12747872015-12-07 14:19:49 -08002093 * @return Any options set by the requestor that created the user.
2094 */
2095 @SystemApi
Jeff Sharkeyd86b8fe2017-06-02 17:36:26 -06002096 @RequiresPermission(android.Manifest.permission.MANAGE_USERS)
Amith Yamasani12747872015-12-07 14:19:49 -08002097 public PersistableBundle getSeedAccountOptions() {
2098 try {
2099 return mService.getSeedAccountOptions();
2100 } catch (RemoteException re) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07002101 throw re.rethrowFromSystemServer();
Amith Yamasani12747872015-12-07 14:19:49 -08002102 }
2103 }
2104
2105 /**
2106 * @hide
2107 *
2108 * Called by a system activity to set the seed account information of a user created
2109 * through the user creation intent.
2110 * @param userId
2111 * @param accountName
2112 * @param accountType
2113 * @param accountOptions
2114 * @see #createUserCreationIntent(String, String, String, PersistableBundle)
2115 */
2116 public void setSeedAccountData(int userId, String accountName, String accountType,
2117 PersistableBundle accountOptions) {
2118 try {
2119 mService.setSeedAccountData(userId, accountName, accountType, accountOptions,
2120 /* persist= */ true);
2121 } catch (RemoteException re) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07002122 throw re.rethrowFromSystemServer();
Amith Yamasani12747872015-12-07 14:19:49 -08002123 }
2124 }
2125
2126 /**
2127 * @hide
Jeff Sharkeyd86b8fe2017-06-02 17:36:26 -06002128 * Clears the seed information used to create this user.
Amith Yamasani12747872015-12-07 14:19:49 -08002129 */
2130 @SystemApi
Jeff Sharkeyd86b8fe2017-06-02 17:36:26 -06002131 @RequiresPermission(android.Manifest.permission.MANAGE_USERS)
Amith Yamasani12747872015-12-07 14:19:49 -08002132 public void clearSeedAccountData() {
2133 try {
2134 mService.clearSeedAccountData();
2135 } catch (RemoteException re) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07002136 throw re.rethrowFromSystemServer();
Amith Yamasani12747872015-12-07 14:19:49 -08002137 }
2138 }
2139
2140 /**
Amith Yamasani1df14732014-08-29 21:37:27 -07002141 * @hide
2142 * Marks the guest user for deletion to allow a new guest to be created before deleting
2143 * the current user who is a guest.
2144 * @param userHandle
2145 * @return
2146 */
Jeff Sharkey8588bc12016-01-06 16:47:42 -07002147 public boolean markGuestForDeletion(@UserIdInt int userHandle) {
Amith Yamasani1df14732014-08-29 21:37:27 -07002148 try {
2149 return mService.markGuestForDeletion(userHandle);
2150 } catch (RemoteException re) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07002151 throw re.rethrowFromSystemServer();
Amith Yamasani1df14732014-08-29 21:37:27 -07002152 }
2153 }
2154
2155 /**
Alexandra Gherghinadf35d572014-04-09 13:54:39 +01002156 * Sets the user as enabled, if such an user exists.
Lenka Trochtova1ddda472016-02-12 10:42:12 +01002157 *
2158 * <p>Requires {@link android.Manifest.permission#MANAGE_USERS} permission.
2159 *
2160 * <p>Note that the default is true, it's only that managed profiles might not be enabled.
2161 * Also ephemeral users can be disabled to indicate that their removal is in progress and they
2162 * shouldn't be re-entered. Therefore ephemeral users should not be re-enabled once disabled.
Alexandra Gherghinadf35d572014-04-09 13:54:39 +01002163 *
jovanakf24ad492018-05-18 12:15:59 -07002164 * @param userId the id of the profile to enable
Alexandra Gherghinadf35d572014-04-09 13:54:39 +01002165 * @hide
2166 */
jovanakf24ad492018-05-18 12:15:59 -07002167 public void setUserEnabled(@UserIdInt int userId) {
Alexandra Gherghinadf35d572014-04-09 13:54:39 +01002168 try {
jovanakf24ad492018-05-18 12:15:59 -07002169 mService.setUserEnabled(userId);
2170 } catch (RemoteException re) {
2171 throw re.rethrowFromSystemServer();
2172 }
2173 }
2174
2175 /**
2176 * Assigns admin privileges to the user, if such a user exists.
2177 *
2178 * <p>Requires {@link android.Manifest.permission#MANAGE_USERS} and
2179 * {@link android.Manifest.permission#INTERACT_ACROSS_USERS_FULL} permissions.
2180 *
2181 * @param userHandle the id of the user to become admin
2182 * @hide
2183 */
2184 @RequiresPermission(allOf = {
2185 Manifest.permission.INTERACT_ACROSS_USERS_FULL,
2186 Manifest.permission.MANAGE_USERS
2187 })
2188 public void setUserAdmin(@UserIdInt int userHandle) {
2189 try {
2190 mService.setUserAdmin(userHandle);
Jeff Sharkey27b2e692016-02-25 17:40:12 -07002191 } catch (RemoteException re) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07002192 throw re.rethrowFromSystemServer();
Alexandra Gherghinadf35d572014-04-09 13:54:39 +01002193 }
2194 }
2195
2196 /**
Andrew Scull85a63bc2016-10-24 13:47:47 +01002197 * Evicts the user's credential encryption key from memory by stopping and restarting the user.
2198 *
2199 * @hide
2200 */
2201 public void evictCredentialEncryptionKey(@UserIdInt int userHandle) {
2202 try {
2203 mService.evictCredentialEncryptionKey(userHandle);
2204 } catch (RemoteException re) {
2205 throw re.rethrowFromSystemServer();
2206 }
2207 }
2208
2209 /**
Dianne Hackbornb26306a2012-10-24 15:22:21 -07002210 * Return the number of users currently created on the device.
2211 */
2212 public int getUserCount() {
2213 List<UserInfo> users = getUsers();
2214 return users != null ? users.size() : 1;
2215 }
2216
2217 /**
Amith Yamasanid04aaa32016-06-13 12:09:36 -07002218 * Returns information for all users on this device, including ones marked for deletion.
2219 * To retrieve only users that are alive, use {@link #getUsers(boolean)}.
2220 * <p>
Amith Yamasani195263742012-08-21 15:40:12 -07002221 * Requires {@link android.Manifest.permission#MANAGE_USERS} permission.
Fyodor Kupolov940e8572016-01-26 12:03:51 -08002222 * @return the list of users that exist on the device.
Amith Yamasani258848d2012-08-10 17:06:33 -07002223 * @hide
2224 */
2225 public List<UserInfo> getUsers() {
2226 try {
Amith Yamasani920ace02012-09-20 22:15:37 -07002227 return mService.getUsers(false);
2228 } catch (RemoteException re) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07002229 throw re.rethrowFromSystemServer();
Amith Yamasani920ace02012-09-20 22:15:37 -07002230 }
2231 }
2232
2233 /**
Fyodor Kupolov940e8572016-01-26 12:03:51 -08002234 * Returns serial numbers of all users on this device.
Fyodor Kupolov940e8572016-01-26 12:03:51 -08002235 *
2236 * @param excludeDying specify if the list should exclude users being removed.
2237 * @return the list of serial numbers of users that exist on the device.
2238 * @hide
2239 */
2240 @SystemApi
Jeff Sharkeyd86b8fe2017-06-02 17:36:26 -06002241 @RequiresPermission(android.Manifest.permission.MANAGE_USERS)
Fyodor Kupolov940e8572016-01-26 12:03:51 -08002242 public long[] getSerialNumbersOfUsers(boolean excludeDying) {
2243 try {
2244 List<UserInfo> users = mService.getUsers(excludeDying);
2245 long[] result = new long[users.size()];
2246 for (int i = 0; i < result.length; i++) {
2247 result[i] = users.get(i).serialNumber;
2248 }
2249 return result;
2250 } catch (RemoteException re) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07002251 throw re.rethrowFromSystemServer();
Fyodor Kupolov940e8572016-01-26 12:03:51 -08002252 }
2253 }
2254
2255 /**
Xiaohui Chenb3b92582015-12-07 11:22:13 -08002256 * @return the user's account name, null if not found.
2257 * @hide
2258 */
2259 @RequiresPermission( allOf = {
2260 Manifest.permission.INTERACT_ACROSS_USERS_FULL,
2261 Manifest.permission.MANAGE_USERS
2262 })
Jeff Sharkey8588bc12016-01-06 16:47:42 -07002263 public @Nullable String getUserAccount(@UserIdInt int userHandle) {
Xiaohui Chenb3b92582015-12-07 11:22:13 -08002264 try {
2265 return mService.getUserAccount(userHandle);
2266 } catch (RemoteException re) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07002267 throw re.rethrowFromSystemServer();
Xiaohui Chenb3b92582015-12-07 11:22:13 -08002268 }
2269 }
2270
2271 /**
2272 * Set account name for the given user.
2273 * @hide
2274 */
2275 @RequiresPermission( allOf = {
2276 Manifest.permission.INTERACT_ACROSS_USERS_FULL,
2277 Manifest.permission.MANAGE_USERS
2278 })
Jeff Sharkey8588bc12016-01-06 16:47:42 -07002279 public void setUserAccount(@UserIdInt int userHandle, @Nullable String accountName) {
Xiaohui Chenb3b92582015-12-07 11:22:13 -08002280 try {
2281 mService.setUserAccount(userHandle, accountName);
2282 } catch (RemoteException re) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07002283 throw re.rethrowFromSystemServer();
Xiaohui Chenb3b92582015-12-07 11:22:13 -08002284 }
2285 }
2286
2287 /**
Xiaohui Chen70f6c382015-04-28 14:21:43 -07002288 * Returns information for Primary user.
2289 * Requires {@link android.Manifest.permission#MANAGE_USERS} permission.
2290 *
2291 * @return the Primary user, null if not found.
2292 * @hide
2293 */
Xiaohui Chen7cb69df2015-07-13 16:01:01 -07002294 public @Nullable UserInfo getPrimaryUser() {
Xiaohui Chen70f6c382015-04-28 14:21:43 -07002295 try {
2296 return mService.getPrimaryUser();
2297 } catch (RemoteException re) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07002298 throw re.rethrowFromSystemServer();
Xiaohui Chen70f6c382015-04-28 14:21:43 -07002299 }
2300 }
2301
2302 /**
Amith Yamasani95ab7842014-08-11 17:09:26 -07002303 * Checks whether it's possible to add more users. Caller must hold the MANAGE_USERS
2304 * permission.
2305 *
2306 * @return true if more users can be added, false if limit has been reached.
2307 * @hide
2308 */
2309 public boolean canAddMoreUsers() {
2310 final List<UserInfo> users = getUsers(true);
2311 final int totalUserCount = users.size();
2312 int aliveUserCount = 0;
2313 for (int i = 0; i < totalUserCount; i++) {
2314 UserInfo user = users.get(i);
2315 if (!user.isGuest()) {
2316 aliveUserCount++;
2317 }
2318 }
2319 return aliveUserCount < getMaxSupportedUsers();
2320 }
2321
2322 /**
Nicolas Prevot72434b72015-05-13 12:15:03 -07002323 * Checks whether it's possible to add more managed profiles. Caller must hold the MANAGE_USERS
2324 * permission.
Nicolas Prevot07387fe2015-10-30 17:53:30 +00002325 * if allowedToRemoveOne is true and if the user already has a managed profile, then return if
2326 * we could add a new managed profile to this user after removing the existing one.
Nicolas Prevot72434b72015-05-13 12:15:03 -07002327 *
2328 * @return true if more managed profiles can be added, false if limit has been reached.
2329 * @hide
2330 */
Jeff Sharkey8588bc12016-01-06 16:47:42 -07002331 public boolean canAddMoreManagedProfiles(@UserIdInt int userId, boolean allowedToRemoveOne) {
Nicolas Prevot72434b72015-05-13 12:15:03 -07002332 try {
Nicolas Prevot07387fe2015-10-30 17:53:30 +00002333 return mService.canAddMoreManagedProfiles(userId, allowedToRemoveOne);
Nicolas Prevot72434b72015-05-13 12:15:03 -07002334 } catch (RemoteException re) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07002335 throw re.rethrowFromSystemServer();
Nicolas Prevot72434b72015-05-13 12:15:03 -07002336 }
2337 }
2338
2339 /**
Kenny Guy2a764942014-04-02 13:29:20 +01002340 * Returns list of the profiles of userHandle including
2341 * userHandle itself.
Amith Yamasani4f7e2e32014-08-14 18:49:48 -07002342 * Note that this returns both enabled and not enabled profiles. See
Ruben Brunk7f75da22015-04-30 17:46:30 -07002343 * {@link #getEnabledProfiles(int)} if you need only the enabled ones.
Amith Yamasani4f582632014-02-19 14:31:52 -08002344 *
Kenny Guy2a764942014-04-02 13:29:20 +01002345 * Requires {@link android.Manifest.permission#MANAGE_USERS} permission.
2346 * @param userHandle profiles of this user will be returned.
2347 * @return the list of profiles.
2348 * @hide
2349 */
Jeff Sharkey8588bc12016-01-06 16:47:42 -07002350 public List<UserInfo> getProfiles(@UserIdInt int userHandle) {
Kenny Guya52dc3e2014-02-11 15:33:14 +00002351 try {
Alexandra Gherghina385124d2014-04-03 13:37:39 +01002352 return mService.getProfiles(userHandle, false /* enabledOnly */);
Kenny Guya52dc3e2014-02-11 15:33:14 +00002353 } catch (RemoteException re) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07002354 throw re.rethrowFromSystemServer();
Kenny Guya52dc3e2014-02-11 15:33:14 +00002355 }
2356 }
2357
2358 /**
Xiaohui Chenfd5b7742015-10-14 15:47:04 -07002359 * Requires {@link android.Manifest.permission#MANAGE_USERS} permission.
2360 * @param userId one of the two user ids to check.
2361 * @param otherUserId one of the two user ids to check.
2362 * @return true if the two user ids are in the same profile group.
2363 * @hide
2364 */
Jeff Sharkey8588bc12016-01-06 16:47:42 -07002365 public boolean isSameProfileGroup(@UserIdInt int userId, int otherUserId) {
Xiaohui Chenfd5b7742015-10-14 15:47:04 -07002366 try {
2367 return mService.isSameProfileGroup(userId, otherUserId);
2368 } catch (RemoteException re) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07002369 throw re.rethrowFromSystemServer();
Xiaohui Chenfd5b7742015-10-14 15:47:04 -07002370 }
2371 }
2372
2373 /**
Ruben Brunk7f75da22015-04-30 17:46:30 -07002374 * Returns list of the profiles of userHandle including
2375 * userHandle itself.
2376 * Note that this returns only enabled.
2377 *
2378 * Requires {@link android.Manifest.permission#MANAGE_USERS} permission.
2379 * @param userHandle profiles of this user will be returned.
2380 * @return the list of profiles.
2381 * @hide
2382 */
Jeff Sharkey8588bc12016-01-06 16:47:42 -07002383 public List<UserInfo> getEnabledProfiles(@UserIdInt int userHandle) {
Ruben Brunk7f75da22015-04-30 17:46:30 -07002384 try {
2385 return mService.getProfiles(userHandle, true /* enabledOnly */);
2386 } catch (RemoteException re) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07002387 throw re.rethrowFromSystemServer();
Ruben Brunk7f75da22015-04-30 17:46:30 -07002388 }
2389 }
2390
2391 /**
Jessica Hummelbe81c802014-04-22 15:49:22 +01002392 * Returns a list of UserHandles for profiles associated with the user that the calling process
2393 * is running on, including the user itself.
Amith Yamasani4f582632014-02-19 14:31:52 -08002394 *
2395 * @return A non-empty list of UserHandles associated with the calling user.
2396 */
2397 public List<UserHandle> getUserProfiles() {
Fyodor Kupolov7f98aa42016-04-07 14:56:25 -07002398 int[] userIds = getProfileIds(UserHandle.myUserId(), true /* enabledOnly */);
2399 List<UserHandle> result = new ArrayList<>(userIds.length);
2400 for (int userId : userIds) {
2401 result.add(UserHandle.of(userId));
2402 }
2403 return result;
2404 }
2405
2406 /**
2407 * Returns a list of ids for profiles associated with the specified user including the user
2408 * itself.
2409 *
2410 * @param userId id of the user to return profiles for
2411 * @param enabledOnly whether return only {@link UserInfo#isEnabled() enabled} profiles
2412 * @return A non-empty list of ids of profiles associated with the specified user.
2413 *
2414 * @hide
2415 */
Philip P. Moltmann4e615e62018-08-28 14:57:49 -07002416 @RequiresPermission(anyOf = {Manifest.permission.MANAGE_USERS,
2417 Manifest.permission.CREATE_USERS}, conditional = true)
Philip P. Moltmann4e615e62018-08-28 14:57:49 -07002418 public @NonNull int[] getProfileIds(@UserIdInt int userId, boolean enabledOnly) {
Alexandra Gherghina385124d2014-04-03 13:37:39 +01002419 try {
Fyodor Kupolov7f98aa42016-04-07 14:56:25 -07002420 return mService.getProfileIds(userId, enabledOnly);
Alexandra Gherghina385124d2014-04-03 13:37:39 +01002421 } catch (RemoteException re) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07002422 throw re.rethrowFromSystemServer();
Alexandra Gherghina385124d2014-04-03 13:37:39 +01002423 }
Fyodor Kupolov7f98aa42016-04-07 14:56:25 -07002424 }
2425
2426 /**
2427 * @see #getProfileIds(int, boolean)
2428 * @hide
2429 */
2430 public int[] getProfileIdsWithDisabled(@UserIdInt int userId) {
2431 return getProfileIds(userId, false /* enabledOnly */);
2432 }
2433
2434 /**
2435 * @see #getProfileIds(int, boolean)
2436 * @hide
2437 */
2438 public int[] getEnabledProfileIds(@UserIdInt int userId) {
2439 return getProfileIds(userId, true /* enabledOnly */);
Amith Yamasani4f582632014-02-19 14:31:52 -08002440 }
2441
Amith Yamasani7dda2652014-04-11 14:57:12 -07002442 /**
Andres Moralesc5548c02015-08-05 10:23:12 -07002443 * Returns the device credential owner id of the profile from
2444 * which this method is called, or userHandle if called from a user that
2445 * is not a profile.
2446 *
2447 * @hide
2448 */
Jeff Sharkey8588bc12016-01-06 16:47:42 -07002449 public int getCredentialOwnerProfile(@UserIdInt int userHandle) {
Andres Moralesc5548c02015-08-05 10:23:12 -07002450 try {
2451 return mService.getCredentialOwnerProfile(userHandle);
2452 } catch (RemoteException re) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07002453 throw re.rethrowFromSystemServer();
Andres Moralesc5548c02015-08-05 10:23:12 -07002454 }
2455 }
2456
2457 /**
Jessica Hummelbe81c802014-04-22 15:49:22 +01002458 * Returns the parent of the profile which this method is called from
2459 * or null if called from a user that is not a profile.
2460 *
2461 * @hide
2462 */
Jeff Sharkey8588bc12016-01-06 16:47:42 -07002463 public UserInfo getProfileParent(@UserIdInt int userHandle) {
Jessica Hummelbe81c802014-04-22 15:49:22 +01002464 try {
2465 return mService.getProfileParent(userHandle);
2466 } catch (RemoteException re) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07002467 throw re.rethrowFromSystemServer();
Jessica Hummelbe81c802014-04-22 15:49:22 +01002468 }
2469 }
2470
2471 /**
Philip P. Moltmanna3e358c2018-11-21 12:45:50 -08002472 * Get the parent of a user profile.
2473 *
2474 * @param user the handle of the user profile
2475 *
2476 * @return the parent of the user or {@code null} if the user is not profile
2477 *
2478 * @hide
2479 */
2480 @SystemApi
2481 @RequiresPermission(android.Manifest.permission.MANAGE_USERS)
2482 public @Nullable UserHandle getProfileParent(@NonNull UserHandle user) {
2483 UserInfo info = getProfileParent(user.getIdentifier());
2484
2485 if (info == null) {
2486 return null;
2487 }
2488
2489 return UserHandle.of(info.id);
2490 }
2491
2492 /**
Tony Make3d1f652017-12-12 11:00:37 +00002493 * Enables or disables quiet mode for a managed profile. If quiet mode is enabled, apps in a
2494 * managed profile don't run, generate notifications, or consume data or battery.
2495 * <p>
2496 * If a user's credential is needed to turn off quiet mode, a confirm credential screen will be
2497 * shown to the user.
2498 * <p>
2499 * The change may not happen instantly, however apps can listen for
2500 * {@link Intent#ACTION_MANAGED_PROFILE_AVAILABLE} and
2501 * {@link Intent#ACTION_MANAGED_PROFILE_UNAVAILABLE} broadcasts in order to be notified of
2502 * the change of the quiet mode. Apps can also check the current state of quiet mode by
2503 * calling {@link #isQuietModeEnabled(UserHandle)}.
2504 * <p>
2505 * The caller must either be the foreground default launcher or have one of these permissions:
2506 * {@code MANAGE_USERS} or {@code MODIFY_QUIET_MODE}.
Rubin Xu0a29ecd2015-11-04 15:11:48 +00002507 *
Tony Make3d1f652017-12-12 11:00:37 +00002508 * @param enableQuietMode whether quiet mode should be enabled or disabled
2509 * @param userHandle user handle of the profile
2510 * @return {@code false} if user's credential is needed in order to turn off quiet mode,
2511 * {@code true} otherwise
2512 * @throws SecurityException if the caller is invalid
2513 * @throws IllegalArgumentException if {@code userHandle} is not a managed profile
2514 *
2515 * @see #isQuietModeEnabled(UserHandle)
Rubin Xu0a29ecd2015-11-04 15:11:48 +00002516 */
Tony Makbece85d2018-01-12 12:10:17 +00002517 public boolean requestQuietModeEnabled(boolean enableQuietMode, @NonNull UserHandle userHandle) {
2518 return requestQuietModeEnabled(enableQuietMode, userHandle, null);
Tony Makb7e6fd42017-12-05 19:40:28 +00002519 }
2520
2521 /**
Tony Makbece85d2018-01-12 12:10:17 +00002522 * Similar to {@link #requestQuietModeEnabled(boolean, UserHandle)}, except you can specify
Tony Makd390ae92017-12-28 13:23:10 +00002523 * a target to start when user is unlocked. If {@code target} is specified, caller must have
2524 * the {@link android.Manifest.permission#MANAGE_USERS} permission.
Tony Makb7e6fd42017-12-05 19:40:28 +00002525 *
Tony Makbece85d2018-01-12 12:10:17 +00002526 * @see {@link #requestQuietModeEnabled(boolean, UserHandle)}
Rubin Xu0a29ecd2015-11-04 15:11:48 +00002527 * @hide
2528 */
Tony Makbece85d2018-01-12 12:10:17 +00002529 public boolean requestQuietModeEnabled(
Tony Makb7e6fd42017-12-05 19:40:28 +00002530 boolean enableQuietMode, @NonNull UserHandle userHandle, IntentSender target) {
Rubin Xu0a29ecd2015-11-04 15:11:48 +00002531 try {
Tony Makbece85d2018-01-12 12:10:17 +00002532 return mService.requestQuietModeEnabled(
Tony Make3d1f652017-12-12 11:00:37 +00002533 mContext.getPackageName(), enableQuietMode, userHandle.getIdentifier(), target);
Jeff Sharkey27b2e692016-02-25 17:40:12 -07002534 } catch (RemoteException re) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07002535 throw re.rethrowFromSystemServer();
Rubin Xu0a29ecd2015-11-04 15:11:48 +00002536 }
2537 }
2538
2539 /**
2540 * Returns whether the given profile is in quiet mode or not.
Ricky Wai7881cf82016-04-15 17:20:12 +01002541 * Notes: Quiet mode is only supported for managed profiles.
Rubin Xu0a29ecd2015-11-04 15:11:48 +00002542 *
2543 * @param userHandle The user handle of the profile to be queried.
2544 * @return true if the profile is in quiet mode, false otherwise.
2545 */
2546 public boolean isQuietModeEnabled(UserHandle userHandle) {
2547 try {
2548 return mService.isQuietModeEnabled(userHandle.getIdentifier());
Jeff Sharkey27b2e692016-02-25 17:40:12 -07002549 } catch (RemoteException re) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07002550 throw re.rethrowFromSystemServer();
Rubin Xu0a29ecd2015-11-04 15:11:48 +00002551 }
Rubin Xu0a29ecd2015-11-04 15:11:48 +00002552 }
2553
2554 /**
Amith Yamasani7dda2652014-04-11 14:57:12 -07002555 * If the target user is a managed profile of the calling user or the caller
2556 * is itself a managed profile, then this returns a badged copy of the given
Svetoslavc71c42f2014-08-05 18:57:05 -07002557 * icon to be able to distinguish it from the original icon. For badging an
2558 * arbitrary drawable use {@link #getBadgedDrawableForUser(
2559 * android.graphics.drawable.Drawable, UserHandle, android.graphics.Rect, int)}.
2560 * <p>
2561 * If the original drawable is a BitmapDrawable and the backing bitmap is
Vadim Tryshev66ae66a2016-02-18 15:41:21 -08002562 * mutable as per {@link android.graphics.Bitmap#isMutable()}, the badging
Svetoslavc71c42f2014-08-05 18:57:05 -07002563 * is performed in place and the original drawable is returned.
2564 * </p>
Amith Yamasani7dda2652014-04-11 14:57:12 -07002565 *
2566 * @param icon The icon to badge.
2567 * @param user The target user.
2568 * @return A drawable that combines the original icon and a badge as
2569 * determined by the system.
Svetoslavc7d62f02014-09-04 15:39:54 -07002570 * @removed
Amith Yamasani7dda2652014-04-11 14:57:12 -07002571 */
Svetoslavc71c42f2014-08-05 18:57:05 -07002572 public Drawable getBadgedIconForUser(Drawable icon, UserHandle user) {
Svetoslavc7d62f02014-09-04 15:39:54 -07002573 return mContext.getPackageManager().getUserBadgedIcon(icon, user);
Amith Yamasani4f582632014-02-19 14:31:52 -08002574 }
2575
Kenny Guy701ea7c2014-05-08 23:34:12 +01002576 /**
2577 * If the target user is a managed profile of the calling user or the caller
Svetoslavc71c42f2014-08-05 18:57:05 -07002578 * is itself a managed profile, then this returns a badged copy of the given
2579 * drawable allowing the user to distinguish it from the original drawable.
2580 * The caller can specify the location in the bounds of the drawable to be
2581 * badged where the badge should be applied as well as the density of the
2582 * badge to be used.
2583 * <p>
2584 * If the original drawable is a BitmapDrawable and the backing bitmap is
Vadim Tryshev66ae66a2016-02-18 15:41:21 -08002585 * mutable as per {@link android.graphics.Bitmap#isMutable()}, the badging
Svetoslavc71c42f2014-08-05 18:57:05 -07002586 * is performed in place and the original drawable is returned.
2587 * </p>
2588 *
2589 * @param badgedDrawable The drawable to badge.
2590 * @param user The target user.
2591 * @param badgeLocation Where in the bounds of the badged drawable to place
Vadim Tryshev66ae66a2016-02-18 15:41:21 -08002592 * the badge. If it's {@code null}, the badge is applied on top of the entire
Svetoslavc71c42f2014-08-05 18:57:05 -07002593 * drawable being badged.
2594 * @param badgeDensity The optional desired density for the badge as per
Vadim Tryshev66ae66a2016-02-18 15:41:21 -08002595 * {@link android.util.DisplayMetrics#densityDpi}. If it's not positive,
Svetoslavc71c42f2014-08-05 18:57:05 -07002596 * the density of the display is used.
2597 * @return A drawable that combines the original drawable and a badge as
2598 * determined by the system.
Svetoslavc7d62f02014-09-04 15:39:54 -07002599 * @removed
Svetoslavc71c42f2014-08-05 18:57:05 -07002600 */
2601 public Drawable getBadgedDrawableForUser(Drawable badgedDrawable, UserHandle user,
2602 Rect badgeLocation, int badgeDensity) {
Svetoslavc7d62f02014-09-04 15:39:54 -07002603 return mContext.getPackageManager().getUserBadgedDrawableForDensity(badgedDrawable, user,
2604 badgeLocation, badgeDensity);
Svetoslavc71c42f2014-08-05 18:57:05 -07002605 }
2606
2607 /**
2608 * If the target user is a managed profile of the calling user or the caller
Kenny Guyf7ecf7c2014-06-18 11:32:05 +01002609 * is itself a managed profile, then this returns a copy of the label with
2610 * badging for accessibility services like talkback. E.g. passing in "Email"
2611 * and it might return "Work Email" for Email in the work profile.
2612 *
2613 * @param label The label to change.
2614 * @param user The target user.
2615 * @return A label that combines the original label and a badge as
2616 * determined by the system.
Svetoslavc7d62f02014-09-04 15:39:54 -07002617 * @removed
Kenny Guyf7ecf7c2014-06-18 11:32:05 +01002618 */
Kenny Guy237aecd2014-07-21 14:06:09 +01002619 public CharSequence getBadgedLabelForUser(CharSequence label, UserHandle user) {
Svetoslavc7d62f02014-09-04 15:39:54 -07002620 return mContext.getPackageManager().getUserBadgedLabel(label, user);
Amith Yamasani4f582632014-02-19 14:31:52 -08002621 }
2622
2623 /**
2624 * Returns information for all users on this device. Requires
2625 * {@link android.Manifest.permission#MANAGE_USERS} permission.
Emily Bernier394a6cd2014-05-07 12:49:20 -04002626 *
Amith Yamasani4f582632014-02-19 14:31:52 -08002627 * @param excludeDying specify if the list should exclude users being
2628 * removed.
Amith Yamasani920ace02012-09-20 22:15:37 -07002629 * @return the list of users that were created.
2630 * @hide
2631 */
Adam Lesinskiada8deb2017-05-12 13:50:42 -07002632 public @NonNull List<UserInfo> getUsers(boolean excludeDying) {
Amith Yamasani920ace02012-09-20 22:15:37 -07002633 try {
2634 return mService.getUsers(excludeDying);
Amith Yamasani258848d2012-08-10 17:06:33 -07002635 } catch (RemoteException re) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07002636 throw re.rethrowFromSystemServer();
Amith Yamasani258848d2012-08-10 17:06:33 -07002637 }
2638 }
2639
2640 /**
2641 * Removes a user and all associated data.
Amith Yamasani195263742012-08-21 15:40:12 -07002642 * Requires {@link android.Manifest.permission#MANAGE_USERS} permission.
Amith Yamasani258848d2012-08-10 17:06:33 -07002643 * @param userHandle the integer handle of the user, where 0 is the primary user.
2644 * @hide
2645 */
Jeff Sharkey8588bc12016-01-06 16:47:42 -07002646 public boolean removeUser(@UserIdInt int userHandle) {
Amith Yamasani258848d2012-08-10 17:06:33 -07002647 try {
2648 return mService.removeUser(userHandle);
2649 } catch (RemoteException re) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07002650 throw re.rethrowFromSystemServer();
Amith Yamasani258848d2012-08-10 17:06:33 -07002651 }
2652 }
2653
2654 /**
Varun Shahe38c6782018-12-04 16:57:49 -08002655 * Removes a user and all associated data.
2656 *
2657 * @param user the user that needs to be removed.
2658 * @hide
2659 */
2660 @SystemApi
2661 @RequiresPermission(android.Manifest.permission.MANAGE_USERS)
2662 public boolean removeUser(UserHandle user) {
2663 return removeUser(user.getIdentifier());
2664 }
2665
2666
2667 /**
Nicolas Prevotd37c4a92017-01-23 11:56:00 +00002668 * Similar to {@link #removeUser(int)} except bypassing the checking of
2669 * {@link UserManager#DISALLOW_REMOVE_USER}
2670 * or {@link UserManager#DISALLOW_REMOVE_MANAGED_PROFILE}.
2671 *
2672 * @see {@link #removeUser(int)}
2673 * @hide
2674 */
2675 public boolean removeUserEvenWhenDisallowed(@UserIdInt int userHandle) {
2676 try {
2677 return mService.removeUserEvenWhenDisallowed(userHandle);
2678 } catch (RemoteException re) {
2679 throw re.rethrowFromSystemServer();
2680 }
2681 }
2682
2683 /**
Amith Yamasani258848d2012-08-10 17:06:33 -07002684 * Updates the user's name.
Amith Yamasani195263742012-08-21 15:40:12 -07002685 * Requires {@link android.Manifest.permission#MANAGE_USERS} permission.
Amith Yamasani258848d2012-08-10 17:06:33 -07002686 *
2687 * @param userHandle the user's integer handle
2688 * @param name the new name for the user
2689 * @hide
2690 */
Jeff Sharkey8588bc12016-01-06 16:47:42 -07002691 public void setUserName(@UserIdInt int userHandle, String name) {
Amith Yamasani258848d2012-08-10 17:06:33 -07002692 try {
2693 mService.setUserName(userHandle, name);
2694 } catch (RemoteException re) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07002695 throw re.rethrowFromSystemServer();
Amith Yamasani258848d2012-08-10 17:06:33 -07002696 }
2697 }
2698
2699 /**
Leo Hsuab28fff2019-01-30 15:40:56 +08002700 * Updates the calling user's name.
2701 * Requires {@link android.Manifest.permission#MANAGE_USERS} permission.
2702 *
2703 * @param name the new name for the user
2704 * @hide
2705 */
2706 @SystemApi
2707 @RequiresPermission(android.Manifest.permission.MANAGE_USERS)
Leo Hsu51839332019-03-05 16:59:04 +08002708 public void setUserName(@Nullable String name) {
Leo Hsuab28fff2019-01-30 15:40:56 +08002709 setUserName(getUserHandle(), name);
2710 }
2711
2712 /**
Amith Yamasanie928d7d2012-09-17 21:46:51 -07002713 * Sets the user's photo.
Amith Yamasani258848d2012-08-10 17:06:33 -07002714 * @param userHandle the user for whom to change the photo.
Amith Yamasanie928d7d2012-09-17 21:46:51 -07002715 * @param icon the bitmap to set as the photo.
Amith Yamasani258848d2012-08-10 17:06:33 -07002716 * @hide
2717 */
Jeff Sharkey8588bc12016-01-06 16:47:42 -07002718 public void setUserIcon(@UserIdInt int userHandle, Bitmap icon) {
Amith Yamasani258848d2012-08-10 17:06:33 -07002719 try {
Amith Yamasanie928d7d2012-09-17 21:46:51 -07002720 mService.setUserIcon(userHandle, icon);
Amith Yamasani258848d2012-08-10 17:06:33 -07002721 } catch (RemoteException re) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07002722 throw re.rethrowFromSystemServer();
Amith Yamasani258848d2012-08-10 17:06:33 -07002723 }
2724 }
2725
2726 /**
Leo Hsuab28fff2019-01-30 15:40:56 +08002727 * Sets the calling user's photo.
2728 * Requires {@link android.Manifest.permission#MANAGE_USERS} permission.
2729 *
2730 * @param icon the bitmap to set as the photo.
2731 * @hide
2732 */
2733 @SystemApi
2734 @RequiresPermission(android.Manifest.permission.MANAGE_USERS)
Leo Hsu51839332019-03-05 16:59:04 +08002735 public void setUserIcon(@NonNull Bitmap icon) {
Leo Hsuab28fff2019-01-30 15:40:56 +08002736 setUserIcon(getUserHandle(), icon);
2737 }
2738
2739 /**
Amith Yamasani3b49f072012-09-17 10:21:43 -07002740 * Returns a file descriptor for the user's photo. PNG data can be read from this file.
2741 * @param userHandle the user whose photo we want to read.
Amith Yamasanie928d7d2012-09-17 21:46:51 -07002742 * @return a {@link Bitmap} of the user's photo, or null if there's no photo.
Alexandra Gherghina64d4dca2014-08-28 18:26:56 +01002743 * @see com.android.internal.util.UserIcons#getDefaultUserIcon for a default.
Amith Yamasani3b49f072012-09-17 10:21:43 -07002744 * @hide
2745 */
Jeff Sharkey8588bc12016-01-06 16:47:42 -07002746 public Bitmap getUserIcon(@UserIdInt int userHandle) {
Amith Yamasani3b49f072012-09-17 10:21:43 -07002747 try {
Adrian Roos1bdff912015-02-17 15:51:35 +01002748 ParcelFileDescriptor fd = mService.getUserIcon(userHandle);
2749 if (fd != null) {
2750 try {
2751 return BitmapFactory.decodeFileDescriptor(fd.getFileDescriptor());
2752 } finally {
2753 try {
2754 fd.close();
2755 } catch (IOException e) {
2756 }
2757 }
2758 }
Amith Yamasani3b49f072012-09-17 10:21:43 -07002759 } catch (RemoteException re) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07002760 throw re.rethrowFromSystemServer();
Amith Yamasani3b49f072012-09-17 10:21:43 -07002761 }
Adrian Roos1bdff912015-02-17 15:51:35 +01002762 return null;
Amith Yamasani3b49f072012-09-17 10:21:43 -07002763 }
2764
2765 /**
Leo Hsuab28fff2019-01-30 15:40:56 +08002766 * Returns a Bitmap for the calling user's photo.
2767 * Requires {@link android.Manifest.permission#MANAGE_USERS} permission.
2768 *
2769 * @return a {@link Bitmap} of the user's photo, or null if there's no photo.
2770 * @see com.android.internal.util.UserIcons#getDefaultUserIcon for a default.
2771 * @hide
2772 */
2773 @SystemApi
2774 @RequiresPermission(android.Manifest.permission.MANAGE_USERS)
Leo Hsu51839332019-03-05 16:59:04 +08002775 public @Nullable Bitmap getUserIcon() {
Leo Hsuab28fff2019-01-30 15:40:56 +08002776 return getUserIcon(getUserHandle());
2777 }
2778
2779 /**
Amith Yamasani258848d2012-08-10 17:06:33 -07002780 * Returns the maximum number of users that can be created on this device. A return value
2781 * of 1 means that it is a single user device.
2782 * @hide
Maggie Benthalla12fccf2013-03-14 18:02:12 -04002783 * @return a value greater than or equal to 1
Amith Yamasani258848d2012-08-10 17:06:33 -07002784 */
Jeff Sharkey27bd34d2012-09-16 12:49:00 -07002785 public static int getMaxSupportedUsers() {
Amith Yamasaniff549202012-10-12 12:44:49 -07002786 // Don't allow multiple users on certain builds
2787 if (android.os.Build.ID.startsWith("JVP")) return 1;
Robin Lee83c79312018-02-09 19:27:18 +01002788 if (ActivityManager.isLowRamDeviceStatic()) {
2789 // Low-ram devices are Svelte. Most of the time they don't get multi-user.
2790 if ((Resources.getSystem().getConfiguration().uiMode & Configuration.UI_MODE_TYPE_MASK)
2791 != Configuration.UI_MODE_TYPE_TELEVISION) {
2792 return 1;
2793 }
2794 }
Jeff Sharkey27bd34d2012-09-16 12:49:00 -07002795 return SystemProperties.getInt("fw.max_users",
2796 Resources.getSystem().getInteger(R.integer.config_multiuserMaximumUsers));
Amith Yamasani258848d2012-08-10 17:06:33 -07002797 }
Amith Yamasani2a003292012-08-14 18:25:45 -07002798
2799 /**
Fyodor Kupolovcd86ebf2015-09-29 17:06:50 -07002800 * Returns true if the user switcher should be shown, this will be if device supports multi-user
2801 * and there are at least 2 users available that are not managed profiles.
Kenny Guy1a447532014-02-20 21:55:32 +00002802 * @hide
2803 * @return true if user switcher should be shown.
2804 */
2805 public boolean isUserSwitcherEnabled() {
Fyodor Kupolovcd86ebf2015-09-29 17:06:50 -07002806 if (!supportsMultipleUsers()) {
2807 return false;
2808 }
Benjamin Franzff66fa92017-08-10 10:39:44 +01002809 if (hasUserRestriction(DISALLOW_USER_SWITCH)) {
2810 return false;
2811 }
Amith Yamasanieb437d42016-04-29 09:31:25 -07002812 // If Demo Mode is on, don't show user switcher
2813 if (isDeviceInDemoMode(mContext)) {
2814 return false;
2815 }
Fan Zhangc69c8b62018-06-05 13:30:11 -07002816 // If user disabled this feature, don't show switcher
2817 final boolean userSwitcherEnabled = Settings.Global.getInt(mContext.getContentResolver(),
2818 Settings.Global.USER_SWITCHER_ENABLED, 1) != 0;
2819 if (!userSwitcherEnabled) {
2820 return false;
2821 }
Kenny Guy1a447532014-02-20 21:55:32 +00002822 List<UserInfo> users = getUsers(true);
2823 if (users == null) {
2824 return false;
2825 }
2826 int switchableUserCount = 0;
2827 for (UserInfo user : users) {
Xiaohui Chen7cb69df2015-07-13 16:01:01 -07002828 if (user.supportsSwitchToByUser()) {
Kenny Guy1a447532014-02-20 21:55:32 +00002829 ++switchableUserCount;
2830 }
2831 }
Fyodor Kupolovcd86ebf2015-09-29 17:06:50 -07002832 final boolean guestEnabled = !mContext.getSystemService(DevicePolicyManager.class)
2833 .getGuestUserDisabled(null);
Amith Yamasania596ff82014-06-12 18:12:38 -07002834 return switchableUserCount > 1 || guestEnabled;
Kenny Guy1a447532014-02-20 21:55:32 +00002835 }
2836
2837 /**
Amith Yamasanieb437d42016-04-29 09:31:25 -07002838 * @hide
2839 */
2840 public static boolean isDeviceInDemoMode(Context context) {
2841 return Settings.Global.getInt(context.getContentResolver(),
2842 Settings.Global.DEVICE_DEMO_MODE, 0) > 0;
2843 }
2844
2845 /**
Amith Yamasani2a003292012-08-14 18:25:45 -07002846 * Returns a serial number on this device for a given userHandle. User handles can be recycled
2847 * when deleting and creating users, but serial numbers are not reused until the device is wiped.
2848 * @param userHandle
2849 * @return a serial number associated with that user, or -1 if the userHandle is not valid.
2850 * @hide
2851 */
Jeff Sharkey8588bc12016-01-06 16:47:42 -07002852 public int getUserSerialNumber(@UserIdInt int userHandle) {
Amith Yamasani2a003292012-08-14 18:25:45 -07002853 try {
2854 return mService.getUserSerialNumber(userHandle);
2855 } catch (RemoteException re) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07002856 throw re.rethrowFromSystemServer();
Amith Yamasani2a003292012-08-14 18:25:45 -07002857 }
Amith Yamasani2a003292012-08-14 18:25:45 -07002858 }
2859
2860 /**
2861 * Returns a userHandle on this device for a given user serial number. User handles can be
2862 * recycled when deleting and creating users, but serial numbers are not reused until the device
2863 * is wiped.
2864 * @param userSerialNumber
2865 * @return the userHandle associated with that user serial number, or -1 if the serial number
2866 * is not valid.
2867 * @hide
2868 */
Jeff Sharkey8588bc12016-01-06 16:47:42 -07002869 public @UserIdInt int getUserHandle(int userSerialNumber) {
Amith Yamasani2a003292012-08-14 18:25:45 -07002870 try {
2871 return mService.getUserHandle(userSerialNumber);
2872 } catch (RemoteException re) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07002873 throw re.rethrowFromSystemServer();
Amith Yamasani2a003292012-08-14 18:25:45 -07002874 }
Amith Yamasani2a003292012-08-14 18:25:45 -07002875 }
Maggie Benthall67944582013-02-22 14:58:27 -05002876
Amith Yamasani7e99bc02013-04-16 18:24:51 -07002877 /**
Esteban Talavera953fe482016-06-07 15:25:20 +01002878 * Returns a {@link Bundle} containing any saved application restrictions for this user, for the
Amith Yamasani7e99bc02013-04-16 18:24:51 -07002879 * given package name. Only an application with this package name can call this method.
Esteban Talavera5b9f1672015-12-11 15:22:34 +00002880 *
2881 * <p>The returned {@link Bundle} consists of key-value pairs, as defined by the application,
2882 * where the types of values may be:
2883 * <ul>
2884 * <li>{@code boolean}
2885 * <li>{@code int}
2886 * <li>{@code String} or {@code String[]}
2887 * <li>From {@link android.os.Build.VERSION_CODES#M}, {@code Bundle} or {@code Bundle[]}
2888 * </ul>
2889 *
Fyodor Kupolov4e9af062016-07-18 16:59:11 -07002890 * <p>NOTE: The method performs disk I/O and shouldn't be called on the main thread
2891 *
Amith Yamasani7e99bc02013-04-16 18:24:51 -07002892 * @param packageName the package name of the calling application
Esteban Talavera953fe482016-06-07 15:25:20 +01002893 * @return a {@link Bundle} with the restrictions for that package, or an empty {@link Bundle}
2894 * if there are no saved restrictions.
Esteban Talavera5b9f1672015-12-11 15:22:34 +00002895 *
2896 * @see #KEY_RESTRICTIONS_PENDING
Amith Yamasani7e99bc02013-04-16 18:24:51 -07002897 */
Fyodor Kupolov4e9af062016-07-18 16:59:11 -07002898 @WorkerThread
Amith Yamasani7e99bc02013-04-16 18:24:51 -07002899 public Bundle getApplicationRestrictions(String packageName) {
2900 try {
2901 return mService.getApplicationRestrictions(packageName);
2902 } catch (RemoteException re) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07002903 throw re.rethrowFromSystemServer();
Amith Yamasani7e99bc02013-04-16 18:24:51 -07002904 }
Amith Yamasani7e99bc02013-04-16 18:24:51 -07002905 }
Amith Yamasanidf2e92a2013-03-01 17:04:38 -08002906
2907 /**
2908 * @hide
2909 */
Fyodor Kupolov4e9af062016-07-18 16:59:11 -07002910 @WorkerThread
Amith Yamasani7e99bc02013-04-16 18:24:51 -07002911 public Bundle getApplicationRestrictions(String packageName, UserHandle user) {
Amith Yamasanidf2e92a2013-03-01 17:04:38 -08002912 try {
Amith Yamasani7e99bc02013-04-16 18:24:51 -07002913 return mService.getApplicationRestrictionsForUser(packageName, user.getIdentifier());
Amith Yamasanidf2e92a2013-03-01 17:04:38 -08002914 } catch (RemoteException re) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07002915 throw re.rethrowFromSystemServer();
Amith Yamasanidf2e92a2013-03-01 17:04:38 -08002916 }
Amith Yamasanidf2e92a2013-03-01 17:04:38 -08002917 }
2918
2919 /**
2920 * @hide
2921 */
Fyodor Kupolov4e9af062016-07-18 16:59:11 -07002922 @WorkerThread
Amith Yamasani7e99bc02013-04-16 18:24:51 -07002923 public void setApplicationRestrictions(String packageName, Bundle restrictions,
Amith Yamasanidf2e92a2013-03-01 17:04:38 -08002924 UserHandle user) {
2925 try {
Amith Yamasani7e99bc02013-04-16 18:24:51 -07002926 mService.setApplicationRestrictions(packageName, restrictions, user.getIdentifier());
Amith Yamasanidf2e92a2013-03-01 17:04:38 -08002927 } catch (RemoteException re) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07002928 throw re.rethrowFromSystemServer();
Amith Yamasanidf2e92a2013-03-01 17:04:38 -08002929 }
2930 }
Amith Yamasani655d0e22013-06-12 14:19:10 -07002931
2932 /**
Amith Yamasanid304af62013-09-05 09:30:23 -07002933 * Sets a new challenge PIN for restrictions. This is only for use by pre-installed
2934 * apps and requires the MANAGE_USERS permission.
2935 * @param newPin the PIN to use for challenge dialogs.
2936 * @return Returns true if the challenge PIN was set successfully.
Fyodor Kupolovef249092015-05-06 13:18:46 -07002937 * @deprecated The restrictions PIN functionality is no longer provided by the system.
2938 * This method is preserved for backwards compatibility reasons and always returns false.
Amith Yamasani655d0e22013-06-12 14:19:10 -07002939 */
Aurimas Liutikas514c5ef2016-05-24 15:22:55 -07002940 @Deprecated
Amith Yamasanid304af62013-09-05 09:30:23 -07002941 public boolean setRestrictionsChallenge(String newPin) {
Amith Yamasani655d0e22013-06-12 14:19:10 -07002942 return false;
2943 }
Amith Yamasani1a7472e2013-07-02 11:17:30 -07002944
Amith Yamasanie4afaa32014-06-30 14:55:07 +05302945 /**
2946 * @hide
2947 * Set restrictions that should apply to any future guest user that's created.
2948 */
2949 public void setDefaultGuestRestrictions(Bundle restrictions) {
2950 try {
2951 mService.setDefaultGuestRestrictions(restrictions);
2952 } catch (RemoteException re) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07002953 throw re.rethrowFromSystemServer();
Amith Yamasanie4afaa32014-06-30 14:55:07 +05302954 }
2955 }
2956
2957 /**
2958 * @hide
2959 * Gets the default guest restrictions.
2960 */
2961 public Bundle getDefaultGuestRestrictions() {
2962 try {
2963 return mService.getDefaultGuestRestrictions();
2964 } catch (RemoteException re) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07002965 throw re.rethrowFromSystemServer();
Amith Yamasanie4afaa32014-06-30 14:55:07 +05302966 }
Amith Yamasanie4afaa32014-06-30 14:55:07 +05302967 }
Fyodor Kupolovff7233e2015-04-08 11:28:52 -07002968
2969 /**
2970 * Returns creation time of the user or of a managed profile associated with the calling user.
2971 * @param userHandle user handle of the user or a managed profile associated with the
2972 * calling user.
2973 * @return creation time in milliseconds since Epoch time.
2974 */
Fyodor Kupolov385de622015-04-10 18:00:19 -07002975 public long getUserCreationTime(UserHandle userHandle) {
Fyodor Kupolovff7233e2015-04-08 11:28:52 -07002976 try {
Fyodor Kupolov385de622015-04-10 18:00:19 -07002977 return mService.getUserCreationTime(userHandle.getIdentifier());
Fyodor Kupolovff7233e2015-04-08 11:28:52 -07002978 } catch (RemoteException re) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07002979 throw re.rethrowFromSystemServer();
Fyodor Kupolovff7233e2015-04-08 11:28:52 -07002980 }
2981 }
Amith Yamasani12747872015-12-07 14:19:49 -08002982
2983 /**
2984 * @hide
2985 * Checks if any uninitialized user has the specific seed account name and type.
2986 *
Pavel Grafov6a40f092016-10-25 15:46:51 +01002987 * @param accountName The account name to check for
2988 * @param accountType The account type of the account to check for
Amith Yamasani12747872015-12-07 14:19:49 -08002989 * @return whether the seed account was found
2990 */
2991 public boolean someUserHasSeedAccount(String accountName, String accountType) {
2992 try {
2993 return mService.someUserHasSeedAccount(accountName, accountType);
2994 } catch (RemoteException re) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07002995 throw re.rethrowFromSystemServer();
Amith Yamasani12747872015-12-07 14:19:49 -08002996 }
2997 }
Pavel Grafov6a40f092016-10-25 15:46:51 +01002998
2999 /**
3000 * @hide
3001 * User that enforces a restriction.
3002 *
3003 * @see #getUserRestrictionSources(String, UserHandle)
3004 */
3005 @SystemApi
3006 public static final class EnforcingUser implements Parcelable {
3007 private final @UserIdInt int userId;
3008 private final @UserRestrictionSource int userRestrictionSource;
3009
3010 /**
3011 * @hide
3012 */
3013 public EnforcingUser(
3014 @UserIdInt int userId, @UserRestrictionSource int userRestrictionSource) {
3015 this.userId = userId;
3016 this.userRestrictionSource = userRestrictionSource;
3017 }
3018
3019 private EnforcingUser(Parcel in) {
3020 userId = in.readInt();
3021 userRestrictionSource = in.readInt();
3022 }
3023
Jeff Sharkey9e8f83d2019-02-28 12:06:45 -07003024 public static final @android.annotation.NonNull Creator<EnforcingUser> CREATOR = new Creator<EnforcingUser>() {
Pavel Grafov6a40f092016-10-25 15:46:51 +01003025 @Override
3026 public EnforcingUser createFromParcel(Parcel in) {
3027 return new EnforcingUser(in);
3028 }
3029
3030 @Override
3031 public EnforcingUser[] newArray(int size) {
3032 return new EnforcingUser[size];
3033 }
3034 };
3035
3036 @Override
3037 public int describeContents() {
3038 return 0;
3039 }
3040
3041 @Override
3042 public void writeToParcel(Parcel dest, int flags) {
3043 dest.writeInt(userId);
3044 dest.writeInt(userRestrictionSource);
3045 }
3046
3047 /**
3048 * Returns an id of the enforcing user.
3049 *
3050 * <p> Will be UserHandle.USER_NULL when restriction is set by the system.
3051 */
3052 public UserHandle getUserHandle() {
3053 return UserHandle.of(userId);
3054 }
3055
3056 /**
3057 * Returns the status of the enforcing user.
3058 *
3059 * <p> One of {@link #RESTRICTION_SOURCE_SYSTEM},
3060 * {@link #RESTRICTION_SOURCE_DEVICE_OWNER} and
3061 * {@link #RESTRICTION_SOURCE_PROFILE_OWNER}
3062 */
3063 public @UserRestrictionSource int getUserRestrictionSource() {
3064 return userRestrictionSource;
3065 }
3066 }
Amith Yamasani258848d2012-08-10 17:06:33 -07003067}