Fix network capabilities

This commit is contained in:
vvb2060 2022-12-09 22:20:44 +08:00 committed by John Wu
parent f989756b93
commit a4a734458b
2 changed files with 9 additions and 9 deletions

View File

@ -1,14 +1,13 @@
package com.topjohnwu.magisk.core.utils.net package com.topjohnwu.magisk.core.utils.net
import android.annotation.TargetApi
import android.content.Context import android.content.Context
import android.net.ConnectivityManager import android.net.ConnectivityManager
import android.net.Network import android.net.Network
import android.net.NetworkCapabilities import android.net.NetworkCapabilities
import android.net.NetworkRequest import android.net.NetworkRequest
import android.os.Build
import androidx.collection.ArraySet import androidx.collection.ArraySet
@TargetApi(21)
open class LollipopNetworkObserver( open class LollipopNetworkObserver(
context: Context, context: Context,
callback: ConnectionCallback callback: ConnectionCallback
@ -17,10 +16,13 @@ open class LollipopNetworkObserver(
private val networkCallback = NetCallback() private val networkCallback = NetCallback()
init { init {
val request = NetworkRequest.Builder() val builder = NetworkRequest.Builder()
.addCapability(NetworkCapabilities.NET_CAPABILITY_INTERNET) if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
.build() builder.addCapability(NetworkCapabilities.NET_CAPABILITY_VALIDATED)
manager.registerNetworkCallback(request, networkCallback) } else {
builder.addCapability(NetworkCapabilities.NET_CAPABILITY_INTERNET)
}
manager.registerNetworkCallback(builder.build(), networkCallback)
} }
@Suppress("DEPRECATION") @Suppress("DEPRECATION")

View File

@ -1,5 +1,3 @@
@file:Suppress("DEPRECATION")
package com.topjohnwu.magisk.core.utils.net package com.topjohnwu.magisk.core.utils.net
import android.annotation.TargetApi import android.annotation.TargetApi
@ -32,7 +30,7 @@ class MarshmallowNetworkObserver(
override fun getCurrentState() { override fun getCurrentState() {
callback(manager.getNetworkCapabilities(manager.activeNetwork) callback(manager.getNetworkCapabilities(manager.activeNetwork)
?.hasCapability(NetworkCapabilities.NET_CAPABILITY_INTERNET) ?: false) ?.hasCapability(NetworkCapabilities.NET_CAPABILITY_VALIDATED) ?: false)
} }
private inner class IdleBroadcastReceiver: BroadcastReceiver() { private inner class IdleBroadcastReceiver: BroadcastReceiver() {