diff options
| author | Lorenzo Colitti <lorenzo@google.com> | 2015-01-26 22:08:16 +0000 |
|---|---|---|
| committer | Android (Google) Code Review <android-gerrit@google.com> | 2015-01-26 22:08:19 +0000 |
| commit | 09dff843b8ed402564c4c89d7e1cabceb715e458 (patch) | |
| tree | 8658926973254855d93cb04696627c888b0e1650 /core/java/android/util | |
| parent | 8df7f4368425f24f46bb8605dd3d1e3d3b97e6ad (diff) | |
| parent | df59053588501178263e0e8de37f23717e649ffc (diff) | |
| download | frameworks_base-09dff843b8ed402564c4c89d7e1cabceb715e458.zip frameworks_base-09dff843b8ed402564c4c89d7e1cabceb715e458.tar.gz frameworks_base-09dff843b8ed402564c4c89d7e1cabceb715e458.tar.bz2 | |
Merge "Only try to fetch time from NTP if there is a network connection." into lmp-mr1-dev
Diffstat (limited to 'core/java/android/util')
| -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 602a68c..8ebcacd 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; @@ -34,10 +36,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; @@ -66,6 +71,7 @@ public class NtpTrustedTime implements TrustedTime { final String server = secureServer != null ? secureServer : defaultServer; sSingleton = new NtpTrustedTime(server, timeout); + sContext = context; } return sSingleton; @@ -78,6 +84,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)) { |
