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