blob: dbfe34d3e96b571d16ac2a533d86bb42a97d9712 [file] [log] [blame]
Amith Yamasani4b2e9342011-03-31 12:38:53 -07001/*
2 * Copyright (C) 2011 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
17package com.android.server.pm;
18
Jeff Sharkeyffe0cb42012-11-05 17:24:43 -080019import static android.text.format.DateUtils.MINUTE_IN_MILLIS;
Amith Yamasani4b2e9342011-03-31 12:38:53 -070020
Amith Yamasanidb6a14c2012-10-17 21:16:52 -070021import android.app.Activity;
Amith Yamasani2a003292012-08-14 18:25:45 -070022import android.app.ActivityManager;
Dianne Hackborn80a4af22012-08-27 19:18:31 -070023import android.app.ActivityManagerNative;
24import android.app.IStopUserCallback;
Amith Yamasanidb6a14c2012-10-17 21:16:52 -070025import android.content.BroadcastReceiver;
Amith Yamasani258848d2012-08-10 17:06:33 -070026import android.content.Context;
27import android.content.Intent;
Amith Yamasani0b285492011-04-14 17:35:23 -070028import android.content.pm.PackageManager;
Amith Yamasani4b2e9342011-03-31 12:38:53 -070029import android.content.pm.UserInfo;
Amith Yamasanie928d7d2012-09-17 21:46:51 -070030import android.graphics.Bitmap;
31import android.graphics.BitmapFactory;
Amith Yamasani258848d2012-08-10 17:06:33 -070032import android.os.Binder;
Amith Yamasani4b2e9342011-03-31 12:38:53 -070033import android.os.Environment;
34import android.os.FileUtils;
Jeff Sharkeyffe0cb42012-11-05 17:24:43 -080035import android.os.Handler;
Amith Yamasani258848d2012-08-10 17:06:33 -070036import android.os.IUserManager;
Amith Yamasani258848d2012-08-10 17:06:33 -070037import android.os.Process;
Dianne Hackborn80a4af22012-08-27 19:18:31 -070038import android.os.RemoteException;
Dianne Hackbornf02b60a2012-08-16 10:48:27 -070039import android.os.UserHandle;
Jeff Sharkey27bd34d2012-09-16 12:49:00 -070040import android.os.UserManager;
Amith Yamasani2a003292012-08-14 18:25:45 -070041import android.util.AtomicFile;
Amith Yamasani4b2e9342011-03-31 12:38:53 -070042import android.util.Slog;
43import android.util.SparseArray;
Jeff Sharkeyffe0cb42012-11-05 17:24:43 -080044import android.util.SparseBooleanArray;
Amith Yamasani920ace02012-09-20 22:15:37 -070045import android.util.TimeUtils;
Amith Yamasani4b2e9342011-03-31 12:38:53 -070046import android.util.Xml;
47
Jeff Sharkeyffe0cb42012-11-05 17:24:43 -080048import com.android.internal.util.ArrayUtils;
49import com.android.internal.util.FastXmlSerializer;
50
51import org.xmlpull.v1.XmlPullParser;
52import org.xmlpull.v1.XmlPullParserException;
53import org.xmlpull.v1.XmlSerializer;
54
Amith Yamasani4b2e9342011-03-31 12:38:53 -070055import java.io.BufferedOutputStream;
56import java.io.File;
Amith Yamasani920ace02012-09-20 22:15:37 -070057import java.io.FileDescriptor;
Amith Yamasani4b2e9342011-03-31 12:38:53 -070058import java.io.FileInputStream;
Amith Yamasanib8151ec2012-04-18 18:02:48 -070059import java.io.FileNotFoundException;
Amith Yamasani4b2e9342011-03-31 12:38:53 -070060import java.io.FileOutputStream;
61import java.io.IOException;
Amith Yamasani920ace02012-09-20 22:15:37 -070062import java.io.PrintWriter;
Amith Yamasani4b2e9342011-03-31 12:38:53 -070063import java.util.ArrayList;
64import java.util.List;
65
Amith Yamasani258848d2012-08-10 17:06:33 -070066public class UserManagerService extends IUserManager.Stub {
Amith Yamasanib8151ec2012-04-18 18:02:48 -070067
Amith Yamasani2a003292012-08-14 18:25:45 -070068 private static final String LOG_TAG = "UserManagerService";
Amith Yamasanib8151ec2012-04-18 18:02:48 -070069
Amith Yamasani16389312012-10-17 21:20:14 -070070 private static final boolean DBG = false;
71
Amith Yamasani4b2e9342011-03-31 12:38:53 -070072 private static final String TAG_NAME = "name";
Amith Yamasani4b2e9342011-03-31 12:38:53 -070073 private static final String ATTR_FLAGS = "flags";
Amith Yamasanib8151ec2012-04-18 18:02:48 -070074 private static final String ATTR_ICON_PATH = "icon";
Amith Yamasani4b2e9342011-03-31 12:38:53 -070075 private static final String ATTR_ID = "id";
Amith Yamasani920ace02012-09-20 22:15:37 -070076 private static final String ATTR_CREATION_TIME = "created";
77 private static final String ATTR_LAST_LOGGED_IN_TIME = "lastLoggedIn";
Amith Yamasani2a003292012-08-14 18:25:45 -070078 private static final String ATTR_SERIAL_NO = "serialNumber";
79 private static final String ATTR_NEXT_SERIAL_NO = "nextSerialNumber";
Dianne Hackbornd4ac8d72012-09-27 23:20:10 -070080 private static final String ATTR_PARTIAL = "partial";
Amith Yamasani6f34b412012-10-22 18:19:27 -070081 private static final String ATTR_USER_VERSION = "version";
Amith Yamasani4b2e9342011-03-31 12:38:53 -070082 private static final String TAG_USERS = "users";
Amith Yamasani4b2e9342011-03-31 12:38:53 -070083 private static final String TAG_USER = "user";
84
Amith Yamasani0b285492011-04-14 17:35:23 -070085 private static final String USER_INFO_DIR = "system" + File.separator + "users";
Amith Yamasani4b2e9342011-03-31 12:38:53 -070086 private static final String USER_LIST_FILENAME = "userlist.xml";
Amith Yamasanib8151ec2012-04-18 18:02:48 -070087 private static final String USER_PHOTO_FILENAME = "photo.png";
Amith Yamasani4b2e9342011-03-31 12:38:53 -070088
Amith Yamasani634cf312012-10-04 17:34:21 -070089 private static final int MIN_USER_ID = 10;
90
Amith Yamasanibc9625052012-11-15 14:39:18 -080091 private static final int USER_VERSION = 2;
Amith Yamasani6f34b412012-10-22 18:19:27 -070092
Amith Yamasani920ace02012-09-20 22:15:37 -070093 private static final long EPOCH_PLUS_30_YEARS = 30L * 365 * 24 * 60 * 60 * 1000L; // ms
94
Dianne Hackborn4428e172012-08-24 17:43:05 -070095 private final Context mContext;
96 private final PackageManagerService mPm;
97 private final Object mInstallLock;
98 private final Object mPackagesLock;
Amith Yamasani4b2e9342011-03-31 12:38:53 -070099
Jeff Sharkeyffe0cb42012-11-05 17:24:43 -0800100 private final Handler mHandler;
101
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700102 private final File mUsersDir;
103 private final File mUserListFile;
Dianne Hackborn4428e172012-08-24 17:43:05 -0700104 private final File mBaseUserPath;
105
Jeff Sharkeyffe0cb42012-11-05 17:24:43 -0800106 private final SparseArray<UserInfo> mUsers = new SparseArray<UserInfo>();
107
108 /**
109 * Set of user IDs being actively removed. Removed IDs linger in this set
110 * for several seconds to work around a VFS caching issue.
111 */
112 // @GuardedBy("mPackagesLock")
113 private final SparseBooleanArray mRemovingUserIds = new SparseBooleanArray();
Dianne Hackborn4428e172012-08-24 17:43:05 -0700114
Amith Yamasani0b285492011-04-14 17:35:23 -0700115 private int[] mUserIds;
Amith Yamasani258848d2012-08-10 17:06:33 -0700116 private boolean mGuestEnabled;
Amith Yamasani2a003292012-08-14 18:25:45 -0700117 private int mNextSerialNumber;
Amith Yamasani6f34b412012-10-22 18:19:27 -0700118 private int mUserVersion = 0;
Amith Yamasani0b285492011-04-14 17:35:23 -0700119
Amith Yamasani258848d2012-08-10 17:06:33 -0700120 private static UserManagerService sInstance;
Amith Yamasani258848d2012-08-10 17:06:33 -0700121
Dianne Hackborn4428e172012-08-24 17:43:05 -0700122 public static UserManagerService getInstance() {
123 synchronized (UserManagerService.class) {
124 return sInstance;
Amith Yamasani258848d2012-08-10 17:06:33 -0700125 }
Amith Yamasani258848d2012-08-10 17:06:33 -0700126 }
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700127
128 /**
129 * Available for testing purposes.
130 */
Amith Yamasani258848d2012-08-10 17:06:33 -0700131 UserManagerService(File dataDir, File baseUserPath) {
Dianne Hackborn4428e172012-08-24 17:43:05 -0700132 this(null, null, new Object(), new Object(), dataDir, baseUserPath);
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700133 }
134
Dianne Hackborn4428e172012-08-24 17:43:05 -0700135 /**
136 * Called by package manager to create the service. This is closely
137 * associated with the package manager, and the given lock is the
138 * package manager's own lock.
139 */
140 UserManagerService(Context context, PackageManagerService pm,
141 Object installLock, Object packagesLock) {
142 this(context, pm, installLock, packagesLock,
143 Environment.getDataDirectory(),
144 new File(Environment.getDataDirectory(), "user"));
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700145 }
146
Dianne Hackborn4428e172012-08-24 17:43:05 -0700147 /**
148 * Available for testing purposes.
149 */
150 private UserManagerService(Context context, PackageManagerService pm,
151 Object installLock, Object packagesLock,
152 File dataDir, File baseUserPath) {
Dianne Hackbornd4ac8d72012-09-27 23:20:10 -0700153 mContext = context;
154 mPm = pm;
155 mInstallLock = installLock;
156 mPackagesLock = packagesLock;
Jeff Sharkeyffe0cb42012-11-05 17:24:43 -0800157 mHandler = new Handler();
Dianne Hackbornd4ac8d72012-09-27 23:20:10 -0700158 synchronized (mInstallLock) {
159 synchronized (mPackagesLock) {
160 mUsersDir = new File(dataDir, USER_INFO_DIR);
161 mUsersDir.mkdirs();
162 // Make zeroth user directory, for services to migrate their files to that location
163 File userZeroDir = new File(mUsersDir, "0");
164 userZeroDir.mkdirs();
165 mBaseUserPath = baseUserPath;
166 FileUtils.setPermissions(mUsersDir.toString(),
167 FileUtils.S_IRWXU|FileUtils.S_IRWXG
168 |FileUtils.S_IROTH|FileUtils.S_IXOTH,
169 -1, -1);
170 mUserListFile = new File(mUsersDir, USER_LIST_FILENAME);
171 readUserListLocked();
Amith Yamasani756901d2012-10-12 12:30:07 -0700172 // Prune out any partially created/partially removed users.
Dianne Hackbornd4ac8d72012-09-27 23:20:10 -0700173 ArrayList<UserInfo> partials = new ArrayList<UserInfo>();
174 for (int i = 0; i < mUsers.size(); i++) {
175 UserInfo ui = mUsers.valueAt(i);
176 if (ui.partial && i != 0) {
177 partials.add(ui);
178 }
179 }
180 for (int i = 0; i < partials.size(); i++) {
181 UserInfo ui = partials.get(i);
182 Slog.w(LOG_TAG, "Removing partially created user #" + i
183 + " (name=" + ui.name + ")");
184 removeUserStateLocked(ui.id);
185 }
186 sInstance = this;
187 }
Dianne Hackborn4428e172012-08-24 17:43:05 -0700188 }
Amith Yamasani258848d2012-08-10 17:06:33 -0700189 }
190
191 @Override
Amith Yamasani920ace02012-09-20 22:15:37 -0700192 public List<UserInfo> getUsers(boolean excludeDying) {
Amith Yamasani2a003292012-08-14 18:25:45 -0700193 checkManageUsersPermission("query users");
Dianne Hackborn4428e172012-08-24 17:43:05 -0700194 synchronized (mPackagesLock) {
Amith Yamasani13593602012-03-22 16:16:17 -0700195 ArrayList<UserInfo> users = new ArrayList<UserInfo>(mUsers.size());
196 for (int i = 0; i < mUsers.size(); i++) {
Dianne Hackbornd4ac8d72012-09-27 23:20:10 -0700197 UserInfo ui = mUsers.valueAt(i);
198 if (ui.partial) {
199 continue;
200 }
Jeff Sharkeyffe0cb42012-11-05 17:24:43 -0800201 if (!excludeDying || !mRemovingUserIds.get(ui.id)) {
Dianne Hackbornd4ac8d72012-09-27 23:20:10 -0700202 users.add(ui);
Amith Yamasani920ace02012-09-20 22:15:37 -0700203 }
Amith Yamasani13593602012-03-22 16:16:17 -0700204 }
205 return users;
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700206 }
Amith Yamasani13593602012-03-22 16:16:17 -0700207 }
208
Amith Yamasani258848d2012-08-10 17:06:33 -0700209 @Override
210 public UserInfo getUserInfo(int userId) {
Amith Yamasani2a003292012-08-14 18:25:45 -0700211 checkManageUsersPermission("query user");
Dianne Hackborn4428e172012-08-24 17:43:05 -0700212 synchronized (mPackagesLock) {
Amith Yamasani195263742012-08-21 15:40:12 -0700213 return getUserInfoLocked(userId);
Amith Yamasani13593602012-03-22 16:16:17 -0700214 }
215 }
216
Amith Yamasani195263742012-08-21 15:40:12 -0700217 /*
218 * Should be locked on mUsers before calling this.
219 */
220 private UserInfo getUserInfoLocked(int userId) {
Dianne Hackbornd4ac8d72012-09-27 23:20:10 -0700221 UserInfo ui = mUsers.get(userId);
Amith Yamasani16389312012-10-17 21:20:14 -0700222 // If it is partial and not in the process of being removed, return as unknown user.
Jeff Sharkeyffe0cb42012-11-05 17:24:43 -0800223 if (ui != null && ui.partial && !mRemovingUserIds.get(userId)) {
Dianne Hackbornd4ac8d72012-09-27 23:20:10 -0700224 Slog.w(LOG_TAG, "getUserInfo: unknown user #" + userId);
225 return null;
226 }
227 return ui;
Amith Yamasani195263742012-08-21 15:40:12 -0700228 }
229
Amith Yamasani13593602012-03-22 16:16:17 -0700230 public boolean exists(int userId) {
Dianne Hackborn4428e172012-08-24 17:43:05 -0700231 synchronized (mPackagesLock) {
Amith Yamasani13593602012-03-22 16:16:17 -0700232 return ArrayUtils.contains(mUserIds, userId);
233 }
234 }
235
Amith Yamasani258848d2012-08-10 17:06:33 -0700236 @Override
Amith Yamasanib8151ec2012-04-18 18:02:48 -0700237 public void setUserName(int userId, String name) {
Amith Yamasani2a003292012-08-14 18:25:45 -0700238 checkManageUsersPermission("rename users");
Dianne Hackbornd4ac8d72012-09-27 23:20:10 -0700239 boolean changed = false;
Dianne Hackborn4428e172012-08-24 17:43:05 -0700240 synchronized (mPackagesLock) {
Amith Yamasani13593602012-03-22 16:16:17 -0700241 UserInfo info = mUsers.get(userId);
Dianne Hackbornd4ac8d72012-09-27 23:20:10 -0700242 if (info == null || info.partial) {
243 Slog.w(LOG_TAG, "setUserName: unknown user #" + userId);
244 return;
245 }
Amith Yamasani13593602012-03-22 16:16:17 -0700246 if (name != null && !name.equals(info.name)) {
247 info.name = name;
248 writeUserLocked(info);
Dianne Hackbornd4ac8d72012-09-27 23:20:10 -0700249 changed = true;
Amith Yamasani13593602012-03-22 16:16:17 -0700250 }
251 }
Dianne Hackbornd4ac8d72012-09-27 23:20:10 -0700252 if (changed) {
253 sendUserInfoChangedBroadcast(userId);
254 }
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700255 }
256
Amith Yamasani258848d2012-08-10 17:06:33 -0700257 @Override
Amith Yamasanie928d7d2012-09-17 21:46:51 -0700258 public void setUserIcon(int userId, Bitmap bitmap) {
Amith Yamasani2a003292012-08-14 18:25:45 -0700259 checkManageUsersPermission("update users");
Dianne Hackborn4428e172012-08-24 17:43:05 -0700260 synchronized (mPackagesLock) {
Amith Yamasanib8151ec2012-04-18 18:02:48 -0700261 UserInfo info = mUsers.get(userId);
Dianne Hackbornd4ac8d72012-09-27 23:20:10 -0700262 if (info == null || info.partial) {
263 Slog.w(LOG_TAG, "setUserIcon: unknown user #" + userId);
264 return;
265 }
Amith Yamasanie928d7d2012-09-17 21:46:51 -0700266 writeBitmapLocked(info, bitmap);
267 writeUserLocked(info);
Amith Yamasanib8151ec2012-04-18 18:02:48 -0700268 }
Amith Yamasanie928d7d2012-09-17 21:46:51 -0700269 sendUserInfoChangedBroadcast(userId);
270 }
271
272 private void sendUserInfoChangedBroadcast(int userId) {
273 Intent changedIntent = new Intent(Intent.ACTION_USER_INFO_CHANGED);
274 changedIntent.putExtra(Intent.EXTRA_USER_HANDLE, userId);
275 changedIntent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY);
276 mContext.sendBroadcastAsUser(changedIntent, new UserHandle(userId));
Amith Yamasanib8151ec2012-04-18 18:02:48 -0700277 }
278
Amith Yamasani258848d2012-08-10 17:06:33 -0700279 @Override
Amith Yamasanie928d7d2012-09-17 21:46:51 -0700280 public Bitmap getUserIcon(int userId) {
Amith Yamasani3b49f072012-09-17 10:21:43 -0700281 checkManageUsersPermission("read users");
282 synchronized (mPackagesLock) {
283 UserInfo info = mUsers.get(userId);
Dianne Hackbornd4ac8d72012-09-27 23:20:10 -0700284 if (info == null || info.partial) {
285 Slog.w(LOG_TAG, "getUserIcon: unknown user #" + userId);
286 return null;
287 }
288 if (info.iconPath == null) {
289 return null;
290 }
Amith Yamasanie928d7d2012-09-17 21:46:51 -0700291 return BitmapFactory.decodeFile(info.iconPath);
Amith Yamasani3b49f072012-09-17 10:21:43 -0700292 }
293 }
294
295 @Override
Amith Yamasani258848d2012-08-10 17:06:33 -0700296 public void setGuestEnabled(boolean enable) {
Amith Yamasani2a003292012-08-14 18:25:45 -0700297 checkManageUsersPermission("enable guest users");
Dianne Hackborn4428e172012-08-24 17:43:05 -0700298 synchronized (mPackagesLock) {
Amith Yamasani258848d2012-08-10 17:06:33 -0700299 if (mGuestEnabled != enable) {
300 mGuestEnabled = enable;
301 // Erase any guest user that currently exists
302 for (int i = 0; i < mUsers.size(); i++) {
303 UserInfo user = mUsers.valueAt(i);
Dianne Hackbornd4ac8d72012-09-27 23:20:10 -0700304 if (!user.partial && user.isGuest()) {
Amith Yamasani258848d2012-08-10 17:06:33 -0700305 if (!enable) {
306 removeUser(user.id);
307 }
308 return;
309 }
310 }
311 // No guest was found
312 if (enable) {
313 createUser("Guest", UserInfo.FLAG_GUEST);
314 }
315 }
316 }
317 }
318
319 @Override
320 public boolean isGuestEnabled() {
Dianne Hackborn4428e172012-08-24 17:43:05 -0700321 synchronized (mPackagesLock) {
Amith Yamasani258848d2012-08-10 17:06:33 -0700322 return mGuestEnabled;
323 }
324 }
325
326 @Override
327 public void wipeUser(int userHandle) {
Amith Yamasani2a003292012-08-14 18:25:45 -0700328 checkManageUsersPermission("wipe user");
Amith Yamasani258848d2012-08-10 17:06:33 -0700329 // TODO:
330 }
331
Dianne Hackborn5dc5a002012-09-15 19:33:48 -0700332 public void makeInitialized(int userId) {
333 checkManageUsersPermission("makeInitialized");
334 synchronized (mPackagesLock) {
335 UserInfo info = mUsers.get(userId);
Dianne Hackbornd4ac8d72012-09-27 23:20:10 -0700336 if (info == null || info.partial) {
337 Slog.w(LOG_TAG, "makeInitialized: unknown user #" + userId);
338 }
339 if ((info.flags&UserInfo.FLAG_INITIALIZED) == 0) {
Dianne Hackborn5dc5a002012-09-15 19:33:48 -0700340 info.flags |= UserInfo.FLAG_INITIALIZED;
341 writeUserLocked(info);
342 }
343 }
344 }
345
Amith Yamasani258848d2012-08-10 17:06:33 -0700346 /**
Amith Yamasanifaea76f2012-09-11 10:59:48 -0700347 * Check if we've hit the limit of how many users can be created.
348 */
Dianne Hackborn5dc5a002012-09-15 19:33:48 -0700349 private boolean isUserLimitReachedLocked() {
350 int nUsers = mUsers.size();
Jeff Sharkey27bd34d2012-09-16 12:49:00 -0700351 return nUsers >= UserManager.getMaxSupportedUsers();
Amith Yamasanifaea76f2012-09-11 10:59:48 -0700352 }
353
354 /**
Amith Yamasani195263742012-08-21 15:40:12 -0700355 * Enforces that only the system UID or root's UID or apps that have the
356 * {@link android.Manifest.permission.MANAGE_USERS MANAGE_USERS}
357 * permission can make certain calls to the UserManager.
Amith Yamasani258848d2012-08-10 17:06:33 -0700358 *
359 * @param message used as message if SecurityException is thrown
360 * @throws SecurityException if the caller is not system or root
361 */
Amith Yamasani2a003292012-08-14 18:25:45 -0700362 private static final void checkManageUsersPermission(String message) {
Amith Yamasani258848d2012-08-10 17:06:33 -0700363 final int uid = Binder.getCallingUid();
Amith Yamasani2a003292012-08-14 18:25:45 -0700364 if (uid != Process.SYSTEM_UID && uid != 0
365 && ActivityManager.checkComponentPermission(
366 android.Manifest.permission.MANAGE_USERS,
367 uid, -1, true) != PackageManager.PERMISSION_GRANTED) {
368 throw new SecurityException("You need MANAGE_USERS permission to: " + message);
Amith Yamasani258848d2012-08-10 17:06:33 -0700369 }
370 }
371
Amith Yamasanie928d7d2012-09-17 21:46:51 -0700372 private void writeBitmapLocked(UserInfo info, Bitmap bitmap) {
Amith Yamasanib8151ec2012-04-18 18:02:48 -0700373 try {
374 File dir = new File(mUsersDir, Integer.toString(info.id));
375 File file = new File(dir, USER_PHOTO_FILENAME);
376 if (!dir.exists()) {
377 dir.mkdir();
378 FileUtils.setPermissions(
379 dir.getPath(),
380 FileUtils.S_IRWXU|FileUtils.S_IRWXG|FileUtils.S_IXOTH,
381 -1, -1);
382 }
Amith Yamasanie928d7d2012-09-17 21:46:51 -0700383 FileOutputStream os;
384 if (bitmap.compress(Bitmap.CompressFormat.PNG, 100, os = new FileOutputStream(file))) {
Amith Yamasani3b49f072012-09-17 10:21:43 -0700385 info.iconPath = file.getAbsolutePath();
386 }
Amith Yamasanie928d7d2012-09-17 21:46:51 -0700387 try {
388 os.close();
389 } catch (IOException ioe) {
390 // What the ... !
391 }
Amith Yamasanib8151ec2012-04-18 18:02:48 -0700392 } catch (FileNotFoundException e) {
Amith Yamasani2a003292012-08-14 18:25:45 -0700393 Slog.w(LOG_TAG, "Error setting photo for user ", e);
Amith Yamasanib8151ec2012-04-18 18:02:48 -0700394 }
Amith Yamasanib8151ec2012-04-18 18:02:48 -0700395 }
396
Amith Yamasani0b285492011-04-14 17:35:23 -0700397 /**
398 * Returns an array of user ids. This array is cached here for quick access, so do not modify or
399 * cache it elsewhere.
400 * @return the array of user ids.
401 */
Dianne Hackborn1676c852012-09-10 14:52:30 -0700402 public int[] getUserIds() {
Dianne Hackborn4428e172012-08-24 17:43:05 -0700403 synchronized (mPackagesLock) {
Dianne Hackborn7767eac2012-08-23 18:25:40 -0700404 return mUserIds;
405 }
Amith Yamasani0b285492011-04-14 17:35:23 -0700406 }
407
Dianne Hackborn4428e172012-08-24 17:43:05 -0700408 int[] getUserIdsLPr() {
409 return mUserIds;
410 }
411
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700412 private void readUserList() {
Dianne Hackborn4428e172012-08-24 17:43:05 -0700413 synchronized (mPackagesLock) {
Amith Yamasani13593602012-03-22 16:16:17 -0700414 readUserListLocked();
415 }
416 }
417
418 private void readUserListLocked() {
Amith Yamasani258848d2012-08-10 17:06:33 -0700419 mGuestEnabled = false;
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700420 if (!mUserListFile.exists()) {
Amith Yamasani13593602012-03-22 16:16:17 -0700421 fallbackToSingleUserLocked();
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700422 return;
423 }
424 FileInputStream fis = null;
Amith Yamasani2a003292012-08-14 18:25:45 -0700425 AtomicFile userListFile = new AtomicFile(mUserListFile);
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700426 try {
Amith Yamasani2a003292012-08-14 18:25:45 -0700427 fis = userListFile.openRead();
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700428 XmlPullParser parser = Xml.newPullParser();
429 parser.setInput(fis, null);
430 int type;
431 while ((type = parser.next()) != XmlPullParser.START_TAG
432 && type != XmlPullParser.END_DOCUMENT) {
433 ;
434 }
435
436 if (type != XmlPullParser.START_TAG) {
Amith Yamasani0b285492011-04-14 17:35:23 -0700437 Slog.e(LOG_TAG, "Unable to read user list");
Amith Yamasani13593602012-03-22 16:16:17 -0700438 fallbackToSingleUserLocked();
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700439 return;
440 }
441
Amith Yamasani2a003292012-08-14 18:25:45 -0700442 mNextSerialNumber = -1;
443 if (parser.getName().equals(TAG_USERS)) {
444 String lastSerialNumber = parser.getAttributeValue(null, ATTR_NEXT_SERIAL_NO);
445 if (lastSerialNumber != null) {
446 mNextSerialNumber = Integer.parseInt(lastSerialNumber);
447 }
Amith Yamasani6f34b412012-10-22 18:19:27 -0700448 String versionNumber = parser.getAttributeValue(null, ATTR_USER_VERSION);
449 if (versionNumber != null) {
450 mUserVersion = Integer.parseInt(versionNumber);
451 }
Amith Yamasani2a003292012-08-14 18:25:45 -0700452 }
453
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700454 while ((type = parser.next()) != XmlPullParser.END_DOCUMENT) {
455 if (type == XmlPullParser.START_TAG && parser.getName().equals(TAG_USER)) {
456 String id = parser.getAttributeValue(null, ATTR_ID);
457 UserInfo user = readUser(Integer.parseInt(id));
Amith Yamasani6f34b412012-10-22 18:19:27 -0700458
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700459 if (user != null) {
460 mUsers.put(user.id, user);
Amith Yamasani2a003292012-08-14 18:25:45 -0700461 if (user.isGuest()) {
462 mGuestEnabled = true;
463 }
464 if (mNextSerialNumber < 0 || mNextSerialNumber <= user.id) {
465 mNextSerialNumber = user.id + 1;
466 }
Amith Yamasani258848d2012-08-10 17:06:33 -0700467 }
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700468 }
469 }
Amith Yamasani13593602012-03-22 16:16:17 -0700470 updateUserIdsLocked();
Amith Yamasani6f34b412012-10-22 18:19:27 -0700471 upgradeIfNecessary();
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700472 } catch (IOException ioe) {
Amith Yamasani13593602012-03-22 16:16:17 -0700473 fallbackToSingleUserLocked();
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700474 } catch (XmlPullParserException pe) {
Amith Yamasani13593602012-03-22 16:16:17 -0700475 fallbackToSingleUserLocked();
Dianne Hackbornbfd89b32011-12-15 18:22:54 -0800476 } finally {
477 if (fis != null) {
478 try {
479 fis.close();
480 } catch (IOException e) {
481 }
482 }
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700483 }
484 }
485
Amith Yamasani6f34b412012-10-22 18:19:27 -0700486 /**
Amith Yamasanibc9625052012-11-15 14:39:18 -0800487 * Upgrade steps between versions, either for fixing bugs or changing the data format.
Amith Yamasani6f34b412012-10-22 18:19:27 -0700488 */
489 private void upgradeIfNecessary() {
490 int userVersion = mUserVersion;
491 if (userVersion < 1) {
492 // Assign a proper name for the owner, if not initialized correctly before
493 UserInfo user = mUsers.get(UserHandle.USER_OWNER);
494 if ("Primary".equals(user.name)) {
495 user.name = mContext.getResources().getString(com.android.internal.R.string.owner_name);
496 writeUserLocked(user);
497 }
498 userVersion = 1;
499 }
500
Amith Yamasanibc9625052012-11-15 14:39:18 -0800501 if (userVersion < 2) {
502 // Owner should be marked as initialized
503 UserInfo user = mUsers.get(UserHandle.USER_OWNER);
504 if ((user.flags & UserInfo.FLAG_INITIALIZED) == 0) {
505 user.flags |= UserInfo.FLAG_INITIALIZED;
506 writeUserLocked(user);
507 }
508 userVersion = 2;
509 }
510
Amith Yamasani6f34b412012-10-22 18:19:27 -0700511 if (userVersion < USER_VERSION) {
512 Slog.w(LOG_TAG, "User version " + mUserVersion + " didn't upgrade as expected to "
513 + USER_VERSION);
514 } else {
515 mUserVersion = userVersion;
516 writeUserListLocked();
517 }
518 }
519
Amith Yamasani13593602012-03-22 16:16:17 -0700520 private void fallbackToSingleUserLocked() {
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700521 // Create the primary user
Jeff Sharkeyffe0cb42012-11-05 17:24:43 -0800522 UserInfo primary = new UserInfo(0,
Amith Yamasani6f34b412012-10-22 18:19:27 -0700523 mContext.getResources().getString(com.android.internal.R.string.owner_name), null,
Amith Yamasani756901d2012-10-12 12:30:07 -0700524 UserInfo.FLAG_ADMIN | UserInfo.FLAG_PRIMARY | UserInfo.FLAG_INITIALIZED);
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700525 mUsers.put(0, primary);
Amith Yamasani634cf312012-10-04 17:34:21 -0700526 mNextSerialNumber = MIN_USER_ID;
Amith Yamasani13593602012-03-22 16:16:17 -0700527 updateUserIdsLocked();
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700528
Amith Yamasani13593602012-03-22 16:16:17 -0700529 writeUserListLocked();
530 writeUserLocked(primary);
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700531 }
532
533 /*
534 * Writes the user file in this format:
535 *
536 * <user flags="20039023" id="0">
537 * <name>Primary</name>
538 * </user>
539 */
Amith Yamasani13593602012-03-22 16:16:17 -0700540 private void writeUserLocked(UserInfo userInfo) {
Amith Yamasani742a6712011-05-04 14:49:28 -0700541 FileOutputStream fos = null;
Amith Yamasani2a003292012-08-14 18:25:45 -0700542 AtomicFile userFile = new AtomicFile(new File(mUsersDir, userInfo.id + ".xml"));
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700543 try {
Amith Yamasani2a003292012-08-14 18:25:45 -0700544 fos = userFile.startWrite();
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700545 final BufferedOutputStream bos = new BufferedOutputStream(fos);
546
547 // XmlSerializer serializer = XmlUtils.serializerInstance();
548 final XmlSerializer serializer = new FastXmlSerializer();
549 serializer.setOutput(bos, "utf-8");
550 serializer.startDocument(null, true);
551 serializer.setFeature("http://xmlpull.org/v1/doc/features.html#indent-output", true);
552
553 serializer.startTag(null, TAG_USER);
554 serializer.attribute(null, ATTR_ID, Integer.toString(userInfo.id));
Amith Yamasani2a003292012-08-14 18:25:45 -0700555 serializer.attribute(null, ATTR_SERIAL_NO, Integer.toString(userInfo.serialNumber));
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700556 serializer.attribute(null, ATTR_FLAGS, Integer.toString(userInfo.flags));
Amith Yamasani920ace02012-09-20 22:15:37 -0700557 serializer.attribute(null, ATTR_CREATION_TIME, Long.toString(userInfo.creationTime));
558 serializer.attribute(null, ATTR_LAST_LOGGED_IN_TIME,
559 Long.toString(userInfo.lastLoggedInTime));
Amith Yamasanib8151ec2012-04-18 18:02:48 -0700560 if (userInfo.iconPath != null) {
561 serializer.attribute(null, ATTR_ICON_PATH, userInfo.iconPath);
562 }
Dianne Hackbornd4ac8d72012-09-27 23:20:10 -0700563 if (userInfo.partial) {
564 serializer.attribute(null, ATTR_PARTIAL, "true");
565 }
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700566
567 serializer.startTag(null, TAG_NAME);
568 serializer.text(userInfo.name);
569 serializer.endTag(null, TAG_NAME);
570
571 serializer.endTag(null, TAG_USER);
572
573 serializer.endDocument();
Amith Yamasani2a003292012-08-14 18:25:45 -0700574 userFile.finishWrite(fos);
575 } catch (Exception ioe) {
Amith Yamasani0b285492011-04-14 17:35:23 -0700576 Slog.e(LOG_TAG, "Error writing user info " + userInfo.id + "\n" + ioe);
Amith Yamasani2a003292012-08-14 18:25:45 -0700577 userFile.failWrite(fos);
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700578 }
579 }
580
581 /*
582 * Writes the user list file in this format:
583 *
Amith Yamasani2a003292012-08-14 18:25:45 -0700584 * <users nextSerialNumber="3">
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700585 * <user id="0"></user>
586 * <user id="2"></user>
587 * </users>
588 */
Amith Yamasani13593602012-03-22 16:16:17 -0700589 private void writeUserListLocked() {
Amith Yamasani742a6712011-05-04 14:49:28 -0700590 FileOutputStream fos = null;
Amith Yamasani2a003292012-08-14 18:25:45 -0700591 AtomicFile userListFile = new AtomicFile(mUserListFile);
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700592 try {
Amith Yamasani2a003292012-08-14 18:25:45 -0700593 fos = userListFile.startWrite();
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700594 final BufferedOutputStream bos = new BufferedOutputStream(fos);
595
596 // XmlSerializer serializer = XmlUtils.serializerInstance();
597 final XmlSerializer serializer = new FastXmlSerializer();
598 serializer.setOutput(bos, "utf-8");
599 serializer.startDocument(null, true);
600 serializer.setFeature("http://xmlpull.org/v1/doc/features.html#indent-output", true);
601
602 serializer.startTag(null, TAG_USERS);
Amith Yamasani2a003292012-08-14 18:25:45 -0700603 serializer.attribute(null, ATTR_NEXT_SERIAL_NO, Integer.toString(mNextSerialNumber));
Amith Yamasani6f34b412012-10-22 18:19:27 -0700604 serializer.attribute(null, ATTR_USER_VERSION, Integer.toString(mUserVersion));
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700605
606 for (int i = 0; i < mUsers.size(); i++) {
607 UserInfo user = mUsers.valueAt(i);
608 serializer.startTag(null, TAG_USER);
609 serializer.attribute(null, ATTR_ID, Integer.toString(user.id));
610 serializer.endTag(null, TAG_USER);
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700611 }
612
613 serializer.endTag(null, TAG_USERS);
614
615 serializer.endDocument();
Amith Yamasani2a003292012-08-14 18:25:45 -0700616 userListFile.finishWrite(fos);
617 } catch (Exception e) {
618 userListFile.failWrite(fos);
Amith Yamasani0b285492011-04-14 17:35:23 -0700619 Slog.e(LOG_TAG, "Error writing user list");
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700620 }
621 }
622
623 private UserInfo readUser(int id) {
624 int flags = 0;
Amith Yamasani2a003292012-08-14 18:25:45 -0700625 int serialNumber = id;
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700626 String name = null;
Amith Yamasanib8151ec2012-04-18 18:02:48 -0700627 String iconPath = null;
Amith Yamasani920ace02012-09-20 22:15:37 -0700628 long creationTime = 0L;
629 long lastLoggedInTime = 0L;
Dianne Hackbornd4ac8d72012-09-27 23:20:10 -0700630 boolean partial = false;
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700631
632 FileInputStream fis = null;
633 try {
Amith Yamasani2a003292012-08-14 18:25:45 -0700634 AtomicFile userFile =
635 new AtomicFile(new File(mUsersDir, Integer.toString(id) + ".xml"));
636 fis = userFile.openRead();
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700637 XmlPullParser parser = Xml.newPullParser();
638 parser.setInput(fis, null);
639 int type;
640 while ((type = parser.next()) != XmlPullParser.START_TAG
641 && type != XmlPullParser.END_DOCUMENT) {
642 ;
643 }
644
645 if (type != XmlPullParser.START_TAG) {
Amith Yamasani0b285492011-04-14 17:35:23 -0700646 Slog.e(LOG_TAG, "Unable to read user " + id);
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700647 return null;
648 }
649
650 if (type == XmlPullParser.START_TAG && parser.getName().equals(TAG_USER)) {
Amith Yamasani920ace02012-09-20 22:15:37 -0700651 int storedId = readIntAttribute(parser, ATTR_ID, -1);
652 if (storedId != id) {
Amith Yamasani0b285492011-04-14 17:35:23 -0700653 Slog.e(LOG_TAG, "User id does not match the file name");
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700654 return null;
655 }
Amith Yamasani920ace02012-09-20 22:15:37 -0700656 serialNumber = readIntAttribute(parser, ATTR_SERIAL_NO, id);
657 flags = readIntAttribute(parser, ATTR_FLAGS, 0);
Amith Yamasanib8151ec2012-04-18 18:02:48 -0700658 iconPath = parser.getAttributeValue(null, ATTR_ICON_PATH);
Amith Yamasani920ace02012-09-20 22:15:37 -0700659 creationTime = readLongAttribute(parser, ATTR_CREATION_TIME, 0);
660 lastLoggedInTime = readLongAttribute(parser, ATTR_LAST_LOGGED_IN_TIME, 0);
Dianne Hackbornd4ac8d72012-09-27 23:20:10 -0700661 String valueString = parser.getAttributeValue(null, ATTR_PARTIAL);
662 if ("true".equals(valueString)) {
663 partial = true;
664 }
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700665
666 while ((type = parser.next()) != XmlPullParser.START_TAG
667 && type != XmlPullParser.END_DOCUMENT) {
668 }
669 if (type == XmlPullParser.START_TAG && parser.getName().equals(TAG_NAME)) {
670 type = parser.next();
671 if (type == XmlPullParser.TEXT) {
672 name = parser.getText();
673 }
674 }
675 }
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700676
Amith Yamasanib8151ec2012-04-18 18:02:48 -0700677 UserInfo userInfo = new UserInfo(id, name, iconPath, flags);
Amith Yamasani2a003292012-08-14 18:25:45 -0700678 userInfo.serialNumber = serialNumber;
Amith Yamasani920ace02012-09-20 22:15:37 -0700679 userInfo.creationTime = creationTime;
680 userInfo.lastLoggedInTime = lastLoggedInTime;
Dianne Hackbornd4ac8d72012-09-27 23:20:10 -0700681 userInfo.partial = partial;
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700682 return userInfo;
683
684 } catch (IOException ioe) {
685 } catch (XmlPullParserException pe) {
Dianne Hackbornbfd89b32011-12-15 18:22:54 -0800686 } finally {
687 if (fis != null) {
688 try {
689 fis.close();
690 } catch (IOException e) {
691 }
692 }
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700693 }
694 return null;
695 }
696
Amith Yamasani920ace02012-09-20 22:15:37 -0700697 private int readIntAttribute(XmlPullParser parser, String attr, int defaultValue) {
698 String valueString = parser.getAttributeValue(null, attr);
699 if (valueString == null) return defaultValue;
700 try {
701 return Integer.parseInt(valueString);
702 } catch (NumberFormatException nfe) {
703 return defaultValue;
704 }
705 }
706
707 private long readLongAttribute(XmlPullParser parser, String attr, long defaultValue) {
708 String valueString = parser.getAttributeValue(null, attr);
709 if (valueString == null) return defaultValue;
710 try {
711 return Long.parseLong(valueString);
712 } catch (NumberFormatException nfe) {
713 return defaultValue;
714 }
715 }
716
Amith Yamasani258848d2012-08-10 17:06:33 -0700717 @Override
Amith Yamasani13593602012-03-22 16:16:17 -0700718 public UserInfo createUser(String name, int flags) {
Amith Yamasani2a003292012-08-14 18:25:45 -0700719 checkManageUsersPermission("Only the system can create users");
Amith Yamasanifaea76f2012-09-11 10:59:48 -0700720
Dianne Hackborn5dc5a002012-09-15 19:33:48 -0700721 final long ident = Binder.clearCallingIdentity();
722 final UserInfo userInfo;
723 try {
724 synchronized (mInstallLock) {
725 synchronized (mPackagesLock) {
726 if (isUserLimitReachedLocked()) return null;
727 int userId = getNextAvailableIdLocked();
728 userInfo = new UserInfo(userId, name, null, flags);
729 File userPath = new File(mBaseUserPath, Integer.toString(userId));
730 userInfo.serialNumber = mNextSerialNumber++;
Amith Yamasani920ace02012-09-20 22:15:37 -0700731 long now = System.currentTimeMillis();
732 userInfo.creationTime = (now > EPOCH_PLUS_30_YEARS) ? now : 0;
Dianne Hackbornd4ac8d72012-09-27 23:20:10 -0700733 userInfo.partial = true;
Amith Yamasani16389312012-10-17 21:20:14 -0700734 Environment.getUserSystemDirectory(userInfo.id).mkdirs();
Dianne Hackborn5dc5a002012-09-15 19:33:48 -0700735 mUsers.put(userId, userInfo);
736 writeUserListLocked();
737 writeUserLocked(userInfo);
Dianne Hackborn5dc5a002012-09-15 19:33:48 -0700738 mPm.createNewUserLILPw(userId, userPath);
Dianne Hackbornd4ac8d72012-09-27 23:20:10 -0700739 userInfo.partial = false;
740 writeUserLocked(userInfo);
741 updateUserIdsLocked();
Dianne Hackborn5dc5a002012-09-15 19:33:48 -0700742 }
Dianne Hackborn4428e172012-08-24 17:43:05 -0700743 }
Dianne Hackborn5dc5a002012-09-15 19:33:48 -0700744 if (userInfo != null) {
745 Intent addedIntent = new Intent(Intent.ACTION_USER_ADDED);
746 addedIntent.putExtra(Intent.EXTRA_USER_HANDLE, userInfo.id);
747 mContext.sendBroadcastAsUser(addedIntent, UserHandle.ALL,
748 android.Manifest.permission.MANAGE_USERS);
749 }
750 } finally {
751 Binder.restoreCallingIdentity(ident);
Amith Yamasani258848d2012-08-10 17:06:33 -0700752 }
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700753 return userInfo;
754 }
755
Amith Yamasani0b285492011-04-14 17:35:23 -0700756 /**
757 * Removes a user and all data directories created for that user. This method should be called
758 * after the user's processes have been terminated.
759 * @param id the user's id
760 */
Amith Yamasani258848d2012-08-10 17:06:33 -0700761 public boolean removeUser(int userHandle) {
Amith Yamasani2a003292012-08-14 18:25:45 -0700762 checkManageUsersPermission("Only the system can remove users");
Dianne Hackborn80a4af22012-08-27 19:18:31 -0700763 final UserInfo user;
764 synchronized (mPackagesLock) {
765 user = mUsers.get(userHandle);
766 if (userHandle == 0 || user == null) {
767 return false;
768 }
Jeff Sharkeyffe0cb42012-11-05 17:24:43 -0800769 mRemovingUserIds.put(userHandle, true);
Amith Yamasani756901d2012-10-12 12:30:07 -0700770 // Set this to a partially created user, so that the user will be purged
771 // on next startup, in case the runtime stops now before stopping and
772 // removing the user completely.
773 user.partial = true;
774 writeUserLocked(user);
Dianne Hackborn80a4af22012-08-27 19:18:31 -0700775 }
Amith Yamasani16389312012-10-17 21:20:14 -0700776 if (DBG) Slog.i(LOG_TAG, "Stopping user " + userHandle);
Dianne Hackborn80a4af22012-08-27 19:18:31 -0700777 int res;
778 try {
779 res = ActivityManagerNative.getDefault().stopUser(userHandle,
780 new IStopUserCallback.Stub() {
781 @Override
782 public void userStopped(int userId) {
783 finishRemoveUser(userId);
784 }
785 @Override
786 public void userStopAborted(int userId) {
787 }
788 });
789 } catch (RemoteException e) {
790 return false;
791 }
792
793 return res == ActivityManager.USER_OP_SUCCESS;
794 }
795
Amith Yamasanidb6a14c2012-10-17 21:16:52 -0700796 void finishRemoveUser(final int userHandle) {
Amith Yamasani16389312012-10-17 21:20:14 -0700797 if (DBG) Slog.i(LOG_TAG, "finishRemoveUser " + userHandle);
Amith Yamasanidb6a14c2012-10-17 21:16:52 -0700798 // Let other services shutdown any activity and clean up their state before completely
799 // wiping the user's system directory and removing from the user list
Dianne Hackborn5dc5a002012-09-15 19:33:48 -0700800 long ident = Binder.clearCallingIdentity();
801 try {
802 Intent addedIntent = new Intent(Intent.ACTION_USER_REMOVED);
803 addedIntent.putExtra(Intent.EXTRA_USER_HANDLE, userHandle);
Amith Yamasanidb6a14c2012-10-17 21:16:52 -0700804 mContext.sendOrderedBroadcastAsUser(addedIntent, UserHandle.ALL,
805 android.Manifest.permission.MANAGE_USERS,
806
807 new BroadcastReceiver() {
808 @Override
809 public void onReceive(Context context, Intent intent) {
810 if (DBG) {
811 Slog.i(LOG_TAG,
812 "USER_REMOVED broadcast sent, cleaning up user data "
813 + userHandle);
814 }
815 new Thread() {
816 public void run() {
817 synchronized (mInstallLock) {
818 synchronized (mPackagesLock) {
819 removeUserStateLocked(userHandle);
820 }
821 }
822 }
823 }.start();
824 }
825 },
826
827 null, Activity.RESULT_OK, null, null);
Dianne Hackborn5dc5a002012-09-15 19:33:48 -0700828 } finally {
829 Binder.restoreCallingIdentity(ident);
830 }
Amith Yamasani2a003292012-08-14 18:25:45 -0700831 }
832
Jeff Sharkeyffe0cb42012-11-05 17:24:43 -0800833 private void removeUserStateLocked(final int userHandle) {
Dianne Hackbornd4ac8d72012-09-27 23:20:10 -0700834 // Cleanup package manager settings
835 mPm.cleanUpUserLILPw(userHandle);
836
837 // Remove this user from the list
838 mUsers.remove(userHandle);
Jeff Sharkeyffe0cb42012-11-05 17:24:43 -0800839
840 // Have user ID linger for several seconds to let external storage VFS
841 // cache entries expire. This must be greater than the 'entry_valid'
842 // timeout used by the FUSE daemon.
843 mHandler.postDelayed(new Runnable() {
844 @Override
845 public void run() {
846 synchronized (mPackagesLock) {
847 mRemovingUserIds.delete(userHandle);
848 }
849 }
850 }, MINUTE_IN_MILLIS);
851
Dianne Hackbornd4ac8d72012-09-27 23:20:10 -0700852 // Remove user file
853 AtomicFile userFile = new AtomicFile(new File(mUsersDir, userHandle + ".xml"));
854 userFile.delete();
855 // Update the user list
856 writeUserListLocked();
857 updateUserIdsLocked();
858 removeDirectoryRecursive(Environment.getUserSystemDirectory(userHandle));
859 }
860
Amith Yamasani61f57372012-08-31 12:12:28 -0700861 private void removeDirectoryRecursive(File parent) {
862 if (parent.isDirectory()) {
863 String[] files = parent.list();
864 for (String filename : files) {
865 File child = new File(parent, filename);
866 removeDirectoryRecursive(child);
867 }
868 }
869 parent.delete();
870 }
871
Amith Yamasani2a003292012-08-14 18:25:45 -0700872 @Override
873 public int getUserSerialNumber(int userHandle) {
Dianne Hackborn4428e172012-08-24 17:43:05 -0700874 synchronized (mPackagesLock) {
Amith Yamasani2a003292012-08-14 18:25:45 -0700875 if (!exists(userHandle)) return -1;
Amith Yamasani195263742012-08-21 15:40:12 -0700876 return getUserInfoLocked(userHandle).serialNumber;
Amith Yamasani2a003292012-08-14 18:25:45 -0700877 }
878 }
879
880 @Override
881 public int getUserHandle(int userSerialNumber) {
Dianne Hackborn4428e172012-08-24 17:43:05 -0700882 synchronized (mPackagesLock) {
Amith Yamasani2a003292012-08-14 18:25:45 -0700883 for (int userId : mUserIds) {
Amith Yamasani195263742012-08-21 15:40:12 -0700884 if (getUserInfoLocked(userId).serialNumber == userSerialNumber) return userId;
Amith Yamasani2a003292012-08-14 18:25:45 -0700885 }
886 // Not found
887 return -1;
Amith Yamasani13593602012-03-22 16:16:17 -0700888 }
889 }
890
Amith Yamasani0b285492011-04-14 17:35:23 -0700891 /**
892 * Caches the list of user ids in an array, adjusting the array size when necessary.
893 */
Amith Yamasani13593602012-03-22 16:16:17 -0700894 private void updateUserIdsLocked() {
Dianne Hackbornd4ac8d72012-09-27 23:20:10 -0700895 int num = 0;
Amith Yamasani0b285492011-04-14 17:35:23 -0700896 for (int i = 0; i < mUsers.size(); i++) {
Dianne Hackbornd4ac8d72012-09-27 23:20:10 -0700897 if (!mUsers.valueAt(i).partial) {
898 num++;
899 }
900 }
Amith Yamasani16389312012-10-17 21:20:14 -0700901 final int[] newUsers = new int[num];
902 int n = 0;
Dianne Hackbornd4ac8d72012-09-27 23:20:10 -0700903 for (int i = 0; i < mUsers.size(); i++) {
904 if (!mUsers.valueAt(i).partial) {
Amith Yamasani16389312012-10-17 21:20:14 -0700905 newUsers[n++] = mUsers.keyAt(i);
Dianne Hackbornd4ac8d72012-09-27 23:20:10 -0700906 }
Amith Yamasani0b285492011-04-14 17:35:23 -0700907 }
Dianne Hackborn7767eac2012-08-23 18:25:40 -0700908 mUserIds = newUsers;
Amith Yamasani0b285492011-04-14 17:35:23 -0700909 }
910
911 /**
Amith Yamasani920ace02012-09-20 22:15:37 -0700912 * Make a note of the last started time of a user.
913 * @param userId the user that was just foregrounded
914 */
915 public void userForeground(int userId) {
916 synchronized (mPackagesLock) {
917 UserInfo user = mUsers.get(userId);
918 long now = System.currentTimeMillis();
Dianne Hackbornd4ac8d72012-09-27 23:20:10 -0700919 if (user == null || user.partial) {
920 Slog.w(LOG_TAG, "userForeground: unknown user #" + userId);
921 return;
922 }
923 if (now > EPOCH_PLUS_30_YEARS) {
Amith Yamasani920ace02012-09-20 22:15:37 -0700924 user.lastLoggedInTime = now;
925 writeUserLocked(user);
926 }
927 }
928 }
929
930 /**
Amith Yamasani0b285492011-04-14 17:35:23 -0700931 * Returns the next available user id, filling in any holes in the ids.
Amith Yamasani742a6712011-05-04 14:49:28 -0700932 * TODO: May not be a good idea to recycle ids, in case it results in confusion
933 * for data and battery stats collection, or unexpected cross-talk.
Amith Yamasani0b285492011-04-14 17:35:23 -0700934 * @return
935 */
Dianne Hackborn5dc5a002012-09-15 19:33:48 -0700936 private int getNextAvailableIdLocked() {
Dianne Hackborn4428e172012-08-24 17:43:05 -0700937 synchronized (mPackagesLock) {
Jeff Sharkeyffe0cb42012-11-05 17:24:43 -0800938 int i = MIN_USER_ID;
Amith Yamasani195263742012-08-21 15:40:12 -0700939 while (i < Integer.MAX_VALUE) {
Jeff Sharkeyffe0cb42012-11-05 17:24:43 -0800940 if (mUsers.indexOfKey(i) < 0 && !mRemovingUserIds.get(i)) {
Amith Yamasani195263742012-08-21 15:40:12 -0700941 break;
942 }
943 i++;
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700944 }
Amith Yamasani195263742012-08-21 15:40:12 -0700945 return i;
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700946 }
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700947 }
Amith Yamasani920ace02012-09-20 22:15:37 -0700948
949 @Override
950 protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
951 if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.DUMP)
952 != PackageManager.PERMISSION_GRANTED) {
953 pw.println("Permission Denial: can't dump UserManager from from pid="
954 + Binder.getCallingPid()
955 + ", uid=" + Binder.getCallingUid()
956 + " without permission "
957 + android.Manifest.permission.DUMP);
958 return;
959 }
960
961 long now = System.currentTimeMillis();
962 StringBuilder sb = new StringBuilder();
963 synchronized (mPackagesLock) {
964 pw.println("Users:");
965 for (int i = 0; i < mUsers.size(); i++) {
966 UserInfo user = mUsers.valueAt(i);
967 if (user == null) continue;
Amith Yamasani634cf312012-10-04 17:34:21 -0700968 pw.print(" "); pw.print(user); pw.print(" serialNo="); pw.print(user.serialNumber);
Jeff Sharkeyffe0cb42012-11-05 17:24:43 -0800969 if (mRemovingUserIds.get(mUsers.keyAt(i))) pw.print(" <removing> ");
Dianne Hackbornd4ac8d72012-09-27 23:20:10 -0700970 if (user.partial) pw.print(" <partial>");
971 pw.println();
Amith Yamasani920ace02012-09-20 22:15:37 -0700972 pw.print(" Created: ");
973 if (user.creationTime == 0) {
974 pw.println("<unknown>");
975 } else {
976 sb.setLength(0);
977 TimeUtils.formatDuration(now - user.creationTime, sb);
978 sb.append(" ago");
979 pw.println(sb);
980 }
981 pw.print(" Last logged in: ");
982 if (user.lastLoggedInTime == 0) {
983 pw.println("<unknown>");
984 } else {
985 sb.setLength(0);
986 TimeUtils.formatDuration(now - user.lastLoggedInTime, sb);
987 sb.append(" ago");
988 pw.println(sb);
989 }
990 }
991 }
992 }
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700993}