UserManager.java revision 8cd28b57ed732656d002d97879e15c5695b54fff
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.annotation.SystemApi;
19import android.app.ActivityManager;
20import android.app.ActivityManagerNative;
21import android.content.Context;
22import android.content.pm.UserInfo;
23import android.content.res.Resources;
24import android.graphics.Bitmap;
25import android.graphics.Rect;
26import android.graphics.drawable.Drawable;
27import android.provider.Settings;
28import android.util.Log;
29import android.view.WindowManager.LayoutParams;
30
31import com.android.internal.R;
32
33import java.util.ArrayList;
34import java.util.List;
35
36/**
37 * Manages users and user details on a multi-user system.
38 */
39public class UserManager {
40
41    private static String TAG = "UserManager";
42    private final IUserManager mService;
43    private final Context mContext;
44
45    /**
46     * Specifies if a user is disallowed from adding and removing accounts.
47     * The default value is <code>false</code>.
48     *
49     * <p/>Key for user restrictions.
50     * <p/>Type: Boolean
51     * @see #setUserRestrictions(Bundle)
52     * @see #getUserRestrictions()
53     */
54    public static final String DISALLOW_MODIFY_ACCOUNTS = "no_modify_accounts";
55
56    /**
57     * Specifies if a user is disallowed from changing Wi-Fi
58     * access points. The default value is <code>false</code>.
59     *
60     * <p/>Key for user restrictions.
61     * <p/>Type: Boolean
62     * @see #setUserRestrictions(Bundle)
63     * @see #getUserRestrictions()
64     */
65    public static final String DISALLOW_CONFIG_WIFI = "no_config_wifi";
66
67    /**
68     * Specifies if a user is disallowed from installing applications.
69     * The default value is <code>false</code>.
70     *
71     * <p/>Key for user restrictions.
72     * <p/>Type: Boolean
73     * @see #setUserRestrictions(Bundle)
74     * @see #getUserRestrictions()
75     */
76    public static final String DISALLOW_INSTALL_APPS = "no_install_apps";
77
78    /**
79     * Specifies if a user is disallowed from uninstalling applications.
80     * The default value is <code>false</code>.
81     *
82     * <p/>Key for user restrictions.
83     * <p/>Type: Boolean
84     * @see #setUserRestrictions(Bundle)
85     * @see #getUserRestrictions()
86     */
87    public static final String DISALLOW_UNINSTALL_APPS = "no_uninstall_apps";
88
89    /**
90     * Specifies if a user is disallowed from toggling location sharing.
91     * The default value is <code>false</code>.
92     *
93     * <p/>Key for user restrictions.
94     * <p/>Type: Boolean
95     * @see #setUserRestrictions(Bundle)
96     * @see #getUserRestrictions()
97     */
98    public static final String DISALLOW_SHARE_LOCATION = "no_share_location";
99
100    /**
101     * Specifies if a user is disallowed from enabling the
102     * "Unknown Sources" setting, that allows installation of apps from unknown sources.
103     * The default value is <code>false</code>.
104     *
105     * <p/>Key for user restrictions.
106     * <p/>Type: Boolean
107     * @see #setUserRestrictions(Bundle)
108     * @see #getUserRestrictions()
109     */
110    public static final String DISALLOW_INSTALL_UNKNOWN_SOURCES = "no_install_unknown_sources";
111
112    /**
113     * Specifies if a user is disallowed from configuring bluetooth.
114     * The default value is <code>false</code>.
115     *
116     * <p/>Key for user restrictions.
117     * <p/>Type: Boolean
118     * @see #setUserRestrictions(Bundle)
119     * @see #getUserRestrictions()
120     */
121    public static final String DISALLOW_CONFIG_BLUETOOTH = "no_config_bluetooth";
122
123    /**
124     * Specifies if a user is disallowed from transferring files over
125     * USB. This can only be set by device owners. The default value is <code>false</code>.
126     *
127     * <p/>Key for user restrictions.
128     * <p/>Type: Boolean
129     * @see #setUserRestrictions(Bundle)
130     * @see #getUserRestrictions()
131     */
132    public static final String DISALLOW_USB_FILE_TRANSFER = "no_usb_file_transfer";
133
134    /**
135     * Specifies if a user is disallowed from configuring user
136     * credentials. The default value is <code>false</code>.
137     *
138     * <p/>Key for user restrictions.
139     * <p/>Type: Boolean
140     * @see #setUserRestrictions(Bundle)
141     * @see #getUserRestrictions()
142     */
143    public static final String DISALLOW_CONFIG_CREDENTIALS = "no_config_credentials";
144
145    /**
146     * Specifies if a user is disallowed from removing itself and other
147     * users. The default value is <code>false</code>.
148     *
149     * <p/>Key for user restrictions.
150     * <p/>Type: Boolean
151     * @see #setUserRestrictions(Bundle)
152     * @see #getUserRestrictions()
153     */
154    public static final String DISALLOW_REMOVE_USER = "no_remove_user";
155
156    /**
157     * Specifies if a user is disallowed from enabling or
158     * accessing debugging features. The default value is <code>false</code>.
159     *
160     * <p/>Key for user restrictions.
161     * <p/>Type: Boolean
162     * @see #setUserRestrictions(Bundle)
163     * @see #getUserRestrictions()
164     */
165    public static final String DISALLOW_DEBUGGING_FEATURES = "no_debugging_features";
166
167    /**
168     * Specifies if a user is disallowed from configuring VPN.
169     * The default value is <code>false</code>.
170     *
171     * <p/>Key for user restrictions.
172     * <p/>Type: Boolean
173     * @see #setUserRestrictions(Bundle)
174     * @see #getUserRestrictions()
175     */
176    public static final String DISALLOW_CONFIG_VPN = "no_config_vpn";
177
178    /**
179     * Specifies if a user is disallowed from configuring Tethering
180     * & portable hotspots. This can only be set by device owners. The default value is
181     * <code>false</code>.
182     *
183     * <p/>Key for user restrictions.
184     * <p/>Type: Boolean
185     * @see #setUserRestrictions(Bundle)
186     * @see #getUserRestrictions()
187     */
188    public static final String DISALLOW_CONFIG_TETHERING = "no_config_tethering";
189
190    /**
191     * Specifies if a user is disallowed from factory resetting
192     * from Settings. This can only be set by device owners. The default value is
193     * <code>false</code>.
194     *
195     * <p/>Key for user restrictions.
196     * <p/>Type: Boolean
197     * @see #setUserRestrictions(Bundle)
198     * @see #getUserRestrictions()
199     */
200    public static final String DISALLOW_FACTORY_RESET = "no_factory_reset";
201
202    /**
203     * Specifies if a user is disallowed from adding new users and
204     * profiles. This can only be set by device owners. The default value is <code>false</code>.
205     *
206     * <p/>Key for user restrictions.
207     * <p/>Type: Boolean
208     * @see #setUserRestrictions(Bundle)
209     * @see #getUserRestrictions()
210     */
211    public static final String DISALLOW_ADD_USER = "no_add_user";
212
213    /**
214     * Specifies if a user is disallowed from disabling application
215     * verification. The default value is <code>false</code>.
216     *
217     * <p/>Key for user restrictions.
218     * <p/>Type: Boolean
219     * @see #setUserRestrictions(Bundle)
220     * @see #getUserRestrictions()
221     */
222    public static final String ENSURE_VERIFY_APPS = "ensure_verify_apps";
223
224    /**
225     * Specifies if a user is disallowed from configuring cell
226     * broadcasts. This can only be set by device owners. The default value is <code>false</code>.
227     *
228     * <p/>Key for user restrictions.
229     * <p/>Type: Boolean
230     * @see #setUserRestrictions(Bundle)
231     * @see #getUserRestrictions()
232     */
233    public static final String DISALLOW_CONFIG_CELL_BROADCASTS = "no_config_cell_broadcasts";
234
235    /**
236     * Specifies if a user is disallowed from configuring mobile
237     * networks. This can only be set by device owners. The default value is <code>false</code>.
238     *
239     * <p/>Key for user restrictions.
240     * <p/>Type: Boolean
241     * @see #setUserRestrictions(Bundle)
242     * @see #getUserRestrictions()
243     */
244    public static final String DISALLOW_CONFIG_MOBILE_NETWORKS = "no_config_mobile_networks";
245
246    /**
247     * Specifies if a user is disallowed from modifying
248     * applications in Settings or launchers. The following actions will not be allowed when this
249     * restriction is enabled:
250     * <li>uninstalling apps</li>
251     * <li>disabling apps</li>
252     * <li>clearing app caches</li>
253     * <li>clearing app data</li>
254     * <li>force stopping apps</li>
255     * <li>clearing app defaults</li>
256     * <p>
257     * The default value is <code>false</code>.
258     *
259     * <p/>Key for user restrictions.
260     * <p/>Type: Boolean
261     * @see #setUserRestrictions(Bundle)
262     * @see #getUserRestrictions()
263     */
264    public static final String DISALLOW_APPS_CONTROL = "no_control_apps";
265
266    /**
267     * Specifies if a user is disallowed from mounting
268     * physical external media. This can only be set by device owners. The default value is
269     * <code>false</code>.
270     *
271     * <p/>Key for user restrictions.
272     * <p/>Type: Boolean
273     * @see #setUserRestrictions(Bundle)
274     * @see #getUserRestrictions()
275     */
276    public static final String DISALLOW_MOUNT_PHYSICAL_MEDIA = "no_physical_media";
277
278    /**
279     * Specifies if a user is disallowed from adjusting microphone
280     * volume. If set, the microphone will be muted. This can only be set by device owners.
281     * The default value is <code>false</code>.
282     *
283     * <p/>Key for user restrictions.
284     * <p/>Type: Boolean
285     * @see #setUserRestrictions(Bundle)
286     * @see #getUserRestrictions()
287     */
288    public static final String DISALLOW_UNMUTE_MICROPHONE = "no_unmute_microphone";
289
290    /**
291     * Specifies if a user is disallowed from adjusting the master
292     * volume. If set, the master volume will be muted. This can only be set by device owners.
293     * The default value is <code>false</code>.
294     *
295     * <p/>Key for user restrictions.
296     * <p/>Type: Boolean
297     * @see #setUserRestrictions(Bundle)
298     * @see #getUserRestrictions()
299     */
300    public static final String DISALLOW_ADJUST_VOLUME = "no_adjust_volume";
301
302    /**
303     * Specifies that the user is not allowed to make outgoing
304     * phone calls. Emergency calls are still permitted.
305     * The default value is <code>false</code>.
306     *
307     * <p/>Key for user restrictions.
308     * <p/>Type: Boolean
309     * @see #setUserRestrictions(Bundle)
310     * @see #getUserRestrictions()
311     */
312    public static final String DISALLOW_OUTGOING_CALLS = "no_outgoing_calls";
313
314    /**
315     * Specifies that the user is not allowed to send or receive
316     * SMS messages. This can only be set by device owners. The default value is <code>false</code>.
317     *
318     * <p/>Key for user restrictions.
319     * <p/>Type: Boolean
320     * @see #setUserRestrictions(Bundle)
321     * @see #getUserRestrictions()
322     */
323    public static final String DISALLOW_SMS = "no_sms";
324
325    /**
326     * Specifies that windows besides app windows should not be
327     * created. This will block the creation of the following types of windows.
328     * <li>{@link LayoutParams#TYPE_TOAST}</li>
329     * <li>{@link LayoutParams#TYPE_PHONE}</li>
330     * <li>{@link LayoutParams#TYPE_PRIORITY_PHONE}</li>
331     * <li>{@link LayoutParams#TYPE_SYSTEM_ALERT}</li>
332     * <li>{@link LayoutParams#TYPE_SYSTEM_ERROR}</li>
333     * <li>{@link LayoutParams#TYPE_SYSTEM_OVERLAY}</li>
334     *
335     * <p>This can only be set by device owners. The default value is <code>false</code>.
336     *
337     * <p/>Key for user restrictions.
338     * <p/>Type: Boolean
339     * @see #setUserRestrictions(Bundle)
340     * @see #getUserRestrictions()
341     */
342    public static final String DISALLOW_CREATE_WINDOWS = "no_create_windows";
343
344    /**
345     * Specifies if what is copied in the clipboard of this profile can
346     * be pasted in related profiles. Does not restrict if the clipboard of related profiles can be
347     * pasted in this profile.
348     * The default value is <code>false</code>.
349     *
350     * <p/>Key for user restrictions.
351     * <p/>Type: Boolean
352     * @see #setUserRestrictions(Bundle)
353     * @see #getUserRestrictions()
354     */
355    public static final String DISALLOW_CROSS_PROFILE_COPY_PASTE = "no_cross_profile_copy_paste";
356
357    /**
358     * Specifies if the user is not allowed to use NFC to beam out data from apps.
359     * The default value is <code>false</code>.
360     *
361     * <p/>Key for user restrictions.
362     * <p/>Type: Boolean
363     * @see #setUserRestrictions(Bundle)
364     * @see #getUserRestrictions()
365     */
366    public static final String DISALLOW_OUTGOING_BEAM = "no_outgoing_beam";
367
368    /** @hide */
369    public static final int PIN_VERIFICATION_FAILED_INCORRECT = -3;
370    /** @hide */
371    public static final int PIN_VERIFICATION_FAILED_NOT_SET = -2;
372    /** @hide */
373    public static final int PIN_VERIFICATION_SUCCESS = -1;
374
375    private static UserManager sInstance = null;
376
377    /** @hide */
378    public synchronized static UserManager get(Context context) {
379        if (sInstance == null) {
380            sInstance = (UserManager) context.getSystemService(Context.USER_SERVICE);
381        }
382        return sInstance;
383    }
384
385    /** @hide */
386    public UserManager(Context context, IUserManager service) {
387        mService = service;
388        mContext = context;
389    }
390
391    /**
392     * Returns whether the system supports multiple users.
393     * @return true if multiple users can be created by user, false if it is a single user device.
394     * @hide
395     */
396    public static boolean supportsMultipleUsers() {
397        return getMaxSupportedUsers() > 1
398                && SystemProperties.getBoolean("fw.show_multiuserui",
399                Resources.getSystem().getBoolean(R.bool.config_enableMultiUserUI));
400    }
401
402    /**
403     * Returns the user handle for the user that the calling process is running on.
404     *
405     * @return the user handle of the user making this call.
406     * @hide
407     */
408    public int getUserHandle() {
409        return UserHandle.myUserId();
410    }
411
412    /**
413     * Returns the user name of the user making this call.  This call is only
414     * available to applications on the system image; it requires the
415     * MANAGE_USERS permission.
416     * @return the user name
417     */
418    public String getUserName() {
419        try {
420            return mService.getUserInfo(getUserHandle()).name;
421        } catch (RemoteException re) {
422            Log.w(TAG, "Could not get user name", re);
423            return "";
424        }
425    }
426
427   /**
428     * Used to determine whether the user making this call is subject to
429     * teleportations.
430     * @return whether the user making this call is a goat
431     */
432    public boolean isUserAGoat() {
433        return false;
434    }
435
436    /**
437     * Used to check if the user making this call is linked to another user. Linked users may have
438     * a reduced number of available apps, app restrictions and account restrictions.
439     * @return whether the user making this call is a linked user
440     * @hide
441     */
442    public boolean isLinkedUser() {
443        try {
444            return mService.isRestricted();
445        } catch (RemoteException re) {
446            Log.w(TAG, "Could not check if user is limited ", re);
447            return false;
448        }
449    }
450
451    /**
452     * Checks if the calling app is running as a guest user.
453     * @return whether the caller is a guest user.
454     * @hide
455     */
456    public boolean isGuestUser() {
457        UserInfo user = getUserInfo(UserHandle.myUserId());
458        return user != null ? user.isGuest() : false;
459    }
460
461    /**
462     * Checks if the calling app is running in a managed profile.
463     * Requires {@link android.Manifest.permission#MANAGE_USERS} permission.
464     *
465     * @return whether the caller is in a managed profile.
466     * @hide
467     */
468    @SystemApi
469    public boolean isManagedProfile() {
470        UserInfo user = getUserInfo(UserHandle.myUserId());
471        return user != null ? user.isManagedProfile() : false;
472    }
473
474    /**
475     * Return whether the given user is actively running.  This means that
476     * the user is in the "started" state, not "stopped" -- it is currently
477     * allowed to run code through scheduled alarms, receiving broadcasts,
478     * etc.  A started user may be either the current foreground user or a
479     * background user; the result here does not distinguish between the two.
480     * @param user The user to retrieve the running state for.
481     */
482    public boolean isUserRunning(UserHandle user) {
483        try {
484            return ActivityManagerNative.getDefault().isUserRunning(
485                    user.getIdentifier(), false);
486        } catch (RemoteException e) {
487            return false;
488        }
489    }
490
491    /**
492     * Return whether the given user is actively running <em>or</em> stopping.
493     * This is like {@link #isUserRunning(UserHandle)}, but will also return
494     * true if the user had been running but is in the process of being stopped
495     * (but is not yet fully stopped, and still running some code).
496     * @param user The user to retrieve the running state for.
497     */
498    public boolean isUserRunningOrStopping(UserHandle user) {
499        try {
500            return ActivityManagerNative.getDefault().isUserRunning(
501                    user.getIdentifier(), true);
502        } catch (RemoteException e) {
503            return false;
504        }
505    }
506
507    /**
508     * Returns the UserInfo object describing a specific user.
509     * Requires {@link android.Manifest.permission#MANAGE_USERS} permission.
510     * @param userHandle the user handle of the user whose information is being requested.
511     * @return the UserInfo object for a specific user.
512     * @hide
513     */
514    public UserInfo getUserInfo(int userHandle) {
515        try {
516            return mService.getUserInfo(userHandle);
517        } catch (RemoteException re) {
518            Log.w(TAG, "Could not get user info", re);
519            return null;
520        }
521    }
522
523    /**
524     * Returns the user-wide restrictions imposed on this user.
525     * @return a Bundle containing all the restrictions.
526     */
527    public Bundle getUserRestrictions() {
528        return getUserRestrictions(Process.myUserHandle());
529    }
530
531    /**
532     * Returns the user-wide restrictions imposed on the user specified by <code>userHandle</code>.
533     * @param userHandle the UserHandle of the user for whom to retrieve the restrictions.
534     * @return a Bundle containing all the restrictions.
535     */
536    public Bundle getUserRestrictions(UserHandle userHandle) {
537        try {
538            return mService.getUserRestrictions(userHandle.getIdentifier());
539        } catch (RemoteException re) {
540            Log.w(TAG, "Could not get user restrictions", re);
541            return Bundle.EMPTY;
542        }
543    }
544
545    /**
546     * Sets all the user-wide restrictions for this user.
547     * Requires the MANAGE_USERS permission.
548     * @param restrictions the Bundle containing all the restrictions.
549     * @deprecated use {@link android.app.admin.DevicePolicyManager#addUserRestriction(
550     * android.content.ComponentName, String)} or
551     * {@link android.app.admin.DevicePolicyManager#clearUserRestriction(
552     * android.content.ComponentName, String)} instead.
553     */
554    @Deprecated
555    public void setUserRestrictions(Bundle restrictions) {
556        setUserRestrictions(restrictions, Process.myUserHandle());
557    }
558
559    /**
560     * Sets all the user-wide restrictions for the specified user.
561     * Requires the MANAGE_USERS permission.
562     * @param restrictions the Bundle containing all the restrictions.
563     * @param userHandle the UserHandle of the user for whom to set the restrictions.
564     * @deprecated use {@link android.app.admin.DevicePolicyManager#addUserRestriction(
565     * android.content.ComponentName, String)} or
566     * {@link android.app.admin.DevicePolicyManager#clearUserRestriction(
567     * android.content.ComponentName, String)} instead.
568     */
569    @Deprecated
570    public void setUserRestrictions(Bundle restrictions, UserHandle userHandle) {
571        try {
572            mService.setUserRestrictions(restrictions, userHandle.getIdentifier());
573        } catch (RemoteException re) {
574            Log.w(TAG, "Could not set user restrictions", re);
575        }
576    }
577
578    /**
579     * Sets the value of a specific restriction.
580     * Requires the MANAGE_USERS permission.
581     * @param key the key of the restriction
582     * @param value the value for the restriction
583     * @deprecated use {@link android.app.admin.DevicePolicyManager#addUserRestriction(
584     * android.content.ComponentName, String)} or
585     * {@link android.app.admin.DevicePolicyManager#clearUserRestriction(
586     * android.content.ComponentName, String)} instead.
587     */
588    @Deprecated
589    public void setUserRestriction(String key, boolean value) {
590        Bundle bundle = getUserRestrictions();
591        bundle.putBoolean(key, value);
592        setUserRestrictions(bundle);
593    }
594
595    /**
596     * @hide
597     * Sets the value of a specific restriction on a specific user.
598     * Requires the MANAGE_USERS permission.
599     * @param key the key of the restriction
600     * @param value the value for the restriction
601     * @param userHandle the user whose restriction is to be changed.
602     * @deprecated use {@link android.app.admin.DevicePolicyManager#addUserRestriction(
603     * android.content.ComponentName, String)} or
604     * {@link android.app.admin.DevicePolicyManager#clearUserRestriction(
605     * android.content.ComponentName, String)} instead.
606     */
607    @Deprecated
608    public void setUserRestriction(String key, boolean value, UserHandle userHandle) {
609        Bundle bundle = getUserRestrictions(userHandle);
610        bundle.putBoolean(key, value);
611        setUserRestrictions(bundle, userHandle);
612    }
613
614    /**
615     * Returns whether the current user has been disallowed from performing certain actions
616     * or setting certain settings.
617     *
618     * @param restrictionKey The string key representing the restriction.
619     * @return {@code true} if the current user has the given restriction, {@code false} otherwise.
620     */
621    public boolean hasUserRestriction(String restrictionKey) {
622        return hasUserRestriction(restrictionKey, Process.myUserHandle());
623    }
624
625    /**
626     * @hide
627     * Returns whether the given user has been disallowed from performing certain actions
628     * or setting certain settings.
629     * @param restrictionKey the string key representing the restriction
630     * @param userHandle the UserHandle of the user for whom to retrieve the restrictions.
631     */
632    public boolean hasUserRestriction(String restrictionKey, UserHandle userHandle) {
633        try {
634            return mService.hasUserRestriction(restrictionKey,
635                    userHandle.getIdentifier());
636        } catch (RemoteException re) {
637            Log.w(TAG, "Could not check user restrictions", re);
638            return false;
639        }
640    }
641
642    /**
643     * Return the serial number for a user.  This is a device-unique
644     * number assigned to that user; if the user is deleted and then a new
645     * user created, the new users will not be given the same serial number.
646     * @param user The user whose serial number is to be retrieved.
647     * @return The serial number of the given user; returns -1 if the
648     * given UserHandle does not exist.
649     * @see #getUserForSerialNumber(long)
650     */
651    public long getSerialNumberForUser(UserHandle user) {
652        return getUserSerialNumber(user.getIdentifier());
653    }
654
655    /**
656     * Return the user associated with a serial number previously
657     * returned by {@link #getSerialNumberForUser(UserHandle)}.
658     * @param serialNumber The serial number of the user that is being
659     * retrieved.
660     * @return Return the user associated with the serial number, or null
661     * if there is not one.
662     * @see #getSerialNumberForUser(UserHandle)
663     */
664    public UserHandle getUserForSerialNumber(long serialNumber) {
665        int ident = getUserHandle((int)serialNumber);
666        return ident >= 0 ? new UserHandle(ident) : null;
667    }
668
669    /**
670     * Creates a user with the specified name and options.
671     * Requires {@link android.Manifest.permission#MANAGE_USERS} permission.
672     *
673     * @param name the user's name
674     * @param flags flags that identify the type of user and other properties.
675     * @see UserInfo
676     *
677     * @return the UserInfo object for the created user, or null if the user could not be created.
678     * @hide
679     */
680    public UserInfo createUser(String name, int flags) {
681        try {
682            return mService.createUser(name, flags);
683        } catch (RemoteException re) {
684            Log.w(TAG, "Could not create a user", re);
685            return null;
686        }
687    }
688
689    /**
690     * Creates a guest user and configures it.
691     * @param context an application context
692     * @param name the name to set for the user
693     * @hide
694     */
695    public UserInfo createGuest(Context context, String name) {
696        UserInfo guest = createUser(name, UserInfo.FLAG_GUEST);
697        if (guest != null) {
698            Settings.Secure.putStringForUser(context.getContentResolver(),
699                    Settings.Secure.SKIP_FIRST_USE_HINTS, "1", guest.id);
700            try {
701                Bundle guestRestrictions = mService.getDefaultGuestRestrictions();
702                guestRestrictions.putBoolean(DISALLOW_SMS, true);
703                guestRestrictions.putBoolean(DISALLOW_INSTALL_UNKNOWN_SOURCES, true);
704                mService.setUserRestrictions(guestRestrictions, guest.id);
705            } catch (RemoteException re) {
706                Log.w(TAG, "Could not update guest restrictions");
707            }
708        }
709        return guest;
710    }
711
712    /**
713     * Creates a user with the specified name and options as a profile of another user.
714     * Requires {@link android.Manifest.permission#MANAGE_USERS} permission.
715     *
716     * @param name the user's name
717     * @param flags flags that identify the type of user and other properties.
718     * @see UserInfo
719     * @param userHandle new user will be a profile of this use.
720     *
721     * @return the UserInfo object for the created user, or null if the user could not be created.
722     * @hide
723     */
724    public UserInfo createProfileForUser(String name, int flags, int userHandle) {
725        try {
726            return mService.createProfileForUser(name, flags, userHandle);
727        } catch (RemoteException re) {
728            Log.w(TAG, "Could not create a user", re);
729            return null;
730        }
731    }
732
733    /**
734     * @hide
735     * Marks the guest user for deletion to allow a new guest to be created before deleting
736     * the current user who is a guest.
737     * @param userHandle
738     * @return
739     */
740    public boolean markGuestForDeletion(int userHandle) {
741        try {
742            return mService.markGuestForDeletion(userHandle);
743        } catch (RemoteException re) {
744            Log.w(TAG, "Could not mark guest for deletion", re);
745            return false;
746        }
747    }
748
749    /**
750     * Sets the user as enabled, if such an user exists.
751     * Requires {@link android.Manifest.permission#MANAGE_USERS} permission.
752     * Note that the default is true, it's only that managed profiles might not be enabled.
753     *
754     * @param userHandle the id of the profile to enable
755     * @hide
756     */
757    public void setUserEnabled(int userHandle) {
758        try {
759            mService.setUserEnabled(userHandle);
760        } catch (RemoteException e) {
761            Log.w(TAG, "Could not enable the profile", e);
762        }
763    }
764
765    /**
766     * Return the number of users currently created on the device.
767     */
768    public int getUserCount() {
769        List<UserInfo> users = getUsers();
770        return users != null ? users.size() : 1;
771    }
772
773    /**
774     * Returns information for all users on this device.
775     * Requires {@link android.Manifest.permission#MANAGE_USERS} permission.
776     * @return the list of users that were created.
777     * @hide
778     */
779    public List<UserInfo> getUsers() {
780        try {
781            return mService.getUsers(false);
782        } catch (RemoteException re) {
783            Log.w(TAG, "Could not get user list", re);
784            return null;
785        }
786    }
787
788    /**
789     * Checks whether it's possible to add more users. Caller must hold the MANAGE_USERS
790     * permission.
791     *
792     * @return true if more users can be added, false if limit has been reached.
793     * @hide
794     */
795    public boolean canAddMoreUsers() {
796        final List<UserInfo> users = getUsers(true);
797        final int totalUserCount = users.size();
798        int aliveUserCount = 0;
799        for (int i = 0; i < totalUserCount; i++) {
800            UserInfo user = users.get(i);
801            if (!user.isGuest()) {
802                aliveUserCount++;
803            }
804        }
805        return aliveUserCount < getMaxSupportedUsers();
806    }
807
808    /**
809     * Returns list of the profiles of userHandle including
810     * userHandle itself.
811     * Note that this returns both enabled and not enabled profiles. See
812     * {@link #getUserProfiles()} if you need only the enabled ones.
813     *
814     * Requires {@link android.Manifest.permission#MANAGE_USERS} permission.
815     * @param userHandle profiles of this user will be returned.
816     * @return the list of profiles.
817     * @hide
818     */
819    public List<UserInfo> getProfiles(int userHandle) {
820        try {
821            return mService.getProfiles(userHandle, false /* enabledOnly */);
822        } catch (RemoteException re) {
823            Log.w(TAG, "Could not get user list", re);
824            return null;
825        }
826    }
827
828    /**
829     * Returns a list of UserHandles for profiles associated with the user that the calling process
830     * is running on, including the user itself.
831     *
832     * @return A non-empty list of UserHandles associated with the calling user.
833     */
834    public List<UserHandle> getUserProfiles() {
835        ArrayList<UserHandle> profiles = new ArrayList<UserHandle>();
836        List<UserInfo> users = new ArrayList<UserInfo>();
837        try {
838            users = mService.getProfiles(UserHandle.myUserId(), true /* enabledOnly */);
839        } catch (RemoteException re) {
840            Log.w(TAG, "Could not get user list", re);
841            return null;
842        }
843        for (UserInfo info : users) {
844            UserHandle userHandle = new UserHandle(info.id);
845            profiles.add(userHandle);
846        }
847        return profiles;
848    }
849
850    /**
851     * Returns the parent of the profile which this method is called from
852     * or null if called from a user that is not a profile.
853     *
854     * @hide
855     */
856    public UserInfo getProfileParent(int userHandle) {
857        try {
858            return mService.getProfileParent(userHandle);
859        } catch (RemoteException re) {
860            Log.w(TAG, "Could not get profile parent", re);
861            return null;
862        }
863    }
864
865    /**
866     * If the target user is a managed profile of the calling user or the caller
867     * is itself a managed profile, then this returns a badged copy of the given
868     * icon to be able to distinguish it from the original icon. For badging an
869     * arbitrary drawable use {@link #getBadgedDrawableForUser(
870     * android.graphics.drawable.Drawable, UserHandle, android.graphics.Rect, int)}.
871     * <p>
872     * If the original drawable is a BitmapDrawable and the backing bitmap is
873     * mutable as per {@link android.graphics.Bitmap#isMutable()}, the bading
874     * is performed in place and the original drawable is returned.
875     * </p>
876     *
877     * @param icon The icon to badge.
878     * @param user The target user.
879     * @return A drawable that combines the original icon and a badge as
880     *         determined by the system.
881     * @removed
882     */
883    public Drawable getBadgedIconForUser(Drawable icon, UserHandle user) {
884        return mContext.getPackageManager().getUserBadgedIcon(icon, user);
885    }
886
887    /**
888     * If the target user is a managed profile of the calling user or the caller
889     * is itself a managed profile, then this returns a badged copy of the given
890     * drawable allowing the user to distinguish it from the original drawable.
891     * The caller can specify the location in the bounds of the drawable to be
892     * badged where the badge should be applied as well as the density of the
893     * badge to be used.
894     * <p>
895     * If the original drawable is a BitmapDrawable and the backing bitmap is
896     * mutable as per {@link android.graphics.Bitmap#isMutable()}, the bading
897     * is performed in place and the original drawable is returned.
898     * </p>
899     *
900     * @param badgedDrawable The drawable to badge.
901     * @param user The target user.
902     * @param badgeLocation Where in the bounds of the badged drawable to place
903     *         the badge. If not provided, the badge is applied on top of the entire
904     *         drawable being badged.
905     * @param badgeDensity The optional desired density for the badge as per
906     *         {@link android.util.DisplayMetrics#densityDpi}. If not provided,
907     *         the density of the display is used.
908     * @return A drawable that combines the original drawable and a badge as
909     *         determined by the system.
910     * @removed
911     */
912    public Drawable getBadgedDrawableForUser(Drawable badgedDrawable, UserHandle user,
913            Rect badgeLocation, int badgeDensity) {
914        return mContext.getPackageManager().getUserBadgedDrawableForDensity(badgedDrawable, user,
915                badgeLocation, badgeDensity);
916    }
917
918    /**
919     * If the target user is a managed profile of the calling user or the caller
920     * is itself a managed profile, then this returns a copy of the label with
921     * badging for accessibility services like talkback. E.g. passing in "Email"
922     * and it might return "Work Email" for Email in the work profile.
923     *
924     * @param label The label to change.
925     * @param user The target user.
926     * @return A label that combines the original label and a badge as
927     *         determined by the system.
928     * @removed
929     */
930    public CharSequence getBadgedLabelForUser(CharSequence label, UserHandle user) {
931        return mContext.getPackageManager().getUserBadgedLabel(label, user);
932    }
933
934    /**
935     * Returns information for all users on this device. Requires
936     * {@link android.Manifest.permission#MANAGE_USERS} permission.
937     *
938     * @param excludeDying specify if the list should exclude users being
939     *            removed.
940     * @return the list of users that were created.
941     * @hide
942     */
943    public List<UserInfo> getUsers(boolean excludeDying) {
944        try {
945            return mService.getUsers(excludeDying);
946        } catch (RemoteException re) {
947            Log.w(TAG, "Could not get user list", re);
948            return null;
949        }
950    }
951
952    /**
953     * Removes a user and all associated data.
954     * Requires {@link android.Manifest.permission#MANAGE_USERS} permission.
955     * @param userHandle the integer handle of the user, where 0 is the primary user.
956     * @hide
957     */
958    public boolean removeUser(int userHandle) {
959        try {
960            return mService.removeUser(userHandle);
961        } catch (RemoteException re) {
962            Log.w(TAG, "Could not remove user ", re);
963            return false;
964        }
965    }
966
967    /**
968     * Updates the user's name.
969     * Requires {@link android.Manifest.permission#MANAGE_USERS} permission.
970     *
971     * @param userHandle the user's integer handle
972     * @param name the new name for the user
973     * @hide
974     */
975    public void setUserName(int userHandle, String name) {
976        try {
977            mService.setUserName(userHandle, name);
978        } catch (RemoteException re) {
979            Log.w(TAG, "Could not set the user name ", re);
980        }
981    }
982
983    /**
984     * Sets the user's photo.
985     * @param userHandle the user for whom to change the photo.
986     * @param icon the bitmap to set as the photo.
987     * @hide
988     */
989    public void setUserIcon(int userHandle, Bitmap icon) {
990        try {
991            mService.setUserIcon(userHandle, icon);
992        } catch (RemoteException re) {
993            Log.w(TAG, "Could not set the user icon ", re);
994        }
995    }
996
997    /**
998     * Returns a file descriptor for the user's photo. PNG data can be read from this file.
999     * @param userHandle the user whose photo we want to read.
1000     * @return a {@link Bitmap} of the user's photo, or null if there's no photo.
1001     * @hide
1002     */
1003    public Bitmap getUserIcon(int userHandle) {
1004        try {
1005            return mService.getUserIcon(userHandle);
1006        } catch (RemoteException re) {
1007            Log.w(TAG, "Could not get the user icon ", re);
1008            return null;
1009        }
1010    }
1011
1012    /**
1013     * Returns the maximum number of users that can be created on this device. A return value
1014     * of 1 means that it is a single user device.
1015     * @hide
1016     * @return a value greater than or equal to 1
1017     */
1018    public static int getMaxSupportedUsers() {
1019        // Don't allow multiple users on certain builds
1020        if (android.os.Build.ID.startsWith("JVP")) return 1;
1021        // Svelte devices don't get multi-user.
1022        if (ActivityManager.isLowRamDeviceStatic()) return 1;
1023        return SystemProperties.getInt("fw.max_users",
1024                Resources.getSystem().getInteger(R.integer.config_multiuserMaximumUsers));
1025    }
1026
1027    /**
1028     * Returns true if the user switcher should be shown, this will be if there
1029     * are multiple users that aren't managed profiles.
1030     * @hide
1031     * @return true if user switcher should be shown.
1032     */
1033    public boolean isUserSwitcherEnabled() {
1034        List<UserInfo> users = getUsers(true);
1035        if (users == null) {
1036           return false;
1037        }
1038        int switchableUserCount = 0;
1039        for (UserInfo user : users) {
1040            if (user.supportsSwitchTo()) {
1041                ++switchableUserCount;
1042            }
1043        }
1044        final boolean guestEnabled = Settings.Global.getInt(mContext.getContentResolver(),
1045                Settings.Global.GUEST_USER_ENABLED, 0) == 1;
1046        return switchableUserCount > 1 || guestEnabled;
1047    }
1048
1049    /**
1050     * Returns a serial number on this device for a given userHandle. User handles can be recycled
1051     * when deleting and creating users, but serial numbers are not reused until the device is wiped.
1052     * @param userHandle
1053     * @return a serial number associated with that user, or -1 if the userHandle is not valid.
1054     * @hide
1055     */
1056    public int getUserSerialNumber(int userHandle) {
1057        try {
1058            return mService.getUserSerialNumber(userHandle);
1059        } catch (RemoteException re) {
1060            Log.w(TAG, "Could not get serial number for user " + userHandle);
1061        }
1062        return -1;
1063    }
1064
1065    /**
1066     * Returns a userHandle on this device for a given user serial number. User handles can be
1067     * recycled when deleting and creating users, but serial numbers are not reused until the device
1068     * is wiped.
1069     * @param userSerialNumber
1070     * @return the userHandle associated with that user serial number, or -1 if the serial number
1071     * is not valid.
1072     * @hide
1073     */
1074    public int getUserHandle(int userSerialNumber) {
1075        try {
1076            return mService.getUserHandle(userSerialNumber);
1077        } catch (RemoteException re) {
1078            Log.w(TAG, "Could not get userHandle for user " + userSerialNumber);
1079        }
1080        return -1;
1081    }
1082
1083    /**
1084     * Returns a Bundle containing any saved application restrictions for this user, for the
1085     * given package name. Only an application with this package name can call this method.
1086     * @param packageName the package name of the calling application
1087     * @return a Bundle with the restrictions as key/value pairs, or null if there are no
1088     * saved restrictions. The values can be of type Boolean, String or String[], depending
1089     * on the restriction type, as defined by the application.
1090     */
1091    public Bundle getApplicationRestrictions(String packageName) {
1092        try {
1093            return mService.getApplicationRestrictions(packageName);
1094        } catch (RemoteException re) {
1095            Log.w(TAG, "Could not get application restrictions for package " + packageName);
1096        }
1097        return null;
1098    }
1099
1100    /**
1101     * @hide
1102     */
1103    public Bundle getApplicationRestrictions(String packageName, UserHandle user) {
1104        try {
1105            return mService.getApplicationRestrictionsForUser(packageName, user.getIdentifier());
1106        } catch (RemoteException re) {
1107            Log.w(TAG, "Could not get application restrictions for user " + user.getIdentifier());
1108        }
1109        return null;
1110    }
1111
1112    /**
1113     * @hide
1114     */
1115    public void setApplicationRestrictions(String packageName, Bundle restrictions,
1116            UserHandle user) {
1117        try {
1118            mService.setApplicationRestrictions(packageName, restrictions, user.getIdentifier());
1119        } catch (RemoteException re) {
1120            Log.w(TAG, "Could not set application restrictions for user " + user.getIdentifier());
1121        }
1122    }
1123
1124    /**
1125     * Sets a new challenge PIN for restrictions. This is only for use by pre-installed
1126     * apps and requires the MANAGE_USERS permission.
1127     * @param newPin the PIN to use for challenge dialogs.
1128     * @return Returns true if the challenge PIN was set successfully.
1129     */
1130    public boolean setRestrictionsChallenge(String newPin) {
1131        try {
1132            return mService.setRestrictionsChallenge(newPin);
1133        } catch (RemoteException re) {
1134            Log.w(TAG, "Could not change restrictions pin");
1135        }
1136        return false;
1137    }
1138
1139    /**
1140     * @hide
1141     * @param pin The PIN to verify, or null to get the number of milliseconds to wait for before
1142     * allowing the user to enter the PIN.
1143     * @return Returns a positive number (including zero) for how many milliseconds before
1144     * you can accept another PIN, when the input is null or the input doesn't match the saved PIN.
1145     * Returns {@link #PIN_VERIFICATION_SUCCESS} if the input matches the saved PIN. Returns
1146     * {@link #PIN_VERIFICATION_FAILED_NOT_SET} if there is no PIN set.
1147     */
1148    public int checkRestrictionsChallenge(String pin) {
1149        try {
1150            return mService.checkRestrictionsChallenge(pin);
1151        } catch (RemoteException re) {
1152            Log.w(TAG, "Could not check restrictions pin");
1153        }
1154        return PIN_VERIFICATION_FAILED_INCORRECT;
1155    }
1156
1157    /**
1158     * @hide
1159     * Checks whether the user has restrictions that are PIN-protected. An application that
1160     * participates in restrictions can check if the owner has requested a PIN challenge for
1161     * any restricted operations. If there is a PIN in effect, the application should launch
1162     * the PIN challenge activity {@link android.content.Intent#ACTION_RESTRICTIONS_CHALLENGE}.
1163     * @see android.content.Intent#ACTION_RESTRICTIONS_CHALLENGE
1164     * @return whether a restrictions PIN is in effect.
1165     */
1166    public boolean hasRestrictionsChallenge() {
1167        try {
1168            return mService.hasRestrictionsChallenge();
1169        } catch (RemoteException re) {
1170            Log.w(TAG, "Could not change restrictions pin");
1171        }
1172        return false;
1173    }
1174
1175    /** @hide */
1176    public void removeRestrictions() {
1177        try {
1178            mService.removeRestrictions();
1179        } catch (RemoteException re) {
1180            Log.w(TAG, "Could not change restrictions pin");
1181        }
1182    }
1183
1184    /**
1185     * @hide
1186     * Set restrictions that should apply to any future guest user that's created.
1187     */
1188    public void setDefaultGuestRestrictions(Bundle restrictions) {
1189        try {
1190            mService.setDefaultGuestRestrictions(restrictions);
1191        } catch (RemoteException re) {
1192            Log.w(TAG, "Could not set guest restrictions");
1193        }
1194    }
1195
1196    /**
1197     * @hide
1198     * Gets the default guest restrictions.
1199     */
1200    public Bundle getDefaultGuestRestrictions() {
1201        try {
1202            return mService.getDefaultGuestRestrictions();
1203        } catch (RemoteException re) {
1204            Log.w(TAG, "Could not set guest restrictions");
1205        }
1206        return new Bundle();
1207    }
1208}
1209