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