diff options
author | Lorenzo Colitti <lorenzo@google.com> | 2015-01-26 22:22:27 +0000 |
---|---|---|
committer | Android Git Automerger <android-git-automerger@android.com> | 2015-01-26 22:22:27 +0000 |
commit | 583dec84d95b69216b4f91dd31397dc5f32f4e95 (patch) | |
tree | 337bb91a7db3756d9452a4620451d7c9af4a1ce4 /core | |
parent | bfe3c357043badac546fbf84d28ee8a81ccc1208 (diff) | |
parent | 3d992507a09b4d32c1e961a99d77027db050f01b (diff) | |
download | frameworks_base-583dec84d95b69216b4f91dd31397dc5f32f4e95.zip frameworks_base-583dec84d95b69216b4f91dd31397dc5f32f4e95.tar.gz frameworks_base-583dec84d95b69216b4f91dd31397dc5f32f4e95.tar.bz2 |
am 3d992507: am 09dff843: Merge "Only try to fetch time from NTP if there is a network connection." into lmp-mr1-dev
* commit '3d992507a09b4d32c1e961a99d77027db050f01b':
Only try to fetch time from NTP if there is a network connection.
Diffstat (limited to 'core')
-rw-r--r-- | core/java/android/util/NtpTrustedTime.java | 20 |
1 files changed, 20 insertions, 0 deletions
diff --git a/core/java/android/util/NtpTrustedTime.java b/core/java/android/util/NtpTrustedTime.java index b18cc3b..ed2d3c6 100644 --- a/core/java/android/util/NtpTrustedTime.java +++ b/core/java/android/util/NtpTrustedTime.java @@ -19,6 +19,8 @@ package android.util; import android.content.ContentResolver; import android.content.Context; import android.content.res.Resources; +import android.net.ConnectivityManager; +import android.net.NetworkInfo; import android.net.SntpClient; import android.os.SystemClock; import android.provider.Settings; @@ -35,10 +37,13 @@ public class NtpTrustedTime implements TrustedTime { private static final boolean LOGD = false; private static NtpTrustedTime sSingleton; + private static Context sContext; private final String mServer; private final long mTimeout; + private ConnectivityManager mCM; + private boolean mHasCache; private long mCachedNtpTime; private long mCachedNtpElapsedRealtime; @@ -67,6 +72,7 @@ public class NtpTrustedTime implements TrustedTime { final String server = secureServer != null ? secureServer : defaultServer; sSingleton = new NtpTrustedTime(server, timeout); + sContext = context; } return sSingleton; @@ -79,6 +85,20 @@ public class NtpTrustedTime implements TrustedTime { 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)) { |