Amith Yamasani | 258848d | 2012-08-10 17:06:33 -0700 | [diff] [blame] | 1 | /* |
| 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 | */ |
| 16 | package android.os; |
| 17 | |
Dianne Hackborn | a8a9bd6 | 2012-10-09 15:36:59 -0700 | [diff] [blame] | 18 | import android.app.ActivityManagerNative; |
Amith Yamasani | 258848d | 2012-08-10 17:06:33 -0700 | [diff] [blame] | 19 | import android.content.Context; |
Amith Yamasani | df2e92a | 2013-03-01 17:04:38 -0800 | [diff] [blame] | 20 | import android.content.RestrictionEntry; |
Amith Yamasani | 258848d | 2012-08-10 17:06:33 -0700 | [diff] [blame] | 21 | import android.content.pm.UserInfo; |
Jeff Sharkey | 27bd34d | 2012-09-16 12:49:00 -0700 | [diff] [blame] | 22 | import android.content.res.Resources; |
Maggie Benthall | 6794458 | 2013-02-22 14:58:27 -0500 | [diff] [blame] | 23 | import android.graphics.Bitmap; |
Amith Yamasani | 258848d | 2012-08-10 17:06:33 -0700 | [diff] [blame] | 24 | import android.util.Log; |
| 25 | |
Maggie Benthall | 6794458 | 2013-02-22 14:58:27 -0500 | [diff] [blame] | 26 | import com.android.internal.R; |
| 27 | |
Amith Yamasani | 258848d | 2012-08-10 17:06:33 -0700 | [diff] [blame] | 28 | import java.util.List; |
| 29 | |
| 30 | /** |
| 31 | * Manages users and user details on a multi-user system. |
| 32 | */ |
| 33 | public class UserManager { |
| 34 | |
| 35 | private static String TAG = "UserManager"; |
| 36 | private final IUserManager mService; |
| 37 | private final Context mContext; |
| 38 | |
Amith Yamasani | e4cf734 | 2012-12-17 11:12:09 -0800 | [diff] [blame] | 39 | /** |
Amith Yamasani | 71e6c69 | 2013-03-24 17:39:28 -0700 | [diff] [blame] | 40 | * Key for user restrictions. Specifies if a user is disallowed from adding and removing |
| 41 | * accounts. |
| 42 | * The default value is <code>false</code>. |
| 43 | * <p/> |
Amith Yamasani | e4cf734 | 2012-12-17 11:12:09 -0800 | [diff] [blame] | 44 | * Type: Boolean |
| 45 | * @see #setUserRestrictions(Bundle) |
| 46 | * @see #getUserRestrictions() |
| 47 | */ |
Amith Yamasani | 71e6c69 | 2013-03-24 17:39:28 -0700 | [diff] [blame] | 48 | public static final String DISALLOW_MODIFY_ACCOUNTS = "no_modify_accounts"; |
Amith Yamasani | e4cf734 | 2012-12-17 11:12:09 -0800 | [diff] [blame] | 49 | |
| 50 | /** |
Amith Yamasani | 71e6c69 | 2013-03-24 17:39:28 -0700 | [diff] [blame] | 51 | * Key for user restrictions. Specifies if a user is disallowed from changing Wi-Fi |
| 52 | * access points. |
| 53 | * The default value is <code>false</code>. |
| 54 | * <p/> |
Amith Yamasani | e4cf734 | 2012-12-17 11:12:09 -0800 | [diff] [blame] | 55 | * Type: Boolean |
| 56 | * @see #setUserRestrictions(Bundle) |
| 57 | * @see #getUserRestrictions() |
| 58 | */ |
Amith Yamasani | 71e6c69 | 2013-03-24 17:39:28 -0700 | [diff] [blame] | 59 | public static final String DISALLOW_CONFIG_WIFI = "no_config_wifi"; |
Amith Yamasani | e4cf734 | 2012-12-17 11:12:09 -0800 | [diff] [blame] | 60 | |
| 61 | /** |
Amith Yamasani | 71e6c69 | 2013-03-24 17:39:28 -0700 | [diff] [blame] | 62 | * Key for user restrictions. Specifies if a user is disallowed from installing applications. |
| 63 | * The default value is <code>false</code>. |
| 64 | * <p/> |
Amith Yamasani | e4cf734 | 2012-12-17 11:12:09 -0800 | [diff] [blame] | 65 | * Type: Boolean |
| 66 | * @see #setUserRestrictions(Bundle) |
| 67 | * @see #getUserRestrictions() |
| 68 | */ |
Amith Yamasani | 71e6c69 | 2013-03-24 17:39:28 -0700 | [diff] [blame] | 69 | public static final String DISALLOW_INSTALL_APPS = "no_install_apps"; |
Amith Yamasani | e4cf734 | 2012-12-17 11:12:09 -0800 | [diff] [blame] | 70 | |
| 71 | /** |
Amith Yamasani | 71e6c69 | 2013-03-24 17:39:28 -0700 | [diff] [blame] | 72 | * Key for user restrictions. Specifies if a user is disallowed from uninstalling applications. |
| 73 | * The default value is <code>false</code>. |
| 74 | * <p/> |
Amith Yamasani | e4cf734 | 2012-12-17 11:12:09 -0800 | [diff] [blame] | 75 | * Type: Boolean |
| 76 | * @see #setUserRestrictions(Bundle) |
| 77 | * @see #getUserRestrictions() |
| 78 | */ |
Amith Yamasani | 71e6c69 | 2013-03-24 17:39:28 -0700 | [diff] [blame] | 79 | public static final String DISALLOW_UNINSTALL_APPS = "no_uninstall_apps"; |
Amith Yamasani | e4cf734 | 2012-12-17 11:12:09 -0800 | [diff] [blame] | 80 | |
Amith Yamasani | 71e6c69 | 2013-03-24 17:39:28 -0700 | [diff] [blame] | 81 | /** |
| 82 | * Key for user restrictions. Specifies if a user is disallowed from toggling location sharing. |
| 83 | * The default value is <code>false</code>. |
| 84 | * <p/> |
Maggie Benthall | 6794458 | 2013-02-22 14:58:27 -0500 | [diff] [blame] | 85 | * Type: Boolean |
| 86 | * @see #setUserRestrictions(Bundle) |
| 87 | * @see #getUserRestrictions() |
| 88 | */ |
Maggie Benthall | a12fccf | 2013-03-14 18:02:12 -0400 | [diff] [blame] | 89 | |
Amith Yamasani | 71e6c69 | 2013-03-24 17:39:28 -0700 | [diff] [blame] | 90 | public static final String DISALLOW_SHARE_LOCATION = "no_share_location"; |
Maggie Benthall | 6794458 | 2013-02-22 14:58:27 -0500 | [diff] [blame] | 91 | |
Maggie Benthall | a12fccf | 2013-03-14 18:02:12 -0400 | [diff] [blame] | 92 | /** |
| 93 | * Key for user restrictions. Specifies if a user is disallowed from enabling the |
| 94 | * "Unknown Sources" setting, that allows installation of apps from unknown sources. |
| 95 | * The default value is <code>false</code>. |
| 96 | * <p/> |
| 97 | * Type: Boolean |
| 98 | * @see #setUserRestrictions(Bundle) |
| 99 | * @see #getUserRestrictions() |
| 100 | */ |
| 101 | public static final String DISALLOW_INSTALL_UNKNOWN_SOURCES = "no_install_unknown_sources"; |
| 102 | |
| 103 | /** |
| 104 | * Key for user restrictions. Specifies if a user is disallowed from configuring bluetooth. |
| 105 | * The default value is <code>false</code>. |
| 106 | * <p/> |
| 107 | * Type: Boolean |
| 108 | * @see #setUserRestrictions(Bundle) |
| 109 | * @see #getUserRestrictions() |
| 110 | */ |
| 111 | public static final String DISALLOW_CONFIG_BLUETOOTH = "no_config_bluetooth"; |
| 112 | |
Maggie Benthall | a12fccf | 2013-03-14 18:02:12 -0400 | [diff] [blame] | 113 | /** |
| 114 | * Key for user restrictions. Specifies if a user is disallowed from transferring files over |
| 115 | * USB. The default value is <code>false</code>. |
| 116 | * <p/> |
| 117 | * Type: Boolean |
| 118 | * @see #setUserRestrictions(Bundle) |
| 119 | * @see #getUserRestrictions() |
| 120 | */ |
| 121 | public static final String DISALLOW_USB_FILE_TRANSFER = "no_usb_file_transfer"; |
| 122 | |
Emily Bernier | b223f73 | 2013-04-11 15:46:36 -0400 | [diff] [blame] | 123 | /** |
| 124 | * Key for user restrictions. Specifies if a user is disallowed from configuring user |
| 125 | * credentials. The default value is <code>false</code>. |
| 126 | * <p/> |
| 127 | * Type: Boolean |
| 128 | * @see #setUserRestrictions(Bundle) |
| 129 | * @see #getUserRestrictions() |
| 130 | */ |
| 131 | public static final String DISALLOW_CONFIG_CREDENTIALS = "no_config_credentials"; |
| 132 | |
| 133 | /** |
| 134 | * Key for user restrictions. Specifies if a user is disallowed from removing users. |
| 135 | * The default value is <code>false</code>. |
| 136 | * <p/> |
| 137 | * Type: Boolean |
| 138 | * @see #setUserRestrictions(Bundle) |
| 139 | * @see #getUserRestrictions() |
| 140 | */ |
| 141 | public static final String DISALLOW_REMOVE_USER = "no_remove_user"; |
| 142 | |
Amith Yamasani | 27db468 | 2013-03-30 17:07:47 -0700 | [diff] [blame] | 143 | private static UserManager sInstance = null; |
| 144 | |
Amith Yamasani | 7e99bc0 | 2013-04-16 18:24:51 -0700 | [diff] [blame] | 145 | /** @hide */ |
Amith Yamasani | 27db468 | 2013-03-30 17:07:47 -0700 | [diff] [blame] | 146 | public synchronized static UserManager get(Context context) { |
| 147 | if (sInstance == null) { |
| 148 | sInstance = (UserManager) context.getSystemService(Context.USER_SERVICE); |
| 149 | } |
| 150 | return sInstance; |
| 151 | } |
Maggie Benthall | a12fccf | 2013-03-14 18:02:12 -0400 | [diff] [blame] | 152 | |
Amith Yamasani | 258848d | 2012-08-10 17:06:33 -0700 | [diff] [blame] | 153 | /** @hide */ |
| 154 | public UserManager(Context context, IUserManager service) { |
| 155 | mService = service; |
| 156 | mContext = context; |
| 157 | } |
| 158 | |
| 159 | /** |
| 160 | * Returns whether the system supports multiple users. |
| 161 | * @return true if multiple users can be created, false if it is a single user device. |
Amith Yamasani | e928d7d | 2012-09-17 21:46:51 -0700 | [diff] [blame] | 162 | * @hide |
Amith Yamasani | 258848d | 2012-08-10 17:06:33 -0700 | [diff] [blame] | 163 | */ |
Jeff Sharkey | 4673e7e | 2012-09-19 13:14:30 -0700 | [diff] [blame] | 164 | public static boolean supportsMultipleUsers() { |
Amith Yamasani | 258848d | 2012-08-10 17:06:33 -0700 | [diff] [blame] | 165 | return getMaxSupportedUsers() > 1; |
| 166 | } |
| 167 | |
Maggie Benthall | 6794458 | 2013-02-22 14:58:27 -0500 | [diff] [blame] | 168 | /** |
Amith Yamasani | 258848d | 2012-08-10 17:06:33 -0700 | [diff] [blame] | 169 | * Returns the user handle for the user that this application is running for. |
| 170 | * @return the user handle of the user making this call. |
| 171 | * @hide |
Maggie Benthall | 6794458 | 2013-02-22 14:58:27 -0500 | [diff] [blame] | 172 | */ |
Amith Yamasani | 258848d | 2012-08-10 17:06:33 -0700 | [diff] [blame] | 173 | public int getUserHandle() { |
Dianne Hackborn | 79af1dd | 2012-08-16 16:42:52 -0700 | [diff] [blame] | 174 | return UserHandle.myUserId(); |
Amith Yamasani | 258848d | 2012-08-10 17:06:33 -0700 | [diff] [blame] | 175 | } |
| 176 | |
| 177 | /** |
Dianne Hackborn | 8832c18 | 2012-09-17 17:20:24 -0700 | [diff] [blame] | 178 | * Returns the user name of the user making this call. This call is only |
| 179 | * available to applications on the system image; it requires the |
| 180 | * MANAGE_USERS permission. |
Amith Yamasani | 258848d | 2012-08-10 17:06:33 -0700 | [diff] [blame] | 181 | * @return the user name |
| 182 | */ |
| 183 | public String getUserName() { |
| 184 | try { |
| 185 | return mService.getUserInfo(getUserHandle()).name; |
| 186 | } catch (RemoteException re) { |
| 187 | Log.w(TAG, "Could not get user name", re); |
| 188 | return ""; |
| 189 | } |
| 190 | } |
| 191 | |
Dan Morrill | e4ab16a | 2012-09-20 20:25:55 -0700 | [diff] [blame] | 192 | /** |
| 193 | * Used to determine whether the user making this call is subject to |
| 194 | * teleportations. |
Maggie Benthall | a12fccf | 2013-03-14 18:02:12 -0400 | [diff] [blame] | 195 | * @return whether the user making this call is a goat |
Dan Morrill | e4ab16a | 2012-09-20 20:25:55 -0700 | [diff] [blame] | 196 | */ |
| 197 | public boolean isUserAGoat() { |
| 198 | return false; |
| 199 | } |
Amith Yamasani | df2e92a | 2013-03-01 17:04:38 -0800 | [diff] [blame] | 200 | |
| 201 | /** |
Amith Yamasani | e137590 | 2013-04-13 16:48:35 -0700 | [diff] [blame] | 202 | * Used to check if the user making this call is linked to another user. Linked users may have |
Amith Yamasani | 46bc4eb | 2013-04-12 13:26:50 -0700 | [diff] [blame] | 203 | * a reduced number of available apps, app restrictions and account restrictions. |
Amith Yamasani | e137590 | 2013-04-13 16:48:35 -0700 | [diff] [blame] | 204 | * @return whether the user making this call is a linked user |
Amith Yamasani | 2555daf | 2013-04-25 13:39:27 -0700 | [diff] [blame] | 205 | * @hide |
Amith Yamasani | df2e92a | 2013-03-01 17:04:38 -0800 | [diff] [blame] | 206 | */ |
Amith Yamasani | e137590 | 2013-04-13 16:48:35 -0700 | [diff] [blame] | 207 | public boolean isLinkedUser() { |
Amith Yamasani | df2e92a | 2013-03-01 17:04:38 -0800 | [diff] [blame] | 208 | try { |
Amith Yamasani | 71e6c69 | 2013-03-24 17:39:28 -0700 | [diff] [blame] | 209 | return mService.isRestricted(); |
Amith Yamasani | df2e92a | 2013-03-01 17:04:38 -0800 | [diff] [blame] | 210 | } catch (RemoteException re) { |
Amith Yamasani | 46bc4eb | 2013-04-12 13:26:50 -0700 | [diff] [blame] | 211 | Log.w(TAG, "Could not check if user is limited ", re); |
Amith Yamasani | df2e92a | 2013-03-01 17:04:38 -0800 | [diff] [blame] | 212 | return false; |
| 213 | } |
| 214 | } |
| 215 | |
Amith Yamasani | 258848d | 2012-08-10 17:06:33 -0700 | [diff] [blame] | 216 | /** |
Dianne Hackborn | a8a9bd6 | 2012-10-09 15:36:59 -0700 | [diff] [blame] | 217 | * Return whether the given user is actively running. This means that |
| 218 | * the user is in the "started" state, not "stopped" -- it is currently |
| 219 | * allowed to run code through scheduled alarms, receiving broadcasts, |
| 220 | * etc. A started user may be either the current foreground user or a |
| 221 | * background user; the result here does not distinguish between the two. |
| 222 | * @param user The user to retrieve the running state for. |
| 223 | */ |
| 224 | public boolean isUserRunning(UserHandle user) { |
| 225 | try { |
| 226 | return ActivityManagerNative.getDefault().isUserRunning( |
| 227 | user.getIdentifier(), false); |
| 228 | } catch (RemoteException e) { |
| 229 | return false; |
| 230 | } |
| 231 | } |
| 232 | |
| 233 | /** |
| 234 | * Return whether the given user is actively running <em>or</em> stopping. |
| 235 | * This is like {@link #isUserRunning(UserHandle)}, but will also return |
| 236 | * true if the user had been running but is in the process of being stopped |
| 237 | * (but is not yet fully stopped, and still running some code). |
| 238 | * @param user The user to retrieve the running state for. |
| 239 | */ |
| 240 | public boolean isUserRunningOrStopping(UserHandle user) { |
| 241 | try { |
| 242 | return ActivityManagerNative.getDefault().isUserRunning( |
| 243 | user.getIdentifier(), true); |
| 244 | } catch (RemoteException e) { |
| 245 | return false; |
| 246 | } |
| 247 | } |
| 248 | |
| 249 | /** |
Amith Yamasani | 258848d | 2012-08-10 17:06:33 -0700 | [diff] [blame] | 250 | * Returns the UserInfo object describing a specific user. |
Amith Yamasani | 19526374 | 2012-08-21 15:40:12 -0700 | [diff] [blame] | 251 | * Requires {@link android.Manifest.permission#MANAGE_USERS} permission. |
Amith Yamasani | 258848d | 2012-08-10 17:06:33 -0700 | [diff] [blame] | 252 | * @param userHandle the user handle of the user whose information is being requested. |
| 253 | * @return the UserInfo object for a specific user. |
| 254 | * @hide |
Dianne Hackborn | b26306a | 2012-10-24 15:22:21 -0700 | [diff] [blame] | 255 | */ |
Amith Yamasani | 258848d | 2012-08-10 17:06:33 -0700 | [diff] [blame] | 256 | public UserInfo getUserInfo(int userHandle) { |
| 257 | try { |
| 258 | return mService.getUserInfo(userHandle); |
| 259 | } catch (RemoteException re) { |
| 260 | Log.w(TAG, "Could not get user info", re); |
| 261 | return null; |
| 262 | } |
| 263 | } |
| 264 | |
Amith Yamasani | 71e6c69 | 2013-03-24 17:39:28 -0700 | [diff] [blame] | 265 | /** |
| 266 | * Returns the user-wide restrictions imposed on this user. |
| 267 | * @return a Bundle containing all the restrictions. |
| 268 | */ |
Amith Yamasani | e4cf734 | 2012-12-17 11:12:09 -0800 | [diff] [blame] | 269 | public Bundle getUserRestrictions() { |
| 270 | return getUserRestrictions(Process.myUserHandle()); |
| 271 | } |
| 272 | |
Amith Yamasani | 71e6c69 | 2013-03-24 17:39:28 -0700 | [diff] [blame] | 273 | /** |
| 274 | * Returns the user-wide restrictions imposed on the user specified by <code>userHandle</code>. |
| 275 | * @param userHandle the UserHandle of the user for whom to retrieve the restrictions. |
| 276 | * @return a Bundle containing all the restrictions. |
| 277 | */ |
Amith Yamasani | e4cf734 | 2012-12-17 11:12:09 -0800 | [diff] [blame] | 278 | public Bundle getUserRestrictions(UserHandle userHandle) { |
| 279 | try { |
| 280 | return mService.getUserRestrictions(userHandle.getIdentifier()); |
| 281 | } catch (RemoteException re) { |
| 282 | Log.w(TAG, "Could not get user restrictions", re); |
| 283 | return Bundle.EMPTY; |
| 284 | } |
| 285 | } |
| 286 | |
Amith Yamasani | 71e6c69 | 2013-03-24 17:39:28 -0700 | [diff] [blame] | 287 | /** |
| 288 | * Sets all the user-wide restrictions for this user. |
Amith Yamasani | ca050ad | 2013-03-26 16:15:43 -0700 | [diff] [blame] | 289 | * Requires the MANAGE_USERS permission. |
Amith Yamasani | 71e6c69 | 2013-03-24 17:39:28 -0700 | [diff] [blame] | 290 | * @param restrictions the Bundle containing all the restrictions. |
| 291 | */ |
Amith Yamasani | e4cf734 | 2012-12-17 11:12:09 -0800 | [diff] [blame] | 292 | public void setUserRestrictions(Bundle restrictions) { |
| 293 | setUserRestrictions(restrictions, Process.myUserHandle()); |
| 294 | } |
| 295 | |
Amith Yamasani | 71e6c69 | 2013-03-24 17:39:28 -0700 | [diff] [blame] | 296 | /** |
| 297 | * Sets all the user-wide restrictions for the specified user. |
Amith Yamasani | ca050ad | 2013-03-26 16:15:43 -0700 | [diff] [blame] | 298 | * Requires the MANAGE_USERS permission. |
Amith Yamasani | 71e6c69 | 2013-03-24 17:39:28 -0700 | [diff] [blame] | 299 | * @param restrictions the Bundle containing all the restrictions. |
| 300 | * @param userHandle the UserHandle of the user for whom to set the restrictions. |
| 301 | */ |
Amith Yamasani | e4cf734 | 2012-12-17 11:12:09 -0800 | [diff] [blame] | 302 | public void setUserRestrictions(Bundle restrictions, UserHandle userHandle) { |
| 303 | try { |
| 304 | mService.setUserRestrictions(restrictions, userHandle.getIdentifier()); |
| 305 | } catch (RemoteException re) { |
| 306 | Log.w(TAG, "Could not set user restrictions", re); |
| 307 | } |
| 308 | } |
| 309 | |
Amith Yamasani | 71e6c69 | 2013-03-24 17:39:28 -0700 | [diff] [blame] | 310 | /** |
| 311 | * Sets the value of a specific restriction. |
Amith Yamasani | ca050ad | 2013-03-26 16:15:43 -0700 | [diff] [blame] | 312 | * Requires the MANAGE_USERS permission. |
Amith Yamasani | 71e6c69 | 2013-03-24 17:39:28 -0700 | [diff] [blame] | 313 | * @param key the key of the restriction |
| 314 | * @param value the value for the restriction |
Amith Yamasani | 71e6c69 | 2013-03-24 17:39:28 -0700 | [diff] [blame] | 315 | */ |
| 316 | public void setUserRestriction(String key, boolean value) { |
| 317 | Bundle bundle = getUserRestrictions(); |
| 318 | bundle.putBoolean(key, value); |
| 319 | setUserRestrictions(bundle); |
| 320 | } |
| 321 | |
| 322 | /** |
| 323 | * @hide |
| 324 | * Sets the value of a specific restriction on a specific user. |
| 325 | * Requires the {@link android.Manifest.permission#MANAGE_USERS} permission. |
| 326 | * @param key the key of the restriction |
| 327 | * @param value the value for the restriction |
| 328 | * @param userHandle the user whose restriction is to be changed. |
| 329 | */ |
Maggie Benthall | 6794458 | 2013-02-22 14:58:27 -0500 | [diff] [blame] | 330 | public void setUserRestriction(String key, boolean value, UserHandle userHandle) { |
| 331 | Bundle bundle = getUserRestrictions(userHandle); |
| 332 | bundle.putBoolean(key, value); |
| 333 | setUserRestrictions(bundle, userHandle); |
| 334 | } |
| 335 | |
Amith Yamasani | 258848d | 2012-08-10 17:06:33 -0700 | [diff] [blame] | 336 | /** |
Maggie Benthall | a12fccf | 2013-03-14 18:02:12 -0400 | [diff] [blame] | 337 | * @hide |
| 338 | * Returns whether the current user has been disallowed from performing certain actions |
| 339 | * or setting certain settings. |
| 340 | * @param restrictionKey the string key representing the restriction |
| 341 | */ |
| 342 | public boolean hasUserRestriction(String restrictionKey) { |
| 343 | return getUserRestrictions().getBoolean(restrictionKey, false); |
| 344 | } |
| 345 | |
| 346 | /** |
Dianne Hackborn | 33f9cb8 | 2012-10-04 17:15:10 -0700 | [diff] [blame] | 347 | * Return the serial number for a user. This is a device-unique |
Dianne Hackborn | b26306a | 2012-10-24 15:22:21 -0700 | [diff] [blame] | 348 | * number assigned to that user; if the user is deleted and then a new |
| 349 | * user created, the new users will not be given the same serial number. |
Dianne Hackborn | 33f9cb8 | 2012-10-04 17:15:10 -0700 | [diff] [blame] | 350 | * @param user The user whose serial number is to be retrieved. |
Dianne Hackborn | b26306a | 2012-10-24 15:22:21 -0700 | [diff] [blame] | 351 | * @return The serial number of the given user; returns -1 if the |
| 352 | * given UserHandle does not exist. |
Dianne Hackborn | 33f9cb8 | 2012-10-04 17:15:10 -0700 | [diff] [blame] | 353 | * @see #getUserForSerialNumber(long) |
| 354 | */ |
| 355 | public long getSerialNumberForUser(UserHandle user) { |
| 356 | return getUserSerialNumber(user.getIdentifier()); |
| 357 | } |
| 358 | |
| 359 | /** |
| 360 | * Return the user associated with a serial number previously |
| 361 | * returned by {@link #getSerialNumberForUser(UserHandle)}. |
| 362 | * @param serialNumber The serial number of the user that is being |
| 363 | * retrieved. |
| 364 | * @return Return the user associated with the serial number, or null |
| 365 | * if there is not one. |
| 366 | * @see #getSerialNumberForUser(UserHandle) |
| 367 | */ |
| 368 | public UserHandle getUserForSerialNumber(long serialNumber) { |
| 369 | int ident = getUserHandle((int)serialNumber); |
| 370 | return ident >= 0 ? new UserHandle(ident) : null; |
| 371 | } |
| 372 | |
| 373 | /** |
Amith Yamasani | 258848d | 2012-08-10 17:06:33 -0700 | [diff] [blame] | 374 | * Creates a user with the specified name and options. |
Amith Yamasani | 19526374 | 2012-08-21 15:40:12 -0700 | [diff] [blame] | 375 | * Requires {@link android.Manifest.permission#MANAGE_USERS} permission. |
Amith Yamasani | 258848d | 2012-08-10 17:06:33 -0700 | [diff] [blame] | 376 | * |
| 377 | * @param name the user's name |
| 378 | * @param flags flags that identify the type of user and other properties. |
| 379 | * @see UserInfo |
| 380 | * |
| 381 | * @return the UserInfo object for the created user, or null if the user could not be created. |
| 382 | * @hide |
| 383 | */ |
| 384 | public UserInfo createUser(String name, int flags) { |
| 385 | try { |
| 386 | return mService.createUser(name, flags); |
| 387 | } catch (RemoteException re) { |
| 388 | Log.w(TAG, "Could not create a user", re); |
| 389 | return null; |
| 390 | } |
| 391 | } |
| 392 | |
| 393 | /** |
Dianne Hackborn | b26306a | 2012-10-24 15:22:21 -0700 | [diff] [blame] | 394 | * Return the number of users currently created on the device. |
| 395 | */ |
| 396 | public int getUserCount() { |
| 397 | List<UserInfo> users = getUsers(); |
| 398 | return users != null ? users.size() : 1; |
| 399 | } |
| 400 | |
| 401 | /** |
Amith Yamasani | 258848d | 2012-08-10 17:06:33 -0700 | [diff] [blame] | 402 | * Returns information for all users on this device. |
Amith Yamasani | 19526374 | 2012-08-21 15:40:12 -0700 | [diff] [blame] | 403 | * Requires {@link android.Manifest.permission#MANAGE_USERS} permission. |
Amith Yamasani | 258848d | 2012-08-10 17:06:33 -0700 | [diff] [blame] | 404 | * @return the list of users that were created. |
| 405 | * @hide |
| 406 | */ |
| 407 | public List<UserInfo> getUsers() { |
| 408 | try { |
Amith Yamasani | 920ace0 | 2012-09-20 22:15:37 -0700 | [diff] [blame] | 409 | return mService.getUsers(false); |
| 410 | } catch (RemoteException re) { |
| 411 | Log.w(TAG, "Could not get user list", re); |
| 412 | return null; |
| 413 | } |
| 414 | } |
| 415 | |
| 416 | /** |
| 417 | * Returns information for all users on this device. |
| 418 | * Requires {@link android.Manifest.permission#MANAGE_USERS} permission. |
| 419 | * @param excludeDying specify if the list should exclude users being removed. |
| 420 | * @return the list of users that were created. |
| 421 | * @hide |
| 422 | */ |
| 423 | public List<UserInfo> getUsers(boolean excludeDying) { |
| 424 | try { |
| 425 | return mService.getUsers(excludeDying); |
Amith Yamasani | 258848d | 2012-08-10 17:06:33 -0700 | [diff] [blame] | 426 | } catch (RemoteException re) { |
| 427 | Log.w(TAG, "Could not get user list", re); |
| 428 | return null; |
| 429 | } |
| 430 | } |
| 431 | |
| 432 | /** |
| 433 | * Removes a user and all associated data. |
Amith Yamasani | 19526374 | 2012-08-21 15:40:12 -0700 | [diff] [blame] | 434 | * Requires {@link android.Manifest.permission#MANAGE_USERS} permission. |
Amith Yamasani | 258848d | 2012-08-10 17:06:33 -0700 | [diff] [blame] | 435 | * @param userHandle the integer handle of the user, where 0 is the primary user. |
| 436 | * @hide |
| 437 | */ |
| 438 | public boolean removeUser(int userHandle) { |
| 439 | try { |
| 440 | return mService.removeUser(userHandle); |
| 441 | } catch (RemoteException re) { |
| 442 | Log.w(TAG, "Could not remove user ", re); |
| 443 | return false; |
| 444 | } |
| 445 | } |
| 446 | |
| 447 | /** |
| 448 | * Updates the user's name. |
Amith Yamasani | 19526374 | 2012-08-21 15:40:12 -0700 | [diff] [blame] | 449 | * Requires {@link android.Manifest.permission#MANAGE_USERS} permission. |
Amith Yamasani | 258848d | 2012-08-10 17:06:33 -0700 | [diff] [blame] | 450 | * |
| 451 | * @param userHandle the user's integer handle |
| 452 | * @param name the new name for the user |
| 453 | * @hide |
| 454 | */ |
| 455 | public void setUserName(int userHandle, String name) { |
| 456 | try { |
| 457 | mService.setUserName(userHandle, name); |
| 458 | } catch (RemoteException re) { |
| 459 | Log.w(TAG, "Could not set the user name ", re); |
| 460 | } |
| 461 | } |
| 462 | |
| 463 | /** |
Amith Yamasani | e928d7d | 2012-09-17 21:46:51 -0700 | [diff] [blame] | 464 | * Sets the user's photo. |
Amith Yamasani | 258848d | 2012-08-10 17:06:33 -0700 | [diff] [blame] | 465 | * @param userHandle the user for whom to change the photo. |
Amith Yamasani | e928d7d | 2012-09-17 21:46:51 -0700 | [diff] [blame] | 466 | * @param icon the bitmap to set as the photo. |
Amith Yamasani | 258848d | 2012-08-10 17:06:33 -0700 | [diff] [blame] | 467 | * @hide |
| 468 | */ |
Amith Yamasani | e928d7d | 2012-09-17 21:46:51 -0700 | [diff] [blame] | 469 | public void setUserIcon(int userHandle, Bitmap icon) { |
Amith Yamasani | 258848d | 2012-08-10 17:06:33 -0700 | [diff] [blame] | 470 | try { |
Amith Yamasani | e928d7d | 2012-09-17 21:46:51 -0700 | [diff] [blame] | 471 | mService.setUserIcon(userHandle, icon); |
Amith Yamasani | 258848d | 2012-08-10 17:06:33 -0700 | [diff] [blame] | 472 | } catch (RemoteException re) { |
| 473 | Log.w(TAG, "Could not set the user icon ", re); |
Amith Yamasani | 258848d | 2012-08-10 17:06:33 -0700 | [diff] [blame] | 474 | } |
| 475 | } |
| 476 | |
| 477 | /** |
Amith Yamasani | 3b49f07 | 2012-09-17 10:21:43 -0700 | [diff] [blame] | 478 | * Returns a file descriptor for the user's photo. PNG data can be read from this file. |
| 479 | * @param userHandle the user whose photo we want to read. |
Amith Yamasani | e928d7d | 2012-09-17 21:46:51 -0700 | [diff] [blame] | 480 | * @return a {@link Bitmap} of the user's photo, or null if there's no photo. |
Amith Yamasani | 3b49f07 | 2012-09-17 10:21:43 -0700 | [diff] [blame] | 481 | * @hide |
| 482 | */ |
Amith Yamasani | e928d7d | 2012-09-17 21:46:51 -0700 | [diff] [blame] | 483 | public Bitmap getUserIcon(int userHandle) { |
Amith Yamasani | 3b49f07 | 2012-09-17 10:21:43 -0700 | [diff] [blame] | 484 | try { |
| 485 | return mService.getUserIcon(userHandle); |
| 486 | } catch (RemoteException re) { |
Amith Yamasani | e928d7d | 2012-09-17 21:46:51 -0700 | [diff] [blame] | 487 | Log.w(TAG, "Could not get the user icon ", re); |
Amith Yamasani | 3b49f07 | 2012-09-17 10:21:43 -0700 | [diff] [blame] | 488 | return null; |
| 489 | } |
| 490 | } |
| 491 | |
| 492 | /** |
Amith Yamasani | 258848d | 2012-08-10 17:06:33 -0700 | [diff] [blame] | 493 | * Enable or disable the use of a guest account. If disabled, the existing guest account |
| 494 | * will be wiped. |
Amith Yamasani | 19526374 | 2012-08-21 15:40:12 -0700 | [diff] [blame] | 495 | * Requires {@link android.Manifest.permission#MANAGE_USERS} permission. |
Amith Yamasani | 258848d | 2012-08-10 17:06:33 -0700 | [diff] [blame] | 496 | * @param enable whether to enable a guest account. |
| 497 | * @hide |
| 498 | */ |
| 499 | public void setGuestEnabled(boolean enable) { |
| 500 | try { |
| 501 | mService.setGuestEnabled(enable); |
| 502 | } catch (RemoteException re) { |
| 503 | Log.w(TAG, "Could not change guest account availability to " + enable); |
| 504 | } |
| 505 | } |
| 506 | |
| 507 | /** |
| 508 | * Checks if a guest user is enabled for this device. |
Amith Yamasani | 19526374 | 2012-08-21 15:40:12 -0700 | [diff] [blame] | 509 | * Requires {@link android.Manifest.permission#MANAGE_USERS} permission. |
Amith Yamasani | 258848d | 2012-08-10 17:06:33 -0700 | [diff] [blame] | 510 | * @return whether a guest user is enabled |
| 511 | * @hide |
| 512 | */ |
| 513 | public boolean isGuestEnabled() { |
| 514 | try { |
| 515 | return mService.isGuestEnabled(); |
| 516 | } catch (RemoteException re) { |
| 517 | Log.w(TAG, "Could not retrieve guest enabled state"); |
| 518 | return false; |
| 519 | } |
| 520 | } |
| 521 | |
| 522 | /** |
| 523 | * Wipes all the data for a user, but doesn't remove the user. |
Amith Yamasani | 19526374 | 2012-08-21 15:40:12 -0700 | [diff] [blame] | 524 | * Requires {@link android.Manifest.permission#MANAGE_USERS} permission. |
Amith Yamasani | 258848d | 2012-08-10 17:06:33 -0700 | [diff] [blame] | 525 | * @param userHandle |
| 526 | * @hide |
| 527 | */ |
| 528 | public void wipeUser(int userHandle) { |
| 529 | try { |
| 530 | mService.wipeUser(userHandle); |
| 531 | } catch (RemoteException re) { |
| 532 | Log.w(TAG, "Could not wipe user " + userHandle); |
| 533 | } |
| 534 | } |
| 535 | |
| 536 | /** |
| 537 | * Returns the maximum number of users that can be created on this device. A return value |
| 538 | * of 1 means that it is a single user device. |
| 539 | * @hide |
Maggie Benthall | a12fccf | 2013-03-14 18:02:12 -0400 | [diff] [blame] | 540 | * @return a value greater than or equal to 1 |
Amith Yamasani | 258848d | 2012-08-10 17:06:33 -0700 | [diff] [blame] | 541 | */ |
Jeff Sharkey | 27bd34d | 2012-09-16 12:49:00 -0700 | [diff] [blame] | 542 | public static int getMaxSupportedUsers() { |
Amith Yamasani | ff54920 | 2012-10-12 12:44:49 -0700 | [diff] [blame] | 543 | // Don't allow multiple users on certain builds |
| 544 | if (android.os.Build.ID.startsWith("JVP")) return 1; |
Jeff Sharkey | 27bd34d | 2012-09-16 12:49:00 -0700 | [diff] [blame] | 545 | return SystemProperties.getInt("fw.max_users", |
| 546 | Resources.getSystem().getInteger(R.integer.config_multiuserMaximumUsers)); |
Amith Yamasani | 258848d | 2012-08-10 17:06:33 -0700 | [diff] [blame] | 547 | } |
Amith Yamasani | 2a00329 | 2012-08-14 18:25:45 -0700 | [diff] [blame] | 548 | |
| 549 | /** |
| 550 | * Returns a serial number on this device for a given userHandle. User handles can be recycled |
| 551 | * when deleting and creating users, but serial numbers are not reused until the device is wiped. |
| 552 | * @param userHandle |
| 553 | * @return a serial number associated with that user, or -1 if the userHandle is not valid. |
| 554 | * @hide |
| 555 | */ |
| 556 | public int getUserSerialNumber(int userHandle) { |
| 557 | try { |
| 558 | return mService.getUserSerialNumber(userHandle); |
| 559 | } catch (RemoteException re) { |
| 560 | Log.w(TAG, "Could not get serial number for user " + userHandle); |
| 561 | } |
| 562 | return -1; |
| 563 | } |
| 564 | |
| 565 | /** |
| 566 | * Returns a userHandle on this device for a given user serial number. User handles can be |
| 567 | * recycled when deleting and creating users, but serial numbers are not reused until the device |
| 568 | * is wiped. |
| 569 | * @param userSerialNumber |
| 570 | * @return the userHandle associated with that user serial number, or -1 if the serial number |
| 571 | * is not valid. |
| 572 | * @hide |
| 573 | */ |
| 574 | public int getUserHandle(int userSerialNumber) { |
| 575 | try { |
| 576 | return mService.getUserHandle(userSerialNumber); |
| 577 | } catch (RemoteException re) { |
| 578 | Log.w(TAG, "Could not get userHandle for user " + userSerialNumber); |
| 579 | } |
| 580 | return -1; |
| 581 | } |
Maggie Benthall | 6794458 | 2013-02-22 14:58:27 -0500 | [diff] [blame] | 582 | |
Amith Yamasani | 7e99bc0 | 2013-04-16 18:24:51 -0700 | [diff] [blame] | 583 | /** |
| 584 | * Returns a Bundle containing any saved application restrictions for this user, for the |
| 585 | * given package name. Only an application with this package name can call this method. |
| 586 | * @param packageName the package name of the calling application |
| 587 | * @return a Bundle with the restrictions as key/value pairs, or null if there are no |
| 588 | * saved restrictions. The values can be of type Boolean, String or String[], depending |
| 589 | * on the restriction type, as defined by the application. |
| 590 | */ |
| 591 | public Bundle getApplicationRestrictions(String packageName) { |
| 592 | try { |
| 593 | return mService.getApplicationRestrictions(packageName); |
| 594 | } catch (RemoteException re) { |
| 595 | Log.w(TAG, "Could not get application restrictions for package " + packageName); |
| 596 | } |
| 597 | return null; |
| 598 | } |
Amith Yamasani | df2e92a | 2013-03-01 17:04:38 -0800 | [diff] [blame] | 599 | |
| 600 | /** |
| 601 | * @hide |
| 602 | */ |
Amith Yamasani | 7e99bc0 | 2013-04-16 18:24:51 -0700 | [diff] [blame] | 603 | public Bundle getApplicationRestrictions(String packageName, UserHandle user) { |
Amith Yamasani | df2e92a | 2013-03-01 17:04:38 -0800 | [diff] [blame] | 604 | try { |
Amith Yamasani | 7e99bc0 | 2013-04-16 18:24:51 -0700 | [diff] [blame] | 605 | return mService.getApplicationRestrictionsForUser(packageName, user.getIdentifier()); |
Amith Yamasani | df2e92a | 2013-03-01 17:04:38 -0800 | [diff] [blame] | 606 | } catch (RemoteException re) { |
| 607 | Log.w(TAG, "Could not get application restrictions for user " + user.getIdentifier()); |
| 608 | } |
| 609 | return null; |
| 610 | } |
| 611 | |
| 612 | /** |
| 613 | * @hide |
| 614 | */ |
Amith Yamasani | 7e99bc0 | 2013-04-16 18:24:51 -0700 | [diff] [blame] | 615 | public void setApplicationRestrictions(String packageName, Bundle restrictions, |
Amith Yamasani | df2e92a | 2013-03-01 17:04:38 -0800 | [diff] [blame] | 616 | UserHandle user) { |
| 617 | try { |
Amith Yamasani | 7e99bc0 | 2013-04-16 18:24:51 -0700 | [diff] [blame] | 618 | mService.setApplicationRestrictions(packageName, restrictions, user.getIdentifier()); |
Amith Yamasani | df2e92a | 2013-03-01 17:04:38 -0800 | [diff] [blame] | 619 | } catch (RemoteException re) { |
| 620 | Log.w(TAG, "Could not set application restrictions for user " + user.getIdentifier()); |
| 621 | } |
| 622 | } |
Amith Yamasani | 258848d | 2012-08-10 17:06:33 -0700 | [diff] [blame] | 623 | } |