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