Revert "Ensure receiver unregistered synchronously in onDestroy"

This reverts commit e55aa17399.
This commit is contained in:
Mygod
2019-08-19 23:49:09 +08:00
parent 5e87244041
commit 4aece2204a
3 changed files with 37 additions and 31 deletions

View File

@@ -126,6 +126,7 @@ class RepeaterService : Service(), CoroutineScope, WifiP2pManager.ChannelListene
private val handler = Handler() private val handler = Handler()
@RequiresApi(28) @RequiresApi(28)
private var timeoutMonitor: TetherTimeoutMonitor? = null private var timeoutMonitor: TetherTimeoutMonitor? = null
private var receiverRegistered = false
private val receiver = broadcastReceiver { _, intent -> private val receiver = broadcastReceiver { _, intent ->
when (intent.action) { when (intent.action) {
WifiP2pManager.WIFI_P2P_STATE_CHANGED_ACTION -> WifiP2pManager.WIFI_P2P_STATE_CHANGED_ACTION ->
@@ -257,8 +258,10 @@ class RepeaterService : Service(), CoroutineScope, WifiP2pManager.ChannelListene
if (Build.VERSION.SDK_INT >= 26 && app.uiMode.currentModeType == Configuration.UI_MODE_TYPE_TELEVISION) { if (Build.VERSION.SDK_INT >= 26 && app.uiMode.currentModeType == Configuration.UI_MODE_TYPE_TELEVISION) {
showNotification() showNotification()
} }
launch {
registerReceiver(receiver, intentFilter(WifiP2pManager.WIFI_P2P_STATE_CHANGED_ACTION, registerReceiver(receiver, intentFilter(WifiP2pManager.WIFI_P2P_STATE_CHANGED_ACTION,
WifiP2pManager.WIFI_P2P_CONNECTION_CHANGED_ACTION)) WifiP2pManager.WIFI_P2P_CONNECTION_CHANGED_ACTION))
receiverRegistered = true
try { try {
p2pManager.requestGroupInfo(channel) { p2pManager.requestGroupInfo(channel) {
when { when {
@@ -278,6 +281,7 @@ class RepeaterService : Service(), CoroutineScope, WifiP2pManager.ChannelListene
Timber.w(e) Timber.w(e)
startFailure(e.readableMessage) startFailure(e.readableMessage)
} }
}
return START_NOT_STICKY return START_NOT_STICKY
} }
/** /**
@@ -396,7 +400,10 @@ class RepeaterService : Service(), CoroutineScope, WifiP2pManager.ChannelListene
}) })
} }
private fun cleanLocked() { private fun cleanLocked() {
ensureReceiverUnregistered(receiver) if (receiverRegistered) {
unregisterReceiver(receiver)
receiverRegistered = false
}
if (Build.VERSION.SDK_INT >= 28) { if (Build.VERSION.SDK_INT >= 28) {
timeoutMonitor?.close() timeoutMonitor?.close()
timeoutMonitor = null timeoutMonitor = null
@@ -411,7 +418,6 @@ class RepeaterService : Service(), CoroutineScope, WifiP2pManager.ChannelListene
override fun onDestroy() { override fun onDestroy() {
handler.removeCallbacksAndMessages(null) handler.removeCallbacksAndMessages(null)
if (status != Status.IDLE) binder.shutdown() if (status != Status.IDLE) binder.shutdown()
ensureReceiverUnregistered(receiver)
launch { // force clean to prevent leakage launch { // force clean to prevent leakage
cleanLocked() cleanLocked()
cancel() cancel()

View File

@@ -10,7 +10,6 @@ import be.mygod.vpnhotspot.net.TetheringManager.tetheredIfaces
import be.mygod.vpnhotspot.net.monitor.IpNeighbourMonitor import be.mygod.vpnhotspot.net.monitor.IpNeighbourMonitor
import be.mygod.vpnhotspot.util.Event0 import be.mygod.vpnhotspot.util.Event0
import be.mygod.vpnhotspot.util.broadcastReceiver import be.mygod.vpnhotspot.util.broadcastReceiver
import be.mygod.vpnhotspot.util.ensureReceiverUnregistered
import kotlinx.coroutines.* import kotlinx.coroutines.*
import java.util.concurrent.ConcurrentHashMap import java.util.concurrent.ConcurrentHashMap
@@ -47,6 +46,7 @@ class TetheringService : IpNeighbourMonitoringService(), CoroutineScope {
override val coroutineContext = dispatcher + Job() override val coroutineContext = dispatcher + Job()
private val binder = Binder() private val binder = Binder()
private val downstreams = ConcurrentHashMap<String, Downstream>() private val downstreams = ConcurrentHashMap<String, Downstream>()
private var receiverRegistered = false
private val receiver = broadcastReceiver { _, intent -> private val receiver = broadcastReceiver { _, intent ->
launch { launch {
val toRemove = downstreams.toMutableMap() // make a copy val toRemove = downstreams.toMutableMap() // make a copy
@@ -69,8 +69,11 @@ class TetheringService : IpNeighbourMonitoringService(), CoroutineScope {
ServiceNotification.stopForeground(this) ServiceNotification.stopForeground(this)
stopSelf() stopSelf()
} else { } else {
if (!receiverRegistered) {
receiverRegistered = true
registerReceiver(receiver, IntentFilter(TetheringManager.ACTION_TETHER_STATE_CHANGED)) registerReceiver(receiver, IntentFilter(TetheringManager.ACTION_TETHER_STATE_CHANGED))
IpNeighbourMonitor.registerCallback(this) IpNeighbourMonitor.registerCallback(this)
}
updateNotification() updateNotification()
} }
launch(Dispatchers.Main) { binder.routingsChanged() } launch(Dispatchers.Main) { binder.routingsChanged() }
@@ -103,9 +106,9 @@ class TetheringService : IpNeighbourMonitoringService(), CoroutineScope {
} }
override fun onDestroy() { override fun onDestroy() {
unregisterReceiver()
launch { launch {
downstreams.values.forEach { it.destroy() } // force clean to prevent leakage downstreams.values.forEach { it.destroy() } // force clean to prevent leakage
unregisterReceiver()
cancel() cancel()
dispatcher.close() dispatcher.close()
} }
@@ -113,7 +116,10 @@ class TetheringService : IpNeighbourMonitoringService(), CoroutineScope {
} }
private fun unregisterReceiver() { private fun unregisterReceiver() {
ensureReceiverUnregistered(receiver) if (receiverRegistered) {
unregisterReceiver(receiver)
IpNeighbourMonitor.unregisterCallback(this) IpNeighbourMonitor.unregisterCallback(this)
receiverRegistered = false
}
} }
} }

View File

@@ -35,12 +35,6 @@ fun Long.toPluralInt(): Int {
return (this % 1000000000).toInt() + 1000000000 return (this % 1000000000).toInt() + 1000000000
} }
fun Context.ensureReceiverUnregistered(receiver: BroadcastReceiver) {
try {
unregisterReceiver(receiver)
} catch (_: IllegalArgumentException) { }
}
@SuppressLint("Recycle") @SuppressLint("Recycle")
fun <T> useParcel(block: (Parcel) -> T) = Parcel.obtain().run { fun <T> useParcel(block: (Parcel) -> T) = Parcel.obtain().run {
try { try {