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.wifi.WifiApManager
import be.mygod.vpnhotspot.util.readableMessage
import be.mygod.vpnhotspot.widget.SmartSnackbar
import timber.log.Timber
import java.io.IOException
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.util.readableMessage
import be.mygod.vpnhotspot.util.stopAndUnbind
import be.mygod.vpnhotspot.widget.SmartSnackbar
import timber.log.Timber
import java.io.IOException
import java.lang.reflect.InvocationTargetException

View File

@@ -9,6 +9,7 @@ import androidx.annotation.RequiresApi
import be.mygod.vpnhotspot.App.Companion.app
import com.android.dx.stock.ProxyBuilder
import timber.log.Timber
import java.lang.ref.WeakReference
/**
* Heavily based on:
@@ -115,17 +116,19 @@ object TetheringManager {
*/
@RequiresApi(24)
fun start(type: Int, showProvisioningUi: Boolean, callback: OnStartTetheringCallback, handler: Handler? = null) {
val reference = WeakReference(callback)
val proxy = ProxyBuilder.forClass(classOnStartTetheringCallback)
.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()
callback?.onTetheringStarted()
null
}
"onTetheringFailed" -> {
callback.onTetheringFailed()
callback?.onTetheringFailed()
null
}
else -> {