blob: d8594424b09ec2797e9ae28eaec6a5c3465b96f8 [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 }
593 synchronized(mPackagesLock) {
594 // Limit number of managed profiles that can be created
595 if (numberOfUsersOfTypeLocked(UserInfo.FLAG_MANAGED_PROFILE, true)
596 >= MAX_MANAGED_PROFILES) {
597 return false;
598 }
599 int usersCount = getAliveUsersExcludingGuestsCountLocked();
600 // We allow creating a managed profile in the special case where there is only one user.
601 return usersCount == 1 || usersCount < UserManager.getMaxSupportedUsers();
602 }
603 }
604
605 private int getAliveUsersExcludingGuestsCountLocked() {
Amith Yamasanif584f012014-05-19 17:57:25 -0700606 int aliveUserCount = 0;
607 final int totalUserCount = mUsers.size();
608 // Skip over users being removed
609 for (int i = 0; i < totalUserCount; i++) {
610 UserInfo user = mUsers.valueAt(i);
Amith Yamasani95ab7842014-08-11 17:09:26 -0700611 if (!mRemovingUserIds.get(user.id)
Amith Yamasani1df14732014-08-29 21:37:27 -0700612 && !user.isGuest() && !user.partial) {
Amith Yamasanif584f012014-05-19 17:57:25 -0700613 aliveUserCount++;
614 }
615 }
Nicolas Prevot12678a92015-05-13 12:15:03 -0700616 return aliveUserCount;
Amith Yamasanifaea76f2012-09-11 10:59:48 -0700617 }
618
619 /**
Amith Yamasani195263742012-08-21 15:40:12 -0700620 * Enforces that only the system UID or root's UID or apps that have the
Dianne Hackborn10ad9822014-03-17 11:28:36 -0700621 * {@link android.Manifest.permission#MANAGE_USERS MANAGE_USERS}
Amith Yamasani195263742012-08-21 15:40:12 -0700622 * permission can make certain calls to the UserManager.
Amith Yamasani258848d2012-08-10 17:06:33 -0700623 *
624 * @param message used as message if SecurityException is thrown
625 * @throws SecurityException if the caller is not system or root
626 */
Amith Yamasanibe465322014-04-24 13:45:17 -0700627 private static final void checkManageUsersPermission(String message) {
Amith Yamasani258848d2012-08-10 17:06:33 -0700628 final int uid = Binder.getCallingUid();
Amith Yamasanibe465322014-04-24 13:45:17 -0700629 if (uid != Process.SYSTEM_UID && uid != 0
Emily Bernier7a2b4d12014-04-23 12:51:35 -0400630 && ActivityManager.checkComponentPermission(
631 android.Manifest.permission.MANAGE_USERS,
Amith Yamasanibe465322014-04-24 13:45:17 -0700632 uid, -1, true) != PackageManager.PERMISSION_GRANTED) {
633 throw new SecurityException("You need MANAGE_USERS permission to: " + message);
634 }
Emily Bernier7a2b4d12014-04-23 12:51:35 -0400635 }
636
Fyodor Kupolovb5013302015-04-17 17:59:14 -0700637 private static void checkSystemOrRoot(String message) {
638 final int uid = Binder.getCallingUid();
639 if (uid != Process.SYSTEM_UID && uid != 0) {
640 throw new SecurityException("Only system may call: " + message);
641 }
642 }
643
Amith Yamasanie928d7d2012-09-17 21:46:51 -0700644 private void writeBitmapLocked(UserInfo info, Bitmap bitmap) {
Amith Yamasanib8151ec2012-04-18 18:02:48 -0700645 try {
646 File dir = new File(mUsersDir, Integer.toString(info.id));
647 File file = new File(dir, USER_PHOTO_FILENAME);
Adrian Roos1bdff912015-02-17 15:51:35 +0100648 File tmp = new File(dir, USER_PHOTO_FILENAME_TMP);
Amith Yamasanib8151ec2012-04-18 18:02:48 -0700649 if (!dir.exists()) {
650 dir.mkdir();
651 FileUtils.setPermissions(
652 dir.getPath(),
653 FileUtils.S_IRWXU|FileUtils.S_IRWXG|FileUtils.S_IXOTH,
654 -1, -1);
655 }
Amith Yamasanie928d7d2012-09-17 21:46:51 -0700656 FileOutputStream os;
Adrian Roos1bdff912015-02-17 15:51:35 +0100657 if (bitmap.compress(Bitmap.CompressFormat.PNG, 100, os = new FileOutputStream(tmp))
658 && tmp.renameTo(file)) {
Amith Yamasani3b49f072012-09-17 10:21:43 -0700659 info.iconPath = file.getAbsolutePath();
660 }
Amith Yamasanie928d7d2012-09-17 21:46:51 -0700661 try {
662 os.close();
663 } catch (IOException ioe) {
664 // What the ... !
665 }
Adrian Roos1bdff912015-02-17 15:51:35 +0100666 tmp.delete();
Amith Yamasanib8151ec2012-04-18 18:02:48 -0700667 } catch (FileNotFoundException e) {
Amith Yamasani2a003292012-08-14 18:25:45 -0700668 Slog.w(LOG_TAG, "Error setting photo for user ", e);
Amith Yamasanib8151ec2012-04-18 18:02:48 -0700669 }
Amith Yamasanib8151ec2012-04-18 18:02:48 -0700670 }
671
Amith Yamasani0b285492011-04-14 17:35:23 -0700672 /**
673 * Returns an array of user ids. This array is cached here for quick access, so do not modify or
674 * cache it elsewhere.
675 * @return the array of user ids.
676 */
Dianne Hackborn1676c852012-09-10 14:52:30 -0700677 public int[] getUserIds() {
Dianne Hackborn4428e172012-08-24 17:43:05 -0700678 synchronized (mPackagesLock) {
Dianne Hackborn7767eac2012-08-23 18:25:40 -0700679 return mUserIds;
680 }
Amith Yamasani0b285492011-04-14 17:35:23 -0700681 }
682
Dianne Hackborn4428e172012-08-24 17:43:05 -0700683 int[] getUserIdsLPr() {
684 return mUserIds;
685 }
686
Amith Yamasani13593602012-03-22 16:16:17 -0700687 private void readUserListLocked() {
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700688 if (!mUserListFile.exists()) {
Amith Yamasani13593602012-03-22 16:16:17 -0700689 fallbackToSingleUserLocked();
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700690 return;
691 }
692 FileInputStream fis = null;
Amith Yamasani2a003292012-08-14 18:25:45 -0700693 AtomicFile userListFile = new AtomicFile(mUserListFile);
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700694 try {
Amith Yamasani2a003292012-08-14 18:25:45 -0700695 fis = userListFile.openRead();
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700696 XmlPullParser parser = Xml.newPullParser();
Wojciech Staszkiewicz9e9e2e72015-05-08 14:58:46 +0100697 parser.setInput(fis, StandardCharsets.UTF_8.name());
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700698 int type;
699 while ((type = parser.next()) != XmlPullParser.START_TAG
700 && type != XmlPullParser.END_DOCUMENT) {
701 ;
702 }
703
704 if (type != XmlPullParser.START_TAG) {
Amith Yamasani0b285492011-04-14 17:35:23 -0700705 Slog.e(LOG_TAG, "Unable to read user list");
Amith Yamasani13593602012-03-22 16:16:17 -0700706 fallbackToSingleUserLocked();
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700707 return;
708 }
709
Amith Yamasani2a003292012-08-14 18:25:45 -0700710 mNextSerialNumber = -1;
711 if (parser.getName().equals(TAG_USERS)) {
712 String lastSerialNumber = parser.getAttributeValue(null, ATTR_NEXT_SERIAL_NO);
713 if (lastSerialNumber != null) {
714 mNextSerialNumber = Integer.parseInt(lastSerialNumber);
715 }
Amith Yamasani6f34b412012-10-22 18:19:27 -0700716 String versionNumber = parser.getAttributeValue(null, ATTR_USER_VERSION);
717 if (versionNumber != null) {
718 mUserVersion = Integer.parseInt(versionNumber);
719 }
Amith Yamasani2a003292012-08-14 18:25:45 -0700720 }
721
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700722 while ((type = parser.next()) != XmlPullParser.END_DOCUMENT) {
Amith Yamasanie4afaa32014-06-30 14:55:07 +0530723 if (type == XmlPullParser.START_TAG) {
724 final String name = parser.getName();
725 if (name.equals(TAG_USER)) {
726 String id = parser.getAttributeValue(null, ATTR_ID);
727 UserInfo user = readUserLocked(Integer.parseInt(id));
Amith Yamasani6f34b412012-10-22 18:19:27 -0700728
Amith Yamasanie4afaa32014-06-30 14:55:07 +0530729 if (user != null) {
730 mUsers.put(user.id, user);
731 if (mNextSerialNumber < 0 || mNextSerialNumber <= user.id) {
732 mNextSerialNumber = user.id + 1;
733 }
Amith Yamasani2a003292012-08-14 18:25:45 -0700734 }
Amith Yamasanie4afaa32014-06-30 14:55:07 +0530735 } else if (name.equals(TAG_GUEST_RESTRICTIONS)) {
Amith Yamasanida0b1682014-11-21 12:58:17 -0800736 while ((type = parser.next()) != XmlPullParser.END_DOCUMENT
737 && type != XmlPullParser.END_TAG) {
738 if (type == XmlPullParser.START_TAG) {
739 if (parser.getName().equals(TAG_RESTRICTIONS)) {
740 readRestrictionsLocked(parser, mGuestRestrictions);
741 }
742 break;
743 }
744 }
Amith Yamasani258848d2012-08-10 17:06:33 -0700745 }
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700746 }
747 }
Amith Yamasani13593602012-03-22 16:16:17 -0700748 updateUserIdsLocked();
Amith Yamasani350962c2013-08-06 11:18:53 -0700749 upgradeIfNecessaryLocked();
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700750 } catch (IOException ioe) {
Amith Yamasani13593602012-03-22 16:16:17 -0700751 fallbackToSingleUserLocked();
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700752 } catch (XmlPullParserException pe) {
Amith Yamasani13593602012-03-22 16:16:17 -0700753 fallbackToSingleUserLocked();
Dianne Hackbornbfd89b32011-12-15 18:22:54 -0800754 } finally {
755 if (fis != null) {
756 try {
757 fis.close();
758 } catch (IOException e) {
759 }
760 }
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700761 }
762 }
763
Amith Yamasani6f34b412012-10-22 18:19:27 -0700764 /**
Amith Yamasanibc9625052012-11-15 14:39:18 -0800765 * Upgrade steps between versions, either for fixing bugs or changing the data format.
Amith Yamasani6f34b412012-10-22 18:19:27 -0700766 */
Amith Yamasani350962c2013-08-06 11:18:53 -0700767 private void upgradeIfNecessaryLocked() {
Amith Yamasani6f34b412012-10-22 18:19:27 -0700768 int userVersion = mUserVersion;
769 if (userVersion < 1) {
770 // Assign a proper name for the owner, if not initialized correctly before
771 UserInfo user = mUsers.get(UserHandle.USER_OWNER);
772 if ("Primary".equals(user.name)) {
773 user.name = mContext.getResources().getString(com.android.internal.R.string.owner_name);
Fyodor Kupolov75d0ea82014-12-15 16:21:07 -0800774 scheduleWriteUserLocked(user);
Amith Yamasani6f34b412012-10-22 18:19:27 -0700775 }
776 userVersion = 1;
777 }
778
Amith Yamasanibc9625052012-11-15 14:39:18 -0800779 if (userVersion < 2) {
780 // Owner should be marked as initialized
781 UserInfo user = mUsers.get(UserHandle.USER_OWNER);
782 if ((user.flags & UserInfo.FLAG_INITIALIZED) == 0) {
783 user.flags |= UserInfo.FLAG_INITIALIZED;
Fyodor Kupolov75d0ea82014-12-15 16:21:07 -0800784 scheduleWriteUserLocked(user);
Amith Yamasanibc9625052012-11-15 14:39:18 -0800785 }
786 userVersion = 2;
787 }
788
Amith Yamasani350962c2013-08-06 11:18:53 -0700789
Amith Yamasani5e486f52013-08-07 11:06:44 -0700790 if (userVersion < 4) {
Amith Yamasani5e486f52013-08-07 11:06:44 -0700791 userVersion = 4;
792 }
793
Amith Yamasaniaa6634e2014-10-06 14:20:28 -0700794 if (userVersion < 5) {
795 initDefaultGuestRestrictions();
796 userVersion = 5;
797 }
798
Amith Yamasani6f34b412012-10-22 18:19:27 -0700799 if (userVersion < USER_VERSION) {
800 Slog.w(LOG_TAG, "User version " + mUserVersion + " didn't upgrade as expected to "
801 + USER_VERSION);
802 } else {
803 mUserVersion = userVersion;
804 writeUserListLocked();
805 }
806 }
807
Amith Yamasani13593602012-03-22 16:16:17 -0700808 private void fallbackToSingleUserLocked() {
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700809 // Create the primary user
Amith Yamasani67df64b2012-12-14 12:09:36 -0800810 UserInfo primary = new UserInfo(UserHandle.USER_OWNER,
Amith Yamasani6f34b412012-10-22 18:19:27 -0700811 mContext.getResources().getString(com.android.internal.R.string.owner_name), null,
Amith Yamasani756901d2012-10-12 12:30:07 -0700812 UserInfo.FLAG_ADMIN | UserInfo.FLAG_PRIMARY | UserInfo.FLAG_INITIALIZED);
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700813 mUsers.put(0, primary);
Amith Yamasani634cf312012-10-04 17:34:21 -0700814 mNextSerialNumber = MIN_USER_ID;
Geoffrey Borggaard15b8b2c2013-08-28 22:11:10 -0400815 mUserVersion = USER_VERSION;
Amith Yamasani67df64b2012-12-14 12:09:36 -0800816
Geoffrey Borggaarde45e45e32013-01-24 10:03:20 -0500817 Bundle restrictions = new Bundle();
Amith Yamasani67df64b2012-12-14 12:09:36 -0800818 mUserRestrictions.append(UserHandle.USER_OWNER, restrictions);
819
Amith Yamasani13593602012-03-22 16:16:17 -0700820 updateUserIdsLocked();
Amith Yamasaniaa6634e2014-10-06 14:20:28 -0700821 initDefaultGuestRestrictions();
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700822
Amith Yamasani13593602012-03-22 16:16:17 -0700823 writeUserListLocked();
824 writeUserLocked(primary);
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700825 }
826
Fyodor Kupolov75d0ea82014-12-15 16:21:07 -0800827 private void scheduleWriteUserLocked(UserInfo userInfo) {
828 if (!mHandler.hasMessages(WRITE_USER_MSG, userInfo)) {
829 Message msg = mHandler.obtainMessage(WRITE_USER_MSG, userInfo);
830 mHandler.sendMessageDelayed(msg, WRITE_USER_DELAY);
831 }
832 }
833
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700834 /*
835 * Writes the user file in this format:
836 *
837 * <user flags="20039023" id="0">
838 * <name>Primary</name>
839 * </user>
840 */
Amith Yamasani13593602012-03-22 16:16:17 -0700841 private void writeUserLocked(UserInfo userInfo) {
Amith Yamasani742a6712011-05-04 14:49:28 -0700842 FileOutputStream fos = null;
Amith Yamasanifc95e702013-09-26 13:20:17 -0700843 AtomicFile userFile = new AtomicFile(new File(mUsersDir, userInfo.id + XML_SUFFIX));
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700844 try {
Amith Yamasani2a003292012-08-14 18:25:45 -0700845 fos = userFile.startWrite();
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700846 final BufferedOutputStream bos = new BufferedOutputStream(fos);
847
848 // XmlSerializer serializer = XmlUtils.serializerInstance();
849 final XmlSerializer serializer = new FastXmlSerializer();
Wojciech Staszkiewicz9e9e2e72015-05-08 14:58:46 +0100850 serializer.setOutput(bos, StandardCharsets.UTF_8.name());
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700851 serializer.startDocument(null, true);
852 serializer.setFeature("http://xmlpull.org/v1/doc/features.html#indent-output", true);
853
854 serializer.startTag(null, TAG_USER);
855 serializer.attribute(null, ATTR_ID, Integer.toString(userInfo.id));
Amith Yamasani2a003292012-08-14 18:25:45 -0700856 serializer.attribute(null, ATTR_SERIAL_NO, Integer.toString(userInfo.serialNumber));
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700857 serializer.attribute(null, ATTR_FLAGS, Integer.toString(userInfo.flags));
Amith Yamasani920ace02012-09-20 22:15:37 -0700858 serializer.attribute(null, ATTR_CREATION_TIME, Long.toString(userInfo.creationTime));
859 serializer.attribute(null, ATTR_LAST_LOGGED_IN_TIME,
860 Long.toString(userInfo.lastLoggedInTime));
Amith Yamasanib8151ec2012-04-18 18:02:48 -0700861 if (userInfo.iconPath != null) {
862 serializer.attribute(null, ATTR_ICON_PATH, userInfo.iconPath);
863 }
Dianne Hackbornd4ac8d72012-09-27 23:20:10 -0700864 if (userInfo.partial) {
865 serializer.attribute(null, ATTR_PARTIAL, "true");
866 }
Adam Lesinskieddeb492014-09-08 17:50:03 -0700867 if (userInfo.guestToRemove) {
868 serializer.attribute(null, ATTR_GUEST_TO_REMOVE, "true");
869 }
Kenny Guy2a764942014-04-02 13:29:20 +0100870 if (userInfo.profileGroupId != UserInfo.NO_PROFILE_GROUP_ID) {
871 serializer.attribute(null, ATTR_PROFILE_GROUP_ID,
872 Integer.toString(userInfo.profileGroupId));
Kenny Guya52dc3e2014-02-11 15:33:14 +0000873 }
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700874
875 serializer.startTag(null, TAG_NAME);
876 serializer.text(userInfo.name);
877 serializer.endTag(null, TAG_NAME);
Amith Yamasanie4cf7342012-12-17 11:12:09 -0800878 Bundle restrictions = mUserRestrictions.get(userInfo.id);
879 if (restrictions != null) {
Amith Yamasanie4afaa32014-06-30 14:55:07 +0530880 writeRestrictionsLocked(serializer, restrictions);
Amith Yamasanie4cf7342012-12-17 11:12:09 -0800881 }
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700882 serializer.endTag(null, TAG_USER);
883
884 serializer.endDocument();
Amith Yamasani2a003292012-08-14 18:25:45 -0700885 userFile.finishWrite(fos);
886 } catch (Exception ioe) {
Amith Yamasani0b285492011-04-14 17:35:23 -0700887 Slog.e(LOG_TAG, "Error writing user info " + userInfo.id + "\n" + ioe);
Amith Yamasani2a003292012-08-14 18:25:45 -0700888 userFile.failWrite(fos);
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700889 }
890 }
891
892 /*
893 * Writes the user list file in this format:
894 *
Amith Yamasani2a003292012-08-14 18:25:45 -0700895 * <users nextSerialNumber="3">
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700896 * <user id="0"></user>
897 * <user id="2"></user>
898 * </users>
899 */
Amith Yamasani13593602012-03-22 16:16:17 -0700900 private void writeUserListLocked() {
Amith Yamasani742a6712011-05-04 14:49:28 -0700901 FileOutputStream fos = null;
Amith Yamasani2a003292012-08-14 18:25:45 -0700902 AtomicFile userListFile = new AtomicFile(mUserListFile);
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700903 try {
Amith Yamasani2a003292012-08-14 18:25:45 -0700904 fos = userListFile.startWrite();
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700905 final BufferedOutputStream bos = new BufferedOutputStream(fos);
906
907 // XmlSerializer serializer = XmlUtils.serializerInstance();
908 final XmlSerializer serializer = new FastXmlSerializer();
Wojciech Staszkiewicz9e9e2e72015-05-08 14:58:46 +0100909 serializer.setOutput(bos, StandardCharsets.UTF_8.name());
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700910 serializer.startDocument(null, true);
911 serializer.setFeature("http://xmlpull.org/v1/doc/features.html#indent-output", true);
912
913 serializer.startTag(null, TAG_USERS);
Amith Yamasani2a003292012-08-14 18:25:45 -0700914 serializer.attribute(null, ATTR_NEXT_SERIAL_NO, Integer.toString(mNextSerialNumber));
Amith Yamasani6f34b412012-10-22 18:19:27 -0700915 serializer.attribute(null, ATTR_USER_VERSION, Integer.toString(mUserVersion));
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700916
Adam Lesinskieddeb492014-09-08 17:50:03 -0700917 serializer.startTag(null, TAG_GUEST_RESTRICTIONS);
Amith Yamasanie4afaa32014-06-30 14:55:07 +0530918 writeRestrictionsLocked(serializer, mGuestRestrictions);
919 serializer.endTag(null, TAG_GUEST_RESTRICTIONS);
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700920 for (int i = 0; i < mUsers.size(); i++) {
921 UserInfo user = mUsers.valueAt(i);
922 serializer.startTag(null, TAG_USER);
923 serializer.attribute(null, ATTR_ID, Integer.toString(user.id));
924 serializer.endTag(null, TAG_USER);
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700925 }
926
927 serializer.endTag(null, TAG_USERS);
928
929 serializer.endDocument();
Amith Yamasani2a003292012-08-14 18:25:45 -0700930 userListFile.finishWrite(fos);
931 } catch (Exception e) {
932 userListFile.failWrite(fos);
Amith Yamasani0b285492011-04-14 17:35:23 -0700933 Slog.e(LOG_TAG, "Error writing user list");
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700934 }
935 }
936
Amith Yamasanie4afaa32014-06-30 14:55:07 +0530937 private void writeRestrictionsLocked(XmlSerializer serializer, Bundle restrictions)
938 throws IOException {
939 serializer.startTag(null, TAG_RESTRICTIONS);
940 writeBoolean(serializer, restrictions, UserManager.DISALLOW_CONFIG_WIFI);
941 writeBoolean(serializer, restrictions, UserManager.DISALLOW_MODIFY_ACCOUNTS);
942 writeBoolean(serializer, restrictions, UserManager.DISALLOW_INSTALL_APPS);
943 writeBoolean(serializer, restrictions, UserManager.DISALLOW_UNINSTALL_APPS);
944 writeBoolean(serializer, restrictions, UserManager.DISALLOW_SHARE_LOCATION);
945 writeBoolean(serializer, restrictions,
946 UserManager.DISALLOW_INSTALL_UNKNOWN_SOURCES);
947 writeBoolean(serializer, restrictions, UserManager.DISALLOW_CONFIG_BLUETOOTH);
948 writeBoolean(serializer, restrictions, UserManager.DISALLOW_USB_FILE_TRANSFER);
949 writeBoolean(serializer, restrictions, UserManager.DISALLOW_CONFIG_CREDENTIALS);
950 writeBoolean(serializer, restrictions, UserManager.DISALLOW_REMOVE_USER);
951 writeBoolean(serializer, restrictions, UserManager.DISALLOW_DEBUGGING_FEATURES);
952 writeBoolean(serializer, restrictions, UserManager.DISALLOW_CONFIG_VPN);
953 writeBoolean(serializer, restrictions, UserManager.DISALLOW_CONFIG_TETHERING);
954 writeBoolean(serializer, restrictions, UserManager.DISALLOW_FACTORY_RESET);
955 writeBoolean(serializer, restrictions, UserManager.DISALLOW_ADD_USER);
956 writeBoolean(serializer, restrictions, UserManager.ENSURE_VERIFY_APPS);
957 writeBoolean(serializer, restrictions, UserManager.DISALLOW_CONFIG_CELL_BROADCASTS);
958 writeBoolean(serializer, restrictions, UserManager.DISALLOW_CONFIG_MOBILE_NETWORKS);
959 writeBoolean(serializer, restrictions, UserManager.DISALLOW_APPS_CONTROL);
960 writeBoolean(serializer, restrictions, UserManager.DISALLOW_MOUNT_PHYSICAL_MEDIA);
961 writeBoolean(serializer, restrictions, UserManager.DISALLOW_UNMUTE_MICROPHONE);
962 writeBoolean(serializer, restrictions, UserManager.DISALLOW_ADJUST_VOLUME);
Amith Yamasani390989d2014-07-17 10:52:03 -0700963 writeBoolean(serializer, restrictions, UserManager.DISALLOW_OUTGOING_CALLS);
964 writeBoolean(serializer, restrictions, UserManager.DISALLOW_SMS);
Jason Monk1c7c3192014-06-26 12:52:18 -0400965 writeBoolean(serializer, restrictions, UserManager.DISALLOW_CREATE_WINDOWS);
Amith Yamasani26af8292014-09-09 09:57:27 -0700966 writeBoolean(serializer, restrictions, UserManager.DISALLOW_CROSS_PROFILE_COPY_PASTE);
967 writeBoolean(serializer, restrictions, UserManager.DISALLOW_OUTGOING_BEAM);
Benjamin Franzf3ece362015-02-11 10:51:10 +0000968 writeBoolean(serializer, restrictions, UserManager.DISALLOW_WALLPAPER);
Benjamin Franzbff46ba2015-03-05 18:33:51 +0000969 writeBoolean(serializer, restrictions, UserManager.DISALLOW_SAFE_BOOT);
Amith Yamasanie4afaa32014-06-30 14:55:07 +0530970 serializer.endTag(null, TAG_RESTRICTIONS);
971 }
972
Amith Yamasanie4cf7342012-12-17 11:12:09 -0800973 private UserInfo readUserLocked(int id) {
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700974 int flags = 0;
Amith Yamasani2a003292012-08-14 18:25:45 -0700975 int serialNumber = id;
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700976 String name = null;
Amith Yamasanib8151ec2012-04-18 18:02:48 -0700977 String iconPath = null;
Amith Yamasani920ace02012-09-20 22:15:37 -0700978 long creationTime = 0L;
979 long lastLoggedInTime = 0L;
Kenny Guy2a764942014-04-02 13:29:20 +0100980 int profileGroupId = UserInfo.NO_PROFILE_GROUP_ID;
Dianne Hackbornd4ac8d72012-09-27 23:20:10 -0700981 boolean partial = false;
Adam Lesinskieddeb492014-09-08 17:50:03 -0700982 boolean guestToRemove = false;
Amith Yamasanie4cf7342012-12-17 11:12:09 -0800983 Bundle restrictions = new Bundle();
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700984
985 FileInputStream fis = null;
986 try {
Amith Yamasani2a003292012-08-14 18:25:45 -0700987 AtomicFile userFile =
Amith Yamasanifc95e702013-09-26 13:20:17 -0700988 new AtomicFile(new File(mUsersDir, Integer.toString(id) + XML_SUFFIX));
Amith Yamasani2a003292012-08-14 18:25:45 -0700989 fis = userFile.openRead();
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700990 XmlPullParser parser = Xml.newPullParser();
Wojciech Staszkiewicz9e9e2e72015-05-08 14:58:46 +0100991 parser.setInput(fis, StandardCharsets.UTF_8.name());
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700992 int type;
993 while ((type = parser.next()) != XmlPullParser.START_TAG
994 && type != XmlPullParser.END_DOCUMENT) {
995 ;
996 }
997
998 if (type != XmlPullParser.START_TAG) {
Amith Yamasani0b285492011-04-14 17:35:23 -0700999 Slog.e(LOG_TAG, "Unable to read user " + id);
Amith Yamasani4b2e9342011-03-31 12:38:53 -07001000 return null;
1001 }
1002
1003 if (type == XmlPullParser.START_TAG && parser.getName().equals(TAG_USER)) {
Amith Yamasani920ace02012-09-20 22:15:37 -07001004 int storedId = readIntAttribute(parser, ATTR_ID, -1);
1005 if (storedId != id) {
Amith Yamasani0b285492011-04-14 17:35:23 -07001006 Slog.e(LOG_TAG, "User id does not match the file name");
Amith Yamasani4b2e9342011-03-31 12:38:53 -07001007 return null;
1008 }
Amith Yamasani920ace02012-09-20 22:15:37 -07001009 serialNumber = readIntAttribute(parser, ATTR_SERIAL_NO, id);
1010 flags = readIntAttribute(parser, ATTR_FLAGS, 0);
Amith Yamasanib8151ec2012-04-18 18:02:48 -07001011 iconPath = parser.getAttributeValue(null, ATTR_ICON_PATH);
Amith Yamasani920ace02012-09-20 22:15:37 -07001012 creationTime = readLongAttribute(parser, ATTR_CREATION_TIME, 0);
1013 lastLoggedInTime = readLongAttribute(parser, ATTR_LAST_LOGGED_IN_TIME, 0);
Kenny Guy2a764942014-04-02 13:29:20 +01001014 profileGroupId = readIntAttribute(parser, ATTR_PROFILE_GROUP_ID,
1015 UserInfo.NO_PROFILE_GROUP_ID);
Dianne Hackbornd4ac8d72012-09-27 23:20:10 -07001016 String valueString = parser.getAttributeValue(null, ATTR_PARTIAL);
1017 if ("true".equals(valueString)) {
1018 partial = true;
1019 }
Adam Lesinskieddeb492014-09-08 17:50:03 -07001020 valueString = parser.getAttributeValue(null, ATTR_GUEST_TO_REMOVE);
1021 if ("true".equals(valueString)) {
1022 guestToRemove = true;
1023 }
Amith Yamasani4b2e9342011-03-31 12:38:53 -07001024
Amith Yamasanie4cf7342012-12-17 11:12:09 -08001025 int outerDepth = parser.getDepth();
1026 while ((type = parser.next()) != XmlPullParser.END_DOCUMENT
1027 && (type != XmlPullParser.END_TAG || parser.getDepth() > outerDepth)) {
1028 if (type == XmlPullParser.END_TAG || type == XmlPullParser.TEXT) {
1029 continue;
1030 }
1031 String tag = parser.getName();
1032 if (TAG_NAME.equals(tag)) {
1033 type = parser.next();
1034 if (type == XmlPullParser.TEXT) {
1035 name = parser.getText();
1036 }
1037 } else if (TAG_RESTRICTIONS.equals(tag)) {
Amith Yamasanie4afaa32014-06-30 14:55:07 +05301038 readRestrictionsLocked(parser, restrictions);
Amith Yamasani4b2e9342011-03-31 12:38:53 -07001039 }
1040 }
1041 }
Amith Yamasani4b2e9342011-03-31 12:38:53 -07001042
Amith Yamasanib8151ec2012-04-18 18:02:48 -07001043 UserInfo userInfo = new UserInfo(id, name, iconPath, flags);
Amith Yamasani2a003292012-08-14 18:25:45 -07001044 userInfo.serialNumber = serialNumber;
Amith Yamasani920ace02012-09-20 22:15:37 -07001045 userInfo.creationTime = creationTime;
1046 userInfo.lastLoggedInTime = lastLoggedInTime;
Dianne Hackbornd4ac8d72012-09-27 23:20:10 -07001047 userInfo.partial = partial;
Adam Lesinskieddeb492014-09-08 17:50:03 -07001048 userInfo.guestToRemove = guestToRemove;
Kenny Guy2a764942014-04-02 13:29:20 +01001049 userInfo.profileGroupId = profileGroupId;
Amith Yamasanie4cf7342012-12-17 11:12:09 -08001050 mUserRestrictions.append(id, restrictions);
Amith Yamasani4b2e9342011-03-31 12:38:53 -07001051 return userInfo;
1052
1053 } catch (IOException ioe) {
1054 } catch (XmlPullParserException pe) {
Dianne Hackbornbfd89b32011-12-15 18:22:54 -08001055 } finally {
1056 if (fis != null) {
1057 try {
1058 fis.close();
1059 } catch (IOException e) {
1060 }
1061 }
Amith Yamasani4b2e9342011-03-31 12:38:53 -07001062 }
1063 return null;
1064 }
1065
Amith Yamasanie4afaa32014-06-30 14:55:07 +05301066 private void readRestrictionsLocked(XmlPullParser parser, Bundle restrictions)
1067 throws IOException {
1068 readBoolean(parser, restrictions, UserManager.DISALLOW_CONFIG_WIFI);
1069 readBoolean(parser, restrictions, UserManager.DISALLOW_MODIFY_ACCOUNTS);
1070 readBoolean(parser, restrictions, UserManager.DISALLOW_INSTALL_APPS);
1071 readBoolean(parser, restrictions, UserManager.DISALLOW_UNINSTALL_APPS);
1072 readBoolean(parser, restrictions, UserManager.DISALLOW_SHARE_LOCATION);
1073 readBoolean(parser, restrictions,
1074 UserManager.DISALLOW_INSTALL_UNKNOWN_SOURCES);
1075 readBoolean(parser, restrictions, UserManager.DISALLOW_CONFIG_BLUETOOTH);
1076 readBoolean(parser, restrictions, UserManager.DISALLOW_USB_FILE_TRANSFER);
1077 readBoolean(parser, restrictions, UserManager.DISALLOW_CONFIG_CREDENTIALS);
1078 readBoolean(parser, restrictions, UserManager.DISALLOW_REMOVE_USER);
1079 readBoolean(parser, restrictions, UserManager.DISALLOW_DEBUGGING_FEATURES);
1080 readBoolean(parser, restrictions, UserManager.DISALLOW_CONFIG_VPN);
1081 readBoolean(parser, restrictions, UserManager.DISALLOW_CONFIG_TETHERING);
1082 readBoolean(parser, restrictions, UserManager.DISALLOW_FACTORY_RESET);
1083 readBoolean(parser, restrictions, UserManager.DISALLOW_ADD_USER);
1084 readBoolean(parser, restrictions, UserManager.ENSURE_VERIFY_APPS);
1085 readBoolean(parser, restrictions, UserManager.DISALLOW_CONFIG_CELL_BROADCASTS);
1086 readBoolean(parser, restrictions, UserManager.DISALLOW_CONFIG_MOBILE_NETWORKS);
1087 readBoolean(parser, restrictions, UserManager.DISALLOW_APPS_CONTROL);
1088 readBoolean(parser, restrictions,
1089 UserManager.DISALLOW_MOUNT_PHYSICAL_MEDIA);
1090 readBoolean(parser, restrictions, UserManager.DISALLOW_UNMUTE_MICROPHONE);
1091 readBoolean(parser, restrictions, UserManager.DISALLOW_ADJUST_VOLUME);
Amith Yamasani390989d2014-07-17 10:52:03 -07001092 readBoolean(parser, restrictions, UserManager.DISALLOW_OUTGOING_CALLS);
1093 readBoolean(parser, restrictions, UserManager.DISALLOW_SMS);
Jason Monk1c7c3192014-06-26 12:52:18 -04001094 readBoolean(parser, restrictions, UserManager.DISALLOW_CREATE_WINDOWS);
Amith Yamasani26af8292014-09-09 09:57:27 -07001095 readBoolean(parser, restrictions, UserManager.DISALLOW_CROSS_PROFILE_COPY_PASTE);
1096 readBoolean(parser, restrictions, UserManager.DISALLOW_OUTGOING_BEAM);
Benjamin Franzf3ece362015-02-11 10:51:10 +00001097 readBoolean(parser, restrictions, UserManager.DISALLOW_WALLPAPER);
Benjamin Franzbff46ba2015-03-05 18:33:51 +00001098 readBoolean(parser, restrictions, UserManager.DISALLOW_SAFE_BOOT);
Amith Yamasanie4afaa32014-06-30 14:55:07 +05301099 }
1100
Amith Yamasanie4cf7342012-12-17 11:12:09 -08001101 private void readBoolean(XmlPullParser parser, Bundle restrictions,
1102 String restrictionKey) {
1103 String value = parser.getAttributeValue(null, restrictionKey);
Amith Yamasani71e6c692013-03-24 17:39:28 -07001104 if (value != null) {
1105 restrictions.putBoolean(restrictionKey, Boolean.parseBoolean(value));
1106 }
Amith Yamasanie4cf7342012-12-17 11:12:09 -08001107 }
1108
1109 private void writeBoolean(XmlSerializer xml, Bundle restrictions, String restrictionKey)
1110 throws IOException {
1111 if (restrictions.containsKey(restrictionKey)) {
1112 xml.attribute(null, restrictionKey,
1113 Boolean.toString(restrictions.getBoolean(restrictionKey)));
1114 }
1115 }
1116
Amith Yamasani920ace02012-09-20 22:15:37 -07001117 private int readIntAttribute(XmlPullParser parser, String attr, int defaultValue) {
1118 String valueString = parser.getAttributeValue(null, attr);
1119 if (valueString == null) return defaultValue;
1120 try {
1121 return Integer.parseInt(valueString);
1122 } catch (NumberFormatException nfe) {
1123 return defaultValue;
1124 }
1125 }
1126
1127 private long readLongAttribute(XmlPullParser parser, String attr, long defaultValue) {
1128 String valueString = parser.getAttributeValue(null, attr);
1129 if (valueString == null) return defaultValue;
1130 try {
1131 return Long.parseLong(valueString);
1132 } catch (NumberFormatException nfe) {
1133 return defaultValue;
1134 }
1135 }
1136
Amith Yamasani1a7472e2013-07-02 11:17:30 -07001137 private boolean isPackageInstalled(String pkg, int userId) {
1138 final ApplicationInfo info = mPm.getApplicationInfo(pkg,
1139 PackageManager.GET_UNINSTALLED_PACKAGES,
1140 userId);
1141 if (info == null || (info.flags&ApplicationInfo.FLAG_INSTALLED) == 0) {
1142 return false;
1143 }
1144 return true;
1145 }
1146
Amith Yamasanib82add22013-07-09 11:24:44 -07001147 /**
Kenny Guyd21b2182014-07-17 16:38:55 +01001148 * Removes all the restrictions files (res_<packagename>) for a given user.
Amith Yamasanib82add22013-07-09 11:24:44 -07001149 * Does not do any permissions checking.
1150 */
Kenny Guyd21b2182014-07-17 16:38:55 +01001151 private void cleanAppRestrictions(int userId) {
Amith Yamasanib82add22013-07-09 11:24:44 -07001152 synchronized (mPackagesLock) {
1153 File dir = Environment.getUserSystemDirectory(userId);
1154 String[] files = dir.list();
1155 if (files == null) return;
1156 for (String fileName : files) {
1157 if (fileName.startsWith(RESTRICTIONS_FILE_PREFIX)) {
1158 File resFile = new File(dir, fileName);
1159 if (resFile.exists()) {
Kenny Guyd21b2182014-07-17 16:38:55 +01001160 resFile.delete();
Amith Yamasanib82add22013-07-09 11:24:44 -07001161 }
1162 }
1163 }
1164 }
1165 }
1166
Amith Yamasani1a7472e2013-07-02 11:17:30 -07001167 /**
1168 * Removes the app restrictions file for a specific package and user id, if it exists.
1169 */
1170 private void cleanAppRestrictionsForPackage(String pkg, int userId) {
1171 synchronized (mPackagesLock) {
1172 File dir = Environment.getUserSystemDirectory(userId);
Amith Yamasanifc95e702013-09-26 13:20:17 -07001173 File resFile = new File(dir, packageToRestrictionsFileName(pkg));
Amith Yamasani1a7472e2013-07-02 11:17:30 -07001174 if (resFile.exists()) {
1175 resFile.delete();
1176 }
1177 }
1178 }
1179
Kenny Guya52dc3e2014-02-11 15:33:14 +00001180 @Override
Kenny Guy2a764942014-04-02 13:29:20 +01001181 public UserInfo createProfileForUser(String name, int flags, int userId) {
Kenny Guya52dc3e2014-02-11 15:33:14 +00001182 checkManageUsersPermission("Only the system can create users");
Kenny Guy2a764942014-04-02 13:29:20 +01001183 if (userId != UserHandle.USER_OWNER) {
1184 Slog.w(LOG_TAG, "Only user owner can have profiles");
Kenny Guya52dc3e2014-02-11 15:33:14 +00001185 return null;
1186 }
Kenny Guy2a764942014-04-02 13:29:20 +01001187 return createUserInternal(name, flags, userId);
Kenny Guya52dc3e2014-02-11 15:33:14 +00001188 }
1189
Amith Yamasani258848d2012-08-10 17:06:33 -07001190 @Override
Amith Yamasani13593602012-03-22 16:16:17 -07001191 public UserInfo createUser(String name, int flags) {
Amith Yamasani2a003292012-08-14 18:25:45 -07001192 checkManageUsersPermission("Only the system can create users");
Nicolas Prevotc6d033e2014-02-27 13:11:09 +00001193 return createUserInternal(name, flags, UserHandle.USER_NULL);
Kenny Guya52dc3e2014-02-11 15:33:14 +00001194 }
Amith Yamasanifaea76f2012-09-11 10:59:48 -07001195
Jessica Hummelbe81c802014-04-22 15:49:22 +01001196 private UserInfo createUserInternal(String name, int flags, int parentId) {
Julia Reynolds75175022014-06-26 16:35:00 -04001197 if (getUserRestrictions(UserHandle.getCallingUserId()).getBoolean(
1198 UserManager.DISALLOW_ADD_USER, false)) {
1199 Log.w(LOG_TAG, "Cannot add user. DISALLOW_ADD_USER is enabled.");
1200 return null;
1201 }
Nicolas Prevot12678a92015-05-13 12:15:03 -07001202 if (ActivityManager.isLowRamDeviceStatic()) {
1203 return null;
1204 }
Amith Yamasani95ab7842014-08-11 17:09:26 -07001205 final boolean isGuest = (flags & UserInfo.FLAG_GUEST) != 0;
Nicolas Prevot12678a92015-05-13 12:15:03 -07001206 final boolean isManagedProfile = (flags & UserInfo.FLAG_MANAGED_PROFILE) != 0;
Dianne Hackborn5dc5a002012-09-15 19:33:48 -07001207 final long ident = Binder.clearCallingIdentity();
Nicolas Prevotc6d033e2014-02-27 13:11:09 +00001208 UserInfo userInfo = null;
Dianne Hackborn5dc5a002012-09-15 19:33:48 -07001209 try {
1210 synchronized (mInstallLock) {
1211 synchronized (mPackagesLock) {
Jessica Hummelbe81c802014-04-22 15:49:22 +01001212 UserInfo parent = null;
1213 if (parentId != UserHandle.USER_NULL) {
1214 parent = getUserInfoLocked(parentId);
1215 if (parent == null) return null;
Nicolas Prevotc6d033e2014-02-27 13:11:09 +00001216 }
Nicolas Prevot12678a92015-05-13 12:15:03 -07001217 if (isManagedProfile && !canAddMoreManagedProfiles()) {
1218 return null;
1219 }
1220 if (!isGuest && !isManagedProfile && isUserLimitReachedLocked()) {
1221 // If we're not adding a guest user or a managed profile and the limit has
1222 // been reached, cannot add a user.
Amith Yamasani95ab7842014-08-11 17:09:26 -07001223 return null;
1224 }
1225 // If we're adding a guest and there already exists one, bail.
Adam Lesinskieddeb492014-09-08 17:50:03 -07001226 if (isGuest && findCurrentGuestUserLocked() != null) {
Amith Yamasani95ab7842014-08-11 17:09:26 -07001227 return null;
1228 }
Dianne Hackborn5dc5a002012-09-15 19:33:48 -07001229 int userId = getNextAvailableIdLocked();
1230 userInfo = new UserInfo(userId, name, null, flags);
1231 File userPath = new File(mBaseUserPath, Integer.toString(userId));
1232 userInfo.serialNumber = mNextSerialNumber++;
Amith Yamasani920ace02012-09-20 22:15:37 -07001233 long now = System.currentTimeMillis();
1234 userInfo.creationTime = (now > EPOCH_PLUS_30_YEARS) ? now : 0;
Dianne Hackbornd4ac8d72012-09-27 23:20:10 -07001235 userInfo.partial = true;
Amith Yamasani16389312012-10-17 21:20:14 -07001236 Environment.getUserSystemDirectory(userInfo.id).mkdirs();
Dianne Hackborn5dc5a002012-09-15 19:33:48 -07001237 mUsers.put(userId, userInfo);
1238 writeUserListLocked();
Jessica Hummelbe81c802014-04-22 15:49:22 +01001239 if (parent != null) {
1240 if (parent.profileGroupId == UserInfo.NO_PROFILE_GROUP_ID) {
1241 parent.profileGroupId = parent.id;
Fyodor Kupolov75d0ea82014-12-15 16:21:07 -08001242 scheduleWriteUserLocked(parent);
Kenny Guya52dc3e2014-02-11 15:33:14 +00001243 }
Jessica Hummelbe81c802014-04-22 15:49:22 +01001244 userInfo.profileGroupId = parent.profileGroupId;
Kenny Guya52dc3e2014-02-11 15:33:14 +00001245 }
Dianne Hackborn5dc5a002012-09-15 19:33:48 -07001246 mPm.createNewUserLILPw(userId, userPath);
Dianne Hackbornd4ac8d72012-09-27 23:20:10 -07001247 userInfo.partial = false;
Fyodor Kupolov75d0ea82014-12-15 16:21:07 -08001248 scheduleWriteUserLocked(userInfo);
Dianne Hackbornd4ac8d72012-09-27 23:20:10 -07001249 updateUserIdsLocked();
Geoffrey Borggaarde45e45e32013-01-24 10:03:20 -05001250 Bundle restrictions = new Bundle();
Geoffrey Borggaarde45e45e32013-01-24 10:03:20 -05001251 mUserRestrictions.append(userId, restrictions);
Svet Ganov78027f32015-03-25 09:10:09 -07001252 mPm.newUserCreatedLILPw(userId);
Dianne Hackborn5dc5a002012-09-15 19:33:48 -07001253 }
Dianne Hackborn4428e172012-08-24 17:43:05 -07001254 }
Dianne Hackborn5dc5a002012-09-15 19:33:48 -07001255 if (userInfo != null) {
1256 Intent addedIntent = new Intent(Intent.ACTION_USER_ADDED);
1257 addedIntent.putExtra(Intent.EXTRA_USER_HANDLE, userInfo.id);
1258 mContext.sendBroadcastAsUser(addedIntent, UserHandle.ALL,
1259 android.Manifest.permission.MANAGE_USERS);
1260 }
1261 } finally {
1262 Binder.restoreCallingIdentity(ident);
Amith Yamasani258848d2012-08-10 17:06:33 -07001263 }
Amith Yamasani4b2e9342011-03-31 12:38:53 -07001264 return userInfo;
1265 }
1266
Amith Yamasani95ab7842014-08-11 17:09:26 -07001267 private int numberOfUsersOfTypeLocked(int flags, boolean excludeDying) {
1268 int count = 0;
1269 for (int i = mUsers.size() - 1; i >= 0; i--) {
1270 UserInfo user = mUsers.valueAt(i);
1271 if (!excludeDying || !mRemovingUserIds.get(user.id)) {
1272 if ((user.flags & flags) != 0) {
1273 count++;
1274 }
1275 }
1276 }
1277 return count;
1278 }
1279
Amith Yamasani0b285492011-04-14 17:35:23 -07001280 /**
Adam Lesinskieddeb492014-09-08 17:50:03 -07001281 * Find the current guest user. If the Guest user is partial,
1282 * then do not include it in the results as it is about to die.
1283 * This is different than {@link #numberOfUsersOfTypeLocked(int, boolean)} due to
1284 * the special handling of Guests being removed.
1285 */
1286 private UserInfo findCurrentGuestUserLocked() {
1287 final int size = mUsers.size();
1288 for (int i = 0; i < size; i++) {
1289 final UserInfo user = mUsers.valueAt(i);
1290 if (user.isGuest() && !user.guestToRemove && !mRemovingUserIds.get(user.id)) {
1291 return user;
1292 }
1293 }
1294 return null;
1295 }
1296
1297 /**
Amith Yamasani1df14732014-08-29 21:37:27 -07001298 * Mark this guest user for deletion to allow us to create another guest
1299 * and switch to that user before actually removing this guest.
1300 * @param userHandle the userid of the current guest
1301 * @return whether the user could be marked for deletion
1302 */
1303 public boolean markGuestForDeletion(int userHandle) {
1304 checkManageUsersPermission("Only the system can remove users");
1305 if (getUserRestrictions(UserHandle.getCallingUserId()).getBoolean(
1306 UserManager.DISALLOW_REMOVE_USER, false)) {
1307 Log.w(LOG_TAG, "Cannot remove user. DISALLOW_REMOVE_USER is enabled.");
1308 return false;
1309 }
1310
1311 long ident = Binder.clearCallingIdentity();
1312 try {
1313 final UserInfo user;
1314 synchronized (mPackagesLock) {
1315 user = mUsers.get(userHandle);
1316 if (userHandle == 0 || user == null || mRemovingUserIds.get(userHandle)) {
1317 return false;
1318 }
1319 if (!user.isGuest()) {
1320 return false;
1321 }
Adam Lesinskieddeb492014-09-08 17:50:03 -07001322 // We set this to a guest user that is to be removed. This is a temporary state
1323 // where we are allowed to add new Guest users, even if this one is still not
1324 // removed. This user will still show up in getUserInfo() calls.
1325 // If we don't get around to removing this Guest user, it will be purged on next
1326 // startup.
1327 user.guestToRemove = true;
Amith Yamasani1df14732014-08-29 21:37:27 -07001328 // Mark it as disabled, so that it isn't returned any more when
1329 // profiles are queried.
1330 user.flags |= UserInfo.FLAG_DISABLED;
Amith Yamasani1df14732014-08-29 21:37:27 -07001331 writeUserLocked(user);
1332 }
1333 } finally {
1334 Binder.restoreCallingIdentity(ident);
1335 }
1336 return true;
1337 }
1338
1339 /**
Amith Yamasani0b285492011-04-14 17:35:23 -07001340 * Removes a user and all data directories created for that user. This method should be called
1341 * after the user's processes have been terminated.
Dianne Hackborn10ad9822014-03-17 11:28:36 -07001342 * @param userHandle the user's id
Amith Yamasani0b285492011-04-14 17:35:23 -07001343 */
Amith Yamasani258848d2012-08-10 17:06:33 -07001344 public boolean removeUser(int userHandle) {
Amith Yamasani2a003292012-08-14 18:25:45 -07001345 checkManageUsersPermission("Only the system can remove users");
Julia Reynolds4ac5f852014-06-23 17:38:51 -04001346 if (getUserRestrictions(UserHandle.getCallingUserId()).getBoolean(
1347 UserManager.DISALLOW_REMOVE_USER, false)) {
1348 Log.w(LOG_TAG, "Cannot remove user. DISALLOW_REMOVE_USER is enabled.");
1349 return false;
1350 }
1351
Kenny Guyee58b4f2014-05-23 15:19:53 +01001352 long ident = Binder.clearCallingIdentity();
1353 try {
1354 final UserInfo user;
1355 synchronized (mPackagesLock) {
1356 user = mUsers.get(userHandle);
Kenny Guy17c9d692014-06-13 13:51:42 +01001357 if (userHandle == 0 || user == null || mRemovingUserIds.get(userHandle)) {
Kenny Guyee58b4f2014-05-23 15:19:53 +01001358 return false;
1359 }
Jeff Sharkey6eb09392014-11-14 15:57:59 -08001360
1361 // We remember deleted user IDs to prevent them from being
1362 // reused during the current boot; they can still be reused
1363 // after a reboot.
Kenny Guyee58b4f2014-05-23 15:19:53 +01001364 mRemovingUserIds.put(userHandle, true);
Jeff Sharkey6eb09392014-11-14 15:57:59 -08001365
Kenny Guyee58b4f2014-05-23 15:19:53 +01001366 try {
1367 mAppOpsService.removeUser(userHandle);
1368 } catch (RemoteException e) {
1369 Log.w(LOG_TAG, "Unable to notify AppOpsService of removing user", e);
1370 }
1371 // Set this to a partially created user, so that the user will be purged
1372 // on next startup, in case the runtime stops now before stopping and
1373 // removing the user completely.
1374 user.partial = true;
1375 // Mark it as disabled, so that it isn't returned any more when
1376 // profiles are queried.
1377 user.flags |= UserInfo.FLAG_DISABLED;
1378 writeUserLocked(user);
1379 }
1380
1381 if (user.profileGroupId != UserInfo.NO_PROFILE_GROUP_ID
1382 && user.isManagedProfile()) {
1383 // Send broadcast to notify system that the user removed was a
1384 // managed user.
1385 sendProfileRemovedBroadcast(user.profileGroupId, user.id);
1386 }
1387
1388 if (DBG) Slog.i(LOG_TAG, "Stopping user " + userHandle);
1389 int res;
1390 try {
1391 res = ActivityManagerNative.getDefault().stopUser(userHandle,
1392 new IStopUserCallback.Stub() {
1393 @Override
1394 public void userStopped(int userId) {
1395 finishRemoveUser(userId);
1396 }
1397 @Override
1398 public void userStopAborted(int userId) {
1399 }
1400 });
1401 } catch (RemoteException e) {
Dianne Hackborn80a4af22012-08-27 19:18:31 -07001402 return false;
1403 }
Kenny Guyee58b4f2014-05-23 15:19:53 +01001404 return res == ActivityManager.USER_OP_SUCCESS;
1405 } finally {
1406 Binder.restoreCallingIdentity(ident);
Dianne Hackborn80a4af22012-08-27 19:18:31 -07001407 }
Dianne Hackborn80a4af22012-08-27 19:18:31 -07001408 }
1409
Amith Yamasanidb6a14c2012-10-17 21:16:52 -07001410 void finishRemoveUser(final int userHandle) {
Amith Yamasani16389312012-10-17 21:20:14 -07001411 if (DBG) Slog.i(LOG_TAG, "finishRemoveUser " + userHandle);
Amith Yamasanidb6a14c2012-10-17 21:16:52 -07001412 // Let other services shutdown any activity and clean up their state before completely
1413 // wiping the user's system directory and removing from the user list
Dianne Hackborn5dc5a002012-09-15 19:33:48 -07001414 long ident = Binder.clearCallingIdentity();
1415 try {
1416 Intent addedIntent = new Intent(Intent.ACTION_USER_REMOVED);
1417 addedIntent.putExtra(Intent.EXTRA_USER_HANDLE, userHandle);
Amith Yamasanidb6a14c2012-10-17 21:16:52 -07001418 mContext.sendOrderedBroadcastAsUser(addedIntent, UserHandle.ALL,
1419 android.Manifest.permission.MANAGE_USERS,
1420
1421 new BroadcastReceiver() {
1422 @Override
1423 public void onReceive(Context context, Intent intent) {
1424 if (DBG) {
1425 Slog.i(LOG_TAG,
1426 "USER_REMOVED broadcast sent, cleaning up user data "
1427 + userHandle);
1428 }
1429 new Thread() {
1430 public void run() {
1431 synchronized (mInstallLock) {
1432 synchronized (mPackagesLock) {
1433 removeUserStateLocked(userHandle);
1434 }
1435 }
1436 }
1437 }.start();
1438 }
1439 },
1440
1441 null, Activity.RESULT_OK, null, null);
Dianne Hackborn5dc5a002012-09-15 19:33:48 -07001442 } finally {
1443 Binder.restoreCallingIdentity(ident);
1444 }
Amith Yamasani2a003292012-08-14 18:25:45 -07001445 }
1446
Jeff Sharkeyffe0cb42012-11-05 17:24:43 -08001447 private void removeUserStateLocked(final int userHandle) {
Paul Crowley85e4e812015-05-19 12:42:00 +01001448 mContext.getSystemService(StorageManager.class)
1449 .deleteUserKey(userHandle);
Dianne Hackbornd4ac8d72012-09-27 23:20:10 -07001450 // Cleanup package manager settings
Amith Yamasanidda003f2014-08-28 18:06:51 -07001451 mPm.cleanUpUserLILPw(this, userHandle);
Dianne Hackbornd4ac8d72012-09-27 23:20:10 -07001452
1453 // Remove this user from the list
1454 mUsers.remove(userHandle);
Dianne Hackbornd4ac8d72012-09-27 23:20:10 -07001455 // Remove user file
Amith Yamasanifc95e702013-09-26 13:20:17 -07001456 AtomicFile userFile = new AtomicFile(new File(mUsersDir, userHandle + XML_SUFFIX));
Dianne Hackbornd4ac8d72012-09-27 23:20:10 -07001457 userFile.delete();
1458 // Update the user list
1459 writeUserListLocked();
1460 updateUserIdsLocked();
1461 removeDirectoryRecursive(Environment.getUserSystemDirectory(userHandle));
1462 }
1463
Amith Yamasani61f57372012-08-31 12:12:28 -07001464 private void removeDirectoryRecursive(File parent) {
1465 if (parent.isDirectory()) {
1466 String[] files = parent.list();
1467 for (String filename : files) {
1468 File child = new File(parent, filename);
1469 removeDirectoryRecursive(child);
1470 }
1471 }
1472 parent.delete();
1473 }
1474
Kenny Guyf8d3a232014-05-15 16:09:52 +01001475 private void sendProfileRemovedBroadcast(int parentUserId, int removedUserId) {
Adam Connors7b66ed52014-04-14 11:58:10 +01001476 Intent managedProfileIntent = new Intent(Intent.ACTION_MANAGED_PROFILE_REMOVED);
Adam Connorsd4b584e2014-06-09 13:55:47 +01001477 managedProfileIntent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY |
1478 Intent.FLAG_RECEIVER_FOREGROUND);
Kenny Guyf8d3a232014-05-15 16:09:52 +01001479 managedProfileIntent.putExtra(Intent.EXTRA_USER, new UserHandle(removedUserId));
1480 mContext.sendBroadcastAsUser(managedProfileIntent, new UserHandle(parentUserId), null);
Adam Connors7b66ed52014-04-14 11:58:10 +01001481 }
1482
Amith Yamasani2a003292012-08-14 18:25:45 -07001483 @Override
Amith Yamasani7e99bc02013-04-16 18:24:51 -07001484 public Bundle getApplicationRestrictions(String packageName) {
1485 return getApplicationRestrictionsForUser(packageName, UserHandle.getCallingUserId());
1486 }
1487
1488 @Override
1489 public Bundle getApplicationRestrictionsForUser(String packageName, int userId) {
Amith Yamasanidf2e92a2013-03-01 17:04:38 -08001490 if (UserHandle.getCallingUserId() != userId
Amith Yamasani9429afb2013-04-10 18:40:51 -07001491 || !UserHandle.isSameApp(Binder.getCallingUid(), getUidForPackage(packageName))) {
Amith Yamasanibe465322014-04-24 13:45:17 -07001492 checkManageUsersPermission("Only system can get restrictions for other users/apps");
Amith Yamasanidf2e92a2013-03-01 17:04:38 -08001493 }
1494 synchronized (mPackagesLock) {
1495 // Read the restrictions from XML
1496 return readApplicationRestrictionsLocked(packageName, userId);
1497 }
1498 }
1499
1500 @Override
Amith Yamasani7e99bc02013-04-16 18:24:51 -07001501 public void setApplicationRestrictions(String packageName, Bundle restrictions,
Amith Yamasanidf2e92a2013-03-01 17:04:38 -08001502 int userId) {
1503 if (UserHandle.getCallingUserId() != userId
Amith Yamasani9429afb2013-04-10 18:40:51 -07001504 || !UserHandle.isSameApp(Binder.getCallingUid(), getUidForPackage(packageName))) {
Amith Yamasanibe465322014-04-24 13:45:17 -07001505 checkManageUsersPermission("Only system can set restrictions for other users/apps");
Amith Yamasanidf2e92a2013-03-01 17:04:38 -08001506 }
1507 synchronized (mPackagesLock) {
Kenny Guyd21b2182014-07-17 16:38:55 +01001508 if (restrictions == null || restrictions.isEmpty()) {
1509 cleanAppRestrictionsForPackage(packageName, userId);
1510 } else {
1511 // Write the restrictions to XML
1512 writeApplicationRestrictionsLocked(packageName, restrictions, userId);
1513 }
Amith Yamasanidf2e92a2013-03-01 17:04:38 -08001514 }
Robin Lee66e5d962014-04-09 16:44:21 +01001515
Kenny Guyd21b2182014-07-17 16:38:55 +01001516 if (isPackageInstalled(packageName, userId)) {
1517 // Notify package of changes via an intent - only sent to explicitly registered receivers.
1518 Intent changeIntent = new Intent(Intent.ACTION_APPLICATION_RESTRICTIONS_CHANGED);
1519 changeIntent.setPackage(packageName);
1520 changeIntent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY);
1521 mContext.sendBroadcastAsUser(changeIntent, new UserHandle(userId));
1522 }
Amith Yamasanidf2e92a2013-03-01 17:04:38 -08001523 }
1524
Amith Yamasani655d0e22013-06-12 14:19:10 -07001525 @Override
Amith Yamasani1a7472e2013-07-02 11:17:30 -07001526 public void removeRestrictions() {
Amith Yamasanibe465322014-04-24 13:45:17 -07001527 checkManageUsersPermission("Only system can remove restrictions");
Amith Yamasani1a7472e2013-07-02 11:17:30 -07001528 final int userHandle = UserHandle.getCallingUserId();
Amith Yamasani5e486f52013-08-07 11:06:44 -07001529 removeRestrictionsForUser(userHandle, true);
Amith Yamasani350962c2013-08-06 11:18:53 -07001530 }
1531
Amith Yamasanie5bcff62014-07-19 15:44:09 -07001532 private void removeRestrictionsForUser(final int userHandle, boolean unhideApps) {
Amith Yamasani1a7472e2013-07-02 11:17:30 -07001533 synchronized (mPackagesLock) {
1534 // Remove all user restrictions
1535 setUserRestrictions(new Bundle(), userHandle);
Amith Yamasani1a7472e2013-07-02 11:17:30 -07001536 // Remove any app restrictions
Kenny Guyd21b2182014-07-17 16:38:55 +01001537 cleanAppRestrictions(userHandle);
Amith Yamasani1a7472e2013-07-02 11:17:30 -07001538 }
Amith Yamasanie5bcff62014-07-19 15:44:09 -07001539 if (unhideApps) {
1540 unhideAllInstalledAppsForUser(userHandle);
Amith Yamasani5e486f52013-08-07 11:06:44 -07001541 }
1542 }
1543
Amith Yamasanie5bcff62014-07-19 15:44:09 -07001544 private void unhideAllInstalledAppsForUser(final int userHandle) {
Amith Yamasani1a7472e2013-07-02 11:17:30 -07001545 mHandler.post(new Runnable() {
1546 @Override
1547 public void run() {
1548 List<ApplicationInfo> apps =
1549 mPm.getInstalledApplications(PackageManager.GET_UNINSTALLED_PACKAGES,
1550 userHandle).getList();
1551 final long ident = Binder.clearCallingIdentity();
1552 try {
1553 for (ApplicationInfo appInfo : apps) {
1554 if ((appInfo.flags & ApplicationInfo.FLAG_INSTALLED) != 0
Alex Klyubinb9f8a522015-02-03 11:12:59 -08001555 && (appInfo.privateFlags & ApplicationInfo.PRIVATE_FLAG_HIDDEN)
1556 != 0) {
Amith Yamasanie5bcff62014-07-19 15:44:09 -07001557 mPm.setApplicationHiddenSettingAsUser(appInfo.packageName, false,
Amith Yamasani1a7472e2013-07-02 11:17:30 -07001558 userHandle);
1559 }
1560 }
1561 } finally {
1562 Binder.restoreCallingIdentity(ident);
1563 }
1564 }
1565 });
1566 }
Amith Yamasanidf2e92a2013-03-01 17:04:38 -08001567 private int getUidForPackage(String packageName) {
Amith Yamasani9429afb2013-04-10 18:40:51 -07001568 long ident = Binder.clearCallingIdentity();
Amith Yamasanidf2e92a2013-03-01 17:04:38 -08001569 try {
1570 return mContext.getPackageManager().getApplicationInfo(packageName,
1571 PackageManager.GET_UNINSTALLED_PACKAGES).uid;
1572 } catch (NameNotFoundException nnfe) {
1573 return -1;
Amith Yamasani9429afb2013-04-10 18:40:51 -07001574 } finally {
1575 Binder.restoreCallingIdentity(ident);
Amith Yamasanidf2e92a2013-03-01 17:04:38 -08001576 }
1577 }
1578
Amith Yamasani7e99bc02013-04-16 18:24:51 -07001579 private Bundle readApplicationRestrictionsLocked(String packageName,
Amith Yamasanidf2e92a2013-03-01 17:04:38 -08001580 int userId) {
Fyodor Kupolov262f9952015-03-23 18:55:11 -07001581 AtomicFile restrictionsFile =
1582 new AtomicFile(new File(Environment.getUserSystemDirectory(userId),
1583 packageToRestrictionsFileName(packageName)));
1584 return readApplicationRestrictionsLocked(restrictionsFile);
1585 }
1586
1587 @VisibleForTesting
1588 static Bundle readApplicationRestrictionsLocked(AtomicFile restrictionsFile) {
Amith Yamasani7e99bc02013-04-16 18:24:51 -07001589 final Bundle restrictions = new Bundle();
Fyodor Kupolov262f9952015-03-23 18:55:11 -07001590 final ArrayList<String> values = new ArrayList<>();
Fyodor Kupolov6f34d362015-04-02 12:42:13 -07001591 if (!restrictionsFile.getBaseFile().exists()) {
Fyodor Kupolovf6ee2242015-04-06 10:15:07 -07001592 return restrictions;
Fyodor Kupolov6f34d362015-04-02 12:42:13 -07001593 }
Amith Yamasanidf2e92a2013-03-01 17:04:38 -08001594
1595 FileInputStream fis = null;
1596 try {
Amith Yamasanidf2e92a2013-03-01 17:04:38 -08001597 fis = restrictionsFile.openRead();
1598 XmlPullParser parser = Xml.newPullParser();
Wojciech Staszkiewicz9e9e2e72015-05-08 14:58:46 +01001599 parser.setInput(fis, StandardCharsets.UTF_8.name());
Fyodor Kupolov262f9952015-03-23 18:55:11 -07001600 XmlUtils.nextElement(parser);
1601 if (parser.getEventType() != XmlPullParser.START_TAG) {
Amith Yamasanidf2e92a2013-03-01 17:04:38 -08001602 Slog.e(LOG_TAG, "Unable to read restrictions file "
1603 + restrictionsFile.getBaseFile());
Amith Yamasani7e99bc02013-04-16 18:24:51 -07001604 return restrictions;
Amith Yamasanidf2e92a2013-03-01 17:04:38 -08001605 }
Fyodor Kupolov262f9952015-03-23 18:55:11 -07001606 while (parser.next() != XmlPullParser.END_DOCUMENT) {
1607 readEntry(restrictions, values, parser);
Amith Yamasanidf2e92a2013-03-01 17:04:38 -08001608 }
Fyodor Kupolov262f9952015-03-23 18:55:11 -07001609 } catch (IOException|XmlPullParserException e) {
1610 Log.w(LOG_TAG, "Error parsing " + restrictionsFile.getBaseFile(), e);
Amith Yamasanidf2e92a2013-03-01 17:04:38 -08001611 } finally {
Fyodor Kupolov262f9952015-03-23 18:55:11 -07001612 IoUtils.closeQuietly(fis);
Amith Yamasanidf2e92a2013-03-01 17:04:38 -08001613 }
Amith Yamasani7e99bc02013-04-16 18:24:51 -07001614 return restrictions;
Amith Yamasanidf2e92a2013-03-01 17:04:38 -08001615 }
1616
Fyodor Kupolov262f9952015-03-23 18:55:11 -07001617 private static void readEntry(Bundle restrictions, ArrayList<String> values,
1618 XmlPullParser parser) throws XmlPullParserException, IOException {
1619 int type = parser.getEventType();
1620 if (type == XmlPullParser.START_TAG && parser.getName().equals(TAG_ENTRY)) {
1621 String key = parser.getAttributeValue(null, ATTR_KEY);
1622 String valType = parser.getAttributeValue(null, ATTR_VALUE_TYPE);
1623 String multiple = parser.getAttributeValue(null, ATTR_MULTIPLE);
1624 if (multiple != null) {
1625 values.clear();
1626 int count = Integer.parseInt(multiple);
1627 while (count > 0 && (type = parser.next()) != XmlPullParser.END_DOCUMENT) {
1628 if (type == XmlPullParser.START_TAG
1629 && parser.getName().equals(TAG_VALUE)) {
1630 values.add(parser.nextText().trim());
1631 count--;
1632 }
1633 }
1634 String [] valueStrings = new String[values.size()];
1635 values.toArray(valueStrings);
1636 restrictions.putStringArray(key, valueStrings);
1637 } else if (ATTR_TYPE_BUNDLE.equals(valType)) {
1638 restrictions.putBundle(key, readBundleEntry(parser, values));
1639 } else if (ATTR_TYPE_BUNDLE_ARRAY.equals(valType)) {
1640 final int outerDepth = parser.getDepth();
1641 ArrayList<Bundle> bundleList = new ArrayList<>();
1642 while (XmlUtils.nextElementWithin(parser, outerDepth)) {
1643 Bundle childBundle = readBundleEntry(parser, values);
1644 bundleList.add(childBundle);
1645 }
1646 restrictions.putParcelableArray(key,
1647 bundleList.toArray(new Bundle[bundleList.size()]));
1648 } else {
1649 String value = parser.nextText().trim();
1650 if (ATTR_TYPE_BOOLEAN.equals(valType)) {
1651 restrictions.putBoolean(key, Boolean.parseBoolean(value));
1652 } else if (ATTR_TYPE_INTEGER.equals(valType)) {
1653 restrictions.putInt(key, Integer.parseInt(value));
1654 } else {
1655 restrictions.putString(key, value);
1656 }
1657 }
1658 }
1659 }
1660
1661 private static Bundle readBundleEntry(XmlPullParser parser, ArrayList<String> values)
1662 throws IOException, XmlPullParserException {
1663 Bundle childBundle = new Bundle();
1664 final int outerDepth = parser.getDepth();
1665 while (XmlUtils.nextElementWithin(parser, outerDepth)) {
1666 readEntry(childBundle, values, parser);
1667 }
1668 return childBundle;
1669 }
1670
Amith Yamasanidf2e92a2013-03-01 17:04:38 -08001671 private void writeApplicationRestrictionsLocked(String packageName,
Amith Yamasani7e99bc02013-04-16 18:24:51 -07001672 Bundle restrictions, int userId) {
Amith Yamasanidf2e92a2013-03-01 17:04:38 -08001673 AtomicFile restrictionsFile = new AtomicFile(
1674 new File(Environment.getUserSystemDirectory(userId),
Amith Yamasanifc95e702013-09-26 13:20:17 -07001675 packageToRestrictionsFileName(packageName)));
Fyodor Kupolov262f9952015-03-23 18:55:11 -07001676 writeApplicationRestrictionsLocked(restrictions, restrictionsFile);
1677 }
1678
1679 @VisibleForTesting
1680 static void writeApplicationRestrictionsLocked(Bundle restrictions,
1681 AtomicFile restrictionsFile) {
1682 FileOutputStream fos = null;
Amith Yamasanidf2e92a2013-03-01 17:04:38 -08001683 try {
1684 fos = restrictionsFile.startWrite();
1685 final BufferedOutputStream bos = new BufferedOutputStream(fos);
1686
Amith Yamasanidf2e92a2013-03-01 17:04:38 -08001687 final XmlSerializer serializer = new FastXmlSerializer();
Wojciech Staszkiewicz9e9e2e72015-05-08 14:58:46 +01001688 serializer.setOutput(bos, StandardCharsets.UTF_8.name());
Amith Yamasanidf2e92a2013-03-01 17:04:38 -08001689 serializer.startDocument(null, true);
1690 serializer.setFeature("http://xmlpull.org/v1/doc/features.html#indent-output", true);
1691
1692 serializer.startTag(null, TAG_RESTRICTIONS);
Fyodor Kupolov262f9952015-03-23 18:55:11 -07001693 writeBundle(restrictions, serializer);
Amith Yamasanidf2e92a2013-03-01 17:04:38 -08001694 serializer.endTag(null, TAG_RESTRICTIONS);
1695
1696 serializer.endDocument();
1697 restrictionsFile.finishWrite(fos);
1698 } catch (Exception e) {
1699 restrictionsFile.failWrite(fos);
Fyodor Kupolov262f9952015-03-23 18:55:11 -07001700 Slog.e(LOG_TAG, "Error writing application restrictions list", e);
1701 }
1702 }
1703
1704 private static void writeBundle(Bundle restrictions, XmlSerializer serializer)
1705 throws IOException {
1706 for (String key : restrictions.keySet()) {
1707 Object value = restrictions.get(key);
1708 serializer.startTag(null, TAG_ENTRY);
1709 serializer.attribute(null, ATTR_KEY, key);
1710
1711 if (value instanceof Boolean) {
1712 serializer.attribute(null, ATTR_VALUE_TYPE, ATTR_TYPE_BOOLEAN);
1713 serializer.text(value.toString());
1714 } else if (value instanceof Integer) {
1715 serializer.attribute(null, ATTR_VALUE_TYPE, ATTR_TYPE_INTEGER);
1716 serializer.text(value.toString());
1717 } else if (value == null || value instanceof String) {
1718 serializer.attribute(null, ATTR_VALUE_TYPE, ATTR_TYPE_STRING);
1719 serializer.text(value != null ? (String) value : "");
1720 } else if (value instanceof Bundle) {
1721 serializer.attribute(null, ATTR_VALUE_TYPE, ATTR_TYPE_BUNDLE);
1722 writeBundle((Bundle) value, serializer);
1723 } else if (value instanceof Parcelable[]) {
1724 serializer.attribute(null, ATTR_VALUE_TYPE, ATTR_TYPE_BUNDLE_ARRAY);
1725 Parcelable[] array = (Parcelable[]) value;
1726 for (Parcelable parcelable : array) {
1727 if (!(parcelable instanceof Bundle)) {
1728 throw new IllegalArgumentException("bundle-array can only hold Bundles");
1729 }
1730 serializer.startTag(null, TAG_ENTRY);
1731 serializer.attribute(null, ATTR_VALUE_TYPE, ATTR_TYPE_BUNDLE);
1732 writeBundle((Bundle) parcelable, serializer);
1733 serializer.endTag(null, TAG_ENTRY);
1734 }
1735 } else {
1736 serializer.attribute(null, ATTR_VALUE_TYPE, ATTR_TYPE_STRING_ARRAY);
1737 String[] values = (String[]) value;
1738 serializer.attribute(null, ATTR_MULTIPLE, Integer.toString(values.length));
1739 for (String choice : values) {
1740 serializer.startTag(null, TAG_VALUE);
1741 serializer.text(choice != null ? choice : "");
1742 serializer.endTag(null, TAG_VALUE);
1743 }
1744 }
1745 serializer.endTag(null, TAG_ENTRY);
Amith Yamasanidf2e92a2013-03-01 17:04:38 -08001746 }
1747 }
1748
1749 @Override
Amith Yamasani2a003292012-08-14 18:25:45 -07001750 public int getUserSerialNumber(int userHandle) {
Dianne Hackborn4428e172012-08-24 17:43:05 -07001751 synchronized (mPackagesLock) {
Amith Yamasani2a003292012-08-14 18:25:45 -07001752 if (!exists(userHandle)) return -1;
Amith Yamasani195263742012-08-21 15:40:12 -07001753 return getUserInfoLocked(userHandle).serialNumber;
Amith Yamasani2a003292012-08-14 18:25:45 -07001754 }
1755 }
1756
1757 @Override
1758 public int getUserHandle(int userSerialNumber) {
Dianne Hackborn4428e172012-08-24 17:43:05 -07001759 synchronized (mPackagesLock) {
Amith Yamasani2a003292012-08-14 18:25:45 -07001760 for (int userId : mUserIds) {
Kenny Guy945f8832015-02-10 15:17:26 +00001761 UserInfo info = getUserInfoLocked(userId);
1762 if (info != null && info.serialNumber == userSerialNumber) return userId;
Amith Yamasani2a003292012-08-14 18:25:45 -07001763 }
1764 // Not found
1765 return -1;
Amith Yamasani13593602012-03-22 16:16:17 -07001766 }
1767 }
1768
Fyodor Kupolovff7233e2015-04-08 11:28:52 -07001769 @Override
1770 public long getUserCreationTime(int userHandle) {
1771 int callingUserId = UserHandle.getCallingUserId();
1772 UserInfo userInfo = null;
1773 synchronized (mPackagesLock) {
1774 if (callingUserId == userHandle) {
1775 userInfo = getUserInfoLocked(userHandle);
1776 } else {
1777 UserInfo parent = getProfileParentLocked(userHandle);
1778 if (parent != null && parent.id == callingUserId) {
1779 userInfo = getUserInfoLocked(userHandle);
1780 }
1781 }
1782 }
1783 if (userInfo == null) {
1784 throw new SecurityException("userHandle can only be the calling user or a managed "
1785 + "profile associated with this user");
1786 }
1787 return userInfo.creationTime;
1788 }
1789
Amith Yamasani0b285492011-04-14 17:35:23 -07001790 /**
1791 * Caches the list of user ids in an array, adjusting the array size when necessary.
1792 */
Amith Yamasani13593602012-03-22 16:16:17 -07001793 private void updateUserIdsLocked() {
Dianne Hackbornd4ac8d72012-09-27 23:20:10 -07001794 int num = 0;
Amith Yamasani0b285492011-04-14 17:35:23 -07001795 for (int i = 0; i < mUsers.size(); i++) {
Dianne Hackbornd4ac8d72012-09-27 23:20:10 -07001796 if (!mUsers.valueAt(i).partial) {
1797 num++;
1798 }
1799 }
Amith Yamasani16389312012-10-17 21:20:14 -07001800 final int[] newUsers = new int[num];
1801 int n = 0;
Dianne Hackbornd4ac8d72012-09-27 23:20:10 -07001802 for (int i = 0; i < mUsers.size(); i++) {
1803 if (!mUsers.valueAt(i).partial) {
Amith Yamasani16389312012-10-17 21:20:14 -07001804 newUsers[n++] = mUsers.keyAt(i);
Dianne Hackbornd4ac8d72012-09-27 23:20:10 -07001805 }
Amith Yamasani0b285492011-04-14 17:35:23 -07001806 }
Dianne Hackborn7767eac2012-08-23 18:25:40 -07001807 mUserIds = newUsers;
Amith Yamasani0b285492011-04-14 17:35:23 -07001808 }
1809
1810 /**
Amith Yamasani1a7472e2013-07-02 11:17:30 -07001811 * Make a note of the last started time of a user and do some cleanup.
Amith Yamasani920ace02012-09-20 22:15:37 -07001812 * @param userId the user that was just foregrounded
1813 */
Amith Yamasani06bf8242015-05-08 16:36:21 -07001814 public void onUserForeground(int userId) {
Amith Yamasani920ace02012-09-20 22:15:37 -07001815 synchronized (mPackagesLock) {
1816 UserInfo user = mUsers.get(userId);
1817 long now = System.currentTimeMillis();
Dianne Hackbornd4ac8d72012-09-27 23:20:10 -07001818 if (user == null || user.partial) {
1819 Slog.w(LOG_TAG, "userForeground: unknown user #" + userId);
1820 return;
1821 }
1822 if (now > EPOCH_PLUS_30_YEARS) {
Amith Yamasani920ace02012-09-20 22:15:37 -07001823 user.lastLoggedInTime = now;
Fyodor Kupolov75d0ea82014-12-15 16:21:07 -08001824 scheduleWriteUserLocked(user);
Amith Yamasani920ace02012-09-20 22:15:37 -07001825 }
1826 }
1827 }
1828
1829 /**
Amith Yamasani0b285492011-04-14 17:35:23 -07001830 * Returns the next available user id, filling in any holes in the ids.
Amith Yamasani742a6712011-05-04 14:49:28 -07001831 * TODO: May not be a good idea to recycle ids, in case it results in confusion
1832 * for data and battery stats collection, or unexpected cross-talk.
Amith Yamasani0b285492011-04-14 17:35:23 -07001833 * @return
1834 */
Dianne Hackborn5dc5a002012-09-15 19:33:48 -07001835 private int getNextAvailableIdLocked() {
Dianne Hackborn4428e172012-08-24 17:43:05 -07001836 synchronized (mPackagesLock) {
Jeff Sharkeyffe0cb42012-11-05 17:24:43 -08001837 int i = MIN_USER_ID;
Amith Yamasani195263742012-08-21 15:40:12 -07001838 while (i < Integer.MAX_VALUE) {
Jeff Sharkeyffe0cb42012-11-05 17:24:43 -08001839 if (mUsers.indexOfKey(i) < 0 && !mRemovingUserIds.get(i)) {
Amith Yamasani195263742012-08-21 15:40:12 -07001840 break;
1841 }
1842 i++;
Amith Yamasani4b2e9342011-03-31 12:38:53 -07001843 }
Amith Yamasani195263742012-08-21 15:40:12 -07001844 return i;
Amith Yamasani4b2e9342011-03-31 12:38:53 -07001845 }
Amith Yamasani4b2e9342011-03-31 12:38:53 -07001846 }
Amith Yamasani920ace02012-09-20 22:15:37 -07001847
Amith Yamasanifc95e702013-09-26 13:20:17 -07001848 private String packageToRestrictionsFileName(String packageName) {
1849 return RESTRICTIONS_FILE_PREFIX + packageName + XML_SUFFIX;
1850 }
1851
Amith Yamasani920ace02012-09-20 22:15:37 -07001852 @Override
1853 protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
1854 if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.DUMP)
1855 != PackageManager.PERMISSION_GRANTED) {
1856 pw.println("Permission Denial: can't dump UserManager from from pid="
1857 + Binder.getCallingPid()
1858 + ", uid=" + Binder.getCallingUid()
1859 + " without permission "
1860 + android.Manifest.permission.DUMP);
1861 return;
1862 }
1863
1864 long now = System.currentTimeMillis();
1865 StringBuilder sb = new StringBuilder();
1866 synchronized (mPackagesLock) {
1867 pw.println("Users:");
1868 for (int i = 0; i < mUsers.size(); i++) {
1869 UserInfo user = mUsers.valueAt(i);
1870 if (user == null) continue;
Amith Yamasani634cf312012-10-04 17:34:21 -07001871 pw.print(" "); pw.print(user); pw.print(" serialNo="); pw.print(user.serialNumber);
Jeff Sharkeyffe0cb42012-11-05 17:24:43 -08001872 if (mRemovingUserIds.get(mUsers.keyAt(i))) pw.print(" <removing> ");
Dianne Hackbornd4ac8d72012-09-27 23:20:10 -07001873 if (user.partial) pw.print(" <partial>");
1874 pw.println();
Amith Yamasani920ace02012-09-20 22:15:37 -07001875 pw.print(" Created: ");
1876 if (user.creationTime == 0) {
1877 pw.println("<unknown>");
1878 } else {
1879 sb.setLength(0);
1880 TimeUtils.formatDuration(now - user.creationTime, sb);
1881 sb.append(" ago");
1882 pw.println(sb);
1883 }
1884 pw.print(" Last logged in: ");
1885 if (user.lastLoggedInTime == 0) {
1886 pw.println("<unknown>");
1887 } else {
1888 sb.setLength(0);
1889 TimeUtils.formatDuration(now - user.lastLoggedInTime, sb);
1890 sb.append(" ago");
1891 pw.println(sb);
1892 }
1893 }
1894 }
1895 }
Fyodor Kupolov75d0ea82014-12-15 16:21:07 -08001896
1897 final class MainHandler extends Handler {
1898
1899 @Override
1900 public void handleMessage(Message msg) {
1901 switch (msg.what) {
1902 case WRITE_USER_MSG:
1903 removeMessages(WRITE_USER_MSG, msg.obj);
1904 synchronized (mPackagesLock) {
1905 int userId = ((UserInfo) msg.obj).id;
1906 UserInfo userInfo = mUsers.get(userId);
1907 if (userInfo != null) {
1908 writeUserLocked(userInfo);
1909 }
1910 }
1911 }
1912 }
1913 }
Amith Yamasani4b2e9342011-03-31 12:38:53 -07001914}