From afd0b42c83aeacc397ca5c9ca4390ab2d03fdd12 Mon Sep 17 00:00:00 2001 From: dece Date: Sun, 9 Jan 2022 22:19:33 +0100 Subject: [PATCH] PageViewModel: clearer message if no connection --- app/src/main/AndroidManifest.xml | 1 + .../java/dev/lowrespalmtree/comet/MainActivity.kt | 4 +++- .../main/java/dev/lowrespalmtree/comet/NetUtils.kt | 11 +++++++++++ 3 files changed, 15 insertions(+), 1 deletion(-) create mode 100644 app/src/main/java/dev/lowrespalmtree/comet/NetUtils.kt diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index 5b3a148..0ea7f5e 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -2,6 +2,7 @@ + { var message = event.details if (!event.serverDetails.isNullOrEmpty()) - message += "\n\n" + "Server details: ${event.serverDetails}" + message += "\n\nServer details: ${event.serverDetails}" + if (!isConnectedToNetwork(this)) + message += "\n\nInternet may be inaccessible…" alert(message, title = event.short) updateState(PageViewModel.State.IDLE) } diff --git a/app/src/main/java/dev/lowrespalmtree/comet/NetUtils.kt b/app/src/main/java/dev/lowrespalmtree/comet/NetUtils.kt new file mode 100644 index 0000000..5dd12fa --- /dev/null +++ b/app/src/main/java/dev/lowrespalmtree/comet/NetUtils.kt @@ -0,0 +1,11 @@ +package dev.lowrespalmtree.comet + +import android.content.Context +import android.net.ConnectivityManager +import android.net.NetworkCapabilities + +fun isConnectedToNetwork(context: Context): Boolean { + val connManager = context.getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager + val caps = connManager.getNetworkCapabilities(connManager.activeNetwork) ?: return false + return caps.hasCapability(NetworkCapabilities.NET_CAPABILITY_VALIDATED) +}