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 | |
| 19 | import android.app.ActivityManager; |
| 20 | import android.app.PendingIntent; |
Christopher Tate | 6597e34 | 2015-02-17 12:15:25 -0800 | [diff] [blame] | 21 | import android.content.BroadcastReceiver; |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 22 | import android.content.ComponentName; |
| 23 | import android.content.ContentResolver; |
| 24 | import android.content.Context; |
| 25 | import android.content.Intent; |
Christopher Tate | 6597e34 | 2015-02-17 12:15:25 -0800 | [diff] [blame] | 26 | import android.content.IntentFilter; |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 27 | import android.content.ServiceConnection; |
| 28 | import android.content.pm.ApplicationInfo; |
| 29 | import android.content.pm.PackageManager; |
| 30 | import android.content.pm.PackageManager.NameNotFoundException; |
| 31 | import android.content.pm.ResolveInfo; |
| 32 | import android.content.pm.ServiceInfo; |
| 33 | import android.content.pm.UserInfo; |
| 34 | import android.database.ContentObserver; |
| 35 | import android.net.Uri; |
| 36 | import android.os.Build; |
| 37 | import android.os.Handler; |
| 38 | import android.os.IBinder; |
| 39 | import android.os.IInterface; |
| 40 | import android.os.RemoteException; |
| 41 | import android.os.UserHandle; |
| 42 | import android.os.UserManager; |
| 43 | import android.provider.Settings; |
| 44 | import android.text.TextUtils; |
Ruben Brunk | dd18a0b | 2015-12-04 16:16:31 -0800 | [diff] [blame] | 45 | import android.util.ArrayMap; |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 46 | import android.util.ArraySet; |
John Spurlock | 4db0d98 | 2014-08-13 09:19:03 -0400 | [diff] [blame] | 47 | import android.util.Log; |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 48 | import android.util.Slog; |
| 49 | import android.util.SparseArray; |
| 50 | |
John Spurlock | 25e2d24 | 2014-06-27 13:58:23 -0400 | [diff] [blame] | 51 | import com.android.server.notification.NotificationManagerService.DumpFilter; |
| 52 | |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 53 | import java.io.PrintWriter; |
| 54 | import java.util.ArrayList; |
John Spurlock | e77bb36 | 2014-04-26 10:24:59 -0400 | [diff] [blame] | 55 | import java.util.Arrays; |
Julia Reynolds | 9a86cc0 | 2016-02-10 15:38:15 -0500 | [diff] [blame^] | 56 | import java.util.HashSet; |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 57 | import java.util.List; |
Ruben Brunk | dd18a0b | 2015-12-04 16:16:31 -0800 | [diff] [blame] | 58 | import java.util.Map.Entry; |
Christopher Tate | 6597e34 | 2015-02-17 12:15:25 -0800 | [diff] [blame] | 59 | import java.util.Objects; |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 60 | import java.util.Set; |
| 61 | |
| 62 | /** |
| 63 | * Manages the lifecycle of application-provided services bound by system server. |
| 64 | * |
| 65 | * Services managed by this helper must have: |
| 66 | * - An associated system settings value with a list of enabled component names. |
| 67 | * - A well-known action for services to use in their intent-filter. |
| 68 | * - A system permission for services to require in order to ensure system has exclusive binding. |
| 69 | * - A settings page for user configuration of enabled services, and associated intent action. |
| 70 | * - A remote interface definition (aidl) provided by the service used for communication. |
| 71 | */ |
| 72 | abstract public class ManagedServices { |
| 73 | protected final String TAG = getClass().getSimpleName(); |
John Spurlock | 4db0d98 | 2014-08-13 09:19:03 -0400 | [diff] [blame] | 74 | protected final boolean DEBUG = Log.isLoggable(TAG, Log.DEBUG); |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 75 | |
Julia Reynolds | c279b99 | 2015-10-30 08:23:51 -0400 | [diff] [blame] | 76 | protected static final String ENABLED_SERVICES_SEPARATOR = ":"; |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 77 | |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 78 | protected final Context mContext; |
John Spurlock | e77bb36 | 2014-04-26 10:24:59 -0400 | [diff] [blame] | 79 | protected final Object mMutex; |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 80 | private final UserProfiles mUserProfiles; |
| 81 | private final SettingsObserver mSettingsObserver; |
| 82 | private final Config mConfig; |
Christopher Tate | 6597e34 | 2015-02-17 12:15:25 -0800 | [diff] [blame] | 83 | private ArraySet<String> mRestored; |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 84 | |
| 85 | // contains connections to all connected services, including app services |
| 86 | // and system services |
| 87 | protected final ArrayList<ManagedServiceInfo> mServices = new ArrayList<ManagedServiceInfo>(); |
| 88 | // things that will be put into mServices as soon as they're ready |
| 89 | private final ArrayList<String> mServicesBinding = new ArrayList<String>(); |
Chris Wren | ab41eec | 2016-01-04 18:01:27 -0500 | [diff] [blame] | 90 | // lists the component names of all enabled (and therefore potentially connected) |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 91 | // app services for current profiles. |
| 92 | private ArraySet<ComponentName> mEnabledServicesForCurrentProfiles |
| 93 | = new ArraySet<ComponentName>(); |
| 94 | // Just the packages from mEnabledServicesForCurrentProfiles |
| 95 | private ArraySet<String> mEnabledServicesPackageNames = new ArraySet<String>(); |
Denis Kuznetsov | 76c0268 | 2015-09-17 19:57:00 +0200 | [diff] [blame] | 96 | // List of packages in restored setting across all mUserProfiles, for quick |
| 97 | // filtering upon package updates. |
| 98 | private ArraySet<String> mRestoredPackages = new ArraySet<>(); |
Ruben Brunk | dd18a0b | 2015-12-04 16:16:31 -0800 | [diff] [blame] | 99 | // State of current service categories |
| 100 | private ArrayMap<String, Boolean> mCategoryEnabled = new ArrayMap<>(); |
Chris Wren | ab41eec | 2016-01-04 18:01:27 -0500 | [diff] [blame] | 101 | // List of enabled packages that have nevertheless asked not to be run |
| 102 | private ArraySet<ComponentName> mSnoozingForCurrentProfiles = new ArraySet<>(); |
Denis Kuznetsov | 76c0268 | 2015-09-17 19:57:00 +0200 | [diff] [blame] | 103 | |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 104 | |
Christoph Studer | b53dfd4 | 2014-09-12 14:45:59 +0200 | [diff] [blame] | 105 | // Kept to de-dupe user change events (experienced after boot, when we receive a settings and a |
| 106 | // user change). |
| 107 | private int[] mLastSeenProfileIds; |
| 108 | |
Christopher Tate | 6597e34 | 2015-02-17 12:15:25 -0800 | [diff] [blame] | 109 | private final BroadcastReceiver mRestoreReceiver; |
| 110 | |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 111 | public ManagedServices(Context context, Handler handler, Object mutex, |
| 112 | UserProfiles userProfiles) { |
| 113 | mContext = context; |
| 114 | mMutex = mutex; |
| 115 | mUserProfiles = userProfiles; |
| 116 | mConfig = getConfig(); |
| 117 | mSettingsObserver = new SettingsObserver(handler); |
Christopher Tate | 6597e34 | 2015-02-17 12:15:25 -0800 | [diff] [blame] | 118 | |
| 119 | mRestoreReceiver = new SettingRestoredReceiver(); |
| 120 | IntentFilter filter = new IntentFilter(Intent.ACTION_SETTING_RESTORED); |
| 121 | context.registerReceiver(mRestoreReceiver, filter); |
Denis Kuznetsov | 76c0268 | 2015-09-17 19:57:00 +0200 | [diff] [blame] | 122 | rebuildRestoredPackages(); |
Christopher Tate | 6597e34 | 2015-02-17 12:15:25 -0800 | [diff] [blame] | 123 | } |
| 124 | |
| 125 | class SettingRestoredReceiver extends BroadcastReceiver { |
| 126 | @Override |
| 127 | public void onReceive(Context context, Intent intent) { |
| 128 | if (Intent.ACTION_SETTING_RESTORED.equals(intent.getAction())) { |
| 129 | String element = intent.getStringExtra(Intent.EXTRA_SETTING_NAME); |
| 130 | if (Objects.equals(element, mConfig.secureSettingName)) { |
| 131 | String prevValue = intent.getStringExtra(Intent.EXTRA_SETTING_PREVIOUS_VALUE); |
| 132 | String newValue = intent.getStringExtra(Intent.EXTRA_SETTING_NEW_VALUE); |
| 133 | settingRestored(element, prevValue, newValue, getSendingUserId()); |
| 134 | } |
| 135 | } |
| 136 | } |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 137 | } |
| 138 | |
| 139 | abstract protected Config getConfig(); |
| 140 | |
| 141 | private String getCaption() { |
| 142 | return mConfig.caption; |
| 143 | } |
| 144 | |
| 145 | abstract protected IInterface asInterface(IBinder binder); |
| 146 | |
Chris Wren | 51017d0 | 2015-12-15 15:34:46 -0500 | [diff] [blame] | 147 | abstract protected boolean checkType(IInterface service); |
| 148 | |
John Spurlock | 3b98b3f | 2014-05-01 09:08:48 -0400 | [diff] [blame] | 149 | abstract protected void onServiceAdded(ManagedServiceInfo info); |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 150 | |
John Spurlock | e77bb36 | 2014-04-26 10:24:59 -0400 | [diff] [blame] | 151 | protected void onServiceRemovedLocked(ManagedServiceInfo removed) { } |
| 152 | |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 153 | private ManagedServiceInfo newServiceInfo(IInterface service, |
| 154 | ComponentName component, int userid, boolean isSystem, ServiceConnection connection, |
| 155 | int targetSdkVersion) { |
| 156 | return new ManagedServiceInfo(service, component, userid, isSystem, connection, |
| 157 | targetSdkVersion); |
| 158 | } |
| 159 | |
| 160 | public void onBootPhaseAppsCanStart() { |
| 161 | mSettingsObserver.observe(); |
| 162 | } |
| 163 | |
John Spurlock | 25e2d24 | 2014-06-27 13:58:23 -0400 | [diff] [blame] | 164 | public void dump(PrintWriter pw, DumpFilter filter) { |
John Spurlock | e77bb36 | 2014-04-26 10:24:59 -0400 | [diff] [blame] | 165 | pw.println(" All " + getCaption() + "s (" + mEnabledServicesForCurrentProfiles.size() |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 166 | + ") enabled for current profiles:"); |
| 167 | for (ComponentName cmpt : mEnabledServicesForCurrentProfiles) { |
John Spurlock | 25e2d24 | 2014-06-27 13:58:23 -0400 | [diff] [blame] | 168 | if (filter != null && !filter.matches(cmpt)) continue; |
John Spurlock | e77bb36 | 2014-04-26 10:24:59 -0400 | [diff] [blame] | 169 | pw.println(" " + cmpt); |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 170 | } |
| 171 | |
John Spurlock | e77bb36 | 2014-04-26 10:24:59 -0400 | [diff] [blame] | 172 | pw.println(" Live " + getCaption() + "s (" + mServices.size() + "):"); |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 173 | for (ManagedServiceInfo info : mServices) { |
John Spurlock | 25e2d24 | 2014-06-27 13:58:23 -0400 | [diff] [blame] | 174 | if (filter != null && !filter.matches(info.component)) continue; |
John Spurlock | e77bb36 | 2014-04-26 10:24:59 -0400 | [diff] [blame] | 175 | pw.println(" " + info.component |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 176 | + " (user " + info.userid + "): " + info.service |
Chris Wren | 51017d0 | 2015-12-15 15:34:46 -0500 | [diff] [blame] | 177 | + (info.isSystem?" SYSTEM":"") |
| 178 | + (info.isGuest(this)?" GUEST":"")); |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 179 | } |
Chris Wren | ab41eec | 2016-01-04 18:01:27 -0500 | [diff] [blame] | 180 | |
| 181 | pw.println(" Snoozed " + getCaption() + "s (" + |
| 182 | mSnoozingForCurrentProfiles.size() + "):"); |
| 183 | for (ComponentName name : mSnoozingForCurrentProfiles) { |
| 184 | pw.println(" " + name.flattenToShortString()); |
| 185 | } |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 186 | } |
| 187 | |
Christopher Tate | 6597e34 | 2015-02-17 12:15:25 -0800 | [diff] [blame] | 188 | // By convention, restored settings are replicated to another settings |
| 189 | // entry, named similarly but with a disambiguation suffix. |
Denis Kuznetsov | 76c0268 | 2015-09-17 19:57:00 +0200 | [diff] [blame] | 190 | public static String restoredSettingName(Config config) { |
Christopher Tate | 6597e34 | 2015-02-17 12:15:25 -0800 | [diff] [blame] | 191 | return config.secureSettingName + ":restored"; |
| 192 | } |
| 193 | |
| 194 | // The OS has done a restore of this service's saved state. We clone it to the |
| 195 | // 'restored' reserve, and then once we return and the actual write to settings is |
| 196 | // performed, our observer will do the work of maintaining the restored vs live |
| 197 | // settings data. |
| 198 | public void settingRestored(String element, String oldValue, String newValue, int userid) { |
| 199 | if (DEBUG) Slog.d(TAG, "Restored managed service setting: " + element |
| 200 | + " ovalue=" + oldValue + " nvalue=" + newValue); |
| 201 | if (mConfig.secureSettingName.equals(element)) { |
| 202 | if (element != null) { |
| 203 | mRestored = null; |
| 204 | Settings.Secure.putStringForUser(mContext.getContentResolver(), |
| 205 | restoredSettingName(mConfig), |
| 206 | newValue, |
| 207 | userid); |
Denis Kuznetsov | 76c0268 | 2015-09-17 19:57:00 +0200 | [diff] [blame] | 208 | updateSettingsAccordingToInstalledServices(userid); |
| 209 | rebuildRestoredPackages(); |
Christopher Tate | 6597e34 | 2015-02-17 12:15:25 -0800 | [diff] [blame] | 210 | } |
| 211 | } |
| 212 | } |
| 213 | |
John Spurlock | 8077493 | 2015-05-07 17:38:50 -0400 | [diff] [blame] | 214 | public boolean isComponentEnabledForPackage(String pkg) { |
| 215 | return mEnabledServicesPackageNames.contains(pkg); |
| 216 | } |
| 217 | |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 218 | public void onPackagesChanged(boolean queryReplace, String[] pkgList) { |
John Spurlock | e77bb36 | 2014-04-26 10:24:59 -0400 | [diff] [blame] | 219 | if (DEBUG) Slog.d(TAG, "onPackagesChanged queryReplace=" + queryReplace |
| 220 | + " pkgList=" + (pkgList == null ? null : Arrays.asList(pkgList)) |
| 221 | + " mEnabledServicesPackageNames=" + mEnabledServicesPackageNames); |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 222 | boolean anyServicesInvolved = false; |
Denis Kuznetsov | 76c0268 | 2015-09-17 19:57:00 +0200 | [diff] [blame] | 223 | |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 224 | if (pkgList != null && (pkgList.length > 0)) { |
| 225 | for (String pkgName : pkgList) { |
Denis Kuznetsov | 76c0268 | 2015-09-17 19:57:00 +0200 | [diff] [blame] | 226 | if (mEnabledServicesPackageNames.contains(pkgName) || |
| 227 | mRestoredPackages.contains(pkgName)) { |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 228 | anyServicesInvolved = true; |
| 229 | } |
| 230 | } |
| 231 | } |
| 232 | |
| 233 | if (anyServicesInvolved) { |
| 234 | // if we're not replacing a package, clean up orphaned bits |
| 235 | if (!queryReplace) { |
Denis Kuznetsov | 76c0268 | 2015-09-17 19:57:00 +0200 | [diff] [blame] | 236 | updateSettingsAccordingToInstalledServices(); |
| 237 | rebuildRestoredPackages(); |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 238 | } |
| 239 | // make sure we're still bound to any of our services who may have just upgraded |
| 240 | rebindServices(); |
| 241 | } |
| 242 | } |
| 243 | |
John Spurlock | 1b8b22b | 2015-05-20 09:47:13 -0400 | [diff] [blame] | 244 | public void onUserSwitched(int user) { |
| 245 | if (DEBUG) Slog.d(TAG, "onUserSwitched u=" + user); |
Denis Kuznetsov | 76c0268 | 2015-09-17 19:57:00 +0200 | [diff] [blame] | 246 | rebuildRestoredPackages(); |
Christoph Studer | b53dfd4 | 2014-09-12 14:45:59 +0200 | [diff] [blame] | 247 | if (Arrays.equals(mLastSeenProfileIds, mUserProfiles.getCurrentProfileIds())) { |
| 248 | if (DEBUG) Slog.d(TAG, "Current profile IDs didn't change, skipping rebindServices()."); |
| 249 | return; |
| 250 | } |
| 251 | rebindServices(); |
| 252 | } |
| 253 | |
Julia Reynolds | a3dcaff | 2016-02-03 15:04:05 -0500 | [diff] [blame] | 254 | public void onUserUnlocked(int user) { |
| 255 | if (DEBUG) Slog.d(TAG, "onUserUnlocked u=" + user); |
| 256 | rebuildRestoredPackages(); |
| 257 | rebindServices(); |
| 258 | } |
| 259 | |
Chris Wren | ab41eec | 2016-01-04 18:01:27 -0500 | [diff] [blame] | 260 | public ManagedServiceInfo getServiceFromTokenLocked(IInterface service) { |
| 261 | if (service == null) { |
| 262 | return null; |
| 263 | } |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 264 | final IBinder token = service.asBinder(); |
| 265 | final int N = mServices.size(); |
Denis Kuznetsov | 76c0268 | 2015-09-17 19:57:00 +0200 | [diff] [blame] | 266 | for (int i = 0; i < N; i++) { |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 267 | final ManagedServiceInfo info = mServices.get(i); |
| 268 | if (info.service.asBinder() == token) return info; |
| 269 | } |
Chris Wren | ab41eec | 2016-01-04 18:01:27 -0500 | [diff] [blame] | 270 | return null; |
| 271 | } |
| 272 | |
| 273 | public ManagedServiceInfo checkServiceTokenLocked(IInterface service) { |
| 274 | checkNotNull(service); |
| 275 | ManagedServiceInfo info = getServiceFromTokenLocked(service); |
| 276 | if (info != null) { |
| 277 | return info; |
| 278 | } |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 279 | throw new SecurityException("Disallowed call from unknown " + getCaption() + ": " |
| 280 | + service); |
| 281 | } |
| 282 | |
| 283 | public void unregisterService(IInterface service, int userid) { |
| 284 | checkNotNull(service); |
| 285 | // no need to check permissions; if your service binder is in the list, |
| 286 | // that's proof that you had permission to add it in the first place |
| 287 | unregisterServiceImpl(service, userid); |
| 288 | } |
| 289 | |
| 290 | public void registerService(IInterface service, ComponentName component, int userid) { |
| 291 | checkNotNull(service); |
Christoph Studer | 3e144d3 | 2014-05-22 16:48:40 +0200 | [diff] [blame] | 292 | ManagedServiceInfo info = registerServiceImpl(service, component, userid); |
| 293 | if (info != null) { |
| 294 | onServiceAdded(info); |
| 295 | } |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 296 | } |
| 297 | |
Chris Wren | 51017d0 | 2015-12-15 15:34:46 -0500 | [diff] [blame] | 298 | /** |
| 299 | * Add a service to our callbacks. The lifecycle of this service is managed externally, |
| 300 | * but unlike a system service, it should not be considered privledged. |
| 301 | * */ |
| 302 | public void registerGuestService(ManagedServiceInfo guest) { |
| 303 | checkNotNull(guest.service); |
| 304 | checkType(guest.service); |
| 305 | if (registerServiceImpl(guest) != null) { |
| 306 | onServiceAdded(guest); |
Chris Wren | ab41eec | 2016-01-04 18:01:27 -0500 | [diff] [blame] | 307 | } |
| 308 | } |
| 309 | |
| 310 | public void setComponentState(ComponentName component, boolean enabled) { |
| 311 | boolean previous = !mSnoozingForCurrentProfiles.contains(component); |
| 312 | if (previous == enabled) { |
| 313 | return; |
| 314 | } |
| 315 | |
| 316 | if (enabled) { |
| 317 | mSnoozingForCurrentProfiles.remove(component); |
| 318 | } else { |
| 319 | mSnoozingForCurrentProfiles.add(component); |
| 320 | } |
| 321 | |
| 322 | // State changed |
| 323 | if (DEBUG) { |
| 324 | Slog.d(TAG, ((enabled) ? "Enabling " : "Disabling ") + "component " + |
| 325 | component.flattenToShortString()); |
| 326 | } |
| 327 | |
| 328 | final int[] userIds = mUserProfiles.getCurrentProfileIds(); |
| 329 | for (int userId : userIds) { |
| 330 | if (enabled) { |
| 331 | registerServiceLocked(component, userId); |
| 332 | } else { |
| 333 | unregisterServiceLocked(component, userId); |
| 334 | } |
Chris Wren | 51017d0 | 2015-12-15 15:34:46 -0500 | [diff] [blame] | 335 | } |
| 336 | } |
| 337 | |
Ruben Brunk | dd18a0b | 2015-12-04 16:16:31 -0800 | [diff] [blame] | 338 | public void setCategoryState(String category, boolean enabled) { |
| 339 | synchronized (mMutex) { |
| 340 | final Boolean previous = mCategoryEnabled.put(category, enabled); |
| 341 | if (!(previous == null || previous != enabled)) { |
| 342 | return; |
| 343 | } |
| 344 | |
| 345 | // State changed |
| 346 | if (DEBUG) { |
| 347 | Slog.d(TAG, ((enabled) ? "Enabling " : "Disabling ") + "category " + category); |
| 348 | } |
| 349 | |
| 350 | final int[] userIds = mUserProfiles.getCurrentProfileIds(); |
| 351 | for (int userId : userIds) { |
| 352 | final Set<ComponentName> componentNames = queryPackageForServices(null, |
| 353 | userId, category); |
| 354 | |
| 355 | // Disallow services not enabled in Settings |
| 356 | final ArraySet<ComponentName> userComponents = |
| 357 | loadComponentNamesFromSetting(mConfig.secureSettingName, userId); |
| 358 | if (userComponents == null) { |
| 359 | componentNames.clear(); |
| 360 | } else { |
| 361 | componentNames.retainAll(userComponents); |
| 362 | } |
| 363 | |
| 364 | if (DEBUG) { |
| 365 | Slog.d(TAG, "Components for category " + category + ": " + componentNames); |
| 366 | } |
| 367 | for (ComponentName c : componentNames) { |
| 368 | if (enabled) { |
| 369 | registerServiceLocked(c, userId); |
| 370 | } else { |
| 371 | unregisterServiceLocked(c, userId); |
| 372 | } |
| 373 | } |
| 374 | } |
| 375 | |
| 376 | } |
| 377 | } |
Denis Kuznetsov | 76c0268 | 2015-09-17 19:57:00 +0200 | [diff] [blame] | 378 | |
| 379 | private void rebuildRestoredPackages() { |
| 380 | mRestoredPackages.clear(); |
Chris Wren | ab41eec | 2016-01-04 18:01:27 -0500 | [diff] [blame] | 381 | mSnoozingForCurrentProfiles.clear(); |
Denis Kuznetsov | 76c0268 | 2015-09-17 19:57:00 +0200 | [diff] [blame] | 382 | String settingName = restoredSettingName(mConfig); |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 383 | int[] userIds = mUserProfiles.getCurrentProfileIds(); |
| 384 | final int N = userIds.length; |
Denis Kuznetsov | 76c0268 | 2015-09-17 19:57:00 +0200 | [diff] [blame] | 385 | for (int i = 0; i < N; ++i) { |
| 386 | ArraySet<ComponentName> names = loadComponentNamesFromSetting(settingName, userIds[i]); |
| 387 | if (names == null) |
| 388 | continue; |
| 389 | for (ComponentName name: names) { |
| 390 | mRestoredPackages.add(name.getPackageName()); |
| 391 | } |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 392 | } |
| 393 | } |
| 394 | |
Denis Kuznetsov | 76c0268 | 2015-09-17 19:57:00 +0200 | [diff] [blame] | 395 | |
Julia Reynolds | c279b99 | 2015-10-30 08:23:51 -0400 | [diff] [blame] | 396 | protected ArraySet<ComponentName> loadComponentNamesFromSetting(String settingName, |
| 397 | int userId) { |
Christopher Tate | 6597e34 | 2015-02-17 12:15:25 -0800 | [diff] [blame] | 398 | final ContentResolver cr = mContext.getContentResolver(); |
Denis Kuznetsov | 76c0268 | 2015-09-17 19:57:00 +0200 | [diff] [blame] | 399 | String settingValue = Settings.Secure.getStringForUser( |
Ruben Brunk | dd18a0b | 2015-12-04 16:16:31 -0800 | [diff] [blame] | 400 | cr, |
| 401 | settingName, |
| 402 | userId); |
Denis Kuznetsov | 76c0268 | 2015-09-17 19:57:00 +0200 | [diff] [blame] | 403 | if (TextUtils.isEmpty(settingValue)) |
| 404 | return null; |
| 405 | String[] restored = settingValue.split(ENABLED_SERVICES_SEPARATOR); |
| 406 | ArraySet<ComponentName> result = new ArraySet<>(restored.length); |
| 407 | for (int i = 0; i < restored.length; i++) { |
| 408 | ComponentName value = ComponentName.unflattenFromString(restored[i]); |
| 409 | if (null != value) { |
| 410 | result.add(value); |
Christopher Tate | 6597e34 | 2015-02-17 12:15:25 -0800 | [diff] [blame] | 411 | } |
| 412 | } |
Denis Kuznetsov | 76c0268 | 2015-09-17 19:57:00 +0200 | [diff] [blame] | 413 | return result; |
| 414 | } |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 415 | |
Denis Kuznetsov | 76c0268 | 2015-09-17 19:57:00 +0200 | [diff] [blame] | 416 | private void storeComponentsToSetting(Set<ComponentName> components, |
| 417 | String settingName, |
| 418 | int userId) { |
| 419 | String[] componentNames = null; |
| 420 | if (null != components) { |
| 421 | componentNames = new String[components.size()]; |
| 422 | int index = 0; |
| 423 | for (ComponentName c: components) { |
| 424 | componentNames[index++] = c.flattenToString(); |
| 425 | } |
| 426 | } |
| 427 | final String value = (componentNames == null) ? "" : |
| 428 | TextUtils.join(ENABLED_SERVICES_SEPARATOR, componentNames); |
| 429 | final ContentResolver cr = mContext.getContentResolver(); |
| 430 | Settings.Secure.putStringForUser( |
Ruben Brunk | dd18a0b | 2015-12-04 16:16:31 -0800 | [diff] [blame] | 431 | cr, |
| 432 | settingName, |
| 433 | value, |
| 434 | userId); |
Denis Kuznetsov | 76c0268 | 2015-09-17 19:57:00 +0200 | [diff] [blame] | 435 | } |
| 436 | |
Denis Kuznetsov | 76c0268 | 2015-09-17 19:57:00 +0200 | [diff] [blame] | 437 | /** |
| 438 | * Remove access for any services that no longer exist. |
| 439 | */ |
| 440 | private void updateSettingsAccordingToInstalledServices() { |
| 441 | int[] userIds = mUserProfiles.getCurrentProfileIds(); |
| 442 | final int N = userIds.length; |
| 443 | for (int i = 0; i < N; ++i) { |
| 444 | updateSettingsAccordingToInstalledServices(userIds[i]); |
| 445 | } |
| 446 | rebuildRestoredPackages(); |
| 447 | } |
| 448 | |
Julia Reynolds | c279b99 | 2015-10-30 08:23:51 -0400 | [diff] [blame] | 449 | protected Set<ComponentName> queryPackageForServices(String packageName, int userId) { |
Ruben Brunk | dd18a0b | 2015-12-04 16:16:31 -0800 | [diff] [blame] | 450 | return queryPackageForServices(packageName, userId, null); |
| 451 | } |
| 452 | |
| 453 | protected Set<ComponentName> queryPackageForServices(String packageName, int userId, |
| 454 | String category) { |
Denis Kuznetsov | 76c0268 | 2015-09-17 19:57:00 +0200 | [diff] [blame] | 455 | Set<ComponentName> installed = new ArraySet<>(); |
Denis Kuznetsov | 76c0268 | 2015-09-17 19:57:00 +0200 | [diff] [blame] | 456 | final PackageManager pm = mContext.getPackageManager(); |
Julia Reynolds | c279b99 | 2015-10-30 08:23:51 -0400 | [diff] [blame] | 457 | Intent queryIntent = new Intent(mConfig.serviceInterface); |
| 458 | if (!TextUtils.isEmpty(packageName)) { |
| 459 | queryIntent.setPackage(packageName); |
| 460 | } |
Ruben Brunk | dd18a0b | 2015-12-04 16:16:31 -0800 | [diff] [blame] | 461 | if (category != null) { |
| 462 | queryIntent.addCategory(category); |
| 463 | } |
Denis Kuznetsov | 76c0268 | 2015-09-17 19:57:00 +0200 | [diff] [blame] | 464 | List<ResolveInfo> installedServices = pm.queryIntentServicesAsUser( |
Julia Reynolds | c279b99 | 2015-10-30 08:23:51 -0400 | [diff] [blame] | 465 | queryIntent, |
Denis Kuznetsov | 76c0268 | 2015-09-17 19:57:00 +0200 | [diff] [blame] | 466 | PackageManager.GET_SERVICES | PackageManager.GET_META_DATA, |
| 467 | userId); |
| 468 | if (DEBUG) |
| 469 | Slog.v(TAG, mConfig.serviceInterface + " services: " + installedServices); |
Julia Reynolds | 50f0514 | 2015-10-30 17:03:59 -0400 | [diff] [blame] | 470 | if (installedServices != null) { |
| 471 | for (int i = 0, count = installedServices.size(); i < count; i++) { |
| 472 | ResolveInfo resolveInfo = installedServices.get(i); |
| 473 | ServiceInfo info = resolveInfo.serviceInfo; |
Denis Kuznetsov | 76c0268 | 2015-09-17 19:57:00 +0200 | [diff] [blame] | 474 | |
Julia Reynolds | 50f0514 | 2015-10-30 17:03:59 -0400 | [diff] [blame] | 475 | ComponentName component = new ComponentName(info.packageName, info.name); |
| 476 | if (!mConfig.bindPermission.equals(info.permission)) { |
| 477 | Slog.w(TAG, "Skipping " + getCaption() + " service " |
Ruben Brunk | dd18a0b | 2015-12-04 16:16:31 -0800 | [diff] [blame] | 478 | + info.packageName + "/" + info.name |
| 479 | + ": it does not require the permission " |
| 480 | + mConfig.bindPermission); |
Julia Reynolds | 50f0514 | 2015-10-30 17:03:59 -0400 | [diff] [blame] | 481 | continue; |
| 482 | } |
| 483 | installed.add(component); |
Denis Kuznetsov | 76c0268 | 2015-09-17 19:57:00 +0200 | [diff] [blame] | 484 | } |
Denis Kuznetsov | 76c0268 | 2015-09-17 19:57:00 +0200 | [diff] [blame] | 485 | } |
Julia Reynolds | c279b99 | 2015-10-30 08:23:51 -0400 | [diff] [blame] | 486 | return installed; |
| 487 | } |
| 488 | |
| 489 | private void updateSettingsAccordingToInstalledServices(int userId) { |
| 490 | boolean restoredChanged = false; |
| 491 | boolean currentChanged = false; |
| 492 | Set<ComponentName> restored = |
| 493 | loadComponentNamesFromSetting(restoredSettingName(mConfig), userId); |
| 494 | Set<ComponentName> current = |
| 495 | loadComponentNamesFromSetting(mConfig.secureSettingName, userId); |
| 496 | // Load all services for all packages. |
| 497 | Set<ComponentName> installed = queryPackageForServices(null, userId); |
Denis Kuznetsov | 76c0268 | 2015-09-17 19:57:00 +0200 | [diff] [blame] | 498 | |
| 499 | ArraySet<ComponentName> retained = new ArraySet<>(); |
| 500 | |
| 501 | for (ComponentName component : installed) { |
| 502 | if (null != restored) { |
| 503 | boolean wasRestored = restored.remove(component); |
| 504 | if (wasRestored) { |
| 505 | // Freshly installed package has service that was mentioned in restored setting. |
| 506 | if (DEBUG) |
| 507 | Slog.v(TAG, "Restoring " + component + " for user " + userId); |
| 508 | restoredChanged = true; |
| 509 | currentChanged = true; |
| 510 | retained.add(component); |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 511 | continue; |
| 512 | } |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 513 | } |
| 514 | |
Denis Kuznetsov | 76c0268 | 2015-09-17 19:57:00 +0200 | [diff] [blame] | 515 | if (null != current) { |
| 516 | if (current.contains(component)) |
| 517 | retained.add(component); |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 518 | } |
Denis Kuznetsov | 76c0268 | 2015-09-17 19:57:00 +0200 | [diff] [blame] | 519 | } |
| 520 | |
| 521 | currentChanged |= ((current == null ? 0 : current.size()) != retained.size()); |
| 522 | |
| 523 | if (currentChanged) { |
| 524 | if (DEBUG) Slog.v(TAG, "List of " + getCaption() + " services was updated " + current); |
| 525 | storeComponentsToSetting(retained, mConfig.secureSettingName, userId); |
| 526 | } |
| 527 | |
| 528 | if (restoredChanged) { |
| 529 | if (DEBUG) Slog.v(TAG, |
| 530 | "List of " + getCaption() + " restored services was updated " + restored); |
| 531 | storeComponentsToSetting(restored, restoredSettingName(mConfig), userId); |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 532 | } |
| 533 | } |
| 534 | |
| 535 | /** |
| 536 | * Called whenever packages change, the user switches, or the secure setting |
| 537 | * is altered. (For example in response to USER_SWITCHED in our broadcast receiver) |
| 538 | */ |
| 539 | private void rebindServices() { |
| 540 | if (DEBUG) Slog.d(TAG, "rebindServices"); |
| 541 | final int[] userIds = mUserProfiles.getCurrentProfileIds(); |
| 542 | final int nUserIds = userIds.length; |
| 543 | |
Denis Kuznetsov | 76c0268 | 2015-09-17 19:57:00 +0200 | [diff] [blame] | 544 | final SparseArray<ArraySet<ComponentName>> componentsByUser = new SparseArray<>(); |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 545 | |
| 546 | for (int i = 0; i < nUserIds; ++i) { |
Denis Kuznetsov | 76c0268 | 2015-09-17 19:57:00 +0200 | [diff] [blame] | 547 | componentsByUser.put(userIds[i], |
| 548 | loadComponentNamesFromSetting(mConfig.secureSettingName, userIds[i])); |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 549 | } |
| 550 | |
Julia Reynolds | 9a86cc0 | 2016-02-10 15:38:15 -0500 | [diff] [blame^] | 551 | final ArrayList<ManagedServiceInfo> removableBoundServices = new ArrayList<>(); |
| 552 | final SparseArray<Set<ComponentName>> toAdd = new SparseArray<>(); |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 553 | |
| 554 | synchronized (mMutex) { |
Julia Reynolds | 9a86cc0 | 2016-02-10 15:38:15 -0500 | [diff] [blame^] | 555 | // Potentially unbind automatically bound services, retain system services. |
Christoph Studer | 5d42384 | 2014-05-23 13:15:54 +0200 | [diff] [blame] | 556 | for (ManagedServiceInfo service : mServices) { |
Chris Wren | 51017d0 | 2015-12-15 15:34:46 -0500 | [diff] [blame] | 557 | if (!service.isSystem && !service.isGuest(this)) { |
Julia Reynolds | 9a86cc0 | 2016-02-10 15:38:15 -0500 | [diff] [blame^] | 558 | removableBoundServices.add(service); |
Christoph Studer | 5d42384 | 2014-05-23 13:15:54 +0200 | [diff] [blame] | 559 | } |
| 560 | } |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 561 | |
Denis Kuznetsov | 76c0268 | 2015-09-17 19:57:00 +0200 | [diff] [blame] | 562 | final ArraySet<ComponentName> newEnabled = new ArraySet<>(); |
| 563 | final ArraySet<String> newPackages = new ArraySet<>(); |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 564 | |
| 565 | for (int i = 0; i < nUserIds; ++i) { |
Denis Kuznetsov | 76c0268 | 2015-09-17 19:57:00 +0200 | [diff] [blame] | 566 | // decode the list of components |
| 567 | final ArraySet<ComponentName> userComponents = componentsByUser.get(userIds[i]); |
| 568 | if (null == userComponents) { |
Julia Reynolds | 9a86cc0 | 2016-02-10 15:38:15 -0500 | [diff] [blame^] | 569 | toAdd.put(userIds[i], new HashSet<ComponentName>()); |
Denis Kuznetsov | 76c0268 | 2015-09-17 19:57:00 +0200 | [diff] [blame] | 570 | continue; |
| 571 | } |
| 572 | |
Julia Reynolds | 9a86cc0 | 2016-02-10 15:38:15 -0500 | [diff] [blame^] | 573 | final Set<ComponentName> add = new HashSet<>(userComponents); |
Ruben Brunk | dd18a0b | 2015-12-04 16:16:31 -0800 | [diff] [blame] | 574 | |
| 575 | // Remove components from disabled categories so that those services aren't run. |
| 576 | for (Entry<String, Boolean> e : mCategoryEnabled.entrySet()) { |
| 577 | if (!e.getValue()) { |
| 578 | Set<ComponentName> c = queryPackageForServices(null, userIds[i], |
| 579 | e.getKey()); |
| 580 | add.removeAll(c); |
| 581 | } |
| 582 | } |
Chris Wren | ab41eec | 2016-01-04 18:01:27 -0500 | [diff] [blame] | 583 | add.removeAll(mSnoozingForCurrentProfiles); |
Ruben Brunk | dd18a0b | 2015-12-04 16:16:31 -0800 | [diff] [blame] | 584 | |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 585 | toAdd.put(userIds[i], add); |
| 586 | |
Denis Kuznetsov | 76c0268 | 2015-09-17 19:57:00 +0200 | [diff] [blame] | 587 | newEnabled.addAll(userComponents); |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 588 | |
Denis Kuznetsov | 76c0268 | 2015-09-17 19:57:00 +0200 | [diff] [blame] | 589 | for (int j = 0; j < userComponents.size(); j++) { |
Chris Wren | 083094c | 2015-12-15 16:25:07 -0500 | [diff] [blame] | 590 | final ComponentName component = userComponents.valueAt(j); |
Denis Kuznetsov | 76c0268 | 2015-09-17 19:57:00 +0200 | [diff] [blame] | 591 | newPackages.add(component.getPackageName()); |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 592 | } |
| 593 | } |
| 594 | mEnabledServicesForCurrentProfiles = newEnabled; |
| 595 | mEnabledServicesPackageNames = newPackages; |
| 596 | } |
| 597 | |
Julia Reynolds | 9a86cc0 | 2016-02-10 15:38:15 -0500 | [diff] [blame^] | 598 | for (ManagedServiceInfo info : removableBoundServices) { |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 599 | final ComponentName component = info.component; |
| 600 | final int oldUser = info.userid; |
Julia Reynolds | 9a86cc0 | 2016-02-10 15:38:15 -0500 | [diff] [blame^] | 601 | final Set<ComponentName> allowedComponents = toAdd.get(info.userid); |
| 602 | if (allowedComponents != null) { |
| 603 | if (allowedComponents.contains(component)) { |
| 604 | // Already bound, don't need to bind again. |
| 605 | allowedComponents.remove(component); |
| 606 | } else { |
| 607 | // No longer allowed to be bound. |
| 608 | Slog.v(TAG, "disabling " + getCaption() + " for user " |
| 609 | + oldUser + ": " + component); |
| 610 | unregisterService(component, oldUser); |
| 611 | } |
| 612 | } |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 613 | } |
| 614 | |
| 615 | for (int i = 0; i < nUserIds; ++i) { |
Julia Reynolds | 9a86cc0 | 2016-02-10 15:38:15 -0500 | [diff] [blame^] | 616 | final Set<ComponentName> add = toAdd.get(userIds[i]); |
| 617 | for (ComponentName component : add) { |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 618 | Slog.v(TAG, "enabling " + getCaption() + " for user " + userIds[i] + ": " |
| 619 | + component); |
| 620 | registerService(component, userIds[i]); |
| 621 | } |
| 622 | } |
Christoph Studer | b53dfd4 | 2014-09-12 14:45:59 +0200 | [diff] [blame] | 623 | |
Denis Kuznetsov | 76c0268 | 2015-09-17 19:57:00 +0200 | [diff] [blame] | 624 | mLastSeenProfileIds = userIds; |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 625 | } |
| 626 | |
| 627 | /** |
| 628 | * Version of registerService that takes the name of a service component to bind to. |
| 629 | */ |
| 630 | private void registerService(final ComponentName name, final int userid) { |
Ruben Brunk | dd18a0b | 2015-12-04 16:16:31 -0800 | [diff] [blame] | 631 | synchronized (mMutex) { |
| 632 | registerServiceLocked(name, userid); |
| 633 | } |
| 634 | } |
| 635 | |
| 636 | private void registerServiceLocked(final ComponentName name, final int userid) { |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 637 | if (DEBUG) Slog.v(TAG, "registerService: " + name + " u=" + userid); |
| 638 | |
Ruben Brunk | dd18a0b | 2015-12-04 16:16:31 -0800 | [diff] [blame] | 639 | final String servicesBindingTag = name.toString() + "/" + userid; |
| 640 | if (mServicesBinding.contains(servicesBindingTag)) { |
| 641 | // stop registering this thing already! we're working on it |
| 642 | return; |
| 643 | } |
| 644 | mServicesBinding.add(servicesBindingTag); |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 645 | |
Ruben Brunk | dd18a0b | 2015-12-04 16:16:31 -0800 | [diff] [blame] | 646 | final int N = mServices.size(); |
| 647 | for (int i = N - 1; i >= 0; i--) { |
| 648 | final ManagedServiceInfo info = mServices.get(i); |
| 649 | if (name.equals(info.component) |
| 650 | && info.userid == userid) { |
| 651 | // cut old connections |
| 652 | if (DEBUG) Slog.v(TAG, " disconnecting old " + getCaption() + ": " |
| 653 | + info.service); |
| 654 | removeServiceLocked(i); |
| 655 | if (info.connection != null) { |
| 656 | mContext.unbindService(info.connection); |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 657 | } |
| 658 | } |
Ruben Brunk | dd18a0b | 2015-12-04 16:16:31 -0800 | [diff] [blame] | 659 | } |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 660 | |
Ruben Brunk | dd18a0b | 2015-12-04 16:16:31 -0800 | [diff] [blame] | 661 | Intent intent = new Intent(mConfig.serviceInterface); |
| 662 | intent.setComponent(name); |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 663 | |
Ruben Brunk | dd18a0b | 2015-12-04 16:16:31 -0800 | [diff] [blame] | 664 | intent.putExtra(Intent.EXTRA_CLIENT_LABEL, mConfig.clientLabel); |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 665 | |
Ruben Brunk | dd18a0b | 2015-12-04 16:16:31 -0800 | [diff] [blame] | 666 | final PendingIntent pendingIntent = PendingIntent.getActivity( |
| 667 | mContext, 0, new Intent(mConfig.settingsAction), 0); |
| 668 | intent.putExtra(Intent.EXTRA_CLIENT_INTENT, pendingIntent); |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 669 | |
Ruben Brunk | dd18a0b | 2015-12-04 16:16:31 -0800 | [diff] [blame] | 670 | ApplicationInfo appInfo = null; |
| 671 | try { |
| 672 | appInfo = mContext.getPackageManager().getApplicationInfo( |
| 673 | name.getPackageName(), 0); |
| 674 | } catch (NameNotFoundException e) { |
| 675 | // Ignore if the package doesn't exist we won't be able to bind to the service. |
| 676 | } |
| 677 | final int targetSdkVersion = |
| 678 | appInfo != null ? appInfo.targetSdkVersion : Build.VERSION_CODES.BASE; |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 679 | |
Ruben Brunk | dd18a0b | 2015-12-04 16:16:31 -0800 | [diff] [blame] | 680 | try { |
| 681 | if (DEBUG) Slog.v(TAG, "binding: " + intent); |
| 682 | ServiceConnection serviceConnection = new ServiceConnection() { |
| 683 | IInterface mService; |
Denis Kuznetsov | 76c0268 | 2015-09-17 19:57:00 +0200 | [diff] [blame] | 684 | |
Ruben Brunk | dd18a0b | 2015-12-04 16:16:31 -0800 | [diff] [blame] | 685 | @Override |
| 686 | public void onServiceConnected(ComponentName name, IBinder binder) { |
| 687 | boolean added = false; |
| 688 | ManagedServiceInfo info = null; |
| 689 | synchronized (mMutex) { |
| 690 | mServicesBinding.remove(servicesBindingTag); |
| 691 | try { |
| 692 | mService = asInterface(binder); |
| 693 | info = newServiceInfo(mService, name, |
| 694 | userid, false /*isSystem*/, this, targetSdkVersion); |
| 695 | binder.linkToDeath(info, 0); |
| 696 | added = mServices.add(info); |
| 697 | } catch (RemoteException e) { |
| 698 | // already dead |
Denis Kuznetsov | 76c0268 | 2015-09-17 19:57:00 +0200 | [diff] [blame] | 699 | } |
| 700 | } |
Ruben Brunk | dd18a0b | 2015-12-04 16:16:31 -0800 | [diff] [blame] | 701 | if (added) { |
| 702 | onServiceAdded(info); |
Denis Kuznetsov | 76c0268 | 2015-09-17 19:57:00 +0200 | [diff] [blame] | 703 | } |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 704 | } |
Ruben Brunk | dd18a0b | 2015-12-04 16:16:31 -0800 | [diff] [blame] | 705 | |
| 706 | @Override |
| 707 | public void onServiceDisconnected(ComponentName name) { |
| 708 | Slog.v(TAG, getCaption() + " connection lost: " + name); |
| 709 | } |
| 710 | }; |
| 711 | if (!mContext.bindServiceAsUser(intent, |
| 712 | serviceConnection, |
| 713 | Context.BIND_AUTO_CREATE | Context.BIND_FOREGROUND_SERVICE, |
| 714 | new UserHandle(userid))) { |
| 715 | mServicesBinding.remove(servicesBindingTag); |
| 716 | Slog.w(TAG, "Unable to bind " + getCaption() + " service: " + intent); |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 717 | return; |
| 718 | } |
Ruben Brunk | dd18a0b | 2015-12-04 16:16:31 -0800 | [diff] [blame] | 719 | } catch (SecurityException ex) { |
| 720 | Slog.e(TAG, "Unable to bind " + getCaption() + " service: " + intent, ex); |
| 721 | return; |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 722 | } |
| 723 | } |
| 724 | |
| 725 | /** |
| 726 | * Remove a service for the given user by ComponentName |
| 727 | */ |
| 728 | private void unregisterService(ComponentName name, int userid) { |
| 729 | synchronized (mMutex) { |
Ruben Brunk | dd18a0b | 2015-12-04 16:16:31 -0800 | [diff] [blame] | 730 | unregisterServiceLocked(name, userid); |
| 731 | } |
| 732 | } |
| 733 | |
| 734 | private void unregisterServiceLocked(ComponentName name, int userid) { |
| 735 | final int N = mServices.size(); |
| 736 | for (int i = N - 1; i >= 0; i--) { |
| 737 | final ManagedServiceInfo info = mServices.get(i); |
| 738 | if (name.equals(info.component) |
| 739 | && info.userid == userid) { |
| 740 | removeServiceLocked(i); |
| 741 | if (info.connection != null) { |
| 742 | try { |
| 743 | mContext.unbindService(info.connection); |
| 744 | } catch (IllegalArgumentException ex) { |
| 745 | // something happened to the service: we think we have a connection |
| 746 | // but it's bogus. |
| 747 | Slog.e(TAG, getCaption() + " " + name + " could not be unbound: " + ex); |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 748 | } |
| 749 | } |
| 750 | } |
| 751 | } |
| 752 | } |
| 753 | |
| 754 | /** |
| 755 | * Removes a service from the list but does not unbind |
| 756 | * |
| 757 | * @return the removed service. |
| 758 | */ |
| 759 | private ManagedServiceInfo removeServiceImpl(IInterface service, final int userid) { |
John Spurlock | e77bb36 | 2014-04-26 10:24:59 -0400 | [diff] [blame] | 760 | if (DEBUG) Slog.d(TAG, "removeServiceImpl service=" + service + " u=" + userid); |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 761 | ManagedServiceInfo serviceInfo = null; |
| 762 | synchronized (mMutex) { |
| 763 | final int N = mServices.size(); |
Denis Kuznetsov | 76c0268 | 2015-09-17 19:57:00 +0200 | [diff] [blame] | 764 | for (int i = N - 1; i >= 0; i--) { |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 765 | final ManagedServiceInfo info = mServices.get(i); |
| 766 | if (info.service.asBinder() == service.asBinder() |
| 767 | && info.userid == userid) { |
John Spurlock | e77bb36 | 2014-04-26 10:24:59 -0400 | [diff] [blame] | 768 | if (DEBUG) Slog.d(TAG, "Removing active service " + info.component); |
| 769 | serviceInfo = removeServiceLocked(i); |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 770 | } |
| 771 | } |
| 772 | } |
| 773 | return serviceInfo; |
| 774 | } |
| 775 | |
John Spurlock | e77bb36 | 2014-04-26 10:24:59 -0400 | [diff] [blame] | 776 | private ManagedServiceInfo removeServiceLocked(int i) { |
| 777 | final ManagedServiceInfo info = mServices.remove(i); |
| 778 | onServiceRemovedLocked(info); |
| 779 | return info; |
| 780 | } |
| 781 | |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 782 | private void checkNotNull(IInterface service) { |
| 783 | if (service == null) { |
| 784 | throw new IllegalArgumentException(getCaption() + " must not be null"); |
| 785 | } |
| 786 | } |
| 787 | |
Christoph Studer | 3e144d3 | 2014-05-22 16:48:40 +0200 | [diff] [blame] | 788 | private ManagedServiceInfo registerServiceImpl(final IInterface service, |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 789 | final ComponentName component, final int userid) { |
Chris Wren | 51017d0 | 2015-12-15 15:34:46 -0500 | [diff] [blame] | 790 | ManagedServiceInfo info = newServiceInfo(service, component, userid, |
| 791 | true /*isSystem*/, null /*connection*/, Build.VERSION_CODES.LOLLIPOP); |
| 792 | return registerServiceImpl(info); |
| 793 | } |
| 794 | |
| 795 | private ManagedServiceInfo registerServiceImpl(ManagedServiceInfo info) { |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 796 | synchronized (mMutex) { |
| 797 | try { |
Chris Wren | 51017d0 | 2015-12-15 15:34:46 -0500 | [diff] [blame] | 798 | info.service.asBinder().linkToDeath(info, 0); |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 799 | mServices.add(info); |
Christoph Studer | 3e144d3 | 2014-05-22 16:48:40 +0200 | [diff] [blame] | 800 | return info; |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 801 | } catch (RemoteException e) { |
| 802 | // already dead |
| 803 | } |
| 804 | } |
Christoph Studer | 3e144d3 | 2014-05-22 16:48:40 +0200 | [diff] [blame] | 805 | return null; |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 806 | } |
| 807 | |
| 808 | /** |
| 809 | * Removes a service from the list and unbinds. |
| 810 | */ |
| 811 | private void unregisterServiceImpl(IInterface service, int userid) { |
| 812 | ManagedServiceInfo info = removeServiceImpl(service, userid); |
Chris Wren | 51017d0 | 2015-12-15 15:34:46 -0500 | [diff] [blame] | 813 | if (info != null && info.connection != null && !info.isGuest(this)) { |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 814 | mContext.unbindService(info.connection); |
| 815 | } |
| 816 | } |
| 817 | |
| 818 | private class SettingsObserver extends ContentObserver { |
| 819 | private final Uri mSecureSettingsUri = Settings.Secure.getUriFor(mConfig.secureSettingName); |
| 820 | |
| 821 | private SettingsObserver(Handler handler) { |
| 822 | super(handler); |
| 823 | } |
| 824 | |
| 825 | private void observe() { |
| 826 | ContentResolver resolver = mContext.getContentResolver(); |
| 827 | resolver.registerContentObserver(mSecureSettingsUri, |
| 828 | false, this, UserHandle.USER_ALL); |
| 829 | update(null); |
| 830 | } |
| 831 | |
| 832 | @Override |
| 833 | public void onChange(boolean selfChange, Uri uri) { |
| 834 | update(uri); |
| 835 | } |
| 836 | |
| 837 | private void update(Uri uri) { |
| 838 | if (uri == null || mSecureSettingsUri.equals(uri)) { |
Christoph Studer | b53dfd4 | 2014-09-12 14:45:59 +0200 | [diff] [blame] | 839 | if (DEBUG) Slog.d(TAG, "Setting changed: mSecureSettingsUri=" + mSecureSettingsUri + |
| 840 | " / uri=" + uri); |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 841 | rebindServices(); |
Denis Kuznetsov | 76c0268 | 2015-09-17 19:57:00 +0200 | [diff] [blame] | 842 | rebuildRestoredPackages(); |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 843 | } |
| 844 | } |
| 845 | } |
| 846 | |
| 847 | public class ManagedServiceInfo implements IBinder.DeathRecipient { |
| 848 | public IInterface service; |
| 849 | public ComponentName component; |
| 850 | public int userid; |
| 851 | public boolean isSystem; |
| 852 | public ServiceConnection connection; |
| 853 | public int targetSdkVersion; |
| 854 | |
| 855 | public ManagedServiceInfo(IInterface service, ComponentName component, |
| 856 | int userid, boolean isSystem, ServiceConnection connection, int targetSdkVersion) { |
| 857 | this.service = service; |
| 858 | this.component = component; |
| 859 | this.userid = userid; |
| 860 | this.isSystem = isSystem; |
| 861 | this.connection = connection; |
| 862 | this.targetSdkVersion = targetSdkVersion; |
| 863 | } |
| 864 | |
Chris Wren | 51017d0 | 2015-12-15 15:34:46 -0500 | [diff] [blame] | 865 | public boolean isGuest(ManagedServices host) { |
| 866 | return ManagedServices.this != host; |
| 867 | } |
| 868 | |
Chris Wren | ab41eec | 2016-01-04 18:01:27 -0500 | [diff] [blame] | 869 | public ManagedServices getOwner() { |
| 870 | return ManagedServices.this; |
| 871 | } |
| 872 | |
John Spurlock | e77bb36 | 2014-04-26 10:24:59 -0400 | [diff] [blame] | 873 | @Override |
| 874 | public String toString() { |
| 875 | return new StringBuilder("ManagedServiceInfo[") |
| 876 | .append("component=").append(component) |
| 877 | .append(",userid=").append(userid) |
| 878 | .append(",isSystem=").append(isSystem) |
| 879 | .append(",targetSdkVersion=").append(targetSdkVersion) |
| 880 | .append(",connection=").append(connection == null ? null : "<connection>") |
| 881 | .append(",service=").append(service) |
| 882 | .append(']').toString(); |
| 883 | } |
| 884 | |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 885 | public boolean enabledAndUserMatches(int nid) { |
| 886 | if (!isEnabledForCurrentProfiles()) { |
| 887 | return false; |
| 888 | } |
| 889 | if (this.userid == UserHandle.USER_ALL) return true; |
| 890 | if (nid == UserHandle.USER_ALL || nid == this.userid) return true; |
| 891 | return supportsProfiles() && mUserProfiles.isCurrentProfile(nid); |
| 892 | } |
| 893 | |
| 894 | public boolean supportsProfiles() { |
Dianne Hackborn | 955d8d6 | 2014-10-07 20:17:19 -0700 | [diff] [blame] | 895 | return targetSdkVersion >= Build.VERSION_CODES.LOLLIPOP; |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 896 | } |
| 897 | |
| 898 | @Override |
| 899 | public void binderDied() { |
John Spurlock | e77bb36 | 2014-04-26 10:24:59 -0400 | [diff] [blame] | 900 | if (DEBUG) Slog.d(TAG, "binderDied"); |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 901 | // Remove the service, but don't unbind from the service. The system will bring the |
| 902 | // service back up, and the onServiceConnected handler will readd the service with the |
| 903 | // new binding. If this isn't a bound service, and is just a registered |
| 904 | // service, just removing it from the list is all we need to do anyway. |
| 905 | removeServiceImpl(this.service, this.userid); |
| 906 | } |
| 907 | |
| 908 | /** convenience method for looking in mEnabledServicesForCurrentProfiles */ |
| 909 | public boolean isEnabledForCurrentProfiles() { |
| 910 | if (this.isSystem) return true; |
| 911 | if (this.connection == null) return false; |
| 912 | return mEnabledServicesForCurrentProfiles.contains(this.component); |
| 913 | } |
| 914 | } |
| 915 | |
Chris Wren | ab41eec | 2016-01-04 18:01:27 -0500 | [diff] [blame] | 916 | /** convenience method for looking in mEnabledServicesForCurrentProfiles */ |
| 917 | public boolean isComponentEnabledForCurrentProfiles(ComponentName component) { |
| 918 | return mEnabledServicesForCurrentProfiles.contains(component); |
| 919 | } |
| 920 | |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 921 | public static class UserProfiles { |
| 922 | // Profiles of the current user. |
| 923 | private final SparseArray<UserInfo> mCurrentProfiles = new SparseArray<UserInfo>(); |
| 924 | |
| 925 | public void updateCache(Context context) { |
| 926 | UserManager userManager = (UserManager) context.getSystemService(Context.USER_SERVICE); |
| 927 | if (userManager != null) { |
| 928 | int currentUserId = ActivityManager.getCurrentUser(); |
| 929 | List<UserInfo> profiles = userManager.getProfiles(currentUserId); |
| 930 | synchronized (mCurrentProfiles) { |
| 931 | mCurrentProfiles.clear(); |
| 932 | for (UserInfo user : profiles) { |
| 933 | mCurrentProfiles.put(user.id, user); |
| 934 | } |
| 935 | } |
| 936 | } |
| 937 | } |
| 938 | |
| 939 | public int[] getCurrentProfileIds() { |
| 940 | synchronized (mCurrentProfiles) { |
| 941 | int[] users = new int[mCurrentProfiles.size()]; |
| 942 | final int N = mCurrentProfiles.size(); |
| 943 | for (int i = 0; i < N; ++i) { |
| 944 | users[i] = mCurrentProfiles.keyAt(i); |
| 945 | } |
| 946 | return users; |
| 947 | } |
| 948 | } |
| 949 | |
| 950 | public boolean isCurrentProfile(int userId) { |
| 951 | synchronized (mCurrentProfiles) { |
| 952 | return mCurrentProfiles.get(userId) != null; |
| 953 | } |
| 954 | } |
| 955 | } |
| 956 | |
| 957 | protected static class Config { |
| 958 | String caption; |
| 959 | String serviceInterface; |
| 960 | String secureSettingName; |
| 961 | String bindPermission; |
| 962 | String settingsAction; |
| 963 | int clientLabel; |
| 964 | } |
| 965 | } |