From 6e839b006e102b4c2d9f495fe0551e7668dae61e Mon Sep 17 00:00:00 2001 From: Julia Reynolds Date: Wed, 13 Apr 2016 10:01:17 -0400 Subject: [PATCH] Allow notification listeners full DND access. Bug: 27976092 Change-Id: I15da87f4b6a17e43ced80ea473288b43d20195d8 --- .../server/notification/ConditionProviders.java | 15 +++-- .../server/notification/ManagedServices.java | 72 ++++++++++++++-------- 2 files changed, 58 insertions(+), 29 deletions(-) diff --git a/services/core/java/com/android/server/notification/ConditionProviders.java b/services/core/java/com/android/server/notification/ConditionProviders.java index c19b51f54a31..62fe70cf176f 100644 --- a/services/core/java/com/android/server/notification/ConditionProviders.java +++ b/services/core/java/com/android/server/notification/ConditionProviders.java @@ -16,7 +16,7 @@ package com.android.server.notification; -import android.app.AutomaticZenRule; +import android.annotation.NonNull; import android.content.ComponentName; import android.content.ContentResolver; import android.content.Context; @@ -29,7 +29,6 @@ import android.os.UserHandle; import android.provider.Settings; import android.service.notification.Condition; import android.service.notification.ConditionProviderService; -import android.service.notification.IConditionListener; import android.service.notification.IConditionProvider; import android.text.TextUtils; import android.util.ArrayMap; @@ -82,6 +81,7 @@ public class ConditionProviders extends ManagedServices { c.caption = "condition provider"; c.serviceInterface = ConditionProviderService.SERVICE_INTERFACE; c.secureSettingName = Settings.Secure.ENABLED_NOTIFICATION_POLICY_ACCESS_PACKAGES; + c.secondarySettingName = Settings.Secure.ENABLED_NOTIFICATION_LISTENERS; c.bindPermission = android.Manifest.permission.BIND_CONDITION_PROVIDER_SERVICE; c.settingsAction = Settings.ACTION_CONDITION_PROVIDER_SETTINGS; c.clientLabel = R.string.condition_provider_service_binding_label; @@ -257,7 +257,7 @@ public class ConditionProviders extends ManagedServices { } @Override - protected ArraySet loadComponentNamesFromSetting(String settingName, + protected @NonNull ArraySet loadComponentNamesFromSetting(String settingName, int userId) { final ContentResolver cr = mContext.getContentResolver(); String settingValue = Settings.Secure.getStringForUser( @@ -265,12 +265,17 @@ public class ConditionProviders extends ManagedServices { settingName, userId); if (TextUtils.isEmpty(settingValue)) - return null; + return new ArraySet<>(); String[] packages = settingValue.split(ENABLED_SERVICES_SEPARATOR); ArraySet result = new ArraySet<>(packages.length); for (int i = 0; i < packages.length; i++) { if (!TextUtils.isEmpty(packages[i])) { - result.addAll(queryPackageForServices(packages[i], userId)); + final ComponentName component = ComponentName.unflattenFromString(packages[i]); + if (component != null) { + result.addAll(queryPackageForServices(component.getPackageName(), userId)); + } else { + result.addAll(queryPackageForServices(packages[i], userId)); + } } } return result; diff --git a/services/core/java/com/android/server/notification/ManagedServices.java b/services/core/java/com/android/server/notification/ManagedServices.java index c31d93e9fe1d..29e2e441dcff 100644 --- a/services/core/java/com/android/server/notification/ManagedServices.java +++ b/services/core/java/com/android/server/notification/ManagedServices.java @@ -126,7 +126,8 @@ abstract public class ManagedServices { public void onReceive(Context context, Intent intent) { if (Intent.ACTION_SETTING_RESTORED.equals(intent.getAction())) { String element = intent.getStringExtra(Intent.EXTRA_SETTING_NAME); - if (Objects.equals(element, mConfig.secureSettingName)) { + if (Objects.equals(element, mConfig.secureSettingName) + || Objects.equals(element, mConfig.secondarySettingName)) { String prevValue = intent.getStringExtra(Intent.EXTRA_SETTING_PREVIOUS_VALUE); String newValue = intent.getStringExtra(Intent.EXTRA_SETTING_NEW_VALUE); settingRestored(element, prevValue, newValue, getSendingUserId()); @@ -186,8 +187,8 @@ abstract public class ManagedServices { // By convention, restored settings are replicated to another settings // entry, named similarly but with a disambiguation suffix. - public static String restoredSettingName(Config config) { - return config.secureSettingName + ":restored"; + public static String restoredSettingName(String setting) { + return setting + ":restored"; } // The OS has done a restore of this service's saved state. We clone it to the @@ -197,14 +198,14 @@ abstract public class ManagedServices { public void settingRestored(String element, String oldValue, String newValue, int userid) { if (DEBUG) Slog.d(TAG, "Restored managed service setting: " + element + " ovalue=" + oldValue + " nvalue=" + newValue); - if (mConfig.secureSettingName.equals(element)) { + if (mConfig.secureSettingName.equals(element) || + mConfig.secondarySettingName.equals(element)) { if (element != null) { - mRestored = null; Settings.Secure.putStringForUser(mContext.getContentResolver(), - restoredSettingName(mConfig), + restoredSettingName(element), newValue, userid); - updateSettingsAccordingToInstalledServices(userid); + updateSettingsAccordingToInstalledServices(element, userid); rebuildRestoredPackages(); } } @@ -343,21 +344,25 @@ abstract public class ManagedServices { private void rebuildRestoredPackages() { mRestoredPackages.clear(); mSnoozingForCurrentProfiles.clear(); - String settingName = restoredSettingName(mConfig); + String secureSettingName = restoredSettingName(mConfig.secureSettingName); + String secondarySettingName = mConfig.secondarySettingName == null + ? null : restoredSettingName(mConfig.secondarySettingName); int[] userIds = mUserProfiles.getCurrentProfileIds(); final int N = userIds.length; for (int i = 0; i < N; ++i) { - ArraySet names = loadComponentNamesFromSetting(settingName, userIds[i]); - if (names == null) - continue; - for (ComponentName name: names) { + ArraySet names = + loadComponentNamesFromSetting(secureSettingName, userIds[i]); + if (secondarySettingName != null) { + names.addAll(loadComponentNamesFromSetting(secondarySettingName, userIds[i])); + } + for (ComponentName name : names) { mRestoredPackages.add(name.getPackageName()); } } } - protected ArraySet loadComponentNamesFromSetting(String settingName, + protected @NonNull ArraySet loadComponentNamesFromSetting(String settingName, int userId) { final ContentResolver cr = mContext.getContentResolver(); String settingValue = Settings.Secure.getStringForUser( @@ -365,7 +370,7 @@ abstract public class ManagedServices { settingName, userId); if (TextUtils.isEmpty(settingValue)) - return null; + return new ArraySet<>(); String[] restored = settingValue.split(ENABLED_SERVICES_SEPARATOR); ArraySet result = new ArraySet<>(restored.length); for (int i = 0; i < restored.length; i++) { @@ -405,7 +410,11 @@ abstract public class ManagedServices { int[] userIds = mUserProfiles.getCurrentProfileIds(); final int N = userIds.length; for (int i = 0; i < N; ++i) { - updateSettingsAccordingToInstalledServices(userIds[i]); + updateSettingsAccordingToInstalledServices(mConfig.secureSettingName, userIds[i]); + if (mConfig.secondarySettingName != null) { + updateSettingsAccordingToInstalledServices( + mConfig.secondarySettingName, userIds[i]); + } } rebuildRestoredPackages(); } @@ -442,13 +451,13 @@ abstract public class ManagedServices { return installed; } - private void updateSettingsAccordingToInstalledServices(int userId) { + private void updateSettingsAccordingToInstalledServices(String setting, int userId) { boolean restoredChanged = false; boolean currentChanged = false; Set restored = - loadComponentNamesFromSetting(restoredSettingName(mConfig), userId); + loadComponentNamesFromSetting(restoredSettingName(setting), userId); Set current = - loadComponentNamesFromSetting(mConfig.secureSettingName, userId); + loadComponentNamesFromSetting(setting, userId); // Load all services for all packages. Set installed = queryPackageForServices(null, userId); @@ -478,13 +487,13 @@ abstract public class ManagedServices { if (currentChanged) { if (DEBUG) Slog.v(TAG, "List of " + getCaption() + " services was updated " + current); - storeComponentsToSetting(retained, mConfig.secureSettingName, userId); + storeComponentsToSetting(retained, setting, userId); } if (restoredChanged) { if (DEBUG) Slog.v(TAG, "List of " + getCaption() + " restored services was updated " + restored); - storeComponentsToSetting(restored, restoredSettingName(mConfig), userId); + storeComponentsToSetting(restored, restoredSettingName(setting), userId); } } @@ -502,6 +511,10 @@ abstract public class ManagedServices { for (int i = 0; i < nUserIds; ++i) { componentsByUser.put(userIds[i], loadComponentNamesFromSetting(mConfig.secureSettingName, userIds[i])); + if (mConfig.secondarySettingName != null) { + componentsByUser.get(userIds[i]).addAll( + loadComponentNamesFromSetting(mConfig.secondarySettingName, userIds[i])); + } } final ArrayList removableBoundServices = new ArrayList<>(); @@ -522,7 +535,7 @@ abstract public class ManagedServices { // decode the list of components final ArraySet userComponents = componentsByUser.get(userIds[i]); if (null == userComponents) { - toAdd.put(userIds[i], new HashSet()); + toAdd.put(userIds[i], new ArraySet()); continue; } @@ -775,15 +788,25 @@ abstract public class ManagedServices { private class SettingsObserver extends ContentObserver { private final Uri mSecureSettingsUri = Settings.Secure.getUriFor(mConfig.secureSettingName); + private final Uri mSecondarySettingsUri; private SettingsObserver(Handler handler) { super(handler); + if (mConfig.secondarySettingName != null) { + mSecondarySettingsUri = Settings.Secure.getUriFor(mConfig.secondarySettingName); + } else { + mSecondarySettingsUri = null; + } } private void observe() { ContentResolver resolver = mContext.getContentResolver(); resolver.registerContentObserver(mSecureSettingsUri, false, this, UserHandle.USER_ALL); + if (mSecondarySettingsUri != null) { + resolver.registerContentObserver(mSecondarySettingsUri, + false, this, UserHandle.USER_ALL); + } update(null); } @@ -793,9 +816,9 @@ abstract public class ManagedServices { } private void update(Uri uri) { - if (uri == null || mSecureSettingsUri.equals(uri)) { - if (DEBUG) Slog.d(TAG, "Setting changed: mSecureSettingsUri=" + mSecureSettingsUri + - " / uri=" + uri); + if (uri == null || mSecureSettingsUri.equals(uri) + || uri.equals(mSecondarySettingsUri)) { + if (DEBUG) Slog.d(TAG, "Setting changed: uri=" + uri); rebindServices(false); rebuildRestoredPackages(); } @@ -917,6 +940,7 @@ abstract public class ManagedServices { public String caption; public String serviceInterface; public String secureSettingName; + public String secondarySettingName; public String bindPermission; public String settingsAction; public int clientLabel; -- 2.11.0