Merge "Make notification icons and autocancel overlayable" am: 2c7e0714c6

Original change: https://android-review.googlesource.com/c/platform/packages/modules/Connectivity/+/1753822

Change-Id: I77e1bcc4190656d3ea717575ebfc58d07df6b24f
This commit is contained in:
Treehugger Robot
2021-07-01 10:27:29 +00:00
committed by Automerger Merge Worker
4 changed files with 43 additions and 26 deletions

View File

@@ -111,4 +111,7 @@
notification that can be dismissed. --> notification that can be dismissed. -->
<bool name="config_ongoingSignInNotification">false</bool> <bool name="config_ongoingSignInNotification">false</bool>
<!-- Whether to cancel network notifications automatically when tapped -->
<bool name="config_autoCancelNetworkNotifications">true</bool>
</resources> </resources>

View File

@@ -31,7 +31,9 @@
<item type="integer" name="config_networkNotifySwitchType"/> <item type="integer" name="config_networkNotifySwitchType"/>
<item type="array" name="config_networkNotifySwitches"/> <item type="array" name="config_networkNotifySwitches"/>
<item type="bool" name="config_ongoingSignInNotification"/> <item type="bool" name="config_ongoingSignInNotification"/>
<item type="bool" name="config_autoCancelNetworkNotifications"/>
<item type="drawable" name="stat_notify_wifi_in_range"/>
<item type="drawable" name="stat_notify_rssi_in_range"/>
</policy> </policy>
</overlayable> </overlayable>
</resources> </resources>

View File

