diff options
| author | Kurt Marcinkiewicz <kurdt@google.com> | 2018-03-05 14:45:04 -0800 |
|---|---|---|
| committer | Remi NGUYEN VAN <reminv@google.com> | 2018-05-16 16:43:43 +0900 |
| commit | 7a4ff6447df79de973ff13751bea3bde689cfcd0 (patch) | |
| tree | c8350bf52459c5a2e529293b902465bd57d48a68 /core/java/android | |
| parent | 6fa8d06c78d21d455015e19691d5d6f2e6bd258b (diff) | |
Allow specifying a network for SNTP time sync
Permits syncing over a specific network instead of the default for
the process. This was causing an issue with Android Wear devices
paired with iOS where the default network is bluetooth
(see b/32663274).
This CL is in support of ag/3776564
Bug: 32663274
Test: adb shell am instrument -e class android.net.SntpClientTest -w \
com.android.frameworks.coretests/android.support.test.runner.AndroidJUnitRunner
(cherry-pick of pi-dev Ic9fc169cf75457810d4992121d85d7642e350b90)
Merged-In: I339c77063c72a9d76a5c4cb17052e20fb6e045a6
Merged-In: I8dfd1cad99c63efdc14c174c19f094a61cdfc44f
Change-Id: I44df66688292b144ec7dfcdd9ae5d82489f82774
Diffstat (limited to 'core/java/android')
| -rw-r--r-- | core/java/android/net/SntpClient.java | 16 | ||||
| -rw-r--r-- | core/java/android/util/NtpTrustedTime.java | 19 |
2 files changed, 28 insertions, 7 deletions
diff --git a/core/java/android/net/SntpClient.java b/core/java/android/net/SntpClient.java index ffc735c93aef..66cdc99150a5 100644 --- a/core/java/android/net/SntpClient.java +++ b/core/java/android/net/SntpClient.java @@ -80,25 +80,27 @@ public class SntpClient { * * @param host host name of the server. * @param timeout network timeout in milliseconds. + * @param network network over which to send the request. * @return true if the transaction was successful. */ - public boolean requestTime(String host, int timeout) { + public boolean requestTime(String host, int timeout, Network network) { InetAddress address = null; try { - address = InetAddress.getByName(host); + address = network.getByName(host); } catch (Exception e) { EventLogTags.writeNtpFailure(host, e.toString()); if (DBG) Log.d(TAG, "request time failed: " + e); return false; } - return requestTime(address, NTP_PORT, timeout); + return requestTime(address, NTP_PORT, timeout, network); } - public boolean requestTime(InetAddress address, int port, int timeout) { + public boolean requestTime(InetAddress address, int port, int timeout, Network network) { DatagramSocket socket = null; final int oldTag = TrafficStats.getAndSetThreadStatsTag(TrafficStats.TAG_SYSTEM_NTP); try { socket = new DatagramSocket(); + network.bindSocket(socket); socket.setSoTimeout(timeout); byte[] buffer = new byte[NTP_PACKET_SIZE]; DatagramPacket request = new DatagramPacket(buffer, buffer.length, address, port); @@ -168,6 +170,12 @@ public class SntpClient { return true; } + @Deprecated + public boolean requestTime(String host, int timeout) { + Log.w(TAG, "Shame on you for calling the hidden API requestTime()!"); + return false; + } + /** * Returns the time computed from the NTP transaction. * diff --git a/core/java/android/util/NtpTrustedTime.java b/core/java/android/util/NtpTrustedTime.java index ed2d3c60fcd0..30d7b6c0786c 100644 --- a/core/java/android/util/NtpTrustedTime.java +++ b/core/java/android/util/NtpTrustedTime.java @@ -20,6 +20,7 @@ import android.content.ContentResolver; import android.content.Context; import android.content.res.Resources; import android.net.ConnectivityManager; +import android.net.Network; import android.net.NetworkInfo; import android.net.SntpClient; import android.os.SystemClock; @@ -80,6 +81,18 @@ public class NtpTrustedTime implements TrustedTime { @Override public boolean forceRefresh() { + // We can't do this at initialization time: ConnectivityService might not be running yet. + synchronized (this) { + if (mCM == null) { + mCM = sContext.getSystemService(ConnectivityManager.class); + } + } + + final Network network = mCM == null ? null : mCM.getActiveNetwork(); + return forceRefresh(network); + } + + public boolean forceRefresh(Network network) { if (TextUtils.isEmpty(mServer)) { // missing server, so no trusted time available return false; @@ -88,11 +101,11 @@ public class NtpTrustedTime implements TrustedTime { // 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); + mCM = sContext.getSystemService(ConnectivityManager.class); } } - final NetworkInfo ni = mCM == null ? null : mCM.getActiveNetworkInfo(); + final NetworkInfo ni = mCM == null ? null : mCM.getNetworkInfo(network); if (ni == null || !ni.isConnected()) { if (LOGD) Log.d(TAG, "forceRefresh: no connectivity"); return false; @@ -101,7 +114,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)) { + if (client.requestTime(mServer, (int) mTimeout, network)) { mHasCache = true; mCachedNtpTime = client.getNtpTime(); mCachedNtpElapsedRealtime = client.getNtpTimeReference(); |
