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()
@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()