UserManager.java revision ff54920ed222a2bd6abe618743a5a3e9fe10bd4b
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;
19
20import android.app.ActivityManagerNative;
21import android.content.Context;
22import android.content.pm.UserInfo;
23import android.graphics.Bitmap;
24import android.content.res.Resources;
25import android.util.Log;
26
27import java.util.List;
28
29/**
30 * Manages users and user details on a multi-user system.
31 */
32public 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 new users
138     * 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.
141     * @see #getUserForSerialNumber(long)
142     */
143    public long getSerialNumberForUser(UserHandle user) {
144        return getUserSerialNumber(user.getIdentifier());
145    }
146
147    /**
148     * Return the user associated with a serial number previously
149     * returned by {@link #getSerialNumberForUser(UserHandle)}.
150     * @param serialNumber The serial number of the user that is being
151     * retrieved.
152     * @return Return the user associated with the serial number, or null
153     * if there is not one.
154     * @see #getSerialNumberForUser(UserHandle)
155     */
156    public UserHandle getUserForSerialNumber(long serialNumber) {
157        int ident = getUserHandle((int)serialNumber);
158        return ident >= 0 ? new UserHandle(ident) : null;
159    }
160
161    /**
162     * Creates a user with the specified name and options.
163     * Requires {@link android.Manifest.permission#MANAGE_USERS} permission.
164     *
165     * @param name the user's name
166     * @param flags flags that identify the type of user and other properties.
167     * @see UserInfo
168     *
169     * @return the UserInfo object for the created user, or null if the user could not be created.
170     * @hide
171     */
172    public UserInfo createUser(String name, int flags) {
173        try {
174            return mService.createUser(name, flags);
175        } catch (RemoteException re) {
176            Log.w(TAG, "Could not create a user", re);
177            return null;
178        }
179    }
180
181    /**
182     * Returns information for all users on this device.
183     * Requires {@link android.Manifest.permission#MANAGE_USERS} permission.
184     * @return the list of users that were created.
185     * @hide
186     */
187    public List<UserInfo> getUsers() {
188        try {
189            return mService.getUsers(false);
190        } catch (RemoteException re) {
191            Log.w(TAG, "Could not get user list", re);
192            return null;
193        }
194    }
195
196    /**
197     * Returns information for all users on this device.
198     * Requires {@link android.Manifest.permission#MANAGE_USERS} permission.
199     * @param excludeDying specify if the list should exclude users being removed.
200     * @return the list of users that were created.
201     * @hide
202     */
203    public List<UserInfo> getUsers(boolean excludeDying) {
204        try {
205            return mService.getUsers(excludeDying);
206        } catch (RemoteException re) {
207            Log.w(TAG, "Could not get user list", re);
208            return null;
209        }
210    }
211
212    /**
213     * Removes a user and all associated data.
214     * Requires {@link android.Manifest.permission#MANAGE_USERS} permission.
215     * @param userHandle the integer handle of the user, where 0 is the primary user.
216     * @hide
217     */
218    public boolean removeUser(int userHandle) {
219        try {
220            return mService.removeUser(userHandle);
221        } catch (RemoteException re) {
222            Log.w(TAG, "Could not remove user ", re);
223            return false;
224        }
225    }
226
227    /**
228     * Updates the user's name.
229     * Requires {@link android.Manifest.permission#MANAGE_USERS} permission.
230     *
231     * @param userHandle the user's integer handle
232     * @param name the new name for the user
233     * @hide
234     */
235    public void setUserName(int userHandle, String name) {
236        try {
237            mService.setUserName(userHandle, name);
238        } catch (RemoteException re) {
239            Log.w(TAG, "Could not set the user name ", re);
240        }
241    }
242
243    /**
244     * Sets the user's photo.
245     * @param userHandle the user for whom to change the photo.
246     * @param icon the bitmap to set as the photo.
247     * @hide
248     */
249    public void setUserIcon(int userHandle, Bitmap icon) {
250        try {
251            mService.setUserIcon(userHandle, icon);
252        } catch (RemoteException re) {
253            Log.w(TAG, "Could not set the user icon ", re);
254        }
255    }
256
257    /**
258     * Returns a file descriptor for the user's photo. PNG data can be read from this file.
259     * @param userHandle the user whose photo we want to read.
260     * @return a {@link Bitmap} of the user's photo, or null if there's no photo.
261     * @hide
262     */
263    public Bitmap getUserIcon(int userHandle) {
264        try {
265            return mService.getUserIcon(userHandle);
266        } catch (RemoteException re) {
267            Log.w(TAG, "Could not get the user icon ", re);
268            return null;
269        }
270    }
271
272    /**
273     * Enable or disable the use of a guest account. If disabled, the existing guest account
274     * will be wiped.
275     * Requires {@link android.Manifest.permission#MANAGE_USERS} permission.
276     * @param enable whether to enable a guest account.
277     * @hide
278     */
279    public void setGuestEnabled(boolean enable) {
280        try {
281            mService.setGuestEnabled(enable);
282        } catch (RemoteException re) {
283            Log.w(TAG, "Could not change guest account availability to " + enable);
284        }
285    }
286
287    /**
288     * Checks if a guest user is enabled for this device.
289     * Requires {@link android.Manifest.permission#MANAGE_USERS} permission.
290     * @return whether a guest user is enabled
291     * @hide
292     */
293    public boolean isGuestEnabled() {
294        try {
295            return mService.isGuestEnabled();
296        } catch (RemoteException re) {
297            Log.w(TAG, "Could not retrieve guest enabled state");
298            return false;
299        }
300    }
301
302    /**
303     * Wipes all the data for a user, but doesn't remove the user.
304     * Requires {@link android.Manifest.permission#MANAGE_USERS} permission.
305     * @param userHandle
306     * @hide
307     */
308    public void wipeUser(int userHandle) {
309        try {
310            mService.wipeUser(userHandle);
311        } catch (RemoteException re) {
312            Log.w(TAG, "Could not wipe user " + userHandle);
313        }
314    }
315
316    /**
317     * Returns the maximum number of users that can be created on this device. A return value
318     * of 1 means that it is a single user device.
319     * @hide
320     * @return a value greater than or equal to 1
321     */
322    public static int getMaxSupportedUsers() {
323        // Don't allow multiple users on certain builds
324        if (android.os.Build.ID.startsWith("JVP")) return 1;
325        return SystemProperties.getInt("fw.max_users",
326                Resources.getSystem().getInteger(R.integer.config_multiuserMaximumUsers));
327    }
328
329    /**
330     * Returns a serial number on this device for a given userHandle. User handles can be recycled
331     * when deleting and creating users, but serial numbers are not reused until the device is wiped.
332     * @param userHandle
333     * @return a serial number associated with that user, or -1 if the userHandle is not valid.
334     * @hide
335     */
336    public int getUserSerialNumber(int userHandle) {
337        try {
338            return mService.getUserSerialNumber(userHandle);
339        } catch (RemoteException re) {
340            Log.w(TAG, "Could not get serial number for user " + userHandle);
341        }
342        return -1;
343    }
344
345    /**
346     * Returns a userHandle on this device for a given user serial number. User handles can be
347     * recycled when deleting and creating users, but serial numbers are not reused until the device
348     * is wiped.
349     * @param userSerialNumber
350     * @return the userHandle associated with that user serial number, or -1 if the serial number
351     * is not valid.
352     * @hide
353     */
354    public int getUserHandle(int userSerialNumber) {
355        try {
356            return mService.getUserHandle(userSerialNumber);
357        } catch (RemoteException re) {
358            Log.w(TAG, "Could not get userHandle for user " + userSerialNumber);
359        }
360        return -1;
361    }
362}
363