Amith Yamasani | 4b2e934 | 2011-03-31 12:38:53 -0700 | [diff] [blame] | 1 | /* |
| 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 | |
| 17 | package com.android.server.pm; |
| 18 | |
Amith Yamasani | b8151ec | 2012-04-18 18:02:48 -0700 | [diff] [blame] | 19 | import static android.os.ParcelFileDescriptor.MODE_CREATE; |
| 20 | import static android.os.ParcelFileDescriptor.MODE_READ_WRITE; |
| 21 | |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 22 | import com.android.internal.util.ArrayUtils; |
Amith Yamasani | 4b2e934 | 2011-03-31 12:38:53 -0700 | [diff] [blame] | 23 | import com.android.internal.util.FastXmlSerializer; |
| 24 | |
Amith Yamasani | 2a00329 | 2012-08-14 18:25:45 -0700 | [diff] [blame] | 25 | import android.app.ActivityManager; |
Dianne Hackborn | 80a4af2 | 2012-08-27 19:18:31 -0700 | [diff] [blame] | 26 | import android.app.ActivityManagerNative; |
| 27 | import android.app.IStopUserCallback; |
Amith Yamasani | 258848d | 2012-08-10 17:06:33 -0700 | [diff] [blame] | 28 | import android.content.Context; |
| 29 | import android.content.Intent; |
Amith Yamasani | 0b28549 | 2011-04-14 17:35:23 -0700 | [diff] [blame] | 30 | import android.content.pm.PackageManager; |
Amith Yamasani | 4b2e934 | 2011-03-31 12:38:53 -0700 | [diff] [blame] | 31 | import android.content.pm.UserInfo; |
Amith Yamasani | 258848d | 2012-08-10 17:06:33 -0700 | [diff] [blame] | 32 | import android.os.Binder; |
Amith Yamasani | 4b2e934 | 2011-03-31 12:38:53 -0700 | [diff] [blame] | 33 | import android.os.Environment; |
| 34 | import android.os.FileUtils; |
Amith Yamasani | 258848d | 2012-08-10 17:06:33 -0700 | [diff] [blame] | 35 | import android.os.IUserManager; |
Amith Yamasani | b8151ec | 2012-04-18 18:02:48 -0700 | [diff] [blame] | 36 | import android.os.ParcelFileDescriptor; |
Amith Yamasani | 258848d | 2012-08-10 17:06:33 -0700 | [diff] [blame] | 37 | import android.os.Process; |
Dianne Hackborn | 80a4af2 | 2012-08-27 19:18:31 -0700 | [diff] [blame] | 38 | import android.os.RemoteException; |
Dianne Hackborn | f02b60a | 2012-08-16 10:48:27 -0700 | [diff] [blame] | 39 | import android.os.UserHandle; |
Amith Yamasani | 2a00329 | 2012-08-14 18:25:45 -0700 | [diff] [blame] | 40 | import android.util.AtomicFile; |
Amith Yamasani | 4b2e934 | 2011-03-31 12:38:53 -0700 | [diff] [blame] | 41 | import android.util.Slog; |
| 42 | import android.util.SparseArray; |
| 43 | import android.util.Xml; |
| 44 | |
| 45 | import java.io.BufferedOutputStream; |
| 46 | import java.io.File; |
| 47 | import java.io.FileInputStream; |
Amith Yamasani | b8151ec | 2012-04-18 18:02:48 -0700 | [diff] [blame] | 48 | import java.io.FileNotFoundException; |
Amith Yamasani | 4b2e934 | 2011-03-31 12:38:53 -0700 | [diff] [blame] | 49 | import java.io.FileOutputStream; |
| 50 | import java.io.IOException; |
| 51 | import java.util.ArrayList; |
| 52 | import java.util.List; |
| 53 | |
| 54 | import org.xmlpull.v1.XmlPullParser; |
| 55 | import org.xmlpull.v1.XmlPullParserException; |
| 56 | import org.xmlpull.v1.XmlSerializer; |
| 57 | |
Amith Yamasani | 258848d | 2012-08-10 17:06:33 -0700 | [diff] [blame] | 58 | public class UserManagerService extends IUserManager.Stub { |
Amith Yamasani | b8151ec | 2012-04-18 18:02:48 -0700 | [diff] [blame] | 59 | |
Amith Yamasani | 2a00329 | 2012-08-14 18:25:45 -0700 | [diff] [blame] | 60 | private static final String LOG_TAG = "UserManagerService"; |
Amith Yamasani | b8151ec | 2012-04-18 18:02:48 -0700 | [diff] [blame] | 61 | |
Amith Yamasani | 4b2e934 | 2011-03-31 12:38:53 -0700 | [diff] [blame] | 62 | private static final String TAG_NAME = "name"; |
Amith Yamasani | 4b2e934 | 2011-03-31 12:38:53 -0700 | [diff] [blame] | 63 | private static final String ATTR_FLAGS = "flags"; |
Amith Yamasani | b8151ec | 2012-04-18 18:02:48 -0700 | [diff] [blame] | 64 | private static final String ATTR_ICON_PATH = "icon"; |
Amith Yamasani | 4b2e934 | 2011-03-31 12:38:53 -0700 | [diff] [blame] | 65 | private static final String ATTR_ID = "id"; |
Amith Yamasani | 2a00329 | 2012-08-14 18:25:45 -0700 | [diff] [blame] | 66 | private static final String ATTR_SERIAL_NO = "serialNumber"; |
| 67 | private static final String ATTR_NEXT_SERIAL_NO = "nextSerialNumber"; |
Amith Yamasani | 4b2e934 | 2011-03-31 12:38:53 -0700 | [diff] [blame] | 68 | private static final String TAG_USERS = "users"; |
Amith Yamasani | 4b2e934 | 2011-03-31 12:38:53 -0700 | [diff] [blame] | 69 | private static final String TAG_USER = "user"; |
| 70 | |
Amith Yamasani | 0b28549 | 2011-04-14 17:35:23 -0700 | [diff] [blame] | 71 | private static final String USER_INFO_DIR = "system" + File.separator + "users"; |
Amith Yamasani | 4b2e934 | 2011-03-31 12:38:53 -0700 | [diff] [blame] | 72 | private static final String USER_LIST_FILENAME = "userlist.xml"; |
Amith Yamasani | b8151ec | 2012-04-18 18:02:48 -0700 | [diff] [blame] | 73 | private static final String USER_PHOTO_FILENAME = "photo.png"; |
Amith Yamasani | 4b2e934 | 2011-03-31 12:38:53 -0700 | [diff] [blame] | 74 | |
Dianne Hackborn | 4428e17 | 2012-08-24 17:43:05 -0700 | [diff] [blame] | 75 | private final Context mContext; |
| 76 | private final PackageManagerService mPm; |
| 77 | private final Object mInstallLock; |
| 78 | private final Object mPackagesLock; |
Amith Yamasani | 4b2e934 | 2011-03-31 12:38:53 -0700 | [diff] [blame] | 79 | |
| 80 | private final File mUsersDir; |
| 81 | private final File mUserListFile; |
Dianne Hackborn | 4428e17 | 2012-08-24 17:43:05 -0700 | [diff] [blame] | 82 | private final File mBaseUserPath; |
| 83 | |
| 84 | private SparseArray<UserInfo> mUsers = new SparseArray<UserInfo>(); |
| 85 | |
Dianne Hackborn | 5dc5a00 | 2012-09-15 19:33:48 -0700 | [diff] [blame] | 86 | private final int mUserLimit; |
| 87 | |
Amith Yamasani | 0b28549 | 2011-04-14 17:35:23 -0700 | [diff] [blame] | 88 | private int[] mUserIds; |
Amith Yamasani | 258848d | 2012-08-10 17:06:33 -0700 | [diff] [blame] | 89 | private boolean mGuestEnabled; |
Amith Yamasani | 2a00329 | 2012-08-14 18:25:45 -0700 | [diff] [blame] | 90 | private int mNextSerialNumber; |
Amith Yamasani | 0b28549 | 2011-04-14 17:35:23 -0700 | [diff] [blame] | 91 | |
Amith Yamasani | 258848d | 2012-08-10 17:06:33 -0700 | [diff] [blame] | 92 | private static UserManagerService sInstance; |
Amith Yamasani | 258848d | 2012-08-10 17:06:33 -0700 | [diff] [blame] | 93 | |
Dianne Hackborn | 4428e17 | 2012-08-24 17:43:05 -0700 | [diff] [blame] | 94 | public static UserManagerService getInstance() { |
| 95 | synchronized (UserManagerService.class) { |
| 96 | return sInstance; |
Amith Yamasani | 258848d | 2012-08-10 17:06:33 -0700 | [diff] [blame] | 97 | } |
Amith Yamasani | 258848d | 2012-08-10 17:06:33 -0700 | [diff] [blame] | 98 | } |
Amith Yamasani | 4b2e934 | 2011-03-31 12:38:53 -0700 | [diff] [blame] | 99 | |
| 100 | /** |
| 101 | * Available for testing purposes. |
| 102 | */ |
Amith Yamasani | 258848d | 2012-08-10 17:06:33 -0700 | [diff] [blame] | 103 | UserManagerService(File dataDir, File baseUserPath) { |
Dianne Hackborn | 4428e17 | 2012-08-24 17:43:05 -0700 | [diff] [blame] | 104 | this(null, null, new Object(), new Object(), dataDir, baseUserPath); |
Amith Yamasani | 4b2e934 | 2011-03-31 12:38:53 -0700 | [diff] [blame] | 105 | } |
| 106 | |
Dianne Hackborn | 4428e17 | 2012-08-24 17:43:05 -0700 | [diff] [blame] | 107 | /** |
| 108 | * Called by package manager to create the service. This is closely |
| 109 | * associated with the package manager, and the given lock is the |
| 110 | * package manager's own lock. |
| 111 | */ |
| 112 | UserManagerService(Context context, PackageManagerService pm, |
| 113 | Object installLock, Object packagesLock) { |
| 114 | this(context, pm, installLock, packagesLock, |
| 115 | Environment.getDataDirectory(), |
| 116 | new File(Environment.getDataDirectory(), "user")); |
Amith Yamasani | 4b2e934 | 2011-03-31 12:38:53 -0700 | [diff] [blame] | 117 | } |
| 118 | |
Dianne Hackborn | 4428e17 | 2012-08-24 17:43:05 -0700 | [diff] [blame] | 119 | /** |
| 120 | * Available for testing purposes. |
| 121 | */ |
| 122 | private UserManagerService(Context context, PackageManagerService pm, |
| 123 | Object installLock, Object packagesLock, |
| 124 | File dataDir, File baseUserPath) { |
| 125 | synchronized (UserManagerService.class) { |
| 126 | mContext = context; |
| 127 | mPm = pm; |
| 128 | mInstallLock = installLock; |
| 129 | mPackagesLock = packagesLock; |
Dianne Hackborn | 5dc5a00 | 2012-09-15 19:33:48 -0700 | [diff] [blame] | 130 | mUserLimit = mContext.getResources().getInteger( |
| 131 | com.android.internal.R.integer.config_multiuserMaximumUsers); |
Dianne Hackborn | 4428e17 | 2012-08-24 17:43:05 -0700 | [diff] [blame] | 132 | mUsersDir = new File(dataDir, USER_INFO_DIR); |
| 133 | mUsersDir.mkdirs(); |
| 134 | // Make zeroth user directory, for services to migrate their files to that location |
| 135 | File userZeroDir = new File(mUsersDir, "0"); |
| 136 | userZeroDir.mkdirs(); |
| 137 | mBaseUserPath = baseUserPath; |
| 138 | FileUtils.setPermissions(mUsersDir.toString(), |
| 139 | FileUtils.S_IRWXU|FileUtils.S_IRWXG |
| 140 | |FileUtils.S_IROTH|FileUtils.S_IXOTH, |
| 141 | -1, -1); |
| 142 | mUserListFile = new File(mUsersDir, USER_LIST_FILENAME); |
| 143 | readUserList(); |
| 144 | sInstance = this; |
| 145 | } |
Amith Yamasani | 258848d | 2012-08-10 17:06:33 -0700 | [diff] [blame] | 146 | } |
| 147 | |
| 148 | @Override |
Amith Yamasani | 4b2e934 | 2011-03-31 12:38:53 -0700 | [diff] [blame] | 149 | public List<UserInfo> getUsers() { |
Amith Yamasani | 2a00329 | 2012-08-14 18:25:45 -0700 | [diff] [blame] | 150 | checkManageUsersPermission("query users"); |
Dianne Hackborn | 4428e17 | 2012-08-24 17:43:05 -0700 | [diff] [blame] | 151 | synchronized (mPackagesLock) { |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 152 | ArrayList<UserInfo> users = new ArrayList<UserInfo>(mUsers.size()); |
| 153 | for (int i = 0; i < mUsers.size(); i++) { |
| 154 | users.add(mUsers.valueAt(i)); |
| 155 | } |
| 156 | return users; |
Amith Yamasani | 4b2e934 | 2011-03-31 12:38:53 -0700 | [diff] [blame] | 157 | } |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 158 | } |
| 159 | |
Amith Yamasani | 258848d | 2012-08-10 17:06:33 -0700 | [diff] [blame] | 160 | @Override |
| 161 | public UserInfo getUserInfo(int userId) { |
Amith Yamasani | 2a00329 | 2012-08-14 18:25:45 -0700 | [diff] [blame] | 162 | checkManageUsersPermission("query user"); |
Dianne Hackborn | 4428e17 | 2012-08-24 17:43:05 -0700 | [diff] [blame] | 163 | synchronized (mPackagesLock) { |
Amith Yamasani | 19526374 | 2012-08-21 15:40:12 -0700 | [diff] [blame] | 164 | return getUserInfoLocked(userId); |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 165 | } |
| 166 | } |
| 167 | |
Amith Yamasani | 19526374 | 2012-08-21 15:40:12 -0700 | [diff] [blame] | 168 | /* |
| 169 | * Should be locked on mUsers before calling this. |
| 170 | */ |
| 171 | private UserInfo getUserInfoLocked(int userId) { |
| 172 | return mUsers.get(userId); |
| 173 | } |
| 174 | |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 175 | public boolean exists(int userId) { |
Dianne Hackborn | 4428e17 | 2012-08-24 17:43:05 -0700 | [diff] [blame] | 176 | synchronized (mPackagesLock) { |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 177 | return ArrayUtils.contains(mUserIds, userId); |
| 178 | } |
| 179 | } |
| 180 | |
Amith Yamasani | 258848d | 2012-08-10 17:06:33 -0700 | [diff] [blame] | 181 | @Override |
Amith Yamasani | b8151ec | 2012-04-18 18:02:48 -0700 | [diff] [blame] | 182 | public void setUserName(int userId, String name) { |
Amith Yamasani | 2a00329 | 2012-08-14 18:25:45 -0700 | [diff] [blame] | 183 | checkManageUsersPermission("rename users"); |
Dianne Hackborn | 4428e17 | 2012-08-24 17:43:05 -0700 | [diff] [blame] | 184 | synchronized (mPackagesLock) { |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 185 | UserInfo info = mUsers.get(userId); |
| 186 | if (name != null && !name.equals(info.name)) { |
| 187 | info.name = name; |
| 188 | writeUserLocked(info); |
| 189 | } |
| 190 | } |
Amith Yamasani | 4b2e934 | 2011-03-31 12:38:53 -0700 | [diff] [blame] | 191 | } |
| 192 | |
Amith Yamasani | 258848d | 2012-08-10 17:06:33 -0700 | [diff] [blame] | 193 | @Override |
Amith Yamasani | b8151ec | 2012-04-18 18:02:48 -0700 | [diff] [blame] | 194 | public ParcelFileDescriptor setUserIcon(int userId) { |
Amith Yamasani | 2a00329 | 2012-08-14 18:25:45 -0700 | [diff] [blame] | 195 | checkManageUsersPermission("update users"); |
Dianne Hackborn | 4428e17 | 2012-08-24 17:43:05 -0700 | [diff] [blame] | 196 | synchronized (mPackagesLock) { |
Amith Yamasani | b8151ec | 2012-04-18 18:02:48 -0700 | [diff] [blame] | 197 | UserInfo info = mUsers.get(userId); |
| 198 | if (info == null) return null; |
| 199 | ParcelFileDescriptor fd = updateIconBitmapLocked(info); |
| 200 | if (fd != null) { |
| 201 | writeUserLocked(info); |
| 202 | } |
| 203 | return fd; |
| 204 | } |
| 205 | } |
| 206 | |
Amith Yamasani | 258848d | 2012-08-10 17:06:33 -0700 | [diff] [blame] | 207 | @Override |
| 208 | public void setGuestEnabled(boolean enable) { |
Amith Yamasani | 2a00329 | 2012-08-14 18:25:45 -0700 | [diff] [blame] | 209 | checkManageUsersPermission("enable guest users"); |
Dianne Hackborn | 4428e17 | 2012-08-24 17:43:05 -0700 | [diff] [blame] | 210 | synchronized (mPackagesLock) { |
Amith Yamasani | 258848d | 2012-08-10 17:06:33 -0700 | [diff] [blame] | 211 | if (mGuestEnabled != enable) { |
| 212 | mGuestEnabled = enable; |
| 213 | // Erase any guest user that currently exists |
| 214 | for (int i = 0; i < mUsers.size(); i++) { |
| 215 | UserInfo user = mUsers.valueAt(i); |
| 216 | if (user.isGuest()) { |
| 217 | if (!enable) { |
| 218 | removeUser(user.id); |
| 219 | } |
| 220 | return; |
| 221 | } |
| 222 | } |
| 223 | // No guest was found |
| 224 | if (enable) { |
| 225 | createUser("Guest", UserInfo.FLAG_GUEST); |
| 226 | } |
| 227 | } |
| 228 | } |
| 229 | } |
| 230 | |
| 231 | @Override |
| 232 | public boolean isGuestEnabled() { |
Dianne Hackborn | 4428e17 | 2012-08-24 17:43:05 -0700 | [diff] [blame] | 233 | synchronized (mPackagesLock) { |
Amith Yamasani | 258848d | 2012-08-10 17:06:33 -0700 | [diff] [blame] | 234 | return mGuestEnabled; |
| 235 | } |
| 236 | } |
| 237 | |
| 238 | @Override |
| 239 | public void wipeUser(int userHandle) { |
Amith Yamasani | 2a00329 | 2012-08-14 18:25:45 -0700 | [diff] [blame] | 240 | checkManageUsersPermission("wipe user"); |
Amith Yamasani | 258848d | 2012-08-10 17:06:33 -0700 | [diff] [blame] | 241 | // TODO: |
| 242 | } |
| 243 | |
Dianne Hackborn | 5dc5a00 | 2012-09-15 19:33:48 -0700 | [diff] [blame] | 244 | public void makeInitialized(int userId) { |
| 245 | checkManageUsersPermission("makeInitialized"); |
| 246 | synchronized (mPackagesLock) { |
| 247 | UserInfo info = mUsers.get(userId); |
| 248 | if (info != null && (info.flags&UserInfo.FLAG_INITIALIZED) == 0) { |
| 249 | info.flags |= UserInfo.FLAG_INITIALIZED; |
| 250 | writeUserLocked(info); |
| 251 | } |
| 252 | } |
| 253 | } |
| 254 | |
Amith Yamasani | 258848d | 2012-08-10 17:06:33 -0700 | [diff] [blame] | 255 | /** |
Amith Yamasani | faea76f | 2012-09-11 10:59:48 -0700 | [diff] [blame] | 256 | * Check if we've hit the limit of how many users can be created. |
| 257 | */ |
Dianne Hackborn | 5dc5a00 | 2012-09-15 19:33:48 -0700 | [diff] [blame] | 258 | private boolean isUserLimitReachedLocked() { |
| 259 | int nUsers = mUsers.size(); |
| 260 | return nUsers >= mUserLimit; |
Amith Yamasani | faea76f | 2012-09-11 10:59:48 -0700 | [diff] [blame] | 261 | } |
| 262 | |
| 263 | /** |
Amith Yamasani | 19526374 | 2012-08-21 15:40:12 -0700 | [diff] [blame] | 264 | * Enforces that only the system UID or root's UID or apps that have the |
| 265 | * {@link android.Manifest.permission.MANAGE_USERS MANAGE_USERS} |
| 266 | * permission can make certain calls to the UserManager. |
Amith Yamasani | 258848d | 2012-08-10 17:06:33 -0700 | [diff] [blame] | 267 | * |
| 268 | * @param message used as message if SecurityException is thrown |
| 269 | * @throws SecurityException if the caller is not system or root |
| 270 | */ |
Amith Yamasani | 2a00329 | 2012-08-14 18:25:45 -0700 | [diff] [blame] | 271 | private static final void checkManageUsersPermission(String message) { |
Amith Yamasani | 258848d | 2012-08-10 17:06:33 -0700 | [diff] [blame] | 272 | final int uid = Binder.getCallingUid(); |
Amith Yamasani | 2a00329 | 2012-08-14 18:25:45 -0700 | [diff] [blame] | 273 | if (uid != Process.SYSTEM_UID && uid != 0 |
| 274 | && ActivityManager.checkComponentPermission( |
| 275 | android.Manifest.permission.MANAGE_USERS, |
| 276 | uid, -1, true) != PackageManager.PERMISSION_GRANTED) { |
| 277 | throw new SecurityException("You need MANAGE_USERS permission to: " + message); |
Amith Yamasani | 258848d | 2012-08-10 17:06:33 -0700 | [diff] [blame] | 278 | } |
| 279 | } |
| 280 | |
Amith Yamasani | b8151ec | 2012-04-18 18:02:48 -0700 | [diff] [blame] | 281 | private ParcelFileDescriptor updateIconBitmapLocked(UserInfo info) { |
| 282 | try { |
| 283 | File dir = new File(mUsersDir, Integer.toString(info.id)); |
| 284 | File file = new File(dir, USER_PHOTO_FILENAME); |
| 285 | if (!dir.exists()) { |
| 286 | dir.mkdir(); |
| 287 | FileUtils.setPermissions( |
| 288 | dir.getPath(), |
| 289 | FileUtils.S_IRWXU|FileUtils.S_IRWXG|FileUtils.S_IXOTH, |
| 290 | -1, -1); |
| 291 | } |
| 292 | ParcelFileDescriptor fd = ParcelFileDescriptor.open(file, |
| 293 | MODE_CREATE|MODE_READ_WRITE); |
| 294 | info.iconPath = file.getAbsolutePath(); |
| 295 | return fd; |
| 296 | } catch (FileNotFoundException e) { |
Amith Yamasani | 2a00329 | 2012-08-14 18:25:45 -0700 | [diff] [blame] | 297 | Slog.w(LOG_TAG, "Error setting photo for user ", e); |
Amith Yamasani | b8151ec | 2012-04-18 18:02:48 -0700 | [diff] [blame] | 298 | } |
| 299 | return null; |
| 300 | } |
| 301 | |
Amith Yamasani | 0b28549 | 2011-04-14 17:35:23 -0700 | [diff] [blame] | 302 | /** |
| 303 | * Returns an array of user ids. This array is cached here for quick access, so do not modify or |
| 304 | * cache it elsewhere. |
| 305 | * @return the array of user ids. |
| 306 | */ |
Dianne Hackborn | 1676c85 | 2012-09-10 14:52:30 -0700 | [diff] [blame] | 307 | public int[] getUserIds() { |
Dianne Hackborn | 4428e17 | 2012-08-24 17:43:05 -0700 | [diff] [blame] | 308 | synchronized (mPackagesLock) { |
Dianne Hackborn | 7767eac | 2012-08-23 18:25:40 -0700 | [diff] [blame] | 309 | return mUserIds; |
| 310 | } |
Amith Yamasani | 0b28549 | 2011-04-14 17:35:23 -0700 | [diff] [blame] | 311 | } |
| 312 | |
Dianne Hackborn | 4428e17 | 2012-08-24 17:43:05 -0700 | [diff] [blame] | 313 | int[] getUserIdsLPr() { |
| 314 | return mUserIds; |
| 315 | } |
| 316 | |
Amith Yamasani | 4b2e934 | 2011-03-31 12:38:53 -0700 | [diff] [blame] | 317 | private void readUserList() { |
Dianne Hackborn | 4428e17 | 2012-08-24 17:43:05 -0700 | [diff] [blame] | 318 | synchronized (mPackagesLock) { |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 319 | readUserListLocked(); |
| 320 | } |
| 321 | } |
| 322 | |
| 323 | private void readUserListLocked() { |
Amith Yamasani | 258848d | 2012-08-10 17:06:33 -0700 | [diff] [blame] | 324 | mGuestEnabled = false; |
Amith Yamasani | 4b2e934 | 2011-03-31 12:38:53 -0700 | [diff] [blame] | 325 | if (!mUserListFile.exists()) { |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 326 | fallbackToSingleUserLocked(); |
Amith Yamasani | 4b2e934 | 2011-03-31 12:38:53 -0700 | [diff] [blame] | 327 | return; |
| 328 | } |
| 329 | FileInputStream fis = null; |
Amith Yamasani | 2a00329 | 2012-08-14 18:25:45 -0700 | [diff] [blame] | 330 | AtomicFile userListFile = new AtomicFile(mUserListFile); |
Amith Yamasani | 4b2e934 | 2011-03-31 12:38:53 -0700 | [diff] [blame] | 331 | try { |
Amith Yamasani | 2a00329 | 2012-08-14 18:25:45 -0700 | [diff] [blame] | 332 | fis = userListFile.openRead(); |
Amith Yamasani | 4b2e934 | 2011-03-31 12:38:53 -0700 | [diff] [blame] | 333 | XmlPullParser parser = Xml.newPullParser(); |
| 334 | parser.setInput(fis, null); |
| 335 | int type; |
| 336 | while ((type = parser.next()) != XmlPullParser.START_TAG |
| 337 | && type != XmlPullParser.END_DOCUMENT) { |
| 338 | ; |
| 339 | } |
| 340 | |
| 341 | if (type != XmlPullParser.START_TAG) { |
Amith Yamasani | 0b28549 | 2011-04-14 17:35:23 -0700 | [diff] [blame] | 342 | Slog.e(LOG_TAG, "Unable to read user list"); |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 343 | fallbackToSingleUserLocked(); |
Amith Yamasani | 4b2e934 | 2011-03-31 12:38:53 -0700 | [diff] [blame] | 344 | return; |
| 345 | } |
| 346 | |
Amith Yamasani | 2a00329 | 2012-08-14 18:25:45 -0700 | [diff] [blame] | 347 | mNextSerialNumber = -1; |
| 348 | if (parser.getName().equals(TAG_USERS)) { |
| 349 | String lastSerialNumber = parser.getAttributeValue(null, ATTR_NEXT_SERIAL_NO); |
| 350 | if (lastSerialNumber != null) { |
| 351 | mNextSerialNumber = Integer.parseInt(lastSerialNumber); |
| 352 | } |
| 353 | } |
| 354 | |
Amith Yamasani | 4b2e934 | 2011-03-31 12:38:53 -0700 | [diff] [blame] | 355 | while ((type = parser.next()) != XmlPullParser.END_DOCUMENT) { |
| 356 | if (type == XmlPullParser.START_TAG && parser.getName().equals(TAG_USER)) { |
| 357 | String id = parser.getAttributeValue(null, ATTR_ID); |
| 358 | UserInfo user = readUser(Integer.parseInt(id)); |
| 359 | if (user != null) { |
| 360 | mUsers.put(user.id, user); |
Amith Yamasani | 2a00329 | 2012-08-14 18:25:45 -0700 | [diff] [blame] | 361 | if (user.isGuest()) { |
| 362 | mGuestEnabled = true; |
| 363 | } |
| 364 | if (mNextSerialNumber < 0 || mNextSerialNumber <= user.id) { |
| 365 | mNextSerialNumber = user.id + 1; |
| 366 | } |
Amith Yamasani | 258848d | 2012-08-10 17:06:33 -0700 | [diff] [blame] | 367 | } |
Amith Yamasani | 4b2e934 | 2011-03-31 12:38:53 -0700 | [diff] [blame] | 368 | } |
| 369 | } |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 370 | updateUserIdsLocked(); |
Amith Yamasani | 4b2e934 | 2011-03-31 12:38:53 -0700 | [diff] [blame] | 371 | } catch (IOException ioe) { |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 372 | fallbackToSingleUserLocked(); |
Amith Yamasani | 4b2e934 | 2011-03-31 12:38:53 -0700 | [diff] [blame] | 373 | } catch (XmlPullParserException pe) { |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 374 | fallbackToSingleUserLocked(); |
Dianne Hackborn | bfd89b3 | 2011-12-15 18:22:54 -0800 | [diff] [blame] | 375 | } finally { |
| 376 | if (fis != null) { |
| 377 | try { |
| 378 | fis.close(); |
| 379 | } catch (IOException e) { |
| 380 | } |
| 381 | } |
Amith Yamasani | 4b2e934 | 2011-03-31 12:38:53 -0700 | [diff] [blame] | 382 | } |
| 383 | } |
| 384 | |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 385 | private void fallbackToSingleUserLocked() { |
Amith Yamasani | 4b2e934 | 2011-03-31 12:38:53 -0700 | [diff] [blame] | 386 | // Create the primary user |
Amith Yamasani | b8151ec | 2012-04-18 18:02:48 -0700 | [diff] [blame] | 387 | UserInfo primary = new UserInfo(0, "Primary", null, |
Amith Yamasani | 4b2e934 | 2011-03-31 12:38:53 -0700 | [diff] [blame] | 388 | UserInfo.FLAG_ADMIN | UserInfo.FLAG_PRIMARY); |
| 389 | mUsers.put(0, primary); |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 390 | updateUserIdsLocked(); |
Amith Yamasani | 4b2e934 | 2011-03-31 12:38:53 -0700 | [diff] [blame] | 391 | |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 392 | writeUserListLocked(); |
| 393 | writeUserLocked(primary); |
Amith Yamasani | 4b2e934 | 2011-03-31 12:38:53 -0700 | [diff] [blame] | 394 | } |
| 395 | |
| 396 | /* |
| 397 | * Writes the user file in this format: |
| 398 | * |
| 399 | * <user flags="20039023" id="0"> |
| 400 | * <name>Primary</name> |
| 401 | * </user> |
| 402 | */ |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 403 | private void writeUserLocked(UserInfo userInfo) { |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 404 | FileOutputStream fos = null; |
Amith Yamasani | 2a00329 | 2012-08-14 18:25:45 -0700 | [diff] [blame] | 405 | AtomicFile userFile = new AtomicFile(new File(mUsersDir, userInfo.id + ".xml")); |
Amith Yamasani | 4b2e934 | 2011-03-31 12:38:53 -0700 | [diff] [blame] | 406 | try { |
Amith Yamasani | 2a00329 | 2012-08-14 18:25:45 -0700 | [diff] [blame] | 407 | fos = userFile.startWrite(); |
Amith Yamasani | 4b2e934 | 2011-03-31 12:38:53 -0700 | [diff] [blame] | 408 | final BufferedOutputStream bos = new BufferedOutputStream(fos); |
| 409 | |
| 410 | // XmlSerializer serializer = XmlUtils.serializerInstance(); |
| 411 | final XmlSerializer serializer = new FastXmlSerializer(); |
| 412 | serializer.setOutput(bos, "utf-8"); |
| 413 | serializer.startDocument(null, true); |
| 414 | serializer.setFeature("http://xmlpull.org/v1/doc/features.html#indent-output", true); |
| 415 | |
| 416 | serializer.startTag(null, TAG_USER); |
| 417 | serializer.attribute(null, ATTR_ID, Integer.toString(userInfo.id)); |
Amith Yamasani | 2a00329 | 2012-08-14 18:25:45 -0700 | [diff] [blame] | 418 | serializer.attribute(null, ATTR_SERIAL_NO, Integer.toString(userInfo.serialNumber)); |
Amith Yamasani | 4b2e934 | 2011-03-31 12:38:53 -0700 | [diff] [blame] | 419 | serializer.attribute(null, ATTR_FLAGS, Integer.toString(userInfo.flags)); |
Amith Yamasani | b8151ec | 2012-04-18 18:02:48 -0700 | [diff] [blame] | 420 | if (userInfo.iconPath != null) { |
| 421 | serializer.attribute(null, ATTR_ICON_PATH, userInfo.iconPath); |
| 422 | } |
Amith Yamasani | 4b2e934 | 2011-03-31 12:38:53 -0700 | [diff] [blame] | 423 | |
| 424 | serializer.startTag(null, TAG_NAME); |
| 425 | serializer.text(userInfo.name); |
| 426 | serializer.endTag(null, TAG_NAME); |
| 427 | |
| 428 | serializer.endTag(null, TAG_USER); |
| 429 | |
| 430 | serializer.endDocument(); |
Amith Yamasani | 2a00329 | 2012-08-14 18:25:45 -0700 | [diff] [blame] | 431 | userFile.finishWrite(fos); |
| 432 | } catch (Exception ioe) { |
Amith Yamasani | 0b28549 | 2011-04-14 17:35:23 -0700 | [diff] [blame] | 433 | Slog.e(LOG_TAG, "Error writing user info " + userInfo.id + "\n" + ioe); |
Amith Yamasani | 2a00329 | 2012-08-14 18:25:45 -0700 | [diff] [blame] | 434 | userFile.failWrite(fos); |
Amith Yamasani | 4b2e934 | 2011-03-31 12:38:53 -0700 | [diff] [blame] | 435 | } |
| 436 | } |
| 437 | |
| 438 | /* |
| 439 | * Writes the user list file in this format: |
| 440 | * |
Amith Yamasani | 2a00329 | 2012-08-14 18:25:45 -0700 | [diff] [blame] | 441 | * <users nextSerialNumber="3"> |
Amith Yamasani | 4b2e934 | 2011-03-31 12:38:53 -0700 | [diff] [blame] | 442 | * <user id="0"></user> |
| 443 | * <user id="2"></user> |
| 444 | * </users> |
| 445 | */ |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 446 | private void writeUserListLocked() { |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 447 | FileOutputStream fos = null; |
Amith Yamasani | 2a00329 | 2012-08-14 18:25:45 -0700 | [diff] [blame] | 448 | AtomicFile userListFile = new AtomicFile(mUserListFile); |
Amith Yamasani | 4b2e934 | 2011-03-31 12:38:53 -0700 | [diff] [blame] | 449 | try { |
Amith Yamasani | 2a00329 | 2012-08-14 18:25:45 -0700 | [diff] [blame] | 450 | fos = userListFile.startWrite(); |
Amith Yamasani | 4b2e934 | 2011-03-31 12:38:53 -0700 | [diff] [blame] | 451 | final BufferedOutputStream bos = new BufferedOutputStream(fos); |
| 452 | |
| 453 | // XmlSerializer serializer = XmlUtils.serializerInstance(); |
| 454 | final XmlSerializer serializer = new FastXmlSerializer(); |
| 455 | serializer.setOutput(bos, "utf-8"); |
| 456 | serializer.startDocument(null, true); |
| 457 | serializer.setFeature("http://xmlpull.org/v1/doc/features.html#indent-output", true); |
| 458 | |
| 459 | serializer.startTag(null, TAG_USERS); |
Amith Yamasani | 2a00329 | 2012-08-14 18:25:45 -0700 | [diff] [blame] | 460 | serializer.attribute(null, ATTR_NEXT_SERIAL_NO, Integer.toString(mNextSerialNumber)); |
Amith Yamasani | 4b2e934 | 2011-03-31 12:38:53 -0700 | [diff] [blame] | 461 | |
| 462 | for (int i = 0; i < mUsers.size(); i++) { |
| 463 | UserInfo user = mUsers.valueAt(i); |
| 464 | serializer.startTag(null, TAG_USER); |
| 465 | serializer.attribute(null, ATTR_ID, Integer.toString(user.id)); |
| 466 | serializer.endTag(null, TAG_USER); |
Amith Yamasani | 4b2e934 | 2011-03-31 12:38:53 -0700 | [diff] [blame] | 467 | } |
| 468 | |
| 469 | serializer.endTag(null, TAG_USERS); |
| 470 | |
| 471 | serializer.endDocument(); |
Amith Yamasani | 2a00329 | 2012-08-14 18:25:45 -0700 | [diff] [blame] | 472 | userListFile.finishWrite(fos); |
| 473 | } catch (Exception e) { |
| 474 | userListFile.failWrite(fos); |
Amith Yamasani | 0b28549 | 2011-04-14 17:35:23 -0700 | [diff] [blame] | 475 | Slog.e(LOG_TAG, "Error writing user list"); |
Amith Yamasani | 4b2e934 | 2011-03-31 12:38:53 -0700 | [diff] [blame] | 476 | } |
| 477 | } |
| 478 | |
| 479 | private UserInfo readUser(int id) { |
| 480 | int flags = 0; |
Amith Yamasani | 2a00329 | 2012-08-14 18:25:45 -0700 | [diff] [blame] | 481 | int serialNumber = id; |
Amith Yamasani | 4b2e934 | 2011-03-31 12:38:53 -0700 | [diff] [blame] | 482 | String name = null; |
Amith Yamasani | b8151ec | 2012-04-18 18:02:48 -0700 | [diff] [blame] | 483 | String iconPath = null; |
Amith Yamasani | 4b2e934 | 2011-03-31 12:38:53 -0700 | [diff] [blame] | 484 | |
| 485 | FileInputStream fis = null; |
| 486 | try { |
Amith Yamasani | 2a00329 | 2012-08-14 18:25:45 -0700 | [diff] [blame] | 487 | AtomicFile userFile = |
| 488 | new AtomicFile(new File(mUsersDir, Integer.toString(id) + ".xml")); |
| 489 | fis = userFile.openRead(); |
Amith Yamasani | 4b2e934 | 2011-03-31 12:38:53 -0700 | [diff] [blame] | 490 | XmlPullParser parser = Xml.newPullParser(); |
| 491 | parser.setInput(fis, null); |
| 492 | int type; |
| 493 | while ((type = parser.next()) != XmlPullParser.START_TAG |
| 494 | && type != XmlPullParser.END_DOCUMENT) { |
| 495 | ; |
| 496 | } |
| 497 | |
| 498 | if (type != XmlPullParser.START_TAG) { |
Amith Yamasani | 0b28549 | 2011-04-14 17:35:23 -0700 | [diff] [blame] | 499 | Slog.e(LOG_TAG, "Unable to read user " + id); |
Amith Yamasani | 4b2e934 | 2011-03-31 12:38:53 -0700 | [diff] [blame] | 500 | return null; |
| 501 | } |
| 502 | |
| 503 | if (type == XmlPullParser.START_TAG && parser.getName().equals(TAG_USER)) { |
| 504 | String storedId = parser.getAttributeValue(null, ATTR_ID); |
| 505 | if (Integer.parseInt(storedId) != id) { |
Amith Yamasani | 0b28549 | 2011-04-14 17:35:23 -0700 | [diff] [blame] | 506 | Slog.e(LOG_TAG, "User id does not match the file name"); |
Amith Yamasani | 4b2e934 | 2011-03-31 12:38:53 -0700 | [diff] [blame] | 507 | return null; |
| 508 | } |
Amith Yamasani | 2a00329 | 2012-08-14 18:25:45 -0700 | [diff] [blame] | 509 | String serialNumberValue = parser.getAttributeValue(null, ATTR_SERIAL_NO); |
| 510 | if (serialNumberValue != null) { |
| 511 | serialNumber = Integer.parseInt(serialNumberValue); |
| 512 | } |
Amith Yamasani | 4b2e934 | 2011-03-31 12:38:53 -0700 | [diff] [blame] | 513 | String flagString = parser.getAttributeValue(null, ATTR_FLAGS); |
| 514 | flags = Integer.parseInt(flagString); |
Amith Yamasani | b8151ec | 2012-04-18 18:02:48 -0700 | [diff] [blame] | 515 | iconPath = parser.getAttributeValue(null, ATTR_ICON_PATH); |
Amith Yamasani | 4b2e934 | 2011-03-31 12:38:53 -0700 | [diff] [blame] | 516 | |
| 517 | while ((type = parser.next()) != XmlPullParser.START_TAG |
| 518 | && type != XmlPullParser.END_DOCUMENT) { |
| 519 | } |
| 520 | if (type == XmlPullParser.START_TAG && parser.getName().equals(TAG_NAME)) { |
| 521 | type = parser.next(); |
| 522 | if (type == XmlPullParser.TEXT) { |
| 523 | name = parser.getText(); |
| 524 | } |
| 525 | } |
| 526 | } |
Amith Yamasani | 4b2e934 | 2011-03-31 12:38:53 -0700 | [diff] [blame] | 527 | |
Amith Yamasani | b8151ec | 2012-04-18 18:02:48 -0700 | [diff] [blame] | 528 | UserInfo userInfo = new UserInfo(id, name, iconPath, flags); |
Amith Yamasani | 2a00329 | 2012-08-14 18:25:45 -0700 | [diff] [blame] | 529 | userInfo.serialNumber = serialNumber; |
Amith Yamasani | 4b2e934 | 2011-03-31 12:38:53 -0700 | [diff] [blame] | 530 | return userInfo; |
| 531 | |
| 532 | } catch (IOException ioe) { |
| 533 | } catch (XmlPullParserException pe) { |
Dianne Hackborn | bfd89b3 | 2011-12-15 18:22:54 -0800 | [diff] [blame] | 534 | } finally { |
| 535 | if (fis != null) { |
| 536 | try { |
| 537 | fis.close(); |
| 538 | } catch (IOException e) { |
| 539 | } |
| 540 | } |
Amith Yamasani | 4b2e934 | 2011-03-31 12:38:53 -0700 | [diff] [blame] | 541 | } |
| 542 | return null; |
| 543 | } |
| 544 | |
Amith Yamasani | 258848d | 2012-08-10 17:06:33 -0700 | [diff] [blame] | 545 | @Override |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 546 | public UserInfo createUser(String name, int flags) { |
Amith Yamasani | 2a00329 | 2012-08-14 18:25:45 -0700 | [diff] [blame] | 547 | checkManageUsersPermission("Only the system can create users"); |
Amith Yamasani | faea76f | 2012-09-11 10:59:48 -0700 | [diff] [blame] | 548 | |
Dianne Hackborn | 5dc5a00 | 2012-09-15 19:33:48 -0700 | [diff] [blame] | 549 | final long ident = Binder.clearCallingIdentity(); |
| 550 | final UserInfo userInfo; |
| 551 | try { |
| 552 | synchronized (mInstallLock) { |
| 553 | synchronized (mPackagesLock) { |
| 554 | if (isUserLimitReachedLocked()) return null; |
| 555 | int userId = getNextAvailableIdLocked(); |
| 556 | userInfo = new UserInfo(userId, name, null, flags); |
| 557 | File userPath = new File(mBaseUserPath, Integer.toString(userId)); |
| 558 | userInfo.serialNumber = mNextSerialNumber++; |
| 559 | mUsers.put(userId, userInfo); |
| 560 | writeUserListLocked(); |
| 561 | writeUserLocked(userInfo); |
| 562 | updateUserIdsLocked(); |
| 563 | mPm.createNewUserLILPw(userId, userPath); |
| 564 | } |
Dianne Hackborn | 4428e17 | 2012-08-24 17:43:05 -0700 | [diff] [blame] | 565 | } |
Dianne Hackborn | 5dc5a00 | 2012-09-15 19:33:48 -0700 | [diff] [blame] | 566 | if (userInfo != null) { |
| 567 | Intent addedIntent = new Intent(Intent.ACTION_USER_ADDED); |
| 568 | addedIntent.putExtra(Intent.EXTRA_USER_HANDLE, userInfo.id); |
| 569 | mContext.sendBroadcastAsUser(addedIntent, UserHandle.ALL, |
| 570 | android.Manifest.permission.MANAGE_USERS); |
| 571 | } |
| 572 | } finally { |
| 573 | Binder.restoreCallingIdentity(ident); |
Amith Yamasani | 258848d | 2012-08-10 17:06:33 -0700 | [diff] [blame] | 574 | } |
Amith Yamasani | 4b2e934 | 2011-03-31 12:38:53 -0700 | [diff] [blame] | 575 | return userInfo; |
| 576 | } |
| 577 | |
Amith Yamasani | 0b28549 | 2011-04-14 17:35:23 -0700 | [diff] [blame] | 578 | /** |
| 579 | * Removes a user and all data directories created for that user. This method should be called |
| 580 | * after the user's processes have been terminated. |
| 581 | * @param id the user's id |
| 582 | */ |
Amith Yamasani | 258848d | 2012-08-10 17:06:33 -0700 | [diff] [blame] | 583 | public boolean removeUser(int userHandle) { |
Amith Yamasani | 2a00329 | 2012-08-14 18:25:45 -0700 | [diff] [blame] | 584 | checkManageUsersPermission("Only the system can remove users"); |
Dianne Hackborn | 80a4af2 | 2012-08-27 19:18:31 -0700 | [diff] [blame] | 585 | final UserInfo user; |
| 586 | synchronized (mPackagesLock) { |
| 587 | user = mUsers.get(userHandle); |
| 588 | if (userHandle == 0 || user == null) { |
| 589 | return false; |
| 590 | } |
| 591 | } |
| 592 | |
| 593 | int res; |
| 594 | try { |
| 595 | res = ActivityManagerNative.getDefault().stopUser(userHandle, |
| 596 | new IStopUserCallback.Stub() { |
| 597 | @Override |
| 598 | public void userStopped(int userId) { |
| 599 | finishRemoveUser(userId); |
| 600 | } |
| 601 | @Override |
| 602 | public void userStopAborted(int userId) { |
| 603 | } |
| 604 | }); |
| 605 | } catch (RemoteException e) { |
| 606 | return false; |
| 607 | } |
| 608 | |
| 609 | return res == ActivityManager.USER_OP_SUCCESS; |
| 610 | } |
| 611 | |
| 612 | void finishRemoveUser(int userHandle) { |
Dianne Hackborn | 4428e17 | 2012-08-24 17:43:05 -0700 | [diff] [blame] | 613 | synchronized (mInstallLock) { |
| 614 | synchronized (mPackagesLock) { |
Dianne Hackborn | 4428e17 | 2012-08-24 17:43:05 -0700 | [diff] [blame] | 615 | // Cleanup package manager settings |
| 616 | mPm.cleanUpUserLILPw(userHandle); |
| 617 | |
| 618 | // Remove this user from the list |
| 619 | mUsers.remove(userHandle); |
| 620 | // Remove user file |
| 621 | AtomicFile userFile = new AtomicFile(new File(mUsersDir, userHandle + ".xml")); |
| 622 | userFile.delete(); |
| 623 | // Update the user list |
| 624 | writeUserListLocked(); |
| 625 | updateUserIdsLocked(); |
Amith Yamasani | 61f5737 | 2012-08-31 12:12:28 -0700 | [diff] [blame] | 626 | removeDirectoryRecursive(Environment.getUserSystemDirectory(userHandle)); |
Dianne Hackborn | 4428e17 | 2012-08-24 17:43:05 -0700 | [diff] [blame] | 627 | } |
| 628 | } |
Amith Yamasani | 0cd867c | 2012-08-22 16:45:47 -0700 | [diff] [blame] | 629 | |
Amith Yamasani | 2a00329 | 2012-08-14 18:25:45 -0700 | [diff] [blame] | 630 | // Let other services shutdown any activity |
Dianne Hackborn | 5dc5a00 | 2012-09-15 19:33:48 -0700 | [diff] [blame] | 631 | long ident = Binder.clearCallingIdentity(); |
| 632 | try { |
| 633 | Intent addedIntent = new Intent(Intent.ACTION_USER_REMOVED); |
| 634 | addedIntent.putExtra(Intent.EXTRA_USER_HANDLE, userHandle); |
| 635 | mContext.sendBroadcastAsUser(addedIntent, UserHandle.ALL, |
| 636 | android.Manifest.permission.MANAGE_USERS); |
| 637 | } finally { |
| 638 | Binder.restoreCallingIdentity(ident); |
| 639 | } |
Amith Yamasani | 2a00329 | 2012-08-14 18:25:45 -0700 | [diff] [blame] | 640 | } |
| 641 | |
Amith Yamasani | 61f5737 | 2012-08-31 12:12:28 -0700 | [diff] [blame] | 642 | private void removeDirectoryRecursive(File parent) { |
| 643 | if (parent.isDirectory()) { |
| 644 | String[] files = parent.list(); |
| 645 | for (String filename : files) { |
| 646 | File child = new File(parent, filename); |
| 647 | removeDirectoryRecursive(child); |
| 648 | } |
| 649 | } |
| 650 | parent.delete(); |
| 651 | } |
| 652 | |
Amith Yamasani | 2a00329 | 2012-08-14 18:25:45 -0700 | [diff] [blame] | 653 | @Override |
| 654 | public int getUserSerialNumber(int userHandle) { |
Dianne Hackborn | 4428e17 | 2012-08-24 17:43:05 -0700 | [diff] [blame] | 655 | synchronized (mPackagesLock) { |
Amith Yamasani | 2a00329 | 2012-08-14 18:25:45 -0700 | [diff] [blame] | 656 | if (!exists(userHandle)) return -1; |
Amith Yamasani | 19526374 | 2012-08-21 15:40:12 -0700 | [diff] [blame] | 657 | return getUserInfoLocked(userHandle).serialNumber; |
Amith Yamasani | 2a00329 | 2012-08-14 18:25:45 -0700 | [diff] [blame] | 658 | } |
| 659 | } |
| 660 | |
| 661 | @Override |
| 662 | public int getUserHandle(int userSerialNumber) { |
Dianne Hackborn | 4428e17 | 2012-08-24 17:43:05 -0700 | [diff] [blame] | 663 | synchronized (mPackagesLock) { |
Amith Yamasani | 2a00329 | 2012-08-14 18:25:45 -0700 | [diff] [blame] | 664 | for (int userId : mUserIds) { |
Amith Yamasani | 19526374 | 2012-08-21 15:40:12 -0700 | [diff] [blame] | 665 | if (getUserInfoLocked(userId).serialNumber == userSerialNumber) return userId; |
Amith Yamasani | 2a00329 | 2012-08-14 18:25:45 -0700 | [diff] [blame] | 666 | } |
| 667 | // Not found |
| 668 | return -1; |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 669 | } |
| 670 | } |
| 671 | |
Amith Yamasani | 0b28549 | 2011-04-14 17:35:23 -0700 | [diff] [blame] | 672 | /** |
| 673 | * Caches the list of user ids in an array, adjusting the array size when necessary. |
| 674 | */ |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 675 | private void updateUserIdsLocked() { |
Dianne Hackborn | 7767eac | 2012-08-23 18:25:40 -0700 | [diff] [blame] | 676 | int[] newUsers = new int[mUsers.size()]; |
Amith Yamasani | 0b28549 | 2011-04-14 17:35:23 -0700 | [diff] [blame] | 677 | for (int i = 0; i < mUsers.size(); i++) { |
Dianne Hackborn | 7767eac | 2012-08-23 18:25:40 -0700 | [diff] [blame] | 678 | newUsers[i] = mUsers.keyAt(i); |
Amith Yamasani | 0b28549 | 2011-04-14 17:35:23 -0700 | [diff] [blame] | 679 | } |
Dianne Hackborn | 7767eac | 2012-08-23 18:25:40 -0700 | [diff] [blame] | 680 | mUserIds = newUsers; |
Amith Yamasani | 0b28549 | 2011-04-14 17:35:23 -0700 | [diff] [blame] | 681 | } |
| 682 | |
| 683 | /** |
| 684 | * Returns the next available user id, filling in any holes in the ids. |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 685 | * TODO: May not be a good idea to recycle ids, in case it results in confusion |
| 686 | * for data and battery stats collection, or unexpected cross-talk. |
Amith Yamasani | 0b28549 | 2011-04-14 17:35:23 -0700 | [diff] [blame] | 687 | * @return |
| 688 | */ |
Dianne Hackborn | 5dc5a00 | 2012-09-15 19:33:48 -0700 | [diff] [blame] | 689 | private int getNextAvailableIdLocked() { |
Dianne Hackborn | 4428e17 | 2012-08-24 17:43:05 -0700 | [diff] [blame] | 690 | synchronized (mPackagesLock) { |
Amith Yamasani | 19526374 | 2012-08-21 15:40:12 -0700 | [diff] [blame] | 691 | int i = 0; |
| 692 | while (i < Integer.MAX_VALUE) { |
| 693 | if (mUsers.indexOfKey(i) < 0) { |
| 694 | break; |
| 695 | } |
| 696 | i++; |
Amith Yamasani | 4b2e934 | 2011-03-31 12:38:53 -0700 | [diff] [blame] | 697 | } |
Amith Yamasani | 19526374 | 2012-08-21 15:40:12 -0700 | [diff] [blame] | 698 | return i; |
Amith Yamasani | 4b2e934 | 2011-03-31 12:38:53 -0700 | [diff] [blame] | 699 | } |
Amith Yamasani | 4b2e934 | 2011-03-31 12:38:53 -0700 | [diff] [blame] | 700 | } |
Amith Yamasani | 4b2e934 | 2011-03-31 12:38:53 -0700 | [diff] [blame] | 701 | } |