Deprecate usage of BuildCompat

This commit is contained in:
Mygod
2020-06-11 03:17:42 +08:00
parent cc84a9ba66
commit b9994bda9e
12 changed files with 57 additions and 69 deletions

View File

@@ -160,11 +160,11 @@ Hidden whitelisted APIs: (same catch as above, however, things in this list are
* (since API 24) `Landroid/bluetooth/BluetoothPan;->isTetheringOn()Z,system-api,whitelist`
* (since API 30) `Landroid/content/Context;->TETHERING_SERVICE:Ljava/lang/String;,system-api,whitelist`
* (since API 24) [`Landroid/net/ConnectivityManager$OnStartTetheringCallback;-><init>()V,system-api,whitelist`](https://android.googlesource.com/platform/prebuilts/runtime/+/3d07e5c/appcompat/hiddenapi-flags.csv#123103)
* (since API 24) [`Landroid/net/ConnectivityManager$OnStartTetheringCallback;->onTetheringFailed()V,system-api,whitelist`](https://android.googlesource.com/platform/prebuilts/runtime/+/3d07e5c/appcompat/hiddenapi-flags.csv#123104)
* (since API 24) [`Landroid/net/ConnectivityManager$OnStartTetheringCallback;->onTetheringStarted()V,system-api,whitelist`](https://android.googlesource.com/platform/prebuilts/runtime/+/3d07e5c/appcompat/hiddenapi-flags.csv#123105)
* (since API 24) [`Landroid/net/ConnectivityManager;->startTethering(IZLandroid/net/ConnectivityManager$OnStartTetheringCallback;Landroid/os/Handler;)V,system-api,whitelist`](https://android.googlesource.com/platform/prebuilts/runtime/+/3d07e5c/appcompat/hiddenapi-flags.csv#123408)
* (since API 24) [`Landroid/net/ConnectivityManager;->stopTethering(I)V,system-api,whitelist`](https://android.googlesource.com/platform/prebuilts/runtime/+/3d07e5c/appcompat/hiddenapi-flags.csv#123410)
* (since API 24, prior to API 30) [`Landroid/net/ConnectivityManager$OnStartTetheringCallback;-><init>()V,system-api,whitelist`](https://android.googlesource.com/platform/prebuilts/runtime/+/3d07e5c/appcompat/hiddenapi-flags.csv#123103)
* (since API 24, prior to API 30) [`Landroid/net/ConnectivityManager$OnStartTetheringCallback;->onTetheringFailed()V,system-api,whitelist`](https://android.googlesource.com/platform/prebuilts/runtime/+/3d07e5c/appcompat/hiddenapi-flags.csv#123104)
* (since API 24, prior to API 30) [`Landroid/net/ConnectivityManager$OnStartTetheringCallback;->onTetheringStarted()V,system-api,whitelist`](https://android.googlesource.com/platform/prebuilts/runtime/+/3d07e5c/appcompat/hiddenapi-flags.csv#123105)
* (since API 24, prior to API 30) [`Landroid/net/ConnectivityManager;->startTethering(IZLandroid/net/ConnectivityManager$OnStartTetheringCallback;Landroid/os/Handler;)V,system-api,whitelist`](https://android.googlesource.com/platform/prebuilts/runtime/+/3d07e5c/appcompat/hiddenapi-flags.csv#123408)
* (since API 24, prior to API 30) [`Landroid/net/ConnectivityManager;->stopTethering(I)V,system-api,whitelist`](https://android.googlesource.com/platform/prebuilts/runtime/+/3d07e5c/appcompat/hiddenapi-flags.csv#123410)
* (since API 30) `Landroid/net/TetheringManager$StartTetheringCallback;->onTetheringFailed(I)V,system-api,test-api,whitelist`
* (since API 30) `Landroid/net/TetheringManager$StartTetheringCallback;->onTetheringStarted()V,system-api,test-api,whitelist`
* (since API 30) `Landroid/net/TetheringManager$TetheringEventCallback;->onClientsChanged(Ljava/util/Collection;)V,system-api,test-api,whitelist`

View File

@@ -5,7 +5,6 @@ import android.content.IntentFilter
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.IpNeighbour
import be.mygod.vpnhotspot.net.TetheringManager
@@ -33,11 +32,9 @@ class LocalOnlyHotspotService : IpNeighbourMonitoringService(), CoroutineScope {
}
val ifaceChanged = StickyEvent1 { iface }
val configuration get() = if (BuildCompat.isAtLeastR()) {
reservation?.softApConfiguration?.toCompat()
} else @Suppress("DEPRECATION") {
val configuration get() = if (Build.VERSION.SDK_INT < 30) @Suppress("DEPRECATION") {
reservation?.wifiConfiguration?.toCompat()
}
} else reservation?.softApConfiguration?.toCompat()
fun stop() {
when (iface) {

View File

@@ -2,7 +2,6 @@ package be.mygod.vpnhotspot
import android.annotation.TargetApi
import android.os.Build
import androidx.core.os.BuildCompat
import be.mygod.vpnhotspot.App.Companion.app
import be.mygod.vpnhotspot.net.Routing
import be.mygod.vpnhotspot.net.TetherType
@@ -61,7 +60,7 @@ abstract class RoutingManager(private val caller: Any, val downstream: String, p
fun start() = when (val other = active.putIfAbsent(downstream, this)) {
null -> {
if (isWifi) WifiDoubleLock.acquire(this)
if (!forceWifi && BuildCompat.isAtLeastR()) TetherType.listener[this] = {
if (!forceWifi && Build.VERSION.SDK_INT >= 30) TetherType.listener[this] = {
val isWifiNow = TetherType.ofInterface(downstream).isWifi
if (isWifi != isWifiNow) {
if (isWifi) WifiDoubleLock.release(this) else WifiDoubleLock.acquire(this)
@@ -97,7 +96,7 @@ abstract class RoutingManager(private val caller: Any, val downstream: String, p
fun stop() {
if (active.remove(downstream, this)) {
if (!forceWifi && BuildCompat.isAtLeastR()) TetherType.listener -= this
if (!forceWifi && Build.VERSION.SDK_INT >= 30) TetherType.listener -= this
if (isWifi) WifiDoubleLock.release(this)
routing?.revert()
}

View File

@@ -1,6 +1,7 @@
package be.mygod.vpnhotspot.client
import android.content.DialogInterface
import android.os.Build
import android.os.Bundle
import android.os.Parcelable
import android.text.format.DateUtils
@@ -14,7 +15,6 @@ import android.view.ViewGroup
import android.widget.EditText
import androidx.appcompat.app.AlertDialog
import androidx.appcompat.widget.PopupMenu
import androidx.core.os.BuildCompat
import androidx.databinding.BaseObservable
import androidx.fragment.app.Fragment
import androidx.fragment.app.activityViewModels
@@ -235,7 +235,7 @@ class ClientsFragment : Fragment() {
override fun onStart() {
// icon might be changed due to TetherType changes
if (BuildCompat.isAtLeastR()) TetherType.listener[this] = {
if (Build.VERSION.SDK_INT >= 30) TetherType.listener[this] = {
lifecycleScope.launchWhenStarted { adapter.notifyItemRangeChanged(0, adapter.size.await()) }
}
super.onStart()
@@ -251,6 +251,6 @@ class ClientsFragment : Fragment() {
override fun onStop() {
TrafficRecorder.foregroundListeners -= this
super.onStop()
if (BuildCompat.isAtLeastR()) TetherType.listener -= this
if (Build.VERSION.SDK_INT >= 30) TetherType.listener -= this
}
}

View File

@@ -7,9 +7,9 @@ import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent
import android.content.IntentFilter
import android.os.Build
import android.widget.Toast
import androidx.annotation.RequiresApi
import androidx.core.os.BuildCompat
import be.mygod.vpnhotspot.App.Companion.app
import be.mygod.vpnhotspot.net.TetheringManager
import be.mygod.vpnhotspot.util.broadcastReceiver
@@ -95,7 +95,7 @@ class BluetoothTethering(context: Context, val stateListener: (Int) -> Unit) :
isTetheringOn(pan) as Boolean
} catch (e: InvocationTargetException) {
activeFailureCause = e.cause ?: e
if (e.cause is SecurityException && BuildCompat.isAtLeastR()) Timber.d(e) else Timber.w(e)
if (e.cause is SecurityException && Build.VERSION.SDK_INT >= 30) Timber.d(e) else Timber.w(e)
return null
}
}

View File

@@ -13,7 +13,6 @@ import androidx.activity.result.contract.ActivityResultContracts
import androidx.annotation.RequiresApi
import androidx.appcompat.widget.Toolbar
import androidx.core.content.ContextCompat
import androidx.core.os.BuildCompat
import androidx.fragment.app.Fragment
import androidx.lifecycle.lifecycleScope
import androidx.recyclerview.widget.DefaultItemAnimator
@@ -101,7 +100,7 @@ class TetheringFragment : Fragment(), ServiceConnection, Toolbar.OnMenuItemClick
list.addAll(tetherManagers)
tetherManagers.forEach { it.updateErrorMessage(erroredIfaces) }
}
if (BuildCompat.isAtLeastR()) {
if (Build.VERSION.SDK_INT >= 30) {
list.addAll(tetherManagers30)
tetherManagers30.forEach { it.updateErrorMessage(erroredIfaces) }
}
@@ -229,7 +228,7 @@ class TetheringFragment : Fragment(), ServiceConnection, Toolbar.OnMenuItemClick
lifecycleScope.launchWhenStarted { adapter.notifyInterfaceChanged() }
}
requireContext().registerReceiver(receiver, IntentFilter(TetheringManager.ACTION_TETHER_STATE_CHANGED))
if (BuildCompat.isAtLeastR()) TetherType.listener[this] = {
if (Build.VERSION.SDK_INT >= 30) TetherType.listener[this] = {
lifecycleScope.launchWhenStarted { adapter.notifyTetherTypeChanged() }
}
}
@@ -237,7 +236,7 @@ class TetheringFragment : Fragment(), ServiceConnection, Toolbar.OnMenuItemClick
override fun onServiceDisconnected(name: ComponentName?) {
(binder ?: return).routingsChanged -= this
binder = null
if (BuildCompat.isAtLeastR()) TetherType.listener -= this
if (Build.VERSION.SDK_INT >= 30) TetherType.listener -= this
requireContext().unregisterReceiver(receiver)
}
}

View File

@@ -5,12 +5,12 @@ import android.content.Context
import android.content.Intent
import android.content.IntentFilter
import android.graphics.drawable.Icon
import android.os.Build
import android.os.IBinder
import android.service.quicksettings.Tile
import android.widget.Toast
import androidx.annotation.RequiresApi
import androidx.core.content.ContextCompat
import androidx.core.os.BuildCompat
import be.mygod.vpnhotspot.R
import be.mygod.vpnhotspot.TetheringService
import be.mygod.vpnhotspot.net.TetherType
@@ -51,12 +51,12 @@ sealed class TetheringTileService : KillableTileService(), TetheringManager.Star
// we need to initialize tethered ASAP for onClick, which is not achievable using registerTetheringEventCallback
tethered = registerReceiver(receiver, IntentFilter(TetheringManager.ACTION_TETHER_STATE_CHANGED))
?.tetheredIfaces
if (BuildCompat.isAtLeastR()) TetherType.listener[this] = this::updateTile
if (Build.VERSION.SDK_INT >= 30) TetherType.listener[this] = this::updateTile
updateTile()
}
override fun onStopListening() {
if (BuildCompat.isAtLeastR()) TetherType.listener -= this
if (Build.VERSION.SDK_INT >= 30) TetherType.listener -= this
unregisterReceiver(receiver)
stopAndUnbind(this)
super.onStopListening()

View File

@@ -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

View File

@@ -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>()
}

View File

@@ -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) {

View File

@@ -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)
}

View File

@@ -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 {