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