mirror of
https://github.com/Shabinder/SpotiFlyer.git
synced 2024-12-22 20:57:54 +01:00
No Internet Connection Fix
This commit is contained in:
parent
e2de7e9f6a
commit
289ada76b0
@ -27,13 +27,10 @@ import androidx.compose.runtime.Composable
|
||||
import androidx.compose.runtime.State
|
||||
import androidx.lifecycle.LiveData
|
||||
import com.shabinder.common.database.appContext
|
||||
import kotlinx.coroutines.CoroutineScope
|
||||
import kotlinx.coroutines.Dispatchers
|
||||
import kotlinx.coroutines.launch
|
||||
import kotlinx.coroutines.withContext
|
||||
import kotlinx.coroutines.*
|
||||
import java.io.IOException
|
||||
import java.lang.Exception
|
||||
import java.net.InetSocketAddress
|
||||
import javax.net.SocketFactory
|
||||
|
||||
const val TAG = "C-Manager"
|
||||
|
||||
@ -89,7 +86,7 @@ class ConnectionLiveData(context: Context = appContext) : LiveData<Boolean>() {
|
||||
if (hasInternetCapability == true) {
|
||||
// check if this network actually has internet
|
||||
CoroutineScope(Dispatchers.IO).launch {
|
||||
val hasInternet = DoesNetworkHaveInternet.execute(network.socketFactory)
|
||||
val hasInternet = DoesNetworkHaveInternet.execute(network)
|
||||
if (hasInternet) {
|
||||
withContext(Dispatchers.Main) {
|
||||
Log.d(TAG, "onAvailable: adding network. $network")
|
||||
@ -117,19 +114,18 @@ class ConnectionLiveData(context: Context = appContext) : LiveData<Boolean>() {
|
||||
* If successful, that means we have internet.
|
||||
*/
|
||||
object DoesNetworkHaveInternet {
|
||||
|
||||
// Make sure to execute this on a background thread.
|
||||
fun execute(socketFactory: SocketFactory): Boolean {
|
||||
return try {
|
||||
suspend fun execute(network: Network): Boolean = withContext(Dispatchers.IO) {
|
||||
try {
|
||||
Log.d(TAG, "PINGING google.")
|
||||
val socket = socketFactory.createSocket() ?: throw IOException("Socket is null.")
|
||||
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.")
|
||||
true
|
||||
} catch (e: IOException) {
|
||||
Log.e(TAG, "No internet connection. $e")
|
||||
false
|
||||
}catch (e:Exception){
|
||||
e.printStackTrace()
|
||||
// Handle VPN Connection / Google DNS Blocked Cases
|
||||
isInternetAccessible()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -18,7 +18,6 @@ package com.shabinder.common.di
|
||||
|
||||
import com.shabinder.common.models.TrackDetails
|
||||
import java.io.File
|
||||
import com.mp3.jaudiotagger.tag.images.ArtworkFactory
|
||||
import com.mpatric.mp3agic.ID3v1Tag
|
||||
import com.mpatric.mp3agic.ID3v24Tag
|
||||
import com.mpatric.mp3agic.Mp3File
|
||||
|
@ -18,7 +18,9 @@ package com.shabinder.common.di
|
||||
|
||||
import com.shabinder.common.models.AllPlatforms
|
||||
import com.shabinder.common.models.TrackDetails
|
||||
import io.ktor.client.request.*
|
||||
import kotlinx.coroutines.CoroutineDispatcher
|
||||
import kotlinx.coroutines.withContext
|
||||
|
||||
expect fun openPlatform(packageID: String, platformLink: String)
|
||||
|
||||
@ -39,3 +41,18 @@ expect suspend fun downloadTracks(
|
||||
)
|
||||
|
||||
expect fun queryActiveTracks()
|
||||
|
||||
/*
|
||||
* Refactor This
|
||||
* */
|
||||
suspend fun isInternetAccessible(): Boolean {
|
||||
return withContext(dispatcherIO) {
|
||||
try {
|
||||
ktorHttpClient.head<String>("http://google.com")
|
||||
true
|
||||
} catch (e: Exception) {
|
||||
e.printStackTrace()
|
||||
false
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -61,15 +61,14 @@ class SpotifyProvider(
|
||||
null
|
||||
} else {
|
||||
logger.d { "Spotify Provider Created with $token" }
|
||||
httpClient = HttpClient {
|
||||
HttpClient {
|
||||
defaultRequest {
|
||||
header("Authorization", "Bearer ${token.access_token}")
|
||||
}
|
||||
install(JsonFeature) {
|
||||
serializer = kotlinxSerializer
|
||||
}
|
||||
}
|
||||
httpClient
|
||||
}?.also { httpClient = it }
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -47,25 +47,10 @@ actual fun giveDonation() {
|
||||
|
||||
actual fun queryActiveTracks() {}
|
||||
|
||||
/*
|
||||
* Refactor This
|
||||
* */
|
||||
private suspend fun isInternetAvailable(): Boolean {
|
||||
return withContext(dispatcherIO) {
|
||||
try {
|
||||
ktorHttpClient.head<String>("http://google.com")
|
||||
true
|
||||
} catch (e: Exception) {
|
||||
println(e.message)
|
||||
false
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
actual val isInternetAvailable: Boolean
|
||||
get() {
|
||||
var result = false
|
||||
val job = GlobalScope.launch { result = isInternetAvailable() }
|
||||
val job = GlobalScope.launch { result = isInternetAccessible() }
|
||||
while (job.isActive) {}
|
||||
return result
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user