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

View file

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