Updates to NLS notif type filtering

- Allow an app to tell us they will never bridge a notification
type. We will then not let a user select that type
- Checkboxes everywhere.

Test: Settings unit
Bug: 181125165
Bug: 181124973
Change-Id: I73939d3d0e99016456e009b2f104cdded98411c6
This commit is contained in:
Julia Reynolds
2021-02-24 14:47:36 -05:00
parent a78849982d
commit dc79f1ba99
14 changed files with 645 additions and 132 deletions

View File

@@ -0,0 +1,65 @@
/*
* 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_SILENT;
import static com.google.common.truth.Truth.assertThat;
import android.content.ComponentName;
import android.content.Context;
import android.content.pm.ServiceInfo;
import androidx.test.core.app.ApplicationProvider;
import androidx.test.ext.junit.runners.AndroidJUnit4;
import com.android.settings.notification.NotificationBackend;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
@RunWith(AndroidJUnit4.class)
public class AlertingTypeFilterPreferenceControllerTest {
private Context mContext;
private AlertingTypeFilterPreferenceController mController;
@Mock
NotificationBackend mNm;
ComponentName mCn = new ComponentName("a", "b");
ServiceInfo mSi = new ServiceInfo();
@Before
public void setUp() {
MockitoAnnotations.initMocks(this);
mContext = ApplicationProvider.getApplicationContext();
mController = new AlertingTypeFilterPreferenceController(mContext, "key");
mController.setCn(mCn);
mController.setNm(mNm);
mController.setServiceInfo(mSi);
mController.setUserId(0);
}
@Test
public void getType() {
assertThat(mController.getType()).isEqualTo(FLAG_FILTER_TYPE_ALERTING);
}
}

View File

@@ -35,10 +35,10 @@ import android.os.Looper;
import android.service.notification.NotificationListenerFilter;
import android.util.ArraySet;
import androidx.preference.CheckBoxPreference;
import androidx.preference.Preference;
import androidx.preference.PreferenceManager;
import androidx.preference.PreferenceScreen;
import androidx.preference.SwitchPreference;
import androidx.test.core.app.ApplicationProvider;
import androidx.test.ext.junit.runners.AndroidJUnit4;
@@ -122,7 +122,7 @@ public class BridgedAppsPreferenceControllerTest {
when(mNm.isNotificationListenerAccessGranted(mCn)).thenReturn(true);
when(mNm.getListenerFilter(mCn, 0)).thenReturn(new NotificationListenerFilter());
SwitchPreference p = mock(SwitchPreference.class);
CheckBoxPreference p = mock(CheckBoxPreference.class);
when(p.getKey()).thenReturn("pkg|12300");
mScreen.addPreference(p);
@@ -163,7 +163,7 @@ public class BridgedAppsPreferenceControllerTest {
mController.onRebuildComplete(entries);
SwitchPreference actual = mScreen.findPreference("pkg|12300");
CheckBoxPreference actual = mScreen.findPreference("pkg|12300");
assertThat(actual.isChecked()).isTrue();
assertThat(actual.getTitle()).isEqualTo("hi");
@@ -180,7 +180,7 @@ public class BridgedAppsPreferenceControllerTest {
when(mNm.isNotificationListenerAccessGranted(mCn)).thenReturn(true);
when(mNm.getListenerFilter(mCn, 0)).thenReturn(nlf);
SwitchPreference pref = new SwitchPreference(mContext);
CheckBoxPreference pref = new CheckBoxPreference(mContext);
pref.setKey("pkg|567");
mController.onPreferenceChange(pref, false);
@@ -206,7 +206,7 @@ public class BridgedAppsPreferenceControllerTest {
when(mNm.isNotificationListenerAccessGranted(mCn)).thenReturn(true);
when(mNm.getListenerFilter(mCn, 0)).thenReturn(nlf);
SwitchPreference pref = new SwitchPreference(mContext);
CheckBoxPreference pref = new CheckBoxPreference(mContext);
pref.setKey("pkg|567");
mController.onPreferenceChange(pref, true);

View File

@@ -0,0 +1,65 @@
/*
* 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 static android.service.notification.NotificationListenerService.FLAG_FILTER_TYPE_SILENT;
import static com.google.common.truth.Truth.assertThat;
import android.content.ComponentName;
import android.content.Context;
import android.content.pm.ServiceInfo;
import androidx.test.core.app.ApplicationProvider;
import androidx.test.ext.junit.runners.AndroidJUnit4;
import com.android.settings.notification.NotificationBackend;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
@RunWith(AndroidJUnit4.class)
public class ConversationTypeFilterPreferenceControllerTest {
private Context mContext;
private ConversationTypeFilterPreferenceController mController;
@Mock
NotificationBackend mNm;
ComponentName mCn = new ComponentName("a", "b");
ServiceInfo mSi = new ServiceInfo();
@Before
public void setUp() {
MockitoAnnotations.initMocks(this);
mContext = ApplicationProvider.getApplicationContext();
mController = new ConversationTypeFilterPreferenceController(mContext, "key");
mController.setCn(mCn);
mController.setNm(mNm);
mController.setServiceInfo(mSi);
mController.setUserId(0);
}
@Test
public void getType() {
assertThat(mController.getType()).isEqualTo(FLAG_FILTER_TYPE_CONVERSATIONS);
}
}

View File

@@ -0,0 +1,65 @@
/*
* 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 static android.service.notification.NotificationListenerService.FLAG_FILTER_TYPE_SILENT;
import static com.google.common.truth.Truth.assertThat;
import android.content.ComponentName;
import android.content.Context;
import android.content.pm.ServiceInfo;
import androidx.test.core.app.ApplicationProvider;
import androidx.test.ext.junit.runners.AndroidJUnit4;
import com.android.settings.notification.NotificationBackend;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
@RunWith(AndroidJUnit4.class)
public class OngoingTypeFilterPreferenceControllerTest {
private Context mContext;
private OngoingTypeFilterPreferenceController mController;
@Mock
NotificationBackend mNm;
ComponentName mCn = new ComponentName("a", "b");
ServiceInfo mSi = new ServiceInfo();
@Before
public void setUp() {
MockitoAnnotations.initMocks(this);
mContext = ApplicationProvider.getApplicationContext();
mController = new OngoingTypeFilterPreferenceController(mContext, "key");
mController.setCn(mCn);
mController.setNm(mNm);
mController.setServiceInfo(mSi);
mController.setUserId(0);
}
@Test
public void getType() {
assertThat(mController.getType()).isEqualTo(FLAG_FILTER_TYPE_ONGOING);
}
}

View File

@@ -0,0 +1,64 @@
/*
* 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 static com.google.common.truth.Truth.assertThat;
import android.content.ComponentName;
import android.content.Context;
import android.content.pm.ServiceInfo;
import androidx.test.core.app.ApplicationProvider;
import androidx.test.ext.junit.runners.AndroidJUnit4;
import com.android.settings.notification.NotificationBackend;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
@RunWith(AndroidJUnit4.class)
public class SilentTypeFilterPreferenceControllerTest {
private Context mContext;
private SilentTypeFilterPreferenceController mController;
@Mock
NotificationBackend mNm;
ComponentName mCn = new ComponentName("a", "b");
ServiceInfo mSi = new ServiceInfo();
@Before
public void setUp() {
MockitoAnnotations.initMocks(this);
mContext = ApplicationProvider.getApplicationContext();
mController = new SilentTypeFilterPreferenceController(mContext, "key");
mController.setCn(mCn);
mController.setNm(mNm);
mController.setServiceInfo(mSi);
mController.setUserId(0);
}
@Test
public void getType() {
assertThat(mController.getType()).isEqualTo(FLAG_FILTER_TYPE_SILENT);
}
}

View File

@@ -28,10 +28,13 @@ import static org.mockito.Mockito.when;
import android.content.ComponentName;
import android.content.Context;
import android.content.pm.ServiceInfo;
import android.os.Bundle;
import android.service.notification.NotificationListenerFilter;
import android.service.notification.NotificationListenerService;
import android.util.ArraySet;
import androidx.preference.MultiSelectListPreference;
import androidx.preference.CheckBoxPreference;
import androidx.test.core.app.ApplicationProvider;
import androidx.test.ext.junit.runners.AndroidJUnit4;
@@ -54,23 +57,76 @@ public class TypeFilterPreferenceControllerTest {
@Mock
NotificationBackend mNm;
ComponentName mCn = new ComponentName("a", "b");
ServiceInfo mSi = new ServiceInfo();
private static class TestTypeFilterPreferenceController extends TypeFilterPreferenceController {
public TestTypeFilterPreferenceController(Context context, String key) {
super(context, key);
}
@Override
protected int getType() {
return 32;
}
}
@Before
public void setUp() {
MockitoAnnotations.initMocks(this);
mContext = ApplicationProvider.getApplicationContext();
mController = new TypeFilterPreferenceController(mContext, "key");
mController = new TestTypeFilterPreferenceController(mContext, "key");
mController.setCn(mCn);
mController.setNm(mNm);
mController.setServiceInfo(mSi);
mController.setUserId(0);
}
@Test
public void updateState_enabled() {
public void updateState_enabled_noMetaData() {
when(mNm.isNotificationListenerAccessGranted(mCn)).thenReturn(true);
when(mNm.getListenerFilter(mCn, 0)).thenReturn(new NotificationListenerFilter());
MultiSelectListPreference pref = new MultiSelectListPreference(mContext);
CheckBoxPreference pref = new CheckBoxPreference(mContext);
mController.updateState(pref);
assertThat(pref.isEnabled()).isTrue();
}
@Test
public void updateState_enabled_metaData_notTheDisableFilter() {
mSi.metaData = new Bundle();
mSi.metaData.putCharSequence("test", "value");
when(mNm.isNotificationListenerAccessGranted(mCn)).thenReturn(true);
when(mNm.getListenerFilter(mCn, 0)).thenReturn(new NotificationListenerFilter());
CheckBoxPreference pref = new CheckBoxPreference(mContext);
mController.updateState(pref);
assertThat(pref.isEnabled()).isTrue();
}
@Test
public void updateState_enabled_metaData_disableFilter_notThisField() {
mSi.metaData = new Bundle();
mSi.metaData.putCharSequence(NotificationListenerService.META_DATA_DISABLED_FILTER_TYPES,
"1,2");
when(mNm.isNotificationListenerAccessGranted(mCn)).thenReturn(true);
when(mNm.getListenerFilter(mCn, 0)).thenReturn(new NotificationListenerFilter());
CheckBoxPreference pref = new CheckBoxPreference(mContext);
mController.updateState(pref);
assertThat(pref.isEnabled()).isTrue();
}
@Test
public void updateState_enabled_metaData_disableFilter_thisField_stateIsChecked() {
mSi.metaData = new Bundle();
mSi.metaData.putCharSequence(NotificationListenerService.META_DATA_DISABLED_FILTER_TYPES,
"1,2,32");
when(mNm.isNotificationListenerAccessGranted(mCn)).thenReturn(true);
when(mNm.getListenerFilter(mCn, 0)).thenReturn(
new NotificationListenerFilter(32, new ArraySet<>()));
CheckBoxPreference pref = new CheckBoxPreference(mContext);
mController.updateState(pref);
assertThat(pref.isEnabled()).isTrue();
@@ -80,57 +136,86 @@ public class TypeFilterPreferenceControllerTest {
public void updateState_disabled() {
when(mNm.isNotificationListenerAccessGranted(mCn)).thenReturn(false);
when(mNm.getListenerFilter(mCn, 0)).thenReturn(new NotificationListenerFilter());
MultiSelectListPreference pref = new MultiSelectListPreference(mContext);
CheckBoxPreference pref = new CheckBoxPreference(mContext);
mController.updateState(pref);
assertThat(pref.isEnabled()).isFalse();
}
@Test
public void updateState() {
NotificationListenerFilter nlf = new NotificationListenerFilter(FLAG_FILTER_TYPE_ONGOING
| FLAG_FILTER_TYPE_SILENT, new ArraySet<>());
public void updateState_disabled_metaData_disableFilter_thisField_stateIsNotChecked() {
mSi.metaData = new Bundle();
mSi.metaData.putCharSequence(NotificationListenerService.META_DATA_DISABLED_FILTER_TYPES,
"1,2,32");
when(mNm.isNotificationListenerAccessGranted(mCn)).thenReturn(true);
when(mNm.getListenerFilter(mCn, 0)).thenReturn(nlf);
NotificationListenerFilter before = new NotificationListenerFilter(4, new ArraySet<>());
when(mNm.getListenerFilter(mCn, 0)).thenReturn(before);
CheckBoxPreference pref = new CheckBoxPreference(mContext);
MultiSelectListPreference pref = new MultiSelectListPreference(mContext);
mController.updateState(pref);
assertThat(pref.getValues()).containsExactlyElementsIn(
new String[] {String.valueOf(FLAG_FILTER_TYPE_ONGOING),
String.valueOf(FLAG_FILTER_TYPE_SILENT)});
assertThat(pref.getSummary()).isNotNull();
assertThat(pref.isChecked()).isFalse();
assertThat(pref.isEnabled()).isFalse();
}
@Test
public void getSummary() {
public void updateState_checked() {
NotificationListenerFilter nlf = new NotificationListenerFilter(mController.getType(),
new ArraySet<>());
when(mNm.isNotificationListenerAccessGranted(mCn)).thenReturn(true);
when(mNm.getListenerFilter(mCn, 0)).thenReturn(nlf);
CheckBoxPreference pref = new CheckBoxPreference(mContext);
mController.updateState(pref);
assertThat(pref.isChecked()).isTrue();
}
@Test
public void updateState_unchecked() {
NotificationListenerFilter nlf = new NotificationListenerFilter(mController.getType() - 1,
new ArraySet<>());
when(mNm.isNotificationListenerAccessGranted(mCn)).thenReturn(true);
when(mNm.getListenerFilter(mCn, 0)).thenReturn(nlf);
CheckBoxPreference pref = new CheckBoxPreference(mContext);
mController.updateState(pref);
assertThat(pref.isChecked()).isFalse();
}
@Test
public void onPreferenceChange_true() {
NotificationListenerFilter nlf = new NotificationListenerFilter(FLAG_FILTER_TYPE_ONGOING
| FLAG_FILTER_TYPE_CONVERSATIONS, new ArraySet<>());
when(mNm.isNotificationListenerAccessGranted(mCn)).thenReturn(true);
when(mNm.getListenerFilter(mCn, 0)).thenReturn(nlf);
MultiSelectListPreference pref = new MultiSelectListPreference(mContext);
mController.updateState(pref);
CheckBoxPreference pref = new CheckBoxPreference(mContext);
assertThat(mController.getSummary().toString()).ignoringCase().contains("ongoing");
assertThat(mController.getSummary().toString()).ignoringCase().contains("conversation");
}
@Test
public void onPreferenceChange() {
NotificationListenerFilter nlf = new NotificationListenerFilter(FLAG_FILTER_TYPE_ONGOING
| FLAG_FILTER_TYPE_CONVERSATIONS, new ArraySet<>());
when(mNm.isNotificationListenerAccessGranted(mCn)).thenReturn(true);
when(mNm.getListenerFilter(mCn, 0)).thenReturn(nlf);
MultiSelectListPreference pref = new MultiSelectListPreference(mContext);
mController.onPreferenceChange(pref, Set.of("8", "1", "4"));
mController.onPreferenceChange(pref, true);
ArgumentCaptor<NotificationListenerFilter> captor =
ArgumentCaptor.forClass(NotificationListenerFilter.class);
verify(mNm).setListenerFilter(eq(mCn), eq(0), captor.capture());
assertThat(captor.getValue().getTypes()).isEqualTo(FLAG_FILTER_TYPE_CONVERSATIONS
| FLAG_FILTER_TYPE_SILENT | FLAG_FILTER_TYPE_ONGOING);
| FLAG_FILTER_TYPE_ONGOING | mController.getType());
}
@Test
public void onPreferenceChange_false() {
NotificationListenerFilter nlf = new NotificationListenerFilter(FLAG_FILTER_TYPE_ONGOING
| FLAG_FILTER_TYPE_CONVERSATIONS | mController.getType(), new ArraySet<>());
when(mNm.isNotificationListenerAccessGranted(mCn)).thenReturn(true);
when(mNm.getListenerFilter(mCn, 0)).thenReturn(nlf);
CheckBoxPreference pref = new CheckBoxPreference(mContext);
mController.onPreferenceChange(pref, false);
ArgumentCaptor<NotificationListenerFilter> captor =
ArgumentCaptor.forClass(NotificationListenerFilter.class);
verify(mNm).setListenerFilter(eq(mCn), eq(0), captor.capture());
assertThat(captor.getValue().getTypes()).isEqualTo(FLAG_FILTER_TYPE_CONVERSATIONS
| FLAG_FILTER_TYPE_ONGOING);
}
}