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

Commit ea17661a authored by dianlujitao's avatar dianlujitao Committed by Michael Bestas
Browse files

NtpTrustedTime: Refresh NTP server from resources before requesting time

 * NtpTrustedTime initialized before carrier/country specific resources
   got loaded, so generally the default NTP server is always in use.
 * Always fetch NTP server from resources unless secure NTP server is
   set to ensure that carrier/country specific customization works.

Change-Id: I9b09680ea5640c35660d1cd0b910af22af930144
parent 1560d2c7
Loading
Loading
Loading
Loading
+5 −9
Original line number Diff line number Diff line
/*
 * Copyright (C) 2011 The Android Open Source Project
 * Copyright (C) 2018 The LineageOS Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
@@ -63,17 +64,14 @@ public class NtpTrustedTime implements TrustedTime {
            final Resources res = context.getResources();
            final ContentResolver resolver = context.getContentResolver();

            final String defaultServer = res.getString(
                    com.android.internal.R.string.config_ntpServer);
            final long defaultTimeout = res.getInteger(
                    com.android.internal.R.integer.config_ntpTimeout);

            final String secureServer = Settings.Global.getString(
            final String server = Settings.Global.getString(
                    resolver, Settings.Global.NTP_SERVER);
            final long timeout = Settings.Global.getLong(
                    resolver, Settings.Global.NTP_TIMEOUT, defaultTimeout);

            final String server = secureServer != null ? secureServer : defaultServer;
            sSingleton = new NtpTrustedTime(server, timeout);
            sContext = context;
        }
@@ -96,10 +94,8 @@ public class NtpTrustedTime implements TrustedTime {
    }

    public boolean forceRefresh(Network network) {
        if (TextUtils.isEmpty(mServer)) {
            // missing server, so no trusted time available
            return false;
        }
        final String realServer = TextUtils.isEmpty(mServer) ? sContext.getResources().getString(
                com.android.internal.R.string.config_ntpServer) : mServer;

        // We can't do this at initialization time: ConnectivityService might not be running yet.
        synchronized (this) {
@@ -117,7 +113,7 @@ public class NtpTrustedTime implements TrustedTime {

        if (LOGD) Log.d(TAG, "forceRefresh() from cache miss");
        final SntpClient client = new SntpClient();
        if (client.requestTime(mServer, (int) mTimeout, network)) {
        if (client.requestTime(realServer, (int) mTimeout, network)) {
            mHasCache = true;
            mCachedNtpTime = client.getNtpTime();
            mCachedNtpElapsedRealtime = client.getNtpTimeReference();