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