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