Move clean to RoutingManager

This commit is contained in:
Mygod
2019-03-08 13:39:28 -05:00
parent 89d007be2c
commit 481cceb3e3
3 changed files with 22 additions and 19 deletions

View File

@@ -17,7 +17,6 @@ import androidx.emoji.text.FontRequestEmojiCompatConfig
import be.mygod.vpnhotspot.net.DhcpWorkaround
import be.mygod.vpnhotspot.room.AppDatabase
import be.mygod.vpnhotspot.util.DeviceStorageApp
import be.mygod.vpnhotspot.util.Event0
import be.mygod.vpnhotspot.util.RootSession
import timber.log.Timber
import java.util.*
@@ -79,7 +78,4 @@ class App : Application() {
.setToolbarColor(ContextCompat.getColor(this, R.color.colorPrimary))
.build()
}
val onPreCleanRoutings = Event0()
val onRoutingsCleaned = Event0()
}

View File

@@ -3,6 +3,7 @@ package be.mygod.vpnhotspot
import be.mygod.vpnhotspot.App.Companion.app
import be.mygod.vpnhotspot.net.Routing
import be.mygod.vpnhotspot.net.wifi.WifiDoubleLock
import be.mygod.vpnhotspot.util.Event0
import be.mygod.vpnhotspot.widget.SmartSnackbar
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
}
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 {
check(!started)
started = true
app.onPreCleanRoutings[this] = { routing?.stop() }
app.onRoutingsCleaned[this] = { initRouting() }
onPreCleanRoutings[this] = { routing?.stop() }
onRoutingsCleaned[this] = { initRouting() }
return initRouting()
}
@@ -66,8 +83,8 @@ abstract class RoutingManager(private val caller: Any, val downstream: String, p
fun stop() {
if (!started) return
routing?.revert()
app.onPreCleanRoutings -= this
app.onRoutingsCleaned -= this
onPreCleanRoutings -= this
onRoutingsCleaned -= this
started = false
}

View File

@@ -9,7 +9,6 @@ import androidx.preference.Preference
import androidx.preference.PreferenceFragmentCompat
import androidx.preference.SwitchPreference
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.monitor.IpMonitor
import be.mygod.vpnhotspot.net.monitor.UpstreamMonitor
@@ -39,16 +38,7 @@ class SettingsPreferenceFragment : PreferenceFragmentCompat() {
boot.isChecked = BootReceiver.enabled
} else boot.parent!!.removePreference(boot)
findPreference<Preference>("service.clean").setOnPreferenceClickListener {
app.onPreCleanRoutings()
val cleaned = try {
Routing.clean()
true
} catch (e: RuntimeException) {
Timber.d(e)
SmartSnackbar.make(e).show()
false
}
if (cleaned) app.onRoutingsCleaned()
RoutingManager.clean()
true
}
findPreference<Preference>(IpMonitor.KEY).setOnPreferenceChangeListener { _, _ ->