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