blob: 11c6dabf8ade8f1abcb51ac1c7dcebe058908685 [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 Yamasanidf2e92a2013-03-01 17:04:38 -080028import android.content.RestrictionEntry;
Amith Yamasani0b285492011-04-14 17:35:23 -070029import android.content.pm.PackageManager;
Amith Yamasanidf2e92a2013-03-01 17:04:38 -080030import android.content.pm.PackageManager.NameNotFoundException;
Amith Yamasani4b2e9342011-03-31 12:38:53 -070031import android.content.pm.UserInfo;
Amith Yamasanie928d7d2012-09-17 21:46:51 -070032import android.graphics.Bitmap;
33import android.graphics.BitmapFactory;
Amith Yamasani258848d2012-08-10 17:06:33 -070034import android.os.Binder;
Amith Yamasanie4cf7342012-12-17 11:12:09 -080035import android.os.Bundle;
Amith Yamasani4b2e9342011-03-31 12:38:53 -070036import android.os.Environment;
37import android.os.FileUtils;
Jeff Sharkeyffe0cb42012-11-05 17:24:43 -080038import android.os.Handler;
Amith Yamasani258848d2012-08-10 17:06:33 -070039import android.os.IUserManager;
Amith Yamasani258848d2012-08-10 17:06:33 -070040import android.os.Process;
Dianne Hackborn80a4af22012-08-27 19:18:31 -070041import android.os.RemoteException;
Dianne Hackbornf02b60a2012-08-16 10:48:27 -070042import android.os.UserHandle;
Jeff Sharkey27bd34d2012-09-16 12:49:00 -070043import android.os.UserManager;
Amith Yamasani2a003292012-08-14 18:25:45 -070044import android.util.AtomicFile;
Amith Yamasani4b2e9342011-03-31 12:38:53 -070045import android.util.Slog;
46import android.util.SparseArray;
Jeff Sharkeyffe0cb42012-11-05 17:24:43 -080047import android.util.SparseBooleanArray;
Amith Yamasani920ace02012-09-20 22:15:37 -070048import android.util.TimeUtils;
Amith Yamasani4b2e9342011-03-31 12:38:53 -070049import android.util.Xml;
50
Jeff Sharkeyffe0cb42012-11-05 17:24:43 -080051import com.android.internal.util.ArrayUtils;
52import com.android.internal.util.FastXmlSerializer;
53
54import org.xmlpull.v1.XmlPullParser;
55import org.xmlpull.v1.XmlPullParserException;
56import org.xmlpull.v1.XmlSerializer;
57
Amith Yamasani4b2e9342011-03-31 12:38:53 -070058import java.io.BufferedOutputStream;
59import java.io.File;
Amith Yamasani920ace02012-09-20 22:15:37 -070060import java.io.FileDescriptor;
Amith Yamasani4b2e9342011-03-31 12:38:53 -070061import java.io.FileInputStream;
Amith Yamasanib8151ec2012-04-18 18:02:48 -070062import java.io.FileNotFoundException;
Amith Yamasani4b2e9342011-03-31 12:38:53 -070063import java.io.FileOutputStream;
64import java.io.IOException;
Amith Yamasani920ace02012-09-20 22:15:37 -070065import java.io.PrintWriter;
Amith Yamasani4b2e9342011-03-31 12:38:53 -070066import java.util.ArrayList;
67import java.util.List;
68
Amith Yamasani258848d2012-08-10 17:06:33 -070069public class UserManagerService extends IUserManager.Stub {
Amith Yamasanib8151ec2012-04-18 18:02:48 -070070
Amith Yamasani2a003292012-08-14 18:25:45 -070071 private static final String LOG_TAG = "UserManagerService";
Amith Yamasanib8151ec2012-04-18 18:02:48 -070072
Amith Yamasani16389312012-10-17 21:20:14 -070073 private static final boolean DBG = false;
74
Amith Yamasani4b2e9342011-03-31 12:38:53 -070075 private static final String TAG_NAME = "name";
Amith Yamasani4b2e9342011-03-31 12:38:53 -070076 private static final String ATTR_FLAGS = "flags";
Amith Yamasanib8151ec2012-04-18 18:02:48 -070077 private static final String ATTR_ICON_PATH = "icon";
Amith Yamasani4b2e9342011-03-31 12:38:53 -070078 private static final String ATTR_ID = "id";
Amith Yamasani920ace02012-09-20 22:15:37 -070079 private static final String ATTR_CREATION_TIME = "created";
80 private static final String ATTR_LAST_LOGGED_IN_TIME = "lastLoggedIn";
Amith Yamasani2a003292012-08-14 18:25:45 -070081 private static final String ATTR_SERIAL_NO = "serialNumber";
82 private static final String ATTR_NEXT_SERIAL_NO = "nextSerialNumber";
Dianne Hackbornd4ac8d72012-09-27 23:20:10 -070083 private static final String ATTR_PARTIAL = "partial";
Amith Yamasani6f34b412012-10-22 18:19:27 -070084 private static final String ATTR_USER_VERSION = "version";
Amith Yamasani4b2e9342011-03-31 12:38:53 -070085 private static final String TAG_USERS = "users";
Amith Yamasani4b2e9342011-03-31 12:38:53 -070086 private static final String TAG_USER = "user";
Amith Yamasanie4cf7342012-12-17 11:12:09 -080087 private static final String TAG_RESTRICTIONS = "restrictions";
Amith Yamasanidf2e92a2013-03-01 17:04:38 -080088 private static final String TAG_ENTRY = "entry";
89 private static final String TAG_VALUE = "value";
90 private static final String ATTR_KEY = "key";
Amith Yamasani7e99bc02013-04-16 18:24:51 -070091 private static final String ATTR_VALUE_TYPE = "type";
Amith Yamasanidf2e92a2013-03-01 17:04:38 -080092 private static final String ATTR_MULTIPLE = "m";
Amith Yamasani4b2e9342011-03-31 12:38:53 -070093
Amith Yamasani7e99bc02013-04-16 18:24:51 -070094 private static final String ATTR_TYPE_STRING_ARRAY = "sa";
95 private static final String ATTR_TYPE_STRING = "s";
96 private static final String ATTR_TYPE_BOOLEAN = "b";
97
Amith Yamasani0b285492011-04-14 17:35:23 -070098 private static final String USER_INFO_DIR = "system" + File.separator + "users";
Amith Yamasani4b2e9342011-03-31 12:38:53 -070099 private static final String USER_LIST_FILENAME = "userlist.xml";
Amith Yamasanib8151ec2012-04-18 18:02:48 -0700100 private static final String USER_PHOTO_FILENAME = "photo.png";
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700101
Amith Yamasanidf2e92a2013-03-01 17:04:38 -0800102 private static final String RESTRICTIONS_FILE_PREFIX = "res_";
103
Amith Yamasani634cf312012-10-04 17:34:21 -0700104 private static final int MIN_USER_ID = 10;
105
Amith Yamasanibc9625052012-11-15 14:39:18 -0800106 private static final int USER_VERSION = 2;
Amith Yamasani6f34b412012-10-22 18:19:27 -0700107
Amith Yamasani920ace02012-09-20 22:15:37 -0700108 private static final long EPOCH_PLUS_30_YEARS = 30L * 365 * 24 * 60 * 60 * 1000L; // ms
109
Dianne Hackborn4428e172012-08-24 17:43:05 -0700110 private final Context mContext;
111 private final PackageManagerService mPm;
112 private final Object mInstallLock;
113 private final Object mPackagesLock;
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700114
Jeff Sharkeyffe0cb42012-11-05 17:24:43 -0800115 private final Handler mHandler;
116
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700117 private final File mUsersDir;
118 private final File mUserListFile;
Dianne Hackborn4428e172012-08-24 17:43:05 -0700119 private final File mBaseUserPath;
120
Jeff Sharkeyffe0cb42012-11-05 17:24:43 -0800121 private final SparseArray<UserInfo> mUsers = new SparseArray<UserInfo>();
Amith Yamasanie4cf7342012-12-17 11:12:09 -0800122 private final SparseArray<Bundle> mUserRestrictions = new SparseArray<Bundle>();
Jeff Sharkeyffe0cb42012-11-05 17:24:43 -0800123
124 /**
125 * Set of user IDs being actively removed. Removed IDs linger in this set
126 * for several seconds to work around a VFS caching issue.
127 */
128 // @GuardedBy("mPackagesLock")
129 private final SparseBooleanArray mRemovingUserIds = new SparseBooleanArray();
Dianne Hackborn4428e172012-08-24 17:43:05 -0700130
Amith Yamasani0b285492011-04-14 17:35:23 -0700131 private int[] mUserIds;
Amith Yamasani258848d2012-08-10 17:06:33 -0700132 private boolean mGuestEnabled;
Amith Yamasani2a003292012-08-14 18:25:45 -0700133 private int mNextSerialNumber;
Amith Yamasani6f34b412012-10-22 18:19:27 -0700134 private int mUserVersion = 0;
Amith Yamasani0b285492011-04-14 17:35:23 -0700135
Amith Yamasani258848d2012-08-10 17:06:33 -0700136 private static UserManagerService sInstance;
Amith Yamasani258848d2012-08-10 17:06:33 -0700137
Dianne Hackborn4428e172012-08-24 17:43:05 -0700138 public static UserManagerService getInstance() {
139 synchronized (UserManagerService.class) {
140 return sInstance;
Amith Yamasani258848d2012-08-10 17:06:33 -0700141 }
Amith Yamasani258848d2012-08-10 17:06:33 -0700142 }
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700143
144 /**
145 * Available for testing purposes.
146 */
Amith Yamasani258848d2012-08-10 17:06:33 -0700147 UserManagerService(File dataDir, File baseUserPath) {
Dianne Hackborn4428e172012-08-24 17:43:05 -0700148 this(null, null, new Object(), new Object(), dataDir, baseUserPath);
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700149 }
150
Dianne Hackborn4428e172012-08-24 17:43:05 -0700151 /**
152 * Called by package manager to create the service. This is closely
153 * associated with the package manager, and the given lock is the
154 * package manager's own lock.
155 */
156 UserManagerService(Context context, PackageManagerService pm,
157 Object installLock, Object packagesLock) {
158 this(context, pm, installLock, packagesLock,
159 Environment.getDataDirectory(),
160 new File(Environment.getDataDirectory(), "user"));
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700161 }
162
Dianne Hackborn4428e172012-08-24 17:43:05 -0700163 /**
164 * Available for testing purposes.
165 */
166 private UserManagerService(Context context, PackageManagerService pm,
167 Object installLock, Object packagesLock,
168 File dataDir, File baseUserPath) {
Dianne Hackbornd4ac8d72012-09-27 23:20:10 -0700169 mContext = context;
170 mPm = pm;
171 mInstallLock = installLock;
172 mPackagesLock = packagesLock;
Jeff Sharkeyffe0cb42012-11-05 17:24:43 -0800173 mHandler = new Handler();
Dianne Hackbornd4ac8d72012-09-27 23:20:10 -0700174 synchronized (mInstallLock) {
175 synchronized (mPackagesLock) {
176 mUsersDir = new File(dataDir, USER_INFO_DIR);
177 mUsersDir.mkdirs();
178 // Make zeroth user directory, for services to migrate their files to that location
179 File userZeroDir = new File(mUsersDir, "0");
180 userZeroDir.mkdirs();
181 mBaseUserPath = baseUserPath;
182 FileUtils.setPermissions(mUsersDir.toString(),
183 FileUtils.S_IRWXU|FileUtils.S_IRWXG
184 |FileUtils.S_IROTH|FileUtils.S_IXOTH,
185 -1, -1);
186 mUserListFile = new File(mUsersDir, USER_LIST_FILENAME);
187 readUserListLocked();
Amith Yamasani756901d2012-10-12 12:30:07 -0700188 // Prune out any partially created/partially removed users.
Dianne Hackbornd4ac8d72012-09-27 23:20:10 -0700189 ArrayList<UserInfo> partials = new ArrayList<UserInfo>();
190 for (int i = 0; i < mUsers.size(); i++) {
191 UserInfo ui = mUsers.valueAt(i);
192 if (ui.partial && i != 0) {
193 partials.add(ui);
194 }
195 }
196 for (int i = 0; i < partials.size(); i++) {
197 UserInfo ui = partials.get(i);
198 Slog.w(LOG_TAG, "Removing partially created user #" + i
199 + " (name=" + ui.name + ")");
200 removeUserStateLocked(ui.id);
201 }
202 sInstance = this;
203 }
Dianne Hackborn4428e172012-08-24 17:43:05 -0700204 }
Amith Yamasani258848d2012-08-10 17:06:33 -0700205 }
206
207 @Override
Amith Yamasani920ace02012-09-20 22:15:37 -0700208 public List<UserInfo> getUsers(boolean excludeDying) {
Amith Yamasani2a003292012-08-14 18:25:45 -0700209 checkManageUsersPermission("query users");
Dianne Hackborn4428e172012-08-24 17:43:05 -0700210 synchronized (mPackagesLock) {
Amith Yamasani13593602012-03-22 16:16:17 -0700211 ArrayList<UserInfo> users = new ArrayList<UserInfo>(mUsers.size());
212 for (int i = 0; i < mUsers.size(); i++) {
Dianne Hackbornd4ac8d72012-09-27 23:20:10 -0700213 UserInfo ui = mUsers.valueAt(i);
214 if (ui.partial) {
215 continue;
216 }
Jeff Sharkeyffe0cb42012-11-05 17:24:43 -0800217 if (!excludeDying || !mRemovingUserIds.get(ui.id)) {
Dianne Hackbornd4ac8d72012-09-27 23:20:10 -0700218 users.add(ui);
Amith Yamasani920ace02012-09-20 22:15:37 -0700219 }
Amith Yamasani13593602012-03-22 16:16:17 -0700220 }
221 return users;
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700222 }
Amith Yamasani13593602012-03-22 16:16:17 -0700223 }
224
Amith Yamasani258848d2012-08-10 17:06:33 -0700225 @Override
226 public UserInfo getUserInfo(int userId) {
Amith Yamasani2a003292012-08-14 18:25:45 -0700227 checkManageUsersPermission("query user");
Dianne Hackborn4428e172012-08-24 17:43:05 -0700228 synchronized (mPackagesLock) {
Amith Yamasani195263742012-08-21 15:40:12 -0700229 return getUserInfoLocked(userId);
Amith Yamasani13593602012-03-22 16:16:17 -0700230 }
231 }
232
Amith Yamasani71e6c692013-03-24 17:39:28 -0700233 @Override
234 public boolean isRestricted() {
235 synchronized (mPackagesLock) {
236 return getUserInfoLocked(UserHandle.getCallingUserId()).isRestricted();
237 }
238 }
239
Amith Yamasani195263742012-08-21 15:40:12 -0700240 /*
241 * Should be locked on mUsers before calling this.
242 */
243 private UserInfo getUserInfoLocked(int userId) {
Dianne Hackbornd4ac8d72012-09-27 23:20:10 -0700244 UserInfo ui = mUsers.get(userId);
Amith Yamasani16389312012-10-17 21:20:14 -0700245 // If it is partial and not in the process of being removed, return as unknown user.
Jeff Sharkeyffe0cb42012-11-05 17:24:43 -0800246 if (ui != null && ui.partial && !mRemovingUserIds.get(userId)) {
Dianne Hackbornd4ac8d72012-09-27 23:20:10 -0700247 Slog.w(LOG_TAG, "getUserInfo: unknown user #" + userId);
248 return null;
249 }
250 return ui;
Amith Yamasani195263742012-08-21 15:40:12 -0700251 }
252
Amith Yamasani13593602012-03-22 16:16:17 -0700253 public boolean exists(int userId) {
Dianne Hackborn4428e172012-08-24 17:43:05 -0700254 synchronized (mPackagesLock) {
Amith Yamasani13593602012-03-22 16:16:17 -0700255 return ArrayUtils.contains(mUserIds, userId);
256 }
257 }
258
Amith Yamasani258848d2012-08-10 17:06:33 -0700259 @Override
Amith Yamasanib8151ec2012-04-18 18:02:48 -0700260 public void setUserName(int userId, String name) {
Amith Yamasani2a003292012-08-14 18:25:45 -0700261 checkManageUsersPermission("rename users");
Dianne Hackbornd4ac8d72012-09-27 23:20:10 -0700262 boolean changed = false;
Dianne Hackborn4428e172012-08-24 17:43:05 -0700263 synchronized (mPackagesLock) {
Amith Yamasani13593602012-03-22 16:16:17 -0700264 UserInfo info = mUsers.get(userId);
Dianne Hackbornd4ac8d72012-09-27 23:20:10 -0700265 if (info == null || info.partial) {
266 Slog.w(LOG_TAG, "setUserName: unknown user #" + userId);
267 return;
268 }
Amith Yamasani13593602012-03-22 16:16:17 -0700269 if (name != null && !name.equals(info.name)) {
270 info.name = name;
271 writeUserLocked(info);
Dianne Hackbornd4ac8d72012-09-27 23:20:10 -0700272 changed = true;
Amith Yamasani13593602012-03-22 16:16:17 -0700273 }
274 }
Dianne Hackbornd4ac8d72012-09-27 23:20:10 -0700275 if (changed) {
276 sendUserInfoChangedBroadcast(userId);
277 }
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700278 }
279
Amith Yamasani258848d2012-08-10 17:06:33 -0700280 @Override
Amith Yamasanie928d7d2012-09-17 21:46:51 -0700281 public void setUserIcon(int userId, Bitmap bitmap) {
Amith Yamasani2a003292012-08-14 18:25:45 -0700282 checkManageUsersPermission("update users");
Dianne Hackborn4428e172012-08-24 17:43:05 -0700283 synchronized (mPackagesLock) {
Amith Yamasanib8151ec2012-04-18 18:02:48 -0700284 UserInfo info = mUsers.get(userId);
Dianne Hackbornd4ac8d72012-09-27 23:20:10 -0700285 if (info == null || info.partial) {
286 Slog.w(LOG_TAG, "setUserIcon: unknown user #" + userId);
287 return;
288 }
Amith Yamasanie928d7d2012-09-17 21:46:51 -0700289 writeBitmapLocked(info, bitmap);
290 writeUserLocked(info);
Amith Yamasanib8151ec2012-04-18 18:02:48 -0700291 }
Amith Yamasanie928d7d2012-09-17 21:46:51 -0700292 sendUserInfoChangedBroadcast(userId);
293 }
294
295 private void sendUserInfoChangedBroadcast(int userId) {
296 Intent changedIntent = new Intent(Intent.ACTION_USER_INFO_CHANGED);
297 changedIntent.putExtra(Intent.EXTRA_USER_HANDLE, userId);
298 changedIntent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY);
299 mContext.sendBroadcastAsUser(changedIntent, new UserHandle(userId));
Amith Yamasanib8151ec2012-04-18 18:02:48 -0700300 }
301
Amith Yamasani258848d2012-08-10 17:06:33 -0700302 @Override
Amith Yamasanie928d7d2012-09-17 21:46:51 -0700303 public Bitmap getUserIcon(int userId) {
Amith Yamasani3b49f072012-09-17 10:21:43 -0700304 checkManageUsersPermission("read users");
305 synchronized (mPackagesLock) {
306 UserInfo info = mUsers.get(userId);
Dianne Hackbornd4ac8d72012-09-27 23:20:10 -0700307 if (info == null || info.partial) {
308 Slog.w(LOG_TAG, "getUserIcon: unknown user #" + userId);
309 return null;
310 }
311 if (info.iconPath == null) {
312 return null;
313 }
Amith Yamasanie928d7d2012-09-17 21:46:51 -0700314 return BitmapFactory.decodeFile(info.iconPath);
Amith Yamasani3b49f072012-09-17 10:21:43 -0700315 }
316 }
317
318 @Override
Amith Yamasani258848d2012-08-10 17:06:33 -0700319 public void setGuestEnabled(boolean enable) {
Amith Yamasani2a003292012-08-14 18:25:45 -0700320 checkManageUsersPermission("enable guest users");
Dianne Hackborn4428e172012-08-24 17:43:05 -0700321 synchronized (mPackagesLock) {
Amith Yamasani258848d2012-08-10 17:06:33 -0700322 if (mGuestEnabled != enable) {
323 mGuestEnabled = enable;
324 // Erase any guest user that currently exists
325 for (int i = 0; i < mUsers.size(); i++) {
326 UserInfo user = mUsers.valueAt(i);
Dianne Hackbornd4ac8d72012-09-27 23:20:10 -0700327 if (!user.partial && user.isGuest()) {
Amith Yamasani258848d2012-08-10 17:06:33 -0700328 if (!enable) {
329 removeUser(user.id);
330 }
331 return;
332 }
333 }
334 // No guest was found
335 if (enable) {
336 createUser("Guest", UserInfo.FLAG_GUEST);
337 }
338 }
339 }
340 }
341
342 @Override
343 public boolean isGuestEnabled() {
Dianne Hackborn4428e172012-08-24 17:43:05 -0700344 synchronized (mPackagesLock) {
Amith Yamasani258848d2012-08-10 17:06:33 -0700345 return mGuestEnabled;
346 }
347 }
348
349 @Override
350 public void wipeUser(int userHandle) {
Amith Yamasani2a003292012-08-14 18:25:45 -0700351 checkManageUsersPermission("wipe user");
Amith Yamasani258848d2012-08-10 17:06:33 -0700352 // TODO:
353 }
354
Dianne Hackborn5dc5a002012-09-15 19:33:48 -0700355 public void makeInitialized(int userId) {
356 checkManageUsersPermission("makeInitialized");
357 synchronized (mPackagesLock) {
358 UserInfo info = mUsers.get(userId);
Dianne Hackbornd4ac8d72012-09-27 23:20:10 -0700359 if (info == null || info.partial) {
360 Slog.w(LOG_TAG, "makeInitialized: unknown user #" + userId);
361 }
362 if ((info.flags&UserInfo.FLAG_INITIALIZED) == 0) {
Dianne Hackborn5dc5a002012-09-15 19:33:48 -0700363 info.flags |= UserInfo.FLAG_INITIALIZED;
364 writeUserLocked(info);
365 }
366 }
367 }
368
Amith Yamasanie4cf7342012-12-17 11:12:09 -0800369 @Override
370 public Bundle getUserRestrictions(int userId) {
371 // checkManageUsersPermission("getUserRestrictions");
372
373 synchronized (mPackagesLock) {
374 Bundle restrictions = mUserRestrictions.get(userId);
375 return restrictions != null ? restrictions : Bundle.EMPTY;
376 }
377 }
378
379 @Override
380 public void setUserRestrictions(Bundle restrictions, int userId) {
381 checkManageUsersPermission("setUserRestrictions");
382
383 synchronized (mPackagesLock) {
384 mUserRestrictions.get(userId).putAll(restrictions);
385 writeUserLocked(mUsers.get(userId));
386 }
387 }
388
Amith Yamasani258848d2012-08-10 17:06:33 -0700389 /**
Amith Yamasanifaea76f2012-09-11 10:59:48 -0700390 * Check if we've hit the limit of how many users can be created.
391 */
Dianne Hackborn5dc5a002012-09-15 19:33:48 -0700392 private boolean isUserLimitReachedLocked() {
393 int nUsers = mUsers.size();
Jeff Sharkey27bd34d2012-09-16 12:49:00 -0700394 return nUsers >= UserManager.getMaxSupportedUsers();
Amith Yamasanifaea76f2012-09-11 10:59:48 -0700395 }
396
397 /**
Amith Yamasani195263742012-08-21 15:40:12 -0700398 * Enforces that only the system UID or root's UID or apps that have the
399 * {@link android.Manifest.permission.MANAGE_USERS MANAGE_USERS}
400 * permission can make certain calls to the UserManager.
Amith Yamasani258848d2012-08-10 17:06:33 -0700401 *
402 * @param message used as message if SecurityException is thrown
403 * @throws SecurityException if the caller is not system or root
404 */
Amith Yamasani2a003292012-08-14 18:25:45 -0700405 private static final void checkManageUsersPermission(String message) {
Amith Yamasani258848d2012-08-10 17:06:33 -0700406 final int uid = Binder.getCallingUid();
Amith Yamasani2a003292012-08-14 18:25:45 -0700407 if (uid != Process.SYSTEM_UID && uid != 0
408 && ActivityManager.checkComponentPermission(
409 android.Manifest.permission.MANAGE_USERS,
410 uid, -1, true) != PackageManager.PERMISSION_GRANTED) {
411 throw new SecurityException("You need MANAGE_USERS permission to: " + message);
Amith Yamasani258848d2012-08-10 17:06:33 -0700412 }
413 }
414
Amith Yamasanie928d7d2012-09-17 21:46:51 -0700415 private void writeBitmapLocked(UserInfo info, Bitmap bitmap) {
Amith Yamasanib8151ec2012-04-18 18:02:48 -0700416 try {
417 File dir = new File(mUsersDir, Integer.toString(info.id));
418 File file = new File(dir, USER_PHOTO_FILENAME);
419 if (!dir.exists()) {
420 dir.mkdir();
421 FileUtils.setPermissions(
422 dir.getPath(),
423 FileUtils.S_IRWXU|FileUtils.S_IRWXG|FileUtils.S_IXOTH,
424 -1, -1);
425 }
Amith Yamasanie928d7d2012-09-17 21:46:51 -0700426 FileOutputStream os;
427 if (bitmap.compress(Bitmap.CompressFormat.PNG, 100, os = new FileOutputStream(file))) {
Amith Yamasani3b49f072012-09-17 10:21:43 -0700428 info.iconPath = file.getAbsolutePath();
429 }
Amith Yamasanie928d7d2012-09-17 21:46:51 -0700430 try {
431 os.close();
432 } catch (IOException ioe) {
433 // What the ... !
434 }
Amith Yamasanib8151ec2012-04-18 18:02:48 -0700435 } catch (FileNotFoundException e) {
Amith Yamasani2a003292012-08-14 18:25:45 -0700436 Slog.w(LOG_TAG, "Error setting photo for user ", e);
Amith Yamasanib8151ec2012-04-18 18:02:48 -0700437 }
Amith Yamasanib8151ec2012-04-18 18:02:48 -0700438 }
439
Amith Yamasani0b285492011-04-14 17:35:23 -0700440 /**
441 * Returns an array of user ids. This array is cached here for quick access, so do not modify or
442 * cache it elsewhere.
443 * @return the array of user ids.
444 */
Dianne Hackborn1676c852012-09-10 14:52:30 -0700445 public int[] getUserIds() {
Dianne Hackborn4428e172012-08-24 17:43:05 -0700446 synchronized (mPackagesLock) {
Dianne Hackborn7767eac2012-08-23 18:25:40 -0700447 return mUserIds;
448 }
Amith Yamasani0b285492011-04-14 17:35:23 -0700449 }
450
Dianne Hackborn4428e172012-08-24 17:43:05 -0700451 int[] getUserIdsLPr() {
452 return mUserIds;
453 }
454
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700455 private void readUserList() {
Dianne Hackborn4428e172012-08-24 17:43:05 -0700456 synchronized (mPackagesLock) {
Amith Yamasani13593602012-03-22 16:16:17 -0700457 readUserListLocked();
458 }
459 }
460
461 private void readUserListLocked() {
Amith Yamasani258848d2012-08-10 17:06:33 -0700462 mGuestEnabled = false;
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700463 if (!mUserListFile.exists()) {
Amith Yamasani13593602012-03-22 16:16:17 -0700464 fallbackToSingleUserLocked();
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700465 return;
466 }
467 FileInputStream fis = null;
Amith Yamasani2a003292012-08-14 18:25:45 -0700468 AtomicFile userListFile = new AtomicFile(mUserListFile);
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700469 try {
Amith Yamasani2a003292012-08-14 18:25:45 -0700470 fis = userListFile.openRead();
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700471 XmlPullParser parser = Xml.newPullParser();
472 parser.setInput(fis, null);
473 int type;
474 while ((type = parser.next()) != XmlPullParser.START_TAG
475 && type != XmlPullParser.END_DOCUMENT) {
476 ;
477 }
478
479 if (type != XmlPullParser.START_TAG) {
Amith Yamasani0b285492011-04-14 17:35:23 -0700480 Slog.e(LOG_TAG, "Unable to read user list");
Amith Yamasani13593602012-03-22 16:16:17 -0700481 fallbackToSingleUserLocked();
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700482 return;
483 }
484
Amith Yamasani2a003292012-08-14 18:25:45 -0700485 mNextSerialNumber = -1;
486 if (parser.getName().equals(TAG_USERS)) {
487 String lastSerialNumber = parser.getAttributeValue(null, ATTR_NEXT_SERIAL_NO);
488 if (lastSerialNumber != null) {
489 mNextSerialNumber = Integer.parseInt(lastSerialNumber);
490 }
Amith Yamasani6f34b412012-10-22 18:19:27 -0700491 String versionNumber = parser.getAttributeValue(null, ATTR_USER_VERSION);
492 if (versionNumber != null) {
493 mUserVersion = Integer.parseInt(versionNumber);
494 }
Amith Yamasani2a003292012-08-14 18:25:45 -0700495 }
496
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700497 while ((type = parser.next()) != XmlPullParser.END_DOCUMENT) {
498 if (type == XmlPullParser.START_TAG && parser.getName().equals(TAG_USER)) {
499 String id = parser.getAttributeValue(null, ATTR_ID);
Amith Yamasanie4cf7342012-12-17 11:12:09 -0800500 UserInfo user = readUserLocked(Integer.parseInt(id));
Amith Yamasani6f34b412012-10-22 18:19:27 -0700501
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700502 if (user != null) {
503 mUsers.put(user.id, user);
Amith Yamasani2a003292012-08-14 18:25:45 -0700504 if (user.isGuest()) {
505 mGuestEnabled = true;
506 }
507 if (mNextSerialNumber < 0 || mNextSerialNumber <= user.id) {
508 mNextSerialNumber = user.id + 1;
509 }
Amith Yamasani258848d2012-08-10 17:06:33 -0700510 }
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700511 }
512 }
Amith Yamasani13593602012-03-22 16:16:17 -0700513 updateUserIdsLocked();
Amith Yamasani6f34b412012-10-22 18:19:27 -0700514 upgradeIfNecessary();
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700515 } catch (IOException ioe) {
Amith Yamasani13593602012-03-22 16:16:17 -0700516 fallbackToSingleUserLocked();
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700517 } catch (XmlPullParserException pe) {
Amith Yamasani13593602012-03-22 16:16:17 -0700518 fallbackToSingleUserLocked();
Dianne Hackbornbfd89b32011-12-15 18:22:54 -0800519 } finally {
520 if (fis != null) {
521 try {
522 fis.close();
523 } catch (IOException e) {
524 }
525 }
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700526 }
527 }
528
Amith Yamasani6f34b412012-10-22 18:19:27 -0700529 /**
Amith Yamasanibc9625052012-11-15 14:39:18 -0800530 * Upgrade steps between versions, either for fixing bugs or changing the data format.
Amith Yamasani6f34b412012-10-22 18:19:27 -0700531 */
532 private void upgradeIfNecessary() {
533 int userVersion = mUserVersion;
534 if (userVersion < 1) {
535 // Assign a proper name for the owner, if not initialized correctly before
536 UserInfo user = mUsers.get(UserHandle.USER_OWNER);
537 if ("Primary".equals(user.name)) {
538 user.name = mContext.getResources().getString(com.android.internal.R.string.owner_name);
539 writeUserLocked(user);
540 }
541 userVersion = 1;
542 }
543
Amith Yamasanibc9625052012-11-15 14:39:18 -0800544 if (userVersion < 2) {
545 // Owner should be marked as initialized
546 UserInfo user = mUsers.get(UserHandle.USER_OWNER);
547 if ((user.flags & UserInfo.FLAG_INITIALIZED) == 0) {
548 user.flags |= UserInfo.FLAG_INITIALIZED;
549 writeUserLocked(user);
550 }
551 userVersion = 2;
552 }
553
Amith Yamasani6f34b412012-10-22 18:19:27 -0700554 if (userVersion < USER_VERSION) {
555 Slog.w(LOG_TAG, "User version " + mUserVersion + " didn't upgrade as expected to "
556 + USER_VERSION);
557 } else {
558 mUserVersion = userVersion;
559 writeUserListLocked();
560 }
561 }
562
Amith Yamasani13593602012-03-22 16:16:17 -0700563 private void fallbackToSingleUserLocked() {
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700564 // Create the primary user
Amith Yamasani67df64b2012-12-14 12:09:36 -0800565 UserInfo primary = new UserInfo(UserHandle.USER_OWNER,
Amith Yamasani6f34b412012-10-22 18:19:27 -0700566 mContext.getResources().getString(com.android.internal.R.string.owner_name), null,
Amith Yamasani756901d2012-10-12 12:30:07 -0700567 UserInfo.FLAG_ADMIN | UserInfo.FLAG_PRIMARY | UserInfo.FLAG_INITIALIZED);
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700568 mUsers.put(0, primary);
Amith Yamasani634cf312012-10-04 17:34:21 -0700569 mNextSerialNumber = MIN_USER_ID;
Amith Yamasani67df64b2012-12-14 12:09:36 -0800570
Geoffrey Borggaarde45e45e32013-01-24 10:03:20 -0500571 Bundle restrictions = new Bundle();
Amith Yamasani67df64b2012-12-14 12:09:36 -0800572 mUserRestrictions.append(UserHandle.USER_OWNER, restrictions);
573
Amith Yamasani13593602012-03-22 16:16:17 -0700574 updateUserIdsLocked();
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700575
Amith Yamasani13593602012-03-22 16:16:17 -0700576 writeUserListLocked();
577 writeUserLocked(primary);
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700578 }
579
580 /*
581 * Writes the user file in this format:
582 *
583 * <user flags="20039023" id="0">
584 * <name>Primary</name>
585 * </user>
586 */
Amith Yamasani13593602012-03-22 16:16:17 -0700587 private void writeUserLocked(UserInfo userInfo) {
Amith Yamasani742a6712011-05-04 14:49:28 -0700588 FileOutputStream fos = null;
Amith Yamasani2a003292012-08-14 18:25:45 -0700589 AtomicFile userFile = new AtomicFile(new File(mUsersDir, userInfo.id + ".xml"));
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700590 try {
Amith Yamasani2a003292012-08-14 18:25:45 -0700591 fos = userFile.startWrite();
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700592 final BufferedOutputStream bos = new BufferedOutputStream(fos);
593
594 // XmlSerializer serializer = XmlUtils.serializerInstance();
595 final XmlSerializer serializer = new FastXmlSerializer();
596 serializer.setOutput(bos, "utf-8");
597 serializer.startDocument(null, true);
598 serializer.setFeature("http://xmlpull.org/v1/doc/features.html#indent-output", true);
599
600 serializer.startTag(null, TAG_USER);
601 serializer.attribute(null, ATTR_ID, Integer.toString(userInfo.id));
Amith Yamasani2a003292012-08-14 18:25:45 -0700602 serializer.attribute(null, ATTR_SERIAL_NO, Integer.toString(userInfo.serialNumber));
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700603 serializer.attribute(null, ATTR_FLAGS, Integer.toString(userInfo.flags));
Amith Yamasani920ace02012-09-20 22:15:37 -0700604 serializer.attribute(null, ATTR_CREATION_TIME, Long.toString(userInfo.creationTime));
605 serializer.attribute(null, ATTR_LAST_LOGGED_IN_TIME,
606 Long.toString(userInfo.lastLoggedInTime));
Amith Yamasanib8151ec2012-04-18 18:02:48 -0700607 if (userInfo.iconPath != null) {
608 serializer.attribute(null, ATTR_ICON_PATH, userInfo.iconPath);
609 }
Dianne Hackbornd4ac8d72012-09-27 23:20:10 -0700610 if (userInfo.partial) {
611 serializer.attribute(null, ATTR_PARTIAL, "true");
612 }
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700613
614 serializer.startTag(null, TAG_NAME);
615 serializer.text(userInfo.name);
616 serializer.endTag(null, TAG_NAME);
617
Amith Yamasanie4cf7342012-12-17 11:12:09 -0800618 Bundle restrictions = mUserRestrictions.get(userInfo.id);
619 if (restrictions != null) {
620 serializer.startTag(null, TAG_RESTRICTIONS);
Amith Yamasani71e6c692013-03-24 17:39:28 -0700621 writeBoolean(serializer, restrictions, UserManager.DISALLOW_CONFIG_WIFI);
622 writeBoolean(serializer, restrictions, UserManager.DISALLOW_MODIFY_ACCOUNTS);
623 writeBoolean(serializer, restrictions, UserManager.DISALLOW_INSTALL_APPS);
624 writeBoolean(serializer, restrictions, UserManager.DISALLOW_UNINSTALL_APPS);
625 writeBoolean(serializer, restrictions, UserManager.DISALLOW_SHARE_LOCATION);
Maggie Benthalla12fccf2013-03-14 18:02:12 -0400626 writeBoolean(serializer, restrictions,
627 UserManager.DISALLOW_INSTALL_UNKNOWN_SOURCES);
628 writeBoolean(serializer, restrictions, UserManager.DISALLOW_CONFIG_BLUETOOTH);
629 writeBoolean(serializer, restrictions, UserManager.DISALLOW_USB_FILE_TRANSFER);
Emily Bernierb223f732013-04-11 15:46:36 -0400630 writeBoolean(serializer, restrictions, UserManager.DISALLOW_CONFIG_CREDENTIALS);
631 writeBoolean(serializer, restrictions, UserManager.DISALLOW_REMOVE_USER);
Amith Yamasanie4cf7342012-12-17 11:12:09 -0800632 serializer.endTag(null, TAG_RESTRICTIONS);
633 }
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700634 serializer.endTag(null, TAG_USER);
635
636 serializer.endDocument();
Amith Yamasani2a003292012-08-14 18:25:45 -0700637 userFile.finishWrite(fos);
638 } catch (Exception ioe) {
Amith Yamasani0b285492011-04-14 17:35:23 -0700639 Slog.e(LOG_TAG, "Error writing user info " + userInfo.id + "\n" + ioe);
Amith Yamasani2a003292012-08-14 18:25:45 -0700640 userFile.failWrite(fos);
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700641 }
642 }
643
644 /*
645 * Writes the user list file in this format:
646 *
Amith Yamasani2a003292012-08-14 18:25:45 -0700647 * <users nextSerialNumber="3">
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700648 * <user id="0"></user>
649 * <user id="2"></user>
650 * </users>
651 */
Amith Yamasani13593602012-03-22 16:16:17 -0700652 private void writeUserListLocked() {
Amith Yamasani742a6712011-05-04 14:49:28 -0700653 FileOutputStream fos = null;
Amith Yamasani2a003292012-08-14 18:25:45 -0700654 AtomicFile userListFile = new AtomicFile(mUserListFile);
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700655 try {
Amith Yamasani2a003292012-08-14 18:25:45 -0700656 fos = userListFile.startWrite();
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700657 final BufferedOutputStream bos = new BufferedOutputStream(fos);
658
659 // XmlSerializer serializer = XmlUtils.serializerInstance();
660 final XmlSerializer serializer = new FastXmlSerializer();
661 serializer.setOutput(bos, "utf-8");
662 serializer.startDocument(null, true);
663 serializer.setFeature("http://xmlpull.org/v1/doc/features.html#indent-output", true);
664
665 serializer.startTag(null, TAG_USERS);
Amith Yamasani2a003292012-08-14 18:25:45 -0700666 serializer.attribute(null, ATTR_NEXT_SERIAL_NO, Integer.toString(mNextSerialNumber));
Amith Yamasani6f34b412012-10-22 18:19:27 -0700667 serializer.attribute(null, ATTR_USER_VERSION, Integer.toString(mUserVersion));
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700668
669 for (int i = 0; i < mUsers.size(); i++) {
670 UserInfo user = mUsers.valueAt(i);
671 serializer.startTag(null, TAG_USER);
672 serializer.attribute(null, ATTR_ID, Integer.toString(user.id));
673 serializer.endTag(null, TAG_USER);
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700674 }
675
676 serializer.endTag(null, TAG_USERS);
677
678 serializer.endDocument();
Amith Yamasani2a003292012-08-14 18:25:45 -0700679 userListFile.finishWrite(fos);
680 } catch (Exception e) {
681 userListFile.failWrite(fos);
Amith Yamasani0b285492011-04-14 17:35:23 -0700682 Slog.e(LOG_TAG, "Error writing user list");
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700683 }
684 }
685
Amith Yamasanie4cf7342012-12-17 11:12:09 -0800686 private UserInfo readUserLocked(int id) {
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700687 int flags = 0;
Amith Yamasani2a003292012-08-14 18:25:45 -0700688 int serialNumber = id;
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700689 String name = null;
Amith Yamasanib8151ec2012-04-18 18:02:48 -0700690 String iconPath = null;
Amith Yamasani920ace02012-09-20 22:15:37 -0700691 long creationTime = 0L;
692 long lastLoggedInTime = 0L;
Dianne Hackbornd4ac8d72012-09-27 23:20:10 -0700693 boolean partial = false;
Amith Yamasanie4cf7342012-12-17 11:12:09 -0800694 Bundle restrictions = new Bundle();
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700695
696 FileInputStream fis = null;
697 try {
Amith Yamasani2a003292012-08-14 18:25:45 -0700698 AtomicFile userFile =
699 new AtomicFile(new File(mUsersDir, Integer.toString(id) + ".xml"));
700 fis = userFile.openRead();
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700701 XmlPullParser parser = Xml.newPullParser();
702 parser.setInput(fis, null);
703 int type;
704 while ((type = parser.next()) != XmlPullParser.START_TAG
705 && type != XmlPullParser.END_DOCUMENT) {
706 ;
707 }
708
709 if (type != XmlPullParser.START_TAG) {
Amith Yamasani0b285492011-04-14 17:35:23 -0700710 Slog.e(LOG_TAG, "Unable to read user " + id);
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700711 return null;
712 }
713
714 if (type == XmlPullParser.START_TAG && parser.getName().equals(TAG_USER)) {
Amith Yamasani920ace02012-09-20 22:15:37 -0700715 int storedId = readIntAttribute(parser, ATTR_ID, -1);
716 if (storedId != id) {
Amith Yamasani0b285492011-04-14 17:35:23 -0700717 Slog.e(LOG_TAG, "User id does not match the file name");
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700718 return null;
719 }
Amith Yamasani920ace02012-09-20 22:15:37 -0700720 serialNumber = readIntAttribute(parser, ATTR_SERIAL_NO, id);
721 flags = readIntAttribute(parser, ATTR_FLAGS, 0);
Amith Yamasanib8151ec2012-04-18 18:02:48 -0700722 iconPath = parser.getAttributeValue(null, ATTR_ICON_PATH);
Amith Yamasani920ace02012-09-20 22:15:37 -0700723 creationTime = readLongAttribute(parser, ATTR_CREATION_TIME, 0);
724 lastLoggedInTime = readLongAttribute(parser, ATTR_LAST_LOGGED_IN_TIME, 0);
Dianne Hackbornd4ac8d72012-09-27 23:20:10 -0700725 String valueString = parser.getAttributeValue(null, ATTR_PARTIAL);
726 if ("true".equals(valueString)) {
727 partial = true;
728 }
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700729
Amith Yamasanie4cf7342012-12-17 11:12:09 -0800730 int outerDepth = parser.getDepth();
731 while ((type = parser.next()) != XmlPullParser.END_DOCUMENT
732 && (type != XmlPullParser.END_TAG || parser.getDepth() > outerDepth)) {
733 if (type == XmlPullParser.END_TAG || type == XmlPullParser.TEXT) {
734 continue;
735 }
736 String tag = parser.getName();
737 if (TAG_NAME.equals(tag)) {
738 type = parser.next();
739 if (type == XmlPullParser.TEXT) {
740 name = parser.getText();
741 }
742 } else if (TAG_RESTRICTIONS.equals(tag)) {
Amith Yamasani71e6c692013-03-24 17:39:28 -0700743 readBoolean(parser, restrictions, UserManager.DISALLOW_CONFIG_WIFI);
744 readBoolean(parser, restrictions, UserManager.DISALLOW_MODIFY_ACCOUNTS);
745 readBoolean(parser, restrictions, UserManager.DISALLOW_INSTALL_APPS);
746 readBoolean(parser, restrictions, UserManager.DISALLOW_UNINSTALL_APPS);
747 readBoolean(parser, restrictions, UserManager.DISALLOW_SHARE_LOCATION);
Maggie Benthalla12fccf2013-03-14 18:02:12 -0400748 readBoolean(parser, restrictions,
749 UserManager.DISALLOW_INSTALL_UNKNOWN_SOURCES);
750 readBoolean(parser, restrictions, UserManager.DISALLOW_CONFIG_BLUETOOTH);
751 readBoolean(parser, restrictions, UserManager.DISALLOW_USB_FILE_TRANSFER);
Emily Bernierb223f732013-04-11 15:46:36 -0400752 readBoolean(parser, restrictions, UserManager.DISALLOW_CONFIG_CREDENTIALS);
753 readBoolean(parser, restrictions, UserManager.DISALLOW_REMOVE_USER);
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700754 }
755 }
756 }
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700757
Amith Yamasanib8151ec2012-04-18 18:02:48 -0700758 UserInfo userInfo = new UserInfo(id, name, iconPath, flags);
Amith Yamasani2a003292012-08-14 18:25:45 -0700759 userInfo.serialNumber = serialNumber;
Amith Yamasani920ace02012-09-20 22:15:37 -0700760 userInfo.creationTime = creationTime;
761 userInfo.lastLoggedInTime = lastLoggedInTime;
Dianne Hackbornd4ac8d72012-09-27 23:20:10 -0700762 userInfo.partial = partial;
Amith Yamasanie4cf7342012-12-17 11:12:09 -0800763 mUserRestrictions.append(id, restrictions);
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700764 return userInfo;
765
766 } catch (IOException ioe) {
767 } catch (XmlPullParserException pe) {
Dianne Hackbornbfd89b32011-12-15 18:22:54 -0800768 } finally {
769 if (fis != null) {
770 try {
771 fis.close();
772 } catch (IOException e) {
773 }
774 }
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700775 }
776 return null;
777 }
778
Amith Yamasanie4cf7342012-12-17 11:12:09 -0800779 private void readBoolean(XmlPullParser parser, Bundle restrictions,
780 String restrictionKey) {
781 String value = parser.getAttributeValue(null, restrictionKey);
Amith Yamasani71e6c692013-03-24 17:39:28 -0700782 if (value != null) {
783 restrictions.putBoolean(restrictionKey, Boolean.parseBoolean(value));
784 }
Amith Yamasanie4cf7342012-12-17 11:12:09 -0800785 }
786
787 private void writeBoolean(XmlSerializer xml, Bundle restrictions, String restrictionKey)
788 throws IOException {
789 if (restrictions.containsKey(restrictionKey)) {
790 xml.attribute(null, restrictionKey,
791 Boolean.toString(restrictions.getBoolean(restrictionKey)));
792 }
793 }
794
Amith Yamasani920ace02012-09-20 22:15:37 -0700795 private int readIntAttribute(XmlPullParser parser, String attr, int defaultValue) {
796 String valueString = parser.getAttributeValue(null, attr);
797 if (valueString == null) return defaultValue;
798 try {
799 return Integer.parseInt(valueString);
800 } catch (NumberFormatException nfe) {
801 return defaultValue;
802 }
803 }
804
805 private long readLongAttribute(XmlPullParser parser, String attr, long defaultValue) {
806 String valueString = parser.getAttributeValue(null, attr);
807 if (valueString == null) return defaultValue;
808 try {
809 return Long.parseLong(valueString);
810 } catch (NumberFormatException nfe) {
811 return defaultValue;
812 }
813 }
814
Amith Yamasani258848d2012-08-10 17:06:33 -0700815 @Override
Amith Yamasani13593602012-03-22 16:16:17 -0700816 public UserInfo createUser(String name, int flags) {
Amith Yamasani2a003292012-08-14 18:25:45 -0700817 checkManageUsersPermission("Only the system can create users");
Amith Yamasanifaea76f2012-09-11 10:59:48 -0700818
Dianne Hackborn5dc5a002012-09-15 19:33:48 -0700819 final long ident = Binder.clearCallingIdentity();
820 final UserInfo userInfo;
821 try {
822 synchronized (mInstallLock) {
823 synchronized (mPackagesLock) {
824 if (isUserLimitReachedLocked()) return null;
825 int userId = getNextAvailableIdLocked();
826 userInfo = new UserInfo(userId, name, null, flags);
827 File userPath = new File(mBaseUserPath, Integer.toString(userId));
828 userInfo.serialNumber = mNextSerialNumber++;
Amith Yamasani920ace02012-09-20 22:15:37 -0700829 long now = System.currentTimeMillis();
830 userInfo.creationTime = (now > EPOCH_PLUS_30_YEARS) ? now : 0;
Dianne Hackbornd4ac8d72012-09-27 23:20:10 -0700831 userInfo.partial = true;
Amith Yamasani16389312012-10-17 21:20:14 -0700832 Environment.getUserSystemDirectory(userInfo.id).mkdirs();
Dianne Hackborn5dc5a002012-09-15 19:33:48 -0700833 mUsers.put(userId, userInfo);
834 writeUserListLocked();
835 writeUserLocked(userInfo);
Dianne Hackborn5dc5a002012-09-15 19:33:48 -0700836 mPm.createNewUserLILPw(userId, userPath);
Dianne Hackbornd4ac8d72012-09-27 23:20:10 -0700837 userInfo.partial = false;
838 writeUserLocked(userInfo);
839 updateUserIdsLocked();
Geoffrey Borggaarde45e45e32013-01-24 10:03:20 -0500840 Bundle restrictions = new Bundle();
Geoffrey Borggaarde45e45e32013-01-24 10:03:20 -0500841 mUserRestrictions.append(userId, restrictions);
Dianne Hackborn5dc5a002012-09-15 19:33:48 -0700842 }
Dianne Hackborn4428e172012-08-24 17:43:05 -0700843 }
Dianne Hackborn5dc5a002012-09-15 19:33:48 -0700844 if (userInfo != null) {
845 Intent addedIntent = new Intent(Intent.ACTION_USER_ADDED);
846 addedIntent.putExtra(Intent.EXTRA_USER_HANDLE, userInfo.id);
847 mContext.sendBroadcastAsUser(addedIntent, UserHandle.ALL,
848 android.Manifest.permission.MANAGE_USERS);
849 }
850 } finally {
851 Binder.restoreCallingIdentity(ident);
Amith Yamasani258848d2012-08-10 17:06:33 -0700852 }
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700853 return userInfo;
854 }
855
Amith Yamasani0b285492011-04-14 17:35:23 -0700856 /**
857 * Removes a user and all data directories created for that user. This method should be called
858 * after the user's processes have been terminated.
859 * @param id the user's id
860 */
Amith Yamasani258848d2012-08-10 17:06:33 -0700861 public boolean removeUser(int userHandle) {
Amith Yamasani2a003292012-08-14 18:25:45 -0700862 checkManageUsersPermission("Only the system can remove users");
Dianne Hackborn80a4af22012-08-27 19:18:31 -0700863 final UserInfo user;
864 synchronized (mPackagesLock) {
865 user = mUsers.get(userHandle);
866 if (userHandle == 0 || user == null) {
867 return false;
868 }
Jeff Sharkeyffe0cb42012-11-05 17:24:43 -0800869 mRemovingUserIds.put(userHandle, true);
Amith Yamasani756901d2012-10-12 12:30:07 -0700870 // Set this to a partially created user, so that the user will be purged
871 // on next startup, in case the runtime stops now before stopping and
872 // removing the user completely.
873 user.partial = true;
874 writeUserLocked(user);
Dianne Hackborn80a4af22012-08-27 19:18:31 -0700875 }
Amith Yamasani16389312012-10-17 21:20:14 -0700876 if (DBG) Slog.i(LOG_TAG, "Stopping user " + userHandle);
Dianne Hackborn80a4af22012-08-27 19:18:31 -0700877 int res;
878 try {
879 res = ActivityManagerNative.getDefault().stopUser(userHandle,
880 new IStopUserCallback.Stub() {
881 @Override
882 public void userStopped(int userId) {
883 finishRemoveUser(userId);
884 }
885 @Override
886 public void userStopAborted(int userId) {
887 }
888 });
889 } catch (RemoteException e) {
890 return false;
891 }
892
893 return res == ActivityManager.USER_OP_SUCCESS;
894 }
895
Amith Yamasanidb6a14c2012-10-17 21:16:52 -0700896 void finishRemoveUser(final int userHandle) {
Amith Yamasani16389312012-10-17 21:20:14 -0700897 if (DBG) Slog.i(LOG_TAG, "finishRemoveUser " + userHandle);
Amith Yamasanidb6a14c2012-10-17 21:16:52 -0700898 // Let other services shutdown any activity and clean up their state before completely
899 // wiping the user's system directory and removing from the user list
Dianne Hackborn5dc5a002012-09-15 19:33:48 -0700900 long ident = Binder.clearCallingIdentity();
901 try {
902 Intent addedIntent = new Intent(Intent.ACTION_USER_REMOVED);
903 addedIntent.putExtra(Intent.EXTRA_USER_HANDLE, userHandle);
Amith Yamasanidb6a14c2012-10-17 21:16:52 -0700904 mContext.sendOrderedBroadcastAsUser(addedIntent, UserHandle.ALL,
905 android.Manifest.permission.MANAGE_USERS,
906
907 new BroadcastReceiver() {
908 @Override
909 public void onReceive(Context context, Intent intent) {
910 if (DBG) {
911 Slog.i(LOG_TAG,
912 "USER_REMOVED broadcast sent, cleaning up user data "
913 + userHandle);
914 }
915 new Thread() {
916 public void run() {
917 synchronized (mInstallLock) {
918 synchronized (mPackagesLock) {
919 removeUserStateLocked(userHandle);
920 }
921 }
922 }
923 }.start();
924 }
925 },
926
927 null, Activity.RESULT_OK, null, null);
Dianne Hackborn5dc5a002012-09-15 19:33:48 -0700928 } finally {
929 Binder.restoreCallingIdentity(ident);
930 }
Amith Yamasani2a003292012-08-14 18:25:45 -0700931 }
932
Jeff Sharkeyffe0cb42012-11-05 17:24:43 -0800933 private void removeUserStateLocked(final int userHandle) {
Dianne Hackbornd4ac8d72012-09-27 23:20:10 -0700934 // Cleanup package manager settings
935 mPm.cleanUpUserLILPw(userHandle);
936
937 // Remove this user from the list
938 mUsers.remove(userHandle);
Jeff Sharkeyffe0cb42012-11-05 17:24:43 -0800939
940 // Have user ID linger for several seconds to let external storage VFS
941 // cache entries expire. This must be greater than the 'entry_valid'
942 // timeout used by the FUSE daemon.
943 mHandler.postDelayed(new Runnable() {
944 @Override
945 public void run() {
946 synchronized (mPackagesLock) {
947 mRemovingUserIds.delete(userHandle);
948 }
949 }
950 }, MINUTE_IN_MILLIS);
951
Dianne Hackbornd4ac8d72012-09-27 23:20:10 -0700952 // Remove user file
953 AtomicFile userFile = new AtomicFile(new File(mUsersDir, userHandle + ".xml"));
954 userFile.delete();
955 // Update the user list
956 writeUserListLocked();
957 updateUserIdsLocked();
958 removeDirectoryRecursive(Environment.getUserSystemDirectory(userHandle));
959 }
960
Amith Yamasani61f57372012-08-31 12:12:28 -0700961 private void removeDirectoryRecursive(File parent) {
962 if (parent.isDirectory()) {
963 String[] files = parent.list();
964 for (String filename : files) {
965 File child = new File(parent, filename);
966 removeDirectoryRecursive(child);
967 }
968 }
969 parent.delete();
970 }
971
Amith Yamasani2a003292012-08-14 18:25:45 -0700972 @Override
Amith Yamasani7e99bc02013-04-16 18:24:51 -0700973 public Bundle getApplicationRestrictions(String packageName) {
974 return getApplicationRestrictionsForUser(packageName, UserHandle.getCallingUserId());
975 }
976
977 @Override
978 public Bundle getApplicationRestrictionsForUser(String packageName, int userId) {
Amith Yamasanidf2e92a2013-03-01 17:04:38 -0800979 if (UserHandle.getCallingUserId() != userId
Amith Yamasani9429afb2013-04-10 18:40:51 -0700980 || !UserHandle.isSameApp(Binder.getCallingUid(), getUidForPackage(packageName))) {
Amith Yamasanidf2e92a2013-03-01 17:04:38 -0800981 checkManageUsersPermission("Only system can get restrictions for other users/apps");
982 }
983 synchronized (mPackagesLock) {
984 // Read the restrictions from XML
985 return readApplicationRestrictionsLocked(packageName, userId);
986 }
987 }
988
989 @Override
Amith Yamasani7e99bc02013-04-16 18:24:51 -0700990 public void setApplicationRestrictions(String packageName, Bundle restrictions,
Amith Yamasanidf2e92a2013-03-01 17:04:38 -0800991 int userId) {
992 if (UserHandle.getCallingUserId() != userId
Amith Yamasani9429afb2013-04-10 18:40:51 -0700993 || !UserHandle.isSameApp(Binder.getCallingUid(), getUidForPackage(packageName))) {
Amith Yamasanidf2e92a2013-03-01 17:04:38 -0800994 checkManageUsersPermission("Only system can set restrictions for other users/apps");
995 }
996 synchronized (mPackagesLock) {
997 // Write the restrictions to XML
Amith Yamasani7e99bc02013-04-16 18:24:51 -0700998 writeApplicationRestrictionsLocked(packageName, restrictions, userId);
Amith Yamasanidf2e92a2013-03-01 17:04:38 -0800999 }
1000 }
1001
1002 private int getUidForPackage(String packageName) {
Amith Yamasani9429afb2013-04-10 18:40:51 -07001003 long ident = Binder.clearCallingIdentity();
Amith Yamasanidf2e92a2013-03-01 17:04:38 -08001004 try {
1005 return mContext.getPackageManager().getApplicationInfo(packageName,
1006 PackageManager.GET_UNINSTALLED_PACKAGES).uid;
1007 } catch (NameNotFoundException nnfe) {
1008 return -1;
Amith Yamasani9429afb2013-04-10 18:40:51 -07001009 } finally {
1010 Binder.restoreCallingIdentity(ident);
Amith Yamasanidf2e92a2013-03-01 17:04:38 -08001011 }
1012 }
1013
Amith Yamasani7e99bc02013-04-16 18:24:51 -07001014 private Bundle readApplicationRestrictionsLocked(String packageName,
Amith Yamasanidf2e92a2013-03-01 17:04:38 -08001015 int userId) {
Amith Yamasani7e99bc02013-04-16 18:24:51 -07001016 final Bundle restrictions = new Bundle();
Amith Yamasanidf2e92a2013-03-01 17:04:38 -08001017 final ArrayList<String> values = new ArrayList<String>();
1018
1019 FileInputStream fis = null;
1020 try {
1021 AtomicFile restrictionsFile =
1022 new AtomicFile(new File(Environment.getUserSystemDirectory(userId),
1023 RESTRICTIONS_FILE_PREFIX + packageName + ".xml"));
1024 fis = restrictionsFile.openRead();
1025 XmlPullParser parser = Xml.newPullParser();
1026 parser.setInput(fis, null);
1027 int type;
1028 while ((type = parser.next()) != XmlPullParser.START_TAG
1029 && type != XmlPullParser.END_DOCUMENT) {
1030 ;
1031 }
1032
1033 if (type != XmlPullParser.START_TAG) {
1034 Slog.e(LOG_TAG, "Unable to read restrictions file "
1035 + restrictionsFile.getBaseFile());
Amith Yamasani7e99bc02013-04-16 18:24:51 -07001036 return restrictions;
Amith Yamasanidf2e92a2013-03-01 17:04:38 -08001037 }
1038
1039 while ((type = parser.next()) != XmlPullParser.END_DOCUMENT) {
1040 if (type == XmlPullParser.START_TAG && parser.getName().equals(TAG_ENTRY)) {
1041 String key = parser.getAttributeValue(null, ATTR_KEY);
Amith Yamasani7e99bc02013-04-16 18:24:51 -07001042 String valType = parser.getAttributeValue(null, ATTR_VALUE_TYPE);
Amith Yamasanidf2e92a2013-03-01 17:04:38 -08001043 String multiple = parser.getAttributeValue(null, ATTR_MULTIPLE);
1044 if (multiple != null) {
1045 int count = Integer.parseInt(multiple);
1046 while (count > 0 && (type = parser.next()) != XmlPullParser.END_DOCUMENT) {
1047 if (type == XmlPullParser.START_TAG
1048 && parser.getName().equals(TAG_VALUE)) {
1049 values.add(parser.nextText().trim());
1050 count--;
1051 }
1052 }
1053 String [] valueStrings = new String[values.size()];
1054 values.toArray(valueStrings);
Amith Yamasani7e99bc02013-04-16 18:24:51 -07001055 restrictions.putStringArray(key, valueStrings);
1056 } else if (ATTR_TYPE_BOOLEAN.equals(valType)) {
1057 restrictions.putBoolean(key, Boolean.parseBoolean(
1058 parser.nextText().trim()));
Amith Yamasanidf2e92a2013-03-01 17:04:38 -08001059 } else {
1060 String value = parser.nextText().trim();
Amith Yamasani7e99bc02013-04-16 18:24:51 -07001061 restrictions.putString(key, value);
Amith Yamasanidf2e92a2013-03-01 17:04:38 -08001062 }
1063 }
1064 }
1065
1066 } catch (IOException ioe) {
1067 } catch (XmlPullParserException pe) {
1068 } finally {
1069 if (fis != null) {
1070 try {
1071 fis.close();
1072 } catch (IOException e) {
1073 }
1074 }
1075 }
Amith Yamasani7e99bc02013-04-16 18:24:51 -07001076 return restrictions;
Amith Yamasanidf2e92a2013-03-01 17:04:38 -08001077 }
1078
1079 private void writeApplicationRestrictionsLocked(String packageName,
Amith Yamasani7e99bc02013-04-16 18:24:51 -07001080 Bundle restrictions, int userId) {
Amith Yamasanidf2e92a2013-03-01 17:04:38 -08001081 FileOutputStream fos = null;
1082 AtomicFile restrictionsFile = new AtomicFile(
1083 new File(Environment.getUserSystemDirectory(userId),
1084 RESTRICTIONS_FILE_PREFIX + packageName + ".xml"));
1085 try {
1086 fos = restrictionsFile.startWrite();
1087 final BufferedOutputStream bos = new BufferedOutputStream(fos);
1088
1089 // XmlSerializer serializer = XmlUtils.serializerInstance();
1090 final XmlSerializer serializer = new FastXmlSerializer();
1091 serializer.setOutput(bos, "utf-8");
1092 serializer.startDocument(null, true);
1093 serializer.setFeature("http://xmlpull.org/v1/doc/features.html#indent-output", true);
1094
1095 serializer.startTag(null, TAG_RESTRICTIONS);
1096
Amith Yamasani7e99bc02013-04-16 18:24:51 -07001097 for (String key : restrictions.keySet()) {
1098 Object value = restrictions.get(key);
Amith Yamasanidf2e92a2013-03-01 17:04:38 -08001099 serializer.startTag(null, TAG_ENTRY);
Amith Yamasani7e99bc02013-04-16 18:24:51 -07001100 serializer.attribute(null, ATTR_KEY, key);
1101
1102 if (value instanceof Boolean) {
1103 serializer.attribute(null, ATTR_VALUE_TYPE, ATTR_TYPE_BOOLEAN);
1104 serializer.text(value.toString());
1105 } else if (value == null || value instanceof String) {
1106 serializer.attribute(null, ATTR_VALUE_TYPE, ATTR_TYPE_STRING);
1107 serializer.text(value != null ? (String) value : "");
Amith Yamasanidf2e92a2013-03-01 17:04:38 -08001108 } else {
Amith Yamasani7e99bc02013-04-16 18:24:51 -07001109 serializer.attribute(null, ATTR_VALUE_TYPE, ATTR_TYPE_STRING_ARRAY);
1110 String[] values = (String[]) value;
Amith Yamasanidf2e92a2013-03-01 17:04:38 -08001111 serializer.attribute(null, ATTR_MULTIPLE, Integer.toString(values.length));
Amith Yamasani7e99bc02013-04-16 18:24:51 -07001112 for (String choice : values) {
Amith Yamasanidf2e92a2013-03-01 17:04:38 -08001113 serializer.startTag(null, TAG_VALUE);
Amith Yamasani7e99bc02013-04-16 18:24:51 -07001114 serializer.text(choice != null ? choice : "");
Amith Yamasanidf2e92a2013-03-01 17:04:38 -08001115 serializer.endTag(null, TAG_VALUE);
1116 }
1117 }
1118 serializer.endTag(null, TAG_ENTRY);
1119 }
1120
1121 serializer.endTag(null, TAG_RESTRICTIONS);
1122
1123 serializer.endDocument();
1124 restrictionsFile.finishWrite(fos);
1125 } catch (Exception e) {
1126 restrictionsFile.failWrite(fos);
1127 Slog.e(LOG_TAG, "Error writing application restrictions list");
1128 }
1129 }
1130
1131 @Override
Amith Yamasani2a003292012-08-14 18:25:45 -07001132 public int getUserSerialNumber(int userHandle) {
Dianne Hackborn4428e172012-08-24 17:43:05 -07001133 synchronized (mPackagesLock) {
Amith Yamasani2a003292012-08-14 18:25:45 -07001134 if (!exists(userHandle)) return -1;
Amith Yamasani195263742012-08-21 15:40:12 -07001135 return getUserInfoLocked(userHandle).serialNumber;
Amith Yamasani2a003292012-08-14 18:25:45 -07001136 }
1137 }
1138
1139 @Override
1140 public int getUserHandle(int userSerialNumber) {
Dianne Hackborn4428e172012-08-24 17:43:05 -07001141 synchronized (mPackagesLock) {
Amith Yamasani2a003292012-08-14 18:25:45 -07001142 for (int userId : mUserIds) {
Amith Yamasani195263742012-08-21 15:40:12 -07001143 if (getUserInfoLocked(userId).serialNumber == userSerialNumber) return userId;
Amith Yamasani2a003292012-08-14 18:25:45 -07001144 }
1145 // Not found
1146 return -1;
Amith Yamasani13593602012-03-22 16:16:17 -07001147 }
1148 }
1149
Amith Yamasani0b285492011-04-14 17:35:23 -07001150 /**
1151 * Caches the list of user ids in an array, adjusting the array size when necessary.
1152 */
Amith Yamasani13593602012-03-22 16:16:17 -07001153 private void updateUserIdsLocked() {
Dianne Hackbornd4ac8d72012-09-27 23:20:10 -07001154 int num = 0;
Amith Yamasani0b285492011-04-14 17:35:23 -07001155 for (int i = 0; i < mUsers.size(); i++) {
Dianne Hackbornd4ac8d72012-09-27 23:20:10 -07001156 if (!mUsers.valueAt(i).partial) {
1157 num++;
1158 }
1159 }
Amith Yamasani16389312012-10-17 21:20:14 -07001160 final int[] newUsers = new int[num];
1161 int n = 0;
Dianne Hackbornd4ac8d72012-09-27 23:20:10 -07001162 for (int i = 0; i < mUsers.size(); i++) {
1163 if (!mUsers.valueAt(i).partial) {
Amith Yamasani16389312012-10-17 21:20:14 -07001164 newUsers[n++] = mUsers.keyAt(i);
Dianne Hackbornd4ac8d72012-09-27 23:20:10 -07001165 }
Amith Yamasani0b285492011-04-14 17:35:23 -07001166 }
Dianne Hackborn7767eac2012-08-23 18:25:40 -07001167 mUserIds = newUsers;
Amith Yamasani0b285492011-04-14 17:35:23 -07001168 }
1169
1170 /**
Amith Yamasani920ace02012-09-20 22:15:37 -07001171 * Make a note of the last started time of a user.
1172 * @param userId the user that was just foregrounded
1173 */
1174 public void userForeground(int userId) {
1175 synchronized (mPackagesLock) {
1176 UserInfo user = mUsers.get(userId);
1177 long now = System.currentTimeMillis();
Dianne Hackbornd4ac8d72012-09-27 23:20:10 -07001178 if (user == null || user.partial) {
1179 Slog.w(LOG_TAG, "userForeground: unknown user #" + userId);
1180 return;
1181 }
1182 if (now > EPOCH_PLUS_30_YEARS) {
Amith Yamasani920ace02012-09-20 22:15:37 -07001183 user.lastLoggedInTime = now;
1184 writeUserLocked(user);
1185 }
1186 }
1187 }
1188
1189 /**
Amith Yamasani0b285492011-04-14 17:35:23 -07001190 * Returns the next available user id, filling in any holes in the ids.
Amith Yamasani742a6712011-05-04 14:49:28 -07001191 * TODO: May not be a good idea to recycle ids, in case it results in confusion
1192 * for data and battery stats collection, or unexpected cross-talk.
Amith Yamasani0b285492011-04-14 17:35:23 -07001193 * @return
1194 */
Dianne Hackborn5dc5a002012-09-15 19:33:48 -07001195 private int getNextAvailableIdLocked() {
Dianne Hackborn4428e172012-08-24 17:43:05 -07001196 synchronized (mPackagesLock) {
Jeff Sharkeyffe0cb42012-11-05 17:24:43 -08001197 int i = MIN_USER_ID;
Amith Yamasani195263742012-08-21 15:40:12 -07001198 while (i < Integer.MAX_VALUE) {
Jeff Sharkeyffe0cb42012-11-05 17:24:43 -08001199 if (mUsers.indexOfKey(i) < 0 && !mRemovingUserIds.get(i)) {
Amith Yamasani195263742012-08-21 15:40:12 -07001200 break;
1201 }
1202 i++;
Amith Yamasani4b2e9342011-03-31 12:38:53 -07001203 }
Amith Yamasani195263742012-08-21 15:40:12 -07001204 return i;
Amith Yamasani4b2e9342011-03-31 12:38:53 -07001205 }
Amith Yamasani4b2e9342011-03-31 12:38:53 -07001206 }
Amith Yamasani920ace02012-09-20 22:15:37 -07001207
1208 @Override
1209 protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
1210 if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.DUMP)
1211 != PackageManager.PERMISSION_GRANTED) {
1212 pw.println("Permission Denial: can't dump UserManager from from pid="
1213 + Binder.getCallingPid()
1214 + ", uid=" + Binder.getCallingUid()
1215 + " without permission "
1216 + android.Manifest.permission.DUMP);
1217 return;
1218 }
1219
1220 long now = System.currentTimeMillis();
1221 StringBuilder sb = new StringBuilder();
1222 synchronized (mPackagesLock) {
1223 pw.println("Users:");
1224 for (int i = 0; i < mUsers.size(); i++) {
1225 UserInfo user = mUsers.valueAt(i);
1226 if (user == null) continue;
Amith Yamasani634cf312012-10-04 17:34:21 -07001227 pw.print(" "); pw.print(user); pw.print(" serialNo="); pw.print(user.serialNumber);
Jeff Sharkeyffe0cb42012-11-05 17:24:43 -08001228 if (mRemovingUserIds.get(mUsers.keyAt(i))) pw.print(" <removing> ");
Dianne Hackbornd4ac8d72012-09-27 23:20:10 -07001229 if (user.partial) pw.print(" <partial>");
1230 pw.println();
Amith Yamasani920ace02012-09-20 22:15:37 -07001231 pw.print(" Created: ");
1232 if (user.creationTime == 0) {
1233 pw.println("<unknown>");
1234 } else {
1235 sb.setLength(0);
1236 TimeUtils.formatDuration(now - user.creationTime, sb);
1237 sb.append(" ago");
1238 pw.println(sb);
1239 }
1240 pw.print(" Last logged in: ");
1241 if (user.lastLoggedInTime == 0) {
1242 pw.println("<unknown>");
1243 } else {
1244 sb.setLength(0);
1245 TimeUtils.formatDuration(now - user.lastLoggedInTime, sb);
1246 sb.append(" ago");
1247 pw.println(sb);
1248 }
1249 }
1250 }
1251 }
Amith Yamasani4b2e9342011-03-31 12:38:53 -07001252}