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