diff options
author | pacien | 2018-08-20 16:52:49 +0200 |
---|---|---|
committer | pacien | 2018-08-20 16:52:49 +0200 |
commit | 957826db1e46bffcd99ee52f21101728279521b9 (patch) | |
tree | 4fb7b0a511c9b50dc8f174cd272c8719fcee380b /app/src/main/java/org/pacien/tincapp/activities/start/ConnectionStarter.kt | |
parent | d6ea28ffe132807390721912f2310fd3f43d0405 (diff) | |
download | tincapp-957826db1e46bffcd99ee52f21101728279521b9.tar.gz |
Refactor start activity
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.kt | 69 |
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 | |||
19 | package org.pacien.tincapp.activities.start | ||
20 | |||
21 | import android.net.VpnService | ||
22 | import android.support.v7.app.AlertDialog | ||
23 | import kotlinx.android.synthetic.main.base.* | ||
24 | import kotlinx.android.synthetic.main.dialog_decrypt_keys.view.* | ||
25 | import org.pacien.tincapp.R | ||
26 | import org.pacien.tincapp.service.TincVpnService | ||
27 | import org.pacien.tincapp.utils.TincKeyring | ||
28 | |||
29 | /** | ||
30 | * @author pacien | ||
31 | */ | ||
32 | class 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 | } | ||