UserManager.java revision be81c800ae6216e30b6008b4c73172b36531c405
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.pm.UserInfo;
21import android.content.res.Resources;
22import android.graphics.Bitmap;
23import android.graphics.Canvas;
24import android.graphics.Bitmap.Config;
25import android.graphics.Rect;
26import android.graphics.drawable.BitmapDrawable;
27import android.graphics.drawable.Drawable;
28import android.util.Log;
29
30import com.android.internal.R;
31
32import java.util.ArrayList;
33import java.util.List;
34
35/**
36 * Manages users and user details on a multi-user system.
37 */
38public class UserManager {
39
40    private static String TAG = "UserManager";
41    private final IUserManager mService;
42    private final Context mContext;
43
44    /**
45     * Key for user restrictions. Specifies if a user is disallowed from adding and removing
46     * accounts.
47     * The default value is <code>false</code>.
48     * <p/>
49     * Type: Boolean
50     * @see #setUserRestrictions(Bundle)
51     * @see #getUserRestrictions()
52     */
53    public static final String DISALLOW_MODIFY_ACCOUNTS = "no_modify_accounts";
54
55    /**
56     * Key for user restrictions. Specifies if a user is disallowed from changing Wi-Fi
57     * access points.
58     * The default value is <code>false</code>.
59     * <p/>
60     * Type: Boolean
61     * @see #setUserRestrictions(Bundle)
62     * @see #getUserRestrictions()
63     */
64    public static final String DISALLOW_CONFIG_WIFI = "no_config_wifi";
65
66    /**
67     * Key for user restrictions. Specifies if a user is disallowed from installing applications.
68     * The default value is <code>false</code>.
69     * <p/>
70     * Type: Boolean
71     * @see #setUserRestrictions(Bundle)
72     * @see #getUserRestrictions()
73     */
74    public static final String DISALLOW_INSTALL_APPS = "no_install_apps";
75
76    /**
77     * Key for user restrictions. Specifies if a user is disallowed from uninstalling applications.
78     * The default value is <code>false</code>.
79     * <p/>
80     * Type: Boolean
81     * @see #setUserRestrictions(Bundle)
82     * @see #getUserRestrictions()
83     */
84    public static final String DISALLOW_UNINSTALL_APPS = "no_uninstall_apps";
85
86    /**
87     * Key for user restrictions. Specifies if a user is disallowed from toggling location sharing.
88     * The default value is <code>false</code>.
89     * <p/>
90     * Type: Boolean
91     * @see #setUserRestrictions(Bundle)
92     * @see #getUserRestrictions()
93     */
94    public static final String DISALLOW_SHARE_LOCATION = "no_share_location";
95
96    /**
97     * Key for user restrictions. Specifies if a user is disallowed from enabling the
98     * "Unknown Sources" setting, that allows installation of apps from unknown sources.
99     * The default value is <code>false</code>.
100     * <p/>
101     * Type: Boolean
102     * @see #setUserRestrictions(Bundle)
103     * @see #getUserRestrictions()
104     */
105    public static final String DISALLOW_INSTALL_UNKNOWN_SOURCES = "no_install_unknown_sources";
106
107    /**
108     * Key for user restrictions. Specifies if a user is disallowed from configuring bluetooth.
109     * The default value is <code>false</code>.
110     * <p/>
111     * Type: Boolean
112     * @see #setUserRestrictions(Bundle)
113     * @see #getUserRestrictions()
114     */
115    public static final String DISALLOW_CONFIG_BLUETOOTH = "no_config_bluetooth";
116
117    /**
118     * Key for user restrictions. Specifies if a user is disallowed from transferring files over
119     * USB. The default value is <code>false</code>.
120     * <p/>
121     * Type: Boolean
122     * @see #setUserRestrictions(Bundle)
123     * @see #getUserRestrictions()
124     */
125    public static final String DISALLOW_USB_FILE_TRANSFER = "no_usb_file_transfer";
126
127    /**
128     * Key for user restrictions. Specifies if a user is disallowed from configuring user
129     * credentials. The default value is <code>false</code>.
130     * <p/>
131     * Type: Boolean
132     * @see #setUserRestrictions(Bundle)
133     * @see #getUserRestrictions()
134     */
135    public static final String DISALLOW_CONFIG_CREDENTIALS = "no_config_credentials";
136
137    /**
138     * Key for user restrictions. Specifies if a user is disallowed from removing users.
139     * The default value is <code>false</code>.
140     * <p/>
141     * Type: Boolean
142     * @see #setUserRestrictions(Bundle)
143     * @see #getUserRestrictions()
144     */
145    public static final String DISALLOW_REMOVE_USER = "no_remove_user";
146
147    /**
148     * Key for user restrictions. Specifies if a user is disallowed from enabling or
149     * accessing debugging features. The default value is <code>false</code>.
150     * <p/>
151     * Type: Boolean
152     * @see #setUserRestrictions(Bundle)
153     * @see #getUserRestrictions()
154     */
155    public static final String DISALLOW_DEBUGGING_FEATURES = "no_debugging_features";
156
157    /**
158     * Key for user restrictions. Specifies if a user is disallowed from configuring VPN.
159     * The default value is <code>false</code>.
160     * <p/>
161     * Type: Boolean
162     * @see #setUserRestrictions(Bundle)
163     * @see #getUserRestrictions()
164     */
165    public static final String DISALLOW_CONFIG_VPN = "no_config_vpn";
166
167    /**
168     * Key for user restrictions. Specifies if a user is disallowed from configuring Tethering
169     * & portable hotspots. The default value is <code>false</code>.
170     * <p/>
171     * Type: Boolean
172     * @see #setUserRestrictions(Bundle)
173     * @see #getUserRestrictions()
174     */
175    public static final String DISALLOW_CONFIG_TETHERING = "no_config_tethering";
176
177    /**
178     * Key for user restrictions. Specifies if a user is disallowed from factory resetting
179     * from Settings.
180     * The default value is <code>false</code>.
181     * <p>
182     * @see #setUserRestrictions(Bundle)
183     * @see #getUserRestrictions()
184     */
185    public static final String DISALLOW_FACTORY_RESET = "no_factory_reset";
186
187    /**
188     * Key for user restrictions. Specifies if a user is disallowed from adding new users and
189     * profiles. The default value is <code>false</code>.
190     * <p>
191     * Type: Boolean
192     * @see #setUserRestrictions(Bundle)
193     * @see #getUserRestrictions()
194     */
195    public static final String DISALLOW_ADD_USER = "no_add_user";
196
197    /**
198     * Key for user restrictions. Specifies if a user is disallowed from disabling application
199     * verification. The default value is <code>false</code>.
200     * <p>
201     * Type: Boolean
202     * @see #setUserRestrictions(Bundle)
203     * @see #getUserRestrictions()
204     */
205    public static final String ENSURE_VERIFY_APPS = "ensure_verify_apps";
206
207    /**
208     * Key for user restrictions. Specifies if a user is disallowed from configuring cell
209     * broadcasts. The default value is <code>false</code>.
210     * <p>
211     * Type: Boolean
212     * @see #setUserRestrictions(Bundle)
213     * @see #getUserRestrictions()
214     */
215    public static final String DISALLOW_CONFIG_CELL_BROADCASTS = "no_config_cell_broadcasts";
216
217    /**
218     * Key for user restrictions. Specifies if a user is disallowed from configuring mobile
219     * networks. The default value is <code>false</code>.
220     * <p>
221     * Type: Boolean
222     * @see #setUserRestrictions(Bundle)
223     * @see #getUserRestrictions()
224     */
225    public static final String DISALLOW_CONFIG_MOBILE_NETWORKS = "no_config_mobile_networks";
226
227    /**
228     * Key for user restrictions. Specifies if a user is disallowed from configuring
229     * applications in Settings. The default value is <code>false</code>.
230     * <p>
231     * Type: Boolean
232     * @see #setUserRestrictions(Bundle)
233     * @see #getUserRestrictions()
234     */
235    public static final String DISALLOW_CONFIG_APPS = "no_config_apps";
236
237    /**
238     * Key for user restrictions. Specifies if a user is disallowed from mounting
239     * physical external media. The default value is <code>false</code>.
240     * <p/>
241     * Type: Boolean
242     * @see #setUserRestrictions(Bundle)
243     * @see #getUserRestrictions()
244     */
245    public static final String DISALLOW_MOUNT_PHYSICAL_MEDIA = "no_physical_media";
246
247    /**
248     * Key for user restrictions. Specifies if a user is disallowed from adjusting microphone
249     * volume.
250     * The default value is <code>false</code>.
251     * <p/>
252     * Type: Boolean
253     * @see #setUserRestrictions(Bundle)
254     * @see #getUserRestrictions()
255     */
256    public static final String DISALLOW_UNMUTE_MICROPHONE = "no_unmute_microphone";
257
258    /**
259     * Key for user restrictions. Specifies if a user is disallowed from adjusting the master
260     * volume.
261     * The default value is <code>false</code>.
262     * <p/>
263     * Type: Boolean
264     * @see #setUserRestrictions(Bundle)
265     * @see #getUserRestrictions()
266     */
267    public static final String DISALLOW_ADJUST_VOLUME = "no_adjust_volume";
268
269    /** @hide */
270    public static final int PIN_VERIFICATION_FAILED_INCORRECT = -3;
271    /** @hide */
272    public static final int PIN_VERIFICATION_FAILED_NOT_SET = -2;
273    /** @hide */
274    public static final int PIN_VERIFICATION_SUCCESS = -1;
275
276    private static UserManager sInstance = null;
277
278    /** @hide */
279    public synchronized static UserManager get(Context context) {
280        if (sInstance == null) {
281            sInstance = (UserManager) context.getSystemService(Context.USER_SERVICE);
282        }
283        return sInstance;
284    }
285
286    /** @hide */
287    public UserManager(Context context, IUserManager service) {
288        mService = service;
289        mContext = context;
290    }
291
292    /**
293     * Returns whether the system supports multiple users.
294     * @return true if multiple users can be created by user, false if it is a single user device.
295     * @hide
296     */
297    public static boolean supportsMultipleUsers() {
298        return getMaxSupportedUsers() > 1
299                && SystemProperties.getBoolean("fw.show_multiuserui",
300                Resources.getSystem().getBoolean(R.bool.config_enableMultiUserUI));
301    }
302
303    /**
304     * Returns the user handle for the user that the calling process is running on.
305     *
306     * @return the user handle of the user making this call.
307     * @hide
308     */
309    public int getUserHandle() {
310        return UserHandle.myUserId();
311    }
312
313    /**
314     * Returns the user name of the user making this call.  This call is only
315     * available to applications on the system image; it requires the
316     * MANAGE_USERS permission.
317     * @return the user name
318     */
319    public String getUserName() {
320        try {
321            return mService.getUserInfo(getUserHandle()).name;
322        } catch (RemoteException re) {
323            Log.w(TAG, "Could not get user name", re);
324            return "";
325        }
326    }
327
328   /**
329     * Used to determine whether the user making this call is subject to
330     * teleportations.
331     * @return whether the user making this call is a goat
332     */
333    public boolean isUserAGoat() {
334        return false;
335    }
336
337    /**
338     * Used to check if the user making this call is linked to another user. Linked users may have
339     * a reduced number of available apps, app restrictions and account restrictions.
340     * @return whether the user making this call is a linked user
341     * @hide
342     */
343    public boolean isLinkedUser() {
344        try {
345            return mService.isRestricted();
346        } catch (RemoteException re) {
347            Log.w(TAG, "Could not check if user is limited ", re);
348            return false;
349        }
350    }
351
352    /**
353     * Return whether the given user is actively running.  This means that
354     * the user is in the "started" state, not "stopped" -- it is currently
355     * allowed to run code through scheduled alarms, receiving broadcasts,
356     * etc.  A started user may be either the current foreground user or a
357     * background user; the result here does not distinguish between the two.
358     * @param user The user to retrieve the running state for.
359     */
360    public boolean isUserRunning(UserHandle user) {
361        try {
362            return ActivityManagerNative.getDefault().isUserRunning(
363                    user.getIdentifier(), false);
364        } catch (RemoteException e) {
365            return false;
366        }
367    }
368
369    /**
370     * Return whether the given user is actively running <em>or</em> stopping.
371     * This is like {@link #isUserRunning(UserHandle)}, but will also return
372     * true if the user had been running but is in the process of being stopped
373     * (but is not yet fully stopped, and still running some code).
374     * @param user The user to retrieve the running state for.
375     */
376    public boolean isUserRunningOrStopping(UserHandle user) {
377        try {
378            return ActivityManagerNative.getDefault().isUserRunning(
379                    user.getIdentifier(), true);
380        } catch (RemoteException e) {
381            return false;
382        }
383    }
384
385    /**
386     * Returns the UserInfo object describing a specific user.
387     * Requires {@link android.Manifest.permission#MANAGE_USERS} permission.
388     * @param userHandle the user handle of the user whose information is being requested.
389     * @return the UserInfo object for a specific user.
390     * @hide
391     */
392    public UserInfo getUserInfo(int userHandle) {
393        try {
394            return mService.getUserInfo(userHandle);
395        } catch (RemoteException re) {
396            Log.w(TAG, "Could not get user info", re);
397            return null;
398        }
399    }
400
401    /**
402     * Returns the user-wide restrictions imposed on this user.
403     * @return a Bundle containing all the restrictions.
404     */
405    public Bundle getUserRestrictions() {
406        return getUserRestrictions(Process.myUserHandle());
407    }
408
409    /**
410     * Returns the user-wide restrictions imposed on the user specified by <code>userHandle</code>.
411     * @param userHandle the UserHandle of the user for whom to retrieve the restrictions.
412     * @return a Bundle containing all the restrictions.
413     */
414    public Bundle getUserRestrictions(UserHandle userHandle) {
415        try {
416            return mService.getUserRestrictions(userHandle.getIdentifier());
417        } catch (RemoteException re) {
418            Log.w(TAG, "Could not get user restrictions", re);
419            return Bundle.EMPTY;
420        }
421    }
422
423    /**
424     * Sets all the user-wide restrictions for this user.
425     * Requires the MANAGE_USERS permission.
426     * @param restrictions the Bundle containing all the restrictions.
427     */
428    public void setUserRestrictions(Bundle restrictions) {
429        setUserRestrictions(restrictions, Process.myUserHandle());
430    }
431
432    /**
433     * Sets all the user-wide restrictions for the specified user.
434     * Requires the MANAGE_USERS permission.
435     * @param restrictions the Bundle containing all the restrictions.
436     * @param userHandle the UserHandle of the user for whom to set the restrictions.
437     */
438    public void setUserRestrictions(Bundle restrictions, UserHandle userHandle) {
439        try {
440            mService.setUserRestrictions(restrictions, userHandle.getIdentifier());
441        } catch (RemoteException re) {
442            Log.w(TAG, "Could not set user restrictions", re);
443        }
444    }
445
446    /**
447     * Sets the value of a specific restriction.
448     * Requires the MANAGE_USERS permission.
449     * @param key the key of the restriction
450     * @param value the value for the restriction
451     */
452    public void setUserRestriction(String key, boolean value) {
453        Bundle bundle = getUserRestrictions();
454        bundle.putBoolean(key, value);
455        setUserRestrictions(bundle);
456    }
457
458    /**
459     * @hide
460     * Sets the value of a specific restriction on a specific user.
461     * Requires the MANAGE_USERS permission.
462     * @param key the key of the restriction
463     * @param value the value for the restriction
464     * @param userHandle the user whose restriction is to be changed.
465     */
466    public void setUserRestriction(String key, boolean value, UserHandle userHandle) {
467        Bundle bundle = getUserRestrictions(userHandle);
468        bundle.putBoolean(key, value);
469        setUserRestrictions(bundle, userHandle);
470    }
471
472    /**
473     * @hide
474     * Returns whether the current user has been disallowed from performing certain actions
475     * or setting certain settings.
476     * @param restrictionKey the string key representing the restriction
477     */
478    public boolean hasUserRestriction(String restrictionKey) {
479        return hasUserRestriction(restrictionKey, Process.myUserHandle());
480    }
481
482    /**
483     * @hide
484     * Returns whether the given user has been disallowed from performing certain actions
485     * or setting certain settings.
486     * @param restrictionKey the string key representing the restriction
487     * @param userHandle the UserHandle of the user for whom to retrieve the restrictions.
488     */
489    public boolean hasUserRestriction(String restrictionKey, UserHandle userHandle) {
490        return getUserRestrictions(userHandle).getBoolean(restrictionKey, false);
491    }
492
493    /**
494     * Return the serial number for a user.  This is a device-unique
495     * number assigned to that user; if the user is deleted and then a new
496     * user created, the new users will not be given the same serial number.
497     * @param user The user whose serial number is to be retrieved.
498     * @return The serial number of the given user; returns -1 if the
499     * given UserHandle does not exist.
500     * @see #getUserForSerialNumber(long)
501     */
502    public long getSerialNumberForUser(UserHandle user) {
503        return getUserSerialNumber(user.getIdentifier());
504    }
505
506    /**
507     * Return the user associated with a serial number previously
508     * returned by {@link #getSerialNumberForUser(UserHandle)}.
509     * @param serialNumber The serial number of the user that is being
510     * retrieved.
511     * @return Return the user associated with the serial number, or null
512     * if there is not one.
513     * @see #getSerialNumberForUser(UserHandle)
514     */
515    public UserHandle getUserForSerialNumber(long serialNumber) {
516        int ident = getUserHandle((int)serialNumber);
517        return ident >= 0 ? new UserHandle(ident) : null;
518    }
519
520    /**
521     * Creates a user with the specified name and options.
522     * Requires {@link android.Manifest.permission#MANAGE_USERS} permission.
523     *
524     * @param name the user's name
525     * @param flags flags that identify the type of user and other properties.
526     * @see UserInfo
527     *
528     * @return the UserInfo object for the created user, or null if the user could not be created.
529     * @hide
530     */
531    public UserInfo createUser(String name, int flags) {
532        try {
533            return mService.createUser(name, flags);
534        } catch (RemoteException re) {
535            Log.w(TAG, "Could not create a user", re);
536            return null;
537        }
538    }
539
540    /**
541     * Creates a user with the specified name and options as a profile of another user.
542     * Requires {@link android.Manifest.permission#MANAGE_USERS} permission.
543     *
544     * @param name the user's name
545     * @param flags flags that identify the type of user and other properties.
546     * @see UserInfo
547     * @param userHandle new user will be a profile of this use.
548     *
549     * @return the UserInfo object for the created user, or null if the user could not be created.
550     * @hide
551     */
552    public UserInfo createProfileForUser(String name, int flags, int userHandle) {
553        try {
554            return mService.createProfileForUser(name, flags, userHandle);
555        } catch (RemoteException re) {
556            Log.w(TAG, "Could not create a user", re);
557            return null;
558        }
559    }
560
561    /**
562     * Sets the user as enabled, if such an user exists.
563     * Requires {@link android.Manifest.permission#MANAGE_USERS} permission.
564     * Note that the default is true, it's only that managed profiles might not be enabled.
565     *
566     * @param userHandle the id of the profile to enable
567     * @hide
568     */
569    public void setUserEnabled(int userHandle) {
570        try {
571            mService.setUserEnabled(userHandle);
572        } catch (RemoteException e) {
573            Log.w(TAG, "Could not enable the profile", e);
574        }
575    }
576
577    /**
578     * Return the number of users currently created on the device.
579     */
580    public int getUserCount() {
581        List<UserInfo> users = getUsers();
582        return users != null ? users.size() : 1;
583    }
584
585    /**
586     * Returns information for all users on this device.
587     * Requires {@link android.Manifest.permission#MANAGE_USERS} permission.
588     * @return the list of users that were created.
589     * @hide
590     */
591    public List<UserInfo> getUsers() {
592        try {
593            return mService.getUsers(false);
594        } catch (RemoteException re) {
595            Log.w(TAG, "Could not get user list", re);
596            return null;
597        }
598    }
599
600    /**
601     * Returns list of the profiles of userHandle including
602     * userHandle itself.
603     * Note that it this returns both enabled and not enabled profiles. See
604     * {@link #getUserProfiles()} if you need only the enabled ones.
605     *
606     * Requires {@link android.Manifest.permission#MANAGE_USERS} permission.
607     * @param userHandle profiles of this user will be returned.
608     * @return the list of profiles.
609     * @hide
610     */
611    public List<UserInfo> getProfiles(int userHandle) {
612        try {
613            return mService.getProfiles(userHandle, false /* enabledOnly */);
614        } catch (RemoteException re) {
615            Log.w(TAG, "Could not get user list", re);
616            return null;
617        }
618    }
619
620    /**
621     * Returns a list of UserHandles for profiles associated with the user that the calling process
622     * is running on, including the user itself.
623     *
624     * @return A non-empty list of UserHandles associated with the calling user.
625     */
626    public List<UserHandle> getUserProfiles() {
627        ArrayList<UserHandle> profiles = new ArrayList<UserHandle>();
628        List<UserInfo> users = new ArrayList<UserInfo>();
629        try {
630            users = mService.getProfiles(UserHandle.myUserId(), true /* enabledOnly */);
631        } catch (RemoteException re) {
632            Log.w(TAG, "Could not get user list", re);
633            return null;
634        }
635        for (UserInfo info : users) {
636            UserHandle userHandle = new UserHandle(info.id);
637            profiles.add(userHandle);
638        }
639        return profiles;
640    }
641
642    /**
643     * Returns the parent of the profile which this method is called from
644     * or null if called from a user that is not a profile.
645     *
646     * @hide
647     */
648    public UserInfo getProfileParent(int userHandle) {
649        try {
650            return mService.getProfileParent(userHandle);
651        } catch (RemoteException re) {
652            Log.w(TAG, "Could not get profile parent", re);
653            return null;
654        }
655    }
656
657    /**
658     * If the target user is a managed profile of the calling user or the caller
659     * is itself a managed profile, then this returns a badged copy of the given
660     * icon to be able to distinguish it from the original icon.
661     * <P>
662     * If the original drawable is not a BitmapDrawable, then the original
663     * drawable is returned.
664     * </P>
665     *
666     * @param icon The icon to badge.
667     * @param user The target user.
668     * @return A drawable that combines the original icon and a badge as
669     *         determined by the system.
670     */
671    public Drawable getBadgedDrawableForUser(Drawable icon, UserHandle user) {
672        int badgeResId = getBadgeResIdForUser(user.getIdentifier());
673        if (badgeResId == 0) {
674            return icon;
675        } else {
676            Drawable badgeIcon = mContext.getPackageManager()
677                    .getDrawable("system", badgeResId, null);
678            return getMergedDrawable(icon, badgeIcon);
679        }
680    }
681
682    private int getBadgeResIdForUser(int userHandle) {
683        // Return the framework-provided badge.
684        List<UserInfo> userProfiles = getProfiles(getUserHandle());
685        for (UserInfo user : userProfiles) {
686            if (user.id == userHandle
687                    && user.isManagedProfile()) {
688                return com.android.internal.R.drawable.ic_corp_badge;
689            }
690        }
691        return 0;
692    }
693
694    private Drawable getMergedDrawable(Drawable icon, Drawable badge) {
695        final int width = icon.getIntrinsicWidth();
696        final int height = icon.getIntrinsicHeight();
697        Bitmap bitmap = Bitmap.createBitmap(width, height, Config.ARGB_8888);
698        Canvas canvas = new Canvas(bitmap);
699        icon.setBounds(0, 0, width, height);
700        icon.draw(canvas);
701        badge.setBounds(0, 0, width, height);
702        badge.draw(canvas);
703        BitmapDrawable merged = new BitmapDrawable(bitmap);
704        if (icon instanceof BitmapDrawable) {
705            merged.setTargetDensity(((BitmapDrawable) icon).getBitmap().getDensity());
706        }
707        return merged;
708    }
709
710    /**
711     * Returns information for all users on this device. Requires
712     * {@link android.Manifest.permission#MANAGE_USERS} permission.
713     *
714     * @param excludeDying specify if the list should exclude users being
715     *            removed.
716     * @return the list of users that were created.
717     * @hide
718     */
719    public List<UserInfo> getUsers(boolean excludeDying) {
720        try {
721            return mService.getUsers(excludeDying);
722        } catch (RemoteException re) {
723            Log.w(TAG, "Could not get user list", re);
724            return null;
725        }
726    }
727
728    /**
729     * Removes a user and all associated data.
730     * Requires {@link android.Manifest.permission#MANAGE_USERS} permission.
731     * @param userHandle the integer handle of the user, where 0 is the primary user.
732     * @hide
733     */
734    public boolean removeUser(int userHandle) {
735        try {
736            return mService.removeUser(userHandle);
737        } catch (RemoteException re) {
738            Log.w(TAG, "Could not remove user ", re);
739            return false;
740        }
741    }
742
743    /**
744     * Updates the user's name.
745     * Requires {@link android.Manifest.permission#MANAGE_USERS} permission.
746     *
747     * @param userHandle the user's integer handle
748     * @param name the new name for the user
749     * @hide
750     */
751    public void setUserName(int userHandle, String name) {
752        try {
753            mService.setUserName(userHandle, name);
754        } catch (RemoteException re) {
755            Log.w(TAG, "Could not set the user name ", re);
756        }
757    }
758
759    /**
760     * Sets the user's photo.
761     * @param userHandle the user for whom to change the photo.
762     * @param icon the bitmap to set as the photo.
763     * @hide
764     */
765    public void setUserIcon(int userHandle, Bitmap icon) {
766        try {
767            mService.setUserIcon(userHandle, icon);
768        } catch (RemoteException re) {
769            Log.w(TAG, "Could not set the user icon ", re);
770        }
771    }
772
773    /**
774     * Returns a file descriptor for the user's photo. PNG data can be read from this file.
775     * @param userHandle the user whose photo we want to read.
776     * @return a {@link Bitmap} of the user's photo, or null if there's no photo.
777     * @hide
778     */
779    public Bitmap getUserIcon(int userHandle) {
780        try {
781            return mService.getUserIcon(userHandle);
782        } catch (RemoteException re) {
783            Log.w(TAG, "Could not get the user icon ", re);
784            return null;
785        }
786    }
787
788    /**
789     * Enable or disable the use of a guest account. If disabled, the existing guest account
790     * will be wiped.
791     * Requires {@link android.Manifest.permission#MANAGE_USERS} permission.
792     * @param enable whether to enable a guest account.
793     * @hide
794     */
795    public void setGuestEnabled(boolean enable) {
796        try {
797            mService.setGuestEnabled(enable);
798        } catch (RemoteException re) {
799            Log.w(TAG, "Could not change guest account availability to " + enable);
800        }
801    }
802
803    /**
804     * Checks if a guest user is enabled for this device.
805     * Requires {@link android.Manifest.permission#MANAGE_USERS} permission.
806     * @return whether a guest user is enabled
807     * @hide
808     */
809    public boolean isGuestEnabled() {
810        try {
811            return mService.isGuestEnabled();
812        } catch (RemoteException re) {
813            Log.w(TAG, "Could not retrieve guest enabled state");
814            return false;
815        }
816    }
817
818    /**
819     * Wipes all the data for a user, but doesn't remove the user.
820     * Requires {@link android.Manifest.permission#MANAGE_USERS} permission.
821     * @param userHandle
822     * @hide
823     */
824    public void wipeUser(int userHandle) {
825        try {
826            mService.wipeUser(userHandle);
827        } catch (RemoteException re) {
828            Log.w(TAG, "Could not wipe user " + userHandle);
829        }
830    }
831
832    /**
833     * Returns the maximum number of users that can be created on this device. A return value
834     * of 1 means that it is a single user device.
835     * @hide
836     * @return a value greater than or equal to 1
837     */
838    public static int getMaxSupportedUsers() {
839        // Don't allow multiple users on certain builds
840        if (android.os.Build.ID.startsWith("JVP")) return 1;
841        return SystemProperties.getInt("fw.max_users",
842                Resources.getSystem().getInteger(R.integer.config_multiuserMaximumUsers));
843    }
844
845    /**
846     * Returns true if the user switcher should be shown, this will be if there
847     * are multiple users that aren't managed profiles.
848     * @hide
849     * @return true if user switcher should be shown.
850     */
851    public boolean isUserSwitcherEnabled() {
852        List<UserInfo> users = getUsers(true);
853        if (users == null) {
854           return false;
855        }
856        int switchableUserCount = 0;
857        for (UserInfo user : users) {
858            if (user.supportsSwitchTo()) {
859                ++switchableUserCount;
860            }
861        }
862        return switchableUserCount > 1;
863    }
864
865    /**
866     * Returns a serial number on this device for a given userHandle. User handles can be recycled
867     * when deleting and creating users, but serial numbers are not reused until the device is wiped.
868     * @param userHandle
869     * @return a serial number associated with that user, or -1 if the userHandle is not valid.
870     * @hide
871     */
872    public int getUserSerialNumber(int userHandle) {
873        try {
874            return mService.getUserSerialNumber(userHandle);
875        } catch (RemoteException re) {
876            Log.w(TAG, "Could not get serial number for user " + userHandle);
877        }
878        return -1;
879    }
880
881    /**
882     * Returns a userHandle on this device for a given user serial number. User handles can be
883     * recycled when deleting and creating users, but serial numbers are not reused until the device
884     * is wiped.
885     * @param userSerialNumber
886     * @return the userHandle associated with that user serial number, or -1 if the serial number
887     * is not valid.
888     * @hide
889     */
890    public int getUserHandle(int userSerialNumber) {
891        try {
892            return mService.getUserHandle(userSerialNumber);
893        } catch (RemoteException re) {
894            Log.w(TAG, "Could not get userHandle for user " + userSerialNumber);
895        }
896        return -1;
897    }
898
899    /**
900     * Returns a Bundle containing any saved application restrictions for this user, for the
901     * given package name. Only an application with this package name can call this method.
902     * @param packageName the package name of the calling application
903     * @return a Bundle with the restrictions as key/value pairs, or null if there are no
904     * saved restrictions. The values can be of type Boolean, String or String[], depending
905     * on the restriction type, as defined by the application.
906     */
907    public Bundle getApplicationRestrictions(String packageName) {
908        try {
909            return mService.getApplicationRestrictions(packageName);
910        } catch (RemoteException re) {
911            Log.w(TAG, "Could not get application restrictions for package " + packageName);
912        }
913        return null;
914    }
915
916    /**
917     * @hide
918     */
919    public Bundle getApplicationRestrictions(String packageName, UserHandle user) {
920        try {
921            return mService.getApplicationRestrictionsForUser(packageName, user.getIdentifier());
922        } catch (RemoteException re) {
923            Log.w(TAG, "Could not get application restrictions for user " + user.getIdentifier());
924        }
925        return null;
926    }
927
928    /**
929     * @hide
930     */
931    public void setApplicationRestrictions(String packageName, Bundle restrictions,
932            UserHandle user) {
933        try {
934            mService.setApplicationRestrictions(packageName, restrictions, user.getIdentifier());
935        } catch (RemoteException re) {
936            Log.w(TAG, "Could not set application restrictions for user " + user.getIdentifier());
937        }
938    }
939
940    /**
941     * Sets a new challenge PIN for restrictions. This is only for use by pre-installed
942     * apps and requires the MANAGE_USERS permission.
943     * @param newPin the PIN to use for challenge dialogs.
944     * @return Returns true if the challenge PIN was set successfully.
945     */
946    public boolean setRestrictionsChallenge(String newPin) {
947        try {
948            return mService.setRestrictionsChallenge(newPin);
949        } catch (RemoteException re) {
950            Log.w(TAG, "Could not change restrictions pin");
951        }
952        return false;
953    }
954
955    /**
956     * @hide
957     * @param pin The PIN to verify, or null to get the number of milliseconds to wait for before
958     * allowing the user to enter the PIN.
959     * @return Returns a positive number (including zero) for how many milliseconds before
960     * you can accept another PIN, when the input is null or the input doesn't match the saved PIN.
961     * Returns {@link #PIN_VERIFICATION_SUCCESS} if the input matches the saved PIN. Returns
962     * {@link #PIN_VERIFICATION_FAILED_NOT_SET} if there is no PIN set.
963     */
964    public int checkRestrictionsChallenge(String pin) {
965        try {
966            return mService.checkRestrictionsChallenge(pin);
967        } catch (RemoteException re) {
968            Log.w(TAG, "Could not check restrictions pin");
969        }
970        return PIN_VERIFICATION_FAILED_INCORRECT;
971    }
972
973    /**
974     * @hide
975     * Checks whether the user has restrictions that are PIN-protected. An application that
976     * participates in restrictions can check if the owner has requested a PIN challenge for
977     * any restricted operations. If there is a PIN in effect, the application should launch
978     * the PIN challenge activity {@link android.content.Intent#ACTION_RESTRICTIONS_CHALLENGE}.
979     * @see android.content.Intent#ACTION_RESTRICTIONS_CHALLENGE
980     * @return whether a restrictions PIN is in effect.
981     */
982    public boolean hasRestrictionsChallenge() {
983        try {
984            return mService.hasRestrictionsChallenge();
985        } catch (RemoteException re) {
986            Log.w(TAG, "Could not change restrictions pin");
987        }
988        return false;
989    }
990
991    /** @hide */
992    public void removeRestrictions() {
993        try {
994            mService.removeRestrictions();
995        } catch (RemoteException re) {
996            Log.w(TAG, "Could not change restrictions pin");
997        }
998    }
999}
1000