UserManager.java revision 4673e7ea8d1f869910a9c0f9c211d4d27ad50b41
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 com.android.internal.R;
19import android.content.Context;
20import android.content.pm.UserInfo;
21import android.graphics.Bitmap;
22import android.content.res.Resources;
23import android.util.Log;
24
25import java.util.List;
26
27/**
28 * Manages users and user details on a multi-user system.
29 */
30public class UserManager {
31
32    private static String TAG = "UserManager";
33    private final IUserManager mService;
34    private final Context mContext;
35
36    /** @hide */
37    public UserManager(Context context, IUserManager service) {
38        mService = service;
39        mContext = context;
40    }
41
42    /**
43     * Returns whether the system supports multiple users.
44     * @return true if multiple users can be created, false if it is a single user device.
45     * @hide
46     */
47    public static boolean supportsMultipleUsers() {
48        return getMaxSupportedUsers() > 1;
49    }
50
51    /**
52     * Returns the user handle for the user that this application is running for.
53     * @return the user handle of the user making this call.
54     * @hide
55     * */
56    public int getUserHandle() {
57        return UserHandle.myUserId();
58    }
59
60    /**
61     * Returns the user name of the user making this call.  This call is only
62     * available to applications on the system image; it requires the
63     * MANAGE_USERS permission.
64     * @return the user name
65     */
66    public String getUserName() {
67        try {
68            return mService.getUserInfo(getUserHandle()).name;
69        } catch (RemoteException re) {
70            Log.w(TAG, "Could not get user name", re);
71            return "";
72        }
73    }
74
75    /**
76     * Returns the UserInfo object describing a specific user.
77     * Requires {@link android.Manifest.permission#MANAGE_USERS} permission.
78     * @param userHandle the user handle of the user whose information is being requested.
79     * @return the UserInfo object for a specific user.
80     * @hide
81     * */
82    public UserInfo getUserInfo(int userHandle) {
83        try {
84            return mService.getUserInfo(userHandle);
85        } catch (RemoteException re) {
86            Log.w(TAG, "Could not get user info", re);
87            return null;
88        }
89    }
90
91    /**
92     * Creates a user with the specified name and options.
93     * Requires {@link android.Manifest.permission#MANAGE_USERS} permission.
94     *
95     * @param name the user's name
96     * @param flags flags that identify the type of user and other properties.
97     * @see UserInfo
98     *
99     * @return the UserInfo object for the created user, or null if the user could not be created.
100     * @hide
101     */
102    public UserInfo createUser(String name, int flags) {
103        try {
104            return mService.createUser(name, flags);
105        } catch (RemoteException re) {
106            Log.w(TAG, "Could not create a user", re);
107            return null;
108        }
109    }
110
111    /**
112     * Returns information for all users on this device.
113     * Requires {@link android.Manifest.permission#MANAGE_USERS} permission.
114     * @return the list of users that were created.
115     * @hide
116     */
117    public List<UserInfo> getUsers() {
118        try {
119            return mService.getUsers();
120        } catch (RemoteException re) {
121            Log.w(TAG, "Could not get user list", re);
122            return null;
123        }
124    }
125
126    /**
127     * Removes a user and all associated data.
128     * Requires {@link android.Manifest.permission#MANAGE_USERS} permission.
129     * @param userHandle the integer handle of the user, where 0 is the primary user.
130     * @hide
131     */
132    public boolean removeUser(int userHandle) {
133        try {
134            return mService.removeUser(userHandle);
135        } catch (RemoteException re) {
136            Log.w(TAG, "Could not remove user ", re);
137            return false;
138        }
139    }
140
141    /**
142     * Updates the user's name.
143     * Requires {@link android.Manifest.permission#MANAGE_USERS} permission.
144     *
145     * @param userHandle the user's integer handle
146     * @param name the new name for the user
147     * @hide
148     */
149    public void setUserName(int userHandle, String name) {
150        try {
151            mService.setUserName(userHandle, name);
152        } catch (RemoteException re) {
153            Log.w(TAG, "Could not set the user name ", re);
154        }
155    }
156
157    /**
158     * Sets the user's photo.
159     * @param userHandle the user for whom to change the photo.
160     * @param icon the bitmap to set as the photo.
161     * @hide
162     */
163    public void setUserIcon(int userHandle, Bitmap icon) {
164        try {
165            mService.setUserIcon(userHandle, icon);
166        } catch (RemoteException re) {
167            Log.w(TAG, "Could not set the user icon ", re);
168        }
169    }
170
171    /**
172     * Returns a file descriptor for the user's photo. PNG data can be read from this file.
173     * @param userHandle the user whose photo we want to read.
174     * @return a {@link Bitmap} of the user's photo, or null if there's no photo.
175     * @hide
176     */
177    public Bitmap getUserIcon(int userHandle) {
178        try {
179            return mService.getUserIcon(userHandle);
180        } catch (RemoteException re) {
181            Log.w(TAG, "Could not get the user icon ", re);
182            return null;
183        }
184    }
185
186    /**
187     * Enable or disable the use of a guest account. If disabled, the existing guest account
188     * will be wiped.
189     * Requires {@link android.Manifest.permission#MANAGE_USERS} permission.
190     * @param enable whether to enable a guest account.
191     * @hide
192     */
193    public void setGuestEnabled(boolean enable) {
194        try {
195            mService.setGuestEnabled(enable);
196        } catch (RemoteException re) {
197            Log.w(TAG, "Could not change guest account availability to " + enable);
198        }
199    }
200
201    /**
202     * Checks if a guest user is enabled for this device.
203     * Requires {@link android.Manifest.permission#MANAGE_USERS} permission.
204     * @return whether a guest user is enabled
205     * @hide
206     */
207    public boolean isGuestEnabled() {
208        try {
209            return mService.isGuestEnabled();
210        } catch (RemoteException re) {
211            Log.w(TAG, "Could not retrieve guest enabled state");
212            return false;
213        }
214    }
215
216    /**
217     * Wipes all the data for a user, but doesn't remove the user.
218     * Requires {@link android.Manifest.permission#MANAGE_USERS} permission.
219     * @param userHandle
220     * @hide
221     */
222    public void wipeUser(int userHandle) {
223        try {
224            mService.wipeUser(userHandle);
225        } catch (RemoteException re) {
226            Log.w(TAG, "Could not wipe user " + userHandle);
227        }
228    }
229
230    /**
231     * Returns the maximum number of users that can be created on this device. A return value
232     * of 1 means that it is a single user device.
233     * @hide
234     * @return a value greater than or equal to 1
235     */
236    public static int getMaxSupportedUsers() {
237        return SystemProperties.getInt("fw.max_users",
238                Resources.getSystem().getInteger(R.integer.config_multiuserMaximumUsers));
239    }
240
241    /**
242     * Returns a serial number on this device for a given userHandle. User handles can be recycled
243     * when deleting and creating users, but serial numbers are not reused until the device is wiped.
244     * @param userHandle
245     * @return a serial number associated with that user, or -1 if the userHandle is not valid.
246     * @hide
247     */
248    public int getUserSerialNumber(int userHandle) {
249        try {
250            return mService.getUserSerialNumber(userHandle);
251        } catch (RemoteException re) {
252            Log.w(TAG, "Could not get serial number for user " + userHandle);
253        }
254        return -1;
255    }
256
257    /**
258     * Returns a userHandle on this device for a given user serial number. User handles can be
259     * recycled when deleting and creating users, but serial numbers are not reused until the device
260     * is wiped.
261     * @param userSerialNumber
262     * @return the userHandle associated with that user serial number, or -1 if the serial number
263     * is not valid.
264     * @hide
265     */
266    public int getUserHandle(int userSerialNumber) {
267        try {
268            return mService.getUserHandle(userSerialNumber);
269        } catch (RemoteException re) {
270            Log.w(TAG, "Could not get userHandle for user " + userSerialNumber);
271        }
272        return -1;
273    }
274
275
276}
277