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