diff --git a/app/build.gradle b/app/build.gradle index 04db1e37f..63625098b 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -95,9 +95,9 @@ dependencies { implementation 'androidx.activity:activity-ktx:1.9.0' implementation 'androidx.fragment:fragment-ktx:1.7.1' implementation 'androidx.collection:collection-ktx:1.4.0' - implementation 'androidx.lifecycle:lifecycle-viewmodel-ktx:2.7.0' - implementation 'androidx.lifecycle:lifecycle-service:2.7.0' - implementation 'androidx.lifecycle:lifecycle-process:2.7.0' + implementation 'androidx.lifecycle:lifecycle-viewmodel-ktx:2.8.0' + implementation 'androidx.lifecycle:lifecycle-service:2.8.0' + implementation 'androidx.lifecycle:lifecycle-process:2.8.0' implementation 'androidx.constraintlayout:constraintlayout:2.1.4' implementation 'androidx.swiperefreshlayout:swiperefreshlayout:1.1.0' implementation 'androidx.recyclerview:recyclerview:1.3.2' @@ -105,7 +105,7 @@ dependencies { implementation 'androidx.preference:preference-ktx:1.2.1' implementation 'androidx.biometric:biometric-ktx:1.2.0-alpha05' implementation 'com.google.android.material:material:1.12.0' - implementation 'androidx.lifecycle:lifecycle-common-java8:2.7.0' + implementation 'androidx.lifecycle:lifecycle-common-java8:2.8.0' implementation 'androidx.webkit:webkit:1.11.0' implementation 'androidx.work:work-runtime:2.9.0' diff --git a/app/src/main/kotlin/org/koitharu/kotatsu/core/AppModule.kt b/app/src/main/kotlin/org/koitharu/kotatsu/core/AppModule.kt index f5525f1ce..0c38a0f50 100644 --- a/app/src/main/kotlin/org/koitharu/kotatsu/core/AppModule.kt +++ b/app/src/main/kotlin/org/koitharu/kotatsu/core/AppModule.kt @@ -34,6 +34,7 @@ import org.koitharu.kotatsu.core.os.NetworkState import org.koitharu.kotatsu.core.parser.MangaLoaderContextImpl import org.koitharu.kotatsu.core.parser.MangaRepository import org.koitharu.kotatsu.core.parser.favicon.FaviconFetcher +import org.koitharu.kotatsu.core.prefs.AppSettings import org.koitharu.kotatsu.core.ui.image.CoilImageGetter import org.koitharu.kotatsu.core.ui.util.ActivityRecreationHandle import org.koitharu.kotatsu.core.util.AcraScreenLogger @@ -70,8 +71,9 @@ interface AppModule { @Provides @Singleton fun provideNetworkState( - @ApplicationContext context: Context - ) = NetworkState(context.connectivityManager) + @ApplicationContext context: Context, + settings: AppSettings, + ) = NetworkState(context.connectivityManager, settings) @Provides @Singleton diff --git a/app/src/main/kotlin/org/koitharu/kotatsu/core/os/NetworkState.kt b/app/src/main/kotlin/org/koitharu/kotatsu/core/os/NetworkState.kt index 9f3224c65..338818b5c 100644 --- a/app/src/main/kotlin/org/koitharu/kotatsu/core/os/NetworkState.kt +++ b/app/src/main/kotlin/org/koitharu/kotatsu/core/os/NetworkState.kt @@ -5,13 +5,15 @@ import android.net.ConnectivityManager.NetworkCallback import android.net.Network import android.net.NetworkCapabilities import android.net.NetworkRequest +import android.os.Build import kotlinx.coroutines.flow.first +import org.koitharu.kotatsu.core.prefs.AppSettings import org.koitharu.kotatsu.core.util.MediatorStateFlow -import org.koitharu.kotatsu.core.util.ext.isOnline class NetworkState( private val connectivityManager: ConnectivityManager, -) : MediatorStateFlow(connectivityManager.isOnline()) { + private val settings: AppSettings, +) : MediatorStateFlow(connectivityManager.isOnline(settings)) { private val callback = NetworkCallbackImpl() @@ -22,6 +24,7 @@ class NetworkState( .addTransportType(NetworkCapabilities.TRANSPORT_WIFI) .addTransportType(NetworkCapabilities.TRANSPORT_CELLULAR) .addTransportType(NetworkCapabilities.TRANSPORT_ETHERNET) + .addTransportType(NetworkCapabilities.TRANSPORT_VPN) .build() connectivityManager.registerNetworkCallback(request, callback) } @@ -39,7 +42,7 @@ class NetworkState( } private fun invalidate() { - publishValue(connectivityManager.isOnline()) + publishValue(connectivityManager.isOnline(settings)) } private inner class NetworkCallbackImpl : NetworkCallback() { @@ -50,4 +53,27 @@ class NetworkState( override fun onUnavailable() = invalidate() } + + private companion object { + + fun ConnectivityManager.isOnline(settings: AppSettings): Boolean { + if (settings.isOfflineCheckDisabled) { + return true + } + return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { + activeNetwork?.let { isOnline(it) } ?: false + } else { + @Suppress("DEPRECATION") + activeNetworkInfo?.isConnected == true + } + } + + private fun ConnectivityManager.isOnline(network: Network): Boolean { + val capabilities = getNetworkCapabilities(network) ?: return false + return capabilities.hasTransport(NetworkCapabilities.TRANSPORT_WIFI) + || capabilities.hasTransport(NetworkCapabilities.TRANSPORT_CELLULAR) + || capabilities.hasTransport(NetworkCapabilities.TRANSPORT_ETHERNET) + || capabilities.hasTransport(NetworkCapabilities.TRANSPORT_VPN) + } + } } diff --git a/app/src/main/kotlin/org/koitharu/kotatsu/core/prefs/AppSettings.kt b/app/src/main/kotlin/org/koitharu/kotatsu/core/prefs/AppSettings.kt index 516bce3e4..94cda77b9 100644 --- a/app/src/main/kotlin/org/koitharu/kotatsu/core/prefs/AppSettings.kt +++ b/app/src/main/kotlin/org/koitharu/kotatsu/core/prefs/AppSettings.kt @@ -136,6 +136,9 @@ class AppSettings @Inject constructor(@ApplicationContext context: Context) { get() = prefs.getBoolean(KEY_TRAFFIC_WARNING, true) set(value) = prefs.edit { putBoolean(KEY_TRAFFIC_WARNING, value) } + val isOfflineCheckDisabled: Boolean + get() = prefs.getBoolean(KEY_OFFLINE_DISABLED, false) + var isAllFavouritesVisible: Boolean get() = prefs.getBoolean(KEY_ALL_FAVOURITES_VISIBLE, true) set(value) = prefs.edit { putBoolean(KEY_ALL_FAVOURITES_VISIBLE, value) } @@ -557,6 +560,7 @@ class AppSettings @Inject constructor(@ApplicationContext context: Context) { const val KEY_COLOR_THEME = "color_theme" const val KEY_THEME_AMOLED = "amoled_theme" const val KEY_TRAFFIC_WARNING = "traffic_warning" + const val KEY_OFFLINE_DISABLED = "no_offline" const val KEY_PAGES_CACHE_CLEAR = "pages_cache_clear" const val KEY_HTTP_CACHE_CLEAR = "http_cache_clear" const val KEY_COOKIES_CLEAR = "cookies_clear" diff --git a/app/src/main/kotlin/org/koitharu/kotatsu/core/util/ext/Android.kt b/app/src/main/kotlin/org/koitharu/kotatsu/core/util/ext/Android.kt index c4cfacc82..b365a3cd3 100644 --- a/app/src/main/kotlin/org/koitharu/kotatsu/core/util/ext/Android.kt +++ b/app/src/main/kotlin/org/koitharu/kotatsu/core/util/ext/Android.kt @@ -19,6 +19,7 @@ import android.content.pm.ResolveInfo import android.database.SQLException import android.graphics.Bitmap import android.graphics.Color +import android.net.ConnectivityManager import android.net.Uri import android.os.Build import android.os.Bundle @@ -75,6 +76,9 @@ val Context.activityManager: ActivityManager? val Context.powerManager: PowerManager? get() = getSystemService(POWER_SERVICE) as? PowerManager +val Context.connectivityManager: ConnectivityManager + get() = getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager + fun String.toUriOrNull() = if (isEmpty()) null else Uri.parse(this) suspend fun CoroutineWorker.trySetForeground(): Boolean = runCatchingCancellable { diff --git a/app/src/main/kotlin/org/koitharu/kotatsu/core/util/ext/Network.kt b/app/src/main/kotlin/org/koitharu/kotatsu/core/util/ext/Network.kt deleted file mode 100644 index eac104dc6..000000000 --- a/app/src/main/kotlin/org/koitharu/kotatsu/core/util/ext/Network.kt +++ /dev/null @@ -1,26 +0,0 @@ -package org.koitharu.kotatsu.core.util.ext - -import android.content.Context -import android.net.ConnectivityManager -import android.net.Network -import android.net.NetworkCapabilities -import android.os.Build - -val Context.connectivityManager: ConnectivityManager - get() = getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager - -fun ConnectivityManager.isOnline(): Boolean { - return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { - activeNetwork?.let { isOnline(it) } ?: false - } else { - @Suppress("DEPRECATION") - activeNetworkInfo?.isConnected == true - } -} - -private fun ConnectivityManager.isOnline(network: Network): Boolean { - val capabilities = getNetworkCapabilities(network) ?: return false - return capabilities.hasTransport(NetworkCapabilities.TRANSPORT_WIFI) - || capabilities.hasTransport(NetworkCapabilities.TRANSPORT_CELLULAR) - || capabilities.hasTransport(NetworkCapabilities.TRANSPORT_ETHERNET) -} diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 269873af7..e4a333a11 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -643,4 +643,5 @@ You are blocked by the server. Try to use a different network connection (VPN, Proxy, etc.) Disable Sources disabled + Disable connectivity check diff --git a/app/src/main/res/xml/pref_network.xml b/app/src/main/res/xml/pref_network.xml index af781034b..dd42ab93c 100644 --- a/app/src/main/res/xml/pref_network.xml +++ b/app/src/main/res/xml/pref_network.xml @@ -49,4 +49,8 @@ android:key="ssl_bypass" android:title="@string/ignore_ssl_errors" /> + +