Replace pinging google with spotify

This commit is contained in:
Shabinder Singh 2021-05-29 03:21:11 +05:30
parent 48e4c04973
commit ae96e37ba2
2 changed files with 8 additions and 7 deletions

View File

@ -31,6 +31,8 @@ import kotlinx.coroutines.withContext
import java.io.IOException import java.io.IOException
import java.lang.Exception import java.lang.Exception
import java.net.InetSocketAddress import java.net.InetSocketAddress
import java.net.URL
import javax.net.ssl.HttpsURLConnection
const val TAG = "C-Manager" const val TAG = "C-Manager"
@ -103,17 +105,16 @@ class ConnectionLiveData(context: Context) : LiveData<Boolean>() {
} }
/** /**
* Send a ping to googles primary DNS. * Try Establishing an Actual Internet Connection.
* If successful, that means we have internet. * If successful, that means we have internet.
*/ */
object DoesNetworkHaveInternet { object DoesNetworkHaveInternet {
suspend fun execute(network: Network): Boolean = withContext(Dispatchers.IO) { suspend fun execute(network: Network): Boolean = withContext(Dispatchers.IO) {
try { try {
Log.d(TAG, "PINGING google.") val url = URL("https://open.spotify.com/")
val socket = network.socketFactory.createSocket() ?: throw IOException("Socket is null.") val connection = network.openConnection(url) as HttpsURLConnection
socket.connect(InetSocketAddress("8.8.8.8", 53), 1500) connection.connect()
socket.close() connection.disconnect()
Log.d(TAG, "PING success.")
true true
} catch (e: Exception) { } catch (e: Exception) {
e.printStackTrace() e.printStackTrace()

View File

@ -41,7 +41,7 @@ expect val currentPlatform: AllPlatforms
suspend fun isInternetAccessible(): Boolean { suspend fun isInternetAccessible(): Boolean {
return withContext(dispatcherIO) { return withContext(dispatcherIO) {
try { try {
ktorHttpClient.head<String>("https://google.com") ktorHttpClient.head<String>("https://open.spotify.com/")
true true
} catch (e: Exception) { } catch (e: Exception) {
e.printStackTrace() e.printStackTrace()