浏览代码

Use "isOnline" utils in DownloadService (#5863)

* Use isOnline

* when -> if/else
FourTOne5 3 年之前
父节点
当前提交
156feb6e8e
共有 1 个文件被更改,包括 9 次插入19 次删除
  1. 9 19
      app/src/main/java/eu/kanade/tachiyomi/data/download/DownloadService.kt

+ 9 - 19
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.
      */