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