diff options
Diffstat (limited to 'app/src/main/java/org/pacien/tincapp/activities/start')
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 | |||
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 | } | ||
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 | |||
19 | package org.pacien.tincapp.activities.start | ||
20 | |||
21 | import android.arch.lifecycle.Observer | ||
22 | import android.os.Bundle | ||
23 | import android.support.v4.app.Fragment | ||
24 | import android.view.LayoutInflater | ||
25 | import android.view.View | ||
26 | import android.view.ViewGroup | ||
27 | import android.widget.AdapterView | ||
28 | import android.widget.ArrayAdapter | ||
29 | import android.widget.TextView | ||
30 | import kotlinx.android.synthetic.main.start_network_list.* | ||
31 | import org.pacien.tincapp.R | ||
32 | import org.pacien.tincapp.context.AppPaths | ||
33 | import org.pacien.tincapp.extensions.hideBottomSeparator | ||
34 | import org.pacien.tincapp.extensions.setElements | ||
35 | |||
36 | /** | ||
37 | * @author pacien | ||
38 | */ | ||
39 | class 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 | |||
19 | package org.pacien.tincapp.activities.start | ||
20 | |||
21 | import android.arch.lifecycle.LiveData | ||
22 | import org.pacien.tincapp.context.AppPaths | ||
23 | import java.util.* | ||
24 | import kotlin.concurrent.timer | ||
25 | |||
26 | /** | ||
27 | * @author pacien | ||
28 | */ | ||
29 | class NetworkListLiveData : LiveData<List<String>>() { | ||
30 | private val updateInterval = 2 * 1000L // in milliseconds | ||
31 | private val appPaths = AppPaths | ||
32 | private lateinit var updateTimer: Timer | ||