Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit c0a9edb6 authored by Amith Yamasani's avatar Amith Yamasani Committed by Android (Google) Code Review
Browse files

Merge "Make NTP constants configurable."

parents 8dfc8291 c3560b81
Loading
Loading
Loading
Loading
+10 −0
Original line number Original line Diff line number Diff line
@@ -882,6 +882,16 @@


    <!-- Remote server that can provide NTP responses. -->
    <!-- Remote server that can provide NTP responses. -->
    <string translatable="false" name="config_ntpServer">2.android.pool.ntp.org</string>
    <string translatable="false" name="config_ntpServer">2.android.pool.ntp.org</string>
    <!-- Normal polling frequency in milliseconds -->
    <integer name="config_ntpPollingInterval">864000000</integer>
    <!-- Try-again polling interval in milliseconds, in case the network request failed -->
    <integer name="config_ntpPollingIntervalShorter">60000</integer>
    <!-- Number of times to try again with the shorter interval, before backing
         off until the normal polling interval. A value < 0 indicates infinite. -->
    <integer name="config_ntpRetry">3</integer>
    <!-- If the time difference is greater than this threshold in milliseconds,
         then update the time. -->
    <integer name="config_ntpThreshold">5000</integer>
    <!-- Timeout to wait for NTP server response. -->
    <!-- Timeout to wait for NTP server response. -->
    <integer name="config_ntpTimeout">20000</integer>
    <integer name="config_ntpTimeout">20000</integer>


+4 −0
Original line number Original line Diff line number Diff line
@@ -281,6 +281,10 @@
  <java-symbol type="integer" name="config_cursorWindowSize" />
  <java-symbol type="integer" name="config_cursorWindowSize" />
  <java-symbol type="integer" name="config_longPressOnPowerBehavior" />
  <java-symbol type="integer" name="config_longPressOnPowerBehavior" />
  <java-symbol type="integer" name="config_max_pan_devices" />
  <java-symbol type="integer" name="config_max_pan_devices" />
  <java-symbol type="integer" name="config_ntpPollingInterval" />
  <java-symbol type="integer" name="config_ntpPollingIntervalShorter" />
  <java-symbol type="integer" name="config_ntpRetry" />
  <java-symbol type="integer" name="config_ntpThreshold" />
  <java-symbol type="integer" name="config_ntpTimeout" />
  <java-symbol type="integer" name="config_ntpTimeout" />
  <java-symbol type="integer" name="config_wifi_framework_scan_interval" />
  <java-symbol type="integer" name="config_wifi_framework_scan_interval" />
  <java-symbol type="integer" name="config_wifi_supplicant_scan_interval" />
  <java-symbol type="integer" name="config_wifi_supplicant_scan_interval" />
