UserManager.java revision 2b2cf72f10f13d9314a1c53386188ed48e12d47c
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    /**
270     * Key for user restrictions. Specifies that the user is not allowed to send or receive
271     * phone calls or text messages. Emergency calls may still be permitted.
272     * The default value is <code>false</code>.
273     * <p/>
274     * Type: Boolean
275     * @see #setUserRestrictions(Bundle)
276     * @see #getUserRestrictions()
277     */
278    public static final String DISALLOW_TELEPHONY = "no_telephony";
279
280    /** @hide */
281    public static final int PIN_VERIFICATION_FAILED_INCORRECT = -3;
282    /** @hide */
283    public static final int PIN_VERIFICATION_FAILED_NOT_SET = -2;
284    /** @hide */
285    public static final int PIN_VERIFICATION_SUCCESS = -1;
286
287    private static UserManager sInstance = null;
288
289    /** @hide */
290    public synchronized static UserManager get(Context context) {
291        if (sInstance == null) {
292            sInstance = (UserManager) context.getSystemService(Context.USER_SERVICE);
293        }
294        return sInstance;
295    }
296
297    /** @hide */
298    public UserManager(Context context, IUserManager service) {
299        mService = service;
300        mContext = context;
301    }
302
303    /**
304     * Returns whether the system supports multiple users.
305     * @return true if multiple users can be created by user, false if it is a single user device.
306     * @hide
307     */
308    public static boolean supportsMultipleUsers() {
309        return getMaxSupportedUsers() > 1
310                && SystemProperties.getBoolean("fw.show_multiuserui",
311                Resources.getSystem().getBoolean(R.bool.config_enableMultiUserUI));
312    }
313
314    /**
315     * Returns the user handle for the user that the calling process is running on.
316     *
317     * @return the user handle of the user making this call.
318     * @hide
319     */
320    public int getUserHandle() {
321        return UserHandle.myUserId();
322    }
323
324    /**
325     * Returns the user name of the user making this call.  This call is only
326     * available to applications on the system image; it requires the
327     * MANAGE_USERS permission.
328     * @return the user name
329     */
330    public String getUserName() {
331        try {
332            return mService.getUserInfo(getUserHandle()).name;
333        } catch (RemoteException re) {
334            Log.w(TAG, "Could not get user name", re);
335            return "";
336        }
337    }
338
339   /**
340     * Used to determine whether the user making this call is subject to
341     * teleportations.
342     * @return whether the user making this call is a goat
343     */
344    public boolean isUserAGoat() {
345        return false;
346    }
347
348    /**
349     * Used to check if the user making this call is linked to another user. Linked users may have
350     * a reduced number of available apps, app restrictions and account restrictions.
351     * @return whether the user making this call is a linked user
352     * @hide
353     */
354    public boolean isLinkedUser() {
355        try {
356            return mService.isRestricted();
357        } catch (RemoteException re) {
358            Log.w(TAG, "Could not check if user is limited ", re);
359            return false;
360        }
361    }
362
363    /**
364     * Return whether the given user is actively running.  This means that
365     * the user is in the "started" state, not "stopped" -- it is currently
366     * allowed to run code through scheduled alarms, receiving broadcasts,
367     * etc.  A started user may be either the current foreground user or a
368     * background user; the result here does not distinguish between the two.
369     * @param user The user to retrieve the running state for.
370     */
371    public boolean isUserRunning(UserHandle user) {
372        try {
373            return ActivityManagerNative.getDefault().isUserRunning(
374                    user.getIdentifier(), false);
375        } catch (RemoteException e) {
376            return false;
377        }
378    }
379
380    /**
381     * Return whether the given user is actively running <em>or</em> stopping.
382     * This is like {@link #isUserRunning(UserHandle)}, but will also return
383     * true if the user had been running but is in the process of being stopped
384     * (but is not yet fully stopped, and still running some code).
385     * @param user The user to retrieve the running state for.
386     */
387    public boolean isUserRunningOrStopping(UserHandle user) {
388        try {
389            return ActivityManagerNative.getDefault().isUserRunning(
390                    user.getIdentifier(), true);
391        } catch (RemoteException e) {
392            return false;
393        }
394    }
395
396    /**
397     * Returns the UserInfo object describing a specific user.
398     * Requires {@link android.Manifest.permission#MANAGE_USERS} permission.
399     * @param userHandle the user handle of the user whose information is being requested.
400     * @return the UserInfo object for a specific user.
401     * @hide
402     */
403    public UserInfo getUserInfo(int userHandle) {
404        try {
405            return mService.getUserInfo(userHandle);
406        } catch (RemoteException re) {
407            Log.w(TAG, "Could not get user info", re);
408            return null;
409        }
410    }
411
412    /**
413     * Returns the user-wide restrictions imposed on this user.
414     * @return a Bundle containing all the restrictions.
415     */
416    public Bundle getUserRestrictions() {
417        return getUserRestrictions(Process.myUserHandle());
418    }
419
420    /**
421     * Returns the user-wide restrictions imposed on the user specified by <code>userHandle</code>.
422     * @param userHandle the UserHandle of the user for whom to retrieve the restrictions.
423     * @return a Bundle containing all the restrictions.
424     */
425    public Bundle getUserRestrictions(UserHandle userHandle) {
426        try {
427            return mService.getUserRestrictions(userHandle.getIdentifier());
428        } catch (RemoteException re) {
429            Log.w(TAG, "Could not get user restrictions", re);
430            return Bundle.EMPTY;
431        }
432    }
433
434    /**
435     * Sets all the user-wide restrictions for this user.
436     * Requires the MANAGE_USERS permission.
437     * @param restrictions the Bundle containing all the restrictions.
438     */
439    public void setUserRestrictions(Bundle restrictions) {
440        setUserRestrictions(restrictions, Process.myUserHandle());
441    }
442
443    /**
444     * Sets all the user-wide restrictions for the specified user.
445     * Requires the MANAGE_USERS permission.
446     * @param restrictions the Bundle containing all the restrictions.
447     * @param userHandle the UserHandle of the user for whom to set the restrictions.
448     */
449    public void setUserRestrictions(Bundle restrictions, UserHandle userHandle) {
450        try {
451            mService.setUserRestrictions(restrictions, userHandle.getIdentifier());
452        } catch (RemoteException re) {
453            Log.w(TAG, "Could not set user restrictions", re);
454        }
455    }
456
457    /**
458     * Sets the value of a specific restriction.
459     * Requires the MANAGE_USERS permission.
460     * @param key the key of the restriction
461     * @param value the value for the restriction
462     */
463    public void setUserRestriction(String key, boolean value) {
464        Bundle bundle = getUserRestrictions();
465        bundle.putBoolean(key, value);
466        setUserRestrictions(bundle);
467    }
468
469    /**
470     * @hide
471     * Sets the value of a specific restriction on a specific user.
472     * Requires the MANAGE_USERS permission.
473     * @param key the key of the restriction
474     * @param value the value for the restriction
475     * @param userHandle the user whose restriction is to be changed.
476     */
477    public void setUserRestriction(String key, boolean value, UserHandle userHandle) {
478        Bundle bundle = getUserRestrictions(userHandle);
479        bundle.putBoolean(key, value);
480        setUserRestrictions(bundle, userHandle);
481    }
482
483    /**
484     * Returns whether the current user has been disallowed from performing certain actions
485     * or setting certain settings.
486     *
487     * @param restrictionKey The string key representing the restriction.
488     * @return {@code true} if the current user has the given restriction, {@code false} otherwise.
489     */
490    public boolean hasUserRestriction(String restrictionKey) {
491        return hasUserRestriction(restrictionKey, Process.myUserHandle());
492    }
493
494    /**
495     * @hide
496     * Returns whether the given user has been disallowed from performing certain actions
497     * or setting certain settings.
498     * @param restrictionKey the string key representing the restriction
499     * @param userHandle the UserHandle of the user for whom to retrieve the restrictions.
500     */
501    public boolean hasUserRestriction(String restrictionKey, UserHandle userHandle) {
502        return getUserRestrictions(userHandle).getBoolean(restrictionKey, false);
503    }
504
505    /**
506     * Return the serial number for a user.  This is a device-unique
507     * number assigned to that user; if the user is deleted and then a new
508     * user created, the new users will not be given the same serial number.
509     * @param user The user whose serial number is to be retrieved.
510     * @return The serial number of the given user; returns -1 if the
511     * given UserHandle does not exist.
512     * @see #getUserForSerialNumber(long)
513     */
514    public long getSerialNumberForUser(UserHandle user) {
515        return getUserSerialNumber(user.getIdentifier());
516    }
517
518    /**
519     * Return the user associated with a serial number previously
520     * returned by {@link #getSerialNumberForUser(UserHandle)}.
521     * @param serialNumber The serial number of the user that is being
522     * retrieved.
523     * @return Return the user associated with the serial number, or null
524     * if there is not one.
525     * @see #getSerialNumberForUser(UserHandle)
526     */
527    public UserHandle getUserForSerialNumber(long serialNumber) {
528        int ident = getUserHandle((int)serialNumber);
529        return ident >= 0 ? new UserHandle(ident) : null;
530    }
531
532    /**
533     * Creates a user with the specified name and options.
534     * Requires {@link android.Manifest.permission#MANAGE_USERS} permission.
535     *
536     * @param name the user's name
537     * @param flags flags that identify the type of user and other properties.
538     * @see UserInfo
539     *
540     * @return the UserInfo object for the created user, or null if the user could not be created.
541     * @hide
542     */
543    public UserInfo createUser(String name, int flags) {
544        try {
545            return mService.createUser(name, flags);
546        } catch (RemoteException re) {
547            Log.w(TAG, "Could not create a user", re);
548            return null;
549        }
550    }
551
552    /**
553     * Creates a user with the specified name and options as a profile of another user.
554     * Requires {@link android.Manifest.permission#MANAGE_USERS} permission.
555     *
556     * @param name the user's name
557     * @param flags flags that identify the type of user and other properties.
558     * @see UserInfo
559     * @param userHandle new user will be a profile of this use.
560     *
561     * @return the UserInfo object for the created user, or null if the user could not be created.
562     * @hide
563     */
564    public UserInfo createProfileForUser(String name, int flags, int userHandle) {
565        try {
566            return mService.createProfileForUser(name, flags, userHandle);
567        } catch (RemoteException re) {
568            Log.w(TAG, "Could not create a user", re);
569            return null;
570        }
571    }
572
573    /**
574     * Sets the user as enabled, if such an user exists.
575     * Requires {@link android.Manifest.permission#MANAGE_USERS} permission.
576     * Note that the default is true, it's only that managed profiles might not be enabled.
577     *
578     * @param userHandle the id of the profile to enable
579     * @hide
580     */
581    public void setUserEnabled(int userHandle) {
582        try {
583            mService.setUserEnabled(userHandle);
584        } catch (RemoteException e) {
585            Log.w(TAG, "Could not enable the profile", e);
586        }
587    }
588
589    /**
590     * Return the number of users currently created on the device.
591     */
592    public int getUserCount() {
593        List<UserInfo> users = getUsers();
594        return users != null ? users.size() : 1;
595    }
596
597    /**
598     * Returns information for all users on this device.
599     * Requires {@link android.Manifest.permission#MANAGE_USERS} permission.
600     * @return the list of users that were created.
601     * @hide
602     */
603    public List<UserInfo> getUsers() {
604        try {
605            return mService.getUsers(false);
606        } catch (RemoteException re) {
607            Log.w(TAG, "Could not get user list", re);
608            return null;
609        }
610    }
611
612    /**
613     * Returns list of the profiles of userHandle including
614     * userHandle itself.
615     * Note that it this returns both enabled and not enabled profiles. See
616     * {@link #getUserProfiles()} if you need only the enabled ones.
617     *
618     * Requires {@link android.Manifest.permission#MANAGE_USERS} permission.
619     * @param userHandle profiles of this user will be returned.
620     * @return the list of profiles.
621     * @hide
622     */
623    public List<UserInfo> getProfiles(int userHandle) {
624        try {
625            return mService.getProfiles(userHandle, false /* enabledOnly */);
626        } catch (RemoteException re) {
627            Log.w(TAG, "Could not get user list", re);
628            return null;
629        }
630    }
631
632    /**
633     * Returns a list of UserHandles for profiles associated with the user that the calling process
634     * is running on, including the user itself.
635     *
636     * @return A non-empty list of UserHandles associated with the calling user.
637     */
638    public List<UserHandle> getUserProfiles() {
639        ArrayList<UserHandle> profiles = new ArrayList<UserHandle>();
640        List<UserInfo> users = new ArrayList<UserInfo>();
641        try {
642            users = mService.getProfiles(UserHandle.myUserId(), true /* enabledOnly */);
643        } catch (RemoteException re) {
644            Log.w(TAG, "Could not get user list", re);
645            return null;
646        }
647        for (UserInfo info : users) {
648            UserHandle userHandle = new UserHandle(info.id);
649            profiles.add(userHandle);
650        }
651        return profiles;
652    }
653
654    /**
655     * Returns the parent of the profile which this method is called from
656     * or null if called from a user that is not a profile.
657     *
658     * @hide
659     */
660    public UserInfo getProfileParent(int userHandle) {
661        try {
662            return mService.getProfileParent(userHandle);
663        } catch (RemoteException re) {
664            Log.w(TAG, "Could not get profile parent", re);
665            return null;
666        }
667    }
668
669    /**
670     * If the target user is a managed profile of the calling user or the caller
671     * is itself a managed profile, then this returns a badged copy of the given
672     * icon to be able to distinguish it from the original icon.
673     * <P>
674     * If the original drawable is not a BitmapDrawable, then the original
675     * drawable is returned.
676     * </P>
677     *
678     * @param icon The icon to badge.
679     * @param user The target user.
680     * @return A drawable that combines the original icon and a badge as
681     *         determined by the system.
682     */
683    public Drawable getBadgedDrawableForUser(Drawable icon, UserHandle user) {
684        int badgeResId = getBadgeResIdForUser(user.getIdentifier());
685        if (badgeResId == 0) {
686            return icon;
687        } else {
688            Drawable badgeIcon = mContext.getPackageManager()
689                    .getDrawable("system", badgeResId, null);
690            return getMergedDrawable(icon, badgeIcon);
691        }
692    }
693
694    private int getBadgeResIdForUser(int userHandle) {
695        // Return the framework-provided badge.
696        List<UserInfo> userProfiles = getProfiles(getUserHandle());
697        for (UserInfo user : userProfiles) {
698            if (user.id == userHandle
699                    && user.isManagedProfile()) {
700                return com.android.internal.R.drawable.ic_corp_badge;
701            }
702        }
703        return 0;
704    }
705
706    private Drawable getMergedDrawable(Drawable icon, Drawable badge) {
707        final int width = icon.getIntrinsicWidth();
708        final int height = icon.getIntrinsicHeight();
709        Bitmap bitmap = Bitmap.createBitmap(width, height, Config.ARGB_8888);
710        Canvas canvas = new Canvas(bitmap);
711        icon.setBounds(0, 0, width, height);
712        icon.draw(canvas);
713        badge.setBounds(0, 0, width, height);
714        badge.draw(canvas);
715        BitmapDrawable merged = new BitmapDrawable(bitmap);
716        if (icon instanceof BitmapDrawable) {
717            merged.setTargetDensity(((BitmapDrawable) icon).getBitmap().getDensity());
718        }
719        return merged;
720    }
721
722    /**
723     * Returns information for all users on this device. Requires
724     * {@link android.Manifest.permission#MANAGE_USERS} permission.
725     *
726     * @param excludeDying specify if the list should exclude users being
727     *            removed.
728     * @return the list of users that were created.
729     * @hide
730     */
731    public List<UserInfo> getUsers(boolean excludeDying) {
732        try {
733            return mService.getUsers(excludeDying);
734        } catch (RemoteException re) {
735            Log.w(TAG, "Could not get user list", re);
736            return null;
737        }
738    }
739
740    /**
741     * Removes a user and all associated data.
742     * Requires {@link android.Manifest.permission#MANAGE_USERS} permission.
743     * @param userHandle the integer handle of the user, where 0 is the primary user.
744     * @hide
745     */
746    public boolean removeUser(int userHandle) {
747        try {
748            return mService.removeUser(userHandle);
749        } catch (RemoteException re) {
750            Log.w(TAG, "Could not remove user ", re);
751            return false;
752        }
753    }
754
755    /**
756     * Updates the user's name.
757     * Requires {@link android.Manifest.permission#MANAGE_USERS} permission.
758     *
759     * @param userHandle the user's integer handle
760     * @param name the new name for the user
761     * @hide
762     */
763    public void setUserName(int userHandle, String name) {
764        try {
765            mService.setUserName(userHandle, name);
766        } catch (RemoteException re) {
767            Log.w(TAG, "Could not set the user name ", re);
768        }
769    }
770
771    /**
772     * Sets the user's photo.
773     * @param userHandle the user for whom to change the photo.
774     * @param icon the bitmap to set as the photo.
775     * @hide
776     */
777    public void setUserIcon(int userHandle, Bitmap icon) {
778        try {
779            mService.setUserIcon(userHandle, icon);
780        } catch (RemoteException re) {
781            Log.w(TAG, "Could not set the user icon ", re);
782        }
783    }
784
785    /**
786     * Returns a file descriptor for the user's photo. PNG data can be read from this file.
787     * @param userHandle the user whose photo we want to read.
788     * @return a {@link Bitmap} of the user's photo, or null if there's no photo.
789     * @hide
790     */
791    public Bitmap getUserIcon(int userHandle) {
792        try {
793            return mService.getUserIcon(userHandle);
794        } catch (RemoteException re) {
795            Log.w(TAG, "Could not get the user icon ", re);
796            return null;
797        }
798    }
799
800    /**
801     * Enable or disable the use of a guest account. If disabled, the existing guest account
802     * will be wiped.
803     * Requires {@link android.Manifest.permission#MANAGE_USERS} permission.
804     * @param enable whether to enable a guest account.
805     * @hide
806     */
807    public void setGuestEnabled(boolean enable) {
808        try {
809            mService.setGuestEnabled(enable);
810        } catch (RemoteException re) {
811            Log.w(TAG, "Could not change guest account availability to " + enable);
812        }
813    }
814
815    /**
816     * Checks if a guest user is enabled for this device.
817     * Requires {@link android.Manifest.permission#MANAGE_USERS} permission.
818     * @return whether a guest user is enabled
819     * @hide
820     */
821    public boolean isGuestEnabled() {
822        try {
823            return mService.isGuestEnabled();
824        } catch (RemoteException re) {
825            Log.w(TAG, "Could not retrieve guest enabled state");
826            return false;
827        }
828    }
829
830    /**
831     * Wipes all the data for a user, but doesn't remove the user.
832     * Requires {@link android.Manifest.permission#MANAGE_USERS} permission.
833     * @param userHandle
834     * @hide
835     */
836    public void wipeUser(int userHandle) {
837        try {
838            mService.wipeUser(userHandle);
839        } catch (RemoteException re) {
840            Log.w(TAG, "Could not wipe user " + userHandle);
841        }
842    }
843
844    /**
845     * Returns the maximum number of users that can be created on this device. A return value
846     * of 1 means that it is a single user device.
847     * @hide
848     * @return a value greater than or equal to 1
849     */
850    public static int getMaxSupportedUsers() {
851        // Don't allow multiple users on certain builds
852        if (android.os.Build.ID.startsWith("JVP")) return 1;
853        return SystemProperties.getInt("fw.max_users",
854                Resources.getSystem().getInteger(R.integer.config_multiuserMaximumUsers));
855    }
856
857    /**
858     * Returns true if the user switcher should be shown, this will be if there
859     * are multiple users that aren't managed profiles.
860     * @hide
861     * @return true if user switcher should be shown.
862     */
863    public boolean isUserSwitcherEnabled() {
864        List<UserInfo> users = getUsers(true);
865        if (users == null) {
866           return false;
867        }
868        int switchableUserCount = 0;
869        for (UserInfo user : users) {
870            if (user.supportsSwitchTo()) {
871                ++switchableUserCount;
872            }
873        }
874        return switchableUserCount > 1;
875    }
876
877    /**
878     * Returns a serial number on this device for a given userHandle. User handles can be recycled
879     * when deleting and creating users, but serial numbers are not reused until the device is wiped.
880     * @param userHandle
881     * @return a serial number associated with that user, or -1 if the userHandle is not valid.
882     * @hide
883     */
884    public int getUserSerialNumber(int userHandle) {
885        try {
886            return mService.getUserSerialNumber(userHandle);
887        } catch (RemoteException re) {
888            Log.w(TAG, "Could not get serial number for user " + userHandle);
889        }
890        return -1;
891    }
892
893    /**
894     * Returns a userHandle on this device for a given user serial number. User handles can be
895     * recycled when deleting and creating users, but serial numbers are not reused until the device
896     * is wiped.
897     * @param userSerialNumber
898     * @return the userHandle associated with that user serial number, or -1 if the serial number
899     * is not valid.
900     * @hide
901     */
902    public int getUserHandle(int userSerialNumber) {
903        try {
904            return mService.getUserHandle(userSerialNumber);
905        } catch (RemoteException re) {
906            Log.w(TAG, "Could not get userHandle for user " + userSerialNumber);
907        }
908        return -1;
909    }
910
911    /**
912     * Returns a Bundle containing any saved application restrictions for this user, for the
913     * given package name. Only an application with this package name can call this method.
914     * @param packageName the package name of the calling application
915     * @return a Bundle with the restrictions as key/value pairs, or null if there are no
916     * saved restrictions. The values can be of type Boolean, String or String[], depending
917     * on the restriction type, as defined by the application.
918     */
919    public Bundle getApplicationRestrictions(String packageName) {
920        try {
921            return mService.getApplicationRestrictions(packageName);
922        } catch (RemoteException re) {
923            Log.w(TAG, "Could not get application restrictions for package " + packageName);
924        }
925        return null;
926    }
927
928    /**
929     * @hide
930     */
931    public Bundle getApplicationRestrictions(String packageName, UserHandle user) {
932        try {
933            return mService.getApplicationRestrictionsForUser(packageName, user.getIdentifier());
934        } catch (RemoteException re) {
935            Log.w(TAG, "Could not get application restrictions for user " + user.getIdentifier());
936        }
937        return null;
938    }
939
940    /**
941     * @hide
942     */
943    public void setApplicationRestrictions(String packageName, Bundle restrictions,
944            UserHandle user) {
945        try {
946            mService.setApplicationRestrictions(packageName, restrictions, user.getIdentifier());
947        } catch (RemoteException re) {
948            Log.w(TAG, "Could not set application restrictions for user " + user.getIdentifier());
949        }
950    }
951
952    /**
953     * Sets a new challenge PIN for restrictions. This is only for use by pre-installed
954     * apps and requires the MANAGE_USERS permission.
955     * @param newPin the PIN to use for challenge dialogs.
956     * @return Returns true if the challenge PIN was set successfully.
957     */
958    public boolean setRestrictionsChallenge(String newPin) {
959        try {
960            return mService.setRestrictionsChallenge(newPin);
961        } catch (RemoteException re) {
962            Log.w(TAG, "Could not change restrictions pin");
963        }
964        return false;
965    }
966
967    /**
968     * @hide
969     * @param pin The PIN to verify, or null to get the number of milliseconds to wait for before
970     * allowing the user to enter the PIN.
971     * @return Returns a positive number (including zero) for how many milliseconds before
972     * you can accept another PIN, when the input is null or the input doesn't match the saved PIN.
973     * Returns {@link #PIN_VERIFICATION_SUCCESS} if the input matches the saved PIN. Returns
974     * {@link #PIN_VERIFICATION_FAILED_NOT_SET} if there is no PIN set.
975     */
976    public int checkRestrictionsChallenge(String pin) {
977        try {
978            return mService.checkRestrictionsChallenge(pin);
979        } catch (RemoteException re) {
980            Log.w(TAG, "Could not check restrictions pin");
981        }
982        return PIN_VERIFICATION_FAILED_INCORRECT;
983    }
984
985    /**
986     * @hide
987     * Checks whether the user has restrictions that are PIN-protected. An application that
988     * participates in restrictions can check if the owner has requested a PIN challenge for
989     * any restricted operations. If there is a PIN in effect, the application should launch
990     * the PIN challenge activity {@link android.content.Intent#ACTION_RESTRICTIONS_CHALLENGE}.
991     * @see android.content.Intent#ACTION_RESTRICTIONS_CHALLENGE
992     * @return whether a restrictions PIN is in effect.
993     */
994    public boolean hasRestrictionsChallenge() {
995        try {
996            return mService.hasRestrictionsChallenge();
997        } catch (RemoteException re) {
998            Log.w(TAG, "Could not change restrictions pin");
999        }
1000        return false;
1001    }
1002
1003    /** @hide */
1004    public void removeRestrictions() {
1005        try {
1006            mService.removeRestrictions();
1007        } catch (RemoteException re) {
1008            Log.w(TAG, "Could not change restrictions pin");
1009        }
1010    }
1011}
1012