Skip to content

Commit a529608

Browse files
committed
wip
Signed-off-by: alperozturk96 <alper_ozturk@proton.me>
1 parent e353a4c commit a529608

2 files changed

Lines changed: 42 additions & 5 deletions

File tree

app/src/main/java/com/nextcloud/client/network/ConnectivityServiceImpl.kt

Lines changed: 41 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,12 @@ import kotlinx.coroutines.Job
2121
import kotlinx.coroutines.launch
2222
import kotlinx.coroutines.withContext
2323
import org.apache.commons.httpclient.HttpStatus
24+
import java.io.IOException
25+
import java.net.ConnectException
26+
import java.net.NoRouteToHostException
27+
import java.net.SocketTimeoutException
28+
import java.net.UnknownHostException
29+
import javax.net.ssl.SSLException
2430
import kotlin.jvm.functions.Function1
2531

2632
@Suppress("TooGenericExceptionCaught", "ReturnCount")
@@ -39,6 +45,8 @@ class ConnectivityServiceImpl(
3945
@Volatile
4046
private var currentConnectivity: Connectivity = Connectivity.DISCONNECTED
4147

48+
private var notifyJob: Job? = null
49+
4250
private val key: ConnectivityKey
4351
get() = ConnectivityKey.getBy(accountManager)
4452

@@ -51,11 +59,15 @@ class ConnectivityServiceImpl(
5159
}
5260

5361
private fun notifyListeners() {
54-
scope.launch {
62+
if (listeners.isEmpty()) {
63+
return
64+
}
65+
66+
notifyJob?.cancel()
67+
notifyJob = scope.launch {
5568
val available = !isInternetWalled()
5669
withContext(Dispatchers.Main) {
5770
listeners.forEach {
58-
Log_OC.d(TAG, "notifying listeners")
5971
it.networkAndServerConnectionListener(available)
6072
}
6173
}
@@ -207,8 +219,33 @@ class ConnectivityServiceImpl(
207219
override fun getConnectivity() = currentConnectivity
208220

209221
private fun getWalledValueFromException(e: Exception): Boolean {
210-
Log_OC.w(TAG, "exception during server check (${e::class.simpleName}), assuming reachable")
211-
return false
222+
return when (e) {
223+
is UnknownHostException,
224+
is ConnectException -> {
225+
Log_OC.w(TAG, "offline exception (${e::class.simpleName}), treating as walled")
226+
true
227+
}
228+
229+
is SocketTimeoutException -> {
230+
Log_OC.w(TAG, "timeout during server check, treating as walled")
231+
true
232+
}
233+
234+
is SSLException -> {
235+
Log_OC.w(TAG, "SSL exception during server check, assuming reachable")
236+
false
237+
}
238+
239+
is IOException -> {
240+
Log_OC.w(TAG, "I/O exception (${e::class.simpleName}), treating as walled")
241+
true
242+
}
243+
244+
else -> {
245+
Log_OC.e(TAG, "unexpected exception type (${e::class.simpleName}), using previous state")
246+
currentConnectivity.isServerAvailable?.let { !it } ?: true
247+
}
248+
}
212249
}
213250

214251
@Suppress("unused")

app/src/test/java/com/nextcloud/client/network/ConnectivityServiceTest.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -257,7 +257,7 @@ class ConnectivityServiceTest {
257257
@Before
258258
fun setUp() {
259259
connectivityService.updateConnectivity()
260-
Thread.sleep(200)
260+
StandardTestDispatcher().scheduler.advanceUntilIdle()
261261
clearInvocations(requestBuilder, client, getRequest)
262262
connectivityService.connectivity.let {
263263
assertTrue(it.isConnected)

0 commit comments

Comments
 (0)