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 | |
| 18 | import com.android.internal.R; |
| 19 | import android.content.Context; |
| 20 | import android.content.pm.UserInfo; |
Amith Yamasani | e928d7d | 2012-09-17 21:46:51 -0700 | [diff] [blame] | 21 | import android.graphics.Bitmap; |
Jeff Sharkey | 27bd34d | 2012-09-16 12:49:00 -0700 | [diff] [blame] | 22 | import android.content.res.Resources; |
Amith Yamasani | 258848d | 2012-08-10 17:06:33 -0700 | [diff] [blame] | 23 | import android.util.Log; |
| 24 | |
| 25 | import java.util.List; |
| 26 | |
| 27 | /** |
| 28 | * Manages users and user details on a multi-user system. |
| 29 | */ |
| 30 | public class UserManager { |
| 31 | |
| 32 | private static String TAG = "UserManager"; |
| 33 | private final IUserManager mService; |
| 34 | private final Context mContext; |
| 35 | |
| 36 | /** @hide */ |
| 37 | public UserManager(Context context, IUserManager service) { |
| 38 | mService = service; |
| 39 | mContext = context; |
| 40 | } |
| 41 | |
| 42 | /** |
| 43 | * Returns whether the system supports multiple users. |
| 44 | * @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] | 45 | * @hide |
Amith Yamasani | 258848d | 2012-08-10 17:06:33 -0700 | [diff] [blame] | 46 | */ |
Jeff Sharkey | 4673e7e | 2012-09-19 13:14:30 -0700 | [diff] [blame] | 47 | public static boolean supportsMultipleUsers() { |
Amith Yamasani | 258848d | 2012-08-10 17:06:33 -0700 | [diff] [blame] | 48 | return getMaxSupportedUsers() > 1; |
| 49 | } |
| 50 | |
| 51 | /** |
| 52 | * Returns the user handle for the user that this application is running for. |
| 53 | * @return the user handle of the user making this call. |
| 54 | * @hide |
| 55 | * */ |
| 56 | public int getUserHandle() { |
Dianne Hackborn | 79af1dd | 2012-08-16 16:42:52 -0700 | [diff] [blame] | 57 | return UserHandle.myUserId(); |
Amith Yamasani | 258848d | 2012-08-10 17:06:33 -0700 | [diff] [blame] | 58 | } |
| 59 | |
| 60 | /** |
Dianne Hackborn | 8832c18 | 2012-09-17 17:20:24 -0700 | [diff] [blame] | 61 | * Returns the user name of the user making this call. This call is only |
| 62 | * available to applications on the system image; it requires the |
| 63 | * MANAGE_USERS permission. |
Amith Yamasani | 258848d | 2012-08-10 17:06:33 -0700 | [diff] [blame] | 64 | * @return the user name |
| 65 | */ |
| 66 | public String getUserName() { |
| 67 | try { |
| 68 | return mService.getUserInfo(getUserHandle()).name; |
| 69 | } catch (RemoteException re) { |
| 70 | Log.w(TAG, "Could not get user name", re); |
| 71 | return ""; |
| 72 | } |
| 73 | } |
| 74 | |
Dan Morrill | e4ab16a | 2012-09-20 20:25:55 -0700 | [diff] [blame] | 75 | /** |
| 76 | * Used to determine whether the user making this call is subject to |
| 77 | * teleportations. |
| 78 | * @return whether the user making this call is a goat |
| 79 | */ |
| 80 | public boolean isUserAGoat() { |
| 81 | return false; |
| 82 | } |
| 83 | |
Amith Yamasani | 258848d | 2012-08-10 17:06:33 -0700 | [diff] [blame] | 84 | /** |
| 85 | * Returns the UserInfo object describing a specific user. |
Amith Yamasani | 19526374 | 2012-08-21 15:40:12 -0700 | [diff] [blame] | 86 | * Requires {@link android.Manifest.permission#MANAGE_USERS} permission. |
Amith Yamasani | 258848d | 2012-08-10 17:06:33 -0700 | [diff] [blame] | 87 | * @param userHandle the user handle of the user whose information is being requested. |
| 88 | * @return the UserInfo object for a specific user. |
| 89 | * @hide |
| 90 | * */ |
| 91 | public UserInfo getUserInfo(int userHandle) { |
| 92 | try { |
| 93 | return mService.getUserInfo(userHandle); |
| 94 | } catch (RemoteException re) { |
| 95 | Log.w(TAG, "Could not get user info", re); |
| 96 | return null; |
| 97 | } |
| 98 | } |
| 99 | |
| 100 | /** |
| 101 | * Creates a user with the specified name and options. |
Amith Yamasani | 19526374 | 2012-08-21 15:40:12 -0700 | [diff] [blame] | 102 | * Requires {@link android.Manifest.permission#MANAGE_USERS} permission. |
Amith Yamasani | 258848d | 2012-08-10 17:06:33 -0700 | [diff] [blame] | 103 | * |
| 104 | * @param name the user's name |
| 105 | * @param flags flags that identify the type of user and other properties. |
| 106 | * @see UserInfo |
| 107 | * |
| 108 | * @return the UserInfo object for the created user, or null if the user could not be created. |
| 109 | * @hide |
| 110 | */ |
| 111 | public UserInfo createUser(String name, int flags) { |
| 112 | try { |
| 113 | return mService.createUser(name, flags); |
| 114 | } catch (RemoteException re) { |
| 115 | Log.w(TAG, "Could not create a user", re); |
| 116 | return null; |
| 117 | } |
| 118 | } |
| 119 | |
| 120 | /** |
| 121 | * Returns information for all users on this device. |
Amith Yamasani | 19526374 | 2012-08-21 15:40:12 -0700 | [diff] [blame] | 122 | * Requires {@link android.Manifest.permission#MANAGE_USERS} permission. |
Amith Yamasani | 258848d | 2012-08-10 17:06:33 -0700 | [diff] [blame] | 123 | * @return the list of users that were created. |
| 124 | * @hide |
| 125 | */ |
| 126 | public List<UserInfo> getUsers() { |
| 127 | try { |
Amith Yamasani | 920ace0 | 2012-09-20 22:15:37 -0700 | [diff] [blame] | 128 | return mService.getUsers(false); |
| 129 | } catch (RemoteException re) { |
| 130 | Log.w(TAG, "Could not get user list", re); |
| 131 | return null; |
| 132 | } |
| 133 | } |
| 134 | |
| 135 | /** |
| 136 | * Returns information for all users on this device. |
| 137 | * Requires {@link android.Manifest.permission#MANAGE_USERS} permission. |
| 138 | * @param excludeDying specify if the list should exclude users being removed. |
| 139 | * @return the list of users that were created. |
| 140 | * @hide |
| 141 | */ |
| 142 | public List<UserInfo> getUsers(boolean excludeDying) { |
| 143 | try { |
| 144 | return mService.getUsers(excludeDying); |
Amith Yamasani | 258848d | 2012-08-10 17:06:33 -0700 | [diff] [blame] | 145 | } catch (RemoteException re) { |
| 146 | Log.w(TAG, "Could not get user list", re); |
| 147 | return null; |
| 148 | } |
| 149 | } |
| 150 | |
| 151 | /** |
| 152 | * Removes a user and all associated data. |
Amith Yamasani | 19526374 | 2012-08-21 15:40:12 -0700 | [diff] [blame] | 153 | * Requires {@link android.Manifest.permission#MANAGE_USERS} permission. |
Amith Yamasani | 258848d | 2012-08-10 17:06:33 -0700 | [diff] [blame] | 154 | * @param userHandle the integer handle of the user, where 0 is the primary user. |
| 155 | * @hide |
| 156 | */ |
| 157 | public boolean removeUser(int userHandle) { |
| 158 | try { |
| 159 | return mService.removeUser(userHandle); |
| 160 | } catch (RemoteException re) { |
| 161 | Log.w(TAG, "Could not remove user ", re); |
| 162 | return false; |
| 163 | } |
| 164 | } |
| 165 | |
| 166 | /** |
| 167 | * Updates the user's name. |
Amith Yamasani | 19526374 | 2012-08-21 15:40:12 -0700 | [diff] [blame] | 168 | * Requires {@link android.Manifest.permission#MANAGE_USERS} permission. |
Amith Yamasani | 258848d | 2012-08-10 17:06:33 -0700 | [diff] [blame] | 169 | * |
| 170 | * @param userHandle the user's integer handle |
| 171 | * @param name the new name for the user |
| 172 | * @hide |
| 173 | */ |
| 174 | public void setUserName(int userHandle, String name) { |
| 175 | try { |
| 176 | mService.setUserName(userHandle, name); |
| 177 | } catch (RemoteException re) { |
| 178 | Log.w(TAG, "Could not set the user name ", re); |
| 179 | } |
| 180 | } |
| 181 | |
| 182 | /** |
Amith Yamasani | e928d7d | 2012-09-17 21:46:51 -0700 | [diff] [blame] | 183 | * Sets the user's photo. |
Amith Yamasani | 258848d | 2012-08-10 17:06:33 -0700 | [diff] [blame] | 184 | * @param userHandle the user for whom to change the photo. |
Amith Yamasani | e928d7d | 2012-09-17 21:46:51 -0700 | [diff] [blame] | 185 | * @param icon the bitmap to set as the photo. |
Amith Yamasani | 258848d | 2012-08-10 17:06:33 -0700 | [diff] [blame] | 186 | * @hide |
| 187 | */ |
Amith Yamasani | e928d7d | 2012-09-17 21:46:51 -0700 | [diff] [blame] | 188 | public void setUserIcon(int userHandle, Bitmap icon) { |
Amith Yamasani | 258848d | 2012-08-10 17:06:33 -0700 | [diff] [blame] | 189 | try { |
Amith Yamasani | e928d7d | 2012-09-17 21:46:51 -0700 | [diff] [blame] | 190 | mService.setUserIcon(userHandle, icon); |
Amith Yamasani | 258848d | 2012-08-10 17:06:33 -0700 | [diff] [blame] | 191 | } catch (RemoteException re) { |
| 192 | Log.w(TAG, "Could not set the user icon ", re); |
Amith Yamasani | 258848d | 2012-08-10 17:06:33 -0700 | [diff] [blame] | 193 | } |
| 194 | } |
| 195 | |
| 196 | /** |
Amith Yamasani | 3b49f07 | 2012-09-17 10:21:43 -0700 | [diff] [blame] | 197 | * Returns a file descriptor for the user's photo. PNG data can be read from this file. |
| 198 | * @param userHandle the user whose photo we want to read. |
Amith Yamasani | e928d7d | 2012-09-17 21:46:51 -0700 | [diff] [blame] | 199 | * @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] | 200 | * @hide |
| 201 | */ |
Amith Yamasani | e928d7d | 2012-09-17 21:46:51 -0700 | [diff] [blame] | 202 | public Bitmap getUserIcon(int userHandle) { |
Amith Yamasani | 3b49f07 | 2012-09-17 10:21:43 -0700 | [diff] [blame] | 203 | try { |
| 204 | return mService.getUserIcon(userHandle); |
| 205 | } catch (RemoteException re) { |
Amith Yamasani | e928d7d | 2012-09-17 21:46:51 -0700 | [diff] [blame] | 206 | Log.w(TAG, "Could not get the user icon ", re); |
Amith Yamasani | 3b49f07 | 2012-09-17 10:21:43 -0700 | [diff] [blame] | 207 | return null; |
| 208 | } |
| 209 | } |
| 210 | |
| 211 | /** |
Amith Yamasani | 258848d | 2012-08-10 17:06:33 -0700 | [diff] [blame] | 212 | * Enable or disable the use of a guest account. If disabled, the existing guest account |
| 213 | * will be wiped. |
Amith Yamasani | 19526374 | 2012-08-21 15:40:12 -0700 | [diff] [blame] | 214 | * Requires {@link android.Manifest.permission#MANAGE_USERS} permission. |
Amith Yamasani | 258848d | 2012-08-10 17:06:33 -0700 | [diff] [blame] | 215 | * @param enable whether to enable a guest account. |
| 216 | * @hide |
| 217 | */ |
| 218 | public void setGuestEnabled(boolean enable) { |
| 219 | try { |
| 220 | mService.setGuestEnabled(enable); |
| 221 | } catch (RemoteException re) { |
| 222 | Log.w(TAG, "Could not change guest account availability to " + enable); |
| 223 | } |
| 224 | } |
| 225 | |
| 226 | /** |
| 227 | * Checks if a guest user is enabled for this device. |
Amith Yamasani | 19526374 | 2012-08-21 15:40:12 -0700 | [diff] [blame] | 228 | * Requires {@link android.Manifest.permission#MANAGE_USERS} permission. |
Amith Yamasani | 258848d | 2012-08-10 17:06:33 -0700 | [diff] [blame] | 229 | * @return whether a guest user is enabled |
| 230 | * @hide |
| 231 | */ |
| 232 | public boolean isGuestEnabled() { |
| 233 | try { |
| 234 | return mService.isGuestEnabled(); |
| 235 | } catch (RemoteException re) { |
| 236 | Log.w(TAG, "Could not retrieve guest enabled state"); |
| 237 | return false; |
| 238 | } |
| 239 | } |
| 240 | |
| 241 | /** |
| 242 | * 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] | 243 | * Requires {@link android.Manifest.permission#MANAGE_USERS} permission. |
Amith Yamasani | 258848d | 2012-08-10 17:06:33 -0700 | [diff] [blame] | 244 | * @param userHandle |
| 245 | * @hide |
| 246 | */ |
| 247 | public void wipeUser(int userHandle) { |
| 248 | try { |
| 249 | mService.wipeUser(userHandle); |
| 250 | } catch (RemoteException re) { |
| 251 | Log.w(TAG, "Could not wipe user " + userHandle); |
| 252 | } |
| 253 | } |
| 254 | |
| 255 | /** |
| 256 | * Returns the maximum number of users that can be created on this device. A return value |
| 257 | * of 1 means that it is a single user device. |
| 258 | * @hide |
| 259 | * @return a value greater than or equal to 1 |
| 260 | */ |
Jeff Sharkey | 27bd34d | 2012-09-16 12:49:00 -0700 | [diff] [blame] | 261 | public static int getMaxSupportedUsers() { |
| 262 | return SystemProperties.getInt("fw.max_users", |
| 263 | Resources.getSystem().getInteger(R.integer.config_multiuserMaximumUsers)); |
Amith Yamasani | 258848d | 2012-08-10 17:06:33 -0700 | [diff] [blame] | 264 | } |
Amith Yamasani | 2a00329 | 2012-08-14 18:25:45 -0700 | [diff] [blame] | 265 | |
| 266 | /** |
| 267 | * Returns a serial number on this device for a given userHandle. User handles can be recycled |
| 268 | * when deleting and creating users, but serial numbers are not reused until the device is wiped. |
| 269 | * @param userHandle |
| 270 | * @return a serial number associated with that user, or -1 if the userHandle is not valid. |
| 271 | * @hide |
| 272 | */ |
| 273 | public int getUserSerialNumber(int userHandle) { |
| 274 | try { |
| 275 | return mService.getUserSerialNumber(userHandle); |
| 276 | } catch (RemoteException re) { |
| 277 | Log.w(TAG, "Could not get serial number for user " + userHandle); |
| 278 | } |
| 279 | return -1; |
| 280 | } |
| 281 | |
| 282 | /** |
| 283 | * Returns a userHandle on this device for a given user serial number. User handles can be |
| 284 | * recycled when deleting and creating users, but serial numbers are not reused until the device |
| 285 | * is wiped. |
| 286 | * @param userSerialNumber |
| 287 | * @return the userHandle associated with that user serial number, or -1 if the serial number |
| 288 | * is not valid. |
| 289 | * @hide |
| 290 | */ |
| 291 | public int getUserHandle(int userSerialNumber) { |
| 292 | try { |
| 293 | return mService.getUserHandle(userSerialNumber); |
| 294 | } catch (RemoteException re) { |
| 295 | Log.w(TAG, "Could not get userHandle for user " + userSerialNumber); |
| 296 | } |
| 297 | return -1; |
| 298 | } |
Amith Yamasani | 258848d | 2012-08-10 17:06:33 -0700 | [diff] [blame] | 299 | } |