aboutsummaryrefslogtreecommitdiff
path: root/app/src/main/kotlin/org/pacien/tincapp/activities/ViewLogActivity.kt
blob: f3f7e24e7d8c570e8130666408e437862e71731a (plain)
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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
/*
 * 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

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.base.*
import kotlinx.android.synthetic.main.page_viewlog.*
import org.pacien.tincapp.R
import org.pacien.tincapp.commands.Executor
import org.pacien.tincapp.commands.Tinc
import org.pacien.tincapp.service.TincVpnService
import java.util.*
import kotlin.concurrent.timer

/**
 * @author pacien
 */
class ViewLogActivity : BaseActivity() {
  companion object {
    private const val LOG_LINES = 250
    private const val LOG_LEVEL = 5
    private const val NEW_LINE = "\n"
    private const val SPACED_NEW_LINE = "\n\n"
    private const val UPDATE_INTERVAL = 250L // ms
    private const val MIME_TYPE = "text/plain"
  }

  private val log = LinkedList<String>()
  private var logUpdateTimer: Timer? = null
  private var logger: Process? = null
  private var toggleButton: MenuItem? = null

  override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    supportActionBar!!.setDisplayHomeAsUpEnabled(true)
    layoutInflater.inflate(R.layout.page_viewlog, main_content)
    toggleLogging(true)
  }

  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
  }

  override fun onDestroy() {
    toggleLogging(false)
    super.onDestroy()
  }

  fun share(@Suppress("UNUSED_PARAMETER") menuItem: MenuItem) {
    synchronized(this) {
      val logFragment = log.joinToString(NEW_LINE)
      val shareIntent = Intent(Intent.ACTION_SEND)
        .setType(MIME_TYPE)
        .putExtra(Intent.EXTRA_TEXT, logFragment)

      startActivity(Intent.createChooser(shareIntent, resources.getString(R.string.menu_share_log)))
    }
  }

  fun toggleLogging(@Suppress("UNUSED_PARAMETER") menuItem: MenuItem) = toggleLogging(logger == null)

  private fun toggleLogging(enable: Boolean) {
    if (enable) {
      disableUserScroll()
      toggleButton?.setIcon(R.drawable.ic_pause_circle_outline_primary_24dp)
      startLogging()
    } else {
      enableUserScroll()
      toggleButton?.setIcon(R.drawable.ic_pause_circle_filled_primary_24dp)
      stopLogging()
    }
  }

  private fun startLogging(level: Int = LOG_LEVEL) {
    appendLog(resources.getString(R.string.message_log_level_set, level))

    TincVpnService.getCurrentNetName()?.let { netName ->
      Tinc.log(netName, level).let { process ->
        logger = process
        Executor.runAsyncTask { captureLog(process) }
      }
      logUpdateTimer = timer(period = UPDATE_INTERVAL, action = { printLog() })
    } ?: run {
      appendLog(resources.getString(R.string.message_no_daemon))
      toggleLogging(false)
    }
  }

  private fun stopLogging() {
    logger?.destroy()
    logger = null
    logUpdateTimer?.cancel()
    logUpdateTimer?.purge()
    logUpdateTimer = null
    appendLog(resources.getString(R.string.message_log_paused))
    printLog()
  }

  private fun captureLog(logger: Process) {
    logger.inputStream?.use { inputStream ->
      inputStream.bufferedReader().useLines { lines ->
        lines.forEach { appendLog(it) }
      }
    }
  }

  private fun appendLog(line: String) = synchronized(this) {
    if (log.size >= LOG_LINES) log.removeFirst()
    log.addLast(line)
  }

  private fun printLog() = synchronized(this) {
    log.joinToString(SPACED_NEW_LINE).let {
      logview_text.post {
        logview_text.text = it
        logview_frame.post { logview_frame.fullScroll(View.FOCUS_DOWN) }
      }
    }
  }

  private fun enableUserScroll() {
    logview_text.setTextIsSelectable(true)
    logview_frame.setState(true)
  }

  private fun disableUserScroll() {
    logview_text.setTextIsSelectable(false)
    logview_frame.setState(false)
  }

  private fun ScrollView.setState(enabled: Boolean) {
    if (enabled) setOnTouchListener(null) else setOnTouchListener { _, _ -> true }
    logview_frame.isSmoothScrollingEnabled = enabled
    logview_frame.isVerticalScrollBarEnabled = enabled
  }
}