From 818943f7e8bad37af981fc01452822bb040c2ca9 Mon Sep 17 00:00:00 2001 From: pacien Date: Wed, 18 Apr 2018 22:36:03 +0200 Subject: Prevent logging when no daemon is running --- .../pacien/tincapp/activities/ViewLogActivity.kt | 46 +++++++++++++--------- app/src/main/res/menu/menu_viewlog.xml | 1 + app/src/main/res/values/strings.xml | 1 + 3 files changed, 30 insertions(+), 18 deletions(-) diff --git a/app/src/main/java/org/pacien/tincapp/activities/ViewLogActivity.kt b/app/src/main/java/org/pacien/tincapp/activities/ViewLogActivity.kt index 2a56ceb..5dc1a36 100644 --- a/app/src/main/java/org/pacien/tincapp/activities/ViewLogActivity.kt +++ b/app/src/main/java/org/pacien/tincapp/activities/ViewLogActivity.kt @@ -31,16 +31,18 @@ class ViewLogActivity : BaseActivity() { private val log = LinkedList() 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) - startLogging() + 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) } @@ -50,20 +52,10 @@ class ViewLogActivity : BaseActivity() { } override fun onDestroy() { - stopLogging() + toggleLogging(false) super.onDestroy() } - fun toggleLogging(menuItem: MenuItem) { - if (logger == null) { - startLogging() - menuItem.setIcon(R.drawable.ic_pause_circle_outline_primary_24dp) - } else { - stopLogging() - menuItem.setIcon(R.drawable.ic_pause_circle_filled_primary_24dp) - } - } - fun share(@Suppress("UNUSED_PARAMETER") menuItem: MenuItem) { synchronized(this) { val logFragment = log.joinToString(NEW_LINE) @@ -75,18 +67,36 @@ class ViewLogActivity : BaseActivity() { } } + 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) { - disableUserScroll() appendLog(resources.getString(R.string.message_log_level_set, level)) - Tinc.log(TincVpnService.getCurrentNetName()!!, level).let { process -> - logger = process - Executor.runAsyncTask { captureLog(process) } + + 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) } - logUpdateTimer = timer(period = UPDATE_INTERVAL, action = { printLog() }) } private fun stopLogging() { - enableUserScroll() logger?.destroy() logger = null logUpdateTimer?.cancel() diff --git a/app/src/main/res/menu/menu_viewlog.xml b/app/src/main/res/menu/menu_viewlog.xml index 397055a..bcf49d2 100644 --- a/app/src/main/res/menu/menu_viewlog.xml +++ b/app/src/main/res/menu/menu_viewlog.xml @@ -4,6 +4,7 @@ tools:context="org.pacien.tincapp.activities.ViewLogActivity"> Invalid network name. The application has previously encountered a fatal error. The crash details have been saved in \"%1$s\". + No running tinc daemon has been found. none yes -- cgit v1.2.3