blob: 4300df622d5d1d661a970b9173d329ec9735c7ce [file] [log] [blame]
Amith Yamasani4b2e9342011-03-31 12:38:53 -07001/*
2 * Copyright (C) 2011 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.android.server.pm;
18
Amith Yamasanidb6a14c2012-10-17 21:16:52 -070019import android.app.Activity;
Amith Yamasani2a003292012-08-14 18:25:45 -070020import android.app.ActivityManager;
Dianne Hackborn80a4af22012-08-27 19:18:31 -070021import android.app.ActivityManagerNative;
22import android.app.IStopUserCallback;
Amith Yamasanidb6a14c2012-10-17 21:16:52 -070023import android.content.BroadcastReceiver;
Amith Yamasani258848d2012-08-10 17:06:33 -070024import android.content.Context;
25import android.content.Intent;
Amith Yamasani1a7472e2013-07-02 11:17:30 -070026import android.content.pm.ApplicationInfo;
Amith Yamasani0b285492011-04-14 17:35:23 -070027import android.content.pm.PackageManager;
Amith Yamasanidf2e92a2013-03-01 17:04:38 -080028import android.content.pm.PackageManager.NameNotFoundException;
Amith Yamasani4b2e9342011-03-31 12:38:53 -070029import android.content.pm.UserInfo;
Amith Yamasanie928d7d2012-09-17 21:46:51 -070030import android.graphics.Bitmap;
Amith Yamasani258848d2012-08-10 17:06:33 -070031import android.os.Binder;
Amith Yamasanie4cf7342012-12-17 11:12:09 -080032import android.os.Bundle;
Amith Yamasani4b2e9342011-03-31 12:38:53 -070033import android.os.Environment;
34import android.os.FileUtils;
Jeff Sharkeyffe0cb42012-11-05 17:24:43 -080035import android.os.Handler;
Amith Yamasani258848d2012-08-10 17:06:33 -070036import android.os.IUserManager;
Fyodor Kupolov75d0ea82014-12-15 16:21:07 -080037import android.os.Message;
Adrian Roos1bdff912015-02-17 15:51:35 +010038import android.os.ParcelFileDescriptor;
Fyodor Kupolov262f9952015-03-23 18:55:11 -070039import android.os.Parcelable;
Amith Yamasani258848d2012-08-10 17:06:33 -070040import android.os.Process;
Dianne Hackborn80a4af22012-08-27 19:18:31 -070041import android.os.RemoteException;
Jason Monk62062992014-05-06 09:55:28 -040042import android.os.ServiceManager;
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
Fyodor Kupolovb5013302015-04-17 17:59:14 -070053import com.google.android.collect.Sets;
54
Fyodor Kupolov262f9952015-03-23 18:55:11 -070055import com.android.internal.annotations.VisibleForTesting;
Jason Monk62062992014-05-06 09:55:28 -040056import com.android.internal.app.IAppOpsService;
Jeff Sharkeyffe0cb42012-11-05 17:24:43 -080057import com.android.internal.util.ArrayUtils;
58import com.android.internal.util.FastXmlSerializer;
Fyodor Kupolov262f9952015-03-23 18:55:11 -070059import com.android.internal.util.XmlUtils;
Jeff Sharkeyffe0cb42012-11-05 17:24:43 -080060
61import org.xmlpull.v1.XmlPullParser;
62import org.xmlpull.v1.XmlPullParserException;
63import org.xmlpull.v1.XmlSerializer;
64
Amith Yamasani4b2e9342011-03-31 12:38:53 -070065import java.io.BufferedOutputStream;
66import java.io.File;
Amith Yamasani920ace02012-09-20 22:15:37 -070067import java.io.FileDescriptor;
Amith Yamasani4b2e9342011-03-31 12:38:53 -070068import java.io.FileInputStream;
Amith Yamasanib8151ec2012-04-18 18:02:48 -070069import java.io.FileNotFoundException;
Amith Yamasani4b2e9342011-03-31 12:38:53 -070070import java.io.FileOutputStream;
71import java.io.IOException;
Amith Yamasani920ace02012-09-20 22:15:37 -070072import java.io.PrintWriter;
Wojciech Staszkiewicz9e9e2e72015-05-08 14:58:46 +010073import java.nio.charset.StandardCharsets;
Amith Yamasani4b2e9342011-03-31 12:38:53 -070074import java.util.ArrayList;
75import java.util.List;
Fyodor Kupolovb5013302015-04-17 17:59:14 -070076import java.util.Set;
Amith Yamasani4b2e9342011-03-31 12:38:53 -070077
Fyodor Kupolov262f9952015-03-23 18:55:11 -070078import libcore.io.IoUtils;
79
Amith Yamasani258848d2012-08-10 17:06:33 -070080public class UserManagerService extends IUserManager.Stub {
Amith Yamasanib8151ec2012-04-18 18:02:48 -070081
Amith Yamasani2a003292012-08-14 18:25:45 -070082 private static final String LOG_TAG = "UserManagerService";
Amith Yamasanib8151ec2012-04-18 18:02:48 -070083
Amith Yamasani16389312012-10-17 21:20:14 -070084 private static final boolean DBG = false;
85
Amith Yamasani4b2e9342011-03-31 12:38:53 -070086 private static final String TAG_NAME = "name";
Amith Yamasani4b2e9342011-03-31 12:38:53 -070087 private static final String ATTR_FLAGS = "flags";
Amith Yamasanib8151ec2012-04-18 18:02:48 -070088 private static final String ATTR_ICON_PATH = "icon";
Amith Yamasani4b2e9342011-03-31 12:38:53 -070089 private static final String ATTR_ID = "id";
Amith Yamasani920ace02012-09-20 22:15:37 -070090 private static final String ATTR_CREATION_TIME = "created";
91 private static final String ATTR_LAST_LOGGED_IN_TIME = "lastLoggedIn";
Amith Yamasani2a003292012-08-14 18:25:45 -070092 private static final String ATTR_SERIAL_NO = "serialNumber";
93 private static final String ATTR_NEXT_SERIAL_NO = "nextSerialNumber";
Dianne Hackbornd4ac8d72012-09-27 23:20:10 -070094 private static final String ATTR_PARTIAL = "partial";
Adam Lesinskieddeb492014-09-08 17:50:03 -070095 private static final String ATTR_GUEST_TO_REMOVE = "guestToRemove";
Amith Yamasani6f34b412012-10-22 18:19:27 -070096 private static final String ATTR_USER_VERSION = "version";
Kenny Guy2a764942014-04-02 13:29:20 +010097 private static final String ATTR_PROFILE_GROUP_ID = "profileGroupId";
Amith Yamasanie4afaa32014-06-30 14:55:07 +053098 private static final String TAG_GUEST_RESTRICTIONS = "guestRestrictions";
Amith Yamasani4b2e9342011-03-31 12:38:53 -070099 private static final String TAG_USERS = "users";
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700100 private static final String TAG_USER = "user";
Amith Yamasanie4cf7342012-12-17 11:12:09 -0800101 private static final String TAG_RESTRICTIONS = "restrictions";
Amith Yamasanidf2e92a2013-03-01 17:04:38 -0800102 private static final String TAG_ENTRY = "entry";
103 private static final String TAG_VALUE = "value";
104 private static final String ATTR_KEY = "key";
Amith Yamasani7e99bc02013-04-16 18:24:51 -0700105 private static final String ATTR_VALUE_TYPE = "type";
Amith Yamasanidf2e92a2013-03-01 17:04:38 -0800106 private static final String ATTR_MULTIPLE = "m";
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700107
Amith Yamasani7e99bc02013-04-16 18:24:51 -0700108 private static final String ATTR_TYPE_STRING_ARRAY = "sa";
109 private static final String ATTR_TYPE_STRING = "s";
110 private static final String ATTR_TYPE_BOOLEAN = "b";
Amith Yamasani5b5aa402014-06-01 20:10:14 -0700111 private static final String ATTR_TYPE_INTEGER = "i";
Fyodor Kupolov262f9952015-03-23 18:55:11 -0700112 private static final String ATTR_TYPE_BUNDLE = "B";
113 private static final String ATTR_TYPE_BUNDLE_ARRAY = "BA";
Amith Yamasani7e99bc02013-04-16 18:24:51 -0700114
Amith Yamasani0b285492011-04-14 17:35:23 -0700115 private static final String USER_INFO_DIR = "system" + File.separator + "users";
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700116 private static final String USER_LIST_FILENAME = "userlist.xml";
Amith Yamasanib8151ec2012-04-18 18:02:48 -0700117 private static final String USER_PHOTO_FILENAME = "photo.png";
Adrian Roos1bdff912015-02-17 15:51:35 +0100118 private static final String USER_PHOTO_FILENAME_TMP = USER_PHOTO_FILENAME + ".tmp";
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700119
Amith Yamasanidf2e92a2013-03-01 17:04:38 -0800120 private static final String RESTRICTIONS_FILE_PREFIX = "res_";
Amith Yamasanifc95e702013-09-26 13:20:17 -0700121 private static final String XML_SUFFIX = ".xml";
Amith Yamasanidf2e92a2013-03-01 17:04:38 -0800122
Amith Yamasani634cf312012-10-04 17:34:21 -0700123 private static final int MIN_USER_ID = 10;
124
Amith Yamasaniaa6634e2014-10-06 14:20:28 -0700125 private static final int USER_VERSION = 5;
Amith Yamasani6f34b412012-10-22 18:19:27 -0700126
Amith Yamasani920ace02012-09-20 22:15:37 -0700127 private static final long EPOCH_PLUS_30_YEARS = 30L * 365 * 24 * 60 * 60 * 1000L; // ms
128
Amith Yamasani95ab7842014-08-11 17:09:26 -0700129 // Maximum number of managed profiles permitted is 1. This cannot be increased
130 // without first making sure that the rest of the framework is prepared for it.
131 private static final int MAX_MANAGED_PROFILES = 1;
132
Fyodor Kupolovb5013302015-04-17 17:59:14 -0700133 // Set of user restrictions, which can only be enforced by the system
134 private static final Set<String> SYSTEM_CONTROLLED_RESTRICTIONS = Sets.newArraySet(
135 UserManager.DISALLOW_RECORD_AUDIO);
136
Fyodor Kupolov75d0ea82014-12-15 16:21:07 -0800137 static final int WRITE_USER_MSG = 1;
138 static final int WRITE_USER_DELAY = 2*1000; // 2 seconds
Amith Yamasanie4afaa32014-06-30 14:55:07 +0530139
Dianne Hackborn4428e172012-08-24 17:43:05 -0700140 private final Context mContext;
141 private final PackageManagerService mPm;
142 private final Object mInstallLock;
143 private final Object mPackagesLock;
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700144
Jeff Sharkeyffe0cb42012-11-05 17:24:43 -0800145 private final Handler mHandler;
146
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700147 private final File mUsersDir;
148 private final File mUserListFile;
Dianne Hackborn4428e172012-08-24 17:43:05 -0700149 private final File mBaseUserPath;
150
Jeff Sharkeyffe0cb42012-11-05 17:24:43 -0800151 private final SparseArray<UserInfo> mUsers = new SparseArray<UserInfo>();
Amith Yamasanie4cf7342012-12-17 11:12:09 -0800152 private final SparseArray<Bundle> mUserRestrictions = new SparseArray<Bundle>();
Amith Yamasanie4afaa32014-06-30 14:55:07 +0530153 private final Bundle mGuestRestrictions = new Bundle();
Jeff Sharkeyffe0cb42012-11-05 17:24:43 -0800154
155 /**
156 * Set of user IDs being actively removed. Removed IDs linger in this set
157 * for several seconds to work around a VFS caching issue.
158 */
159 // @GuardedBy("mPackagesLock")
160 private final SparseBooleanArray mRemovingUserIds = new SparseBooleanArray();
Dianne Hackborn4428e172012-08-24 17:43:05 -0700161
Amith Yamasani0b285492011-04-14 17:35:23 -0700162 private int[] mUserIds;
Amith Yamasani2a003292012-08-14 18:25:45 -0700163 private int mNextSerialNumber;
Amith Yamasani6f34b412012-10-22 18:19:27 -0700164 private int mUserVersion = 0;
Amith Yamasani0b285492011-04-14 17:35:23 -0700165
Jason Monk62062992014-05-06 09:55:28 -0400166 private IAppOpsService mAppOpsService;
167
Amith Yamasani258848d2012-08-10 17:06:33 -0700168 private static UserManagerService sInstance;
Amith Yamasani258848d2012-08-10 17:06:33 -0700169
Dianne Hackborn4428e172012-08-24 17:43:05 -0700170 public static UserManagerService getInstance() {
171 synchronized (UserManagerService.class) {
172 return sInstance;
Amith Yamasani258848d2012-08-10 17:06:33 -0700173 }
Amith Yamasani258848d2012-08-10 17:06:33 -0700174 }
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700175
176 /**
177 * Available for testing purposes.
178 */
Amith Yamasani258848d2012-08-10 17:06:33 -0700179 UserManagerService(File dataDir, File baseUserPath) {
Dianne Hackborn4428e172012-08-24 17:43:05 -0700180 this(null, null, new Object(), new Object(), dataDir, baseUserPath);
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700181 }
182
Dianne Hackborn4428e172012-08-24 17:43:05 -0700183 /**
184 * Called by package manager to create the service. This is closely
185 * associated with the package manager, and the given lock is the
186 * package manager's own lock.
187 */
188 UserManagerService(Context context, PackageManagerService pm,
189 Object installLock, Object packagesLock) {
190 this(context, pm, installLock, packagesLock,
191 Environment.getDataDirectory(),
192 new File(Environment.getDataDirectory(), "user"));
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700193 }
194
Dianne Hackborn4428e172012-08-24 17:43:05 -0700195 /**
196 * Available for testing purposes.
197 */
198 private UserManagerService(Context context, PackageManagerService pm,
199 Object installLock, Object packagesLock,
200 File dataDir, File baseUserPath) {
Dianne Hackbornd4ac8d72012-09-27 23:20:10 -0700201 mContext = context;
202 mPm = pm;
203 mInstallLock = installLock;
204 mPackagesLock = packagesLock;
Fyodor Kupolov75d0ea82014-12-15 16:21:07 -0800205 mHandler = new MainHandler();
Dianne Hackbornd4ac8d72012-09-27 23:20:10 -0700206 synchronized (mInstallLock) {
207 synchronized (mPackagesLock) {
208 mUsersDir = new File(dataDir, USER_INFO_DIR);
209 mUsersDir.mkdirs();
210 // Make zeroth user directory, for services to migrate their files to that location
211 File userZeroDir = new File(mUsersDir, "0");
212 userZeroDir.mkdirs();
213 mBaseUserPath = baseUserPath;
214 FileUtils.setPermissions(mUsersDir.toString(),
215 FileUtils.S_IRWXU|FileUtils.S_IRWXG
216 |FileUtils.S_IROTH|FileUtils.S_IXOTH,
217 -1, -1);
218 mUserListFile = new File(mUsersDir, USER_LIST_FILENAME);
Amith Yamasanida0b1682014-11-21 12:58:17 -0800219 initDefaultGuestRestrictions();
Dianne Hackbornd4ac8d72012-09-27 23:20:10 -0700220 readUserListLocked();
Xiaohui Chen4be96e42015-05-06 09:55:43 -0700221 sInstance = this;
222 }
223 }
224 }
225
226 void systemReady() {
227 synchronized (mInstallLock) {
228 synchronized (mPackagesLock) {
Amith Yamasani756901d2012-10-12 12:30:07 -0700229 // Prune out any partially created/partially removed users.
Dianne Hackbornd4ac8d72012-09-27 23:20:10 -0700230 ArrayList<UserInfo> partials = new ArrayList<UserInfo>();
231 for (int i = 0; i < mUsers.size(); i++) {
232 UserInfo ui = mUsers.valueAt(i);
Adam Lesinskieddeb492014-09-08 17:50:03 -0700233 if ((ui.partial || ui.guestToRemove) && i != 0) {
Dianne Hackbornd4ac8d72012-09-27 23:20:10 -0700234 partials.add(ui);
235 }
236 }
237 for (int i = 0; i < partials.size(); i++) {
238 UserInfo ui = partials.get(i);
239 Slog.w(LOG_TAG, "Removing partially created user #" + i
240 + " (name=" + ui.name + ")");
241 removeUserStateLocked(ui.id);
242 }
Dianne Hackbornd4ac8d72012-09-27 23:20:10 -0700243 }
Dianne Hackborn4428e172012-08-24 17:43:05 -0700244 }
Amith Yamasani06bf8242015-05-08 16:36:21 -0700245 onUserForeground(UserHandle.USER_OWNER);
Jason Monk62062992014-05-06 09:55:28 -0400246 mAppOpsService = IAppOpsService.Stub.asInterface(
247 ServiceManager.getService(Context.APP_OPS_SERVICE));
248 for (int i = 0; i < mUserIds.length; ++i) {
249 try {
250 mAppOpsService.setUserRestrictions(mUserRestrictions.get(mUserIds[i]), mUserIds[i]);
251 } catch (RemoteException e) {
252 Log.w(LOG_TAG, "Unable to notify AppOpsService of UserRestrictions");
253 }
254 }
Amith Yamasani258848d2012-08-10 17:06:33 -0700255 }
256
257 @Override
Amith Yamasani920ace02012-09-20 22:15:37 -0700258 public List<UserInfo> getUsers(boolean excludeDying) {
Amith Yamasani2a003292012-08-14 18:25:45 -0700259 checkManageUsersPermission("query users");
Dianne Hackborn4428e172012-08-24 17:43:05 -0700260 synchronized (mPackagesLock) {
Amith Yamasani13593602012-03-22 16:16:17 -0700261 ArrayList<UserInfo> users = new ArrayList<UserInfo>(mUsers.size());
262 for (int i = 0; i < mUsers.size(); i++) {
Dianne Hackbornd4ac8d72012-09-27 23:20:10 -0700263 UserInfo ui = mUsers.valueAt(i);
264 if (ui.partial) {
265 continue;
266 }
Jeff Sharkeyffe0cb42012-11-05 17:24:43 -0800267 if (!excludeDying || !mRemovingUserIds.get(ui.id)) {
Dianne Hackbornd4ac8d72012-09-27 23:20:10 -0700268 users.add(ui);
Amith Yamasani920ace02012-09-20 22:15:37 -0700269 }
Amith Yamasani13593602012-03-22 16:16:17 -0700270 }
271 return users;
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700272 }
Amith Yamasani13593602012-03-22 16:16:17 -0700273 }
274
Amith Yamasani258848d2012-08-10 17:06:33 -0700275 @Override
Alexandra Gherghina385124d2014-04-03 13:37:39 +0100276 public List<UserInfo> getProfiles(int userId, boolean enabledOnly) {
Amith Yamasani4f582632014-02-19 14:31:52 -0800277 if (userId != UserHandle.getCallingUserId()) {
278 checkManageUsersPermission("getting profiles related to user " + userId);
279 }
Amith Yamasanibe465322014-04-24 13:45:17 -0700280 final long ident = Binder.clearCallingIdentity();
281 try {
282 synchronized (mPackagesLock) {
283 return getProfilesLocked(userId, enabledOnly);
Alexandra Gherghina385124d2014-04-03 13:37:39 +0100284 }
Amith Yamasanibe465322014-04-24 13:45:17 -0700285 } finally {
286 Binder.restoreCallingIdentity(ident);
Kenny Guya52dc3e2014-02-11 15:33:14 +0000287 }
288 }
289
Amith Yamasanibe465322014-04-24 13:45:17 -0700290 /** Assume permissions already checked and caller's identity cleared */
291 private List<UserInfo> getProfilesLocked(int userId, boolean enabledOnly) {
Amith Yamasanibe465322014-04-24 13:45:17 -0700292 UserInfo user = getUserInfoLocked(userId);
293 ArrayList<UserInfo> users = new ArrayList<UserInfo>(mUsers.size());
Amith Yamasanidda003f2014-08-28 18:06:51 -0700294 if (user == null) {
295 // Probably a dying user
296 return users;
297 }
Amith Yamasanibe465322014-04-24 13:45:17 -0700298 for (int i = 0; i < mUsers.size(); i++) {
299 UserInfo profile = mUsers.valueAt(i);
300 if (!isProfileOf(user, profile)) {
301 continue;
302 }
Alexandra Gherghinadf35d572014-04-09 13:54:39 +0100303 if (enabledOnly && !profile.isEnabled()) {
304 continue;
Amith Yamasanibe465322014-04-24 13:45:17 -0700305 }
Amith Yamasani70fcf0c2014-07-11 08:40:19 -0700306 if (mRemovingUserIds.get(profile.id)) {
307 continue;
308 }
Amith Yamasanibe465322014-04-24 13:45:17 -0700309 users.add(profile);
310 }
311 return users;
312 }
313
Jessica Hummelbe81c802014-04-22 15:49:22 +0100314 @Override
315 public UserInfo getProfileParent(int userHandle) {
316 checkManageUsersPermission("get the profile parent");
317 synchronized (mPackagesLock) {
Fyodor Kupolovff7233e2015-04-08 11:28:52 -0700318 return getProfileParentLocked(userHandle);
319 }
320 }
321
322 private UserInfo getProfileParentLocked(int userHandle) {
323 UserInfo profile = getUserInfoLocked(userHandle);
324 if (profile == null) {
325 return null;
326 }
327 int parentUserId = profile.profileGroupId;
328 if (parentUserId == UserInfo.NO_PROFILE_GROUP_ID) {
329 return null;
330 } else {
331 return getUserInfoLocked(parentUserId);
Jessica Hummelbe81c802014-04-22 15:49:22 +0100332 }
333 }
334
Kenny Guy2a764942014-04-02 13:29:20 +0100335 private boolean isProfileOf(UserInfo user, UserInfo profile) {
336 return user.id == profile.id ||
337 (user.profileGroupId != UserInfo.NO_PROFILE_GROUP_ID
338 && user.profileGroupId == profile.profileGroupId);
Kenny Guya52dc3e2014-02-11 15:33:14 +0000339 }
340
341 @Override
Alexandra Gherghinadf35d572014-04-09 13:54:39 +0100342 public void setUserEnabled(int userId) {
343 checkManageUsersPermission("enable user");
344 synchronized (mPackagesLock) {
345 UserInfo info = getUserInfoLocked(userId);
346 if (info != null && !info.isEnabled()) {
347 info.flags ^= UserInfo.FLAG_DISABLED;
348 writeUserLocked(info);
349 }
350 }
351 }
352
353 @Override
Amith Yamasani258848d2012-08-10 17:06:33 -0700354 public UserInfo getUserInfo(int userId) {
Amith Yamasani2a003292012-08-14 18:25:45 -0700355 checkManageUsersPermission("query user");
Dianne Hackborn4428e172012-08-24 17:43:05 -0700356 synchronized (mPackagesLock) {
Amith Yamasani195263742012-08-21 15:40:12 -0700357 return getUserInfoLocked(userId);
Amith Yamasani13593602012-03-22 16:16:17 -0700358 }
359 }
360
Amith Yamasani71e6c692013-03-24 17:39:28 -0700361 @Override
362 public boolean isRestricted() {
363 synchronized (mPackagesLock) {
364 return getUserInfoLocked(UserHandle.getCallingUserId()).isRestricted();
365 }
366 }
367
Amith Yamasani195263742012-08-21 15:40:12 -0700368 /*
369 * Should be locked on mUsers before calling this.
370 */
371 private UserInfo getUserInfoLocked(int userId) {
Dianne Hackbornd4ac8d72012-09-27 23:20:10 -0700372 UserInfo ui = mUsers.get(userId);
Amith Yamasani16389312012-10-17 21:20:14 -0700373 // If it is partial and not in the process of being removed, return as unknown user.
Jeff Sharkeyffe0cb42012-11-05 17:24:43 -0800374 if (ui != null && ui.partial && !mRemovingUserIds.get(userId)) {
Dianne Hackbornd4ac8d72012-09-27 23:20:10 -0700375 Slog.w(LOG_TAG, "getUserInfo: unknown user #" + userId);
376 return null;
377 }
378 return ui;
Amith Yamasani195263742012-08-21 15:40:12 -0700379 }
380
Amith Yamasani13593602012-03-22 16:16:17 -0700381 public boolean exists(int userId) {
Dianne Hackborn4428e172012-08-24 17:43:05 -0700382 synchronized (mPackagesLock) {
Amith Yamasani13593602012-03-22 16:16:17 -0700383 return ArrayUtils.contains(mUserIds, userId);
384 }
385 }
386
Amith Yamasani258848d2012-08-10 17:06:33 -0700387 @Override
Amith Yamasanib8151ec2012-04-18 18:02:48 -0700388 public void setUserName(int userId, String name) {
Amith Yamasani2a003292012-08-14 18:25:45 -0700389 checkManageUsersPermission("rename users");
Dianne Hackbornd4ac8d72012-09-27 23:20:10 -0700390 boolean changed = false;
Dianne Hackborn4428e172012-08-24 17:43:05 -0700391 synchronized (mPackagesLock) {
Amith Yamasani13593602012-03-22 16:16:17 -0700392 UserInfo info = mUsers.get(userId);
Dianne Hackbornd4ac8d72012-09-27 23:20:10 -0700393 if (info == null || info.partial) {
394 Slog.w(LOG_TAG, "setUserName: unknown user #" + userId);
395 return;
396 }
Amith Yamasani13593602012-03-22 16:16:17 -0700397 if (name != null && !name.equals(info.name)) {
398 info.name = name;
399 writeUserLocked(info);
Dianne Hackbornd4ac8d72012-09-27 23:20:10 -0700400 changed = true;
Amith Yamasani13593602012-03-22 16:16:17 -0700401 }
402 }
Dianne Hackbornd4ac8d72012-09-27 23:20:10 -0700403 if (changed) {
404 sendUserInfoChangedBroadcast(userId);
405 }
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700406 }
407
Amith Yamasani258848d2012-08-10 17:06:33 -0700408 @Override
Amith Yamasanie928d7d2012-09-17 21:46:51 -0700409 public void setUserIcon(int userId, Bitmap bitmap) {
Amith Yamasani2a003292012-08-14 18:25:45 -0700410 checkManageUsersPermission("update users");
Jason Monk9a944532014-07-08 09:31:21 -0400411 long ident = Binder.clearCallingIdentity();
412 try {
413 synchronized (mPackagesLock) {
414 UserInfo info = mUsers.get(userId);
415 if (info == null || info.partial) {
416 Slog.w(LOG_TAG, "setUserIcon: unknown user #" + userId);
417 return;
418 }
419 writeBitmapLocked(info, bitmap);
420 writeUserLocked(info);
Dianne Hackbornd4ac8d72012-09-27 23:20:10 -0700421 }
Jason Monk9a944532014-07-08 09:31:21 -0400422 sendUserInfoChangedBroadcast(userId);
423 } finally {
424 Binder.restoreCallingIdentity(ident);
Amith Yamasanib8151ec2012-04-18 18:02:48 -0700425 }
Amith Yamasanie928d7d2012-09-17 21:46:51 -0700426 }
427
428 private void sendUserInfoChangedBroadcast(int userId) {
429 Intent changedIntent = new Intent(Intent.ACTION_USER_INFO_CHANGED);
430 changedIntent.putExtra(Intent.EXTRA_USER_HANDLE, userId);
431 changedIntent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY);
Amith Yamasani6fc1d4e2013-05-08 16:43:58 -0700432 mContext.sendBroadcastAsUser(changedIntent, UserHandle.ALL);
Amith Yamasanib8151ec2012-04-18 18:02:48 -0700433 }
434
Amith Yamasani258848d2012-08-10 17:06:33 -0700435 @Override
Adrian Roos1bdff912015-02-17 15:51:35 +0100436 public ParcelFileDescriptor getUserIcon(int userId) {
437 String iconPath;
Amith Yamasani3b49f072012-09-17 10:21:43 -0700438 synchronized (mPackagesLock) {
439 UserInfo info = mUsers.get(userId);
Dianne Hackbornd4ac8d72012-09-27 23:20:10 -0700440 if (info == null || info.partial) {
441 Slog.w(LOG_TAG, "getUserIcon: unknown user #" + userId);
442 return null;
443 }
Nicolas Prevot88cc3462014-05-14 14:51:48 +0100444 int callingGroupId = mUsers.get(UserHandle.getCallingUserId()).profileGroupId;
445 if (callingGroupId == UserInfo.NO_PROFILE_GROUP_ID
446 || callingGroupId != info.profileGroupId) {
447 checkManageUsersPermission("get the icon of a user who is not related");
448 }
Dianne Hackbornd4ac8d72012-09-27 23:20:10 -0700449 if (info.iconPath == null) {
450 return null;
451 }
Adrian Roos1bdff912015-02-17 15:51:35 +0100452 iconPath = info.iconPath;
Amith Yamasani3b49f072012-09-17 10:21:43 -0700453 }
Adrian Roos1bdff912015-02-17 15:51:35 +0100454
455 try {
456 return ParcelFileDescriptor.open(
457 new File(iconPath), ParcelFileDescriptor.MODE_READ_ONLY);
458 } catch (FileNotFoundException e) {
459 Log.e(LOG_TAG, "Couldn't find icon file", e);
460 }
461 return null;
Amith Yamasani3b49f072012-09-17 10:21:43 -0700462 }
463
Dianne Hackborn5dc5a002012-09-15 19:33:48 -0700464 public void makeInitialized(int userId) {
465 checkManageUsersPermission("makeInitialized");
466 synchronized (mPackagesLock) {
467 UserInfo info = mUsers.get(userId);
Dianne Hackbornd4ac8d72012-09-27 23:20:10 -0700468 if (info == null || info.partial) {
469 Slog.w(LOG_TAG, "makeInitialized: unknown user #" + userId);
470 }
471 if ((info.flags&UserInfo.FLAG_INITIALIZED) == 0) {
Dianne Hackborn5dc5a002012-09-15 19:33:48 -0700472 info.flags |= UserInfo.FLAG_INITIALIZED;
Fyodor Kupolov75d0ea82014-12-15 16:21:07 -0800473 scheduleWriteUserLocked(info);
Dianne Hackborn5dc5a002012-09-15 19:33:48 -0700474 }
475 }
476 }
477
Amith Yamasaniaa6634e2014-10-06 14:20:28 -0700478 /**
479 * If default guest restrictions haven't been initialized yet, add the basic
480 * restrictions.
481 */
482 private void initDefaultGuestRestrictions() {
483 if (mGuestRestrictions.isEmpty()) {
484 mGuestRestrictions.putBoolean(UserManager.DISALLOW_OUTGOING_CALLS, true);
Amith Yamasanida0b1682014-11-21 12:58:17 -0800485 mGuestRestrictions.putBoolean(UserManager.DISALLOW_SMS, true);
Amith Yamasaniaa6634e2014-10-06 14:20:28 -0700486 }
487 }
488
Amith Yamasanie4cf7342012-12-17 11:12:09 -0800489 @Override
Amith Yamasanie4afaa32014-06-30 14:55:07 +0530490 public Bundle getDefaultGuestRestrictions() {
491 checkManageUsersPermission("getDefaultGuestRestrictions");
492 synchronized (mPackagesLock) {
493 return new Bundle(mGuestRestrictions);
494 }
495 }
496
497 @Override
498 public void setDefaultGuestRestrictions(Bundle restrictions) {
499 checkManageUsersPermission("setDefaultGuestRestrictions");
500 synchronized (mPackagesLock) {
501 mGuestRestrictions.clear();
502 mGuestRestrictions.putAll(restrictions);
503 writeUserListLocked();
504 }
505 }
506
507 @Override
Amith Yamasani8cd28b52014-06-08 17:54:27 -0700508 public boolean hasUserRestriction(String restrictionKey, int userId) {
509 synchronized (mPackagesLock) {
510 Bundle restrictions = mUserRestrictions.get(userId);
Fyodor Kupolovb5013302015-04-17 17:59:14 -0700511 return restrictions != null && restrictions.getBoolean(restrictionKey);
Amith Yamasani8cd28b52014-06-08 17:54:27 -0700512 }
513 }
514
515 @Override
Amith Yamasanie4cf7342012-12-17 11:12:09 -0800516 public Bundle getUserRestrictions(int userId) {
517 // checkManageUsersPermission("getUserRestrictions");
518
519 synchronized (mPackagesLock) {
520 Bundle restrictions = mUserRestrictions.get(userId);
Amith Yamasanibe465322014-04-24 13:45:17 -0700521 return restrictions != null ? new Bundle(restrictions) : new Bundle();
Amith Yamasanie4cf7342012-12-17 11:12:09 -0800522 }
523 }
524
525 @Override
Fyodor Kupolovb5013302015-04-17 17:59:14 -0700526 public void setUserRestriction(String key, boolean value, int userId) {
527 synchronized (mPackagesLock) {
528 if (!SYSTEM_CONTROLLED_RESTRICTIONS.contains(key)) {
529 Bundle restrictions = getUserRestrictions(userId);
530 restrictions.putBoolean(key, value);
531 setUserRestrictionsInternalLocked(restrictions, userId);
532 }
533 }
534 }
535
536 @Override
537 public void setSystemControlledUserRestriction(String key, boolean value, int userId) {
538 checkSystemOrRoot("setSystemControlledUserRestriction");
539 synchronized (mPackagesLock) {
540 Bundle restrictions = getUserRestrictions(userId);
541 restrictions.putBoolean(key, value);
542 setUserRestrictionsInternalLocked(restrictions, userId);
543 }
544 }
545
546 @Override
Amith Yamasanie4cf7342012-12-17 11:12:09 -0800547 public void setUserRestrictions(Bundle restrictions, int userId) {
Amith Yamasanibe465322014-04-24 13:45:17 -0700548 checkManageUsersPermission("setUserRestrictions");
Amith Yamasani0343ec32013-07-22 14:52:06 -0700549 if (restrictions == null) return;
Amith Yamasanie4cf7342012-12-17 11:12:09 -0800550
551 synchronized (mPackagesLock) {
Fyodor Kupolovb5013302015-04-17 17:59:14 -0700552 final Bundle oldUserRestrictions = mUserRestrictions.get(userId);
553 // Restore the original state of system controlled restrictions from oldUserRestrictions
554 for (String key : SYSTEM_CONTROLLED_RESTRICTIONS) {
555 restrictions.remove(key);
556 if (oldUserRestrictions.containsKey(key)) {
557 restrictions.putBoolean(key, oldUserRestrictions.getBoolean(key));
558 }
Jason Monk62062992014-05-06 09:55:28 -0400559 }
Fyodor Kupolovb5013302015-04-17 17:59:14 -0700560 setUserRestrictionsInternalLocked(restrictions, userId);
Amith Yamasanie4cf7342012-12-17 11:12:09 -0800561 }
562 }
563
Fyodor Kupolovb5013302015-04-17 17:59:14 -0700564 private void setUserRestrictionsInternalLocked(Bundle restrictions, int userId) {
565 final Bundle userRestrictions = mUserRestrictions.get(userId);
566 userRestrictions.clear();
567 userRestrictions.putAll(restrictions);
568 long token = Binder.clearCallingIdentity();
569 try {
570 mAppOpsService.setUserRestrictions(userRestrictions, userId);
571 } catch (RemoteException e) {
572 Log.w(LOG_TAG, "Unable to notify AppOpsService of UserRestrictions");
573 } finally {
574 Binder.restoreCallingIdentity(token);
575 }
576 scheduleWriteUserLocked(mUsers.get(userId));
577 }
578
Amith Yamasani258848d2012-08-10 17:06:33 -0700579 /**
Amith Yamasanifaea76f2012-09-11 10:59:48 -0700580 * Check if we've hit the limit of how many users can be created.
581 */
Dianne Hackborn5dc5a002012-09-15 19:33:48 -0700582 private boolean isUserLimitReachedLocked() {
Nicolas Prevot72434b72015-05-13 12:15:03 -0700583 return getAliveUsersExcludingGuestsCountLocked() >= UserManager.getMaxSupportedUsers();
584 }
585
586 @Override
587 public boolean canAddMoreManagedProfiles() {
588 checkManageUsersPermission("check if more managed profiles can be added.");
589 if (ActivityManager.isLowRamDeviceStatic()) {
590 return false;
591 }
Fyodor Kupolovb6157992015-06-05 15:32:28 -0700592 if (!mContext.getPackageManager().hasSystemFeature(
593 PackageManager.FEATURE_MANAGED_USERS)) {
594 return false;
595 }
Nicolas Prevot72434b72015-05-13 12:15:03 -0700596 synchronized(mPackagesLock) {
597 // Limit number of managed profiles that can be created
598 if (numberOfUsersOfTypeLocked(UserInfo.FLAG_MANAGED_PROFILE, true)
599 >= MAX_MANAGED_PROFILES) {
600 return false;
601 }
602 int usersCount = getAliveUsersExcludingGuestsCountLocked();
603 // We allow creating a managed profile in the special case where there is only one user.
604 return usersCount == 1 || usersCount < UserManager.getMaxSupportedUsers();
605 }
606 }
607
608 private int getAliveUsersExcludingGuestsCountLocked() {
Amith Yamasanif584f012014-05-19 17:57:25 -0700609 int aliveUserCount = 0;
610 final int totalUserCount = mUsers.size();
611 // Skip over users being removed
612 for (int i = 0; i < totalUserCount; i++) {
613 UserInfo user = mUsers.valueAt(i);
Amith Yamasani95ab7842014-08-11 17:09:26 -0700614 if (!mRemovingUserIds.get(user.id)
Amith Yamasani1df14732014-08-29 21:37:27 -0700615 && !user.isGuest() && !user.partial) {
Amith Yamasanif584f012014-05-19 17:57:25 -0700616 aliveUserCount++;
617 }
618 }
Nicolas Prevot72434b72015-05-13 12:15:03 -0700619 return aliveUserCount;
Amith Yamasanifaea76f2012-09-11 10:59:48 -0700620 }
621
622 /**
Amith Yamasani195263742012-08-21 15:40:12 -0700623 * Enforces that only the system UID or root's UID or apps that have the
Dianne Hackborn10ad9822014-03-17 11:28:36 -0700624 * {@link android.Manifest.permission#MANAGE_USERS MANAGE_USERS}
Amith Yamasani195263742012-08-21 15:40:12 -0700625 * permission can make certain calls to the UserManager.
Amith Yamasani258848d2012-08-10 17:06:33 -0700626 *
627 * @param message used as message if SecurityException is thrown
628 * @throws SecurityException if the caller is not system or root
629 */
Amith Yamasanibe465322014-04-24 13:45:17 -0700630 private static final void checkManageUsersPermission(String message) {
Amith Yamasani258848d2012-08-10 17:06:33 -0700631 final int uid = Binder.getCallingUid();
Amith Yamasanibe465322014-04-24 13:45:17 -0700632 if (uid != Process.SYSTEM_UID && uid != 0
Emily Bernier7a2b4d12014-04-23 12:51:35 -0400633 && ActivityManager.checkComponentPermission(
634 android.Manifest.permission.MANAGE_USERS,
Amith Yamasanibe465322014-04-24 13:45:17 -0700635 uid, -1, true) != PackageManager.PERMISSION_GRANTED) {
636 throw new SecurityException("You need MANAGE_USERS permission to: " + message);
637 }
Emily Bernier7a2b4d12014-04-23 12:51:35 -0400638 }
639
Fyodor Kupolovb5013302015-04-17 17:59:14 -0700640 private static void checkSystemOrRoot(String message) {
641 final int uid = Binder.getCallingUid();
642 if (uid != Process.SYSTEM_UID && uid != 0) {
643 throw new SecurityException("Only system may call: " + message);
644 }
645 }
646
Amith Yamasanie928d7d2012-09-17 21:46:51 -0700647 private void writeBitmapLocked(UserInfo info, Bitmap bitmap) {
Amith Yamasanib8151ec2012-04-18 18:02:48 -0700648 try {
649 File dir = new File(mUsersDir, Integer.toString(info.id));
650 File file = new File(dir, USER_PHOTO_FILENAME);
Adrian Roos1bdff912015-02-17 15:51:35 +0100651 File tmp = new File(dir, USER_PHOTO_FILENAME_TMP);
Amith Yamasanib8151ec2012-04-18 18:02:48 -0700652 if (!dir.exists()) {
653 dir.mkdir();
654 FileUtils.setPermissions(
655 dir.getPath(),
656 FileUtils.S_IRWXU|FileUtils.S_IRWXG|FileUtils.S_IXOTH,
657 -1, -1);
658 }
Amith Yamasanie928d7d2012-09-17 21:46:51 -0700659 FileOutputStream os;
Adrian Roos1bdff912015-02-17 15:51:35 +0100660 if (bitmap.compress(Bitmap.CompressFormat.PNG, 100, os = new FileOutputStream(tmp))
661 && tmp.renameTo(file)) {
Amith Yamasani3b49f072012-09-17 10:21:43 -0700662 info.iconPath = file.getAbsolutePath();
663 }
Amith Yamasanie928d7d2012-09-17 21:46:51 -0700664 try {
665 os.close();
666 } catch (IOException ioe) {
667 // What the ... !
668 }
Adrian Roos1bdff912015-02-17 15:51:35 +0100669 tmp.delete();
Amith Yamasanib8151ec2012-04-18 18:02:48 -0700670 } catch (FileNotFoundException e) {
Amith Yamasani2a003292012-08-14 18:25:45 -0700671 Slog.w(LOG_TAG, "Error setting photo for user ", e);
Amith Yamasanib8151ec2012-04-18 18:02:48 -0700672 }
Amith Yamasanib8151ec2012-04-18 18:02:48 -0700673 }
674
Amith Yamasani0b285492011-04-14 17:35:23 -0700675 /**
676 * Returns an array of user ids. This array is cached here for quick access, so do not modify or
677 * cache it elsewhere.
678 * @return the array of user ids.
679 */
Dianne Hackborn1676c852012-09-10 14:52:30 -0700680 public int[] getUserIds() {
Dianne Hackborn4428e172012-08-24 17:43:05 -0700681 synchronized (mPackagesLock) {
Dianne Hackborn7767eac2012-08-23 18:25:40 -0700682 return mUserIds;
683 }
Amith Yamasani0b285492011-04-14 17:35:23 -0700684 }
685
Dianne Hackborn4428e172012-08-24 17:43:05 -0700686 int[] getUserIdsLPr() {
687 return mUserIds;
688 }
689
Amith Yamasani13593602012-03-22 16:16:17 -0700690 private void readUserListLocked() {
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700691 if (!mUserListFile.exists()) {
Amith Yamasani13593602012-03-22 16:16:17 -0700692 fallbackToSingleUserLocked();
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700693 return;
694 }
695 FileInputStream fis = null;
Amith Yamasani2a003292012-08-14 18:25:45 -0700696 AtomicFile userListFile = new AtomicFile(mUserListFile);
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700697 try {
Amith Yamasani2a003292012-08-14 18:25:45 -0700698 fis = userListFile.openRead();
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700699 XmlPullParser parser = Xml.newPullParser();
Wojciech Staszkiewicz9e9e2e72015-05-08 14:58:46 +0100700 parser.setInput(fis, StandardCharsets.UTF_8.name());
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700701 int type;
702 while ((type = parser.next()) != XmlPullParser.START_TAG
703 && type != XmlPullParser.END_DOCUMENT) {
704 ;
705 }
706
707 if (type != XmlPullParser.START_TAG) {
Amith Yamasani0b285492011-04-14 17:35:23 -0700708 Slog.e(LOG_TAG, "Unable to read user list");
Amith Yamasani13593602012-03-22 16:16:17 -0700709 fallbackToSingleUserLocked();
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700710 return;
711 }
712
Amith Yamasani2a003292012-08-14 18:25:45 -0700713 mNextSerialNumber = -1;
714 if (parser.getName().equals(TAG_USERS)) {
715 String lastSerialNumber = parser.getAttributeValue(null, ATTR_NEXT_SERIAL_NO);
716 if (lastSerialNumber != null) {
717 mNextSerialNumber = Integer.parseInt(lastSerialNumber);
718 }
Amith Yamasani6f34b412012-10-22 18:19:27 -0700719 String versionNumber = parser.getAttributeValue(null, ATTR_USER_VERSION);
720 if (versionNumber != null) {
721 mUserVersion = Integer.parseInt(versionNumber);
722 }
Amith Yamasani2a003292012-08-14 18:25:45 -0700723 }
724
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700725 while ((type = parser.next()) != XmlPullParser.END_DOCUMENT) {
Amith Yamasanie4afaa32014-06-30 14:55:07 +0530726 if (type == XmlPullParser.START_TAG) {
727 final String name = parser.getName();
728 if (name.equals(TAG_USER)) {
729 String id = parser.getAttributeValue(null, ATTR_ID);
730 UserInfo user = readUserLocked(Integer.parseInt(id));
Amith Yamasani6f34b412012-10-22 18:19:27 -0700731
Amith Yamasanie4afaa32014-06-30 14:55:07 +0530732 if (user != null) {
733 mUsers.put(user.id, user);
734 if (mNextSerialNumber < 0 || mNextSerialNumber <= user.id) {
735 mNextSerialNumber = user.id + 1;
736 }
Amith Yamasani2a003292012-08-14 18:25:45 -0700737 }
Amith Yamasanie4afaa32014-06-30 14:55:07 +0530738 } else if (name.equals(TAG_GUEST_RESTRICTIONS)) {
Amith Yamasanida0b1682014-11-21 12:58:17 -0800739 while ((type = parser.next()) != XmlPullParser.END_DOCUMENT
740 && type != XmlPullParser.END_TAG) {
741 if (type == XmlPullParser.START_TAG) {
742 if (parser.getName().equals(TAG_RESTRICTIONS)) {
743 readRestrictionsLocked(parser, mGuestRestrictions);
744 }
745 break;
746 }
747 }
Amith Yamasani258848d2012-08-10 17:06:33 -0700748 }
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700749 }
750 }
Amith Yamasani13593602012-03-22 16:16:17 -0700751 updateUserIdsLocked();
Amith Yamasani350962c2013-08-06 11:18:53 -0700752 upgradeIfNecessaryLocked();
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700753 } catch (IOException ioe) {
Amith Yamasani13593602012-03-22 16:16:17 -0700754 fallbackToSingleUserLocked();
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700755 } catch (XmlPullParserException pe) {
Amith Yamasani13593602012-03-22 16:16:17 -0700756 fallbackToSingleUserLocked();
Dianne Hackbornbfd89b32011-12-15 18:22:54 -0800757 } finally {
758 if (fis != null) {
759 try {
760 fis.close();
761 } catch (IOException e) {
762 }
763 }
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700764 }
765 }
766
Amith Yamasani6f34b412012-10-22 18:19:27 -0700767 /**
Amith Yamasanibc9625052012-11-15 14:39:18 -0800768 * Upgrade steps between versions, either for fixing bugs or changing the data format.
Amith Yamasani6f34b412012-10-22 18:19:27 -0700769 */
Amith Yamasani350962c2013-08-06 11:18:53 -0700770 private void upgradeIfNecessaryLocked() {
Amith Yamasani6f34b412012-10-22 18:19:27 -0700771 int userVersion = mUserVersion;
772 if (userVersion < 1) {
773 // Assign a proper name for the owner, if not initialized correctly before
774 UserInfo user = mUsers.get(UserHandle.USER_OWNER);
775 if ("Primary".equals(user.name)) {
776 user.name = mContext.getResources().getString(com.android.internal.R.string.owner_name);
Fyodor Kupolov75d0ea82014-12-15 16:21:07 -0800777 scheduleWriteUserLocked(user);
Amith Yamasani6f34b412012-10-22 18:19:27 -0700778 }
779 userVersion = 1;
780 }
781
Amith Yamasanibc9625052012-11-15 14:39:18 -0800782 if (userVersion < 2) {
783 // Owner should be marked as initialized
784 UserInfo user = mUsers.get(UserHandle.USER_OWNER);
785 if ((user.flags & UserInfo.FLAG_INITIALIZED) == 0) {
786 user.flags |= UserInfo.FLAG_INITIALIZED;
Fyodor Kupolov75d0ea82014-12-15 16:21:07 -0800787 scheduleWriteUserLocked(user);
Amith Yamasanibc9625052012-11-15 14:39:18 -0800788 }
789 userVersion = 2;
790 }
791
Amith Yamasani350962c2013-08-06 11:18:53 -0700792
Amith Yamasani5e486f52013-08-07 11:06:44 -0700793 if (userVersion < 4) {
Amith Yamasani5e486f52013-08-07 11:06:44 -0700794 userVersion = 4;
795 }
796
Amith Yamasaniaa6634e2014-10-06 14:20:28 -0700797 if (userVersion < 5) {
798 initDefaultGuestRestrictions();
799 userVersion = 5;
800 }
801
Amith Yamasani6f34b412012-10-22 18:19:27 -0700802 if (userVersion < USER_VERSION) {
803 Slog.w(LOG_TAG, "User version " + mUserVersion + " didn't upgrade as expected to "
804 + USER_VERSION);
805 } else {
806 mUserVersion = userVersion;
807 writeUserListLocked();
808 }
809 }
810
Amith Yamasani13593602012-03-22 16:16:17 -0700811 private void fallbackToSingleUserLocked() {
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700812 // Create the primary user
Amith Yamasani67df64b2012-12-14 12:09:36 -0800813 UserInfo primary = new UserInfo(UserHandle.USER_OWNER,
Amith Yamasani6f34b412012-10-22 18:19:27 -0700814 mContext.getResources().getString(com.android.internal.R.string.owner_name), null,
Amith Yamasani756901d2012-10-12 12:30:07 -0700815 UserInfo.FLAG_ADMIN | UserInfo.FLAG_PRIMARY | UserInfo.FLAG_INITIALIZED);
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700816 mUsers.put(0, primary);
Amith Yamasani634cf312012-10-04 17:34:21 -0700817 mNextSerialNumber = MIN_USER_ID;
Geoffrey Borggaard15b8b2c2013-08-28 22:11:10 -0400818 mUserVersion = USER_VERSION;
Amith Yamasani67df64b2012-12-14 12:09:36 -0800819
Geoffrey Borggaarde45e45e32013-01-24 10:03:20 -0500820 Bundle restrictions = new Bundle();
Amith Yamasani67df64b2012-12-14 12:09:36 -0800821 mUserRestrictions.append(UserHandle.USER_OWNER, restrictions);
822
Amith Yamasani13593602012-03-22 16:16:17 -0700823 updateUserIdsLocked();
Amith Yamasaniaa6634e2014-10-06 14:20:28 -0700824 initDefaultGuestRestrictions();
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700825
Amith Yamasani13593602012-03-22 16:16:17 -0700826 writeUserListLocked();
827 writeUserLocked(primary);
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700828 }
829
Fyodor Kupolov75d0ea82014-12-15 16:21:07 -0800830 private void scheduleWriteUserLocked(UserInfo userInfo) {
831 if (!mHandler.hasMessages(WRITE_USER_MSG, userInfo)) {
832 Message msg = mHandler.obtainMessage(WRITE_USER_MSG, userInfo);
833 mHandler.sendMessageDelayed(msg, WRITE_USER_DELAY);
834 }
835 }
836
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700837 /*
838 * Writes the user file in this format:
839 *
840 * <user flags="20039023" id="0">
841 * <name>Primary</name>
842 * </user>
843 */
Amith Yamasani13593602012-03-22 16:16:17 -0700844 private void writeUserLocked(UserInfo userInfo) {
Amith Yamasani742a6712011-05-04 14:49:28 -0700845 FileOutputStream fos = null;
Amith Yamasanifc95e702013-09-26 13:20:17 -0700846 AtomicFile userFile = new AtomicFile(new File(mUsersDir, userInfo.id + XML_SUFFIX));
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700847 try {
Amith Yamasani2a003292012-08-14 18:25:45 -0700848 fos = userFile.startWrite();
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700849 final BufferedOutputStream bos = new BufferedOutputStream(fos);
850
851 // XmlSerializer serializer = XmlUtils.serializerInstance();
852 final XmlSerializer serializer = new FastXmlSerializer();
Wojciech Staszkiewicz9e9e2e72015-05-08 14:58:46 +0100853 serializer.setOutput(bos, StandardCharsets.UTF_8.name());
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700854 serializer.startDocument(null, true);
855 serializer.setFeature("http://xmlpull.org/v1/doc/features.html#indent-output", true);
856
857 serializer.startTag(null, TAG_USER);
858 serializer.attribute(null, ATTR_ID, Integer.toString(userInfo.id));
Amith Yamasani2a003292012-08-14 18:25:45 -0700859 serializer.attribute(null, ATTR_SERIAL_NO, Integer.toString(userInfo.serialNumber));
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700860 serializer.attribute(null, ATTR_FLAGS, Integer.toString(userInfo.flags));
Amith Yamasani920ace02012-09-20 22:15:37 -0700861 serializer.attribute(null, ATTR_CREATION_TIME, Long.toString(userInfo.creationTime));
862 serializer.attribute(null, ATTR_LAST_LOGGED_IN_TIME,
863 Long.toString(userInfo.lastLoggedInTime));
Amith Yamasanib8151ec2012-04-18 18:02:48 -0700864 if (userInfo.iconPath != null) {
865 serializer.attribute(null, ATTR_ICON_PATH, userInfo.iconPath);
866 }
Dianne Hackbornd4ac8d72012-09-27 23:20:10 -0700867 if (userInfo.partial) {
868 serializer.attribute(null, ATTR_PARTIAL, "true");
869 }
Adam Lesinskieddeb492014-09-08 17:50:03 -0700870 if (userInfo.guestToRemove) {
871 serializer.attribute(null, ATTR_GUEST_TO_REMOVE, "true");
872 }
Kenny Guy2a764942014-04-02 13:29:20 +0100873 if (userInfo.profileGroupId != UserInfo.NO_PROFILE_GROUP_ID) {
874 serializer.attribute(null, ATTR_PROFILE_GROUP_ID,
875 Integer.toString(userInfo.profileGroupId));
Kenny Guya52dc3e2014-02-11 15:33:14 +0000876 }
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700877
878 serializer.startTag(null, TAG_NAME);
879 serializer.text(userInfo.name);
880 serializer.endTag(null, TAG_NAME);
Amith Yamasanie4cf7342012-12-17 11:12:09 -0800881 Bundle restrictions = mUserRestrictions.get(userInfo.id);
882 if (restrictions != null) {
Amith Yamasanie4afaa32014-06-30 14:55:07 +0530883 writeRestrictionsLocked(serializer, restrictions);
Amith Yamasanie4cf7342012-12-17 11:12:09 -0800884 }
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700885 serializer.endTag(null, TAG_USER);
886
887 serializer.endDocument();
Amith Yamasani2a003292012-08-14 18:25:45 -0700888 userFile.finishWrite(fos);
889 } catch (Exception ioe) {
Amith Yamasani0b285492011-04-14 17:35:23 -0700890 Slog.e(LOG_TAG, "Error writing user info " + userInfo.id + "\n" + ioe);
Amith Yamasani2a003292012-08-14 18:25:45 -0700891 userFile.failWrite(fos);
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700892 }
893 }
894
895 /*
896 * Writes the user list file in this format:
897 *
Amith Yamasani2a003292012-08-14 18:25:45 -0700898 * <users nextSerialNumber="3">
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700899 * <user id="0"></user>
900 * <user id="2"></user>
901 * </users>
902 */
Amith Yamasani13593602012-03-22 16:16:17 -0700903 private void writeUserListLocked() {
Amith Yamasani742a6712011-05-04 14:49:28 -0700904 FileOutputStream fos = null;
Amith Yamasani2a003292012-08-14 18:25:45 -0700905 AtomicFile userListFile = new AtomicFile(mUserListFile);
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700906 try {
Amith Yamasani2a003292012-08-14 18:25:45 -0700907 fos = userListFile.startWrite();
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700908 final BufferedOutputStream bos = new BufferedOutputStream(fos);
909
910 // XmlSerializer serializer = XmlUtils.serializerInstance();
911 final XmlSerializer serializer = new FastXmlSerializer();
Wojciech Staszkiewicz9e9e2e72015-05-08 14:58:46 +0100912 serializer.setOutput(bos, StandardCharsets.UTF_8.name());
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700913 serializer.startDocument(null, true);
914 serializer.setFeature("http://xmlpull.org/v1/doc/features.html#indent-output", true);
915
916 serializer.startTag(null, TAG_USERS);
Amith Yamasani2a003292012-08-14 18:25:45 -0700917 serializer.attribute(null, ATTR_NEXT_SERIAL_NO, Integer.toString(mNextSerialNumber));
Amith Yamasani6f34b412012-10-22 18:19:27 -0700918 serializer.attribute(null, ATTR_USER_VERSION, Integer.toString(mUserVersion));
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700919
Adam Lesinskieddeb492014-09-08 17:50:03 -0700920 serializer.startTag(null, TAG_GUEST_RESTRICTIONS);
Amith Yamasanie4afaa32014-06-30 14:55:07 +0530921 writeRestrictionsLocked(serializer, mGuestRestrictions);
922 serializer.endTag(null, TAG_GUEST_RESTRICTIONS);
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700923 for (int i = 0; i < mUsers.size(); i++) {
924 UserInfo user = mUsers.valueAt(i);
925 serializer.startTag(null, TAG_USER);
926 serializer.attribute(null, ATTR_ID, Integer.toString(user.id));
927 serializer.endTag(null, TAG_USER);
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700928 }
929
930 serializer.endTag(null, TAG_USERS);
931
932 serializer.endDocument();
Amith Yamasani2a003292012-08-14 18:25:45 -0700933 userListFile.finishWrite(fos);
934 } catch (Exception e) {
935 userListFile.failWrite(fos);
Amith Yamasani0b285492011-04-14 17:35:23 -0700936 Slog.e(LOG_TAG, "Error writing user list");
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700937 }
938 }
939
Amith Yamasanie4afaa32014-06-30 14:55:07 +0530940 private void writeRestrictionsLocked(XmlSerializer serializer, Bundle restrictions)
941 throws IOException {
942 serializer.startTag(null, TAG_RESTRICTIONS);
943 writeBoolean(serializer, restrictions, UserManager.DISALLOW_CONFIG_WIFI);
944 writeBoolean(serializer, restrictions, UserManager.DISALLOW_MODIFY_ACCOUNTS);
945 writeBoolean(serializer, restrictions, UserManager.DISALLOW_INSTALL_APPS);
946 writeBoolean(serializer, restrictions, UserManager.DISALLOW_UNINSTALL_APPS);
947 writeBoolean(serializer, restrictions, UserManager.DISALLOW_SHARE_LOCATION);
948 writeBoolean(serializer, restrictions,
949 UserManager.DISALLOW_INSTALL_UNKNOWN_SOURCES);
950 writeBoolean(serializer, restrictions, UserManager.DISALLOW_CONFIG_BLUETOOTH);
951 writeBoolean(serializer, restrictions, UserManager.DISALLOW_USB_FILE_TRANSFER);
952 writeBoolean(serializer, restrictions, UserManager.DISALLOW_CONFIG_CREDENTIALS);
953 writeBoolean(serializer, restrictions, UserManager.DISALLOW_REMOVE_USER);
954 writeBoolean(serializer, restrictions, UserManager.DISALLOW_DEBUGGING_FEATURES);
955 writeBoolean(serializer, restrictions, UserManager.DISALLOW_CONFIG_VPN);
956 writeBoolean(serializer, restrictions, UserManager.DISALLOW_CONFIG_TETHERING);
Stuart Scotte3e314d2015-04-20 14:07:45 -0700957 writeBoolean(serializer, restrictions, UserManager.DISALLOW_NETWORK_RESET);
Amith Yamasanie4afaa32014-06-30 14:55:07 +0530958 writeBoolean(serializer, restrictions, UserManager.DISALLOW_FACTORY_RESET);
959 writeBoolean(serializer, restrictions, UserManager.DISALLOW_ADD_USER);
960 writeBoolean(serializer, restrictions, UserManager.ENSURE_VERIFY_APPS);
961 writeBoolean(serializer, restrictions, UserManager.DISALLOW_CONFIG_CELL_BROADCASTS);
962 writeBoolean(serializer, restrictions, UserManager.DISALLOW_CONFIG_MOBILE_NETWORKS);
963 writeBoolean(serializer, restrictions, UserManager.DISALLOW_APPS_CONTROL);
964 writeBoolean(serializer, restrictions, UserManager.DISALLOW_MOUNT_PHYSICAL_MEDIA);
965 writeBoolean(serializer, restrictions, UserManager.DISALLOW_UNMUTE_MICROPHONE);
966 writeBoolean(serializer, restrictions, UserManager.DISALLOW_ADJUST_VOLUME);
Amith Yamasani390989d2014-07-17 10:52:03 -0700967 writeBoolean(serializer, restrictions, UserManager.DISALLOW_OUTGOING_CALLS);
968 writeBoolean(serializer, restrictions, UserManager.DISALLOW_SMS);
Jeff Sharkey2cc03e52015-03-20 11:24:04 -0700969 writeBoolean(serializer, restrictions, UserManager.DISALLOW_FUN);
Jason Monk1c7c3192014-06-26 12:52:18 -0400970 writeBoolean(serializer, restrictions, UserManager.DISALLOW_CREATE_WINDOWS);
Amith Yamasani26af8292014-09-09 09:57:27 -0700971 writeBoolean(serializer, restrictions, UserManager.DISALLOW_CROSS_PROFILE_COPY_PASTE);
972 writeBoolean(serializer, restrictions, UserManager.DISALLOW_OUTGOING_BEAM);
Benjamin Franzf3ece362015-02-11 10:51:10 +0000973 writeBoolean(serializer, restrictions, UserManager.DISALLOW_WALLPAPER);
Benjamin Franzbff46ba2015-03-05 18:33:51 +0000974 writeBoolean(serializer, restrictions, UserManager.DISALLOW_SAFE_BOOT);
Nicolas Prevotf0029c12015-06-25 14:25:41 -0700975 writeBoolean(serializer, restrictions, UserManager.ALLOW_PARENT_PROFILE_APP_LINKING);
Amith Yamasanie4afaa32014-06-30 14:55:07 +0530976 serializer.endTag(null, TAG_RESTRICTIONS);
977 }
978
Amith Yamasanie4cf7342012-12-17 11:12:09 -0800979 private UserInfo readUserLocked(int id) {
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700980 int flags = 0;
Amith Yamasani2a003292012-08-14 18:25:45 -0700981 int serialNumber = id;
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700982 String name = null;
Amith Yamasanib8151ec2012-04-18 18:02:48 -0700983 String iconPath = null;
Amith Yamasani920ace02012-09-20 22:15:37 -0700984 long creationTime = 0L;
985 long lastLoggedInTime = 0L;
Kenny Guy2a764942014-04-02 13:29:20 +0100986 int profileGroupId = UserInfo.NO_PROFILE_GROUP_ID;
Dianne Hackbornd4ac8d72012-09-27 23:20:10 -0700987 boolean partial = false;
Adam Lesinskieddeb492014-09-08 17:50:03 -0700988 boolean guestToRemove = false;
Amith Yamasanie4cf7342012-12-17 11:12:09 -0800989 Bundle restrictions = new Bundle();
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700990
991 FileInputStream fis = null;
992 try {
Amith Yamasani2a003292012-08-14 18:25:45 -0700993 AtomicFile userFile =
Amith Yamasanifc95e702013-09-26 13:20:17 -0700994 new AtomicFile(new File(mUsersDir, Integer.toString(id) + XML_SUFFIX));
Amith Yamasani2a003292012-08-14 18:25:45 -0700995 fis = userFile.openRead();
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700996 XmlPullParser parser = Xml.newPullParser();
Wojciech Staszkiewicz9e9e2e72015-05-08 14:58:46 +0100997 parser.setInput(fis, StandardCharsets.UTF_8.name());
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700998 int type;
999 while ((type = parser.next()) != XmlPullParser.START_TAG
1000 && type != XmlPullParser.END_DOCUMENT) {
1001 ;
1002 }
1003
1004 if (type != XmlPullParser.START_TAG) {
Amith Yamasani0b285492011-04-14 17:35:23 -07001005 Slog.e(LOG_TAG, "Unable to read user " + id);
Amith Yamasani4b2e9342011-03-31 12:38:53 -07001006 return null;
1007 }
1008
1009 if (type == XmlPullParser.START_TAG && parser.getName().equals(TAG_USER)) {
Amith Yamasani920ace02012-09-20 22:15:37 -07001010 int storedId = readIntAttribute(parser, ATTR_ID, -1);
1011 if (storedId != id) {
Amith Yamasani0b285492011-04-14 17:35:23 -07001012 Slog.e(LOG_TAG, "User id does not match the file name");
Amith Yamasani4b2e9342011-03-31 12:38:53 -07001013 return null;
1014 }
Amith Yamasani920ace02012-09-20 22:15:37 -07001015 serialNumber = readIntAttribute(parser, ATTR_SERIAL_NO, id);
1016 flags = readIntAttribute(parser, ATTR_FLAGS, 0);
Amith Yamasanib8151ec2012-04-18 18:02:48 -07001017 iconPath = parser.getAttributeValue(null, ATTR_ICON_PATH);
Amith Yamasani920ace02012-09-20 22:15:37 -07001018 creationTime = readLongAttribute(parser, ATTR_CREATION_TIME, 0);
1019 lastLoggedInTime = readLongAttribute(parser, ATTR_LAST_LOGGED_IN_TIME, 0);
Kenny Guy2a764942014-04-02 13:29:20 +01001020 profileGroupId = readIntAttribute(parser, ATTR_PROFILE_GROUP_ID,
1021 UserInfo.NO_PROFILE_GROUP_ID);
Dianne Hackbornd4ac8d72012-09-27 23:20:10 -07001022 String valueString = parser.getAttributeValue(null, ATTR_PARTIAL);
1023 if ("true".equals(valueString)) {
1024 partial = true;
1025 }
Adam Lesinskieddeb492014-09-08 17:50:03 -07001026 valueString = parser.getAttributeValue(null, ATTR_GUEST_TO_REMOVE);
1027 if ("true".equals(valueString)) {
1028 guestToRemove = true;
1029 }
Amith Yamasani4b2e9342011-03-31 12:38:53 -07001030
Amith Yamasanie4cf7342012-12-17 11:12:09 -08001031 int outerDepth = parser.getDepth();
1032 while ((type = parser.next()) != XmlPullParser.END_DOCUMENT
1033 && (type != XmlPullParser.END_TAG || parser.getDepth() > outerDepth)) {
1034 if (type == XmlPullParser.END_TAG || type == XmlPullParser.TEXT) {
1035 continue;
1036 }
1037 String tag = parser.getName();
1038 if (TAG_NAME.equals(tag)) {
1039 type = parser.next();
1040 if (type == XmlPullParser.TEXT) {
1041 name = parser.getText();
1042 }
1043 } else if (TAG_RESTRICTIONS.equals(tag)) {
Amith Yamasanie4afaa32014-06-30 14:55:07 +05301044 readRestrictionsLocked(parser, restrictions);
Amith Yamasani4b2e9342011-03-31 12:38:53 -07001045 }
1046 }
1047 }
Amith Yamasani4b2e9342011-03-31 12:38:53 -07001048
Amith Yamasanib8151ec2012-04-18 18:02:48 -07001049 UserInfo userInfo = new UserInfo(id, name, iconPath, flags);
Amith Yamasani2a003292012-08-14 18:25:45 -07001050 userInfo.serialNumber = serialNumber;
Amith Yamasani920ace02012-09-20 22:15:37 -07001051 userInfo.creationTime = creationTime;
1052 userInfo.lastLoggedInTime = lastLoggedInTime;
Dianne Hackbornd4ac8d72012-09-27 23:20:10 -07001053 userInfo.partial = partial;
Adam Lesinskieddeb492014-09-08 17:50:03 -07001054 userInfo.guestToRemove = guestToRemove;
Kenny Guy2a764942014-04-02 13:29:20 +01001055 userInfo.profileGroupId = profileGroupId;
Amith Yamasanie4cf7342012-12-17 11:12:09 -08001056 mUserRestrictions.append(id, restrictions);
Amith Yamasani4b2e9342011-03-31 12:38:53 -07001057 return userInfo;
1058
1059 } catch (IOException ioe) {
1060 } catch (XmlPullParserException pe) {
Dianne Hackbornbfd89b32011-12-15 18:22:54 -08001061 } finally {
1062 if (fis != null) {
1063 try {
1064 fis.close();
1065 } catch (IOException e) {
1066 }
1067 }
Amith Yamasani4b2e9342011-03-31 12:38:53 -07001068 }
1069 return null;
1070 }
1071
Amith Yamasanie4afaa32014-06-30 14:55:07 +05301072 private void readRestrictionsLocked(XmlPullParser parser, Bundle restrictions)
1073 throws IOException {
1074 readBoolean(parser, restrictions, UserManager.DISALLOW_CONFIG_WIFI);
1075 readBoolean(parser, restrictions, UserManager.DISALLOW_MODIFY_ACCOUNTS);
1076 readBoolean(parser, restrictions, UserManager.DISALLOW_INSTALL_APPS);
1077 readBoolean(parser, restrictions, UserManager.DISALLOW_UNINSTALL_APPS);
1078 readBoolean(parser, restrictions, UserManager.DISALLOW_SHARE_LOCATION);
1079 readBoolean(parser, restrictions,
1080 UserManager.DISALLOW_INSTALL_UNKNOWN_SOURCES);
1081 readBoolean(parser, restrictions, UserManager.DISALLOW_CONFIG_BLUETOOTH);
1082 readBoolean(parser, restrictions, UserManager.DISALLOW_USB_FILE_TRANSFER);
1083 readBoolean(parser, restrictions, UserManager.DISALLOW_CONFIG_CREDENTIALS);
1084 readBoolean(parser, restrictions, UserManager.DISALLOW_REMOVE_USER);
1085 readBoolean(parser, restrictions, UserManager.DISALLOW_DEBUGGING_FEATURES);
1086 readBoolean(parser, restrictions, UserManager.DISALLOW_CONFIG_VPN);
1087 readBoolean(parser, restrictions, UserManager.DISALLOW_CONFIG_TETHERING);
Stuart Scotte3e314d2015-04-20 14:07:45 -07001088 readBoolean(parser, restrictions, UserManager.DISALLOW_NETWORK_RESET);
Amith Yamasanie4afaa32014-06-30 14:55:07 +05301089 readBoolean(parser, restrictions, UserManager.DISALLOW_FACTORY_RESET);
1090 readBoolean(parser, restrictions, UserManager.DISALLOW_ADD_USER);
1091 readBoolean(parser, restrictions, UserManager.ENSURE_VERIFY_APPS);
1092 readBoolean(parser, restrictions, UserManager.DISALLOW_CONFIG_CELL_BROADCASTS);
1093 readBoolean(parser, restrictions, UserManager.DISALLOW_CONFIG_MOBILE_NETWORKS);
1094 readBoolean(parser, restrictions, UserManager.DISALLOW_APPS_CONTROL);
1095 readBoolean(parser, restrictions,
1096 UserManager.DISALLOW_MOUNT_PHYSICAL_MEDIA);
1097 readBoolean(parser, restrictions, UserManager.DISALLOW_UNMUTE_MICROPHONE);
1098 readBoolean(parser, restrictions, UserManager.DISALLOW_ADJUST_VOLUME);
Amith Yamasani390989d2014-07-17 10:52:03 -07001099 readBoolean(parser, restrictions, UserManager.DISALLOW_OUTGOING_CALLS);
1100 readBoolean(parser, restrictions, UserManager.DISALLOW_SMS);
Jeff Sharkey2cc03e52015-03-20 11:24:04 -07001101 readBoolean(parser, restrictions, UserManager.DISALLOW_FUN);
Jason Monk1c7c3192014-06-26 12:52:18 -04001102 readBoolean(parser, restrictions, UserManager.DISALLOW_CREATE_WINDOWS);
Amith Yamasani26af8292014-09-09 09:57:27 -07001103 readBoolean(parser, restrictions, UserManager.DISALLOW_CROSS_PROFILE_COPY_PASTE);
1104 readBoolean(parser, restrictions, UserManager.DISALLOW_OUTGOING_BEAM);
Benjamin Franzf3ece362015-02-11 10:51:10 +00001105 readBoolean(parser, restrictions, UserManager.DISALLOW_WALLPAPER);
Benjamin Franzbff46ba2015-03-05 18:33:51 +00001106 readBoolean(parser, restrictions, UserManager.DISALLOW_SAFE_BOOT);
Nicolas Prevotf0029c12015-06-25 14:25:41 -07001107 readBoolean(parser, restrictions, UserManager.ALLOW_PARENT_PROFILE_APP_LINKING);
Amith Yamasanie4afaa32014-06-30 14:55:07 +05301108 }
1109
Amith Yamasanie4cf7342012-12-17 11:12:09 -08001110 private void readBoolean(XmlPullParser parser, Bundle restrictions,
1111 String restrictionKey) {
1112 String value = parser.getAttributeValue(null, restrictionKey);
Amith Yamasani71e6c692013-03-24 17:39:28 -07001113 if (value != null) {
1114 restrictions.putBoolean(restrictionKey, Boolean.parseBoolean(value));
1115 }
Amith Yamasanie4cf7342012-12-17 11:12:09 -08001116 }
1117
1118 private void writeBoolean(XmlSerializer xml, Bundle restrictions, String restrictionKey)
1119 throws IOException {
1120 if (restrictions.containsKey(restrictionKey)) {
1121 xml.attribute(null, restrictionKey,
1122 Boolean.toString(restrictions.getBoolean(restrictionKey)));
1123 }
1124 }
1125
Amith Yamasani920ace02012-09-20 22:15:37 -07001126 private int readIntAttribute(XmlPullParser parser, String attr, int defaultValue) {
1127 String valueString = parser.getAttributeValue(null, attr);
1128 if (valueString == null) return defaultValue;
1129 try {
1130 return Integer.parseInt(valueString);
1131 } catch (NumberFormatException nfe) {
1132 return defaultValue;
1133 }
1134 }
1135
1136 private long readLongAttribute(XmlPullParser parser, String attr, long defaultValue) {
1137 String valueString = parser.getAttributeValue(null, attr);
1138 if (valueString == null) return defaultValue;
1139 try {
1140 return Long.parseLong(valueString);
1141 } catch (NumberFormatException nfe) {
1142 return defaultValue;
1143 }
1144 }
1145
Amith Yamasani1a7472e2013-07-02 11:17:30 -07001146 private boolean isPackageInstalled(String pkg, int userId) {
1147 final ApplicationInfo info = mPm.getApplicationInfo(pkg,
1148 PackageManager.GET_UNINSTALLED_PACKAGES,
1149 userId);
1150 if (info == null || (info.flags&ApplicationInfo.FLAG_INSTALLED) == 0) {
1151 return false;
1152 }
1153 return true;
1154 }
1155
Amith Yamasanib82add22013-07-09 11:24:44 -07001156 /**
Kenny Guyd21b2182014-07-17 16:38:55 +01001157 * Removes all the restrictions files (res_<packagename>) for a given user.
Amith Yamasanib82add22013-07-09 11:24:44 -07001158 * Does not do any permissions checking.
1159 */
Kenny Guyd21b2182014-07-17 16:38:55 +01001160 private void cleanAppRestrictions(int userId) {
Amith Yamasanib82add22013-07-09 11:24:44 -07001161 synchronized (mPackagesLock) {
1162 File dir = Environment.getUserSystemDirectory(userId);
1163 String[] files = dir.list();
1164 if (files == null) return;
1165 for (String fileName : files) {
1166 if (fileName.startsWith(RESTRICTIONS_FILE_PREFIX)) {
1167 File resFile = new File(dir, fileName);
1168 if (resFile.exists()) {
Kenny Guyd21b2182014-07-17 16:38:55 +01001169 resFile.delete();
Amith Yamasanib82add22013-07-09 11:24:44 -07001170 }
1171 }
1172 }
1173 }
1174 }
1175
Amith Yamasani1a7472e2013-07-02 11:17:30 -07001176 /**
1177 * Removes the app restrictions file for a specific package and user id, if it exists.
1178 */
1179 private void cleanAppRestrictionsForPackage(String pkg, int userId) {
1180 synchronized (mPackagesLock) {
1181 File dir = Environment.getUserSystemDirectory(userId);
Amith Yamasanifc95e702013-09-26 13:20:17 -07001182 File resFile = new File(dir, packageToRestrictionsFileName(pkg));
Amith Yamasani1a7472e2013-07-02 11:17:30 -07001183 if (resFile.exists()) {
1184 resFile.delete();
1185 }
1186 }
1187 }
1188
Kenny Guya52dc3e2014-02-11 15:33:14 +00001189 @Override
Kenny Guy2a764942014-04-02 13:29:20 +01001190 public UserInfo createProfileForUser(String name, int flags, int userId) {
Kenny Guya52dc3e2014-02-11 15:33:14 +00001191 checkManageUsersPermission("Only the system can create users");
Kenny Guy2a764942014-04-02 13:29:20 +01001192 if (userId != UserHandle.USER_OWNER) {
1193 Slog.w(LOG_TAG, "Only user owner can have profiles");
Kenny Guya52dc3e2014-02-11 15:33:14 +00001194 return null;
1195 }
Kenny Guy2a764942014-04-02 13:29:20 +01001196 return createUserInternal(name, flags, userId);
Kenny Guya52dc3e2014-02-11 15:33:14 +00001197 }
1198
Amith Yamasani258848d2012-08-10 17:06:33 -07001199 @Override
Amith Yamasani13593602012-03-22 16:16:17 -07001200 public UserInfo createUser(String name, int flags) {
Amith Yamasani2a003292012-08-14 18:25:45 -07001201 checkManageUsersPermission("Only the system can create users");
Nicolas Prevotc6d033e2014-02-27 13:11:09 +00001202 return createUserInternal(name, flags, UserHandle.USER_NULL);
Kenny Guya52dc3e2014-02-11 15:33:14 +00001203 }
Amith Yamasanifaea76f2012-09-11 10:59:48 -07001204
Jessica Hummelbe81c802014-04-22 15:49:22 +01001205 private UserInfo createUserInternal(String name, int flags, int parentId) {
Julia Reynolds75175022014-06-26 16:35:00 -04001206 if (getUserRestrictions(UserHandle.getCallingUserId()).getBoolean(
1207 UserManager.DISALLOW_ADD_USER, false)) {
1208 Log.w(LOG_TAG, "Cannot add user. DISALLOW_ADD_USER is enabled.");
1209 return null;
1210 }
Nicolas Prevot72434b72015-05-13 12:15:03 -07001211 if (ActivityManager.isLowRamDeviceStatic()) {
1212 return null;
1213 }
Amith Yamasani95ab7842014-08-11 17:09:26 -07001214 final boolean isGuest = (flags & UserInfo.FLAG_GUEST) != 0;
Nicolas Prevot72434b72015-05-13 12:15:03 -07001215 final boolean isManagedProfile = (flags & UserInfo.FLAG_MANAGED_PROFILE) != 0;
Dianne Hackborn5dc5a002012-09-15 19:33:48 -07001216 final long ident = Binder.clearCallingIdentity();
Nicolas Prevotc6d033e2014-02-27 13:11:09 +00001217 UserInfo userInfo = null;
Dianne Hackborn5dc5a002012-09-15 19:33:48 -07001218 try {
1219 synchronized (mInstallLock) {
1220 synchronized (mPackagesLock) {
Jessica Hummelbe81c802014-04-22 15:49:22 +01001221 UserInfo parent = null;
1222 if (parentId != UserHandle.USER_NULL) {
1223 parent = getUserInfoLocked(parentId);
1224 if (parent == null) return null;
Nicolas Prevotc6d033e2014-02-27 13:11:09 +00001225 }
Nicolas Prevot72434b72015-05-13 12:15:03 -07001226 if (isManagedProfile && !canAddMoreManagedProfiles()) {
1227 return null;
1228 }
1229 if (!isGuest && !isManagedProfile && isUserLimitReachedLocked()) {
1230 // If we're not adding a guest user or a managed profile and the limit has
1231 // been reached, cannot add a user.
Amith Yamasani95ab7842014-08-11 17:09:26 -07001232 return null;
1233 }
1234 // If we're adding a guest and there already exists one, bail.
Adam Lesinskieddeb492014-09-08 17:50:03 -07001235 if (isGuest && findCurrentGuestUserLocked() != null) {
Amith Yamasani95ab7842014-08-11 17:09:26 -07001236 return null;
1237 }
Dianne Hackborn5dc5a002012-09-15 19:33:48 -07001238 int userId = getNextAvailableIdLocked();
1239 userInfo = new UserInfo(userId, name, null, flags);
1240 File userPath = new File(mBaseUserPath, Integer.toString(userId));
1241 userInfo.serialNumber = mNextSerialNumber++;
Amith Yamasani920ace02012-09-20 22:15:37 -07001242 long now = System.currentTimeMillis();
1243 userInfo.creationTime = (now > EPOCH_PLUS_30_YEARS) ? now : 0;
Dianne Hackbornd4ac8d72012-09-27 23:20:10 -07001244 userInfo.partial = true;
Amith Yamasani16389312012-10-17 21:20:14 -07001245 Environment.getUserSystemDirectory(userInfo.id).mkdirs();
Dianne Hackborn5dc5a002012-09-15 19:33:48 -07001246 mUsers.put(userId, userInfo);
1247 writeUserListLocked();
Jessica Hummelbe81c802014-04-22 15:49:22 +01001248 if (parent != null) {
1249 if (parent.profileGroupId == UserInfo.NO_PROFILE_GROUP_ID) {
1250 parent.profileGroupId = parent.id;
Fyodor Kupolov75d0ea82014-12-15 16:21:07 -08001251 scheduleWriteUserLocked(parent);
Kenny Guya52dc3e2014-02-11 15:33:14 +00001252 }
Jessica Hummelbe81c802014-04-22 15:49:22 +01001253 userInfo.profileGroupId = parent.profileGroupId;
Kenny Guya52dc3e2014-02-11 15:33:14 +00001254 }
Dianne Hackborn5dc5a002012-09-15 19:33:48 -07001255 mPm.createNewUserLILPw(userId, userPath);
Dianne Hackbornd4ac8d72012-09-27 23:20:10 -07001256 userInfo.partial = false;
Fyodor Kupolov75d0ea82014-12-15 16:21:07 -08001257 scheduleWriteUserLocked(userInfo);
Dianne Hackbornd4ac8d72012-09-27 23:20:10 -07001258 updateUserIdsLocked();
Geoffrey Borggaarde45e45e32013-01-24 10:03:20 -05001259 Bundle restrictions = new Bundle();
Geoffrey Borggaarde45e45e32013-01-24 10:03:20 -05001260 mUserRestrictions.append(userId, restrictions);
Svet Ganov78027f32015-03-25 09:10:09 -07001261 mPm.newUserCreatedLILPw(userId);
Dianne Hackborn5dc5a002012-09-15 19:33:48 -07001262 }
Dianne Hackborn4428e172012-08-24 17:43:05 -07001263 }
Dianne Hackborn5dc5a002012-09-15 19:33:48 -07001264 if (userInfo != null) {
1265 Intent addedIntent = new Intent(Intent.ACTION_USER_ADDED);
1266 addedIntent.putExtra(Intent.EXTRA_USER_HANDLE, userInfo.id);
1267 mContext.sendBroadcastAsUser(addedIntent, UserHandle.ALL,
1268 android.Manifest.permission.MANAGE_USERS);
1269 }
1270 } finally {
1271 Binder.restoreCallingIdentity(ident);
Amith Yamasani258848d2012-08-10 17:06:33 -07001272 }
Amith Yamasani4b2e9342011-03-31 12:38:53 -07001273 return userInfo;
1274 }
1275
Amith Yamasani95ab7842014-08-11 17:09:26 -07001276 private int numberOfUsersOfTypeLocked(int flags, boolean excludeDying) {
1277 int count = 0;
1278 for (int i = mUsers.size() - 1; i >= 0; i--) {
1279 UserInfo user = mUsers.valueAt(i);
1280 if (!excludeDying || !mRemovingUserIds.get(user.id)) {
1281 if ((user.flags & flags) != 0) {
1282 count++;
1283 }
1284 }
1285 }
1286 return count;
1287 }
1288
Amith Yamasani0b285492011-04-14 17:35:23 -07001289 /**
Adam Lesinskieddeb492014-09-08 17:50:03 -07001290 * Find the current guest user. If the Guest user is partial,
1291 * then do not include it in the results as it is about to die.
1292 * This is different than {@link #numberOfUsersOfTypeLocked(int, boolean)} due to
1293 * the special handling of Guests being removed.
1294 */
1295 private UserInfo findCurrentGuestUserLocked() {
1296 final int size = mUsers.size();
1297 for (int i = 0; i < size; i++) {
1298 final UserInfo user = mUsers.valueAt(i);
1299 if (user.isGuest() && !user.guestToRemove && !mRemovingUserIds.get(user.id)) {
1300 return user;
1301 }
1302 }
1303 return null;
1304 }
1305
1306 /**
Amith Yamasani1df14732014-08-29 21:37:27 -07001307 * Mark this guest user for deletion to allow us to create another guest
1308 * and switch to that user before actually removing this guest.
1309 * @param userHandle the userid of the current guest
1310 * @return whether the user could be marked for deletion
1311 */
1312 public boolean markGuestForDeletion(int userHandle) {
1313 checkManageUsersPermission("Only the system can remove users");
1314 if (getUserRestrictions(UserHandle.getCallingUserId()).getBoolean(
1315 UserManager.DISALLOW_REMOVE_USER, false)) {
1316 Log.w(LOG_TAG, "Cannot remove user. DISALLOW_REMOVE_USER is enabled.");
1317 return false;
1318 }
1319
1320 long ident = Binder.clearCallingIdentity();
1321 try {
1322 final UserInfo user;
1323 synchronized (mPackagesLock) {
1324 user = mUsers.get(userHandle);
1325 if (userHandle == 0 || user == null || mRemovingUserIds.get(userHandle)) {
1326 return false;
1327 }
1328 if (!user.isGuest()) {
1329 return false;
1330 }
Adam Lesinskieddeb492014-09-08 17:50:03 -07001331 // We set this to a guest user that is to be removed. This is a temporary state
1332 // where we are allowed to add new Guest users, even if this one is still not
1333 // removed. This user will still show up in getUserInfo() calls.
1334 // If we don't get around to removing this Guest user, it will be purged on next
1335 // startup.
1336 user.guestToRemove = true;
Amith Yamasani1df14732014-08-29 21:37:27 -07001337 // Mark it as disabled, so that it isn't returned any more when
1338 // profiles are queried.
1339 user.flags |= UserInfo.FLAG_DISABLED;
Amith Yamasani1df14732014-08-29 21:37:27 -07001340 writeUserLocked(user);
1341 }
1342 } finally {
1343 Binder.restoreCallingIdentity(ident);
1344 }
1345 return true;
1346 }
1347
1348 /**
Amith Yamasani0b285492011-04-14 17:35:23 -07001349 * Removes a user and all data directories created for that user. This method should be called
1350 * after the user's processes have been terminated.
Dianne Hackborn10ad9822014-03-17 11:28:36 -07001351 * @param userHandle the user's id
Amith Yamasani0b285492011-04-14 17:35:23 -07001352 */
Amith Yamasani258848d2012-08-10 17:06:33 -07001353 public boolean removeUser(int userHandle) {
Amith Yamasani2a003292012-08-14 18:25:45 -07001354 checkManageUsersPermission("Only the system can remove users");
Julia Reynolds4ac5f852014-06-23 17:38:51 -04001355 if (getUserRestrictions(UserHandle.getCallingUserId()).getBoolean(
1356 UserManager.DISALLOW_REMOVE_USER, false)) {
1357 Log.w(LOG_TAG, "Cannot remove user. DISALLOW_REMOVE_USER is enabled.");
1358 return false;
1359 }
1360
Kenny Guyee58b4f2014-05-23 15:19:53 +01001361 long ident = Binder.clearCallingIdentity();
1362 try {
1363 final UserInfo user;
1364 synchronized (mPackagesLock) {
1365 user = mUsers.get(userHandle);
Kenny Guy17c9d692014-06-13 13:51:42 +01001366 if (userHandle == 0 || user == null || mRemovingUserIds.get(userHandle)) {
Kenny Guyee58b4f2014-05-23 15:19:53 +01001367 return false;
1368 }
Jeff Sharkey6eb09392014-11-14 15:57:59 -08001369
1370 // We remember deleted user IDs to prevent them from being
1371 // reused during the current boot; they can still be reused
1372 // after a reboot.
Kenny Guyee58b4f2014-05-23 15:19:53 +01001373 mRemovingUserIds.put(userHandle, true);
Jeff Sharkey6eb09392014-11-14 15:57:59 -08001374
Kenny Guyee58b4f2014-05-23 15:19:53 +01001375 try {
1376 mAppOpsService.removeUser(userHandle);
1377 } catch (RemoteException e) {
1378 Log.w(LOG_TAG, "Unable to notify AppOpsService of removing user", e);
1379 }
1380 // Set this to a partially created user, so that the user will be purged
1381 // on next startup, in case the runtime stops now before stopping and
1382 // removing the user completely.
1383 user.partial = true;
1384 // Mark it as disabled, so that it isn't returned any more when
1385 // profiles are queried.
1386 user.flags |= UserInfo.FLAG_DISABLED;
1387 writeUserLocked(user);
1388 }
1389
1390 if (user.profileGroupId != UserInfo.NO_PROFILE_GROUP_ID
1391 && user.isManagedProfile()) {
1392 // Send broadcast to notify system that the user removed was a
1393 // managed user.
1394 sendProfileRemovedBroadcast(user.profileGroupId, user.id);
1395 }
1396
1397 if (DBG) Slog.i(LOG_TAG, "Stopping user " + userHandle);
1398 int res;
1399 try {
1400 res = ActivityManagerNative.getDefault().stopUser(userHandle,
1401 new IStopUserCallback.Stub() {
1402 @Override
1403 public void userStopped(int userId) {
1404 finishRemoveUser(userId);
1405 }
1406 @Override
1407 public void userStopAborted(int userId) {
1408 }
1409 });
1410 } catch (RemoteException e) {
Dianne Hackborn80a4af22012-08-27 19:18:31 -07001411 return false;
1412 }
Kenny Guyee58b4f2014-05-23 15:19:53 +01001413 return res == ActivityManager.USER_OP_SUCCESS;
1414 } finally {
1415 Binder.restoreCallingIdentity(ident);
Dianne Hackborn80a4af22012-08-27 19:18:31 -07001416 }
Dianne Hackborn80a4af22012-08-27 19:18:31 -07001417 }
1418
Amith Yamasanidb6a14c2012-10-17 21:16:52 -07001419 void finishRemoveUser(final int userHandle) {
Amith Yamasani16389312012-10-17 21:20:14 -07001420 if (DBG) Slog.i(LOG_TAG, "finishRemoveUser " + userHandle);
Amith Yamasanidb6a14c2012-10-17 21:16:52 -07001421 // Let other services shutdown any activity and clean up their state before completely
1422 // wiping the user's system directory and removing from the user list
Dianne Hackborn5dc5a002012-09-15 19:33:48 -07001423 long ident = Binder.clearCallingIdentity();
1424 try {
1425 Intent addedIntent = new Intent(Intent.ACTION_USER_REMOVED);
1426 addedIntent.putExtra(Intent.EXTRA_USER_HANDLE, userHandle);
Amith Yamasanidb6a14c2012-10-17 21:16:52 -07001427 mContext.sendOrderedBroadcastAsUser(addedIntent, UserHandle.ALL,
1428 android.Manifest.permission.MANAGE_USERS,
1429
1430 new BroadcastReceiver() {
1431 @Override
1432 public void onReceive(Context context, Intent intent) {
1433 if (DBG) {
1434 Slog.i(LOG_TAG,
1435 "USER_REMOVED broadcast sent, cleaning up user data "
1436 + userHandle);
1437 }
1438 new Thread() {
1439 public void run() {
1440 synchronized (mInstallLock) {
1441 synchronized (mPackagesLock) {
1442 removeUserStateLocked(userHandle);
1443 }
1444 }
1445 }
1446 }.start();
1447 }
1448 },
1449
1450 null, Activity.RESULT_OK, null, null);
Dianne Hackborn5dc5a002012-09-15 19:33:48 -07001451 } finally {
1452 Binder.restoreCallingIdentity(ident);
1453 }
Amith Yamasani2a003292012-08-14 18:25:45 -07001454 }
1455
Jeff Sharkeyffe0cb42012-11-05 17:24:43 -08001456 private void removeUserStateLocked(final int userHandle) {
Dianne Hackbornd4ac8d72012-09-27 23:20:10 -07001457 // Cleanup package manager settings
Amith Yamasanidda003f2014-08-28 18:06:51 -07001458 mPm.cleanUpUserLILPw(this, userHandle);
Dianne Hackbornd4ac8d72012-09-27 23:20:10 -07001459
1460 // Remove this user from the list
1461 mUsers.remove(userHandle);
Dianne Hackbornd4ac8d72012-09-27 23:20:10 -07001462 // Remove user file
Amith Yamasanifc95e702013-09-26 13:20:17 -07001463 AtomicFile userFile = new AtomicFile(new File(mUsersDir, userHandle + XML_SUFFIX));
Dianne Hackbornd4ac8d72012-09-27 23:20:10 -07001464 userFile.delete();
1465 // Update the user list
1466 writeUserListLocked();
1467 updateUserIdsLocked();
1468 removeDirectoryRecursive(Environment.getUserSystemDirectory(userHandle));
1469 }
1470
Amith Yamasani61f57372012-08-31 12:12:28 -07001471 private void removeDirectoryRecursive(File parent) {
1472 if (parent.isDirectory()) {
1473 String[] files = parent.list();
1474 for (String filename : files) {
1475 File child = new File(parent, filename);
1476 removeDirectoryRecursive(child);
1477 }
1478 }
1479 parent.delete();
1480 }
1481
Kenny Guyf8d3a232014-05-15 16:09:52 +01001482 private void sendProfileRemovedBroadcast(int parentUserId, int removedUserId) {
Adam Connors7b66ed52014-04-14 11:58:10 +01001483 Intent managedProfileIntent = new Intent(Intent.ACTION_MANAGED_PROFILE_REMOVED);
Adam Connorsd4b584e2014-06-09 13:55:47 +01001484 managedProfileIntent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY |
1485 Intent.FLAG_RECEIVER_FOREGROUND);
Kenny Guyf8d3a232014-05-15 16:09:52 +01001486 managedProfileIntent.putExtra(Intent.EXTRA_USER, new UserHandle(removedUserId));
1487 mContext.sendBroadcastAsUser(managedProfileIntent, new UserHandle(parentUserId), null);
Adam Connors7b66ed52014-04-14 11:58:10 +01001488 }
1489
Amith Yamasani2a003292012-08-14 18:25:45 -07001490 @Override
Amith Yamasani7e99bc02013-04-16 18:24:51 -07001491 public Bundle getApplicationRestrictions(String packageName) {
1492 return getApplicationRestrictionsForUser(packageName, UserHandle.getCallingUserId());
1493 }
1494
1495 @Override
1496 public Bundle getApplicationRestrictionsForUser(String packageName, int userId) {
Amith Yamasanidf2e92a2013-03-01 17:04:38 -08001497 if (UserHandle.getCallingUserId() != userId
Amith Yamasani9429afb2013-04-10 18:40:51 -07001498 || !UserHandle.isSameApp(Binder.getCallingUid(), getUidForPackage(packageName))) {
Amith Yamasanibe465322014-04-24 13:45:17 -07001499 checkManageUsersPermission("Only system can get restrictions for other users/apps");
Amith Yamasanidf2e92a2013-03-01 17:04:38 -08001500 }
1501 synchronized (mPackagesLock) {
1502 // Read the restrictions from XML
1503 return readApplicationRestrictionsLocked(packageName, userId);
1504 }
1505 }
1506
1507 @Override
Amith Yamasani7e99bc02013-04-16 18:24:51 -07001508 public void setApplicationRestrictions(String packageName, Bundle restrictions,
Amith Yamasanidf2e92a2013-03-01 17:04:38 -08001509 int userId) {
1510 if (UserHandle.getCallingUserId() != userId
Amith Yamasani9429afb2013-04-10 18:40:51 -07001511 || !UserHandle.isSameApp(Binder.getCallingUid(), getUidForPackage(packageName))) {
Amith Yamasanibe465322014-04-24 13:45:17 -07001512 checkManageUsersPermission("Only system can set restrictions for other users/apps");
Amith Yamasanidf2e92a2013-03-01 17:04:38 -08001513 }
1514 synchronized (mPackagesLock) {
Kenny Guyd21b2182014-07-17 16:38:55 +01001515 if (restrictions == null || restrictions.isEmpty()) {
1516 cleanAppRestrictionsForPackage(packageName, userId);
1517 } else {
1518 // Write the restrictions to XML
1519 writeApplicationRestrictionsLocked(packageName, restrictions, userId);
1520 }
Amith Yamasanidf2e92a2013-03-01 17:04:38 -08001521 }
Robin Lee66e5d962014-04-09 16:44:21 +01001522
Kenny Guyd21b2182014-07-17 16:38:55 +01001523 if (isPackageInstalled(packageName, userId)) {
1524 // Notify package of changes via an intent - only sent to explicitly registered receivers.
1525 Intent changeIntent = new Intent(Intent.ACTION_APPLICATION_RESTRICTIONS_CHANGED);
1526 changeIntent.setPackage(packageName);
1527 changeIntent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY);
1528 mContext.sendBroadcastAsUser(changeIntent, new UserHandle(userId));
1529 }
Amith Yamasanidf2e92a2013-03-01 17:04:38 -08001530 }
1531
Amith Yamasani655d0e22013-06-12 14:19:10 -07001532 @Override
Amith Yamasani1a7472e2013-07-02 11:17:30 -07001533 public void removeRestrictions() {
Amith Yamasanibe465322014-04-24 13:45:17 -07001534 checkManageUsersPermission("Only system can remove restrictions");
Amith Yamasani1a7472e2013-07-02 11:17:30 -07001535 final int userHandle = UserHandle.getCallingUserId();
Amith Yamasani5e486f52013-08-07 11:06:44 -07001536 removeRestrictionsForUser(userHandle, true);
Amith Yamasani350962c2013-08-06 11:18:53 -07001537 }
1538
Amith Yamasanie5bcff62014-07-19 15:44:09 -07001539 private void removeRestrictionsForUser(final int userHandle, boolean unhideApps) {
Amith Yamasani1a7472e2013-07-02 11:17:30 -07001540 synchronized (mPackagesLock) {
1541 // Remove all user restrictions
1542 setUserRestrictions(new Bundle(), userHandle);
Amith Yamasani1a7472e2013-07-02 11:17:30 -07001543 // Remove any app restrictions
Kenny Guyd21b2182014-07-17 16:38:55 +01001544 cleanAppRestrictions(userHandle);
Amith Yamasani1a7472e2013-07-02 11:17:30 -07001545 }
Amith Yamasanie5bcff62014-07-19 15:44:09 -07001546 if (unhideApps) {
1547 unhideAllInstalledAppsForUser(userHandle);
Amith Yamasani5e486f52013-08-07 11:06:44 -07001548 }
1549 }
1550
Amith Yamasanie5bcff62014-07-19 15:44:09 -07001551 private void unhideAllInstalledAppsForUser(final int userHandle) {
Amith Yamasani1a7472e2013-07-02 11:17:30 -07001552 mHandler.post(new Runnable() {
1553 @Override
1554 public void run() {
1555 List<ApplicationInfo> apps =
1556 mPm.getInstalledApplications(PackageManager.GET_UNINSTALLED_PACKAGES,
1557 userHandle).getList();
1558 final long ident = Binder.clearCallingIdentity();
1559 try {
1560 for (ApplicationInfo appInfo : apps) {
1561 if ((appInfo.flags & ApplicationInfo.FLAG_INSTALLED) != 0
Alex Klyubinb9f8a522015-02-03 11:12:59 -08001562 && (appInfo.privateFlags & ApplicationInfo.PRIVATE_FLAG_HIDDEN)
1563 != 0) {
Amith Yamasanie5bcff62014-07-19 15:44:09 -07001564 mPm.setApplicationHiddenSettingAsUser(appInfo.packageName, false,
Amith Yamasani1a7472e2013-07-02 11:17:30 -07001565 userHandle);
1566 }
1567 }
1568 } finally {
1569 Binder.restoreCallingIdentity(ident);
1570 }
1571 }
1572 });
1573 }
Amith Yamasanidf2e92a2013-03-01 17:04:38 -08001574 private int getUidForPackage(String packageName) {
Amith Yamasani9429afb2013-04-10 18:40:51 -07001575 long ident = Binder.clearCallingIdentity();
Amith Yamasanidf2e92a2013-03-01 17:04:38 -08001576 try {
1577 return mContext.getPackageManager().getApplicationInfo(packageName,
1578 PackageManager.GET_UNINSTALLED_PACKAGES).uid;
1579 } catch (NameNotFoundException nnfe) {
1580 return -1;
Amith Yamasani9429afb2013-04-10 18:40:51 -07001581 } finally {
1582 Binder.restoreCallingIdentity(ident);
Amith Yamasanidf2e92a2013-03-01 17:04:38 -08001583 }
1584 }
1585
Amith Yamasani7e99bc02013-04-16 18:24:51 -07001586 private Bundle readApplicationRestrictionsLocked(String packageName,
Amith Yamasanidf2e92a2013-03-01 17:04:38 -08001587 int userId) {
Fyodor Kupolov262f9952015-03-23 18:55:11 -07001588 AtomicFile restrictionsFile =
1589 new AtomicFile(new File(Environment.getUserSystemDirectory(userId),
1590 packageToRestrictionsFileName(packageName)));
1591 return readApplicationRestrictionsLocked(restrictionsFile);
1592 }
1593
1594 @VisibleForTesting
1595 static Bundle readApplicationRestrictionsLocked(AtomicFile restrictionsFile) {
Amith Yamasani7e99bc02013-04-16 18:24:51 -07001596 final Bundle restrictions = new Bundle();
Fyodor Kupolov262f9952015-03-23 18:55:11 -07001597 final ArrayList<String> values = new ArrayList<>();
Fyodor Kupolov6f34d362015-04-02 12:42:13 -07001598 if (!restrictionsFile.getBaseFile().exists()) {
Fyodor Kupolovf6ee2242015-04-06 10:15:07 -07001599 return restrictions;
Fyodor Kupolov6f34d362015-04-02 12:42:13 -07001600 }
Amith Yamasanidf2e92a2013-03-01 17:04:38 -08001601
1602 FileInputStream fis = null;
1603 try {
Amith Yamasanidf2e92a2013-03-01 17:04:38 -08001604 fis = restrictionsFile.openRead();
1605 XmlPullParser parser = Xml.newPullParser();
Wojciech Staszkiewicz9e9e2e72015-05-08 14:58:46 +01001606 parser.setInput(fis, StandardCharsets.UTF_8.name());
Fyodor Kupolov262f9952015-03-23 18:55:11 -07001607 XmlUtils.nextElement(parser);
1608 if (parser.getEventType() != XmlPullParser.START_TAG) {
Amith Yamasanidf2e92a2013-03-01 17:04:38 -08001609 Slog.e(LOG_TAG, "Unable to read restrictions file "
1610 + restrictionsFile.getBaseFile());
Amith Yamasani7e99bc02013-04-16 18:24:51 -07001611 return restrictions;
Amith Yamasanidf2e92a2013-03-01 17:04:38 -08001612 }
Fyodor Kupolov262f9952015-03-23 18:55:11 -07001613 while (parser.next() != XmlPullParser.END_DOCUMENT) {
1614 readEntry(restrictions, values, parser);
Amith Yamasanidf2e92a2013-03-01 17:04:38 -08001615 }
Fyodor Kupolov262f9952015-03-23 18:55:11 -07001616 } catch (IOException|XmlPullParserException e) {
1617 Log.w(LOG_TAG, "Error parsing " + restrictionsFile.getBaseFile(), e);
Amith Yamasanidf2e92a2013-03-01 17:04:38 -08001618 } finally {
Fyodor Kupolov262f9952015-03-23 18:55:11 -07001619 IoUtils.closeQuietly(fis);
Amith Yamasanidf2e92a2013-03-01 17:04:38 -08001620 }
Amith Yamasani7e99bc02013-04-16 18:24:51 -07001621 return restrictions;
Amith Yamasanidf2e92a2013-03-01 17:04:38 -08001622 }
1623
Fyodor Kupolov262f9952015-03-23 18:55:11 -07001624 private static void readEntry(Bundle restrictions, ArrayList<String> values,
1625 XmlPullParser parser) throws XmlPullParserException, IOException {
1626 int type = parser.getEventType();
1627 if (type == XmlPullParser.START_TAG && parser.getName().equals(TAG_ENTRY)) {
1628 String key = parser.getAttributeValue(null, ATTR_KEY);
1629 String valType = parser.getAttributeValue(null, ATTR_VALUE_TYPE);
1630 String multiple = parser.getAttributeValue(null, ATTR_MULTIPLE);
1631 if (multiple != null) {
1632 values.clear();
1633 int count = Integer.parseInt(multiple);
1634 while (count > 0 && (type = parser.next()) != XmlPullParser.END_DOCUMENT) {
1635 if (type == XmlPullParser.START_TAG
1636 && parser.getName().equals(TAG_VALUE)) {
1637 values.add(parser.nextText().trim());
1638 count--;
1639 }
1640 }
1641 String [] valueStrings = new String[values.size()];
1642 values.toArray(valueStrings);
1643 restrictions.putStringArray(key, valueStrings);
1644 } else if (ATTR_TYPE_BUNDLE.equals(valType)) {
1645 restrictions.putBundle(key, readBundleEntry(parser, values));
1646 } else if (ATTR_TYPE_BUNDLE_ARRAY.equals(valType)) {
1647 final int outerDepth = parser.getDepth();
1648 ArrayList<Bundle> bundleList = new ArrayList<>();
1649 while (XmlUtils.nextElementWithin(parser, outerDepth)) {
1650 Bundle childBundle = readBundleEntry(parser, values);
1651 bundleList.add(childBundle);
1652 }
1653 restrictions.putParcelableArray(key,
1654 bundleList.toArray(new Bundle[bundleList.size()]));
1655 } else {
1656 String value = parser.nextText().trim();
1657 if (ATTR_TYPE_BOOLEAN.equals(valType)) {
1658 restrictions.putBoolean(key, Boolean.parseBoolean(value));
1659 } else if (ATTR_TYPE_INTEGER.equals(valType)) {
1660 restrictions.putInt(key, Integer.parseInt(value));
1661 } else {
1662 restrictions.putString(key, value);
1663 }
1664 }
1665 }
1666 }
1667
1668 private static Bundle readBundleEntry(XmlPullParser parser, ArrayList<String> values)
1669 throws IOException, XmlPullParserException {
1670 Bundle childBundle = new Bundle();
1671 final int outerDepth = parser.getDepth();
1672 while (XmlUtils.nextElementWithin(parser, outerDepth)) {
1673 readEntry(childBundle, values, parser);
1674 }
1675 return childBundle;
1676 }
1677
Amith Yamasanidf2e92a2013-03-01 17:04:38 -08001678 private void writeApplicationRestrictionsLocked(String packageName,
Amith Yamasani7e99bc02013-04-16 18:24:51 -07001679 Bundle restrictions, int userId) {
Amith Yamasanidf2e92a2013-03-01 17:04:38 -08001680 AtomicFile restrictionsFile = new AtomicFile(
1681 new File(Environment.getUserSystemDirectory(userId),
Amith Yamasanifc95e702013-09-26 13:20:17 -07001682 packageToRestrictionsFileName(packageName)));
Fyodor Kupolov262f9952015-03-23 18:55:11 -07001683 writeApplicationRestrictionsLocked(restrictions, restrictionsFile);
1684 }
1685
1686 @VisibleForTesting
1687 static void writeApplicationRestrictionsLocked(Bundle restrictions,
1688 AtomicFile restrictionsFile) {
1689 FileOutputStream fos = null;
Amith Yamasanidf2e92a2013-03-01 17:04:38 -08001690 try {
1691 fos = restrictionsFile.startWrite();
1692 final BufferedOutputStream bos = new BufferedOutputStream(fos);
1693
Amith Yamasanidf2e92a2013-03-01 17:04:38 -08001694 final XmlSerializer serializer = new FastXmlSerializer();
Wojciech Staszkiewicz9e9e2e72015-05-08 14:58:46 +01001695 serializer.setOutput(bos, StandardCharsets.UTF_8.name());
Amith Yamasanidf2e92a2013-03-01 17:04:38 -08001696 serializer.startDocument(null, true);
1697 serializer.setFeature("http://xmlpull.org/v1/doc/features.html#indent-output", true);
1698
1699 serializer.startTag(null, TAG_RESTRICTIONS);
Fyodor Kupolov262f9952015-03-23 18:55:11 -07001700 writeBundle(restrictions, serializer);
Amith Yamasanidf2e92a2013-03-01 17:04:38 -08001701 serializer.endTag(null, TAG_RESTRICTIONS);
1702
1703 serializer.endDocument();
1704 restrictionsFile.finishWrite(fos);
1705 } catch (Exception e) {
1706 restrictionsFile.failWrite(fos);
Fyodor Kupolov262f9952015-03-23 18:55:11 -07001707 Slog.e(LOG_TAG, "Error writing application restrictions list", e);
1708 }
1709 }
1710
1711 private static void writeBundle(Bundle restrictions, XmlSerializer serializer)
1712 throws IOException {
1713 for (String key : restrictions.keySet()) {
1714 Object value = restrictions.get(key);
1715 serializer.startTag(null, TAG_ENTRY);
1716 serializer.attribute(null, ATTR_KEY, key);
1717
1718 if (value instanceof Boolean) {
1719 serializer.attribute(null, ATTR_VALUE_TYPE, ATTR_TYPE_BOOLEAN);
1720 serializer.text(value.toString());
1721 } else if (value instanceof Integer) {
1722 serializer.attribute(null, ATTR_VALUE_TYPE, ATTR_TYPE_INTEGER);
1723 serializer.text(value.toString());
1724 } else if (value == null || value instanceof String) {
1725 serializer.attribute(null, ATTR_VALUE_TYPE, ATTR_TYPE_STRING);
1726 serializer.text(value != null ? (String) value : "");
1727 } else if (value instanceof Bundle) {
1728 serializer.attribute(null, ATTR_VALUE_TYPE, ATTR_TYPE_BUNDLE);
1729 writeBundle((Bundle) value, serializer);
1730 } else if (value instanceof Parcelable[]) {
1731 serializer.attribute(null, ATTR_VALUE_TYPE, ATTR_TYPE_BUNDLE_ARRAY);
1732 Parcelable[] array = (Parcelable[]) value;
1733 for (Parcelable parcelable : array) {
1734 if (!(parcelable instanceof Bundle)) {
1735 throw new IllegalArgumentException("bundle-array can only hold Bundles");
1736 }
1737 serializer.startTag(null, TAG_ENTRY);
1738 serializer.attribute(null, ATTR_VALUE_TYPE, ATTR_TYPE_BUNDLE);
1739 writeBundle((Bundle) parcelable, serializer);
1740 serializer.endTag(null, TAG_ENTRY);
1741 }
1742 } else {
1743 serializer.attribute(null, ATTR_VALUE_TYPE, ATTR_TYPE_STRING_ARRAY);
1744 String[] values = (String[]) value;
1745 serializer.attribute(null, ATTR_MULTIPLE, Integer.toString(values.length));
1746 for (String choice : values) {
1747 serializer.startTag(null, TAG_VALUE);
1748 serializer.text(choice != null ? choice : "");
1749 serializer.endTag(null, TAG_VALUE);
1750 }
1751 }
1752 serializer.endTag(null, TAG_ENTRY);
Amith Yamasanidf2e92a2013-03-01 17:04:38 -08001753 }
1754 }
1755
1756 @Override
Amith Yamasani2a003292012-08-14 18:25:45 -07001757 public int getUserSerialNumber(int userHandle) {
Dianne Hackborn4428e172012-08-24 17:43:05 -07001758 synchronized (mPackagesLock) {
Amith Yamasani2a003292012-08-14 18:25:45 -07001759 if (!exists(userHandle)) return -1;
Amith Yamasani195263742012-08-21 15:40:12 -07001760 return getUserInfoLocked(userHandle).serialNumber;
Amith Yamasani2a003292012-08-14 18:25:45 -07001761 }
1762 }
1763
1764 @Override
1765 public int getUserHandle(int userSerialNumber) {
Dianne Hackborn4428e172012-08-24 17:43:05 -07001766 synchronized (mPackagesLock) {
Amith Yamasani2a003292012-08-14 18:25:45 -07001767 for (int userId : mUserIds) {
Kenny Guy945f8832015-02-10 15:17:26 +00001768 UserInfo info = getUserInfoLocked(userId);
1769 if (info != null && info.serialNumber == userSerialNumber) return userId;
Amith Yamasani2a003292012-08-14 18:25:45 -07001770 }
1771 // Not found
1772 return -1;
Amith Yamasani13593602012-03-22 16:16:17 -07001773 }
1774 }
1775
Fyodor Kupolovff7233e2015-04-08 11:28:52 -07001776 @Override
1777 public long getUserCreationTime(int userHandle) {
1778 int callingUserId = UserHandle.getCallingUserId();
1779 UserInfo userInfo = null;
1780 synchronized (mPackagesLock) {
1781 if (callingUserId == userHandle) {
1782 userInfo = getUserInfoLocked(userHandle);
1783 } else {
1784 UserInfo parent = getProfileParentLocked(userHandle);
1785 if (parent != null && parent.id == callingUserId) {
1786 userInfo = getUserInfoLocked(userHandle);
1787 }
1788 }
1789 }
1790 if (userInfo == null) {
1791 throw new SecurityException("userHandle can only be the calling user or a managed "
1792 + "profile associated with this user");
1793 }
1794 return userInfo.creationTime;
1795 }
1796
Amith Yamasani0b285492011-04-14 17:35:23 -07001797 /**
1798 * Caches the list of user ids in an array, adjusting the array size when necessary.
1799 */
Amith Yamasani13593602012-03-22 16:16:17 -07001800 private void updateUserIdsLocked() {
Dianne Hackbornd4ac8d72012-09-27 23:20:10 -07001801 int num = 0;
Amith Yamasani0b285492011-04-14 17:35:23 -07001802 for (int i = 0; i < mUsers.size(); i++) {
Dianne Hackbornd4ac8d72012-09-27 23:20:10 -07001803 if (!mUsers.valueAt(i).partial) {
1804 num++;
1805 }
1806 }
Amith Yamasani16389312012-10-17 21:20:14 -07001807 final int[] newUsers = new int[num];
1808 int n = 0;
Dianne Hackbornd4ac8d72012-09-27 23:20:10 -07001809 for (int i = 0; i < mUsers.size(); i++) {
1810 if (!mUsers.valueAt(i).partial) {
Amith Yamasani16389312012-10-17 21:20:14 -07001811 newUsers[n++] = mUsers.keyAt(i);
Dianne Hackbornd4ac8d72012-09-27 23:20:10 -07001812 }
Amith Yamasani0b285492011-04-14 17:35:23 -07001813 }
Dianne Hackborn7767eac2012-08-23 18:25:40 -07001814 mUserIds = newUsers;
Amith Yamasani0b285492011-04-14 17:35:23 -07001815 }
1816
1817 /**
Amith Yamasani1a7472e2013-07-02 11:17:30 -07001818 * Make a note of the last started time of a user and do some cleanup.
Amith Yamasani920ace02012-09-20 22:15:37 -07001819 * @param userId the user that was just foregrounded
1820 */
Amith Yamasani06bf8242015-05-08 16:36:21 -07001821 public void onUserForeground(int userId) {
Amith Yamasani920ace02012-09-20 22:15:37 -07001822 synchronized (mPackagesLock) {
1823 UserInfo user = mUsers.get(userId);
1824 long now = System.currentTimeMillis();
Dianne Hackbornd4ac8d72012-09-27 23:20:10 -07001825 if (user == null || user.partial) {
1826 Slog.w(LOG_TAG, "userForeground: unknown user #" + userId);
1827 return;
1828 }
1829 if (now > EPOCH_PLUS_30_YEARS) {
Amith Yamasani920ace02012-09-20 22:15:37 -07001830 user.lastLoggedInTime = now;
Fyodor Kupolov75d0ea82014-12-15 16:21:07 -08001831 scheduleWriteUserLocked(user);
Amith Yamasani920ace02012-09-20 22:15:37 -07001832 }
1833 }
1834 }
1835
1836 /**
Amith Yamasani0b285492011-04-14 17:35:23 -07001837 * Returns the next available user id, filling in any holes in the ids.
Amith Yamasani742a6712011-05-04 14:49:28 -07001838 * TODO: May not be a good idea to recycle ids, in case it results in confusion
1839 * for data and battery stats collection, or unexpected cross-talk.
Amith Yamasani0b285492011-04-14 17:35:23 -07001840 * @return
1841 */
Dianne Hackborn5dc5a002012-09-15 19:33:48 -07001842 private int getNextAvailableIdLocked() {
Dianne Hackborn4428e172012-08-24 17:43:05 -07001843 synchronized (mPackagesLock) {
Jeff Sharkeyffe0cb42012-11-05 17:24:43 -08001844 int i = MIN_USER_ID;
Amith Yamasani195263742012-08-21 15:40:12 -07001845 while (i < Integer.MAX_VALUE) {
Jeff Sharkeyffe0cb42012-11-05 17:24:43 -08001846 if (mUsers.indexOfKey(i) < 0 && !mRemovingUserIds.get(i)) {
Amith Yamasani195263742012-08-21 15:40:12 -07001847 break;
1848 }
1849 i++;
Amith Yamasani4b2e9342011-03-31 12:38:53 -07001850 }
Amith Yamasani195263742012-08-21 15:40:12 -07001851 return i;
Amith Yamasani4b2e9342011-03-31 12:38:53 -07001852 }
Amith Yamasani4b2e9342011-03-31 12:38:53 -07001853 }
Amith Yamasani920ace02012-09-20 22:15:37 -07001854
Amith Yamasanifc95e702013-09-26 13:20:17 -07001855 private String packageToRestrictionsFileName(String packageName) {
1856 return RESTRICTIONS_FILE_PREFIX + packageName + XML_SUFFIX;
1857 }
1858
Amith Yamasani920ace02012-09-20 22:15:37 -07001859 @Override
1860 protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
1861 if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.DUMP)
1862 != PackageManager.PERMISSION_GRANTED) {
1863 pw.println("Permission Denial: can't dump UserManager from from pid="
1864 + Binder.getCallingPid()
1865 + ", uid=" + Binder.getCallingUid()
1866 + " without permission "
1867 + android.Manifest.permission.DUMP);
1868 return;
1869 }
1870
1871 long now = System.currentTimeMillis();
1872 StringBuilder sb = new StringBuilder();
1873 synchronized (mPackagesLock) {
1874 pw.println("Users:");
1875 for (int i = 0; i < mUsers.size(); i++) {
1876 UserInfo user = mUsers.valueAt(i);
1877 if (user == null) continue;
Amith Yamasani634cf312012-10-04 17:34:21 -07001878 pw.print(" "); pw.print(user); pw.print(" serialNo="); pw.print(user.serialNumber);
Jeff Sharkeyffe0cb42012-11-05 17:24:43 -08001879 if (mRemovingUserIds.get(mUsers.keyAt(i))) pw.print(" <removing> ");
Dianne Hackbornd4ac8d72012-09-27 23:20:10 -07001880 if (user.partial) pw.print(" <partial>");
1881 pw.println();
Amith Yamasani920ace02012-09-20 22:15:37 -07001882 pw.print(" Created: ");
1883 if (user.creationTime == 0) {
1884 pw.println("<unknown>");
1885 } else {
1886 sb.setLength(0);
1887 TimeUtils.formatDuration(now - user.creationTime, sb);
1888 sb.append(" ago");
1889 pw.println(sb);
1890 }
1891 pw.print(" Last logged in: ");
1892 if (user.lastLoggedInTime == 0) {
1893 pw.println("<unknown>");
1894 } else {
1895 sb.setLength(0);
1896 TimeUtils.formatDuration(now - user.lastLoggedInTime, sb);
1897 sb.append(" ago");
1898 pw.println(sb);
1899 }
1900 }
1901 }
1902 }
Fyodor Kupolov75d0ea82014-12-15 16:21:07 -08001903
1904 final class MainHandler extends Handler {
1905
1906 @Override
1907 public void handleMessage(Message msg) {
1908 switch (msg.what) {
1909 case WRITE_USER_MSG:
1910 removeMessages(WRITE_USER_MSG, msg.obj);
1911 synchronized (mPackagesLock) {
1912 int userId = ((UserInfo) msg.obj).id;
1913 UserInfo userInfo = mUsers.get(userId);
1914 if (userInfo != null) {
1915 writeUserLocked(userInfo);
1916 }
1917 }
1918 }
1919 }
1920 }
Amith Yamasani4b2e9342011-03-31 12:38:53 -07001921}