UserManager.java revision 33f9cb8cf01e0a6288eb5b9ce724c56aa4e1e382
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 */ 16package android.os; 17 18import com.android.internal.R; 19import android.content.Context; 20import android.content.pm.UserInfo; 21import android.graphics.Bitmap; 22import android.content.res.Resources; 23import android.util.Log; 24 25import java.util.List; 26 27/** 28 * Manages users and user details on a multi-user system. 29 */ 30public 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. 45 * @hide 46 */ 47 public static boolean supportsMultipleUsers() { 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() { 57 return UserHandle.myUserId(); 58 } 59 60 /** 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. 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 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 84 /** 85 * Returns the UserInfo object describing a specific user. 86 * Requires {@link android.Manifest.permission#MANAGE_USERS} permission. 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 * Return the serial number for a user. This is a device-unique 102 * number assigned to that user; if the user is deleted and new users 103 * created, the new users will not be given the same serial number. 104 * @param user The user whose serial number is to be retrieved. 105 * @return The serial number of the given user. 106 * @see #getUserForSerialNumber(long) 107 */ 108 public long getSerialNumberForUser(UserHandle user) { 109 return getUserSerialNumber(user.getIdentifier()); 110 } 111 112 /** 113 * Return the user associated with a serial number previously 114 * returned by {@link #getSerialNumberForUser(UserHandle)}. 115 * @param serialNumber The serial number of the user that is being 116 * retrieved. 117 * @return Return the user associated with the serial number, or null 118 * if there is not one. 119 * @see #getSerialNumberForUser(UserHandle) 120 */ 121 public UserHandle getUserForSerialNumber(long serialNumber) { 122 int ident = getUserHandle((int)serialNumber); 123 return ident >= 0 ? new UserHandle(ident) : null; 124 } 125 126 /** 127 * Creates a user with the specified name and options. 128 * Requires {@link android.Manifest.permission#MANAGE_USERS} permission. 129 * 130 * @param name the user's name 131 * @param flags flags that identify the type of user and other properties. 132 * @see UserInfo 133 * 134 * @return the UserInfo object for the created user, or null if the user could not be created. 135 * @hide 136 */ 137 public UserInfo createUser(String name, int flags) { 138 try { 139 return mService.createUser(name, flags); 140 } catch (RemoteException re) { 141 Log.w(TAG, "Could not create a user", re); 142 return null; 143 } 144 } 145 146 /** 147 * Returns information for all users on this device. 148 * Requires {@link android.Manifest.permission#MANAGE_USERS} permission. 149 * @return the list of users that were created. 150 * @hide 151 */ 152 public List<UserInfo> getUsers() { 153 try { 154 return mService.getUsers(false); 155 } catch (RemoteException re) { 156 Log.w(TAG, "Could not get user list", re); 157 return null; 158 } 159 } 160 161 /** 162 * Returns information for all users on this device. 163 * Requires {@link android.Manifest.permission#MANAGE_USERS} permission. 164 * @param excludeDying specify if the list should exclude users being removed. 165 * @return the list of users that were created. 166 * @hide 167 */ 168 public List<UserInfo> getUsers(boolean excludeDying) { 169 try { 170 return mService.getUsers(excludeDying); 171 } catch (RemoteException re) { 172 Log.w(TAG, "Could not get user list", re); 173 return null; 174 } 175 } 176 177 /** 178 * Removes a user and all associated data. 179 * Requires {@link android.Manifest.permission#MANAGE_USERS} permission. 180 * @param userHandle the integer handle of the user, where 0 is the primary user. 181 * @hide 182 */ 183 public boolean removeUser(int userHandle) { 184 try { 185 return mService.removeUser(userHandle); 186 } catch (RemoteException re) { 187 Log.w(TAG, "Could not remove user ", re); 188 return false; 189 } 190 } 191 192 /** 193 * Updates the user's name. 194 * Requires {@link android.Manifest.permission#MANAGE_USERS} permission. 195 * 196 * @param userHandle the user's integer handle 197 * @param name the new name for the user 198 * @hide 199 */ 200 public void setUserName(int userHandle, String name) { 201 try { 202 mService.setUserName(userHandle, name); 203 } catch (RemoteException re) { 204 Log.w(TAG, "Could not set the user name ", re); 205 } 206 } 207 208 /** 209 * Sets the user's photo. 210 * @param userHandle the user for whom to change the photo. 211 * @param icon the bitmap to set as the photo. 212 * @hide 213 */ 214 public void setUserIcon(int userHandle, Bitmap icon) { 215 try { 216 mService.setUserIcon(userHandle, icon); 217 } catch (RemoteException re) { 218 Log.w(TAG, "Could not set the user icon ", re); 219 } 220 } 221 222 /** 223 * Returns a file descriptor for the user's photo. PNG data can be read from this file. 224 * @param userHandle the user whose photo we want to read. 225 * @return a {@link Bitmap} of the user's photo, or null if there's no photo. 226 * @hide 227 */ 228 public Bitmap getUserIcon(int userHandle) { 229 try { 230 return mService.getUserIcon(userHandle); 231 } catch (RemoteException re) { 232 Log.w(TAG, "Could not get the user icon ", re); 233 return null; 234 } 235 } 236 237 /** 238 * Enable or disable the use of a guest account. If disabled, the existing guest account 239 * will be wiped. 240 * Requires {@link android.Manifest.permission#MANAGE_USERS} permission. 241 * @param enable whether to enable a guest account. 242 * @hide 243 */ 244 public void setGuestEnabled(boolean enable) { 245 try { 246 mService.setGuestEnabled(enable); 247 } catch (RemoteException re) { 248 Log.w(TAG, "Could not change guest account availability to " + enable); 249 } 250 } 251 252 /** 253 * Checks if a guest user is enabled for this device. 254 * Requires {@link android.Manifest.permission#MANAGE_USERS} permission. 255 * @return whether a guest user is enabled 256 * @hide 257 */ 258 public boolean isGuestEnabled() { 259 try { 260 return mService.isGuestEnabled(); 261 } catch (RemoteException re) { 262 Log.w(TAG, "Could not retrieve guest enabled state"); 263 return false; 264 } 265 } 266 267 /** 268 * Wipes all the data for a user, but doesn't remove the user. 269 * Requires {@link android.Manifest.permission#MANAGE_USERS} permission. 270 * @param userHandle 271 * @hide 272 */ 273 public void wipeUser(int userHandle) { 274 try { 275 mService.wipeUser(userHandle); 276 } catch (RemoteException re) { 277 Log.w(TAG, "Could not wipe user " + userHandle); 278 } 279 } 280 281 /** 282 * Returns the maximum number of users that can be created on this device. A return value 283 * of 1 means that it is a single user device. 284 * @hide 285 * @return a value greater than or equal to 1 286 */ 287 public static int getMaxSupportedUsers() { 288 return SystemProperties.getInt("fw.max_users", 289 Resources.getSystem().getInteger(R.integer.config_multiuserMaximumUsers)); 290 } 291 292 /** 293 * Returns a serial number on this device for a given userHandle. User handles can be recycled 294 * when deleting and creating users, but serial numbers are not reused until the device is wiped. 295 * @param userHandle 296 * @return a serial number associated with that user, or -1 if the userHandle is not valid. 297 * @hide 298 */ 299 public int getUserSerialNumber(int userHandle) { 300 try { 301 return mService.getUserSerialNumber(userHandle); 302 } catch (RemoteException re) { 303 Log.w(TAG, "Could not get serial number for user " + userHandle); 304 } 305 return -1; 306 } 307 308 /** 309 * Returns a userHandle on this device for a given user serial number. User handles can be 310 * recycled when deleting and creating users, but serial numbers are not reused until the device 311 * is wiped. 312 * @param userSerialNumber 313 * @return the userHandle associated with that user serial number, or -1 if the serial number 314 * is not valid. 315 * @hide 316 */ 317 public int getUserHandle(int userSerialNumber) { 318 try { 319 return mService.getUserHandle(userSerialNumber); 320 } catch (RemoteException re) { 321 Log.w(TAG, "Could not get userHandle for user " + userSerialNumber); 322 } 323 return -1; 324 } 325} 326