From 289ada76b0be7b5f744ffc7f00a8c449c0c145a1 Mon Sep 17 00:00:00 2001 From: shabinder Date: Mon, 19 Apr 2021 18:54:21 +0530 Subject: [PATCH] No Internet Connection Fix --- .../common/di/AndroidNetworkObserver.kt | 24 ++++++++----------- .../com/shabinder/common/di/AudioTagging.kt | 1 - .../kotlin/com/shabinder/common/di/Expect.kt | 17 +++++++++++++ .../common/di/providers/SpotifyProvider.kt | 5 ++-- .../com/shabinder/common/di/DesktopActual.kt | 17 +------------ 5 files changed, 30 insertions(+), 34 deletions(-) diff --git a/common/dependency-injection/src/androidMain/kotlin/com/shabinder/common/di/AndroidNetworkObserver.kt b/common/dependency-injection/src/androidMain/kotlin/com/shabinder/common/di/AndroidNetworkObserver.kt index 43935ab7..4dd7c676 100644 --- a/common/dependency-injection/src/androidMain/kotlin/com/shabinder/common/di/AndroidNetworkObserver.kt +++ b/common/dependency-injection/src/androidMain/kotlin/com/shabinder/common/di/AndroidNetworkObserver.kt @@ -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() { 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() { * 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() } } } diff --git a/common/dependency-injection/src/androidMain/kotlin/com/shabinder/common/di/AudioTagging.kt b/common/dependency-injection/src/androidMain/kotlin/com/shabinder/common/di/AudioTagging.kt index 8da03aa3..e133c28d 100644 --- a/common/dependency-injection/src/androidMain/kotlin/com/shabinder/common/di/AudioTagging.kt +++ b/common/dependency-injection/src/androidMain/kotlin/com/shabinder/common/di/AudioTagging.kt @@ -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 diff --git a/common/dependency-injection/src/commonMain/kotlin/com/shabinder/common/di/Expect.kt b/common/dependency-injection/src/commonMain/kotlin/com/shabinder/common/di/Expect.kt index 2001575d..115b2911 100644 --- a/common/dependency-injection/src/commonMain/kotlin/com/shabinder/common/di/Expect.kt +++ b/common/dependency-injection/src/commonMain/kotlin/com/shabinder/common/di/Expect.kt @@ -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("http://google.com") + true + } catch (e: Exception) { + e.printStackTrace() + false + } + } +} diff --git a/common/dependency-injection/src/commonMain/kotlin/com/shabinder/common/di/providers/SpotifyProvider.kt b/common/dependency-injection/src/commonMain/kotlin/com/shabinder/common/di/providers/SpotifyProvider.kt index 475f8a6d..95fd198a 100644 --- a/common/dependency-injection/src/commonMain/kotlin/com/shabinder/common/di/providers/SpotifyProvider.kt +++ b/common/dependency-injection/src/commonMain/kotlin/com/shabinder/common/di/providers/SpotifyProvider.kt @@ -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 } } } diff --git a/common/dependency-injection/src/desktopMain/kotlin/com/shabinder/common/di/DesktopActual.kt b/common/dependency-injection/src/desktopMain/kotlin/com/shabinder/common/di/DesktopActual.kt index 806a2def..86280473 100644 --- a/common/dependency-injection/src/desktopMain/kotlin/com/shabinder/common/di/DesktopActual.kt +++ b/common/dependency-injection/src/desktopMain/kotlin/com/shabinder/common/di/DesktopActual.kt @@ -47,25 +47,10 @@ actual fun giveDonation() { actual fun queryActiveTracks() {} -/* -* Refactor This -* */ -private suspend fun isInternetAvailable(): Boolean { - return withContext(dispatcherIO) { - try { - ktorHttpClient.head("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 }