Prevent callback not recycled

This commit is contained in:
Mygod
2019-03-18 17:41:04 +08:00
parent 549381050b
commit 621b6eac74
3 changed files with 5 additions and 4 deletions

View File

@@ -22,7 +22,6 @@ import be.mygod.vpnhotspot.net.TetherType
import be.mygod.vpnhotspot.net.TetheringManager import be.mygod.vpnhotspot.net.TetheringManager
import be.mygod.vpnhotspot.net.wifi.WifiApManager import be.mygod.vpnhotspot.net.wifi.WifiApManager
import be.mygod.vpnhotspot.util.readableMessage import be.mygod.vpnhotspot.util.readableMessage
import be.mygod.vpnhotspot.widget.SmartSnackbar
import timber.log.Timber import timber.log.Timber
import java.io.IOException import java.io.IOException
import java.lang.reflect.InvocationTargetException import java.lang.reflect.InvocationTargetException

View File

@@ -17,7 +17,6 @@ import be.mygod.vpnhotspot.net.TetheringManager
import be.mygod.vpnhotspot.net.wifi.WifiApManager import be.mygod.vpnhotspot.net.wifi.WifiApManager
import be.mygod.vpnhotspot.util.readableMessage import be.mygod.vpnhotspot.util.readableMessage
import be.mygod.vpnhotspot.util.stopAndUnbind import be.mygod.vpnhotspot.util.stopAndUnbind
import be.mygod.vpnhotspot.widget.SmartSnackbar
import timber.log.Timber import timber.log.Timber
import java.io.IOException import java.io.IOException
import java.lang.reflect.InvocationTargetException import java.lang.reflect.InvocationTargetException

View File

@@ -9,6 +9,7 @@ import androidx.annotation.RequiresApi
import be.mygod.vpnhotspot.App.Companion.app import be.mygod.vpnhotspot.App.Companion.app
import com.android.dx.stock.ProxyBuilder import com.android.dx.stock.ProxyBuilder
import timber.log.Timber import timber.log.Timber
import java.lang.ref.WeakReference
/** /**
* Heavily based on: * Heavily based on:
@@ -115,17 +116,19 @@ object TetheringManager {
*/ */
@RequiresApi(24) @RequiresApi(24)
fun start(type: Int, showProvisioningUi: Boolean, callback: OnStartTetheringCallback, handler: Handler? = null) { fun start(type: Int, showProvisioningUi: Boolean, callback: OnStartTetheringCallback, handler: Handler? = null) {
val reference = WeakReference(callback)
val proxy = ProxyBuilder.forClass(classOnStartTetheringCallback) val proxy = ProxyBuilder.forClass(classOnStartTetheringCallback)
.dexCache(app.deviceStorage.cacheDir) .dexCache(app.deviceStorage.cacheDir)
.handler { proxy, method, args -> .handler { proxy, method, args ->
if (args.isNotEmpty()) Timber.w("Unexpected args for ${method.name}: $args") if (args.isNotEmpty()) Timber.w("Unexpected args for ${method.name}: $args")
@Suppress("NAME_SHADOWING") val callback = reference.get()
when (method.name) { when (method.name) {
"onTetheringStarted" -> { "onTetheringStarted" -> {
callback.onTetheringStarted() callback?.onTetheringStarted()
null null
} }
"onTetheringFailed" -> { "onTetheringFailed" -> {
callback.onTetheringFailed() callback?.onTetheringFailed()
null null
} }
else -> { else -> {