blob: 66c3ce927bcc780ba21fd5f8d57a9433472187b5 [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 Yamasani6f34b412012-10-22 18:19:27 -070091 private static final int USER_VERSION = 1;
92
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 /**
487 * This fixes an incorrect initialization of user name for the owner.
488 * TODO: Remove in the next release.
489 */
490 private void upgradeIfNecessary() {
491 int userVersion = mUserVersion;
492 if (userVersion < 1) {
493 // Assign a proper name for the owner, if not initialized correctly before
494 UserInfo user = mUsers.get(UserHandle.USER_OWNER);
495 if ("Primary".equals(user.name)) {
496 user.name = mContext.getResources().getString(com.android.internal.R.string.owner_name);
497 writeUserLocked(user);
498 }
499 userVersion = 1;
500 }
501
502 if (userVersion < USER_VERSION) {
503 Slog.w(LOG_TAG, "User version " + mUserVersion + " didn't upgrade as expected to "
504 + USER_VERSION);
505 } else {
506 mUserVersion = userVersion;
507 writeUserListLocked();
508 }
509 }
510
Amith Yamasani13593602012-03-22 16:16:17 -0700511 private void fallbackToSingleUserLocked() {
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700512 // Create the primary user
Jeff Sharkeyffe0cb42012-11-05 17:24:43 -0800513 UserInfo primary = new UserInfo(0,
Amith Yamasani6f34b412012-10-22 18:19:27 -0700514 mContext.getResources().getString(com.android.internal.R.string.owner_name), null,
Amith Yamasani756901d2012-10-12 12:30:07 -0700515 UserInfo.FLAG_ADMIN | UserInfo.FLAG_PRIMARY | UserInfo.FLAG_INITIALIZED);
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700516 mUsers.put(0, primary);
Amith Yamasani634cf312012-10-04 17:34:21 -0700517 mNextSerialNumber = MIN_USER_ID;
Amith Yamasani13593602012-03-22 16:16:17 -0700518 updateUserIdsLocked();
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700519
Amith Yamasani13593602012-03-22 16:16:17 -0700520 writeUserListLocked();
521 writeUserLocked(primary);
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700522 }
523
524 /*
525 * Writes the user file in this format:
526 *
527 * <user flags="20039023" id="0">
528 * <name>Primary</name>
529 * </user>
530 */
Amith Yamasani13593602012-03-22 16:16:17 -0700531 private void writeUserLocked(UserInfo userInfo) {
Amith Yamasani742a6712011-05-04 14:49:28 -0700532 FileOutputStream fos = null;
Amith Yamasani2a003292012-08-14 18:25:45 -0700533 AtomicFile userFile = new AtomicFile(new File(mUsersDir, userInfo.id + ".xml"));
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700534 try {
Amith Yamasani2a003292012-08-14 18:25:45 -0700535 fos = userFile.startWrite();
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700536 final BufferedOutputStream bos = new BufferedOutputStream(fos);
537
538 // XmlSerializer serializer = XmlUtils.serializerInstance();
539 final XmlSerializer serializer = new FastXmlSerializer();
540 serializer.setOutput(bos, "utf-8");
541 serializer.startDocument(null, true);
542 serializer.setFeature("http://xmlpull.org/v1/doc/features.html#indent-output", true);
543
544 serializer.startTag(null, TAG_USER);
545 serializer.attribute(null, ATTR_ID, Integer.toString(userInfo.id));
Amith Yamasani2a003292012-08-14 18:25:45 -0700546 serializer.attribute(null, ATTR_SERIAL_NO, Integer.toString(userInfo.serialNumber));
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700547 serializer.attribute(null, ATTR_FLAGS, Integer.toString(userInfo.flags));
Amith Yamasani920ace02012-09-20 22:15:37 -0700548 serializer.attribute(null, ATTR_CREATION_TIME, Long.toString(userInfo.creationTime));
549 serializer.attribute(null, ATTR_LAST_LOGGED_IN_TIME,
550 Long.toString(userInfo.lastLoggedInTime));
Amith Yamasanib8151ec2012-04-18 18:02:48 -0700551 if (userInfo.iconPath != null) {
552 serializer.attribute(null, ATTR_ICON_PATH, userInfo.iconPath);
553 }
Dianne Hackbornd4ac8d72012-09-27 23:20:10 -0700554 if (userInfo.partial) {
555 serializer.attribute(null, ATTR_PARTIAL, "true");
556 }
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700557
558 serializer.startTag(null, TAG_NAME);
559 serializer.text(userInfo.name);
560 serializer.endTag(null, TAG_NAME);
561
562 serializer.endTag(null, TAG_USER);
563
564 serializer.endDocument();
Amith Yamasani2a003292012-08-14 18:25:45 -0700565 userFile.finishWrite(fos);
566 } catch (Exception ioe) {
Amith Yamasani0b285492011-04-14 17:35:23 -0700567 Slog.e(LOG_TAG, "Error writing user info " + userInfo.id + "\n" + ioe);
Amith Yamasani2a003292012-08-14 18:25:45 -0700568 userFile.failWrite(fos);
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700569 }
570 }
571
572 /*
573 * Writes the user list file in this format:
574 *
Amith Yamasani2a003292012-08-14 18:25:45 -0700575 * <users nextSerialNumber="3">
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700576 * <user id="0"></user>
577 * <user id="2"></user>
578 * </users>
579 */
Amith Yamasani13593602012-03-22 16:16:17 -0700580 private void writeUserListLocked() {
Amith Yamasani742a6712011-05-04 14:49:28 -0700581 FileOutputStream fos = null;
Amith Yamasani2a003292012-08-14 18:25:45 -0700582 AtomicFile userListFile = new AtomicFile(mUserListFile);
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700583 try {
Amith Yamasani2a003292012-08-14 18:25:45 -0700584 fos = userListFile.startWrite();
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700585 final BufferedOutputStream bos = new BufferedOutputStream(fos);
586
587 // XmlSerializer serializer = XmlUtils.serializerInstance();
588 final XmlSerializer serializer = new FastXmlSerializer();
589 serializer.setOutput(bos, "utf-8");
590 serializer.startDocument(null, true);
591 serializer.setFeature("http://xmlpull.org/v1/doc/features.html#indent-output", true);
592
593 serializer.startTag(null, TAG_USERS);
Amith Yamasani2a003292012-08-14 18:25:45 -0700594 serializer.attribute(null, ATTR_NEXT_SERIAL_NO, Integer.toString(mNextSerialNumber));
Amith Yamasani6f34b412012-10-22 18:19:27 -0700595 serializer.attribute(null, ATTR_USER_VERSION, Integer.toString(mUserVersion));
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700596
597 for (int i = 0; i < mUsers.size(); i++) {
598 UserInfo user = mUsers.valueAt(i);
599 serializer.startTag(null, TAG_USER);
600 serializer.attribute(null, ATTR_ID, Integer.toString(user.id));
601 serializer.endTag(null, TAG_USER);
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700602 }
603
604 serializer.endTag(null, TAG_USERS);
605
606 serializer.endDocument();
Amith Yamasani2a003292012-08-14 18:25:45 -0700607 userListFile.finishWrite(fos);
608 } catch (Exception e) {
609 userListFile.failWrite(fos);
Amith Yamasani0b285492011-04-14 17:35:23 -0700610 Slog.e(LOG_TAG, "Error writing user list");
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700611 }
612 }
613
614 private UserInfo readUser(int id) {
615 int flags = 0;
Amith Yamasani2a003292012-08-14 18:25:45 -0700616 int serialNumber = id;
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700617 String name = null;
Amith Yamasanib8151ec2012-04-18 18:02:48 -0700618 String iconPath = null;
Amith Yamasani920ace02012-09-20 22:15:37 -0700619 long creationTime = 0L;
620 long lastLoggedInTime = 0L;
Dianne Hackbornd4ac8d72012-09-27 23:20:10 -0700621 boolean partial = false;
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700622
623 FileInputStream fis = null;
624 try {
Amith Yamasani2a003292012-08-14 18:25:45 -0700625 AtomicFile userFile =
626 new AtomicFile(new File(mUsersDir, Integer.toString(id) + ".xml"));
627 fis = userFile.openRead();
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700628 XmlPullParser parser = Xml.newPullParser();
629 parser.setInput(fis, null);
630 int type;
631 while ((type = parser.next()) != XmlPullParser.START_TAG
632 && type != XmlPullParser.END_DOCUMENT) {
633 ;
634 }
635
636 if (type != XmlPullParser.START_TAG) {
Amith Yamasani0b285492011-04-14 17:35:23 -0700637 Slog.e(LOG_TAG, "Unable to read user " + id);
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700638 return null;
639 }
640
641 if (type == XmlPullParser.START_TAG && parser.getName().equals(TAG_USER)) {
Amith Yamasani920ace02012-09-20 22:15:37 -0700642 int storedId = readIntAttribute(parser, ATTR_ID, -1);
643 if (storedId != id) {
Amith Yamasani0b285492011-04-14 17:35:23 -0700644 Slog.e(LOG_TAG, "User id does not match the file name");
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700645 return null;
646 }
Amith Yamasani920ace02012-09-20 22:15:37 -0700647 serialNumber = readIntAttribute(parser, ATTR_SERIAL_NO, id);
648 flags = readIntAttribute(parser, ATTR_FLAGS, 0);
Amith Yamasanib8151ec2012-04-18 18:02:48 -0700649 iconPath = parser.getAttributeValue(null, ATTR_ICON_PATH);
Amith Yamasani920ace02012-09-20 22:15:37 -0700650 creationTime = readLongAttribute(parser, ATTR_CREATION_TIME, 0);
651 lastLoggedInTime = readLongAttribute(parser, ATTR_LAST_LOGGED_IN_TIME, 0);
Dianne Hackbornd4ac8d72012-09-27 23:20:10 -0700652 String valueString = parser.getAttributeValue(null, ATTR_PARTIAL);
653 if ("true".equals(valueString)) {
654 partial = true;
655 }
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700656
657 while ((type = parser.next()) != XmlPullParser.START_TAG
658 && type != XmlPullParser.END_DOCUMENT) {
659 }
660 if (type == XmlPullParser.START_TAG && parser.getName().equals(TAG_NAME)) {
661 type = parser.next();
662 if (type == XmlPullParser.TEXT) {
663 name = parser.getText();
664 }
665 }
666 }
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700667
Amith Yamasanib8151ec2012-04-18 18:02:48 -0700668 UserInfo userInfo = new UserInfo(id, name, iconPath, flags);
Amith Yamasani2a003292012-08-14 18:25:45 -0700669 userInfo.serialNumber = serialNumber;
Amith Yamasani920ace02012-09-20 22:15:37 -0700670 userInfo.creationTime = creationTime;
671 userInfo.lastLoggedInTime = lastLoggedInTime;
Dianne Hackbornd4ac8d72012-09-27 23:20:10 -0700672 userInfo.partial = partial;
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700673 return userInfo;
674
675 } catch (IOException ioe) {
676 } catch (XmlPullParserException pe) {
Dianne Hackbornbfd89b32011-12-15 18:22:54 -0800677 } finally {
678 if (fis != null) {
679 try {
680 fis.close();
681 } catch (IOException e) {
682 }
683 }
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700684 }
685 return null;
686 }
687
Amith Yamasani920ace02012-09-20 22:15:37 -0700688 private int readIntAttribute(XmlPullParser parser, String attr, int defaultValue) {
689 String valueString = parser.getAttributeValue(null, attr);
690 if (valueString == null) return defaultValue;
691 try {
692 return Integer.parseInt(valueString);
693 } catch (NumberFormatException nfe) {
694 return defaultValue;
695 }
696 }
697
698 private long readLongAttribute(XmlPullParser parser, String attr, long defaultValue) {
699 String valueString = parser.getAttributeValue(null, attr);
700 if (valueString == null) return defaultValue;
701 try {
702 return Long.parseLong(valueString);
703 } catch (NumberFormatException nfe) {
704 return defaultValue;
705 }
706 }
707
Amith Yamasani258848d2012-08-10 17:06:33 -0700708 @Override
Amith Yamasani13593602012-03-22 16:16:17 -0700709 public UserInfo createUser(String name, int flags) {
Amith Yamasani2a003292012-08-14 18:25:45 -0700710 checkManageUsersPermission("Only the system can create users");
Amith Yamasanifaea76f2012-09-11 10:59:48 -0700711
Dianne Hackborn5dc5a002012-09-15 19:33:48 -0700712 final long ident = Binder.clearCallingIdentity();
713 final UserInfo userInfo;
714 try {
715 synchronized (mInstallLock) {
716 synchronized (mPackagesLock) {
717 if (isUserLimitReachedLocked()) return null;
718 int userId = getNextAvailableIdLocked();
719 userInfo = new UserInfo(userId, name, null, flags);
720 File userPath = new File(mBaseUserPath, Integer.toString(userId));
721 userInfo.serialNumber = mNextSerialNumber++;
Amith Yamasani920ace02012-09-20 22:15:37 -0700722 long now = System.currentTimeMillis();
723 userInfo.creationTime = (now > EPOCH_PLUS_30_YEARS) ? now : 0;
Dianne Hackbornd4ac8d72012-09-27 23:20:10 -0700724 userInfo.partial = true;
Amith Yamasani16389312012-10-17 21:20:14 -0700725 Environment.getUserSystemDirectory(userInfo.id).mkdirs();
Dianne Hackborn5dc5a002012-09-15 19:33:48 -0700726 mUsers.put(userId, userInfo);
727 writeUserListLocked();
728 writeUserLocked(userInfo);
Dianne Hackborn5dc5a002012-09-15 19:33:48 -0700729 mPm.createNewUserLILPw(userId, userPath);
Dianne Hackbornd4ac8d72012-09-27 23:20:10 -0700730 userInfo.partial = false;
731 writeUserLocked(userInfo);
732 updateUserIdsLocked();
Dianne Hackborn5dc5a002012-09-15 19:33:48 -0700733 }
Dianne Hackborn4428e172012-08-24 17:43:05 -0700734 }
Dianne Hackborn5dc5a002012-09-15 19:33:48 -0700735 if (userInfo != null) {
736 Intent addedIntent = new Intent(Intent.ACTION_USER_ADDED);
737 addedIntent.putExtra(Intent.EXTRA_USER_HANDLE, userInfo.id);
738 mContext.sendBroadcastAsUser(addedIntent, UserHandle.ALL,
739 android.Manifest.permission.MANAGE_USERS);
740 }
741 } finally {
742 Binder.restoreCallingIdentity(ident);
Amith Yamasani258848d2012-08-10 17:06:33 -0700743 }
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700744 return userInfo;
745 }
746
Amith Yamasani0b285492011-04-14 17:35:23 -0700747 /**
748 * Removes a user and all data directories created for that user. This method should be called
749 * after the user's processes have been terminated.
750 * @param id the user's id
751 */
Amith Yamasani258848d2012-08-10 17:06:33 -0700752 public boolean removeUser(int userHandle) {
Amith Yamasani2a003292012-08-14 18:25:45 -0700753 checkManageUsersPermission("Only the system can remove users");
Dianne Hackborn80a4af22012-08-27 19:18:31 -0700754 final UserInfo user;
755 synchronized (mPackagesLock) {
756 user = mUsers.get(userHandle);
757 if (userHandle == 0 || user == null) {
758 return false;
759 }
Jeff Sharkeyffe0cb42012-11-05 17:24:43 -0800760 mRemovingUserIds.put(userHandle, true);
Amith Yamasani756901d2012-10-12 12:30:07 -0700761 // Set this to a partially created user, so that the user will be purged
762 // on next startup, in case the runtime stops now before stopping and
763 // removing the user completely.
764 user.partial = true;
765 writeUserLocked(user);
Dianne Hackborn80a4af22012-08-27 19:18:31 -0700766 }
Amith Yamasani16389312012-10-17 21:20:14 -0700767 if (DBG) Slog.i(LOG_TAG, "Stopping user " + userHandle);
Dianne Hackborn80a4af22012-08-27 19:18:31 -0700768 int res;
769 try {
770 res = ActivityManagerNative.getDefault().stopUser(userHandle,
771 new IStopUserCallback.Stub() {
772 @Override
773 public void userStopped(int userId) {
774 finishRemoveUser(userId);
775 }
776 @Override
777 public void userStopAborted(int userId) {
778 }
779 });
780 } catch (RemoteException e) {
781 return false;
782 }
783
784 return res == ActivityManager.USER_OP_SUCCESS;
785 }
786
Amith Yamasanidb6a14c2012-10-17 21:16:52 -0700787 void finishRemoveUser(final int userHandle) {
Amith Yamasani16389312012-10-17 21:20:14 -0700788 if (DBG) Slog.i(LOG_TAG, "finishRemoveUser " + userHandle);
Amith Yamasanidb6a14c2012-10-17 21:16:52 -0700789 // Let other services shutdown any activity and clean up their state before completely
790 // wiping the user's system directory and removing from the user list
Dianne Hackborn5dc5a002012-09-15 19:33:48 -0700791 long ident = Binder.clearCallingIdentity();
792 try {
793 Intent addedIntent = new Intent(Intent.ACTION_USER_REMOVED);
794 addedIntent.putExtra(Intent.EXTRA_USER_HANDLE, userHandle);
Amith Yamasanidb6a14c2012-10-17 21:16:52 -0700795 mContext.sendOrderedBroadcastAsUser(addedIntent, UserHandle.ALL,
796 android.Manifest.permission.MANAGE_USERS,
797
798 new BroadcastReceiver() {
799 @Override
800 public void onReceive(Context context, Intent intent) {
801 if (DBG) {
802 Slog.i(LOG_TAG,
803 "USER_REMOVED broadcast sent, cleaning up user data "
804 + userHandle);
805 }
806 new Thread() {
807 public void run() {
808 synchronized (mInstallLock) {
809 synchronized (mPackagesLock) {
810 removeUserStateLocked(userHandle);
811 }
812 }
813 }
814 }.start();
815 }
816 },
817
818 null, Activity.RESULT_OK, null, null);
Dianne Hackborn5dc5a002012-09-15 19:33:48 -0700819 } finally {
820 Binder.restoreCallingIdentity(ident);
821 }
Amith Yamasani2a003292012-08-14 18:25:45 -0700822 }
823
Jeff Sharkeyffe0cb42012-11-05 17:24:43 -0800824 private void removeUserStateLocked(final int userHandle) {
Dianne Hackbornd4ac8d72012-09-27 23:20:10 -0700825 // Cleanup package manager settings
826 mPm.cleanUpUserLILPw(userHandle);
827
828 // Remove this user from the list
829 mUsers.remove(userHandle);
Jeff Sharkeyffe0cb42012-11-05 17:24:43 -0800830
831 // Have user ID linger for several seconds to let external storage VFS
832 // cache entries expire. This must be greater than the 'entry_valid'
833 // timeout used by the FUSE daemon.
834 mHandler.postDelayed(new Runnable() {
835 @Override
836 public void run() {
837 synchronized (mPackagesLock) {
838 mRemovingUserIds.delete(userHandle);
839 }
840 }
841 }, MINUTE_IN_MILLIS);
842
Dianne Hackbornd4ac8d72012-09-27 23:20:10 -0700843 // Remove user file
844 AtomicFile userFile = new AtomicFile(new File(mUsersDir, userHandle + ".xml"));
845 userFile.delete();
846 // Update the user list
847 writeUserListLocked();
848 updateUserIdsLocked();
849 removeDirectoryRecursive(Environment.getUserSystemDirectory(userHandle));
850 }
851
Amith Yamasani61f57372012-08-31 12:12:28 -0700852 private void removeDirectoryRecursive(File parent) {
853 if (parent.isDirectory()) {
854 String[] files = parent.list();
855 for (String filename : files) {
856 File child = new File(parent, filename);
857 removeDirectoryRecursive(child);
858 }
859 }
860 parent.delete();
861 }
862
Amith Yamasani2a003292012-08-14 18:25:45 -0700863 @Override
864 public int getUserSerialNumber(int userHandle) {
Dianne Hackborn4428e172012-08-24 17:43:05 -0700865 synchronized (mPackagesLock) {
Amith Yamasani2a003292012-08-14 18:25:45 -0700866 if (!exists(userHandle)) return -1;
Amith Yamasani195263742012-08-21 15:40:12 -0700867 return getUserInfoLocked(userHandle).serialNumber;
Amith Yamasani2a003292012-08-14 18:25:45 -0700868 }
869 }
870
871 @Override
872 public int getUserHandle(int userSerialNumber) {
Dianne Hackborn4428e172012-08-24 17:43:05 -0700873 synchronized (mPackagesLock) {
Amith Yamasani2a003292012-08-14 18:25:45 -0700874 for (int userId : mUserIds) {
Amith Yamasani195263742012-08-21 15:40:12 -0700875 if (getUserInfoLocked(userId).serialNumber == userSerialNumber) return userId;
Amith Yamasani2a003292012-08-14 18:25:45 -0700876 }
877 // Not found
878 return -1;
Amith Yamasani13593602012-03-22 16:16:17 -0700879 }
880 }
881
Amith Yamasani0b285492011-04-14 17:35:23 -0700882 /**
883 * Caches the list of user ids in an array, adjusting the array size when necessary.
884 */
Amith Yamasani13593602012-03-22 16:16:17 -0700885 private void updateUserIdsLocked() {
Dianne Hackbornd4ac8d72012-09-27 23:20:10 -0700886 int num = 0;
Amith Yamasani0b285492011-04-14 17:35:23 -0700887 for (int i = 0; i < mUsers.size(); i++) {
Dianne Hackbornd4ac8d72012-09-27 23:20:10 -0700888 if (!mUsers.valueAt(i).partial) {
889 num++;
890 }
891 }
Amith Yamasani16389312012-10-17 21:20:14 -0700892 final int[] newUsers = new int[num];
893 int n = 0;
Dianne Hackbornd4ac8d72012-09-27 23:20:10 -0700894 for (int i = 0; i < mUsers.size(); i++) {
895 if (!mUsers.valueAt(i).partial) {
Amith Yamasani16389312012-10-17 21:20:14 -0700896 newUsers[n++] = mUsers.keyAt(i);
Dianne Hackbornd4ac8d72012-09-27 23:20:10 -0700897 }
Amith Yamasani0b285492011-04-14 17:35:23 -0700898 }
Dianne Hackborn7767eac2012-08-23 18:25:40 -0700899 mUserIds = newUsers;
Amith Yamasani0b285492011-04-14 17:35:23 -0700900 }
901
902 /**
Amith Yamasani920ace02012-09-20 22:15:37 -0700903 * Make a note of the last started time of a user.
904 * @param userId the user that was just foregrounded
905 */
906 public void userForeground(int userId) {
907 synchronized (mPackagesLock) {
908 UserInfo user = mUsers.get(userId);
909 long now = System.currentTimeMillis();
Dianne Hackbornd4ac8d72012-09-27 23:20:10 -0700910 if (user == null || user.partial) {
911 Slog.w(LOG_TAG, "userForeground: unknown user #" + userId);
912 return;
913 }
914 if (now > EPOCH_PLUS_30_YEARS) {
Amith Yamasani920ace02012-09-20 22:15:37 -0700915 user.lastLoggedInTime = now;
916 writeUserLocked(user);
917 }
918 }
919 }
920
921 /**
Amith Yamasani0b285492011-04-14 17:35:23 -0700922 * Returns the next available user id, filling in any holes in the ids.
Amith Yamasani742a6712011-05-04 14:49:28 -0700923 * TODO: May not be a good idea to recycle ids, in case it results in confusion
924 * for data and battery stats collection, or unexpected cross-talk.
Amith Yamasani0b285492011-04-14 17:35:23 -0700925 * @return
926 */
Dianne Hackborn5dc5a002012-09-15 19:33:48 -0700927 private int getNextAvailableIdLocked() {
Dianne Hackborn4428e172012-08-24 17:43:05 -0700928 synchronized (mPackagesLock) {
Jeff Sharkeyffe0cb42012-11-05 17:24:43 -0800929 int i = MIN_USER_ID;
Amith Yamasani195263742012-08-21 15:40:12 -0700930 while (i < Integer.MAX_VALUE) {
Jeff Sharkeyffe0cb42012-11-05 17:24:43 -0800931 if (mUsers.indexOfKey(i) < 0 && !mRemovingUserIds.get(i)) {
Amith Yamasani195263742012-08-21 15:40:12 -0700932 break;
933 }
934 i++;
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700935 }
Amith Yamasani195263742012-08-21 15:40:12 -0700936 return i;
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700937 }
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700938 }
Amith Yamasani920ace02012-09-20 22:15:37 -0700939
940 @Override
941 protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
942 if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.DUMP)
943 != PackageManager.PERMISSION_GRANTED) {
944 pw.println("Permission Denial: can't dump UserManager from from pid="
945 + Binder.getCallingPid()
946 + ", uid=" + Binder.getCallingUid()
947 + " without permission "
948 + android.Manifest.permission.DUMP);
949 return;
950 }
951
952 long now = System.currentTimeMillis();
953 StringBuilder sb = new StringBuilder();
954 synchronized (mPackagesLock) {
955 pw.println("Users:");
956 for (int i = 0; i < mUsers.size(); i++) {
957 UserInfo user = mUsers.valueAt(i);
958 if (user == null) continue;
Amith Yamasani634cf312012-10-04 17:34:21 -0700959 pw.print(" "); pw.print(user); pw.print(" serialNo="); pw.print(user.serialNumber);
Jeff Sharkeyffe0cb42012-11-05 17:24:43 -0800960 if (mRemovingUserIds.get(mUsers.keyAt(i))) pw.print(" <removing> ");
Dianne Hackbornd4ac8d72012-09-27 23:20:10 -0700961 if (user.partial) pw.print(" <partial>");
962 pw.println();
Amith Yamasani920ace02012-09-20 22:15:37 -0700963 pw.print(" Created: ");
964 if (user.creationTime == 0) {
965 pw.println("<unknown>");
966 } else {
967 sb.setLength(0);
968 TimeUtils.formatDuration(now - user.creationTime, sb);
969 sb.append(" ago");
970 pw.println(sb);
971 }
972 pw.print(" Last logged in: ");
973 if (user.lastLoggedInTime == 0) {
974 pw.println("<unknown>");
975 } else {
976 sb.setLength(0);
977 TimeUtils.formatDuration(now - user.lastLoggedInTime, sb);
978 sb.append(" ago");
979 pw.println(sb);
980 }
981 }
982 }
983 }
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700984}