Fixed a bug with turning on/off the VPN.

This commit is contained in:
Revertron 2022-11-19 16:02:45 +01:00
parent 631d321206
commit 4b8e6cf922
2 changed files with 6 additions and 6 deletions

View file

@ -128,6 +128,7 @@ class MainActivity : AppCompatActivity() {
receiver, IntentFilter(STATE_INTENT)
)
val preferences = PreferenceManager.getDefaultSharedPreferences(this.baseContext)
enabledSwitch.isChecked = preferences.getBoolean(PREF_KEY_ENABLED, false)
val serverString = preferences.getString(KEY_DNS_SERVERS, "")
if (serverString!!.isNotEmpty()) {
val servers = serverString.split(",")
@ -153,7 +154,6 @@ class MainActivity : AppCompatActivity() {
when (intent.getStringExtra("type")) {
"state" -> {
enabledLabel.text = if (intent.getBooleanExtra("started", false)) {
enabledSwitch.isChecked = true
var count = 0
if (intent.hasExtra("dht")) {
val dht = intent.getStringExtra("dht")
@ -170,7 +170,6 @@ class MainActivity : AppCompatActivity() {
getString(R.string.main_enabled)
}
} else {
enabledSwitch.isChecked = false
enabledLabel.setTextColor(Color.GRAY)
getString(R.string.main_disabled)
}

View file

@ -59,10 +59,7 @@ class PacketTunnelProvider: VpnService() {
return START_NOT_STICKY
}
val preferences = PreferenceManager.getDefaultSharedPreferences(this.baseContext)
if (!preferences.getBoolean(PREF_KEY_ENABLED, false)) {
Log.d(TAG, "Service is disabled")
return START_NOT_STICKY
}
val enabled = preferences.getBoolean(PREF_KEY_ENABLED, false)
return when (intent.action ?: ACTION_STOP) {
ACTION_STOP -> {
Log.d(TAG, "Stopping...")
@ -86,6 +83,10 @@ class PacketTunnelProvider: VpnService() {
}
}
else -> {
if (!enabled) {
Log.d(TAG, "Service is disabled")
return START_NOT_STICKY
}
Log.d(TAG, "Starting...")
start(); START_STICKY
}