diff options
Diffstat (limited to 'app/src/main/java')
4 files changed, 53 insertions, 23 deletions
diff --git a/app/src/main/java/org/pacien/tincapp/activities/PromptActivity.kt b/app/src/main/java/org/pacien/tincapp/activities/PromptActivity.kt index 6906ead..6310d63 100644 --- a/app/src/main/java/org/pacien/tincapp/activities/PromptActivity.kt +++ b/app/src/main/java/org/pacien/tincapp/activities/PromptActivity.kt | |||
@@ -2,9 +2,14 @@ package org.pacien.tincapp.activities | |||
2 | 2 | ||
3 | import android.app.Activity | 3 | import android.app.Activity |
4 | import android.content.Intent | 4 | import android.content.Intent |
5 | import android.net.Uri | ||
5 | import android.net.VpnService | 6 | import android.net.VpnService |
6 | import android.os.Bundle | 7 | import android.os.Bundle |
8 | import org.pacien.tincapp.BuildConfig | ||
7 | import org.pacien.tincapp.context.App | 9 | import org.pacien.tincapp.context.App |
10 | import org.pacien.tincapp.intent.action.ACTION_CONNECT | ||
11 | import org.pacien.tincapp.intent.action.ACTION_DISCONNECT | ||
12 | import org.pacien.tincapp.intent.action.TINC_SCHEME | ||
8 | import org.pacien.tincapp.service.TincVpnService | 13 | import org.pacien.tincapp.service.TincVpnService |
9 | 14 | ||
10 | /** | 15 | /** |
@@ -15,35 +20,44 @@ class PromptActivity : Activity() { | |||
15 | override fun onCreate(savedInstanceState: Bundle?) { | 20 | override fun onCreate(savedInstanceState: Bundle?) { |
16 | super.onCreate(savedInstanceState) | 21 | super.onCreate(savedInstanceState) |
17 | 22 | ||
18 | when (intent.getSerializableExtra(INTENT_EXTRA_ACTION) as Action) { | 23 | when (intent.action) { |
19 | Action.REQUEST_PERMISSION -> requestVpnPermission() | 24 | ACTION_CONNECT -> connect() |
25 | ACTION_DISCONNECT -> disconnect() | ||
20 | } | 26 | } |
21 | } | 27 | } |
22 | 28 | ||
23 | override fun onActivityResult(request: Int, result: Int, data: Intent?) { | 29 | override fun onActivityResult(request: Int, result: Int, data: Intent?) { |
24 | if (result == Activity.RESULT_OK) TincVpnService.startVpn(intent.getStringExtra(INTENT_EXTRA_NET_NAME)) | 30 | if (result == Activity.RESULT_OK) TincVpnService.startVpn(intent.data.schemeSpecificPart) |
25 | finish() | 31 | finish() |
26 | } | 32 | } |
27 | 33 | ||
28 | private fun requestVpnPermission() = VpnService.prepare(this).let { | 34 | private fun connect() = VpnService.prepare(this).let { |
29 | if (it != null) | 35 | if (it != null) |
30 | startActivityForResult(it, 0) | 36 | startActivityForResult(it, 0) |
31 | else | 37 | else |
32 | onActivityResult(0, Activity.RESULT_OK, Intent()) | 38 | onActivityResult(0, Activity.RESULT_OK, null) |
39 | } | ||
40 | |||
41 | private fun disconnect() { | ||
42 | TincVpnService.stopVpn() | ||
43 | finish() | ||
33 | } | 44 | } |
34 | 45 | ||
35 | companion object { | 46 | companion object { |
36 | private val INTENT_EXTRA_ACTION = "action" | ||
37 | private val INTENT_EXTRA_NET_NAME = "netName" | ||
38 | 47 | ||
39 | private enum class Action { REQUEST_PERMISSION } | 48 | fun connect(netName: String) { |
49 | App.getContext().startActivity(Intent(App.getContext(), PromptActivity::class.java) | ||
50 | .addFlags(Intent.FLAG_ACTIVITY_NEW_TASK) | ||
51 | .setAction(ACTION_CONNECT) | ||
52 | .setData(Uri.Builder().scheme(TINC_SCHEME).opaquePart(netName).build())) | ||
53 | } | ||
40 | 54 | ||
41 | fun requestVpnPermission(netName: String) { | 55 | fun disconnect() { |
42 | App.getContext().startActivity(Intent(App.getContext(), PromptActivity::class.java) | 56 | App.getContext().startActivity(Intent(App.getContext(), PromptActivity::class.java) |
43 | .addFlags(Intent.FLAG_ACTIVITY_NEW_TASK) | 57 | .addFlags(Intent.FLAG_ACTIVITY_NEW_TASK) |
44 | .putExtra(INTENT_EXTRA_ACTION, Action.REQUEST_PERMISSION) | 58 | .setAction(ACTION_DISCONNECT)) |
45 | .putExtra(INTENT_EXTRA_NET_NAME, netName)) | ||
46 | } | 59 | } |
60 | |||
47 | } | 61 | } |
48 | 62 | ||
49 | } | 63 | } |
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 | |||
77 | } | 77 | } |
78 | 78 | ||
79 | override fun onItemClick(parent: AdapterView<*>?, view: View?, position: Int, id: Long) = | 79 | override fun onItemClick(parent: AdapterView<*>?, view: View?, position: Int, id: Long) = |
80 | PromptActivity.requestVpnPermission((view as TextView).text.toString()) | 80 | PromptActivity.connect((view as TextView).text.toString()) |
81 | 81 | ||
82 | fun openConfigureActivity(@Suppress("UNUSED_PARAMETER") i: MenuItem) = | 82 | fun openConfigureActivity(@Suppress("UNUSED_PARAMETER") i: MenuItem) = |
83 | startActivity(Intent(this, ConfigureActivity::class.java)) | 83 | 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 @@ | |||
1 | package org.pacien.tincapp.intent.action | ||
2 | |||
3 | import org.pacien.tincapp.BuildConfig | ||
4 | |||
5 | /** | ||
6 | * @author pacien | ||
7 | */ | ||
8 | |||
9 | private val PREFIX = "${BuildConfig.APPLICATION_ID}.intent.action" | ||
10 | |||
11 | val ACTION_CONNECT = "$PREFIX.CONNECT" | ||
12 | val ACTION_DISCONNECT = "$PREFIX.DISCONNECT" | ||
13 | |||
14 | val ACTION_START_SERVICE = "$PREFIX.START_SERVICE" | ||
15 | val ACTION_STOP_SERVICE = "$PREFIX.STOP_SERVICE" | ||
16 | |||
17 | 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 | |||
2 | 2 | ||
3 | import android.app.Service | 3 | import android.app.Service |
4 | import android.content.Intent | 4 | import android.content.Intent |
5 | import android.net.Uri | ||
5 | import android.net.VpnService | 6 | import android.net.VpnService |
6 | import android.os.ParcelFileDescriptor | 7 | import android.os.ParcelFileDescriptor |
7 | import org.pacien.tincapp.BuildConfig | 8 | import org.pacien.tincapp.BuildConfig |
@@ -12,6 +13,9 @@ import org.pacien.tincapp.context.AppPaths | |||
12 | import org.pacien.tincapp.data.VpnInterfaceConfiguration | 13 | import org.pacien.tincapp.data.VpnInterfaceConfiguration |
13 | import org.pacien.tincapp.extensions.Java.applyIgnoringException | 14 | import org.pacien.tincapp.extensions.Java.applyIgnoringException |
14 | import org.pacien.tincapp.extensions.VpnServiceBuilder.applyCfg | 15 | import org.pacien.tincapp.extensions.VpnServiceBuilder.applyCfg |
16 | import org.pacien.tincapp.intent.action.ACTION_START_SERVICE | ||
17 | import org.pacien.tincapp.intent.action.ACTION_STOP_SERVICE | ||
18 | import org.pacien.tincapp.intent.action.TINC_SCHEME | ||
15 | import java.io.IOException | 19 | import java.io.IOException |
16 | 20 | ||
17 | /** | 21 | /** |
@@ -20,9 +24,9 @@ import java.io.IOException | |||
20 | class TincVpnService : VpnService() { | 24 | class TincVpnService : VpnService() { |
21 | 25 | ||
22 | override fun onStartCommand(intent: Intent, flags: Int, startId: Int): Int { | 26 | override fun onStartCommand(intent: Intent, flags: Int, startId: Int): Int { |
23 | when (intent.getSerializableExtra(INTENT_EXTRA_ACTION)) { | 27 | when (intent.action) { |
24 | Action.START -> startVpn(intent.getStringExtra(INTENT_EXTRA_NET_NAME)!!) | 28 | ACTION_START_SERVICE -> startVpn(intent.data.schemeSpecificPart) |
25 | Action.STOP -> onDestroy() | 29 | ACTION_STOP_SERVICE -> onDestroy() |
26 | } | 30 | } |
27 | 31 | ||
28 | return Service.START_STICKY | 32 | return Service.START_STICKY |
@@ -64,11 +68,6 @@ class TincVpnService : VpnService() { | |||
64 | 68 | ||
65 | companion object { | 69 | companion object { |
66 | 70 | ||
67 | private val INTENT_EXTRA_ACTION = "action" | ||
68 | private val INTENT_EXTRA_NET_NAME = "netName" | ||
69 | |||
70 | private enum class Action { START, STOP } | ||
71 | |||
72 | private var connected: Boolean = false | 71 | private var connected: Boolean = false |
73 | private var netName: String? = null | 72 | private var netName: String? = null |
74 | private var interfaceCfg: VpnInterfaceConfiguration? = null | 73 | private var interfaceCfg: VpnInterfaceConfiguration? = null |
@@ -76,13 +75,13 @@ class TincVpnService : VpnService() { | |||
76 | 75 | ||
77 | fun startVpn(netName: String) { | 76 | fun startVpn(netName: String) { |
78 | App.getContext().startService(Intent(App.getContext(), TincVpnService::class.java) | 77 | App.getContext().startService(Intent(App.getContext(), TincVpnService::class.java) |
79 | .putExtra(INTENT_EXTRA_ACTION, Action.START) | 78 | .setAction(ACTION_START_SERVICE) |
80 | .putExtra(TincVpnService.INTENT_EXTRA_NET_NAME, netName)) | 79 | .setData(Uri.Builder().scheme(TINC_SCHEME).opaquePart(netName).build())) |
81 | } | 80 | } |
82 | 81 | ||
83 | fun stopVpn() { | 82 | fun stopVpn() { |
84 | App.getContext().startService(Intent(App.getContext(), TincVpnService::class.java) | 83 | App.getContext().startService(Intent(App.getContext(), TincVpnService::class.java) |
85 | .putExtra(INTENT_EXTRA_ACTION, Action.STOP)) | 84 | .setAction(ACTION_STOP_SERVICE)) |
86 | } | 85 | } |
87 | 86 | ||
88 | fun getCurrentNetName() = netName | 87 | fun getCurrentNetName() = net |