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