blob: 0a60764428dc7256c39764b2a97ec464d055d6bd [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 /**
Amith Yamasani26af8292014-09-09 09:57:27 -0700737 * Specifies if what is copied in the clipboard of this profile can
Nicolas Prevotf1939902014-06-25 09:29:02 +0100738 * be pasted in related profiles. Does not restrict if the clipboard of related profiles can be
739 * pasted in this profile.
740 * The default value is <code>false</code>.
Amith Yamasani26af8292014-09-09 09:57:27 -0700741 *
Fyodor Kupolov0ffbfea2016-01-19 10:07:54 -0800742 * <p>Key for user restrictions.
743 * <p>Type: Boolean
Makoto Onuki068c54a2015-10-13 14:34:03 -0700744 * @see DevicePolicyManager#addUserRestriction(ComponentName, String)
745 * @see DevicePolicyManager#clearUserRestriction(ComponentName, String)
Nicolas Prevotf1939902014-06-25 09:29:02 +0100746 * @see #getUserRestrictions()
747 */
748 public static final String DISALLOW_CROSS_PROFILE_COPY_PASTE = "no_cross_profile_copy_paste";
749
Amith Yamasani26af8292014-09-09 09:57:27 -0700750 /**
751 * Specifies if the user is not allowed to use NFC to beam out data from apps.
752 * The default value is <code>false</code>.
753 *
Fyodor Kupolov0ffbfea2016-01-19 10:07:54 -0800754 * <p>Key for user restrictions.
755 * <p>Type: Boolean
Makoto Onuki068c54a2015-10-13 14:34:03 -0700756 * @see DevicePolicyManager#addUserRestriction(ComponentName, String)
757 * @see DevicePolicyManager#clearUserRestriction(ComponentName, String)
Amith Yamasani26af8292014-09-09 09:57:27 -0700758 * @see #getUserRestrictions()
759 */
760 public static final String DISALLOW_OUTGOING_BEAM = "no_outgoing_beam";
761
Sander Alewijnse53d63dc2014-11-07 21:43:00 +0000762 /**
Oleksandr Peletskyif2519812016-01-26 20:16:06 +0100763 * Hidden user restriction to disallow access to wallpaper manager APIs. This restriction
764 * generally means that wallpapers are not supported for the particular user. This user
765 * restriction is always set for managed profiles, because such profiles don't have wallpapers.
Benjamin Franzf3ece362015-02-11 10:51:10 +0000766 * @hide
Oleksandr Peletskyif2519812016-01-26 20:16:06 +0100767 * @see #DISALLOW_SET_WALLPAPER
Makoto Onuki068c54a2015-10-13 14:34:03 -0700768 * @see DevicePolicyManager#addUserRestriction(ComponentName, String)
769 * @see DevicePolicyManager#clearUserRestriction(ComponentName, String)
Benjamin Franzf3ece362015-02-11 10:51:10 +0000770 * @see #getUserRestrictions()
771 */
772 public static final String DISALLOW_WALLPAPER = "no_wallpaper";
773
774 /**
Oleksandr Peletskyif2519812016-01-26 20:16:06 +0100775 * User restriction to disallow setting a wallpaper. Profile owner and device owner
776 * are able to set wallpaper regardless of this restriction.
777 * The default value is <code>false</code>.
778 *
779 * <p>Key for user restrictions.
780 * <p>Type: Boolean
781 * @see DevicePolicyManager#addUserRestriction(ComponentName, String)
782 * @see DevicePolicyManager#clearUserRestriction(ComponentName, String)
783 * @see #getUserRestrictions()
784 */
785 public static final String DISALLOW_SET_WALLPAPER = "no_set_wallpaper";
786
787 /**
Benjamin Franzbff46ba2015-03-05 18:33:51 +0000788 * Specifies if the user is not allowed to reboot the device into safe boot mode.
789 * This can only be set by device owners and profile owners on the primary user.
790 * The default value is <code>false</code>.
791 *
Fyodor Kupolov0ffbfea2016-01-19 10:07:54 -0800792 * <p>Key for user restrictions.
793 * <p>Type: Boolean
Makoto Onuki068c54a2015-10-13 14:34:03 -0700794 * @see DevicePolicyManager#addUserRestriction(ComponentName, String)
795 * @see DevicePolicyManager#clearUserRestriction(ComponentName, String)
Benjamin Franzbff46ba2015-03-05 18:33:51 +0000796 * @see #getUserRestrictions()
797 */
798 public static final String DISALLOW_SAFE_BOOT = "no_safe_boot";
799
800 /**
Fyodor Kupolovb5013302015-04-17 17:59:14 -0700801 * Specifies if a user is not allowed to record audio. This restriction is always enabled for
802 * background users. The default value is <code>false</code>.
803 *
Makoto Onuki068c54a2015-10-13 14:34:03 -0700804 * @see DevicePolicyManager#addUserRestriction(ComponentName, String)
805 * @see DevicePolicyManager#clearUserRestriction(ComponentName, String)
Fyodor Kupolovb5013302015-04-17 17:59:14 -0700806 * @see #getUserRestrictions()
807 * @hide
808 */
809 public static final String DISALLOW_RECORD_AUDIO = "no_record_audio";
810
811 /**
Fyodor Kupolov9cbfc9e2015-10-07 15:52:33 -0700812 * Specifies if a user is not allowed to run in the background and should be stopped during
813 * user switch. The default value is <code>false</code>.
814 *
815 * <p>This restriction can be set by device owners and profile owners.
816 *
817 * @see DevicePolicyManager#addUserRestriction(ComponentName, String)
818 * @see DevicePolicyManager#clearUserRestriction(ComponentName, String)
819 * @see #getUserRestrictions()
820 * @hide
821 */
Robin Lee6cfb7a92017-12-18 15:14:46 +0100822 @SystemApi
Fyodor Kupolov9cbfc9e2015-10-07 15:52:33 -0700823 public static final String DISALLOW_RUN_IN_BACKGROUND = "no_run_in_background";
824
825 /**
Makoto Onuki759a7632015-10-28 16:43:10 -0700826 * Specifies if a user is not allowed to use the camera.
827 *
828 * @see DevicePolicyManager#addUserRestriction(ComponentName, String)
829 * @see DevicePolicyManager#clearUserRestriction(ComponentName, String)
830 * @see #getUserRestrictions()
831 * @hide
832 */
833 public static final String DISALLOW_CAMERA = "no_camera";
834
835 /**
Tony Makc1205112016-07-22 16:02:59 +0100836 * Specifies if a user is not allowed to unmute the device's master volume.
837 *
838 * @see DevicePolicyManager#setMasterVolumeMuted(ComponentName, boolean)
839 * @see DevicePolicyManager#clearUserRestriction(ComponentName, String)
840 * @see #getUserRestrictions()
841 * @hide
842 */
Esteban Talavera492b4722017-02-13 14:59:45 +0000843 public static final String DISALLOW_UNMUTE_DEVICE = "disallow_unmute_device";
Tony Makc1205112016-07-22 16:02:59 +0100844
845 /**
Mahaver Chopradea471e2015-12-17 11:02:37 +0000846 * Specifies if a user is not allowed to use cellular data when roaming. This can only be set by
847 * device owners. The default value is <code>false</code>.
848 *
849 * @see DevicePolicyManager#addUserRestriction(ComponentName, String)
850 * @see DevicePolicyManager#clearUserRestriction(ComponentName, String)
851 * @see #getUserRestrictions()
852 */
853 public static final String DISALLOW_DATA_ROAMING = "no_data_roaming";
854
855 /**
Oleksandr Peletskyi7f1f1df2016-01-18 15:40:21 +0100856 * Specifies if a user is not allowed to change their icon. Device owner and profile owner
857 * can set this restriction. When it is set by device owner, only the target user will be
858 * affected. The default value is <code>false</code>.
859 *
860 * <p>Key for user restrictions.
Oleksandr Peletskyi7f1f1df2016-01-18 15:40:21 +0100861 * <p>Type: Boolean
Oleksandr Peletskyi7f1f1df2016-01-18 15:40:21 +0100862 * @see DevicePolicyManager#addUserRestriction(ComponentName, String)
863 * @see DevicePolicyManager#clearUserRestriction(ComponentName, String)
864 * @see #getUserRestrictions()
865 */
866 public static final String DISALLOW_SET_USER_ICON = "no_set_user_icon";
867
868 /**
Mahaver Chopra3d9805d2016-07-07 16:25:05 +0100869 * Specifies if a user is not allowed to enable the oem unlock setting. The default value is
Esteban Talaverac48b20f2016-08-11 11:23:40 +0100870 * <code>false</code>. Setting this restriction has no effect if the bootloader is already
871 * unlocked.
Mahaver Chopra3d9805d2016-07-07 16:25:05 +0100872 *
Lenka Trochtova12b04962016-11-29 21:00:12 +0100873 * <p>Not for use by third-party applications.
874 *
Mahaver Chopra3d9805d2016-07-07 16:25:05 +0100875 * @see DevicePolicyManager#addUserRestriction(ComponentName, String)
876 * @see DevicePolicyManager#clearUserRestriction(ComponentName, String)
877 * @see #getUserRestrictions()
Andrew Scull3b8b46f2017-02-13 18:12:15 +0000878 * @deprecated use {@link OemLockManager#setOemUnlockAllowedByCarrier(boolean, byte[])} instead.
Mahaver Chopra3d9805d2016-07-07 16:25:05 +0100879 * @hide
880 */
Andrew Scull3b8b46f2017-02-13 18:12:15 +0000881 @Deprecated
Lenka Trochtova12b04962016-11-29 21:00:12 +0100882 @SystemApi
Mahaver Chopra3d9805d2016-07-07 16:25:05 +0100883 public static final String DISALLOW_OEM_UNLOCK = "no_oem_unlock";
884
885 /**
Pavel Grafovc4f87e92017-10-26 16:34:25 +0100886 * Specifies that the managed profile is not allowed to have unified lock screen challenge with
887 * the primary user.
888 *
889 * <p><strong>Note:</strong> Setting this restriction alone doesn't automatically set a
890 * separate challenge. Profile owner can ask the user to set a new password using
891 * {@link DevicePolicyManager#ACTION_SET_NEW_PASSWORD} and verify it using
892 * {@link DevicePolicyManager#isUsingUnifiedPassword(ComponentName)}.
893 *
894 * <p>Can be set by profile owners. It only has effect on managed profiles when set by managed
895 * profile owner. Has no effect on non-managed profiles or users.
896 * <p>Key for user restrictions.
897 * <p>Type: Boolean
898 * @see DevicePolicyManager#addUserRestriction(ComponentName, String)
899 * @see DevicePolicyManager#clearUserRestriction(ComponentName, String)
900 * @see #getUserRestrictions()
901 */
902 public static final String DISALLOW_UNIFIED_PASSWORD = "no_unified_password";
903
904 /**
Nicolas Prevotf0029c12015-06-25 14:25:41 -0700905 * Allows apps in the parent profile to handle web links from the managed profile.
906 *
Nicolas Prevot9edbda12015-06-17 11:09:48 -0700907 * This user restriction has an effect only in a managed profile.
908 * If set:
909 * Intent filters of activities in the parent profile with action
910 * {@link android.content.Intent#ACTION_VIEW},
911 * category {@link android.content.Intent#CATEGORY_BROWSABLE}, scheme http or https, and which
912 * define a host can handle intents from the managed profile.
913 * The default value is <code>false</code>.
914 *
Fyodor Kupolov0ffbfea2016-01-19 10:07:54 -0800915 * <p>Key for user restrictions.
916 * <p>Type: Boolean
Makoto Onuki068c54a2015-10-13 14:34:03 -0700917 * @see DevicePolicyManager#addUserRestriction(ComponentName, String)
918 * @see DevicePolicyManager#clearUserRestriction(ComponentName, String)
Nicolas Prevot9edbda12015-06-17 11:09:48 -0700919 * @see #getUserRestrictions()
920 */
Nicolas Prevotf0029c12015-06-25 14:25:41 -0700921 public static final String ALLOW_PARENT_PROFILE_APP_LINKING
922 = "allow_parent_profile_app_linking";
Nicolas Prevot9edbda12015-06-17 11:09:48 -0700923
924 /**
Felipe Leme24d58932017-03-21 14:13:58 -0700925 * Specifies if a user is not allowed to use Autofill Services.
926 *
927 * <p>Device owner and profile owner can set this restriction. When it is set by device owner,
928 * only the target user will be affected.
929 *
930 * <p>The default value is <code>false</code>.
931 *
932 * @see DevicePolicyManager#addUserRestriction(ComponentName, String)
933 * @see DevicePolicyManager#clearUserRestriction(ComponentName, String)
934 * @see #getUserRestrictions()
935 */
936 public static final String DISALLOW_AUTOFILL = "no_autofill";
937
938 /**
Felipe Leme1dfa9a02018-10-17 17:24:37 -0700939 * Specifies if the contents of a user's screen is not allowed to be captured for artificial
940 * intelligence purposes.
941 *
942 * <p>Device owner and profile owner can set this restriction. When it is set by device owner,
943 * only the target user will be affected.
944 *
945 * <p>The default value is <code>false</code>.
946 *
947 * @see DevicePolicyManager#addUserRestriction(ComponentName, String)
948 * @see DevicePolicyManager#clearUserRestriction(ComponentName, String)
949 * @see #getUserRestrictions()
950 */
Felipe Leme749b8892018-12-03 16:30:30 -0800951 public static final String DISALLOW_CONTENT_CAPTURE = "no_content_capture";
Felipe Leme1dfa9a02018-10-17 17:24:37 -0700952
953 /**
Benjamin Franzff66fa92017-08-10 10:39:44 +0100954 * Specifies if user switching is blocked on the current user.
955 *
956 * <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 +0800957 * Device owner can still switch user via
958 * {@link DevicePolicyManager#switchUser(ComponentName, UserHandle)} when this restriction is
959 * set.
Benjamin Franzff66fa92017-08-10 10:39:44 +0100960 *
961 * <p>The default value is <code>false</code>.
962 *
963 * @see DevicePolicyManager#addUserRestriction(ComponentName, String)
964 * @see DevicePolicyManager#clearUserRestriction(ComponentName, String)
965 * @see #getUserRestrictions()
966 */
967 public static final String DISALLOW_USER_SWITCH = "no_user_switch";
968
969 /**
Rubin Xucc391c22018-01-02 20:37:35 +0000970 * Specifies whether the user can share file / picture / data from the primary user into the
971 * managed profile, either by sending them from the primary side, or by picking up data within
972 * an app in the managed profile.
973 * <p>
974 * When a managed profile is created, the system allows the user to send data from the primary
975 * side to the profile by setting up certain default cross profile intent filters. If
976 * this is undesired, this restriction can be set to disallow it. Note that this restriction
977 * will not block any sharing allowed by explicit
978 * {@link DevicePolicyManager#addCrossProfileIntentFilter} calls by the profile owner.
979 * <p>
980 * This restriction is only meaningful when set by profile owner. When it is set by device
981 * owner, it does not have any effect.
982 * <p>
983 * The default value is <code>false</code>.
984 *
985 * @see DevicePolicyManager#addUserRestriction(ComponentName, String)
986 * @see DevicePolicyManager#clearUserRestriction(ComponentName, String)
987 * @see #getUserRestrictions()
988 */
989 public static final String DISALLOW_SHARE_INTO_MANAGED_PROFILE = "no_sharing_into_profile";
Vladislav Kuzkokov622b9f92018-01-25 16:33:05 +0100990
991 /**
992 * Specifies whether the user is allowed to print.
993 *
994 * This restriction can be set by device or profile owner.
995 *
996 * The default value is {@code false}.
997 *
998 * @see DevicePolicyManager#addUserRestriction(ComponentName, String)
999 * @see DevicePolicyManager#clearUserRestriction(ComponentName, String)
1000 * @see #getUserRestrictions()
1001 */
1002 public static final String DISALLOW_PRINTING = "no_printing";
1003
Rubin Xucc391c22018-01-02 20:37:35 +00001004 /**
Eran Messeri09b122da2018-10-05 15:33:53 +01001005 * Specifies whether the user is allowed to modify private DNS settings.
1006 *
1007 * <p>The default value is <code>false</code>.
1008 *
1009 * <p>This user restriction can only be applied by the Device Owner.
1010 * <p>Key for user restrictions.
1011 * <p>Type: Boolean
1012 * @see DevicePolicyManager#addUserRestriction(ComponentName, String)
1013 * @see DevicePolicyManager#clearUserRestriction(ComponentName, String)
1014 * @see #getUserRestrictions()
1015 */
1016 public static final String DISALLOW_CONFIG_PRIVATE_DNS =
1017 "disallow_config_private_dns";
1018
1019 /**
Sander Alewijnse53d63dc2014-11-07 21:43:00 +00001020 * Application restriction key that is used to indicate the pending arrival
1021 * of real restrictions for the app.
1022 *
1023 * <p>
1024 * Applications that support restrictions should check for the presence of this key.
1025 * A <code>true</code> value indicates that restrictions may be applied in the near
1026 * future but are not available yet. It is the responsibility of any
1027 * management application that sets this flag to update it when the final
1028 * restrictions are enforced.
1029 *
Fyodor Kupolov0ffbfea2016-01-19 10:07:54 -08001030 * <p>Key for application restrictions.
1031 * <p>Type: Boolean
Nicolas Prevotb14ed952015-01-13 14:23:53 +00001032 * @see android.app.admin.DevicePolicyManager#setApplicationRestrictions(
1033 * android.content.ComponentName, String, Bundle)
1034 * @see android.app.admin.DevicePolicyManager#getApplicationRestrictions(
1035 * android.content.ComponentName, String)
Sander Alewijnse53d63dc2014-11-07 21:43:00 +00001036 */
1037 public static final String KEY_RESTRICTIONS_PENDING = "restrictions_pending";
1038
Amith Yamasani12747872015-12-07 14:19:49 -08001039 private static final String ACTION_CREATE_USER = "android.os.action.CREATE_USER";
1040
1041 /**
1042 * Extra containing a name for the user being created. Optional parameter passed to
1043 * ACTION_CREATE_USER activity.
1044 * @hide
1045 */
1046 public static final String EXTRA_USER_NAME = "android.os.extra.USER_NAME";
1047
1048 /**
1049 * Extra containing account name for the user being created. Optional parameter passed to
1050 * ACTION_CREATE_USER activity.
1051 * @hide
1052 */
1053 public static final String EXTRA_USER_ACCOUNT_NAME = "android.os.extra.USER_ACCOUNT_NAME";
1054
1055 /**
1056 * Extra containing account type for the user being created. Optional parameter passed to
1057 * ACTION_CREATE_USER activity.
1058 * @hide
1059 */
1060 public static final String EXTRA_USER_ACCOUNT_TYPE = "android.os.extra.USER_ACCOUNT_TYPE";
1061
1062 /**
1063 * Extra containing account-specific data for the user being created. Optional parameter passed
1064 * to ACTION_CREATE_USER activity.
1065 * @hide
1066 */
1067 public static final String EXTRA_USER_ACCOUNT_OPTIONS
1068 = "android.os.extra.USER_ACCOUNT_OPTIONS";
1069
Amith Yamasani655d0e22013-06-12 14:19:10 -07001070 /** @hide */
1071 public static final int PIN_VERIFICATION_FAILED_INCORRECT = -3;
1072 /** @hide */
1073 public static final int PIN_VERIFICATION_FAILED_NOT_SET = -2;
1074 /** @hide */
1075 public static final int PIN_VERIFICATION_SUCCESS = -1;
1076
Amith Yamasani37ed8d12016-01-27 14:40:16 -08001077 /**
Makoto Onukie72f81b2017-03-16 14:08:19 -07001078 * Sent when user restrictions have changed.
1079 *
1080 * @hide
1081 */
1082 @SystemApi
1083 @TestApi // To allow seeing it from CTS.
1084 public static final String ACTION_USER_RESTRICTIONS_CHANGED =
1085 "android.os.action.USER_RESTRICTIONS_CHANGED";
1086
1087 /**
Amith Yamasani37ed8d12016-01-27 14:40:16 -08001088 * Error result indicating that this user is not allowed to add other users on this device.
1089 * This is a result code returned from the activity created by the intent
1090 * {@link #createUserCreationIntent(String, String, String, PersistableBundle)}.
1091 */
1092 public static final int USER_CREATION_FAILED_NOT_PERMITTED = Activity.RESULT_FIRST_USER;
1093
1094 /**
1095 * Error result indicating that no more users can be created on this device.
1096 * This is a result code returned from the activity created by the intent
1097 * {@link #createUserCreationIntent(String, String, String, PersistableBundle)}.
1098 */
1099 public static final int USER_CREATION_FAILED_NO_MORE_USERS = Activity.RESULT_FIRST_USER + 1;
1100
Alex Chau89386ba2018-02-13 11:09:17 +08001101 /**
1102 * Indicates user operation is successful.
1103 */
1104 public static final int USER_OPERATION_SUCCESS = 0;
1105
1106 /**
1107 * Indicates user operation failed for unknown reason.
1108 */
1109 public static final int USER_OPERATION_ERROR_UNKNOWN = 1;
1110
1111 /**
1112 * Indicates user operation failed because target user is a managed profile.
1113 */
1114 public static final int USER_OPERATION_ERROR_MANAGED_PROFILE = 2;
1115
1116 /**
1117 * Indicates user operation failed because maximum running user limit has been reached.
1118 */
1119 public static final int USER_OPERATION_ERROR_MAX_RUNNING_USERS = 3;
1120
1121 /**
1122 * Indicates user operation failed because the target user is in the foreground.
1123 */
1124 public static final int USER_OPERATION_ERROR_CURRENT_USER = 4;
1125
1126 /**
1127 * Indicates user operation failed because device has low data storage.
1128 */
1129 public static final int USER_OPERATION_ERROR_LOW_STORAGE = 5;
1130
1131 /**
1132 * Indicates user operation failed because maximum user limit has been reached.
1133 */
1134 public static final int USER_OPERATION_ERROR_MAX_USERS = 6;
1135
1136 /**
1137 * Result returned from various user operations.
1138 *
1139 * @hide
1140 */
1141 @Retention(RetentionPolicy.SOURCE)
1142 @IntDef(prefix = { "USER_OPERATION_" }, value = {
1143 USER_OPERATION_SUCCESS,
1144 USER_OPERATION_ERROR_UNKNOWN,
1145 USER_OPERATION_ERROR_MANAGED_PROFILE,
1146 USER_OPERATION_ERROR_MAX_RUNNING_USERS,
1147 USER_OPERATION_ERROR_CURRENT_USER,
1148 USER_OPERATION_ERROR_LOW_STORAGE,
1149 USER_OPERATION_ERROR_MAX_USERS
1150 })
1151 public @interface UserOperationResult {}
1152
1153 /**
1154 * Thrown to indicate user operation failed.
1155 */
1156 public static class UserOperationException extends RuntimeException {
1157 private final @UserOperationResult int mUserOperationResult;
1158
1159 /**
1160 * Constructs a UserOperationException with specific result code.
1161 *
1162 * @param message the detail message
1163 * @param userOperationResult the result code
1164 * @hide
1165 */
1166 public UserOperationException(String message,
1167 @UserOperationResult int userOperationResult) {
1168 super(message);
1169 mUserOperationResult = userOperationResult;
1170 }
1171
1172 /**
1173 * Returns the operation result code.
1174 */
1175 public @UserOperationResult int getUserOperationResult() {
1176 return mUserOperationResult;
1177 }
1178 }
1179
Amith Yamasani7e99bc02013-04-16 18:24:51 -07001180 /** @hide */
Amith Yamasanic0688302015-10-30 10:40:03 -07001181 public static UserManager get(Context context) {
1182 return (UserManager) context.getSystemService(Context.USER_SERVICE);
Amith Yamasani27db4682013-03-30 17:07:47 -07001183 }
Maggie Benthalla12fccf2013-03-14 18:02:12 -04001184
Amith Yamasani258848d2012-08-10 17:06:33 -07001185 /** @hide */
1186 public UserManager(Context context, IUserManager service) {
1187 mService = service;
Fyodor Kupolov5200e1c2016-10-17 18:46:16 -07001188 mContext = context.getApplicationContext();
Amith Yamasani258848d2012-08-10 17:06:33 -07001189 }
1190
1191 /**
Amith Yamasani06964342016-04-15 13:55:01 -07001192 * Returns whether this device supports multiple users with their own login and customizable
1193 * space.
1194 * @return whether the device supports multiple users.
Amith Yamasani258848d2012-08-10 17:06:33 -07001195 */
Jeff Sharkey4673e7e2012-09-19 13:14:30 -07001196 public static boolean supportsMultipleUsers() {
Kenny Guy1a447532014-02-20 21:55:32 +00001197 return getMaxSupportedUsers() > 1
1198 && SystemProperties.getBoolean("fw.show_multiuserui",
1199 Resources.getSystem().getBoolean(R.bool.config_enableMultiUserUI));
Amith Yamasani258848d2012-08-10 17:06:33 -07001200 }
1201
Maggie Benthall67944582013-02-22 14:58:27 -05001202 /**
Xiaohui Chen7cb69df2015-07-13 16:01:01 -07001203 * @hide
1204 * @return Whether the device is running with split system user. It means the system user and
1205 * primary user are two separate users. Previously system user and primary user are combined as
1206 * a single owner user. see @link {android.os.UserHandle#USER_OWNER}
1207 */
Philip P. Moltmannf80809f2018-04-04 11:20:44 -07001208 @TestApi
Xiaohui Chen7cb69df2015-07-13 16:01:01 -07001209 public static boolean isSplitSystemUser() {
John Reckaa67f682016-09-20 14:24:21 -07001210 return RoSystemProperties.FW_SYSTEM_USER_SPLIT;
Xiaohui Chen7cb69df2015-07-13 16:01:01 -07001211 }
1212
1213 /**
Evan Rosky13a58a92016-07-27 15:51:09 -07001214 * @return Whether guest user is always ephemeral
1215 * @hide
1216 */
1217 public static boolean isGuestUserEphemeral() {
1218 return Resources.getSystem()
1219 .getBoolean(com.android.internal.R.bool.config_guestUserEphemeral);
1220 }
1221
1222 /**
Fyodor Kupolov523c4042016-02-24 15:03:13 -08001223 * Returns whether switching users is currently allowed.
1224 * <p>For instance switching users is not allowed if the current user is in a phone call,
Benjamin Franzff66fa92017-08-10 10:39:44 +01001225 * system user hasn't been unlocked yet, or {@link #DISALLOW_USER_SWITCH} is set.
Fyodor Kupolov523c4042016-02-24 15:03:13 -08001226 * @hide
1227 */
Varun Shahe38c6782018-12-04 16:57:49 -08001228 @SystemApi
1229 @RequiresPermission(anyOf = {android.Manifest.permission.MANAGE_USERS,
1230 android.Manifest.permission.INTERACT_ACROSS_USERS}, conditional = true)
Fyodor Kupolov523c4042016-02-24 15:03:13 -08001231 public boolean canSwitchUsers() {
1232 boolean allowUserSwitchingWhenSystemUserLocked = Settings.Global.getInt(
1233 mContext.getContentResolver(),
1234 Settings.Global.ALLOW_USER_SWITCHING_WHEN_SYSTEM_USER_LOCKED, 0) != 0;
1235 boolean isSystemUserUnlocked = isUserUnlocked(UserHandle.SYSTEM);
1236 boolean inCall = TelephonyManager.getDefault().getCallState()
1237 != TelephonyManager.CALL_STATE_IDLE;
Benjamin Franzff66fa92017-08-10 10:39:44 +01001238 boolean isUserSwitchDisallowed = hasUserRestriction(DISALLOW_USER_SWITCH);
1239 return (allowUserSwitchingWhenSystemUserLocked || isSystemUserUnlocked) && !inCall
1240 && !isUserSwitchDisallowed;
Fyodor Kupolov523c4042016-02-24 15:03:13 -08001241 }
1242
1243 /**
Amith Yamasani5760e172015-04-17 18:42:41 -07001244 * Returns the user handle for the user that this process is running under.
Jessica Hummelbe81c802014-04-22 15:49:22 +01001245 *
Amith Yamasani5760e172015-04-17 18:42:41 -07001246 * @return the user handle of this process.
Amith Yamasani258848d2012-08-10 17:06:33 -07001247 * @hide
Maggie Benthall67944582013-02-22 14:58:27 -05001248 */
Jeff Sharkey8588bc12016-01-06 16:47:42 -07001249 public @UserIdInt int getUserHandle() {
Dianne Hackborn79af1dd2012-08-16 16:42:52 -07001250 return UserHandle.myUserId();
Amith Yamasani258848d2012-08-10 17:06:33 -07001251 }
1252
1253 /**
Dianne Hackborn8832c182012-09-17 17:20:24 -07001254 * Returns the user name of the user making this call. This call is only
1255 * available to applications on the system image; it requires the
1256 * MANAGE_USERS permission.
Amith Yamasani258848d2012-08-10 17:06:33 -07001257 * @return the user name
1258 */
1259 public String getUserName() {
Will Harmond9dcfb12017-05-18 15:41:15 -07001260 UserInfo user = getUserInfo(getUserHandle());
1261 return user == null ? "" : user.name;
Amith Yamasani258848d2012-08-10 17:06:33 -07001262 }
1263
Dianne Hackborn67a101a2014-10-02 12:42:18 -07001264 /**
Fyodor Kupolov605b12a2017-05-10 15:58:09 -07001265 * Returns whether user name has been set.
1266 * <p>This method can be used to check that the value returned by {@link #getUserName()} was
1267 * set by the user and is not a placeholder string provided by the system.
1268 * @hide
1269 */
1270 public boolean isUserNameSet() {
1271 try {
1272 return mService.isUserNameSet(getUserHandle());
1273 } catch (RemoteException re) {
1274 throw re.rethrowFromSystemServer();
1275 }
1276 }
1277
1278 /**
Dan Morrille4ab16a2012-09-20 20:25:55 -07001279 * Used to determine whether the user making this call is subject to
1280 * teleportations.
Dianne Hackborn67a101a2014-10-02 12:42:18 -07001281 *
Dianne Hackborn955d8d62014-10-07 20:17:19 -07001282 * <p>As of {@link android.os.Build.VERSION_CODES#LOLLIPOP}, this method can
Dianne Hackborn67a101a2014-10-02 12:42:18 -07001283 * now automatically identify goats using advanced goat recognition technology.</p>
1284 *
1285 * @return Returns true if the user making this call is a goat.
Dan Morrille4ab16a2012-09-20 20:25:55 -07001286 */
1287 public boolean isUserAGoat() {
Adam Powell988ae302014-09-17 17:58:33 -07001288 return mContext.getPackageManager()
1289 .isPackageAvailable("com.coffeestainstudios.goatsimulator");
Dan Morrille4ab16a2012-09-20 20:25:55 -07001290 }
Amith Yamasanidf2e92a2013-03-01 17:04:38 -08001291
1292 /**
Xiaohui Chen70f6c382015-04-28 14:21:43 -07001293 * Used to check if this process is running under the primary user. The primary user
1294 * is the first human user on a device.
1295 *
1296 * @return whether this process is running under the primary user.
1297 * @hide
1298 */
Varun Shahe142e332018-10-22 15:52:56 -07001299 @SystemApi
1300 @RequiresPermission(android.Manifest.permission.MANAGE_USERS)
Xiaohui Chen70f6c382015-04-28 14:21:43 -07001301 public boolean isPrimaryUser() {
1302 UserInfo user = getUserInfo(UserHandle.myUserId());
Amith Yamasanid35a89c2016-05-26 16:58:43 -07001303 return user != null && user.isPrimary();
Xiaohui Chen70f6c382015-04-28 14:21:43 -07001304 }
1305
1306 /**
Amith Yamasani5760e172015-04-17 18:42:41 -07001307 * Used to check if this process is running under the system user. The system user
1308 * is the initial user that is implicitly created on first boot and hosts most of the
1309 * system services.
1310 *
1311 * @return whether this process is running under the system user.
1312 */
1313 public boolean isSystemUser() {
Xiaohui Chen70f6c382015-04-28 14:21:43 -07001314 return UserHandle.myUserId() == UserHandle.USER_SYSTEM;
Amith Yamasani5760e172015-04-17 18:42:41 -07001315 }
Xiaohui Chen2b45f8a2015-08-04 15:12:23 -07001316
Amith Yamasani5760e172015-04-17 18:42:41 -07001317 /**
Varun Shahe142e332018-10-22 15:52:56 -07001318 * Used to check if this process is running as an admin user. An admin user is allowed to
1319 * modify or configure certain settings that aren't available to non-admin users,
1320 * create and delete additional users, etc. There can be more than one admin users.
1321 *
1322 * @return whether this process is running under an admin user.
Amith Yamasani462ac3a2015-06-30 14:21:01 -07001323 * @hide
Amith Yamasani462ac3a2015-06-30 14:21:01 -07001324 */
Varun Shahe142e332018-10-22 15:52:56 -07001325 @SystemApi
1326 @RequiresPermission(android.Manifest.permission.MANAGE_USERS)
Amith Yamasani462ac3a2015-06-30 14:21:01 -07001327 public boolean isAdminUser() {
Xiaohui Chen2b45f8a2015-08-04 15:12:23 -07001328 return isUserAdmin(UserHandle.myUserId());
1329 }
1330
1331 /**
1332 * @hide
1333 * Returns whether the provided user is an admin user. There can be more than one admin
1334 * user.
1335 */
Jeff Sharkey8588bc12016-01-06 16:47:42 -07001336 public boolean isUserAdmin(@UserIdInt int userId) {
Xiaohui Chen2b45f8a2015-08-04 15:12:23 -07001337 UserInfo user = getUserInfo(userId);
1338 return user != null && user.isAdmin();
Amith Yamasani462ac3a2015-06-30 14:21:01 -07001339 }
1340
1341 /**
Fyodor Kupolovca177562017-11-09 17:43:01 -08001342 * @hide
1343 * @deprecated Use {@link #isRestrictedProfile()}
1344 */
1345 @Deprecated
1346 public boolean isLinkedUser() {
1347 return isRestrictedProfile();
1348 }
1349
1350 /**
Varun Shahe142e332018-10-22 15:52:56 -07001351 * Used to check if this process is running under a restricted profile. Restricted profiles
1352 * may have a reduced number of available apps, app restrictions, and account restrictions.
1353 *
1354 * @return whether this process is running under a restricted profile.
Amith Yamasani2555daf2013-04-25 13:39:27 -07001355 * @hide
Amith Yamasanidf2e92a2013-03-01 17:04:38 -08001356 */
Fyodor Kupolovca177562017-11-09 17:43:01 -08001357 @SystemApi
Varun Shahe142e332018-10-22 15:52:56 -07001358 @RequiresPermission(android.Manifest.permission.MANAGE_USERS)
Fyodor Kupolovca177562017-11-09 17:43:01 -08001359 public boolean isRestrictedProfile() {
Amith Yamasanidf2e92a2013-03-01 17:04:38 -08001360 try {
Amith Yamasani71e6c692013-03-24 17:39:28 -07001361 return mService.isRestricted();
Amith Yamasanidf2e92a2013-03-01 17:04:38 -08001362 } catch (RemoteException re) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001363 throw re.rethrowFromSystemServer();
Amith Yamasanidf2e92a2013-03-01 17:04:38 -08001364 }
1365 }
1366
Amith Yamasani258848d2012-08-10 17:06:33 -07001367 /**
Fyodor Kupolov1c363152015-09-02 13:27:21 -07001368 * Checks if specified user can have restricted profile.
1369 * @hide
1370 */
Jeff Sharkey8588bc12016-01-06 16:47:42 -07001371 public boolean canHaveRestrictedProfile(@UserIdInt int userId) {
Fyodor Kupolov1c363152015-09-02 13:27:21 -07001372 try {
1373 return mService.canHaveRestrictedProfile(userId);
1374 } catch (RemoteException re) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001375 throw re.rethrowFromSystemServer();
Fyodor Kupolov1c363152015-09-02 13:27:21 -07001376 }
1377 }
1378
1379 /**
Fyodor Kupolovca177562017-11-09 17:43:01 -08001380 * Returns whether the calling user has at least one restricted profile associated with it.
1381 * @return
1382 * @hide
1383 */
1384 @SystemApi
1385 public boolean hasRestrictedProfiles() {
1386 try {
1387 return mService.hasRestrictedProfiles();
1388 } catch (RemoteException re) {
1389 throw re.rethrowFromSystemServer();
1390 }
1391 }
1392
1393 /**
Evan Rosky13a58a92016-07-27 15:51:09 -07001394 * Checks if a user is a guest user.
1395 * @return whether user is a guest user.
1396 * @hide
1397 */
1398 public boolean isGuestUser(int id) {
1399 UserInfo user = getUserInfo(id);
1400 return user != null && user.isGuest();
1401 }
1402
1403 /**
Varun Shahe142e332018-10-22 15:52:56 -07001404 * Used to check if this process is running under a guest user. A guest user may be transient.
1405 *
1406 * @return whether this process is running under a guest user.
Amith Yamasani1e9c2182014-06-11 17:25:51 -07001407 * @hide
1408 */
Varun Shahe142e332018-10-22 15:52:56 -07001409 @SystemApi
1410 @RequiresPermission(android.Manifest.permission.MANAGE_USERS)
Amith Yamasani1e9c2182014-06-11 17:25:51 -07001411 public boolean isGuestUser() {
1412 UserInfo user = getUserInfo(UserHandle.myUserId());
Amith Yamasanid35a89c2016-05-26 16:58:43 -07001413 return user != null && user.isGuest();
1414 }
1415
Fyodor Kupolovca177562017-11-09 17:43:01 -08001416
Amith Yamasanid35a89c2016-05-26 16:58:43 -07001417 /**
Amith Yamasani1c41dc82016-06-28 16:13:15 -07001418 * Checks if the calling app is running in a demo user. When running in a demo user,
1419 * apps can be more helpful to the user, or explain their features in more detail.
1420 *
Amith Yamasanid35a89c2016-05-26 16:58:43 -07001421 * @return whether the caller is a demo user.
Amith Yamasanid35a89c2016-05-26 16:58:43 -07001422 */
1423 public boolean isDemoUser() {
Amith Yamasani1c41dc82016-06-28 16:13:15 -07001424 try {
1425 return mService.isDemoUser(UserHandle.myUserId());
1426 } catch (RemoteException re) {
1427 throw re.rethrowFromSystemServer();
1428 }
Amith Yamasani1e9c2182014-06-11 17:25:51 -07001429 }
1430
1431 /**
Amith Yamasani0e8d7d62014-09-03 13:17:28 -07001432 * Checks if the calling app is running in a managed profile.
Amith Yamasani0e8d7d62014-09-03 13:17:28 -07001433 *
1434 * @return whether the caller is in a managed profile.
1435 * @hide
1436 */
1437 @SystemApi
Jeff Sharkeyd86b8fe2017-06-02 17:36:26 -06001438 @RequiresPermission(android.Manifest.permission.MANAGE_USERS)
Amith Yamasani0e8d7d62014-09-03 13:17:28 -07001439 public boolean isManagedProfile() {
Makoto Onukid49f3fa2017-01-25 14:09:48 -08001440 // No need for synchronization. Once it becomes non-null, it'll be non-null forever.
1441 // Worst case we might end up calling the AIDL method multiple times but that's fine.
1442 if (mIsManagedProfileCached != null) {
1443 return mIsManagedProfileCached;
1444 }
Tony Mak8673b282016-03-21 21:10:59 +00001445 try {
Makoto Onukid49f3fa2017-01-25 14:09:48 -08001446 mIsManagedProfileCached = mService.isManagedProfile(UserHandle.myUserId());
1447 return mIsManagedProfileCached;
Tony Mak8673b282016-03-21 21:10:59 +00001448 } catch (RemoteException re) {
1449 throw re.rethrowFromSystemServer();
1450 }
1451 }
1452
1453 /**
1454 * Checks if the specified user is a managed profile.
1455 * Requires {@link android.Manifest.permission#MANAGE_USERS} permission, otherwise the caller
1456 * must be in the same profile group of specified user.
1457 *
1458 * @return whether the specified user is a managed profile.
1459 * @hide
1460 */
1461 @SystemApi
Jeff Sharkeyd86b8fe2017-06-02 17:36:26 -06001462 @RequiresPermission(android.Manifest.permission.MANAGE_USERS)
Tony Mak8673b282016-03-21 21:10:59 +00001463 public boolean isManagedProfile(@UserIdInt int userId) {
Makoto Onukid49f3fa2017-01-25 14:09:48 -08001464 if (userId == UserHandle.myUserId()) {
1465 return isManagedProfile();
1466 }
Tony Mak8673b282016-03-21 21:10:59 +00001467 try {
1468 return mService.isManagedProfile(userId);
1469 } catch (RemoteException re) {
1470 throw re.rethrowFromSystemServer();
1471 }
Amith Yamasani0e8d7d62014-09-03 13:17:28 -07001472 }
1473
1474 /**
Kenny Guy02c89902016-11-15 19:36:38 +00001475 * Gets badge for a managed profile.
1476 * Requires {@link android.Manifest.permission#MANAGE_USERS} permission, otherwise the caller
1477 * must be in the same profile group of specified user.
1478 *
1479 * @return which badge to use for the managed profile badge id will be less than
1480 * UserManagerService.getMaxManagedProfiles()
1481 * @hide
1482 */
1483 public int getManagedProfileBadge(@UserIdInt int userId) {
1484 try {
1485 return mService.getManagedProfileBadge(userId);
1486 } catch (RemoteException re) {
1487 throw re.rethrowFromSystemServer();
1488 }
1489 }
1490
1491 /**
Lenka Trochtovac4dd0212015-11-18 12:22:06 +01001492 * Checks if the calling app is running as an ephemeral user.
1493 *
1494 * @return whether the caller is an ephemeral user.
1495 * @hide
1496 */
1497 public boolean isEphemeralUser() {
1498 return isUserEphemeral(UserHandle.myUserId());
1499 }
1500
1501 /**
1502 * Returns whether the specified user is ephemeral.
1503 * @hide
1504 */
Jeff Sharkey8588bc12016-01-06 16:47:42 -07001505 public boolean isUserEphemeral(@UserIdInt int userId) {
Lenka Trochtovac4dd0212015-11-18 12:22:06 +01001506 final UserInfo user = getUserInfo(userId);
1507 return user != null && user.isEphemeral();
1508 }
1509
1510 /**
Dianne Hackborna8a9bd62012-10-09 15:36:59 -07001511 * Return whether the given user is actively running. This means that
1512 * the user is in the "started" state, not "stopped" -- it is currently
1513 * allowed to run code through scheduled alarms, receiving broadcasts,
1514 * etc. A started user may be either the current foreground user or a
1515 * background user; the result here does not distinguish between the two.
Makoto Onuki5eef50d2017-03-02 16:38:45 -08001516 *
1517 * <p>Note prior to Android Nougat MR1 (SDK version <= 24;
Andrew Solovay8a788f42018-12-12 14:25:47 -08001518 * {@link android.os.Build.VERSION_CODES#N}, this API required a system permission
Makoto Onuki5eef50d2017-03-02 16:38:45 -08001519 * in order to check other profile's status.
1520 * Since Android Nougat MR1 (SDK version >= 25;
Andrew Solovay8a788f42018-12-12 14:25:47 -08001521 * {@link android.os.Build.VERSION_CODES#N_MR1}), the restriction has been relaxed, and now
1522 * it'll accept any {@link android.os.UserHandle} within the same profile group as the caller.
Fyodor Kupolovcdb3c2f2017-02-10 11:48:32 -08001523 *
Dianne Hackborna8a9bd62012-10-09 15:36:59 -07001524 * @param user The user to retrieve the running state for.
1525 */
Makoto Onuki5eef50d2017-03-02 16:38:45 -08001526 // Note this requires either INTERACT_ACROSS_USERS or MANAGE_USERS.
Dianne Hackborna8a9bd62012-10-09 15:36:59 -07001527 public boolean isUserRunning(UserHandle user) {
Jeff Sharkey0e62384c2016-01-13 18:52:55 -07001528 return isUserRunning(user.getIdentifier());
1529 }
1530
1531 /** {@hide} */
Fyodor Kupolov2e7e0962016-12-01 18:09:17 -08001532 public boolean isUserRunning(@UserIdInt int userId) {
Dianne Hackborna8a9bd62012-10-09 15:36:59 -07001533 try {
Fyodor Kupolov2e7e0962016-12-01 18:09:17 -08001534 return mService.isUserRunning(userId);
Jeff Sharkey27b2e692016-02-25 17:40:12 -07001535 } catch (RemoteException re) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001536 throw re.rethrowFromSystemServer();
Dianne Hackborna8a9bd62012-10-09 15:36:59 -07001537 }
1538 }
1539
1540 /**
1541 * Return whether the given user is actively running <em>or</em> stopping.
1542 * This is like {@link #isUserRunning(UserHandle)}, but will also return
1543 * true if the user had been running but is in the process of being stopped
1544 * (but is not yet fully stopped, and still running some code).
Makoto Onuki5eef50d2017-03-02 16:38:45 -08001545 *
1546 * <p>Note prior to Android Nougat MR1 (SDK version <= 24;
Andrew Solovay8a788f42018-12-12 14:25:47 -08001547 * {@link android.os.Build.VERSION_CODES#N}, this API required a system permission
Makoto Onuki5eef50d2017-03-02 16:38:45 -08001548 * in order to check other profile's status.
1549 * Since Android Nougat MR1 (SDK version >= 25;
Andrew Solovay8a788f42018-12-12 14:25:47 -08001550 * {@link android.os.Build.VERSION_CODES#N_MR1}), the restriction has been relaxed, and now
1551 * it'll accept any {@link android.os.UserHandle} within the same profile group as the caller.
Fyodor Kupolovcdb3c2f2017-02-10 11:48:32 -08001552 *
Dianne Hackborna8a9bd62012-10-09 15:36:59 -07001553 * @param user The user to retrieve the running state for.
1554 */
Makoto Onuki5eef50d2017-03-02 16:38:45 -08001555 // Note this requires either INTERACT_ACROSS_USERS or MANAGE_USERS.
Dianne Hackborna8a9bd62012-10-09 15:36:59 -07001556 public boolean isUserRunningOrStopping(UserHandle user) {
1557 try {
Jeff Sharkeye17ac152015-11-06 22:40:29 -08001558 // TODO: reconcile stopped vs stopping?
Sudheer Shankadc589ac2016-11-10 15:30:17 -08001559 return ActivityManager.getService().isUserRunning(
Jeff Sharkeye17ac152015-11-06 22:40:29 -08001560 user.getIdentifier(), ActivityManager.FLAG_OR_STOPPED);
Jeff Sharkey27b2e692016-02-25 17:40:12 -07001561 } catch (RemoteException re) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001562 throw re.rethrowFromSystemServer();
Dianne Hackborna8a9bd62012-10-09 15:36:59 -07001563 }
1564 }
1565
Jeff Sharkey0825ab22015-12-02 13:04:49 -07001566 /**
Jeff Sharkeycf3f0a12016-03-17 19:57:58 -06001567 * Return whether the calling user is running in an "unlocked" state.
1568 * <p>
1569 * On devices with direct boot, a user is unlocked only after they've
1570 * entered their credentials (such as a lock pattern or PIN). On devices
1571 * without direct boot, a user is unlocked as soon as it starts.
1572 * <p>
1573 * When a user is locked, only device-protected data storage is available.
1574 * When a user is unlocked, both device-protected and credential-protected
1575 * private app data storage is available.
Jeff Sharkey8a372a02016-03-16 16:25:45 -06001576 *
1577 * @see Intent#ACTION_USER_UNLOCKED
1578 * @see Context#createDeviceProtectedStorageContext()
Jeff Sharkeyb6423872015-12-11 11:11:58 -07001579 */
1580 public boolean isUserUnlocked() {
1581 return isUserUnlocked(Process.myUserHandle());
1582 }
1583
1584 /**
Jeff Sharkeycf3f0a12016-03-17 19:57:58 -06001585 * Return whether the given user is running in an "unlocked" state.
1586 * <p>
1587 * On devices with direct boot, a user is unlocked only after they've
1588 * entered their credentials (such as a lock pattern or PIN). On devices
1589 * without direct boot, a user is unlocked as soon as it starts.
1590 * <p>
1591 * When a user is locked, only device-protected data storage is available.
1592 * When a user is unlocked, both device-protected and credential-protected
1593 * private app data storage is available.
Fyodor Kupolovcdb3c2f2017-02-10 11:48:32 -08001594 * <p>Requires {@code android.permission.MANAGE_USERS} or
1595 * {@code android.permission.INTERACT_ACROSS_USERS}, otherwise specified {@link UserHandle user}
1596 * must be the calling user or a managed profile associated with it.
Jeff Sharkeyb6423872015-12-11 11:11:58 -07001597 *
1598 * @param user to retrieve the unlocked state for.
Jeff Sharkey8a372a02016-03-16 16:25:45 -06001599 * @see Intent#ACTION_USER_UNLOCKED
1600 * @see Context#createDeviceProtectedStorageContext()
Jeff Sharkeyb6423872015-12-11 11:11:58 -07001601 */
1602 public boolean isUserUnlocked(UserHandle user) {
Jeff Sharkey0999c0d2015-12-17 15:12:22 -07001603 return isUserUnlocked(user.getIdentifier());
1604 }
1605
1606 /** {@hide} */
Jeff Sharkey8588bc12016-01-06 16:47:42 -07001607 public boolean isUserUnlocked(@UserIdInt int userId) {
Jeff Sharkeyce18c812016-04-27 16:00:41 -06001608 try {
Fyodor Kupolov2e7e0962016-12-01 18:09:17 -08001609 return mService.isUserUnlocked(userId);
Jeff Sharkeyce18c812016-04-27 16:00:41 -06001610 } catch (RemoteException re) {
1611 throw re.rethrowFromSystemServer();
1612 }
1613 }
1614
1615 /** {@hide} */
1616 public boolean isUserUnlockingOrUnlocked(UserHandle user) {
1617 return isUserUnlockingOrUnlocked(user.getIdentifier());
1618 }
1619
1620 /** {@hide} */
1621 public boolean isUserUnlockingOrUnlocked(@UserIdInt int userId) {
1622 try {
Fyodor Kupolovc413f702016-10-06 17:11:14 -07001623 return mService.isUserUnlockingOrUnlocked(userId);
Jeff Sharkeyce18c812016-04-27 16:00:41 -06001624 } catch (RemoteException re) {
1625 throw re.rethrowFromSystemServer();
1626 }
Jeff Sharkeyb6423872015-12-11 11:11:58 -07001627 }
1628
1629 /**
Makoto Onuki73dded22017-12-20 13:14:48 +09001630 * Return the time when the calling user started in elapsed milliseconds since boot,
1631 * or 0 if not started.
1632 *
1633 * @hide
1634 */
1635 public long getUserStartRealtime() {
1636 try {
1637 return mService.getUserStartRealtime();
1638 } catch (RemoteException re) {
1639 throw re.rethrowFromSystemServer();
1640 }
1641 }
1642
1643 /**
1644 * Return the time when the calling user was unlocked elapsed milliseconds since boot,
1645 * or 0 if not unlocked.
1646 *
1647 * @hide
1648 */
1649 public long getUserUnlockRealtime() {
1650 try {
1651 return mService.getUserUnlockRealtime();
1652 } catch (RemoteException re) {
1653 throw re.rethrowFromSystemServer();
1654 }
1655 }
1656
1657 /**
Amith Yamasani258848d2012-08-10 17:06:33 -07001658 * Returns the UserInfo object describing a specific user.
Tony Mak8673b282016-03-21 21:10:59 +00001659 * Requires {@link android.Manifest.permission#MANAGE_USERS} permission.
Amith Yamasani258848d2012-08-10 17:06:33 -07001660 * @param userHandle the user handle of the user whose information is being requested.
1661 * @return the UserInfo object for a specific user.
1662 * @hide
Dianne Hackbornb26306a2012-10-24 15:22:21 -07001663 */
Jeff Sharkey8588bc12016-01-06 16:47:42 -07001664 public UserInfo getUserInfo(@UserIdInt int userHandle) {
Amith Yamasani258848d2012-08-10 17:06:33 -07001665 try {
1666 return mService.getUserInfo(userHandle);
1667 } catch (RemoteException re) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001668 throw re.rethrowFromSystemServer();
Amith Yamasani258848d2012-08-10 17:06:33 -07001669 }
1670 }
1671
Amith Yamasani71e6c692013-03-24 17:39:28 -07001672 /**
Zoltan Szatmary-Bane7834602016-04-08 18:41:11 +01001673 * @hide
1674 *
1675 * Returns who set a user restriction on a user.
Zoltan Szatmary-Bane7834602016-04-08 18:41:11 +01001676 * @param restrictionKey the string key representing the restriction
1677 * @param userHandle the UserHandle of the user for whom to retrieve the restrictions.
1678 * @return The source of user restriction. Any combination of {@link #RESTRICTION_NOT_SET},
1679 * {@link #RESTRICTION_SOURCE_SYSTEM}, {@link #RESTRICTION_SOURCE_DEVICE_OWNER}
1680 * and {@link #RESTRICTION_SOURCE_PROFILE_OWNER}
Pavel Grafov6a40f092016-10-25 15:46:51 +01001681 * @deprecated use {@link #getUserRestrictionSources(String, int)} instead.
Zoltan Szatmary-Bane7834602016-04-08 18:41:11 +01001682 */
Pavel Grafov6a40f092016-10-25 15:46:51 +01001683 @Deprecated
Zoltan Szatmary-Bane7834602016-04-08 18:41:11 +01001684 @SystemApi
1685 @UserRestrictionSource
Jeff Sharkeyd86b8fe2017-06-02 17:36:26 -06001686 @RequiresPermission(android.Manifest.permission.MANAGE_USERS)
Zoltan Szatmary-Bane7834602016-04-08 18:41:11 +01001687 public int getUserRestrictionSource(String restrictionKey, UserHandle userHandle) {
1688 try {
1689 return mService.getUserRestrictionSource(restrictionKey, userHandle.getIdentifier());
1690 } catch (RemoteException re) {
1691 throw re.rethrowFromSystemServer();
1692 }
1693 }
1694
1695 /**
Pavel Grafov6a40f092016-10-25 15:46:51 +01001696 * @hide
1697 *
1698 * Returns a list of users who set a user restriction on a given user.
Pavel Grafov6a40f092016-10-25 15:46:51 +01001699 * @param restrictionKey the string key representing the restriction
1700 * @param userHandle the UserHandle of the user for whom to retrieve the restrictions.
1701 * @return a list of user ids enforcing this restriction.
1702 */
1703 @SystemApi
Jeff Sharkeyd86b8fe2017-06-02 17:36:26 -06001704 @RequiresPermission(android.Manifest.permission.MANAGE_USERS)
Pavel Grafov6a40f092016-10-25 15:46:51 +01001705 public List<EnforcingUser> getUserRestrictionSources(
1706 String restrictionKey, UserHandle userHandle) {
1707 try {
1708 return mService.getUserRestrictionSources(restrictionKey, userHandle.getIdentifier());
1709 } catch (RemoteException re) {
1710 throw re.rethrowFromSystemServer();
1711 }
1712 }
1713
1714 /**
Amith Yamasani71e6c692013-03-24 17:39:28 -07001715 * Returns the user-wide restrictions imposed on this user.
1716 * @return a Bundle containing all the restrictions.
1717 */
Amith Yamasanie4cf7342012-12-17 11:12:09 -08001718 public Bundle getUserRestrictions() {
1719 return getUserRestrictions(Process.myUserHandle());
1720 }
1721
Amith Yamasani71e6c692013-03-24 17:39:28 -07001722 /**
1723 * Returns the user-wide restrictions imposed on the user specified by <code>userHandle</code>.
1724 * @param userHandle the UserHandle of the user for whom to retrieve the restrictions.
1725 * @return a Bundle containing all the restrictions.
1726 */
Amith Yamasanie4cf7342012-12-17 11:12:09 -08001727 public Bundle getUserRestrictions(UserHandle userHandle) {
1728 try {
1729 return mService.getUserRestrictions(userHandle.getIdentifier());
1730 } catch (RemoteException re) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001731 throw re.rethrowFromSystemServer();
Amith Yamasanie4cf7342012-12-17 11:12:09 -08001732 }
1733 }
1734
Zoltan Szatmary-Ban3bbcedd2015-11-26 13:45:51 +00001735 /**
1736 * @hide
1737 * Returns whether the given user has been disallowed from performing certain actions
Irina Dumitrescu4638edd2018-09-05 14:08:33 +01001738 * or setting certain settings through UserManager (e.g. this type of restriction would prevent
1739 * the guest user from doing certain things, such as making calls). This method disregards
1740 * restrictions set by device policy.
Zoltan Szatmary-Ban3bbcedd2015-11-26 13:45:51 +00001741 * @param restrictionKey the string key representing the restriction
1742 * @param userHandle the UserHandle of the user for whom to retrieve the restrictions.
1743 */
1744 public boolean hasBaseUserRestriction(String restrictionKey, UserHandle userHandle) {
1745 try {
1746 return mService.hasBaseUserRestriction(restrictionKey, userHandle.getIdentifier());
1747 } catch (RemoteException re) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001748 throw re.rethrowFromSystemServer();
Zoltan Szatmary-Ban3bbcedd2015-11-26 13:45:51 +00001749 }
1750 }
1751
Amith Yamasani71e6c692013-03-24 17:39:28 -07001752 /**
Makoto Onukia3c12502015-10-28 10:18:32 -07001753 * This will no longer work. Device owners and profile owners should use
1754 * {@link DevicePolicyManager#addUserRestriction(ComponentName, String)} instead.
Amith Yamasani71e6c692013-03-24 17:39:28 -07001755 */
Makoto Onukia3c12502015-10-28 10:18:32 -07001756 // System apps should use UserManager.setUserRestriction() instead.
Julia Reynolds3d9eb782014-08-11 16:40:08 -04001757 @Deprecated
Amith Yamasanie4cf7342012-12-17 11:12:09 -08001758 public void setUserRestrictions(Bundle restrictions) {
Makoto Onuki068c54a2015-10-13 14:34:03 -07001759 throw new UnsupportedOperationException("This method is no longer supported");
Amith Yamasanie4cf7342012-12-17 11:12:09 -08001760 }
1761
Amith Yamasani71e6c692013-03-24 17:39:28 -07001762 /**
Makoto Onukia3c12502015-10-28 10:18:32 -07001763 * This will no longer work. Device owners and profile owners should use
1764 * {@link DevicePolicyManager#addUserRestriction(ComponentName, String)} instead.
Amith Yamasani71e6c692013-03-24 17:39:28 -07001765 */
Makoto Onukia3c12502015-10-28 10:18:32 -07001766 // System apps should use UserManager.setUserRestriction() instead.
Julia Reynolds3d9eb782014-08-11 16:40:08 -04001767 @Deprecated
Amith Yamasanie4cf7342012-12-17 11:12:09 -08001768 public void setUserRestrictions(Bundle restrictions, UserHandle userHandle) {
Makoto Onuki068c54a2015-10-13 14:34:03 -07001769 throw new UnsupportedOperationException("This method is no longer supported");
Amith Yamasanie4cf7342012-12-17 11:12:09 -08001770 }
1771
Amith Yamasani71e6c692013-03-24 17:39:28 -07001772 /**
1773 * Sets the value of a specific restriction.
Amith Yamasanibe465322014-04-24 13:45:17 -07001774 * Requires the MANAGE_USERS permission.
Amith Yamasani71e6c692013-03-24 17:39:28 -07001775 * @param key the key of the restriction
1776 * @param value the value for the restriction
Julia Reynolds3d9eb782014-08-11 16:40:08 -04001777 * @deprecated use {@link android.app.admin.DevicePolicyManager#addUserRestriction(
1778 * android.content.ComponentName, String)} or
1779 * {@link android.app.admin.DevicePolicyManager#clearUserRestriction(
1780 * android.content.ComponentName, String)} instead.
Amith Yamasani71e6c692013-03-24 17:39:28 -07001781 */
Julia Reynolds3d9eb782014-08-11 16:40:08 -04001782 @Deprecated
Amith Yamasani71e6c692013-03-24 17:39:28 -07001783 public void setUserRestriction(String key, boolean value) {
Makoto Onuki068c54a2015-10-13 14:34:03 -07001784 setUserRestriction(key, value, Process.myUserHandle());
Amith Yamasani71e6c692013-03-24 17:39:28 -07001785 }
1786
1787 /**
1788 * @hide
1789 * Sets the value of a specific restriction on a specific user.
Amith Yamasanibe465322014-04-24 13:45:17 -07001790 * Requires the MANAGE_USERS permission.
Amith Yamasani71e6c692013-03-24 17:39:28 -07001791 * @param key the key of the restriction
1792 * @param value the value for the restriction
1793 * @param userHandle the user whose restriction is to be changed.
Julia Reynolds3d9eb782014-08-11 16:40:08 -04001794 * @deprecated use {@link android.app.admin.DevicePolicyManager#addUserRestriction(
1795 * android.content.ComponentName, String)} or
1796 * {@link android.app.admin.DevicePolicyManager#clearUserRestriction(
1797 * android.content.ComponentName, String)} instead.
Amith Yamasani71e6c692013-03-24 17:39:28 -07001798 */
Julia Reynolds3d9eb782014-08-11 16:40:08 -04001799 @Deprecated
Maggie Benthall67944582013-02-22 14:58:27 -05001800 public void setUserRestriction(String key, boolean value, UserHandle userHandle) {
Fyodor Kupolovb5013302015-04-17 17:59:14 -07001801 try {
1802 mService.setUserRestriction(key, value, userHandle.getIdentifier());
1803 } catch (RemoteException re) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001804 throw re.rethrowFromSystemServer();
Fyodor Kupolovb5013302015-04-17 17:59:14 -07001805 }
Maggie Benthall67944582013-02-22 14:58:27 -05001806 }
1807
Amith Yamasani258848d2012-08-10 17:06:33 -07001808 /**
Maggie Benthalla12fccf2013-03-14 18:02:12 -04001809 * Returns whether the current user has been disallowed from performing certain actions
1810 * or setting certain settings.
Julia Reynolds2b2cf722014-06-06 11:41:04 -04001811 *
1812 * @param restrictionKey The string key representing the restriction.
1813 * @return {@code true} if the current user has the given restriction, {@code false} otherwise.
Maggie Benthalla12fccf2013-03-14 18:02:12 -04001814 */
1815 public boolean hasUserRestriction(String restrictionKey) {
David Christieb12ba932013-09-03 17:15:28 -07001816 return hasUserRestriction(restrictionKey, Process.myUserHandle());
1817 }
1818
1819 /**
1820 * @hide
1821 * Returns whether the given user has been disallowed from performing certain actions
1822 * or setting certain settings.
1823 * @param restrictionKey the string key representing the restriction
1824 * @param userHandle the UserHandle of the user for whom to retrieve the restrictions.
1825 */
1826 public boolean hasUserRestriction(String restrictionKey, UserHandle userHandle) {
Amith Yamasani8cd28b52014-06-08 17:54:27 -07001827 try {
1828 return mService.hasUserRestriction(restrictionKey,
1829 userHandle.getIdentifier());
1830 } catch (RemoteException re) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001831 throw re.rethrowFromSystemServer();
Amith Yamasani8cd28b52014-06-08 17:54:27 -07001832 }
Maggie Benthalla12fccf2013-03-14 18:02:12 -04001833 }
1834
1835 /**
Makoto Onukiacc50462018-02-14 14:13:49 -08001836 * @hide
1837 * Returns whether any user on the device has the given user restriction set.
1838 */
1839 public boolean hasUserRestrictionOnAnyUser(String restrictionKey) {
1840 try {
1841 return mService.hasUserRestrictionOnAnyUser(restrictionKey);
1842 } catch (RemoteException re) {
1843 throw re.rethrowFromSystemServer();
1844 }
1845 }
1846
1847 /**
Dianne Hackborn33f9cb82012-10-04 17:15:10 -07001848 * Return the serial number for a user. This is a device-unique
Dianne Hackbornb26306a2012-10-24 15:22:21 -07001849 * number assigned to that user; if the user is deleted and then a new
1850 * user created, the new users will not be given the same serial number.
Dianne Hackborn33f9cb82012-10-04 17:15:10 -07001851 * @param user The user whose serial number is to be retrieved.
Dianne Hackbornb26306a2012-10-24 15:22:21 -07001852 * @return The serial number of the given user; returns -1 if the
1853 * given UserHandle does not exist.
Dianne Hackborn33f9cb82012-10-04 17:15:10 -07001854 * @see #getUserForSerialNumber(long)
1855 */
1856 public long getSerialNumberForUser(UserHandle user) {
1857 return getUserSerialNumber(user.getIdentifier());
1858 }
1859
1860 /**
1861 * Return the user associated with a serial number previously
1862 * returned by {@link #getSerialNumberForUser(UserHandle)}.
1863 * @param serialNumber The serial number of the user that is being
1864 * retrieved.
1865 * @return Return the user associated with the serial number, or null
1866 * if there is not one.
1867 * @see #getSerialNumberForUser(UserHandle)
1868 */
1869 public UserHandle getUserForSerialNumber(long serialNumber) {
Fyodor Kupolovef249092015-05-06 13:18:46 -07001870 int ident = getUserHandle((int) serialNumber);
Dianne Hackborn33f9cb82012-10-04 17:15:10 -07001871 return ident >= 0 ? new UserHandle(ident) : null;
1872 }
1873
1874 /**
Xiaohui Chencfe64c82015-07-16 14:30:50 -07001875 * Creates a user with the specified name and options. For non-admin users, default user
1876 * restrictions are going to be applied.
Amith Yamasani195263742012-08-21 15:40:12 -07001877 * Requires {@link android.Manifest.permission#MANAGE_USERS} permission.
Amith Yamasani258848d2012-08-10 17:06:33 -07001878 *
1879 * @param name the user's name
1880 * @param flags flags that identify the type of user and other properties.
1881 * @see UserInfo
1882 *
1883 * @return the UserInfo object for the created user, or null if the user could not be created.
1884 * @hide
1885 */
1886 public UserInfo createUser(String name, int flags) {
Xiaohui Chencfe64c82015-07-16 14:30:50 -07001887 UserInfo user = null;
Amith Yamasani258848d2012-08-10 17:06:33 -07001888 try {
Xiaohui Chencfe64c82015-07-16 14:30:50 -07001889 user = mService.createUser(name, flags);
phweisse9c44062016-02-10 12:57:38 +01001890 // TODO: Keep this in sync with
1891 // UserManagerService.LocalService.createUserEvenWhenDisallowed
Christine Franks97a54802017-08-09 10:06:43 -07001892 if (user != null && !user.isAdmin() && !user.isDemo()) {
Makoto Onuki068c54a2015-10-13 14:34:03 -07001893 mService.setUserRestriction(DISALLOW_SMS, true, user.id);
1894 mService.setUserRestriction(DISALLOW_OUTGOING_CALLS, true, user.id);
Xiaohui Chencfe64c82015-07-16 14:30:50 -07001895 }
Amith Yamasani258848d2012-08-10 17:06:33 -07001896 } catch (RemoteException re) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001897 throw re.rethrowFromSystemServer();
Amith Yamasani258848d2012-08-10 17:06:33 -07001898 }
Xiaohui Chencfe64c82015-07-16 14:30:50 -07001899 return user;
Amith Yamasani258848d2012-08-10 17:06:33 -07001900 }
1901
1902 /**
Amith Yamasani1e9c2182014-06-11 17:25:51 -07001903 * Creates a guest user and configures it.
1904 * @param context an application context
1905 * @param name the name to set for the user
1906 * @hide
1907 */
1908 public UserInfo createGuest(Context context, String name) {
Makoto Onuki068c54a2015-10-13 14:34:03 -07001909 UserInfo guest = null;
1910 try {
1911 guest = mService.createUser(name, UserInfo.FLAG_GUEST);
1912 if (guest != null) {
1913 Settings.Secure.putStringForUser(context.getContentResolver(),
1914 Settings.Secure.SKIP_FIRST_USE_HINTS, "1", guest.id);
Amith Yamasanibf3a9462014-07-28 14:26:42 -07001915 }
Makoto Onuki068c54a2015-10-13 14:34:03 -07001916 } catch (RemoteException re) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001917 throw re.rethrowFromSystemServer();
Amith Yamasani1e9c2182014-06-11 17:25:51 -07001918 }
1919 return guest;
1920 }
1921
Amith Yamasaniaa6634e2014-10-06 14:20:28 -07001922 /**
Kenny Guy2a764942014-04-02 13:29:20 +01001923 * Creates a user with the specified name and options as a profile of another user.
Kenny Guya52dc3e2014-02-11 15:33:14 +00001924 * Requires {@link android.Manifest.permission#MANAGE_USERS} permission.
1925 *
1926 * @param name the user's name
1927 * @param flags flags that identify the type of user and other properties.
Sudheer Shanka7cb54a32016-09-16 12:59:05 -07001928 * @param userHandle new user will be a profile of this user.
Kenny Guya52dc3e2014-02-11 15:33:14 +00001929 *
Sudheer Shanka7cb54a32016-09-16 12:59:05 -07001930 * @return the {@link UserInfo} object for the created user, or null if the user
1931 * could not be created.
Kenny Guya52dc3e2014-02-11 15:33:14 +00001932 * @hide
1933 */
Jeff Sharkey8588bc12016-01-06 16:47:42 -07001934 public UserInfo createProfileForUser(String name, int flags, @UserIdInt int userHandle) {
Sudheer Shanka7cb54a32016-09-16 12:59:05 -07001935 return createProfileForUser(name, flags, userHandle, null);
1936 }
1937
1938 /**
1939 * Version of {@link #createProfileForUser(String, int, int)} that allows you to specify
1940 * any packages that should not be installed in the new profile by default, these packages can
1941 * still be installed later by the user if needed.
1942 *
1943 * @param name the user's name
1944 * @param flags flags that identify the type of user and other properties.
1945 * @param userHandle new user will be a profile of this user.
1946 * @param disallowedPackages packages that will not be installed in the profile being created.
1947 *
1948 * @return the {@link UserInfo} object for the created user, or null if the user
1949 * could not be created.
1950 * @hide
1951 */
1952 public UserInfo createProfileForUser(String name, int flags, @UserIdInt int userHandle,
1953 String[] disallowedPackages) {
Kenny Guya52dc3e2014-02-11 15:33:14 +00001954 try {
Sudheer Shanka7cb54a32016-09-16 12:59:05 -07001955 return mService.createProfileForUser(name, flags, userHandle, disallowedPackages);
Kenny Guya52dc3e2014-02-11 15:33:14 +00001956 } catch (RemoteException re) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001957 throw re.rethrowFromSystemServer();
Kenny Guya52dc3e2014-02-11 15:33:14 +00001958 }
1959 }
1960
1961 /**
Tony Mak6dc428f2016-10-10 15:48:27 +01001962 * Similar to {@link #createProfileForUser(String, int, int, String[])}
Esteban Talavera6c9116a2016-11-24 16:12:44 +00001963 * except bypassing the checking of {@link UserManager#DISALLOW_ADD_MANAGED_PROFILE}.
Tony Mak6dc428f2016-10-10 15:48:27 +01001964 * Requires {@link android.Manifest.permission#MANAGE_USERS} permission.
1965 *
1966 * @see #createProfileForUser(String, int, int, String[])
1967 * @hide
1968 */
1969 public UserInfo createProfileForUserEvenWhenDisallowed(String name, int flags,
1970 @UserIdInt int userHandle, String[] disallowedPackages) {
1971 try {
1972 return mService.createProfileForUserEvenWhenDisallowed(name, flags, userHandle,
1973 disallowedPackages);
1974 } catch (RemoteException re) {
1975 throw re.rethrowFromSystemServer();
1976 }
1977 }
1978
1979 /**
Fyodor Kupolov02cb6e72015-09-18 18:20:55 -07001980 * Creates a restricted profile with the specified name. This method also sets necessary
1981 * restrictions and adds shared accounts.
Fyodor Kupolov06a484a2015-08-21 16:33:20 -07001982 *
1983 * @param name profile's name
1984 * @return UserInfo object for the created user, or null if the user could not be created.
1985 * @hide
1986 */
1987 public UserInfo createRestrictedProfile(String name) {
1988 try {
Fyodor Kupolov02cb6e72015-09-18 18:20:55 -07001989 UserHandle parentUserHandle = Process.myUserHandle();
1990 UserInfo user = mService.createRestrictedProfile(name,
1991 parentUserHandle.getIdentifier());
1992 if (user != null) {
1993 AccountManager.get(mContext).addSharedAccountsFromParentUser(parentUserHandle,
1994 UserHandle.of(user.id));
Fyodor Kupolov06a484a2015-08-21 16:33:20 -07001995 }
Fyodor Kupolov02cb6e72015-09-18 18:20:55 -07001996 return user;
Jeff Sharkey27b2e692016-02-25 17:40:12 -07001997 } catch (RemoteException re) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001998 throw re.rethrowFromSystemServer();
Fyodor Kupolov06a484a2015-08-21 16:33:20 -07001999 }
Fyodor Kupolov06a484a2015-08-21 16:33:20 -07002000 }
2001
2002 /**
Amith Yamasani06964342016-04-15 13:55:01 -07002003 * Returns an intent to create a user for the provided name and account name. The name
2004 * and account name will be used when the setup process for the new user is started.
2005 * <p>
Amith Yamasani12747872015-12-07 14:19:49 -08002006 * The intent should be launched using startActivityForResult and the return result will
Amith Yamasani37ed8d12016-01-27 14:40:16 -08002007 * indicate if the user consented to adding a new user and if the operation succeeded. Any
2008 * errors in creating the user will be returned in the result code. If the user cancels the
2009 * request, the return result will be {@link Activity#RESULT_CANCELED}. On success, the
2010 * result code will be {@link Activity#RESULT_OK}.
Amith Yamasani06964342016-04-15 13:55:01 -07002011 * <p>
2012 * Use {@link #supportsMultipleUsers()} to first check if the device supports this operation
2013 * at all.
2014 * <p>
Amith Yamasani12747872015-12-07 14:19:49 -08002015 * The new user is created but not initialized. After switching into the user for the first
2016 * time, the preferred user name and account information are used by the setup process for that
2017 * user.
2018 *
2019 * @param userName Optional name to assign to the user.
Amith Yamasani06964342016-04-15 13:55:01 -07002020 * @param accountName Optional account name that will be used by the setup wizard to initialize
Amith Yamasani12747872015-12-07 14:19:49 -08002021 * the user.
2022 * @param accountType Optional account type for the account to be created. This is required
2023 * if the account name is specified.
2024 * @param accountOptions Optional bundle of data to be passed in during account creation in the
2025 * new user via {@link AccountManager#addAccount(String, String, String[],
2026 * Bundle, android.app.Activity, android.accounts.AccountManagerCallback,
2027 * Handler)}.
Amith Yamasani06964342016-04-15 13:55:01 -07002028 * @return An Intent that can be launched from an Activity.
Amith Yamasani37ed8d12016-01-27 14:40:16 -08002029 * @see #USER_CREATION_FAILED_NOT_PERMITTED
2030 * @see #USER_CREATION_FAILED_NO_MORE_USERS
Amith Yamasani06964342016-04-15 13:55:01 -07002031 * @see #supportsMultipleUsers
Amith Yamasani12747872015-12-07 14:19:49 -08002032 */
2033 public static Intent createUserCreationIntent(@Nullable String userName,
2034 @Nullable String accountName,
2035 @Nullable String accountType, @Nullable PersistableBundle accountOptions) {
Amith Yamasani12747872015-12-07 14:19:49 -08002036 Intent intent = new Intent(ACTION_CREATE_USER);
2037 if (userName != null) {
2038 intent.putExtra(EXTRA_USER_NAME, userName);
2039 }
2040 if (accountName != null && accountType == null) {
2041 throw new IllegalArgumentException("accountType must be specified if accountName is "
2042 + "specified");
2043 }
2044 if (accountName != null) {
2045 intent.putExtra(EXTRA_USER_ACCOUNT_NAME, accountName);
2046 }
2047 if (accountType != null) {
2048 intent.putExtra(EXTRA_USER_ACCOUNT_TYPE, accountType);
2049 }
2050 if (accountOptions != null) {
2051 intent.putExtra(EXTRA_USER_ACCOUNT_OPTIONS, accountOptions);
2052 }
2053 return intent;
2054 }
2055
2056 /**
2057 * @hide
2058 *
Jeff Sharkeyd86b8fe2017-06-02 17:36:26 -06002059 * Returns the preferred account name for user creation.
Amith Yamasani12747872015-12-07 14:19:49 -08002060 */
2061 @SystemApi
Jeff Sharkeyd86b8fe2017-06-02 17:36:26 -06002062 @RequiresPermission(android.Manifest.permission.MANAGE_USERS)
Amith Yamasani12747872015-12-07 14:19:49 -08002063 public String getSeedAccountName() {
2064 try {
2065 return mService.getSeedAccountName();
2066 } catch (RemoteException re) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07002067 throw re.rethrowFromSystemServer();
Amith Yamasani12747872015-12-07 14:19:49 -08002068 }
2069 }
2070
2071 /**
2072 * @hide
2073 *
Jeff Sharkeyd86b8fe2017-06-02 17:36:26 -06002074 * Returns the preferred account type for user creation.
Amith Yamasani12747872015-12-07 14:19:49 -08002075 */
2076 @SystemApi
Jeff Sharkeyd86b8fe2017-06-02 17:36:26 -06002077 @RequiresPermission(android.Manifest.permission.MANAGE_USERS)
Amith Yamasani12747872015-12-07 14:19:49 -08002078 public String getSeedAccountType() {
2079 try {
2080 return mService.getSeedAccountType();
2081 } catch (RemoteException re) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07002082 throw re.rethrowFromSystemServer();
Amith Yamasani12747872015-12-07 14:19:49 -08002083 }
2084 }
2085
2086 /**
2087 * @hide
2088 *
Jeff Sharkeyd86b8fe2017-06-02 17:36:26 -06002089 * Returns the preferred account's options bundle for user creation.
Amith Yamasani12747872015-12-07 14:19:49 -08002090 * @return Any options set by the requestor that created the user.
2091 */
2092 @SystemApi
Jeff Sharkeyd86b8fe2017-06-02 17:36:26 -06002093 @RequiresPermission(android.Manifest.permission.MANAGE_USERS)
Amith Yamasani12747872015-12-07 14:19:49 -08002094 public PersistableBundle getSeedAccountOptions() {
2095 try {
2096 return mService.getSeedAccountOptions();
2097 } catch (RemoteException re) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07002098 throw re.rethrowFromSystemServer();
Amith Yamasani12747872015-12-07 14:19:49 -08002099 }
2100 }
2101
2102 /**
2103 * @hide
2104 *
2105 * Called by a system activity to set the seed account information of a user created
2106 * through the user creation intent.
2107 * @param userId
2108 * @param accountName
2109 * @param accountType
2110 * @param accountOptions
2111 * @see #createUserCreationIntent(String, String, String, PersistableBundle)
2112 */
2113 public void setSeedAccountData(int userId, String accountName, String accountType,
2114 PersistableBundle accountOptions) {
2115 try {
2116 mService.setSeedAccountData(userId, accountName, accountType, accountOptions,
2117 /* persist= */ true);
2118 } catch (RemoteException re) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07002119 throw re.rethrowFromSystemServer();
Amith Yamasani12747872015-12-07 14:19:49 -08002120 }
2121 }
2122
2123 /**
2124 * @hide
Jeff Sharkeyd86b8fe2017-06-02 17:36:26 -06002125 * Clears the seed information used to create this user.
Amith Yamasani12747872015-12-07 14:19:49 -08002126 */
2127 @SystemApi
Jeff Sharkeyd86b8fe2017-06-02 17:36:26 -06002128 @RequiresPermission(android.Manifest.permission.MANAGE_USERS)
Amith Yamasani12747872015-12-07 14:19:49 -08002129 public void clearSeedAccountData() {
2130 try {
2131 mService.clearSeedAccountData();
2132 } catch (RemoteException re) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07002133 throw re.rethrowFromSystemServer();
Amith Yamasani12747872015-12-07 14:19:49 -08002134 }
2135 }
2136
2137 /**
Amith Yamasani1df14732014-08-29 21:37:27 -07002138 * @hide
2139 * Marks the guest user for deletion to allow a new guest to be created before deleting
2140 * the current user who is a guest.
2141 * @param userHandle
2142 * @return
2143 */
Jeff Sharkey8588bc12016-01-06 16:47:42 -07002144 public boolean markGuestForDeletion(@UserIdInt int userHandle) {
Amith Yamasani1df14732014-08-29 21:37:27 -07002145 try {
2146 return mService.markGuestForDeletion(userHandle);
2147 } catch (RemoteException re) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07002148 throw re.rethrowFromSystemServer();
Amith Yamasani1df14732014-08-29 21:37:27 -07002149 }
2150 }
2151
2152 /**
Alexandra Gherghinadf35d572014-04-09 13:54:39 +01002153 * Sets the user as enabled, if such an user exists.
Lenka Trochtova1ddda472016-02-12 10:42:12 +01002154 *
2155 * <p>Requires {@link android.Manifest.permission#MANAGE_USERS} permission.
2156 *
2157 * <p>Note that the default is true, it's only that managed profiles might not be enabled.
2158 * Also ephemeral users can be disabled to indicate that their removal is in progress and they
2159 * shouldn't be re-entered. Therefore ephemeral users should not be re-enabled once disabled.
Alexandra Gherghinadf35d572014-04-09 13:54:39 +01002160 *
jovanakf24ad492018-05-18 12:15:59 -07002161 * @param userId the id of the profile to enable
Alexandra Gherghinadf35d572014-04-09 13:54:39 +01002162 * @hide
2163 */
jovanakf24ad492018-05-18 12:15:59 -07002164 public void setUserEnabled(@UserIdInt int userId) {
Alexandra Gherghinadf35d572014-04-09 13:54:39 +01002165 try {
jovanakf24ad492018-05-18 12:15:59 -07002166 mService.setUserEnabled(userId);
2167 } catch (RemoteException re) {
2168 throw re.rethrowFromSystemServer();
2169 }
2170 }
2171
2172 /**
2173 * Assigns admin privileges to the user, if such a user exists.
2174 *
2175 * <p>Requires {@link android.Manifest.permission#MANAGE_USERS} and
2176 * {@link android.Manifest.permission#INTERACT_ACROSS_USERS_FULL} permissions.
2177 *
2178 * @param userHandle the id of the user to become admin
2179 * @hide
2180 */
2181 @RequiresPermission(allOf = {
2182 Manifest.permission.INTERACT_ACROSS_USERS_FULL,
2183 Manifest.permission.MANAGE_USERS
2184 })
2185 public void setUserAdmin(@UserIdInt int userHandle) {
2186 try {
2187 mService.setUserAdmin(userHandle);
Jeff Sharkey27b2e692016-02-25 17:40:12 -07002188 } catch (RemoteException re) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07002189 throw re.rethrowFromSystemServer();
Alexandra Gherghinadf35d572014-04-09 13:54:39 +01002190 }
2191 }
2192
2193 /**
Andrew Scull85a63bc2016-10-24 13:47:47 +01002194 * Evicts the user's credential encryption key from memory by stopping and restarting the user.
2195 *
2196 * @hide
2197 */
2198 public void evictCredentialEncryptionKey(@UserIdInt int userHandle) {
2199 try {
2200 mService.evictCredentialEncryptionKey(userHandle);
2201 } catch (RemoteException re) {
2202 throw re.rethrowFromSystemServer();
2203 }
2204 }
2205
2206 /**
Dianne Hackbornb26306a2012-10-24 15:22:21 -07002207 * Return the number of users currently created on the device.
2208 */
2209 public int getUserCount() {
2210 List<UserInfo> users = getUsers();
2211 return users != null ? users.size() : 1;
2212 }
2213
2214 /**
Amith Yamasanid04aaa32016-06-13 12:09:36 -07002215 * Returns information for all users on this device, including ones marked for deletion.
2216 * To retrieve only users that are alive, use {@link #getUsers(boolean)}.
2217 * <p>
Amith Yamasani195263742012-08-21 15:40:12 -07002218 * Requires {@link android.Manifest.permission#MANAGE_USERS} permission.
Fyodor Kupolov940e8572016-01-26 12:03:51 -08002219 * @return the list of users that exist on the device.
Amith Yamasani258848d2012-08-10 17:06:33 -07002220 * @hide
2221 */
2222 public List<UserInfo> getUsers() {
2223 try {
Amith Yamasani920ace02012-09-20 22:15:37 -07002224 return mService.getUsers(false);
2225 } catch (RemoteException re) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07002226 throw re.rethrowFromSystemServer();
Amith Yamasani920ace02012-09-20 22:15:37 -07002227 }
2228 }
2229
2230 /**
Fyodor Kupolov940e8572016-01-26 12:03:51 -08002231 * Returns serial numbers of all users on this device.
Fyodor Kupolov940e8572016-01-26 12:03:51 -08002232 *
2233 * @param excludeDying specify if the list should exclude users being removed.
2234 * @return the list of serial numbers of users that exist on the device.
2235 * @hide
2236 */
2237 @SystemApi
Jeff Sharkeyd86b8fe2017-06-02 17:36:26 -06002238 @RequiresPermission(android.Manifest.permission.MANAGE_USERS)
Fyodor Kupolov940e8572016-01-26 12:03:51 -08002239 public long[] getSerialNumbersOfUsers(boolean excludeDying) {
2240 try {
2241 List<UserInfo> users = mService.getUsers(excludeDying);
2242 long[] result = new long[users.size()];
2243 for (int i = 0; i < result.length; i++) {
2244 result[i] = users.get(i).serialNumber;
2245 }
2246 return result;
2247 } catch (RemoteException re) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07002248 throw re.rethrowFromSystemServer();
Fyodor Kupolov940e8572016-01-26 12:03:51 -08002249 }
2250 }
2251
2252 /**
Xiaohui Chenb3b92582015-12-07 11:22:13 -08002253 * @return the user's account name, null if not found.
2254 * @hide
2255 */
2256 @RequiresPermission( allOf = {
2257 Manifest.permission.INTERACT_ACROSS_USERS_FULL,
2258 Manifest.permission.MANAGE_USERS
2259 })
Jeff Sharkey8588bc12016-01-06 16:47:42 -07002260 public @Nullable String getUserAccount(@UserIdInt int userHandle) {
Xiaohui Chenb3b92582015-12-07 11:22:13 -08002261 try {
2262 return mService.getUserAccount(userHandle);
2263 } catch (RemoteException re) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07002264 throw re.rethrowFromSystemServer();
Xiaohui Chenb3b92582015-12-07 11:22:13 -08002265 }
2266 }
2267
2268 /**
2269 * Set account name for the given user.
2270 * @hide
2271 */
2272 @RequiresPermission( allOf = {
2273 Manifest.permission.INTERACT_ACROSS_USERS_FULL,
2274 Manifest.permission.MANAGE_USERS
2275 })
Jeff Sharkey8588bc12016-01-06 16:47:42 -07002276 public void setUserAccount(@UserIdInt int userHandle, @Nullable String accountName) {
Xiaohui Chenb3b92582015-12-07 11:22:13 -08002277 try {
2278 mService.setUserAccount(userHandle, accountName);
2279 } catch (RemoteException re) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07002280 throw re.rethrowFromSystemServer();
Xiaohui Chenb3b92582015-12-07 11:22:13 -08002281 }
2282 }
2283
2284 /**
Xiaohui Chen70f6c382015-04-28 14:21:43 -07002285 * Returns information for Primary user.
2286 * Requires {@link android.Manifest.permission#MANAGE_USERS} permission.
2287 *
2288 * @return the Primary user, null if not found.
2289 * @hide
2290 */
Xiaohui Chen7cb69df2015-07-13 16:01:01 -07002291 public @Nullable UserInfo getPrimaryUser() {
Xiaohui Chen70f6c382015-04-28 14:21:43 -07002292 try {
2293 return mService.getPrimaryUser();
2294 } catch (RemoteException re) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07002295 throw re.rethrowFromSystemServer();
Xiaohui Chen70f6c382015-04-28 14:21:43 -07002296 }
2297 }
2298
2299 /**
Amith Yamasani95ab7842014-08-11 17:09:26 -07002300 * Checks whether it's possible to add more users. Caller must hold the MANAGE_USERS
2301 * permission.
2302 *
2303 * @return true if more users can be added, false if limit has been reached.
2304 * @hide
2305 */
2306 public boolean canAddMoreUsers() {
2307 final List<UserInfo> users = getUsers(true);
2308 final int totalUserCount = users.size();
2309 int aliveUserCount = 0;
2310 for (int i = 0; i < totalUserCount; i++) {
2311 UserInfo user = users.get(i);
2312 if (!user.isGuest()) {
2313 aliveUserCount++;
2314 }
2315 }
2316 return aliveUserCount < getMaxSupportedUsers();
2317 }
2318
2319 /**
Nicolas Prevot72434b72015-05-13 12:15:03 -07002320 * Checks whether it's possible to add more managed profiles. Caller must hold the MANAGE_USERS
2321 * permission.
Nicolas Prevot07387fe2015-10-30 17:53:30 +00002322 * if allowedToRemoveOne is true and if the user already has a managed profile, then return if
2323 * we could add a new managed profile to this user after removing the existing one.
Nicolas Prevot72434b72015-05-13 12:15:03 -07002324 *
2325 * @return true if more managed profiles can be added, false if limit has been reached.
2326 * @hide
2327 */
Jeff Sharkey8588bc12016-01-06 16:47:42 -07002328 public boolean canAddMoreManagedProfiles(@UserIdInt int userId, boolean allowedToRemoveOne) {
Nicolas Prevot72434b72015-05-13 12:15:03 -07002329 try {
Nicolas Prevot07387fe2015-10-30 17:53:30 +00002330 return mService.canAddMoreManagedProfiles(userId, allowedToRemoveOne);
Nicolas Prevot72434b72015-05-13 12:15:03 -07002331 } catch (RemoteException re) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07002332 throw re.rethrowFromSystemServer();
Nicolas Prevot72434b72015-05-13 12:15:03 -07002333 }
2334 }
2335
2336 /**
Kenny Guy2a764942014-04-02 13:29:20 +01002337 * Returns list of the profiles of userHandle including
2338 * userHandle itself.
Amith Yamasani4f7e2e32014-08-14 18:49:48 -07002339 * Note that this returns both enabled and not enabled profiles. See
Ruben Brunk7f75da22015-04-30 17:46:30 -07002340 * {@link #getEnabledProfiles(int)} if you need only the enabled ones.
Amith Yamasani4f582632014-02-19 14:31:52 -08002341 *
Kenny Guy2a764942014-04-02 13:29:20 +01002342 * Requires {@link android.Manifest.permission#MANAGE_USERS} permission.
2343 * @param userHandle profiles of this user will be returned.
2344 * @return the list of profiles.
2345 * @hide
2346 */
Jeff Sharkey8588bc12016-01-06 16:47:42 -07002347 public List<UserInfo> getProfiles(@UserIdInt int userHandle) {
Kenny Guya52dc3e2014-02-11 15:33:14 +00002348 try {
Alexandra Gherghina385124d2014-04-03 13:37:39 +01002349 return mService.getProfiles(userHandle, false /* enabledOnly */);
Kenny Guya52dc3e2014-02-11 15:33:14 +00002350 } catch (RemoteException re) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07002351 throw re.rethrowFromSystemServer();
Kenny Guya52dc3e2014-02-11 15:33:14 +00002352 }
2353 }
2354
2355 /**
Xiaohui Chenfd5b7742015-10-14 15:47:04 -07002356 * Requires {@link android.Manifest.permission#MANAGE_USERS} permission.
2357 * @param userId one of the two user ids to check.
2358 * @param otherUserId one of the two user ids to check.
2359 * @return true if the two user ids are in the same profile group.
2360 * @hide
2361 */
Jeff Sharkey8588bc12016-01-06 16:47:42 -07002362 public boolean isSameProfileGroup(@UserIdInt int userId, int otherUserId) {
Xiaohui Chenfd5b7742015-10-14 15:47:04 -07002363 try {
2364 return mService.isSameProfileGroup(userId, otherUserId);
2365 } catch (RemoteException re) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07002366 throw re.rethrowFromSystemServer();
Xiaohui Chenfd5b7742015-10-14 15:47:04 -07002367 }
2368 }
2369
2370 /**
Ruben Brunk7f75da22015-04-30 17:46:30 -07002371 * Returns list of the profiles of userHandle including
2372 * userHandle itself.
2373 * Note that this returns only enabled.
2374 *
2375 * Requires {@link android.Manifest.permission#MANAGE_USERS} permission.
2376 * @param userHandle profiles of this user will be returned.
2377 * @return the list of profiles.
2378 * @hide
2379 */
Jeff Sharkey8588bc12016-01-06 16:47:42 -07002380 public List<UserInfo> getEnabledProfiles(@UserIdInt int userHandle) {
Ruben Brunk7f75da22015-04-30 17:46:30 -07002381 try {
2382 return mService.getProfiles(userHandle, true /* enabledOnly */);
2383 } catch (RemoteException re) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07002384 throw re.rethrowFromSystemServer();
Ruben Brunk7f75da22015-04-30 17:46:30 -07002385 }
2386 }
2387
2388 /**
Jessica Hummelbe81c802014-04-22 15:49:22 +01002389 * Returns a list of UserHandles for profiles associated with the user that the calling process
2390 * is running on, including the user itself.
Amith Yamasani4f582632014-02-19 14:31:52 -08002391 *
2392 * @return A non-empty list of UserHandles associated with the calling user.
2393 */
2394 public List<UserHandle> getUserProfiles() {
Fyodor Kupolov7f98aa42016-04-07 14:56:25 -07002395 int[] userIds = getProfileIds(UserHandle.myUserId(), true /* enabledOnly */);
2396 List<UserHandle> result = new ArrayList<>(userIds.length);
2397 for (int userId : userIds) {
2398 result.add(UserHandle.of(userId));
2399 }
2400 return result;
2401 }
2402
2403 /**
2404 * Returns a list of ids for profiles associated with the specified user including the user
2405 * itself.
2406 *
2407 * @param userId id of the user to return profiles for
2408 * @param enabledOnly whether return only {@link UserInfo#isEnabled() enabled} profiles
2409 * @return A non-empty list of ids of profiles associated with the specified user.
2410 *
2411 * @hide
2412 */
Philip P. Moltmann4e615e62018-08-28 14:57:49 -07002413 @RequiresPermission(anyOf = {Manifest.permission.MANAGE_USERS,
2414 Manifest.permission.CREATE_USERS}, conditional = true)
Philip P. Moltmann4e615e62018-08-28 14:57:49 -07002415 public @NonNull int[] getProfileIds(@UserIdInt int userId, boolean enabledOnly) {
Alexandra Gherghina385124d2014-04-03 13:37:39 +01002416 try {
Fyodor Kupolov7f98aa42016-04-07 14:56:25 -07002417 return mService.getProfileIds(userId, enabledOnly);
Alexandra Gherghina385124d2014-04-03 13:37:39 +01002418 } catch (RemoteException re) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07002419 throw re.rethrowFromSystemServer();
Alexandra Gherghina385124d2014-04-03 13:37:39 +01002420 }
Fyodor Kupolov7f98aa42016-04-07 14:56:25 -07002421 }
2422
2423 /**
2424 * @see #getProfileIds(int, boolean)
2425 * @hide
2426 */
2427 public int[] getProfileIdsWithDisabled(@UserIdInt int userId) {
2428 return getProfileIds(userId, false /* enabledOnly */);
2429 }
2430
2431 /**
2432 * @see #getProfileIds(int, boolean)
2433 * @hide
2434 */
2435 public int[] getEnabledProfileIds(@UserIdInt int userId) {
2436 return getProfileIds(userId, true /* enabledOnly */);
Amith Yamasani4f582632014-02-19 14:31:52 -08002437 }
2438
Amith Yamasani7dda2652014-04-11 14:57:12 -07002439 /**
Andres Moralesc5548c02015-08-05 10:23:12 -07002440 * Returns the device credential owner id of the profile from
2441 * which this method is called, or userHandle if called from a user that
2442 * is not a profile.
2443 *
2444 * @hide
2445 */
Jeff Sharkey8588bc12016-01-06 16:47:42 -07002446 public int getCredentialOwnerProfile(@UserIdInt int userHandle) {
Andres Moralesc5548c02015-08-05 10:23:12 -07002447 try {
2448 return mService.getCredentialOwnerProfile(userHandle);
2449 } catch (RemoteException re) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07002450 throw re.rethrowFromSystemServer();
Andres Moralesc5548c02015-08-05 10:23:12 -07002451 }
2452 }
2453
2454 /**
Jessica Hummelbe81c802014-04-22 15:49:22 +01002455 * Returns the parent of the profile which this method is called from
2456 * or null if called from a user that is not a profile.
2457 *
2458 * @hide
2459 */
Jeff Sharkey8588bc12016-01-06 16:47:42 -07002460 public UserInfo getProfileParent(@UserIdInt int userHandle) {
Jessica Hummelbe81c802014-04-22 15:49:22 +01002461 try {
2462 return mService.getProfileParent(userHandle);
2463 } catch (RemoteException re) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07002464 throw re.rethrowFromSystemServer();
Jessica Hummelbe81c802014-04-22 15:49:22 +01002465 }
2466 }
2467
2468 /**
Philip P. Moltmanna3e358c2018-11-21 12:45:50 -08002469 * Get the parent of a user profile.
2470 *
2471 * @param user the handle of the user profile
2472 *
2473 * @return the parent of the user or {@code null} if the user is not profile
2474 *
2475 * @hide
2476 */
2477 @SystemApi
2478 @RequiresPermission(android.Manifest.permission.MANAGE_USERS)
2479 public @Nullable UserHandle getProfileParent(@NonNull UserHandle user) {
2480 UserInfo info = getProfileParent(user.getIdentifier());
2481
2482 if (info == null) {
2483 return null;
2484 }
2485
2486 return UserHandle.of(info.id);
2487 }
2488
2489 /**
Tony Make3d1f652017-12-12 11:00:37 +00002490 * Enables or disables quiet mode for a managed profile. If quiet mode is enabled, apps in a
2491 * managed profile don't run, generate notifications, or consume data or battery.
2492 * <p>
2493 * If a user's credential is needed to turn off quiet mode, a confirm credential screen will be
2494 * shown to the user.
2495 * <p>
2496 * The change may not happen instantly, however apps can listen for
2497 * {@link Intent#ACTION_MANAGED_PROFILE_AVAILABLE} and
2498 * {@link Intent#ACTION_MANAGED_PROFILE_UNAVAILABLE} broadcasts in order to be notified of
2499 * the change of the quiet mode. Apps can also check the current state of quiet mode by
2500 * calling {@link #isQuietModeEnabled(UserHandle)}.
2501 * <p>
2502 * The caller must either be the foreground default launcher or have one of these permissions:
2503 * {@code MANAGE_USERS} or {@code MODIFY_QUIET_MODE}.
Rubin Xu0a29ecd2015-11-04 15:11:48 +00002504 *
Tony Make3d1f652017-12-12 11:00:37 +00002505 * @param enableQuietMode whether quiet mode should be enabled or disabled
2506 * @param userHandle user handle of the profile
2507 * @return {@code false} if user's credential is needed in order to turn off quiet mode,
2508 * {@code true} otherwise
2509 * @throws SecurityException if the caller is invalid
2510 * @throws IllegalArgumentException if {@code userHandle} is not a managed profile
2511 *
2512 * @see #isQuietModeEnabled(UserHandle)
Rubin Xu0a29ecd2015-11-04 15:11:48 +00002513 */
Tony Makbece85d2018-01-12 12:10:17 +00002514 public boolean requestQuietModeEnabled(boolean enableQuietMode, @NonNull UserHandle userHandle) {
2515 return requestQuietModeEnabled(enableQuietMode, userHandle, null);
Tony Makb7e6fd42017-12-05 19:40:28 +00002516 }
2517
2518 /**
Tony Makbece85d2018-01-12 12:10:17 +00002519 * Similar to {@link #requestQuietModeEnabled(boolean, UserHandle)}, except you can specify
Tony Makd390ae92017-12-28 13:23:10 +00002520 * a target to start when user is unlocked. If {@code target} is specified, caller must have
2521 * the {@link android.Manifest.permission#MANAGE_USERS} permission.
Tony Makb7e6fd42017-12-05 19:40:28 +00002522 *
Tony Makbece85d2018-01-12 12:10:17 +00002523 * @see {@link #requestQuietModeEnabled(boolean, UserHandle)}
Rubin Xu0a29ecd2015-11-04 15:11:48 +00002524 * @hide
2525 */
Tony Makbece85d2018-01-12 12:10:17 +00002526 public boolean requestQuietModeEnabled(
Tony Makb7e6fd42017-12-05 19:40:28 +00002527 boolean enableQuietMode, @NonNull UserHandle userHandle, IntentSender target) {
Rubin Xu0a29ecd2015-11-04 15:11:48 +00002528 try {
Tony Makbece85d2018-01-12 12:10:17 +00002529 return mService.requestQuietModeEnabled(
Tony Make3d1f652017-12-12 11:00:37 +00002530 mContext.getPackageName(), enableQuietMode, userHandle.getIdentifier(), target);
Jeff Sharkey27b2e692016-02-25 17:40:12 -07002531 } catch (RemoteException re) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07002532 throw re.rethrowFromSystemServer();
Rubin Xu0a29ecd2015-11-04 15:11:48 +00002533 }
2534 }
2535
2536 /**
2537 * Returns whether the given profile is in quiet mode or not.
Ricky Wai7881cf82016-04-15 17:20:12 +01002538 * Notes: Quiet mode is only supported for managed profiles.
Rubin Xu0a29ecd2015-11-04 15:11:48 +00002539 *
2540 * @param userHandle The user handle of the profile to be queried.
2541 * @return true if the profile is in quiet mode, false otherwise.
2542 */
2543 public boolean isQuietModeEnabled(UserHandle userHandle) {
2544 try {
2545 return mService.isQuietModeEnabled(userHandle.getIdentifier());
Jeff Sharkey27b2e692016-02-25 17:40:12 -07002546 } catch (RemoteException re) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07002547 throw re.rethrowFromSystemServer();
Rubin Xu0a29ecd2015-11-04 15:11:48 +00002548 }
Rubin Xu0a29ecd2015-11-04 15:11:48 +00002549 }
2550
2551 /**
Amith Yamasani7dda2652014-04-11 14:57:12 -07002552 * If the target user is a managed profile of the calling user or the caller
2553 * is itself a managed profile, then this returns a badged copy of the given
Svetoslavc71c42f2014-08-05 18:57:05 -07002554 * icon to be able to distinguish it from the original icon. For badging an
2555 * arbitrary drawable use {@link #getBadgedDrawableForUser(
2556 * android.graphics.drawable.Drawable, UserHandle, android.graphics.Rect, int)}.
2557 * <p>
2558 * If the original drawable is a BitmapDrawable and the backing bitmap is
Vadim Tryshev66ae66a2016-02-18 15:41:21 -08002559 * mutable as per {@link android.graphics.Bitmap#isMutable()}, the badging
Svetoslavc71c42f2014-08-05 18:57:05 -07002560 * is performed in place and the original drawable is returned.
2561 * </p>
Amith Yamasani7dda2652014-04-11 14:57:12 -07002562 *
2563 * @param icon The icon to badge.
2564 * @param user The target user.
2565 * @return A drawable that combines the original icon and a badge as
2566 * determined by the system.
Svetoslavc7d62f02014-09-04 15:39:54 -07002567 * @removed
Amith Yamasani7dda2652014-04-11 14:57:12 -07002568 */
Svetoslavc71c42f2014-08-05 18:57:05 -07002569 public Drawable getBadgedIconForUser(Drawable icon, UserHandle user) {
Svetoslavc7d62f02014-09-04 15:39:54 -07002570 return mContext.getPackageManager().getUserBadgedIcon(icon, user);
Amith Yamasani4f582632014-02-19 14:31:52 -08002571 }
2572
Kenny Guy701ea7c2014-05-08 23:34:12 +01002573 /**
2574 * If the target user is a managed profile of the calling user or the caller
Svetoslavc71c42f2014-08-05 18:57:05 -07002575 * is itself a managed profile, then this returns a badged copy of the given
2576 * drawable allowing the user to distinguish it from the original drawable.
2577 * The caller can specify the location in the bounds of the drawable to be
2578 * badged where the badge should be applied as well as the density of the
2579 * badge to be used.
2580 * <p>
2581 * If the original drawable is a BitmapDrawable and the backing bitmap is
Vadim Tryshev66ae66a2016-02-18 15:41:21 -08002582 * mutable as per {@link android.graphics.Bitmap#isMutable()}, the badging
Svetoslavc71c42f2014-08-05 18:57:05 -07002583 * is performed in place and the original drawable is returned.
2584 * </p>
2585 *
2586 * @param badgedDrawable The drawable to badge.
2587 * @param user The target user.
2588 * @param badgeLocation Where in the bounds of the badged drawable to place
Vadim Tryshev66ae66a2016-02-18 15:41:21 -08002589 * the badge. If it's {@code null}, the badge is applied on top of the entire
Svetoslavc71c42f2014-08-05 18:57:05 -07002590 * drawable being badged.
2591 * @param badgeDensity The optional desired density for the badge as per
Vadim Tryshev66ae66a2016-02-18 15:41:21 -08002592 * {@link android.util.DisplayMetrics#densityDpi}. If it's not positive,
Svetoslavc71c42f2014-08-05 18:57:05 -07002593 * the density of the display is used.
2594 * @return A drawable that combines the original drawable and a badge as
2595 * determined by the system.
Svetoslavc7d62f02014-09-04 15:39:54 -07002596 * @removed
Svetoslavc71c42f2014-08-05 18:57:05 -07002597 */
2598 public Drawable getBadgedDrawableForUser(Drawable badgedDrawable, UserHandle user,
2599 Rect badgeLocation, int badgeDensity) {
Svetoslavc7d62f02014-09-04 15:39:54 -07002600 return mContext.getPackageManager().getUserBadgedDrawableForDensity(badgedDrawable, user,
2601 badgeLocation, badgeDensity);
Svetoslavc71c42f2014-08-05 18:57:05 -07002602 }
2603
2604 /**
2605 * If the target user is a managed profile of the calling user or the caller
Kenny Guyf7ecf7c2014-06-18 11:32:05 +01002606 * is itself a managed profile, then this returns a copy of the label with
2607 * badging for accessibility services like talkback. E.g. passing in "Email"
2608 * and it might return "Work Email" for Email in the work profile.
2609 *
2610 * @param label The label to change.
2611 * @param user The target user.
2612 * @return A label that combines the original label and a badge as
2613 * determined by the system.
Svetoslavc7d62f02014-09-04 15:39:54 -07002614 * @removed
Kenny Guyf7ecf7c2014-06-18 11:32:05 +01002615 */
Kenny Guy237aecd2014-07-21 14:06:09 +01002616 public CharSequence getBadgedLabelForUser(CharSequence label, UserHandle user) {
Svetoslavc7d62f02014-09-04 15:39:54 -07002617 return mContext.getPackageManager().getUserBadgedLabel(label, user);
Amith Yamasani4f582632014-02-19 14:31:52 -08002618 }
2619
2620 /**
2621 * Returns information for all users on this device. Requires
2622 * {@link android.Manifest.permission#MANAGE_USERS} permission.
Emily Bernier394a6cd2014-05-07 12:49:20 -04002623 *
Amith Yamasani4f582632014-02-19 14:31:52 -08002624 * @param excludeDying specify if the list should exclude users being
2625 * removed.
Amith Yamasani920ace02012-09-20 22:15:37 -07002626 * @return the list of users that were created.
2627 * @hide
2628 */
Adam Lesinskiada8deb2017-05-12 13:50:42 -07002629 public @NonNull List<UserInfo> getUsers(boolean excludeDying) {
Amith Yamasani920ace02012-09-20 22:15:37 -07002630 try {
2631 return mService.getUsers(excludeDying);
Amith Yamasani258848d2012-08-10 17:06:33 -07002632 } catch (RemoteException re) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07002633 throw re.rethrowFromSystemServer();
Amith Yamasani258848d2012-08-10 17:06:33 -07002634 }
2635 }
2636
2637 /**
2638 * Removes a user and all associated data.
Amith Yamasani195263742012-08-21 15:40:12 -07002639 * Requires {@link android.Manifest.permission#MANAGE_USERS} permission.
Amith Yamasani258848d2012-08-10 17:06:33 -07002640 * @param userHandle the integer handle of the user, where 0 is the primary user.
2641 * @hide
2642 */
Jeff Sharkey8588bc12016-01-06 16:47:42 -07002643 public boolean removeUser(@UserIdInt int userHandle) {
Amith Yamasani258848d2012-08-10 17:06:33 -07002644 try {
2645 return mService.removeUser(userHandle);
2646 } catch (RemoteException re) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07002647 throw re.rethrowFromSystemServer();
Amith Yamasani258848d2012-08-10 17:06:33 -07002648 }
2649 }
2650
2651 /**
Varun Shahe38c6782018-12-04 16:57:49 -08002652 * Removes a user and all associated data.
2653 *
2654 * @param user the user that needs to be removed.
2655 * @hide
2656 */
2657 @SystemApi
2658 @RequiresPermission(android.Manifest.permission.MANAGE_USERS)
2659 public boolean removeUser(UserHandle user) {
2660 return removeUser(user.getIdentifier());
2661 }
2662
2663
2664 /**
Nicolas Prevotd37c4a92017-01-23 11:56:00 +00002665 * Similar to {@link #removeUser(int)} except bypassing the checking of
2666 * {@link UserManager#DISALLOW_REMOVE_USER}
2667 * or {@link UserManager#DISALLOW_REMOVE_MANAGED_PROFILE}.
2668 *
2669 * @see {@link #removeUser(int)}
2670 * @hide
2671 */
2672 public boolean removeUserEvenWhenDisallowed(@UserIdInt int userHandle) {
2673 try {
2674 return mService.removeUserEvenWhenDisallowed(userHandle);
2675 } catch (RemoteException re) {
2676 throw re.rethrowFromSystemServer();
2677 }
2678 }
2679
2680 /**
Amith Yamasani258848d2012-08-10 17:06:33 -07002681 * Updates the user's name.
Amith Yamasani195263742012-08-21 15:40:12 -07002682 * Requires {@link android.Manifest.permission#MANAGE_USERS} permission.
Amith Yamasani258848d2012-08-10 17:06:33 -07002683 *
2684 * @param userHandle the user's integer handle
2685 * @param name the new name for the user
2686 * @hide
2687 */
Jeff Sharkey8588bc12016-01-06 16:47:42 -07002688 public void setUserName(@UserIdInt int userHandle, String name) {
Amith Yamasani258848d2012-08-10 17:06:33 -07002689 try {
2690 mService.setUserName(userHandle, name);
2691 } catch (RemoteException re) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07002692 throw re.rethrowFromSystemServer();
Amith Yamasani258848d2012-08-10 17:06:33 -07002693 }
2694 }
2695
2696 /**
Amith Yamasanie928d7d2012-09-17 21:46:51 -07002697 * Sets the user's photo.
Amith Yamasani258848d2012-08-10 17:06:33 -07002698 * @param userHandle the user for whom to change the photo.
Amith Yamasanie928d7d2012-09-17 21:46:51 -07002699 * @param icon the bitmap to set as the photo.
Amith Yamasani258848d2012-08-10 17:06:33 -07002700 * @hide
2701 */
Jeff Sharkey8588bc12016-01-06 16:47:42 -07002702 public void setUserIcon(@UserIdInt int userHandle, Bitmap icon) {
Amith Yamasani258848d2012-08-10 17:06:33 -07002703 try {
Amith Yamasanie928d7d2012-09-17 21:46:51 -07002704 mService.setUserIcon(userHandle, icon);
Amith Yamasani258848d2012-08-10 17:06:33 -07002705 } catch (RemoteException re) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07002706 throw re.rethrowFromSystemServer();
Amith Yamasani258848d2012-08-10 17:06:33 -07002707 }
2708 }
2709
2710 /**
Amith Yamasani3b49f072012-09-17 10:21:43 -07002711 * Returns a file descriptor for the user's photo. PNG data can be read from this file.
2712 * @param userHandle the user whose photo we want to read.
Amith Yamasanie928d7d2012-09-17 21:46:51 -07002713 * @return a {@link Bitmap} of the user's photo, or null if there's no photo.
Alexandra Gherghina64d4dca2014-08-28 18:26:56 +01002714 * @see com.android.internal.util.UserIcons#getDefaultUserIcon for a default.
Amith Yamasani3b49f072012-09-17 10:21:43 -07002715 * @hide
2716 */
Jeff Sharkey8588bc12016-01-06 16:47:42 -07002717 public Bitmap getUserIcon(@UserIdInt int userHandle) {
Amith Yamasani3b49f072012-09-17 10:21:43 -07002718 try {
Adrian Roos1bdff912015-02-17 15:51:35 +01002719 ParcelFileDescriptor fd = mService.getUserIcon(userHandle);
2720 if (fd != null) {
2721 try {
2722 return BitmapFactory.decodeFileDescriptor(fd.getFileDescriptor());
2723 } finally {
2724 try {
2725 fd.close();
2726 } catch (IOException e) {
2727 }
2728 }
2729 }
Amith Yamasani3b49f072012-09-17 10:21:43 -07002730 } catch (RemoteException re) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07002731 throw re.rethrowFromSystemServer();
Amith Yamasani3b49f072012-09-17 10:21:43 -07002732 }
Adrian Roos1bdff912015-02-17 15:51:35 +01002733 return null;
Amith Yamasani3b49f072012-09-17 10:21:43 -07002734 }
2735
2736 /**
Amith Yamasani258848d2012-08-10 17:06:33 -07002737 * Returns the maximum number of users that can be created on this device. A return value
2738 * of 1 means that it is a single user device.
2739 * @hide
Maggie Benthalla12fccf2013-03-14 18:02:12 -04002740 * @return a value greater than or equal to 1
Amith Yamasani258848d2012-08-10 17:06:33 -07002741 */
Jeff Sharkey27bd34d2012-09-16 12:49:00 -07002742 public static int getMaxSupportedUsers() {
Amith Yamasaniff549202012-10-12 12:44:49 -07002743 // Don't allow multiple users on certain builds
2744 if (android.os.Build.ID.startsWith("JVP")) return 1;
Robin Lee83c79312018-02-09 19:27:18 +01002745 if (ActivityManager.isLowRamDeviceStatic()) {
2746 // Low-ram devices are Svelte. Most of the time they don't get multi-user.
2747 if ((Resources.getSystem().getConfiguration().uiMode & Configuration.UI_MODE_TYPE_MASK)
2748 != Configuration.UI_MODE_TYPE_TELEVISION) {
2749 return 1;
2750 }
2751 }
Jeff Sharkey27bd34d2012-09-16 12:49:00 -07002752 return SystemProperties.getInt("fw.max_users",
2753 Resources.getSystem().getInteger(R.integer.config_multiuserMaximumUsers));
Amith Yamasani258848d2012-08-10 17:06:33 -07002754 }
Amith Yamasani2a003292012-08-14 18:25:45 -07002755
2756 /**
Fyodor Kupolovcd86ebf2015-09-29 17:06:50 -07002757 * Returns true if the user switcher should be shown, this will be if device supports multi-user
2758 * and there are at least 2 users available that are not managed profiles.
Kenny Guy1a447532014-02-20 21:55:32 +00002759 * @hide
2760 * @return true if user switcher should be shown.
2761 */
2762 public boolean isUserSwitcherEnabled() {
Fyodor Kupolovcd86ebf2015-09-29 17:06:50 -07002763 if (!supportsMultipleUsers()) {
2764 return false;
2765 }
Benjamin Franzff66fa92017-08-10 10:39:44 +01002766 if (hasUserRestriction(DISALLOW_USER_SWITCH)) {
2767 return false;
2768 }
Amith Yamasanieb437d42016-04-29 09:31:25 -07002769 // If Demo Mode is on, don't show user switcher
2770 if (isDeviceInDemoMode(mContext)) {
2771 return false;
2772 }
Fan Zhangc69c8b62018-06-05 13:30:11 -07002773 // If user disabled this feature, don't show switcher
2774 final boolean userSwitcherEnabled = Settings.Global.getInt(mContext.getContentResolver(),
2775 Settings.Global.USER_SWITCHER_ENABLED, 1) != 0;
2776 if (!userSwitcherEnabled) {
2777 return false;
2778 }
Kenny Guy1a447532014-02-20 21:55:32 +00002779 List<UserInfo> users = getUsers(true);
2780 if (users == null) {
2781 return false;
2782 }
2783 int switchableUserCount = 0;
2784 for (UserInfo user : users) {
Xiaohui Chen7cb69df2015-07-13 16:01:01 -07002785 if (user.supportsSwitchToByUser()) {
Kenny Guy1a447532014-02-20 21:55:32 +00002786 ++switchableUserCount;
2787 }
2788 }
Fyodor Kupolovcd86ebf2015-09-29 17:06:50 -07002789 final boolean guestEnabled = !mContext.getSystemService(DevicePolicyManager.class)
2790 .getGuestUserDisabled(null);
Amith Yamasania596ff82014-06-12 18:12:38 -07002791 return switchableUserCount > 1 || guestEnabled;
Kenny Guy1a447532014-02-20 21:55:32 +00002792 }
2793
2794 /**
Amith Yamasanieb437d42016-04-29 09:31:25 -07002795 * @hide
2796 */
2797 public static boolean isDeviceInDemoMode(Context context) {
2798 return Settings.Global.getInt(context.getContentResolver(),
2799 Settings.Global.DEVICE_DEMO_MODE, 0) > 0;
2800 }
2801
2802 /**
Amith Yamasani2a003292012-08-14 18:25:45 -07002803 * Returns a serial number on this device for a given userHandle. User handles can be recycled
2804 * when deleting and creating users, but serial numbers are not reused until the device is wiped.
2805 * @param userHandle
2806 * @return a serial number associated with that user, or -1 if the userHandle is not valid.
2807 * @hide
2808 */
Jeff Sharkey8588bc12016-01-06 16:47:42 -07002809 public int getUserSerialNumber(@UserIdInt int userHandle) {
Amith Yamasani2a003292012-08-14 18:25:45 -07002810 try {
2811 return mService.getUserSerialNumber(userHandle);
2812 } catch (RemoteException re) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07002813 throw re.rethrowFromSystemServer();
Amith Yamasani2a003292012-08-14 18:25:45 -07002814 }
Amith Yamasani2a003292012-08-14 18:25:45 -07002815 }
2816
2817 /**
2818 * Returns a userHandle on this device for a given user serial number. User handles can be
2819 * recycled when deleting and creating users, but serial numbers are not reused until the device
2820 * is wiped.
2821 * @param userSerialNumber
2822 * @return the userHandle associated with that user serial number, or -1 if the serial number
2823 * is not valid.
2824 * @hide
2825 */
Jeff Sharkey8588bc12016-01-06 16:47:42 -07002826 public @UserIdInt int getUserHandle(int userSerialNumber) {
Amith Yamasani2a003292012-08-14 18:25:45 -07002827 try {
2828 return mService.getUserHandle(userSerialNumber);
2829 } catch (RemoteException re) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07002830 throw re.rethrowFromSystemServer();
Amith Yamasani2a003292012-08-14 18:25:45 -07002831 }
Amith Yamasani2a003292012-08-14 18:25:45 -07002832 }
Maggie Benthall67944582013-02-22 14:58:27 -05002833
Amith Yamasani7e99bc02013-04-16 18:24:51 -07002834 /**
Esteban Talavera953fe482016-06-07 15:25:20 +01002835 * Returns a {@link Bundle} containing any saved application restrictions for this user, for the
Amith Yamasani7e99bc02013-04-16 18:24:51 -07002836 * given package name. Only an application with this package name can call this method.
Esteban Talavera5b9f1672015-12-11 15:22:34 +00002837 *
2838 * <p>The returned {@link Bundle} consists of key-value pairs, as defined by the application,
2839 * where the types of values may be:
2840 * <ul>
2841 * <li>{@code boolean}
2842 * <li>{@code int}
2843 * <li>{@code String} or {@code String[]}
2844 * <li>From {@link android.os.Build.VERSION_CODES#M}, {@code Bundle} or {@code Bundle[]}
2845 * </ul>
2846 *
Fyodor Kupolov4e9af062016-07-18 16:59:11 -07002847 * <p>NOTE: The method performs disk I/O and shouldn't be called on the main thread
2848 *
Amith Yamasani7e99bc02013-04-16 18:24:51 -07002849 * @param packageName the package name of the calling application
Esteban Talavera953fe482016-06-07 15:25:20 +01002850 * @return a {@link Bundle} with the restrictions for that package, or an empty {@link Bundle}
2851 * if there are no saved restrictions.
Esteban Talavera5b9f1672015-12-11 15:22:34 +00002852 *
2853 * @see #KEY_RESTRICTIONS_PENDING
Amith Yamasani7e99bc02013-04-16 18:24:51 -07002854 */
Fyodor Kupolov4e9af062016-07-18 16:59:11 -07002855 @WorkerThread
Amith Yamasani7e99bc02013-04-16 18:24:51 -07002856 public Bundle getApplicationRestrictions(String packageName) {
2857 try {
2858 return mService.getApplicationRestrictions(packageName);
2859 } catch (RemoteException re) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07002860 throw re.rethrowFromSystemServer();
Amith Yamasani7e99bc02013-04-16 18:24:51 -07002861 }
Amith Yamasani7e99bc02013-04-16 18:24:51 -07002862 }
Amith Yamasanidf2e92a2013-03-01 17:04:38 -08002863
2864 /**
2865 * @hide
2866 */
Fyodor Kupolov4e9af062016-07-18 16:59:11 -07002867 @WorkerThread
Amith Yamasani7e99bc02013-04-16 18:24:51 -07002868 public Bundle getApplicationRestrictions(String packageName, UserHandle user) {
Amith Yamasanidf2e92a2013-03-01 17:04:38 -08002869 try {
Amith Yamasani7e99bc02013-04-16 18:24:51 -07002870 return mService.getApplicationRestrictionsForUser(packageName, user.getIdentifier());
Amith Yamasanidf2e92a2013-03-01 17:04:38 -08002871 } catch (RemoteException re) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07002872 throw re.rethrowFromSystemServer();
Amith Yamasanidf2e92a2013-03-01 17:04:38 -08002873 }
Amith Yamasanidf2e92a2013-03-01 17:04:38 -08002874 }
2875
2876 /**
2877 * @hide
2878 */
Fyodor Kupolov4e9af062016-07-18 16:59:11 -07002879 @WorkerThread
Amith Yamasani7e99bc02013-04-16 18:24:51 -07002880 public void setApplicationRestrictions(String packageName, Bundle restrictions,
Amith Yamasanidf2e92a2013-03-01 17:04:38 -08002881 UserHandle user) {
2882 try {
Amith Yamasani7e99bc02013-04-16 18:24:51 -07002883 mService.setApplicationRestrictions(packageName, restrictions, user.getIdentifier());
Amith Yamasanidf2e92a2013-03-01 17:04:38 -08002884 } catch (RemoteException re) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07002885 throw re.rethrowFromSystemServer();
Amith Yamasanidf2e92a2013-03-01 17:04:38 -08002886 }
2887 }
Amith Yamasani655d0e22013-06-12 14:19:10 -07002888
2889 /**
Amith Yamasanid304af62013-09-05 09:30:23 -07002890 * Sets a new challenge PIN for restrictions. This is only for use by pre-installed
2891 * apps and requires the MANAGE_USERS permission.
2892 * @param newPin the PIN to use for challenge dialogs.
2893 * @return Returns true if the challenge PIN was set successfully.
Fyodor Kupolovef249092015-05-06 13:18:46 -07002894 * @deprecated The restrictions PIN functionality is no longer provided by the system.
2895 * This method is preserved for backwards compatibility reasons and always returns false.
Amith Yamasani655d0e22013-06-12 14:19:10 -07002896 */
Aurimas Liutikas514c5ef2016-05-24 15:22:55 -07002897 @Deprecated
Amith Yamasanid304af62013-09-05 09:30:23 -07002898 public boolean setRestrictionsChallenge(String newPin) {
Amith Yamasani655d0e22013-06-12 14:19:10 -07002899 return false;
2900 }
Amith Yamasani1a7472e2013-07-02 11:17:30 -07002901
Amith Yamasanie4afaa32014-06-30 14:55:07 +05302902 /**
2903 * @hide
2904 * Set restrictions that should apply to any future guest user that's created.
2905 */
2906 public void setDefaultGuestRestrictions(Bundle restrictions) {
2907 try {
2908 mService.setDefaultGuestRestrictions(restrictions);
2909 } catch (RemoteException re) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07002910 throw re.rethrowFromSystemServer();
Amith Yamasanie4afaa32014-06-30 14:55:07 +05302911 }
2912 }
2913
2914 /**
2915 * @hide
2916 * Gets the default guest restrictions.
2917 */
2918 public Bundle getDefaultGuestRestrictions() {
2919 try {
2920 return mService.getDefaultGuestRestrictions();
2921 } catch (RemoteException re) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07002922 throw re.rethrowFromSystemServer();
Amith Yamasanie4afaa32014-06-30 14:55:07 +05302923 }
Amith Yamasanie4afaa32014-06-30 14:55:07 +05302924 }
Fyodor Kupolovff7233e2015-04-08 11:28:52 -07002925
2926 /**
2927 * Returns creation time of the user or of a managed profile associated with the calling user.
2928 * @param userHandle user handle of the user or a managed profile associated with the
2929 * calling user.
2930 * @return creation time in milliseconds since Epoch time.
2931 */
Fyodor Kupolov385de622015-04-10 18:00:19 -07002932 public long getUserCreationTime(UserHandle userHandle) {
Fyodor Kupolovff7233e2015-04-08 11:28:52 -07002933 try {
Fyodor Kupolov385de622015-04-10 18:00:19 -07002934 return mService.getUserCreationTime(userHandle.getIdentifier());
Fyodor Kupolovff7233e2015-04-08 11:28:52 -07002935 } catch (RemoteException re) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07002936 throw re.rethrowFromSystemServer();
Fyodor Kupolovff7233e2015-04-08 11:28:52 -07002937 }
2938 }
Amith Yamasani12747872015-12-07 14:19:49 -08002939
2940 /**
2941 * @hide
2942 * Checks if any uninitialized user has the specific seed account name and type.
2943 *
Pavel Grafov6a40f092016-10-25 15:46:51 +01002944 * @param accountName The account name to check for
2945 * @param accountType The account type of the account to check for
Amith Yamasani12747872015-12-07 14:19:49 -08002946 * @return whether the seed account was found
2947 */
2948 public boolean someUserHasSeedAccount(String accountName, String accountType) {
2949 try {
2950 return mService.someUserHasSeedAccount(accountName, accountType);
2951 } catch (RemoteException re) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07002952 throw re.rethrowFromSystemServer();
Amith Yamasani12747872015-12-07 14:19:49 -08002953 }
2954 }
Pavel Grafov6a40f092016-10-25 15:46:51 +01002955
2956 /**
2957 * @hide
2958 * User that enforces a restriction.
2959 *
2960 * @see #getUserRestrictionSources(String, UserHandle)
2961 */
2962 @SystemApi
2963 public static final class EnforcingUser implements Parcelable {
2964 private final @UserIdInt int userId;
2965 private final @UserRestrictionSource int userRestrictionSource;
2966
2967 /**
2968 * @hide
2969 */
2970 public EnforcingUser(
2971 @UserIdInt int userId, @UserRestrictionSource int userRestrictionSource) {
2972 this.userId = userId;
2973 this.userRestrictionSource = userRestrictionSource;
2974 }
2975
2976 private EnforcingUser(Parcel in) {
2977 userId = in.readInt();
2978 userRestrictionSource = in.readInt();
2979 }
2980
2981 public static final Creator<EnforcingUser> CREATOR = new Creator<EnforcingUser>() {
2982 @Override
2983 public EnforcingUser createFromParcel(Parcel in) {
2984 return new EnforcingUser(in);
2985 }
2986
2987 @Override
2988 public EnforcingUser[] newArray(int size) {
2989 return new EnforcingUser[size];
2990 }
2991 };
2992
2993 @Override
2994 public int describeContents() {
2995 return 0;
2996 }
2997
2998 @Override
2999 public void writeToParcel(Parcel dest, int flags) {
3000 dest.writeInt(userId);
3001 dest.writeInt(userRestrictionSource);
3002 }
3003
3004 /**
3005 * Returns an id of the enforcing user.
3006 *
3007 * <p> Will be UserHandle.USER_NULL when restriction is set by the system.
3008 */
3009 public UserHandle getUserHandle() {
3010 return UserHandle.of(userId);
3011 }
3012
3013 /**
3014 * Returns the status of the enforcing user.
3015 *
3016 * <p> One of {@link #RESTRICTION_SOURCE_SYSTEM},
3017 * {@link #RESTRICTION_SOURCE_DEVICE_OWNER} and
3018 * {@link #RESTRICTION_SOURCE_PROFILE_OWNER}
3019 */
3020 public @UserRestrictionSource int getUserRestrictionSource() {
3021 return userRestrictionSource;
3022 }
3023 }
Amith Yamasani258848d2012-08-10 17:06:33 -07003024}