From ac289a9e48e08c8cea7e301eb5f70b6aa025a1d6 Mon Sep 17 00:00:00 2001 From: pacien Date: Fri, 18 Aug 2017 10:27:25 +0200 Subject: Expose CONNECT and DISCONNECT intents --- app/src/main/AndroidManifest.xml | 9 ++++++ .../pacien/tincapp/activities/PromptActivity.kt | 36 +++++++++++++++------- .../org/pacien/tincapp/activities/StartActivity.kt | 2 +- .../org/pacien/tincapp/intent/action/Actions.kt | 17 ++++++++++ .../org/pacien/tincapp/service/TincVpnService.kt | 21 ++++++------- 5 files changed, 62 insertions(+), 23 deletions(-) create mode 100644 app/src/main/java/org/pacien/tincapp/intent/action/Actions.kt (limited to 'app/src') diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index 89e618c..5611e4c 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -39,6 +39,15 @@ + + + + + + + + + requestVpnPermission() + when (intent.action) { + ACTION_CONNECT -> connect() + ACTION_DISCONNECT -> disconnect() } } override fun onActivityResult(request: Int, result: Int, data: Intent?) { - if (result == Activity.RESULT_OK) TincVpnService.startVpn(intent.getStringExtra(INTENT_EXTRA_NET_NAME)) + if (result == Activity.RESULT_OK) TincVpnService.startVpn(intent.data.schemeSpecificPart) finish() } - private fun requestVpnPermission() = VpnService.prepare(this).let { + private fun connect() = VpnService.prepare(this).let { if (it != null) startActivityForResult(it, 0) else - onActivityResult(0, Activity.RESULT_OK, Intent()) + onActivityResult(0, Activity.RESULT_OK, null) + } + + private fun disconnect() { + TincVpnService.stopVpn() + finish() } companion object { - private val INTENT_EXTRA_ACTION = "action" - private val INTENT_EXTRA_NET_NAME = "netName" - private enum class Action { REQUEST_PERMISSION } + fun connect(netName: String) { + App.getContext().startActivity(Intent(App.getContext(), PromptActivity::class.java) + .addFlags(Intent.FLAG_ACTIVITY_NEW_TASK) + .setAction(ACTION_CONNECT) + .setData(Uri.Builder().scheme(TINC_SCHEME).opaquePart(netName).build())) + } - fun requestVpnPermission(netName: String) { + fun disconnect() { App.getContext().startActivity(Intent(App.getContext(), PromptActivity::class.java) .addFlags(Intent.FLAG_ACTIVITY_NEW_TASK) - .putExtra(INTENT_EXTRA_ACTION, Action.REQUEST_PERMISSION) - .putExtra(INTENT_EXTRA_NET_NAME, netName)) + .setAction(ACTION_DISCONNECT)) } + } } diff --git a/app/src/main/java/org/pacien/tincapp/activities/StartActivity.kt b/app/src/main/java/org/pacien/tincapp/activities/StartActivity.kt index 6e8ad37..e49e261 100644 --- a/app/src/main/java/org/pacien/tincapp/activities/StartActivity.kt +++ b/app/src/main/java/org/pacien/tincapp/activities/StartActivity.kt @@ -77,7 +77,7 @@ class StartActivity : BaseActivity(), AdapterView.OnItemClickListener, SwipeRefr } override fun onItemClick(parent: AdapterView<*>?, view: View?, position: Int, id: Long) = - PromptActivity.requestVpnPermission((view as TextView).text.toString()) + PromptActivity.connect((view as TextView).text.toString()) fun openConfigureActivity(@Suppress("UNUSED_PARAMETER") i: MenuItem) = startActivity(Intent(this, ConfigureActivity::class.java)) diff --git a/app/src/main/java/org/pacien/tincapp/intent/action/Actions.kt b/app/src/main/java/org/pacien/tincapp/intent/action/Actions.kt new file mode 100644 index 0000000..b210e14 --- /dev/null +++ b/app/src/main/java/org/pacien/tincapp/intent/action/Actions.kt @@ -0,0 +1,17 @@ +package org.pacien.tincapp.intent.action + +import org.pacien.tincapp.BuildConfig + +/** + * @author pacien + */ + +private val PREFIX = "${BuildConfig.APPLICATION_ID}.intent.action" + +val ACTION_CONNECT = "$PREFIX.CONNECT" +val ACTION_DISCONNECT = "$PREFIX.DISCONNECT" + +val ACTION_START_SERVICE = "$PREFIX.START_SERVICE" +val ACTION_STOP_SERVICE = "$PREFIX.STOP_SERVICE" + +val TINC_SCHEME = "tinc" 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 e2eae00..12ac17f 100644 --- a/app/src/main/java/org/pacien/tincapp/service/TincVpnService.kt +++ b/app/src/main/java/org/pacien/tincapp/service/TincVpnService.kt @@ -2,6 +2,7 @@ package org.pacien.tincapp.service import android.app.Service import android.content.Intent +import android.net.Uri import android.net.VpnService import android.os.ParcelFileDescriptor import org.pacien.tincapp.BuildConfig @@ -12,6 +13,9 @@ import org.pacien.tincapp.context.AppPaths import org.pacien.tincapp.data.VpnInterfaceConfiguration import org.pacien.tincapp.extensions.Java.applyIgnoringException import org.pacien.tincapp.extensions.VpnServiceBuilder.applyCfg +import org.pacien.tincapp.intent.action.ACTION_START_SERVICE +import org.pacien.tincapp.intent.action.ACTION_STOP_SERVICE +import org.pacien.tincapp.intent.action.TINC_SCHEME import java.io.IOException /** @@ -20,9 +24,9 @@ import java.io.IOException class TincVpnService : VpnService() { override fun onStartCommand(intent: Intent, flags: Int, startId: Int): Int { - when (intent.getSerializableExtra(INTENT_EXTRA_ACTION)) { - Action.START -> startVpn(intent.getStringExtra(INTENT_EXTRA_NET_NAME)!!) - Action.STOP -> onDestroy() + when (intent.action) { + ACTION_START_SERVICE -> startVpn(intent.data.schemeSpecificPart) + ACTION_STOP_SERVICE -> onDestroy() } return Service.START_STICKY @@ -64,11 +68,6 @@ class TincVpnService : VpnService() { companion object { - private val INTENT_EXTRA_ACTION = "action" - private val INTENT_EXTRA_NET_NAME = "netName" - - private enum class Action { START, STOP } - private var connected: Boolean = false private var netName: String? = null private var interfaceCfg: VpnInterfaceConfiguration? = null @@ -76,13 +75,13 @@ class TincVpnService : VpnService() { fun startVpn(netName: String) { App.getContext().startService(Intent(App.getContext(), TincVpnService::class.java) - .putExtra(INTENT_EXTRA_ACTION, Action.START) - .putExtra(TincVpnService.INTENT_EXTRA_NET_NAME, netName)) + .setAction(ACTION_START_SERVICE) + .setData(Uri.Builder().scheme(TINC_SCHEME).opaquePart(netName).build())) } fun stopVpn() { App.getContext().startService(Intent(App.getContext(), TincVpnService::class.java) - .putExtra(INTENT_EXTRA_ACTION, Action.STOP)) + .setAction(ACTION_STOP_SERVICE)) } fun getCurrentNetName() = netName -- cgit v1.2.3