diff options
author | pacien | 2018-02-17 01:43:30 +0100 |
---|---|---|
committer | pacien | 2018-02-17 01:43:30 +0100 |
commit | 000b7459192ea4f801e204eabbf19107a3ac2c60 (patch) | |
tree | 77c1830505a81ccf4a76100a531de5f5586c1c31 /app | |
parent | e38fbf4a348201241522a90d09221e609ad58d2d (diff) | |
download | tincapp-000b7459192ea4f801e204eabbf19107a3ac2c60.tar.gz |
Refactor broadcast action mapper
Diffstat (limited to 'app')
-rw-r--r-- | app/src/main/java/org/pacien/tincapp/activities/StartActivity.kt | 14 | ||||
-rw-r--r-- | app/src/main/java/org/pacien/tincapp/activities/StatusActivity.kt | 9 | ||||
-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 | |||
3 | import android.app.Activity | 3 | import android.app.Activity |
4 | import android.app.ProgressDialog | 4 | import android.app.ProgressDialog |
5 | import android.content.Intent | 5 | import android.content.Intent |
6 | import android.content.IntentFilter | ||
7 | import android.net.VpnService | 6 | import android.net.VpnService |
8 | import android.os.Bundle | 7 | import android.os.Bundle |
9 | import android.support.v4.widget.SwipeRefreshLayout | 8 | import android.support.v4.widget.SwipeRefreshLayout |
@@ -22,7 +21,7 @@ import org.pacien.tincapp.R | |||
22 | import org.pacien.tincapp.context.AppPaths | 21 | import org.pacien.tincapp.context.AppPaths |
23 | import org.pacien.tincapp.extensions.Android.setElements | 22 | import org.pacien.tincapp.extensions.Android.setElements |
24 | import org.pacien.tincapp.intent.Actions | 23 | import org.pacien.tincapp.intent.Actions |
25 | import org.pacien.tincapp.intent.SimpleBroadcastReceiver | 24 | import org.pacien.tincapp.intent.BroadcastMapper |
26 | import org.pacien.tincapp.service.TincVpnService | 25 | import org.pacien.tincapp.service.TincVpnService |
27 | import org.pacien.tincapp.utils.TincKeyring | 26 | import 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 | ||
3 | import android.app.ProgressDialog | 3 | import android.app.ProgressDialog |
4 | import android.content.Intent | 4 | import android.content.Intent |
5 | import android.content.IntentFilter | ||
6 | import android.os.Bundle | 5 | import android.os.Bundle |
7 | import android.support.v4.widget.SwipeRefreshLayout | 6 | import android.support.v4.widget.SwipeRefreshLayout |
8 | import android.support.v7.app.AlertDialog | 7 | import android.support.v7.app.AlertDialog |
@@ -23,7 +22,7 @@ import org.pacien.tincapp.data.VpnInterfaceConfiguration | |||
23 | import org.pacien.tincapp.extensions.Android.setElements | 22 | import org.pacien.tincapp.extensions.Android.setElements |
24 | import org.pacien.tincapp.extensions.Android.setText | 23 | import org.pacien.tincapp.extensions.Android.setText |
25 | import org.pacien.tincapp.intent.Actions | 24 | import org.pacien.tincapp.intent.Actions |
26 | import org.pacien.tincapp.intent.SimpleBroadcastReceiver | 25 | import org.pacien.tincapp.intent.BroadcastMapper |
27 | import org.pacien.tincapp.service.TincVpnService | 26 | import org.pacien.tincapp.service.TincVpnService |
28 | import java.util.* | 27 | import java.util.* |
29 | import kotlin.concurrent.timerTask | 28 | import kotlin.concurrent.timerTask |
@@ -32,7 +31,7 @@ import kotlin.concurrent.timerTask | |||
32 | * @author pacien | 31 | * @author pacien |
33 | */ | 32 | */ |
34 | class StatusActivity : BaseActivity(), AdapterView.OnItemClickListener, SwipeRefreshLayout.OnRefreshListener { | 33 | class 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 | */ |
13 | class SimpleBroadcastReceiver(private val intentFilter: IntentFilter, private val eventHandler: () -> Unit) : BroadcastReceiver() { | 13 | class 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 | } |