|
@@ -39,11 +39,14 @@ import kotlin.jvm.functions.Function1;
|
|
|
class ConnectivityServiceImpl implements ConnectivityService {
|
|
|
|
|
|
private static final String TAG = "ConnectivityServiceImpl";
|
|
|
+ private static final String CONNECTIVITY_CHECK_ROUTE = "/index.php/204";
|
|
|
+
|
|
|
private final ConnectivityManager platformConnectivityManager;
|
|
|
private final UserAccountManager accountManager;
|
|
|
private final ClientFactory clientFactory;
|
|
|
private final GetRequestBuilder requestBuilder;
|
|
|
- private final int sdkVersion;
|
|
|
+ private final WalledCheckCache walledCheckCache;
|
|
|
+
|
|
|
|
|
|
static class GetRequestBuilder implements Function1<String, GetMethod> {
|
|
|
@Override
|
|
@@ -56,37 +59,49 @@ class ConnectivityServiceImpl implements ConnectivityService {
|
|
|
UserAccountManager accountManager,
|
|
|
ClientFactory clientFactory,
|
|
|
GetRequestBuilder requestBuilder,
|
|
|
- int sdkVersion) {
|
|
|
+ final WalledCheckCache walledCheckCache) {
|
|
|
this.platformConnectivityManager = platformConnectivityManager;
|
|
|
this.accountManager = accountManager;
|
|
|
this.clientFactory = clientFactory;
|
|
|
this.requestBuilder = requestBuilder;
|
|
|
- this.sdkVersion = sdkVersion;
|
|
|
+ this.walledCheckCache = walledCheckCache;
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
public boolean isInternetWalled() {
|
|
|
- Connectivity c = getConnectivity();
|
|
|
- if (c.isConnected() && c.isWifi() && !c.isMetered()) {
|
|
|
-
|
|
|
- Server server = accountManager.getUser().getServer();
|
|
|
- String baseServerAddress = server.getUri().toString();
|
|
|
- if (baseServerAddress.isEmpty()) {
|
|
|
- return true;
|
|
|
+ final Boolean cachedValue = walledCheckCache.getValue();
|
|
|
+ if (cachedValue != null) {
|
|
|
+ return cachedValue;
|
|
|
+ } else {
|
|
|
+ boolean result;
|
|
|
+ Connectivity c = getConnectivity();
|
|
|
+ if (c.isConnected() && c.isWifi() && !c.isMetered()) {
|
|
|
+
|
|
|
+ Server server = accountManager.getUser().getServer();
|
|
|
+ String baseServerAddress = server.getUri().toString();
|
|
|
+ if (baseServerAddress.isEmpty()) {
|
|
|
+ result = true;
|
|
|
+ } else {
|
|
|
+
|
|
|
+ GetMethod get = requestBuilder.invoke(baseServerAddress + CONNECTIVITY_CHECK_ROUTE);
|
|
|
+ PlainClient client = clientFactory.createPlainClient();
|
|
|
+
|
|
|
+ int status = get.execute(client);
|
|
|
+
|
|
|
+ // Content-Length is not available when using chunked transfer encoding, so check for -1 as well
|
|
|
+ result = !(status == HttpStatus.SC_NO_CONTENT && get.getResponseContentLength() <= 0);
|
|
|
+ get.releaseConnection();
|
|
|
+ if (result) {
|
|
|
+ Log_OC.w(TAG, "isInternetWalled(): Failed to GET " + CONNECTIVITY_CHECK_ROUTE + "," +
|
|
|
+ " assuming connectivity is impaired");
|
|
|
+ }
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ result = !c.isConnected();
|
|
|
}
|
|
|
|
|
|
- GetMethod get = requestBuilder.invoke(baseServerAddress + "/index.php/204");
|
|
|
- PlainClient client = clientFactory.createPlainClient();
|
|
|
-
|
|
|
- int status = get.execute(client);
|
|
|
-
|
|
|
- // Content-Length is not available when using chunked transfer encoding, so check for -1 as well
|
|
|
- boolean result = !(status == HttpStatus.SC_NO_CONTENT && get.getResponseContentLength() <= 0);
|
|
|
- get.releaseConnection();
|
|
|
-
|
|
|
+ walledCheckCache.setValue(result);
|
|
|
return result;
|
|
|
- } else {
|
|
|
- return !c.isConnected();
|
|
|
}
|
|
|
}
|
|
|
|