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 android.app.ActivityManager;
20 import android.app.PendingIntent;
21 import android.content.BroadcastReceiver;
22 import android.content.ComponentName;
23 import android.content.ContentResolver;
24 import android.content.Context;
25 import android.content.Intent;
26 import android.content.IntentFilter;
27 import android.content.ServiceConnection;
28 import android.content.pm.ApplicationInfo;
29 import android.content.pm.PackageManager;
30 import android.content.pm.PackageManager.NameNotFoundException;
31 import android.content.pm.ResolveInfo;
32 import android.content.pm.ServiceInfo;
33 import android.content.pm.UserInfo;
34 import android.database.ContentObserver;
35 import android.net.Uri;
36 import android.os.Build;
37 import android.os.Handler;
38 import android.os.IBinder;
39 import android.os.IInterface;
40 import android.os.RemoteException;
41 import android.os.UserHandle;
42 import android.os.UserManager;
43 import android.provider.Settings;
44 import android.text.TextUtils;
45 import android.util.ArraySet;
46 import android.util.Log;
47 import android.util.Slog;
48 import android.util.SparseArray;
50 import com.android.server.notification.NotificationManagerService.DumpFilter;
52 import java.io.PrintWriter;
53 import java.util.ArrayList;
54 import java.util.Arrays;
55 import java.util.List;
56 import java.util.Objects;
60 * Manages the lifecycle of application-provided services bound by system server.
62 * Services managed by this helper must have:
63 * - An associated system settings value with a list of enabled component names.
64 * - A well-known action for services to use in their intent-filter.
65 * - A system permission for services to require in order to ensure system has exclusive binding.
66 * - A settings page for user configuration of enabled services, and associated intent action.
67 * - A remote interface definition (aidl) provided by the service used for communication.
69 abstract public class ManagedServices {
70 protected final String TAG = getClass().getSimpleName();
71 protected final boolean DEBUG = Log.isLoggable(TAG, Log.DEBUG);
73 private static final String ENABLED_SERVICES_SEPARATOR = ":";
75 protected final Context mContext;
76 protected final Object mMutex;
77 private final UserProfiles mUserProfiles;
78 private final SettingsObserver mSettingsObserver;
79 private final Config mConfig;
80 private ArraySet<String> mRestored;
82 // contains connections to all connected services, including app services
83 // and system services
84 protected final ArrayList<ManagedServiceInfo> mServices = new ArrayList<ManagedServiceInfo>();
85 // things that will be put into mServices as soon as they're ready
86 private final ArrayList<String> mServicesBinding = new ArrayList<String>();
87 // lists the component names of all enabled (and therefore connected)
88 // app services for current profiles.
89 private ArraySet<ComponentName> mEnabledServicesForCurrentProfiles
90 = new ArraySet<ComponentName>();
91 // Just the packages from mEnabledServicesForCurrentProfiles
92 private ArraySet<String> mEnabledServicesPackageNames = new ArraySet<String>();
94 // Kept to de-dupe user change events (experienced after boot, when we receive a settings and a
96 private int[] mLastSeenProfileIds;
98 private final BroadcastReceiver mRestoreReceiver;
100 public ManagedServices(Context context, Handler handler, Object mutex,
101 UserProfiles userProfiles) {
104 mUserProfiles = userProfiles;
105 mConfig = getConfig();
106 mSettingsObserver = new SettingsObserver(handler);
108 mRestoreReceiver = new SettingRestoredReceiver();
109 IntentFilter filter = new IntentFilter(Intent.ACTION_SETTING_RESTORED);
110 context.registerReceiver(mRestoreReceiver, filter);
113 class SettingRestoredReceiver extends BroadcastReceiver {
115 public void onReceive(Context context, Intent intent) {
116 if (Intent.ACTION_SETTING_RESTORED.equals(intent.getAction())) {
117 String element = intent.getStringExtra(Intent.EXTRA_SETTING_NAME);
118 if (Objects.equals(element, mConfig.secureSettingName)) {
119 String prevValue = intent.getStringExtra(Intent.EXTRA_SETTING_PREVIOUS_VALUE);
120 String newValue = intent.getStringExtra(Intent.EXTRA_SETTING_NEW_VALUE);
121 settingRestored(element, prevValue, newValue, getSendingUserId());
127 abstract protected Config getConfig();
129 private String getCaption() {
130 return mConfig.caption;
133 abstract protected IInterface asInterface(IBinder binder);
135 abstract protected void onServiceAdded(ManagedServiceInfo info);
137 protected void onServiceRemovedLocked(ManagedServiceInfo removed) { }
139 private ManagedServiceInfo newServiceInfo(IInterface service,
140 ComponentName component, int userid, boolean isSystem, ServiceConnection connection,
141 int targetSdkVersion) {
142 return new ManagedServiceInfo(service, component, userid, isSystem, connection,
146 public void onBootPhaseAppsCanStart() {
147 mSettingsObserver.observe();
150 public void dump(PrintWriter pw, DumpFilter filter) {
151 pw.println(" All " + getCaption() + "s (" + mEnabledServicesForCurrentProfiles.size()
152 + ") enabled for current profiles:");
153 for (ComponentName cmpt : mEnabledServicesForCurrentProfiles) {
154 if (filter != null && !filter.matches(cmpt)) continue;
155 pw.println(" " + cmpt);
158 pw.println(" Live " + getCaption() + "s (" + mServices.size() + "):");
159 for (ManagedServiceInfo info : mServices) {
160 if (filter != null && !filter.matches(info.component)) continue;
161 pw.println(" " + info.component
162 + " (user " + info.userid + "): " + info.service
163 + (info.isSystem?" SYSTEM":""));
167 // By convention, restored settings are replicated to another settings
168 // entry, named similarly but with a disambiguation suffix.
169 public static final String restoredSettingName(Config config) {
170 return config.secureSettingName + ":restored";
173 // The OS has done a restore of this service's saved state. We clone it to the
174 // 'restored' reserve, and then once we return and the actual write to settings is
175 // performed, our observer will do the work of maintaining the restored vs live
177 public void settingRestored(String element, String oldValue, String newValue, int userid) {
178 if (DEBUG) Slog.d(TAG, "Restored managed service setting: " + element
179 + " ovalue=" + oldValue + " nvalue=" + newValue);
180 if (mConfig.secureSettingName.equals(element)) {
181 if (element != null) {
183 Settings.Secure.putStringForUser(mContext.getContentResolver(),
184 restoredSettingName(mConfig),
187 disableNonexistentServices(userid);
192 public boolean isComponentEnabledForPackage(String pkg) {
193 return mEnabledServicesPackageNames.contains(pkg);
196 public void onPackagesChanged(boolean queryReplace, String[] pkgList) {
197 if (DEBUG) Slog.d(TAG, "onPackagesChanged queryReplace=" + queryReplace
198 + " pkgList=" + (pkgList == null ? null : Arrays.asList(pkgList))
199 + " mEnabledServicesPackageNames=" + mEnabledServicesPackageNames);
200 boolean anyServicesInvolved = false;
201 if (pkgList != null && (pkgList.length > 0)) {
202 for (String pkgName : pkgList) {
203 if (mEnabledServicesPackageNames.contains(pkgName)) {
204 anyServicesInvolved = true;
209 if (anyServicesInvolved) {
210 // if we're not replacing a package, clean up orphaned bits
212 disableNonexistentServices();
214 // make sure we're still bound to any of our services who may have just upgraded
219 public void onUserSwitched(int user) {
220 if (DEBUG) Slog.d(TAG, "onUserSwitched u=" + user);
221 if (Arrays.equals(mLastSeenProfileIds, mUserProfiles.getCurrentProfileIds())) {
222 if (DEBUG) Slog.d(TAG, "Current profile IDs didn't change, skipping rebindServices().");
228 public ManagedServiceInfo checkServiceTokenLocked(IInterface service) {
229 checkNotNull(service);
230 final IBinder token = service.asBinder();
231 final int N = mServices.size();
232 for (int i=0; i<N; i++) {
233 final ManagedServiceInfo info = mServices.get(i);
234 if (info.service.asBinder() == token) return info;
236 throw new SecurityException("Disallowed call from unknown " + getCaption() + ": "
240 public void unregisterService(IInterface service, int userid) {
241 checkNotNull(service);
242 // no need to check permissions; if your service binder is in the list,
243 // that's proof that you had permission to add it in the first place
244 unregisterServiceImpl(service, userid);
247 public void registerService(IInterface service, ComponentName component, int userid) {
248 checkNotNull(service);
249 ManagedServiceInfo info = registerServiceImpl(service, component, userid);
251 onServiceAdded(info);
256 * Remove access for any services that no longer exist.
258 private void disableNonexistentServices() {
259 int[] userIds = mUserProfiles.getCurrentProfileIds();
260 final int N = userIds.length;
261 for (int i = 0 ; i < N; ++i) {
262 disableNonexistentServices(userIds[i]);
266 private void disableNonexistentServices(int userId) {
267 final ContentResolver cr = mContext.getContentResolver();
268 boolean restoredChanged = false;
269 if (mRestored == null) {
270 String restoredSetting = Settings.Secure.getStringForUser(
272 restoredSettingName(mConfig),
274 if (!TextUtils.isEmpty(restoredSetting)) {
275 if (DEBUG) Slog.d(TAG, "restored: " + restoredSetting);
276 String[] restored = restoredSetting.split(ENABLED_SERVICES_SEPARATOR);
277 mRestored = new ArraySet<String>(Arrays.asList(restored));
279 mRestored = new ArraySet<String>();
282 String flatIn = Settings.Secure.getStringForUser(
284 mConfig.secureSettingName,
286 if (!TextUtils.isEmpty(flatIn)) {
287 if (DEBUG) Slog.v(TAG, "flat before: " + flatIn);
288 PackageManager pm = mContext.getPackageManager();
289 List<ResolveInfo> installedServices = pm.queryIntentServicesAsUser(
290 new Intent(mConfig.serviceInterface),
291 PackageManager.GET_SERVICES | PackageManager.GET_META_DATA,
293 if (DEBUG) Slog.v(TAG, mConfig.serviceInterface + " services: " + installedServices);
294 Set<ComponentName> installed = new ArraySet<ComponentName>();
295 for (int i = 0, count = installedServices.size(); i < count; i++) {
296 ResolveInfo resolveInfo = installedServices.get(i);
297 ServiceInfo info = resolveInfo.serviceInfo;
299 ComponentName component = new ComponentName(info.packageName, info.name);
300 if (!mConfig.bindPermission.equals(info.permission)) {
301 Slog.w(TAG, "Skipping " + getCaption() + " service "
302 + info.packageName + "/" + info.name
303 + ": it does not require the permission "
304 + mConfig.bindPermission);
305 restoredChanged |= mRestored.remove(component.flattenToString());
308 installed.add(component);
312 if (!installed.isEmpty()) {
313 String[] enabled = flatIn.split(ENABLED_SERVICES_SEPARATOR);
314 ArrayList<String> remaining = new ArrayList<String>(enabled.length);
315 for (int i = 0; i < enabled.length; i++) {
316 ComponentName enabledComponent = ComponentName.unflattenFromString(enabled[i]);
317 if (installed.contains(enabledComponent)) {
318 remaining.add(enabled[i]);
319 restoredChanged |= mRestored.remove(enabled[i]);
322 remaining.addAll(mRestored);
323 flatOut = TextUtils.join(ENABLED_SERVICES_SEPARATOR, remaining);
325 if (DEBUG) Slog.v(TAG, "flat after: " + flatOut);
326 if (!flatIn.equals(flatOut)) {
327 Settings.Secure.putStringForUser(cr,
328 mConfig.secureSettingName,
331 if (restoredChanged) {
332 if (DEBUG) Slog.d(TAG, "restored changed; rewriting");
333 final String flatRestored = TextUtils.join(ENABLED_SERVICES_SEPARATOR,
334 mRestored.toArray());
335 Settings.Secure.putStringForUser(cr,
336 restoredSettingName(mConfig),
344 * Called whenever packages change, the user switches, or the secure setting
345 * is altered. (For example in response to USER_SWITCHED in our broadcast receiver)
347 private void rebindServices() {
348 if (DEBUG) Slog.d(TAG, "rebindServices");
349 final int[] userIds = mUserProfiles.getCurrentProfileIds();
350 final int nUserIds = userIds.length;
352 final SparseArray<String> flat = new SparseArray<String>();
354 for (int i = 0; i < nUserIds; ++i) {
355 flat.put(userIds[i], Settings.Secure.getStringForUser(
356 mContext.getContentResolver(),
357 mConfig.secureSettingName,
361 ArrayList<ManagedServiceInfo> toRemove = new ArrayList<ManagedServiceInfo>();
362 final SparseArray<ArrayList<ComponentName>> toAdd
363 = new SparseArray<ArrayList<ComponentName>>();
365 synchronized (mMutex) {
366 // Unbind automatically bound services, retain system services.
367 for (ManagedServiceInfo service : mServices) {
368 if (!service.isSystem) {
369 toRemove.add(service);
373 final ArraySet<ComponentName> newEnabled = new ArraySet<ComponentName>();
374 final ArraySet<String> newPackages = new ArraySet<String>();
376 for (int i = 0; i < nUserIds; ++i) {
377 final ArrayList<ComponentName> add = new ArrayList<ComponentName>();
378 toAdd.put(userIds[i], add);
380 // decode the list of components
381 String toDecode = flat.get(userIds[i]);
382 if (toDecode != null) {
383 String[] components = toDecode.split(ENABLED_SERVICES_SEPARATOR);
384 for (int j = 0; j < components.length; j++) {
385 final ComponentName component
386 = ComponentName.unflattenFromString(components[j]);
387 if (component != null) {
388 newEnabled.add(component);
390 newPackages.add(component.getPackageName());
396 mEnabledServicesForCurrentProfiles = newEnabled;
397 mEnabledServicesPackageNames = newPackages;
400 for (ManagedServiceInfo info : toRemove) {
401 final ComponentName component = info.component;
402 final int oldUser = info.userid;
403 Slog.v(TAG, "disabling " + getCaption() + " for user "
404 + oldUser + ": " + component);
405 unregisterService(component, info.userid);
408 for (int i = 0; i < nUserIds; ++i) {
409 final ArrayList<ComponentName> add = toAdd.get(userIds[i]);
410 final int N = add.size();
411 for (int j = 0; j < N; j++) {
412 final ComponentName component = add.get(j);
413 Slog.v(TAG, "enabling " + getCaption() + " for user " + userIds[i] + ": "
415 registerService(component, userIds[i]);
419 mLastSeenProfileIds = mUserProfiles.getCurrentProfileIds();
423 * Version of registerService that takes the name of a service component to bind to.
425 private void registerService(final ComponentName name, final int userid) {
426 if (DEBUG) Slog.v(TAG, "registerService: " + name + " u=" + userid);
428 synchronized (mMutex) {
429 final String servicesBindingTag = name.toString() + "/" + userid;
430 if (mServicesBinding.contains(servicesBindingTag)) {
431 // stop registering this thing already! we're working on it
434 mServicesBinding.add(servicesBindingTag);
436 final int N = mServices.size();
437 for (int i=N-1; i>=0; i--) {
438 final ManagedServiceInfo info = mServices.get(i);
439 if (name.equals(info.component)
440 && info.userid == userid) {
441 // cut old connections
442 if (DEBUG) Slog.v(TAG, " disconnecting old " + getCaption() + ": "
444 removeServiceLocked(i);
445 if (info.connection != null) {
446 mContext.unbindService(info.connection);
451 Intent intent = new Intent(mConfig.serviceInterface);
452 intent.setComponent(name);
454 intent.putExtra(Intent.EXTRA_CLIENT_LABEL, mConfig.clientLabel);
456 final PendingIntent pendingIntent = PendingIntent.getActivity(
457 mContext, 0, new Intent(mConfig.settingsAction), 0);
458 intent.putExtra(Intent.EXTRA_CLIENT_INTENT, pendingIntent);
460 ApplicationInfo appInfo = null;
462 appInfo = mContext.getPackageManager().getApplicationInfo(
463 name.getPackageName(), 0);
464 } catch (NameNotFoundException e) {
465 // Ignore if the package doesn't exist we won't be able to bind to the service.
467 final int targetSdkVersion =
468 appInfo != null ? appInfo.targetSdkVersion : Build.VERSION_CODES.BASE;
471 if (DEBUG) Slog.v(TAG, "binding: " + intent);
472 if (!mContext.bindServiceAsUser(intent,
473 new ServiceConnection() {
477 public void onServiceConnected(ComponentName name, IBinder binder) {
478 boolean added = false;
479 ManagedServiceInfo info = null;
480 synchronized (mMutex) {
481 mServicesBinding.remove(servicesBindingTag);
483 mService = asInterface(binder);
484 info = newServiceInfo(mService, name,
485 userid, false /*isSystem*/, this, targetSdkVersion);
486 binder.linkToDeath(info, 0);
487 added = mServices.add(info);
488 } catch (RemoteException e) {
493 onServiceAdded(info);
498 public void onServiceDisconnected(ComponentName name) {
499 Slog.v(TAG, getCaption() + " connection lost: " + name);
502 Context.BIND_AUTO_CREATE | Context.BIND_FOREGROUND_SERVICE,
503 new UserHandle(userid)))
505 mServicesBinding.remove(servicesBindingTag);
506 Slog.w(TAG, "Unable to bind " + getCaption() + " service: " + intent);
509 } catch (SecurityException ex) {
510 Slog.e(TAG, "Unable to bind " + getCaption() + " service: " + intent, ex);
517 * Remove a service for the given user by ComponentName
519 private void unregisterService(ComponentName name, int userid) {
520 synchronized (mMutex) {
521 final int N = mServices.size();
522 for (int i=N-1; i>=0; i--) {
523 final ManagedServiceInfo info = mServices.get(i);
524 if (name.equals(info.component)
525 && info.userid == userid) {
526 removeServiceLocked(i);
527 if (info.connection != null) {
529 mContext.unbindService(info.connection);
530 } catch (IllegalArgumentException ex) {
531 // something happened to the service: we think we have a connection
533 Slog.e(TAG, getCaption() + " " + name + " could not be unbound: " + ex);
542 * Removes a service from the list but does not unbind
544 * @return the removed service.
546 private ManagedServiceInfo removeServiceImpl(IInterface service, final int userid) {
547 if (DEBUG) Slog.d(TAG, "removeServiceImpl service=" + service + " u=" + userid);
548 ManagedServiceInfo serviceInfo = null;
549 synchronized (mMutex) {
550 final int N = mServices.size();
551 for (int i=N-1; i>=0; i--) {
552 final ManagedServiceInfo info = mServices.get(i);
553 if (info.service.asBinder() == service.asBinder()
554 && info.userid == userid) {
555 if (DEBUG) Slog.d(TAG, "Removing active service " + info.component);
556 serviceInfo = removeServiceLocked(i);
563 private ManagedServiceInfo removeServiceLocked(int i) {
564 final ManagedServiceInfo info = mServices.remove(i);
565 onServiceRemovedLocked(info);
569 private void checkNotNull(IInterface service) {
570 if (service == null) {
571 throw new IllegalArgumentException(getCaption() + " must not be null");
575 private ManagedServiceInfo registerServiceImpl(final IInterface service,
576 final ComponentName component, final int userid) {
577 synchronized (mMutex) {
579 ManagedServiceInfo info = newServiceInfo(service, component, userid,
580 true /*isSystem*/, null, Build.VERSION_CODES.LOLLIPOP);
581 service.asBinder().linkToDeath(info, 0);
584 } catch (RemoteException e) {
592 * Removes a service from the list and unbinds.
594 private void unregisterServiceImpl(IInterface service, int userid) {
595 ManagedServiceInfo info = removeServiceImpl(service, userid);
596 if (info != null && info.connection != null) {
597 mContext.unbindService(info.connection);
601 private class SettingsObserver extends ContentObserver {
602 private final Uri mSecureSettingsUri = Settings.Secure.getUriFor(mConfig.secureSettingName);
604 private SettingsObserver(Handler handler) {
608 private void observe() {
609 ContentResolver resolver = mContext.getContentResolver();
610 resolver.registerContentObserver(mSecureSettingsUri,
611 false, this, UserHandle.USER_ALL);
616 public void onChange(boolean selfChange, Uri uri) {
620 private void update(Uri uri) {
621 if (uri == null || mSecureSettingsUri.equals(uri)) {
622 if (DEBUG) Slog.d(TAG, "Setting changed: mSecureSettingsUri=" + mSecureSettingsUri +
629 public class ManagedServiceInfo implements IBinder.DeathRecipient {
630 public IInterface service;
631 public ComponentName component;
633 public boolean isSystem;
634 public ServiceConnection connection;
635 public int targetSdkVersion;
637 public ManagedServiceInfo(IInterface service, ComponentName component,
638 int userid, boolean isSystem, ServiceConnection connection, int targetSdkVersion) {
639 this.service = service;
640 this.component = component;
641 this.userid = userid;
642 this.isSystem = isSystem;
643 this.connection = connection;
644 this.targetSdkVersion = targetSdkVersion;
648 public String toString() {
649 return new StringBuilder("ManagedServiceInfo[")
650 .append("component=").append(component)
651 .append(",userid=").append(userid)
652 .append(",isSystem=").append(isSystem)
653 .append(",targetSdkVersion=").append(targetSdkVersion)
654 .append(",connection=").append(connection == null ? null : "<connection>")
655 .append(",service=").append(service)
656 .append(']').toString();
659 public boolean enabledAndUserMatches(int nid) {
660 if (!isEnabledForCurrentProfiles()) {
663 if (this.userid == UserHandle.USER_ALL) return true;
664 if (nid == UserHandle.USER_ALL || nid == this.userid) return true;
665 return supportsProfiles() && mUserProfiles.isCurrentProfile(nid);
668 public boolean supportsProfiles() {
669 return targetSdkVersion >= Build.VERSION_CODES.LOLLIPOP;
673 public void binderDied() {
674 if (DEBUG) Slog.d(TAG, "binderDied");
675 // Remove the service, but don't unbind from the service. The system will bring the
676 // service back up, and the onServiceConnected handler will readd the service with the
677 // new binding. If this isn't a bound service, and is just a registered
678 // service, just removing it from the list is all we need to do anyway.
679 removeServiceImpl(this.service, this.userid);
682 /** convenience method for looking in mEnabledServicesForCurrentProfiles */
683 public boolean isEnabledForCurrentProfiles() {
684 if (this.isSystem) return true;
685 if (this.connection == null) return false;
686 return mEnabledServicesForCurrentProfiles.contains(this.component);
690 public static class UserProfiles {
691 // Profiles of the current user.
692 private final SparseArray<UserInfo> mCurrentProfiles = new SparseArray<UserInfo>();
694 public void updateCache(Context context) {
695 UserManager userManager = (UserManager) context.getSystemService(Context.USER_SERVICE);
696 if (userManager != null) {
697 int currentUserId = ActivityManager.getCurrentUser();
698 List<UserInfo> profiles = userManager.getProfiles(currentUserId);
699 synchronized (mCurrentProfiles) {
700 mCurrentProfiles.clear();
701 for (UserInfo user : profiles) {
702 mCurrentProfiles.put(user.id, user);
708 public int[] getCurrentProfileIds() {
709 synchronized (mCurrentProfiles) {
710 int[] users = new int[mCurrentProfiles.size()];
711 final int N = mCurrentProfiles.size();
712 for (int i = 0; i < N; ++i) {
713 users[i] = mCurrentProfiles.keyAt(i);
719 public boolean isCurrentProfile(int userId) {
720 synchronized (mCurrentProfiles) {
721 return mCurrentProfiles.get(userId) != null;
726 protected static class Config {
728 String serviceInterface;
729 String secureSettingName;
730 String bindPermission;
731 String settingsAction;