aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPacien TRAN-GIRARD2017-07-03 22:33:51 +0200
committerPacien TRAN-GIRARD2017-07-03 22:33:51 +0200
commit7dfc7b2fb2fb1006bf210ea6396c6e898e74d40e (patch)
treea0eb66ec7beb9a3c677a72eecc17ce7d9eb3c6ff
parentab577b06a6eaa9b8dd950b79b658ed8c08eef1c9 (diff)
downloadtincapp-7dfc7b2fb2fb1006bf210ea6396c6e898e74d40e.tar.gz
List configured VPN on startup screen
-rw-r--r--app/src/main/AndroidManifest.xml5
-rw-r--r--app/src/main/java/org/pacien/tincapp/activities/PromptActivity.kt49
-rw-r--r--app/src/main/java/org/pacien/tincapp/activities/StartActivity.kt55
-rw-r--r--app/src/main/java/org/pacien/tincapp/service/TincVpnService.kt8
-rw-r--r--app/src/main/res/layout/base.xml4
-rw-r--r--app/src/main/res/layout/fragment_list_item.xml9
-rw-r--r--app/src/main/res/layout/fragment_network_list_empty_placeholder.xml5
-rw-r--r--app/src/main/res/layout/fragment_network_list_header.xml4
-rw-r--r--app/src/main/res/layout/page_configure.xml113
-rw-r--r--app/src/main/res/layout/page_start.xml40
-rw-r--r--app/src/main/res/values-ja/strings.xml4
-rw-r--r--app/src/main/res/values-nb-rNO/strings.xml4
-rw-r--r--app/src/main/res/values/strings.xml4
-rw-r--r--app/src/main/res/values/styles.xml4
14 files changed, 176 insertions, 132 deletions
diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml
index 4866800..a3f5569 100644
--- a/app/src/main/AndroidManifest.xml
+++ b/app/src/main/AndroidManifest.xml
@@ -29,6 +29,11 @@
29 android:theme="@style/AppTheme.NoActionBar"> 29 android:theme="@style/AppTheme.NoActionBar">
30 </activity> 30 </activity>
31 31
32 <activity
33 android:name=".activities.PromptActivity"
34 android:theme="@android:style/Theme.Translucent.NoTitleBar">
35 </activity>
36
32 <service 37 <service
33 android:name="org.pacien.tincapp.service.TincVpnService" 38 android:name="org.pacien.tincapp.service.TincVpnService"
34 android:permission="android.permission.BIND_VPN_SERVICE"> 39 android:permission="android.permission.BIND_VPN_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
new file mode 100644
index 0000000..6906ead
--- /dev/null
+++ b/app/src/main/java/org/pacien/tincapp/activities/PromptActivity.kt
@@ -0,0 +1,49 @@
1package org.pacien.tincapp.activities
2
3import android.app.Activity
4import android.content.Intent
5import android.net.VpnService
6import android.os.Bundle
7import org.pacien.tincapp.context.App
8import org.pacien.tincapp.service.TincVpnService
9
10/**
11 * @author pacien
12 */
13class PromptActivity : Activity() {
14
15 override fun onCreate(savedInstanceState: Bundle?) {
16 super.onCreate(savedInstanceState)
17
18 when (intent.getSerializableExtra(INTENT_EXTRA_ACTION) as Action) {
19 Action.REQUEST_PERMISSION -> requestVpnPermission()
20 }
21 }
22
23 override fun onActivityResult(request: Int, result: Int, data: Intent?) {
24 if (result == Activity.RESULT_OK) TincVpnService.startVpn(intent.getStringExtra(INTENT_EXTRA_NET_NAME))
25 finish()
26 }
27
28 private fun requestVpnPermission() = VpnService.prepare(this).let {
29 if (it != null)
30 startActivityForResult(it, 0)
31 else
32 onActivityResult(0, Activity.RESULT_OK, Intent())
33 }
34
35 companion object {
36 private val INTENT_EXTRA_ACTION = "action"
37 private val INTENT_EXTRA_NET_NAME = "netName"
38
39 private enum class Action { REQUEST_PERMISSION }
40
41 fun requestVpnPermission(netName: String) {
42 App.getContext().startActivity(Intent(App.getContext(), PromptActivity::class.java)
43 .addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
44 .putExtra(INTENT_EXTRA_ACTION, Action.REQUEST_PERMISSION)
45 .putExtra(INTENT_EXTRA_NET_NAME, netName))
46 }
47 }
48
49}
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 1c4b5a1..28d0a29 100644
--- a/app/src/main/java/org/pacien/tincapp/activities/StartActivity.kt
+++ b/app/src/main/java/org/pacien/tincapp/activities/StartActivity.kt
@@ -1,29 +1,27 @@
1package org.pacien.tincapp.activities 1package org.pacien.tincapp.activities
2 2
3import android.annotation.SuppressLint
4import android.app.Activity
5import android.content.Intent 3import android.content.Intent
6import android.net.VpnService
7import android.os.Bundle 4import android.os.Bundle
8import android.support.v7.app.AlertDialog
9import android.view.Menu 5import android.view.Menu
10import android.view.MenuItem 6import android.view.MenuItem
11import android.view.View 7import android.view.View
12import android.view.ViewGroup 8import android.widget.AdapterView
13import android.widget.EditText 9import android.widget.ArrayAdapter
14import android.widget.FrameLayout 10import android.widget.TextView
15import kotlinx.android.synthetic.main.base.* 11import kotlinx.android.synthetic.main.base.*
12import kotlinx.android.synthetic.main.page_start.*
16import org.pacien.tincapp.R 13import org.pacien.tincapp.R
17import org.pacien.tincapp.service.TincVpnService 14import org.pacien.tincapp.context.AppPaths
18 15
19/** 16/**
20 * @author pacien 17 * @author pacien
21 */ 18 */
22class StartActivity : BaseActivity() { 19class StartActivity : BaseActivity(), AdapterView.OnItemClickListener {
23 20
24 override fun onCreate(savedInstanceState: Bundle?) { 21 override fun onCreate(savedInstanceState: Bundle?) {
25 super.onCreate(savedInstanceState) 22 super.onCreate(savedInstanceState)
26 layoutInflater.inflate(R.layout.page_start, main_content) 23 layoutInflater.inflate(R.layout.page_start, main_content)
24 writeContent()
27 } 25 }
28 26
29 override fun onCreateOptionsMenu(m: Menu): Boolean { 27 override fun onCreateOptionsMenu(m: Menu): Boolean {
@@ -31,39 +29,20 @@ class StartActivity : BaseActivity() {
31 return super.onCreateOptionsMenu(m) 29 return super.onCreateOptionsMenu(m)
32 } 30 }
33 31
34 override fun onActivityResult(request: Int, result: Int, data: Intent?) { 32 override fun onItemClick(parent: AdapterView<*>?, view: View?, position: Int, id: Long) {
35 notify(if (result == Activity.RESULT_OK) R.string.message_vpn_permissions_granted else R.string.message_vpn_permissions_denied) 33 PromptActivity.requestVpnPermission((view as TextView).text.toString())
36 } 34 }
37 35
38 fun requestVpnPermission(@Suppress("UNUSED_PARAMETER") v: View) { 36 fun openConfigureActivity(@Suppress("UNUSED_PARAMETER") i: MenuItem) {
39 val askPermIntent = VpnService.prepare(this) 37 startActivity(Intent(this, ConfigureActivity::class.java))
40
41 if (askPermIntent != null)
42 startActivityForResult(askPermIntent, 0)
43 else
44 onActivityResult(0, Activity.RESULT_OK, null)
45 } 38 }
46 39
47 fun startVpnDialog(@Suppress("UNUSED_PARAMETER") v: View) { 40 private fun writeContent() {
48 val i = EditText(this) 41 network_list.addHeaderView(layoutInflater.inflate(R.layout.fragment_network_list_header, network_list, false))
49 i.layoutParams = FrameLayout.LayoutParams(FrameLayout.LayoutParams.MATCH_PARENT, FrameLayout.LayoutParams.MATCH_PARENT) 42 network_list.addFooterView(View(this))
50 i.setHint(R.string.field_net_name) 43 network_list.emptyView = layoutInflater.inflate(R.layout.fragment_network_list_empty_placeholder, network_list, false)
51 44 network_list.adapter = ArrayAdapter<String>(this, R.layout.fragment_list_item, AppPaths.confDir().list())
52 @SuppressLint("InflateParams") 45 network_list.onItemClickListener = this
53 val vg = layoutInflater.inflate(R.layout.dialog_frame, main_content, false) as ViewGroup
54 vg.addView(i)
55
56 AlertDialog.Builder(this)
57 .setTitle(R.string.title_connect_to_network)
58 .setView(vg)
59 .setPositiveButton(R.string.action_connect) { _, _ -> startVpn(i.text.toString()) }
60 .setNegativeButton(R.string.action_close) { _, _ -> /* nop */ }
61 .show()
62 } 46 }
63 47
64 fun openConfigureActivity(@Suppress("UNUSED_PARAMETER") i: MenuItem) = startActivity(Intent(this, ConfigureActivity::class.java))
65
66 private fun startVpn(netName: String) =
67 startService(Intent(this, TincVpnService::class.java).putExtra(TincVpnService.INTENT_EXTRA_NET_NAME, netName))
68
69} 48}
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 31541b3..59d9e34 100644
--- a/app/src/main/java/org/pacien/tincapp/service/TincVpnService.kt
+++ b/app/src/main/java/org/pacien/tincapp/service/TincVpnService.kt
@@ -6,6 +6,7 @@ import android.net.VpnService
6import org.pacien.tincapp.BuildConfig 6import org.pacien.tincapp.BuildConfig
7import org.pacien.tincapp.commands.Tinc 7import org.pacien.tincapp.commands.Tinc
8import org.pacien.tincapp.commands.Tincd 8import org.pacien.tincapp.commands.Tincd
9import org.pacien.tincapp.context.App
9import org.pacien.tincapp.context.AppPaths 10import org.pacien.tincapp.context.AppPaths
10import org.pacien.tincapp.utils.applyIgnoringException 11import org.pacien.tincapp.utils.applyIgnoringException
11import java.io.IOException 12import java.io.IOException
@@ -43,7 +44,12 @@ class TincVpnService : VpnService() {
43 } 44 }