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