Make NTP constants configurable.

Bug: 6943067
Change-Id: I0b0f9076fba99e9c457ef7180b9936c0d2a7d589
diff --git a/services/java/com/android/server/NetworkTimeUpdateService.java b/services/java/com/android/server/NetworkTimeUpdateService.java
index 790be55..3bfd190 100644
--- a/services/java/com/android/server/NetworkTimeUpdateService.java
+++ b/services/java/com/android/server/NetworkTimeUpdateService.java
@@ -57,15 +57,6 @@
     private static final int EVENT_POLL_NETWORK_TIME = 2;
     private static final int EVENT_NETWORK_CONNECTED = 3;
 
-    /** Normal polling frequency */
-    private static final long POLLING_INTERVAL_MS = 24L * 60 * 60 * 1000; // 24 hrs
-    /** Try-again polling interval, in case the network request failed */
-    private static final long POLLING_INTERVAL_SHORTER_MS = 60 * 1000L; // 60 seconds
-    /** Number of times to try again */
-    private static final int TRY_AGAIN_TIMES_MAX = 3;
-    /** If the time difference is greater than this threshold, then update the time. */
-    private static final int TIME_ERROR_THRESHOLD_MS = 5 * 1000;
-
     private static final String ACTION_POLL =
             "com.android.server.NetworkTimeUpdateService.action.POLL";
     private static int POLL_REQUEST = 0;
@@ -86,6 +77,15 @@
     private SettingsObserver mSettingsObserver;
     // The last time that we successfully fetched the NTP time.
     private long mLastNtpFetchTime = NOT_SET;
+
+    // Normal polling frequency
+    private final long mPollingIntervalMs;
+    // Try-again polling interval, in case the network request failed
+    private final long mPollingIntervalShorterMs;
+    // Number of times to try again
+    private final int mTryAgainTimesMax;
+    // If the time difference is greater than this threshold, then update the time.
+    private final int mTimeErrorThresholdMs;
     // Keeps track of how many quick attempts were made to fetch NTP time.
     // During bootup, the network may not have been up yet, or it's taking time for the
     // connection to happen.
@@ -97,6 +97,15 @@
         mAlarmManager = (AlarmManager) mContext.getSystemService(Context.ALARM_SERVICE);
         Intent pollIntent = new Intent(ACTION_POLL, null);
         mPendingPollIntent = PendingIntent.getBroadcast(mContext, POLL_REQUEST, pollIntent, 0);
+
+        mPollingIntervalMs = mContext.getResources().getInteger(
+                com.android.internal.R.integer.config_ntpPollingInterval);
+        mPollingIntervalShorterMs = mContext.getResources().getInteger(
+                com.android.internal.R.integer.config_ntpPollingIntervalShorter);
+        mTryAgainTimesMax = mContext.getResources().getInteger(
+                com.android.internal.R.integer.config_ntpRetry);
+        mTimeErrorThresholdMs = mContext.getResources().getInteger(
+                com.android.internal.R.integer.config_ntpThreshold);
     }
 
     /** Initialize the receivers and initiate the first NTP request */
@@ -143,35 +152,35 @@
         if (!isAutomaticTimeRequested()) return;
 
         final long refTime = SystemClock.elapsedRealtime();
-        // If NITZ time was received less than POLLING_INTERVAL_MS time ago,
+        // If NITZ time was received less than mPollingIntervalMs time ago,
         // no need to sync to NTP.
-        if (mNitzTimeSetTime != NOT_SET && refTime - mNitzTimeSetTime < POLLING_INTERVAL_MS) {
-            resetAlarm(POLLING_INTERVAL_MS);
+        if (mNitzTimeSetTime != NOT_SET && refTime - mNitzTimeSetTime < mPollingIntervalMs) {
+            resetAlarm(mPollingIntervalMs);
             return;
         }
         final long currentTime = System.currentTimeMillis();
         if (DBG) Log.d(TAG, "System time = " + currentTime);
         // Get the NTP time
-        if (mLastNtpFetchTime == NOT_SET || refTime >= mLastNtpFetchTime + POLLING_INTERVAL_MS
+        if (mLastNtpFetchTime == NOT_SET || refTime >= mLastNtpFetchTime + mPollingIntervalMs
                 || event == EVENT_AUTO_TIME_CHANGED) {
             if (DBG) Log.d(TAG, "Before Ntp fetch");
 
             // force refresh NTP cache when outdated
-            if (mTime.getCacheAge() >= POLLING_INTERVAL_MS) {
+            if (mTime.getCacheAge() >= mPollingIntervalMs) {
                 mTime.forceRefresh();
             }
 
             // only update when NTP time is fresh
-            if (mTime.getCacheAge() < POLLING_INTERVAL_MS) {
+            if (mTime.getCacheAge() < mPollingIntervalMs) {
                 final long ntp = mTime.currentTimeMillis();
                 mTryAgainCounter = 0;
                 // If the clock is more than N seconds off or this is the first time it's been
                 // fetched since boot, set the current time.
-                if (Math.abs(ntp - currentTime) > TIME_ERROR_THRESHOLD_MS
+                if (Math.abs(ntp - currentTime) > mTimeErrorThresholdMs
                         || mLastNtpFetchTime == NOT_SET) {
                     // Set the system time
                     if (DBG && mLastNtpFetchTime == NOT_SET
-                            && Math.abs(ntp - currentTime) <= TIME_ERROR_THRESHOLD_MS) {
+                            && Math.abs(ntp - currentTime) <= mTimeErrorThresholdMs) {
                         Log.d(TAG, "For initial setup, rtc = " + currentTime);
                     }
                     if (DBG) Log.d(TAG, "Ntp time to be set = " + ntp);
@@ -186,17 +195,17 @@
             } else {
                 // Try again shortly
                 mTryAgainCounter++;
-                if (mTryAgainCounter <= TRY_AGAIN_TIMES_MAX) {
-                    resetAlarm(POLLING_INTERVAL_SHORTER_MS);
+                if (mTryAgainTimesMax < 0 || mTryAgainCounter <= mTryAgainTimesMax) {
+                    resetAlarm(mPollingIntervalShorterMs);
                 } else {
                     // Try much later
                     mTryAgainCounter = 0;
-                    resetAlarm(POLLING_INTERVAL_MS);
+                    resetAlarm(mPollingIntervalMs);
                 }
                 return;
             }
         }
-        resetAlarm(POLLING_INTERVAL_MS);
+        resetAlarm(mPollingIntervalMs);
     }
 
     /**