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