Allow firing the LOST_INTERNET intent immediately in config

When config_notifyNoInternetAsDialogWhenHighPriority is on, the
LOST_INTERNET notification intent should be fired immediately
rather than as a pending intent.

Bug: 281970908
Test: Improve test for this in NetworkNotificationManagerTest
Change-Id: I88565839a12a1ab4b096f763250944ebaf6c5349
This commit is contained in:
Chalard Jean
2023-06-05 18:47:42 +09:00
parent 0828c6e0f2
commit e13b833b21
3 changed files with 67 additions and 22 deletions

View File

@@ -135,10 +135,17 @@
<!-- Whether to cancel network notifications automatically when tapped --> <!-- Whether to cancel network notifications automatically when tapped -->
<bool name="config_autoCancelNetworkNotifications">true</bool> <bool name="config_autoCancelNetworkNotifications">true</bool>
<!-- When no internet or partial connectivity is detected on a network, and a high priority <!-- Configuration to let OEMs customize what to do when :
(heads up) notification would be shown due to the network being explicitly selected, • Partial connectivity is detected on the network
directly show the dialog that would normally be shown when tapping the notification • No internet is detected on the network, and
instead of showing the notification. --> - the network was explicitly selected
- the system is configured to actively prefer bad wifi (see config_activelyPreferBadWifi)
The default behavior (false) is to post a notification with a PendingIntent so
the user is informed and can act if they wish.
Making this true instead will have the system fire the intent immediately instead
of showing a notification. OEMs who do this should have some intent receiver
listening to the intent and take the action they prefer (e.g. show a dialog,
show a customized notification etc). -->
<bool name="config_notifyNoInternetAsDialogWhenHighPriority">false</bool> <bool name="config_notifyNoInternetAsDialogWhenHighPriority">false</bool>
<!-- When showing notifications indicating partial connectivity, display the same notifications <!-- When showing notifications indicating partial connectivity, display the same notifications

View File

