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 20 import android.app.ActivityManagerNative; 21 import android.content.Context; 22 import android.content.pm.UserInfo; 23 import android.graphics.Bitmap; 24 import android.content.res.Resources; 25 import android.util.Log; 26 27 import java.util.List; 28 29 /** 30 * Manages users and user details on a multi-user system. 31 */ 32 public class UserManager { 33 34 private static String TAG = "UserManager"; 35 private final IUserManager mService; 36 private final Context mContext; 37 38 /** @hide */ 39 public UserManager(Context context, IUserManager service) { 40 mService = service; 41 mContext = context; 42 } 43 44 /** 45 * Returns whether the system supports multiple users. 46 * @return true if multiple users can be created, false if it is a single user device. 47 * @hide 48 */ 49 public static boolean supportsMultipleUsers() { 50 return getMaxSupportedUsers() > 1; 51 } 52 53 /** 54 * Returns the user handle for the user that this application is running for. 55 * @return the user handle of the user making this call. 56 * @hide 57 * */ 58 public int getUserHandle() { 59 return UserHandle.myUserId(); 60 } 61 62 /** 63 * Returns the user name of the user making this call. This call is only 64 * available to applications on the system image; it requires the 65 * MANAGE_USERS permission. 66 * @return the user name 67 */ 68 public String getUserName() { 69 try { 70 return mService.getUserInfo(getUserHandle()).name; 71 } catch (RemoteException re) { 72 Log.w(TAG, "Could not get user name", re); 73 return ""; 74 } 75 } 76 77 /** 78 * Used to determine whether the user making this call is subject to 79 * teleportations. 80 * @return whether the user making this call is a goat 81 */ 82 public boolean isUserAGoat() { 83 return false; 84 } 85 86 /** 87 * Return whether the given user is actively running. This means that 88 * the user is in the "started" state, not "stopped" -- it is currently 89 * allowed to run code through scheduled alarms, receiving broadcasts, 90 * etc. A started user may be either the current foreground user or a 91 * background user; the result here does not distinguish between the two. 92 * @param user The user to retrieve the running state for. 93 */ 94 public boolean isUserRunning(UserHandle user) { 95 try { 96 return ActivityManagerNative.getDefault().isUserRunning( 97 user.getIdentifier(), false); 98 } catch (RemoteException e) { 99 return false; 100 } 101 } 102 103 /** 104 * Return whether the given user is actively running <em>or</em> stopping. 105 * This is like {@link #isUserRunning(UserHandle)}, but will also return 106 * true if the user had been running but is in the process of being stopped 107 * (but is not yet fully stopped, and still running some code). 108 * @param user The user to retrieve the running state for. 109 */ 110 public boolean isUserRunningOrStopping(UserHandle user) { 111 try { 112 return ActivityManagerNative.getDefault().isUserRunning( 113 user.getIdentifier(), true); 114 } catch (RemoteException e) { 115 return false; 116 } 117 } 118 119 /** 120 * Returns the UserInfo object describing a specific user. 121 * Requires {@link android.Manifest.permission#MANAGE_USERS} permission. 122 * @param userHandle the user handle of the user whose information is being requested. 123 * @return the UserInfo object for a specific user. 124 * @hide 125 */ 126 public UserInfo getUserInfo(int userHandle) { 127 try { 128 return mService.getUserInfo(userHandle); 129 } catch (RemoteException re) { 130 Log.w(TAG, "Could not get user info", re); 131 return null; 132 } 133 } 134 135 /** 136 * Return the serial number for a user. This is a device-unique 137 * number assigned to that user; if the user is deleted and then a new 138 * user created, the new users will not be given the same serial number. 139 * @param user The user whose serial number is to be retrieved. 140 * @return The serial number of the given user; returns -1 if the 141 * given UserHandle does not exist. 142 * @see #getUserForSerialNumber(long) 143 */ 144 public long getSerialNumberForUser(UserHandle user) { 145 return getUserSerialNumber(user.getIdentifier()); 146 } 147 148 /** 149 * Return the user associated with a serial number previously 150 * returned by {@link #getSerialNumberForUser(UserHandle)}. 151 * @param serialNumber The serial number of the user that is being 152 * retrieved. 153 * @return Return the user associated with the serial number, or null 154 * if there is not one. 155 * @see #getSerialNumberForUser(UserHandle) 156 */ 157 public UserHandle getUserForSerialNumber(long serialNumber) { 158 int ident = getUserHandle((int)serialNumber); 159 return ident >= 0 ? new UserHandle(ident) : null; 160 } 161 162 /** 163 * Creates a user with the specified name and options. 164 * Requires {@link android.Manifest.permission#MANAGE_USERS} permission. 165 * 166 * @param name the user's name 167 * @param flags flags that identify the type of user and other properties. 168 * @see UserInfo 169 * 170 * @return the UserInfo object for the created user, or null if the user could not be created. 171 * @hide 172 */ 173 public UserInfo createUser(String name, int flags) { 174 try { 175 return mService.createUser(name, flags); 176 } catch (RemoteException re) { 177 Log.w(TAG, "Could not create a user", re); 178 return null; 179 } 180 } 181 182 /** 183 * Return the number of users currently created on the device. 184 */ 185 public int getUserCount() { 186 List<UserInfo> users = getUsers(); 187 return users != null ? users.size() : 1; 188 } 189 190 /** 191 * Returns information for all users on this device. 192 * Requires {@link android.Manifest.permission#MANAGE_USERS} permission. 193 * @return the list of users that were created. 194 * @hide 195 */ 196 public List<UserInfo> getUsers() { 197 try { 198 return mService.getUsers(false); 199 } catch (RemoteException re) { 200 Log.w(TAG, "Could not get user list", re); 201 return null; 202 } 203 } 204 205 /** 206 * Returns information for all users on this device. 207 * Requires {@link android.Manifest.permission#MANAGE_USERS} permission. 208 * @param excludeDying specify if the list should exclude users being removed. 209 * @return the list of users that were created. 210 * @hide 211 */ 212 public List<UserInfo> getUsers(boolean excludeDying) { 213 try { 214 return mService.getUsers(excludeDying); 215 } catch (RemoteException re) { 216 Log.w(TAG, "Could not get user list", re); 217 return null; 218 } 219 } 220 221 /** 222 * Removes a user and all associated data. 223 * Requires {@link android.Manifest.permission#MANAGE_USERS} permission. 224 * @param userHandle the integer handle of the user, where 0 is the primary user. 225 * @hide 226 */ 227 public boolean removeUser(int userHandle) { 228 try { 229 return mService.removeUser(userHandle); 230 } catch (RemoteException re) { 231 Log.w(TAG, "Could not remove user ", re); 232 return false; 233 } 234 } 235 236 /** 237 * Updates the user's name. 238 * Requires {@link android.Manifest.permission#MANAGE_USERS} permission. 239 * 240 * @param userHandle the user's integer handle 241 * @param name the new name for the user 242 * @hide 243 */ 244 public void setUserName(int userHandle, String name) { 245 try { 246 mService.setUserName(userHandle, name); 247 } catch (RemoteException re) { 248 Log.w(TAG, "Could not set the user name ", re); 249 } 250 } 251 252 /** 253 * Sets the user's photo. 254 * @param userHandle the user for whom to change the photo. 255 * @param icon the bitmap to set as the photo. 256 * @hide 257 */ 258 public void setUserIcon(int userHandle, Bitmap icon) { 259 try { 260 mService.setUserIcon(userHandle, icon); 261 } catch (RemoteException re) { 262 Log.w(TAG, "Could not set the user icon ", re); 263 } 264 } 265 266 /** 267 * Returns a file descriptor for the user's photo. PNG data can be read from this file. 268 * @param userHandle the user whose photo we want to read. 269 * @return a {@link Bitmap} of the user's photo, or null if there's no photo. 270 * @hide 271 */ 272 public Bitmap getUserIcon(int userHandle) { 273 try { 274 return mService.getUserIcon(userHandle); 275 } catch (RemoteException re) { 276 Log.w(TAG, "Could not get the user icon ", re); 277 return null; 278 } 279 } 280 281 /** 282 * Enable or disable the use of a guest account. If disabled, the existing guest account 283 * will be wiped. 284 * Requires {@link android.Manifest.permission#MANAGE_USERS} permission. 285 * @param enable whether to enable a guest account. 286 * @hide 287 */ 288 public void setGuestEnabled(boolean enable) { 289 try { 290 mService.setGuestEnabled(enable); 291 } catch (RemoteException re) { 292 Log.w(TAG, "Could not change guest account availability to " + enable); 293 } 294 } 295 296 /** 297 * Checks if a guest user is enabled for this device. 298 * Requires {@link android.Manifest.permission#MANAGE_USERS} permission. 299 * @return whether a guest user is enabled 300 * @hide 301 */ 302 public boolean isGuestEnabled() { 303 try { 304 return mService.isGuestEnabled(); 305 } catch (RemoteException re) { 306 Log.w(TAG, "Could not retrieve guest enabled state"); 307 return false; 308 } 309 } 310 311 /** 312 * Wipes all the data for a user, but doesn't remove the user. 313 * Requires {@link android.Manifest.permission#MANAGE_USERS} permission. 314 * @param userHandle 315 * @hide 316 */ 317 public void wipeUser(int userHandle) { 318 try { 319 mService.wipeUser(userHandle); 320 } catch (RemoteException re) { 321 Log.w(TAG, "Could not wipe user " + userHandle); 322 } 323 } 324 325 /** 326 * Returns the maximum number of users that can be created on this device. A return value 327 * of 1 means that it is a single user device. 328 * @hide 329 * @return a value greater than or equal to 1 330 */ 331 public static int getMaxSupportedUsers() { 332 // Don't allow multiple users on certain builds 333 if (android.os.Build.ID.startsWith("JVP")) return 1; 334 return SystemProperties.getInt("fw.max_users", 335 Resources.getSystem().getInteger(R.integer.config_multiuserMaximumUsers)); 336 } 337 338 /** 339 * Returns a serial number on this device for a given userHandle. User handles can be recycled 340 * when deleting and creating users, but serial numbers are not reused until the device is wiped. 341 * @param userHandle 342 * @return a serial number associated with that user, or -1 if the userHandle is not valid. 343 * @hide 344 */ 345 public int getUserSerialNumber(int userHandle) { 346 try { 347 return mService.getUserSerialNumber(userHandle); 348 } catch (RemoteException re) { 349 Log.w(TAG, "Could not get serial number for user " + userHandle); 350 } 351 return -1; 352 } 353 354 /** 355 * Returns a userHandle on this device for a given user serial number. User handles can be 356 * recycled when deleting and creating users, but serial numbers are not reused until the device 357 * is wiped. 358 * @param userSerialNumber 359 * @return the userHandle associated with that user serial number, or -1 if the serial number 360 * is not valid. 361 * @hide 362 */ 363 public int getUserHandle(int userSerialNumber) { 364 try { 365 return mService.getUserHandle(userSerialNumber); 366 } catch (RemoteException re) { 367 Log.w(TAG, "Could not get userHandle for user " + userSerialNumber); 368 } 369 return -1; 370 } 371 } 372