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