@@ -322,7 +322,8 @@ public class NetworkNotificationManager {
private boolean maybeNotifyViaDialog(Resources res, NotificationType notifyType, private boolean maybeNotifyViaDialog(Resources res, NotificationType notifyType,
PendingIntent intent) { PendingIntent intent) {
if (notifyType != NotificationType.NO_INTERNET if (notifyType != NotificationType.LOST_INTERNET
&& notifyType != NotificationType.NO_INTERNET
&& notifyType != NotificationType.PARTIAL_CONNECTIVITY) { && notifyType != NotificationType.PARTIAL_CONNECTIVITY) {
return false; return false;
} }
@@ -432,7 +433,8 @@ public class NetworkNotificationManager {
* A notification with a higher number will take priority over a notification with a lower * A notification with a higher number will take priority over a notification with a lower
* number. * number.
*/ */
private static int priority(NotificationType t) { @VisibleForTesting
public static int priority(NotificationType t) {
if (t == null) { if (t == null) {
return 0; return 0;
} }

View File

@@ -62,6 +62,7 @@ import android.testing.PollingCheck;
import android.util.DisplayMetrics; import android.util.DisplayMetrics;
import android.widget.TextView; import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable; import androidx.annotation.Nullable;
import androidx.annotation.StringRes; import androidx.annotation.StringRes;
import androidx.test.filters.SmallTest; import androidx.test.filters.SmallTest;
@@ -386,14 +387,37 @@ public class NetworkNotificationManagerTest {
} }
@Test @Test
public void testNotifyNoInternetAsDialogWhenHighPriority() throws Exception { public void testNotifyNoInternet_asNotification() throws Exception {
doReturn(true).when(mResources).getBoolean( doTestNotifyNotificationAsDialogWhenHighPriority(false, NO_INTERNET);
}
@Test
public void testNotifyNoInternet_asDialog() throws Exception {
doTestNotifyNotificationAsDialogWhenHighPriority(true, NO_INTERNET);
}
@Test
public void testNotifyLostInternet_asNotification() throws Exception {
doTestNotifyNotificationAsDialogWhenHighPriority(false, LOST_INTERNET);
}
@Test
public void testNotifyLostInternet_asDialog() throws Exception {
doTestNotifyNotificationAsDialogWhenHighPriority(true, LOST_INTERNET);
}
public void doTestNotifyNotificationAsDialogWhenHighPriority(final boolean configActive,
@NonNull final NotificationType notifType) throws Exception {
doReturn(configActive).when(mResources).getBoolean(
R.bool.config_notifyNoInternetAsDialogWhenHighPriority); R.bool.config_notifyNoInternetAsDialogWhenHighPriority);
final Instrumentation instr = InstrumentationRegistry.getInstrumentation(); final Instrumentation instr = InstrumentationRegistry.getInstrumentation();
final UiDevice uiDevice = UiDevice.getInstance(instr); final UiDevice uiDevice = UiDevice.getInstance(instr);
final Context ctx = instr.getContext(); final Context ctx = instr.getContext();
final PowerManager pm = ctx.getSystemService(PowerManager.class); final PowerManager pm = ctx.getSystemService(PowerManager.class);
// If the prio of this notif is < that of NETWORK_SWITCH, it's the lowest prio and
// therefore it can't be tested whether it cancels other lower-prio notifs.
final boolean isLowestPrioNotif = NetworkNotificationManager.priority(notifType)
< NetworkNotificationManager.priority(NETWORK_SWITCH);
// Wake up the device (it has no effect if the device is already awake). // Wake up the device (it has no effect if the device is already awake).
uiDevice.executeShellCommand("input keyevent KEYCODE_WAKEUP"); uiDevice.executeShellCommand("input keyevent KEYCODE_WAKEUP");
@@ -409,9 +433,13 @@ public class NetworkNotificationManagerTest {
uiDevice.wait(Until.hasObject(By.pkg(launcherPackageName)), uiDevice.wait(Until.hasObject(By.pkg(launcherPackageName)),
UI_AUTOMATOR_WAIT_TIME_MILLIS)); UI_AUTOMATOR_WAIT_TIME_MILLIS));
mManager.showNotification(TEST_NOTIF_ID, NETWORK_SWITCH, mWifiNai, mCellNai, null, false); if (!isLowestPrioNotif) {
// Non-"no internet" notifications are not affected mManager.showNotification(TEST_NOTIF_ID, NETWORK_SWITCH, mWifiNai, mCellNai,
verify(mNotificationManager).notify(eq(TEST_NOTIF_TAG), eq(NETWORK_SWITCH.eventId), any()); null, false);
// Non-"no internet" notifications are not affected
verify(mNotificationManager).notify(eq(TEST_NOTIF_TAG), eq(NETWORK_SWITCH.eventId),
any());
}
final String testAction = "com.android.connectivity.coverage.TEST_DIALOG"; final String testAction = "com.android.connectivity.coverage.TEST_DIALOG";
final Intent intent = new Intent(testAction) final Intent intent = new Intent(testAction)
@@ -420,22 +448,30 @@ public class NetworkNotificationManagerTest {
final PendingIntent pendingIntent = PendingIntent.getActivity(ctx, 0 /* requestCode */, final PendingIntent pendingIntent = PendingIntent.getActivity(ctx, 0 /* requestCode */,
intent, PendingIntent.FLAG_CANCEL_CURRENT | PendingIntent.FLAG_IMMUTABLE); intent, PendingIntent.FLAG_CANCEL_CURRENT | PendingIntent.FLAG_IMMUTABLE);
mManager.showNotification(TEST_NOTIF_ID, NO_INTERNET, mWifiNai, null /* switchToNai */, mManager.showNotification(TEST_NOTIF_ID, notifType, mWifiNai, null /* switchToNai */,
pendingIntent, true /* highPriority */); pendingIntent, true /* highPriority */);
// Previous notifications are still dismissed if (!isLowestPrioNotif) {
verify(mNotificationManager).cancel(TEST_NOTIF_TAG, NETWORK_SWITCH.eventId); // Previous notifications are still dismissed
verify(mNotificationManager).cancel(TEST_NOTIF_TAG, NETWORK_SWITCH.eventId);
}
// Verify that the activity is shown (the activity shows the action on screen) if (configActive) {
final UiObject actionText = uiDevice.findObject(new UiSelector().text(testAction)); // Verify that the activity is shown (the activity shows the action on screen)
assertTrue("Activity not shown", actionText.waitForExists(TEST_TIMEOUT_MS)); final UiObject actionText = uiDevice.findObject(new UiSelector().text(testAction));
assertTrue("Activity not shown", actionText.waitForExists(TEST_TIMEOUT_MS));
// Tapping the text should dismiss the dialog // Tapping the text should dismiss the dialog
actionText.click(); actionText.click();
assertTrue("Activity not dismissed", actionText.waitUntilGone(TEST_TIMEOUT_MS)); assertTrue("Activity not dismissed", actionText.waitUntilGone(TEST_TIMEOUT_MS));
// Verify no NO_INTERNET notification was posted // Verify that the notification was not posted
verify(mNotificationManager, never()).notify(any(), eq(NO_INTERNET.eventId), any()); verify(mNotificationManager, never()).notify(any(), eq(notifType.eventId), any());
} else {
// Notification should have been posted, and will have overridden the previous
// one because it has the same id (hence no cancel).
verify(mNotificationManager).notify(eq(TEST_NOTIF_TAG), eq(notifType.eventId), any());
}
} }
private void doNotificationTextTest(NotificationType type, @StringRes int expectedTitleRes, private void doNotificationTextTest(NotificationType type, @StringRes int expectedTitleRes,