mirror of
https://github.com/tachiyomiorg/tachiyomi.git
synced 2024-11-05 18:45:07 +01:00
Replace NetworkInfo with NetworkCapabilities (#5785)
This commit is contained in:
parent
24fd82d773
commit
6922394b8e
@ -4,13 +4,12 @@ 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.ConnectivityManager
|
import android.net.Network
|
||||||
import android.net.NetworkInfo.State.CONNECTED
|
import android.net.NetworkCapabilities
|
||||||
import android.net.NetworkInfo.State.DISCONNECTED
|
|
||||||
import android.os.IBinder
|
import android.os.IBinder
|
||||||
import android.os.PowerManager
|
import android.os.PowerManager
|
||||||
|
import androidx.annotation.StringRes
|
||||||
import androidx.core.content.ContextCompat
|
import androidx.core.content.ContextCompat
|
||||||
import com.github.pwittchen.reactivenetwork.library.Connectivity
|
|
||||||
import com.github.pwittchen.reactivenetwork.library.ReactiveNetwork
|
import com.github.pwittchen.reactivenetwork.library.ReactiveNetwork
|
||||||
import com.jakewharton.rxrelay.BehaviorRelay
|
import com.jakewharton.rxrelay.BehaviorRelay
|
||||||
import eu.kanade.tachiyomi.R
|
import eu.kanade.tachiyomi.R
|
||||||
@ -133,8 +132,8 @@ class DownloadService : Service() {
|
|||||||
.subscribeOn(Schedulers.io())
|
.subscribeOn(Schedulers.io())
|
||||||
.observeOn(AndroidSchedulers.mainThread())
|
.observeOn(AndroidSchedulers.mainThread())
|
||||||
.subscribe(
|
.subscribe(
|
||||||
{ state ->
|
{
|
||||||
onNetworkStateChanged(state)
|
onNetworkStateChanged()
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
toast(R.string.download_queue_error)
|
toast(R.string.download_queue_error)
|
||||||
@ -145,26 +144,34 @@ class DownloadService : Service() {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Called when the network state changes.
|
* Called when the network state changes.
|
||||||
*
|
|
||||||
* @param connectivity the new network state.
|
|
||||||
*/
|
*/
|
||||||
private fun onNetworkStateChanged(connectivity: Connectivity) {
|
private fun onNetworkStateChanged() {
|
||||||
when (connectivity.state) {
|
val manager = connectivityManager
|
||||||
CONNECTED -> {
|
val activeNetwork: Network = manager.activeNetwork ?: return
|
||||||
if (preferences.downloadOnlyOverWifi() && connectivityManager.activeNetworkInfo?.type != ConnectivityManager.TYPE_WIFI) {
|
val networkCapabilities = manager.getNetworkCapabilities(activeNetwork) ?: return
|
||||||
downloadManager.stopDownloads(getString(R.string.download_notifier_text_only_wifi))
|
if (!networkCapabilities.connectedToInternet()) {
|
||||||
} else {
|
return stopDownloads(R.string.download_notifier_no_network)
|
||||||
val started = downloadManager.startDownloads()
|
|
||||||
if (!started) stopSelf()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
DISCONNECTED -> {
|
|
||||||
downloadManager.stopDownloads(getString(R.string.download_notifier_no_network))
|
|
||||||
}
|
|
||||||
else -> {
|
|
||||||
/* Do nothing */
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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)
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
Loading…
Reference in New Issue
Block a user