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