summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
author Treehugger Robot <android-test-infra-autosubmit@system.gserviceaccount.com> 2024-08-13 21:48:34 +0000
committer Automerger Merge Worker <android-build-automerger-merge-worker@system.gserviceaccount.com> 2024-08-13 21:48:34 +0000
commit4a6dfb93faff381132db0d65fcc632cca1c66f9b (patch)
tree3a0362b786c7a785fd69308653b6dd11cb9a6c99
parent247f087818e3a8736a8637952cf39cb2caff6a4d (diff)
parent50c7c5e2022d5f592f0cfd4267ad47fa607bfe50 (diff)
Merge "Migrate to best practise to read network state" into main am: 50c7c5e202
Original change: https://android-review.googlesource.com/c/platform/frameworks/base/+/3214635 Change-Id: I587af922ef62cd426a2602212c64a4497f7656b9 Signed-off-by: Automerger Merge Worker <android-build-automerger-merge-worker@system.gserviceaccount.com>
-rw-r--r--core/java/android/util/NtpTrustedTime.java18
1 files changed, 13 insertions, 5 deletions
diff --git a/core/java/android/util/NtpTrustedTime.java b/core/java/android/util/NtpTrustedTime.java
index 3adbd686cd2c..9f54d9fca24b 100644
--- a/core/java/android/util/NtpTrustedTime.java
+++ b/core/java/android/util/NtpTrustedTime.java
@@ -24,7 +24,7 @@ import android.content.Context;
import android.content.res.Resources;
import android.net.ConnectivityManager;
import android.net.Network;
-import android.net.NetworkInfo;
+import android.net.NetworkCapabilities;
import android.net.SntpClient;
import android.os.Build;
import android.os.SystemClock;
@@ -687,8 +687,16 @@ public abstract class NtpTrustedTime implements TrustedTime {
if (connectivityManager == null) {
return false;
}
- final NetworkInfo ni = connectivityManager.getNetworkInfo(network);
-
+ final NetworkCapabilities networkCapabilities =
+ connectivityManager.getNetworkCapabilities(network);
+ if (networkCapabilities == null) {
+ if (LOGD) Log.d(TAG, "getNetwork: failed to get network capabilities");
+ return false;
+ }
+ final boolean isConnectedToInternet = networkCapabilities.hasCapability(
+ NetworkCapabilities.NET_CAPABILITY_INTERNET)
+ && networkCapabilities.hasCapability(
+ NetworkCapabilities.NET_CAPABILITY_VALIDATED);
// This connectivity check is to avoid performing a DNS lookup for the time server on a
// unconnected network. There are races to obtain time in Android when connectivity
// changes, which means that forceRefresh() can be called by various components before
@@ -698,8 +706,8 @@ public abstract class NtpTrustedTime implements TrustedTime {
// A side effect of check is that tests that run a fake NTP server on the device itself
// will only be able to use it if the active network is connected, even though loopback
// addresses are actually reachable.
- if (ni == null || !ni.isConnected()) {
- if (LOGD) Log.d(TAG, "getNetwork: no connectivity");
+ if (!isConnectedToInternet) {
+ if (LOGD) Log.d(TAG, "getNetwork: no internet connectivity");
return false;
}
return true;