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