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