aboutsummaryrefslogtreecommitdiff
path: root/app
diff options
context:
space:
mode:
authorpacien2018-02-17 01:43:30 +0100
committerpacien2018-02-17 01:43:30 +0100
commit000b7459192ea4f801e204eabbf19107a3ac2c60 (patch)
tree77c1830505a81ccf4a76100a531de5f5586c1c31 /app
parente38fbf4a348201241522a90d09221e609ad58d2d (diff)
downloadtincapp-000b7459192ea4f801e204eabbf19107a3ac2c60.tar.gz
Refactor broadcast action mapper
Diffstat (limited to 'app')
-rw-r--r--app/src/main/java/org/pacien/tincapp/activities/StartActivity.kt14
-rw-r--r--app/src/main/java/org/pacien/tincapp/activities/StatusActivity.kt9
-rw-r--r--app/src/main/java/org/pacien/tincapp/intent/BroadcastMapper.kt (renamed from app/src/main/java/org/pacien/tincapp/intent/SimpleBroadcastReceiver.kt)5
3 files changed, 13 insertions, 15 deletions
diff --git a/app/src/main/java/org/pacien/tincapp/activities/StartActivity.kt b/app/src/main/java/org/pacien/tincapp/activities/StartActivity.kt
index 2dd1c46..6e74891 100644
--- a/app/src/main/java/org/pacien/tincapp/activities/StartActivity.kt
+++ b/app/src/main/java/org/pacien/tincapp/activities/StartActivity.kt
@@ -3,7 +3,6 @@ package org.pacien.tincapp.activities
3import android.app.Activity 3import android.app.Activity
4import android.app.ProgressDialog 4import android.app.ProgressDialog
5import android.content.Intent 5import android.content.Intent
6import android.content.IntentFilter
7import android.net.VpnService 6import android.net.VpnService
8import android.os.Bundle 7import android.os.Bundle
9import android.support.v4.widget.SwipeRefreshLayout 8import android.support.v4.widget.SwipeRefreshLayout
@@ -22,7 +21,7 @@ import org.pacien.tincapp.R
22import org.pacien.tincapp.context.AppPaths 21import org.pacien.tincapp.context.AppPaths
23import org.pacien.tincapp.extensions.Android.setElements 22import org.pacien.tincapp.extensions.Android.setElements
24import org.pacien.tincapp.intent.Actions 23import org.pacien.tincapp.intent.Actions
25import org.pacien.tincapp.intent.SimpleBroadcastReceiver 24import org.pacien.tincapp.intent.BroadcastMapper
26import org.pacien.tincapp.service.TincVpnService 25import org.pacien.tincapp.service.TincVpnService
27import org.pacien.tincapp.utils.TincKeyring 26import org.pacien.tincapp.utils.TincKeyring
28 27
@@ -116,8 +115,9 @@ class StartActivity : BaseActivity() {
116 } 115 }
117 } 116 }
118 117
119 private val startupBroadcastReceiver = SimpleBroadcastReceiver(IntentFilter(Actions.EVENT_CONNECTED), this::onVpnStart) 118 private val broadcastMapper = BroadcastMapper(mapOf(
120 private val errorBroadcastReceiver = SimpleBroadcastReceiver(IntentFilter(Actions.EVENT_ABORTED), this::onVpnStartError) 119 Actions.EVENT_CONNECTED to this::onVpnStart,
120 Actions.EVENT_ABORTED to this::onVpnStartError))
121 121
122 private var connectDialog: ProgressDialog? = null 122 private var connectDialog: ProgressDialog? = null
123 123
@@ -148,13 +148,11 @@ class StartActivity : BaseActivity() {
148 override fun onResume() { 148 override fun onResume() {
149 super.onResume() 149 super.onResume()
150 if (TincVpnService.isConnected()) openStatusActivity() 150 if (TincVpnService.isConnected()) openStatusActivity()
151 startupBroadcastReceiver.register() 151 broadcastMapper.register()
152 errorBroadcastReceiver.register()
153 } 152 }
154 153
155 override fun onPause() { 154 override fun onPause() {
156 startupBroadcastReceiver.unregister() 155 broadcastMapper.unregister()
157 errorBroadcastReceiver.unregister()
158 super.onPause() 156 super.onPause()
159 } 157 }
160 158
diff --git a/app/src/main/java/org/pacien/tincapp/activities/StatusActivity.kt b/app/src/main/java/org/pacien/tincapp/activities/StatusActivity.kt
index dc45947..c4873d8 100644
--- a/app/src/main/java/org/pacien/tincapp/activities/StatusActivity.kt
+++ b/app/src/main/java/org/pacien/tincapp/activities/StatusActivity.kt
@@ -2,7 +2,6 @@ package org.pacien.tincapp.activities
2 2
3import android.app.ProgressDialog 3import android.app.ProgressDialog
4import android.content.Intent 4import android.content.Intent
5import android.content.IntentFilter
6import android.os.Bundle 5import android.os.Bundle
7import android.support.v4.widget.SwipeRefreshLayout 6import android.support.v4.widget.SwipeRefreshLayout
8import android.support.v7.app.AlertDialog 7import android.support.v7.app.AlertDialog
@@ -23,7 +22,7 @@ import org.pacien.tincapp.data.VpnInterfaceConfiguration
23import org.pacien.tincapp.extensions.Android.setElements 22import org.pacien.tincapp.extensions.Android.setElements
24import org.pacien.tincapp.extensions.Android.setText 23import org.pacien.tincapp.extensions.Android.setText
25import org.pacien.tincapp.intent.Actions 24import org.pacien.tincapp.intent.Actions
26import org.pacien.tincapp.intent.SimpleBroadcastReceiver 25import org.pacien.tincapp.intent.BroadcastMapper
27import org.pacien.tincapp.service.TincVpnService 26import org.pacien.tincapp.service.TincVpnService
28import java.util.* 27import java.util.*
29import kotlin.concurrent.timerTask 28import kotlin.concurrent.timerTask
@@ -32,7 +31,7 @@ import kotlin.concurrent.timerTask
32 * @author pacien 31 * @author pacien
33 */ 32 */
34class StatusActivity : BaseActivity(), AdapterView.OnItemClickListener, SwipeRefreshLayout.OnRefreshListener { 33class StatusActivity : BaseActivity(), AdapterView.OnItemClickListener, SwipeRefreshLayout.OnRefreshListener {
35 private val shutdownBroadcastReceiver = SimpleBroadcastReceiver(IntentFilter(Actions.EVENT_DISCONNECTED), this::onVpnShutdown) 34 private val broadcastMapper = BroadcastMapper(mapOf(Actions.EVENT_DISCONNECTED to this::onVpnShutdown))
36 private var shutdownDialog: ProgressDialog? = null 35 private var shutdownDialog: ProgressDialog? = null
37 private var nodeListAdapter: ArrayAdapter<String>? = null 36 private var nodeListAdapter: ArrayAdapter<String>? = null
38 private var refreshTimer: Timer? = null 37 private var refreshTimer: Timer? = null
@@ -82,12 +81,12 @@ class StatusActivity : BaseActivity(), AdapterView.OnItemClickListener, SwipeRef
82 81
83 override fun onResume() { 82 override fun onResume() {
84 super.onResume() 83 super.onResume()
85 shutdownBroadcastReceiver.register() 84 broadcastMapper.register()
86 updateView() 85 updateView()
87 } 86 }
88 87
89 override fun onPause() { 88 override fun onPause() {
90 shutdownBroadcastReceiver.unregister() 89 broadcastMapper.unregister()
91 super.onPause() 90 super.onPause()
92 } 91 }
93 92
diff --git a/app/src/main/java/org/pacien/tincapp/intent/SimpleBroadcastReceiver.kt b/app/src/main/java/org/pacien/tincapp/intent/BroadcastMapper.kt
index fb77174..1382f0a 100644
--- a/app/src/main/java/org/pacien/tincapp/intent/SimpleBroadcastReceiver.kt
+++ b/app/src/main/java/org/pacien/tincapp/intent/BroadcastMapper.kt
@@ -10,10 +10,11 @@ import org.pacien.tincapp.context.App
10/** 10/**
11 * @author pacien 11 * @author pacien
12 */ 12 */
13class SimpleBroadcastReceiver(private val intentFilter: IntentFilter, private val eventHandler: () -> Unit) : BroadcastReceiver() { 13class BroadcastMapper(private val actionHandlers: Map<String, () -> Unit>) : BroadcastReceiver() {
14 private val broadcastManager = LocalBroadcastManager.getInstance(App.getContext()) 14 private val broadcastManager = LocalBroadcastManager.getInstance(App.getContext())
15 private val intentFilter = actionHandlers.keys.fold(IntentFilter(), { filter, action -> filter.apply { addAction(action) } })
15 16
16 fun register() = broadcastManager.registerReceiver(this, intentFilter) 17 fun register() = broadcastManager.registerReceiver(this, intentFilter)
17 fun unregister() = broadcastManager.unregisterReceiver(this) 18 fun unregister() = broadcastManager.unregisterReceiver(this)
18 override fun onReceive(context: Context?, intent: Intent?) = eventHandler() 19 override fun onReceive(context: Context?, intent: Intent?) = actionHandlers[intent?.action]?.invoke() ?: Unit
19} 20}