Prevent callbacks called in main

This commit is contained in:
Mygod
2019-07-30 10:08:12 +08:00
parent 4f7f778114
commit 193a918fce
2 changed files with 35 additions and 27 deletions

View File

@@ -2,6 +2,8 @@ package be.mygod.vpnhotspot.net.monitor
import android.content.SharedPreferences
import be.mygod.vpnhotspot.App.Companion.app
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.launch
abstract class FallbackUpstreamMonitor private constructor() : UpstreamMonitor() {
companion object : SharedPreferences.OnSharedPreferenceChangeListener {
@@ -21,7 +23,8 @@ abstract class FallbackUpstreamMonitor private constructor() : UpstreamMonitor()
fun unregisterCallback(callback: Callback) = synchronized(this) { monitor.unregisterCallback(callback) }
override fun onSharedPreferenceChanged(sharedPreferences: SharedPreferences?, key: String?) {
if (key == KEY) synchronized(this) {
if (key == KEY) GlobalScope.launch { // prevent callback called in main
synchronized(this) {
val old = monitor
val callbacks = synchronized(old) {
val callbacks = old.callbacks.toList()
@@ -39,3 +42,4 @@ abstract class FallbackUpstreamMonitor private constructor() : UpstreamMonitor()
}
}
}
}

View File

@@ -3,6 +3,8 @@ package be.mygod.vpnhotspot.net.monitor
import android.content.SharedPreferences
import android.net.LinkProperties
import be.mygod.vpnhotspot.App.Companion.app
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.launch
import java.lang.UnsupportedOperationException
import java.net.InetAddress
import java.util.*
@@ -26,7 +28,8 @@ abstract class UpstreamMonitor {
fun unregisterCallback(callback: Callback) = synchronized(this) { monitor.unregisterCallback(callback) }
override fun onSharedPreferenceChanged(sharedPreferences: SharedPreferences?, key: String?) {
if (key == KEY) synchronized(this) {
if (key == KEY) GlobalScope.launch { // prevent callback called in main
synchronized(this) {
val old = monitor
val (active, callbacks) = synchronized(old) {
val active = old.currentIface != null
@@ -44,6 +47,7 @@ abstract class UpstreamMonitor {
}
}
}
}
interface Callback {
/**