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 | 0b28549 | 2011-04-14 17:35:23 -0700 | [diff] [blame] | 25 | import android.content.pm.ApplicationInfo; |
| 26 | import android.content.pm.PackageManager; |
Amith Yamasani | 4b2e934 | 2011-03-31 12:38:53 -0700 | [diff] [blame] | 27 | import android.content.pm.UserInfo; |
| 28 | import android.os.Environment; |
| 29 | import android.os.FileUtils; |
Amith Yamasani | b8151ec | 2012-04-18 18:02:48 -0700 | [diff] [blame^] | 30 | import android.os.ParcelFileDescriptor; |
Amith Yamasani | 0b28549 | 2011-04-14 17:35:23 -0700 | [diff] [blame] | 31 | import android.os.SystemClock; |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 32 | import android.os.UserId; |
Amith Yamasani | 0b28549 | 2011-04-14 17:35:23 -0700 | [diff] [blame] | 33 | import android.util.Log; |
Amith Yamasani | 4b2e934 | 2011-03-31 12:38:53 -0700 | [diff] [blame] | 34 | import android.util.Slog; |
| 35 | import android.util.SparseArray; |
| 36 | import android.util.Xml; |
| 37 | |
| 38 | import java.io.BufferedOutputStream; |
| 39 | import java.io.File; |
| 40 | import java.io.FileInputStream; |
Amith Yamasani | b8151ec | 2012-04-18 18:02:48 -0700 | [diff] [blame^] | 41 | import java.io.FileNotFoundException; |
Amith Yamasani | 4b2e934 | 2011-03-31 12:38:53 -0700 | [diff] [blame] | 42 | import java.io.FileOutputStream; |
| 43 | import java.io.IOException; |
| 44 | import java.util.ArrayList; |
| 45 | import java.util.List; |
| 46 | |
| 47 | import org.xmlpull.v1.XmlPullParser; |
| 48 | import org.xmlpull.v1.XmlPullParserException; |
| 49 | import org.xmlpull.v1.XmlSerializer; |
| 50 | |
Amith Yamasani | 0b28549 | 2011-04-14 17:35:23 -0700 | [diff] [blame] | 51 | public class UserManager { |
Amith Yamasani | b8151ec | 2012-04-18 18:02:48 -0700 | [diff] [blame^] | 52 | |
| 53 | private static final String TAG = "UserManager"; |
| 54 | |
Amith Yamasani | 4b2e934 | 2011-03-31 12:38:53 -0700 | [diff] [blame] | 55 | private static final String TAG_NAME = "name"; |
| 56 | |
| 57 | private static final String ATTR_FLAGS = "flags"; |
| 58 | |
Amith Yamasani | b8151ec | 2012-04-18 18:02:48 -0700 | [diff] [blame^] | 59 | private static final String ATTR_ICON_PATH = "icon"; |
| 60 | |
Amith Yamasani | 4b2e934 | 2011-03-31 12:38:53 -0700 | [diff] [blame] | 61 | private static final String ATTR_ID = "id"; |
| 62 | |
| 63 | private static final String TAG_USERS = "users"; |
| 64 | |
| 65 | private static final String TAG_USER = "user"; |
| 66 | |
Amith Yamasani | 0b28549 | 2011-04-14 17:35:23 -0700 | [diff] [blame] | 67 | private static final String LOG_TAG = "UserManager"; |
Amith Yamasani | 4b2e934 | 2011-03-31 12:38:53 -0700 | [diff] [blame] | 68 | |
Amith Yamasani | 0b28549 | 2011-04-14 17:35:23 -0700 | [diff] [blame] | 69 | private static final String USER_INFO_DIR = "system" + File.separator + "users"; |
Amith Yamasani | 4b2e934 | 2011-03-31 12:38:53 -0700 | [diff] [blame] | 70 | private static final String USER_LIST_FILENAME = "userlist.xml"; |
Amith Yamasani | b8151ec | 2012-04-18 18:02:48 -0700 | [diff] [blame^] | 71 | private static final String USER_PHOTO_FILENAME = "photo.png"; |
Amith Yamasani | 4b2e934 | 2011-03-31 12:38:53 -0700 | [diff] [blame] | 72 | |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 73 | private SparseArray<UserInfo> mUsers = new SparseArray<UserInfo>(); |
Amith Yamasani | 4b2e934 | 2011-03-31 12:38:53 -0700 | [diff] [blame] | 74 | |
| 75 | private final File mUsersDir; |
| 76 | private final File mUserListFile; |
Amith Yamasani | 0b28549 | 2011-04-14 17:35:23 -0700 | [diff] [blame] | 77 | private int[] mUserIds; |
| 78 | |
| 79 | private Installer mInstaller; |
| 80 | private File mBaseUserPath; |
Amith Yamasani | 4b2e934 | 2011-03-31 12:38:53 -0700 | [diff] [blame] | 81 | |
| 82 | /** |
| 83 | * Available for testing purposes. |
| 84 | */ |
Amith Yamasani | 0b28549 | 2011-04-14 17:35:23 -0700 | [diff] [blame] | 85 | UserManager(File dataDir, File baseUserPath) { |
Amith Yamasani | 4b2e934 | 2011-03-31 12:38:53 -0700 | [diff] [blame] | 86 | mUsersDir = new File(dataDir, USER_INFO_DIR); |
| 87 | mUsersDir.mkdirs(); |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 88 | // Make zeroth user directory, for services to migrate their files to that location |
| 89 | File userZeroDir = new File(mUsersDir, "0"); |
| 90 | userZeroDir.mkdirs(); |
Amith Yamasani | 0b28549 | 2011-04-14 17:35:23 -0700 | [diff] [blame] | 91 | mBaseUserPath = baseUserPath; |
Amith Yamasani | 4b2e934 | 2011-03-31 12:38:53 -0700 | [diff] [blame] | 92 | FileUtils.setPermissions(mUsersDir.toString(), |
| 93 | FileUtils.S_IRWXU|FileUtils.S_IRWXG |
| 94 | |FileUtils.S_IROTH|FileUtils.S_IXOTH, |
| 95 | -1, -1); |
| 96 | mUserListFile = new File(mUsersDir, USER_LIST_FILENAME); |
| 97 | readUserList(); |
| 98 | } |
| 99 | |
Amith Yamasani | 0b28549 | 2011-04-14 17:35:23 -0700 | [diff] [blame] | 100 | public UserManager(Installer installer, File baseUserPath) { |
| 101 | this(Environment.getDataDirectory(), baseUserPath); |
| 102 | mInstaller = installer; |
Amith Yamasani | 4b2e934 | 2011-03-31 12:38:53 -0700 | [diff] [blame] | 103 | } |
| 104 | |
| 105 | public List<UserInfo> getUsers() { |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 106 | synchronized (mUsers) { |
| 107 | ArrayList<UserInfo> users = new ArrayList<UserInfo>(mUsers.size()); |
| 108 | for (int i = 0; i < mUsers.size(); i++) { |
| 109 | users.add(mUsers.valueAt(i)); |
| 110 | } |
| 111 | return users; |
Amith Yamasani | 4b2e934 | 2011-03-31 12:38:53 -0700 | [diff] [blame] | 112 | } |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 113 | } |
| 114 | |
| 115 | public UserInfo getUser(int userId) { |
| 116 | synchronized (mUsers) { |
| 117 | UserInfo info = mUsers.get(userId); |
| 118 | return info; |
| 119 | } |
| 120 | } |
| 121 | |
| 122 | public boolean exists(int userId) { |
| 123 | synchronized (mUsers) { |
| 124 | return ArrayUtils.contains(mUserIds, userId); |
| 125 | } |
| 126 | } |
| 127 | |
Amith Yamasani | b8151ec | 2012-04-18 18:02:48 -0700 | [diff] [blame^] | 128 | public void setUserName(int userId, String name) { |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 129 | synchronized (mUsers) { |
| 130 | UserInfo info = mUsers.get(userId); |
| 131 | if (name != null && !name.equals(info.name)) { |
| 132 | info.name = name; |
| 133 | writeUserLocked(info); |
| 134 | } |
| 135 | } |
Amith Yamasani | 4b2e934 | 2011-03-31 12:38:53 -0700 | [diff] [blame] | 136 | } |
| 137 | |
Amith Yamasani | b8151ec | 2012-04-18 18:02:48 -0700 | [diff] [blame^] | 138 | public ParcelFileDescriptor setUserIcon(int userId) { |
| 139 | synchronized (mUsers) { |
| 140 | UserInfo info = mUsers.get(userId); |
| 141 | if (info == null) return null; |
| 142 | ParcelFileDescriptor fd = updateIconBitmapLocked(info); |
| 143 | if (fd != null) { |
| 144 | writeUserLocked(info); |
| 145 | } |
| 146 | return fd; |
| 147 | } |
| 148 | } |
| 149 | |
| 150 | private ParcelFileDescriptor updateIconBitmapLocked(UserInfo info) { |
| 151 | try { |
| 152 | File dir = new File(mUsersDir, Integer.toString(info.id)); |
| 153 | File file = new File(dir, USER_PHOTO_FILENAME); |
| 154 | if (!dir.exists()) { |
| 155 | dir.mkdir(); |
| 156 | FileUtils.setPermissions( |
| 157 | dir.getPath(), |
| 158 | FileUtils.S_IRWXU|FileUtils.S_IRWXG|FileUtils.S_IXOTH, |
| 159 | -1, -1); |
| 160 | } |
| 161 | ParcelFileDescriptor fd = ParcelFileDescriptor.open(file, |
| 162 | MODE_CREATE|MODE_READ_WRITE); |
| 163 | info.iconPath = file.getAbsolutePath(); |
| 164 | return fd; |
| 165 | } catch (FileNotFoundException e) { |
| 166 | Slog.w(TAG, "Error setting photo for user ", e); |
| 167 | } |
| 168 | return null; |
| 169 | } |
| 170 | |
Amith Yamasani | 0b28549 | 2011-04-14 17:35:23 -0700 | [diff] [blame] | 171 | /** |
| 172 | * Returns an array of user ids. This array is cached here for quick access, so do not modify or |
| 173 | * cache it elsewhere. |
| 174 | * @return the array of user ids. |
| 175 | */ |
| 176 | int[] getUserIds() { |
| 177 | return mUserIds; |
| 178 | } |
| 179 | |
Amith Yamasani | 4b2e934 | 2011-03-31 12:38:53 -0700 | [diff] [blame] | 180 | private void readUserList() { |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 181 | synchronized (mUsers) { |
| 182 | readUserListLocked(); |
| 183 | } |
| 184 | } |
| 185 | |
| 186 | private void readUserListLocked() { |
Amith Yamasani | 4b2e934 | 2011-03-31 12:38:53 -0700 | [diff] [blame] | 187 | if (!mUserListFile.exists()) { |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 188 | fallbackToSingleUserLocked(); |
Amith Yamasani | 4b2e934 | 2011-03-31 12:38:53 -0700 | [diff] [blame] | 189 | return; |
| 190 | } |
| 191 | FileInputStream fis = null; |
| 192 | try { |
| 193 | fis = new FileInputStream(mUserListFile); |
| 194 | XmlPullParser parser = Xml.newPullParser(); |
| 195 | parser.setInput(fis, null); |
| 196 | int type; |
| 197 | while ((type = parser.next()) != XmlPullParser.START_TAG |
| 198 | && type != XmlPullParser.END_DOCUMENT) { |
| 199 | ; |
| 200 | } |
| 201 | |
| 202 | if (type != XmlPullParser.START_TAG) { |
Amith Yamasani | 0b28549 | 2011-04-14 17:35:23 -0700 | [diff] [blame] | 203 | Slog.e(LOG_TAG, "Unable to read user list"); |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 204 | fallbackToSingleUserLocked(); |
Amith Yamasani | 4b2e934 | 2011-03-31 12:38:53 -0700 | [diff] [blame] | 205 | return; |
| 206 | } |
| 207 | |
| 208 | while ((type = parser.next()) != XmlPullParser.END_DOCUMENT) { |
| 209 | if (type == XmlPullParser.START_TAG && parser.getName().equals(TAG_USER)) { |
| 210 | String id = parser.getAttributeValue(null, ATTR_ID); |
| 211 | UserInfo user = readUser(Integer.parseInt(id)); |
| 212 | if (user != null) { |
| 213 | mUsers.put(user.id, user); |
| 214 | } |
| 215 | } |
| 216 | } |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 217 | updateUserIdsLocked(); |
Amith Yamasani | 4b2e934 | 2011-03-31 12:38:53 -0700 | [diff] [blame] | 218 | } catch (IOException ioe) { |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 219 | fallbackToSingleUserLocked(); |
Amith Yamasani | 4b2e934 | 2011-03-31 12:38:53 -0700 | [diff] [blame] | 220 | } catch (XmlPullParserException pe) { |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 221 | fallbackToSingleUserLocked(); |
Dianne Hackborn | bfd89b3 | 2011-12-15 18:22:54 -0800 | [diff] [blame] | 222 | } finally { |
| 223 | if (fis != null) { |
| 224 | try { |
| 225 | fis.close(); |
| 226 | } catch (IOException e) { |
| 227 | } |
| 228 | } |
Amith Yamasani | 4b2e934 | 2011-03-31 12:38:53 -0700 | [diff] [blame] | 229 | } |
| 230 | } |
| 231 | |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 232 | private void fallbackToSingleUserLocked() { |
Amith Yamasani | 4b2e934 | 2011-03-31 12:38:53 -0700 | [diff] [blame] | 233 | // Create the primary user |
Amith Yamasani | b8151ec | 2012-04-18 18:02:48 -0700 | [diff] [blame^] | 234 | UserInfo primary = new UserInfo(0, "Primary", null, |
Amith Yamasani | 4b2e934 | 2011-03-31 12:38:53 -0700 | [diff] [blame] | 235 | UserInfo.FLAG_ADMIN | UserInfo.FLAG_PRIMARY); |
| 236 | mUsers.put(0, primary); |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 237 | updateUserIdsLocked(); |
Amith Yamasani | 4b2e934 | 2011-03-31 12:38:53 -0700 | [diff] [blame] | 238 | |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 239 | writeUserListLocked(); |
| 240 | writeUserLocked(primary); |
Amith Yamasani | 4b2e934 | 2011-03-31 12:38:53 -0700 | [diff] [blame] | 241 | } |
| 242 | |
| 243 | /* |
| 244 | * Writes the user file in this format: |
| 245 | * |
| 246 | * <user flags="20039023" id="0"> |
| 247 | * <name>Primary</name> |
| 248 | * </user> |
| 249 | */ |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 250 | private void writeUserLocked(UserInfo userInfo) { |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 251 | FileOutputStream fos = null; |
Amith Yamasani | 4b2e934 | 2011-03-31 12:38:53 -0700 | [diff] [blame] | 252 | try { |
| 253 | final File mUserFile = new File(mUsersDir, userInfo.id + ".xml"); |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 254 | fos = new FileOutputStream(mUserFile); |
Amith Yamasani | 4b2e934 | 2011-03-31 12:38:53 -0700 | [diff] [blame] | 255 | final BufferedOutputStream bos = new BufferedOutputStream(fos); |
| 256 | |
| 257 | // XmlSerializer serializer = XmlUtils.serializerInstance(); |
| 258 | final XmlSerializer serializer = new FastXmlSerializer(); |
| 259 | serializer.setOutput(bos, "utf-8"); |
| 260 | serializer.startDocument(null, true); |
| 261 | serializer.setFeature("http://xmlpull.org/v1/doc/features.html#indent-output", true); |
| 262 | |
| 263 | serializer.startTag(null, TAG_USER); |
| 264 | serializer.attribute(null, ATTR_ID, Integer.toString(userInfo.id)); |
| 265 | serializer.attribute(null, ATTR_FLAGS, Integer.toString(userInfo.flags)); |
Amith Yamasani | b8151ec | 2012-04-18 18:02:48 -0700 | [diff] [blame^] | 266 | if (userInfo.iconPath != null) { |
| 267 | serializer.attribute(null, ATTR_ICON_PATH, userInfo.iconPath); |
| 268 | } |
Amith Yamasani | 4b2e934 | 2011-03-31 12:38:53 -0700 | [diff] [blame] | 269 | |
| 270 | serializer.startTag(null, TAG_NAME); |
| 271 | serializer.text(userInfo.name); |
| 272 | serializer.endTag(null, TAG_NAME); |
| 273 | |
| 274 | serializer.endTag(null, TAG_USER); |
| 275 | |
| 276 | serializer.endDocument(); |
| 277 | } catch (IOException ioe) { |
Amith Yamasani | 0b28549 | 2011-04-14 17:35:23 -0700 | [diff] [blame] | 278 | Slog.e(LOG_TAG, "Error writing user info " + userInfo.id + "\n" + ioe); |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 279 | } finally { |
| 280 | if (fos != null) { |
| 281 | try { |
| 282 | fos.close(); |
| 283 | } catch (IOException ioe) { |
| 284 | } |
| 285 | } |
Amith Yamasani | 4b2e934 | 2011-03-31 12:38:53 -0700 | [diff] [blame] | 286 | } |
| 287 | } |
| 288 | |
| 289 | /* |
| 290 | * Writes the user list file in this format: |
| 291 | * |
| 292 | * <users> |
| 293 | * <user id="0"></user> |
| 294 | * <user id="2"></user> |
| 295 | * </users> |
| 296 | */ |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 297 | private void writeUserListLocked() { |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 298 | FileOutputStream fos = null; |
Amith Yamasani | 4b2e934 | 2011-03-31 12:38:53 -0700 | [diff] [blame] | 299 | try { |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 300 | fos = new FileOutputStream(mUserListFile); |
Amith Yamasani | 4b2e934 | 2011-03-31 12:38:53 -0700 | [diff] [blame] | 301 | final BufferedOutputStream bos = new BufferedOutputStream(fos); |
| 302 | |
| 303 | // XmlSerializer serializer = XmlUtils.serializerInstance(); |
| 304 | final XmlSerializer serializer = new FastXmlSerializer(); |
| 305 | serializer.setOutput(bos, "utf-8"); |
| 306 | serializer.startDocument(null, true); |
| 307 | serializer.setFeature("http://xmlpull.org/v1/doc/features.html#indent-output", true); |
| 308 | |
| 309 | serializer.startTag(null, TAG_USERS); |
| 310 | |
| 311 | for (int i = 0; i < mUsers.size(); i++) { |
| 312 | UserInfo user = mUsers.valueAt(i); |
| 313 | serializer.startTag(null, TAG_USER); |
| 314 | serializer.attribute(null, ATTR_ID, Integer.toString(user.id)); |
| 315 | serializer.endTag(null, TAG_USER); |
Amith Yamasani | 4b2e934 | 2011-03-31 12:38:53 -0700 | [diff] [blame] | 316 | } |
| 317 | |
| 318 | serializer.endTag(null, TAG_USERS); |
| 319 | |
| 320 | serializer.endDocument(); |
| 321 | } catch (IOException ioe) { |
Amith Yamasani | 0b28549 | 2011-04-14 17:35:23 -0700 | [diff] [blame] | 322 | Slog.e(LOG_TAG, "Error writing user list"); |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 323 | } finally { |
| 324 | if (fos != null) { |
| 325 | try { |
| 326 | fos.close(); |
| 327 | } catch (IOException ioe) { |
| 328 | } |
| 329 | } |
Amith Yamasani | 4b2e934 | 2011-03-31 12:38:53 -0700 | [diff] [blame] | 330 | } |
| 331 | } |
| 332 | |
| 333 | private UserInfo readUser(int id) { |
| 334 | int flags = 0; |
| 335 | String name = null; |
Amith Yamasani | b8151ec | 2012-04-18 18:02:48 -0700 | [diff] [blame^] | 336 | String iconPath = null; |
Amith Yamasani | 4b2e934 | 2011-03-31 12:38:53 -0700 | [diff] [blame] | 337 | |
| 338 | FileInputStream fis = null; |
| 339 | try { |
| 340 | File userFile = new File(mUsersDir, Integer.toString(id) + ".xml"); |
| 341 | fis = new FileInputStream(userFile); |
| 342 | XmlPullParser parser = Xml.newPullParser(); |
| 343 | parser.setInput(fis, null); |
| 344 | int type; |
| 345 | while ((type = parser.next()) != XmlPullParser.START_TAG |
| 346 | && type != XmlPullParser.END_DOCUMENT) { |
| 347 | ; |
| 348 | } |
| 349 | |
| 350 | if (type != XmlPullParser.START_TAG) { |
Amith Yamasani | 0b28549 | 2011-04-14 17:35:23 -0700 | [diff] [blame] | 351 | Slog.e(LOG_TAG, "Unable to read user " + id); |
Amith Yamasani | 4b2e934 | 2011-03-31 12:38:53 -0700 | [diff] [blame] | 352 | return null; |
| 353 | } |
| 354 | |
| 355 | if (type == XmlPullParser.START_TAG && parser.getName().equals(TAG_USER)) { |
| 356 | String storedId = parser.getAttributeValue(null, ATTR_ID); |
| 357 | if (Integer.parseInt(storedId) != id) { |
Amith Yamasani | 0b28549 | 2011-04-14 17:35:23 -0700 | [diff] [blame] | 358 | Slog.e(LOG_TAG, "User id does not match the file name"); |
Amith Yamasani | 4b2e934 | 2011-03-31 12:38:53 -0700 | [diff] [blame] | 359 | return null; |
| 360 | } |
| 361 | String flagString = parser.getAttributeValue(null, ATTR_FLAGS); |
| 362 | flags = Integer.parseInt(flagString); |
Amith Yamasani | b8151ec | 2012-04-18 18:02:48 -0700 | [diff] [blame^] | 363 | iconPath = parser.getAttributeValue(null, ATTR_ICON_PATH); |
Amith Yamasani | 4b2e934 | 2011-03-31 12:38:53 -0700 | [diff] [blame] | 364 | |
| 365 | while ((type = parser.next()) != XmlPullParser.START_TAG |
| 366 | && type != XmlPullParser.END_DOCUMENT) { |
| 367 | } |
| 368 | if (type == XmlPullParser.START_TAG && parser.getName().equals(TAG_NAME)) { |
| 369 | type = parser.next(); |
| 370 | if (type == XmlPullParser.TEXT) { |
| 371 | name = parser.getText(); |
| 372 | } |
| 373 | } |
| 374 | } |
Amith Yamasani | 4b2e934 | 2011-03-31 12:38:53 -0700 | [diff] [blame] | 375 | |
Amith Yamasani | b8151ec | 2012-04-18 18:02:48 -0700 | [diff] [blame^] | 376 | UserInfo userInfo = new UserInfo(id, name, iconPath, flags); |
Amith Yamasani | 4b2e934 | 2011-03-31 12:38:53 -0700 | [diff] [blame] | 377 | return userInfo; |
| 378 | |
| 379 | } catch (IOException ioe) { |
| 380 | } catch (XmlPullParserException pe) { |
Dianne Hackborn | bfd89b3 | 2011-12-15 18:22:54 -0800 | [diff] [blame] | 381 | } finally { |
| 382 | if (fis != null) { |
| 383 | try { |
| 384 | fis.close(); |
| 385 | } catch (IOException e) { |
| 386 | } |
| 387 | } |
Amith Yamasani | 4b2e934 | 2011-03-31 12:38:53 -0700 | [diff] [blame] | 388 | } |
| 389 | return null; |
| 390 | } |
| 391 | |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 392 | public UserInfo createUser(String name, int flags) { |
Amith Yamasani | 0b28549 | 2011-04-14 17:35:23 -0700 | [diff] [blame] | 393 | int userId = getNextAvailableId(); |
Amith Yamasani | b8151ec | 2012-04-18 18:02:48 -0700 | [diff] [blame^] | 394 | UserInfo userInfo = new UserInfo(userId, name, null, flags); |
Amith Yamasani | 0b28549 | 2011-04-14 17:35:23 -0700 | [diff] [blame] | 395 | File userPath = new File(mBaseUserPath, Integer.toString(userId)); |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 396 | if (!createPackageFolders(userId, userPath)) { |
Amith Yamasani | 4b2e934 | 2011-03-31 12:38:53 -0700 | [diff] [blame] | 397 | return null; |
| 398 | } |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 399 | synchronized (mUsers) { |
| 400 | mUsers.put(userId, userInfo); |
| 401 | writeUserListLocked(); |
| 402 | writeUserLocked(userInfo); |
| 403 | updateUserIdsLocked(); |
| 404 | } |
Amith Yamasani | 4b2e934 | 2011-03-31 12:38:53 -0700 | [diff] [blame] | 405 | return userInfo; |
| 406 | } |
| 407 | |
Amith Yamasani | 0b28549 | 2011-04-14 17:35:23 -0700 | [diff] [blame] | 408 | /** |
| 409 | * Removes a user and all data directories created for that user. This method should be called |
| 410 | * after the user's processes have been terminated. |
| 411 | * @param id the user's id |
| 412 | */ |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 413 | public boolean removeUser(int id) { |
| 414 | synchronized (mUsers) { |
| 415 | return removeUserLocked(id); |
| 416 | } |
| 417 | } |
| 418 | |
| 419 | private boolean removeUserLocked(int id) { |
Amith Yamasani | 4b2e934 | 2011-03-31 12:38:53 -0700 | [diff] [blame] | 420 | // Remove from the list |
| 421 | UserInfo userInfo = mUsers.get(id); |
| 422 | if (userInfo != null) { |
| 423 | // Remove this user from the list |
| 424 | mUsers.remove(id); |
| 425 | // Remove user file |
| 426 | File userFile = new File(mUsersDir, id + ".xml"); |
| 427 | userFile.delete(); |
Amith Yamasani | 0b28549 | 2011-04-14 17:35:23 -0700 | [diff] [blame] | 428 | // Update the user list |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 429 | writeUserListLocked(); |
| 430 | updateUserIdsLocked(); |
| 431 | return true; |
Amith Yamasani | 4b2e934 | 2011-03-31 12:38:53 -0700 | [diff] [blame] | 432 | } |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 433 | return false; |
Amith Yamasani | 4b2e934 | 2011-03-31 12:38:53 -0700 | [diff] [blame] | 434 | } |
| 435 | |
Amith Yamasani | 0b28549 | 2011-04-14 17:35:23 -0700 | [diff] [blame] | 436 | public void installPackageForAllUsers(String packageName, int uid) { |
| 437 | for (int userId : mUserIds) { |
| 438 | // Don't do it for the primary user, it will become recursive. |
| 439 | if (userId == 0) |
| 440 | continue; |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 441 | mInstaller.createUserData(packageName, UserId.getUid(userId, uid), |
Amith Yamasani | 0b28549 | 2011-04-14 17:35:23 -0700 | [diff] [blame] | 442 | userId); |
| 443 | } |
| 444 | } |
| 445 | |
| 446 | public void clearUserDataForAllUsers(String packageName) { |
| 447 | for (int userId : mUserIds) { |
| 448 | // Don't do it for the primary user, it will become recursive. |
| 449 | if (userId == 0) |
| 450 | continue; |
| 451 | mInstaller.clearUserData(packageName, userId); |
| 452 | } |
| 453 | } |
| 454 | |
| 455 | public void removePackageForAllUsers(String packageName) { |
| 456 | for (int userId : mUserIds) { |
| 457 | // Don't do it for the primary user, it will become recursive. |
| 458 | if (userId == 0) |
| 459 | continue; |
| 460 | mInstaller.remove(packageName, userId); |
| 461 | } |
| 462 | } |
| 463 | |
| 464 | /** |
| 465 | * Caches the list of user ids in an array, adjusting the array size when necessary. |
| 466 | */ |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 467 | private void updateUserIdsLocked() { |
Amith Yamasani | 0b28549 | 2011-04-14 17:35:23 -0700 | [diff] [blame] | 468 | if (mUserIds == null || mUserIds.length != mUsers.size()) { |
| 469 | mUserIds = new int[mUsers.size()]; |
| 470 | } |
| 471 | for (int i = 0; i < mUsers.size(); i++) { |
| 472 | mUserIds[i] = mUsers.keyAt(i); |
| 473 | } |
| 474 | } |
| 475 | |
| 476 | /** |
| 477 | * 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] | 478 | * TODO: May not be a good idea to recycle ids, in case it results in confusion |
| 479 | * for data and battery stats collection, or unexpected cross-talk. |
Amith Yamasani | 0b28549 | 2011-04-14 17:35:23 -0700 | [diff] [blame] | 480 | * @return |
| 481 | */ |
Amith Yamasani | 4b2e934 | 2011-03-31 12:38:53 -0700 | [diff] [blame] | 482 | private int getNextAvailableId() { |
| 483 | int i = 0; |
| 484 | while (i < Integer.MAX_VALUE) { |
| 485 | if (mUsers.indexOfKey(i) < 0) { |
| 486 | break; |
| 487 | } |
| 488 | i++; |
| 489 | } |
| 490 | return i; |
| 491 | } |
| 492 | |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 493 | private boolean createPackageFolders(int id, File userPath) { |
Amith Yamasani | 0b28549 | 2011-04-14 17:35:23 -0700 | [diff] [blame] | 494 | // mInstaller may not be available for unit-tests. |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 495 | if (mInstaller == null) return true; |
Amith Yamasani | 0b28549 | 2011-04-14 17:35:23 -0700 | [diff] [blame] | 496 | |
Amith Yamasani | 0b28549 | 2011-04-14 17:35:23 -0700 | [diff] [blame] | 497 | // Create the user path |
| 498 | userPath.mkdir(); |
| 499 | FileUtils.setPermissions(userPath.toString(), FileUtils.S_IRWXU | FileUtils.S_IRWXG |
| 500 | | FileUtils.S_IXOTH, -1, -1); |
| 501 | |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 502 | mInstaller.cloneUserData(0, id, false); |
| 503 | |
Amith Yamasani | 4b2e934 | 2011-03-31 12:38:53 -0700 | [diff] [blame] | 504 | return true; |
| 505 | } |
| 506 | |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 507 | boolean removePackageFolders(int id) { |
Amith Yamasani | 0b28549 | 2011-04-14 17:35:23 -0700 | [diff] [blame] | 508 | // mInstaller may not be available for unit-tests. |
| 509 | if (mInstaller == null) return true; |
| 510 | |
| 511 | mInstaller.removeUserDataDirs(id); |
Amith Yamasani | 4b2e934 | 2011-03-31 12:38:53 -0700 | [diff] [blame] | 512 | return true; |
| 513 | } |
| 514 | } |