blob: 85b010a6a8f32c2107d5409b0244fb15250f89be [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;
Xiaohui Chen7cb69df2015-07-13 16:01:01 -070022import android.annotation.Nullable;
Xiaohui Chenb3b92582015-12-07 11:22:13 -080023import android.annotation.RequiresPermission;
Amith Yamasani0e8d7d62014-09-03 13:17:28 -070024import android.annotation.SystemApi;
Jeff Sharkey8588bc12016-01-06 16:47:42 -070025import android.annotation.UserIdInt;
Amith Yamasani37ed8d12016-01-27 14:40:16 -080026import android.app.Activity;
Dianne Hackborn409297d2014-07-10 17:39:20 -070027import android.app.ActivityManager;
Dianne Hackborna8a9bd62012-10-09 15:36:59 -070028import android.app.ActivityManagerNative;
Fyodor Kupolovcd86ebf2015-09-29 17:06:50 -070029import android.app.admin.DevicePolicyManager;
Makoto Onuki068c54a2015-10-13 14:34:03 -070030import android.content.ComponentName;
Amith Yamasani258848d2012-08-10 17:06:33 -070031import android.content.Context;
Amith Yamasani12747872015-12-07 14:19:49 -080032import android.content.Intent;
Benjamin Franzf02420c2016-04-04 18:52:21 +010033import android.content.IntentSender;
Amith Yamasani258848d2012-08-10 17:06:33 -070034import android.content.pm.UserInfo;
Jeff Sharkey27bd34d2012-09-16 12:49:00 -070035import android.content.res.Resources;
Maggie Benthall67944582013-02-22 14:58:27 -050036import android.graphics.Bitmap;
Adrian Roos1bdff912015-02-17 15:51:35 +010037import android.graphics.BitmapFactory;
Amith Yamasani4f582632014-02-19 14:31:52 -080038import android.graphics.Rect;
Amith Yamasani4f582632014-02-19 14:31:52 -080039import android.graphics.drawable.Drawable;
Amith Yamasani1e9c2182014-06-11 17:25:51 -070040import android.provider.Settings;
Fyodor Kupolov523c4042016-02-24 15:03:13 -080041import android.telephony.TelephonyManager;
Jason Monk1c7c3192014-06-26 12:52:18 -040042import android.view.WindowManager.LayoutParams;
Amith Yamasani258848d2012-08-10 17:06:33 -070043
Maggie Benthall67944582013-02-22 14:58:27 -050044import com.android.internal.R;
45
Adrian Roos1bdff912015-02-17 15:51:35 +010046import java.io.IOException;
Zoltan Szatmary-Bane7834602016-04-08 18:41:11 +010047import java.lang.annotation.Retention;
48import java.lang.annotation.RetentionPolicy;
Amith Yamasani4f582632014-02-19 14:31:52 -080049import java.util.ArrayList;
Amith Yamasani258848d2012-08-10 17:06:33 -070050import java.util.List;
51
52/**
Amith Yamasani06964342016-04-15 13:55:01 -070053 * Manages users and user details on a multi-user system. There are two major categories of
54 * users: fully customizable users with their own login, and managed profiles that share a workspace
55 * with a related user.
56 * <p>
57 * Users are different from accounts, which are managed by
58 * {@link AccountManager}. Each user can have their own set of accounts.
59 * <p>
60 * See {@link DevicePolicyManager#ACTION_PROVISION_MANAGED_PROFILE} for more on managed profiles.
Amith Yamasani258848d2012-08-10 17:06:33 -070061 */
62public class UserManager {
63
64 private static String TAG = "UserManager";
65 private final IUserManager mService;
66 private final Context mContext;
67
Amith Yamasanie4cf7342012-12-17 11:12:09 -080068 /**
Zoltan Szatmary-Bane7834602016-04-08 18:41:11 +010069 * @hide
70 * No user restriction.
71 */
72 @SystemApi
73 public static final int RESTRICTION_NOT_SET = 0x0;
74
75 /**
76 * @hide
77 * User restriction set by system/user.
78 */
79 @SystemApi
80 public static final int RESTRICTION_SOURCE_SYSTEM = 0x1;
81
82 /**
83 * @hide
84 * User restriction set by a device owner.
85 */
86 @SystemApi
87 public static final int RESTRICTION_SOURCE_DEVICE_OWNER = 0x2;
88
89 /**
90 * @hide
91 * User restriction set by a profile owner.
92 */
93 @SystemApi
94 public static final int RESTRICTION_SOURCE_PROFILE_OWNER = 0x4;
95
96 /** @hide */
97 @Retention(RetentionPolicy.SOURCE)
98 @IntDef(flag=true, value={RESTRICTION_NOT_SET, RESTRICTION_SOURCE_SYSTEM,
99 RESTRICTION_SOURCE_DEVICE_OWNER, RESTRICTION_SOURCE_PROFILE_OWNER})
100 @SystemApi
101 public @interface UserRestrictionSource {}
102
103 /**
Fyodor Kupolov53019282015-07-21 11:48:18 -0700104 * Specifies if a user is disallowed from adding and removing accounts, unless they are
105 * {@link android.accounts.AccountManager#addAccountExplicitly programmatically} added by
106 * Authenticator.
Amith Yamasani71e6c692013-03-24 17:39:28 -0700107 * The default value is <code>false</code>.
Amith Yamasani26af8292014-09-09 09:57:27 -0700108 *
Benjamin Franzb6c0ce42015-11-05 10:06:51 +0000109 * <p>From {@link android.os.Build.VERSION_CODES#N} a profile or device owner app can still
110 * use {@link android.accounts.AccountManager} APIs to add or remove accounts when account
111 * management is disallowed.
112 *
Fyodor Kupolov0ffbfea2016-01-19 10:07:54 -0800113 * <p>Key for user restrictions.
114 * <p>Type: Boolean
Makoto Onuki068c54a2015-10-13 14:34:03 -0700115 * @see DevicePolicyManager#addUserRestriction(ComponentName, String)
116 * @see DevicePolicyManager#clearUserRestriction(ComponentName, String)
Amith Yamasanie4cf7342012-12-17 11:12:09 -0800117 * @see #getUserRestrictions()
118 */
Amith Yamasani71e6c692013-03-24 17:39:28 -0700119 public static final String DISALLOW_MODIFY_ACCOUNTS = "no_modify_accounts";
Amith Yamasanie4cf7342012-12-17 11:12:09 -0800120
121 /**
Amith Yamasani26af8292014-09-09 09:57:27 -0700122 * Specifies if a user is disallowed from changing Wi-Fi
Julia Reynolds2cb384f2014-08-13 15:15:55 -0400123 * access points. The default value is <code>false</code>.
Fyodor Kupolov0ffbfea2016-01-19 10:07:54 -0800124 * <p>This restriction has no effect in a managed profile.
Amith Yamasani26af8292014-09-09 09:57:27 -0700125 *
Fyodor Kupolov0ffbfea2016-01-19 10:07:54 -0800126 * <p>Key for user restrictions.
127 * <p>Type: Boolean
Makoto Onuki068c54a2015-10-13 14:34:03 -0700128 * @see DevicePolicyManager#addUserRestriction(ComponentName, String)
129 * @see DevicePolicyManager#clearUserRestriction(ComponentName, String)
Amith Yamasanie4cf7342012-12-17 11:12:09 -0800130 * @see #getUserRestrictions()
131 */
Amith Yamasani71e6c692013-03-24 17:39:28 -0700132 public static final String DISALLOW_CONFIG_WIFI = "no_config_wifi";
Amith Yamasanie4cf7342012-12-17 11:12:09 -0800133
134 /**
Amith Yamasani26af8292014-09-09 09:57:27 -0700135 * Specifies if a user is disallowed from installing applications.
Amith Yamasani71e6c692013-03-24 17:39:28 -0700136 * The default value is <code>false</code>.
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_INSTALL_APPS = "no_install_apps";
Amith Yamasanie4cf7342012-12-17 11:12:09 -0800145
146 /**
Amith Yamasani26af8292014-09-09 09:57:27 -0700147 * Specifies if a user is disallowed from uninstalling applications.
Amith Yamasani71e6c692013-03-24 17:39:28 -0700148 * The default value is <code>false</code>.
Amith Yamasani26af8292014-09-09 09:57:27 -0700149 *
Fyodor Kupolov0ffbfea2016-01-19 10:07:54 -0800150 * <p>Key for user restrictions.
151 * <p>Type: Boolean
Makoto Onuki068c54a2015-10-13 14:34:03 -0700152 * @see DevicePolicyManager#addUserRestriction(ComponentName, String)
153 * @see DevicePolicyManager#clearUserRestriction(ComponentName, String)
Amith Yamasanie4cf7342012-12-17 11:12:09 -0800154 * @see #getUserRestrictions()
155 */
Amith Yamasani71e6c692013-03-24 17:39:28 -0700156 public static final String DISALLOW_UNINSTALL_APPS = "no_uninstall_apps";
Amith Yamasanie4cf7342012-12-17 11:12:09 -0800157
Amith Yamasani71e6c692013-03-24 17:39:28 -0700158 /**
Amith Yamasani150514b2015-01-07 16:05:05 -0800159 * Specifies if a user is disallowed from turning on location sharing.
Amith Yamasani71e6c692013-03-24 17:39:28 -0700160 * The default value is <code>false</code>.
Fyodor Kupolov0ffbfea2016-01-19 10:07:54 -0800161 * <p>In a managed profile, location sharing always reflects the primary user's setting, but
Amith Yamasani150514b2015-01-07 16:05:05 -0800162 * can be overridden and forced off by setting this restriction to true in the managed profile.
Amith Yamasani26af8292014-09-09 09:57:27 -0700163 *
Fyodor Kupolov0ffbfea2016-01-19 10:07:54 -0800164 * <p>Key for user restrictions.
165 * <p>Type: Boolean
Makoto Onuki068c54a2015-10-13 14:34:03 -0700166 * @see DevicePolicyManager#addUserRestriction(ComponentName, String)
167 * @see DevicePolicyManager#clearUserRestriction(ComponentName, String)
Maggie Benthall67944582013-02-22 14:58:27 -0500168 * @see #getUserRestrictions()
169 */
Amith Yamasani71e6c692013-03-24 17:39:28 -0700170 public static final String DISALLOW_SHARE_LOCATION = "no_share_location";
Maggie Benthall67944582013-02-22 14:58:27 -0500171
Maggie Benthalla12fccf2013-03-14 18:02:12 -0400172 /**
Amith Yamasani26af8292014-09-09 09:57:27 -0700173 * Specifies if a user is disallowed from enabling the
Maggie Benthalla12fccf2013-03-14 18:02:12 -0400174 * "Unknown Sources" setting, that allows installation of apps from unknown sources.
175 * The default value is <code>false</code>.
Amith Yamasani26af8292014-09-09 09:57:27 -0700176 *
Fyodor Kupolov0ffbfea2016-01-19 10:07:54 -0800177 * <p>Key for user restrictions.
178 * <p>Type: Boolean
Makoto Onuki068c54a2015-10-13 14:34:03 -0700179 * @see DevicePolicyManager#addUserRestriction(ComponentName, String)
180 * @see DevicePolicyManager#clearUserRestriction(ComponentName, String)
Maggie Benthalla12fccf2013-03-14 18:02:12 -0400181 * @see #getUserRestrictions()
182 */
183 public static final String DISALLOW_INSTALL_UNKNOWN_SOURCES = "no_install_unknown_sources";
184
185 /**
Amith Yamasani26af8292014-09-09 09:57:27 -0700186 * Specifies if a user is disallowed from configuring bluetooth.
Nicolas Prevot1c4c4422015-02-16 11:32:21 +0000187 * This does <em>not</em> restrict the user from turning bluetooth on or off.
Maggie Benthalla12fccf2013-03-14 18:02:12 -0400188 * The default value is <code>false</code>.
Fyodor Kupolov0ffbfea2016-01-19 10:07:54 -0800189 * <p>This restriction has no effect in a managed profile.
Amith Yamasani26af8292014-09-09 09:57:27 -0700190 *
Fyodor Kupolov0ffbfea2016-01-19 10:07:54 -0800191 * <p>Key for user restrictions.
192 * <p>Type: Boolean
Makoto Onuki068c54a2015-10-13 14:34:03 -0700193 * @see DevicePolicyManager#addUserRestriction(ComponentName, String)
194 * @see DevicePolicyManager#clearUserRestriction(ComponentName, String)
Maggie Benthalla12fccf2013-03-14 18:02:12 -0400195 * @see #getUserRestrictions()
196 */
197 public static final String DISALLOW_CONFIG_BLUETOOTH = "no_config_bluetooth";
198
Maggie Benthalla12fccf2013-03-14 18:02:12 -0400199 /**
Amith Yamasani26af8292014-09-09 09:57:27 -0700200 * Specifies if a user is disallowed from transferring files over
Amith Yamasanic34dc7c2014-09-18 09:42:42 -0700201 * USB. This can only be set by device owners and profile owners on the primary user.
202 * The default value is <code>false</code>.
Amith Yamasani26af8292014-09-09 09:57:27 -0700203 *
Fyodor Kupolov0ffbfea2016-01-19 10:07:54 -0800204 * <p>Key for user restrictions.
205 * <p>Type: Boolean
Makoto Onuki068c54a2015-10-13 14:34:03 -0700206 * @see DevicePolicyManager#addUserRestriction(ComponentName, String)
207 * @see DevicePolicyManager#clearUserRestriction(ComponentName, String)
Maggie Benthalla12fccf2013-03-14 18:02:12 -0400208 * @see #getUserRestrictions()
209 */
210 public static final String DISALLOW_USB_FILE_TRANSFER = "no_usb_file_transfer";
211
Emily Bernierb223f732013-04-11 15:46:36 -0400212 /**
Amith Yamasani26af8292014-09-09 09:57:27 -0700213 * Specifies if a user is disallowed from configuring user
Emily Bernierb223f732013-04-11 15:46:36 -0400214 * credentials. The default value is <code>false</code>.
Amith Yamasani26af8292014-09-09 09:57:27 -0700215 *
Fyodor Kupolov0ffbfea2016-01-19 10:07:54 -0800216 * <p>Key for user restrictions.
217 * <p>Type: Boolean
Makoto Onuki068c54a2015-10-13 14:34:03 -0700218 * @see DevicePolicyManager#addUserRestriction(ComponentName, String)
219 * @see DevicePolicyManager#clearUserRestriction(ComponentName, String)
Emily Bernierb223f732013-04-11 15:46:36 -0400220 * @see #getUserRestrictions()
221 */
222 public static final String DISALLOW_CONFIG_CREDENTIALS = "no_config_credentials";
223
224 /**
Amith Yamasani150514b2015-01-07 16:05:05 -0800225 * When set on the primary user this specifies if the user can remove other users.
226 * When set on a secondary user, this specifies if the user can remove itself.
227 * This restriction has no effect on managed profiles.
228 * The default value is <code>false</code>.
Amith Yamasani26af8292014-09-09 09:57:27 -0700229 *
Fyodor Kupolov0ffbfea2016-01-19 10:07:54 -0800230 * <p>Key for user restrictions.
231 * <p>Type: Boolean
Makoto Onuki068c54a2015-10-13 14:34:03 -0700232 * @see DevicePolicyManager#addUserRestriction(ComponentName, String)
233 * @see DevicePolicyManager#clearUserRestriction(ComponentName, String)
Emily Bernierb223f732013-04-11 15:46:36 -0400234 * @see #getUserRestrictions()
235 */
236 public static final String DISALLOW_REMOVE_USER = "no_remove_user";
237
Julia Reynoldsd46d0f92014-04-23 15:23:24 -0400238 /**
Amith Yamasani26af8292014-09-09 09:57:27 -0700239 * Specifies if a user is disallowed from enabling or
Julia Reynoldsd46d0f92014-04-23 15:23:24 -0400240 * accessing debugging features. The default value is <code>false</code>.
Amith Yamasani26af8292014-09-09 09:57:27 -0700241 *
Fyodor Kupolov0ffbfea2016-01-19 10:07:54 -0800242 * <p>Key for user restrictions.
243 * <p>Type: Boolean
Makoto Onuki068c54a2015-10-13 14:34:03 -0700244 * @see DevicePolicyManager#addUserRestriction(ComponentName, String)
245 * @see DevicePolicyManager#clearUserRestriction(ComponentName, String)
Julia Reynoldsd46d0f92014-04-23 15:23:24 -0400246 * @see #getUserRestrictions()
247 */
248 public static final String DISALLOW_DEBUGGING_FEATURES = "no_debugging_features";
249
250 /**
Amith Yamasani26af8292014-09-09 09:57:27 -0700251 * Specifies if a user is disallowed from configuring VPN.
Julia Reynoldsd46d0f92014-04-23 15:23:24 -0400252 * The default value is <code>false</code>.
Nicolas Prevot298f98f2015-05-22 16:43:03 -0700253 * This restriction has an effect in a managed profile only from
Dianne Hackborn0e3de6c2015-07-29 15:20:21 -0700254 * {@link android.os.Build.VERSION_CODES#M}
Amith Yamasani26af8292014-09-09 09:57:27 -0700255 *
Fyodor Kupolov0ffbfea2016-01-19 10:07:54 -0800256 * <p>Key for user restrictions.
257 * <p>Type: Boolean
Makoto Onuki068c54a2015-10-13 14:34:03 -0700258 * @see DevicePolicyManager#addUserRestriction(ComponentName, String)
259 * @see DevicePolicyManager#clearUserRestriction(ComponentName, String)
Julia Reynoldsd46d0f92014-04-23 15:23:24 -0400260 * @see #getUserRestrictions()
261 */
262 public static final String DISALLOW_CONFIG_VPN = "no_config_vpn";
263
264 /**
Amith Yamasani26af8292014-09-09 09:57:27 -0700265 * Specifies if a user is disallowed from configuring Tethering
Amith Yamasanic34dc7c2014-09-18 09:42:42 -0700266 * & portable hotspots. This can only be set by device owners and profile owners on the
267 * primary user. The default value is <code>false</code>.
Amith Yamasani26af8292014-09-09 09:57:27 -0700268 *
Fyodor Kupolov0ffbfea2016-01-19 10:07:54 -0800269 * <p>Key for user restrictions.
270 * <p>Type: Boolean
Makoto Onuki068c54a2015-10-13 14:34:03 -0700271 * @see DevicePolicyManager#addUserRestriction(ComponentName, String)
272 * @see DevicePolicyManager#clearUserRestriction(ComponentName, String)
Julia Reynoldsd46d0f92014-04-23 15:23:24 -0400273 * @see #getUserRestrictions()
274 */
275 public static final String DISALLOW_CONFIG_TETHERING = "no_config_tethering";
276
277 /**
Stuart Scotte3e314d2015-04-20 14:07:45 -0700278 * Specifies if a user is disallowed from resetting network settings
279 * from Settings. This can only be set by device owners and profile owners on the primary user.
280 * The default value is <code>false</code>.
Fyodor Kupolov0ffbfea2016-01-19 10:07:54 -0800281 * <p>This restriction has no effect on secondary users and managed profiles since only the
Stuart Scotte3e314d2015-04-20 14:07:45 -0700282 * primary user can reset the network settings of the device.
283 *
Fyodor Kupolov0ffbfea2016-01-19 10:07:54 -0800284 * <p>Key for user restrictions.
285 * <p>Type: Boolean
Makoto Onuki068c54a2015-10-13 14:34:03 -0700286 * @see DevicePolicyManager#addUserRestriction(ComponentName, String)
287 * @see DevicePolicyManager#clearUserRestriction(ComponentName, String)
Stuart Scotte3e314d2015-04-20 14:07:45 -0700288 * @see #getUserRestrictions()
289 */
290 public static final String DISALLOW_NETWORK_RESET = "no_network_reset";
291
292 /**
Amith Yamasani26af8292014-09-09 09:57:27 -0700293 * Specifies if a user is disallowed from factory resetting
Amith Yamasanic34dc7c2014-09-18 09:42:42 -0700294 * from Settings. This can only be set by device owners and profile owners on the primary user.
295 * The default value is <code>false</code>.
Fyodor Kupolov0ffbfea2016-01-19 10:07:54 -0800296 * <p>This restriction has no effect on secondary users and managed profiles since only the
Amith Yamasani150514b2015-01-07 16:05:05 -0800297 * primary user can factory reset the device.
Amith Yamasani26af8292014-09-09 09:57:27 -0700298 *
Fyodor Kupolov0ffbfea2016-01-19 10:07:54 -0800299 * <p>Key for user restrictions.
300 * <p>Type: Boolean
Makoto Onuki068c54a2015-10-13 14:34:03 -0700301 * @see DevicePolicyManager#addUserRestriction(ComponentName, String)
302 * @see DevicePolicyManager#clearUserRestriction(ComponentName, String)
Julia Reynoldsd46d0f92014-04-23 15:23:24 -0400303 * @see #getUserRestrictions()
304 */
305 public static final String DISALLOW_FACTORY_RESET = "no_factory_reset";
306
307 /**
Amith Yamasani26af8292014-09-09 09:57:27 -0700308 * Specifies if a user is disallowed from adding new users and
Amith Yamasanic34dc7c2014-09-18 09:42:42 -0700309 * profiles. This can only be set by device owners and profile owners on the primary user.
310 * The default value is <code>false</code>.
Fyodor Kupolov0ffbfea2016-01-19 10:07:54 -0800311 * <p>This restriction has no effect on secondary users and managed profiles since only the
Amith Yamasani150514b2015-01-07 16:05:05 -0800312 * primary user can add other users.
Amith Yamasani26af8292014-09-09 09:57:27 -0700313 *
Fyodor Kupolov0ffbfea2016-01-19 10:07:54 -0800314 * <p>Key for user restrictions.
315 * <p>Type: Boolean
Makoto Onuki068c54a2015-10-13 14:34:03 -0700316 * @see DevicePolicyManager#addUserRestriction(ComponentName, String)
317 * @see DevicePolicyManager#clearUserRestriction(ComponentName, String)
Julia Reynoldsd46d0f92014-04-23 15:23:24 -0400318 * @see #getUserRestrictions()
319 */
320 public static final String DISALLOW_ADD_USER = "no_add_user";
321
322 /**
Amith Yamasani26af8292014-09-09 09:57:27 -0700323 * Specifies if a user is disallowed from disabling application
Julia Reynoldsd46d0f92014-04-23 15:23:24 -0400324 * verification. The default value is <code>false</code>.
Amith Yamasani26af8292014-09-09 09:57:27 -0700325 *
Fyodor Kupolov0ffbfea2016-01-19 10:07:54 -0800326 * <p>Key for user restrictions.
327 * <p>Type: Boolean
Makoto Onuki068c54a2015-10-13 14:34:03 -0700328 * @see DevicePolicyManager#addUserRestriction(ComponentName, String)
329 * @see DevicePolicyManager#clearUserRestriction(ComponentName, String)
Julia Reynoldsd46d0f92014-04-23 15:23:24 -0400330 * @see #getUserRestrictions()
331 */
332 public static final String ENSURE_VERIFY_APPS = "ensure_verify_apps";
333
334 /**
Amith Yamasani26af8292014-09-09 09:57:27 -0700335 * Specifies if a user is disallowed from configuring cell
Amith Yamasanic34dc7c2014-09-18 09:42:42 -0700336 * broadcasts. This can only be set by device owners and profile owners on the primary user.
337 * The default value is <code>false</code>.
Fyodor Kupolov0ffbfea2016-01-19 10:07:54 -0800338 * <p>This restriction has no effect on secondary users and managed profiles since only the
Amith Yamasani150514b2015-01-07 16:05:05 -0800339 * primary user can configure cell broadcasts.
Amith Yamasani26af8292014-09-09 09:57:27 -0700340 *
Fyodor Kupolov0ffbfea2016-01-19 10:07:54 -0800341 * <p>Key for user restrictions.
342 * <p>Type: Boolean
Makoto Onuki068c54a2015-10-13 14:34:03 -0700343 * @see DevicePolicyManager#addUserRestriction(ComponentName, String)
344 * @see DevicePolicyManager#clearUserRestriction(ComponentName, String)
Julia Reynoldsd46d0f92014-04-23 15:23:24 -0400345 * @see #getUserRestrictions()
346 */
347 public static final String DISALLOW_CONFIG_CELL_BROADCASTS = "no_config_cell_broadcasts";
348
349 /**
Amith Yamasani26af8292014-09-09 09:57:27 -0700350 * Specifies if a user is disallowed from configuring mobile
Amith Yamasanic34dc7c2014-09-18 09:42:42 -0700351 * networks. This can only be set by device owners and profile owners on the primary user.
352 * The default value is <code>false</code>.
Fyodor Kupolov0ffbfea2016-01-19 10:07:54 -0800353 * <p>This restriction has no effect on secondary users and managed profiles since only the
Amith Yamasani150514b2015-01-07 16:05:05 -0800354 * primary user can configure mobile networks.
Amith Yamasani26af8292014-09-09 09:57:27 -0700355 *
Fyodor Kupolov0ffbfea2016-01-19 10:07:54 -0800356 * <p>Key for user restrictions.
357 * <p>Type: Boolean
Makoto Onuki068c54a2015-10-13 14:34:03 -0700358 * @see DevicePolicyManager#addUserRestriction(ComponentName, String)
359 * @see DevicePolicyManager#clearUserRestriction(ComponentName, String)
Julia Reynoldsd46d0f92014-04-23 15:23:24 -0400360 * @see #getUserRestrictions()
361 */
362 public static final String DISALLOW_CONFIG_MOBILE_NETWORKS = "no_config_mobile_networks";
363
364 /**
Amith Yamasani26af8292014-09-09 09:57:27 -0700365 * Specifies if a user is disallowed from modifying
Julia Reynoldsc617f812014-07-25 16:32:27 -0400366 * applications in Settings or launchers. The following actions will not be allowed when this
367 * restriction is enabled:
368 * <li>uninstalling apps</li>
369 * <li>disabling apps</li>
370 * <li>clearing app caches</li>
371 * <li>clearing app data</li>
372 * <li>force stopping apps</li>
373 * <li>clearing app defaults</li>
374 * <p>
375 * The default value is <code>false</code>.
Amith Yamasani26af8292014-09-09 09:57:27 -0700376 *
Fyodor Kupolov0ffbfea2016-01-19 10:07:54 -0800377 * <p>Key for user restrictions.
378 * <p>Type: Boolean
Makoto Onuki068c54a2015-10-13 14:34:03 -0700379 * @see DevicePolicyManager#addUserRestriction(ComponentName, String)
380 * @see DevicePolicyManager#clearUserRestriction(ComponentName, String)
Julia Reynoldsd46d0f92014-04-23 15:23:24 -0400381 * @see #getUserRestrictions()
382 */
Julia Reynolds36fbc8d2014-06-18 09:26:30 -0400383 public static final String DISALLOW_APPS_CONTROL = "no_control_apps";
Julia Reynoldsd46d0f92014-04-23 15:23:24 -0400384
Emily Bernier394a6cd2014-05-07 12:49:20 -0400385 /**
Amith Yamasani26af8292014-09-09 09:57:27 -0700386 * Specifies if a user is disallowed from mounting
Amith Yamasanic34dc7c2014-09-18 09:42:42 -0700387 * physical external media. This can only be set by device owners and profile owners on the
388 * primary user. The default value is <code>false</code>.
Amith Yamasani26af8292014-09-09 09:57:27 -0700389 *
Fyodor Kupolov0ffbfea2016-01-19 10:07:54 -0800390 * <p>Key for user restrictions.
391 * <p>Type: Boolean
Makoto Onuki068c54a2015-10-13 14:34:03 -0700392 * @see DevicePolicyManager#addUserRestriction(ComponentName, String)
393 * @see DevicePolicyManager#clearUserRestriction(ComponentName, String)
Emily Bernier394a6cd2014-05-07 12:49:20 -0400394 * @see #getUserRestrictions()
395 */
396 public static final String DISALLOW_MOUNT_PHYSICAL_MEDIA = "no_physical_media";
397
398 /**
Amith Yamasani26af8292014-09-09 09:57:27 -0700399 * Specifies if a user is disallowed from adjusting microphone
Amith Yamasanic34dc7c2014-09-18 09:42:42 -0700400 * volume. If set, the microphone will be muted. This can only be set by device owners
401 * and profile owners on the primary user. The default value is <code>false</code>.
Amith Yamasani26af8292014-09-09 09:57:27 -0700402 *
Fyodor Kupolov0ffbfea2016-01-19 10:07:54 -0800403 * <p>Key for user restrictions.
404 * <p>Type: Boolean
Makoto Onuki068c54a2015-10-13 14:34:03 -0700405 * @see DevicePolicyManager#addUserRestriction(ComponentName, String)
406 * @see DevicePolicyManager#clearUserRestriction(ComponentName, String)
Emily Bernier394a6cd2014-05-07 12:49:20 -0400407 * @see #getUserRestrictions()
408 */
409 public static final String DISALLOW_UNMUTE_MICROPHONE = "no_unmute_microphone";
410
411 /**
Amith Yamasani26af8292014-09-09 09:57:27 -0700412 * Specifies if a user is disallowed from adjusting the master
Amith Yamasanic34dc7c2014-09-18 09:42:42 -0700413 * volume. If set, the master volume will be muted. This can only be set by device owners
414 * and profile owners on the primary user. The default value is <code>false</code>.
Amith Yamasani26af8292014-09-09 09:57:27 -0700415 *
Fyodor Kupolov0ffbfea2016-01-19 10:07:54 -0800416 * <p>Key for user restrictions.
417 * <p>Type: Boolean
Makoto Onuki068c54a2015-10-13 14:34:03 -0700418 * @see DevicePolicyManager#addUserRestriction(ComponentName, String)
419 * @see DevicePolicyManager#clearUserRestriction(ComponentName, String)
Emily Bernier394a6cd2014-05-07 12:49:20 -0400420 * @see #getUserRestrictions()
421 */
422 public static final String DISALLOW_ADJUST_VOLUME = "no_adjust_volume";
423
Amith Yamasani9f6c25f2014-05-16 14:49:15 -0700424 /**
Amith Yamasani26af8292014-09-09 09:57:27 -0700425 * Specifies that the user is not allowed to make outgoing
Amith Yamasani390989d2014-07-17 10:52:03 -0700426 * phone calls. Emergency calls are still permitted.
Amith Yamasani9f6c25f2014-05-16 14:49:15 -0700427 * The default value is <code>false</code>.
Tony Makeb83ab52016-02-22 18:36:08 +0000428 * <p>This restriction has no effect on managed profiles.
Amith Yamasani26af8292014-09-09 09:57:27 -0700429 *
Fyodor Kupolov0ffbfea2016-01-19 10:07:54 -0800430 * <p>Key for user restrictions.
431 * <p>Type: Boolean
Makoto Onuki068c54a2015-10-13 14:34:03 -0700432 * @see DevicePolicyManager#addUserRestriction(ComponentName, String)
433 * @see DevicePolicyManager#clearUserRestriction(ComponentName, String)
Amith Yamasani9f6c25f2014-05-16 14:49:15 -0700434 * @see #getUserRestrictions()
435 */
Amith Yamasani390989d2014-07-17 10:52:03 -0700436 public static final String DISALLOW_OUTGOING_CALLS = "no_outgoing_calls";
437
438 /**
Amith Yamasani26af8292014-09-09 09:57:27 -0700439 * Specifies that the user is not allowed to send or receive
Amith Yamasanic34dc7c2014-09-18 09:42:42 -0700440 * SMS messages. The default value is <code>false</code>.
Amith Yamasani26af8292014-09-09 09:57:27 -0700441 *
Fyodor Kupolov0ffbfea2016-01-19 10:07:54 -0800442 * <p>Key for user restrictions.
443 * <p>Type: Boolean
Makoto Onuki068c54a2015-10-13 14:34:03 -0700444 * @see DevicePolicyManager#addUserRestriction(ComponentName, String)
445 * @see DevicePolicyManager#clearUserRestriction(ComponentName, String)
Amith Yamasani390989d2014-07-17 10:52:03 -0700446 * @see #getUserRestrictions()
447 */
448 public static final String DISALLOW_SMS = "no_sms";
Amith Yamasani9f6c25f2014-05-16 14:49:15 -0700449
Jason Monk1c7c3192014-06-26 12:52:18 -0400450 /**
Jeff Sharkey2cc03e52015-03-20 11:24:04 -0700451 * Specifies if the user is not allowed to have fun. In some cases, the
452 * device owner may wish to prevent the user from experiencing amusement or
453 * joy while using the device. The default value is <code>false</code>.
454 *
Fyodor Kupolov0ffbfea2016-01-19 10:07:54 -0800455 * <p>Key for user restrictions.
456 * <p>Type: Boolean
Makoto Onuki068c54a2015-10-13 14:34:03 -0700457 * @see DevicePolicyManager#addUserRestriction(ComponentName, String)
458 * @see DevicePolicyManager#clearUserRestriction(ComponentName, String)
Jeff Sharkey2cc03e52015-03-20 11:24:04 -0700459 * @see #getUserRestrictions()
460 */
461 public static final String DISALLOW_FUN = "no_fun";
462
463 /**
Amith Yamasani26af8292014-09-09 09:57:27 -0700464 * Specifies that windows besides app windows should not be
Jason Monk1c7c3192014-06-26 12:52:18 -0400465 * created. This will block the creation of the following types of windows.
466 * <li>{@link LayoutParams#TYPE_TOAST}</li>
467 * <li>{@link LayoutParams#TYPE_PHONE}</li>
468 * <li>{@link LayoutParams#TYPE_PRIORITY_PHONE}</li>
469 * <li>{@link LayoutParams#TYPE_SYSTEM_ALERT}</li>
470 * <li>{@link LayoutParams#TYPE_SYSTEM_ERROR}</li>
471 * <li>{@link LayoutParams#TYPE_SYSTEM_OVERLAY}</li>
472 *
Amith Yamasanic34dc7c2014-09-18 09:42:42 -0700473 * <p>This can only be set by device owners and profile owners on the primary user.
474 * The default value is <code>false</code>.
Amith Yamasani26af8292014-09-09 09:57:27 -0700475 *
Fyodor Kupolov0ffbfea2016-01-19 10:07:54 -0800476 * <p>Key for user restrictions.
477 * <p>Type: Boolean
Makoto Onuki068c54a2015-10-13 14:34:03 -0700478 * @see DevicePolicyManager#addUserRestriction(ComponentName, String)
479 * @see DevicePolicyManager#clearUserRestriction(ComponentName, String)
Jason Monk1c7c3192014-06-26 12:52:18 -0400480 * @see #getUserRestrictions()
481 */
482 public static final String DISALLOW_CREATE_WINDOWS = "no_create_windows";
483
Nicolas Prevotf1939902014-06-25 09:29:02 +0100484 /**
Amith Yamasani26af8292014-09-09 09:57:27 -0700485 * Specifies if what is copied in the clipboard of this profile can
Nicolas Prevotf1939902014-06-25 09:29:02 +0100486 * be pasted in related profiles. Does not restrict if the clipboard of related profiles can be
487 * pasted in this profile.
488 * The default value is <code>false</code>.
Amith Yamasani26af8292014-09-09 09:57:27 -0700489 *
Fyodor Kupolov0ffbfea2016-01-19 10:07:54 -0800490 * <p>Key for user restrictions.
491 * <p>Type: Boolean
Makoto Onuki068c54a2015-10-13 14:34:03 -0700492 * @see DevicePolicyManager#addUserRestriction(ComponentName, String)
493 * @see DevicePolicyManager#clearUserRestriction(ComponentName, String)
Nicolas Prevotf1939902014-06-25 09:29:02 +0100494 * @see #getUserRestrictions()
495 */
496 public static final String DISALLOW_CROSS_PROFILE_COPY_PASTE = "no_cross_profile_copy_paste";
497
Amith Yamasani26af8292014-09-09 09:57:27 -0700498 /**
499 * Specifies if the user is not allowed to use NFC to beam out data from apps.
500 * The default value is <code>false</code>.
501 *
Fyodor Kupolov0ffbfea2016-01-19 10:07:54 -0800502 * <p>Key for user restrictions.
503 * <p>Type: Boolean
Makoto Onuki068c54a2015-10-13 14:34:03 -0700504 * @see DevicePolicyManager#addUserRestriction(ComponentName, String)
505 * @see DevicePolicyManager#clearUserRestriction(ComponentName, String)
Amith Yamasani26af8292014-09-09 09:57:27 -0700506 * @see #getUserRestrictions()
507 */
508 public static final String DISALLOW_OUTGOING_BEAM = "no_outgoing_beam";
509
Sander Alewijnse53d63dc2014-11-07 21:43:00 +0000510 /**
Oleksandr Peletskyif2519812016-01-26 20:16:06 +0100511 * Hidden user restriction to disallow access to wallpaper manager APIs. This restriction
512 * generally means that wallpapers are not supported for the particular user. This user
513 * restriction is always set for managed profiles, because such profiles don't have wallpapers.
Benjamin Franzf3ece362015-02-11 10:51:10 +0000514 * @hide
Oleksandr Peletskyif2519812016-01-26 20:16:06 +0100515 * @see #DISALLOW_SET_WALLPAPER
Makoto Onuki068c54a2015-10-13 14:34:03 -0700516 * @see DevicePolicyManager#addUserRestriction(ComponentName, String)
517 * @see DevicePolicyManager#clearUserRestriction(ComponentName, String)
Benjamin Franzf3ece362015-02-11 10:51:10 +0000518 * @see #getUserRestrictions()
519 */
520 public static final String DISALLOW_WALLPAPER = "no_wallpaper";
521
522 /**
Oleksandr Peletskyif2519812016-01-26 20:16:06 +0100523 * User restriction to disallow setting a wallpaper. Profile owner and device owner
524 * are able to set wallpaper regardless of this restriction.
525 * The default value is <code>false</code>.
526 *
527 * <p>Key for user restrictions.
528 * <p>Type: Boolean
529 * @see DevicePolicyManager#addUserRestriction(ComponentName, String)
530 * @see DevicePolicyManager#clearUserRestriction(ComponentName, String)
531 * @see #getUserRestrictions()
532 */
533 public static final String DISALLOW_SET_WALLPAPER = "no_set_wallpaper";
534
535 /**
Benjamin Franzbff46ba2015-03-05 18:33:51 +0000536 * Specifies if the user is not allowed to reboot the device into safe boot mode.
537 * This can only be set by device owners and profile owners on the primary user.
538 * The default value is <code>false</code>.
539 *
Fyodor Kupolov0ffbfea2016-01-19 10:07:54 -0800540 * <p>Key for user restrictions.
541 * <p>Type: Boolean
Makoto Onuki068c54a2015-10-13 14:34:03 -0700542 * @see DevicePolicyManager#addUserRestriction(ComponentName, String)
543 * @see DevicePolicyManager#clearUserRestriction(ComponentName, String)
Benjamin Franzbff46ba2015-03-05 18:33:51 +0000544 * @see #getUserRestrictions()
545 */
546 public static final String DISALLOW_SAFE_BOOT = "no_safe_boot";
547
548 /**
Fyodor Kupolovb5013302015-04-17 17:59:14 -0700549 * Specifies if a user is not allowed to record audio. This restriction is always enabled for
550 * background users. The default value is <code>false</code>.
551 *
Makoto Onuki068c54a2015-10-13 14:34:03 -0700552 * @see DevicePolicyManager#addUserRestriction(ComponentName, String)
553 * @see DevicePolicyManager#clearUserRestriction(ComponentName, String)
Fyodor Kupolovb5013302015-04-17 17:59:14 -0700554 * @see #getUserRestrictions()
555 * @hide
556 */
557 public static final String DISALLOW_RECORD_AUDIO = "no_record_audio";
558
559 /**
Fyodor Kupolov9cbfc9e2015-10-07 15:52:33 -0700560 * Specifies if a user is not allowed to run in the background and should be stopped during
561 * user switch. The default value is <code>false</code>.
562 *
563 * <p>This restriction can be set by device owners and profile owners.
564 *
565 * @see DevicePolicyManager#addUserRestriction(ComponentName, String)
566 * @see DevicePolicyManager#clearUserRestriction(ComponentName, String)
567 * @see #getUserRestrictions()
568 * @hide
569 */
570 public static final String DISALLOW_RUN_IN_BACKGROUND = "no_run_in_background";
571
572 /**
Makoto Onuki759a7632015-10-28 16:43:10 -0700573 * Specifies if a user is not allowed to use the camera.
574 *
575 * @see DevicePolicyManager#addUserRestriction(ComponentName, String)
576 * @see DevicePolicyManager#clearUserRestriction(ComponentName, String)
577 * @see #getUserRestrictions()
578 * @hide
579 */
580 public static final String DISALLOW_CAMERA = "no_camera";
581
582 /**
Tony Makc1205112016-07-22 16:02:59 +0100583 * Specifies if a user is not allowed to unmute the device's master volume.
584 *
585 * @see DevicePolicyManager#setMasterVolumeMuted(ComponentName, boolean)
586 * @see DevicePolicyManager#clearUserRestriction(ComponentName, String)
587 * @see #getUserRestrictions()
588 * @hide
589 */
590 public static final String DISALLLOW_UNMUTE_DEVICE = "disallow_unmute_device";
591
592 /**
Mahaver Chopradea471e2015-12-17 11:02:37 +0000593 * Specifies if a user is not allowed to use cellular data when roaming. This can only be set by
594 * device owners. The default value is <code>false</code>.
595 *
596 * @see DevicePolicyManager#addUserRestriction(ComponentName, String)
597 * @see DevicePolicyManager#clearUserRestriction(ComponentName, String)
598 * @see #getUserRestrictions()
599 */
600 public static final String DISALLOW_DATA_ROAMING = "no_data_roaming";
601
602 /**
Oleksandr Peletskyi7f1f1df2016-01-18 15:40:21 +0100603 * Specifies if a user is not allowed to change their icon. Device owner and profile owner
604 * can set this restriction. When it is set by device owner, only the target user will be
605 * affected. The default value is <code>false</code>.
606 *
607 * <p>Key for user restrictions.
Oleksandr Peletskyi7f1f1df2016-01-18 15:40:21 +0100608 * <p>Type: Boolean
Oleksandr Peletskyi7f1f1df2016-01-18 15:40:21 +0100609 * @see DevicePolicyManager#addUserRestriction(ComponentName, String)
610 * @see DevicePolicyManager#clearUserRestriction(ComponentName, String)
611 * @see #getUserRestrictions()
612 */
613 public static final String DISALLOW_SET_USER_ICON = "no_set_user_icon";
614
615 /**
Mahaver Chopra3d9805d2016-07-07 16:25:05 +0100616 * Specifies if a user is not allowed to enable the oem unlock setting. The default value is
Esteban Talaverac48b20f2016-08-11 11:23:40 +0100617 * <code>false</code>. Setting this restriction has no effect if the bootloader is already
618 * unlocked.
Mahaver Chopra3d9805d2016-07-07 16:25:05 +0100619 *
620 * @see DevicePolicyManager#addUserRestriction(ComponentName, String)
621 * @see DevicePolicyManager#clearUserRestriction(ComponentName, String)
622 * @see #getUserRestrictions()
623 * @hide
624 */
625 public static final String DISALLOW_OEM_UNLOCK = "no_oem_unlock";
626
627 /**
Nicolas Prevotf0029c12015-06-25 14:25:41 -0700628 * Allows apps in the parent profile to handle web links from the managed profile.
629 *
Nicolas Prevot9edbda12015-06-17 11:09:48 -0700630 * This user restriction has an effect only in a managed profile.
631 * If set:
632 * Intent filters of activities in the parent profile with action
633 * {@link android.content.Intent#ACTION_VIEW},
634 * category {@link android.content.Intent#CATEGORY_BROWSABLE}, scheme http or https, and which
635 * define a host can handle intents from the managed profile.
636 * The default value is <code>false</code>.
637 *
Fyodor Kupolov0ffbfea2016-01-19 10:07:54 -0800638 * <p>Key for user restrictions.
639 * <p>Type: Boolean
Makoto Onuki068c54a2015-10-13 14:34:03 -0700640 * @see DevicePolicyManager#addUserRestriction(ComponentName, String)
641 * @see DevicePolicyManager#clearUserRestriction(ComponentName, String)
Nicolas Prevot9edbda12015-06-17 11:09:48 -0700642 * @see #getUserRestrictions()
643 */
Nicolas Prevotf0029c12015-06-25 14:25:41 -0700644 public static final String ALLOW_PARENT_PROFILE_APP_LINKING
645 = "allow_parent_profile_app_linking";
Nicolas Prevot9edbda12015-06-17 11:09:48 -0700646
647 /**
Sander Alewijnse53d63dc2014-11-07 21:43:00 +0000648 * Application restriction key that is used to indicate the pending arrival
649 * of real restrictions for the app.
650 *
651 * <p>
652 * Applications that support restrictions should check for the presence of this key.
653 * A <code>true</code> value indicates that restrictions may be applied in the near
654 * future but are not available yet. It is the responsibility of any
655 * management application that sets this flag to update it when the final
656 * restrictions are enforced.
657 *
Fyodor Kupolov0ffbfea2016-01-19 10:07:54 -0800658 * <p>Key for application restrictions.
659 * <p>Type: Boolean
Nicolas Prevotb14ed952015-01-13 14:23:53 +0000660 * @see android.app.admin.DevicePolicyManager#setApplicationRestrictions(
661 * android.content.ComponentName, String, Bundle)
662 * @see android.app.admin.DevicePolicyManager#getApplicationRestrictions(
663 * android.content.ComponentName, String)
Sander Alewijnse53d63dc2014-11-07 21:43:00 +0000664 */
665 public static final String KEY_RESTRICTIONS_PENDING = "restrictions_pending";
666
Amith Yamasani12747872015-12-07 14:19:49 -0800667 private static final String ACTION_CREATE_USER = "android.os.action.CREATE_USER";
668
669 /**
670 * Extra containing a name for the user being created. Optional parameter passed to
671 * ACTION_CREATE_USER activity.
672 * @hide
673 */
674 public static final String EXTRA_USER_NAME = "android.os.extra.USER_NAME";
675
676 /**
677 * Extra containing account name for the user being created. Optional parameter passed to
678 * ACTION_CREATE_USER activity.
679 * @hide
680 */
681 public static final String EXTRA_USER_ACCOUNT_NAME = "android.os.extra.USER_ACCOUNT_NAME";
682
683 /**
684 * Extra containing account type for the user being created. Optional parameter passed to
685 * ACTION_CREATE_USER activity.
686 * @hide
687 */
688 public static final String EXTRA_USER_ACCOUNT_TYPE = "android.os.extra.USER_ACCOUNT_TYPE";
689
690 /**
691 * Extra containing account-specific data for the user being created. Optional parameter passed
692 * to ACTION_CREATE_USER activity.
693 * @hide
694 */
695 public static final String EXTRA_USER_ACCOUNT_OPTIONS
696 = "android.os.extra.USER_ACCOUNT_OPTIONS";
697
Amith Yamasani655d0e22013-06-12 14:19:10 -0700698 /** @hide */
699 public static final int PIN_VERIFICATION_FAILED_INCORRECT = -3;
700 /** @hide */
701 public static final int PIN_VERIFICATION_FAILED_NOT_SET = -2;
702 /** @hide */
703 public static final int PIN_VERIFICATION_SUCCESS = -1;
704
Amith Yamasani37ed8d12016-01-27 14:40:16 -0800705 /**
706 * Error result indicating that this user is not allowed to add other users on this device.
707 * This is a result code returned from the activity created by the intent
708 * {@link #createUserCreationIntent(String, String, String, PersistableBundle)}.
709 */
710 public static final int USER_CREATION_FAILED_NOT_PERMITTED = Activity.RESULT_FIRST_USER;
711
712 /**
713 * Error result indicating that no more users can be created on this device.
714 * This is a result code returned from the activity created by the intent
715 * {@link #createUserCreationIntent(String, String, String, PersistableBundle)}.
716 */
717 public static final int USER_CREATION_FAILED_NO_MORE_USERS = Activity.RESULT_FIRST_USER + 1;
718
Amith Yamasani7e99bc02013-04-16 18:24:51 -0700719 /** @hide */
Amith Yamasanic0688302015-10-30 10:40:03 -0700720 public static UserManager get(Context context) {
721 return (UserManager) context.getSystemService(Context.USER_SERVICE);
Amith Yamasani27db4682013-03-30 17:07:47 -0700722 }
Maggie Benthalla12fccf2013-03-14 18:02:12 -0400723
Amith Yamasani258848d2012-08-10 17:06:33 -0700724 /** @hide */
725 public UserManager(Context context, IUserManager service) {
726 mService = service;
727 mContext = context;
728 }
729
730 /**
Amith Yamasani06964342016-04-15 13:55:01 -0700731 * Returns whether this device supports multiple users with their own login and customizable
732 * space.
733 * @return whether the device supports multiple users.
Amith Yamasani258848d2012-08-10 17:06:33 -0700734 */
Jeff Sharkey4673e7e2012-09-19 13:14:30 -0700735 public static boolean supportsMultipleUsers() {
Kenny Guy1a447532014-02-20 21:55:32 +0000736 return getMaxSupportedUsers() > 1
737 && SystemProperties.getBoolean("fw.show_multiuserui",
738 Resources.getSystem().getBoolean(R.bool.config_enableMultiUserUI));
Amith Yamasani258848d2012-08-10 17:06:33 -0700739 }
740
Maggie Benthall67944582013-02-22 14:58:27 -0500741 /**
Xiaohui Chen7cb69df2015-07-13 16:01:01 -0700742 * @hide
743 * @return Whether the device is running with split system user. It means the system user and
744 * primary user are two separate users. Previously system user and primary user are combined as
745 * a single owner user. see @link {android.os.UserHandle#USER_OWNER}
746 */
747 public static boolean isSplitSystemUser() {
748 return SystemProperties.getBoolean("ro.fw.system_user_split", false);
749 }
750
751 /**
Fyodor Kupolov523c4042016-02-24 15:03:13 -0800752 * Returns whether switching users is currently allowed.
753 * <p>For instance switching users is not allowed if the current user is in a phone call,
754 * or system user hasn't been unlocked yet
755 * @hide
756 */
757 public boolean canSwitchUsers() {
758 boolean allowUserSwitchingWhenSystemUserLocked = Settings.Global.getInt(
759 mContext.getContentResolver(),
760 Settings.Global.ALLOW_USER_SWITCHING_WHEN_SYSTEM_USER_LOCKED, 0) != 0;
761 boolean isSystemUserUnlocked = isUserUnlocked(UserHandle.SYSTEM);
762 boolean inCall = TelephonyManager.getDefault().getCallState()
763 != TelephonyManager.CALL_STATE_IDLE;
764 return (allowUserSwitchingWhenSystemUserLocked || isSystemUserUnlocked) && !inCall;
765 }
766
767 /**
Amith Yamasani5760e172015-04-17 18:42:41 -0700768 * Returns the user handle for the user that this process is running under.
Jessica Hummelbe81c802014-04-22 15:49:22 +0100769 *
Amith Yamasani5760e172015-04-17 18:42:41 -0700770 * @return the user handle of this process.
Amith Yamasani258848d2012-08-10 17:06:33 -0700771 * @hide
Maggie Benthall67944582013-02-22 14:58:27 -0500772 */
Jeff Sharkey8588bc12016-01-06 16:47:42 -0700773 public @UserIdInt int getUserHandle() {
Dianne Hackborn79af1dd2012-08-16 16:42:52 -0700774 return UserHandle.myUserId();
Amith Yamasani258848d2012-08-10 17:06:33 -0700775 }
776
777 /**
Dianne Hackborn8832c182012-09-17 17:20:24 -0700778 * Returns the user name of the user making this call. This call is only
779 * available to applications on the system image; it requires the
780 * MANAGE_USERS permission.
Amith Yamasani258848d2012-08-10 17:06:33 -0700781 * @return the user name
782 */
783 public String getUserName() {
784 try {
785 return mService.getUserInfo(getUserHandle()).name;
786 } catch (RemoteException re) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700787 throw re.rethrowFromSystemServer();
Amith Yamasani258848d2012-08-10 17:06:33 -0700788 }
789 }
790
Dianne Hackborn67a101a2014-10-02 12:42:18 -0700791 /**
Dan Morrille4ab16a2012-09-20 20:25:55 -0700792 * Used to determine whether the user making this call is subject to
793 * teleportations.
Dianne Hackborn67a101a2014-10-02 12:42:18 -0700794 *
Dianne Hackborn955d8d62014-10-07 20:17:19 -0700795 * <p>As of {@link android.os.Build.VERSION_CODES#LOLLIPOP}, this method can
Dianne Hackborn67a101a2014-10-02 12:42:18 -0700796 * now automatically identify goats using advanced goat recognition technology.</p>
797 *
798 * @return Returns true if the user making this call is a goat.
Dan Morrille4ab16a2012-09-20 20:25:55 -0700799 */
800 public boolean isUserAGoat() {
Adam Powell988ae302014-09-17 17:58:33 -0700801 return mContext.getPackageManager()
802 .isPackageAvailable("com.coffeestainstudios.goatsimulator");
Dan Morrille4ab16a2012-09-20 20:25:55 -0700803 }
Amith Yamasanidf2e92a2013-03-01 17:04:38 -0800804
805 /**
Xiaohui Chen70f6c382015-04-28 14:21:43 -0700806 * Used to check if this process is running under the primary user. The primary user
807 * is the first human user on a device.
808 *
809 * @return whether this process is running under the primary user.
810 * @hide
811 */
812 public boolean isPrimaryUser() {
813 UserInfo user = getUserInfo(UserHandle.myUserId());
Amith Yamasanid35a89c2016-05-26 16:58:43 -0700814 return user != null && user.isPrimary();
Xiaohui Chen70f6c382015-04-28 14:21:43 -0700815 }
816
817 /**
Amith Yamasani5760e172015-04-17 18:42:41 -0700818 * Used to check if this process is running under the system user. The system user
819 * is the initial user that is implicitly created on first boot and hosts most of the
820 * system services.
821 *
822 * @return whether this process is running under the system user.
823 */
824 public boolean isSystemUser() {
Xiaohui Chen70f6c382015-04-28 14:21:43 -0700825 return UserHandle.myUserId() == UserHandle.USER_SYSTEM;
Amith Yamasani5760e172015-04-17 18:42:41 -0700826 }
Xiaohui Chen2b45f8a2015-08-04 15:12:23 -0700827
Amith Yamasani5760e172015-04-17 18:42:41 -0700828 /**
Amith Yamasani462ac3a2015-06-30 14:21:01 -0700829 * @hide
830 * Returns whether the caller is running as an admin user. There can be more than one admin
831 * user.
832 */
833 public boolean isAdminUser() {
Xiaohui Chen2b45f8a2015-08-04 15:12:23 -0700834 return isUserAdmin(UserHandle.myUserId());
835 }
836
837 /**
838 * @hide
839 * Returns whether the provided user is an admin user. There can be more than one admin
840 * user.
841 */
Jeff Sharkey8588bc12016-01-06 16:47:42 -0700842 public boolean isUserAdmin(@UserIdInt int userId) {
Xiaohui Chen2b45f8a2015-08-04 15:12:23 -0700843 UserInfo user = getUserInfo(userId);
844 return user != null && user.isAdmin();
Amith Yamasani462ac3a2015-06-30 14:21:01 -0700845 }
846
847 /**
Amith Yamasanie1375902013-04-13 16:48:35 -0700848 * Used to check if the user making this call is linked to another user. Linked users may have
Amith Yamasani46bc4eb2013-04-12 13:26:50 -0700849 * a reduced number of available apps, app restrictions and account restrictions.
Amith Yamasanie1375902013-04-13 16:48:35 -0700850 * @return whether the user making this call is a linked user
Amith Yamasani2555daf2013-04-25 13:39:27 -0700851 * @hide
Amith Yamasanidf2e92a2013-03-01 17:04:38 -0800852 */
Amith Yamasanie1375902013-04-13 16:48:35 -0700853 public boolean isLinkedUser() {
Amith Yamasanidf2e92a2013-03-01 17:04:38 -0800854 try {
Amith Yamasani71e6c692013-03-24 17:39:28 -0700855 return mService.isRestricted();
Amith Yamasanidf2e92a2013-03-01 17:04:38 -0800856 } catch (RemoteException re) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700857 throw re.rethrowFromSystemServer();
Amith Yamasanidf2e92a2013-03-01 17:04:38 -0800858 }
859 }
860
Amith Yamasani258848d2012-08-10 17:06:33 -0700861 /**
Fyodor Kupolov1c363152015-09-02 13:27:21 -0700862 * Checks if specified user can have restricted profile.
863 * @hide
864 */
Jeff Sharkey8588bc12016-01-06 16:47:42 -0700865 public boolean canHaveRestrictedProfile(@UserIdInt int userId) {
Fyodor Kupolov1c363152015-09-02 13:27:21 -0700866 try {
867 return mService.canHaveRestrictedProfile(userId);
868 } catch (RemoteException re) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700869 throw re.rethrowFromSystemServer();
Fyodor Kupolov1c363152015-09-02 13:27:21 -0700870 }
871 }
872
873 /**
Amith Yamasani1e9c2182014-06-11 17:25:51 -0700874 * Checks if the calling app is running as a guest user.
875 * @return whether the caller is a guest user.
876 * @hide
877 */
878 public boolean isGuestUser() {
879 UserInfo user = getUserInfo(UserHandle.myUserId());
Amith Yamasanid35a89c2016-05-26 16:58:43 -0700880 return user != null && user.isGuest();
881 }
882
883 /**
Amith Yamasani1c41dc82016-06-28 16:13:15 -0700884 * Checks if the calling app is running in a demo user. When running in a demo user,
885 * apps can be more helpful to the user, or explain their features in more detail.
886 *
Amith Yamasanid35a89c2016-05-26 16:58:43 -0700887 * @return whether the caller is a demo user.
Amith Yamasanid35a89c2016-05-26 16:58:43 -0700888 */
889 public boolean isDemoUser() {
Amith Yamasani1c41dc82016-06-28 16:13:15 -0700890 try {
891 return mService.isDemoUser(UserHandle.myUserId());
892 } catch (RemoteException re) {
893 throw re.rethrowFromSystemServer();
894 }
Amith Yamasani1e9c2182014-06-11 17:25:51 -0700895 }
896
897 /**
Amith Yamasani0e8d7d62014-09-03 13:17:28 -0700898 * Checks if the calling app is running in a managed profile.
899 * Requires {@link android.Manifest.permission#MANAGE_USERS} permission.
900 *
901 * @return whether the caller is in a managed profile.
902 * @hide
903 */
904 @SystemApi
905 public boolean isManagedProfile() {
Tony Mak8673b282016-03-21 21:10:59 +0000906 try {
907 return mService.isManagedProfile(UserHandle.myUserId());
908 } catch (RemoteException re) {
909 throw re.rethrowFromSystemServer();
910 }
911 }
912
913 /**
914 * Checks if the specified user is a managed profile.
915 * Requires {@link android.Manifest.permission#MANAGE_USERS} permission, otherwise the caller
916 * must be in the same profile group of specified user.
917 *
918 * @return whether the specified user is a managed profile.
919 * @hide
920 */
921 @SystemApi
922 public boolean isManagedProfile(@UserIdInt int userId) {
923 try {
924 return mService.isManagedProfile(userId);
925 } catch (RemoteException re) {
926 throw re.rethrowFromSystemServer();
927 }
Amith Yamasani0e8d7d62014-09-03 13:17:28 -0700928 }
929
930 /**
Lenka Trochtovac4dd0212015-11-18 12:22:06 +0100931 * Checks if the calling app is running as an ephemeral user.
932 *
933 * @return whether the caller is an ephemeral user.
934 * @hide
935 */
936 public boolean isEphemeralUser() {
937 return isUserEphemeral(UserHandle.myUserId());
938 }
939
940 /**
941 * Returns whether the specified user is ephemeral.
942 * @hide
943 */
Jeff Sharkey8588bc12016-01-06 16:47:42 -0700944 public boolean isUserEphemeral(@UserIdInt int userId) {
Lenka Trochtovac4dd0212015-11-18 12:22:06 +0100945 final UserInfo user = getUserInfo(userId);
946 return user != null && user.isEphemeral();
947 }
948
949 /**
Dianne Hackborna8a9bd62012-10-09 15:36:59 -0700950 * Return whether the given user is actively running. This means that
951 * the user is in the "started" state, not "stopped" -- it is currently
952 * allowed to run code through scheduled alarms, receiving broadcasts,
953 * etc. A started user may be either the current foreground user or a
954 * background user; the result here does not distinguish between the two.
955 * @param user The user to retrieve the running state for.
956 */
957 public boolean isUserRunning(UserHandle user) {
Jeff Sharkey0e62384c2016-01-13 18:52:55 -0700958 return isUserRunning(user.getIdentifier());
959 }
960
961 /** {@hide} */
962 public boolean isUserRunning(int userId) {
Dianne Hackborna8a9bd62012-10-09 15:36:59 -0700963 try {
Fyodor Kupolov7a1e6822016-12-05 13:23:28 -0800964 return mService.isUserRunning(userId);
Jeff Sharkey27b2e692016-02-25 17:40:12 -0700965 } catch (RemoteException re) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700966 throw re.rethrowFromSystemServer();
Dianne Hackborna8a9bd62012-10-09 15:36:59 -0700967 }
968 }
969
970 /**
971 * Return whether the given user is actively running <em>or</em> stopping.
972 * This is like {@link #isUserRunning(UserHandle)}, but will also return
973 * true if the user had been running but is in the process of being stopped
974 * (but is not yet fully stopped, and still running some code).
975 * @param user The user to retrieve the running state for.
976 */
977 public boolean isUserRunningOrStopping(UserHandle user) {
978 try {
Jeff Sharkeye17ac152015-11-06 22:40:29 -0800979 // TODO: reconcile stopped vs stopping?
Dianne Hackborna8a9bd62012-10-09 15:36:59 -0700980 return ActivityManagerNative.getDefault().isUserRunning(
Jeff Sharkeye17ac152015-11-06 22:40:29 -0800981 user.getIdentifier(), ActivityManager.FLAG_OR_STOPPED);
Jeff Sharkey27b2e692016-02-25 17:40:12 -0700982 } catch (RemoteException re) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700983 throw re.rethrowFromSystemServer();
Dianne Hackborna8a9bd62012-10-09 15:36:59 -0700984 }
985 }
986
Jeff Sharkey8a372a02016-03-16 16:25:45 -0600987 /** @removed */
Jeff Sharkeyb6423872015-12-11 11:11:58 -0700988 @Deprecated
Jeff Sharkeye13529a2015-12-09 14:15:27 -0700989 public boolean isUserRunningAndLocked() {
990 return isUserRunningAndLocked(Process.myUserHandle());
991 }
992
Jeff Sharkey8a372a02016-03-16 16:25:45 -0600993 /** @removed */
Jeff Sharkeyb6423872015-12-11 11:11:58 -0700994 @Deprecated
Jeff Sharkey9d8a1042015-12-03 17:56:20 -0700995 public boolean isUserRunningAndLocked(UserHandle user) {
996 try {
997 return ActivityManagerNative.getDefault().isUserRunning(
998 user.getIdentifier(), ActivityManager.FLAG_AND_LOCKED);
Jeff Sharkey27b2e692016-02-25 17:40:12 -0700999 } catch (RemoteException re) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001000 throw re.rethrowFromSystemServer();
Jeff Sharkey9d8a1042015-12-03 17:56:20 -07001001 }
1002 }
1003
Jeff Sharkey8a372a02016-03-16 16:25:45 -06001004 /** @removed */
Jeff Sharkeyb6423872015-12-11 11:11:58 -07001005 @Deprecated
Jeff Sharkeye13529a2015-12-09 14:15:27 -07001006 public boolean isUserRunningAndUnlocked() {
1007 return isUserRunningAndUnlocked(Process.myUserHandle());
1008 }
1009
Jeff Sharkey8a372a02016-03-16 16:25:45 -06001010 /** @removed */
Jeff Sharkeyb6423872015-12-11 11:11:58 -07001011 @Deprecated
Jeff Sharkey9d8a1042015-12-03 17:56:20 -07001012 public boolean isUserRunningAndUnlocked(UserHandle user) {
Jeff Sharkey0825ab22015-12-02 13:04:49 -07001013 try {
1014 return ActivityManagerNative.getDefault().isUserRunning(
1015 user.getIdentifier(), ActivityManager.FLAG_AND_UNLOCKED);
Jeff Sharkey27b2e692016-02-25 17:40:12 -07001016 } catch (RemoteException re) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001017 throw re.rethrowFromSystemServer();
Jeff Sharkey0825ab22015-12-02 13:04:49 -07001018 }
1019 }
1020
1021 /**
Jeff Sharkeycf3f0a12016-03-17 19:57:58 -06001022 * Return whether the calling user is running in an "unlocked" state.
1023 * <p>
1024 * On devices with direct boot, a user is unlocked only after they've
1025 * entered their credentials (such as a lock pattern or PIN). On devices
1026 * without direct boot, a user is unlocked as soon as it starts.
1027 * <p>
1028 * When a user is locked, only device-protected data storage is available.
1029 * When a user is unlocked, both device-protected and credential-protected
1030 * private app data storage is available.
Jeff Sharkey8a372a02016-03-16 16:25:45 -06001031 *
1032 * @see Intent#ACTION_USER_UNLOCKED
1033 * @see Context#createDeviceProtectedStorageContext()
Jeff Sharkeyb6423872015-12-11 11:11:58 -07001034 */
1035 public boolean isUserUnlocked() {
1036 return isUserUnlocked(Process.myUserHandle());
1037 }
1038
1039 /**
Jeff Sharkeycf3f0a12016-03-17 19:57:58 -06001040 * Return whether the given user is running in an "unlocked" state.
1041 * <p>
1042 * On devices with direct boot, a user is unlocked only after they've
1043 * entered their credentials (such as a lock pattern or PIN). On devices
1044 * without direct boot, a user is unlocked as soon as it starts.
1045 * <p>
1046 * When a user is locked, only device-protected data storage is available.
1047 * When a user is unlocked, both device-protected and credential-protected
1048 * private app data storage is available.
Jeff Sharkeyb6423872015-12-11 11:11:58 -07001049 *
1050 * @param user to retrieve the unlocked state for.
Jeff Sharkey8a372a02016-03-16 16:25:45 -06001051 * @see Intent#ACTION_USER_UNLOCKED
1052 * @see Context#createDeviceProtectedStorageContext()
Jeff Sharkeyb6423872015-12-11 11:11:58 -07001053 */
1054 public boolean isUserUnlocked(UserHandle user) {
Jeff Sharkey0999c0d2015-12-17 15:12:22 -07001055 return isUserUnlocked(user.getIdentifier());
1056 }
1057
1058 /** {@hide} */
Jeff Sharkey8588bc12016-01-06 16:47:42 -07001059 public boolean isUserUnlocked(@UserIdInt int userId) {
Jeff Sharkeyce18c812016-04-27 16:00:41 -06001060 try {
Fyodor Kupolov7a1e6822016-12-05 13:23:28 -08001061 return mService.isUserUnlocked(userId);
Jeff Sharkeyce18c812016-04-27 16:00:41 -06001062 } catch (RemoteException re) {
1063 throw re.rethrowFromSystemServer();
1064 }
1065 }
1066
1067 /** {@hide} */
1068 public boolean isUserUnlockingOrUnlocked(UserHandle user) {
1069 return isUserUnlockingOrUnlocked(user.getIdentifier());
1070 }
1071
1072 /** {@hide} */
1073 public boolean isUserUnlockingOrUnlocked(@UserIdInt int userId) {
1074 try {
Fyodor Kupolov7a1e6822016-12-05 13:23:28 -08001075 return mService.isUserUnlockingOrUnlocked(userId);
Jeff Sharkeyce18c812016-04-27 16:00:41 -06001076 } catch (RemoteException re) {
1077 throw re.rethrowFromSystemServer();
1078 }
Jeff Sharkeyb6423872015-12-11 11:11:58 -07001079 }
1080
1081 /**
Amith Yamasani258848d2012-08-10 17:06:33 -07001082 * Returns the UserInfo object describing a specific user.
Tony Mak8673b282016-03-21 21:10:59 +00001083 * Requires {@link android.Manifest.permission#MANAGE_USERS} permission.
Amith Yamasani258848d2012-08-10 17:06:33 -07001084 * @param userHandle the user handle of the user whose information is being requested.
1085 * @return the UserInfo object for a specific user.
1086 * @hide
Dianne Hackbornb26306a2012-10-24 15:22:21 -07001087 */
Jeff Sharkey8588bc12016-01-06 16:47:42 -07001088 public UserInfo getUserInfo(@UserIdInt int userHandle) {
Amith Yamasani258848d2012-08-10 17:06:33 -07001089 try {
1090 return mService.getUserInfo(userHandle);
1091 } catch (RemoteException re) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001092 throw re.rethrowFromSystemServer();
Amith Yamasani258848d2012-08-10 17:06:33 -07001093 }
1094 }
1095
Amith Yamasani71e6c692013-03-24 17:39:28 -07001096 /**
Zoltan Szatmary-Bane7834602016-04-08 18:41:11 +01001097 * @hide
1098 *
1099 * Returns who set a user restriction on a user.
1100 * Requires {@link android.Manifest.permission#MANAGE_USERS} permission.
1101 * @param restrictionKey the string key representing the restriction
1102 * @param userHandle the UserHandle of the user for whom to retrieve the restrictions.
1103 * @return The source of user restriction. Any combination of {@link #RESTRICTION_NOT_SET},
1104 * {@link #RESTRICTION_SOURCE_SYSTEM}, {@link #RESTRICTION_SOURCE_DEVICE_OWNER}
1105 * and {@link #RESTRICTION_SOURCE_PROFILE_OWNER}
1106 */
1107 @SystemApi
1108 @UserRestrictionSource
1109 public int getUserRestrictionSource(String restrictionKey, UserHandle userHandle) {
1110 try {
1111 return mService.getUserRestrictionSource(restrictionKey, userHandle.getIdentifier());
1112 } catch (RemoteException re) {
1113 throw re.rethrowFromSystemServer();
1114 }
1115 }
1116
1117 /**
Amith Yamasani71e6c692013-03-24 17:39:28 -07001118 * Returns the user-wide restrictions imposed on this user.
1119 * @return a Bundle containing all the restrictions.
1120 */
Amith Yamasanie4cf7342012-12-17 11:12:09 -08001121 public Bundle getUserRestrictions() {
1122 return getUserRestrictions(Process.myUserHandle());
1123 }
1124
Amith Yamasani71e6c692013-03-24 17:39:28 -07001125 /**
1126 * Returns the user-wide restrictions imposed on the user specified by <code>userHandle</code>.
1127 * @param userHandle the UserHandle of the user for whom to retrieve the restrictions.
1128 * @return a Bundle containing all the restrictions.
1129 */
Amith Yamasanie4cf7342012-12-17 11:12:09 -08001130 public Bundle getUserRestrictions(UserHandle userHandle) {
1131 try {
1132 return mService.getUserRestrictions(userHandle.getIdentifier());
1133 } catch (RemoteException re) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001134 throw re.rethrowFromSystemServer();
Amith Yamasanie4cf7342012-12-17 11:12:09 -08001135 }
1136 }
1137
Zoltan Szatmary-Ban3bbcedd2015-11-26 13:45:51 +00001138 /**
1139 * @hide
1140 * Returns whether the given user has been disallowed from performing certain actions
1141 * or setting certain settings through UserManager. This method disregards restrictions
1142 * set by device policy.
1143 * @param restrictionKey the string key representing the restriction
1144 * @param userHandle the UserHandle of the user for whom to retrieve the restrictions.
1145 */
1146 public boolean hasBaseUserRestriction(String restrictionKey, UserHandle userHandle) {
1147 try {
1148 return mService.hasBaseUserRestriction(restrictionKey, userHandle.getIdentifier());
1149 } catch (RemoteException re) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001150 throw re.rethrowFromSystemServer();
Zoltan Szatmary-Ban3bbcedd2015-11-26 13:45:51 +00001151 }
1152 }
1153
Amith Yamasani71e6c692013-03-24 17:39:28 -07001154 /**
Makoto Onukia3c12502015-10-28 10:18:32 -07001155 * This will no longer work. Device owners and profile owners should use
1156 * {@link DevicePolicyManager#addUserRestriction(ComponentName, String)} instead.
Amith Yamasani71e6c692013-03-24 17:39:28 -07001157 */
Makoto Onukia3c12502015-10-28 10:18:32 -07001158 // System apps should use UserManager.setUserRestriction() instead.
Julia Reynolds3d9eb782014-08-11 16:40:08 -04001159 @Deprecated
Amith Yamasanie4cf7342012-12-17 11:12:09 -08001160 public void setUserRestrictions(Bundle restrictions) {
Makoto Onuki068c54a2015-10-13 14:34:03 -07001161 throw new UnsupportedOperationException("This method is no longer supported");
Amith Yamasanie4cf7342012-12-17 11:12:09 -08001162 }
1163
Amith Yamasani71e6c692013-03-24 17:39:28 -07001164 /**
Makoto Onukia3c12502015-10-28 10:18:32 -07001165 * This will no longer work. Device owners and profile owners should use
1166 * {@link DevicePolicyManager#addUserRestriction(ComponentName, String)} instead.
Amith Yamasani71e6c692013-03-24 17:39:28 -07001167 */
Makoto Onukia3c12502015-10-28 10:18:32 -07001168 // System apps should use UserManager.setUserRestriction() instead.
Julia Reynolds3d9eb782014-08-11 16:40:08 -04001169 @Deprecated
Amith Yamasanie4cf7342012-12-17 11:12:09 -08001170 public void setUserRestrictions(Bundle restrictions, UserHandle userHandle) {
Makoto Onuki068c54a2015-10-13 14:34:03 -07001171 throw new UnsupportedOperationException("This method is no longer supported");
Amith Yamasanie4cf7342012-12-17 11:12:09 -08001172 }
1173
Amith Yamasani71e6c692013-03-24 17:39:28 -07001174 /**
1175 * Sets the value of a specific restriction.
Amith Yamasanibe465322014-04-24 13:45:17 -07001176 * Requires the MANAGE_USERS permission.
Amith Yamasani71e6c692013-03-24 17:39:28 -07001177 * @param key the key of the restriction
1178 * @param value the value for the restriction
Julia Reynolds3d9eb782014-08-11 16:40:08 -04001179 * @deprecated use {@link android.app.admin.DevicePolicyManager#addUserRestriction(
1180 * android.content.ComponentName, String)} or
1181 * {@link android.app.admin.DevicePolicyManager#clearUserRestriction(
1182 * android.content.ComponentName, String)} instead.
Amith Yamasani71e6c692013-03-24 17:39:28 -07001183 */
Julia Reynolds3d9eb782014-08-11 16:40:08 -04001184 @Deprecated
Amith Yamasani71e6c692013-03-24 17:39:28 -07001185 public void setUserRestriction(String key, boolean value) {
Makoto Onuki068c54a2015-10-13 14:34:03 -07001186 setUserRestriction(key, value, Process.myUserHandle());
Amith Yamasani71e6c692013-03-24 17:39:28 -07001187 }
1188
1189 /**
1190 * @hide
1191 * Sets the value of a specific restriction on a specific user.
Amith Yamasanibe465322014-04-24 13:45:17 -07001192 * Requires the MANAGE_USERS permission.
Amith Yamasani71e6c692013-03-24 17:39:28 -07001193 * @param key the key of the restriction
1194 * @param value the value for the restriction
1195 * @param userHandle the user whose restriction is to be changed.
Julia Reynolds3d9eb782014-08-11 16:40:08 -04001196 * @deprecated use {@link android.app.admin.DevicePolicyManager#addUserRestriction(
1197 * android.content.ComponentName, String)} or
1198 * {@link android.app.admin.DevicePolicyManager#clearUserRestriction(
1199 * android.content.ComponentName, String)} instead.
Amith Yamasani71e6c692013-03-24 17:39:28 -07001200 */
Julia Reynolds3d9eb782014-08-11 16:40:08 -04001201 @Deprecated
Maggie Benthall67944582013-02-22 14:58:27 -05001202 public void setUserRestriction(String key, boolean value, UserHandle userHandle) {
Fyodor Kupolovb5013302015-04-17 17:59:14 -07001203 try {
1204 mService.setUserRestriction(key, value, userHandle.getIdentifier());
1205 } catch (RemoteException re) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001206 throw re.rethrowFromSystemServer();
Fyodor Kupolovb5013302015-04-17 17:59:14 -07001207 }
Maggie Benthall67944582013-02-22 14:58:27 -05001208 }
1209
Amith Yamasani258848d2012-08-10 17:06:33 -07001210 /**
Maggie Benthalla12fccf2013-03-14 18:02:12 -04001211 * Returns whether the current user has been disallowed from performing certain actions
1212 * or setting certain settings.
Julia Reynolds2b2cf722014-06-06 11:41:04 -04001213 *
1214 * @param restrictionKey The string key representing the restriction.
1215 * @return {@code true} if the current user has the given restriction, {@code false} otherwise.
Maggie Benthalla12fccf2013-03-14 18:02:12 -04001216 */
1217 public boolean hasUserRestriction(String restrictionKey) {
David Christieb12ba932013-09-03 17:15:28 -07001218 return hasUserRestriction(restrictionKey, Process.myUserHandle());
1219 }
1220
1221 /**
1222 * @hide
1223 * Returns whether the given user has been disallowed from performing certain actions
1224 * or setting certain settings.
1225 * @param restrictionKey the string key representing the restriction
1226 * @param userHandle the UserHandle of the user for whom to retrieve the restrictions.
1227 */
1228 public boolean hasUserRestriction(String restrictionKey, UserHandle userHandle) {
Amith Yamasani8cd28b52014-06-08 17:54:27 -07001229 try {
1230 return mService.hasUserRestriction(restrictionKey,
1231 userHandle.getIdentifier());
1232 } catch (RemoteException re) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001233 throw re.rethrowFromSystemServer();
Amith Yamasani8cd28b52014-06-08 17:54:27 -07001234 }
Maggie Benthalla12fccf2013-03-14 18:02:12 -04001235 }
1236
1237 /**
Dianne Hackborn33f9cb82012-10-04 17:15:10 -07001238 * Return the serial number for a user. This is a device-unique
Dianne Hackbornb26306a2012-10-24 15:22:21 -07001239 * number assigned to that user; if the user is deleted and then a new
1240 * user created, the new users will not be given the same serial number.
Dianne Hackborn33f9cb82012-10-04 17:15:10 -07001241 * @param user The user whose serial number is to be retrieved.
Dianne Hackbornb26306a2012-10-24 15:22:21 -07001242 * @return The serial number of the given user; returns -1 if the
1243 * given UserHandle does not exist.
Dianne Hackborn33f9cb82012-10-04 17:15:10 -07001244 * @see #getUserForSerialNumber(long)
1245 */
1246 public long getSerialNumberForUser(UserHandle user) {
1247 return getUserSerialNumber(user.getIdentifier());
1248 }
1249
1250 /**
1251 * Return the user associated with a serial number previously
1252 * returned by {@link #getSerialNumberForUser(UserHandle)}.
1253 * @param serialNumber The serial number of the user that is being
1254 * retrieved.
1255 * @return Return the user associated with the serial number, or null
1256 * if there is not one.
1257 * @see #getSerialNumberForUser(UserHandle)
1258 */
1259 public UserHandle getUserForSerialNumber(long serialNumber) {
Fyodor Kupolovef249092015-05-06 13:18:46 -07001260 int ident = getUserHandle((int) serialNumber);
Dianne Hackborn33f9cb82012-10-04 17:15:10 -07001261 return ident >= 0 ? new UserHandle(ident) : null;
1262 }
1263
1264 /**
Xiaohui Chencfe64c82015-07-16 14:30:50 -07001265 * Creates a user with the specified name and options. For non-admin users, default user
1266 * restrictions are going to be applied.
Amith Yamasani195263742012-08-21 15:40:12 -07001267 * Requires {@link android.Manifest.permission#MANAGE_USERS} permission.
Amith Yamasani258848d2012-08-10 17:06:33 -07001268 *
1269 * @param name the user's name
1270 * @param flags flags that identify the type of user and other properties.
1271 * @see UserInfo
1272 *
1273 * @return the UserInfo object for the created user, or null if the user could not be created.
1274 * @hide
1275 */
1276 public UserInfo createUser(String name, int flags) {
Xiaohui Chencfe64c82015-07-16 14:30:50 -07001277 UserInfo user = null;
Amith Yamasani258848d2012-08-10 17:06:33 -07001278 try {
Xiaohui Chencfe64c82015-07-16 14:30:50 -07001279 user = mService.createUser(name, flags);
phweisse9c44062016-02-10 12:57:38 +01001280 // TODO: Keep this in sync with
1281 // UserManagerService.LocalService.createUserEvenWhenDisallowed
Xiaohui Chencfe64c82015-07-16 14:30:50 -07001282 if (user != null && !user.isAdmin()) {
Makoto Onuki068c54a2015-10-13 14:34:03 -07001283 mService.setUserRestriction(DISALLOW_SMS, true, user.id);
1284 mService.setUserRestriction(DISALLOW_OUTGOING_CALLS, true, user.id);
Xiaohui Chencfe64c82015-07-16 14:30:50 -07001285 }
Amith Yamasani258848d2012-08-10 17:06:33 -07001286 } catch (RemoteException re) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001287 throw re.rethrowFromSystemServer();
Amith Yamasani258848d2012-08-10 17:06:33 -07001288 }
Xiaohui Chencfe64c82015-07-16 14:30:50 -07001289 return user;
Amith Yamasani258848d2012-08-10 17:06:33 -07001290 }
1291
1292 /**
Amith Yamasani1e9c2182014-06-11 17:25:51 -07001293 * Creates a guest user and configures it.
1294 * @param context an application context
1295 * @param name the name to set for the user
1296 * @hide
1297 */
1298 public UserInfo createGuest(Context context, String name) {
Makoto Onuki068c54a2015-10-13 14:34:03 -07001299 UserInfo guest = null;
1300 try {
1301 guest = mService.createUser(name, UserInfo.FLAG_GUEST);
1302 if (guest != null) {
1303 Settings.Secure.putStringForUser(context.getContentResolver(),
1304 Settings.Secure.SKIP_FIRST_USE_HINTS, "1", guest.id);
Amith Yamasanibf3a9462014-07-28 14:26:42 -07001305 }
Makoto Onuki068c54a2015-10-13 14:34:03 -07001306 } catch (RemoteException re) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001307 throw re.rethrowFromSystemServer();
Amith Yamasani1e9c2182014-06-11 17:25:51 -07001308 }
1309 return guest;
1310 }
1311
Amith Yamasaniaa6634e2014-10-06 14:20:28 -07001312 /**
Kenny Guy2a764942014-04-02 13:29:20 +01001313 * Creates a user with the specified name and options as a profile of another user.
Kenny Guya52dc3e2014-02-11 15:33:14 +00001314 * Requires {@link android.Manifest.permission#MANAGE_USERS} permission.
1315 *
1316 * @param name the user's name
1317 * @param flags flags that identify the type of user and other properties.
1318 * @see UserInfo
Kenny Guy2a764942014-04-02 13:29:20 +01001319 * @param userHandle new user will be a profile of this use.
Kenny Guya52dc3e2014-02-11 15:33:14 +00001320 *
1321 * @return the UserInfo object for the created user, or null if the user could not be created.
1322 * @hide
1323 */
Jeff Sharkey8588bc12016-01-06 16:47:42 -07001324 public UserInfo createProfileForUser(String name, int flags, @UserIdInt int userHandle) {
Kenny Guya52dc3e2014-02-11 15:33:14 +00001325 try {
Kenny Guy2a764942014-04-02 13:29:20 +01001326 return mService.createProfileForUser(name, flags, userHandle);
Kenny Guya52dc3e2014-02-11 15:33:14 +00001327 } catch (RemoteException re) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001328 throw re.rethrowFromSystemServer();
Kenny Guya52dc3e2014-02-11 15:33:14 +00001329 }
1330 }
1331
1332 /**
Fyodor Kupolov02cb6e72015-09-18 18:20:55 -07001333 * Creates a restricted profile with the specified name. This method also sets necessary
1334 * restrictions and adds shared accounts.
Fyodor Kupolov06a484a2015-08-21 16:33:20 -07001335 *
1336 * @param name profile's name
1337 * @return UserInfo object for the created user, or null if the user could not be created.
1338 * @hide
1339 */
1340 public UserInfo createRestrictedProfile(String name) {
1341 try {
Fyodor Kupolov02cb6e72015-09-18 18:20:55 -07001342 UserHandle parentUserHandle = Process.myUserHandle();
1343 UserInfo user = mService.createRestrictedProfile(name,
1344 parentUserHandle.getIdentifier());
1345 if (user != null) {
1346 AccountManager.get(mContext).addSharedAccountsFromParentUser(parentUserHandle,
1347 UserHandle.of(user.id));
Fyodor Kupolov06a484a2015-08-21 16:33:20 -07001348 }
Fyodor Kupolov02cb6e72015-09-18 18:20:55 -07001349 return user;
Jeff Sharkey27b2e692016-02-25 17:40:12 -07001350 } catch (RemoteException re) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001351 throw re.rethrowFromSystemServer();
Fyodor Kupolov06a484a2015-08-21 16:33:20 -07001352 }
Fyodor Kupolov06a484a2015-08-21 16:33:20 -07001353 }
1354
1355 /**
Amith Yamasani06964342016-04-15 13:55:01 -07001356 * Returns an intent to create a user for the provided name and account name. The name
1357 * and account name will be used when the setup process for the new user is started.
1358 * <p>
Amith Yamasani12747872015-12-07 14:19:49 -08001359 * The intent should be launched using startActivityForResult and the return result will
Amith Yamasani37ed8d12016-01-27 14:40:16 -08001360 * indicate if the user consented to adding a new user and if the operation succeeded. Any
1361 * errors in creating the user will be returned in the result code. If the user cancels the
1362 * request, the return result will be {@link Activity#RESULT_CANCELED}. On success, the
1363 * result code will be {@link Activity#RESULT_OK}.
Amith Yamasani06964342016-04-15 13:55:01 -07001364 * <p>
1365 * Use {@link #supportsMultipleUsers()} to first check if the device supports this operation
1366 * at all.
1367 * <p>
Amith Yamasani12747872015-12-07 14:19:49 -08001368 * The new user is created but not initialized. After switching into the user for the first
1369 * time, the preferred user name and account information are used by the setup process for that
1370 * user.
1371 *
1372 * @param userName Optional name to assign to the user.
Amith Yamasani06964342016-04-15 13:55:01 -07001373 * @param accountName Optional account name that will be used by the setup wizard to initialize
Amith Yamasani12747872015-12-07 14:19:49 -08001374 * the user.
1375 * @param accountType Optional account type for the account to be created. This is required
1376 * if the account name is specified.
1377 * @param accountOptions Optional bundle of data to be passed in during account creation in the
1378 * new user via {@link AccountManager#addAccount(String, String, String[],
1379 * Bundle, android.app.Activity, android.accounts.AccountManagerCallback,
1380 * Handler)}.
Amith Yamasani06964342016-04-15 13:55:01 -07001381 * @return An Intent that can be launched from an Activity.
Amith Yamasani37ed8d12016-01-27 14:40:16 -08001382 * @see #USER_CREATION_FAILED_NOT_PERMITTED
1383 * @see #USER_CREATION_FAILED_NO_MORE_USERS
Amith Yamasani06964342016-04-15 13:55:01 -07001384 * @see #supportsMultipleUsers
Amith Yamasani12747872015-12-07 14:19:49 -08001385 */
1386 public static Intent createUserCreationIntent(@Nullable String userName,
1387 @Nullable String accountName,
1388 @Nullable String accountType, @Nullable PersistableBundle accountOptions) {
Amith Yamasani12747872015-12-07 14:19:49 -08001389 Intent intent = new Intent(ACTION_CREATE_USER);
1390 if (userName != null) {
1391 intent.putExtra(EXTRA_USER_NAME, userName);
1392 }
1393 if (accountName != null && accountType == null) {
1394 throw new IllegalArgumentException("accountType must be specified if accountName is "
1395 + "specified");
1396 }
1397 if (accountName != null) {
1398 intent.putExtra(EXTRA_USER_ACCOUNT_NAME, accountName);
1399 }
1400 if (accountType != null) {
1401 intent.putExtra(EXTRA_USER_ACCOUNT_TYPE, accountType);
1402 }
1403 if (accountOptions != null) {
1404 intent.putExtra(EXTRA_USER_ACCOUNT_OPTIONS, accountOptions);
1405 }
1406 return intent;
1407 }
1408
1409 /**
1410 * @hide
1411 *
1412 * Returns the preferred account name for user creation. Requires MANAGE_USERS permission.
1413 */
1414 @SystemApi
1415 public String getSeedAccountName() {
1416 try {
1417 return mService.getSeedAccountName();
1418 } catch (RemoteException re) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001419 throw re.rethrowFromSystemServer();
Amith Yamasani12747872015-12-07 14:19:49 -08001420 }
1421 }
1422
1423 /**
1424 * @hide
1425 *
1426 * Returns the preferred account type for user creation. Requires MANAGE_USERS permission.
1427 */
1428 @SystemApi
1429 public String getSeedAccountType() {
1430 try {
1431 return mService.getSeedAccountType();
1432 } catch (RemoteException re) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001433 throw re.rethrowFromSystemServer();
Amith Yamasani12747872015-12-07 14:19:49 -08001434 }
1435 }
1436
1437 /**
1438 * @hide
1439 *
1440 * Returns the preferred account's options bundle for user creation. Requires MANAGE_USERS
1441 * permission.
1442 * @return Any options set by the requestor that created the user.
1443 */
1444 @SystemApi
1445 public PersistableBundle getSeedAccountOptions() {
1446 try {
1447 return mService.getSeedAccountOptions();
1448 } catch (RemoteException re) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001449 throw re.rethrowFromSystemServer();
Amith Yamasani12747872015-12-07 14:19:49 -08001450 }
1451 }
1452
1453 /**
1454 * @hide
1455 *
1456 * Called by a system activity to set the seed account information of a user created
1457 * through the user creation intent.
1458 * @param userId
1459 * @param accountName
1460 * @param accountType
1461 * @param accountOptions
1462 * @see #createUserCreationIntent(String, String, String, PersistableBundle)
1463 */
1464 public void setSeedAccountData(int userId, String accountName, String accountType,
1465 PersistableBundle accountOptions) {
1466 try {
1467 mService.setSeedAccountData(userId, accountName, accountType, accountOptions,
1468 /* persist= */ true);
1469 } catch (RemoteException re) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001470 throw re.rethrowFromSystemServer();
Amith Yamasani12747872015-12-07 14:19:49 -08001471 }
1472 }
1473
1474 /**
1475 * @hide
1476 * Clears the seed information used to create this user. Requires MANAGE_USERS permission.
1477 */
1478 @SystemApi
1479 public void clearSeedAccountData() {
1480 try {
1481 mService.clearSeedAccountData();
1482 } catch (RemoteException re) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001483 throw re.rethrowFromSystemServer();
Amith Yamasani12747872015-12-07 14:19:49 -08001484 }
1485 }
1486
1487 /**
Amith Yamasani1df14732014-08-29 21:37:27 -07001488 * @hide
1489 * Marks the guest user for deletion to allow a new guest to be created before deleting
1490 * the current user who is a guest.
1491 * @param userHandle
1492 * @return
1493 */
Jeff Sharkey8588bc12016-01-06 16:47:42 -07001494 public boolean markGuestForDeletion(@UserIdInt int userHandle) {
Amith Yamasani1df14732014-08-29 21:37:27 -07001495 try {
1496 return mService.markGuestForDeletion(userHandle);
1497 } catch (RemoteException re) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001498 throw re.rethrowFromSystemServer();
Amith Yamasani1df14732014-08-29 21:37:27 -07001499 }
1500 }
1501
1502 /**
Alexandra Gherghinadf35d572014-04-09 13:54:39 +01001503 * Sets the user as enabled, if such an user exists.
Lenka Trochtova1ddda472016-02-12 10:42:12 +01001504 *
1505 * <p>Requires {@link android.Manifest.permission#MANAGE_USERS} permission.
1506 *
1507 * <p>Note that the default is true, it's only that managed profiles might not be enabled.
1508 * Also ephemeral users can be disabled to indicate that their removal is in progress and they
1509 * shouldn't be re-entered. Therefore ephemeral users should not be re-enabled once disabled.
Alexandra Gherghinadf35d572014-04-09 13:54:39 +01001510 *
1511 * @param userHandle the id of the profile to enable
1512 * @hide
1513 */
Jeff Sharkey8588bc12016-01-06 16:47:42 -07001514 public void setUserEnabled(@UserIdInt int userHandle) {
Alexandra Gherghinadf35d572014-04-09 13:54:39 +01001515 try {
1516 mService.setUserEnabled(userHandle);
Jeff Sharkey27b2e692016-02-25 17:40:12 -07001517 } catch (RemoteException re) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001518 throw re.rethrowFromSystemServer();
Alexandra Gherghinadf35d572014-04-09 13:54:39 +01001519 }
1520 }
1521
1522 /**
Dianne Hackbornb26306a2012-10-24 15:22:21 -07001523 * Return the number of users currently created on the device.
1524 */
1525 public int getUserCount() {
1526 List<UserInfo> users = getUsers();
1527 return users != null ? users.size() : 1;
1528 }
1529
1530 /**
Amith Yamasanid04aaa32016-06-13 12:09:36 -07001531 * Returns information for all users on this device, including ones marked for deletion.
1532 * To retrieve only users that are alive, use {@link #getUsers(boolean)}.
1533 * <p>
Amith Yamasani195263742012-08-21 15:40:12 -07001534 * Requires {@link android.Manifest.permission#MANAGE_USERS} permission.
Fyodor Kupolov940e8572016-01-26 12:03:51 -08001535 * @return the list of users that exist on the device.
Amith Yamasani258848d2012-08-10 17:06:33 -07001536 * @hide
1537 */
1538 public List<UserInfo> getUsers() {
1539 try {
Amith Yamasani920ace02012-09-20 22:15:37 -07001540 return mService.getUsers(false);
1541 } catch (RemoteException re) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001542 throw re.rethrowFromSystemServer();
Amith Yamasani920ace02012-09-20 22:15:37 -07001543 }
1544 }
1545
1546 /**
Fyodor Kupolov940e8572016-01-26 12:03:51 -08001547 * Returns serial numbers of all users on this device.
1548 * Requires {@link android.Manifest.permission#MANAGE_USERS} permission.
1549 *
1550 * @param excludeDying specify if the list should exclude users being removed.
1551 * @return the list of serial numbers of users that exist on the device.
1552 * @hide
1553 */
1554 @SystemApi
1555 public long[] getSerialNumbersOfUsers(boolean excludeDying) {
1556 try {
1557 List<UserInfo> users = mService.getUsers(excludeDying);
1558 long[] result = new long[users.size()];
1559 for (int i = 0; i < result.length; i++) {
1560 result[i] = users.get(i).serialNumber;
1561 }
1562 return result;
1563 } catch (RemoteException re) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001564 throw re.rethrowFromSystemServer();
Fyodor Kupolov940e8572016-01-26 12:03:51 -08001565 }
1566 }
1567
1568 /**
Xiaohui Chenb3b92582015-12-07 11:22:13 -08001569 * @return the user's account name, null if not found.
1570 * @hide
1571 */
1572 @RequiresPermission( allOf = {
1573 Manifest.permission.INTERACT_ACROSS_USERS_FULL,
1574 Manifest.permission.MANAGE_USERS
1575 })
Jeff Sharkey8588bc12016-01-06 16:47:42 -07001576 public @Nullable String getUserAccount(@UserIdInt int userHandle) {
Xiaohui Chenb3b92582015-12-07 11:22:13 -08001577 try {
1578 return mService.getUserAccount(userHandle);
1579 } catch (RemoteException re) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001580 throw re.rethrowFromSystemServer();
Xiaohui Chenb3b92582015-12-07 11:22:13 -08001581 }
1582 }
1583
1584 /**
1585 * Set account name for the given user.
1586 * @hide
1587 */
1588 @RequiresPermission( allOf = {
1589 Manifest.permission.INTERACT_ACROSS_USERS_FULL,
1590 Manifest.permission.MANAGE_USERS
1591 })
Jeff Sharkey8588bc12016-01-06 16:47:42 -07001592 public void setUserAccount(@UserIdInt int userHandle, @Nullable String accountName) {
Xiaohui Chenb3b92582015-12-07 11:22:13 -08001593 try {
1594 mService.setUserAccount(userHandle, accountName);
1595 } catch (RemoteException re) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001596 throw re.rethrowFromSystemServer();
Xiaohui Chenb3b92582015-12-07 11:22:13 -08001597 }
1598 }
1599
1600 /**
Xiaohui Chen70f6c382015-04-28 14:21:43 -07001601 * Returns information for Primary user.
1602 * Requires {@link android.Manifest.permission#MANAGE_USERS} permission.
1603 *
1604 * @return the Primary user, null if not found.
1605 * @hide
1606 */
Xiaohui Chen7cb69df2015-07-13 16:01:01 -07001607 public @Nullable UserInfo getPrimaryUser() {
Xiaohui Chen70f6c382015-04-28 14:21:43 -07001608 try {
1609 return mService.getPrimaryUser();
1610 } catch (RemoteException re) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001611 throw re.rethrowFromSystemServer();
Xiaohui Chen70f6c382015-04-28 14:21:43 -07001612 }
1613 }
1614
1615 /**
Amith Yamasani95ab7842014-08-11 17:09:26 -07001616 * Checks whether it's possible to add more users. Caller must hold the MANAGE_USERS
1617 * permission.
1618 *
1619 * @return true if more users can be added, false if limit has been reached.
1620 * @hide
1621 */
1622 public boolean canAddMoreUsers() {
1623 final List<UserInfo> users = getUsers(true);
1624 final int totalUserCount = users.size();
1625 int aliveUserCount = 0;
1626 for (int i = 0; i < totalUserCount; i++) {
1627 UserInfo user = users.get(i);
1628 if (!user.isGuest()) {
1629 aliveUserCount++;
1630 }
1631 }
1632 return aliveUserCount < getMaxSupportedUsers();
1633 }
1634
1635 /**
Nicolas Prevot72434b72015-05-13 12:15:03 -07001636 * Checks whether it's possible to add more managed profiles. Caller must hold the MANAGE_USERS
1637 * permission.
Nicolas Prevot07387fe2015-10-30 17:53:30 +00001638 * if allowedToRemoveOne is true and if the user already has a managed profile, then return if
1639 * we could add a new managed profile to this user after removing the existing one.
Nicolas Prevot72434b72015-05-13 12:15:03 -07001640 *
1641 * @return true if more managed profiles can be added, false if limit has been reached.
1642 * @hide
1643 */
Jeff Sharkey8588bc12016-01-06 16:47:42 -07001644 public boolean canAddMoreManagedProfiles(@UserIdInt int userId, boolean allowedToRemoveOne) {
Nicolas Prevot72434b72015-05-13 12:15:03 -07001645 try {
Nicolas Prevot07387fe2015-10-30 17:53:30 +00001646 return mService.canAddMoreManagedProfiles(userId, allowedToRemoveOne);
Nicolas Prevot72434b72015-05-13 12:15:03 -07001647 } catch (RemoteException re) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001648 throw re.rethrowFromSystemServer();
Nicolas Prevot72434b72015-05-13 12:15:03 -07001649 }
1650 }
1651
1652 /**
Kenny Guy2a764942014-04-02 13:29:20 +01001653 * Returns list of the profiles of userHandle including
1654 * userHandle itself.
Amith Yamasani4f7e2e32014-08-14 18:49:48 -07001655 * Note that this returns both enabled and not enabled profiles. See
Ruben Brunk7f75da22015-04-30 17:46:30 -07001656 * {@link #getEnabledProfiles(int)} if you need only the enabled ones.
Amith Yamasani4f582632014-02-19 14:31:52 -08001657 *
Kenny Guy2a764942014-04-02 13:29:20 +01001658 * Requires {@link android.Manifest.permission#MANAGE_USERS} permission.
1659 * @param userHandle profiles of this user will be returned.
1660 * @return the list of profiles.
1661 * @hide
1662 */
Jeff Sharkey8588bc12016-01-06 16:47:42 -07001663 public List<UserInfo> getProfiles(@UserIdInt int userHandle) {
Kenny Guya52dc3e2014-02-11 15:33:14 +00001664 try {
Alexandra Gherghina385124d2014-04-03 13:37:39 +01001665 return mService.getProfiles(userHandle, false /* enabledOnly */);
Kenny Guya52dc3e2014-02-11 15:33:14 +00001666 } catch (RemoteException re) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001667 throw re.rethrowFromSystemServer();
Kenny Guya52dc3e2014-02-11 15:33:14 +00001668 }
1669 }
1670
1671 /**
Xiaohui Chenfd5b7742015-10-14 15:47:04 -07001672 * Requires {@link android.Manifest.permission#MANAGE_USERS} permission.
1673 * @param userId one of the two user ids to check.
1674 * @param otherUserId one of the two user ids to check.
1675 * @return true if the two user ids are in the same profile group.
1676 * @hide
1677 */
Jeff Sharkey8588bc12016-01-06 16:47:42 -07001678 public boolean isSameProfileGroup(@UserIdInt int userId, int otherUserId) {
Xiaohui Chenfd5b7742015-10-14 15:47:04 -07001679 try {
1680 return mService.isSameProfileGroup(userId, otherUserId);
1681 } catch (RemoteException re) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001682 throw re.rethrowFromSystemServer();
Xiaohui Chenfd5b7742015-10-14 15:47:04 -07001683 }
1684 }
1685
1686 /**
Ruben Brunk7f75da22015-04-30 17:46:30 -07001687 * Returns list of the profiles of userHandle including
1688 * userHandle itself.
1689 * Note that this returns only enabled.
1690 *
1691 * Requires {@link android.Manifest.permission#MANAGE_USERS} permission.
1692 * @param userHandle profiles of this user will be returned.
1693 * @return the list of profiles.
1694 * @hide
1695 */
Jeff Sharkey8588bc12016-01-06 16:47:42 -07001696 public List<UserInfo> getEnabledProfiles(@UserIdInt int userHandle) {
Ruben Brunk7f75da22015-04-30 17:46:30 -07001697 try {
1698 return mService.getProfiles(userHandle, true /* enabledOnly */);
1699 } catch (RemoteException re) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001700 throw re.rethrowFromSystemServer();
Ruben Brunk7f75da22015-04-30 17:46:30 -07001701 }
1702 }
1703
1704 /**
Jessica Hummelbe81c802014-04-22 15:49:22 +01001705 * Returns a list of UserHandles for profiles associated with the user that the calling process
1706 * is running on, including the user itself.
Amith Yamasani4f582632014-02-19 14:31:52 -08001707 *
1708 * @return A non-empty list of UserHandles associated with the calling user.
1709 */
1710 public List<UserHandle> getUserProfiles() {
Fyodor Kupolov7f98aa42016-04-07 14:56:25 -07001711 int[] userIds = getProfileIds(UserHandle.myUserId(), true /* enabledOnly */);
1712 List<UserHandle> result = new ArrayList<>(userIds.length);
1713 for (int userId : userIds) {
1714 result.add(UserHandle.of(userId));
1715 }
1716 return result;
1717 }
1718
1719 /**
1720 * Returns a list of ids for profiles associated with the specified user including the user
1721 * itself.
1722 *
1723 * @param userId id of the user to return profiles for
1724 * @param enabledOnly whether return only {@link UserInfo#isEnabled() enabled} profiles
1725 * @return A non-empty list of ids of profiles associated with the specified user.
1726 *
1727 * @hide
1728 */
1729 public int[] getProfileIds(@UserIdInt int userId, boolean enabledOnly) {
Alexandra Gherghina385124d2014-04-03 13:37:39 +01001730 try {
Fyodor Kupolov7f98aa42016-04-07 14:56:25 -07001731 return mService.getProfileIds(userId, enabledOnly);
Alexandra Gherghina385124d2014-04-03 13:37:39 +01001732 } catch (RemoteException re) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001733 throw re.rethrowFromSystemServer();
Alexandra Gherghina385124d2014-04-03 13:37:39 +01001734 }
Fyodor Kupolov7f98aa42016-04-07 14:56:25 -07001735 }
1736
1737 /**
1738 * @see #getProfileIds(int, boolean)
1739 * @hide
1740 */
1741 public int[] getProfileIdsWithDisabled(@UserIdInt int userId) {
1742 return getProfileIds(userId, false /* enabledOnly */);
1743 }
1744
1745 /**
1746 * @see #getProfileIds(int, boolean)
1747 * @hide
1748 */
1749 public int[] getEnabledProfileIds(@UserIdInt int userId) {
1750 return getProfileIds(userId, true /* enabledOnly */);
Amith Yamasani4f582632014-02-19 14:31:52 -08001751 }
1752
Amith Yamasani7dda2652014-04-11 14:57:12 -07001753 /**
Andres Moralesc5548c02015-08-05 10:23:12 -07001754 * Returns the device credential owner id of the profile from
1755 * which this method is called, or userHandle if called from a user that
1756 * is not a profile.
1757 *
1758 * @hide
1759 */
Jeff Sharkey8588bc12016-01-06 16:47:42 -07001760 public int getCredentialOwnerProfile(@UserIdInt int userHandle) {
Andres Moralesc5548c02015-08-05 10:23:12 -07001761 try {
1762 return mService.getCredentialOwnerProfile(userHandle);
1763 } catch (RemoteException re) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001764 throw re.rethrowFromSystemServer();
Andres Moralesc5548c02015-08-05 10:23:12 -07001765 }
1766 }
1767
1768 /**
Jessica Hummelbe81c802014-04-22 15:49:22 +01001769 * Returns the parent of the profile which this method is called from
1770 * or null if called from a user that is not a profile.
1771 *
1772 * @hide
1773 */
Jeff Sharkey8588bc12016-01-06 16:47:42 -07001774 public UserInfo getProfileParent(@UserIdInt int userHandle) {
Jessica Hummelbe81c802014-04-22 15:49:22 +01001775 try {
1776 return mService.getProfileParent(userHandle);
1777 } catch (RemoteException re) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001778 throw re.rethrowFromSystemServer();
Jessica Hummelbe81c802014-04-22 15:49:22 +01001779 }
1780 }
1781
1782 /**
Rubin Xu0a29ecd2015-11-04 15:11:48 +00001783 * Set quiet mode of a managed profile.
1784 *
1785 * @param userHandle The user handle of the profile.
1786 * @param enableQuietMode Whether quiet mode should be enabled or disabled.
1787 * @hide
1788 */
Jeff Sharkey8588bc12016-01-06 16:47:42 -07001789 public void setQuietModeEnabled(@UserIdInt int userHandle, boolean enableQuietMode) {
Rubin Xu0a29ecd2015-11-04 15:11:48 +00001790 try {
1791 mService.setQuietModeEnabled(userHandle, enableQuietMode);
Jeff Sharkey27b2e692016-02-25 17:40:12 -07001792 } catch (RemoteException re) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001793 throw re.rethrowFromSystemServer();
Rubin Xu0a29ecd2015-11-04 15:11:48 +00001794 }
1795 }
1796
1797 /**
1798 * Returns whether the given profile is in quiet mode or not.
Ricky Wai7881cf82016-04-15 17:20:12 +01001799 * Notes: Quiet mode is only supported for managed profiles.
Rubin Xu0a29ecd2015-11-04 15:11:48 +00001800 *
1801 * @param userHandle The user handle of the profile to be queried.
1802 * @return true if the profile is in quiet mode, false otherwise.
1803 */
1804 public boolean isQuietModeEnabled(UserHandle userHandle) {
1805 try {
1806 return mService.isQuietModeEnabled(userHandle.getIdentifier());
Jeff Sharkey27b2e692016-02-25 17:40:12 -07001807 } catch (RemoteException re) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001808 throw re.rethrowFromSystemServer();
Rubin Xu0a29ecd2015-11-04 15:11:48 +00001809 }
Rubin Xu0a29ecd2015-11-04 15:11:48 +00001810 }
1811
1812 /**
Benjamin Franzf02420c2016-04-04 18:52:21 +01001813 * Tries disabling quiet mode for a given user. If the user is still locked, we unlock the user
1814 * first by showing the confirm credentials screen and disable quiet mode upon successful
1815 * unlocking. If the user is already unlocked, we call through to {@link #setQuietModeEnabled}
1816 * directly.
1817 *
1818 * @return true if the quiet mode was disabled immediately
1819 * @hide
1820 */
1821 public boolean trySetQuietModeDisabled(@UserIdInt int userHandle, IntentSender target) {
1822 try {
1823 return mService.trySetQuietModeDisabled(userHandle, target);
1824 } catch (RemoteException re) {
1825 throw re.rethrowFromSystemServer();
1826 }
1827 }
1828
1829 /**
Amith Yamasani7dda2652014-04-11 14:57:12 -07001830 * If the target user is a managed profile of the calling user or the caller
1831 * is itself a managed profile, then this returns a badged copy of the given
Svetoslavc71c42f2014-08-05 18:57:05 -07001832 * icon to be able to distinguish it from the original icon. For badging an
1833 * arbitrary drawable use {@link #getBadgedDrawableForUser(
1834 * android.graphics.drawable.Drawable, UserHandle, android.graphics.Rect, int)}.
1835 * <p>
1836 * If the original drawable is a BitmapDrawable and the backing bitmap is
Vadim Tryshev66ae66a2016-02-18 15:41:21 -08001837 * mutable as per {@link android.graphics.Bitmap#isMutable()}, the badging
Svetoslavc71c42f2014-08-05 18:57:05 -07001838 * is performed in place and the original drawable is returned.
1839 * </p>
Amith Yamasani7dda2652014-04-11 14:57:12 -07001840 *
1841 * @param icon The icon to badge.
1842 * @param user The target user.
1843 * @return A drawable that combines the original icon and a badge as
1844 * determined by the system.
Svetoslavc7d62f02014-09-04 15:39:54 -07001845 * @removed
Amith Yamasani7dda2652014-04-11 14:57:12 -07001846 */
Svetoslavc71c42f2014-08-05 18:57:05 -07001847 public Drawable getBadgedIconForUser(Drawable icon, UserHandle user) {
Svetoslavc7d62f02014-09-04 15:39:54 -07001848 return mContext.getPackageManager().getUserBadgedIcon(icon, user);
Amith Yamasani4f582632014-02-19 14:31:52 -08001849 }
1850
Kenny Guy701ea7c2014-05-08 23:34:12 +01001851 /**
1852 * If the target user is a managed profile of the calling user or the caller
Svetoslavc71c42f2014-08-05 18:57:05 -07001853 * is itself a managed profile, then this returns a badged copy of the given
1854 * drawable allowing the user to distinguish it from the original drawable.
1855 * The caller can specify the location in the bounds of the drawable to be
1856 * badged where the badge should be applied as well as the density of the
1857 * badge to be used.
1858 * <p>
1859 * If the original drawable is a BitmapDrawable and the backing bitmap is
Vadim Tryshev66ae66a2016-02-18 15:41:21 -08001860 * mutable as per {@link android.graphics.Bitmap#isMutable()}, the badging
Svetoslavc71c42f2014-08-05 18:57:05 -07001861 * is performed in place and the original drawable is returned.
1862 * </p>
1863 *
1864 * @param badgedDrawable The drawable to badge.
1865 * @param user The target user.
1866 * @param badgeLocation Where in the bounds of the badged drawable to place
Vadim Tryshev66ae66a2016-02-18 15:41:21 -08001867 * the badge. If it's {@code null}, the badge is applied on top of the entire
Svetoslavc71c42f2014-08-05 18:57:05 -07001868 * drawable being badged.
1869 * @param badgeDensity The optional desired density for the badge as per
Vadim Tryshev66ae66a2016-02-18 15:41:21 -08001870 * {@link android.util.DisplayMetrics#densityDpi}. If it's not positive,
Svetoslavc71c42f2014-08-05 18:57:05 -07001871 * the density of the display is used.
1872 * @return A drawable that combines the original drawable and a badge as
1873 * determined by the system.
Svetoslavc7d62f02014-09-04 15:39:54 -07001874 * @removed
Svetoslavc71c42f2014-08-05 18:57:05 -07001875 */
1876 public Drawable getBadgedDrawableForUser(Drawable badgedDrawable, UserHandle user,
1877 Rect badgeLocation, int badgeDensity) {
Svetoslavc7d62f02014-09-04 15:39:54 -07001878 return mContext.getPackageManager().getUserBadgedDrawableForDensity(badgedDrawable, user,
1879 badgeLocation, badgeDensity);
Svetoslavc71c42f2014-08-05 18:57:05 -07001880 }
1881
1882 /**
1883 * If the target user is a managed profile of the calling user or the caller
Kenny Guyf7ecf7c2014-06-18 11:32:05 +01001884 * is itself a managed profile, then this returns a copy of the label with
1885 * badging for accessibility services like talkback. E.g. passing in "Email"
1886 * and it might return "Work Email" for Email in the work profile.
1887 *
1888 * @param label The label to change.
1889 * @param user The target user.
1890 * @return A label that combines the original label and a badge as
1891 * determined by the system.
Svetoslavc7d62f02014-09-04 15:39:54 -07001892 * @removed
Kenny Guyf7ecf7c2014-06-18 11:32:05 +01001893 */
Kenny Guy237aecd2014-07-21 14:06:09 +01001894 public CharSequence getBadgedLabelForUser(CharSequence label, UserHandle user) {
Svetoslavc7d62f02014-09-04 15:39:54 -07001895 return mContext.getPackageManager().getUserBadgedLabel(label, user);
Amith Yamasani4f582632014-02-19 14:31:52 -08001896 }
1897
1898 /**
1899 * Returns information for all users on this device. Requires
1900 * {@link android.Manifest.permission#MANAGE_USERS} permission.
Emily Bernier394a6cd2014-05-07 12:49:20 -04001901 *
Amith Yamasani4f582632014-02-19 14:31:52 -08001902 * @param excludeDying specify if the list should exclude users being
1903 * removed.
Amith Yamasani920ace02012-09-20 22:15:37 -07001904 * @return the list of users that were created.
1905 * @hide
1906 */
1907 public List<UserInfo> getUsers(boolean excludeDying) {
1908 try {
1909 return mService.getUsers(excludeDying);
Amith Yamasani258848d2012-08-10 17:06:33 -07001910 } catch (RemoteException re) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001911 throw re.rethrowFromSystemServer();
Amith Yamasani258848d2012-08-10 17:06:33 -07001912 }
1913 }
1914
1915 /**
1916 * Removes a user and all associated data.
Amith Yamasani195263742012-08-21 15:40:12 -07001917 * Requires {@link android.Manifest.permission#MANAGE_USERS} permission.
Amith Yamasani258848d2012-08-10 17:06:33 -07001918 * @param userHandle the integer handle of the user, where 0 is the primary user.
1919 * @hide
1920 */
Jeff Sharkey8588bc12016-01-06 16:47:42 -07001921 public boolean removeUser(@UserIdInt int userHandle) {
Amith Yamasani258848d2012-08-10 17:06:33 -07001922 try {
1923 return mService.removeUser(userHandle);
1924 } catch (RemoteException re) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001925 throw re.rethrowFromSystemServer();
Amith Yamasani258848d2012-08-10 17:06:33 -07001926 }
1927 }
1928
1929 /**
1930 * Updates the user's name.
Amith Yamasani195263742012-08-21 15:40:12 -07001931 * Requires {@link android.Manifest.permission#MANAGE_USERS} permission.
Amith Yamasani258848d2012-08-10 17:06:33 -07001932 *
1933 * @param userHandle the user's integer handle
1934 * @param name the new name for the user
1935 * @hide
1936 */
Jeff Sharkey8588bc12016-01-06 16:47:42 -07001937 public void setUserName(@UserIdInt int userHandle, String name) {
Amith Yamasani258848d2012-08-10 17:06:33 -07001938 try {
1939 mService.setUserName(userHandle, name);
1940 } catch (RemoteException re) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001941 throw re.rethrowFromSystemServer();
Amith Yamasani258848d2012-08-10 17:06:33 -07001942 }
1943 }
1944
1945 /**
Amith Yamasanie928d7d2012-09-17 21:46:51 -07001946 * Sets the user's photo.
Amith Yamasani258848d2012-08-10 17:06:33 -07001947 * @param userHandle the user for whom to change the photo.
Amith Yamasanie928d7d2012-09-17 21:46:51 -07001948 * @param icon the bitmap to set as the photo.
Amith Yamasani258848d2012-08-10 17:06:33 -07001949 * @hide
1950 */
Jeff Sharkey8588bc12016-01-06 16:47:42 -07001951 public void setUserIcon(@UserIdInt int userHandle, Bitmap icon) {
Amith Yamasani258848d2012-08-10 17:06:33 -07001952 try {
Amith Yamasanie928d7d2012-09-17 21:46:51 -07001953 mService.setUserIcon(userHandle, icon);
Amith Yamasani258848d2012-08-10 17:06:33 -07001954 } catch (RemoteException re) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001955 throw re.rethrowFromSystemServer();
Amith Yamasani258848d2012-08-10 17:06:33 -07001956 }
1957 }
1958
1959 /**
Amith Yamasani3b49f072012-09-17 10:21:43 -07001960 * Returns a file descriptor for the user's photo. PNG data can be read from this file.
1961 * @param userHandle the user whose photo we want to read.
Amith Yamasanie928d7d2012-09-17 21:46:51 -07001962 * @return a {@link Bitmap} of the user's photo, or null if there's no photo.
Alexandra Gherghina64d4dca2014-08-28 18:26:56 +01001963 * @see com.android.internal.util.UserIcons#getDefaultUserIcon for a default.
Amith Yamasani3b49f072012-09-17 10:21:43 -07001964 * @hide
1965 */
Jeff Sharkey8588bc12016-01-06 16:47:42 -07001966 public Bitmap getUserIcon(@UserIdInt int userHandle) {
Amith Yamasani3b49f072012-09-17 10:21:43 -07001967 try {
Adrian Roos1bdff912015-02-17 15:51:35 +01001968 ParcelFileDescriptor fd = mService.getUserIcon(userHandle);
1969 if (fd != null) {
1970 try {
1971 return BitmapFactory.decodeFileDescriptor(fd.getFileDescriptor());
1972 } finally {
1973 try {
1974 fd.close();
1975 } catch (IOException e) {
1976 }
1977 }
1978 }
Amith Yamasani3b49f072012-09-17 10:21:43 -07001979 } catch (RemoteException re) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001980 throw re.rethrowFromSystemServer();
Amith Yamasani3b49f072012-09-17 10:21:43 -07001981 }
Adrian Roos1bdff912015-02-17 15:51:35 +01001982 return null;
Amith Yamasani3b49f072012-09-17 10:21:43 -07001983 }
1984
1985 /**
Amith Yamasani258848d2012-08-10 17:06:33 -07001986 * Returns the maximum number of users that can be created on this device. A return value
1987 * of 1 means that it is a single user device.
1988 * @hide
Maggie Benthalla12fccf2013-03-14 18:02:12 -04001989 * @return a value greater than or equal to 1
Amith Yamasani258848d2012-08-10 17:06:33 -07001990 */
Jeff Sharkey27bd34d2012-09-16 12:49:00 -07001991 public static int getMaxSupportedUsers() {
Amith Yamasaniff549202012-10-12 12:44:49 -07001992 // Don't allow multiple users on certain builds
1993 if (android.os.Build.ID.startsWith("JVP")) return 1;
Dianne Hackborn409297d2014-07-10 17:39:20 -07001994 // Svelte devices don't get multi-user.
1995 if (ActivityManager.isLowRamDeviceStatic()) return 1;
Jeff Sharkey27bd34d2012-09-16 12:49:00 -07001996 return SystemProperties.getInt("fw.max_users",
1997 Resources.getSystem().getInteger(R.integer.config_multiuserMaximumUsers));
Amith Yamasani258848d2012-08-10 17:06:33 -07001998 }
Amith Yamasani2a003292012-08-14 18:25:45 -07001999
2000 /**
Fyodor Kupolovcd86ebf2015-09-29 17:06:50 -07002001 * Returns true if the user switcher should be shown, this will be if device supports multi-user
2002 * and there are at least 2 users available that are not managed profiles.
Kenny Guy1a447532014-02-20 21:55:32 +00002003 * @hide
2004 * @return true if user switcher should be shown.
2005 */
2006 public boolean isUserSwitcherEnabled() {
Fyodor Kupolovcd86ebf2015-09-29 17:06:50 -07002007 if (!supportsMultipleUsers()) {
2008 return false;
2009 }
Amith Yamasanieb437d42016-04-29 09:31:25 -07002010 // If Demo Mode is on, don't show user switcher
2011 if (isDeviceInDemoMode(mContext)) {
2012 return false;
2013 }
Kenny Guy1a447532014-02-20 21:55:32 +00002014 List<UserInfo> users = getUsers(true);
2015 if (users == null) {
2016 return false;
2017 }
2018 int switchableUserCount = 0;
2019 for (UserInfo user : users) {
Xiaohui Chen7cb69df2015-07-13 16:01:01 -07002020 if (user.supportsSwitchToByUser()) {
Kenny Guy1a447532014-02-20 21:55:32 +00002021 ++switchableUserCount;
2022 }
2023 }
Fyodor Kupolovcd86ebf2015-09-29 17:06:50 -07002024 final boolean guestEnabled = !mContext.getSystemService(DevicePolicyManager.class)
2025 .getGuestUserDisabled(null);
Amith Yamasania596ff82014-06-12 18:12:38 -07002026 return switchableUserCount > 1 || guestEnabled;
Kenny Guy1a447532014-02-20 21:55:32 +00002027 }
2028
2029 /**
Amith Yamasanieb437d42016-04-29 09:31:25 -07002030 * @hide
2031 */
2032 public static boolean isDeviceInDemoMode(Context context) {
2033 return Settings.Global.getInt(context.getContentResolver(),
2034 Settings.Global.DEVICE_DEMO_MODE, 0) > 0;
2035 }
2036
2037 /**
Amith Yamasani2a003292012-08-14 18:25:45 -07002038 * Returns a serial number on this device for a given userHandle. User handles can be recycled
2039 * when deleting and creating users, but serial numbers are not reused until the device is wiped.
2040 * @param userHandle
2041 * @return a serial number associated with that user, or -1 if the userHandle is not valid.
2042 * @hide
2043 */
Jeff Sharkey8588bc12016-01-06 16:47:42 -07002044 public int getUserSerialNumber(@UserIdInt int userHandle) {
Amith Yamasani2a003292012-08-14 18:25:45 -07002045 try {
2046 return mService.getUserSerialNumber(userHandle);
2047 } catch (RemoteException re) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07002048 throw re.rethrowFromSystemServer();
Amith Yamasani2a003292012-08-14 18:25:45 -07002049 }
Amith Yamasani2a003292012-08-14 18:25:45 -07002050 }
2051
2052 /**
2053 * Returns a userHandle on this device for a given user serial number. User handles can be
2054 * recycled when deleting and creating users, but serial numbers are not reused until the device
2055 * is wiped.
2056 * @param userSerialNumber
2057 * @return the userHandle associated with that user serial number, or -1 if the serial number
2058 * is not valid.
2059 * @hide
2060 */
Jeff Sharkey8588bc12016-01-06 16:47:42 -07002061 public @UserIdInt int getUserHandle(int userSerialNumber) {
Amith Yamasani2a003292012-08-14 18:25:45 -07002062 try {
2063 return mService.getUserHandle(userSerialNumber);
2064 } catch (RemoteException re) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07002065 throw re.rethrowFromSystemServer();
Amith Yamasani2a003292012-08-14 18:25:45 -07002066 }
Amith Yamasani2a003292012-08-14 18:25:45 -07002067 }
Maggie Benthall67944582013-02-22 14:58:27 -05002068
Amith Yamasani7e99bc02013-04-16 18:24:51 -07002069 /**
Esteban Talavera953fe482016-06-07 15:25:20 +01002070 * Returns a {@link Bundle} containing any saved application restrictions for this user, for the
Amith Yamasani7e99bc02013-04-16 18:24:51 -07002071 * given package name. Only an application with this package name can call this method.
Esteban Talavera5b9f1672015-12-11 15:22:34 +00002072 *
2073 * <p>The returned {@link Bundle} consists of key-value pairs, as defined by the application,
2074 * where the types of values may be:
2075 * <ul>
2076 * <li>{@code boolean}
2077 * <li>{@code int}
2078 * <li>{@code String} or {@code String[]}
2079 * <li>From {@link android.os.Build.VERSION_CODES#M}, {@code Bundle} or {@code Bundle[]}
2080 * </ul>
2081 *
Amith Yamasani7e99bc02013-04-16 18:24:51 -07002082 * @param packageName the package name of the calling application
Esteban Talavera953fe482016-06-07 15:25:20 +01002083 * @return a {@link Bundle} with the restrictions for that package, or an empty {@link Bundle}
2084 * if there are no saved restrictions.
Esteban Talavera5b9f1672015-12-11 15:22:34 +00002085 *
2086 * @see #KEY_RESTRICTIONS_PENDING
Amith Yamasani7e99bc02013-04-16 18:24:51 -07002087 */
2088 public Bundle getApplicationRestrictions(String packageName) {
2089 try {
2090 return mService.getApplicationRestrictions(packageName);
2091 } catch (RemoteException re) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07002092 throw re.rethrowFromSystemServer();
Amith Yamasani7e99bc02013-04-16 18:24:51 -07002093 }
Amith Yamasani7e99bc02013-04-16 18:24:51 -07002094 }
Amith Yamasanidf2e92a2013-03-01 17:04:38 -08002095
2096 /**
2097 * @hide
2098 */
Amith Yamasani7e99bc02013-04-16 18:24:51 -07002099 public Bundle getApplicationRestrictions(String packageName, UserHandle user) {
Amith Yamasanidf2e92a2013-03-01 17:04:38 -08002100 try {
Amith Yamasani7e99bc02013-04-16 18:24:51 -07002101 return mService.getApplicationRestrictionsForUser(packageName, user.getIdentifier());
Amith Yamasanidf2e92a2013-03-01 17:04:38 -08002102 } catch (RemoteException re) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07002103 throw re.rethrowFromSystemServer();
Amith Yamasanidf2e92a2013-03-01 17:04:38 -08002104 }
Amith Yamasanidf2e92a2013-03-01 17:04:38 -08002105 }
2106
2107 /**
2108 * @hide
2109 */
Amith Yamasani7e99bc02013-04-16 18:24:51 -07002110 public void setApplicationRestrictions(String packageName, Bundle restrictions,
Amith Yamasanidf2e92a2013-03-01 17:04:38 -08002111 UserHandle user) {
2112 try {
Amith Yamasani7e99bc02013-04-16 18:24:51 -07002113 mService.setApplicationRestrictions(packageName, restrictions, user.getIdentifier());
Amith Yamasanidf2e92a2013-03-01 17:04:38 -08002114 } catch (RemoteException re) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07002115 throw re.rethrowFromSystemServer();
Amith Yamasanidf2e92a2013-03-01 17:04:38 -08002116 }
2117 }
Amith Yamasani655d0e22013-06-12 14:19:10 -07002118
2119 /**
Amith Yamasanid304af62013-09-05 09:30:23 -07002120 * Sets a new challenge PIN for restrictions. This is only for use by pre-installed
2121 * apps and requires the MANAGE_USERS permission.
2122 * @param newPin the PIN to use for challenge dialogs.
2123 * @return Returns true if the challenge PIN was set successfully.
Fyodor Kupolovef249092015-05-06 13:18:46 -07002124 * @deprecated The restrictions PIN functionality is no longer provided by the system.
2125 * This method is preserved for backwards compatibility reasons and always returns false.
Amith Yamasani655d0e22013-06-12 14:19:10 -07002126 */
Amith Yamasanid304af62013-09-05 09:30:23 -07002127 public boolean setRestrictionsChallenge(String newPin) {
Amith Yamasani655d0e22013-06-12 14:19:10 -07002128 return false;
2129 }
Amith Yamasani1a7472e2013-07-02 11:17:30 -07002130
Amith Yamasanie4afaa32014-06-30 14:55:07 +05302131 /**
2132 * @hide
2133 * Set restrictions that should apply to any future guest user that's created.
2134 */
2135 public void setDefaultGuestRestrictions(Bundle restrictions) {
2136 try {
2137 mService.setDefaultGuestRestrictions(restrictions);
2138 } catch (RemoteException re) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07002139 throw re.rethrowFromSystemServer();
Amith Yamasanie4afaa32014-06-30 14:55:07 +05302140 }
2141 }
2142
2143 /**
2144 * @hide
2145 * Gets the default guest restrictions.
2146 */
2147 public Bundle getDefaultGuestRestrictions() {
2148 try {
2149 return mService.getDefaultGuestRestrictions();
2150 } catch (RemoteException re) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07002151 throw re.rethrowFromSystemServer();
Amith Yamasanie4afaa32014-06-30 14:55:07 +05302152 }
Amith Yamasanie4afaa32014-06-30 14:55:07 +05302153 }
Fyodor Kupolovff7233e2015-04-08 11:28:52 -07002154
2155 /**
2156 * Returns creation time of the user or of a managed profile associated with the calling user.
2157 * @param userHandle user handle of the user or a managed profile associated with the
2158 * calling user.
2159 * @return creation time in milliseconds since Epoch time.
2160 */
Fyodor Kupolov385de622015-04-10 18:00:19 -07002161 public long getUserCreationTime(UserHandle userHandle) {
Fyodor Kupolovff7233e2015-04-08 11:28:52 -07002162 try {
Fyodor Kupolov385de622015-04-10 18:00:19 -07002163 return mService.getUserCreationTime(userHandle.getIdentifier());
Fyodor Kupolovff7233e2015-04-08 11:28:52 -07002164 } catch (RemoteException re) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07002165 throw re.rethrowFromSystemServer();
Fyodor Kupolovff7233e2015-04-08 11:28:52 -07002166 }
2167 }
Amith Yamasani12747872015-12-07 14:19:49 -08002168
2169 /**
2170 * @hide
2171 * Checks if any uninitialized user has the specific seed account name and type.
2172 *
2173 * @param mAccountName The account name to check for
2174 * @param mAccountType The account type of the account to check for
2175 * @return whether the seed account was found
2176 */
2177 public boolean someUserHasSeedAccount(String accountName, String accountType) {
2178 try {
2179 return mService.someUserHasSeedAccount(accountName, accountType);
2180 } catch (RemoteException re) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07002181 throw re.rethrowFromSystemServer();
Amith Yamasani12747872015-12-07 14:19:49 -08002182 }
2183 }
Amith Yamasani258848d2012-08-10 17:06:33 -07002184}