diff --git a/mobile/src/main/java/be/mygod/vpnhotspot/net/TetheringManager.kt b/mobile/src/main/java/be/mygod/vpnhotspot/net/TetheringManager.kt index 4125b9a5..f8865126 100644 --- a/mobile/src/main/java/be/mygod/vpnhotspot/net/TetheringManager.kt +++ b/mobile/src/main/java/be/mygod/vpnhotspot/net/TetheringManager.kt @@ -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) { diff --git a/mobile/src/main/java/be/mygod/vpnhotspot/util/Utils.kt b/mobile/src/main/java/be/mygod/vpnhotspot/util/Utils.kt index 6b7c3ddd..d4419f37 100644 --- a/mobile/src/main/java/be/mygod/vpnhotspot/util/Utils.kt +++ b/mobile/src/main/java/be/mygod/vpnhotspot/util/Utils.kt @@ -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) }