blob: 32515b5b73b193722ffbb822bc10b3dfe634175a [file] [log] [blame]
Amith Yamasani4b2e9342011-03-31 12:38:53 -07001/*
2 * Copyright (C) 2011 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.android.server.pm;
18
Jeff Sharkeyffe0cb42012-11-05 17:24:43 -080019import static android.text.format.DateUtils.MINUTE_IN_MILLIS;
Amith Yamasani4b2e9342011-03-31 12:38:53 -070020
Amith Yamasanidb6a14c2012-10-17 21:16:52 -070021import android.app.Activity;
Amith Yamasani2a003292012-08-14 18:25:45 -070022import android.app.ActivityManager;
Dianne Hackborn80a4af22012-08-27 19:18:31 -070023import android.app.ActivityManagerNative;
Amith Yamasani1a7472e2013-07-02 11:17:30 -070024import android.app.ActivityThread;
Dianne Hackborn80a4af22012-08-27 19:18:31 -070025import android.app.IStopUserCallback;
Amith Yamasanidb6a14c2012-10-17 21:16:52 -070026import android.content.BroadcastReceiver;
Amith Yamasani258848d2012-08-10 17:06:33 -070027import android.content.Context;
28import android.content.Intent;
Amith Yamasani1a7472e2013-07-02 11:17:30 -070029import android.content.pm.ApplicationInfo;
Amith Yamasani0b285492011-04-14 17:35:23 -070030import android.content.pm.PackageManager;
Amith Yamasanidf2e92a2013-03-01 17:04:38 -080031import android.content.pm.PackageManager.NameNotFoundException;
Amith Yamasani4b2e9342011-03-31 12:38:53 -070032import android.content.pm.UserInfo;
Amith Yamasanie928d7d2012-09-17 21:46:51 -070033import android.graphics.Bitmap;
34import android.graphics.BitmapFactory;
Amith Yamasani258848d2012-08-10 17:06:33 -070035import android.os.Binder;
Amith Yamasanie4cf7342012-12-17 11:12:09 -080036import android.os.Bundle;
Amith Yamasani4b2e9342011-03-31 12:38:53 -070037import android.os.Environment;
38import android.os.FileUtils;
Jeff Sharkeyffe0cb42012-11-05 17:24:43 -080039import android.os.Handler;
Amith Yamasani258848d2012-08-10 17:06:33 -070040import android.os.IUserManager;
Amith Yamasani258848d2012-08-10 17:06:33 -070041import android.os.Process;
Dianne Hackborn80a4af22012-08-27 19:18:31 -070042import android.os.RemoteException;
Dianne Hackbornf02b60a2012-08-16 10:48:27 -070043import android.os.UserHandle;
Jeff Sharkey27bd34d2012-09-16 12:49:00 -070044import android.os.UserManager;
Amith Yamasani2a003292012-08-14 18:25:45 -070045import android.util.AtomicFile;
Amith Yamasani655d0e22013-06-12 14:19:10 -070046import android.util.Log;
Amith Yamasani4b2e9342011-03-31 12:38:53 -070047import android.util.Slog;
48import android.util.SparseArray;
Jeff Sharkeyffe0cb42012-11-05 17:24:43 -080049import android.util.SparseBooleanArray;
Amith Yamasani920ace02012-09-20 22:15:37 -070050import android.util.TimeUtils;
Amith Yamasani4b2e9342011-03-31 12:38:53 -070051import android.util.Xml;
52
Amith Yamasani1a7472e2013-07-02 11:17:30 -070053import com.android.internal.content.PackageMonitor;
Jeff Sharkeyffe0cb42012-11-05 17:24:43 -080054import com.android.internal.util.ArrayUtils;
55import com.android.internal.util.FastXmlSerializer;
56
57import org.xmlpull.v1.XmlPullParser;
58import org.xmlpull.v1.XmlPullParserException;
59import org.xmlpull.v1.XmlSerializer;
60
Amith Yamasani4b2e9342011-03-31 12:38:53 -070061import java.io.BufferedOutputStream;
62import java.io.File;
Amith Yamasani920ace02012-09-20 22:15:37 -070063import java.io.FileDescriptor;
Amith Yamasani4b2e9342011-03-31 12:38:53 -070064import java.io.FileInputStream;
Amith Yamasanib8151ec2012-04-18 18:02:48 -070065import java.io.FileNotFoundException;
Amith Yamasani4b2e9342011-03-31 12:38:53 -070066import java.io.FileOutputStream;
67import java.io.IOException;
Amith Yamasani920ace02012-09-20 22:15:37 -070068import java.io.PrintWriter;
Amith Yamasani655d0e22013-06-12 14:19:10 -070069import java.security.MessageDigest;
70import java.security.NoSuchAlgorithmException;
71import java.security.SecureRandom;
Amith Yamasani4b2e9342011-03-31 12:38:53 -070072import java.util.ArrayList;
73import java.util.List;
74
Amith Yamasani258848d2012-08-10 17:06:33 -070075public class UserManagerService extends IUserManager.Stub {
Amith Yamasanib8151ec2012-04-18 18:02:48 -070076
Amith Yamasani2a003292012-08-14 18:25:45 -070077 private static final String LOG_TAG = "UserManagerService";
Amith Yamasanib8151ec2012-04-18 18:02:48 -070078
Amith Yamasani16389312012-10-17 21:20:14 -070079 private static final boolean DBG = false;
80
Amith Yamasani4b2e9342011-03-31 12:38:53 -070081 private static final String TAG_NAME = "name";
Amith Yamasani4b2e9342011-03-31 12:38:53 -070082 private static final String ATTR_FLAGS = "flags";
Amith Yamasanib8151ec2012-04-18 18:02:48 -070083 private static final String ATTR_ICON_PATH = "icon";
Amith Yamasani4b2e9342011-03-31 12:38:53 -070084 private static final String ATTR_ID = "id";
Amith Yamasani920ace02012-09-20 22:15:37 -070085 private static final String ATTR_CREATION_TIME = "created";
86 private static final String ATTR_LAST_LOGGED_IN_TIME = "lastLoggedIn";
Amith Yamasani655d0e22013-06-12 14:19:10 -070087 private static final String ATTR_SALT = "salt";
88 private static final String ATTR_PIN_HASH = "pinHash";
89 private static final String ATTR_FAILED_ATTEMPTS = "failedAttempts";
90 private static final String ATTR_LAST_RETRY_MS = "lastAttemptMs";
Amith Yamasani2a003292012-08-14 18:25:45 -070091 private static final String ATTR_SERIAL_NO = "serialNumber";
92 private static final String ATTR_NEXT_SERIAL_NO = "nextSerialNumber";
Dianne Hackbornd4ac8d72012-09-27 23:20:10 -070093 private static final String ATTR_PARTIAL = "partial";
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 Yamasani4b2e9342011-03-31 12:38:53 -070096 private static final String TAG_USERS = "users";
Amith Yamasani4b2e9342011-03-31 12:38:53 -070097 private static final String TAG_USER = "user";
Amith Yamasanie4cf7342012-12-17 11:12:09 -080098 private static final String TAG_RESTRICTIONS = "restrictions";
Amith Yamasanidf2e92a2013-03-01 17:04:38 -080099 private static final String TAG_ENTRY = "entry";
100 private static final String TAG_VALUE = "value";
101 private static final String ATTR_KEY = "key";
Amith Yamasani7e99bc02013-04-16 18:24:51 -0700102 private static final String ATTR_VALUE_TYPE = "type";
Amith Yamasanidf2e92a2013-03-01 17:04:38 -0800103 private static final String ATTR_MULTIPLE = "m";
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700104
Amith Yamasani7e99bc02013-04-16 18:24:51 -0700105 private static final String ATTR_TYPE_STRING_ARRAY = "sa";
106 private static final String ATTR_TYPE_STRING = "s";
107 private static final String ATTR_TYPE_BOOLEAN = "b";
108
Amith Yamasani0b285492011-04-14 17:35:23 -0700109 private static final String USER_INFO_DIR = "system" + File.separator + "users";
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700110 private static final String USER_LIST_FILENAME = "userlist.xml";
Amith Yamasanib8151ec2012-04-18 18:02:48 -0700111 private static final String USER_PHOTO_FILENAME = "photo.png";
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700112
Amith Yamasanidf2e92a2013-03-01 17:04:38 -0800113 private static final String RESTRICTIONS_FILE_PREFIX = "res_";
Amith Yamasanifc95e702013-09-26 13:20:17 -0700114 private static final String XML_SUFFIX = ".xml";
Amith Yamasanidf2e92a2013-03-01 17:04:38 -0800115
Amith Yamasani634cf312012-10-04 17:34:21 -0700116 private static final int MIN_USER_ID = 10;
117
Amith Yamasani5e486f52013-08-07 11:06:44 -0700118 private static final int USER_VERSION = 4;
Amith Yamasani6f34b412012-10-22 18:19:27 -0700119
Amith Yamasani920ace02012-09-20 22:15:37 -0700120 private static final long EPOCH_PLUS_30_YEARS = 30L * 365 * 24 * 60 * 60 * 1000L; // ms
121
Amith Yamasani655d0e22013-06-12 14:19:10 -0700122 // Number of attempts before jumping to the next BACKOFF_TIMES slot
123 private static final int BACKOFF_INC_INTERVAL = 5;
124
125 // Amount of time to force the user to wait before entering the PIN again, after failing
126 // BACKOFF_INC_INTERVAL times.
127 private static final int[] BACKOFF_TIMES = { 0, 30*1000, 60*1000, 5*60*1000, 30*60*1000 };
128
Dianne Hackborn4428e172012-08-24 17:43:05 -0700129 private final Context mContext;
130 private final PackageManagerService mPm;
131 private final Object mInstallLock;
132 private final Object mPackagesLock;
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700133
Jeff Sharkeyffe0cb42012-11-05 17:24:43 -0800134 private final Handler mHandler;
135
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700136 private final File mUsersDir;
137 private final File mUserListFile;
Dianne Hackborn4428e172012-08-24 17:43:05 -0700138 private final File mBaseUserPath;
139
Jeff Sharkeyffe0cb42012-11-05 17:24:43 -0800140 private final SparseArray<UserInfo> mUsers = new SparseArray<UserInfo>();
Amith Yamasanie4cf7342012-12-17 11:12:09 -0800141 private final SparseArray<Bundle> mUserRestrictions = new SparseArray<Bundle>();
Jeff Sharkeyffe0cb42012-11-05 17:24:43 -0800142
Amith Yamasani655d0e22013-06-12 14:19:10 -0700143 class RestrictionsPinState {
144 long salt;
145 String pinHash;
146 int failedAttempts;
147 long lastAttemptTime;
148 }
149
150 private final SparseArray<RestrictionsPinState> mRestrictionsPinStates =
151 new SparseArray<RestrictionsPinState>();
152
Jeff Sharkeyffe0cb42012-11-05 17:24:43 -0800153 /**
154 * Set of user IDs being actively removed. Removed IDs linger in this set
155 * for several seconds to work around a VFS caching issue.
156 */
157 // @GuardedBy("mPackagesLock")
158 private final SparseBooleanArray mRemovingUserIds = new SparseBooleanArray();
Dianne Hackborn4428e172012-08-24 17:43:05 -0700159
Amith Yamasani0b285492011-04-14 17:35:23 -0700160 private int[] mUserIds;
Amith Yamasani258848d2012-08-10 17:06:33 -0700161 private boolean mGuestEnabled;
Amith Yamasani2a003292012-08-14 18:25:45 -0700162 private int mNextSerialNumber;
Amith Yamasani6f34b412012-10-22 18:19:27 -0700163 private int mUserVersion = 0;
Amith Yamasani0b285492011-04-14 17:35:23 -0700164
Amith Yamasani258848d2012-08-10 17:06:33 -0700165 private static UserManagerService sInstance;
Amith Yamasani258848d2012-08-10 17:06:33 -0700166
Dianne Hackborn4428e172012-08-24 17:43:05 -0700167 public static UserManagerService getInstance() {
168 synchronized (UserManagerService.class) {
169 return sInstance;
Amith Yamasani258848d2012-08-10 17:06:33 -0700170 }
Amith Yamasani258848d2012-08-10 17:06:33 -0700171 }
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700172
173 /**
174 * Available for testing purposes.
175 */
Amith Yamasani258848d2012-08-10 17:06:33 -0700176 UserManagerService(File dataDir, File baseUserPath) {
Dianne Hackborn4428e172012-08-24 17:43:05 -0700177 this(null, null, new Object(), new Object(), dataDir, baseUserPath);
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700178 }
179
Dianne Hackborn4428e172012-08-24 17:43:05 -0700180 /**
181 * Called by package manager to create the service. This is closely
182 * associated with the package manager, and the given lock is the
183 * package manager's own lock.
184 */
185 UserManagerService(Context context, PackageManagerService pm,
186 Object installLock, Object packagesLock) {
187 this(context, pm, installLock, packagesLock,
188 Environment.getDataDirectory(),
189 new File(Environment.getDataDirectory(), "user"));
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700190 }
191
Dianne Hackborn4428e172012-08-24 17:43:05 -0700192 /**
193 * Available for testing purposes.
194 */
195 private UserManagerService(Context context, PackageManagerService pm,
196 Object installLock, Object packagesLock,
197 File dataDir, File baseUserPath) {
Dianne Hackbornd4ac8d72012-09-27 23:20:10 -0700198 mContext = context;
199 mPm = pm;
200 mInstallLock = installLock;
201 mPackagesLock = packagesLock;
Jeff Sharkeyffe0cb42012-11-05 17:24:43 -0800202 mHandler = new Handler();
Dianne Hackbornd4ac8d72012-09-27 23:20:10 -0700203 synchronized (mInstallLock) {
204 synchronized (mPackagesLock) {
205 mUsersDir = new File(dataDir, USER_INFO_DIR);
206 mUsersDir.mkdirs();
207 // Make zeroth user directory, for services to migrate their files to that location
208 File userZeroDir = new File(mUsersDir, "0");
209 userZeroDir.mkdirs();
210 mBaseUserPath = baseUserPath;
211 FileUtils.setPermissions(mUsersDir.toString(),
212 FileUtils.S_IRWXU|FileUtils.S_IRWXG
213 |FileUtils.S_IROTH|FileUtils.S_IXOTH,
214 -1, -1);
215 mUserListFile = new File(mUsersDir, USER_LIST_FILENAME);
216 readUserListLocked();
Amith Yamasani756901d2012-10-12 12:30:07 -0700217 // Prune out any partially created/partially removed users.
Dianne Hackbornd4ac8d72012-09-27 23:20:10 -0700218 ArrayList<UserInfo> partials = new ArrayList<UserInfo>();
219 for (int i = 0; i < mUsers.size(); i++) {
220 UserInfo ui = mUsers.valueAt(i);
221 if (ui.partial && i != 0) {
222 partials.add(ui);
223 }
224 }
225 for (int i = 0; i < partials.size(); i++) {
226 UserInfo ui = partials.get(i);
227 Slog.w(LOG_TAG, "Removing partially created user #" + i
228 + " (name=" + ui.name + ")");
229 removeUserStateLocked(ui.id);
230 }
231 sInstance = this;
232 }
Dianne Hackborn4428e172012-08-24 17:43:05 -0700233 }
Amith Yamasani1a7472e2013-07-02 11:17:30 -0700234 }
235
236 void systemReady() {
Dianne Hackborn10ad9822014-03-17 11:28:36 -0700237 mUserPackageMonitor.register(mContext, null, UserHandle.ALL, false);
Amith Yamasani1a7472e2013-07-02 11:17:30 -0700238 userForeground(UserHandle.USER_OWNER);
Amith Yamasani258848d2012-08-10 17:06:33 -0700239 }
240
241 @Override
Amith Yamasani920ace02012-09-20 22:15:37 -0700242 public List<UserInfo> getUsers(boolean excludeDying) {
Amith Yamasani2a003292012-08-14 18:25:45 -0700243 checkManageUsersPermission("query users");
Dianne Hackborn4428e172012-08-24 17:43:05 -0700244 synchronized (mPackagesLock) {
Amith Yamasani13593602012-03-22 16:16:17 -0700245 ArrayList<UserInfo> users = new ArrayList<UserInfo>(mUsers.size());
246 for (int i = 0; i < mUsers.size(); i++) {
Dianne Hackbornd4ac8d72012-09-27 23:20:10 -0700247 UserInfo ui = mUsers.valueAt(i);
248 if (ui.partial) {
249 continue;
250 }
Jeff Sharkeyffe0cb42012-11-05 17:24:43 -0800251 if (!excludeDying || !mRemovingUserIds.get(ui.id)) {
Dianne Hackbornd4ac8d72012-09-27 23:20:10 -0700252 users.add(ui);
Amith Yamasani920ace02012-09-20 22:15:37 -0700253 }
Amith Yamasani13593602012-03-22 16:16:17 -0700254 }
255 return users;
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700256 }
Amith Yamasani13593602012-03-22 16:16:17 -0700257 }
258
Amith Yamasani258848d2012-08-10 17:06:33 -0700259 @Override
Kenny Guy2a764942014-04-02 13:29:20 +0100260 public List<UserInfo> getProfiles(int userId) {
Amith Yamasani4f582632014-02-19 14:31:52 -0800261 if (userId != UserHandle.getCallingUserId()) {
262 checkManageUsersPermission("getting profiles related to user " + userId);
263 }
Kenny Guya52dc3e2014-02-11 15:33:14 +0000264 synchronized (mPackagesLock) {
265 UserInfo user = getUserInfoLocked(userId);
266 ArrayList<UserInfo> users = new ArrayList<UserInfo>(mUsers.size());
267 for (int i = 0; i < mUsers.size(); i++) {
Kenny Guy2a764942014-04-02 13:29:20 +0100268 UserInfo profile = mUsers.valueAt(i);
269 if (!isProfileOf(user, profile)) {
Kenny Guya52dc3e2014-02-11 15:33:14 +0000270 continue;
271 }
Kenny Guy2a764942014-04-02 13:29:20 +0100272 users.add(profile);
Kenny Guya52dc3e2014-02-11 15:33:14 +0000273 }
274 return users;
275 }
276 }
277
Kenny Guy2a764942014-04-02 13:29:20 +0100278 private boolean isProfileOf(UserInfo user, UserInfo profile) {
279 return user.id == profile.id ||
280 (user.profileGroupId != UserInfo.NO_PROFILE_GROUP_ID
281 && user.profileGroupId == profile.profileGroupId);
Kenny Guya52dc3e2014-02-11 15:33:14 +0000282 }
283
284 @Override
Amith Yamasani258848d2012-08-10 17:06:33 -0700285 public UserInfo getUserInfo(int userId) {
Amith Yamasani2a003292012-08-14 18:25:45 -0700286 checkManageUsersPermission("query user");
Dianne Hackborn4428e172012-08-24 17:43:05 -0700287 synchronized (mPackagesLock) {
Amith Yamasani195263742012-08-21 15:40:12 -0700288 return getUserInfoLocked(userId);
Amith Yamasani13593602012-03-22 16:16:17 -0700289 }
290 }
291
Amith Yamasani71e6c692013-03-24 17:39:28 -0700292 @Override
293 public boolean isRestricted() {
294 synchronized (mPackagesLock) {
295 return getUserInfoLocked(UserHandle.getCallingUserId()).isRestricted();
296 }
297 }
298
Amith Yamasani195263742012-08-21 15:40:12 -0700299 /*
300 * Should be locked on mUsers before calling this.
301 */
302 private UserInfo getUserInfoLocked(int userId) {
Dianne Hackbornd4ac8d72012-09-27 23:20:10 -0700303 UserInfo ui = mUsers.get(userId);
Amith Yamasani16389312012-10-17 21:20:14 -0700304 // If it is partial and not in the process of being removed, return as unknown user.
Jeff Sharkeyffe0cb42012-11-05 17:24:43 -0800305 if (ui != null && ui.partial && !mRemovingUserIds.get(userId)) {
Dianne Hackbornd4ac8d72012-09-27 23:20:10 -0700306 Slog.w(LOG_TAG, "getUserInfo: unknown user #" + userId);
307 return null;
308 }
309 return ui;
Amith Yamasani195263742012-08-21 15:40:12 -0700310 }
311
Amith Yamasani13593602012-03-22 16:16:17 -0700312 public boolean exists(int userId) {
Dianne Hackborn4428e172012-08-24 17:43:05 -0700313 synchronized (mPackagesLock) {
Amith Yamasani13593602012-03-22 16:16:17 -0700314 return ArrayUtils.contains(mUserIds, userId);
315 }
316 }
317
Amith Yamasani258848d2012-08-10 17:06:33 -0700318 @Override
Amith Yamasanib8151ec2012-04-18 18:02:48 -0700319 public void setUserName(int userId, String name) {
Amith Yamasani2a003292012-08-14 18:25:45 -0700320 checkManageUsersPermission("rename users");
Dianne Hackbornd4ac8d72012-09-27 23:20:10 -0700321 boolean changed = false;
Dianne Hackborn4428e172012-08-24 17:43:05 -0700322 synchronized (mPackagesLock) {
Amith Yamasani13593602012-03-22 16:16:17 -0700323 UserInfo info = mUsers.get(userId);
Dianne Hackbornd4ac8d72012-09-27 23:20:10 -0700324 if (info == null || info.partial) {
325 Slog.w(LOG_TAG, "setUserName: unknown user #" + userId);
326 return;
327 }
Amith Yamasani13593602012-03-22 16:16:17 -0700328 if (name != null && !name.equals(info.name)) {
329 info.name = name;
330 writeUserLocked(info);
Dianne Hackbornd4ac8d72012-09-27 23:20:10 -0700331 changed = true;
Amith Yamasani13593602012-03-22 16:16:17 -0700332 }
333 }
Dianne Hackbornd4ac8d72012-09-27 23:20:10 -0700334 if (changed) {
335 sendUserInfoChangedBroadcast(userId);
336 }
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700337 }
338
Amith Yamasani258848d2012-08-10 17:06:33 -0700339 @Override
Amith Yamasanie928d7d2012-09-17 21:46:51 -0700340 public void setUserIcon(int userId, Bitmap bitmap) {
Amith Yamasani2a003292012-08-14 18:25:45 -0700341 checkManageUsersPermission("update users");
Dianne Hackborn4428e172012-08-24 17:43:05 -0700342 synchronized (mPackagesLock) {
Amith Yamasanib8151ec2012-04-18 18:02:48 -0700343 UserInfo info = mUsers.get(userId);
Dianne Hackbornd4ac8d72012-09-27 23:20:10 -0700344 if (info == null || info.partial) {
345 Slog.w(LOG_TAG, "setUserIcon: unknown user #" + userId);
346 return;
347 }
Amith Yamasanie928d7d2012-09-17 21:46:51 -0700348 writeBitmapLocked(info, bitmap);
349 writeUserLocked(info);
Amith Yamasanib8151ec2012-04-18 18:02:48 -0700350 }
Amith Yamasanie928d7d2012-09-17 21:46:51 -0700351 sendUserInfoChangedBroadcast(userId);
352 }
353
354 private void sendUserInfoChangedBroadcast(int userId) {
355 Intent changedIntent = new Intent(Intent.ACTION_USER_INFO_CHANGED);
356 changedIntent.putExtra(Intent.EXTRA_USER_HANDLE, userId);
357 changedIntent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY);
Amith Yamasani6fc1d4e2013-05-08 16:43:58 -0700358 mContext.sendBroadcastAsUser(changedIntent, UserHandle.ALL);
Amith Yamasanib8151ec2012-04-18 18:02:48 -0700359 }
360
Amith Yamasani258848d2012-08-10 17:06:33 -0700361 @Override
Amith Yamasanie928d7d2012-09-17 21:46:51 -0700362 public Bitmap getUserIcon(int userId) {
Amith Yamasani3b49f072012-09-17 10:21:43 -0700363 checkManageUsersPermission("read users");
364 synchronized (mPackagesLock) {
365 UserInfo info = mUsers.get(userId);
Dianne Hackbornd4ac8d72012-09-27 23:20:10 -0700366 if (info == null || info.partial) {
367 Slog.w(LOG_TAG, "getUserIcon: unknown user #" + userId);
368 return null;
369 }
370 if (info.iconPath == null) {
371 return null;
372 }
Amith Yamasanie928d7d2012-09-17 21:46:51 -0700373 return BitmapFactory.decodeFile(info.iconPath);
Amith Yamasani3b49f072012-09-17 10:21:43 -0700374 }
375 }
376
377 @Override
Amith Yamasani258848d2012-08-10 17:06:33 -0700378 public void setGuestEnabled(boolean enable) {
Amith Yamasani2a003292012-08-14 18:25:45 -0700379 checkManageUsersPermission("enable guest users");
Dianne Hackborn4428e172012-08-24 17:43:05 -0700380 synchronized (mPackagesLock) {
Amith Yamasani258848d2012-08-10 17:06:33 -0700381 if (mGuestEnabled != enable) {
382 mGuestEnabled = enable;
383 // Erase any guest user that currently exists
384 for (int i = 0; i < mUsers.size(); i++) {
385 UserInfo user = mUsers.valueAt(i);
Dianne Hackbornd4ac8d72012-09-27 23:20:10 -0700386 if (!user.partial && user.isGuest()) {
Amith Yamasani258848d2012-08-10 17:06:33 -0700387 if (!enable) {
388 removeUser(user.id);
389 }
390 return;
391 }
392 }
393 // No guest was found
394 if (enable) {
395 createUser("Guest", UserInfo.FLAG_GUEST);
396 }
397 }
398 }
399 }
400
401 @Override
402 public boolean isGuestEnabled() {
Dianne Hackborn4428e172012-08-24 17:43:05 -0700403 synchronized (mPackagesLock) {
Amith Yamasani258848d2012-08-10 17:06:33 -0700404 return mGuestEnabled;
405 }
406 }
407
408 @Override
409 public void wipeUser(int userHandle) {
Amith Yamasani2a003292012-08-14 18:25:45 -0700410 checkManageUsersPermission("wipe user");
Amith Yamasani258848d2012-08-10 17:06:33 -0700411 // TODO:
412 }
413
Dianne Hackborn5dc5a002012-09-15 19:33:48 -0700414 public void makeInitialized(int userId) {
415 checkManageUsersPermission("makeInitialized");
416 synchronized (mPackagesLock) {
417 UserInfo info = mUsers.get(userId);
Dianne Hackbornd4ac8d72012-09-27 23:20:10 -0700418 if (info == null || info.partial) {
419 Slog.w(LOG_TAG, "makeInitialized: unknown user #" + userId);
420 }
421 if ((info.flags&UserInfo.FLAG_INITIALIZED) == 0) {
Dianne Hackborn5dc5a002012-09-15 19:33:48 -0700422 info.flags |= UserInfo.FLAG_INITIALIZED;
423 writeUserLocked(info);
424 }
425 }
426 }
427
Amith Yamasanie4cf7342012-12-17 11:12:09 -0800428 @Override
429 public Bundle getUserRestrictions(int userId) {
430 // checkManageUsersPermission("getUserRestrictions");
431
432 synchronized (mPackagesLock) {
433 Bundle restrictions = mUserRestrictions.get(userId);
434 return restrictions != null ? restrictions : Bundle.EMPTY;
435 }
436 }
437
438 @Override
439 public void setUserRestrictions(Bundle restrictions, int userId) {
440 checkManageUsersPermission("setUserRestrictions");
Amith Yamasani0343ec32013-07-22 14:52:06 -0700441 if (restrictions == null) return;
Amith Yamasanie4cf7342012-12-17 11:12:09 -0800442
443 synchronized (mPackagesLock) {
Amith Yamasani350962c2013-08-06 11:18:53 -0700444 mUserRestrictions.get(userId).clear();
Amith Yamasanie4cf7342012-12-17 11:12:09 -0800445 mUserRestrictions.get(userId).putAll(restrictions);
446 writeUserLocked(mUsers.get(userId));
447 }
448 }
449
Amith Yamasani258848d2012-08-10 17:06:33 -0700450 /**
Amith Yamasanifaea76f2012-09-11 10:59:48 -0700451 * Check if we've hit the limit of how many users can be created.
452 */
Dianne Hackborn5dc5a002012-09-15 19:33:48 -0700453 private boolean isUserLimitReachedLocked() {
454 int nUsers = mUsers.size();
Jeff Sharkey27bd34d2012-09-16 12:49:00 -0700455 return nUsers >= UserManager.getMaxSupportedUsers();
Amith Yamasanifaea76f2012-09-11 10:59:48 -0700456 }
457
458 /**
Amith Yamasani195263742012-08-21 15:40:12 -0700459 * Enforces that only the system UID or root's UID or apps that have the
Dianne Hackborn10ad9822014-03-17 11:28:36 -0700460 * {@link android.Manifest.permission#MANAGE_USERS MANAGE_USERS}
Amith Yamasani195263742012-08-21 15:40:12 -0700461 * permission can make certain calls to the UserManager.
Amith Yamasani258848d2012-08-10 17:06:33 -0700462 *
463 * @param message used as message if SecurityException is thrown
464 * @throws SecurityException if the caller is not system or root
465 */
Amith Yamasani2a003292012-08-14 18:25:45 -0700466 private static final void checkManageUsersPermission(String message) {
Amith Yamasani258848d2012-08-10 17:06:33 -0700467 final int uid = Binder.getCallingUid();
Amith Yamasani2a003292012-08-14 18:25:45 -0700468 if (uid != Process.SYSTEM_UID && uid != 0
469 && ActivityManager.checkComponentPermission(
470 android.Manifest.permission.MANAGE_USERS,
471 uid, -1, true) != PackageManager.PERMISSION_GRANTED) {
472 throw new SecurityException("You need MANAGE_USERS permission to: " + message);
Amith Yamasani258848d2012-08-10 17:06:33 -0700473 }
474 }
475
Amith Yamasanie928d7d2012-09-17 21:46:51 -0700476 private void writeBitmapLocked(UserInfo info, Bitmap bitmap) {
Amith Yamasanib8151ec2012-04-18 18:02:48 -0700477 try {
478 File dir = new File(mUsersDir, Integer.toString(info.id));
479 File file = new File(dir, USER_PHOTO_FILENAME);
480 if (!dir.exists()) {
481 dir.mkdir();
482 FileUtils.setPermissions(
483 dir.getPath(),
484 FileUtils.S_IRWXU|FileUtils.S_IRWXG|FileUtils.S_IXOTH,
485 -1, -1);
486 }
Amith Yamasanie928d7d2012-09-17 21:46:51 -0700487 FileOutputStream os;
488 if (bitmap.compress(Bitmap.CompressFormat.PNG, 100, os = new FileOutputStream(file))) {
Amith Yamasani3b49f072012-09-17 10:21:43 -0700489 info.iconPath = file.getAbsolutePath();
490 }
Amith Yamasanie928d7d2012-09-17 21:46:51 -0700491 try {
492 os.close();
493 } catch (IOException ioe) {
494 // What the ... !
495 }
Amith Yamasanib8151ec2012-04-18 18:02:48 -0700496 } catch (FileNotFoundException e) {
Amith Yamasani2a003292012-08-14 18:25:45 -0700497 Slog.w(LOG_TAG, "Error setting photo for user ", e);
Amith Yamasanib8151ec2012-04-18 18:02:48 -0700498 }
Amith Yamasanib8151ec2012-04-18 18:02:48 -0700499 }
500
Amith Yamasani0b285492011-04-14 17:35:23 -0700501 /**
502 * Returns an array of user ids. This array is cached here for quick access, so do not modify or
503 * cache it elsewhere.
504 * @return the array of user ids.
505 */
Dianne Hackborn1676c852012-09-10 14:52:30 -0700506 public int[] getUserIds() {
Dianne Hackborn4428e172012-08-24 17:43:05 -0700507 synchronized (mPackagesLock) {
Dianne Hackborn7767eac2012-08-23 18:25:40 -0700508 return mUserIds;
509 }
Amith Yamasani0b285492011-04-14 17:35:23 -0700510 }
511
Dianne Hackborn4428e172012-08-24 17:43:05 -0700512 int[] getUserIdsLPr() {
513 return mUserIds;
514 }
515
Amith Yamasani13593602012-03-22 16:16:17 -0700516 private void readUserListLocked() {
Amith Yamasani258848d2012-08-10 17:06:33 -0700517 mGuestEnabled = false;
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700518 if (!mUserListFile.exists()) {
Amith Yamasani13593602012-03-22 16:16:17 -0700519 fallbackToSingleUserLocked();
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700520 return;
521 }
522 FileInputStream fis = null;
Amith Yamasani2a003292012-08-14 18:25:45 -0700523 AtomicFile userListFile = new AtomicFile(mUserListFile);
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700524 try {
Amith Yamasani2a003292012-08-14 18:25:45 -0700525 fis = userListFile.openRead();
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700526 XmlPullParser parser = Xml.newPullParser();
527 parser.setInput(fis, null);
528 int type;
529 while ((type = parser.next()) != XmlPullParser.START_TAG
530 && type != XmlPullParser.END_DOCUMENT) {
531 ;
532 }
533
534 if (type != XmlPullParser.START_TAG) {
Amith Yamasani0b285492011-04-14 17:35:23 -0700535 Slog.e(LOG_TAG, "Unable to read user list");
Amith Yamasani13593602012-03-22 16:16:17 -0700536 fallbackToSingleUserLocked();
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700537 return;
538 }
539
Amith Yamasani2a003292012-08-14 18:25:45 -0700540 mNextSerialNumber = -1;
541 if (parser.getName().equals(TAG_USERS)) {
542 String lastSerialNumber = parser.getAttributeValue(null, ATTR_NEXT_SERIAL_NO);
543 if (lastSerialNumber != null) {
544 mNextSerialNumber = Integer.parseInt(lastSerialNumber);
545 }
Amith Yamasani6f34b412012-10-22 18:19:27 -0700546 String versionNumber = parser.getAttributeValue(null, ATTR_USER_VERSION);
547 if (versionNumber != null) {
548 mUserVersion = Integer.parseInt(versionNumber);
549 }
Amith Yamasani2a003292012-08-14 18:25:45 -0700550 }
551
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700552 while ((type = parser.next()) != XmlPullParser.END_DOCUMENT) {
553 if (type == XmlPullParser.START_TAG && parser.getName().equals(TAG_USER)) {
554 String id = parser.getAttributeValue(null, ATTR_ID);
Amith Yamasanie4cf7342012-12-17 11:12:09 -0800555 UserInfo user = readUserLocked(Integer.parseInt(id));
Amith Yamasani6f34b412012-10-22 18:19:27 -0700556
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700557 if (user != null) {
558 mUsers.put(user.id, user);
Amith Yamasani2a003292012-08-14 18:25:45 -0700559 if (user.isGuest()) {
560 mGuestEnabled = true;
561 }
562 if (mNextSerialNumber < 0 || mNextSerialNumber <= user.id) {
563 mNextSerialNumber = user.id + 1;
564 }
Amith Yamasani258848d2012-08-10 17:06:33 -0700565 }
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700566 }
567 }
Amith Yamasani13593602012-03-22 16:16:17 -0700568 updateUserIdsLocked();
Amith Yamasani350962c2013-08-06 11:18:53 -0700569 upgradeIfNecessaryLocked();
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700570 } catch (IOException ioe) {
Amith Yamasani13593602012-03-22 16:16:17 -0700571 fallbackToSingleUserLocked();
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700572 } catch (XmlPullParserException pe) {
Amith Yamasani13593602012-03-22 16:16:17 -0700573 fallbackToSingleUserLocked();
Dianne Hackbornbfd89b32011-12-15 18:22:54 -0800574 } finally {
575 if (fis != null) {
576 try {
577 fis.close();
578 } catch (IOException e) {
579 }
580 }
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700581 }
582 }
583
Amith Yamasani6f34b412012-10-22 18:19:27 -0700584 /**
Amith Yamasanibc9625052012-11-15 14:39:18 -0800585 * Upgrade steps between versions, either for fixing bugs or changing the data format.
Amith Yamasani6f34b412012-10-22 18:19:27 -0700586 */
Amith Yamasani350962c2013-08-06 11:18:53 -0700587 private void upgradeIfNecessaryLocked() {
Amith Yamasani6f34b412012-10-22 18:19:27 -0700588 int userVersion = mUserVersion;
589 if (userVersion < 1) {
590 // Assign a proper name for the owner, if not initialized correctly before
591 UserInfo user = mUsers.get(UserHandle.USER_OWNER);
592 if ("Primary".equals(user.name)) {
593 user.name = mContext.getResources().getString(com.android.internal.R.string.owner_name);
594 writeUserLocked(user);
595 }
596 userVersion = 1;
597 }
598
Amith Yamasanibc9625052012-11-15 14:39:18 -0800599 if (userVersion < 2) {
600 // Owner should be marked as initialized
601 UserInfo user = mUsers.get(UserHandle.USER_OWNER);
602 if ((user.flags & UserInfo.FLAG_INITIALIZED) == 0) {
603 user.flags |= UserInfo.FLAG_INITIALIZED;
604 writeUserLocked(user);
605 }
606 userVersion = 2;
607 }
608
Amith Yamasani350962c2013-08-06 11:18:53 -0700609
Amith Yamasani5e486f52013-08-07 11:06:44 -0700610 if (userVersion < 4) {
Amith Yamasani5e486f52013-08-07 11:06:44 -0700611 userVersion = 4;
612 }
613
Amith Yamasani6f34b412012-10-22 18:19:27 -0700614 if (userVersion < USER_VERSION) {
615 Slog.w(LOG_TAG, "User version " + mUserVersion + " didn't upgrade as expected to "
616 + USER_VERSION);
617 } else {
618 mUserVersion = userVersion;
619 writeUserListLocked();
620 }
621 }
622
Amith Yamasani13593602012-03-22 16:16:17 -0700623 private void fallbackToSingleUserLocked() {
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700624 // Create the primary user
Amith Yamasani67df64b2012-12-14 12:09:36 -0800625 UserInfo primary = new UserInfo(UserHandle.USER_OWNER,
Amith Yamasani6f34b412012-10-22 18:19:27 -0700626 mContext.getResources().getString(com.android.internal.R.string.owner_name), null,
Amith Yamasani756901d2012-10-12 12:30:07 -0700627 UserInfo.FLAG_ADMIN | UserInfo.FLAG_PRIMARY | UserInfo.FLAG_INITIALIZED);
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700628 mUsers.put(0, primary);
Amith Yamasani634cf312012-10-04 17:34:21 -0700629 mNextSerialNumber = MIN_USER_ID;
Geoffrey Borggaard15b8b2c2013-08-28 22:11:10 -0400630 mUserVersion = USER_VERSION;
Amith Yamasani67df64b2012-12-14 12:09:36 -0800631
Geoffrey Borggaarde45e45e32013-01-24 10:03:20 -0500632 Bundle restrictions = new Bundle();
Amith Yamasani67df64b2012-12-14 12:09:36 -0800633 mUserRestrictions.append(UserHandle.USER_OWNER, restrictions);
634
Amith Yamasani13593602012-03-22 16:16:17 -0700635 updateUserIdsLocked();
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700636
Amith Yamasani13593602012-03-22 16:16:17 -0700637 writeUserListLocked();
638 writeUserLocked(primary);
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700639 }
640
641 /*
642 * Writes the user file in this format:
643 *
644 * <user flags="20039023" id="0">
645 * <name>Primary</name>
646 * </user>
647 */
Amith Yamasani13593602012-03-22 16:16:17 -0700648 private void writeUserLocked(UserInfo userInfo) {
Amith Yamasani742a6712011-05-04 14:49:28 -0700649 FileOutputStream fos = null;
Amith Yamasanifc95e702013-09-26 13:20:17 -0700650 AtomicFile userFile = new AtomicFile(new File(mUsersDir, userInfo.id + XML_SUFFIX));
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700651 try {
Amith Yamasani2a003292012-08-14 18:25:45 -0700652 fos = userFile.startWrite();
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700653 final BufferedOutputStream bos = new BufferedOutputStream(fos);
654
655 // XmlSerializer serializer = XmlUtils.serializerInstance();
656 final XmlSerializer serializer = new FastXmlSerializer();
657 serializer.setOutput(bos, "utf-8");
658 serializer.startDocument(null, true);
659 serializer.setFeature("http://xmlpull.org/v1/doc/features.html#indent-output", true);
660
661 serializer.startTag(null, TAG_USER);
662 serializer.attribute(null, ATTR_ID, Integer.toString(userInfo.id));
Amith Yamasani2a003292012-08-14 18:25:45 -0700663 serializer.attribute(null, ATTR_SERIAL_NO, Integer.toString(userInfo.serialNumber));
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700664 serializer.attribute(null, ATTR_FLAGS, Integer.toString(userInfo.flags));
Amith Yamasani920ace02012-09-20 22:15:37 -0700665 serializer.attribute(null, ATTR_CREATION_TIME, Long.toString(userInfo.creationTime));
666 serializer.attribute(null, ATTR_LAST_LOGGED_IN_TIME,
667 Long.toString(userInfo.lastLoggedInTime));
Amith Yamasani655d0e22013-06-12 14:19:10 -0700668 RestrictionsPinState pinState = mRestrictionsPinStates.get(userInfo.id);
669 if (pinState != null) {
670 if (pinState.salt != 0) {
671 serializer.attribute(null, ATTR_SALT, Long.toString(pinState.salt));
672 }
673 if (pinState.pinHash != null) {
674 serializer.attribute(null, ATTR_PIN_HASH, pinState.pinHash);
675 }
676 if (pinState.failedAttempts != 0) {
677 serializer.attribute(null, ATTR_FAILED_ATTEMPTS,
678 Integer.toString(pinState.failedAttempts));
679 serializer.attribute(null, ATTR_LAST_RETRY_MS,
680 Long.toString(pinState.lastAttemptTime));
681 }
682 }
Amith Yamasanib8151ec2012-04-18 18:02:48 -0700683 if (userInfo.iconPath != null) {
684 serializer.attribute(null, ATTR_ICON_PATH, userInfo.iconPath);
685 }
Dianne Hackbornd4ac8d72012-09-27 23:20:10 -0700686 if (userInfo.partial) {
687 serializer.attribute(null, ATTR_PARTIAL, "true");
688 }
Kenny Guy2a764942014-04-02 13:29:20 +0100689 if (userInfo.profileGroupId != UserInfo.NO_PROFILE_GROUP_ID) {
690 serializer.attribute(null, ATTR_PROFILE_GROUP_ID,
691 Integer.toString(userInfo.profileGroupId));
Kenny Guya52dc3e2014-02-11 15:33:14 +0000692 }
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700693
694 serializer.startTag(null, TAG_NAME);
695 serializer.text(userInfo.name);
696 serializer.endTag(null, TAG_NAME);
697
Amith Yamasanie4cf7342012-12-17 11:12:09 -0800698 Bundle restrictions = mUserRestrictions.get(userInfo.id);
699 if (restrictions != null) {
700 serializer.startTag(null, TAG_RESTRICTIONS);
Amith Yamasani71e6c692013-03-24 17:39:28 -0700701 writeBoolean(serializer, restrictions, UserManager.DISALLOW_CONFIG_WIFI);
702 writeBoolean(serializer, restrictions, UserManager.DISALLOW_MODIFY_ACCOUNTS);
703 writeBoolean(serializer, restrictions, UserManager.DISALLOW_INSTALL_APPS);
704 writeBoolean(serializer, restrictions, UserManager.DISALLOW_UNINSTALL_APPS);
705 writeBoolean(serializer, restrictions, UserManager.DISALLOW_SHARE_LOCATION);
Maggie Benthalla12fccf2013-03-14 18:02:12 -0400706 writeBoolean(serializer, restrictions,
707 UserManager.DISALLOW_INSTALL_UNKNOWN_SOURCES);
708 writeBoolean(serializer, restrictions, UserManager.DISALLOW_CONFIG_BLUETOOTH);
709 writeBoolean(serializer, restrictions, UserManager.DISALLOW_USB_FILE_TRANSFER);
Emily Bernierb223f732013-04-11 15:46:36 -0400710 writeBoolean(serializer, restrictions, UserManager.DISALLOW_CONFIG_CREDENTIALS);
711 writeBoolean(serializer, restrictions, UserManager.DISALLOW_REMOVE_USER);
Amith Yamasanie4cf7342012-12-17 11:12:09 -0800712 serializer.endTag(null, TAG_RESTRICTIONS);
713 }
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700714 serializer.endTag(null, TAG_USER);
715
716 serializer.endDocument();
Amith Yamasani2a003292012-08-14 18:25:45 -0700717 userFile.finishWrite(fos);
718 } catch (Exception ioe) {
Amith Yamasani0b285492011-04-14 17:35:23 -0700719 Slog.e(LOG_TAG, "Error writing user info " + userInfo.id + "\n" + ioe);
Amith Yamasani2a003292012-08-14 18:25:45 -0700720 userFile.failWrite(fos);
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700721 }
722 }
723
724 /*
725 * Writes the user list file in this format:
726 *
Amith Yamasani2a003292012-08-14 18:25:45 -0700727 * <users nextSerialNumber="3">
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700728 * <user id="0"></user>
729 * <user id="2"></user>
730 * </users>
731 */
Amith Yamasani13593602012-03-22 16:16:17 -0700732 private void writeUserListLocked() {
Amith Yamasani742a6712011-05-04 14:49:28 -0700733 FileOutputStream fos = null;
Amith Yamasani2a003292012-08-14 18:25:45 -0700734 AtomicFile userListFile = new AtomicFile(mUserListFile);
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700735 try {
Amith Yamasani2a003292012-08-14 18:25:45 -0700736 fos = userListFile.startWrite();
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700737 final BufferedOutputStream bos = new BufferedOutputStream(fos);
738
739 // XmlSerializer serializer = XmlUtils.serializerInstance();
740 final XmlSerializer serializer = new FastXmlSerializer();
741 serializer.setOutput(bos, "utf-8");
742 serializer.startDocument(null, true);
743 serializer.setFeature("http://xmlpull.org/v1/doc/features.html#indent-output", true);
744
745 serializer.startTag(null, TAG_USERS);
Amith Yamasani2a003292012-08-14 18:25:45 -0700746 serializer.attribute(null, ATTR_NEXT_SERIAL_NO, Integer.toString(mNextSerialNumber));
Amith Yamasani6f34b412012-10-22 18:19:27 -0700747 serializer.attribute(null, ATTR_USER_VERSION, Integer.toString(mUserVersion));
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700748
749 for (int i = 0; i < mUsers.size(); i++) {
750 UserInfo user = mUsers.valueAt(i);
751 serializer.startTag(null, TAG_USER);
752 serializer.attribute(null, ATTR_ID, Integer.toString(user.id));
753 serializer.endTag(null, TAG_USER);
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700754 }
755
756 serializer.endTag(null, TAG_USERS);
757
758 serializer.endDocument();
Amith Yamasani2a003292012-08-14 18:25:45 -0700759 userListFile.finishWrite(fos);
760 } catch (Exception e) {
761 userListFile.failWrite(fos);
Amith Yamasani0b285492011-04-14 17:35:23 -0700762 Slog.e(LOG_TAG, "Error writing user list");
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700763 }
764 }
765
Amith Yamasanie4cf7342012-12-17 11:12:09 -0800766 private UserInfo readUserLocked(int id) {
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700767 int flags = 0;
Amith Yamasani2a003292012-08-14 18:25:45 -0700768 int serialNumber = id;
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700769 String name = null;
Amith Yamasanib8151ec2012-04-18 18:02:48 -0700770 String iconPath = null;
Amith Yamasani920ace02012-09-20 22:15:37 -0700771 long creationTime = 0L;
772 long lastLoggedInTime = 0L;
Amith Yamasani655d0e22013-06-12 14:19:10 -0700773 long salt = 0L;
774 String pinHash = null;
775 int failedAttempts = 0;
Kenny Guy2a764942014-04-02 13:29:20 +0100776 int profileGroupId = UserInfo.NO_PROFILE_GROUP_ID;
Amith Yamasani655d0e22013-06-12 14:19:10 -0700777 long lastAttemptTime = 0L;
Dianne Hackbornd4ac8d72012-09-27 23:20:10 -0700778 boolean partial = false;
Amith Yamasanie4cf7342012-12-17 11:12:09 -0800779 Bundle restrictions = new Bundle();
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700780
781 FileInputStream fis = null;
782 try {
Amith Yamasani2a003292012-08-14 18:25:45 -0700783 AtomicFile userFile =
Amith Yamasanifc95e702013-09-26 13:20:17 -0700784 new AtomicFile(new File(mUsersDir, Integer.toString(id) + XML_SUFFIX));
Amith Yamasani2a003292012-08-14 18:25:45 -0700785 fis = userFile.openRead();
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700786 XmlPullParser parser = Xml.newPullParser();
787 parser.setInput(fis, null);
788 int type;
789 while ((type = parser.next()) != XmlPullParser.START_TAG
790 && type != XmlPullParser.END_DOCUMENT) {
791 ;
792 }
793
794 if (type != XmlPullParser.START_TAG) {
Amith Yamasani0b285492011-04-14 17:35:23 -0700795 Slog.e(LOG_TAG, "Unable to read user " + id);
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700796 return null;
797 }
798
799 if (type == XmlPullParser.START_TAG && parser.getName().equals(TAG_USER)) {
Amith Yamasani920ace02012-09-20 22:15:37 -0700800 int storedId = readIntAttribute(parser, ATTR_ID, -1);
801 if (storedId != id) {
Amith Yamasani0b285492011-04-14 17:35:23 -0700802 Slog.e(LOG_TAG, "User id does not match the file name");
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700803 return null;
804 }
Amith Yamasani920ace02012-09-20 22:15:37 -0700805 serialNumber = readIntAttribute(parser, ATTR_SERIAL_NO, id);
806 flags = readIntAttribute(parser, ATTR_FLAGS, 0);
Amith Yamasanib8151ec2012-04-18 18:02:48 -0700807 iconPath = parser.getAttributeValue(null, ATTR_ICON_PATH);
Amith Yamasani920ace02012-09-20 22:15:37 -0700808 creationTime = readLongAttribute(parser, ATTR_CREATION_TIME, 0);
809 lastLoggedInTime = readLongAttribute(parser, ATTR_LAST_LOGGED_IN_TIME, 0);
Amith Yamasani655d0e22013-06-12 14:19:10 -0700810 salt = readLongAttribute(parser, ATTR_SALT, 0L);
811 pinHash = parser.getAttributeValue(null, ATTR_PIN_HASH);
812 failedAttempts = readIntAttribute(parser, ATTR_FAILED_ATTEMPTS, 0);
813 lastAttemptTime = readLongAttribute(parser, ATTR_LAST_RETRY_MS, 0L);
Kenny Guy2a764942014-04-02 13:29:20 +0100814 profileGroupId = readIntAttribute(parser, ATTR_PROFILE_GROUP_ID,
815 UserInfo.NO_PROFILE_GROUP_ID);
816 if (profileGroupId == UserInfo.NO_PROFILE_GROUP_ID) {
817 // This attribute was added and renamed during development of L.
818 // TODO Remove upgrade path by 1st May 2014
819 profileGroupId = readIntAttribute(parser, "relatedGroupId",
820 UserInfo.NO_PROFILE_GROUP_ID);
821 }
Dianne Hackbornd4ac8d72012-09-27 23:20:10 -0700822 String valueString = parser.getAttributeValue(null, ATTR_PARTIAL);
823 if ("true".equals(valueString)) {
824 partial = true;
825 }
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700826
Amith Yamasanie4cf7342012-12-17 11:12:09 -0800827 int outerDepth = parser.getDepth();
828 while ((type = parser.next()) != XmlPullParser.END_DOCUMENT
829 && (type != XmlPullParser.END_TAG || parser.getDepth() > outerDepth)) {
830 if (type == XmlPullParser.END_TAG || type == XmlPullParser.TEXT) {
831 continue;
832 }
833 String tag = parser.getName();
834 if (TAG_NAME.equals(tag)) {
835 type = parser.next();
836 if (type == XmlPullParser.TEXT) {
837 name = parser.getText();
838 }
839 } else if (TAG_RESTRICTIONS.equals(tag)) {
Amith Yamasani71e6c692013-03-24 17:39:28 -0700840 readBoolean(parser, restrictions, UserManager.DISALLOW_CONFIG_WIFI);
841 readBoolean(parser, restrictions, UserManager.DISALLOW_MODIFY_ACCOUNTS);
842 readBoolean(parser, restrictions, UserManager.DISALLOW_INSTALL_APPS);
843 readBoolean(parser, restrictions, UserManager.DISALLOW_UNINSTALL_APPS);
844 readBoolean(parser, restrictions, UserManager.DISALLOW_SHARE_LOCATION);
Maggie Benthalla12fccf2013-03-14 18:02:12 -0400845 readBoolean(parser, restrictions,
846 UserManager.DISALLOW_INSTALL_UNKNOWN_SOURCES);
847 readBoolean(parser, restrictions, UserManager.DISALLOW_CONFIG_BLUETOOTH);
848 readBoolean(parser, restrictions, UserManager.DISALLOW_USB_FILE_TRANSFER);
Emily Bernierb223f732013-04-11 15:46:36 -0400849 readBoolean(parser, restrictions, UserManager.DISALLOW_CONFIG_CREDENTIALS);
850 readBoolean(parser, restrictions, UserManager.DISALLOW_REMOVE_USER);
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700851 }
852 }
853 }
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700854
Amith Yamasanib8151ec2012-04-18 18:02:48 -0700855 UserInfo userInfo = new UserInfo(id, name, iconPath, flags);
Amith Yamasani2a003292012-08-14 18:25:45 -0700856 userInfo.serialNumber = serialNumber;
Amith Yamasani920ace02012-09-20 22:15:37 -0700857 userInfo.creationTime = creationTime;
858 userInfo.lastLoggedInTime = lastLoggedInTime;
Dianne Hackbornd4ac8d72012-09-27 23:20:10 -0700859 userInfo.partial = partial;
Kenny Guy2a764942014-04-02 13:29:20 +0100860 userInfo.profileGroupId = profileGroupId;
Amith Yamasanie4cf7342012-12-17 11:12:09 -0800861 mUserRestrictions.append(id, restrictions);
Amith Yamasani655d0e22013-06-12 14:19:10 -0700862 if (salt != 0L) {
863 RestrictionsPinState pinState = mRestrictionsPinStates.get(id);
864 if (pinState == null) {
865 pinState = new RestrictionsPinState();
866 mRestrictionsPinStates.put(id, pinState);
867 }
868 pinState.salt = salt;
869 pinState.pinHash = pinHash;
870 pinState.failedAttempts = failedAttempts;
871 pinState.lastAttemptTime = lastAttemptTime;
872 }
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700873 return userInfo;
874
875 } catch (IOException ioe) {
876 } catch (XmlPullParserException pe) {
Dianne Hackbornbfd89b32011-12-15 18:22:54 -0800877 } finally {
878 if (fis != null) {
879 try {
880 fis.close();
881 } catch (IOException e) {
882 }
883 }
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700884 }
885 return null;
886 }
887
Amith Yamasanie4cf7342012-12-17 11:12:09 -0800888 private void readBoolean(XmlPullParser parser, Bundle restrictions,
889 String restrictionKey) {
890 String value = parser.getAttributeValue(null, restrictionKey);
Amith Yamasani71e6c692013-03-24 17:39:28 -0700891 if (value != null) {
892 restrictions.putBoolean(restrictionKey, Boolean.parseBoolean(value));
893 }
Amith Yamasanie4cf7342012-12-17 11:12:09 -0800894 }
895
896 private void writeBoolean(XmlSerializer xml, Bundle restrictions, String restrictionKey)
897 throws IOException {
898 if (restrictions.containsKey(restrictionKey)) {
899 xml.attribute(null, restrictionKey,
900 Boolean.toString(restrictions.getBoolean(restrictionKey)));
901 }
902 }
903
Amith Yamasani920ace02012-09-20 22:15:37 -0700904 private int readIntAttribute(XmlPullParser parser, String attr, int defaultValue) {
905 String valueString = parser.getAttributeValue(null, attr);
906 if (valueString == null) return defaultValue;
907 try {
908 return Integer.parseInt(valueString);
909 } catch (NumberFormatException nfe) {
910 return defaultValue;
911 }
912 }
913
914 private long readLongAttribute(XmlPullParser parser, String attr, long defaultValue) {
915 String valueString = parser.getAttributeValue(null, attr);
916 if (valueString == null) return defaultValue;
917 try {
918 return Long.parseLong(valueString);
919 } catch (NumberFormatException nfe) {
920 return defaultValue;
921 }
922 }
923
Amith Yamasani1a7472e2013-07-02 11:17:30 -0700924 private boolean isPackageInstalled(String pkg, int userId) {
925 final ApplicationInfo info = mPm.getApplicationInfo(pkg,
926 PackageManager.GET_UNINSTALLED_PACKAGES,
927 userId);
928 if (info == null || (info.flags&ApplicationInfo.FLAG_INSTALLED) == 0) {
929 return false;
930 }
931 return true;
932 }
933
Amith Yamasanib82add22013-07-09 11:24:44 -0700934 /**
Amith Yamasani1a7472e2013-07-02 11:17:30 -0700935 * Removes all the restrictions files (res_<packagename>) for a given user, if all is true,
936 * else removes only those packages that have been uninstalled.
Amith Yamasanib82add22013-07-09 11:24:44 -0700937 * Does not do any permissions checking.
938 */
Amith Yamasani1a7472e2013-07-02 11:17:30 -0700939 private void cleanAppRestrictions(int userId, boolean all) {
Amith Yamasanib82add22013-07-09 11:24:44 -0700940 synchronized (mPackagesLock) {
941 File dir = Environment.getUserSystemDirectory(userId);
942 String[] files = dir.list();
943 if (files == null) return;
944 for (String fileName : files) {
945 if (fileName.startsWith(RESTRICTIONS_FILE_PREFIX)) {
946 File resFile = new File(dir, fileName);
947 if (resFile.exists()) {
Amith Yamasani1a7472e2013-07-02 11:17:30 -0700948 if (all) {
949 resFile.delete();
950 } else {
Amith Yamasanifc95e702013-09-26 13:20:17 -0700951 String pkg = restrictionsFileNameToPackage(fileName);
Amith Yamasani1a7472e2013-07-02 11:17:30 -0700952 if (!isPackageInstalled(pkg, userId)) {
953 resFile.delete();
954 }
955 }
Amith Yamasanib82add22013-07-09 11:24:44 -0700956 }
957 }
958 }
959 }
960 }
961
Amith Yamasani1a7472e2013-07-02 11:17:30 -0700962 /**
963 * Removes the app restrictions file for a specific package and user id, if it exists.
964 */
965 private void cleanAppRestrictionsForPackage(String pkg, int userId) {
966 synchronized (mPackagesLock) {
967 File dir = Environment.getUserSystemDirectory(userId);
Amith Yamasanifc95e702013-09-26 13:20:17 -0700968 File resFile = new File(dir, packageToRestrictionsFileName(pkg));
Amith Yamasani1a7472e2013-07-02 11:17:30 -0700969 if (resFile.exists()) {
970 resFile.delete();
971 }
972 }
973 }
974
Kenny Guy2a764942014-04-02 13:29:20 +0100975 private int getNextProfileGroupIdLocked() {
976 int maxGroupId = UserInfo.NO_PROFILE_GROUP_ID;
Kenny Guya52dc3e2014-02-11 15:33:14 +0000977 for (int i = 0; i < mUsers.size(); i++) {
978 UserInfo ui = mUsers.valueAt(i);
Kenny Guy2a764942014-04-02 13:29:20 +0100979 if (maxGroupId < ui.profileGroupId) {
980 maxGroupId = ui.profileGroupId;
Kenny Guya52dc3e2014-02-11 15:33:14 +0000981 }
982 }
983 return maxGroupId + 1;
984 }
985
986 @Override
Kenny Guy2a764942014-04-02 13:29:20 +0100987 public UserInfo createProfileForUser(String name, int flags, int userId) {
Kenny Guya52dc3e2014-02-11 15:33:14 +0000988 checkManageUsersPermission("Only the system can create users");
Kenny Guy2a764942014-04-02 13:29:20 +0100989 if (userId != UserHandle.USER_OWNER) {
990 Slog.w(LOG_TAG, "Only user owner can have profiles");
Kenny Guya52dc3e2014-02-11 15:33:14 +0000991 return null;
992 }
Kenny Guy2a764942014-04-02 13:29:20 +0100993 return createUserInternal(name, flags, userId);
Kenny Guya52dc3e2014-02-11 15:33:14 +0000994 }
995
Amith Yamasani258848d2012-08-10 17:06:33 -0700996 @Override
Amith Yamasani13593602012-03-22 16:16:17 -0700997 public UserInfo createUser(String name, int flags) {
Amith Yamasani2a003292012-08-14 18:25:45 -0700998 checkManageUsersPermission("Only the system can create users");
Nicolas Prevotc6d033e2014-02-27 13:11:09 +0000999 return createUserInternal(name, flags, UserHandle.USER_NULL);
Kenny Guya52dc3e2014-02-11 15:33:14 +00001000 }
Amith Yamasanifaea76f2012-09-11 10:59:48 -07001001
Kenny Guy2a764942014-04-02 13:29:20 +01001002 private UserInfo createUserInternal(String name, int flags, int profileId) {
Dianne Hackborn5dc5a002012-09-15 19:33:48 -07001003 final long ident = Binder.clearCallingIdentity();
Nicolas Prevotc6d033e2014-02-27 13:11:09 +00001004 UserInfo userInfo = null;
Dianne Hackborn5dc5a002012-09-15 19:33:48 -07001005 try {
1006 synchronized (mInstallLock) {
1007 synchronized (mPackagesLock) {
Kenny Guy2a764942014-04-02 13:29:20 +01001008 UserInfo profile = null;
1009 if (profileId != UserHandle.USER_NULL) {
1010 profile = getUserInfoLocked(profileId);
1011 if (profile == null) return null;
Nicolas Prevotc6d033e2014-02-27 13:11:09 +00001012 }
Dianne Hackborn5dc5a002012-09-15 19:33:48 -07001013 if (isUserLimitReachedLocked()) return null;
1014 int userId = getNextAvailableIdLocked();
1015 userInfo = new UserInfo(userId, name, null, flags);
1016 File userPath = new File(mBaseUserPath, Integer.toString(userId));
1017 userInfo.serialNumber = mNextSerialNumber++;
Amith Yamasani920ace02012-09-20 22:15:37 -07001018 long now = System.currentTimeMillis();
1019 userInfo.creationTime = (now > EPOCH_PLUS_30_YEARS) ? now : 0;
Dianne Hackbornd4ac8d72012-09-27 23:20:10 -07001020 userInfo.partial = true;
Amith Yamasani16389312012-10-17 21:20:14 -07001021 Environment.getUserSystemDirectory(userInfo.id).mkdirs();
Dianne Hackborn5dc5a002012-09-15 19:33:48 -07001022 mUsers.put(userId, userInfo);
1023 writeUserListLocked();
Kenny Guy2a764942014-04-02 13:29:20 +01001024 if (profile != null) {
1025 if (profile.profileGroupId == UserInfo.NO_PROFILE_GROUP_ID) {
1026 profile.profileGroupId = getNextProfileGroupIdLocked();
1027 writeUserLocked(profile);
Kenny Guya52dc3e2014-02-11 15:33:14 +00001028 }
Kenny Guy2a764942014-04-02 13:29:20 +01001029 userInfo.profileGroupId = profile.profileGroupId;
Kenny Guya52dc3e2014-02-11 15:33:14 +00001030 }
Dianne Hackborn5dc5a002012-09-15 19:33:48 -07001031 writeUserLocked(userInfo);
Dianne Hackborn5dc5a002012-09-15 19:33:48 -07001032 mPm.createNewUserLILPw(userId, userPath);
Dianne Hackbornd4ac8d72012-09-27 23:20:10 -07001033 userInfo.partial = false;
1034 writeUserLocked(userInfo);
1035 updateUserIdsLocked();
Geoffrey Borggaarde45e45e32013-01-24 10:03:20 -05001036 Bundle restrictions = new Bundle();
Geoffrey Borggaarde45e45e32013-01-24 10:03:20 -05001037 mUserRestrictions.append(userId, restrictions);
Dianne Hackborn5dc5a002012-09-15 19:33:48 -07001038 }
Dianne Hackborn4428e172012-08-24 17:43:05 -07001039 }
Dianne Hackborn5dc5a002012-09-15 19:33:48 -07001040 if (userInfo != null) {
1041 Intent addedIntent = new Intent(Intent.ACTION_USER_ADDED);
1042 addedIntent.putExtra(Intent.EXTRA_USER_HANDLE, userInfo.id);
1043 mContext.sendBroadcastAsUser(addedIntent, UserHandle.ALL,
1044 android.Manifest.permission.MANAGE_USERS);
1045 }
1046 } finally {
1047 Binder.restoreCallingIdentity(ident);
Amith Yamasani258848d2012-08-10 17:06:33 -07001048 }
Amith Yamasani4b2e9342011-03-31 12:38:53 -07001049 return userInfo;
1050 }
1051
Amith Yamasani0b285492011-04-14 17:35:23 -07001052 /**
1053 * Removes a user and all data directories created for that user. This method should be called
1054 * after the user's processes have been terminated.
Dianne Hackborn10ad9822014-03-17 11:28:36 -07001055 * @param userHandle the user's id
Amith Yamasani0b285492011-04-14 17:35:23 -07001056 */
Amith Yamasani258848d2012-08-10 17:06:33 -07001057 public boolean removeUser(int userHandle) {
Amith Yamasani2a003292012-08-14 18:25:45 -07001058 checkManageUsersPermission("Only the system can remove users");
Dianne Hackborn80a4af22012-08-27 19:18:31 -07001059 final UserInfo user;
1060 synchronized (mPackagesLock) {
1061 user = mUsers.get(userHandle);
1062 if (userHandle == 0 || user == null) {
1063 return false;
1064 }
Jeff Sharkeyffe0cb42012-11-05 17:24:43 -08001065 mRemovingUserIds.put(userHandle, true);
Amith Yamasani756901d2012-10-12 12:30:07 -07001066 // Set this to a partially created user, so that the user will be purged
1067 // on next startup, in case the runtime stops now before stopping and
1068 // removing the user completely.
1069 user.partial = true;
1070 writeUserLocked(user);
Dianne Hackborn80a4af22012-08-27 19:18:31 -07001071 }
Amith Yamasani16389312012-10-17 21:20:14 -07001072 if (DBG) Slog.i(LOG_TAG, "Stopping user " + userHandle);
Dianne Hackborn80a4af22012-08-27 19:18:31 -07001073 int res;
1074 try {
1075 res = ActivityManagerNative.getDefault().stopUser(userHandle,
1076 new IStopUserCallback.Stub() {
1077 @Override
1078 public void userStopped(int userId) {
1079 finishRemoveUser(userId);
1080 }
1081 @Override
1082 public void userStopAborted(int userId) {
1083 }
1084 });
1085 } catch (RemoteException e) {
1086 return false;
1087 }
1088
1089 return res == ActivityManager.USER_OP_SUCCESS;
1090 }
1091
Amith Yamasanidb6a14c2012-10-17 21:16:52 -07001092 void finishRemoveUser(final int userHandle) {
Amith Yamasani16389312012-10-17 21:20:14 -07001093 if (DBG) Slog.i(LOG_TAG, "finishRemoveUser " + userHandle);
Amith Yamasanidb6a14c2012-10-17 21:16:52 -07001094 // Let other services shutdown any activity and clean up their state before completely
1095 // wiping the user's system directory and removing from the user list
Dianne Hackborn5dc5a002012-09-15 19:33:48 -07001096 long ident = Binder.clearCallingIdentity();
1097 try {
1098 Intent addedIntent = new Intent(Intent.ACTION_USER_REMOVED);
1099 addedIntent.putExtra(Intent.EXTRA_USER_HANDLE, userHandle);
Amith Yamasanidb6a14c2012-10-17 21:16:52 -07001100 mContext.sendOrderedBroadcastAsUser(addedIntent, UserHandle.ALL,
1101 android.Manifest.permission.MANAGE_USERS,
1102
1103 new BroadcastReceiver() {
1104 @Override
1105 public void onReceive(Context context, Intent intent) {
1106 if (DBG) {
1107 Slog.i(LOG_TAG,
1108 "USER_REMOVED broadcast sent, cleaning up user data "
1109 + userHandle);
1110 }
1111 new Thread() {
1112 public void run() {
1113 synchronized (mInstallLock) {
1114 synchronized (mPackagesLock) {
1115 removeUserStateLocked(userHandle);
1116 }
1117 }
1118 }
1119 }.start();
1120 }
1121 },
1122
1123 null, Activity.RESULT_OK, null, null);
Dianne Hackborn5dc5a002012-09-15 19:33:48 -07001124 } finally {
1125 Binder.restoreCallingIdentity(ident);
1126 }
Amith Yamasani2a003292012-08-14 18:25:45 -07001127 }
1128
Jeff Sharkeyffe0cb42012-11-05 17:24:43 -08001129 private void removeUserStateLocked(final int userHandle) {
Dianne Hackbornd4ac8d72012-09-27 23:20:10 -07001130 // Cleanup package manager settings
1131 mPm.cleanUpUserLILPw(userHandle);
1132
1133 // Remove this user from the list
1134 mUsers.remove(userHandle);
Jeff Sharkeyffe0cb42012-11-05 17:24:43 -08001135
1136 // Have user ID linger for several seconds to let external storage VFS
1137 // cache entries expire. This must be greater than the 'entry_valid'
1138 // timeout used by the FUSE daemon.
1139 mHandler.postDelayed(new Runnable() {
1140 @Override
1141 public void run() {
1142 synchronized (mPackagesLock) {
1143 mRemovingUserIds.delete(userHandle);
1144 }
1145 }
1146 }, MINUTE_IN_MILLIS);
1147
Amith Yamasani655d0e22013-06-12 14:19:10 -07001148 mRestrictionsPinStates.remove(userHandle);
Dianne Hackbornd4ac8d72012-09-27 23:20:10 -07001149 // Remove user file
Amith Yamasanifc95e702013-09-26 13:20:17 -07001150 AtomicFile userFile = new AtomicFile(new File(mUsersDir, userHandle + XML_SUFFIX));
Dianne Hackbornd4ac8d72012-09-27 23:20:10 -07001151 userFile.delete();
1152 // Update the user list
1153 writeUserListLocked();
1154 updateUserIdsLocked();
1155 removeDirectoryRecursive(Environment.getUserSystemDirectory(userHandle));
1156 }
1157
Amith Yamasani61f57372012-08-31 12:12:28 -07001158 private void removeDirectoryRecursive(File parent) {
1159 if (parent.isDirectory()) {
1160 String[] files = parent.list();
1161 for (String filename : files) {
1162 File child = new File(parent, filename);
1163 removeDirectoryRecursive(child);
1164 }
1165 }
1166 parent.delete();
1167 }
1168
Amith Yamasani2a003292012-08-14 18:25:45 -07001169 @Override
Amith Yamasani7e99bc02013-04-16 18:24:51 -07001170 public Bundle getApplicationRestrictions(String packageName) {
1171 return getApplicationRestrictionsForUser(packageName, UserHandle.getCallingUserId());
1172 }
1173
1174 @Override
1175 public Bundle getApplicationRestrictionsForUser(String packageName, int userId) {
Amith Yamasanidf2e92a2013-03-01 17:04:38 -08001176 if (UserHandle.getCallingUserId() != userId
Amith Yamasani9429afb2013-04-10 18:40:51 -07001177 || !UserHandle.isSameApp(Binder.getCallingUid(), getUidForPackage(packageName))) {
Amith Yamasanidf2e92a2013-03-01 17:04:38 -08001178 checkManageUsersPermission("Only system can get restrictions for other users/apps");
1179 }
1180 synchronized (mPackagesLock) {
1181 // Read the restrictions from XML
1182 return readApplicationRestrictionsLocked(packageName, userId);
1183 }
1184 }
1185
1186 @Override
Amith Yamasani7e99bc02013-04-16 18:24:51 -07001187 public void setApplicationRestrictions(String packageName, Bundle restrictions,
Amith Yamasanidf2e92a2013-03-01 17:04:38 -08001188 int userId) {
1189 if (UserHandle.getCallingUserId() != userId
Amith Yamasani9429afb2013-04-10 18:40:51 -07001190 || !UserHandle.isSameApp(Binder.getCallingUid(), getUidForPackage(packageName))) {
Amith Yamasanidf2e92a2013-03-01 17:04:38 -08001191 checkManageUsersPermission("Only system can set restrictions for other users/apps");
1192 }
1193 synchronized (mPackagesLock) {
1194 // Write the restrictions to XML
Amith Yamasani7e99bc02013-04-16 18:24:51 -07001195 writeApplicationRestrictionsLocked(packageName, restrictions, userId);
Amith Yamasanidf2e92a2013-03-01 17:04:38 -08001196 }
1197 }
1198
Amith Yamasani655d0e22013-06-12 14:19:10 -07001199 @Override
Amith Yamasanid304af62013-09-05 09:30:23 -07001200 public boolean setRestrictionsChallenge(String newPin) {
Amith Yamasani655d0e22013-06-12 14:19:10 -07001201 checkManageUsersPermission("Only system can modify the restrictions pin");
1202 int userId = UserHandle.getCallingUserId();
1203 synchronized (mPackagesLock) {
1204 RestrictionsPinState pinState = mRestrictionsPinStates.get(userId);
1205 if (pinState == null) {
1206 pinState = new RestrictionsPinState();
1207 }
1208 if (newPin == null) {
1209 pinState.salt = 0;
1210 pinState.pinHash = null;
1211 } else {
1212 try {
1213 pinState.salt = SecureRandom.getInstance("SHA1PRNG").nextLong();
1214 } catch (NoSuchAlgorithmException e) {
1215 pinState.salt = (long) (Math.random() * Long.MAX_VALUE);
1216 }
1217 pinState.pinHash = passwordToHash(newPin, pinState.salt);
1218 pinState.failedAttempts = 0;
1219 }
1220 mRestrictionsPinStates.put(userId, pinState);
1221 writeUserLocked(mUsers.get(userId));
1222 }
1223 return true;
1224 }
1225
1226 @Override
Amith Yamasanid304af62013-09-05 09:30:23 -07001227 public int checkRestrictionsChallenge(String pin) {
Amith Yamasani655d0e22013-06-12 14:19:10 -07001228 checkManageUsersPermission("Only system can verify the restrictions pin");
1229 int userId = UserHandle.getCallingUserId();
1230 synchronized (mPackagesLock) {
1231 RestrictionsPinState pinState = mRestrictionsPinStates.get(userId);
1232 // If there's no pin set, return error code
1233 if (pinState == null || pinState.salt == 0 || pinState.pinHash == null) {
1234 return UserManager.PIN_VERIFICATION_FAILED_NOT_SET;
1235 } else if (pin == null) {
1236 // If just checking if user can be prompted, return remaining time
1237 int waitTime = getRemainingTimeForPinAttempt(pinState);
1238 Slog.d(LOG_TAG, "Remaining waittime peek=" + waitTime);
1239 return waitTime;
1240 } else {
1241 int waitTime = getRemainingTimeForPinAttempt(pinState);
1242 Slog.d(LOG_TAG, "Remaining waittime=" + waitTime);
1243 if (waitTime > 0) {
1244 return waitTime;
1245 }
1246 if (passwordToHash(pin, pinState.salt).equals(pinState.pinHash)) {
1247 pinState.failedAttempts = 0;
1248 writeUserLocked(mUsers.get(userId));
1249 return UserManager.PIN_VERIFICATION_SUCCESS;
1250 } else {
1251 pinState.failedAttempts++;
1252 pinState.lastAttemptTime = System.currentTimeMillis();
1253 writeUserLocked(mUsers.get(userId));
1254 return waitTime;
1255 }
1256 }
1257 }
1258 }
1259
1260 private int getRemainingTimeForPinAttempt(RestrictionsPinState pinState) {
1261 int backoffIndex = Math.min(pinState.failedAttempts / BACKOFF_INC_INTERVAL,
1262 BACKOFF_TIMES.length - 1);
1263 int backoffTime = (pinState.failedAttempts % BACKOFF_INC_INTERVAL) == 0 ?
1264 BACKOFF_TIMES[backoffIndex] : 0;
1265 return (int) Math.max(backoffTime + pinState.lastAttemptTime - System.currentTimeMillis(),
1266 0);
1267 }
1268
1269 @Override
Amith Yamasanid304af62013-09-05 09:30:23 -07001270 public boolean hasRestrictionsChallenge() {
Amith Yamasani655d0e22013-06-12 14:19:10 -07001271 int userId = UserHandle.getCallingUserId();
1272 synchronized (mPackagesLock) {
Amith Yamasani0343ec32013-07-22 14:52:06 -07001273 return hasRestrictionsPinLocked(userId);
1274 }
1275 }
1276
1277 private boolean hasRestrictionsPinLocked(int userId) {
1278 RestrictionsPinState pinState = mRestrictionsPinStates.get(userId);
1279 if (pinState == null || pinState.salt == 0 || pinState.pinHash == null) {
1280 return false;
Amith Yamasani655d0e22013-06-12 14:19:10 -07001281 }
1282 return true;
1283 }
1284
Amith Yamasani1a7472e2013-07-02 11:17:30 -07001285 @Override
1286 public void removeRestrictions() {
1287 checkManageUsersPermission("Only system can remove restrictions");
1288 final int userHandle = UserHandle.getCallingUserId();
Amith Yamasani5e486f52013-08-07 11:06:44 -07001289 removeRestrictionsForUser(userHandle, true);
Amith Yamasani350962c2013-08-06 11:18:53 -07001290 }
1291
Amith Yamasani5e486f52013-08-07 11:06:44 -07001292 private void removeRestrictionsForUser(final int userHandle, boolean unblockApps) {
Amith Yamasani1a7472e2013-07-02 11:17:30 -07001293 synchronized (mPackagesLock) {
1294 // Remove all user restrictions
1295 setUserRestrictions(new Bundle(), userHandle);
1296 // Remove restrictions pin
Amith Yamasanid304af62013-09-05 09:30:23 -07001297 setRestrictionsChallenge(null);
Amith Yamasani1a7472e2013-07-02 11:17:30 -07001298 // Remove any app restrictions
1299 cleanAppRestrictions(userHandle, true);
1300 }
Amith Yamasani5e486f52013-08-07 11:06:44 -07001301 if (unblockApps) {
1302 unblockAllAppsForUser(userHandle);
1303 }
1304 }
1305
1306 private void unblockAllAppsForUser(final int userHandle) {
Amith Yamasani1a7472e2013-07-02 11:17:30 -07001307 mHandler.post(new Runnable() {
1308 @Override
1309 public void run() {
1310 List<ApplicationInfo> apps =
1311 mPm.getInstalledApplications(PackageManager.GET_UNINSTALLED_PACKAGES,
1312 userHandle).getList();
1313 final long ident = Binder.clearCallingIdentity();
1314 try {
1315 for (ApplicationInfo appInfo : apps) {
1316 if ((appInfo.flags & ApplicationInfo.FLAG_INSTALLED) != 0
1317 && (appInfo.flags & ApplicationInfo.FLAG_BLOCKED) != 0) {
1318 mPm.setApplicationBlockedSettingAsUser(appInfo.packageName, false,
1319 userHandle);
1320 }
1321 }
1322 } finally {
1323 Binder.restoreCallingIdentity(ident);
1324 }
1325 }
1326 });
1327 }
1328
Amith Yamasani655d0e22013-06-12 14:19:10 -07001329 /*
1330 * Generate a hash for the given password. To avoid brute force attacks, we use a salted hash.
1331 * Not the most secure, but it is at least a second level of protection. First level is that
1332 * the file is in a location only readable by the system process.
1333 * @param password the password.
1334 * @param salt the randomly generated salt
1335 * @return the hash of the pattern in a String.
1336 */
1337 private String passwordToHash(String password, long salt) {
1338 if (password == null) {
1339 return null;
1340 }
1341 String algo = null;
1342 String hashed = salt + password;
1343 try {
1344 byte[] saltedPassword = (password + salt).getBytes();
1345 byte[] sha1 = MessageDigest.getInstance(algo = "SHA-1").digest(saltedPassword);
1346 byte[] md5 = MessageDigest.getInstance(algo = "MD5").digest(saltedPassword);
1347 hashed = toHex(sha1) + toHex(md5);
1348 } catch (NoSuchAlgorithmException e) {
1349 Log.w(LOG_TAG, "Failed to encode string because of missing algorithm: " + algo);
1350 }
1351 return hashed;
1352 }
1353
1354 private static String toHex(byte[] ary) {
1355 final String hex = "0123456789ABCDEF";
1356 String ret = "";
1357 for (int i = 0; i < ary.length; i++) {
1358 ret += hex.charAt((ary[i] >> 4) & 0xf);
1359 ret += hex.charAt(ary[i] & 0xf);
1360 }
1361 return ret;
1362 }
1363
Amith Yamasanidf2e92a2013-03-01 17:04:38 -08001364 private int getUidForPackage(String packageName) {
Amith Yamasani9429afb2013-04-10 18:40:51 -07001365 long ident = Binder.clearCallingIdentity();
Amith Yamasanidf2e92a2013-03-01 17:04:38 -08001366 try {
1367 return mContext.getPackageManager().getApplicationInfo(packageName,
1368 PackageManager.GET_UNINSTALLED_PACKAGES).uid;
1369 } catch (NameNotFoundException nnfe) {
1370 return -1;
Amith Yamasani9429afb2013-04-10 18:40:51 -07001371 } finally {
1372 Binder.restoreCallingIdentity(ident);
Amith Yamasanidf2e92a2013-03-01 17:04:38 -08001373 }
1374 }
1375
Amith Yamasani7e99bc02013-04-16 18:24:51 -07001376 private Bundle readApplicationRestrictionsLocked(String packageName,
Amith Yamasanidf2e92a2013-03-01 17:04:38 -08001377 int userId) {
Amith Yamasani7e99bc02013-04-16 18:24:51 -07001378 final Bundle restrictions = new Bundle();
Amith Yamasanidf2e92a2013-03-01 17:04:38 -08001379 final ArrayList<String> values = new ArrayList<String>();
1380
1381 FileInputStream fis = null;
1382 try {
1383 AtomicFile restrictionsFile =
1384 new AtomicFile(new File(Environment.getUserSystemDirectory(userId),
Amith Yamasanifc95e702013-09-26 13:20:17 -07001385 packageToRestrictionsFileName(packageName)));
Amith Yamasanidf2e92a2013-03-01 17:04:38 -08001386 fis = restrictionsFile.openRead();
1387 XmlPullParser parser = Xml.newPullParser();
1388 parser.setInput(fis, null);
1389 int type;
1390 while ((type = parser.next()) != XmlPullParser.START_TAG
1391 && type != XmlPullParser.END_DOCUMENT) {
1392 ;
1393 }
1394
1395 if (type != XmlPullParser.START_TAG) {
1396 Slog.e(LOG_TAG, "Unable to read restrictions file "
1397 + restrictionsFile.getBaseFile());
Amith Yamasani7e99bc02013-04-16 18:24:51 -07001398 return restrictions;
Amith Yamasanidf2e92a2013-03-01 17:04:38 -08001399 }
1400
1401 while ((type = parser.next()) != XmlPullParser.END_DOCUMENT) {
1402 if (type == XmlPullParser.START_TAG && parser.getName().equals(TAG_ENTRY)) {
1403 String key = parser.getAttributeValue(null, ATTR_KEY);
Amith Yamasani7e99bc02013-04-16 18:24:51 -07001404 String valType = parser.getAttributeValue(null, ATTR_VALUE_TYPE);
Amith Yamasanidf2e92a2013-03-01 17:04:38 -08001405 String multiple = parser.getAttributeValue(null, ATTR_MULTIPLE);
1406 if (multiple != null) {
1407 int count = Integer.parseInt(multiple);
1408 while (count > 0 && (type = parser.next()) != XmlPullParser.END_DOCUMENT) {
1409 if (type == XmlPullParser.START_TAG
1410 && parser.getName().equals(TAG_VALUE)) {
1411 values.add(parser.nextText().trim());
1412 count--;
1413 }
1414 }
1415 String [] valueStrings = new String[values.size()];
1416 values.toArray(valueStrings);
Amith Yamasani7e99bc02013-04-16 18:24:51 -07001417 restrictions.putStringArray(key, valueStrings);
1418 } else if (ATTR_TYPE_BOOLEAN.equals(valType)) {
1419 restrictions.putBoolean(key, Boolean.parseBoolean(
1420 parser.nextText().trim()));
Amith Yamasanidf2e92a2013-03-01 17:04:38 -08001421 } else {
1422 String value = parser.nextText().trim();
Amith Yamasani7e99bc02013-04-16 18:24:51 -07001423 restrictions.putString(key, value);
Amith Yamasanidf2e92a2013-03-01 17:04:38 -08001424 }
1425 }
1426 }
1427
1428 } catch (IOException ioe) {
1429 } catch (XmlPullParserException pe) {
1430 } finally {
1431 if (fis != null) {
1432 try {
1433 fis.close();
1434 } catch (IOException e) {
1435 }
1436 }
1437 }
Amith Yamasani7e99bc02013-04-16 18:24:51 -07001438 return restrictions;
Amith Yamasanidf2e92a2013-03-01 17:04:38 -08001439 }
1440
1441 private void writeApplicationRestrictionsLocked(String packageName,
Amith Yamasani7e99bc02013-04-16 18:24:51 -07001442 Bundle restrictions, int userId) {
Amith Yamasanidf2e92a2013-03-01 17:04:38 -08001443 FileOutputStream fos = null;
1444 AtomicFile restrictionsFile = new AtomicFile(
1445 new File(Environment.getUserSystemDirectory(userId),
Amith Yamasanifc95e702013-09-26 13:20:17 -07001446 packageToRestrictionsFileName(packageName)));
Amith Yamasanidf2e92a2013-03-01 17:04:38 -08001447 try {
1448 fos = restrictionsFile.startWrite();
1449 final BufferedOutputStream bos = new BufferedOutputStream(fos);
1450
1451 // XmlSerializer serializer = XmlUtils.serializerInstance();
1452 final XmlSerializer serializer = new FastXmlSerializer();
1453 serializer.setOutput(bos, "utf-8");
1454 serializer.startDocument(null, true);
1455 serializer.setFeature("http://xmlpull.org/v1/doc/features.html#indent-output", true);
1456
1457 serializer.startTag(null, TAG_RESTRICTIONS);
1458
Amith Yamasani7e99bc02013-04-16 18:24:51 -07001459 for (String key : restrictions.keySet()) {
1460 Object value = restrictions.get(key);
Amith Yamasanidf2e92a2013-03-01 17:04:38 -08001461 serializer.startTag(null, TAG_ENTRY);
Amith Yamasani7e99bc02013-04-16 18:24:51 -07001462 serializer.attribute(null, ATTR_KEY, key);
1463
1464 if (value instanceof Boolean) {
1465 serializer.attribute(null, ATTR_VALUE_TYPE, ATTR_TYPE_BOOLEAN);
1466 serializer.text(value.toString());
1467 } else if (value == null || value instanceof String) {
1468 serializer.attribute(null, ATTR_VALUE_TYPE, ATTR_TYPE_STRING);
1469 serializer.text(value != null ? (String) value : "");
Amith Yamasanidf2e92a2013-03-01 17:04:38 -08001470 } else {
Amith Yamasani7e99bc02013-04-16 18:24:51 -07001471 serializer.attribute(null, ATTR_VALUE_TYPE, ATTR_TYPE_STRING_ARRAY);
1472 String[] values = (String[]) value;
Amith Yamasanidf2e92a2013-03-01 17:04:38 -08001473 serializer.attribute(null, ATTR_MULTIPLE, Integer.toString(values.length));
Amith Yamasani7e99bc02013-04-16 18:24:51 -07001474 for (String choice : values) {
Amith Yamasanidf2e92a2013-03-01 17:04:38 -08001475 serializer.startTag(null, TAG_VALUE);
Amith Yamasani7e99bc02013-04-16 18:24:51 -07001476 serializer.text(choice != null ? choice : "");
Amith Yamasanidf2e92a2013-03-01 17:04:38 -08001477 serializer.endTag(null, TAG_VALUE);
1478 }
1479 }
1480 serializer.endTag(null, TAG_ENTRY);
1481 }
1482
1483 serializer.endTag(null, TAG_RESTRICTIONS);
1484
1485 serializer.endDocument();
1486 restrictionsFile.finishWrite(fos);
1487 } catch (Exception e) {
1488 restrictionsFile.failWrite(fos);
1489 Slog.e(LOG_TAG, "Error writing application restrictions list");
1490 }
1491 }
1492
1493 @Override
Amith Yamasani2a003292012-08-14 18:25:45 -07001494 public int getUserSerialNumber(int userHandle) {
Dianne Hackborn4428e172012-08-24 17:43:05 -07001495 synchronized (mPackagesLock) {
Amith Yamasani2a003292012-08-14 18:25:45 -07001496 if (!exists(userHandle)) return -1;
Amith Yamasani195263742012-08-21 15:40:12 -07001497 return getUserInfoLocked(userHandle).serialNumber;
Amith Yamasani2a003292012-08-14 18:25:45 -07001498 }
1499 }
1500
1501 @Override
1502 public int getUserHandle(int userSerialNumber) {
Dianne Hackborn4428e172012-08-24 17:43:05 -07001503 synchronized (mPackagesLock) {
Amith Yamasani2a003292012-08-14 18:25:45 -07001504 for (int userId : mUserIds) {
Amith Yamasani195263742012-08-21 15:40:12 -07001505 if (getUserInfoLocked(userId).serialNumber == userSerialNumber) return userId;
Amith Yamasani2a003292012-08-14 18:25:45 -07001506 }
1507 // Not found
1508 return -1;
Amith Yamasani13593602012-03-22 16:16:17 -07001509 }
1510 }
1511
Amith Yamasani0b285492011-04-14 17:35:23 -07001512 /**
1513 * Caches the list of user ids in an array, adjusting the array size when necessary.
1514 */
Amith Yamasani13593602012-03-22 16:16:17 -07001515 private void updateUserIdsLocked() {
Dianne Hackbornd4ac8d72012-09-27 23:20:10 -07001516 int num = 0;
Amith Yamasani0b285492011-04-14 17:35:23 -07001517 for (int i = 0; i < mUsers.size(); i++) {
Dianne Hackbornd4ac8d72012-09-27 23:20:10 -07001518 if (!mUsers.valueAt(i).partial) {
1519 num++;
1520 }
1521 }
Amith Yamasani16389312012-10-17 21:20:14 -07001522 final int[] newUsers = new int[num];
1523 int n = 0;
Dianne Hackbornd4ac8d72012-09-27 23:20:10 -07001524 for (int i = 0; i < mUsers.size(); i++) {
1525 if (!mUsers.valueAt(i).partial) {
Amith Yamasani16389312012-10-17 21:20:14 -07001526 newUsers[n++] = mUsers.keyAt(i);
Dianne Hackbornd4ac8d72012-09-27 23:20:10 -07001527 }
Amith Yamasani0b285492011-04-14 17:35:23 -07001528 }
Dianne Hackborn7767eac2012-08-23 18:25:40 -07001529 mUserIds = newUsers;
Amith Yamasani0b285492011-04-14 17:35:23 -07001530 }
1531
1532 /**
Amith Yamasani1a7472e2013-07-02 11:17:30 -07001533 * Make a note of the last started time of a user and do some cleanup.
Amith Yamasani920ace02012-09-20 22:15:37 -07001534 * @param userId the user that was just foregrounded
1535 */
1536 public void userForeground(int userId) {
1537 synchronized (mPackagesLock) {
1538 UserInfo user = mUsers.get(userId);
1539 long now = System.currentTimeMillis();
Dianne Hackbornd4ac8d72012-09-27 23:20:10 -07001540 if (user == null || user.partial) {
1541 Slog.w(LOG_TAG, "userForeground: unknown user #" + userId);
1542 return;
1543 }
1544 if (now > EPOCH_PLUS_30_YEARS) {
Amith Yamasani920ace02012-09-20 22:15:37 -07001545 user.lastLoggedInTime = now;
1546 writeUserLocked(user);
1547 }
Amith Yamasani1a7472e2013-07-02 11:17:30 -07001548 // If this is not a restricted profile and there is no restrictions pin, clean up
1549 // all restrictions files that might have been left behind, else clean up just the
1550 // ones with uninstalled packages
1551 RestrictionsPinState pinState = mRestrictionsPinStates.get(userId);
1552 final long salt = pinState == null ? 0 : pinState.salt;
1553 cleanAppRestrictions(userId, (!user.isRestricted() && salt == 0));
Amith Yamasani920ace02012-09-20 22:15:37 -07001554 }
1555 }
1556
1557 /**
Amith Yamasani0b285492011-04-14 17:35:23 -07001558 * Returns the next available user id, filling in any holes in the ids.
Amith Yamasani742a6712011-05-04 14:49:28 -07001559 * TODO: May not be a good idea to recycle ids, in case it results in confusion
1560 * for data and battery stats collection, or unexpected cross-talk.
Amith Yamasani0b285492011-04-14 17:35:23 -07001561 * @return
1562 */
Dianne Hackborn5dc5a002012-09-15 19:33:48 -07001563 private int getNextAvailableIdLocked() {
Dianne Hackborn4428e172012-08-24 17:43:05 -07001564 synchronized (mPackagesLock) {
Jeff Sharkeyffe0cb42012-11-05 17:24:43 -08001565 int i = MIN_USER_ID;
Amith Yamasani195263742012-08-21 15:40:12 -07001566 while (i < Integer.MAX_VALUE) {
Jeff Sharkeyffe0cb42012-11-05 17:24:43 -08001567 if (mUsers.indexOfKey(i) < 0 && !mRemovingUserIds.get(i)) {
Amith Yamasani195263742012-08-21 15:40:12 -07001568 break;
1569 }
1570 i++;
Amith Yamasani4b2e9342011-03-31 12:38:53 -07001571 }
Amith Yamasani195263742012-08-21 15:40:12 -07001572 return i;
Amith Yamasani4b2e9342011-03-31 12:38:53 -07001573 }
Amith Yamasani4b2e9342011-03-31 12:38:53 -07001574 }
Amith Yamasani920ace02012-09-20 22:15:37 -07001575
Amith Yamasanifc95e702013-09-26 13:20:17 -07001576 private String packageToRestrictionsFileName(String packageName) {
1577 return RESTRICTIONS_FILE_PREFIX + packageName + XML_SUFFIX;
1578 }
1579
1580 private String restrictionsFileNameToPackage(String fileName) {
1581 return fileName.substring(RESTRICTIONS_FILE_PREFIX.length(),
1582 (int) (fileName.length() - XML_SUFFIX.length()));
1583 }
1584
Amith Yamasani920ace02012-09-20 22:15:37 -07001585 @Override
1586 protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
1587 if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.DUMP)
1588 != PackageManager.PERMISSION_GRANTED) {
1589 pw.println("Permission Denial: can't dump UserManager from from pid="
1590 + Binder.getCallingPid()
1591 + ", uid=" + Binder.getCallingUid()
1592 + " without permission "
1593 + android.Manifest.permission.DUMP);
1594 return;
1595 }
1596
1597 long now = System.currentTimeMillis();
1598 StringBuilder sb = new StringBuilder();
1599 synchronized (mPackagesLock) {
1600 pw.println("Users:");
1601 for (int i = 0; i < mUsers.size(); i++) {
1602 UserInfo user = mUsers.valueAt(i);
1603 if (user == null) continue;
Amith Yamasani634cf312012-10-04 17:34:21 -07001604 pw.print(" "); pw.print(user); pw.print(" serialNo="); pw.print(user.serialNumber);
Jeff Sharkeyffe0cb42012-11-05 17:24:43 -08001605 if (mRemovingUserIds.get(mUsers.keyAt(i))) pw.print(" <removing> ");
Dianne Hackbornd4ac8d72012-09-27 23:20:10 -07001606 if (user.partial) pw.print(" <partial>");
1607 pw.println();
Amith Yamasani920ace02012-09-20 22:15:37 -07001608 pw.print(" Created: ");
1609 if (user.creationTime == 0) {
1610 pw.println("<unknown>");
1611 } else {
1612 sb.setLength(0);
1613 TimeUtils.formatDuration(now - user.creationTime, sb);
1614 sb.append(" ago");
1615 pw.println(sb);
1616 }
1617 pw.print(" Last logged in: ");
1618 if (user.lastLoggedInTime == 0) {
1619 pw.println("<unknown>");
1620 } else {
1621 sb.setLength(0);
1622 TimeUtils.formatDuration(now - user.lastLoggedInTime, sb);
1623 sb.append(" ago");
1624 pw.println(sb);
1625 }
1626 }
1627 }
1628 }
Amith Yamasani1a7472e2013-07-02 11:17:30 -07001629
1630 private PackageMonitor mUserPackageMonitor = new PackageMonitor() {
1631 @Override
1632 public void onPackageRemoved(String pkg, int uid) {
1633 final int userId = this.getChangingUserId();
1634 // Package could be disappearing because it is being blocked, so also check if
1635 // it has been uninstalled.
1636 final boolean uninstalled = isPackageDisappearing(pkg) == PACKAGE_PERMANENT_CHANGE;
1637 if (uninstalled && userId >= 0 && !isPackageInstalled(pkg, userId)) {
1638 cleanAppRestrictionsForPackage(pkg, userId);
1639 }
1640 }
1641 };
Amith Yamasani4b2e9342011-03-31 12:38:53 -07001642}