Fix settings crash when disabling listener.

Bug: 32092445
Test: manual
Change-Id: Iaa26e755381ba202cccf6d518dacb86eae4c6d10
This commit is contained in:
Julia Reynolds
2016-09-21 09:36:02 -04:00
parent 36a6cb0370
commit 6e99b3ee4e
2 changed files with 18 additions and 27 deletions

View File

@@ -18,33 +18,30 @@ package com.android.settings.notification;
import android.app.AlertDialog;
import android.app.Dialog;
import android.app.DialogFragment;
import android.app.Fragment;
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;
import android.util.Log;
import com.android.internal.logging.MetricsProto.MetricsEvent;
import com.android.settings.R;
import com.android.settings.core.instrumentation.InstrumentedDialogFragment;
import com.android.settings.utils.ManagedServiceSettings;
import com.android.settings.utils.ServiceListing;
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() {
@@ -70,14 +67,6 @@ public class NotificationAccessSettings extends ManagedServiceSettings {
return CONFIG;
}
public static int getListenersCount(PackageManager pm) {
return ServiceListing.getServicesCount(CONFIG, pm);
}
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)) {
@@ -85,7 +74,7 @@ public class NotificationAccessSettings extends ManagedServiceSettings {
}
// show a friendly dialog
new FriendlyWarningDialogFragment()
.setServiceInfo(service, title)
.setServiceInfo(service, title, this)
.show(getFragmentManager(), "friendlydialog");
return false;
} else {
@@ -93,25 +82,33 @@ public class NotificationAccessSettings extends ManagedServiceSettings {
}
}
private static void deleteRules(final Context context, final String pkg) {
private static void disable(final Context context, final NotificationAccessSettings parent,
final ComponentName cn) {
parent.mServiceListing.setEnabled(cn, false);
AsyncTask.execute(new Runnable() {
@Override
public void run() {
final NotificationManager mgr = context.getSystemService(NotificationManager.class);
mgr.removeAutomaticZenRules(pkg);
if (!mgr.isNotificationPolicyAccessGrantedForPackage(
cn.getPackageName())) {
mgr.removeAutomaticZenRules(cn.getPackageName());
}
}
});
}
public class FriendlyWarningDialogFragment extends InstrumentedDialogFragment {
public static class FriendlyWarningDialogFragment extends InstrumentedDialogFragment {
static final String KEY_COMPONENT = "c";
static final String KEY_LABEL = "l";
public FriendlyWarningDialogFragment setServiceInfo(ComponentName cn, String label) {
public FriendlyWarningDialogFragment setServiceInfo(ComponentName cn, String label,
Fragment target) {
Bundle args = new Bundle();
args.putString(KEY_COMPONENT, cn.flattenToString());
args.putString(KEY_LABEL, label);
setArguments(args);
setTargetFragment(target, 0);
return this;
}
@@ -122,25 +119,21 @@ public class NotificationAccessSettings extends ManagedServiceSettings {
@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));
NotificationAccessSettings parent = (NotificationAccessSettings) getTargetFragment();
final String summary = getResources().getString(
R.string.notification_listener_disable_warning_summary, label);
return new AlertDialog.Builder(mContext)
return new AlertDialog.Builder(getContext())
.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());
}
disable(getContext(), parent, cn);
}
})
.setNegativeButton(R.string.notification_listener_disable_warning_cancel,

View File

@@ -49,7 +49,6 @@ public abstract class ManagedServiceSettings extends EmptyTextSettings {
protected Context mContext;
private PackageManager mPM;
protected ServiceListing mServiceListing;
private TextView mEmpty;
abstract protected Config getConfig();
@@ -153,7 +152,6 @@ public abstract class ManagedServiceSettings extends EmptyTextSettings {
@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