From 39fd06d29b2ef712e1031d243fe88419f2b32b60 Mon Sep 17 00:00:00 2001 From: Andy Scherzinger Date: Mon, 30 Oct 2023 19:14:58 +0100 Subject: [PATCH] extract isInternetWalledOnConnectedNonMeteredWifi into seperate function ignore generic exception issues Signed-off-by: Andy Scherzinger --- .../client/network/ConnectivityServiceImpl.kt | 52 ++++++++++--------- 1 file changed, 27 insertions(+), 25 deletions(-) diff --git a/app/src/main/java/com/nextcloud/client/network/ConnectivityServiceImpl.kt b/app/src/main/java/com/nextcloud/client/network/ConnectivityServiceImpl.kt index e7f0699522a9..3ffa304cf52c 100644 --- a/app/src/main/java/com/nextcloud/client/network/ConnectivityServiceImpl.kt +++ b/app/src/main/java/com/nextcloud/client/network/ConnectivityServiceImpl.kt @@ -46,39 +46,40 @@ internal class ConnectivityServiceImpl( return if (cachedValue != null) { cachedValue } else { - val result: Boolean val (isConnected, isMetered, isWifi) = getConnectivity() - if (isConnected && isWifi && !isMetered) { - val (uri) = accountManager.user.server - val baseServerAddress = uri.toString() - if (baseServerAddress.isEmpty()) { - result = true - } else { - val get = - requestBuilder.invoke(baseServerAddress + CONNECTIVITY_CHECK_ROUTE) - val client = clientFactory.createPlainClient() - val 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" - ) - } - } + val result: Boolean = if (isConnected && isWifi && !isMetered) { + isInternetWalledOnConnectedNonMeteredWifi() } else { - result = !isConnected + !isConnected } walledCheckCache.setValue(result) result } } + private fun isInternetWalledOnConnectedNonMeteredWifi(): Boolean { + val baseServerAddress = accountManager.user.server.toString() + return if (baseServerAddress.isEmpty()) { + true + } else { + val get = requestBuilder.invoke(baseServerAddress + CONNECTIVITY_CHECK_ROUTE) + val client = clientFactory.createPlainClient() + val status = get.execute(client) + + // Content-Length is not available when using chunked transfer encoding, so check for -1 as well + val 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" + ) + } + result + } + } + + @Suppress("TooGenericExceptionCaught") override fun getConnectivity(): Connectivity { val networkInfo: NetworkInfo? = try { platformConnectivityManager.activeNetworkInfo @@ -96,6 +97,7 @@ internal class ConnectivityServiceImpl( } } + @Suppress("TooGenericExceptionCaught") private val isNetworkMetered: Boolean get() { val network = platformConnectivityManager.activeNetwork