2 * Copyright (c) 2014, The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package com.android.server.notification;
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 import static android.content.Context.DEVICE_POLICY_SERVICE;
24 import android.annotation.NonNull;
25 import android.app.ActivityManager;
26 import android.app.PendingIntent;
27 import android.app.admin.DevicePolicyManager;
28 import android.content.BroadcastReceiver;
29 import android.content.ComponentName;
30 import android.content.ContentResolver;
31 import android.content.Context;
32 import android.content.Intent;
33 import android.content.IntentFilter;
34 import android.content.ServiceConnection;
35 import android.content.pm.ApplicationInfo;
36 import android.content.pm.IPackageManager;
37 import android.content.pm.PackageManager;
38 import android.content.pm.PackageManager.NameNotFoundException;
39 import android.content.pm.ResolveInfo;
40 import android.content.pm.ServiceInfo;
41 import android.content.pm.UserInfo;
42 import android.database.ContentObserver;
43 import android.net.Uri;
44 import android.os.Binder;
45 import android.os.Build;
46 import android.os.Handler;
47 import android.os.IBinder;
48 import android.os.IInterface;
49 import android.os.RemoteException;
50 import android.os.ServiceManager;
51 import android.os.UserHandle;
52 import android.os.UserManager;
53 import android.provider.Settings;
54 import android.text.TextUtils;
55 import android.util.ArraySet;
56 import android.util.Log;
57 import android.util.Slog;
58 import android.util.SparseArray;
60 import com.android.server.notification.NotificationManagerService.DumpFilter;
62 import java.io.PrintWriter;
63 import java.util.ArrayList;
64 import java.util.Arrays;
65 import java.util.HashSet;
66 import java.util.List;
67 import java.util.Objects;
71 * Manages the lifecycle of application-provided services bound by system server.
73 * Services managed by this helper must have:
74 * - An associated system settings value with a list of enabled component names.
75 * - A well-known action for services to use in their intent-filter.
76 * - A system permission for services to require in order to ensure system has exclusive binding.
77 * - A settings page for user configuration of enabled services, and associated intent action.
78 * - A remote interface definition (aidl) provided by the service used for communication.
80 abstract public class ManagedServices {
81 protected final String TAG = getClass().getSimpleName();
82 protected final boolean DEBUG = Log.isLoggable(TAG, Log.DEBUG);
84 protected static final String ENABLED_SERVICES_SEPARATOR = ":";
86 protected final Context mContext;
87 protected final Object mMutex;
88 private final UserProfiles mUserProfiles;
89 private final SettingsObserver mSettingsObserver;
90 private final IPackageManager mPm;
91 private final Config mConfig;
92 private ArraySet<String> mRestored;
94 // contains connections to all connected services, including app services
95 // and system services
96 private final ArrayList<ManagedServiceInfo> mServices = new ArrayList<ManagedServiceInfo>();
97 // things that will be put into mServices as soon as they're ready
98 private final ArrayList<String> mServicesBinding = new ArrayList<String>();
99 // lists the component names of all enabled (and therefore potentially connected)
100 // app services for current profiles.
101 private ArraySet<ComponentName> mEnabledServicesForCurrentProfiles
102 = new ArraySet<ComponentName>();
103 // Just the packages from mEnabledServicesForCurrentProfiles
104 private ArraySet<String> mEnabledServicesPackageNames = new ArraySet<String>();
105 // List of packages in restored setting across all mUserProfiles, for quick
106 // filtering upon package updates.
107 private ArraySet<String> mRestoredPackages = new ArraySet<>();
108 // List of enabled packages that have nevertheless asked not to be run
109 private ArraySet<ComponentName> mSnoozingForCurrentProfiles = new ArraySet<>();
112 // Kept to de-dupe user change events (experienced after boot, when we receive a settings and a
114 private int[] mLastSeenProfileIds;
116 private final BroadcastReceiver mRestoreReceiver;
118 public ManagedServices(Context context, Handler handler, Object mutex,
119 UserProfiles userProfiles) {
122 mUserProfiles = userProfiles;
123 mPm = IPackageManager.Stub.asInterface(ServiceManager.getService("package"));
124 mConfig = getConfig();
125 mSettingsObserver = new SettingsObserver(handler);
127 mRestoreReceiver = new SettingRestoredReceiver();
128 IntentFilter filter = new IntentFilter(Intent.ACTION_SETTING_RESTORED);
129 context.registerReceiver(mRestoreReceiver, filter);
130 rebuildRestoredPackages();
133 class SettingRestoredReceiver extends BroadcastReceiver {
135 public void onReceive(Context context, Intent intent) {
136 if (Intent.ACTION_SETTING_RESTORED.equals(intent.getAction())) {
137 String element = intent.getStringExtra(Intent.EXTRA_SETTING_NAME);
138 if (Objects.equals(element, mConfig.secureSettingName)
139 || Objects.equals(element, mConfig.secondarySettingName)) {
140 String prevValue = intent.getStringExtra(Intent.EXTRA_SETTING_PREVIOUS_VALUE);
141 String newValue = intent.getStringExtra(Intent.EXTRA_SETTING_NEW_VALUE);
142 settingRestored(element, prevValue, newValue, getSendingUserId());
148 abstract protected Config getConfig();
150 private String getCaption() {
151 return mConfig.caption;
154 abstract protected IInterface asInterface(IBinder binder);
156 abstract protected boolean checkType(IInterface service);
158 abstract protected void onServiceAdded(ManagedServiceInfo info);
160 protected List<ManagedServiceInfo> getServices() {
161 synchronized (mMutex) {
162 List<ManagedServiceInfo> services = new ArrayList<>(mServices);
167 protected void onServiceRemovedLocked(ManagedServiceInfo removed) { }
169 private ManagedServiceInfo newServiceInfo(IInterface service,
170 ComponentName component, int userid, boolean isSystem, ServiceConnection connection,
171 int targetSdkVersion) {
172 return new ManagedServiceInfo(service, component, userid, isSystem, connection,
176 public void onBootPhaseAppsCanStart() {
177 mSettingsObserver.observe();
180 public void dump(PrintWriter pw, DumpFilter filter) {
181 pw.println(" All " + getCaption() + "s (" + mEnabledServicesForCurrentProfiles.size()
182 + ") enabled for current profiles:");
183 for (ComponentName cmpt : mEnabledServicesForCurrentProfiles) {
184 if (filter != null && !filter.matches(cmpt)) continue;
185 pw.println(" " + cmpt);
188 pw.println(" Live " + getCaption() + "s (" + mServices.size() + "):");
189 for (ManagedServiceInfo info : mServices) {
190 if (filter != null && !filter.matches(info.component)) continue;
191 pw.println(" " + info.component
192 + " (user " + info.userid + "): " + info.service
193 + (info.isSystem?" SYSTEM":"")
194 + (info.isGuest(this)?" GUEST":""));
197 pw.println(" Snoozed " + getCaption() + "s (" +
198 mSnoozingForCurrentProfiles.size() + "):");
199 for (ComponentName name : mSnoozingForCurrentProfiles) {
200 pw.println(" " + name.flattenToShortString());
204 // By convention, restored settings are replicated to another settings
205 // entry, named similarly but with a disambiguation suffix.
206 public static String restoredSettingName(String setting) {
207 return setting + ":restored";
210 // The OS has done a restore of this service's saved state. We clone it to the
211 // 'restored' reserve, and then once we return and the actual write to settings is
212 // performed, our observer will do the work of maintaining the restored vs live
214 public void settingRestored(String element, String oldValue, String newValue, int userid) {
215 if (DEBUG) Slog.d(TAG, "Restored managed service setting: " + element
216 + " ovalue=" + oldValue + " nvalue=" + newValue);
217 if (mConfig.secureSettingName.equals(element) ||
218 mConfig.secondarySettingName.equals(element)) {
219 if (element != null) {
220 Settings.Secure.putStringForUser(mContext.getContentResolver(),
221 restoredSettingName(element),
224 if (mConfig.secureSettingName.equals(element)) {
225 updateSettingsAccordingToInstalledServices(element, userid);
227 rebuildRestoredPackages();
232 public boolean isComponentEnabledForPackage(String pkg) {
233 return mEnabledServicesPackageNames.contains(pkg);
236 public void onPackagesChanged(boolean removingPackage, String[] pkgList) {
237 if (DEBUG) Slog.d(TAG, "onPackagesChanged removingPackage=" + removingPackage
238 + " pkgList=" + (pkgList == null ? null : Arrays.asList(pkgList))
239 + " mEnabledServicesPackageNames=" + mEnabledServicesPackageNames);
240 boolean anyServicesInvolved = false;
242 if (pkgList != null && (pkgList.length > 0)) {
243 for (String pkgName : pkgList) {
244 if (mEnabledServicesPackageNames.contains(pkgName) ||
245 mRestoredPackages.contains(pkgName)) {
246 anyServicesInvolved = true;
251 if (anyServicesInvolved) {
252 // if we're not replacing a package, clean up orphaned bits
253 if (removingPackage) {
254 updateSettingsAccordingToInstalledServices();
255 rebuildRestoredPackages();
257 // make sure we're still bound to any of our services who may have just upgraded
258 rebindServices(false);
262 public void onUserSwitched(int user) {
263 if (DEBUG) Slog.d(TAG, "onUserSwitched u=" + user);
264 rebuildRestoredPackages();
265 if (Arrays.equals(mLastSeenProfileIds, mUserProfiles.getCurrentProfileIds())) {
266 if (DEBUG) Slog.d(TAG, "Current profile IDs didn't change, skipping rebindServices().");
269 rebindServices(true);
272 public void onUserUnlocked(int user) {
273 if (DEBUG) Slog.d(TAG, "onUserUnlocked u=" + user);
274 rebuildRestoredPackages();
275 rebindServices(false);
278 public ManagedServiceInfo getServiceFromTokenLocked(IInterface service) {
279 if (service == null) {
282 final IBinder token = service.asBinder();
283 final int N = mServices.size();
284 for (int i = 0; i < N; i++) {
285 final ManagedServiceInfo info = mServices.get(i);
286 if (info.service.asBinder() == token) return info;
291 public ManagedServiceInfo checkServiceTokenLocked(IInterface service) {
292 checkNotNull(service);
293 ManagedServiceInfo info = getServiceFromTokenLocked(service);
297 throw new SecurityException("Disallowed call from unknown " + getCaption() + ": "
301 public void unregisterService(IInterface service, int userid) {
302 checkNotNull(service);
303 // no need to check permissions; if your service binder is in the list,
304 // that's proof that you had permission to add it in the first place
305 unregisterServiceImpl(service, userid);
308 public void registerService(IInterface service, ComponentName component, int userid) {
309 checkNotNull(service);
310 ManagedServiceInfo info = registerServiceImpl(service, component, userid);
312 onServiceAdded(info);
317 * Add a service to our callbacks. The lifecycle of this service is managed externally,
318 * but unlike a system service, it should not be considered privledged.
320 public void registerGuestService(ManagedServiceInfo guest) {
321 checkNotNull(guest.service);
322 if (!checkType(guest.service)) {
323 throw new IllegalArgumentException();
325 if (registerServiceImpl(guest) != null) {
326 onServiceAdded(guest);
330 public void setComponentState(ComponentName component, boolean enabled) {
331 boolean previous = !mSnoozingForCurrentProfiles.contains(component);
332 if (previous == enabled) {
337 mSnoozingForCurrentProfiles.remove(component);
339 mSnoozingForCurrentProfiles.add(component);
344 Slog.d(TAG, ((enabled) ? "Enabling " : "Disabling ") + "component " +
345 component.flattenToShortString());
349 synchronized (mMutex) {
350 final int[] userIds = mUserProfiles.getCurrentProfileIds();
352 for (int userId : userIds) {
354 registerServiceLocked(component, userId);
356 unregisterServiceLocked(component, userId);
362 private void rebuildRestoredPackages() {
363 mRestoredPackages.clear();
364 String secureSettingName = restoredSettingName(mConfig.secureSettingName);
365 String secondarySettingName = mConfig.secondarySettingName == null
366 ? null : restoredSettingName(mConfig.secondarySettingName);
367 int[] userIds = mUserProfiles.getCurrentProfileIds();
368 final int N = userIds.length;
369 for (int i = 0; i < N; ++i) {
370 ArraySet<ComponentName> names =
371 loadComponentNamesFromSetting(secureSettingName, userIds[i]);
372 if (secondarySettingName != null) {
373 names.addAll(loadComponentNamesFromSetting(secondarySettingName, userIds[i]));
375 for (ComponentName name : names) {
376 mRestoredPackages.add(name.getPackageName());
382 protected @NonNull ArraySet<ComponentName> loadComponentNamesFromSetting(String settingName,
384 final ContentResolver cr = mContext.getContentResolver();
385 String settingValue = Settings.Secure.getStringForUser(
389 if (TextUtils.isEmpty(settingValue))
390 return new ArraySet<>();
391 String[] restored = settingValue.split(ENABLED_SERVICES_SEPARATOR);
392 ArraySet<ComponentName> result = new ArraySet<>(restored.length);
393 for (int i = 0; i < restored.length; i++) {
394 ComponentName value = ComponentName.unflattenFromString(restored[i]);
402 private void storeComponentsToSetting(Set<ComponentName> components,
405 String[] componentNames = null;
406 if (null != components) {
407 componentNames = new String[components.size()];
409 for (ComponentName c: components) {
410 componentNames[index++] = c.flattenToString();
413 final String value = (componentNames == null) ? "" :
414 TextUtils.join(ENABLED_SERVICES_SEPARATOR, componentNames);
415 final ContentResolver cr = mContext.getContentResolver();
416 Settings.Secure.putStringForUser(
424 * Remove access for any services that no longer exist.
426 private void updateSettingsAccordingToInstalledServices() {
427 int[] userIds = mUserProfiles.getCurrentProfileIds();
428 final int N = userIds.length;
429 for (int i = 0; i < N; ++i) {
430 updateSettingsAccordingToInstalledServices(mConfig.secureSettingName, userIds[i]);
431 if (mConfig.secondarySettingName != null) {
432 updateSettingsAccordingToInstalledServices(
433 mConfig.secondarySettingName, userIds[i]);
436 rebuildRestoredPackages();
439 protected Set<ComponentName> queryPackageForServices(String packageName, int userId) {
440 Set<ComponentName> installed = new ArraySet<>();
441 final PackageManager pm = mContext.getPackageManager();
442 Intent queryIntent = new Intent(mConfig.serviceInterface);
443 if (!TextUtils.isEmpty(packageName)) {
444 queryIntent.setPackage(packageName);
446 List<ResolveInfo> installedServices = pm.queryIntentServicesAsUser(
448 PackageManager.GET_SERVICES | PackageManager.GET_META_DATA,
451 Slog.v(TAG, mConfig.serviceInterface + " services: " + installedServices);
452 if (installedServices != null) {
453 for (int i = 0, count = installedServices.size(); i < count; i++) {
454 ResolveInfo resolveInfo = installedServices.get(i);
455 ServiceInfo info = resolveInfo.serviceInfo;
457 ComponentName component = new ComponentName(info.packageName, info.name);
458 if (!mConfig.bindPermission.equals(info.permission)) {
459 Slog.w(TAG, "Skipping " + getCaption() + " service "
460 + info.packageName + "/" + info.name
461 + ": it does not require the permission "
462 + mConfig.bindPermission);
465 installed.add(component);
471 private void updateSettingsAccordingToInstalledServices(String setting, int userId) {
472 boolean restoredChanged = false;
473 boolean currentChanged = false;
474 Set<ComponentName> restored =
475 loadComponentNamesFromSetting(restoredSettingName(setting), userId);
476 Set<ComponentName> current =
477 loadComponentNamesFromSetting(setting, userId);
478 // Load all services for all packages.
479 Set<ComponentName> installed = queryPackageForServices(null, userId);
481 ArraySet<ComponentName> retained = new ArraySet<>();
483 for (ComponentName component : installed) {
484 if (null != restored) {
485 boolean wasRestored = restored.remove(component);
487 // Freshly installed package has service that was mentioned in restored setting.
489 Slog.v(TAG, "Restoring " + component + " for user " + userId);
490 restoredChanged = true;
491 currentChanged = true;
492 retained.add(component);
497 if (null != current) {
498 if (current.contains(component))
499 retained.add(component);
503 currentChanged |= ((current == null ? 0 : current.size()) != retained.size());
505 if (currentChanged) {
506 if (DEBUG) Slog.v(TAG, "List of " + getCaption() + " services was updated " + current);
507 storeComponentsToSetting(retained, setting, userId);
510 if (restoredChanged) {
511 if (DEBUG) Slog.v(TAG,
512 "List of " + getCaption() + " restored services was updated " + restored);
513 storeComponentsToSetting(restored, restoredSettingName(setting), userId);
518 * Called whenever packages change, the user switches, or the secure setting
519 * is altered. (For example in response to USER_SWITCHED in our broadcast receiver)
521 private void rebindServices(boolean forceRebind) {
522 if (DEBUG) Slog.d(TAG, "rebindServices");
523 final int[] userIds = mUserProfiles.getCurrentProfileIds();
524 final int nUserIds = userIds.length;
526 final SparseArray<ArraySet<ComponentName>> componentsByUser = new SparseArray<>();
528 for (int i = 0; i < nUserIds; ++i) {
529 componentsByUser.put(userIds[i],
530 loadComponentNamesFromSetting(mConfig.secureSettingName, userIds[i]));
531 if (mConfig.secondarySettingName != null) {
532 componentsByUser.get(userIds[i]).addAll(
533 loadComponentNamesFromSetting(mConfig.secondarySettingName, userIds[i]));
537 final ArrayList<ManagedServiceInfo> removableBoundServices = new ArrayList<>();
538 final SparseArray<Set<ComponentName>> toAdd = new SparseArray<>();
540 synchronized (mMutex) {
541 // Rebind to non-system services if user switched
542 for (ManagedServiceInfo service : mServices) {
543 if (!service.isSystem && !service.isGuest(this)) {
544 removableBoundServices.add(service);
548 mEnabledServicesForCurrentProfiles.clear();
549 mEnabledServicesPackageNames.clear();
551 for (int i = 0; i < nUserIds; ++i) {
552 // decode the list of components
553 final ArraySet<ComponentName> userComponents = componentsByUser.get(userIds[i]);
554 if (null == userComponents) {
555 toAdd.put(userIds[i], new ArraySet<ComponentName>());
559 final Set<ComponentName> add = new HashSet<>(userComponents);
560 add.removeAll(mSnoozingForCurrentProfiles);
562 toAdd.put(userIds[i], add);
564 mEnabledServicesForCurrentProfiles.addAll(userComponents);
566 for (int j = 0; j < userComponents.size(); j++) {
567 final ComponentName component = userComponents.valueAt(j);
568 mEnabledServicesPackageNames.add(component.getPackageName());
573 for (ManagedServiceInfo info : removableBoundServices) {
574 final ComponentName component = info.component;
575 final int oldUser = info.userid;
576 final Set<ComponentName> allowedComponents = toAdd.get(info.userid);
577 if (allowedComponents != null) {
578 if (allowedComponents.contains(component) && !forceRebind) {
579 // Already bound, don't need to bind again.
580 allowedComponents.remove(component);
582 // No longer allowed to be bound, or must rebind.
583 Slog.v(TAG, "disabling " + getCaption() + " for user "
584 + oldUser + ": " + component);
585 unregisterService(component, oldUser);
590 for (int i = 0; i < nUserIds; ++i) {
591 final Set<ComponentName> add = toAdd.get(userIds[i]);
592 for (ComponentName component : add) {
594 ServiceInfo info = mPm.getServiceInfo(component,
595 PackageManager.MATCH_DIRECT_BOOT_AWARE
596 | PackageManager.MATCH_DIRECT_BOOT_UNAWARE, userIds[i]);
597 if (info == null || !mConfig.bindPermission.equals(info.permission)) {
598 Slog.w(TAG, "Skipping " + getCaption() + " service " + component
599 + ": it does not require the permission " + mConfig.bindPermission);
603 "enabling " + getCaption() + " for " + userIds[i] + ": " + component);
604 registerService(component, userIds[i]);
605 } catch (RemoteException e) {
606 e.rethrowFromSystemServer();
611 mLastSeenProfileIds = userIds;
615 * Version of registerService that takes the name of a service component to bind to.
617 private void registerService(final ComponentName name, final int userid) {
618 synchronized (mMutex) {
619 registerServiceLocked(name, userid);
624 * Inject a system service into the management list.
626 public void registerSystemService(final ComponentName name, final int userid) {
627 synchronized (mMutex) {
628 registerServiceLocked(name, userid, true /* isSystem */);
632 private void registerServiceLocked(final ComponentName name, final int userid) {
633 registerServiceLocked(name, userid, false /* isSystem */);
636 private void registerServiceLocked(final ComponentName name, final int userid,
637 final boolean isSystem) {
638 if (DEBUG) Slog.v(TAG, "registerService: " + name + " u=" + userid);
640 final String servicesBindingTag = name.toString() + "/" + userid;
641 if (mServicesBinding.contains(servicesBindingTag)) {
642 // stop registering this thing already! we're working on it
645 mServicesBinding.add(servicesBindingTag);
647 final int N = mServices.size();
648 for (int i = N - 1; i >= 0; i--) {
649 final ManagedServiceInfo info = mServices.get(i);
650 if (name.equals(info.component)
651 && info.userid == userid) {
652 // cut old connections
653 if (DEBUG) Slog.v(TAG, " disconnecting old " + getCaption() + ": "
655 removeServiceLocked(i);
656 if (info.connection != null) {
657 mContext.unbindService(info.connection);
662 Intent intent = new Intent(mConfig.serviceInterface);
663 intent.setComponent(name);
665 intent.putExtra(Intent.EXTRA_CLIENT_LABEL, mConfig.clientLabel);
667 final PendingIntent pendingIntent = PendingIntent.getActivity(
668 mContext, 0, new Intent(mConfig.settingsAction), 0);
669 intent.putExtra(Intent.EXTRA_CLIENT_INTENT, pendingIntent);
671 ApplicationInfo appInfo = null;
673 appInfo = mContext.getPackageManager().getApplicationInfo(
674 name.getPackageName(), 0);
675 } catch (NameNotFoundException e) {
676 // Ignore if the package doesn't exist we won't be able to bind to the service.
678 final int targetSdkVersion =
679 appInfo != null ? appInfo.targetSdkVersion : Build.VERSION_CODES.BASE;
682 if (DEBUG) Slog.v(TAG, "binding: " + intent);
683 ServiceConnection serviceConnection = new ServiceConnection() {
687 public void onServiceConnected(ComponentName name, IBinder binder) {
688 boolean added = false;
689 ManagedServiceInfo info = null;
690 synchronized (mMutex) {
691 mServicesBinding.remove(servicesBindingTag);
693 mService = asInterface(binder);
694 info = newServiceInfo(mService, name,
695 userid, isSystem, this, targetSdkVersion);
696 binder.linkToDeath(info, 0);
697 added = mServices.add(info);
698 } catch (RemoteException e) {
703 onServiceAdded(info);
708 public void onServiceDisconnected(ComponentName name) {
709 Slog.v(TAG, getCaption() + " connection lost: " + name);
712 if (!mContext.bindServiceAsUser(intent,
714 BIND_AUTO_CREATE | BIND_FOREGROUND_SERVICE | BIND_ALLOW_WHITELIST_MANAGEMENT,
715 new UserHandle(userid))) {
716 mServicesBinding.remove(servicesBindingTag);
717 Slog.w(TAG, "Unable to bind " + getCaption() + " service: " + intent);
720 } catch (SecurityException ex) {
721 Slog.e(TAG, "Unable to bind " + getCaption() + " service: " + intent, ex);
727 * Remove a service for the given user by ComponentName
729 private void unregisterService(ComponentName name, int userid) {
730 synchronized (mMutex) {
731 unregisterServiceLocked(name, userid);
735 private void unregisterServiceLocked(ComponentName name, int userid) {
736 final int N = mServices.size();
737 for (int i = N - 1; i >= 0; i--) {
738 final ManagedServiceInfo info = mServices.get(i);
739 if (name.equals(info.component)
740 && info.userid == userid) {
741 removeServiceLocked(i);
742 if (info.connection != null) {
744 mContext.unbindService(info.connection);
745 } catch (IllegalArgumentException ex) {
746 // something happened to the service: we think we have a connection
748 Slog.e(TAG, getCaption() + " " + name + " could not be unbound: " + ex);
756 * Removes a service from the list but does not unbind
758 * @return the removed service.
760 private ManagedServiceInfo removeServiceImpl(IInterface service, final int userid) {
761 if (DEBUG) Slog.d(TAG, "removeServiceImpl service=" + service + " u=" + userid);
762 ManagedServiceInfo serviceInfo = null;
763 synchronized (mMutex) {
764 final int N = mServices.size();
765 for (int i = N - 1; i >= 0; i--) {
766 final ManagedServiceInfo info = mServices.get(i);
767 if (info.service.asBinder() == service.asBinder()
768 && info.userid == userid) {
769 if (DEBUG) Slog.d(TAG, "Removing active service " + info.component);
770 serviceInfo = removeServiceLocked(i);
777 private ManagedServiceInfo removeServiceLocked(int i) {
778 final ManagedServiceInfo info = mServices.remove(i);
779 onServiceRemovedLocked(info);
783 private void checkNotNull(IInterface service) {
784 if (service == null) {
785 throw new IllegalArgumentException(getCaption() + " must not be null");
789 private ManagedServiceInfo registerServiceImpl(final IInterface service,
790 final ComponentName component, final int userid) {
791 ManagedServiceInfo info = newServiceInfo(service, component, userid,
792 true /*isSystem*/, null /*connection*/, Build.VERSION_CODES.LOLLIPOP);
793 return registerServiceImpl(info);
796 private ManagedServiceInfo registerServiceImpl(ManagedServiceInfo info) {
797 synchronized (mMutex) {
799 info.service.asBinder().linkToDeath(info, 0);
802 } catch (RemoteException e) {
810 * Removes a service from the list and unbinds.
812 private void unregisterServiceImpl(IInterface service, int userid) {
813 ManagedServiceInfo info = removeServiceImpl(service, userid);
814 if (info != null && info.connection != null && !info.isGuest(this)) {
815 mContext.unbindService(info.connection);
819 private class SettingsObserver extends ContentObserver {
820 private final Uri mSecureSettingsUri = Settings.Secure.getUriFor(mConfig.secureSettingName);
821 private final Uri mSecondarySettingsUri;
823 private SettingsObserver(Handler handler) {
825 if (mConfig.secondarySettingName != null) {
826 mSecondarySettingsUri = Settings.Secure.getUriFor(mConfig.secondarySettingName);
828 mSecondarySettingsUri = null;
832 private void observe() {
833 ContentResolver resolver = mContext.getContentResolver();
834 resolver.registerContentObserver(mSecureSettingsUri,
835 false, this, UserHandle.USER_ALL);
836 if (mSecondarySettingsUri != null) {
837 resolver.registerContentObserver(mSecondarySettingsUri,
838 false, this, UserHandle.USER_ALL);
844 public void onChange(boolean selfChange, Uri uri) {
848 private void update(Uri uri) {
849 if (uri == null || mSecureSettingsUri.equals(uri)
850 || uri.equals(mSecondarySettingsUri)) {
851 if (DEBUG) Slog.d(TAG, "Setting changed: uri=" + uri);
852 rebindServices(false);
853 rebuildRestoredPackages();
858 public class ManagedServiceInfo implements IBinder.DeathRecipient {
859 public IInterface service;
860 public ComponentName component;
862 public boolean isSystem;
863 public ServiceConnection connection;
864 public int targetSdkVersion;
866 public ManagedServiceInfo(IInterface service, ComponentName component,
867 int userid, boolean isSystem, ServiceConnection connection, int targetSdkVersion) {
868 this.service = service;
869 this.component = component;
870 this.userid = userid;
871 this.isSystem = isSystem;
872 this.connection = connection;
873 this.targetSdkVersion = targetSdkVersion;
876 public boolean isGuest(ManagedServices host) {
877 return ManagedServices.this != host;
880 public ManagedServices getOwner() {
881 return ManagedServices.this;
885 public String toString() {
886 return new StringBuilder("ManagedServiceInfo[")
887 .append("component=").append(component)
888 .append(",userid=").append(userid)
889 .append(",isSystem=").append(isSystem)
890 .append(",targetSdkVersion=").append(targetSdkVersion)
891 .append(",connection=").append(connection == null ? null : "<connection>")
892 .append(",service=").append(service)
893 .append(']').toString();
896 public boolean enabledAndUserMatches(int nid) {
897 if (!isEnabledForCurrentProfiles()) {
900 if (this.userid == UserHandle.USER_ALL) return true;
901 if (this.isSystem) return true;
902 if (nid == UserHandle.USER_ALL || nid == this.userid) return true;
903 return supportsProfiles()
904 && mUserProfiles.isCurrentProfile(nid)
905 && isPermittedForProfile(nid);
908 public boolean supportsProfiles() {
909 return targetSdkVersion >= Build.VERSION_CODES.LOLLIPOP;
913 public void binderDied() {
914 if (DEBUG) Slog.d(TAG, "binderDied");
915 // Remove the service, but don't unbind from the service. The system will bring the
916 // service back up, and the onServiceConnected handler will read the service with the
917 // new binding. If this isn't a bound service, and is just a registered
918 // service, just removing it from the list is all we need to do anyway.
919 removeServiceImpl(this.service, this.userid);
922 /** convenience method for looking in mEnabledServicesForCurrentProfiles */
923 public boolean isEnabledForCurrentProfiles() {
924 if (this.isSystem) return true;
925 if (this.connection == null) return false;
926 return mEnabledServicesForCurrentProfiles.contains(this.component);
930 * Returns true if this service is allowed to receive events for the given userId. A
931 * managed profile owner can disallow non-system services running outside of the profile
932 * from receiving events from the profile.
934 public boolean isPermittedForProfile(int userId) {
935 if (!mUserProfiles.isManagedProfile(userId)) {
938 DevicePolicyManager dpm =
939 (DevicePolicyManager) mContext.getSystemService(DEVICE_POLICY_SERVICE);
940 final long identity = Binder.clearCallingIdentity();
942 return dpm.isNotificationListenerServicePermitted(
943 component.getPackageName(), userId);
945 Binder.restoreCallingIdentity(identity);
950 /** convenience method for looking in mEnabledServicesForCurrentProfiles */
951 public boolean isComponentEnabledForCurrentProfiles(ComponentName component) {
952 return mEnabledServicesForCurrentProfiles.contains(component);
955 public static class UserProfiles {
956 // Profiles of the current user.
957 private final SparseArray<UserInfo> mCurrentProfiles = new SparseArray<>();
959 public void updateCache(@NonNull Context context) {
960 UserManager userManager = (UserManager) context.getSystemService(Context.USER_SERVICE);
961 if (userManager != null) {
962 int currentUserId = ActivityManager.getCurrentUser();
963 List<UserInfo> profiles = userManager.getProfiles(currentUserId);
964 synchronized (mCurrentProfiles) {
965 mCurrentProfiles.clear();
966 for (UserInfo user : profiles) {
967 mCurrentProfiles.put(user.id, user);
973 public int[] getCurrentProfileIds() {
974 synchronized (mCurrentProfiles) {
975 int[] users = new int[mCurrentProfiles.size()];
976 final int N = mCurrentProfiles.size();
977 for (int i = 0; i < N; ++i) {
978 users[i] = mCurrentProfiles.keyAt(i);
984 public boolean isCurrentProfile(int userId) {
985 synchronized (mCurrentProfiles) {
986 return mCurrentProfiles.get(userId) != null;
990 public boolean isManagedProfile(int userId) {
991 synchronized (mCurrentProfiles) {
992 UserInfo user = mCurrentProfiles.get(userId);
993 return user != null && user.isManagedProfile();
998 public static class Config {
999 public String caption;
1000 public String serviceInterface;
1001 public String secureSettingName;
1002 public String secondarySettingName;
1003 public String bindPermission;
1004 public String settingsAction;
1005 public int clientLabel;