+30 −21
Original line number Original line Diff line number Diff line
@@ -57,15 +57,6 @@ public class NetworkTimeUpdateService {
    private static final int EVENT_POLL_NETWORK_TIME = 2;
    private static final int EVENT_POLL_NETWORK_TIME = 2;
    private static final int EVENT_NETWORK_CONNECTED = 3;
    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 =
    private static final String ACTION_POLL =
            "com.android.server.NetworkTimeUpdateService.action.POLL";
            "com.android.server.NetworkTimeUpdateService.action.POLL";
    private static int POLL_REQUEST = 0;
    private static int POLL_REQUEST = 0;
@@ -86,6 +77,15 @@ public class NetworkTimeUpdateService {
    private SettingsObserver mSettingsObserver;
    private SettingsObserver mSettingsObserver;
    // The last time that we successfully fetched the NTP time.
    // The last time that we successfully fetched the NTP time.
    private long mLastNtpFetchTime = NOT_SET;
    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.
    // 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
    // During bootup, the network may not have been up yet, or it's taking time for the
    // connection to happen.
    // connection to happen.
@@ -97,6 +97,15 @@ public class NetworkTimeUpdateService {
        mAlarmManager = (AlarmManager) mContext.getSystemService(Context.ALARM_SERVICE);
        mAlarmManager = (AlarmManager) mContext.getSystemService(Context.ALARM_SERVICE);
        Intent pollIntent = new Intent(ACTION_POLL, null);
        Intent pollIntent = new Intent(ACTION_POLL, null);
        mPendingPollIntent = PendingIntent.getBroadcast(mContext, POLL_REQUEST, pollIntent, 0);
        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 */
    /** Initialize the receivers and initiate the first NTP request */
@@ -143,35 +152,35 @@ public class NetworkTimeUpdateService {
        if (!isAutomaticTimeRequested()) return;
        if (!isAutomaticTimeRequested()) return;


        final long refTime = SystemClock.elapsedRealtime();
        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.
        // no need to sync to NTP.
        if (mNitzTimeSetTime != NOT_SET && refTime - mNitzTimeSetTime < POLLING_INTERVAL_MS) {
        if (mNitzTimeSetTime != NOT_SET && refTime - mNitzTimeSetTime < mPollingIntervalMs) {
            resetAlarm(POLLING_INTERVAL_MS);
            resetAlarm(mPollingIntervalMs);
            return;
            return;
        }
        }
        final long currentTime = System.currentTimeMillis();
        final long currentTime = System.currentTimeMillis();
        if (DBG) Log.d(TAG, "System time = " + currentTime);
        if (DBG) Log.d(TAG, "System time = " + currentTime);
        // Get the NTP time
        // 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) {
                || event == EVENT_AUTO_TIME_CHANGED) {
            if (DBG) Log.d(TAG, "Before Ntp fetch");
            if (DBG) Log.d(TAG, "Before Ntp fetch");


            // force refresh NTP cache when outdated
            // force refresh NTP cache when outdated
            if (mTime.getCacheAge() >= POLLING_INTERVAL_MS) {
            if (mTime.getCacheAge() >= mPollingIntervalMs) {
                mTime.forceRefresh();
                mTime.forceRefresh();
            }
            }


            // only update when NTP time is fresh
            // only update when NTP time is fresh
            if (mTime.getCacheAge() < POLLING_INTERVAL_MS) {
            if (mTime.getCacheAge() < mPollingIntervalMs) {
                final long ntp = mTime.currentTimeMillis();
                final long ntp = mTime.currentTimeMillis();
                mTryAgainCounter = 0;
                mTryAgainCounter = 0;
                // If the clock is more than N seconds off or this is the first time it's been
                // 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.
                // 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) {
                        || mLastNtpFetchTime == NOT_SET) {
                    // Set the system time
                    // Set the system time
                    if (DBG && mLastNtpFetchTime == NOT_SET
                    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);
                        Log.d(TAG, "For initial setup, rtc = " + currentTime);
                    }
                    }
                    if (DBG) Log.d(TAG, "Ntp time to be set = " + ntp);
                    if (DBG) Log.d(TAG, "Ntp time to be set = " + ntp);
@@ -186,17 +195,17 @@ public class NetworkTimeUpdateService {
            } else {
            } else {
                // Try again shortly
                // Try again shortly
                mTryAgainCounter++;
                mTryAgainCounter++;
                if (mTryAgainCounter <= TRY_AGAIN_TIMES_MAX) {
                if (mTryAgainTimesMax < 0 || mTryAgainCounter <= mTryAgainTimesMax) {
                    resetAlarm(POLLING_INTERVAL_SHORTER_MS);
                    resetAlarm(mPollingIntervalShorterMs);
                } else {
                } else {
                    // Try much later
                    // Try much later
                    mTryAgainCounter = 0;
                    mTryAgainCounter = 0;
                    resetAlarm(POLLING_INTERVAL_MS);
                    resetAlarm(mPollingIntervalMs);
                }
                }
                return;
                return;
            }
            }
        }
        }
        resetAlarm(POLLING_INTERVAL_MS);
        resetAlarm(mPollingIntervalMs);
    }
    }


    /**
    /**