@@ -274,7 +274,7 @@ public class NetworkNotificationManager {
.setWhen(System.currentTimeMillis()) .setWhen(System.currentTimeMillis())
.setShowWhen(notifyType == NotificationType.NETWORK_SWITCH) .setShowWhen(notifyType == NotificationType.NETWORK_SWITCH)
.setSmallIcon(icon) .setSmallIcon(icon)
.setAutoCancel(true) .setAutoCancel(r.getBoolean(R.bool.config_autoCancelNetworkNotifications))
.setTicker(title) .setTicker(title)
.setColor(mContext.getColor(android.R.color.system_notification_accent_color)) .setColor(mContext.getColor(android.R.color.system_notification_accent_color))
.setContentTitle(title) .setContentTitle(title)

View File

@@ -16,6 +16,7 @@
package com.android.server.connectivity; package com.android.server.connectivity;
import static android.app.Notification.FLAG_AUTO_CANCEL;
import static android.app.Notification.FLAG_ONGOING_EVENT; import static android.app.Notification.FLAG_ONGOING_EVENT;
import static com.android.server.connectivity.NetworkNotificationManager.NotificationType.LOST_INTERNET; import static com.android.server.connectivity.NetworkNotificationManager.NotificationType.LOST_INTERNET;
@@ -80,6 +81,8 @@ public class NetworkNotificationManagerTest {
private static final String TEST_SSID = "Test SSID"; private static final String TEST_SSID = "Test SSID";
private static final String TEST_EXTRA_INFO = "extra"; private static final String TEST_EXTRA_INFO = "extra";
private static final int TEST_NOTIF_ID = 101;
private static final String TEST_NOTIF_TAG = NetworkNotificationManager.tagFor(TEST_NOTIF_ID);
static final NetworkCapabilities CELL_CAPABILITIES = new NetworkCapabilities(); static final NetworkCapabilities CELL_CAPABILITIES = new NetworkCapabilities();
static final NetworkCapabilities WIFI_CAPABILITIES = new NetworkCapabilities(); static final NetworkCapabilities WIFI_CAPABILITIES = new NetworkCapabilities();
static final NetworkCapabilities VPN_CAPABILITIES = new NetworkCapabilities(); static final NetworkCapabilities VPN_CAPABILITIES = new NetworkCapabilities();
@@ -141,6 +144,7 @@ public class NetworkNotificationManagerTest {
} }
when(mResources.getStringArray(R.array.network_switch_type_name)) when(mResources.getStringArray(R.array.network_switch_type_name))
.thenReturn(transportNames); .thenReturn(transportNames);
when(mResources.getBoolean(R.bool.config_autoCancelNetworkNotifications)).thenReturn(true);
mManager = new NetworkNotificationManager(mCtx, mTelephonyManager); mManager = new NetworkNotificationManager(mCtx, mTelephonyManager);
} }
@@ -237,57 +241,65 @@ public class NetworkNotificationManagerTest {
verify(mNotificationManager, never()).notify(any(), anyInt(), any()); verify(mNotificationManager, never()).notify(any(), anyInt(), any());
} }
private void assertNotification(NotificationType type, boolean ongoing) { private void assertNotification(NotificationType type, boolean ongoing, boolean autoCancel) {
final int id = 101;
final String tag = NetworkNotificationManager.tagFor(id);
final ArgumentCaptor<Notification> noteCaptor = ArgumentCaptor.forClass(Notification.class); final ArgumentCaptor<Notification> noteCaptor = ArgumentCaptor.forClass(Notification.class);
mManager.showNotification(id, type, mWifiNai, mCellNai, null, false); mManager.showNotification(TEST_NOTIF_ID, type, mWifiNai, mCellNai, null, false);
verify(mNotificationManager, times(1)).notify(eq(tag), eq(type.eventId), verify(mNotificationManager, times(1)).notify(eq(TEST_NOTIF_TAG), eq(type.eventId),
noteCaptor.capture()); noteCaptor.capture());
assertEquals("Notification ongoing flag should be " + (ongoing ? "set" : "unset"), assertEquals("Notification ongoing flag should be " + (ongoing ? "set" : "unset"),
ongoing, (noteCaptor.getValue().flags & FLAG_ONGOING_EVENT) != 0); ongoing, (noteCaptor.getValue().flags & FLAG_ONGOING_EVENT) != 0);
assertEquals("Notification autocancel flag should be " + (autoCancel ? "set" : "unset"),
autoCancel, (noteCaptor.getValue().flags & FLAG_AUTO_CANCEL) != 0);
} }
@Test @Test
public void testDuplicatedNotificationsNoInternetThenSignIn() { public void testDuplicatedNotificationsNoInternetThenSignIn() {
final int id = 101;
final String tag = NetworkNotificationManager.tagFor(id);
// Show first NO_INTERNET // Show first NO_INTERNET
assertNotification(NO_INTERNET, false /* ongoing */); assertNotification(NO_INTERNET, false /* ongoing */, true /* autoCancel */);
// Captive portal detection triggers SIGN_IN a bit later, clearing the previous NO_INTERNET // Captive portal detection triggers SIGN_IN a bit later, clearing the previous NO_INTERNET
assertNotification(SIGN_IN, false /* ongoing */); assertNotification(SIGN_IN, false /* ongoing */, true /* autoCancel */);
verify(mNotificationManager, times(1)).cancel(eq(tag), eq(NO_INTERNET.eventId)); verify(mNotificationManager, times(1)).cancel(eq(TEST_NOTIF_TAG), eq(NO_INTERNET.eventId));
// Network disconnects // Network disconnects
mManager.clearNotification(id); mManager.clearNotification(TEST_NOTIF_ID);
verify(mNotificationManager, times(1)).cancel(eq(tag), eq(SIGN_IN.eventId)); verify(mNotificationManager, times(1)).cancel(eq(TEST_NOTIF_TAG), eq(SIGN_IN.eventId));
} }
@Test @Test
public void testOngoingSignInNotification() { public void testOngoingSignInNotification() {
doReturn(true).when(mResources).getBoolean(R.bool.config_ongoingSignInNotification); doReturn(true).when(mResources).getBoolean(R.bool.config_ongoingSignInNotification);
final int id = 101;
final String tag = NetworkNotificationManager.tagFor(id);
// Show first NO_INTERNET // Show first NO_INTERNET
assertNotification(NO_INTERNET, false /* ongoing */); assertNotification(NO_INTERNET, false /* ongoing */, true /* autoCancel */);
// Captive portal detection triggers SIGN_IN a bit later, clearing the previous NO_INTERNET // Captive portal detection triggers SIGN_IN a bit later, clearing the previous NO_INTERNET
assertNotification(SIGN_IN, true /* ongoing */); assertNotification(SIGN_IN, true /* ongoing */, true /* autoCancel */);
verify(mNotificationManager, times(1)).cancel(eq(tag), eq(NO_INTERNET.eventId)); verify(mNotificationManager, times(1)).cancel(eq(TEST_NOTIF_TAG), eq(NO_INTERNET.eventId));
// Network disconnects // Network disconnects
mManager.clearNotification(id); mManager.clearNotification(TEST_NOTIF_ID);
verify(mNotificationManager, times(1)).cancel(eq(tag), eq(SIGN_IN.eventId)); verify(mNotificationManager, times(1)).cancel(eq(TEST_NOTIF_TAG), eq(SIGN_IN.eventId));
}
@Test
public void testNoAutoCancelNotification() {
doReturn(false).when(mResources).getBoolean(R.bool.config_autoCancelNetworkNotifications);
// Show NO_INTERNET, then SIGN_IN
assertNotification(NO_INTERNET, false /* ongoing */, false /* autoCancel */);
assertNotification(SIGN_IN, false /* ongoing */, false /* autoCancel */);
verify(mNotificationManager, times(1)).cancel(eq(TEST_NOTIF_TAG), eq(NO_INTERNET.eventId));
mManager.clearNotification(TEST_NOTIF_ID);
verify(mNotificationManager, times(1)).cancel(eq(TEST_NOTIF_TAG), eq(SIGN_IN.eventId));
} }
@Test @Test
public void testDuplicatedNotificationsSignInThenNoInternet() { public void testDuplicatedNotificationsSignInThenNoInternet() {
final int id = 101; final int id = TEST_NOTIF_ID;
final String tag = NetworkNotificationManager.tagFor(id); final String tag = TEST_NOTIF_TAG;
// Show first SIGN_IN // Show first SIGN_IN
mManager.showNotification(id, SIGN_IN, mWifiNai, mCellNai, null, false); mManager.showNotification(id, SIGN_IN, mWifiNai, mCellNai, null, false);
@@ -306,8 +318,8 @@ public class NetworkNotificationManagerTest {
@Test @Test
public void testClearNotificationByType() { public void testClearNotificationByType() {
final int id = 101; final int id = TEST_NOTIF_ID;
final String tag = NetworkNotificationManager.tagFor(id); final String tag = TEST_NOTIF_TAG;
// clearNotification(int id, NotificationType notifyType) will check if given type is equal // clearNotification(int id, NotificationType notifyType) will check if given type is equal
// to previous type or not. If they are equal then clear the notification; if they are not // to previous type or not. If they are equal then clear the notification; if they are not