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