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