Use "isOnline" utils in DownloadService (#5863)
* Use isOnline * when -> if/else
This commit is contained in:
parent
e942b8a402
commit
156feb6e8e
@ -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,30 +153,22 @@ 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 (isOnline()) {
|
||||
if (preferences.downloadOnlyOverWifi() && !wifiManager.isWifiEnabled) {
|
||||
stopDownloads(R.string.download_notifier_text_only_wifi)
|
||||
} else {
|
||||
val started = downloadManager.startDownloads()
|
||||
if (!started) stopSelf()
|
||||
}
|
||||
} else {
|
||||
stopDownloads(R.string.download_notifier_no_network)
|
||||
}
|
||||
}
|
||||
|
||||
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)
|
||||
}
|
||||
|
||||
/**
|
||||
* Listens to downloader status. Enables or disables the wake lock depending on the status.
|
||||
*/
|
||||
|
Loading…
Reference in New Issue
Block a user