blob: b1319cfe93aeb895234672361b3628ec106754d0 [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;
Paul Crowley85e4e812015-05-19 12:42:00 +010045import android.os.storage.StorageManager;
Amith Yamasani2a003292012-08-14 18:25:45 -070046import android.util.AtomicFile;
Amith Yamasani655d0e22013-06-12 14:19:10 -070047import android.util.Log;
Amith Yamasani4b2e9342011-03-31 12:38:53 -070048import android.util.Slog;
49import android.util.SparseArray;
Jeff Sharkeyffe0cb42012-11-05 17:24:43 -080050import android.util.SparseBooleanArray;
Amith Yamasani920ace02012-09-20 22:15:37 -070051import android.util.TimeUtils;
Amith Yamasani4b2e9342011-03-31 12:38:53 -070052import android.util.Xml;
53
Fyodor Kupolovb5013302015-04-17 17:59:14 -070054import com.google.android.collect.Sets;
55
Fyodor Kupolov262f9952015-03-23 18:55:11 -070056import com.android.internal.annotations.VisibleForTesting;
Jason Monk62062992014-05-06 09:55:28 -040057import com.android.internal.app.IAppOpsService;
Jeff Sharkeyffe0cb42012-11-05 17:24:43 -080058import com.android.internal.util.ArrayUtils;
59import com.android.internal.util.FastXmlSerializer;
Fyodor Kupolov262f9952015-03-23 18:55:11 -070060import com.android.internal.util.XmlUtils;
Jeff Sharkeyffe0cb42012-11-05 17:24:43 -080061
62import org.xmlpull.v1.XmlPullParser;
63import org.xmlpull.v1.XmlPullParserException;
64import org.xmlpull.v1.XmlSerializer;
65
Amith Yamasani4b2e9342011-03-31 12:38:53 -070066import java.io.BufferedOutputStream;
67import java.io.File;
Amith Yamasani920ace02012-09-20 22:15:37 -070068import java.io.FileDescriptor;
Amith Yamasani4b2e9342011-03-31 12:38:53 -070069import java.io.FileInputStream;
Amith Yamasanib8151ec2012-04-18 18:02:48 -070070import java.io.FileNotFoundException;
Amith Yamasani4b2e9342011-03-31 12:38:53 -070071import java.io.FileOutputStream;
72import java.io.IOException;
Amith Yamasani920ace02012-09-20 22:15:37 -070073import java.io.PrintWriter;
Wojciech Staszkiewicz9e9e2e72015-05-08 14:58:46 +010074import java.nio.charset.StandardCharsets;
Amith Yamasani4b2e9342011-03-31 12:38:53 -070075import java.util.ArrayList;
76import java.util.List;
Fyodor Kupolovb5013302015-04-17 17:59:14 -070077import java.util.Set;
Amith Yamasani4b2e9342011-03-31 12:38:53 -070078
Fyodor Kupolov262f9952015-03-23 18:55:11 -070079import libcore.io.IoUtils;
80
Amith Yamasani258848d2012-08-10 17:06:33 -070081public class UserManagerService extends IUserManager.Stub {
Amith Yamasanib8151ec2012-04-18 18:02:48 -070082
Amith Yamasani2a003292012-08-14 18:25:45 -070083 private static final String LOG_TAG = "UserManagerService";
Amith Yamasanib8151ec2012-04-18 18:02:48 -070084
Amith Yamasani16389312012-10-17 21:20:14 -070085 private static final boolean DBG = false;
86
Amith Yamasani4b2e9342011-03-31 12:38:53 -070087 private static final String TAG_NAME = "name";
Amith Yamasani4b2e9342011-03-31 12:38:53 -070088 private static final String ATTR_FLAGS = "flags";
Amith Yamasanib8151ec2012-04-18 18:02:48 -070089 private static final String ATTR_ICON_PATH = "icon";
Amith Yamasani4b2e9342011-03-31 12:38:53 -070090 private static final String ATTR_ID = "id";
Amith Yamasani920ace02012-09-20 22:15:37 -070091 private static final String ATTR_CREATION_TIME = "created";
92 private static final String ATTR_LAST_LOGGED_IN_TIME = "lastLoggedIn";
Amith Yamasani2a003292012-08-14 18:25:45 -070093 private static final String ATTR_SERIAL_NO = "serialNumber";
94 private static final String ATTR_NEXT_SERIAL_NO = "nextSerialNumber";
Dianne Hackbornd4ac8d72012-09-27 23:20:10 -070095 private static final String ATTR_PARTIAL = "partial";
Adam Lesinskieddeb492014-09-08 17:50:03 -070096 private static final String ATTR_GUEST_TO_REMOVE = "guestToRemove";
Amith Yamasani6f34b412012-10-22 18:19:27 -070097 private static final String ATTR_USER_VERSION = "version";
Kenny Guy2a764942014-04-02 13:29:20 +010098 private static final String ATTR_PROFILE_GROUP_ID = "profileGroupId";
Amith Yamasanie4afaa32014-06-30 14:55:07 +053099 private static final String TAG_GUEST_RESTRICTIONS = "guestRestrictions";
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700100 private static final String TAG_USERS = "users";
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700101 private static final String TAG_USER = "user";
Amith Yamasanie4cf7342012-12-17 11:12:09 -0800102 private static final String TAG_RESTRICTIONS = "restrictions";
Amith Yamasanidf2e92a2013-03-01 17:04:38 -0800103 private static final String TAG_ENTRY = "entry";
104 private static final String TAG_VALUE = "value";
105 private static final String ATTR_KEY = "key";
Amith Yamasani7e99bc02013-04-16 18:24:51 -0700106 private static final String ATTR_VALUE_TYPE = "type";
Amith Yamasanidf2e92a2013-03-01 17:04:38 -0800107 private static final String ATTR_MULTIPLE = "m";
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700108
Amith Yamasani7e99bc02013-04-16 18:24:51 -0700109 private static final String ATTR_TYPE_STRING_ARRAY = "sa";
110 private static final String ATTR_TYPE_STRING = "s";
111 private static final String ATTR_TYPE_BOOLEAN = "b";
Amith Yamasani5b5aa402014-06-01 20:10:14 -0700112 private static final String ATTR_TYPE_INTEGER = "i";
Fyodor Kupolov262f9952015-03-23 18:55:11 -0700113 private static final String ATTR_TYPE_BUNDLE = "B";
114 private static final String ATTR_TYPE_BUNDLE_ARRAY = "BA";
Amith Yamasani7e99bc02013-04-16 18:24:51 -0700115
Amith Yamasani0b285492011-04-14 17:35:23 -0700116 private static final String USER_INFO_DIR = "system" + File.separator + "users";
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700117 private static final String USER_LIST_FILENAME = "userlist.xml";
Amith Yamasanib8151ec2012-04-18 18:02:48 -0700118 private static final String USER_PHOTO_FILENAME = "photo.png";
Adrian Roos1bdff912015-02-17 15:51:35 +0100119 private static final String USER_PHOTO_FILENAME_TMP = USER_PHOTO_FILENAME + ".tmp";
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700120
Amith Yamasanidf2e92a2013-03-01 17:04:38 -0800121 private static final String RESTRICTIONS_FILE_PREFIX = "res_";
Amith Yamasanifc95e702013-09-26 13:20:17 -0700122 private static final String XML_SUFFIX = ".xml";
Amith Yamasanidf2e92a2013-03-01 17:04:38 -0800123
Amith Yamasani634cf312012-10-04 17:34:21 -0700124 private static final int MIN_USER_ID = 10;
125
Amith Yamasaniaa6634e2014-10-06 14:20:28 -0700126 private static final int USER_VERSION = 5;
Amith Yamasani6f34b412012-10-22 18:19:27 -0700127
Amith Yamasani920ace02012-09-20 22:15:37 -0700128 private static final long EPOCH_PLUS_30_YEARS = 30L * 365 * 24 * 60 * 60 * 1000L; // ms
129
Amith Yamasani95ab7842014-08-11 17:09:26 -0700130 // Maximum number of managed profiles permitted is 1. This cannot be increased
131 // without first making sure that the rest of the framework is prepared for it.
132 private static final int MAX_MANAGED_PROFILES = 1;
133
Fyodor Kupolovb5013302015-04-17 17:59:14 -0700134 // Set of user restrictions, which can only be enforced by the system
135 private static final Set<String> SYSTEM_CONTROLLED_RESTRICTIONS = Sets.newArraySet(
136 UserManager.DISALLOW_RECORD_AUDIO);
137
Fyodor Kupolov75d0ea82014-12-15 16:21:07 -0800138 static final int WRITE_USER_MSG = 1;
139 static final int WRITE_USER_DELAY = 2*1000; // 2 seconds
Amith Yamasanie4afaa32014-06-30 14:55:07 +0530140
Dianne Hackborn4428e172012-08-24 17:43:05 -0700141 private final Context mContext;
142 private final PackageManagerService mPm;
143 private final Object mInstallLock;
144 private final Object mPackagesLock;
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700145
Jeff Sharkeyffe0cb42012-11-05 17:24:43 -0800146 private final Handler mHandler;
147
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700148 private final File mUsersDir;
149 private final File mUserListFile;
Dianne Hackborn4428e172012-08-24 17:43:05 -0700150 private final File mBaseUserPath;
151
Jeff Sharkeyffe0cb42012-11-05 17:24:43 -0800152 private final SparseArray<UserInfo> mUsers = new SparseArray<UserInfo>();
Amith Yamasanie4cf7342012-12-17 11:12:09 -0800153 private final SparseArray<Bundle> mUserRestrictions = new SparseArray<Bundle>();
Amith Yamasanie4afaa32014-06-30 14:55:07 +0530154 private final Bundle mGuestRestrictions = new Bundle();
Jeff Sharkeyffe0cb42012-11-05 17:24:43 -0800155
156 /**
157 * Set of user IDs being actively removed. Removed IDs linger in this set
158 * for several seconds to work around a VFS caching issue.
159 */
160 // @GuardedBy("mPackagesLock")
161 private final SparseBooleanArray mRemovingUserIds = new SparseBooleanArray();
Dianne Hackborn4428e172012-08-24 17:43:05 -0700162
Amith Yamasani0b285492011-04-14 17:35:23 -0700163 private int[] mUserIds;
Amith Yamasani2a003292012-08-14 18:25:45 -0700164 private int mNextSerialNumber;
Amith Yamasani6f34b412012-10-22 18:19:27 -0700165 private int mUserVersion = 0;
Amith Yamasani0b285492011-04-14 17:35:23 -0700166
Jason Monk62062992014-05-06 09:55:28 -0400167 private IAppOpsService mAppOpsService;
168
Amith Yamasani258848d2012-08-10 17:06:33 -0700169 private static UserManagerService sInstance;
Amith Yamasani258848d2012-08-10 17:06:33 -0700170
Dianne Hackborn4428e172012-08-24 17:43:05 -0700171 public static UserManagerService getInstance() {
172 synchronized (UserManagerService.class) {
173 return sInstance;
Amith Yamasani258848d2012-08-10 17:06:33 -0700174 }
Amith Yamasani258848d2012-08-10 17:06:33 -0700175 }
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700176
177 /**
178 * Available for testing purposes.
179 */
Amith Yamasani258848d2012-08-10 17:06:33 -0700180 UserManagerService(File dataDir, File baseUserPath) {
Dianne Hackborn4428e172012-08-24 17:43:05 -0700181 this(null, null, new Object(), new Object(), dataDir, baseUserPath);
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700182 }
183
Dianne Hackborn4428e172012-08-24 17:43:05 -0700184 /**
185 * Called by package manager to create the service. This is closely
186 * associated with the package manager, and the given lock is the
187 * package manager's own lock.
188 */
189 UserManagerService(Context context, PackageManagerService pm,
190 Object installLock, Object packagesLock) {
191 this(context, pm, installLock, packagesLock,
192 Environment.getDataDirectory(),
193 new File(Environment.getDataDirectory(), "user"));
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700194 }
195
Dianne Hackborn4428e172012-08-24 17:43:05 -0700196 /**
197 * Available for testing purposes.
198 */
199 private UserManagerService(Context context, PackageManagerService pm,
200 Object installLock, Object packagesLock,
201 File dataDir, File baseUserPath) {
Dianne Hackbornd4ac8d72012-09-27 23:20:10 -0700202 mContext = context;
203 mPm = pm;
204 mInstallLock = installLock;
205 mPackagesLock = packagesLock;
Fyodor Kupolov75d0ea82014-12-15 16:21:07 -0800206 mHandler = new MainHandler();
Dianne Hackbornd4ac8d72012-09-27 23:20:10 -0700207 synchronized (mInstallLock) {
208 synchronized (mPackagesLock) {
209 mUsersDir = new File(dataDir, USER_INFO_DIR);
210 mUsersDir.mkdirs();
211 // Make zeroth user directory, for services to migrate their files to that location
212 File userZeroDir = new File(mUsersDir, "0");
213 userZeroDir.mkdirs();
214 mBaseUserPath = baseUserPath;
215 FileUtils.setPermissions(mUsersDir.toString(),
216 FileUtils.S_IRWXU|FileUtils.S_IRWXG
217 |FileUtils.S_IROTH|FileUtils.S_IXOTH,
218 -1, -1);
219 mUserListFile = new File(mUsersDir, USER_LIST_FILENAME);
Amith Yamasanida0b1682014-11-21 12:58:17 -0800220 initDefaultGuestRestrictions();
Dianne Hackbornd4ac8d72012-09-27 23:20:10 -0700221 readUserListLocked();
Xiaohui Chen4be96e42015-05-06 09:55:43 -0700222 sInstance = this;
223 }
224 }
225 }
226
227 void systemReady() {
228 synchronized (mInstallLock) {
229 synchronized (mPackagesLock) {
Amith Yamasani756901d2012-10-12 12:30:07 -0700230 // Prune out any partially created/partially removed users.
Dianne Hackbornd4ac8d72012-09-27 23:20:10 -0700231 ArrayList<UserInfo> partials = new ArrayList<UserInfo>();
232 for (int i = 0; i < mUsers.size(); i++) {
233 UserInfo ui = mUsers.valueAt(i);
Adam Lesinskieddeb492014-09-08 17:50:03 -0700234 if ((ui.partial || ui.guestToRemove) && i != 0) {
Dianne Hackbornd4ac8d72012-09-27 23:20:10 -0700235 partials.add(ui);
236 }
237 }
238 for (int i = 0; i < partials.size(); i++) {
239 UserInfo ui = partials.get(i);
240 Slog.w(LOG_TAG, "Removing partially created user #" + i
241 + " (name=" + ui.name + ")");
242 removeUserStateLocked(ui.id);
243 }
Dianne Hackbornd4ac8d72012-09-27 23:20:10 -0700244 }
Dianne Hackborn4428e172012-08-24 17:43:05 -0700245 }
Amith Yamasani06bf8242015-05-08 16:36:21 -0700246 onUserForeground(UserHandle.USER_OWNER);
Jason Monk62062992014-05-06 09:55:28 -0400247 mAppOpsService = IAppOpsService.Stub.asInterface(
248 ServiceManager.getService(Context.APP_OPS_SERVICE));
249 for (int i = 0; i < mUserIds.length; ++i) {
250 try {
251 mAppOpsService.setUserRestrictions(mUserRestrictions.get(mUserIds[i]), mUserIds[i]);
252 } catch (RemoteException e) {
253 Log.w(LOG_TAG, "Unable to notify AppOpsService of UserRestrictions");
254 }
255 }
Amith Yamasani258848d2012-08-10 17:06:33 -0700256 }
257
258 @Override
Amith Yamasani920ace02012-09-20 22:15:37 -0700259 public List<UserInfo> getUsers(boolean excludeDying) {
Amith Yamasani2a003292012-08-14 18:25:45 -0700260 checkManageUsersPermission("query users");
Dianne Hackborn4428e172012-08-24 17:43:05 -0700261 synchronized (mPackagesLock) {
Amith Yamasani13593602012-03-22 16:16:17 -0700262 ArrayList<UserInfo> users = new ArrayList<UserInfo>(mUsers.size());
263 for (int i = 0; i < mUsers.size(); i++) {
Dianne Hackbornd4ac8d72012-09-27 23:20:10 -0700264 UserInfo ui = mUsers.valueAt(i);
265 if (ui.partial) {
266 continue;
267 }
Jeff Sharkeyffe0cb42012-11-05 17:24:43 -0800268 if (!excludeDying || !mRemovingUserIds.get(ui.id)) {
Dianne Hackbornd4ac8d72012-09-27 23:20:10 -0700269 users.add(ui);
Amith Yamasani920ace02012-09-20 22:15:37 -0700270 }
Amith Yamasani13593602012-03-22 16:16:17 -0700271 }
272 return users;
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700273 }
Amith Yamasani13593602012-03-22 16:16:17 -0700274 }
275
Amith Yamasani258848d2012-08-10 17:06:33 -0700276 @Override
Alexandra Gherghina385124d2014-04-03 13:37:39 +0100277 public List<UserInfo> getProfiles(int userId, boolean enabledOnly) {
Amith Yamasani4f582632014-02-19 14:31:52 -0800278 if (userId != UserHandle.getCallingUserId()) {
279 checkManageUsersPermission("getting profiles related to user " + userId);
280 }
Amith Yamasanibe465322014-04-24 13:45:17 -0700281 final long ident = Binder.clearCallingIdentity();
282 try {
283 synchronized (mPackagesLock) {
284 return getProfilesLocked(userId, enabledOnly);
Alexandra Gherghina385124d2014-04-03 13:37:39 +0100285 }
Amith Yamasanibe465322014-04-24 13:45:17 -0700286 } finally {
287 Binder.restoreCallingIdentity(ident);
Kenny Guya52dc3e2014-02-11 15:33:14 +0000288 }
289 }
290
Amith Yamasanibe465322014-04-24 13:45:17 -0700291 /** Assume permissions already checked and caller's identity cleared */
292 private List<UserInfo> getProfilesLocked(int userId, boolean enabledOnly) {
Amith Yamasanibe465322014-04-24 13:45:17 -0700293 UserInfo user = getUserInfoLocked(userId);
294 ArrayList<UserInfo> users = new ArrayList<UserInfo>(mUsers.size());
Amith Yamasanidda003f2014-08-28 18:06:51 -0700295 if (user == null) {
296 // Probably a dying user
297 return users;
298 }
Amith Yamasanibe465322014-04-24 13:45:17 -0700299 for (int i = 0; i < mUsers.size(); i++) {
300 UserInfo profile = mUsers.valueAt(i);
301 if (!isProfileOf(user, profile)) {
302 continue;
303 }
Alexandra Gherghinadf35d572014-04-09 13:54:39 +0100304 if (enabledOnly && !profile.isEnabled()) {
305 continue;
Amith Yamasanibe465322014-04-24 13:45:17 -0700306 }
Amith Yamasani70fcf0c2014-07-11 08:40:19 -0700307 if (mRemovingUserIds.get(profile.id)) {
308 continue;
309 }
Amith Yamasanibe465322014-04-24 13:45:17 -0700310 users.add(profile);
311 }
312 return users;
313 }
314
Jessica Hummelbe81c802014-04-22 15:49:22 +0100315 @Override
316 public UserInfo getProfileParent(int userHandle) {
317 checkManageUsersPermission("get the profile parent");
318 synchronized (mPackagesLock) {
Fyodor Kupolovff7233e2015-04-08 11:28:52 -0700319 return getProfileParentLocked(userHandle);
320 }
321 }
322
323 private UserInfo getProfileParentLocked(int userHandle) {
324 UserInfo profile = getUserInfoLocked(userHandle);
325 if (profile == null) {
326 return null;
327 }
328 int parentUserId = profile.profileGroupId;
329 if (parentUserId == UserInfo.NO_PROFILE_GROUP_ID) {
330 return null;
331 } else {
332 return getUserInfoLocked(parentUserId);
Jessica Hummelbe81c802014-04-22 15:49:22 +0100333 }
334 }
335
Kenny Guy2a764942014-04-02 13:29:20 +0100336 private boolean isProfileOf(UserInfo user, UserInfo profile) {
337 return user.id == profile.id ||
338 (user.profileGroupId != UserInfo.NO_PROFILE_GROUP_ID
339 && user.profileGroupId == profile.profileGroupId);
Kenny Guya52dc3e2014-02-11 15:33:14 +0000340 }
341
342 @Override
Alexandra Gherghinadf35d572014-04-09 13:54:39 +0100343 public void setUserEnabled(int userId) {
344 checkManageUsersPermission("enable user");
345 synchronized (mPackagesLock) {
346 UserInfo info = getUserInfoLocked(userId);
347 if (info != null && !info.isEnabled()) {
348 info.flags ^= UserInfo.FLAG_DISABLED;
349 writeUserLocked(info);
350 }
351 }
352 }
353
354 @Override
Amith Yamasani258848d2012-08-10 17:06:33 -0700355 public UserInfo getUserInfo(int userId) {
Amith Yamasani2a003292012-08-14 18:25:45 -0700356 checkManageUsersPermission("query user");
Dianne Hackborn4428e172012-08-24 17:43:05 -0700357 synchronized (mPackagesLock) {
Amith Yamasani195263742012-08-21 15:40:12 -0700358 return getUserInfoLocked(userId);
Amith Yamasani13593602012-03-22 16:16:17 -0700359 }
360 }
361
Amith Yamasani71e6c692013-03-24 17:39:28 -0700362 @Override
363 public boolean isRestricted() {
364 synchronized (mPackagesLock) {
365 return getUserInfoLocked(UserHandle.getCallingUserId()).isRestricted();
366 }
367 }
368
Amith Yamasani195263742012-08-21 15:40:12 -0700369 /*
370 * Should be locked on mUsers before calling this.
371 */
372 private UserInfo getUserInfoLocked(int userId) {
Dianne Hackbornd4ac8d72012-09-27 23:20:10 -0700373 UserInfo ui = mUsers.get(userId);
Amith Yamasani16389312012-10-17 21:20:14 -0700374 // If it is partial and not in the process of being removed, return as unknown user.
Jeff Sharkeyffe0cb42012-11-05 17:24:43 -0800375 if (ui != null && ui.partial && !mRemovingUserIds.get(userId)) {
Dianne Hackbornd4ac8d72012-09-27 23:20:10 -0700376 Slog.w(LOG_TAG, "getUserInfo: unknown user #" + userId);
377 return null;
378 }
379 return ui;
Amith Yamasani195263742012-08-21 15:40:12 -0700380 }
381
Amith Yamasani13593602012-03-22 16:16:17 -0700382 public boolean exists(int userId) {
Dianne Hackborn4428e172012-08-24 17:43:05 -0700383 synchronized (mPackagesLock) {
Amith Yamasani13593602012-03-22 16:16:17 -0700384 return ArrayUtils.contains(mUserIds, userId);
385 }
386 }
387
Amith Yamasani258848d2012-08-10 17:06:33 -0700388 @Override
Amith Yamasanib8151ec2012-04-18 18:02:48 -0700389 public void setUserName(int userId, String name) {
Amith Yamasani2a003292012-08-14 18:25:45 -0700390 checkManageUsersPermission("rename users");
Dianne Hackbornd4ac8d72012-09-27 23:20:10 -0700391 boolean changed = false;
Dianne Hackborn4428e172012-08-24 17:43:05 -0700392 synchronized (mPackagesLock) {
Amith Yamasani13593602012-03-22 16:16:17 -0700393 UserInfo info = mUsers.get(userId);
Dianne Hackbornd4ac8d72012-09-27 23:20:10 -0700394 if (info == null || info.partial) {
395 Slog.w(LOG_TAG, "setUserName: unknown user #" + userId);
396 return;
397 }
Amith Yamasani13593602012-03-22 16:16:17 -0700398 if (name != null && !name.equals(info.name)) {
399 info.name = name;
400 writeUserLocked(info);
Dianne Hackbornd4ac8d72012-09-27 23:20:10 -0700401 changed = true;
Amith Yamasani13593602012-03-22 16:16:17 -0700402 }
403 }
Dianne Hackbornd4ac8d72012-09-27 23:20:10 -0700404 if (changed) {
405 sendUserInfoChangedBroadcast(userId);
406 }
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700407 }
408
Amith Yamasani258848d2012-08-10 17:06:33 -0700409 @Override
Amith Yamasanie928d7d2012-09-17 21:46:51 -0700410 public void setUserIcon(int userId, Bitmap bitmap) {
Amith Yamasani2a003292012-08-14 18:25:45 -0700411 checkManageUsersPermission("update users");
Jason Monk9a944532014-07-08 09:31:21 -0400412 long ident = Binder.clearCallingIdentity();
413 try {
414 synchronized (mPackagesLock) {
415 UserInfo info = mUsers.get(userId);
416 if (info == null || info.partial) {
417 Slog.w(LOG_TAG, "setUserIcon: unknown user #" + userId);
418 return;
419 }
420 writeBitmapLocked(info, bitmap);
421 writeUserLocked(info);
Dianne Hackbornd4ac8d72012-09-27 23:20:10 -0700422 }
Jason Monk9a944532014-07-08 09:31:21 -0400423 sendUserInfoChangedBroadcast(userId);
424 } finally {
425 Binder.restoreCallingIdentity(ident);
Amith Yamasanib8151ec2012-04-18 18:02:48 -0700426 }
Amith Yamasanie928d7d2012-09-17 21:46:51 -0700427 }
428
429 private void sendUserInfoChangedBroadcast(int userId) {
430 Intent changedIntent = new Intent(Intent.ACTION_USER_INFO_CHANGED);
431 changedIntent.putExtra(Intent.EXTRA_USER_HANDLE, userId);
432 changedIntent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY);
Amith Yamasani6fc1d4e2013-05-08 16:43:58 -0700433 mContext.sendBroadcastAsUser(changedIntent, UserHandle.ALL);
Amith Yamasanib8151ec2012-04-18 18:02:48 -0700434 }
435
Amith Yamasani258848d2012-08-10 17:06:33 -0700436 @Override
Adrian Roos1bdff912015-02-17 15:51:35 +0100437 public ParcelFileDescriptor getUserIcon(int userId) {
438 String iconPath;
Amith Yamasani3b49f072012-09-17 10:21:43 -0700439 synchronized (mPackagesLock) {
440 UserInfo info = mUsers.get(userId);
Dianne Hackbornd4ac8d72012-09-27 23:20:10 -0700441 if (info == null || info.partial) {
442 Slog.w(LOG_TAG, "getUserIcon: unknown user #" + userId);
443 return null;
444 }
Nicolas Prevot88cc3462014-05-14 14:51:48 +0100445 int callingGroupId = mUsers.get(UserHandle.getCallingUserId()).profileGroupId;
446 if (callingGroupId == UserInfo.NO_PROFILE_GROUP_ID
447 || callingGroupId != info.profileGroupId) {
448 checkManageUsersPermission("get the icon of a user who is not related");
449 }
Dianne Hackbornd4ac8d72012-09-27 23:20:10 -0700450 if (info.iconPath == null) {
451 return null;
452 }
Adrian Roos1bdff912015-02-17 15:51:35 +0100453 iconPath = info.iconPath;
Amith Yamasani3b49f072012-09-17 10:21:43 -0700454 }
Adrian Roos1bdff912015-02-17 15:51:35 +0100455
456 try {
457 return ParcelFileDescriptor.open(
458 new File(iconPath), ParcelFileDescriptor.MODE_READ_ONLY);
459 } catch (FileNotFoundException e) {
460 Log.e(LOG_TAG, "Couldn't find icon file", e);
461 }
462 return null;
Amith Yamasani3b49f072012-09-17 10:21:43 -0700463 }
464
Dianne Hackborn5dc5a002012-09-15 19:33:48 -0700465 public void makeInitialized(int userId) {
466 checkManageUsersPermission("makeInitialized");
467 synchronized (mPackagesLock) {
468 UserInfo info = mUsers.get(userId);
Dianne Hackbornd4ac8d72012-09-27 23:20:10 -0700469 if (info == null || info.partial) {
470 Slog.w(LOG_TAG, "makeInitialized: unknown user #" + userId);
471 }
472 if ((info.flags&UserInfo.FLAG_INITIALIZED) == 0) {
Dianne Hackborn5dc5a002012-09-15 19:33:48 -0700473 info.flags |= UserInfo.FLAG_INITIALIZED;
Fyodor Kupolov75d0ea82014-12-15 16:21:07 -0800474 scheduleWriteUserLocked(info);
Dianne Hackborn5dc5a002012-09-15 19:33:48 -0700475 }
476 }
477 }
478
Amith Yamasaniaa6634e2014-10-06 14:20:28 -0700479 /**
480 * If default guest restrictions haven't been initialized yet, add the basic
481 * restrictions.
482 */
483 private void initDefaultGuestRestrictions() {
484 if (mGuestRestrictions.isEmpty()) {
485 mGuestRestrictions.putBoolean(UserManager.DISALLOW_OUTGOING_CALLS, true);
Amith Yamasanida0b1682014-11-21 12:58:17 -0800486 mGuestRestrictions.putBoolean(UserManager.DISALLOW_SMS, true);
Amith Yamasaniaa6634e2014-10-06 14:20:28 -0700487 }
488 }
489
Amith Yamasanie4cf7342012-12-17 11:12:09 -0800490 @Override
Amith Yamasanie4afaa32014-06-30 14:55:07 +0530491 public Bundle getDefaultGuestRestrictions() {
492 checkManageUsersPermission("getDefaultGuestRestrictions");
493 synchronized (mPackagesLock) {
494 return new Bundle(mGuestRestrictions);
495 }
496 }
497
498 @Override
499 public void setDefaultGuestRestrictions(Bundle restrictions) {
500 checkManageUsersPermission("setDefaultGuestRestrictions");
501 synchronized (mPackagesLock) {
502 mGuestRestrictions.clear();
503 mGuestRestrictions.putAll(restrictions);
504 writeUserListLocked();
505 }
506 }
507
508 @Override
Amith Yamasani8cd28b52014-06-08 17:54:27 -0700509 public boolean hasUserRestriction(String restrictionKey, int userId) {
510 synchronized (mPackagesLock) {
511 Bundle restrictions = mUserRestrictions.get(userId);
Fyodor Kupolovb5013302015-04-17 17:59:14 -0700512 return restrictions != null && restrictions.getBoolean(restrictionKey);
Amith Yamasani8cd28b52014-06-08 17:54:27 -0700513 }
514 }
515
516 @Override
Amith Yamasanie4cf7342012-12-17 11:12:09 -0800517 public Bundle getUserRestrictions(int userId) {
518 // checkManageUsersPermission("getUserRestrictions");
519
520 synchronized (mPackagesLock) {
521 Bundle restrictions = mUserRestrictions.get(userId);
Amith Yamasanibe465322014-04-24 13:45:17 -0700522 return restrictions != null ? new Bundle(restrictions) : new Bundle();
Amith Yamasanie4cf7342012-12-17 11:12:09 -0800523 }
524 }
525
526 @Override
Fyodor Kupolovb5013302015-04-17 17:59:14 -0700527 public void setUserRestriction(String key, boolean value, int userId) {
528 synchronized (mPackagesLock) {
529 if (!SYSTEM_CONTROLLED_RESTRICTIONS.contains(key)) {
530 Bundle restrictions = getUserRestrictions(userId);
531 restrictions.putBoolean(key, value);
532 setUserRestrictionsInternalLocked(restrictions, userId);
533 }
534 }
535 }
536
537 @Override
538 public void setSystemControlledUserRestriction(String key, boolean value, int userId) {
539 checkSystemOrRoot("setSystemControlledUserRestriction");
540 synchronized (mPackagesLock) {
541 Bundle restrictions = getUserRestrictions(userId);
542 restrictions.putBoolean(key, value);
543 setUserRestrictionsInternalLocked(restrictions, userId);
544 }
545 }
546
547 @Override
Amith Yamasanie4cf7342012-12-17 11:12:09 -0800548 public void setUserRestrictions(Bundle restrictions, int userId) {
Amith Yamasanibe465322014-04-24 13:45:17 -0700549 checkManageUsersPermission("setUserRestrictions");
Amith Yamasani0343ec32013-07-22 14:52:06 -0700550 if (restrictions == null) return;
Amith Yamasanie4cf7342012-12-17 11:12:09 -0800551
552 synchronized (mPackagesLock) {
Fyodor Kupolovb5013302015-04-17 17:59:14 -0700553 final Bundle oldUserRestrictions = mUserRestrictions.get(userId);
554 // Restore the original state of system controlled restrictions from oldUserRestrictions
555 for (String key : SYSTEM_CONTROLLED_RESTRICTIONS) {
556 restrictions.remove(key);
557 if (oldUserRestrictions.containsKey(key)) {
558 restrictions.putBoolean(key, oldUserRestrictions.getBoolean(key));
559 }
Jason Monk62062992014-05-06 09:55:28 -0400560 }
Fyodor Kupolovb5013302015-04-17 17:59:14 -0700561 setUserRestrictionsInternalLocked(restrictions, userId);
Amith Yamasanie4cf7342012-12-17 11:12:09 -0800562 }
563 }
564
Fyodor Kupolovb5013302015-04-17 17:59:14 -0700565 private void setUserRestrictionsInternalLocked(Bundle restrictions, int userId) {
566 final Bundle userRestrictions = mUserRestrictions.get(userId);
567 userRestrictions.clear();
568 userRestrictions.putAll(restrictions);
569 long token = Binder.clearCallingIdentity();
570 try {
571 mAppOpsService.setUserRestrictions(userRestrictions, userId);
572 } catch (RemoteException e) {
573 Log.w(LOG_TAG, "Unable to notify AppOpsService of UserRestrictions");
574 } finally {
575 Binder.restoreCallingIdentity(token);
576 }
577 scheduleWriteUserLocked(mUsers.get(userId));
578 }
579
Amith Yamasani258848d2012-08-10 17:06:33 -0700580 /**
Amith Yamasanifaea76f2012-09-11 10:59:48 -0700581 * Check if we've hit the limit of how many users can be created.
582 */
Dianne Hackborn5dc5a002012-09-15 19:33:48 -0700583 private boolean isUserLimitReachedLocked() {
Nicolas Prevot12678a92015-05-13 12:15:03 -0700584 return getAliveUsersExcludingGuestsCountLocked() >= UserManager.getMaxSupportedUsers();
585 }
586
587 @Override
588 public boolean canAddMoreManagedProfiles() {
589 checkManageUsersPermission("check if more managed profiles can be added.");
590 if (ActivityManager.isLowRamDeviceStatic()) {
591 return false;
592 }
Fyodor Kupolovb6157992015-06-05 15:32:28 -0700593 if (!mContext.getPackageManager().hasSystemFeature(
594 PackageManager.FEATURE_MANAGED_USERS)) {
595 return false;
596 }
Nicolas Prevot12678a92015-05-13 12:15:03 -0700597 synchronized(mPackagesLock) {
598 // Limit number of managed profiles that can be created
599 if (numberOfUsersOfTypeLocked(UserInfo.FLAG_MANAGED_PROFILE, true)
600 >= MAX_MANAGED_PROFILES) {
601 return false;
602 }
603 int usersCount = getAliveUsersExcludingGuestsCountLocked();
604 // We allow creating a managed profile in the special case where there is only one user.
605 return usersCount == 1 || usersCount < UserManager.getMaxSupportedUsers();
606 }
607 }
608
609 private int getAliveUsersExcludingGuestsCountLocked() {
Amith Yamasanif584f012014-05-19 17:57:25 -0700610 int aliveUserCount = 0;
611 final int totalUserCount = mUsers.size();
612 // Skip over users being removed
613 for (int i = 0; i < totalUserCount; i++) {
614 UserInfo user = mUsers.valueAt(i);
Amith Yamasani95ab7842014-08-11 17:09:26 -0700615 if (!mRemovingUserIds.get(user.id)
Amith Yamasani1df14732014-08-29 21:37:27 -0700616 && !user.isGuest() && !user.partial) {
Amith Yamasanif584f012014-05-19 17:57:25 -0700617 aliveUserCount++;
618 }
619 }
Nicolas Prevot12678a92015-05-13 12:15:03 -0700620 return aliveUserCount;
Amith Yamasanifaea76f2012-09-11 10:59:48 -0700621 }
622
623 /**
Amith Yamasani195263742012-08-21 15:40:12 -0700624 * Enforces that only the system UID or root's UID or apps that have the
Dianne Hackborn10ad9822014-03-17 11:28:36 -0700625 * {@link android.Manifest.permission#MANAGE_USERS MANAGE_USERS}
Amith Yamasani195263742012-08-21 15:40:12 -0700626 * permission can make certain calls to the UserManager.
Amith Yamasani258848d2012-08-10 17:06:33 -0700627 *
628 * @param message used as message if SecurityException is thrown
629 * @throws SecurityException if the caller is not system or root
630 */
Amith Yamasanibe465322014-04-24 13:45:17 -0700631 private static final void checkManageUsersPermission(String message) {
Amith Yamasani258848d2012-08-10 17:06:33 -0700632 final int uid = Binder.getCallingUid();
Amith Yamasanibe465322014-04-24 13:45:17 -0700633 if (uid != Process.SYSTEM_UID && uid != 0
Emily Bernier7a2b4d12014-04-23 12:51:35 -0400634 && ActivityManager.checkComponentPermission(
635 android.Manifest.permission.MANAGE_USERS,
Amith Yamasanibe465322014-04-24 13:45:17 -0700636 uid, -1, true) != PackageManager.PERMISSION_GRANTED) {
637 throw new SecurityException("You need MANAGE_USERS permission to: " + message);
638 }
Emily Bernier7a2b4d12014-04-23 12:51:35 -0400639 }
640
Fyodor Kupolovb5013302015-04-17 17:59:14 -0700641 private static void checkSystemOrRoot(String message) {
642 final int uid = Binder.getCallingUid();
643 if (uid != Process.SYSTEM_UID && uid != 0) {
644 throw new SecurityException("Only system may call: " + message);
645 }
646 }
647
Amith Yamasanie928d7d2012-09-17 21:46:51 -0700648 private void writeBitmapLocked(UserInfo info, Bitmap bitmap) {
Amith Yamasanib8151ec2012-04-18 18:02:48 -0700649 try {
650 File dir = new File(mUsersDir, Integer.toString(info.id));
651 File file = new File(dir, USER_PHOTO_FILENAME);
Adrian Roos1bdff912015-02-17 15:51:35 +0100652 File tmp = new File(dir, USER_PHOTO_FILENAME_TMP);
Amith Yamasanib8151ec2012-04-18 18:02:48 -0700653 if (!dir.exists()) {
654 dir.mkdir();
655 FileUtils.setPermissions(
656 dir.getPath(),
657 FileUtils.S_IRWXU|FileUtils.S_IRWXG|FileUtils.S_IXOTH,
658 -1, -1);
659 }
Amith Yamasanie928d7d2012-09-17 21:46:51 -0700660 FileOutputStream os;
Adrian Roos1bdff912015-02-17 15:51:35 +0100661 if (bitmap.compress(Bitmap.CompressFormat.PNG, 100, os = new FileOutputStream(tmp))
662 && tmp.renameTo(file)) {
Amith Yamasani3b49f072012-09-17 10:21:43 -0700663 info.iconPath = file.getAbsolutePath();
664 }
Amith Yamasanie928d7d2012-09-17 21:46:51 -0700665 try {
666 os.close();
667 } catch (IOException ioe) {
668 // What the ... !
669 }
Adrian Roos1bdff912015-02-17 15:51:35 +0100670 tmp.delete();
Amith Yamasanib8151ec2012-04-18 18:02:48 -0700671 } catch (FileNotFoundException e) {
Amith Yamasani2a003292012-08-14 18:25:45 -0700672 Slog.w(LOG_TAG, "Error setting photo for user ", e);
Amith Yamasanib8151ec2012-04-18 18:02:48 -0700673 }
Amith Yamasanib8151ec2012-04-18 18:02:48 -0700674 }
675
Amith Yamasani0b285492011-04-14 17:35:23 -0700676 /**
677 * Returns an array of user ids. This array is cached here for quick access, so do not modify or
678 * cache it elsewhere.
679 * @return the array of user ids.
680 */
Dianne Hackborn1676c852012-09-10 14:52:30 -0700681 public int[] getUserIds() {
Dianne Hackborn4428e172012-08-24 17:43:05 -0700682 synchronized (mPackagesLock) {
Dianne Hackborn7767eac2012-08-23 18:25:40 -0700683 return mUserIds;
684 }
Amith Yamasani0b285492011-04-14 17:35:23 -0700685 }
686
Dianne Hackborn4428e172012-08-24 17:43:05 -0700687 int[] getUserIdsLPr() {
688 return mUserIds;
689 }
690
Amith Yamasani13593602012-03-22 16:16:17 -0700691 private void readUserListLocked() {
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700692 if (!mUserListFile.exists()) {
Amith Yamasani13593602012-03-22 16:16:17 -0700693 fallbackToSingleUserLocked();
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700694 return;
695 }
696 FileInputStream fis = null;
Amith Yamasani2a003292012-08-14 18:25:45 -0700697 AtomicFile userListFile = new AtomicFile(mUserListFile);
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700698 try {
Amith Yamasani2a003292012-08-14 18:25:45 -0700699 fis = userListFile.openRead();
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700700 XmlPullParser parser = Xml.newPullParser();
Wojciech Staszkiewicz9e9e2e72015-05-08 14:58:46 +0100701 parser.setInput(fis, StandardCharsets.UTF_8.name());
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700702 int type;
703 while ((type = parser.next()) != XmlPullParser.START_TAG
704 && type != XmlPullParser.END_DOCUMENT) {
705 ;
706 }
707
708 if (type != XmlPullParser.START_TAG) {
Amith Yamasani0b285492011-04-14 17:35:23 -0700709 Slog.e(LOG_TAG, "Unable to read user list");
Amith Yamasani13593602012-03-22 16:16:17 -0700710 fallbackToSingleUserLocked();
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700711 return;
712 }
713
Amith Yamasani2a003292012-08-14 18:25:45 -0700714 mNextSerialNumber = -1;
715 if (parser.getName().equals(TAG_USERS)) {
716 String lastSerialNumber = parser.getAttributeValue(null, ATTR_NEXT_SERIAL_NO);
717 if (lastSerialNumber != null) {
718 mNextSerialNumber = Integer.parseInt(lastSerialNumber);
719 }
Amith Yamasani6f34b412012-10-22 18:19:27 -0700720 String versionNumber = parser.getAttributeValue(null, ATTR_USER_VERSION);
721 if (versionNumber != null) {
722 mUserVersion = Integer.parseInt(versionNumber);
723 }
Amith Yamasani2a003292012-08-14 18:25:45 -0700724 }
725
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700726 while ((type = parser.next()) != XmlPullParser.END_DOCUMENT) {
Amith Yamasanie4afaa32014-06-30 14:55:07 +0530727 if (type == XmlPullParser.START_TAG) {
728 final String name = parser.getName();
729 if (name.equals(TAG_USER)) {
730 String id = parser.getAttributeValue(null, ATTR_ID);
731 UserInfo user = readUserLocked(Integer.parseInt(id));
Amith Yamasani6f34b412012-10-22 18:19:27 -0700732
Amith Yamasanie4afaa32014-06-30 14:55:07 +0530733 if (user != null) {
734 mUsers.put(user.id, user);
735 if (mNextSerialNumber < 0 || mNextSerialNumber <= user.id) {
736 mNextSerialNumber = user.id + 1;
737 }
Amith Yamasani2a003292012-08-14 18:25:45 -0700738 }
Amith Yamasanie4afaa32014-06-30 14:55:07 +0530739 } else if (name.equals(TAG_GUEST_RESTRICTIONS)) {
Amith Yamasanida0b1682014-11-21 12:58:17 -0800740 while ((type = parser.next()) != XmlPullParser.END_DOCUMENT
741 && type != XmlPullParser.END_TAG) {
742 if (type == XmlPullParser.START_TAG) {
743 if (parser.getName().equals(TAG_RESTRICTIONS)) {
744 readRestrictionsLocked(parser, mGuestRestrictions);
745 }
746 break;
747 }
748 }
Amith Yamasani258848d2012-08-10 17:06:33 -0700749 }
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700750 }
751 }
Amith Yamasani13593602012-03-22 16:16:17 -0700752 updateUserIdsLocked();
Amith Yamasani350962c2013-08-06 11:18:53 -0700753 upgradeIfNecessaryLocked();
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700754 } catch (IOException ioe) {
Amith Yamasani13593602012-03-22 16:16:17 -0700755 fallbackToSingleUserLocked();
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700756 } catch (XmlPullParserException pe) {
Amith Yamasani13593602012-03-22 16:16:17 -0700757 fallbackToSingleUserLocked();
Dianne Hackbornbfd89b32011-12-15 18:22:54 -0800758 } finally {
759 if (fis != null) {
760 try {
761 fis.close();
762 } catch (IOException e) {
763 }
764 }
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700765 }
766 }
767
Amith Yamasani6f34b412012-10-22 18:19:27 -0700768 /**
Amith Yamasanibc9625052012-11-15 14:39:18 -0800769 * Upgrade steps between versions, either for fixing bugs or changing the data format.
Amith Yamasani6f34b412012-10-22 18:19:27 -0700770 */
Amith Yamasani350962c2013-08-06 11:18:53 -0700771 private void upgradeIfNecessaryLocked() {
Amith Yamasani6f34b412012-10-22 18:19:27 -0700772 int userVersion = mUserVersion;
773 if (userVersion < 1) {
774 // Assign a proper name for the owner, if not initialized correctly before
775 UserInfo user = mUsers.get(UserHandle.USER_OWNER);
776 if ("Primary".equals(user.name)) {
777 user.name = mContext.getResources().getString(com.android.internal.R.string.owner_name);
Fyodor Kupolov75d0ea82014-12-15 16:21:07 -0800778 scheduleWriteUserLocked(user);
Amith Yamasani6f34b412012-10-22 18:19:27 -0700779 }
780 userVersion = 1;
781 }
782
Amith Yamasanibc9625052012-11-15 14:39:18 -0800783 if (userVersion < 2) {
784 // Owner should be marked as initialized
785 UserInfo user = mUsers.get(UserHandle.USER_OWNER);
786 if ((user.flags & UserInfo.FLAG_INITIALIZED) == 0) {
787 user.flags |= UserInfo.FLAG_INITIALIZED;
Fyodor Kupolov75d0ea82014-12-15 16:21:07 -0800788 scheduleWriteUserLocked(user);
Amith Yamasanibc9625052012-11-15 14:39:18 -0800789 }
790 userVersion = 2;
791 }
792
Amith Yamasani350962c2013-08-06 11:18:53 -0700793
Amith Yamasani5e486f52013-08-07 11:06:44 -0700794 if (userVersion < 4) {
Amith Yamasani5e486f52013-08-07 11:06:44 -0700795 userVersion = 4;
796 }
797
Amith Yamasaniaa6634e2014-10-06 14:20:28 -0700798 if (userVersion < 5) {
799 initDefaultGuestRestrictions();
800 userVersion = 5;
801 }
802
Amith Yamasani6f34b412012-10-22 18:19:27 -0700803 if (userVersion < USER_VERSION) {
804 Slog.w(LOG_TAG, "User version " + mUserVersion + " didn't upgrade as expected to "
805 + USER_VERSION);
806 } else {
807 mUserVersion = userVersion;
808 writeUserListLocked();
809 }
810 }
811
Amith Yamasani13593602012-03-22 16:16:17 -0700812 private void fallbackToSingleUserLocked() {
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700813 // Create the primary user
Amith Yamasani67df64b2012-12-14 12:09:36 -0800814 UserInfo primary = new UserInfo(UserHandle.USER_OWNER,
Amith Yamasani6f34b412012-10-22 18:19:27 -0700815 mContext.getResources().getString(com.android.internal.R.string.owner_name), null,
Amith Yamasani756901d2012-10-12 12:30:07 -0700816 UserInfo.FLAG_ADMIN | UserInfo.FLAG_PRIMARY | UserInfo.FLAG_INITIALIZED);
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700817 mUsers.put(0, primary);
Amith Yamasani634cf312012-10-04 17:34:21 -0700818 mNextSerialNumber = MIN_USER_ID;
Geoffrey Borggaard15b8b2c2013-08-28 22:11:10 -0400819 mUserVersion = USER_VERSION;
Amith Yamasani67df64b2012-12-14 12:09:36 -0800820
Geoffrey Borggaarde45e45e32013-01-24 10:03:20 -0500821 Bundle restrictions = new Bundle();
Amith Yamasani67df64b2012-12-14 12:09:36 -0800822 mUserRestrictions.append(UserHandle.USER_OWNER, restrictions);
823
Amith Yamasani13593602012-03-22 16:16:17 -0700824 updateUserIdsLocked();
Amith Yamasaniaa6634e2014-10-06 14:20:28 -0700825 initDefaultGuestRestrictions();
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700826
Amith Yamasani13593602012-03-22 16:16:17 -0700827 writeUserListLocked();
828 writeUserLocked(primary);
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700829 }
830
Fyodor Kupolov75d0ea82014-12-15 16:21:07 -0800831 private void scheduleWriteUserLocked(UserInfo userInfo) {
832 if (!mHandler.hasMessages(WRITE_USER_MSG, userInfo)) {
833 Message msg = mHandler.obtainMessage(WRITE_USER_MSG, userInfo);
834 mHandler.sendMessageDelayed(msg, WRITE_USER_DELAY);
835 }
836 }
837
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700838 /*
839 * Writes the user file in this format:
840 *
841 * <user flags="20039023" id="0">
842 * <name>Primary</name>
843 * </user>
844 */
Amith Yamasani13593602012-03-22 16:16:17 -0700845 private void writeUserLocked(UserInfo userInfo) {
Amith Yamasani742a6712011-05-04 14:49:28 -0700846 FileOutputStream fos = null;
Amith Yamasanifc95e702013-09-26 13:20:17 -0700847 AtomicFile userFile = new AtomicFile(new File(mUsersDir, userInfo.id + XML_SUFFIX));
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700848 try {
Amith Yamasani2a003292012-08-14 18:25:45 -0700849 fos = userFile.startWrite();
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700850 final BufferedOutputStream bos = new BufferedOutputStream(fos);
851
852 // XmlSerializer serializer = XmlUtils.serializerInstance();
853 final XmlSerializer serializer = new FastXmlSerializer();
Wojciech Staszkiewicz9e9e2e72015-05-08 14:58:46 +0100854 serializer.setOutput(bos, StandardCharsets.UTF_8.name());
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700855 serializer.startDocument(null, true);
856 serializer.setFeature("http://xmlpull.org/v1/doc/features.html#indent-output", true);
857
858 serializer.startTag(null, TAG_USER);
859 serializer.attribute(null, ATTR_ID, Integer.toString(userInfo.id));
Amith Yamasani2a003292012-08-14 18:25:45 -0700860 serializer.attribute(null, ATTR_SERIAL_NO, Integer.toString(userInfo.serialNumber));
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700861 serializer.attribute(null, ATTR_FLAGS, Integer.toString(userInfo.flags));
Amith Yamasani920ace02012-09-20 22:15:37 -0700862 serializer.attribute(null, ATTR_CREATION_TIME, Long.toString(userInfo.creationTime));
863 serializer.attribute(null, ATTR_LAST_LOGGED_IN_TIME,
864 Long.toString(userInfo.lastLoggedInTime));
Amith Yamasanib8151ec2012-04-18 18:02:48 -0700865 if (userInfo.iconPath != null) {
866 serializer.attribute(null, ATTR_ICON_PATH, userInfo.iconPath);
867 }
Dianne Hackbornd4ac8d72012-09-27 23:20:10 -0700868 if (userInfo.partial) {
869 serializer.attribute(null, ATTR_PARTIAL, "true");
870 }
Adam Lesinskieddeb492014-09-08 17:50:03 -0700871 if (userInfo.guestToRemove) {
872 serializer.attribute(null, ATTR_GUEST_TO_REMOVE, "true");
873 }
Kenny Guy2a764942014-04-02 13:29:20 +0100874 if (userInfo.profileGroupId != UserInfo.NO_PROFILE_GROUP_ID) {
875 serializer.attribute(null, ATTR_PROFILE_GROUP_ID,
876 Integer.toString(userInfo.profileGroupId));
Kenny Guya52dc3e2014-02-11 15:33:14 +0000877 }
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700878
879 serializer.startTag(null, TAG_NAME);
880 serializer.text(userInfo.name);
881 serializer.endTag(null, TAG_NAME);
Amith Yamasanie4cf7342012-12-17 11:12:09 -0800882 Bundle restrictions = mUserRestrictions.get(userInfo.id);
883 if (restrictions != null) {
Amith Yamasanie4afaa32014-06-30 14:55:07 +0530884 writeRestrictionsLocked(serializer, restrictions);
Amith Yamasanie4cf7342012-12-17 11:12:09 -0800885 }
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700886 serializer.endTag(null, TAG_USER);
887
888 serializer.endDocument();
Amith Yamasani2a003292012-08-14 18:25:45 -0700889 userFile.finishWrite(fos);
890 } catch (Exception ioe) {
Amith Yamasani0b285492011-04-14 17:35:23 -0700891 Slog.e(LOG_TAG, "Error writing user info " + userInfo.id + "\n" + ioe);
Amith Yamasani2a003292012-08-14 18:25:45 -0700892 userFile.failWrite(fos);
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700893 }
894 }
895
896 /*
897 * Writes the user list file in this format:
898 *
Amith Yamasani2a003292012-08-14 18:25:45 -0700899 * <users nextSerialNumber="3">
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700900 * <user id="0"></user>
901 * <user id="2"></user>
902 * </users>
903 */
Amith Yamasani13593602012-03-22 16:16:17 -0700904 private void writeUserListLocked() {
Amith Yamasani742a6712011-05-04 14:49:28 -0700905 FileOutputStream fos = null;
Amith Yamasani2a003292012-08-14 18:25:45 -0700906 AtomicFile userListFile = new AtomicFile(mUserListFile);
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700907 try {
Amith Yamasani2a003292012-08-14 18:25:45 -0700908 fos = userListFile.startWrite();
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700909 final BufferedOutputStream bos = new BufferedOutputStream(fos);
910
911 // XmlSerializer serializer = XmlUtils.serializerInstance();
912 final XmlSerializer serializer = new FastXmlSerializer();
Wojciech Staszkiewicz9e9e2e72015-05-08 14:58:46 +0100913 serializer.setOutput(bos, StandardCharsets.UTF_8.name());
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700914 serializer.startDocument(null, true);
915 serializer.setFeature("http://xmlpull.org/v1/doc/features.html#indent-output", true);
916
917 serializer.startTag(null, TAG_USERS);
Amith Yamasani2a003292012-08-14 18:25:45 -0700918 serializer.attribute(null, ATTR_NEXT_SERIAL_NO, Integer.toString(mNextSerialNumber));
Amith Yamasani6f34b412012-10-22 18:19:27 -0700919 serializer.attribute(null, ATTR_USER_VERSION, Integer.toString(mUserVersion));
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700920
Adam Lesinskieddeb492014-09-08 17:50:03 -0700921 serializer.startTag(null, TAG_GUEST_RESTRICTIONS);
Amith Yamasanie4afaa32014-06-30 14:55:07 +0530922 writeRestrictionsLocked(serializer, mGuestRestrictions);
923 serializer.endTag(null, TAG_GUEST_RESTRICTIONS);
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700924 for (int i = 0; i < mUsers.size(); i++) {
925 UserInfo user = mUsers.valueAt(i);
926 serializer.startTag(null, TAG_USER);
927 serializer.attribute(null, ATTR_ID, Integer.toString(user.id));
928 serializer.endTag(null, TAG_USER);
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700929 }
930
931 serializer.endTag(null, TAG_USERS);
932
933 serializer.endDocument();
Amith Yamasani2a003292012-08-14 18:25:45 -0700934 userListFile.finishWrite(fos);
935 } catch (Exception e) {
936 userListFile.failWrite(fos);
Amith Yamasani0b285492011-04-14 17:35:23 -0700937 Slog.e(LOG_TAG, "Error writing user list");
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700938 }
939 }
940
Amith Yamasanie4afaa32014-06-30 14:55:07 +0530941 private void writeRestrictionsLocked(XmlSerializer serializer, Bundle restrictions)
942 throws IOException {
943 serializer.startTag(null, TAG_RESTRICTIONS);
944 writeBoolean(serializer, restrictions, UserManager.DISALLOW_CONFIG_WIFI);
945 writeBoolean(serializer, restrictions, UserManager.DISALLOW_MODIFY_ACCOUNTS);
946 writeBoolean(serializer, restrictions, UserManager.DISALLOW_INSTALL_APPS);
947 writeBoolean(serializer, restrictions, UserManager.DISALLOW_UNINSTALL_APPS);
948 writeBoolean(serializer, restrictions, UserManager.DISALLOW_SHARE_LOCATION);
949 writeBoolean(serializer, restrictions,
950 UserManager.DISALLOW_INSTALL_UNKNOWN_SOURCES);
951 writeBoolean(serializer, restrictions, UserManager.DISALLOW_CONFIG_BLUETOOTH);
952 writeBoolean(serializer, restrictions, UserManager.DISALLOW_USB_FILE_TRANSFER);
953 writeBoolean(serializer, restrictions, UserManager.DISALLOW_CONFIG_CREDENTIALS);
954 writeBoolean(serializer, restrictions, UserManager.DISALLOW_REMOVE_USER);
955 writeBoolean(serializer, restrictions, UserManager.DISALLOW_DEBUGGING_FEATURES);
956 writeBoolean(serializer, restrictions, UserManager.DISALLOW_CONFIG_VPN);
957 writeBoolean(serializer, restrictions, UserManager.DISALLOW_CONFIG_TETHERING);
Stuart Scott94b038b2015-04-20 14:07:45 -0700958 writeBoolean(serializer, restrictions, UserManager.DISALLOW_NETWORK_RESET);
Amith Yamasanie4afaa32014-06-30 14:55:07 +0530959 writeBoolean(serializer, restrictions, UserManager.DISALLOW_FACTORY_RESET);
960 writeBoolean(serializer, restrictions, UserManager.DISALLOW_ADD_USER);
961 writeBoolean(serializer, restrictions, UserManager.ENSURE_VERIFY_APPS);
962 writeBoolean(serializer, restrictions, UserManager.DISALLOW_CONFIG_CELL_BROADCASTS);
963 writeBoolean(serializer, restrictions, UserManager.DISALLOW_CONFIG_MOBILE_NETWORKS);
964 writeBoolean(serializer, restrictions, UserManager.DISALLOW_APPS_CONTROL);
965 writeBoolean(serializer, restrictions, UserManager.DISALLOW_MOUNT_PHYSICAL_MEDIA);
966 writeBoolean(serializer, restrictions, UserManager.DISALLOW_UNMUTE_MICROPHONE);
967 writeBoolean(serializer, restrictions, UserManager.DISALLOW_ADJUST_VOLUME);
Amith Yamasani390989d2014-07-17 10:52:03 -0700968 writeBoolean(serializer, restrictions, UserManager.DISALLOW_OUTGOING_CALLS);
969 writeBoolean(serializer, restrictions, UserManager.DISALLOW_SMS);
Jeff Sharkey2cc03e52015-03-20 11:24:04 -0700970 writeBoolean(serializer, restrictions, UserManager.DISALLOW_FUN);
Jason Monk1c7c3192014-06-26 12:52:18 -0400971 writeBoolean(serializer, restrictions, UserManager.DISALLOW_CREATE_WINDOWS);
Amith Yamasani26af8292014-09-09 09:57:27 -0700972 writeBoolean(serializer, restrictions, UserManager.DISALLOW_CROSS_PROFILE_COPY_PASTE);
973 writeBoolean(serializer, restrictions, UserManager.DISALLOW_OUTGOING_BEAM);
Benjamin Franzf3ece362015-02-11 10:51:10 +0000974 writeBoolean(serializer, restrictions, UserManager.DISALLOW_WALLPAPER);
Benjamin Franzbff46ba2015-03-05 18:33:51 +0000975 writeBoolean(serializer, restrictions, UserManager.DISALLOW_SAFE_BOOT);
Nicolas Prevot9edbda12015-06-17 11:09:48 -0700976 writeBoolean(serializer, restrictions, UserManager.ALLOW_PARENT_APP_LINKING);
Amith Yamasanie4afaa32014-06-30 14:55:07 +0530977 serializer.endTag(null, TAG_RESTRICTIONS);
978 }
979
Amith Yamasanie4cf7342012-12-17 11:12:09 -0800980 private UserInfo readUserLocked(int id) {
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700981 int flags = 0;
Amith Yamasani2a003292012-08-14 18:25:45 -0700982 int serialNumber = id;
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700983 String name = null;
Amith Yamasanib8151ec2012-04-18 18:02:48 -0700984 String iconPath = null;
Amith Yamasani920ace02012-09-20 22:15:37 -0700985 long creationTime = 0L;
986 long lastLoggedInTime = 0L;
Kenny Guy2a764942014-04-02 13:29:20 +0100987 int profileGroupId = UserInfo.NO_PROFILE_GROUP_ID;
Dianne Hackbornd4ac8d72012-09-27 23:20:10 -0700988 boolean partial = false;
Adam Lesinskieddeb492014-09-08 17:50:03 -0700989 boolean guestToRemove = false;
Amith Yamasanie4cf7342012-12-17 11:12:09 -0800990 Bundle restrictions = new Bundle();
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700991
992 FileInputStream fis = null;
993 try {
Amith Yamasani2a003292012-08-14 18:25:45 -0700994 AtomicFile userFile =
Amith Yamasanifc95e702013-09-26 13:20:17 -0700995 new AtomicFile(new File(mUsersDir, Integer.toString(id) + XML_SUFFIX));
Amith Yamasani2a003292012-08-14 18:25:45 -0700996 fis = userFile.openRead();
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700997 XmlPullParser parser = Xml.newPullParser();
Wojciech Staszkiewicz9e9e2e72015-05-08 14:58:46 +0100998 parser.setInput(fis, StandardCharsets.UTF_8.name());
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700999 int type;
1000 while ((type = parser.next()) != XmlPullParser.START_TAG
1001 && type != XmlPullParser.END_DOCUMENT) {
1002 ;
1003 }
1004
1005 if (type != XmlPullParser.START_TAG) {
Amith Yamasani0b285492011-04-14 17:35:23 -07001006 Slog.e(LOG_TAG, "Unable to read user " + id);
Amith Yamasani4b2e9342011-03-31 12:38:53 -07001007 return null;
1008 }
1009
1010 if (type == XmlPullParser.START_TAG && parser.getName().equals(TAG_USER)) {
Amith Yamasani920ace02012-09-20 22:15:37 -07001011 int storedId = readIntAttribute(parser, ATTR_ID, -1);
1012 if (storedId != id) {
Amith Yamasani0b285492011-04-14 17:35:23 -07001013 Slog.e(LOG_TAG, "User id does not match the file name");
Amith Yamasani4b2e9342011-03-31 12:38:53 -07001014 return null;
1015 }
Amith Yamasani920ace02012-09-20 22:15:37 -07001016 serialNumber = readIntAttribute(parser, ATTR_SERIAL_NO, id);
1017 flags = readIntAttribute(parser, ATTR_FLAGS, 0);
Amith Yamasanib8151ec2012-04-18 18:02:48 -07001018 iconPath = parser.getAttributeValue(null, ATTR_ICON_PATH);
Amith Yamasani920ace02012-09-20 22:15:37 -07001019 creationTime = readLongAttribute(parser, ATTR_CREATION_TIME, 0);
1020 lastLoggedInTime = readLongAttribute(parser, ATTR_LAST_LOGGED_IN_TIME, 0);
Kenny Guy2a764942014-04-02 13:29:20 +01001021 profileGroupId = readIntAttribute(parser, ATTR_PROFILE_GROUP_ID,
1022 UserInfo.NO_PROFILE_GROUP_ID);
Dianne Hackbornd4ac8d72012-09-27 23:20:10 -07001023 String valueString = parser.getAttributeValue(null, ATTR_PARTIAL);
1024 if ("true".equals(valueString)) {
1025 partial = true;
1026 }
Adam Lesinskieddeb492014-09-08 17:50:03 -07001027 valueString = parser.getAttributeValue(null, ATTR_GUEST_TO_REMOVE);
1028 if ("true".equals(valueString)) {
1029 guestToRemove = true;
1030 }
Amith Yamasani4b2e9342011-03-31 12:38:53 -07001031
Amith Yamasanie4cf7342012-12-17 11:12:09 -08001032 int outerDepth = parser.getDepth();
1033 while ((type = parser.next()) != XmlPullParser.END_DOCUMENT
1034 && (type != XmlPullParser.END_TAG || parser.getDepth() > outerDepth)) {
1035 if (type == XmlPullParser.END_TAG || type == XmlPullParser.TEXT) {
1036 continue;
1037 }
1038 String tag = parser.getName();
1039 if (TAG_NAME.equals(tag)) {
1040 type = parser.next();
1041 if (type == XmlPullParser.TEXT) {
1042 name = parser.getText();
1043 }
1044 } else if (TAG_RESTRICTIONS.equals(tag)) {
Amith Yamasanie4afaa32014-06-30 14:55:07 +05301045 readRestrictionsLocked(parser, restrictions);
Amith Yamasani4b2e9342011-03-31 12:38:53 -07001046 }
1047 }
1048 }
Amith Yamasani4b2e9342011-03-31 12:38:53 -07001049
Amith Yamasanib8151ec2012-04-18 18:02:48 -07001050 UserInfo userInfo = new UserInfo(id, name, iconPath, flags);
Amith Yamasani2a003292012-08-14 18:25:45 -07001051 userInfo.serialNumber = serialNumber;
Amith Yamasani920ace02012-09-20 22:15:37 -07001052 userInfo.creationTime = creationTime;
1053 userInfo.lastLoggedInTime = lastLoggedInTime;
Dianne Hackbornd4ac8d72012-09-27 23:20:10 -07001054 userInfo.partial = partial;
Adam Lesinskieddeb492014-09-08 17:50:03 -07001055 userInfo.guestToRemove = guestToRemove;
Kenny Guy2a764942014-04-02 13:29:20 +01001056 userInfo.profileGroupId = profileGroupId;
Amith Yamasanie4cf7342012-12-17 11:12:09 -08001057 mUserRestrictions.append(id, restrictions);
Amith Yamasani4b2e9342011-03-31 12:38:53 -07001058 return userInfo;
1059
1060 } catch (IOException ioe) {
1061 } catch (XmlPullParserException pe) {
Dianne Hackbornbfd89b32011-12-15 18:22:54 -08001062 } finally {
1063 if (fis != null) {
1064 try {
1065 fis.close();
1066 } catch (IOException e) {
1067 }
1068 }
Amith Yamasani4b2e9342011-03-31 12:38:53 -07001069 }
1070 return null;
1071 }
1072
Amith Yamasanie4afaa32014-06-30 14:55:07 +05301073 private void readRestrictionsLocked(XmlPullParser parser, Bundle restrictions)
1074 throws IOException {
1075 readBoolean(parser, restrictions, UserManager.DISALLOW_CONFIG_WIFI);
1076 readBoolean(parser, restrictions, UserManager.DISALLOW_MODIFY_ACCOUNTS);
1077 readBoolean(parser, restrictions, UserManager.DISALLOW_INSTALL_APPS);
1078 readBoolean(parser, restrictions, UserManager.DISALLOW_UNINSTALL_APPS);
1079 readBoolean(parser, restrictions, UserManager.DISALLOW_SHARE_LOCATION);
1080 readBoolean(parser, restrictions,
1081 UserManager.DISALLOW_INSTALL_UNKNOWN_SOURCES);
1082 readBoolean(parser, restrictions, UserManager.DISALLOW_CONFIG_BLUETOOTH);
1083 readBoolean(parser, restrictions, UserManager.DISALLOW_USB_FILE_TRANSFER);
1084 readBoolean(parser, restrictions, UserManager.DISALLOW_CONFIG_CREDENTIALS);
1085 readBoolean(parser, restrictions, UserManager.DISALLOW_REMOVE_USER);
1086 readBoolean(parser, restrictions, UserManager.DISALLOW_DEBUGGING_FEATURES);
1087 readBoolean(parser, restrictions, UserManager.DISALLOW_CONFIG_VPN);
1088 readBoolean(parser, restrictions, UserManager.DISALLOW_CONFIG_TETHERING);
Stuart Scott94b038b2015-04-20 14:07:45 -07001089 readBoolean(parser, restrictions, UserManager.DISALLOW_NETWORK_RESET);
Amith Yamasanie4afaa32014-06-30 14:55:07 +05301090 readBoolean(parser, restrictions, UserManager.DISALLOW_FACTORY_RESET);
1091 readBoolean(parser, restrictions, UserManager.DISALLOW_ADD_USER);
1092 readBoolean(parser, restrictions, UserManager.ENSURE_VERIFY_APPS);
1093 readBoolean(parser, restrictions, UserManager.DISALLOW_CONFIG_CELL_BROADCASTS);
1094 readBoolean(parser, restrictions, UserManager.DISALLOW_CONFIG_MOBILE_NETWORKS);
1095 readBoolean(parser, restrictions, UserManager.DISALLOW_APPS_CONTROL);
1096 readBoolean(parser, restrictions,
1097 UserManager.DISALLOW_MOUNT_PHYSICAL_MEDIA);
1098 readBoolean(parser, restrictions, UserManager.DISALLOW_UNMUTE_MICROPHONE);
1099 readBoolean(parser, restrictions, UserManager.DISALLOW_ADJUST_VOLUME);
Amith Yamasani390989d2014-07-17 10:52:03 -07001100 readBoolean(parser, restrictions, UserManager.DISALLOW_OUTGOING_CALLS);
1101 readBoolean(parser, restrictions, UserManager.DISALLOW_SMS);
Jeff Sharkey2cc03e52015-03-20 11:24:04 -07001102 readBoolean(parser, restrictions, UserManager.DISALLOW_FUN);
Jason Monk1c7c3192014-06-26 12:52:18 -04001103 readBoolean(parser, restrictions, UserManager.DISALLOW_CREATE_WINDOWS);
Amith Yamasani26af8292014-09-09 09:57:27 -07001104 readBoolean(parser, restrictions, UserManager.DISALLOW_CROSS_PROFILE_COPY_PASTE);
1105 readBoolean(parser, restrictions, UserManager.DISALLOW_OUTGOING_BEAM);
Benjamin Franzf3ece362015-02-11 10:51:10 +00001106 readBoolean(parser, restrictions, UserManager.DISALLOW_WALLPAPER);
Benjamin Franzbff46ba2015-03-05 18:33:51 +00001107 readBoolean(parser, restrictions, UserManager.DISALLOW_SAFE_BOOT);
Nicolas Prevot9edbda12015-06-17 11:09:48 -07001108 readBoolean(parser, restrictions, UserManager.ALLOW_PARENT_APP_LINKING);
Amith Yamasanie4afaa32014-06-30 14:55:07 +05301109 }
1110
Amith Yamasanie4cf7342012-12-17 11:12:09 -08001111 private void readBoolean(XmlPullParser parser, Bundle restrictions,
1112 String restrictionKey) {
1113 String value = parser.getAttributeValue(null, restrictionKey);
Amith Yamasani71e6c692013-03-24 17:39:28 -07001114 if (value != null) {
1115 restrictions.putBoolean(restrictionKey, Boolean.parseBoolean(value));
1116 }
Amith Yamasanie4cf7342012-12-17 11:12:09 -08001117 }
1118
1119 private void writeBoolean(XmlSerializer xml, Bundle restrictions, String restrictionKey)
1120 throws IOException {
1121 if (restrictions.containsKey(restrictionKey)) {
1122 xml.attribute(null, restrictionKey,
1123 Boolean.toString(restrictions.getBoolean(restrictionKey)));
1124 }
1125 }
1126
Amith Yamasani920ace02012-09-20 22:15:37 -07001127 private int readIntAttribute(XmlPullParser parser, String attr, int defaultValue) {
1128 String valueString = parser.getAttributeValue(null, attr);
1129 if (valueString == null) return defaultValue;
1130 try {
1131 return Integer.parseInt(valueString);
1132 } catch (NumberFormatException nfe) {
1133 return defaultValue;
1134 }
1135 }
1136
1137 private long readLongAttribute(XmlPullParser parser, String attr, long defaultValue) {
1138 String valueString = parser.getAttributeValue(null, attr);
1139 if (valueString == null) return defaultValue;
1140 try {
1141 return Long.parseLong(valueString);
1142 } catch (NumberFormatException nfe) {
1143 return defaultValue;
1144 }
1145 }
1146
Amith Yamasani1a7472e2013-07-02 11:17:30 -07001147 private boolean isPackageInstalled(String pkg, int userId) {
1148 final ApplicationInfo info = mPm.getApplicationInfo(pkg,
1149 PackageManager.GET_UNINSTALLED_PACKAGES,
1150 userId);
1151 if (info == null || (info.flags&ApplicationInfo.FLAG_INSTALLED) == 0) {
1152 return false;
1153 }
1154 return true;
1155 }
1156
Amith Yamasanib82add22013-07-09 11:24:44 -07001157 /**
Kenny Guyd21b2182014-07-17 16:38:55 +01001158 * Removes all the restrictions files (res_<packagename>) for a given user.
Amith Yamasanib82add22013-07-09 11:24:44 -07001159 * Does not do any permissions checking.
1160 */
Kenny Guyd21b2182014-07-17 16:38:55 +01001161 private void cleanAppRestrictions(int userId) {
Amith Yamasanib82add22013-07-09 11:24:44 -07001162 synchronized (mPackagesLock) {
1163 File dir = Environment.getUserSystemDirectory(userId);
1164 String[] files = dir.list();
1165 if (files == null) return;
1166 for (String fileName : files) {
1167 if (fileName.startsWith(RESTRICTIONS_FILE_PREFIX)) {
1168 File resFile = new File(dir, fileName);
1169 if (resFile.exists()) {
Kenny Guyd21b2182014-07-17 16:38:55 +01001170 resFile.delete();
Amith Yamasanib82add22013-07-09 11:24:44 -07001171 }
1172 }
1173 }
1174 }
1175 }
1176
Amith Yamasani1a7472e2013-07-02 11:17:30 -07001177 /**
1178 * Removes the app restrictions file for a specific package and user id, if it exists.
1179 */
1180 private void cleanAppRestrictionsForPackage(String pkg, int userId) {
1181 synchronized (mPackagesLock) {
1182 File dir = Environment.getUserSystemDirectory(userId);
Amith Yamasanifc95e702013-09-26 13:20:17 -07001183 File resFile = new File(dir, packageToRestrictionsFileName(pkg));
Amith Yamasani1a7472e2013-07-02 11:17:30 -07001184 if (resFile.exists()) {
1185 resFile.delete();
1186 }
1187 }
1188 }
1189
Kenny Guya52dc3e2014-02-11 15:33:14 +00001190 @Override
Kenny Guy2a764942014-04-02 13:29:20 +01001191 public UserInfo createProfileForUser(String name, int flags, int userId) {
Kenny Guya52dc3e2014-02-11 15:33:14 +00001192 checkManageUsersPermission("Only the system can create users");
Kenny Guy2a764942014-04-02 13:29:20 +01001193 if (userId != UserHandle.USER_OWNER) {
1194 Slog.w(LOG_TAG, "Only user owner can have profiles");
Kenny Guya52dc3e2014-02-11 15:33:14 +00001195 return null;
1196 }
Kenny Guy2a764942014-04-02 13:29:20 +01001197 return createUserInternal(name, flags, userId);
Kenny Guya52dc3e2014-02-11 15:33:14 +00001198 }
1199
Amith Yamasani258848d2012-08-10 17:06:33 -07001200 @Override
Amith Yamasani13593602012-03-22 16:16:17 -07001201 public UserInfo createUser(String name, int flags) {
Amith Yamasani2a003292012-08-14 18:25:45 -07001202 checkManageUsersPermission("Only the system can create users");
Nicolas Prevotc6d033e2014-02-27 13:11:09 +00001203 return createUserInternal(name, flags, UserHandle.USER_NULL);
Kenny Guya52dc3e2014-02-11 15:33:14 +00001204 }
Amith Yamasanifaea76f2012-09-11 10:59:48 -07001205
Jessica Hummelbe81c802014-04-22 15:49:22 +01001206 private UserInfo createUserInternal(String name, int flags, int parentId) {
Julia Reynolds75175022014-06-26 16:35:00 -04001207 if (getUserRestrictions(UserHandle.getCallingUserId()).getBoolean(
1208 UserManager.DISALLOW_ADD_USER, false)) {
1209 Log.w(LOG_TAG, "Cannot add user. DISALLOW_ADD_USER is enabled.");
1210 return null;
1211 }
Nicolas Prevot12678a92015-05-13 12:15:03 -07001212 if (ActivityManager.isLowRamDeviceStatic()) {
1213 return null;
1214 }
Amith Yamasani95ab7842014-08-11 17:09:26 -07001215 final boolean isGuest = (flags & UserInfo.FLAG_GUEST) != 0;
Nicolas Prevot12678a92015-05-13 12:15:03 -07001216 final boolean isManagedProfile = (flags & UserInfo.FLAG_MANAGED_PROFILE) != 0;
Dianne Hackborn5dc5a002012-09-15 19:33:48 -07001217 final long ident = Binder.clearCallingIdentity();
Nicolas Prevotc6d033e2014-02-27 13:11:09 +00001218 UserInfo userInfo = null;
Dianne Hackborn5dc5a002012-09-15 19:33:48 -07001219 try {
1220 synchronized (mInstallLock) {
1221 synchronized (mPackagesLock) {
Jessica Hummelbe81c802014-04-22 15:49:22 +01001222 UserInfo parent = null;
1223 if (parentId != UserHandle.USER_NULL) {
1224 parent = getUserInfoLocked(parentId);
1225 if (parent == null) return null;
Nicolas Prevotc6d033e2014-02-27 13:11:09 +00001226 }
Nicolas Prevot12678a92015-05-13 12:15:03 -07001227 if (isManagedProfile && !canAddMoreManagedProfiles()) {
1228 return null;
1229 }
1230 if (!isGuest && !isManagedProfile && isUserLimitReachedLocked()) {
1231 // If we're not adding a guest user or a managed profile and the limit has
1232 // been reached, cannot add a user.
Amith Yamasani95ab7842014-08-11 17:09:26 -07001233 return null;
1234 }
1235 // If we're adding a guest and there already exists one, bail.
Adam Lesinskieddeb492014-09-08 17:50:03 -07001236 if (isGuest && findCurrentGuestUserLocked() != null) {
Amith Yamasani95ab7842014-08-11 17:09:26 -07001237 return null;
1238 }
Dianne Hackborn5dc5a002012-09-15 19:33:48 -07001239 int userId = getNextAvailableIdLocked();
1240 userInfo = new UserInfo(userId, name, null, flags);
1241 File userPath = new File(mBaseUserPath, Integer.toString(userId));
1242 userInfo.serialNumber = mNextSerialNumber++;
Amith Yamasani920ace02012-09-20 22:15:37 -07001243 long now = System.currentTimeMillis();
1244 userInfo.creationTime = (now > EPOCH_PLUS_30_YEARS) ? now : 0;
Dianne Hackbornd4ac8d72012-09-27 23:20:10 -07001245 userInfo.partial = true;
Amith Yamasani16389312012-10-17 21:20:14 -07001246 Environment.getUserSystemDirectory(userInfo.id).mkdirs();
Dianne Hackborn5dc5a002012-09-15 19:33:48 -07001247 mUsers.put(userId, userInfo);
1248 writeUserListLocked();
Jessica Hummelbe81c802014-04-22 15:49:22 +01001249 if (parent != null) {
1250 if (parent.profileGroupId == UserInfo.NO_PROFILE_GROUP_ID) {
1251 parent.profileGroupId = parent.id;
Fyodor Kupolov75d0ea82014-12-15 16:21:07 -08001252 scheduleWriteUserLocked(parent);
Kenny Guya52dc3e2014-02-11 15:33:14 +00001253 }
Jessica Hummelbe81c802014-04-22 15:49:22 +01001254 userInfo.profileGroupId = parent.profileGroupId;
Kenny Guya52dc3e2014-02-11 15:33:14 +00001255 }
Dianne Hackborn5dc5a002012-09-15 19:33:48 -07001256 mPm.createNewUserLILPw(userId, userPath);
Dianne Hackbornd4ac8d72012-09-27 23:20:10 -07001257 userInfo.partial = false;
Fyodor Kupolov75d0ea82014-12-15 16:21:07 -08001258 scheduleWriteUserLocked(userInfo);
Dianne Hackbornd4ac8d72012-09-27 23:20:10 -07001259 updateUserIdsLocked();
Geoffrey Borggaarde45e45e32013-01-24 10:03:20 -05001260 Bundle restrictions = new Bundle();
Geoffrey Borggaarde45e45e32013-01-24 10:03:20 -05001261 mUserRestrictions.append(userId, restrictions);
Svet Ganov78027f32015-03-25 09:10:09 -07001262 mPm.newUserCreatedLILPw(userId);
Dianne Hackborn5dc5a002012-09-15 19:33:48 -07001263 }
Dianne Hackborn4428e172012-08-24 17:43:05 -07001264 }
Dianne Hackborn5dc5a002012-09-15 19:33:48 -07001265 if (userInfo != null) {
1266 Intent addedIntent = new Intent(Intent.ACTION_USER_ADDED);
1267 addedIntent.putExtra(Intent.EXTRA_USER_HANDLE, userInfo.id);
1268 mContext.sendBroadcastAsUser(addedIntent, UserHandle.ALL,
1269 android.Manifest.permission.MANAGE_USERS);
1270 }
1271 } finally {
1272 Binder.restoreCallingIdentity(ident);
Amith Yamasani258848d2012-08-10 17:06:33 -07001273 }
Amith Yamasani4b2e9342011-03-31 12:38:53 -07001274 return userInfo;
1275 }
1276
Amith Yamasani95ab7842014-08-11 17:09:26 -07001277 private int numberOfUsersOfTypeLocked(int flags, boolean excludeDying) {
1278 int count = 0;
1279 for (int i = mUsers.size() - 1; i >= 0; i--) {
1280 UserInfo user = mUsers.valueAt(i);
1281 if (!excludeDying || !mRemovingUserIds.get(user.id)) {
1282 if ((user.flags & flags) != 0) {
1283 count++;
1284 }
1285 }
1286 }
1287 return count;
1288 }
1289
Amith Yamasani0b285492011-04-14 17:35:23 -07001290 /**
Adam Lesinskieddeb492014-09-08 17:50:03 -07001291 * Find the current guest user. If the Guest user is partial,
1292 * then do not include it in the results as it is about to die.
1293 * This is different than {@link #numberOfUsersOfTypeLocked(int, boolean)} due to
1294 * the special handling of Guests being removed.
1295 */
1296 private UserInfo findCurrentGuestUserLocked() {
1297 final int size = mUsers.size();
1298 for (int i = 0; i < size; i++) {
1299 final UserInfo user = mUsers.valueAt(i);
1300 if (user.isGuest() && !user.guestToRemove && !mRemovingUserIds.get(user.id)) {
1301 return user;
1302 }
1303 }
1304 return null;
1305 }
1306
1307 /**
Amith Yamasani1df14732014-08-29 21:37:27 -07001308 * Mark this guest user for deletion to allow us to create another guest
1309 * and switch to that user before actually removing this guest.
1310 * @param userHandle the userid of the current guest
1311 * @return whether the user could be marked for deletion
1312 */
1313 public boolean markGuestForDeletion(int userHandle) {
1314 checkManageUsersPermission("Only the system can remove users");
1315 if (getUserRestrictions(UserHandle.getCallingUserId()).getBoolean(
1316 UserManager.DISALLOW_REMOVE_USER, false)) {
1317 Log.w(LOG_TAG, "Cannot remove user. DISALLOW_REMOVE_USER is enabled.");
1318 return false;
1319 }
1320
1321 long ident = Binder.clearCallingIdentity();
1322 try {
1323 final UserInfo user;
1324 synchronized (mPackagesLock) {
1325 user = mUsers.get(userHandle);
1326 if (userHandle == 0 || user == null || mRemovingUserIds.get(userHandle)) {
1327 return false;
1328 }
1329 if (!user.isGuest()) {
1330 return false;
1331 }
Adam Lesinskieddeb492014-09-08 17:50:03 -07001332 // We set this to a guest user that is to be removed. This is a temporary state
1333 // where we are allowed to add new Guest users, even if this one is still not
1334 // removed. This user will still show up in getUserInfo() calls.
1335 // If we don't get around to removing this Guest user, it will be purged on next
1336 // startup.
1337 user.guestToRemove = true;
Amith Yamasani1df14732014-08-29 21:37:27 -07001338 // Mark it as disabled, so that it isn't returned any more when
1339 // profiles are queried.
1340 user.flags |= UserInfo.FLAG_DISABLED;
Amith Yamasani1df14732014-08-29 21:37:27 -07001341 writeUserLocked(user);
1342 }
1343 } finally {
1344 Binder.restoreCallingIdentity(ident);
1345 }
1346 return true;
1347 }
1348
1349 /**
Amith Yamasani0b285492011-04-14 17:35:23 -07001350 * Removes a user and all data directories created for that user. This method should be called
1351 * after the user's processes have been terminated.
Dianne Hackborn10ad9822014-03-17 11:28:36 -07001352 * @param userHandle the user's id
Amith Yamasani0b285492011-04-14 17:35:23 -07001353 */
Amith Yamasani258848d2012-08-10 17:06:33 -07001354 public boolean removeUser(int userHandle) {
Amith Yamasani2a003292012-08-14 18:25:45 -07001355 checkManageUsersPermission("Only the system can remove users");
Julia Reynolds4ac5f852014-06-23 17:38:51 -04001356 if (getUserRestrictions(UserHandle.getCallingUserId()).getBoolean(
1357 UserManager.DISALLOW_REMOVE_USER, false)) {
1358 Log.w(LOG_TAG, "Cannot remove user. DISALLOW_REMOVE_USER is enabled.");
1359 return false;
1360 }
1361
Kenny Guyee58b4f2014-05-23 15:19:53 +01001362 long ident = Binder.clearCallingIdentity();
1363 try {
1364 final UserInfo user;
1365 synchronized (mPackagesLock) {
1366 user = mUsers.get(userHandle);
Kenny Guy17c9d692014-06-13 13:51:42 +01001367 if (userHandle == 0 || user == null || mRemovingUserIds.get(userHandle)) {
Kenny Guyee58b4f2014-05-23 15:19:53 +01001368 return false;
1369 }
Jeff Sharkey6eb09392014-11-14 15:57:59 -08001370
1371 // We remember deleted user IDs to prevent them from being
1372 // reused during the current boot; they can still be reused
1373 // after a reboot.
Kenny Guyee58b4f2014-05-23 15:19:53 +01001374 mRemovingUserIds.put(userHandle, true);
Jeff Sharkey6eb09392014-11-14 15:57:59 -08001375
Kenny Guyee58b4f2014-05-23 15:19:53 +01001376 try {
1377 mAppOpsService.removeUser(userHandle);
1378 } catch (RemoteException e) {
1379 Log.w(LOG_TAG, "Unable to notify AppOpsService of removing user", e);
1380 }
1381 // Set this to a partially created user, so that the user will be purged
1382 // on next startup, in case the runtime stops now before stopping and
1383 // removing the user completely.
1384 user.partial = true;
1385 // Mark it as disabled, so that it isn't returned any more when
1386 // profiles are queried.
1387 user.flags |= UserInfo.FLAG_DISABLED;
1388 writeUserLocked(user);
1389 }
1390
1391 if (user.profileGroupId != UserInfo.NO_PROFILE_GROUP_ID
1392 && user.isManagedProfile()) {
1393 // Send broadcast to notify system that the user removed was a
1394 // managed user.
1395 sendProfileRemovedBroadcast(user.profileGroupId, user.id);
1396 }
1397
1398 if (DBG) Slog.i(LOG_TAG, "Stopping user " + userHandle);
1399 int res;
1400 try {
1401 res = ActivityManagerNative.getDefault().stopUser(userHandle,
1402 new IStopUserCallback.Stub() {
1403 @Override
1404 public void userStopped(int userId) {
1405 finishRemoveUser(userId);
1406 }
1407 @Override
1408 public void userStopAborted(int userId) {
1409 }
1410 });
1411 } catch (RemoteException e) {
Dianne Hackborn80a4af22012-08-27 19:18:31 -07001412 return false;
1413 }
Kenny Guyee58b4f2014-05-23 15:19:53 +01001414 return res == ActivityManager.USER_OP_SUCCESS;
1415 } finally {
1416 Binder.restoreCallingIdentity(ident);
Dianne Hackborn80a4af22012-08-27 19:18:31 -07001417 }
Dianne Hackborn80a4af22012-08-27 19:18:31 -07001418 }
1419
Amith Yamasanidb6a14c2012-10-17 21:16:52 -07001420 void finishRemoveUser(final int userHandle) {
Amith Yamasani16389312012-10-17 21:20:14 -07001421 if (DBG) Slog.i(LOG_TAG, "finishRemoveUser " + userHandle);
Amith Yamasanidb6a14c2012-10-17 21:16:52 -07001422 // Let other services shutdown any activity and clean up their state before completely
1423 // wiping the user's system directory and removing from the user list
Dianne Hackborn5dc5a002012-09-15 19:33:48 -07001424 long ident = Binder.clearCallingIdentity();
1425 try {
1426 Intent addedIntent = new Intent(Intent.ACTION_USER_REMOVED);
1427 addedIntent.putExtra(Intent.EXTRA_USER_HANDLE, userHandle);
Amith Yamasanidb6a14c2012-10-17 21:16:52 -07001428 mContext.sendOrderedBroadcastAsUser(addedIntent, UserHandle.ALL,
1429 android.Manifest.permission.MANAGE_USERS,
1430
1431 new BroadcastReceiver() {
1432 @Override
1433 public void onReceive(Context context, Intent intent) {
1434 if (DBG) {
1435 Slog.i(LOG_TAG,
1436 "USER_REMOVED broadcast sent, cleaning up user data "
1437 + userHandle);
1438 }
1439 new Thread() {
1440 public void run() {
1441 synchronized (mInstallLock) {
1442 synchronized (mPackagesLock) {
1443 removeUserStateLocked(userHandle);
1444 }
1445 }
1446 }
1447 }.start();
1448 }
1449 },
1450
1451 null, Activity.RESULT_OK, null, null);
Dianne Hackborn5dc5a002012-09-15 19:33:48 -07001452 } finally {
1453 Binder.restoreCallingIdentity(ident);
1454 }
Amith Yamasani2a003292012-08-14 18:25:45 -07001455 }
1456
Jeff Sharkeyffe0cb42012-11-05 17:24:43 -08001457 private void removeUserStateLocked(final int userHandle) {
Paul Crowley85e4e812015-05-19 12:42:00 +01001458 mContext.getSystemService(StorageManager.class)
1459 .deleteUserKey(userHandle);
Dianne Hackbornd4ac8d72012-09-27 23:20:10 -07001460 // Cleanup package manager settings
Amith Yamasanidda003f2014-08-28 18:06:51 -07001461 mPm.cleanUpUserLILPw(this, userHandle);
Dianne Hackbornd4ac8d72012-09-27 23:20:10 -07001462
1463 // Remove this user from the list
1464 mUsers.remove(userHandle);
Dianne Hackbornd4ac8d72012-09-27 23:20:10 -07001465 // Remove user file
Amith Yamasanifc95e702013-09-26 13:20:17 -07001466 AtomicFile userFile = new AtomicFile(new File(mUsersDir, userHandle + XML_SUFFIX));
Dianne Hackbornd4ac8d72012-09-27 23:20:10 -07001467 userFile.delete();
1468 // Update the user list
1469 writeUserListLocked();
1470 updateUserIdsLocked();
1471 removeDirectoryRecursive(Environment.getUserSystemDirectory(userHandle));
1472 }
1473
Amith Yamasani61f57372012-08-31 12:12:28 -07001474 private void removeDirectoryRecursive(File parent) {
1475 if (parent.isDirectory()) {
1476 String[] files = parent.list();
1477 for (String filename : files) {
1478 File child = new File(parent, filename);
1479 removeDirectoryRecursive(child);
1480 }
1481 }
1482 parent.delete();
1483 }
1484
Kenny Guyf8d3a232014-05-15 16:09:52 +01001485 private void sendProfileRemovedBroadcast(int parentUserId, int removedUserId) {
Adam Connors7b66ed52014-04-14 11:58:10 +01001486 Intent managedProfileIntent = new Intent(Intent.ACTION_MANAGED_PROFILE_REMOVED);
Adam Connorsd4b584e2014-06-09 13:55:47 +01001487 managedProfileIntent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY |
1488 Intent.FLAG_RECEIVER_FOREGROUND);
Kenny Guyf8d3a232014-05-15 16:09:52 +01001489 managedProfileIntent.putExtra(Intent.EXTRA_USER, new UserHandle(removedUserId));
1490 mContext.sendBroadcastAsUser(managedProfileIntent, new UserHandle(parentUserId), null);
Adam Connors7b66ed52014-04-14 11:58:10 +01001491 }
1492
Amith Yamasani2a003292012-08-14 18:25:45 -07001493 @Override
Amith Yamasani7e99bc02013-04-16 18:24:51 -07001494 public Bundle getApplicationRestrictions(String packageName) {
1495 return getApplicationRestrictionsForUser(packageName, UserHandle.getCallingUserId());
1496 }
1497
1498 @Override
1499 public Bundle getApplicationRestrictionsForUser(String packageName, int userId) {
Amith Yamasanidf2e92a2013-03-01 17:04:38 -08001500 if (UserHandle.getCallingUserId() != userId
Amith Yamasani9429afb2013-04-10 18:40:51 -07001501 || !UserHandle.isSameApp(Binder.getCallingUid(), getUidForPackage(packageName))) {
Amith Yamasanibe465322014-04-24 13:45:17 -07001502 checkManageUsersPermission("Only system can get restrictions for other users/apps");
Amith Yamasanidf2e92a2013-03-01 17:04:38 -08001503 }
1504 synchronized (mPackagesLock) {
1505 // Read the restrictions from XML
1506 return readApplicationRestrictionsLocked(packageName, userId);
1507 }
1508 }
1509
1510 @Override
Amith Yamasani7e99bc02013-04-16 18:24:51 -07001511 public void setApplicationRestrictions(String packageName, Bundle restrictions,
Amith Yamasanidf2e92a2013-03-01 17:04:38 -08001512 int userId) {
1513 if (UserHandle.getCallingUserId() != userId
Amith Yamasani9429afb2013-04-10 18:40:51 -07001514 || !UserHandle.isSameApp(Binder.getCallingUid(), getUidForPackage(packageName))) {
Amith Yamasanibe465322014-04-24 13:45:17 -07001515 checkManageUsersPermission("Only system can set restrictions for other users/apps");
Amith Yamasanidf2e92a2013-03-01 17:04:38 -08001516 }
1517 synchronized (mPackagesLock) {
Kenny Guyd21b2182014-07-17 16:38:55 +01001518 if (restrictions == null || restrictions.isEmpty()) {
1519 cleanAppRestrictionsForPackage(packageName, userId);
1520 } else {
1521 // Write the restrictions to XML
1522 writeApplicationRestrictionsLocked(packageName, restrictions, userId);
1523 }
Amith Yamasanidf2e92a2013-03-01 17:04:38 -08001524 }
Robin Lee66e5d962014-04-09 16:44:21 +01001525
Kenny Guyd21b2182014-07-17 16:38:55 +01001526 if (isPackageInstalled(packageName, userId)) {
1527 // Notify package of changes via an intent - only sent to explicitly registered receivers.
1528 Intent changeIntent = new Intent(Intent.ACTION_APPLICATION_RESTRICTIONS_CHANGED);
1529 changeIntent.setPackage(packageName);
1530 changeIntent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY);
1531 mContext.sendBroadcastAsUser(changeIntent, new UserHandle(userId));
1532 }
Amith Yamasanidf2e92a2013-03-01 17:04:38 -08001533 }
1534
Amith Yamasani655d0e22013-06-12 14:19:10 -07001535 @Override
Amith Yamasani1a7472e2013-07-02 11:17:30 -07001536 public void removeRestrictions() {
Amith Yamasanibe465322014-04-24 13:45:17 -07001537 checkManageUsersPermission("Only system can remove restrictions");
Amith Yamasani1a7472e2013-07-02 11:17:30 -07001538 final int userHandle = UserHandle.getCallingUserId();
Amith Yamasani5e486f52013-08-07 11:06:44 -07001539 removeRestrictionsForUser(userHandle, true);
Amith Yamasani350962c2013-08-06 11:18:53 -07001540 }
1541
Amith Yamasanie5bcff62014-07-19 15:44:09 -07001542 private void removeRestrictionsForUser(final int userHandle, boolean unhideApps) {
Amith Yamasani1a7472e2013-07-02 11:17:30 -07001543 synchronized (mPackagesLock) {
1544 // Remove all user restrictions
1545 setUserRestrictions(new Bundle(), userHandle);
Amith Yamasani1a7472e2013-07-02 11:17:30 -07001546 // Remove any app restrictions
Kenny Guyd21b2182014-07-17 16:38:55 +01001547 cleanAppRestrictions(userHandle);
Amith Yamasani1a7472e2013-07-02 11:17:30 -07001548 }
Amith Yamasanie5bcff62014-07-19 15:44:09 -07001549 if (unhideApps) {
1550 unhideAllInstalledAppsForUser(userHandle);
Amith Yamasani5e486f52013-08-07 11:06:44 -07001551 }
1552 }
1553
Amith Yamasanie5bcff62014-07-19 15:44:09 -07001554 private void unhideAllInstalledAppsForUser(final int userHandle) {
Amith Yamasani1a7472e2013-07-02 11:17:30 -07001555 mHandler.post(new Runnable() {
1556 @Override
1557 public void run() {
1558 List<ApplicationInfo> apps =
1559 mPm.getInstalledApplications(PackageManager.GET_UNINSTALLED_PACKAGES,
1560 userHandle).getList();
1561 final long ident = Binder.clearCallingIdentity();
1562 try {
1563 for (ApplicationInfo appInfo : apps) {
1564 if ((appInfo.flags & ApplicationInfo.FLAG_INSTALLED) != 0
Alex Klyubinb9f8a522015-02-03 11:12:59 -08001565 && (appInfo.privateFlags & ApplicationInfo.PRIVATE_FLAG_HIDDEN)
1566 != 0) {
Amith Yamasanie5bcff62014-07-19 15:44:09 -07001567 mPm.setApplicationHiddenSettingAsUser(appInfo.packageName, false,
Amith Yamasani1a7472e2013-07-02 11:17:30 -07001568 userHandle);
1569 }
1570 }
1571 } finally {
1572 Binder.restoreCallingIdentity(ident);
1573 }
1574 }
1575 });
1576 }
Amith Yamasanidf2e92a2013-03-01 17:04:38 -08001577 private int getUidForPackage(String packageName) {
Amith Yamasani9429afb2013-04-10 18:40:51 -07001578 long ident = Binder.clearCallingIdentity();
Amith Yamasanidf2e92a2013-03-01 17:04:38 -08001579 try {
1580 return mContext.getPackageManager().getApplicationInfo(packageName,
1581 PackageManager.GET_UNINSTALLED_PACKAGES).uid;
1582 } catch (NameNotFoundException nnfe) {
1583 return -1;
Amith Yamasani9429afb2013-04-10 18:40:51 -07001584 } finally {
1585 Binder.restoreCallingIdentity(ident);
Amith Yamasanidf2e92a2013-03-01 17:04:38 -08001586 }
1587 }
1588
Amith Yamasani7e99bc02013-04-16 18:24:51 -07001589 private Bundle readApplicationRestrictionsLocked(String packageName,
Amith Yamasanidf2e92a2013-03-01 17:04:38 -08001590 int userId) {
Fyodor Kupolov262f9952015-03-23 18:55:11 -07001591 AtomicFile restrictionsFile =
1592 new AtomicFile(new File(Environment.getUserSystemDirectory(userId),
1593 packageToRestrictionsFileName(packageName)));
1594 return readApplicationRestrictionsLocked(restrictionsFile);
1595 }
1596
1597 @VisibleForTesting
1598 static Bundle readApplicationRestrictionsLocked(AtomicFile restrictionsFile) {
Amith Yamasani7e99bc02013-04-16 18:24:51 -07001599 final Bundle restrictions = new Bundle();
Fyodor Kupolov262f9952015-03-23 18:55:11 -07001600 final ArrayList<String> values = new ArrayList<>();
Fyodor Kupolov6f34d362015-04-02 12:42:13 -07001601 if (!restrictionsFile.getBaseFile().exists()) {
Fyodor Kupolovf6ee2242015-04-06 10:15:07 -07001602 return restrictions;
Fyodor Kupolov6f34d362015-04-02 12:42:13 -07001603 }
Amith Yamasanidf2e92a2013-03-01 17:04:38 -08001604
1605 FileInputStream fis = null;
1606 try {
Amith Yamasanidf2e92a2013-03-01 17:04:38 -08001607 fis = restrictionsFile.openRead();
1608 XmlPullParser parser = Xml.newPullParser();
Wojciech Staszkiewicz9e9e2e72015-05-08 14:58:46 +01001609 parser.setInput(fis, StandardCharsets.UTF_8.name());
Fyodor Kupolov262f9952015-03-23 18:55:11 -07001610 XmlUtils.nextElement(parser);
1611 if (parser.getEventType() != XmlPullParser.START_TAG) {
Amith Yamasanidf2e92a2013-03-01 17:04:38 -08001612 Slog.e(LOG_TAG, "Unable to read restrictions file "
1613 + restrictionsFile.getBaseFile());
Amith Yamasani7e99bc02013-04-16 18:24:51 -07001614 return restrictions;
Amith Yamasanidf2e92a2013-03-01 17:04:38 -08001615 }
Fyodor Kupolov262f9952015-03-23 18:55:11 -07001616 while (parser.next() != XmlPullParser.END_DOCUMENT) {
1617 readEntry(restrictions, values, parser);
Amith Yamasanidf2e92a2013-03-01 17:04:38 -08001618 }
Fyodor Kupolov262f9952015-03-23 18:55:11 -07001619 } catch (IOException|XmlPullParserException e) {
1620 Log.w(LOG_TAG, "Error parsing " + restrictionsFile.getBaseFile(), e);
Amith Yamasanidf2e92a2013-03-01 17:04:38 -08001621 } finally {
Fyodor Kupolov262f9952015-03-23 18:55:11 -07001622 IoUtils.closeQuietly(fis);
Amith Yamasanidf2e92a2013-03-01 17:04:38 -08001623 }
Amith Yamasani7e99bc02013-04-16 18:24:51 -07001624 return restrictions;
Amith Yamasanidf2e92a2013-03-01 17:04:38 -08001625 }
1626
Fyodor Kupolov262f9952015-03-23 18:55:11 -07001627 private static void readEntry(Bundle restrictions, ArrayList<String> values,
1628 XmlPullParser parser) throws XmlPullParserException, IOException {
1629 int type = parser.getEventType();
1630 if (type == XmlPullParser.START_TAG && parser.getName().equals(TAG_ENTRY)) {
1631 String key = parser.getAttributeValue(null, ATTR_KEY);
1632 String valType = parser.getAttributeValue(null, ATTR_VALUE_TYPE);
1633 String multiple = parser.getAttributeValue(null, ATTR_MULTIPLE);
1634 if (multiple != null) {
1635 values.clear();
1636 int count = Integer.parseInt(multiple);
1637 while (count > 0 && (type = parser.next()) != XmlPullParser.END_DOCUMENT) {
1638 if (type == XmlPullParser.START_TAG
1639 && parser.getName().equals(TAG_VALUE)) {
1640 values.add(parser.nextText().trim());
1641 count--;
1642 }
1643 }
1644 String [] valueStrings = new String[values.size()];
1645 values.toArray(valueStrings);
1646 restrictions.putStringArray(key, valueStrings);
1647 } else if (ATTR_TYPE_BUNDLE.equals(valType)) {
1648 restrictions.putBundle(key, readBundleEntry(parser, values));
1649 } else if (ATTR_TYPE_BUNDLE_ARRAY.equals(valType)) {
1650 final int outerDepth = parser.getDepth();
1651 ArrayList<Bundle> bundleList = new ArrayList<>();
1652 while (XmlUtils.nextElementWithin(parser, outerDepth)) {
1653 Bundle childBundle = readBundleEntry(parser, values);
1654 bundleList.add(childBundle);
1655 }
1656 restrictions.putParcelableArray(key,
1657 bundleList.toArray(new Bundle[bundleList.size()]));
1658 } else {
1659 String value = parser.nextText().trim();
1660 if (ATTR_TYPE_BOOLEAN.equals(valType)) {
1661 restrictions.putBoolean(key, Boolean.parseBoolean(value));
1662 } else if (ATTR_TYPE_INTEGER.equals(valType)) {
1663 restrictions.putInt(key, Integer.parseInt(value));
1664 } else {
1665 restrictions.putString(key, value);
1666 }
1667 }
1668 }
1669 }
1670
1671 private static Bundle readBundleEntry(XmlPullParser parser, ArrayList<String> values)
1672 throws IOException, XmlPullParserException {
1673 Bundle childBundle = new Bundle();
1674 final int outerDepth = parser.getDepth();
1675 while (XmlUtils.nextElementWithin(parser, outerDepth)) {
1676 readEntry(childBundle, values, parser);
1677 }
1678 return childBundle;
1679 }
1680
Amith Yamasanidf2e92a2013-03-01 17:04:38 -08001681 private void writeApplicationRestrictionsLocked(String packageName,
Amith Yamasani7e99bc02013-04-16 18:24:51 -07001682 Bundle restrictions, int userId) {
Amith Yamasanidf2e92a2013-03-01 17:04:38 -08001683 AtomicFile restrictionsFile = new AtomicFile(
1684 new File(Environment.getUserSystemDirectory(userId),
Amith Yamasanifc95e702013-09-26 13:20:17 -07001685 packageToRestrictionsFileName(packageName)));
Fyodor Kupolov262f9952015-03-23 18:55:11 -07001686 writeApplicationRestrictionsLocked(restrictions, restrictionsFile);
1687 }
1688
1689 @VisibleForTesting
1690 static void writeApplicationRestrictionsLocked(Bundle restrictions,
1691 AtomicFile restrictionsFile) {
1692 FileOutputStream fos = null;
Amith Yamasanidf2e92a2013-03-01 17:04:38 -08001693 try {
1694 fos = restrictionsFile.startWrite();
1695 final BufferedOutputStream bos = new BufferedOutputStream(fos);
1696
Amith Yamasanidf2e92a2013-03-01 17:04:38 -08001697 final XmlSerializer serializer = new FastXmlSerializer();
Wojciech Staszkiewicz9e9e2e72015-05-08 14:58:46 +01001698 serializer.setOutput(bos, StandardCharsets.UTF_8.name());
Amith Yamasanidf2e92a2013-03-01 17:04:38 -08001699 serializer.startDocument(null, true);
1700 serializer.setFeature("http://xmlpull.org/v1/doc/features.html#indent-output", true);
1701
1702 serializer.startTag(null, TAG_RESTRICTIONS);
Fyodor Kupolov262f9952015-03-23 18:55:11 -07001703 writeBundle(restrictions, serializer);
Amith Yamasanidf2e92a2013-03-01 17:04:38 -08001704 serializer.endTag(null, TAG_RESTRICTIONS);
1705
1706 serializer.endDocument();
1707 restrictionsFile.finishWrite(fos);
1708 } catch (Exception e) {
1709 restrictionsFile.failWrite(fos);
Fyodor Kupolov262f9952015-03-23 18:55:11 -07001710 Slog.e(LOG_TAG, "Error writing application restrictions list", e);
1711 }
1712 }
1713
1714 private static void writeBundle(Bundle restrictions, XmlSerializer serializer)
1715 throws IOException {
1716 for (String key : restrictions.keySet()) {
1717 Object value = restrictions.get(key);
1718 serializer.startTag(null, TAG_ENTRY);
1719 serializer.attribute(null, ATTR_KEY, key);
1720
1721 if (value instanceof Boolean) {
1722 serializer.attribute(null, ATTR_VALUE_TYPE, ATTR_TYPE_BOOLEAN);
1723 serializer.text(value.toString());
1724 } else if (value instanceof Integer) {
1725 serializer.attribute(null, ATTR_VALUE_TYPE, ATTR_TYPE_INTEGER);
1726 serializer.text(value.toString());
1727 } else if (value == null || value instanceof String) {
1728 serializer.attribute(null, ATTR_VALUE_TYPE, ATTR_TYPE_STRING);
1729 serializer.text(value != null ? (String) value : "");
1730 } else if (value instanceof Bundle) {
1731 serializer.attribute(null, ATTR_VALUE_TYPE, ATTR_TYPE_BUNDLE);
1732 writeBundle((Bundle) value, serializer);
1733 } else if (value instanceof Parcelable[]) {
1734 serializer.attribute(null, ATTR_VALUE_TYPE, ATTR_TYPE_BUNDLE_ARRAY);
1735 Parcelable[] array = (Parcelable[]) value;
1736 for (Parcelable parcelable : array) {
1737 if (!(parcelable instanceof Bundle)) {
1738 throw new IllegalArgumentException("bundle-array can only hold Bundles");
1739 }
1740 serializer.startTag(null, TAG_ENTRY);
1741 serializer.attribute(null, ATTR_VALUE_TYPE, ATTR_TYPE_BUNDLE);
1742 writeBundle((Bundle) parcelable, serializer);
1743 serializer.endTag(null, TAG_ENTRY);
1744 }
1745 } else {
1746 serializer.attribute(null, ATTR_VALUE_TYPE, ATTR_TYPE_STRING_ARRAY);
1747 String[] values = (String[]) value;
1748 serializer.attribute(null, ATTR_MULTIPLE, Integer.toString(values.length));
1749 for (String choice : values) {
1750 serializer.startTag(null, TAG_VALUE);
1751 serializer.text(choice != null ? choice : "");
1752 serializer.endTag(null, TAG_VALUE);
1753 }
1754 }
1755 serializer.endTag(null, TAG_ENTRY);
Amith Yamasanidf2e92a2013-03-01 17:04:38 -08001756 }
1757 }
1758
1759 @Override
Amith Yamasani2a003292012-08-14 18:25:45 -07001760 public int getUserSerialNumber(int userHandle) {
Dianne Hackborn4428e172012-08-24 17:43:05 -07001761 synchronized (mPackagesLock) {
Amith Yamasani2a003292012-08-14 18:25:45 -07001762 if (!exists(userHandle)) return -1;
Amith Yamasani195263742012-08-21 15:40:12 -07001763 return getUserInfoLocked(userHandle).serialNumber;
Amith Yamasani2a003292012-08-14 18:25:45 -07001764 }
1765 }
1766
1767 @Override
1768 public int getUserHandle(int userSerialNumber) {
Dianne Hackborn4428e172012-08-24 17:43:05 -07001769 synchronized (mPackagesLock) {
Amith Yamasani2a003292012-08-14 18:25:45 -07001770 for (int userId : mUserIds) {
Kenny Guy945f8832015-02-10 15:17:26 +00001771 UserInfo info = getUserInfoLocked(userId);
1772 if (info != null && info.serialNumber == userSerialNumber) return userId;
Amith Yamasani2a003292012-08-14 18:25:45 -07001773 }
1774 // Not found
1775 return -1;
Amith Yamasani13593602012-03-22 16:16:17 -07001776 }
1777 }
1778
Fyodor Kupolovff7233e2015-04-08 11:28:52 -07001779 @Override
1780 public long getUserCreationTime(int userHandle) {
1781 int callingUserId = UserHandle.getCallingUserId();
1782 UserInfo userInfo = null;
1783 synchronized (mPackagesLock) {
1784 if (callingUserId == userHandle) {
1785 userInfo = getUserInfoLocked(userHandle);
1786 } else {
1787 UserInfo parent = getProfileParentLocked(userHandle);
1788 if (parent != null && parent.id == callingUserId) {
1789 userInfo = getUserInfoLocked(userHandle);
1790 }
1791 }
1792 }
1793 if (userInfo == null) {
1794 throw new SecurityException("userHandle can only be the calling user or a managed "
1795 + "profile associated with this user");
1796 }
1797 return userInfo.creationTime;
1798 }
1799
Amith Yamasani0b285492011-04-14 17:35:23 -07001800 /**
1801 * Caches the list of user ids in an array, adjusting the array size when necessary.
1802 */
Amith Yamasani13593602012-03-22 16:16:17 -07001803 private void updateUserIdsLocked() {
Dianne Hackbornd4ac8d72012-09-27 23:20:10 -07001804 int num = 0;
Amith Yamasani0b285492011-04-14 17:35:23 -07001805 for (int i = 0; i < mUsers.size(); i++) {
Dianne Hackbornd4ac8d72012-09-27 23:20:10 -07001806 if (!mUsers.valueAt(i).partial) {
1807 num++;
1808 }
1809 }
Amith Yamasani16389312012-10-17 21:20:14 -07001810 final int[] newUsers = new int[num];
1811 int n = 0;
Dianne Hackbornd4ac8d72012-09-27 23:20:10 -07001812 for (int i = 0; i < mUsers.size(); i++) {
1813 if (!mUsers.valueAt(i).partial) {
Amith Yamasani16389312012-10-17 21:20:14 -07001814 newUsers[n++] = mUsers.keyAt(i);
Dianne Hackbornd4ac8d72012-09-27 23:20:10 -07001815 }
Amith Yamasani0b285492011-04-14 17:35:23 -07001816 }
Dianne Hackborn7767eac2012-08-23 18:25:40 -07001817 mUserIds = newUsers;
Amith Yamasani0b285492011-04-14 17:35:23 -07001818 }
1819
1820 /**
Amith Yamasani1a7472e2013-07-02 11:17:30 -07001821 * Make a note of the last started time of a user and do some cleanup.
Amith Yamasani920ace02012-09-20 22:15:37 -07001822 * @param userId the user that was just foregrounded
1823 */
Amith Yamasani06bf8242015-05-08 16:36:21 -07001824 public void onUserForeground(int userId) {
Amith Yamasani920ace02012-09-20 22:15:37 -07001825 synchronized (mPackagesLock) {
1826 UserInfo user = mUsers.get(userId);
1827 long now = System.currentTimeMillis();
Dianne Hackbornd4ac8d72012-09-27 23:20:10 -07001828 if (user == null || user.partial) {
1829 Slog.w(LOG_TAG, "userForeground: unknown user #" + userId);
1830 return;
1831 }
1832 if (now > EPOCH_PLUS_30_YEARS) {
Amith Yamasani920ace02012-09-20 22:15:37 -07001833 user.lastLoggedInTime = now;
Fyodor Kupolov75d0ea82014-12-15 16:21:07 -08001834 scheduleWriteUserLocked(user);
Amith Yamasani920ace02012-09-20 22:15:37 -07001835 }
1836 }
1837 }
1838
1839 /**
Amith Yamasani0b285492011-04-14 17:35:23 -07001840 * Returns the next available user id, filling in any holes in the ids.
Amith Yamasani742a6712011-05-04 14:49:28 -07001841 * TODO: May not be a good idea to recycle ids, in case it results in confusion
1842 * for data and battery stats collection, or unexpected cross-talk.
Amith Yamasani0b285492011-04-14 17:35:23 -07001843 * @return
1844 */
Dianne Hackborn5dc5a002012-09-15 19:33:48 -07001845 private int getNextAvailableIdLocked() {
Dianne Hackborn4428e172012-08-24 17:43:05 -07001846 synchronized (mPackagesLock) {
Jeff Sharkeyffe0cb42012-11-05 17:24:43 -08001847 int i = MIN_USER_ID;
Amith Yamasani195263742012-08-21 15:40:12 -07001848 while (i < Integer.MAX_VALUE) {
Jeff Sharkeyffe0cb42012-11-05 17:24:43 -08001849 if (mUsers.indexOfKey(i) < 0 && !mRemovingUserIds.get(i)) {
Amith Yamasani195263742012-08-21 15:40:12 -07001850 break;
1851 }
1852 i++;
Amith Yamasani4b2e9342011-03-31 12:38:53 -07001853 }
Amith Yamasani195263742012-08-21 15:40:12 -07001854 return i;
Amith Yamasani4b2e9342011-03-31 12:38:53 -07001855 }
Amith Yamasani4b2e9342011-03-31 12:38:53 -07001856 }
Amith Yamasani920ace02012-09-20 22:15:37 -07001857
Amith Yamasanifc95e702013-09-26 13:20:17 -07001858 private String packageToRestrictionsFileName(String packageName) {
1859 return RESTRICTIONS_FILE_PREFIX + packageName + XML_SUFFIX;
1860 }
1861
Amith Yamasani920ace02012-09-20 22:15:37 -07001862 @Override
1863 protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
1864 if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.DUMP)
1865 != PackageManager.PERMISSION_GRANTED) {
1866 pw.println("Permission Denial: can't dump UserManager from from pid="
1867 + Binder.getCallingPid()
1868 + ", uid=" + Binder.getCallingUid()
1869 + " without permission "
1870 + android.Manifest.permission.DUMP);
1871 return;
1872 }
1873
1874 long now = System.currentTimeMillis();
1875 StringBuilder sb = new StringBuilder();
1876 synchronized (mPackagesLock) {
1877 pw.println("Users:");
1878 for (int i = 0; i < mUsers.size(); i++) {
1879 UserInfo user = mUsers.valueAt(i);
1880 if (user == null) continue;
Amith Yamasani634cf312012-10-04 17:34:21 -07001881 pw.print(" "); pw.print(user); pw.print(" serialNo="); pw.print(user.serialNumber);
Jeff Sharkeyffe0cb42012-11-05 17:24:43 -08001882 if (mRemovingUserIds.get(mUsers.keyAt(i))) pw.print(" <removing> ");
Dianne Hackbornd4ac8d72012-09-27 23:20:10 -07001883 if (user.partial) pw.print(" <partial>");
1884 pw.println();
Amith Yamasani920ace02012-09-20 22:15:37 -07001885 pw.print(" Created: ");
1886 if (user.creationTime == 0) {
1887 pw.println("<unknown>");
1888 } else {
1889 sb.setLength(0);
1890 TimeUtils.formatDuration(now - user.creationTime, sb);
1891 sb.append(" ago");
1892 pw.println(sb);
1893 }
1894 pw.print(" Last logged in: ");
1895 if (user.lastLoggedInTime == 0) {
1896 pw.println("<unknown>");
1897 } else {
1898 sb.setLength(0);
1899 TimeUtils.formatDuration(now - user.lastLoggedInTime, sb);
1900 sb.append(" ago");
1901 pw.println(sb);
1902 }
1903 }
1904 }
1905 }
Fyodor Kupolov75d0ea82014-12-15 16:21:07 -08001906
1907 final class MainHandler extends Handler {
1908
1909 @Override
1910 public void handleMessage(Message msg) {
1911 switch (msg.what) {
1912 case WRITE_USER_MSG:
1913 removeMessages(WRITE_USER_MSG, msg.obj);
1914 synchronized (mPackagesLock) {
1915 int userId = ((UserInfo) msg.obj).id;
1916 UserInfo userInfo = mUsers.get(userId);
1917 if (userInfo != null) {
1918 writeUserLocked(userInfo);
1919 }
1920 }
1921 }
1922 }
1923 }
Amith Yamasani4b2e9342011-03-31 12:38:53 -07001924}