Migrate from setting to NotificationManager.
Test: manual; changing settings and confirming with bugreport Change-Id: I1595fc3ceca8cd31d5bece52dff75aebe29d2ae3
This commit is contained in:
@@ -27,7 +27,9 @@ import static com.android.internal.notification.NotificationAccessConfirmationAc
|
||||
import android.Manifest;
|
||||
import android.annotation.Nullable;
|
||||
import android.app.Activity;
|
||||
import android.app.NotificationManager;
|
||||
import android.content.ComponentName;
|
||||
import android.content.Context;
|
||||
import android.content.DialogInterface;
|
||||
import android.content.pm.PackageManager;
|
||||
import android.content.pm.ServiceInfo;
|
||||
@@ -53,12 +55,14 @@ public class NotificationAccessConfirmationActivity extends Activity
|
||||
private int mUserId;
|
||||
private ComponentName mComponentName;
|
||||
private TouchOverlayManager mTouchOverlayManager;
|
||||
private NotificationManager mNm;
|
||||
|
||||
@Override
|
||||
protected void onCreate(@Nullable Bundle savedInstanceState) {
|
||||
super.onCreate(savedInstanceState);
|
||||
|
||||
mTouchOverlayManager = new TouchOverlayManager(this);
|
||||
mNm = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
|
||||
|
||||
mComponentName = getIntent().getParcelableExtra(EXTRA_COMPONENT_NAME);
|
||||
mUserId = getIntent().getIntExtra(EXTRA_USER_ID, UserHandle.USER_NULL);
|
||||
@@ -94,12 +98,7 @@ public class NotificationAccessConfirmationActivity extends Activity
|
||||
return;
|
||||
}
|
||||
|
||||
final SettingsStringUtil.SettingStringHelper setting =
|
||||
new SettingsStringUtil.SettingStringHelper(
|
||||
getContentResolver(),
|
||||
Settings.Secure.ENABLED_NOTIFICATION_LISTENERS,
|
||||
mUserId);
|
||||
setting.write(SettingsStringUtil.ComponentNameSet.add(setting.read(), mComponentName));
|
||||
mNm.setNotificationListenerAccessGranted(mComponentName, true);
|
||||
|
||||
finish();
|
||||
}
|
||||
|
@@ -41,7 +41,6 @@ public class NotificationAccessSettings extends ManagedServiceSettings {
|
||||
private static final String TAG = NotificationAccessSettings.class.getSimpleName();
|
||||
private static final Config CONFIG = getNotificationListenerConfig();
|
||||
|
||||
|
||||
@Override
|
||||
public void onCreate(Bundle icicle) {
|
||||
super.onCreate(icicle);
|
||||
@@ -70,10 +69,11 @@ public class NotificationAccessSettings extends ManagedServiceSettings {
|
||||
return CONFIG;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected boolean setEnabled(ComponentName service, String title, boolean enable) {
|
||||
logSpecialPermissionChange(enable, service.getPackageName());
|
||||
if (!enable) {
|
||||
if (!mServiceListing.isEnabled(service)) {
|
||||
if (!isServiceEnabled(service)) {
|
||||
return true; // already disabled
|
||||
}
|
||||
// show a friendly dialog
|
||||
@@ -82,10 +82,27 @@ public class NotificationAccessSettings extends ManagedServiceSettings {
|
||||
.show(getFragmentManager(), "friendlydialog");
|
||||
return false;
|
||||
} else {
|
||||
return super.setEnabled(service, title, enable);
|
||||
if (isServiceEnabled(service)) {
|
||||
return true; // already enabled
|
||||
}
|
||||
// show a scary dialog
|
||||
new ScaryWarningDialogFragment()
|
||||
.setServiceInfo(service, title, this)
|
||||
.show(getFragmentManager(), "dialog");
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
protected boolean isServiceEnabled(ComponentName cn) {
|
||||
return mNm.isNotificationListenerAccessGranted(cn);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void enable(ComponentName service) {
|
||||
mNm.setNotificationListenerAccessGranted(service, true);
|
||||
}
|
||||
|
||||
@VisibleForTesting
|
||||
void logSpecialPermissionChange(boolean enable, String packageName) {
|
||||
int logCategory = enable ? MetricsEvent.APP_SPECIAL_PERMISSION_NOTIVIEW_ALLOW
|
||||
@@ -94,17 +111,14 @@ public class NotificationAccessSettings extends ManagedServiceSettings {
|
||||
logCategory, packageName);
|
||||
}
|
||||
|
||||
private static void disable(final Context context, final NotificationAccessSettings parent,
|
||||
final ComponentName cn) {
|
||||
parent.mServiceListing.setEnabled(cn, false);
|
||||
private static void disable(final NotificationAccessSettings parent, final ComponentName cn) {
|
||||
parent.mNm.setNotificationListenerAccessGranted(cn, false);
|
||||
AsyncTask.execute(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
final NotificationManager mgr = context.getSystemService(NotificationManager.class);
|
||||
|
||||
if (!mgr.isNotificationPolicyAccessGrantedForPackage(
|
||||
if (!parent.mNm.isNotificationPolicyAccessGrantedForPackage(
|
||||
cn.getPackageName())) {
|
||||
mgr.removeAutomaticZenRules(cn.getPackageName());
|
||||
parent.mNm.removeAutomaticZenRules(cn.getPackageName());
|
||||
}
|
||||
}
|
||||
});
|
||||
@@ -145,7 +159,7 @@ public class NotificationAccessSettings extends ManagedServiceSettings {
|
||||
.setPositiveButton(R.string.notification_listener_disable_warning_confirm,
|
||||
new DialogInterface.OnClickListener() {
|
||||
public void onClick(DialogInterface dialog, int id) {
|
||||
disable(getContext(), parent, cn);
|
||||
disable(parent, cn);
|
||||
}
|
||||
})
|
||||
.setNegativeButton(R.string.notification_listener_disable_warning_cancel,
|
||||
|
@@ -17,7 +17,9 @@
|
||||
package com.android.settings.notification;
|
||||
|
||||
import android.annotation.Nullable;
|
||||
import android.app.ActivityManager;
|
||||
import android.app.AlertDialog;
|
||||
import android.app.AppGlobals;
|
||||
import android.app.Dialog;
|
||||
import android.app.DialogFragment;
|
||||
import android.app.NotificationManager;
|
||||
@@ -25,14 +27,17 @@ import android.content.ComponentName;
|
||||
import android.content.Context;
|
||||
import android.content.DialogInterface;
|
||||
import android.content.pm.ApplicationInfo;
|
||||
import android.content.pm.PackageInfo;
|
||||
import android.content.pm.PackageItemInfo;
|
||||
import android.content.pm.PackageManager;
|
||||
import android.content.pm.ParceledListSlice;
|
||||
import android.database.ContentObserver;
|
||||
import android.net.Uri;
|
||||
import android.os.AsyncTask;
|
||||
import android.os.Bundle;
|
||||
import android.os.Handler;
|
||||
import android.os.Looper;
|
||||
import android.os.RemoteException;
|
||||
import android.provider.Settings;
|
||||
import android.provider.Settings.Secure;
|
||||
import android.support.v14.preference.SwitchPreference;
|
||||
@@ -41,6 +46,7 @@ import android.support.v7.preference.Preference.OnPreferenceChangeListener;
|
||||
import android.support.v7.preference.PreferenceScreen;
|
||||
import android.text.TextUtils;
|
||||
import android.util.ArraySet;
|
||||
import android.util.Log;
|
||||
import android.view.View;
|
||||
import android.widget.Toast;
|
||||
|
||||
@@ -55,10 +61,9 @@ import java.util.Collections;
|
||||
import java.util.List;
|
||||
|
||||
public class ZenAccessSettings extends EmptyTextSettings {
|
||||
private final String TAG = "ZenAccessSettings";
|
||||
|
||||
private final SettingObserver mObserver = new SettingObserver();
|
||||
private static final String ENABLED_SERVICES_SEPARATOR = ":";
|
||||
|
||||
private Context mContext;
|
||||
private PackageManager mPkgMan;
|
||||
private NotificationManager mNoMan;
|
||||
@@ -106,7 +111,7 @@ public class ZenAccessSettings extends EmptyTextSettings {
|
||||
final PreferenceScreen screen = getPreferenceScreen();
|
||||
screen.removeAll();
|
||||
final ArrayList<ApplicationInfo> apps = new ArrayList<>();
|
||||
final ArraySet<String> requesting = mNoMan.getPackagesRequestingNotificationPolicyAccess();
|
||||
final ArraySet<String> requesting = getPackagesRequestingNotificationPolicyAccess();
|
||||
if (!requesting.isEmpty()) {
|
||||
final List<ApplicationInfo> installed = mPkgMan.getInstalledApplications(0);
|
||||
if (installed != null) {
|
||||
@@ -117,7 +122,8 @@ public class ZenAccessSettings extends EmptyTextSettings {
|
||||
}
|
||||
}
|
||||
}
|
||||
ArraySet<String> autoApproved = getEnabledNotificationListeners();
|
||||
ArraySet<String> autoApproved = new ArraySet<>();
|
||||
autoApproved.addAll(mNoMan.getEnabledNotificationListenerPackages());
|
||||
requesting.addAll(autoApproved);
|
||||
Collections.sort(apps, new PackageItemInfo.DisplayNameComparator(mPkgMan));
|
||||
for (ApplicationInfo app : apps) {
|
||||
@@ -152,20 +158,25 @@ 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());
|
||||
private ArraySet<String> getPackagesRequestingNotificationPolicyAccess() {
|
||||
ArraySet<String> requestingPackages = new ArraySet<>();
|
||||
try {
|
||||
final String[] PERM = {
|
||||
android.Manifest.permission.ACCESS_NOTIFICATION_POLICY
|
||||
};
|
||||
final ParceledListSlice list = AppGlobals.getPackageManager()
|
||||
.getPackagesHoldingPermissions(PERM, 0 /*flags*/,
|
||||
ActivityManager.getCurrentUser());
|
||||
final List<PackageInfo> pkgs = list.getList();
|
||||
if (pkgs != null) {
|
||||
for (PackageInfo info : pkgs) {
|
||||
requestingPackages.add(info.packageName);
|
||||
}
|
||||
}
|
||||
} catch(RemoteException e) {
|
||||
Log.e(TAG, "Cannot reach packagemanager", e);
|
||||
}
|
||||
return packages;
|
||||
return requestingPackages;
|
||||
}
|
||||
|
||||
private boolean hasAccess(String pkg) {
|
||||
|
@@ -179,8 +179,6 @@ public class ZenModeSettings extends ZenModeSettingsBase {
|
||||
private static ManagedServiceSettings.Config getConditionProviderConfig() {
|
||||
final ManagedServiceSettings.Config c = new ManagedServiceSettings.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";
|
||||
|
Reference in New Issue
Block a user