@@ -21,6 +21,12 @@ import kotlinx.coroutines.Job
2121import kotlinx.coroutines.launch
2222import kotlinx.coroutines.withContext
2323import 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
2430import 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" )
0 commit comments