blob: 96c96d76a423665249041d468349778bfbafa1a6 [file] [log] [blame]
Amith Yamasani258848d2012-08-10 17:06:33 -07001/*
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;
Amith Yamasanie928d7d2012-09-17 21:46:51 -070021import android.graphics.Bitmap;
Jeff Sharkey27bd34d2012-09-16 12:49:00 -070022import android.content.res.Resources;
Amith Yamasani258848d2012-08-10 17:06:33 -070023import 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.
Amith Yamasanie928d7d2012-09-17 21:46:51 -070045 * @hide
Amith Yamasani258848d2012-08-10 17:06:33 -070046 */
Jeff Sharkey4673e7e2012-09-19 13:14:30 -070047 public static boolean supportsMultipleUsers() {
Amith Yamasani258848d2012-08-10 17:06:33 -070048 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 Hackborn79af1dd2012-08-16 16:42:52 -070057 return UserHandle.myUserId();
Amith Yamasani258848d2012-08-10 17:06:33 -070058 }
59
60 /**
Dianne Hackborn8832c182012-09-17 17:20:24 -070061 * 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 Yamasani258848d2012-08-10 17:06:33 -070064 * @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 Morrille4ab16a2012-09-20 20:25:55 -070075 /**
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 Yamasani258848d2012-08-10 17:06:33 -070084 /**
85 * Returns the UserInfo object describing a specific user.
Amith Yamasani195263742012-08-21 15:40:12 -070086 * Requires {@link android.Manifest.permission#MANAGE_USERS} permission.
Amith Yamasani258848d2012-08-10 17:06:33 -070087 * @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 Yamasani195263742012-08-21 15:40:12 -0700102 * Requires {@link android.Manifest.permission#MANAGE_USERS} permission.
Amith Yamasani258848d2012-08-10 17:06:33 -0700103 *
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 Yamasani195263742012-08-21 15:40:12 -0700122 * Requires {@link android.Manifest.permission#MANAGE_USERS} permission.
Amith Yamasani258848d2012-08-10 17:06:33 -0700123 * @return the list of users that were created.
124 * @hide
125 */
126 public List<UserInfo> getUsers() {
127 try {
Amith Yamasani920ace02012-09-20 22:15:37 -0700128 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 Yamasani258848d2012-08-10 17:06:33 -0700145 } 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 Yamasani195263742012-08-21 15:40:12 -0700153 * Requires {@link android.Manifest.permission#MANAGE_USERS} permission.
Amith Yamasani258848d2012-08-10 17:06:33 -0700154 * @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 Yamasani195263742012-08-21 15:40:12 -0700168 * Requires {@link android.Manifest.permission#MANAGE_USERS} permission.
Amith Yamasani258848d2012-08-10 17:06:33 -0700169 *
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 Yamasanie928d7d2012-09-17 21:46:51 -0700183 * Sets the user's photo.
Amith Yamasani258848d2012-08-10 17:06:33 -0700184 * @param userHandle the user for whom to change the photo.
Amith Yamasanie928d7d2012-09-17 21:46:51 -0700185 * @param icon the bitmap to set as the photo.
Amith Yamasani258848d2012-08-10 17:06:33 -0700186 * @hide
187 */
Amith Yamasanie928d7d2012-09-17 21:46:51 -0700188 public void setUserIcon(int userHandle, Bitmap icon) {
Amith Yamasani258848d2012-08-10 17:06:33 -0700189 try {
Amith Yamasanie928d7d2012-09-17 21:46:51 -0700190 mService.setUserIcon(userHandle, icon);
Amith Yamasani258848d2012-08-10 17:06:33 -0700191 } catch (RemoteException re) {
192 Log.w(TAG, "Could not set the user icon ", re);
Amith Yamasani258848d2012-08-10 17:06:33 -0700193 }
194 }
195
196 /**
Amith Yamasani3b49f072012-09-17 10:21:43 -0700197 * 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 Yamasanie928d7d2012-09-17 21:46:51 -0700199 * @return a {@link Bitmap} of the user's photo, or null if there's no photo.
Amith Yamasani3b49f072012-09-17 10:21:43 -0700200 * @hide
201 */
Amith Yamasanie928d7d2012-09-17 21:46:51 -0700202 public Bitmap getUserIcon(int userHandle) {
Amith Yamasani3b49f072012-09-17 10:21:43 -0700203 try {
204 return mService.getUserIcon(userHandle);
205 } catch (RemoteException re) {
Amith Yamasanie928d7d2012-09-17 21:46:51 -0700206 Log.w(TAG, "Could not get the user icon ", re);
Amith Yamasani3b49f072012-09-17 10:21:43 -0700207 return null;
208 }
209 }
210
211 /**
Amith Yamasani258848d2012-08-10 17:06:33 -0700212 * Enable or disable the use of a guest account. If disabled, the existing guest account
213 * will be wiped.
Amith Yamasani195263742012-08-21 15:40:12 -0700214 * Requires {@link android.Manifest.permission#MANAGE_USERS} permission.
Amith Yamasani258848d2012-08-10 17:06:33 -0700215 * @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 Yamasani195263742012-08-21 15:40:12 -0700228 * Requires {@link android.Manifest.permission#MANAGE_USERS} permission.
Amith Yamasani258848d2012-08-10 17:06:33 -0700229 * @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 Yamasani195263742012-08-21 15:40:12 -0700243 * Requires {@link android.Manifest.permission#MANAGE_USERS} permission.
Amith Yamasani258848d2012-08-10 17:06:33 -0700244 * @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 Sharkey27bd34d2012-09-16 12:49:00 -0700261 public static int getMaxSupportedUsers() {
262 return SystemProperties.getInt("fw.max_users",
263 Resources.getSystem().getInteger(R.integer.config_multiuserMaximumUsers));
Amith Yamasani258848d2012-08-10 17:06:33 -0700264 }
Amith Yamasani2a003292012-08-14 18:25:45 -0700265
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 Yamasani258848d2012-08-10 17:06:33 -0700299}