aboutsummaryrefslogtreecommitdiff
path: root/app/src/main/java/org/pacien/tincapp/activities/start/ConnectionStarter.kt
diff options
context:
space:
mode:
Diffstat (limited to 'app/src/main/java/org/pacien/tincapp/activities/start/ConnectionStarter.kt')
-rw-r--r--app/src/main/java/org/pacien/tincapp/activities/start/ConnectionStarter.kt69
1 files changed, 69 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}