Move clean to RoutingManager
This commit is contained in:
@@ -17,7 +17,6 @@ import androidx.emoji.text.FontRequestEmojiCompatConfig
|
|||||||
import be.mygod.vpnhotspot.net.DhcpWorkaround
|
import be.mygod.vpnhotspot.net.DhcpWorkaround
|
||||||
import be.mygod.vpnhotspot.room.AppDatabase
|
import be.mygod.vpnhotspot.room.AppDatabase
|
||||||
import be.mygod.vpnhotspot.util.DeviceStorageApp
|
import be.mygod.vpnhotspot.util.DeviceStorageApp
|
||||||
import be.mygod.vpnhotspot.util.Event0
|
|
||||||
import be.mygod.vpnhotspot.util.RootSession
|
import be.mygod.vpnhotspot.util.RootSession
|
||||||
import timber.log.Timber
|
import timber.log.Timber
|
||||||
import java.util.*
|
import java.util.*
|
||||||
@@ -79,7 +78,4 @@ class App : Application() {
|
|||||||
.setToolbarColor(ContextCompat.getColor(this, R.color.colorPrimary))
|
.setToolbarColor(ContextCompat.getColor(this, R.color.colorPrimary))
|
||||||
.build()
|
.build()
|
||||||
}
|
}
|
||||||
|
|
||||||
val onPreCleanRoutings = Event0()
|
|
||||||
val onRoutingsCleaned = Event0()
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -3,6 +3,7 @@ package be.mygod.vpnhotspot
|
|||||||
import be.mygod.vpnhotspot.App.Companion.app
|
import be.mygod.vpnhotspot.App.Companion.app
|
||||||
import be.mygod.vpnhotspot.net.Routing
|
import be.mygod.vpnhotspot.net.Routing
|
||||||
import be.mygod.vpnhotspot.net.wifi.WifiDoubleLock
|
import be.mygod.vpnhotspot.net.wifi.WifiDoubleLock
|
||||||
|
import be.mygod.vpnhotspot.util.Event0
|
||||||
import be.mygod.vpnhotspot.widget.SmartSnackbar
|
import be.mygod.vpnhotspot.widget.SmartSnackbar
|
||||||
import timber.log.Timber
|
import timber.log.Timber
|
||||||
|
|
||||||
@@ -16,6 +17,22 @@ abstract class RoutingManager(private val caller: Any, val downstream: String, p
|
|||||||
Routing.MasqueradeMode.Simple else Routing.MasqueradeMode.None
|
Routing.MasqueradeMode.Simple else Routing.MasqueradeMode.None
|
||||||
}
|
}
|
||||||
set(value) = app.pref.edit().putString(KEY_MASQUERADE_MODE, value.name).apply()
|
set(value) = app.pref.edit().putString(KEY_MASQUERADE_MODE, value.name).apply()
|
||||||
|
|
||||||
|
private val onPreCleanRoutings = Event0()
|
||||||
|
private val onRoutingsCleaned = Event0()
|
||||||
|
|
||||||
|
fun clean() {
|
||||||
|
onPreCleanRoutings()
|
||||||
|
val cleaned = try {
|
||||||
|
Routing.clean()
|
||||||
|
true
|
||||||
|
} catch (e: RuntimeException) {
|
||||||
|
Timber.d(e)
|
||||||
|
SmartSnackbar.make(e).show()
|
||||||
|
false
|
||||||
|
}
|
||||||
|
if (cleaned) onRoutingsCleaned()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -39,8 +56,8 @@ abstract class RoutingManager(private val caller: Any, val downstream: String, p
|
|||||||
fun start(): Boolean {
|
fun start(): Boolean {
|
||||||
check(!started)
|
check(!started)
|
||||||
started = true
|
started = true
|
||||||
app.onPreCleanRoutings[this] = { routing?.stop() }
|
onPreCleanRoutings[this] = { routing?.stop() }
|
||||||
app.onRoutingsCleaned[this] = { initRouting() }
|
onRoutingsCleaned[this] = { initRouting() }
|
||||||
return initRouting()
|
return initRouting()
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -66,8 +83,8 @@ abstract class RoutingManager(private val caller: Any, val downstream: String, p
|
|||||||
fun stop() {
|
fun stop() {
|
||||||
if (!started) return
|
if (!started) return
|
||||||
routing?.revert()
|
routing?.revert()
|
||||||
app.onPreCleanRoutings -= this
|
onPreCleanRoutings -= this
|
||||||
app.onRoutingsCleaned -= this
|
onRoutingsCleaned -= this
|
||||||
started = false
|
started = false
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -9,7 +9,6 @@ import androidx.preference.Preference
|
|||||||
import androidx.preference.PreferenceFragmentCompat
|
import androidx.preference.PreferenceFragmentCompat
|
||||||
import androidx.preference.SwitchPreference
|
import androidx.preference.SwitchPreference
|
||||||
import be.mygod.vpnhotspot.App.Companion.app
|
import be.mygod.vpnhotspot.App.Companion.app
|
||||||
import be.mygod.vpnhotspot.net.Routing
|
|
||||||
import be.mygod.vpnhotspot.net.Routing.Companion.IPTABLES
|
import be.mygod.vpnhotspot.net.Routing.Companion.IPTABLES
|
||||||
import be.mygod.vpnhotspot.net.monitor.IpMonitor
|
import be.mygod.vpnhotspot.net.monitor.IpMonitor
|
||||||
import be.mygod.vpnhotspot.net.monitor.UpstreamMonitor
|
import be.mygod.vpnhotspot.net.monitor.UpstreamMonitor
|
||||||
@@ -39,16 +38,7 @@ class SettingsPreferenceFragment : PreferenceFragmentCompat() {
|
|||||||
boot.isChecked = BootReceiver.enabled
|
boot.isChecked = BootReceiver.enabled
|
||||||
} else boot.parent!!.removePreference(boot)
|
} else boot.parent!!.removePreference(boot)
|
||||||
findPreference<Preference>("service.clean").setOnPreferenceClickListener {
|
findPreference<Preference>("service.clean").setOnPreferenceClickListener {
|
||||||
app.onPreCleanRoutings()
|
RoutingManager.clean()
|
||||||
val cleaned = try {
|
|
||||||
Routing.clean()
|
|
||||||
true
|
|
||||||
} catch (e: RuntimeException) {
|
|
||||||
Timber.d(e)
|
|
||||||
SmartSnackbar.make(e).show()
|
|
||||||
false
|
|
||||||
}
|
|
||||||
if (cleaned) app.onRoutingsCleaned()
|
|
||||||
true
|
true
|
||||||
}
|
}
|
||||||
findPreference<Preference>(IpMonitor.KEY).setOnPreferenceChangeListener { _, _ ->
|
findPreference<Preference>(IpMonitor.KEY).setOnPreferenceChangeListener { _, _ ->
|
||||||
|
|||||||
Reference in New Issue
Block a user