blob: 5dd5a727af05dcc305547da42010ebc2dec80c8e [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
Xiaohui Chen70f6c382015-04-28 14:21:43 -0700212 File userZeroDir = new File(mUsersDir, String.valueOf(UserHandle.USER_SYSTEM));
Dianne Hackbornd4ac8d72012-09-27 23:20:10 -0700213 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 }
Xiaohui Chen70f6c382015-04-28 14:21:43 -0700246 onUserForeground(UserHandle.USER_SYSTEM);
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
Xiaohui Chen70f6c382015-04-28 14:21:43 -0700259 public UserInfo getPrimaryUser() {
260 checkManageUsersPermission("query users");
261 synchronized (mPackagesLock) {
262 for (int i = 0; i < mUsers.size(); i++) {
263 UserInfo ui = mUsers.valueAt(i);
264 if (ui.isPrimary()) {
265 return ui;
266 }
267 }
268 }
269 return null;
270 }
271
272 @Override
Amith Yamasani920ace02012-09-20 22:15:37 -0700273 public List<UserInfo> getUsers(boolean excludeDying) {
Amith Yamasani2a003292012-08-14 18:25:45 -0700274 checkManageUsersPermission("query users");
Dianne Hackborn4428e172012-08-24 17:43:05 -0700275 synchronized (mPackagesLock) {
Amith Yamasani13593602012-03-22 16:16:17 -0700276 ArrayList<UserInfo> users = new ArrayList<UserInfo>(mUsers.size());
277 for (int i = 0; i < mUsers.size(); i++) {
Dianne Hackbornd4ac8d72012-09-27 23:20:10 -0700278 UserInfo ui = mUsers.valueAt(i);
279 if (ui.partial) {
280 continue;
281 }
Jeff Sharkeyffe0cb42012-11-05 17:24:43 -0800282 if (!excludeDying || !mRemovingUserIds.get(ui.id)) {
Dianne Hackbornd4ac8d72012-09-27 23:20:10 -0700283 users.add(ui);
Amith Yamasani920ace02012-09-20 22:15:37 -0700284 }
Amith Yamasani13593602012-03-22 16:16:17 -0700285 }
286 return users;
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700287 }
Amith Yamasani13593602012-03-22 16:16:17 -0700288 }
289
Amith Yamasani258848d2012-08-10 17:06:33 -0700290 @Override
Alexandra Gherghina385124d2014-04-03 13:37:39 +0100291 public List<UserInfo> getProfiles(int userId, boolean enabledOnly) {
Amith Yamasani4f582632014-02-19 14:31:52 -0800292 if (userId != UserHandle.getCallingUserId()) {
293 checkManageUsersPermission("getting profiles related to user " + userId);
294 }
Amith Yamasanibe465322014-04-24 13:45:17 -0700295 final long ident = Binder.clearCallingIdentity();
296 try {
297 synchronized (mPackagesLock) {
298 return getProfilesLocked(userId, enabledOnly);
Alexandra Gherghina385124d2014-04-03 13:37:39 +0100299 }
Amith Yamasanibe465322014-04-24 13:45:17 -0700300 } finally {
301 Binder.restoreCallingIdentity(ident);
Kenny Guya52dc3e2014-02-11 15:33:14 +0000302 }
303 }
304
Amith Yamasanibe465322014-04-24 13:45:17 -0700305 /** Assume permissions already checked and caller's identity cleared */
306 private List<UserInfo> getProfilesLocked(int userId, boolean enabledOnly) {
Amith Yamasanibe465322014-04-24 13:45:17 -0700307 UserInfo user = getUserInfoLocked(userId);
308 ArrayList<UserInfo> users = new ArrayList<UserInfo>(mUsers.size());
Amith Yamasanidda003f2014-08-28 18:06:51 -0700309 if (user == null) {
310 // Probably a dying user
311 return users;
312 }
Amith Yamasanibe465322014-04-24 13:45:17 -0700313 for (int i = 0; i < mUsers.size(); i++) {
314 UserInfo profile = mUsers.valueAt(i);
315 if (!isProfileOf(user, profile)) {
316 continue;
317 }
Alexandra Gherghinadf35d572014-04-09 13:54:39 +0100318 if (enabledOnly && !profile.isEnabled()) {
319 continue;
Amith Yamasanibe465322014-04-24 13:45:17 -0700320 }
Amith Yamasani70fcf0c2014-07-11 08:40:19 -0700321 if (mRemovingUserIds.get(profile.id)) {
322 continue;
323 }
Amith Yamasanibe465322014-04-24 13:45:17 -0700324 users.add(profile);
325 }
326 return users;
327 }
328
Jessica Hummelbe81c802014-04-22 15:49:22 +0100329 @Override
330 public UserInfo getProfileParent(int userHandle) {
331 checkManageUsersPermission("get the profile parent");
332 synchronized (mPackagesLock) {
Fyodor Kupolovff7233e2015-04-08 11:28:52 -0700333 return getProfileParentLocked(userHandle);
334 }
335 }
336
337 private UserInfo getProfileParentLocked(int userHandle) {
338 UserInfo profile = getUserInfoLocked(userHandle);
339 if (profile == null) {
340 return null;
341 }
342 int parentUserId = profile.profileGroupId;
343 if (parentUserId == UserInfo.NO_PROFILE_GROUP_ID) {
344 return null;
345 } else {
346 return getUserInfoLocked(parentUserId);
Jessica Hummelbe81c802014-04-22 15:49:22 +0100347 }
348 }
349
Kenny Guy2a764942014-04-02 13:29:20 +0100350 private boolean isProfileOf(UserInfo user, UserInfo profile) {
351 return user.id == profile.id ||
352 (user.profileGroupId != UserInfo.NO_PROFILE_GROUP_ID
353 && user.profileGroupId == profile.profileGroupId);
Kenny Guya52dc3e2014-02-11 15:33:14 +0000354 }
355
356 @Override
Alexandra Gherghinadf35d572014-04-09 13:54:39 +0100357 public void setUserEnabled(int userId) {
358 checkManageUsersPermission("enable user");
359 synchronized (mPackagesLock) {
360 UserInfo info = getUserInfoLocked(userId);
361 if (info != null && !info.isEnabled()) {
362 info.flags ^= UserInfo.FLAG_DISABLED;
363 writeUserLocked(info);
364 }
365 }
366 }
367
368 @Override
Amith Yamasani258848d2012-08-10 17:06:33 -0700369 public UserInfo getUserInfo(int userId) {
Amith Yamasani2a003292012-08-14 18:25:45 -0700370 checkManageUsersPermission("query user");
Dianne Hackborn4428e172012-08-24 17:43:05 -0700371 synchronized (mPackagesLock) {
Amith Yamasani195263742012-08-21 15:40:12 -0700372 return getUserInfoLocked(userId);
Amith Yamasani13593602012-03-22 16:16:17 -0700373 }
374 }
375
Amith Yamasani71e6c692013-03-24 17:39:28 -0700376 @Override
377 public boolean isRestricted() {
378 synchronized (mPackagesLock) {
379 return getUserInfoLocked(UserHandle.getCallingUserId()).isRestricted();
380 }
381 }
382
Amith Yamasani195263742012-08-21 15:40:12 -0700383 /*
384 * Should be locked on mUsers before calling this.
385 */
386 private UserInfo getUserInfoLocked(int userId) {
Dianne Hackbornd4ac8d72012-09-27 23:20:10 -0700387 UserInfo ui = mUsers.get(userId);
Amith Yamasani16389312012-10-17 21:20:14 -0700388 // If it is partial and not in the process of being removed, return as unknown user.
Jeff Sharkeyffe0cb42012-11-05 17:24:43 -0800389 if (ui != null && ui.partial && !mRemovingUserIds.get(userId)) {
Dianne Hackbornd4ac8d72012-09-27 23:20:10 -0700390 Slog.w(LOG_TAG, "getUserInfo: unknown user #" + userId);
391 return null;
392 }
393 return ui;
Amith Yamasani195263742012-08-21 15:40:12 -0700394 }
395
Amith Yamasani13593602012-03-22 16:16:17 -0700396 public boolean exists(int userId) {
Dianne Hackborn4428e172012-08-24 17:43:05 -0700397 synchronized (mPackagesLock) {
Amith Yamasani13593602012-03-22 16:16:17 -0700398 return ArrayUtils.contains(mUserIds, userId);
399 }
400 }
401
Amith Yamasani258848d2012-08-10 17:06:33 -0700402 @Override
Amith Yamasanib8151ec2012-04-18 18:02:48 -0700403 public void setUserName(int userId, String name) {
Amith Yamasani2a003292012-08-14 18:25:45 -0700404 checkManageUsersPermission("rename users");
Dianne Hackbornd4ac8d72012-09-27 23:20:10 -0700405 boolean changed = false;
Dianne Hackborn4428e172012-08-24 17:43:05 -0700406 synchronized (mPackagesLock) {
Amith Yamasani13593602012-03-22 16:16:17 -0700407 UserInfo info = mUsers.get(userId);
Dianne Hackbornd4ac8d72012-09-27 23:20:10 -0700408 if (info == null || info.partial) {
409 Slog.w(LOG_TAG, "setUserName: unknown user #" + userId);
410 return;
411 }
Amith Yamasani13593602012-03-22 16:16:17 -0700412 if (name != null && !name.equals(info.name)) {
413 info.name = name;
414 writeUserLocked(info);
Dianne Hackbornd4ac8d72012-09-27 23:20:10 -0700415 changed = true;
Amith Yamasani13593602012-03-22 16:16:17 -0700416 }
417 }
Dianne Hackbornd4ac8d72012-09-27 23:20:10 -0700418 if (changed) {
419 sendUserInfoChangedBroadcast(userId);
420 }
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700421 }
422
Amith Yamasani258848d2012-08-10 17:06:33 -0700423 @Override
Amith Yamasanie928d7d2012-09-17 21:46:51 -0700424 public void setUserIcon(int userId, Bitmap bitmap) {
Amith Yamasani2a003292012-08-14 18:25:45 -0700425 checkManageUsersPermission("update users");
Jason Monk9a944532014-07-08 09:31:21 -0400426 long ident = Binder.clearCallingIdentity();
427 try {
428 synchronized (mPackagesLock) {
429 UserInfo info = mUsers.get(userId);
430 if (info == null || info.partial) {
431 Slog.w(LOG_TAG, "setUserIcon: unknown user #" + userId);
432 return;
433 }
434 writeBitmapLocked(info, bitmap);
435 writeUserLocked(info);
Dianne Hackbornd4ac8d72012-09-27 23:20:10 -0700436 }
Jason Monk9a944532014-07-08 09:31:21 -0400437 sendUserInfoChangedBroadcast(userId);
438 } finally {
439 Binder.restoreCallingIdentity(ident);
Amith Yamasanib8151ec2012-04-18 18:02:48 -0700440 }
Amith Yamasanie928d7d2012-09-17 21:46:51 -0700441 }
442
443 private void sendUserInfoChangedBroadcast(int userId) {
444 Intent changedIntent = new Intent(Intent.ACTION_USER_INFO_CHANGED);
445 changedIntent.putExtra(Intent.EXTRA_USER_HANDLE, userId);
446 changedIntent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY);
Amith Yamasani6fc1d4e2013-05-08 16:43:58 -0700447 mContext.sendBroadcastAsUser(changedIntent, UserHandle.ALL);
Amith Yamasanib8151ec2012-04-18 18:02:48 -0700448 }
449
Amith Yamasani258848d2012-08-10 17:06:33 -0700450 @Override
Adrian Roos1bdff912015-02-17 15:51:35 +0100451 public ParcelFileDescriptor getUserIcon(int userId) {
452 String iconPath;
Amith Yamasani3b49f072012-09-17 10:21:43 -0700453 synchronized (mPackagesLock) {
454 UserInfo info = mUsers.get(userId);
Dianne Hackbornd4ac8d72012-09-27 23:20:10 -0700455 if (info == null || info.partial) {
456 Slog.w(LOG_TAG, "getUserIcon: unknown user #" + userId);
457 return null;
458 }
Nicolas Prevot88cc3462014-05-14 14:51:48 +0100459 int callingGroupId = mUsers.get(UserHandle.getCallingUserId()).profileGroupId;
460 if (callingGroupId == UserInfo.NO_PROFILE_GROUP_ID
461 || callingGroupId != info.profileGroupId) {
462 checkManageUsersPermission("get the icon of a user who is not related");
463 }
Dianne Hackbornd4ac8d72012-09-27 23:20:10 -0700464 if (info.iconPath == null) {
465 return null;
466 }
Adrian Roos1bdff912015-02-17 15:51:35 +0100467 iconPath = info.iconPath;
Amith Yamasani3b49f072012-09-17 10:21:43 -0700468 }
Adrian Roos1bdff912015-02-17 15:51:35 +0100469
470 try {
471 return ParcelFileDescriptor.open(
472 new File(iconPath), ParcelFileDescriptor.MODE_READ_ONLY);
473 } catch (FileNotFoundException e) {
474 Log.e(LOG_TAG, "Couldn't find icon file", e);
475 }
476 return null;
Amith Yamasani3b49f072012-09-17 10:21:43 -0700477 }
478
Dianne Hackborn5dc5a002012-09-15 19:33:48 -0700479 public void makeInitialized(int userId) {
480 checkManageUsersPermission("makeInitialized");
481 synchronized (mPackagesLock) {
482 UserInfo info = mUsers.get(userId);
Dianne Hackbornd4ac8d72012-09-27 23:20:10 -0700483 if (info == null || info.partial) {
484 Slog.w(LOG_TAG, "makeInitialized: unknown user #" + userId);
485 }
486 if ((info.flags&UserInfo.FLAG_INITIALIZED) == 0) {
Dianne Hackborn5dc5a002012-09-15 19:33:48 -0700487 info.flags |= UserInfo.FLAG_INITIALIZED;
Fyodor Kupolov75d0ea82014-12-15 16:21:07 -0800488 scheduleWriteUserLocked(info);
Dianne Hackborn5dc5a002012-09-15 19:33:48 -0700489 }
490 }
491 }
492
Amith Yamasaniaa6634e2014-10-06 14:20:28 -0700493 /**
494 * If default guest restrictions haven't been initialized yet, add the basic
495 * restrictions.
496 */
497 private void initDefaultGuestRestrictions() {
498 if (mGuestRestrictions.isEmpty()) {
499 mGuestRestrictions.putBoolean(UserManager.DISALLOW_OUTGOING_CALLS, true);
Amith Yamasanida0b1682014-11-21 12:58:17 -0800500 mGuestRestrictions.putBoolean(UserManager.DISALLOW_SMS, true);
Amith Yamasaniaa6634e2014-10-06 14:20:28 -0700501 }
502 }
503
Amith Yamasanie4cf7342012-12-17 11:12:09 -0800504 @Override
Amith Yamasanie4afaa32014-06-30 14:55:07 +0530505 public Bundle getDefaultGuestRestrictions() {
506 checkManageUsersPermission("getDefaultGuestRestrictions");
507 synchronized (mPackagesLock) {
508 return new Bundle(mGuestRestrictions);
509 }
510 }
511
512 @Override
513 public void setDefaultGuestRestrictions(Bundle restrictions) {
514 checkManageUsersPermission("setDefaultGuestRestrictions");
515 synchronized (mPackagesLock) {
516 mGuestRestrictions.clear();
517 mGuestRestrictions.putAll(restrictions);
518 writeUserListLocked();
519 }
520 }
521
522 @Override
Amith Yamasani8cd28b52014-06-08 17:54:27 -0700523 public boolean hasUserRestriction(String restrictionKey, int userId) {
524 synchronized (mPackagesLock) {
525 Bundle restrictions = mUserRestrictions.get(userId);
Fyodor Kupolovb5013302015-04-17 17:59:14 -0700526 return restrictions != null && restrictions.getBoolean(restrictionKey);
Amith Yamasani8cd28b52014-06-08 17:54:27 -0700527 }
528 }
529
530 @Override
Amith Yamasanie4cf7342012-12-17 11:12:09 -0800531 public Bundle getUserRestrictions(int userId) {
532 // checkManageUsersPermission("getUserRestrictions");
533
534 synchronized (mPackagesLock) {
535 Bundle restrictions = mUserRestrictions.get(userId);
Amith Yamasanibe465322014-04-24 13:45:17 -0700536 return restrictions != null ? new Bundle(restrictions) : new Bundle();
Amith Yamasanie4cf7342012-12-17 11:12:09 -0800537 }
538 }
539
540 @Override
Fyodor Kupolovb5013302015-04-17 17:59:14 -0700541 public void setUserRestriction(String key, boolean value, int userId) {
542 synchronized (mPackagesLock) {
543 if (!SYSTEM_CONTROLLED_RESTRICTIONS.contains(key)) {
544 Bundle restrictions = getUserRestrictions(userId);
545 restrictions.putBoolean(key, value);
546 setUserRestrictionsInternalLocked(restrictions, userId);
547 }
548 }
549 }
550
551 @Override
552 public void setSystemControlledUserRestriction(String key, boolean value, int userId) {
553 checkSystemOrRoot("setSystemControlledUserRestriction");
554 synchronized (mPackagesLock) {
555 Bundle restrictions = getUserRestrictions(userId);
556 restrictions.putBoolean(key, value);
557 setUserRestrictionsInternalLocked(restrictions, userId);
558 }
559 }
560
561 @Override
Amith Yamasanie4cf7342012-12-17 11:12:09 -0800562 public void setUserRestrictions(Bundle restrictions, int userId) {
Amith Yamasanibe465322014-04-24 13:45:17 -0700563 checkManageUsersPermission("setUserRestrictions");
Amith Yamasani0343ec32013-07-22 14:52:06 -0700564 if (restrictions == null) return;
Amith Yamasanie4cf7342012-12-17 11:12:09 -0800565
566 synchronized (mPackagesLock) {
Fyodor Kupolovb5013302015-04-17 17:59:14 -0700567 final Bundle oldUserRestrictions = mUserRestrictions.get(userId);
568 // Restore the original state of system controlled restrictions from oldUserRestrictions
569 for (String key : SYSTEM_CONTROLLED_RESTRICTIONS) {
570 restrictions.remove(key);
571 if (oldUserRestrictions.containsKey(key)) {
572 restrictions.putBoolean(key, oldUserRestrictions.getBoolean(key));
573 }
Jason Monk62062992014-05-06 09:55:28 -0400574 }
Fyodor Kupolovb5013302015-04-17 17:59:14 -0700575 setUserRestrictionsInternalLocked(restrictions, userId);
Amith Yamasanie4cf7342012-12-17 11:12:09 -0800576 }
577 }
578
Fyodor Kupolovb5013302015-04-17 17:59:14 -0700579 private void setUserRestrictionsInternalLocked(Bundle restrictions, int userId) {
580 final Bundle userRestrictions = mUserRestrictions.get(userId);
581 userRestrictions.clear();
582 userRestrictions.putAll(restrictions);
583 long token = Binder.clearCallingIdentity();
584 try {
585 mAppOpsService.setUserRestrictions(userRestrictions, userId);
586 } catch (RemoteException e) {
587 Log.w(LOG_TAG, "Unable to notify AppOpsService of UserRestrictions");
588 } finally {
589 Binder.restoreCallingIdentity(token);
590 }
591 scheduleWriteUserLocked(mUsers.get(userId));
592 }
593
Amith Yamasani258848d2012-08-10 17:06:33 -0700594 /**
Amith Yamasanifaea76f2012-09-11 10:59:48 -0700595 * Check if we've hit the limit of how many users can be created.
596 */
Dianne Hackborn5dc5a002012-09-15 19:33:48 -0700597 private boolean isUserLimitReachedLocked() {
Nicolas Prevot12678a92015-05-13 12:15:03 -0700598 return getAliveUsersExcludingGuestsCountLocked() >= UserManager.getMaxSupportedUsers();
599 }
600
601 @Override
602 public boolean canAddMoreManagedProfiles() {
603 checkManageUsersPermission("check if more managed profiles can be added.");
604 if (ActivityManager.isLowRamDeviceStatic()) {
605 return false;
606 }
Fyodor Kupolovb6157992015-06-05 15:32:28 -0700607 if (!mContext.getPackageManager().hasSystemFeature(
608 PackageManager.FEATURE_MANAGED_USERS)) {
609 return false;
610 }
Nicolas Prevot12678a92015-05-13 12:15:03 -0700611 synchronized(mPackagesLock) {
612 // Limit number of managed profiles that can be created
613 if (numberOfUsersOfTypeLocked(UserInfo.FLAG_MANAGED_PROFILE, true)
614 >= MAX_MANAGED_PROFILES) {
615 return false;
616 }
617 int usersCount = getAliveUsersExcludingGuestsCountLocked();
618 // We allow creating a managed profile in the special case where there is only one user.
619 return usersCount == 1 || usersCount < UserManager.getMaxSupportedUsers();
620 }
621 }
622
623 private int getAliveUsersExcludingGuestsCountLocked() {
Amith Yamasanif584f012014-05-19 17:57:25 -0700624 int aliveUserCount = 0;
625 final int totalUserCount = mUsers.size();
626 // Skip over users being removed
627 for (int i = 0; i < totalUserCount; i++) {
628 UserInfo user = mUsers.valueAt(i);
Amith Yamasani95ab7842014-08-11 17:09:26 -0700629 if (!mRemovingUserIds.get(user.id)
Amith Yamasani1df14732014-08-29 21:37:27 -0700630 && !user.isGuest() && !user.partial) {
Amith Yamasanif584f012014-05-19 17:57:25 -0700631 aliveUserCount++;
632 }
633 }
Nicolas Prevot12678a92015-05-13 12:15:03 -0700634 return aliveUserCount;
Amith Yamasanifaea76f2012-09-11 10:59:48 -0700635 }
636
637 /**
Amith Yamasani195263742012-08-21 15:40:12 -0700638 * Enforces that only the system UID or root's UID or apps that have the
Dianne Hackborn10ad9822014-03-17 11:28:36 -0700639 * {@link android.Manifest.permission#MANAGE_USERS MANAGE_USERS}
Amith Yamasani195263742012-08-21 15:40:12 -0700640 * permission can make certain calls to the UserManager.
Amith Yamasani258848d2012-08-10 17:06:33 -0700641 *
642 * @param message used as message if SecurityException is thrown
643 * @throws SecurityException if the caller is not system or root
644 */
Amith Yamasanibe465322014-04-24 13:45:17 -0700645 private static final void checkManageUsersPermission(String message) {
Amith Yamasani258848d2012-08-10 17:06:33 -0700646 final int uid = Binder.getCallingUid();
Amith Yamasanibe465322014-04-24 13:45:17 -0700647 if (uid != Process.SYSTEM_UID && uid != 0
Emily Bernier7a2b4d12014-04-23 12:51:35 -0400648 && ActivityManager.checkComponentPermission(
649 android.Manifest.permission.MANAGE_USERS,
Amith Yamasanibe465322014-04-24 13:45:17 -0700650 uid, -1, true) != PackageManager.PERMISSION_GRANTED) {
651 throw new SecurityException("You need MANAGE_USERS permission to: " + message);
652 }
Emily Bernier7a2b4d12014-04-23 12:51:35 -0400653 }
654
Fyodor Kupolovb5013302015-04-17 17:59:14 -0700655 private static void checkSystemOrRoot(String message) {
656 final int uid = Binder.getCallingUid();
657 if (uid != Process.SYSTEM_UID && uid != 0) {
658 throw new SecurityException("Only system may call: " + message);
659 }
660 }
661
Amith Yamasanie928d7d2012-09-17 21:46:51 -0700662 private void writeBitmapLocked(UserInfo info, Bitmap bitmap) {
Amith Yamasanib8151ec2012-04-18 18:02:48 -0700663 try {
664 File dir = new File(mUsersDir, Integer.toString(info.id));
665 File file = new File(dir, USER_PHOTO_FILENAME);
Adrian Roos1bdff912015-02-17 15:51:35 +0100666 File tmp = new File(dir, USER_PHOTO_FILENAME_TMP);
Amith Yamasanib8151ec2012-04-18 18:02:48 -0700667 if (!dir.exists()) {
668 dir.mkdir();
669 FileUtils.setPermissions(
670 dir.getPath(),
671 FileUtils.S_IRWXU|FileUtils.S_IRWXG|FileUtils.S_IXOTH,
672 -1, -1);
673 }
Amith Yamasanie928d7d2012-09-17 21:46:51 -0700674 FileOutputStream os;
Adrian Roos1bdff912015-02-17 15:51:35 +0100675 if (bitmap.compress(Bitmap.CompressFormat.PNG, 100, os = new FileOutputStream(tmp))
676 && tmp.renameTo(file)) {
Amith Yamasani3b49f072012-09-17 10:21:43 -0700677 info.iconPath = file.getAbsolutePath();
678 }
Amith Yamasanie928d7d2012-09-17 21:46:51 -0700679 try {
680 os.close();
681 } catch (IOException ioe) {
682 // What the ... !
683 }
Adrian Roos1bdff912015-02-17 15:51:35 +0100684 tmp.delete();
Amith Yamasanib8151ec2012-04-18 18:02:48 -0700685 } catch (FileNotFoundException e) {
Amith Yamasani2a003292012-08-14 18:25:45 -0700686 Slog.w(LOG_TAG, "Error setting photo for user ", e);
Amith Yamasanib8151ec2012-04-18 18:02:48 -0700687 }
Amith Yamasanib8151ec2012-04-18 18:02:48 -0700688 }
689
Amith Yamasani0b285492011-04-14 17:35:23 -0700690 /**
691 * Returns an array of user ids. This array is cached here for quick access, so do not modify or
692 * cache it elsewhere.
693 * @return the array of user ids.
694 */
Dianne Hackborn1676c852012-09-10 14:52:30 -0700695 public int[] getUserIds() {
Dianne Hackborn4428e172012-08-24 17:43:05 -0700696 synchronized (mPackagesLock) {
Dianne Hackborn7767eac2012-08-23 18:25:40 -0700697 return mUserIds;
698 }
Amith Yamasani0b285492011-04-14 17:35:23 -0700699 }
700
Dianne Hackborn4428e172012-08-24 17:43:05 -0700701 int[] getUserIdsLPr() {
702 return mUserIds;
703 }
704
Amith Yamasani13593602012-03-22 16:16:17 -0700705 private void readUserListLocked() {
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700706 if (!mUserListFile.exists()) {
Amith Yamasani13593602012-03-22 16:16:17 -0700707 fallbackToSingleUserLocked();
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700708 return;
709 }
710 FileInputStream fis = null;
Amith Yamasani2a003292012-08-14 18:25:45 -0700711 AtomicFile userListFile = new AtomicFile(mUserListFile);
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700712 try {
Amith Yamasani2a003292012-08-14 18:25:45 -0700713 fis = userListFile.openRead();
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700714 XmlPullParser parser = Xml.newPullParser();
Wojciech Staszkiewicz9e9e2e72015-05-08 14:58:46 +0100715 parser.setInput(fis, StandardCharsets.UTF_8.name());
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700716 int type;
717 while ((type = parser.next()) != XmlPullParser.START_TAG
718 && type != XmlPullParser.END_DOCUMENT) {
719 ;
720 }
721
722 if (type != XmlPullParser.START_TAG) {
Amith Yamasani0b285492011-04-14 17:35:23 -0700723 Slog.e(LOG_TAG, "Unable to read user list");
Amith Yamasani13593602012-03-22 16:16:17 -0700724 fallbackToSingleUserLocked();
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700725 return;
726 }
727
Amith Yamasani2a003292012-08-14 18:25:45 -0700728 mNextSerialNumber = -1;
729 if (parser.getName().equals(TAG_USERS)) {
730 String lastSerialNumber = parser.getAttributeValue(null, ATTR_NEXT_SERIAL_NO);
731 if (lastSerialNumber != null) {
732 mNextSerialNumber = Integer.parseInt(lastSerialNumber);
733 }
Amith Yamasani6f34b412012-10-22 18:19:27 -0700734 String versionNumber = parser.getAttributeValue(null, ATTR_USER_VERSION);
735 if (versionNumber != null) {
736 mUserVersion = Integer.parseInt(versionNumber);
737 }
Amith Yamasani2a003292012-08-14 18:25:45 -0700738 }
739
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700740 while ((type = parser.next()) != XmlPullParser.END_DOCUMENT) {
Amith Yamasanie4afaa32014-06-30 14:55:07 +0530741 if (type == XmlPullParser.START_TAG) {
742 final String name = parser.getName();
743 if (name.equals(TAG_USER)) {
744 String id = parser.getAttributeValue(null, ATTR_ID);
745 UserInfo user = readUserLocked(Integer.parseInt(id));
Amith Yamasani6f34b412012-10-22 18:19:27 -0700746
Amith Yamasanie4afaa32014-06-30 14:55:07 +0530747 if (user != null) {
748 mUsers.put(user.id, user);
749 if (mNextSerialNumber < 0 || mNextSerialNumber <= user.id) {
750 mNextSerialNumber = user.id + 1;
751 }
Amith Yamasani2a003292012-08-14 18:25:45 -0700752 }
Amith Yamasanie4afaa32014-06-30 14:55:07 +0530753 } else if (name.equals(TAG_GUEST_RESTRICTIONS)) {
Amith Yamasanida0b1682014-11-21 12:58:17 -0800754 while ((type = parser.next()) != XmlPullParser.END_DOCUMENT
755 && type != XmlPullParser.END_TAG) {
756 if (type == XmlPullParser.START_TAG) {
757 if (parser.getName().equals(TAG_RESTRICTIONS)) {
758 readRestrictionsLocked(parser, mGuestRestrictions);
759 }
760 break;
761 }
762 }
Amith Yamasani258848d2012-08-10 17:06:33 -0700763 }
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700764 }
765 }
Amith Yamasani13593602012-03-22 16:16:17 -0700766 updateUserIdsLocked();
Amith Yamasani350962c2013-08-06 11:18:53 -0700767 upgradeIfNecessaryLocked();
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700768 } catch (IOException ioe) {
Amith Yamasani13593602012-03-22 16:16:17 -0700769 fallbackToSingleUserLocked();
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700770 } catch (XmlPullParserException pe) {
Amith Yamasani13593602012-03-22 16:16:17 -0700771 fallbackToSingleUserLocked();
Dianne Hackbornbfd89b32011-12-15 18:22:54 -0800772 } finally {
773 if (fis != null) {
774 try {
775 fis.close();
776 } catch (IOException e) {
777 }
778 }
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700779 }
780 }
781
Amith Yamasani6f34b412012-10-22 18:19:27 -0700782 /**
Amith Yamasanibc9625052012-11-15 14:39:18 -0800783 * Upgrade steps between versions, either for fixing bugs or changing the data format.
Amith Yamasani6f34b412012-10-22 18:19:27 -0700784 */
Amith Yamasani350962c2013-08-06 11:18:53 -0700785 private void upgradeIfNecessaryLocked() {
Amith Yamasani6f34b412012-10-22 18:19:27 -0700786 int userVersion = mUserVersion;
787 if (userVersion < 1) {
788 // Assign a proper name for the owner, if not initialized correctly before
789 UserInfo user = mUsers.get(UserHandle.USER_OWNER);
790 if ("Primary".equals(user.name)) {
791 user.name = mContext.getResources().getString(com.android.internal.R.string.owner_name);
Fyodor Kupolov75d0ea82014-12-15 16:21:07 -0800792 scheduleWriteUserLocked(user);
Amith Yamasani6f34b412012-10-22 18:19:27 -0700793 }
794 userVersion = 1;
795 }
796
Amith Yamasanibc9625052012-11-15 14:39:18 -0800797 if (userVersion < 2) {
798 // Owner should be marked as initialized
799 UserInfo user = mUsers.get(UserHandle.USER_OWNER);
800 if ((user.flags & UserInfo.FLAG_INITIALIZED) == 0) {
801 user.flags |= UserInfo.FLAG_INITIALIZED;
Fyodor Kupolov75d0ea82014-12-15 16:21:07 -0800802 scheduleWriteUserLocked(user);
Amith Yamasanibc9625052012-11-15 14:39:18 -0800803 }
804 userVersion = 2;
805 }
806
Amith Yamasani350962c2013-08-06 11:18:53 -0700807
Amith Yamasani5e486f52013-08-07 11:06:44 -0700808 if (userVersion < 4) {
Amith Yamasani5e486f52013-08-07 11:06:44 -0700809 userVersion = 4;
810 }
811
Amith Yamasaniaa6634e2014-10-06 14:20:28 -0700812 if (userVersion < 5) {
813 initDefaultGuestRestrictions();
814 userVersion = 5;
815 }
816
Amith Yamasani6f34b412012-10-22 18:19:27 -0700817 if (userVersion < USER_VERSION) {
818 Slog.w(LOG_TAG, "User version " + mUserVersion + " didn't upgrade as expected to "
819 + USER_VERSION);
820 } else {
821 mUserVersion = userVersion;
822 writeUserListLocked();
823 }
824 }
825
Amith Yamasani13593602012-03-22 16:16:17 -0700826 private void fallbackToSingleUserLocked() {
Xiaohui Chen70f6c382015-04-28 14:21:43 -0700827 // Create the system user
828 // TODO: UserInfo.FLAG_PRIMARY flag should be set on the first human user.
829 UserInfo system = new UserInfo(UserHandle.USER_SYSTEM,
Amith Yamasani6f34b412012-10-22 18:19:27 -0700830 mContext.getResources().getString(com.android.internal.R.string.owner_name), null,
Amith Yamasani756901d2012-10-12 12:30:07 -0700831 UserInfo.FLAG_ADMIN | UserInfo.FLAG_PRIMARY | UserInfo.FLAG_INITIALIZED);
Xiaohui Chen70f6c382015-04-28 14:21:43 -0700832 mUsers.put(system.id, system);
Amith Yamasani634cf312012-10-04 17:34:21 -0700833 mNextSerialNumber = MIN_USER_ID;
Geoffrey Borggaard15b8b2c2013-08-28 22:11:10 -0400834 mUserVersion = USER_VERSION;
Amith Yamasani67df64b2012-12-14 12:09:36 -0800835
Geoffrey Borggaarde45e45e32013-01-24 10:03:20 -0500836 Bundle restrictions = new Bundle();
Xiaohui Chen70f6c382015-04-28 14:21:43 -0700837 mUserRestrictions.append(UserHandle.USER_SYSTEM, restrictions);
Amith Yamasani67df64b2012-12-14 12:09:36 -0800838
Amith Yamasani13593602012-03-22 16:16:17 -0700839 updateUserIdsLocked();
Amith Yamasaniaa6634e2014-10-06 14:20:28 -0700840 initDefaultGuestRestrictions();
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700841
Amith Yamasani13593602012-03-22 16:16:17 -0700842 writeUserListLocked();
Xiaohui Chen70f6c382015-04-28 14:21:43 -0700843 writeUserLocked(system);
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700844 }
845
Fyodor Kupolov75d0ea82014-12-15 16:21:07 -0800846 private void scheduleWriteUserLocked(UserInfo userInfo) {
847 if (!mHandler.hasMessages(WRITE_USER_MSG, userInfo)) {
848 Message msg = mHandler.obtainMessage(WRITE_USER_MSG, userInfo);
849 mHandler.sendMessageDelayed(msg, WRITE_USER_DELAY);
850 }
851 }
852
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700853 /*
854 * Writes the user file in this format:
855 *
856 * <user flags="20039023" id="0">
857 * <name>Primary</name>
858 * </user>
859 */
Amith Yamasani13593602012-03-22 16:16:17 -0700860 private void writeUserLocked(UserInfo userInfo) {
Amith Yamasani742a6712011-05-04 14:49:28 -0700861 FileOutputStream fos = null;
Amith Yamasanifc95e702013-09-26 13:20:17 -0700862 AtomicFile userFile = new AtomicFile(new File(mUsersDir, userInfo.id + XML_SUFFIX));
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700863 try {
Amith Yamasani2a003292012-08-14 18:25:45 -0700864 fos = userFile.startWrite();
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700865 final BufferedOutputStream bos = new BufferedOutputStream(fos);
866
867 // XmlSerializer serializer = XmlUtils.serializerInstance();
868 final XmlSerializer serializer = new FastXmlSerializer();
Wojciech Staszkiewicz9e9e2e72015-05-08 14:58:46 +0100869 serializer.setOutput(bos, StandardCharsets.UTF_8.name());
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700870 serializer.startDocument(null, true);
871 serializer.setFeature("http://xmlpull.org/v1/doc/features.html#indent-output", true);
872
873 serializer.startTag(null, TAG_USER);
874 serializer.attribute(null, ATTR_ID, Integer.toString(userInfo.id));
Amith Yamasani2a003292012-08-14 18:25:45 -0700875 serializer.attribute(null, ATTR_SERIAL_NO, Integer.toString(userInfo.serialNumber));
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700876 serializer.attribute(null, ATTR_FLAGS, Integer.toString(userInfo.flags));
Amith Yamasani920ace02012-09-20 22:15:37 -0700877 serializer.attribute(null, ATTR_CREATION_TIME, Long.toString(userInfo.creationTime));
878 serializer.attribute(null, ATTR_LAST_LOGGED_IN_TIME,
879 Long.toString(userInfo.lastLoggedInTime));
Amith Yamasanib8151ec2012-04-18 18:02:48 -0700880 if (userInfo.iconPath != null) {
881 serializer.attribute(null, ATTR_ICON_PATH, userInfo.iconPath);
882 }
Dianne Hackbornd4ac8d72012-09-27 23:20:10 -0700883 if (userInfo.partial) {
884 serializer.attribute(null, ATTR_PARTIAL, "true");
885 }
Adam Lesinskieddeb492014-09-08 17:50:03 -0700886 if (userInfo.guestToRemove) {
887 serializer.attribute(null, ATTR_GUEST_TO_REMOVE, "true");
888 }
Kenny Guy2a764942014-04-02 13:29:20 +0100889 if (userInfo.profileGroupId != UserInfo.NO_PROFILE_GROUP_ID) {
890 serializer.attribute(null, ATTR_PROFILE_GROUP_ID,
891 Integer.toString(userInfo.profileGroupId));
Kenny Guya52dc3e2014-02-11 15:33:14 +0000892 }
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700893
894 serializer.startTag(null, TAG_NAME);
895 serializer.text(userInfo.name);
896 serializer.endTag(null, TAG_NAME);
Amith Yamasanie4cf7342012-12-17 11:12:09 -0800897 Bundle restrictions = mUserRestrictions.get(userInfo.id);
898 if (restrictions != null) {
Amith Yamasanie4afaa32014-06-30 14:55:07 +0530899 writeRestrictionsLocked(serializer, restrictions);
Amith Yamasanie4cf7342012-12-17 11:12:09 -0800900 }
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700901 serializer.endTag(null, TAG_USER);
902
903 serializer.endDocument();
Amith Yamasani2a003292012-08-14 18:25:45 -0700904 userFile.finishWrite(fos);
905 } catch (Exception ioe) {
Amith Yamasani0b285492011-04-14 17:35:23 -0700906 Slog.e(LOG_TAG, "Error writing user info " + userInfo.id + "\n" + ioe);
Amith Yamasani2a003292012-08-14 18:25:45 -0700907 userFile.failWrite(fos);
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700908 }
909 }
910
911 /*
912 * Writes the user list file in this format:
913 *
Amith Yamasani2a003292012-08-14 18:25:45 -0700914 * <users nextSerialNumber="3">
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700915 * <user id="0"></user>
916 * <user id="2"></user>
917 * </users>
918 */
Amith Yamasani13593602012-03-22 16:16:17 -0700919 private void writeUserListLocked() {
Amith Yamasani742a6712011-05-04 14:49:28 -0700920 FileOutputStream fos = null;
Amith Yamasani2a003292012-08-14 18:25:45 -0700921 AtomicFile userListFile = new AtomicFile(mUserListFile);
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700922 try {
Amith Yamasani2a003292012-08-14 18:25:45 -0700923 fos = userListFile.startWrite();
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700924 final BufferedOutputStream bos = new BufferedOutputStream(fos);
925
926 // XmlSerializer serializer = XmlUtils.serializerInstance();
927 final XmlSerializer serializer = new FastXmlSerializer();
Wojciech Staszkiewicz9e9e2e72015-05-08 14:58:46 +0100928 serializer.setOutput(bos, StandardCharsets.UTF_8.name());
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700929 serializer.startDocument(null, true);
930 serializer.setFeature("http://xmlpull.org/v1/doc/features.html#indent-output", true);
931
932 serializer.startTag(null, TAG_USERS);
Amith Yamasani2a003292012-08-14 18:25:45 -0700933 serializer.attribute(null, ATTR_NEXT_SERIAL_NO, Integer.toString(mNextSerialNumber));
Amith Yamasani6f34b412012-10-22 18:19:27 -0700934 serializer.attribute(null, ATTR_USER_VERSION, Integer.toString(mUserVersion));
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700935
Adam Lesinskieddeb492014-09-08 17:50:03 -0700936 serializer.startTag(null, TAG_GUEST_RESTRICTIONS);
Amith Yamasanie4afaa32014-06-30 14:55:07 +0530937 writeRestrictionsLocked(serializer, mGuestRestrictions);
938 serializer.endTag(null, TAG_GUEST_RESTRICTIONS);
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700939 for (int i = 0; i < mUsers.size(); i++) {
940 UserInfo user = mUsers.valueAt(i);
941 serializer.startTag(null, TAG_USER);
942 serializer.attribute(null, ATTR_ID, Integer.toString(user.id));
943 serializer.endTag(null, TAG_USER);
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700944 }
945
946 serializer.endTag(null, TAG_USERS);
947
948 serializer.endDocument();
Amith Yamasani2a003292012-08-14 18:25:45 -0700949 userListFile.finishWrite(fos);
950 } catch (Exception e) {
951 userListFile.failWrite(fos);
Amith Yamasani0b285492011-04-14 17:35:23 -0700952 Slog.e(LOG_TAG, "Error writing user list");
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700953 }
954 }
955
Amith Yamasanie4afaa32014-06-30 14:55:07 +0530956 private void writeRestrictionsLocked(XmlSerializer serializer, Bundle restrictions)
957 throws IOException {
958 serializer.startTag(null, TAG_RESTRICTIONS);
959 writeBoolean(serializer, restrictions, UserManager.DISALLOW_CONFIG_WIFI);
960 writeBoolean(serializer, restrictions, UserManager.DISALLOW_MODIFY_ACCOUNTS);
961 writeBoolean(serializer, restrictions, UserManager.DISALLOW_INSTALL_APPS);
962 writeBoolean(serializer, restrictions, UserManager.DISALLOW_UNINSTALL_APPS);
963 writeBoolean(serializer, restrictions, UserManager.DISALLOW_SHARE_LOCATION);
964 writeBoolean(serializer, restrictions,
965 UserManager.DISALLOW_INSTALL_UNKNOWN_SOURCES);
966 writeBoolean(serializer, restrictions, UserManager.DISALLOW_CONFIG_BLUETOOTH);
967 writeBoolean(serializer, restrictions, UserManager.DISALLOW_USB_FILE_TRANSFER);
968 writeBoolean(serializer, restrictions, UserManager.DISALLOW_CONFIG_CREDENTIALS);
969 writeBoolean(serializer, restrictions, UserManager.DISALLOW_REMOVE_USER);
970 writeBoolean(serializer, restrictions, UserManager.DISALLOW_DEBUGGING_FEATURES);
971 writeBoolean(serializer, restrictions, UserManager.DISALLOW_CONFIG_VPN);
972 writeBoolean(serializer, restrictions, UserManager.DISALLOW_CONFIG_TETHERING);
Stuart Scott94b038b2015-04-20 14:07:45 -0700973 writeBoolean(serializer, restrictions, UserManager.DISALLOW_NETWORK_RESET);
Amith Yamasanie4afaa32014-06-30 14:55:07 +0530974 writeBoolean(serializer, restrictions, UserManager.DISALLOW_FACTORY_RESET);
975 writeBoolean(serializer, restrictions, UserManager.DISALLOW_ADD_USER);
976 writeBoolean(serializer, restrictions, UserManager.ENSURE_VERIFY_APPS);
977 writeBoolean(serializer, restrictions, UserManager.DISALLOW_CONFIG_CELL_BROADCASTS);
978 writeBoolean(serializer, restrictions, UserManager.DISALLOW_CONFIG_MOBILE_NETWORKS);
979 writeBoolean(serializer, restrictions, UserManager.DISALLOW_APPS_CONTROL);
980 writeBoolean(serializer, restrictions, UserManager.DISALLOW_MOUNT_PHYSICAL_MEDIA);
981 writeBoolean(serializer, restrictions, UserManager.DISALLOW_UNMUTE_MICROPHONE);
982 writeBoolean(serializer, restrictions, UserManager.DISALLOW_ADJUST_VOLUME);
Amith Yamasani390989d2014-07-17 10:52:03 -0700983 writeBoolean(serializer, restrictions, UserManager.DISALLOW_OUTGOING_CALLS);
984 writeBoolean(serializer, restrictions, UserManager.DISALLOW_SMS);
Jeff Sharkey2cc03e52015-03-20 11:24:04 -0700985 writeBoolean(serializer, restrictions, UserManager.DISALLOW_FUN);
Jason Monk1c7c3192014-06-26 12:52:18 -0400986 writeBoolean(serializer, restrictions, UserManager.DISALLOW_CREATE_WINDOWS);
Amith Yamasani26af8292014-09-09 09:57:27 -0700987 writeBoolean(serializer, restrictions, UserManager.DISALLOW_CROSS_PROFILE_COPY_PASTE);
988 writeBoolean(serializer, restrictions, UserManager.DISALLOW_OUTGOING_BEAM);
Benjamin Franzf3ece362015-02-11 10:51:10 +0000989 writeBoolean(serializer, restrictions, UserManager.DISALLOW_WALLPAPER);
Benjamin Franzbff46ba2015-03-05 18:33:51 +0000990 writeBoolean(serializer, restrictions, UserManager.DISALLOW_SAFE_BOOT);
Nicolas Prevot9edbda12015-06-17 11:09:48 -0700991 writeBoolean(serializer, restrictions, UserManager.ALLOW_PARENT_APP_LINKING);
Amith Yamasanie4afaa32014-06-30 14:55:07 +0530992 serializer.endTag(null, TAG_RESTRICTIONS);
993 }
994
Amith Yamasanie4cf7342012-12-17 11:12:09 -0800995 private UserInfo readUserLocked(int id) {
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700996 int flags = 0;
Amith Yamasani2a003292012-08-14 18:25:45 -0700997 int serialNumber = id;
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700998 String name = null;
Amith Yamasanib8151ec2012-04-18 18:02:48 -0700999 String iconPath = null;
Amith Yamasani920ace02012-09-20 22:15:37 -07001000 long creationTime = 0L;
1001 long lastLoggedInTime = 0L;
Kenny Guy2a764942014-04-02 13:29:20 +01001002 int profileGroupId = UserInfo.NO_PROFILE_GROUP_ID;
Dianne Hackbornd4ac8d72012-09-27 23:20:10 -07001003 boolean partial = false;
Adam Lesinskieddeb492014-09-08 17:50:03 -07001004 boolean guestToRemove = false;
Amith Yamasanie4cf7342012-12-17 11:12:09 -08001005 Bundle restrictions = new Bundle();
Amith Yamasani4b2e9342011-03-31 12:38:53 -07001006
1007 FileInputStream fis = null;
1008 try {
Amith Yamasani2a003292012-08-14 18:25:45 -07001009 AtomicFile userFile =
Amith Yamasanifc95e702013-09-26 13:20:17 -07001010 new AtomicFile(new File(mUsersDir, Integer.toString(id) + XML_SUFFIX));
Amith Yamasani2a003292012-08-14 18:25:45 -07001011 fis = userFile.openRead();
Amith Yamasani4b2e9342011-03-31 12:38:53 -07001012 XmlPullParser parser = Xml.newPullParser();
Wojciech Staszkiewicz9e9e2e72015-05-08 14:58:46 +01001013 parser.setInput(fis, StandardCharsets.UTF_8.name());
Amith Yamasani4b2e9342011-03-31 12:38:53 -07001014 int type;
1015 while ((type = parser.next()) != XmlPullParser.START_TAG
1016 && type != XmlPullParser.END_DOCUMENT) {
1017 ;
1018 }
1019
1020 if (type != XmlPullParser.START_TAG) {
Amith Yamasani0b285492011-04-14 17:35:23 -07001021 Slog.e(LOG_TAG, "Unable to read user " + id);
Amith Yamasani4b2e9342011-03-31 12:38:53 -07001022 return null;
1023 }
1024
1025 if (type == XmlPullParser.START_TAG && parser.getName().equals(TAG_USER)) {
Amith Yamasani920ace02012-09-20 22:15:37 -07001026 int storedId = readIntAttribute(parser, ATTR_ID, -1);
1027 if (storedId != id) {
Amith Yamasani0b285492011-04-14 17:35:23 -07001028 Slog.e(LOG_TAG, "User id does not match the file name");
Amith Yamasani4b2e9342011-03-31 12:38:53 -07001029 return null;
1030 }
Amith Yamasani920ace02012-09-20 22:15:37 -07001031 serialNumber = readIntAttribute(parser, ATTR_SERIAL_NO, id);
1032 flags = readIntAttribute(parser, ATTR_FLAGS, 0);
Amith Yamasanib8151ec2012-04-18 18:02:48 -07001033 iconPath = parser.getAttributeValue(null, ATTR_ICON_PATH);
Amith Yamasani920ace02012-09-20 22:15:37 -07001034 creationTime = readLongAttribute(parser, ATTR_CREATION_TIME, 0);
1035 lastLoggedInTime = readLongAttribute(parser, ATTR_LAST_LOGGED_IN_TIME, 0);
Kenny Guy2a764942014-04-02 13:29:20 +01001036 profileGroupId = readIntAttribute(parser, ATTR_PROFILE_GROUP_ID,
1037 UserInfo.NO_PROFILE_GROUP_ID);
Dianne Hackbornd4ac8d72012-09-27 23:20:10 -07001038 String valueString = parser.getAttributeValue(null, ATTR_PARTIAL);
1039 if ("true".equals(valueString)) {
1040 partial = true;
1041 }
Adam Lesinskieddeb492014-09-08 17:50:03 -07001042 valueString = parser.getAttributeValue(null, ATTR_GUEST_TO_REMOVE);
1043 if ("true".equals(valueString)) {
1044 guestToRemove = true;
1045 }
Amith Yamasani4b2e9342011-03-31 12:38:53 -07001046
Amith Yamasanie4cf7342012-12-17 11:12:09 -08001047 int outerDepth = parser.getDepth();
1048 while ((type = parser.next()) != XmlPullParser.END_DOCUMENT
1049 && (type != XmlPullParser.END_TAG || parser.getDepth() > outerDepth)) {
1050 if (type == XmlPullParser.END_TAG || type == XmlPullParser.TEXT) {
1051 continue;
1052 }
1053 String tag = parser.getName();
1054 if (TAG_NAME.equals(tag)) {
1055 type = parser.next();
1056 if (type == XmlPullParser.TEXT) {
1057 name = parser.getText();
1058 }
1059 } else if (TAG_RESTRICTIONS.equals(tag)) {
Amith Yamasanie4afaa32014-06-30 14:55:07 +05301060 readRestrictionsLocked(parser, restrictions);
Amith Yamasani4b2e9342011-03-31 12:38:53 -07001061 }
1062 }
1063 }
Amith Yamasani4b2e9342011-03-31 12:38:53 -07001064
Amith Yamasanib8151ec2012-04-18 18:02:48 -07001065 UserInfo userInfo = new UserInfo(id, name, iconPath, flags);
Amith Yamasani2a003292012-08-14 18:25:45 -07001066 userInfo.serialNumber = serialNumber;
Amith Yamasani920ace02012-09-20 22:15:37 -07001067 userInfo.creationTime = creationTime;
1068 userInfo.lastLoggedInTime = lastLoggedInTime;
Dianne Hackbornd4ac8d72012-09-27 23:20:10 -07001069 userInfo.partial = partial;
Adam Lesinskieddeb492014-09-08 17:50:03 -07001070 userInfo.guestToRemove = guestToRemove;
Kenny Guy2a764942014-04-02 13:29:20 +01001071 userInfo.profileGroupId = profileGroupId;
Amith Yamasanie4cf7342012-12-17 11:12:09 -08001072 mUserRestrictions.append(id, restrictions);
Amith Yamasani4b2e9342011-03-31 12:38:53 -07001073 return userInfo;
1074
1075 } catch (IOException ioe) {
1076 } catch (XmlPullParserException pe) {
Dianne Hackbornbfd89b32011-12-15 18:22:54 -08001077 } finally {
1078 if (fis != null) {
1079 try {
1080 fis.close();
1081 } catch (IOException e) {
1082 }
1083 }
Amith Yamasani4b2e9342011-03-31 12:38:53 -07001084 }
1085 return null;
1086 }
1087
Amith Yamasanie4afaa32014-06-30 14:55:07 +05301088 private void readRestrictionsLocked(XmlPullParser parser, Bundle restrictions)
1089 throws IOException {
1090 readBoolean(parser, restrictions, UserManager.DISALLOW_CONFIG_WIFI);
1091 readBoolean(parser, restrictions, UserManager.DISALLOW_MODIFY_ACCOUNTS);
1092 readBoolean(parser, restrictions, UserManager.DISALLOW_INSTALL_APPS);
1093 readBoolean(parser, restrictions, UserManager.DISALLOW_UNINSTALL_APPS);
1094 readBoolean(parser, restrictions, UserManager.DISALLOW_SHARE_LOCATION);
1095 readBoolean(parser, restrictions,
1096 UserManager.DISALLOW_INSTALL_UNKNOWN_SOURCES);
1097 readBoolean(parser, restrictions, UserManager.DISALLOW_CONFIG_BLUETOOTH);
1098 readBoolean(parser, restrictions, UserManager.DISALLOW_USB_FILE_TRANSFER);
1099 readBoolean(parser, restrictions, UserManager.DISALLOW_CONFIG_CREDENTIALS);
1100 readBoolean(parser, restrictions, UserManager.DISALLOW_REMOVE_USER);
1101 readBoolean(parser, restrictions, UserManager.DISALLOW_DEBUGGING_FEATURES);
1102 readBoolean(parser, restrictions, UserManager.DISALLOW_CONFIG_VPN);
1103 readBoolean(parser, restrictions, UserManager.DISALLOW_CONFIG_TETHERING);
Stuart Scott94b038b2015-04-20 14:07:45 -07001104 readBoolean(parser, restrictions, UserManager.DISALLOW_NETWORK_RESET);
Amith Yamasanie4afaa32014-06-30 14:55:07 +05301105 readBoolean(parser, restrictions, UserManager.DISALLOW_FACTORY_RESET);
1106 readBoolean(parser, restrictions, UserManager.DISALLOW_ADD_USER);
1107 readBoolean(parser, restrictions, UserManager.ENSURE_VERIFY_APPS);
1108 readBoolean(parser, restrictions, UserManager.DISALLOW_CONFIG_CELL_BROADCASTS);
1109 readBoolean(parser, restrictions, UserManager.DISALLOW_CONFIG_MOBILE_NETWORKS);
1110 readBoolean(parser, restrictions, UserManager.DISALLOW_APPS_CONTROL);
1111 readBoolean(parser, restrictions,
1112 UserManager.DISALLOW_MOUNT_PHYSICAL_MEDIA);
1113 readBoolean(parser, restrictions, UserManager.DISALLOW_UNMUTE_MICROPHONE);
1114 readBoolean(parser, restrictions, UserManager.DISALLOW_ADJUST_VOLUME);
Amith Yamasani390989d2014-07-17 10:52:03 -07001115 readBoolean(parser, restrictions, UserManager.DISALLOW_OUTGOING_CALLS);
1116 readBoolean(parser, restrictions, UserManager.DISALLOW_SMS);
Jeff Sharkey2cc03e52015-03-20 11:24:04 -07001117 readBoolean(parser, restrictions, UserManager.DISALLOW_FUN);
Jason Monk1c7c3192014-06-26 12:52:18 -04001118 readBoolean(parser, restrictions, UserManager.DISALLOW_CREATE_WINDOWS);
Amith Yamasani26af8292014-09-09 09:57:27 -07001119 readBoolean(parser, restrictions, UserManager.DISALLOW_CROSS_PROFILE_COPY_PASTE);
1120 readBoolean(parser, restrictions, UserManager.DISALLOW_OUTGOING_BEAM);
Benjamin Franzf3ece362015-02-11 10:51:10 +00001121 readBoolean(parser, restrictions, UserManager.DISALLOW_WALLPAPER);
Benjamin Franzbff46ba2015-03-05 18:33:51 +00001122 readBoolean(parser, restrictions, UserManager.DISALLOW_SAFE_BOOT);
Nicolas Prevot9edbda12015-06-17 11:09:48 -07001123 readBoolean(parser, restrictions, UserManager.ALLOW_PARENT_APP_LINKING);
Amith Yamasanie4afaa32014-06-30 14:55:07 +05301124 }
1125
Amith Yamasanie4cf7342012-12-17 11:12:09 -08001126 private void readBoolean(XmlPullParser parser, Bundle restrictions,
1127 String restrictionKey) {
1128 String value = parser.getAttributeValue(null, restrictionKey);
Amith Yamasani71e6c692013-03-24 17:39:28 -07001129 if (value != null) {
1130 restrictions.putBoolean(restrictionKey, Boolean.parseBoolean(value));
1131 }
Amith Yamasanie4cf7342012-12-17 11:12:09 -08001132 }
1133
1134 private void writeBoolean(XmlSerializer xml, Bundle restrictions, String restrictionKey)
1135 throws IOException {
1136 if (restrictions.containsKey(restrictionKey)) {
1137 xml.attribute(null, restrictionKey,
1138 Boolean.toString(restrictions.getBoolean(restrictionKey)));
1139 }
1140 }
1141
Amith Yamasani920ace02012-09-20 22:15:37 -07001142 private int readIntAttribute(XmlPullParser parser, String attr, int defaultValue) {
1143 String valueString = parser.getAttributeValue(null, attr);
1144 if (valueString == null) return defaultValue;
1145 try {
1146 return Integer.parseInt(valueString);
1147 } catch (NumberFormatException nfe) {
1148 return defaultValue;
1149 }
1150 }
1151
1152 private long readLongAttribute(XmlPullParser parser, String attr, long defaultValue) {
1153 String valueString = parser.getAttributeValue(null, attr);
1154 if (valueString == null) return defaultValue;
1155 try {
1156 return Long.parseLong(valueString);
1157 } catch (NumberFormatException nfe) {
1158 return defaultValue;
1159 }
1160 }
1161
Amith Yamasani1a7472e2013-07-02 11:17:30 -07001162 private boolean isPackageInstalled(String pkg, int userId) {
1163 final ApplicationInfo info = mPm.getApplicationInfo(pkg,
1164 PackageManager.GET_UNINSTALLED_PACKAGES,
1165 userId);
1166 if (info == null || (info.flags&ApplicationInfo.FLAG_INSTALLED) == 0) {
1167 return false;
1168 }
1169 return true;
1170 }
1171
Amith Yamasanib82add22013-07-09 11:24:44 -07001172 /**
Kenny Guyd21b2182014-07-17 16:38:55 +01001173 * Removes all the restrictions files (res_<packagename>) for a given user.
Amith Yamasanib82add22013-07-09 11:24:44 -07001174 * Does not do any permissions checking.
1175 */
Kenny Guyd21b2182014-07-17 16:38:55 +01001176 private void cleanAppRestrictions(int userId) {
Amith Yamasanib82add22013-07-09 11:24:44 -07001177 synchronized (mPackagesLock) {
1178 File dir = Environment.getUserSystemDirectory(userId);
1179 String[] files = dir.list();
1180 if (files == null) return;
1181 for (String fileName : files) {
1182 if (fileName.startsWith(RESTRICTIONS_FILE_PREFIX)) {
1183 File resFile = new File(dir, fileName);
1184 if (resFile.exists()) {
Kenny Guyd21b2182014-07-17 16:38:55 +01001185 resFile.delete();
Amith Yamasanib82add22013-07-09 11:24:44 -07001186 }
1187 }
1188 }
1189 }
1190 }
1191
Amith Yamasani1a7472e2013-07-02 11:17:30 -07001192 /**
1193 * Removes the app restrictions file for a specific package and user id, if it exists.
1194 */
1195 private void cleanAppRestrictionsForPackage(String pkg, int userId) {
1196 synchronized (mPackagesLock) {
1197 File dir = Environment.getUserSystemDirectory(userId);
Amith Yamasanifc95e702013-09-26 13:20:17 -07001198 File resFile = new File(dir, packageToRestrictionsFileName(pkg));
Amith Yamasani1a7472e2013-07-02 11:17:30 -07001199 if (resFile.exists()) {
1200 resFile.delete();
1201 }
1202 }
1203 }
1204
Kenny Guya52dc3e2014-02-11 15:33:14 +00001205 @Override
Kenny Guy2a764942014-04-02 13:29:20 +01001206 public UserInfo createProfileForUser(String name, int flags, int userId) {
Kenny Guya52dc3e2014-02-11 15:33:14 +00001207 checkManageUsersPermission("Only the system can create users");
Kenny Guy2a764942014-04-02 13:29:20 +01001208 if (userId != UserHandle.USER_OWNER) {
1209 Slog.w(LOG_TAG, "Only user owner can have profiles");
Kenny Guya52dc3e2014-02-11 15:33:14 +00001210 return null;
1211 }
Kenny Guy2a764942014-04-02 13:29:20 +01001212 return createUserInternal(name, flags, userId);
Kenny Guya52dc3e2014-02-11 15:33:14 +00001213 }
1214
Amith Yamasani258848d2012-08-10 17:06:33 -07001215 @Override
Amith Yamasani13593602012-03-22 16:16:17 -07001216 public UserInfo createUser(String name, int flags) {
Amith Yamasani2a003292012-08-14 18:25:45 -07001217 checkManageUsersPermission("Only the system can create users");
Nicolas Prevotc6d033e2014-02-27 13:11:09 +00001218 return createUserInternal(name, flags, UserHandle.USER_NULL);
Kenny Guya52dc3e2014-02-11 15:33:14 +00001219 }
Amith Yamasanifaea76f2012-09-11 10:59:48 -07001220
Jessica Hummelbe81c802014-04-22 15:49:22 +01001221 private UserInfo createUserInternal(String name, int flags, int parentId) {
Julia Reynolds75175022014-06-26 16:35:00 -04001222 if (getUserRestrictions(UserHandle.getCallingUserId()).getBoolean(
1223 UserManager.DISALLOW_ADD_USER, false)) {
1224 Log.w(LOG_TAG, "Cannot add user. DISALLOW_ADD_USER is enabled.");
1225 return null;
1226 }
Nicolas Prevot12678a92015-05-13 12:15:03 -07001227 if (ActivityManager.isLowRamDeviceStatic()) {
1228 return null;
1229 }
Amith Yamasani95ab7842014-08-11 17:09:26 -07001230 final boolean isGuest = (flags & UserInfo.FLAG_GUEST) != 0;
Nicolas Prevot12678a92015-05-13 12:15:03 -07001231 final boolean isManagedProfile = (flags & UserInfo.FLAG_MANAGED_PROFILE) != 0;
Dianne Hackborn5dc5a002012-09-15 19:33:48 -07001232 final long ident = Binder.clearCallingIdentity();
Nicolas Prevotc6d033e2014-02-27 13:11:09 +00001233 UserInfo userInfo = null;
Dianne Hackborn5dc5a002012-09-15 19:33:48 -07001234 try {
1235 synchronized (mInstallLock) {
1236 synchronized (mPackagesLock) {
Jessica Hummelbe81c802014-04-22 15:49:22 +01001237 UserInfo parent = null;
1238 if (parentId != UserHandle.USER_NULL) {
1239 parent = getUserInfoLocked(parentId);
1240 if (parent == null) return null;
Nicolas Prevotc6d033e2014-02-27 13:11:09 +00001241 }
Nicolas Prevot12678a92015-05-13 12:15:03 -07001242 if (isManagedProfile && !canAddMoreManagedProfiles()) {
1243 return null;
1244 }
1245 if (!isGuest && !isManagedProfile && isUserLimitReachedLocked()) {
1246 // If we're not adding a guest user or a managed profile and the limit has
1247 // been reached, cannot add a user.
Amith Yamasani95ab7842014-08-11 17:09:26 -07001248 return null;
1249 }
1250 // If we're adding a guest and there already exists one, bail.
Adam Lesinskieddeb492014-09-08 17:50:03 -07001251 if (isGuest && findCurrentGuestUserLocked() != null) {
Amith Yamasani95ab7842014-08-11 17:09:26 -07001252 return null;
1253 }
Dianne Hackborn5dc5a002012-09-15 19:33:48 -07001254 int userId = getNextAvailableIdLocked();
1255 userInfo = new UserInfo(userId, name, null, flags);
1256 File userPath = new File(mBaseUserPath, Integer.toString(userId));
1257 userInfo.serialNumber = mNextSerialNumber++;
Amith Yamasani920ace02012-09-20 22:15:37 -07001258 long now = System.currentTimeMillis();
1259 userInfo.creationTime = (now > EPOCH_PLUS_30_YEARS) ? now : 0;
Dianne Hackbornd4ac8d72012-09-27 23:20:10 -07001260 userInfo.partial = true;
Amith Yamasani16389312012-10-17 21:20:14 -07001261 Environment.getUserSystemDirectory(userInfo.id).mkdirs();
Dianne Hackborn5dc5a002012-09-15 19:33:48 -07001262 mUsers.put(userId, userInfo);
1263 writeUserListLocked();
Jessica Hummelbe81c802014-04-22 15:49:22 +01001264 if (parent != null) {
1265 if (parent.profileGroupId == UserInfo.NO_PROFILE_GROUP_ID) {
1266 parent.profileGroupId = parent.id;
Fyodor Kupolov75d0ea82014-12-15 16:21:07 -08001267 scheduleWriteUserLocked(parent);
Kenny Guya52dc3e2014-02-11 15:33:14 +00001268 }
Jessica Hummelbe81c802014-04-22 15:49:22 +01001269 userInfo.profileGroupId = parent.profileGroupId;
Kenny Guya52dc3e2014-02-11 15:33:14 +00001270 }
Dianne Hackborn5dc5a002012-09-15 19:33:48 -07001271 mPm.createNewUserLILPw(userId, userPath);
Dianne Hackbornd4ac8d72012-09-27 23:20:10 -07001272 userInfo.partial = false;
Fyodor Kupolov75d0ea82014-12-15 16:21:07 -08001273 scheduleWriteUserLocked(userInfo);
Dianne Hackbornd4ac8d72012-09-27 23:20:10 -07001274 updateUserIdsLocked();
Geoffrey Borggaarde45e45e32013-01-24 10:03:20 -05001275 Bundle restrictions = new Bundle();
Geoffrey Borggaarde45e45e32013-01-24 10:03:20 -05001276 mUserRestrictions.append(userId, restrictions);
Svet Ganov78027f32015-03-25 09:10:09 -07001277 mPm.newUserCreatedLILPw(userId);
Dianne Hackborn5dc5a002012-09-15 19:33:48 -07001278 }
Dianne Hackborn4428e172012-08-24 17:43:05 -07001279 }
Dianne Hackborn5dc5a002012-09-15 19:33:48 -07001280 if (userInfo != null) {
1281 Intent addedIntent = new Intent(Intent.ACTION_USER_ADDED);
1282 addedIntent.putExtra(Intent.EXTRA_USER_HANDLE, userInfo.id);
1283 mContext.sendBroadcastAsUser(addedIntent, UserHandle.ALL,
1284 android.Manifest.permission.MANAGE_USERS);
1285 }
1286 } finally {
1287 Binder.restoreCallingIdentity(ident);
Amith Yamasani258848d2012-08-10 17:06:33 -07001288 }
Amith Yamasani4b2e9342011-03-31 12:38:53 -07001289 return userInfo;
1290 }
1291
Amith Yamasani95ab7842014-08-11 17:09:26 -07001292 private int numberOfUsersOfTypeLocked(int flags, boolean excludeDying) {
1293 int count = 0;
1294 for (int i = mUsers.size() - 1; i >= 0; i--) {
1295 UserInfo user = mUsers.valueAt(i);
1296 if (!excludeDying || !mRemovingUserIds.get(user.id)) {
1297 if ((user.flags & flags) != 0) {
1298 count++;
1299 }
1300 }
1301 }
1302 return count;
1303 }
1304
Amith Yamasani0b285492011-04-14 17:35:23 -07001305 /**
Adam Lesinskieddeb492014-09-08 17:50:03 -07001306 * Find the current guest user. If the Guest user is partial,
1307 * then do not include it in the results as it is about to die.
1308 * This is different than {@link #numberOfUsersOfTypeLocked(int, boolean)} due to
1309 * the special handling of Guests being removed.
1310 */
1311 private UserInfo findCurrentGuestUserLocked() {
1312 final int size = mUsers.size();
1313 for (int i = 0; i < size; i++) {
1314 final UserInfo user = mUsers.valueAt(i);
1315 if (user.isGuest() && !user.guestToRemove && !mRemovingUserIds.get(user.id)) {
1316 return user;
1317 }
1318 }
1319 return null;
1320 }
1321
1322 /**
Amith Yamasani1df14732014-08-29 21:37:27 -07001323 * Mark this guest user for deletion to allow us to create another guest
1324 * and switch to that user before actually removing this guest.
1325 * @param userHandle the userid of the current guest
1326 * @return whether the user could be marked for deletion
1327 */
1328 public boolean markGuestForDeletion(int userHandle) {
1329 checkManageUsersPermission("Only the system can remove users");
1330 if (getUserRestrictions(UserHandle.getCallingUserId()).getBoolean(
1331 UserManager.DISALLOW_REMOVE_USER, false)) {
1332 Log.w(LOG_TAG, "Cannot remove user. DISALLOW_REMOVE_USER is enabled.");
1333 return false;
1334 }
1335
1336 long ident = Binder.clearCallingIdentity();
1337 try {
1338 final UserInfo user;
1339 synchronized (mPackagesLock) {
1340 user = mUsers.get(userHandle);
1341 if (userHandle == 0 || user == null || mRemovingUserIds.get(userHandle)) {
1342 return false;
1343 }
1344 if (!user.isGuest()) {
1345 return false;
1346 }
Adam Lesinskieddeb492014-09-08 17:50:03 -07001347 // We set this to a guest user that is to be removed. This is a temporary state
1348 // where we are allowed to add new Guest users, even if this one is still not
1349 // removed. This user will still show up in getUserInfo() calls.
1350 // If we don't get around to removing this Guest user, it will be purged on next
1351 // startup.
1352 user.guestToRemove = true;
Amith Yamasani1df14732014-08-29 21:37:27 -07001353 // Mark it as disabled, so that it isn't returned any more when
1354 // profiles are queried.
1355 user.flags |= UserInfo.FLAG_DISABLED;
Amith Yamasani1df14732014-08-29 21:37:27 -07001356 writeUserLocked(user);
1357 }
1358 } finally {
1359 Binder.restoreCallingIdentity(ident);
1360 }
1361 return true;
1362 }
1363
1364 /**
Amith Yamasani0b285492011-04-14 17:35:23 -07001365 * Removes a user and all data directories created for that user. This method should be called
1366 * after the user's processes have been terminated.
Dianne Hackborn10ad9822014-03-17 11:28:36 -07001367 * @param userHandle the user's id
Amith Yamasani0b285492011-04-14 17:35:23 -07001368 */
Amith Yamasani258848d2012-08-10 17:06:33 -07001369 public boolean removeUser(int userHandle) {
Amith Yamasani2a003292012-08-14 18:25:45 -07001370 checkManageUsersPermission("Only the system can remove users");
Julia Reynolds4ac5f852014-06-23 17:38:51 -04001371 if (getUserRestrictions(UserHandle.getCallingUserId()).getBoolean(
1372 UserManager.DISALLOW_REMOVE_USER, false)) {
1373 Log.w(LOG_TAG, "Cannot remove user. DISALLOW_REMOVE_USER is enabled.");
1374 return false;
1375 }
1376
Kenny Guyee58b4f2014-05-23 15:19:53 +01001377 long ident = Binder.clearCallingIdentity();
1378 try {
1379 final UserInfo user;
1380 synchronized (mPackagesLock) {
1381 user = mUsers.get(userHandle);
Kenny Guy17c9d692014-06-13 13:51:42 +01001382 if (userHandle == 0 || user == null || mRemovingUserIds.get(userHandle)) {
Kenny Guyee58b4f2014-05-23 15:19:53 +01001383 return false;
1384 }
Jeff Sharkey6eb09392014-11-14 15:57:59 -08001385
1386 // We remember deleted user IDs to prevent them from being
1387 // reused during the current boot; they can still be reused
1388 // after a reboot.
Kenny Guyee58b4f2014-05-23 15:19:53 +01001389 mRemovingUserIds.put(userHandle, true);
Jeff Sharkey6eb09392014-11-14 15:57:59 -08001390
Kenny Guyee58b4f2014-05-23 15:19:53 +01001391 try {
1392 mAppOpsService.removeUser(userHandle);
1393 } catch (RemoteException e) {
1394 Log.w(LOG_TAG, "Unable to notify AppOpsService of removing user", e);
1395 }
1396 // Set this to a partially created user, so that the user will be purged
1397 // on next startup, in case the runtime stops now before stopping and
1398 // removing the user completely.
1399 user.partial = true;
1400 // Mark it as disabled, so that it isn't returned any more when
1401 // profiles are queried.
1402 user.flags |= UserInfo.FLAG_DISABLED;
1403 writeUserLocked(user);
1404 }
1405
1406 if (user.profileGroupId != UserInfo.NO_PROFILE_GROUP_ID
1407 && user.isManagedProfile()) {
1408 // Send broadcast to notify system that the user removed was a
1409 // managed user.
1410 sendProfileRemovedBroadcast(user.profileGroupId, user.id);
1411 }
1412
1413 if (DBG) Slog.i(LOG_TAG, "Stopping user " + userHandle);
1414 int res;
1415 try {
1416 res = ActivityManagerNative.getDefault().stopUser(userHandle,
1417 new IStopUserCallback.Stub() {
1418 @Override
1419 public void userStopped(int userId) {
1420 finishRemoveUser(userId);
1421 }
1422 @Override
1423 public void userStopAborted(int userId) {
1424 }
1425 });
1426 } catch (RemoteException e) {
Dianne Hackborn80a4af22012-08-27 19:18:31 -07001427 return false;
1428 }
Kenny Guyee58b4f2014-05-23 15:19:53 +01001429 return res == ActivityManager.USER_OP_SUCCESS;
1430 } finally {
1431 Binder.restoreCallingIdentity(ident);
Dianne Hackborn80a4af22012-08-27 19:18:31 -07001432 }
Dianne Hackborn80a4af22012-08-27 19:18:31 -07001433 }
1434
Amith Yamasanidb6a14c2012-10-17 21:16:52 -07001435 void finishRemoveUser(final int userHandle) {
Amith Yamasani16389312012-10-17 21:20:14 -07001436 if (DBG) Slog.i(LOG_TAG, "finishRemoveUser " + userHandle);
Amith Yamasanidb6a14c2012-10-17 21:16:52 -07001437 // Let other services shutdown any activity and clean up their state before completely
1438 // wiping the user's system directory and removing from the user list
Dianne Hackborn5dc5a002012-09-15 19:33:48 -07001439 long ident = Binder.clearCallingIdentity();
1440 try {
1441 Intent addedIntent = new Intent(Intent.ACTION_USER_REMOVED);
1442 addedIntent.putExtra(Intent.EXTRA_USER_HANDLE, userHandle);
Amith Yamasanidb6a14c2012-10-17 21:16:52 -07001443 mContext.sendOrderedBroadcastAsUser(addedIntent, UserHandle.ALL,
1444 android.Manifest.permission.MANAGE_USERS,
1445
1446 new BroadcastReceiver() {
1447 @Override
1448 public void onReceive(Context context, Intent intent) {
1449 if (DBG) {
1450 Slog.i(LOG_TAG,
1451 "USER_REMOVED broadcast sent, cleaning up user data "
1452 + userHandle);
1453 }
1454 new Thread() {
1455 public void run() {
1456 synchronized (mInstallLock) {
1457 synchronized (mPackagesLock) {
1458 removeUserStateLocked(userHandle);
1459 }
1460 }
1461 }
1462 }.start();
1463 }
1464 },
1465
1466 null, Activity.RESULT_OK, null, null);
Dianne Hackborn5dc5a002012-09-15 19:33:48 -07001467 } finally {
1468 Binder.restoreCallingIdentity(ident);
1469 }
Amith Yamasani2a003292012-08-14 18:25:45 -07001470 }
1471
Jeff Sharkeyffe0cb42012-11-05 17:24:43 -08001472 private void removeUserStateLocked(final int userHandle) {
Paul Crowley85e4e812015-05-19 12:42:00 +01001473 mContext.getSystemService(StorageManager.class)
1474 .deleteUserKey(userHandle);
Dianne Hackbornd4ac8d72012-09-27 23:20:10 -07001475 // Cleanup package manager settings
Amith Yamasanidda003f2014-08-28 18:06:51 -07001476 mPm.cleanUpUserLILPw(this, userHandle);
Dianne Hackbornd4ac8d72012-09-27 23:20:10 -07001477
1478 // Remove this user from the list
1479 mUsers.remove(userHandle);
Dianne Hackbornd4ac8d72012-09-27 23:20:10 -07001480 // Remove user file
Amith Yamasanifc95e702013-09-26 13:20:17 -07001481 AtomicFile userFile = new AtomicFile(new File(mUsersDir, userHandle + XML_SUFFIX));
Dianne Hackbornd4ac8d72012-09-27 23:20:10 -07001482 userFile.delete();
1483 // Update the user list
1484 writeUserListLocked();
1485 updateUserIdsLocked();
1486 removeDirectoryRecursive(Environment.getUserSystemDirectory(userHandle));
1487 }
1488
Amith Yamasani61f57372012-08-31 12:12:28 -07001489 private void removeDirectoryRecursive(File parent) {
1490 if (parent.isDirectory()) {
1491 String[] files = parent.list();
1492 for (String filename : files) {
1493 File child = new File(parent, filename);
1494 removeDirectoryRecursive(child);
1495 }
1496 }
1497 parent.delete();
1498 }
1499
Kenny Guyf8d3a232014-05-15 16:09:52 +01001500 private void sendProfileRemovedBroadcast(int parentUserId, int removedUserId) {
Adam Connors7b66ed52014-04-14 11:58:10 +01001501 Intent managedProfileIntent = new Intent(Intent.ACTION_MANAGED_PROFILE_REMOVED);
Adam Connorsd4b584e2014-06-09 13:55:47 +01001502 managedProfileIntent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY |
1503 Intent.FLAG_RECEIVER_FOREGROUND);
Kenny Guyf8d3a232014-05-15 16:09:52 +01001504 managedProfileIntent.putExtra(Intent.EXTRA_USER, new UserHandle(removedUserId));
1505 mContext.sendBroadcastAsUser(managedProfileIntent, new UserHandle(parentUserId), null);
Adam Connors7b66ed52014-04-14 11:58:10 +01001506 }
1507
Amith Yamasani2a003292012-08-14 18:25:45 -07001508 @Override
Amith Yamasani7e99bc02013-04-16 18:24:51 -07001509 public Bundle getApplicationRestrictions(String packageName) {
1510 return getApplicationRestrictionsForUser(packageName, UserHandle.getCallingUserId());
1511 }
1512
1513 @Override
1514 public Bundle getApplicationRestrictionsForUser(String packageName, int userId) {
Amith Yamasanidf2e92a2013-03-01 17:04:38 -08001515 if (UserHandle.getCallingUserId() != userId
Amith Yamasani9429afb2013-04-10 18:40:51 -07001516 || !UserHandle.isSameApp(Binder.getCallingUid(), getUidForPackage(packageName))) {
Amith Yamasanibe465322014-04-24 13:45:17 -07001517 checkManageUsersPermission("Only system can get restrictions for other users/apps");
Amith Yamasanidf2e92a2013-03-01 17:04:38 -08001518 }
1519 synchronized (mPackagesLock) {
1520 // Read the restrictions from XML
1521 return readApplicationRestrictionsLocked(packageName, userId);
1522 }
1523 }
1524
1525 @Override
Amith Yamasani7e99bc02013-04-16 18:24:51 -07001526 public void setApplicationRestrictions(String packageName, Bundle restrictions,
Amith Yamasanidf2e92a2013-03-01 17:04:38 -08001527 int userId) {
1528 if (UserHandle.getCallingUserId() != userId
Amith Yamasani9429afb2013-04-10 18:40:51 -07001529 || !UserHandle.isSameApp(Binder.getCallingUid(), getUidForPackage(packageName))) {
Amith Yamasanibe465322014-04-24 13:45:17 -07001530 checkManageUsersPermission("Only system can set restrictions for other users/apps");
Amith Yamasanidf2e92a2013-03-01 17:04:38 -08001531 }
1532 synchronized (mPackagesLock) {
Kenny Guyd21b2182014-07-17 16:38:55 +01001533 if (restrictions == null || restrictions.isEmpty()) {
1534 cleanAppRestrictionsForPackage(packageName, userId);
1535 } else {
1536 // Write the restrictions to XML
1537 writeApplicationRestrictionsLocked(packageName, restrictions, userId);
1538 }
Amith Yamasanidf2e92a2013-03-01 17:04:38 -08001539 }
Robin Lee66e5d962014-04-09 16:44:21 +01001540
Kenny Guyd21b2182014-07-17 16:38:55 +01001541 if (isPackageInstalled(packageName, userId)) {
1542 // Notify package of changes via an intent - only sent to explicitly registered receivers.
1543 Intent changeIntent = new Intent(Intent.ACTION_APPLICATION_RESTRICTIONS_CHANGED);
1544 changeIntent.setPackage(packageName);
1545 changeIntent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY);
1546 mContext.sendBroadcastAsUser(changeIntent, new UserHandle(userId));
1547 }
Amith Yamasanidf2e92a2013-03-01 17:04:38 -08001548 }
1549
Amith Yamasani655d0e22013-06-12 14:19:10 -07001550 @Override
Amith Yamasani1a7472e2013-07-02 11:17:30 -07001551 public void removeRestrictions() {
Amith Yamasanibe465322014-04-24 13:45:17 -07001552 checkManageUsersPermission("Only system can remove restrictions");
Amith Yamasani1a7472e2013-07-02 11:17:30 -07001553 final int userHandle = UserHandle.getCallingUserId();
Amith Yamasani5e486f52013-08-07 11:06:44 -07001554 removeRestrictionsForUser(userHandle, true);
Amith Yamasani350962c2013-08-06 11:18:53 -07001555 }
1556
Amith Yamasanie5bcff62014-07-19 15:44:09 -07001557 private void removeRestrictionsForUser(final int userHandle, boolean unhideApps) {
Amith Yamasani1a7472e2013-07-02 11:17:30 -07001558 synchronized (mPackagesLock) {
1559 // Remove all user restrictions
1560 setUserRestrictions(new Bundle(), userHandle);
Amith Yamasani1a7472e2013-07-02 11:17:30 -07001561 // Remove any app restrictions
Kenny Guyd21b2182014-07-17 16:38:55 +01001562 cleanAppRestrictions(userHandle);
Amith Yamasani1a7472e2013-07-02 11:17:30 -07001563 }
Amith Yamasanie5bcff62014-07-19 15:44:09 -07001564 if (unhideApps) {
1565 unhideAllInstalledAppsForUser(userHandle);
Amith Yamasani5e486f52013-08-07 11:06:44 -07001566 }
1567 }
1568
Amith Yamasanie5bcff62014-07-19 15:44:09 -07001569 private void unhideAllInstalledAppsForUser(final int userHandle) {
Amith Yamasani1a7472e2013-07-02 11:17:30 -07001570 mHandler.post(new Runnable() {
1571 @Override
1572 public void run() {
1573 List<ApplicationInfo> apps =
1574 mPm.getInstalledApplications(PackageManager.GET_UNINSTALLED_PACKAGES,
1575 userHandle).getList();
1576 final long ident = Binder.clearCallingIdentity();
1577 try {
1578 for (ApplicationInfo appInfo : apps) {
1579 if ((appInfo.flags & ApplicationInfo.FLAG_INSTALLED) != 0
Alex Klyubinb9f8a522015-02-03 11:12:59 -08001580 && (appInfo.privateFlags & ApplicationInfo.PRIVATE_FLAG_HIDDEN)
1581 != 0) {
Amith Yamasanie5bcff62014-07-19 15:44:09 -07001582 mPm.setApplicationHiddenSettingAsUser(appInfo.packageName, false,
Amith Yamasani1a7472e2013-07-02 11:17:30 -07001583 userHandle);
1584 }
1585 }
1586 } finally {
1587 Binder.restoreCallingIdentity(ident);
1588 }
1589 }
1590 });
1591 }
Amith Yamasanidf2e92a2013-03-01 17:04:38 -08001592 private int getUidForPackage(String packageName) {
Amith Yamasani9429afb2013-04-10 18:40:51 -07001593 long ident = Binder.clearCallingIdentity();
Amith Yamasanidf2e92a2013-03-01 17:04:38 -08001594 try {
1595 return mContext.getPackageManager().getApplicationInfo(packageName,
1596 PackageManager.GET_UNINSTALLED_PACKAGES).uid;
1597 } catch (NameNotFoundException nnfe) {
1598 return -1;
Amith Yamasani9429afb2013-04-10 18:40:51 -07001599 } finally {
1600 Binder.restoreCallingIdentity(ident);
Amith Yamasanidf2e92a2013-03-01 17:04:38 -08001601 }
1602 }
1603
Amith Yamasani7e99bc02013-04-16 18:24:51 -07001604 private Bundle readApplicationRestrictionsLocked(String packageName,
Amith Yamasanidf2e92a2013-03-01 17:04:38 -08001605 int userId) {
Fyodor Kupolov262f9952015-03-23 18:55:11 -07001606 AtomicFile restrictionsFile =
1607 new AtomicFile(new File(Environment.getUserSystemDirectory(userId),
1608 packageToRestrictionsFileName(packageName)));
1609 return readApplicationRestrictionsLocked(restrictionsFile);
1610 }
1611
1612 @VisibleForTesting
1613 static Bundle readApplicationRestrictionsLocked(AtomicFile restrictionsFile) {
Amith Yamasani7e99bc02013-04-16 18:24:51 -07001614 final Bundle restrictions = new Bundle();
Fyodor Kupolov262f9952015-03-23 18:55:11 -07001615 final ArrayList<String> values = new ArrayList<>();
Fyodor Kupolov6f34d362015-04-02 12:42:13 -07001616 if (!restrictionsFile.getBaseFile().exists()) {
Fyodor Kupolovf6ee2242015-04-06 10:15:07 -07001617 return restrictions;
Fyodor Kupolov6f34d362015-04-02 12:42:13 -07001618 }
Amith Yamasanidf2e92a2013-03-01 17:04:38 -08001619
1620 FileInputStream fis = null;
1621 try {
Amith Yamasanidf2e92a2013-03-01 17:04:38 -08001622 fis = restrictionsFile.openRead();
1623 XmlPullParser parser = Xml.newPullParser();
Wojciech Staszkiewicz9e9e2e72015-05-08 14:58:46 +01001624 parser.setInput(fis, StandardCharsets.UTF_8.name());
Fyodor Kupolov262f9952015-03-23 18:55:11 -07001625 XmlUtils.nextElement(parser);
1626 if (parser.getEventType() != XmlPullParser.START_TAG) {
Amith Yamasanidf2e92a2013-03-01 17:04:38 -08001627 Slog.e(LOG_TAG, "Unable to read restrictions file "
1628 + restrictionsFile.getBaseFile());
Amith Yamasani7e99bc02013-04-16 18:24:51 -07001629 return restrictions;
Amith Yamasanidf2e92a2013-03-01 17:04:38 -08001630 }
Fyodor Kupolov262f9952015-03-23 18:55:11 -07001631 while (parser.next() != XmlPullParser.END_DOCUMENT) {
1632 readEntry(restrictions, values, parser);
Amith Yamasanidf2e92a2013-03-01 17:04:38 -08001633 }
Fyodor Kupolov262f9952015-03-23 18:55:11 -07001634 } catch (IOException|XmlPullParserException e) {
1635 Log.w(LOG_TAG, "Error parsing " + restrictionsFile.getBaseFile(), e);
Amith Yamasanidf2e92a2013-03-01 17:04:38 -08001636 } finally {
Fyodor Kupolov262f9952015-03-23 18:55:11 -07001637 IoUtils.closeQuietly(fis);
Amith Yamasanidf2e92a2013-03-01 17:04:38 -08001638 }
Amith Yamasani7e99bc02013-04-16 18:24:51 -07001639 return restrictions;
Amith Yamasanidf2e92a2013-03-01 17:04:38 -08001640 }
1641
Fyodor Kupolov262f9952015-03-23 18:55:11 -07001642 private static void readEntry(Bundle restrictions, ArrayList<String> values,
1643 XmlPullParser parser) throws XmlPullParserException, IOException {
1644 int type = parser.getEventType();
1645 if (type == XmlPullParser.START_TAG && parser.getName().equals(TAG_ENTRY)) {
1646 String key = parser.getAttributeValue(null, ATTR_KEY);
1647 String valType = parser.getAttributeValue(null, ATTR_VALUE_TYPE);
1648 String multiple = parser.getAttributeValue(null, ATTR_MULTIPLE);
1649 if (multiple != null) {
1650 values.clear();
1651 int count = Integer.parseInt(multiple);
1652 while (count > 0 && (type = parser.next()) != XmlPullParser.END_DOCUMENT) {
1653 if (type == XmlPullParser.START_TAG
1654 && parser.getName().equals(TAG_VALUE)) {
1655 values.add(parser.nextText().trim());
1656 count--;
1657 }
1658 }
1659 String [] valueStrings = new String[values.size()];
1660 values.toArray(valueStrings);
1661 restrictions.putStringArray(key, valueStrings);
1662 } else if (ATTR_TYPE_BUNDLE.equals(valType)) {
1663 restrictions.putBundle(key, readBundleEntry(parser, values));
1664 } else if (ATTR_TYPE_BUNDLE_ARRAY.equals(valType)) {
1665 final int outerDepth = parser.getDepth();
1666 ArrayList<Bundle> bundleList = new ArrayList<>();
1667 while (XmlUtils.nextElementWithin(parser, outerDepth)) {
1668 Bundle childBundle = readBundleEntry(parser, values);
1669 bundleList.add(childBundle);
1670 }
1671 restrictions.putParcelableArray(key,
1672 bundleList.toArray(new Bundle[bundleList.size()]));
1673 } else {
1674 String value = parser.nextText().trim();
1675 if (ATTR_TYPE_BOOLEAN.equals(valType)) {
1676 restrictions.putBoolean(key, Boolean.parseBoolean(value));
1677 } else if (ATTR_TYPE_INTEGER.equals(valType)) {
1678 restrictions.putInt(key, Integer.parseInt(value));
1679 } else {
1680 restrictions.putString(key, value);
1681 }
1682 }
1683 }
1684 }
1685
1686 private static Bundle readBundleEntry(XmlPullParser parser, ArrayList<String> values)
1687 throws IOException, XmlPullParserException {
1688 Bundle childBundle = new Bundle();
1689 final int outerDepth = parser.getDepth();
1690 while (XmlUtils.nextElementWithin(parser, outerDepth)) {
1691 readEntry(childBundle, values, parser);
1692 }
1693 return childBundle;
1694 }
1695
Amith Yamasanidf2e92a2013-03-01 17:04:38 -08001696 private void writeApplicationRestrictionsLocked(String packageName,
Amith Yamasani7e99bc02013-04-16 18:24:51 -07001697 Bundle restrictions, int userId) {
Amith Yamasanidf2e92a2013-03-01 17:04:38 -08001698 AtomicFile restrictionsFile = new AtomicFile(
1699 new File(Environment.getUserSystemDirectory(userId),
Amith Yamasanifc95e702013-09-26 13:20:17 -07001700 packageToRestrictionsFileName(packageName)));
Fyodor Kupolov262f9952015-03-23 18:55:11 -07001701 writeApplicationRestrictionsLocked(restrictions, restrictionsFile);
1702 }
1703
1704 @VisibleForTesting
1705 static void writeApplicationRestrictionsLocked(Bundle restrictions,
1706 AtomicFile restrictionsFile) {
1707 FileOutputStream fos = null;
Amith Yamasanidf2e92a2013-03-01 17:04:38 -08001708 try {
1709 fos = restrictionsFile.startWrite();
1710 final BufferedOutputStream bos = new BufferedOutputStream(fos);
1711
Amith Yamasanidf2e92a2013-03-01 17:04:38 -08001712 final XmlSerializer serializer = new FastXmlSerializer();
Wojciech Staszkiewicz9e9e2e72015-05-08 14:58:46 +01001713 serializer.setOutput(bos, StandardCharsets.UTF_8.name());
Amith Yamasanidf2e92a2013-03-01 17:04:38 -08001714 serializer.startDocument(null, true);
1715 serializer.setFeature("http://xmlpull.org/v1/doc/features.html#indent-output", true);
1716
1717 serializer.startTag(null, TAG_RESTRICTIONS);
Fyodor Kupolov262f9952015-03-23 18:55:11 -07001718 writeBundle(restrictions, serializer);
Amith Yamasanidf2e92a2013-03-01 17:04:38 -08001719 serializer.endTag(null, TAG_RESTRICTIONS);
1720
1721 serializer.endDocument();
1722 restrictionsFile.finishWrite(fos);
1723 } catch (Exception e) {
1724 restrictionsFile.failWrite(fos);
Fyodor Kupolov262f9952015-03-23 18:55:11 -07001725 Slog.e(LOG_TAG, "Error writing application restrictions list", e);
1726 }
1727 }
1728
1729 private static void writeBundle(Bundle restrictions, XmlSerializer serializer)
1730 throws IOException {
1731 for (String key : restrictions.keySet()) {
1732 Object value = restrictions.get(key);
1733 serializer.startTag(null, TAG_ENTRY);
1734 serializer.attribute(null, ATTR_KEY, key);
1735
1736 if (value instanceof Boolean) {
1737 serializer.attribute(null, ATTR_VALUE_TYPE, ATTR_TYPE_BOOLEAN);
1738 serializer.text(value.toString());
1739 } else if (value instanceof Integer) {
1740 serializer.attribute(null, ATTR_VALUE_TYPE, ATTR_TYPE_INTEGER);
1741 serializer.text(value.toString());
1742 } else if (value == null || value instanceof String) {
1743 serializer.attribute(null, ATTR_VALUE_TYPE, ATTR_TYPE_STRING);
1744 serializer.text(value != null ? (String) value : "");
1745 } else if (value instanceof Bundle) {
1746 serializer.attribute(null, ATTR_VALUE_TYPE, ATTR_TYPE_BUNDLE);
1747 writeBundle((Bundle) value, serializer);
1748 } else if (value instanceof Parcelable[]) {
1749 serializer.attribute(null, ATTR_VALUE_TYPE, ATTR_TYPE_BUNDLE_ARRAY);
1750 Parcelable[] array = (Parcelable[]) value;
1751 for (Parcelable parcelable : array) {
1752 if (!(parcelable instanceof Bundle)) {
1753 throw new IllegalArgumentException("bundle-array can only hold Bundles");
1754 }
1755 serializer.startTag(null, TAG_ENTRY);
1756 serializer.attribute(null, ATTR_VALUE_TYPE, ATTR_TYPE_BUNDLE);
1757 writeBundle((Bundle) parcelable, serializer);
1758 serializer.endTag(null, TAG_ENTRY);
1759 }
1760 } else {
1761 serializer.attribute(null, ATTR_VALUE_TYPE, ATTR_TYPE_STRING_ARRAY);
1762 String[] values = (String[]) value;
1763 serializer.attribute(null, ATTR_MULTIPLE, Integer.toString(values.length));
1764 for (String choice : values) {
1765 serializer.startTag(null, TAG_VALUE);
1766 serializer.text(choice != null ? choice : "");
1767 serializer.endTag(null, TAG_VALUE);
1768 }
1769 }
1770 serializer.endTag(null, TAG_ENTRY);
Amith Yamasanidf2e92a2013-03-01 17:04:38 -08001771 }
1772 }
1773
1774 @Override
Amith Yamasani2a003292012-08-14 18:25:45 -07001775 public int getUserSerialNumber(int userHandle) {
Dianne Hackborn4428e172012-08-24 17:43:05 -07001776 synchronized (mPackagesLock) {
Amith Yamasani2a003292012-08-14 18:25:45 -07001777 if (!exists(userHandle)) return -1;
Amith Yamasani195263742012-08-21 15:40:12 -07001778 return getUserInfoLocked(userHandle).serialNumber;
Amith Yamasani2a003292012-08-14 18:25:45 -07001779 }
1780 }
1781
1782 @Override
1783 public int getUserHandle(int userSerialNumber) {
Dianne Hackborn4428e172012-08-24 17:43:05 -07001784 synchronized (mPackagesLock) {
Amith Yamasani2a003292012-08-14 18:25:45 -07001785 for (int userId : mUserIds) {
Kenny Guy945f8832015-02-10 15:17:26 +00001786 UserInfo info = getUserInfoLocked(userId);
1787 if (info != null && info.serialNumber == userSerialNumber) return userId;
Amith Yamasani2a003292012-08-14 18:25:45 -07001788 }
1789 // Not found
1790 return -1;
Amith Yamasani13593602012-03-22 16:16:17 -07001791 }
1792 }
1793
Fyodor Kupolovff7233e2015-04-08 11:28:52 -07001794 @Override
1795 public long getUserCreationTime(int userHandle) {
1796 int callingUserId = UserHandle.getCallingUserId();
1797 UserInfo userInfo = null;
1798 synchronized (mPackagesLock) {
1799 if (callingUserId == userHandle) {
1800 userInfo = getUserInfoLocked(userHandle);
1801 } else {
1802 UserInfo parent = getProfileParentLocked(userHandle);
1803 if (parent != null && parent.id == callingUserId) {
1804 userInfo = getUserInfoLocked(userHandle);
1805 }
1806 }
1807 }
1808 if (userInfo == null) {
1809 throw new SecurityException("userHandle can only be the calling user or a managed "
1810 + "profile associated with this user");
1811 }
1812 return userInfo.creationTime;
1813 }
1814
Amith Yamasani0b285492011-04-14 17:35:23 -07001815 /**
1816 * Caches the list of user ids in an array, adjusting the array size when necessary.
1817 */
Amith Yamasani13593602012-03-22 16:16:17 -07001818 private void updateUserIdsLocked() {
Dianne Hackbornd4ac8d72012-09-27 23:20:10 -07001819 int num = 0;
Amith Yamasani0b285492011-04-14 17:35:23 -07001820 for (int i = 0; i < mUsers.size(); i++) {
Dianne Hackbornd4ac8d72012-09-27 23:20:10 -07001821 if (!mUsers.valueAt(i).partial) {
1822 num++;
1823 }
1824 }
Amith Yamasani16389312012-10-17 21:20:14 -07001825 final int[] newUsers = new int[num];
1826 int n = 0;
Dianne Hackbornd4ac8d72012-09-27 23:20:10 -07001827 for (int i = 0; i < mUsers.size(); i++) {
1828 if (!mUsers.valueAt(i).partial) {
Amith Yamasani16389312012-10-17 21:20:14 -07001829 newUsers[n++] = mUsers.keyAt(i);
Dianne Hackbornd4ac8d72012-09-27 23:20:10 -07001830 }
Amith Yamasani0b285492011-04-14 17:35:23 -07001831 }
Dianne Hackborn7767eac2012-08-23 18:25:40 -07001832 mUserIds = newUsers;
Amith Yamasani0b285492011-04-14 17:35:23 -07001833 }
1834
1835 /**
Amith Yamasani1a7472e2013-07-02 11:17:30 -07001836 * Make a note of the last started time of a user and do some cleanup.
Amith Yamasani920ace02012-09-20 22:15:37 -07001837 * @param userId the user that was just foregrounded
1838 */
Amith Yamasani06bf8242015-05-08 16:36:21 -07001839 public void onUserForeground(int userId) {
Amith Yamasani920ace02012-09-20 22:15:37 -07001840 synchronized (mPackagesLock) {
1841 UserInfo user = mUsers.get(userId);
1842 long now = System.currentTimeMillis();
Dianne Hackbornd4ac8d72012-09-27 23:20:10 -07001843 if (user == null || user.partial) {
1844 Slog.w(LOG_TAG, "userForeground: unknown user #" + userId);
1845 return;
1846 }
1847 if (now > EPOCH_PLUS_30_YEARS) {
Amith Yamasani920ace02012-09-20 22:15:37 -07001848 user.lastLoggedInTime = now;
Fyodor Kupolov75d0ea82014-12-15 16:21:07 -08001849 scheduleWriteUserLocked(user);
Amith Yamasani920ace02012-09-20 22:15:37 -07001850 }
1851 }
1852 }
1853
1854 /**
Amith Yamasani0b285492011-04-14 17:35:23 -07001855 * Returns the next available user id, filling in any holes in the ids.
Amith Yamasani742a6712011-05-04 14:49:28 -07001856 * TODO: May not be a good idea to recycle ids, in case it results in confusion
1857 * for data and battery stats collection, or unexpected cross-talk.
Amith Yamasani0b285492011-04-14 17:35:23 -07001858 * @return
1859 */
Dianne Hackborn5dc5a002012-09-15 19:33:48 -07001860 private int getNextAvailableIdLocked() {
Dianne Hackborn4428e172012-08-24 17:43:05 -07001861 synchronized (mPackagesLock) {
Jeff Sharkeyffe0cb42012-11-05 17:24:43 -08001862 int i = MIN_USER_ID;
Amith Yamasani195263742012-08-21 15:40:12 -07001863 while (i < Integer.MAX_VALUE) {
Jeff Sharkeyffe0cb42012-11-05 17:24:43 -08001864 if (mUsers.indexOfKey(i) < 0 && !mRemovingUserIds.get(i)) {
Amith Yamasani195263742012-08-21 15:40:12 -07001865 break;
1866 }
1867 i++;
Amith Yamasani4b2e9342011-03-31 12:38:53 -07001868 }
Amith Yamasani195263742012-08-21 15:40:12 -07001869 return i;
Amith Yamasani4b2e9342011-03-31 12:38:53 -07001870 }
Amith Yamasani4b2e9342011-03-31 12:38:53 -07001871 }
Amith Yamasani920ace02012-09-20 22:15:37 -07001872
Amith Yamasanifc95e702013-09-26 13:20:17 -07001873 private String packageToRestrictionsFileName(String packageName) {
1874 return RESTRICTIONS_FILE_PREFIX + packageName + XML_SUFFIX;
1875 }
1876
Amith Yamasani920ace02012-09-20 22:15:37 -07001877 @Override
1878 protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
1879 if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.DUMP)
1880 != PackageManager.PERMISSION_GRANTED) {
1881 pw.println("Permission Denial: can't dump UserManager from from pid="
1882 + Binder.getCallingPid()
1883 + ", uid=" + Binder.getCallingUid()
1884 + " without permission "
1885 + android.Manifest.permission.DUMP);
1886 return;
1887 }
1888
1889 long now = System.currentTimeMillis();
1890 StringBuilder sb = new StringBuilder();
1891 synchronized (mPackagesLock) {
1892 pw.println("Users:");
1893 for (int i = 0; i < mUsers.size(); i++) {
1894 UserInfo user = mUsers.valueAt(i);
1895 if (user == null) continue;
Amith Yamasani634cf312012-10-04 17:34:21 -07001896 pw.print(" "); pw.print(user); pw.print(" serialNo="); pw.print(user.serialNumber);
Jeff Sharkeyffe0cb42012-11-05 17:24:43 -08001897 if (mRemovingUserIds.get(mUsers.keyAt(i))) pw.print(" <removing> ");
Dianne Hackbornd4ac8d72012-09-27 23:20:10 -07001898 if (user.partial) pw.print(" <partial>");
1899 pw.println();
Amith Yamasani920ace02012-09-20 22:15:37 -07001900 pw.print(" Created: ");
1901 if (user.creationTime == 0) {
1902 pw.println("<unknown>");
1903 } else {
1904 sb.setLength(0);
1905 TimeUtils.formatDuration(now - user.creationTime, sb);
1906 sb.append(" ago");
1907 pw.println(sb);
1908 }
1909 pw.print(" Last logged in: ");
1910 if (user.lastLoggedInTime == 0) {
1911 pw.println("<unknown>");
1912 } else {
1913 sb.setLength(0);
1914 TimeUtils.formatDuration(now - user.lastLoggedInTime, sb);
1915 sb.append(" ago");
1916 pw.println(sb);
1917 }
1918 }
1919 }
1920 }
Fyodor Kupolov75d0ea82014-12-15 16:21:07 -08001921
1922 final class MainHandler extends Handler {
1923
1924 @Override
1925 public void handleMessage(Message msg) {
1926 switch (msg.what) {
1927 case WRITE_USER_MSG:
1928 removeMessages(WRITE_USER_MSG, msg.obj);
1929 synchronized (mPackagesLock) {
1930 int userId = ((UserInfo) msg.obj).id;
1931 UserInfo userInfo = mUsers.get(userId);
1932 if (userInfo != null) {
1933 writeUserLocked(userInfo);
1934 }
1935 }
1936 }
1937 }
1938 }
Amith Yamasani4b2e9342011-03-31 12:38:53 -07001939}