diff options
Diffstat (limited to 'app/src/main/java/org/pacien/tincapp/activities/viewlog/ViewLogActivity.kt')
-rw-r--r-- | app/src/main/java/org/pacien/tincapp/activities/viewlog/ViewLogActivity.kt | 125 |
1 files changed, 125 insertions, 0 deletions
diff --git a/app/src/main/java/org/pacien/tincapp/activities/viewlog/ViewLogActivity.kt b/app/src/main/java/org/pacien/tincapp/activities/viewlog/ViewLogActivity.kt new file mode 100644 index 0000000..a4e2216 --- /dev/null +++ b/app/src/main/java/org/pacien/tincapp/activities/viewlog/ViewLogActivity.kt | |||
@@ -0,0 +1,125 @@ | |||
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.viewlog | ||
20 | |||
21 | import android.arch.lifecycle.Observer | ||
22 | import android.arch.lifecycle.ViewModelProviders | ||
23 | import android.content.Intent | ||
24 | import android.os.Bundle | ||
25 | import android.view.Menu | ||
26 | import android.view.MenuItem | ||
27 | import android.view.View | ||
28 | import android.widget.ScrollView | ||
29 | import kotlinx.android.synthetic.main.base.* | ||
30 | import kotlinx.android.synthetic.main.view_log_activity.* | ||
31 | import org.pacien.tincapp.R | ||
32 | import org.pacien.tincapp.activities.BaseActivity | ||
33 | |||
34 | /** | ||
35 | * @author pacien | ||
36 | */ | ||
37 | class ViewLogActivity : BaseActivity() { | ||
38 | private val viewModel by lazy { ViewModelProviders.of(this).get(LogViewModel::class.java) } | ||
39 | private val logObserver: Observer<List<String>> = Observer { showLog(it) } | ||
40 | private var toggleButton: MenuItem? = null | ||
41 | |||
42 | override fun onCreate(savedInstanceState: Bundle?) { | ||
43 | super.onCreate(savedInstanceState) | ||
44 | supportActionBar.setDisplayHomeAsUpEnabled(true) | ||
45 | layoutInflater.inflate(R.layout.view_log_activity, main_content) | ||
46 | enableLogging(viewModel.logging) | ||
47 | } | ||
48 | |||
49 | override fun onCreateOptionsMenu(m: Menu): Boolean { | ||
50 | menuInflater.inflate(R.menu.menu_viewlog, m) | ||
51 | toggleButton = m.findItem(R.id.log_viewer_action_toggle) | ||
52 | return super.onCreateOptionsMenu(m) | ||
53 | } | ||
54 | |||
55 | override fun onSupportNavigateUp(): Boolean { | ||
56 | finish() | ||
57 | return true | ||
58 | } | ||
59 | |||
60 | @Suppress("UNUSED_PARAMETER") | ||
61 | fun shareLog(m: MenuItem) { | ||
62 | val logSnippet = viewModel.log.value?.joinToString("\n") | ||
63 | val shareIntent = Intent(Intent.ACTION_SEND) | ||
64 | .setType("text/plain") | ||
65 | .putExtra(Intent.EXTRA_TEXT, logSnippet) | ||
66 | |||
67 | startActivityChooser(shareIntent, resources.getString(R.string.log_view_menu_share_log)) | ||
68 | } | ||
69 | |||
70 | @Suppress("UNUSED_PARAMETER") | ||
71 | fun toggleLogging(m: MenuItem) = | ||
72 | enableLogging(!viewModel.logging) | ||
73 | |||
74 | private fun enableLogging(enable: Boolean) { | ||
75 | setLoggingStateSubtitle(enable) | ||
76 | setPauseButtonState(!enable) | ||
77 | enableScrolling(!enable) | ||
78 | viewModel.logging = enable | ||
79 | |||
80 | if (enable) | ||
81 | viewModel.log.observe(this, logObserver) | ||
82 | else | ||
83 | viewModel.log.removeObservers(this) | ||
84 | } | ||
85 | |||
86 | private fun showLog(logLines: List<String>?) { | ||
87 | val logSnippet = logLines?.joinToString("\n\n") ?: "" | ||
88 | |||
89 | log_view_text.post { | ||
90 | log_view_text.text = logSnippet | ||
91 | log_view_frame.scrollToBottom() | ||
92 | } | ||
93 | } | ||
94 | |||
95 | private fun setLoggingStateSubtitle(enabled: Boolean) { | ||
96 | supportActionBar.subtitle = when (enabled) { | ||
97 | true -> getString(R.string.log_view_state_level_format, viewModel.logLevelText) | ||
98 | false -> getString(R.string.log_view_state_paused) | ||
99 | } | ||
100 | } | ||
101 | |||
102 | private fun setPauseButtonState(paused: Boolean) { | ||
103 | val iconRes = when (paused) { | ||
104 | true -> R.drawable.ic_pause_circle_filled_primary_24dp | ||
105 | false -> R.drawable.ic_pause_circle_outline_primary_24dp | ||
106 | } | ||
107 | |||
108 | toggleButton?.setIcon(iconRes) | ||
109 | } | ||
110 | |||
111 | private fun enableScrolling(enabled: Boolean) { | ||
112 | if (enabled) | ||
113 | log_view_frame.setOnTouchListener(null) | ||
114 | else | ||
115 | log_view_frame.setOnTouchListener { _, _ -> true } | ||
116 | |||
117 | log_view_frame.isSmoothScrollingEnabled = enabled | ||
118 | log_view_text.setTextIsSelectable(enabled) | ||
119 | log_view_frame.scrollToBottom() | ||
120 | } | ||
121 | |||
122 | private fun ScrollView.scrollToBottom() { | ||
123 | postDelayed({ fullScroll(View.FOCUS_DOWN) }, 50) | ||
124 | } | ||
125 | } | ||