From b6105c72b9d1298126270cb04b644aca2648df40 Mon Sep 17 00:00:00 2001 From: Sarah Chin Date: Fri, 18 Nov 2022 02:28:13 -0800 Subject: [PATCH] Add support for daily and monthly notification maximums Slice purchase app notifies SlicePurchaseController when a notification is shown to the user. If the daily or monthly limit has been reached, return result throttled. Test: atest TeleServiceTests, CarrierDefaultAppUnitTests Bug: 248533515 Change-Id: I99cb6ea2ae9bfb85b630810b9f4bf42b1692231d --- .../SlicePurchaseBroadcastReceiver.java | 17 ++++++++++++----- .../SlicePurchaseBroadcastReceiverTest.java | 15 ++++++++++++--- 2 files changed, 24 insertions(+), 8 deletions(-) diff --git a/packages/CarrierDefaultApp/src/com/android/carrierdefaultapp/SlicePurchaseBroadcastReceiver.java b/packages/CarrierDefaultApp/src/com/android/carrierdefaultapp/SlicePurchaseBroadcastReceiver.java index b322b8bd95bb2..367ae06adfc72 100644 --- a/packages/CarrierDefaultApp/src/com/android/carrierdefaultapp/SlicePurchaseBroadcastReceiver.java +++ b/packages/CarrierDefaultApp/src/com/android/carrierdefaultapp/SlicePurchaseBroadcastReceiver.java @@ -175,7 +175,9 @@ public class SlicePurchaseBroadcastReceiver extends BroadcastReceiver{ && isPendingIntentValid(intent, SlicePurchaseController.EXTRA_INTENT_REQUEST_FAILED) && isPendingIntentValid(intent, SlicePurchaseController.EXTRA_INTENT_NOT_DEFAULT_DATA_SUBSCRIPTION) - && isPendingIntentValid(intent, SlicePurchaseController.EXTRA_INTENT_SUCCESS); + && isPendingIntentValid(intent, SlicePurchaseController.EXTRA_INTENT_SUCCESS) + && isPendingIntentValid(intent, + SlicePurchaseController.EXTRA_INTENT_NOTIFICATION_SHOWN); } private static boolean isPendingIntentValid(@NonNull Intent intent, @NonNull String extra) { @@ -208,6 +210,8 @@ public class SlicePurchaseBroadcastReceiver extends BroadcastReceiver{ case SlicePurchaseController.EXTRA_INTENT_NOT_DEFAULT_DATA_SUBSCRIPTION: return "not default data subscription"; case SlicePurchaseController.EXTRA_INTENT_SUCCESS: return "success"; + case SlicePurchaseController.EXTRA_INTENT_NOTIFICATION_SHOWN: + return "notification shown"; default: { loge("Unknown pending intent extra: " + extra); return "unknown(" + extra + ")"; @@ -220,7 +224,7 @@ public class SlicePurchaseBroadcastReceiver extends BroadcastReceiver{ logd("onReceive intent: " + intent.getAction()); switch (intent.getAction()) { case SlicePurchaseController.ACTION_START_SLICE_PURCHASE_APP: - onDisplayBoosterNotification(context, intent); + onDisplayNetworkBoostNotification(context, intent); break; case SlicePurchaseController.ACTION_SLICE_PURCHASE_APP_RESPONSE_TIMEOUT: onTimeout(context, intent); @@ -233,7 +237,8 @@ public class SlicePurchaseBroadcastReceiver extends BroadcastReceiver{ } } - private void onDisplayBoosterNotification(@NonNull Context context, @NonNull Intent intent) { + private void onDisplayNetworkBoostNotification(@NonNull Context context, + @NonNull Intent intent) { if (!isIntentValid(intent)) { sendSlicePurchaseAppResponse(intent, SlicePurchaseController.EXTRA_INTENT_REQUEST_FAILED); @@ -280,10 +285,12 @@ public class SlicePurchaseBroadcastReceiver extends BroadcastReceiver{ int capability = intent.getIntExtra(SlicePurchaseController.EXTRA_PREMIUM_CAPABILITY, SlicePurchaseController.PREMIUM_CAPABILITY_INVALID); - logd("Display the booster notification for capability " + logd("Display the network boost notification for capability " + TelephonyManager.convertPremiumCapabilityToString(capability)); context.getSystemService(NotificationManager.class).notifyAsUser( NETWORK_BOOST_NOTIFICATION_TAG, capability, notification, UserHandle.ALL); + sendSlicePurchaseAppResponse(intent, + SlicePurchaseController.EXTRA_INTENT_NOTIFICATION_SHOWN); } /** @@ -338,7 +345,7 @@ public class SlicePurchaseBroadcastReceiver extends BroadcastReceiver{ + " timed out."); if (sSlicePurchaseActivities.get(capability) == null) { // Notification is still active - logd("Closing booster notification since the user did not respond in time."); + logd("Closing network boost notification since the user did not respond in time."); context.getSystemService(NotificationManager.class).cancelAsUser( NETWORK_BOOST_NOTIFICATION_TAG, capability, UserHandle.ALL); } else { diff --git a/packages/CarrierDefaultApp/tests/unit/src/com/android/carrierdefaultapp/SlicePurchaseBroadcastReceiverTest.java b/packages/CarrierDefaultApp/tests/unit/src/com/android/carrierdefaultapp/SlicePurchaseBroadcastReceiverTest.java index 5765e5b956a92..ab99a76902d9c 100644 --- a/packages/CarrierDefaultApp/tests/unit/src/com/android/carrierdefaultapp/SlicePurchaseBroadcastReceiverTest.java +++ b/packages/CarrierDefaultApp/tests/unit/src/com/android/carrierdefaultapp/SlicePurchaseBroadcastReceiverTest.java @@ -64,6 +64,7 @@ public class SlicePurchaseBroadcastReceiverTest { @Mock PendingIntent mCanceledIntent; @Mock PendingIntent mContentIntent1; @Mock PendingIntent mContentIntent2; + @Mock PendingIntent mNotificationShownIntent; @Mock Context mContext; @Mock Resources mResources; @Mock NotificationManager mNotificationManager; @@ -136,7 +137,7 @@ public class SlicePurchaseBroadcastReceiverTest { } @Test - public void testDisplayBoosterNotification() { + public void testDisplayNetworkBoostNotification() throws Exception { // set up intent doReturn(SlicePurchaseController.ACTION_START_SLICE_PURCHASE_APP).when(mIntent).getAction(); doReturn(PHONE_ID).when(mIntent).getIntExtra( @@ -148,11 +149,17 @@ public class SlicePurchaseBroadcastReceiverTest { doReturn(TAG).when(mIntent).getStringExtra( eq(SlicePurchaseController.EXTRA_REQUESTING_APP_NAME)); - // set up pending intent + // set up pending intents doReturn(TelephonyManager.PHONE_PROCESS_NAME).when(mPendingIntent).getCreatorPackage(); doReturn(true).when(mPendingIntent).isBroadcast(); doReturn(mPendingIntent).when(mIntent).getParcelableExtra( anyString(), eq(PendingIntent.class)); + doReturn(TelephonyManager.PHONE_PROCESS_NAME).when(mNotificationShownIntent) + .getCreatorPackage(); + doReturn(true).when(mNotificationShownIntent).isBroadcast(); + doReturn(mNotificationShownIntent).when(mIntent).getParcelableExtra( + eq(SlicePurchaseController.EXTRA_INTENT_NOTIFICATION_SHOWN), + eq(PendingIntent.class)); // set up notification doReturn(mResources).when(mContext).getResources(); @@ -185,8 +192,10 @@ public class SlicePurchaseBroadcastReceiverTest { assertEquals(2, notification.actions.length); assertEquals(mCanceledIntent, notification.actions[0].actionIntent); assertEquals(mContentIntent2, notification.actions[1].actionIntent); - } + // verify SlicePurchaseController was notified + verify(mNotificationShownIntent).send(); + } @Test public void testNotificationCanceled() {