From 5c52c7fbc522e7d11141291d5650bb53cd1fa509 Mon Sep 17 00:00:00 2001 From: Pacien TRAN-GIRARD Date: Thu, 6 Jul 2017 22:40:17 +0200 Subject: Return to start screen if VPN disconnected by other means --- .../main/java/org/pacien/tincapp/activities/StatusActivity.kt | 10 +++++++++- .../main/java/org/pacien/tincapp/service/TincVpnService.kt | 11 ++++++++--- 2 files changed, 17 insertions(+), 4 deletions(-) diff --git a/app/src/main/java/org/pacien/tincapp/activities/StatusActivity.kt b/app/src/main/java/org/pacien/tincapp/activities/StatusActivity.kt index b4ba7dd..44f4f89 100644 --- a/app/src/main/java/org/pacien/tincapp/activities/StatusActivity.kt +++ b/app/src/main/java/org/pacien/tincapp/activities/StatusActivity.kt @@ -72,11 +72,17 @@ class StatusActivity : BaseActivity(), AdapterView.OnItemClickListener, SwipeRef updateView = false } + override fun onResume() { + super.onResume() + if (!TincVpnService.isConnected()) openStartActivity() + } + override fun onRefresh() { val nodes = getNodeNames() runOnUiThread { nodeListAdapter?.setElements(nodes) node_list_wrapper.isRefreshing = false + if (!TincVpnService.isConnected()) openStartActivity() } } @@ -114,10 +120,12 @@ class StatusActivity : BaseActivity(), AdapterView.OnItemClickListener, SwipeRef fun stopVpn(@Suppress("UNUSED_PARAMETER") i: MenuItem) { TincVpnService.stopVpn() - startActivity(Intent(this, StartActivity::class.java).addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)) + openStartActivity() finish() } + fun openStartActivity() = startActivity(Intent(this, StartActivity::class.java).addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)) + companion object { private val REFRESH_RATE = 5000L diff --git a/app/src/main/java/org/pacien/tincapp/service/TincVpnService.kt b/app/src/main/java/org/pacien/tincapp/service/TincVpnService.kt index 90b7b87..45f901b 100644 --- a/app/src/main/java/org/pacien/tincapp/service/TincVpnService.kt +++ b/app/src/main/java/org/pacien/tincapp/service/TincVpnService.kt @@ -28,8 +28,10 @@ class TincVpnService : VpnService() { } override fun onDestroy() { + connected = false + try { - Tinc.stop(netName!!) + if (netName != null) Tinc.stop(netName!!) fd?.close() } catch (e: IOException) { e.printStackTrace() @@ -42,7 +44,7 @@ class TincVpnService : VpnService() { } private fun startVpn(netName: String) { - if (netName == TincVpnService.netName) onDestroy() + if (isConnected()) onDestroy() TincVpnService.netName = netName TincVpnService.interfaceCfg = VpnInterfaceConfiguration(AppPaths.netConfFile(netName)) @@ -55,6 +57,8 @@ class TincVpnService : VpnService() { } catch (e: IOException) { e.printStackTrace() } + + connected = true } companion object { @@ -64,6 +68,7 @@ class TincVpnService : VpnService() { private enum class Action { START, STOP } + private var connected: Boolean = false private var netName: String? = null private var interfaceCfg: VpnInterfaceConfiguration? = null private var fd: ParcelFileDescriptor? = null @@ -81,7 +86,7 @@ class TincVpnService : VpnService() { fun getCurrentNetName() = netName fun getCurrentInterfaceCfg() = interfaceCfg - fun isConnected() = netName != null + fun isConnected() = connected } -- cgit v1.2.3