Fix a bug where the storage manager notification showed too many times.

This occurred when the user dismisses the notification. An intent without
metadata was being used as the delete intent, causing the notification
controller to not increment its shown count.

Bug: 32183602
Test: StorageManager robotests
Change-Id: Ide3a038d475ef313a71b262419c8720c65fb140d
(cherry picked from commit 9f6d7f00138afbb634fabf04d8dd67f6e86b60f5)
diff --git a/src/com/android/storagemanager/automatic/NotificationController.java b/src/com/android/storagemanager/automatic/NotificationController.java
index 8f50bdb..443caa3 100644
--- a/src/com/android/storagemanager/automatic/NotificationController.java
+++ b/src/com/android/storagemanager/automatic/NotificationController.java
@@ -76,11 +76,13 @@
     private static final String SHARED_PREFERENCES_NAME = "NotificationController";
     private static final String NOTIFICATION_NEXT_SHOW_TIME = "notification_next_show_time";
     private static final String NOTIFICATION_SHOWN_COUNT = "notification_shown_count";
+    private static final String NOTIFICATION_DISMISS_COUNT = "notification_dismiss_count";
     private static final String STORAGE_MANAGER_PROPERTY = "ro.storage_manager.enabled";
 
     private static final long DISMISS_DELAY = TimeUnit.DAYS.toMillis(15);
     private static final long NO_THANKS_DELAY = TimeUnit.DAYS.toMillis(90);
     private static final long MAXIMUM_SHOWN_COUNT = 4;
+    private static final long MAXIMUM_DISMISS_COUNT = 9;
     private static final int NOTIFICATION_ID = 0;
 
     @Override
@@ -130,7 +132,8 @@
                 SHARED_PREFERENCES_NAME,
                 Context.MODE_PRIVATE);
         int timesShown = sp.getInt(NOTIFICATION_SHOWN_COUNT, 0);
-        if (timesShown > MAXIMUM_SHOWN_COUNT) {
+        int timesDismissed = sp.getInt(NOTIFICATION_DISMISS_COUNT, 0);
+        if (timesShown >= MAXIMUM_SHOWN_COUNT || timesDismissed >= MAXIMUM_DISMISS_COUNT) {
             return false;
         }
 
@@ -159,7 +162,7 @@
         Intent dismissIntent = new Intent(INTENT_ACTION_DISMISS);
         dismissIntent.putExtra(INTENT_EXTRA_ID, NOTIFICATION_ID);
         PendingIntent deleteIntent = PendingIntent.getBroadcast(context, 0,
-                new Intent(INTENT_ACTION_DISMISS),
+                dismissIntent,
                 PendingIntent.FLAG_ONE_SHOT);
 
         Intent contentIntent = new Intent(INTENT_ACTION_TAP);
@@ -186,6 +189,12 @@
     }
 
     private void cancelNotification(Context context, Intent intent) {
+        if (intent.getAction() == INTENT_ACTION_DISMISS) {
+            incrementNotificationDismissedCount(context);
+        } else {
+            incrementNotificationShownCount(context);
+        }
+
         int id = intent.getIntExtra(INTENT_EXTRA_ID, -1);
         if (id == -1) {
             return;
@@ -193,8 +202,6 @@
         NotificationManager manager = (NotificationManager) context
                 .getSystemService(Context.NOTIFICATION_SERVICE);
         manager.cancel(id);
-
-        incrementNotificationShownCount(context);
     }
 
     private void incrementNotificationShownCount(Context context) {
@@ -206,6 +213,15 @@
         editor.apply();
     }
 
+    private void incrementNotificationDismissedCount(Context context) {
+        SharedPreferences sp = context.getSharedPreferences(SHARED_PREFERENCES_NAME,
+                Context.MODE_PRIVATE);
+        SharedPreferences.Editor editor = sp.edit();
+        int dismissCount = sp.getInt(NOTIFICATION_DISMISS_COUNT, 0) + 1;
+        editor.putInt(NOTIFICATION_DISMISS_COUNT, dismissCount);
+        editor.apply();
+    }
+
     private void delayNextNotification(Context context, long timeInMillis) {
         SharedPreferences sp = context.getSharedPreferences(SHARED_PREFERENCES_NAME,
                 Context.MODE_PRIVATE);