diff --git a/res/xml/notification_access_permission_details.xml b/res/xml/notification_access_permission_details.xml index edac955c308..55a47f5361a 100644 --- a/res/xml/notification_access_permission_details.xml +++ b/res/xml/notification_access_permission_details.xml @@ -31,21 +31,28 @@ android:title="@string/notification_access_detail_switch" settings:controller="com.android.settings.applications.specialaccess.notificationaccess.ApprovalPreferenceController"/> - /> + /> + /> + /> + /> - + \ No newline at end of file diff --git a/src/com/android/settings/applications/specialaccess/notificationaccess/AlertingTypeFilterPreferenceController.java b/src/com/android/settings/applications/specialaccess/notificationaccess/AlertingTypeFilterPreferenceController.java new file mode 100644 index 00000000000..4fc5fc6b920 --- /dev/null +++ b/src/com/android/settings/applications/specialaccess/notificationaccess/AlertingTypeFilterPreferenceController.java @@ -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; + } +} \ No newline at end of file diff --git a/src/com/android/settings/applications/specialaccess/notificationaccess/BridgedAppsPreferenceController.java b/src/com/android/settings/applications/specialaccess/notificationaccess/BridgedAppsPreferenceController.java index 9186bdb16a3..7ff3c9c4d62 100644 --- a/src/com/android/settings/applications/specialaccess/notificationaccess/BridgedAppsPreferenceController.java +++ b/src/com/android/settings/applications/specialaccess/notificationaccess/BridgedAppsPreferenceController.java @@ -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)); diff --git a/src/com/android/settings/applications/specialaccess/notificationaccess/ConversationTypeFilterPreferenceController.java b/src/com/android/settings/applications/specialaccess/notificationaccess/ConversationTypeFilterPreferenceController.java new file mode 100644 index 00000000000..5a463ed284a --- /dev/null +++ b/src/com/android/settings/applications/specialaccess/notificationaccess/ConversationTypeFilterPreferenceController.java @@ -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; + } +} \ No newline at end of file diff --git a/src/com/android/settings/applications/specialaccess/notificationaccess/NotificationAccessDetails.java b/src/com/android/settings/applications/specialaccess/notificationaccess/NotificationAccessDetails.java index 41a6efa82dc..a205b57433b 100644 --- a/src/com/android/settings/applications/specialaccess/notificationaccess/NotificationAccessDetails.java +++ b/src/com/android/settings/applications/specialaccess/notificationaccess/NotificationAccessDetails.java @@ -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; } } diff --git a/src/com/android/settings/applications/specialaccess/notificationaccess/OngoingTypeFilterPreferenceController.java b/src/com/android/settings/applications/specialaccess/notificationaccess/OngoingTypeFilterPreferenceController.java new file mode 100644 index 00000000000..6192a04fe2b --- /dev/null +++ b/src/com/android/settings/applications/specialaccess/notificationaccess/OngoingTypeFilterPreferenceController.java @@ -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; + } +} \ No newline at end of file diff --git a/src/com/android/settings/applications/specialaccess/notificationaccess/SilentTypeFilterPreferenceController.java b/src/com/android/settings/applications/specialaccess/notificationaccess/SilentTypeFilterPreferenceController.java new file mode 100644 index 00000000000..fab3b7ba89f --- /dev/null +++ b/src/com/android/settings/applications/specialaccess/notificationaccess/SilentTypeFilterPreferenceController.java @@ -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; + } +} \ No newline at end of file diff --git a/src/com/android/settings/applications/specialaccess/notificationaccess/TypeFilterPreferenceController.java b/src/com/android/settings/applications/specialaccess/notificationaccess/TypeFilterPreferenceController.java index 9d7fcc1aa0b..a5ac1e10b4f 100644 --- a/src/com/android/settings/applications/specialaccess/notificationaccess/TypeFilterPreferenceController.java +++ b/src/com/android/settings/applications/specialaccess/notificationaccess/TypeFilterPreferenceController.java @@ -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 values = new HashSet<>(); - Set 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 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 set = (Set) 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); + } } \ No newline at end of file diff --git a/tests/unit/src/com/android/settings/applications/specialaccess/notificationaccess/AlertingTypeFilterPreferenceControllerTest.java b/tests/unit/src/com/android/settings/applications/specialaccess/notificationaccess/AlertingTypeFilterPreferenceControllerTest.java new file mode 100644 index 00000000000..7eebba02dba --- /dev/null +++ b/tests/unit/src/com/android/settings/applications/specialaccess/notificationaccess/AlertingTypeFilterPreferenceControllerTest.java @@ -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); + } +} diff --git a/tests/unit/src/com/android/settings/applications/specialaccess/notificationaccess/BridgedAppsPreferenceControllerTest.java b/tests/unit/src/com/android/settings/applications/specialaccess/notificationaccess/BridgedAppsPreferenceControllerTest.java index 3cdbd3a20d5..90b89278762 100644 --- a/tests/unit/src/com/android/settings/applications/specialaccess/notificationaccess/BridgedAppsPreferenceControllerTest.java +++ b/tests/unit/src/com/android/settings/applications/specialaccess/notificationaccess/BridgedAppsPreferenceControllerTest.java @@ -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); diff --git a/tests/unit/src/com/android/settings/applications/specialaccess/notificationaccess/ConversationTypeFilterPreferenceControllerTest.java b/tests/unit/src/com/android/settings/applications/specialaccess/notificationaccess/ConversationTypeFilterPreferenceControllerTest.java new file mode 100644 index 00000000000..e12ebebcc5a --- /dev/null +++ b/tests/unit/src/com/android/settings/applications/specialaccess/notificationaccess/ConversationTypeFilterPreferenceControllerTest.java @@ -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); + } +} diff --git a/tests/unit/src/com/android/settings/applications/specialaccess/notificationaccess/OngoingTypeFilterPreferenceControllerTest.java b/tests/unit/src/com/android/settings/applications/specialaccess/notificationaccess/OngoingTypeFilterPreferenceControllerTest.java new file mode 100644 index 00000000000..eead84833e3 --- /dev/null +++ b/tests/unit/src/com/android/settings/applications/specialaccess/notificationaccess/OngoingTypeFilterPreferenceControllerTest.java @@ -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); + } +} diff --git a/tests/unit/src/com/android/settings/applications/specialaccess/notificationaccess/SilentTypeFilterPreferenceControllerTest.java b/tests/unit/src/com/android/settings/applications/specialaccess/notificationaccess/SilentTypeFilterPreferenceControllerTest.java new file mode 100644 index 00000000000..1d099e3280d --- /dev/null +++ b/tests/unit/src/com/android/settings/applications/specialaccess/notificationaccess/SilentTypeFilterPreferenceControllerTest.java @@ -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); + } +} diff --git a/tests/unit/src/com/android/settings/applications/specialaccess/notificationaccess/TypeFilterPreferenceControllerTest.java b/tests/unit/src/com/android/settings/applications/specialaccess/notificationaccess/TypeFilterPreferenceControllerTest.java index 3014066f477..d36f7755c50 100644 --- a/tests/unit/src/com/android/settings/applications/specialaccess/notificationaccess/TypeFilterPreferenceControllerTest.java +++ b/tests/unit/src/com/android/settings/applications/specialaccess/notificationaccess/TypeFilterPreferenceControllerTest.java @@ -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 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 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); } }