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