aboutsummaryrefslogtreecommitdiff
path: root/app/src/main
diff options
context:
space:
mode:
Diffstat (limited to 'app/src/main')
-rw-r--r--app/src/main/java/org/pacien/tincapp/activities/common/SelfRefreshingLiveData.kt42
-rw-r--r--app/src/main/java/org/pacien/tincapp/activities/start/NetworkListLiveData.kt19
-rw-r--r--app/src/main/java/org/pacien/tincapp/activities/status/nodes/NodeListLiveData.kt25
-rw-r--r--app/src/main/java/org/pacien/tincapp/activities/viewlog/LogLiveData.kt32
4 files changed, 70 insertions, 48 deletions
diff --git a/app/src/main/java/org/pacien/tincapp/activities/common/SelfRefreshingLiveData.kt b/app/src/main/java/org/pacien/tincapp/activities/common/SelfRefreshingLiveData.kt
new file mode 100644
index 0000000..cb98736
--- /dev/null
+++ b/app/src/main/java/org/pacien/tincapp/activities/common/SelfRefreshingLiveData.kt
@@ -0,0 +1,42 @@
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
19package org.pacien.tincapp.activities.common
20
21import android.arch.lifecycle.LiveData
22import java.util.concurrent.Executors
23import java.util.concurrent.ScheduledFuture
24import java.util.concurrent.TimeUnit
25
26/**
27 * @author pacien
28 */
29abstract class SelfRefreshingLiveData<T>(private val refreshInterval: Long, private val timeUnit: TimeUnit) : LiveData<T>() {
30 private val scheduledExecutor = Executors.newSingleThreadScheduledExecutor()
31 private lateinit var scheduledFuture: ScheduledFuture<*>
32
33 override fun onActive() {
34 scheduledFuture = scheduledExecutor.scheduleWithFixedDelay(this::onRefresh, 0, refreshInterval, timeUnit)
35 }
36
37 override fun onInactive() {
38 scheduledFuture.cancel(false)
39 }
40
41 abstract fun onRefresh()
42}
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
index d0d39b8..aaab0e7 100644
--- a/app/src/main/java/org/pacien/tincapp/activities/start/NetworkListLiveData.kt
+++ b/app/src/main/java/org/pacien/tincapp/activities/start/NetworkListLiveData.kt
@@ -18,28 +18,17 @@
18 18
19package org.pacien.tincapp.activities.start 19package org.pacien.tincapp.activities.start
20 20
21import android.arch.lifecycle.LiveData 21import org.pacien.tincapp.activities.common.SelfRefreshingLiveData
22import org.pacien.tincapp.context.AppPaths 22import org.pacien.tincapp.context.AppPaths
23import java.util.* 23import java.util.concurrent.TimeUnit
24import kotlin.concurrent.timer
25 24
26/** 25/**
27 * @author pacien 26 * @author pacien
28 */ 27 */
29class NetworkListLiveData : LiveData<List<String>>() { 28class NetworkListLiveData : SelfRefreshingLiveData<List<String>>(1, TimeUnit.SECONDS) {
30 private val updateInterval = 2 * 1000L // in milliseconds
31 private val appPaths = AppPaths 29 private val appPaths = AppPaths
32 private lateinit var updateTimer: Timer
33 30
34 override fun onActive() { 31 override fun onRefresh() {
35 updateTimer = timer(period = updateInterval, action = { updateNetworkList() })
36 }
37
38 override fun onInactive() {
39 updateTimer.apply { cancel() }.apply { purge() }
40 }
41
42 private fun updateNetworkList() {
43 val networkList = appPaths.confDir().list()?.sorted() ?: emptyList() 32 val networkList = appPaths.confDir().list()?.sorted() ?: emptyList()
44 postValue(networkList) 33 postValue(networkList)
45 } 34 }
diff --git a/app/src/main/java/org/pacien/tincapp/activities/status/nodes/NodeListLiveData.kt b/app/src/main/java/org/pacien/tincapp/activities/status/nodes/NodeListLiveData.kt
index cdbdf0a..70ea54e 100644
--- a/app/src/main/java/org/pacien/tincapp/activities/status/nodes/NodeListLiveData.kt
+++ b/app/src/main/java/org/pacien/tincapp/activities/status/nodes/NodeListLiveData.kt
@@ -18,30 +18,21 @@
18 18
19package org.pacien.tincapp.activities.status.nodes 19package org.pacien.tincapp.activities.status.nodes
20 20
21import android.arch.lifecycle.LiveData 21import org.pacien.tincapp.activities.common.SelfRefreshingLiveData
22import org.pacien.tincapp.commands.Tinc 22import org.pacien.tincapp.commands.Tinc
23import java.util.* 23import java.util.concurrent.TimeUnit
24import kotlin.concurrent.timer
25 24
26/** 25/**
27 * @author pacien 26 * @author pacien
28 */ 27 */
29class NodeListLiveData(private val netName: String) : LiveData<List<String>>() { 28class NodeListLiveData(private val netName: String) : SelfRefreshingLiveData<List<String>>(1, TimeUnit.SECONDS) {
30 private val updateInterval = 2 * 1000L // in milliseconds
31 private val tincCtl = Tinc 29 private val tincCtl = Tinc
32 private lateinit var updateTimer: Timer
33 30
34 override fun onActive() { 31 override fun onRefresh() {
35 updateTimer = timer(period = updateInterval, action = { updateNodeList() }) 32 val nodeList = tincCtl.dumpNodes(netName)
36 } 33 .thenApply { list -> list.map { it.substringBefore(' ') } }
37 34 .get()
38 override fun onInactive() {
39 updateTimer.apply { cancel() }.apply { purge() }
40 }
41 35
42 private fun updateNodeList() { 36 postValue(nodeList)
43 tincCtl.dumpNodes(netName)
44 .thenApply { list -> list.map { it.substringBefore(' ')} }
45 .thenAccept(this::postValue)
46 } 37 }
47} 38}
diff --git a/app/src/main/java/org/pacien/tincapp/activities/viewlog/LogLiveData.kt b/app/src/main/java/org/pacien/tincapp/activities/viewlog/LogLiveData.kt
index f410a8c..9767e12 100644
--- a/app/src/main/java/org/pacien/tincapp/activities/viewlog/LogLiveData.kt
+++ b/app/src/main/java/org/pacien/tincapp/activities/viewlog/LogLiveData.kt
@@ -18,30 +18,37 @@
18 18
19package org.pacien.tincapp.activities.viewlog 19package org.pacien.tincapp.activities.viewlog
20 20
21import android.arch.lifecycle.LiveData 21import org.pacien.tincapp.activities.common.SelfRefreshingLiveData
22import org.pacien.tincapp.commands.Executor 22import org.pacien.tincapp.commands.Executor
23import org.pacien.tincapp.commands.Tinc 23import org.pacien.tincapp.commands.Tinc
24import java.util.* 24import java.util.*
25import kotlin.concurrent.timer 25import java.util.concurrent.TimeUnit
26 26
27/** 27/**
28 * @author pacien 28 * @author pacien
29 */ 29 */
30class LogLiveData(private val netName: String, private val logLevel: Int, private val logLineSize: Int) : LiveData<List<String>>() { 30class LogLiveData(private val netName: String, private val logLevel: Int, private val logLineSize: Int)
31 private val updateInterval = 250L // milliseconds 31 : SelfRefreshingLiveData<List<String>>(250, TimeUnit.MILLISECONDS) {
32
32 private val executor = Executor 33 private val executor = Executor
33 private val log = LinkedList<String>() 34 private val log = LinkedList<String>()
34 private var loggerProcess: Process? = null 35 private lateinit var loggerProcess: Process
35 private var logUpdateTimer: Timer? = null
36 36
37 override fun onActive() { 37 override fun onActive() {
38 super.onActive()
38 loggerProcess = startNewLogger() 39 loggerProcess = startNewLogger()
39 logUpdateTimer = timer(period = updateInterval, action = { outputLog() })
40 } 40 }
41 41
42 override fun onInactive() { 42 override fun onInactive() {
43 loggerProcess?.destroy() 43 loggerProcess.destroy()
44 logUpdateTimer?.apply { cancel() }?.apply { purge() } 44 super.onInactive()
45 }
46
47 override fun onRefresh() {
48 synchronized(log) {
49 val logView = ArrayList(log)
50 postValue(logView)
51 }
45 } 52 }
46 53
47 private fun startNewLogger(): Process { 54 private fun startNewLogger(): Process {
@@ -64,11 +71,4 @@ class LogLiveData(private val netName: String, private val logLevel: Int, privat
64 log.addLast(line) 71 log.addLast(line)
65 } 72 }
66 }