blob: dbfd7c7bab20247182684471db3b4e92cc119bab [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
Amith Yamasanidb6a14c2012-10-17 21:16:52 -070019import android.app.Activity;
Amith Yamasani2a003292012-08-14 18:25:45 -070020import android.app.ActivityManager;
Dianne Hackborn80a4af22012-08-27 19:18:31 -070021import android.app.ActivityManagerNative;
22import android.app.IStopUserCallback;
Amith Yamasanidb6a14c2012-10-17 21:16:52 -070023import android.content.BroadcastReceiver;
Amith Yamasani258848d2012-08-10 17:06:33 -070024import android.content.Context;
25import android.content.Intent;
Amith Yamasani1a7472e2013-07-02 11:17:30 -070026import android.content.pm.ApplicationInfo;
Amith Yamasani0b285492011-04-14 17:35:23 -070027import android.content.pm.PackageManager;
Amith Yamasanidf2e92a2013-03-01 17:04:38 -080028import android.content.pm.PackageManager.NameNotFoundException;
Amith Yamasani4b2e9342011-03-31 12:38:53 -070029import android.content.pm.UserInfo;
Amith Yamasanie928d7d2012-09-17 21:46:51 -070030import android.graphics.Bitmap;
Amith Yamasani258848d2012-08-10 17:06:33 -070031import android.os.Binder;
Amith Yamasanie4cf7342012-12-17 11:12:09 -080032import android.os.Bundle;
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;
Fyodor Kupolov75d0ea82014-12-15 16:21:07 -080037import android.os.Message;
Adrian Roos1bdff912015-02-17 15:51:35 +010038import android.os.ParcelFileDescriptor;
Amith Yamasani258848d2012-08-10 17:06:33 -070039import android.os.Process;
Dianne Hackborn80a4af22012-08-27 19:18:31 -070040import android.os.RemoteException;
Jason Monk62062992014-05-06 09:55:28 -040041import android.os.ServiceManager;
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 Yamasani655d0e22013-06-12 14:19:10 -070045import android.util.Log;
Amith Yamasani4b2e9342011-03-31 12:38:53 -070046import android.util.Slog;
47import android.util.SparseArray;
Jeff Sharkeyffe0cb42012-11-05 17:24:43 -080048import android.util.SparseBooleanArray;
Amith Yamasani920ace02012-09-20 22:15:37 -070049import android.util.TimeUtils;
Amith Yamasani4b2e9342011-03-31 12:38:53 -070050import android.util.Xml;
51
Jason Monk62062992014-05-06 09:55:28 -040052import com.android.internal.app.IAppOpsService;
Jeff Sharkeyffe0cb42012-11-05 17:24:43 -080053import com.android.internal.util.ArrayUtils;
54import com.android.internal.util.FastXmlSerializer;
55
56import org.xmlpull.v1.XmlPullParser;
57import org.xmlpull.v1.XmlPullParserException;
58import org.xmlpull.v1.XmlSerializer;
59
Amith Yamasani4b2e9342011-03-31 12:38:53 -070060import java.io.BufferedOutputStream;
61import java.io.File;
Amith Yamasani920ace02012-09-20 22:15:37 -070062import java.io.FileDescriptor;
Amith Yamasani4b2e9342011-03-31 12:38:53 -070063import java.io.FileInputStream;
Amith Yamasanib8151ec2012-04-18 18:02:48 -070064import java.io.FileNotFoundException;
Amith Yamasani4b2e9342011-03-31 12:38:53 -070065import java.io.FileOutputStream;
66import java.io.IOException;
Amith Yamasani920ace02012-09-20 22:15:37 -070067import java.io.PrintWriter;
Amith Yamasani655d0e22013-06-12 14:19:10 -070068import java.security.MessageDigest;
69import java.security.NoSuchAlgorithmException;
70import java.security.SecureRandom;
Amith Yamasani4b2e9342011-03-31 12:38:53 -070071import java.util.ArrayList;
72import java.util.List;
73
Amith Yamasani258848d2012-08-10 17:06:33 -070074public class UserManagerService extends IUserManager.Stub {
Amith Yamasanib8151ec2012-04-18 18:02:48 -070075
Amith Yamasani2a003292012-08-14 18:25:45 -070076 private static final String LOG_TAG = "UserManagerService";
Amith Yamasanib8151ec2012-04-18 18:02:48 -070077
Amith Yamasani16389312012-10-17 21:20:14 -070078 private static final boolean DBG = false;
79
Amith Yamasani4b2e9342011-03-31 12:38:53 -070080 private static final String TAG_NAME = "name";
Amith Yamasani4b2e9342011-03-31 12:38:53 -070081 private static final String ATTR_FLAGS = "flags";
Amith Yamasanib8151ec2012-04-18 18:02:48 -070082 private static final String ATTR_ICON_PATH = "icon";
Amith Yamasani4b2e9342011-03-31 12:38:53 -070083 private static final String ATTR_ID = "id";
Amith Yamasani920ace02012-09-20 22:15:37 -070084 private static final String ATTR_CREATION_TIME = "created";
85 private static final String ATTR_LAST_LOGGED_IN_TIME = "lastLoggedIn";
Amith Yamasani655d0e22013-06-12 14:19:10 -070086 private static final String ATTR_SALT = "salt";
87 private static final String ATTR_PIN_HASH = "pinHash";
88 private static final String ATTR_FAILED_ATTEMPTS = "failedAttempts";
89 private static final String ATTR_LAST_RETRY_MS = "lastAttemptMs";
Amith Yamasani2a003292012-08-14 18:25:45 -070090 private static final String ATTR_SERIAL_NO = "serialNumber";
91 private static final String ATTR_NEXT_SERIAL_NO = "nextSerialNumber";
Dianne Hackbornd4ac8d72012-09-27 23:20:10 -070092 private static final String ATTR_PARTIAL = "partial";
Adam Lesinskieddeb492014-09-08 17:50:03 -070093 private static final String ATTR_GUEST_TO_REMOVE = "guestToRemove";
Amith Yamasani6f34b412012-10-22 18:19:27 -070094 private static final String ATTR_USER_VERSION = "version";
Kenny Guy2a764942014-04-02 13:29:20 +010095 private static final String ATTR_PROFILE_GROUP_ID = "profileGroupId";
Amith Yamasanie4afaa32014-06-30 14:55:07 +053096 private static final String TAG_GUEST_RESTRICTIONS = "guestRestrictions";
Amith Yamasani4b2e9342011-03-31 12:38:53 -070097 private static final String TAG_USERS = "users";
Amith Yamasani4b2e9342011-03-31 12:38:53 -070098 private static final String TAG_USER = "user";
Amith Yamasanie4cf7342012-12-17 11:12:09 -080099 private static final String TAG_RESTRICTIONS = "restrictions";
Amith Yamasanidf2e92a2013-03-01 17:04:38 -0800100 private static final String TAG_ENTRY = "entry";
101 private static final String TAG_VALUE = "value";
102 private static final String ATTR_KEY = "key";
Amith Yamasani7e99bc02013-04-16 18:24:51 -0700103 private static final String ATTR_VALUE_TYPE = "type";
Amith Yamasanidf2e92a2013-03-01 17:04:38 -0800104 private static final String ATTR_MULTIPLE = "m";
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700105
Amith Yamasani7e99bc02013-04-16 18:24:51 -0700106 private static final String ATTR_TYPE_STRING_ARRAY = "sa";
107 private static final String ATTR_TYPE_STRING = "s";
108 private static final String ATTR_TYPE_BOOLEAN = "b";
Amith Yamasani5b5aa402014-06-01 20:10:14 -0700109 private static final String ATTR_TYPE_INTEGER = "i";
Amith Yamasani7e99bc02013-04-16 18:24:51 -0700110
Amith Yamasani0b285492011-04-14 17:35:23 -0700111 private static final String USER_INFO_DIR = "system" + File.separator + "users";
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700112 private static final String USER_LIST_FILENAME = "userlist.xml";
Amith Yamasanib8151ec2012-04-18 18:02:48 -0700113 private static final String USER_PHOTO_FILENAME = "photo.png";
Adrian Roos1bdff912015-02-17 15:51:35 +0100114 private static final String USER_PHOTO_FILENAME_TMP = USER_PHOTO_FILENAME + ".tmp";
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700115
Amith Yamasanidf2e92a2013-03-01 17:04:38 -0800116 private static final String RESTRICTIONS_FILE_PREFIX = "res_";
Amith Yamasanifc95e702013-09-26 13:20:17 -0700117 private static final String XML_SUFFIX = ".xml";
Amith Yamasanidf2e92a2013-03-01 17:04:38 -0800118
Amith Yamasani634cf312012-10-04 17:34:21 -0700119 private static final int MIN_USER_ID = 10;
120
Amith Yamasaniaa6634e2014-10-06 14:20:28 -0700121 private static final int USER_VERSION = 5;
Amith Yamasani6f34b412012-10-22 18:19:27 -0700122
Amith Yamasani920ace02012-09-20 22:15:37 -0700123 private static final long EPOCH_PLUS_30_YEARS = 30L * 365 * 24 * 60 * 60 * 1000L; // ms
124
Amith Yamasani655d0e22013-06-12 14:19:10 -0700125 // Number of attempts before jumping to the next BACKOFF_TIMES slot
126 private static final int BACKOFF_INC_INTERVAL = 5;
127
Amith Yamasani95ab7842014-08-11 17:09:26 -0700128 // Maximum number of managed profiles permitted is 1. This cannot be increased
129 // without first making sure that the rest of the framework is prepared for it.
130 private static final int MAX_MANAGED_PROFILES = 1;
131
Amith Yamasani655d0e22013-06-12 14:19:10 -0700132 // Amount of time to force the user to wait before entering the PIN again, after failing
133 // BACKOFF_INC_INTERVAL times.
134 private static final int[] BACKOFF_TIMES = { 0, 30*1000, 60*1000, 5*60*1000, 30*60*1000 };
135
Fyodor Kupolov75d0ea82014-12-15 16:21:07 -0800136 static final int WRITE_USER_MSG = 1;
137 static final int WRITE_USER_DELAY = 2*1000; // 2 seconds
Amith Yamasanie4afaa32014-06-30 14:55:07 +0530138
Dianne Hackborn4428e172012-08-24 17:43:05 -0700139 private final Context mContext;
140 private final PackageManagerService mPm;
141 private final Object mInstallLock;
142 private final Object mPackagesLock;
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700143
Jeff Sharkeyffe0cb42012-11-05 17:24:43 -0800144 private final Handler mHandler;
145
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700146 private final File mUsersDir;
147 private final File mUserListFile;
Dianne Hackborn4428e172012-08-24 17:43:05 -0700148 private final File mBaseUserPath;
149
Jeff Sharkeyffe0cb42012-11-05 17:24:43 -0800150 private final SparseArray<UserInfo> mUsers = new SparseArray<UserInfo>();
Amith Yamasanie4cf7342012-12-17 11:12:09 -0800151 private final SparseArray<Bundle> mUserRestrictions = new SparseArray<Bundle>();
Amith Yamasanie4afaa32014-06-30 14:55:07 +0530152 private final Bundle mGuestRestrictions = new Bundle();
Jeff Sharkeyffe0cb42012-11-05 17:24:43 -0800153
Amith Yamasani655d0e22013-06-12 14:19:10 -0700154 class RestrictionsPinState {
155 long salt;
156 String pinHash;
157 int failedAttempts;
158 long lastAttemptTime;
159 }
160
161 private final SparseArray<RestrictionsPinState> mRestrictionsPinStates =
162 new SparseArray<RestrictionsPinState>();
163
Jeff Sharkeyffe0cb42012-11-05 17:24:43 -0800164 /**
165 * Set of user IDs being actively removed. Removed IDs linger in this set
166 * for several seconds to work around a VFS caching issue.
167 */
168 // @GuardedBy("mPackagesLock")
169 private final SparseBooleanArray mRemovingUserIds = new SparseBooleanArray();
Dianne Hackborn4428e172012-08-24 17:43:05 -0700170
Amith Yamasani0b285492011-04-14 17:35:23 -0700171 private int[] mUserIds;
Amith Yamasani2a003292012-08-14 18:25:45 -0700172 private int mNextSerialNumber;
Amith Yamasani6f34b412012-10-22 18:19:27 -0700173 private int mUserVersion = 0;
Amith Yamasani0b285492011-04-14 17:35:23 -0700174
Jason Monk62062992014-05-06 09:55:28 -0400175 private IAppOpsService mAppOpsService;
176
Amith Yamasani258848d2012-08-10 17:06:33 -0700177 private static UserManagerService sInstance;
Amith Yamasani258848d2012-08-10 17:06:33 -0700178
Dianne Hackborn4428e172012-08-24 17:43:05 -0700179 public static UserManagerService getInstance() {
180 synchronized (UserManagerService.class) {
181 return sInstance;
Amith Yamasani258848d2012-08-10 17:06:33 -0700182 }
Amith Yamasani258848d2012-08-10 17:06:33 -0700183 }
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700184
185 /**
186 * Available for testing purposes.
187 */
Amith Yamasani258848d2012-08-10 17:06:33 -0700188 UserManagerService(File dataDir, File baseUserPath) {
Dianne Hackborn4428e172012-08-24 17:43:05 -0700189 this(null, null, new Object(), new Object(), dataDir, baseUserPath);
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700190 }
191
Dianne Hackborn4428e172012-08-24 17:43:05 -0700192 /**
193 * Called by package manager to create the service. This is closely
194 * associated with the package manager, and the given lock is the
195 * package manager's own lock.
196 */
197 UserManagerService(Context context, PackageManagerService pm,
198 Object installLock, Object packagesLock) {
199 this(context, pm, installLock, packagesLock,
200 Environment.getDataDirectory(),
201 new File(Environment.getDataDirectory(), "user"));
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700202 }
203
Dianne Hackborn4428e172012-08-24 17:43:05 -0700204 /**
205 * Available for testing purposes.
206 */
207 private UserManagerService(Context context, PackageManagerService pm,
208 Object installLock, Object packagesLock,
209 File dataDir, File baseUserPath) {
Dianne Hackbornd4ac8d72012-09-27 23:20:10 -0700210 mContext = context;
211 mPm = pm;
212 mInstallLock = installLock;
213 mPackagesLock = packagesLock;
Fyodor Kupolov75d0ea82014-12-15 16:21:07 -0800214 mHandler = new MainHandler();
Dianne Hackbornd4ac8d72012-09-27 23:20:10 -0700215 synchronized (mInstallLock) {
216 synchronized (mPackagesLock) {
217 mUsersDir = new File(dataDir, USER_INFO_DIR);
218 mUsersDir.mkdirs();
219 // Make zeroth user directory, for services to migrate their files to that location
220 File userZeroDir = new File(mUsersDir, "0");
221 userZeroDir.mkdirs();
222 mBaseUserPath = baseUserPath;
223 FileUtils.setPermissions(mUsersDir.toString(),
224 FileUtils.S_IRWXU|FileUtils.S_IRWXG
225 |FileUtils.S_IROTH|FileUtils.S_IXOTH,
226 -1, -1);
227 mUserListFile = new File(mUsersDir, USER_LIST_FILENAME);
Amith Yamasanida0b1682014-11-21 12:58:17 -0800228 initDefaultGuestRestrictions();
Dianne Hackbornd4ac8d72012-09-27 23:20:10 -0700229 readUserListLocked();
Amith Yamasani756901d2012-10-12 12:30:07 -0700230 // Prune out any partially created/partially removed users.
Dianne Hackbornd4ac8d72012-09-27 23:20:10 -0700231 ArrayList<UserInfo> partials = new ArrayList<UserInfo>();
232 for (int i = 0; i < mUsers.size(); i++) {
233 UserInfo ui = mUsers.valueAt(i);
Adam Lesinskieddeb492014-09-08 17:50:03 -0700234 if ((ui.partial || ui.guestToRemove) && i != 0) {
Dianne Hackbornd4ac8d72012-09-27 23:20:10 -0700235 partials.add(ui);
236 }
237 }
238 for (int i = 0; i < partials.size(); i++) {
239 UserInfo ui = partials.get(i);
240 Slog.w(LOG_TAG, "Removing partially created user #" + i
241 + " (name=" + ui.name + ")");
242 removeUserStateLocked(ui.id);
243 }
244 sInstance = this;
245 }
Dianne Hackborn4428e172012-08-24 17:43:05 -0700246 }
Amith Yamasani1a7472e2013-07-02 11:17:30 -0700247 }
248
249 void systemReady() {
Amith Yamasani1a7472e2013-07-02 11:17:30 -0700250 userForeground(UserHandle.USER_OWNER);
Jason Monk62062992014-05-06 09:55:28 -0400251 mAppOpsService = IAppOpsService.Stub.asInterface(
252 ServiceManager.getService(Context.APP_OPS_SERVICE));
253 for (int i = 0; i < mUserIds.length; ++i) {
254 try {
255 mAppOpsService.setUserRestrictions(mUserRestrictions.get(mUserIds[i]), mUserIds[i]);
256 } catch (RemoteException e) {
257 Log.w(LOG_TAG, "Unable to notify AppOpsService of UserRestrictions");
258 }
259 }
Amith Yamasani258848d2012-08-10 17:06:33 -0700260 }
261
262 @Override
Amith Yamasani920ace02012-09-20 22:15:37 -0700263 public List<UserInfo> getUsers(boolean excludeDying) {
Amith Yamasani2a003292012-08-14 18:25:45 -0700264 checkManageUsersPermission("query users");
Dianne Hackborn4428e172012-08-24 17:43:05 -0700265 synchronized (mPackagesLock) {
Amith Yamasani13593602012-03-22 16:16:17 -0700266 ArrayList<UserInfo> users = new ArrayList<UserInfo>(mUsers.size());
267 for (int i = 0; i < mUsers.size(); i++) {
Dianne Hackbornd4ac8d72012-09-27 23:20:10 -0700268 UserInfo ui = mUsers.valueAt(i);
269 if (ui.partial) {
270 continue;
271 }
Jeff Sharkeyffe0cb42012-11-05 17:24:43 -0800272 if (!excludeDying || !mRemovingUserIds.get(ui.id)) {
Dianne Hackbornd4ac8d72012-09-27 23:20:10 -0700273 users.add(ui);
Amith Yamasani920ace02012-09-20 22:15:37 -0700274 }
Amith Yamasani13593602012-03-22 16:16:17 -0700275 }
276 return users;
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700277 }
Amith Yamasani13593602012-03-22 16:16:17 -0700278 }
279
Amith Yamasani258848d2012-08-10 17:06:33 -0700280 @Override
Alexandra Gherghina385124d2014-04-03 13:37:39 +0100281 public List<UserInfo> getProfiles(int userId, boolean enabledOnly) {
Amith Yamasani4f582632014-02-19 14:31:52 -0800282 if (userId != UserHandle.getCallingUserId()) {
283 checkManageUsersPermission("getting profiles related to user " + userId);
284 }
Amith Yamasanibe465322014-04-24 13:45:17 -0700285 final long ident = Binder.clearCallingIdentity();
286 try {
287 synchronized (mPackagesLock) {
288 return getProfilesLocked(userId, enabledOnly);
Alexandra Gherghina385124d2014-04-03 13:37:39 +0100289 }
Amith Yamasanibe465322014-04-24 13:45:17 -0700290 } finally {
291 Binder.restoreCallingIdentity(ident);
Kenny Guya52dc3e2014-02-11 15:33:14 +0000292 }
293 }
294
Amith Yamasanibe465322014-04-24 13:45:17 -0700295 /** Assume permissions already checked and caller's identity cleared */
296 private List<UserInfo> getProfilesLocked(int userId, boolean enabledOnly) {
Amith Yamasanibe465322014-04-24 13:45:17 -0700297 UserInfo user = getUserInfoLocked(userId);
298 ArrayList<UserInfo> users = new ArrayList<UserInfo>(mUsers.size());
Amith Yamasanidda003f2014-08-28 18:06:51 -0700299 if (user == null) {
300 // Probably a dying user
301 return users;
302 }
Amith Yamasanibe465322014-04-24 13:45:17 -0700303 for (int i = 0; i < mUsers.size(); i++) {
304 UserInfo profile = mUsers.valueAt(i);
305 if (!isProfileOf(user, profile)) {
306 continue;
307 }
Alexandra Gherghinadf35d572014-04-09 13:54:39 +0100308 if (enabledOnly && !profile.isEnabled()) {
309 continue;
Amith Yamasanibe465322014-04-24 13:45:17 -0700310 }
Amith Yamasani70fcf0c2014-07-11 08:40:19 -0700311 if (mRemovingUserIds.get(profile.id)) {
312 continue;
313 }
Amith Yamasanibe465322014-04-24 13:45:17 -0700314 users.add(profile);
315 }
316 return users;
317 }
318
Jessica Hummelbe81c802014-04-22 15:49:22 +0100319 @Override
320 public UserInfo getProfileParent(int userHandle) {
321 checkManageUsersPermission("get the profile parent");
322 synchronized (mPackagesLock) {
323 UserInfo profile = getUserInfoLocked(userHandle);
Dianne Hackborn29cd7f12015-01-08 10:37:05 -0800324 if (profile == null) {
325 return null;
326 }
Jessica Hummelbe81c802014-04-22 15:49:22 +0100327 int parentUserId = profile.profileGroupId;
328 if (parentUserId == UserInfo.NO_PROFILE_GROUP_ID) {
329 return null;
330 } else {
331 return getUserInfoLocked(parentUserId);
332 }
333 }
334 }
335
Kenny Guy2a764942014-04-02 13:29:20 +0100336 private boolean isProfileOf(UserInfo user, UserInfo profile) {
337 return user.id == profile.id ||
338 (user.profileGroupId != UserInfo.NO_PROFILE_GROUP_ID
339 && user.profileGroupId == profile.profileGroupId);
Kenny Guya52dc3e2014-02-11 15:33:14 +0000340 }
341
342 @Override
Alexandra Gherghinadf35d572014-04-09 13:54:39 +0100343 public void setUserEnabled(int userId) {
344 checkManageUsersPermission("enable user");
345 synchronized (mPackagesLock) {
346 UserInfo info = getUserInfoLocked(userId);
347 if (info != null && !info.isEnabled()) {
348 info.flags ^= UserInfo.FLAG_DISABLED;
349 writeUserLocked(info);
350 }
351 }
352 }
353
354 @Override
Amith Yamasani258848d2012-08-10 17:06:33 -0700355 public UserInfo getUserInfo(int userId) {
Amith Yamasani2a003292012-08-14 18:25:45 -0700356 checkManageUsersPermission("query user");
Dianne Hackborn4428e172012-08-24 17:43:05 -0700357 synchronized (mPackagesLock) {
Amith Yamasani195263742012-08-21 15:40:12 -0700358 return getUserInfoLocked(userId);
Amith Yamasani13593602012-03-22 16:16:17 -0700359 }
360 }
361
Amith Yamasani71e6c692013-03-24 17:39:28 -0700362 @Override
363 public boolean isRestricted() {
364 synchronized (mPackagesLock) {
365 return getUserInfoLocked(UserHandle.getCallingUserId()).isRestricted();
366 }
367 }
368
Amith Yamasani195263742012-08-21 15:40:12 -0700369 /*
370 * Should be locked on mUsers before calling this.
371 */
372 private UserInfo getUserInfoLocked(int userId) {
Dianne Hackbornd4ac8d72012-09-27 23:20:10 -0700373 UserInfo ui = mUsers.get(userId);
Amith Yamasani16389312012-10-17 21:20:14 -0700374 // If it is partial and not in the process of being removed, return as unknown user.
Jeff Sharkeyffe0cb42012-11-05 17:24:43 -0800375 if (ui != null && ui.partial && !mRemovingUserIds.get(userId)) {
Dianne Hackbornd4ac8d72012-09-27 23:20:10 -0700376 Slog.w(LOG_TAG, "getUserInfo: unknown user #" + userId);
377 return null;
378 }
379 return ui;
Amith Yamasani195263742012-08-21 15:40:12 -0700380 }
381
Amith Yamasani13593602012-03-22 16:16:17 -0700382 public boolean exists(int userId) {
Dianne Hackborn4428e172012-08-24 17:43:05 -0700383 synchronized (mPackagesLock) {
Amith Yamasani13593602012-03-22 16:16:17 -0700384 return ArrayUtils.contains(mUserIds, userId);
385 }
386 }
387
Amith Yamasani258848d2012-08-10 17:06:33 -0700388 @Override
Amith Yamasanib8151ec2012-04-18 18:02:48 -0700389 public void setUserName(int userId, String name) {
Amith Yamasani2a003292012-08-14 18:25:45 -0700390 checkManageUsersPermission("rename users");
Dianne Hackbornd4ac8d72012-09-27 23:20:10 -0700391 boolean changed = false;
Dianne Hackborn4428e172012-08-24 17:43:05 -0700392 synchronized (mPackagesLock) {
Amith Yamasani13593602012-03-22 16:16:17 -0700393 UserInfo info = mUsers.get(userId);
Dianne Hackbornd4ac8d72012-09-27 23:20:10 -0700394 if (info == null || info.partial) {
395 Slog.w(LOG_TAG, "setUserName: unknown user #" + userId);
396 return;
397 }
Amith Yamasani13593602012-03-22 16:16:17 -0700398 if (name != null && !name.equals(info.name)) {
399 info.name = name;
400 writeUserLocked(info);
Dianne Hackbornd4ac8d72012-09-27 23:20:10 -0700401 changed = true;
Amith Yamasani13593602012-03-22 16:16:17 -0700402 }
403 }
Dianne Hackbornd4ac8d72012-09-27 23:20:10 -0700404 if (changed) {
405 sendUserInfoChangedBroadcast(userId);
406 }
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700407 }
408
Amith Yamasani258848d2012-08-10 17:06:33 -0700409 @Override
Amith Yamasanie928d7d2012-09-17 21:46:51 -0700410 public void setUserIcon(int userId, Bitmap bitmap) {
Amith Yamasani2a003292012-08-14 18:25:45 -0700411 checkManageUsersPermission("update users");
Jason Monk9a944532014-07-08 09:31:21 -0400412 long ident = Binder.clearCallingIdentity();
413 try {
414 synchronized (mPackagesLock) {
415 UserInfo info = mUsers.get(userId);
416 if (info == null || info.partial) {
417 Slog.w(LOG_TAG, "setUserIcon: unknown user #" + userId);
418 return;
419 }
420 writeBitmapLocked(info, bitmap);
421 writeUserLocked(info);
Dianne Hackbornd4ac8d72012-09-27 23:20:10 -0700422 }
Jason Monk9a944532014-07-08 09:31:21 -0400423 sendUserInfoChangedBroadcast(userId);
424 } finally {
425 Binder.restoreCallingIdentity(ident);
Amith Yamasanib8151ec2012-04-18 18:02:48 -0700426 }
Amith Yamasanie928d7d2012-09-17 21:46:51 -0700427 }
428
429 private void sendUserInfoChangedBroadcast(int userId) {
430 Intent changedIntent = new Intent(Intent.ACTION_USER_INFO_CHANGED);
431 changedIntent.putExtra(Intent.EXTRA_USER_HANDLE, userId);
432 changedIntent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY);
Amith Yamasani6fc1d4e2013-05-08 16:43:58 -0700433 mContext.sendBroadcastAsUser(changedIntent, UserHandle.ALL);
Amith Yamasanib8151ec2012-04-18 18:02:48 -0700434 }
435
Amith Yamasani258848d2012-08-10 17:06:33 -0700436 @Override
Adrian Roos1bdff912015-02-17 15:51:35 +0100437 public ParcelFileDescriptor getUserIcon(int userId) {
438 String iconPath;
Amith Yamasani3b49f072012-09-17 10:21:43 -0700439 synchronized (mPackagesLock) {
440 UserInfo info = mUsers.get(userId);
Dianne Hackbornd4ac8d72012-09-27 23:20:10 -0700441 if (info == null || info.partial) {
442 Slog.w(LOG_TAG, "getUserIcon: unknown user #" + userId);
443 return null;
444 }
Nicolas Prevot88cc3462014-05-14 14:51:48 +0100445 int callingGroupId = mUsers.get(UserHandle.getCallingUserId()).profileGroupId;
446 if (callingGroupId == UserInfo.NO_PROFILE_GROUP_ID
447 || callingGroupId != info.profileGroupId) {
448 checkManageUsersPermission("get the icon of a user who is not related");
449 }
Dianne Hackbornd4ac8d72012-09-27 23:20:10 -0700450 if (info.iconPath == null) {
451 return null;
452 }
Adrian Roos1bdff912015-02-17 15:51:35 +0100453 iconPath = info.iconPath;
Amith Yamasani3b49f072012-09-17 10:21:43 -0700454 }
Adrian Roos1bdff912015-02-17 15:51:35 +0100455
456 try {
457 return ParcelFileDescriptor.open(
458 new File(iconPath), ParcelFileDescriptor.MODE_READ_ONLY);
459 } catch (FileNotFoundException e) {
460 Log.e(LOG_TAG, "Couldn't find icon file", e);
461 }
462 return null;
Amith Yamasani3b49f072012-09-17 10:21:43 -0700463 }
464
Dianne Hackborn5dc5a002012-09-15 19:33:48 -0700465 public void makeInitialized(int userId) {
466 checkManageUsersPermission("makeInitialized");
467 synchronized (mPackagesLock) {
468 UserInfo info = mUsers.get(userId);
Dianne Hackbornd4ac8d72012-09-27 23:20:10 -0700469 if (info == null || info.partial) {
470 Slog.w(LOG_TAG, "makeInitialized: unknown user #" + userId);
471 }
472 if ((info.flags&UserInfo.FLAG_INITIALIZED) == 0) {
Dianne Hackborn5dc5a002012-09-15 19:33:48 -0700473 info.flags |= UserInfo.FLAG_INITIALIZED;
Fyodor Kupolov75d0ea82014-12-15 16:21:07 -0800474 scheduleWriteUserLocked(info);
Dianne Hackborn5dc5a002012-09-15 19:33:48 -0700475 }
476 }
477 }
478
Amith Yamasaniaa6634e2014-10-06 14:20:28 -0700479 /**
480 * If default guest restrictions haven't been initialized yet, add the basic
481 * restrictions.
482 */
483 private void initDefaultGuestRestrictions() {
484 if (mGuestRestrictions.isEmpty()) {
485 mGuestRestrictions.putBoolean(UserManager.DISALLOW_OUTGOING_CALLS, true);
Amith Yamasanida0b1682014-11-21 12:58:17 -0800486 mGuestRestrictions.putBoolean(UserManager.DISALLOW_SMS, true);
Amith Yamasaniaa6634e2014-10-06 14:20:28 -0700487 }
488 }
489
Amith Yamasanie4cf7342012-12-17 11:12:09 -0800490 @Override
Amith Yamasanie4afaa32014-06-30 14:55:07 +0530491 public Bundle getDefaultGuestRestrictions() {
492 checkManageUsersPermission("getDefaultGuestRestrictions");
493 synchronized (mPackagesLock) {
494 return new Bundle(mGuestRestrictions);
495 }
496 }
497
498 @Override
499 public void setDefaultGuestRestrictions(Bundle restrictions) {
500 checkManageUsersPermission("setDefaultGuestRestrictions");
501 synchronized (mPackagesLock) {
502 mGuestRestrictions.clear();
503 mGuestRestrictions.putAll(restrictions);
504 writeUserListLocked();
505 }
506 }
507
508 @Override
Amith Yamasani8cd28b52014-06-08 17:54:27 -0700509 public boolean hasUserRestriction(String restrictionKey, int userId) {
510 synchronized (mPackagesLock) {
511 Bundle restrictions = mUserRestrictions.get(userId);
512 return restrictions != null ? restrictions.getBoolean(restrictionKey) : false;
513 }
514 }
515
516 @Override
Amith Yamasanie4cf7342012-12-17 11:12:09 -0800517 public Bundle getUserRestrictions(int userId) {
518 // checkManageUsersPermission("getUserRestrictions");
519
520 synchronized (mPackagesLock) {
521 Bundle restrictions = mUserRestrictions.get(userId);
Amith Yamasanibe465322014-04-24 13:45:17 -0700522 return restrictions != null ? new Bundle(restrictions) : new Bundle();
Amith Yamasanie4cf7342012-12-17 11:12:09 -0800523 }
524 }
525
526 @Override
527 public void setUserRestrictions(Bundle restrictions, int userId) {
Amith Yamasanibe465322014-04-24 13:45:17 -0700528 checkManageUsersPermission("setUserRestrictions");
Amith Yamasani0343ec32013-07-22 14:52:06 -0700529 if (restrictions == null) return;
Amith Yamasanie4cf7342012-12-17 11:12:09 -0800530
531 synchronized (mPackagesLock) {
Amith Yamasani350962c2013-08-06 11:18:53 -0700532 mUserRestrictions.get(userId).clear();
Amith Yamasanie4cf7342012-12-17 11:12:09 -0800533 mUserRestrictions.get(userId).putAll(restrictions);
Jason Monk62062992014-05-06 09:55:28 -0400534 long token = Binder.clearCallingIdentity();
535 try {
536 mAppOpsService.setUserRestrictions(mUserRestrictions.get(userId), userId);
537 } catch (RemoteException e) {
538 Log.w(LOG_TAG, "Unable to notify AppOpsService of UserRestrictions");
539 } finally {
540 Binder.restoreCallingIdentity(token);
541 }
Fyodor Kupolov75d0ea82014-12-15 16:21:07 -0800542 scheduleWriteUserLocked(mUsers.get(userId));
Amith Yamasanie4cf7342012-12-17 11:12:09 -0800543 }
544 }
545
Amith Yamasani258848d2012-08-10 17:06:33 -0700546 /**
Amith Yamasanifaea76f2012-09-11 10:59:48 -0700547 * Check if we've hit the limit of how many users can be created.
548 */
Dianne Hackborn5dc5a002012-09-15 19:33:48 -0700549 private boolean isUserLimitReachedLocked() {
Amith Yamasanif584f012014-05-19 17:57:25 -0700550 int aliveUserCount = 0;
551 final int totalUserCount = mUsers.size();
552 // Skip over users being removed
553 for (int i = 0; i < totalUserCount; i++) {
554 UserInfo user = mUsers.valueAt(i);
Amith Yamasani95ab7842014-08-11 17:09:26 -0700555 if (!mRemovingUserIds.get(user.id)
Amith Yamasani1df14732014-08-29 21:37:27 -0700556 && !user.isGuest() && !user.partial) {
Amith Yamasanif584f012014-05-19 17:57:25 -0700557 aliveUserCount++;
558 }
559 }
560 return aliveUserCount >= UserManager.getMaxSupportedUsers();
Amith Yamasanifaea76f2012-09-11 10:59:48 -0700561 }
562
563 /**
Amith Yamasani195263742012-08-21 15:40:12 -0700564 * Enforces that only the system UID or root's UID or apps that have the
Dianne Hackborn10ad9822014-03-17 11:28:36 -0700565 * {@link android.Manifest.permission#MANAGE_USERS MANAGE_USERS}
Amith Yamasani195263742012-08-21 15:40:12 -0700566 * permission can make certain calls to the UserManager.
Amith Yamasani258848d2012-08-10 17:06:33 -0700567 *
568 * @param message used as message if SecurityException is thrown
569 * @throws SecurityException if the caller is not system or root
570 */
Amith Yamasanibe465322014-04-24 13:45:17 -0700571 private static final void checkManageUsersPermission(String message) {
Amith Yamasani258848d2012-08-10 17:06:33 -0700572 final int uid = Binder.getCallingUid();
Amith Yamasanibe465322014-04-24 13:45:17 -0700573 if (uid != Process.SYSTEM_UID && uid != 0
Emily Bernier7a2b4d12014-04-23 12:51:35 -0400574 && ActivityManager.checkComponentPermission(
575 android.Manifest.permission.MANAGE_USERS,
Amith Yamasanibe465322014-04-24 13:45:17 -0700576 uid, -1, true) != PackageManager.PERMISSION_GRANTED) {
577 throw new SecurityException("You need MANAGE_USERS permission to: " + message);
578 }
Emily Bernier7a2b4d12014-04-23 12:51:35 -0400579 }
580
Amith Yamasanie928d7d2012-09-17 21:46:51 -0700581 private void writeBitmapLocked(UserInfo info, Bitmap bitmap) {
Amith Yamasanib8151ec2012-04-18 18:02:48 -0700582 try {
583 File dir = new File(mUsersDir, Integer.toString(info.id));
584 File file = new File(dir, USER_PHOTO_FILENAME);
Adrian Roos1bdff912015-02-17 15:51:35 +0100585 File tmp = new File(dir, USER_PHOTO_FILENAME_TMP);
Amith Yamasanib8151ec2012-04-18 18:02:48 -0700586 if (!dir.exists()) {
587 dir.mkdir();
588 FileUtils.setPermissions(
589 dir.getPath(),
590 FileUtils.S_IRWXU|FileUtils.S_IRWXG|FileUtils.S_IXOTH,
591 -1, -1);
592 }
Amith Yamasanie928d7d2012-09-17 21:46:51 -0700593 FileOutputStream os;
Adrian Roos1bdff912015-02-17 15:51:35 +0100594 if (bitmap.compress(Bitmap.CompressFormat.PNG, 100, os = new FileOutputStream(tmp))
595 && tmp.renameTo(file)) {
Amith Yamasani3b49f072012-09-17 10:21:43 -0700596 info.iconPath = file.getAbsolutePath();
597 }
Amith Yamasanie928d7d2012-09-17 21:46:51 -0700598 try {
599 os.close();
600 } catch (IOException ioe) {
601 // What the ... !
602 }
Adrian Roos1bdff912015-02-17 15:51:35 +0100603 tmp.delete();
Amith Yamasanib8151ec2012-04-18 18:02:48 -0700604 } catch (FileNotFoundException e) {
Amith Yamasani2a003292012-08-14 18:25:45 -0700605 Slog.w(LOG_TAG, "Error setting photo for user ", e);
Amith Yamasanib8151ec2012-04-18 18:02:48 -0700606 }
Amith Yamasanib8151ec2012-04-18 18:02:48 -0700607 }
608
Amith Yamasani0b285492011-04-14 17:35:23 -0700609 /**
610 * Returns an array of user ids. This array is cached here for quick access, so do not modify or
611 * cache it elsewhere.
612 * @return the array of user ids.
613 */
Dianne Hackborn1676c852012-09-10 14:52:30 -0700614 public int[] getUserIds() {
Dianne Hackborn4428e172012-08-24 17:43:05 -0700615 synchronized (mPackagesLock) {
Dianne Hackborn7767eac2012-08-23 18:25:40 -0700616 return mUserIds;
617 }
Amith Yamasani0b285492011-04-14 17:35:23 -0700618 }
619
Dianne Hackborn4428e172012-08-24 17:43:05 -0700620 int[] getUserIdsLPr() {
621 return mUserIds;
622 }
623
Amith Yamasani13593602012-03-22 16:16:17 -0700624 private void readUserListLocked() {
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700625 if (!mUserListFile.exists()) {
Amith Yamasani13593602012-03-22 16:16:17 -0700626 fallbackToSingleUserLocked();
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700627 return;
628 }
629 FileInputStream fis = null;
Amith Yamasani2a003292012-08-14 18:25:45 -0700630 AtomicFile userListFile = new AtomicFile(mUserListFile);
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700631 try {
Amith Yamasani2a003292012-08-14 18:25:45 -0700632 fis = userListFile.openRead();
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700633 XmlPullParser parser = Xml.newPullParser();
634 parser.setInput(fis, null);
635 int type;
636 while ((type = parser.next()) != XmlPullParser.START_TAG
637 && type != XmlPullParser.END_DOCUMENT) {
638 ;
639 }
640
641 if (type != XmlPullParser.START_TAG) {
Amith Yamasani0b285492011-04-14 17:35:23 -0700642 Slog.e(LOG_TAG, "Unable to read user list");
Amith Yamasani13593602012-03-22 16:16:17 -0700643 fallbackToSingleUserLocked();
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700644 return;
645 }
646
Amith Yamasani2a003292012-08-14 18:25:45 -0700647 mNextSerialNumber = -1;
648 if (parser.getName().equals(TAG_USERS)) {
649 String lastSerialNumber = parser.getAttributeValue(null, ATTR_NEXT_SERIAL_NO);
650 if (lastSerialNumber != null) {
651 mNextSerialNumber = Integer.parseInt(lastSerialNumber);
652 }
Amith Yamasani6f34b412012-10-22 18:19:27 -0700653 String versionNumber = parser.getAttributeValue(null, ATTR_USER_VERSION);
654 if (versionNumber != null) {
655 mUserVersion = Integer.parseInt(versionNumber);
656 }
Amith Yamasani2a003292012-08-14 18:25:45 -0700657 }
658
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700659 while ((type = parser.next()) != XmlPullParser.END_DOCUMENT) {
Amith Yamasanie4afaa32014-06-30 14:55:07 +0530660 if (type == XmlPullParser.START_TAG) {
661 final String name = parser.getName();
662 if (name.equals(TAG_USER)) {
663 String id = parser.getAttributeValue(null, ATTR_ID);
664 UserInfo user = readUserLocked(Integer.parseInt(id));
Amith Yamasani6f34b412012-10-22 18:19:27 -0700665
Amith Yamasanie4afaa32014-06-30 14:55:07 +0530666 if (user != null) {
667 mUsers.put(user.id, user);
668 if (mNextSerialNumber < 0 || mNextSerialNumber <= user.id) {
669 mNextSerialNumber = user.id + 1;
670 }
Amith Yamasani2a003292012-08-14 18:25:45 -0700671 }
Amith Yamasanie4afaa32014-06-30 14:55:07 +0530672 } else if (name.equals(TAG_GUEST_RESTRICTIONS)) {
Amith Yamasanida0b1682014-11-21 12:58:17 -0800673 while ((type = parser.next()) != XmlPullParser.END_DOCUMENT
674 && type != XmlPullParser.END_TAG) {
675 if (type == XmlPullParser.START_TAG) {
676 if (parser.getName().equals(TAG_RESTRICTIONS)) {
677 readRestrictionsLocked(parser, mGuestRestrictions);
678 }
679 break;
680 }
681 }
Amith Yamasani258848d2012-08-10 17:06:33 -0700682 }
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700683 }
684 }
Amith Yamasani13593602012-03-22 16:16:17 -0700685 updateUserIdsLocked();
Amith Yamasani350962c2013-08-06 11:18:53 -0700686 upgradeIfNecessaryLocked();
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700687 } catch (IOException ioe) {
Amith Yamasani13593602012-03-22 16:16:17 -0700688 fallbackToSingleUserLocked();
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700689 } catch (XmlPullParserException pe) {
Amith Yamasani13593602012-03-22 16:16:17 -0700690 fallbackToSingleUserLocked();
Dianne Hackbornbfd89b32011-12-15 18:22:54 -0800691 } finally {
692 if (fis != null) {
693 try {
694 fis.close();
695 } catch (IOException e) {
696 }
697 }
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700698 }
699 }
700
Amith Yamasani6f34b412012-10-22 18:19:27 -0700701 /**
Amith Yamasanibc9625052012-11-15 14:39:18 -0800702 * Upgrade steps between versions, either for fixing bugs or changing the data format.
Amith Yamasani6f34b412012-10-22 18:19:27 -0700703 */
Amith Yamasani350962c2013-08-06 11:18:53 -0700704 private void upgradeIfNecessaryLocked() {
Amith Yamasani6f34b412012-10-22 18:19:27 -0700705 int userVersion = mUserVersion;
706 if (userVersion < 1) {
707 // Assign a proper name for the owner, if not initialized correctly before
708 UserInfo user = mUsers.get(UserHandle.USER_OWNER);
709 if ("Primary".equals(user.name)) {
710 user.name = mContext.getResources().getString(com.android.internal.R.string.owner_name);
Fyodor Kupolov75d0ea82014-12-15 16:21:07 -0800711 scheduleWriteUserLocked(user);
Amith Yamasani6f34b412012-10-22 18:19:27 -0700712 }
713 userVersion = 1;
714 }
715
Amith Yamasanibc9625052012-11-15 14:39:18 -0800716 if (userVersion < 2) {
717 // Owner should be marked as initialized
718 UserInfo user = mUsers.get(UserHandle.USER_OWNER);
719 if ((user.flags & UserInfo.FLAG_INITIALIZED) == 0) {
720 user.flags |= UserInfo.FLAG_INITIALIZED;
Fyodor Kupolov75d0ea82014-12-15 16:21:07 -0800721 scheduleWriteUserLocked(user);
Amith Yamasanibc9625052012-11-15 14:39:18 -0800722 }
723 userVersion = 2;
724 }
725
Amith Yamasani350962c2013-08-06 11:18:53 -0700726
Amith Yamasani5e486f52013-08-07 11:06:44 -0700727 if (userVersion < 4) {
Amith Yamasani5e486f52013-08-07 11:06:44 -0700728 userVersion = 4;
729 }
730
Amith Yamasaniaa6634e2014-10-06 14:20:28 -0700731 if (userVersion < 5) {
732 initDefaultGuestRestrictions();
733 userVersion = 5;
734 }
735
Amith Yamasani6f34b412012-10-22 18:19:27 -0700736 if (userVersion < USER_VERSION) {
737 Slog.w(LOG_TAG, "User version " + mUserVersion + " didn't upgrade as expected to "
738 + USER_VERSION);
739 } else {
740 mUserVersion = userVersion;
741 writeUserListLocked();
742 }
743 }
744
Amith Yamasani13593602012-03-22 16:16:17 -0700745 private void fallbackToSingleUserLocked() {
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700746 // Create the primary user
Amith Yamasani67df64b2012-12-14 12:09:36 -0800747 UserInfo primary = new UserInfo(UserHandle.USER_OWNER,
Amith Yamasani6f34b412012-10-22 18:19:27 -0700748 mContext.getResources().getString(com.android.internal.R.string.owner_name), null,
Amith Yamasani756901d2012-10-12 12:30:07 -0700749 UserInfo.FLAG_ADMIN | UserInfo.FLAG_PRIMARY | UserInfo.FLAG_INITIALIZED);
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700750 mUsers.put(0, primary);
Amith Yamasani634cf312012-10-04 17:34:21 -0700751 mNextSerialNumber = MIN_USER_ID;
Geoffrey Borggaard15b8b2c2013-08-28 22:11:10 -0400752 mUserVersion = USER_VERSION;
Amith Yamasani67df64b2012-12-14 12:09:36 -0800753
Geoffrey Borggaarde45e45e32013-01-24 10:03:20 -0500754 Bundle restrictions = new Bundle();
Amith Yamasani67df64b2012-12-14 12:09:36 -0800755 mUserRestrictions.append(UserHandle.USER_OWNER, restrictions);
756
Amith Yamasani13593602012-03-22 16:16:17 -0700757 updateUserIdsLocked();
Amith Yamasaniaa6634e2014-10-06 14:20:28 -0700758 initDefaultGuestRestrictions();
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700759
Amith Yamasani13593602012-03-22 16:16:17 -0700760 writeUserListLocked();
761 writeUserLocked(primary);
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700762 }
763
Fyodor Kupolov75d0ea82014-12-15 16:21:07 -0800764 private void scheduleWriteUserLocked(UserInfo userInfo) {
765 if (!mHandler.hasMessages(WRITE_USER_MSG, userInfo)) {
766 Message msg = mHandler.obtainMessage(WRITE_USER_MSG, userInfo);
767 mHandler.sendMessageDelayed(msg, WRITE_USER_DELAY);
768 }
769 }
770
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700771 /*
772 * Writes the user file in this format:
773 *
774 * <user flags="20039023" id="0">
775 * <name>Primary</name>
776 * </user>
777 */
Amith Yamasani13593602012-03-22 16:16:17 -0700778 private void writeUserLocked(UserInfo userInfo) {
Amith Yamasani742a6712011-05-04 14:49:28 -0700779 FileOutputStream fos = null;
Amith Yamasanifc95e702013-09-26 13:20:17 -0700780 AtomicFile userFile = new AtomicFile(new File(mUsersDir, userInfo.id + XML_SUFFIX));
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700781 try {
Amith Yamasani2a003292012-08-14 18:25:45 -0700782 fos = userFile.startWrite();
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700783 final BufferedOutputStream bos = new BufferedOutputStream(fos);
784
785 // XmlSerializer serializer = XmlUtils.serializerInstance();
786 final XmlSerializer serializer = new FastXmlSerializer();
787 serializer.setOutput(bos, "utf-8");
788 serializer.startDocument(null, true);
789 serializer.setFeature("http://xmlpull.org/v1/doc/features.html#indent-output", true);
790
791 serializer.startTag(null, TAG_USER);
792 serializer.attribute(null, ATTR_ID, Integer.toString(userInfo.id));
Amith Yamasani2a003292012-08-14 18:25:45 -0700793 serializer.attribute(null, ATTR_SERIAL_NO, Integer.toString(userInfo.serialNumber));
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700794 serializer.attribute(null, ATTR_FLAGS, Integer.toString(userInfo.flags));
Amith Yamasani920ace02012-09-20 22:15:37 -0700795 serializer.attribute(null, ATTR_CREATION_TIME, Long.toString(userInfo.creationTime));
796 serializer.attribute(null, ATTR_LAST_LOGGED_IN_TIME,
797 Long.toString(userInfo.lastLoggedInTime));
Amith Yamasani655d0e22013-06-12 14:19:10 -0700798 RestrictionsPinState pinState = mRestrictionsPinStates.get(userInfo.id);
799 if (pinState != null) {
800 if (pinState.salt != 0) {
801 serializer.attribute(null, ATTR_SALT, Long.toString(pinState.salt));
802 }
803 if (pinState.pinHash != null) {
804 serializer.attribute(null, ATTR_PIN_HASH, pinState.pinHash);
805 }
806 if (pinState.failedAttempts != 0) {
807 serializer.attribute(null, ATTR_FAILED_ATTEMPTS,
808 Integer.toString(pinState.failedAttempts));
809 serializer.attribute(null, ATTR_LAST_RETRY_MS,
810 Long.toString(pinState.lastAttemptTime));
811 }
812 }
Amith Yamasanib8151ec2012-04-18 18:02:48 -0700813 if (userInfo.iconPath != null) {
814 serializer.attribute(null, ATTR_ICON_PATH, userInfo.iconPath);
815 }
Dianne Hackbornd4ac8d72012-09-27 23:20:10 -0700816 if (userInfo.partial) {
817 serializer.attribute(null, ATTR_PARTIAL, "true");
818 }
Adam Lesinskieddeb492014-09-08 17:50:03 -0700819 if (userInfo.guestToRemove) {
820 serializer.attribute(null, ATTR_GUEST_TO_REMOVE, "true");
821 }
Kenny Guy2a764942014-04-02 13:29:20 +0100822 if (userInfo.profileGroupId != UserInfo.NO_PROFILE_GROUP_ID) {
823 serializer.attribute(null, ATTR_PROFILE_GROUP_ID,
824 Integer.toString(userInfo.profileGroupId));
Kenny Guya52dc3e2014-02-11 15:33:14 +0000825 }
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700826
827 serializer.startTag(null, TAG_NAME);
828 serializer.text(userInfo.name);
829 serializer.endTag(null, TAG_NAME);
Amith Yamasanie4cf7342012-12-17 11:12:09 -0800830 Bundle restrictions = mUserRestrictions.get(userInfo.id);
831 if (restrictions != null) {
Amith Yamasanie4afaa32014-06-30 14:55:07 +0530832 writeRestrictionsLocked(serializer, restrictions);
Amith Yamasanie4cf7342012-12-17 11:12:09 -0800833 }
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700834 serializer.endTag(null, TAG_USER);
835
836 serializer.endDocument();
Amith Yamasani2a003292012-08-14 18:25:45 -0700837 userFile.finishWrite(fos);
838 } catch (Exception ioe) {
Amith Yamasani0b285492011-04-14 17:35:23 -0700839 Slog.e(LOG_TAG, "Error writing user info " + userInfo.id + "\n" + ioe);
Amith Yamasani2a003292012-08-14 18:25:45 -0700840 userFile.failWrite(fos);
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700841 }
842 }
843
844 /*
845 * Writes the user list file in this format:
846 *
Amith Yamasani2a003292012-08-14 18:25:45 -0700847 * <users nextSerialNumber="3">
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700848 * <user id="0"></user>
849 * <user id="2"></user>
850 * </users>
851 */
Amith Yamasani13593602012-03-22 16:16:17 -0700852 private void writeUserListLocked() {
Amith Yamasani742a6712011-05-04 14:49:28 -0700853 FileOutputStream fos = null;
Amith Yamasani2a003292012-08-14 18:25:45 -0700854 AtomicFile userListFile = new AtomicFile(mUserListFile);
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700855 try {
Amith Yamasani2a003292012-08-14 18:25:45 -0700856 fos = userListFile.startWrite();
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700857 final BufferedOutputStream bos = new BufferedOutputStream(fos);
858
859 // XmlSerializer serializer = XmlUtils.serializerInstance();
860 final XmlSerializer serializer = new FastXmlSerializer();
861 serializer.setOutput(bos, "utf-8");
862 serializer.startDocument(null, true);
863 serializer.setFeature("http://xmlpull.org/v1/doc/features.html#indent-output", true);
864
865 serializer.startTag(null, TAG_USERS);
Amith Yamasani2a003292012-08-14 18:25:45 -0700866 serializer.attribute(null, ATTR_NEXT_SERIAL_NO, Integer.toString(mNextSerialNumber));
Amith Yamasani6f34b412012-10-22 18:19:27 -0700867 serializer.attribute(null, ATTR_USER_VERSION, Integer.toString(mUserVersion));
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700868
Adam Lesinskieddeb492014-09-08 17:50:03 -0700869 serializer.startTag(null, TAG_GUEST_RESTRICTIONS);
Amith Yamasanie4afaa32014-06-30 14:55:07 +0530870 writeRestrictionsLocked(serializer, mGuestRestrictions);
871 serializer.endTag(null, TAG_GUEST_RESTRICTIONS);
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700872 for (int i = 0; i < mUsers.size(); i++) {
873 UserInfo user = mUsers.valueAt(i);
874 serializer.startTag(null, TAG_USER);
875 serializer.attribute(null, ATTR_ID, Integer.toString(user.id));
876 serializer.endTag(null, TAG_USER);
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700877 }
878
879 serializer.endTag(null, TAG_USERS);
880
881 serializer.endDocument();
Amith Yamasani2a003292012-08-14 18:25:45 -0700882 userListFile.finishWrite(fos);
883 } catch (Exception e) {
884 userListFile.failWrite(fos);
Amith Yamasani0b285492011-04-14 17:35:23 -0700885 Slog.e(LOG_TAG, "Error writing user list");
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700886 }
887 }
888
Amith Yamasanie4afaa32014-06-30 14:55:07 +0530889 private void writeRestrictionsLocked(XmlSerializer serializer, Bundle restrictions)
890 throws IOException {
891 serializer.startTag(null, TAG_RESTRICTIONS);
892 writeBoolean(serializer, restrictions, UserManager.DISALLOW_CONFIG_WIFI);
893 writeBoolean(serializer, restrictions, UserManager.DISALLOW_MODIFY_ACCOUNTS);
894 writeBoolean(serializer, restrictions, UserManager.DISALLOW_INSTALL_APPS);
895 writeBoolean(serializer, restrictions, UserManager.DISALLOW_UNINSTALL_APPS);
896 writeBoolean(serializer, restrictions, UserManager.DISALLOW_SHARE_LOCATION);
897 writeBoolean(serializer, restrictions,
898 UserManager.DISALLOW_INSTALL_UNKNOWN_SOURCES);
899 writeBoolean(serializer, restrictions, UserManager.DISALLOW_CONFIG_BLUETOOTH);
900 writeBoolean(serializer, restrictions, UserManager.DISALLOW_USB_FILE_TRANSFER);
901 writeBoolean(serializer, restrictions, UserManager.DISALLOW_CONFIG_CREDENTIALS);
902 writeBoolean(serializer, restrictions, UserManager.DISALLOW_REMOVE_USER);
903 writeBoolean(serializer, restrictions, UserManager.DISALLOW_DEBUGGING_FEATURES);
904 writeBoolean(serializer, restrictions, UserManager.DISALLOW_CONFIG_VPN);
905 writeBoolean(serializer, restrictions, UserManager.DISALLOW_CONFIG_TETHERING);
906 writeBoolean(serializer, restrictions, UserManager.DISALLOW_FACTORY_RESET);
907 writeBoolean(serializer, restrictions, UserManager.DISALLOW_ADD_USER);
908 writeBoolean(serializer, restrictions, UserManager.ENSURE_VERIFY_APPS);
909 writeBoolean(serializer, restrictions, UserManager.DISALLOW_CONFIG_CELL_BROADCASTS);
910 writeBoolean(serializer, restrictions, UserManager.DISALLOW_CONFIG_MOBILE_NETWORKS);
911 writeBoolean(serializer, restrictions, UserManager.DISALLOW_APPS_CONTROL);
912 writeBoolean(serializer, restrictions, UserManager.DISALLOW_MOUNT_PHYSICAL_MEDIA);
913 writeBoolean(serializer, restrictions, UserManager.DISALLOW_UNMUTE_MICROPHONE);
914 writeBoolean(serializer, restrictions, UserManager.DISALLOW_ADJUST_VOLUME);
Amith Yamasani390989d2014-07-17 10:52:03 -0700915 writeBoolean(serializer, restrictions, UserManager.DISALLOW_OUTGOING_CALLS);
916 writeBoolean(serializer, restrictions, UserManager.DISALLOW_SMS);
Jason Monk1c7c3192014-06-26 12:52:18 -0400917 writeBoolean(serializer, restrictions, UserManager.DISALLOW_CREATE_WINDOWS);
Amith Yamasani26af8292014-09-09 09:57:27 -0700918 writeBoolean(serializer, restrictions, UserManager.DISALLOW_CROSS_PROFILE_COPY_PASTE);
919 writeBoolean(serializer, restrictions, UserManager.DISALLOW_OUTGOING_BEAM);
Amith Yamasanie4afaa32014-06-30 14:55:07 +0530920 serializer.endTag(null, TAG_RESTRICTIONS);
921 }
922
Amith Yamasanie4cf7342012-12-17 11:12:09 -0800923 private UserInfo readUserLocked(int id) {
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700924 int flags = 0;
Amith Yamasani2a003292012-08-14 18:25:45 -0700925 int serialNumber = id;
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700926 String name = null;
Amith Yamasanib8151ec2012-04-18 18:02:48 -0700927 String iconPath = null;
Amith Yamasani920ace02012-09-20 22:15:37 -0700928 long creationTime = 0L;
929 long lastLoggedInTime = 0L;
Amith Yamasani655d0e22013-06-12 14:19:10 -0700930 long salt = 0L;
931 String pinHash = null;
932 int failedAttempts = 0;
Kenny Guy2a764942014-04-02 13:29:20 +0100933 int profileGroupId = UserInfo.NO_PROFILE_GROUP_ID;
Amith Yamasani655d0e22013-06-12 14:19:10 -0700934 long lastAttemptTime = 0L;
Dianne Hackbornd4ac8d72012-09-27 23:20:10 -0700935 boolean partial = false;
Adam Lesinskieddeb492014-09-08 17:50:03 -0700936 boolean guestToRemove = false;
Amith Yamasanie4cf7342012-12-17 11:12:09 -0800937 Bundle restrictions = new Bundle();
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700938
939 FileInputStream fis = null;
940 try {
Amith Yamasani2a003292012-08-14 18:25:45 -0700941 AtomicFile userFile =
Amith Yamasanifc95e702013-09-26 13:20:17 -0700942 new AtomicFile(new File(mUsersDir, Integer.toString(id) + XML_SUFFIX));
Amith Yamasani2a003292012-08-14 18:25:45 -0700943 fis = userFile.openRead();
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700944 XmlPullParser parser = Xml.newPullParser();
945 parser.setInput(fis, null);
946 int type;
947 while ((type = parser.next()) != XmlPullParser.START_TAG
948 && type != XmlPullParser.END_DOCUMENT) {
949 ;
950 }
951
952 if (type != XmlPullParser.START_TAG) {
Amith Yamasani0b285492011-04-14 17:35:23 -0700953 Slog.e(LOG_TAG, "Unable to read user " + id);
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700954 return null;
955 }
956
957 if (type == XmlPullParser.START_TAG && parser.getName().equals(TAG_USER)) {
Amith Yamasani920ace02012-09-20 22:15:37 -0700958 int storedId = readIntAttribute(parser, ATTR_ID, -1);
959 if (storedId != id) {
Amith Yamasani0b285492011-04-14 17:35:23 -0700960 Slog.e(LOG_TAG, "User id does not match the file name");
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700961 return null;
962 }
Amith Yamasani920ace02012-09-20 22:15:37 -0700963 serialNumber = readIntAttribute(parser, ATTR_SERIAL_NO, id);
964 flags = readIntAttribute(parser, ATTR_FLAGS, 0);
Amith Yamasanib8151ec2012-04-18 18:02:48 -0700965 iconPath = parser.getAttributeValue(null, ATTR_ICON_PATH);
Amith Yamasani920ace02012-09-20 22:15:37 -0700966 creationTime = readLongAttribute(parser, ATTR_CREATION_TIME, 0);
967 lastLoggedInTime = readLongAttribute(parser, ATTR_LAST_LOGGED_IN_TIME, 0);
Amith Yamasani655d0e22013-06-12 14:19:10 -0700968 salt = readLongAttribute(parser, ATTR_SALT, 0L);
969 pinHash = parser.getAttributeValue(null, ATTR_PIN_HASH);
970 failedAttempts = readIntAttribute(parser, ATTR_FAILED_ATTEMPTS, 0);
971 lastAttemptTime = readLongAttribute(parser, ATTR_LAST_RETRY_MS, 0L);
Kenny Guy2a764942014-04-02 13:29:20 +0100972 profileGroupId = readIntAttribute(parser, ATTR_PROFILE_GROUP_ID,
973 UserInfo.NO_PROFILE_GROUP_ID);
Dianne Hackbornd4ac8d72012-09-27 23:20:10 -0700974 String valueString = parser.getAttributeValue(null, ATTR_PARTIAL);
975 if ("true".equals(valueString)) {
976 partial = true;
977 }
Adam Lesinskieddeb492014-09-08 17:50:03 -0700978 valueString = parser.getAttributeValue(null, ATTR_GUEST_TO_REMOVE);
979 if ("true".equals(valueString)) {
980 guestToRemove = true;
981 }
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700982
Amith Yamasanie4cf7342012-12-17 11:12:09 -0800983 int outerDepth = parser.getDepth();
984 while ((type = parser.next()) != XmlPullParser.END_DOCUMENT
985 && (type != XmlPullParser.END_TAG || parser.getDepth() > outerDepth)) {
986 if (type == XmlPullParser.END_TAG || type == XmlPullParser.TEXT) {
987 continue;
988 }
989 String tag = parser.getName();
990 if (TAG_NAME.equals(tag)) {
991 type = parser.next();
992 if (type == XmlPullParser.TEXT) {
993 name = parser.getText();
994 }
995 } else if (TAG_RESTRICTIONS.equals(tag)) {
Amith Yamasanie4afaa32014-06-30 14:55:07 +0530996 readRestrictionsLocked(parser, restrictions);
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700997 }
998 }
999 }
Amith Yamasani4b2e9342011-03-31 12:38:53 -07001000
Amith Yamasanib8151ec2012-04-18 18:02:48 -07001001 UserInfo userInfo = new UserInfo(id, name, iconPath, flags);
Amith Yamasani2a003292012-08-14 18:25:45 -07001002 userInfo.serialNumber = serialNumber;
Amith Yamasani920ace02012-09-20 22:15:37 -07001003 userInfo.creationTime = creationTime;
1004 userInfo.lastLoggedInTime = lastLoggedInTime;
Dianne Hackbornd4ac8d72012-09-27 23:20:10 -07001005 userInfo.partial = partial;
Adam Lesinskieddeb492014-09-08 17:50:03 -07001006 userInfo.guestToRemove = guestToRemove;
Kenny Guy2a764942014-04-02 13:29:20 +01001007 userInfo.profileGroupId = profileGroupId;
Amith Yamasanie4cf7342012-12-17 11:12:09 -08001008 mUserRestrictions.append(id, restrictions);
Amith Yamasani655d0e22013-06-12 14:19:10 -07001009 if (salt != 0L) {
1010 RestrictionsPinState pinState = mRestrictionsPinStates.get(id);
1011 if (pinState == null) {
1012 pinState = new RestrictionsPinState();
1013 mRestrictionsPinStates.put(id, pinState);
1014 }
1015 pinState.salt = salt;
1016 pinState.pinHash = pinHash;
1017 pinState.failedAttempts = failedAttempts;
1018 pinState.lastAttemptTime = lastAttemptTime;
1019 }
Amith Yamasani4b2e9342011-03-31 12:38:53 -07001020 return userInfo;
1021
1022 } catch (IOException ioe) {
1023 } catch (XmlPullParserException pe) {
Dianne Hackbornbfd89b32011-12-15 18:22:54 -08001024 } finally {
1025 if (fis != null) {
1026 try {
1027 fis.close();
1028 } catch (IOException e) {
1029 }
1030 }
Amith Yamasani4b2e9342011-03-31 12:38:53 -07001031 }
1032 return null;
1033 }
1034
Amith Yamasanie4afaa32014-06-30 14:55:07 +05301035 private void readRestrictionsLocked(XmlPullParser parser, Bundle restrictions)
1036 throws IOException {
1037 readBoolean(parser, restrictions, UserManager.DISALLOW_CONFIG_WIFI);
1038 readBoolean(parser, restrictions, UserManager.DISALLOW_MODIFY_ACCOUNTS);
1039 readBoolean(parser, restrictions, UserManager.DISALLOW_INSTALL_APPS);
1040 readBoolean(parser, restrictions, UserManager.DISALLOW_UNINSTALL_APPS);
1041 readBoolean(parser, restrictions, UserManager.DISALLOW_SHARE_LOCATION);
1042 readBoolean(parser, restrictions,
1043 UserManager.DISALLOW_INSTALL_UNKNOWN_SOURCES);
1044 readBoolean(parser, restrictions, UserManager.DISALLOW_CONFIG_BLUETOOTH);
1045 readBoolean(parser, restrictions, UserManager.DISALLOW_USB_FILE_TRANSFER);
1046 readBoolean(parser, restrictions, UserManager.DISALLOW_CONFIG_CREDENTIALS);
1047 readBoolean(parser, restrictions, UserManager.DISALLOW_REMOVE_USER);
1048 readBoolean(parser, restrictions, UserManager.DISALLOW_DEBUGGING_FEATURES);
1049 readBoolean(parser, restrictions, UserManager.DISALLOW_CONFIG_VPN);
1050 readBoolean(parser, restrictions, UserManager.DISALLOW_CONFIG_TETHERING);
1051 readBoolean(parser, restrictions, UserManager.DISALLOW_FACTORY_RESET);
1052 readBoolean(parser, restrictions, UserManager.DISALLOW_ADD_USER);
1053 readBoolean(parser, restrictions, UserManager.ENSURE_VERIFY_APPS);
1054 readBoolean(parser, restrictions, UserManager.DISALLOW_CONFIG_CELL_BROADCASTS);
1055 readBoolean(parser, restrictions, UserManager.DISALLOW_CONFIG_MOBILE_NETWORKS);
1056 readBoolean(parser, restrictions, UserManager.DISALLOW_APPS_CONTROL);
1057 readBoolean(parser, restrictions,
1058 UserManager.DISALLOW_MOUNT_PHYSICAL_MEDIA);
1059 readBoolean(parser, restrictions, UserManager.DISALLOW_UNMUTE_MICROPHONE);
1060 readBoolean(parser, restrictions, UserManager.DISALLOW_ADJUST_VOLUME);
Amith Yamasani390989d2014-07-17 10:52:03 -07001061 readBoolean(parser, restrictions, UserManager.DISALLOW_OUTGOING_CALLS);
1062 readBoolean(parser, restrictions, UserManager.DISALLOW_SMS);
Jason Monk1c7c3192014-06-26 12:52:18 -04001063 readBoolean(parser, restrictions, UserManager.DISALLOW_CREATE_WINDOWS);
Amith Yamasani26af8292014-09-09 09:57:27 -07001064 readBoolean(parser, restrictions, UserManager.DISALLOW_CROSS_PROFILE_COPY_PASTE);
1065 readBoolean(parser, restrictions, UserManager.DISALLOW_OUTGOING_BEAM);
Amith Yamasanie4afaa32014-06-30 14:55:07 +05301066 }
1067
Amith Yamasanie4cf7342012-12-17 11:12:09 -08001068 private void readBoolean(XmlPullParser parser, Bundle restrictions,
1069 String restrictionKey) {
1070 String value = parser.getAttributeValue(null, restrictionKey);
Amith Yamasani71e6c692013-03-24 17:39:28 -07001071 if (value != null) {
1072 restrictions.putBoolean(restrictionKey, Boolean.parseBoolean(value));
1073 }
Amith Yamasanie4cf7342012-12-17 11:12:09 -08001074 }
1075
1076 private void writeBoolean(XmlSerializer xml, Bundle restrictions, String restrictionKey)
1077 throws IOException {
1078 if (restrictions.containsKey(restrictionKey)) {
1079 xml.attribute(null, restrictionKey,
1080 Boolean.toString(restrictions.getBoolean(restrictionKey)));
1081 }
1082 }
1083
Amith Yamasani920ace02012-09-20 22:15:37 -07001084 private int readIntAttribute(XmlPullParser parser, String attr, int defaultValue) {
1085 String valueString = parser.getAttributeValue(null, attr);
1086 if (valueString == null) return defaultValue;
1087 try {
1088 return Integer.parseInt(valueString);
1089 } catch (NumberFormatException nfe) {
1090 return defaultValue;
1091 }
1092 }
1093
1094 private long readLongAttribute(XmlPullParser parser, String attr, long defaultValue) {
1095 String valueString = parser.getAttributeValue(null, attr);
1096 if (valueString == null) return defaultValue;
1097 try {
1098 return Long.parseLong(valueString);
1099 } catch (NumberFormatException nfe) {
1100 return defaultValue;
1101 }
1102 }
1103
Amith Yamasani1a7472e2013-07-02 11:17:30 -07001104 private boolean isPackageInstalled(String pkg, int userId) {
1105 final ApplicationInfo info = mPm.getApplicationInfo(pkg,
1106 PackageManager.GET_UNINSTALLED_PACKAGES,
1107 userId);
1108 if (info == null || (info.flags&ApplicationInfo.FLAG_INSTALLED) == 0) {
1109 return false;
1110 }
1111 return true;
1112 }
1113
Amith Yamasanib82add22013-07-09 11:24:44 -07001114 /**
Kenny Guyd21b2182014-07-17 16:38:55 +01001115 * Removes all the restrictions files (res_<packagename>) for a given user.
Amith Yamasanib82add22013-07-09 11:24:44 -07001116 * Does not do any permissions checking.
1117 */
Kenny Guyd21b2182014-07-17 16:38:55 +01001118 private void cleanAppRestrictions(int userId) {
Amith Yamasanib82add22013-07-09 11:24:44 -07001119 synchronized (mPackagesLock) {
1120 File dir = Environment.getUserSystemDirectory(userId);
1121 String[] files = dir.list();
1122 if (files == null) return;
1123 for (String fileName : files) {
1124 if (fileName.startsWith(RESTRICTIONS_FILE_PREFIX)) {
1125 File resFile = new File(dir, fileName);
1126 if (resFile.exists()) {
Kenny Guyd21b2182014-07-17 16:38:55 +01001127 resFile.delete();
Amith Yamasanib82add22013-07-09 11:24:44 -07001128 }
1129 }
1130 }
1131 }
1132 }
1133
Amith Yamasani1a7472e2013-07-02 11:17:30 -07001134 /**
1135 * Removes the app restrictions file for a specific package and user id, if it exists.
1136 */
1137 private void cleanAppRestrictionsForPackage(String pkg, int userId) {
1138 synchronized (mPackagesLock) {
1139 File dir = Environment.getUserSystemDirectory(userId);
Amith Yamasanifc95e702013-09-26 13:20:17 -07001140 File resFile = new File(dir, packageToRestrictionsFileName(pkg));
Amith Yamasani1a7472e2013-07-02 11:17:30 -07001141 if (resFile.exists()) {
1142 resFile.delete();
1143 }
1144 }
1145 }
1146
Kenny Guya52dc3e2014-02-11 15:33:14 +00001147 @Override
Kenny Guy2a764942014-04-02 13:29:20 +01001148 public UserInfo createProfileForUser(String name, int flags, int userId) {
Kenny Guya52dc3e2014-02-11 15:33:14 +00001149 checkManageUsersPermission("Only the system can create users");
Kenny Guy2a764942014-04-02 13:29:20 +01001150 if (userId != UserHandle.USER_OWNER) {
1151 Slog.w(LOG_TAG, "Only user owner can have profiles");
Kenny Guya52dc3e2014-02-11 15:33:14 +00001152 return null;
1153 }
Kenny Guy2a764942014-04-02 13:29:20 +01001154 return createUserInternal(name, flags, userId);
Kenny Guya52dc3e2014-02-11 15:33:14 +00001155 }
1156
Amith Yamasani258848d2012-08-10 17:06:33 -07001157 @Override
Amith Yamasani13593602012-03-22 16:16:17 -07001158 public UserInfo createUser(String name, int flags) {
Amith Yamasani2a003292012-08-14 18:25:45 -07001159 checkManageUsersPermission("Only the system can create users");
Nicolas Prevotc6d033e2014-02-27 13:11:09 +00001160 return createUserInternal(name, flags, UserHandle.USER_NULL);
Kenny Guya52dc3e2014-02-11 15:33:14 +00001161 }
Amith Yamasanifaea76f2012-09-11 10:59:48 -07001162
Jessica Hummelbe81c802014-04-22 15:49:22 +01001163 private UserInfo createUserInternal(String name, int flags, int parentId) {
Julia Reynolds75175022014-06-26 16:35:00 -04001164 if (getUserRestrictions(UserHandle.getCallingUserId()).getBoolean(
1165 UserManager.DISALLOW_ADD_USER, false)) {
1166 Log.w(LOG_TAG, "Cannot add user. DISALLOW_ADD_USER is enabled.");
1167 return null;
1168 }
Amith Yamasani95ab7842014-08-11 17:09:26 -07001169 final boolean isGuest = (flags & UserInfo.FLAG_GUEST) != 0;
Dianne Hackborn5dc5a002012-09-15 19:33:48 -07001170 final long ident = Binder.clearCallingIdentity();
Nicolas Prevotc6d033e2014-02-27 13:11:09 +00001171 UserInfo userInfo = null;
Dianne Hackborn5dc5a002012-09-15 19:33:48 -07001172 try {
1173 synchronized (mInstallLock) {
1174 synchronized (mPackagesLock) {
Jessica Hummelbe81c802014-04-22 15:49:22 +01001175 UserInfo parent = null;
1176 if (parentId != UserHandle.USER_NULL) {
1177 parent = getUserInfoLocked(parentId);
1178 if (parent == null) return null;
Nicolas Prevotc6d033e2014-02-27 13:11:09 +00001179 }
Amith Yamasani95ab7842014-08-11 17:09:26 -07001180 // If we're not adding a guest user and the limit has been reached,
1181 // cannot add a user.
1182 if (!isGuest && isUserLimitReachedLocked()) {
1183 return null;
1184 }
1185 // If we're adding a guest and there already exists one, bail.
Adam Lesinskieddeb492014-09-08 17:50:03 -07001186 if (isGuest && findCurrentGuestUserLocked() != null) {
Amith Yamasani95ab7842014-08-11 17:09:26 -07001187 return null;
1188 }
1189 // Limit number of managed profiles that can be created
1190 if ((flags & UserInfo.FLAG_MANAGED_PROFILE) != 0
1191 && numberOfUsersOfTypeLocked(UserInfo.FLAG_MANAGED_PROFILE, true)
1192 >= MAX_MANAGED_PROFILES) {
1193 return null;
1194 }
Dianne Hackborn5dc5a002012-09-15 19:33:48 -07001195 int userId = getNextAvailableIdLocked();
1196 userInfo = new UserInfo(userId, name, null, flags);
1197 File userPath = new File(mBaseUserPath, Integer.toString(userId));
1198 userInfo.serialNumber = mNextSerialNumber++;
Amith Yamasani920ace02012-09-20 22:15:37 -07001199 long now = System.currentTimeMillis();
1200 userInfo.creationTime = (now > EPOCH_PLUS_30_YEARS) ? now : 0;
Dianne Hackbornd4ac8d72012-09-27 23:20:10 -07001201 userInfo.partial = true;
Amith Yamasani16389312012-10-17 21:20:14 -07001202 Environment.getUserSystemDirectory(userInfo.id).mkdirs();
Dianne Hackborn5dc5a002012-09-15 19:33:48 -07001203 mUsers.put(userId, userInfo);
1204 writeUserListLocked();
Jessica Hummelbe81c802014-04-22 15:49:22 +01001205 if (parent != null) {
1206 if (parent.profileGroupId == UserInfo.NO_PROFILE_GROUP_ID) {
1207 parent.profileGroupId = parent.id;
Fyodor Kupolov75d0ea82014-12-15 16:21:07 -08001208 scheduleWriteUserLocked(parent);
Kenny Guya52dc3e2014-02-11 15:33:14 +00001209 }
Jessica Hummelbe81c802014-04-22 15:49:22 +01001210 userInfo.profileGroupId = parent.profileGroupId;
Kenny Guya52dc3e2014-02-11 15:33:14 +00001211 }
Dianne Hackborn5dc5a002012-09-15 19:33:48 -07001212 mPm.createNewUserLILPw(userId, userPath);
Dianne Hackbornd4ac8d72012-09-27 23:20:10 -07001213 userInfo.partial = false;
Fyodor Kupolov75d0ea82014-12-15 16:21:07 -08001214 scheduleWriteUserLocked(userInfo);
Dianne Hackbornd4ac8d72012-09-27 23:20:10 -07001215 updateUserIdsLocked();
Geoffrey Borggaarde45e45e32013-01-24 10:03:20 -05001216 Bundle restrictions = new Bundle();
Geoffrey Borggaarde45e45e32013-01-24 10:03:20 -05001217 mUserRestrictions.append(userId, restrictions);
Dianne Hackborn5dc5a002012-09-15 19:33:48 -07001218 }
Dianne Hackborn4428e172012-08-24 17:43:05 -07001219 }
Dianne Hackborn5dc5a002012-09-15 19:33:48 -07001220 if (userInfo != null) {
1221 Intent addedIntent = new Intent(Intent.ACTION_USER_ADDED);
1222 addedIntent.putExtra(Intent.EXTRA_USER_HANDLE, userInfo.id);
1223 mContext.sendBroadcastAsUser(addedIntent, UserHandle.ALL,
1224 android.Manifest.permission.MANAGE_USERS);
1225 }
1226 } finally {
1227 Binder.restoreCallingIdentity(ident);
Amith Yamasani258848d2012-08-10 17:06:33 -07001228 }
Amith Yamasani4b2e9342011-03-31 12:38:53 -07001229 return userInfo;
1230 }
1231
Amith Yamasani95ab7842014-08-11 17:09:26 -07001232 private int numberOfUsersOfTypeLocked(int flags, boolean excludeDying) {
1233 int count = 0;
1234 for (int i = mUsers.size() - 1; i >= 0; i--) {
1235 UserInfo user = mUsers.valueAt(i);
1236 if (!excludeDying || !mRemovingUserIds.get(user.id)) {
1237 if ((user.flags & flags) != 0) {
1238 count++;
1239 }
1240 }
1241 }
1242 return count;
1243 }
1244
Amith Yamasani0b285492011-04-14 17:35:23 -07001245 /**
Adam Lesinskieddeb492014-09-08 17:50:03 -07001246 * Find the current guest user. If the Guest user is partial,
1247 * then do not include it in the results as it is about to die.
1248 * This is different than {@link #numberOfUsersOfTypeLocked(int, boolean)} due to
1249 * the special handling of Guests being removed.
1250 */
1251 private UserInfo findCurrentGuestUserLocked() {
1252 final int size = mUsers.size();
1253 for (int i = 0; i < size; i++) {
1254 final UserInfo user = mUsers.valueAt(i);
1255 if (user.isGuest() && !user.guestToRemove && !mRemovingUserIds.get(user.id)) {
1256 return user;
1257 }
1258 }
1259 return null;
1260 }
1261
1262 /**
Amith Yamasani1df14732014-08-29 21:37:27 -07001263 * Mark this guest user for deletion to allow us to create another guest
1264 * and switch to that user before actually removing this guest.
1265 * @param userHandle the userid of the current guest
1266 * @return whether the user could be marked for deletion
1267 */
1268 public boolean markGuestForDeletion(int userHandle) {
1269 checkManageUsersPermission("Only the system can remove users");
1270 if (getUserRestrictions(UserHandle.getCallingUserId()).getBoolean(
1271 UserManager.DISALLOW_REMOVE_USER, false)) {
1272 Log.w(LOG_TAG, "Cannot remove user. DISALLOW_REMOVE_USER is enabled.");
1273 return false;
1274 }
1275
1276 long ident = Binder.clearCallingIdentity();
1277 try {
1278 final UserInfo user;
1279 synchronized (mPackagesLock) {
1280 user = mUsers.get(userHandle);
1281 if (userHandle == 0 || user == null || mRemovingUserIds.get(userHandle)) {
1282 return false;
1283 }
1284 if (!user.isGuest()) {
1285 return false;
1286 }
Adam Lesinskieddeb492014-09-08 17:50:03 -07001287 // We set this to a guest user that is to be removed. This is a temporary state
1288 // where we are allowed to add new Guest users, even if this one is still not
1289 // removed. This user will still show up in getUserInfo() calls.
1290 // If we don't get around to removing this Guest user, it will be purged on next
1291 // startup.
1292 user.guestToRemove = true;
Amith Yamasani1df14732014-08-29 21:37:27 -07001293 // Mark it as disabled, so that it isn't returned any more when
1294 // profiles are queried.
1295 user.flags |= UserInfo.FLAG_DISABLED;
Amith Yamasani1df14732014-08-29 21:37:27 -07001296 writeUserLocked(user);
1297 }
1298 } finally {
1299 Binder.restoreCallingIdentity(ident);
1300 }
1301 return true;
1302 }
1303
1304 /**
Amith Yamasani0b285492011-04-14 17:35:23 -07001305 * Removes a user and all data directories created for that user. This method should be called
1306 * after the user's processes have been terminated.
Dianne Hackborn10ad9822014-03-17 11:28:36 -07001307 * @param userHandle the user's id
Amith Yamasani0b285492011-04-14 17:35:23 -07001308 */
Amith Yamasani258848d2012-08-10 17:06:33 -07001309 public boolean removeUser(int userHandle) {
Amith Yamasani2a003292012-08-14 18:25:45 -07001310 checkManageUsersPermission("Only the system can remove users");
Julia Reynolds4ac5f852014-06-23 17:38:51 -04001311 if (getUserRestrictions(UserHandle.getCallingUserId()).getBoolean(
1312 UserManager.DISALLOW_REMOVE_USER, false)) {
1313 Log.w(LOG_TAG, "Cannot remove user. DISALLOW_REMOVE_USER is enabled.");
1314 return false;
1315 }
1316
Kenny Guyee58b4f2014-05-23 15:19:53 +01001317 long ident = Binder.clearCallingIdentity();
1318 try {
1319 final UserInfo user;
1320 synchronized (mPackagesLock) {
1321 user = mUsers.get(userHandle);
Kenny Guy17c9d692014-06-13 13:51:42 +01001322 if (userHandle == 0 || user == null || mRemovingUserIds.get(userHandle)) {
Kenny Guyee58b4f2014-05-23 15:19:53 +01001323 return false;
1324 }
Jeff Sharkey6eb09392014-11-14 15:57:59 -08001325
1326 // We remember deleted user IDs to prevent them from being
1327 // reused during the current boot; they can still be reused
1328 // after a reboot.
Kenny Guyee58b4f2014-05-23 15:19:53 +01001329 mRemovingUserIds.put(userHandle, true);
Jeff Sharkey6eb09392014-11-14 15:57:59 -08001330
Kenny Guyee58b4f2014-05-23 15:19:53 +01001331 try {
1332 mAppOpsService.removeUser(userHandle);
1333 } catch (RemoteException e) {
1334 Log.w(LOG_TAG, "Unable to notify AppOpsService of removing user", e);
1335 }
1336 // Set this to a partially created user, so that the user will be purged
1337 // on next startup, in case the runtime stops now before stopping and
1338 // removing the user completely.
1339 user.partial = true;
1340 // Mark it as disabled, so that it isn't returned any more when
1341 // profiles are queried.
1342 user.flags |= UserInfo.FLAG_DISABLED;
1343 writeUserLocked(user);
1344 }
1345
1346 if (user.profileGroupId != UserInfo.NO_PROFILE_GROUP_ID
1347 && user.isManagedProfile()) {
1348 // Send broadcast to notify system that the user removed was a
1349 // managed user.
1350 sendProfileRemovedBroadcast(user.profileGroupId, user.id);
1351 }
1352
1353 if (DBG) Slog.i(LOG_TAG, "Stopping user " + userHandle);
1354 int res;
1355 try {
1356 res = ActivityManagerNative.getDefault().stopUser(userHandle,
1357 new IStopUserCallback.Stub() {
1358 @Override
1359 public void userStopped(int userId) {
1360 finishRemoveUser(userId);
1361 }
1362 @Override
1363 public void userStopAborted(int userId) {
1364 }
1365 });
1366 } catch (RemoteException e) {
Dianne Hackborn80a4af22012-08-27 19:18:31 -07001367 return false;
1368 }
Kenny Guyee58b4f2014-05-23 15:19:53 +01001369 return res == ActivityManager.USER_OP_SUCCESS;
1370 } finally {
1371 Binder.restoreCallingIdentity(ident);
Dianne Hackborn80a4af22012-08-27 19:18:31 -07001372 }
Dianne Hackborn80a4af22012-08-27 19:18:31 -07001373 }
1374
Amith Yamasanidb6a14c2012-10-17 21:16:52 -07001375 void finishRemoveUser(final int userHandle) {
Amith Yamasani16389312012-10-17 21:20:14 -07001376 if (DBG) Slog.i(LOG_TAG, "finishRemoveUser " + userHandle);
Amith Yamasanidb6a14c2012-10-17 21:16:52 -07001377 // Let other services shutdown any activity and clean up their state before completely
1378 // wiping the user's system directory and removing from the user list
Dianne Hackborn5dc5a002012-09-15 19:33:48 -07001379 long ident = Binder.clearCallingIdentity();
1380 try {
1381 Intent addedIntent = new Intent(Intent.ACTION_USER_REMOVED);
1382 addedIntent.putExtra(Intent.EXTRA_USER_HANDLE, userHandle);
Amith Yamasanidb6a14c2012-10-17 21:16:52 -07001383 mContext.sendOrderedBroadcastAsUser(addedIntent, UserHandle.ALL,
1384 android.Manifest.permission.MANAGE_USERS,
1385
1386 new BroadcastReceiver() {
1387 @Override
1388 public void onReceive(Context context, Intent intent) {
1389 if (DBG) {
1390 Slog.i(LOG_TAG,
1391 "USER_REMOVED broadcast sent, cleaning up user data "
1392 + userHandle);
1393 }
1394 new Thread() {
1395 public void run() {
1396 synchronized (mInstallLock) {
1397 synchronized (mPackagesLock) {
1398 removeUserStateLocked(userHandle);
1399 }
1400 }
1401 }
1402 }.start();
1403 }
1404 },
1405
1406 null, Activity.RESULT_OK, null, null);
Dianne Hackborn5dc5a002012-09-15 19:33:48 -07001407 } finally {
1408 Binder.restoreCallingIdentity(ident);
1409 }
Amith Yamasani2a003292012-08-14 18:25:45 -07001410 }
1411
Jeff Sharkeyffe0cb42012-11-05 17:24:43 -08001412 private void removeUserStateLocked(final int userHandle) {
Dianne Hackbornd4ac8d72012-09-27 23:20:10 -07001413 // Cleanup package manager settings
Amith Yamasanidda003f2014-08-28 18:06:51 -07001414 mPm.cleanUpUserLILPw(this, userHandle);
Dianne Hackbornd4ac8d72012-09-27 23:20:10 -07001415
1416 // Remove this user from the list
1417 mUsers.remove(userHandle);
Jeff Sharkeyffe0cb42012-11-05 17:24:43 -08001418
Amith Yamasani655d0e22013-06-12 14:19:10 -07001419 mRestrictionsPinStates.remove(userHandle);
Dianne Hackbornd4ac8d72012-09-27 23:20:10 -07001420 // Remove user file
Amith Yamasanifc95e702013-09-26 13:20:17 -07001421 AtomicFile userFile = new AtomicFile(new File(mUsersDir, userHandle + XML_SUFFIX));
Dianne Hackbornd4ac8d72012-09-27 23:20:10 -07001422 userFile.delete();
1423 // Update the user list
1424 writeUserListLocked();
1425 updateUserIdsLocked();
1426 removeDirectoryRecursive(Environment.getUserSystemDirectory(userHandle));
1427 }
1428
Amith Yamasani61f57372012-08-31 12:12:28 -07001429 private void removeDirectoryRecursive(File parent) {
1430 if (parent.isDirectory()) {
1431 String[] files = parent.list();
1432 for (String filename : files) {
1433 File child = new File(parent, filename);
1434 removeDirectoryRecursive(child);
1435 }
1436 }
1437 parent.delete();
1438 }
1439
Kenny Guyf8d3a232014-05-15 16:09:52 +01001440 private void sendProfileRemovedBroadcast(int parentUserId, int removedUserId) {
Adam Connors7b66ed52014-04-14 11:58:10 +01001441 Intent managedProfileIntent = new Intent(Intent.ACTION_MANAGED_PROFILE_REMOVED);
Adam Connorsd4b584e2014-06-09 13:55:47 +01001442 managedProfileIntent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY |
1443 Intent.FLAG_RECEIVER_FOREGROUND);
Kenny Guyf8d3a232014-05-15 16:09:52 +01001444 managedProfileIntent.putExtra(Intent.EXTRA_USER, new UserHandle(removedUserId));
1445 mContext.sendBroadcastAsUser(managedProfileIntent, new UserHandle(parentUserId), null);
Adam Connors7b66ed52014-04-14 11:58:10 +01001446 }
1447
Amith Yamasani2a003292012-08-14 18:25:45 -07001448 @Override
Amith Yamasani7e99bc02013-04-16 18:24:51 -07001449 public Bundle getApplicationRestrictions(String packageName) {
1450 return getApplicationRestrictionsForUser(packageName, UserHandle.getCallingUserId());
1451 }
1452
1453 @Override
1454 public Bundle getApplicationRestrictionsForUser(String packageName, int userId) {
Amith Yamasanidf2e92a2013-03-01 17:04:38 -08001455 if (UserHandle.getCallingUserId() != userId
Amith Yamasani9429afb2013-04-10 18:40:51 -07001456 || !UserHandle.isSameApp(Binder.getCallingUid(), getUidForPackage(packageName))) {
Amith Yamasanibe465322014-04-24 13:45:17 -07001457 checkManageUsersPermission("Only system can get restrictions for other users/apps");
Amith Yamasanidf2e92a2013-03-01 17:04:38 -08001458 }
1459 synchronized (mPackagesLock) {
1460 // Read the restrictions from XML
1461 return readApplicationRestrictionsLocked(packageName, userId);
1462 }
1463 }
1464
1465 @Override
Amith Yamasani7e99bc02013-04-16 18:24:51 -07001466 public void setApplicationRestrictions(String packageName, Bundle restrictions,
Amith Yamasanidf2e92a2013-03-01 17:04:38 -08001467 int userId) {
1468 if (UserHandle.getCallingUserId() != userId
Amith Yamasani9429afb2013-04-10 18:40:51 -07001469 || !UserHandle.isSameApp(Binder.getCallingUid(), getUidForPackage(packageName))) {
Amith Yamasanibe465322014-04-24 13:45:17 -07001470 checkManageUsersPermission("Only system can set restrictions for other users/apps");
Amith Yamasanidf2e92a2013-03-01 17:04:38 -08001471 }
1472 synchronized (mPackagesLock) {
Kenny Guyd21b2182014-07-17 16:38:55 +01001473 if (restrictions == null || restrictions.isEmpty()) {
1474 cleanAppRestrictionsForPackage(packageName, userId);
1475 } else {
1476 // Write the restrictions to XML
1477 writeApplicationRestrictionsLocked(packageName, restrictions, userId);
1478 }
Amith Yamasanidf2e92a2013-03-01 17:04:38 -08001479 }
Robin Lee66e5d962014-04-09 16:44:21 +01001480
Kenny Guyd21b2182014-07-17 16:38:55 +01001481 if (isPackageInstalled(packageName, userId)) {
1482 // Notify package of changes via an intent - only sent to explicitly registered receivers.
1483 Intent changeIntent = new Intent(Intent.ACTION_APPLICATION_RESTRICTIONS_CHANGED);
1484 changeIntent.setPackage(packageName);
1485 changeIntent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY);
1486 mContext.sendBroadcastAsUser(changeIntent, new UserHandle(userId));
1487 }
Amith Yamasanidf2e92a2013-03-01 17:04:38 -08001488 }
1489
Amith Yamasani655d0e22013-06-12 14:19:10 -07001490 @Override
Amith Yamasanid304af62013-09-05 09:30:23 -07001491 public boolean setRestrictionsChallenge(String newPin) {
Amith Yamasani655d0e22013-06-12 14:19:10 -07001492 checkManageUsersPermission("Only system can modify the restrictions pin");
1493 int userId = UserHandle.getCallingUserId();
1494 synchronized (mPackagesLock) {
1495 RestrictionsPinState pinState = mRestrictionsPinStates.get(userId);
1496 if (pinState == null) {
1497 pinState = new RestrictionsPinState();
1498 }
1499 if (newPin == null) {
1500 pinState.salt = 0;
1501 pinState.pinHash = null;
1502 } else {
1503 try {
1504 pinState.salt = SecureRandom.getInstance("SHA1PRNG").nextLong();
1505 } catch (NoSuchAlgorithmException e) {
1506 pinState.salt = (long) (Math.random() * Long.MAX_VALUE);
1507 }
1508 pinState.pinHash = passwordToHash(newPin, pinState.salt);
1509 pinState.failedAttempts = 0;
1510 }
1511 mRestrictionsPinStates.put(userId, pinState);
1512 writeUserLocked(mUsers.get(userId));
1513 }
1514 return true;
1515 }
1516
1517 @Override
Amith Yamasanid304af62013-09-05 09:30:23 -07001518 public int checkRestrictionsChallenge(String pin) {
Amith Yamasani655d0e22013-06-12 14:19:10 -07001519 checkManageUsersPermission("Only system can verify the restrictions pin");
1520 int userId = UserHandle.getCallingUserId();
1521 synchronized (mPackagesLock) {
1522 RestrictionsPinState pinState = mRestrictionsPinStates.get(userId);
1523 // If there's no pin set, return error code
1524 if (pinState == null || pinState.salt == 0 || pinState.pinHash == null) {
1525 return UserManager.PIN_VERIFICATION_FAILED_NOT_SET;
1526 } else if (pin == null) {
1527 // If just checking if user can be prompted, return remaining time
1528 int waitTime = getRemainingTimeForPinAttempt(pinState);
1529 Slog.d(LOG_TAG, "Remaining waittime peek=" + waitTime);
1530 return waitTime;
1531 } else {
1532 int waitTime = getRemainingTimeForPinAttempt(pinState);
1533 Slog.d(LOG_TAG, "Remaining waittime=" + waitTime);
1534 if (waitTime > 0) {
1535 return waitTime;
1536 }
1537 if (passwordToHash(pin, pinState.salt).equals(pinState.pinHash)) {
1538 pinState.failedAttempts = 0;
Fyodor Kupolov75d0ea82014-12-15 16:21:07 -08001539 scheduleWriteUserLocked(mUsers.get(userId));
Amith Yamasani655d0e22013-06-12 14:19:10 -07001540 return UserManager.PIN_VERIFICATION_SUCCESS;
1541 } else {
1542 pinState.failedAttempts++;
1543 pinState.lastAttemptTime = System.currentTimeMillis();
Fyodor Kupolov75d0ea82014-12-15 16:21:07 -08001544 scheduleWriteUserLocked(mUsers.get(userId));
Amith Yamasani655d0e22013-06-12 14:19:10 -07001545 return waitTime;
1546 }
1547 }
1548 }
1549 }
1550
1551 private int getRemainingTimeForPinAttempt(RestrictionsPinState pinState) {
1552 int backoffIndex = Math.min(pinState.failedAttempts / BACKOFF_INC_INTERVAL,
1553 BACKOFF_TIMES.length - 1);
1554 int backoffTime = (pinState.failedAttempts % BACKOFF_INC_INTERVAL) == 0 ?
1555 BACKOFF_TIMES[backoffIndex] : 0;
1556 return (int) Math.max(backoffTime + pinState.lastAttemptTime - System.currentTimeMillis(),
1557 0);
1558 }
1559
1560 @Override
Amith Yamasanid304af62013-09-05 09:30:23 -07001561 public boolean hasRestrictionsChallenge() {
Amith Yamasani655d0e22013-06-12 14:19:10 -07001562 int userId = UserHandle.getCallingUserId();
1563 synchronized (mPackagesLock) {
Amith Yamasani0343ec32013-07-22 14:52:06 -07001564 return hasRestrictionsPinLocked(userId);
1565 }
1566 }
1567
1568 private boolean hasRestrictionsPinLocked(int userId) {
1569 RestrictionsPinState pinState = mRestrictionsPinStates.get(userId);
1570 if (pinState == null || pinState.salt == 0 || pinState.pinHash == null) {
1571 return false;
Amith Yamasani655d0e22013-06-12 14:19:10 -07001572 }
1573 return true;
1574 }
1575
Amith Yamasani1a7472e2013-07-02 11:17:30 -07001576 @Override
1577 public void removeRestrictions() {
Amith Yamasanibe465322014-04-24 13:45:17 -07001578 checkManageUsersPermission("Only system can remove restrictions");
Amith Yamasani1a7472e2013-07-02 11:17:30 -07001579 final int userHandle = UserHandle.getCallingUserId();
Amith Yamasani5e486f52013-08-07 11:06:44 -07001580 removeRestrictionsForUser(userHandle, true);
Amith Yamasani350962c2013-08-06 11:18:53 -07001581 }
1582
Amith Yamasanie5bcff62014-07-19 15:44:09 -07001583 private void removeRestrictionsForUser(final int userHandle, boolean unhideApps) {
Amith Yamasani1a7472e2013-07-02 11:17:30 -07001584 synchronized (mPackagesLock) {
1585 // Remove all user restrictions
1586 setUserRestrictions(new Bundle(), userHandle);
1587 // Remove restrictions pin
Amith Yamasanid304af62013-09-05 09:30:23 -07001588 setRestrictionsChallenge(null);
Amith Yamasani1a7472e2013-07-02 11:17:30 -07001589 // Remove any app restrictions
Kenny Guyd21b2182014-07-17 16:38:55 +01001590 cleanAppRestrictions(userHandle);
Amith Yamasani1a7472e2013-07-02 11:17:30 -07001591 }
Amith Yamasanie5bcff62014-07-19 15:44:09 -07001592 if (unhideApps) {
1593 unhideAllInstalledAppsForUser(userHandle);
Amith Yamasani5e486f52013-08-07 11:06:44 -07001594 }
1595 }
1596
Amith Yamasanie5bcff62014-07-19 15:44:09 -07001597 private void unhideAllInstalledAppsForUser(final int userHandle) {
Amith Yamasani1a7472e2013-07-02 11:17:30 -07001598 mHandler.post(new Runnable() {
1599 @Override
1600 public void run() {
1601 List<ApplicationInfo> apps =
1602 mPm.getInstalledApplications(PackageManager.GET_UNINSTALLED_PACKAGES,
1603 userHandle).getList();
1604 final long ident = Binder.clearCallingIdentity();
1605 try {
1606 for (ApplicationInfo appInfo : apps) {
1607 if ((appInfo.flags & ApplicationInfo.FLAG_INSTALLED) != 0
Alex Klyubinb9f8a522015-02-03 11:12:59 -08001608 && (appInfo.privateFlags & ApplicationInfo.PRIVATE_FLAG_HIDDEN)
1609 != 0) {
Amith Yamasanie5bcff62014-07-19 15:44:09 -07001610 mPm.setApplicationHiddenSettingAsUser(appInfo.packageName, false,
Amith Yamasani1a7472e2013-07-02 11:17:30 -07001611 userHandle);
1612 }
1613 }
1614 } finally {
1615 Binder.restoreCallingIdentity(ident);
1616 }
1617 }
1618 });
1619 }
1620
Amith Yamasani655d0e22013-06-12 14:19:10 -07001621 /*
1622 * Generate a hash for the given password. To avoid brute force attacks, we use a salted hash.
1623 * Not the most secure, but it is at least a second level of protection. First level is that
1624 * the file is in a location only readable by the system process.
1625 * @param password the password.
1626 * @param salt the randomly generated salt
1627 * @return the hash of the pattern in a String.
1628 */
1629 private String passwordToHash(String password, long salt) {
1630 if (password == null) {
1631 return null;
1632 }
1633 String algo = null;
1634 String hashed = salt + password;
1635 try {
1636 byte[] saltedPassword = (password + salt).getBytes();
1637 byte[] sha1 = MessageDigest.getInstance(algo = "SHA-1").digest(saltedPassword);
1638 byte[] md5 = MessageDigest.getInstance(algo = "MD5").digest(saltedPassword);
1639 hashed = toHex(sha1) + toHex(md5);
1640 } catch (NoSuchAlgorithmException e) {
1641 Log.w(LOG_TAG, "Failed to encode string because of missing algorithm: " + algo);
1642 }
1643 return hashed;
1644 }
1645
1646 private static String toHex(byte[] ary) {
1647 final String hex = "0123456789ABCDEF";
1648 String ret = "";
1649 for (int i = 0; i < ary.length; i++) {
1650 ret += hex.charAt((ary[i] >> 4) & 0xf);
1651 ret += hex.charAt(ary[i] & 0xf);
1652 }
1653 return ret;
1654 }
1655
Amith Yamasanidf2e92a2013-03-01 17:04:38 -08001656 private int getUidForPackage(String packageName) {
Amith Yamasani9429afb2013-04-10 18:40:51 -07001657 long ident = Binder.clearCallingIdentity();
Amith Yamasanidf2e92a2013-03-01 17:04:38 -08001658 try {
1659 return mContext.getPackageManager().getApplicationInfo(packageName,
1660 PackageManager.GET_UNINSTALLED_PACKAGES).uid;
1661 } catch (NameNotFoundException nnfe) {
1662 return -1;
Amith Yamasani9429afb2013-04-10 18:40:51 -07001663 } finally {
1664 Binder.restoreCallingIdentity(ident);
Amith Yamasanidf2e92a2013-03-01 17:04:38 -08001665 }
1666 }
1667
Amith Yamasani7e99bc02013-04-16 18:24:51 -07001668 private Bundle readApplicationRestrictionsLocked(String packageName,
Amith Yamasanidf2e92a2013-03-01 17:04:38 -08001669 int userId) {
Amith Yamasani7e99bc02013-04-16 18:24:51 -07001670 final Bundle restrictions = new Bundle();
Amith Yamasanidf2e92a2013-03-01 17:04:38 -08001671 final ArrayList<String> values = new ArrayList<String>();
1672
1673 FileInputStream fis = null;
1674 try {
1675 AtomicFile restrictionsFile =
1676 new AtomicFile(new File(Environment.getUserSystemDirectory(userId),
Amith Yamasanifc95e702013-09-26 13:20:17 -07001677 packageToRestrictionsFileName(packageName)));
Amith Yamasanidf2e92a2013-03-01 17:04:38 -08001678 fis = restrictionsFile.openRead();
1679 XmlPullParser parser = Xml.newPullParser();
1680 parser.setInput(fis, null);
1681 int type;
1682 while ((type = parser.next()) != XmlPullParser.START_TAG
1683 && type != XmlPullParser.END_DOCUMENT) {
1684 ;
1685 }
1686
1687 if (type != XmlPullParser.START_TAG) {
1688 Slog.e(LOG_TAG, "Unable to read restrictions file "
1689 + restrictionsFile.getBaseFile());
Amith Yamasani7e99bc02013-04-16 18:24:51 -07001690 return restrictions;
Amith Yamasanidf2e92a2013-03-01 17:04:38 -08001691 }
1692
1693 while ((type = parser.next()) != XmlPullParser.END_DOCUMENT) {
1694 if (type == XmlPullParser.START_TAG && parser.getName().equals(TAG_ENTRY)) {
1695 String key = parser.getAttributeValue(null, ATTR_KEY);
Amith Yamasani7e99bc02013-04-16 18:24:51 -07001696 String valType = parser.getAttributeValue(null, ATTR_VALUE_TYPE);
Amith Yamasanidf2e92a2013-03-01 17:04:38 -08001697 String multiple = parser.getAttributeValue(null, ATTR_MULTIPLE);
1698 if (multiple != null) {
Amith Yamasanie0840392014-08-27 10:33:17 -07001699 values.clear();
Amith Yamasanidf2e92a2013-03-01 17:04:38 -08001700 int count = Integer.parseInt(multiple);
1701 while (count > 0 && (type = parser.next()) != XmlPullParser.END_DOCUMENT) {
1702 if (type == XmlPullParser.START_TAG
1703 && parser.getName().equals(TAG_VALUE)) {
1704 values.add(parser.nextText().trim());
1705 count--;
1706 }
1707 }
1708 String [] valueStrings = new String[values.size()];
1709 values.toArray(valueStrings);
Amith Yamasani7e99bc02013-04-16 18:24:51 -07001710 restrictions.putStringArray(key, valueStrings);
Amith Yamasanidf2e92a2013-03-01 17:04:38 -08001711 } else {
1712 String value = parser.nextText().trim();
Amith Yamasani5b5aa402014-06-01 20:10:14 -07001713 if (ATTR_TYPE_BOOLEAN.equals(valType)) {
1714 restrictions.putBoolean(key, Boolean.parseBoolean(value));
1715 } else if (ATTR_TYPE_INTEGER.equals(valType)) {
1716 restrictions.putInt(key, Integer.parseInt(value));
1717 } else {
1718 restrictions.putString(key, value);
1719 }
Amith Yamasanidf2e92a2013-03-01 17:04:38 -08001720 }
1721 }
1722 }
Amith Yamasanidf2e92a2013-03-01 17:04:38 -08001723 } catch (IOException ioe) {
1724 } catch (XmlPullParserException pe) {
1725 } finally {
1726 if (fis != null) {
1727 try {
1728 fis.close();
1729 } catch (IOException e) {
1730 }
1731 }
1732 }
Amith Yamasani7e99bc02013-04-16 18:24:51 -07001733 return restrictions;
Amith Yamasanidf2e92a2013-03-01 17:04:38 -08001734 }
1735
1736 private void writeApplicationRestrictionsLocked(String packageName,
Amith Yamasani7e99bc02013-04-16 18:24:51 -07001737 Bundle restrictions, int userId) {
Amith Yamasanidf2e92a2013-03-01 17:04:38 -08001738 FileOutputStream fos = null;
1739 AtomicFile restrictionsFile = new AtomicFile(
1740 new File(Environment.getUserSystemDirectory(userId),
Amith Yamasanifc95e702013-09-26 13:20:17 -07001741 packageToRestrictionsFileName(packageName)));
Amith Yamasanidf2e92a2013-03-01 17:04:38 -08001742 try {
1743 fos = restrictionsFile.startWrite();
1744 final BufferedOutputStream bos = new BufferedOutputStream(fos);
1745
1746 // XmlSerializer serializer = XmlUtils.serializerInstance();
1747 final XmlSerializer serializer = new FastXmlSerializer();
1748 serializer.setOutput(bos, "utf-8");
1749 serializer.startDocument(null, true);
1750 serializer.setFeature("http://xmlpull.org/v1/doc/features.html#indent-output", true);
1751
1752 serializer.startTag(null, TAG_RESTRICTIONS);
1753
Amith Yamasani7e99bc02013-04-16 18:24:51 -07001754 for (String key : restrictions.keySet()) {
1755 Object value = restrictions.get(key);
Amith Yamasanidf2e92a2013-03-01 17:04:38 -08001756 serializer.startTag(null, TAG_ENTRY);
Amith Yamasani7e99bc02013-04-16 18:24:51 -07001757 serializer.attribute(null, ATTR_KEY, key);
1758
1759 if (value instanceof Boolean) {
1760 serializer.attribute(null, ATTR_VALUE_TYPE, ATTR_TYPE_BOOLEAN);
1761 serializer.text(value.toString());
Amith Yamasani5b5aa402014-06-01 20:10:14 -07001762 } else if (value instanceof Integer) {
1763 serializer.attribute(null, ATTR_VALUE_TYPE, ATTR_TYPE_INTEGER);
1764 serializer.text(value.toString());
Amith Yamasani7e99bc02013-04-16 18:24:51 -07001765 } else if (value == null || value instanceof String) {
1766 serializer.attribute(null, ATTR_VALUE_TYPE, ATTR_TYPE_STRING);
1767 serializer.text(value != null ? (String) value : "");
Amith Yamasanidf2e92a2013-03-01 17:04:38 -08001768 } else {
Amith Yamasani7e99bc02013-04-16 18:24:51 -07001769 serializer.attribute(null, ATTR_VALUE_TYPE, ATTR_TYPE_STRING_ARRAY);
1770 String[] values = (String[]) value;
Amith Yamasanidf2e92a2013-03-01 17:04:38 -08001771 serializer.attribute(null, ATTR_MULTIPLE, Integer.toString(values.length));
Amith Yamasani7e99bc02013-04-16 18:24:51 -07001772 for (String choice : values) {
Amith Yamasanidf2e92a2013-03-01 17:04:38 -08001773 serializer.startTag(null, TAG_VALUE);
Amith Yamasani7e99bc02013-04-16 18:24:51 -07001774 serializer.text(choice != null ? choice : "");
Amith Yamasanidf2e92a2013-03-01 17:04:38 -08001775 serializer.endTag(null, TAG_VALUE);
1776 }
1777 }
1778 serializer.endTag(null, TAG_ENTRY);
1779 }
1780
1781 serializer.endTag(null, TAG_RESTRICTIONS);
1782
1783 serializer.endDocument();
1784 restrictionsFile.finishWrite(fos);
1785 } catch (Exception e) {
1786 restrictionsFile.failWrite(fos);
1787 Slog.e(LOG_TAG, "Error writing application restrictions list");
1788 }
1789 }
1790
1791 @Override
Amith Yamasani2a003292012-08-14 18:25:45 -07001792 public int getUserSerialNumber(int userHandle) {
Dianne Hackborn4428e172012-08-24 17:43:05 -07001793 synchronized (mPackagesLock) {
Amith Yamasani2a003292012-08-14 18:25:45 -07001794 if (!exists(userHandle)) return -1;
Amith Yamasani195263742012-08-21 15:40:12 -07001795 return getUserInfoLocked(userHandle).serialNumber;
Amith Yamasani2a003292012-08-14 18:25:45 -07001796 }
1797 }
1798
1799 @Override
1800 public int getUserHandle(int userSerialNumber) {
Dianne Hackborn4428e172012-08-24 17:43:05 -07001801 synchronized (mPackagesLock) {
Amith Yamasani2a003292012-08-14 18:25:45 -07001802 for (int userId : mUserIds) {
Amith Yamasani195263742012-08-21 15:40:12 -07001803 if (getUserInfoLocked(userId).serialNumber == userSerialNumber) return userId;
Amith Yamasani2a003292012-08-14 18:25:45 -07001804 }
1805 // Not found
1806 return -1;
Amith Yamasani13593602012-03-22 16:16:17 -07001807 }
1808 }
1809
Amith Yamasani0b285492011-04-14 17:35:23 -07001810 /**
1811 * Caches the list of user ids in an array, adjusting the array size when necessary.
1812 */
Amith Yamasani13593602012-03-22 16:16:17 -07001813 private void updateUserIdsLocked() {
Dianne Hackbornd4ac8d72012-09-27 23:20:10 -07001814 int num = 0;
Amith Yamasani0b285492011-04-14 17:35:23 -07001815 for (int i = 0; i < mUsers.size(); i++) {
Dianne Hackbornd4ac8d72012-09-27 23:20:10 -07001816 if (!mUsers.valueAt(i).partial) {
1817 num++;
1818 }
1819 }
Amith Yamasani16389312012-10-17 21:20:14 -07001820 final int[] newUsers = new int[num];
1821 int n = 0;
Dianne Hackbornd4ac8d72012-09-27 23:20:10 -07001822 for (int i = 0; i < mUsers.size(); i++) {
1823 if (!mUsers.valueAt(i).partial) {
Amith Yamasani16389312012-10-17 21:20:14 -07001824 newUsers[n++] = mUsers.keyAt(i);
Dianne Hackbornd4ac8d72012-09-27 23:20:10 -07001825 }
Amith Yamasani0b285492011-04-14 17:35:23 -07001826 }
Dianne Hackborn7767eac2012-08-23 18:25:40 -07001827 mUserIds = newUsers;
Amith Yamasani0b285492011-04-14 17:35:23 -07001828 }
1829
1830 /**
Amith Yamasani1a7472e2013-07-02 11:17:30 -07001831 * Make a note of the last started time of a user and do some cleanup.
Amith Yamasani920ace02012-09-20 22:15:37 -07001832 * @param userId the user that was just foregrounded
1833 */
1834 public void userForeground(int userId) {
1835 synchronized (mPackagesLock) {
1836 UserInfo user = mUsers.get(userId);
1837 long now = System.currentTimeMillis();
Dianne Hackbornd4ac8d72012-09-27 23:20:10 -07001838 if (user == null || user.partial) {
1839 Slog.w(LOG_TAG, "userForeground: unknown user #" + userId);
1840 return;
1841 }
1842 if (now > EPOCH_PLUS_30_YEARS) {
Amith Yamasani920ace02012-09-20 22:15:37 -07001843 user.lastLoggedInTime = now;
Fyodor Kupolov75d0ea82014-12-15 16:21:07 -08001844 scheduleWriteUserLocked(user);
Amith Yamasani920ace02012-09-20 22:15:37 -07001845 }
1846 }
1847 }
1848
1849 /**
Amith Yamasani0b285492011-04-14 17:35:23 -07001850 * Returns the next available user id, filling in any holes in the ids.
Amith Yamasani742a6712011-05-04 14:49:28 -07001851 * TODO: May not be a good idea to recycle ids, in case it results in confusion
1852 * for data and battery stats collection, or unexpected cross-talk.
Amith Yamasani0b285492011-04-14 17:35:23 -07001853 * @return
1854 */
Dianne Hackborn5dc5a002012-09-15 19:33:48 -07001855 private int getNextAvailableIdLocked() {
Dianne Hackborn4428e172012-08-24 17:43:05 -07001856 synchronized (mPackagesLock) {
Jeff Sharkeyffe0cb42012-11-05 17:24:43 -08001857 int i = MIN_USER_ID;
Amith Yamasani195263742012-08-21 15:40:12 -07001858 while (i < Integer.MAX_VALUE) {
Jeff Sharkeyffe0cb42012-11-05 17:24:43 -08001859 if (mUsers.indexOfKey(i) < 0 && !mRemovingUserIds.get(i)) {
Amith Yamasani195263742012-08-21 15:40:12 -07001860 break;
1861 }
1862 i++;
Amith Yamasani4b2e9342011-03-31 12:38:53 -07001863 }
Amith Yamasani195263742012-08-21 15:40:12 -07001864 return i;
Amith Yamasani4b2e9342011-03-31 12:38:53 -07001865 }
Amith Yamasani4b2e9342011-03-31 12:38:53 -07001866 }
Amith Yamasani920ace02012-09-20 22:15:37 -07001867
Amith Yamasanifc95e702013-09-26 13:20:17 -07001868 private String packageToRestrictionsFileName(String packageName) {
1869 return RESTRICTIONS_FILE_PREFIX + packageName + XML_SUFFIX;
1870 }
1871
1872 private String restrictionsFileNameToPackage(String fileName) {
1873 return fileName.substring(RESTRICTIONS_FILE_PREFIX.length(),
1874 (int) (fileName.length() - XML_SUFFIX.length()));
1875 }
1876
Amith Yamasani920ace02012-09-20 22:15:37 -07001877 @Override
1878 protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
1879 if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.DUMP)
1880 != PackageManager.PERMISSION_GRANTED) {
1881 pw.println("Permission Denial: can't dump UserManager from from pid="
1882 + Binder.getCallingPid()
1883 + ", uid=" + Binder.getCallingUid()
1884 + " without permission "
1885 + android.Manifest.permission.DUMP);
1886 return;
1887 }
1888
1889 long now = System.currentTimeMillis();
1890 StringBuilder sb = new StringBuilder();
1891 synchronized (mPackagesLock) {
1892 pw.println("Users:");
1893 for (int i = 0; i < mUsers.size(); i++) {
1894 UserInfo user = mUsers.valueAt(i);
1895 if (user == null) continue;
Amith Yamasani634cf312012-10-04 17:34:21 -07001896 pw.print(" "); pw.print(user); pw.print(" serialNo="); pw.print(user.serialNumber);
Jeff Sharkeyffe0cb42012-11-05 17:24:43 -08001897 if (mRemovingUserIds.get(mUsers.keyAt(i))) pw.print(" <removing> ");
Dianne Hackbornd4ac8d72012-09-27 23:20:10 -07001898 if (user.partial) pw.print(" <partial>");
1899 pw.println();
Amith Yamasani920ace02012-09-20 22:15:37 -07001900 pw.print(" Created: ");
1901 if (user.creationTime == 0) {
1902 pw.println("<unknown>");
1903 } else {
1904 sb.setLength(0);
1905 TimeUtils.formatDuration(now - user.creationTime, sb);
1906 sb.append(" ago");
1907 pw.println(sb);
1908 }
1909 pw.print(" Last logged in: ");
1910 if (user.lastLoggedInTime == 0) {
1911 pw.println("<unknown>");
1912 } else {
1913 sb.setLength(0);
1914 TimeUtils.formatDuration(now - user.lastLoggedInTime, sb);
1915 sb.append(" ago");
1916 pw.println(sb);
1917 }
1918 }
1919 }
1920 }
Fyodor Kupolov75d0ea82014-12-15 16:21:07 -08001921
1922 final class MainHandler extends Handler {
1923
1924 @Override
1925 public void handleMessage(Message msg) {
1926 switch (msg.what) {
1927 case WRITE_USER_MSG:
1928 removeMessages(WRITE_USER_MSG, msg.obj);
1929 synchronized (mPackagesLock) {
1930 int userId = ((UserInfo) msg.obj).id;
1931 UserInfo userInfo = mUsers.get(userId);
1932 if (userInfo != null) {
1933 writeUserLocked(userInfo);
1934 }
1935 }
1936 }
1937 }
1938 }
Amith Yamasani4b2e9342011-03-31 12:38:53 -07001939}