Fix a bug where the ASM upsell never stops showing.
am: 28f13680ec

Change-Id: Id2265c6ad78caea2ed0ee4018496499153c7242c
diff --git a/src/com/android/storagemanager/deletionhelper/ConfirmDeletionDialog.java b/src/com/android/storagemanager/deletionhelper/ConfirmDeletionDialog.java
index 640d68b..aef5038 100644
--- a/src/com/android/storagemanager/deletionhelper/ConfirmDeletionDialog.java
+++ b/src/com/android/storagemanager/deletionhelper/ConfirmDeletionDialog.java
@@ -80,7 +80,8 @@
                 ((DeletionHelperSettings) getTargetFragment()).clearData();
                 MetricsLogger.action(getContext(),
                         MetricsEvent.ACTION_DELETION_HELPER_REMOVE_CONFIRM);
-                if (StorageManagerUpsellDialog.shouldShow(getContext())) {
+                if (StorageManagerUpsellDialog.shouldShow(
+                        getContext(), System.currentTimeMillis())) {
                     StorageManagerUpsellDialog upsellDialog =
                             StorageManagerUpsellDialog.newInstance(mFreeableBytes);
                     upsellDialog.show(getFragmentManager(), StorageManagerUpsellDialog.TAG);
diff --git a/src/com/android/storagemanager/deletionhelper/StorageManagerUpsellDialog.java b/src/com/android/storagemanager/deletionhelper/StorageManagerUpsellDialog.java
index 405db87..cb19ada 100644
--- a/src/com/android/storagemanager/deletionhelper/StorageManagerUpsellDialog.java
+++ b/src/com/android/storagemanager/deletionhelper/StorageManagerUpsellDialog.java
@@ -23,6 +23,7 @@
 import android.content.SharedPreferences;
 import android.os.Bundle;
 import android.provider.Settings;
+import android.support.annotation.VisibleForTesting;
 import android.text.format.Formatter;
 import com.android.storagemanager.R;
 
@@ -49,6 +50,8 @@
     private static final long NO_THANKS_LONG_DELAY = NEVER;
     private static final int NO_THANKS_LONG_THRESHOLD = 3;
 
+    private Clock mClock;
+
     public static StorageManagerUpsellDialog newInstance(long freedBytes) {
         StorageManagerUpsellDialog dialog = new StorageManagerUpsellDialog();
         Bundle args = new Bundle(1);
@@ -57,6 +60,11 @@
         return dialog;
     }
 
+    @VisibleForTesting(otherwise = VisibleForTesting.NONE)
+    protected void setClock(Clock clock) {
+        mClock = clock;
+    }
+
     @Override
     public Dialog onCreateDialog(Bundle savedInstanceState) {
         final Bundle args = getArguments();
@@ -82,8 +90,9 @@
             int noThanksCount = sp.getInt(NO_THANKS_COUNT, 0) + 1;
             SharedPreferences.Editor editor = sp.edit();
             editor.putInt(NO_THANKS_COUNT, noThanksCount);
-            editor.putLong(NEXT_SHOW_TIME,
-                    System.currentTimeMillis() + getNoThanksDelay(noThanksCount));
+            long noThanksDelay = getNoThanksDelay(noThanksCount);
+            long nextShowTime = noThanksDelay == NEVER ? NEVER : getCurrentTime() + noThanksDelay;
+            editor.putLong(NEXT_SHOW_TIME, nextShowTime);
             editor.apply();
         }
 
@@ -96,8 +105,7 @@
         int dismissCount = sp.getInt(DISMISSED_COUNT, 0) + 1;
         SharedPreferences.Editor editor = sp.edit();
         editor.putInt(DISMISSED_COUNT, dismissCount);
-        editor.putLong(NEXT_SHOW_TIME,
-                System.currentTimeMillis() + getDismissDelay(dismissCount));
+        editor.putLong(NEXT_SHOW_TIME, getCurrentTime() + getDismissDelay(dismissCount));
         editor.apply();
 
         finishActivity();
@@ -106,8 +114,9 @@
     /**
      * Returns if the dialog should be shown, given the delays between when it is shown.
      * @param context Context to get shared preferences for determining the next show time.
+     * @param time The current time in millis.
      */
-    public static boolean shouldShow(Context context) {
+    public static boolean shouldShow(Context context, long time) {
         boolean isEnabled =
                 Settings.Secure.getInt(context.getContentResolver(),
                         Settings.Secure.AUTOMATIC_STORAGE_MANAGER_ENABLED, 0) != 0;
@@ -116,12 +125,11 @@
         }
 
         long nextTimeToShow = getSharedPreferences(context).getLong(NEXT_SHOW_TIME, 0);
-
         if (nextTimeToShow == NEVER) {
             return false;
         }
 
-        return System.currentTimeMillis() > nextTimeToShow;
+        return time >= nextTimeToShow;
     }
 
     private static SharedPreferences getSharedPreferences(Context context) {
@@ -144,4 +152,24 @@
             activity.finish();
         }
     }
+
+    private long getCurrentTime() {
+        if (mClock == null) {
+            mClock = new Clock();
+        }
+
+        return mClock.currentTimeMillis();
+    }
+
+    /**
+     * Clock provides the current time.
+     */
+    protected static class Clock {
+        /**
+         * Returns the current time in milliseconds.
+         */
+        public long currentTimeMillis() {
+            return System.currentTimeMillis();
+        }
+    }
 }