Merge "Updates to NLS notif type filtering" into sc-dev
This commit is contained in:
committed by
Android (Google) Code Review
commit
6698d3b2c8
@@ -0,0 +1,35 @@
|
||||
/*
|
||||
* 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 android.content.Context;
|
||||
|
||||
public class AlertingTypeFilterPreferenceController extends TypeFilterPreferenceController {
|
||||
|
||||
private static final String TAG = "AlertFilterPrefCntlr";
|
||||
|
||||
public AlertingTypeFilterPreferenceController(Context context, String key) {
|
||||
super(context, key);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected int getType() {
|
||||
return FLAG_FILTER_TYPE_ALERTING;
|
||||
}
|
||||
}
|
||||
@@ -20,6 +20,7 @@ import android.os.UserHandle;
|
||||
import android.service.notification.NotificationListenerFilter;
|
||||
|
||||
import androidx.annotation.VisibleForTesting;
|
||||
import androidx.preference.CheckBoxPreference;
|
||||
import androidx.preference.Preference;
|
||||
import androidx.preference.PreferenceScreen;
|
||||
import androidx.preference.SwitchPreference;
|
||||
@@ -130,9 +131,9 @@ public class BridgedAppsPreferenceController extends BasePreferenceController im
|
||||
}
|
||||
final String prefKey = entry.info.packageName + "|" + entry.info.uid;
|
||||
appsKeySet.add(prefKey);
|
||||
SwitchPreference preference = mScreen.findPreference(prefKey);
|
||||
CheckBoxPreference preference = mScreen.findPreference(prefKey);
|
||||
if (preference == null) {
|
||||
preference = new SwitchPreference(mScreen.getContext());
|
||||
preference = new CheckBoxPreference(mScreen.getContext());
|
||||
preference.setIcon(entry.icon);
|
||||
preference.setTitle(entry.label);
|
||||
preference.setKey(prefKey);
|
||||
@@ -172,7 +173,7 @@ public class BridgedAppsPreferenceController extends BasePreferenceController im
|
||||
}
|
||||
|
||||
public boolean onPreferenceChange(Preference preference, Object newValue) {
|
||||
if (preference instanceof SwitchPreference) {
|
||||
if (preference instanceof CheckBoxPreference) {
|
||||
String packageName = preference.getKey().substring(0, preference.getKey().indexOf("|"));
|
||||
int uid = Integer.parseInt(preference.getKey().substring(
|
||||
preference.getKey().indexOf("|") + 1));
|
||||
|
||||
@@ -0,0 +1,35 @@
|
||||
/*
|
||||
* 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_CONVERSATIONS;
|
||||
|
||||
import android.content.Context;
|
||||
|
||||
public class ConversationTypeFilterPreferenceController extends TypeFilterPreferenceController {
|
||||
|
||||
private static final String TAG = "ConvFilterPrefCntlr";
|
||||
|
||||
public ConversationTypeFilterPreferenceController(Context context, String key) {
|
||||
super(context, key);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected int getType() {
|
||||
return FLAG_FILTER_TYPE_CONVERSATIONS;
|
||||
}
|
||||
}
|
||||
@@ -21,6 +21,7 @@ import static com.android.settings.applications.AppInfoBase.ARG_PACKAGE_NAME;
|
||||
import android.app.Activity;
|
||||
import android.app.NotificationManager;
|
||||
import android.app.settings.SettingsEnums;
|
||||
import android.bluetooth.BluetoothAdapter;
|
||||
import android.content.ComponentName;
|
||||
import android.content.Context;
|
||||
import android.content.Intent;
|
||||
@@ -49,7 +50,9 @@ 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.bluetooth.CachedBluetoothDevice;
|
||||
|
||||
import java.util.Collection;
|
||||
import java.util.List;
|
||||
import java.util.Objects;
|
||||
|
||||
@@ -60,6 +63,7 @@ public class NotificationAccessDetails extends DashboardFragment {
|
||||
private NotificationListenerFilter mNlf;
|
||||
private ComponentName mComponentName;
|
||||
private CharSequence mServiceName;
|
||||
protected ServiceInfo mServiceInfo;
|
||||
protected PackageInfo mPackageInfo;
|
||||
protected int mUserId;
|
||||
protected String mPackageName;
|
||||
@@ -96,10 +100,18 @@ public class NotificationAccessDetails extends DashboardFragment {
|
||||
.setPackageInfo(mPackageInfo)
|
||||
.setPm(context.getPackageManager())
|
||||
.setServiceName(mServiceName);
|
||||
use(TypeFilterPreferenceController.class)
|
||||
.setNm(new NotificationBackend())
|
||||
.setCn(mComponentName)
|
||||
.setUserId(mUserId);
|
||||
getPreferenceControllers().forEach(controllers -> {
|
||||
controllers.forEach(controller -> {
|
||||
if (controller instanceof TypeFilterPreferenceController) {
|
||||
TypeFilterPreferenceController tfpc =
|
||||
(TypeFilterPreferenceController) controller;
|
||||
tfpc.setNm(new NotificationBackend())
|
||||
.setCn(mComponentName)
|
||||
.setServiceInfo(mServiceInfo)
|
||||
.setUserId(mUserId);
|
||||
}
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -205,20 +217,34 @@ public class NotificationAccessDetails extends DashboardFragment {
|
||||
// along to keep business logic out of this file
|
||||
public void disable(final ComponentName cn) {
|
||||
final PreferenceScreen screen = getPreferenceScreen();
|
||||
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()));
|
||||
ApprovalPreferenceController apc = use(ApprovalPreferenceController.class);
|
||||
apc.disable(cn);
|
||||
apc.updateState(screen.findPreference(apc.getPreferenceKey()));
|
||||
getPreferenceControllers().forEach(controllers -> {
|
||||
controllers.forEach(controller -> {
|
||||
if (controller instanceof TypeFilterPreferenceController) {
|
||||
TypeFilterPreferenceController tfpc =
|
||||
(TypeFilterPreferenceController) controller;
|
||||
tfpc.updateState(screen.findPreference(tfpc.getPreferenceKey()));
|
||||
}
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
protected void enable(ComponentName cn) {
|
||||
final PreferenceScreen screen = getPreferenceScreen();
|
||||
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()));
|
||||
ApprovalPreferenceController apc = use(ApprovalPreferenceController.class);
|
||||
apc.enable(cn);
|
||||
apc.updateState(screen.findPreference(apc.getPreferenceKey()));
|
||||
getPreferenceControllers().forEach(controllers -> {
|
||||
controllers.forEach(controller -> {
|
||||
if (controller instanceof TypeFilterPreferenceController) {
|
||||
TypeFilterPreferenceController tfpc =
|
||||
(TypeFilterPreferenceController) controller;
|
||||
tfpc.updateState(screen.findPreference(tfpc.getPreferenceKey()));
|
||||
}
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
// To save binder calls, load this in the fragment rather than each preference controller
|
||||
@@ -239,6 +265,7 @@ public class NotificationAccessDetails extends DashboardFragment {
|
||||
if (Objects.equals(mComponentName, info.getComponentName())) {
|
||||
mIsNls = true;
|
||||
mServiceName = info.loadLabel(mPm);
|
||||
mServiceInfo = info;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,35 @@
|
||||
/*
|
||||
* 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_ONGOING;
|
||||
|
||||
import android.content.Context;
|
||||
|
||||
public class OngoingTypeFilterPreferenceController extends TypeFilterPreferenceController {
|
||||
|
||||
private static final String TAG = "OngoingFilterPrefCntlr";
|
||||
|
||||
public OngoingTypeFilterPreferenceController(Context context, String key) {
|
||||
super(context, key);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected int getType() {
|
||||
return FLAG_FILTER_TYPE_ONGOING;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,35 @@
|
||||
/*
|
||||
* 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_SILENT;
|
||||
|
||||
import android.content.Context;
|
||||
|
||||
public class SilentTypeFilterPreferenceController extends TypeFilterPreferenceController {
|
||||
|
||||
private static final String TAG = "SilentFilterPrefCntlr";
|
||||
|
||||
public SilentTypeFilterPreferenceController(Context context, String key) {
|
||||
super(context, key);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected int getType() {
|
||||
return FLAG_FILTER_TYPE_SILENT;
|
||||
}
|
||||
}
|
||||
@@ -16,35 +16,31 @@
|
||||
|
||||
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.content.pm.ServiceInfo;
|
||||
import android.service.notification.NotificationListenerFilter;
|
||||
import android.service.notification.NotificationListenerService;
|
||||
import android.text.TextUtils;
|
||||
|
||||
import androidx.preference.MultiSelectListPreference;
|
||||
import androidx.preference.CheckBoxPreference;
|
||||
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
|
||||
public abstract class TypeFilterPreferenceController extends BasePreferenceController implements
|
||||
PreferenceControllerMixin, Preference.OnPreferenceChangeListener {
|
||||
|
||||
private static final String TAG = "TypeFilterPrefCntlr";
|
||||
private static final String XML_SEPARATOR = ",";
|
||||
|
||||
private ComponentName mCn;
|
||||
private int mUserId;
|
||||
private NotificationBackend mNm;
|
||||
private NotificationListenerFilter mNlf;
|
||||
private ServiceInfo mSi;
|
||||
|
||||
public TypeFilterPreferenceController(Context context, String key) {
|
||||
super(context, key);
|
||||
@@ -65,6 +61,13 @@ public class TypeFilterPreferenceController extends BasePreferenceController imp
|
||||
return this;
|
||||
}
|
||||
|
||||
public TypeFilterPreferenceController setServiceInfo(ServiceInfo si) {
|
||||
mSi = si;
|
||||
return this;
|
||||
}
|
||||
|
||||
abstract protected int getType();
|
||||
|
||||
@Override
|
||||
public int getAvailabilityStatus() {
|
||||
if (mNm.isNotificationListenerAccessGranted(mCn)) {
|
||||
@@ -74,71 +77,62 @@ public class TypeFilterPreferenceController extends BasePreferenceController imp
|
||||
}
|
||||
}
|
||||
|
||||
@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;
|
||||
boolean enabled = (boolean) newValue;
|
||||
|
||||
int newFilter = 0;
|
||||
for (String filterType : set) {
|
||||
newFilter |= Integer.parseInt(filterType);
|
||||
int newFilter = mNlf.getTypes();
|
||||
if (enabled) {
|
||||
newFilter |= getType();
|
||||
} else {
|
||||
newFilter &= ~getType();
|
||||
}
|
||||
mNlf.setTypes(newFilter);
|
||||
preference.setSummary(getSummary());
|
||||
mNm.setListenerFilter(mCn, mUserId, mNlf);
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void updateState(Preference pref) {
|
||||
mNlf = mNm.getListenerFilter(mCn, mUserId);
|
||||
|
||||
CheckBoxPreference check = (CheckBoxPreference) pref;
|
||||
check.setChecked(hasFlag(mNlf.getTypes(), getType()));
|
||||
|
||||
boolean disableRequestedByApp = false;
|
||||
if (mSi != null) {
|
||||
if (mSi.metaData != null && mSi.metaData.containsKey(
|
||||
NotificationListenerService.META_DATA_DISABLED_FILTER_TYPES)) {
|
||||
String typeList = mSi.metaData.get(
|
||||
NotificationListenerService.META_DATA_DISABLED_FILTER_TYPES).toString();
|
||||
if (typeList != null) {
|
||||
int types = 0;
|
||||
String[] typeStrings = typeList.split(XML_SEPARATOR);
|
||||
for (int i = 0; i < typeStrings.length; i++) {
|
||||
if (TextUtils.isEmpty(typeStrings[i])) {
|
||||
continue;
|
||||
}
|
||||
try {
|
||||
types |= Integer.parseInt(typeStrings[i]);
|
||||
} catch (NumberFormatException e) {
|
||||
// skip
|
||||
}
|
||||
}
|
||||
if (hasFlag(types, getType())) {
|
||||
disableRequestedByApp = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
// Apps can prevent a category from being turned on, but not turned off
|
||||
boolean disabledByApp = disableRequestedByApp && !check.isChecked();
|
||||
pref.setEnabled(getAvailabilityStatus() == AVAILABLE && !disabledByApp);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user