aboutsummaryrefslogtreecommitdiff
path: root/app
diff options
context:
space:
mode:
authorpacien2017-08-18 10:27:25 +0200
committerpacien2017-08-18 10:27:25 +0200
commitac289a9e48e08c8cea7e301eb5f70b6aa025a1d6 (patch)
tree7383c704023ed70c3a6de2383edb69a06043397a /app
parent451869d60f1158a6fe54af7b59f72554908915ea (diff)
downloadtincapp-ac289a9e48e08c8cea7e301eb5f70b6aa025a1d6.tar.gz
Expose CONNECT and DISCONNECT intents
Diffstat (limited to 'app')
-rw-r--r--app/src/main/AndroidManifest.xml9
-rw-r--r--app/src/main/java/org/pacien/tincapp/activities/PromptActivity.kt36
-rw-r--r--app/src/main/java/org/pacien/tincapp/activities/StartActivity.kt2
-rw-r--r--app/src/main/java/org/pacien/tincapp/intent/action/Actions.kt17
-rw-r--r--app/src/main/java/org/pacien/tincapp/service/TincVpnService.kt21
5 files changed, 62 insertions, 23 deletions
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 @@
39 <activity 39 <activity
40 android:name=".activities.PromptActivity" 40 android:name=".activities.PromptActivity"
41 android:theme="@android:style/Theme.Translucent.NoTitleBar"> 41 android:theme="@android:style/Theme.Translucent.NoTitleBar">
42 <intent-filter>
43 <action android:name="org.pacien.tincapp.intent.action.CONNECT"/>
44 <data android:scheme="tinc"/>
45 <category android:name="android.intent.category.DEFAULT"/>
46 </intent-filter>
47 <intent-filter>
48 <action android:name="org.pacien.tincapp.intent.action.DISCONNECT"/>
49 <category android:name="android.intent.category.DEFAULT"/>
50 </intent-filter>
42 </activity> 51 </activity>
43 52
44 <service 53 <service
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
3import android.app.Activity 3import android.app.Activity
4import android.content.Intent 4import android.content.Intent
5import android.net.Uri
5import android.net.VpnService 6import android.net.VpnService
6import android.os.Bundle 7import android.os.Bundle
8import org.pacien.tincapp.BuildConfig
7import org.pacien.tincapp.context.App 9import org.pacien.tincapp.context.App
10import org.pacien.tincapp.intent.action.ACTION_CONNECT
11import org.pacien.tincapp.intent.action.ACTION_DISCONNECT
12import org.pacien.tincapp.intent.action.TINC_SCHEME
8import org.pacien.tincapp.service.TincVpnService 13import 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 @@
1package org.pacien.tincapp.intent.action
2
3import org.pacien.tincapp.BuildConfig
4
5/**
6 * @author pacien
7 */
8
9private val PREFIX = "${BuildConfig.APPLICATION_ID}.intent.action"
10
11val ACTION_CONNECT = "$PREFIX.CONNECT"
12val ACTION_DISCONNECT = "$PREFIX.DISCONNECT"
13
14val ACTION_START_SERVICE = "$PREFIX.START_SERVICE"
15val ACTION_STOP_SERVICE = "$PREFIX.STOP_SERVICE"
16
17val 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
3import android.app.Service 3import android.app.Service
4import android.content.Intent 4import android.content.Intent
5import android.net.Uri
5import android.net.VpnService 6import android.net.VpnService
6import android.os.ParcelFileDescriptor 7import android.os.ParcelFileDescriptor
7import org.pacien.tincapp.BuildConfig 8import org.pacien.tincapp.BuildConfig
@@ -12,6 +13,9 @@ import org.pacien.tincapp.context.AppPaths
12import org.pacien.tincapp.data.VpnInterfaceConfiguration 13import org.pacien.tincapp.data.VpnInterfaceConfiguration
13import org.pacien.tincapp.extensions.Java.applyIgnoringException 14import org.pacien.tincapp.extensions.Java.applyIgnoringException
14import org.pacien.tincapp.extensions.VpnServiceBuilder.applyCfg 15import org.pacien.tincapp.extensions.VpnServiceBuilder.applyCfg
16import org.pacien.tincapp.intent.action.ACTION_START_SERVICE
17import org.pacien.tincapp.intent.action.ACTION_STOP_SERVICE
18import org.pacien.tincapp.intent.action.TINC_SCHEME
15import java.io.IOException 19import java.io.IOException
16 20
17/** 21/**
@@ -20,9 +24,9 @@ import java.io.IOException
20class TincVpnService : VpnService() { 24class 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: VpnInterface