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.lang.Exception
import java.net.InetSocketAddress
import java.net.URL
import javax.net.ssl.HttpsURLConnection
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.
*/
object DoesNetworkHaveInternet {
suspend fun execute(network: Network): Boolean = withContext(Dispatchers.IO) {
try {
Log.d(TAG, "PINGING google.")
val socket = network.socketFactory.createSocket() ?: throw IOException("Socket is null.")
socket.connect(InetSocketAddress("8.8.8.8", 53), 1500)
socket.close()
Log.d(TAG, "PING success.")
val url = URL("https://open.spotify.com/")
val connection = network.openConnection(url) as HttpsURLConnection
connection.connect()
connection.disconnect()
true
} catch (e: Exception) {
e.printStackTrace()

View File

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