From 7dfc7b2fb2fb1006bf210ea6396c6e898e74d40e Mon Sep 17 00:00:00 2001 From: Pacien TRAN-GIRARD Date: Mon, 3 Jul 2017 22:33:51 +0200 Subject: List configured VPN on startup screen --- app/src/main/AndroidManifest.xml | 5 + .../pacien/tincapp/activities/PromptActivity.kt | 49 +++++++++ .../org/pacien/tincapp/activities/StartActivity.kt | 55 ++++------ .../org/pacien/tincapp/service/TincVpnService.kt | 8 +- app/src/main/res/layout/base.xml | 4 +- app/src/main/res/layout/fragment_list_item.xml | 9 ++ .../fragment_network_list_empty_placeholder.xml | 5 + .../res/layout/fragment_network_list_header.xml | 4 + app/src/main/res/layout/page_configure.xml | 113 +++++++++++---------- app/src/main/res/layout/page_start.xml | 40 +++----- app/src/main/res/values-ja/strings.xml | 4 - app/src/main/res/values-nb-rNO/strings.xml | 4 - app/src/main/res/values/strings.xml | 4 +- app/src/main/res/values/styles.xml | 4 + 14 files changed, 176 insertions(+), 132 deletions(-) create mode 100644 app/src/main/java/org/pacien/tincapp/activities/PromptActivity.kt create mode 100644 app/src/main/res/layout/fragment_list_item.xml create mode 100644 app/src/main/res/layout/fragment_network_list_empty_placeholder.xml create mode 100644 app/src/main/res/layout/fragment_network_list_header.xml (limited to 'app/src/main') 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 @@ android:theme="@style/AppTheme.NoActionBar"> + + + 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 @@ +package org.pacien.tincapp.activities + +import android.app.Activity +import android.content.Intent +import android.net.VpnService +import android.os.Bundle +import org.pacien.tincapp.context.App +import org.pacien.tincapp.service.TincVpnService + +/** + * @author pacien + */ +class PromptActivity : Activity() { + + override fun onCreate(savedInstanceState: Bundle?) { + super.onCreate(savedInstanceState) + + when (intent.getSerializableExtra(INTENT_EXTRA_ACTION) as Action) { + Action.REQUEST_PERMISSION -> requestVpnPermission() + } + } + + override fun onActivityResult(request: Int, result: Int, data: Intent?) { + if (result == Activity.RESULT_OK) TincVpnService.startVpn(intent.getStringExtra(INTENT_EXTRA_NET_NAME)) + finish() + } + + private fun requestVpnPermission() = VpnService.prepare(this).let { + if (it != null) + startActivityForResult(it, 0) + else + onActivityResult(0, Activity.RESULT_OK, Intent()) + } + + companion object { + private val INTENT_EXTRA_ACTION = "action" + private val INTENT_EXTRA_NET_NAME = "netName" + + private enum class Action { REQUEST_PERMISSION } + + fun requestVpnPermission(netName: String) { + 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)) + } + } + +} 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 @@ package org.pacien.tincapp.activities -import android.annotation.SuppressLint -import android.app.Activity import android.content.Intent -import android.net.VpnService import android.os.Bundle -import android.support.v7.app.AlertDialog import android.view.Menu import android.view.MenuItem import android.view.View -import android.view.ViewGroup -import android.widget.EditText -import android.widget.FrameLayout +import android.widget.AdapterView +import android.widget.ArrayAdapter +import android.widget.TextView import kotlinx.android.synthetic.main.base.* +import kotlinx.android.synthetic.main.page_start.* import org.pacien.tincapp.R -import org.pacien.tincapp.service.TincVpnService +import org.pacien.tincapp.context.AppPaths /** * @author pacien */ -class StartActivity : BaseActivity() { +class StartActivity : BaseActivity(), AdapterView.OnItemClickListener { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) layoutInflater.inflate(R.layout.page_start, main_content) + writeContent() } override fun onCreateOptionsMenu(m: Menu): Boolean { @@ -31,39 +29,20 @@ class StartActivity : BaseActivity() { return super.onCreateOptionsMenu(m) } - override fun onActivityResult(request: Int, result: Int, data: Intent?) { - notify(if (result == Activity.RESULT_OK) R.string.message_vpn_permissions_granted else R.string.message_vpn_permissions_denied) + override fun onItemClick(parent: AdapterView<*>?, view: View?, position: Int, id: Long) { + PromptActivity.requestVpnPermission((view as TextView).text.toString()) } - fun requestVpnPermission(@Suppress("UNUSED_PARAMETER") v: View) { - val askPermIntent = VpnService.prepare(this) - - if (askPermIntent != null) - startActivityForResult(askPermIntent, 0) - else - onActivityResult(0, Activity.RESULT_OK, null) + fun openConfigureActivity(@Suppress("UNUSED_PARAMETER") i: MenuItem) { + startActivity(Intent(this, ConfigureActivity::class.java)) } - fun startVpnDialog(@Suppress("UNUSED_PARAMETER") v: View) { - val i = EditText(this) - i.layoutParams = FrameLayout.LayoutParams(FrameLayout.LayoutParams.MATCH_PARENT, FrameLayout.LayoutParams.MATCH_PARENT) - i.setHint(R.string.field_net_name) - - @SuppressLint("InflateParams") - val vg = layoutInflater.inflate(R.layout.dialog_frame, main_content, false) as ViewGroup - vg.addView(i) - - AlertDialog.Builder(this) - .setTitle(R.string.title_connect_to_network) - .setView(vg) - .setPositiveButton(R.string.action_connect) { _, _ -> startVpn(i.text.toString()) } - .setNegativeButton(R.string.action_close) { _, _ -> /* nop */ } - .show() + private fun writeContent() { + network_list.addHeaderView(layoutInflater.inflate(R.layout.fragment_network_list_header, network_list, false)) + network_list.addFooterView(View(this)) + network_list.emptyView = layoutInflater.inflate(R.layout.fragment_network_list_empty_placeholder, network_list, false) + network_list.adapter = ArrayAdapter(this, R.layout.fragment_list_item, AppPaths.confDir().list()) + network_list.onItemClickListener = this } - fun openConfigureActivity(@Suppress("UNUSED_PARAMETER") i: MenuItem) = startActivity(Intent(this, ConfigureActivity::class.java)) - - private fun startVpn(netName: String) = - startService(Intent(this, TincVpnService::class.java).putExtra(TincVpnService.INTENT_EXTRA_NET_NAME, netName)) - } 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 import org.pacien.tincapp.BuildConfig import org.pacien.tincapp.commands.Tinc import org.pacien.tincapp.commands.Tincd +import org.pacien.tincapp.context.App import org.pacien.tincapp.context.AppPaths import org.pacien.tincapp.utils.applyIgnoringException import java.io.IOException @@ -43,7 +44,12 @@ class TincVpnService : VpnService() { } companion object { - val INTENT_EXTRA_NET_NAME = "netName" + private val INTENT_EXTRA_NET_NAME = "netName" + + fun startVpn(netName: String) { + App.getContext().startService(Intent(App.getContext(), TincVpnService::class.java) + .putExtra(TincVpnService.INTENT_EXTRA_NET_NAME, netName)) + } } } diff --git a/app/src/main/res/layout/base.xml b/app/src/main/res/layout/base.xml index f2a8f49..997dd01 100644 --- a/app/src/main/res/layout/base.xml +++ b/app/src/main/res/layout/base.xml @@ -26,12 +26,12 @@ - - + diff --git a/app/src/main/res/layout/fragment_list_item.xml b/app/src/main/res/layout/fragment_list_item.xml new file mode 100644 index 0000000..c6049f5 --- /dev/null +++ b/app/src/main/res/layout/fragment_list_item.xml @@ -0,0 +1,9 @@ + + + diff --git a/app/src/main/res/layout/fragment_network_list_empty_placeholder.xml b/app/src/main/res/layout/fragment_network_list_empty_placeholder.xml new file mode 100644 index 0000000..169942f --- /dev/null +++ b/app/src/main/res/layout/fragment_network_list_empty_placeholder.xml @@ -0,0 +1,5 @@ + diff --git a/app/src/main/res/layout/fragment_network_list_header.xml b/app/src/main/res/layout/fragment_network_list_header.xml new file mode 100644 index 0000000..3531e3e --- /dev/null +++ b/app/src/main/res/layout/fragment_network_list_header.xml @@ -0,0 +1,4 @@ + diff --git a/app/src/main/res/layout/page_configure.xml b/app/src/main/res/layout/page_configure.xml index bc8832e..35a7416 100644 --- a/app/src/main/res/layout/page_configure.xml +++ b/app/src/main/res/layout/page_configure.xml @@ -1,81 +1,88 @@ - + - + - + - + - + - + - + - + - + - + - + + + - + - + - + + + - + - + - + - + - + - + - + - + - + diff --git a/app/src/main/res/layout/page_start.xml b/app/src/main/res/layout/page_start.xml index d470dc0..fec55de 100644 --- a/app/src/main/res/layout/page_start.xml +++ b/app/src/main/res/layout/page_start.xml @@ -1,27 +1,13 @@ - - - - -