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