Migrate from setting to NotificationManager.

Test: manual; changing settings and confirming with bugreport
Change-Id: I1595fc3ceca8cd31d5bece52dff75aebe29d2ae3
This commit is contained in:
Julia Reynolds
2017-06-09 15:00:14 -04:00
parent ec102eb32e
commit ee4b6ba6ae
8 changed files with 82 additions and 258 deletions

View File

@@ -20,6 +20,8 @@ import android.annotation.Nullable;
import android.app.AlertDialog;
import android.app.Dialog;
import android.app.Fragment;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.admin.DevicePolicyManager;
import android.content.ComponentName;
import android.content.Context;
@@ -54,6 +56,7 @@ public abstract class ManagedServiceSettings extends EmptyTextSettings {
private PackageManager mPm;
private DevicePolicyManager mDpm;
protected ServiceListing mServiceListing;
protected NotificationManager mNm;
abstract protected Config getConfig();
@@ -68,6 +71,7 @@ public abstract class ManagedServiceSettings extends EmptyTextSettings {
mContext = getActivity();
mPm = mContext.getPackageManager();
mDpm = (DevicePolicyManager) mContext.getSystemService(Context.DEVICE_POLICY_SERVICE);
mNm = (NotificationManager) mContext.getSystemService(Context.NOTIFICATION_SERVICE);
mServiceListing = new ServiceListing(mContext, mConfig);
mServiceListing.addCallback(new ServiceListing.Callback() {
@Override
@@ -124,7 +128,7 @@ public abstract class ManagedServiceSettings extends EmptyTextSettings {
} else {
pref.setTitle(summary);
}
pref.setChecked(mServiceListing.isEnabled(cn));
pref.setChecked(isServiceEnabled(cn));
if (managedProfileId != UserHandle.USER_NULL
&& !mDpm.isNotificationListenerServicePermitted(
service.packageName, managedProfileId)) {
@@ -148,6 +152,10 @@ public abstract class ManagedServiceSettings extends EmptyTextSettings {
return UserHandle.myUserId();
}
protected boolean isServiceEnabled(ComponentName cn) {
return mServiceListing.isEnabled(cn);
}
protected boolean setEnabled(ComponentName service, String title, boolean enable) {
if (!enable) {
// the simple version: disabling
@@ -165,6 +173,10 @@ public abstract class ManagedServiceSettings extends EmptyTextSettings {
}
}
protected void enable(ComponentName service) {
mServiceListing.setEnabled(service, true);
}
public static class ScaryWarningDialogFragment extends InstrumentedDialogFragment {
static final String KEY_COMPONENT = "c";
static final String KEY_LABEL = "l";
@@ -202,7 +214,7 @@ public abstract class ManagedServiceSettings extends EmptyTextSettings {
.setPositiveButton(R.string.allow,
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
parent.mServiceListing.setEnabled(cn, true);
parent.enable(cn);
}
})
.setNegativeButton(R.string.deny,

View File

@@ -16,6 +16,7 @@
package com.android.settings.utils;
import android.app.ActivityManager;
import android.app.NotificationManager;
import android.content.ComponentName;
import android.content.ContentResolver;
import android.content.Context;
@@ -35,16 +36,16 @@ import java.util.Set;
public class ZenServiceListing {
private final ContentResolver mContentResolver;
private final Context mContext;
private final ManagedServiceSettings.Config mConfig;
private final Set<ServiceInfo> mApprovedServices = new ArraySet<ServiceInfo>();
private final List<Callback> mZenCallbacks = new ArrayList<>();
private final NotificationManager mNm;
public ZenServiceListing(Context context, ManagedServiceSettings.Config config) {
mContext = context;
mConfig = config;
mContentResolver = context.getContentResolver();
mNm = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
}
public ServiceInfo findService(final ComponentName cn) {
@@ -67,23 +68,18 @@ public class ZenServiceListing {
public void reloadApprovedServices() {
mApprovedServices.clear();
String[] settings = {mConfig.setting, mConfig.secondarySetting};
for (String setting : settings) {
if (!TextUtils.isEmpty(setting)) {
final String flat = Settings.Secure.getString(mContentResolver, setting);
if (!TextUtils.isEmpty(flat)) {
final List<String> names = Arrays.asList(flat.split(":"));
List<ServiceInfo> services = new ArrayList<>();
getServices(mConfig, services, mContext.getPackageManager());
for (ServiceInfo service : services) {
if (matchesApprovedPackage(names, service.getComponentName())) {
mApprovedServices.add(service);
}
}
}
List<String> enabledNotificationListenerPkgs = mNm.getEnabledNotificationListenerPackages();
List<ServiceInfo> services = new ArrayList<>();
getServices(mConfig, services, mContext.getPackageManager());
for (ServiceInfo service : services) {
final String servicePackage = service.getComponentName().getPackageName();
if (mNm.isNotificationPolicyAccessGrantedForPackage(servicePackage)
|| enabledNotificationListenerPkgs.contains(servicePackage)) {
mApprovedServices.add(service);
}
}
if (!mApprovedServices.isEmpty()) {
for (Callback callback : mZenCallbacks) {
callback.onServicesReloaded(mApprovedServices);
@@ -91,25 +87,6 @@ public class ZenServiceListing {
}
}
// Setting could contain: the component name of the condition provider, the package name of
// the condition provider, the component name of the notification listener.
private boolean matchesApprovedPackage(List<String> approved, ComponentName serviceOwner) {
String flatCn = serviceOwner.flattenToString();
if (approved.contains(flatCn) || approved.contains(serviceOwner.getPackageName())) {
return true;
}
for (String entry : approved) {
if (!TextUtils.isEmpty(entry)) {
ComponentName approvedComponent = ComponentName.unflattenFromString(entry);
if (approvedComponent != null && approvedComponent.getPackageName().equals(
serviceOwner.getPackageName())) {
return true;
}
}
}
return false;
}
private static int getServices(ManagedServiceSettings.Config c, List<ServiceInfo> list,
PackageManager pm) {
int services = 0;