VVM: Do not do full sync more than once within a 1 minute span.

Sometimes duplicate sync requests will be made for visual voicemail. We
only want to do one sync for each of these requests.

Bug: 22607570
Change-Id: I0ec31a9e7d2d7823e2bbaf4441951dc552ce5709
diff --git a/src/com/android/phone/settings/VisualVoicemailSettingsUtil.java b/src/com/android/phone/settings/VisualVoicemailSettingsUtil.java
index 45ad1cb..888a5f9 100644
--- a/src/com/android/phone/settings/VisualVoicemailSettingsUtil.java
+++ b/src/com/android/phone/settings/VisualVoicemailSettingsUtil.java
@@ -39,6 +39,10 @@
     // If a carrier vvm app is installed, Google visual voicemail is automatically switched off
     // however, the user can override this setting.
     private static final String IS_USER_SET = "is_user_set";
+    // Record the timestamp of the last full sync so that duplicate syncs can be reduced.
+    private static final String LAST_FULL_SYNC_TIMESTAMP = "last_full_sync_timestamp";
+    // Constant indicating that there has never been a full sync.
+    public static final long NO_PRIOR_FULL_SYNC = -1;
 
     // Setting for how often retries should be done.
     private static final String SYNC_RETRY_INTERVAL = "sync_retry_interval";
@@ -142,6 +146,24 @@
         editor.commit();
     }
 
+    public static void setVisualVoicemailLastFullSyncTime(Context context,
+            PhoneAccountHandle phoneAccount, long timestamp) {
+        SharedPreferences.Editor editor =
+                PreferenceManager.getDefaultSharedPreferences(context).edit();
+        editor.putLong(getVisualVoicemailSharedPrefsKey(LAST_FULL_SYNC_TIMESTAMP, phoneAccount),
+                timestamp);
+        editor.commit();
+
+    }
+
+    public static long getVisualVoicemailLastFullSyncTime(Context context,
+            PhoneAccountHandle phoneAccount) {
+        SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
+        return prefs.getLong(
+                getVisualVoicemailSharedPrefsKey(LAST_FULL_SYNC_TIMESTAMP, phoneAccount),
+                NO_PRIOR_FULL_SYNC);
+    }
+
     private static String getVisualVoicemailSharedPrefsKey(String key,
             PhoneAccountHandle phoneAccount) {
         return VISUAL_VOICEMAIL_SHARED_PREFS_KEY_PREFIX + key + "_" + phoneAccount.getId();
diff --git a/src/com/android/phone/vvm/omtp/sync/OmtpVvmSyncService.java b/src/com/android/phone/vvm/omtp/sync/OmtpVvmSyncService.java
index 199e97d..5667b94 100644
--- a/src/com/android/phone/vvm/omtp/sync/OmtpVvmSyncService.java
+++ b/src/com/android/phone/vvm/omtp/sync/OmtpVvmSyncService.java
@@ -58,6 +58,9 @@
     // Timeout used to call ConnectivityManager.requestNetwork
     private static final int NETWORK_REQUEST_TIMEOUT_MILLIS = 60 * 1000;
 
+    // Minimum time allowed between full syncs
+    private static final int MINIMUM_FULL_SYNC_INTERVAL_MILLIS = 60 * 1000;
+
     // Number of retries
     private static final int NETWORK_RETRY_COUNT = 6;
 
@@ -164,6 +167,20 @@
             return;
         }
 
+        if (SYNC_FULL_SYNC.equals(action)) {
+            long lastSyncTime = VisualVoicemailSettingsUtil.getVisualVoicemailLastFullSyncTime(
+                    this, phoneAccount);
+            long currentTime = System.currentTimeMillis();
+            if (currentTime - lastSyncTime < MINIMUM_FULL_SYNC_INTERVAL_MILLIS) {
+                // If it's been less than a minute since the last sync, bail.
+                Log.v(TAG, "Avoiding duplicate full sync: synced recently for "
+                        + phoneAccount.getId());
+                return;
+            }
+            VisualVoicemailSettingsUtil.setVisualVoicemailLastFullSyncTime(
+                    this, phoneAccount, currentTime);
+        }
+
         OmtpVvmNetworkRequestCallback networkCallback = new OmtpVvmNetworkRequestCallback(this,
                 phoneAccount, action);
         requestNetwork(networkCallback);