Merge "Allow notification filtering per listener."

This commit is contained in:
Julia Reynolds
2021-01-08 03:14:52 +00:00
committed by Android (Google) Code Review
7 changed files with 355 additions and 2 deletions

View File

@@ -42,11 +42,10 @@ import com.android.settings.R;
import com.android.settings.SettingsActivity;
import com.android.settings.applications.manageapplications.ManageApplications;
import com.android.settings.dashboard.DashboardFragment;
import com.android.settings.notification.NotificationBackend;
import com.android.settingslib.RestrictedLockUtils;
import com.android.settingslib.RestrictedLockUtilsInternal;
import com.android.settingslib.applications.ApplicationsState;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
@@ -91,6 +90,10 @@ public class NotificationAccessDetails extends DashboardFragment {
.setPackageInfo(mPackageInfo)
.setPm(context.getPackageManager())
.setServiceName(mServiceName);
use(TypeFilterPreferenceController.class)
.setNm(new NotificationBackend())
.setCn(mComponentName)
.setUserId(mUserId);
}
@Override
@@ -172,6 +175,8 @@ public class NotificationAccessDetails extends DashboardFragment {
ApprovalPreferenceController controller = use(ApprovalPreferenceController.class);
controller.disable(cn);
controller.updateState(screen.findPreference(controller.getPreferenceKey()));
TypeFilterPreferenceController dependent1 = use(TypeFilterPreferenceController.class);
dependent1.updateState(screen.findPreference(dependent1.getPreferenceKey()));
}
protected void enable(ComponentName cn) {
@@ -179,6 +184,8 @@ public class NotificationAccessDetails extends DashboardFragment {
ApprovalPreferenceController controller = use(ApprovalPreferenceController.class);
controller.enable(cn);
controller.updateState(screen.findPreference(controller.getPreferenceKey()));
TypeFilterPreferenceController dependent1 = use(TypeFilterPreferenceController.class);
dependent1.updateState(screen.findPreference(dependent1.getPreferenceKey()));
}
// To save binder calls, load this in the fragment rather than each preference controller

View File

@@ -0,0 +1,144 @@
/*
* Copyright (C) 2021 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.android.settings.applications.specialaccess.notificationaccess;
import static android.service.notification.NotificationListenerService.FLAG_FILTER_TYPE_ALERTING;
import static android.service.notification.NotificationListenerService.FLAG_FILTER_TYPE_CONVERSATIONS;
import static android.service.notification.NotificationListenerService.FLAG_FILTER_TYPE_ONGOING;
import static android.service.notification.NotificationListenerService.FLAG_FILTER_TYPE_SILENT;
import android.content.ComponentName;
import android.content.Context;
import android.service.notification.NotificationListenerFilter;
import androidx.preference.MultiSelectListPreference;
import androidx.preference.Preference;
import com.android.settings.R;
import com.android.settings.core.BasePreferenceController;
import com.android.settings.core.PreferenceControllerMixin;
import com.android.settings.notification.NotificationBackend;
import java.util.HashSet;
import java.util.Set;
public class TypeFilterPreferenceController extends BasePreferenceController implements
PreferenceControllerMixin, Preference.OnPreferenceChangeListener {
private static final String TAG = "TypeFilterPrefCntlr";
private ComponentName mCn;
private int mUserId;
private NotificationBackend mNm;
private NotificationListenerFilter mNlf;
public TypeFilterPreferenceController(Context context, String key) {
super(context, key);
}
public TypeFilterPreferenceController setCn(ComponentName cn) {
mCn = cn;
return this;
}
public TypeFilterPreferenceController setUserId(int userId) {
mUserId = userId;
return this;
}
public TypeFilterPreferenceController setNm(NotificationBackend nm) {
mNm = nm;
return this;
}
@Override
public int getAvailabilityStatus() {
if (mNm.isNotificationListenerAccessGranted(mCn)) {
return AVAILABLE;
} else {
return DISABLED_DEPENDENT_SETTING;
}
}
@Override
public void updateState(Preference pref) {
mNlf = mNm.getListenerFilter(mCn, mUserId);
Set<String> values = new HashSet<>();
Set<String> entries = new HashSet<>();
if (hasFlag(mNlf.getTypes(), FLAG_FILTER_TYPE_ONGOING)) {
values.add(String.valueOf(FLAG_FILTER_TYPE_ONGOING));
entries.add(mContext.getString(R.string.notif_type_ongoing));
}
if (hasFlag(mNlf.getTypes(), FLAG_FILTER_TYPE_CONVERSATIONS)) {
values.add(String.valueOf(FLAG_FILTER_TYPE_CONVERSATIONS));
entries.add(mContext.getString(R.string.notif_type_conversation));
}
if (hasFlag(mNlf.getTypes(), FLAG_FILTER_TYPE_ALERTING)) {
values.add(String.valueOf(FLAG_FILTER_TYPE_ALERTING));
entries.add(mContext.getString(R.string.notif_type_alerting));
}
if (hasFlag(mNlf.getTypes(), FLAG_FILTER_TYPE_SILENT)) {
values.add(String.valueOf(FLAG_FILTER_TYPE_SILENT));
entries.add(mContext.getString(R.string.notif_type_silent));
}
final MultiSelectListPreference preference = (MultiSelectListPreference) pref;
preference.setValues(values);
super.updateState(preference);
pref.setEnabled(getAvailabilityStatus() == AVAILABLE);
}
private boolean hasFlag(int value, int flag) {
return (value & flag) != 0;
}
public CharSequence getSummary() {
Set<String> entries = new HashSet<>();
if (hasFlag(mNlf.getTypes(), FLAG_FILTER_TYPE_ONGOING)) {
entries.add(mContext.getString(R.string.notif_type_ongoing));
}
if (hasFlag(mNlf.getTypes(), FLAG_FILTER_TYPE_CONVERSATIONS)) {
entries.add(mContext.getString(R.string.notif_type_conversation));
}
if (hasFlag(mNlf.getTypes(), FLAG_FILTER_TYPE_ALERTING)) {
entries.add(mContext.getString(R.string.notif_type_alerting));
}
if (hasFlag(mNlf.getTypes(), FLAG_FILTER_TYPE_SILENT)) {
entries.add(mContext.getString(R.string.notif_type_silent));
}
return String.join(System.lineSeparator(), entries);
}
@Override
public boolean onPreferenceChange(Preference preference, Object newValue) {
// retrieve latest in case the package filter has changed
mNlf = mNm.getListenerFilter(mCn, mUserId);
Set<String> set = (Set<String>) newValue;
int newFilter = 0;
for (String filterType : set) {
newFilter |= Integer.parseInt(filterType);
}
mNlf.setTypes(newFilter);
preference.setSummary(getSummary());
mNm.setListenerFilter(mCn, mUserId, mNlf);
return true;
}
}

View File

@@ -45,6 +45,7 @@ import android.os.RemoteException;
import android.os.ServiceManager;
import android.os.UserHandle;
import android.service.notification.ConversationChannelWrapper;
import android.service.notification.NotificationListenerFilter;
import android.text.format.DateUtils;
import android.util.IconDrawableFactory;
import android.util.Log;
@@ -593,6 +594,32 @@ public class NotificationBackend {
}
}
public NotificationListenerFilter getListenerFilter(ComponentName cn, int userId) {
try {
return sINM.getListenerFilter(cn, userId);
} catch (Exception e) {
Log.w(TAG, "Error calling NoMan", e);
}
return new NotificationListenerFilter();
}
public void setListenerFilter(ComponentName cn, int userId, NotificationListenerFilter nlf) {
try {
sINM.setListenerFilter(cn, userId, nlf);
} catch (Exception e) {
Log.w(TAG, "Error calling NoMan", e);
}
}
public boolean isNotificationListenerAccessGranted(ComponentName cn) {
try {
return sINM.isNotificationListenerAccessGranted(cn);
} catch (Exception e) {
Log.w(TAG, "Error calling NoMan", e);
}
return false;
}
/**
* NotificationsSentState contains how often an app sends notifications and how recently it sent
* one.