FileDocCategorySizeDatePackage
NtpTrustedTime.javaAPI DocAndroid 5.1 API5015Thu Mar 12 22:22:10 GMT 2015android.util

NtpTrustedTime

public class NtpTrustedTime extends Object implements TrustedTime
{@link TrustedTime} that connects with a remote NTP server as its trusted time source.
hide

Fields Summary
private static final String
TAG
private static final boolean
LOGD
private static NtpTrustedTime
sSingleton
private static android.content.Context
sContext
private final String
mServer
private final long
mTimeout
private android.net.ConnectivityManager
mCM
private boolean
mHasCache
private long
mCachedNtpTime
private long
mCachedNtpElapsedRealtime
private long
mCachedNtpCertainty
Constructors Summary
private NtpTrustedTime(String server, long timeout)


         
        if (LOGD) Log.d(TAG, "creating NtpTrustedTime using " + server);
        mServer = server;
        mTimeout = timeout;
    
Methods Summary
public longcurrentTimeMillis()

        if (!mHasCache) {
            throw new IllegalStateException("Missing authoritative time source");
        }
        if (LOGD) Log.d(TAG, "currentTimeMillis() cache hit");

        // current time is age after the last ntp cache; callers who
        // want fresh values will hit makeAuthoritative() first.
        return mCachedNtpTime + getCacheAge();
    
public booleanforceRefresh()

        if (mServer == null) {
            // missing server, so no trusted time available
            return false;
        }

        // We can't do this at initialization time: ConnectivityService might not be running yet.
        synchronized (this) {
            if (mCM == null) {
                mCM = (ConnectivityManager) sContext.getSystemService(Context.CONNECTIVITY_SERVICE);
            }
        }

        final NetworkInfo ni = mCM == null ? null : mCM.getActiveNetworkInfo();
        if (ni == null || !ni.isConnected()) {
            if (LOGD) Log.d(TAG, "forceRefresh: no connectivity");
            return false;
        }


        if (LOGD) Log.d(TAG, "forceRefresh() from cache miss");
        final SntpClient client = new SntpClient();
        if (client.requestTime(mServer, (int) mTimeout)) {
            mHasCache = true;
            mCachedNtpTime = client.getNtpTime();
            mCachedNtpElapsedRealtime = client.getNtpTimeReference();
            mCachedNtpCertainty = client.getRoundTripTime() / 2;
            return true;
        } else {
            return false;
        }
    
public longgetCacheAge()

        if (mHasCache) {
            return SystemClock.elapsedRealtime() - mCachedNtpElapsedRealtime;
        } else {
            return Long.MAX_VALUE;
        }
    
public longgetCacheCertainty()

        if (mHasCache) {
            return mCachedNtpCertainty;
        } else {
            return Long.MAX_VALUE;
        }
    
public longgetCachedNtpTime()

        if (LOGD) Log.d(TAG, "getCachedNtpTime() cache hit");
        return mCachedNtpTime;
    
public longgetCachedNtpTimeReference()

        return mCachedNtpElapsedRealtime;
    
public static synchronized android.util.NtpTrustedTimegetInstance(android.content.Context context)

        if (sSingleton == null) {
            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(
                    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;
        }

        return sSingleton;
    
public booleanhasCache()

        return mHasCache;