PageRenderTime 45ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 1ms

/core/java/android/os/UserManager.java

https://bitbucket.org/Lloir/basee
Java | 371 lines | 167 code | 29 blank | 175 comment | 2 complexity | 461f86c1d3a4b054923a8825f2e04c45 MD5 | raw file
  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. */
  16. package android.os;
  17. import com.android.internal.R;
  18. import android.app.ActivityManagerNative;
  19. import android.content.Context;
  20. import android.content.pm.UserInfo;
  21. import android.graphics.Bitmap;
  22. import android.content.res.Resources;
  23. import android.util.Log;
  24. import java.util.List;
  25. /**
  26. * Manages users and user details on a multi-user system.
  27. */
  28. public class UserManager {
  29. private static String TAG = "UserManager";
  30. private final IUserManager mService;
  31. private final Context mContext;
  32. /** @hide */
  33. public UserManager(Context context, IUserManager service) {
  34. mService = service;
  35. mContext = context;
  36. }
  37. /**
  38. * Returns whether the system supports multiple users.
  39. * @return true if multiple users can be created, false if it is a single user device.
  40. * @hide
  41. */
  42. public static boolean supportsMultipleUsers() {
  43. return getMaxSupportedUsers() > 1;
  44. }
  45. /**
  46. * Returns the user handle for the user that this application is running for.
  47. * @return the user handle of the user making this call.
  48. * @hide
  49. * */
  50. public int getUserHandle() {
  51. return UserHandle.myUserId();
  52. }
  53. /**
  54. * Returns the user name of the user making this call. This call is only
  55. * available to applications on the system image; it requires the
  56. * MANAGE_USERS permission.
  57. * @return the user name
  58. */
  59. public String getUserName() {
  60. try {
  61. return mService.getUserInfo(getUserHandle()).name;
  62. } catch (RemoteException re) {
  63. Log.w(TAG, "Could not get user name", re);
  64. return "";
  65. }
  66. }
  67. /**
  68. * Used to determine whether the user making this call is subject to
  69. * teleportations.
  70. * @return whether the user making this call is a goat
  71. */
  72. public boolean isUserAGoat() {
  73. return false;
  74. }
  75. /**
  76. * Return whether the given user is actively running. This means that
  77. * the user is in the "started" state, not "stopped" -- it is currently
  78. * allowed to run code through scheduled alarms, receiving broadcasts,
  79. * etc. A started user may be either the current foreground user or a
  80. * background user; the result here does not distinguish between the two.
  81. * @param user The user to retrieve the running state for.
  82. */
  83. public boolean isUserRunning(UserHandle user) {
  84. try {
  85. return ActivityManagerNative.getDefault().isUserRunning(
  86. user.getIdentifier(), false);
  87. } catch (RemoteException e) {
  88. return false;
  89. }
  90. }
  91. /**
  92. * Return whether the given user is actively running <em>or</em> stopping.
  93. * This is like {@link #isUserRunning(UserHandle)}, but will also return
  94. * true if the user had been running but is in the process of being stopped
  95. * (but is not yet fully stopped, and still running some code).
  96. * @param user The user to retrieve the running state for.
  97. */
  98. public boolean isUserRunningOrStopping(UserHandle user) {
  99. try {
  100. return ActivityManagerNative.getDefault().isUserRunning(
  101. user.getIdentifier(), true);
  102. } catch (RemoteException e) {
  103. return false;
  104. }
  105. }
  106. /**
  107. * Returns the UserInfo object describing a specific user.
  108. * Requires {@link android.Manifest.permission#MANAGE_USERS} permission.
  109. * @param userHandle the user handle of the user whose information is being requested.
  110. * @return the UserInfo object for a specific user.
  111. * @hide
  112. */
  113. public UserInfo getUserInfo(int userHandle) {
  114. try {
  115. return mService.getUserInfo(userHandle);
  116. } catch (RemoteException re) {
  117. Log.w(TAG, "Could not get user info", re);
  118. return null;
  119. }
  120. }
  121. /**
  122. * Return the serial number for a user. This is a device-unique
  123. * number assigned to that user; if the user is deleted and then a new
  124. * user created, the new users will not be given the same serial number.
  125. * @param user The user whose serial number is to be retrieved.
  126. * @return The serial number of the given user; returns -1 if the
  127. * given UserHandle does not exist.
  128. * @see #getUserForSerialNumber(long)
  129. */
  130. public long getSerialNumberForUser(UserHandle user) {
  131. return getUserSerialNumber(user.getIdentifier());
  132. }
  133. /**
  134. * Return the user associated with a serial number previously
  135. * returned by {@link #getSerialNumberForUser(UserHandle)}.
  136. * @param serialNumber The serial number of the user that is being
  137. * retrieved.
  138. * @return Return the user associated with the serial number, or null
  139. * if there is not one.
  140. * @see #getSerialNumberForUser(UserHandle)
  141. */
  142. public UserHandle getUserForSerialNumber(long serialNumber) {
  143. int ident = getUserHandle((int)serialNumber);
  144. return ident >= 0 ? new UserHandle(ident) : null;
  145. }
  146. /**
  147. * Creates a user with the specified name and options.
  148. * Requires {@link android.Manifest.permission#MANAGE_USERS} permission.
  149. *
  150. * @param name the user's name
  151. * @param flags flags that identify the type of user and other properties.
  152. * @see UserInfo
  153. *
  154. * @return the UserInfo object for the created user, or null if the user could not be created.
  155. * @hide
  156. */
  157. public UserInfo createUser(String name, int flags) {
  158. try {
  159. return mService.createUser(name, flags);
  160. } catch (RemoteException re) {
  161. Log.w(TAG, "Could not create a user", re);
  162. return null;
  163. }
  164. }
  165. /**
  166. * Return the number of users currently created on the device.
  167. */
  168. public int getUserCount() {
  169. List<UserInfo> users = getUsers();
  170. return users != null ? users.size() : 1;
  171. }
  172. /**
  173. * Returns information for all users on this device.
  174. * Requires {@link android.Manifest.permission#MANAGE_USERS} permission.
  175. * @return the list of users that were created.
  176. * @hide
  177. */
  178. public List<UserInfo> getUsers() {
  179. try {
  180. return mService.getUsers(false);
  181. } catch (RemoteException re) {
  182. Log.w(TAG, "Could not get user list", re);
  183. return null;
  184. }
  185. }
  186. /**
  187. * Returns information for all users on this device.
  188. * Requires {@link android.Manifest.permission#MANAGE_USERS} permission.
  189. * @param excludeDying specify if the list should exclude users being removed.
  190. * @return the list of users that were created.
  191. * @hide
  192. */
  193. public List<UserInfo> getUsers(boolean excludeDying) {
  194. try {
  195. return mService.getUsers(excludeDying);
  196. } catch (RemoteException re) {
  197. Log.w(TAG, "Could not get user list", re);
  198. return null;
  199. }
  200. }
  201. /**
  202. * Removes a user and all associated data.
  203. * Requires {@link android.Manifest.permission#MANAGE_USERS} permission.
  204. * @param userHandle the integer handle of the user, where 0 is the primary user.
  205. * @hide
  206. */
  207. public boolean removeUser(int userHandle) {
  208. try {
  209. return mService.removeUser(userHandle);
  210. } catch (RemoteException re) {
  211. Log.w(TAG, "Could not remove user ", re);
  212. return false;
  213. }
  214. }
  215. /**
  216. * Updates the user's name.
  217. * Requires {@link android.Manifest.permission#MANAGE_USERS} permission.
  218. *
  219. * @param userHandle the user's integer handle
  220. * @param name the new name for the user
  221. * @hide
  222. */
  223. public void setUserName(int userHandle, String name) {
  224. try {
  225. mService.setUserName(userHandle, name);
  226. } catch (RemoteException re) {
  227. Log.w(TAG, "Could not set the user name ", re);
  228. }
  229. }
  230. /**
  231. * Sets the user's photo.
  232. * @param userHandle the user for whom to change the photo.
  233. * @param icon the bitmap to set as the photo.
  234. * @hide
  235. */
  236. public void setUserIcon(int userHandle, Bitmap icon) {
  237. try {
  238. mService.setUserIcon(userHandle, icon);
  239. } catch (RemoteException re) {
  240. Log.w(TAG, "Could not set the user icon ", re);
  241. }
  242. }
  243. /**
  244. * Returns a file descriptor for the user's photo. PNG data can be read from this file.
  245. * @param userHandle the user whose photo we want to read.
  246. * @return a {@link Bitmap} of the user's photo, or null if there's no photo.
  247. * @hide
  248. */
  249. public Bitmap getUserIcon(int userHandle) {
  250. try {
  251. return mService.getUserIcon(userHandle);
  252. } catch (RemoteException re) {
  253. Log.w(TAG, "Could not get the user icon ", re);
  254. return null;
  255. }
  256. }
  257. /**
  258. * Enable or disable the use of a guest account. If disabled, the existing guest account
  259. * will be wiped.
  260. * Requires {@link android.Manifest.permission#MANAGE_USERS} permission.
  261. * @param enable whether to enable a guest account.
  262. * @hide
  263. */
  264. public void setGuestEnabled(boolean enable) {
  265. try {
  266. mService.setGuestEnabled(enable);
  267. } catch (RemoteException re) {
  268. Log.w(TAG, "Could not change guest account availability to " + enable);
  269. }
  270. }
  271. /**
  272. * Checks if a guest user is enabled for this device.
  273. * Requires {@link android.Manifest.permission#MANAGE_USERS} permission.
  274. * @return whether a guest user is enabled
  275. * @hide
  276. */
  277. public boolean isGuestEnabled() {
  278. try {
  279. return mService.isGuestEnabled();
  280. } catch (RemoteException re) {
  281. Log.w(TAG, "Could not retrieve guest enabled state");
  282. return false;
  283. }
  284. }
  285. /**
  286. * Wipes all the data for a user, but doesn't remove the user.
  287. * Requires {@link android.Manifest.permission#MANAGE_USERS} permission.
  288. * @param userHandle
  289. * @hide
  290. */
  291. public void wipeUser(int userHandle) {
  292. try {
  293. mService.wipeUser(userHandle);
  294. } catch (RemoteException re) {
  295. Log.w(TAG, "Could not wipe user " + userHandle);
  296. }
  297. }
  298. /**
  299. * Returns the maximum number of users that can be created on this device. A return value
  300. * of 1 means that it is a single user device.
  301. * @hide
  302. * @return a value greater than or equal to 1
  303. */
  304. public static int getMaxSupportedUsers() {
  305. // Don't allow multiple users on certain builds
  306. if (android.os.Build.ID.startsWith("JVP")) return 1;
  307. return SystemProperties.getInt("fw.max_users",
  308. Resources.getSystem().getInteger(R.integer.config_multiuserMaximumUsers));
  309. }
  310. /**
  311. * Returns a serial number on this device for a given userHandle. User handles can be recycled
  312. * when deleting and creating users, but serial numbers are not reused until the device is wiped.
  313. * @param userHandle
  314. * @return a serial number associated with that user, or -1 if the userHandle is not valid.
  315. * @hide
  316. */
  317. public int getUserSerialNumber(int userHandle) {
  318. try {
  319. return mService.getUserSerialNumber(userHandle);
  320. } catch (RemoteException re) {
  321. Log.w(TAG, "Could not get serial number for user " + userHandle);
  322. }
  323. return -1;
  324. }
  325. /**
  326. * Returns a userHandle on this device for a given user serial number. User handles can be
  327. * recycled when deleting and creating users, but serial numbers are not reused until the device
  328. * is wiped.
  329. * @param userSerialNumber
  330. * @return the userHandle associated with that user serial number, or -1 if the serial number
  331. * is not valid.
  332. * @hide
  333. */
  334. public int getUserHandle(int userSerialNumber) {
  335. try {
  336. return mService.getUserHandle(userSerialNumber);
  337. } catch (RemoteException re) {
  338. Log.w(TAG, "Could not get userHandle for user " + userSerialNumber);
  339. }
  340. return -1;
  341. }
  342. }