John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 1 | /** |
| 2 | * Copyright (c) 2014, 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.notification; |
| 18 | |
Felipe Leme | a1b79bf | 2016-05-24 13:06:54 -0700 | [diff] [blame] | 19 | import static android.content.Context.BIND_ALLOW_WHITELIST_MANAGEMENT; |
| 20 | import static android.content.Context.BIND_AUTO_CREATE; |
| 21 | import static android.content.Context.BIND_FOREGROUND_SERVICE; |
Esteban Talavera | 9c6458d | 2017-03-30 17:59:50 +0100 | [diff] [blame] | 22 | import static android.content.Context.DEVICE_POLICY_SERVICE; |
Julia Reynolds | ca8e535 | 2018-09-18 13:39:26 -0400 | [diff] [blame] | 23 | import static android.os.UserHandle.USER_ALL; |
Jay Aliomer | 76e1f272 | 2020-03-05 12:36:38 -0500 | [diff] [blame] | 24 | import static android.os.UserHandle.USER_SYSTEM; |
Felipe Leme | a1b79bf | 2016-05-24 13:06:54 -0700 | [diff] [blame] | 25 | |
Ruben Brunk | e24b9a6 | 2016-02-16 21:38:24 -0800 | [diff] [blame] | 26 | import android.annotation.NonNull; |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 27 | import android.app.ActivityManager; |
| 28 | import android.app.PendingIntent; |
Esteban Talavera | 9c6458d | 2017-03-30 17:59:50 +0100 | [diff] [blame] | 29 | import android.app.admin.DevicePolicyManager; |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 30 | import android.content.ComponentName; |
| 31 | import android.content.ContentResolver; |
| 32 | import android.content.Context; |
| 33 | import android.content.Intent; |
| 34 | import android.content.ServiceConnection; |
| 35 | import android.content.pm.ApplicationInfo; |
Julia Reynolds | a75c752 | 2017-03-21 17:34:25 -0400 | [diff] [blame] | 36 | import android.content.pm.IPackageManager; |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 37 | import android.content.pm.PackageManager; |
| 38 | import android.content.pm.PackageManager.NameNotFoundException; |
| 39 | import android.content.pm.ResolveInfo; |
| 40 | import android.content.pm.ServiceInfo; |
| 41 | import android.content.pm.UserInfo; |
Esteban Talavera | 9c6458d | 2017-03-30 17:59:50 +0100 | [diff] [blame] | 42 | import android.os.Binder; |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 43 | import android.os.Build; |
Ryan Lothian | 4a86a51 | 2017-12-04 11:56:58 -0500 | [diff] [blame] | 44 | import android.os.Handler; |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 45 | import android.os.IBinder; |
| 46 | import android.os.IInterface; |
Ryan Lothian | 4a86a51 | 2017-12-04 11:56:58 -0500 | [diff] [blame] | 47 | import android.os.Looper; |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 48 | import android.os.RemoteException; |
| 49 | import android.os.UserHandle; |
| 50 | import android.os.UserManager; |
| 51 | import android.provider.Settings; |
Kweku Adams | 93304b6 | 2017-09-20 17:03:00 -0700 | [diff] [blame] | 52 | import android.service.notification.ManagedServiceInfoProto; |
| 53 | import android.service.notification.ManagedServicesProto; |
| 54 | import android.service.notification.ManagedServicesProto.ServiceProto; |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 55 | import android.text.TextUtils; |
Julia Reynolds | b852e56 | 2017-06-06 16:14:18 -0400 | [diff] [blame] | 56 | import android.util.ArrayMap; |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 57 | import android.util.ArraySet; |
Julia Reynolds | ca8e535 | 2018-09-18 13:39:26 -0400 | [diff] [blame] | 58 | import android.util.IntArray; |
John Spurlock | 4db0d98 | 2014-08-13 09:19:03 -0400 | [diff] [blame] | 59 | import android.util.Log; |
Andrew Zeng | 190c067 | 2018-06-28 15:15:05 -0700 | [diff] [blame] | 60 | import android.util.Pair; |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 61 | import android.util.Slog; |
| 62 | import android.util.SparseArray; |
Kweku Adams | 93304b6 | 2017-09-20 17:03:00 -0700 | [diff] [blame] | 63 | import android.util.proto.ProtoOutputStream; |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 64 | |
Julia Reynolds | ca8e535 | 2018-09-18 13:39:26 -0400 | [diff] [blame] | 65 | import com.android.internal.annotations.GuardedBy; |
| 66 | import com.android.internal.annotations.VisibleForTesting; |
Julia Reynolds | d1bf5f0 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 67 | import com.android.internal.util.XmlUtils; |
Julia Reynolds | d0ceefa | 2019-03-03 16:10:52 -0500 | [diff] [blame] | 68 | import com.android.internal.util.function.TriPredicate; |
John Spurlock | 25e2d24 | 2014-06-27 13:58:23 -0400 | [diff] [blame] | 69 | import com.android.server.notification.NotificationManagerService.DumpFilter; |
| 70 | |
Julia Reynolds | b852e56 | 2017-06-06 16:14:18 -0400 | [diff] [blame] | 71 | import org.xmlpull.v1.XmlPullParser; |
| 72 | import org.xmlpull.v1.XmlPullParserException; |
| 73 | import org.xmlpull.v1.XmlSerializer; |
| 74 | |
| 75 | import java.io.IOException; |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 76 | import java.io.PrintWriter; |
| 77 | import java.util.ArrayList; |
John Spurlock | e77bb36 | 2014-04-26 10:24:59 -0400 | [diff] [blame] | 78 | import java.util.Arrays; |
Julia Reynolds | 9a86cc0 | 2016-02-10 15:38:15 -0500 | [diff] [blame] | 79 | import java.util.HashSet; |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 80 | import java.util.List; |
Julia Reynolds | ca8e535 | 2018-09-18 13:39:26 -0400 | [diff] [blame] | 81 | import java.util.Objects; |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 82 | import java.util.Set; |
| 83 | |
| 84 | /** |
| 85 | * Manages the lifecycle of application-provided services bound by system server. |
| 86 | * |
| 87 | * Services managed by this helper must have: |
| 88 | * - An associated system settings value with a list of enabled component names. |
| 89 | * - A well-known action for services to use in their intent-filter. |
| 90 | * - A system permission for services to require in order to ensure system has exclusive binding. |
| 91 | * - A settings page for user configuration of enabled services, and associated intent action. |
| 92 | * - A remote interface definition (aidl) provided by the service used for communication. |
| 93 | */ |
| 94 | abstract public class ManagedServices { |
| 95 | protected final String TAG = getClass().getSimpleName(); |
John Spurlock | 4db0d98 | 2014-08-13 09:19:03 -0400 | [diff] [blame] | 96 | protected final boolean DEBUG = Log.isLoggable(TAG, Log.DEBUG); |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 97 | |
Ryan Lothian | 4a86a51 | 2017-12-04 11:56:58 -0500 | [diff] [blame] | 98 | private static final int ON_BINDING_DIED_REBIND_DELAY_MS = 10000; |
Julia Reynolds | c279b99 | 2015-10-30 08:23:51 -0400 | [diff] [blame] | 99 | protected static final String ENABLED_SERVICES_SEPARATOR = ":"; |
Jay Aliomer | 76e1f272 | 2020-03-05 12:36:38 -0500 | [diff] [blame] | 100 | private static final String DB_VERSION_1 = "1"; |
| 101 | |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 102 | |
Julia Reynolds | b852e56 | 2017-06-06 16:14:18 -0400 | [diff] [blame] | 103 | /** |
| 104 | * List of components and apps that can have running {@link ManagedServices}. |
| 105 | */ |
| 106 | static final String TAG_MANAGED_SERVICES = "service_listing"; |
| 107 | static final String ATT_APPROVED_LIST = "approved"; |
| 108 | static final String ATT_USER_ID = "user"; |
| 109 | static final String ATT_IS_PRIMARY = "primary"; |
Julia Reynolds | 7380d87 | 2018-01-12 10:28:26 -0500 | [diff] [blame] | 110 | static final String ATT_VERSION = "version"; |
Jay Aliomer | 4204f25 | 2019-08-26 11:36:53 -0400 | [diff] [blame] | 111 | static final String ATT_DEFAULTS = "defaults"; |
Julia Reynolds | 7380d87 | 2018-01-12 10:28:26 -0500 | [diff] [blame] | 112 | |
Jay Aliomer | 76e1f272 | 2020-03-05 12:36:38 -0500 | [diff] [blame] | 113 | static final int DB_VERSION = 2; |
Julia Reynolds | b852e56 | 2017-06-06 16:14:18 -0400 | [diff] [blame] | 114 | |
| 115 | static final int APPROVAL_BY_PACKAGE = 0; |
| 116 | static final int APPROVAL_BY_COMPONENT = 1; |
| 117 | |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 118 | protected final Context mContext; |
John Spurlock | e77bb36 | 2014-04-26 10:24:59 -0400 | [diff] [blame] | 119 | protected final Object mMutex; |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 120 | private final UserProfiles mUserProfiles; |
Julia Reynolds | a75c752 | 2017-03-21 17:34:25 -0400 | [diff] [blame] | 121 | private final IPackageManager mPm; |
Julia Reynolds | d6d5a59 | 2018-04-02 11:03:32 -0400 | [diff] [blame] | 122 | protected final UserManager mUm; |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 123 | private final Config mConfig; |
Ryan Lothian | 4a86a51 | 2017-12-04 11:56:58 -0500 | [diff] [blame] | 124 | private final Handler mHandler = new Handler(Looper.getMainLooper()); |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 125 | |
| 126 | // contains connections to all connected services, including app services |
| 127 | // and system services |
Julia Reynolds | b852e56 | 2017-06-06 16:14:18 -0400 | [diff] [blame] | 128 | private final ArrayList<ManagedServiceInfo> mServices = new ArrayList<>(); |
Andrew Zeng | 190c067 | 2018-06-28 15:15:05 -0700 | [diff] [blame] | 129 | /** |
| 130 | * The services that have been bound by us. If the service is also connected, it will also |
| 131 | * be in {@link #mServices}. |
| 132 | */ |
| 133 | private final ArrayList<Pair<ComponentName, Integer>> mServicesBound = new ArrayList<>(); |
| 134 | private final ArraySet<Pair<ComponentName, Integer>> mServicesRebinding = new ArraySet<>(); |
Jay Aliomer | 4204f25 | 2019-08-26 11:36:53 -0400 | [diff] [blame] | 135 | // we need these packages to be protected because classes that inherit from it need to see it |
| 136 | protected final Object mDefaultsLock = new Object(); |
| 137 | protected final ArraySet<ComponentName> mDefaultComponents = new ArraySet<>(); |
| 138 | protected final ArraySet<String> mDefaultPackages = new ArraySet<>(); |
Ryan Lothian | 4a86a51 | 2017-12-04 11:56:58 -0500 | [diff] [blame] | 139 | |
Chris Wren | ab41eec | 2016-01-04 18:01:27 -0500 | [diff] [blame] | 140 | // lists the component names of all enabled (and therefore potentially connected) |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 141 | // app services for current profiles. |
| 142 | private ArraySet<ComponentName> mEnabledServicesForCurrentProfiles |
Julia Reynolds | b852e56 | 2017-06-06 16:14:18 -0400 | [diff] [blame] | 143 | = new ArraySet<>(); |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 144 | // Just the packages from mEnabledServicesForCurrentProfiles |
Julia Reynolds | b852e56 | 2017-06-06 16:14:18 -0400 | [diff] [blame] | 145 | private ArraySet<String> mEnabledServicesPackageNames = new ArraySet<>(); |
Chris Wren | ab41eec | 2016-01-04 18:01:27 -0500 | [diff] [blame] | 146 | // List of enabled packages that have nevertheless asked not to be run |
| 147 | private ArraySet<ComponentName> mSnoozingForCurrentProfiles = new ArraySet<>(); |
Denis Kuznetsov | 76c0268 | 2015-09-17 19:57:00 +0200 | [diff] [blame] | 148 | |
Julia Reynolds | b852e56 | 2017-06-06 16:14:18 -0400 | [diff] [blame] | 149 | // List of approved packages or components (by user, then by primary/secondary) that are |
| 150 | // allowed to be bound as managed services. A package or component appearing in this list does |
| 151 | // not mean that we are currently bound to said package/component. |
| 152 | private ArrayMap<Integer, ArrayMap<Boolean, ArraySet<String>>> mApproved = new ArrayMap<>(); |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 153 | |
Julia Reynolds | b852e56 | 2017-06-06 16:14:18 -0400 | [diff] [blame] | 154 | // True if approved services are stored in xml, not settings. |
| 155 | private boolean mUseXml; |
Christopher Tate | 6597e34 | 2015-02-17 12:15:25 -0800 | [diff] [blame] | 156 | |
Julia Reynolds | b852e56 | 2017-06-06 16:14:18 -0400 | [diff] [blame] | 157 | // Whether managed services are approved individually or package wide |
| 158 | protected int mApprovalLevel; |
| 159 | |
| 160 | public ManagedServices(Context context, Object mutex, UserProfiles userProfiles, |
| 161 | IPackageManager pm) { |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 162 | mContext = context; |
| 163 | mMutex = mutex; |
| 164 | mUserProfiles = userProfiles; |
Julia Reynolds | b852e56 | 2017-06-06 16:14:18 -0400 | [diff] [blame] | 165 | mPm = pm; |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 166 | mConfig = getConfig(); |
Julia Reynolds | b852e56 | 2017-06-06 16:14:18 -0400 | [diff] [blame] | 167 | mApprovalLevel = APPROVAL_BY_COMPONENT; |
Julia Reynolds | 4552300 | 2017-09-12 09:43:57 -0400 | [diff] [blame] | 168 | mUm = (UserManager) mContext.getSystemService(Context.USER_SERVICE); |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 169 | } |
| 170 | |
| 171 | abstract protected Config getConfig(); |
| 172 | |
| 173 | private String getCaption() { |
| 174 | return mConfig.caption; |
| 175 | } |
| 176 | |
| 177 | abstract protected IInterface asInterface(IBinder binder); |
| 178 | |
Chris Wren | 51017d0 | 2015-12-15 15:34:46 -0500 | [diff] [blame] | 179 | abstract protected boolean checkType(IInterface service); |
| 180 | |
John Spurlock | 3b98b3f | 2014-05-01 09:08:48 -0400 | [diff] [blame] | 181 | abstract protected void onServiceAdded(ManagedServiceInfo info); |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 182 | |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 183 | protected List<ManagedServiceInfo> getServices() { |
| 184 | synchronized (mMutex) { |
| 185 | List<ManagedServiceInfo> services = new ArrayList<>(mServices); |
| 186 | return services; |
| 187 | } |
| 188 | } |
| 189 | |
Jay Aliomer | 4204f25 | 2019-08-26 11:36:53 -0400 | [diff] [blame] | 190 | protected void addDefaultComponentOrPackage(String packageOrComponent) { |
Julia Reynolds | 57a974b | 2019-10-07 11:51:47 -0400 | [diff] [blame] | 191 | if (!TextUtils.isEmpty(packageOrComponent)) { |
Jay Aliomer | 4204f25 | 2019-08-26 11:36:53 -0400 | [diff] [blame] | 192 | synchronized (mDefaultsLock) { |
Jay Aliomer | 76e1f272 | 2020-03-05 12:36:38 -0500 | [diff] [blame] | 193 | if (mApprovalLevel == APPROVAL_BY_PACKAGE) { |
Jay Aliomer | 4204f25 | 2019-08-26 11:36:53 -0400 | [diff] [blame] | 194 | mDefaultPackages.add(packageOrComponent); |
Jay Aliomer | 76e1f272 | 2020-03-05 12:36:38 -0500 | [diff] [blame] | 195 | return; |
| 196 | } |
| 197 | ComponentName cn = ComponentName.unflattenFromString(packageOrComponent); |
| 198 | if (cn != null && mApprovalLevel == APPROVAL_BY_COMPONENT) { |
Jay Aliomer | 4204f25 | 2019-08-26 11:36:53 -0400 | [diff] [blame] | 199 | mDefaultPackages.add(cn.getPackageName()); |
| 200 | mDefaultComponents.add(cn); |
Jay Aliomer | 76e1f272 | 2020-03-05 12:36:38 -0500 | [diff] [blame] | 201 | return; |
Jay Aliomer | 4204f25 | 2019-08-26 11:36:53 -0400 | [diff] [blame] | 202 | } |
| 203 | } |
| 204 | } |
| 205 | } |
| 206 | |
Jay Aliomer | 76e1f272 | 2020-03-05 12:36:38 -0500 | [diff] [blame] | 207 | protected abstract void loadDefaultsFromConfig(); |
| 208 | |
Jay Aliomer | 4204f25 | 2019-08-26 11:36:53 -0400 | [diff] [blame] | 209 | boolean isDefaultComponentOrPackage(String packageOrComponent) { |
| 210 | synchronized (mDefaultsLock) { |
| 211 | ComponentName cn = ComponentName.unflattenFromString(packageOrComponent); |
| 212 | if (cn == null) { |
| 213 | return mDefaultPackages.contains(packageOrComponent); |
| 214 | } else { |
| 215 | return mDefaultComponents.contains(cn); |
| 216 | } |
| 217 | } |
| 218 | } |
| 219 | |
| 220 | ArraySet<ComponentName> getDefaultComponents() { |
| 221 | synchronized (mDefaultsLock) { |
| 222 | return new ArraySet<>(mDefaultComponents); |
| 223 | } |
| 224 | } |
| 225 | |
| 226 | ArraySet<String> getDefaultPackages() { |
| 227 | synchronized (mDefaultsLock) { |
| 228 | return new ArraySet<>(mDefaultPackages); |
| 229 | } |
| 230 | } |
| 231 | |
| 232 | /** |
| 233 | * When resetting a package, we need to enable default components that belong to that packages |
| 234 | * we also need to disable components that are not default to return the managed service state |
| 235 | * to when a new android device is first turned on for that package. |
| 236 | * |
| 237 | * @param packageName package to reset. |
| 238 | * @param userId the android user id |
| 239 | * @return a list of components that were permitted |
| 240 | */ |
| 241 | @NonNull |
| 242 | ArrayMap<Boolean, ArrayList<ComponentName>> resetComponents(String packageName, int userId) { |
| 243 | // components that we want to enable |
| 244 | ArrayList<ComponentName> componentsToEnable = |
| 245 | new ArrayList<>(mDefaultComponents.size()); |
| 246 | |
| 247 | // components that were removed |
| 248 | ArrayList<ComponentName> disabledComponents = |
| 249 | new ArrayList<>(mDefaultComponents.size()); |
| 250 | |
| 251 | // all components that are enabled now |
| 252 | ArraySet<ComponentName> enabledComponents = |
| 253 | new ArraySet<>(getAllowedComponents(userId)); |
| 254 | |
| 255 | boolean changed = false; |
| 256 | |
| 257 | synchronized (mDefaultsLock) { |
| 258 | // record all components that are enabled but should not be by default |
| 259 | for (int i = 0; i < mDefaultComponents.size() && enabledComponents.size() > 0; i++) { |
| 260 | ComponentName currentDefault = mDefaultComponents.valueAt(i); |
| 261 | if (packageName.equals(currentDefault.getPackageName()) |
| 262 | && !enabledComponents.contains(currentDefault)) { |
| 263 | componentsToEnable.add(currentDefault); |
| 264 | } |
| 265 | } |
| 266 | synchronized (mApproved) { |
| 267 | final ArrayMap<Boolean, ArraySet<String>> approvedByType = mApproved.get( |
| 268 | userId); |
| 269 | if (approvedByType != null) { |
| 270 | final int M = approvedByType.size(); |
| 271 | for (int j = 0; j < M; j++) { |
| 272 | final ArraySet<String> approved = approvedByType.valueAt(j); |
| 273 | for (int i = 0; i < enabledComponents.size(); i++) { |
| 274 | ComponentName currentComponent = enabledComponents.valueAt(i); |
| 275 | if (packageName.equals(currentComponent.getPackageName()) |
| 276 | && !mDefaultComponents.contains(currentComponent)) { |
| 277 | if (approved.remove(currentComponent.flattenToString())) { |
| 278 | disabledComponents.add(currentComponent); |
| 279 | changed = true; |
| 280 | } |
| 281 | } |
| 282 | } |
| 283 | for (int i = 0; i < componentsToEnable.size(); i++) { |
| 284 | ComponentName candidate = componentsToEnable.get(i); |
| 285 | changed |= approved.add(candidate.flattenToString()); |
| 286 | } |
| 287 | } |
| 288 | |
| 289 | } |
| 290 | } |
| 291 | } |
| 292 | if (changed) rebindServices(false, USER_ALL); |
| 293 | |
| 294 | ArrayMap<Boolean, ArrayList<ComponentName>> changes = new ArrayMap<>(); |
| 295 | changes.put(true, componentsToEnable); |
| 296 | changes.put(false, disabledComponents); |
| 297 | |
| 298 | return changes; |
| 299 | } |
| 300 | |
Amith Yamasani | e5bfeee | 2018-09-05 18:52:35 -0700 | [diff] [blame] | 301 | protected int getBindFlags() { |
| 302 | return BIND_AUTO_CREATE | BIND_FOREGROUND_SERVICE | BIND_ALLOW_WHITELIST_MANAGEMENT; |
| 303 | } |
| 304 | |
John Spurlock | e77bb36 | 2014-04-26 10:24:59 -0400 | [diff] [blame] | 305 | protected void onServiceRemovedLocked(ManagedServiceInfo removed) { } |
| 306 | |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 307 | private ManagedServiceInfo newServiceInfo(IInterface service, |
Julia Reynolds | b852e56 | 2017-06-06 16:14:18 -0400 | [diff] [blame] | 308 | ComponentName component, int userId, boolean isSystem, ServiceConnection connection, |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 309 | int targetSdkVersion) { |
Julia Reynolds | b852e56 | 2017-06-06 16:14:18 -0400 | [diff] [blame] | 310 | return new ManagedServiceInfo(service, component, userId, isSystem, connection, |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 311 | targetSdkVersion); |
| 312 | } |
| 313 | |
Julia Reynolds | b852e56 | 2017-06-06 16:14:18 -0400 | [diff] [blame] | 314 | public void onBootPhaseAppsCanStart() {} |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 315 | |
John Spurlock | 25e2d24 | 2014-06-27 13:58:23 -0400 | [diff] [blame] | 316 | public void dump(PrintWriter pw, DumpFilter filter) { |
Julia Reynolds | b852e56 | 2017-06-06 16:14:18 -0400 | [diff] [blame] | 317 | pw.println(" Allowed " + getCaption() + "s:"); |
Julia Reynolds | b776e39 | 2019-07-25 12:36:14 -0400 | [diff] [blame] | 318 | synchronized (mApproved) { |
| 319 | final int N = mApproved.size(); |
| 320 | for (int i = 0; i < N; i++) { |
| 321 | final int userId = mApproved.keyAt(i); |
| 322 | final ArrayMap<Boolean, ArraySet<String>> approvedByType = mApproved.valueAt(i); |
| 323 | if (approvedByType != null) { |
| 324 | final int M = approvedByType.size(); |
| 325 | for (int j = 0; j < M; j++) { |
| 326 | final boolean isPrimary = approvedByType.keyAt(j); |
| 327 | final ArraySet<String> approved = approvedByType.valueAt(j); |
| 328 | if (approvedByType != null && approvedByType.size() > 0) { |
| 329 | pw.println(" " + String.join(ENABLED_SERVICES_SEPARATOR, approved) |
| 330 | + " (user: " + userId + " isPrimary: " + isPrimary + ")"); |
| 331 | } |
Julia Reynolds | b852e56 | 2017-06-06 16:14:18 -0400 | [diff] [blame] | 332 | } |
| 333 | } |
| 334 | } |
| 335 | } |
| 336 | |
John Spurlock | e77bb36 | 2014-04-26 10:24:59 -0400 | [diff] [blame] | 337 | pw.println(" All " + getCaption() + "s (" + mEnabledServicesForCurrentProfiles.size() |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 338 | + ") enabled for current profiles:"); |
| 339 | for (ComponentName cmpt : mEnabledServicesForCurrentProfiles) { |
John Spurlock | 25e2d24 | 2014-06-27 13:58:23 -0400 | [diff] [blame] | 340 | if (filter != null && !filter.matches(cmpt)) continue; |
John Spurlock | e77bb36 | 2014-04-26 10:24:59 -0400 | [diff] [blame] | 341 | pw.println(" " + cmpt); |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 342 | } |
| 343 | |
John Spurlock | e77bb36 | 2014-04-26 10:24:59 -0400 | [diff] [blame] | 344 | pw.println(" Live " + getCaption() + "s (" + mServices.size() + "):"); |
Julia Reynolds | 3ff1fd9 | 2018-08-29 09:57:11 -0400 | [diff] [blame] | 345 | synchronized (mMutex) { |
| 346 | for (ManagedServiceInfo info : mServices) { |
| 347 | if (filter != null && !filter.matches(info.component)) continue; |
| 348 | pw.println(" " + info.component |
| 349 | + " (user " + info.userid + "): " + info.service |
| 350 | + (info.isSystem ? " SYSTEM" : "") |
| 351 | + (info.isGuest(this) ? " GUEST" : "")); |
| 352 | } |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 353 | } |
Chris Wren | ab41eec | 2016-01-04 18:01:27 -0500 | [diff] [blame] | 354 | |
| 355 | pw.println(" Snoozed " + getCaption() + "s (" + |
| 356 | mSnoozingForCurrentProfiles.size() + "):"); |
| 357 | for (ComponentName name : mSnoozingForCurrentProfiles) { |
| 358 | pw.println(" " + name.flattenToShortString()); |
| 359 | } |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 360 | } |
| 361 | |
Kweku Adams | 93304b6 | 2017-09-20 17:03:00 -0700 | [diff] [blame] | 362 | public void dump(ProtoOutputStream proto, DumpFilter filter) { |
| 363 | proto.write(ManagedServicesProto.CAPTION, getCaption()); |
Julia Reynolds | b776e39 | 2019-07-25 12:36:14 -0400 | [diff] [blame] | 364 | synchronized (mApproved) { |
| 365 | final int N = mApproved.size(); |
| 366 | for (int i = 0; i < N; i++) { |
| 367 | final int userId = mApproved.keyAt(i); |
| 368 | final ArrayMap<Boolean, ArraySet<String>> approvedByType = mApproved.valueAt(i); |
| 369 | if (approvedByType != null) { |
| 370 | final int M = approvedByType.size(); |
| 371 | for (int j = 0; j < M; j++) { |
| 372 | final boolean isPrimary = approvedByType.keyAt(j); |
| 373 | final ArraySet<String> approved = approvedByType.valueAt(j); |
| 374 | if (approvedByType != null && approvedByType.size() > 0) { |
| 375 | final long sToken = proto.start(ManagedServicesProto.APPROVED); |
| 376 | for (String s : approved) { |
| 377 | proto.write(ServiceProto.NAME, s); |
| 378 | } |
| 379 | proto.write(ServiceProto.USER_ID, userId); |
| 380 | proto.write(ServiceProto.IS_PRIMARY, isPrimary); |
| 381 | proto.end(sToken); |
Kweku Adams | 93304b6 | 2017-09-20 17:03:00 -0700 | [diff] [blame] | 382 | } |
Kweku Adams | 93304b6 | 2017-09-20 17:03:00 -0700 | [diff] [blame] | 383 | } |
| 384 | } |
| 385 | } |
| 386 | } |
| 387 | |
| 388 | for (ComponentName cmpt : mEnabledServicesForCurrentProfiles) { |
| 389 | if (filter != null && !filter.matches(cmpt)) continue; |
Jeffrey Huang | cb78285 | 2019-12-05 11:28:11 -0800 | [diff] [blame] | 390 | cmpt.dumpDebug(proto, ManagedServicesProto.ENABLED); |
Kweku Adams | 93304b6 | 2017-09-20 17:03:00 -0700 | [diff] [blame] | 391 | } |
| 392 | |
Julia Reynolds | 3ff1fd9 | 2018-08-29 09:57:11 -0400 | [diff] [blame] | 393 | synchronized (mMutex) { |
| 394 | for (ManagedServiceInfo info : mServices) { |
| 395 | if (filter != null && !filter.matches(info.component)) continue; |
Jeffrey Huang | cb78285 | 2019-12-05 11:28:11 -0800 | [diff] [blame] | 396 | info.dumpDebug(proto, ManagedServicesProto.LIVE_SERVICES, this); |
Julia Reynolds | 3ff1fd9 | 2018-08-29 09:57:11 -0400 | [diff] [blame] | 397 | } |
Kweku Adams | 93304b6 | 2017-09-20 17:03:00 -0700 | [diff] [blame] | 398 | } |
| 399 | |
| 400 | for (ComponentName name : mSnoozingForCurrentProfiles) { |
Jeffrey Huang | cb78285 | 2019-12-05 11:28:11 -0800 | [diff] [blame] | 401 | name.dumpDebug(proto, ManagedServicesProto.SNOOZED); |
Kweku Adams | 93304b6 | 2017-09-20 17:03:00 -0700 | [diff] [blame] | 402 | } |
| 403 | } |
| 404 | |
Julia Reynolds | fa206a4 | 2017-08-14 13:22:23 -0400 | [diff] [blame] | 405 | protected void onSettingRestored(String element, String value, int backupSdkInt, int userId) { |
Julia Reynolds | b852e56 | 2017-06-06 16:14:18 -0400 | [diff] [blame] | 406 | if (!mUseXml) { |
| 407 | Slog.d(TAG, "Restored managed service setting: " + element); |
| 408 | if (mConfig.secureSettingName.equals(element) || |
| 409 | (mConfig.secondarySettingName != null |
| 410 | && mConfig.secondarySettingName.equals(element))) { |
Julia Reynolds | fa206a4 | 2017-08-14 13:22:23 -0400 | [diff] [blame] | 411 | if (backupSdkInt < Build.VERSION_CODES.O) { |
| 412 | // automatic system grants were added in O, so append the approved apps |
| 413 | // rather than wiping out the setting |
| 414 | String currentSetting = |
| 415 | getApproved(userId, mConfig.secureSettingName.equals(element)); |
| 416 | if (!TextUtils.isEmpty(currentSetting)) { |
| 417 | if (!TextUtils.isEmpty(value)) { |
| 418 | value = value + ENABLED_SERVICES_SEPARATOR + currentSetting; |
| 419 | } else { |
| 420 | value = currentSetting; |
| 421 | } |
| 422 | } |
| 423 | } |
Julia Reynolds | b852e56 | 2017-06-06 16:14:18 -0400 | [diff] [blame] | 424 | Settings.Secure.putStringForUser( |
| 425 | mContext.getContentResolver(), element, value, userId); |
| 426 | loadAllowedComponentsFromSettings(); |
Julia Reynolds | ca8e535 | 2018-09-18 13:39:26 -0400 | [diff] [blame] | 427 | rebindServices(false, userId); |
Christopher Tate | 6597e34 | 2015-02-17 12:15:25 -0800 | [diff] [blame] | 428 | } |
| 429 | } |
| 430 | } |
| 431 | |
Jay Aliomer | 4204f25 | 2019-08-26 11:36:53 -0400 | [diff] [blame] | 432 | void writeDefaults(XmlSerializer out) throws IOException { |
| 433 | synchronized (mDefaultsLock) { |
| 434 | List<String> componentStrings = new ArrayList<>(mDefaultComponents.size()); |
| 435 | for (int i = 0; i < mDefaultComponents.size(); i++) { |
| 436 | componentStrings.add(mDefaultComponents.valueAt(i).flattenToString()); |
| 437 | } |
| 438 | String defaults = String.join(ENABLED_SERVICES_SEPARATOR, componentStrings); |
| 439 | out.attribute(null, ATT_DEFAULTS, defaults); |
| 440 | } |
| 441 | } |
| 442 | |
Annie Meng | 8b646fd | 2019-02-01 18:46:42 +0000 | [diff] [blame] | 443 | public void writeXml(XmlSerializer out, boolean forBackup, int userId) throws IOException { |
Julia Reynolds | d1bf5f0 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 444 | out.startTag(null, getConfig().xmlTag); |
Julia Reynolds | b852e56 | 2017-06-06 16:14:18 -0400 | [diff] [blame] | 445 | |
Julia Reynolds | 7380d87 | 2018-01-12 10:28:26 -0500 | [diff] [blame] | 446 | out.attribute(null, ATT_VERSION, String.valueOf(DB_VERSION)); |
| 447 | |
Jay Aliomer | 4204f25 | 2019-08-26 11:36:53 -0400 | [diff] [blame] | 448 | writeDefaults(out); |
| 449 | |
Julia Reynolds | b852e56 | 2017-06-06 16:14:18 -0400 | [diff] [blame] | 450 | if (forBackup) { |
Annie Meng | 8b646fd | 2019-02-01 18:46:42 +0000 | [diff] [blame] | 451 | trimApprovedListsAccordingToInstalledServices(userId); |
Julia Reynolds | b852e56 | 2017-06-06 16:14:18 -0400 | [diff] [blame] | 452 | } |
| 453 | |
Julia Reynolds | b776e39 | 2019-07-25 12:36:14 -0400 | [diff] [blame] | 454 | synchronized (mApproved) { |
| 455 | final int N = mApproved.size(); |
| 456 | for (int i = 0; i < N; i++) { |
| 457 | final int approvedUserId = mApproved.keyAt(i); |
| 458 | if (forBackup && approvedUserId != userId) { |
| 459 | continue; |
| 460 | } |
| 461 | final ArrayMap<Boolean, ArraySet<String>> approvedByType = mApproved.valueAt(i); |
| 462 | if (approvedByType != null) { |
| 463 | final int M = approvedByType.size(); |
| 464 | for (int j = 0; j < M; j++) { |
| 465 | final boolean isPrimary = approvedByType.keyAt(j); |
| 466 | final Set<String> approved = approvedByType.valueAt(j); |
| 467 | if (approved != null) { |
| 468 | String allowedItems = String.join(ENABLED_SERVICES_SEPARATOR, approved); |
| 469 | out.startTag(null, TAG_MANAGED_SERVICES); |
| 470 | out.attribute(null, ATT_APPROVED_LIST, allowedItems); |
| 471 | out.attribute(null, ATT_USER_ID, Integer.toString(approvedUserId)); |
| 472 | out.attribute(null, ATT_IS_PRIMARY, Boolean.toString(isPrimary)); |
| 473 | writeExtraAttributes(out, approvedUserId); |
| 474 | out.endTag(null, TAG_MANAGED_SERVICES); |
Julia Reynolds | b852e56 | 2017-06-06 16:14:18 -0400 | [diff] [blame] | 475 | |
Julia Reynolds | b776e39 | 2019-07-25 12:36:14 -0400 | [diff] [blame] | 476 | if (!forBackup && isPrimary) { |
| 477 | // Also write values to settings, for observers who haven't migrated yet |
| 478 | Settings.Secure.putStringForUser(mContext.getContentResolver(), |
| 479 | getConfig().secureSettingName, allowedItems, |
| 480 | approvedUserId); |
| 481 | } |
| 482 | |
Julia Reynolds | b852e56 | 2017-06-06 16:14:18 -0400 | [diff] [blame] | 483 | } |
Julia Reynolds | b852e56 | 2017-06-06 16:14:18 -0400 | [diff] [blame] | 484 | } |
| 485 | } |
| 486 | } |
| 487 | } |
Julia Reynolds | ad6dd35 | 2019-03-07 16:46:22 -0500 | [diff] [blame] | 488 | |
| 489 | writeExtraXmlTags(out); |
| 490 | |
Julia Reynolds | d1bf5f0 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 491 | out.endTag(null, getConfig().xmlTag); |
Julia Reynolds | b852e56 | 2017-06-06 16:14:18 -0400 | [diff] [blame] | 492 | } |
| 493 | |
Tony Mak | 9a3c1f1 | 2019-03-04 16:04:42 +0000 | [diff] [blame] | 494 | /** |
| 495 | * Writes extra xml attributes to {@link #TAG_MANAGED_SERVICES} tag. |
| 496 | */ |
| 497 | protected void writeExtraAttributes(XmlSerializer out, int userId) throws IOException {} |
| 498 | |
Julia Reynolds | ad6dd35 | 2019-03-07 16:46:22 -0500 | [diff] [blame] | 499 | /** |
| 500 | * Writes extra xml tags within the parent tag specified in {@link Config#xmlTag}. |
| 501 | */ |
| 502 | protected void writeExtraXmlTags(XmlSerializer out) throws IOException {} |
| 503 | |
| 504 | /** |
| 505 | * This is called to process tags other than {@link #TAG_MANAGED_SERVICES}. |
| 506 | */ |
| 507 | protected void readExtraTag(String tag, XmlPullParser parser) throws IOException {} |
| 508 | |
Julia Reynolds | d1bf5f0 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 509 | protected void migrateToXml() { |
| 510 | loadAllowedComponentsFromSettings(); |
| 511 | } |
| 512 | |
Jay Aliomer | 4204f25 | 2019-08-26 11:36:53 -0400 | [diff] [blame] | 513 | void readDefaults(XmlPullParser parser) { |
| 514 | String defaultComponents = XmlUtils.readStringAttribute(parser, ATT_DEFAULTS); |
Jay Aliomer | 76e1f272 | 2020-03-05 12:36:38 -0500 | [diff] [blame] | 515 | |
| 516 | if (!TextUtils.isEmpty(defaultComponents)) { |
| 517 | String[] components = defaultComponents.split(ENABLED_SERVICES_SEPARATOR); |
| 518 | synchronized (mDefaultsLock) { |
| 519 | for (int i = 0; i < components.length; i++) { |
| 520 | if (!TextUtils.isEmpty(components[i])) { |
| 521 | ComponentName cn = ComponentName.unflattenFromString(components[i]); |
| 522 | if (cn != null) { |
| 523 | mDefaultPackages.add(cn.getPackageName()); |
| 524 | mDefaultComponents.add(cn); |
| 525 | } else { |
| 526 | mDefaultPackages.add(components[i]); |
| 527 | } |
Jay Aliomer | 4204f25 | 2019-08-26 11:36:53 -0400 | [diff] [blame] | 528 | } |
| 529 | } |
| 530 | } |
| 531 | } |
| 532 | } |
| 533 | |
Annie Meng | 8b646fd | 2019-02-01 18:46:42 +0000 | [diff] [blame] | 534 | public void readXml( |
| 535 | XmlPullParser parser, |
Julia Reynolds | d0ceefa | 2019-03-03 16:10:52 -0500 | [diff] [blame] | 536 | TriPredicate<String, Integer, String> allowedManagedServicePackages, |
Annie Meng | 8b646fd | 2019-02-01 18:46:42 +0000 | [diff] [blame] | 537 | boolean forRestore, |
| 538 | int userId) |
Julia Reynolds | b852e56 | 2017-06-06 16:14:18 -0400 | [diff] [blame] | 539 | throws XmlPullParserException, IOException { |
Julia Reynolds | 7380d87 | 2018-01-12 10:28:26 -0500 | [diff] [blame] | 540 | // read grants |
Julia Reynolds | d1bf5f0 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 541 | int type; |
Jay Aliomer | 76e1f272 | 2020-03-05 12:36:38 -0500 | [diff] [blame] | 542 | String version = ""; |
Jay Aliomer | 4204f25 | 2019-08-26 11:36:53 -0400 | [diff] [blame] | 543 | readDefaults(parser); |
Julia Reynolds | d1bf5f0 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 544 | while ((type = parser.next()) != XmlPullParser.END_DOCUMENT) { |
| 545 | String tag = parser.getName(); |
Jay Aliomer | 76e1f272 | 2020-03-05 12:36:38 -0500 | [diff] [blame] | 546 | version = XmlUtils.readStringAttribute(parser, ATT_VERSION); |
Julia Reynolds | d1bf5f0 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 547 | if (type == XmlPullParser.END_TAG |
| 548 | && getConfig().xmlTag.equals(tag)) { |
| 549 | break; |
| 550 | } |
| 551 | if (type == XmlPullParser.START_TAG) { |
| 552 | if (TAG_MANAGED_SERVICES.equals(tag)) { |
Julia Reynolds | e5ce071bf | 2017-09-15 14:26:32 -0400 | [diff] [blame] | 553 | Slog.i(TAG, "Read " + mConfig.caption + " permissions from xml"); |
Julia Reynolds | 7380d87 | 2018-01-12 10:28:26 -0500 | [diff] [blame] | 554 | |
Julia Reynolds | d1bf5f0 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 555 | final String approved = XmlUtils.readStringAttribute(parser, ATT_APPROVED_LIST); |
Annie Meng | 8b646fd | 2019-02-01 18:46:42 +0000 | [diff] [blame] | 556 | // Ignore parser's user id for restore. |
| 557 | final int resolvedUserId = forRestore |
| 558 | ? userId : XmlUtils.readIntAttribute(parser, ATT_USER_ID, 0); |
Julia Reynolds | d1bf5f0 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 559 | final boolean isPrimary = |
| 560 | XmlUtils.readBooleanAttribute(parser, ATT_IS_PRIMARY, true); |
Tony Mak | 9a3c1f1 | 2019-03-04 16:04:42 +0000 | [diff] [blame] | 561 | readExtraAttributes(tag, parser, resolvedUserId); |
Julia Reynolds | d0ceefa | 2019-03-03 16:10:52 -0500 | [diff] [blame] | 562 | if (allowedManagedServicePackages == null || allowedManagedServicePackages.test( |
| 563 | getPackageName(approved), resolvedUserId, getRequiredPermission())) { |
Annie Meng | 8b646fd | 2019-02-01 18:46:42 +0000 | [diff] [blame] | 564 | if (mUm.getUserInfo(resolvedUserId) != null) { |
| 565 | addApprovedList(approved, resolvedUserId, isPrimary); |
Kristian Monsen | 05f3479 | 2018-04-09 10:27:16 +0200 | [diff] [blame] | 566 | } |
| 567 | mUseXml = true; |
Julia Reynolds | 4552300 | 2017-09-12 09:43:57 -0400 | [diff] [blame] | 568 | } |
Julia Reynolds | ad6dd35 | 2019-03-07 16:46:22 -0500 | [diff] [blame] | 569 | } else { |
| 570 | readExtraTag(tag, parser); |
Julia Reynolds | b852e56 | 2017-06-06 16:14:18 -0400 | [diff] [blame] | 571 | } |
| 572 | } |
Julia Reynolds | b852e56 | 2017-06-06 16:14:18 -0400 | [diff] [blame] | 573 | } |
Jay Aliomer | 76e1f272 | 2020-03-05 12:36:38 -0500 | [diff] [blame] | 574 | boolean isVersionOne = TextUtils.isEmpty(version) || DB_VERSION_1.equals(version); |
| 575 | if (isVersionOne) { |
| 576 | upgradeToVersionTwo(); |
| 577 | } |
Julia Reynolds | ca8e535 | 2018-09-18 13:39:26 -0400 | [diff] [blame] | 578 | rebindServices(false, USER_ALL); |
Julia Reynolds | b852e56 | 2017-06-06 16:14:18 -0400 | [diff] [blame] | 579 | } |
| 580 | |
Jay Aliomer | 76e1f272 | 2020-03-05 12:36:38 -0500 | [diff] [blame] | 581 | private void upgradeToVersionTwo() { |
| 582 | // check if any defaults are loaded |
| 583 | int defaultsSize = mDefaultComponents.size() + mDefaultPackages.size(); |
| 584 | if (defaultsSize == 0) { |
| 585 | // load defaults from current allowed |
| 586 | if (this.mApprovalLevel == APPROVAL_BY_COMPONENT) { |
| 587 | List<ComponentName> approvedComponents = getAllowedComponents(USER_SYSTEM); |
| 588 | for (int i = 0; i < approvedComponents.size(); i++) { |
| 589 | addDefaultComponentOrPackage(approvedComponents.get(i).flattenToString()); |
| 590 | } |
| 591 | } |
| 592 | if (this.mApprovalLevel == APPROVAL_BY_PACKAGE) { |
| 593 | List<String> approvedPkgs = getAllowedPackages(USER_SYSTEM); |
| 594 | for (int i = 0; i < approvedPkgs.size(); i++) { |
| 595 | addDefaultComponentOrPackage(approvedPkgs.get(i)); |
| 596 | } |
| 597 | } |
| 598 | } |
| 599 | // if no defaults are loaded, then load from config |
| 600 | defaultsSize = mDefaultComponents.size() + mDefaultPackages.size(); |
| 601 | if (defaultsSize == 0) { |
| 602 | loadDefaultsFromConfig(); |
| 603 | } |
| 604 | } |
| 605 | |
Tony Mak | 9a3c1f1 | 2019-03-04 16:04:42 +0000 | [diff] [blame] | 606 | /** |
| 607 | * Read extra attributes in the {@link #TAG_MANAGED_SERVICES} tag. |
| 608 | */ |
| 609 | protected void readExtraAttributes(String tag, XmlPullParser parser, int userId) |
| 610 | throws IOException {} |
| 611 | |
Julia Reynolds | d0ceefa | 2019-03-03 16:10:52 -0500 | [diff] [blame] | 612 | protected abstract String getRequiredPermission(); |
| 613 | |
Julia Reynolds | b852e56 | 2017-06-06 16:14:18 -0400 | [diff] [blame] | 614 | private void loadAllowedComponentsFromSettings() { |
Julia Reynolds | e0d711f | 2017-09-01 08:50:47 -0400 | [diff] [blame] | 615 | for (UserInfo user : mUm.getUsers()) { |
Julia Reynolds | b852e56 | 2017-06-06 16:14:18 -0400 | [diff] [blame] | 616 | final ContentResolver cr = mContext.getContentResolver(); |
| 617 | addApprovedList(Settings.Secure.getStringForUser( |
| 618 | cr, |
| 619 | getConfig().secureSettingName, |
| 620 | user.id), user.id, true); |
| 621 | if (!TextUtils.isEmpty(getConfig().secondarySettingName)) { |
| 622 | addApprovedList(Settings.Secure.getStringForUser( |
| 623 | cr, |
| 624 | getConfig().secondarySettingName, |
| 625 | user.id), user.id, false); |
| 626 | } |
| 627 | } |
| 628 | Slog.d(TAG, "Done loading approved values from settings"); |
| 629 | } |
| 630 | |
Julia Reynolds | 7380d87 | 2018-01-12 10:28:26 -0500 | [diff] [blame] | 631 | protected void addApprovedList(String approved, int userId, boolean isPrimary) { |
Julia Reynolds | b852e56 | 2017-06-06 16:14:18 -0400 | [diff] [blame] | 632 | if (TextUtils.isEmpty(approved)) { |
| 633 | approved = ""; |
| 634 | } |
Julia Reynolds | b776e39 | 2019-07-25 12:36:14 -0400 | [diff] [blame] | 635 | synchronized (mApproved) { |
| 636 | ArrayMap<Boolean, ArraySet<String>> approvedByType = mApproved.get(userId); |
| 637 | if (approvedByType == null) { |
| 638 | approvedByType = new ArrayMap<>(); |
| 639 | mApproved.put(userId, approvedByType); |
| 640 | } |
Artem Iglikov | bec9617 | 2018-04-24 12:46:44 +0100 | [diff] [blame] | 641 | |
Julia Reynolds | b776e39 | 2019-07-25 12:36:14 -0400 | [diff] [blame] | 642 | ArraySet<String> approvedList = approvedByType.get(isPrimary); |
| 643 | if (approvedList == null) { |
| 644 | approvedList = new ArraySet<>(); |
| 645 | approvedByType.put(isPrimary, approvedList); |
| 646 | } |
Artem Iglikov | bec9617 | 2018-04-24 12:46:44 +0100 | [diff] [blame] | 647 | |
Julia Reynolds | b776e39 | 2019-07-25 12:36:14 -0400 | [diff] [blame] | 648 | String[] approvedArray = approved.split(ENABLED_SERVICES_SEPARATOR); |
| 649 | for (String pkgOrComponent : approvedArray) { |
| 650 | String approvedItem = getApprovedValue(pkgOrComponent); |
| 651 | if (approvedItem != null) { |
| 652 | approvedList.add(approvedItem); |
| 653 | } |
Julia Reynolds | b852e56 | 2017-06-06 16:14:18 -0400 | [diff] [blame] | 654 | } |
| 655 | } |
Julia Reynolds | b852e56 | 2017-06-06 16:14:18 -0400 | [diff] [blame] | 656 | } |
| 657 | |
| 658 | protected boolean isComponentEnabledForPackage(String pkg) { |
John Spurlock | 8077493 | 2015-05-07 17:38:50 -0400 | [diff] [blame] | 659 | return mEnabledServicesPackageNames.contains(pkg); |
| 660 | } |
| 661 | |
Julia Reynolds | b852e56 | 2017-06-06 16:14:18 -0400 | [diff] [blame] | 662 | protected void setPackageOrComponentEnabled(String pkgOrComponent, int userId, |
| 663 | boolean isPrimary, boolean enabled) { |
Julia Reynolds | e5ce071bf | 2017-09-15 14:26:32 -0400 | [diff] [blame] | 664 | Slog.i(TAG, |
| 665 | (enabled ? " Allowing " : "Disallowing ") + mConfig.caption + " " + pkgOrComponent); |
Julia Reynolds | b776e39 | 2019-07-25 12:36:14 -0400 | [diff] [blame] | 666 | synchronized (mApproved) { |
| 667 | ArrayMap<Boolean, ArraySet<String>> allowedByType = mApproved.get(userId); |
| 668 | if (allowedByType == null) { |
| 669 | allowedByType = new ArrayMap<>(); |
| 670 | mApproved.put(userId, allowedByType); |
| 671 | } |
| 672 | ArraySet<String> approved = allowedByType.get(isPrimary); |
| 673 | if (approved == null) { |
| 674 | approved = new ArraySet<>(); |
| 675 | allowedByType.put(isPrimary, approved); |
| 676 | } |
| 677 | String approvedItem = getApprovedValue(pkgOrComponent); |
Denis Kuznetsov | 76c0268 | 2015-09-17 19:57:00 +0200 | [diff] [blame] | 678 | |
Julia Reynolds | b776e39 | 2019-07-25 12:36:14 -0400 | [diff] [blame] | 679 | if (approvedItem != null) { |
| 680 | if (enabled) { |
| 681 | approved.add(approvedItem); |
| 682 | } else { |
| 683 | approved.remove(approvedItem); |
| 684 | } |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 685 | } |
| 686 | } |
| 687 | |
Julia Reynolds | ca8e535 | 2018-09-18 13:39:26 -0400 | [diff] [blame] | 688 | rebindServices(false, userId); |
Julia Reynolds | b852e56 | 2017-06-06 16:14:18 -0400 | [diff] [blame] | 689 | } |
| 690 | |
| 691 | private String getApprovedValue(String pkgOrComponent) { |
| 692 | if (mApprovalLevel == APPROVAL_BY_COMPONENT) { |
| 693 | if(ComponentName.unflattenFromString(pkgOrComponent) != null) { |
| 694 | return pkgOrComponent; |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 695 | } |
Julia Reynolds | b852e56 | 2017-06-06 16:14:18 -0400 | [diff] [blame] | 696 | return null; |
| 697 | } else { |
| 698 | return getPackageName(pkgOrComponent); |
| 699 | } |
| 700 | } |
| 701 | |
Julia Reynolds | fa206a4 | 2017-08-14 13:22:23 -0400 | [diff] [blame] | 702 | protected String getApproved(int userId, boolean primary) { |
Julia Reynolds | b776e39 | 2019-07-25 12:36:14 -0400 | [diff] [blame] | 703 | synchronized (mApproved) { |
| 704 | final ArrayMap<Boolean, ArraySet<String>> allowedByType = |
| 705 | mApproved.getOrDefault(userId, new ArrayMap<>()); |
| 706 | ArraySet<String> approved = allowedByType.getOrDefault(primary, new ArraySet<>()); |
| 707 | return String.join(ENABLED_SERVICES_SEPARATOR, approved); |
| 708 | } |
Julia Reynolds | fa206a4 | 2017-08-14 13:22:23 -0400 | [diff] [blame] | 709 | } |
| 710 | |
Julia Reynolds | b852e56 | 2017-06-06 16:14:18 -0400 | [diff] [blame] | 711 | protected List<ComponentName> getAllowedComponents(int userId) { |
| 712 | final List<ComponentName> allowedComponents = new ArrayList<>(); |
Julia Reynolds | b776e39 | 2019-07-25 12:36:14 -0400 | [diff] [blame] | 713 | synchronized (mApproved) { |
| 714 | final ArrayMap<Boolean, ArraySet<String>> allowedByType = |
| 715 | mApproved.getOrDefault(userId, new ArrayMap<>()); |
| 716 | for (int i = 0; i < allowedByType.size(); i++) { |
| 717 | final ArraySet<String> allowed = allowedByType.valueAt(i); |
| 718 | for (int j = 0; j < allowed.size(); j++) { |
| 719 | ComponentName cn = ComponentName.unflattenFromString(allowed.valueAt(j)); |
| 720 | if (cn != null) { |
| 721 | allowedComponents.add(cn); |
| 722 | } |
Julia Reynolds | 2f49696 | 2018-04-16 13:17:01 -0400 | [diff] [blame] | 723 | } |
| 724 | } |
Julia Reynolds | b852e56 | 2017-06-06 16:14:18 -0400 | [diff] [blame] | 725 | } |
| 726 | return allowedComponents; |
| 727 | } |
| 728 | |
| 729 | protected List<String> getAllowedPackages(int userId) { |
| 730 | final List<String> allowedPackages = new ArrayList<>(); |
Julia Reynolds | b776e39 | 2019-07-25 12:36:14 -0400 | [diff] [blame] | 731 | synchronized (mApproved) { |
| 732 | final ArrayMap<Boolean, ArraySet<String>> allowedByType = |
| 733 | mApproved.getOrDefault(userId, new ArrayMap<>()); |
| 734 | for (int i = 0; i < allowedByType.size(); i++) { |
| 735 | final ArraySet<String> allowed = allowedByType.valueAt(i); |
| 736 | for (int j = 0; j < allowed.size(); j++) { |
| 737 | String pkgName = getPackageName(allowed.valueAt(j)); |
| 738 | if (!TextUtils.isEmpty(pkgName)) { |
| 739 | allowedPackages.add(pkgName); |
| 740 | } |
Julia Reynolds | 2f49696 | 2018-04-16 13:17:01 -0400 | [diff] [blame] | 741 | } |
| 742 | } |
Julia Reynolds | b852e56 | 2017-06-06 16:14:18 -0400 | [diff] [blame] | 743 | } |
| 744 | return allowedPackages; |
| 745 | } |
| 746 | |
| 747 | protected boolean isPackageOrComponentAllowed(String pkgOrComponent, int userId) { |
Julia Reynolds | b776e39 | 2019-07-25 12:36:14 -0400 | [diff] [blame] | 748 | synchronized (mApproved) { |
| 749 | ArrayMap<Boolean, ArraySet<String>> allowedByType = |
| 750 | mApproved.getOrDefault(userId, new ArrayMap<>()); |
| 751 | for (int i = 0; i < allowedByType.size(); i++) { |
| 752 | ArraySet<String> allowed = allowedByType.valueAt(i); |
| 753 | if (allowed.contains(pkgOrComponent)) { |
| 754 | return true; |
| 755 | } |
Julia Reynolds | b852e56 | 2017-06-06 16:14:18 -0400 | [diff] [blame] | 756 | } |
| 757 | } |
| 758 | return false; |
| 759 | } |
| 760 | |
Julia Reynolds | 1459074 | 2018-07-30 09:25:35 -0400 | [diff] [blame] | 761 | protected boolean isPackageAllowed(String pkg, int userId) { |
| 762 | if (pkg == null) { |
| 763 | return false; |
| 764 | } |
Julia Reynolds | b776e39 | 2019-07-25 12:36:14 -0400 | [diff] [blame] | 765 | synchronized (mApproved) { |
| 766 | ArrayMap<Boolean, ArraySet<String>> allowedByType = |
| 767 | mApproved.getOrDefault(userId, new ArrayMap<>()); |
| 768 | for (int i = 0; i < allowedByType.size(); i++) { |
| 769 | ArraySet<String> allowed = allowedByType.valueAt(i); |
| 770 | for (String allowedEntry : allowed) { |
| 771 | ComponentName component = ComponentName.unflattenFromString(allowedEntry); |
| 772 | if (component != null) { |
| 773 | if (pkg.equals(component.getPackageName())) { |
| 774 | return true; |
| 775 | } |
| 776 | } else { |
| 777 | if (pkg.equals(allowedEntry)) { |
| 778 | return true; |
| 779 | } |
Julia Reynolds | 1459074 | 2018-07-30 09:25:35 -0400 | [diff] [blame] | 780 | } |
| 781 | } |
| 782 | } |
| 783 | } |
| 784 | return false; |
| 785 | } |
| 786 | |
Julia Reynolds | b852e56 | 2017-06-06 16:14:18 -0400 | [diff] [blame] | 787 | public void onPackagesChanged(boolean removingPackage, String[] pkgList, int[] uidList) { |
| 788 | if (DEBUG) Slog.d(TAG, "onPackagesChanged removingPackage=" + removingPackage |
| 789 | + " pkgList=" + (pkgList == null ? null : Arrays.asList(pkgList)) |
| 790 | + " mEnabledServicesPackageNames=" + mEnabledServicesPackageNames); |
| 791 | |
| 792 | if (pkgList != null && (pkgList.length > 0)) { |
| 793 | boolean anyServicesInvolved = false; |
| 794 | // Remove notification settings for uninstalled package |
Beverly | 42d0190 | 2019-01-03 12:47:57 -0500 | [diff] [blame] | 795 | if (removingPackage && uidList != null) { |
Julia Reynolds | b852e56 | 2017-06-06 16:14:18 -0400 | [diff] [blame] | 796 | int size = Math.min(pkgList.length, uidList.length); |
| 797 | for (int i = 0; i < size; i++) { |
| 798 | final String pkg = pkgList[i]; |
| 799 | final int userId = UserHandle.getUserId(uidList[i]); |
| 800 | anyServicesInvolved = removeUninstalledItemsFromApprovedLists(userId, pkg); |
| 801 | } |
| 802 | } |
| 803 | for (String pkgName : pkgList) { |
| 804 | if (mEnabledServicesPackageNames.contains(pkgName)) { |
| 805 | anyServicesInvolved = true; |
| 806 | } |
Beverly | 42d0190 | 2019-01-03 12:47:57 -0500 | [diff] [blame] | 807 | if (uidList != null && uidList.length > 0) { |
| 808 | for (int uid : uidList) { |
| 809 | if (isPackageAllowed(pkgName, UserHandle.getUserId(uid))) { |
| 810 | anyServicesInvolved = true; |
| 811 | } |
Julia Reynolds | 1459074 | 2018-07-30 09:25:35 -0400 | [diff] [blame] | 812 | } |
| 813 | } |
Julia Reynolds | b852e56 | 2017-06-06 16:14:18 -0400 | [diff] [blame] | 814 | } |
| 815 | |
| 816 | if (anyServicesInvolved) { |
| 817 | // make sure we're still bound to any of our services who may have just upgraded |
Julia Reynolds | ca8e535 | 2018-09-18 13:39:26 -0400 | [diff] [blame] | 818 | rebindServices(false, USER_ALL); |
Julia Reynolds | b852e56 | 2017-06-06 16:14:18 -0400 | [diff] [blame] | 819 | } |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 820 | } |
| 821 | } |
| 822 | |
Julia Reynolds | 5aa13a4 | 2017-08-24 09:10:23 -0400 | [diff] [blame] | 823 | public void onUserRemoved(int user) { |
Julia Reynolds | e5ce071bf | 2017-09-15 14:26:32 -0400 | [diff] [blame] | 824 | Slog.i(TAG, "Removing approved services for removed user " + user); |
Julia Reynolds | b776e39 | 2019-07-25 12:36:14 -0400 | [diff] [blame] | 825 | synchronized (mApproved) { |
| 826 | mApproved.remove(user); |
| 827 | } |
Julia Reynolds | ca8e535 | 2018-09-18 13:39:26 -0400 | [diff] [blame] | 828 | rebindServices(true, user); |
Julia Reynolds | 5aa13a4 | 2017-08-24 09:10:23 -0400 | [diff] [blame] | 829 | } |
| 830 | |
John Spurlock | 1b8b22b | 2015-05-20 09:47:13 -0400 | [diff] [blame] | 831 | public void onUserSwitched(int user) { |
| 832 | if (DEBUG) Slog.d(TAG, "onUserSwitched u=" + user); |
Julia Reynolds | ca8e535 | 2018-09-18 13:39:26 -0400 | [diff] [blame] | 833 | rebindServices(true, user); |
Christoph Studer | b53dfd4 | 2014-09-12 14:45:59 +0200 | [diff] [blame] | 834 | } |
| 835 | |
Julia Reynolds | a3dcaff | 2016-02-03 15:04:05 -0500 | [diff] [blame] | 836 | public void onUserUnlocked(int user) { |
| 837 | if (DEBUG) Slog.d(TAG, "onUserUnlocked u=" + user); |
Julia Reynolds | ca8e535 | 2018-09-18 13:39:26 -0400 | [diff] [blame] | 838 | rebindServices(false, user); |
Julia Reynolds | a3dcaff | 2016-02-03 15:04:05 -0500 | [diff] [blame] | 839 | } |
| 840 | |
Julia Reynolds | b852e56 | 2017-06-06 16:14:18 -0400 | [diff] [blame] | 841 | private ManagedServiceInfo getServiceFromTokenLocked(IInterface service) { |
Chris Wren | ab41eec | 2016-01-04 18:01:27 -0500 | [diff] [blame] | 842 | if (service == null) { |
| 843 | return null; |
| 844 | } |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 845 | final IBinder token = service.asBinder(); |
| 846 | final int N = mServices.size(); |
Denis Kuznetsov | 76c0268 | 2015-09-17 19:57:00 +0200 | [diff] [blame] | 847 | for (int i = 0; i < N; i++) { |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 848 | final ManagedServiceInfo info = mServices.get(i); |
| 849 | if (info.service.asBinder() == token) return info; |
| 850 | } |
Chris Wren | ab41eec | 2016-01-04 18:01:27 -0500 | [diff] [blame] | 851 | return null; |
| 852 | } |
| 853 | |
Julia Reynolds | 503ed94 | 2017-10-04 16:04:56 -0400 | [diff] [blame] | 854 | protected boolean isServiceTokenValidLocked(IInterface service) { |
| 855 | if (service == null) { |
| 856 | return false; |
| 857 | } |
| 858 | ManagedServiceInfo info = getServiceFromTokenLocked(service); |
| 859 | if (info != null) { |
| 860 | return true; |
| 861 | } |
| 862 | return false; |
| 863 | } |
| 864 | |
Julia Reynolds | b852e56 | 2017-06-06 16:14:18 -0400 | [diff] [blame] | 865 | protected ManagedServiceInfo checkServiceTokenLocked(IInterface service) { |
Chris Wren | ab41eec | 2016-01-04 18:01:27 -0500 | [diff] [blame] | 866 | checkNotNull(service); |
| 867 | ManagedServiceInfo info = getServiceFromTokenLocked(service); |
| 868 | if (info != null) { |
| 869 | return info; |
| 870 | } |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 871 | throw new SecurityException("Disallowed call from unknown " + getCaption() + ": " |
Julia Reynolds | cb507b8 | 2017-09-07 13:53:40 -0400 | [diff] [blame] | 872 | + service + " " + service.getClass()); |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 873 | } |
| 874 | |
Julia Reynolds | 70aaea7 | 2018-07-13 13:38:34 -0400 | [diff] [blame] | 875 | public boolean isSameUser(IInterface service, int userId) { |
| 876 | checkNotNull(service); |
Julia Reynolds | 3ff1fd9 | 2018-08-29 09:57:11 -0400 | [diff] [blame] | 877 | synchronized (mMutex) { |
| 878 | ManagedServiceInfo info = getServiceFromTokenLocked(service); |
| 879 | if (info != null) { |
| 880 | return info.isSameUser(userId); |
| 881 | } |
| 882 | return false; |
Julia Reynolds | 70aaea7 | 2018-07-13 13:38:34 -0400 | [diff] [blame] | 883 | } |
Julia Reynolds | 70aaea7 | 2018-07-13 13:38:34 -0400 | [diff] [blame] | 884 | } |
| 885 | |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 886 | public void unregisterService(IInterface service, int userid) { |
| 887 | checkNotNull(service); |
| 888 | // no need to check permissions; if your service binder is in the list, |
| 889 | // that's proof that you had permission to add it in the first place |
| 890 | unregisterServiceImpl(service, userid); |
| 891 | } |
| 892 | |
| 893 | public void registerService(IInterface service, ComponentName component, int userid) { |
| 894 | checkNotNull(service); |
Christoph Studer | 3e144d3 | 2014-05-22 16:48:40 +0200 | [diff] [blame] | 895 | ManagedServiceInfo info = registerServiceImpl(service, component, userid); |
| 896 | if (info != null) { |
| 897 | onServiceAdded(info); |
| 898 | } |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 899 | } |
| 900 | |
Chris Wren | 51017d0 | 2015-12-15 15:34:46 -0500 | [diff] [blame] | 901 | /** |
| 902 | * Add a service to our callbacks. The lifecycle of this service is managed externally, |
Julia Reynolds | b852e56 | 2017-06-06 16:14:18 -0400 | [diff] [blame] | 903 | * but unlike a system service, it should not be considered privileged. |
Chris Wren | 51017d0 | 2015-12-15 15:34:46 -0500 | [diff] [blame] | 904 | * */ |
Julia Reynolds | b852e56 | 2017-06-06 16:14:18 -0400 | [diff] [blame] | 905 | protected void registerGuestService(ManagedServiceInfo guest) { |
Chris Wren | 51017d0 | 2015-12-15 15:34:46 -0500 | [diff] [blame] | 906 | checkNotNull(guest.service); |
Ruben Brunk | e24b9a6 | 2016-02-16 21:38:24 -0800 | [diff] [blame] | 907 | if (!checkType(guest.service)) { |
| 908 | throw new IllegalArgumentException(); |
| 909 | } |
Chris Wren | 51017d0 | 2015-12-15 15:34:46 -0500 | [diff] [blame] | 910 | if (registerServiceImpl(guest) != null) { |
| 911 | onServiceAdded(guest); |
Chris Wren | ab41eec | 2016-01-04 18:01:27 -0500 | [diff] [blame] | 912 | } |
| 913 | } |
| 914 | |
Julia Reynolds | b852e56 | 2017-06-06 16:14:18 -0400 | [diff] [blame] | 915 | protected void setComponentState(ComponentName component, boolean enabled) { |
Chris Wren | ab41eec | 2016-01-04 18:01:27 -0500 | [diff] [blame] | 916 | boolean previous = !mSnoozingForCurrentProfiles.contains(component); |
| 917 | if (previous == enabled) { |
| 918 | return; |
| 919 | } |
| 920 | |
| 921 | if (enabled) { |
| 922 | mSnoozingForCurrentProfiles.remove(component); |
| 923 | } else { |
| 924 | mSnoozingForCurrentProfiles.add(component); |
| 925 | } |
| 926 | |
| 927 | // State changed |
Julia Reynolds | e5ce071bf | 2017-09-15 14:26:32 -0400 | [diff] [blame] | 928 | Slog.d(TAG, ((enabled) ? "Enabling " : "Disabling ") + "component " + |
| 929 | component.flattenToShortString()); |
Chris Wren | ab41eec | 2016-01-04 18:01:27 -0500 | [diff] [blame] | 930 | |
Chris Wren | 0efdb88 | 2016-03-01 17:17:47 -0500 | [diff] [blame] | 931 | synchronized (mMutex) { |
Julia Reynolds | ca8e535 | 2018-09-18 13:39:26 -0400 | [diff] [blame] | 932 | final IntArray userIds = mUserProfiles.getCurrentProfileIds(); |
Chris Wren | 0efdb88 | 2016-03-01 17:17:47 -0500 | [diff] [blame] | 933 | |
Julia Reynolds | ca8e535 | 2018-09-18 13:39:26 -0400 | [diff] [blame] | 934 | for (int i = 0; i < userIds.size(); i++) { |
| 935 | final int userId = userIds.get(i); |
Chris Wren | 0efdb88 | 2016-03-01 17:17:47 -0500 | [diff] [blame] | 936 | if (enabled) { |
Beverly | 363da78 | 2018-10-09 16:05:15 -0400 | [diff] [blame] | 937 | if (isPackageOrComponentAllowed(component.flattenToString(), userId) |
Beverly | 2d783fc | 2018-09-07 13:38:21 -0400 | [diff] [blame] | 938 | || isPackageOrComponentAllowed(component.getPackageName(), userId)) { |
Julia Reynolds | 00fa840 | 2018-07-25 08:57:58 -0400 | [diff] [blame] | 939 | registerServiceLocked(component, userId); |
| 940 | } else { |
| 941 | Slog.d(TAG, component + " no longer has permission to be bound"); |
| 942 | } |
Chris Wren | 0efdb88 | 2016-03-01 17:17:47 -0500 | [diff] [blame] | 943 | } else { |
| 944 | unregisterServiceLocked(component, userId); |
| 945 | } |
Chris Wren | ab41eec | 2016-01-04 18:01:27 -0500 | [diff] [blame] | 946 | } |
Chris Wren | 51017d0 | 2015-12-15 15:34:46 -0500 | [diff] [blame] | 947 | } |
| 948 | } |
| 949 | |
Julia Reynolds | b852e56 | 2017-06-06 16:14:18 -0400 | [diff] [blame] | 950 | private @NonNull ArraySet<ComponentName> loadComponentNamesFromValues( |
| 951 | ArraySet<String> approved, int userId) { |
| 952 | if (approved == null || approved.size() == 0) |
Julia Reynolds | 6e839b0 | 2016-04-13 10:01:17 -0400 | [diff] [blame] | 953 | return new ArraySet<>(); |
Julia Reynolds | b852e56 | 2017-06-06 16:14:18 -0400 | [diff] [blame] | 954 | ArraySet<ComponentName> result = new ArraySet<>(approved.size()); |
| 955 | for (int i = 0; i < approved.size(); i++) { |
| 956 | final String packageOrComponent = approved.valueAt(i); |
| 957 | if (!TextUtils.isEmpty(packageOrComponent)) { |
| 958 | ComponentName component = ComponentName.unflattenFromString(packageOrComponent); |
| 959 | if (component != null) { |
| 960 | result.add(component); |
| 961 | } else { |
| 962 | result.addAll(queryPackageForServices(packageOrComponent, userId)); |
| 963 | } |
Christopher Tate | 6597e34 | 2015-02-17 12:15:25 -0800 | [diff] [blame] | 964 | } |
| 965 | } |
Denis Kuznetsov | 76c0268 | 2015-09-17 19:57:00 +0200 | [diff] [blame] | 966 | return result; |
| 967 | } |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 968 | |
Julia Reynolds | c279b99 | 2015-10-30 08:23:51 -0400 | [diff] [blame] | 969 | protected Set<ComponentName> queryPackageForServices(String packageName, int userId) { |
Julia Reynolds | b852e56 | 2017-06-06 16:14:18 -0400 | [diff] [blame] | 970 | return queryPackageForServices(packageName, 0, userId); |
| 971 | } |
| 972 | |
Jay Aliomer | 4204f25 | 2019-08-26 11:36:53 -0400 | [diff] [blame] | 973 | protected ArraySet<ComponentName> queryPackageForServices(String packageName, int extraFlags, |
Julia Reynolds | b852e56 | 2017-06-06 16:14:18 -0400 | [diff] [blame] | 974 | int userId) { |
Jay Aliomer | 4204f25 | 2019-08-26 11:36:53 -0400 | [diff] [blame] | 975 | ArraySet<ComponentName> installed = new ArraySet<>(); |
Denis Kuznetsov | 76c0268 | 2015-09-17 19:57:00 +0200 | [diff] [blame] | 976 | final PackageManager pm = mContext.getPackageManager(); |
Julia Reynolds | c279b99 | 2015-10-30 08:23:51 -0400 | [diff] [blame] | 977 | Intent queryIntent = new Intent(mConfig.serviceInterface); |
| 978 | if (!TextUtils.isEmpty(packageName)) { |
| 979 | queryIntent.setPackage(packageName); |
| 980 | } |
Denis Kuznetsov | 76c0268 | 2015-09-17 19:57:00 +0200 | [diff] [blame] | 981 | List<ResolveInfo> installedServices = pm.queryIntentServicesAsUser( |
Julia Reynolds | c279b99 | 2015-10-30 08:23:51 -0400 | [diff] [blame] | 982 | queryIntent, |
Julia Reynolds | b852e56 | 2017-06-06 16:14:18 -0400 | [diff] [blame] | 983 | PackageManager.GET_SERVICES | PackageManager.GET_META_DATA | extraFlags, |
Denis Kuznetsov | 76c0268 | 2015-09-17 19:57:00 +0200 | [diff] [blame] | 984 | userId); |
| 985 | if (DEBUG) |
| 986 | Slog.v(TAG, mConfig.serviceInterface + " services: " + installedServices); |
Julia Reynolds | 50f0514 | 2015-10-30 17:03:59 -0400 | [diff] [blame] | 987 | if (installedServices != null) { |
| 988 | for (int i = 0, count = installedServices.size(); i < count; i++) { |
| 989 | ResolveInfo resolveInfo = installedServices.get(i); |
| 990 | ServiceInfo info = resolveInfo.serviceInfo; |
Denis Kuznetsov | 76c0268 | 2015-09-17 19:57:00 +0200 | [diff] [blame] | 991 | |
Julia Reynolds | 50f0514 | 2015-10-30 17:03:59 -0400 | [diff] [blame] | 992 | ComponentName component = new ComponentName(info.packageName, info.name); |
| 993 | if (!mConfig.bindPermission.equals(info.permission)) { |
| 994 | Slog.w(TAG, "Skipping " + getCaption() + " service " |
Ruben Brunk | dd18a0b | 2015-12-04 16:16:31 -0800 | [diff] [blame] | 995 | + info.packageName + "/" + info.name |
| 996 | + ": it does not require the permission " |
| 997 | + mConfig.bindPermission); |
Julia Reynolds | 50f0514 | 2015-10-30 17:03:59 -0400 | [diff] [blame] | 998 | continue; |
| 999 | } |
| 1000 | installed.add(component); |
Denis Kuznetsov | 76c0268 | 2015-09-17 19:57:00 +0200 | [diff] [blame] | 1001 | } |
Denis Kuznetsov | 76c0268 | 2015-09-17 19:57:00 +0200 | [diff] [blame] | 1002 | } |
Julia Reynolds | c279b99 | 2015-10-30 08:23:51 -0400 | [diff] [blame] | 1003 | return installed; |
| 1004 | } |
| 1005 | |
Julia Reynolds | 4c456dd | 2019-01-07 12:22:00 -0500 | [diff] [blame] | 1006 | protected Set<String> getAllowedPackages() { |
| 1007 | final Set<String> allowedPackages = new ArraySet<>(); |
Julia Reynolds | b776e39 | 2019-07-25 12:36:14 -0400 | [diff] [blame] | 1008 | synchronized (mApproved) { |
| 1009 | for (int k = 0; k < mApproved.size(); k++) { |
| 1010 | ArrayMap<Boolean, ArraySet<String>> allowedByType = mApproved.valueAt(k); |
| 1011 | for (int i = 0; i < allowedByType.size(); i++) { |
| 1012 | final ArraySet<String> allowed = allowedByType.valueAt(i); |
| 1013 | for (int j = 0; j < allowed.size(); j++) { |
| 1014 | String pkgName = getPackageName(allowed.valueAt(j)); |
| 1015 | if (!TextUtils.isEmpty(pkgName)) { |
| 1016 | allowedPackages.add(pkgName); |
| 1017 | } |
Julia Reynolds | 4c456dd | 2019-01-07 12:22:00 -0500 | [diff] [blame] | 1018 | } |
| 1019 | } |
| 1020 | } |
| 1021 | } |
| 1022 | return allowedPackages; |
| 1023 | } |
| 1024 | |
Annie Meng | 8b646fd | 2019-02-01 18:46:42 +0000 | [diff] [blame] | 1025 | private void trimApprovedListsAccordingToInstalledServices(int userId) { |
Julia Reynolds | b776e39 | 2019-07-25 12:36:14 -0400 | [diff] [blame] | 1026 | synchronized (mApproved) { |
| 1027 | final ArrayMap<Boolean, ArraySet<String>> approvedByType = mApproved.get(userId); |
| 1028 | if (approvedByType == null) { |
| 1029 | return; |
| 1030 | } |
| 1031 | for (int i = 0; i < approvedByType.size(); i++) { |
| 1032 | final ArraySet<String> approved = approvedByType.valueAt(i); |
| 1033 | for (int j = approved.size() - 1; j >= 0; j--) { |
| 1034 | final String approvedPackageOrComponent = approved.valueAt(j); |
| 1035 | if (!isValidEntry(approvedPackageOrComponent, userId)) { |
| 1036 | approved.removeAt(j); |
| 1037 | Slog.v(TAG, "Removing " + approvedPackageOrComponent |
| 1038 | + " from approved list; no matching services found"); |
| 1039 | } else { |
| 1040 | if (DEBUG) { |
| 1041 | Slog.v(TAG, "Keeping " + approvedPackageOrComponent |
| 1042 | + " on approved list; matching services found"); |
| 1043 | } |
Julia Reynolds | b852e56 | 2017-06-06 16:14:18 -0400 | [diff] [blame] | 1044 | } |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 1045 | } |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 1046 | } |
Julia Reynolds | b852e56 | 2017-06-06 16:14:18 -0400 | [diff] [blame] | 1047 | } |
| 1048 | } |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 1049 | |
Julia Reynolds | b852e56 | 2017-06-06 16:14:18 -0400 | [diff] [blame] | 1050 | private boolean removeUninstalledItemsFromApprovedLists(int uninstalledUserId, String pkg) { |
| 1051 | boolean removed = false; |
Julia Reynolds | b776e39 | 2019-07-25 12:36:14 -0400 | [diff] [blame] | 1052 | synchronized (mApproved) { |
| 1053 | final ArrayMap<Boolean, ArraySet<String>> approvedByType = mApproved.get( |
| 1054 | uninstalledUserId); |
| 1055 | if (approvedByType != null) { |
| 1056 | int M = approvedByType.size(); |
| 1057 | for (int j = 0; j < M; j++) { |
| 1058 | final ArraySet<String> approved = approvedByType.valueAt(j); |
| 1059 | int O = approved.size(); |
| 1060 | for (int k = O - 1; k >= 0; k--) { |
| 1061 | final String packageOrComponent = approved.valueAt(k); |
| 1062 | final String packageName = getPackageName(packageOrComponent); |
| 1063 | if (TextUtils.equals(pkg, packageName)) { |
| 1064 | approved.removeAt(k); |
| 1065 | if (DEBUG) { |
| 1066 | Slog.v(TAG, "Removing " + packageOrComponent |
| 1067 | + " from approved list; uninstalled"); |
| 1068 | } |
Julia Reynolds | b852e56 | 2017-06-06 16:14:18 -0400 | [diff] [blame] | 1069 | } |
| 1070 | } |
| 1071 | } |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 1072 | } |
Denis Kuznetsov | 76c0268 | 2015-09-17 19:57:00 +0200 | [diff] [blame] | 1073 | } |
Julia Reynolds | b852e56 | 2017-06-06 16:14:18 -0400 | [diff] [blame] | 1074 | return removed; |
| 1075 | } |
Denis Kuznetsov | 76c0268 | 2015-09-17 19:57:00 +0200 | [diff] [blame] | 1076 | |
Julia Reynolds | b852e56 | 2017-06-06 16:14:18 -0400 | [diff] [blame] | 1077 | protected String getPackageName(String packageOrComponent) { |
| 1078 | final ComponentName component = ComponentName.unflattenFromString(packageOrComponent); |
| 1079 | if (component != null) { |
| 1080 | return component.getPackageName(); |
| 1081 | } else { |
| 1082 | return packageOrComponent; |
Denis Kuznetsov | 76c0268 | 2015-09-17 19:57:00 +0200 | [diff] [blame] | 1083 | } |
Julia Reynolds | b852e56 | 2017-06-06 16:14:18 -0400 | [diff] [blame] | 1084 | } |
Denis Kuznetsov | 76c0268 | 2015-09-17 19:57:00 +0200 | [diff] [blame] | 1085 | |
Julia Reynolds | e5ce071bf | 2017-09-15 14:26:32 -0400 | [diff] [blame] | 1086 | protected boolean isValidEntry(String packageOrComponent, int userId) { |
| 1087 | return hasMatchingServices(packageOrComponent, userId); |
| 1088 | } |
| 1089 | |
Julia Reynolds | b852e56 | 2017-06-06 16:14:18 -0400 | [diff] [blame] | 1090 | private boolean hasMatchingServices(String packageOrComponent, int userId) { |
| 1091 | if (!TextUtils.isEmpty(packageOrComponent)) { |
| 1092 | final String packageName = getPackageName(packageOrComponent); |
| 1093 | return queryPackageForServices(packageName, userId).size() > 0; |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 1094 | } |
Julia Reynolds | b852e56 | 2017-06-06 16:14:18 -0400 | [diff] [blame] | 1095 | return false; |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 1096 | } |
| 1097 | |
Julia Reynolds | ca8e535 | 2018-09-18 13:39:26 -0400 | [diff] [blame] | 1098 | @VisibleForTesting |
| 1099 | protected SparseArray<ArraySet<ComponentName>> getAllowedComponents(IntArray userIds) { |
| 1100 | final int nUserIds = userIds.size(); |
Denis Kuznetsov | 76c0268 | 2015-09-17 19:57:00 +0200 | [diff] [blame] | 1101 | final SparseArray<ArraySet<ComponentName>> componentsByUser = new SparseArray<>(); |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 1102 | |
| 1103 | for (int i = 0; i < nUserIds; ++i) { |
Julia Reynolds | ca8e535 | 2018-09-18 13:39:26 -0400 | [diff] [blame] | 1104 | final int userId = userIds.get(i); |
Julia Reynolds | b776e39 | 2019-07-25 12:36:14 -0400 | [diff] [blame] | 1105 | synchronized (mApproved) { |
| 1106 | final ArrayMap<Boolean, ArraySet<String>> approvedLists = mApproved.get(userId); |
| 1107 | if (approvedLists != null) { |
| 1108 | final int N = approvedLists.size(); |
| 1109 | for (int j = 0; j < N; j++) { |
| 1110 | ArraySet<ComponentName> approvedByUser = componentsByUser.get(userId); |
| 1111 | if (approvedByUser == null) { |
| 1112 | approvedByUser = new ArraySet<>(); |
| 1113 | componentsByUser.put(userId, approvedByUser); |
| 1114 | } |
| 1115 | approvedByUser.addAll( |
| 1116 | loadComponentNamesFromValues(approvedLists.valueAt(j), userId)); |
Julia Reynolds | b852e56 | 2017-06-06 16:14:18 -0400 | [diff] [blame] | 1117 | } |
Julia Reynolds | b852e56 | 2017-06-06 16:14:18 -0400 | [diff] [blame] | 1118 | } |
Julia Reynolds | 6e839b0 | 2016-04-13 10:01:17 -0400 | [diff] [blame] | 1119 | } |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 1120 | } |
Julia Reynolds | ca8e535 | 2018-09-18 13:39:26 -0400 | [diff] [blame] | 1121 | return componentsByUser; |
| 1122 | } |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 1123 | |
Julia Reynolds | ca8e535 | 2018-09-18 13:39:26 -0400 | [diff] [blame] | 1124 | @GuardedBy("mMutex") |
| 1125 | protected void populateComponentsToBind(SparseArray<Set<ComponentName>> componentsToBind, |
| 1126 | final IntArray activeUsers, |
| 1127 | SparseArray<ArraySet<ComponentName>> approvedComponentsByUser) { |
| 1128 | mEnabledServicesForCurrentProfiles.clear(); |
| 1129 | mEnabledServicesPackageNames.clear(); |
| 1130 | final int nUserIds = activeUsers.size(); |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 1131 | |
| 1132 | for (int i = 0; i < nUserIds; ++i) { |
Julia Reynolds | ca8e535 | 2018-09-18 13:39:26 -0400 | [diff] [blame] | 1133 | // decode the list of components |
| 1134 | final int userId = activeUsers.get(i); |
| 1135 | final ArraySet<ComponentName> userComponents = approvedComponentsByUser.get(userId); |
| 1136 | if (null == userComponents) { |
| 1137 | componentsToBind.put(userId, new ArraySet<>()); |
| 1138 | continue; |
| 1139 | } |
| 1140 | |
| 1141 | final Set<ComponentName> add = new HashSet<>(userComponents); |
| 1142 | add.removeAll(mSnoozingForCurrentProfiles); |
| 1143 | |
| 1144 | componentsToBind.put(userId, add); |
| 1145 | |
| 1146 | mEnabledServicesForCurrentProfiles.addAll(userComponents); |
| 1147 | |
| 1148 | for (int j = 0; j < userComponents.size(); j++) { |
| 1149 | final ComponentName component = userComponents.valueAt(j); |
| 1150 | mEnabledServicesPackageNames.add(component.getPackageName()); |
| 1151 | } |
| 1152 | } |
| 1153 | } |
| 1154 | |
| 1155 | @GuardedBy("mMutex") |
| 1156 | protected Set<ManagedServiceInfo> getRemovableConnectedServices() { |
| 1157 | final Set<ManagedServiceInfo> removableBoundServices = new ArraySet<>(); |
| 1158 | for (ManagedServiceInfo service : mServices) { |
| 1159 | if (!service.isSystem && !service.isGuest(this)) { |
| 1160 | removableBoundServices.add(service); |
| 1161 | } |
| 1162 | } |
| 1163 | return removableBoundServices; |
| 1164 | } |
| 1165 | |
| 1166 | protected void populateComponentsToUnbind( |
| 1167 | boolean forceRebind, |
| 1168 | Set<ManagedServiceInfo> removableBoundServices, |
| 1169 | SparseArray<Set<ComponentName>> allowedComponentsToBind, |
| 1170 | SparseArray<Set<ComponentName>> componentsToUnbind) { |
| 1171 | for (ManagedServiceInfo info : removableBoundServices) { |
| 1172 | final Set<ComponentName> allowedComponents = allowedComponentsToBind.get(info.userid); |
| 1173 | if (allowedComponents != null) { |
| 1174 | if (forceRebind || !allowedComponents.contains(info.component)) { |
| 1175 | Set<ComponentName> toUnbind = |
| 1176 | componentsToUnbind.get(info.userid, new ArraySet<>()); |
| 1177 | toUnbind.add(info.component); |
| 1178 | componentsToUnbind.put(info.userid, toUnbind); |
| 1179 | } |
| 1180 | } |
| 1181 | } |
| 1182 | } |
| 1183 | |
| 1184 | /** |
| 1185 | * Called whenever packages change, the user switches, or the secure setting |
| 1186 | * is altered. (For example in response to USER_SWITCHED in our broadcast receiver) |
| 1187 | */ |
| 1188 | protected void rebindServices(boolean forceRebind, int userToRebind) { |
| 1189 | if (DEBUG) Slog.d(TAG, "rebindServices " + forceRebind + " " + userToRebind); |
| 1190 | IntArray userIds = mUserProfiles.getCurrentProfileIds(); |
| 1191 | if (userToRebind != USER_ALL) { |
| 1192 | userIds = new IntArray(1); |
| 1193 | userIds.add(userToRebind); |
| 1194 | } |
| 1195 | |
| 1196 | final SparseArray<Set<ComponentName>> componentsToBind = new SparseArray<>(); |
| 1197 | final SparseArray<Set<ComponentName>> componentsToUnbind = new SparseArray<>(); |
| 1198 | |
| 1199 | synchronized (mMutex) { |
| 1200 | final SparseArray<ArraySet<ComponentName>> approvedComponentsByUser = |
| 1201 | getAllowedComponents(userIds); |
| 1202 | final Set<ManagedServiceInfo> removableBoundServices = getRemovableConnectedServices(); |
| 1203 | |
| 1204 | // Filter approvedComponentsByUser to collect all of the components that are allowed |
| 1205 | // for the currently active user(s). |
| 1206 | populateComponentsToBind(componentsToBind, userIds, approvedComponentsByUser); |
| 1207 | |
| 1208 | // For every current non-system connection, disconnect services that are no longer |
| 1209 | // approved, or ALL services if we are force rebinding |
| 1210 | populateComponentsToUnbind( |
| 1211 | forceRebind, removableBoundServices, componentsToBind, componentsToUnbind); |
| 1212 | } |
| 1213 | |
| 1214 | unbindFromServices(componentsToUnbind); |
| 1215 | bindToServices(componentsToBind); |
| 1216 | } |
| 1217 | |
| 1218 | protected void unbindFromServices(SparseArray<Set<ComponentName>> componentsToUnbind) { |
| 1219 | for (int i = 0; i < componentsToUnbind.size(); i++) { |
| 1220 | final int userId = componentsToUnbind.keyAt(i); |
| 1221 | final Set<ComponentName> removableComponents = componentsToUnbind.get(userId); |
| 1222 | for (ComponentName cn : removableComponents) { |
| 1223 | // No longer allowed to be bound, or must rebind. |
| 1224 | Slog.v(TAG, "disabling " + getCaption() + " for user " + userId + ": " + cn); |
| 1225 | unregisterService(cn, userId); |
| 1226 | } |
| 1227 | } |
| 1228 | } |
| 1229 | |
| 1230 | // Attempt to bind to services, skipping those that cannot be found or lack the permission. |
| 1231 | private void bindToServices(SparseArray<Set<ComponentName>> componentsToBind) { |
| 1232 | for (int i = 0; i < componentsToBind.size(); i++) { |
| 1233 | final int userId = componentsToBind.keyAt(i); |
| 1234 | final Set<ComponentName> add = componentsToBind.get(userId); |
Julia Reynolds | 9a86cc0 | 2016-02-10 15:38:15 -0500 | [diff] [blame] | 1235 | for (ComponentName component : add) { |
Julia Reynolds | a75c752 | 2017-03-21 17:34:25 -0400 | [diff] [blame] | 1236 | try { |
| 1237 | ServiceInfo info = mPm.getServiceInfo(component, |
| 1238 | PackageManager.MATCH_DIRECT_BOOT_AWARE |
Julia Reynolds | ca8e535 | 2018-09-18 13:39:26 -0400 | [diff] [blame] | 1239 | | PackageManager.MATCH_DIRECT_BOOT_UNAWARE, userId); |
Julia Reynolds | 503ed94 | 2017-10-04 16:04:56 -0400 | [diff] [blame] | 1240 | if (info == null) { |
| 1241 | Slog.w(TAG, "Not binding " + getCaption() + " service " + component |
| 1242 | + ": service not found"); |
| 1243 | continue; |
| 1244 | } |
| 1245 | if (!mConfig.bindPermission.equals(info.permission)) { |
Julia Reynolds | b852e56 | 2017-06-06 16:14:18 -0400 | [diff] [blame] | 1246 | Slog.w(TAG, "Not binding " + getCaption() + " service " + component |
Julia Reynolds | a75c752 | 2017-03-21 17:34:25 -0400 | [diff] [blame] | 1247 | + ": it does not require the permission " + mConfig.bindPermission); |
| 1248 | continue; |
| 1249 | } |
| 1250 | Slog.v(TAG, |
Julia Reynolds | ca8e535 | 2018-09-18 13:39:26 -0400 | [diff] [blame] | 1251 | "enabling " + getCaption() + " for " + userId + ": " + component); |
| 1252 | registerService(component, userId); |
Julia Reynolds | a75c752 | 2017-03-21 17:34:25 -0400 | [diff] [blame] | 1253 | } catch (RemoteException e) { |
| 1254 | e.rethrowFromSystemServer(); |
| 1255 | } |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 1256 | } |
| 1257 | } |
| 1258 | } |
| 1259 | |
| 1260 | /** |
| 1261 | * Version of registerService that takes the name of a service component to bind to. |
| 1262 | */ |
| 1263 | private void registerService(final ComponentName name, final int userid) { |
Ruben Brunk | dd18a0b | 2015-12-04 16:16:31 -0800 | [diff] [blame] | 1264 | synchronized (mMutex) { |
| 1265 | registerServiceLocked(name, userid); |
| 1266 | } |
| 1267 | } |
| 1268 | |
Chris Wren | 0efdb88 | 2016-03-01 17:17:47 -0500 | [diff] [blame] | 1269 | /** |
| 1270 | * Inject a system service into the management list. |
| 1271 | */ |
| 1272 | public void registerSystemService(final ComponentName name, final int userid) { |
| 1273 | synchronized (mMutex) { |
| 1274 | registerServiceLocked(name, userid, true /* isSystem */); |
| 1275 | } |
| 1276 | } |
| 1277 | |
Ruben Brunk | dd18a0b | 2015-12-04 16:16:31 -0800 | [diff] [blame] | 1278 | private void registerServiceLocked(final ComponentName name, final int userid) { |
Chris Wren | 0efdb88 | 2016-03-01 17:17:47 -0500 | [diff] [blame] | 1279 | registerServiceLocked(name, userid, false /* isSystem */); |
| 1280 | } |
| 1281 | |
| 1282 | private void registerServiceLocked(final ComponentName name, final int userid, |
| 1283 | final boolean isSystem) { |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 1284 | if (DEBUG) Slog.v(TAG, "registerService: " + name + " u=" + userid); |
| 1285 | |
Andrew Zeng | 190c067 | 2018-06-28 15:15:05 -0700 | [diff] [blame] | 1286 | final Pair<ComponentName, Integer> servicesBindingTag = Pair.create(name, userid); |
| 1287 | if (mServicesBound.contains(servicesBindingTag)) { |
| 1288 | Slog.v(TAG, "Not registering " + name + " is already bound"); |
Ruben Brunk | dd18a0b | 2015-12-04 16:16:31 -0800 | [diff] [blame] | 1289 | // stop registering this thing already! we're working on it |
| 1290 | return; |
| 1291 | } |
Andrew Zeng | 190c067 | 2018-06-28 15:15:05 -0700 | [diff] [blame] | 1292 | mServicesBound.add(servicesBindingTag); |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 1293 | |
Ruben Brunk | dd18a0b | 2015-12-04 16:16:31 -0800 | [diff] [blame] | 1294 | final int N = mServices.size(); |
| 1295 | for (int i = N - 1; i >= 0; i--) { |
| 1296 | final ManagedServiceInfo info = mServices.get(i); |
| 1297 | if (name.equals(info.component) |
| 1298 | && info.userid == userid) { |
| 1299 | // cut old connections |
Julia Reynolds | e5ce071bf | 2017-09-15 14:26:32 -0400 | [diff] [blame] | 1300 | Slog.v(TAG, " disconnecting old " + getCaption() + ": " + info.service); |
Ruben Brunk | dd18a0b | 2015-12-04 16:16:31 -0800 | [diff] [blame] | 1301 | removeServiceLocked(i); |
| 1302 | if (info.connection != null) { |
Andrew Zeng | 190c067 | 2018-06-28 15:15:05 -0700 | [diff] [blame] | 1303 | unbindService(info.connection, info.component, info.userid); |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 1304 | } |
| 1305 | } |
Ruben Brunk | dd18a0b | 2015-12-04 16:16:31 -0800 | [diff] [blame] | 1306 | } |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 1307 | |
Ruben Brunk | dd18a0b | 2015-12-04 16:16:31 -0800 | [diff] [blame] | 1308 | Intent intent = new Intent(mConfig.serviceInterface); |
| 1309 | intent.setComponent(name); |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 1310 | |
Ruben Brunk | dd18a0b | 2015-12-04 16:16:31 -0800 | [diff] [blame] | 1311 | intent.putExtra(Intent.EXTRA_CLIENT_LABEL, mConfig.clientLabel); |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 1312 | |
Ruben Brunk | dd18a0b | 2015-12-04 16:16:31 -0800 | [diff] [blame] | 1313 | final PendingIntent pendingIntent = PendingIntent.getActivity( |
| 1314 | mContext, 0, new Intent(mConfig.settingsAction), 0); |
| 1315 | intent.putExtra(Intent.EXTRA_CLIENT_INTENT, pendingIntent); |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 1316 | |
Ruben Brunk | dd18a0b | 2015-12-04 16:16:31 -0800 | [diff] [blame] | 1317 | ApplicationInfo appInfo = null; |
| 1318 | try { |
| 1319 | appInfo = mContext.getPackageManager().getApplicationInfo( |
| 1320 | name.getPackageName(), 0); |
| 1321 | } catch (NameNotFoundException e) { |
| 1322 | // Ignore if the package doesn't exist we won't be able to bind to the service. |
| 1323 | } |
| 1324 | final int targetSdkVersion = |
| 1325 | appInfo != null ? appInfo.targetSdkVersion : Build.VERSION_CODES.BASE; |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 1326 | |
Ruben Brunk | dd18a0b | 2015-12-04 16:16:31 -0800 | [diff] [blame] | 1327 | try { |
Julia Reynolds | e5ce071bf | 2017-09-15 14:26:32 -0400 | [diff] [blame] | 1328 | Slog.v(TAG, "binding: " + intent); |
Ruben Brunk | dd18a0b | 2015-12-04 16:16:31 -0800 | [diff] [blame] | 1329 | ServiceConnection serviceConnection = new ServiceConnection() { |
| 1330 | IInterface mService; |
Denis Kuznetsov | 76c0268 | 2015-09-17 19:57:00 +0200 | [diff] [blame] | 1331 | |
Ruben Brunk | dd18a0b | 2015-12-04 16:16:31 -0800 | [diff] [blame] | 1332 | @Override |
| 1333 | public void onServiceConnected(ComponentName name, IBinder binder) { |
Julia Reynolds | ca8e535 | 2018-09-18 13:39:26 -0400 | [diff] [blame] | 1334 | Slog.v(TAG, userid + " " + getCaption() + " service connected: " + name); |
Ruben Brunk | dd18a0b | 2015-12-04 16:16:31 -0800 | [diff] [blame] | 1335 | boolean added = false; |
| 1336 | ManagedServiceInfo info = null; |
| 1337 | synchronized (mMutex) { |
Ryan Lothian | 4a86a51 | 2017-12-04 11:56:58 -0500 | [diff] [blame] | 1338 | mServicesRebinding.remove(servicesBindingTag); |
Ruben Brunk | dd18a0b | 2015-12-04 16:16:31 -0800 | [diff] [blame] | 1339 | try { |
| 1340 | mService = asInterface(binder); |
| 1341 | info = newServiceInfo(mService, name, |
Chris Wren | 0efdb88 | 2016-03-01 17:17:47 -0500 | [diff] [blame] | 1342 | userid, isSystem, this, targetSdkVersion); |
Ruben Brunk | dd18a0b | 2015-12-04 16:16:31 -0800 | [diff] [blame] | 1343 | binder.linkToDeath(info, 0); |
| 1344 | added = mServices.add(info); |
| 1345 | } catch (RemoteException e) { |
Tony Mak | 180a9c4 | 2019-03-08 13:33:08 +0000 | [diff] [blame] | 1346 | Slog.e(TAG, "Failed to linkToDeath, already dead", e); |
Denis Kuznetsov | 76c0268 | 2015-09-17 19:57:00 +0200 | [diff] [blame] | 1347 | } |
| 1348 | } |
Ruben Brunk | dd18a0b | 2015-12-04 16:16:31 -0800 | [diff] [blame] | 1349 | if (added) { |
| 1350 | onServiceAdded(info); |
Denis Kuznetsov | 76c0268 | 2015-09-17 19:57:00 +0200 | [diff] [blame] | 1351 | } |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 1352 | } |
Ruben Brunk | dd18a0b | 2015-12-04 16:16:31 -0800 | [diff] [blame] | 1353 | |
| 1354 | @Override |
| 1355 | public void onServiceDisconnected(ComponentName name) { |
Julia Reynolds | ca8e535 | 2018-09-18 13:39:26 -0400 | [diff] [blame] | 1356 | Slog.v(TAG, userid + " " + getCaption() + " connection lost: " + name); |
Ruben Brunk | dd18a0b | 2015-12-04 16:16:31 -0800 | [diff] [blame] | 1357 | } |
Ryan Lothian | 4a86a51 | 2017-12-04 11:56:58 -0500 | [diff] [blame] | 1358 | |
| 1359 | @Override |
| 1360 | public void onBindingDied(ComponentName name) { |
Julia Reynolds | ca8e535 | 2018-09-18 13:39:26 -0400 | [diff] [blame] | 1361 | Slog.w(TAG, userid + " " + getCaption() + " binding died: " + name); |
Ryan Lothian | 4a86a51 | 2017-12-04 11:56:58 -0500 | [diff] [blame] | 1362 | synchronized (mMutex) { |
Andrew Zeng | 190c067 | 2018-06-28 15:15:05 -0700 | [diff] [blame] | 1363 | unbindService(this, name, userid); |
Ryan Lothian | 4a86a51 | 2017-12-04 11:56:58 -0500 | [diff] [blame] | 1364 | if (!mServicesRebinding.contains(servicesBindingTag)) { |
| 1365 | mServicesRebinding.add(servicesBindingTag); |
| 1366 | mHandler.postDelayed(new Runnable() { |
| 1367 | @Override |
| 1368 | public void run() { |
| 1369 | registerService(name, userid); |
| 1370 | } |
| 1371 | }, ON_BINDING_DIED_REBIND_DELAY_MS); |
| 1372 | } else { |
Julia Reynolds | ca8e535 | 2018-09-18 13:39:26 -0400 | [diff] [blame] | 1373 | Slog.v(TAG, getCaption() + " not rebinding in user " + userid |
| 1374 | + " as a previous rebind attempt was made: " + name); |
Ryan Lothian | 4a86a51 | 2017-12-04 11:56:58 -0500 | [diff] [blame] | 1375 | } |
| 1376 | } |
| 1377 | } |
Tony Mak | 180a9c4 | 2019-03-08 13:33:08 +0000 | [diff] [blame] | 1378 | |
| 1379 | @Override |
| 1380 | public void onNullBinding(ComponentName name) { |
| 1381 | Slog.v(TAG, "onNullBinding() called with: name = [" + name + "]"); |
Julia Reynolds | 8a4f2ab | 2019-04-10 10:08:21 -0400 | [diff] [blame] | 1382 | mServicesBound.remove(servicesBindingTag); |
Tony Mak | 180a9c4 | 2019-03-08 13:33:08 +0000 | [diff] [blame] | 1383 | } |
Ruben Brunk | dd18a0b | 2015-12-04 16:16:31 -0800 | [diff] [blame] | 1384 | }; |
| 1385 | if (!mContext.bindServiceAsUser(intent, |
Amith Yamasani | e5bfeee | 2018-09-05 18:52:35 -0700 | [diff] [blame] | 1386 | serviceConnection, |
| 1387 | getBindFlags(), |
| 1388 | new UserHandle(userid))) { |
Andrew Zeng | 190c067 | 2018-06-28 15:15:05 -0700 | [diff] [blame] | 1389 | mServicesBound.remove(servicesBindingTag); |
Julia Reynolds | ca8e535 | 2018-09-18 13:39:26 -0400 | [diff] [blame] | 1390 | Slog.w(TAG, "Unable to bind " + getCaption() + " service: " + intent |
| 1391 | + " in user " + userid); |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 1392 | return; |
| 1393 | } |
Ruben Brunk | dd18a0b | 2015-12-04 16:16:31 -0800 | [diff] [blame] | 1394 | } catch (SecurityException ex) { |
Andrew Zeng | 190c067 | 2018-06-28 15:15:05 -0700 | [diff] [blame] | 1395 | mServicesBound.remove(servicesBindingTag); |
Ruben Brunk | dd18a0b | 2015-12-04 16:16:31 -0800 | [diff] [blame] | 1396 | Slog.e(TAG, "Unable to bind " + getCaption() + " service: " + intent, ex); |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 1397 | } |
| 1398 | } |
| 1399 | |
Julia Reynolds | 8a4f2ab | 2019-04-10 10:08:21 -0400 | [diff] [blame] | 1400 | boolean isBound(ComponentName cn, int userId) { |
| 1401 | final Pair<ComponentName, Integer> servicesBindingTag = Pair.create(cn, userId); |
| 1402 | return mServicesBound.contains(servicesBindingTag); |
| 1403 | } |
| 1404 | |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 1405 | /** |
| 1406 | * Remove a service for the given user by ComponentName |
| 1407 | */ |
| 1408 | private void unregisterService(ComponentName name, int userid) { |
| 1409 | synchronized (mMutex) { |
Ruben Brunk | dd18a0b | 2015-12-04 16:16:31 -0800 | [diff] [blame] | 1410 | unregisterServiceLocked(name, userid); |
| 1411 | } |
| 1412 | } |
| 1413 | |
| 1414 | private void unregisterServiceLocked(ComponentName name, int userid) { |
| 1415 | final int N = mServices.size(); |
| 1416 | for (int i = N - 1; i >= 0; i--) { |
| 1417 | final ManagedServiceInfo info = mServices.get(i); |
Julia Reynolds | e5ce071bf | 2017-09-15 14:26:32 -0400 | [diff] [blame] | 1418 | if (name.equals(info.component) && info.userid == userid) { |
Ruben Brunk | dd18a0b | 2015-12-04 16:16:31 -0800 | [diff] [blame] | 1419 | removeServiceLocked(i); |
| 1420 | if (info.connection != null) { |
Andrew Zeng | 190c067 | 2018-06-28 15:15:05 -0700 | [diff] [blame] | 1421 | unbindService(info.connection, info.component, info.userid); |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 1422 | } |
| 1423 | } |
| 1424 | } |
| 1425 | } |
| 1426 | |
| 1427 | /** |
| 1428 | * Removes a service from the list but does not unbind |
| 1429 | * |
| 1430 | * @return the removed service. |
| 1431 | */ |
| 1432 | private ManagedServiceInfo removeServiceImpl(IInterface service, final int userid) { |
John Spurlock | e77bb36 | 2014-04-26 10:24:59 -0400 | [diff] [blame] | 1433 | if (DEBUG) Slog.d(TAG, "removeServiceImpl service=" + service + " u=" + userid); |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 1434 | ManagedServiceInfo serviceInfo = null; |
| 1435 | synchronized (mMutex) { |
| 1436 | final int N = mServices.size(); |
Denis Kuznetsov | 76c0268 | 2015-09-17 19:57:00 +0200 | [diff] [blame] | 1437 | for (int i = N - 1; i >= 0; i--) { |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 1438 | final ManagedServiceInfo info = mServices.get(i); |
Julia Reynolds | e5ce071bf | 2017-09-15 14:26:32 -0400 | [diff] [blame] | 1439 | if (info.service.asBinder() == service.asBinder() && info.userid == userid) { |
| 1440 | Slog.d(TAG, "Removing active service " + info.component); |
John Spurlock | e77bb36 | 2014-04-26 10:24:59 -0400 | [diff] [blame] | 1441 | serviceInfo = removeServiceLocked(i); |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 1442 | } |
| 1443 | } |
| 1444 | } |
| 1445 | return serviceInfo; |
| 1446 | } |
| 1447 | |
John Spurlock | e77bb36 | 2014-04-26 10:24:59 -0400 | [diff] [blame] | 1448 | private ManagedServiceInfo removeServiceLocked(int i) { |
| 1449 | final ManagedServiceInfo info = mServices.remove(i); |
| 1450 | onServiceRemovedLocked(info); |
| 1451 | return info; |
| 1452 | } |
| 1453 | |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 1454 | private void checkNotNull(IInterface service) { |
| 1455 | if (service == null) { |
| 1456 | throw new IllegalArgumentException(getCaption() + " must not be null"); |
| 1457 | } |
| 1458 | } |
| 1459 | |
Christoph Studer | 3e144d3 | 2014-05-22 16:48:40 +0200 | [diff] [blame] | 1460 | private ManagedServiceInfo registerServiceImpl(final IInterface service, |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 1461 | final ComponentName component, final int userid) { |
Chris Wren | 51017d0 | 2015-12-15 15:34:46 -0500 | [diff] [blame] | 1462 | ManagedServiceInfo info = newServiceInfo(service, component, userid, |
| 1463 | true /*isSystem*/, null /*connection*/, Build.VERSION_CODES.LOLLIPOP); |
| 1464 | return registerServiceImpl(info); |
| 1465 | } |
| 1466 | |
| 1467 | private ManagedServiceInfo registerServiceImpl(ManagedServiceInfo info) { |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 1468 | synchronized (mMutex) { |
| 1469 | try { |
Chris Wren | 51017d0 | 2015-12-15 15:34:46 -0500 | [diff] [blame] | 1470 | info.service.asBinder().linkToDeath(info, 0); |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 1471 | mServices.add(info); |
Christoph Studer | 3e144d3 | 2014-05-22 16:48:40 +0200 | [diff] [blame] | 1472 | return info; |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 1473 | } catch (RemoteException e) { |
| 1474 | // already dead |
| 1475 | } |
| 1476 | } |
Christoph Studer | 3e144d3 | 2014-05-22 16:48:40 +0200 | [diff] [blame] | 1477 | return null; |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 1478 | } |
| 1479 | |
| 1480 | /** |
| 1481 | * Removes a service from the list and unbinds. |
| 1482 | */ |
| 1483 | private void unregisterServiceImpl(IInterface service, int userid) { |
| 1484 | ManagedServiceInfo info = removeServiceImpl(service, userid); |
Chris Wren | 51017d0 | 2015-12-15 15:34:46 -0500 | [diff] [blame] | 1485 | if (info != null && info.connection != null && !info.isGuest(this)) { |
Andrew Zeng | 190c067 | 2018-06-28 15:15:05 -0700 | [diff] [blame] | 1486 | unbindService(info.connection, info.component, info.userid); |
| 1487 | } |
| 1488 | } |
| 1489 | |
| 1490 | private void unbindService(ServiceConnection connection, ComponentName component, int userId) { |
| 1491 | try { |
| 1492 | mContext.unbindService(connection); |
| 1493 | } catch (IllegalArgumentException e) { |
| 1494 | Slog.e(TAG, getCaption() + " " + component + " could not be unbound", e); |
| 1495 | } |
| 1496 | synchronized (mMutex) { |
| 1497 | mServicesBound.remove(Pair.create(component, userId)); |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 1498 | } |
| 1499 | } |
| 1500 | |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 1501 | public class ManagedServiceInfo implements IBinder.DeathRecipient { |
| 1502 | public IInterface service; |
| 1503 | public ComponentName component; |
| 1504 | public int userid; |
| 1505 | public boolean isSystem; |
| 1506 | public ServiceConnection connection; |
| 1507 | public int targetSdkVersion; |
| 1508 | |
| 1509 | public ManagedServiceInfo(IInterface service, ComponentName component, |
| 1510 | int userid, boolean isSystem, ServiceConnection connection, int targetSdkVersion) { |
| 1511 | this.service = service; |
| 1512 | this.component = component; |
| 1513 | this.userid = userid; |
| 1514 | this.isSystem = isSystem; |
| 1515 | this.connection = connection; |
| 1516 | this.targetSdkVersion = targetSdkVersion; |
| 1517 | } |
| 1518 | |
Chris Wren | 51017d0 | 2015-12-15 15:34:46 -0500 | [diff] [blame] | 1519 | public boolean isGuest(ManagedServices host) { |
| 1520 | return ManagedServices.this != host; |
| 1521 | } |
| 1522 | |
Chris Wren | ab41eec | 2016-01-04 18:01:27 -0500 | [diff] [blame] | 1523 | public ManagedServices getOwner() { |
| 1524 | return ManagedServices.this; |
| 1525 | } |
| 1526 | |
John Spurlock | e77bb36 | 2014-04-26 10:24:59 -0400 | [diff] [blame] | 1527 | @Override |
| 1528 | public String toString() { |
| 1529 | return new StringBuilder("ManagedServiceInfo[") |
| 1530 | .append("component=").append(component) |
| 1531 | .append(",userid=").append(userid) |
| 1532 | .append(",isSystem=").append(isSystem) |
| 1533 | .append(",targetSdkVersion=").append(targetSdkVersion) |
| 1534 | .append(",connection=").append(connection == null ? null : "<connection>") |
| 1535 | .append(",service=").append(service) |
| 1536 | .append(']').toString(); |
| 1537 | } |
| 1538 | |
Jeffrey Huang | cb78285 | 2019-12-05 11:28:11 -0800 | [diff] [blame] | 1539 | public void dumpDebug(ProtoOutputStream proto, long fieldId, ManagedServices host) { |
Kweku Adams | 85f2fbc | 2017-12-18 12:04:12 -0800 | [diff] [blame] | 1540 | final long token = proto.start(fieldId); |
Jeffrey Huang | cb78285 | 2019-12-05 11:28:11 -0800 | [diff] [blame] | 1541 | component.dumpDebug(proto, ManagedServiceInfoProto.COMPONENT); |
Kweku Adams | 93304b6 | 2017-09-20 17:03:00 -0700 | [diff] [blame] | 1542 | proto.write(ManagedServiceInfoProto.USER_ID, userid); |
| 1543 | proto.write(ManagedServiceInfoProto.SERVICE, service.getClass().getName()); |
| 1544 | proto.write(ManagedServiceInfoProto.IS_SYSTEM, isSystem); |
| 1545 | proto.write(ManagedServiceInfoProto.IS_GUEST, isGuest(host)); |
Kweku Adams | 85f2fbc | 2017-12-18 12:04:12 -0800 | [diff] [blame] | 1546 | proto.end(token); |
Kweku Adams | 93304b6 | 2017-09-20 17:03:00 -0700 | [diff] [blame] | 1547 | } |
| 1548 | |
Julia Reynolds | 70aaea7 | 2018-07-13 13:38:34 -0400 | [diff] [blame] | 1549 | public boolean isSameUser(int userId) { |
| 1550 | if (!isEnabledForCurrentProfiles()) { |
| 1551 | return false; |
| 1552 | } |
| 1553 | return this.userid == userId; |
| 1554 | } |
| 1555 | |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 1556 | public boolean enabledAndUserMatches(int nid) { |
| 1557 | if (!isEnabledForCurrentProfiles()) { |
| 1558 | return false; |
| 1559 | } |
Julia Reynolds | ca8e535 | 2018-09-18 13:39:26 -0400 | [diff] [blame] | 1560 | if (this.userid == USER_ALL) return true; |
Chris Wren | 0c4eeb4 | 2016-05-12 13:21:08 -0400 | [diff] [blame] | 1561 | if (this.isSystem) return true; |
Julia Reynolds | ca8e535 | 2018-09-18 13:39:26 -0400 | [diff] [blame] | 1562 | if (nid == USER_ALL || nid == this.userid) return true; |
Esteban Talavera | 9c6458d | 2017-03-30 17:59:50 +0100 | [diff] [blame] | 1563 | return supportsProfiles() |
| 1564 | && mUserProfiles.isCurrentProfile(nid) |
| 1565 | && isPermittedForProfile(nid); |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 1566 | } |
| 1567 | |
| 1568 | public boolean supportsProfiles() { |
Dianne Hackborn | 955d8d6 | 2014-10-07 20:17:19 -0700 | [diff] [blame] | 1569 | return targetSdkVersion >= Build.VERSION_CODES.LOLLIPOP; |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 1570 | } |
| 1571 | |
| 1572 | @Override |
| 1573 | public void binderDied() { |
John Spurlock | e77bb36 | 2014-04-26 10:24:59 -0400 | [diff] [blame] | 1574 | if (DEBUG) Slog.d(TAG, "binderDied"); |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 1575 | // Remove the service, but don't unbind from the service. The system will bring the |
Esteban Talavera | 9c6458d | 2017-03-30 17:59:50 +0100 | [diff] [blame] | 1576 | // service back up, and the onServiceConnected handler will read the service with the |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 1577 | // new binding. If this isn't a bound service, and is just a registered |
| 1578 | // service, just removing it from the list is all we need to do anyway. |
| 1579 | removeServiceImpl(this.service, this.userid); |
| 1580 | } |
| 1581 | |
| 1582 | /** convenience method for looking in mEnabledServicesForCurrentProfiles */ |
| 1583 | public boolean isEnabledForCurrentProfiles() { |
| 1584 | if (this.isSystem) return true; |
| 1585 | if (this.connection == null) return false; |
| 1586 | return mEnabledServicesForCurrentProfiles.contains(this.component); |
| 1587 | } |
Esteban Talavera | 9c6458d | 2017-03-30 17:59:50 +0100 | [diff] [blame] | 1588 | |
| 1589 | /** |
| 1590 | * Returns true if this service is allowed to receive events for the given userId. A |
| 1591 | * managed profile owner can disallow non-system services running outside of the profile |
| 1592 | * from receiving events from the profile. |
| 1593 | */ |
| 1594 | public boolean isPermittedForProfile(int userId) { |
| 1595 | if (!mUserProfiles.isManagedProfile(userId)) { |
| 1596 | return true; |
| 1597 | } |
| 1598 | DevicePolicyManager dpm = |
| 1599 | (DevicePolicyManager) mContext.getSystemService(DEVICE_POLICY_SERVICE); |
| 1600 | final long identity = Binder.clearCallingIdentity(); |
| 1601 | try { |
| 1602 | return dpm.isNotificationListenerServicePermitted( |
| 1603 | component.getPackageName(), userId); |
| 1604 | } finally { |
| 1605 | Binder.restoreCallingIdentity(identity); |
| 1606 | } |
| 1607 | } |
Julia Reynolds | ca8e535 | 2018-09-18 13:39:26 -0400 | [diff] [blame] | 1608 | |
| 1609 | @Override |
| 1610 | public boolean equals(Object o) { |
| 1611 | if (this == o) return true; |
| 1612 | if (o == null || getClass() != o.getClass()) return false; |
| 1613 | ManagedServiceInfo that = (ManagedServiceInfo) o; |
| 1614 | return userid == that.userid |
| 1615 | && isSystem == that.isSystem |
| 1616 | && targetSdkVersion == that.targetSdkVersion |
| 1617 | && Objects.equals(service, that.service) |
| 1618 | && Objects.equals(component, that.component) |
| 1619 | && Objects.equals(connection, that.connection); |
| 1620 | } |
| 1621 | |
| 1622 | @Override |
| 1623 | public int hashCode() { |
| 1624 | return Objects.hash(service, component, userid, isSystem, connection, targetSdkVersion); |
| 1625 | } |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 1626 | } |
| 1627 | |
Chris Wren | ab41eec | 2016-01-04 18:01:27 -0500 | [diff] [blame] | 1628 | /** convenience method for looking in mEnabledServicesForCurrentProfiles */ |
| 1629 | public boolean isComponentEnabledForCurrentProfiles(ComponentName component) { |
| 1630 | return mEnabledServicesForCurrentProfiles.contains(component); |
| 1631 | } |
| 1632 | |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 1633 | public static class UserProfiles { |
| 1634 | // Profiles of the current user. |
Ruben Brunk | e24b9a6 | 2016-02-16 21:38:24 -0800 | [diff] [blame] | 1635 | private final SparseArray<UserInfo> mCurrentProfiles = new SparseArray<>(); |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 1636 | |
Ruben Brunk | e24b9a6 | 2016-02-16 21:38:24 -0800 | [diff] [blame] | 1637 | public void updateCache(@NonNull Context context) { |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 1638 | UserManager userManager = (UserManager) context.getSystemService(Context.USER_SERVICE); |
| 1639 | if (userManager != null) { |
| 1640 | int currentUserId = ActivityManager.getCurrentUser(); |
| 1641 | List<UserInfo> profiles = userManager.getProfiles(currentUserId); |
| 1642 | synchronized (mCurrentProfiles) { |
| 1643 | mCurrentProfiles.clear(); |
| 1644 | for (UserInfo user : profiles) { |
| 1645 | mCurrentProfiles.put(user.id, user); |
| 1646 | } |
| 1647 | } |
| 1648 | } |
| 1649 | } |
| 1650 | |
Julia Reynolds | ca8e535 | 2018-09-18 13:39:26 -0400 | [diff] [blame] | 1651 | /** |
| 1652 | * Returns the currently active users (generally one user and its work profile). |
| 1653 | */ |
| 1654 | public IntArray getCurrentProfileIds() { |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 1655 | synchronized (mCurrentProfiles) { |
Julia Reynolds | ca8e535 | 2018-09-18 13:39:26 -0400 | [diff] [blame] | 1656 | IntArray users = new IntArray(mCurrentProfiles.size()); |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 1657 | final int N = mCurrentProfiles.size(); |
| 1658 | for (int i = 0; i < N; ++i) { |
Julia Reynolds | ca8e535 | 2018-09-18 13:39:26 -0400 | [diff] [blame] | 1659 | users.add(mCurrentProfiles.keyAt(i)); |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 1660 | } |
| 1661 | return users; |
| 1662 | } |
| 1663 | } |
| 1664 | |
| 1665 | public boolean isCurrentProfile(int userId) { |
| 1666 | synchronized (mCurrentProfiles) { |
| 1667 | return mCurrentProfiles.get(userId) != null; |
| 1668 | } |
| 1669 | } |
Esteban Talavera | 9c6458d | 2017-03-30 17:59:50 +0100 | [diff] [blame] | 1670 | |
| 1671 | public boolean isManagedProfile(int userId) { |
| 1672 | synchronized (mCurrentProfiles) { |
| 1673 | UserInfo user = mCurrentProfiles.get(userId); |
| 1674 | return user != null && user.isManagedProfile(); |
| 1675 | } |
| 1676 | } |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 1677 | } |
| 1678 | |
Ruben Brunk | e24b9a6 | 2016-02-16 21:38:24 -0800 | [diff] [blame] | 1679 | public static class Config { |
| 1680 | public String caption; |
| 1681 | public String serviceInterface; |
| 1682 | public String secureSettingName; |
Julia Reynolds | 6e839b0 | 2016-04-13 10:01:17 -0400 | [diff] [blame] | 1683 | public String secondarySettingName; |
Julia Reynolds | d1bf5f0 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 1684 | public String xmlTag; |
Ruben Brunk | e24b9a6 | 2016-02-16 21:38:24 -0800 | [diff] [blame] | 1685 | public String bindPermission; |
| 1686 | public String settingsAction; |
| 1687 | public int clientLabel; |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 1688 | } |
| 1689 | } |