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