1258848d2ae04f447ff1c18023fa76b139fcc0862Amith Yamasani/*
2258848d2ae04f447ff1c18023fa76b139fcc0862Amith Yamasani * Copyright (C) 2012 The Android Open Source Project
3258848d2ae04f447ff1c18023fa76b139fcc0862Amith Yamasani *
4258848d2ae04f447ff1c18023fa76b139fcc0862Amith Yamasani * Licensed under the Apache License, Version 2.0 (the "License");
5258848d2ae04f447ff1c18023fa76b139fcc0862Amith Yamasani * you may not use this file except in compliance with the License.
6258848d2ae04f447ff1c18023fa76b139fcc0862Amith Yamasani * You may obtain a copy of the License at
7258848d2ae04f447ff1c18023fa76b139fcc0862Amith Yamasani *
8258848d2ae04f447ff1c18023fa76b139fcc0862Amith Yamasani *      http://www.apache.org/licenses/LICENSE-2.0
9258848d2ae04f447ff1c18023fa76b139fcc0862Amith Yamasani *
10258848d2ae04f447ff1c18023fa76b139fcc0862Amith Yamasani * Unless required by applicable law or agreed to in writing, software
11258848d2ae04f447ff1c18023fa76b139fcc0862Amith Yamasani * distributed under the License is distributed on an "AS IS" BASIS,
12258848d2ae04f447ff1c18023fa76b139fcc0862Amith Yamasani * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13258848d2ae04f447ff1c18023fa76b139fcc0862Amith Yamasani * See the License for the specific language governing permissions and
14258848d2ae04f447ff1c18023fa76b139fcc0862Amith Yamasani * limitations under the License.
15258848d2ae04f447ff1c18023fa76b139fcc0862Amith Yamasani */
16258848d2ae04f447ff1c18023fa76b139fcc0862Amith Yamasanipackage android.os;
17258848d2ae04f447ff1c18023fa76b139fcc0862Amith Yamasani
18258848d2ae04f447ff1c18023fa76b139fcc0862Amith Yamasaniimport com.android.internal.R;
19a8a9bd65bf5865d83ef44f54552ca39522bfbcf0Dianne Hackborn
20a8a9bd65bf5865d83ef44f54552ca39522bfbcf0Dianne Hackbornimport android.app.ActivityManagerNative;
21258848d2ae04f447ff1c18023fa76b139fcc0862Amith Yamasaniimport android.content.Context;
22258848d2ae04f447ff1c18023fa76b139fcc0862Amith Yamasaniimport android.content.pm.UserInfo;
23e928d7d95dbb64627e6ff3a0572190c555b59d96Amith Yamasaniimport android.graphics.Bitmap;
2427bd34d9d9fe99f11b80aa0bbdb402fb47ef4158Jeff Sharkeyimport android.content.res.Resources;
25258848d2ae04f447ff1c18023fa76b139fcc0862Amith Yamasaniimport android.util.Log;
26258848d2ae04f447ff1c18023fa76b139fcc0862Amith Yamasani
27258848d2ae04f447ff1c18023fa76b139fcc0862Amith Yamasaniimport java.util.List;
28258848d2ae04f447ff1c18023fa76b139fcc0862Amith Yamasani
29258848d2ae04f447ff1c18023fa76b139fcc0862Amith Yamasani/**
30258848d2ae04f447ff1c18023fa76b139fcc0862Amith Yamasani * Manages users and user details on a multi-user system.
31258848d2ae04f447ff1c18023fa76b139fcc0862Amith Yamasani */
32258848d2ae04f447ff1c18023fa76b139fcc0862Amith Yamasanipublic class UserManager {
33258848d2ae04f447ff1c18023fa76b139fcc0862Amith Yamasani
34258848d2ae04f447ff1c18023fa76b139fcc0862Amith Yamasani    private static String TAG = "UserManager";
35258848d2ae04f447ff1c18023fa76b139fcc0862Amith Yamasani    private final IUserManager mService;
36258848d2ae04f447ff1c18023fa76b139fcc0862Amith Yamasani    private final Context mContext;
37258848d2ae04f447ff1c18023fa76b139fcc0862Amith Yamasani
38258848d2ae04f447ff1c18023fa76b139fcc0862Amith Yamasani    /** @hide */
39258848d2ae04f447ff1c18023fa76b139fcc0862Amith Yamasani    public UserManager(Context context, IUserManager service) {
40258848d2ae04f447ff1c18023fa76b139fcc0862Amith Yamasani        mService = service;
41258848d2ae04f447ff1c18023fa76b139fcc0862Amith Yamasani        mContext = context;
42258848d2ae04f447ff1c18023fa76b139fcc0862Amith Yamasani    }
43258848d2ae04f447ff1c18023fa76b139fcc0862Amith Yamasani
44258848d2ae04f447ff1c18023fa76b139fcc0862Amith Yamasani    /**
45258848d2ae04f447ff1c18023fa76b139fcc0862Amith Yamasani     * Returns whether the system supports multiple users.
46258848d2ae04f447ff1c18023fa76b139fcc0862Amith Yamasani     * @return true if multiple users can be created, false if it is a single user device.
47e928d7d95dbb64627e6ff3a0572190c555b59d96Amith Yamasani     * @hide
48258848d2ae04f447ff1c18023fa76b139fcc0862Amith Yamasani     */
494673e7ea8d1f869910a9c0f9c211d4d27ad50b41Jeff Sharkey    public static boolean supportsMultipleUsers() {
50258848d2ae04f447ff1c18023fa76b139fcc0862Amith Yamasani        return getMaxSupportedUsers() > 1;
51258848d2ae04f447ff1c18023fa76b139fcc0862Amith Yamasani    }
52258848d2ae04f447ff1c18023fa76b139fcc0862Amith Yamasani
53258848d2ae04f447ff1c18023fa76b139fcc0862Amith Yamasani    /**
54258848d2ae04f447ff1c18023fa76b139fcc0862Amith Yamasani     * Returns the user handle for the user that this application is running for.
55258848d2ae04f447ff1c18023fa76b139fcc0862Amith Yamasani     * @return the user handle of the user making this call.
56258848d2ae04f447ff1c18023fa76b139fcc0862Amith Yamasani     * @hide
57258848d2ae04f447ff1c18023fa76b139fcc0862Amith Yamasani     * */
58258848d2ae04f447ff1c18023fa76b139fcc0862Amith Yamasani    public int getUserHandle() {
5979af1dd54c16cde063152922b42c96d72ae9eca8Dianne Hackborn        return UserHandle.myUserId();
60258848d2ae04f447ff1c18023fa76b139fcc0862Amith Yamasani    }
61258848d2ae04f447ff1c18023fa76b139fcc0862Amith Yamasani
62258848d2ae04f447ff1c18023fa76b139fcc0862Amith Yamasani    /**
638832c18d8b63367929c2d394c9c508f56003d400Dianne Hackborn     * Returns the user name of the user making this call.  This call is only
648832c18d8b63367929c2d394c9c508f56003d400Dianne Hackborn     * available to applications on the system image; it requires the
658832c18d8b63367929c2d394c9c508f56003d400Dianne Hackborn     * MANAGE_USERS permission.
66258848d2ae04f447ff1c18023fa76b139fcc0862Amith Yamasani     * @return the user name
67258848d2ae04f447ff1c18023fa76b139fcc0862Amith Yamasani     */
68258848d2ae04f447ff1c18023fa76b139fcc0862Amith Yamasani    public String getUserName() {
69258848d2ae04f447ff1c18023fa76b139fcc0862Amith Yamasani        try {
70258848d2ae04f447ff1c18023fa76b139fcc0862Amith Yamasani            return mService.getUserInfo(getUserHandle()).name;
71258848d2ae04f447ff1c18023fa76b139fcc0862Amith Yamasani        } catch (RemoteException re) {
72258848d2ae04f447ff1c18023fa76b139fcc0862Amith Yamasani            Log.w(TAG, "Could not get user name", re);
73258848d2ae04f447ff1c18023fa76b139fcc0862Amith Yamasani            return "";
74258848d2ae04f447ff1c18023fa76b139fcc0862Amith Yamasani        }
75258848d2ae04f447ff1c18023fa76b139fcc0862Amith Yamasani    }
76258848d2ae04f447ff1c18023fa76b139fcc0862Amith Yamasani
77e4ab16ad98b183afbf7a21ad7314372de41a8b57Dan Morrill   /**
78e4ab16ad98b183afbf7a21ad7314372de41a8b57Dan Morrill     * Used to determine whether the user making this call is subject to
79e4ab16ad98b183afbf7a21ad7314372de41a8b57Dan Morrill     * teleportations.
80e4ab16ad98b183afbf7a21ad7314372de41a8b57Dan Morrill     * @return whether the user making this call is a goat
81e4ab16ad98b183afbf7a21ad7314372de41a8b57Dan Morrill     */
82e4ab16ad98b183afbf7a21ad7314372de41a8b57Dan Morrill    public boolean isUserAGoat() {
83e4ab16ad98b183afbf7a21ad7314372de41a8b57Dan Morrill        return false;
84e4ab16ad98b183afbf7a21ad7314372de41a8b57Dan Morrill    }
85e4ab16ad98b183afbf7a21ad7314372de41a8b57Dan Morrill
86258848d2ae04f447ff1c18023fa76b139fcc0862Amith Yamasani    /**
87a8a9bd65bf5865d83ef44f54552ca39522bfbcf0Dianne Hackborn     * Return whether the given user is actively running.  This means that
88a8a9bd65bf5865d83ef44f54552ca39522bfbcf0Dianne Hackborn     * the user is in the "started" state, not "stopped" -- it is currently
89a8a9bd65bf5865d83ef44f54552ca39522bfbcf0Dianne Hackborn     * allowed to run code through scheduled alarms, receiving broadcasts,
90a8a9bd65bf5865d83ef44f54552ca39522bfbcf0Dianne Hackborn     * etc.  A started user may be either the current foreground user or a
91a8a9bd65bf5865d83ef44f54552ca39522bfbcf0Dianne Hackborn     * background user; the result here does not distinguish between the two.
92a8a9bd65bf5865d83ef44f54552ca39522bfbcf0Dianne Hackborn     * @param user The user to retrieve the running state for.
93a8a9bd65bf5865d83ef44f54552ca39522bfbcf0Dianne Hackborn     */
94a8a9bd65bf5865d83ef44f54552ca39522bfbcf0Dianne Hackborn    public boolean isUserRunning(UserHandle user) {
95a8a9bd65bf5865d83ef44f54552ca39522bfbcf0Dianne Hackborn        try {
96a8a9bd65bf5865d83ef44f54552ca39522bfbcf0Dianne Hackborn            return ActivityManagerNative.getDefault().isUserRunning(
97a8a9bd65bf5865d83ef44f54552ca39522bfbcf0Dianne Hackborn                    user.getIdentifier(), false);
98a8a9bd65bf5865d83ef44f54552ca39522bfbcf0Dianne Hackborn        } catch (RemoteException e) {
99a8a9bd65bf5865d83ef44f54552ca39522bfbcf0Dianne Hackborn            return false;
100a8a9bd65bf5865d83ef44f54552ca39522bfbcf0Dianne Hackborn        }
101a8a9bd65bf5865d83ef44f54552ca39522bfbcf0Dianne Hackborn    }
102a8a9bd65bf5865d83ef44f54552ca39522bfbcf0Dianne Hackborn
103a8a9bd65bf5865d83ef44f54552ca39522bfbcf0Dianne Hackborn    /**
104a8a9bd65bf5865d83ef44f54552ca39522bfbcf0Dianne Hackborn     * Return whether the given user is actively running <em>or</em> stopping.
105a8a9bd65bf5865d83ef44f54552ca39522bfbcf0Dianne Hackborn     * This is like {@link #isUserRunning(UserHandle)}, but will also return
106a8a9bd65bf5865d83ef44f54552ca39522bfbcf0Dianne Hackborn     * true if the user had been running but is in the process of being stopped
107a8a9bd65bf5865d83ef44f54552ca39522bfbcf0Dianne Hackborn     * (but is not yet fully stopped, and still running some code).
108a8a9bd65bf5865d83ef44f54552ca39522bfbcf0Dianne Hackborn     * @param user The user to retrieve the running state for.
109a8a9bd65bf5865d83ef44f54552ca39522bfbcf0Dianne Hackborn     */
110a8a9bd65bf5865d83ef44f54552ca39522bfbcf0Dianne Hackborn    public boolean isUserRunningOrStopping(UserHandle user) {
111a8a9bd65bf5865d83ef44f54552ca39522bfbcf0Dianne Hackborn        try {
112a8a9bd65bf5865d83ef44f54552ca39522bfbcf0Dianne Hackborn            return ActivityManagerNative.getDefault().isUserRunning(
113a8a9bd65bf5865d83ef44f54552ca39522bfbcf0Dianne Hackborn                    user.getIdentifier(), true);
114a8a9bd65bf5865d83ef44f54552ca39522bfbcf0Dianne Hackborn        } catch (RemoteException e) {
115a8a9bd65bf5865d83ef44f54552ca39522bfbcf0Dianne Hackborn            return false;
116a8a9bd65bf5865d83ef44f54552ca39522bfbcf0Dianne Hackborn        }
117a8a9bd65bf5865d83ef44f54552ca39522bfbcf0Dianne Hackborn    }
118a8a9bd65bf5865d83ef44f54552ca39522bfbcf0Dianne Hackborn
119a8a9bd65bf5865d83ef44f54552ca39522bfbcf0Dianne Hackborn    /**
120258848d2ae04f447ff1c18023fa76b139fcc0862Amith Yamasani     * Returns the UserInfo object describing a specific user.
1211952637425eece18aa1ce3d80d4b49086ef3bcf7Amith Yamasani     * Requires {@link android.Manifest.permission#MANAGE_USERS} permission.
122258848d2ae04f447ff1c18023fa76b139fcc0862Amith Yamasani     * @param userHandle the user handle of the user whose information is being requested.
123258848d2ae04f447ff1c18023fa76b139fcc0862Amith Yamasani     * @return the UserInfo object for a specific user.
124258848d2ae04f447ff1c18023fa76b139fcc0862Amith Yamasani     * @hide
125b26306ad5277097b3abb345112b24d9a142fb299Dianne Hackborn     */
126258848d2ae04f447ff1c18023fa76b139fcc0862Amith Yamasani    public UserInfo getUserInfo(int userHandle) {
127258848d2ae04f447ff1c18023fa76b139fcc0862Amith Yamasani        try {
128258848d2ae04f447ff1c18023fa76b139fcc0862Amith Yamasani            return mService.getUserInfo(userHandle);
129258848d2ae04f447ff1c18023fa76b139fcc0862Amith Yamasani        } catch (RemoteException re) {
130258848d2ae04f447ff1c18023fa76b139fcc0862Amith Yamasani            Log.w(TAG, "Could not get user info", re);
131258848d2ae04f447ff1c18023fa76b139fcc0862Amith Yamasani            return null;
132258848d2ae04f447ff1c18023fa76b139fcc0862Amith Yamasani        }
133258848d2ae04f447ff1c18023fa76b139fcc0862Amith Yamasani    }
134258848d2ae04f447ff1c18023fa76b139fcc0862Amith Yamasani
135258848d2ae04f447ff1c18023fa76b139fcc0862Amith Yamasani    /**
13633f9cb8cf01e0a6288eb5b9ce724c56aa4e1e382Dianne Hackborn     * Return the serial number for a user.  This is a device-unique
137b26306ad5277097b3abb345112b24d9a142fb299Dianne Hackborn     * number assigned to that user; if the user is deleted and then a new
138b26306ad5277097b3abb345112b24d9a142fb299Dianne Hackborn     * user created, the new users will not be given the same serial number.
13933f9cb8cf01e0a6288eb5b9ce724c56aa4e1e382Dianne Hackborn     * @param user The user whose serial number is to be retrieved.
140b26306ad5277097b3abb345112b24d9a142fb299Dianne Hackborn     * @return The serial number of the given user; returns -1 if the
141b26306ad5277097b3abb345112b24d9a142fb299Dianne Hackborn     * given UserHandle does not exist.
14233f9cb8cf01e0a6288eb5b9ce724c56aa4e1e382Dianne Hackborn     * @see #getUserForSerialNumber(long)
14333f9cb8cf01e0a6288eb5b9ce724c56aa4e1e382Dianne Hackborn     */
14433f9cb8cf01e0a6288eb5b9ce724c56aa4e1e382Dianne Hackborn    public long getSerialNumberForUser(UserHandle user) {
14533f9cb8cf01e0a6288eb5b9ce724c56aa4e1e382Dianne Hackborn        return getUserSerialNumber(user.getIdentifier());
14633f9cb8cf01e0a6288eb5b9ce724c56aa4e1e382Dianne Hackborn    }
14733f9cb8cf01e0a6288eb5b9ce724c56aa4e1e382Dianne Hackborn
14833f9cb8cf01e0a6288eb5b9ce724c56aa4e1e382Dianne Hackborn    /**
14933f9cb8cf01e0a6288eb5b9ce724c56aa4e1e382Dianne Hackborn     * Return the user associated with a serial number previously
15033f9cb8cf01e0a6288eb5b9ce724c56aa4e1e382Dianne Hackborn     * returned by {@link #getSerialNumberForUser(UserHandle)}.
15133f9cb8cf01e0a6288eb5b9ce724c56aa4e1e382Dianne Hackborn     * @param serialNumber The serial number of the user that is being
15233f9cb8cf01e0a6288eb5b9ce724c56aa4e1e382Dianne Hackborn     * retrieved.
15333f9cb8cf01e0a6288eb5b9ce724c56aa4e1e382Dianne Hackborn     * @return Return the user associated with the serial number, or null
15433f9cb8cf01e0a6288eb5b9ce724c56aa4e1e382Dianne Hackborn     * if there is not one.
15533f9cb8cf01e0a6288eb5b9ce724c56aa4e1e382Dianne Hackborn     * @see #getSerialNumberForUser(UserHandle)
15633f9cb8cf01e0a6288eb5b9ce724c56aa4e1e382Dianne Hackborn     */
15733f9cb8cf01e0a6288eb5b9ce724c56aa4e1e382Dianne Hackborn    public UserHandle getUserForSerialNumber(long serialNumber) {
15833f9cb8cf01e0a6288eb5b9ce724c56aa4e1e382Dianne Hackborn        int ident = getUserHandle((int)serialNumber);
15933f9cb8cf01e0a6288eb5b9ce724c56aa4e1e382Dianne Hackborn        return ident >= 0 ? new UserHandle(ident) : null;
16033f9cb8cf01e0a6288eb5b9ce724c56aa4e1e382Dianne Hackborn    }
16133f9cb8cf01e0a6288eb5b9ce724c56aa4e1e382Dianne Hackborn
16233f9cb8cf01e0a6288eb5b9ce724c56aa4e1e382Dianne Hackborn    /**
163258848d2ae04f447ff1c18023fa76b139fcc0862Amith Yamasani     * Creates a user with the specified name and options.
1641952637425eece18aa1ce3d80d4b49086ef3bcf7Amith Yamasani     * Requires {@link android.Manifest.permission#MANAGE_USERS} permission.
165258848d2ae04f447ff1c18023fa76b139fcc0862Amith Yamasani     *
166258848d2ae04f447ff1c18023fa76b139fcc0862Amith Yamasani     * @param name the user's name
167258848d2ae04f447ff1c18023fa76b139fcc0862Amith Yamasani     * @param flags flags that identify the type of user and other properties.
168258848d2ae04f447ff1c18023fa76b139fcc0862Amith Yamasani     * @see UserInfo
169258848d2ae04f447ff1c18023fa76b139fcc0862Amith Yamasani     *
170258848d2ae04f447ff1c18023fa76b139fcc0862Amith Yamasani     * @return the UserInfo object for the created user, or null if the user could not be created.
171258848d2ae04f447ff1c18023fa76b139fcc0862Amith Yamasani     * @hide
172258848d2ae04f447ff1c18023fa76b139fcc0862Amith Yamasani     */
173258848d2ae04f447ff1c18023fa76b139fcc0862Amith Yamasani    public UserInfo createUser(String name, int flags) {
174258848d2ae04f447ff1c18023fa76b139fcc0862Amith Yamasani        try {
175258848d2ae04f447ff1c18023fa76b139fcc0862Amith Yamasani            return mService.createUser(name, flags);
176258848d2ae04f447ff1c18023fa76b139fcc0862Amith Yamasani        } catch (RemoteException re) {
177258848d2ae04f447ff1c18023fa76b139fcc0862Amith Yamasani            Log.w(TAG, "Could not create a user", re);
178258848d2ae04f447ff1c18023fa76b139fcc0862Amith Yamasani            return null;
179258848d2ae04f447ff1c18023fa76b139fcc0862Amith Yamasani        }
180258848d2ae04f447ff1c18023fa76b139fcc0862Amith Yamasani    }
181258848d2ae04f447ff1c18023fa76b139fcc0862Amith Yamasani
182258848d2ae04f447ff1c18023fa76b139fcc0862Amith Yamasani    /**
183b26306ad5277097b3abb345112b24d9a142fb299Dianne Hackborn     * Return the number of users currently created on the device.
184b26306ad5277097b3abb345112b24d9a142fb299Dianne Hackborn     */
185b26306ad5277097b3abb345112b24d9a142fb299Dianne Hackborn    public int getUserCount() {
186b26306ad5277097b3abb345112b24d9a142fb299Dianne Hackborn        List<UserInfo> users = getUsers();
187b26306ad5277097b3abb345112b24d9a142fb299Dianne Hackborn        return users != null ? users.size() : 1;
188b26306ad5277097b3abb345112b24d9a142fb299Dianne Hackborn    }
189b26306ad5277097b3abb345112b24d9a142fb299Dianne Hackborn
190b26306ad5277097b3abb345112b24d9a142fb299Dianne Hackborn    /**
191258848d2ae04f447ff1c18023fa76b139fcc0862Amith Yamasani     * Returns information for all users on this device.
1921952637425eece18aa1ce3d80d4b49086ef3bcf7Amith Yamasani     * Requires {@link android.Manifest.permission#MANAGE_USERS} permission.
193258848d2ae04f447ff1c18023fa76b139fcc0862Amith Yamasani     * @return the list of users that were created.
194258848d2ae04f447ff1c18023fa76b139fcc0862Amith Yamasani     * @hide
195258848d2ae04f447ff1c18023fa76b139fcc0862Amith Yamasani     */
196258848d2ae04f447ff1c18023fa76b139fcc0862Amith Yamasani    public List<UserInfo> getUsers() {
197258848d2ae04f447ff1c18023fa76b139fcc0862Amith Yamasani        try {
198920ace0bbc2d4133dbec991d2636c99a57d6245eAmith Yamasani            return mService.getUsers(false);
199920ace0bbc2d4133dbec991d2636c99a57d6245eAmith Yamasani        } catch (RemoteException re) {
200920ace0bbc2d4133dbec991d2636c99a57d6245eAmith Yamasani            Log.w(TAG, "Could not get user list", re);
201920ace0bbc2d4133dbec991d2636c99a57d6245eAmith Yamasani            return null;
202920ace0bbc2d4133dbec991d2636c99a57d6245eAmith Yamasani        }
203920ace0bbc2d4133dbec991d2636c99a57d6245eAmith Yamasani    }
204920ace0bbc2d4133dbec991d2636c99a57d6245eAmith Yamasani
205920ace0bbc2d4133dbec991d2636c99a57d6245eAmith Yamasani    /**
206920ace0bbc2d4133dbec991d2636c99a57d6245eAmith Yamasani     * Returns information for all users on this device.
207920ace0bbc2d4133dbec991d2636c99a57d6245eAmith Yamasani     * Requires {@link android.Manifest.permission#MANAGE_USERS} permission.
208920ace0bbc2d4133dbec991d2636c99a57d6245eAmith Yamasani     * @param excludeDying specify if the list should exclude users being removed.
209920ace0bbc2d4133dbec991d2636c99a57d6245eAmith Yamasani     * @return the list of users that were created.
210920ace0bbc2d4133dbec991d2636c99a57d6245eAmith Yamasani     * @hide
211920ace0bbc2d4133dbec991d2636c99a57d6245eAmith Yamasani     */
212920ace0bbc2d4133dbec991d2636c99a57d6245eAmith Yamasani    public List<UserInfo> getUsers(boolean excludeDying) {
213920ace0bbc2d4133dbec991d2636c99a57d6245eAmith Yamasani        try {
214920ace0bbc2d4133dbec991d2636c99a57d6245eAmith Yamasani            return mService.getUsers(excludeDying);
215258848d2ae04f447ff1c18023fa76b139fcc0862Amith Yamasani        } catch (RemoteException re) {
216258848d2ae04f447ff1c18023fa76b139fcc0862Amith Yamasani            Log.w(TAG, "Could not get user list", re);
217258848d2ae04f447ff1c18023fa76b139fcc0862Amith Yamasani            return null;
218258848d2ae04f447ff1c18023fa76b139fcc0862Amith Yamasani        }
219258848d2ae04f447ff1c18023fa76b139fcc0862Amith Yamasani    }
220258848d2ae04f447ff1c18023fa76b139fcc0862Amith Yamasani
221258848d2ae04f447ff1c18023fa76b139fcc0862Amith Yamasani    /**
222258848d2ae04f447ff1c18023fa76b139fcc0862Amith Yamasani     * Removes a user and all associated data.
2231952637425eece18aa1ce3d80d4b49086ef3bcf7Amith Yamasani     * Requires {@link android.Manifest.permission#MANAGE_USERS} permission.
224258848d2ae04f447ff1c18023fa76b139fcc0862Amith Yamasani     * @param userHandle the integer handle of the user, where 0 is the primary user.
225258848d2ae04f447ff1c18023fa76b139fcc0862Amith Yamasani     * @hide
226258848d2ae04f447ff1c18023fa76b139fcc0862Amith Yamasani     */
227258848d2ae04f447ff1c18023fa76b139fcc0862Amith Yamasani    public boolean removeUser(int userHandle) {
228258848d2ae04f447ff1c18023fa76b139fcc0862Amith Yamasani        try {
229258848d2ae04f447ff1c18023fa76b139fcc0862Amith Yamasani            return mService.removeUser(userHandle);
230258848d2ae04f447ff1c18023fa76b139fcc0862Amith Yamasani        } catch (RemoteException re) {
231258848d2ae04f447ff1c18023fa76b139fcc0862Amith Yamasani            Log.w(TAG, "Could not remove user ", re);
232258848d2ae04f447ff1c18023fa76b139fcc0862Amith Yamasani            return false;
233258848d2ae04f447ff1c18023fa76b139fcc0862Amith Yamasani        }
234258848d2ae04f447ff1c18023fa76b139fcc0862Amith Yamasani    }
235258848d2ae04f447ff1c18023fa76b139fcc0862Amith Yamasani
236258848d2ae04f447ff1c18023fa76b139fcc0862Amith Yamasani    /**
237258848d2ae04f447ff1c18023fa76b139fcc0862Amith Yamasani     * Updates the user's name.
2381952637425eece18aa1ce3d80d4b49086ef3bcf7Amith Yamasani     * Requires {@link android.Manifest.permission#MANAGE_USERS} permission.
239258848d2ae04f447ff1c18023fa76b139fcc0862Amith Yamasani     *
240258848d2ae04f447ff1c18023fa76b139fcc0862Amith Yamasani     * @param userHandle the user's integer handle
241258848d2ae04f447ff1c18023fa76b139fcc0862Amith Yamasani     * @param name the new name for the user
242258848d2ae04f447ff1c18023fa76b139fcc0862Amith Yamasani     * @hide
243258848d2ae04f447ff1c18023fa76b139fcc0862Amith Yamasani     */
244258848d2ae04f447ff1c18023fa76b139fcc0862Amith Yamasani    public void setUserName(int userHandle, String name) {
245258848d2ae04f447ff1c18023fa76b139fcc0862Amith Yamasani        try {
246258848d2ae04f447ff1c18023fa76b139fcc0862Amith Yamasani            mService.setUserName(userHandle, name);
247258848d2ae04f447ff1c18023fa76b139fcc0862Amith Yamasani        } catch (RemoteException re) {
248258848d2ae04f447ff1c18023fa76b139fcc0862Amith Yamasani            Log.w(TAG, "Could not set the user name ", re);
249258848d2ae04f447ff1c18023fa76b139fcc0862Amith Yamasani        }
250258848d2ae04f447ff1c18023fa76b139fcc0862Amith Yamasani    }
251258848d2ae04f447ff1c18023fa76b139fcc0862Amith Yamasani
252258848d2ae04f447ff1c18023fa76b139fcc0862Amith Yamasani    /**
253e928d7d95dbb64627e6ff3a0572190c555b59d96Amith Yamasani     * Sets the user's photo.
254258848d2ae04f447ff1c18023fa76b139fcc0862Amith Yamasani     * @param userHandle the user for whom to change the photo.
255e928d7d95dbb64627e6ff3a0572190c555b59d96Amith Yamasani     * @param icon the bitmap to set as the photo.
256258848d2ae04f447ff1c18023fa76b139fcc0862Amith Yamasani     * @hide
257258848d2ae04f447ff1c18023fa76b139fcc0862Amith Yamasani     */
258e928d7d95dbb64627e6ff3a0572190c555b59d96Amith Yamasani    public void setUserIcon(int userHandle, Bitmap icon) {
259258848d2ae04f447ff1c18023fa76b139fcc0862Amith Yamasani        try {
260e928d7d95dbb64627e6ff3a0572190c555b59d96Amith Yamasani            mService.setUserIcon(userHandle, icon);
261258848d2ae04f447ff1c18023fa76b139fcc0862Amith Yamasani        } catch (RemoteException re) {
262258848d2ae04f447ff1c18023fa76b139fcc0862Amith Yamasani            Log.w(TAG, "Could not set the user icon ", re);
263258848d2ae04f447ff1c18023fa76b139fcc0862Amith Yamasani        }
264258848d2ae04f447ff1c18023fa76b139fcc0862Amith Yamasani    }
265258848d2ae04f447ff1c18023fa76b139fcc0862Amith Yamasani
266258848d2ae04f447ff1c18023fa76b139fcc0862Amith Yamasani    /**
2673b49f07a452e0a77c1d22db2065255689a461d31Amith Yamasani     * Returns a file descriptor for the user's photo. PNG data can be read from this file.
2683b49f07a452e0a77c1d22db2065255689a461d31Amith Yamasani     * @param userHandle the user whose photo we want to read.
269e928d7d95dbb64627e6ff3a0572190c555b59d96Amith Yamasani     * @return a {@link Bitmap} of the user's photo, or null if there's no photo.
2703b49f07a452e0a77c1d22db2065255689a461d31Amith Yamasani     * @hide
2713b49f07a452e0a77c1d22db2065255689a461d31Amith Yamasani     */
272e928d7d95dbb64627e6ff3a0572190c555b59d96Amith Yamasani    public Bitmap getUserIcon(int userHandle) {
2733b49f07a452e0a77c1d22db2065255689a461d31Amith Yamasani        try {
2743b49f07a452e0a77c1d22db2065255689a461d31Amith Yamasani            return mService.getUserIcon(userHandle);
2753b49f07a452e0a77c1d22db2065255689a461d31Amith Yamasani        } catch (RemoteException re) {
276e928d7d95dbb64627e6ff3a0572190c555b59d96Amith Yamasani            Log.w(TAG, "Could not get the user icon ", re);
2773b49f07a452e0a77c1d22db2065255689a461d31Amith Yamasani            return null;
2783b49f07a452e0a77c1d22db2065255689a461d31Amith Yamasani        }
2793b49f07a452e0a77c1d22db2065255689a461d31Amith Yamasani    }
2803b49f07a452e0a77c1d22db2065255689a461d31Amith Yamasani
2813b49f07a452e0a77c1d22db2065255689a461d31Amith Yamasani    /**
282258848d2ae04f447ff1c18023fa76b139fcc0862Amith Yamasani     * Enable or disable the use of a guest account. If disabled, the existing guest account
283258848d2ae04f447ff1c18023fa76b139fcc0862Amith Yamasani     * will be wiped.
2841952637425eece18aa1ce3d80d4b49086ef3bcf7Amith Yamasani     * Requires {@link android.Manifest.permission#MANAGE_USERS} permission.
285258848d2ae04f447ff1c18023fa76b139fcc0862Amith Yamasani     * @param enable whether to enable a guest account.
286258848d2ae04f447ff1c18023fa76b139fcc0862Amith Yamasani     * @hide
287258848d2ae04f447ff1c18023fa76b139fcc0862Amith Yamasani     */
288258848d2ae04f447ff1c18023fa76b139fcc0862Amith Yamasani    public void setGuestEnabled(boolean enable) {
289258848d2ae04f447ff1c18023fa76b139fcc0862Amith Yamasani        try {
290258848d2ae04f447ff1c18023fa76b139fcc0862Amith Yamasani            mService.setGuestEnabled(enable);
291258848d2ae04f447ff1c18023fa76b139fcc0862Amith Yamasani        } catch (RemoteException re) {
292258848d2ae04f447ff1c18023fa76b139fcc0862Amith Yamasani            Log.w(TAG, "Could not change guest account availability to " + enable);
293258848d2ae04f447ff1c18023fa76b139fcc0862Amith Yamasani        }
294258848d2ae04f447ff1c18023fa76b139fcc0862Amith Yamasani    }
295258848d2ae04f447ff1c18023fa76b139fcc0862Amith Yamasani
296258848d2ae04f447ff1c18023fa76b139fcc0862Amith Yamasani    /**
297258848d2ae04f447ff1c18023fa76b139fcc0862Amith Yamasani     * Checks if a guest user is enabled for this device.
2981952637425eece18aa1ce3d80d4b49086ef3bcf7Amith Yamasani     * Requires {@link android.Manifest.permission#MANAGE_USERS} permission.
299258848d2ae04f447ff1c18023fa76b139fcc0862Amith Yamasani     * @return whether a guest user is enabled
300258848d2ae04f447ff1c18023fa76b139fcc0862Amith Yamasani     * @hide
301258848d2ae04f447ff1c18023fa76b139fcc0862Amith Yamasani     */
302258848d2ae04f447ff1c18023fa76b139fcc0862Amith Yamasani    public boolean isGuestEnabled() {
303258848d2ae04f447ff1c18023fa76b139fcc0862Amith Yamasani        try {
304258848d2ae04f447ff1c18023fa76b139fcc0862Amith Yamasani            return mService.isGuestEnabled();
305258848d2ae04f447ff1c18023fa76b139fcc0862Amith Yamasani        } catch (RemoteException re) {
306258848d2ae04f447ff1c18023fa76b139fcc0862Amith Yamasani            Log.w(TAG, "Could not retrieve guest enabled state");
307258848d2ae04f447ff1c18023fa76b139fcc0862Amith Yamasani            return false;
308258848d2ae04f447ff1c18023fa76b139fcc0862Amith Yamasani        }
309258848d2ae04f447ff1c18023fa76b139fcc0862Amith Yamasani    }
310258848d2ae04f447ff1c18023fa76b139fcc0862Amith Yamasani
311258848d2ae04f447ff1c18023fa76b139fcc0862Amith Yamasani    /**
312258848d2ae04f447ff1c18023fa76b139fcc0862Amith Yamasani     * Wipes all the data for a user, but doesn't remove the user.
3131952637425eece18aa1ce3d80d4b49086ef3bcf7Amith Yamasani     * Requires {@link android.Manifest.permission#MANAGE_USERS} permission.
314258848d2ae04f447ff1c18023fa76b139fcc0862Amith Yamasani     * @param userHandle
315258848d2ae04f447ff1c18023fa76b139fcc0862Amith Yamasani     * @hide
316258848d2ae04f447ff1c18023fa76b139fcc0862Amith Yamasani     */
317258848d2ae04f447ff1c18023fa76b139fcc0862Amith Yamasani    public void wipeUser(int userHandle) {
318258848d2ae04f447ff1c18023fa76b139fcc0862Amith Yamasani        try {
319258848d2ae04f447ff1c18023fa76b139fcc0862Amith Yamasani            mService.wipeUser(userHandle);
320258848d2ae04f447ff1c18023fa76b139fcc0862Amith Yamasani        } catch (RemoteException re) {
321258848d2ae04f447ff1c18023fa76b139fcc0862Amith Yamasani            Log.w(TAG, "Could not wipe user " + userHandle);
322258848d2ae04f447ff1c18023fa76b139fcc0862Amith Yamasani        }
323258848d2ae04f447ff1c18023fa76b139fcc0862Amith Yamasani    }
324258848d2ae04f447ff1c18023fa76b139fcc0862Amith Yamasani
325258848d2ae04f447ff1c18023fa76b139fcc0862Amith Yamasani    /**
326258848d2ae04f447ff1c18023fa76b139fcc0862Amith Yamasani     * Returns the maximum number of users that can be created on this device. A return value
327258848d2ae04f447ff1c18023fa76b139fcc0862Amith Yamasani     * of 1 means that it is a single user device.
328258848d2ae04f447ff1c18023fa76b139fcc0862Amith Yamasani     * @hide
329258848d2ae04f447ff1c18023fa76b139fcc0862Amith Yamasani     * @return a value greater than or equal to 1
330258848d2ae04f447ff1c18023fa76b139fcc0862Amith Yamasani     */
33127bd34d9d9fe99f11b80aa0bbdb402fb47ef4158Jeff Sharkey    public static int getMaxSupportedUsers() {
332ff54920ed222a2bd6abe618743a5a3e9fe10bd4bAmith Yamasani        // Don't allow multiple users on certain builds
333ff54920ed222a2bd6abe618743a5a3e9fe10bd4bAmith Yamasani        if (android.os.Build.ID.startsWith("JVP")) return 1;
33427bd34d9d9fe99f11b80aa0bbdb402fb47ef4158Jeff Sharkey        return SystemProperties.getInt("fw.max_users",
33527bd34d9d9fe99f11b80aa0bbdb402fb47ef4158Jeff Sharkey                Resources.getSystem().getInteger(R.integer.config_multiuserMaximumUsers));
336258848d2ae04f447ff1c18023fa76b139fcc0862Amith Yamasani    }
3372a00329c6d55c6cd9166e01963d7410e95d80d21Amith Yamasani
3382a00329c6d55c6cd9166e01963d7410e95d80d21Amith Yamasani    /**
3392a00329c6d55c6cd9166e01963d7410e95d80d21Amith Yamasani     * Returns a serial number on this device for a given userHandle. User handles can be recycled
3402a00329c6d55c6cd9166e01963d7410e95d80d21Amith Yamasani     * when deleting and creating users, but serial numbers are not reused until the device is wiped.
3412a00329c6d55c6cd9166e01963d7410e95d80d21Amith Yamasani     * @param userHandle
3422a00329c6d55c6cd9166e01963d7410e95d80d21Amith Yamasani     * @return a serial number associated with that user, or -1 if the userHandle is not valid.
3432a00329c6d55c6cd9166e01963d7410e95d80d21Amith Yamasani     * @hide
3442a00329c6d55c6cd9166e01963d7410e95d80d21Amith Yamasani     */
3452a00329c6d55c6cd9166e01963d7410e95d80d21Amith Yamasani    public int getUserSerialNumber(int userHandle) {
3462a00329c6d55c6cd9166e01963d7410e95d80d21Amith Yamasani        try {
3472a00329c6d55c6cd9166e01963d7410e95d80d21Amith Yamasani            return mService.getUserSerialNumber(userHandle);
3482a00329c6d55c6cd9166e01963d7410e95d80d21Amith Yamasani        } catch (RemoteException re) {
3492a00329c6d55c6cd9166e01963d7410e95d80d21Amith Yamasani            Log.w(TAG, "Could not get serial number for user " + userHandle);
3502a00329c6d55c6cd9166e01963d7410e95d80d21Amith Yamasani        }
3512a00329c6d55c6cd9166e01963d7410e95d80d21Amith Yamasani        return -1;
3522a00329c6d55c6cd9166e01963d7410e95d80d21Amith Yamasani    }
3532a00329c6d55c6cd9166e01963d7410e95d80d21Amith Yamasani
3542a00329c6d55c6cd9166e01963d7410e95d80d21Amith Yamasani    /**
3552a00329c6d55c6cd9166e01963d7410e95d80d21Amith Yamasani     * Returns a userHandle on this device for a given user serial number. User handles can be
3562a00329c6d55c6cd9166e01963d7410e95d80d21Amith Yamasani     * recycled when deleting and creating users, but serial numbers are not reused until the device
3572a00329c6d55c6cd9166e01963d7410e95d80d21Amith Yamasani     * is wiped.
3582a00329c6d55c6cd9166e01963d7410e95d80d21Amith Yamasani     * @param userSerialNumber
3592a00329c6d55c6cd9166e01963d7410e95d80d21Amith Yamasani     * @return the userHandle associated with that user serial number, or -1 if the serial number
3602a00329c6d55c6cd9166e01963d7410e95d80d21Amith Yamasani     * is not valid.
3612a00329c6d55c6cd9166e01963d7410e95d80d21Amith Yamasani     * @hide
3622a00329c6d55c6cd9166e01963d7410e95d80d21Amith Yamasani     */
3632a00329c6d55c6cd9166e01963d7410e95d80d21Amith Yamasani    public int getUserHandle(int userSerialNumber) {
3642a00329c6d55c6cd9166e01963d7410e95d80d21Amith Yamasani        try {
3652a00329c6d55c6cd9166e01963d7410e95d80d21Amith Yamasani            return mService.getUserHandle(userSerialNumber);
3662a00329c6d55c6cd9166e01963d7410e95d80d21Amith Yamasani        } catch (RemoteException re) {
3672a00329c6d55c6cd9166e01963d7410e95d80d21Amith Yamasani            Log.w(TAG, "Could not get userHandle for user " + userSerialNumber);
3682a00329c6d55c6cd9166e01963d7410e95d80d21Amith Yamasani        }
3692a00329c6d55c6cd9166e01963d7410e95d80d21Amith Yamasani        return -1;
3702a00329c6d55c6cd9166e01963d7410e95d80d21Amith Yamasani    }
371258848d2ae04f447ff1c18023fa76b139fcc0862Amith Yamasani}
372