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