specs =
- CaptivePortalProbeSpec.parseCaptivePortalProbeSpecs(settingsValue);
- final CaptivePortalProbeSpec[] specsArray = new CaptivePortalProbeSpec[specs.size()];
- return specs.toArray(specsArray);
+ return getArrayConfig(providerValue, R.array.config_captive_portal_fallback_probe_specs,
+ R.array.default_captive_portal_fallback_probe_specs,
+ CaptivePortalProbeSpec::parseSpecOrNull);
} catch (Exception e) {
// Don't let a misconfiguration bootloop the system.
Log.e(TAG, "Error parsing configured fallback probe specs", e);
@@ -1253,6 +1261,83 @@ public class NetworkMonitor extends StateMachine {
}
}
+ /**
+ * Read a setting from a resource or the settings provider.
+ *
+ * The configuration resource is prioritized, then the provider value, then the default
+ * resource value.
+ * @param context The context
+ * @param configResource The resource id for the configuration parameter
+ * @param defaultResource The resource id for the default value
+ * @param symbol The symbol in the settings provider
+ * @return The best available value
+ */
+ @NonNull
+ private String getSettingFromResource(@NonNull final Context context,
+ @StringRes int configResource, @StringRes int defaultResource,
+ @NonNull String symbol) {
+ final Resources res = context.getResources();
+ String setting = res.getString(configResource);
+
+ if (!TextUtils.isEmpty(setting)) return setting;
+
+ setting = mDependencies.getSetting(context, symbol, null);
+ if (!TextUtils.isEmpty(setting)) return setting;
+
+ return res.getString(defaultResource);
+ }
+
+ /**
+ * Get an array configuration from resources or the settings provider.
+ *
+ *
The configuration resource is prioritized, then the provider values, then the default
+ * resource values.
+ * @param providerValue Values obtained from the setting provider.
+ * @param configResId ID of the configuration resource.
+ * @param defaultResId ID of the default resource.
+ * @param resourceConverter Converter from the resource strings to stored setting class. Null
+ * return values are ignored.
+ */
+ private T[] getArrayConfig(@NonNull T[] providerValue, @ArrayRes int configResId,
+ @ArrayRes int defaultResId, @NonNull Function resourceConverter) {
+ final Resources res = mContext.getResources();
+ String[] configValue = res.getStringArray(configResId);
+
+ if (configValue.length == 0) {
+ if (providerValue.length > 0) {
+ return providerValue;
+ }
+
+ configValue = res.getStringArray(defaultResId);
+ }
+
+ return convertStrings(configValue, resourceConverter, Arrays.copyOf(providerValue, 0));
+ }
+
+ /**
+ * Convert a String array to an array of some other type using the specified converter.
+ *
+ * Any null value, or value for which the converter throws a {@link RuntimeException}, will
+ * not be added to the output array, so the output array may be smaller than the input.
+ */
+ private T[] convertStrings(
+ @NonNull String[] strings, Function converter, T[] emptyArray) {
+ final ArrayList convertedValues = new ArrayList<>(strings.length);
+ for (String configString : strings) {
+ T convertedValue = null;
+ try {
+ convertedValue = converter.apply(configString);
+ } catch (Exception e) {
+ Log.e(TAG, "Error parsing configuration", e);
+ // Fall through
+ }
+ if (convertedValue != null) {
+ convertedValues.add(convertedValue);
+ }
+ }
+ return convertedValues.toArray(emptyArray);
+ }
+
private String getCaptivePortalUserAgent() {
return mDependencies.getSetting(mContext,
Settings.Global.CAPTIVE_PORTAL_USER_AGENT, DEFAULT_USER_AGENT);
@@ -1693,19 +1778,6 @@ public class NetworkMonitor extends StateMachine {
return new Random();
}
- /**
- * Get the captive portal server HTTP URL that is configured on the device.
- *
- * NetworkMonitor does not use {@link ConnectivityManager#getCaptivePortalServerUrl()} as
- * it has its own updatable strategies to detect captive portals. The framework only advises
- * on one URL that can be used, while NetworkMonitor may implement more complex logic.
- */
- public String getCaptivePortalServerHttpUrl(Context context) {
- final String defaultUrl =
- context.getResources().getString(R.string.config_captive_portal_http_url);
- return NetworkMonitorUtils.getCaptivePortalServerHttpUrl(context, defaultUrl);
- }
-
/**
* Get the value of a global integer setting.
* @param symbol Name of the setting
diff --git a/packages/NetworkStack/tests/src/com/android/server/connectivity/NetworkMonitorTest.java b/packages/NetworkStack/tests/src/com/android/server/connectivity/NetworkMonitorTest.java
index 6665aae65d90d..1c11586ce59a9 100644
--- a/packages/NetworkStack/tests/src/com/android/server/connectivity/NetworkMonitorTest.java
+++ b/packages/NetworkStack/tests/src/com/android/server/connectivity/NetworkMonitorTest.java
@@ -30,7 +30,6 @@ import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
-import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.any;
import static org.mockito.Mockito.anyInt;
@@ -45,6 +44,7 @@ import static org.mockito.Mockito.when;
import android.annotation.NonNull;
import android.content.Context;
+import android.content.res.Resources;
import android.net.ConnectivityManager;
import android.net.INetworkMonitorCallbacks;
import android.net.InetAddresses;
@@ -95,6 +95,7 @@ public class NetworkMonitorTest {
private static final String LOCATION_HEADER = "location";
private @Mock Context mContext;
+ private @Mock Resources mResources;
private @Mock IpConnectivityLog mLogger;
private @Mock SharedLog mValidationLogger;
private @Mock NetworkInfo mNetworkInfo;
@@ -150,14 +151,20 @@ public class NetworkMonitorTest {
.thenReturn(Settings.Global.CAPTIVE_PORTAL_MODE_PROMPT);
when(mDependencies.getSetting(any(), eq(Settings.Global.CAPTIVE_PORTAL_USE_HTTPS),
anyInt())).thenReturn(1);
- when(mDependencies.getCaptivePortalServerHttpUrl(any())).thenReturn(TEST_HTTP_URL);
- when(mDependencies.getSetting(any(), eq(Settings.Global.CAPTIVE_PORTAL_HTTPS_URL),
- anyString())).thenReturn(TEST_HTTPS_URL);
+ when(mDependencies.getSetting(any(), eq(Settings.Global.CAPTIVE_PORTAL_HTTP_URL), any()))
+ .thenReturn(TEST_HTTP_URL);
+ when(mDependencies.getSetting(any(), eq(Settings.Global.CAPTIVE_PORTAL_HTTPS_URL), any()))
+ .thenReturn(TEST_HTTPS_URL);
+
doReturn(mNetwork).when(mNetwork).getPrivateDnsBypassingCopy();
when(mContext.getSystemService(Context.CONNECTIVITY_SERVICE)).thenReturn(mCm);
when(mContext.getSystemService(Context.TELEPHONY_SERVICE)).thenReturn(mTelephony);
when(mContext.getSystemService(Context.WIFI_SERVICE)).thenReturn(mWifi);
+ when(mContext.getResources()).thenReturn(mResources);
+
+ when(mResources.getString(anyInt())).thenReturn("");
+ when(mResources.getStringArray(anyInt())).thenReturn(new String[0]);
when(mNetworkInfo.getType()).thenReturn(ConnectivityManager.TYPE_WIFI);
setFallbackUrl(TEST_FALLBACK_URL);
diff --git a/services/core/java/com/android/server/ConnectivityService.java b/services/core/java/com/android/server/ConnectivityService.java
index 9d2965afdbca3..e3382c311cb97 100644
--- a/services/core/java/com/android/server/ConnectivityService.java
+++ b/services/core/java/com/android/server/ConnectivityService.java
@@ -108,7 +108,6 @@ import android.net.VpnService;
import android.net.metrics.IpConnectivityLog;
import android.net.metrics.NetworkEvent;
import android.net.netlink.InetDiagMessage;
-import android.net.shared.NetworkMonitorUtils;
import android.net.shared.PrivateDnsConfig;
import android.net.util.MultinetworkPolicyTracker;
import android.net.util.NetdService;
@@ -238,6 +237,16 @@ public class ConnectivityService extends IConnectivityManager.Stub
private static final boolean LOGD_BLOCKED_NETWORKINFO = true;
+ /**
+ * Default URL to use for {@link #getCaptivePortalServerUrl()}. This should not be changed
+ * by OEMs for configuration purposes, as this value is overridden by
+ * Settings.Global.CAPTIVE_PORTAL_HTTP_URL.
+ * R.string.config_networkCaptivePortalServerUrl should be overridden instead for this purpose
+ * (preferably via runtime resource overlays).
+ */
+ private static final String DEFAULT_CAPTIVE_PORTAL_HTTP_URL =
+ "http://connectivitycheck.gstatic.com/generate_204";
+
// TODO: create better separation between radio types and network types
// how long to wait before switching back to a radio's default network
@@ -6712,9 +6721,20 @@ public class ConnectivityService extends IConnectivityManager.Stub
@Override
public String getCaptivePortalServerUrl() {
enforceConnectivityInternalPermission();
- final String defaultUrl = mContext.getResources().getString(
- R.string.config_networkDefaultCaptivePortalServerUrl);
- return NetworkMonitorUtils.getCaptivePortalServerHttpUrl(mContext, defaultUrl);
+ String settingUrl = mContext.getResources().getString(
+ R.string.config_networkCaptivePortalServerUrl);
+
+ if (!TextUtils.isEmpty(settingUrl)) {
+ return settingUrl;
+ }
+
+ settingUrl = Settings.Global.getString(mContext.getContentResolver(),
+ Settings.Global.CAPTIVE_PORTAL_HTTP_URL);
+ if (!TextUtils.isEmpty(settingUrl)) {
+ return settingUrl;
+ }
+
+ return DEFAULT_CAPTIVE_PORTAL_HTTP_URL;
}
@Override
diff --git a/services/net/java/android/net/shared/NetworkMonitorUtils.java b/services/net/java/android/net/shared/NetworkMonitorUtils.java
index a17cb46471586..bb4a603ba4217 100644
--- a/services/net/java/android/net/shared/NetworkMonitorUtils.java
+++ b/services/net/java/android/net/shared/NetworkMonitorUtils.java
@@ -21,9 +21,7 @@ import static android.net.NetworkCapabilities.NET_CAPABILITY_NOT_RESTRICTED;
import static android.net.NetworkCapabilities.NET_CAPABILITY_NOT_VPN;
import static android.net.NetworkCapabilities.NET_CAPABILITY_TRUSTED;
-import android.content.Context;
import android.net.NetworkCapabilities;
-import android.provider.Settings;
/** @hide */
public class NetworkMonitorUtils {
@@ -44,16 +42,6 @@ public class NetworkMonitorUtils {
public static final String PERMISSION_ACCESS_NETWORK_CONDITIONS =
"android.permission.ACCESS_NETWORK_CONDITIONS";
- /**
- * Get the captive portal server HTTP URL that is configured on the device.
- */
- public static String getCaptivePortalServerHttpUrl(Context context, String defaultUrl) {
- final String settingUrl = Settings.Global.getString(
- context.getContentResolver(),
- Settings.Global.CAPTIVE_PORTAL_HTTP_URL);
- return settingUrl != null ? settingUrl : defaultUrl;
- }
-
/**
* Return whether validation is required for a network.
* @param dfltNetCap Default requested network capabilities.