blob: 14e2ba3896d8a419548264ed128e8f1a9e33b8a1 [file] [log] [blame]
John Spurlock7340fc82014-04-24 18:50:12 -04001/**
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
17package com.android.server.notification;
18
Felipe Lemea1b79bf2016-05-24 13:06:54 -070019import static android.content.Context.BIND_ALLOW_WHITELIST_MANAGEMENT;
20import static android.content.Context.BIND_AUTO_CREATE;
21import static android.content.Context.BIND_FOREGROUND_SERVICE;
22
Ruben Brunke24b9a62016-02-16 21:38:24 -080023import android.annotation.NonNull;
John Spurlock7340fc82014-04-24 18:50:12 -040024import android.app.ActivityManager;
25import android.app.PendingIntent;
Christopher Tate6597e342015-02-17 12:15:25 -080026import android.content.BroadcastReceiver;
John Spurlock7340fc82014-04-24 18:50:12 -040027import android.content.ComponentName;
28import android.content.ContentResolver;
29import android.content.Context;
30import android.content.Intent;
Christopher Tate6597e342015-02-17 12:15:25 -080031import android.content.IntentFilter;
John Spurlock7340fc82014-04-24 18:50:12 -040032import android.content.ServiceConnection;
33import android.content.pm.ApplicationInfo;
34import android.content.pm.PackageManager;
35import android.content.pm.PackageManager.NameNotFoundException;
36import android.content.pm.ResolveInfo;
37import android.content.pm.ServiceInfo;
38import android.content.pm.UserInfo;
39import android.database.ContentObserver;
40import android.net.Uri;
41import android.os.Build;
42import android.os.Handler;
43import android.os.IBinder;
44import android.os.IInterface;
45import android.os.RemoteException;
46import android.os.UserHandle;
47import android.os.UserManager;
48import android.provider.Settings;
49import android.text.TextUtils;
50import android.util.ArraySet;
John Spurlock4db0d982014-08-13 09:19:03 -040051import android.util.Log;
John Spurlock7340fc82014-04-24 18:50:12 -040052import android.util.Slog;
53import android.util.SparseArray;
54
John Spurlock25e2d242014-06-27 13:58:23 -040055import com.android.server.notification.NotificationManagerService.DumpFilter;
56
John Spurlock7340fc82014-04-24 18:50:12 -040057import java.io.PrintWriter;
58import java.util.ArrayList;
John Spurlocke77bb362014-04-26 10:24:59 -040059import java.util.Arrays;
Julia Reynolds9a86cc02016-02-10 15:38:15 -050060import java.util.HashSet;
John Spurlock7340fc82014-04-24 18:50:12 -040061import java.util.List;
Christopher Tate6597e342015-02-17 12:15:25 -080062import java.util.Objects;
John Spurlock7340fc82014-04-24 18:50:12 -040063import java.util.Set;
64
65/**
66 * Manages the lifecycle of application-provided services bound by system server.
67 *
68 * Services managed by this helper must have:
69 * - An associated system settings value with a list of enabled component names.
70 * - A well-known action for services to use in their intent-filter.
71 * - A system permission for services to require in order to ensure system has exclusive binding.
72 * - A settings page for user configuration of enabled services, and associated intent action.
73 * - A remote interface definition (aidl) provided by the service used for communication.
74 */
75abstract public class ManagedServices {
76 protected final String TAG = getClass().getSimpleName();
John Spurlock4db0d982014-08-13 09:19:03 -040077 protected final boolean DEBUG = Log.isLoggable(TAG, Log.DEBUG);
John Spurlock7340fc82014-04-24 18:50:12 -040078
Julia Reynoldsc279b992015-10-30 08:23:51 -040079 protected static final String ENABLED_SERVICES_SEPARATOR = ":";
John Spurlock7340fc82014-04-24 18:50:12 -040080
John Spurlockaf8d6c42014-05-07 17:49:08 -040081 protected final Context mContext;
John Spurlocke77bb362014-04-26 10:24:59 -040082 protected final Object mMutex;
John Spurlock7340fc82014-04-24 18:50:12 -040083 private final UserProfiles mUserProfiles;
84 private final SettingsObserver mSettingsObserver;
85 private final Config mConfig;
Christopher Tate6597e342015-02-17 12:15:25 -080086 private ArraySet<String> mRestored;
John Spurlock7340fc82014-04-24 18:50:12 -040087
88 // contains connections to all connected services, including app services
89 // and system services
90 protected final ArrayList<ManagedServiceInfo> mServices = new ArrayList<ManagedServiceInfo>();
91 // things that will be put into mServices as soon as they're ready
92 private final ArrayList<String> mServicesBinding = new ArrayList<String>();
Chris Wrenab41eec2016-01-04 18:01:27 -050093 // lists the component names of all enabled (and therefore potentially connected)
John Spurlock7340fc82014-04-24 18:50:12 -040094 // app services for current profiles.
95 private ArraySet<ComponentName> mEnabledServicesForCurrentProfiles
96 = new ArraySet<ComponentName>();
97 // Just the packages from mEnabledServicesForCurrentProfiles
98 private ArraySet<String> mEnabledServicesPackageNames = new ArraySet<String>();
Denis Kuznetsov76c02682015-09-17 19:57:00 +020099 // List of packages in restored setting across all mUserProfiles, for quick
100 // filtering upon package updates.
101 private ArraySet<String> mRestoredPackages = new ArraySet<>();
Chris Wrenab41eec2016-01-04 18:01:27 -0500102 // List of enabled packages that have nevertheless asked not to be run
103 private ArraySet<ComponentName> mSnoozingForCurrentProfiles = new ArraySet<>();
Denis Kuznetsov76c02682015-09-17 19:57:00 +0200104
John Spurlock7340fc82014-04-24 18:50:12 -0400105
Christoph Studerb53dfd42014-09-12 14:45:59 +0200106 // Kept to de-dupe user change events (experienced after boot, when we receive a settings and a
107 // user change).
108 private int[] mLastSeenProfileIds;
109
Christopher Tate6597e342015-02-17 12:15:25 -0800110 private final BroadcastReceiver mRestoreReceiver;
111
John Spurlock7340fc82014-04-24 18:50:12 -0400112 public ManagedServices(Context context, Handler handler, Object mutex,
113 UserProfiles userProfiles) {
114 mContext = context;
115 mMutex = mutex;
116 mUserProfiles = userProfiles;
117 mConfig = getConfig();
118 mSettingsObserver = new SettingsObserver(handler);
Christopher Tate6597e342015-02-17 12:15:25 -0800119
120 mRestoreReceiver = new SettingRestoredReceiver();
121 IntentFilter filter = new IntentFilter(Intent.ACTION_SETTING_RESTORED);
122 context.registerReceiver(mRestoreReceiver, filter);
Denis Kuznetsov76c02682015-09-17 19:57:00 +0200123 rebuildRestoredPackages();
Christopher Tate6597e342015-02-17 12:15:25 -0800124 }
125
126 class SettingRestoredReceiver extends BroadcastReceiver {
127 @Override
128 public void onReceive(Context context, Intent intent) {
129 if (Intent.ACTION_SETTING_RESTORED.equals(intent.getAction())) {
130 String element = intent.getStringExtra(Intent.EXTRA_SETTING_NAME);
Julia Reynolds6e839b02016-04-13 10:01:17 -0400131 if (Objects.equals(element, mConfig.secureSettingName)
132 || Objects.equals(element, mConfig.secondarySettingName)) {
Christopher Tate6597e342015-02-17 12:15:25 -0800133 String prevValue = intent.getStringExtra(Intent.EXTRA_SETTING_PREVIOUS_VALUE);
134 String newValue = intent.getStringExtra(Intent.EXTRA_SETTING_NEW_VALUE);
135 settingRestored(element, prevValue, newValue, getSendingUserId());
136 }
137 }
138 }
John Spurlock7340fc82014-04-24 18:50:12 -0400139 }
140
141 abstract protected Config getConfig();
142
143 private String getCaption() {
144 return mConfig.caption;
145 }
146
147 abstract protected IInterface asInterface(IBinder binder);
148
Chris Wren51017d02015-12-15 15:34:46 -0500149 abstract protected boolean checkType(IInterface service);
150
John Spurlock3b98b3f2014-05-01 09:08:48 -0400151 abstract protected void onServiceAdded(ManagedServiceInfo info);
John Spurlock7340fc82014-04-24 18:50:12 -0400152
John Spurlocke77bb362014-04-26 10:24:59 -0400153 protected void onServiceRemovedLocked(ManagedServiceInfo removed) { }
154
John Spurlock7340fc82014-04-24 18:50:12 -0400155 private ManagedServiceInfo newServiceInfo(IInterface service,
156 ComponentName component, int userid, boolean isSystem, ServiceConnection connection,
157 int targetSdkVersion) {
158 return new ManagedServiceInfo(service, component, userid, isSystem, connection,
159 targetSdkVersion);
160 }
161
162 public void onBootPhaseAppsCanStart() {
163 mSettingsObserver.observe();
164 }
165
John Spurlock25e2d242014-06-27 13:58:23 -0400166 public void dump(PrintWriter pw, DumpFilter filter) {
John Spurlocke77bb362014-04-26 10:24:59 -0400167 pw.println(" All " + getCaption() + "s (" + mEnabledServicesForCurrentProfiles.size()
John Spurlock7340fc82014-04-24 18:50:12 -0400168 + ") enabled for current profiles:");
169 for (ComponentName cmpt : mEnabledServicesForCurrentProfiles) {
John Spurlock25e2d242014-06-27 13:58:23 -0400170 if (filter != null && !filter.matches(cmpt)) continue;
John Spurlocke77bb362014-04-26 10:24:59 -0400171 pw.println(" " + cmpt);
John Spurlock7340fc82014-04-24 18:50:12 -0400172 }
173
John Spurlocke77bb362014-04-26 10:24:59 -0400174 pw.println(" Live " + getCaption() + "s (" + mServices.size() + "):");
John Spurlock7340fc82014-04-24 18:50:12 -0400175 for (ManagedServiceInfo info : mServices) {
John Spurlock25e2d242014-06-27 13:58:23 -0400176 if (filter != null && !filter.matches(info.component)) continue;
John Spurlocke77bb362014-04-26 10:24:59 -0400177 pw.println(" " + info.component
John Spurlock7340fc82014-04-24 18:50:12 -0400178 + " (user " + info.userid + "): " + info.service
Chris Wren51017d02015-12-15 15:34:46 -0500179 + (info.isSystem?" SYSTEM":"")
180 + (info.isGuest(this)?" GUEST":""));
John Spurlock7340fc82014-04-24 18:50:12 -0400181 }
Chris Wrenab41eec2016-01-04 18:01:27 -0500182
183 pw.println(" Snoozed " + getCaption() + "s (" +
184 mSnoozingForCurrentProfiles.size() + "):");
185 for (ComponentName name : mSnoozingForCurrentProfiles) {
186 pw.println(" " + name.flattenToShortString());
187 }
John Spurlock7340fc82014-04-24 18:50:12 -0400188 }
189
Christopher Tate6597e342015-02-17 12:15:25 -0800190 // By convention, restored settings are replicated to another settings
191 // entry, named similarly but with a disambiguation suffix.
Julia Reynolds6e839b02016-04-13 10:01:17 -0400192 public static String restoredSettingName(String setting) {
193 return setting + ":restored";
Christopher Tate6597e342015-02-17 12:15:25 -0800194 }
195
196 // The OS has done a restore of this service's saved state. We clone it to the
197 // 'restored' reserve, and then once we return and the actual write to settings is
198 // performed, our observer will do the work of maintaining the restored vs live
199 // settings data.
200 public void settingRestored(String element, String oldValue, String newValue, int userid) {
201 if (DEBUG) Slog.d(TAG, "Restored managed service setting: " + element
202 + " ovalue=" + oldValue + " nvalue=" + newValue);
Julia Reynolds6e839b02016-04-13 10:01:17 -0400203 if (mConfig.secureSettingName.equals(element) ||
204 mConfig.secondarySettingName.equals(element)) {
Christopher Tate6597e342015-02-17 12:15:25 -0800205 if (element != null) {
Christopher Tate6597e342015-02-17 12:15:25 -0800206 Settings.Secure.putStringForUser(mContext.getContentResolver(),
Julia Reynolds6e839b02016-04-13 10:01:17 -0400207 restoredSettingName(element),
Christopher Tate6597e342015-02-17 12:15:25 -0800208 newValue,
209 userid);
Julia Reynolds6e839b02016-04-13 10:01:17 -0400210 updateSettingsAccordingToInstalledServices(element, userid);
Denis Kuznetsov76c02682015-09-17 19:57:00 +0200211 rebuildRestoredPackages();
Christopher Tate6597e342015-02-17 12:15:25 -0800212 }
213 }
214 }
215
John Spurlock80774932015-05-07 17:38:50 -0400216 public boolean isComponentEnabledForPackage(String pkg) {
217 return mEnabledServicesPackageNames.contains(pkg);
218 }
219
Julia Reynolds6434eb22016-08-08 17:19:26 -0400220 public void onPackagesChanged(boolean removingPackage, String[] pkgList) {
221 if (DEBUG) Slog.d(TAG, "onPackagesChanged removingPackage=" + removingPackage
John Spurlocke77bb362014-04-26 10:24:59 -0400222 + " pkgList=" + (pkgList == null ? null : Arrays.asList(pkgList))
223 + " mEnabledServicesPackageNames=" + mEnabledServicesPackageNames);
John Spurlock7340fc82014-04-24 18:50:12 -0400224 boolean anyServicesInvolved = false;
Denis Kuznetsov76c02682015-09-17 19:57:00 +0200225
John Spurlock7340fc82014-04-24 18:50:12 -0400226 if (pkgList != null && (pkgList.length > 0)) {
227 for (String pkgName : pkgList) {
Denis Kuznetsov76c02682015-09-17 19:57:00 +0200228 if (mEnabledServicesPackageNames.contains(pkgName) ||
229 mRestoredPackages.contains(pkgName)) {
John Spurlock7340fc82014-04-24 18:50:12 -0400230 anyServicesInvolved = true;
231 }
232 }
233 }
234
235 if (anyServicesInvolved) {
236 // if we're not replacing a package, clean up orphaned bits
Julia Reynolds6434eb22016-08-08 17:19:26 -0400237 if (removingPackage) {
Denis Kuznetsov76c02682015-09-17 19:57:00 +0200238 updateSettingsAccordingToInstalledServices();
239 rebuildRestoredPackages();
John Spurlock7340fc82014-04-24 18:50:12 -0400240 }
241 // make sure we're still bound to any of our services who may have just upgraded
Julia Reynolds1c9bd422016-03-15 09:25:56 -0400242 rebindServices(false);
John Spurlock7340fc82014-04-24 18:50:12 -0400243 }
244 }
245
John Spurlock1b8b22b2015-05-20 09:47:13 -0400246 public void onUserSwitched(int user) {
247 if (DEBUG) Slog.d(TAG, "onUserSwitched u=" + user);
Denis Kuznetsov76c02682015-09-17 19:57:00 +0200248 rebuildRestoredPackages();
Christoph Studerb53dfd42014-09-12 14:45:59 +0200249 if (Arrays.equals(mLastSeenProfileIds, mUserProfiles.getCurrentProfileIds())) {
250 if (DEBUG) Slog.d(TAG, "Current profile IDs didn't change, skipping rebindServices().");
251 return;
252 }
Julia Reynolds1c9bd422016-03-15 09:25:56 -0400253 rebindServices(true);
Christoph Studerb53dfd42014-09-12 14:45:59 +0200254 }
255
Julia Reynoldsa3dcaff2016-02-03 15:04:05 -0500256 public void onUserUnlocked(int user) {
257 if (DEBUG) Slog.d(TAG, "onUserUnlocked u=" + user);
258 rebuildRestoredPackages();
Julia Reynolds1c9bd422016-03-15 09:25:56 -0400259 rebindServices(false);
Julia Reynoldsa3dcaff2016-02-03 15:04:05 -0500260 }
261
Chris Wrenab41eec2016-01-04 18:01:27 -0500262 public ManagedServiceInfo getServiceFromTokenLocked(IInterface service) {
263 if (service == null) {
264 return null;
265 }
John Spurlock7340fc82014-04-24 18:50:12 -0400266 final IBinder token = service.asBinder();
267 final int N = mServices.size();
Denis Kuznetsov76c02682015-09-17 19:57:00 +0200268 for (int i = 0; i < N; i++) {
John Spurlock7340fc82014-04-24 18:50:12 -0400269 final ManagedServiceInfo info = mServices.get(i);
270 if (info.service.asBinder() == token) return info;
271 }
Chris Wrenab41eec2016-01-04 18:01:27 -0500272 return null;
273 }
274
275 public ManagedServiceInfo checkServiceTokenLocked(IInterface service) {
276 checkNotNull(service);
277 ManagedServiceInfo info = getServiceFromTokenLocked(service);
278 if (info != null) {
279 return info;
280 }
John Spurlock7340fc82014-04-24 18:50:12 -0400281 throw new SecurityException("Disallowed call from unknown " + getCaption() + ": "
282 + service);
283 }
284
285 public void unregisterService(IInterface service, int userid) {
286 checkNotNull(service);
287 // no need to check permissions; if your service binder is in the list,
288 // that's proof that you had permission to add it in the first place
289 unregisterServiceImpl(service, userid);
290 }
291
292 public void registerService(IInterface service, ComponentName component, int userid) {
293 checkNotNull(service);
Christoph Studer3e144d32014-05-22 16:48:40 +0200294 ManagedServiceInfo info = registerServiceImpl(service, component, userid);
295 if (info != null) {
296 onServiceAdded(info);
297 }
John Spurlock7340fc82014-04-24 18:50:12 -0400298 }
299
Chris Wren51017d02015-12-15 15:34:46 -0500300 /**
301 * Add a service to our callbacks. The lifecycle of this service is managed externally,
302 * but unlike a system service, it should not be considered privledged.
303 * */
304 public void registerGuestService(ManagedServiceInfo guest) {
305 checkNotNull(guest.service);
Ruben Brunke24b9a62016-02-16 21:38:24 -0800306 if (!checkType(guest.service)) {
307 throw new IllegalArgumentException();
308 }
Chris Wren51017d02015-12-15 15:34:46 -0500309 if (registerServiceImpl(guest) != null) {
310 onServiceAdded(guest);
Chris Wrenab41eec2016-01-04 18:01:27 -0500311 }
312 }
313
314 public void setComponentState(ComponentName component, boolean enabled) {
315 boolean previous = !mSnoozingForCurrentProfiles.contains(component);
316 if (previous == enabled) {
317 return;
318 }
319
320 if (enabled) {
321 mSnoozingForCurrentProfiles.remove(component);
322 } else {
323 mSnoozingForCurrentProfiles.add(component);
324 }
325
326 // State changed
327 if (DEBUG) {
328 Slog.d(TAG, ((enabled) ? "Enabling " : "Disabling ") + "component " +
329 component.flattenToShortString());
330 }
331
Chris Wren0efdb882016-03-01 17:17:47 -0500332
333 synchronized (mMutex) {
334 final int[] userIds = mUserProfiles.getCurrentProfileIds();
335
336 for (int userId : userIds) {
337 if (enabled) {
338 registerServiceLocked(component, userId);
339 } else {
340 unregisterServiceLocked(component, userId);
341 }
Chris Wrenab41eec2016-01-04 18:01:27 -0500342 }
Chris Wren51017d02015-12-15 15:34:46 -0500343 }
344 }
345
Denis Kuznetsov76c02682015-09-17 19:57:00 +0200346 private void rebuildRestoredPackages() {
347 mRestoredPackages.clear();
Chris Wrenab41eec2016-01-04 18:01:27 -0500348 mSnoozingForCurrentProfiles.clear();
Julia Reynolds6e839b02016-04-13 10:01:17 -0400349 String secureSettingName = restoredSettingName(mConfig.secureSettingName);
350 String secondarySettingName = mConfig.secondarySettingName == null
351 ? null : restoredSettingName(mConfig.secondarySettingName);
John Spurlock7340fc82014-04-24 18:50:12 -0400352 int[] userIds = mUserProfiles.getCurrentProfileIds();
353 final int N = userIds.length;
Denis Kuznetsov76c02682015-09-17 19:57:00 +0200354 for (int i = 0; i < N; ++i) {
Julia Reynolds6e839b02016-04-13 10:01:17 -0400355 ArraySet<ComponentName> names =
356 loadComponentNamesFromSetting(secureSettingName, userIds[i]);
357 if (secondarySettingName != null) {
358 names.addAll(loadComponentNamesFromSetting(secondarySettingName, userIds[i]));
359 }
360 for (ComponentName name : names) {
Denis Kuznetsov76c02682015-09-17 19:57:00 +0200361 mRestoredPackages.add(name.getPackageName());
362 }
John Spurlock7340fc82014-04-24 18:50:12 -0400363 }
364 }
365
Denis Kuznetsov76c02682015-09-17 19:57:00 +0200366
Julia Reynolds6e839b02016-04-13 10:01:17 -0400367 protected @NonNull ArraySet<ComponentName> loadComponentNamesFromSetting(String settingName,
Julia Reynoldsc279b992015-10-30 08:23:51 -0400368 int userId) {
Christopher Tate6597e342015-02-17 12:15:25 -0800369 final ContentResolver cr = mContext.getContentResolver();
Denis Kuznetsov76c02682015-09-17 19:57:00 +0200370 String settingValue = Settings.Secure.getStringForUser(
Ruben Brunkdd18a0b2015-12-04 16:16:31 -0800371 cr,
372 settingName,
373 userId);
Denis Kuznetsov76c02682015-09-17 19:57:00 +0200374 if (TextUtils.isEmpty(settingValue))
Julia Reynolds6e839b02016-04-13 10:01:17 -0400375 return new ArraySet<>();
Denis Kuznetsov76c02682015-09-17 19:57:00 +0200376 String[] restored = settingValue.split(ENABLED_SERVICES_SEPARATOR);
377 ArraySet<ComponentName> result = new ArraySet<>(restored.length);
378 for (int i = 0; i < restored.length; i++) {
379 ComponentName value = ComponentName.unflattenFromString(restored[i]);
380 if (null != value) {
381 result.add(value);
Christopher Tate6597e342015-02-17 12:15:25 -0800382 }
383 }
Denis Kuznetsov76c02682015-09-17 19:57:00 +0200384 return result;
385 }
John Spurlock7340fc82014-04-24 18:50:12 -0400386
Denis Kuznetsov76c02682015-09-17 19:57:00 +0200387 private void storeComponentsToSetting(Set<ComponentName> components,
388 String settingName,
389 int userId) {
390 String[] componentNames = null;
391 if (null != components) {
392 componentNames = new String[components.size()];
393 int index = 0;
394 for (ComponentName c: components) {
395 componentNames[index++] = c.flattenToString();
396 }
397 }
398 final String value = (componentNames == null) ? "" :
399 TextUtils.join(ENABLED_SERVICES_SEPARATOR, componentNames);
400 final ContentResolver cr = mContext.getContentResolver();
401 Settings.Secure.putStringForUser(
Ruben Brunkdd18a0b2015-12-04 16:16:31 -0800402 cr,
403 settingName,
404 value,
405 userId);
Denis Kuznetsov76c02682015-09-17 19:57:00 +0200406 }
407
Denis Kuznetsov76c02682015-09-17 19:57:00 +0200408 /**
409 * Remove access for any services that no longer exist.
410 */
411 private void updateSettingsAccordingToInstalledServices() {
412 int[] userIds = mUserProfiles.getCurrentProfileIds();
413 final int N = userIds.length;
414 for (int i = 0; i < N; ++i) {
Julia Reynolds6e839b02016-04-13 10:01:17 -0400415 updateSettingsAccordingToInstalledServices(mConfig.secureSettingName, userIds[i]);
416 if (mConfig.secondarySettingName != null) {
417 updateSettingsAccordingToInstalledServices(
418 mConfig.secondarySettingName, userIds[i]);
419 }
Denis Kuznetsov76c02682015-09-17 19:57:00 +0200420 }
421 rebuildRestoredPackages();
422 }
423
Julia Reynoldsc279b992015-10-30 08:23:51 -0400424 protected Set<ComponentName> queryPackageForServices(String packageName, int userId) {
Denis Kuznetsov76c02682015-09-17 19:57:00 +0200425 Set<ComponentName> installed = new ArraySet<>();
Denis Kuznetsov76c02682015-09-17 19:57:00 +0200426 final PackageManager pm = mContext.getPackageManager();
Julia Reynoldsc279b992015-10-30 08:23:51 -0400427 Intent queryIntent = new Intent(mConfig.serviceInterface);
428 if (!TextUtils.isEmpty(packageName)) {
429 queryIntent.setPackage(packageName);
430 }
Denis Kuznetsov76c02682015-09-17 19:57:00 +0200431 List<ResolveInfo> installedServices = pm.queryIntentServicesAsUser(
Julia Reynoldsc279b992015-10-30 08:23:51 -0400432 queryIntent,
Denis Kuznetsov76c02682015-09-17 19:57:00 +0200433 PackageManager.GET_SERVICES | PackageManager.GET_META_DATA,
434 userId);
435 if (DEBUG)
436 Slog.v(TAG, mConfig.serviceInterface + " services: " + installedServices);
Julia Reynolds50f05142015-10-30 17:03:59 -0400437 if (installedServices != null) {
438 for (int i = 0, count = installedServices.size(); i < count; i++) {
439 ResolveInfo resolveInfo = installedServices.get(i);
440 ServiceInfo info = resolveInfo.serviceInfo;
Denis Kuznetsov76c02682015-09-17 19:57:00 +0200441
Julia Reynolds50f05142015-10-30 17:03:59 -0400442 ComponentName component = new ComponentName(info.packageName, info.name);
443 if (!mConfig.bindPermission.equals(info.permission)) {
444 Slog.w(TAG, "Skipping " + getCaption() + " service "
Ruben Brunkdd18a0b2015-12-04 16:16:31 -0800445 + info.packageName + "/" + info.name
446 + ": it does not require the permission "
447 + mConfig.bindPermission);
Julia Reynolds50f05142015-10-30 17:03:59 -0400448 continue;
449 }
450 installed.add(component);
Denis Kuznetsov76c02682015-09-17 19:57:00 +0200451 }
Denis Kuznetsov76c02682015-09-17 19:57:00 +0200452 }
Julia Reynoldsc279b992015-10-30 08:23:51 -0400453 return installed;
454 }
455
Julia Reynolds6e839b02016-04-13 10:01:17 -0400456 private void updateSettingsAccordingToInstalledServices(String setting, int userId) {
Julia Reynoldsc279b992015-10-30 08:23:51 -0400457 boolean restoredChanged = false;
458 boolean currentChanged = false;
459 Set<ComponentName> restored =
Julia Reynolds6e839b02016-04-13 10:01:17 -0400460 loadComponentNamesFromSetting(restoredSettingName(setting), userId);
Julia Reynoldsc279b992015-10-30 08:23:51 -0400461 Set<ComponentName> current =
Julia Reynolds6e839b02016-04-13 10:01:17 -0400462 loadComponentNamesFromSetting(setting, userId);
Julia Reynoldsc279b992015-10-30 08:23:51 -0400463 // Load all services for all packages.
464 Set<ComponentName> installed = queryPackageForServices(null, userId);
Denis Kuznetsov76c02682015-09-17 19:57:00 +0200465
466 ArraySet<ComponentName> retained = new ArraySet<>();
467
468 for (ComponentName component : installed) {
469 if (null != restored) {
470 boolean wasRestored = restored.remove(component);
471 if (wasRestored) {
472 // Freshly installed package has service that was mentioned in restored setting.
473 if (DEBUG)
474 Slog.v(TAG, "Restoring " + component + " for user " + userId);
475 restoredChanged = true;
476 currentChanged = true;
477 retained.add(component);
John Spurlock7340fc82014-04-24 18:50:12 -0400478 continue;
479 }
John Spurlock7340fc82014-04-24 18:50:12 -0400480 }
481
Denis Kuznetsov76c02682015-09-17 19:57:00 +0200482 if (null != current) {
483 if (current.contains(component))
484 retained.add(component);
John Spurlock7340fc82014-04-24 18:50:12 -0400485 }
Denis Kuznetsov76c02682015-09-17 19:57:00 +0200486 }
487
488 currentChanged |= ((current == null ? 0 : current.size()) != retained.size());
489
490 if (currentChanged) {
491 if (DEBUG) Slog.v(TAG, "List of " + getCaption() + " services was updated " + current);
Julia Reynolds6e839b02016-04-13 10:01:17 -0400492 storeComponentsToSetting(retained, setting, userId);
Denis Kuznetsov76c02682015-09-17 19:57:00 +0200493 }
494
495 if (restoredChanged) {
496 if (DEBUG) Slog.v(TAG,
497 "List of " + getCaption() + " restored services was updated " + restored);
Julia Reynolds6e839b02016-04-13 10:01:17 -0400498 storeComponentsToSetting(restored, restoredSettingName(setting), userId);
John Spurlock7340fc82014-04-24 18:50:12 -0400499 }
500 }
501
502 /**
503 * Called whenever packages change, the user switches, or the secure setting
504 * is altered. (For example in response to USER_SWITCHED in our broadcast receiver)
505 */
Julia Reynolds1c9bd422016-03-15 09:25:56 -0400506 private void rebindServices(boolean forceRebind) {
John Spurlock7340fc82014-04-24 18:50:12 -0400507 if (DEBUG) Slog.d(TAG, "rebindServices");
508 final int[] userIds = mUserProfiles.getCurrentProfileIds();
509 final int nUserIds = userIds.length;
510
Denis Kuznetsov76c02682015-09-17 19:57:00 +0200511 final SparseArray<ArraySet<ComponentName>> componentsByUser = new SparseArray<>();
John Spurlock7340fc82014-04-24 18:50:12 -0400512
513 for (int i = 0; i < nUserIds; ++i) {
Denis Kuznetsov76c02682015-09-17 19:57:00 +0200514 componentsByUser.put(userIds[i],
515 loadComponentNamesFromSetting(mConfig.secureSettingName, userIds[i]));
Julia Reynolds6e839b02016-04-13 10:01:17 -0400516 if (mConfig.secondarySettingName != null) {
517 componentsByUser.get(userIds[i]).addAll(
518 loadComponentNamesFromSetting(mConfig.secondarySettingName, userIds[i]));
519 }
John Spurlock7340fc82014-04-24 18:50:12 -0400520 }
521
Julia Reynolds9a86cc02016-02-10 15:38:15 -0500522 final ArrayList<ManagedServiceInfo> removableBoundServices = new ArrayList<>();
523 final SparseArray<Set<ComponentName>> toAdd = new SparseArray<>();
John Spurlock7340fc82014-04-24 18:50:12 -0400524
525 synchronized (mMutex) {
Julia Reynolds1c9bd422016-03-15 09:25:56 -0400526 // Rebind to non-system services if user switched
Christoph Studer5d423842014-05-23 13:15:54 +0200527 for (ManagedServiceInfo service : mServices) {
Chris Wren51017d02015-12-15 15:34:46 -0500528 if (!service.isSystem && !service.isGuest(this)) {
Julia Reynolds9a86cc02016-02-10 15:38:15 -0500529 removableBoundServices.add(service);
Christoph Studer5d423842014-05-23 13:15:54 +0200530 }
531 }
John Spurlock7340fc82014-04-24 18:50:12 -0400532
Julia Reynolds1c9bd422016-03-15 09:25:56 -0400533 mEnabledServicesForCurrentProfiles.clear();
534 mEnabledServicesPackageNames.clear();
John Spurlock7340fc82014-04-24 18:50:12 -0400535
536 for (int i = 0; i < nUserIds; ++i) {
Denis Kuznetsov76c02682015-09-17 19:57:00 +0200537 // decode the list of components
538 final ArraySet<ComponentName> userComponents = componentsByUser.get(userIds[i]);
539 if (null == userComponents) {
Julia Reynolds6e839b02016-04-13 10:01:17 -0400540 toAdd.put(userIds[i], new ArraySet<ComponentName>());
Denis Kuznetsov76c02682015-09-17 19:57:00 +0200541 continue;
542 }
543
Julia Reynolds9a86cc02016-02-10 15:38:15 -0500544 final Set<ComponentName> add = new HashSet<>(userComponents);
Chris Wrenab41eec2016-01-04 18:01:27 -0500545 add.removeAll(mSnoozingForCurrentProfiles);
Ruben Brunkdd18a0b2015-12-04 16:16:31 -0800546
John Spurlock7340fc82014-04-24 18:50:12 -0400547 toAdd.put(userIds[i], add);
548
Julia Reynolds1c9bd422016-03-15 09:25:56 -0400549 mEnabledServicesForCurrentProfiles.addAll(userComponents);
John Spurlock7340fc82014-04-24 18:50:12 -0400550
Denis Kuznetsov76c02682015-09-17 19:57:00 +0200551 for (int j = 0; j < userComponents.size(); j++) {
Chris Wren083094c2015-12-15 16:25:07 -0500552 final ComponentName component = userComponents.valueAt(j);
Julia Reynolds1c9bd422016-03-15 09:25:56 -0400553 mEnabledServicesPackageNames.add(component.getPackageName());
John Spurlock7340fc82014-04-24 18:50:12 -0400554 }
555 }
John Spurlock7340fc82014-04-24 18:50:12 -0400556 }
557
Julia Reynolds9a86cc02016-02-10 15:38:15 -0500558 for (ManagedServiceInfo info : removableBoundServices) {
John Spurlock7340fc82014-04-24 18:50:12 -0400559 final ComponentName component = info.component;
560 final int oldUser = info.userid;
Julia Reynolds9a86cc02016-02-10 15:38:15 -0500561 final Set<ComponentName> allowedComponents = toAdd.get(info.userid);
562 if (allowedComponents != null) {
Julia Reynolds1c9bd422016-03-15 09:25:56 -0400563 if (allowedComponents.contains(component) && !forceRebind) {
Julia Reynolds9a86cc02016-02-10 15:38:15 -0500564 // Already bound, don't need to bind again.
565 allowedComponents.remove(component);
566 } else {
Julia Reynolds1c9bd422016-03-15 09:25:56 -0400567 // No longer allowed to be bound, or must rebind.
Julia Reynolds9a86cc02016-02-10 15:38:15 -0500568 Slog.v(TAG, "disabling " + getCaption() + " for user "
569 + oldUser + ": " + component);
570 unregisterService(component, oldUser);
571 }
572 }
John Spurlock7340fc82014-04-24 18:50:12 -0400573 }
574
575 for (int i = 0; i < nUserIds; ++i) {
Julia Reynolds9a86cc02016-02-10 15:38:15 -0500576 final Set<ComponentName> add = toAdd.get(userIds[i]);
577 for (ComponentName component : add) {
Julia Reynolds1c9bd422016-03-15 09:25:56 -0400578 Slog.v(TAG, "enabling " + getCaption() + " for " + userIds[i] + ": " + component);
John Spurlock7340fc82014-04-24 18:50:12 -0400579 registerService(component, userIds[i]);
580 }
581 }
Christoph Studerb53dfd42014-09-12 14:45:59 +0200582
Denis Kuznetsov76c02682015-09-17 19:57:00 +0200583 mLastSeenProfileIds = userIds;
John Spurlock7340fc82014-04-24 18:50:12 -0400584 }
585
586 /**
587 * Version of registerService that takes the name of a service component to bind to.
588 */
589 private void registerService(final ComponentName name, final int userid) {
Ruben Brunkdd18a0b2015-12-04 16:16:31 -0800590 synchronized (mMutex) {
591 registerServiceLocked(name, userid);
592 }
593 }
594
Chris Wren0efdb882016-03-01 17:17:47 -0500595 /**
596 * Inject a system service into the management list.
597 */
598 public void registerSystemService(final ComponentName name, final int userid) {
599 synchronized (mMutex) {
600 registerServiceLocked(name, userid, true /* isSystem */);
601 }
602 }
603
Ruben Brunkdd18a0b2015-12-04 16:16:31 -0800604 private void registerServiceLocked(final ComponentName name, final int userid) {
Chris Wren0efdb882016-03-01 17:17:47 -0500605 registerServiceLocked(name, userid, false /* isSystem */);
606 }
607
608 private void registerServiceLocked(final ComponentName name, final int userid,
609 final boolean isSystem) {
John Spurlock7340fc82014-04-24 18:50:12 -0400610 if (DEBUG) Slog.v(TAG, "registerService: " + name + " u=" + userid);
611
Ruben Brunkdd18a0b2015-12-04 16:16:31 -0800612 final String servicesBindingTag = name.toString() + "/" + userid;
613 if (mServicesBinding.contains(servicesBindingTag)) {
614 // stop registering this thing already! we're working on it
615 return;
616 }
617 mServicesBinding.add(servicesBindingTag);
John Spurlock7340fc82014-04-24 18:50:12 -0400618
Ruben Brunkdd18a0b2015-12-04 16:16:31 -0800619 final int N = mServices.size();
620 for (int i = N - 1; i >= 0; i--) {
621 final ManagedServiceInfo info = mServices.get(i);
622 if (name.equals(info.component)
623 && info.userid == userid) {
624 // cut old connections
625 if (DEBUG) Slog.v(TAG, " disconnecting old " + getCaption() + ": "
626 + info.service);
627 removeServiceLocked(i);
628 if (info.connection != null) {
629 mContext.unbindService(info.connection);
John Spurlock7340fc82014-04-24 18:50:12 -0400630 }
631 }
Ruben Brunkdd18a0b2015-12-04 16:16:31 -0800632 }
John Spurlock7340fc82014-04-24 18:50:12 -0400633
Ruben Brunkdd18a0b2015-12-04 16:16:31 -0800634 Intent intent = new Intent(mConfig.serviceInterface);
635 intent.setComponent(name);
John Spurlock7340fc82014-04-24 18:50:12 -0400636
Ruben Brunkdd18a0b2015-12-04 16:16:31 -0800637 intent.putExtra(Intent.EXTRA_CLIENT_LABEL, mConfig.clientLabel);
John Spurlock7340fc82014-04-24 18:50:12 -0400638
Ruben Brunkdd18a0b2015-12-04 16:16:31 -0800639 final PendingIntent pendingIntent = PendingIntent.getActivity(
640 mContext, 0, new Intent(mConfig.settingsAction), 0);
641 intent.putExtra(Intent.EXTRA_CLIENT_INTENT, pendingIntent);
John Spurlock7340fc82014-04-24 18:50:12 -0400642
Ruben Brunkdd18a0b2015-12-04 16:16:31 -0800643 ApplicationInfo appInfo = null;
644 try {
645 appInfo = mContext.getPackageManager().getApplicationInfo(
646 name.getPackageName(), 0);
647 } catch (NameNotFoundException e) {
648 // Ignore if the package doesn't exist we won't be able to bind to the service.
649 }
650 final int targetSdkVersion =
651 appInfo != null ? appInfo.targetSdkVersion : Build.VERSION_CODES.BASE;
John Spurlock7340fc82014-04-24 18:50:12 -0400652
Ruben Brunkdd18a0b2015-12-04 16:16:31 -0800653 try {
654 if (DEBUG) Slog.v(TAG, "binding: " + intent);
655 ServiceConnection serviceConnection = new ServiceConnection() {
656 IInterface mService;
Denis Kuznetsov76c02682015-09-17 19:57:00 +0200657
Ruben Brunkdd18a0b2015-12-04 16:16:31 -0800658 @Override
659 public void onServiceConnected(ComponentName name, IBinder binder) {
660 boolean added = false;
661 ManagedServiceInfo info = null;
662 synchronized (mMutex) {
663 mServicesBinding.remove(servicesBindingTag);
664 try {
665 mService = asInterface(binder);
666 info = newServiceInfo(mService, name,
Chris Wren0efdb882016-03-01 17:17:47 -0500667 userid, isSystem, this, targetSdkVersion);
Ruben Brunkdd18a0b2015-12-04 16:16:31 -0800668 binder.linkToDeath(info, 0);
669 added = mServices.add(info);
670 } catch (RemoteException e) {
671 // already dead
Denis Kuznetsov76c02682015-09-17 19:57:00 +0200672 }
673 }
Ruben Brunkdd18a0b2015-12-04 16:16:31 -0800674 if (added) {
675 onServiceAdded(info);
Denis Kuznetsov76c02682015-09-17 19:57:00 +0200676 }
John Spurlock7340fc82014-04-24 18:50:12 -0400677 }
Ruben Brunkdd18a0b2015-12-04 16:16:31 -0800678
679 @Override
680 public void onServiceDisconnected(ComponentName name) {
681 Slog.v(TAG, getCaption() + " connection lost: " + name);
682 }
683 };
684 if (!mContext.bindServiceAsUser(intent,
685 serviceConnection,
Felipe Lemea1b79bf2016-05-24 13:06:54 -0700686 BIND_AUTO_CREATE | BIND_FOREGROUND_SERVICE | BIND_ALLOW_WHITELIST_MANAGEMENT,
Ruben Brunkdd18a0b2015-12-04 16:16:31 -0800687 new UserHandle(userid))) {
688 mServicesBinding.remove(servicesBindingTag);
689 Slog.w(TAG, "Unable to bind " + getCaption() + " service: " + intent);
John Spurlock7340fc82014-04-24 18:50:12 -0400690 return;
691 }
Ruben Brunkdd18a0b2015-12-04 16:16:31 -0800692 } catch (SecurityException ex) {
693 Slog.e(TAG, "Unable to bind " + getCaption() + " service: " + intent, ex);
694 return;
John Spurlock7340fc82014-04-24 18:50:12 -0400695 }
696 }
697
698 /**
699 * Remove a service for the given user by ComponentName
700 */
701 private void unregisterService(ComponentName name, int userid) {
702 synchronized (mMutex) {
Ruben Brunkdd18a0b2015-12-04 16:16:31 -0800703 unregisterServiceLocked(name, userid);
704 }
705 }
706
707 private void unregisterServiceLocked(ComponentName name, int userid) {
708 final int N = mServices.size();
709 for (int i = N - 1; i >= 0; i--) {
710 final ManagedServiceInfo info = mServices.get(i);
711 if (name.equals(info.component)
712 && info.userid == userid) {
713 removeServiceLocked(i);
714 if (info.connection != null) {
715 try {
716 mContext.unbindService(info.connection);
717 } catch (IllegalArgumentException ex) {
718 // something happened to the service: we think we have a connection
719 // but it's bogus.
720 Slog.e(TAG, getCaption() + " " + name + " could not be unbound: " + ex);
John Spurlock7340fc82014-04-24 18:50:12 -0400721 }
722 }
723 }
724 }
725 }
726
727 /**
728 * Removes a service from the list but does not unbind
729 *
730 * @return the removed service.
731 */
732 private ManagedServiceInfo removeServiceImpl(IInterface service, final int userid) {
John Spurlocke77bb362014-04-26 10:24:59 -0400733 if (DEBUG) Slog.d(TAG, "removeServiceImpl service=" + service + " u=" + userid);
John Spurlock7340fc82014-04-24 18:50:12 -0400734 ManagedServiceInfo serviceInfo = null;
735 synchronized (mMutex) {
736 final int N = mServices.size();
Denis Kuznetsov76c02682015-09-17 19:57:00 +0200737 for (int i = N - 1; i >= 0; i--) {
John Spurlock7340fc82014-04-24 18:50:12 -0400738 final ManagedServiceInfo info = mServices.get(i);
739 if (info.service.asBinder() == service.asBinder()
740 && info.userid == userid) {
John Spurlocke77bb362014-04-26 10:24:59 -0400741 if (DEBUG) Slog.d(TAG, "Removing active service " + info.component);
742 serviceInfo = removeServiceLocked(i);
John Spurlock7340fc82014-04-24 18:50:12 -0400743 }
744 }
745 }
746 return serviceInfo;
747 }
748
John Spurlocke77bb362014-04-26 10:24:59 -0400749 private ManagedServiceInfo removeServiceLocked(int i) {
750 final ManagedServiceInfo info = mServices.remove(i);
751 onServiceRemovedLocked(info);
752 return info;
753 }
754
John Spurlock7340fc82014-04-24 18:50:12 -0400755 private void checkNotNull(IInterface service) {
756 if (service == null) {
757 throw new IllegalArgumentException(getCaption() + " must not be null");
758 }
759 }
760
Christoph Studer3e144d32014-05-22 16:48:40 +0200761 private ManagedServiceInfo registerServiceImpl(final IInterface service,
John Spurlock7340fc82014-04-24 18:50:12 -0400762 final ComponentName component, final int userid) {
Chris Wren51017d02015-12-15 15:34:46 -0500763 ManagedServiceInfo info = newServiceInfo(service, component, userid,
764 true /*isSystem*/, null /*connection*/, Build.VERSION_CODES.LOLLIPOP);
765 return registerServiceImpl(info);
766 }
767
768 private ManagedServiceInfo registerServiceImpl(ManagedServiceInfo info) {
John Spurlock7340fc82014-04-24 18:50:12 -0400769 synchronized (mMutex) {
770 try {
Chris Wren51017d02015-12-15 15:34:46 -0500771 info.service.asBinder().linkToDeath(info, 0);
John Spurlock7340fc82014-04-24 18:50:12 -0400772 mServices.add(info);
Christoph Studer3e144d32014-05-22 16:48:40 +0200773 return info;
John Spurlock7340fc82014-04-24 18:50:12 -0400774 } catch (RemoteException e) {
775 // already dead
776 }
777 }
Christoph Studer3e144d32014-05-22 16:48:40 +0200778 return null;
John Spurlock7340fc82014-04-24 18:50:12 -0400779 }
780
781 /**
782 * Removes a service from the list and unbinds.
783 */
784 private void unregisterServiceImpl(IInterface service, int userid) {
785 ManagedServiceInfo info = removeServiceImpl(service, userid);
Chris Wren51017d02015-12-15 15:34:46 -0500786 if (info != null && info.connection != null && !info.isGuest(this)) {
John Spurlock7340fc82014-04-24 18:50:12 -0400787 mContext.unbindService(info.connection);
788 }
789 }
790
791 private class SettingsObserver extends ContentObserver {
792 private final Uri mSecureSettingsUri = Settings.Secure.getUriFor(mConfig.secureSettingName);
Julia Reynolds6e839b02016-04-13 10:01:17 -0400793 private final Uri mSecondarySettingsUri;
John Spurlock7340fc82014-04-24 18:50:12 -0400794
795 private SettingsObserver(Handler handler) {
796 super(handler);
Julia Reynolds6e839b02016-04-13 10:01:17 -0400797 if (mConfig.secondarySettingName != null) {
798 mSecondarySettingsUri = Settings.Secure.getUriFor(mConfig.secondarySettingName);
799 } else {
800 mSecondarySettingsUri = null;
801 }
John Spurlock7340fc82014-04-24 18:50:12 -0400802 }
803
804 private void observe() {
805 ContentResolver resolver = mContext.getContentResolver();
806 resolver.registerContentObserver(mSecureSettingsUri,
807 false, this, UserHandle.USER_ALL);
Julia Reynolds6e839b02016-04-13 10:01:17 -0400808 if (mSecondarySettingsUri != null) {
809 resolver.registerContentObserver(mSecondarySettingsUri,
810 false, this, UserHandle.USER_ALL);
811 }
John Spurlock7340fc82014-04-24 18:50:12 -0400812 update(null);
813 }
814
815 @Override
816 public void onChange(boolean selfChange, Uri uri) {
817 update(uri);
818 }
819
820 private void update(Uri uri) {
Julia Reynolds6e839b02016-04-13 10:01:17 -0400821 if (uri == null || mSecureSettingsUri.equals(uri)
822 || uri.equals(mSecondarySettingsUri)) {
823 if (DEBUG) Slog.d(TAG, "Setting changed: uri=" + uri);
Julia Reynolds1c9bd422016-03-15 09:25:56 -0400824 rebindServices(false);
Denis Kuznetsov76c02682015-09-17 19:57:00 +0200825 rebuildRestoredPackages();
John Spurlock7340fc82014-04-24 18:50:12 -0400826 }
827 }
828 }
829
830 public class ManagedServiceInfo implements IBinder.DeathRecipient {
831 public IInterface service;
832 public ComponentName component;
833 public int userid;
834 public boolean isSystem;
835 public ServiceConnection connection;
836 public int targetSdkVersion;
837
838 public ManagedServiceInfo(IInterface service, ComponentName component,
839 int userid, boolean isSystem, ServiceConnection connection, int targetSdkVersion) {
840 this.service = service;
841 this.component = component;
842 this.userid = userid;
843 this.isSystem = isSystem;
844 this.connection = connection;
845 this.targetSdkVersion = targetSdkVersion;
846 }
847
Chris Wren51017d02015-12-15 15:34:46 -0500848 public boolean isGuest(ManagedServices host) {
849 return ManagedServices.this != host;
850 }
851
Chris Wrenab41eec2016-01-04 18:01:27 -0500852 public ManagedServices getOwner() {
853 return ManagedServices.this;
854 }
855
John Spurlocke77bb362014-04-26 10:24:59 -0400856 @Override
857 public String toString() {
858 return new StringBuilder("ManagedServiceInfo[")
859 .append("component=").append(component)
860 .append(",userid=").append(userid)
861 .append(",isSystem=").append(isSystem)
862 .append(",targetSdkVersion=").append(targetSdkVersion)
863 .append(",connection=").append(connection == null ? null : "<connection>")
864 .append(",service=").append(service)
865 .append(']').toString();
866 }
867
John Spurlock7340fc82014-04-24 18:50:12 -0400868 public boolean enabledAndUserMatches(int nid) {
869 if (!isEnabledForCurrentProfiles()) {
870 return false;
871 }
872 if (this.userid == UserHandle.USER_ALL) return true;
Chris Wren0c4eeb42016-05-12 13:21:08 -0400873 if (this.isSystem) return true;
John Spurlock7340fc82014-04-24 18:50:12 -0400874 if (nid == UserHandle.USER_ALL || nid == this.userid) return true;
875 return supportsProfiles() && mUserProfiles.isCurrentProfile(nid);
876 }
877
878 public boolean supportsProfiles() {
Dianne Hackborn955d8d62014-10-07 20:17:19 -0700879 return targetSdkVersion >= Build.VERSION_CODES.LOLLIPOP;
John Spurlock7340fc82014-04-24 18:50:12 -0400880 }
881
882 @Override
883 public void binderDied() {
John Spurlocke77bb362014-04-26 10:24:59 -0400884 if (DEBUG) Slog.d(TAG, "binderDied");
John Spurlock7340fc82014-04-24 18:50:12 -0400885 // Remove the service, but don't unbind from the service. The system will bring the
886 // service back up, and the onServiceConnected handler will readd the service with the
887 // new binding. If this isn't a bound service, and is just a registered
888 // service, just removing it from the list is all we need to do anyway.
889 removeServiceImpl(this.service, this.userid);
890 }
891
892 /** convenience method for looking in mEnabledServicesForCurrentProfiles */
893 public boolean isEnabledForCurrentProfiles() {
894 if (this.isSystem) return true;
895 if (this.connection == null) return false;
896 return mEnabledServicesForCurrentProfiles.contains(this.component);
897 }
898 }
899
Chris Wrenab41eec2016-01-04 18:01:27 -0500900 /** convenience method for looking in mEnabledServicesForCurrentProfiles */
901 public boolean isComponentEnabledForCurrentProfiles(ComponentName component) {
902 return mEnabledServicesForCurrentProfiles.contains(component);
903 }
904
John Spurlock7340fc82014-04-24 18:50:12 -0400905 public static class UserProfiles {
906 // Profiles of the current user.
Ruben Brunke24b9a62016-02-16 21:38:24 -0800907 private final SparseArray<UserInfo> mCurrentProfiles = new SparseArray<>();
John Spurlock7340fc82014-04-24 18:50:12 -0400908
Ruben Brunke24b9a62016-02-16 21:38:24 -0800909 public void updateCache(@NonNull Context context) {
John Spurlock7340fc82014-04-24 18:50:12 -0400910 UserManager userManager = (UserManager) context.getSystemService(Context.USER_SERVICE);
911 if (userManager != null) {
912 int currentUserId = ActivityManager.getCurrentUser();
913 List<UserInfo> profiles = userManager.getProfiles(currentUserId);
914 synchronized (mCurrentProfiles) {
915 mCurrentProfiles.clear();
916 for (UserInfo user : profiles) {
917 mCurrentProfiles.put(user.id, user);
918 }
919 }
920 }
921 }
922
923 public int[] getCurrentProfileIds() {
924 synchronized (mCurrentProfiles) {
925 int[] users = new int[mCurrentProfiles.size()];
926 final int N = mCurrentProfiles.size();
927 for (int i = 0; i < N; ++i) {
928 users[i] = mCurrentProfiles.keyAt(i);
929 }
930 return users;
931 }
932 }
933
934 public boolean isCurrentProfile(int userId) {
935 synchronized (mCurrentProfiles) {
936 return mCurrentProfiles.get(userId) != null;
937 }
938 }
939 }
940
Ruben Brunke24b9a62016-02-16 21:38:24 -0800941 public static class Config {
942 public String caption;
943 public String serviceInterface;
944 public String secureSettingName;
Julia Reynolds6e839b02016-04-13 10:01:17 -0400945 public String secondarySettingName;
Ruben Brunke24b9a62016-02-16 21:38:24 -0800946 public String bindPermission;
947 public String settingsAction;
948 public int clientLabel;
John Spurlock7340fc82014-04-24 18:50:12 -0400949 }
950}