Deprecate usage of BuildCompat
This commit is contained in:
@@ -1,9 +1,9 @@
|
||||
package be.mygod.vpnhotspot.net
|
||||
|
||||
import android.content.res.Resources
|
||||
import android.os.Build
|
||||
import androidx.annotation.DrawableRes
|
||||
import androidx.annotation.RequiresApi
|
||||
import androidx.core.os.BuildCompat
|
||||
import be.mygod.vpnhotspot.App.Companion.app
|
||||
import be.mygod.vpnhotspot.R
|
||||
import be.mygod.vpnhotspot.util.Event0
|
||||
@@ -71,7 +71,7 @@ enum class TetherType(@DrawableRes val icon: Int) {
|
||||
*/
|
||||
init {
|
||||
val system = "android" to Resources.getSystem()
|
||||
if (BuildCompat.isAtLeastR()) requiresUpdate = true else {
|
||||
if (Build.VERSION.SDK_INT >= 30) requiresUpdate = true else {
|
||||
usbRegexs = system.getRegexs("config_tether_usb_regexs")
|
||||
wifiRegexs = system.getRegexs("config_tether_wifi_regexs")
|
||||
bluetoothRegexs = system.getRegexs("config_tether_bluetooth_regexs")
|
||||
@@ -92,7 +92,7 @@ enum class TetherType(@DrawableRes val icon: Int) {
|
||||
iface == null -> NONE
|
||||
iface == p2pDev -> WIFI_P2P
|
||||
requiresUpdate -> {
|
||||
if (BuildCompat.isAtLeastR()) updateRegexs() else error("unexpected requiresUpdate")
|
||||
if (Build.VERSION.SDK_INT >= 30) updateRegexs() else error("unexpected requiresUpdate")
|
||||
ofInterface(iface, p2pDev)
|
||||
}
|
||||
wifiRegexs.any { it.matcher(iface).matches() } -> WIFI
|
||||
|
||||
@@ -13,7 +13,6 @@ import android.os.Build
|
||||
import android.os.Handler
|
||||
import androidx.annotation.RequiresApi
|
||||
import androidx.collection.SparseArrayCompat
|
||||
import androidx.core.os.BuildCompat
|
||||
import be.mygod.vpnhotspot.App.Companion.app
|
||||
import be.mygod.vpnhotspot.R
|
||||
import be.mygod.vpnhotspot.util.broadcastReceiver
|
||||
@@ -237,7 +236,7 @@ object TetheringManager {
|
||||
fun startTethering(type: Int, showProvisioningUi: Boolean, callback: StartTetheringCallback,
|
||||
handler: Handler? = null) {
|
||||
val reference = WeakReference(callback)
|
||||
if (BuildCompat.isAtLeastR()) try {
|
||||
if (Build.VERSION.SDK_INT >= 30) {
|
||||
val request = newTetheringRequestBuilder.newInstance(type).let { builder ->
|
||||
// setting exemption requires TETHER_PRIVILEGED permission
|
||||
if (app.checkSelfPermission("android.permission.TETHER_PRIVILEGED") ==
|
||||
@@ -263,23 +262,21 @@ object TetheringManager {
|
||||
}
|
||||
})
|
||||
startTethering(instance, request, handler.makeExecutor(), proxy)
|
||||
return
|
||||
} catch (e: InvocationTargetException) {
|
||||
Timber.w(e, "Unable to invoke TetheringManager.startTethering, falling back to ConnectivityManager")
|
||||
}
|
||||
val proxy = ProxyBuilder.forClass(classOnStartTetheringCallback).apply {
|
||||
dexCache(app.deviceStorage.cacheDir)
|
||||
handler { proxy, method, args ->
|
||||
if (args.isNotEmpty()) Timber.w("Unexpected args for ${method.name}: $args")
|
||||
@Suppress("NAME_SHADOWING") val callback = reference.get()
|
||||
when (method.name) {
|
||||
"onTetheringStarted" -> callback?.onTetheringStarted()
|
||||
"onTetheringFailed" -> callback?.onTetheringFailed()
|
||||
else -> ProxyBuilder.callSuper(proxy, method, args)
|
||||
} else {
|
||||
val proxy = ProxyBuilder.forClass(classOnStartTetheringCallback).apply {
|
||||
dexCache(app.deviceStorage.cacheDir)
|
||||
handler { proxy, method, args ->
|
||||
if (args.isNotEmpty()) Timber.w("Unexpected args for ${method.name}: $args")
|
||||
@Suppress("NAME_SHADOWING") val callback = reference.get()
|
||||
when (method.name) {
|
||||
"onTetheringStarted" -> callback?.onTetheringStarted()
|
||||
"onTetheringFailed" -> callback?.onTetheringFailed()
|
||||
else -> ProxyBuilder.callSuper(proxy, method, args)
|
||||
}
|
||||
}
|
||||
}
|
||||
}.build()
|
||||
startTetheringLegacy(app.connectivity, type, showProvisioningUi, proxy, handler)
|
||||
}.build()
|
||||
startTetheringLegacy(app.connectivity, type, showProvisioningUi, proxy, handler)
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -293,12 +290,7 @@ object TetheringManager {
|
||||
*/
|
||||
@RequiresApi(24)
|
||||
fun stopTethering(type: Int) {
|
||||
if (BuildCompat.isAtLeastR()) try {
|
||||
stopTethering(instance, type)
|
||||
} catch (e: InvocationTargetException) {
|
||||
Timber.w(e, "Unable to invoke TetheringManager.stopTethering, falling back to ConnectivityManager")
|
||||
}
|
||||
stopTetheringLegacy(app.connectivity, type)
|
||||
if (Build.VERSION.SDK_INT >= 30) stopTethering(instance, type) else stopTetheringLegacy(app.connectivity, type)
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -498,7 +490,7 @@ object TetheringManager {
|
||||
* Only [TetheringEventCallback.onTetheredInterfacesChanged] is supported on API 29-.
|
||||
*/
|
||||
fun registerTetheringEventCallbackCompat(context: Context, callback: TetheringEventCallback) {
|
||||
if (BuildCompat.isAtLeastR()) {
|
||||
if (Build.VERSION.SDK_INT >= 30) {
|
||||
registerTetheringEventCallback(null.makeExecutor(), callback)
|
||||
} else synchronized(callbackMap) {
|
||||
callbackMap.computeIfAbsent(callback) {
|
||||
@@ -509,7 +501,7 @@ object TetheringManager {
|
||||
}
|
||||
}
|
||||
fun unregisterTetheringEventCallbackCompat(context: Context, callback: TetheringEventCallback) {
|
||||
if (BuildCompat.isAtLeastR()) {
|
||||
if (Build.VERSION.SDK_INT >= 30) {
|
||||
unregisterTetheringEventCallback(callback)
|
||||
} else {
|
||||
val receiver = synchronized(callbackMap) { callbackMap.remove(callback) } ?: return
|
||||
@@ -545,7 +537,7 @@ object TetheringManager {
|
||||
}
|
||||
}
|
||||
fun tetherErrorMessage(error: Int): String {
|
||||
if (BuildCompat.isAtLeastR()) try {
|
||||
if (Build.VERSION.SDK_INT >= 30) try {
|
||||
tetherErrors.get(error)?.let { return it }
|
||||
} catch (e: ReflectiveOperationException) {
|
||||
Timber.w(e)
|
||||
@@ -557,6 +549,6 @@ object TetheringManager {
|
||||
if (Build.VERSION.SDK_INT >= 26) EXTRA_ACTIVE_TETHER else EXTRA_ACTIVE_TETHER_LEGACY)
|
||||
val Intent.localOnlyTetheredIfaces get() = if (Build.VERSION.SDK_INT >= 26) {
|
||||
getStringArrayListExtra(
|
||||
if (BuildCompat.isAtLeastR()) EXTRA_ACTIVE_LOCAL_ONLY else EXTRA_ACTIVE_LOCAL_ONLY_LEGACY)
|
||||
if (Build.VERSION.SDK_INT >= 30) EXTRA_ACTIVE_LOCAL_ONLY else EXTRA_ACTIVE_LOCAL_ONLY_LEGACY)
|
||||
} else emptyList<String>()
|
||||
}
|
||||
|
||||
@@ -1,8 +1,8 @@
|
||||
package be.mygod.vpnhotspot.net.monitor
|
||||
|
||||
import android.os.Build
|
||||
import android.system.ErrnoException
|
||||
import android.system.OsConstants
|
||||
import androidx.core.os.BuildCompat
|
||||
import be.mygod.vpnhotspot.App.Companion.app
|
||||
import be.mygod.vpnhotspot.R
|
||||
import be.mygod.vpnhotspot.util.RootSession
|
||||
@@ -21,8 +21,12 @@ abstract class IpMonitor : Runnable {
|
||||
// https://android.googlesource.com/platform/external/iproute2/+/7f7a711/lib/libnetlink.c#493
|
||||
private val errorMatcher = "(^Cannot bind netlink socket: |Dump (was interrupted and may be inconsistent.|terminated)$)"
|
||||
.toRegex()
|
||||
private val currentMode get() = Mode.valueOf(app.pref.getString(KEY, (if (BuildCompat.isAtLeastR())
|
||||
Mode.MonitorRoot else @Suppress("DEPRECATION") Mode.Poll).toString()) ?: "")
|
||||
private val currentMode: Mode get() {
|
||||
val defaultMode = if (Build.VERSION.SDK_INT < 30) @Suppress("DEPRECATION") {
|
||||
Mode.Poll
|
||||
} else Mode.MonitorRoot
|
||||
return Mode.valueOf(app.pref.getString(KEY, defaultMode.toString()) ?: "")
|
||||
}
|
||||
}
|
||||
|
||||
enum class Mode(val isMonitor: Boolean = false) {
|
||||
|
||||
@@ -16,7 +16,6 @@ import android.widget.ArrayAdapter
|
||||
import androidx.annotation.RequiresApi
|
||||
import androidx.appcompat.app.AlertDialog
|
||||
import androidx.appcompat.widget.Toolbar
|
||||
import androidx.core.os.BuildCompat
|
||||
import androidx.core.view.isGone
|
||||
import be.mygod.vpnhotspot.AlertDialogFragment
|
||||
import be.mygod.vpnhotspot.App.Companion.app
|
||||
@@ -128,7 +127,7 @@ class WifiApDialogFragment : AlertDialogFragment<WifiApDialogFragment.Arg, WifiA
|
||||
if (Build.VERSION.SDK_INT >= 28) add(BandOption.BandAny)
|
||||
add(BandOption.Band2GHz)
|
||||
add(BandOption.Band5GHz)
|
||||
if (BuildCompat.isAtLeastR()) add(BandOption.Band6GHz)
|
||||
if (Build.VERSION.SDK_INT >= 30) add(BandOption.Band6GHz)
|
||||
}
|
||||
addAll(channels)
|
||||
}
|
||||
|
||||
@@ -3,8 +3,8 @@ package be.mygod.vpnhotspot.net.wifi
|
||||
import android.annotation.TargetApi
|
||||
import android.net.wifi.SoftApConfiguration
|
||||
import android.net.wifi.WifiManager
|
||||
import android.os.Build
|
||||
import androidx.annotation.RequiresApi
|
||||
import androidx.core.os.BuildCompat
|
||||
import be.mygod.vpnhotspot.App.Companion.app
|
||||
import be.mygod.vpnhotspot.net.wifi.SoftApConfigurationCompat.Companion.toCompat
|
||||
|
||||
@@ -23,17 +23,15 @@ object WifiApManager {
|
||||
}
|
||||
|
||||
var configuration: SoftApConfigurationCompat
|
||||
get() = if (BuildCompat.isAtLeastR()) {
|
||||
(getSoftApConfiguration(app.wifi) as SoftApConfiguration).toCompat()
|
||||
} else @Suppress("DEPRECATION") {
|
||||
get() = if (Build.VERSION.SDK_INT < 30) @Suppress("DEPRECATION") {
|
||||
(getWifiApConfiguration(app.wifi) as android.net.wifi.WifiConfiguration?)?.toCompat()
|
||||
?: SoftApConfigurationCompat.empty()
|
||||
}
|
||||
set(value) = if (BuildCompat.isAtLeastR()) {
|
||||
require(setSoftApConfiguration(app.wifi, value.toPlatform()) as Boolean) { "setSoftApConfiguration failed" }
|
||||
} else @Suppress("DEPRECATION") {
|
||||
} else (getSoftApConfiguration(app.wifi) as SoftApConfiguration).toCompat()
|
||||
set(value) = if (Build.VERSION.SDK_INT < 30) @Suppress("DEPRECATION") {
|
||||
require(setWifiApConfiguration(app.wifi,
|
||||
value.toWifiConfiguration()) as Boolean) { "setWifiApConfiguration failed" }
|
||||
} else require(setSoftApConfiguration(app.wifi, value.toPlatform()) as Boolean) {
|
||||
"setSoftApConfiguration failed"
|
||||
}
|
||||
|
||||
private val cancelLocalOnlyHotspotRequest by lazy {
|
||||
|
||||
Reference in New Issue
Block a user