UserManager.java revision b3b9258ab6a01af5e0df4b8385b73084cd9ec530
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.Manifest;
19import android.accounts.AccountManager;
20import android.annotation.Nullable;
21import android.annotation.RequiresPermission;
22import android.annotation.SystemApi;
23import android.app.ActivityManager;
24import android.app.ActivityManagerNative;
25import android.app.admin.DevicePolicyManager;
26import android.content.ComponentName;
27import android.content.Context;
28import android.content.pm.UserInfo;
29import android.content.res.Resources;
30import android.graphics.Bitmap;
31import android.graphics.BitmapFactory;
32import android.graphics.Rect;
33import android.graphics.drawable.Drawable;
34import android.provider.Settings;
35import android.util.Log;
36import android.view.WindowManager.LayoutParams;
37
38import com.android.internal.R;
39
40import java.io.IOException;
41import java.util.ArrayList;
42import java.util.List;
43
44/**
45 * Manages users and user details on a multi-user system.
46 */
47public class UserManager {
48
49    private static String TAG = "UserManager";
50    private final IUserManager mService;
51    private final Context mContext;
52
53    /**
54     * Specifies if a user is disallowed from adding and removing accounts, unless they are
55     * {@link android.accounts.AccountManager#addAccountExplicitly programmatically} added by
56     * Authenticator.
57     * The default value is <code>false</code>.
58     *
59     * <p/>Key for user restrictions.
60     * <p/>Type: Boolean
61     * @see DevicePolicyManager#addUserRestriction(ComponentName, String)
62     * @see DevicePolicyManager#clearUserRestriction(ComponentName, String)
63     * @see #getUserRestrictions()
64     */
65    public static final String DISALLOW_MODIFY_ACCOUNTS = "no_modify_accounts";
66
67    /**
68     * Specifies if a user is disallowed from changing Wi-Fi
69     * access points. The default value is <code>false</code>.
70     * <p/>This restriction has no effect in a managed profile.
71     *
72     * <p/>Key for user restrictions.
73     * <p/>Type: Boolean
74     * @see DevicePolicyManager#addUserRestriction(ComponentName, String)
75     * @see DevicePolicyManager#clearUserRestriction(ComponentName, String)
76     * @see #getUserRestrictions()
77     */
78    public static final String DISALLOW_CONFIG_WIFI = "no_config_wifi";
79
80    /**
81     * Specifies if a user is disallowed from installing applications.
82     * The default value is <code>false</code>.
83     *
84     * <p/>Key for user restrictions.
85     * <p/>Type: Boolean
86     * @see DevicePolicyManager#addUserRestriction(ComponentName, String)
87     * @see DevicePolicyManager#clearUserRestriction(ComponentName, String)
88     * @see #getUserRestrictions()
89     */
90    public static final String DISALLOW_INSTALL_APPS = "no_install_apps";
91
92    /**
93     * Specifies if a user is disallowed from uninstalling applications.
94     * The default value is <code>false</code>.
95     *
96     * <p/>Key for user restrictions.
97     * <p/>Type: Boolean
98     * @see DevicePolicyManager#addUserRestriction(ComponentName, String)
99     * @see DevicePolicyManager#clearUserRestriction(ComponentName, String)
100     * @see #getUserRestrictions()
101     */
102    public static final String DISALLOW_UNINSTALL_APPS = "no_uninstall_apps";
103
104    /**
105     * Specifies if a user is disallowed from turning on location sharing.
106     * The default value is <code>false</code>.
107     * <p/>In a managed profile, location sharing always reflects the primary user's setting, but
108     * can be overridden and forced off by setting this restriction to true in the managed profile.
109     *
110     * <p/>Key for user restrictions.
111     * <p/>Type: Boolean
112     * @see DevicePolicyManager#addUserRestriction(ComponentName, String)
113     * @see DevicePolicyManager#clearUserRestriction(ComponentName, String)
114     * @see #getUserRestrictions()
115     */
116    public static final String DISALLOW_SHARE_LOCATION = "no_share_location";
117
118    /**
119     * Specifies if a user is disallowed from enabling the
120     * "Unknown Sources" setting, that allows installation of apps from unknown sources.
121     * The default value is <code>false</code>.
122     *
123     * <p/>Key for user restrictions.
124     * <p/>Type: Boolean
125     * @see DevicePolicyManager#addUserRestriction(ComponentName, String)
126     * @see DevicePolicyManager#clearUserRestriction(ComponentName, String)
127     * @see #getUserRestrictions()
128     */
129    public static final String DISALLOW_INSTALL_UNKNOWN_SOURCES = "no_install_unknown_sources";
130
131    /**
132     * Specifies if a user is disallowed from configuring bluetooth.
133     * This does <em>not</em> restrict the user from turning bluetooth on or off.
134     * The default value is <code>false</code>.
135     * <p/>This restriction has no effect in a managed profile.
136     *
137     * <p/>Key for user restrictions.
138     * <p/>Type: Boolean
139     * @see DevicePolicyManager#addUserRestriction(ComponentName, String)
140     * @see DevicePolicyManager#clearUserRestriction(ComponentName, String)
141     * @see #getUserRestrictions()
142     */
143    public static final String DISALLOW_CONFIG_BLUETOOTH = "no_config_bluetooth";
144
145    /**
146     * Specifies if a user is disallowed from transferring files over
147     * USB. This can only be set by device owners and profile owners on the primary user.
148     * The default value is <code>false</code>.
149     *
150     * <p/>Key for user restrictions.
151     * <p/>Type: Boolean
152     * @see DevicePolicyManager#addUserRestriction(ComponentName, String)
153     * @see DevicePolicyManager#clearUserRestriction(ComponentName, String)
154     * @see #getUserRestrictions()
155     */
156    public static final String DISALLOW_USB_FILE_TRANSFER = "no_usb_file_transfer";
157
158    /**
159     * Specifies if a user is disallowed from configuring user
160     * credentials. The default value is <code>false</code>.
161     *
162     * <p/>Key for user restrictions.
163     * <p/>Type: Boolean
164     * @see DevicePolicyManager#addUserRestriction(ComponentName, String)
165     * @see DevicePolicyManager#clearUserRestriction(ComponentName, String)
166     * @see #getUserRestrictions()
167     */
168    public static final String DISALLOW_CONFIG_CREDENTIALS = "no_config_credentials";
169
170    /**
171     * When set on the primary user this specifies if the user can remove other users.
172     * When set on a secondary user, this specifies if the user can remove itself.
173     * This restriction has no effect on managed profiles.
174     * The default value is <code>false</code>.
175     *
176     * <p/>Key for user restrictions.
177     * <p/>Type: Boolean
178     * @see DevicePolicyManager#addUserRestriction(ComponentName, String)
179     * @see DevicePolicyManager#clearUserRestriction(ComponentName, String)
180     * @see #getUserRestrictions()
181     */
182    public static final String DISALLOW_REMOVE_USER = "no_remove_user";
183
184    /**
185     * Specifies if a user is disallowed from enabling or
186     * accessing debugging features. The default value is <code>false</code>.
187     *
188     * <p/>Key for user restrictions.
189     * <p/>Type: Boolean
190     * @see DevicePolicyManager#addUserRestriction(ComponentName, String)
191     * @see DevicePolicyManager#clearUserRestriction(ComponentName, String)
192     * @see #getUserRestrictions()
193     */
194    public static final String DISALLOW_DEBUGGING_FEATURES = "no_debugging_features";
195
196    /**
197     * Specifies if a user is disallowed from configuring VPN.
198     * The default value is <code>false</code>.
199     * This restriction has an effect in a managed profile only from
200     * {@link android.os.Build.VERSION_CODES#M}
201     *
202     * <p/>Key for user restrictions.
203     * <p/>Type: Boolean
204     * @see DevicePolicyManager#addUserRestriction(ComponentName, String)
205     * @see DevicePolicyManager#clearUserRestriction(ComponentName, String)
206     * @see #getUserRestrictions()
207     */
208    public static final String DISALLOW_CONFIG_VPN = "no_config_vpn";
209
210    /**
211     * Specifies if a user is disallowed from configuring Tethering
212     * & portable hotspots. This can only be set by device owners and profile owners on the
213     * primary user. The default value is <code>false</code>.
214     *
215     * <p/>Key for user restrictions.
216     * <p/>Type: Boolean
217     * @see DevicePolicyManager#addUserRestriction(ComponentName, String)
218     * @see DevicePolicyManager#clearUserRestriction(ComponentName, String)
219     * @see #getUserRestrictions()
220     */
221    public static final String DISALLOW_CONFIG_TETHERING = "no_config_tethering";
222
223    /**
224     * Specifies if a user is disallowed from resetting network settings
225     * from Settings. This can only be set by device owners and profile owners on the primary user.
226     * The default value is <code>false</code>.
227     * <p/>This restriction has no effect on secondary users and managed profiles since only the
228     * primary user can reset the network settings of the device.
229     *
230     * <p/>Key for user restrictions.
231     * <p/>Type: Boolean
232     * @see DevicePolicyManager#addUserRestriction(ComponentName, String)
233     * @see DevicePolicyManager#clearUserRestriction(ComponentName, String)
234     * @see #getUserRestrictions()
235     */
236    public static final String DISALLOW_NETWORK_RESET = "no_network_reset";
237
238    /**
239     * Specifies if a user is disallowed from factory resetting
240     * from Settings. This can only be set by device owners and profile owners on the primary user.
241     * The default value is <code>false</code>.
242     * <p/>This restriction has no effect on secondary users and managed profiles since only the
243     * primary user can factory reset the device.
244     *
245     * <p/>Key for user restrictions.
246     * <p/>Type: Boolean
247     * @see DevicePolicyManager#addUserRestriction(ComponentName, String)
248     * @see DevicePolicyManager#clearUserRestriction(ComponentName, String)
249     * @see #getUserRestrictions()
250     */
251    public static final String DISALLOW_FACTORY_RESET = "no_factory_reset";
252
253    /**
254     * Specifies if a user is disallowed from adding new users and
255     * profiles. This can only be set by device owners and profile owners on the primary user.
256     * The default value is <code>false</code>.
257     * <p/>This restriction has no effect on secondary users and managed profiles since only the
258     * primary user can add other users.
259     *
260     * <p/>Key for user restrictions.
261     * <p/>Type: Boolean
262     * @see DevicePolicyManager#addUserRestriction(ComponentName, String)
263     * @see DevicePolicyManager#clearUserRestriction(ComponentName, String)
264     * @see #getUserRestrictions()
265     */
266    public static final String DISALLOW_ADD_USER = "no_add_user";
267
268    /**
269     * Specifies if a user is disallowed from disabling application
270     * verification. The default value is <code>false</code>.
271     *
272     * <p/>Key for user restrictions.
273     * <p/>Type: Boolean
274     * @see DevicePolicyManager#addUserRestriction(ComponentName, String)
275     * @see DevicePolicyManager#clearUserRestriction(ComponentName, String)
276     * @see #getUserRestrictions()
277     */
278    public static final String ENSURE_VERIFY_APPS = "ensure_verify_apps";
279
280    /**
281     * Specifies if a user is disallowed from configuring cell
282     * broadcasts. This can only be set by device owners and profile owners on the primary user.
283     * The default value is <code>false</code>.
284     * <p/>This restriction has no effect on secondary users and managed profiles since only the
285     * primary user can configure cell broadcasts.
286     *
287     * <p/>Key for user restrictions.
288     * <p/>Type: Boolean
289     * @see DevicePolicyManager#addUserRestriction(ComponentName, String)
290     * @see DevicePolicyManager#clearUserRestriction(ComponentName, String)
291     * @see #getUserRestrictions()
292     */
293    public static final String DISALLOW_CONFIG_CELL_BROADCASTS = "no_config_cell_broadcasts";
294
295    /**
296     * Specifies if a user is disallowed from configuring mobile
297     * networks. This can only be set by device owners and profile owners on the primary user.
298     * The default value is <code>false</code>.
299     * <p/>This restriction has no effect on secondary users and managed profiles since only the
300     * primary user can configure mobile networks.
301     *
302     * <p/>Key for user restrictions.
303     * <p/>Type: Boolean
304     * @see DevicePolicyManager#addUserRestriction(ComponentName, String)
305     * @see DevicePolicyManager#clearUserRestriction(ComponentName, String)
306     * @see #getUserRestrictions()
307     */
308    public static final String DISALLOW_CONFIG_MOBILE_NETWORKS = "no_config_mobile_networks";
309
310    /**
311     * Specifies if a user is disallowed from modifying
312     * applications in Settings or launchers. The following actions will not be allowed when this
313     * restriction is enabled:
314     * <li>uninstalling apps</li>
315     * <li>disabling apps</li>
316     * <li>clearing app caches</li>
317     * <li>clearing app data</li>
318     * <li>force stopping apps</li>
319     * <li>clearing app defaults</li>
320     * <p>
321     * The default value is <code>false</code>.
322     *
323     * <p/>Key for user restrictions.
324     * <p/>Type: Boolean
325     * @see DevicePolicyManager#addUserRestriction(ComponentName, String)
326     * @see DevicePolicyManager#clearUserRestriction(ComponentName, String)
327     * @see #getUserRestrictions()
328     */
329    public static final String DISALLOW_APPS_CONTROL = "no_control_apps";
330
331    /**
332     * Specifies if a user is disallowed from mounting
333     * physical external media. This can only be set by device owners and profile owners on the
334     * primary user. The default value is <code>false</code>.
335     *
336     * <p/>Key for user restrictions.
337     * <p/>Type: Boolean
338     * @see DevicePolicyManager#addUserRestriction(ComponentName, String)
339     * @see DevicePolicyManager#clearUserRestriction(ComponentName, String)
340     * @see #getUserRestrictions()
341     */
342    public static final String DISALLOW_MOUNT_PHYSICAL_MEDIA = "no_physical_media";
343
344    /**
345     * Specifies if a user is disallowed from adjusting microphone
346     * volume. If set, the microphone will be muted. This can only be set by device owners
347     * and profile owners on the primary user. The default value is <code>false</code>.
348     *
349     * <p/>Key for user restrictions.
350     * <p/>Type: Boolean
351     * @see DevicePolicyManager#addUserRestriction(ComponentName, String)
352     * @see DevicePolicyManager#clearUserRestriction(ComponentName, String)
353     * @see #getUserRestrictions()
354     */
355    public static final String DISALLOW_UNMUTE_MICROPHONE = "no_unmute_microphone";
356
357    /**
358     * Specifies if a user is disallowed from adjusting the master
359     * volume. If set, the master volume will be muted. This can only be set by device owners
360     * and profile owners on the primary user. The default value is <code>false</code>.
361     *
362     * <p/>Key for user restrictions.
363     * <p/>Type: Boolean
364     * @see DevicePolicyManager#addUserRestriction(ComponentName, String)
365     * @see DevicePolicyManager#clearUserRestriction(ComponentName, String)
366     * @see #getUserRestrictions()
367     */
368    public static final String DISALLOW_ADJUST_VOLUME = "no_adjust_volume";
369
370    /**
371     * Specifies that the user is not allowed to make outgoing
372     * phone calls. Emergency calls are still permitted.
373     * The default value is <code>false</code>.
374     * <p/>This restriction has no effect on managed profiles since call intents are normally
375     * forwarded to the primary user.
376     *
377     * <p/>Key for user restrictions.
378     * <p/>Type: Boolean
379     * @see DevicePolicyManager#addUserRestriction(ComponentName, String)
380     * @see DevicePolicyManager#clearUserRestriction(ComponentName, String)
381     * @see #getUserRestrictions()
382     */
383    public static final String DISALLOW_OUTGOING_CALLS = "no_outgoing_calls";
384
385    /**
386     * Specifies that the user is not allowed to send or receive
387     * SMS messages. The default value is <code>false</code>.
388     *
389     * <p/>Key for user restrictions.
390     * <p/>Type: Boolean
391     * @see DevicePolicyManager#addUserRestriction(ComponentName, String)
392     * @see DevicePolicyManager#clearUserRestriction(ComponentName, String)
393     * @see #getUserRestrictions()
394     */
395    public static final String DISALLOW_SMS = "no_sms";
396
397    /**
398     * Specifies if the user is not allowed to have fun. In some cases, the
399     * device owner may wish to prevent the user from experiencing amusement or
400     * joy while using the device. The default value is <code>false</code>.
401     *
402     * <p/>Key for user restrictions.
403     * <p/>Type: Boolean
404     * @see DevicePolicyManager#addUserRestriction(ComponentName, String)
405     * @see DevicePolicyManager#clearUserRestriction(ComponentName, String)
406     * @see #getUserRestrictions()
407     */
408    public static final String DISALLOW_FUN = "no_fun";
409
410    /**
411     * Specifies that windows besides app windows should not be
412     * created. This will block the creation of the following types of windows.
413     * <li>{@link LayoutParams#TYPE_TOAST}</li>
414     * <li>{@link LayoutParams#TYPE_PHONE}</li>
415     * <li>{@link LayoutParams#TYPE_PRIORITY_PHONE}</li>
416     * <li>{@link LayoutParams#TYPE_SYSTEM_ALERT}</li>
417     * <li>{@link LayoutParams#TYPE_SYSTEM_ERROR}</li>
418     * <li>{@link LayoutParams#TYPE_SYSTEM_OVERLAY}</li>
419     *
420     * <p>This can only be set by device owners and profile owners on the primary user.
421     * The default value is <code>false</code>.
422     *
423     * <p/>Key for user restrictions.
424     * <p/>Type: Boolean
425     * @see DevicePolicyManager#addUserRestriction(ComponentName, String)
426     * @see DevicePolicyManager#clearUserRestriction(ComponentName, String)
427     * @see #getUserRestrictions()
428     */
429    public static final String DISALLOW_CREATE_WINDOWS = "no_create_windows";
430
431    /**
432     * Specifies if what is copied in the clipboard of this profile can
433     * be pasted in related profiles. Does not restrict if the clipboard of related profiles can be
434     * pasted in this profile.
435     * The default value is <code>false</code>.
436     *
437     * <p/>Key for user restrictions.
438     * <p/>Type: Boolean
439     * @see DevicePolicyManager#addUserRestriction(ComponentName, String)
440     * @see DevicePolicyManager#clearUserRestriction(ComponentName, String)
441     * @see #getUserRestrictions()
442     */
443    public static final String DISALLOW_CROSS_PROFILE_COPY_PASTE = "no_cross_profile_copy_paste";
444
445    /**
446     * Specifies if the user is not allowed to use NFC to beam out data from apps.
447     * The default value is <code>false</code>.
448     *
449     * <p/>Key for user restrictions.
450     * <p/>Type: Boolean
451     * @see DevicePolicyManager#addUserRestriction(ComponentName, String)
452     * @see DevicePolicyManager#clearUserRestriction(ComponentName, String)
453     * @see #getUserRestrictions()
454     */
455    public static final String DISALLOW_OUTGOING_BEAM = "no_outgoing_beam";
456
457    /**
458     * Hidden user restriction to disallow access to wallpaper manager APIs. This user restriction
459     * is always set for managed profiles.
460     * @hide
461     * @see DevicePolicyManager#addUserRestriction(ComponentName, String)
462     * @see DevicePolicyManager#clearUserRestriction(ComponentName, String)
463     * @see #getUserRestrictions()
464     */
465    public static final String DISALLOW_WALLPAPER = "no_wallpaper";
466
467    /**
468     * Specifies if the user is not allowed to reboot the device into safe boot mode.
469     * This can only be set by device owners and profile owners on the primary user.
470     * The default value is <code>false</code>.
471     *
472     * <p/>Key for user restrictions.
473     * <p/>Type: Boolean
474     * @see DevicePolicyManager#addUserRestriction(ComponentName, String)
475     * @see DevicePolicyManager#clearUserRestriction(ComponentName, String)
476     * @see #getUserRestrictions()
477     */
478    public static final String DISALLOW_SAFE_BOOT = "no_safe_boot";
479
480    /**
481     * Specifies if a user is not allowed to record audio. This restriction is always enabled for
482     * background users. The default value is <code>false</code>.
483     *
484     * @see DevicePolicyManager#addUserRestriction(ComponentName, String)
485     * @see DevicePolicyManager#clearUserRestriction(ComponentName, String)
486     * @see #getUserRestrictions()
487     * @hide
488     */
489    public static final String DISALLOW_RECORD_AUDIO = "no_record_audio";
490
491    /**
492     * Specifies if a user is not allowed to run in the background and should be stopped during
493     * user switch. The default value is <code>false</code>.
494     *
495     * <p>This restriction can be set by device owners and profile owners.
496     *
497     * @see DevicePolicyManager#addUserRestriction(ComponentName, String)
498     * @see DevicePolicyManager#clearUserRestriction(ComponentName, String)
499     * @see #getUserRestrictions()
500     * @hide
501     */
502    public static final String DISALLOW_RUN_IN_BACKGROUND = "no_run_in_background";
503
504    /**
505     * Specifies if a user is not allowed to use the camera.
506     *
507     * @see DevicePolicyManager#addUserRestriction(ComponentName, String)
508     * @see DevicePolicyManager#clearUserRestriction(ComponentName, String)
509     * @see #getUserRestrictions()
510     * @hide
511     */
512    public static final String DISALLOW_CAMERA = "no_camera";
513
514    /**
515     * Allows apps in the parent profile to handle web links from the managed profile.
516     *
517     * This user restriction has an effect only in a managed profile.
518     * If set:
519     * Intent filters of activities in the parent profile with action
520     * {@link android.content.Intent#ACTION_VIEW},
521     * category {@link android.content.Intent#CATEGORY_BROWSABLE}, scheme http or https, and which
522     * define a host can handle intents from the managed profile.
523     * The default value is <code>false</code>.
524     *
525     * <p/>Key for user restrictions.
526     * <p/>Type: Boolean
527     * @see DevicePolicyManager#addUserRestriction(ComponentName, String)
528     * @see DevicePolicyManager#clearUserRestriction(ComponentName, String)
529     * @see #getUserRestrictions()
530     */
531    public static final String ALLOW_PARENT_PROFILE_APP_LINKING
532            = "allow_parent_profile_app_linking";
533
534    /**
535     * Application restriction key that is used to indicate the pending arrival
536     * of real restrictions for the app.
537     *
538     * <p>
539     * Applications that support restrictions should check for the presence of this key.
540     * A <code>true</code> value indicates that restrictions may be applied in the near
541     * future but are not available yet. It is the responsibility of any
542     * management application that sets this flag to update it when the final
543     * restrictions are enforced.
544     *
545     * <p/>Key for application restrictions.
546     * <p/>Type: Boolean
547     * @see android.app.admin.DevicePolicyManager#setApplicationRestrictions(
548     *      android.content.ComponentName, String, Bundle)
549     * @see android.app.admin.DevicePolicyManager#getApplicationRestrictions(
550     *      android.content.ComponentName, String)
551     */
552    public static final String KEY_RESTRICTIONS_PENDING = "restrictions_pending";
553
554    /** @hide */
555    public static final int PIN_VERIFICATION_FAILED_INCORRECT = -3;
556    /** @hide */
557    public static final int PIN_VERIFICATION_FAILED_NOT_SET = -2;
558    /** @hide */
559    public static final int PIN_VERIFICATION_SUCCESS = -1;
560
561    /** @hide */
562    public static UserManager get(Context context) {
563        return (UserManager) context.getSystemService(Context.USER_SERVICE);
564    }
565
566    /** @hide */
567    public UserManager(Context context, IUserManager service) {
568        mService = service;
569        mContext = context;
570    }
571
572    /**
573     * Returns whether the system supports multiple users.
574     * @return true if multiple users can be created by user, false if it is a single user device.
575     * @hide
576     */
577    public static boolean supportsMultipleUsers() {
578        return getMaxSupportedUsers() > 1
579                && SystemProperties.getBoolean("fw.show_multiuserui",
580                Resources.getSystem().getBoolean(R.bool.config_enableMultiUserUI));
581    }
582
583    /**
584     * @hide
585     * @return Whether the device is running with split system user. It means the system user and
586     * primary user are two separate users. Previously system user and primary user are combined as
587     * a single owner user.  see @link {android.os.UserHandle#USER_OWNER}
588     */
589    public static boolean isSplitSystemUser() {
590        return SystemProperties.getBoolean("ro.fw.system_user_split", false);
591    }
592
593    /**
594     * Returns the user handle for the user that this process is running under.
595     *
596     * @return the user handle of this process.
597     * @hide
598     */
599    public int getUserHandle() {
600        return UserHandle.myUserId();
601    }
602
603    /**
604     * Returns the user name of the user making this call.  This call is only
605     * available to applications on the system image; it requires the
606     * MANAGE_USERS permission.
607     * @return the user name
608     */
609    public String getUserName() {
610        try {
611            return mService.getUserInfo(getUserHandle()).name;
612        } catch (RemoteException re) {
613            Log.w(TAG, "Could not get user name", re);
614            return "";
615        }
616    }
617
618    /**
619     * Used to determine whether the user making this call is subject to
620     * teleportations.
621     *
622     * <p>As of {@link android.os.Build.VERSION_CODES#LOLLIPOP}, this method can
623     * now automatically identify goats using advanced goat recognition technology.</p>
624     *
625     * @return Returns true if the user making this call is a goat.
626     */
627    public boolean isUserAGoat() {
628        return mContext.getPackageManager()
629                .isPackageAvailable("com.coffeestainstudios.goatsimulator");
630    }
631
632    /**
633     * Used to check if this process is running under the primary user. The primary user
634     * is the first human user on a device.
635     *
636     * @return whether this process is running under the primary user.
637     * @hide
638     */
639    public boolean isPrimaryUser() {
640        UserInfo user = getUserInfo(UserHandle.myUserId());
641        return user != null ? user.isPrimary() : false;
642    }
643
644    /**
645     * Used to check if this process is running under the system user. The system user
646     * is the initial user that is implicitly created on first boot and hosts most of the
647     * system services.
648     *
649     * @return whether this process is running under the system user.
650     */
651    public boolean isSystemUser() {
652        return UserHandle.myUserId() == UserHandle.USER_SYSTEM;
653    }
654
655    /**
656     * @hide
657     * Returns whether the caller is running as an admin user. There can be more than one admin
658     * user.
659     */
660    public boolean isAdminUser() {
661        return isUserAdmin(UserHandle.myUserId());
662    }
663
664    /**
665     * @hide
666     * Returns whether the provided user is an admin user. There can be more than one admin
667     * user.
668     */
669    public boolean isUserAdmin(int userId) {
670        UserInfo user = getUserInfo(userId);
671        return user != null && user.isAdmin();
672    }
673
674    /**
675     * Used to check if the user making this call is linked to another user. Linked users may have
676     * a reduced number of available apps, app restrictions and account restrictions.
677     * @return whether the user making this call is a linked user
678     * @hide
679     */
680    public boolean isLinkedUser() {
681        try {
682            return mService.isRestricted();
683        } catch (RemoteException re) {
684            Log.w(TAG, "Could not check if user is limited ", re);
685            return false;
686        }
687    }
688
689    /**
690     * Checks if specified user can have restricted profile.
691     * @hide
692     */
693    public boolean canHaveRestrictedProfile(int userId) {
694        try {
695            return mService.canHaveRestrictedProfile(userId);
696        } catch (RemoteException re) {
697            Log.w(TAG, "Could not check if user can have restricted profile", re);
698            return false;
699        }
700    }
701
702    /**
703     * Checks if the calling app is running as a guest user.
704     * @return whether the caller is a guest user.
705     * @hide
706     */
707    public boolean isGuestUser() {
708        UserInfo user = getUserInfo(UserHandle.myUserId());
709        return user != null ? user.isGuest() : false;
710    }
711
712    /**
713     * Checks if the calling app is running in a managed profile.
714     * Requires {@link android.Manifest.permission#MANAGE_USERS} permission.
715     *
716     * @return whether the caller is in a managed profile.
717     * @hide
718     */
719    @SystemApi
720    public boolean isManagedProfile() {
721        UserInfo user = getUserInfo(UserHandle.myUserId());
722        return user != null ? user.isManagedProfile() : false;
723    }
724
725    /**
726     * Return whether the given user is actively running.  This means that
727     * the user is in the "started" state, not "stopped" -- it is currently
728     * allowed to run code through scheduled alarms, receiving broadcasts,
729     * etc.  A started user may be either the current foreground user or a
730     * background user; the result here does not distinguish between the two.
731     * @param user The user to retrieve the running state for.
732     */
733    public boolean isUserRunning(UserHandle user) {
734        try {
735            return ActivityManagerNative.getDefault().isUserRunning(
736                    user.getIdentifier(), 0);
737        } catch (RemoteException e) {
738            return false;
739        }
740    }
741
742    /**
743     * Return whether the given user is actively running <em>or</em> stopping.
744     * This is like {@link #isUserRunning(UserHandle)}, but will also return
745     * true if the user had been running but is in the process of being stopped
746     * (but is not yet fully stopped, and still running some code).
747     * @param user The user to retrieve the running state for.
748     */
749    public boolean isUserRunningOrStopping(UserHandle user) {
750        try {
751            // TODO: reconcile stopped vs stopping?
752            return ActivityManagerNative.getDefault().isUserRunning(
753                    user.getIdentifier(), ActivityManager.FLAG_OR_STOPPED);
754        } catch (RemoteException e) {
755            return false;
756        }
757    }
758
759    /**
760     * Return whether the given user is running in a "locked" state. A user
761     * is unlocked only after they've entered their credentials (such as a lock
762     * pattern or PIN), and credential-encrypted private app data storage is
763     * available.
764     *
765     * @param user to retrieve the unlocked state for.
766     */
767    public boolean isUserRunningAndLocked(UserHandle user) {
768        try {
769            return ActivityManagerNative.getDefault().isUserRunning(
770                    user.getIdentifier(), ActivityManager.FLAG_AND_LOCKED);
771        } catch (RemoteException e) {
772            return false;
773        }
774    }
775
776    /**
777     * Return whether the given user is running in an "unlocked" state. A user
778     * is unlocked only after they've entered their credentials (such as a lock
779     * pattern or PIN), and credential-encrypted private app data storage is
780     * available.
781     *
782     * @param user to retrieve the unlocked state for.
783     */
784    public boolean isUserRunningAndUnlocked(UserHandle user) {
785        try {
786            return ActivityManagerNative.getDefault().isUserRunning(
787                    user.getIdentifier(), ActivityManager.FLAG_AND_UNLOCKED);
788        } catch (RemoteException e) {
789            return false;
790        }
791    }
792
793    /**
794     * Returns the UserInfo object describing a specific user.
795     * Requires {@link android.Manifest.permission#MANAGE_USERS} permission.
796     * @param userHandle the user handle of the user whose information is being requested.
797     * @return the UserInfo object for a specific user.
798     * @hide
799     */
800    public UserInfo getUserInfo(int userHandle) {
801        try {
802            return mService.getUserInfo(userHandle);
803        } catch (RemoteException re) {
804            Log.w(TAG, "Could not get user info", re);
805            return null;
806        }
807    }
808
809    /**
810     * Returns the user-wide restrictions imposed on this user.
811     * @return a Bundle containing all the restrictions.
812     */
813    public Bundle getUserRestrictions() {
814        return getUserRestrictions(Process.myUserHandle());
815    }
816
817    /**
818     * Returns the user-wide restrictions imposed on the user specified by <code>userHandle</code>.
819     * @param userHandle the UserHandle of the user for whom to retrieve the restrictions.
820     * @return a Bundle containing all the restrictions.
821     */
822    public Bundle getUserRestrictions(UserHandle userHandle) {
823        try {
824            return mService.getUserRestrictions(userHandle.getIdentifier());
825        } catch (RemoteException re) {
826            Log.w(TAG, "Could not get user restrictions", re);
827            return Bundle.EMPTY;
828        }
829    }
830
831    /**
832     * This will no longer work.  Device owners and profile owners should use
833     * {@link DevicePolicyManager#addUserRestriction(ComponentName, String)} instead.
834     */
835    // System apps should use UserManager.setUserRestriction() instead.
836    @Deprecated
837    public void setUserRestrictions(Bundle restrictions) {
838        throw new UnsupportedOperationException("This method is no longer supported");
839    }
840
841    /**
842     * This will no longer work.  Device owners and profile owners should use
843     * {@link DevicePolicyManager#addUserRestriction(ComponentName, String)} instead.
844     */
845    // System apps should use UserManager.setUserRestriction() instead.
846    @Deprecated
847    public void setUserRestrictions(Bundle restrictions, UserHandle userHandle) {
848        throw new UnsupportedOperationException("This method is no longer supported");
849    }
850
851    /**
852     * Sets the value of a specific restriction.
853     * Requires the MANAGE_USERS permission.
854     * @param key the key of the restriction
855     * @param value the value for the restriction
856     * @deprecated use {@link android.app.admin.DevicePolicyManager#addUserRestriction(
857     * android.content.ComponentName, String)} or
858     * {@link android.app.admin.DevicePolicyManager#clearUserRestriction(
859     * android.content.ComponentName, String)} instead.
860     */
861    @Deprecated
862    public void setUserRestriction(String key, boolean value) {
863        setUserRestriction(key, value, Process.myUserHandle());
864    }
865
866    /**
867     * @hide
868     * Sets the value of a specific restriction on a specific user.
869     * Requires the MANAGE_USERS permission.
870     * @param key the key of the restriction
871     * @param value the value for the restriction
872     * @param userHandle the user whose restriction is to be changed.
873     * @deprecated use {@link android.app.admin.DevicePolicyManager#addUserRestriction(
874     * android.content.ComponentName, String)} or
875     * {@link android.app.admin.DevicePolicyManager#clearUserRestriction(
876     * android.content.ComponentName, String)} instead.
877     */
878    @Deprecated
879    public void setUserRestriction(String key, boolean value, UserHandle userHandle) {
880        try {
881            mService.setUserRestriction(key, value, userHandle.getIdentifier());
882        } catch (RemoteException re) {
883            Log.w(TAG, "Could not set user restriction", re);
884        }
885    }
886
887    /**
888     * Returns whether the current user has been disallowed from performing certain actions
889     * or setting certain settings.
890     *
891     * @param restrictionKey The string key representing the restriction.
892     * @return {@code true} if the current user has the given restriction, {@code false} otherwise.
893     */
894    public boolean hasUserRestriction(String restrictionKey) {
895        return hasUserRestriction(restrictionKey, Process.myUserHandle());
896    }
897
898    /**
899     * @hide
900     * Returns whether the given user has been disallowed from performing certain actions
901     * or setting certain settings.
902     * @param restrictionKey the string key representing the restriction
903     * @param userHandle the UserHandle of the user for whom to retrieve the restrictions.
904     */
905    public boolean hasUserRestriction(String restrictionKey, UserHandle userHandle) {
906        try {
907            return mService.hasUserRestriction(restrictionKey,
908                    userHandle.getIdentifier());
909        } catch (RemoteException re) {
910            Log.w(TAG, "Could not check user restrictions", re);
911            return false;
912        }
913    }
914
915    /**
916     * Return the serial number for a user.  This is a device-unique
917     * number assigned to that user; if the user is deleted and then a new
918     * user created, the new users will not be given the same serial number.
919     * @param user The user whose serial number is to be retrieved.
920     * @return The serial number of the given user; returns -1 if the
921     * given UserHandle does not exist.
922     * @see #getUserForSerialNumber(long)
923     */
924    public long getSerialNumberForUser(UserHandle user) {
925        return getUserSerialNumber(user.getIdentifier());
926    }
927
928    /**
929     * Return the user associated with a serial number previously
930     * returned by {@link #getSerialNumberForUser(UserHandle)}.
931     * @param serialNumber The serial number of the user that is being
932     * retrieved.
933     * @return Return the user associated with the serial number, or null
934     * if there is not one.
935     * @see #getSerialNumberForUser(UserHandle)
936     */
937    public UserHandle getUserForSerialNumber(long serialNumber) {
938        int ident = getUserHandle((int) serialNumber);
939        return ident >= 0 ? new UserHandle(ident) : null;
940    }
941
942    /**
943     * Creates a user with the specified name and options. For non-admin users, default user
944     * restrictions are going to be applied.
945     * Requires {@link android.Manifest.permission#MANAGE_USERS} permission.
946     *
947     * @param name the user's name
948     * @param flags flags that identify the type of user and other properties.
949     * @see UserInfo
950     *
951     * @return the UserInfo object for the created user, or null if the user could not be created.
952     * @hide
953     */
954    public UserInfo createUser(String name, int flags) {
955        UserInfo user = null;
956        try {
957            user = mService.createUser(name, flags);
958            if (user != null && !user.isAdmin()) {
959                mService.setUserRestriction(DISALLOW_SMS, true, user.id);
960                mService.setUserRestriction(DISALLOW_OUTGOING_CALLS, true, user.id);
961            }
962        } catch (RemoteException re) {
963            Log.w(TAG, "Could not create a user", re);
964        }
965        return user;
966    }
967
968    /**
969     * Creates a guest user and configures it.
970     * @param context an application context
971     * @param name the name to set for the user
972     * @hide
973     */
974    public UserInfo createGuest(Context context, String name) {
975        UserInfo guest = null;
976        try {
977            guest = mService.createUser(name, UserInfo.FLAG_GUEST);
978            if (guest != null) {
979                Settings.Secure.putStringForUser(context.getContentResolver(),
980                        Settings.Secure.SKIP_FIRST_USE_HINTS, "1", guest.id);
981            }
982        } catch (RemoteException re) {
983            Log.w(TAG, "Could not create a user", re);
984        }
985        return guest;
986    }
987
988    /**
989     * Creates a user with the specified name and options as a profile of another user.
990     * Requires {@link android.Manifest.permission#MANAGE_USERS} permission.
991     *
992     * @param name the user's name
993     * @param flags flags that identify the type of user and other properties.
994     * @see UserInfo
995     * @param userHandle new user will be a profile of this use.
996     *
997     * @return the UserInfo object for the created user, or null if the user could not be created.
998     * @hide
999     */
1000    public UserInfo createProfileForUser(String name, int flags, int userHandle) {
1001        try {
1002            return mService.createProfileForUser(name, flags, userHandle);
1003        } catch (RemoteException re) {
1004            Log.w(TAG, "Could not create a user", re);
1005            return null;
1006        }
1007    }
1008
1009    /**
1010     * Creates a restricted profile with the specified name. This method also sets necessary
1011     * restrictions and adds shared accounts.
1012     *
1013     * @param name profile's name
1014     * @return UserInfo object for the created user, or null if the user could not be created.
1015     * @hide
1016     */
1017    public UserInfo createRestrictedProfile(String name) {
1018        try {
1019            UserHandle parentUserHandle = Process.myUserHandle();
1020            UserInfo user = mService.createRestrictedProfile(name,
1021                    parentUserHandle.getIdentifier());
1022            if (user != null) {
1023                AccountManager.get(mContext).addSharedAccountsFromParentUser(parentUserHandle,
1024                        UserHandle.of(user.id));
1025            }
1026            return user;
1027        } catch (RemoteException e) {
1028            Log.w(TAG, "Could not create a restricted profile", e);
1029        }
1030        return null;
1031    }
1032
1033    /**
1034     * @hide
1035     * Marks the guest user for deletion to allow a new guest to be created before deleting
1036     * the current user who is a guest.
1037     * @param userHandle
1038     * @return
1039     */
1040    public boolean markGuestForDeletion(int userHandle) {
1041        try {
1042            return mService.markGuestForDeletion(userHandle);
1043        } catch (RemoteException re) {
1044            Log.w(TAG, "Could not mark guest for deletion", re);
1045            return false;
1046        }
1047    }
1048
1049    /**
1050     * Sets the user as enabled, if such an user exists.
1051     * Requires {@link android.Manifest.permission#MANAGE_USERS} permission.
1052     * Note that the default is true, it's only that managed profiles might not be enabled.
1053     *
1054     * @param userHandle the id of the profile to enable
1055     * @hide
1056     */
1057    public void setUserEnabled(int userHandle) {
1058        try {
1059            mService.setUserEnabled(userHandle);
1060        } catch (RemoteException e) {
1061            Log.w(TAG, "Could not enable the profile", e);
1062        }
1063    }
1064
1065    /**
1066     * Return the number of users currently created on the device.
1067     */
1068    public int getUserCount() {
1069        List<UserInfo> users = getUsers();
1070        return users != null ? users.size() : 1;
1071    }
1072
1073    /**
1074     * Returns information for all users on this device.
1075     * Requires {@link android.Manifest.permission#MANAGE_USERS} permission.
1076     * @return the list of users that were created.
1077     * @hide
1078     */
1079    public List<UserInfo> getUsers() {
1080        try {
1081            return mService.getUsers(false);
1082        } catch (RemoteException re) {
1083            Log.w(TAG, "Could not get user list", re);
1084            return null;
1085        }
1086    }
1087
1088    /**
1089     * @return the user's account name, null if not found.
1090     * @hide
1091     */
1092    @RequiresPermission( allOf = {
1093            Manifest.permission.INTERACT_ACROSS_USERS_FULL,
1094            Manifest.permission.MANAGE_USERS
1095    })
1096    public @Nullable String getUserAccount(int userHandle) {
1097        try {
1098            return mService.getUserAccount(userHandle);
1099        } catch (RemoteException re) {
1100            Log.w(TAG, "Could not get user account", re);
1101            return null;
1102        }
1103    }
1104
1105    /**
1106     * Set account name for the given user.
1107     * @hide
1108     */
1109    @RequiresPermission( allOf = {
1110            Manifest.permission.INTERACT_ACROSS_USERS_FULL,
1111            Manifest.permission.MANAGE_USERS
1112    })
1113    public void setUserAccount(int userHandle, @Nullable String accountName) {
1114        try {
1115            mService.setUserAccount(userHandle, accountName);
1116        } catch (RemoteException re) {
1117            Log.w(TAG, "Could not set user account", re);
1118        }
1119    }
1120
1121    /**
1122     * Returns information for Primary user.
1123     * Requires {@link android.Manifest.permission#MANAGE_USERS} permission.
1124     *
1125     * @return the Primary user, null if not found.
1126     * @hide
1127     */
1128    public @Nullable UserInfo getPrimaryUser() {
1129        try {
1130            return mService.getPrimaryUser();
1131        } catch (RemoteException re) {
1132            Log.w(TAG, "Could not get Primary user", re);
1133            return null;
1134        }
1135    }
1136
1137    /**
1138     * Checks whether it's possible to add more users. Caller must hold the MANAGE_USERS
1139     * permission.
1140     *
1141     * @return true if more users can be added, false if limit has been reached.
1142     * @hide
1143     */
1144    public boolean canAddMoreUsers() {
1145        final List<UserInfo> users = getUsers(true);
1146        final int totalUserCount = users.size();
1147        int aliveUserCount = 0;
1148        for (int i = 0; i < totalUserCount; i++) {
1149            UserInfo user = users.get(i);
1150            if (!user.isGuest()) {
1151                aliveUserCount++;
1152            }
1153        }
1154        return aliveUserCount < getMaxSupportedUsers();
1155    }
1156
1157    /**
1158     * Checks whether it's possible to add more managed profiles. Caller must hold the MANAGE_USERS
1159     * permission.
1160     * if allowedToRemoveOne is true and if the user already has a managed profile, then return if
1161     * we could add a new managed profile to this user after removing the existing one.
1162     *
1163     * @return true if more managed profiles can be added, false if limit has been reached.
1164     * @hide
1165     */
1166    public boolean canAddMoreManagedProfiles(int userId, boolean allowedToRemoveOne) {
1167        try {
1168            return mService.canAddMoreManagedProfiles(userId, allowedToRemoveOne);
1169        } catch (RemoteException re) {
1170            Log.w(TAG, "Could not check if we can add more managed profiles", re);
1171            return false;
1172        }
1173    }
1174
1175    /**
1176     * Returns list of the profiles of userHandle including
1177     * userHandle itself.
1178     * Note that this returns both enabled and not enabled profiles. See
1179     * {@link #getEnabledProfiles(int)} if you need only the enabled ones.
1180     *
1181     * Requires {@link android.Manifest.permission#MANAGE_USERS} permission.
1182     * @param userHandle profiles of this user will be returned.
1183     * @return the list of profiles.
1184     * @hide
1185     */
1186    public List<UserInfo> getProfiles(int userHandle) {
1187        try {
1188            return mService.getProfiles(userHandle, false /* enabledOnly */);
1189        } catch (RemoteException re) {
1190            Log.w(TAG, "Could not get user list", re);
1191            return null;
1192        }
1193    }
1194
1195    /**
1196     * Requires {@link android.Manifest.permission#MANAGE_USERS} permission.
1197     * @param userId one of the two user ids to check.
1198     * @param otherUserId one of the two user ids to check.
1199     * @return true if the two user ids are in the same profile group.
1200     * @hide
1201     */
1202    public boolean isSameProfileGroup(int userId, int otherUserId) {
1203        try {
1204            return mService.isSameProfileGroup(userId, otherUserId);
1205        } catch (RemoteException re) {
1206            Log.w(TAG, "Could not get user list", re);
1207            return false;
1208        }
1209    }
1210
1211    /**
1212     * Returns list of the profiles of userHandle including
1213     * userHandle itself.
1214     * Note that this returns only enabled.
1215     *
1216     * Requires {@link android.Manifest.permission#MANAGE_USERS} permission.
1217     * @param userHandle profiles of this user will be returned.
1218     * @return the list of profiles.
1219     * @hide
1220     */
1221    public List<UserInfo> getEnabledProfiles(int userHandle) {
1222        try {
1223            return mService.getProfiles(userHandle, true /* enabledOnly */);
1224        } catch (RemoteException re) {
1225            Log.w(TAG, "Could not get user list", re);
1226            return null;
1227        }
1228    }
1229
1230    /**
1231     * Returns a list of UserHandles for profiles associated with the user that the calling process
1232     * is running on, including the user itself.
1233     *
1234     * @return A non-empty list of UserHandles associated with the calling user.
1235     */
1236    public List<UserHandle> getUserProfiles() {
1237        ArrayList<UserHandle> profiles = new ArrayList<UserHandle>();
1238        List<UserInfo> users;
1239        try {
1240            users = mService.getProfiles(UserHandle.myUserId(), true /* enabledOnly */);
1241        } catch (RemoteException re) {
1242            Log.w(TAG, "Could not get user list", re);
1243            return null;
1244        }
1245        for (UserInfo info : users) {
1246            UserHandle userHandle = new UserHandle(info.id);
1247            profiles.add(userHandle);
1248        }
1249        return profiles;
1250    }
1251
1252    /**
1253     * Returns the device credential owner id of the profile from
1254     * which this method is called, or userHandle if called from a user that
1255     * is not a profile.
1256     *
1257     * @hide
1258     */
1259    public int getCredentialOwnerProfile(int userHandle) {
1260        try {
1261            return mService.getCredentialOwnerProfile(userHandle);
1262        } catch (RemoteException re) {
1263            Log.w(TAG, "Could not get credential owner", re);
1264            return -1;
1265        }
1266    }
1267
1268    /**
1269     * Returns the parent of the profile which this method is called from
1270     * or null if called from a user that is not a profile.
1271     *
1272     * @hide
1273     */
1274    public UserInfo getProfileParent(int userHandle) {
1275        try {
1276            return mService.getProfileParent(userHandle);
1277        } catch (RemoteException re) {
1278            Log.w(TAG, "Could not get profile parent", re);
1279            return null;
1280        }
1281    }
1282
1283    /**
1284     * If the target user is a managed profile of the calling user or the caller
1285     * is itself a managed profile, then this returns a badged copy of the given
1286     * icon to be able to distinguish it from the original icon. For badging an
1287     * arbitrary drawable use {@link #getBadgedDrawableForUser(
1288     * android.graphics.drawable.Drawable, UserHandle, android.graphics.Rect, int)}.
1289     * <p>
1290     * If the original drawable is a BitmapDrawable and the backing bitmap is
1291     * mutable as per {@link android.graphics.Bitmap#isMutable()}, the bading
1292     * is performed in place and the original drawable is returned.
1293     * </p>
1294     *
1295     * @param icon The icon to badge.
1296     * @param user The target user.
1297     * @return A drawable that combines the original icon and a badge as
1298     *         determined by the system.
1299     * @removed
1300     */
1301    public Drawable getBadgedIconForUser(Drawable icon, UserHandle user) {
1302        return mContext.getPackageManager().getUserBadgedIcon(icon, user);
1303    }
1304
1305    /**
1306     * If the target user is a managed profile of the calling user or the caller
1307     * is itself a managed profile, then this returns a badged copy of the given
1308     * drawable allowing the user to distinguish it from the original drawable.
1309     * The caller can specify the location in the bounds of the drawable to be
1310     * badged where the badge should be applied as well as the density of the
1311     * badge to be used.
1312     * <p>
1313     * If the original drawable is a BitmapDrawable and the backing bitmap is
1314     * mutable as per {@link android.graphics.Bitmap#isMutable()}, the bading
1315     * is performed in place and the original drawable is returned.
1316     * </p>
1317     *
1318     * @param badgedDrawable The drawable to badge.
1319     * @param user The target user.
1320     * @param badgeLocation Where in the bounds of the badged drawable to place
1321     *         the badge. If not provided, the badge is applied on top of the entire
1322     *         drawable being badged.
1323     * @param badgeDensity The optional desired density for the badge as per
1324     *         {@link android.util.DisplayMetrics#densityDpi}. If not provided,
1325     *         the density of the display is used.
1326     * @return A drawable that combines the original drawable and a badge as
1327     *         determined by the system.
1328     * @removed
1329     */
1330    public Drawable getBadgedDrawableForUser(Drawable badgedDrawable, UserHandle user,
1331            Rect badgeLocation, int badgeDensity) {
1332        return mContext.getPackageManager().getUserBadgedDrawableForDensity(badgedDrawable, user,
1333                badgeLocation, badgeDensity);
1334    }
1335
1336    /**
1337     * If the target user is a managed profile of the calling user or the caller
1338     * is itself a managed profile, then this returns a copy of the label with
1339     * badging for accessibility services like talkback. E.g. passing in "Email"
1340     * and it might return "Work Email" for Email in the work profile.
1341     *
1342     * @param label The label to change.
1343     * @param user The target user.
1344     * @return A label that combines the original label and a badge as
1345     *         determined by the system.
1346     * @removed
1347     */
1348    public CharSequence getBadgedLabelForUser(CharSequence label, UserHandle user) {
1349        return mContext.getPackageManager().getUserBadgedLabel(label, user);
1350    }
1351
1352    /**
1353     * Returns information for all users on this device. Requires
1354     * {@link android.Manifest.permission#MANAGE_USERS} permission.
1355     *
1356     * @param excludeDying specify if the list should exclude users being
1357     *            removed.
1358     * @return the list of users that were created.
1359     * @hide
1360     */
1361    public List<UserInfo> getUsers(boolean excludeDying) {
1362        try {
1363            return mService.getUsers(excludeDying);
1364        } catch (RemoteException re) {
1365            Log.w(TAG, "Could not get user list", re);
1366            return null;
1367        }
1368    }
1369
1370    /**
1371     * Removes a user and all associated data.
1372     * Requires {@link android.Manifest.permission#MANAGE_USERS} permission.
1373     * @param userHandle the integer handle of the user, where 0 is the primary user.
1374     * @hide
1375     */
1376    public boolean removeUser(int userHandle) {
1377        try {
1378            return mService.removeUser(userHandle);
1379        } catch (RemoteException re) {
1380            Log.w(TAG, "Could not remove user ", re);
1381            return false;
1382        }
1383    }
1384
1385    /**
1386     * Updates the user's name.
1387     * Requires {@link android.Manifest.permission#MANAGE_USERS} permission.
1388     *
1389     * @param userHandle the user's integer handle
1390     * @param name the new name for the user
1391     * @hide
1392     */
1393    public void setUserName(int userHandle, String name) {
1394        try {
1395            mService.setUserName(userHandle, name);
1396        } catch (RemoteException re) {
1397            Log.w(TAG, "Could not set the user name ", re);
1398        }
1399    }
1400
1401    /**
1402     * Sets the user's photo.
1403     * @param userHandle the user for whom to change the photo.
1404     * @param icon the bitmap to set as the photo.
1405     * @hide
1406     */
1407    public void setUserIcon(int userHandle, Bitmap icon) {
1408        try {
1409            mService.setUserIcon(userHandle, icon);
1410        } catch (RemoteException re) {
1411            Log.w(TAG, "Could not set the user icon ", re);
1412        }
1413    }
1414
1415    /**
1416     * Returns a file descriptor for the user's photo. PNG data can be read from this file.
1417     * @param userHandle the user whose photo we want to read.
1418     * @return a {@link Bitmap} of the user's photo, or null if there's no photo.
1419     * @see com.android.internal.util.UserIcons#getDefaultUserIcon for a default.
1420     * @hide
1421     */
1422    public Bitmap getUserIcon(int userHandle) {
1423        try {
1424            ParcelFileDescriptor fd = mService.getUserIcon(userHandle);
1425            if (fd != null) {
1426                try {
1427                    return BitmapFactory.decodeFileDescriptor(fd.getFileDescriptor());
1428                } finally {
1429                    try {
1430                        fd.close();
1431                    } catch (IOException e) {
1432                    }
1433                }
1434            }
1435        } catch (RemoteException re) {
1436            Log.w(TAG, "Could not get the user icon ", re);
1437        }
1438        return null;
1439    }
1440
1441    /**
1442     * Returns the maximum number of users that can be created on this device. A return value
1443     * of 1 means that it is a single user device.
1444     * @hide
1445     * @return a value greater than or equal to 1
1446     */
1447    public static int getMaxSupportedUsers() {
1448        // Don't allow multiple users on certain builds
1449        if (android.os.Build.ID.startsWith("JVP")) return 1;
1450        // Svelte devices don't get multi-user.
1451        if (ActivityManager.isLowRamDeviceStatic()) return 1;
1452        return SystemProperties.getInt("fw.max_users",
1453                Resources.getSystem().getInteger(R.integer.config_multiuserMaximumUsers));
1454    }
1455
1456    /**
1457     * Returns true if the user switcher should be shown, this will be if device supports multi-user
1458     * and there are at least 2 users available that are not managed profiles.
1459     * @hide
1460     * @return true if user switcher should be shown.
1461     */
1462    public boolean isUserSwitcherEnabled() {
1463        if (!supportsMultipleUsers()) {
1464            return false;
1465        }
1466        List<UserInfo> users = getUsers(true);
1467        if (users == null) {
1468           return false;
1469        }
1470        int switchableUserCount = 0;
1471        for (UserInfo user : users) {
1472            if (user.supportsSwitchToByUser()) {
1473                ++switchableUserCount;
1474            }
1475        }
1476        final boolean guestEnabled = !mContext.getSystemService(DevicePolicyManager.class)
1477                .getGuestUserDisabled(null);
1478        return switchableUserCount > 1 || guestEnabled;
1479    }
1480
1481    /**
1482     * Returns a serial number on this device for a given userHandle. User handles can be recycled
1483     * when deleting and creating users, but serial numbers are not reused until the device is wiped.
1484     * @param userHandle
1485     * @return a serial number associated with that user, or -1 if the userHandle is not valid.
1486     * @hide
1487     */
1488    public int getUserSerialNumber(int userHandle) {
1489        try {
1490            return mService.getUserSerialNumber(userHandle);
1491        } catch (RemoteException re) {
1492            Log.w(TAG, "Could not get serial number for user " + userHandle);
1493        }
1494        return -1;
1495    }
1496
1497    /**
1498     * Returns a userHandle on this device for a given user serial number. User handles can be
1499     * recycled when deleting and creating users, but serial numbers are not reused until the device
1500     * is wiped.
1501     * @param userSerialNumber
1502     * @return the userHandle associated with that user serial number, or -1 if the serial number
1503     * is not valid.
1504     * @hide
1505     */
1506    public int getUserHandle(int userSerialNumber) {
1507        try {
1508            return mService.getUserHandle(userSerialNumber);
1509        } catch (RemoteException re) {
1510            Log.w(TAG, "Could not get userHandle for user " + userSerialNumber);
1511        }
1512        return -1;
1513    }
1514
1515    /**
1516     * Returns a Bundle containing any saved application restrictions for this user, for the
1517     * given package name. Only an application with this package name can call this method.
1518     * @param packageName the package name of the calling application
1519     * @return a Bundle with the restrictions as key/value pairs, or null if there are no
1520     * saved restrictions. The values can be of type Boolean, String or String[], depending
1521     * on the restriction type, as defined by the application.
1522     */
1523    public Bundle getApplicationRestrictions(String packageName) {
1524        try {
1525            return mService.getApplicationRestrictions(packageName);
1526        } catch (RemoteException re) {
1527            Log.w(TAG, "Could not get application restrictions for package " + packageName);
1528        }
1529        return null;
1530    }
1531
1532    /**
1533     * @hide
1534     */
1535    public Bundle getApplicationRestrictions(String packageName, UserHandle user) {
1536        try {
1537            return mService.getApplicationRestrictionsForUser(packageName, user.getIdentifier());
1538        } catch (RemoteException re) {
1539            Log.w(TAG, "Could not get application restrictions for user " + user.getIdentifier());
1540        }
1541        return null;
1542    }
1543
1544    /**
1545     * @hide
1546     */
1547    public void setApplicationRestrictions(String packageName, Bundle restrictions,
1548            UserHandle user) {
1549        try {
1550            mService.setApplicationRestrictions(packageName, restrictions, user.getIdentifier());
1551        } catch (RemoteException re) {
1552            Log.w(TAG, "Could not set application restrictions for user " + user.getIdentifier());
1553        }
1554    }
1555
1556    /**
1557     * Sets a new challenge PIN for restrictions. This is only for use by pre-installed
1558     * apps and requires the MANAGE_USERS permission.
1559     * @param newPin the PIN to use for challenge dialogs.
1560     * @return Returns true if the challenge PIN was set successfully.
1561     * @deprecated The restrictions PIN functionality is no longer provided by the system.
1562     * This method is preserved for backwards compatibility reasons and always returns false.
1563     */
1564    public boolean setRestrictionsChallenge(String newPin) {
1565        return false;
1566    }
1567
1568    /**
1569     * @hide
1570     * Set restrictions that should apply to any future guest user that's created.
1571     */
1572    public void setDefaultGuestRestrictions(Bundle restrictions) {
1573        try {
1574            mService.setDefaultGuestRestrictions(restrictions);
1575        } catch (RemoteException re) {
1576            Log.w(TAG, "Could not set guest restrictions");
1577        }
1578    }
1579
1580    /**
1581     * @hide
1582     * Gets the default guest restrictions.
1583     */
1584    public Bundle getDefaultGuestRestrictions() {
1585        try {
1586            return mService.getDefaultGuestRestrictions();
1587        } catch (RemoteException re) {
1588            Log.w(TAG, "Could not set guest restrictions");
1589        }
1590        return new Bundle();
1591    }
1592
1593    /**
1594     * Returns creation time of the user or of a managed profile associated with the calling user.
1595     * @param userHandle user handle of the user or a managed profile associated with the
1596     *                   calling user.
1597     * @return creation time in milliseconds since Epoch time.
1598     */
1599    public long getUserCreationTime(UserHandle userHandle) {
1600        try {
1601            return mService.getUserCreationTime(userHandle.getIdentifier());
1602        } catch (RemoteException re) {
1603            Log.w(TAG, "Could not get user creation time", re);
1604            return 0;
1605        }
1606    }
1607}
1608