aboutsummaryrefslogtreecommitdiff
path: root/app/src/main/java/org/pacien/tincapp/activities/start
diff options
context:
space:
mode:
Diffstat (limited to 'app/src/main/java/org/pacien/tincapp/activities/start')
-rw-r--r--app/src/main/java/org/pacien/tincapp/activities/start/ConnectionStarter.kt69
-rw-r--r--app/src/main/java/org/pacien/tincapp/activities/start/NetworkListFragment.kt84
-rw-r--r--app/src/main/java/org/pacien/tincapp/activities/start/NetworkListLiveData.kt46
-rw-r--r--app/src/main/java/org/pacien/tincapp/activities/start/NetworkListViewModel.kt28
-rw-r--r--app/src/main/java/org/pacien/tincapp/activities/start/StartActivity.kt117
5 files changed, 344 insertions, 0 deletions
diff --git a/app/src/main/java/org/pacien/tincapp/activities/start/ConnectionStarter.kt b/app/src/main/java/org/pacien/tincapp/activities/start/ConnectionStarter.kt
new file mode 100644
index 0000000..9e7e59d
--- /dev/null
+++ b/app/src/main/java/org/pacien/tincapp/activities/start/ConnectionStarter.kt
@@ -0,0 +1,69 @@
1/*
2 * Tinc App, an Android binding and user interface for the tinc mesh VPN daemon
3 * Copyright (C) 2017-2018 Pacien TRAN-GIRARD
4 *
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <https://www.gnu.org/licenses/>.
17 */
18
19package org.pacien.tincapp.activities.start
20
21import android.net.VpnService
22import android.support.v7.app.AlertDialog
23import kotlinx.android.synthetic.main.base.*
24import kotlinx.android.synthetic.main.dialog_decrypt_keys.view.*
25import org.pacien.tincapp.R
26import org.pacien.tincapp.service.TincVpnService
27import org.pacien.tincapp.utils.TincKeyring
28
29/**
30 * @author pacien
31 */
32class ConnectionStarter(private val parentActivity: StartActivity) {
33 private var netName: String? = null
34 private var passphrase: String? = null
35 private var displayStatus = false
36
37 fun displayStatus() = displayStatus
38
39 fun tryStart(netName: String? = null, passphrase: String? = null, displayStatus: Boolean? = null) {
40 if (netName != null) this.netName = netName
41 this.passphrase = passphrase
42 if (displayStatus != null) this.displayStatus = displayStatus
43
44 val permissionRequestIntent = VpnService.prepare(parentActivity)
45 if (permissionRequestIntent != null)
46 return parentActivity.startActivityForResult(permissionRequestIntent, parentActivity.permissionRequestCode)
47
48 if (TincKeyring.needsPassphrase(this.netName!!) && this.passphrase == null)
49 return askForPassphrase()
50
51 startVpn(this.netName!!, this.passphrase)
52 }
53
54 private fun askForPassphrase() {
55 val dialogView = parentActivity.layoutInflater.inflate(R.layout.dialog_decrypt_keys, parentActivity.main_content, false)
56
57 AlertDialog.Builder(parentActivity)
58 .setTitle(R.string.title_unlock_private_keys)
59 .setView(dialogView)
60 .setPositiveButton(R.string.action_unlock) { _, _ -> tryStart(passphrase = dialogView.passphrase.text.toString()) }
61 .setNegativeButton(R.string.action_cancel) { _, _ -> Unit }
62 .show()
63 }
64
65 private fun startVpn(netName: String, passphrase: String? = null) {
66 parentActivity.showConnectProgressDialog()
67 TincVpnService.connect(netName, passphrase)
68 }
69}
diff --git a/app/src/main/java/org/pacien/tincapp/activities/start/NetworkListFragment.kt b/app/src/main/java/org/pacien/tincapp/activities/start/NetworkListFragment.kt
new file mode 100644
index 0000000..f255f53
--- /dev/null
+++ b/app/src/main/java/org/pacien/tincapp/activities/start/NetworkListFragment.kt
@@ -0,0 +1,84 @@
1/*
2 * Tinc App, an Android binding and user interface for the tinc mesh VPN daemon
3 * Copyright (C) 2017-2018 Pacien TRAN-GIRARD
4 *
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <https://www.gnu.org/licenses/>.
17 */
18
19package org.pacien.tincapp.activities.start
20
21import android.arch.lifecycle.Observer
22import android.os.Bundle
23import android.support.v4.app.Fragment
24import android.view.LayoutInflater
25import android.view.View
26import android.view.ViewGroup
27import android.widget.AdapterView
28import android.widget.ArrayAdapter
29import android.widget.TextView
30import kotlinx.android.synthetic.main.start_network_list.*
31import org.pacien.tincapp.R
32import org.pacien.tincapp.context.AppPaths
33import org.pacien.tincapp.extensions.hideBottomSeparator
34import org.pacien.tincapp.extensions.setElements
35
36/**
37 * @author pacien
38 */
39class NetworkListFragment : Fragment() {
40 private val appPaths = AppPaths
41 private val networkListViewModel by lazy { NetworkListViewModel() }
42 private val networkListAdapter by lazy { ArrayAdapter<String>(context, R.layout.start_network_list_item) }
43 var connectToNetworkAction = { _: String -> Unit }
44
45 override fun onCreate(savedInstanceState: Bundle?) {
46 super.onCreate(savedInstanceState)
47 networkListViewModel.networkList.observe(this, Observer { updateNetworkList(it.orEmpty()) })
48 }
49
50 override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
51 return inflater.inflate(R.layout.start_network_list, container, false)
52 }
53
54 override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
55 val listHeaderView = layoutInflater.inflate(R.layout.start_network_list_header, start_network_list, false)
56 start_network_list.addHeaderView(listHeaderView, null, false)
57 start_network_list.hideBottomSeparator()
58 start_network_list.emptyView = start_network_list_placeholder
59 start_network_list.onItemClickListener = AdapterView.OnItemClickListener(this::onItemClick)
60 start_network_list.adapter = networkListAdapter
61 }
62
63 @Suppress("UNUSED_PARAMETER")
64 private fun onItemClick(parent: AdapterView<*>?, view: View?, position: Int, id: Long) = when (view) {
65 is TextView -> connectToNetworkAction(view.text.toString())
66 else -> Unit
67 }
68
69 private fun updateNetworkList(networks: List<String>) {
70 networkListAdapter.setElements(networks)
71 if (networks.isEmpty()) updatePlaceholder()
72 }
73
74 private fun updatePlaceholder() {
75 val placeholderTextResource = when (appPaths.storageAvailable()) {
76 true -> R.string.start_network_list_empty_none_found
77 false -> R.string.start_network_list_empty_storage_not_available
78 }
79
80 start_network_list_placeholder.post {
81 start_network_list_placeholder_text.text = getString(placeholderTextResource)
82 }
83 }
84}
diff --git a/app/src/main/java/org/pacien/tincapp/activities/start/NetworkListLiveData.kt b/app/src/main/java/org/pacien/tincapp/activities/start/NetworkListLiveData.kt
new file mode 100644
index 0000000..d0d39b8
--- /dev/null
+++ b/app/src/main/java/org/pacien/tincapp/activities/start/NetworkListLiveData.kt
@@ -0,0 +1,46 @@
1/*
2 * Tinc App, an Android binding and user interface for the tinc mesh VPN daemon
3 * Copyright (C) 2017-2018 Pacien TRAN-GIRARD
4 *
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <https://www.gnu.org/licenses/>.
17 */
18
19package org.pacien.tincapp.activities.start
20
21import android.arch.lifecycle.LiveData
22import org.pacien.tincapp.context.AppPaths
23import java.util.*
24import kotlin.concurrent.timer
25
26/**
27 * @author pacien
28 */
29class NetworkListLiveData : LiveData<List<String>>() {
30 private val updateInterval = 2 * 1000L // in milliseconds
31 private val appPaths = AppPaths
32 private lateinit var updateTimer: Timer