UserManager.java revision 7dda2657be1fcc808566dab3482df9d643ceb0f5
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.
304     * @param restrictions the Bundle containing all the restrictions.
305     */
306    public void setUserRestrictions(Bundle restrictions) {
307        setUserRestrictions(restrictions, Process.myUserHandle());
308    }
309
310    /**
311     * Sets all the user-wide restrictions for the specified user.
312     * Requires the MANAGE_USERS permission.
313     * @param restrictions the Bundle containing all the restrictions.
314     * @param userHandle the UserHandle of the user for whom to set the restrictions.
315     */
316    public void setUserRestrictions(Bundle restrictions, UserHandle userHandle) {
317        try {
318            mService.setUserRestrictions(restrictions, userHandle.getIdentifier());
319        } catch (RemoteException re) {
320            Log.w(TAG, "Could not set user restrictions", re);
321        }
322    }
323
324    /**
325     * Sets the value of a specific restriction.
326     * Requires the MANAGE_USERS permission.
327     * @param key the key of the restriction
328     * @param value the value for the restriction
329     */
330    public void setUserRestriction(String key, boolean value) {
331        Bundle bundle = getUserRestrictions();
332        bundle.putBoolean(key, value);
333        setUserRestrictions(bundle);
334    }
335
336    /**
337     * @hide
338     * Sets the value of a specific restriction on a specific user.
339     * Requires the {@link android.Manifest.permission#MANAGE_USERS} permission.
340     * @param key the key of the restriction
341     * @param value the value for the restriction
342     * @param userHandle the user whose restriction is to be changed.
343     */
344    public void setUserRestriction(String key, boolean value, UserHandle userHandle) {
345        Bundle bundle = getUserRestrictions(userHandle);
346        bundle.putBoolean(key, value);
347        setUserRestrictions(bundle, userHandle);
348    }
349
350    /**
351     * @hide
352     * Returns whether the current user has been disallowed from performing certain actions
353     * or setting certain settings.
354     * @param restrictionKey the string key representing the restriction
355     */
356    public boolean hasUserRestriction(String restrictionKey) {
357        return hasUserRestriction(restrictionKey, Process.myUserHandle());
358    }
359
360    /**
361     * @hide
362     * Returns whether the given user has been disallowed from performing certain actions
363     * or setting certain settings.
364     * @param restrictionKey the string key representing the restriction
365     * @param userHandle the UserHandle of the user for whom to retrieve the restrictions.
366     */
367    public boolean hasUserRestriction(String restrictionKey, UserHandle userHandle) {
368        return getUserRestrictions(userHandle).getBoolean(restrictionKey, false);
369    }
370
371    /**
372     * Return the serial number for a user.  This is a device-unique
373     * number assigned to that user; if the user is deleted and then a new
374     * user created, the new users will not be given the same serial number.
375     * @param user The user whose serial number is to be retrieved.
376     * @return The serial number of the given user; returns -1 if the
377     * given UserHandle does not exist.
378     * @see #getUserForSerialNumber(long)
379     */
380    public long getSerialNumberForUser(UserHandle user) {
381        return getUserSerialNumber(user.getIdentifier());
382    }
383
384    /**
385     * Return the user associated with a serial number previously
386     * returned by {@link #getSerialNumberForUser(UserHandle)}.
387     * @param serialNumber The serial number of the user that is being
388     * retrieved.
389     * @return Return the user associated with the serial number, or null
390     * if there is not one.
391     * @see #getSerialNumberForUser(UserHandle)
392     */
393    public UserHandle getUserForSerialNumber(long serialNumber) {
394        int ident = getUserHandle((int)serialNumber);
395        return ident >= 0 ? new UserHandle(ident) : null;
396    }
397
398    /**
399     * Creates a user with the specified name and options.
400     * Requires {@link android.Manifest.permission#MANAGE_USERS} permission.
401     *
402     * @param name the user's name
403     * @param flags flags that identify the type of user and other properties.
404     * @see UserInfo
405     *
406     * @return the UserInfo object for the created user, or null if the user could not be created.
407     * @hide
408     */
409    public UserInfo createUser(String name, int flags) {
410        try {
411            return mService.createUser(name, flags);
412        } catch (RemoteException re) {
413            Log.w(TAG, "Could not create a user", re);
414            return null;
415        }
416    }
417
418    /**
419     * Creates a user with the specified name and options as a profile of another user.
420     * Requires {@link android.Manifest.permission#MANAGE_USERS} permission.
421     *
422     * @param name the user's name
423     * @param flags flags that identify the type of user and other properties.
424     * @see UserInfo
425     * @param userHandle new user will be a profile of this use.
426     *
427     * @return the UserInfo object for the created user, or null if the user could not be created.
428     * @hide
429     */
430    public UserInfo createProfileForUser(String name, int flags, int userHandle) {
431        try {
432            return mService.createProfileForUser(name, flags, userHandle);
433        } catch (RemoteException re) {
434            Log.w(TAG, "Could not create a user", re);
435            return null;
436        }
437    }
438
439    /**
440     * Return the number of users currently created on the device.
441     */
442    public int getUserCount() {
443        List<UserInfo> users = getUsers();
444        return users != null ? users.size() : 1;
445    }
446
447    /**
448     * Returns information for all users on this device.
449     * Requires {@link android.Manifest.permission#MANAGE_USERS} permission.
450     * @return the list of users that were created.
451     * @hide
452     */
453    public List<UserInfo> getUsers() {
454        try {
455            return mService.getUsers(false);
456        } catch (RemoteException re) {
457            Log.w(TAG, "Could not get user list", re);
458            return null;
459        }
460    }
461
462    /**
463     * Returns list of the profiles of userHandle including
464     * userHandle itself.
465     *
466     * Requires {@link android.Manifest.permission#MANAGE_USERS} permission.
467     * @param userHandle profiles of this user will be returned.
468     * @return the list of profiles.
469     * @hide
470     */
471    public List<UserInfo> getProfiles(int userHandle) {
472        try {
473            return mService.getProfiles(userHandle);
474        } catch (RemoteException re) {
475            Log.w(TAG, "Could not get user list", re);
476            return null;
477        }
478    }
479
480    /**
481     * Returns a list of UserHandles for profiles associated with this user, including this user.
482     *
483     * @return A non-empty list of UserHandles associated with the calling user.
484     */
485    public List<UserHandle> getUserProfiles() {
486        ArrayList<UserHandle> profiles = new ArrayList<UserHandle>();
487        List<UserInfo> users = getProfiles(UserHandle.myUserId());
488        for (UserInfo info : users) {
489            UserHandle userHandle = new UserHandle(info.id);
490            profiles.add(userHandle);
491        }
492        return profiles;
493    }
494
495    /**
496     * If the target user is a managed profile of the calling user or the caller
497     * is itself a managed profile, then this returns a badged copy of the given
498     * icon to be able to distinguish it from the original icon.
499     * <P>
500     * If the original drawable is not a BitmapDrawable, then the original
501     * drawable is returned.
502     * </P>
503     *
504     * @param icon The icon to badge.
505     * @param user The target user.
506     * @return A drawable that combines the original icon and a badge as
507     *         determined by the system.
508     */
509    public Drawable getBadgedDrawableForUser(Drawable icon, UserHandle user) {
510        int badgeResId = getBadgeResIdForUser(user.getIdentifier());
511        if (badgeResId == 0) {
512            return icon;
513        } else {
514            Drawable badgeIcon = mContext.getPackageManager()
515                    .getDrawable("system", badgeResId, null);
516            return getMergedDrawable(icon, badgeIcon);
517        }
518    }
519
520    private int getBadgeResIdForUser(int userHandle) {
521        // Return the framework-provided badge.
522        List<UserInfo> userProfiles = getProfiles(UserHandle.myUserId());
523        for (UserInfo user : userProfiles) {
524            if (user.id == userHandle
525                    && user.isManagedProfile()) {
526                return com.android.internal.R.drawable.ic_corp_badge;
527            }
528        }
529        return 0;
530    }
531
532    private Drawable getMergedDrawable(Drawable icon, Drawable badge) {
533        final int width = icon.getIntrinsicWidth();
534        final int height = icon.getIntrinsicHeight();
535        Bitmap bitmap = Bitmap.createBitmap(width, height, Config.ARGB_8888);
536        Canvas canvas = new Canvas(bitmap);
537        icon.setBounds(0, 0, width, height);
538        icon.draw(canvas);
539        badge.setBounds(0, 0, width, height);
540        badge.draw(canvas);
541        BitmapDrawable merged = new BitmapDrawable(bitmap);
542        if (icon instanceof BitmapDrawable) {
543            merged.setTargetDensity(((BitmapDrawable) icon).getBitmap().getDensity());
544        }
545        return merged;
546    }
547
548    /**
549     * Returns information for all users on this device. Requires
550     * {@link android.Manifest.permission#MANAGE_USERS} permission.
551     *
552     * @param excludeDying specify if the list should exclude users being
553     *            removed.
554     * @return the list of users that were created.
555     * @hide
556     */
557    public List<UserInfo> getUsers(boolean excludeDying) {
558        try {
559            return mService.getUsers(excludeDying);
560        } catch (RemoteException re) {
561            Log.w(TAG, "Could not get user list", re);
562            return null;
563        }
564    }
565
566    /**
567     * Removes a user and all associated data.
568     * Requires {@link android.Manifest.permission#MANAGE_USERS} permission.
569     * @param userHandle the integer handle of the user, where 0 is the primary user.
570     * @hide
571     */
572    public boolean removeUser(int userHandle) {
573        try {
574            return mService.removeUser(userHandle);
575        } catch (RemoteException re) {
576            Log.w(TAG, "Could not remove user ", re);
577            return false;
578        }
579    }
580
581    /**
582     * Updates the user's name.
583     * Requires {@link android.Manifest.permission#MANAGE_USERS} permission.
584     *
585     * @param userHandle the user's integer handle
586     * @param name the new name for the user
587     * @hide
588     */
589    public void setUserName(int userHandle, String name) {
590        try {
591            mService.setUserName(userHandle, name);
592        } catch (RemoteException re) {
593            Log.w(TAG, "Could not set the user name ", re);
594        }
595    }
596
597    /**
598     * Sets the user's photo.
599     * @param userHandle the user for whom to change the photo.
600     * @param icon the bitmap to set as the photo.
601     * @hide
602     */
603    public void setUserIcon(int userHandle, Bitmap icon) {
604        try {
605            mService.setUserIcon(userHandle, icon);
606        } catch (RemoteException re) {
607            Log.w(TAG, "Could not set the user icon ", re);
608        }
609    }
610
611    /**
612     * Returns a file descriptor for the user's photo. PNG data can be read from this file.
613     * @param userHandle the user whose photo we want to read.
614     * @return a {@link Bitmap} of the user's photo, or null if there's no photo.
615     * @hide
616     */
617    public Bitmap getUserIcon(int userHandle) {
618        try {
619            return mService.getUserIcon(userHandle);
620        } catch (RemoteException re) {
621            Log.w(TAG, "Could not get the user icon ", re);
622            return null;
623        }
624    }
625
626    /**
627     * Enable or disable the use of a guest account. If disabled, the existing guest account
628     * will be wiped.
629     * Requires {@link android.Manifest.permission#MANAGE_USERS} permission.
630     * @param enable whether to enable a guest account.
631     * @hide
632     */
633    public void setGuestEnabled(boolean enable) {
634        try {
635            mService.setGuestEnabled(enable);
636        } catch (RemoteException re) {
637            Log.w(TAG, "Could not change guest account availability to " + enable);
638        }
639    }
640
641    /**
642     * Checks if a guest user is enabled for this device.
643     * Requires {@link android.Manifest.permission#MANAGE_USERS} permission.
644     * @return whether a guest user is enabled
645     * @hide
646     */
647    public boolean isGuestEnabled() {
648        try {
649            return mService.isGuestEnabled();
650        } catch (RemoteException re) {
651            Log.w(TAG, "Could not retrieve guest enabled state");
652            return false;
653        }
654    }
655
656    /**
657     * Wipes all the data for a user, but doesn't remove the user.
658     * Requires {@link android.Manifest.permission#MANAGE_USERS} permission.
659     * @param userHandle
660     * @hide
661     */
662    public void wipeUser(int userHandle) {
663        try {
664            mService.wipeUser(userHandle);
665        } catch (RemoteException re) {
666            Log.w(TAG, "Could not wipe user " + userHandle);
667        }
668    }
669
670    /**
671     * Returns the maximum number of users that can be created on this device. A return value
672     * of 1 means that it is a single user device.
673     * @hide
674     * @return a value greater than or equal to 1
675     */
676    public static int getMaxSupportedUsers() {
677        // Don't allow multiple users on certain builds
678        if (android.os.Build.ID.startsWith("JVP")) return 1;
679        return SystemProperties.getInt("fw.max_users",
680                Resources.getSystem().getInteger(R.integer.config_multiuserMaximumUsers));
681    }
682
683    /**
684     * Returns true if the user switcher should be shown, this will be if there
685     * are multiple users that aren't managed profiles.
686     * @hide
687     * @return true if user switcher should be shown.
688     */
689    public boolean isUserSwitcherEnabled() {
690        List<UserInfo> users = getUsers(true);
691        if (users == null) {
692           return false;
693        }
694        int switchableUserCount = 0;
695        for (UserInfo user : users) {
696            if (user.supportsSwitchTo()) {
697                ++switchableUserCount;
698            }
699        }
700        return switchableUserCount > 1;
701    }
702
703    /**
704     * Returns a serial number on this device for a given userHandle. User handles can be recycled
705     * when deleting and creating users, but serial numbers are not reused until the device is wiped.
706     * @param userHandle
707     * @return a serial number associated with that user, or -1 if the userHandle is not valid.
708     * @hide
709     */
710    public int getUserSerialNumber(int userHandle) {
711        try {
712            return mService.getUserSerialNumber(userHandle);
713        } catch (RemoteException re) {
714            Log.w(TAG, "Could not get serial number for user " + userHandle);
715        }
716        return -1;
717    }
718
719    /**
720     * Returns a userHandle on this device for a given user serial number. User handles can be
721     * recycled when deleting and creating users, but serial numbers are not reused until the device
722     * is wiped.
723     * @param userSerialNumber
724     * @return the userHandle associated with that user serial number, or -1 if the serial number
725     * is not valid.
726     * @hide
727     */
728    public int getUserHandle(int userSerialNumber) {
729        try {
730            return mService.getUserHandle(userSerialNumber);
731        } catch (RemoteException re) {
732            Log.w(TAG, "Could not get userHandle for user " + userSerialNumber);
733        }
734        return -1;
735    }
736
737    /**
738     * Returns a Bundle containing any saved application restrictions for this user, for the
739     * given package name. Only an application with this package name can call this method.
740     * @param packageName the package name of the calling application
741     * @return a Bundle with the restrictions as key/value pairs, or null if there are no
742     * saved restrictions. The values can be of type Boolean, String or String[], depending
743     * on the restriction type, as defined by the application.
744     */
745    public Bundle getApplicationRestrictions(String packageName) {
746        try {
747            return mService.getApplicationRestrictions(packageName);
748        } catch (RemoteException re) {
749            Log.w(TAG, "Could not get application restrictions for package " + packageName);
750        }
751        return null;
752    }
753
754    /**
755     * @hide
756     */
757    public Bundle getApplicationRestrictions(String packageName, UserHandle user) {
758        try {
759            return mService.getApplicationRestrictionsForUser(packageName, user.getIdentifier());
760        } catch (RemoteException re) {
761            Log.w(TAG, "Could not get application restrictions for user " + user.getIdentifier());
762        }
763        return null;
764    }
765
766    /**
767     * @hide
768     */
769    public void setApplicationRestrictions(String packageName, Bundle restrictions,
770            UserHandle user) {
771        try {
772            mService.setApplicationRestrictions(packageName, restrictions, user.getIdentifier());
773        } catch (RemoteException re) {
774            Log.w(TAG, "Could not set application restrictions for user " + user.getIdentifier());
775        }
776    }
777
778    /**
779     * Sets a new challenge PIN for restrictions. This is only for use by pre-installed
780     * apps and requires the MANAGE_USERS permission.
781     * @param newPin the PIN to use for challenge dialogs.
782     * @return Returns true if the challenge PIN was set successfully.
783     */
784    public boolean setRestrictionsChallenge(String newPin) {
785        try {
786            return mService.setRestrictionsChallenge(newPin);
787        } catch (RemoteException re) {
788            Log.w(TAG, "Could not change restrictions pin");
789        }
790        return false;
791    }
792
793    /**
794     * @hide
795     * @param pin The PIN to verify, or null to get the number of milliseconds to wait for before
796     * allowing the user to enter the PIN.
797     * @return Returns a positive number (including zero) for how many milliseconds before
798     * you can accept another PIN, when the input is null or the input doesn't match the saved PIN.
799     * Returns {@link #PIN_VERIFICATION_SUCCESS} if the input matches the saved PIN. Returns
800     * {@link #PIN_VERIFICATION_FAILED_NOT_SET} if there is no PIN set.
801     */
802    public int checkRestrictionsChallenge(String pin) {
803        try {
804            return mService.checkRestrictionsChallenge(pin);
805        } catch (RemoteException re) {
806            Log.w(TAG, "Could not check restrictions pin");
807        }
808        return PIN_VERIFICATION_FAILED_INCORRECT;
809    }
810
811    /**
812     * @hide
813     * Checks whether the user has restrictions that are PIN-protected. An application that
814     * participates in restrictions can check if the owner has requested a PIN challenge for
815     * any restricted operations. If there is a PIN in effect, the application should launch
816     * the PIN challenge activity {@link android.content.Intent#ACTION_RESTRICTIONS_CHALLENGE}.
817     * @see android.content.Intent#ACTION_RESTRICTIONS_CHALLENGE
818     * @return whether a restrictions PIN is in effect.
819     */
820    public boolean hasRestrictionsChallenge() {
821        try {
822            return mService.hasRestrictionsChallenge();
823        } catch (RemoteException re) {
824            Log.w(TAG, "Could not change restrictions pin");
825        }
826        return false;
827    }
828
829    /** @hide */
830    public void removeRestrictions() {
831        try {
832            mService.removeRestrictions();
833        } catch (RemoteException re) {
834            Log.w(TAG, "Could not change restrictions pin");
835        }
836    }
837}
838