Use "isOnline" utils in DownloadService (#5863)

* Use isOnline

* when -> if/else
This commit is contained in:
FourTOne5 2021-09-06 22:31:03 +06:00 committed by GitHub
parent e942b8a402
commit 156feb6e8e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -4,8 +4,6 @@ import android.app.Notification
import android.app.Service import android.app.Service
import android.content.Context import android.content.Context
import android.content.Intent import android.content.Intent
import android.net.Network
import android.net.NetworkCapabilities
import android.os.IBinder import android.os.IBinder
import android.os.PowerManager import android.os.PowerManager
import androidx.annotation.StringRes 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.plusAssign
import eu.kanade.tachiyomi.util.lang.withUIContext import eu.kanade.tachiyomi.util.lang.withUIContext
import eu.kanade.tachiyomi.util.system.acquireWakeLock 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.isServiceRunning
import eu.kanade.tachiyomi.util.system.notification import eu.kanade.tachiyomi.util.system.notification
import eu.kanade.tachiyomi.util.system.toast import eu.kanade.tachiyomi.util.system.toast
@ -155,30 +153,22 @@ class DownloadService : Service() {
* Called when the network state changes. * Called when the network state changes.
*/ */
private fun onNetworkStateChanged() { private fun onNetworkStateChanged() {
val manager = connectivityManager if (isOnline()) {
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) { if (preferences.downloadOnlyOverWifi() && !wifiManager.isWifiEnabled) {
stopDownloads(R.string.download_notifier_text_only_wifi) stopDownloads(R.string.download_notifier_text_only_wifi)
} else { } else {
val started = downloadManager.startDownloads() val started = downloadManager.startDownloads()
if (!started) stopSelf() if (!started) stopSelf()
} }
} else {
stopDownloads(R.string.download_notifier_no_network)
}
} }
private fun stopDownloads(@StringRes string: Int) { private fun stopDownloads(@StringRes string: Int) {
downloadManager.stopDownloads(getString(string)) 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. * Listens to downloader status. Enables or disables the wake lock depending on the status.
*/ */