Notification listeners have full DND access.
Bug: 27976092 Change-Id: I9603d900d7cee5666ec3567b4f42fee6d93ae5f8
This commit is contained in:
@@ -16,8 +16,16 @@
|
||||
|
||||
package com.android.settings.notification;
|
||||
|
||||
import android.app.AlertDialog;
|
||||
import android.app.Dialog;
|
||||
import android.app.DialogFragment;
|
||||
import android.app.NotificationManager;
|
||||
import android.content.ComponentName;
|
||||
import android.content.Context;
|
||||
import android.content.DialogInterface;
|
||||
import android.content.pm.PackageManager;
|
||||
import android.os.AsyncTask;
|
||||
import android.os.Bundle;
|
||||
import android.provider.Settings;
|
||||
import android.service.notification.NotificationListenerService;
|
||||
|
||||
@@ -30,6 +38,14 @@ public class NotificationAccessSettings extends ManagedServiceSettings {
|
||||
private static final String TAG = NotificationAccessSettings.class.getSimpleName();
|
||||
private static final Config CONFIG = getNotificationListenerConfig();
|
||||
|
||||
private NotificationManager mNm;
|
||||
|
||||
@Override
|
||||
public void onCreate(Bundle icicle) {
|
||||
super.onCreate(icicle);
|
||||
mNm = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
|
||||
}
|
||||
|
||||
private static Config getNotificationListenerConfig() {
|
||||
final Config c = new Config();
|
||||
c.tag = TAG;
|
||||
@@ -60,4 +76,74 @@ public class NotificationAccessSettings extends ManagedServiceSettings {
|
||||
public static int getEnabledListenersCount(Context context) {
|
||||
return ServiceListing.getEnabledServicesCount(CONFIG, context);
|
||||
}
|
||||
|
||||
protected boolean setEnabled(ComponentName service, String title, boolean enable) {
|
||||
if (!enable) {
|
||||
if (!mServiceListing.isEnabled(service)) {
|
||||
return true; // already disabled
|
||||
}
|
||||
// show a friendly dialog
|
||||
new FriendlyWarningDialogFragment()
|
||||
.setServiceInfo(service, title)
|
||||
.show(getFragmentManager(), "friendlydialog");
|
||||
return false;
|
||||
} else {
|
||||
return super.setEnabled(service, title, enable);
|
||||
}
|
||||
}
|
||||
|
||||
private static void deleteRules(final Context context, final String pkg) {
|
||||
AsyncTask.execute(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
final NotificationManager mgr = context.getSystemService(NotificationManager.class);
|
||||
mgr.removeAutomaticZenRules(pkg);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
public class FriendlyWarningDialogFragment extends DialogFragment {
|
||||
static final String KEY_COMPONENT = "c";
|
||||
static final String KEY_LABEL = "l";
|
||||
|
||||
public FriendlyWarningDialogFragment setServiceInfo(ComponentName cn, String label) {
|
||||
Bundle args = new Bundle();
|
||||
args.putString(KEY_COMPONENT, cn.flattenToString());
|
||||
args.putString(KEY_LABEL, label);
|
||||
setArguments(args);
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Dialog onCreateDialog(Bundle savedInstanceState) {
|
||||
super.onCreate(savedInstanceState);
|
||||
final Bundle args = getArguments();
|
||||
final String label = args.getString(KEY_LABEL);
|
||||
final ComponentName cn = ComponentName.unflattenFromString(args
|
||||
.getString(KEY_COMPONENT));
|
||||
|
||||
final String summary = getResources().getString(
|
||||
R.string.notification_listener_disable_warning_summary, label);
|
||||
return new AlertDialog.Builder(mContext)
|
||||
.setMessage(summary)
|
||||
.setCancelable(true)
|
||||
.setPositiveButton(R.string.notification_listener_disable_warning_confirm,
|
||||
new DialogInterface.OnClickListener() {
|
||||
public void onClick(DialogInterface dialog, int id) {
|
||||
mServiceListing.setEnabled(cn, false);
|
||||
if (!mNm.isNotificationPolicyAccessGrantedForPackage(
|
||||
cn.getPackageName())) {
|
||||
deleteRules(mContext, cn.getPackageName());
|
||||
}
|
||||
}
|
||||
})
|
||||
.setNegativeButton(R.string.notification_listener_disable_warning_cancel,
|
||||
new DialogInterface.OnClickListener() {
|
||||
public void onClick(DialogInterface dialog, int id) {
|
||||
// pass
|
||||
}
|
||||
})
|
||||
.create();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@@ -21,6 +21,7 @@ import android.app.AlertDialog;
|
||||
import android.app.Dialog;
|
||||
import android.app.DialogFragment;
|
||||
import android.app.NotificationManager;
|
||||
import android.content.ComponentName;
|
||||
import android.content.Context;
|
||||
import android.content.DialogInterface;
|
||||
import android.content.pm.ApplicationInfo;
|
||||
@@ -32,6 +33,7 @@ import android.os.AsyncTask;
|
||||
import android.os.Bundle;
|
||||
import android.os.Handler;
|
||||
import android.os.Looper;
|
||||
import android.provider.Settings;
|
||||
import android.provider.Settings.Secure;
|
||||
import android.support.v14.preference.SwitchPreference;
|
||||
import android.support.v7.preference.Preference;
|
||||
@@ -40,6 +42,8 @@ import android.support.v7.preference.PreferenceScreen;
|
||||
import android.text.TextUtils;
|
||||
import android.util.ArraySet;
|
||||
import android.view.View;
|
||||
import android.widget.Toast;
|
||||
|
||||
import com.android.internal.logging.MetricsProto.MetricsEvent;
|
||||
import com.android.settings.R;
|
||||
|
||||
@@ -50,6 +54,7 @@ import java.util.List;
|
||||
public class ZenAccessSettings extends EmptyTextSettings {
|
||||
|
||||
private final SettingObserver mObserver = new SettingObserver();
|
||||
private static final String ENABLED_SERVICES_SEPARATOR = ":";
|
||||
|
||||
private Context mContext;
|
||||
private PackageManager mPkgMan;
|
||||
@@ -83,6 +88,9 @@ public class ZenAccessSettings extends EmptyTextSettings {
|
||||
getContentResolver().registerContentObserver(
|
||||
Secure.getUriFor(Secure.ENABLED_NOTIFICATION_POLICY_ACCESS_PACKAGES), false,
|
||||
mObserver);
|
||||
getContentResolver().registerContentObserver(
|
||||
Secure.getUriFor(Secure.ENABLED_NOTIFICATION_LISTENERS), false,
|
||||
mObserver);
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -96,7 +104,7 @@ public class ZenAccessSettings extends EmptyTextSettings {
|
||||
screen.removeAll();
|
||||
final ArrayList<ApplicationInfo> apps = new ArrayList<>();
|
||||
final ArraySet<String> requesting = mNoMan.getPackagesRequestingNotificationPolicyAccess();
|
||||
if (requesting != null && !requesting.isEmpty()) {
|
||||
if (!requesting.isEmpty()) {
|
||||
final List<ApplicationInfo> installed = mPkgMan.getInstalledApplications(0);
|
||||
if (installed != null) {
|
||||
for (ApplicationInfo app : installed) {
|
||||
@@ -106,6 +114,8 @@ public class ZenAccessSettings extends EmptyTextSettings {
|
||||
}
|
||||
}
|
||||
}
|
||||
ArraySet<String> autoApproved = getEnabledNotificationListeners();
|
||||
requesting.addAll(autoApproved);
|
||||
Collections.sort(apps, new PackageItemInfo.DisplayNameComparator(mPkgMan));
|
||||
for (ApplicationInfo app : apps) {
|
||||
final String pkg = app.packageName;
|
||||
@@ -115,6 +125,10 @@ public class ZenAccessSettings extends EmptyTextSettings {
|
||||
pref.setIcon(app.loadIcon(mPkgMan));
|
||||
pref.setTitle(label);
|
||||
pref.setChecked(hasAccess(pkg));
|
||||
if (autoApproved.contains(pkg)) {
|
||||
pref.setEnabled(false);
|
||||
pref.setSummary(getString(R.string.zen_access_disabled_package_warning));
|
||||
}
|
||||
pref.setOnPreferenceChangeListener(new OnPreferenceChangeListener() {
|
||||
@Override
|
||||
public boolean onPreferenceChange(Preference preference, Object newValue) {
|
||||
@@ -135,6 +149,22 @@ public class ZenAccessSettings extends EmptyTextSettings {
|
||||
}
|
||||
}
|
||||
|
||||
private ArraySet<String> getEnabledNotificationListeners() {
|
||||
ArraySet<String> packages = new ArraySet<>();
|
||||
String settingValue = Settings.Secure.getString(getContext().getContentResolver(),
|
||||
Settings.Secure.ENABLED_NOTIFICATION_LISTENERS);
|
||||
if (!TextUtils.isEmpty(settingValue)) {
|
||||
String[] restored = settingValue.split(ENABLED_SERVICES_SEPARATOR);
|
||||
for (int i = 0; i < restored.length; i++) {
|
||||
ComponentName value = ComponentName.unflattenFromString(restored[i]);
|
||||
if (null != value) {
|
||||
packages.add(value.getPackageName());
|
||||
}
|
||||
}
|
||||
}
|
||||
return packages;
|
||||
}
|
||||
|
||||
private boolean hasAccess(String pkg) {
|
||||
return mNoMan.isNotificationPolicyAccessGrantedForPackage(pkg);
|
||||
}
|
||||
|
@@ -43,11 +43,10 @@ import com.android.internal.logging.MetricsLogger;
|
||||
import com.android.internal.logging.MetricsProto.MetricsEvent;
|
||||
import com.android.settings.R;
|
||||
import com.android.settings.utils.ManagedServiceSettings.Config;
|
||||
import com.android.settings.utils.ServiceListing;
|
||||
import com.android.settings.utils.ZenServiceListing;
|
||||
|
||||
import java.lang.ref.WeakReference;
|
||||
import java.util.Arrays;
|
||||
import java.util.Collections;
|
||||
import java.util.Comparator;
|
||||
import java.util.Map;
|
||||
|
||||
@@ -56,14 +55,14 @@ public class ZenModeAutomationSettings extends ZenModeSettingsBase {
|
||||
static final Config CONFIG = getConditionProviderConfig();
|
||||
|
||||
private PackageManager mPm;
|
||||
private ServiceListing mServiceListing;
|
||||
private ZenServiceListing mServiceListing;
|
||||
|
||||
@Override
|
||||
public void onCreate(Bundle icicle) {
|
||||
super.onCreate(icicle);
|
||||
addPreferencesFromResource(R.xml.zen_mode_automation_settings);
|
||||
mPm = mContext.getPackageManager();
|
||||
mServiceListing = new ServiceListing(mContext, CONFIG);
|
||||
mServiceListing = new ZenServiceListing(mContext, CONFIG);
|
||||
mServiceListing.reloadApprovedServices();
|
||||
}
|
||||
|
||||
@@ -203,6 +202,7 @@ public class ZenModeAutomationSettings extends ZenModeSettingsBase {
|
||||
final Config c = new Config();
|
||||
c.tag = TAG;
|
||||
c.setting = Settings.Secure.ENABLED_NOTIFICATION_POLICY_ACCESS_PACKAGES;
|
||||
c.secondarySetting = Settings.Secure.ENABLED_NOTIFICATION_LISTENERS;
|
||||
c.intentAction = ConditionProviderService.SERVICE_INTERFACE;
|
||||
c.permission = android.Manifest.permission.BIND_CONDITION_PROVIDER_SERVICE;
|
||||
c.noun = "condition provider";
|
||||
@@ -308,7 +308,7 @@ public class ZenModeAutomationSettings extends ZenModeSettingsBase {
|
||||
|
||||
final String action = isSchedule ? ZenModeScheduleRuleSettings.ACTION
|
||||
: isEvent ? ZenModeEventRuleSettings.ACTION : "";
|
||||
ServiceInfo si = mServiceListing.findService(mContext, CONFIG, rule.getOwner());
|
||||
ServiceInfo si = mServiceListing.findService(rule.getOwner());
|
||||
ComponentName settingsActivity = getSettingsActivity(si);
|
||||
setIntent(getRuleIntent(action, settingsActivity, mId));
|
||||
setSelectable(settingsActivity != null || isSystemRule);
|
||||
|
@@ -4,6 +4,31 @@ import android.content.ComponentName;
|
||||
import android.net.Uri;
|
||||
|
||||
public class ZenRuleInfo {
|
||||
@Override
|
||||
public boolean equals(Object o) {
|
||||
if (this == o) return true;
|
||||
if (o == null || getClass() != o.getClass()) return false;
|
||||
|
||||
ZenRuleInfo that = (ZenRuleInfo) o;
|
||||
|
||||
if (isSystem != that.isSystem) return false;
|
||||
if (ruleInstanceLimit != that.ruleInstanceLimit) return false;
|
||||
if (packageName != null ? !packageName.equals(that.packageName) : that.packageName != null)
|
||||
return false;
|
||||
if (title != null ? !title.equals(that.title) : that.title != null) return false;
|
||||
if (settingsAction != null ? !settingsAction.equals(
|
||||
that.settingsAction) : that.settingsAction != null) return false;
|
||||
if (configurationActivity != null ? !configurationActivity.equals(
|
||||
that.configurationActivity) : that.configurationActivity != null) return false;
|
||||
if (defaultConditionId != null ? !defaultConditionId.equals(
|
||||
that.defaultConditionId) : that.defaultConditionId != null) return false;
|
||||
if (serviceComponent != null ? !serviceComponent.equals(
|
||||
that.serviceComponent) : that.serviceComponent != null) return false;
|
||||
return packageLabel != null ? packageLabel.equals(
|
||||
that.packageLabel) : that.packageLabel == null;
|
||||
|
||||
}
|
||||
|
||||
public String packageName;
|
||||
public String title;
|
||||
public String settingsAction;
|
||||
|
@@ -27,6 +27,7 @@ import android.content.pm.ServiceInfo;
|
||||
import android.graphics.drawable.Drawable;
|
||||
import android.os.AsyncTask;
|
||||
import android.service.notification.ZenModeConfig;
|
||||
import android.util.ArraySet;
|
||||
import android.util.Log;
|
||||
import android.view.LayoutInflater;
|
||||
import android.view.View;
|
||||
@@ -36,6 +37,7 @@ import android.widget.TextView;
|
||||
|
||||
import com.android.settings.R;
|
||||
import com.android.settings.utils.ServiceListing;
|
||||
import com.android.settings.utils.ZenServiceListing;
|
||||
|
||||
import java.lang.ref.WeakReference;
|
||||
import java.text.Collator;
|
||||
@@ -43,6 +45,8 @@ import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.Comparator;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
import java.util.TreeSet;
|
||||
|
||||
public abstract class ZenRuleSelectionDialog {
|
||||
private static final String TAG = "ZenRuleSelectionDialog";
|
||||
@@ -53,9 +57,9 @@ public abstract class ZenRuleSelectionDialog {
|
||||
private NotificationManager mNm;
|
||||
private final AlertDialog mDialog;
|
||||
private final LinearLayout mRuleContainer;
|
||||
private final ServiceListing mServiceListing;
|
||||
private final ZenServiceListing mServiceListing;
|
||||
|
||||
public ZenRuleSelectionDialog(Context context, ServiceListing serviceListing) {
|
||||
public ZenRuleSelectionDialog(Context context, ZenServiceListing serviceListing) {
|
||||
mContext = context;
|
||||
mPm = context.getPackageManager();
|
||||
mNm = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
|
||||
@@ -67,7 +71,7 @@ public abstract class ZenRuleSelectionDialog {
|
||||
if (mServiceListing != null) {
|
||||
bindType(defaultNewEvent());
|
||||
bindType(defaultNewSchedule());
|
||||
mServiceListing.addCallback(mServiceListingCallback);
|
||||
mServiceListing.addZenCallback(mServiceListingCallback);
|
||||
mServiceListing.reloadApprovedServices();
|
||||
}
|
||||
mDialog = new AlertDialog.Builder(context)
|
||||
@@ -77,7 +81,7 @@ public abstract class ZenRuleSelectionDialog {
|
||||
@Override
|
||||
public void onDismiss(DialogInterface dialog) {
|
||||
if (mServiceListing != null) {
|
||||
mServiceListing.removeCallback(mServiceListingCallback);
|
||||
mServiceListing.removeZenCallback(mServiceListingCallback);
|
||||
}
|
||||
}
|
||||
})
|
||||
@@ -152,24 +156,24 @@ public abstract class ZenRuleSelectionDialog {
|
||||
return rt;
|
||||
}
|
||||
|
||||
private void bindExternalRules(List<ZenRuleInfo> externalRuleTypes) {
|
||||
Collections.sort(externalRuleTypes, RULE_TYPE_COMPARATOR);
|
||||
private void bindExternalRules(Set<ZenRuleInfo> externalRuleTypes) {
|
||||
for (ZenRuleInfo ri : externalRuleTypes) {
|
||||
bindType(ri);
|
||||
}
|
||||
}
|
||||
|
||||
private final ServiceListing.Callback mServiceListingCallback = new ServiceListing.Callback() {
|
||||
private final ZenServiceListing.Callback mServiceListingCallback = new
|
||||
ZenServiceListing.Callback() {
|
||||
@Override
|
||||
public void onServicesReloaded(List<ServiceInfo> services) {
|
||||
public void onServicesReloaded(Set<ServiceInfo> services) {
|
||||
if (DEBUG) Log.d(TAG, "Services reloaded: count=" + services.size());
|
||||
List<ZenRuleInfo> externalRuleTypes = new ArrayList<>();
|
||||
for (int i = 0; i < services.size(); i++) {
|
||||
final ZenRuleInfo ri = ZenModeAutomationSettings.getRuleInfo(mPm, services.get(i));
|
||||
Set<ZenRuleInfo> externalRuleTypes = new TreeSet<>(RULE_TYPE_COMPARATOR);
|
||||
for (ServiceInfo serviceInfo : services) {
|
||||
final ZenRuleInfo ri = ZenModeAutomationSettings.getRuleInfo(mPm, serviceInfo);
|
||||
if (ri != null && ri.configurationActivity != null
|
||||
&& mNm.isNotificationPolicyAccessGrantedForPackage(ri.packageName)
|
||||
&& (ri.ruleInstanceLimit <= 0 || ri.ruleInstanceLimit
|
||||
>= (mNm.getRuleInstanceCount(services.get(i).getComponentName()) + 1))) {
|
||||
>= (mNm.getRuleInstanceCount(serviceInfo.getComponentName()) + 1))) {
|
||||
externalRuleTypes.add(ri);
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user