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