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