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