From 156feb6e8effa76431bc3c0baf0c588803869ec8 Mon Sep 17 00:00:00 2001 From: FourTOne5 <59261191+FourTOne5@users.noreply.github.com> Date: Mon, 6 Sep 2021 22:31:03 +0600 Subject: [PATCH] Use "isOnline" utils in DownloadService (#5863) * Use isOnline * when -> if/else --- .../data/download/DownloadService.kt | 28 ++++++------------- 1 file changed, 9 insertions(+), 19 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 b505866f4..2e39dfae6 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,8 +4,6 @@ import android.app.Notification import android.app.Service import android.content.Context import android.content.Intent -import android.net.Network -import android.net.NetworkCapabilities import android.os.IBinder import android.os.PowerManager import androidx.annotation.StringRes @@ -17,7 +15,7 @@ import eu.kanade.tachiyomi.data.preference.PreferencesHelper import eu.kanade.tachiyomi.util.lang.plusAssign import eu.kanade.tachiyomi.util.lang.withUIContext import eu.kanade.tachiyomi.util.system.acquireWakeLock -import eu.kanade.tachiyomi.util.system.connectivityManager +import eu.kanade.tachiyomi.util.system.isOnline import eu.kanade.tachiyomi.util.system.isServiceRunning import eu.kanade.tachiyomi.util.system.notification import eu.kanade.tachiyomi.util.system.toast @@ -155,18 +153,15 @@ class DownloadService : Service() { * Called when the network state changes. */ 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() && !wifiManager.isWifiEnabled) { - stopDownloads(R.string.download_notifier_text_only_wifi) + if (isOnline()) { + if (preferences.downloadOnlyOverWifi() && !wifiManager.isWifiEnabled) { + stopDownloads(R.string.download_notifier_text_only_wifi) + } else { + val started = downloadManager.startDownloads() + if (!started) stopSelf() + } } else { - val started = downloadManager.startDownloads() - if (!started) stopSelf() + stopDownloads(R.string.download_notifier_no_network) } } @@ -174,11 +169,6 @@ class DownloadService : Service() { downloadManager.stopDownloads(getString(string)) } - private fun NetworkCapabilities.connectedToInternet(): Boolean { - return this.hasCapability(NetworkCapabilities.NET_CAPABILITY_INTERNET) && - this.hasCapability(NetworkCapabilities.NET_CAPABILITY_VALIDATED) - } - /** * Listens to downloader status. Enables or disables the wake lock depending on the status. */