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