UserManager.java revision 2a00329c6d55c6cd9166e01963d7410e95d80d21
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.util.Log; 22 23import java.util.List; 24 25/** 26 * Manages users and user details on a multi-user system. 27 */ 28public class UserManager { 29 30 private static String TAG = "UserManager"; 31 private final IUserManager mService; 32 private final Context mContext; 33 34 /** @hide */ 35 public UserManager(Context context, IUserManager service) { 36 mService = service; 37 mContext = context; 38 } 39 40 /** 41 * Returns whether the system supports multiple users. 42 * @return true if multiple users can be created, false if it is a single user device. 43 */ 44 public boolean supportsMultipleUsers() { 45 return getMaxSupportedUsers() > 1; 46 } 47 48 /** 49 * Returns the user handle for the user that this application is running for. 50 * @return the user handle of the user making this call. 51 * @hide 52 * */ 53 public int getUserHandle() { 54 return Process.myUserHandle(); 55 } 56 57 /** 58 * Returns the user name of the user making this call. 59 * @return the user name 60 */ 61 public String getUserName() { 62 try { 63 return mService.getUserInfo(getUserHandle()).name; 64 } catch (RemoteException re) { 65 Log.w(TAG, "Could not get user name", re); 66 return ""; 67 } 68 } 69 70 /** 71 * Returns the UserInfo object describing a specific user. 72 * @param userHandle the user handle of the user whose information is being requested. 73 * @return the UserInfo object for a specific user. 74 * @hide 75 * */ 76 public UserInfo getUserInfo(int userHandle) { 77 try { 78 return mService.getUserInfo(userHandle); 79 } catch (RemoteException re) { 80 Log.w(TAG, "Could not get user info", re); 81 return null; 82 } 83 } 84 85 /** 86 * Creates a user with the specified name and options. 87 * 88 * @param name the user's name 89 * @param flags flags that identify the type of user and other properties. 90 * @see UserInfo 91 * 92 * @return the UserInfo object for the created user, or null if the user could not be created. 93 * @hide 94 */ 95 public UserInfo createUser(String name, int flags) { 96 try { 97 return mService.createUser(name, flags); 98 } catch (RemoteException re) { 99 Log.w(TAG, "Could not create a user", re); 100 return null; 101 } 102 } 103 104 /** 105 * Returns information for all users on this device. 106 * @return the list of users that were created. 107 * @hide 108 */ 109 public List<UserInfo> getUsers() { 110 try { 111 return mService.getUsers(); 112 } catch (RemoteException re) { 113 Log.w(TAG, "Could not get user list", re); 114 return null; 115 } 116 } 117 118 /** 119 * Removes a user and all associated data. 120 * @param userHandle the integer handle of the user, where 0 is the primary user. 121 * @hide 122 */ 123 public boolean removeUser(int userHandle) { 124 try { 125 return mService.removeUser(userHandle); 126 } catch (RemoteException re) { 127 Log.w(TAG, "Could not remove user ", re); 128 return false; 129 } 130 } 131 132 /** 133 * Updates the user's name. 134 * 135 * @param userHandle the user's integer handle 136 * @param name the new name for the user 137 * @hide 138 */ 139 public void setUserName(int userHandle, String name) { 140 try { 141 mService.setUserName(userHandle, name); 142 } catch (RemoteException re) { 143 Log.w(TAG, "Could not set the user name ", re); 144 } 145 } 146 147 /** 148 * Returns a file descriptor for the user's photo. PNG data can be written into this file. 149 * @param userHandle the user for whom to change the photo. 150 * @return a {@link ParcelFileDescriptor} to which to write the photo. 151 * @hide 152 */ 153 public ParcelFileDescriptor setUserIcon(int userHandle) { 154 try { 155 return mService.setUserIcon(userHandle); 156 } catch (RemoteException re) { 157 Log.w(TAG, "Could not set the user icon ", re); 158 return null; 159 } 160 } 161 162 /** 163 * Enable or disable the use of a guest account. If disabled, the existing guest account 164 * will be wiped. 165 * @param enable whether to enable a guest account. 166 * @hide 167 */ 168 public void setGuestEnabled(boolean enable) { 169 try { 170 mService.setGuestEnabled(enable); 171 } catch (RemoteException re) { 172 Log.w(TAG, "Could not change guest account availability to " + enable); 173 } 174 } 175 176 /** 177 * Checks if a guest user is enabled for this device. 178 * @return whether a guest user is enabled 179 * @hide 180 */ 181 public boolean isGuestEnabled() { 182 try { 183 return mService.isGuestEnabled(); 184 } catch (RemoteException re) { 185 Log.w(TAG, "Could not retrieve guest enabled state"); 186 return false; 187 } 188 } 189 190 /** 191 * Wipes all the data for a user, but doesn't remove the user. 192 * @param userHandle 193 * @hide 194 */ 195 public void wipeUser(int userHandle) { 196 try { 197 mService.wipeUser(userHandle); 198 } catch (RemoteException re) { 199 Log.w(TAG, "Could not wipe user " + userHandle); 200 } 201 } 202 203 /** 204 * Returns the maximum number of users that can be created on this device. A return value 205 * of 1 means that it is a single user device. 206 * @hide 207 * @return a value greater than or equal to 1 208 */ 209 public int getMaxSupportedUsers() { 210 return mContext.getResources().getInteger(R.integer.config_multiuserMaximumUsers); 211 } 212 213 /** 214 * Returns a serial number on this device for a given userHandle. User handles can be recycled 215 * when deleting and creating users, but serial numbers are not reused until the device is wiped. 216 * @param userHandle 217 * @return a serial number associated with that user, or -1 if the userHandle is not valid. 218 * @hide 219 */ 220 public int getUserSerialNumber(int userHandle) { 221 try { 222 return mService.getUserSerialNumber(userHandle); 223 } catch (RemoteException re) { 224 Log.w(TAG, "Could not get serial number for user " + userHandle); 225 } 226 return -1; 227 } 228 229 /** 230 * Returns a userHandle on this device for a given user serial number. User handles can be 231 * recycled when deleting and creating users, but serial numbers are not reused until the device 232 * is wiped. 233 * @param userSerialNumber 234 * @return the userHandle associated with that user serial number, or -1 if the serial number 235 * is not valid. 236 * @hide 237 */ 238 public int getUserHandle(int userSerialNumber) { 239 try { 240 return mService.getUserHandle(userSerialNumber); 241 } catch (RemoteException re) { 242 Log.w(TAG, "Could not get userHandle for user " + userSerialNumber); 243 } 244 return -1; 245 } 246 247 248} 249