2nd attempt to fix double unregistering receivers

This commit is contained in:
Mygod
2019-09-19 11:33:50 +08:00
parent add1c96d5c
commit 95ddd5c2d5
3 changed files with 9 additions and 2 deletions

View File

@@ -401,7 +401,7 @@ class RepeaterService : Service(), CoroutineScope, WifiP2pManager.ChannelListene
} }
private fun cleanLocked() { private fun cleanLocked() {
if (receiverRegistered) { if (receiverRegistered) {
unregisterReceiver(receiver) ensureReceiverUnregistered(receiver)
receiverRegistered = false receiverRegistered = false
} }
if (Build.VERSION.SDK_INT >= 28) { if (Build.VERSION.SDK_INT >= 28) {

View File

@@ -10,6 +10,7 @@ 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
@@ -117,7 +118,7 @@ class TetheringService : IpNeighbourMonitoringService(), CoroutineScope {
private fun unregisterReceiver() { private fun unregisterReceiver() {
if (receiverRegistered) { if (receiverRegistered) {
unregisterReceiver(receiver) ensureReceiverUnregistered(receiver)
IpNeighbourMonitor.unregisterCallback(this) IpNeighbourMonitor.unregisterCallback(this)
receiverRegistered = false receiverRegistered = false
} }

View File

@@ -35,6 +35,12 @@ 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 {