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