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; |
Amith Yamasani | 1a7472e | 2013-07-02 11:17:30 -0700 | [diff] [blame] | 24 | import android.app.ActivityThread; |
Dianne Hackborn | 80a4af2 | 2012-08-27 19:18:31 -0700 | [diff] [blame] | 25 | import android.app.IStopUserCallback; |
Amith Yamasani | db6a14c | 2012-10-17 21:16:52 -0700 | [diff] [blame] | 26 | import android.content.BroadcastReceiver; |
Amith Yamasani | 258848d | 2012-08-10 17:06:33 -0700 | [diff] [blame] | 27 | import android.content.Context; |
| 28 | import android.content.Intent; |
Amith Yamasani | 1a7472e | 2013-07-02 11:17:30 -0700 | [diff] [blame] | 29 | import android.content.pm.ApplicationInfo; |
Amith Yamasani | 0b28549 | 2011-04-14 17:35:23 -0700 | [diff] [blame] | 30 | import android.content.pm.PackageManager; |
Amith Yamasani | df2e92a | 2013-03-01 17:04:38 -0800 | [diff] [blame] | 31 | import android.content.pm.PackageManager.NameNotFoundException; |
Amith Yamasani | 4b2e934 | 2011-03-31 12:38:53 -0700 | [diff] [blame] | 32 | import android.content.pm.UserInfo; |
Amith Yamasani | e928d7d | 2012-09-17 21:46:51 -0700 | [diff] [blame] | 33 | import android.graphics.Bitmap; |
| 34 | import android.graphics.BitmapFactory; |
Amith Yamasani | 258848d | 2012-08-10 17:06:33 -0700 | [diff] [blame] | 35 | import android.os.Binder; |
Amith Yamasani | e4cf734 | 2012-12-17 11:12:09 -0800 | [diff] [blame] | 36 | import android.os.Bundle; |
Amith Yamasani | 4b2e934 | 2011-03-31 12:38:53 -0700 | [diff] [blame] | 37 | import android.os.Environment; |
| 38 | import android.os.FileUtils; |
Jeff Sharkey | ffe0cb4 | 2012-11-05 17:24:43 -0800 | [diff] [blame] | 39 | import android.os.Handler; |
Amith Yamasani | 258848d | 2012-08-10 17:06:33 -0700 | [diff] [blame] | 40 | import android.os.IUserManager; |
Amith Yamasani | 258848d | 2012-08-10 17:06:33 -0700 | [diff] [blame] | 41 | import android.os.Process; |
Dianne Hackborn | 80a4af2 | 2012-08-27 19:18:31 -0700 | [diff] [blame] | 42 | import android.os.RemoteException; |
Dianne Hackborn | f02b60a | 2012-08-16 10:48:27 -0700 | [diff] [blame] | 43 | import android.os.UserHandle; |
Jeff Sharkey | 27bd34d | 2012-09-16 12:49:00 -0700 | [diff] [blame] | 44 | import android.os.UserManager; |
Amith Yamasani | 2a00329 | 2012-08-14 18:25:45 -0700 | [diff] [blame] | 45 | import android.util.AtomicFile; |
Amith Yamasani | 655d0e2 | 2013-06-12 14:19:10 -0700 | [diff] [blame] | 46 | import android.util.Log; |
Amith Yamasani | 4b2e934 | 2011-03-31 12:38:53 -0700 | [diff] [blame] | 47 | import android.util.Slog; |
| 48 | import android.util.SparseArray; |
Jeff Sharkey | ffe0cb4 | 2012-11-05 17:24:43 -0800 | [diff] [blame] | 49 | import android.util.SparseBooleanArray; |
Amith Yamasani | 920ace0 | 2012-09-20 22:15:37 -0700 | [diff] [blame] | 50 | import android.util.TimeUtils; |
Amith Yamasani | 4b2e934 | 2011-03-31 12:38:53 -0700 | [diff] [blame] | 51 | import android.util.Xml; |
| 52 | |
Amith Yamasani | 1a7472e | 2013-07-02 11:17:30 -0700 | [diff] [blame] | 53 | import com.android.internal.content.PackageMonitor; |
Jeff Sharkey | ffe0cb4 | 2012-11-05 17:24:43 -0800 | [diff] [blame] | 54 | import com.android.internal.util.ArrayUtils; |
| 55 | import com.android.internal.util.FastXmlSerializer; |
| 56 | |
| 57 | import org.xmlpull.v1.XmlPullParser; |
| 58 | import org.xmlpull.v1.XmlPullParserException; |
| 59 | import org.xmlpull.v1.XmlSerializer; |
| 60 | |
Amith Yamasani | 4b2e934 | 2011-03-31 12:38:53 -0700 | [diff] [blame] | 61 | import java.io.BufferedOutputStream; |
| 62 | import java.io.File; |
Amith Yamasani | 920ace0 | 2012-09-20 22:15:37 -0700 | [diff] [blame] | 63 | import java.io.FileDescriptor; |
Amith Yamasani | 4b2e934 | 2011-03-31 12:38:53 -0700 | [diff] [blame] | 64 | import java.io.FileInputStream; |
Amith Yamasani | b8151ec | 2012-04-18 18:02:48 -0700 | [diff] [blame] | 65 | import java.io.FileNotFoundException; |
Amith Yamasani | 4b2e934 | 2011-03-31 12:38:53 -0700 | [diff] [blame] | 66 | import java.io.FileOutputStream; |
| 67 | import java.io.IOException; |
Amith Yamasani | 920ace0 | 2012-09-20 22:15:37 -0700 | [diff] [blame] | 68 | import java.io.PrintWriter; |
Amith Yamasani | 655d0e2 | 2013-06-12 14:19:10 -0700 | [diff] [blame] | 69 | import java.security.MessageDigest; |
| 70 | import java.security.NoSuchAlgorithmException; |
| 71 | import java.security.SecureRandom; |
Amith Yamasani | 4b2e934 | 2011-03-31 12:38:53 -0700 | [diff] [blame] | 72 | import java.util.ArrayList; |
| 73 | import java.util.List; |
| 74 | |
Amith Yamasani | 258848d | 2012-08-10 17:06:33 -0700 | [diff] [blame] | 75 | public class UserManagerService extends IUserManager.Stub { |
Amith Yamasani | b8151ec | 2012-04-18 18:02:48 -0700 | [diff] [blame] | 76 | |
Amith Yamasani | 2a00329 | 2012-08-14 18:25:45 -0700 | [diff] [blame] | 77 | private static final String LOG_TAG = "UserManagerService"; |
Amith Yamasani | b8151ec | 2012-04-18 18:02:48 -0700 | [diff] [blame] | 78 | |
Amith Yamasani | 1638931 | 2012-10-17 21:20:14 -0700 | [diff] [blame] | 79 | private static final boolean DBG = false; |
| 80 | |
Amith Yamasani | 4b2e934 | 2011-03-31 12:38:53 -0700 | [diff] [blame] | 81 | private static final String TAG_NAME = "name"; |
Amith Yamasani | 4b2e934 | 2011-03-31 12:38:53 -0700 | [diff] [blame] | 82 | private static final String ATTR_FLAGS = "flags"; |
Amith Yamasani | b8151ec | 2012-04-18 18:02:48 -0700 | [diff] [blame] | 83 | private static final String ATTR_ICON_PATH = "icon"; |
Amith Yamasani | 4b2e934 | 2011-03-31 12:38:53 -0700 | [diff] [blame] | 84 | private static final String ATTR_ID = "id"; |
Amith Yamasani | 920ace0 | 2012-09-20 22:15:37 -0700 | [diff] [blame] | 85 | private static final String ATTR_CREATION_TIME = "created"; |
| 86 | private static final String ATTR_LAST_LOGGED_IN_TIME = "lastLoggedIn"; |
Amith Yamasani | 655d0e2 | 2013-06-12 14:19:10 -0700 | [diff] [blame] | 87 | private static final String ATTR_SALT = "salt"; |
| 88 | private static final String ATTR_PIN_HASH = "pinHash"; |
| 89 | private static final String ATTR_FAILED_ATTEMPTS = "failedAttempts"; |
| 90 | private static final String ATTR_LAST_RETRY_MS = "lastAttemptMs"; |
Amith Yamasani | 2a00329 | 2012-08-14 18:25:45 -0700 | [diff] [blame] | 91 | private static final String ATTR_SERIAL_NO = "serialNumber"; |
| 92 | private static final String ATTR_NEXT_SERIAL_NO = "nextSerialNumber"; |
Dianne Hackborn | d4ac8d7 | 2012-09-27 23:20:10 -0700 | [diff] [blame] | 93 | private static final String ATTR_PARTIAL = "partial"; |
Amith Yamasani | 6f34b41 | 2012-10-22 18:19:27 -0700 | [diff] [blame] | 94 | private static final String ATTR_USER_VERSION = "version"; |
Kenny Guy | 2a76494 | 2014-04-02 13:29:20 +0100 | [diff] [blame] | 95 | private static final String ATTR_PROFILE_GROUP_ID = "profileGroupId"; |
Amith Yamasani | 4b2e934 | 2011-03-31 12:38:53 -0700 | [diff] [blame] | 96 | private static final String TAG_USERS = "users"; |
Amith Yamasani | 4b2e934 | 2011-03-31 12:38:53 -0700 | [diff] [blame] | 97 | private static final String TAG_USER = "user"; |
Amith Yamasani | e4cf734 | 2012-12-17 11:12:09 -0800 | [diff] [blame] | 98 | private static final String TAG_RESTRICTIONS = "restrictions"; |
Amith Yamasani | df2e92a | 2013-03-01 17:04:38 -0800 | [diff] [blame] | 99 | private static final String TAG_ENTRY = "entry"; |
| 100 | private static final String TAG_VALUE = "value"; |
| 101 | private static final String ATTR_KEY = "key"; |
Amith Yamasani | 7e99bc0 | 2013-04-16 18:24:51 -0700 | [diff] [blame] | 102 | private static final String ATTR_VALUE_TYPE = "type"; |
Amith Yamasani | df2e92a | 2013-03-01 17:04:38 -0800 | [diff] [blame] | 103 | private static final String ATTR_MULTIPLE = "m"; |
Amith Yamasani | 4b2e934 | 2011-03-31 12:38:53 -0700 | [diff] [blame] | 104 | |
Amith Yamasani | 7e99bc0 | 2013-04-16 18:24:51 -0700 | [diff] [blame] | 105 | private static final String ATTR_TYPE_STRING_ARRAY = "sa"; |
| 106 | private static final String ATTR_TYPE_STRING = "s"; |
| 107 | private static final String ATTR_TYPE_BOOLEAN = "b"; |
| 108 | |
Amith Yamasani | 0b28549 | 2011-04-14 17:35:23 -0700 | [diff] [blame] | 109 | private static final String USER_INFO_DIR = "system" + File.separator + "users"; |
Amith Yamasani | 4b2e934 | 2011-03-31 12:38:53 -0700 | [diff] [blame] | 110 | private static final String USER_LIST_FILENAME = "userlist.xml"; |
Amith Yamasani | b8151ec | 2012-04-18 18:02:48 -0700 | [diff] [blame] | 111 | private static final String USER_PHOTO_FILENAME = "photo.png"; |
Amith Yamasani | 4b2e934 | 2011-03-31 12:38:53 -0700 | [diff] [blame] | 112 | |
Amith Yamasani | df2e92a | 2013-03-01 17:04:38 -0800 | [diff] [blame] | 113 | private static final String RESTRICTIONS_FILE_PREFIX = "res_"; |
Amith Yamasani | fc95e70 | 2013-09-26 13:20:17 -0700 | [diff] [blame] | 114 | private static final String XML_SUFFIX = ".xml"; |
Amith Yamasani | df2e92a | 2013-03-01 17:04:38 -0800 | [diff] [blame] | 115 | |
Amith Yamasani | 634cf31 | 2012-10-04 17:34:21 -0700 | [diff] [blame] | 116 | private static final int MIN_USER_ID = 10; |
| 117 | |
Amith Yamasani | 5e486f5 | 2013-08-07 11:06:44 -0700 | [diff] [blame] | 118 | private static final int USER_VERSION = 4; |
Amith Yamasani | 6f34b41 | 2012-10-22 18:19:27 -0700 | [diff] [blame] | 119 | |
Amith Yamasani | 920ace0 | 2012-09-20 22:15:37 -0700 | [diff] [blame] | 120 | private static final long EPOCH_PLUS_30_YEARS = 30L * 365 * 24 * 60 * 60 * 1000L; // ms |
| 121 | |
Amith Yamasani | 655d0e2 | 2013-06-12 14:19:10 -0700 | [diff] [blame] | 122 | // Number of attempts before jumping to the next BACKOFF_TIMES slot |
| 123 | private static final int BACKOFF_INC_INTERVAL = 5; |
| 124 | |
| 125 | // Amount of time to force the user to wait before entering the PIN again, after failing |
| 126 | // BACKOFF_INC_INTERVAL times. |
| 127 | private static final int[] BACKOFF_TIMES = { 0, 30*1000, 60*1000, 5*60*1000, 30*60*1000 }; |
| 128 | |
Dianne Hackborn | 4428e17 | 2012-08-24 17:43:05 -0700 | [diff] [blame] | 129 | private final Context mContext; |
| 130 | private final PackageManagerService mPm; |
| 131 | private final Object mInstallLock; |
| 132 | private final Object mPackagesLock; |
Amith Yamasani | 4b2e934 | 2011-03-31 12:38:53 -0700 | [diff] [blame] | 133 | |
Jeff Sharkey | ffe0cb4 | 2012-11-05 17:24:43 -0800 | [diff] [blame] | 134 | private final Handler mHandler; |
| 135 | |
Amith Yamasani | 4b2e934 | 2011-03-31 12:38:53 -0700 | [diff] [blame] | 136 | private final File mUsersDir; |
| 137 | private final File mUserListFile; |
Dianne Hackborn | 4428e17 | 2012-08-24 17:43:05 -0700 | [diff] [blame] | 138 | private final File mBaseUserPath; |
| 139 | |
Jeff Sharkey | ffe0cb4 | 2012-11-05 17:24:43 -0800 | [diff] [blame] | 140 | private final SparseArray<UserInfo> mUsers = new SparseArray<UserInfo>(); |
Amith Yamasani | e4cf734 | 2012-12-17 11:12:09 -0800 | [diff] [blame] | 141 | private final SparseArray<Bundle> mUserRestrictions = new SparseArray<Bundle>(); |
Jeff Sharkey | ffe0cb4 | 2012-11-05 17:24:43 -0800 | [diff] [blame] | 142 | |
Amith Yamasani | 655d0e2 | 2013-06-12 14:19:10 -0700 | [diff] [blame] | 143 | class RestrictionsPinState { |
| 144 | long salt; |
| 145 | String pinHash; |
| 146 | int failedAttempts; |
| 147 | long lastAttemptTime; |
| 148 | } |
| 149 | |
| 150 | private final SparseArray<RestrictionsPinState> mRestrictionsPinStates = |
| 151 | new SparseArray<RestrictionsPinState>(); |
| 152 | |
Jeff Sharkey | ffe0cb4 | 2012-11-05 17:24:43 -0800 | [diff] [blame] | 153 | /** |
| 154 | * Set of user IDs being actively removed. Removed IDs linger in this set |
| 155 | * for several seconds to work around a VFS caching issue. |
| 156 | */ |
| 157 | // @GuardedBy("mPackagesLock") |
| 158 | private final SparseBooleanArray mRemovingUserIds = new SparseBooleanArray(); |
Dianne Hackborn | 4428e17 | 2012-08-24 17:43:05 -0700 | [diff] [blame] | 159 | |
Amith Yamasani | 0b28549 | 2011-04-14 17:35:23 -0700 | [diff] [blame] | 160 | private int[] mUserIds; |
Amith Yamasani | 258848d | 2012-08-10 17:06:33 -0700 | [diff] [blame] | 161 | private boolean mGuestEnabled; |
Amith Yamasani | 2a00329 | 2012-08-14 18:25:45 -0700 | [diff] [blame] | 162 | private int mNextSerialNumber; |
Amith Yamasani | 6f34b41 | 2012-10-22 18:19:27 -0700 | [diff] [blame] | 163 | private int mUserVersion = 0; |
Amith Yamasani | 0b28549 | 2011-04-14 17:35:23 -0700 | [diff] [blame] | 164 | |
Amith Yamasani | 258848d | 2012-08-10 17:06:33 -0700 | [diff] [blame] | 165 | private static UserManagerService sInstance; |
Amith Yamasani | 258848d | 2012-08-10 17:06:33 -0700 | [diff] [blame] | 166 | |
Dianne Hackborn | 4428e17 | 2012-08-24 17:43:05 -0700 | [diff] [blame] | 167 | public static UserManagerService getInstance() { |
| 168 | synchronized (UserManagerService.class) { |
| 169 | return sInstance; |
Amith Yamasani | 258848d | 2012-08-10 17:06:33 -0700 | [diff] [blame] | 170 | } |
Amith Yamasani | 258848d | 2012-08-10 17:06:33 -0700 | [diff] [blame] | 171 | } |
Amith Yamasani | 4b2e934 | 2011-03-31 12:38:53 -0700 | [diff] [blame] | 172 | |
| 173 | /** |
| 174 | * Available for testing purposes. |
| 175 | */ |
Amith Yamasani | 258848d | 2012-08-10 17:06:33 -0700 | [diff] [blame] | 176 | UserManagerService(File dataDir, File baseUserPath) { |
Dianne Hackborn | 4428e17 | 2012-08-24 17:43:05 -0700 | [diff] [blame] | 177 | this(null, null, new Object(), new Object(), dataDir, baseUserPath); |
Amith Yamasani | 4b2e934 | 2011-03-31 12:38:53 -0700 | [diff] [blame] | 178 | } |
| 179 | |
Dianne Hackborn | 4428e17 | 2012-08-24 17:43:05 -0700 | [diff] [blame] | 180 | /** |
| 181 | * Called by package manager to create the service. This is closely |
| 182 | * associated with the package manager, and the given lock is the |
| 183 | * package manager's own lock. |
| 184 | */ |
| 185 | UserManagerService(Context context, PackageManagerService pm, |
| 186 | Object installLock, Object packagesLock) { |
| 187 | this(context, pm, installLock, packagesLock, |
| 188 | Environment.getDataDirectory(), |
| 189 | new File(Environment.getDataDirectory(), "user")); |
Amith Yamasani | 4b2e934 | 2011-03-31 12:38:53 -0700 | [diff] [blame] | 190 | } |
| 191 | |
Dianne Hackborn | 4428e17 | 2012-08-24 17:43:05 -0700 | [diff] [blame] | 192 | /** |
| 193 | * Available for testing purposes. |
| 194 | */ |
| 195 | private UserManagerService(Context context, PackageManagerService pm, |
| 196 | Object installLock, Object packagesLock, |
| 197 | File dataDir, File baseUserPath) { |
Dianne Hackborn | d4ac8d7 | 2012-09-27 23:20:10 -0700 | [diff] [blame] | 198 | mContext = context; |
| 199 | mPm = pm; |
| 200 | mInstallLock = installLock; |
| 201 | mPackagesLock = packagesLock; |
Jeff Sharkey | ffe0cb4 | 2012-11-05 17:24:43 -0800 | [diff] [blame] | 202 | mHandler = new Handler(); |
Dianne Hackborn | d4ac8d7 | 2012-09-27 23:20:10 -0700 | [diff] [blame] | 203 | synchronized (mInstallLock) { |
| 204 | synchronized (mPackagesLock) { |
| 205 | mUsersDir = new File(dataDir, USER_INFO_DIR); |
| 206 | mUsersDir.mkdirs(); |
| 207 | // Make zeroth user directory, for services to migrate their files to that location |
| 208 | File userZeroDir = new File(mUsersDir, "0"); |
| 209 | userZeroDir.mkdirs(); |
| 210 | mBaseUserPath = baseUserPath; |
| 211 | FileUtils.setPermissions(mUsersDir.toString(), |
| 212 | FileUtils.S_IRWXU|FileUtils.S_IRWXG |
| 213 | |FileUtils.S_IROTH|FileUtils.S_IXOTH, |
| 214 | -1, -1); |
| 215 | mUserListFile = new File(mUsersDir, USER_LIST_FILENAME); |
| 216 | readUserListLocked(); |
Amith Yamasani | 756901d | 2012-10-12 12:30:07 -0700 | [diff] [blame] | 217 | // Prune out any partially created/partially removed users. |
Dianne Hackborn | d4ac8d7 | 2012-09-27 23:20:10 -0700 | [diff] [blame] | 218 | ArrayList<UserInfo> partials = new ArrayList<UserInfo>(); |
| 219 | for (int i = 0; i < mUsers.size(); i++) { |
| 220 | UserInfo ui = mUsers.valueAt(i); |
| 221 | if (ui.partial && i != 0) { |
| 222 | partials.add(ui); |
| 223 | } |
| 224 | } |
| 225 | for (int i = 0; i < partials.size(); i++) { |
| 226 | UserInfo ui = partials.get(i); |
| 227 | Slog.w(LOG_TAG, "Removing partially created user #" + i |
| 228 | + " (name=" + ui.name + ")"); |
| 229 | removeUserStateLocked(ui.id); |
| 230 | } |
| 231 | sInstance = this; |
| 232 | } |
Dianne Hackborn | 4428e17 | 2012-08-24 17:43:05 -0700 | [diff] [blame] | 233 | } |
Amith Yamasani | 1a7472e | 2013-07-02 11:17:30 -0700 | [diff] [blame] | 234 | } |
| 235 | |
| 236 | void systemReady() { |
Dianne Hackborn | 10ad982 | 2014-03-17 11:28:36 -0700 | [diff] [blame] | 237 | mUserPackageMonitor.register(mContext, null, UserHandle.ALL, false); |
Amith Yamasani | 1a7472e | 2013-07-02 11:17:30 -0700 | [diff] [blame] | 238 | userForeground(UserHandle.USER_OWNER); |
Amith Yamasani | 258848d | 2012-08-10 17:06:33 -0700 | [diff] [blame] | 239 | } |
| 240 | |
| 241 | @Override |
Amith Yamasani | 920ace0 | 2012-09-20 22:15:37 -0700 | [diff] [blame] | 242 | public List<UserInfo> getUsers(boolean excludeDying) { |
Amith Yamasani | 2a00329 | 2012-08-14 18:25:45 -0700 | [diff] [blame] | 243 | checkManageUsersPermission("query users"); |
Dianne Hackborn | 4428e17 | 2012-08-24 17:43:05 -0700 | [diff] [blame] | 244 | synchronized (mPackagesLock) { |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 245 | ArrayList<UserInfo> users = new ArrayList<UserInfo>(mUsers.size()); |
| 246 | for (int i = 0; i < mUsers.size(); i++) { |
Dianne Hackborn | d4ac8d7 | 2012-09-27 23:20:10 -0700 | [diff] [blame] | 247 | UserInfo ui = mUsers.valueAt(i); |
| 248 | if (ui.partial) { |
| 249 | continue; |
| 250 | } |
Jeff Sharkey | ffe0cb4 | 2012-11-05 17:24:43 -0800 | [diff] [blame] | 251 | if (!excludeDying || !mRemovingUserIds.get(ui.id)) { |
Dianne Hackborn | d4ac8d7 | 2012-09-27 23:20:10 -0700 | [diff] [blame] | 252 | users.add(ui); |
Amith Yamasani | 920ace0 | 2012-09-20 22:15:37 -0700 | [diff] [blame] | 253 | } |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 254 | } |
| 255 | return users; |
Amith Yamasani | 4b2e934 | 2011-03-31 12:38:53 -0700 | [diff] [blame] | 256 | } |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 257 | } |
| 258 | |
Amith Yamasani | 258848d | 2012-08-10 17:06:33 -0700 | [diff] [blame] | 259 | @Override |
Alexandra Gherghina | 385124d | 2014-04-03 13:37:39 +0100 | [diff] [blame] | 260 | public List<UserInfo> getProfiles(int userId, boolean enabledOnly) { |
Amith Yamasani | 4f58263 | 2014-02-19 14:31:52 -0800 | [diff] [blame] | 261 | if (userId != UserHandle.getCallingUserId()) { |
| 262 | checkManageUsersPermission("getting profiles related to user " + userId); |
| 263 | } |
Amith Yamasani | be46532 | 2014-04-24 13:45:17 -0700 | [diff] [blame] | 264 | final long ident = Binder.clearCallingIdentity(); |
| 265 | try { |
| 266 | synchronized (mPackagesLock) { |
| 267 | return getProfilesLocked(userId, enabledOnly); |
Alexandra Gherghina | 385124d | 2014-04-03 13:37:39 +0100 | [diff] [blame] | 268 | } |
Amith Yamasani | be46532 | 2014-04-24 13:45:17 -0700 | [diff] [blame] | 269 | } finally { |
| 270 | Binder.restoreCallingIdentity(ident); |
Kenny Guy | a52dc3e | 2014-02-11 15:33:14 +0000 | [diff] [blame] | 271 | } |
| 272 | } |
| 273 | |
Amith Yamasani | be46532 | 2014-04-24 13:45:17 -0700 | [diff] [blame] | 274 | /** Assume permissions already checked and caller's identity cleared */ |
| 275 | private List<UserInfo> getProfilesLocked(int userId, boolean enabledOnly) { |
Amith Yamasani | be46532 | 2014-04-24 13:45:17 -0700 | [diff] [blame] | 276 | UserInfo user = getUserInfoLocked(userId); |
| 277 | ArrayList<UserInfo> users = new ArrayList<UserInfo>(mUsers.size()); |
| 278 | for (int i = 0; i < mUsers.size(); i++) { |
| 279 | UserInfo profile = mUsers.valueAt(i); |
| 280 | if (!isProfileOf(user, profile)) { |
| 281 | continue; |
| 282 | } |
Alexandra Gherghina | df35d57 | 2014-04-09 13:54:39 +0100 | [diff] [blame] | 283 | if (enabledOnly && !profile.isEnabled()) { |
| 284 | continue; |
Amith Yamasani | be46532 | 2014-04-24 13:45:17 -0700 | [diff] [blame] | 285 | } |
| 286 | users.add(profile); |
| 287 | } |
| 288 | return users; |
| 289 | } |
| 290 | |
Kenny Guy | 2a76494 | 2014-04-02 13:29:20 +0100 | [diff] [blame] | 291 | private boolean isProfileOf(UserInfo user, UserInfo profile) { |
| 292 | return user.id == profile.id || |
| 293 | (user.profileGroupId != UserInfo.NO_PROFILE_GROUP_ID |
| 294 | && user.profileGroupId == profile.profileGroupId); |
Kenny Guy | a52dc3e | 2014-02-11 15:33:14 +0000 | [diff] [blame] | 295 | } |
| 296 | |
| 297 | @Override |
Alexandra Gherghina | df35d57 | 2014-04-09 13:54:39 +0100 | [diff] [blame] | 298 | public void setUserEnabled(int userId) { |
| 299 | checkManageUsersPermission("enable user"); |
| 300 | synchronized (mPackagesLock) { |
| 301 | UserInfo info = getUserInfoLocked(userId); |
| 302 | if (info != null && !info.isEnabled()) { |
| 303 | info.flags ^= UserInfo.FLAG_DISABLED; |
| 304 | writeUserLocked(info); |
| 305 | } |
| 306 | } |
| 307 | } |
| 308 | |
| 309 | @Override |
Amith Yamasani | 258848d | 2012-08-10 17:06:33 -0700 | [diff] [blame] | 310 | public UserInfo getUserInfo(int userId) { |
Amith Yamasani | 2a00329 | 2012-08-14 18:25:45 -0700 | [diff] [blame] | 311 | checkManageUsersPermission("query user"); |
Dianne Hackborn | 4428e17 | 2012-08-24 17:43:05 -0700 | [diff] [blame] | 312 | synchronized (mPackagesLock) { |
Amith Yamasani | 19526374 | 2012-08-21 15:40:12 -0700 | [diff] [blame] | 313 | return getUserInfoLocked(userId); |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 314 | } |
| 315 | } |
| 316 | |
Amith Yamasani | 71e6c69 | 2013-03-24 17:39:28 -0700 | [diff] [blame] | 317 | @Override |
| 318 | public boolean isRestricted() { |
| 319 | synchronized (mPackagesLock) { |
| 320 | return getUserInfoLocked(UserHandle.getCallingUserId()).isRestricted(); |
| 321 | } |
| 322 | } |
| 323 | |
Amith Yamasani | 19526374 | 2012-08-21 15:40:12 -0700 | [diff] [blame] | 324 | /* |
| 325 | * Should be locked on mUsers before calling this. |
| 326 | */ |
| 327 | private UserInfo getUserInfoLocked(int userId) { |
Dianne Hackborn | d4ac8d7 | 2012-09-27 23:20:10 -0700 | [diff] [blame] | 328 | UserInfo ui = mUsers.get(userId); |
Amith Yamasani | 1638931 | 2012-10-17 21:20:14 -0700 | [diff] [blame] | 329 | // 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] | 330 | if (ui != null && ui.partial && !mRemovingUserIds.get(userId)) { |
Dianne Hackborn | d4ac8d7 | 2012-09-27 23:20:10 -0700 | [diff] [blame] | 331 | Slog.w(LOG_TAG, "getUserInfo: unknown user #" + userId); |
| 332 | return null; |
| 333 | } |
| 334 | return ui; |
Amith Yamasani | 19526374 | 2012-08-21 15:40:12 -0700 | [diff] [blame] | 335 | } |
| 336 | |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 337 | public boolean exists(int userId) { |
Dianne Hackborn | 4428e17 | 2012-08-24 17:43:05 -0700 | [diff] [blame] | 338 | synchronized (mPackagesLock) { |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 339 | return ArrayUtils.contains(mUserIds, userId); |
| 340 | } |
| 341 | } |
| 342 | |
Amith Yamasani | 258848d | 2012-08-10 17:06:33 -0700 | [diff] [blame] | 343 | @Override |
Amith Yamasani | b8151ec | 2012-04-18 18:02:48 -0700 | [diff] [blame] | 344 | public void setUserName(int userId, String name) { |
Amith Yamasani | 2a00329 | 2012-08-14 18:25:45 -0700 | [diff] [blame] | 345 | checkManageUsersPermission("rename users"); |
Dianne Hackborn | d4ac8d7 | 2012-09-27 23:20:10 -0700 | [diff] [blame] | 346 | boolean changed = false; |
Dianne Hackborn | 4428e17 | 2012-08-24 17:43:05 -0700 | [diff] [blame] | 347 | synchronized (mPackagesLock) { |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 348 | UserInfo info = mUsers.get(userId); |
Dianne Hackborn | d4ac8d7 | 2012-09-27 23:20:10 -0700 | [diff] [blame] | 349 | if (info == null || info.partial) { |
| 350 | Slog.w(LOG_TAG, "setUserName: unknown user #" + userId); |
| 351 | return; |
| 352 | } |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 353 | if (name != null && !name.equals(info.name)) { |
| 354 | info.name = name; |
| 355 | writeUserLocked(info); |
Dianne Hackborn | d4ac8d7 | 2012-09-27 23:20:10 -0700 | [diff] [blame] | 356 | changed = true; |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 357 | } |
| 358 | } |
Dianne Hackborn | d4ac8d7 | 2012-09-27 23:20:10 -0700 | [diff] [blame] | 359 | if (changed) { |
| 360 | sendUserInfoChangedBroadcast(userId); |
| 361 | } |
Amith Yamasani | 4b2e934 | 2011-03-31 12:38:53 -0700 | [diff] [blame] | 362 | } |
| 363 | |
Amith Yamasani | 258848d | 2012-08-10 17:06:33 -0700 | [diff] [blame] | 364 | @Override |
Amith Yamasani | e928d7d | 2012-09-17 21:46:51 -0700 | [diff] [blame] | 365 | public void setUserIcon(int userId, Bitmap bitmap) { |
Amith Yamasani | 2a00329 | 2012-08-14 18:25:45 -0700 | [diff] [blame] | 366 | checkManageUsersPermission("update users"); |
Dianne Hackborn | 4428e17 | 2012-08-24 17:43:05 -0700 | [diff] [blame] | 367 | synchronized (mPackagesLock) { |
Amith Yamasani | b8151ec | 2012-04-18 18:02:48 -0700 | [diff] [blame] | 368 | UserInfo info = mUsers.get(userId); |
Dianne Hackborn | d4ac8d7 | 2012-09-27 23:20:10 -0700 | [diff] [blame] | 369 | if (info == null || info.partial) { |
| 370 | Slog.w(LOG_TAG, "setUserIcon: unknown user #" + userId); |
| 371 | return; |
| 372 | } |
Amith Yamasani | e928d7d | 2012-09-17 21:46:51 -0700 | [diff] [blame] | 373 | writeBitmapLocked(info, bitmap); |
| 374 | writeUserLocked(info); |
Amith Yamasani | b8151ec | 2012-04-18 18:02:48 -0700 | [diff] [blame] | 375 | } |
Amith Yamasani | e928d7d | 2012-09-17 21:46:51 -0700 | [diff] [blame] | 376 | sendUserInfoChangedBroadcast(userId); |
| 377 | } |
| 378 | |
| 379 | private void sendUserInfoChangedBroadcast(int userId) { |
| 380 | Intent changedIntent = new Intent(Intent.ACTION_USER_INFO_CHANGED); |
| 381 | changedIntent.putExtra(Intent.EXTRA_USER_HANDLE, userId); |
| 382 | changedIntent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY); |
Amith Yamasani | 6fc1d4e | 2013-05-08 16:43:58 -0700 | [diff] [blame] | 383 | mContext.sendBroadcastAsUser(changedIntent, UserHandle.ALL); |
Amith Yamasani | b8151ec | 2012-04-18 18:02:48 -0700 | [diff] [blame] | 384 | } |
| 385 | |
Amith Yamasani | 258848d | 2012-08-10 17:06:33 -0700 | [diff] [blame] | 386 | @Override |
Amith Yamasani | e928d7d | 2012-09-17 21:46:51 -0700 | [diff] [blame] | 387 | public Bitmap getUserIcon(int userId) { |
Amith Yamasani | 3b49f07 | 2012-09-17 10:21:43 -0700 | [diff] [blame] | 388 | checkManageUsersPermission("read users"); |
| 389 | synchronized (mPackagesLock) { |
| 390 | UserInfo info = mUsers.get(userId); |
Dianne Hackborn | d4ac8d7 | 2012-09-27 23:20:10 -0700 | [diff] [blame] | 391 | if (info == null || info.partial) { |
| 392 | Slog.w(LOG_TAG, "getUserIcon: unknown user #" + userId); |
| 393 | return null; |
| 394 | } |
| 395 | if (info.iconPath == null) { |
| 396 | return null; |
| 397 | } |
Amith Yamasani | e928d7d | 2012-09-17 21:46:51 -0700 | [diff] [blame] | 398 | return BitmapFactory.decodeFile(info.iconPath); |
Amith Yamasani | 3b49f07 | 2012-09-17 10:21:43 -0700 | [diff] [blame] | 399 | } |
| 400 | } |
| 401 | |
| 402 | @Override |
Amith Yamasani | 258848d | 2012-08-10 17:06:33 -0700 | [diff] [blame] | 403 | public void setGuestEnabled(boolean enable) { |
Amith Yamasani | 2a00329 | 2012-08-14 18:25:45 -0700 | [diff] [blame] | 404 | checkManageUsersPermission("enable guest users"); |
Dianne Hackborn | 4428e17 | 2012-08-24 17:43:05 -0700 | [diff] [blame] | 405 | synchronized (mPackagesLock) { |
Amith Yamasani | 258848d | 2012-08-10 17:06:33 -0700 | [diff] [blame] | 406 | if (mGuestEnabled != enable) { |
| 407 | mGuestEnabled = enable; |
| 408 | // Erase any guest user that currently exists |
| 409 | for (int i = 0; i < mUsers.size(); i++) { |
| 410 | UserInfo user = mUsers.valueAt(i); |
Dianne Hackborn | d4ac8d7 | 2012-09-27 23:20:10 -0700 | [diff] [blame] | 411 | if (!user.partial && user.isGuest()) { |
Amith Yamasani | 258848d | 2012-08-10 17:06:33 -0700 | [diff] [blame] | 412 | if (!enable) { |
| 413 | removeUser(user.id); |
| 414 | } |
| 415 | return; |
| 416 | } |
| 417 | } |
| 418 | // No guest was found |
| 419 | if (enable) { |
| 420 | createUser("Guest", UserInfo.FLAG_GUEST); |
| 421 | } |
| 422 | } |
| 423 | } |
| 424 | } |
| 425 | |
| 426 | @Override |
| 427 | public boolean isGuestEnabled() { |
Dianne Hackborn | 4428e17 | 2012-08-24 17:43:05 -0700 | [diff] [blame] | 428 | synchronized (mPackagesLock) { |
Amith Yamasani | 258848d | 2012-08-10 17:06:33 -0700 | [diff] [blame] | 429 | return mGuestEnabled; |
| 430 | } |
| 431 | } |
| 432 | |
| 433 | @Override |
| 434 | public void wipeUser(int userHandle) { |
Amith Yamasani | 2a00329 | 2012-08-14 18:25:45 -0700 | [diff] [blame] | 435 | checkManageUsersPermission("wipe user"); |
Amith Yamasani | 258848d | 2012-08-10 17:06:33 -0700 | [diff] [blame] | 436 | // TODO: |
| 437 | } |
| 438 | |
Dianne Hackborn | 5dc5a00 | 2012-09-15 19:33:48 -0700 | [diff] [blame] | 439 | public void makeInitialized(int userId) { |
| 440 | checkManageUsersPermission("makeInitialized"); |
| 441 | synchronized (mPackagesLock) { |
| 442 | UserInfo info = mUsers.get(userId); |
Dianne Hackborn | d4ac8d7 | 2012-09-27 23:20:10 -0700 | [diff] [blame] | 443 | if (info == null || info.partial) { |
| 444 | Slog.w(LOG_TAG, "makeInitialized: unknown user #" + userId); |
| 445 | } |
| 446 | if ((info.flags&UserInfo.FLAG_INITIALIZED) == 0) { |
Dianne Hackborn | 5dc5a00 | 2012-09-15 19:33:48 -0700 | [diff] [blame] | 447 | info.flags |= UserInfo.FLAG_INITIALIZED; |
| 448 | writeUserLocked(info); |
| 449 | } |
| 450 | } |
| 451 | } |
| 452 | |
Amith Yamasani | e4cf734 | 2012-12-17 11:12:09 -0800 | [diff] [blame] | 453 | @Override |
| 454 | public Bundle getUserRestrictions(int userId) { |
| 455 | // checkManageUsersPermission("getUserRestrictions"); |
| 456 | |
| 457 | synchronized (mPackagesLock) { |
| 458 | Bundle restrictions = mUserRestrictions.get(userId); |
Amith Yamasani | be46532 | 2014-04-24 13:45:17 -0700 | [diff] [blame] | 459 | return restrictions != null ? new Bundle(restrictions) : new Bundle(); |
Amith Yamasani | e4cf734 | 2012-12-17 11:12:09 -0800 | [diff] [blame] | 460 | } |
| 461 | } |
| 462 | |
| 463 | @Override |
| 464 | public void setUserRestrictions(Bundle restrictions, int userId) { |
Amith Yamasani | be46532 | 2014-04-24 13:45:17 -0700 | [diff] [blame] | 465 | checkManageUsersPermission("setUserRestrictions"); |
Amith Yamasani | 0343ec3 | 2013-07-22 14:52:06 -0700 | [diff] [blame] | 466 | if (restrictions == null) return; |
Amith Yamasani | e4cf734 | 2012-12-17 11:12:09 -0800 | [diff] [blame] | 467 | |
| 468 | synchronized (mPackagesLock) { |
Amith Yamasani | 350962c | 2013-08-06 11:18:53 -0700 | [diff] [blame] | 469 | mUserRestrictions.get(userId).clear(); |
Amith Yamasani | e4cf734 | 2012-12-17 11:12:09 -0800 | [diff] [blame] | 470 | mUserRestrictions.get(userId).putAll(restrictions); |
| 471 | writeUserLocked(mUsers.get(userId)); |
| 472 | } |
| 473 | } |
| 474 | |
Amith Yamasani | 258848d | 2012-08-10 17:06:33 -0700 | [diff] [blame] | 475 | /** |
Amith Yamasani | faea76f | 2012-09-11 10:59:48 -0700 | [diff] [blame] | 476 | * Check if we've hit the limit of how many users can be created. |
| 477 | */ |
Dianne Hackborn | 5dc5a00 | 2012-09-15 19:33:48 -0700 | [diff] [blame] | 478 | private boolean isUserLimitReachedLocked() { |
| 479 | int nUsers = mUsers.size(); |
Jeff Sharkey | 27bd34d | 2012-09-16 12:49:00 -0700 | [diff] [blame] | 480 | return nUsers >= UserManager.getMaxSupportedUsers(); |
Amith Yamasani | faea76f | 2012-09-11 10:59:48 -0700 | [diff] [blame] | 481 | } |
| 482 | |
| 483 | /** |
Amith Yamasani | 19526374 | 2012-08-21 15:40:12 -0700 | [diff] [blame] | 484 | * Enforces that only the system UID or root's UID or apps that have the |
Dianne Hackborn | 10ad982 | 2014-03-17 11:28:36 -0700 | [diff] [blame] | 485 | * {@link android.Manifest.permission#MANAGE_USERS MANAGE_USERS} |
Amith Yamasani | 19526374 | 2012-08-21 15:40:12 -0700 | [diff] [blame] | 486 | * permission can make certain calls to the UserManager. |
Amith Yamasani | 258848d | 2012-08-10 17:06:33 -0700 | [diff] [blame] | 487 | * |
| 488 | * @param message used as message if SecurityException is thrown |
| 489 | * @throws SecurityException if the caller is not system or root |
| 490 | */ |
Amith Yamasani | be46532 | 2014-04-24 13:45:17 -0700 | [diff] [blame] | 491 | private static final void checkManageUsersPermission(String message) { |
Amith Yamasani | 258848d | 2012-08-10 17:06:33 -0700 | [diff] [blame] | 492 | final int uid = Binder.getCallingUid(); |
Amith Yamasani | be46532 | 2014-04-24 13:45:17 -0700 | [diff] [blame] | 493 | if (uid != Process.SYSTEM_UID && uid != 0 |
Emily Bernier | 7a2b4d1 | 2014-04-23 12:51:35 -0400 | [diff] [blame] | 494 | && ActivityManager.checkComponentPermission( |
| 495 | android.Manifest.permission.MANAGE_USERS, |
Amith Yamasani | be46532 | 2014-04-24 13:45:17 -0700 | [diff] [blame] | 496 | uid, -1, true) != PackageManager.PERMISSION_GRANTED) { |
| 497 | throw new SecurityException("You need MANAGE_USERS permission to: " + message); |
| 498 | } |
Emily Bernier | 7a2b4d1 | 2014-04-23 12:51:35 -0400 | [diff] [blame] | 499 | } |
| 500 | |
Amith Yamasani | e928d7d | 2012-09-17 21:46:51 -0700 | [diff] [blame] | 501 | private void writeBitmapLocked(UserInfo info, Bitmap bitmap) { |
Amith Yamasani | b8151ec | 2012-04-18 18:02:48 -0700 | [diff] [blame] | 502 | try { |
| 503 | File dir = new File(mUsersDir, Integer.toString(info.id)); |
| 504 | File file = new File(dir, USER_PHOTO_FILENAME); |
| 505 | if (!dir.exists()) { |
| 506 | dir.mkdir(); |
| 507 | FileUtils.setPermissions( |
| 508 | dir.getPath(), |
| 509 | FileUtils.S_IRWXU|FileUtils.S_IRWXG|FileUtils.S_IXOTH, |
| 510 | -1, -1); |
| 511 | } |
Amith Yamasani | e928d7d | 2012-09-17 21:46:51 -0700 | [diff] [blame] | 512 | FileOutputStream os; |
| 513 | if (bitmap.compress(Bitmap.CompressFormat.PNG, 100, os = new FileOutputStream(file))) { |
Amith Yamasani | 3b49f07 | 2012-09-17 10:21:43 -0700 | [diff] [blame] | 514 | info.iconPath = file.getAbsolutePath(); |
| 515 | } |
Amith Yamasani | e928d7d | 2012-09-17 21:46:51 -0700 | [diff] [blame] | 516 | try { |
| 517 | os.close(); |
| 518 | } catch (IOException ioe) { |
| 519 | // What the ... ! |
| 520 | } |
Amith Yamasani | b8151ec | 2012-04-18 18:02:48 -0700 | [diff] [blame] | 521 | } catch (FileNotFoundException e) { |
Amith Yamasani | 2a00329 | 2012-08-14 18:25:45 -0700 | [diff] [blame] | 522 | Slog.w(LOG_TAG, "Error setting photo for user ", e); |
Amith Yamasani | b8151ec | 2012-04-18 18:02:48 -0700 | [diff] [blame] | 523 | } |
Amith Yamasani | b8151ec | 2012-04-18 18:02:48 -0700 | [diff] [blame] | 524 | } |
| 525 | |
Amith Yamasani | 0b28549 | 2011-04-14 17:35:23 -0700 | [diff] [blame] | 526 | /** |
| 527 | * Returns an array of user ids. This array is cached here for quick access, so do not modify or |
| 528 | * cache it elsewhere. |
| 529 | * @return the array of user ids. |
| 530 | */ |
Dianne Hackborn | 1676c85 | 2012-09-10 14:52:30 -0700 | [diff] [blame] | 531 | public int[] getUserIds() { |
Dianne Hackborn | 4428e17 | 2012-08-24 17:43:05 -0700 | [diff] [blame] | 532 | synchronized (mPackagesLock) { |
Dianne Hackborn | 7767eac | 2012-08-23 18:25:40 -0700 | [diff] [blame] | 533 | return mUserIds; |
| 534 | } |
Amith Yamasani | 0b28549 | 2011-04-14 17:35:23 -0700 | [diff] [blame] | 535 | } |
| 536 | |
Dianne Hackborn | 4428e17 | 2012-08-24 17:43:05 -0700 | [diff] [blame] | 537 | int[] getUserIdsLPr() { |
| 538 | return mUserIds; |
| 539 | } |
| 540 | |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 541 | private void readUserListLocked() { |
Amith Yamasani | 258848d | 2012-08-10 17:06:33 -0700 | [diff] [blame] | 542 | mGuestEnabled = false; |
Amith Yamasani | 4b2e934 | 2011-03-31 12:38:53 -0700 | [diff] [blame] | 543 | if (!mUserListFile.exists()) { |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 544 | fallbackToSingleUserLocked(); |
Amith Yamasani | 4b2e934 | 2011-03-31 12:38:53 -0700 | [diff] [blame] | 545 | return; |
| 546 | } |
| 547 | FileInputStream fis = null; |
Amith Yamasani | 2a00329 | 2012-08-14 18:25:45 -0700 | [diff] [blame] | 548 | AtomicFile userListFile = new AtomicFile(mUserListFile); |
Amith Yamasani | 4b2e934 | 2011-03-31 12:38:53 -0700 | [diff] [blame] | 549 | try { |
Amith Yamasani | 2a00329 | 2012-08-14 18:25:45 -0700 | [diff] [blame] | 550 | fis = userListFile.openRead(); |
Amith Yamasani | 4b2e934 | 2011-03-31 12:38:53 -0700 | [diff] [blame] | 551 | XmlPullParser parser = Xml.newPullParser(); |
| 552 | parser.setInput(fis, null); |
| 553 | int type; |
| 554 | while ((type = parser.next()) != XmlPullParser.START_TAG |
| 555 | && type != XmlPullParser.END_DOCUMENT) { |
| 556 | ; |
| 557 | } |
| 558 | |
| 559 | if (type != XmlPullParser.START_TAG) { |
Amith Yamasani | 0b28549 | 2011-04-14 17:35:23 -0700 | [diff] [blame] | 560 | Slog.e(LOG_TAG, "Unable to read user list"); |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 561 | fallbackToSingleUserLocked(); |
Amith Yamasani | 4b2e934 | 2011-03-31 12:38:53 -0700 | [diff] [blame] | 562 | return; |
| 563 | } |
| 564 | |
Amith Yamasani | 2a00329 | 2012-08-14 18:25:45 -0700 | [diff] [blame] | 565 | mNextSerialNumber = -1; |
| 566 | if (parser.getName().equals(TAG_USERS)) { |
| 567 | String lastSerialNumber = parser.getAttributeValue(null, ATTR_NEXT_SERIAL_NO); |
| 568 | if (lastSerialNumber != null) { |
| 569 | mNextSerialNumber = Integer.parseInt(lastSerialNumber); |
| 570 | } |
Amith Yamasani | 6f34b41 | 2012-10-22 18:19:27 -0700 | [diff] [blame] | 571 | String versionNumber = parser.getAttributeValue(null, ATTR_USER_VERSION); |
| 572 | if (versionNumber != null) { |
| 573 | mUserVersion = Integer.parseInt(versionNumber); |
| 574 | } |
Amith Yamasani | 2a00329 | 2012-08-14 18:25:45 -0700 | [diff] [blame] | 575 | } |
| 576 | |
Amith Yamasani | 4b2e934 | 2011-03-31 12:38:53 -0700 | [diff] [blame] | 577 | while ((type = parser.next()) != XmlPullParser.END_DOCUMENT) { |
| 578 | if (type == XmlPullParser.START_TAG && parser.getName().equals(TAG_USER)) { |
| 579 | String id = parser.getAttributeValue(null, ATTR_ID); |
Amith Yamasani | e4cf734 | 2012-12-17 11:12:09 -0800 | [diff] [blame] | 580 | UserInfo user = readUserLocked(Integer.parseInt(id)); |
Amith Yamasani | 6f34b41 | 2012-10-22 18:19:27 -0700 | [diff] [blame] | 581 | |
Amith Yamasani | 4b2e934 | 2011-03-31 12:38:53 -0700 | [diff] [blame] | 582 | if (user != null) { |
| 583 | mUsers.put(user.id, user); |
Amith Yamasani | 2a00329 | 2012-08-14 18:25:45 -0700 | [diff] [blame] | 584 | if (user.isGuest()) { |
| 585 | mGuestEnabled = true; |
| 586 | } |
| 587 | if (mNextSerialNumber < 0 || mNextSerialNumber <= user.id) { |
| 588 | mNextSerialNumber = user.id + 1; |
| 589 | } |
Amith Yamasani | 258848d | 2012-08-10 17:06:33 -0700 | [diff] [blame] | 590 | } |
Amith Yamasani | 4b2e934 | 2011-03-31 12:38:53 -0700 | [diff] [blame] | 591 | } |
| 592 | } |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 593 | updateUserIdsLocked(); |
Amith Yamasani | 350962c | 2013-08-06 11:18:53 -0700 | [diff] [blame] | 594 | upgradeIfNecessaryLocked(); |
Amith Yamasani | 4b2e934 | 2011-03-31 12:38:53 -0700 | [diff] [blame] | 595 | } catch (IOException ioe) { |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 596 | fallbackToSingleUserLocked(); |
Amith Yamasani | 4b2e934 | 2011-03-31 12:38:53 -0700 | [diff] [blame] | 597 | } catch (XmlPullParserException pe) { |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 598 | fallbackToSingleUserLocked(); |
Dianne Hackborn | bfd89b3 | 2011-12-15 18:22:54 -0800 | [diff] [blame] | 599 | } finally { |
| 600 | if (fis != null) { |
| 601 | try { |
| 602 | fis.close(); |
| 603 | } catch (IOException e) { |
| 604 | } |
| 605 | } |
Amith Yamasani | 4b2e934 | 2011-03-31 12:38:53 -0700 | [diff] [blame] | 606 | } |
| 607 | } |
| 608 | |
Amith Yamasani | 6f34b41 | 2012-10-22 18:19:27 -0700 | [diff] [blame] | 609 | /** |
Amith Yamasani | bc962505 | 2012-11-15 14:39:18 -0800 | [diff] [blame] | 610 | * 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] | 611 | */ |
Amith Yamasani | 350962c | 2013-08-06 11:18:53 -0700 | [diff] [blame] | 612 | private void upgradeIfNecessaryLocked() { |
Amith Yamasani | 6f34b41 | 2012-10-22 18:19:27 -0700 | [diff] [blame] | 613 | int userVersion = mUserVersion; |
| 614 | if (userVersion < 1) { |
| 615 | // Assign a proper name for the owner, if not initialized correctly before |
| 616 | UserInfo user = mUsers.get(UserHandle.USER_OWNER); |
| 617 | if ("Primary".equals(user.name)) { |
| 618 | user.name = mContext.getResources().getString(com.android.internal.R.string.owner_name); |
| 619 | writeUserLocked(user); |
| 620 | } |
| 621 | userVersion = 1; |
| 622 | } |
| 623 | |
Amith Yamasani | bc962505 | 2012-11-15 14:39:18 -0800 | [diff] [blame] | 624 | if (userVersion < 2) { |
| 625 | // Owner should be marked as initialized |
| 626 | UserInfo user = mUsers.get(UserHandle.USER_OWNER); |
| 627 | if ((user.flags & UserInfo.FLAG_INITIALIZED) == 0) { |
| 628 | user.flags |= UserInfo.FLAG_INITIALIZED; |
| 629 | writeUserLocked(user); |
| 630 | } |
| 631 | userVersion = 2; |
| 632 | } |
| 633 | |
Amith Yamasani | 350962c | 2013-08-06 11:18:53 -0700 | [diff] [blame] | 634 | |
Amith Yamasani | 5e486f5 | 2013-08-07 11:06:44 -0700 | [diff] [blame] | 635 | if (userVersion < 4) { |
Amith Yamasani | 5e486f5 | 2013-08-07 11:06:44 -0700 | [diff] [blame] | 636 | userVersion = 4; |
| 637 | } |
| 638 | |
Amith Yamasani | 6f34b41 | 2012-10-22 18:19:27 -0700 | [diff] [blame] | 639 | if (userVersion < USER_VERSION) { |
| 640 | Slog.w(LOG_TAG, "User version " + mUserVersion + " didn't upgrade as expected to " |
| 641 | + USER_VERSION); |
| 642 | } else { |
| 643 | mUserVersion = userVersion; |
| 644 | writeUserListLocked(); |
| 645 | } |
| 646 | } |
| 647 | |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 648 | private void fallbackToSingleUserLocked() { |
Amith Yamasani | 4b2e934 | 2011-03-31 12:38:53 -0700 | [diff] [blame] | 649 | // Create the primary user |
Amith Yamasani | 67df64b | 2012-12-14 12:09:36 -0800 | [diff] [blame] | 650 | UserInfo primary = new UserInfo(UserHandle.USER_OWNER, |
Amith Yamasani | 6f34b41 | 2012-10-22 18:19:27 -0700 | [diff] [blame] | 651 | mContext.getResources().getString(com.android.internal.R.string.owner_name), null, |
Amith Yamasani | 756901d | 2012-10-12 12:30:07 -0700 | [diff] [blame] | 652 | UserInfo.FLAG_ADMIN | UserInfo.FLAG_PRIMARY | UserInfo.FLAG_INITIALIZED); |
Amith Yamasani | 4b2e934 | 2011-03-31 12:38:53 -0700 | [diff] [blame] | 653 | mUsers.put(0, primary); |
Amith Yamasani | 634cf31 | 2012-10-04 17:34:21 -0700 | [diff] [blame] | 654 | mNextSerialNumber = MIN_USER_ID; |
Geoffrey Borggaard | 15b8b2c | 2013-08-28 22:11:10 -0400 | [diff] [blame] | 655 | mUserVersion = USER_VERSION; |
Amith Yamasani | 67df64b | 2012-12-14 12:09:36 -0800 | [diff] [blame] | 656 | |
Geoffrey Borggaard | e45e45e3 | 2013-01-24 10:03:20 -0500 | [diff] [blame] | 657 | Bundle restrictions = new Bundle(); |
Amith Yamasani | 67df64b | 2012-12-14 12:09:36 -0800 | [diff] [blame] | 658 | mUserRestrictions.append(UserHandle.USER_OWNER, restrictions); |
| 659 | |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 660 | updateUserIdsLocked(); |
Amith Yamasani | 4b2e934 | 2011-03-31 12:38:53 -0700 | [diff] [blame] | 661 | |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 662 | writeUserListLocked(); |
| 663 | writeUserLocked(primary); |
Amith Yamasani | 4b2e934 | 2011-03-31 12:38:53 -0700 | [diff] [blame] | 664 | } |
| 665 | |
| 666 | /* |
| 667 | * Writes the user file in this format: |
| 668 | * |
| 669 | * <user flags="20039023" id="0"> |
| 670 | * <name>Primary</name> |
| 671 | * </user> |
| 672 | */ |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 673 | private void writeUserLocked(UserInfo userInfo) { |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 674 | FileOutputStream fos = null; |
Amith Yamasani | fc95e70 | 2013-09-26 13:20:17 -0700 | [diff] [blame] | 675 | AtomicFile userFile = new AtomicFile(new File(mUsersDir, userInfo.id + XML_SUFFIX)); |
Amith Yamasani | 4b2e934 | 2011-03-31 12:38:53 -0700 | [diff] [blame] | 676 | try { |
Amith Yamasani | 2a00329 | 2012-08-14 18:25:45 -0700 | [diff] [blame] | 677 | fos = userFile.startWrite(); |
Amith Yamasani | 4b2e934 | 2011-03-31 12:38:53 -0700 | [diff] [blame] | 678 | final BufferedOutputStream bos = new BufferedOutputStream(fos); |
| 679 | |
| 680 | // XmlSerializer serializer = XmlUtils.serializerInstance(); |
| 681 | final XmlSerializer serializer = new FastXmlSerializer(); |
| 682 | serializer.setOutput(bos, "utf-8"); |
| 683 | serializer.startDocument(null, true); |
| 684 | serializer.setFeature("http://xmlpull.org/v1/doc/features.html#indent-output", true); |
| 685 | |
| 686 | serializer.startTag(null, TAG_USER); |
| 687 | serializer.attribute(null, ATTR_ID, Integer.toString(userInfo.id)); |
Amith Yamasani | 2a00329 | 2012-08-14 18:25:45 -0700 | [diff] [blame] | 688 | serializer.attribute(null, ATTR_SERIAL_NO, Integer.toString(userInfo.serialNumber)); |
Amith Yamasani | 4b2e934 | 2011-03-31 12:38:53 -0700 | [diff] [blame] | 689 | serializer.attribute(null, ATTR_FLAGS, Integer.toString(userInfo.flags)); |
Amith Yamasani | 920ace0 | 2012-09-20 22:15:37 -0700 | [diff] [blame] | 690 | serializer.attribute(null, ATTR_CREATION_TIME, Long.toString(userInfo.creationTime)); |
| 691 | serializer.attribute(null, ATTR_LAST_LOGGED_IN_TIME, |
| 692 | Long.toString(userInfo.lastLoggedInTime)); |
Amith Yamasani | 655d0e2 | 2013-06-12 14:19:10 -0700 | [diff] [blame] | 693 | RestrictionsPinState pinState = mRestrictionsPinStates.get(userInfo.id); |
| 694 | if (pinState != null) { |
| 695 | if (pinState.salt != 0) { |
| 696 | serializer.attribute(null, ATTR_SALT, Long.toString(pinState.salt)); |
| 697 | } |
| 698 | if (pinState.pinHash != null) { |
| 699 | serializer.attribute(null, ATTR_PIN_HASH, pinState.pinHash); |
| 700 | } |
| 701 | if (pinState.failedAttempts != 0) { |
| 702 | serializer.attribute(null, ATTR_FAILED_ATTEMPTS, |
| 703 | Integer.toString(pinState.failedAttempts)); |
| 704 | serializer.attribute(null, ATTR_LAST_RETRY_MS, |
| 705 | Long.toString(pinState.lastAttemptTime)); |
| 706 | } |
| 707 | } |
Amith Yamasani | b8151ec | 2012-04-18 18:02:48 -0700 | [diff] [blame] | 708 | if (userInfo.iconPath != null) { |
| 709 | serializer.attribute(null, ATTR_ICON_PATH, userInfo.iconPath); |
| 710 | } |
Dianne Hackborn | d4ac8d7 | 2012-09-27 23:20:10 -0700 | [diff] [blame] | 711 | if (userInfo.partial) { |
| 712 | serializer.attribute(null, ATTR_PARTIAL, "true"); |
| 713 | } |
Kenny Guy | 2a76494 | 2014-04-02 13:29:20 +0100 | [diff] [blame] | 714 | if (userInfo.profileGroupId != UserInfo.NO_PROFILE_GROUP_ID) { |
| 715 | serializer.attribute(null, ATTR_PROFILE_GROUP_ID, |
| 716 | Integer.toString(userInfo.profileGroupId)); |
Kenny Guy | a52dc3e | 2014-02-11 15:33:14 +0000 | [diff] [blame] | 717 | } |
Amith Yamasani | 4b2e934 | 2011-03-31 12:38:53 -0700 | [diff] [blame] | 718 | |
| 719 | serializer.startTag(null, TAG_NAME); |
| 720 | serializer.text(userInfo.name); |
| 721 | serializer.endTag(null, TAG_NAME); |
| 722 | |
Amith Yamasani | e4cf734 | 2012-12-17 11:12:09 -0800 | [diff] [blame] | 723 | Bundle restrictions = mUserRestrictions.get(userInfo.id); |
| 724 | if (restrictions != null) { |
| 725 | serializer.startTag(null, TAG_RESTRICTIONS); |
Amith Yamasani | 71e6c69 | 2013-03-24 17:39:28 -0700 | [diff] [blame] | 726 | writeBoolean(serializer, restrictions, UserManager.DISALLOW_CONFIG_WIFI); |
| 727 | writeBoolean(serializer, restrictions, UserManager.DISALLOW_MODIFY_ACCOUNTS); |
| 728 | writeBoolean(serializer, restrictions, UserManager.DISALLOW_INSTALL_APPS); |
| 729 | writeBoolean(serializer, restrictions, UserManager.DISALLOW_UNINSTALL_APPS); |
| 730 | writeBoolean(serializer, restrictions, UserManager.DISALLOW_SHARE_LOCATION); |
Maggie Benthall | a12fccf | 2013-03-14 18:02:12 -0400 | [diff] [blame] | 731 | writeBoolean(serializer, restrictions, |
| 732 | UserManager.DISALLOW_INSTALL_UNKNOWN_SOURCES); |
| 733 | writeBoolean(serializer, restrictions, UserManager.DISALLOW_CONFIG_BLUETOOTH); |
| 734 | writeBoolean(serializer, restrictions, UserManager.DISALLOW_USB_FILE_TRANSFER); |
Emily Bernier | b223f73 | 2013-04-11 15:46:36 -0400 | [diff] [blame] | 735 | writeBoolean(serializer, restrictions, UserManager.DISALLOW_CONFIG_CREDENTIALS); |
| 736 | writeBoolean(serializer, restrictions, UserManager.DISALLOW_REMOVE_USER); |
Julia Reynolds | d46d0f9 | 2014-04-23 15:23:24 -0400 | [diff] [blame^] | 737 | writeBoolean(serializer, restrictions, UserManager.DISALLOW_DEBUGGING_FEATURES); |
| 738 | writeBoolean(serializer, restrictions, UserManager.DISALLOW_CONFIG_VPN); |
| 739 | writeBoolean(serializer, restrictions, UserManager.DISALLOW_CONFIG_TETHERING); |
| 740 | writeBoolean(serializer, restrictions, UserManager.DISALLOW_FACTORY_RESET); |
| 741 | writeBoolean(serializer, restrictions, UserManager.DISALLOW_ADD_USER); |
| 742 | writeBoolean(serializer, restrictions, UserManager.ENSURE_VERIFY_APPS); |
| 743 | writeBoolean(serializer, restrictions, UserManager.DISALLOW_CONFIG_CELL_BROADCASTS); |
| 744 | writeBoolean(serializer, restrictions, UserManager.DISALLOW_CONFIG_MOBILE_NETWORKS); |
| 745 | writeBoolean(serializer, restrictions, UserManager.DISALLOW_CONFIG_APPS); |
Amith Yamasani | e4cf734 | 2012-12-17 11:12:09 -0800 | [diff] [blame] | 746 | serializer.endTag(null, TAG_RESTRICTIONS); |
| 747 | } |
Amith Yamasani | 4b2e934 | 2011-03-31 12:38:53 -0700 | [diff] [blame] | 748 | serializer.endTag(null, TAG_USER); |
| 749 | |
| 750 | serializer.endDocument(); |
Amith Yamasani | 2a00329 | 2012-08-14 18:25:45 -0700 | [diff] [blame] | 751 | userFile.finishWrite(fos); |
| 752 | } catch (Exception ioe) { |
Amith Yamasani | 0b28549 | 2011-04-14 17:35:23 -0700 | [diff] [blame] | 753 | Slog.e(LOG_TAG, "Error writing user info " + userInfo.id + "\n" + ioe); |
Amith Yamasani | 2a00329 | 2012-08-14 18:25:45 -0700 | [diff] [blame] | 754 | userFile.failWrite(fos); |
Amith Yamasani | 4b2e934 | 2011-03-31 12:38:53 -0700 | [diff] [blame] | 755 | } |
| 756 | } |
| 757 | |
| 758 | /* |
| 759 | * Writes the user list file in this format: |
| 760 | * |
Amith Yamasani | 2a00329 | 2012-08-14 18:25:45 -0700 | [diff] [blame] | 761 | * <users nextSerialNumber="3"> |
Amith Yamasani | 4b2e934 | 2011-03-31 12:38:53 -0700 | [diff] [blame] | 762 | * <user id="0"></user> |
| 763 | * <user id="2"></user> |
| 764 | * </users> |
| 765 | */ |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 766 | private void writeUserListLocked() { |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 767 | FileOutputStream fos = null; |
Amith Yamasani | 2a00329 | 2012-08-14 18:25:45 -0700 | [diff] [blame] | 768 | AtomicFile userListFile = new AtomicFile(mUserListFile); |
Amith Yamasani | 4b2e934 | 2011-03-31 12:38:53 -0700 | [diff] [blame] | 769 | try { |
Amith Yamasani | 2a00329 | 2012-08-14 18:25:45 -0700 | [diff] [blame] | 770 | fos = userListFile.startWrite(); |
Amith Yamasani | 4b2e934 | 2011-03-31 12:38:53 -0700 | [diff] [blame] | 771 | final BufferedOutputStream bos = new BufferedOutputStream(fos); |
| 772 | |
| 773 | // XmlSerializer serializer = XmlUtils.serializerInstance(); |
| 774 | final XmlSerializer serializer = new FastXmlSerializer(); |
| 775 | serializer.setOutput(bos, "utf-8"); |
| 776 | serializer.startDocument(null, true); |
| 777 | serializer.setFeature("http://xmlpull.org/v1/doc/features.html#indent-output", true); |
| 778 | |
| 779 | serializer.startTag(null, TAG_USERS); |
Amith Yamasani | 2a00329 | 2012-08-14 18:25:45 -0700 | [diff] [blame] | 780 | serializer.attribute(null, ATTR_NEXT_SERIAL_NO, Integer.toString(mNextSerialNumber)); |
Amith Yamasani | 6f34b41 | 2012-10-22 18:19:27 -0700 | [diff] [blame] | 781 | serializer.attribute(null, ATTR_USER_VERSION, Integer.toString(mUserVersion)); |
Amith Yamasani | 4b2e934 | 2011-03-31 12:38:53 -0700 | [diff] [blame] | 782 | |
| 783 | for (int i = 0; i < mUsers.size(); i++) { |
| 784 | UserInfo user = mUsers.valueAt(i); |
| 785 | serializer.startTag(null, TAG_USER); |
| 786 | serializer.attribute(null, ATTR_ID, Integer.toString(user.id)); |
| 787 | serializer.endTag(null, TAG_USER); |
Amith Yamasani | 4b2e934 | 2011-03-31 12:38:53 -0700 | [diff] [blame] | 788 | } |
| 789 | |
| 790 | serializer.endTag(null, TAG_USERS); |
| 791 | |
| 792 | serializer.endDocument(); |
Amith Yamasani | 2a00329 | 2012-08-14 18:25:45 -0700 | [diff] [blame] | 793 | userListFile.finishWrite(fos); |
| 794 | } catch (Exception e) { |
| 795 | userListFile.failWrite(fos); |
Amith Yamasani | 0b28549 | 2011-04-14 17:35:23 -0700 | [diff] [blame] | 796 | Slog.e(LOG_TAG, "Error writing user list"); |
Amith Yamasani | 4b2e934 | 2011-03-31 12:38:53 -0700 | [diff] [blame] | 797 | } |
| 798 | } |
| 799 | |
Amith Yamasani | e4cf734 | 2012-12-17 11:12:09 -0800 | [diff] [blame] | 800 | private UserInfo readUserLocked(int id) { |
Amith Yamasani | 4b2e934 | 2011-03-31 12:38:53 -0700 | [diff] [blame] | 801 | int flags = 0; |
Amith Yamasani | 2a00329 | 2012-08-14 18:25:45 -0700 | [diff] [blame] | 802 | int serialNumber = id; |
Amith Yamasani | 4b2e934 | 2011-03-31 12:38:53 -0700 | [diff] [blame] | 803 | String name = null; |
Amith Yamasani | b8151ec | 2012-04-18 18:02:48 -0700 | [diff] [blame] | 804 | String iconPath = null; |
Amith Yamasani | 920ace0 | 2012-09-20 22:15:37 -0700 | [diff] [blame] | 805 | long creationTime = 0L; |
| 806 | long lastLoggedInTime = 0L; |
Amith Yamasani | 655d0e2 | 2013-06-12 14:19:10 -0700 | [diff] [blame] | 807 | long salt = 0L; |
| 808 | String pinHash = null; |
| 809 | int failedAttempts = 0; |
Kenny Guy | 2a76494 | 2014-04-02 13:29:20 +0100 | [diff] [blame] | 810 | int profileGroupId = UserInfo.NO_PROFILE_GROUP_ID; |
Amith Yamasani | 655d0e2 | 2013-06-12 14:19:10 -0700 | [diff] [blame] | 811 | long lastAttemptTime = 0L; |
Dianne Hackborn | d4ac8d7 | 2012-09-27 23:20:10 -0700 | [diff] [blame] | 812 | boolean partial = false; |
Amith Yamasani | e4cf734 | 2012-12-17 11:12:09 -0800 | [diff] [blame] | 813 | Bundle restrictions = new Bundle(); |
Amith Yamasani | 4b2e934 | 2011-03-31 12:38:53 -0700 | [diff] [blame] | 814 | |
| 815 | FileInputStream fis = null; |
| 816 | try { |
Amith Yamasani | 2a00329 | 2012-08-14 18:25:45 -0700 | [diff] [blame] | 817 | AtomicFile userFile = |
Amith Yamasani | fc95e70 | 2013-09-26 13:20:17 -0700 | [diff] [blame] | 818 | new AtomicFile(new File(mUsersDir, Integer.toString(id) + XML_SUFFIX)); |
Amith Yamasani | 2a00329 | 2012-08-14 18:25:45 -0700 | [diff] [blame] | 819 | fis = userFile.openRead(); |
Amith Yamasani | 4b2e934 | 2011-03-31 12:38:53 -0700 | [diff] [blame] | 820 | XmlPullParser parser = Xml.newPullParser(); |
| 821 | parser.setInput(fis, null); |
| 822 | int type; |
| 823 | while ((type = parser.next()) != XmlPullParser.START_TAG |
| 824 | && type != XmlPullParser.END_DOCUMENT) { |
| 825 | ; |
| 826 | } |
| 827 | |
| 828 | if (type != XmlPullParser.START_TAG) { |
Amith Yamasani | 0b28549 | 2011-04-14 17:35:23 -0700 | [diff] [blame] | 829 | Slog.e(LOG_TAG, "Unable to read user " + id); |
Amith Yamasani | 4b2e934 | 2011-03-31 12:38:53 -0700 | [diff] [blame] | 830 | return null; |
| 831 | } |
| 832 | |
| 833 | if (type == XmlPullParser.START_TAG && parser.getName().equals(TAG_USER)) { |
Amith Yamasani | 920ace0 | 2012-09-20 22:15:37 -0700 | [diff] [blame] | 834 | int storedId = readIntAttribute(parser, ATTR_ID, -1); |
| 835 | if (storedId != id) { |
Amith Yamasani | 0b28549 | 2011-04-14 17:35:23 -0700 | [diff] [blame] | 836 | Slog.e(LOG_TAG, "User id does not match the file name"); |
Amith Yamasani | 4b2e934 | 2011-03-31 12:38:53 -0700 | [diff] [blame] | 837 | return null; |
| 838 | } |
Amith Yamasani | 920ace0 | 2012-09-20 22:15:37 -0700 | [diff] [blame] | 839 | serialNumber = readIntAttribute(parser, ATTR_SERIAL_NO, id); |
| 840 | flags = readIntAttribute(parser, ATTR_FLAGS, 0); |
Amith Yamasani | b8151ec | 2012-04-18 18:02:48 -0700 | [diff] [blame] | 841 | iconPath = parser.getAttributeValue(null, ATTR_ICON_PATH); |
Amith Yamasani | 920ace0 | 2012-09-20 22:15:37 -0700 | [diff] [blame] | 842 | creationTime = readLongAttribute(parser, ATTR_CREATION_TIME, 0); |
| 843 | lastLoggedInTime = readLongAttribute(parser, ATTR_LAST_LOGGED_IN_TIME, 0); |
Amith Yamasani | 655d0e2 | 2013-06-12 14:19:10 -0700 | [diff] [blame] | 844 | salt = readLongAttribute(parser, ATTR_SALT, 0L); |
| 845 | pinHash = parser.getAttributeValue(null, ATTR_PIN_HASH); |
| 846 | failedAttempts = readIntAttribute(parser, ATTR_FAILED_ATTEMPTS, 0); |
| 847 | lastAttemptTime = readLongAttribute(parser, ATTR_LAST_RETRY_MS, 0L); |
Kenny Guy | 2a76494 | 2014-04-02 13:29:20 +0100 | [diff] [blame] | 848 | profileGroupId = readIntAttribute(parser, ATTR_PROFILE_GROUP_ID, |
| 849 | UserInfo.NO_PROFILE_GROUP_ID); |
| 850 | if (profileGroupId == UserInfo.NO_PROFILE_GROUP_ID) { |
| 851 | // This attribute was added and renamed during development of L. |
| 852 | // TODO Remove upgrade path by 1st May 2014 |
| 853 | profileGroupId = readIntAttribute(parser, "relatedGroupId", |
| 854 | UserInfo.NO_PROFILE_GROUP_ID); |
| 855 | } |
Dianne Hackborn | d4ac8d7 | 2012-09-27 23:20:10 -0700 | [diff] [blame] | 856 | String valueString = parser.getAttributeValue(null, ATTR_PARTIAL); |
| 857 | if ("true".equals(valueString)) { |
| 858 | partial = true; |
| 859 | } |
Amith Yamasani | 4b2e934 | 2011-03-31 12:38:53 -0700 | [diff] [blame] | 860 | |
Amith Yamasani | e4cf734 | 2012-12-17 11:12:09 -0800 | [diff] [blame] | 861 | int outerDepth = parser.getDepth(); |
| 862 | while ((type = parser.next()) != XmlPullParser.END_DOCUMENT |
| 863 | && (type != XmlPullParser.END_TAG || parser.getDepth() > outerDepth)) { |
| 864 | if (type == XmlPullParser.END_TAG || type == XmlPullParser.TEXT) { |
| 865 | continue; |
| 866 | } |
| 867 | String tag = parser.getName(); |
| 868 | if (TAG_NAME.equals(tag)) { |
| 869 | type = parser.next(); |
| 870 | if (type == XmlPullParser.TEXT) { |
| 871 | name = parser.getText(); |
| 872 | } |
| 873 | } else if (TAG_RESTRICTIONS.equals(tag)) { |
Amith Yamasani | 71e6c69 | 2013-03-24 17:39:28 -0700 | [diff] [blame] | 874 | readBoolean(parser, restrictions, UserManager.DISALLOW_CONFIG_WIFI); |
| 875 | readBoolean(parser, restrictions, UserManager.DISALLOW_MODIFY_ACCOUNTS); |
| 876 | readBoolean(parser, restrictions, UserManager.DISALLOW_INSTALL_APPS); |
| 877 | readBoolean(parser, restrictions, UserManager.DISALLOW_UNINSTALL_APPS); |
| 878 | readBoolean(parser, restrictions, UserManager.DISALLOW_SHARE_LOCATION); |
Maggie Benthall | a12fccf | 2013-03-14 18:02:12 -0400 | [diff] [blame] | 879 | readBoolean(parser, restrictions, |
| 880 | UserManager.DISALLOW_INSTALL_UNKNOWN_SOURCES); |
| 881 | readBoolean(parser, restrictions, UserManager.DISALLOW_CONFIG_BLUETOOTH); |
| 882 | readBoolean(parser, restrictions, UserManager.DISALLOW_USB_FILE_TRANSFER); |
Emily Bernier | b223f73 | 2013-04-11 15:46:36 -0400 | [diff] [blame] | 883 | readBoolean(parser, restrictions, UserManager.DISALLOW_CONFIG_CREDENTIALS); |
| 884 | readBoolean(parser, restrictions, UserManager.DISALLOW_REMOVE_USER); |
Julia Reynolds | d46d0f9 | 2014-04-23 15:23:24 -0400 | [diff] [blame^] | 885 | readBoolean(parser, restrictions, UserManager.DISALLOW_DEBUGGING_FEATURES); |
| 886 | readBoolean(parser, restrictions, UserManager.DISALLOW_CONFIG_VPN); |
| 887 | readBoolean(parser, restrictions, UserManager.DISALLOW_CONFIG_TETHERING); |
| 888 | readBoolean(parser, restrictions, UserManager.DISALLOW_FACTORY_RESET); |
| 889 | readBoolean(parser, restrictions, UserManager.DISALLOW_ADD_USER); |
| 890 | readBoolean(parser, restrictions, UserManager.ENSURE_VERIFY_APPS); |
| 891 | readBoolean(parser, restrictions, UserManager.DISALLOW_CONFIG_CELL_BROADCASTS); |
| 892 | readBoolean(parser, restrictions, UserManager.DISALLOW_CONFIG_MOBILE_NETWORKS); |
| 893 | readBoolean(parser, restrictions, UserManager.DISALLOW_CONFIG_APPS); |
Amith Yamasani | 4b2e934 | 2011-03-31 12:38:53 -0700 | [diff] [blame] | 894 | } |
| 895 | } |
| 896 | } |
Amith Yamasani | 4b2e934 | 2011-03-31 12:38:53 -0700 | [diff] [blame] | 897 | |
Amith Yamasani | b8151ec | 2012-04-18 18:02:48 -0700 | [diff] [blame] | 898 | UserInfo userInfo = new UserInfo(id, name, iconPath, flags); |
Amith Yamasani | 2a00329 | 2012-08-14 18:25:45 -0700 | [diff] [blame] | 899 | userInfo.serialNumber = serialNumber; |
Amith Yamasani | 920ace0 | 2012-09-20 22:15:37 -0700 | [diff] [blame] | 900 | userInfo.creationTime = creationTime; |
| 901 | userInfo.lastLoggedInTime = lastLoggedInTime; |
Dianne Hackborn | d4ac8d7 | 2012-09-27 23:20:10 -0700 | [diff] [blame] | 902 | userInfo.partial = partial; |
Kenny Guy | 2a76494 | 2014-04-02 13:29:20 +0100 | [diff] [blame] | 903 | userInfo.profileGroupId = profileGroupId; |
Amith Yamasani | e4cf734 | 2012-12-17 11:12:09 -0800 | [diff] [blame] | 904 | mUserRestrictions.append(id, restrictions); |
Amith Yamasani | 655d0e2 | 2013-06-12 14:19:10 -0700 | [diff] [blame] | 905 | if (salt != 0L) { |
| 906 | RestrictionsPinState pinState = mRestrictionsPinStates.get(id); |
| 907 | if (pinState == null) { |
| 908 | pinState = new RestrictionsPinState(); |
| 909 | mRestrictionsPinStates.put(id, pinState); |
| 910 | } |
| 911 | pinState.salt = salt; |
| 912 | pinState.pinHash = pinHash; |
| 913 | pinState.failedAttempts = failedAttempts; |
| 914 | pinState.lastAttemptTime = lastAttemptTime; |
| 915 | } |
Amith Yamasani | 4b2e934 | 2011-03-31 12:38:53 -0700 | [diff] [blame] | 916 | return userInfo; |
| 917 | |
| 918 | } catch (IOException ioe) { |
| 919 | } catch (XmlPullParserException pe) { |
Dianne Hackborn | bfd89b3 | 2011-12-15 18:22:54 -0800 | [diff] [blame] | 920 | } finally { |
| 921 | if (fis != null) { |
| 922 | try { |
| 923 | fis.close(); |
| 924 | } catch (IOException e) { |
| 925 | } |
| 926 | } |
Amith Yamasani | 4b2e934 | 2011-03-31 12:38:53 -0700 | [diff] [blame] | 927 | } |
| 928 | return null; |
| 929 | } |
| 930 | |
Amith Yamasani | e4cf734 | 2012-12-17 11:12:09 -0800 | [diff] [blame] | 931 | private void readBoolean(XmlPullParser parser, Bundle restrictions, |
| 932 | String restrictionKey) { |
| 933 | String value = parser.getAttributeValue(null, restrictionKey); |
Amith Yamasani | 71e6c69 | 2013-03-24 17:39:28 -0700 | [diff] [blame] | 934 | if (value != null) { |
| 935 | restrictions.putBoolean(restrictionKey, Boolean.parseBoolean(value)); |
| 936 | } |
Amith Yamasani | e4cf734 | 2012-12-17 11:12:09 -0800 | [diff] [blame] | 937 | } |
| 938 | |
| 939 | private void writeBoolean(XmlSerializer xml, Bundle restrictions, String restrictionKey) |
| 940 | throws IOException { |
| 941 | if (restrictions.containsKey(restrictionKey)) { |
| 942 | xml.attribute(null, restrictionKey, |
| 943 | Boolean.toString(restrictions.getBoolean(restrictionKey))); |
| 944 | } |
| 945 | } |
| 946 | |
Amith Yamasani | 920ace0 | 2012-09-20 22:15:37 -0700 | [diff] [blame] | 947 | private int readIntAttribute(XmlPullParser parser, String attr, int defaultValue) { |
| 948 | String valueString = parser.getAttributeValue(null, attr); |
| 949 | if (valueString == null) return defaultValue; |
| 950 | try { |
| 951 | return Integer.parseInt(valueString); |
| 952 | } catch (NumberFormatException nfe) { |
| 953 | return defaultValue; |
| 954 | } |
| 955 | } |
| 956 | |
| 957 | private long readLongAttribute(XmlPullParser parser, String attr, long defaultValue) { |
| 958 | String valueString = parser.getAttributeValue(null, attr); |
| 959 | if (valueString == null) return defaultValue; |
| 960 | try { |
| 961 | return Long.parseLong(valueString); |
| 962 | } catch (NumberFormatException nfe) { |
| 963 | return defaultValue; |
| 964 | } |
| 965 | } |
| 966 | |
Amith Yamasani | 1a7472e | 2013-07-02 11:17:30 -0700 | [diff] [blame] | 967 | private boolean isPackageInstalled(String pkg, int userId) { |
| 968 | final ApplicationInfo info = mPm.getApplicationInfo(pkg, |
| 969 | PackageManager.GET_UNINSTALLED_PACKAGES, |
| 970 | userId); |
| 971 | if (info == null || (info.flags&ApplicationInfo.FLAG_INSTALLED) == 0) { |
| 972 | return false; |
| 973 | } |
| 974 | return true; |
| 975 | } |
| 976 | |
Amith Yamasani | b82add2 | 2013-07-09 11:24:44 -0700 | [diff] [blame] | 977 | /** |
Amith Yamasani | 1a7472e | 2013-07-02 11:17:30 -0700 | [diff] [blame] | 978 | * Removes all the restrictions files (res_<packagename>) for a given user, if all is true, |
| 979 | * else removes only those packages that have been uninstalled. |
Amith Yamasani | b82add2 | 2013-07-09 11:24:44 -0700 | [diff] [blame] | 980 | * Does not do any permissions checking. |
| 981 | */ |
Amith Yamasani | 1a7472e | 2013-07-02 11:17:30 -0700 | [diff] [blame] | 982 | private void cleanAppRestrictions(int userId, boolean all) { |
Amith Yamasani | b82add2 | 2013-07-09 11:24:44 -0700 | [diff] [blame] | 983 | synchronized (mPackagesLock) { |
| 984 | File dir = Environment.getUserSystemDirectory(userId); |
| 985 | String[] files = dir.list(); |
| 986 | if (files == null) return; |
| 987 | for (String fileName : files) { |
| 988 | if (fileName.startsWith(RESTRICTIONS_FILE_PREFIX)) { |
| 989 | File resFile = new File(dir, fileName); |
| 990 | if (resFile.exists()) { |
Amith Yamasani | 1a7472e | 2013-07-02 11:17:30 -0700 | [diff] [blame] | 991 | if (all) { |
| 992 | resFile.delete(); |
| 993 | } else { |
Amith Yamasani | fc95e70 | 2013-09-26 13:20:17 -0700 | [diff] [blame] | 994 | String pkg = restrictionsFileNameToPackage(fileName); |
Amith Yamasani | 1a7472e | 2013-07-02 11:17:30 -0700 | [diff] [blame] | 995 | if (!isPackageInstalled(pkg, userId)) { |
| 996 | resFile.delete(); |
| 997 | } |
| 998 | } |
Amith Yamasani | b82add2 | 2013-07-09 11:24:44 -0700 | [diff] [blame] | 999 | } |
| 1000 | } |
| 1001 | } |
| 1002 | } |
| 1003 | } |
| 1004 | |
Amith Yamasani | 1a7472e | 2013-07-02 11:17:30 -0700 | [diff] [blame] | 1005 | /** |
| 1006 | * Removes the app restrictions file for a specific package and user id, if it exists. |
| 1007 | */ |
| 1008 | private void cleanAppRestrictionsForPackage(String pkg, int userId) { |
| 1009 | synchronized (mPackagesLock) { |
| 1010 | File dir = Environment.getUserSystemDirectory(userId); |
Amith Yamasani | fc95e70 | 2013-09-26 13:20:17 -0700 | [diff] [blame] | 1011 | File resFile = new File(dir, packageToRestrictionsFileName(pkg)); |
Amith Yamasani | 1a7472e | 2013-07-02 11:17:30 -0700 | [diff] [blame] | 1012 | if (resFile.exists()) { |
| 1013 | resFile.delete(); |
| 1014 | } |
| 1015 | } |
| 1016 | } |
| 1017 | |
Kenny Guy | 2a76494 | 2014-04-02 13:29:20 +0100 | [diff] [blame] | 1018 | private int getNextProfileGroupIdLocked() { |
| 1019 | int maxGroupId = UserInfo.NO_PROFILE_GROUP_ID; |
Kenny Guy | a52dc3e | 2014-02-11 15:33:14 +0000 | [diff] [blame] | 1020 | for (int i = 0; i < mUsers.size(); i++) { |
| 1021 | UserInfo ui = mUsers.valueAt(i); |
Kenny Guy | 2a76494 | 2014-04-02 13:29:20 +0100 | [diff] [blame] | 1022 | if (maxGroupId < ui.profileGroupId) { |
| 1023 | maxGroupId = ui.profileGroupId; |
Kenny Guy | a52dc3e | 2014-02-11 15:33:14 +0000 | [diff] [blame] | 1024 | } |
| 1025 | } |
| 1026 | return maxGroupId + 1; |
| 1027 | } |
| 1028 | |
| 1029 | @Override |
Kenny Guy | 2a76494 | 2014-04-02 13:29:20 +0100 | [diff] [blame] | 1030 | public UserInfo createProfileForUser(String name, int flags, int userId) { |
Kenny Guy | a52dc3e | 2014-02-11 15:33:14 +0000 | [diff] [blame] | 1031 | checkManageUsersPermission("Only the system can create users"); |
Kenny Guy | 2a76494 | 2014-04-02 13:29:20 +0100 | [diff] [blame] | 1032 | if (userId != UserHandle.USER_OWNER) { |
| 1033 | Slog.w(LOG_TAG, "Only user owner can have profiles"); |
Kenny Guy | a52dc3e | 2014-02-11 15:33:14 +0000 | [diff] [blame] | 1034 | return null; |
| 1035 | } |
Kenny Guy | 2a76494 | 2014-04-02 13:29:20 +0100 | [diff] [blame] | 1036 | return createUserInternal(name, flags, userId); |
Kenny Guy | a52dc3e | 2014-02-11 15:33:14 +0000 | [diff] [blame] | 1037 | } |
| 1038 | |
Amith Yamasani | 258848d | 2012-08-10 17:06:33 -0700 | [diff] [blame] | 1039 | @Override |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 1040 | public UserInfo createUser(String name, int flags) { |
Amith Yamasani | 2a00329 | 2012-08-14 18:25:45 -0700 | [diff] [blame] | 1041 | checkManageUsersPermission("Only the system can create users"); |
Nicolas Prevot | c6d033e | 2014-02-27 13:11:09 +0000 | [diff] [blame] | 1042 | return createUserInternal(name, flags, UserHandle.USER_NULL); |
Kenny Guy | a52dc3e | 2014-02-11 15:33:14 +0000 | [diff] [blame] | 1043 | } |
Amith Yamasani | faea76f | 2012-09-11 10:59:48 -0700 | [diff] [blame] | 1044 | |
Kenny Guy | 2a76494 | 2014-04-02 13:29:20 +0100 | [diff] [blame] | 1045 | private UserInfo createUserInternal(String name, int flags, int profileId) { |
Dianne Hackborn | 5dc5a00 | 2012-09-15 19:33:48 -0700 | [diff] [blame] | 1046 | final long ident = Binder.clearCallingIdentity(); |
Nicolas Prevot | c6d033e | 2014-02-27 13:11:09 +0000 | [diff] [blame] | 1047 | UserInfo userInfo = null; |
Dianne Hackborn | 5dc5a00 | 2012-09-15 19:33:48 -0700 | [diff] [blame] | 1048 | try { |
| 1049 | synchronized (mInstallLock) { |
| 1050 | synchronized (mPackagesLock) { |
Kenny Guy | 2a76494 | 2014-04-02 13:29:20 +0100 | [diff] [blame] | 1051 | UserInfo profile = null; |
| 1052 | if (profileId != UserHandle.USER_NULL) { |
| 1053 | profile = getUserInfoLocked(profileId); |
| 1054 | if (profile == null) return null; |
Nicolas Prevot | c6d033e | 2014-02-27 13:11:09 +0000 | [diff] [blame] | 1055 | } |
Dianne Hackborn | 5dc5a00 | 2012-09-15 19:33:48 -0700 | [diff] [blame] | 1056 | if (isUserLimitReachedLocked()) return null; |
| 1057 | int userId = getNextAvailableIdLocked(); |
| 1058 | userInfo = new UserInfo(userId, name, null, flags); |
| 1059 | File userPath = new File(mBaseUserPath, Integer.toString(userId)); |
| 1060 | userInfo.serialNumber = mNextSerialNumber++; |
Amith Yamasani | 920ace0 | 2012-09-20 22:15:37 -0700 | [diff] [blame] | 1061 | long now = System.currentTimeMillis(); |
| 1062 | userInfo.creationTime = (now > EPOCH_PLUS_30_YEARS) ? now : 0; |
Dianne Hackborn | d4ac8d7 | 2012-09-27 23:20:10 -0700 | [diff] [blame] | 1063 | userInfo.partial = true; |
Amith Yamasani | 1638931 | 2012-10-17 21:20:14 -0700 | [diff] [blame] | 1064 | Environment.getUserSystemDirectory(userInfo.id).mkdirs(); |
Dianne Hackborn | 5dc5a00 | 2012-09-15 19:33:48 -0700 | [diff] [blame] | 1065 | mUsers.put(userId, userInfo); |
| 1066 | writeUserListLocked(); |
Kenny Guy | 2a76494 | 2014-04-02 13:29:20 +0100 | [diff] [blame] | 1067 | if (profile != null) { |
| 1068 | if (profile.profileGroupId == UserInfo.NO_PROFILE_GROUP_ID) { |
| 1069 | profile.profileGroupId = getNextProfileGroupIdLocked(); |
| 1070 | writeUserLocked(profile); |
Kenny Guy | a52dc3e | 2014-02-11 15:33:14 +0000 | [diff] [blame] | 1071 | } |
Kenny Guy | 2a76494 | 2014-04-02 13:29:20 +0100 | [diff] [blame] | 1072 | userInfo.profileGroupId = profile.profileGroupId; |
Kenny Guy | a52dc3e | 2014-02-11 15:33:14 +0000 | [diff] [blame] | 1073 | } |
Dianne Hackborn | 5dc5a00 | 2012-09-15 19:33:48 -0700 | [diff] [blame] | 1074 | writeUserLocked(userInfo); |
Dianne Hackborn | 5dc5a00 | 2012-09-15 19:33:48 -0700 | [diff] [blame] | 1075 | mPm.createNewUserLILPw(userId, userPath); |
Dianne Hackborn | d4ac8d7 | 2012-09-27 23:20:10 -0700 | [diff] [blame] | 1076 | userInfo.partial = false; |
| 1077 | writeUserLocked(userInfo); |
| 1078 | updateUserIdsLocked(); |
Geoffrey Borggaard | e45e45e3 | 2013-01-24 10:03:20 -0500 | [diff] [blame] | 1079 | Bundle restrictions = new Bundle(); |
Geoffrey Borggaard | e45e45e3 | 2013-01-24 10:03:20 -0500 | [diff] [blame] | 1080 | mUserRestrictions.append(userId, restrictions); |
Dianne Hackborn | 5dc5a00 | 2012-09-15 19:33:48 -0700 | [diff] [blame] | 1081 | } |
Dianne Hackborn | 4428e17 | 2012-08-24 17:43:05 -0700 | [diff] [blame] | 1082 | } |
Dianne Hackborn | 5dc5a00 | 2012-09-15 19:33:48 -0700 | [diff] [blame] | 1083 | if (userInfo != null) { |
| 1084 | Intent addedIntent = new Intent(Intent.ACTION_USER_ADDED); |
| 1085 | addedIntent.putExtra(Intent.EXTRA_USER_HANDLE, userInfo.id); |
| 1086 | mContext.sendBroadcastAsUser(addedIntent, UserHandle.ALL, |
| 1087 | android.Manifest.permission.MANAGE_USERS); |
| 1088 | } |
| 1089 | } finally { |
| 1090 | Binder.restoreCallingIdentity(ident); |
Amith Yamasani | 258848d | 2012-08-10 17:06:33 -0700 | [diff] [blame] | 1091 | } |
Amith Yamasani | 4b2e934 | 2011-03-31 12:38:53 -0700 | [diff] [blame] | 1092 | return userInfo; |
| 1093 | } |
| 1094 | |
Amith Yamasani | 0b28549 | 2011-04-14 17:35:23 -0700 | [diff] [blame] | 1095 | /** |
| 1096 | * Removes a user and all data directories created for that user. This method should be called |
| 1097 | * after the user's processes have been terminated. |
Dianne Hackborn | 10ad982 | 2014-03-17 11:28:36 -0700 | [diff] [blame] | 1098 | * @param userHandle the user's id |
Amith Yamasani | 0b28549 | 2011-04-14 17:35:23 -0700 | [diff] [blame] | 1099 | */ |
Amith Yamasani | 258848d | 2012-08-10 17:06:33 -0700 | [diff] [blame] | 1100 | public boolean removeUser(int userHandle) { |
Amith Yamasani | 2a00329 | 2012-08-14 18:25:45 -0700 | [diff] [blame] | 1101 | checkManageUsersPermission("Only the system can remove users"); |
Dianne Hackborn | 80a4af2 | 2012-08-27 19:18:31 -0700 | [diff] [blame] | 1102 | final UserInfo user; |
| 1103 | synchronized (mPackagesLock) { |
| 1104 | user = mUsers.get(userHandle); |
| 1105 | if (userHandle == 0 || user == null) { |
| 1106 | return false; |
| 1107 | } |
Jeff Sharkey | ffe0cb4 | 2012-11-05 17:24:43 -0800 | [diff] [blame] | 1108 | mRemovingUserIds.put(userHandle, true); |
Amith Yamasani | 756901d | 2012-10-12 12:30:07 -0700 | [diff] [blame] | 1109 | // Set this to a partially created user, so that the user will be purged |
| 1110 | // on next startup, in case the runtime stops now before stopping and |
| 1111 | // removing the user completely. |
| 1112 | user.partial = true; |
Adam Connors | 7b66ed5 | 2014-04-14 11:58:10 +0100 | [diff] [blame] | 1113 | // Mark it as disabled, so that it isn't returned any more when |
| 1114 | // profiles are queried. |
| 1115 | user.flags |= UserInfo.FLAG_DISABLED; |
Amith Yamasani | 756901d | 2012-10-12 12:30:07 -0700 | [diff] [blame] | 1116 | writeUserLocked(user); |
Dianne Hackborn | 80a4af2 | 2012-08-27 19:18:31 -0700 | [diff] [blame] | 1117 | } |
Amith Yamasani | 1638931 | 2012-10-17 21:20:14 -0700 | [diff] [blame] | 1118 | if (DBG) Slog.i(LOG_TAG, "Stopping user " + userHandle); |
Dianne Hackborn | 80a4af2 | 2012-08-27 19:18:31 -0700 | [diff] [blame] | 1119 | int res; |
| 1120 | try { |
| 1121 | res = ActivityManagerNative.getDefault().stopUser(userHandle, |
| 1122 | new IStopUserCallback.Stub() { |
| 1123 | @Override |
| 1124 | public void userStopped(int userId) { |
| 1125 | finishRemoveUser(userId); |
| 1126 | } |
| 1127 | @Override |
| 1128 | public void userStopAborted(int userId) { |
| 1129 | } |
| 1130 | }); |
| 1131 | } catch (RemoteException e) { |
| 1132 | return false; |
| 1133 | } |
| 1134 | |
| 1135 | return res == ActivityManager.USER_OP_SUCCESS; |
| 1136 | } |
| 1137 | |
Amith Yamasani | db6a14c | 2012-10-17 21:16:52 -0700 | [diff] [blame] | 1138 | void finishRemoveUser(final int userHandle) { |
Amith Yamasani | 1638931 | 2012-10-17 21:20:14 -0700 | [diff] [blame] | 1139 | if (DBG) Slog.i(LOG_TAG, "finishRemoveUser " + userHandle); |
Amith Yamasani | db6a14c | 2012-10-17 21:16:52 -0700 | [diff] [blame] | 1140 | // Let other services shutdown any activity and clean up their state before completely |
| 1141 | // wiping the user's system directory and removing from the user list |
Dianne Hackborn | 5dc5a00 | 2012-09-15 19:33:48 -0700 | [diff] [blame] | 1142 | long ident = Binder.clearCallingIdentity(); |
| 1143 | try { |
Adam Connors | 7b66ed5 | 2014-04-14 11:58:10 +0100 | [diff] [blame] | 1144 | final boolean isManaged = getUserInfo(userHandle).isManagedProfile(); |
Dianne Hackborn | 5dc5a00 | 2012-09-15 19:33:48 -0700 | [diff] [blame] | 1145 | Intent addedIntent = new Intent(Intent.ACTION_USER_REMOVED); |
| 1146 | addedIntent.putExtra(Intent.EXTRA_USER_HANDLE, userHandle); |
Amith Yamasani | db6a14c | 2012-10-17 21:16:52 -0700 | [diff] [blame] | 1147 | mContext.sendOrderedBroadcastAsUser(addedIntent, UserHandle.ALL, |
| 1148 | android.Manifest.permission.MANAGE_USERS, |
| 1149 | |
| 1150 | new BroadcastReceiver() { |
| 1151 | @Override |
| 1152 | public void onReceive(Context context, Intent intent) { |
| 1153 | if (DBG) { |
| 1154 | Slog.i(LOG_TAG, |
| 1155 | "USER_REMOVED broadcast sent, cleaning up user data " |
| 1156 | + userHandle); |
| 1157 | } |
| 1158 | new Thread() { |
| 1159 | public void run() { |
| 1160 | synchronized (mInstallLock) { |
| 1161 | synchronized (mPackagesLock) { |
| 1162 | removeUserStateLocked(userHandle); |
| 1163 | } |
| 1164 | } |
Adam Connors | 7b66ed5 | 2014-04-14 11:58:10 +0100 | [diff] [blame] | 1165 | // Send broadcast to notify system that the user removed was a |
| 1166 | // managed user. |
| 1167 | if (isManaged) { |
| 1168 | sendProfileRemovedBroadcast(userHandle); |
| 1169 | } |
Amith Yamasani | db6a14c | 2012-10-17 21:16:52 -0700 | [diff] [blame] | 1170 | } |
| 1171 | }.start(); |
| 1172 | } |
| 1173 | }, |
| 1174 | |
| 1175 | null, Activity.RESULT_OK, null, null); |
Dianne Hackborn | 5dc5a00 | 2012-09-15 19:33:48 -0700 | [diff] [blame] | 1176 | } finally { |
| 1177 | Binder.restoreCallingIdentity(ident); |
| 1178 | } |
Amith Yamasani | 2a00329 | 2012-08-14 18:25:45 -0700 | [diff] [blame] | 1179 | } |
| 1180 | |
Jeff Sharkey | ffe0cb4 | 2012-11-05 17:24:43 -0800 | [diff] [blame] | 1181 | private void removeUserStateLocked(final int userHandle) { |
Dianne Hackborn | d4ac8d7 | 2012-09-27 23:20:10 -0700 | [diff] [blame] | 1182 | // Cleanup package manager settings |
| 1183 | mPm.cleanUpUserLILPw(userHandle); |
| 1184 | |
| 1185 | // Remove this user from the list |
| 1186 | mUsers.remove(userHandle); |
Jeff Sharkey | ffe0cb4 | 2012-11-05 17:24:43 -0800 | [diff] [blame] | 1187 | |
| 1188 | // Have user ID linger for several seconds to let external storage VFS |
| 1189 | // cache entries expire. This must be greater than the 'entry_valid' |
| 1190 | // timeout used by the FUSE daemon. |
| 1191 | mHandler.postDelayed(new Runnable() { |
| 1192 | @Override |
| 1193 | public void run() { |
| 1194 | synchronized (mPackagesLock) { |
| 1195 | mRemovingUserIds.delete(userHandle); |
| 1196 | } |
| 1197 | } |
| 1198 | }, MINUTE_IN_MILLIS); |
| 1199 | |
Amith Yamasani | 655d0e2 | 2013-06-12 14:19:10 -0700 | [diff] [blame] | 1200 | mRestrictionsPinStates.remove(userHandle); |
Dianne Hackborn | d4ac8d7 | 2012-09-27 23:20:10 -0700 | [diff] [blame] | 1201 | // Remove user file |
Amith Yamasani | fc95e70 | 2013-09-26 13:20:17 -0700 | [diff] [blame] | 1202 | AtomicFile userFile = new AtomicFile(new File(mUsersDir, userHandle + XML_SUFFIX)); |
Dianne Hackborn | d4ac8d7 | 2012-09-27 23:20:10 -0700 | [diff] [blame] | 1203 | userFile.delete(); |
| 1204 | // Update the user list |
| 1205 | writeUserListLocked(); |
| 1206 | updateUserIdsLocked(); |
| 1207 | removeDirectoryRecursive(Environment.getUserSystemDirectory(userHandle)); |
| 1208 | } |
| 1209 | |
Amith Yamasani | 61f5737 | 2012-08-31 12:12:28 -0700 | [diff] [blame] | 1210 | private void removeDirectoryRecursive(File parent) { |
| 1211 | if (parent.isDirectory()) { |
| 1212 | String[] files = parent.list(); |
| 1213 | for (String filename : files) { |
| 1214 | File child = new File(parent, filename); |
| 1215 | removeDirectoryRecursive(child); |
| 1216 | } |
| 1217 | } |
| 1218 | parent.delete(); |
| 1219 | } |
| 1220 | |
Adam Connors | 7b66ed5 | 2014-04-14 11:58:10 +0100 | [diff] [blame] | 1221 | private void sendProfileRemovedBroadcast(int userHandle) { |
| 1222 | Intent managedProfileIntent = new Intent(Intent.ACTION_MANAGED_PROFILE_REMOVED); |
| 1223 | managedProfileIntent.putExtra(Intent.EXTRA_USER, new UserHandle(userHandle)); |
| 1224 | // Note: This makes an assumption that the parent owner is user 0. |
| 1225 | mContext.sendBroadcastAsUser(managedProfileIntent, UserHandle.OWNER, null); |
| 1226 | } |
| 1227 | |
Amith Yamasani | 2a00329 | 2012-08-14 18:25:45 -0700 | [diff] [blame] | 1228 | @Override |
Amith Yamasani | 7e99bc0 | 2013-04-16 18:24:51 -0700 | [diff] [blame] | 1229 | public Bundle getApplicationRestrictions(String packageName) { |
| 1230 | return getApplicationRestrictionsForUser(packageName, UserHandle.getCallingUserId()); |
| 1231 | } |
| 1232 | |
| 1233 | @Override |
| 1234 | public Bundle getApplicationRestrictionsForUser(String packageName, int userId) { |
Amith Yamasani | df2e92a | 2013-03-01 17:04:38 -0800 | [diff] [blame] | 1235 | if (UserHandle.getCallingUserId() != userId |
Amith Yamasani | 9429afb | 2013-04-10 18:40:51 -0700 | [diff] [blame] | 1236 | || !UserHandle.isSameApp(Binder.getCallingUid(), getUidForPackage(packageName))) { |
Amith Yamasani | be46532 | 2014-04-24 13:45:17 -0700 | [diff] [blame] | 1237 | checkManageUsersPermission("Only system can get restrictions for other users/apps"); |
Amith Yamasani | df2e92a | 2013-03-01 17:04:38 -0800 | [diff] [blame] | 1238 | } |
| 1239 | synchronized (mPackagesLock) { |
| 1240 | // Read the restrictions from XML |
| 1241 | return readApplicationRestrictionsLocked(packageName, userId); |
| 1242 | } |
| 1243 | } |
| 1244 | |
| 1245 | @Override |
Amith Yamasani | 7e99bc0 | 2013-04-16 18:24:51 -0700 | [diff] [blame] | 1246 | public void setApplicationRestrictions(String packageName, Bundle restrictions, |
Amith Yamasani | df2e92a | 2013-03-01 17:04:38 -0800 | [diff] [blame] | 1247 | int userId) { |
| 1248 | if (UserHandle.getCallingUserId() != userId |
Amith Yamasani | 9429afb | 2013-04-10 18:40:51 -0700 | [diff] [blame] | 1249 | || !UserHandle.isSameApp(Binder.getCallingUid(), getUidForPackage(packageName))) { |
Amith Yamasani | be46532 | 2014-04-24 13:45:17 -0700 | [diff] [blame] | 1250 | checkManageUsersPermission("Only system can set restrictions for other users/apps"); |
Amith Yamasani | df2e92a | 2013-03-01 17:04:38 -0800 | [diff] [blame] | 1251 | } |
| 1252 | synchronized (mPackagesLock) { |
| 1253 | // Write the restrictions to XML |
Amith Yamasani | 7e99bc0 | 2013-04-16 18:24:51 -0700 | [diff] [blame] | 1254 | writeApplicationRestrictionsLocked(packageName, restrictions, userId); |
Amith Yamasani | df2e92a | 2013-03-01 17:04:38 -0800 | [diff] [blame] | 1255 | } |
Robin Lee | 66e5d96 | 2014-04-09 16:44:21 +0100 | [diff] [blame] | 1256 | |
| 1257 | // Notify package of changes via an intent - only sent to explicitly registered receivers. |
| 1258 | Intent changeIntent = new Intent(Intent.ACTION_APPLICATION_RESTRICTIONS_CHANGED); |
| 1259 | changeIntent.setPackage(packageName); |
| 1260 | changeIntent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY); |
| 1261 | mContext.sendBroadcastAsUser(changeIntent, new UserHandle(userId)); |
Amith Yamasani | df2e92a | 2013-03-01 17:04:38 -0800 | [diff] [blame] | 1262 | } |
| 1263 | |
Amith Yamasani | 655d0e2 | 2013-06-12 14:19:10 -0700 | [diff] [blame] | 1264 | @Override |
Amith Yamasani | d304af6 | 2013-09-05 09:30:23 -0700 | [diff] [blame] | 1265 | public boolean setRestrictionsChallenge(String newPin) { |
Amith Yamasani | 655d0e2 | 2013-06-12 14:19:10 -0700 | [diff] [blame] | 1266 | checkManageUsersPermission("Only system can modify the restrictions pin"); |
| 1267 | int userId = UserHandle.getCallingUserId(); |
| 1268 | synchronized (mPackagesLock) { |
| 1269 | RestrictionsPinState pinState = mRestrictionsPinStates.get(userId); |
| 1270 | if (pinState == null) { |
| 1271 | pinState = new RestrictionsPinState(); |
| 1272 | } |
| 1273 | if (newPin == null) { |
| 1274 | pinState.salt = 0; |
| 1275 | pinState.pinHash = null; |
| 1276 | } else { |
| 1277 | try { |
| 1278 | pinState.salt = SecureRandom.getInstance("SHA1PRNG").nextLong(); |
| 1279 | } catch (NoSuchAlgorithmException e) { |
| 1280 | pinState.salt = (long) (Math.random() * Long.MAX_VALUE); |
| 1281 | } |
| 1282 | pinState.pinHash = passwordToHash(newPin, pinState.salt); |
| 1283 | pinState.failedAttempts = 0; |
| 1284 | } |
| 1285 | mRestrictionsPinStates.put(userId, pinState); |
| 1286 | writeUserLocked(mUsers.get(userId)); |
| 1287 | } |
| 1288 | return true; |
| 1289 | } |
| 1290 | |
| 1291 | @Override |
Amith Yamasani | d304af6 | 2013-09-05 09:30:23 -0700 | [diff] [blame] | 1292 | public int checkRestrictionsChallenge(String pin) { |
Amith Yamasani | 655d0e2 | 2013-06-12 14:19:10 -0700 | [diff] [blame] | 1293 | checkManageUsersPermission("Only system can verify the restrictions pin"); |
| 1294 | int userId = UserHandle.getCallingUserId(); |
| 1295 | synchronized (mPackagesLock) { |
| 1296 | RestrictionsPinState pinState = mRestrictionsPinStates.get(userId); |
| 1297 | // If there's no pin set, return error code |
| 1298 | if (pinState == null || pinState.salt == 0 || pinState.pinHash == null) { |
| 1299 | return UserManager.PIN_VERIFICATION_FAILED_NOT_SET; |
| 1300 | } else if (pin == null) { |
| 1301 | // If just checking if user can be prompted, return remaining time |
| 1302 | int waitTime = getRemainingTimeForPinAttempt(pinState); |
| 1303 | Slog.d(LOG_TAG, "Remaining waittime peek=" + waitTime); |
| 1304 | return waitTime; |
| 1305 | } else { |
| 1306 | int waitTime = getRemainingTimeForPinAttempt(pinState); |
| 1307 | Slog.d(LOG_TAG, "Remaining waittime=" + waitTime); |
| 1308 | if (waitTime > 0) { |
| 1309 | return waitTime; |
| 1310 | } |
| 1311 | if (passwordToHash(pin, pinState.salt).equals(pinState.pinHash)) { |
| 1312 | pinState.failedAttempts = 0; |
| 1313 | writeUserLocked(mUsers.get(userId)); |
| 1314 | return UserManager.PIN_VERIFICATION_SUCCESS; |
| 1315 | } else { |
| 1316 | pinState.failedAttempts++; |
| 1317 | pinState.lastAttemptTime = System.currentTimeMillis(); |
| 1318 | writeUserLocked(mUsers.get(userId)); |
| 1319 | return waitTime; |
| 1320 | } |
| 1321 | } |
| 1322 | } |
| 1323 | } |
| 1324 | |
| 1325 | private int getRemainingTimeForPinAttempt(RestrictionsPinState pinState) { |
| 1326 | int backoffIndex = Math.min(pinState.failedAttempts / BACKOFF_INC_INTERVAL, |
| 1327 | BACKOFF_TIMES.length - 1); |
| 1328 | int backoffTime = (pinState.failedAttempts % BACKOFF_INC_INTERVAL) == 0 ? |
| 1329 | BACKOFF_TIMES[backoffIndex] : 0; |
| 1330 | return (int) Math.max(backoffTime + pinState.lastAttemptTime - System.currentTimeMillis(), |
| 1331 | 0); |
| 1332 | } |
| 1333 | |
| 1334 | @Override |
Amith Yamasani | d304af6 | 2013-09-05 09:30:23 -0700 | [diff] [blame] | 1335 | public boolean hasRestrictionsChallenge() { |
Amith Yamasani | 655d0e2 | 2013-06-12 14:19:10 -0700 | [diff] [blame] | 1336 | int userId = UserHandle.getCallingUserId(); |
| 1337 | synchronized (mPackagesLock) { |
Amith Yamasani | 0343ec3 | 2013-07-22 14:52:06 -0700 | [diff] [blame] | 1338 | return hasRestrictionsPinLocked(userId); |
| 1339 | } |
| 1340 | } |
| 1341 | |
| 1342 | private boolean hasRestrictionsPinLocked(int userId) { |
| 1343 | RestrictionsPinState pinState = mRestrictionsPinStates.get(userId); |
| 1344 | if (pinState == null || pinState.salt == 0 || pinState.pinHash == null) { |
| 1345 | return false; |
Amith Yamasani | 655d0e2 | 2013-06-12 14:19:10 -0700 | [diff] [blame] | 1346 | } |
| 1347 | return true; |
| 1348 | } |
| 1349 | |
Amith Yamasani | 1a7472e | 2013-07-02 11:17:30 -0700 | [diff] [blame] | 1350 | @Override |
| 1351 | public void removeRestrictions() { |
Amith Yamasani | be46532 | 2014-04-24 13:45:17 -0700 | [diff] [blame] | 1352 | checkManageUsersPermission("Only system can remove restrictions"); |
Amith Yamasani | 1a7472e | 2013-07-02 11:17:30 -0700 | [diff] [blame] | 1353 | final int userHandle = UserHandle.getCallingUserId(); |
Amith Yamasani | 5e486f5 | 2013-08-07 11:06:44 -0700 | [diff] [blame] | 1354 | removeRestrictionsForUser(userHandle, true); |
Amith Yamasani | 350962c | 2013-08-06 11:18:53 -0700 | [diff] [blame] | 1355 | } |
| 1356 | |
Amith Yamasani | 5e486f5 | 2013-08-07 11:06:44 -0700 | [diff] [blame] | 1357 | private void removeRestrictionsForUser(final int userHandle, boolean unblockApps) { |
Amith Yamasani | 1a7472e | 2013-07-02 11:17:30 -0700 | [diff] [blame] | 1358 | synchronized (mPackagesLock) { |
| 1359 | // Remove all user restrictions |
| 1360 | setUserRestrictions(new Bundle(), userHandle); |
| 1361 | // Remove restrictions pin |
Amith Yamasani | d304af6 | 2013-09-05 09:30:23 -0700 | [diff] [blame] | 1362 | setRestrictionsChallenge(null); |
Amith Yamasani | 1a7472e | 2013-07-02 11:17:30 -0700 | [diff] [blame] | 1363 | // Remove any app restrictions |
| 1364 | cleanAppRestrictions(userHandle, true); |
| 1365 | } |
Amith Yamasani | 5e486f5 | 2013-08-07 11:06:44 -0700 | [diff] [blame] | 1366 | if (unblockApps) { |
| 1367 | unblockAllAppsForUser(userHandle); |
| 1368 | } |
| 1369 | } |
| 1370 | |
| 1371 | private void unblockAllAppsForUser(final int userHandle) { |
Amith Yamasani | 1a7472e | 2013-07-02 11:17:30 -0700 | [diff] [blame] | 1372 | mHandler.post(new Runnable() { |
| 1373 | @Override |
| 1374 | public void run() { |
| 1375 | List<ApplicationInfo> apps = |
| 1376 | mPm.getInstalledApplications(PackageManager.GET_UNINSTALLED_PACKAGES, |
| 1377 | userHandle).getList(); |
| 1378 | final long ident = Binder.clearCallingIdentity(); |
| 1379 | try { |
| 1380 | for (ApplicationInfo appInfo : apps) { |
| 1381 | if ((appInfo.flags & ApplicationInfo.FLAG_INSTALLED) != 0 |
| 1382 | && (appInfo.flags & ApplicationInfo.FLAG_BLOCKED) != 0) { |
| 1383 | mPm.setApplicationBlockedSettingAsUser(appInfo.packageName, false, |
| 1384 | userHandle); |
| 1385 | } |
| 1386 | } |
| 1387 | } finally { |
| 1388 | Binder.restoreCallingIdentity(ident); |
| 1389 | } |
| 1390 | } |
| 1391 | }); |
| 1392 | } |
| 1393 | |
Amith Yamasani | 655d0e2 | 2013-06-12 14:19:10 -0700 | [diff] [blame] | 1394 | /* |
| 1395 | * Generate a hash for the given password. To avoid brute force attacks, we use a salted hash. |
| 1396 | * Not the most secure, but it is at least a second level of protection. First level is that |
| 1397 | * the file is in a location only readable by the system process. |
| 1398 | * @param password the password. |
| 1399 | * @param salt the randomly generated salt |
| 1400 | * @return the hash of the pattern in a String. |
| 1401 | */ |
| 1402 | private String passwordToHash(String password, long salt) { |
| 1403 | if (password == null) { |
| 1404 | return null; |
| 1405 | } |
| 1406 | String algo = null; |
| 1407 | String hashed = salt + password; |
| 1408 | try { |
| 1409 | byte[] saltedPassword = (password + salt).getBytes(); |
| 1410 | byte[] sha1 = MessageDigest.getInstance(algo = "SHA-1").digest(saltedPassword); |
| 1411 | byte[] md5 = MessageDigest.getInstance(algo = "MD5").digest(saltedPassword); |
| 1412 | hashed = toHex(sha1) + toHex(md5); |
| 1413 | } catch (NoSuchAlgorithmException e) { |
| 1414 | Log.w(LOG_TAG, "Failed to encode string because of missing algorithm: " + algo); |
| 1415 | } |
| 1416 | return hashed; |
| 1417 | } |
| 1418 | |
| 1419 | private static String toHex(byte[] ary) { |
| 1420 | final String hex = "0123456789ABCDEF"; |
| 1421 | String ret = ""; |
| 1422 | for (int i = 0; i < ary.length; i++) { |
| 1423 | ret += hex.charAt((ary[i] >> 4) & 0xf); |
| 1424 | ret += hex.charAt(ary[i] & 0xf); |
| 1425 | } |
| 1426 | return ret; |
| 1427 | } |
| 1428 | |
Amith Yamasani | df2e92a | 2013-03-01 17:04:38 -0800 | [diff] [blame] | 1429 | private int getUidForPackage(String packageName) { |
Amith Yamasani | 9429afb | 2013-04-10 18:40:51 -0700 | [diff] [blame] | 1430 | long ident = Binder.clearCallingIdentity(); |
Amith Yamasani | df2e92a | 2013-03-01 17:04:38 -0800 | [diff] [blame] | 1431 | try { |
| 1432 | return mContext.getPackageManager().getApplicationInfo(packageName, |
| 1433 | PackageManager.GET_UNINSTALLED_PACKAGES).uid; |
| 1434 | } catch (NameNotFoundException nnfe) { |
| 1435 | return -1; |
Amith Yamasani | 9429afb | 2013-04-10 18:40:51 -0700 | [diff] [blame] | 1436 | } finally { |
| 1437 | Binder.restoreCallingIdentity(ident); |
Amith Yamasani | df2e92a | 2013-03-01 17:04:38 -0800 | [diff] [blame] | 1438 | } |
| 1439 | } |
| 1440 | |
Amith Yamasani | 7e99bc0 | 2013-04-16 18:24:51 -0700 | [diff] [blame] | 1441 | private Bundle readApplicationRestrictionsLocked(String packageName, |
Amith Yamasani | df2e92a | 2013-03-01 17:04:38 -0800 | [diff] [blame] | 1442 | int userId) { |
Amith Yamasani | 7e99bc0 | 2013-04-16 18:24:51 -0700 | [diff] [blame] | 1443 | final Bundle restrictions = new Bundle(); |
Amith Yamasani | df2e92a | 2013-03-01 17:04:38 -0800 | [diff] [blame] | 1444 | final ArrayList<String> values = new ArrayList<String>(); |
| 1445 | |
| 1446 | FileInputStream fis = null; |
| 1447 | try { |
| 1448 | AtomicFile restrictionsFile = |
| 1449 | new AtomicFile(new File(Environment.getUserSystemDirectory(userId), |
Amith Yamasani | fc95e70 | 2013-09-26 13:20:17 -0700 | [diff] [blame] | 1450 | packageToRestrictionsFileName(packageName))); |
Amith Yamasani | df2e92a | 2013-03-01 17:04:38 -0800 | [diff] [blame] | 1451 | fis = restrictionsFile.openRead(); |
| 1452 | XmlPullParser parser = Xml.newPullParser(); |
| 1453 | parser.setInput(fis, null); |
| 1454 | int type; |
| 1455 | while ((type = parser.next()) != XmlPullParser.START_TAG |
| 1456 | && type != XmlPullParser.END_DOCUMENT) { |
| 1457 | ; |
| 1458 | } |
| 1459 | |
| 1460 | if (type != XmlPullParser.START_TAG) { |
| 1461 | Slog.e(LOG_TAG, "Unable to read restrictions file " |
| 1462 | + restrictionsFile.getBaseFile()); |
Amith Yamasani | 7e99bc0 | 2013-04-16 18:24:51 -0700 | [diff] [blame] | 1463 | return restrictions; |
Amith Yamasani | df2e92a | 2013-03-01 17:04:38 -0800 | [diff] [blame] | 1464 | } |
| 1465 | |
| 1466 | while ((type = parser.next()) != XmlPullParser.END_DOCUMENT) { |
| 1467 | if (type == XmlPullParser.START_TAG && parser.getName().equals(TAG_ENTRY)) { |
| 1468 | String key = parser.getAttributeValue(null, ATTR_KEY); |
Amith Yamasani | 7e99bc0 | 2013-04-16 18:24:51 -0700 | [diff] [blame] | 1469 | String valType = parser.getAttributeValue(null, ATTR_VALUE_TYPE); |
Amith Yamasani | df2e92a | 2013-03-01 17:04:38 -0800 | [diff] [blame] | 1470 | String multiple = parser.getAttributeValue(null, ATTR_MULTIPLE); |
| 1471 | if (multiple != null) { |
| 1472 | int count = Integer.parseInt(multiple); |
| 1473 | while (count > 0 && (type = parser.next()) != XmlPullParser.END_DOCUMENT) { |
| 1474 | if (type == XmlPullParser.START_TAG |
| 1475 | && parser.getName().equals(TAG_VALUE)) { |
| 1476 | values.add(parser.nextText().trim()); |
| 1477 | count--; |
| 1478 | } |
| 1479 | } |
| 1480 | String [] valueStrings = new String[values.size()]; |
| 1481 | values.toArray(valueStrings); |
Amith Yamasani | 7e99bc0 | 2013-04-16 18:24:51 -0700 | [diff] [blame] | 1482 | restrictions.putStringArray(key, valueStrings); |
| 1483 | } else if (ATTR_TYPE_BOOLEAN.equals(valType)) { |
| 1484 | restrictions.putBoolean(key, Boolean.parseBoolean( |
| 1485 | parser.nextText().trim())); |
Amith Yamasani | df2e92a | 2013-03-01 17:04:38 -0800 | [diff] [blame] | 1486 | } else { |
| 1487 | String value = parser.nextText().trim(); |
Amith Yamasani | 7e99bc0 | 2013-04-16 18:24:51 -0700 | [diff] [blame] | 1488 | restrictions.putString(key, value); |
Amith Yamasani | df2e92a | 2013-03-01 17:04:38 -0800 | [diff] [blame] | 1489 | } |
| 1490 | } |
| 1491 | } |
| 1492 | |
| 1493 | } catch (IOException ioe) { |
| 1494 | } catch (XmlPullParserException pe) { |
| 1495 | } finally { |
| 1496 | if (fis != null) { |
| 1497 | try { |
| 1498 | fis.close(); |
| 1499 | } catch (IOException e) { |
| 1500 | } |
| 1501 | } |
| 1502 | } |
Amith Yamasani | 7e99bc0 | 2013-04-16 18:24:51 -0700 | [diff] [blame] | 1503 | return restrictions; |
Amith Yamasani | df2e92a | 2013-03-01 17:04:38 -0800 | [diff] [blame] | 1504 | } |
| 1505 | |
| 1506 | private void writeApplicationRestrictionsLocked(String packageName, |
Amith Yamasani | 7e99bc0 | 2013-04-16 18:24:51 -0700 | [diff] [blame] | 1507 | Bundle restrictions, int userId) { |
Amith Yamasani | df2e92a | 2013-03-01 17:04:38 -0800 | [diff] [blame] | 1508 | FileOutputStream fos = null; |
| 1509 | AtomicFile restrictionsFile = new AtomicFile( |
| 1510 | new File(Environment.getUserSystemDirectory(userId), |
Amith Yamasani | fc95e70 | 2013-09-26 13:20:17 -0700 | [diff] [blame] | 1511 | packageToRestrictionsFileName(packageName))); |
Amith Yamasani | df2e92a | 2013-03-01 17:04:38 -0800 | [diff] [blame] | 1512 | try { |
| 1513 | fos = restrictionsFile.startWrite(); |
| 1514 | final BufferedOutputStream bos = new BufferedOutputStream(fos); |
| 1515 | |
| 1516 | // XmlSerializer serializer = XmlUtils.serializerInstance(); |
| 1517 | final XmlSerializer serializer = new FastXmlSerializer(); |
| 1518 | serializer.setOutput(bos, "utf-8"); |
| 1519 | serializer.startDocument(null, true); |
| 1520 | serializer.setFeature("http://xmlpull.org/v1/doc/features.html#indent-output", true); |
| 1521 | |
| 1522 | serializer.startTag(null, TAG_RESTRICTIONS); |
| 1523 | |
Amith Yamasani | 7e99bc0 | 2013-04-16 18:24:51 -0700 | [diff] [blame] | 1524 | for (String key : restrictions.keySet()) { |
| 1525 | Object value = restrictions.get(key); |
Amith Yamasani | df2e92a | 2013-03-01 17:04:38 -0800 | [diff] [blame] | 1526 | serializer.startTag(null, TAG_ENTRY); |
Amith Yamasani | 7e99bc0 | 2013-04-16 18:24:51 -0700 | [diff] [blame] | 1527 | serializer.attribute(null, ATTR_KEY, key); |
| 1528 | |
| 1529 | if (value instanceof Boolean) { |
| 1530 | serializer.attribute(null, ATTR_VALUE_TYPE, ATTR_TYPE_BOOLEAN); |
| 1531 | serializer.text(value.toString()); |
| 1532 | } else if (value == null || value instanceof String) { |
| 1533 | serializer.attribute(null, ATTR_VALUE_TYPE, ATTR_TYPE_STRING); |
| 1534 | serializer.text(value != null ? (String) value : ""); |
Amith Yamasani | df2e92a | 2013-03-01 17:04:38 -0800 | [diff] [blame] | 1535 | } else { |
Amith Yamasani | 7e99bc0 | 2013-04-16 18:24:51 -0700 | [diff] [blame] | 1536 | serializer.attribute(null, ATTR_VALUE_TYPE, ATTR_TYPE_STRING_ARRAY); |
| 1537 | String[] values = (String[]) value; |
Amith Yamasani | df2e92a | 2013-03-01 17:04:38 -0800 | [diff] [blame] | 1538 | serializer.attribute(null, ATTR_MULTIPLE, Integer.toString(values.length)); |
Amith Yamasani | 7e99bc0 | 2013-04-16 18:24:51 -0700 | [diff] [blame] | 1539 | for (String choice : values) { |
Amith Yamasani | df2e92a | 2013-03-01 17:04:38 -0800 | [diff] [blame] | 1540 | serializer.startTag(null, TAG_VALUE); |
Amith Yamasani | 7e99bc0 | 2013-04-16 18:24:51 -0700 | [diff] [blame] | 1541 | serializer.text(choice != null ? choice : ""); |
Amith Yamasani | df2e92a | 2013-03-01 17:04:38 -0800 | [diff] [blame] | 1542 | serializer.endTag(null, TAG_VALUE); |
| 1543 | } |
| 1544 | } |
| 1545 | serializer.endTag(null, TAG_ENTRY); |
| 1546 | } |
| 1547 | |
| 1548 | serializer.endTag(null, TAG_RESTRICTIONS); |
| 1549 | |
| 1550 | serializer.endDocument(); |
| 1551 | restrictionsFile.finishWrite(fos); |
| 1552 | } catch (Exception e) { |
| 1553 | restrictionsFile.failWrite(fos); |
| 1554 | Slog.e(LOG_TAG, "Error writing application restrictions list"); |
| 1555 | } |
| 1556 | } |
| 1557 | |
| 1558 | @Override |
Amith Yamasani | 2a00329 | 2012-08-14 18:25:45 -0700 | [diff] [blame] | 1559 | public int getUserSerialNumber(int userHandle) { |
Dianne Hackborn | 4428e17 | 2012-08-24 17:43:05 -0700 | [diff] [blame] | 1560 | synchronized (mPackagesLock) { |
Amith Yamasani | 2a00329 | 2012-08-14 18:25:45 -0700 | [diff] [blame] | 1561 | if (!exists(userHandle)) return -1; |
Amith Yamasani | 19526374 | 2012-08-21 15:40:12 -0700 | [diff] [blame] | 1562 | return getUserInfoLocked(userHandle).serialNumber; |
Amith Yamasani | 2a00329 | 2012-08-14 18:25:45 -0700 | [diff] [blame] | 1563 | } |
| 1564 | } |
| 1565 | |
| 1566 | @Override |
| 1567 | public int getUserHandle(int userSerialNumber) { |
Dianne Hackborn | 4428e17 | 2012-08-24 17:43:05 -0700 | [diff] [blame] | 1568 | synchronized (mPackagesLock) { |
Amith Yamasani | 2a00329 | 2012-08-14 18:25:45 -0700 | [diff] [blame] | 1569 | for (int userId : mUserIds) { |
Amith Yamasani | 19526374 | 2012-08-21 15:40:12 -0700 | [diff] [blame] | 1570 | if (getUserInfoLocked(userId).serialNumber == userSerialNumber) return userId; |
Amith Yamasani | 2a00329 | 2012-08-14 18:25:45 -0700 | [diff] [blame] | 1571 | } |
| 1572 | // Not found |
| 1573 | return -1; |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 1574 | } |
| 1575 | } |
| 1576 | |
Amith Yamasani | 0b28549 | 2011-04-14 17:35:23 -0700 | [diff] [blame] | 1577 | /** |
| 1578 | * Caches the list of user ids in an array, adjusting the array size when necessary. |
| 1579 | */ |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 1580 | private void updateUserIdsLocked() { |
Dianne Hackborn | d4ac8d7 | 2012-09-27 23:20:10 -0700 | [diff] [blame] | 1581 | int num = 0; |
Amith Yamasani | 0b28549 | 2011-04-14 17:35:23 -0700 | [diff] [blame] | 1582 | for (int i = 0; i < mUsers.size(); i++) { |
Dianne Hackborn | d4ac8d7 | 2012-09-27 23:20:10 -0700 | [diff] [blame] | 1583 | if (!mUsers.valueAt(i).partial) { |
| 1584 | num++; |
| 1585 | } |
| 1586 | } |
Amith Yamasani | 1638931 | 2012-10-17 21:20:14 -0700 | [diff] [blame] | 1587 | final int[] newUsers = new int[num]; |
| 1588 | int n = 0; |
Dianne Hackborn | d4ac8d7 | 2012-09-27 23:20:10 -0700 | [diff] [blame] | 1589 | for (int i = 0; i < mUsers.size(); i++) { |
| 1590 | if (!mUsers.valueAt(i).partial) { |
Amith Yamasani | 1638931 | 2012-10-17 21:20:14 -0700 | [diff] [blame] | 1591 | newUsers[n++] = mUsers.keyAt(i); |
Dianne Hackborn | d4ac8d7 | 2012-09-27 23:20:10 -0700 | [diff] [blame] | 1592 | } |
Amith Yamasani | 0b28549 | 2011-04-14 17:35:23 -0700 | [diff] [blame] | 1593 | } |
Dianne Hackborn | 7767eac | 2012-08-23 18:25:40 -0700 | [diff] [blame] | 1594 | mUserIds = newUsers; |
Amith Yamasani | 0b28549 | 2011-04-14 17:35:23 -0700 | [diff] [blame] | 1595 | } |
| 1596 | |
| 1597 | /** |
Amith Yamasani | 1a7472e | 2013-07-02 11:17:30 -0700 | [diff] [blame] | 1598 | * Make a note of the last started time of a user and do some cleanup. |
Amith Yamasani | 920ace0 | 2012-09-20 22:15:37 -0700 | [diff] [blame] | 1599 | * @param userId the user that was just foregrounded |
| 1600 | */ |
| 1601 | public void userForeground(int userId) { |
| 1602 | synchronized (mPackagesLock) { |
| 1603 | UserInfo user = mUsers.get(userId); |
| 1604 | long now = System.currentTimeMillis(); |
Dianne Hackborn | d4ac8d7 | 2012-09-27 23:20:10 -0700 | [diff] [blame] | 1605 | if (user == null || user.partial) { |
| 1606 | Slog.w(LOG_TAG, "userForeground: unknown user #" + userId); |
| 1607 | return; |
| 1608 | } |
| 1609 | if (now > EPOCH_PLUS_30_YEARS) { |
Amith Yamasani | 920ace0 | 2012-09-20 22:15:37 -0700 | [diff] [blame] | 1610 | user.lastLoggedInTime = now; |
| 1611 | writeUserLocked(user); |
| 1612 | } |
Amith Yamasani | 1a7472e | 2013-07-02 11:17:30 -0700 | [diff] [blame] | 1613 | // If this is not a restricted profile and there is no restrictions pin, clean up |
| 1614 | // all restrictions files that might have been left behind, else clean up just the |
| 1615 | // ones with uninstalled packages |
| 1616 | RestrictionsPinState pinState = mRestrictionsPinStates.get(userId); |
| 1617 | final long salt = pinState == null ? 0 : pinState.salt; |
| 1618 | cleanAppRestrictions(userId, (!user.isRestricted() && salt == 0)); |
Amith Yamasani | 920ace0 | 2012-09-20 22:15:37 -0700 | [diff] [blame] | 1619 | } |
| 1620 | } |
| 1621 | |
| 1622 | /** |
Amith Yamasani | 0b28549 | 2011-04-14 17:35:23 -0700 | [diff] [blame] | 1623 | * 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] | 1624 | * TODO: May not be a good idea to recycle ids, in case it results in confusion |
| 1625 | * for data and battery stats collection, or unexpected cross-talk. |
Amith Yamasani | 0b28549 | 2011-04-14 17:35:23 -0700 | [diff] [blame] | 1626 | * @return |
| 1627 | */ |
Dianne Hackborn | 5dc5a00 | 2012-09-15 19:33:48 -0700 | [diff] [blame] | 1628 | private int getNextAvailableIdLocked() { |
Dianne Hackborn | 4428e17 | 2012-08-24 17:43:05 -0700 | [diff] [blame] | 1629 | synchronized (mPackagesLock) { |
Jeff Sharkey | ffe0cb4 | 2012-11-05 17:24:43 -0800 | [diff] [blame] | 1630 | int i = MIN_USER_ID; |
Amith Yamasani | 19526374 | 2012-08-21 15:40:12 -0700 | [diff] [blame] | 1631 | while (i < Integer.MAX_VALUE) { |
Jeff Sharkey | ffe0cb4 | 2012-11-05 17:24:43 -0800 | [diff] [blame] | 1632 | if (mUsers.indexOfKey(i) < 0 && !mRemovingUserIds.get(i)) { |
Amith Yamasani | 19526374 | 2012-08-21 15:40:12 -0700 | [diff] [blame] | 1633 | break; |
| 1634 | } |
| 1635 | i++; |
Amith Yamasani | 4b2e934 | 2011-03-31 12:38:53 -0700 | [diff] [blame] | 1636 | } |
Amith Yamasani | 19526374 | 2012-08-21 15:40:12 -0700 | [diff] [blame] | 1637 | return i; |
Amith Yamasani | 4b2e934 | 2011-03-31 12:38:53 -0700 | [diff] [blame] | 1638 | } |
Amith Yamasani | 4b2e934 | 2011-03-31 12:38:53 -0700 | [diff] [blame] | 1639 | } |
Amith Yamasani | 920ace0 | 2012-09-20 22:15:37 -0700 | [diff] [blame] | 1640 | |
Amith Yamasani | fc95e70 | 2013-09-26 13:20:17 -0700 | [diff] [blame] | 1641 | private String packageToRestrictionsFileName(String packageName) { |
| 1642 | return RESTRICTIONS_FILE_PREFIX + packageName + XML_SUFFIX; |
| 1643 | } |
| 1644 | |
| 1645 | private String restrictionsFileNameToPackage(String fileName) { |
| 1646 | return fileName.substring(RESTRICTIONS_FILE_PREFIX.length(), |
| 1647 | (int) (fileName.length() - XML_SUFFIX.length())); |
| 1648 | } |
| 1649 | |
Amith Yamasani | 920ace0 | 2012-09-20 22:15:37 -0700 | [diff] [blame] | 1650 | @Override |
| 1651 | protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) { |
| 1652 | if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.DUMP) |
| 1653 | != PackageManager.PERMISSION_GRANTED) { |
| 1654 | pw.println("Permission Denial: can't dump UserManager from from pid=" |
| 1655 | + Binder.getCallingPid() |
| 1656 | + ", uid=" + Binder.getCallingUid() |
| 1657 | + " without permission " |
| 1658 | + android.Manifest.permission.DUMP); |
| 1659 | return; |
| 1660 | } |
| 1661 | |
| 1662 | long now = System.currentTimeMillis(); |
| 1663 | StringBuilder sb = new StringBuilder(); |
| 1664 | synchronized (mPackagesLock) { |
| 1665 | pw.println("Users:"); |
| 1666 | for (int i = 0; i < mUsers.size(); i++) { |
| 1667 | UserInfo user = mUsers.valueAt(i); |
| 1668 | if (user == null) continue; |
Amith Yamasani | 634cf31 | 2012-10-04 17:34:21 -0700 | [diff] [blame] | 1669 | pw.print(" "); pw.print(user); pw.print(" serialNo="); pw.print(user.serialNumber); |
Jeff Sharkey | ffe0cb4 | 2012-11-05 17:24:43 -0800 | [diff] [blame] | 1670 | if (mRemovingUserIds.get(mUsers.keyAt(i))) pw.print(" <removing> "); |
Dianne Hackborn | d4ac8d7 | 2012-09-27 23:20:10 -0700 | [diff] [blame] | 1671 | if (user.partial) pw.print(" <partial>"); |
| 1672 | pw.println(); |
Amith Yamasani | 920ace0 | 2012-09-20 22:15:37 -0700 | [diff] [blame] | 1673 | pw.print(" Created: "); |
| 1674 | if (user.creationTime == 0) { |
| 1675 | pw.println("<unknown>"); |
| 1676 | } else { |
| 1677 | sb.setLength(0); |
| 1678 | TimeUtils.formatDuration(now - user.creationTime, sb); |
| 1679 | sb.append(" ago"); |
| 1680 | pw.println(sb); |
| 1681 | } |
| 1682 | pw.print(" Last logged in: "); |
| 1683 | if (user.lastLoggedInTime == 0) { |
| 1684 | pw.println("<unknown>"); |
| 1685 | } else { |
| 1686 | sb.setLength(0); |
| 1687 | TimeUtils.formatDuration(now - user.lastLoggedInTime, sb); |
| 1688 | sb.append(" ago"); |
| 1689 | pw.println(sb); |
| 1690 | } |
| 1691 | } |
| 1692 | } |
| 1693 | } |
Amith Yamasani | 1a7472e | 2013-07-02 11:17:30 -0700 | [diff] [blame] | 1694 | |
| 1695 | private PackageMonitor mUserPackageMonitor = new PackageMonitor() { |
| 1696 | @Override |
| 1697 | public void onPackageRemoved(String pkg, int uid) { |
| 1698 | final int userId = this.getChangingUserId(); |
| 1699 | // Package could be disappearing because it is being blocked, so also check if |
| 1700 | // it has been uninstalled. |
| 1701 | final boolean uninstalled = isPackageDisappearing(pkg) == PACKAGE_PERMANENT_CHANGE; |
| 1702 | if (uninstalled && userId >= 0 && !isPackageInstalled(pkg, userId)) { |
| 1703 | cleanAppRestrictionsForPackage(pkg, userId); |
| 1704 | } |
| 1705 | } |
| 1706 | }; |
Amith Yamasani | 4b2e934 | 2011-03-31 12:38:53 -0700 | [diff] [blame] | 1707 | } |