From 6922394b8ec1a886f6552b86b6d5dc9504f8de10 Mon Sep 17 00:00:00 2001 From: Taco Date: Thu, 26 Aug 2021 22:09:40 -0400 Subject: [PATCH] Replace NetworkInfo with NetworkCapabilities (#5785) --- .../data/download/DownloadService.kt | 55 +++++++++++-------- 1 file changed, 31 insertions(+), 24 deletions(-) diff --git a/app/src/main/java/eu/kanade/tachiyomi/data/download/DownloadService.kt b/app/src/main/java/eu/kanade/tachiyomi/data/download/DownloadService.kt index 46daf5ce1..8cbd3da89 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/data/download/DownloadService.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/data/download/DownloadService.kt @@ -4,13 +4,12 @@ import android.app.Notification import android.app.Service import android.content.Context import android.content.Intent -import android.net.ConnectivityManager -import android.net.NetworkInfo.State.CONNECTED -import android.net.NetworkInfo.State.DISCONNECTED +import android.net.Network +import android.net.NetworkCapabilities import android.os.IBinder import android.os.PowerManager +import androidx.annotation.StringRes import androidx.core.content.ContextCompat -import com.github.pwittchen.reactivenetwork.library.Connectivity import com.github.pwittchen.reactivenetwork.library.ReactiveNetwork import com.jakewharton.rxrelay.BehaviorRelay import eu.kanade.tachiyomi.R @@ -133,8 +132,8 @@ class DownloadService : Service() { .subscribeOn(Schedulers.io()) .observeOn(AndroidSchedulers.mainThread()) .subscribe( - { state -> - onNetworkStateChanged(state) + { + onNetworkStateChanged() }, { toast(R.string.download_queue_error) @@ -145,26 +144,34 @@ class DownloadService : Service() { /** * Called when the network state changes. - * - * @param connectivity the new network state. */ - private fun onNetworkStateChanged(connectivity: Connectivity) { - when (connectivity.state) { - CONNECTED -> { - if (preferences.downloadOnlyOverWifi() && connectivityManager.activeNetworkInfo?.type != ConnectivityManager.TYPE_WIFI) { - downloadManager.stopDownloads(getString(R.string.download_notifier_text_only_wifi)) - } else { - val started = downloadManager.startDownloads() - if (!started) stopSelf() - } - } - DISCONNECTED -> { - downloadManager.stopDownloads(getString(R.string.download_notifier_no_network)) - } - else -> { - /* Do nothing */ - } + private fun onNetworkStateChanged() { + val manager = connectivityManager + val activeNetwork: Network = manager.activeNetwork ?: return + val networkCapabilities = manager.getNetworkCapabilities(activeNetwork) ?: return + if (!networkCapabilities.connectedToInternet()) { + return stopDownloads(R.string.download_notifier_no_network) } + + if (preferences.downloadOnlyOverWifi() && !networkCapabilities.connectedToWifi()) { + stopDownloads(R.string.download_notifier_text_only_wifi) + } else { + val started = downloadManager.startDownloads() + if (!started) stopSelf() + } + } + + private fun stopDownloads(@StringRes string: Int) { + downloadManager.stopDownloads(getString(string)) + } + + private fun NetworkCapabilities.connectedToInternet(): Boolean { + return this.hasCapability(NetworkCapabilities.NET_CAPABILITY_INTERNET) && + this.hasCapability(NetworkCapabilities.NET_CAPABILITY_VALIDATED) + } + + private fun NetworkCapabilities.connectedToWifi(): Boolean { + return this.hasTransport(NetworkCapabilities.TRANSPORT_WIFI) } /**