blob: 9c9829fb08e2cc6ff987697a6f0fa1bc13095b41 [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;
Varun Shah50ef2002019-03-04 16:41:12 -080028import android.annotation.UnsupportedAppUsage;
Jeff Sharkey8588bc12016-01-06 16:47:42 -070029import android.annotation.UserIdInt;
Fyodor Kupolov4e9af062016-07-18 16:59:11 -070030import android.annotation.WorkerThread;
Amith Yamasani37ed8d12016-01-27 14:40:16 -080031import android.app.Activity;
Dianne Hackborn409297d2014-07-10 17:39:20 -070032import android.app.ActivityManager;
Fyodor Kupolovcd86ebf2015-09-29 17:06:50 -070033import android.app.admin.DevicePolicyManager;
Makoto Onuki068c54a2015-10-13 14:34:03 -070034import android.content.ComponentName;
Amith Yamasani258848d2012-08-10 17:06:33 -070035import android.content.Context;
Amith Yamasani12747872015-12-07 14:19:49 -080036import android.content.Intent;
Esteban Talavera8bd7c522017-02-13 12:35:04 +000037import android.content.IntentFilter;
Benjamin Franzf02420c2016-04-04 18:52:21 +010038import android.content.IntentSender;
Amith Yamasani258848d2012-08-10 17:06:33 -070039import android.content.pm.UserInfo;
Robin Lee83c79312018-02-09 19:27:18 +010040import android.content.res.Configuration;
Jeff Sharkey27bd34d2012-09-16 12:49:00 -070041import android.content.res.Resources;
Maggie Benthall67944582013-02-22 14:58:27 -050042import android.graphics.Bitmap;
Adrian Roos1bdff912015-02-17 15:51:35 +010043import android.graphics.BitmapFactory;
Amith Yamasani4f582632014-02-19 14:31:52 -080044import android.graphics.Rect;
Amith Yamasani4f582632014-02-19 14:31:52 -080045import android.graphics.drawable.Drawable;
Amith Yamasani1e9c2182014-06-11 17:25:51 -070046import android.provider.Settings;
Fyodor Kupolov523c4042016-02-24 15:03:13 -080047import android.telephony.TelephonyManager;
Jason Monk1c7c3192014-06-26 12:52:18 -040048import android.view.WindowManager.LayoutParams;
Amith Yamasani258848d2012-08-10 17:06:33 -070049
Maggie Benthall67944582013-02-22 14:58:27 -050050import com.android.internal.R;
John Reckaa67f682016-09-20 14:24:21 -070051import com.android.internal.os.RoSystemProperties;
Maggie Benthall67944582013-02-22 14:58:27 -050052
Adrian Roos1bdff912015-02-17 15:51:35 +010053import java.io.IOException;
Zoltan Szatmary-Bane7834602016-04-08 18:41:11 +010054import java.lang.annotation.Retention;
55import java.lang.annotation.RetentionPolicy;
Amith Yamasani4f582632014-02-19 14:31:52 -080056import java.util.ArrayList;
Amith Yamasani258848d2012-08-10 17:06:33 -070057import java.util.List;
58
59/**
Amith Yamasani06964342016-04-15 13:55:01 -070060 * Manages users and user details on a multi-user system. There are two major categories of
61 * users: fully customizable users with their own login, and managed profiles that share a workspace
62 * with a related user.
63 * <p>
64 * Users are different from accounts, which are managed by
65 * {@link AccountManager}. Each user can have their own set of accounts.
66 * <p>
67 * See {@link DevicePolicyManager#ACTION_PROVISION_MANAGED_PROFILE} for more on managed profiles.
Amith Yamasani258848d2012-08-10 17:06:33 -070068 */
Jeff Sharkeyd86b8fe2017-06-02 17:36:26 -060069@SystemService(Context.USER_SERVICE)
Amith Yamasani258848d2012-08-10 17:06:33 -070070public class UserManager {
71
Pavel Grafov4f4f6f82017-03-28 13:44:04 +010072 private static final String TAG = "UserManager";
Andrei Onea24ec3212019-03-15 17:35:05 +000073 @UnsupportedAppUsage
Amith Yamasani258848d2012-08-10 17:06:33 -070074 private final IUserManager mService;
75 private final Context mContext;
76
Makoto Onukid49f3fa2017-01-25 14:09:48 -080077 private Boolean mIsManagedProfileCached;
78
Amith Yamasanie4cf7342012-12-17 11:12:09 -080079 /**
Zoltan Szatmary-Bane7834602016-04-08 18:41:11 +010080 * @hide
81 * No user restriction.
82 */
83 @SystemApi
84 public static final int RESTRICTION_NOT_SET = 0x0;
85
86 /**
87 * @hide
88 * User restriction set by system/user.
89 */
90 @SystemApi
91 public static final int RESTRICTION_SOURCE_SYSTEM = 0x1;
92
93 /**
94 * @hide
95 * User restriction set by a device owner.
96 */
97 @SystemApi
98 public static final int RESTRICTION_SOURCE_DEVICE_OWNER = 0x2;
99
100 /**
101 * @hide
102 * User restriction set by a profile owner.
103 */
104 @SystemApi
105 public static final int RESTRICTION_SOURCE_PROFILE_OWNER = 0x4;
106
107 /** @hide */
108 @Retention(RetentionPolicy.SOURCE)
Jeff Sharkeyce8db992017-12-13 20:05:05 -0700109 @IntDef(flag = true, prefix = { "RESTRICTION_" }, value = {
110 RESTRICTION_NOT_SET,
111 RESTRICTION_SOURCE_SYSTEM,
112 RESTRICTION_SOURCE_DEVICE_OWNER,
113 RESTRICTION_SOURCE_PROFILE_OWNER
114 })
Zoltan Szatmary-Bane7834602016-04-08 18:41:11 +0100115 @SystemApi
116 public @interface UserRestrictionSource {}
117
118 /**
Fyodor Kupolov53019282015-07-21 11:48:18 -0700119 * Specifies if a user is disallowed from adding and removing accounts, unless they are
120 * {@link android.accounts.AccountManager#addAccountExplicitly programmatically} added by
121 * Authenticator.
Amith Yamasani71e6c692013-03-24 17:39:28 -0700122 * The default value is <code>false</code>.
Amith Yamasani26af8292014-09-09 09:57:27 -0700123 *
Benjamin Franzb6c0ce42015-11-05 10:06:51 +0000124 * <p>From {@link android.os.Build.VERSION_CODES#N} a profile or device owner app can still
125 * use {@link android.accounts.AccountManager} APIs to add or remove accounts when account
126 * management is disallowed.
127 *
Fyodor Kupolov0ffbfea2016-01-19 10:07:54 -0800128 * <p>Key for user restrictions.
129 * <p>Type: Boolean
Makoto Onuki068c54a2015-10-13 14:34:03 -0700130 * @see DevicePolicyManager#addUserRestriction(ComponentName, String)
131 * @see DevicePolicyManager#clearUserRestriction(ComponentName, String)
Amith Yamasanie4cf7342012-12-17 11:12:09 -0800132 * @see #getUserRestrictions()
133 */
Amith Yamasani71e6c692013-03-24 17:39:28 -0700134 public static final String DISALLOW_MODIFY_ACCOUNTS = "no_modify_accounts";
Amith Yamasanie4cf7342012-12-17 11:12:09 -0800135
136 /**
Amith Yamasani26af8292014-09-09 09:57:27 -0700137 * Specifies if a user is disallowed from changing Wi-Fi
Julia Reynolds2cb384f2014-08-13 15:15:55 -0400138 * access points. The default value is <code>false</code>.
Fyodor Kupolov0ffbfea2016-01-19 10:07:54 -0800139 * <p>This restriction has no effect in a managed profile.
Amith Yamasani26af8292014-09-09 09:57:27 -0700140 *
Fyodor Kupolov0ffbfea2016-01-19 10:07:54 -0800141 * <p>Key for user restrictions.
142 * <p>Type: Boolean
Makoto Onuki068c54a2015-10-13 14:34:03 -0700143 * @see DevicePolicyManager#addUserRestriction(ComponentName, String)
144 * @see DevicePolicyManager#clearUserRestriction(ComponentName, String)
Amith Yamasanie4cf7342012-12-17 11:12:09 -0800145 * @see #getUserRestrictions()
146 */
Amith Yamasani71e6c692013-03-24 17:39:28 -0700147 public static final String DISALLOW_CONFIG_WIFI = "no_config_wifi";
Amith Yamasanie4cf7342012-12-17 11:12:09 -0800148
149 /**
Christine Franks1bade5d2017-10-10 15:41:50 -0700150 * Specifies if a user is disallowed from changing the device
151 * language. The default value is <code>false</code>.
152 *
153 * <p>Key for user restrictions.
154 * <p>Type: Boolean
155 * @see DevicePolicyManager#addUserRestriction(ComponentName, String)
156 * @see DevicePolicyManager#clearUserRestriction(ComponentName, String)
157 * @see #getUserRestrictions()
158 */
159 public static final String DISALLOW_CONFIG_LOCALE = "no_config_locale";
160
161 /**
Benjamin Millerf1c3ed12018-05-03 10:56:28 +0000162 * Specifies if a user is disallowed from installing applications. This user restriction also
163 * prevents device owners and profile owners installing apps. The default value is
164 * {@code false}.
Amith Yamasani26af8292014-09-09 09:57:27 -0700165 *
Fyodor Kupolov0ffbfea2016-01-19 10:07:54 -0800166 * <p>Key for user restrictions.
167 * <p>Type: Boolean
Makoto Onuki068c54a2015-10-13 14:34:03 -0700168 * @see DevicePolicyManager#addUserRestriction(ComponentName, String)
169 * @see DevicePolicyManager#clearUserRestriction(ComponentName, String)
Amith Yamasanie4cf7342012-12-17 11:12:09 -0800170 * @see #getUserRestrictions()
171 */
Amith Yamasani71e6c692013-03-24 17:39:28 -0700172 public static final String DISALLOW_INSTALL_APPS = "no_install_apps";
Amith Yamasanie4cf7342012-12-17 11:12:09 -0800173
174 /**
Amith Yamasani26af8292014-09-09 09:57:27 -0700175 * Specifies if a user is disallowed from uninstalling applications.
Amith Yamasani71e6c692013-03-24 17:39:28 -0700176 * The default value is <code>false</code>.
Amith Yamasani26af8292014-09-09 09:57:27 -0700177 *
Fyodor Kupolov0ffbfea2016-01-19 10:07:54 -0800178 * <p>Key for user restrictions.
179 * <p>Type: Boolean
Makoto Onuki068c54a2015-10-13 14:34:03 -0700180 * @see DevicePolicyManager#addUserRestriction(ComponentName, String)
181 * @see DevicePolicyManager#clearUserRestriction(ComponentName, String)
Amith Yamasanie4cf7342012-12-17 11:12:09 -0800182 * @see #getUserRestrictions()
183 */
Amith Yamasani71e6c692013-03-24 17:39:28 -0700184 public static final String DISALLOW_UNINSTALL_APPS = "no_uninstall_apps";
Amith Yamasanie4cf7342012-12-17 11:12:09 -0800185
Amith Yamasani71e6c692013-03-24 17:39:28 -0700186 /**
Amith Yamasani150514b2015-01-07 16:05:05 -0800187 * Specifies if a user is disallowed from turning on location sharing.
Amith Yamasani71e6c692013-03-24 17:39:28 -0700188 * The default value is <code>false</code>.
Fyodor Kupolov0ffbfea2016-01-19 10:07:54 -0800189 * <p>In a managed profile, location sharing always reflects the primary user's setting, but
Amith Yamasani150514b2015-01-07 16:05:05 -0800190 * can be overridden and forced off by setting this restriction to true in the managed profile.
Amith Yamasani26af8292014-09-09 09:57:27 -0700191 *
Fyodor Kupolov0ffbfea2016-01-19 10:07:54 -0800192 * <p>Key for user restrictions.
193 * <p>Type: Boolean
Makoto Onuki068c54a2015-10-13 14:34:03 -0700194 * @see DevicePolicyManager#addUserRestriction(ComponentName, String)
195 * @see DevicePolicyManager#clearUserRestriction(ComponentName, String)
Maggie Benthall67944582013-02-22 14:58:27 -0500196 * @see #getUserRestrictions()
197 */
Amith Yamasani71e6c692013-03-24 17:39:28 -0700198 public static final String DISALLOW_SHARE_LOCATION = "no_share_location";
Maggie Benthall67944582013-02-22 14:58:27 -0500199
Maggie Benthalla12fccf2013-03-14 18:02:12 -0400200 /**
yuemingw5fe75dc2017-11-29 15:52:56 +0000201 * Specifies if airplane mode is disallowed on the device.
202 *
203 * <p> This restriction can only be set by the device owner and the profile owner on the
204 * primary user and it applies globally - i.e. it disables airplane mode on the entire device.
205 * <p>The default value is <code>false</code>.
206 *
207 * <p>Key for user restrictions.
208 * <p>Type: Boolean
209 * @see DevicePolicyManager#addUserRestriction(ComponentName, String)
210 * @see DevicePolicyManager#clearUserRestriction(ComponentName, String)
211 * @see #getUserRestrictions()
212 */
213 public static final String DISALLOW_AIRPLANE_MODE = "no_airplane_mode";
214
215 /**
yuemingwc6ac29d2018-01-10 16:54:08 +0000216 * Specifies if a user is disallowed from configuring brightness. When device owner sets it,
217 * it'll only be applied on the target(system) user.
218 *
219 * <p>The default value is <code>false</code>.
220 *
221 * <p>This user restriction has no effect on managed profiles.
222 * <p>Key for user restrictions.
223 * <p>Type: Boolean
224 * @see DevicePolicyManager#addUserRestriction(ComponentName, String)
225 * @see DevicePolicyManager#clearUserRestriction(ComponentName, String)
226 * @see #getUserRestrictions()
227 */
228 public static final String DISALLOW_CONFIG_BRIGHTNESS = "no_config_brightness";
229
230 /**
yuemingwa1781442018-01-10 18:59:43 +0000231 * Specifies if ambient display is disallowed for the user.
232 *
233 * <p>The default value is <code>false</code>.
234 *
235 * <p>This user restriction has no effect on managed profiles.
236 * <p>Key for user restrictions.
237 * <p>Type: Boolean
238 * @see DevicePolicyManager#addUserRestriction(ComponentName, String)
239 * @see DevicePolicyManager#clearUserRestriction(ComponentName, String)
240 * @see #getUserRestrictions()
241 */
242 public static final String DISALLOW_AMBIENT_DISPLAY = "no_ambient_display";
243
244 /**
yuemingw5cda3ae2018-01-15 10:26:19 +0000245 * Specifies if a user is disallowed from changing screen off timeout.
246 *
247 * <p>The default value is <code>false</code>.
248 *
249 * <p>This user restriction has no effect on managed profiles.
250 * <p>Key for user restrictions.
251 * <p>Type: Boolean
252 * @see DevicePolicyManager#addUserRestriction(ComponentName, String)
253 * @see DevicePolicyManager#clearUserRestriction(ComponentName, String)
254 * @see #getUserRestrictions()
255 */
256 public static final String DISALLOW_CONFIG_SCREEN_TIMEOUT = "no_config_screen_timeout";
257
258 /**
Amith Yamasani26af8292014-09-09 09:57:27 -0700259 * Specifies if a user is disallowed from enabling the
Maggie Benthalla12fccf2013-03-14 18:02:12 -0400260 * "Unknown Sources" setting, that allows installation of apps from unknown sources.
Irina Dumitrescu4638edd2018-09-05 14:08:33 +0100261 * Unknown sources exclude adb and special apps such as trusted app stores.
Maggie Benthalla12fccf2013-03-14 18:02:12 -0400262 * The default value is <code>false</code>.
Amith Yamasani26af8292014-09-09 09:57:27 -0700263 *
Fyodor Kupolov0ffbfea2016-01-19 10:07:54 -0800264 * <p>Key for user restrictions.
265 * <p>Type: Boolean
Makoto Onuki068c54a2015-10-13 14:34:03 -0700266 * @see DevicePolicyManager#addUserRestriction(ComponentName, String)
267 * @see DevicePolicyManager#clearUserRestriction(ComponentName, String)
Maggie Benthalla12fccf2013-03-14 18:02:12 -0400268 * @see #getUserRestrictions()
269 */
270 public static final String DISALLOW_INSTALL_UNKNOWN_SOURCES = "no_install_unknown_sources";
271
272 /**
Tor Norbye33ca9122018-10-03 09:28:21 +0200273 * This restriction is a device-wide version of {@link #DISALLOW_INSTALL_UNKNOWN_SOURCES}.
Irina Dumitrescu4638edd2018-09-05 14:08:33 +0100274 *
275 * Specifies if all users on the device are disallowed from enabling the
276 * "Unknown Sources" setting, that allows installation of apps from unknown sources.
Irina Dumitrescu0501eee2018-10-19 23:50:18 +0100277 *
278 * This restriction can be enabled by the profile owner, in which case all accounts and
279 * profiles will be affected.
280 *
Irina Dumitrescu4638edd2018-09-05 14:08:33 +0100281 * The default value is <code>false</code>.
282 *
283 * <p>Key for user restrictions.
284 * <p>Type: Boolean
285 * @see DevicePolicyManager#addUserRestriction(ComponentName, String)
286 * @see DevicePolicyManager#clearUserRestriction(ComponentName, String)
287 * @see #getUserRestrictions()
288 */
289 public static final String DISALLOW_INSTALL_UNKNOWN_SOURCES_GLOBALLY =
290 "no_install_unknown_sources_globally";
291
292 /**
Amith Yamasani26af8292014-09-09 09:57:27 -0700293 * Specifies if a user is disallowed from configuring bluetooth.
Nicolas Prevot1c4c4422015-02-16 11:32:21 +0000294 * This does <em>not</em> restrict the user from turning bluetooth on or off.
Maggie Benthalla12fccf2013-03-14 18:02:12 -0400295 * The default value is <code>false</code>.
Lenka Trochtova63d5e4a72016-12-02 12:19:39 +0100296 * <p>This restriction doesn't prevent the user from using bluetooth. For disallowing usage of
297 * bluetooth completely on the device, use {@link #DISALLOW_BLUETOOTH}.
Fyodor Kupolov0ffbfea2016-01-19 10:07:54 -0800298 * <p>This restriction has no effect in a managed profile.
Amith Yamasani26af8292014-09-09 09:57:27 -0700299 *
Fyodor Kupolov0ffbfea2016-01-19 10:07:54 -0800300 * <p>Key for user restrictions.
301 * <p>Type: Boolean
Makoto Onuki068c54a2015-10-13 14:34:03 -0700302 * @see DevicePolicyManager#addUserRestriction(ComponentName, String)
303 * @see DevicePolicyManager#clearUserRestriction(ComponentName, String)
Maggie Benthalla12fccf2013-03-14 18:02:12 -0400304 * @see #getUserRestrictions()
305 */
306 public static final String DISALLOW_CONFIG_BLUETOOTH = "no_config_bluetooth";
307
Maggie Benthalla12fccf2013-03-14 18:02:12 -0400308 /**
Lenka Trochtova63d5e4a72016-12-02 12:19:39 +0100309 * Specifies if bluetooth is disallowed on the device.
310 *
311 * <p> This restriction can only be set by the device owner and the profile owner on the
312 * primary user and it applies globally - i.e. it disables bluetooth on the entire device.
313 * <p>The default value is <code>false</code>.
314 * <p>Key for user restrictions.
315 * <p>Type: Boolean
316 * @see DevicePolicyManager#addUserRestriction(ComponentName, String)
317 * @see DevicePolicyManager#clearUserRestriction(ComponentName, String)
318 * @see #getUserRestrictions()
319 */
320 public static final String DISALLOW_BLUETOOTH = "no_bluetooth";
321
322 /**
Pavel Grafov4f4f6f82017-03-28 13:44:04 +0100323 * Specifies if outgoing bluetooth sharing is disallowed on the device. Device owner and profile
324 * owner can set this restriction. When it is set by device owner, all users on this device will
325 * be affected.
326 *
327 * <p>Default is <code>true</code> for managed profiles and false for otherwise. When a device
328 * upgrades to {@link android.os.Build.VERSION_CODES#O}, the system sets it for all existing
329 * managed profiles.
330 *
331 * <p>Key for user restrictions.
332 * <p>Type: Boolean
333 * @see DevicePolicyManager#addUserRestriction(ComponentName, String)
334 * @see DevicePolicyManager#clearUserRestriction(ComponentName, String)
335 * @see #getUserRestrictions()
336 */
337 public static final String DISALLOW_BLUETOOTH_SHARING = "no_bluetooth_sharing";
338
339 /**
Amith Yamasani26af8292014-09-09 09:57:27 -0700340 * Specifies if a user is disallowed from transferring files over
Amith Yamasanic34dc7c2014-09-18 09:42:42 -0700341 * USB. This can only be set by device owners and profile owners on the primary user.
342 * The default value is <code>false</code>.
Amith Yamasani26af8292014-09-09 09:57:27 -0700343 *
Fyodor Kupolov0ffbfea2016-01-19 10:07:54 -0800344 * <p>Key for user restrictions.
345 * <p>Type: Boolean
Makoto Onuki068c54a2015-10-13 14:34:03 -0700346 * @see DevicePolicyManager#addUserRestriction(ComponentName, String)
347 * @see DevicePolicyManager#clearUserRestriction(ComponentName, String)
Maggie Benthalla12fccf2013-03-14 18:02:12 -0400348 * @see #getUserRestrictions()
349 */
350 public static final String DISALLOW_USB_FILE_TRANSFER = "no_usb_file_transfer";
351
Emily Bernierb223f732013-04-11 15:46:36 -0400352 /**
Amith Yamasani26af8292014-09-09 09:57:27 -0700353 * Specifies if a user is disallowed from configuring user
Emily Bernierb223f732013-04-11 15:46:36 -0400354 * credentials. The default value is <code>false</code>.
Amith Yamasani26af8292014-09-09 09:57:27 -0700355 *
Fyodor Kupolov0ffbfea2016-01-19 10:07:54 -0800356 * <p>Key for user restrictions.
357 * <p>Type: Boolean
Makoto Onuki068c54a2015-10-13 14:34:03 -0700358 * @see DevicePolicyManager#addUserRestriction(ComponentName, String)
359 * @see DevicePolicyManager#clearUserRestriction(ComponentName, String)
Emily Bernierb223f732013-04-11 15:46:36 -0400360 * @see #getUserRestrictions()
361 */
362 public static final String DISALLOW_CONFIG_CREDENTIALS = "no_config_credentials";
363
364 /**
Amith Yamasani150514b2015-01-07 16:05:05 -0800365 * When set on the primary user this specifies if the user can remove other users.
366 * When set on a secondary user, this specifies if the user can remove itself.
367 * This restriction has no effect on managed profiles.
368 * The default value is <code>false</code>.
Amith Yamasani26af8292014-09-09 09:57:27 -0700369 *
Fyodor Kupolov0ffbfea2016-01-19 10:07:54 -0800370 * <p>Key for user restrictions.
371 * <p>Type: Boolean
Makoto Onuki068c54a2015-10-13 14:34:03 -0700372 * @see DevicePolicyManager#addUserRestriction(ComponentName, String)
373 * @see DevicePolicyManager#clearUserRestriction(ComponentName, String)
Emily Bernierb223f732013-04-11 15:46:36 -0400374 * @see #getUserRestrictions()
375 */
376 public static final String DISALLOW_REMOVE_USER = "no_remove_user";
377
Julia Reynoldsd46d0f92014-04-23 15:23:24 -0400378 /**
Esteban Talavera6c9116a2016-11-24 16:12:44 +0000379 * Specifies if managed profiles of this user can be removed, other than by its profile owner.
380 * The default value is <code>false</code>.
381 * <p>
Nicolas Prevot2ea46fe2017-01-05 10:29:34 +0000382 * This restriction has no effect on managed profiles.
Esteban Talavera6c9116a2016-11-24 16:12:44 +0000383 *
384 * <p>Key for user restrictions.
385 * <p>Type: Boolean
386 * @see DevicePolicyManager#addUserRestriction(ComponentName, String)
387 * @see DevicePolicyManager#clearUserRestriction(ComponentName, String)
388 * @see #getUserRestrictions()
389 */
390 public static final String DISALLOW_REMOVE_MANAGED_PROFILE = "no_remove_managed_profile";
391
392 /**
Pavel Grafov9fb706c2018-02-27 14:07:52 +0000393 * Specifies if a user is disallowed from enabling or accessing debugging features. When set on
394 * the primary user, disables debugging features altogether, including USB debugging. When set
395 * on a managed profile or a secondary user, blocks debugging for that user only, including
396 * starting activities, making service calls, accessing content providers, sending broadcasts,
397 * installing/uninstalling packages, clearing user data, etc.
398 * The default value is <code>false</code>.
Amith Yamasani26af8292014-09-09 09:57:27 -0700399 *
Fyodor Kupolov0ffbfea2016-01-19 10:07:54 -0800400 * <p>Key for user restrictions.
401 * <p>Type: Boolean
Makoto Onuki068c54a2015-10-13 14:34:03 -0700402 * @see DevicePolicyManager#addUserRestriction(ComponentName, String)
403 * @see DevicePolicyManager#clearUserRestriction(ComponentName, String)
Julia Reynoldsd46d0f92014-04-23 15:23:24 -0400404 * @see #getUserRestrictions()
405 */
406 public static final String DISALLOW_DEBUGGING_FEATURES = "no_debugging_features";
407
408 /**
Benjamin Miller05fef7e2017-07-24 10:11:39 +0200409 * Specifies if a user is disallowed from configuring a VPN. The default value is
410 * <code>false</code>. This restriction has an effect when set by device owners and, in Android
411 * 6.0 ({@linkplain android.os.Build.VERSION_CODES#M API level 23}) or higher, profile owners.
412 * <p>This restriction also prevents VPNs from starting. However, in Android 7.0
413 * ({@linkplain android.os.Build.VERSION_CODES#N API level 24}) or higher, the system does
414 * start always-on VPNs created by the device or profile owner.
Amith Yamasani26af8292014-09-09 09:57:27 -0700415 *
Fyodor Kupolov0ffbfea2016-01-19 10:07:54 -0800416 * <p>Key for user restrictions.
417 * <p>Type: Boolean
Makoto Onuki068c54a2015-10-13 14:34:03 -0700418 * @see DevicePolicyManager#addUserRestriction(ComponentName, String)
419 * @see DevicePolicyManager#clearUserRestriction(ComponentName, String)
Julia Reynoldsd46d0f92014-04-23 15:23:24 -0400420 * @see #getUserRestrictions()
421 */
422 public static final String DISALLOW_CONFIG_VPN = "no_config_vpn";
423
424 /**
yuemingw7810b8b2018-02-01 17:32:25 +0000425 * Specifies if a user is disallowed from enabling or disabling location providers. As a
426 * result, user is disallowed from turning on or off location. Device owner and profile owners
427 * can set this restriction and it only applies on the managed user.
yuemingw7cc2c4c2017-11-28 17:20:01 +0000428 *
429 * <p>In a managed profile, location sharing is forced off when it's off on primary user, so
430 * user can still turn off location sharing on managed profile when the restriction is set by
431 * profile owner on managed profile.
432 *
433 * <p>This user restriction is different from {@link #DISALLOW_SHARE_LOCATION},
434 * as the device owner or profile owner can still enable or disable location mode via
435 * {@link DevicePolicyManager#setSecureSetting} when this restriction is on.
436 *
437 * <p>The default value is <code>false</code>.
438 *
439 * <p>Key for user restrictions.
440 * <p>Type: Boolean
yuemingw7810b8b2018-02-01 17:32:25 +0000441 * @see android.location.LocationManager#isProviderEnabled(String)
yuemingw7cc2c4c2017-11-28 17:20:01 +0000442 * @see DevicePolicyManager#addUserRestriction(ComponentName, String)
443 * @see DevicePolicyManager#clearUserRestriction(ComponentName, String)
444 * @see #getUserRestrictions()
445 */
yuemingw7810b8b2018-02-01 17:32:25 +0000446 public static final String DISALLOW_CONFIG_LOCATION = "no_config_location";
yuemingw7cc2c4c2017-11-28 17:20:01 +0000447
448 /**
yuemingwa9772f362017-10-23 18:34:35 +0100449 * Specifies if date, time and timezone configuring is disallowed.
450 *
451 * <p>When restriction is set by device owners, it applies globally - i.e., it disables date,
452 * time and timezone setting on the entire device and all users will be affected. When it's set
453 * by profile owners, it's only applied to the managed user.
454 * <p>The default value is <code>false</code>.
455 *
456 * <p>This user restriction has no effect on managed profiles.
457 * <p>Key for user restrictions.
458 * <p>Type: Boolean
459 * @see DevicePolicyManager#addUserRestriction(ComponentName, String)
460 * @see DevicePolicyManager#clearUserRestriction(ComponentName, String)
461 * @see #getUserRestrictions()
462 */
463 public static final String DISALLOW_CONFIG_DATE_TIME = "no_config_date_time";
464
465 /**
Amith Yamasani26af8292014-09-09 09:57:27 -0700466 * Specifies if a user is disallowed from configuring Tethering
Amith Yamasanic34dc7c2014-09-18 09:42:42 -0700467 * & portable hotspots. This can only be set by device owners and profile owners on the
468 * primary user. The default value is <code>false</code>.
Rubin Xu1faf1442017-08-23 15:48:12 +0100469 * <p>In Android 9.0 or higher, if tethering is enabled when this restriction is set,
470 * tethering will be automatically turned off.
Amith Yamasani26af8292014-09-09 09:57:27 -0700471 *
Fyodor Kupolov0ffbfea2016-01-19 10:07:54 -0800472 * <p>Key for user restrictions.
473 * <p>Type: Boolean
Makoto Onuki068c54a2015-10-13 14:34:03 -0700474 * @see DevicePolicyManager#addUserRestriction(ComponentName, String)
475 * @see DevicePolicyManager#clearUserRestriction(ComponentName, String)
Julia Reynoldsd46d0f92014-04-23 15:23:24 -0400476 * @see #getUserRestrictions()
477 */
478 public static final String DISALLOW_CONFIG_TETHERING = "no_config_tethering";
479
480 /**
Stuart Scotte3e314d2015-04-20 14:07:45 -0700481 * Specifies if a user is disallowed from resetting network settings
482 * from Settings. This can only be set by device owners and profile owners on the primary user.
483 * The default value is <code>false</code>.
Fyodor Kupolov0ffbfea2016-01-19 10:07:54 -0800484 * <p>This restriction has no effect on secondary users and managed profiles since only the
Stuart Scotte3e314d2015-04-20 14:07:45 -0700485 * primary user can reset the network settings of the device.
486 *
Fyodor Kupolov0ffbfea2016-01-19 10:07:54 -0800487 * <p>Key for user restrictions.
488 * <p>Type: Boolean
Makoto Onuki068c54a2015-10-13 14:34:03 -0700489 * @see DevicePolicyManager#addUserRestriction(ComponentName, String)
490 * @see DevicePolicyManager#clearUserRestriction(ComponentName, String)
Stuart Scotte3e314d2015-04-20 14:07:45 -0700491 * @see #getUserRestrictions()
492 */
493 public static final String DISALLOW_NETWORK_RESET = "no_network_reset";
494
495 /**
Amith Yamasani26af8292014-09-09 09:57:27 -0700496 * Specifies if a user is disallowed from factory resetting
Amith Yamasanic34dc7c2014-09-18 09:42:42 -0700497 * from Settings. This can only be set by device owners and profile owners on the primary user.
498 * The default value is <code>false</code>.
Fyodor Kupolov0ffbfea2016-01-19 10:07:54 -0800499 * <p>This restriction has no effect on secondary users and managed profiles since only the
Amith Yamasani150514b2015-01-07 16:05:05 -0800500 * primary user can factory reset the device.
Amith Yamasani26af8292014-09-09 09:57:27 -0700501 *
Fyodor Kupolov0ffbfea2016-01-19 10:07:54 -0800502 * <p>Key for user restrictions.
503 * <p>Type: Boolean
Makoto Onuki068c54a2015-10-13 14:34:03 -0700504 * @see DevicePolicyManager#addUserRestriction(ComponentName, String)
505 * @see DevicePolicyManager#clearUserRestriction(ComponentName, String)
Julia Reynoldsd46d0f92014-04-23 15:23:24 -0400506 * @see #getUserRestrictions()
507 */
508 public static final String DISALLOW_FACTORY_RESET = "no_factory_reset";
509
510 /**
Esteban Talavera6c9116a2016-11-24 16:12:44 +0000511 * Specifies if a user is disallowed from adding new users. This can only be set by device
512 * owners and profile owners on the primary user.
Amith Yamasanic34dc7c2014-09-18 09:42:42 -0700513 * The default value is <code>false</code>.
Fyodor Kupolov0ffbfea2016-01-19 10:07:54 -0800514 * <p>This restriction has no effect on secondary users and managed profiles since only the
Amith Yamasani150514b2015-01-07 16:05:05 -0800515 * primary user can add other users.
Amith Yamasani26af8292014-09-09 09:57:27 -0700516 *
Fyodor Kupolov0ffbfea2016-01-19 10:07:54 -0800517 * <p>Key for user restrictions.
518 * <p>Type: Boolean
Makoto Onuki068c54a2015-10-13 14:34:03 -0700519 * @see DevicePolicyManager#addUserRestriction(ComponentName, String)
520 * @see DevicePolicyManager#clearUserRestriction(ComponentName, String)
Julia Reynoldsd46d0f92014-04-23 15:23:24 -0400521 * @see #getUserRestrictions()
522 */
523 public static final String DISALLOW_ADD_USER = "no_add_user";
524
525 /**
Esteban Talavera6c9116a2016-11-24 16:12:44 +0000526 * Specifies if a user is disallowed from adding managed profiles.
527 * <p>The default value for an unmanaged user is <code>false</code>.
Nicolas Prevot2ea46fe2017-01-05 10:29:34 +0000528 * For users with a device owner set, the default is <code>true</code>.
529 * <p>This restriction has no effect on managed profiles.
Esteban Talavera6c9116a2016-11-24 16:12:44 +0000530 *
531 * <p>Key for user restrictions.
532 * <p>Type: Boolean
533 * @see DevicePolicyManager#addUserRestriction(ComponentName, String)
534 * @see DevicePolicyManager#clearUserRestriction(ComponentName, String)
535 * @see #getUserRestrictions()
536 */
537 public static final String DISALLOW_ADD_MANAGED_PROFILE = "no_add_managed_profile";
538
539 /**
Benjamin Millerd41a9fc2017-07-17 17:24:44 +0200540 * Specifies if a user is disallowed from disabling application verification. The default
541 * value is <code>false</code>.
542 *
543 * <p>In Android 8.0 ({@linkplain android.os.Build.VERSION_CODES#O API level 26}) and higher,
544 * this is a global user restriction. If a device owner or profile owner sets this restriction,
545 * the system enforces app verification across all users on the device. Running in earlier
546 * Android versions, this restriction affects only the profile that sets it.
Amith Yamasani26af8292014-09-09 09:57:27 -0700547 *
Fyodor Kupolov0ffbfea2016-01-19 10:07:54 -0800548 * <p>Key for user restrictions.
549 * <p>Type: Boolean
Makoto Onuki068c54a2015-10-13 14:34:03 -0700550 * @see DevicePolicyManager#addUserRestriction(ComponentName, String)
551 * @see DevicePolicyManager#clearUserRestriction(ComponentName, String)
Julia Reynoldsd46d0f92014-04-23 15:23:24 -0400552 * @see #getUserRestrictions()
553 */
554 public static final String ENSURE_VERIFY_APPS = "ensure_verify_apps";
555
556 /**
Amith Yamasani26af8292014-09-09 09:57:27 -0700557 * Specifies if a user is disallowed from configuring cell
Amith Yamasanic34dc7c2014-09-18 09:42:42 -0700558 * broadcasts. This can only be set by device owners and profile owners on the primary user.
559 * The default value is <code>false</code>.
Fyodor Kupolov0ffbfea2016-01-19 10:07:54 -0800560 * <p>This restriction has no effect on secondary users and managed profiles since only the
Amith Yamasani150514b2015-01-07 16:05:05 -0800561 * primary user can configure cell broadcasts.
Amith Yamasani26af8292014-09-09 09:57:27 -0700562 *
Fyodor Kupolov0ffbfea2016-01-19 10:07:54 -0800563 * <p>Key for user restrictions.
564 * <p>Type: Boolean
Makoto Onuki068c54a2015-10-13 14:34:03 -0700565 * @see DevicePolicyManager#addUserRestriction(ComponentName, String)
566 * @see DevicePolicyManager#clearUserRestriction(ComponentName, String)
Julia Reynoldsd46d0f92014-04-23 15:23:24 -0400567 * @see #getUserRestrictions()
568 */
569 public static final String DISALLOW_CONFIG_CELL_BROADCASTS = "no_config_cell_broadcasts";
570
571 /**
Amith Yamasani26af8292014-09-09 09:57:27 -0700572 * Specifies if a user is disallowed from configuring mobile
Amith Yamasanic34dc7c2014-09-18 09:42:42 -0700573 * networks. This can only be set by device owners and profile owners on the primary user.
574 * The default value is <code>false</code>.
Fyodor Kupolov0ffbfea2016-01-19 10:07:54 -0800575 * <p>This restriction has no effect on secondary users and managed profiles since only the
Amith Yamasani150514b2015-01-07 16:05:05 -0800576 * primary user can configure mobile networks.
Amith Yamasani26af8292014-09-09 09:57:27 -0700577 *
Fyodor Kupolov0ffbfea2016-01-19 10:07:54 -0800578 * <p>Key for user restrictions.
579 * <p>Type: Boolean
Makoto Onuki068c54a2015-10-13 14:34:03 -0700580 * @see DevicePolicyManager#addUserRestriction(ComponentName, String)
581 * @see DevicePolicyManager#clearUserRestriction(ComponentName, String)
Julia Reynoldsd46d0f92014-04-23 15:23:24 -0400582 * @see #getUserRestrictions()
583 */
584 public static final String DISALLOW_CONFIG_MOBILE_NETWORKS = "no_config_mobile_networks";
585
586 /**
Amith Yamasani26af8292014-09-09 09:57:27 -0700587 * Specifies if a user is disallowed from modifying
Julia Reynoldsc617f812014-07-25 16:32:27 -0400588 * applications in Settings or launchers. The following actions will not be allowed when this
589 * restriction is enabled:
590 * <li>uninstalling apps</li>
591 * <li>disabling apps</li>
592 * <li>clearing app caches</li>
593 * <li>clearing app data</li>
594 * <li>force stopping apps</li>
595 * <li>clearing app defaults</li>
596 * <p>
597 * The default value is <code>false</code>.
Amith Yamasani26af8292014-09-09 09:57:27 -0700598 *
Esteban Talavera8bd7c522017-02-13 12:35:04 +0000599 * <p><strong>Note:</strong> The user will still be able to perform those actions via other
600 * means (such as adb). Third party apps will also be able to uninstall apps via the
601 * {@link android.content.pm.PackageInstaller}. {@link #DISALLOW_UNINSTALL_APPS} or
602 * {@link DevicePolicyManager#setUninstallBlocked(ComponentName, String, boolean)} should be
603 * used to prevent the user from uninstalling apps completely, and
604 * {@link DevicePolicyManager#addPersistentPreferredActivity(ComponentName, IntentFilter, ComponentName)}
605 * to add a default intent handler for a given intent filter.
606 *
Fyodor Kupolov0ffbfea2016-01-19 10:07:54 -0800607 * <p>Key for user restrictions.
608 * <p>Type: Boolean
Makoto Onuki068c54a2015-10-13 14:34:03 -0700609 * @see DevicePolicyManager#addUserRestriction(ComponentName, String)
610 * @see DevicePolicyManager#clearUserRestriction(ComponentName, String)
Julia Reynoldsd46d0f92014-04-23 15:23:24 -0400611 * @see #getUserRestrictions()
612 */
Julia Reynolds36fbc8d2014-06-18 09:26:30 -0400613 public static final String DISALLOW_APPS_CONTROL = "no_control_apps";
Julia Reynoldsd46d0f92014-04-23 15:23:24 -0400614
Emily Bernier394a6cd2014-05-07 12:49:20 -0400615 /**
Amith Yamasani26af8292014-09-09 09:57:27 -0700616 * Specifies if a user is disallowed from mounting
Amith Yamasanic34dc7c2014-09-18 09:42:42 -0700617 * physical external media. This can only be set by device owners and profile owners on the
618 * primary user. The default value is <code>false</code>.
Amith Yamasani26af8292014-09-09 09:57:27 -0700619 *
Fyodor Kupolov0ffbfea2016-01-19 10:07:54 -0800620 * <p>Key for user restrictions.
621 * <p>Type: Boolean
Makoto Onuki068c54a2015-10-13 14:34:03 -0700622 * @see DevicePolicyManager#addUserRestriction(ComponentName, String)
623 * @see DevicePolicyManager#clearUserRestriction(ComponentName, String)
Emily Bernier394a6cd2014-05-07 12:49:20 -0400624 * @see #getUserRestrictions()
625 */
626 public static final String DISALLOW_MOUNT_PHYSICAL_MEDIA = "no_physical_media";
627
628 /**
Pavel Grafovce3e1a32017-04-21 14:48:21 +0100629 * Specifies if a user is disallowed from adjusting microphone volume. If set, the microphone
630 * will be muted. This can be set by device owners and profile owners. The default value is
631 * <code>false</code>.
Amith Yamasani26af8292014-09-09 09:57:27 -0700632 *
Pavel Grafovce3e1a32017-04-21 14:48:21 +0100633 * <p>This restriction has no effect on managed profiles.
Fyodor Kupolov0ffbfea2016-01-19 10:07:54 -0800634 * <p>Key for user restrictions.
635 * <p>Type: Boolean
Makoto Onuki068c54a2015-10-13 14:34:03 -0700636 * @see DevicePolicyManager#addUserRestriction(ComponentName, String)
637 * @see DevicePolicyManager#clearUserRestriction(ComponentName, String)
Emily Bernier394a6cd2014-05-07 12:49:20 -0400638 * @see #getUserRestrictions()
639 */
640 public static final String DISALLOW_UNMUTE_MICROPHONE = "no_unmute_microphone";
641
642 /**
Pavel Grafovce3e1a32017-04-21 14:48:21 +0100643 * Specifies if a user is disallowed from adjusting the master volume. If set, the master volume
Wen ZHANG61ed0dc2017-08-23 14:27:02 +0100644 * will be muted. This can be set by device owners from API 21 and profile owners from API 24.
645 * The default value is <code>false</code>.
646 *
647 * <p>When the restriction is set by profile owners, then it only applies to relevant
648 * profiles.
Amith Yamasani26af8292014-09-09 09:57:27 -0700649 *
Pavel Grafovce3e1a32017-04-21 14:48:21 +0100650 * <p>This restriction has no effect on managed profiles.
Fyodor Kupolov0ffbfea2016-01-19 10:07:54 -0800651 * <p>Key for user restrictions.
652 * <p>Type: Boolean
Makoto Onuki068c54a2015-10-13 14:34:03 -0700653 * @see DevicePolicyManager#addUserRestriction(ComponentName, String)
654 * @see DevicePolicyManager#clearUserRestriction(ComponentName, String)
Emily Bernier394a6cd2014-05-07 12:49:20 -0400655 * @see #getUserRestrictions()
656 */
657 public static final String DISALLOW_ADJUST_VOLUME = "no_adjust_volume";
658
Amith Yamasani9f6c25f2014-05-16 14:49:15 -0700659 /**
Amith Yamasani26af8292014-09-09 09:57:27 -0700660 * Specifies that the user is not allowed to make outgoing
Amith Yamasani390989d2014-07-17 10:52:03 -0700661 * phone calls. Emergency calls are still permitted.
Amith Yamasani9f6c25f2014-05-16 14:49:15 -0700662 * The default value is <code>false</code>.
Tony Makeb83ab52016-02-22 18:36:08 +0000663 * <p>This restriction has no effect on managed profiles.
Amith Yamasani26af8292014-09-09 09:57:27 -0700664 *
Fyodor Kupolov0ffbfea2016-01-19 10:07:54 -0800665 * <p>Key for user restrictions.
666 * <p>Type: Boolean
Makoto Onuki068c54a2015-10-13 14:34:03 -0700667 * @see DevicePolicyManager#addUserRestriction(ComponentName, String)
668 * @see DevicePolicyManager#clearUserRestriction(ComponentName, String)
Amith Yamasani9f6c25f2014-05-16 14:49:15 -0700669 * @see #getUserRestrictions()
670 */
Amith Yamasani390989d2014-07-17 10:52:03 -0700671 public static final String DISALLOW_OUTGOING_CALLS = "no_outgoing_calls";
672
673 /**
Amith Yamasani26af8292014-09-09 09:57:27 -0700674 * Specifies that the user is not allowed to send or receive
Amith Yamasanic34dc7c2014-09-18 09:42:42 -0700675 * SMS messages. The default value is <code>false</code>.
Amith Yamasani26af8292014-09-09 09:57:27 -0700676 *
Fyodor Kupolov0ffbfea2016-01-19 10:07:54 -0800677 * <p>Key for user restrictions.
678 * <p>Type: Boolean
Makoto Onuki068c54a2015-10-13 14:34:03 -0700679 * @see DevicePolicyManager#addUserRestriction(ComponentName, String)
680 * @see DevicePolicyManager#clearUserRestriction(ComponentName, String)
Amith Yamasani390989d2014-07-17 10:52:03 -0700681 * @see #getUserRestrictions()
682 */
683 public static final String DISALLOW_SMS = "no_sms";
Amith Yamasani9f6c25f2014-05-16 14:49:15 -0700684
Jason Monk1c7c3192014-06-26 12:52:18 -0400685 /**
Jeff Sharkey2cc03e52015-03-20 11:24:04 -0700686 * Specifies if the user is not allowed to have fun. In some cases, the
687 * device owner may wish to prevent the user from experiencing amusement or
688 * joy while using the device. The default value is <code>false</code>.
689 *
Fyodor Kupolov0ffbfea2016-01-19 10:07:54 -0800690 * <p>Key for user restrictions.
691 * <p>Type: Boolean
Makoto Onuki068c54a2015-10-13 14:34:03 -0700692 * @see DevicePolicyManager#addUserRestriction(ComponentName, String)
693 * @see DevicePolicyManager#clearUserRestriction(ComponentName, String)
Jeff Sharkey2cc03e52015-03-20 11:24:04 -0700694 * @see #getUserRestrictions()
695 */
696 public static final String DISALLOW_FUN = "no_fun";
697
698 /**
Amith Yamasani26af8292014-09-09 09:57:27 -0700699 * Specifies that windows besides app windows should not be
Jason Monk1c7c3192014-06-26 12:52:18 -0400700 * created. This will block the creation of the following types of windows.
701 * <li>{@link LayoutParams#TYPE_TOAST}</li>
702 * <li>{@link LayoutParams#TYPE_PHONE}</li>
703 * <li>{@link LayoutParams#TYPE_PRIORITY_PHONE}</li>
704 * <li>{@link LayoutParams#TYPE_SYSTEM_ALERT}</li>
705 * <li>{@link LayoutParams#TYPE_SYSTEM_ERROR}</li>
706 * <li>{@link LayoutParams#TYPE_SYSTEM_OVERLAY}</li>
Wale Ogunwale5cd907d2017-01-26 14:14:08 -0800707 * <li>{@link LayoutParams#TYPE_APPLICATION_OVERLAY}</li>
Jason Monk1c7c3192014-06-26 12:52:18 -0400708 *
Amith Yamasanic34dc7c2014-09-18 09:42:42 -0700709 * <p>This can only be set by device owners and profile owners on the primary user.
710 * The default value is <code>false</code>.
Amith Yamasani26af8292014-09-09 09:57:27 -0700711 *
Fyodor Kupolov0ffbfea2016-01-19 10:07:54 -0800712 * <p>Key for user restrictions.
713 * <p>Type: Boolean
Makoto Onuki068c54a2015-10-13 14:34:03 -0700714 * @see DevicePolicyManager#addUserRestriction(ComponentName, String)
715 * @see DevicePolicyManager#clearUserRestriction(ComponentName, String)
Jason Monk1c7c3192014-06-26 12:52:18 -0400716 * @see #getUserRestrictions()
717 */
718 public static final String DISALLOW_CREATE_WINDOWS = "no_create_windows";
719
Nicolas Prevotf1939902014-06-25 09:29:02 +0100720 /**
Charles He22ff6f9d2017-10-05 21:28:55 +0100721 * Specifies that system error dialogs for crashed or unresponsive apps should not be shown.
722 * 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 +0000723 * option on the UI. A feedback report isn't collected as there is no way for the user to
724 * provide explicit consent. The default value is <code>false</code>.
Charles He22ff6f9d2017-10-05 21:28:55 +0100725 *
Benjamin Franz0c86fe12018-02-28 09:49:17 +0000726 * <p>When this user restriction is set by device owners, it's applied to all users. When set by
727 * the profile owner of the primary user or a secondary user, the restriction affects only the
728 * calling user. This user restriction has no effect on managed profiles.
Charles He22ff6f9d2017-10-05 21:28:55 +0100729 *
Charles He22ff6f9d2017-10-05 21:28:55 +0100730 * <p>Key for user restrictions.
731 * <p>Type: Boolean
732 * @see DevicePolicyManager#addUserRestriction(ComponentName, String)
733 * @see DevicePolicyManager#clearUserRestriction(ComponentName, String)
734 * @see #getUserRestrictions()
735 */
736 public static final String DISALLOW_SYSTEM_ERROR_DIALOGS = "no_system_error_dialogs";
737
738 /**
Benjamin Miller8cc730d2019-01-18 19:03:40 +0000739 * Specifies if the clipboard contents can be exported by pasting the data into other users or
740 * profiles. This restriction doesn't prevent import, such as someone pasting clipboard data
741 * from other profiles or users. The default value is {@code false}.
742 *
743 * <p><strong>Note</strong>: Because it's possible to extract data from screenshots using
744 * optical character recognition (OCR), we strongly recommend combining this user restriction
745 * with {@link DevicePolicyManager#setScreenCaptureDisabled(ComponentName, boolean)}.
Amith Yamasani26af8292014-09-09 09:57:27 -0700746 *
Fyodor Kupolov0ffbfea2016-01-19 10:07:54 -0800747 * <p>Key for user restrictions.
748 * <p>Type: Boolean
Makoto Onuki068c54a2015-10-13 14:34:03 -0700749 * @see DevicePolicyManager#addUserRestriction(ComponentName, String)
750 * @see DevicePolicyManager#clearUserRestriction(ComponentName, String)
Nicolas Prevotf1939902014-06-25 09:29:02 +0100751 * @see #getUserRestrictions()
752 */
753 public static final String DISALLOW_CROSS_PROFILE_COPY_PASTE = "no_cross_profile_copy_paste";
754
Amith Yamasani26af8292014-09-09 09:57:27 -0700755 /**
756 * Specifies if the user is not allowed to use NFC to beam out data from apps.
757 * The default value is <code>false</code>.
758 *
Fyodor Kupolov0ffbfea2016-01-19 10:07:54 -0800759 * <p>Key for user restrictions.
760 * <p>Type: Boolean
Makoto Onuki068c54a2015-10-13 14:34:03 -0700761 * @see DevicePolicyManager#addUserRestriction(ComponentName, String)
762 * @see DevicePolicyManager#clearUserRestriction(ComponentName, String)
Amith Yamasani26af8292014-09-09 09:57:27 -0700763 * @see #getUserRestrictions()
764 */
765 public static final String DISALLOW_OUTGOING_BEAM = "no_outgoing_beam";
766
Sander Alewijnse53d63dc2014-11-07 21:43:00 +0000767 /**
Oleksandr Peletskyif2519812016-01-26 20:16:06 +0100768 * Hidden user restriction to disallow access to wallpaper manager APIs. This restriction
769 * generally means that wallpapers are not supported for the particular user. This user
770 * restriction is always set for managed profiles, because such profiles don't have wallpapers.
Benjamin Franzf3ece362015-02-11 10:51:10 +0000771 * @hide
Oleksandr Peletskyif2519812016-01-26 20:16:06 +0100772 * @see #DISALLOW_SET_WALLPAPER
Makoto Onuki068c54a2015-10-13 14:34:03 -0700773 * @see DevicePolicyManager#addUserRestriction(ComponentName, String)
774 * @see DevicePolicyManager#clearUserRestriction(ComponentName, String)
Benjamin Franzf3ece362015-02-11 10:51:10 +0000775 * @see #getUserRestrictions()
776 */
777 public static final String DISALLOW_WALLPAPER = "no_wallpaper";
778
779 /**
Oleksandr Peletskyif2519812016-01-26 20:16:06 +0100780 * User restriction to disallow setting a wallpaper. Profile owner and device owner
781 * are able to set wallpaper regardless of this restriction.
782 * The default value is <code>false</code>.
783 *
784 * <p>Key for user restrictions.
785 * <p>Type: Boolean
786 * @see DevicePolicyManager#addUserRestriction(ComponentName, String)
787 * @see DevicePolicyManager#clearUserRestriction(ComponentName, String)
788 * @see #getUserRestrictions()
789 */
790 public static final String DISALLOW_SET_WALLPAPER = "no_set_wallpaper";
791
792 /**
Benjamin Franzbff46ba2015-03-05 18:33:51 +0000793 * Specifies if the user is not allowed to reboot the device into safe boot mode.
794 * This can only be set by device owners and profile owners on the primary user.
795 * The default value is <code>false</code>.
796 *
Fyodor Kupolov0ffbfea2016-01-19 10:07:54 -0800797 * <p>Key for user restrictions.
798 * <p>Type: Boolean
Makoto Onuki068c54a2015-10-13 14:34:03 -0700799 * @see DevicePolicyManager#addUserRestriction(ComponentName, String)
800 * @see DevicePolicyManager#clearUserRestriction(ComponentName, String)
Benjamin Franzbff46ba2015-03-05 18:33:51 +0000801 * @see #getUserRestrictions()
802 */
803 public static final String DISALLOW_SAFE_BOOT = "no_safe_boot";
804
805 /**
Fyodor Kupolovb5013302015-04-17 17:59:14 -0700806 * Specifies if a user is not allowed to record audio. This restriction is always enabled for
807 * background users. The default value is <code>false</code>.
808 *
Makoto Onuki068c54a2015-10-13 14:34:03 -0700809 * @see DevicePolicyManager#addUserRestriction(ComponentName, String)
810 * @see DevicePolicyManager#clearUserRestriction(ComponentName, String)
Fyodor Kupolovb5013302015-04-17 17:59:14 -0700811 * @see #getUserRestrictions()
812 * @hide
813 */
Andrei Onea24ec3212019-03-15 17:35:05 +0000814 @UnsupportedAppUsage
Fyodor Kupolovb5013302015-04-17 17:59:14 -0700815 public static final String DISALLOW_RECORD_AUDIO = "no_record_audio";
816
817 /**
Fyodor Kupolov9cbfc9e2015-10-07 15:52:33 -0700818 * Specifies if a user is not allowed to run in the background and should be stopped during
819 * user switch. The default value is <code>false</code>.
820 *
821 * <p>This restriction can be set by device owners and profile owners.
822 *
823 * @see DevicePolicyManager#addUserRestriction(ComponentName, String)
824 * @see DevicePolicyManager#clearUserRestriction(ComponentName, String)
825 * @see #getUserRestrictions()
826 * @hide
827 */
Robin Lee6cfb7a92017-12-18 15:14:46 +0100828 @SystemApi
Fyodor Kupolov9cbfc9e2015-10-07 15:52:33 -0700829 public static final String DISALLOW_RUN_IN_BACKGROUND = "no_run_in_background";
830
831 /**
Makoto Onuki759a7632015-10-28 16:43:10 -0700832 * Specifies if a user is not allowed to use the camera.
833 *
834 * @see DevicePolicyManager#addUserRestriction(ComponentName, String)
835 * @see DevicePolicyManager#clearUserRestriction(ComponentName, String)
836 * @see #getUserRestrictions()
837 * @hide
838 */
839 public static final String DISALLOW_CAMERA = "no_camera";
840
841 /**
Tony Makc1205112016-07-22 16:02:59 +0100842 * Specifies if a user is not allowed to unmute the device's master volume.
843 *
844 * @see DevicePolicyManager#setMasterVolumeMuted(ComponentName, boolean)
845 * @see DevicePolicyManager#clearUserRestriction(ComponentName, String)
846 * @see #getUserRestrictions()
847 * @hide
848 */
Esteban Talavera492b4722017-02-13 14:59:45 +0000849 public static final String DISALLOW_UNMUTE_DEVICE = "disallow_unmute_device";
Tony Makc1205112016-07-22 16:02:59 +0100850
851 /**
Mahaver Chopradea471e2015-12-17 11:02:37 +0000852 * Specifies if a user is not allowed to use cellular data when roaming. This can only be set by
853 * device owners. The default value is <code>false</code>.
854 *
855 * @see DevicePolicyManager#addUserRestriction(ComponentName, String)
856 * @see DevicePolicyManager#clearUserRestriction(ComponentName, String)
857 * @see #getUserRestrictions()
858 */
859 public static final String DISALLOW_DATA_ROAMING = "no_data_roaming";
860
861 /**
Oleksandr Peletskyi7f1f1df2016-01-18 15:40:21 +0100862 * Specifies if a user is not allowed to change their icon. Device owner and profile owner
863 * can set this restriction. When it is set by device owner, only the target user will be
864 * affected. The default value is <code>false</code>.
865 *
866 * <p>Key for user restrictions.
Oleksandr Peletskyi7f1f1df2016-01-18 15:40:21 +0100867 * <p>Type: Boolean
Oleksandr Peletskyi7f1f1df2016-01-18 15:40:21 +0100868 * @see DevicePolicyManager#addUserRestriction(ComponentName, String)
869 * @see DevicePolicyManager#clearUserRestriction(ComponentName, String)
870 * @see #getUserRestrictions()
871 */
872 public static final String DISALLOW_SET_USER_ICON = "no_set_user_icon";
873
874 /**
Mahaver Chopra3d9805d2016-07-07 16:25:05 +0100875 * Specifies if a user is not allowed to enable the oem unlock setting. The default value is
Esteban Talaverac48b20f2016-08-11 11:23:40 +0100876 * <code>false</code>. Setting this restriction has no effect if the bootloader is already
877 * unlocked.
Mahaver Chopra3d9805d2016-07-07 16:25:05 +0100878 *
Lenka Trochtova12b04962016-11-29 21:00:12 +0100879 * <p>Not for use by third-party applications.
880 *
Mahaver Chopra3d9805d2016-07-07 16:25:05 +0100881 * @see DevicePolicyManager#addUserRestriction(ComponentName, String)
882 * @see DevicePolicyManager#clearUserRestriction(ComponentName, String)
883 * @see #getUserRestrictions()
Andrew Scull3b8b46f2017-02-13 18:12:15 +0000884 * @deprecated use {@link OemLockManager#setOemUnlockAllowedByCarrier(boolean, byte[])} instead.
Mahaver Chopra3d9805d2016-07-07 16:25:05 +0100885 * @hide
886 */
Andrew Scull3b8b46f2017-02-13 18:12:15 +0000887 @Deprecated
Lenka Trochtova12b04962016-11-29 21:00:12 +0100888 @SystemApi
Mahaver Chopra3d9805d2016-07-07 16:25:05 +0100889 public static final String DISALLOW_OEM_UNLOCK = "no_oem_unlock";
890
891 /**
Pavel Grafovc4f87e92017-10-26 16:34:25 +0100892 * Specifies that the managed profile is not allowed to have unified lock screen challenge with
893 * the primary user.
894 *
895 * <p><strong>Note:</strong> Setting this restriction alone doesn't automatically set a
896 * separate challenge. Profile owner can ask the user to set a new password using
897 * {@link DevicePolicyManager#ACTION_SET_NEW_PASSWORD} and verify it using
898 * {@link DevicePolicyManager#isUsingUnifiedPassword(ComponentName)}.
899 *
900 * <p>Can be set by profile owners. It only has effect on managed profiles when set by managed
901 * profile owner. Has no effect on non-managed profiles or users.
902 * <p>Key for user restrictions.
903 * <p>Type: Boolean
904 * @see DevicePolicyManager#addUserRestriction(ComponentName, String)
905 * @see DevicePolicyManager#clearUserRestriction(ComponentName, String)
906 * @see #getUserRestrictions()
907 */
908 public static final String DISALLOW_UNIFIED_PASSWORD = "no_unified_password";
909
910 /**
Nicolas Prevotf0029c12015-06-25 14:25:41 -0700911 * Allows apps in the parent profile to handle web links from the managed profile.
912 *
Nicolas Prevot9edbda12015-06-17 11:09:48 -0700913 * This user restriction has an effect only in a managed profile.
914 * If set:
915 * Intent filters of activities in the parent profile with action
916 * {@link android.content.Intent#ACTION_VIEW},
917 * category {@link android.content.Intent#CATEGORY_BROWSABLE}, scheme http or https, and which
918 * define a host can handle intents from the managed profile.
919 * The default value is <code>false</code>.
920 *
Fyodor Kupolov0ffbfea2016-01-19 10:07:54 -0800921 * <p>Key for user restrictions.
922 * <p>Type: Boolean
Makoto Onuki068c54a2015-10-13 14:34:03 -0700923 * @see DevicePolicyManager#addUserRestriction(ComponentName, String)
924 * @see DevicePolicyManager#clearUserRestriction(ComponentName, String)
Nicolas Prevot9edbda12015-06-17 11:09:48 -0700925 * @see #getUserRestrictions()
926 */
Nicolas Prevotf0029c12015-06-25 14:25:41 -0700927 public static final String ALLOW_PARENT_PROFILE_APP_LINKING
928 = "allow_parent_profile_app_linking";
Nicolas Prevot9edbda12015-06-17 11:09:48 -0700929
930 /**
Felipe Leme24d58932017-03-21 14:13:58 -0700931 * Specifies if a user is not allowed to use Autofill Services.
932 *
933 * <p>Device owner and profile owner can set this restriction. When it is set by device owner,
934 * only the target user will be affected.
935 *
936 * <p>The default value is <code>false</code>.
937 *
938 * @see DevicePolicyManager#addUserRestriction(ComponentName, String)
939 * @see DevicePolicyManager#clearUserRestriction(ComponentName, String)
940 * @see #getUserRestrictions()
941 */
942 public static final String DISALLOW_AUTOFILL = "no_autofill";
943
944 /**
Felipe Leme1dfa9a02018-10-17 17:24:37 -0700945 * Specifies if the contents of a user's screen is not allowed to be captured for artificial
946 * intelligence purposes.
947 *
948 * <p>Device owner and profile owner can set this restriction. When it is set by device owner,
949 * only the target user will be affected.
950 *
951 * <p>The default value is <code>false</code>.
952 *
953 * @see DevicePolicyManager#addUserRestriction(ComponentName, String)
954 * @see DevicePolicyManager#clearUserRestriction(ComponentName, String)
955 * @see #getUserRestrictions()
956 */
Felipe Leme749b8892018-12-03 16:30:30 -0800957 public static final String DISALLOW_CONTENT_CAPTURE = "no_content_capture";
Felipe Leme1dfa9a02018-10-17 17:24:37 -0700958
959 /**
Zak Cohen3e12ac72019-03-05 16:50:39 -0800960 * Specifies if the current user is able to receive content suggestions for selections based on
961 * the contents of their screen.
962 *
963 * <p>Device owner and profile owner can set this restriction. When it is set by device owner,
964 * only the target user will be affected.
965 *
966 * <p>The default value is <code>false</code>.
967 *
968 * @see DevicePolicyManager#addUserRestriction(ComponentName, String)
969 * @see DevicePolicyManager#clearUserRestriction(ComponentName, String)
970 * @see #getUserRestrictions()
971 */
972 public static final String DISALLOW_CONTENT_SUGGESTIONS = "no_content_suggestions";
973
974 /**
Benjamin Franzff66fa92017-08-10 10:39:44 +0100975 * Specifies if user switching is blocked on the current user.
976 *
977 * <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 +0800978 * Device owner can still switch user via
979 * {@link DevicePolicyManager#switchUser(ComponentName, UserHandle)} when this restriction is
980 * set.
Benjamin Franzff66fa92017-08-10 10:39:44 +0100981 *
982 * <p>The default value is <code>false</code>.
983 *
984 * @see DevicePolicyManager#addUserRestriction(ComponentName, String)
985 * @see DevicePolicyManager#clearUserRestriction(ComponentName, String)
986 * @see #getUserRestrictions()
987 */
988 public static final String DISALLOW_USER_SWITCH = "no_user_switch";
989
990 /**
Rubin Xucc391c22018-01-02 20:37:35 +0000991 * Specifies whether the user can share file / picture / data from the primary user into the
992 * managed profile, either by sending them from the primary side, or by picking up data within
993 * an app in the managed profile.
994 * <p>
995 * When a managed profile is created, the system allows the user to send data from the primary
996 * side to the profile by setting up certain default cross profile intent filters. If
997 * this is undesired, this restriction can be set to disallow it. Note that this restriction
998 * will not block any sharing allowed by explicit
999 * {@link DevicePolicyManager#addCrossProfileIntentFilter} calls by the profile owner.
1000 * <p>
1001 * This restriction is only meaningful when set by profile owner. When it is set by device
1002 * owner, it does not have any effect.
1003 * <p>
1004 * The default value is <code>false</code>.
1005 *
1006 * @see DevicePolicyManager#addUserRestriction(ComponentName, String)
1007 * @see DevicePolicyManager#clearUserRestriction(ComponentName, String)
1008 * @see #getUserRestrictions()
1009 */
1010 public static final String DISALLOW_SHARE_INTO_MANAGED_PROFILE = "no_sharing_into_profile";
Vladislav Kuzkokov622b9f92018-01-25 16:33:05 +01001011
1012 /**
1013 * Specifies whether the user is allowed to print.
1014 *
1015 * This restriction can be set by device or profile owner.
1016 *
1017 * The default value is {@code false}.
1018 *
1019 * @see DevicePolicyManager#addUserRestriction(ComponentName, String)
1020 * @see DevicePolicyManager#clearUserRestriction(ComponentName, String)
1021 * @see #getUserRestrictions()
1022 */
1023 public static final String DISALLOW_PRINTING = "no_printing";
1024
Rubin Xucc391c22018-01-02 20:37:35 +00001025 /**
Eran Messeri09b122da2018-10-05 15:33:53 +01001026 * Specifies whether the user is allowed to modify private DNS settings.
1027 *
1028 * <p>The default value is <code>false</code>.
1029 *
1030 * <p>This user restriction can only be applied by the Device Owner.
1031 * <p>Key for user restrictions.
1032 * <p>Type: Boolean
1033 * @see DevicePolicyManager#addUserRestriction(ComponentName, String)
1034 * @see DevicePolicyManager#clearUserRestriction(ComponentName, String)
1035 * @see #getUserRestrictions()
1036 */
1037 public static final String DISALLOW_CONFIG_PRIVATE_DNS =
1038 "disallow_config_private_dns";
1039
1040 /**
Sander Alewijnse53d63dc2014-11-07 21:43:00 +00001041 * Application restriction key that is used to indicate the pending arrival
1042 * of real restrictions for the app.
1043 *
1044 * <p>
1045 * Applications that support restrictions should check for the presence of this key.
1046 * A <code>true</code> value indicates that restrictions may be applied in the near
1047 * future but are not available yet. It is the responsibility of any
1048 * management application that sets this flag to update it when the final
1049 * restrictions are enforced.
1050 *
Fyodor Kupolov0ffbfea2016-01-19 10:07:54 -08001051 * <p>Key for application restrictions.
1052 * <p>Type: Boolean
Nicolas Prevotb14ed952015-01-13 14:23:53 +00001053 * @see android.app.admin.DevicePolicyManager#setApplicationRestrictions(
1054 * android.content.ComponentName, String, Bundle)
1055 * @see android.app.admin.DevicePolicyManager#getApplicationRestrictions(
1056 * android.content.ComponentName, String)
Sander Alewijnse53d63dc2014-11-07 21:43:00 +00001057 */
1058 public static final String KEY_RESTRICTIONS_PENDING = "restrictions_pending";
1059
Amith Yamasani12747872015-12-07 14:19:49 -08001060 private static final String ACTION_CREATE_USER = "android.os.action.CREATE_USER";
1061
1062 /**
1063 * Extra containing a name for the user being created. Optional parameter passed to
1064 * ACTION_CREATE_USER activity.
1065 * @hide
1066 */
1067 public static final String EXTRA_USER_NAME = "android.os.extra.USER_NAME";
1068
1069 /**
1070 * Extra containing account name for the user being created. Optional parameter passed to
1071 * ACTION_CREATE_USER activity.
1072 * @hide
1073 */
1074 public static final String EXTRA_USER_ACCOUNT_NAME = "android.os.extra.USER_ACCOUNT_NAME";
1075
1076 /**
1077 * Extra containing account type for the user being created. Optional parameter passed to
1078 * ACTION_CREATE_USER activity.
1079 * @hide
1080 */
1081 public static final String EXTRA_USER_ACCOUNT_TYPE = "android.os.extra.USER_ACCOUNT_TYPE";
1082
1083 /**
1084 * Extra containing account-specific data for the user being created. Optional parameter passed
1085 * to ACTION_CREATE_USER activity.
1086 * @hide
1087 */
1088 public static final String EXTRA_USER_ACCOUNT_OPTIONS
1089 = "android.os.extra.USER_ACCOUNT_OPTIONS";
1090
Amith Yamasani655d0e22013-06-12 14:19:10 -07001091 /** @hide */
1092 public static final int PIN_VERIFICATION_FAILED_INCORRECT = -3;
1093 /** @hide */
1094 public static final int PIN_VERIFICATION_FAILED_NOT_SET = -2;
1095 /** @hide */
1096 public static final int PIN_VERIFICATION_SUCCESS = -1;
1097
Amith Yamasani37ed8d12016-01-27 14:40:16 -08001098 /**
Makoto Onukie72f81b2017-03-16 14:08:19 -07001099 * Sent when user restrictions have changed.
1100 *
1101 * @hide
1102 */
1103 @SystemApi
1104 @TestApi // To allow seeing it from CTS.
1105 public static final String ACTION_USER_RESTRICTIONS_CHANGED =
1106 "android.os.action.USER_RESTRICTIONS_CHANGED";
1107
1108 /**
Amith Yamasani37ed8d12016-01-27 14:40:16 -08001109 * Error result indicating that this user is not allowed to add other users on this device.
1110 * This is a result code returned from the activity created by the intent
1111 * {@link #createUserCreationIntent(String, String, String, PersistableBundle)}.
1112 */
1113 public static final int USER_CREATION_FAILED_NOT_PERMITTED = Activity.RESULT_FIRST_USER;
1114
1115 /**
1116 * Error result indicating that no more users can be created on this device.
1117 * This is a result code returned from the activity created by the intent
1118 * {@link #createUserCreationIntent(String, String, String, PersistableBundle)}.
1119 */
1120 public static final int USER_CREATION_FAILED_NO_MORE_USERS = Activity.RESULT_FIRST_USER + 1;
1121
Alex Chau89386ba2018-02-13 11:09:17 +08001122 /**
Varun Shah50ef2002019-03-04 16:41:12 -08001123 * Indicates that users are switchable.
1124 * @hide
1125 */
1126 @SystemApi
1127 public static final int SWITCHABILITY_STATUS_OK = 0;
1128
1129 /**
1130 * Indicated that the user is in a phone call.
1131 * @hide
1132 */
1133 @SystemApi
1134 public static final int SWITCHABILITY_STATUS_USER_IN_CALL = 1 << 0;
1135
1136 /**
1137 * Indicates that user switching is disallowed ({@link #DISALLOW_USER_SWITCH} is set).
1138 * @hide
1139 */
1140 @SystemApi
1141 public static final int SWITCHABILITY_STATUS_USER_SWITCH_DISALLOWED = 1 << 1;
1142
1143 /**
1144 * Indicates that the system user is locked and user switching is not allowed.
1145 * @hide
1146 */
1147 @SystemApi
1148 public static final int SWITCHABILITY_STATUS_SYSTEM_USER_LOCKED = 1 << 2;
1149
1150 /**
1151 * Result returned in {@link #getUserSwitchability()} indicating user swichability.
1152 * @hide
1153 */
1154 @Retention(RetentionPolicy.SOURCE)
1155 @IntDef(flag = true, prefix = { "SWITCHABILITY_STATUS_" }, value = {
1156 SWITCHABILITY_STATUS_OK,
1157 SWITCHABILITY_STATUS_USER_IN_CALL,
1158 SWITCHABILITY_STATUS_USER_SWITCH_DISALLOWED,
1159 SWITCHABILITY_STATUS_SYSTEM_USER_LOCKED
1160 })
1161 public @interface UserSwitchabilityResult {}
1162
1163 /**
Alex Chau89386ba2018-02-13 11:09:17 +08001164 * Indicates user operation is successful.
1165 */
1166 public static final int USER_OPERATION_SUCCESS = 0;
1167
1168 /**
1169 * Indicates user operation failed for unknown reason.
1170 */
1171 public static final int USER_OPERATION_ERROR_UNKNOWN = 1;
1172
1173 /**
1174 * Indicates user operation failed because target user is a managed profile.
1175 */
1176 public static final int USER_OPERATION_ERROR_MANAGED_PROFILE = 2;
1177
1178 /**
1179 * Indicates user operation failed because maximum running user limit has been reached.
1180 */
1181 public static final int USER_OPERATION_ERROR_MAX_RUNNING_USERS = 3;
1182
1183 /**
1184 * Indicates user operation failed because the target user is in the foreground.
1185 */
1186 public static final int USER_OPERATION_ERROR_CURRENT_USER = 4;
1187
1188 /**
1189 * Indicates user operation failed because device has low data storage.
1190 */
1191 public static final int USER_OPERATION_ERROR_LOW_STORAGE = 5;
1192
1193 /**
1194 * Indicates user operation failed because maximum user limit has been reached.
1195 */
1196 public static final int USER_OPERATION_ERROR_MAX_USERS = 6;
1197
1198 /**
1199 * Result returned from various user operations.
1200 *
1201 * @hide
1202 */
1203 @Retention(RetentionPolicy.SOURCE)
1204 @IntDef(prefix = { "USER_OPERATION_" }, value = {
1205 USER_OPERATION_SUCCESS,
1206 USER_OPERATION_ERROR_UNKNOWN,
1207 USER_OPERATION_ERROR_MANAGED_PROFILE,
1208 USER_OPERATION_ERROR_MAX_RUNNING_USERS,
1209 USER_OPERATION_ERROR_CURRENT_USER,
1210 USER_OPERATION_ERROR_LOW_STORAGE,
1211 USER_OPERATION_ERROR_MAX_USERS
1212 })
1213 public @interface UserOperationResult {}
1214
1215 /**
1216 * Thrown to indicate user operation failed.
1217 */
1218 public static class UserOperationException extends RuntimeException {
1219 private final @UserOperationResult int mUserOperationResult;
1220
1221 /**
1222 * Constructs a UserOperationException with specific result code.
1223 *
1224 * @param message the detail message
1225 * @param userOperationResult the result code
1226 * @hide
1227 */
1228 public UserOperationException(String message,
1229 @UserOperationResult int userOperationResult) {
1230 super(message);
1231 mUserOperationResult = userOperationResult;
1232 }
1233
1234 /**
1235 * Returns the operation result code.
1236 */
1237 public @UserOperationResult int getUserOperationResult() {
1238 return mUserOperationResult;
1239 }
1240 }
1241
Amith Yamasani7e99bc02013-04-16 18:24:51 -07001242 /** @hide */
Andrei Onea24ec3212019-03-15 17:35:05 +00001243 @UnsupportedAppUsage
Amith Yamasanic0688302015-10-30 10:40:03 -07001244 public static UserManager get(Context context) {
1245 return (UserManager) context.getSystemService(Context.USER_SERVICE);
Amith Yamasani27db4682013-03-30 17:07:47 -07001246 }
Maggie Benthalla12fccf2013-03-14 18:02:12 -04001247
Amith Yamasani258848d2012-08-10 17:06:33 -07001248 /** @hide */
1249 public UserManager(Context context, IUserManager service) {
1250 mService = service;
Fyodor Kupolov5200e1c2016-10-17 18:46:16 -07001251 mContext = context.getApplicationContext();
Amith Yamasani258848d2012-08-10 17:06:33 -07001252 }
1253
1254 /**
Amith Yamasani06964342016-04-15 13:55:01 -07001255 * Returns whether this device supports multiple users with their own login and customizable
1256 * space.
1257 * @return whether the device supports multiple users.
Amith Yamasani258848d2012-08-10 17:06:33 -07001258 */
Jeff Sharkey4673e7e2012-09-19 13:14:30 -07001259 public static boolean supportsMultipleUsers() {
Kenny Guy1a447532014-02-20 21:55:32 +00001260 return getMaxSupportedUsers() > 1
1261 && SystemProperties.getBoolean("fw.show_multiuserui",
1262 Resources.getSystem().getBoolean(R.bool.config_enableMultiUserUI));
Amith Yamasani258848d2012-08-10 17:06:33 -07001263 }
1264
Maggie Benthall67944582013-02-22 14:58:27 -05001265 /**
Xiaohui Chen7cb69df2015-07-13 16:01:01 -07001266 * @hide
1267 * @return Whether the device is running with split system user. It means the system user and
1268 * primary user are two separate users. Previously system user and primary user are combined as
1269 * a single owner user. see @link {android.os.UserHandle#USER_OWNER}
1270 */
Philip P. Moltmannf80809f2018-04-04 11:20:44 -07001271 @TestApi
Xiaohui Chen7cb69df2015-07-13 16:01:01 -07001272 public static boolean isSplitSystemUser() {
John Reckaa67f682016-09-20 14:24:21 -07001273 return RoSystemProperties.FW_SYSTEM_USER_SPLIT;
Xiaohui Chen7cb69df2015-07-13 16:01:01 -07001274 }
1275
1276 /**
Evan Rosky13a58a92016-07-27 15:51:09 -07001277 * @return Whether guest user is always ephemeral
1278 * @hide
1279 */
1280 public static boolean isGuestUserEphemeral() {
1281 return Resources.getSystem()
1282 .getBoolean(com.android.internal.R.bool.config_guestUserEphemeral);
1283 }
1284
1285 /**
Varun Shah50ef2002019-03-04 16:41:12 -08001286 * @deprecated use {@link #getUserSwitchability()} instead.
1287 *
1288 * @removed
Fyodor Kupolov523c4042016-02-24 15:03:13 -08001289 * @hide
1290 */
Varun Shah50ef2002019-03-04 16:41:12 -08001291 @Deprecated
1292 @UnsupportedAppUsage
Fyodor Kupolov523c4042016-02-24 15:03:13 -08001293 public boolean canSwitchUsers() {
1294 boolean allowUserSwitchingWhenSystemUserLocked = Settings.Global.getInt(
1295 mContext.getContentResolver(),
1296 Settings.Global.ALLOW_USER_SWITCHING_WHEN_SYSTEM_USER_LOCKED, 0) != 0;
1297 boolean isSystemUserUnlocked = isUserUnlocked(UserHandle.SYSTEM);
1298 boolean inCall = TelephonyManager.getDefault().getCallState()
1299 != TelephonyManager.CALL_STATE_IDLE;
Benjamin Franzff66fa92017-08-10 10:39:44 +01001300 boolean isUserSwitchDisallowed = hasUserRestriction(DISALLOW_USER_SWITCH);
1301 return (allowUserSwitchingWhenSystemUserLocked || isSystemUserUnlocked) && !inCall
1302 && !isUserSwitchDisallowed;
Fyodor Kupolov523c4042016-02-24 15:03:13 -08001303 }
1304
1305 /**
Varun Shah50ef2002019-03-04 16:41:12 -08001306 * Returns whether switching users is currently allowed.
1307 * <p>
1308 * Switching users is not allowed in the following cases:
1309 * <li>the user is in a phone call</li>
1310 * <li>{@link #DISALLOW_USER_SWITCH} is set</li>
1311 * <li>system user hasn't been unlocked yet</li>
1312 *
1313 * @return A {@link UserSwitchabilityResult} flag indicating if the user is switchable.
1314 * @hide
1315 */
1316 @SystemApi
1317 @RequiresPermission(allOf = {Manifest.permission.READ_PHONE_STATE,
1318 android.Manifest.permission.MANAGE_USERS,
1319 android.Manifest.permission.INTERACT_ACROSS_USERS}, conditional = true)
1320 public @UserSwitchabilityResult int getUserSwitchability() {
1321 final boolean allowUserSwitchingWhenSystemUserLocked = Settings.Global.getInt(
1322 mContext.getContentResolver(),
1323 Settings.Global.ALLOW_USER_SWITCHING_WHEN_SYSTEM_USER_LOCKED, 0) != 0;
1324 final boolean systemUserUnlocked = isUserUnlocked(UserHandle.SYSTEM);
1325 final TelephonyManager tm =
1326 (TelephonyManager) mContext.getSystemService(Context.TELEPHONY_SERVICE);
1327
1328 int flags = SWITCHABILITY_STATUS_OK;
1329 if (tm.getCallState() != TelephonyManager.CALL_STATE_IDLE) {
1330 flags |= SWITCHABILITY_STATUS_USER_IN_CALL;
1331 }
1332 if (hasUserRestriction(DISALLOW_USER_SWITCH)) {
1333 flags |= SWITCHABILITY_STATUS_USER_SWITCH_DISALLOWED;
1334 }
1335 if (!allowUserSwitchingWhenSystemUserLocked && !systemUserUnlocked) {
1336 flags |= SWITCHABILITY_STATUS_SYSTEM_USER_LOCKED;
1337 }
1338 return flags;
1339 }
1340
1341 /**
Amith Yamasani5760e172015-04-17 18:42:41 -07001342 * Returns the user handle for the user that this process is running under.
Jessica Hummelbe81c802014-04-22 15:49:22 +01001343 *
Amith Yamasani5760e172015-04-17 18:42:41 -07001344 * @return the user handle of this process.
Amith Yamasani258848d2012-08-10 17:06:33 -07001345 * @hide
Maggie Benthall67944582013-02-22 14:58:27 -05001346 */
Andrei Onea24ec3212019-03-15 17:35:05 +00001347 @UnsupportedAppUsage
Jeff Sharkey8588bc12016-01-06 16:47:42 -07001348 public @UserIdInt int getUserHandle() {
Dianne Hackborn79af1dd2012-08-16 16:42:52 -07001349 return UserHandle.myUserId();
Amith Yamasani258848d2012-08-10 17:06:33 -07001350 }
1351
1352 /**
Dianne Hackborn8832c182012-09-17 17:20:24 -07001353 * Returns the user name of the user making this call. This call is only
1354 * available to applications on the system image; it requires the
Bookatzcde3d922019-03-08 14:30:00 -08001355 * {@code android.permission.MANAGE_USERS} or {@code android.permission.GET_ACCOUNTS_PRIVILEGED}
1356 * permissions.
Amith Yamasani258848d2012-08-10 17:06:33 -07001357 * @return the user name
1358 */
1359 public String getUserName() {
Bookatzcde3d922019-03-08 14:30:00 -08001360 try {
1361 return mService.getUserName();
1362 } catch (RemoteException re) {
1363 throw re.rethrowFromSystemServer();
1364 }
Amith Yamasani258848d2012-08-10 17:06:33 -07001365 }
1366
Dianne Hackborn67a101a2014-10-02 12:42:18 -07001367 /**
Fyodor Kupolov605b12a2017-05-10 15:58:09 -07001368 * Returns whether user name has been set.
1369 * <p>This method can be used to check that the value returned by {@link #getUserName()} was
1370 * set by the user and is not a placeholder string provided by the system.
1371 * @hide
1372 */
1373 public boolean isUserNameSet() {
1374 try {
1375 return mService.isUserNameSet(getUserHandle());
1376 } catch (RemoteException re) {
1377 throw re.rethrowFromSystemServer();
1378 }
1379 }
1380
1381 /**
Dan Morrille4ab16a2012-09-20 20:25:55 -07001382 * Used to determine whether the user making this call is subject to
1383 * teleportations.
Dianne Hackborn67a101a2014-10-02 12:42:18 -07001384 *
Dianne Hackborn955d8d62014-10-07 20:17:19 -07001385 * <p>As of {@link android.os.Build.VERSION_CODES#LOLLIPOP}, this method can
Dianne Hackborn67a101a2014-10-02 12:42:18 -07001386 * now automatically identify goats using advanced goat recognition technology.</p>
1387 *
1388 * @return Returns true if the user making this call is a goat.
Dan Morrille4ab16a2012-09-20 20:25:55 -07001389 */
1390 public boolean isUserAGoat() {
Adam Powell988ae302014-09-17 17:58:33 -07001391 return mContext.getPackageManager()
1392 .isPackageAvailable("com.coffeestainstudios.goatsimulator");
Dan Morrille4ab16a2012-09-20 20:25:55 -07001393 }
Amith Yamasanidf2e92a2013-03-01 17:04:38 -08001394
1395 /**
Xiaohui Chen70f6c382015-04-28 14:21:43 -07001396 * Used to check if this process is running under the primary user. The primary user
1397 * is the first human user on a device.
1398 *
1399 * @return whether this process is running under the primary user.
1400 * @hide
1401 */
Varun Shahe142e332018-10-22 15:52:56 -07001402 @SystemApi
1403 @RequiresPermission(android.Manifest.permission.MANAGE_USERS)
Xiaohui Chen70f6c382015-04-28 14:21:43 -07001404 public boolean isPrimaryUser() {
1405 UserInfo user = getUserInfo(UserHandle.myUserId());
Amith Yamasanid35a89c2016-05-26 16:58:43 -07001406 return user != null && user.isPrimary();
Xiaohui Chen70f6c382015-04-28 14:21:43 -07001407 }
1408
1409 /**
Amith Yamasani5760e172015-04-17 18:42:41 -07001410 * Used to check if this process is running under the system user. The system user
1411 * is the initial user that is implicitly created on first boot and hosts most of the
1412 * system services.
1413 *
1414 * @return whether this process is running under the system user.
1415 */
1416 public boolean isSystemUser() {
Xiaohui Chen70f6c382015-04-28 14:21:43 -07001417 return UserHandle.myUserId() == UserHandle.USER_SYSTEM;
Amith Yamasani5760e172015-04-17 18:42:41 -07001418 }
Xiaohui Chen2b45f8a2015-08-04 15:12:23 -07001419
Amith Yamasani5760e172015-04-17 18:42:41 -07001420 /**
Varun Shahe142e332018-10-22 15:52:56 -07001421 * Used to check if this process is running as an admin user. An admin user is allowed to
1422 * modify or configure certain settings that aren't available to non-admin users,
1423 * create and delete additional users, etc. There can be more than one admin users.
1424 *
1425 * @return whether this process is running under an admin user.
Amith Yamasani462ac3a2015-06-30 14:21:01 -07001426 * @hide
Amith Yamasani462ac3a2015-06-30 14:21:01 -07001427 */
Varun Shahe142e332018-10-22 15:52:56 -07001428 @SystemApi
1429 @RequiresPermission(android.Manifest.permission.MANAGE_USERS)
Amith Yamasani462ac3a2015-06-30 14:21:01 -07001430 public boolean isAdminUser() {
Xiaohui Chen2b45f8a2015-08-04 15:12:23 -07001431 return isUserAdmin(UserHandle.myUserId());
1432 }
1433
1434 /**
1435 * @hide
1436 * Returns whether the provided user is an admin user. There can be more than one admin
1437 * user.
1438 */
Andrei Onea24ec3212019-03-15 17:35:05 +00001439 @UnsupportedAppUsage
Jeff Sharkey8588bc12016-01-06 16:47:42 -07001440 public boolean isUserAdmin(@UserIdInt int userId) {
Xiaohui Chen2b45f8a2015-08-04 15:12:23 -07001441 UserInfo user = getUserInfo(userId);
1442 return user != null && user.isAdmin();
Amith Yamasani462ac3a2015-06-30 14:21:01 -07001443 }
1444
1445 /**
Fyodor Kupolovca177562017-11-09 17:43:01 -08001446 * @hide
1447 * @deprecated Use {@link #isRestrictedProfile()}
1448 */
Andrei Onea24ec3212019-03-15 17:35:05 +00001449 @UnsupportedAppUsage
Fyodor Kupolovca177562017-11-09 17:43:01 -08001450 @Deprecated
1451 public boolean isLinkedUser() {
1452 return isRestrictedProfile();
1453 }
1454
1455 /**
Varun Shahe142e332018-10-22 15:52:56 -07001456 * Used to check if this process is running under a restricted profile. Restricted profiles
1457 * may have a reduced number of available apps, app restrictions, and account restrictions.
1458 *
1459 * @return whether this process is running under a restricted profile.
Amith Yamasani2555daf2013-04-25 13:39:27 -07001460 * @hide
Amith Yamasanidf2e92a2013-03-01 17:04:38 -08001461 */
Fyodor Kupolovca177562017-11-09 17:43:01 -08001462 @SystemApi
Varun Shahe142e332018-10-22 15:52:56 -07001463 @RequiresPermission(android.Manifest.permission.MANAGE_USERS)
Fyodor Kupolovca177562017-11-09 17:43:01 -08001464 public boolean isRestrictedProfile() {
Amith Yamasanidf2e92a2013-03-01 17:04:38 -08001465 try {
Amith Yamasani71e6c692013-03-24 17:39:28 -07001466 return mService.isRestricted();
Amith Yamasanidf2e92a2013-03-01 17:04:38 -08001467 } catch (RemoteException re) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001468 throw re.rethrowFromSystemServer();
Amith Yamasanidf2e92a2013-03-01 17:04:38 -08001469 }
1470 }
1471
Amith Yamasani258848d2012-08-10 17:06:33 -07001472 /**
Hai Zhang7c064ca2019-03-18 18:21:38 -07001473 * Check if a user is a restricted profile. Restricted profiles may have a reduced number of
1474 * available apps, app restrictions, and account restrictions.
1475 *
1476 * @param user the user to check
1477 * @return whether the user is a restricted profile.
1478 * @hide
1479 */
1480 @SystemApi
1481 @RequiresPermission(android.Manifest.permission.MANAGE_USERS)
1482 public boolean isRestrictedProfile(@NonNull UserHandle user) {
1483 try {
1484 return mService.getUserInfo(user.getIdentifier()).isRestricted();
1485 } catch (RemoteException re) {
1486 throw re.rethrowFromSystemServer();
1487 }
1488 }
1489
1490 /**
Fyodor Kupolov1c363152015-09-02 13:27:21 -07001491 * Checks if specified user can have restricted profile.
1492 * @hide
1493 */
Jeff Sharkey8588bc12016-01-06 16:47:42 -07001494 public boolean canHaveRestrictedProfile(@UserIdInt int userId) {
Fyodor Kupolov1c363152015-09-02 13:27:21 -07001495 try {
1496 return mService.canHaveRestrictedProfile(userId);
1497 } catch (RemoteException re) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001498 throw re.rethrowFromSystemServer();
Fyodor Kupolov1c363152015-09-02 13:27:21 -07001499 }
1500 }
1501
1502 /**
Fyodor Kupolovca177562017-11-09 17:43:01 -08001503 * Returns whether the calling user has at least one restricted profile associated with it.
1504 * @return
1505 * @hide
1506 */
1507 @SystemApi
1508 public boolean hasRestrictedProfiles() {
1509 try {
1510 return mService.hasRestrictedProfiles();
1511 } catch (RemoteException re) {
1512 throw re.rethrowFromSystemServer();
1513 }
1514 }
1515
1516 /**
Evan Rosky13a58a92016-07-27 15:51:09 -07001517 * Checks if a user is a guest user.
1518 * @return whether user is a guest user.
1519 * @hide
1520 */
Andrei Onea24ec3212019-03-15 17:35:05 +00001521 @UnsupportedAppUsage
Evan Rosky13a58a92016-07-27 15:51:09 -07001522 public boolean isGuestUser(int id) {
1523 UserInfo user = getUserInfo(id);
1524 return user != null && user.isGuest();
1525 }
1526
1527 /**
Varun Shahe142e332018-10-22 15:52:56 -07001528 * Used to check if this process is running under a guest user. A guest user may be transient.
1529 *
1530 * @return whether this process is running under a guest user.
Amith Yamasani1e9c2182014-06-11 17:25:51 -07001531 * @hide
1532 */
Varun Shahe142e332018-10-22 15:52:56 -07001533 @SystemApi
1534 @RequiresPermission(android.Manifest.permission.MANAGE_USERS)
Amith Yamasani1e9c2182014-06-11 17:25:51 -07001535 public boolean isGuestUser() {
1536 UserInfo user = getUserInfo(UserHandle.myUserId());
Amith Yamasanid35a89c2016-05-26 16:58:43 -07001537 return user != null && user.isGuest();
1538 }
1539
Fyodor Kupolovca177562017-11-09 17:43:01 -08001540
Amith Yamasanid35a89c2016-05-26 16:58:43 -07001541 /**
Amith Yamasani1c41dc82016-06-28 16:13:15 -07001542 * Checks if the calling app is running in a demo user. When running in a demo user,
1543 * apps can be more helpful to the user, or explain their features in more detail.
1544 *
Amith Yamasanid35a89c2016-05-26 16:58:43 -07001545 * @return whether the caller is a demo user.
Amith Yamasanid35a89c2016-05-26 16:58:43 -07001546 */
1547 public boolean isDemoUser() {
Amith Yamasani1c41dc82016-06-28 16:13:15 -07001548 try {
1549 return mService.isDemoUser(UserHandle.myUserId());
1550 } catch (RemoteException re) {
1551 throw re.rethrowFromSystemServer();
1552 }
Amith Yamasani1e9c2182014-06-11 17:25:51 -07001553 }
1554
1555 /**
Amith Yamasani0e8d7d62014-09-03 13:17:28 -07001556 * Checks if the calling app is running in a managed profile.
Amith Yamasani0e8d7d62014-09-03 13:17:28 -07001557 *
1558 * @return whether the caller is in a managed profile.
1559 * @hide
1560 */
1561 @SystemApi
Jeff Sharkeyd86b8fe2017-06-02 17:36:26 -06001562 @RequiresPermission(android.Manifest.permission.MANAGE_USERS)
Amith Yamasani0e8d7d62014-09-03 13:17:28 -07001563 public boolean isManagedProfile() {
Makoto Onukid49f3fa2017-01-25 14:09:48 -08001564 // No need for synchronization. Once it becomes non-null, it'll be non-null forever.
1565 // Worst case we might end up calling the AIDL method multiple times but that's fine.
1566 if (mIsManagedProfileCached != null) {
1567 return mIsManagedProfileCached;
1568 }
Tony Mak8673b282016-03-21 21:10:59 +00001569 try {
Makoto Onukid49f3fa2017-01-25 14:09:48 -08001570 mIsManagedProfileCached = mService.isManagedProfile(UserHandle.myUserId());
1571 return mIsManagedProfileCached;
Tony Mak8673b282016-03-21 21:10:59 +00001572 } catch (RemoteException re) {
1573 throw re.rethrowFromSystemServer();
1574 }
1575 }
1576
1577 /**
1578 * Checks if the specified user is a managed profile.
1579 * Requires {@link android.Manifest.permission#MANAGE_USERS} permission, otherwise the caller
1580 * must be in the same profile group of specified user.
1581 *
1582 * @return whether the specified user is a managed profile.
1583 * @hide
1584 */
1585 @SystemApi
Jeff Sharkeyd86b8fe2017-06-02 17:36:26 -06001586 @RequiresPermission(android.Manifest.permission.MANAGE_USERS)
Tony Mak8673b282016-03-21 21:10:59 +00001587 public boolean isManagedProfile(@UserIdInt int userId) {
Makoto Onukid49f3fa2017-01-25 14:09:48 -08001588 if (userId == UserHandle.myUserId()) {
1589 return isManagedProfile();
1590 }
Tony Mak8673b282016-03-21 21:10:59 +00001591 try {
1592 return mService.isManagedProfile(userId);
1593 } catch (RemoteException re) {
1594 throw re.rethrowFromSystemServer();
1595 }
Amith Yamasani0e8d7d62014-09-03 13:17:28 -07001596 }
1597
1598 /**
Kenny Guy02c89902016-11-15 19:36:38 +00001599 * Gets badge for a managed profile.
1600 * Requires {@link android.Manifest.permission#MANAGE_USERS} permission, otherwise the caller
1601 * must be in the same profile group of specified user.
1602 *
1603 * @return which badge to use for the managed profile badge id will be less than
1604 * UserManagerService.getMaxManagedProfiles()
1605 * @hide
1606 */
1607 public int getManagedProfileBadge(@UserIdInt int userId) {
1608 try {
1609 return mService.getManagedProfileBadge(userId);
1610 } catch (RemoteException re) {
1611 throw re.rethrowFromSystemServer();
1612 }
1613 }
1614
1615 /**
Lenka Trochtovac4dd0212015-11-18 12:22:06 +01001616 * Checks if the calling app is running as an ephemeral user.
1617 *
1618 * @return whether the caller is an ephemeral user.
1619 * @hide
1620 */
1621 public boolean isEphemeralUser() {
1622 return isUserEphemeral(UserHandle.myUserId());
1623 }
1624
1625 /**
1626 * Returns whether the specified user is ephemeral.
1627 * @hide
1628 */
Jeff Sharkey8588bc12016-01-06 16:47:42 -07001629 public boolean isUserEphemeral(@UserIdInt int userId) {
Lenka Trochtovac4dd0212015-11-18 12:22:06 +01001630 final UserInfo user = getUserInfo(userId);
1631 return user != null && user.isEphemeral();
1632 }
1633
1634 /**
Dianne Hackborna8a9bd62012-10-09 15:36:59 -07001635 * Return whether the given user is actively running. This means that
1636 * the user is in the "started" state, not "stopped" -- it is currently
1637 * allowed to run code through scheduled alarms, receiving broadcasts,
1638 * etc. A started user may be either the current foreground user or a
1639 * background user; the result here does not distinguish between the two.
Makoto Onuki5eef50d2017-03-02 16:38:45 -08001640 *
1641 * <p>Note prior to Android Nougat MR1 (SDK version <= 24;
Andrew Solovay8a788f42018-12-12 14:25:47 -08001642 * {@link android.os.Build.VERSION_CODES#N}, this API required a system permission
Makoto Onuki5eef50d2017-03-02 16:38:45 -08001643 * in order to check other profile's status.
1644 * Since Android Nougat MR1 (SDK version >= 25;
Andrew Solovay8a788f42018-12-12 14:25:47 -08001645 * {@link android.os.Build.VERSION_CODES#N_MR1}), the restriction has been relaxed, and now
1646 * it'll accept any {@link android.os.UserHandle} within the same profile group as the caller.
Fyodor Kupolovcdb3c2f2017-02-10 11:48:32 -08001647 *
Dianne Hackborna8a9bd62012-10-09 15:36:59 -07001648 * @param user The user to retrieve the running state for.
1649 */
Makoto Onuki5eef50d2017-03-02 16:38:45 -08001650 // Note this requires either INTERACT_ACROSS_USERS or MANAGE_USERS.
Dianne Hackborna8a9bd62012-10-09 15:36:59 -07001651 public boolean isUserRunning(UserHandle user) {
Jeff Sharkey0e62384c2016-01-13 18:52:55 -07001652 return isUserRunning(user.getIdentifier());
1653 }
1654
1655 /** {@hide} */
Fyodor Kupolov2e7e0962016-12-01 18:09:17 -08001656 public boolean isUserRunning(@UserIdInt int userId) {
Dianne Hackborna8a9bd62012-10-09 15:36:59 -07001657 try {
Fyodor Kupolov2e7e0962016-12-01 18:09:17 -08001658 return mService.isUserRunning(userId);
Jeff Sharkey27b2e692016-02-25 17:40:12 -07001659 } catch (RemoteException re) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001660 throw re.rethrowFromSystemServer();
Dianne Hackborna8a9bd62012-10-09 15:36:59 -07001661 }
1662 }
1663
1664 /**
1665 * Return whether the given user is actively running <em>or</em> stopping.
1666 * This is like {@link #isUserRunning(UserHandle)}, but will also return
1667 * true if the user had been running but is in the process of being stopped
1668 * (but is not yet fully stopped, and still running some code).
Makoto Onuki5eef50d2017-03-02 16:38:45 -08001669 *
1670 * <p>Note prior to Android Nougat MR1 (SDK version <= 24;
Andrew Solovay8a788f42018-12-12 14:25:47 -08001671 * {@link android.os.Build.VERSION_CODES#N}, this API required a system permission
Makoto Onuki5eef50d2017-03-02 16:38:45 -08001672 * in order to check other profile's status.
1673 * Since Android Nougat MR1 (SDK version >= 25;
Andrew Solovay8a788f42018-12-12 14:25:47 -08001674 * {@link android.os.Build.VERSION_CODES#N_MR1}), the restriction has been relaxed, and now
1675 * it'll accept any {@link android.os.UserHandle} within the same profile group as the caller.
Fyodor Kupolovcdb3c2f2017-02-10 11:48:32 -08001676 *
Dianne Hackborna8a9bd62012-10-09 15:36:59 -07001677 * @param user The user to retrieve the running state for.
1678 */
Makoto Onuki5eef50d2017-03-02 16:38:45 -08001679 // Note this requires either INTERACT_ACROSS_USERS or MANAGE_USERS.
Dianne Hackborna8a9bd62012-10-09 15:36:59 -07001680 public boolean isUserRunningOrStopping(UserHandle user) {
1681 try {
Jeff Sharkeye17ac152015-11-06 22:40:29 -08001682 // TODO: reconcile stopped vs stopping?
Sudheer Shankadc589ac2016-11-10 15:30:17 -08001683 return ActivityManager.getService().isUserRunning(
Jeff Sharkeye17ac152015-11-06 22:40:29 -08001684 user.getIdentifier(), ActivityManager.FLAG_OR_STOPPED);
Jeff Sharkey27b2e692016-02-25 17:40:12 -07001685 } catch (RemoteException re) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001686 throw re.rethrowFromSystemServer();
Dianne Hackborna8a9bd62012-10-09 15:36:59 -07001687 }
1688 }
1689
Jeff Sharkey0825ab22015-12-02 13:04:49 -07001690 /**
Jeff Sharkeycf3f0a12016-03-17 19:57:58 -06001691 * Return whether the calling user is running in an "unlocked" state.
1692 * <p>
1693 * On devices with direct boot, a user is unlocked only after they've
1694 * entered their credentials (such as a lock pattern or PIN). On devices
1695 * without direct boot, a user is unlocked as soon as it starts.
1696 * <p>
1697 * When a user is locked, only device-protected data storage is available.
1698 * When a user is unlocked, both device-protected and credential-protected
1699 * private app data storage is available.
Jeff Sharkey8a372a02016-03-16 16:25:45 -06001700 *
1701 * @see Intent#ACTION_USER_UNLOCKED
1702 * @see Context#createDeviceProtectedStorageContext()
Jeff Sharkeyb6423872015-12-11 11:11:58 -07001703 */
1704 public boolean isUserUnlocked() {
1705 return isUserUnlocked(Process.myUserHandle());
1706 }
1707
1708 /**
Jeff Sharkeycf3f0a12016-03-17 19:57:58 -06001709 * Return whether the given user is running in an "unlocked" state.
1710 * <p>
1711 * On devices with direct boot, a user is unlocked only after they've
1712 * entered their credentials (such as a lock pattern or PIN). On devices
1713 * without direct boot, a user is unlocked as soon as it starts.
1714 * <p>
1715 * When a user is locked, only device-protected data storage is available.
1716 * When a user is unlocked, both device-protected and credential-protected
1717 * private app data storage is available.
Fyodor Kupolovcdb3c2f2017-02-10 11:48:32 -08001718 * <p>Requires {@code android.permission.MANAGE_USERS} or
1719 * {@code android.permission.INTERACT_ACROSS_USERS}, otherwise specified {@link UserHandle user}
1720 * must be the calling user or a managed profile associated with it.
Jeff Sharkeyb6423872015-12-11 11:11:58 -07001721 *
1722 * @param user to retrieve the unlocked state for.
Jeff Sharkey8a372a02016-03-16 16:25:45 -06001723 * @see Intent#ACTION_USER_UNLOCKED
1724 * @see Context#createDeviceProtectedStorageContext()
Jeff Sharkeyb6423872015-12-11 11:11:58 -07001725 */
1726 public boolean isUserUnlocked(UserHandle user) {
Jeff Sharkey0999c0d2015-12-17 15:12:22 -07001727 return isUserUnlocked(user.getIdentifier());
1728 }
1729
1730 /** {@hide} */
Andrei Onea24ec3212019-03-15 17:35:05 +00001731 @UnsupportedAppUsage
Jeff Sharkey8588bc12016-01-06 16:47:42 -07001732 public boolean isUserUnlocked(@UserIdInt int userId) {
Jeff Sharkeyce18c812016-04-27 16:00:41 -06001733 try {
Fyodor Kupolov2e7e0962016-12-01 18:09:17 -08001734 return mService.isUserUnlocked(userId);
Jeff Sharkeyce18c812016-04-27 16:00:41 -06001735 } catch (RemoteException re) {
1736 throw re.rethrowFromSystemServer();
1737 }
1738 }
1739
1740 /** {@hide} */
1741 public boolean isUserUnlockingOrUnlocked(UserHandle user) {
1742 return isUserUnlockingOrUnlocked(user.getIdentifier());
1743 }
1744
1745 /** {@hide} */
1746 public boolean isUserUnlockingOrUnlocked(@UserIdInt int userId) {
1747 try {
Fyodor Kupolovc413f702016-10-06 17:11:14 -07001748 return mService.isUserUnlockingOrUnlocked(userId);
Jeff Sharkeyce18c812016-04-27 16:00:41 -06001749 } catch (RemoteException re) {
1750 throw re.rethrowFromSystemServer();
1751 }
Jeff Sharkeyb6423872015-12-11 11:11:58 -07001752 }
1753
1754 /**
Makoto Onuki73dded22017-12-20 13:14:48 +09001755 * Return the time when the calling user started in elapsed milliseconds since boot,
1756 * or 0 if not started.
1757 *
1758 * @hide
1759 */
Andrei Onea24ec3212019-03-15 17:35:05 +00001760 @UnsupportedAppUsage
Makoto Onuki73dded22017-12-20 13:14:48 +09001761 public long getUserStartRealtime() {
1762 try {
1763 return mService.getUserStartRealtime();
1764 } catch (RemoteException re) {
1765 throw re.rethrowFromSystemServer();
1766 }
1767 }
1768
1769 /**
1770 * Return the time when the calling user was unlocked elapsed milliseconds since boot,
1771 * or 0 if not unlocked.
1772 *
1773 * @hide
1774 */
Andrei Onea24ec3212019-03-15 17:35:05 +00001775 @UnsupportedAppUsage
Makoto Onuki73dded22017-12-20 13:14:48 +09001776 public long getUserUnlockRealtime() {
1777 try {
1778 return mService.getUserUnlockRealtime();
1779 } catch (RemoteException re) {
1780 throw re.rethrowFromSystemServer();
1781 }
1782 }
1783
1784 /**
Amith Yamasani258848d2012-08-10 17:06:33 -07001785 * Returns the UserInfo object describing a specific user.
Tony Mak8673b282016-03-21 21:10:59 +00001786 * Requires {@link android.Manifest.permission#MANAGE_USERS} permission.
Amith Yamasani258848d2012-08-10 17:06:33 -07001787 * @param userHandle the user handle of the user whose information is being requested.
1788 * @return the UserInfo object for a specific user.
1789 * @hide
Dianne Hackbornb26306a2012-10-24 15:22:21 -07001790 */
Andrei Onea24ec3212019-03-15 17:35:05 +00001791 @UnsupportedAppUsage
Jeff Sharkey8588bc12016-01-06 16:47:42 -07001792 public UserInfo getUserInfo(@UserIdInt int userHandle) {
Amith Yamasani258848d2012-08-10 17:06:33 -07001793 try {
1794 return mService.getUserInfo(userHandle);
1795 } catch (RemoteException re) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001796 throw re.rethrowFromSystemServer();
Amith Yamasani258848d2012-08-10 17:06:33 -07001797 }
1798 }
1799
Amith Yamasani71e6c692013-03-24 17:39:28 -07001800 /**
Zoltan Szatmary-Bane7834602016-04-08 18:41:11 +01001801 * @hide
1802 *
1803 * Returns who set a user restriction on a user.
Zoltan Szatmary-Bane7834602016-04-08 18:41:11 +01001804 * @param restrictionKey the string key representing the restriction
1805 * @param userHandle the UserHandle of the user for whom to retrieve the restrictions.
1806 * @return The source of user restriction. Any combination of {@link #RESTRICTION_NOT_SET},
1807 * {@link #RESTRICTION_SOURCE_SYSTEM}, {@link #RESTRICTION_SOURCE_DEVICE_OWNER}
1808 * and {@link #RESTRICTION_SOURCE_PROFILE_OWNER}
Pavel Grafov6a40f092016-10-25 15:46:51 +01001809 * @deprecated use {@link #getUserRestrictionSources(String, int)} instead.
Zoltan Szatmary-Bane7834602016-04-08 18:41:11 +01001810 */
Pavel Grafov6a40f092016-10-25 15:46:51 +01001811 @Deprecated
Zoltan Szatmary-Bane7834602016-04-08 18:41:11 +01001812 @SystemApi
1813 @UserRestrictionSource
Jeff Sharkeyd86b8fe2017-06-02 17:36:26 -06001814 @RequiresPermission(android.Manifest.permission.MANAGE_USERS)
Zoltan Szatmary-Bane7834602016-04-08 18:41:11 +01001815 public int getUserRestrictionSource(String restrictionKey, UserHandle userHandle) {
1816 try {
1817 return mService.getUserRestrictionSource(restrictionKey, userHandle.getIdentifier());
1818 } catch (RemoteException re) {
1819 throw re.rethrowFromSystemServer();
1820 }
1821 }
1822
1823 /**
Pavel Grafov6a40f092016-10-25 15:46:51 +01001824 * @hide
1825 *
1826 * Returns a list of users who set a user restriction on a given user.
Pavel Grafov6a40f092016-10-25 15:46:51 +01001827 * @param restrictionKey the string key representing the restriction
1828 * @param userHandle the UserHandle of the user for whom to retrieve the restrictions.
1829 * @return a list of user ids enforcing this restriction.
1830 */
1831 @SystemApi
Jeff Sharkeyd86b8fe2017-06-02 17:36:26 -06001832 @RequiresPermission(android.Manifest.permission.MANAGE_USERS)
Pavel Grafov6a40f092016-10-25 15:46:51 +01001833 public List<EnforcingUser> getUserRestrictionSources(
1834 String restrictionKey, UserHandle userHandle) {
1835 try {
1836 return mService.getUserRestrictionSources(restrictionKey, userHandle.getIdentifier());
1837 } catch (RemoteException re) {
1838 throw re.rethrowFromSystemServer();
1839 }
1840 }
1841
1842 /**
Amith Yamasani71e6c692013-03-24 17:39:28 -07001843 * Returns the user-wide restrictions imposed on this user.
1844 * @return a Bundle containing all the restrictions.
1845 */
Amith Yamasanie4cf7342012-12-17 11:12:09 -08001846 public Bundle getUserRestrictions() {
1847 return getUserRestrictions(Process.myUserHandle());
1848 }
1849
Amith Yamasani71e6c692013-03-24 17:39:28 -07001850 /**
1851 * Returns the user-wide restrictions imposed on the user specified by <code>userHandle</code>.
1852 * @param userHandle the UserHandle of the user for whom to retrieve the restrictions.
1853 * @return a Bundle containing all the restrictions.
1854 */
Amith Yamasanie4cf7342012-12-17 11:12:09 -08001855 public Bundle getUserRestrictions(UserHandle userHandle) {
1856 try {
1857 return mService.getUserRestrictions(userHandle.getIdentifier());
1858 } catch (RemoteException re) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001859 throw re.rethrowFromSystemServer();
Amith Yamasanie4cf7342012-12-17 11:12:09 -08001860 }
1861 }
1862
Zoltan Szatmary-Ban3bbcedd2015-11-26 13:45:51 +00001863 /**
1864 * @hide
1865 * Returns whether the given user has been disallowed from performing certain actions
Irina Dumitrescu4638edd2018-09-05 14:08:33 +01001866 * or setting certain settings through UserManager (e.g. this type of restriction would prevent
1867 * the guest user from doing certain things, such as making calls). This method disregards
1868 * restrictions set by device policy.
Zoltan Szatmary-Ban3bbcedd2015-11-26 13:45:51 +00001869 * @param restrictionKey the string key representing the restriction
1870 * @param userHandle the UserHandle of the user for whom to retrieve the restrictions.
1871 */
Andrei Onea24ec3212019-03-15 17:35:05 +00001872 @UnsupportedAppUsage
Zoltan Szatmary-Ban3bbcedd2015-11-26 13:45:51 +00001873 public boolean hasBaseUserRestriction(String restrictionKey, UserHandle userHandle) {
1874 try {
1875 return mService.hasBaseUserRestriction(restrictionKey, userHandle.getIdentifier());
1876 } catch (RemoteException re) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001877 throw re.rethrowFromSystemServer();
Zoltan Szatmary-Ban3bbcedd2015-11-26 13:45:51 +00001878 }
1879 }
1880
Amith Yamasani71e6c692013-03-24 17:39:28 -07001881 /**
Makoto Onukia3c12502015-10-28 10:18:32 -07001882 * This will no longer work. Device owners and profile owners should use
1883 * {@link DevicePolicyManager#addUserRestriction(ComponentName, String)} instead.
Amith Yamasani71e6c692013-03-24 17:39:28 -07001884 */
Makoto Onukia3c12502015-10-28 10:18:32 -07001885 // System apps should use UserManager.setUserRestriction() instead.
Julia Reynolds3d9eb782014-08-11 16:40:08 -04001886 @Deprecated
Amith Yamasanie4cf7342012-12-17 11:12:09 -08001887 public void setUserRestrictions(Bundle restrictions) {
Makoto Onuki068c54a2015-10-13 14:34:03 -07001888 throw new UnsupportedOperationException("This method is no longer supported");
Amith Yamasanie4cf7342012-12-17 11:12:09 -08001889 }
1890
Amith Yamasani71e6c692013-03-24 17:39:28 -07001891 /**
Makoto Onukia3c12502015-10-28 10:18:32 -07001892 * This will no longer work. Device owners and profile owners should use
1893 * {@link DevicePolicyManager#addUserRestriction(ComponentName, String)} instead.
Amith Yamasani71e6c692013-03-24 17:39:28 -07001894 */
Makoto Onukia3c12502015-10-28 10:18:32 -07001895 // System apps should use UserManager.setUserRestriction() instead.
Julia Reynolds3d9eb782014-08-11 16:40:08 -04001896 @Deprecated
Amith Yamasanie4cf7342012-12-17 11:12:09 -08001897 public void setUserRestrictions(Bundle restrictions, UserHandle userHandle) {
Makoto Onuki068c54a2015-10-13 14:34:03 -07001898 throw new UnsupportedOperationException("This method is no longer supported");
Amith Yamasanie4cf7342012-12-17 11:12:09 -08001899 }
1900
Amith Yamasani71e6c692013-03-24 17:39:28 -07001901 /**
1902 * Sets the value of a specific restriction.
Amith Yamasanibe465322014-04-24 13:45:17 -07001903 * Requires the MANAGE_USERS permission.
Amith Yamasani71e6c692013-03-24 17:39:28 -07001904 * @param key the key of the restriction
1905 * @param value the value for the restriction
Julia Reynolds3d9eb782014-08-11 16:40:08 -04001906 * @deprecated use {@link android.app.admin.DevicePolicyManager#addUserRestriction(
1907 * android.content.ComponentName, String)} or
1908 * {@link android.app.admin.DevicePolicyManager#clearUserRestriction(
1909 * android.content.ComponentName, String)} instead.
Amith Yamasani71e6c692013-03-24 17:39:28 -07001910 */
Julia Reynolds3d9eb782014-08-11 16:40:08 -04001911 @Deprecated
Amith Yamasani71e6c692013-03-24 17:39:28 -07001912 public void setUserRestriction(String key, boolean value) {
Makoto Onuki068c54a2015-10-13 14:34:03 -07001913 setUserRestriction(key, value, Process.myUserHandle());
Amith Yamasani71e6c692013-03-24 17:39:28 -07001914 }
1915
1916 /**
1917 * @hide
1918 * Sets the value of a specific restriction on a specific user.
Amith Yamasanibe465322014-04-24 13:45:17 -07001919 * Requires the MANAGE_USERS permission.
Amith Yamasani71e6c692013-03-24 17:39:28 -07001920 * @param key the key of the restriction
1921 * @param value the value for the restriction
1922 * @param userHandle the user whose restriction is to be changed.
Julia Reynolds3d9eb782014-08-11 16:40:08 -04001923 * @deprecated use {@link android.app.admin.DevicePolicyManager#addUserRestriction(
1924 * android.content.ComponentName, String)} or
1925 * {@link android.app.admin.DevicePolicyManager#clearUserRestriction(
1926 * android.content.ComponentName, String)} instead.
Amith Yamasani71e6c692013-03-24 17:39:28 -07001927 */
Julia Reynolds3d9eb782014-08-11 16:40:08 -04001928 @Deprecated
Maggie Benthall67944582013-02-22 14:58:27 -05001929 public void setUserRestriction(String key, boolean value, UserHandle userHandle) {
Fyodor Kupolovb5013302015-04-17 17:59:14 -07001930 try {
1931 mService.setUserRestriction(key, value, userHandle.getIdentifier());
1932 } catch (RemoteException re) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001933 throw re.rethrowFromSystemServer();
Fyodor Kupolovb5013302015-04-17 17:59:14 -07001934 }
Maggie Benthall67944582013-02-22 14:58:27 -05001935 }
1936
Amith Yamasani258848d2012-08-10 17:06:33 -07001937 /**
Maggie Benthalla12fccf2013-03-14 18:02:12 -04001938 * Returns whether the current user has been disallowed from performing certain actions
1939 * or setting certain settings.
Julia Reynolds2b2cf722014-06-06 11:41:04 -04001940 *
1941 * @param restrictionKey The string key representing the restriction.
1942 * @return {@code true} if the current user has the given restriction, {@code false} otherwise.
Maggie Benthalla12fccf2013-03-14 18:02:12 -04001943 */
1944 public boolean hasUserRestriction(String restrictionKey) {
David Christieb12ba932013-09-03 17:15:28 -07001945 return hasUserRestriction(restrictionKey, Process.myUserHandle());
1946 }
1947
1948 /**
1949 * @hide
1950 * Returns whether the given user has been disallowed from performing certain actions
1951 * or setting certain settings.
1952 * @param restrictionKey the string key representing the restriction
1953 * @param userHandle the UserHandle of the user for whom to retrieve the restrictions.
1954 */
Andrei Onea24ec3212019-03-15 17:35:05 +00001955 @UnsupportedAppUsage
David Christieb12ba932013-09-03 17:15:28 -07001956 public boolean hasUserRestriction(String restrictionKey, UserHandle userHandle) {
Amith Yamasani8cd28b52014-06-08 17:54:27 -07001957 try {
1958 return mService.hasUserRestriction(restrictionKey,
1959 userHandle.getIdentifier());
1960 } catch (RemoteException re) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001961 throw re.rethrowFromSystemServer();
Amith Yamasani8cd28b52014-06-08 17:54:27 -07001962 }
Maggie Benthalla12fccf2013-03-14 18:02:12 -04001963 }
1964
1965 /**
Makoto Onukiacc50462018-02-14 14:13:49 -08001966 * @hide
1967 * Returns whether any user on the device has the given user restriction set.
1968 */
1969 public boolean hasUserRestrictionOnAnyUser(String restrictionKey) {
1970 try {
1971 return mService.hasUserRestrictionOnAnyUser(restrictionKey);
1972 } catch (RemoteException re) {
1973 throw re.rethrowFromSystemServer();
1974 }
1975 }
1976
1977 /**
Dianne Hackborn33f9cb82012-10-04 17:15:10 -07001978 * Return the serial number for a user. This is a device-unique
Dianne Hackbornb26306a2012-10-24 15:22:21 -07001979 * number assigned to that user; if the user is deleted and then a new
1980 * user created, the new users will not be given the same serial number.
Dianne Hackborn33f9cb82012-10-04 17:15:10 -07001981 * @param user The user whose serial number is to be retrieved.
Dianne Hackbornb26306a2012-10-24 15:22:21 -07001982 * @return The serial number of the given user; returns -1 if the
1983 * given UserHandle does not exist.
Dianne Hackborn33f9cb82012-10-04 17:15:10 -07001984 * @see #getUserForSerialNumber(long)
1985 */
1986 public long getSerialNumberForUser(UserHandle user) {
1987 return getUserSerialNumber(user.getIdentifier());
1988 }
1989
1990 /**
1991 * Return the user associated with a serial number previously
1992 * returned by {@link #getSerialNumberForUser(UserHandle)}.
1993 * @param serialNumber The serial number of the user that is being
1994 * retrieved.
1995 * @return Return the user associated with the serial number, or null
1996 * if there is not one.
1997 * @see #getSerialNumberForUser(UserHandle)
1998 */
1999 public UserHandle getUserForSerialNumber(long serialNumber) {
Fyodor Kupolovef249092015-05-06 13:18:46 -07002000 int ident = getUserHandle((int) serialNumber);
Dianne Hackborn33f9cb82012-10-04 17:15:10 -07002001 return ident >= 0 ? new UserHandle(ident) : null;
2002 }
2003
2004 /**
Xiaohui Chencfe64c82015-07-16 14:30:50 -07002005 * Creates a user with the specified name and options. For non-admin users, default user
2006 * restrictions are going to be applied.
Amith Yamasani195263742012-08-21 15:40:12 -07002007 * Requires {@link android.Manifest.permission#MANAGE_USERS} permission.
Amith Yamasani258848d2012-08-10 17:06:33 -07002008 *
2009 * @param name the user's name
2010 * @param flags flags that identify the type of user and other properties.
2011 * @see UserInfo
2012 *
2013 * @return the UserInfo object for the created user, or null if the user could not be created.
2014 * @hide
2015 */
Andrei Onea24ec3212019-03-15 17:35:05 +00002016 @UnsupportedAppUsage
Amith Yamasani258848d2012-08-10 17:06:33 -07002017 public UserInfo createUser(String name, int flags) {
Xiaohui Chencfe64c82015-07-16 14:30:50 -07002018 UserInfo user = null;
Amith Yamasani258848d2012-08-10 17:06:33 -07002019 try {
Xiaohui Chencfe64c82015-07-16 14:30:50 -07002020 user = mService.createUser(name, flags);
phweisse9c44062016-02-10 12:57:38 +01002021 // TODO: Keep this in sync with
2022 // UserManagerService.LocalService.createUserEvenWhenDisallowed
Christine Franks97a54802017-08-09 10:06:43 -07002023 if (user != null && !user.isAdmin() && !user.isDemo()) {
Makoto Onuki068c54a2015-10-13 14:34:03 -07002024 mService.setUserRestriction(DISALLOW_SMS, true, user.id);
2025 mService.setUserRestriction(DISALLOW_OUTGOING_CALLS, true, user.id);
Xiaohui Chencfe64c82015-07-16 14:30:50 -07002026 }
Amith Yamasani258848d2012-08-10 17:06:33 -07002027 } catch (RemoteException re) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07002028 throw re.rethrowFromSystemServer();
Amith Yamasani258848d2012-08-10 17:06:33 -07002029 }
Xiaohui Chencfe64c82015-07-16 14:30:50 -07002030 return user;
Amith Yamasani258848d2012-08-10 17:06:33 -07002031 }
2032
2033 /**
Amith Yamasani1e9c2182014-06-11 17:25:51 -07002034 * Creates a guest user and configures it.
2035 * @param context an application context
2036 * @param name the name to set for the user
2037 * @hide
2038 */
2039 public UserInfo createGuest(Context context, String name) {
Makoto Onuki068c54a2015-10-13 14:34:03 -07002040 UserInfo guest = null;
2041 try {
2042 guest = mService.createUser(name, UserInfo.FLAG_GUEST);
2043 if (guest != null) {
2044 Settings.Secure.putStringForUser(context.getContentResolver(),
2045 Settings.Secure.SKIP_FIRST_USE_HINTS, "1", guest.id);
Amith Yamasanibf3a9462014-07-28 14:26:42 -07002046 }
Makoto Onuki068c54a2015-10-13 14:34:03 -07002047 } catch (RemoteException re) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07002048 throw re.rethrowFromSystemServer();
Amith Yamasani1e9c2182014-06-11 17:25:51 -07002049 }
2050 return guest;
2051 }
2052
Amith Yamasaniaa6634e2014-10-06 14:20:28 -07002053 /**
Kenny Guy2a764942014-04-02 13:29:20 +01002054 * Creates a user with the specified name and options as a profile of another user.
Kenny Guya52dc3e2014-02-11 15:33:14 +00002055 * Requires {@link android.Manifest.permission#MANAGE_USERS} permission.
2056 *
2057 * @param name the user's name
2058 * @param flags flags that identify the type of user and other properties.
Sudheer Shanka7cb54a32016-09-16 12:59:05 -07002059 * @param userHandle new user will be a profile of this user.
Kenny Guya52dc3e2014-02-11 15:33:14 +00002060 *
Sudheer Shanka7cb54a32016-09-16 12:59:05 -07002061 * @return the {@link UserInfo} object for the created user, or null if the user
2062 * could not be created.
Kenny Guya52dc3e2014-02-11 15:33:14 +00002063 * @hide
2064 */
Andrei Onea24ec3212019-03-15 17:35:05 +00002065 @UnsupportedAppUsage
Jeff Sharkey8588bc12016-01-06 16:47:42 -07002066 public UserInfo createProfileForUser(String name, int flags, @UserIdInt int userHandle) {
Sudheer Shanka7cb54a32016-09-16 12:59:05 -07002067 return createProfileForUser(name, flags, userHandle, null);
2068 }
2069
2070 /**
2071 * Version of {@link #createProfileForUser(String, int, int)} that allows you to specify
2072 * any packages that should not be installed in the new profile by default, these packages can
2073 * still be installed later by the user if needed.
2074 *
2075 * @param name the user's name
2076 * @param flags flags that identify the type of user and other properties.
2077 * @param userHandle new user will be a profile of this user.
2078 * @param disallowedPackages packages that will not be installed in the profile being created.
2079 *
2080 * @return the {@link UserInfo} object for the created user, or null if the user
2081 * could not be created.
2082 * @hide
2083 */
2084 public UserInfo createProfileForUser(String name, int flags, @UserIdInt int userHandle,
2085 String[] disallowedPackages) {
Kenny Guya52dc3e2014-02-11 15:33:14 +00002086 try {
Sudheer Shanka7cb54a32016-09-16 12:59:05 -07002087 return mService.createProfileForUser(name, flags, userHandle, disallowedPackages);
Kenny Guya52dc3e2014-02-11 15:33:14 +00002088 } catch (RemoteException re) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07002089 throw re.rethrowFromSystemServer();
Kenny Guya52dc3e2014-02-11 15:33:14 +00002090 }
2091 }
2092
2093 /**
Tony Mak6dc428f2016-10-10 15:48:27 +01002094 * Similar to {@link #createProfileForUser(String, int, int, String[])}
Esteban Talavera6c9116a2016-11-24 16:12:44 +00002095 * except bypassing the checking of {@link UserManager#DISALLOW_ADD_MANAGED_PROFILE}.
Tony Mak6dc428f2016-10-10 15:48:27 +01002096 * Requires {@link android.Manifest.permission#MANAGE_USERS} permission.
2097 *
2098 * @see #createProfileForUser(String, int, int, String[])
2099 * @hide
2100 */
2101 public UserInfo createProfileForUserEvenWhenDisallowed(String name, int flags,
2102 @UserIdInt int userHandle, String[] disallowedPackages) {
2103 try {
2104 return mService.createProfileForUserEvenWhenDisallowed(name, flags, userHandle,
2105 disallowedPackages);
2106 } catch (RemoteException re) {
2107 throw re.rethrowFromSystemServer();
2108 }
2109 }
2110
2111 /**
Fyodor Kupolov02cb6e72015-09-18 18:20:55 -07002112 * Creates a restricted profile with the specified name. This method also sets necessary
2113 * restrictions and adds shared accounts.
Fyodor Kupolov06a484a2015-08-21 16:33:20 -07002114 *
2115 * @param name profile's name
2116 * @return UserInfo object for the created user, or null if the user could not be created.
2117 * @hide
2118 */
2119 public UserInfo createRestrictedProfile(String name) {
2120 try {
Fyodor Kupolov02cb6e72015-09-18 18:20:55 -07002121 UserHandle parentUserHandle = Process.myUserHandle();
2122 UserInfo user = mService.createRestrictedProfile(name,
2123 parentUserHandle.getIdentifier());
2124 if (user != null) {
2125 AccountManager.get(mContext).addSharedAccountsFromParentUser(parentUserHandle,
2126 UserHandle.of(user.id));
Fyodor Kupolov06a484a2015-08-21 16:33:20 -07002127 }
Fyodor Kupolov02cb6e72015-09-18 18:20:55 -07002128 return user;
Jeff Sharkey27b2e692016-02-25 17:40:12 -07002129 } catch (RemoteException re) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07002130 throw re.rethrowFromSystemServer();
Fyodor Kupolov06a484a2015-08-21 16:33:20 -07002131 }
Fyodor Kupolov06a484a2015-08-21 16:33:20 -07002132 }
2133
2134 /**
Amith Yamasani06964342016-04-15 13:55:01 -07002135 * Returns an intent to create a user for the provided name and account name. The name
2136 * and account name will be used when the setup process for the new user is started.
2137 * <p>
Amith Yamasani12747872015-12-07 14:19:49 -08002138 * The intent should be launched using startActivityForResult and the return result will
Amith Yamasani37ed8d12016-01-27 14:40:16 -08002139 * indicate if the user consented to adding a new user and if the operation succeeded. Any
2140 * errors in creating the user will be returned in the result code. If the user cancels the
2141 * request, the return result will be {@link Activity#RESULT_CANCELED}. On success, the
2142 * result code will be {@link Activity#RESULT_OK}.
Amith Yamasani06964342016-04-15 13:55:01 -07002143 * <p>
2144 * Use {@link #supportsMultipleUsers()} to first check if the device supports this operation
2145 * at all.
2146 * <p>
Amith Yamasani12747872015-12-07 14:19:49 -08002147 * The new user is created but not initialized. After switching into the user for the first
2148 * time, the preferred user name and account information are used by the setup process for that
2149 * user.
2150 *
2151 * @param userName Optional name to assign to the user.
Amith Yamasani06964342016-04-15 13:55:01 -07002152 * @param accountName Optional account name that will be used by the setup wizard to initialize
Amith Yamasani12747872015-12-07 14:19:49 -08002153 * the user.
2154 * @param accountType Optional account type for the account to be created. This is required
2155 * if the account name is specified.
2156 * @param accountOptions Optional bundle of data to be passed in during account creation in the
2157 * new user via {@link AccountManager#addAccount(String, String, String[],
2158 * Bundle, android.app.Activity, android.accounts.AccountManagerCallback,
2159 * Handler)}.
Amith Yamasani06964342016-04-15 13:55:01 -07002160 * @return An Intent that can be launched from an Activity.
Amith Yamasani37ed8d12016-01-27 14:40:16 -08002161 * @see #USER_CREATION_FAILED_NOT_PERMITTED
2162 * @see #USER_CREATION_FAILED_NO_MORE_USERS
Amith Yamasani06964342016-04-15 13:55:01 -07002163 * @see #supportsMultipleUsers
Amith Yamasani12747872015-12-07 14:19:49 -08002164 */
2165 public static Intent createUserCreationIntent(@Nullable String userName,
2166 @Nullable String accountName,
2167 @Nullable String accountType, @Nullable PersistableBundle accountOptions) {
Amith Yamasani12747872015-12-07 14:19:49 -08002168 Intent intent = new Intent(ACTION_CREATE_USER);
2169 if (userName != null) {
2170 intent.putExtra(EXTRA_USER_NAME, userName);
2171 }
2172 if (accountName != null && accountType == null) {
2173 throw new IllegalArgumentException("accountType must be specified if accountName is "
2174 + "specified");
2175 }
2176 if (accountName != null) {
2177 intent.putExtra(EXTRA_USER_ACCOUNT_NAME, accountName);
2178 }
2179 if (accountType != null) {
2180 intent.putExtra(EXTRA_USER_ACCOUNT_TYPE, accountType);
2181 }
2182 if (accountOptions != null) {
2183 intent.putExtra(EXTRA_USER_ACCOUNT_OPTIONS, accountOptions);
2184 }
2185 return intent;
2186 }
2187
2188 /**
2189 * @hide
2190 *
Jeff Sharkeyd86b8fe2017-06-02 17:36:26 -06002191 * Returns the preferred account name for user creation.
Amith Yamasani12747872015-12-07 14:19:49 -08002192 */
2193 @SystemApi
Jeff Sharkeyd86b8fe2017-06-02 17:36:26 -06002194 @RequiresPermission(android.Manifest.permission.MANAGE_USERS)
Amith Yamasani12747872015-12-07 14:19:49 -08002195 public String getSeedAccountName() {
2196 try {
2197 return mService.getSeedAccountName();
2198 } catch (RemoteException re) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07002199 throw re.rethrowFromSystemServer();
Amith Yamasani12747872015-12-07 14:19:49 -08002200 }
2201 }
2202
2203 /**
2204 * @hide
2205 *
Jeff Sharkeyd86b8fe2017-06-02 17:36:26 -06002206 * Returns the preferred account type for user creation.
Amith Yamasani12747872015-12-07 14:19:49 -08002207 */
2208 @SystemApi
Jeff Sharkeyd86b8fe2017-06-02 17:36:26 -06002209 @RequiresPermission(android.Manifest.permission.MANAGE_USERS)
Amith Yamasani12747872015-12-07 14:19:49 -08002210 public String getSeedAccountType() {
2211 try {
2212 return mService.getSeedAccountType();
2213 } catch (RemoteException re) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07002214 throw re.rethrowFromSystemServer();
Amith Yamasani12747872015-12-07 14:19:49 -08002215 }
2216 }
2217
2218 /**
2219 * @hide
2220 *
Jeff Sharkeyd86b8fe2017-06-02 17:36:26 -06002221 * Returns the preferred account's options bundle for user creation.
Amith Yamasani12747872015-12-07 14:19:49 -08002222 * @return Any options set by the requestor that created the user.
2223 */
2224 @SystemApi
Jeff Sharkeyd86b8fe2017-06-02 17:36:26 -06002225 @RequiresPermission(android.Manifest.permission.MANAGE_USERS)
Amith Yamasani12747872015-12-07 14:19:49 -08002226 public PersistableBundle getSeedAccountOptions() {
2227 try {
2228 return mService.getSeedAccountOptions();
2229 } catch (RemoteException re) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07002230 throw re.rethrowFromSystemServer();
Amith Yamasani12747872015-12-07 14:19:49 -08002231 }
2232 }
2233
2234 /**
2235 * @hide
2236 *
2237 * Called by a system activity to set the seed account information of a user created
2238 * through the user creation intent.
2239 * @param userId
2240 * @param accountName
2241 * @param accountType
2242 * @param accountOptions
2243 * @see #createUserCreationIntent(String, String, String, PersistableBundle)
2244 */
2245 public void setSeedAccountData(int userId, String accountName, String accountType,
2246 PersistableBundle accountOptions) {
2247 try {
2248 mService.setSeedAccountData(userId, accountName, accountType, accountOptions,
2249 /* persist= */ true);
2250 } catch (RemoteException re) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07002251 throw re.rethrowFromSystemServer();
Amith Yamasani12747872015-12-07 14:19:49 -08002252 }
2253 }
2254
2255 /**
2256 * @hide
Jeff Sharkeyd86b8fe2017-06-02 17:36:26 -06002257 * Clears the seed information used to create this user.
Amith Yamasani12747872015-12-07 14:19:49 -08002258 */
2259 @SystemApi
Jeff Sharkeyd86b8fe2017-06-02 17:36:26 -06002260 @RequiresPermission(android.Manifest.permission.MANAGE_USERS)
Amith Yamasani12747872015-12-07 14:19:49 -08002261 public void clearSeedAccountData() {
2262 try {
2263 mService.clearSeedAccountData();
2264 } catch (RemoteException re) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07002265 throw re.rethrowFromSystemServer();
Amith Yamasani12747872015-12-07 14:19:49 -08002266 }
2267 }
2268
2269 /**
Amith Yamasani1df14732014-08-29 21:37:27 -07002270 * @hide
2271 * Marks the guest user for deletion to allow a new guest to be created before deleting
2272 * the current user who is a guest.
2273 * @param userHandle
2274 * @return
2275 */
Jeff Sharkey8588bc12016-01-06 16:47:42 -07002276 public boolean markGuestForDeletion(@UserIdInt int userHandle) {
Amith Yamasani1df14732014-08-29 21:37:27 -07002277 try {
2278 return mService.markGuestForDeletion(userHandle);
2279 } catch (RemoteException re) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07002280 throw re.rethrowFromSystemServer();
Amith Yamasani1df14732014-08-29 21:37:27 -07002281 }
2282 }
2283
2284 /**
Alexandra Gherghinadf35d572014-04-09 13:54:39 +01002285 * Sets the user as enabled, if such an user exists.
Lenka Trochtova1ddda472016-02-12 10:42:12 +01002286 *
2287 * <p>Requires {@link android.Manifest.permission#MANAGE_USERS} permission.
2288 *
2289 * <p>Note that the default is true, it's only that managed profiles might not be enabled.
2290 * Also ephemeral users can be disabled to indicate that their removal is in progress and they
2291 * shouldn't be re-entered. Therefore ephemeral users should not be re-enabled once disabled.
Alexandra Gherghinadf35d572014-04-09 13:54:39 +01002292 *
jovanakf24ad492018-05-18 12:15:59 -07002293 * @param userId the id of the profile to enable
Alexandra Gherghinadf35d572014-04-09 13:54:39 +01002294 * @hide
2295 */
jovanakf24ad492018-05-18 12:15:59 -07002296 public void setUserEnabled(@UserIdInt int userId) {
Alexandra Gherghinadf35d572014-04-09 13:54:39 +01002297 try {
jovanakf24ad492018-05-18 12:15:59 -07002298 mService.setUserEnabled(userId);
2299 } catch (RemoteException re) {
2300 throw re.rethrowFromSystemServer();
2301 }
2302 }
2303
2304 /**
2305 * Assigns admin privileges to the user, if such a user exists.
2306 *
2307 * <p>Requires {@link android.Manifest.permission#MANAGE_USERS} and
2308 * {@link android.Manifest.permission#INTERACT_ACROSS_USERS_FULL} permissions.
2309 *
2310 * @param userHandle the id of the user to become admin
2311 * @hide
2312 */
2313 @RequiresPermission(allOf = {
2314 Manifest.permission.INTERACT_ACROSS_USERS_FULL,
2315 Manifest.permission.MANAGE_USERS
2316 })
2317 public void setUserAdmin(@UserIdInt int userHandle) {
2318 try {
2319 mService.setUserAdmin(userHandle);
Jeff Sharkey27b2e692016-02-25 17:40:12 -07002320 } catch (RemoteException re) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07002321 throw re.rethrowFromSystemServer();
Alexandra Gherghinadf35d572014-04-09 13:54:39 +01002322 }
2323 }
2324
2325 /**
Andrew Scull85a63bc2016-10-24 13:47:47 +01002326 * Evicts the user's credential encryption key from memory by stopping and restarting the user.
2327 *
2328 * @hide
2329 */
2330 public void evictCredentialEncryptionKey(@UserIdInt int userHandle) {
2331 try {
2332 mService.evictCredentialEncryptionKey(userHandle);
2333 } catch (RemoteException re) {
2334 throw re.rethrowFromSystemServer();
2335 }
2336 }
2337
2338 /**
Dianne Hackbornb26306a2012-10-24 15:22:21 -07002339 * Return the number of users currently created on the device.
2340 */
2341 public int getUserCount() {
2342 List<UserInfo> users = getUsers();
2343 return users != null ? users.size() : 1;
2344 }
2345
2346 /**
Amith Yamasanid04aaa32016-06-13 12:09:36 -07002347 * Returns information for all users on this device, including ones marked for deletion.
2348 * To retrieve only users that are alive, use {@link #getUsers(boolean)}.
2349 * <p>
Amith Yamasani195263742012-08-21 15:40:12 -07002350 * Requires {@link android.Manifest.permission#MANAGE_USERS} permission.
Fyodor Kupolov940e8572016-01-26 12:03:51 -08002351 * @return the list of users that exist on the device.
Amith Yamasani258848d2012-08-10 17:06:33 -07002352 * @hide
2353 */
Andrei Onea24ec3212019-03-15 17:35:05 +00002354 @UnsupportedAppUsage
Amith Yamasani258848d2012-08-10 17:06:33 -07002355 public List<UserInfo> getUsers() {
2356 try {
Amith Yamasani920ace02012-09-20 22:15:37 -07002357 return mService.getUsers(false);
2358 } catch (RemoteException re) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07002359 throw re.rethrowFromSystemServer();
Amith Yamasani920ace02012-09-20 22:15:37 -07002360 }
2361 }
2362
2363 /**
Fyodor Kupolov940e8572016-01-26 12:03:51 -08002364 * Returns serial numbers of all users on this device.
Fyodor Kupolov940e8572016-01-26 12:03:51 -08002365 *
2366 * @param excludeDying specify if the list should exclude users being removed.
2367 * @return the list of serial numbers of users that exist on the device.
2368 * @hide
2369 */
2370 @SystemApi
Jeff Sharkeyd86b8fe2017-06-02 17:36:26 -06002371 @RequiresPermission(android.Manifest.permission.MANAGE_USERS)
Fyodor Kupolov940e8572016-01-26 12:03:51 -08002372 public long[] getSerialNumbersOfUsers(boolean excludeDying) {
2373 try {
2374 List<UserInfo> users = mService.getUsers(excludeDying);
2375 long[] result = new long[users.size()];
2376 for (int i = 0; i < result.length; i++) {
2377 result[i] = users.get(i).serialNumber;
2378 }
2379 return result;
2380 } catch (RemoteException re) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07002381 throw re.rethrowFromSystemServer();
Fyodor Kupolov940e8572016-01-26 12:03:51 -08002382 }
2383 }
2384
2385 /**
Xiaohui Chenb3b92582015-12-07 11:22:13 -08002386 * @return the user's account name, null if not found.
2387 * @hide
2388 */
2389 @RequiresPermission( allOf = {
2390 Manifest.permission.INTERACT_ACROSS_USERS_FULL,
2391 Manifest.permission.MANAGE_USERS
2392 })
Jeff Sharkey8588bc12016-01-06 16:47:42 -07002393 public @Nullable String getUserAccount(@UserIdInt int userHandle) {
Xiaohui Chenb3b92582015-12-07 11:22:13 -08002394 try {
2395 return mService.getUserAccount(userHandle);
2396 } catch (RemoteException re) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07002397 throw re.rethrowFromSystemServer();
Xiaohui Chenb3b92582015-12-07 11:22:13 -08002398 }
2399 }
2400
2401 /**
2402 * Set account name for the given user.
2403 * @hide
2404 */
2405 @RequiresPermission( allOf = {
2406 Manifest.permission.INTERACT_ACROSS_USERS_FULL,
2407 Manifest.permission.MANAGE_USERS
2408 })
Jeff Sharkey8588bc12016-01-06 16:47:42 -07002409 public void setUserAccount(@UserIdInt int userHandle, @Nullable String accountName) {
Xiaohui Chenb3b92582015-12-07 11:22:13 -08002410 try {
2411 mService.setUserAccount(userHandle, accountName);
2412 } catch (RemoteException re) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07002413 throw re.rethrowFromSystemServer();
Xiaohui Chenb3b92582015-12-07 11:22:13 -08002414 }
2415 }
2416
2417 /**
Xiaohui Chen70f6c382015-04-28 14:21:43 -07002418 * Returns information for Primary user.
2419 * Requires {@link android.Manifest.permission#MANAGE_USERS} permission.
2420 *
2421 * @return the Primary user, null if not found.
2422 * @hide
2423 */
Xiaohui Chen7cb69df2015-07-13 16:01:01 -07002424 public @Nullable UserInfo getPrimaryUser() {
Xiaohui Chen70f6c382015-04-28 14:21:43 -07002425 try {
2426 return mService.getPrimaryUser();
2427 } catch (RemoteException re) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07002428 throw re.rethrowFromSystemServer();
Xiaohui Chen70f6c382015-04-28 14:21:43 -07002429 }
2430 }
2431
2432 /**
Amith Yamasani95ab7842014-08-11 17:09:26 -07002433 * Checks whether it's possible to add more users. Caller must hold the MANAGE_USERS
2434 * permission.
2435 *
2436 * @return true if more users can be added, false if limit has been reached.
2437 * @hide
2438 */
2439 public boolean canAddMoreUsers() {
2440 final List<UserInfo> users = getUsers(true);
2441 final int totalUserCount = users.size();
2442 int aliveUserCount = 0;
2443 for (int i = 0; i < totalUserCount; i++) {
2444 UserInfo user = users.get(i);
2445 if (!user.isGuest()) {
2446 aliveUserCount++;
2447 }
2448 }
2449 return aliveUserCount < getMaxSupportedUsers();
2450 }
2451
2452 /**
Nicolas Prevot72434b72015-05-13 12:15:03 -07002453 * Checks whether it's possible to add more managed profiles. Caller must hold the MANAGE_USERS
2454 * permission.
Nicolas Prevot07387fe2015-10-30 17:53:30 +00002455 * if allowedToRemoveOne is true and if the user already has a managed profile, then return if
2456 * we could add a new managed profile to this user after removing the existing one.
Nicolas Prevot72434b72015-05-13 12:15:03 -07002457 *
2458 * @return true if more managed profiles can be added, false if limit has been reached.
2459 * @hide
2460 */
Jeff Sharkey8588bc12016-01-06 16:47:42 -07002461 public boolean canAddMoreManagedProfiles(@UserIdInt int userId, boolean allowedToRemoveOne) {
Nicolas Prevot72434b72015-05-13 12:15:03 -07002462 try {
Nicolas Prevot07387fe2015-10-30 17:53:30 +00002463 return mService.canAddMoreManagedProfiles(userId, allowedToRemoveOne);
Nicolas Prevot72434b72015-05-13 12:15:03 -07002464 } catch (RemoteException re) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07002465 throw re.rethrowFromSystemServer();
Nicolas Prevot72434b72015-05-13 12:15:03 -07002466 }
2467 }
2468
2469 /**
Kenny Guy2a764942014-04-02 13:29:20 +01002470 * Returns list of the profiles of userHandle including
2471 * userHandle itself.
Amith Yamasani4f7e2e32014-08-14 18:49:48 -07002472 * Note that this returns both enabled and not enabled profiles. See
Ruben Brunk7f75da22015-04-30 17:46:30 -07002473 * {@link #getEnabledProfiles(int)} if you need only the enabled ones.
Amith Yamasani4f582632014-02-19 14:31:52 -08002474 *
Kenny Guy2a764942014-04-02 13:29:20 +01002475 * Requires {@link android.Manifest.permission#MANAGE_USERS} permission.
2476 * @param userHandle profiles of this user will be returned.
2477 * @return the list of profiles.
2478 * @hide
2479 */
Andrei Onea24ec3212019-03-15 17:35:05 +00002480 @UnsupportedAppUsage
Jeff Sharkey8588bc12016-01-06 16:47:42 -07002481 public List<UserInfo> getProfiles(@UserIdInt int userHandle) {
Kenny Guya52dc3e2014-02-11 15:33:14 +00002482 try {
Alexandra Gherghina385124d2014-04-03 13:37:39 +01002483 return mService.getProfiles(userHandle, false /* enabledOnly */);
Kenny Guya52dc3e2014-02-11 15:33:14 +00002484 } catch (RemoteException re) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07002485 throw re.rethrowFromSystemServer();
Kenny Guya52dc3e2014-02-11 15:33:14 +00002486 }
2487 }
2488
2489 /**
Xiaohui Chenfd5b7742015-10-14 15:47:04 -07002490 * Requires {@link android.Manifest.permission#MANAGE_USERS} permission.
2491 * @param userId one of the two user ids to check.
2492 * @param otherUserId one of the two user ids to check.
2493 * @return true if the two user ids are in the same profile group.
2494 * @hide
2495 */
Jeff Sharkey8588bc12016-01-06 16:47:42 -07002496 public boolean isSameProfileGroup(@UserIdInt int userId, int otherUserId) {
Xiaohui Chenfd5b7742015-10-14 15:47:04 -07002497 try {
2498 return mService.isSameProfileGroup(userId, otherUserId);
2499 } catch (RemoteException re) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07002500 throw re.rethrowFromSystemServer();
Xiaohui Chenfd5b7742015-10-14 15:47:04 -07002501 }
2502 }
2503
2504 /**
Ruben Brunk7f75da22015-04-30 17:46:30 -07002505 * Returns list of the profiles of userHandle including
2506 * userHandle itself.
2507 * Note that this returns only enabled.
2508 *
2509 * Requires {@link android.Manifest.permission#MANAGE_USERS} permission.
2510 * @param userHandle profiles of this user will be returned.
2511 * @return the list of profiles.
2512 * @hide
2513 */
Andrei Onea24ec3212019-03-15 17:35:05 +00002514 @UnsupportedAppUsage
Jeff Sharkey8588bc12016-01-06 16:47:42 -07002515 public List<UserInfo> getEnabledProfiles(@UserIdInt int userHandle) {
Ruben Brunk7f75da22015-04-30 17:46:30 -07002516 try {
2517 return mService.getProfiles(userHandle, true /* enabledOnly */);
2518 } catch (RemoteException re) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07002519 throw re.rethrowFromSystemServer();
Ruben Brunk7f75da22015-04-30 17:46:30 -07002520 }
2521 }
2522
2523 /**
Jessica Hummelbe81c802014-04-22 15:49:22 +01002524 * Returns a list of UserHandles for profiles associated with the user that the calling process
2525 * is running on, including the user itself.
Amith Yamasani4f582632014-02-19 14:31:52 -08002526 *
2527 * @return A non-empty list of UserHandles associated with the calling user.
2528 */
2529 public List<UserHandle> getUserProfiles() {
Fyodor Kupolov7f98aa42016-04-07 14:56:25 -07002530 int[] userIds = getProfileIds(UserHandle.myUserId(), true /* enabledOnly */);
2531 List<UserHandle> result = new ArrayList<>(userIds.length);
2532 for (int userId : userIds) {
2533 result.add(UserHandle.of(userId));
2534 }
2535 return result;
2536 }
2537
2538 /**
2539 * Returns a list of ids for profiles associated with the specified user including the user
2540 * itself.
2541 *
2542 * @param userId id of the user to return profiles for
2543 * @param enabledOnly whether return only {@link UserInfo#isEnabled() enabled} profiles
2544 * @return A non-empty list of ids of profiles associated with the specified user.
2545 *
2546 * @hide
2547 */
Philip P. Moltmann4e615e62018-08-28 14:57:49 -07002548 @RequiresPermission(anyOf = {Manifest.permission.MANAGE_USERS,
2549 Manifest.permission.CREATE_USERS}, conditional = true)
Philip P. Moltmann4e615e62018-08-28 14:57:49 -07002550 public @NonNull int[] getProfileIds(@UserIdInt int userId, boolean enabledOnly) {
Alexandra Gherghina385124d2014-04-03 13:37:39 +01002551 try {
Fyodor Kupolov7f98aa42016-04-07 14:56:25 -07002552 return mService.getProfileIds(userId, enabledOnly);
Alexandra Gherghina385124d2014-04-03 13:37:39 +01002553 } catch (RemoteException re) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07002554 throw re.rethrowFromSystemServer();
Alexandra Gherghina385124d2014-04-03 13:37:39 +01002555 }
Fyodor Kupolov7f98aa42016-04-07 14:56:25 -07002556 }
2557
2558 /**
2559 * @see #getProfileIds(int, boolean)
2560 * @hide
2561 */
Andrei Onea24ec3212019-03-15 17:35:05 +00002562 @UnsupportedAppUsage
Fyodor Kupolov7f98aa42016-04-07 14:56:25 -07002563 public int[] getProfileIdsWithDisabled(@UserIdInt int userId) {
2564 return getProfileIds(userId, false /* enabledOnly */);
2565 }
2566
2567 /**
2568 * @see #getProfileIds(int, boolean)
2569 * @hide
2570 */
2571 public int[] getEnabledProfileIds(@UserIdInt int userId) {
2572 return getProfileIds(userId, true /* enabledOnly */);
Amith Yamasani4f582632014-02-19 14:31:52 -08002573 }
2574
Amith Yamasani7dda2652014-04-11 14:57:12 -07002575 /**
Andres Moralesc5548c02015-08-05 10:23:12 -07002576 * Returns the device credential owner id of the profile from
2577 * which this method is called, or userHandle if called from a user that
2578 * is not a profile.
2579 *
2580 * @hide
2581 */
Jeff Sharkey8588bc12016-01-06 16:47:42 -07002582 public int getCredentialOwnerProfile(@UserIdInt int userHandle) {
Andres Moralesc5548c02015-08-05 10:23:12 -07002583 try {
2584 return mService.getCredentialOwnerProfile(userHandle);
2585 } catch (RemoteException re) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07002586 throw re.rethrowFromSystemServer();
Andres Moralesc5548c02015-08-05 10:23:12 -07002587 }
2588 }
2589
2590 /**
Jessica Hummelbe81c802014-04-22 15:49:22 +01002591 * Returns the parent of the profile which this method is called from
2592 * or null if called from a user that is not a profile.
2593 *
2594 * @hide
2595 */
Andrei Onea24ec3212019-03-15 17:35:05 +00002596 @UnsupportedAppUsage
Jeff Sharkey8588bc12016-01-06 16:47:42 -07002597 public UserInfo getProfileParent(@UserIdInt int userHandle) {
Jessica Hummelbe81c802014-04-22 15:49:22 +01002598 try {
2599 return mService.getProfileParent(userHandle);
2600 } catch (RemoteException re) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07002601 throw re.rethrowFromSystemServer();
Jessica Hummelbe81c802014-04-22 15:49:22 +01002602 }
2603 }
2604
2605 /**
Philip P. Moltmanna3e358c2018-11-21 12:45:50 -08002606 * Get the parent of a user profile.
2607 *
2608 * @param user the handle of the user profile
2609 *
2610 * @return the parent of the user or {@code null} if the user is not profile
2611 *
2612 * @hide
2613 */
2614 @SystemApi
2615 @RequiresPermission(android.Manifest.permission.MANAGE_USERS)
2616 public @Nullable UserHandle getProfileParent(@NonNull UserHandle user) {
2617 UserInfo info = getProfileParent(user.getIdentifier());
2618
2619 if (info == null) {
2620 return null;
2621 }
2622
2623 return UserHandle.of(info.id);
2624 }
2625
2626 /**
Tony Make3d1f652017-12-12 11:00:37 +00002627 * Enables or disables quiet mode for a managed profile. If quiet mode is enabled, apps in a
2628 * managed profile don't run, generate notifications, or consume data or battery.
2629 * <p>
2630 * If a user's credential is needed to turn off quiet mode, a confirm credential screen will be
2631 * shown to the user.
2632 * <p>
2633 * The change may not happen instantly, however apps can listen for
2634 * {@link Intent#ACTION_MANAGED_PROFILE_AVAILABLE} and
2635 * {@link Intent#ACTION_MANAGED_PROFILE_UNAVAILABLE} broadcasts in order to be notified of
2636 * the change of the quiet mode. Apps can also check the current state of quiet mode by
2637 * calling {@link #isQuietModeEnabled(UserHandle)}.
2638 * <p>
2639 * The caller must either be the foreground default launcher or have one of these permissions:
2640 * {@code MANAGE_USERS} or {@code MODIFY_QUIET_MODE}.
Rubin Xu0a29ecd2015-11-04 15:11:48 +00002641 *
Tony Make3d1f652017-12-12 11:00:37 +00002642 * @param enableQuietMode whether quiet mode should be enabled or disabled
2643 * @param userHandle user handle of the profile
2644 * @return {@code false} if user's credential is needed in order to turn off quiet mode,
2645 * {@code true} otherwise
2646 * @throws SecurityException if the caller is invalid
2647 * @throws IllegalArgumentException if {@code userHandle} is not a managed profile
2648 *
2649 * @see #isQuietModeEnabled(UserHandle)
Rubin Xu0a29ecd2015-11-04 15:11:48 +00002650 */
Tony Makbece85d2018-01-12 12:10:17 +00002651 public boolean requestQuietModeEnabled(boolean enableQuietMode, @NonNull UserHandle userHandle) {
2652 return requestQuietModeEnabled(enableQuietMode, userHandle, null);
Tony Makb7e6fd42017-12-05 19:40:28 +00002653 }
2654
2655 /**
Tony Makbece85d2018-01-12 12:10:17 +00002656 * Similar to {@link #requestQuietModeEnabled(boolean, UserHandle)}, except you can specify
Tony Makd390ae92017-12-28 13:23:10 +00002657 * a target to start when user is unlocked. If {@code target} is specified, caller must have
2658 * the {@link android.Manifest.permission#MANAGE_USERS} permission.
Tony Makb7e6fd42017-12-05 19:40:28 +00002659 *
Tony Makbece85d2018-01-12 12:10:17 +00002660 * @see {@link #requestQuietModeEnabled(boolean, UserHandle)}
Rubin Xu0a29ecd2015-11-04 15:11:48 +00002661 * @hide
2662 */
Tony Makbece85d2018-01-12 12:10:17 +00002663 public boolean requestQuietModeEnabled(
Tony Makb7e6fd42017-12-05 19:40:28 +00002664 boolean enableQuietMode, @NonNull UserHandle userHandle, IntentSender target) {
Rubin Xu0a29ecd2015-11-04 15:11:48 +00002665 try {
Tony Makbece85d2018-01-12 12:10:17 +00002666 return mService.requestQuietModeEnabled(
Tony Make3d1f652017-12-12 11:00:37 +00002667 mContext.getPackageName(), enableQuietMode, userHandle.getIdentifier(), target);
Jeff Sharkey27b2e692016-02-25 17:40:12 -07002668 } catch (RemoteException re) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07002669 throw re.rethrowFromSystemServer();
Rubin Xu0a29ecd2015-11-04 15:11:48 +00002670 }
2671 }
2672
2673 /**
2674 * Returns whether the given profile is in quiet mode or not.
Ricky Wai7881cf82016-04-15 17:20:12 +01002675 * Notes: Quiet mode is only supported for managed profiles.
Rubin Xu0a29ecd2015-11-04 15:11:48 +00002676 *
2677 * @param userHandle The user handle of the profile to be queried.
2678 * @return true if the profile is in quiet mode, false otherwise.
2679 */
2680 public boolean isQuietModeEnabled(UserHandle userHandle) {
2681 try {
2682 return mService.isQuietModeEnabled(userHandle.getIdentifier());
Jeff Sharkey27b2e692016-02-25 17:40:12 -07002683 } catch (RemoteException re) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07002684 throw re.rethrowFromSystemServer();
Rubin Xu0a29ecd2015-11-04 15:11:48 +00002685 }
Rubin Xu0a29ecd2015-11-04 15:11:48 +00002686 }
2687
2688 /**
Amith Yamasani7dda2652014-04-11 14:57:12 -07002689 * If the target user is a managed profile of the calling user or the caller
2690 * is itself a managed profile, then this returns a badged copy of the given
Svetoslavc71c42f2014-08-05 18:57:05 -07002691 * icon to be able to distinguish it from the original icon. For badging an
2692 * arbitrary drawable use {@link #getBadgedDrawableForUser(
2693 * android.graphics.drawable.Drawable, UserHandle, android.graphics.Rect, int)}.
2694 * <p>
2695 * If the original drawable is a BitmapDrawable and the backing bitmap is
Vadim Tryshev66ae66a2016-02-18 15:41:21 -08002696 * mutable as per {@link android.graphics.Bitmap#isMutable()}, the badging
Svetoslavc71c42f2014-08-05 18:57:05 -07002697 * is performed in place and the original drawable is returned.
2698 * </p>
Amith Yamasani7dda2652014-04-11 14:57:12 -07002699 *
2700 * @param icon The icon to badge.
2701 * @param user The target user.
2702 * @return A drawable that combines the original icon and a badge as
2703 * determined by the system.
Svetoslavc7d62f02014-09-04 15:39:54 -07002704 * @removed
Amith Yamasani7dda2652014-04-11 14:57:12 -07002705 */
Svetoslavc71c42f2014-08-05 18:57:05 -07002706 public Drawable getBadgedIconForUser(Drawable icon, UserHandle user) {
Svetoslavc7d62f02014-09-04 15:39:54 -07002707 return mContext.getPackageManager().getUserBadgedIcon(icon, user);
Amith Yamasani4f582632014-02-19 14:31:52 -08002708 }
2709
Kenny Guy701ea7c2014-05-08 23:34:12 +01002710 /**
2711 * If the target user is a managed profile of the calling user or the caller
Svetoslavc71c42f2014-08-05 18:57:05 -07002712 * is itself a managed profile, then this returns a badged copy of the given
2713 * drawable allowing the user to distinguish it from the original drawable.
2714 * The caller can specify the location in the bounds of the drawable to be
2715 * badged where the badge should be applied as well as the density of the
2716 * badge to be used.
2717 * <p>
2718 * If the original drawable is a BitmapDrawable and the backing bitmap is
Vadim Tryshev66ae66a2016-02-18 15:41:21 -08002719 * mutable as per {@link android.graphics.Bitmap#isMutable()}, the badging
Svetoslavc71c42f2014-08-05 18:57:05 -07002720 * is performed in place and the original drawable is returned.
2721 * </p>
2722 *
2723 * @param badgedDrawable The drawable to badge.
2724 * @param user The target user.
2725 * @param badgeLocation Where in the bounds of the badged drawable to place
Vadim Tryshev66ae66a2016-02-18 15:41:21 -08002726 * the badge. If it's {@code null}, the badge is applied on top of the entire
Svetoslavc71c42f2014-08-05 18:57:05 -07002727 * drawable being badged.
2728 * @param badgeDensity The optional desired density for the badge as per
Vadim Tryshev66ae66a2016-02-18 15:41:21 -08002729 * {@link android.util.DisplayMetrics#densityDpi}. If it's not positive,
Svetoslavc71c42f2014-08-05 18:57:05 -07002730 * the density of the display is used.
2731 * @return A drawable that combines the original drawable and a badge as
2732 * determined by the system.
Svetoslavc7d62f02014-09-04 15:39:54 -07002733 * @removed
Svetoslavc71c42f2014-08-05 18:57:05 -07002734 */
2735 public Drawable getBadgedDrawableForUser(Drawable badgedDrawable, UserHandle user,
2736 Rect badgeLocation, int badgeDensity) {
Svetoslavc7d62f02014-09-04 15:39:54 -07002737 return mContext.getPackageManager().getUserBadgedDrawableForDensity(badgedDrawable, user,
2738 badgeLocation, badgeDensity);
Svetoslavc71c42f2014-08-05 18:57:05 -07002739 }
2740
2741 /**
2742 * If the target user is a managed profile of the calling user or the caller
Kenny Guyf7ecf7c2014-06-18 11:32:05 +01002743 * is itself a managed profile, then this returns a copy of the label with
2744 * badging for accessibility services like talkback. E.g. passing in "Email"
2745 * and it might return "Work Email" for Email in the work profile.
2746 *
2747 * @param label The label to change.
2748 * @param user The target user.
2749 * @return A label that combines the original label and a badge as
2750 * determined by the system.
Svetoslavc7d62f02014-09-04 15:39:54 -07002751 * @removed
Kenny Guyf7ecf7c2014-06-18 11:32:05 +01002752 */
Kenny Guy237aecd2014-07-21 14:06:09 +01002753 public CharSequence getBadgedLabelForUser(CharSequence label, UserHandle user) {
Svetoslavc7d62f02014-09-04 15:39:54 -07002754 return mContext.getPackageManager().getUserBadgedLabel(label, user);
Amith Yamasani4f582632014-02-19 14:31:52 -08002755 }
2756
2757 /**
2758 * Returns information for all users on this device. Requires
2759 * {@link android.Manifest.permission#MANAGE_USERS} permission.
Emily Bernier394a6cd2014-05-07 12:49:20 -04002760 *
Amith Yamasani4f582632014-02-19 14:31:52 -08002761 * @param excludeDying specify if the list should exclude users being
2762 * removed.
Amith Yamasani920ace02012-09-20 22:15:37 -07002763 * @return the list of users that were created.
2764 * @hide
2765 */
Andrei Onea24ec3212019-03-15 17:35:05 +00002766 @UnsupportedAppUsage
Adam Lesinskiada8deb2017-05-12 13:50:42 -07002767 public @NonNull List<UserInfo> getUsers(boolean excludeDying) {
Amith Yamasani920ace02012-09-20 22:15:37 -07002768 try {
2769 return mService.getUsers(excludeDying);
Amith Yamasani258848d2012-08-10 17:06:33 -07002770 } catch (RemoteException re) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07002771 throw re.rethrowFromSystemServer();
Amith Yamasani258848d2012-08-10 17:06:33 -07002772 }
2773 }
2774
2775 /**
2776 * Removes a user and all associated data.
Amith Yamasani195263742012-08-21 15:40:12 -07002777 * Requires {@link android.Manifest.permission#MANAGE_USERS} permission.
Amith Yamasani258848d2012-08-10 17:06:33 -07002778 * @param userHandle the integer handle of the user, where 0 is the primary user.
2779 * @hide
2780 */
Andrei Onea24ec3212019-03-15 17:35:05 +00002781 @UnsupportedAppUsage
Jeff Sharkey8588bc12016-01-06 16:47:42 -07002782 public boolean removeUser(@UserIdInt int userHandle) {
Amith Yamasani258848d2012-08-10 17:06:33 -07002783 try {
2784 return mService.removeUser(userHandle);
2785 } catch (RemoteException re) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07002786 throw re.rethrowFromSystemServer();
Amith Yamasani258848d2012-08-10 17:06:33 -07002787 }
2788 }
2789
2790 /**
Varun Shahe38c6782018-12-04 16:57:49 -08002791 * Removes a user and all associated data.
2792 *
2793 * @param user the user that needs to be removed.
Varun Shah9533e622019-02-27 15:26:56 -08002794 * @return {@code true} if the user was successfully removed, {@code false} otherwise.
2795 * @throws IllegalArgumentException if {@code user} is {@code null}
Varun Shahe38c6782018-12-04 16:57:49 -08002796 * @hide
2797 */
2798 @SystemApi
2799 @RequiresPermission(android.Manifest.permission.MANAGE_USERS)
Varun Shah9533e622019-02-27 15:26:56 -08002800 public boolean removeUser(@NonNull UserHandle user) {
2801 if (user == null) {
2802 throw new IllegalArgumentException("user cannot be null");
2803 }
Varun Shahe38c6782018-12-04 16:57:49 -08002804 return removeUser(user.getIdentifier());
2805 }
2806
2807
2808 /**
Nicolas Prevotd37c4a92017-01-23 11:56:00 +00002809 * Similar to {@link #removeUser(int)} except bypassing the checking of
2810 * {@link UserManager#DISALLOW_REMOVE_USER}
2811 * or {@link UserManager#DISALLOW_REMOVE_MANAGED_PROFILE}.
2812 *
2813 * @see {@link #removeUser(int)}
2814 * @hide
2815 */
2816 public boolean removeUserEvenWhenDisallowed(@UserIdInt int userHandle) {
2817 try {
2818 return mService.removeUserEvenWhenDisallowed(userHandle);
2819 } catch (RemoteException re) {
2820 throw re.rethrowFromSystemServer();
2821 }
2822 }
2823
2824 /**
Amith Yamasani258848d2012-08-10 17:06:33 -07002825 * Updates the user's name.
Amith Yamasani195263742012-08-21 15:40:12 -07002826 * Requires {@link android.Manifest.permission#MANAGE_USERS} permission.
Amith Yamasani258848d2012-08-10 17:06:33 -07002827 *
2828 * @param userHandle the user's integer handle
2829 * @param name the new name for the user
2830 * @hide
2831 */
Jeff Sharkey8588bc12016-01-06 16:47:42 -07002832 public void setUserName(@UserIdInt int userHandle, String name) {
Amith Yamasani258848d2012-08-10 17:06:33 -07002833 try {
2834 mService.setUserName(userHandle, name);
2835 } catch (RemoteException re) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07002836 throw re.rethrowFromSystemServer();
Amith Yamasani258848d2012-08-10 17:06:33 -07002837 }
2838 }
2839
2840 /**
Leo Hsuab28fff2019-01-30 15:40:56 +08002841 * Updates the calling user's name.
2842 * Requires {@link android.Manifest.permission#MANAGE_USERS} permission.
2843 *
2844 * @param name the new name for the user
2845 * @hide
2846 */
2847 @SystemApi
2848 @RequiresPermission(android.Manifest.permission.MANAGE_USERS)
Leo Hsu51839332019-03-05 16:59:04 +08002849 public void setUserName(@Nullable String name) {
Leo Hsuab28fff2019-01-30 15:40:56 +08002850 setUserName(getUserHandle(), name);
2851 }
2852
2853 /**
Amith Yamasanie928d7d2012-09-17 21:46:51 -07002854 * Sets the user's photo.
Amith Yamasani258848d2012-08-10 17:06:33 -07002855 * @param userHandle the user for whom to change the photo.
Amith Yamasanie928d7d2012-09-17 21:46:51 -07002856 * @param icon the bitmap to set as the photo.
Amith Yamasani258848d2012-08-10 17:06:33 -07002857 * @hide
2858 */
Jeff Sharkey8588bc12016-01-06 16:47:42 -07002859 public void setUserIcon(@UserIdInt int userHandle, Bitmap icon) {
Amith Yamasani258848d2012-08-10 17:06:33 -07002860 try {
Amith Yamasanie928d7d2012-09-17 21:46:51 -07002861 mService.setUserIcon(userHandle, icon);
Amith Yamasani258848d2012-08-10 17:06:33 -07002862 } catch (RemoteException re) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07002863 throw re.rethrowFromSystemServer();
Amith Yamasani258848d2012-08-10 17:06:33 -07002864 }
2865 }
2866
2867 /**
Leo Hsuab28fff2019-01-30 15:40:56 +08002868 * Sets the calling user's photo.
2869 * Requires {@link android.Manifest.permission#MANAGE_USERS} permission.
2870 *
2871 * @param icon the bitmap to set as the photo.
2872 * @hide
2873 */
2874 @SystemApi
2875 @RequiresPermission(android.Manifest.permission.MANAGE_USERS)
Leo Hsu51839332019-03-05 16:59:04 +08002876 public void setUserIcon(@NonNull Bitmap icon) {
Leo Hsuab28fff2019-01-30 15:40:56 +08002877 setUserIcon(getUserHandle(), icon);
2878 }
2879
2880 /**
Amith Yamasani3b49f072012-09-17 10:21:43 -07002881 * Returns a file descriptor for the user's photo. PNG data can be read from this file.
2882 * @param userHandle the user whose photo we want to read.
Amith Yamasanie928d7d2012-09-17 21:46:51 -07002883 * @return a {@link Bitmap} of the user's photo, or null if there's no photo.
Alexandra Gherghina64d4dca2014-08-28 18:26:56 +01002884 * @see com.android.internal.util.UserIcons#getDefaultUserIcon for a default.
Amith Yamasani3b49f072012-09-17 10:21:43 -07002885 * @hide
2886 */
Andrei Onea24ec3212019-03-15 17:35:05 +00002887 @UnsupportedAppUsage
Jeff Sharkey8588bc12016-01-06 16:47:42 -07002888 public Bitmap getUserIcon(@UserIdInt int userHandle) {
Amith Yamasani3b49f072012-09-17 10:21:43 -07002889 try {
Adrian Roos1bdff912015-02-17 15:51:35 +01002890 ParcelFileDescriptor fd = mService.getUserIcon(userHandle);
2891 if (fd != null) {
2892 try {
2893 return BitmapFactory.decodeFileDescriptor(fd.getFileDescriptor());
2894 } finally {
2895 try {
2896 fd.close();
2897 } catch (IOException e) {
2898 }
2899 }
2900 }
Amith Yamasani3b49f072012-09-17 10:21:43 -07002901 } catch (RemoteException re) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07002902 throw re.rethrowFromSystemServer();
Amith Yamasani3b49f072012-09-17 10:21:43 -07002903 }
Adrian Roos1bdff912015-02-17 15:51:35 +01002904 return null;
Amith Yamasani3b49f072012-09-17 10:21:43 -07002905 }
2906
2907 /**
Leo Hsuab28fff2019-01-30 15:40:56 +08002908 * Returns a Bitmap for the calling user's photo.
Bookatzcde3d922019-03-08 14:30:00 -08002909 * Requires {@link android.Manifest.permission#MANAGE_USERS}
2910 * or {@link android.Manifest.permission#GET_ACCOUNTS_PRIVILEGED} permissions.
Leo Hsuab28fff2019-01-30 15:40:56 +08002911 *
2912 * @return a {@link Bitmap} of the user's photo, or null if there's no photo.
2913 * @see com.android.internal.util.UserIcons#getDefaultUserIcon for a default.
2914 * @hide
2915 */
2916 @SystemApi
Bookatzcde3d922019-03-08 14:30:00 -08002917 @RequiresPermission(anyOf = {android.Manifest.permission.MANAGE_USERS,
2918 android.Manifest.permission.GET_ACCOUNTS_PRIVILEGED})
Leo Hsu51839332019-03-05 16:59:04 +08002919 public @Nullable Bitmap getUserIcon() {
Leo Hsuab28fff2019-01-30 15:40:56 +08002920 return getUserIcon(getUserHandle());
2921 }
2922
2923 /**
Amith Yamasani258848d2012-08-10 17:06:33 -07002924 * Returns the maximum number of users that can be created on this device. A return value
2925 * of 1 means that it is a single user device.
2926 * @hide
Maggie Benthalla12fccf2013-03-14 18:02:12 -04002927 * @return a value greater than or equal to 1
Amith Yamasani258848d2012-08-10 17:06:33 -07002928 */
Andrei Onea24ec3212019-03-15 17:35:05 +00002929 @UnsupportedAppUsage
Jeff Sharkey27bd34d2012-09-16 12:49:00 -07002930 public static int getMaxSupportedUsers() {
Amith Yamasaniff549202012-10-12 12:44:49 -07002931 // Don't allow multiple users on certain builds
2932 if (android.os.Build.ID.startsWith("JVP")) return 1;
Robin Lee83c79312018-02-09 19:27:18 +01002933 if (ActivityManager.isLowRamDeviceStatic()) {
2934 // Low-ram devices are Svelte. Most of the time they don't get multi-user.
2935 if ((Resources.getSystem().getConfiguration().uiMode & Configuration.UI_MODE_TYPE_MASK)
2936 != Configuration.UI_MODE_TYPE_TELEVISION) {
2937 return 1;
2938 }
2939 }
Jeff Sharkey27bd34d2012-09-16 12:49:00 -07002940 return SystemProperties.getInt("fw.max_users",
2941 Resources.getSystem().getInteger(R.integer.config_multiuserMaximumUsers));
Amith Yamasani258848d2012-08-10 17:06:33 -07002942 }
Amith Yamasani2a003292012-08-14 18:25:45 -07002943
2944 /**
Fyodor Kupolovcd86ebf2015-09-29 17:06:50 -07002945 * Returns true if the user switcher should be shown, this will be if device supports multi-user
2946 * and there are at least 2 users available that are not managed profiles.
Kenny Guy1a447532014-02-20 21:55:32 +00002947 * @hide
2948 * @return true if user switcher should be shown.
2949 */
2950 public boolean isUserSwitcherEnabled() {
Fyodor Kupolovcd86ebf2015-09-29 17:06:50 -07002951 if (!supportsMultipleUsers()) {
2952 return false;
2953 }
Benjamin Franzff66fa92017-08-10 10:39:44 +01002954 if (hasUserRestriction(DISALLOW_USER_SWITCH)) {
2955 return false;
2956 }
Amith Yamasanieb437d42016-04-29 09:31:25 -07002957 // If Demo Mode is on, don't show user switcher
2958 if (isDeviceInDemoMode(mContext)) {
2959 return false;
2960 }
Fan Zhangc69c8b62018-06-05 13:30:11 -07002961 // If user disabled this feature, don't show switcher
2962 final boolean userSwitcherEnabled = Settings.Global.getInt(mContext.getContentResolver(),
2963 Settings.Global.USER_SWITCHER_ENABLED, 1) != 0;
2964 if (!userSwitcherEnabled) {
2965 return false;
2966 }
Kenny Guy1a447532014-02-20 21:55:32 +00002967 List<UserInfo> users = getUsers(true);
2968 if (users == null) {
2969 return false;
2970 }
2971 int switchableUserCount = 0;
2972 for (UserInfo user : users) {
Xiaohui Chen7cb69df2015-07-13 16:01:01 -07002973 if (user.supportsSwitchToByUser()) {
Kenny Guy1a447532014-02-20 21:55:32 +00002974 ++switchableUserCount;
2975 }
2976 }
Fyodor Kupolovcd86ebf2015-09-29 17:06:50 -07002977 final boolean guestEnabled = !mContext.getSystemService(DevicePolicyManager.class)
2978 .getGuestUserDisabled(null);
Amith Yamasania596ff82014-06-12 18:12:38 -07002979 return switchableUserCount > 1 || guestEnabled;
Kenny Guy1a447532014-02-20 21:55:32 +00002980 }
2981
2982 /**
Amith Yamasanieb437d42016-04-29 09:31:25 -07002983 * @hide
2984 */
Andrei Onea24ec3212019-03-15 17:35:05 +00002985 @UnsupportedAppUsage
Amith Yamasanieb437d42016-04-29 09:31:25 -07002986 public static boolean isDeviceInDemoMode(Context context) {
2987 return Settings.Global.getInt(context.getContentResolver(),
2988 Settings.Global.DEVICE_DEMO_MODE, 0) > 0;
2989 }
2990
2991 /**
Amith Yamasani2a003292012-08-14 18:25:45 -07002992 * Returns a serial number on this device for a given userHandle. User handles can be recycled
2993 * when deleting and creating users, but serial numbers are not reused until the device is wiped.
2994 * @param userHandle
2995 * @return a serial number associated with that user, or -1 if the userHandle is not valid.
2996 * @hide
2997 */
Andrei Onea24ec3212019-03-15 17:35:05 +00002998 @UnsupportedAppUsage
Jeff Sharkey8588bc12016-01-06 16:47:42 -07002999 public int getUserSerialNumber(@UserIdInt int userHandle) {
Amith Yamasani2a003292012-08-14 18:25:45 -07003000 try {
3001 return mService.getUserSerialNumber(userHandle);
3002 } catch (RemoteException re) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07003003 throw re.rethrowFromSystemServer();
Amith Yamasani2a003292012-08-14 18:25:45 -07003004 }
Amith Yamasani2a003292012-08-14 18:25:45 -07003005 }
3006
3007 /**
3008 * Returns a userHandle on this device for a given user serial number. User handles can be
3009 * recycled when deleting and creating users, but serial numbers are not reused until the device
3010 * is wiped.
3011 * @param userSerialNumber
3012 * @return the userHandle associated with that user serial number, or -1 if the serial number
3013 * is not valid.
3014 * @hide
3015 */
Andrei Onea24ec3212019-03-15 17:35:05 +00003016 @UnsupportedAppUsage
Jeff Sharkey8588bc12016-01-06 16:47:42 -07003017 public @UserIdInt int getUserHandle(int userSerialNumber) {
Amith Yamasani2a003292012-08-14 18:25:45 -07003018 try {
3019 return mService.getUserHandle(userSerialNumber);
3020 } catch (RemoteException re) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07003021 throw re.rethrowFromSystemServer();
Amith Yamasani2a003292012-08-14 18:25:45 -07003022 }
Amith Yamasani2a003292012-08-14 18:25:45 -07003023 }
Maggie Benthall67944582013-02-22 14:58:27 -05003024
Amith Yamasani7e99bc02013-04-16 18:24:51 -07003025 /**
Esteban Talavera953fe482016-06-07 15:25:20 +01003026 * Returns a {@link Bundle} containing any saved application restrictions for this user, for the
Amith Yamasani7e99bc02013-04-16 18:24:51 -07003027 * given package name. Only an application with this package name can call this method.
Esteban Talavera5b9f1672015-12-11 15:22:34 +00003028 *
3029 * <p>The returned {@link Bundle} consists of key-value pairs, as defined by the application,
3030 * where the types of values may be:
3031 * <ul>
3032 * <li>{@code boolean}
3033 * <li>{@code int}
3034 * <li>{@code String} or {@code String[]}
3035 * <li>From {@link android.os.Build.VERSION_CODES#M}, {@code Bundle} or {@code Bundle[]}
3036 * </ul>
3037 *
Fyodor Kupolov4e9af062016-07-18 16:59:11 -07003038 * <p>NOTE: The method performs disk I/O and shouldn't be called on the main thread
3039 *
Amith Yamasani7e99bc02013-04-16 18:24:51 -07003040 * @param packageName the package name of the calling application
Esteban Talavera953fe482016-06-07 15:25:20 +01003041 * @return a {@link Bundle} with the restrictions for that package, or an empty {@link Bundle}
3042 * if there are no saved restrictions.
Esteban Talavera5b9f1672015-12-11 15:22:34 +00003043 *
3044 * @see #KEY_RESTRICTIONS_PENDING
Amith Yamasani7e99bc02013-04-16 18:24:51 -07003045 */
Fyodor Kupolov4e9af062016-07-18 16:59:11 -07003046 @WorkerThread
Amith Yamasani7e99bc02013-04-16 18:24:51 -07003047 public Bundle getApplicationRestrictions(String packageName) {
3048 try {
3049 return mService.getApplicationRestrictions(packageName);
3050 } catch (RemoteException re) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07003051 throw re.rethrowFromSystemServer();
Amith Yamasani7e99bc02013-04-16 18:24:51 -07003052 }
Amith Yamasani7e99bc02013-04-16 18:24:51 -07003053 }
Amith Yamasanidf2e92a2013-03-01 17:04:38 -08003054
3055 /**
3056 * @hide
3057 */
Fyodor Kupolov4e9af062016-07-18 16:59:11 -07003058 @WorkerThread
Amith Yamasani7e99bc02013-04-16 18:24:51 -07003059 public Bundle getApplicationRestrictions(String packageName, UserHandle user) {
Amith Yamasanidf2e92a2013-03-01 17:04:38 -08003060 try {
Amith Yamasani7e99bc02013-04-16 18:24:51 -07003061 return mService.getApplicationRestrictionsForUser(packageName, user.getIdentifier());
Amith Yamasanidf2e92a2013-03-01 17:04:38 -08003062 } catch (RemoteException re) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07003063 throw re.rethrowFromSystemServer();
Amith Yamasanidf2e92a2013-03-01 17:04:38 -08003064 }
Amith Yamasanidf2e92a2013-03-01 17:04:38 -08003065 }
3066
3067 /**
3068 * @hide
3069 */
Fyodor Kupolov4e9af062016-07-18 16:59:11 -07003070 @WorkerThread
Amith Yamasani7e99bc02013-04-16 18:24:51 -07003071 public void setApplicationRestrictions(String packageName, Bundle restrictions,
Amith Yamasanidf2e92a2013-03-01 17:04:38 -08003072 UserHandle user) {
3073 try {
Amith Yamasani7e99bc02013-04-16 18:24:51 -07003074 mService.setApplicationRestrictions(packageName, restrictions, user.getIdentifier());
Amith Yamasanidf2e92a2013-03-01 17:04:38 -08003075 } catch (RemoteException re) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07003076 throw re.rethrowFromSystemServer();
Amith Yamasanidf2e92a2013-03-01 17:04:38 -08003077 }
3078 }
Amith Yamasani655d0e22013-06-12 14:19:10 -07003079
3080 /**
Amith Yamasanid304af62013-09-05 09:30:23 -07003081 * Sets a new challenge PIN for restrictions. This is only for use by pre-installed
3082 * apps and requires the MANAGE_USERS permission.
3083 * @param newPin the PIN to use for challenge dialogs.
3084 * @return Returns true if the challenge PIN was set successfully.
Fyodor Kupolovef249092015-05-06 13:18:46 -07003085 * @deprecated The restrictions PIN functionality is no longer provided by the system.
3086 * This method is preserved for backwards compatibility reasons and always returns false.
Amith Yamasani655d0e22013-06-12 14:19:10 -07003087 */
Aurimas Liutikas514c5ef2016-05-24 15:22:55 -07003088 @Deprecated
Amith Yamasanid304af62013-09-05 09:30:23 -07003089 public boolean setRestrictionsChallenge(String newPin) {
Amith Yamasani655d0e22013-06-12 14:19:10 -07003090 return false;
3091 }
Amith Yamasani1a7472e2013-07-02 11:17:30 -07003092
Amith Yamasanie4afaa32014-06-30 14:55:07 +05303093 /**
3094 * @hide
3095 * Set restrictions that should apply to any future guest user that's created.
3096 */
3097 public void setDefaultGuestRestrictions(Bundle restrictions) {
3098 try {
3099 mService.setDefaultGuestRestrictions(restrictions);
3100 } catch (RemoteException re) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07003101 throw re.rethrowFromSystemServer();
Amith Yamasanie4afaa32014-06-30 14:55:07 +05303102 }
3103 }
3104
3105 /**
3106 * @hide
3107 * Gets the default guest restrictions.
3108 */
3109 public Bundle getDefaultGuestRestrictions() {
3110 try {
3111 return mService.getDefaultGuestRestrictions();
3112 } catch (RemoteException re) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07003113 throw re.rethrowFromSystemServer();
Amith Yamasanie4afaa32014-06-30 14:55:07 +05303114 }
Amith Yamasanie4afaa32014-06-30 14:55:07 +05303115 }
Fyodor Kupolovff7233e2015-04-08 11:28:52 -07003116
3117 /**
3118 * Returns creation time of the user or of a managed profile associated with the calling user.
3119 * @param userHandle user handle of the user or a managed profile associated with the
3120 * calling user.
3121 * @return creation time in milliseconds since Epoch time.
3122 */
Fyodor Kupolov385de622015-04-10 18:00:19 -07003123 public long getUserCreationTime(UserHandle userHandle) {
Fyodor Kupolovff7233e2015-04-08 11:28:52 -07003124 try {
Fyodor Kupolov385de622015-04-10 18:00:19 -07003125 return mService.getUserCreationTime(userHandle.getIdentifier());
Fyodor Kupolovff7233e2015-04-08 11:28:52 -07003126 } catch (RemoteException re) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07003127 throw re.rethrowFromSystemServer();
Fyodor Kupolovff7233e2015-04-08 11:28:52 -07003128 }
3129 }
Amith Yamasani12747872015-12-07 14:19:49 -08003130
3131 /**
3132 * @hide
3133 * Checks if any uninitialized user has the specific seed account name and type.
3134 *
Pavel Grafov6a40f092016-10-25 15:46:51 +01003135 * @param accountName The account name to check for
3136 * @param accountType The account type of the account to check for
Amith Yamasani12747872015-12-07 14:19:49 -08003137 * @return whether the seed account was found
3138 */
3139 public boolean someUserHasSeedAccount(String accountName, String accountType) {
3140 try {
3141 return mService.someUserHasSeedAccount(accountName, accountType);
3142 } catch (RemoteException re) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07003143 throw re.rethrowFromSystemServer();
Amith Yamasani12747872015-12-07 14:19:49 -08003144 }
3145 }
Pavel Grafov6a40f092016-10-25 15:46:51 +01003146
3147 /**
3148 * @hide
3149 * User that enforces a restriction.
3150 *
3151 * @see #getUserRestrictionSources(String, UserHandle)
3152 */
3153 @SystemApi
3154 public static final class EnforcingUser implements Parcelable {
3155 private final @UserIdInt int userId;
3156 private final @UserRestrictionSource int userRestrictionSource;
3157
3158 /**
3159 * @hide
3160 */
3161 public EnforcingUser(
3162 @UserIdInt int userId, @UserRestrictionSource int userRestrictionSource) {
3163 this.userId = userId;
3164 this.userRestrictionSource = userRestrictionSource;
3165 }
3166
3167 private EnforcingUser(Parcel in) {
3168 userId = in.readInt();
3169 userRestrictionSource = in.readInt();
3170 }
3171
Jeff Sharkey9e8f83d2019-02-28 12:06:45 -07003172 public static final @android.annotation.NonNull Creator<EnforcingUser> CREATOR = new Creator<EnforcingUser>() {
Pavel Grafov6a40f092016-10-25 15:46:51 +01003173 @Override
3174 public EnforcingUser createFromParcel(Parcel in) {
3175 return new EnforcingUser(in);
3176 }
3177
3178 @Override
3179 public EnforcingUser[] newArray(int size) {
3180 return new EnforcingUser[size];
3181 }
3182 };
3183
3184 @Override
3185 public int describeContents() {
3186 return 0;
3187 }
3188
3189 @Override
3190 public void writeToParcel(Parcel dest, int flags) {
3191 dest.writeInt(userId);
3192 dest.writeInt(userRestrictionSource);
3193 }
3194
3195 /**
3196 * Returns an id of the enforcing user.
3197 *
3198 * <p> Will be UserHandle.USER_NULL when restriction is set by the system.
3199 */
3200 public UserHandle getUserHandle() {
3201 return UserHandle.of(userId);
3202 }
3203
3204 /**
3205 * Returns the status of the enforcing user.
3206 *
3207 * <p> One of {@link #RESTRICTION_SOURCE_SYSTEM},
3208 * {@link #RESTRICTION_SOURCE_DEVICE_OWNER} and
3209 * {@link #RESTRICTION_SOURCE_PROFILE_OWNER}
3210 */
3211 public @UserRestrictionSource int getUserRestrictionSource() {
3212 return userRestrictionSource;
3213 }
3214 }
Amith Yamasani258848d2012-08-10 17:06:33 -07003215}