From 4aece2204a54c839c1dfe8ddc903883deaf21a13 Mon Sep 17 00:00:00 2001 From: Mygod Date: Mon, 19 Aug 2019 23:49:09 +0800 Subject: [PATCH] Revert "Ensure receiver unregistered synchronously in onDestroy" This reverts commit e55aa173998d0a3cf68e5d3067a2808f1f3dbce0. --- .../be/mygod/vpnhotspot/RepeaterService.kt | 44 +++++++++++-------- .../be/mygod/vpnhotspot/TetheringService.kt | 18 +++++--- .../java/be/mygod/vpnhotspot/util/Utils.kt | 6 --- 3 files changed, 37 insertions(+), 31 deletions(-) diff --git a/mobile/src/main/java/be/mygod/vpnhotspot/RepeaterService.kt b/mobile/src/main/java/be/mygod/vpnhotspot/RepeaterService.kt index 28c70bb7..d6fb714b 100644 --- a/mobile/src/main/java/be/mygod/vpnhotspot/RepeaterService.kt +++ b/mobile/src/main/java/be/mygod/vpnhotspot/RepeaterService.kt @@ -126,6 +126,7 @@ class RepeaterService : Service(), CoroutineScope, WifiP2pManager.ChannelListene private val handler = Handler() @RequiresApi(28) private var timeoutMonitor: TetherTimeoutMonitor? = null + private var receiverRegistered = false private val receiver = broadcastReceiver { _, intent -> when (intent.action) { WifiP2pManager.WIFI_P2P_STATE_CHANGED_ACTION -> @@ -257,26 +258,29 @@ class RepeaterService : Service(), CoroutineScope, WifiP2pManager.ChannelListene if (Build.VERSION.SDK_INT >= 26 && app.uiMode.currentModeType == Configuration.UI_MODE_TYPE_TELEVISION) { showNotification() } - registerReceiver(receiver, intentFilter(WifiP2pManager.WIFI_P2P_STATE_CHANGED_ACTION, - WifiP2pManager.WIFI_P2P_CONNECTION_CHANGED_ACTION)) - try { - p2pManager.requestGroupInfo(channel) { - when { - it == null -> doStart() - it.isGroupOwner -> launch { if (routingManager == null) doStartLocked(it) } - else -> { - Timber.i("Removing old group ($it)") - p2pManager.removeGroup(channel, object : WifiP2pManager.ActionListener { - override fun onSuccess() = doStart() - override fun onFailure(reason: Int) = - startFailure(formatReason(R.string.repeater_remove_old_group_failure, reason)) - }) + launch { + registerReceiver(receiver, intentFilter(WifiP2pManager.WIFI_P2P_STATE_CHANGED_ACTION, + WifiP2pManager.WIFI_P2P_CONNECTION_CHANGED_ACTION)) + receiverRegistered = true + try { + p2pManager.requestGroupInfo(channel) { + when { + it == null -> doStart() + it.isGroupOwner -> launch { if (routingManager == null) doStartLocked(it) } + else -> { + Timber.i("Removing old group ($it)") + p2pManager.removeGroup(channel, object : WifiP2pManager.ActionListener { + override fun onSuccess() = doStart() + override fun onFailure(reason: Int) = + startFailure(formatReason(R.string.repeater_remove_old_group_failure, reason)) + }) + } } } + } catch (e: SecurityException) { + Timber.w(e) + startFailure(e.readableMessage) } - } catch (e: SecurityException) { - Timber.w(e) - startFailure(e.readableMessage) } return START_NOT_STICKY } @@ -396,7 +400,10 @@ class RepeaterService : Service(), CoroutineScope, WifiP2pManager.ChannelListene }) } private fun cleanLocked() { - ensureReceiverUnregistered(receiver) + if (receiverRegistered) { + unregisterReceiver(receiver) + receiverRegistered = false + } if (Build.VERSION.SDK_INT >= 28) { timeoutMonitor?.close() timeoutMonitor = null @@ -411,7 +418,6 @@ class RepeaterService : Service(), CoroutineScope, WifiP2pManager.ChannelListene override fun onDestroy() { handler.removeCallbacksAndMessages(null) if (status != Status.IDLE) binder.shutdown() - ensureReceiverUnregistered(receiver) launch { // force clean to prevent leakage cleanLocked() cancel() diff --git a/mobile/src/main/java/be/mygod/vpnhotspot/TetheringService.kt b/mobile/src/main/java/be/mygod/vpnhotspot/TetheringService.kt index 28367ff3..00004964 100644 --- a/mobile/src/main/java/be/mygod/vpnhotspot/TetheringService.kt +++ b/mobile/src/main/java/be/mygod/vpnhotspot/TetheringService.kt @@ -10,7 +10,6 @@ import be.mygod.vpnhotspot.net.TetheringManager.tetheredIfaces import be.mygod.vpnhotspot.net.monitor.IpNeighbourMonitor import be.mygod.vpnhotspot.util.Event0 import be.mygod.vpnhotspot.util.broadcastReceiver -import be.mygod.vpnhotspot.util.ensureReceiverUnregistered import kotlinx.coroutines.* import java.util.concurrent.ConcurrentHashMap @@ -47,6 +46,7 @@ class TetheringService : IpNeighbourMonitoringService(), CoroutineScope { override val coroutineContext = dispatcher + Job() private val binder = Binder() private val downstreams = ConcurrentHashMap() + private var receiverRegistered = false private val receiver = broadcastReceiver { _, intent -> launch { val toRemove = downstreams.toMutableMap() // make a copy @@ -69,8 +69,11 @@ class TetheringService : IpNeighbourMonitoringService(), CoroutineScope { ServiceNotification.stopForeground(this) stopSelf() } else { - registerReceiver(receiver, IntentFilter(TetheringManager.ACTION_TETHER_STATE_CHANGED)) - IpNeighbourMonitor.registerCallback(this) + if (!receiverRegistered) { + receiverRegistered = true + registerReceiver(receiver, IntentFilter(TetheringManager.ACTION_TETHER_STATE_CHANGED)) + IpNeighbourMonitor.registerCallback(this) + } updateNotification() } launch(Dispatchers.Main) { binder.routingsChanged() } @@ -103,9 +106,9 @@ class TetheringService : IpNeighbourMonitoringService(), CoroutineScope { } override fun onDestroy() { - unregisterReceiver() launch { downstreams.values.forEach { it.destroy() } // force clean to prevent leakage + unregisterReceiver() cancel() dispatcher.close() } @@ -113,7 +116,10 @@ class TetheringService : IpNeighbourMonitoringService(), CoroutineScope { } private fun unregisterReceiver() { - ensureReceiverUnregistered(receiver) - IpNeighbourMonitor.unregisterCallback(this) + if (receiverRegistered) { + unregisterReceiver(receiver) + IpNeighbourMonitor.unregisterCallback(this) + receiverRegistered = false + } } } 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 c42c9155..4d129b70 100644 --- a/mobile/src/main/java/be/mygod/vpnhotspot/util/Utils.kt +++ b/mobile/src/main/java/be/mygod/vpnhotspot/util/Utils.kt @@ -35,12 +35,6 @@ fun Long.toPluralInt(): Int { return (this % 1000000000).toInt() + 1000000000 } -fun Context.ensureReceiverUnregistered(receiver: BroadcastReceiver) { - try { - unregisterReceiver(receiver) - } catch (_: IllegalArgumentException) { } -} - @SuppressLint("Recycle") fun useParcel(block: (Parcel) -> T) = Parcel.obtain().run { try {