1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
|
/*
* Tinc App, an Android binding and user interface for the tinc mesh VPN daemon
* Copyright (C) 2017-2018 Pacien TRAN-GIRARD
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package org.pacien.tincapp.activities.viewlog
import android.arch.lifecycle.Observer
import android.arch.lifecycle.ViewModelProviders
import android.content.Intent
import android.os.Bundle
import android.view.Menu
import android.view.MenuItem
import android.view.View
import android.widget.ScrollView
import kotlinx.android.synthetic.main.view_log_activity.*
import org.pacien.tincapp.R
import org.pacien.tincapp.activities.BaseActivity
/**
* @author pacien
*/
class ViewLogActivity : BaseActivity() {
private val viewModel by lazy { ViewModelProviders.of(this).get(LogViewModel::class.java) }
private val logObserver: Observer<List<String>> = Observer { showLog(it) }
private var toggleButton: MenuItem? = null
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
supportActionBar.setDisplayHomeAsUpEnabled(true)
setContentView(R.layout.view_log_activity)
enableLogging(viewModel.logging)
}
override fun onCreateOptionsMenu(m: Menu): Boolean {
menuInflater.inflate(R.menu.menu_viewlog, m)
toggleButton = m.findItem(R.id.log_viewer_action_toggle)
return super.onCreateOptionsMenu(m)
}
override fun onSupportNavigateUp(): Boolean {
finish()
return true
}
@Suppress("UNUSED_PARAMETER")
fun shareLog(m: MenuItem) {
val logSnippet = viewModel.log.value?.joinToString("\n")
val shareIntent = Intent(Intent.ACTION_SEND)
.setType("text/plain")
.putExtra(Intent.EXTRA_TEXT, logSnippet)
startActivityChooser(shareIntent, resources.getString(R.string.log_view_menu_share_log))
}
@Suppress("UNUSED_PARAMETER")
fun toggleLogging(m: MenuItem) =
enableLogging(!viewModel.logging)
private fun enableLogging(enable: Boolean) {
setLoggingStateSubtitle(enable)
setPauseButtonState(!enable)
enableScrolling(!enable)
viewModel.logging = enable
if (enable)
viewModel.log.observe(this, logObserver)
else
viewModel.log.removeObservers(this)
}
private fun showLog(logLines: List<String>?) {
val logSnippet = logLines?.joinToString("\n\n") ?: ""
log_view_text.post {
log_view_text.text = logSnippet
log_view_frame.scrollToBottom()
}
}
private fun setLoggingStateSubtitle(enabled: Boolean) {
supportActionBar.subtitle = when (enabled) {
true -> getString(R.string.log_view_state_level_format, viewModel.logLevelText)
false -> getString(R.string.log_view_state_paused)
}
}
private fun setPauseButtonState(paused: Boolean) {
val iconRes = when (paused) {
true -> R.drawable.ic_pause_circle_filled_primary_24dp
false -> R.drawable.ic_pause_circle_outline_primary_24dp
}
toggleButton?.setIcon(iconRes)
}
private fun enableScrolling(enabled: Boolean) {
if (enabled)
log_view_frame.setOnTouchListener(null)
else
log_view_frame.setOnTouchListener { _, _ -> true }
log_view_frame.isSmoothScrollingEnabled = enabled
log_view_text.setTextIsSelectable(enabled)
log_view_frame.scrollToBottom()
}
private fun ScrollView.scrollToBottom() {
postDelayed({ fullScroll(View.FOCUS_DOWN) }, 50)
}
}
|