Use ExecutorCompat

This commit is contained in:
Mygod
2021-03-14 20:42:20 -04:00
parent c0c497d5f3
commit 791e41fb74
2 changed files with 8 additions and 7 deletions

View File

@@ -13,6 +13,7 @@ import android.net.Network
import android.os.Build
import android.os.Handler
import androidx.annotation.RequiresApi
import androidx.core.os.ExecutorCompat
import be.mygod.vpnhotspot.App.Companion.app
import be.mygod.vpnhotspot.root.RootManager
import be.mygod.vpnhotspot.root.StartTethering
@@ -65,6 +66,10 @@ object TetheringManager {
}
}
private object InPlaceExecutor : Executor {
override fun execute(command: Runnable) = command.run()
}
/**
* Use with {@link #getSystemService(String)} to retrieve a {@link android.net.TetheringManager}
* for managing tethering functions.
@@ -314,7 +319,7 @@ object TetheringManager {
fun startTethering(type: Int, showProvisioningUi: Boolean, callback: StartTetheringCallback,
handler: Handler? = null, cacheDir: File = app.deviceStorage.codeCacheDir) {
if (Build.VERSION.SDK_INT >= 30) try {
val executor = handler.makeExecutor()
val executor = if (handler == null) InPlaceExecutor else ExecutorCompat.create(handler)
startTethering(type, true, showProvisioningUi,
executor, proxy(object : StartTetheringCallback {
override fun onTetheringStarted() = callback.onTetheringStarted()
@@ -581,7 +586,7 @@ object TetheringManager {
})
}.also { if (!computed) return }
}
registerTetheringEventCallback(instance, executor ?: null.makeExecutor(), proxy)
registerTetheringEventCallback(instance, executor ?: InPlaceExecutor, proxy)
}
/**
* Remove tethering event callback previously registered with
@@ -609,7 +614,7 @@ object TetheringManager {
callback.onTetheredInterfacesChanged(intent.tetheredIfaces ?: return@broadcastReceiver)
}.also { context.registerReceiver(it, IntentFilter(ACTION_TETHER_STATE_CHANGED)) }
}
} else registerTetheringEventCallback(null.makeExecutor(), callback)
} else registerTetheringEventCallback(InPlaceExecutor, callback)
}
fun unregisterTetheringEventCallbackCompat(context: Context, callback: TetheringEventCallback) {
if (Build.VERSION.SDK_INT < 30) {

View File

@@ -7,7 +7,6 @@ import android.net.InetAddresses
import android.net.LinkProperties
import android.net.RouteInfo
import android.os.Build
import android.os.Handler
import android.os.RemoteException
import android.system.Os
import android.text.Spannable
@@ -36,7 +35,6 @@ import java.lang.reflect.Method
import java.net.InetAddress
import java.net.NetworkInterface
import java.net.SocketException
import java.util.concurrent.Executor
tailrec fun Throwable.getRootCause(): Throwable {
if (this is InvocationTargetException || this is RemoteException) return (cause ?: return this).getRootCause()
@@ -60,8 +58,6 @@ fun Context.ensureReceiverUnregistered(receiver: BroadcastReceiver) {
} catch (_: IllegalArgumentException) { }
}
fun Handler?.makeExecutor() = Executor { if (this == null) it.run() else post(it) }
fun DialogFragment.showAllowingStateLoss(manager: FragmentManager, tag: String? = null) {
if (!manager.isStateSaved) show(manager, tag)
}