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