UserManager.java revision 7a2b4d11c741de8b78570c0e11f49deb165e35da
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 {@link android.Manifest.permission#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 {@link android.Manifest.permission#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 {@link android.Manifest.permission#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     *
470     * Requires {@link android.Manifest.permission#MANAGE_USERS} permission.
471     * @param userHandle profiles of this user will be returned.
472     * @return the list of profiles.
473     * @hide
474     */
475    public List<UserInfo> getProfiles(int userHandle) {
476        try {
477            return mService.getProfiles(userHandle);
478        } catch (RemoteException re) {
479            Log.w(TAG, "Could not get user list", re);
480            return null;
481        }
482    }
483
484    /**
485     * Returns a list of UserHandles for profiles associated with this user, including this user.
486     *
487     * @return A non-empty list of UserHandles associated with the calling user.
488     */
489    public List<UserHandle> getUserProfiles() {
490        ArrayList<UserHandle> profiles = new ArrayList<UserHandle>();
491        List<UserInfo> users = getProfiles(UserHandle.myUserId());
492        for (UserInfo info : users) {
493            UserHandle userHandle = new UserHandle(info.id);
494            profiles.add(userHandle);
495        }
496        return profiles;
497    }
498
499    /**
500     * If the target user is a managed profile of the calling user or the caller
501     * is itself a managed profile, then this returns a badged copy of the given
502     * icon to be able to distinguish it from the original icon.
503     * <P>
504     * If the original drawable is not a BitmapDrawable, then the original
505     * drawable is returned.
506     * </P>
507     *
508     * @param icon The icon to badge.
509     * @param user The target user.
510     * @return A drawable that combines the original icon and a badge as
511     *         determined by the system.
512     */
513    public Drawable getBadgedDrawableForUser(Drawable icon, UserHandle user) {
514        int badgeResId = getBadgeResIdForUser(user.getIdentifier());
515        if (badgeResId == 0) {
516            return icon;
517        } else {
518            Drawable badgeIcon = mContext.getPackageManager()
519                    .getDrawable("system", badgeResId, null);
520            return getMergedDrawable(icon, badgeIcon);
521        }
522    }
523
524    private int getBadgeResIdForUser(int userHandle) {
525        // Return the framework-provided badge.
526        List<UserInfo> userProfiles = getProfiles(UserHandle.myUserId());
527        for (UserInfo user : userProfiles) {
528            if (user.id == userHandle
529                    && user.isManagedProfile()) {
530                return com.android.internal.R.drawable.ic_corp_badge;
531            }
532        }
533        return 0;
534    }
535
536    private Drawable getMergedDrawable(Drawable icon, Drawable badge) {
537        final int width = icon.getIntrinsicWidth();
538        final int height = icon.getIntrinsicHeight();
539        Bitmap bitmap = Bitmap.createBitmap(width, height, Config.ARGB_8888);
540        Canvas canvas = new Canvas(bitmap);
541        icon.setBounds(0, 0, width, height);
542        icon.draw(canvas);
543        badge.setBounds(0, 0, width, height);
544        badge.draw(canvas);
545        BitmapDrawable merged = new BitmapDrawable(bitmap);
546        if (icon instanceof BitmapDrawable) {
547            merged.setTargetDensity(((BitmapDrawable) icon).getBitmap().getDensity());
548        }
549        return merged;
550    }
551
552    /**
553     * Returns information for all users on this device. Requires
554     * {@link android.Manifest.permission#MANAGE_USERS} permission.
555     *
556     * @param excludeDying specify if the list should exclude users being
557     *            removed.
558     * @return the list of users that were created.
559     * @hide
560     */
561    public List<UserInfo> getUsers(boolean excludeDying) {
562        try {
563            return mService.getUsers(excludeDying);
564        } catch (RemoteException re) {
565            Log.w(TAG, "Could not get user list", re);
566            return null;
567        }
568    }
569
570    /**
571     * Removes a user and all associated data.
572     * Requires {@link android.Manifest.permission#MANAGE_USERS} permission.
573     * @param userHandle the integer handle of the user, where 0 is the primary user.
574     * @hide
575     */
576    public boolean removeUser(int userHandle) {
577        try {
578            return mService.removeUser(userHandle);
579        } catch (RemoteException re) {
580            Log.w(TAG, "Could not remove user ", re);
581            return false;
582        }
583    }
584
585    /**
586     * Updates the user's name.
587     * Requires {@link android.Manifest.permission#MANAGE_USERS} permission.
588     *
589     * @param userHandle the user's integer handle
590     * @param name the new name for the user
591     * @hide
592     */
593    public void setUserName(int userHandle, String name) {
594        try {
595            mService.setUserName(userHandle, name);
596        } catch (RemoteException re) {
597            Log.w(TAG, "Could not set the user name ", re);
598        }
599    }
600
601    /**
602     * Sets the user's photo.
603     * @param userHandle the user for whom to change the photo.
604     * @param icon the bitmap to set as the photo.
605     * @hide
606     */
607    public void setUserIcon(int userHandle, Bitmap icon) {
608        try {
609            mService.setUserIcon(userHandle, icon);
610        } catch (RemoteException re) {
611            Log.w(TAG, "Could not set the user icon ", re);
612        }
613    }
614
615    /**
616     * Returns a file descriptor for the user's photo. PNG data can be read from this file.
617     * @param userHandle the user whose photo we want to read.
618     * @return a {@link Bitmap} of the user's photo, or null if there's no photo.
619     * @hide
620     */
621    public Bitmap getUserIcon(int userHandle) {
622        try {
623            return mService.getUserIcon(userHandle);
624        } catch (RemoteException re) {
625            Log.w(TAG, "Could not get the user icon ", re);
626            return null;
627        }
628    }
629
630    /**
631     * Enable or disable the use of a guest account. If disabled, the existing guest account
632     * will be wiped.
633     * Requires {@link android.Manifest.permission#MANAGE_USERS} permission.
634     * @param enable whether to enable a guest account.
635     * @hide
636     */
637    public void setGuestEnabled(boolean enable) {
638        try {
639            mService.setGuestEnabled(enable);
640        } catch (RemoteException re) {
641            Log.w(TAG, "Could not change guest account availability to " + enable);
642        }
643    }
644
645    /**
646     * Checks if a guest user is enabled for this device.
647     * Requires {@link android.Manifest.permission#MANAGE_USERS} permission.
648     * @return whether a guest user is enabled
649     * @hide
650     */
651    public boolean isGuestEnabled() {
652        try {
653            return mService.isGuestEnabled();
654        } catch (RemoteException re) {
655            Log.w(TAG, "Could not retrieve guest enabled state");
656            return false;
657        }
658    }
659
660    /**
661     * Wipes all the data for a user, but doesn't remove the user.
662     * Requires {@link android.Manifest.permission#MANAGE_USERS} permission.
663     * @param userHandle
664     * @hide
665     */
666    public void wipeUser(int userHandle) {
667        try {
668            mService.wipeUser(userHandle);
669        } catch (RemoteException re) {
670            Log.w(TAG, "Could not wipe user " + userHandle);
671        }
672    }
673
674    /**
675     * Returns the maximum number of users that can be created on this device. A return value
676     * of 1 means that it is a single user device.
677     * @hide
678     * @return a value greater than or equal to 1
679     */
680    public static int getMaxSupportedUsers() {
681        // Don't allow multiple users on certain builds
682        if (android.os.Build.ID.startsWith("JVP")) return 1;
683        return SystemProperties.getInt("fw.max_users",
684                Resources.getSystem().getInteger(R.integer.config_multiuserMaximumUsers));
685    }
686
687    /**
688     * Returns true if the user switcher should be shown, this will be if there
689     * are multiple users that aren't managed profiles.
690     * @hide
691     * @return true if user switcher should be shown.
692     */
693    public boolean isUserSwitcherEnabled() {
694        List<UserInfo> users = getUsers(true);
695        if (users == null) {
696           return false;
697        }
698        int switchableUserCount = 0;
699        for (UserInfo user : users) {
700            if (user.supportsSwitchTo()) {
701                ++switchableUserCount;
702            }
703        }
704        return switchableUserCount > 1;
705    }
706
707    /**
708     * Returns a serial number on this device for a given userHandle. User handles can be recycled
709     * when deleting and creating users, but serial numbers are not reused until the device is wiped.
710     * @param userHandle
711     * @return a serial number associated with that user, or -1 if the userHandle is not valid.
712     * @hide
713     */
714    public int getUserSerialNumber(int userHandle) {
715        try {
716            return mService.getUserSerialNumber(userHandle);
717        } catch (RemoteException re) {
718            Log.w(TAG, "Could not get serial number for user " + userHandle);
719        }
720        return -1;
721    }
722
723    /**
724     * Returns a userHandle on this device for a given user serial number. User handles can be
725     * recycled when deleting and creating users, but serial numbers are not reused until the device
726     * is wiped.
727     * @param userSerialNumber
728     * @return the userHandle associated with that user serial number, or -1 if the serial number
729     * is not valid.
730     * @hide
731     */
732    public int getUserHandle(int userSerialNumber) {
733        try {
734            return mService.getUserHandle(userSerialNumber);
735        } catch (RemoteException re) {
736            Log.w(TAG, "Could not get userHandle for user " + userSerialNumber);
737        }
738        return -1;
739    }
740
741    /**
742     * Returns a Bundle containing any saved application restrictions for this user, for the
743     * given package name. Only an application with this package name can call this method.
744     * @param packageName the package name of the calling application
745     * @return a Bundle with the restrictions as key/value pairs, or null if there are no
746     * saved restrictions. The values can be of type Boolean, String or String[], depending
747     * on the restriction type, as defined by the application.
748     */
749    public Bundle getApplicationRestrictions(String packageName) {
750        try {
751            return mService.getApplicationRestrictions(packageName);
752        } catch (RemoteException re) {
753            Log.w(TAG, "Could not get application restrictions for package " + packageName);
754        }
755        return null;
756    }
757
758    /**
759     * @hide
760     */
761    public Bundle getApplicationRestrictions(String packageName, UserHandle user) {
762        try {
763            return mService.getApplicationRestrictionsForUser(packageName, user.getIdentifier());
764        } catch (RemoteException re) {
765            Log.w(TAG, "Could not get application restrictions for user " + user.getIdentifier());
766        }
767        return null;
768    }
769
770    /**
771     * @hide
772     */
773    public void setApplicationRestrictions(String packageName, Bundle restrictions,
774            UserHandle user) {
775        try {
776            mService.setApplicationRestrictions(packageName, restrictions, user.getIdentifier());
777        } catch (RemoteException re) {
778            Log.w(TAG, "Could not set application restrictions for user " + user.getIdentifier());
779        }
780    }
781
782    /**
783     * Sets a new challenge PIN for restrictions. This is only for use by pre-installed
784     * apps and requires the MANAGE_USERS permission.
785     * @param newPin the PIN to use for challenge dialogs.
786     * @return Returns true if the challenge PIN was set successfully.
787     */
788    public boolean setRestrictionsChallenge(String newPin) {
789        try {
790            return mService.setRestrictionsChallenge(newPin);
791        } catch (RemoteException re) {
792            Log.w(TAG, "Could not change restrictions pin");
793        }
794        return false;
795    }
796
797    /**
798     * @hide
799     * @param pin The PIN to verify, or null to get the number of milliseconds to wait for before
800     * allowing the user to enter the PIN.
801     * @return Returns a positive number (including zero) for how many milliseconds before
802     * you can accept another PIN, when the input is null or the input doesn't match the saved PIN.
803     * Returns {@link #PIN_VERIFICATION_SUCCESS} if the input matches the saved PIN. Returns
804     * {@link #PIN_VERIFICATION_FAILED_NOT_SET} if there is no PIN set.
805     */
806    public int checkRestrictionsChallenge(String pin) {
807        try {
808            return mService.checkRestrictionsChallenge(pin);
809        } catch (RemoteException re) {
810            Log.w(TAG, "Could not check restrictions pin");
811        }
812        return PIN_VERIFICATION_FAILED_INCORRECT;
813    }
814
815    /**
816     * @hide
817     * Checks whether the user has restrictions that are PIN-protected. An application that
818     * participates in restrictions can check if the owner has requested a PIN challenge for
819     * any restricted operations. If there is a PIN in effect, the application should launch
820     * the PIN challenge activity {@link android.content.Intent#ACTION_RESTRICTIONS_CHALLENGE}.
821     * @see android.content.Intent#ACTION_RESTRICTIONS_CHALLENGE
822     * @return whether a restrictions PIN is in effect.
823     */
824    public boolean hasRestrictionsChallenge() {
825        try {
826            return mService.hasRestrictionsChallenge();
827        } catch (RemoteException re) {
828            Log.w(TAG, "Could not change restrictions pin");
829        }
830        return false;
831    }
832
833    /** @hide */
834    public void removeRestrictions() {
835        try {
836            mService.removeRestrictions();
837        } catch (RemoteException re) {
838            Log.w(TAG, "Could not change restrictions pin");
839        }
840    }
841}
842