Merge "Add new settings page for apps overriding dnd"
diff --git a/res/values/strings.xml b/res/values/strings.xml
index 2eeedc6..fee22d2 100644
--- a/res/values/strings.xml
+++ b/res/values/strings.xml
@@ -8043,6 +8043,19 @@
<!-- [CHAR LIMIT=80] Zen mode settings: Allow event notifications/sounds to bypass DND -->
<string name="zen_mode_events">Allow events</string>
+ <!-- [CHAR LIMIT=100] Zen mode settings: Allow apps to bypass DND -->
+ <string name="zen_mode_bypassing_apps">Allow apps to override</string>
+
+ <!-- [CHAR LIMIT=100] Zen mode settings: Allow apps to bypass DND title-->
+ <string name="zen_mode_bypassing_apps_title">Overrides Do Not Disturb</string>
+
+ <!-- [CHAR LIMIT=80] Zen mode settings: Allow apps to bypass DND -->
+ <plurals name="zen_mode_bypassing_apps_subtext">
+ <item quantity="zero">No apps can override Do Not Disturb</item>
+ <item quantity="one">1 app can override Do Not Disturb</item>
+ <item quantity="other"><xliff:g id="number" example="2">%1$d</xliff:g> apps can override Do Not Disturb</item>
+ </plurals>
+
<!-- [CHAR LIMIT=50] Zen mode settings: Events (ie: calendar events) -->
<string name="zen_mode_events_list">events</string>
diff --git a/res/xml/zen_mode_bypassing_apps.xml b/res/xml/zen_mode_bypassing_apps.xml
new file mode 100644
index 0000000..0789839
--- /dev/null
+++ b/res/xml/zen_mode_bypassing_apps.xml
@@ -0,0 +1,21 @@
+<?xml version="1.0" encoding="utf-8"?>
+<!--
+ Copyright (C) 2018 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.
+-->
+
+<PreferenceScreen
+ xmlns:android="http://schemas.android.com/apk/res/android"
+ android:key="zen_mode_bypassing_apps_screen"
+ android:title="@string/zen_mode_bypassing_apps_title" />
diff --git a/res/xml/zen_mode_sound_vibration_settings.xml b/res/xml/zen_mode_sound_vibration_settings.xml
index 3d1da2f..30e2abb 100644
--- a/res/xml/zen_mode_sound_vibration_settings.xml
+++ b/res/xml/zen_mode_sound_vibration_settings.xml
@@ -20,50 +20,55 @@
android:key="zen_mode_sound_vibration_settings_page"
android:title="@string/zen_category_exceptions" >
- <PreferenceCategory
- android:title="@string/zen_mode_settings_category"
- android:key="zen_mode_settings_category_sound_vibration">
+ <PreferenceCategory
+ android:title="@string/zen_mode_settings_category"
+ android:key="zen_mode_settings_category_sound_vibration">
- <!-- Calls -->
- <Preference
- android:key="zen_mode_calls_settings"
- android:title="@string/zen_mode_calls_title"
- android:fragment="com.android.settings.notification.ZenModeCallsSettings" />
+ <!-- Calls -->
+ <Preference
+ android:key="zen_mode_calls_settings"
+ android:title="@string/zen_mode_calls_title"
+ android:fragment="com.android.settings.notification.ZenModeCallsSettings" />
- <!-- Messages -->
- <Preference
- android:key="zen_mode_messages_settings"
- android:title="@string/zen_mode_messages_title"
- android:fragment="com.android.settings.notification.ZenModeMessagesSettings" />
+ <!-- Messages -->
+ <Preference
+ android:key="zen_mode_messages_settings"
+ android:title="@string/zen_mode_messages_title"
+ android:fragment="com.android.settings.notification.ZenModeMessagesSettings" />
- <!-- Alarms -->
- <SwitchPreference
- android:key="zen_mode_alarms"
- android:title="@string/zen_mode_alarms"/>
+ <!-- Alarms -->
+ <SwitchPreference
+ android:key="zen_mode_alarms"
+ android:title="@string/zen_mode_alarms"/>
- <!-- Media -->
- <SwitchPreference
- android:key="zen_mode_media"
- android:title="@string/zen_mode_media"/>
+ <!-- Media -->
+ <SwitchPreference
+ android:key="zen_mode_media"
+ android:title="@string/zen_mode_media"/>
- <!-- System -->
- <SwitchPreference
- android:key="zen_mode_system"
- android:title="@string/zen_mode_system"/>
+ <!-- System -->
+ <SwitchPreference
+ android:key="zen_mode_system"
+ android:title="@string/zen_mode_system"/>
- <!-- Reminders -->
- <SwitchPreference
- android:key="zen_mode_reminders"
- android:title="@string/zen_mode_reminders"/>
+ <!-- Reminders -->
+ <SwitchPreference
+ android:key="zen_mode_reminders"
+ android:title="@string/zen_mode_reminders"/>
- <!-- Events -->
- <SwitchPreference
- android:key="zen_mode_events"
- android:title="@string/zen_mode_events"/>
+ <!-- Events -->
+ <SwitchPreference
+ android:key="zen_mode_events"
+ android:title="@string/zen_mode_events"/>
- <!-- TODO: beverlyt, add "Allow apps to override" -->
- </PreferenceCategory>
+ <!-- Apps overriding DND -->
+ <Preference
+ android:key="zen_mode_bypassing_apps"
+ android:title="@string/zen_mode_bypassing_apps"
+ android:fragment="com.android.settings.notification.ZenModeBypassingAppsSettings"/>
- <com.android.settingslib.widget.FooterPreference/>
+ </PreferenceCategory>
+
+ <com.android.settingslib.widget.FooterPreference/>
</PreferenceScreen>
diff --git a/src/com/android/settings/notification/NotificationBackend.java b/src/com/android/settings/notification/NotificationBackend.java
index 03b4ec3..d5fea09 100644
--- a/src/com/android/settings/notification/NotificationBackend.java
+++ b/src/com/android/settings/notification/NotificationBackend.java
@@ -21,6 +21,7 @@
import android.app.INotificationManager;
import android.app.NotificationChannel;
import android.app.NotificationChannelGroup;
+import android.app.NotificationManager;
import android.app.usage.IUsageStatsManager;
import android.app.usage.UsageEvents;
import android.content.Context;
@@ -38,8 +39,6 @@
import android.util.IconDrawableFactory;
import android.util.Log;
-import androidx.annotation.VisibleForTesting;
-
import com.android.settingslib.R;
import com.android.settingslib.Utils;
import com.android.settingslib.utils.StringUtil;
@@ -49,6 +48,8 @@
import java.util.List;
import java.util.Map;
+import androidx.annotation.VisibleForTesting;
+
public class NotificationBackend {
private static final String TAG = "NotificationBackend";
@@ -208,6 +209,19 @@
}
}
+ /**
+ * Returns all notification channels associated with the package and uid that will bypass DND
+ */
+ public ParceledListSlice<NotificationChannel> getNotificationChannelsBypassingDnd(String pkg,
+ int uid) {
+ try {
+ return sINM.getNotificationChannelsBypassingDnd(pkg, uid);
+ } catch (Exception e) {
+ Log.w(TAG, "Error calling NoMan", e);
+ return ParceledListSlice.emptyList();
+ }
+ }
+
public void updateChannel(String pkg, int uid, NotificationChannel channel) {
try {
sINM.updateNotificationChannelForPackage(pkg, uid, channel);
@@ -260,6 +274,15 @@
}
}
+ public int getNumAppsBypassingDnd(int uid) {
+ try {
+ return sINM.getAppsBypassingDndCount(uid);
+ } catch (Exception e) {
+ Log.w(TAG, "Error calling NoMan", e);
+ return 0;
+ }
+ }
+
public List<NotifyingApp> getRecentApps() {
try {
return sINM.getRecentNotifyingAppsForUser(UserHandle.myUserId()).getList();
diff --git a/src/com/android/settings/notification/ZenModeAllBypassingAppsPreferenceController.java b/src/com/android/settings/notification/ZenModeAllBypassingAppsPreferenceController.java
new file mode 100644
index 0000000..5539ba4
--- /dev/null
+++ b/src/com/android/settings/notification/ZenModeAllBypassingAppsPreferenceController.java
@@ -0,0 +1,200 @@
+/*
+ * Copyright (C) 2018 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.notification;
+
+import android.app.Application;
+import android.app.NotificationChannel;
+import android.content.Context;
+import android.os.Bundle;
+import android.provider.Settings;
+
+import com.android.internal.logging.nano.MetricsProto.MetricsEvent;
+import com.android.settings.R;
+import com.android.settings.applications.AppInfoBase;
+import com.android.settings.core.PreferenceControllerMixin;
+import com.android.settings.core.SubSettingLauncher;
+import com.android.settingslib.applications.ApplicationsState;
+import com.android.settingslib.core.AbstractPreferenceController;
+import com.android.settingslib.widget.apppreference.AppPreference;
+
+import java.util.ArrayList;
+import java.util.List;
+
+import androidx.annotation.VisibleForTesting;
+import androidx.core.text.BidiFormatter;
+import androidx.fragment.app.Fragment;
+import androidx.lifecycle.Lifecycle;
+import androidx.preference.Preference;
+import androidx.preference.PreferenceScreen;
+
+/**
+ * Adds a preference to the PreferenceScreen for each notification channel that can bypass DND.
+ */
+public class ZenModeAllBypassingAppsPreferenceController extends AbstractPreferenceController
+ implements PreferenceControllerMixin {
+
+ private final String KEY = "zen_mode_bypassing_apps_category";
+
+ @VisibleForTesting ApplicationsState mApplicationsState;
+ @VisibleForTesting PreferenceScreen mPreferenceScreen;
+ @VisibleForTesting Context mPrefContext;
+
+ private ApplicationsState.Session mAppSession;
+ private NotificationBackend mNotificationBackend = new NotificationBackend();
+ private Fragment mHostFragment;
+
+ public ZenModeAllBypassingAppsPreferenceController(Context context, Application app,
+ Fragment host) {
+
+ this(context, app == null ? null : ApplicationsState.getInstance(app), host);
+ }
+
+ private ZenModeAllBypassingAppsPreferenceController(Context context, ApplicationsState appState,
+ Fragment host) {
+ super(context);
+ mApplicationsState = appState;
+ mHostFragment = host;
+
+ if (mApplicationsState != null && host != null) {
+ mAppSession = mApplicationsState.newSession(mAppSessionCallbacks, host.getLifecycle());
+ }
+ }
+
+ @Override
+ public void displayPreference(PreferenceScreen screen) {
+ mPreferenceScreen = screen;
+ mPrefContext = mPreferenceScreen.getContext();
+ updateNotificationChannelList();
+ super.displayPreference(screen);
+ }
+
+ @Override
+ public boolean isAvailable() {
+ return true;
+ }
+
+ @Override
+ public String getPreferenceKey() {
+ return KEY;
+ }
+
+ /**
+ * Call this method to trigger the notification channels list to refresh.
+ */
+ public void updateNotificationChannelList() {
+ if (mAppSession == null) {
+ return;
+ }
+
+ ApplicationsState.AppFilter filter = ApplicationsState.FILTER_ALL_ENABLED;
+ List<ApplicationsState.AppEntry> apps = mAppSession.rebuild(filter,
+ ApplicationsState.ALPHA_COMPARATOR);
+ if (apps != null) {
+ updateNotificationChannelList(apps);
+ }
+ }
+
+ @VisibleForTesting
+ void updateNotificationChannelList(List<ApplicationsState.AppEntry> apps) {
+ if (mPreferenceScreen == null || apps == null) {
+ return;
+ }
+
+ List<Preference> channelsBypassingDnd = new ArrayList<>();
+ for (ApplicationsState.AppEntry entry : apps) {
+ String pkg = entry.info.packageName;
+ mApplicationsState.ensureIcon(entry);
+ for (NotificationChannel channel : mNotificationBackend
+ .getNotificationChannelsBypassingDnd(pkg, entry.info.uid).getList()) {
+ Preference pref = new AppPreference(mPrefContext);
+ pref.setKey(pkg + "|" + channel.getId());
+ pref.setTitle(BidiFormatter.getInstance().unicodeWrap(entry.label));
+ pref.setIcon(entry.icon);
+ pref.setSummary(BidiFormatter.getInstance().unicodeWrap(channel.getName()));
+
+ pref.setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() {
+ @Override
+ public boolean onPreferenceClick(Preference preference) {
+ Bundle args = new Bundle();
+ args.putString(AppInfoBase.ARG_PACKAGE_NAME, entry.info.packageName);
+ args.putInt(AppInfoBase.ARG_PACKAGE_UID, entry.info.uid);
+ args.putString(Settings.EXTRA_CHANNEL_ID, channel.getId());
+ new SubSettingLauncher(mContext)
+ .setDestination(ChannelNotificationSettings.class.getName())
+ .setArguments(args)
+ .setTitleRes(R.string.notification_channel_title)
+ .setResultListener(mHostFragment, 0)
+ .setSourceMetricsCategory(
+ MetricsEvent.NOTIFICATION_ZEN_MODE_OVERRIDING_APP)
+ .launch();
+ return true;
+ }
+ });
+ channelsBypassingDnd.add(pref);
+ }
+
+ mPreferenceScreen.removeAll();
+ if (channelsBypassingDnd.size() > 0) {
+ for (Preference prefToAdd : channelsBypassingDnd) {
+ mPreferenceScreen.addPreference(prefToAdd);
+ }
+ }
+ }
+ }
+
+ private final ApplicationsState.Callbacks mAppSessionCallbacks =
+ new ApplicationsState.Callbacks() {
+
+ @Override
+ public void onRunningStateChanged(boolean running) {
+ updateNotificationChannelList();
+ }
+
+ @Override
+ public void onPackageListChanged() {
+ updateNotificationChannelList();
+ }
+
+ @Override
+ public void onRebuildComplete(ArrayList<ApplicationsState.AppEntry> apps) {
+ updateNotificationChannelList(apps);
+ }
+
+ @Override
+ public void onPackageIconChanged() {
+ updateNotificationChannelList();
+ }
+
+ @Override
+ public void onPackageSizeChanged(String packageName) {
+ updateNotificationChannelList();
+ }
+
+ @Override
+ public void onAllSizesComputed() { }
+
+ @Override
+ public void onLauncherInfoChanged() {
+ updateNotificationChannelList();
+ }
+
+ @Override
+ public void onLoadEntriesCompleted() {
+ updateNotificationChannelList();
+ }
+ };
+}
diff --git a/src/com/android/settings/notification/ZenModeBypassingAppsPreferenceController.java b/src/com/android/settings/notification/ZenModeBypassingAppsPreferenceController.java
new file mode 100644
index 0000000..33e03d9
--- /dev/null
+++ b/src/com/android/settings/notification/ZenModeBypassingAppsPreferenceController.java
@@ -0,0 +1,30 @@
+package com.android.settings.notification;
+
+import android.content.Context;
+import android.os.UserHandle;
+
+import com.android.settings.R;
+import com.android.settingslib.core.lifecycle.Lifecycle;
+
+public class ZenModeBypassingAppsPreferenceController extends AbstractZenModePreferenceController {
+
+ protected static final String KEY = "zen_mode_bypassing_apps";
+ private NotificationBackend mNotificationBackend = new NotificationBackend();
+
+ public ZenModeBypassingAppsPreferenceController(Context context, Lifecycle lifecycle) {
+ super(context, KEY, lifecycle);
+ }
+
+ @Override
+ public boolean isAvailable() {
+ return mNotificationBackend.getNumAppsBypassingDnd(UserHandle.getCallingUserId()) != 0;
+ }
+
+ @Override
+ public String getSummary() {
+ final int channelsBypassing =
+ mNotificationBackend.getNumAppsBypassingDnd(UserHandle.getCallingUserId());
+ return mContext.getResources().getQuantityString(R.plurals.zen_mode_bypassing_apps_subtext,
+ channelsBypassing, channelsBypassing);
+ }
+}
diff --git a/src/com/android/settings/notification/ZenModeBypassingAppsSettings.java b/src/com/android/settings/notification/ZenModeBypassingAppsSettings.java
new file mode 100644
index 0000000..53dc8d1
--- /dev/null
+++ b/src/com/android/settings/notification/ZenModeBypassingAppsSettings.java
@@ -0,0 +1,98 @@
+/*
+ * Copyright (C) 2018 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.notification;
+
+import android.app.Activity;
+import android.app.Application;
+import android.content.Context;
+import android.provider.SearchIndexableResource;
+
+import com.android.internal.logging.nano.MetricsProto.MetricsEvent;
+import com.android.settings.R;
+import com.android.settings.search.BaseSearchIndexProvider;
+import com.android.settings.search.Indexable;
+import com.android.settingslib.core.AbstractPreferenceController;
+import com.android.settingslib.search.SearchIndexable;
+
+import java.util.ArrayList;
+import java.util.List;
+
+import androidx.fragment.app.Fragment;
+
+@SearchIndexable
+public class ZenModeBypassingAppsSettings extends ZenModeSettingsBase implements
+ Indexable {
+ private final String TAG = "ZenBypassingApps";
+
+ @Override
+ protected List<AbstractPreferenceController> createPreferenceControllers(Context context) {
+ final Activity activity = getActivity();
+ final Application app;
+ if (activity != null) {
+ app = activity.getApplication();
+ } else {
+ app = null;
+ }
+ return buildPreferenceControllers(context, app, this);
+ }
+
+ private static List<AbstractPreferenceController> buildPreferenceControllers(Context context,
+ Application app, Fragment host) {
+ final List<AbstractPreferenceController> controllers = new ArrayList<>();
+ controllers.add(new ZenModeAllBypassingAppsPreferenceController(context, app, host));
+ return controllers;
+ }
+
+ @Override
+ protected int getPreferenceScreenResId() {
+ return R.xml.zen_mode_bypassing_apps;
+ }
+
+ @Override
+ protected String getLogTag() {
+ return TAG;
+ }
+
+ @Override
+ public int getMetricsCategory() {
+ return MetricsEvent.NOTIFICATION_ZEN_MODE_OVERRIDING_APPS;
+ }
+
+ /**
+ * For Search.
+ */
+ public static final SearchIndexProvider SEARCH_INDEX_DATA_PROVIDER =
+ new BaseSearchIndexProvider() {
+
+ @Override
+ public List<SearchIndexableResource> getXmlResourcesToIndex(Context context,
+ boolean enabled) {
+ final ArrayList<SearchIndexableResource> result = new ArrayList<>();
+
+ final SearchIndexableResource sir = new SearchIndexableResource(context);
+ sir.xmlResId = R.xml.zen_mode_bypassing_apps;
+ result.add(sir);
+ return result;
+ }
+
+ @Override
+ public List<AbstractPreferenceController> createPreferenceControllers(
+ Context context) {
+ return buildPreferenceControllers(context, null, null);
+ }
+ };
+}
diff --git a/src/com/android/settings/notification/ZenModeSoundVibrationSettings.java b/src/com/android/settings/notification/ZenModeSoundVibrationSettings.java
index df92446..5bc5848 100644
--- a/src/com/android/settings/notification/ZenModeSoundVibrationSettings.java
+++ b/src/com/android/settings/notification/ZenModeSoundVibrationSettings.java
@@ -16,8 +16,6 @@
package com.android.settings.notification;
-import static android.app.NotificationManager.Policy.PRIORITY_CATEGORY_MESSAGES;
-
import android.content.Context;
import android.provider.SearchIndexableResource;
@@ -55,6 +53,7 @@
controllers.add(new ZenModeEventsPreferenceController(context, lifecycle));
controllers.add(new ZenModeBehaviorFooterPreferenceController(context, lifecycle,
R.string.zen_sound_footer));
+ controllers.add(new ZenModeBypassingAppsPreferenceController(context, lifecycle));
return controllers;
}
diff --git a/src/com/android/settings/notification/ZenModeStarredContactsPreferenceController.java b/src/com/android/settings/notification/ZenModeStarredContactsPreferenceController.java
index adb65b6..66c2ddd 100644
--- a/src/com/android/settings/notification/ZenModeStarredContactsPreferenceController.java
+++ b/src/com/android/settings/notification/ZenModeStarredContactsPreferenceController.java
@@ -28,16 +28,16 @@
import android.provider.Contacts;
import android.provider.ContactsContract;
-import androidx.annotation.VisibleForTesting;
-import androidx.preference.Preference;
-import androidx.preference.PreferenceScreen;
-
import com.android.settings.R;
import com.android.settingslib.core.lifecycle.Lifecycle;
import java.util.ArrayList;
import java.util.List;
+import androidx.annotation.VisibleForTesting;
+import androidx.preference.Preference;
+import androidx.preference.PreferenceScreen;
+
public class ZenModeStarredContactsPreferenceController extends
AbstractZenModePreferenceController implements Preference.OnPreferenceClickListener {
@@ -134,7 +134,6 @@
@VisibleForTesting
List<String> getStarredContacts(Cursor cursor) {
List<String> starredContacts = new ArrayList<>();
-
if (cursor.moveToFirst()) {
do {
String contact = cursor.getString(0);
@@ -147,7 +146,15 @@
}
private List<String> getStarredContacts() {
- return getStarredContacts(queryData());
+ Cursor cursor = null;
+ try {
+ cursor = queryData();
+ return getStarredContacts(cursor);
+ } finally {
+ if (cursor != null) {
+ cursor.close();
+ }
+ }
}
private Cursor queryData() {
diff --git a/tests/robotests/src/com/android/settings/notification/ZenModeAllBypassingAppsPreferenceControllerTest.java b/tests/robotests/src/com/android/settings/notification/ZenModeAllBypassingAppsPreferenceControllerTest.java
new file mode 100644
index 0000000..45f4210
--- /dev/null
+++ b/tests/robotests/src/com/android/settings/notification/ZenModeAllBypassingAppsPreferenceControllerTest.java
@@ -0,0 +1,116 @@
+/*
+ * Copyright (C) 2018 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.notification;
+
+import static com.google.common.truth.Truth.assertThat;
+
+import static org.mockito.ArgumentMatchers.any;
+import static org.mockito.ArgumentMatchers.anyInt;
+import static org.mockito.ArgumentMatchers.anyString;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.never;
+import static org.mockito.Mockito.times;
+import static org.mockito.Mockito.verify;
+import static org.mockito.Mockito.when;
+
+import android.app.NotificationChannel;
+import android.app.NotificationManager;
+import android.content.Context;
+import android.content.pm.ApplicationInfo;
+import android.content.pm.ParceledListSlice;
+
+import com.android.settings.testutils.SettingsRobolectricTestRunner;
+import com.android.settingslib.applications.ApplicationsState;
+
+import org.junit.Before;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.mockito.Mock;
+import org.mockito.MockitoAnnotations;
+import org.robolectric.RuntimeEnvironment;
+import org.robolectric.util.ReflectionHelpers;
+
+import java.util.ArrayList;
+import java.util.List;
+
+import androidx.fragment.app.Fragment;
+import androidx.preference.PreferenceScreen;
+
+@RunWith(SettingsRobolectricTestRunner.class)
+public class ZenModeAllBypassingAppsPreferenceControllerTest {
+ private ZenModeAllBypassingAppsPreferenceController mController;
+
+ private Context mContext;
+ @Mock
+ private NotificationBackend mBackend;
+ @Mock
+ private PreferenceScreen mPreferenceScreen;
+ @Mock
+ private ApplicationsState mApplicationState;
+
+ @Before
+ public void setup() {
+ MockitoAnnotations.initMocks(this);
+ mContext = RuntimeEnvironment.application;
+
+ mController = new ZenModeAllBypassingAppsPreferenceController(
+ mContext, null, mock(Fragment.class));
+ mController.mPreferenceScreen = mPreferenceScreen;
+ mController.mApplicationsState = mApplicationState;
+ mController.mPrefContext = mContext;
+ ReflectionHelpers.setField(mController, "mNotificationBackend", mBackend);
+ }
+
+ @Test
+ public void testIsAvailable() {
+ assertThat(mController.isAvailable()).isTrue();
+ }
+
+ @Test
+ public void testUpdateNotificationChannelList() {
+ ApplicationsState.AppEntry entry = mock(ApplicationsState.AppEntry.class);
+ entry.info = new ApplicationInfo();
+ entry.info.packageName = "test";
+ entry.info.uid = 0;
+
+ List<ApplicationsState.AppEntry> appEntries = new ArrayList<>();
+ appEntries.add(entry);
+
+ List<NotificationChannel> channelsBypassing = new ArrayList<>();
+ channelsBypassing.add(mock(NotificationChannel.class));
+ channelsBypassing.add(mock(NotificationChannel.class));
+ channelsBypassing.add(mock(NotificationChannel.class));
+
+ when(mBackend.getNotificationChannelsBypassingDnd(entry.info.packageName,
+ entry.info.uid)).thenReturn(new ParceledListSlice<>(channelsBypassing));
+
+ mController.updateNotificationChannelList(appEntries);
+ verify(mPreferenceScreen, times(3)).addPreference(any());
+ }
+
+ @Test
+ public void testUpdateNotificationChannelList_nullChannels() {
+ mController.updateNotificationChannelList(null);
+ verify(mPreferenceScreen, never()).addPreference(any());
+ }
+
+ @Test
+ public void testUpdateNotificationChannelList_emptyChannelsList() {
+ mController.updateNotificationChannelList(new ArrayList<ApplicationsState.AppEntry>());
+ verify(mPreferenceScreen, never()).addPreference(any());
+ }
+}
diff --git a/tests/robotests/src/com/android/settings/notification/ZenModeBypassingAppsPreferenceControllerTest.java b/tests/robotests/src/com/android/settings/notification/ZenModeBypassingAppsPreferenceControllerTest.java
new file mode 100644
index 0000000..0bdf6f5
--- /dev/null
+++ b/tests/robotests/src/com/android/settings/notification/ZenModeBypassingAppsPreferenceControllerTest.java
@@ -0,0 +1,72 @@
+/*
+ * Copyright (C) 2018 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.notification;
+
+import static com.google.common.truth.Truth.assertThat;
+
+import static org.mockito.ArgumentMatchers.anyInt;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.when;
+
+import android.content.Context;
+
+import com.android.settings.testutils.SettingsRobolectricTestRunner;
+import com.android.settingslib.core.lifecycle.Lifecycle;
+
+import org.junit.Before;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.mockito.Mock;
+import org.mockito.MockitoAnnotations;
+import org.robolectric.RuntimeEnvironment;
+import org.robolectric.util.ReflectionHelpers;
+
+@RunWith(SettingsRobolectricTestRunner.class)
+public class ZenModeBypassingAppsPreferenceControllerTest {
+
+ private ZenModeBypassingAppsPreferenceController mController;
+
+ private Context mContext;
+ @Mock
+ private NotificationBackend mBackend;
+
+ @Before
+ public void setup() {
+ MockitoAnnotations.initMocks(this);
+ mContext = RuntimeEnvironment.application;
+
+ mController = new ZenModeBypassingAppsPreferenceController(mContext, mock(Lifecycle.class));
+ ReflectionHelpers.setField(mController, "mNotificationBackend", mBackend);
+ }
+
+ @Test
+ public void testIsAvailable() {
+ when(mBackend.getNumAppsBypassingDnd(anyInt())).thenReturn(5);
+ assertThat(mController.isAvailable()).isTrue();
+ }
+
+ @Test
+ public void testNotAvailable() {
+ when(mBackend.getNumAppsBypassingDnd(anyInt())).thenReturn(0);
+ assertThat(mController.isAvailable()).isFalse();
+ }
+
+ @Test
+ public void testHasSummary() {
+ assertThat(mController.getSummary()).isNotNull();
+ }
+}