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