blob: 7b0c153f52341b67a24031ad0a55ab69f3209795 [file] [log] [blame]
Amith Yamasani258848d2012-08-10 17:06:33 -07001/*
2 * Copyright (C) 2012 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
Jeff Sharkey8a372a02016-03-16 16:25:45 -060016
Amith Yamasani258848d2012-08-10 17:06:33 -070017package android.os;
18
Xiaohui Chenb3b92582015-12-07 11:22:13 -080019import android.Manifest;
Fyodor Kupolov02cb6e72015-09-18 18:20:55 -070020import android.accounts.AccountManager;
Zoltan Szatmary-Bane7834602016-04-08 18:41:11 +010021import android.annotation.IntDef;
Adam Lesinskiada8deb2017-05-12 13:50:42 -070022import android.annotation.NonNull;
Xiaohui Chen7cb69df2015-07-13 16:01:01 -070023import android.annotation.Nullable;
Xiaohui Chenb3b92582015-12-07 11:22:13 -080024import android.annotation.RequiresPermission;
Amith Yamasani0e8d7d62014-09-03 13:17:28 -070025import android.annotation.SystemApi;
Jeff Sharkeyd86b8fe2017-06-02 17:36:26 -060026import android.annotation.SystemService;
Makoto Onukie72f81b2017-03-16 14:08:19 -070027import android.annotation.TestApi;
Jeff Sharkey8588bc12016-01-06 16:47:42 -070028import android.annotation.UserIdInt;
Fyodor Kupolov4e9af062016-07-18 16:59:11 -070029import android.annotation.WorkerThread;
Amith Yamasani37ed8d12016-01-27 14:40:16 -080030import android.app.Activity;
Dianne Hackborn409297d2014-07-10 17:39:20 -070031import android.app.ActivityManager;
Fyodor Kupolovcd86ebf2015-09-29 17:06:50 -070032import android.app.admin.DevicePolicyManager;
Makoto Onuki068c54a2015-10-13 14:34:03 -070033import android.content.ComponentName;
Amith Yamasani258848d2012-08-10 17:06:33 -070034import android.content.Context;
Amith Yamasani12747872015-12-07 14:19:49 -080035import android.content.Intent;
Esteban Talavera8bd7c522017-02-13 12:35:04 +000036import android.content.IntentFilter;
Benjamin Franzf02420c2016-04-04 18:52:21 +010037import android.content.IntentSender;
Amith Yamasani258848d2012-08-10 17:06:33 -070038import android.content.pm.UserInfo;
Jeff Sharkey27bd34d2012-09-16 12:49:00 -070039import android.content.res.Resources;
Maggie Benthall67944582013-02-22 14:58:27 -050040import android.graphics.Bitmap;
Adrian Roos1bdff912015-02-17 15:51:35 +010041import android.graphics.BitmapFactory;
Amith Yamasani4f582632014-02-19 14:31:52 -080042import android.graphics.Rect;
Amith Yamasani4f582632014-02-19 14:31:52 -080043import android.graphics.drawable.Drawable;
Amith Yamasani1e9c2182014-06-11 17:25:51 -070044import android.provider.Settings;
Fyodor Kupolov523c4042016-02-24 15:03:13 -080045import android.telephony.TelephonyManager;
Jason Monk1c7c3192014-06-26 12:52:18 -040046import android.view.WindowManager.LayoutParams;
Amith Yamasani258848d2012-08-10 17:06:33 -070047
Maggie Benthall67944582013-02-22 14:58:27 -050048import com.android.internal.R;
John Reckaa67f682016-09-20 14:24:21 -070049import com.android.internal.os.RoSystemProperties;
Maggie Benthall67944582013-02-22 14:58:27 -050050
Adrian Roos1bdff912015-02-17 15:51:35 +010051import java.io.IOException;
Zoltan Szatmary-Bane7834602016-04-08 18:41:11 +010052import java.lang.annotation.Retention;
53import java.lang.annotation.RetentionPolicy;
Amith Yamasani4f582632014-02-19 14:31:52 -080054import java.util.ArrayList;
Amith Yamasani258848d2012-08-10 17:06:33 -070055import java.util.List;
56
57/**
Amith Yamasani06964342016-04-15 13:55:01 -070058 * Manages users and user details on a multi-user system. There are two major categories of
59 * users: fully customizable users with their own login, and managed profiles that share a workspace
60 * with a related user.
61 * <p>
62 * Users are different from accounts, which are managed by
63 * {@link AccountManager}. Each user can have their own set of accounts.
64 * <p>
65 * See {@link DevicePolicyManager#ACTION_PROVISION_MANAGED_PROFILE} for more on managed profiles.
Amith Yamasani258848d2012-08-10 17:06:33 -070066 */
Jeff Sharkeyd86b8fe2017-06-02 17:36:26 -060067@SystemService(Context.USER_SERVICE)
Amith Yamasani258848d2012-08-10 17:06:33 -070068public class UserManager {
69
Pavel Grafov4f4f6f82017-03-28 13:44:04 +010070 private static final String TAG = "UserManager";
Amith Yamasani258848d2012-08-10 17:06:33 -070071 private final IUserManager mService;
72 private final Context mContext;
73
Makoto Onukid49f3fa2017-01-25 14:09:48 -080074 private Boolean mIsManagedProfileCached;
75
Amith Yamasanie4cf7342012-12-17 11:12:09 -080076 /**
Zoltan Szatmary-Bane7834602016-04-08 18:41:11 +010077 * @hide
78 * No user restriction.
79 */
80 @SystemApi
81 public static final int RESTRICTION_NOT_SET = 0x0;
82
83 /**
84 * @hide
85 * User restriction set by system/user.
86 */
87 @SystemApi
88 public static final int RESTRICTION_SOURCE_SYSTEM = 0x1;
89
90 /**
91 * @hide
92 * User restriction set by a device owner.
93 */
94 @SystemApi
95 public static final int RESTRICTION_SOURCE_DEVICE_OWNER = 0x2;
96
97 /**
98 * @hide
99 * User restriction set by a profile owner.
100 */
101 @SystemApi
102 public static final int RESTRICTION_SOURCE_PROFILE_OWNER = 0x4;
103
104 /** @hide */
105 @Retention(RetentionPolicy.SOURCE)
Jeff Sharkeyce8db992017-12-13 20:05:05 -0700106 @IntDef(flag = true, prefix = { "RESTRICTION_" }, value = {
107 RESTRICTION_NOT_SET,
108 RESTRICTION_SOURCE_SYSTEM,
109 RESTRICTION_SOURCE_DEVICE_OWNER,
110 RESTRICTION_SOURCE_PROFILE_OWNER
111 })
Zoltan Szatmary-Bane7834602016-04-08 18:41:11 +0100112 @SystemApi
113 public @interface UserRestrictionSource {}
114
115 /**
Fyodor Kupolov53019282015-07-21 11:48:18 -0700116 * Specifies if a user is disallowed from adding and removing accounts, unless they are
117 * {@link android.accounts.AccountManager#addAccountExplicitly programmatically} added by
118 * Authenticator.
Amith Yamasani71e6c692013-03-24 17:39:28 -0700119 * The default value is <code>false</code>.
Amith Yamasani26af8292014-09-09 09:57:27 -0700120 *
Benjamin Franzb6c0ce42015-11-05 10:06:51 +0000121 * <p>From {@link android.os.Build.VERSION_CODES#N} a profile or device owner app can still
122 * use {@link android.accounts.AccountManager} APIs to add or remove accounts when account
123 * management is disallowed.
124 *
Fyodor Kupolov0ffbfea2016-01-19 10:07:54 -0800125 * <p>Key for user restrictions.
126 * <p>Type: Boolean
Makoto Onuki068c54a2015-10-13 14:34:03 -0700127 * @see DevicePolicyManager#addUserRestriction(ComponentName, String)
128 * @see DevicePolicyManager#clearUserRestriction(ComponentName, String)
Amith Yamasanie4cf7342012-12-17 11:12:09 -0800129 * @see #getUserRestrictions()
130 */
Amith Yamasani71e6c692013-03-24 17:39:28 -0700131 public static final String DISALLOW_MODIFY_ACCOUNTS = "no_modify_accounts";
Amith Yamasanie4cf7342012-12-17 11:12:09 -0800132
133 /**
Amith Yamasani26af8292014-09-09 09:57:27 -0700134 * Specifies if a user is disallowed from changing Wi-Fi
Julia Reynolds2cb384f2014-08-13 15:15:55 -0400135 * access points. The default value is <code>false</code>.
Fyodor Kupolov0ffbfea2016-01-19 10:07:54 -0800136 * <p>This restriction has no effect in a managed profile.
Amith Yamasani26af8292014-09-09 09:57:27 -0700137 *
Fyodor Kupolov0ffbfea2016-01-19 10:07:54 -0800138 * <p>Key for user restrictions.
139 * <p>Type: Boolean
Makoto Onuki068c54a2015-10-13 14:34:03 -0700140 * @see DevicePolicyManager#addUserRestriction(ComponentName, String)
141 * @see DevicePolicyManager#clearUserRestriction(ComponentName, String)
Amith Yamasanie4cf7342012-12-17 11:12:09 -0800142 * @see #getUserRestrictions()
143 */
Amith Yamasani71e6c692013-03-24 17:39:28 -0700144 public static final String DISALLOW_CONFIG_WIFI = "no_config_wifi";
Amith Yamasanie4cf7342012-12-17 11:12:09 -0800145
146 /**
Christine Franks1bade5d2017-10-10 15:41:50 -0700147 * Specifies if a user is disallowed from changing the device
148 * language. The default value is <code>false</code>.
149 *
150 * <p>Key for user restrictions.
151 * <p>Type: Boolean
152 * @see DevicePolicyManager#addUserRestriction(ComponentName, String)
153 * @see DevicePolicyManager#clearUserRestriction(ComponentName, String)
154 * @see #getUserRestrictions()
155 */
156 public static final String DISALLOW_CONFIG_LOCALE = "no_config_locale";
157
158 /**
Amith Yamasani26af8292014-09-09 09:57:27 -0700159 * Specifies if a user is disallowed from installing applications.
Amith Yamasani71e6c692013-03-24 17:39:28 -0700160 * The default value is <code>false</code>.
Amith Yamasani26af8292014-09-09 09:57:27 -0700161 *
Fyodor Kupolov0ffbfea2016-01-19 10:07:54 -0800162 * <p>Key for user restrictions.
163 * <p>Type: Boolean
Makoto Onuki068c54a2015-10-13 14:34:03 -0700164 * @see DevicePolicyManager#addUserRestriction(ComponentName, String)
165 * @see DevicePolicyManager#clearUserRestriction(ComponentName, String)
Amith Yamasanie4cf7342012-12-17 11:12:09 -0800166 * @see #getUserRestrictions()
167 */
Amith Yamasani71e6c692013-03-24 17:39:28 -0700168 public static final String DISALLOW_INSTALL_APPS = "no_install_apps";
Amith Yamasanie4cf7342012-12-17 11:12:09 -0800169
170 /**
Amith Yamasani26af8292014-09-09 09:57:27 -0700171 * Specifies if a user is disallowed from uninstalling applications.
Amith Yamasani71e6c692013-03-24 17:39:28 -0700172 * The default value is <code>false</code>.
Amith Yamasani26af8292014-09-09 09:57:27 -0700173 *
Fyodor Kupolov0ffbfea2016-01-19 10:07:54 -0800174 * <p>Key for user restrictions.
175 * <p>Type: Boolean
Makoto Onuki068c54a2015-10-13 14:34:03 -0700176 * @see DevicePolicyManager#addUserRestriction(ComponentName, String)
177 * @see DevicePolicyManager#clearUserRestriction(ComponentName, String)
Amith Yamasanie4cf7342012-12-17 11:12:09 -0800178 * @see #getUserRestrictions()
179 */
Amith Yamasani71e6c692013-03-24 17:39:28 -0700180 public static final String DISALLOW_UNINSTALL_APPS = "no_uninstall_apps";
Amith Yamasanie4cf7342012-12-17 11:12:09 -0800181
Amith Yamasani71e6c692013-03-24 17:39:28 -0700182 /**
Amith Yamasani150514b2015-01-07 16:05:05 -0800183 * Specifies if a user is disallowed from turning on location sharing.
Amith Yamasani71e6c692013-03-24 17:39:28 -0700184 * The default value is <code>false</code>.
Fyodor Kupolov0ffbfea2016-01-19 10:07:54 -0800185 * <p>In a managed profile, location sharing always reflects the primary user's setting, but
Amith Yamasani150514b2015-01-07 16:05:05 -0800186 * can be overridden and forced off by setting this restriction to true in the managed profile.
Amith Yamasani26af8292014-09-09 09:57:27 -0700187 *
Fyodor Kupolov0ffbfea2016-01-19 10:07:54 -0800188 * <p>Key for user restrictions.
189 * <p>Type: Boolean
Makoto Onuki068c54a2015-10-13 14:34:03 -0700190 * @see DevicePolicyManager#addUserRestriction(ComponentName, String)
191 * @see DevicePolicyManager#clearUserRestriction(ComponentName, String)
Maggie Benthall67944582013-02-22 14:58:27 -0500192 * @see #getUserRestrictions()
193 */
Amith Yamasani71e6c692013-03-24 17:39:28 -0700194 public static final String DISALLOW_SHARE_LOCATION = "no_share_location";
Maggie Benthall67944582013-02-22 14:58:27 -0500195
Maggie Benthalla12fccf2013-03-14 18:02:12 -0400196 /**
yuemingw5fe75dc2017-11-29 15:52:56 +0000197 * Specifies if airplane mode is disallowed on the device.
198 *
199 * <p> This restriction can only be set by the device owner and the profile owner on the
200 * primary user and it applies globally - i.e. it disables airplane mode on the entire device.
201 * <p>The default value is <code>false</code>.
202 *
203 * <p>Key for user restrictions.
204 * <p>Type: Boolean
205 * @see DevicePolicyManager#addUserRestriction(ComponentName, String)
206 * @see DevicePolicyManager#clearUserRestriction(ComponentName, String)
207 * @see #getUserRestrictions()
208 */
209 public static final String DISALLOW_AIRPLANE_MODE = "no_airplane_mode";
210
211 /**
yuemingwc6ac29d2018-01-10 16:54:08 +0000212 * Specifies if a user is disallowed from configuring brightness. When device owner sets it,
213 * it'll only be applied on the target(system) user.
214 *
215 * <p>The default value is <code>false</code>.
216 *
217 * <p>This user restriction has no effect on managed profiles.
218 * <p>Key for user restrictions.
219 * <p>Type: Boolean
220 * @see DevicePolicyManager#addUserRestriction(ComponentName, String)
221 * @see DevicePolicyManager#clearUserRestriction(ComponentName, String)
222 * @see #getUserRestrictions()
223 */
224 public static final String DISALLOW_CONFIG_BRIGHTNESS = "no_config_brightness";
225
226 /**
yuemingwa1781442018-01-10 18:59:43 +0000227 * Specifies if ambient display is disallowed for the user.
228 *
229 * <p>The default value is <code>false</code>.
230 *
231 * <p>This user restriction has no effect on managed profiles.
232 * <p>Key for user restrictions.
233 * <p>Type: Boolean
234 * @see DevicePolicyManager#addUserRestriction(ComponentName, String)
235 * @see DevicePolicyManager#clearUserRestriction(ComponentName, String)
236 * @see #getUserRestrictions()
237 */
238 public static final String DISALLOW_AMBIENT_DISPLAY = "no_ambient_display";
239
240 /**
yuemingw5cda3ae2018-01-15 10:26:19 +0000241 * Specifies if a user is disallowed from changing screen off timeout.
242 *
243 * <p>The default value is <code>false</code>.
244 *
245 * <p>This user restriction has no effect on managed profiles.
246 * <p>Key for user restrictions.
247 * <p>Type: Boolean
248 * @see DevicePolicyManager#addUserRestriction(ComponentName, String)
249 * @see DevicePolicyManager#clearUserRestriction(ComponentName, String)
250 * @see #getUserRestrictions()
251 */
252 public static final String DISALLOW_CONFIG_SCREEN_TIMEOUT = "no_config_screen_timeout";
253
254 /**
Amith Yamasani26af8292014-09-09 09:57:27 -0700255 * Specifies if a user is disallowed from enabling the
Maggie Benthalla12fccf2013-03-14 18:02:12 -0400256 * "Unknown Sources" setting, that allows installation of apps from unknown sources.
257 * The default value is <code>false</code>.
Amith Yamasani26af8292014-09-09 09:57:27 -0700258 *
Fyodor Kupolov0ffbfea2016-01-19 10:07:54 -0800259 * <p>Key for user restrictions.
260 * <p>Type: Boolean
Makoto Onuki068c54a2015-10-13 14:34:03 -0700261 * @see DevicePolicyManager#addUserRestriction(ComponentName, String)
262 * @see DevicePolicyManager#clearUserRestriction(ComponentName, String)
Maggie Benthalla12fccf2013-03-14 18:02:12 -0400263 * @see #getUserRestrictions()
264 */
265 public static final String DISALLOW_INSTALL_UNKNOWN_SOURCES = "no_install_unknown_sources";
266
267 /**
Amith Yamasani26af8292014-09-09 09:57:27 -0700268 * Specifies if a user is disallowed from configuring bluetooth.
Nicolas Prevot1c4c4422015-02-16 11:32:21 +0000269 * This does <em>not</em> restrict the user from turning bluetooth on or off.
Maggie Benthalla12fccf2013-03-14 18:02:12 -0400270 * The default value is <code>false</code>.
Lenka Trochtova63d5e4a72016-12-02 12:19:39 +0100271 * <p>This restriction doesn't prevent the user from using bluetooth. For disallowing usage of
272 * bluetooth completely on the device, use {@link #DISALLOW_BLUETOOTH}.
Fyodor Kupolov0ffbfea2016-01-19 10:07:54 -0800273 * <p>This restriction has no effect in a managed profile.
Amith Yamasani26af8292014-09-09 09:57:27 -0700274 *
Fyodor Kupolov0ffbfea2016-01-19 10:07:54 -0800275 * <p>Key for user restrictions.
276 * <p>Type: Boolean
Makoto Onuki068c54a2015-10-13 14:34:03 -0700277 * @see DevicePolicyManager#addUserRestriction(ComponentName, String)
278 * @see DevicePolicyManager#clearUserRestriction(ComponentName, String)
Maggie Benthalla12fccf2013-03-14 18:02:12 -0400279 * @see #getUserRestrictions()
280 */
281 public static final String DISALLOW_CONFIG_BLUETOOTH = "no_config_bluetooth";
282
Maggie Benthalla12fccf2013-03-14 18:02:12 -0400283 /**
Lenka Trochtova63d5e4a72016-12-02 12:19:39 +0100284 * Specifies if bluetooth is disallowed on the device.
285 *
286 * <p> This restriction can only be set by the device owner and the profile owner on the
287 * primary user and it applies globally - i.e. it disables bluetooth on the entire device.
288 * <p>The default value is <code>false</code>.
289 * <p>Key for user restrictions.
290 * <p>Type: Boolean
291 * @see DevicePolicyManager#addUserRestriction(ComponentName, String)
292 * @see DevicePolicyManager#clearUserRestriction(ComponentName, String)
293 * @see #getUserRestrictions()
294 */
295 public static final String DISALLOW_BLUETOOTH = "no_bluetooth";
296
297 /**
Pavel Grafov4f4f6f82017-03-28 13:44:04 +0100298 * Specifies if outgoing bluetooth sharing is disallowed on the device. Device owner and profile
299 * owner can set this restriction. When it is set by device owner, all users on this device will
300 * be affected.
301 *
302 * <p>Default is <code>true</code> for managed profiles and false for otherwise. When a device
303 * upgrades to {@link android.os.Build.VERSION_CODES#O}, the system sets it for all existing
304 * managed profiles.
305 *
306 * <p>Key for user restrictions.
307 * <p>Type: Boolean
308 * @see DevicePolicyManager#addUserRestriction(ComponentName, String)
309 * @see DevicePolicyManager#clearUserRestriction(ComponentName, String)
310 * @see #getUserRestrictions()
311 */
312 public static final String DISALLOW_BLUETOOTH_SHARING = "no_bluetooth_sharing";
313
314 /**
Amith Yamasani26af8292014-09-09 09:57:27 -0700315 * Specifies if a user is disallowed from transferring files over
Amith Yamasanic34dc7c2014-09-18 09:42:42 -0700316 * USB. This can only be set by device owners and profile owners on the primary user.
317 * The default value is <code>false</code>.
Amith Yamasani26af8292014-09-09 09:57:27 -0700318 *
Fyodor Kupolov0ffbfea2016-01-19 10:07:54 -0800319 * <p>Key for user restrictions.
320 * <p>Type: Boolean
Makoto Onuki068c54a2015-10-13 14:34:03 -0700321 * @see DevicePolicyManager#addUserRestriction(ComponentName, String)
322 * @see DevicePolicyManager#clearUserRestriction(ComponentName, String)
Maggie Benthalla12fccf2013-03-14 18:02:12 -0400323 * @see #getUserRestrictions()
324 */
325 public static final String DISALLOW_USB_FILE_TRANSFER = "no_usb_file_transfer";
326
Emily Bernierb223f732013-04-11 15:46:36 -0400327 /**
Amith Yamasani26af8292014-09-09 09:57:27 -0700328 * Specifies if a user is disallowed from configuring user
Emily Bernierb223f732013-04-11 15:46:36 -0400329 * credentials. The default value is <code>false</code>.
Amith Yamasani26af8292014-09-09 09:57:27 -0700330 *
Fyodor Kupolov0ffbfea2016-01-19 10:07:54 -0800331 * <p>Key for user restrictions.
332 * <p>Type: Boolean
Makoto Onuki068c54a2015-10-13 14:34:03 -0700333 * @see DevicePolicyManager#addUserRestriction(ComponentName, String)
334 * @see DevicePolicyManager#clearUserRestriction(ComponentName, String)
Emily Bernierb223f732013-04-11 15:46:36 -0400335 * @see #getUserRestrictions()
336 */
337 public static final String DISALLOW_CONFIG_CREDENTIALS = "no_config_credentials";
338
339 /**
Amith Yamasani150514b2015-01-07 16:05:05 -0800340 * When set on the primary user this specifies if the user can remove other users.
341 * When set on a secondary user, this specifies if the user can remove itself.
342 * This restriction has no effect on managed profiles.
343 * The default value is <code>false</code>.
Amith Yamasani26af8292014-09-09 09:57:27 -0700344 *
Fyodor Kupolov0ffbfea2016-01-19 10:07:54 -0800345 * <p>Key for user restrictions.
346 * <p>Type: Boolean
Makoto Onuki068c54a2015-10-13 14:34:03 -0700347 * @see DevicePolicyManager#addUserRestriction(ComponentName, String)
348 * @see DevicePolicyManager#clearUserRestriction(ComponentName, String)
Emily Bernierb223f732013-04-11 15:46:36 -0400349 * @see #getUserRestrictions()
350 */
351 public static final String DISALLOW_REMOVE_USER = "no_remove_user";
352
Julia Reynoldsd46d0f92014-04-23 15:23:24 -0400353 /**
Esteban Talavera6c9116a2016-11-24 16:12:44 +0000354 * Specifies if managed profiles of this user can be removed, other than by its profile owner.
355 * The default value is <code>false</code>.
356 * <p>
Nicolas Prevot2ea46fe2017-01-05 10:29:34 +0000357 * This restriction has no effect on managed profiles.
Esteban Talavera6c9116a2016-11-24 16:12:44 +0000358 *
359 * <p>Key for user restrictions.
360 * <p>Type: Boolean
361 * @see DevicePolicyManager#addUserRestriction(ComponentName, String)
362 * @see DevicePolicyManager#clearUserRestriction(ComponentName, String)
363 * @see #getUserRestrictions()
364 */
365 public static final String DISALLOW_REMOVE_MANAGED_PROFILE = "no_remove_managed_profile";
366
367 /**
Amith Yamasani26af8292014-09-09 09:57:27 -0700368 * Specifies if a user is disallowed from enabling or
Julia Reynoldsd46d0f92014-04-23 15:23:24 -0400369 * accessing debugging features. The default value is <code>false</code>.
Amith Yamasani26af8292014-09-09 09:57:27 -0700370 *
Fyodor Kupolov0ffbfea2016-01-19 10:07:54 -0800371 * <p>Key for user restrictions.
372 * <p>Type: Boolean
Makoto Onuki068c54a2015-10-13 14:34:03 -0700373 * @see DevicePolicyManager#addUserRestriction(ComponentName, String)
374 * @see DevicePolicyManager#clearUserRestriction(ComponentName, String)
Julia Reynoldsd46d0f92014-04-23 15:23:24 -0400375 * @see #getUserRestrictions()
376 */
377 public static final String DISALLOW_DEBUGGING_FEATURES = "no_debugging_features";
378
379 /**
Benjamin Miller05fef7e2017-07-24 10:11:39 +0200380 * Specifies if a user is disallowed from configuring a VPN. The default value is
381 * <code>false</code>. This restriction has an effect when set by device owners and, in Android
382 * 6.0 ({@linkplain android.os.Build.VERSION_CODES#M API level 23}) or higher, profile owners.
383 * <p>This restriction also prevents VPNs from starting. However, in Android 7.0
384 * ({@linkplain android.os.Build.VERSION_CODES#N API level 24}) or higher, the system does
385 * start always-on VPNs created by the device or profile owner.
Amith Yamasani26af8292014-09-09 09:57:27 -0700386 *
Fyodor Kupolov0ffbfea2016-01-19 10:07:54 -0800387 * <p>Key for user restrictions.
388 * <p>Type: Boolean
Makoto Onuki068c54a2015-10-13 14:34:03 -0700389 * @see DevicePolicyManager#addUserRestriction(ComponentName, String)
390 * @see DevicePolicyManager#clearUserRestriction(ComponentName, String)
Julia Reynoldsd46d0f92014-04-23 15:23:24 -0400391 * @see #getUserRestrictions()
392 */
393 public static final String DISALLOW_CONFIG_VPN = "no_config_vpn";
394
395 /**
yuemingw7cc2c4c2017-11-28 17:20:01 +0000396 * Specifies if a user is disallowed from configuring location mode. Device owner and profile
397 * owners can set this restriction and it only applies on the managed user.
398 *
399 * <p>In a managed profile, location sharing is forced off when it's off on primary user, so
400 * user can still turn off location sharing on managed profile when the restriction is set by
401 * profile owner on managed profile.
402 *
403 * <p>This user restriction is different from {@link #DISALLOW_SHARE_LOCATION},
404 * as the device owner or profile owner can still enable or disable location mode via
405 * {@link DevicePolicyManager#setSecureSetting} when this restriction is on.
406 *
407 * <p>The default value is <code>false</code>.
408 *
409 * <p>Key for user restrictions.
410 * <p>Type: Boolean
411 * @see DevicePolicyManager#addUserRestriction(ComponentName, String)
412 * @see DevicePolicyManager#clearUserRestriction(ComponentName, String)
413 * @see #getUserRestrictions()
414 */
415 public static final String DISALLOW_CONFIG_LOCATION_MODE = "no_config_location_mode";
416
417 /**
yuemingwa9772f362017-10-23 18:34:35 +0100418 * Specifies if date, time and timezone configuring is disallowed.
419 *
420 * <p>When restriction is set by device owners, it applies globally - i.e., it disables date,
421 * time and timezone setting on the entire device and all users will be affected. When it's set
422 * by profile owners, it's only applied to the managed user.
423 * <p>The default value is <code>false</code>.
424 *
425 * <p>This user restriction has no effect on managed profiles.
426 * <p>Key for user restrictions.
427 * <p>Type: Boolean
428 * @see DevicePolicyManager#addUserRestriction(ComponentName, String)
429 * @see DevicePolicyManager#clearUserRestriction(ComponentName, String)
430 * @see #getUserRestrictions()
431 */
432 public static final String DISALLOW_CONFIG_DATE_TIME = "no_config_date_time";
433
434 /**
Amith Yamasani26af8292014-09-09 09:57:27 -0700435 * Specifies if a user is disallowed from configuring Tethering
Amith Yamasanic34dc7c2014-09-18 09:42:42 -0700436 * & portable hotspots. This can only be set by device owners and profile owners on the
437 * primary user. The default value is <code>false</code>.
Rubin Xu1faf1442017-08-23 15:48:12 +0100438 * <p>In Android 9.0 or higher, if tethering is enabled when this restriction is set,
439 * tethering will be automatically turned off.
Amith Yamasani26af8292014-09-09 09:57:27 -0700440 *
Fyodor Kupolov0ffbfea2016-01-19 10:07:54 -0800441 * <p>Key for user restrictions.
442 * <p>Type: Boolean
Makoto Onuki068c54a2015-10-13 14:34:03 -0700443 * @see DevicePolicyManager#addUserRestriction(ComponentName, String)
444 * @see DevicePolicyManager#clearUserRestriction(ComponentName, String)
Julia Reynoldsd46d0f92014-04-23 15:23:24 -0400445 * @see #getUserRestrictions()
446 */
447 public static final String DISALLOW_CONFIG_TETHERING = "no_config_tethering";
448
449 /**
Stuart Scotte3e314d2015-04-20 14:07:45 -0700450 * Specifies if a user is disallowed from resetting network settings
451 * from Settings. This can only be set by device owners and profile owners on the primary user.
452 * The default value is <code>false</code>.
Fyodor Kupolov0ffbfea2016-01-19 10:07:54 -0800453 * <p>This restriction has no effect on secondary users and managed profiles since only the
Stuart Scotte3e314d2015-04-20 14:07:45 -0700454 * primary user can reset the network settings of the device.
455 *
Fyodor Kupolov0ffbfea2016-01-19 10:07:54 -0800456 * <p>Key for user restrictions.
457 * <p>Type: Boolean
Makoto Onuki068c54a2015-10-13 14:34:03 -0700458 * @see DevicePolicyManager#addUserRestriction(ComponentName, String)
459 * @see DevicePolicyManager#clearUserRestriction(ComponentName, String)
Stuart Scotte3e314d2015-04-20 14:07:45 -0700460 * @see #getUserRestrictions()
461 */
462 public static final String DISALLOW_NETWORK_RESET = "no_network_reset";
463
464 /**
Amith Yamasani26af8292014-09-09 09:57:27 -0700465 * Specifies if a user is disallowed from factory resetting
Amith Yamasanic34dc7c2014-09-18 09:42:42 -0700466 * from Settings. This can only be set by device owners and profile owners on the primary user.
467 * The default value is <code>false</code>.
Fyodor Kupolov0ffbfea2016-01-19 10:07:54 -0800468 * <p>This restriction has no effect on secondary users and managed profiles since only the
Amith Yamasani150514b2015-01-07 16:05:05 -0800469 * primary user can factory reset the device.
Amith Yamasani26af8292014-09-09 09:57:27 -0700470 *
Fyodor Kupolov0ffbfea2016-01-19 10:07:54 -0800471 * <p>Key for user restrictions.
472 * <p>Type: Boolean
Makoto Onuki068c54a2015-10-13 14:34:03 -0700473 * @see DevicePolicyManager#addUserRestriction(ComponentName, String)
474 * @see DevicePolicyManager#clearUserRestriction(ComponentName, String)
Julia Reynoldsd46d0f92014-04-23 15:23:24 -0400475 * @see #getUserRestrictions()
476 */
477 public static final String DISALLOW_FACTORY_RESET = "no_factory_reset";
478
479 /**
Esteban Talavera6c9116a2016-11-24 16:12:44 +0000480 * Specifies if a user is disallowed from adding new users. This can only be set by device
481 * owners and profile owners on the primary user.
Amith Yamasanic34dc7c2014-09-18 09:42:42 -0700482 * The default value is <code>false</code>.
Fyodor Kupolov0ffbfea2016-01-19 10:07:54 -0800483 * <p>This restriction has no effect on secondary users and managed profiles since only the
Amith Yamasani150514b2015-01-07 16:05:05 -0800484 * primary user can add other users.
Amith Yamasani26af8292014-09-09 09:57:27 -0700485 *
Fyodor Kupolov0ffbfea2016-01-19 10:07:54 -0800486 * <p>Key for user restrictions.
487 * <p>Type: Boolean
Makoto Onuki068c54a2015-10-13 14:34:03 -0700488 * @see DevicePolicyManager#addUserRestriction(ComponentName, String)
489 * @see DevicePolicyManager#clearUserRestriction(ComponentName, String)
Julia Reynoldsd46d0f92014-04-23 15:23:24 -0400490 * @see #getUserRestrictions()
491 */
492 public static final String DISALLOW_ADD_USER = "no_add_user";
493
494 /**
Esteban Talavera6c9116a2016-11-24 16:12:44 +0000495 * Specifies if a user is disallowed from adding managed profiles.
496 * <p>The default value for an unmanaged user is <code>false</code>.
Nicolas Prevot2ea46fe2017-01-05 10:29:34 +0000497 * For users with a device owner set, the default is <code>true</code>.
498 * <p>This restriction has no effect on managed profiles.
Esteban Talavera6c9116a2016-11-24 16:12:44 +0000499 *
500 * <p>Key for user restrictions.
501 * <p>Type: Boolean
502 * @see DevicePolicyManager#addUserRestriction(ComponentName, String)
503 * @see DevicePolicyManager#clearUserRestriction(ComponentName, String)
504 * @see #getUserRestrictions()
505 */
506 public static final String DISALLOW_ADD_MANAGED_PROFILE = "no_add_managed_profile";
507
508 /**
Benjamin Millerd41a9fc2017-07-17 17:24:44 +0200509 * Specifies if a user is disallowed from disabling application verification. The default
510 * value is <code>false</code>.
511 *
512 * <p>In Android 8.0 ({@linkplain android.os.Build.VERSION_CODES#O API level 26}) and higher,
513 * this is a global user restriction. If a device owner or profile owner sets this restriction,
514 * the system enforces app verification across all users on the device. Running in earlier
515 * Android versions, this restriction affects only the profile that sets it.
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 ENSURE_VERIFY_APPS = "ensure_verify_apps";
524
525 /**
Amith Yamasani26af8292014-09-09 09:57:27 -0700526 * Specifies if a user is disallowed from configuring cell
Amith Yamasanic34dc7c2014-09-18 09:42:42 -0700527 * broadcasts. This can only be set by device owners and profile owners on the primary user.
528 * The default value is <code>false</code>.
Fyodor Kupolov0ffbfea2016-01-19 10:07:54 -0800529 * <p>This restriction has no effect on secondary users and managed profiles since only the
Amith Yamasani150514b2015-01-07 16:05:05 -0800530 * primary user can configure cell broadcasts.
Amith Yamasani26af8292014-09-09 09:57:27 -0700531 *
Fyodor Kupolov0ffbfea2016-01-19 10:07:54 -0800532 * <p>Key for user restrictions.
533 * <p>Type: Boolean
Makoto Onuki068c54a2015-10-13 14:34:03 -0700534 * @see DevicePolicyManager#addUserRestriction(ComponentName, String)
535 * @see DevicePolicyManager#clearUserRestriction(ComponentName, String)
Julia Reynoldsd46d0f92014-04-23 15:23:24 -0400536 * @see #getUserRestrictions()
537 */
538 public static final String DISALLOW_CONFIG_CELL_BROADCASTS = "no_config_cell_broadcasts";
539
540 /**
Amith Yamasani26af8292014-09-09 09:57:27 -0700541 * Specifies if a user is disallowed from configuring mobile
Amith Yamasanic34dc7c2014-09-18 09:42:42 -0700542 * networks. This can only be set by device owners and profile owners on the primary user.
543 * The default value is <code>false</code>.
Fyodor Kupolov0ffbfea2016-01-19 10:07:54 -0800544 * <p>This restriction has no effect on secondary users and managed profiles since only the
Amith Yamasani150514b2015-01-07 16:05:05 -0800545 * primary user can configure mobile networks.
Amith Yamasani26af8292014-09-09 09:57:27 -0700546 *
Fyodor Kupolov0ffbfea2016-01-19 10:07:54 -0800547 * <p>Key for user restrictions.
548 * <p>Type: Boolean
Makoto Onuki068c54a2015-10-13 14:34:03 -0700549 * @see DevicePolicyManager#addUserRestriction(ComponentName, String)
550 * @see DevicePolicyManager#clearUserRestriction(ComponentName, String)
Julia Reynoldsd46d0f92014-04-23 15:23:24 -0400551 * @see #getUserRestrictions()
552 */
553 public static final String DISALLOW_CONFIG_MOBILE_NETWORKS = "no_config_mobile_networks";
554
555 /**
Amith Yamasani26af8292014-09-09 09:57:27 -0700556 * Specifies if a user is disallowed from modifying
Julia Reynoldsc617f812014-07-25 16:32:27 -0400557 * applications in Settings or launchers. The following actions will not be allowed when this
558 * restriction is enabled:
559 * <li>uninstalling apps</li>
560 * <li>disabling apps</li>
561 * <li>clearing app caches</li>
562 * <li>clearing app data</li>
563 * <li>force stopping apps</li>
564 * <li>clearing app defaults</li>
565 * <p>
566 * The default value is <code>false</code>.
Amith Yamasani26af8292014-09-09 09:57:27 -0700567 *
Esteban Talavera8bd7c522017-02-13 12:35:04 +0000568 * <p><strong>Note:</strong> The user will still be able to perform those actions via other
569 * means (such as adb). Third party apps will also be able to uninstall apps via the
570 * {@link android.content.pm.PackageInstaller}. {@link #DISALLOW_UNINSTALL_APPS} or
571 * {@link DevicePolicyManager#setUninstallBlocked(ComponentName, String, boolean)} should be
572 * used to prevent the user from uninstalling apps completely, and
573 * {@link DevicePolicyManager#addPersistentPreferredActivity(ComponentName, IntentFilter, ComponentName)}
574 * to add a default intent handler for a given intent filter.
575 *
Fyodor Kupolov0ffbfea2016-01-19 10:07:54 -0800576 * <p>Key for user restrictions.
577 * <p>Type: Boolean
Makoto Onuki068c54a2015-10-13 14:34:03 -0700578 * @see DevicePolicyManager#addUserRestriction(ComponentName, String)
579 * @see DevicePolicyManager#clearUserRestriction(ComponentName, String)
Julia Reynoldsd46d0f92014-04-23 15:23:24 -0400580 * @see #getUserRestrictions()
581 */
Julia Reynolds36fbc8d2014-06-18 09:26:30 -0400582 public static final String DISALLOW_APPS_CONTROL = "no_control_apps";
Julia Reynoldsd46d0f92014-04-23 15:23:24 -0400583
Emily Bernier394a6cd2014-05-07 12:49:20 -0400584 /**
Amith Yamasani26af8292014-09-09 09:57:27 -0700585 * Specifies if a user is disallowed from mounting
Amith Yamasanic34dc7c2014-09-18 09:42:42 -0700586 * physical external media. This can only be set by device owners and profile owners on the
587 * primary user. The default value is <code>false</code>.
Amith Yamasani26af8292014-09-09 09:57:27 -0700588 *
Fyodor Kupolov0ffbfea2016-01-19 10:07:54 -0800589 * <p>Key for user restrictions.
590 * <p>Type: Boolean
Makoto Onuki068c54a2015-10-13 14:34:03 -0700591 * @see DevicePolicyManager#addUserRestriction(ComponentName, String)
592 * @see DevicePolicyManager#clearUserRestriction(ComponentName, String)
Emily Bernier394a6cd2014-05-07 12:49:20 -0400593 * @see #getUserRestrictions()
594 */
595 public static final String DISALLOW_MOUNT_PHYSICAL_MEDIA = "no_physical_media";
596
597 /**
Pavel Grafovce3e1a32017-04-21 14:48:21 +0100598 * Specifies if a user is disallowed from adjusting microphone volume. If set, the microphone
599 * will be muted. This can be set by device owners and profile owners. The default value is
600 * <code>false</code>.
Amith Yamasani26af8292014-09-09 09:57:27 -0700601 *
Pavel Grafovce3e1a32017-04-21 14:48:21 +0100602 * <p>This restriction has no effect on managed profiles.
Fyodor Kupolov0ffbfea2016-01-19 10:07:54 -0800603 * <p>Key for user restrictions.
604 * <p>Type: Boolean
Makoto Onuki068c54a2015-10-13 14:34:03 -0700605 * @see DevicePolicyManager#addUserRestriction(ComponentName, String)
606 * @see DevicePolicyManager#clearUserRestriction(ComponentName, String)
Emily Bernier394a6cd2014-05-07 12:49:20 -0400607 * @see #getUserRestrictions()
608 */
609 public static final String DISALLOW_UNMUTE_MICROPHONE = "no_unmute_microphone";
610
611 /**
Pavel Grafovce3e1a32017-04-21 14:48:21 +0100612 * Specifies if a user is disallowed from adjusting the master volume. If set, the master volume
Wen ZHANG61ed0dc2017-08-23 14:27:02 +0100613 * will be muted. This can be set by device owners from API 21 and profile owners from API 24.
614 * The default value is <code>false</code>.
615 *
616 * <p>When the restriction is set by profile owners, then it only applies to relevant
617 * profiles.
Amith Yamasani26af8292014-09-09 09:57:27 -0700618 *
Pavel Grafovce3e1a32017-04-21 14:48:21 +0100619 * <p>This restriction has no effect on managed profiles.
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_ADJUST_VOLUME = "no_adjust_volume";
627
Amith Yamasani9f6c25f2014-05-16 14:49:15 -0700628 /**
Amith Yamasani26af8292014-09-09 09:57:27 -0700629 * Specifies that the user is not allowed to make outgoing
Amith Yamasani390989d2014-07-17 10:52:03 -0700630 * phone calls. Emergency calls are still permitted.
Amith Yamasani9f6c25f2014-05-16 14:49:15 -0700631 * The default value is <code>false</code>.
Tony Makeb83ab52016-02-22 18:36:08 +0000632 * <p>This restriction has no effect on managed profiles.
Amith Yamasani26af8292014-09-09 09:57:27 -0700633 *
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)
Amith Yamasani9f6c25f2014-05-16 14:49:15 -0700638 * @see #getUserRestrictions()
639 */
Amith Yamasani390989d2014-07-17 10:52:03 -0700640 public static final String DISALLOW_OUTGOING_CALLS = "no_outgoing_calls";
641
642 /**
Amith Yamasani26af8292014-09-09 09:57:27 -0700643 * Specifies that the user is not allowed to send or receive
Amith Yamasanic34dc7c2014-09-18 09:42:42 -0700644 * SMS messages. The default value is <code>false</code>.
Amith Yamasani26af8292014-09-09 09:57:27 -0700645 *
Fyodor Kupolov0ffbfea2016-01-19 10:07:54 -0800646 * <p>Key for user restrictions.
647 * <p>Type: Boolean
Makoto Onuki068c54a2015-10-13 14:34:03 -0700648 * @see DevicePolicyManager#addUserRestriction(ComponentName, String)
649 * @see DevicePolicyManager#clearUserRestriction(ComponentName, String)
Amith Yamasani390989d2014-07-17 10:52:03 -0700650 * @see #getUserRestrictions()
651 */
652 public static final String DISALLOW_SMS = "no_sms";
Amith Yamasani9f6c25f2014-05-16 14:49:15 -0700653
Jason Monk1c7c3192014-06-26 12:52:18 -0400654 /**
Jeff Sharkey2cc03e52015-03-20 11:24:04 -0700655 * Specifies if the user is not allowed to have fun. In some cases, the
656 * device owner may wish to prevent the user from experiencing amusement or
657 * joy while using the device. The default value is <code>false</code>.
658 *
Fyodor Kupolov0ffbfea2016-01-19 10:07:54 -0800659 * <p>Key for user restrictions.
660 * <p>Type: Boolean
Makoto Onuki068c54a2015-10-13 14:34:03 -0700661 * @see DevicePolicyManager#addUserRestriction(ComponentName, String)
662 * @see DevicePolicyManager#clearUserRestriction(ComponentName, String)
Jeff Sharkey2cc03e52015-03-20 11:24:04 -0700663 * @see #getUserRestrictions()
664 */
665 public static final String DISALLOW_FUN = "no_fun";
666
667 /**
Amith Yamasani26af8292014-09-09 09:57:27 -0700668 * Specifies that windows besides app windows should not be
Jason Monk1c7c3192014-06-26 12:52:18 -0400669 * created. This will block the creation of the following types of windows.
670 * <li>{@link LayoutParams#TYPE_TOAST}</li>
671 * <li>{@link LayoutParams#TYPE_PHONE}</li>
672 * <li>{@link LayoutParams#TYPE_PRIORITY_PHONE}</li>
673 * <li>{@link LayoutParams#TYPE_SYSTEM_ALERT}</li>
674 * <li>{@link LayoutParams#TYPE_SYSTEM_ERROR}</li>
675 * <li>{@link LayoutParams#TYPE_SYSTEM_OVERLAY}</li>
Wale Ogunwale5cd907d2017-01-26 14:14:08 -0800676 * <li>{@link LayoutParams#TYPE_APPLICATION_OVERLAY}</li>
Jason Monk1c7c3192014-06-26 12:52:18 -0400677 *
Amith Yamasanic34dc7c2014-09-18 09:42:42 -0700678 * <p>This can only be set by device owners and profile owners on the primary user.
679 * The default value is <code>false</code>.
Amith Yamasani26af8292014-09-09 09:57:27 -0700680 *
Fyodor Kupolov0ffbfea2016-01-19 10:07:54 -0800681 * <p>Key for user restrictions.
682 * <p>Type: Boolean
Makoto Onuki068c54a2015-10-13 14:34:03 -0700683 * @see DevicePolicyManager#addUserRestriction(ComponentName, String)
684 * @see DevicePolicyManager#clearUserRestriction(ComponentName, String)
Jason Monk1c7c3192014-06-26 12:52:18 -0400685 * @see #getUserRestrictions()
686 */
687 public static final String DISALLOW_CREATE_WINDOWS = "no_create_windows";
688
Nicolas Prevotf1939902014-06-25 09:29:02 +0100689 /**
Charles He22ff6f9d2017-10-05 21:28:55 +0100690 * Specifies that system error dialogs for crashed or unresponsive apps should not be shown.
691 * In this case, the system will force-stop the app as if the user chooses the "close app"
692 * option on the UI. No feedback report will be collected as there is no way for the user to
693 * provide explicit consent.
694 *
695 * When this user restriction is set by device owners, it's applied to all users; when it's set
696 * by profile owners, it's only applied to the relevant profiles.
697 * The default value is <code>false</code>.
698 *
699 * <p>This user restriction has no effect on managed profiles.
700 * <p>Key for user restrictions.
701 * <p>Type: Boolean
702 * @see DevicePolicyManager#addUserRestriction(ComponentName, String)
703 * @see DevicePolicyManager#clearUserRestriction(ComponentName, String)
704 * @see #getUserRestrictions()
705 */
706 public static final String DISALLOW_SYSTEM_ERROR_DIALOGS = "no_system_error_dialogs";
707
708 /**
Amith Yamasani26af8292014-09-09 09:57:27 -0700709 * Specifies if what is copied in the clipboard of this profile can
Nicolas Prevotf1939902014-06-25 09:29:02 +0100710 * be pasted in related profiles. Does not restrict if the clipboard of related profiles can be
711 * pasted in this profile.
712 * The default value is <code>false</code>.
Amith Yamasani26af8292014-09-09 09:57:27 -0700713 *
Fyodor Kupolov0ffbfea2016-01-19 10:07:54 -0800714 * <p>Key for user restrictions.
715 * <p>Type: Boolean
Makoto Onuki068c54a2015-10-13 14:34:03 -0700716 * @see DevicePolicyManager#addUserRestriction(ComponentName, String)
717 * @see DevicePolicyManager#clearUserRestriction(ComponentName, String)
Nicolas Prevotf1939902014-06-25 09:29:02 +0100718 * @see #getUserRestrictions()
719 */
720 public static final String DISALLOW_CROSS_PROFILE_COPY_PASTE = "no_cross_profile_copy_paste";
721
Amith Yamasani26af8292014-09-09 09:57:27 -0700722 /**
723 * Specifies if the user is not allowed to use NFC to beam out data from apps.
724 * The default value is <code>false</code>.
725 *
Fyodor Kupolov0ffbfea2016-01-19 10:07:54 -0800726 * <p>Key for user restrictions.
727 * <p>Type: Boolean
Makoto Onuki068c54a2015-10-13 14:34:03 -0700728 * @see DevicePolicyManager#addUserRestriction(ComponentName, String)
729 * @see DevicePolicyManager#clearUserRestriction(ComponentName, String)
Amith Yamasani26af8292014-09-09 09:57:27 -0700730 * @see #getUserRestrictions()
731 */
732 public static final String DISALLOW_OUTGOING_BEAM = "no_outgoing_beam";
733
Sander Alewijnse53d63dc2014-11-07 21:43:00 +0000734 /**
Oleksandr Peletskyif2519812016-01-26 20:16:06 +0100735 * Hidden user restriction to disallow access to wallpaper manager APIs. This restriction
736 * generally means that wallpapers are not supported for the particular user. This user
737 * restriction is always set for managed profiles, because such profiles don't have wallpapers.
Benjamin Franzf3ece362015-02-11 10:51:10 +0000738 * @hide
Oleksandr Peletskyif2519812016-01-26 20:16:06 +0100739 * @see #DISALLOW_SET_WALLPAPER
Makoto Onuki068c54a2015-10-13 14:34:03 -0700740 * @see DevicePolicyManager#addUserRestriction(ComponentName, String)
741 * @see DevicePolicyManager#clearUserRestriction(ComponentName, String)
Benjamin Franzf3ece362015-02-11 10:51:10 +0000742 * @see #getUserRestrictions()
743 */
744 public static final String DISALLOW_WALLPAPER = "no_wallpaper";
745
746 /**
Oleksandr Peletskyif2519812016-01-26 20:16:06 +0100747 * User restriction to disallow setting a wallpaper. Profile owner and device owner
748 * are able to set wallpaper regardless of this restriction.
749 * The default value is <code>false</code>.
750 *
751 * <p>Key for user restrictions.
752 * <p>Type: Boolean
753 * @see DevicePolicyManager#addUserRestriction(ComponentName, String)
754 * @see DevicePolicyManager#clearUserRestriction(ComponentName, String)
755 * @see #getUserRestrictions()
756 */
757 public static final String DISALLOW_SET_WALLPAPER = "no_set_wallpaper";
758
759 /**
Benjamin Franzbff46ba2015-03-05 18:33:51 +0000760 * Specifies if the user is not allowed to reboot the device into safe boot mode.
761 * This can only be set by device owners and profile owners on the primary user.
762 * The default value is <code>false</code>.
763 *
Fyodor Kupolov0ffbfea2016-01-19 10:07:54 -0800764 * <p>Key for user restrictions.
765 * <p>Type: Boolean
Makoto Onuki068c54a2015-10-13 14:34:03 -0700766 * @see DevicePolicyManager#addUserRestriction(ComponentName, String)
767 * @see DevicePolicyManager#clearUserRestriction(ComponentName, String)
Benjamin Franzbff46ba2015-03-05 18:33:51 +0000768 * @see #getUserRestrictions()
769 */
770 public static final String DISALLOW_SAFE_BOOT = "no_safe_boot";
771
772 /**
Fyodor Kupolovb5013302015-04-17 17:59:14 -0700773 * Specifies if a user is not allowed to record audio. This restriction is always enabled for
774 * background users. The default value is <code>false</code>.
775 *
Makoto Onuki068c54a2015-10-13 14:34:03 -0700776 * @see DevicePolicyManager#addUserRestriction(ComponentName, String)
777 * @see DevicePolicyManager#clearUserRestriction(ComponentName, String)
Fyodor Kupolovb5013302015-04-17 17:59:14 -0700778 * @see #getUserRestrictions()
779 * @hide
780 */
781 public static final String DISALLOW_RECORD_AUDIO = "no_record_audio";
782
783 /**
Fyodor Kupolov9cbfc9e2015-10-07 15:52:33 -0700784 * Specifies if a user is not allowed to run in the background and should be stopped during
785 * user switch. The default value is <code>false</code>.
786 *
787 * <p>This restriction can be set by device owners and profile owners.
788 *
789 * @see DevicePolicyManager#addUserRestriction(ComponentName, String)
790 * @see DevicePolicyManager#clearUserRestriction(ComponentName, String)
791 * @see #getUserRestrictions()
792 * @hide
793 */
794 public static final String DISALLOW_RUN_IN_BACKGROUND = "no_run_in_background";
795
796 /**
Makoto Onuki759a7632015-10-28 16:43:10 -0700797 * Specifies if a user is not allowed to use the camera.
798 *
799 * @see DevicePolicyManager#addUserRestriction(ComponentName, String)
800 * @see DevicePolicyManager#clearUserRestriction(ComponentName, String)
801 * @see #getUserRestrictions()
802 * @hide
803 */
804 public static final String DISALLOW_CAMERA = "no_camera";
805
806 /**
Tony Makc1205112016-07-22 16:02:59 +0100807 * Specifies if a user is not allowed to unmute the device's master volume.
808 *
809 * @see DevicePolicyManager#setMasterVolumeMuted(ComponentName, boolean)
810 * @see DevicePolicyManager#clearUserRestriction(ComponentName, String)
811 * @see #getUserRestrictions()
812 * @hide
813 */
Esteban Talavera492b4722017-02-13 14:59:45 +0000814 public static final String DISALLOW_UNMUTE_DEVICE = "disallow_unmute_device";
Tony Makc1205112016-07-22 16:02:59 +0100815
816 /**
Mahaver Chopradea471e2015-12-17 11:02:37 +0000817 * Specifies if a user is not allowed to use cellular data when roaming. This can only be set by
818 * device owners. The default value is <code>false</code>.
819 *
820 * @see DevicePolicyManager#addUserRestriction(ComponentName, String)
821 * @see DevicePolicyManager#clearUserRestriction(ComponentName, String)
822 * @see #getUserRestrictions()
823 */
824 public static final String DISALLOW_DATA_ROAMING = "no_data_roaming";
825
826 /**
Oleksandr Peletskyi7f1f1df2016-01-18 15:40:21 +0100827 * Specifies if a user is not allowed to change their icon. Device owner and profile owner
828 * can set this restriction. When it is set by device owner, only the target user will be
829 * affected. The default value is <code>false</code>.
830 *
831 * <p>Key for user restrictions.
Oleksandr Peletskyi7f1f1df2016-01-18 15:40:21 +0100832 * <p>Type: Boolean
Oleksandr Peletskyi7f1f1df2016-01-18 15:40:21 +0100833 * @see DevicePolicyManager#addUserRestriction(ComponentName, String)
834 * @see DevicePolicyManager#clearUserRestriction(ComponentName, String)
835 * @see #getUserRestrictions()
836 */
837 public static final String DISALLOW_SET_USER_ICON = "no_set_user_icon";
838
839 /**
Mahaver Chopra3d9805d2016-07-07 16:25:05 +0100840 * Specifies if a user is not allowed to enable the oem unlock setting. The default value is
Esteban Talaverac48b20f2016-08-11 11:23:40 +0100841 * <code>false</code>. Setting this restriction has no effect if the bootloader is already
842 * unlocked.
Mahaver Chopra3d9805d2016-07-07 16:25:05 +0100843 *
Lenka Trochtova12b04962016-11-29 21:00:12 +0100844 * <p>Not for use by third-party applications.
845 *
Mahaver Chopra3d9805d2016-07-07 16:25:05 +0100846 * @see DevicePolicyManager#addUserRestriction(ComponentName, String)
847 * @see DevicePolicyManager#clearUserRestriction(ComponentName, String)
848 * @see #getUserRestrictions()
Andrew Scull3b8b46f2017-02-13 18:12:15 +0000849 * @deprecated use {@link OemLockManager#setOemUnlockAllowedByCarrier(boolean, byte[])} instead.
Mahaver Chopra3d9805d2016-07-07 16:25:05 +0100850 * @hide
851 */
Andrew Scull3b8b46f2017-02-13 18:12:15 +0000852 @Deprecated
Lenka Trochtova12b04962016-11-29 21:00:12 +0100853 @SystemApi
Mahaver Chopra3d9805d2016-07-07 16:25:05 +0100854 public static final String DISALLOW_OEM_UNLOCK = "no_oem_unlock";
855
856 /**
Pavel Grafovc4f87e92017-10-26 16:34:25 +0100857 * Specifies that the managed profile is not allowed to have unified lock screen challenge with
858 * the primary user.
859 *
860 * <p><strong>Note:</strong> Setting this restriction alone doesn't automatically set a
861 * separate challenge. Profile owner can ask the user to set a new password using
862 * {@link DevicePolicyManager#ACTION_SET_NEW_PASSWORD} and verify it using
863 * {@link DevicePolicyManager#isUsingUnifiedPassword(ComponentName)}.
864 *
865 * <p>Can be set by profile owners. It only has effect on managed profiles when set by managed
866 * profile owner. Has no effect on non-managed profiles or users.
867 * <p>Key for user restrictions.
868 * <p>Type: Boolean
869 * @see DevicePolicyManager#addUserRestriction(ComponentName, String)
870 * @see DevicePolicyManager#clearUserRestriction(ComponentName, String)
871 * @see #getUserRestrictions()
872 */
873 public static final String DISALLOW_UNIFIED_PASSWORD = "no_unified_password";
874
875 /**
Nicolas Prevotf0029c12015-06-25 14:25:41 -0700876 * Allows apps in the parent profile to handle web links from the managed profile.
877 *
Nicolas Prevot9edbda12015-06-17 11:09:48 -0700878 * This user restriction has an effect only in a managed profile.
879 * If set:
880 * Intent filters of activities in the parent profile with action
881 * {@link android.content.Intent#ACTION_VIEW},
882 * category {@link android.content.Intent#CATEGORY_BROWSABLE}, scheme http or https, and which
883 * define a host can handle intents from the managed profile.
884 * The default value is <code>false</code>.
885 *
Fyodor Kupolov0ffbfea2016-01-19 10:07:54 -0800886 * <p>Key for user restrictions.
887 * <p>Type: Boolean
Makoto Onuki068c54a2015-10-13 14:34:03 -0700888 * @see DevicePolicyManager#addUserRestriction(ComponentName, String)
889 * @see DevicePolicyManager#clearUserRestriction(ComponentName, String)
Nicolas Prevot9edbda12015-06-17 11:09:48 -0700890 * @see #getUserRestrictions()
891 */
Nicolas Prevotf0029c12015-06-25 14:25:41 -0700892 public static final String ALLOW_PARENT_PROFILE_APP_LINKING
893 = "allow_parent_profile_app_linking";
Nicolas Prevot9edbda12015-06-17 11:09:48 -0700894
895 /**
Felipe Leme24d58932017-03-21 14:13:58 -0700896 * Specifies if a user is not allowed to use Autofill Services.
897 *
898 * <p>Device owner and profile owner can set this restriction. When it is set by device owner,
899 * only the target user will be affected.
900 *
901 * <p>The default value is <code>false</code>.
902 *
903 * @see DevicePolicyManager#addUserRestriction(ComponentName, String)
904 * @see DevicePolicyManager#clearUserRestriction(ComponentName, String)
905 * @see #getUserRestrictions()
906 */
907 public static final String DISALLOW_AUTOFILL = "no_autofill";
908
909 /**
Benjamin Franzff66fa92017-08-10 10:39:44 +0100910 * Specifies if user switching is blocked on the current user.
911 *
912 * <p> This restriction can only be set by the device owner, it will be applied to all users.
913 *
914 * <p>The default value is <code>false</code>.
915 *
916 * @see DevicePolicyManager#addUserRestriction(ComponentName, String)
917 * @see DevicePolicyManager#clearUserRestriction(ComponentName, String)
918 * @see #getUserRestrictions()
919 */
920 public static final String DISALLOW_USER_SWITCH = "no_user_switch";
921
922 /**
Rubin Xucc391c22018-01-02 20:37:35 +0000923 * Specifies whether the user can share file / picture / data from the primary user into the
924 * managed profile, either by sending them from the primary side, or by picking up data within
925 * an app in the managed profile.
926 * <p>
927 * When a managed profile is created, the system allows the user to send data from the primary
928 * side to the profile by setting up certain default cross profile intent filters. If
929 * this is undesired, this restriction can be set to disallow it. Note that this restriction
930 * will not block any sharing allowed by explicit
931 * {@link DevicePolicyManager#addCrossProfileIntentFilter} calls by the profile owner.
932 * <p>
933 * This restriction is only meaningful when set by profile owner. When it is set by device
934 * owner, it does not have any effect.
935 * <p>
936 * 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_SHARE_INTO_MANAGED_PROFILE = "no_sharing_into_profile";
943 /**
Sander Alewijnse53d63dc2014-11-07 21:43:00 +0000944 * Application restriction key that is used to indicate the pending arrival
945 * of real restrictions for the app.
946 *
947 * <p>
948 * Applications that support restrictions should check for the presence of this key.
949 * A <code>true</code> value indicates that restrictions may be applied in the near
950 * future but are not available yet. It is the responsibility of any
951 * management application that sets this flag to update it when the final
952 * restrictions are enforced.
953 *
Fyodor Kupolov0ffbfea2016-01-19 10:07:54 -0800954 * <p>Key for application restrictions.
955 * <p>Type: Boolean
Nicolas Prevotb14ed952015-01-13 14:23:53 +0000956 * @see android.app.admin.DevicePolicyManager#setApplicationRestrictions(
957 * android.content.ComponentName, String, Bundle)
958 * @see android.app.admin.DevicePolicyManager#getApplicationRestrictions(
959 * android.content.ComponentName, String)
Sander Alewijnse53d63dc2014-11-07 21:43:00 +0000960 */
961 public static final String KEY_RESTRICTIONS_PENDING = "restrictions_pending";
962
Amith Yamasani12747872015-12-07 14:19:49 -0800963 private static final String ACTION_CREATE_USER = "android.os.action.CREATE_USER";
964
965 /**
966 * Extra containing a name for the user being created. Optional parameter passed to
967 * ACTION_CREATE_USER activity.
968 * @hide
969 */
970 public static final String EXTRA_USER_NAME = "android.os.extra.USER_NAME";
971
972 /**
973 * Extra containing account name for the user being created. Optional parameter passed to
974 * ACTION_CREATE_USER activity.
975 * @hide
976 */
977 public static final String EXTRA_USER_ACCOUNT_NAME = "android.os.extra.USER_ACCOUNT_NAME";
978
979 /**
980 * Extra containing account type for the user being created. Optional parameter passed to
981 * ACTION_CREATE_USER activity.
982 * @hide
983 */
984 public static final String EXTRA_USER_ACCOUNT_TYPE = "android.os.extra.USER_ACCOUNT_TYPE";
985
986 /**
987 * Extra containing account-specific data for the user being created. Optional parameter passed
988 * to ACTION_CREATE_USER activity.
989 * @hide
990 */
991 public static final String EXTRA_USER_ACCOUNT_OPTIONS
992 = "android.os.extra.USER_ACCOUNT_OPTIONS";
993
Amith Yamasani655d0e22013-06-12 14:19:10 -0700994 /** @hide */
995 public static final int PIN_VERIFICATION_FAILED_INCORRECT = -3;
996 /** @hide */
997 public static final int PIN_VERIFICATION_FAILED_NOT_SET = -2;
998 /** @hide */
999 public static final int PIN_VERIFICATION_SUCCESS = -1;
1000
Amith Yamasani37ed8d12016-01-27 14:40:16 -08001001 /**
Makoto Onukie72f81b2017-03-16 14:08:19 -07001002 * Sent when user restrictions have changed.
1003 *
1004 * @hide
1005 */
1006 @SystemApi
1007 @TestApi // To allow seeing it from CTS.
1008 public static final String ACTION_USER_RESTRICTIONS_CHANGED =
1009 "android.os.action.USER_RESTRICTIONS_CHANGED";
1010
1011 /**
Amith Yamasani37ed8d12016-01-27 14:40:16 -08001012 * Error result indicating that this user is not allowed to add other users on this device.
1013 * This is a result code returned from the activity created by the intent
1014 * {@link #createUserCreationIntent(String, String, String, PersistableBundle)}.
1015 */
1016 public static final int USER_CREATION_FAILED_NOT_PERMITTED = Activity.RESULT_FIRST_USER;
1017
1018 /**
1019 * Error result indicating that no more users can be created on this device.
1020 * This is a result code returned from the activity created by the intent
1021 * {@link #createUserCreationIntent(String, String, String, PersistableBundle)}.
1022 */
1023 public static final int USER_CREATION_FAILED_NO_MORE_USERS = Activity.RESULT_FIRST_USER + 1;
1024
Amith Yamasani7e99bc02013-04-16 18:24:51 -07001025 /** @hide */
Amith Yamasanic0688302015-10-30 10:40:03 -07001026 public static UserManager get(Context context) {
1027 return (UserManager) context.getSystemService(Context.USER_SERVICE);
Amith Yamasani27db4682013-03-30 17:07:47 -07001028 }
Maggie Benthalla12fccf2013-03-14 18:02:12 -04001029
Amith Yamasani258848d2012-08-10 17:06:33 -07001030 /** @hide */
1031 public UserManager(Context context, IUserManager service) {
1032 mService = service;
Fyodor Kupolov5200e1c2016-10-17 18:46:16 -07001033 mContext = context.getApplicationContext();
Amith Yamasani258848d2012-08-10 17:06:33 -07001034 }
1035
1036 /**
Amith Yamasani06964342016-04-15 13:55:01 -07001037 * Returns whether this device supports multiple users with their own login and customizable
1038 * space.
1039 * @return whether the device supports multiple users.
Amith Yamasani258848d2012-08-10 17:06:33 -07001040 */
Jeff Sharkey4673e7e2012-09-19 13:14:30 -07001041 public static boolean supportsMultipleUsers() {
Kenny Guy1a447532014-02-20 21:55:32 +00001042 return getMaxSupportedUsers() > 1
1043 && SystemProperties.getBoolean("fw.show_multiuserui",
1044 Resources.getSystem().getBoolean(R.bool.config_enableMultiUserUI));
Amith Yamasani258848d2012-08-10 17:06:33 -07001045 }
1046
Maggie Benthall67944582013-02-22 14:58:27 -05001047 /**
Xiaohui Chen7cb69df2015-07-13 16:01:01 -07001048 * @hide
1049 * @return Whether the device is running with split system user. It means the system user and
1050 * primary user are two separate users. Previously system user and primary user are combined as
1051 * a single owner user. see @link {android.os.UserHandle#USER_OWNER}
1052 */
1053 public static boolean isSplitSystemUser() {
John Reckaa67f682016-09-20 14:24:21 -07001054 return RoSystemProperties.FW_SYSTEM_USER_SPLIT;
Xiaohui Chen7cb69df2015-07-13 16:01:01 -07001055 }
1056
1057 /**
Evan Rosky13a58a92016-07-27 15:51:09 -07001058 * @return Whether guest user is always ephemeral
1059 * @hide
1060 */
1061 public static boolean isGuestUserEphemeral() {
1062 return Resources.getSystem()
1063 .getBoolean(com.android.internal.R.bool.config_guestUserEphemeral);
1064 }
1065
1066 /**
Fyodor Kupolov523c4042016-02-24 15:03:13 -08001067 * Returns whether switching users is currently allowed.
1068 * <p>For instance switching users is not allowed if the current user is in a phone call,
Benjamin Franzff66fa92017-08-10 10:39:44 +01001069 * system user hasn't been unlocked yet, or {@link #DISALLOW_USER_SWITCH} is set.
Fyodor Kupolov523c4042016-02-24 15:03:13 -08001070 * @hide
1071 */
1072 public boolean canSwitchUsers() {
1073 boolean allowUserSwitchingWhenSystemUserLocked = Settings.Global.getInt(
1074 mContext.getContentResolver(),
1075 Settings.Global.ALLOW_USER_SWITCHING_WHEN_SYSTEM_USER_LOCKED, 0) != 0;
1076 boolean isSystemUserUnlocked = isUserUnlocked(UserHandle.SYSTEM);
1077 boolean inCall = TelephonyManager.getDefault().getCallState()
1078 != TelephonyManager.CALL_STATE_IDLE;
Benjamin Franzff66fa92017-08-10 10:39:44 +01001079 boolean isUserSwitchDisallowed = hasUserRestriction(DISALLOW_USER_SWITCH);
1080 return (allowUserSwitchingWhenSystemUserLocked || isSystemUserUnlocked) && !inCall
1081 && !isUserSwitchDisallowed;
Fyodor Kupolov523c4042016-02-24 15:03:13 -08001082 }
1083
1084 /**
Amith Yamasani5760e172015-04-17 18:42:41 -07001085 * Returns the user handle for the user that this process is running under.
Jessica Hummelbe81c802014-04-22 15:49:22 +01001086 *
Amith Yamasani5760e172015-04-17 18:42:41 -07001087 * @return the user handle of this process.
Amith Yamasani258848d2012-08-10 17:06:33 -07001088 * @hide
Maggie Benthall67944582013-02-22 14:58:27 -05001089 */
Jeff Sharkey8588bc12016-01-06 16:47:42 -07001090 public @UserIdInt int getUserHandle() {
Dianne Hackborn79af1dd2012-08-16 16:42:52 -07001091 return UserHandle.myUserId();
Amith Yamasani258848d2012-08-10 17:06:33 -07001092 }
1093
1094 /**
Dianne Hackborn8832c182012-09-17 17:20:24 -07001095 * Returns the user name of the user making this call. This call is only
1096 * available to applications on the system image; it requires the
1097 * MANAGE_USERS permission.
Amith Yamasani258848d2012-08-10 17:06:33 -07001098 * @return the user name
1099 */
1100 public String getUserName() {
Will Harmond9dcfb12017-05-18 15:41:15 -07001101 UserInfo user = getUserInfo(getUserHandle());
1102 return user == null ? "" : user.name;
Amith Yamasani258848d2012-08-10 17:06:33 -07001103 }
1104
Dianne Hackborn67a101a2014-10-02 12:42:18 -07001105 /**
Fyodor Kupolov605b12a2017-05-10 15:58:09 -07001106 * Returns whether user name has been set.
1107 * <p>This method can be used to check that the value returned by {@link #getUserName()} was
1108 * set by the user and is not a placeholder string provided by the system.
1109 * @hide
1110 */
1111 public boolean isUserNameSet() {
1112 try {
1113 return mService.isUserNameSet(getUserHandle());
1114 } catch (RemoteException re) {
1115 throw re.rethrowFromSystemServer();
1116 }
1117 }
1118
1119 /**
Dan Morrille4ab16a2012-09-20 20:25:55 -07001120 * Used to determine whether the user making this call is subject to
1121 * teleportations.
Dianne Hackborn67a101a2014-10-02 12:42:18 -07001122 *
Dianne Hackborn955d8d62014-10-07 20:17:19 -07001123 * <p>As of {@link android.os.Build.VERSION_CODES#LOLLIPOP}, this method can
Dianne Hackborn67a101a2014-10-02 12:42:18 -07001124 * now automatically identify goats using advanced goat recognition technology.</p>
1125 *
1126 * @return Returns true if the user making this call is a goat.
Dan Morrille4ab16a2012-09-20 20:25:55 -07001127 */
1128 public boolean isUserAGoat() {
Adam Powell988ae302014-09-17 17:58:33 -07001129 return mContext.getPackageManager()
1130 .isPackageAvailable("com.coffeestainstudios.goatsimulator");
Dan Morrille4ab16a2012-09-20 20:25:55 -07001131 }
Amith Yamasanidf2e92a2013-03-01 17:04:38 -08001132
1133 /**
Xiaohui Chen70f6c382015-04-28 14:21:43 -07001134 * Used to check if this process is running under the primary user. The primary user
1135 * is the first human user on a device.
1136 *
1137 * @return whether this process is running under the primary user.
1138 * @hide
1139 */
1140 public boolean isPrimaryUser() {
1141 UserInfo user = getUserInfo(UserHandle.myUserId());
Amith Yamasanid35a89c2016-05-26 16:58:43 -07001142 return user != null && user.isPrimary();
Xiaohui Chen70f6c382015-04-28 14:21:43 -07001143 }
1144
1145 /**
Amith Yamasani5760e172015-04-17 18:42:41 -07001146 * Used to check if this process is running under the system user. The system user
1147 * is the initial user that is implicitly created on first boot and hosts most of the
1148 * system services.
1149 *
1150 * @return whether this process is running under the system user.
1151 */
1152 public boolean isSystemUser() {
Xiaohui Chen70f6c382015-04-28 14:21:43 -07001153 return UserHandle.myUserId() == UserHandle.USER_SYSTEM;
Amith Yamasani5760e172015-04-17 18:42:41 -07001154 }
Xiaohui Chen2b45f8a2015-08-04 15:12:23 -07001155
Amith Yamasani5760e172015-04-17 18:42:41 -07001156 /**
Amith Yamasani462ac3a2015-06-30 14:21:01 -07001157 * @hide
1158 * Returns whether the caller is running as an admin user. There can be more than one admin
1159 * user.
1160 */
1161 public boolean isAdminUser() {
Xiaohui Chen2b45f8a2015-08-04 15:12:23 -07001162 return isUserAdmin(UserHandle.myUserId());
1163 }
1164
1165 /**
1166 * @hide
1167 * Returns whether the provided user is an admin user. There can be more than one admin
1168 * user.
1169 */
Jeff Sharkey8588bc12016-01-06 16:47:42 -07001170 public boolean isUserAdmin(@UserIdInt int userId) {
Xiaohui Chen2b45f8a2015-08-04 15:12:23 -07001171 UserInfo user = getUserInfo(userId);
1172 return user != null && user.isAdmin();
Amith Yamasani462ac3a2015-06-30 14:21:01 -07001173 }
1174
1175 /**
Fyodor Kupolovca177562017-11-09 17:43:01 -08001176 * @hide
1177 * @deprecated Use {@link #isRestrictedProfile()}
1178 */
1179 @Deprecated
1180 public boolean isLinkedUser() {
1181 return isRestrictedProfile();
1182 }
1183
1184 /**
1185 * Returns whether the caller is running as restricted profile. Restricted profile may have
Amith Yamasani46bc4eb2013-04-12 13:26:50 -07001186 * a reduced number of available apps, app restrictions and account restrictions.
Amith Yamasanie1375902013-04-13 16:48:35 -07001187 * @return whether the user making this call is a linked user
Amith Yamasani2555daf2013-04-25 13:39:27 -07001188 * @hide
Amith Yamasanidf2e92a2013-03-01 17:04:38 -08001189 */
Fyodor Kupolovca177562017-11-09 17:43:01 -08001190 @SystemApi
1191 public boolean isRestrictedProfile() {
Amith Yamasanidf2e92a2013-03-01 17:04:38 -08001192 try {
Amith Yamasani71e6c692013-03-24 17:39:28 -07001193 return mService.isRestricted();
Amith Yamasanidf2e92a2013-03-01 17:04:38 -08001194 } catch (RemoteException re) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001195 throw re.rethrowFromSystemServer();
Amith Yamasanidf2e92a2013-03-01 17:04:38 -08001196 }
1197 }
1198
Amith Yamasani258848d2012-08-10 17:06:33 -07001199 /**
Fyodor Kupolov1c363152015-09-02 13:27:21 -07001200 * Checks if specified user can have restricted profile.
1201 * @hide
1202 */
Jeff Sharkey8588bc12016-01-06 16:47:42 -07001203 public boolean canHaveRestrictedProfile(@UserIdInt int userId) {
Fyodor Kupolov1c363152015-09-02 13:27:21 -07001204 try {
1205 return mService.canHaveRestrictedProfile(userId);
1206 } catch (RemoteException re) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001207 throw re.rethrowFromSystemServer();
Fyodor Kupolov1c363152015-09-02 13:27:21 -07001208 }
1209 }
1210
1211 /**
Fyodor Kupolovca177562017-11-09 17:43:01 -08001212 * Returns whether the calling user has at least one restricted profile associated with it.
1213 * @return
1214 * @hide
1215 */
1216 @SystemApi
1217 public boolean hasRestrictedProfiles() {
1218 try {
1219 return mService.hasRestrictedProfiles();
1220 } catch (RemoteException re) {
1221 throw re.rethrowFromSystemServer();
1222 }
1223 }
1224
1225 /**
Evan Rosky13a58a92016-07-27 15:51:09 -07001226 * Checks if a user is a guest user.
1227 * @return whether user is a guest user.
1228 * @hide
1229 */
1230 public boolean isGuestUser(int id) {
1231 UserInfo user = getUserInfo(id);
1232 return user != null && user.isGuest();
1233 }
1234
1235 /**
Amith Yamasani1e9c2182014-06-11 17:25:51 -07001236 * Checks if the calling app is running as a guest user.
1237 * @return whether the caller is a guest user.
1238 * @hide
1239 */
1240 public boolean isGuestUser() {
1241 UserInfo user = getUserInfo(UserHandle.myUserId());
Amith Yamasanid35a89c2016-05-26 16:58:43 -07001242 return user != null && user.isGuest();
1243 }
1244
Fyodor Kupolovca177562017-11-09 17:43:01 -08001245
Amith Yamasanid35a89c2016-05-26 16:58:43 -07001246 /**
Amith Yamasani1c41dc82016-06-28 16:13:15 -07001247 * Checks if the calling app is running in a demo user. When running in a demo user,
1248 * apps can be more helpful to the user, or explain their features in more detail.
1249 *
Amith Yamasanid35a89c2016-05-26 16:58:43 -07001250 * @return whether the caller is a demo user.
Amith Yamasanid35a89c2016-05-26 16:58:43 -07001251 */
1252 public boolean isDemoUser() {
Amith Yamasani1c41dc82016-06-28 16:13:15 -07001253 try {
1254 return mService.isDemoUser(UserHandle.myUserId());
1255 } catch (RemoteException re) {
1256 throw re.rethrowFromSystemServer();
1257 }
Amith Yamasani1e9c2182014-06-11 17:25:51 -07001258 }
1259
1260 /**
Amith Yamasani0e8d7d62014-09-03 13:17:28 -07001261 * Checks if the calling app is running in a managed profile.
Amith Yamasani0e8d7d62014-09-03 13:17:28 -07001262 *
1263 * @return whether the caller is in a managed profile.
1264 * @hide
1265 */
1266 @SystemApi
Jeff Sharkeyd86b8fe2017-06-02 17:36:26 -06001267 @RequiresPermission(android.Manifest.permission.MANAGE_USERS)
Amith Yamasani0e8d7d62014-09-03 13:17:28 -07001268 public boolean isManagedProfile() {
Makoto Onukid49f3fa2017-01-25 14:09:48 -08001269 // No need for synchronization. Once it becomes non-null, it'll be non-null forever.
1270 // Worst case we might end up calling the AIDL method multiple times but that's fine.
1271 if (mIsManagedProfileCached != null) {
1272 return mIsManagedProfileCached;
1273 }
Tony Mak8673b282016-03-21 21:10:59 +00001274 try {
Makoto Onukid49f3fa2017-01-25 14:09:48 -08001275 mIsManagedProfileCached = mService.isManagedProfile(UserHandle.myUserId());
1276 return mIsManagedProfileCached;
Tony Mak8673b282016-03-21 21:10:59 +00001277 } catch (RemoteException re) {
1278 throw re.rethrowFromSystemServer();
1279 }
1280 }
1281
1282 /**
1283 * Checks if the specified user is a managed profile.
1284 * Requires {@link android.Manifest.permission#MANAGE_USERS} permission, otherwise the caller
1285 * must be in the same profile group of specified user.
1286 *
1287 * @return whether the specified user is a managed profile.
1288 * @hide
1289 */
1290 @SystemApi
Jeff Sharkeyd86b8fe2017-06-02 17:36:26 -06001291 @RequiresPermission(android.Manifest.permission.MANAGE_USERS)
Tony Mak8673b282016-03-21 21:10:59 +00001292 public boolean isManagedProfile(@UserIdInt int userId) {
Makoto Onukid49f3fa2017-01-25 14:09:48 -08001293 if (userId == UserHandle.myUserId()) {
1294 return isManagedProfile();
1295 }
Tony Mak8673b282016-03-21 21:10:59 +00001296 try {
1297 return mService.isManagedProfile(userId);
1298 } catch (RemoteException re) {
1299 throw re.rethrowFromSystemServer();
1300 }
Amith Yamasani0e8d7d62014-09-03 13:17:28 -07001301 }
1302
1303 /**
Kenny Guy02c89902016-11-15 19:36:38 +00001304 * Gets badge for a managed profile.
1305 * Requires {@link android.Manifest.permission#MANAGE_USERS} permission, otherwise the caller
1306 * must be in the same profile group of specified user.
1307 *
1308 * @return which badge to use for the managed profile badge id will be less than
1309 * UserManagerService.getMaxManagedProfiles()
1310 * @hide
1311 */
1312 public int getManagedProfileBadge(@UserIdInt int userId) {
1313 try {
1314 return mService.getManagedProfileBadge(userId);
1315 } catch (RemoteException re) {
1316 throw re.rethrowFromSystemServer();
1317 }
1318 }
1319
1320 /**
Lenka Trochtovac4dd0212015-11-18 12:22:06 +01001321 * Checks if the calling app is running as an ephemeral user.
1322 *
1323 * @return whether the caller is an ephemeral user.
1324 * @hide
1325 */
1326 public boolean isEphemeralUser() {
1327 return isUserEphemeral(UserHandle.myUserId());
1328 }
1329
1330 /**
1331 * Returns whether the specified user is ephemeral.
1332 * @hide
1333 */
Jeff Sharkey8588bc12016-01-06 16:47:42 -07001334 public boolean isUserEphemeral(@UserIdInt int userId) {
Lenka Trochtovac4dd0212015-11-18 12:22:06 +01001335 final UserInfo user = getUserInfo(userId);
1336 return user != null && user.isEphemeral();
1337 }
1338
1339 /**
Dianne Hackborna8a9bd62012-10-09 15:36:59 -07001340 * Return whether the given user is actively running. This means that
1341 * the user is in the "started" state, not "stopped" -- it is currently
1342 * allowed to run code through scheduled alarms, receiving broadcasts,
1343 * etc. A started user may be either the current foreground user or a
1344 * background user; the result here does not distinguish between the two.
Makoto Onuki5eef50d2017-03-02 16:38:45 -08001345 *
1346 * <p>Note prior to Android Nougat MR1 (SDK version <= 24;
1347 * {@link android.os.Build.VERSION_CODES#N), this API required a system permission
1348 * in order to check other profile's status.
1349 * Since Android Nougat MR1 (SDK version >= 25;
1350 * {@link android.os.Build.VERSION_CODES#N_MR1)), the restriction has been relaxed, and now
1351 * it'll accept any {@link UserHandle} within the same profile group as the caller.
Fyodor Kupolovcdb3c2f2017-02-10 11:48:32 -08001352 *
Dianne Hackborna8a9bd62012-10-09 15:36:59 -07001353 * @param user The user to retrieve the running state for.
1354 */
Makoto Onuki5eef50d2017-03-02 16:38:45 -08001355 // Note this requires either INTERACT_ACROSS_USERS or MANAGE_USERS.
Dianne Hackborna8a9bd62012-10-09 15:36:59 -07001356 public boolean isUserRunning(UserHandle user) {
Jeff Sharkey0e62384c2016-01-13 18:52:55 -07001357 return isUserRunning(user.getIdentifier());
1358 }
1359
1360 /** {@hide} */
Fyodor Kupolov2e7e0962016-12-01 18:09:17 -08001361 public boolean isUserRunning(@UserIdInt int userId) {
Dianne Hackborna8a9bd62012-10-09 15:36:59 -07001362 try {
Fyodor Kupolov2e7e0962016-12-01 18:09:17 -08001363 return mService.isUserRunning(userId);
Jeff Sharkey27b2e692016-02-25 17:40:12 -07001364 } catch (RemoteException re) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001365 throw re.rethrowFromSystemServer();
Dianne Hackborna8a9bd62012-10-09 15:36:59 -07001366 }
1367 }
1368
1369 /**
1370 * Return whether the given user is actively running <em>or</em> stopping.
1371 * This is like {@link #isUserRunning(UserHandle)}, but will also return
1372 * true if the user had been running but is in the process of being stopped
1373 * (but is not yet fully stopped, and still running some code).
Makoto Onuki5eef50d2017-03-02 16:38:45 -08001374 *
1375 * <p>Note prior to Android Nougat MR1 (SDK version <= 24;
1376 * {@link android.os.Build.VERSION_CODES#N), this API required a system permission
1377 * in order to check other profile's status.
1378 * Since Android Nougat MR1 (SDK version >= 25;
1379 * {@link android.os.Build.VERSION_CODES#N_MR1)), the restriction has been relaxed, and now
1380 * it'll accept any {@link UserHandle} within the same profile group as the caller.
Fyodor Kupolovcdb3c2f2017-02-10 11:48:32 -08001381 *
Dianne Hackborna8a9bd62012-10-09 15:36:59 -07001382 * @param user The user to retrieve the running state for.
1383 */
Makoto Onuki5eef50d2017-03-02 16:38:45 -08001384 // Note this requires either INTERACT_ACROSS_USERS or MANAGE_USERS.
Dianne Hackborna8a9bd62012-10-09 15:36:59 -07001385 public boolean isUserRunningOrStopping(UserHandle user) {
1386 try {
Jeff Sharkeye17ac152015-11-06 22:40:29 -08001387 // TODO: reconcile stopped vs stopping?
Sudheer Shankadc589ac2016-11-10 15:30:17 -08001388 return ActivityManager.getService().isUserRunning(
Jeff Sharkeye17ac152015-11-06 22:40:29 -08001389 user.getIdentifier(), ActivityManager.FLAG_OR_STOPPED);
Jeff Sharkey27b2e692016-02-25 17:40:12 -07001390 } catch (RemoteException re) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001391 throw re.rethrowFromSystemServer();
Dianne Hackborna8a9bd62012-10-09 15:36:59 -07001392 }
1393 }
1394
Jeff Sharkey0825ab22015-12-02 13:04:49 -07001395 /**
Jeff Sharkeycf3f0a12016-03-17 19:57:58 -06001396 * Return whether the calling user is running in an "unlocked" state.
1397 * <p>
1398 * On devices with direct boot, a user is unlocked only after they've
1399 * entered their credentials (such as a lock pattern or PIN). On devices
1400 * without direct boot, a user is unlocked as soon as it starts.
1401 * <p>
1402 * When a user is locked, only device-protected data storage is available.
1403 * When a user is unlocked, both device-protected and credential-protected
1404 * private app data storage is available.
Jeff Sharkey8a372a02016-03-16 16:25:45 -06001405 *
1406 * @see Intent#ACTION_USER_UNLOCKED
1407 * @see Context#createDeviceProtectedStorageContext()
Jeff Sharkeyb6423872015-12-11 11:11:58 -07001408 */
1409 public boolean isUserUnlocked() {
1410 return isUserUnlocked(Process.myUserHandle());
1411 }
1412
1413 /**
Jeff Sharkeycf3f0a12016-03-17 19:57:58 -06001414 * Return whether the given user is running in an "unlocked" state.
1415 * <p>
1416 * On devices with direct boot, a user is unlocked only after they've
1417 * entered their credentials (such as a lock pattern or PIN). On devices
1418 * without direct boot, a user is unlocked as soon as it starts.
1419 * <p>
1420 * When a user is locked, only device-protected data storage is available.
1421 * When a user is unlocked, both device-protected and credential-protected
1422 * private app data storage is available.
Fyodor Kupolovcdb3c2f2017-02-10 11:48:32 -08001423 * <p>Requires {@code android.permission.MANAGE_USERS} or
1424 * {@code android.permission.INTERACT_ACROSS_USERS}, otherwise specified {@link UserHandle user}
1425 * must be the calling user or a managed profile associated with it.
Jeff Sharkeyb6423872015-12-11 11:11:58 -07001426 *
1427 * @param user to retrieve the unlocked state for.
Jeff Sharkey8a372a02016-03-16 16:25:45 -06001428 * @see Intent#ACTION_USER_UNLOCKED
1429 * @see Context#createDeviceProtectedStorageContext()
Jeff Sharkeyb6423872015-12-11 11:11:58 -07001430 */
1431 public boolean isUserUnlocked(UserHandle user) {
Jeff Sharkey0999c0d2015-12-17 15:12:22 -07001432 return isUserUnlocked(user.getIdentifier());
1433 }
1434
1435 /** {@hide} */
Jeff Sharkey8588bc12016-01-06 16:47:42 -07001436 public boolean isUserUnlocked(@UserIdInt int userId) {
Jeff Sharkeyce18c812016-04-27 16:00:41 -06001437 try {
Fyodor Kupolov2e7e0962016-12-01 18:09:17 -08001438 return mService.isUserUnlocked(userId);
Jeff Sharkeyce18c812016-04-27 16:00:41 -06001439 } catch (RemoteException re) {
1440 throw re.rethrowFromSystemServer();
1441 }
1442 }
1443
1444 /** {@hide} */
1445 public boolean isUserUnlockingOrUnlocked(UserHandle user) {
1446 return isUserUnlockingOrUnlocked(user.getIdentifier());
1447 }
1448
1449 /** {@hide} */
1450 public boolean isUserUnlockingOrUnlocked(@UserIdInt int userId) {
1451 try {
Fyodor Kupolovc413f702016-10-06 17:11:14 -07001452 return mService.isUserUnlockingOrUnlocked(userId);
Jeff Sharkeyce18c812016-04-27 16:00:41 -06001453 } catch (RemoteException re) {
1454 throw re.rethrowFromSystemServer();
1455 }
Jeff Sharkeyb6423872015-12-11 11:11:58 -07001456 }
1457
1458 /**
Makoto Onuki73dded22017-12-20 13:14:48 +09001459 * Return the time when the calling user started in elapsed milliseconds since boot,
1460 * or 0 if not started.
1461 *
1462 * @hide
1463 */
1464 public long getUserStartRealtime() {
1465 try {
1466 return mService.getUserStartRealtime();
1467 } catch (RemoteException re) {
1468 throw re.rethrowFromSystemServer();
1469 }
1470 }
1471
1472 /**
1473 * Return the time when the calling user was unlocked elapsed milliseconds since boot,
1474 * or 0 if not unlocked.
1475 *
1476 * @hide
1477 */
1478 public long getUserUnlockRealtime() {
1479 try {
1480 return mService.getUserUnlockRealtime();
1481 } catch (RemoteException re) {
1482 throw re.rethrowFromSystemServer();
1483 }
1484 }
1485
1486 /**
Amith Yamasani258848d2012-08-10 17:06:33 -07001487 * Returns the UserInfo object describing a specific user.
Tony Mak8673b282016-03-21 21:10:59 +00001488 * Requires {@link android.Manifest.permission#MANAGE_USERS} permission.
Amith Yamasani258848d2012-08-10 17:06:33 -07001489 * @param userHandle the user handle of the user whose information is being requested.
1490 * @return the UserInfo object for a specific user.
1491 * @hide
Dianne Hackbornb26306a2012-10-24 15:22:21 -07001492 */
Jeff Sharkey8588bc12016-01-06 16:47:42 -07001493 public UserInfo getUserInfo(@UserIdInt int userHandle) {
Amith Yamasani258848d2012-08-10 17:06:33 -07001494 try {
1495 return mService.getUserInfo(userHandle);
1496 } catch (RemoteException re) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001497 throw re.rethrowFromSystemServer();
Amith Yamasani258848d2012-08-10 17:06:33 -07001498 }
1499 }
1500
Amith Yamasani71e6c692013-03-24 17:39:28 -07001501 /**
Zoltan Szatmary-Bane7834602016-04-08 18:41:11 +01001502 * @hide
1503 *
1504 * Returns who set a user restriction on a user.
Zoltan Szatmary-Bane7834602016-04-08 18:41:11 +01001505 * @param restrictionKey the string key representing the restriction
1506 * @param userHandle the UserHandle of the user for whom to retrieve the restrictions.
1507 * @return The source of user restriction. Any combination of {@link #RESTRICTION_NOT_SET},
1508 * {@link #RESTRICTION_SOURCE_SYSTEM}, {@link #RESTRICTION_SOURCE_DEVICE_OWNER}
1509 * and {@link #RESTRICTION_SOURCE_PROFILE_OWNER}
Pavel Grafov6a40f092016-10-25 15:46:51 +01001510 * @deprecated use {@link #getUserRestrictionSources(String, int)} instead.
Zoltan Szatmary-Bane7834602016-04-08 18:41:11 +01001511 */
Pavel Grafov6a40f092016-10-25 15:46:51 +01001512 @Deprecated
Zoltan Szatmary-Bane7834602016-04-08 18:41:11 +01001513 @SystemApi
1514 @UserRestrictionSource
Jeff Sharkeyd86b8fe2017-06-02 17:36:26 -06001515 @RequiresPermission(android.Manifest.permission.MANAGE_USERS)
Zoltan Szatmary-Bane7834602016-04-08 18:41:11 +01001516 public int getUserRestrictionSource(String restrictionKey, UserHandle userHandle) {
1517 try {
1518 return mService.getUserRestrictionSource(restrictionKey, userHandle.getIdentifier());
1519 } catch (RemoteException re) {
1520 throw re.rethrowFromSystemServer();
1521 }
1522 }
1523
1524 /**
Pavel Grafov6a40f092016-10-25 15:46:51 +01001525 * @hide
1526 *
1527 * Returns a list of users who set a user restriction on a given user.
Pavel Grafov6a40f092016-10-25 15:46:51 +01001528 * @param restrictionKey the string key representing the restriction
1529 * @param userHandle the UserHandle of the user for whom to retrieve the restrictions.
1530 * @return a list of user ids enforcing this restriction.
1531 */
1532 @SystemApi
Jeff Sharkeyd86b8fe2017-06-02 17:36:26 -06001533 @RequiresPermission(android.Manifest.permission.MANAGE_USERS)
Pavel Grafov6a40f092016-10-25 15:46:51 +01001534 public List<EnforcingUser> getUserRestrictionSources(
1535 String restrictionKey, UserHandle userHandle) {
1536 try {
1537 return mService.getUserRestrictionSources(restrictionKey, userHandle.getIdentifier());
1538 } catch (RemoteException re) {
1539 throw re.rethrowFromSystemServer();
1540 }
1541 }
1542
1543 /**
Amith Yamasani71e6c692013-03-24 17:39:28 -07001544 * Returns the user-wide restrictions imposed on this user.
1545 * @return a Bundle containing all the restrictions.
1546 */
Amith Yamasanie4cf7342012-12-17 11:12:09 -08001547 public Bundle getUserRestrictions() {
1548 return getUserRestrictions(Process.myUserHandle());
1549 }
1550
Amith Yamasani71e6c692013-03-24 17:39:28 -07001551 /**
1552 * Returns the user-wide restrictions imposed on the user specified by <code>userHandle</code>.
1553 * @param userHandle the UserHandle of the user for whom to retrieve the restrictions.
1554 * @return a Bundle containing all the restrictions.
1555 */
Amith Yamasanie4cf7342012-12-17 11:12:09 -08001556 public Bundle getUserRestrictions(UserHandle userHandle) {
1557 try {
1558 return mService.getUserRestrictions(userHandle.getIdentifier());
1559 } catch (RemoteException re) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001560 throw re.rethrowFromSystemServer();
Amith Yamasanie4cf7342012-12-17 11:12:09 -08001561 }
1562 }
1563
Zoltan Szatmary-Ban3bbcedd2015-11-26 13:45:51 +00001564 /**
1565 * @hide
1566 * Returns whether the given user has been disallowed from performing certain actions
1567 * or setting certain settings through UserManager. This method disregards restrictions
1568 * set by device policy.
1569 * @param restrictionKey the string key representing the restriction
1570 * @param userHandle the UserHandle of the user for whom to retrieve the restrictions.
1571 */
1572 public boolean hasBaseUserRestriction(String restrictionKey, UserHandle userHandle) {
1573 try {
1574 return mService.hasBaseUserRestriction(restrictionKey, userHandle.getIdentifier());
1575 } catch (RemoteException re) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001576 throw re.rethrowFromSystemServer();
Zoltan Szatmary-Ban3bbcedd2015-11-26 13:45:51 +00001577 }
1578 }
1579
Amith Yamasani71e6c692013-03-24 17:39:28 -07001580 /**
Makoto Onukia3c12502015-10-28 10:18:32 -07001581 * This will no longer work. Device owners and profile owners should use
1582 * {@link DevicePolicyManager#addUserRestriction(ComponentName, String)} instead.
Amith Yamasani71e6c692013-03-24 17:39:28 -07001583 */
Makoto Onukia3c12502015-10-28 10:18:32 -07001584 // System apps should use UserManager.setUserRestriction() instead.
Julia Reynolds3d9eb782014-08-11 16:40:08 -04001585 @Deprecated
Amith Yamasanie4cf7342012-12-17 11:12:09 -08001586 public void setUserRestrictions(Bundle restrictions) {
Makoto Onuki068c54a2015-10-13 14:34:03 -07001587 throw new UnsupportedOperationException("This method is no longer supported");
Amith Yamasanie4cf7342012-12-17 11:12:09 -08001588 }
1589
Amith Yamasani71e6c692013-03-24 17:39:28 -07001590 /**
Makoto Onukia3c12502015-10-28 10:18:32 -07001591 * This will no longer work. Device owners and profile owners should use
1592 * {@link DevicePolicyManager#addUserRestriction(ComponentName, String)} instead.
Amith Yamasani71e6c692013-03-24 17:39:28 -07001593 */
Makoto Onukia3c12502015-10-28 10:18:32 -07001594 // System apps should use UserManager.setUserRestriction() instead.
Julia Reynolds3d9eb782014-08-11 16:40:08 -04001595 @Deprecated
Amith Yamasanie4cf7342012-12-17 11:12:09 -08001596 public void setUserRestrictions(Bundle restrictions, UserHandle userHandle) {
Makoto Onuki068c54a2015-10-13 14:34:03 -07001597 throw new UnsupportedOperationException("This method is no longer supported");
Amith Yamasanie4cf7342012-12-17 11:12:09 -08001598 }
1599
Amith Yamasani71e6c692013-03-24 17:39:28 -07001600 /**
1601 * Sets the value of a specific restriction.
Amith Yamasanibe465322014-04-24 13:45:17 -07001602 * Requires the MANAGE_USERS permission.
Amith Yamasani71e6c692013-03-24 17:39:28 -07001603 * @param key the key of the restriction
1604 * @param value the value for the restriction
Julia Reynolds3d9eb782014-08-11 16:40:08 -04001605 * @deprecated use {@link android.app.admin.DevicePolicyManager#addUserRestriction(
1606 * android.content.ComponentName, String)} or
1607 * {@link android.app.admin.DevicePolicyManager#clearUserRestriction(
1608 * android.content.ComponentName, String)} instead.
Amith Yamasani71e6c692013-03-24 17:39:28 -07001609 */
Julia Reynolds3d9eb782014-08-11 16:40:08 -04001610 @Deprecated
Amith Yamasani71e6c692013-03-24 17:39:28 -07001611 public void setUserRestriction(String key, boolean value) {
Makoto Onuki068c54a2015-10-13 14:34:03 -07001612 setUserRestriction(key, value, Process.myUserHandle());
Amith Yamasani71e6c692013-03-24 17:39:28 -07001613 }
1614
1615 /**
1616 * @hide
1617 * Sets the value of a specific restriction on a specific user.
Amith Yamasanibe465322014-04-24 13:45:17 -07001618 * Requires the MANAGE_USERS permission.
Amith Yamasani71e6c692013-03-24 17:39:28 -07001619 * @param key the key of the restriction
1620 * @param value the value for the restriction
1621 * @param userHandle the user whose restriction is to be changed.
Julia Reynolds3d9eb782014-08-11 16:40:08 -04001622 * @deprecated use {@link android.app.admin.DevicePolicyManager#addUserRestriction(
1623 * android.content.ComponentName, String)} or
1624 * {@link android.app.admin.DevicePolicyManager#clearUserRestriction(
1625 * android.content.ComponentName, String)} instead.
Amith Yamasani71e6c692013-03-24 17:39:28 -07001626 */
Julia Reynolds3d9eb782014-08-11 16:40:08 -04001627 @Deprecated
Maggie Benthall67944582013-02-22 14:58:27 -05001628 public void setUserRestriction(String key, boolean value, UserHandle userHandle) {
Fyodor Kupolovb5013302015-04-17 17:59:14 -07001629 try {
1630 mService.setUserRestriction(key, value, userHandle.getIdentifier());
1631 } catch (RemoteException re) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001632 throw re.rethrowFromSystemServer();
Fyodor Kupolovb5013302015-04-17 17:59:14 -07001633 }
Maggie Benthall67944582013-02-22 14:58:27 -05001634 }
1635
Amith Yamasani258848d2012-08-10 17:06:33 -07001636 /**
Maggie Benthalla12fccf2013-03-14 18:02:12 -04001637 * Returns whether the current user has been disallowed from performing certain actions
1638 * or setting certain settings.
Julia Reynolds2b2cf722014-06-06 11:41:04 -04001639 *
1640 * @param restrictionKey The string key representing the restriction.
1641 * @return {@code true} if the current user has the given restriction, {@code false} otherwise.
Maggie Benthalla12fccf2013-03-14 18:02:12 -04001642 */
1643 public boolean hasUserRestriction(String restrictionKey) {
David Christieb12ba932013-09-03 17:15:28 -07001644 return hasUserRestriction(restrictionKey, Process.myUserHandle());
1645 }
1646
1647 /**
1648 * @hide
1649 * Returns whether the given user has been disallowed from performing certain actions
1650 * or setting certain settings.
1651 * @param restrictionKey the string key representing the restriction
1652 * @param userHandle the UserHandle of the user for whom to retrieve the restrictions.
1653 */
1654 public boolean hasUserRestriction(String restrictionKey, UserHandle userHandle) {
Amith Yamasani8cd28b52014-06-08 17:54:27 -07001655 try {
1656 return mService.hasUserRestriction(restrictionKey,
1657 userHandle.getIdentifier());
1658 } catch (RemoteException re) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001659 throw re.rethrowFromSystemServer();
Amith Yamasani8cd28b52014-06-08 17:54:27 -07001660 }
Maggie Benthalla12fccf2013-03-14 18:02:12 -04001661 }
1662
1663 /**
Dianne Hackborn33f9cb82012-10-04 17:15:10 -07001664 * Return the serial number for a user. This is a device-unique
Dianne Hackbornb26306a2012-10-24 15:22:21 -07001665 * number assigned to that user; if the user is deleted and then a new
1666 * user created, the new users will not be given the same serial number.
Dianne Hackborn33f9cb82012-10-04 17:15:10 -07001667 * @param user The user whose serial number is to be retrieved.
Dianne Hackbornb26306a2012-10-24 15:22:21 -07001668 * @return The serial number of the given user; returns -1 if the
1669 * given UserHandle does not exist.
Dianne Hackborn33f9cb82012-10-04 17:15:10 -07001670 * @see #getUserForSerialNumber(long)
1671 */
1672 public long getSerialNumberForUser(UserHandle user) {
1673 return getUserSerialNumber(user.getIdentifier());
1674 }
1675
1676 /**
1677 * Return the user associated with a serial number previously
1678 * returned by {@link #getSerialNumberForUser(UserHandle)}.
1679 * @param serialNumber The serial number of the user that is being
1680 * retrieved.
1681 * @return Return the user associated with the serial number, or null
1682 * if there is not one.
1683 * @see #getSerialNumberForUser(UserHandle)
1684 */
1685 public UserHandle getUserForSerialNumber(long serialNumber) {
Fyodor Kupolovef249092015-05-06 13:18:46 -07001686 int ident = getUserHandle((int) serialNumber);
Dianne Hackborn33f9cb82012-10-04 17:15:10 -07001687 return ident >= 0 ? new UserHandle(ident) : null;
1688 }
1689
1690 /**
Xiaohui Chencfe64c82015-07-16 14:30:50 -07001691 * Creates a user with the specified name and options. For non-admin users, default user
1692 * restrictions are going to be applied.
Amith Yamasani195263742012-08-21 15:40:12 -07001693 * Requires {@link android.Manifest.permission#MANAGE_USERS} permission.
Amith Yamasani258848d2012-08-10 17:06:33 -07001694 *
1695 * @param name the user's name
1696 * @param flags flags that identify the type of user and other properties.
1697 * @see UserInfo
1698 *
1699 * @return the UserInfo object for the created user, or null if the user could not be created.
1700 * @hide
1701 */
1702 public UserInfo createUser(String name, int flags) {
Xiaohui Chencfe64c82015-07-16 14:30:50 -07001703 UserInfo user = null;
Amith Yamasani258848d2012-08-10 17:06:33 -07001704 try {
Xiaohui Chencfe64c82015-07-16 14:30:50 -07001705 user = mService.createUser(name, flags);
phweisse9c44062016-02-10 12:57:38 +01001706 // TODO: Keep this in sync with
1707 // UserManagerService.LocalService.createUserEvenWhenDisallowed
Christine Franks97a54802017-08-09 10:06:43 -07001708 if (user != null && !user.isAdmin() && !user.isDemo()) {
Makoto Onuki068c54a2015-10-13 14:34:03 -07001709 mService.setUserRestriction(DISALLOW_SMS, true, user.id);
1710 mService.setUserRestriction(DISALLOW_OUTGOING_CALLS, true, user.id);
Xiaohui Chencfe64c82015-07-16 14:30:50 -07001711 }
Amith Yamasani258848d2012-08-10 17:06:33 -07001712 } catch (RemoteException re) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001713 throw re.rethrowFromSystemServer();
Amith Yamasani258848d2012-08-10 17:06:33 -07001714 }
Xiaohui Chencfe64c82015-07-16 14:30:50 -07001715 return user;
Amith Yamasani258848d2012-08-10 17:06:33 -07001716 }
1717
1718 /**
Amith Yamasani1e9c2182014-06-11 17:25:51 -07001719 * Creates a guest user and configures it.
1720 * @param context an application context
1721 * @param name the name to set for the user
1722 * @hide
1723 */
1724 public UserInfo createGuest(Context context, String name) {
Makoto Onuki068c54a2015-10-13 14:34:03 -07001725 UserInfo guest = null;
1726 try {
1727 guest = mService.createUser(name, UserInfo.FLAG_GUEST);
1728 if (guest != null) {
1729 Settings.Secure.putStringForUser(context.getContentResolver(),
1730 Settings.Secure.SKIP_FIRST_USE_HINTS, "1", guest.id);
Amith Yamasanibf3a9462014-07-28 14:26:42 -07001731 }
Makoto Onuki068c54a2015-10-13 14:34:03 -07001732 } catch (RemoteException re) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001733 throw re.rethrowFromSystemServer();
Amith Yamasani1e9c2182014-06-11 17:25:51 -07001734 }
1735 return guest;
1736 }
1737
Amith Yamasaniaa6634e2014-10-06 14:20:28 -07001738 /**
Kenny Guy2a764942014-04-02 13:29:20 +01001739 * Creates a user with the specified name and options as a profile of another user.
Kenny Guya52dc3e2014-02-11 15:33:14 +00001740 * Requires {@link android.Manifest.permission#MANAGE_USERS} permission.
1741 *
1742 * @param name the user's name
1743 * @param flags flags that identify the type of user and other properties.
Sudheer Shanka7cb54a32016-09-16 12:59:05 -07001744 * @param userHandle new user will be a profile of this user.
Kenny Guya52dc3e2014-02-11 15:33:14 +00001745 *
Sudheer Shanka7cb54a32016-09-16 12:59:05 -07001746 * @return the {@link UserInfo} object for the created user, or null if the user
1747 * could not be created.
Kenny Guya52dc3e2014-02-11 15:33:14 +00001748 * @hide
1749 */
Jeff Sharkey8588bc12016-01-06 16:47:42 -07001750 public UserInfo createProfileForUser(String name, int flags, @UserIdInt int userHandle) {
Sudheer Shanka7cb54a32016-09-16 12:59:05 -07001751 return createProfileForUser(name, flags, userHandle, null);
1752 }
1753
1754 /**
1755 * Version of {@link #createProfileForUser(String, int, int)} that allows you to specify
1756 * any packages that should not be installed in the new profile by default, these packages can
1757 * still be installed later by the user if needed.
1758 *
1759 * @param name the user's name
1760 * @param flags flags that identify the type of user and other properties.
1761 * @param userHandle new user will be a profile of this user.
1762 * @param disallowedPackages packages that will not be installed in the profile being created.
1763 *
1764 * @return the {@link UserInfo} object for the created user, or null if the user
1765 * could not be created.
1766 * @hide
1767 */
1768 public UserInfo createProfileForUser(String name, int flags, @UserIdInt int userHandle,
1769 String[] disallowedPackages) {
Kenny Guya52dc3e2014-02-11 15:33:14 +00001770 try {
Sudheer Shanka7cb54a32016-09-16 12:59:05 -07001771 return mService.createProfileForUser(name, flags, userHandle, disallowedPackages);
Kenny Guya52dc3e2014-02-11 15:33:14 +00001772 } catch (RemoteException re) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001773 throw re.rethrowFromSystemServer();
Kenny Guya52dc3e2014-02-11 15:33:14 +00001774 }
1775 }
1776
1777 /**
Tony Mak6dc428f2016-10-10 15:48:27 +01001778 * Similar to {@link #createProfileForUser(String, int, int, String[])}
Esteban Talavera6c9116a2016-11-24 16:12:44 +00001779 * except bypassing the checking of {@link UserManager#DISALLOW_ADD_MANAGED_PROFILE}.
Tony Mak6dc428f2016-10-10 15:48:27 +01001780 * Requires {@link android.Manifest.permission#MANAGE_USERS} permission.
1781 *
1782 * @see #createProfileForUser(String, int, int, String[])
1783 * @hide
1784 */
1785 public UserInfo createProfileForUserEvenWhenDisallowed(String name, int flags,
1786 @UserIdInt int userHandle, String[] disallowedPackages) {
1787 try {
1788 return mService.createProfileForUserEvenWhenDisallowed(name, flags, userHandle,
1789 disallowedPackages);
1790 } catch (RemoteException re) {
1791 throw re.rethrowFromSystemServer();
1792 }
1793 }
1794
1795 /**
Fyodor Kupolov02cb6e72015-09-18 18:20:55 -07001796 * Creates a restricted profile with the specified name. This method also sets necessary
1797 * restrictions and adds shared accounts.
Fyodor Kupolov06a484a2015-08-21 16:33:20 -07001798 *
1799 * @param name profile's name
1800 * @return UserInfo object for the created user, or null if the user could not be created.
1801 * @hide
1802 */
1803 public UserInfo createRestrictedProfile(String name) {
1804 try {
Fyodor Kupolov02cb6e72015-09-18 18:20:55 -07001805 UserHandle parentUserHandle = Process.myUserHandle();
1806 UserInfo user = mService.createRestrictedProfile(name,
1807 parentUserHandle.getIdentifier());
1808 if (user != null) {
1809 AccountManager.get(mContext).addSharedAccountsFromParentUser(parentUserHandle,
1810 UserHandle.of(user.id));
Fyodor Kupolov06a484a2015-08-21 16:33:20 -07001811 }
Fyodor Kupolov02cb6e72015-09-18 18:20:55 -07001812 return user;
Jeff Sharkey27b2e692016-02-25 17:40:12 -07001813 } catch (RemoteException re) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001814 throw re.rethrowFromSystemServer();
Fyodor Kupolov06a484a2015-08-21 16:33:20 -07001815 }
Fyodor Kupolov06a484a2015-08-21 16:33:20 -07001816 }
1817
1818 /**
Amith Yamasani06964342016-04-15 13:55:01 -07001819 * Returns an intent to create a user for the provided name and account name. The name
1820 * and account name will be used when the setup process for the new user is started.
1821 * <p>
Amith Yamasani12747872015-12-07 14:19:49 -08001822 * The intent should be launched using startActivityForResult and the return result will
Amith Yamasani37ed8d12016-01-27 14:40:16 -08001823 * indicate if the user consented to adding a new user and if the operation succeeded. Any
1824 * errors in creating the user will be returned in the result code. If the user cancels the
1825 * request, the return result will be {@link Activity#RESULT_CANCELED}. On success, the
1826 * result code will be {@link Activity#RESULT_OK}.
Amith Yamasani06964342016-04-15 13:55:01 -07001827 * <p>
1828 * Use {@link #supportsMultipleUsers()} to first check if the device supports this operation
1829 * at all.
1830 * <p>
Amith Yamasani12747872015-12-07 14:19:49 -08001831 * The new user is created but not initialized. After switching into the user for the first
1832 * time, the preferred user name and account information are used by the setup process for that
1833 * user.
1834 *
1835 * @param userName Optional name to assign to the user.
Amith Yamasani06964342016-04-15 13:55:01 -07001836 * @param accountName Optional account name that will be used by the setup wizard to initialize
Amith Yamasani12747872015-12-07 14:19:49 -08001837 * the user.
1838 * @param accountType Optional account type for the account to be created. This is required
1839 * if the account name is specified.
1840 * @param accountOptions Optional bundle of data to be passed in during account creation in the
1841 * new user via {@link AccountManager#addAccount(String, String, String[],
1842 * Bundle, android.app.Activity, android.accounts.AccountManagerCallback,
1843 * Handler)}.
Amith Yamasani06964342016-04-15 13:55:01 -07001844 * @return An Intent that can be launched from an Activity.
Amith Yamasani37ed8d12016-01-27 14:40:16 -08001845 * @see #USER_CREATION_FAILED_NOT_PERMITTED
1846 * @see #USER_CREATION_FAILED_NO_MORE_USERS
Amith Yamasani06964342016-04-15 13:55:01 -07001847 * @see #supportsMultipleUsers
Amith Yamasani12747872015-12-07 14:19:49 -08001848 */
1849 public static Intent createUserCreationIntent(@Nullable String userName,
1850 @Nullable String accountName,
1851 @Nullable String accountType, @Nullable PersistableBundle accountOptions) {
Amith Yamasani12747872015-12-07 14:19:49 -08001852 Intent intent = new Intent(ACTION_CREATE_USER);
1853 if (userName != null) {
1854 intent.putExtra(EXTRA_USER_NAME, userName);
1855 }
1856 if (accountName != null && accountType == null) {
1857 throw new IllegalArgumentException("accountType must be specified if accountName is "
1858 + "specified");
1859 }
1860 if (accountName != null) {
1861 intent.putExtra(EXTRA_USER_ACCOUNT_NAME, accountName);
1862 }
1863 if (accountType != null) {
1864 intent.putExtra(EXTRA_USER_ACCOUNT_TYPE, accountType);
1865 }
1866 if (accountOptions != null) {
1867 intent.putExtra(EXTRA_USER_ACCOUNT_OPTIONS, accountOptions);
1868 }
1869 return intent;
1870 }
1871
1872 /**
1873 * @hide
1874 *
Jeff Sharkeyd86b8fe2017-06-02 17:36:26 -06001875 * Returns the preferred account name for user creation.
Amith Yamasani12747872015-12-07 14:19:49 -08001876 */
1877 @SystemApi
Jeff Sharkeyd86b8fe2017-06-02 17:36:26 -06001878 @RequiresPermission(android.Manifest.permission.MANAGE_USERS)
Amith Yamasani12747872015-12-07 14:19:49 -08001879 public String getSeedAccountName() {
1880 try {
1881 return mService.getSeedAccountName();
1882 } catch (RemoteException re) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001883 throw re.rethrowFromSystemServer();
Amith Yamasani12747872015-12-07 14:19:49 -08001884 }
1885 }
1886
1887 /**
1888 * @hide
1889 *
Jeff Sharkeyd86b8fe2017-06-02 17:36:26 -06001890 * Returns the preferred account type for user creation.
Amith Yamasani12747872015-12-07 14:19:49 -08001891 */
1892 @SystemApi
Jeff Sharkeyd86b8fe2017-06-02 17:36:26 -06001893 @RequiresPermission(android.Manifest.permission.MANAGE_USERS)
Amith Yamasani12747872015-12-07 14:19:49 -08001894 public String getSeedAccountType() {
1895 try {
1896 return mService.getSeedAccountType();
1897 } catch (RemoteException re) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001898 throw re.rethrowFromSystemServer();
Amith Yamasani12747872015-12-07 14:19:49 -08001899 }
1900 }
1901
1902 /**
1903 * @hide
1904 *
Jeff Sharkeyd86b8fe2017-06-02 17:36:26 -06001905 * Returns the preferred account's options bundle for user creation.
Amith Yamasani12747872015-12-07 14:19:49 -08001906 * @return Any options set by the requestor that created the user.
1907 */
1908 @SystemApi
Jeff Sharkeyd86b8fe2017-06-02 17:36:26 -06001909 @RequiresPermission(android.Manifest.permission.MANAGE_USERS)
Amith Yamasani12747872015-12-07 14:19:49 -08001910 public PersistableBundle getSeedAccountOptions() {
1911 try {
1912 return mService.getSeedAccountOptions();
1913 } catch (RemoteException re) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001914 throw re.rethrowFromSystemServer();
Amith Yamasani12747872015-12-07 14:19:49 -08001915 }
1916 }
1917
1918 /**
1919 * @hide
1920 *
1921 * Called by a system activity to set the seed account information of a user created
1922 * through the user creation intent.
1923 * @param userId
1924 * @param accountName
1925 * @param accountType
1926 * @param accountOptions
1927 * @see #createUserCreationIntent(String, String, String, PersistableBundle)
1928 */
1929 public void setSeedAccountData(int userId, String accountName, String accountType,
1930 PersistableBundle accountOptions) {
1931 try {
1932 mService.setSeedAccountData(userId, accountName, accountType, accountOptions,
1933 /* persist= */ true);
1934 } catch (RemoteException re) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001935 throw re.rethrowFromSystemServer();
Amith Yamasani12747872015-12-07 14:19:49 -08001936 }
1937 }
1938
1939 /**
1940 * @hide
Jeff Sharkeyd86b8fe2017-06-02 17:36:26 -06001941 * Clears the seed information used to create this user.
Amith Yamasani12747872015-12-07 14:19:49 -08001942 */
1943 @SystemApi
Jeff Sharkeyd86b8fe2017-06-02 17:36:26 -06001944 @RequiresPermission(android.Manifest.permission.MANAGE_USERS)
Amith Yamasani12747872015-12-07 14:19:49 -08001945 public void clearSeedAccountData() {
1946 try {
1947 mService.clearSeedAccountData();
1948 } catch (RemoteException re) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001949 throw re.rethrowFromSystemServer();
Amith Yamasani12747872015-12-07 14:19:49 -08001950 }
1951 }
1952
1953 /**
Amith Yamasani1df14732014-08-29 21:37:27 -07001954 * @hide
1955 * Marks the guest user for deletion to allow a new guest to be created before deleting
1956 * the current user who is a guest.
1957 * @param userHandle
1958 * @return
1959 */
Jeff Sharkey8588bc12016-01-06 16:47:42 -07001960 public boolean markGuestForDeletion(@UserIdInt int userHandle) {
Amith Yamasani1df14732014-08-29 21:37:27 -07001961 try {
1962 return mService.markGuestForDeletion(userHandle);
1963 } catch (RemoteException re) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001964 throw re.rethrowFromSystemServer();
Amith Yamasani1df14732014-08-29 21:37:27 -07001965 }
1966 }
1967
1968 /**
Alexandra Gherghinadf35d572014-04-09 13:54:39 +01001969 * Sets the user as enabled, if such an user exists.
Lenka Trochtova1ddda472016-02-12 10:42:12 +01001970 *
1971 * <p>Requires {@link android.Manifest.permission#MANAGE_USERS} permission.
1972 *
1973 * <p>Note that the default is true, it's only that managed profiles might not be enabled.
1974 * Also ephemeral users can be disabled to indicate that their removal is in progress and they
1975 * shouldn't be re-entered. Therefore ephemeral users should not be re-enabled once disabled.
Alexandra Gherghinadf35d572014-04-09 13:54:39 +01001976 *
1977 * @param userHandle the id of the profile to enable
1978 * @hide
1979 */
Jeff Sharkey8588bc12016-01-06 16:47:42 -07001980 public void setUserEnabled(@UserIdInt int userHandle) {
Alexandra Gherghinadf35d572014-04-09 13:54:39 +01001981 try {
1982 mService.setUserEnabled(userHandle);
Jeff Sharkey27b2e692016-02-25 17:40:12 -07001983 } catch (RemoteException re) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001984 throw re.rethrowFromSystemServer();
Alexandra Gherghinadf35d572014-04-09 13:54:39 +01001985 }
1986 }
1987
1988 /**
Andrew Scull85a63bc2016-10-24 13:47:47 +01001989 * Evicts the user's credential encryption key from memory by stopping and restarting the user.
1990 *
1991 * @hide
1992 */
1993 public void evictCredentialEncryptionKey(@UserIdInt int userHandle) {
1994 try {
1995 mService.evictCredentialEncryptionKey(userHandle);
1996 } catch (RemoteException re) {
1997 throw re.rethrowFromSystemServer();
1998 }
1999 }
2000
2001 /**
Dianne Hackbornb26306a2012-10-24 15:22:21 -07002002 * Return the number of users currently created on the device.
2003 */
2004 public int getUserCount() {
2005 List<UserInfo> users = getUsers();
2006 return users != null ? users.size() : 1;
2007 }
2008
2009 /**
Amith Yamasanid04aaa32016-06-13 12:09:36 -07002010 * Returns information for all users on this device, including ones marked for deletion.
2011 * To retrieve only users that are alive, use {@link #getUsers(boolean)}.
2012 * <p>
Amith Yamasani195263742012-08-21 15:40:12 -07002013 * Requires {@link android.Manifest.permission#MANAGE_USERS} permission.
Fyodor Kupolov940e8572016-01-26 12:03:51 -08002014 * @return the list of users that exist on the device.
Amith Yamasani258848d2012-08-10 17:06:33 -07002015 * @hide
2016 */
2017 public List<UserInfo> getUsers() {
2018 try {
Amith Yamasani920ace02012-09-20 22:15:37 -07002019 return mService.getUsers(false);
2020 } catch (RemoteException re) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07002021 throw re.rethrowFromSystemServer();
Amith Yamasani920ace02012-09-20 22:15:37 -07002022 }
2023 }
2024
2025 /**
Fyodor Kupolov940e8572016-01-26 12:03:51 -08002026 * Returns serial numbers of all users on this device.
Fyodor Kupolov940e8572016-01-26 12:03:51 -08002027 *
2028 * @param excludeDying specify if the list should exclude users being removed.
2029 * @return the list of serial numbers of users that exist on the device.
2030 * @hide
2031 */
2032 @SystemApi
Jeff Sharkeyd86b8fe2017-06-02 17:36:26 -06002033 @RequiresPermission(android.Manifest.permission.MANAGE_USERS)
Fyodor Kupolov940e8572016-01-26 12:03:51 -08002034 public long[] getSerialNumbersOfUsers(boolean excludeDying) {
2035 try {
2036 List<UserInfo> users = mService.getUsers(excludeDying);
2037 long[] result = new long[users.size()];
2038 for (int i = 0; i < result.length; i++) {
2039 result[i] = users.get(i).serialNumber;
2040 }
2041 return result;
2042 } catch (RemoteException re) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07002043 throw re.rethrowFromSystemServer();
Fyodor Kupolov940e8572016-01-26 12:03:51 -08002044 }
2045 }
2046
2047 /**
Xiaohui Chenb3b92582015-12-07 11:22:13 -08002048 * @return the user's account name, null if not found.
2049 * @hide
2050 */
2051 @RequiresPermission( allOf = {
2052 Manifest.permission.INTERACT_ACROSS_USERS_FULL,
2053 Manifest.permission.MANAGE_USERS
2054 })
Jeff Sharkey8588bc12016-01-06 16:47:42 -07002055 public @Nullable String getUserAccount(@UserIdInt int userHandle) {
Xiaohui Chenb3b92582015-12-07 11:22:13 -08002056 try {
2057 return mService.getUserAccount(userHandle);
2058 } catch (RemoteException re) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07002059 throw re.rethrowFromSystemServer();
Xiaohui Chenb3b92582015-12-07 11:22:13 -08002060 }
2061 }
2062
2063 /**
2064 * Set account name for the given user.
2065 * @hide
2066 */
2067 @RequiresPermission( allOf = {
2068 Manifest.permission.INTERACT_ACROSS_USERS_FULL,
2069 Manifest.permission.MANAGE_USERS
2070 })
Jeff Sharkey8588bc12016-01-06 16:47:42 -07002071 public void setUserAccount(@UserIdInt int userHandle, @Nullable String accountName) {
Xiaohui Chenb3b92582015-12-07 11:22:13 -08002072 try {
2073 mService.setUserAccount(userHandle, accountName);
2074 } catch (RemoteException re) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07002075 throw re.rethrowFromSystemServer();
Xiaohui Chenb3b92582015-12-07 11:22:13 -08002076 }
2077 }
2078
2079 /**
Xiaohui Chen70f6c382015-04-28 14:21:43 -07002080 * Returns information for Primary user.
2081 * Requires {@link android.Manifest.permission#MANAGE_USERS} permission.
2082 *
2083 * @return the Primary user, null if not found.
2084 * @hide
2085 */
Xiaohui Chen7cb69df2015-07-13 16:01:01 -07002086 public @Nullable UserInfo getPrimaryUser() {
Xiaohui Chen70f6c382015-04-28 14:21:43 -07002087 try {
2088 return mService.getPrimaryUser();
2089 } catch (RemoteException re) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07002090 throw re.rethrowFromSystemServer();
Xiaohui Chen70f6c382015-04-28 14:21:43 -07002091 }
2092 }
2093
2094 /**
Amith Yamasani95ab7842014-08-11 17:09:26 -07002095 * Checks whether it's possible to add more users. Caller must hold the MANAGE_USERS
2096 * permission.
2097 *
2098 * @return true if more users can be added, false if limit has been reached.
2099 * @hide
2100 */
2101 public boolean canAddMoreUsers() {
2102 final List<UserInfo> users = getUsers(true);
2103 final int totalUserCount = users.size();
2104 int aliveUserCount = 0;
2105 for (int i = 0; i < totalUserCount; i++) {
2106 UserInfo user = users.get(i);
2107 if (!user.isGuest()) {
2108 aliveUserCount++;
2109 }
2110 }
2111 return aliveUserCount < getMaxSupportedUsers();
2112 }
2113
2114 /**
Nicolas Prevot72434b72015-05-13 12:15:03 -07002115 * Checks whether it's possible to add more managed profiles. Caller must hold the MANAGE_USERS
2116 * permission.
Nicolas Prevot07387fe2015-10-30 17:53:30 +00002117 * if allowedToRemoveOne is true and if the user already has a managed profile, then return if
2118 * we could add a new managed profile to this user after removing the existing one.
Nicolas Prevot72434b72015-05-13 12:15:03 -07002119 *
2120 * @return true if more managed profiles can be added, false if limit has been reached.
2121 * @hide
2122 */
Jeff Sharkey8588bc12016-01-06 16:47:42 -07002123 public boolean canAddMoreManagedProfiles(@UserIdInt int userId, boolean allowedToRemoveOne) {
Nicolas Prevot72434b72015-05-13 12:15:03 -07002124 try {
Nicolas Prevot07387fe2015-10-30 17:53:30 +00002125 return mService.canAddMoreManagedProfiles(userId, allowedToRemoveOne);
Nicolas Prevot72434b72015-05-13 12:15:03 -07002126 } catch (RemoteException re) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07002127 throw re.rethrowFromSystemServer();
Nicolas Prevot72434b72015-05-13 12:15:03 -07002128 }
2129 }
2130
2131 /**
Kenny Guy2a764942014-04-02 13:29:20 +01002132 * Returns list of the profiles of userHandle including
2133 * userHandle itself.
Amith Yamasani4f7e2e32014-08-14 18:49:48 -07002134 * Note that this returns both enabled and not enabled profiles. See
Ruben Brunk7f75da22015-04-30 17:46:30 -07002135 * {@link #getEnabledProfiles(int)} if you need only the enabled ones.
Amith Yamasani4f582632014-02-19 14:31:52 -08002136 *
Kenny Guy2a764942014-04-02 13:29:20 +01002137 * Requires {@link android.Manifest.permission#MANAGE_USERS} permission.
2138 * @param userHandle profiles of this user will be returned.
2139 * @return the list of profiles.
2140 * @hide
2141 */
Jeff Sharkey8588bc12016-01-06 16:47:42 -07002142 public List<UserInfo> getProfiles(@UserIdInt int userHandle) {
Kenny Guya52dc3e2014-02-11 15:33:14 +00002143 try {
Alexandra Gherghina385124d2014-04-03 13:37:39 +01002144 return mService.getProfiles(userHandle, false /* enabledOnly */);
Kenny Guya52dc3e2014-02-11 15:33:14 +00002145 } catch (RemoteException re) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07002146 throw re.rethrowFromSystemServer();
Kenny Guya52dc3e2014-02-11 15:33:14 +00002147 }
2148 }
2149
2150 /**
Xiaohui Chenfd5b7742015-10-14 15:47:04 -07002151 * Requires {@link android.Manifest.permission#MANAGE_USERS} permission.
2152 * @param userId one of the two user ids to check.
2153 * @param otherUserId one of the two user ids to check.
2154 * @return true if the two user ids are in the same profile group.
2155 * @hide
2156 */
Jeff Sharkey8588bc12016-01-06 16:47:42 -07002157 public boolean isSameProfileGroup(@UserIdInt int userId, int otherUserId) {
Xiaohui Chenfd5b7742015-10-14 15:47:04 -07002158 try {
2159 return mService.isSameProfileGroup(userId, otherUserId);
2160 } catch (RemoteException re) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07002161 throw re.rethrowFromSystemServer();
Xiaohui Chenfd5b7742015-10-14 15:47:04 -07002162 }
2163 }
2164
2165 /**
Ruben Brunk7f75da22015-04-30 17:46:30 -07002166 * Returns list of the profiles of userHandle including
2167 * userHandle itself.
2168 * Note that this returns only enabled.
2169 *
2170 * Requires {@link android.Manifest.permission#MANAGE_USERS} permission.
2171 * @param userHandle profiles of this user will be returned.
2172 * @return the list of profiles.
2173 * @hide
2174 */
Jeff Sharkey8588bc12016-01-06 16:47:42 -07002175 public List<UserInfo> getEnabledProfiles(@UserIdInt int userHandle) {
Ruben Brunk7f75da22015-04-30 17:46:30 -07002176 try {
2177 return mService.getProfiles(userHandle, true /* enabledOnly */);
2178 } catch (RemoteException re) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07002179 throw re.rethrowFromSystemServer();
Ruben Brunk7f75da22015-04-30 17:46:30 -07002180 }
2181 }
2182
2183 /**
Jessica Hummelbe81c802014-04-22 15:49:22 +01002184 * Returns a list of UserHandles for profiles associated with the user that the calling process
2185 * is running on, including the user itself.
Amith Yamasani4f582632014-02-19 14:31:52 -08002186 *
2187 * @return A non-empty list of UserHandles associated with the calling user.
2188 */
2189 public List<UserHandle> getUserProfiles() {
Fyodor Kupolov7f98aa42016-04-07 14:56:25 -07002190 int[] userIds = getProfileIds(UserHandle.myUserId(), true /* enabledOnly */);
2191 List<UserHandle> result = new ArrayList<>(userIds.length);
2192 for (int userId : userIds) {
2193 result.add(UserHandle.of(userId));
2194 }
2195 return result;
2196 }
2197
2198 /**
2199 * Returns a list of ids for profiles associated with the specified user including the user
2200 * itself.
2201 *
2202 * @param userId id of the user to return profiles for
2203 * @param enabledOnly whether return only {@link UserInfo#isEnabled() enabled} profiles
2204 * @return A non-empty list of ids of profiles associated with the specified user.
2205 *
2206 * @hide
2207 */
2208 public int[] getProfileIds(@UserIdInt int userId, boolean enabledOnly) {
Alexandra Gherghina385124d2014-04-03 13:37:39 +01002209 try {
Fyodor Kupolov7f98aa42016-04-07 14:56:25 -07002210 return mService.getProfileIds(userId, enabledOnly);
Alexandra Gherghina385124d2014-04-03 13:37:39 +01002211 } catch (RemoteException re) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07002212 throw re.rethrowFromSystemServer();
Alexandra Gherghina385124d2014-04-03 13:37:39 +01002213 }
Fyodor Kupolov7f98aa42016-04-07 14:56:25 -07002214 }
2215
2216 /**
2217 * @see #getProfileIds(int, boolean)
2218 * @hide
2219 */
2220 public int[] getProfileIdsWithDisabled(@UserIdInt int userId) {
2221 return getProfileIds(userId, false /* enabledOnly */);
2222 }
2223
2224 /**
2225 * @see #getProfileIds(int, boolean)
2226 * @hide
2227 */
2228 public int[] getEnabledProfileIds(@UserIdInt int userId) {
2229 return getProfileIds(userId, true /* enabledOnly */);
Amith Yamasani4f582632014-02-19 14:31:52 -08002230 }
2231
Amith Yamasani7dda2652014-04-11 14:57:12 -07002232 /**
Andres Moralesc5548c02015-08-05 10:23:12 -07002233 * Returns the device credential owner id of the profile from
2234 * which this method is called, or userHandle if called from a user that
2235 * is not a profile.
2236 *
2237 * @hide
2238 */
Jeff Sharkey8588bc12016-01-06 16:47:42 -07002239 public int getCredentialOwnerProfile(@UserIdInt int userHandle) {
Andres Moralesc5548c02015-08-05 10:23:12 -07002240 try {
2241 return mService.getCredentialOwnerProfile(userHandle);
2242 } catch (RemoteException re) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07002243 throw re.rethrowFromSystemServer();
Andres Moralesc5548c02015-08-05 10:23:12 -07002244 }
2245 }
2246
2247 /**
Jessica Hummelbe81c802014-04-22 15:49:22 +01002248 * Returns the parent of the profile which this method is called from
2249 * or null if called from a user that is not a profile.
2250 *
2251 * @hide
2252 */
Jeff Sharkey8588bc12016-01-06 16:47:42 -07002253 public UserInfo getProfileParent(@UserIdInt int userHandle) {
Jessica Hummelbe81c802014-04-22 15:49:22 +01002254 try {
2255 return mService.getProfileParent(userHandle);
2256 } catch (RemoteException re) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07002257 throw re.rethrowFromSystemServer();
Jessica Hummelbe81c802014-04-22 15:49:22 +01002258 }
2259 }
2260
Tony Makbece85d2018-01-12 12:10:17 +00002261 /** @removed */
2262 @Deprecated
2263 public boolean trySetQuietModeEnabled(boolean enableQuietMode, @NonNull UserHandle userHandle) {
2264 return requestQuietModeEnabled(enableQuietMode, userHandle);
2265 }
2266
Jessica Hummelbe81c802014-04-22 15:49:22 +01002267 /**
Tony Make3d1f652017-12-12 11:00:37 +00002268 * Enables or disables quiet mode for a managed profile. If quiet mode is enabled, apps in a
2269 * managed profile don't run, generate notifications, or consume data or battery.
2270 * <p>
2271 * If a user's credential is needed to turn off quiet mode, a confirm credential screen will be
2272 * shown to the user.
2273 * <p>
2274 * The change may not happen instantly, however apps can listen for
2275 * {@link Intent#ACTION_MANAGED_PROFILE_AVAILABLE} and
2276 * {@link Intent#ACTION_MANAGED_PROFILE_UNAVAILABLE} broadcasts in order to be notified of
2277 * the change of the quiet mode. Apps can also check the current state of quiet mode by
2278 * calling {@link #isQuietModeEnabled(UserHandle)}.
2279 * <p>
2280 * The caller must either be the foreground default launcher or have one of these permissions:
2281 * {@code MANAGE_USERS} or {@code MODIFY_QUIET_MODE}.
Rubin Xu0a29ecd2015-11-04 15:11:48 +00002282 *
Tony Make3d1f652017-12-12 11:00:37 +00002283 * @param enableQuietMode whether quiet mode should be enabled or disabled
2284 * @param userHandle user handle of the profile
2285 * @return {@code false} if user's credential is needed in order to turn off quiet mode,
2286 * {@code true} otherwise
2287 * @throws SecurityException if the caller is invalid
2288 * @throws IllegalArgumentException if {@code userHandle} is not a managed profile
2289 *
2290 * @see #isQuietModeEnabled(UserHandle)
Rubin Xu0a29ecd2015-11-04 15:11:48 +00002291 */
Tony Makbece85d2018-01-12 12:10:17 +00002292 public boolean requestQuietModeEnabled(boolean enableQuietMode, @NonNull UserHandle userHandle) {
2293 return requestQuietModeEnabled(enableQuietMode, userHandle, null);
Tony Makb7e6fd42017-12-05 19:40:28 +00002294 }
2295
2296 /**
Tony Makbece85d2018-01-12 12:10:17 +00002297 * Similar to {@link #requestQuietModeEnabled(boolean, UserHandle)}, except you can specify
Tony Makd390ae92017-12-28 13:23:10 +00002298 * a target to start when user is unlocked. If {@code target} is specified, caller must have
2299 * the {@link android.Manifest.permission#MANAGE_USERS} permission.
Tony Makb7e6fd42017-12-05 19:40:28 +00002300 *
Tony Makbece85d2018-01-12 12:10:17 +00002301 * @see {@link #requestQuietModeEnabled(boolean, UserHandle)}
Rubin Xu0a29ecd2015-11-04 15:11:48 +00002302 * @hide
2303 */
Tony Makbece85d2018-01-12 12:10:17 +00002304 public boolean requestQuietModeEnabled(
Tony Makb7e6fd42017-12-05 19:40:28 +00002305 boolean enableQuietMode, @NonNull UserHandle userHandle, IntentSender target) {
Rubin Xu0a29ecd2015-11-04 15:11:48 +00002306 try {
Tony Makbece85d2018-01-12 12:10:17 +00002307 return mService.requestQuietModeEnabled(
Tony Make3d1f652017-12-12 11:00:37 +00002308 mContext.getPackageName(), enableQuietMode, userHandle.getIdentifier(), target);
Jeff Sharkey27b2e692016-02-25 17:40:12 -07002309 } catch (RemoteException re) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07002310 throw re.rethrowFromSystemServer();
Rubin Xu0a29ecd2015-11-04 15:11:48 +00002311 }
2312 }
2313
2314 /**
2315 * Returns whether the given profile is in quiet mode or not.
Ricky Wai7881cf82016-04-15 17:20:12 +01002316 * Notes: Quiet mode is only supported for managed profiles.
Rubin Xu0a29ecd2015-11-04 15:11:48 +00002317 *
2318 * @param userHandle The user handle of the profile to be queried.
2319 * @return true if the profile is in quiet mode, false otherwise.
2320 */
2321 public boolean isQuietModeEnabled(UserHandle userHandle) {
2322 try {
2323 return mService.isQuietModeEnabled(userHandle.getIdentifier());
Jeff Sharkey27b2e692016-02-25 17:40:12 -07002324 } catch (RemoteException re) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07002325 throw re.rethrowFromSystemServer();
Rubin Xu0a29ecd2015-11-04 15:11:48 +00002326 }
Rubin Xu0a29ecd2015-11-04 15:11:48 +00002327 }
2328
2329 /**
Amith Yamasani7dda2652014-04-11 14:57:12 -07002330 * If the target user is a managed profile of the calling user or the caller
2331 * is itself a managed profile, then this returns a badged copy of the given
Svetoslavc71c42f2014-08-05 18:57:05 -07002332 * icon to be able to distinguish it from the original icon. For badging an
2333 * arbitrary drawable use {@link #getBadgedDrawableForUser(
2334 * android.graphics.drawable.Drawable, UserHandle, android.graphics.Rect, int)}.
2335 * <p>
2336 * If the original drawable is a BitmapDrawable and the backing bitmap is
Vadim Tryshev66ae66a2016-02-18 15:41:21 -08002337 * mutable as per {@link android.graphics.Bitmap#isMutable()}, the badging
Svetoslavc71c42f2014-08-05 18:57:05 -07002338 * is performed in place and the original drawable is returned.
2339 * </p>
Amith Yamasani7dda2652014-04-11 14:57:12 -07002340 *
2341 * @param icon The icon to badge.
2342 * @param user The target user.
2343 * @return A drawable that combines the original icon and a badge as
2344 * determined by the system.
Svetoslavc7d62f02014-09-04 15:39:54 -07002345 * @removed
Amith Yamasani7dda2652014-04-11 14:57:12 -07002346 */
Svetoslavc71c42f2014-08-05 18:57:05 -07002347 public Drawable getBadgedIconForUser(Drawable icon, UserHandle user) {
Svetoslavc7d62f02014-09-04 15:39:54 -07002348 return mContext.getPackageManager().getUserBadgedIcon(icon, user);
Amith Yamasani4f582632014-02-19 14:31:52 -08002349 }
2350
Kenny Guy701ea7c2014-05-08 23:34:12 +01002351 /**
2352 * If the target user is a managed profile of the calling user or the caller
Svetoslavc71c42f2014-08-05 18:57:05 -07002353 * is itself a managed profile, then this returns a badged copy of the given
2354 * drawable allowing the user to distinguish it from the original drawable.
2355 * The caller can specify the location in the bounds of the drawable to be
2356 * badged where the badge should be applied as well as the density of the
2357 * badge to be used.
2358 * <p>
2359 * If the original drawable is a BitmapDrawable and the backing bitmap is
Vadim Tryshev66ae66a2016-02-18 15:41:21 -08002360 * mutable as per {@link android.graphics.Bitmap#isMutable()}, the badging
Svetoslavc71c42f2014-08-05 18:57:05 -07002361 * is performed in place and the original drawable is returned.
2362 * </p>
2363 *
2364 * @param badgedDrawable The drawable to badge.
2365 * @param user The target user.
2366 * @param badgeLocation Where in the bounds of the badged drawable to place
Vadim Tryshev66ae66a2016-02-18 15:41:21 -08002367 * the badge. If it's {@code null}, the badge is applied on top of the entire
Svetoslavc71c42f2014-08-05 18:57:05 -07002368 * drawable being badged.
2369 * @param badgeDensity The optional desired density for the badge as per
Vadim Tryshev66ae66a2016-02-18 15:41:21 -08002370 * {@link android.util.DisplayMetrics#densityDpi}. If it's not positive,
Svetoslavc71c42f2014-08-05 18:57:05 -07002371 * the density of the display is used.
2372 * @return A drawable that combines the original drawable and a badge as
2373 * determined by the system.
Svetoslavc7d62f02014-09-04 15:39:54 -07002374 * @removed
Svetoslavc71c42f2014-08-05 18:57:05 -07002375 */
2376 public Drawable getBadgedDrawableForUser(Drawable badgedDrawable, UserHandle user,
2377 Rect badgeLocation, int badgeDensity) {
Svetoslavc7d62f02014-09-04 15:39:54 -07002378 return mContext.getPackageManager().getUserBadgedDrawableForDensity(badgedDrawable, user,
2379 badgeLocation, badgeDensity);
Svetoslavc71c42f2014-08-05 18:57:05 -07002380 }
2381
2382 /**
2383 * If the target user is a managed profile of the calling user or the caller
Kenny Guyf7ecf7c2014-06-18 11:32:05 +01002384 * is itself a managed profile, then this returns a copy of the label with
2385 * badging for accessibility services like talkback. E.g. passing in "Email"
2386 * and it might return "Work Email" for Email in the work profile.
2387 *
2388 * @param label The label to change.
2389 * @param user The target user.
2390 * @return A label that combines the original label and a badge as
2391 * determined by the system.
Svetoslavc7d62f02014-09-04 15:39:54 -07002392 * @removed
Kenny Guyf7ecf7c2014-06-18 11:32:05 +01002393 */
Kenny Guy237aecd2014-07-21 14:06:09 +01002394 public CharSequence getBadgedLabelForUser(CharSequence label, UserHandle user) {
Svetoslavc7d62f02014-09-04 15:39:54 -07002395 return mContext.getPackageManager().getUserBadgedLabel(label, user);
Amith Yamasani4f582632014-02-19 14:31:52 -08002396 }
2397
2398 /**
2399 * Returns information for all users on this device. Requires
2400 * {@link android.Manifest.permission#MANAGE_USERS} permission.
Emily Bernier394a6cd2014-05-07 12:49:20 -04002401 *
Amith Yamasani4f582632014-02-19 14:31:52 -08002402 * @param excludeDying specify if the list should exclude users being
2403 * removed.
Amith Yamasani920ace02012-09-20 22:15:37 -07002404 * @return the list of users that were created.
2405 * @hide
2406 */
Adam Lesinskiada8deb2017-05-12 13:50:42 -07002407 public @NonNull List<UserInfo> getUsers(boolean excludeDying) {
Amith Yamasani920ace02012-09-20 22:15:37 -07002408 try {
2409 return mService.getUsers(excludeDying);
Amith Yamasani258848d2012-08-10 17:06:33 -07002410 } catch (RemoteException re) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07002411 throw re.rethrowFromSystemServer();
Amith Yamasani258848d2012-08-10 17:06:33 -07002412 }
2413 }
2414
2415 /**
2416 * Removes a user and all associated data.
Amith Yamasani195263742012-08-21 15:40:12 -07002417 * Requires {@link android.Manifest.permission#MANAGE_USERS} permission.
Amith Yamasani258848d2012-08-10 17:06:33 -07002418 * @param userHandle the integer handle of the user, where 0 is the primary user.
2419 * @hide
2420 */
Jeff Sharkey8588bc12016-01-06 16:47:42 -07002421 public boolean removeUser(@UserIdInt int userHandle) {
Amith Yamasani258848d2012-08-10 17:06:33 -07002422 try {
2423 return mService.removeUser(userHandle);
2424 } catch (RemoteException re) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07002425 throw re.rethrowFromSystemServer();
Amith Yamasani258848d2012-08-10 17:06:33 -07002426 }
2427 }
2428
2429 /**
Nicolas Prevotd37c4a92017-01-23 11:56:00 +00002430 * Similar to {@link #removeUser(int)} except bypassing the checking of
2431 * {@link UserManager#DISALLOW_REMOVE_USER}
2432 * or {@link UserManager#DISALLOW_REMOVE_MANAGED_PROFILE}.
2433 *
2434 * @see {@link #removeUser(int)}
2435 * @hide
2436 */
2437 public boolean removeUserEvenWhenDisallowed(@UserIdInt int userHandle) {
2438 try {
2439 return mService.removeUserEvenWhenDisallowed(userHandle);
2440 } catch (RemoteException re) {
2441 throw re.rethrowFromSystemServer();
2442 }
2443 }
2444
2445 /**
Amith Yamasani258848d2012-08-10 17:06:33 -07002446 * Updates the user's name.
Amith Yamasani195263742012-08-21 15:40:12 -07002447 * Requires {@link android.Manifest.permission#MANAGE_USERS} permission.
Amith Yamasani258848d2012-08-10 17:06:33 -07002448 *
2449 * @param userHandle the user's integer handle
2450 * @param name the new name for the user
2451 * @hide
2452 */
Jeff Sharkey8588bc12016-01-06 16:47:42 -07002453 public void setUserName(@UserIdInt int userHandle, String name) {
Amith Yamasani258848d2012-08-10 17:06:33 -07002454 try {
2455 mService.setUserName(userHandle, name);
2456 } catch (RemoteException re) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07002457 throw re.rethrowFromSystemServer();
Amith Yamasani258848d2012-08-10 17:06:33 -07002458 }
2459 }
2460
2461 /**
Amith Yamasanie928d7d2012-09-17 21:46:51 -07002462 * Sets the user's photo.
Amith Yamasani258848d2012-08-10 17:06:33 -07002463 * @param userHandle the user for whom to change the photo.
Amith Yamasanie928d7d2012-09-17 21:46:51 -07002464 * @param icon the bitmap to set as the photo.
Amith Yamasani258848d2012-08-10 17:06:33 -07002465 * @hide
2466 */
Jeff Sharkey8588bc12016-01-06 16:47:42 -07002467 public void setUserIcon(@UserIdInt int userHandle, Bitmap icon) {
Amith Yamasani258848d2012-08-10 17:06:33 -07002468 try {
Amith Yamasanie928d7d2012-09-17 21:46:51 -07002469 mService.setUserIcon(userHandle, icon);
Amith Yamasani258848d2012-08-10 17:06:33 -07002470 } catch (RemoteException re) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07002471 throw re.rethrowFromSystemServer();
Amith Yamasani258848d2012-08-10 17:06:33 -07002472 }
2473 }
2474
2475 /**
Amith Yamasani3b49f072012-09-17 10:21:43 -07002476 * Returns a file descriptor for the user's photo. PNG data can be read from this file.
2477 * @param userHandle the user whose photo we want to read.
Amith Yamasanie928d7d2012-09-17 21:46:51 -07002478 * @return a {@link Bitmap} of the user's photo, or null if there's no photo.
Alexandra Gherghina64d4dca2014-08-28 18:26:56 +01002479 * @see com.android.internal.util.UserIcons#getDefaultUserIcon for a default.
Amith Yamasani3b49f072012-09-17 10:21:43 -07002480 * @hide
2481 */
Jeff Sharkey8588bc12016-01-06 16:47:42 -07002482 public Bitmap getUserIcon(@UserIdInt int userHandle) {
Amith Yamasani3b49f072012-09-17 10:21:43 -07002483 try {
Adrian Roos1bdff912015-02-17 15:51:35 +01002484 ParcelFileDescriptor fd = mService.getUserIcon(userHandle);
2485 if (fd != null) {
2486 try {
2487 return BitmapFactory.decodeFileDescriptor(fd.getFileDescriptor());
2488 } finally {
2489 try {
2490 fd.close();
2491 } catch (IOException e) {
2492 }
2493 }
2494 }
Amith Yamasani3b49f072012-09-17 10:21:43 -07002495 } catch (RemoteException re) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07002496 throw re.rethrowFromSystemServer();
Amith Yamasani3b49f072012-09-17 10:21:43 -07002497 }
Adrian Roos1bdff912015-02-17 15:51:35 +01002498 return null;
Amith Yamasani3b49f072012-09-17 10:21:43 -07002499 }
2500
2501 /**
Amith Yamasani258848d2012-08-10 17:06:33 -07002502 * Returns the maximum number of users that can be created on this device. A return value
2503 * of 1 means that it is a single user device.
2504 * @hide
Maggie Benthalla12fccf2013-03-14 18:02:12 -04002505 * @return a value greater than or equal to 1
Amith Yamasani258848d2012-08-10 17:06:33 -07002506 */
Jeff Sharkey27bd34d2012-09-16 12:49:00 -07002507 public static int getMaxSupportedUsers() {
Amith Yamasaniff549202012-10-12 12:44:49 -07002508 // Don't allow multiple users on certain builds
2509 if (android.os.Build.ID.startsWith("JVP")) return 1;
Dianne Hackborn409297d2014-07-10 17:39:20 -07002510 // Svelte devices don't get multi-user.
2511 if (ActivityManager.isLowRamDeviceStatic()) return 1;
Jeff Sharkey27bd34d2012-09-16 12:49:00 -07002512 return SystemProperties.getInt("fw.max_users",
2513 Resources.getSystem().getInteger(R.integer.config_multiuserMaximumUsers));
Amith Yamasani258848d2012-08-10 17:06:33 -07002514 }
Amith Yamasani2a003292012-08-14 18:25:45 -07002515
2516 /**
Fyodor Kupolovcd86ebf2015-09-29 17:06:50 -07002517 * Returns true if the user switcher should be shown, this will be if device supports multi-user
2518 * and there are at least 2 users available that are not managed profiles.
Kenny Guy1a447532014-02-20 21:55:32 +00002519 * @hide
2520 * @return true if user switcher should be shown.
2521 */
2522 public boolean isUserSwitcherEnabled() {
Fyodor Kupolovcd86ebf2015-09-29 17:06:50 -07002523 if (!supportsMultipleUsers()) {
2524 return false;
2525 }
Benjamin Franzff66fa92017-08-10 10:39:44 +01002526 if (hasUserRestriction(DISALLOW_USER_SWITCH)) {
2527 return false;
2528 }
Amith Yamasanieb437d42016-04-29 09:31:25 -07002529 // If Demo Mode is on, don't show user switcher
2530 if (isDeviceInDemoMode(mContext)) {
2531 return false;
2532 }
Kenny Guy1a447532014-02-20 21:55:32 +00002533 List<UserInfo> users = getUsers(true);
2534 if (users == null) {
2535 return false;
2536 }
2537 int switchableUserCount = 0;
2538 for (UserInfo user : users) {
Xiaohui Chen7cb69df2015-07-13 16:01:01 -07002539 if (user.supportsSwitchToByUser()) {
Kenny Guy1a447532014-02-20 21:55:32 +00002540 ++switchableUserCount;
2541 }
2542 }
Fyodor Kupolovcd86ebf2015-09-29 17:06:50 -07002543 final boolean guestEnabled = !mContext.getSystemService(DevicePolicyManager.class)
2544 .getGuestUserDisabled(null);
Amith Yamasania596ff82014-06-12 18:12:38 -07002545 return switchableUserCount > 1 || guestEnabled;
Kenny Guy1a447532014-02-20 21:55:32 +00002546 }
2547
2548 /**
Amith Yamasanieb437d42016-04-29 09:31:25 -07002549 * @hide
2550 */
2551 public static boolean isDeviceInDemoMode(Context context) {
2552 return Settings.Global.getInt(context.getContentResolver(),
2553 Settings.Global.DEVICE_DEMO_MODE, 0) > 0;
2554 }
2555
2556 /**
Amith Yamasani2a003292012-08-14 18:25:45 -07002557 * Returns a serial number on this device for a given userHandle. User handles can be recycled
2558 * when deleting and creating users, but serial numbers are not reused until the device is wiped.
2559 * @param userHandle
2560 * @return a serial number associated with that user, or -1 if the userHandle is not valid.
2561 * @hide
2562 */
Jeff Sharkey8588bc12016-01-06 16:47:42 -07002563 public int getUserSerialNumber(@UserIdInt int userHandle) {
Amith Yamasani2a003292012-08-14 18:25:45 -07002564 try {
2565 return mService.getUserSerialNumber(userHandle);
2566 } catch (RemoteException re) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07002567 throw re.rethrowFromSystemServer();
Amith Yamasani2a003292012-08-14 18:25:45 -07002568 }
Amith Yamasani2a003292012-08-14 18:25:45 -07002569 }
2570
2571 /**
2572 * Returns a userHandle on this device for a given user serial number. User handles can be
2573 * recycled when deleting and creating users, but serial numbers are not reused until the device
2574 * is wiped.
2575 * @param userSerialNumber
2576 * @return the userHandle associated with that user serial number, or -1 if the serial number
2577 * is not valid.
2578 * @hide
2579 */
Jeff Sharkey8588bc12016-01-06 16:47:42 -07002580 public @UserIdInt int getUserHandle(int userSerialNumber) {
Amith Yamasani2a003292012-08-14 18:25:45 -07002581 try {
2582 return mService.getUserHandle(userSerialNumber);
2583 } catch (RemoteException re) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07002584 throw re.rethrowFromSystemServer();
Amith Yamasani2a003292012-08-14 18:25:45 -07002585 }
Amith Yamasani2a003292012-08-14 18:25:45 -07002586 }
Maggie Benthall67944582013-02-22 14:58:27 -05002587
Amith Yamasani7e99bc02013-04-16 18:24:51 -07002588 /**
Esteban Talavera953fe482016-06-07 15:25:20 +01002589 * Returns a {@link Bundle} containing any saved application restrictions for this user, for the
Amith Yamasani7e99bc02013-04-16 18:24:51 -07002590 * given package name. Only an application with this package name can call this method.
Esteban Talavera5b9f1672015-12-11 15:22:34 +00002591 *
2592 * <p>The returned {@link Bundle} consists of key-value pairs, as defined by the application,
2593 * where the types of values may be:
2594 * <ul>
2595 * <li>{@code boolean}
2596 * <li>{@code int}
2597 * <li>{@code String} or {@code String[]}
2598 * <li>From {@link android.os.Build.VERSION_CODES#M}, {@code Bundle} or {@code Bundle[]}
2599 * </ul>
2600 *
Fyodor Kupolov4e9af062016-07-18 16:59:11 -07002601 * <p>NOTE: The method performs disk I/O and shouldn't be called on the main thread
2602 *
Amith Yamasani7e99bc02013-04-16 18:24:51 -07002603 * @param packageName the package name of the calling application
Esteban Talavera953fe482016-06-07 15:25:20 +01002604 * @return a {@link Bundle} with the restrictions for that package, or an empty {@link Bundle}
2605 * if there are no saved restrictions.
Esteban Talavera5b9f1672015-12-11 15:22:34 +00002606 *
2607 * @see #KEY_RESTRICTIONS_PENDING
Amith Yamasani7e99bc02013-04-16 18:24:51 -07002608 */
Fyodor Kupolov4e9af062016-07-18 16:59:11 -07002609 @WorkerThread
Amith Yamasani7e99bc02013-04-16 18:24:51 -07002610 public Bundle getApplicationRestrictions(String packageName) {
2611 try {
2612 return mService.getApplicationRestrictions(packageName);
2613 } catch (RemoteException re) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07002614 throw re.rethrowFromSystemServer();
Amith Yamasani7e99bc02013-04-16 18:24:51 -07002615 }
Amith Yamasani7e99bc02013-04-16 18:24:51 -07002616 }
Amith Yamasanidf2e92a2013-03-01 17:04:38 -08002617
2618 /**
2619 * @hide
2620 */
Fyodor Kupolov4e9af062016-07-18 16:59:11 -07002621 @WorkerThread
Amith Yamasani7e99bc02013-04-16 18:24:51 -07002622 public Bundle getApplicationRestrictions(String packageName, UserHandle user) {
Amith Yamasanidf2e92a2013-03-01 17:04:38 -08002623 try {
Amith Yamasani7e99bc02013-04-16 18:24:51 -07002624 return mService.getApplicationRestrictionsForUser(packageName, user.getIdentifier());
Amith Yamasanidf2e92a2013-03-01 17:04:38 -08002625 } catch (RemoteException re) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07002626 throw re.rethrowFromSystemServer();
Amith Yamasanidf2e92a2013-03-01 17:04:38 -08002627 }
Amith Yamasanidf2e92a2013-03-01 17:04:38 -08002628 }
2629
2630 /**
2631 * @hide
2632 */
Fyodor Kupolov4e9af062016-07-18 16:59:11 -07002633 @WorkerThread
Amith Yamasani7e99bc02013-04-16 18:24:51 -07002634 public void setApplicationRestrictions(String packageName, Bundle restrictions,
Amith Yamasanidf2e92a2013-03-01 17:04:38 -08002635 UserHandle user) {
2636 try {
Amith Yamasani7e99bc02013-04-16 18:24:51 -07002637 mService.setApplicationRestrictions(packageName, restrictions, user.getIdentifier());
Amith Yamasanidf2e92a2013-03-01 17:04:38 -08002638 } catch (RemoteException re) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07002639 throw re.rethrowFromSystemServer();
Amith Yamasanidf2e92a2013-03-01 17:04:38 -08002640 }
2641 }
Amith Yamasani655d0e22013-06-12 14:19:10 -07002642
2643 /**
Amith Yamasanid304af62013-09-05 09:30:23 -07002644 * Sets a new challenge PIN for restrictions. This is only for use by pre-installed
2645 * apps and requires the MANAGE_USERS permission.
2646 * @param newPin the PIN to use for challenge dialogs.
2647 * @return Returns true if the challenge PIN was set successfully.
Fyodor Kupolovef249092015-05-06 13:18:46 -07002648 * @deprecated The restrictions PIN functionality is no longer provided by the system.
2649 * This method is preserved for backwards compatibility reasons and always returns false.
Amith Yamasani655d0e22013-06-12 14:19:10 -07002650 */
Aurimas Liutikas514c5ef2016-05-24 15:22:55 -07002651 @Deprecated
Amith Yamasanid304af62013-09-05 09:30:23 -07002652 public boolean setRestrictionsChallenge(String newPin) {
Amith Yamasani655d0e22013-06-12 14:19:10 -07002653 return false;
2654 }
Amith Yamasani1a7472e2013-07-02 11:17:30 -07002655
Amith Yamasanie4afaa32014-06-30 14:55:07 +05302656 /**
2657 * @hide
2658 * Set restrictions that should apply to any future guest user that's created.
2659 */
2660 public void setDefaultGuestRestrictions(Bundle restrictions) {
2661 try {
2662 mService.setDefaultGuestRestrictions(restrictions);
2663 } catch (RemoteException re) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07002664 throw re.rethrowFromSystemServer();
Amith Yamasanie4afaa32014-06-30 14:55:07 +05302665 }
2666 }
2667
2668 /**
2669 * @hide
2670 * Gets the default guest restrictions.
2671 */
2672 public Bundle getDefaultGuestRestrictions() {
2673 try {
2674 return mService.getDefaultGuestRestrictions();
2675 } catch (RemoteException re) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07002676 throw re.rethrowFromSystemServer();
Amith Yamasanie4afaa32014-06-30 14:55:07 +05302677 }
Amith Yamasanie4afaa32014-06-30 14:55:07 +05302678 }
Fyodor Kupolovff7233e2015-04-08 11:28:52 -07002679
2680 /**
2681 * Returns creation time of the user or of a managed profile associated with the calling user.
2682 * @param userHandle user handle of the user or a managed profile associated with the
2683 * calling user.
2684 * @return creation time in milliseconds since Epoch time.
2685 */
Fyodor Kupolov385de622015-04-10 18:00:19 -07002686 public long getUserCreationTime(UserHandle userHandle) {
Fyodor Kupolovff7233e2015-04-08 11:28:52 -07002687 try {
Fyodor Kupolov385de622015-04-10 18:00:19 -07002688 return mService.getUserCreationTime(userHandle.getIdentifier());
Fyodor Kupolovff7233e2015-04-08 11:28:52 -07002689 } catch (RemoteException re) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07002690 throw re.rethrowFromSystemServer();
Fyodor Kupolovff7233e2015-04-08 11:28:52 -07002691 }
2692 }
Amith Yamasani12747872015-12-07 14:19:49 -08002693
2694 /**
2695 * @hide
2696 * Checks if any uninitialized user has the specific seed account name and type.
2697 *
Pavel Grafov6a40f092016-10-25 15:46:51 +01002698 * @param accountName The account name to check for
2699 * @param accountType The account type of the account to check for
Amith Yamasani12747872015-12-07 14:19:49 -08002700 * @return whether the seed account was found
2701 */
2702 public boolean someUserHasSeedAccount(String accountName, String accountType) {
2703 try {
2704 return mService.someUserHasSeedAccount(accountName, accountType);
2705 } catch (RemoteException re) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07002706 throw re.rethrowFromSystemServer();
Amith Yamasani12747872015-12-07 14:19:49 -08002707 }
2708 }
Pavel Grafov6a40f092016-10-25 15:46:51 +01002709
2710 /**
2711 * @hide
2712 * User that enforces a restriction.
2713 *
2714 * @see #getUserRestrictionSources(String, UserHandle)
2715 */
2716 @SystemApi
2717 public static final class EnforcingUser implements Parcelable {
2718 private final @UserIdInt int userId;
2719 private final @UserRestrictionSource int userRestrictionSource;
2720
2721 /**
2722 * @hide
2723 */
2724 public EnforcingUser(
2725 @UserIdInt int userId, @UserRestrictionSource int userRestrictionSource) {
2726 this.userId = userId;
2727 this.userRestrictionSource = userRestrictionSource;
2728 }
2729
2730 private EnforcingUser(Parcel in) {
2731 userId = in.readInt();
2732 userRestrictionSource = in.readInt();
2733 }
2734
2735 public static final Creator<EnforcingUser> CREATOR = new Creator<EnforcingUser>() {
2736 @Override
2737 public EnforcingUser createFromParcel(Parcel in) {
2738 return new EnforcingUser(in);
2739 }
2740
2741 @Override
2742 public EnforcingUser[] newArray(int size) {
2743 return new EnforcingUser[size];
2744 }
2745 };
2746
2747 @Override
2748 public int describeContents() {
2749 return 0;
2750 }
2751
2752 @Override
2753 public void writeToParcel(Parcel dest, int flags) {
2754 dest.writeInt(userId);
2755 dest.writeInt(userRestrictionSource);
2756 }
2757
2758 /**
2759 * Returns an id of the enforcing user.
2760 *
2761 * <p> Will be UserHandle.USER_NULL when restriction is set by the system.
2762 */
2763 public UserHandle getUserHandle() {
2764 return UserHandle.of(userId);
2765 }
2766
2767 /**
2768 * Returns the status of the enforcing user.
2769 *
2770 * <p> One of {@link #RESTRICTION_SOURCE_SYSTEM},
2771 * {@link #RESTRICTION_SOURCE_DEVICE_OWNER} and
2772 * {@link #RESTRICTION_SOURCE_PROFILE_OWNER}
2773 */
2774 public @UserRestrictionSource int getUserRestrictionSource() {
2775 return userRestrictionSource;
2776 }
2777 }
Amith Yamasani258848d2012-08-10 17:06:33 -07002778}