blob: da7f85fc0e1dac9c329e24aff2498331bc1bc830 [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 */
16package android.os;
17
Xiaohui Chenb3b92582015-12-07 11:22:13 -080018import android.Manifest;
Fyodor Kupolov02cb6e72015-09-18 18:20:55 -070019import android.accounts.AccountManager;
Xiaohui Chen7cb69df2015-07-13 16:01:01 -070020import android.annotation.Nullable;
Xiaohui Chenb3b92582015-12-07 11:22:13 -080021import android.annotation.RequiresPermission;
Amith Yamasani0e8d7d62014-09-03 13:17:28 -070022import android.annotation.SystemApi;
Jeff Sharkey8588bc12016-01-06 16:47:42 -070023import android.annotation.UserIdInt;
Amith Yamasani37ed8d12016-01-27 14:40:16 -080024import android.app.Activity;
Dianne Hackborn409297d2014-07-10 17:39:20 -070025import android.app.ActivityManager;
Dianne Hackborna8a9bd62012-10-09 15:36:59 -070026import android.app.ActivityManagerNative;
Fyodor Kupolovcd86ebf2015-09-29 17:06:50 -070027import android.app.admin.DevicePolicyManager;
Makoto Onuki068c54a2015-10-13 14:34:03 -070028import android.content.ComponentName;
Amith Yamasani258848d2012-08-10 17:06:33 -070029import android.content.Context;
Amith Yamasani12747872015-12-07 14:19:49 -080030import android.content.Intent;
Amith Yamasani258848d2012-08-10 17:06:33 -070031import android.content.pm.UserInfo;
Jeff Sharkey27bd34d2012-09-16 12:49:00 -070032import android.content.res.Resources;
Maggie Benthall67944582013-02-22 14:58:27 -050033import android.graphics.Bitmap;
Adrian Roos1bdff912015-02-17 15:51:35 +010034import android.graphics.BitmapFactory;
Amith Yamasani4f582632014-02-19 14:31:52 -080035import android.graphics.Rect;
Amith Yamasani4f582632014-02-19 14:31:52 -080036import android.graphics.drawable.Drawable;
Jeff Sharkey5b0e5202015-12-18 17:18:09 -070037import android.os.storage.StorageManager;
Amith Yamasani1e9c2182014-06-11 17:25:51 -070038import android.provider.Settings;
Amith Yamasani258848d2012-08-10 17:06:33 -070039import android.util.Log;
Jason Monk1c7c3192014-06-26 12:52:18 -040040import android.view.WindowManager.LayoutParams;
Amith Yamasani258848d2012-08-10 17:06:33 -070041
Maggie Benthall67944582013-02-22 14:58:27 -050042import com.android.internal.R;
43
Adrian Roos1bdff912015-02-17 15:51:35 +010044import java.io.IOException;
Amith Yamasani4f582632014-02-19 14:31:52 -080045import java.util.ArrayList;
Amith Yamasani258848d2012-08-10 17:06:33 -070046import java.util.List;
47
48/**
49 * Manages users and user details on a multi-user system.
50 */
51public class UserManager {
52
53 private static String TAG = "UserManager";
54 private final IUserManager mService;
55 private final Context mContext;
56
Amith Yamasanie4cf7342012-12-17 11:12:09 -080057 /**
Fyodor Kupolov53019282015-07-21 11:48:18 -070058 * Specifies if a user is disallowed from adding and removing accounts, unless they are
59 * {@link android.accounts.AccountManager#addAccountExplicitly programmatically} added by
60 * Authenticator.
Amith Yamasani71e6c692013-03-24 17:39:28 -070061 * The default value is <code>false</code>.
Amith Yamasani26af8292014-09-09 09:57:27 -070062 *
Benjamin Franzb6c0ce42015-11-05 10:06:51 +000063 * <p>From {@link android.os.Build.VERSION_CODES#N} a profile or device owner app can still
64 * use {@link android.accounts.AccountManager} APIs to add or remove accounts when account
65 * management is disallowed.
66 *
Fyodor Kupolov0ffbfea2016-01-19 10:07:54 -080067 * <p>Key for user restrictions.
68 * <p>Type: Boolean
Makoto Onuki068c54a2015-10-13 14:34:03 -070069 * @see DevicePolicyManager#addUserRestriction(ComponentName, String)
70 * @see DevicePolicyManager#clearUserRestriction(ComponentName, String)
Amith Yamasanie4cf7342012-12-17 11:12:09 -080071 * @see #getUserRestrictions()
72 */
Amith Yamasani71e6c692013-03-24 17:39:28 -070073 public static final String DISALLOW_MODIFY_ACCOUNTS = "no_modify_accounts";
Amith Yamasanie4cf7342012-12-17 11:12:09 -080074
75 /**
Amith Yamasani26af8292014-09-09 09:57:27 -070076 * Specifies if a user is disallowed from changing Wi-Fi
Julia Reynolds2cb384f2014-08-13 15:15:55 -040077 * access points. The default value is <code>false</code>.
Fyodor Kupolov0ffbfea2016-01-19 10:07:54 -080078 * <p>This restriction has no effect in a managed profile.
Amith Yamasani26af8292014-09-09 09:57:27 -070079 *
Fyodor Kupolov0ffbfea2016-01-19 10:07:54 -080080 * <p>Key for user restrictions.
81 * <p>Type: Boolean
Makoto Onuki068c54a2015-10-13 14:34:03 -070082 * @see DevicePolicyManager#addUserRestriction(ComponentName, String)
83 * @see DevicePolicyManager#clearUserRestriction(ComponentName, String)
Amith Yamasanie4cf7342012-12-17 11:12:09 -080084 * @see #getUserRestrictions()
85 */
Amith Yamasani71e6c692013-03-24 17:39:28 -070086 public static final String DISALLOW_CONFIG_WIFI = "no_config_wifi";
Amith Yamasanie4cf7342012-12-17 11:12:09 -080087
88 /**
Amith Yamasani26af8292014-09-09 09:57:27 -070089 * Specifies if a user is disallowed from installing applications.
Amith Yamasani71e6c692013-03-24 17:39:28 -070090 * The default value is <code>false</code>.
Amith Yamasani26af8292014-09-09 09:57:27 -070091 *
Fyodor Kupolov0ffbfea2016-01-19 10:07:54 -080092 * <p>Key for user restrictions.
93 * <p>Type: Boolean
Makoto Onuki068c54a2015-10-13 14:34:03 -070094 * @see DevicePolicyManager#addUserRestriction(ComponentName, String)
95 * @see DevicePolicyManager#clearUserRestriction(ComponentName, String)
Amith Yamasanie4cf7342012-12-17 11:12:09 -080096 * @see #getUserRestrictions()
97 */
Amith Yamasani71e6c692013-03-24 17:39:28 -070098 public static final String DISALLOW_INSTALL_APPS = "no_install_apps";
Amith Yamasanie4cf7342012-12-17 11:12:09 -080099
100 /**
Amith Yamasani26af8292014-09-09 09:57:27 -0700101 * Specifies if a user is disallowed from uninstalling applications.
Amith Yamasani71e6c692013-03-24 17:39:28 -0700102 * The default value is <code>false</code>.
Amith Yamasani26af8292014-09-09 09:57:27 -0700103 *
Fyodor Kupolov0ffbfea2016-01-19 10:07:54 -0800104 * <p>Key for user restrictions.
105 * <p>Type: Boolean
Makoto Onuki068c54a2015-10-13 14:34:03 -0700106 * @see DevicePolicyManager#addUserRestriction(ComponentName, String)
107 * @see DevicePolicyManager#clearUserRestriction(ComponentName, String)
Amith Yamasanie4cf7342012-12-17 11:12:09 -0800108 * @see #getUserRestrictions()
109 */
Amith Yamasani71e6c692013-03-24 17:39:28 -0700110 public static final String DISALLOW_UNINSTALL_APPS = "no_uninstall_apps";
Amith Yamasanie4cf7342012-12-17 11:12:09 -0800111
Amith Yamasani71e6c692013-03-24 17:39:28 -0700112 /**
Amith Yamasani150514b2015-01-07 16:05:05 -0800113 * Specifies if a user is disallowed from turning on location sharing.
Amith Yamasani71e6c692013-03-24 17:39:28 -0700114 * The default value is <code>false</code>.
Fyodor Kupolov0ffbfea2016-01-19 10:07:54 -0800115 * <p>In a managed profile, location sharing always reflects the primary user's setting, but
Amith Yamasani150514b2015-01-07 16:05:05 -0800116 * can be overridden and forced off by setting this restriction to true in the managed profile.
Amith Yamasani26af8292014-09-09 09:57:27 -0700117 *
Fyodor Kupolov0ffbfea2016-01-19 10:07:54 -0800118 * <p>Key for user restrictions.
119 * <p>Type: Boolean
Makoto Onuki068c54a2015-10-13 14:34:03 -0700120 * @see DevicePolicyManager#addUserRestriction(ComponentName, String)
121 * @see DevicePolicyManager#clearUserRestriction(ComponentName, String)
Maggie Benthall67944582013-02-22 14:58:27 -0500122 * @see #getUserRestrictions()
123 */
Amith Yamasani71e6c692013-03-24 17:39:28 -0700124 public static final String DISALLOW_SHARE_LOCATION = "no_share_location";
Maggie Benthall67944582013-02-22 14:58:27 -0500125
Maggie Benthalla12fccf2013-03-14 18:02:12 -0400126 /**
Amith Yamasani26af8292014-09-09 09:57:27 -0700127 * Specifies if a user is disallowed from enabling the
Maggie Benthalla12fccf2013-03-14 18:02:12 -0400128 * "Unknown Sources" setting, that allows installation of apps from unknown sources.
129 * The default value is <code>false</code>.
Amith Yamasani26af8292014-09-09 09:57:27 -0700130 *
Fyodor Kupolov0ffbfea2016-01-19 10:07:54 -0800131 * <p>Key for user restrictions.
132 * <p>Type: Boolean
Makoto Onuki068c54a2015-10-13 14:34:03 -0700133 * @see DevicePolicyManager#addUserRestriction(ComponentName, String)
134 * @see DevicePolicyManager#clearUserRestriction(ComponentName, String)
Maggie Benthalla12fccf2013-03-14 18:02:12 -0400135 * @see #getUserRestrictions()
136 */
137 public static final String DISALLOW_INSTALL_UNKNOWN_SOURCES = "no_install_unknown_sources";
138
139 /**
Amith Yamasani26af8292014-09-09 09:57:27 -0700140 * Specifies if a user is disallowed from configuring bluetooth.
Nicolas Prevot1c4c4422015-02-16 11:32:21 +0000141 * This does <em>not</em> restrict the user from turning bluetooth on or off.
Maggie Benthalla12fccf2013-03-14 18:02:12 -0400142 * The default value is <code>false</code>.
Fyodor Kupolov0ffbfea2016-01-19 10:07:54 -0800143 * <p>This restriction has no effect in a managed profile.
Amith Yamasani26af8292014-09-09 09:57:27 -0700144 *
Fyodor Kupolov0ffbfea2016-01-19 10:07:54 -0800145 * <p>Key for user restrictions.
146 * <p>Type: Boolean
Makoto Onuki068c54a2015-10-13 14:34:03 -0700147 * @see DevicePolicyManager#addUserRestriction(ComponentName, String)
148 * @see DevicePolicyManager#clearUserRestriction(ComponentName, String)
Maggie Benthalla12fccf2013-03-14 18:02:12 -0400149 * @see #getUserRestrictions()
150 */
151 public static final String DISALLOW_CONFIG_BLUETOOTH = "no_config_bluetooth";
152
Maggie Benthalla12fccf2013-03-14 18:02:12 -0400153 /**
Amith Yamasani26af8292014-09-09 09:57:27 -0700154 * Specifies if a user is disallowed from transferring files over
Amith Yamasanic34dc7c2014-09-18 09:42:42 -0700155 * USB. This can only be set by device owners and profile owners on the primary user.
156 * The default value is <code>false</code>.
Amith Yamasani26af8292014-09-09 09:57:27 -0700157 *
Fyodor Kupolov0ffbfea2016-01-19 10:07:54 -0800158 * <p>Key for user restrictions.
159 * <p>Type: Boolean
Makoto Onuki068c54a2015-10-13 14:34:03 -0700160 * @see DevicePolicyManager#addUserRestriction(ComponentName, String)
161 * @see DevicePolicyManager#clearUserRestriction(ComponentName, String)
Maggie Benthalla12fccf2013-03-14 18:02:12 -0400162 * @see #getUserRestrictions()
163 */
164 public static final String DISALLOW_USB_FILE_TRANSFER = "no_usb_file_transfer";
165
Emily Bernierb223f732013-04-11 15:46:36 -0400166 /**
Amith Yamasani26af8292014-09-09 09:57:27 -0700167 * Specifies if a user is disallowed from configuring user
Emily Bernierb223f732013-04-11 15:46:36 -0400168 * credentials. The default value is <code>false</code>.
Amith Yamasani26af8292014-09-09 09:57:27 -0700169 *
Fyodor Kupolov0ffbfea2016-01-19 10:07:54 -0800170 * <p>Key for user restrictions.
171 * <p>Type: Boolean
Makoto Onuki068c54a2015-10-13 14:34:03 -0700172 * @see DevicePolicyManager#addUserRestriction(ComponentName, String)
173 * @see DevicePolicyManager#clearUserRestriction(ComponentName, String)
Emily Bernierb223f732013-04-11 15:46:36 -0400174 * @see #getUserRestrictions()
175 */
176 public static final String DISALLOW_CONFIG_CREDENTIALS = "no_config_credentials";
177
178 /**
Amith Yamasani150514b2015-01-07 16:05:05 -0800179 * When set on the primary user this specifies if the user can remove other users.
180 * When set on a secondary user, this specifies if the user can remove itself.
181 * This restriction has no effect on managed profiles.
182 * The default value is <code>false</code>.
Amith Yamasani26af8292014-09-09 09:57:27 -0700183 *
Fyodor Kupolov0ffbfea2016-01-19 10:07:54 -0800184 * <p>Key for user restrictions.
185 * <p>Type: Boolean
Makoto Onuki068c54a2015-10-13 14:34:03 -0700186 * @see DevicePolicyManager#addUserRestriction(ComponentName, String)
187 * @see DevicePolicyManager#clearUserRestriction(ComponentName, String)
Emily Bernierb223f732013-04-11 15:46:36 -0400188 * @see #getUserRestrictions()
189 */
190 public static final String DISALLOW_REMOVE_USER = "no_remove_user";
191
Julia Reynoldsd46d0f92014-04-23 15:23:24 -0400192 /**
Amith Yamasani26af8292014-09-09 09:57:27 -0700193 * Specifies if a user is disallowed from enabling or
Julia Reynoldsd46d0f92014-04-23 15:23:24 -0400194 * accessing debugging features. The default value is <code>false</code>.
Amith Yamasani26af8292014-09-09 09:57:27 -0700195 *
Fyodor Kupolov0ffbfea2016-01-19 10:07:54 -0800196 * <p>Key for user restrictions.
197 * <p>Type: Boolean
Makoto Onuki068c54a2015-10-13 14:34:03 -0700198 * @see DevicePolicyManager#addUserRestriction(ComponentName, String)
199 * @see DevicePolicyManager#clearUserRestriction(ComponentName, String)
Julia Reynoldsd46d0f92014-04-23 15:23:24 -0400200 * @see #getUserRestrictions()
201 */
202 public static final String DISALLOW_DEBUGGING_FEATURES = "no_debugging_features";
203
204 /**
Amith Yamasani26af8292014-09-09 09:57:27 -0700205 * Specifies if a user is disallowed from configuring VPN.
Julia Reynoldsd46d0f92014-04-23 15:23:24 -0400206 * The default value is <code>false</code>.
Nicolas Prevot298f98f2015-05-22 16:43:03 -0700207 * This restriction has an effect in a managed profile only from
Dianne Hackborn0e3de6c2015-07-29 15:20:21 -0700208 * {@link android.os.Build.VERSION_CODES#M}
Amith Yamasani26af8292014-09-09 09:57:27 -0700209 *
Fyodor Kupolov0ffbfea2016-01-19 10:07:54 -0800210 * <p>Key for user restrictions.
211 * <p>Type: Boolean
Makoto Onuki068c54a2015-10-13 14:34:03 -0700212 * @see DevicePolicyManager#addUserRestriction(ComponentName, String)
213 * @see DevicePolicyManager#clearUserRestriction(ComponentName, String)
Julia Reynoldsd46d0f92014-04-23 15:23:24 -0400214 * @see #getUserRestrictions()
215 */
216 public static final String DISALLOW_CONFIG_VPN = "no_config_vpn";
217
218 /**
Amith Yamasani26af8292014-09-09 09:57:27 -0700219 * Specifies if a user is disallowed from configuring Tethering
Amith Yamasanic34dc7c2014-09-18 09:42:42 -0700220 * & portable hotspots. This can only be set by device owners and profile owners on the
221 * primary user. The default value is <code>false</code>.
Amith Yamasani26af8292014-09-09 09:57:27 -0700222 *
Fyodor Kupolov0ffbfea2016-01-19 10:07:54 -0800223 * <p>Key for user restrictions.
224 * <p>Type: Boolean
Makoto Onuki068c54a2015-10-13 14:34:03 -0700225 * @see DevicePolicyManager#addUserRestriction(ComponentName, String)
226 * @see DevicePolicyManager#clearUserRestriction(ComponentName, String)
Julia Reynoldsd46d0f92014-04-23 15:23:24 -0400227 * @see #getUserRestrictions()
228 */
229 public static final String DISALLOW_CONFIG_TETHERING = "no_config_tethering";
230
231 /**
Stuart Scotte3e314d2015-04-20 14:07:45 -0700232 * Specifies if a user is disallowed from resetting network settings
233 * from Settings. This can only be set by device owners and profile owners on the primary user.
234 * The default value is <code>false</code>.
Fyodor Kupolov0ffbfea2016-01-19 10:07:54 -0800235 * <p>This restriction has no effect on secondary users and managed profiles since only the
Stuart Scotte3e314d2015-04-20 14:07:45 -0700236 * primary user can reset the network settings of the device.
237 *
Fyodor Kupolov0ffbfea2016-01-19 10:07:54 -0800238 * <p>Key for user restrictions.
239 * <p>Type: Boolean
Makoto Onuki068c54a2015-10-13 14:34:03 -0700240 * @see DevicePolicyManager#addUserRestriction(ComponentName, String)
241 * @see DevicePolicyManager#clearUserRestriction(ComponentName, String)
Stuart Scotte3e314d2015-04-20 14:07:45 -0700242 * @see #getUserRestrictions()
243 */
244 public static final String DISALLOW_NETWORK_RESET = "no_network_reset";
245
246 /**
Amith Yamasani26af8292014-09-09 09:57:27 -0700247 * Specifies if a user is disallowed from factory resetting
Amith Yamasanic34dc7c2014-09-18 09:42:42 -0700248 * from Settings. This can only be set by device owners and profile owners on the primary user.
249 * The default value is <code>false</code>.
Fyodor Kupolov0ffbfea2016-01-19 10:07:54 -0800250 * <p>This restriction has no effect on secondary users and managed profiles since only the
Amith Yamasani150514b2015-01-07 16:05:05 -0800251 * primary user can factory reset the device.
Amith Yamasani26af8292014-09-09 09:57:27 -0700252 *
Fyodor Kupolov0ffbfea2016-01-19 10:07:54 -0800253 * <p>Key for user restrictions.
254 * <p>Type: Boolean
Makoto Onuki068c54a2015-10-13 14:34:03 -0700255 * @see DevicePolicyManager#addUserRestriction(ComponentName, String)
256 * @see DevicePolicyManager#clearUserRestriction(ComponentName, String)
Julia Reynoldsd46d0f92014-04-23 15:23:24 -0400257 * @see #getUserRestrictions()
258 */
259 public static final String DISALLOW_FACTORY_RESET = "no_factory_reset";
260
261 /**
Amith Yamasani26af8292014-09-09 09:57:27 -0700262 * Specifies if a user is disallowed from adding new users and
Amith Yamasanic34dc7c2014-09-18 09:42:42 -0700263 * profiles. This can only be set by device owners and profile owners on the primary user.
264 * The default value is <code>false</code>.
Fyodor Kupolov0ffbfea2016-01-19 10:07:54 -0800265 * <p>This restriction has no effect on secondary users and managed profiles since only the
Amith Yamasani150514b2015-01-07 16:05:05 -0800266 * primary user can add other users.
Amith Yamasani26af8292014-09-09 09:57:27 -0700267 *
Fyodor Kupolov0ffbfea2016-01-19 10:07:54 -0800268 * <p>Key for user restrictions.
269 * <p>Type: Boolean
Makoto Onuki068c54a2015-10-13 14:34:03 -0700270 * @see DevicePolicyManager#addUserRestriction(ComponentName, String)
271 * @see DevicePolicyManager#clearUserRestriction(ComponentName, String)
Julia Reynoldsd46d0f92014-04-23 15:23:24 -0400272 * @see #getUserRestrictions()
273 */
274 public static final String DISALLOW_ADD_USER = "no_add_user";
275
276 /**
Amith Yamasani26af8292014-09-09 09:57:27 -0700277 * Specifies if a user is disallowed from disabling application
Julia Reynoldsd46d0f92014-04-23 15:23:24 -0400278 * verification. The default value is <code>false</code>.
Amith Yamasani26af8292014-09-09 09:57:27 -0700279 *
Fyodor Kupolov0ffbfea2016-01-19 10:07:54 -0800280 * <p>Key for user restrictions.
281 * <p>Type: Boolean
Makoto Onuki068c54a2015-10-13 14:34:03 -0700282 * @see DevicePolicyManager#addUserRestriction(ComponentName, String)
283 * @see DevicePolicyManager#clearUserRestriction(ComponentName, String)
Julia Reynoldsd46d0f92014-04-23 15:23:24 -0400284 * @see #getUserRestrictions()
285 */
286 public static final String ENSURE_VERIFY_APPS = "ensure_verify_apps";
287
288 /**
Amith Yamasani26af8292014-09-09 09:57:27 -0700289 * Specifies if a user is disallowed from configuring cell
Amith Yamasanic34dc7c2014-09-18 09:42:42 -0700290 * broadcasts. This can only be set by device owners and profile owners on the primary user.
291 * The default value is <code>false</code>.
Fyodor Kupolov0ffbfea2016-01-19 10:07:54 -0800292 * <p>This restriction has no effect on secondary users and managed profiles since only the
Amith Yamasani150514b2015-01-07 16:05:05 -0800293 * primary user can configure cell broadcasts.
Amith Yamasani26af8292014-09-09 09:57:27 -0700294 *
Fyodor Kupolov0ffbfea2016-01-19 10:07:54 -0800295 * <p>Key for user restrictions.
296 * <p>Type: Boolean
Makoto Onuki068c54a2015-10-13 14:34:03 -0700297 * @see DevicePolicyManager#addUserRestriction(ComponentName, String)
298 * @see DevicePolicyManager#clearUserRestriction(ComponentName, String)
Julia Reynoldsd46d0f92014-04-23 15:23:24 -0400299 * @see #getUserRestrictions()
300 */
301 public static final String DISALLOW_CONFIG_CELL_BROADCASTS = "no_config_cell_broadcasts";
302
303 /**
Amith Yamasani26af8292014-09-09 09:57:27 -0700304 * Specifies if a user is disallowed from configuring mobile
Amith Yamasanic34dc7c2014-09-18 09:42:42 -0700305 * networks. This can only be set by device owners and profile owners on the primary user.
306 * The default value is <code>false</code>.
Fyodor Kupolov0ffbfea2016-01-19 10:07:54 -0800307 * <p>This restriction has no effect on secondary users and managed profiles since only the
Amith Yamasani150514b2015-01-07 16:05:05 -0800308 * primary user can configure mobile networks.
Amith Yamasani26af8292014-09-09 09:57:27 -0700309 *
Fyodor Kupolov0ffbfea2016-01-19 10:07:54 -0800310 * <p>Key for user restrictions.
311 * <p>Type: Boolean
Makoto Onuki068c54a2015-10-13 14:34:03 -0700312 * @see DevicePolicyManager#addUserRestriction(ComponentName, String)
313 * @see DevicePolicyManager#clearUserRestriction(ComponentName, String)
Julia Reynoldsd46d0f92014-04-23 15:23:24 -0400314 * @see #getUserRestrictions()
315 */
316 public static final String DISALLOW_CONFIG_MOBILE_NETWORKS = "no_config_mobile_networks";
317
318 /**
Amith Yamasani26af8292014-09-09 09:57:27 -0700319 * Specifies if a user is disallowed from modifying
Julia Reynoldsc617f812014-07-25 16:32:27 -0400320 * applications in Settings or launchers. The following actions will not be allowed when this
321 * restriction is enabled:
322 * <li>uninstalling apps</li>
323 * <li>disabling apps</li>
324 * <li>clearing app caches</li>
325 * <li>clearing app data</li>
326 * <li>force stopping apps</li>
327 * <li>clearing app defaults</li>
328 * <p>
329 * The default value is <code>false</code>.
Amith Yamasani26af8292014-09-09 09:57:27 -0700330 *
Fyodor Kupolov0ffbfea2016-01-19 10:07:54 -0800331 * <p>Key for user restrictions.
332 * <p>Type: Boolean
Makoto Onuki068c54a2015-10-13 14:34:03 -0700333 * @see DevicePolicyManager#addUserRestriction(ComponentName, String)
334 * @see DevicePolicyManager#clearUserRestriction(ComponentName, String)
Julia Reynoldsd46d0f92014-04-23 15:23:24 -0400335 * @see #getUserRestrictions()
336 */
Julia Reynolds36fbc8d2014-06-18 09:26:30 -0400337 public static final String DISALLOW_APPS_CONTROL = "no_control_apps";
Julia Reynoldsd46d0f92014-04-23 15:23:24 -0400338
Emily Bernier394a6cd2014-05-07 12:49:20 -0400339 /**
Amith Yamasani26af8292014-09-09 09:57:27 -0700340 * Specifies if a user is disallowed from mounting
Amith Yamasanic34dc7c2014-09-18 09:42:42 -0700341 * physical external media. This can only be set by device owners and profile owners on the
342 * primary user. The default value is <code>false</code>.
Amith Yamasani26af8292014-09-09 09:57:27 -0700343 *
Fyodor Kupolov0ffbfea2016-01-19 10:07:54 -0800344 * <p>Key for user restrictions.
345 * <p>Type: Boolean
Makoto Onuki068c54a2015-10-13 14:34:03 -0700346 * @see DevicePolicyManager#addUserRestriction(ComponentName, String)
347 * @see DevicePolicyManager#clearUserRestriction(ComponentName, String)
Emily Bernier394a6cd2014-05-07 12:49:20 -0400348 * @see #getUserRestrictions()
349 */
350 public static final String DISALLOW_MOUNT_PHYSICAL_MEDIA = "no_physical_media";
351
352 /**
Amith Yamasani26af8292014-09-09 09:57:27 -0700353 * Specifies if a user is disallowed from adjusting microphone
Amith Yamasanic34dc7c2014-09-18 09:42:42 -0700354 * volume. If set, the microphone will be muted. This can only be set by device owners
355 * and profile owners on the primary user. The default value is <code>false</code>.
Amith Yamasani26af8292014-09-09 09:57:27 -0700356 *
Fyodor Kupolov0ffbfea2016-01-19 10:07:54 -0800357 * <p>Key for user restrictions.
358 * <p>Type: Boolean
Makoto Onuki068c54a2015-10-13 14:34:03 -0700359 * @see DevicePolicyManager#addUserRestriction(ComponentName, String)
360 * @see DevicePolicyManager#clearUserRestriction(ComponentName, String)
Emily Bernier394a6cd2014-05-07 12:49:20 -0400361 * @see #getUserRestrictions()
362 */
363 public static final String DISALLOW_UNMUTE_MICROPHONE = "no_unmute_microphone";
364
365 /**
Amith Yamasani26af8292014-09-09 09:57:27 -0700366 * Specifies if a user is disallowed from adjusting the master
Amith Yamasanic34dc7c2014-09-18 09:42:42 -0700367 * volume. If set, the master volume will be muted. This can only be set by device owners
368 * and profile owners on the primary user. The default value is <code>false</code>.
Amith Yamasani26af8292014-09-09 09:57:27 -0700369 *
Fyodor Kupolov0ffbfea2016-01-19 10:07:54 -0800370 * <p>Key for user restrictions.
371 * <p>Type: Boolean
Makoto Onuki068c54a2015-10-13 14:34:03 -0700372 * @see DevicePolicyManager#addUserRestriction(ComponentName, String)
373 * @see DevicePolicyManager#clearUserRestriction(ComponentName, String)
Emily Bernier394a6cd2014-05-07 12:49:20 -0400374 * @see #getUserRestrictions()
375 */
376 public static final String DISALLOW_ADJUST_VOLUME = "no_adjust_volume";
377
Amith Yamasani9f6c25f2014-05-16 14:49:15 -0700378 /**
Amith Yamasani26af8292014-09-09 09:57:27 -0700379 * Specifies that the user is not allowed to make outgoing
Amith Yamasani390989d2014-07-17 10:52:03 -0700380 * phone calls. Emergency calls are still permitted.
Amith Yamasani9f6c25f2014-05-16 14:49:15 -0700381 * The default value is <code>false</code>.
Tony Makeb83ab52016-02-22 18:36:08 +0000382 * <p>This restriction has no effect on managed profiles.
Amith Yamasani26af8292014-09-09 09:57:27 -0700383 *
Fyodor Kupolov0ffbfea2016-01-19 10:07:54 -0800384 * <p>Key for user restrictions.
385 * <p>Type: Boolean
Makoto Onuki068c54a2015-10-13 14:34:03 -0700386 * @see DevicePolicyManager#addUserRestriction(ComponentName, String)
387 * @see DevicePolicyManager#clearUserRestriction(ComponentName, String)
Amith Yamasani9f6c25f2014-05-16 14:49:15 -0700388 * @see #getUserRestrictions()
389 */
Amith Yamasani390989d2014-07-17 10:52:03 -0700390 public static final String DISALLOW_OUTGOING_CALLS = "no_outgoing_calls";
391
392 /**
Amith Yamasani26af8292014-09-09 09:57:27 -0700393 * Specifies that the user is not allowed to send or receive
Amith Yamasanic34dc7c2014-09-18 09:42:42 -0700394 * SMS messages. The default value is <code>false</code>.
Amith Yamasani26af8292014-09-09 09:57:27 -0700395 *
Fyodor Kupolov0ffbfea2016-01-19 10:07:54 -0800396 * <p>Key for user restrictions.
397 * <p>Type: Boolean
Makoto Onuki068c54a2015-10-13 14:34:03 -0700398 * @see DevicePolicyManager#addUserRestriction(ComponentName, String)
399 * @see DevicePolicyManager#clearUserRestriction(ComponentName, String)
Amith Yamasani390989d2014-07-17 10:52:03 -0700400 * @see #getUserRestrictions()
401 */
402 public static final String DISALLOW_SMS = "no_sms";
Amith Yamasani9f6c25f2014-05-16 14:49:15 -0700403
Jason Monk1c7c3192014-06-26 12:52:18 -0400404 /**
Jeff Sharkey2cc03e52015-03-20 11:24:04 -0700405 * Specifies if the user is not allowed to have fun. In some cases, the
406 * device owner may wish to prevent the user from experiencing amusement or
407 * joy while using the device. The default value is <code>false</code>.
408 *
Fyodor Kupolov0ffbfea2016-01-19 10:07:54 -0800409 * <p>Key for user restrictions.
410 * <p>Type: Boolean
Makoto Onuki068c54a2015-10-13 14:34:03 -0700411 * @see DevicePolicyManager#addUserRestriction(ComponentName, String)
412 * @see DevicePolicyManager#clearUserRestriction(ComponentName, String)
Jeff Sharkey2cc03e52015-03-20 11:24:04 -0700413 * @see #getUserRestrictions()
414 */
415 public static final String DISALLOW_FUN = "no_fun";
416
417 /**
Amith Yamasani26af8292014-09-09 09:57:27 -0700418 * Specifies that windows besides app windows should not be
Jason Monk1c7c3192014-06-26 12:52:18 -0400419 * created. This will block the creation of the following types of windows.
420 * <li>{@link LayoutParams#TYPE_TOAST}</li>
421 * <li>{@link LayoutParams#TYPE_PHONE}</li>
422 * <li>{@link LayoutParams#TYPE_PRIORITY_PHONE}</li>
423 * <li>{@link LayoutParams#TYPE_SYSTEM_ALERT}</li>
424 * <li>{@link LayoutParams#TYPE_SYSTEM_ERROR}</li>
425 * <li>{@link LayoutParams#TYPE_SYSTEM_OVERLAY}</li>
426 *
Amith Yamasanic34dc7c2014-09-18 09:42:42 -0700427 * <p>This can only be set by device owners and profile owners on the primary user.
428 * The default value is <code>false</code>.
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)
Jason Monk1c7c3192014-06-26 12:52:18 -0400434 * @see #getUserRestrictions()
435 */
436 public static final String DISALLOW_CREATE_WINDOWS = "no_create_windows";
437
Nicolas Prevotf1939902014-06-25 09:29:02 +0100438 /**
Amith Yamasani26af8292014-09-09 09:57:27 -0700439 * Specifies if what is copied in the clipboard of this profile can
Nicolas Prevotf1939902014-06-25 09:29:02 +0100440 * be pasted in related profiles. Does not restrict if the clipboard of related profiles can be
441 * pasted in this profile.
442 * The default value is <code>false</code>.
Amith Yamasani26af8292014-09-09 09:57:27 -0700443 *
Fyodor Kupolov0ffbfea2016-01-19 10:07:54 -0800444 * <p>Key for user restrictions.
445 * <p>Type: Boolean
Makoto Onuki068c54a2015-10-13 14:34:03 -0700446 * @see DevicePolicyManager#addUserRestriction(ComponentName, String)
447 * @see DevicePolicyManager#clearUserRestriction(ComponentName, String)
Nicolas Prevotf1939902014-06-25 09:29:02 +0100448 * @see #getUserRestrictions()
449 */
450 public static final String DISALLOW_CROSS_PROFILE_COPY_PASTE = "no_cross_profile_copy_paste";
451
Amith Yamasani26af8292014-09-09 09:57:27 -0700452 /**
453 * Specifies if the user is not allowed to use NFC to beam out data from apps.
454 * The default value is <code>false</code>.
455 *
Fyodor Kupolov0ffbfea2016-01-19 10:07:54 -0800456 * <p>Key for user restrictions.
457 * <p>Type: Boolean
Makoto Onuki068c54a2015-10-13 14:34:03 -0700458 * @see DevicePolicyManager#addUserRestriction(ComponentName, String)
459 * @see DevicePolicyManager#clearUserRestriction(ComponentName, String)
Amith Yamasani26af8292014-09-09 09:57:27 -0700460 * @see #getUserRestrictions()
461 */
462 public static final String DISALLOW_OUTGOING_BEAM = "no_outgoing_beam";
463
Sander Alewijnse53d63dc2014-11-07 21:43:00 +0000464 /**
Oleksandr Peletskyif2519812016-01-26 20:16:06 +0100465 * Hidden user restriction to disallow access to wallpaper manager APIs. This restriction
466 * generally means that wallpapers are not supported for the particular user. This user
467 * restriction is always set for managed profiles, because such profiles don't have wallpapers.
Benjamin Franzf3ece362015-02-11 10:51:10 +0000468 * @hide
Oleksandr Peletskyif2519812016-01-26 20:16:06 +0100469 * @see #DISALLOW_SET_WALLPAPER
Makoto Onuki068c54a2015-10-13 14:34:03 -0700470 * @see DevicePolicyManager#addUserRestriction(ComponentName, String)
471 * @see DevicePolicyManager#clearUserRestriction(ComponentName, String)
Benjamin Franzf3ece362015-02-11 10:51:10 +0000472 * @see #getUserRestrictions()
473 */
474 public static final String DISALLOW_WALLPAPER = "no_wallpaper";
475
476 /**
Oleksandr Peletskyif2519812016-01-26 20:16:06 +0100477 * User restriction to disallow setting a wallpaper. Profile owner and device owner
478 * are able to set wallpaper regardless of this restriction.
479 * The default value is <code>false</code>.
480 *
481 * <p>Key for user restrictions.
482 * <p>Type: Boolean
483 * @see DevicePolicyManager#addUserRestriction(ComponentName, String)
484 * @see DevicePolicyManager#clearUserRestriction(ComponentName, String)
485 * @see #getUserRestrictions()
486 */
487 public static final String DISALLOW_SET_WALLPAPER = "no_set_wallpaper";
488
489 /**
Benjamin Franzbff46ba2015-03-05 18:33:51 +0000490 * Specifies if the user is not allowed to reboot the device into safe boot mode.
491 * This can only be set by device owners and profile owners on the primary user.
492 * The default value is <code>false</code>.
493 *
Fyodor Kupolov0ffbfea2016-01-19 10:07:54 -0800494 * <p>Key for user restrictions.
495 * <p>Type: Boolean
Makoto Onuki068c54a2015-10-13 14:34:03 -0700496 * @see DevicePolicyManager#addUserRestriction(ComponentName, String)
497 * @see DevicePolicyManager#clearUserRestriction(ComponentName, String)
Benjamin Franzbff46ba2015-03-05 18:33:51 +0000498 * @see #getUserRestrictions()
499 */
500 public static final String DISALLOW_SAFE_BOOT = "no_safe_boot";
501
502 /**
Fyodor Kupolovb5013302015-04-17 17:59:14 -0700503 * Specifies if a user is not allowed to record audio. This restriction is always enabled for
504 * background users. The default value is <code>false</code>.
505 *
Makoto Onuki068c54a2015-10-13 14:34:03 -0700506 * @see DevicePolicyManager#addUserRestriction(ComponentName, String)
507 * @see DevicePolicyManager#clearUserRestriction(ComponentName, String)
Fyodor Kupolovb5013302015-04-17 17:59:14 -0700508 * @see #getUserRestrictions()
509 * @hide
510 */
511 public static final String DISALLOW_RECORD_AUDIO = "no_record_audio";
512
513 /**
Fyodor Kupolov9cbfc9e2015-10-07 15:52:33 -0700514 * Specifies if a user is not allowed to run in the background and should be stopped during
515 * user switch. The default value is <code>false</code>.
516 *
517 * <p>This restriction can be set by device owners and profile owners.
518 *
519 * @see DevicePolicyManager#addUserRestriction(ComponentName, String)
520 * @see DevicePolicyManager#clearUserRestriction(ComponentName, String)
521 * @see #getUserRestrictions()
522 * @hide
523 */
524 public static final String DISALLOW_RUN_IN_BACKGROUND = "no_run_in_background";
525
526 /**
Makoto Onuki759a7632015-10-28 16:43:10 -0700527 * Specifies if a user is not allowed to use the camera.
528 *
529 * @see DevicePolicyManager#addUserRestriction(ComponentName, String)
530 * @see DevicePolicyManager#clearUserRestriction(ComponentName, String)
531 * @see #getUserRestrictions()
532 * @hide
533 */
534 public static final String DISALLOW_CAMERA = "no_camera";
535
536 /**
Mahaver Chopradea471e2015-12-17 11:02:37 +0000537 * Specifies if a user is not allowed to use cellular data when roaming. This can only be set by
538 * device owners. The default value is <code>false</code>.
539 *
540 * @see DevicePolicyManager#addUserRestriction(ComponentName, String)
541 * @see DevicePolicyManager#clearUserRestriction(ComponentName, String)
542 * @see #getUserRestrictions()
543 */
544 public static final String DISALLOW_DATA_ROAMING = "no_data_roaming";
545
546 /**
Oleksandr Peletskyi7f1f1df2016-01-18 15:40:21 +0100547 * Specifies if a user is not allowed to change their icon. Device owner and profile owner
548 * can set this restriction. When it is set by device owner, only the target user will be
549 * affected. The default value is <code>false</code>.
550 *
551 * <p>Key for user restrictions.
Oleksandr Peletskyi7f1f1df2016-01-18 15:40:21 +0100552 * <p>Type: Boolean
Oleksandr Peletskyi7f1f1df2016-01-18 15:40:21 +0100553 * @see DevicePolicyManager#addUserRestriction(ComponentName, String)
554 * @see DevicePolicyManager#clearUserRestriction(ComponentName, String)
555 * @see #getUserRestrictions()
556 */
557 public static final String DISALLOW_SET_USER_ICON = "no_set_user_icon";
558
559 /**
Nicolas Prevotf0029c12015-06-25 14:25:41 -0700560 * Allows apps in the parent profile to handle web links from the managed profile.
561 *
Nicolas Prevot9edbda12015-06-17 11:09:48 -0700562 * This user restriction has an effect only in a managed profile.
563 * If set:
564 * Intent filters of activities in the parent profile with action
565 * {@link android.content.Intent#ACTION_VIEW},
566 * category {@link android.content.Intent#CATEGORY_BROWSABLE}, scheme http or https, and which
567 * define a host can handle intents from the managed profile.
568 * The default value is <code>false</code>.
569 *
Fyodor Kupolov0ffbfea2016-01-19 10:07:54 -0800570 * <p>Key for user restrictions.
571 * <p>Type: Boolean
Makoto Onuki068c54a2015-10-13 14:34:03 -0700572 * @see DevicePolicyManager#addUserRestriction(ComponentName, String)
573 * @see DevicePolicyManager#clearUserRestriction(ComponentName, String)
Nicolas Prevot9edbda12015-06-17 11:09:48 -0700574 * @see #getUserRestrictions()
575 */
Nicolas Prevotf0029c12015-06-25 14:25:41 -0700576 public static final String ALLOW_PARENT_PROFILE_APP_LINKING
577 = "allow_parent_profile_app_linking";
Nicolas Prevot9edbda12015-06-17 11:09:48 -0700578
579 /**
Sander Alewijnse53d63dc2014-11-07 21:43:00 +0000580 * Application restriction key that is used to indicate the pending arrival
581 * of real restrictions for the app.
582 *
583 * <p>
584 * Applications that support restrictions should check for the presence of this key.
585 * A <code>true</code> value indicates that restrictions may be applied in the near
586 * future but are not available yet. It is the responsibility of any
587 * management application that sets this flag to update it when the final
588 * restrictions are enforced.
589 *
Fyodor Kupolov0ffbfea2016-01-19 10:07:54 -0800590 * <p>Key for application restrictions.
591 * <p>Type: Boolean
Nicolas Prevotb14ed952015-01-13 14:23:53 +0000592 * @see android.app.admin.DevicePolicyManager#setApplicationRestrictions(
593 * android.content.ComponentName, String, Bundle)
594 * @see android.app.admin.DevicePolicyManager#getApplicationRestrictions(
595 * android.content.ComponentName, String)
Sander Alewijnse53d63dc2014-11-07 21:43:00 +0000596 */
597 public static final String KEY_RESTRICTIONS_PENDING = "restrictions_pending";
598
Amith Yamasani12747872015-12-07 14:19:49 -0800599 private static final String ACTION_CREATE_USER = "android.os.action.CREATE_USER";
600
601 /**
602 * Extra containing a name for the user being created. Optional parameter passed to
603 * ACTION_CREATE_USER activity.
604 * @hide
605 */
606 public static final String EXTRA_USER_NAME = "android.os.extra.USER_NAME";
607
608 /**
609 * Extra containing account name for the user being created. Optional parameter passed to
610 * ACTION_CREATE_USER activity.
611 * @hide
612 */
613 public static final String EXTRA_USER_ACCOUNT_NAME = "android.os.extra.USER_ACCOUNT_NAME";
614
615 /**
616 * Extra containing account type for the user being created. Optional parameter passed to
617 * ACTION_CREATE_USER activity.
618 * @hide
619 */
620 public static final String EXTRA_USER_ACCOUNT_TYPE = "android.os.extra.USER_ACCOUNT_TYPE";
621
622 /**
623 * Extra containing account-specific data for the user being created. Optional parameter passed
624 * to ACTION_CREATE_USER activity.
625 * @hide
626 */
627 public static final String EXTRA_USER_ACCOUNT_OPTIONS
628 = "android.os.extra.USER_ACCOUNT_OPTIONS";
629
Amith Yamasani655d0e22013-06-12 14:19:10 -0700630 /** @hide */
631 public static final int PIN_VERIFICATION_FAILED_INCORRECT = -3;
632 /** @hide */
633 public static final int PIN_VERIFICATION_FAILED_NOT_SET = -2;
634 /** @hide */
635 public static final int PIN_VERIFICATION_SUCCESS = -1;
636
Amith Yamasani37ed8d12016-01-27 14:40:16 -0800637 /**
638 * Error result indicating that this user is not allowed to add other users on this device.
639 * This is a result code returned from the activity created by the intent
640 * {@link #createUserCreationIntent(String, String, String, PersistableBundle)}.
641 */
642 public static final int USER_CREATION_FAILED_NOT_PERMITTED = Activity.RESULT_FIRST_USER;
643
644 /**
645 * Error result indicating that no more users can be created on this device.
646 * This is a result code returned from the activity created by the intent
647 * {@link #createUserCreationIntent(String, String, String, PersistableBundle)}.
648 */
649 public static final int USER_CREATION_FAILED_NO_MORE_USERS = Activity.RESULT_FIRST_USER + 1;
650
Amith Yamasani7e99bc02013-04-16 18:24:51 -0700651 /** @hide */
Amith Yamasanic0688302015-10-30 10:40:03 -0700652 public static UserManager get(Context context) {
653 return (UserManager) context.getSystemService(Context.USER_SERVICE);
Amith Yamasani27db4682013-03-30 17:07:47 -0700654 }
Maggie Benthalla12fccf2013-03-14 18:02:12 -0400655
Amith Yamasani258848d2012-08-10 17:06:33 -0700656 /** @hide */
657 public UserManager(Context context, IUserManager service) {
658 mService = service;
659 mContext = context;
660 }
661
662 /**
663 * Returns whether the system supports multiple users.
Kenny Guy1a447532014-02-20 21:55:32 +0000664 * @return true if multiple users can be created by user, false if it is a single user device.
Amith Yamasanie928d7d2012-09-17 21:46:51 -0700665 * @hide
Amith Yamasani258848d2012-08-10 17:06:33 -0700666 */
Jeff Sharkey4673e7e2012-09-19 13:14:30 -0700667 public static boolean supportsMultipleUsers() {
Kenny Guy1a447532014-02-20 21:55:32 +0000668 return getMaxSupportedUsers() > 1
669 && SystemProperties.getBoolean("fw.show_multiuserui",
670 Resources.getSystem().getBoolean(R.bool.config_enableMultiUserUI));
Amith Yamasani258848d2012-08-10 17:06:33 -0700671 }
672
Maggie Benthall67944582013-02-22 14:58:27 -0500673 /**
Xiaohui Chen7cb69df2015-07-13 16:01:01 -0700674 * @hide
675 * @return Whether the device is running with split system user. It means the system user and
676 * primary user are two separate users. Previously system user and primary user are combined as
677 * a single owner user. see @link {android.os.UserHandle#USER_OWNER}
678 */
679 public static boolean isSplitSystemUser() {
680 return SystemProperties.getBoolean("ro.fw.system_user_split", false);
681 }
682
683 /**
Amith Yamasani5760e172015-04-17 18:42:41 -0700684 * Returns the user handle for the user that this process is running under.
Jessica Hummelbe81c802014-04-22 15:49:22 +0100685 *
Amith Yamasani5760e172015-04-17 18:42:41 -0700686 * @return the user handle of this process.
Amith Yamasani258848d2012-08-10 17:06:33 -0700687 * @hide
Maggie Benthall67944582013-02-22 14:58:27 -0500688 */
Jeff Sharkey8588bc12016-01-06 16:47:42 -0700689 public @UserIdInt int getUserHandle() {
Dianne Hackborn79af1dd2012-08-16 16:42:52 -0700690 return UserHandle.myUserId();
Amith Yamasani258848d2012-08-10 17:06:33 -0700691 }
692
693 /**
Dianne Hackborn8832c182012-09-17 17:20:24 -0700694 * Returns the user name of the user making this call. This call is only
695 * available to applications on the system image; it requires the
696 * MANAGE_USERS permission.
Amith Yamasani258848d2012-08-10 17:06:33 -0700697 * @return the user name
698 */
699 public String getUserName() {
700 try {
701 return mService.getUserInfo(getUserHandle()).name;
702 } catch (RemoteException re) {
703 Log.w(TAG, "Could not get user name", re);
704 return "";
705 }
706 }
707
Dianne Hackborn67a101a2014-10-02 12:42:18 -0700708 /**
Dan Morrille4ab16a2012-09-20 20:25:55 -0700709 * Used to determine whether the user making this call is subject to
710 * teleportations.
Dianne Hackborn67a101a2014-10-02 12:42:18 -0700711 *
Dianne Hackborn955d8d62014-10-07 20:17:19 -0700712 * <p>As of {@link android.os.Build.VERSION_CODES#LOLLIPOP}, this method can
Dianne Hackborn67a101a2014-10-02 12:42:18 -0700713 * now automatically identify goats using advanced goat recognition technology.</p>
714 *
715 * @return Returns true if the user making this call is a goat.
Dan Morrille4ab16a2012-09-20 20:25:55 -0700716 */
717 public boolean isUserAGoat() {
Adam Powell988ae302014-09-17 17:58:33 -0700718 return mContext.getPackageManager()
719 .isPackageAvailable("com.coffeestainstudios.goatsimulator");
Dan Morrille4ab16a2012-09-20 20:25:55 -0700720 }
Amith Yamasanidf2e92a2013-03-01 17:04:38 -0800721
722 /**
Xiaohui Chen70f6c382015-04-28 14:21:43 -0700723 * Used to check if this process is running under the primary user. The primary user
724 * is the first human user on a device.
725 *
726 * @return whether this process is running under the primary user.
727 * @hide
728 */
729 public boolean isPrimaryUser() {
730 UserInfo user = getUserInfo(UserHandle.myUserId());
731 return user != null ? user.isPrimary() : false;
732 }
733
734 /**
Amith Yamasani5760e172015-04-17 18:42:41 -0700735 * Used to check if this process is running under the system user. The system user
736 * is the initial user that is implicitly created on first boot and hosts most of the
737 * system services.
738 *
739 * @return whether this process is running under the system user.
740 */
741 public boolean isSystemUser() {
Xiaohui Chen70f6c382015-04-28 14:21:43 -0700742 return UserHandle.myUserId() == UserHandle.USER_SYSTEM;
Amith Yamasani5760e172015-04-17 18:42:41 -0700743 }
Xiaohui Chen2b45f8a2015-08-04 15:12:23 -0700744
Amith Yamasani5760e172015-04-17 18:42:41 -0700745 /**
Amith Yamasani462ac3a2015-06-30 14:21:01 -0700746 * @hide
747 * Returns whether the caller is running as an admin user. There can be more than one admin
748 * user.
749 */
750 public boolean isAdminUser() {
Xiaohui Chen2b45f8a2015-08-04 15:12:23 -0700751 return isUserAdmin(UserHandle.myUserId());
752 }
753
754 /**
755 * @hide
756 * Returns whether the provided user is an admin user. There can be more than one admin
757 * user.
758 */
Jeff Sharkey8588bc12016-01-06 16:47:42 -0700759 public boolean isUserAdmin(@UserIdInt int userId) {
Xiaohui Chen2b45f8a2015-08-04 15:12:23 -0700760 UserInfo user = getUserInfo(userId);
761 return user != null && user.isAdmin();
Amith Yamasani462ac3a2015-06-30 14:21:01 -0700762 }
763
764 /**
Amith Yamasanie1375902013-04-13 16:48:35 -0700765 * 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 -0700766 * a reduced number of available apps, app restrictions and account restrictions.
Amith Yamasanie1375902013-04-13 16:48:35 -0700767 * @return whether the user making this call is a linked user
Amith Yamasani2555daf2013-04-25 13:39:27 -0700768 * @hide
Amith Yamasanidf2e92a2013-03-01 17:04:38 -0800769 */
Amith Yamasanie1375902013-04-13 16:48:35 -0700770 public boolean isLinkedUser() {
Amith Yamasanidf2e92a2013-03-01 17:04:38 -0800771 try {
Amith Yamasani71e6c692013-03-24 17:39:28 -0700772 return mService.isRestricted();
Amith Yamasanidf2e92a2013-03-01 17:04:38 -0800773 } catch (RemoteException re) {
Amith Yamasani46bc4eb2013-04-12 13:26:50 -0700774 Log.w(TAG, "Could not check if user is limited ", re);
Amith Yamasanidf2e92a2013-03-01 17:04:38 -0800775 return false;
776 }
777 }
778
Amith Yamasani258848d2012-08-10 17:06:33 -0700779 /**
Fyodor Kupolov1c363152015-09-02 13:27:21 -0700780 * Checks if specified user can have restricted profile.
781 * @hide
782 */
Jeff Sharkey8588bc12016-01-06 16:47:42 -0700783 public boolean canHaveRestrictedProfile(@UserIdInt int userId) {
Fyodor Kupolov1c363152015-09-02 13:27:21 -0700784 try {
785 return mService.canHaveRestrictedProfile(userId);
786 } catch (RemoteException re) {
787 Log.w(TAG, "Could not check if user can have restricted profile", re);
788 return false;
789 }
790 }
791
792 /**
Amith Yamasani1e9c2182014-06-11 17:25:51 -0700793 * Checks if the calling app is running as a guest user.
794 * @return whether the caller is a guest user.
795 * @hide
796 */
797 public boolean isGuestUser() {
798 UserInfo user = getUserInfo(UserHandle.myUserId());
799 return user != null ? user.isGuest() : false;
800 }
801
802 /**
Amith Yamasani0e8d7d62014-09-03 13:17:28 -0700803 * Checks if the calling app is running in a managed profile.
804 * Requires {@link android.Manifest.permission#MANAGE_USERS} permission.
805 *
806 * @return whether the caller is in a managed profile.
807 * @hide
808 */
809 @SystemApi
810 public boolean isManagedProfile() {
811 UserInfo user = getUserInfo(UserHandle.myUserId());
812 return user != null ? user.isManagedProfile() : false;
813 }
814
815 /**
Lenka Trochtovac4dd0212015-11-18 12:22:06 +0100816 * Checks if the calling app is running as an ephemeral user.
817 *
818 * @return whether the caller is an ephemeral user.
819 * @hide
820 */
821 public boolean isEphemeralUser() {
822 return isUserEphemeral(UserHandle.myUserId());
823 }
824
825 /**
826 * Returns whether the specified user is ephemeral.
827 * @hide
828 */
Jeff Sharkey8588bc12016-01-06 16:47:42 -0700829 public boolean isUserEphemeral(@UserIdInt int userId) {
Lenka Trochtovac4dd0212015-11-18 12:22:06 +0100830 final UserInfo user = getUserInfo(userId);
831 return user != null && user.isEphemeral();
832 }
833
834 /**
Dianne Hackborna8a9bd62012-10-09 15:36:59 -0700835 * Return whether the given user is actively running. This means that
836 * the user is in the "started" state, not "stopped" -- it is currently
837 * allowed to run code through scheduled alarms, receiving broadcasts,
838 * etc. A started user may be either the current foreground user or a
839 * background user; the result here does not distinguish between the two.
840 * @param user The user to retrieve the running state for.
841 */
842 public boolean isUserRunning(UserHandle user) {
Jeff Sharkey0e62384c2016-01-13 18:52:55 -0700843 return isUserRunning(user.getIdentifier());
844 }
845
846 /** {@hide} */
847 public boolean isUserRunning(int userId) {
Dianne Hackborna8a9bd62012-10-09 15:36:59 -0700848 try {
Jeff Sharkey0e62384c2016-01-13 18:52:55 -0700849 return ActivityManagerNative.getDefault().isUserRunning(userId, 0);
Dianne Hackborna8a9bd62012-10-09 15:36:59 -0700850 } catch (RemoteException e) {
851 return false;
852 }
853 }
854
855 /**
856 * Return whether the given user is actively running <em>or</em> stopping.
857 * This is like {@link #isUserRunning(UserHandle)}, but will also return
858 * true if the user had been running but is in the process of being stopped
859 * (but is not yet fully stopped, and still running some code).
860 * @param user The user to retrieve the running state for.
861 */
862 public boolean isUserRunningOrStopping(UserHandle user) {
863 try {
Jeff Sharkeye17ac152015-11-06 22:40:29 -0800864 // TODO: reconcile stopped vs stopping?
Dianne Hackborna8a9bd62012-10-09 15:36:59 -0700865 return ActivityManagerNative.getDefault().isUserRunning(
Jeff Sharkeye17ac152015-11-06 22:40:29 -0800866 user.getIdentifier(), ActivityManager.FLAG_OR_STOPPED);
Dianne Hackborna8a9bd62012-10-09 15:36:59 -0700867 } catch (RemoteException e) {
868 return false;
869 }
870 }
871
872 /**
Jeff Sharkeye13529a2015-12-09 14:15:27 -0700873 * Return whether the calling user is running in a "locked" state. A user is
874 * unlocked only after they've entered their credentials (such as a lock
875 * pattern or PIN), and credential-encrypted private app data storage is
876 * available.
877 */
Jeff Sharkeyb6423872015-12-11 11:11:58 -0700878 @Deprecated
Jeff Sharkeye13529a2015-12-09 14:15:27 -0700879 public boolean isUserRunningAndLocked() {
880 return isUserRunningAndLocked(Process.myUserHandle());
881 }
882
883 /**
Jeff Sharkey9d8a1042015-12-03 17:56:20 -0700884 * Return whether the given user is running in a "locked" state. A user
885 * is unlocked only after they've entered their credentials (such as a lock
886 * pattern or PIN), and credential-encrypted private app data storage is
887 * available.
888 *
889 * @param user to retrieve the unlocked state for.
890 */
Jeff Sharkeyb6423872015-12-11 11:11:58 -0700891 @Deprecated
Jeff Sharkey9d8a1042015-12-03 17:56:20 -0700892 public boolean isUserRunningAndLocked(UserHandle user) {
893 try {
894 return ActivityManagerNative.getDefault().isUserRunning(
895 user.getIdentifier(), ActivityManager.FLAG_AND_LOCKED);
896 } catch (RemoteException e) {
897 return false;
898 }
899 }
900
901 /**
Jeff Sharkeye13529a2015-12-09 14:15:27 -0700902 * Return whether the calling user is running in an "unlocked" state. A user
903 * is unlocked only after they've entered their credentials (such as a lock
904 * pattern or PIN), and credential-encrypted private app data storage is
905 * available.
906 */
Jeff Sharkeyb6423872015-12-11 11:11:58 -0700907 @Deprecated
Jeff Sharkeye13529a2015-12-09 14:15:27 -0700908 public boolean isUserRunningAndUnlocked() {
909 return isUserRunningAndUnlocked(Process.myUserHandle());
910 }
911
912 /**
Jeff Sharkey0825ab22015-12-02 13:04:49 -0700913 * Return whether the given user is running in an "unlocked" state. A user
914 * is unlocked only after they've entered their credentials (such as a lock
915 * pattern or PIN), and credential-encrypted private app data storage is
916 * available.
917 *
918 * @param user to retrieve the unlocked state for.
919 */
Jeff Sharkeyb6423872015-12-11 11:11:58 -0700920 @Deprecated
Jeff Sharkey9d8a1042015-12-03 17:56:20 -0700921 public boolean isUserRunningAndUnlocked(UserHandle user) {
Jeff Sharkey0825ab22015-12-02 13:04:49 -0700922 try {
923 return ActivityManagerNative.getDefault().isUserRunning(
924 user.getIdentifier(), ActivityManager.FLAG_AND_UNLOCKED);
925 } catch (RemoteException e) {
926 return false;
927 }
928 }
929
930 /**
Jeff Sharkeyb6423872015-12-11 11:11:58 -0700931 * Return whether the calling user is running in an "unlocked" state. A user
932 * is unlocked only after they've entered their credentials (such as a lock
933 * pattern or PIN), and credential-encrypted private app data storage is
934 * available.
935 */
936 public boolean isUserUnlocked() {
937 return isUserUnlocked(Process.myUserHandle());
938 }
939
940 /**
941 * Return whether the given user is running in an "unlocked" state. A user
942 * is unlocked only after they've entered their credentials (such as a lock
943 * pattern or PIN), and credential-encrypted private app data storage is
944 * available.
945 *
946 * @param user to retrieve the unlocked state for.
947 */
948 public boolean isUserUnlocked(UserHandle user) {
Jeff Sharkey0999c0d2015-12-17 15:12:22 -0700949 return isUserUnlocked(user.getIdentifier());
950 }
951
952 /** {@hide} */
Jeff Sharkey8588bc12016-01-06 16:47:42 -0700953 public boolean isUserUnlocked(@UserIdInt int userId) {
Jeff Sharkey5b0e5202015-12-18 17:18:09 -0700954 // TODO: eventually pivot this back to look at ActivityManager state,
955 // but there is race where we can start a non-encryption-aware launcher
956 // before that lifecycle has entered the running unlocked state.
957 return mContext.getSystemService(StorageManager.class).isUserKeyUnlocked(userId);
Jeff Sharkeyb6423872015-12-11 11:11:58 -0700958 }
959
960 /**
Amith Yamasani258848d2012-08-10 17:06:33 -0700961 * Returns the UserInfo object describing a specific user.
Amith Yamasani195263742012-08-21 15:40:12 -0700962 * Requires {@link android.Manifest.permission#MANAGE_USERS} permission.
Amith Yamasani258848d2012-08-10 17:06:33 -0700963 * @param userHandle the user handle of the user whose information is being requested.
964 * @return the UserInfo object for a specific user.
965 * @hide
Dianne Hackbornb26306a2012-10-24 15:22:21 -0700966 */
Jeff Sharkey8588bc12016-01-06 16:47:42 -0700967 public UserInfo getUserInfo(@UserIdInt int userHandle) {
Amith Yamasani258848d2012-08-10 17:06:33 -0700968 try {
969 return mService.getUserInfo(userHandle);
970 } catch (RemoteException re) {
971 Log.w(TAG, "Could not get user info", re);
972 return null;
973 }
974 }
975
Amith Yamasani71e6c692013-03-24 17:39:28 -0700976 /**
977 * Returns the user-wide restrictions imposed on this user.
978 * @return a Bundle containing all the restrictions.
979 */
Amith Yamasanie4cf7342012-12-17 11:12:09 -0800980 public Bundle getUserRestrictions() {
981 return getUserRestrictions(Process.myUserHandle());
982 }
983
Amith Yamasani71e6c692013-03-24 17:39:28 -0700984 /**
985 * Returns the user-wide restrictions imposed on the user specified by <code>userHandle</code>.
986 * @param userHandle the UserHandle of the user for whom to retrieve the restrictions.
987 * @return a Bundle containing all the restrictions.
988 */
Amith Yamasanie4cf7342012-12-17 11:12:09 -0800989 public Bundle getUserRestrictions(UserHandle userHandle) {
990 try {
991 return mService.getUserRestrictions(userHandle.getIdentifier());
992 } catch (RemoteException re) {
993 Log.w(TAG, "Could not get user restrictions", re);
994 return Bundle.EMPTY;
995 }
996 }
997
Zoltan Szatmary-Ban3bbcedd2015-11-26 13:45:51 +0000998 /**
999 * @hide
1000 * Returns whether the given user has been disallowed from performing certain actions
1001 * or setting certain settings through UserManager. This method disregards restrictions
1002 * set by device policy.
1003 * @param restrictionKey the string key representing the restriction
1004 * @param userHandle the UserHandle of the user for whom to retrieve the restrictions.
1005 */
1006 public boolean hasBaseUserRestriction(String restrictionKey, UserHandle userHandle) {
1007 try {
1008 return mService.hasBaseUserRestriction(restrictionKey, userHandle.getIdentifier());
1009 } catch (RemoteException re) {
1010 Log.w(TAG, "Could not get base user restrictions for user " +
1011 userHandle.getIdentifier(), re);
1012 return false;
1013 }
1014 }
1015
Amith Yamasani71e6c692013-03-24 17:39:28 -07001016 /**
Makoto Onukia3c12502015-10-28 10:18:32 -07001017 * This will no longer work. Device owners and profile owners should use
1018 * {@link DevicePolicyManager#addUserRestriction(ComponentName, String)} instead.
Amith Yamasani71e6c692013-03-24 17:39:28 -07001019 */
Makoto Onukia3c12502015-10-28 10:18:32 -07001020 // System apps should use UserManager.setUserRestriction() instead.
Julia Reynolds3d9eb782014-08-11 16:40:08 -04001021 @Deprecated
Amith Yamasanie4cf7342012-12-17 11:12:09 -08001022 public void setUserRestrictions(Bundle restrictions) {
Makoto Onuki068c54a2015-10-13 14:34:03 -07001023 throw new UnsupportedOperationException("This method is no longer supported");
Amith Yamasanie4cf7342012-12-17 11:12:09 -08001024 }
1025
Amith Yamasani71e6c692013-03-24 17:39:28 -07001026 /**
Makoto Onukia3c12502015-10-28 10:18:32 -07001027 * This will no longer work. Device owners and profile owners should use
1028 * {@link DevicePolicyManager#addUserRestriction(ComponentName, String)} instead.
Amith Yamasani71e6c692013-03-24 17:39:28 -07001029 */
Makoto Onukia3c12502015-10-28 10:18:32 -07001030 // System apps should use UserManager.setUserRestriction() instead.
Julia Reynolds3d9eb782014-08-11 16:40:08 -04001031 @Deprecated
Amith Yamasanie4cf7342012-12-17 11:12:09 -08001032 public void setUserRestrictions(Bundle restrictions, UserHandle userHandle) {
Makoto Onuki068c54a2015-10-13 14:34:03 -07001033 throw new UnsupportedOperationException("This method is no longer supported");
Amith Yamasanie4cf7342012-12-17 11:12:09 -08001034 }
1035
Amith Yamasani71e6c692013-03-24 17:39:28 -07001036 /**
1037 * Sets the value of a specific restriction.
Amith Yamasanibe465322014-04-24 13:45:17 -07001038 * Requires the MANAGE_USERS permission.
Amith Yamasani71e6c692013-03-24 17:39:28 -07001039 * @param key the key of the restriction
1040 * @param value the value for the restriction
Julia Reynolds3d9eb782014-08-11 16:40:08 -04001041 * @deprecated use {@link android.app.admin.DevicePolicyManager#addUserRestriction(
1042 * android.content.ComponentName, String)} or
1043 * {@link android.app.admin.DevicePolicyManager#clearUserRestriction(
1044 * android.content.ComponentName, String)} instead.
Amith Yamasani71e6c692013-03-24 17:39:28 -07001045 */
Julia Reynolds3d9eb782014-08-11 16:40:08 -04001046 @Deprecated
Amith Yamasani71e6c692013-03-24 17:39:28 -07001047 public void setUserRestriction(String key, boolean value) {
Makoto Onuki068c54a2015-10-13 14:34:03 -07001048 setUserRestriction(key, value, Process.myUserHandle());
Amith Yamasani71e6c692013-03-24 17:39:28 -07001049 }
1050
1051 /**
1052 * @hide
1053 * Sets the value of a specific restriction on a specific user.
Amith Yamasanibe465322014-04-24 13:45:17 -07001054 * Requires the MANAGE_USERS permission.
Amith Yamasani71e6c692013-03-24 17:39:28 -07001055 * @param key the key of the restriction
1056 * @param value the value for the restriction
1057 * @param userHandle the user whose restriction is to be changed.
Julia Reynolds3d9eb782014-08-11 16:40:08 -04001058 * @deprecated use {@link android.app.admin.DevicePolicyManager#addUserRestriction(
1059 * android.content.ComponentName, String)} or
1060 * {@link android.app.admin.DevicePolicyManager#clearUserRestriction(
1061 * android.content.ComponentName, String)} instead.
Amith Yamasani71e6c692013-03-24 17:39:28 -07001062 */
Julia Reynolds3d9eb782014-08-11 16:40:08 -04001063 @Deprecated
Maggie Benthall67944582013-02-22 14:58:27 -05001064 public void setUserRestriction(String key, boolean value, UserHandle userHandle) {
Fyodor Kupolovb5013302015-04-17 17:59:14 -07001065 try {
1066 mService.setUserRestriction(key, value, userHandle.getIdentifier());
1067 } catch (RemoteException re) {
1068 Log.w(TAG, "Could not set user restriction", re);
1069 }
Maggie Benthall67944582013-02-22 14:58:27 -05001070 }
1071
Amith Yamasani258848d2012-08-10 17:06:33 -07001072 /**
Maggie Benthalla12fccf2013-03-14 18:02:12 -04001073 * Returns whether the current user has been disallowed from performing certain actions
1074 * or setting certain settings.
Julia Reynolds2b2cf722014-06-06 11:41:04 -04001075 *
1076 * @param restrictionKey The string key representing the restriction.
1077 * @return {@code true} if the current user has the given restriction, {@code false} otherwise.
Maggie Benthalla12fccf2013-03-14 18:02:12 -04001078 */
1079 public boolean hasUserRestriction(String restrictionKey) {
David Christieb12ba932013-09-03 17:15:28 -07001080 return hasUserRestriction(restrictionKey, Process.myUserHandle());
1081 }
1082
1083 /**
1084 * @hide
1085 * Returns whether the given user has been disallowed from performing certain actions
1086 * or setting certain settings.
1087 * @param restrictionKey the string key representing the restriction
1088 * @param userHandle the UserHandle of the user for whom to retrieve the restrictions.
1089 */
1090 public boolean hasUserRestriction(String restrictionKey, UserHandle userHandle) {
Amith Yamasani8cd28b52014-06-08 17:54:27 -07001091 try {
1092 return mService.hasUserRestriction(restrictionKey,
1093 userHandle.getIdentifier());
1094 } catch (RemoteException re) {
1095 Log.w(TAG, "Could not check user restrictions", re);
1096 return false;
1097 }
Maggie Benthalla12fccf2013-03-14 18:02:12 -04001098 }
1099
1100 /**
Dianne Hackborn33f9cb82012-10-04 17:15:10 -07001101 * Return the serial number for a user. This is a device-unique
Dianne Hackbornb26306a2012-10-24 15:22:21 -07001102 * number assigned to that user; if the user is deleted and then a new
1103 * user created, the new users will not be given the same serial number.
Dianne Hackborn33f9cb82012-10-04 17:15:10 -07001104 * @param user The user whose serial number is to be retrieved.
Dianne Hackbornb26306a2012-10-24 15:22:21 -07001105 * @return The serial number of the given user; returns -1 if the
1106 * given UserHandle does not exist.
Dianne Hackborn33f9cb82012-10-04 17:15:10 -07001107 * @see #getUserForSerialNumber(long)
1108 */
1109 public long getSerialNumberForUser(UserHandle user) {
1110 return getUserSerialNumber(user.getIdentifier());
1111 }
1112
1113 /**
1114 * Return the user associated with a serial number previously
1115 * returned by {@link #getSerialNumberForUser(UserHandle)}.
1116 * @param serialNumber The serial number of the user that is being
1117 * retrieved.
1118 * @return Return the user associated with the serial number, or null
1119 * if there is not one.
1120 * @see #getSerialNumberForUser(UserHandle)
1121 */
1122 public UserHandle getUserForSerialNumber(long serialNumber) {
Fyodor Kupolovef249092015-05-06 13:18:46 -07001123 int ident = getUserHandle((int) serialNumber);
Dianne Hackborn33f9cb82012-10-04 17:15:10 -07001124 return ident >= 0 ? new UserHandle(ident) : null;
1125 }
1126
1127 /**
Xiaohui Chencfe64c82015-07-16 14:30:50 -07001128 * Creates a user with the specified name and options. For non-admin users, default user
1129 * restrictions are going to be applied.
Amith Yamasani195263742012-08-21 15:40:12 -07001130 * Requires {@link android.Manifest.permission#MANAGE_USERS} permission.
Amith Yamasani258848d2012-08-10 17:06:33 -07001131 *
1132 * @param name the user's name
1133 * @param flags flags that identify the type of user and other properties.
1134 * @see UserInfo
1135 *
1136 * @return the UserInfo object for the created user, or null if the user could not be created.
1137 * @hide
1138 */
1139 public UserInfo createUser(String name, int flags) {
Xiaohui Chencfe64c82015-07-16 14:30:50 -07001140 UserInfo user = null;
Amith Yamasani258848d2012-08-10 17:06:33 -07001141 try {
Xiaohui Chencfe64c82015-07-16 14:30:50 -07001142 user = mService.createUser(name, flags);
phweisse9c44062016-02-10 12:57:38 +01001143 // TODO: Keep this in sync with
1144 // UserManagerService.LocalService.createUserEvenWhenDisallowed
Xiaohui Chencfe64c82015-07-16 14:30:50 -07001145 if (user != null && !user.isAdmin()) {
Makoto Onuki068c54a2015-10-13 14:34:03 -07001146 mService.setUserRestriction(DISALLOW_SMS, true, user.id);
1147 mService.setUserRestriction(DISALLOW_OUTGOING_CALLS, true, user.id);
Xiaohui Chencfe64c82015-07-16 14:30:50 -07001148 }
Amith Yamasani258848d2012-08-10 17:06:33 -07001149 } catch (RemoteException re) {
1150 Log.w(TAG, "Could not create a user", re);
Amith Yamasani258848d2012-08-10 17:06:33 -07001151 }
Xiaohui Chencfe64c82015-07-16 14:30:50 -07001152 return user;
Amith Yamasani258848d2012-08-10 17:06:33 -07001153 }
1154
1155 /**
Amith Yamasani1e9c2182014-06-11 17:25:51 -07001156 * Creates a guest user and configures it.
1157 * @param context an application context
1158 * @param name the name to set for the user
1159 * @hide
1160 */
1161 public UserInfo createGuest(Context context, String name) {
Makoto Onuki068c54a2015-10-13 14:34:03 -07001162 UserInfo guest = null;
1163 try {
1164 guest = mService.createUser(name, UserInfo.FLAG_GUEST);
1165 if (guest != null) {
1166 Settings.Secure.putStringForUser(context.getContentResolver(),
1167 Settings.Secure.SKIP_FIRST_USE_HINTS, "1", guest.id);
Amith Yamasanibf3a9462014-07-28 14:26:42 -07001168 }
Makoto Onuki068c54a2015-10-13 14:34:03 -07001169 } catch (RemoteException re) {
1170 Log.w(TAG, "Could not create a user", re);
Amith Yamasani1e9c2182014-06-11 17:25:51 -07001171 }
1172 return guest;
1173 }
1174
Amith Yamasaniaa6634e2014-10-06 14:20:28 -07001175 /**
Kenny Guy2a764942014-04-02 13:29:20 +01001176 * Creates a user with the specified name and options as a profile of another user.
Kenny Guya52dc3e2014-02-11 15:33:14 +00001177 * Requires {@link android.Manifest.permission#MANAGE_USERS} permission.
1178 *
1179 * @param name the user's name
1180 * @param flags flags that identify the type of user and other properties.
1181 * @see UserInfo
Kenny Guy2a764942014-04-02 13:29:20 +01001182 * @param userHandle new user will be a profile of this use.
Kenny Guya52dc3e2014-02-11 15:33:14 +00001183 *
1184 * @return the UserInfo object for the created user, or null if the user could not be created.
1185 * @hide
1186 */
Jeff Sharkey8588bc12016-01-06 16:47:42 -07001187 public UserInfo createProfileForUser(String name, int flags, @UserIdInt int userHandle) {
Kenny Guya52dc3e2014-02-11 15:33:14 +00001188 try {
Kenny Guy2a764942014-04-02 13:29:20 +01001189 return mService.createProfileForUser(name, flags, userHandle);
Kenny Guya52dc3e2014-02-11 15:33:14 +00001190 } catch (RemoteException re) {
1191 Log.w(TAG, "Could not create a user", re);
1192 return null;
1193 }
1194 }
1195
1196 /**
Fyodor Kupolov02cb6e72015-09-18 18:20:55 -07001197 * Creates a restricted profile with the specified name. This method also sets necessary
1198 * restrictions and adds shared accounts.
Fyodor Kupolov06a484a2015-08-21 16:33:20 -07001199 *
1200 * @param name profile's name
1201 * @return UserInfo object for the created user, or null if the user could not be created.
1202 * @hide
1203 */
1204 public UserInfo createRestrictedProfile(String name) {
1205 try {
Fyodor Kupolov02cb6e72015-09-18 18:20:55 -07001206 UserHandle parentUserHandle = Process.myUserHandle();
1207 UserInfo user = mService.createRestrictedProfile(name,
1208 parentUserHandle.getIdentifier());
1209 if (user != null) {
1210 AccountManager.get(mContext).addSharedAccountsFromParentUser(parentUserHandle,
1211 UserHandle.of(user.id));
Fyodor Kupolov06a484a2015-08-21 16:33:20 -07001212 }
Fyodor Kupolov02cb6e72015-09-18 18:20:55 -07001213 return user;
Fyodor Kupolov06a484a2015-08-21 16:33:20 -07001214 } catch (RemoteException e) {
1215 Log.w(TAG, "Could not create a restricted profile", e);
1216 }
1217 return null;
1218 }
1219
1220 /**
Amith Yamasani12747872015-12-07 14:19:49 -08001221 * Returns an intent to create a user for the provided name and email address. The name
1222 * and email address will be used when the setup process for the new user is started.
1223 * If this device does not support multiple users, null is returned.
1224 * <p/>
1225 * The intent should be launched using startActivityForResult and the return result will
Amith Yamasani37ed8d12016-01-27 14:40:16 -08001226 * indicate if the user consented to adding a new user and if the operation succeeded. Any
1227 * errors in creating the user will be returned in the result code. If the user cancels the
1228 * request, the return result will be {@link Activity#RESULT_CANCELED}. On success, the
1229 * result code will be {@link Activity#RESULT_OK}.
Amith Yamasani12747872015-12-07 14:19:49 -08001230 * <p/>
1231 * The new user is created but not initialized. After switching into the user for the first
1232 * time, the preferred user name and account information are used by the setup process for that
1233 * user.
1234 *
1235 * @param userName Optional name to assign to the user.
1236 * @param accountName Optional email address that will be used by the setup wizard to initialize
1237 * the user.
1238 * @param accountType Optional account type for the account to be created. This is required
1239 * if the account name is specified.
1240 * @param accountOptions Optional bundle of data to be passed in during account creation in the
1241 * new user via {@link AccountManager#addAccount(String, String, String[],
1242 * Bundle, android.app.Activity, android.accounts.AccountManagerCallback,
1243 * Handler)}.
1244 * @return An Intent that can be launched from an Activity or null if creating users is not
1245 * supported on this device.
Amith Yamasani37ed8d12016-01-27 14:40:16 -08001246 * @see #USER_CREATION_FAILED_NOT_PERMITTED
1247 * @see #USER_CREATION_FAILED_NO_MORE_USERS
Amith Yamasani12747872015-12-07 14:19:49 -08001248 */
1249 public static Intent createUserCreationIntent(@Nullable String userName,
1250 @Nullable String accountName,
1251 @Nullable String accountType, @Nullable PersistableBundle accountOptions) {
1252 if (!supportsMultipleUsers() || getMaxSupportedUsers() < 2) {
1253 return null;
1254 }
1255 Intent intent = new Intent(ACTION_CREATE_USER);
1256 if (userName != null) {
1257 intent.putExtra(EXTRA_USER_NAME, userName);
1258 }
1259 if (accountName != null && accountType == null) {
1260 throw new IllegalArgumentException("accountType must be specified if accountName is "
1261 + "specified");
1262 }
1263 if (accountName != null) {
1264 intent.putExtra(EXTRA_USER_ACCOUNT_NAME, accountName);
1265 }
1266 if (accountType != null) {
1267 intent.putExtra(EXTRA_USER_ACCOUNT_TYPE, accountType);
1268 }
1269 if (accountOptions != null) {
1270 intent.putExtra(EXTRA_USER_ACCOUNT_OPTIONS, accountOptions);
1271 }
1272 return intent;
1273 }
1274
1275 /**
1276 * @hide
1277 *
1278 * Returns the preferred account name for user creation. Requires MANAGE_USERS permission.
1279 */
1280 @SystemApi
1281 public String getSeedAccountName() {
1282 try {
1283 return mService.getSeedAccountName();
1284 } catch (RemoteException re) {
1285 Log.w(TAG, "Could not get the seed account name", re);
1286 return null;
1287 }
1288 }
1289
1290 /**
1291 * @hide
1292 *
1293 * Returns the preferred account type for user creation. Requires MANAGE_USERS permission.
1294 */
1295 @SystemApi
1296 public String getSeedAccountType() {
1297 try {
1298 return mService.getSeedAccountType();
1299 } catch (RemoteException re) {
1300 Log.w(TAG, "Could not get the seed account type", re);
1301 return null;
1302 }
1303 }
1304
1305 /**
1306 * @hide
1307 *
1308 * Returns the preferred account's options bundle for user creation. Requires MANAGE_USERS
1309 * permission.
1310 * @return Any options set by the requestor that created the user.
1311 */
1312 @SystemApi
1313 public PersistableBundle getSeedAccountOptions() {
1314 try {
1315 return mService.getSeedAccountOptions();
1316 } catch (RemoteException re) {
1317 Log.w(TAG, "Could not get the seed account options", re);
1318 return null;
1319 }
1320 }
1321
1322 /**
1323 * @hide
1324 *
1325 * Called by a system activity to set the seed account information of a user created
1326 * through the user creation intent.
1327 * @param userId
1328 * @param accountName
1329 * @param accountType
1330 * @param accountOptions
1331 * @see #createUserCreationIntent(String, String, String, PersistableBundle)
1332 */
1333 public void setSeedAccountData(int userId, String accountName, String accountType,
1334 PersistableBundle accountOptions) {
1335 try {
1336 mService.setSeedAccountData(userId, accountName, accountType, accountOptions,
1337 /* persist= */ true);
1338 } catch (RemoteException re) {
1339 Log.w(TAG, "Could not set the seed account data", re);
1340 }
1341 }
1342
1343 /**
1344 * @hide
1345 * Clears the seed information used to create this user. Requires MANAGE_USERS permission.
1346 */
1347 @SystemApi
1348 public void clearSeedAccountData() {
1349 try {
1350 mService.clearSeedAccountData();
1351 } catch (RemoteException re) {
1352 Log.w(TAG, "Could not clear the seed account data", re);
1353 }
1354 }
1355
1356 /**
Amith Yamasani1df14732014-08-29 21:37:27 -07001357 * @hide
1358 * Marks the guest user for deletion to allow a new guest to be created before deleting
1359 * the current user who is a guest.
1360 * @param userHandle
1361 * @return
1362 */
Jeff Sharkey8588bc12016-01-06 16:47:42 -07001363 public boolean markGuestForDeletion(@UserIdInt int userHandle) {
Amith Yamasani1df14732014-08-29 21:37:27 -07001364 try {
1365 return mService.markGuestForDeletion(userHandle);
1366 } catch (RemoteException re) {
1367 Log.w(TAG, "Could not mark guest for deletion", re);
1368 return false;
1369 }
1370 }
1371
1372 /**
Alexandra Gherghinadf35d572014-04-09 13:54:39 +01001373 * Sets the user as enabled, if such an user exists.
Lenka Trochtova1ddda472016-02-12 10:42:12 +01001374 *
1375 * <p>Requires {@link android.Manifest.permission#MANAGE_USERS} permission.
1376 *
1377 * <p>Note that the default is true, it's only that managed profiles might not be enabled.
1378 * Also ephemeral users can be disabled to indicate that their removal is in progress and they
1379 * shouldn't be re-entered. Therefore ephemeral users should not be re-enabled once disabled.
Alexandra Gherghinadf35d572014-04-09 13:54:39 +01001380 *
1381 * @param userHandle the id of the profile to enable
1382 * @hide
1383 */
Jeff Sharkey8588bc12016-01-06 16:47:42 -07001384 public void setUserEnabled(@UserIdInt int userHandle) {
Alexandra Gherghinadf35d572014-04-09 13:54:39 +01001385 try {
1386 mService.setUserEnabled(userHandle);
1387 } catch (RemoteException e) {
1388 Log.w(TAG, "Could not enable the profile", e);
1389 }
1390 }
1391
1392 /**
Dianne Hackbornb26306a2012-10-24 15:22:21 -07001393 * Return the number of users currently created on the device.
1394 */
1395 public int getUserCount() {
1396 List<UserInfo> users = getUsers();
1397 return users != null ? users.size() : 1;
1398 }
1399
1400 /**
Amith Yamasani258848d2012-08-10 17:06:33 -07001401 * Returns information for all users on this device.
Amith Yamasani195263742012-08-21 15:40:12 -07001402 * Requires {@link android.Manifest.permission#MANAGE_USERS} permission.
Fyodor Kupolov940e8572016-01-26 12:03:51 -08001403 * @return the list of users that exist on the device.
Amith Yamasani258848d2012-08-10 17:06:33 -07001404 * @hide
1405 */
1406 public List<UserInfo> getUsers() {
1407 try {
Amith Yamasani920ace02012-09-20 22:15:37 -07001408 return mService.getUsers(false);
1409 } catch (RemoteException re) {
1410 Log.w(TAG, "Could not get user list", re);
1411 return null;
1412 }
1413 }
1414
1415 /**
Fyodor Kupolov940e8572016-01-26 12:03:51 -08001416 * Returns serial numbers of all users on this device.
1417 * Requires {@link android.Manifest.permission#MANAGE_USERS} permission.
1418 *
1419 * @param excludeDying specify if the list should exclude users being removed.
1420 * @return the list of serial numbers of users that exist on the device.
1421 * @hide
1422 */
1423 @SystemApi
1424 public long[] getSerialNumbersOfUsers(boolean excludeDying) {
1425 try {
1426 List<UserInfo> users = mService.getUsers(excludeDying);
1427 long[] result = new long[users.size()];
1428 for (int i = 0; i < result.length; i++) {
1429 result[i] = users.get(i).serialNumber;
1430 }
1431 return result;
1432 } catch (RemoteException re) {
1433 Log.w(TAG, "Could not get users list", re);
1434 return null;
1435 }
1436 }
1437
1438 /**
Xiaohui Chenb3b92582015-12-07 11:22:13 -08001439 * @return the user's account name, null if not found.
1440 * @hide
1441 */
1442 @RequiresPermission( allOf = {
1443 Manifest.permission.INTERACT_ACROSS_USERS_FULL,
1444 Manifest.permission.MANAGE_USERS
1445 })
Jeff Sharkey8588bc12016-01-06 16:47:42 -07001446 public @Nullable String getUserAccount(@UserIdInt int userHandle) {
Xiaohui Chenb3b92582015-12-07 11:22:13 -08001447 try {
1448 return mService.getUserAccount(userHandle);
1449 } catch (RemoteException re) {
1450 Log.w(TAG, "Could not get user account", re);
1451 return null;
1452 }
1453 }
1454
1455 /**
1456 * Set account name for the given user.
1457 * @hide
1458 */
1459 @RequiresPermission( allOf = {
1460 Manifest.permission.INTERACT_ACROSS_USERS_FULL,
1461 Manifest.permission.MANAGE_USERS
1462 })
Jeff Sharkey8588bc12016-01-06 16:47:42 -07001463 public void setUserAccount(@UserIdInt int userHandle, @Nullable String accountName) {
Xiaohui Chenb3b92582015-12-07 11:22:13 -08001464 try {
1465 mService.setUserAccount(userHandle, accountName);
1466 } catch (RemoteException re) {
1467 Log.w(TAG, "Could not set user account", re);
1468 }
1469 }
1470
1471 /**
Xiaohui Chen70f6c382015-04-28 14:21:43 -07001472 * Returns information for Primary user.
1473 * Requires {@link android.Manifest.permission#MANAGE_USERS} permission.
1474 *
1475 * @return the Primary user, null if not found.
1476 * @hide
1477 */
Xiaohui Chen7cb69df2015-07-13 16:01:01 -07001478 public @Nullable UserInfo getPrimaryUser() {
Xiaohui Chen70f6c382015-04-28 14:21:43 -07001479 try {
1480 return mService.getPrimaryUser();
1481 } catch (RemoteException re) {
1482 Log.w(TAG, "Could not get Primary user", re);
1483 return null;
1484 }
1485 }
1486
1487 /**
Amith Yamasani95ab7842014-08-11 17:09:26 -07001488 * Checks whether it's possible to add more users. Caller must hold the MANAGE_USERS
1489 * permission.
1490 *
1491 * @return true if more users can be added, false if limit has been reached.
1492 * @hide
1493 */
1494 public boolean canAddMoreUsers() {
1495 final List<UserInfo> users = getUsers(true);
1496 final int totalUserCount = users.size();
1497 int aliveUserCount = 0;
1498 for (int i = 0; i < totalUserCount; i++) {
1499 UserInfo user = users.get(i);
1500 if (!user.isGuest()) {
1501 aliveUserCount++;
1502 }
1503 }
1504 return aliveUserCount < getMaxSupportedUsers();
1505 }
1506
1507 /**
Nicolas Prevot72434b72015-05-13 12:15:03 -07001508 * Checks whether it's possible to add more managed profiles. Caller must hold the MANAGE_USERS
1509 * permission.
Nicolas Prevot07387fe2015-10-30 17:53:30 +00001510 * if allowedToRemoveOne is true and if the user already has a managed profile, then return if
1511 * we could add a new managed profile to this user after removing the existing one.
Nicolas Prevot72434b72015-05-13 12:15:03 -07001512 *
1513 * @return true if more managed profiles can be added, false if limit has been reached.
1514 * @hide
1515 */
Jeff Sharkey8588bc12016-01-06 16:47:42 -07001516 public boolean canAddMoreManagedProfiles(@UserIdInt int userId, boolean allowedToRemoveOne) {
Nicolas Prevot72434b72015-05-13 12:15:03 -07001517 try {
Nicolas Prevot07387fe2015-10-30 17:53:30 +00001518 return mService.canAddMoreManagedProfiles(userId, allowedToRemoveOne);
Nicolas Prevot72434b72015-05-13 12:15:03 -07001519 } catch (RemoteException re) {
1520 Log.w(TAG, "Could not check if we can add more managed profiles", re);
1521 return false;
1522 }
1523 }
1524
1525 /**
Kenny Guy2a764942014-04-02 13:29:20 +01001526 * Returns list of the profiles of userHandle including
1527 * userHandle itself.
Amith Yamasani4f7e2e32014-08-14 18:49:48 -07001528 * Note that this returns both enabled and not enabled profiles. See
Ruben Brunk7f75da22015-04-30 17:46:30 -07001529 * {@link #getEnabledProfiles(int)} if you need only the enabled ones.
Amith Yamasani4f582632014-02-19 14:31:52 -08001530 *
Kenny Guy2a764942014-04-02 13:29:20 +01001531 * Requires {@link android.Manifest.permission#MANAGE_USERS} permission.
1532 * @param userHandle profiles of this user will be returned.
1533 * @return the list of profiles.
1534 * @hide
1535 */
Jeff Sharkey8588bc12016-01-06 16:47:42 -07001536 public List<UserInfo> getProfiles(@UserIdInt int userHandle) {
Kenny Guya52dc3e2014-02-11 15:33:14 +00001537 try {
Alexandra Gherghina385124d2014-04-03 13:37:39 +01001538 return mService.getProfiles(userHandle, false /* enabledOnly */);
Kenny Guya52dc3e2014-02-11 15:33:14 +00001539 } catch (RemoteException re) {
1540 Log.w(TAG, "Could not get user list", re);
1541 return null;
1542 }
1543 }
1544
1545 /**
Xiaohui Chenfd5b7742015-10-14 15:47:04 -07001546 * Requires {@link android.Manifest.permission#MANAGE_USERS} permission.
1547 * @param userId one of the two user ids to check.
1548 * @param otherUserId one of the two user ids to check.
1549 * @return true if the two user ids are in the same profile group.
1550 * @hide
1551 */
Jeff Sharkey8588bc12016-01-06 16:47:42 -07001552 public boolean isSameProfileGroup(@UserIdInt int userId, int otherUserId) {
Xiaohui Chenfd5b7742015-10-14 15:47:04 -07001553 try {
1554 return mService.isSameProfileGroup(userId, otherUserId);
1555 } catch (RemoteException re) {
1556 Log.w(TAG, "Could not get user list", re);
1557 return false;
1558 }
1559 }
1560
1561 /**
Ruben Brunk7f75da22015-04-30 17:46:30 -07001562 * Returns list of the profiles of userHandle including
1563 * userHandle itself.
1564 * Note that this returns only enabled.
1565 *
1566 * Requires {@link android.Manifest.permission#MANAGE_USERS} permission.
1567 * @param userHandle profiles of this user will be returned.
1568 * @return the list of profiles.
1569 * @hide
1570 */
Jeff Sharkey8588bc12016-01-06 16:47:42 -07001571 public List<UserInfo> getEnabledProfiles(@UserIdInt int userHandle) {
Ruben Brunk7f75da22015-04-30 17:46:30 -07001572 try {
1573 return mService.getProfiles(userHandle, true /* enabledOnly */);
1574 } catch (RemoteException re) {
1575 Log.w(TAG, "Could not get user list", re);
1576 return null;
1577 }
1578 }
1579
1580 /**
Jessica Hummelbe81c802014-04-22 15:49:22 +01001581 * Returns a list of UserHandles for profiles associated with the user that the calling process
1582 * is running on, including the user itself.
Amith Yamasani4f582632014-02-19 14:31:52 -08001583 *
1584 * @return A non-empty list of UserHandles associated with the calling user.
1585 */
1586 public List<UserHandle> getUserProfiles() {
1587 ArrayList<UserHandle> profiles = new ArrayList<UserHandle>();
Xiaohui Chen2b45f8a2015-08-04 15:12:23 -07001588 List<UserInfo> users;
Alexandra Gherghina385124d2014-04-03 13:37:39 +01001589 try {
Alexandra Gherghinaf2e7b3f2014-04-30 17:01:03 +00001590 users = mService.getProfiles(UserHandle.myUserId(), true /* enabledOnly */);
Alexandra Gherghina385124d2014-04-03 13:37:39 +01001591 } catch (RemoteException re) {
1592 Log.w(TAG, "Could not get user list", re);
1593 return null;
1594 }
Amith Yamasani4f582632014-02-19 14:31:52 -08001595 for (UserInfo info : users) {
1596 UserHandle userHandle = new UserHandle(info.id);
1597 profiles.add(userHandle);
1598 }
1599 return profiles;
1600 }
1601
Amith Yamasani7dda2652014-04-11 14:57:12 -07001602 /**
Andres Moralesc5548c02015-08-05 10:23:12 -07001603 * Returns the device credential owner id of the profile from
1604 * which this method is called, or userHandle if called from a user that
1605 * is not a profile.
1606 *
1607 * @hide
1608 */
Jeff Sharkey8588bc12016-01-06 16:47:42 -07001609 public int getCredentialOwnerProfile(@UserIdInt int userHandle) {
Andres Moralesc5548c02015-08-05 10:23:12 -07001610 try {
1611 return mService.getCredentialOwnerProfile(userHandle);
1612 } catch (RemoteException re) {
1613 Log.w(TAG, "Could not get credential owner", re);
1614 return -1;
1615 }
1616 }
1617
1618 /**
Jessica Hummelbe81c802014-04-22 15:49:22 +01001619 * Returns the parent of the profile which this method is called from
1620 * or null if called from a user that is not a profile.
1621 *
1622 * @hide
1623 */
Jeff Sharkey8588bc12016-01-06 16:47:42 -07001624 public UserInfo getProfileParent(@UserIdInt int userHandle) {
Jessica Hummelbe81c802014-04-22 15:49:22 +01001625 try {
1626 return mService.getProfileParent(userHandle);
1627 } catch (RemoteException re) {
1628 Log.w(TAG, "Could not get profile parent", re);
1629 return null;
1630 }
1631 }
1632
1633 /**
Rubin Xu0a29ecd2015-11-04 15:11:48 +00001634 * Set quiet mode of a managed profile.
1635 *
1636 * @param userHandle The user handle of the profile.
1637 * @param enableQuietMode Whether quiet mode should be enabled or disabled.
1638 * @hide
1639 */
Jeff Sharkey8588bc12016-01-06 16:47:42 -07001640 public void setQuietModeEnabled(@UserIdInt int userHandle, boolean enableQuietMode) {
Rubin Xu0a29ecd2015-11-04 15:11:48 +00001641 try {
1642 mService.setQuietModeEnabled(userHandle, enableQuietMode);
1643 } catch (RemoteException e) {
1644 Log.w(TAG, "Could not change the profile's quiet mode", e);
1645 }
1646 }
1647
1648 /**
1649 * Returns whether the given profile is in quiet mode or not.
1650 *
1651 * @param userHandle The user handle of the profile to be queried.
1652 * @return true if the profile is in quiet mode, false otherwise.
1653 */
1654 public boolean isQuietModeEnabled(UserHandle userHandle) {
1655 try {
1656 return mService.isQuietModeEnabled(userHandle.getIdentifier());
1657 } catch (RemoteException e) {
1658 Log.w(TAG, "Could not query the profile's quiet mode", e);
1659 }
1660 return false;
1661 }
1662
1663 /**
Amith Yamasani7dda2652014-04-11 14:57:12 -07001664 * If the target user is a managed profile of the calling user or the caller
1665 * is itself a managed profile, then this returns a badged copy of the given
Svetoslavc71c42f2014-08-05 18:57:05 -07001666 * icon to be able to distinguish it from the original icon. For badging an
1667 * arbitrary drawable use {@link #getBadgedDrawableForUser(
1668 * android.graphics.drawable.Drawable, UserHandle, android.graphics.Rect, int)}.
1669 * <p>
1670 * If the original drawable is a BitmapDrawable and the backing bitmap is
1671 * mutable as per {@link android.graphics.Bitmap#isMutable()}, the bading
1672 * is performed in place and the original drawable is returned.
1673 * </p>
Amith Yamasani7dda2652014-04-11 14:57:12 -07001674 *
1675 * @param icon The icon to badge.
1676 * @param user The target user.
1677 * @return A drawable that combines the original icon and a badge as
1678 * determined by the system.
Svetoslavc7d62f02014-09-04 15:39:54 -07001679 * @removed
Amith Yamasani7dda2652014-04-11 14:57:12 -07001680 */
Svetoslavc71c42f2014-08-05 18:57:05 -07001681 public Drawable getBadgedIconForUser(Drawable icon, UserHandle user) {
Svetoslavc7d62f02014-09-04 15:39:54 -07001682 return mContext.getPackageManager().getUserBadgedIcon(icon, user);
Amith Yamasani4f582632014-02-19 14:31:52 -08001683 }
1684
Kenny Guy701ea7c2014-05-08 23:34:12 +01001685 /**
1686 * If the target user is a managed profile of the calling user or the caller
Svetoslavc71c42f2014-08-05 18:57:05 -07001687 * is itself a managed profile, then this returns a badged copy of the given
1688 * drawable allowing the user to distinguish it from the original drawable.
1689 * The caller can specify the location in the bounds of the drawable to be
1690 * badged where the badge should be applied as well as the density of the
1691 * badge to be used.
1692 * <p>
1693 * If the original drawable is a BitmapDrawable and the backing bitmap is
1694 * mutable as per {@link android.graphics.Bitmap#isMutable()}, the bading
1695 * is performed in place and the original drawable is returned.
1696 * </p>
1697 *
1698 * @param badgedDrawable The drawable to badge.
1699 * @param user The target user.
1700 * @param badgeLocation Where in the bounds of the badged drawable to place
1701 * the badge. If not provided, the badge is applied on top of the entire
1702 * drawable being badged.
1703 * @param badgeDensity The optional desired density for the badge as per
1704 * {@link android.util.DisplayMetrics#densityDpi}. If not provided,
1705 * the density of the display is used.
1706 * @return A drawable that combines the original drawable and a badge as
1707 * determined by the system.
Svetoslavc7d62f02014-09-04 15:39:54 -07001708 * @removed
Svetoslavc71c42f2014-08-05 18:57:05 -07001709 */
1710 public Drawable getBadgedDrawableForUser(Drawable badgedDrawable, UserHandle user,
1711 Rect badgeLocation, int badgeDensity) {
Svetoslavc7d62f02014-09-04 15:39:54 -07001712 return mContext.getPackageManager().getUserBadgedDrawableForDensity(badgedDrawable, user,
1713 badgeLocation, badgeDensity);
Svetoslavc71c42f2014-08-05 18:57:05 -07001714 }
1715
1716 /**
1717 * If the target user is a managed profile of the calling user or the caller
Kenny Guyf7ecf7c2014-06-18 11:32:05 +01001718 * is itself a managed profile, then this returns a copy of the label with
1719 * badging for accessibility services like talkback. E.g. passing in "Email"
1720 * and it might return "Work Email" for Email in the work profile.
1721 *
1722 * @param label The label to change.
1723 * @param user The target user.
1724 * @return A label that combines the original label and a badge as
1725 * determined by the system.
Svetoslavc7d62f02014-09-04 15:39:54 -07001726 * @removed
Kenny Guyf7ecf7c2014-06-18 11:32:05 +01001727 */
Kenny Guy237aecd2014-07-21 14:06:09 +01001728 public CharSequence getBadgedLabelForUser(CharSequence label, UserHandle user) {
Svetoslavc7d62f02014-09-04 15:39:54 -07001729 return mContext.getPackageManager().getUserBadgedLabel(label, user);
Amith Yamasani4f582632014-02-19 14:31:52 -08001730 }
1731
1732 /**
1733 * Returns information for all users on this device. Requires
1734 * {@link android.Manifest.permission#MANAGE_USERS} permission.
Emily Bernier394a6cd2014-05-07 12:49:20 -04001735 *
Amith Yamasani4f582632014-02-19 14:31:52 -08001736 * @param excludeDying specify if the list should exclude users being
1737 * removed.
Amith Yamasani920ace02012-09-20 22:15:37 -07001738 * @return the list of users that were created.
1739 * @hide
1740 */
1741 public List<UserInfo> getUsers(boolean excludeDying) {
1742 try {
1743 return mService.getUsers(excludeDying);
Amith Yamasani258848d2012-08-10 17:06:33 -07001744 } catch (RemoteException re) {
1745 Log.w(TAG, "Could not get user list", re);
1746 return null;
1747 }
1748 }
1749
1750 /**
1751 * Removes a user and all associated data.
Amith Yamasani195263742012-08-21 15:40:12 -07001752 * Requires {@link android.Manifest.permission#MANAGE_USERS} permission.
Amith Yamasani258848d2012-08-10 17:06:33 -07001753 * @param userHandle the integer handle of the user, where 0 is the primary user.
1754 * @hide
1755 */
Jeff Sharkey8588bc12016-01-06 16:47:42 -07001756 public boolean removeUser(@UserIdInt int userHandle) {
Amith Yamasani258848d2012-08-10 17:06:33 -07001757 try {
1758 return mService.removeUser(userHandle);
1759 } catch (RemoteException re) {
1760 Log.w(TAG, "Could not remove user ", re);
1761 return false;
1762 }
1763 }
1764
1765 /**
1766 * Updates the user's name.
Amith Yamasani195263742012-08-21 15:40:12 -07001767 * Requires {@link android.Manifest.permission#MANAGE_USERS} permission.
Amith Yamasani258848d2012-08-10 17:06:33 -07001768 *
1769 * @param userHandle the user's integer handle
1770 * @param name the new name for the user
1771 * @hide
1772 */
Jeff Sharkey8588bc12016-01-06 16:47:42 -07001773 public void setUserName(@UserIdInt int userHandle, String name) {
Amith Yamasani258848d2012-08-10 17:06:33 -07001774 try {
1775 mService.setUserName(userHandle, name);
1776 } catch (RemoteException re) {
1777 Log.w(TAG, "Could not set the user name ", re);
1778 }
1779 }
1780
1781 /**
Amith Yamasanie928d7d2012-09-17 21:46:51 -07001782 * Sets the user's photo.
Amith Yamasani258848d2012-08-10 17:06:33 -07001783 * @param userHandle the user for whom to change the photo.
Amith Yamasanie928d7d2012-09-17 21:46:51 -07001784 * @param icon the bitmap to set as the photo.
Amith Yamasani258848d2012-08-10 17:06:33 -07001785 * @hide
1786 */
Jeff Sharkey8588bc12016-01-06 16:47:42 -07001787 public void setUserIcon(@UserIdInt int userHandle, Bitmap icon) {
Amith Yamasani258848d2012-08-10 17:06:33 -07001788 try {
Amith Yamasanie928d7d2012-09-17 21:46:51 -07001789 mService.setUserIcon(userHandle, icon);
Amith Yamasani258848d2012-08-10 17:06:33 -07001790 } catch (RemoteException re) {
1791 Log.w(TAG, "Could not set the user icon ", re);
Amith Yamasani258848d2012-08-10 17:06:33 -07001792 }
1793 }
1794
1795 /**
Amith Yamasani3b49f072012-09-17 10:21:43 -07001796 * Returns a file descriptor for the user's photo. PNG data can be read from this file.
1797 * @param userHandle the user whose photo we want to read.
Amith Yamasanie928d7d2012-09-17 21:46:51 -07001798 * @return a {@link Bitmap} of the user's photo, or null if there's no photo.
Alexandra Gherghina64d4dca2014-08-28 18:26:56 +01001799 * @see com.android.internal.util.UserIcons#getDefaultUserIcon for a default.
Amith Yamasani3b49f072012-09-17 10:21:43 -07001800 * @hide
1801 */
Jeff Sharkey8588bc12016-01-06 16:47:42 -07001802 public Bitmap getUserIcon(@UserIdInt int userHandle) {
Amith Yamasani3b49f072012-09-17 10:21:43 -07001803 try {
Adrian Roos1bdff912015-02-17 15:51:35 +01001804 ParcelFileDescriptor fd = mService.getUserIcon(userHandle);
1805 if (fd != null) {
1806 try {
1807 return BitmapFactory.decodeFileDescriptor(fd.getFileDescriptor());
1808 } finally {
1809 try {
1810 fd.close();
1811 } catch (IOException e) {
1812 }
1813 }
1814 }
Amith Yamasani3b49f072012-09-17 10:21:43 -07001815 } catch (RemoteException re) {
Amith Yamasanie928d7d2012-09-17 21:46:51 -07001816 Log.w(TAG, "Could not get the user icon ", re);
Amith Yamasani3b49f072012-09-17 10:21:43 -07001817 }
Adrian Roos1bdff912015-02-17 15:51:35 +01001818 return null;
Amith Yamasani3b49f072012-09-17 10:21:43 -07001819 }
1820
1821 /**
Amith Yamasani258848d2012-08-10 17:06:33 -07001822 * Returns the maximum number of users that can be created on this device. A return value
1823 * of 1 means that it is a single user device.
1824 * @hide
Maggie Benthalla12fccf2013-03-14 18:02:12 -04001825 * @return a value greater than or equal to 1
Amith Yamasani258848d2012-08-10 17:06:33 -07001826 */
Jeff Sharkey27bd34d2012-09-16 12:49:00 -07001827 public static int getMaxSupportedUsers() {
Amith Yamasaniff549202012-10-12 12:44:49 -07001828 // Don't allow multiple users on certain builds
1829 if (android.os.Build.ID.startsWith("JVP")) return 1;
Dianne Hackborn409297d2014-07-10 17:39:20 -07001830 // Svelte devices don't get multi-user.
1831 if (ActivityManager.isLowRamDeviceStatic()) return 1;
Jeff Sharkey27bd34d2012-09-16 12:49:00 -07001832 return SystemProperties.getInt("fw.max_users",
1833 Resources.getSystem().getInteger(R.integer.config_multiuserMaximumUsers));
Amith Yamasani258848d2012-08-10 17:06:33 -07001834 }
Amith Yamasani2a003292012-08-14 18:25:45 -07001835
1836 /**
Fyodor Kupolovcd86ebf2015-09-29 17:06:50 -07001837 * Returns true if the user switcher should be shown, this will be if device supports multi-user
1838 * and there are at least 2 users available that are not managed profiles.
Kenny Guy1a447532014-02-20 21:55:32 +00001839 * @hide
1840 * @return true if user switcher should be shown.
1841 */
1842 public boolean isUserSwitcherEnabled() {
Fyodor Kupolovcd86ebf2015-09-29 17:06:50 -07001843 if (!supportsMultipleUsers()) {
1844 return false;
1845 }
Kenny Guy1a447532014-02-20 21:55:32 +00001846 List<UserInfo> users = getUsers(true);
1847 if (users == null) {
1848 return false;
1849 }
1850 int switchableUserCount = 0;
1851 for (UserInfo user : users) {
Xiaohui Chen7cb69df2015-07-13 16:01:01 -07001852 if (user.supportsSwitchToByUser()) {
Kenny Guy1a447532014-02-20 21:55:32 +00001853 ++switchableUserCount;
1854 }
1855 }
Fyodor Kupolovcd86ebf2015-09-29 17:06:50 -07001856 final boolean guestEnabled = !mContext.getSystemService(DevicePolicyManager.class)
1857 .getGuestUserDisabled(null);
Amith Yamasania596ff82014-06-12 18:12:38 -07001858 return switchableUserCount > 1 || guestEnabled;
Kenny Guy1a447532014-02-20 21:55:32 +00001859 }
1860
1861 /**
Amith Yamasani2a003292012-08-14 18:25:45 -07001862 * Returns a serial number on this device for a given userHandle. User handles can be recycled
1863 * when deleting and creating users, but serial numbers are not reused until the device is wiped.
1864 * @param userHandle
1865 * @return a serial number associated with that user, or -1 if the userHandle is not valid.
1866 * @hide
1867 */
Jeff Sharkey8588bc12016-01-06 16:47:42 -07001868 public int getUserSerialNumber(@UserIdInt int userHandle) {
Amith Yamasani2a003292012-08-14 18:25:45 -07001869 try {
1870 return mService.getUserSerialNumber(userHandle);
1871 } catch (RemoteException re) {
1872 Log.w(TAG, "Could not get serial number for user " + userHandle);
1873 }
1874 return -1;
1875 }
1876
1877 /**
1878 * Returns a userHandle on this device for a given user serial number. User handles can be
1879 * recycled when deleting and creating users, but serial numbers are not reused until the device
1880 * is wiped.
1881 * @param userSerialNumber
1882 * @return the userHandle associated with that user serial number, or -1 if the serial number
1883 * is not valid.
1884 * @hide
1885 */
Jeff Sharkey8588bc12016-01-06 16:47:42 -07001886 public @UserIdInt int getUserHandle(int userSerialNumber) {
Amith Yamasani2a003292012-08-14 18:25:45 -07001887 try {
1888 return mService.getUserHandle(userSerialNumber);
1889 } catch (RemoteException re) {
1890 Log.w(TAG, "Could not get userHandle for user " + userSerialNumber);
1891 }
1892 return -1;
1893 }
Maggie Benthall67944582013-02-22 14:58:27 -05001894
Amith Yamasani7e99bc02013-04-16 18:24:51 -07001895 /**
Esteban Talavera5b9f1672015-12-11 15:22:34 +00001896 * Returns a {@code Bundle} containing any saved application restrictions for this user, for the
Amith Yamasani7e99bc02013-04-16 18:24:51 -07001897 * given package name. Only an application with this package name can call this method.
Esteban Talavera5b9f1672015-12-11 15:22:34 +00001898 *
1899 * <p>The returned {@link Bundle} consists of key-value pairs, as defined by the application,
1900 * where the types of values may be:
1901 * <ul>
1902 * <li>{@code boolean}
1903 * <li>{@code int}
1904 * <li>{@code String} or {@code String[]}
1905 * <li>From {@link android.os.Build.VERSION_CODES#M}, {@code Bundle} or {@code Bundle[]}
1906 * </ul>
1907 *
Amith Yamasani7e99bc02013-04-16 18:24:51 -07001908 * @param packageName the package name of the calling application
Esteban Talavera5b9f1672015-12-11 15:22:34 +00001909 * @return a {@code Bundle} with the restrictions for that package, or {@code null} if there
1910 * are no saved restrictions.
1911 *
1912 * @see #KEY_RESTRICTIONS_PENDING
Amith Yamasani7e99bc02013-04-16 18:24:51 -07001913 */
1914 public Bundle getApplicationRestrictions(String packageName) {
1915 try {
1916 return mService.getApplicationRestrictions(packageName);
1917 } catch (RemoteException re) {
1918 Log.w(TAG, "Could not get application restrictions for package " + packageName);
1919 }
1920 return null;
1921 }
Amith Yamasanidf2e92a2013-03-01 17:04:38 -08001922
1923 /**
1924 * @hide
1925 */
Amith Yamasani7e99bc02013-04-16 18:24:51 -07001926 public Bundle getApplicationRestrictions(String packageName, UserHandle user) {
Amith Yamasanidf2e92a2013-03-01 17:04:38 -08001927 try {
Amith Yamasani7e99bc02013-04-16 18:24:51 -07001928 return mService.getApplicationRestrictionsForUser(packageName, user.getIdentifier());
Amith Yamasanidf2e92a2013-03-01 17:04:38 -08001929 } catch (RemoteException re) {
1930 Log.w(TAG, "Could not get application restrictions for user " + user.getIdentifier());
1931 }
1932 return null;
1933 }
1934
1935 /**
1936 * @hide
1937 */
Amith Yamasani7e99bc02013-04-16 18:24:51 -07001938 public void setApplicationRestrictions(String packageName, Bundle restrictions,
Amith Yamasanidf2e92a2013-03-01 17:04:38 -08001939 UserHandle user) {
1940 try {
Amith Yamasani7e99bc02013-04-16 18:24:51 -07001941 mService.setApplicationRestrictions(packageName, restrictions, user.getIdentifier());
Amith Yamasanidf2e92a2013-03-01 17:04:38 -08001942 } catch (RemoteException re) {
1943 Log.w(TAG, "Could not set application restrictions for user " + user.getIdentifier());
1944 }
1945 }
Amith Yamasani655d0e22013-06-12 14:19:10 -07001946
1947 /**
Amith Yamasanid304af62013-09-05 09:30:23 -07001948 * Sets a new challenge PIN for restrictions. This is only for use by pre-installed
1949 * apps and requires the MANAGE_USERS permission.
1950 * @param newPin the PIN to use for challenge dialogs.
1951 * @return Returns true if the challenge PIN was set successfully.
Fyodor Kupolovef249092015-05-06 13:18:46 -07001952 * @deprecated The restrictions PIN functionality is no longer provided by the system.
1953 * This method is preserved for backwards compatibility reasons and always returns false.
Amith Yamasani655d0e22013-06-12 14:19:10 -07001954 */
Amith Yamasanid304af62013-09-05 09:30:23 -07001955 public boolean setRestrictionsChallenge(String newPin) {
Amith Yamasani655d0e22013-06-12 14:19:10 -07001956 return false;
1957 }
Amith Yamasani1a7472e2013-07-02 11:17:30 -07001958
Amith Yamasanie4afaa32014-06-30 14:55:07 +05301959 /**
1960 * @hide
1961 * Set restrictions that should apply to any future guest user that's created.
1962 */
1963 public void setDefaultGuestRestrictions(Bundle restrictions) {
1964 try {
1965 mService.setDefaultGuestRestrictions(restrictions);
1966 } catch (RemoteException re) {
1967 Log.w(TAG, "Could not set guest restrictions");
1968 }
1969 }
1970
1971 /**
1972 * @hide
1973 * Gets the default guest restrictions.
1974 */
1975 public Bundle getDefaultGuestRestrictions() {
1976 try {
1977 return mService.getDefaultGuestRestrictions();
1978 } catch (RemoteException re) {
1979 Log.w(TAG, "Could not set guest restrictions");
1980 }
1981 return new Bundle();
1982 }
Fyodor Kupolovff7233e2015-04-08 11:28:52 -07001983
1984 /**
1985 * Returns creation time of the user or of a managed profile associated with the calling user.
1986 * @param userHandle user handle of the user or a managed profile associated with the
1987 * calling user.
1988 * @return creation time in milliseconds since Epoch time.
1989 */
Fyodor Kupolov385de622015-04-10 18:00:19 -07001990 public long getUserCreationTime(UserHandle userHandle) {
Fyodor Kupolovff7233e2015-04-08 11:28:52 -07001991 try {
Fyodor Kupolov385de622015-04-10 18:00:19 -07001992 return mService.getUserCreationTime(userHandle.getIdentifier());
Fyodor Kupolovff7233e2015-04-08 11:28:52 -07001993 } catch (RemoteException re) {
1994 Log.w(TAG, "Could not get user creation time", re);
1995 return 0;
1996 }
1997 }
Amith Yamasani12747872015-12-07 14:19:49 -08001998
1999 /**
2000 * @hide
2001 * Checks if any uninitialized user has the specific seed account name and type.
2002 *
2003 * @param mAccountName The account name to check for
2004 * @param mAccountType The account type of the account to check for
2005 * @return whether the seed account was found
2006 */
2007 public boolean someUserHasSeedAccount(String accountName, String accountType) {
2008 try {
2009 return mService.someUserHasSeedAccount(accountName, accountType);
2010 } catch (RemoteException re) {
2011 Log.w(TAG, "Could not check seed accounts", re);
2012 return false;
2013 }
2014 }
Amith Yamasani258848d2012-08-10 17:06:33 -07002015}