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