From e899b4c96c1bbf54602be7b288837707456efab6 Mon Sep 17 00:00:00 2001 From: Yining Liu Date: Tue, 25 Oct 2022 20:22:12 +0000 Subject: [PATCH] Fix text in notification settings for apps that do not send notifications Added a new string to show for apps apps that target T, but do not declare the POST_NOTIFICATIONS permission in notification settings. Bug: 229108007 Test: `croot` `make -j64 RunSettingsRoboTests ROBOTEST_FILTER="com.android.settings.notification.app.NotificationsOffPreferenceControllerTest"` and manual: open Settings-Notifications-App Settings, check the text for apps that does not post notifications with toggle disabled. Text should be: "This app does not send notifications" when system language is English. Change-Id: I9822b6f983e00c5982522099e339d86fae45aefe --- res/values/strings.xml | 3 +++ .../notification/NotificationBackend.java | 4 ++++ .../NotificationsOffPreferenceController.java | 2 ++ ...ificationsOffPreferenceControllerTest.java | 21 +++++++++++++++++++ 4 files changed, 30 insertions(+) diff --git a/res/values/strings.xml b/res/values/strings.xml index 42f8e4c0811..dfb99647f81 100644 --- a/res/values/strings.xml +++ b/res/values/strings.xml @@ -9895,6 +9895,9 @@ At your request, Android is blocking this group of notifications from appearing on this device + + This app does not send notifications + Categories diff --git a/src/com/android/settings/notification/NotificationBackend.java b/src/com/android/settings/notification/NotificationBackend.java index 150dbe0483d..68f5d081d11 100644 --- a/src/com/android/settings/notification/NotificationBackend.java +++ b/src/com/android/settings/notification/NotificationBackend.java @@ -126,6 +126,7 @@ public class NotificationBackend { if (app.requestedPermissions == null || Arrays.stream(app.requestedPermissions) .noneMatch(p -> p.equals(android.Manifest.permission.POST_NOTIFICATIONS))) { row.lockedImportance = true; + row.permissionStateLocked = true; } } } @@ -684,6 +685,9 @@ public class NotificationBackend { public boolean systemApp; public boolean lockedImportance; public boolean showBadge; + // For apps target T but have not but has not requested the permission + // we cannot change the permission state + public boolean permissionStateLocked; public int bubblePreference = NotificationManager.BUBBLE_PREFERENCE_NONE; public int userId; public int blockedChannelCount; diff --git a/src/com/android/settings/notification/app/NotificationsOffPreferenceController.java b/src/com/android/settings/notification/app/NotificationsOffPreferenceController.java index 0c7cd2361e2..04e3f0e311b 100644 --- a/src/com/android/settings/notification/app/NotificationsOffPreferenceController.java +++ b/src/com/android/settings/notification/app/NotificationsOffPreferenceController.java @@ -62,6 +62,8 @@ public class NotificationsOffPreferenceController extends NotificationPreference preference.setTitle(R.string.channel_notifications_off_desc); } else if (mChannelGroup != null) { preference.setTitle(R.string.channel_group_notifications_off_desc); + } else if (mAppRow.permissionStateLocked) { + preference.setTitle(R.string.app_notifications_not_send_desc); } else { preference.setTitle(R.string.app_notifications_off_desc); } diff --git a/tests/robotests/src/com/android/settings/notification/app/NotificationsOffPreferenceControllerTest.java b/tests/robotests/src/com/android/settings/notification/app/NotificationsOffPreferenceControllerTest.java index 34e94a042f9..2eebd3a2eab 100644 --- a/tests/robotests/src/com/android/settings/notification/app/NotificationsOffPreferenceControllerTest.java +++ b/tests/robotests/src/com/android/settings/notification/app/NotificationsOffPreferenceControllerTest.java @@ -20,6 +20,7 @@ import static android.app.NotificationManager.IMPORTANCE_NONE; import static com.google.common.truth.Truth.assertThat; +import static org.junit.Assert.assertEquals; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.spy; import static org.mockito.Mockito.when; @@ -32,6 +33,7 @@ import android.os.UserManager; import androidx.preference.Preference; +import com.android.settings.R; import com.android.settings.notification.NotificationBackend; import com.google.common.collect.ImmutableList; @@ -157,4 +159,23 @@ public class NotificationsOffPreferenceControllerTest { assertThat(pref.getTitle().toString()).contains("app"); assertThat(pref.isSelectable()).isFalse(); } + + @Test + public void testUpdateState_whenToggleDisabled() { + // Given: the app does not request to post notifications + // and it's preference toggle is disabled + NotificationBackend.AppRow appRow = new NotificationBackend.AppRow(); + appRow.banned = true; + appRow.permissionStateLocked = true; + mController.onResume(appRow, null, null, null, null, null, null); + Preference pref = new Preference(RuntimeEnvironment.application); + + // When: updateState(Preference preference) is called + mController.updateState(pref); + + // Then: title of pref should be app_notifications_not_send_desc + assertEquals( + RuntimeEnvironment.application.getString(R.string.app_notifications_not_send_desc), + pref.getTitle().toString()); + } }