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 | |
parent | d6ea28ffe132807390721912f2310fd3f43d0405 (diff) | |
download | tincapp-957826db1e46bffcd99ee52f21101728279521b9.tar.gz |
Refactor start activity
23 files changed, 453 insertions, 250 deletions
diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index 054438e..70839b7 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml | |||
@@ -34,7 +34,7 @@ | |||
34 | tools:ignore="GoogleAppIndexingWarning"> | 34 | tools:ignore="GoogleAppIndexingWarning"> |
35 | 35 | ||
36 | <activity | 36 | <activity |
37 | android:name=".activities.StartActivity" | 37 | android:name=".activities.start.StartActivity" |
38 | android:configChanges="orientation|screenSize" | 38 | android:configChanges="orientation|screenSize" |
39 | android:label="@string/app_name"> | 39 | android:label="@string/app_name"> |
40 | <intent-filter> | 40 | <intent-filter> |
@@ -51,7 +51,7 @@ | |||
51 | <activity | 51 | <activity |
52 | android:name=".activities.configure.ConfigureActivity" | 52 | android:name=".activities.configure.ConfigureActivity" |
53 | android:configChanges="orientation|screenSize" | 53 | android:configChanges="orientation|screenSize" |
54 | android:parentActivityName=".activities.StartActivity"> | 54 | android:parentActivityName=".activities.start.StartActivity"> |
55 | </activity> | 55 | </activity> |
56 | 56 | ||
57 | <activity | 57 | <activity |
diff --git a/app/src/main/java/org/pacien/tincapp/activities/StartActivity.kt b/app/src/main/java/org/pacien/tincapp/activities/StartActivity.kt deleted file mode 100644 index 7023316..0000000 --- a/app/src/main/java/org/pacien/tincapp/activities/StartActivity.kt +++ /dev/null | |||
@@ -1,204 +0,0 @@ | |||
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 | ||
20 | |||
21 | import android.app.Activity | ||
22 | import android.content.Intent | ||
23 | import android.net.VpnService | ||
24 | import android.os.Bundle | ||
25 | import android.support.v4.widget.SwipeRefreshLayout | ||
26 | import android.support.v7.app.AlertDialog | ||
27 | import android.view.Menu | ||
28 | import android.view.MenuItem | ||
29 | import android.view.View | ||
30 | import android.widget.AdapterView | ||
31 | import android.widget.ArrayAdapter | ||
32 | import android.widget.TextView | ||
33 | import kotlinx.android.synthetic.main.base.* | ||
34 | import kotlinx.android.synthetic.main.dialog_decrypt_keys.view.* | ||
35 | import kotlinx.android.synthetic.main.fragment_list_view.* | ||
36 | import kotlinx.android.synthetic.main.fragment_network_list_header.* | ||
37 | import org.pacien.tincapp.R | ||
38 | import org.pacien.tincapp.activities.common.ProgressModal | ||
39 | import org.pacien.tincapp.activities.configure.ConfigureActivity | ||
40 | import org.pacien.tincapp.activities.status.StatusActivity | ||
41 | import org.pacien.tincapp.context.AppPaths | ||
42 | import org.pacien.tincapp.extensions.setElements | ||
43 | import org.pacien.tincapp.intent.Actions | ||
44 | import org.pacien.tincapp.intent.BroadcastMapper | ||
45 | import org.pacien.tincapp.service.TincVpnService | ||
46 | import org.pacien.tincapp.utils.TincKeyring | ||
47 | |||
48 | /** | ||
49 | * @author pacien | ||
50 | */ | ||
51 | class StartActivity : BaseActivity() { | ||
52 | companion object { | ||
53 | private const val PERMISSION_REQUEST = 0 | ||
54 | } | ||
55 | |||
56 | private val networkList = object : AdapterView.OnItemClickListener, SwipeRefreshLayout.OnRefreshListener { | ||
57 | private var networkListAdapter: ArrayAdapter<String>? = null | ||
58 | |||
59 | fun init() { | ||
60 | networkListAdapter = ArrayAdapter(this@StartActivity, R.layout.fragment_list_item) | ||
61 | layoutInflater.inflate(R.layout.fragment_list_view, main_content) | ||
62 | list_wrapper.setOnRefreshListener(this) | ||
63 | list.addHeaderView(layoutInflater.inflate(R.layout.fragment_network_list_header, list, false), null, false) | ||
64 | list.addFooterView(View(this@StartActivity), null, false) | ||
65 | list.adapter = networkListAdapter | ||
66 | list.onItemClickListener = this | ||
67 | } | ||
68 | |||
69 | fun destroy() { | ||
70 | networkListAdapter = null | ||
71 | } | ||
72 | |||
73 | override fun onRefresh() { | ||
74 | val networks = AppPaths.confDir().list()?.sorted() ?: emptyList() | ||
75 | runOnUiThread { | ||
76 | networkListAdapter?.setElements(networks) | ||
77 | setPlaceholderVisibility() | ||
78 | list_wrapper.isRefreshing = false | ||
79 | } | ||
80 | } | ||
81 | |||
82 | override fun onItemClick(parent: AdapterView<*>?, view: View?, position: Int, id: Long) { | ||
83 | connectionStarter.tryStart(netName = (view as TextView).text.toString(), displayStatus = true) | ||
84 | } | ||
85 | |||
86 | private fun setPlaceholderVisibility() = if (networkListAdapter?.isEmpty != false) { | ||
87 | network_list_placeholder.text = getListPlaceholderText() | ||
88 | network_list_placeholder.visibility = View.VISIBLE | ||
89 | } else { | ||
90 | network_list_placeholder.visibility = View.GONE | ||
91 | } | ||
92 | |||
93 | private fun getListPlaceholderText() = if (!AppPaths.storageAvailable()) { | ||
94 | getText(R.string.message_storage_unavailable) | ||
95 | } else { | ||
96 | getText(R.string.message_no_network_configuration_found) | ||
97 | } | ||
98 | } | ||
99 | |||
100 | private val connectionStarter = object { | ||
101 | private var netName: String? = null | ||
102 | private var passphrase: String? = null | ||
103 | private var displayStatus = false | ||
104 | |||
105 | fun displayStatus() = displayStatus | ||
106 | |||
107 | fun tryStart(netName: String? = null, passphrase: String? = null, displayStatus: Boolean? = null) { | ||
108 | if (netName != null) this.netName = netName | ||
109 | this.passphrase = passphrase | ||
110 | if (displayStatus != null) this.displayStatus = displayStatus | ||
111 | |||
112 | val permissionRequestIntent = VpnService.prepare(this@StartActivity) | ||
113 | if (permissionRequestIntent != null) | ||
114 | return startActivityForResult(permissionRequestIntent, PERMISSION_REQUEST) | ||
115 | |||
116 | if (TincKeyring.needsPassphrase(this.netName!!) && this.passphrase == null) | ||
117 | return askForPassphrase() | ||
118 | |||
119 | startVpn(this.netName!!, this.passphrase) | ||
120 | } | ||
121 | |||
122 | private fun askForPassphrase() { | ||
123 | layoutInflater.inflate(R.layout.dialog_decrypt_keys, main_content, false).let { dialog -> | ||
124 | AlertDialog.Builder(this@StartActivity) | ||
125 | .setTitle(R.string.title_unlock_private_keys).setView(dialog) | ||
126 | .setPositiveButton(R.string.action_unlock) { _, _ -> tryStart(passphrase = dialog.passphrase.text.toString()) } | ||
127 | .setNegativeButton(R.string.action_cancel) { _, _ -> Unit } | ||
128 | .show() | ||
129 | } | ||
130 | } | ||
131 | |||
132 | private fun startVpn(netName: String, passphrase: String? = null) { | ||
133 | connectDialog = ProgressModal.show(this@StartActivity, getString(R.string.message_starting_vpn)) | ||
134 | TincVpnService.connect(netName, passphrase) | ||
135 | } | ||
136 | } | ||
137 | |||
138 | private val broadcastMapper = BroadcastMapper(mapOf( | ||
139 | Actions.EVENT_CONNECTED to this::onVpnStart, | ||
140 | Actions.EVENT_ABORTED to this::onVpnStartError)) | ||
141 | |||
142 | private var connectDialog: AlertDialog? = null | ||
143 | |||
144 | override fun onCreate(savedInstanceState: Bundle?) { | ||
145 | super.onCreate(savedInstanceState) | ||