blob: a39e958a5b6fd74f31d5dbb3930340e517015132 [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) {
Kenny Guya52dc3e2014-02-11 15:33:14 +0000261 checkManageUsersPermission("query users");
262 synchronized (mPackagesLock) {
263 UserInfo user = getUserInfoLocked(userId);
264 ArrayList<UserInfo> users = new ArrayList<UserInfo>(mUsers.size());
265 for (int i = 0; i < mUsers.size(); i++) {
Kenny Guy2a764942014-04-02 13:29:20 +0100266 UserInfo profile = mUsers.valueAt(i);
267 if (!isProfileOf(user, profile)) {
Kenny Guya52dc3e2014-02-11 15:33:14 +0000268 continue;
269 }
Kenny Guy2a764942014-04-02 13:29:20 +0100270 users.add(profile);
Kenny Guya52dc3e2014-02-11 15:33:14 +0000271 }
272 return users;
273 }
274 }
275
Kenny Guy2a764942014-04-02 13:29:20 +0100276 private boolean isProfileOf(UserInfo user, UserInfo profile) {
277 return user.id == profile.id ||
278 (user.profileGroupId != UserInfo.NO_PROFILE_GROUP_ID
279 && user.profileGroupId == profile.profileGroupId);
Kenny Guya52dc3e2014-02-11 15:33:14 +0000280 }
281
282 @Override
Amith Yamasani258848d2012-08-10 17:06:33 -0700283 public UserInfo getUserInfo(int userId) {
Amith Yamasani2a003292012-08-14 18:25:45 -0700284 checkManageUsersPermission("query user");
Dianne Hackborn4428e172012-08-24 17:43:05 -0700285 synchronized (mPackagesLock) {
Amith Yamasani195263742012-08-21 15:40:12 -0700286 return getUserInfoLocked(userId);
Amith Yamasani13593602012-03-22 16:16:17 -0700287 }
288 }
289
Amith Yamasani71e6c692013-03-24 17:39:28 -0700290 @Override
291 public boolean isRestricted() {
292 synchronized (mPackagesLock) {
293 return getUserInfoLocked(UserHandle.getCallingUserId()).isRestricted();
294 }
295 }
296
Amith Yamasani195263742012-08-21 15:40:12 -0700297 /*
298 * Should be locked on mUsers before calling this.
299 */
300 private UserInfo getUserInfoLocked(int userId) {
Dianne Hackbornd4ac8d72012-09-27 23:20:10 -0700301 UserInfo ui = mUsers.get(userId);
Amith Yamasani16389312012-10-17 21:20:14 -0700302 // If it is partial and not in the process of being removed, return as unknown user.
Jeff Sharkeyffe0cb42012-11-05 17:24:43 -0800303 if (ui != null && ui.partial && !mRemovingUserIds.get(userId)) {
Dianne Hackbornd4ac8d72012-09-27 23:20:10 -0700304 Slog.w(LOG_TAG, "getUserInfo: unknown user #" + userId);
305 return null;
306 }
307 return ui;
Amith Yamasani195263742012-08-21 15:40:12 -0700308 }
309
Amith Yamasani13593602012-03-22 16:16:17 -0700310 public boolean exists(int userId) {
Dianne Hackborn4428e172012-08-24 17:43:05 -0700311 synchronized (mPackagesLock) {
Amith Yamasani13593602012-03-22 16:16:17 -0700312 return ArrayUtils.contains(mUserIds, userId);
313 }
314 }
315
Amith Yamasani258848d2012-08-10 17:06:33 -0700316 @Override
Amith Yamasanib8151ec2012-04-18 18:02:48 -0700317 public void setUserName(int userId, String name) {
Amith Yamasani2a003292012-08-14 18:25:45 -0700318 checkManageUsersPermission("rename users");
Dianne Hackbornd4ac8d72012-09-27 23:20:10 -0700319 boolean changed = false;
Dianne Hackborn4428e172012-08-24 17:43:05 -0700320 synchronized (mPackagesLock) {
Amith Yamasani13593602012-03-22 16:16:17 -0700321 UserInfo info = mUsers.get(userId);
Dianne Hackbornd4ac8d72012-09-27 23:20:10 -0700322 if (info == null || info.partial) {
323 Slog.w(LOG_TAG, "setUserName: unknown user #" + userId);
324 return;
325 }
Amith Yamasani13593602012-03-22 16:16:17 -0700326 if (name != null && !name.equals(info.name)) {
327 info.name = name;
328 writeUserLocked(info);
Dianne Hackbornd4ac8d72012-09-27 23:20:10 -0700329 changed = true;
Amith Yamasani13593602012-03-22 16:16:17 -0700330 }
331 }
Dianne Hackbornd4ac8d72012-09-27 23:20:10 -0700332 if (changed) {
333 sendUserInfoChangedBroadcast(userId);
334 }
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700335 }
336
Amith Yamasani258848d2012-08-10 17:06:33 -0700337 @Override
Amith Yamasanie928d7d2012-09-17 21:46:51 -0700338 public void setUserIcon(int userId, Bitmap bitmap) {
Amith Yamasani2a003292012-08-14 18:25:45 -0700339 checkManageUsersPermission("update users");
Dianne Hackborn4428e172012-08-24 17:43:05 -0700340 synchronized (mPackagesLock) {
Amith Yamasanib8151ec2012-04-18 18:02:48 -0700341 UserInfo info = mUsers.get(userId);
Dianne Hackbornd4ac8d72012-09-27 23:20:10 -0700342 if (info == null || info.partial) {
343 Slog.w(LOG_TAG, "setUserIcon: unknown user #" + userId);
344 return;
345 }
Amith Yamasanie928d7d2012-09-17 21:46:51 -0700346 writeBitmapLocked(info, bitmap);
347 writeUserLocked(info);
Amith Yamasanib8151ec2012-04-18 18:02:48 -0700348 }
Amith Yamasanie928d7d2012-09-17 21:46:51 -0700349 sendUserInfoChangedBroadcast(userId);
350 }
351
352 private void sendUserInfoChangedBroadcast(int userId) {
353 Intent changedIntent = new Intent(Intent.ACTION_USER_INFO_CHANGED);
354 changedIntent.putExtra(Intent.EXTRA_USER_HANDLE, userId);
355 changedIntent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY);
Amith Yamasani6fc1d4e2013-05-08 16:43:58 -0700356 mContext.sendBroadcastAsUser(changedIntent, UserHandle.ALL);
Amith Yamasanib8151ec2012-04-18 18:02:48 -0700357 }
358
Amith Yamasani258848d2012-08-10 17:06:33 -0700359 @Override
Amith Yamasanie928d7d2012-09-17 21:46:51 -0700360 public Bitmap getUserIcon(int userId) {
Amith Yamasani3b49f072012-09-17 10:21:43 -0700361 checkManageUsersPermission("read users");
362 synchronized (mPackagesLock) {
363 UserInfo info = mUsers.get(userId);
Dianne Hackbornd4ac8d72012-09-27 23:20:10 -0700364 if (info == null || info.partial) {
365 Slog.w(LOG_TAG, "getUserIcon: unknown user #" + userId);
366 return null;
367 }
368 if (info.iconPath == null) {
369 return null;
370 }
Amith Yamasanie928d7d2012-09-17 21:46:51 -0700371 return BitmapFactory.decodeFile(info.iconPath);
Amith Yamasani3b49f072012-09-17 10:21:43 -0700372 }
373 }
374
375 @Override
Amith Yamasani258848d2012-08-10 17:06:33 -0700376 public void setGuestEnabled(boolean enable) {
Amith Yamasani2a003292012-08-14 18:25:45 -0700377 checkManageUsersPermission("enable guest users");
Dianne Hackborn4428e172012-08-24 17:43:05 -0700378 synchronized (mPackagesLock) {
Amith Yamasani258848d2012-08-10 17:06:33 -0700379 if (mGuestEnabled != enable) {
380 mGuestEnabled = enable;
381 // Erase any guest user that currently exists
382 for (int i = 0; i < mUsers.size(); i++) {
383 UserInfo user = mUsers.valueAt(i);
Dianne Hackbornd4ac8d72012-09-27 23:20:10 -0700384 if (!user.partial && user.isGuest()) {
Amith Yamasani258848d2012-08-10 17:06:33 -0700385 if (!enable) {
386 removeUser(user.id);
387 }
388 return;
389 }
390 }
391 // No guest was found
392 if (enable) {
393 createUser("Guest", UserInfo.FLAG_GUEST);
394 }
395 }
396 }
397 }
398
399 @Override
400 public boolean isGuestEnabled() {
Dianne Hackborn4428e172012-08-24 17:43:05 -0700401 synchronized (mPackagesLock) {
Amith Yamasani258848d2012-08-10 17:06:33 -0700402 return mGuestEnabled;
403 }
404 }
405
406 @Override
407 public void wipeUser(int userHandle) {
Amith Yamasani2a003292012-08-14 18:25:45 -0700408 checkManageUsersPermission("wipe user");
Amith Yamasani258848d2012-08-10 17:06:33 -0700409 // TODO:
410 }
411
Dianne Hackborn5dc5a002012-09-15 19:33:48 -0700412 public void makeInitialized(int userId) {
413 checkManageUsersPermission("makeInitialized");
414 synchronized (mPackagesLock) {
415 UserInfo info = mUsers.get(userId);
Dianne Hackbornd4ac8d72012-09-27 23:20:10 -0700416 if (info == null || info.partial) {
417 Slog.w(LOG_TAG, "makeInitialized: unknown user #" + userId);
418 }
419 if ((info.flags&UserInfo.FLAG_INITIALIZED) == 0) {
Dianne Hackborn5dc5a002012-09-15 19:33:48 -0700420 info.flags |= UserInfo.FLAG_INITIALIZED;
421 writeUserLocked(info);
422 }
423 }
424 }
425
Amith Yamasanie4cf7342012-12-17 11:12:09 -0800426 @Override
427 public Bundle getUserRestrictions(int userId) {
428 // checkManageUsersPermission("getUserRestrictions");
429
430 synchronized (mPackagesLock) {
431 Bundle restrictions = mUserRestrictions.get(userId);
432 return restrictions != null ? restrictions : Bundle.EMPTY;
433 }
434 }
435
436 @Override
437 public void setUserRestrictions(Bundle restrictions, int userId) {
438 checkManageUsersPermission("setUserRestrictions");
Amith Yamasani0343ec32013-07-22 14:52:06 -0700439 if (restrictions == null) return;
Amith Yamasanie4cf7342012-12-17 11:12:09 -0800440
441 synchronized (mPackagesLock) {
Amith Yamasani350962c2013-08-06 11:18:53 -0700442 mUserRestrictions.get(userId).clear();
Amith Yamasanie4cf7342012-12-17 11:12:09 -0800443 mUserRestrictions.get(userId).putAll(restrictions);
444 writeUserLocked(mUsers.get(userId));
445 }
446 }
447
Amith Yamasani258848d2012-08-10 17:06:33 -0700448 /**
Amith Yamasanifaea76f2012-09-11 10:59:48 -0700449 * Check if we've hit the limit of how many users can be created.
450 */
Dianne Hackborn5dc5a002012-09-15 19:33:48 -0700451 private boolean isUserLimitReachedLocked() {
452 int nUsers = mUsers.size();
Jeff Sharkey27bd34d2012-09-16 12:49:00 -0700453 return nUsers >= UserManager.getMaxSupportedUsers();
Amith Yamasanifaea76f2012-09-11 10:59:48 -0700454 }
455
456 /**
Amith Yamasani195263742012-08-21 15:40:12 -0700457 * Enforces that only the system UID or root's UID or apps that have the
Dianne Hackborn10ad9822014-03-17 11:28:36 -0700458 * {@link android.Manifest.permission#MANAGE_USERS MANAGE_USERS}
Amith Yamasani195263742012-08-21 15:40:12 -0700459 * permission can make certain calls to the UserManager.
Amith Yamasani258848d2012-08-10 17:06:33 -0700460 *
461 * @param message used as message if SecurityException is thrown
462 * @throws SecurityException if the caller is not system or root
463 */
Amith Yamasani2a003292012-08-14 18:25:45 -0700464 private static final void checkManageUsersPermission(String message) {
Amith Yamasani258848d2012-08-10 17:06:33 -0700465 final int uid = Binder.getCallingUid();
Amith Yamasani2a003292012-08-14 18:25:45 -0700466 if (uid != Process.SYSTEM_UID && uid != 0
467 && ActivityManager.checkComponentPermission(
468 android.Manifest.permission.MANAGE_USERS,
469 uid, -1, true) != PackageManager.PERMISSION_GRANTED) {
470 throw new SecurityException("You need MANAGE_USERS permission to: " + message);
Amith Yamasani258848d2012-08-10 17:06:33 -0700471 }
472 }
473
Amith Yamasanie928d7d2012-09-17 21:46:51 -0700474 private void writeBitmapLocked(UserInfo info, Bitmap bitmap) {
Amith Yamasanib8151ec2012-04-18 18:02:48 -0700475 try {
476 File dir = new File(mUsersDir, Integer.toString(info.id));
477 File file = new File(dir, USER_PHOTO_FILENAME);
478 if (!dir.exists()) {
479 dir.mkdir();
480 FileUtils.setPermissions(
481 dir.getPath(),
482 FileUtils.S_IRWXU|FileUtils.S_IRWXG|FileUtils.S_IXOTH,
483 -1, -1);
484 }
Amith Yamasanie928d7d2012-09-17 21:46:51 -0700485 FileOutputStream os;
486 if (bitmap.compress(Bitmap.CompressFormat.PNG, 100, os = new FileOutputStream(file))) {
Amith Yamasani3b49f072012-09-17 10:21:43 -0700487 info.iconPath = file.getAbsolutePath();
488 }
Amith Yamasanie928d7d2012-09-17 21:46:51 -0700489 try {
490 os.close();
491 } catch (IOException ioe) {
492 // What the ... !
493 }
Amith Yamasanib8151ec2012-04-18 18:02:48 -0700494 } catch (FileNotFoundException e) {
Amith Yamasani2a003292012-08-14 18:25:45 -0700495 Slog.w(LOG_TAG, "Error setting photo for user ", e);
Amith Yamasanib8151ec2012-04-18 18:02:48 -0700496 }
Amith Yamasanib8151ec2012-04-18 18:02:48 -0700497 }
498
Amith Yamasani0b285492011-04-14 17:35:23 -0700499 /**
500 * Returns an array of user ids. This array is cached here for quick access, so do not modify or
501 * cache it elsewhere.
502 * @return the array of user ids.
503 */
Dianne Hackborn1676c852012-09-10 14:52:30 -0700504 public int[] getUserIds() {
Dianne Hackborn4428e172012-08-24 17:43:05 -0700505 synchronized (mPackagesLock) {
Dianne Hackborn7767eac2012-08-23 18:25:40 -0700506 return mUserIds;
507 }
Amith Yamasani0b285492011-04-14 17:35:23 -0700508 }
509
Dianne Hackborn4428e172012-08-24 17:43:05 -0700510 int[] getUserIdsLPr() {
511 return mUserIds;
512 }
513
Amith Yamasani13593602012-03-22 16:16:17 -0700514 private void readUserListLocked() {
Amith Yamasani258848d2012-08-10 17:06:33 -0700515 mGuestEnabled = false;
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700516 if (!mUserListFile.exists()) {
Amith Yamasani13593602012-03-22 16:16:17 -0700517 fallbackToSingleUserLocked();
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700518 return;
519 }
520 FileInputStream fis = null;
Amith Yamasani2a003292012-08-14 18:25:45 -0700521 AtomicFile userListFile = new AtomicFile(mUserListFile);
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700522 try {
Amith Yamasani2a003292012-08-14 18:25:45 -0700523 fis = userListFile.openRead();
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700524 XmlPullParser parser = Xml.newPullParser();
525 parser.setInput(fis, null);
526 int type;
527 while ((type = parser.next()) != XmlPullParser.START_TAG
528 && type != XmlPullParser.END_DOCUMENT) {
529 ;
530 }
531
532 if (type != XmlPullParser.START_TAG) {
Amith Yamasani0b285492011-04-14 17:35:23 -0700533 Slog.e(LOG_TAG, "Unable to read user list");
Amith Yamasani13593602012-03-22 16:16:17 -0700534 fallbackToSingleUserLocked();
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700535 return;
536 }
537
Amith Yamasani2a003292012-08-14 18:25:45 -0700538 mNextSerialNumber = -1;
539 if (parser.getName().equals(TAG_USERS)) {
540 String lastSerialNumber = parser.getAttributeValue(null, ATTR_NEXT_SERIAL_NO);
541 if (lastSerialNumber != null) {
542 mNextSerialNumber = Integer.parseInt(lastSerialNumber);
543 }
Amith Yamasani6f34b412012-10-22 18:19:27 -0700544 String versionNumber = parser.getAttributeValue(null, ATTR_USER_VERSION);
545 if (versionNumber != null) {
546 mUserVersion = Integer.parseInt(versionNumber);
547 }
Amith Yamasani2a003292012-08-14 18:25:45 -0700548 }
549
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700550 while ((type = parser.next()) != XmlPullParser.END_DOCUMENT) {
551 if (type == XmlPullParser.START_TAG && parser.getName().equals(TAG_USER)) {
552 String id = parser.getAttributeValue(null, ATTR_ID);
Amith Yamasanie4cf7342012-12-17 11:12:09 -0800553 UserInfo user = readUserLocked(Integer.parseInt(id));
Amith Yamasani6f34b412012-10-22 18:19:27 -0700554
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700555 if (user != null) {
556 mUsers.put(user.id, user);
Amith Yamasani2a003292012-08-14 18:25:45 -0700557 if (user.isGuest()) {
558 mGuestEnabled = true;
559 }
560 if (mNextSerialNumber < 0 || mNextSerialNumber <= user.id) {
561 mNextSerialNumber = user.id + 1;
562 }
Amith Yamasani258848d2012-08-10 17:06:33 -0700563 }
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700564 }
565 }
Amith Yamasani13593602012-03-22 16:16:17 -0700566 updateUserIdsLocked();
Amith Yamasani350962c2013-08-06 11:18:53 -0700567 upgradeIfNecessaryLocked();
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700568 } catch (IOException ioe) {
Amith Yamasani13593602012-03-22 16:16:17 -0700569 fallbackToSingleUserLocked();
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700570 } catch (XmlPullParserException pe) {
Amith Yamasani13593602012-03-22 16:16:17 -0700571 fallbackToSingleUserLocked();
Dianne Hackbornbfd89b32011-12-15 18:22:54 -0800572 } finally {
573 if (fis != null) {
574 try {
575 fis.close();
576 } catch (IOException e) {
577 }
578 }
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700579 }
580 }
581
Amith Yamasani6f34b412012-10-22 18:19:27 -0700582 /**
Amith Yamasanibc9625052012-11-15 14:39:18 -0800583 * Upgrade steps between versions, either for fixing bugs or changing the data format.
Amith Yamasani6f34b412012-10-22 18:19:27 -0700584 */
Amith Yamasani350962c2013-08-06 11:18:53 -0700585 private void upgradeIfNecessaryLocked() {
Amith Yamasani6f34b412012-10-22 18:19:27 -0700586 int userVersion = mUserVersion;
587 if (userVersion < 1) {
588 // Assign a proper name for the owner, if not initialized correctly before
589 UserInfo user = mUsers.get(UserHandle.USER_OWNER);
590 if ("Primary".equals(user.name)) {
591 user.name = mContext.getResources().getString(com.android.internal.R.string.owner_name);
592 writeUserLocked(user);
593 }
594 userVersion = 1;
595 }
596
Amith Yamasanibc9625052012-11-15 14:39:18 -0800597 if (userVersion < 2) {
598 // Owner should be marked as initialized
599 UserInfo user = mUsers.get(UserHandle.USER_OWNER);
600 if ((user.flags & UserInfo.FLAG_INITIALIZED) == 0) {
601 user.flags |= UserInfo.FLAG_INITIALIZED;
602 writeUserLocked(user);
603 }
604 userVersion = 2;
605 }
606
Amith Yamasani350962c2013-08-06 11:18:53 -0700607
Amith Yamasani5e486f52013-08-07 11:06:44 -0700608 if (userVersion < 4) {
Amith Yamasani5e486f52013-08-07 11:06:44 -0700609 userVersion = 4;
610 }
611
Amith Yamasani6f34b412012-10-22 18:19:27 -0700612 if (userVersion < USER_VERSION) {
613 Slog.w(LOG_TAG, "User version " + mUserVersion + " didn't upgrade as expected to "
614 + USER_VERSION);
615 } else {
616 mUserVersion = userVersion;
617 writeUserListLocked();
618 }
619 }
620
Amith Yamasani13593602012-03-22 16:16:17 -0700621 private void fallbackToSingleUserLocked() {
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700622 // Create the primary user
Amith Yamasani67df64b2012-12-14 12:09:36 -0800623 UserInfo primary = new UserInfo(UserHandle.USER_OWNER,
Amith Yamasani6f34b412012-10-22 18:19:27 -0700624 mContext.getResources().getString(com.android.internal.R.string.owner_name), null,
Amith Yamasani756901d2012-10-12 12:30:07 -0700625 UserInfo.FLAG_ADMIN | UserInfo.FLAG_PRIMARY | UserInfo.FLAG_INITIALIZED);
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700626 mUsers.put(0, primary);
Amith Yamasani634cf312012-10-04 17:34:21 -0700627 mNextSerialNumber = MIN_USER_ID;
Geoffrey Borggaard15b8b2c2013-08-28 22:11:10 -0400628 mUserVersion = USER_VERSION;
Amith Yamasani67df64b2012-12-14 12:09:36 -0800629
Geoffrey Borggaarde45e45e32013-01-24 10:03:20 -0500630 Bundle restrictions = new Bundle();
Amith Yamasani67df64b2012-12-14 12:09:36 -0800631 mUserRestrictions.append(UserHandle.USER_OWNER, restrictions);
632
Amith Yamasani13593602012-03-22 16:16:17 -0700633 updateUserIdsLocked();
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700634
Amith Yamasani13593602012-03-22 16:16:17 -0700635 writeUserListLocked();
636 writeUserLocked(primary);
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700637 }
638
639 /*
640 * Writes the user file in this format:
641 *
642 * <user flags="20039023" id="0">
643 * <name>Primary</name>
644 * </user>
645 */
Amith Yamasani13593602012-03-22 16:16:17 -0700646 private void writeUserLocked(UserInfo userInfo) {
Amith Yamasani742a6712011-05-04 14:49:28 -0700647 FileOutputStream fos = null;
Amith Yamasanifc95e702013-09-26 13:20:17 -0700648 AtomicFile userFile = new AtomicFile(new File(mUsersDir, userInfo.id + XML_SUFFIX));
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700649 try {
Amith Yamasani2a003292012-08-14 18:25:45 -0700650 fos = userFile.startWrite();
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700651 final BufferedOutputStream bos = new BufferedOutputStream(fos);
652
653 // XmlSerializer serializer = XmlUtils.serializerInstance();
654 final XmlSerializer serializer = new FastXmlSerializer();
655 serializer.setOutput(bos, "utf-8");
656 serializer.startDocument(null, true);
657 serializer.setFeature("http://xmlpull.org/v1/doc/features.html#indent-output", true);
658
659 serializer.startTag(null, TAG_USER);
660 serializer.attribute(null, ATTR_ID, Integer.toString(userInfo.id));
Amith Yamasani2a003292012-08-14 18:25:45 -0700661 serializer.attribute(null, ATTR_SERIAL_NO, Integer.toString(userInfo.serialNumber));
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700662 serializer.attribute(null, ATTR_FLAGS, Integer.toString(userInfo.flags));
Amith Yamasani920ace02012-09-20 22:15:37 -0700663 serializer.attribute(null, ATTR_CREATION_TIME, Long.toString(userInfo.creationTime));
664 serializer.attribute(null, ATTR_LAST_LOGGED_IN_TIME,
665 Long.toString(userInfo.lastLoggedInTime));
Amith Yamasani655d0e22013-06-12 14:19:10 -0700666 RestrictionsPinState pinState = mRestrictionsPinStates.get(userInfo.id);
667 if (pinState != null) {
668 if (pinState.salt != 0) {
669 serializer.attribute(null, ATTR_SALT, Long.toString(pinState.salt));
670 }
671 if (pinState.pinHash != null) {
672 serializer.attribute(null, ATTR_PIN_HASH, pinState.pinHash);
673 }
674 if (pinState.failedAttempts != 0) {
675 serializer.attribute(null, ATTR_FAILED_ATTEMPTS,
676 Integer.toString(pinState.failedAttempts));
677 serializer.attribute(null, ATTR_LAST_RETRY_MS,
678 Long.toString(pinState.lastAttemptTime));
679 }
680 }
Amith Yamasanib8151ec2012-04-18 18:02:48 -0700681 if (userInfo.iconPath != null) {
682 serializer.attribute(null, ATTR_ICON_PATH, userInfo.iconPath);
683 }
Dianne Hackbornd4ac8d72012-09-27 23:20:10 -0700684 if (userInfo.partial) {
685 serializer.attribute(null, ATTR_PARTIAL, "true");
686 }
Kenny Guy2a764942014-04-02 13:29:20 +0100687 if (userInfo.profileGroupId != UserInfo.NO_PROFILE_GROUP_ID) {
688 serializer.attribute(null, ATTR_PROFILE_GROUP_ID,
689 Integer.toString(userInfo.profileGroupId));
Kenny Guya52dc3e2014-02-11 15:33:14 +0000690 }
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700691
692 serializer.startTag(null, TAG_NAME);
693 serializer.text(userInfo.name);
694 serializer.endTag(null, TAG_NAME);
695
Amith Yamasanie4cf7342012-12-17 11:12:09 -0800696 Bundle restrictions = mUserRestrictions.get(userInfo.id);
697 if (restrictions != null) {
698 serializer.startTag(null, TAG_RESTRICTIONS);
Amith Yamasani71e6c692013-03-24 17:39:28 -0700699 writeBoolean(serializer, restrictions, UserManager.DISALLOW_CONFIG_WIFI);
700 writeBoolean(serializer, restrictions, UserManager.DISALLOW_MODIFY_ACCOUNTS);
701 writeBoolean(serializer, restrictions, UserManager.DISALLOW_INSTALL_APPS);
702 writeBoolean(serializer, restrictions, UserManager.DISALLOW_UNINSTALL_APPS);
703 writeBoolean(serializer, restrictions, UserManager.DISALLOW_SHARE_LOCATION);
Maggie Benthalla12fccf2013-03-14 18:02:12 -0400704 writeBoolean(serializer, restrictions,
705 UserManager.DISALLOW_INSTALL_UNKNOWN_SOURCES);
706 writeBoolean(serializer, restrictions, UserManager.DISALLOW_CONFIG_BLUETOOTH);
707 writeBoolean(serializer, restrictions, UserManager.DISALLOW_USB_FILE_TRANSFER);
Emily Bernierb223f732013-04-11 15:46:36 -0400708 writeBoolean(serializer, restrictions, UserManager.DISALLOW_CONFIG_CREDENTIALS);
709 writeBoolean(serializer, restrictions, UserManager.DISALLOW_REMOVE_USER);
Amith Yamasanie4cf7342012-12-17 11:12:09 -0800710 serializer.endTag(null, TAG_RESTRICTIONS);
711 }
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700712 serializer.endTag(null, TAG_USER);
713
714 serializer.endDocument();
Amith Yamasani2a003292012-08-14 18:25:45 -0700715 userFile.finishWrite(fos);
716 } catch (Exception ioe) {
Amith Yamasani0b285492011-04-14 17:35:23 -0700717 Slog.e(LOG_TAG, "Error writing user info " + userInfo.id + "\n" + ioe);
Amith Yamasani2a003292012-08-14 18:25:45 -0700718 userFile.failWrite(fos);
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700719 }
720 }
721
722 /*
723 * Writes the user list file in this format:
724 *
Amith Yamasani2a003292012-08-14 18:25:45 -0700725 * <users nextSerialNumber="3">
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700726 * <user id="0"></user>
727 * <user id="2"></user>
728 * </users>
729 */
Amith Yamasani13593602012-03-22 16:16:17 -0700730 private void writeUserListLocked() {
Amith Yamasani742a6712011-05-04 14:49:28 -0700731 FileOutputStream fos = null;
Amith Yamasani2a003292012-08-14 18:25:45 -0700732 AtomicFile userListFile = new AtomicFile(mUserListFile);
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700733 try {
Amith Yamasani2a003292012-08-14 18:25:45 -0700734 fos = userListFile.startWrite();
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700735 final BufferedOutputStream bos = new BufferedOutputStream(fos);
736
737 // XmlSerializer serializer = XmlUtils.serializerInstance();
738 final XmlSerializer serializer = new FastXmlSerializer();
739 serializer.setOutput(bos, "utf-8");
740 serializer.startDocument(null, true);
741 serializer.setFeature("http://xmlpull.org/v1/doc/features.html#indent-output", true);
742
743 serializer.startTag(null, TAG_USERS);
Amith Yamasani2a003292012-08-14 18:25:45 -0700744 serializer.attribute(null, ATTR_NEXT_SERIAL_NO, Integer.toString(mNextSerialNumber));
Amith Yamasani6f34b412012-10-22 18:19:27 -0700745 serializer.attribute(null, ATTR_USER_VERSION, Integer.toString(mUserVersion));
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700746
747 for (int i = 0; i < mUsers.size(); i++) {
748 UserInfo user = mUsers.valueAt(i);
749 serializer.startTag(null, TAG_USER);
750 serializer.attribute(null, ATTR_ID, Integer.toString(user.id));
751 serializer.endTag(null, TAG_USER);
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700752 }
753
754 serializer.endTag(null, TAG_USERS);
755
756 serializer.endDocument();
Amith Yamasani2a003292012-08-14 18:25:45 -0700757 userListFile.finishWrite(fos);
758 } catch (Exception e) {
759 userListFile.failWrite(fos);
Amith Yamasani0b285492011-04-14 17:35:23 -0700760 Slog.e(LOG_TAG, "Error writing user list");
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700761 }
762 }
763
Amith Yamasanie4cf7342012-12-17 11:12:09 -0800764 private UserInfo readUserLocked(int id) {
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700765 int flags = 0;
Amith Yamasani2a003292012-08-14 18:25:45 -0700766 int serialNumber = id;
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700767 String name = null;
Amith Yamasanib8151ec2012-04-18 18:02:48 -0700768 String iconPath = null;
Amith Yamasani920ace02012-09-20 22:15:37 -0700769 long creationTime = 0L;
770 long lastLoggedInTime = 0L;
Amith Yamasani655d0e22013-06-12 14:19:10 -0700771 long salt = 0L;
772 String pinHash = null;
773 int failedAttempts = 0;
Kenny Guy2a764942014-04-02 13:29:20 +0100774 int profileGroupId = UserInfo.NO_PROFILE_GROUP_ID;
Amith Yamasani655d0e22013-06-12 14:19:10 -0700775 long lastAttemptTime = 0L;
Dianne Hackbornd4ac8d72012-09-27 23:20:10 -0700776 boolean partial = false;
Amith Yamasanie4cf7342012-12-17 11:12:09 -0800777 Bundle restrictions = new Bundle();
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700778
779 FileInputStream fis = null;
780 try {
Amith Yamasani2a003292012-08-14 18:25:45 -0700781 AtomicFile userFile =
Amith Yamasanifc95e702013-09-26 13:20:17 -0700782 new AtomicFile(new File(mUsersDir, Integer.toString(id) + XML_SUFFIX));
Amith Yamasani2a003292012-08-14 18:25:45 -0700783 fis = userFile.openRead();
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700784 XmlPullParser parser = Xml.newPullParser();
785 parser.setInput(fis, null);
786 int type;
787 while ((type = parser.next()) != XmlPullParser.START_TAG
788 && type != XmlPullParser.END_DOCUMENT) {
789 ;
790 }
791
792 if (type != XmlPullParser.START_TAG) {
Amith Yamasani0b285492011-04-14 17:35:23 -0700793 Slog.e(LOG_TAG, "Unable to read user " + id);
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700794 return null;
795 }
796
797 if (type == XmlPullParser.START_TAG && parser.getName().equals(TAG_USER)) {
Amith Yamasani920ace02012-09-20 22:15:37 -0700798 int storedId = readIntAttribute(parser, ATTR_ID, -1);
799 if (storedId != id) {
Amith Yamasani0b285492011-04-14 17:35:23 -0700800 Slog.e(LOG_TAG, "User id does not match the file name");
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700801 return null;
802 }
Amith Yamasani920ace02012-09-20 22:15:37 -0700803 serialNumber = readIntAttribute(parser, ATTR_SERIAL_NO, id);
804 flags = readIntAttribute(parser, ATTR_FLAGS, 0);
Amith Yamasanib8151ec2012-04-18 18:02:48 -0700805 iconPath = parser.getAttributeValue(null, ATTR_ICON_PATH);
Amith Yamasani920ace02012-09-20 22:15:37 -0700806 creationTime = readLongAttribute(parser, ATTR_CREATION_TIME, 0);
807 lastLoggedInTime = readLongAttribute(parser, ATTR_LAST_LOGGED_IN_TIME, 0);
Amith Yamasani655d0e22013-06-12 14:19:10 -0700808 salt = readLongAttribute(parser, ATTR_SALT, 0L);
809 pinHash = parser.getAttributeValue(null, ATTR_PIN_HASH);
810 failedAttempts = readIntAttribute(parser, ATTR_FAILED_ATTEMPTS, 0);
811 lastAttemptTime = readLongAttribute(parser, ATTR_LAST_RETRY_MS, 0L);
Kenny Guy2a764942014-04-02 13:29:20 +0100812 profileGroupId = readIntAttribute(parser, ATTR_PROFILE_GROUP_ID,
813 UserInfo.NO_PROFILE_GROUP_ID);
814 if (profileGroupId == UserInfo.NO_PROFILE_GROUP_ID) {
815 // This attribute was added and renamed during development of L.
816 // TODO Remove upgrade path by 1st May 2014
817 profileGroupId = readIntAttribute(parser, "relatedGroupId",
818 UserInfo.NO_PROFILE_GROUP_ID);
819 }
Dianne Hackbornd4ac8d72012-09-27 23:20:10 -0700820 String valueString = parser.getAttributeValue(null, ATTR_PARTIAL);
821 if ("true".equals(valueString)) {
822 partial = true;
823 }
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700824
Amith Yamasanie4cf7342012-12-17 11:12:09 -0800825 int outerDepth = parser.getDepth();
826 while ((type = parser.next()) != XmlPullParser.END_DOCUMENT
827 && (type != XmlPullParser.END_TAG || parser.getDepth() > outerDepth)) {
828 if (type == XmlPullParser.END_TAG || type == XmlPullParser.TEXT) {
829 continue;
830 }
831 String tag = parser.getName();
832 if (TAG_NAME.equals(tag)) {
833 type = parser.next();
834 if (type == XmlPullParser.TEXT) {
835 name = parser.getText();
836 }
837 } else if (TAG_RESTRICTIONS.equals(tag)) {
Amith Yamasani71e6c692013-03-24 17:39:28 -0700838 readBoolean(parser, restrictions, UserManager.DISALLOW_CONFIG_WIFI);
839 readBoolean(parser, restrictions, UserManager.DISALLOW_MODIFY_ACCOUNTS);
840 readBoolean(parser, restrictions, UserManager.DISALLOW_INSTALL_APPS);
841 readBoolean(parser, restrictions, UserManager.DISALLOW_UNINSTALL_APPS);
842 readBoolean(parser, restrictions, UserManager.DISALLOW_SHARE_LOCATION);
Maggie Benthalla12fccf2013-03-14 18:02:12 -0400843 readBoolean(parser, restrictions,
844 UserManager.DISALLOW_INSTALL_UNKNOWN_SOURCES);
845 readBoolean(parser, restrictions, UserManager.DISALLOW_CONFIG_BLUETOOTH);
846 readBoolean(parser, restrictions, UserManager.DISALLOW_USB_FILE_TRANSFER);
Emily Bernierb223f732013-04-11 15:46:36 -0400847 readBoolean(parser, restrictions, UserManager.DISALLOW_CONFIG_CREDENTIALS);
848 readBoolean(parser, restrictions, UserManager.DISALLOW_REMOVE_USER);
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700849 }
850 }
851 }
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700852
Amith Yamasanib8151ec2012-04-18 18:02:48 -0700853 UserInfo userInfo = new UserInfo(id, name, iconPath, flags);
Amith Yamasani2a003292012-08-14 18:25:45 -0700854 userInfo.serialNumber = serialNumber;
Amith Yamasani920ace02012-09-20 22:15:37 -0700855 userInfo.creationTime = creationTime;
856 userInfo.lastLoggedInTime = lastLoggedInTime;
Dianne Hackbornd4ac8d72012-09-27 23:20:10 -0700857 userInfo.partial = partial;
Kenny Guy2a764942014-04-02 13:29:20 +0100858 userInfo.profileGroupId = profileGroupId;
Amith Yamasanie4cf7342012-12-17 11:12:09 -0800859 mUserRestrictions.append(id, restrictions);
Amith Yamasani655d0e22013-06-12 14:19:10 -0700860 if (salt != 0L) {
861 RestrictionsPinState pinState = mRestrictionsPinStates.get(id);
862 if (pinState == null) {
863 pinState = new RestrictionsPinState();
864 mRestrictionsPinStates.put(id, pinState);
865 }
866 pinState.salt = salt;
867 pinState.pinHash = pinHash;
868 pinState.failedAttempts = failedAttempts;
869 pinState.lastAttemptTime = lastAttemptTime;
870 }
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700871 return userInfo;
872
873 } catch (IOException ioe) {
874 } catch (XmlPullParserException pe) {
Dianne Hackbornbfd89b32011-12-15 18:22:54 -0800875 } finally {
876 if (fis != null) {
877 try {
878 fis.close();
879 } catch (IOException e) {
880 }
881 }
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700882 }
883 return null;
884 }
885
Amith Yamasanie4cf7342012-12-17 11:12:09 -0800886 private void readBoolean(XmlPullParser parser, Bundle restrictions,
887 String restrictionKey) {
888 String value = parser.getAttributeValue(null, restrictionKey);
Amith Yamasani71e6c692013-03-24 17:39:28 -0700889 if (value != null) {
890 restrictions.putBoolean(restrictionKey, Boolean.parseBoolean(value));
891 }
Amith Yamasanie4cf7342012-12-17 11:12:09 -0800892 }
893
894 private void writeBoolean(XmlSerializer xml, Bundle restrictions, String restrictionKey)
895 throws IOException {
896 if (restrictions.containsKey(restrictionKey)) {
897 xml.attribute(null, restrictionKey,
898 Boolean.toString(restrictions.getBoolean(restrictionKey)));
899 }
900 }
901
Amith Yamasani920ace02012-09-20 22:15:37 -0700902 private int readIntAttribute(XmlPullParser parser, String attr, int defaultValue) {
903 String valueString = parser.getAttributeValue(null, attr);
904 if (valueString == null) return defaultValue;
905 try {
906 return Integer.parseInt(valueString);
907 } catch (NumberFormatException nfe) {
908 return defaultValue;
909 }
910 }
911
912 private long readLongAttribute(XmlPullParser parser, String attr, long defaultValue) {
913 String valueString = parser.getAttributeValue(null, attr);
914 if (valueString == null) return defaultValue;
915 try {
916 return Long.parseLong(valueString);
917 } catch (NumberFormatException nfe) {
918 return defaultValue;
919 }
920 }
921
Amith Yamasani1a7472e2013-07-02 11:17:30 -0700922 private boolean isPackageInstalled(String pkg, int userId) {
923 final ApplicationInfo info = mPm.getApplicationInfo(pkg,
924 PackageManager.GET_UNINSTALLED_PACKAGES,
925 userId);
926 if (info == null || (info.flags&ApplicationInfo.FLAG_INSTALLED) == 0) {
927 return false;
928 }
929 return true;
930 }
931
Amith Yamasanib82add22013-07-09 11:24:44 -0700932 /**
Amith Yamasani1a7472e2013-07-02 11:17:30 -0700933 * Removes all the restrictions files (res_<packagename>) for a given user, if all is true,
934 * else removes only those packages that have been uninstalled.
Amith Yamasanib82add22013-07-09 11:24:44 -0700935 * Does not do any permissions checking.
936 */
Amith Yamasani1a7472e2013-07-02 11:17:30 -0700937 private void cleanAppRestrictions(int userId, boolean all) {
Amith Yamasanib82add22013-07-09 11:24:44 -0700938 synchronized (mPackagesLock) {
939 File dir = Environment.getUserSystemDirectory(userId);
940 String[] files = dir.list();
941 if (files == null) return;
942 for (String fileName : files) {
943 if (fileName.startsWith(RESTRICTIONS_FILE_PREFIX)) {
944 File resFile = new File(dir, fileName);
945 if (resFile.exists()) {
Amith Yamasani1a7472e2013-07-02 11:17:30 -0700946 if (all) {
947 resFile.delete();
948 } else {
Amith Yamasanifc95e702013-09-26 13:20:17 -0700949 String pkg = restrictionsFileNameToPackage(fileName);
Amith Yamasani1a7472e2013-07-02 11:17:30 -0700950 if (!isPackageInstalled(pkg, userId)) {
951 resFile.delete();
952 }
953 }
Amith Yamasanib82add22013-07-09 11:24:44 -0700954 }
955 }
956 }
957 }
958 }
959
Amith Yamasani1a7472e2013-07-02 11:17:30 -0700960 /**
961 * Removes the app restrictions file for a specific package and user id, if it exists.
962 */
963 private void cleanAppRestrictionsForPackage(String pkg, int userId) {
964 synchronized (mPackagesLock) {
965 File dir = Environment.getUserSystemDirectory(userId);
Amith Yamasanifc95e702013-09-26 13:20:17 -0700966 File resFile = new File(dir, packageToRestrictionsFileName(pkg));
Amith Yamasani1a7472e2013-07-02 11:17:30 -0700967 if (resFile.exists()) {
968 resFile.delete();
969 }
970 }
971 }
972
Kenny Guy2a764942014-04-02 13:29:20 +0100973 private int getNextProfileGroupIdLocked() {
974 int maxGroupId = UserInfo.NO_PROFILE_GROUP_ID;
Kenny Guya52dc3e2014-02-11 15:33:14 +0000975 for (int i = 0; i < mUsers.size(); i++) {
976 UserInfo ui = mUsers.valueAt(i);
Kenny Guy2a764942014-04-02 13:29:20 +0100977 if (maxGroupId < ui.profileGroupId) {
978 maxGroupId = ui.profileGroupId;
Kenny Guya52dc3e2014-02-11 15:33:14 +0000979 }
980 }
981 return maxGroupId + 1;
982 }
983
984 @Override
Kenny Guy2a764942014-04-02 13:29:20 +0100985 public UserInfo createProfileForUser(String name, int flags, int userId) {
Kenny Guya52dc3e2014-02-11 15:33:14 +0000986 checkManageUsersPermission("Only the system can create users");
Kenny Guy2a764942014-04-02 13:29:20 +0100987 if (userId != UserHandle.USER_OWNER) {
988 Slog.w(LOG_TAG, "Only user owner can have profiles");
Kenny Guya52dc3e2014-02-11 15:33:14 +0000989 return null;
990 }
Kenny Guy2a764942014-04-02 13:29:20 +0100991 return createUserInternal(name, flags, userId);
Kenny Guya52dc3e2014-02-11 15:33:14 +0000992 }
993
Amith Yamasani258848d2012-08-10 17:06:33 -0700994 @Override
Amith Yamasani13593602012-03-22 16:16:17 -0700995 public UserInfo createUser(String name, int flags) {
Amith Yamasani2a003292012-08-14 18:25:45 -0700996 checkManageUsersPermission("Only the system can create users");
Nicolas Prevotc6d033e2014-02-27 13:11:09 +0000997 return createUserInternal(name, flags, UserHandle.USER_NULL);
Kenny Guya52dc3e2014-02-11 15:33:14 +0000998 }
Amith Yamasanifaea76f2012-09-11 10:59:48 -0700999
Kenny Guy2a764942014-04-02 13:29:20 +01001000 private UserInfo createUserInternal(String name, int flags, int profileId) {
Dianne Hackborn5dc5a002012-09-15 19:33:48 -07001001 final long ident = Binder.clearCallingIdentity();
Nicolas Prevotc6d033e2014-02-27 13:11:09 +00001002 UserInfo userInfo = null;
Dianne Hackborn5dc5a002012-09-15 19:33:48 -07001003 try {
1004 synchronized (mInstallLock) {
1005 synchronized (mPackagesLock) {
Kenny Guy2a764942014-04-02 13:29:20 +01001006 UserInfo profile = null;
1007 if (profileId != UserHandle.USER_NULL) {
1008 profile = getUserInfoLocked(profileId);
1009 if (profile == null) return null;
Nicolas Prevotc6d033e2014-02-27 13:11:09 +00001010 }
Dianne Hackborn5dc5a002012-09-15 19:33:48 -07001011 if (isUserLimitReachedLocked()) return null;
1012 int userId = getNextAvailableIdLocked();
1013 userInfo = new UserInfo(userId, name, null, flags);
1014 File userPath = new File(mBaseUserPath, Integer.toString(userId));
1015 userInfo.serialNumber = mNextSerialNumber++;
Amith Yamasani920ace02012-09-20 22:15:37 -07001016 long now = System.currentTimeMillis();
1017 userInfo.creationTime = (now > EPOCH_PLUS_30_YEARS) ? now : 0;
Dianne Hackbornd4ac8d72012-09-27 23:20:10 -07001018 userInfo.partial = true;
Amith Yamasani16389312012-10-17 21:20:14 -07001019 Environment.getUserSystemDirectory(userInfo.id).mkdirs();
Dianne Hackborn5dc5a002012-09-15 19:33:48 -07001020 mUsers.put(userId, userInfo);
1021 writeUserListLocked();
Kenny Guy2a764942014-04-02 13:29:20 +01001022 if (profile != null) {
1023 if (profile.profileGroupId == UserInfo.NO_PROFILE_GROUP_ID) {
1024 profile.profileGroupId = getNextProfileGroupIdLocked();
1025 writeUserLocked(profile);
Kenny Guya52dc3e2014-02-11 15:33:14 +00001026 }
Kenny Guy2a764942014-04-02 13:29:20 +01001027 userInfo.profileGroupId = profile.profileGroupId;
Kenny Guya52dc3e2014-02-11 15:33:14 +00001028 }
Dianne Hackborn5dc5a002012-09-15 19:33:48 -07001029 writeUserLocked(userInfo);
Dianne Hackborn5dc5a002012-09-15 19:33:48 -07001030 mPm.createNewUserLILPw(userId, userPath);
Dianne Hackbornd4ac8d72012-09-27 23:20:10 -07001031 userInfo.partial = false;
1032 writeUserLocked(userInfo);
1033 updateUserIdsLocked();
Geoffrey Borggaarde45e45e32013-01-24 10:03:20 -05001034 Bundle restrictions = new Bundle();
Geoffrey Borggaarde45e45e32013-01-24 10:03:20 -05001035 mUserRestrictions.append(userId, restrictions);
Dianne Hackborn5dc5a002012-09-15 19:33:48 -07001036 }
Dianne Hackborn4428e172012-08-24 17:43:05 -07001037 }
Dianne Hackborn5dc5a002012-09-15 19:33:48 -07001038 if (userInfo != null) {
1039 Intent addedIntent = new Intent(Intent.ACTION_USER_ADDED);
1040 addedIntent.putExtra(Intent.EXTRA_USER_HANDLE, userInfo.id);
1041 mContext.sendBroadcastAsUser(addedIntent, UserHandle.ALL,
1042 android.Manifest.permission.MANAGE_USERS);
1043 }
1044 } finally {
1045 Binder.restoreCallingIdentity(ident);
Amith Yamasani258848d2012-08-10 17:06:33 -07001046 }
Amith Yamasani4b2e9342011-03-31 12:38:53 -07001047 return userInfo;
1048 }
1049
Amith Yamasani0b285492011-04-14 17:35:23 -07001050 /**
1051 * Removes a user and all data directories created for that user. This method should be called
1052 * after the user's processes have been terminated.
Dianne Hackborn10ad9822014-03-17 11:28:36 -07001053 * @param userHandle the user's id
Amith Yamasani0b285492011-04-14 17:35:23 -07001054 */
Amith Yamasani258848d2012-08-10 17:06:33 -07001055 public boolean removeUser(int userHandle) {
Amith Yamasani2a003292012-08-14 18:25:45 -07001056 checkManageUsersPermission("Only the system can remove users");
Dianne Hackborn80a4af22012-08-27 19:18:31 -07001057 final UserInfo user;
1058 synchronized (mPackagesLock) {
1059 user = mUsers.get(userHandle);
1060 if (userHandle == 0 || user == null) {
1061 return false;
1062 }
Jeff Sharkeyffe0cb42012-11-05 17:24:43 -08001063 mRemovingUserIds.put(userHandle, true);
Amith Yamasani756901d2012-10-12 12:30:07 -07001064 // Set this to a partially created user, so that the user will be purged
1065 // on next startup, in case the runtime stops now before stopping and
1066 // removing the user completely.
1067 user.partial = true;
1068 writeUserLocked(user);
Dianne Hackborn80a4af22012-08-27 19:18:31 -07001069 }
Amith Yamasani16389312012-10-17 21:20:14 -07001070 if (DBG) Slog.i(LOG_TAG, "Stopping user " + userHandle);
Dianne Hackborn80a4af22012-08-27 19:18:31 -07001071 int res;
1072 try {
1073 res = ActivityManagerNative.getDefault().stopUser(userHandle,
1074 new IStopUserCallback.Stub() {
1075 @Override
1076 public void userStopped(int userId) {
1077 finishRemoveUser(userId);
1078 }
1079 @Override
1080 public void userStopAborted(int userId) {
1081 }
1082 });
1083 } catch (RemoteException e) {
1084 return false;
1085 }
1086
1087 return res == ActivityManager.USER_OP_SUCCESS;
1088 }
1089
Amith Yamasanidb6a14c2012-10-17 21:16:52 -07001090 void finishRemoveUser(final int userHandle) {
Amith Yamasani16389312012-10-17 21:20:14 -07001091 if (DBG) Slog.i(LOG_TAG, "finishRemoveUser " + userHandle);
Amith Yamasanidb6a14c2012-10-17 21:16:52 -07001092 // Let other services shutdown any activity and clean up their state before completely
1093 // wiping the user's system directory and removing from the user list
Dianne Hackborn5dc5a002012-09-15 19:33:48 -07001094 long ident = Binder.clearCallingIdentity();
1095 try {
1096 Intent addedIntent = new Intent(Intent.ACTION_USER_REMOVED);
1097 addedIntent.putExtra(Intent.EXTRA_USER_HANDLE, userHandle);
Amith Yamasanidb6a14c2012-10-17 21:16:52 -07001098 mContext.sendOrderedBroadcastAsUser(addedIntent, UserHandle.ALL,
1099 android.Manifest.permission.MANAGE_USERS,
1100
1101 new BroadcastReceiver() {
1102 @Override
1103 public void onReceive(Context context, Intent intent) {
1104 if (DBG) {
1105 Slog.i(LOG_TAG,
1106 "USER_REMOVED broadcast sent, cleaning up user data "
1107 + userHandle);
1108 }
1109 new Thread() {
1110 public void run() {
1111 synchronized (mInstallLock) {
1112 synchronized (mPackagesLock) {
1113 removeUserStateLocked(userHandle);
1114 }
1115 }
1116 }
1117 }.start();
1118 }
1119 },
1120
1121 null, Activity.RESULT_OK, null, null);
Dianne Hackborn5dc5a002012-09-15 19:33:48 -07001122 } finally {
1123 Binder.restoreCallingIdentity(ident);
1124 }
Amith Yamasani2a003292012-08-14 18:25:45 -07001125 }
1126
Jeff Sharkeyffe0cb42012-11-05 17:24:43 -08001127 private void removeUserStateLocked(final int userHandle) {
Dianne Hackbornd4ac8d72012-09-27 23:20:10 -07001128 // Cleanup package manager settings
1129 mPm.cleanUpUserLILPw(userHandle);
1130
1131 // Remove this user from the list
1132 mUsers.remove(userHandle);
Jeff Sharkeyffe0cb42012-11-05 17:24:43 -08001133
1134 // Have user ID linger for several seconds to let external storage VFS
1135 // cache entries expire. This must be greater than the 'entry_valid'
1136 // timeout used by the FUSE daemon.
1137 mHandler.postDelayed(new Runnable() {
1138 @Override
1139 public void run() {
1140 synchronized (mPackagesLock) {
1141 mRemovingUserIds.delete(userHandle);
1142 }
1143 }
1144 }, MINUTE_IN_MILLIS);
1145
Amith Yamasani655d0e22013-06-12 14:19:10 -07001146 mRestrictionsPinStates.remove(userHandle);
Dianne Hackbornd4ac8d72012-09-27 23:20:10 -07001147 // Remove user file
Amith Yamasanifc95e702013-09-26 13:20:17 -07001148 AtomicFile userFile = new AtomicFile(new File(mUsersDir, userHandle + XML_SUFFIX));
Dianne Hackbornd4ac8d72012-09-27 23:20:10 -07001149 userFile.delete();
1150 // Update the user list
1151 writeUserListLocked();
1152 updateUserIdsLocked();
1153 removeDirectoryRecursive(Environment.getUserSystemDirectory(userHandle));
1154 }
1155
Amith Yamasani61f57372012-08-31 12:12:28 -07001156 private void removeDirectoryRecursive(File parent) {
1157 if (parent.isDirectory()) {
1158 String[] files = parent.list();
1159 for (String filename : files) {
1160 File child = new File(parent, filename);
1161 removeDirectoryRecursive(child);
1162 }
1163 }
1164 parent.delete();
1165 }
1166
Amith Yamasani2a003292012-08-14 18:25:45 -07001167 @Override
Amith Yamasani7e99bc02013-04-16 18:24:51 -07001168 public Bundle getApplicationRestrictions(String packageName) {
1169 return getApplicationRestrictionsForUser(packageName, UserHandle.getCallingUserId());
1170 }
1171
1172 @Override
1173 public Bundle getApplicationRestrictionsForUser(String packageName, int userId) {
Amith Yamasanidf2e92a2013-03-01 17:04:38 -08001174 if (UserHandle.getCallingUserId() != userId
Amith Yamasani9429afb2013-04-10 18:40:51 -07001175 || !UserHandle.isSameApp(Binder.getCallingUid(), getUidForPackage(packageName))) {
Amith Yamasanidf2e92a2013-03-01 17:04:38 -08001176 checkManageUsersPermission("Only system can get restrictions for other users/apps");
1177 }
1178 synchronized (mPackagesLock) {
1179 // Read the restrictions from XML
1180 return readApplicationRestrictionsLocked(packageName, userId);
1181 }
1182 }
1183
1184 @Override
Amith Yamasani7e99bc02013-04-16 18:24:51 -07001185 public void setApplicationRestrictions(String packageName, Bundle restrictions,
Amith Yamasanidf2e92a2013-03-01 17:04:38 -08001186 int userId) {
1187 if (UserHandle.getCallingUserId() != userId
Amith Yamasani9429afb2013-04-10 18:40:51 -07001188 || !UserHandle.isSameApp(Binder.getCallingUid(), getUidForPackage(packageName))) {
Amith Yamasanidf2e92a2013-03-01 17:04:38 -08001189 checkManageUsersPermission("Only system can set restrictions for other users/apps");
1190 }
1191 synchronized (mPackagesLock) {
1192 // Write the restrictions to XML
Amith Yamasani7e99bc02013-04-16 18:24:51 -07001193 writeApplicationRestrictionsLocked(packageName, restrictions, userId);
Amith Yamasanidf2e92a2013-03-01 17:04:38 -08001194 }
1195 }
1196
Amith Yamasani655d0e22013-06-12 14:19:10 -07001197 @Override
Amith Yamasanid304af62013-09-05 09:30:23 -07001198 public boolean setRestrictionsChallenge(String newPin) {
Amith Yamasani655d0e22013-06-12 14:19:10 -07001199 checkManageUsersPermission("Only system can modify the restrictions pin");
1200 int userId = UserHandle.getCallingUserId();
1201 synchronized (mPackagesLock) {
1202 RestrictionsPinState pinState = mRestrictionsPinStates.get(userId);
1203 if (pinState == null) {
1204 pinState = new RestrictionsPinState();
1205 }
1206 if (newPin == null) {
1207 pinState.salt = 0;
1208 pinState.pinHash = null;
1209 } else {
1210 try {
1211 pinState.salt = SecureRandom.getInstance("SHA1PRNG").nextLong();
1212 } catch (NoSuchAlgorithmException e) {
1213 pinState.salt = (long) (Math.random() * Long.MAX_VALUE);
1214 }
1215 pinState.pinHash = passwordToHash(newPin, pinState.salt);
1216 pinState.failedAttempts = 0;
1217 }
1218 mRestrictionsPinStates.put(userId, pinState);
1219 writeUserLocked(mUsers.get(userId));
1220 }
1221 return true;
1222 }
1223
1224 @Override
Amith Yamasanid304af62013-09-05 09:30:23 -07001225 public int checkRestrictionsChallenge(String pin) {
Amith Yamasani655d0e22013-06-12 14:19:10 -07001226 checkManageUsersPermission("Only system can verify the restrictions pin");
1227 int userId = UserHandle.getCallingUserId();
1228 synchronized (mPackagesLock) {
1229 RestrictionsPinState pinState = mRestrictionsPinStates.get(userId);
1230 // If there's no pin set, return error code
1231 if (pinState == null || pinState.salt == 0 || pinState.pinHash == null) {
1232 return UserManager.PIN_VERIFICATION_FAILED_NOT_SET;
1233 } else if (pin == null) {
1234 // If just checking if user can be prompted, return remaining time
1235 int waitTime = getRemainingTimeForPinAttempt(pinState);
1236 Slog.d(LOG_TAG, "Remaining waittime peek=" + waitTime);
1237 return waitTime;
1238 } else {
1239 int waitTime = getRemainingTimeForPinAttempt(pinState);
1240 Slog.d(LOG_TAG, "Remaining waittime=" + waitTime);
1241 if (waitTime > 0) {
1242 return waitTime;
1243 }
1244 if (passwordToHash(pin, pinState.salt).equals(pinState.pinHash)) {
1245 pinState.failedAttempts = 0;
1246 writeUserLocked(mUsers.get(userId));
1247 return UserManager.PIN_VERIFICATION_SUCCESS;
1248 } else {
1249 pinState.failedAttempts++;
1250 pinState.lastAttemptTime = System.currentTimeMillis();
1251 writeUserLocked(mUsers.get(userId));
1252 return waitTime;
1253 }
1254 }
1255 }
1256 }
1257
1258 private int getRemainingTimeForPinAttempt(RestrictionsPinState pinState) {
1259 int backoffIndex = Math.min(pinState.failedAttempts / BACKOFF_INC_INTERVAL,
1260 BACKOFF_TIMES.length - 1);
1261 int backoffTime = (pinState.failedAttempts % BACKOFF_INC_INTERVAL) == 0 ?
1262 BACKOFF_TIMES[backoffIndex] : 0;
1263 return (int) Math.max(backoffTime + pinState.lastAttemptTime - System.currentTimeMillis(),
1264 0);
1265 }
1266
1267 @Override
Amith Yamasanid304af62013-09-05 09:30:23 -07001268 public boolean hasRestrictionsChallenge() {
Amith Yamasani655d0e22013-06-12 14:19:10 -07001269 int userId = UserHandle.getCallingUserId();
1270 synchronized (mPackagesLock) {
Amith Yamasani0343ec32013-07-22 14:52:06 -07001271 return hasRestrictionsPinLocked(userId);
1272 }
1273 }
1274
1275 private boolean hasRestrictionsPinLocked(int userId) {
1276 RestrictionsPinState pinState = mRestrictionsPinStates.get(userId);
1277 if (pinState == null || pinState.salt == 0 || pinState.pinHash == null) {
1278 return false;
Amith Yamasani655d0e22013-06-12 14:19:10 -07001279 }
1280 return true;
1281 }
1282
Amith Yamasani1a7472e2013-07-02 11:17:30 -07001283 @Override
1284 public void removeRestrictions() {
1285 checkManageUsersPermission("Only system can remove restrictions");
1286 final int userHandle = UserHandle.getCallingUserId();
Amith Yamasani5e486f52013-08-07 11:06:44 -07001287 removeRestrictionsForUser(userHandle, true);
Amith Yamasani350962c2013-08-06 11:18:53 -07001288 }
1289
Amith Yamasani5e486f52013-08-07 11:06:44 -07001290 private void removeRestrictionsForUser(final int userHandle, boolean unblockApps) {
Amith Yamasani1a7472e2013-07-02 11:17:30 -07001291 synchronized (mPackagesLock) {
1292 // Remove all user restrictions
1293 setUserRestrictions(new Bundle(), userHandle);
1294 // Remove restrictions pin
Amith Yamasanid304af62013-09-05 09:30:23 -07001295 setRestrictionsChallenge(null);
Amith Yamasani1a7472e2013-07-02 11:17:30 -07001296 // Remove any app restrictions
1297 cleanAppRestrictions(userHandle, true);
1298 }
Amith Yamasani5e486f52013-08-07 11:06:44 -07001299 if (unblockApps) {
1300 unblockAllAppsForUser(userHandle);
1301 }
1302 }
1303
1304 private void unblockAllAppsForUser(final int userHandle) {
Amith Yamasani1a7472e2013-07-02 11:17:30 -07001305 mHandler.post(new Runnable() {
1306 @Override
1307 public void run() {
1308 List<ApplicationInfo> apps =
1309 mPm.getInstalledApplications(PackageManager.GET_UNINSTALLED_PACKAGES,
1310 userHandle).getList();
1311 final long ident = Binder.clearCallingIdentity();
1312 try {
1313 for (ApplicationInfo appInfo : apps) {
1314 if ((appInfo.flags & ApplicationInfo.FLAG_INSTALLED) != 0
1315 && (appInfo.flags & ApplicationInfo.FLAG_BLOCKED) != 0) {
1316 mPm.setApplicationBlockedSettingAsUser(appInfo.packageName, false,
1317 userHandle);
1318 }
1319 }
1320 } finally {
1321 Binder.restoreCallingIdentity(ident);
1322 }
1323 }
1324 });
1325 }
1326
Amith Yamasani655d0e22013-06-12 14:19:10 -07001327 /*
1328 * Generate a hash for the given password. To avoid brute force attacks, we use a salted hash.
1329 * Not the most secure, but it is at least a second level of protection. First level is that
1330 * the file is in a location only readable by the system process.
1331 * @param password the password.
1332 * @param salt the randomly generated salt
1333 * @return the hash of the pattern in a String.
1334 */
1335 private String passwordToHash(String password, long salt) {
1336 if (password == null) {
1337 return null;
1338 }
1339 String algo = null;
1340 String hashed = salt + password;
1341 try {
1342 byte[] saltedPassword = (password + salt).getBytes();
1343 byte[] sha1 = MessageDigest.getInstance(algo = "SHA-1").digest(saltedPassword);
1344 byte[] md5 = MessageDigest.getInstance(algo = "MD5").digest(saltedPassword);
1345 hashed = toHex(sha1) + toHex(md5);
1346 } catch (NoSuchAlgorithmException e) {
1347 Log.w(LOG_TAG, "Failed to encode string because of missing algorithm: " + algo);
1348 }
1349 return hashed;
1350 }
1351
1352 private static String toHex(byte[] ary) {
1353 final String hex = "0123456789ABCDEF";
1354 String ret = "";
1355 for (int i = 0; i < ary.length; i++) {
1356 ret += hex.charAt((ary[i] >> 4) & 0xf);
1357 ret += hex.charAt(ary[i] & 0xf);
1358 }
1359 return ret;
1360 }
1361
Amith Yamasanidf2e92a2013-03-01 17:04:38 -08001362 private int getUidForPackage(String packageName) {
Amith Yamasani9429afb2013-04-10 18:40:51 -07001363 long ident = Binder.clearCallingIdentity();
Amith Yamasanidf2e92a2013-03-01 17:04:38 -08001364 try {
1365 return mContext.getPackageManager().getApplicationInfo(packageName,
1366 PackageManager.GET_UNINSTALLED_PACKAGES).uid;
1367 } catch (NameNotFoundException nnfe) {
1368 return -1;
Amith Yamasani9429afb2013-04-10 18:40:51 -07001369 } finally {
1370 Binder.restoreCallingIdentity(ident);
Amith Yamasanidf2e92a2013-03-01 17:04:38 -08001371 }
1372 }
1373
Amith Yamasani7e99bc02013-04-16 18:24:51 -07001374 private Bundle readApplicationRestrictionsLocked(String packageName,
Amith Yamasanidf2e92a2013-03-01 17:04:38 -08001375 int userId) {
Amith Yamasani7e99bc02013-04-16 18:24:51 -07001376 final Bundle restrictions = new Bundle();
Amith Yamasanidf2e92a2013-03-01 17:04:38 -08001377 final ArrayList<String> values = new ArrayList<String>();
1378
1379 FileInputStream fis = null;
1380 try {
1381 AtomicFile restrictionsFile =
1382 new AtomicFile(new File(Environment.getUserSystemDirectory(userId),
Amith Yamasanifc95e702013-09-26 13:20:17 -07001383 packageToRestrictionsFileName(packageName)));
Amith Yamasanidf2e92a2013-03-01 17:04:38 -08001384 fis = restrictionsFile.openRead();
1385 XmlPullParser parser = Xml.newPullParser();
1386 parser.setInput(fis, null);
1387 int type;
1388 while ((type = parser.next()) != XmlPullParser.START_TAG
1389 && type != XmlPullParser.END_DOCUMENT) {
1390 ;
1391 }
1392
1393 if (type != XmlPullParser.START_TAG) {
1394 Slog.e(LOG_TAG, "Unable to read restrictions file "
1395 + restrictionsFile.getBaseFile());
Amith Yamasani7e99bc02013-04-16 18:24:51 -07001396 return restrictions;
Amith Yamasanidf2e92a2013-03-01 17:04:38 -08001397 }
1398
1399 while ((type = parser.next()) != XmlPullParser.END_DOCUMENT) {
1400 if (type == XmlPullParser.START_TAG && parser.getName().equals(TAG_ENTRY)) {
1401 String key = parser.getAttributeValue(null, ATTR_KEY);
Amith Yamasani7e99bc02013-04-16 18:24:51 -07001402 String valType = parser.getAttributeValue(null, ATTR_VALUE_TYPE);
Amith Yamasanidf2e92a2013-03-01 17:04:38 -08001403 String multiple = parser.getAttributeValue(null, ATTR_MULTIPLE);
1404 if (multiple != null) {
1405 int count = Integer.parseInt(multiple);
1406 while (count > 0 && (type = parser.next()) != XmlPullParser.END_DOCUMENT) {
1407 if (type == XmlPullParser.START_TAG
1408 && parser.getName().equals(TAG_VALUE)) {
1409 values.add(parser.nextText().trim());
1410 count--;
1411 }
1412 }
1413 String [] valueStrings = new String[values.size()];
1414 values.toArray(valueStrings);
Amith Yamasani7e99bc02013-04-16 18:24:51 -07001415 restrictions.putStringArray(key, valueStrings);
1416 } else if (ATTR_TYPE_BOOLEAN.equals(valType)) {
1417 restrictions.putBoolean(key, Boolean.parseBoolean(
1418 parser.nextText().trim()));
Amith Yamasanidf2e92a2013-03-01 17:04:38 -08001419 } else {
1420 String value = parser.nextText().trim();
Amith Yamasani7e99bc02013-04-16 18:24:51 -07001421 restrictions.putString(key, value);
Amith Yamasanidf2e92a2013-03-01 17:04:38 -08001422 }
1423 }
1424 }
1425
1426 } catch (IOException ioe) {
1427 } catch (XmlPullParserException pe) {
1428 } finally {
1429 if (fis != null) {
1430 try {
1431 fis.close();
1432 } catch (IOException e) {
1433 }
1434 }
1435 }
Amith Yamasani7e99bc02013-04-16 18:24:51 -07001436 return restrictions;
Amith Yamasanidf2e92a2013-03-01 17:04:38 -08001437 }
1438
1439 private void writeApplicationRestrictionsLocked(String packageName,
Amith Yamasani7e99bc02013-04-16 18:24:51 -07001440 Bundle restrictions, int userId) {
Amith Yamasanidf2e92a2013-03-01 17:04:38 -08001441 FileOutputStream fos = null;
1442 AtomicFile restrictionsFile = new AtomicFile(
1443 new File(Environment.getUserSystemDirectory(userId),
Amith Yamasanifc95e702013-09-26 13:20:17 -07001444 packageToRestrictionsFileName(packageName)));
Amith Yamasanidf2e92a2013-03-01 17:04:38 -08001445 try {
1446 fos = restrictionsFile.startWrite();
1447 final BufferedOutputStream bos = new BufferedOutputStream(fos);
1448
1449 // XmlSerializer serializer = XmlUtils.serializerInstance();
1450 final XmlSerializer serializer = new FastXmlSerializer();
1451 serializer.setOutput(bos, "utf-8");
1452 serializer.startDocument(null, true);
1453 serializer.setFeature("http://xmlpull.org/v1/doc/features.html#indent-output", true);
1454
1455 serializer.startTag(null, TAG_RESTRICTIONS);
1456
Amith Yamasani7e99bc02013-04-16 18:24:51 -07001457 for (String key : restrictions.keySet()) {
1458 Object value = restrictions.get(key);
Amith Yamasanidf2e92a2013-03-01 17:04:38 -08001459 serializer.startTag(null, TAG_ENTRY);
Amith Yamasani7e99bc02013-04-16 18:24:51 -07001460 serializer.attribute(null, ATTR_KEY, key);
1461
1462 if (value instanceof Boolean) {
1463 serializer.attribute(null, ATTR_VALUE_TYPE, ATTR_TYPE_BOOLEAN);
1464 serializer.text(value.toString());
1465 } else if (value == null || value instanceof String) {
1466 serializer.attribute(null, ATTR_VALUE_TYPE, ATTR_TYPE_STRING);
1467 serializer.text(value != null ? (String) value : "");
Amith Yamasanidf2e92a2013-03-01 17:04:38 -08001468 } else {
Amith Yamasani7e99bc02013-04-16 18:24:51 -07001469 serializer.attribute(null, ATTR_VALUE_TYPE, ATTR_TYPE_STRING_ARRAY);
1470 String[] values = (String[]) value;
Amith Yamasanidf2e92a2013-03-01 17:04:38 -08001471 serializer.attribute(null, ATTR_MULTIPLE, Integer.toString(values.length));
Amith Yamasani7e99bc02013-04-16 18:24:51 -07001472 for (String choice : values) {
Amith Yamasanidf2e92a2013-03-01 17:04:38 -08001473 serializer.startTag(null, TAG_VALUE);
Amith Yamasani7e99bc02013-04-16 18:24:51 -07001474 serializer.text(choice != null ? choice : "");
Amith Yamasanidf2e92a2013-03-01 17:04:38 -08001475 serializer.endTag(null, TAG_VALUE);
1476 }
1477 }
1478 serializer.endTag(null, TAG_ENTRY);
1479 }
1480
1481 serializer.endTag(null, TAG_RESTRICTIONS);
1482
1483 serializer.endDocument();
1484 restrictionsFile.finishWrite(fos);
1485 } catch (Exception e) {
1486 restrictionsFile.failWrite(fos);
1487 Slog.e(LOG_TAG, "Error writing application restrictions list");
1488 }
1489 }
1490
1491 @Override
Amith Yamasani2a003292012-08-14 18:25:45 -07001492 public int getUserSerialNumber(int userHandle) {
Dianne Hackborn4428e172012-08-24 17:43:05 -07001493 synchronized (mPackagesLock) {
Amith Yamasani2a003292012-08-14 18:25:45 -07001494 if (!exists(userHandle)) return -1;
Amith Yamasani195263742012-08-21 15:40:12 -07001495 return getUserInfoLocked(userHandle).serialNumber;
Amith Yamasani2a003292012-08-14 18:25:45 -07001496 }
1497 }
1498
1499 @Override
1500 public int getUserHandle(int userSerialNumber) {
Dianne Hackborn4428e172012-08-24 17:43:05 -07001501 synchronized (mPackagesLock) {
Amith Yamasani2a003292012-08-14 18:25:45 -07001502 for (int userId : mUserIds) {
Amith Yamasani195263742012-08-21 15:40:12 -07001503 if (getUserInfoLocked(userId).serialNumber == userSerialNumber) return userId;
Amith Yamasani2a003292012-08-14 18:25:45 -07001504 }
1505 // Not found
1506 return -1;
Amith Yamasani13593602012-03-22 16:16:17 -07001507 }
1508 }
1509
Amith Yamasani0b285492011-04-14 17:35:23 -07001510 /**
1511 * Caches the list of user ids in an array, adjusting the array size when necessary.
1512 */
Amith Yamasani13593602012-03-22 16:16:17 -07001513 private void updateUserIdsLocked() {
Dianne Hackbornd4ac8d72012-09-27 23:20:10 -07001514 int num = 0;
Amith Yamasani0b285492011-04-14 17:35:23 -07001515 for (int i = 0; i < mUsers.size(); i++) {
Dianne Hackbornd4ac8d72012-09-27 23:20:10 -07001516 if (!mUsers.valueAt(i).partial) {
1517 num++;
1518 }
1519 }
Amith Yamasani16389312012-10-17 21:20:14 -07001520 final int[] newUsers = new int[num];
1521 int n = 0;
Dianne Hackbornd4ac8d72012-09-27 23:20:10 -07001522 for (int i = 0; i < mUsers.size(); i++) {
1523 if (!mUsers.valueAt(i).partial) {
Amith Yamasani16389312012-10-17 21:20:14 -07001524 newUsers[n++] = mUsers.keyAt(i);
Dianne Hackbornd4ac8d72012-09-27 23:20:10 -07001525 }
Amith Yamasani0b285492011-04-14 17:35:23 -07001526 }
Dianne Hackborn7767eac2012-08-23 18:25:40 -07001527 mUserIds = newUsers;
Amith Yamasani0b285492011-04-14 17:35:23 -07001528 }
1529
1530 /**
Amith Yamasani1a7472e2013-07-02 11:17:30 -07001531 * Make a note of the last started time of a user and do some cleanup.
Amith Yamasani920ace02012-09-20 22:15:37 -07001532 * @param userId the user that was just foregrounded
1533 */
1534 public void userForeground(int userId) {
1535 synchronized (mPackagesLock) {
1536 UserInfo user = mUsers.get(userId);
1537 long now = System.currentTimeMillis();
Dianne Hackbornd4ac8d72012-09-27 23:20:10 -07001538 if (user == null || user.partial) {
1539 Slog.w(LOG_TAG, "userForeground: unknown user #" + userId);
1540 return;
1541 }
1542 if (now > EPOCH_PLUS_30_YEARS) {
Amith Yamasani920ace02012-09-20 22:15:37 -07001543 user.lastLoggedInTime = now;
1544 writeUserLocked(user);
1545 }
Amith Yamasani1a7472e2013-07-02 11:17:30 -07001546 // If this is not a restricted profile and there is no restrictions pin, clean up
1547 // all restrictions files that might have been left behind, else clean up just the
1548 // ones with uninstalled packages
1549 RestrictionsPinState pinState = mRestrictionsPinStates.get(userId);
1550 final long salt = pinState == null ? 0 : pinState.salt;
1551 cleanAppRestrictions(userId, (!user.isRestricted() && salt == 0));
Amith Yamasani920ace02012-09-20 22:15:37 -07001552 }
1553 }
1554
1555 /**
Amith Yamasani0b285492011-04-14 17:35:23 -07001556 * Returns the next available user id, filling in any holes in the ids.
Amith Yamasani742a6712011-05-04 14:49:28 -07001557 * TODO: May not be a good idea to recycle ids, in case it results in confusion
1558 * for data and battery stats collection, or unexpected cross-talk.
Amith Yamasani0b285492011-04-14 17:35:23 -07001559 * @return
1560 */
Dianne Hackborn5dc5a002012-09-15 19:33:48 -07001561 private int getNextAvailableIdLocked() {
Dianne Hackborn4428e172012-08-24 17:43:05 -07001562 synchronized (mPackagesLock) {
Jeff Sharkeyffe0cb42012-11-05 17:24:43 -08001563 int i = MIN_USER_ID;
Amith Yamasani195263742012-08-21 15:40:12 -07001564 while (i < Integer.MAX_VALUE) {
Jeff Sharkeyffe0cb42012-11-05 17:24:43 -08001565 if (mUsers.indexOfKey(i) < 0 && !mRemovingUserIds.get(i)) {
Amith Yamasani195263742012-08-21 15:40:12 -07001566 break;
1567 }
1568 i++;
Amith Yamasani4b2e9342011-03-31 12:38:53 -07001569 }
Amith Yamasani195263742012-08-21 15:40:12 -07001570 return i;
Amith Yamasani4b2e9342011-03-31 12:38:53 -07001571 }
Amith Yamasani4b2e9342011-03-31 12:38:53 -07001572 }
Amith Yamasani920ace02012-09-20 22:15:37 -07001573
Amith Yamasanifc95e702013-09-26 13:20:17 -07001574 private String packageToRestrictionsFileName(String packageName) {
1575 return RESTRICTIONS_FILE_PREFIX + packageName + XML_SUFFIX;
1576 }
1577
1578 private String restrictionsFileNameToPackage(String fileName) {
1579 return fileName.substring(RESTRICTIONS_FILE_PREFIX.length(),
1580 (int) (fileName.length() - XML_SUFFIX.length()));
1581 }
1582
Amith Yamasani920ace02012-09-20 22:15:37 -07001583 @Override
1584 protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
1585 if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.DUMP)
1586 != PackageManager.PERMISSION_GRANTED) {
1587 pw.println("Permission Denial: can't dump UserManager from from pid="
1588 + Binder.getCallingPid()
1589 + ", uid=" + Binder.getCallingUid()
1590 + " without permission "
1591 + android.Manifest.permission.DUMP);
1592 return;
1593 }
1594
1595 long now = System.currentTimeMillis();
1596 StringBuilder sb = new StringBuilder();
1597 synchronized (mPackagesLock) {
1598 pw.println("Users:");
1599 for (int i = 0; i < mUsers.size(); i++) {
1600 UserInfo user = mUsers.valueAt(i);
1601 if (user == null) continue;
Amith Yamasani634cf312012-10-04 17:34:21 -07001602 pw.print(" "); pw.print(user); pw.print(" serialNo="); pw.print(user.serialNumber);
Jeff Sharkeyffe0cb42012-11-05 17:24:43 -08001603 if (mRemovingUserIds.get(mUsers.keyAt(i))) pw.print(" <removing> ");
Dianne Hackbornd4ac8d72012-09-27 23:20:10 -07001604 if (user.partial) pw.print(" <partial>");
1605 pw.println();
Amith Yamasani920ace02012-09-20 22:15:37 -07001606 pw.print(" Created: ");
1607 if (user.creationTime == 0) {
1608 pw.println("<unknown>");
1609 } else {
1610 sb.setLength(0);
1611 TimeUtils.formatDuration(now - user.creationTime, sb);
1612 sb.append(" ago");
1613 pw.println(sb);
1614 }
1615 pw.print(" Last logged in: ");
1616 if (user.lastLoggedInTime == 0) {
1617 pw.println("<unknown>");
1618 } else {
1619 sb.setLength(0);
1620 TimeUtils.formatDuration(now - user.lastLoggedInTime, sb);
1621 sb.append(" ago");
1622 pw.println(sb);
1623 }
1624 }
1625 }
1626 }
Amith Yamasani1a7472e2013-07-02 11:17:30 -07001627
1628 private PackageMonitor mUserPackageMonitor = new PackageMonitor() {
1629 @Override
1630 public void onPackageRemoved(String pkg, int uid) {
1631 final int userId = this.getChangingUserId();
1632 // Package could be disappearing because it is being blocked, so also check if
1633 // it has been uninstalled.
1634 final boolean uninstalled = isPackageDisappearing(pkg) == PACKAGE_PERMANENT_CHANGE;
1635 if (uninstalled && userId >= 0 && !isPackageInstalled(pkg, userId)) {
1636 cleanAppRestrictionsForPackage(pkg, userId);
1637 }
1638 }
1639 };
Amith Yamasani4b2e9342011-03-31 12:38:53 -07001640}