UserManager.java revision 71e6c697e54a43d357cc25d87a446d140f17396a
1/*
2 * Copyright (C) 2012 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16package android.os;
17
18import android.app.ActivityManagerNative;
19import android.content.Context;
20import android.content.RestrictionEntry;
21import android.content.pm.UserInfo;
22import android.content.res.Resources;
23import android.graphics.Bitmap;
24import android.util.Log;
25
26import com.android.internal.R;
27
28import java.util.List;
29
30/**
31 * Manages users and user details on a multi-user system.
32 */
33public class UserManager {
34
35    private static String TAG = "UserManager";
36    private final IUserManager mService;
37    private final Context mContext;
38
39    /**
40     * Key for user restrictions. Specifies if a user is disallowed from adding and removing
41     * accounts.
42     * The default value is <code>false</code>.
43     * <p/>
44     * Type: Boolean
45     * @see #setUserRestrictions(Bundle)
46     * @see #getUserRestrictions()
47     */
48    public static final String DISALLOW_MODIFY_ACCOUNTS = "no_modify_accounts";
49
50    /**
51     * Key for user restrictions. Specifies if a user is disallowed from changing Wi-Fi
52     * access points.
53     * The default value is <code>false</code>.
54     * <p/>
55     * Type: Boolean
56     * @see #setUserRestrictions(Bundle)
57     * @see #getUserRestrictions()
58     */
59    public static final String DISALLOW_CONFIG_WIFI = "no_config_wifi";
60
61    /**
62     * Key for user restrictions. Specifies if a user is disallowed from installing applications.
63     * The default value is <code>false</code>.
64     * <p/>
65     * Type: Boolean
66     * @see #setUserRestrictions(Bundle)
67     * @see #getUserRestrictions()
68     */
69    public static final String DISALLOW_INSTALL_APPS = "no_install_apps";
70
71    /**
72     * Key for user restrictions. Specifies if a user is disallowed from uninstalling applications.
73     * The default value is <code>false</code>.
74     * <p/>
75     * Type: Boolean
76     * @see #setUserRestrictions(Bundle)
77     * @see #getUserRestrictions()
78     */
79    public static final String DISALLOW_UNINSTALL_APPS = "no_uninstall_apps";
80
81    /**
82     * Key for user restrictions. Specifies if a user is disallowed from toggling location sharing.
83     * The default value is <code>false</code>.
84     * <p/>
85     * Type: Boolean
86     * @see #setUserRestrictions(Bundle)
87     * @see #getUserRestrictions()
88     */
89    public static final String DISALLOW_SHARE_LOCATION = "no_share_location";
90
91    /** @hide */
92    public UserManager(Context context, IUserManager service) {
93        mService = service;
94        mContext = context;
95    }
96
97    /**
98     * Returns whether the system supports multiple users.
99     * @return true if multiple users can be created, false if it is a single user device.
100     * @hide
101     */
102    public static boolean supportsMultipleUsers() {
103        return getMaxSupportedUsers() > 1;
104    }
105
106    /**
107     * Returns the user handle for the user that this application is running for.
108     * @return the user handle of the user making this call.
109     * @hide
110     */
111    public int getUserHandle() {
112        return UserHandle.myUserId();
113    }
114
115    /**
116     * Returns the user name of the user making this call.  This call is only
117     * available to applications on the system image; it requires the
118     * MANAGE_USERS permission.
119     * @return the user name
120     */
121    public String getUserName() {
122        try {
123            return mService.getUserInfo(getUserHandle()).name;
124        } catch (RemoteException re) {
125            Log.w(TAG, "Could not get user name", re);
126            return "";
127        }
128    }
129
130   /**
131     * Used to determine whether the user making this call is subject to
132     * teleportations.
133     * @return whether the user making this call is a goat
134     */
135    public boolean isUserAGoat() {
136        return false;
137    }
138
139    /**
140     * Used to check if the user making this call is a restricted user. Restricted users may have
141     * application restrictions imposed on them. All apps should default to the most restrictive
142     * version, unless they have specific restrictions available through a call to
143     * {@link Context#getApplicationRestrictions()}.
144     */
145    public boolean isUserRestricted() {
146        try {
147            return mService.isRestricted();
148        } catch (RemoteException re) {
149            Log.w(TAG, "Could not check if user restricted ", re);
150            return false;
151        }
152    }
153
154    /**
155     * Return whether the given user is actively running.  This means that
156     * the user is in the "started" state, not "stopped" -- it is currently
157     * allowed to run code through scheduled alarms, receiving broadcasts,
158     * etc.  A started user may be either the current foreground user or a
159     * background user; the result here does not distinguish between the two.
160     * @param user The user to retrieve the running state for.
161     */
162    public boolean isUserRunning(UserHandle user) {
163        try {
164            return ActivityManagerNative.getDefault().isUserRunning(
165                    user.getIdentifier(), false);
166        } catch (RemoteException e) {
167            return false;
168        }
169    }
170
171    /**
172     * Return whether the given user is actively running <em>or</em> stopping.
173     * This is like {@link #isUserRunning(UserHandle)}, but will also return
174     * true if the user had been running but is in the process of being stopped
175     * (but is not yet fully stopped, and still running some code).
176     * @param user The user to retrieve the running state for.
177     */
178    public boolean isUserRunningOrStopping(UserHandle user) {
179        try {
180            return ActivityManagerNative.getDefault().isUserRunning(
181                    user.getIdentifier(), true);
182        } catch (RemoteException e) {
183            return false;
184        }
185    }
186
187    /**
188     * Returns the UserInfo object describing a specific user.
189     * Requires {@link android.Manifest.permission#MANAGE_USERS} permission.
190     * @param userHandle the user handle of the user whose information is being requested.
191     * @return the UserInfo object for a specific user.
192     * @hide
193     */
194    public UserInfo getUserInfo(int userHandle) {
195        try {
196            return mService.getUserInfo(userHandle);
197        } catch (RemoteException re) {
198            Log.w(TAG, "Could not get user info", re);
199            return null;
200        }
201    }
202
203    /**
204     * Returns the user-wide restrictions imposed on this user.
205     * @return a Bundle containing all the restrictions.
206     */
207    public Bundle getUserRestrictions() {
208        return getUserRestrictions(Process.myUserHandle());
209    }
210
211    /**
212     * Returns the user-wide restrictions imposed on the user specified by <code>userHandle</code>.
213     * @param userHandle the UserHandle of the user for whom to retrieve the restrictions.
214     * @return a Bundle containing all the restrictions.
215     */
216    public Bundle getUserRestrictions(UserHandle userHandle) {
217        try {
218            return mService.getUserRestrictions(userHandle.getIdentifier());
219        } catch (RemoteException re) {
220            Log.w(TAG, "Could not get user restrictions", re);
221            return Bundle.EMPTY;
222        }
223    }
224
225    /**
226     * Sets all the user-wide restrictions for this user.
227     * Requires the {@link android.Manifest.permission#MANAGE_USERS} permission.
228     * @param restrictions the Bundle containing all the restrictions.
229     */
230    public void setUserRestrictions(Bundle restrictions) {
231        setUserRestrictions(restrictions, Process.myUserHandle());
232    }
233
234    /**
235     * Sets all the user-wide restrictions for the specified user.
236     * Requires the {@link android.Manifest.permission#MANAGE_USERS} permission.
237     * @param restrictions the Bundle containing all the restrictions.
238     * @param userHandle the UserHandle of the user for whom to set the restrictions.
239     */
240    public void setUserRestrictions(Bundle restrictions, UserHandle userHandle) {
241        try {
242            mService.setUserRestrictions(restrictions, userHandle.getIdentifier());
243        } catch (RemoteException re) {
244            Log.w(TAG, "Could not set user restrictions", re);
245        }
246    }
247
248    /**
249     * Sets the value of a specific restriction.
250     * Requires the {@link android.Manifest.permission#MANAGE_USERS} permission.
251     * @param key the key of the restriction
252     * @param value the value for the restriction
253     * @param userHandle the user whose restriction is to be changed.
254     */
255    public void setUserRestriction(String key, boolean value) {
256        Bundle bundle = getUserRestrictions();
257        bundle.putBoolean(key, value);
258        setUserRestrictions(bundle);
259    }
260
261    /**
262     * @hide
263     * Sets the value of a specific restriction on a specific user.
264     * Requires the {@link android.Manifest.permission#MANAGE_USERS} permission.
265     * @param key the key of the restriction
266     * @param value the value for the restriction
267     * @param userHandle the user whose restriction is to be changed.
268     */
269    public void setUserRestriction(String key, boolean value, UserHandle userHandle) {
270        Bundle bundle = getUserRestrictions(userHandle);
271        bundle.putBoolean(key, value);
272        setUserRestrictions(bundle, userHandle);
273    }
274
275    /**
276     * Return the serial number for a user.  This is a device-unique
277     * number assigned to that user; if the user is deleted and then a new
278     * user created, the new users will not be given the same serial number.
279     * @param user The user whose serial number is to be retrieved.
280     * @return The serial number of the given user; returns -1 if the
281     * given UserHandle does not exist.
282     * @see #getUserForSerialNumber(long)
283     */
284    public long getSerialNumberForUser(UserHandle user) {
285        return getUserSerialNumber(user.getIdentifier());
286    }
287
288    /**
289     * Return the user associated with a serial number previously
290     * returned by {@link #getSerialNumberForUser(UserHandle)}.
291     * @param serialNumber The serial number of the user that is being
292     * retrieved.
293     * @return Return the user associated with the serial number, or null
294     * if there is not one.
295     * @see #getSerialNumberForUser(UserHandle)
296     */
297    public UserHandle getUserForSerialNumber(long serialNumber) {
298        int ident = getUserHandle((int)serialNumber);
299        return ident >= 0 ? new UserHandle(ident) : null;
300    }
301
302    /**
303     * Creates a user with the specified name and options.
304     * Requires {@link android.Manifest.permission#MANAGE_USERS} permission.
305     *
306     * @param name the user's name
307     * @param flags flags that identify the type of user and other properties.
308     * @see UserInfo
309     *
310     * @return the UserInfo object for the created user, or null if the user could not be created.
311     * @hide
312     */
313    public UserInfo createUser(String name, int flags) {
314        try {
315            return mService.createUser(name, flags);
316        } catch (RemoteException re) {
317            Log.w(TAG, "Could not create a user", re);
318            return null;
319        }
320    }
321
322    /**
323     * Return the number of users currently created on the device.
324     */
325    public int getUserCount() {
326        List<UserInfo> users = getUsers();
327        return users != null ? users.size() : 1;
328    }
329
330    /**
331     * Returns information for all users on this device.
332     * Requires {@link android.Manifest.permission#MANAGE_USERS} permission.
333     * @return the list of users that were created.
334     * @hide
335     */
336    public List<UserInfo> getUsers() {
337        try {
338            return mService.getUsers(false);
339        } catch (RemoteException re) {
340            Log.w(TAG, "Could not get user list", re);
341            return null;
342        }
343    }
344
345    /**
346     * Returns information for all users on this device.
347     * Requires {@link android.Manifest.permission#MANAGE_USERS} permission.
348     * @param excludeDying specify if the list should exclude users being removed.
349     * @return the list of users that were created.
350     * @hide
351     */
352    public List<UserInfo> getUsers(boolean excludeDying) {
353        try {
354            return mService.getUsers(excludeDying);
355        } catch (RemoteException re) {
356            Log.w(TAG, "Could not get user list", re);
357            return null;
358        }
359    }
360
361    /**
362     * Removes a user and all associated data.
363     * Requires {@link android.Manifest.permission#MANAGE_USERS} permission.
364     * @param userHandle the integer handle of the user, where 0 is the primary user.
365     * @hide
366     */
367    public boolean removeUser(int userHandle) {
368        try {
369            return mService.removeUser(userHandle);
370        } catch (RemoteException re) {
371            Log.w(TAG, "Could not remove user ", re);
372            return false;
373        }
374    }
375
376    /**
377     * Updates the user's name.
378     * Requires {@link android.Manifest.permission#MANAGE_USERS} permission.
379     *
380     * @param userHandle the user's integer handle
381     * @param name the new name for the user
382     * @hide
383     */
384    public void setUserName(int userHandle, String name) {
385        try {
386            mService.setUserName(userHandle, name);
387        } catch (RemoteException re) {
388            Log.w(TAG, "Could not set the user name ", re);
389        }
390    }
391
392    /**
393     * Sets the user's photo.
394     * @param userHandle the user for whom to change the photo.
395     * @param icon the bitmap to set as the photo.
396     * @hide
397     */
398    public void setUserIcon(int userHandle, Bitmap icon) {
399        try {
400            mService.setUserIcon(userHandle, icon);
401        } catch (RemoteException re) {
402            Log.w(TAG, "Could not set the user icon ", re);
403        }
404    }
405
406    /**
407     * Returns a file descriptor for the user's photo. PNG data can be read from this file.
408     * @param userHandle the user whose photo we want to read.
409     * @return a {@link Bitmap} of the user's photo, or null if there's no photo.
410     * @hide
411     */
412    public Bitmap getUserIcon(int userHandle) {
413        try {
414            return mService.getUserIcon(userHandle);
415        } catch (RemoteException re) {
416            Log.w(TAG, "Could not get the user icon ", re);
417            return null;
418        }
419    }
420
421    /**
422     * Enable or disable the use of a guest account. If disabled, the existing guest account
423     * will be wiped.
424     * Requires {@link android.Manifest.permission#MANAGE_USERS} permission.
425     * @param enable whether to enable a guest account.
426     * @hide
427     */
428    public void setGuestEnabled(boolean enable) {
429        try {
430            mService.setGuestEnabled(enable);
431        } catch (RemoteException re) {
432            Log.w(TAG, "Could not change guest account availability to " + enable);
433        }
434    }
435
436    /**
437     * Checks if a guest user is enabled for this device.
438     * Requires {@link android.Manifest.permission#MANAGE_USERS} permission.
439     * @return whether a guest user is enabled
440     * @hide
441     */
442    public boolean isGuestEnabled() {
443        try {
444            return mService.isGuestEnabled();
445        } catch (RemoteException re) {
446            Log.w(TAG, "Could not retrieve guest enabled state");
447            return false;
448        }
449    }
450
451    /**
452     * Wipes all the data for a user, but doesn't remove the user.
453     * Requires {@link android.Manifest.permission#MANAGE_USERS} permission.
454     * @param userHandle
455     * @hide
456     */
457    public void wipeUser(int userHandle) {
458        try {
459            mService.wipeUser(userHandle);
460        } catch (RemoteException re) {
461            Log.w(TAG, "Could not wipe user " + userHandle);
462        }
463    }
464
465    /**
466     * Returns the maximum number of users that can be created on this device. A return value
467     * of 1 means that it is a single user device.
468     * @hide
469     * @return a value greater than or equal to 1
470     */
471    public static int getMaxSupportedUsers() {
472        // Don't allow multiple users on certain builds
473        if (android.os.Build.ID.startsWith("JVP")) return 1;
474        return SystemProperties.getInt("fw.max_users",
475                Resources.getSystem().getInteger(R.integer.config_multiuserMaximumUsers));
476    }
477
478    /**
479     * Returns a serial number on this device for a given userHandle. User handles can be recycled
480     * when deleting and creating users, but serial numbers are not reused until the device is wiped.
481     * @param userHandle
482     * @return a serial number associated with that user, or -1 if the userHandle is not valid.
483     * @hide
484     */
485    public int getUserSerialNumber(int userHandle) {
486        try {
487            return mService.getUserSerialNumber(userHandle);
488        } catch (RemoteException re) {
489            Log.w(TAG, "Could not get serial number for user " + userHandle);
490        }
491        return -1;
492    }
493
494    /**
495     * Returns a userHandle on this device for a given user serial number. User handles can be
496     * recycled when deleting and creating users, but serial numbers are not reused until the device
497     * is wiped.
498     * @param userSerialNumber
499     * @return the userHandle associated with that user serial number, or -1 if the serial number
500     * is not valid.
501     * @hide
502     */
503    public int getUserHandle(int userSerialNumber) {
504        try {
505            return mService.getUserHandle(userSerialNumber);
506        } catch (RemoteException re) {
507            Log.w(TAG, "Could not get userHandle for user " + userSerialNumber);
508        }
509        return -1;
510    }
511
512    /**
513     * Returns whether the current user is allowed to toggle location sharing settings.
514     * @hide
515     */
516    public boolean isLocationSharingToggleAllowed() {
517        return !getUserRestrictions().getBoolean(DISALLOW_SHARE_LOCATION, false);
518    }
519
520    /**
521     * @hide
522     */
523    public List<RestrictionEntry> getApplicationRestrictions(String packageName, UserHandle user) {
524        try {
525            return mService.getApplicationRestrictions(packageName, user.getIdentifier());
526        } catch (RemoteException re) {
527            Log.w(TAG, "Could not get application restrictions for user " + user.getIdentifier());
528        }
529        return null;
530    }
531
532    /**
533     * @hide
534     */
535    public void setApplicationRestrictions(String packageName, List<RestrictionEntry> entries,
536            UserHandle user) {
537        try {
538            mService.setApplicationRestrictions(packageName, entries, user.getIdentifier());
539        } catch (RemoteException re) {
540            Log.w(TAG, "Could not set application restrictions for user " + user.getIdentifier());
541        }
542    }
543}
544