From 621b6eac74d19e0b87680d8fe3508742651eafa2 Mon Sep 17 00:00:00 2001 From: Mygod Date: Mon, 18 Mar 2019 17:41:04 +0800 Subject: [PATCH] Prevent callback not recycled --- .../main/java/be/mygod/vpnhotspot/manage/TetherManager.kt | 1 - .../be/mygod/vpnhotspot/manage/TetheringTileService.kt | 1 - .../main/java/be/mygod/vpnhotspot/net/TetheringManager.kt | 7 +++++-- 3 files changed, 5 insertions(+), 4 deletions(-) diff --git a/mobile/src/main/java/be/mygod/vpnhotspot/manage/TetherManager.kt b/mobile/src/main/java/be/mygod/vpnhotspot/manage/TetherManager.kt index 3ec81097..1a0c3be4 100644 --- a/mobile/src/main/java/be/mygod/vpnhotspot/manage/TetherManager.kt +++ b/mobile/src/main/java/be/mygod/vpnhotspot/manage/TetherManager.kt @@ -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 diff --git a/mobile/src/main/java/be/mygod/vpnhotspot/manage/TetheringTileService.kt b/mobile/src/main/java/be/mygod/vpnhotspot/manage/TetheringTileService.kt index 26ff4ed6..371ff8f7 100644 --- a/mobile/src/main/java/be/mygod/vpnhotspot/manage/TetheringTileService.kt +++ b/mobile/src/main/java/be/mygod/vpnhotspot/manage/TetheringTileService.kt @@ -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 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 207ab751..3d1448b6 100644 --- a/mobile/src/main/java/be/mygod/vpnhotspot/net/TetheringManager.kt +++ b/mobile/src/main/java/be/mygod/vpnhotspot/net/TetheringManager.kt @@ -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 -> {