Skip to content

Commit

Permalink
extract isInternetWalledOnConnectedNonMeteredWifi into seperate function
Browse files Browse the repository at this point in the history
ignore generic exception issues

Signed-off-by: Andy Scherzinger <[email protected]>
  • Loading branch information
AndyScherzinger committed Oct 31, 2023
1 parent 0f092e5 commit 39fd06d
Showing 1 changed file with 27 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -96,6 +97,7 @@ internal class ConnectivityServiceImpl(
}
}

@Suppress("TooGenericExceptionCaught")
private val isNetworkMetered: Boolean
get() {
val network = platformConnectivityManager.activeNetwork
Expand Down

0 comments on commit 39fd06d

Please sign in to comment.