From 000b7459192ea4f801e204eabbf19107a3ac2c60 Mon Sep 17 00:00:00 2001 From: pacien Date: Sat, 17 Feb 2018 01:43:30 +0100 Subject: Refactor broadcast action mapper --- .../org/pacien/tincapp/activities/StartActivity.kt | 14 ++++++-------- .../org/pacien/tincapp/activities/StatusActivity.kt | 9 ++++----- .../org/pacien/tincapp/intent/BroadcastMapper.kt | 20 ++++++++++++++++++++ .../pacien/tincapp/intent/SimpleBroadcastReceiver.kt | 19 ------------------- 4 files changed, 30 insertions(+), 32 deletions(-) create mode 100644 app/src/main/java/org/pacien/tincapp/intent/BroadcastMapper.kt delete mode 100644 app/src/main/java/org/pacien/tincapp/intent/SimpleBroadcastReceiver.kt (limited to 'app/src/main') 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 import android.app.Activity import android.app.ProgressDialog import android.content.Intent -import android.content.IntentFilter import android.net.VpnService import android.os.Bundle import android.support.v4.widget.SwipeRefreshLayout @@ -22,7 +21,7 @@ import org.pacien.tincapp.R import org.pacien.tincapp.context.AppPaths import org.pacien.tincapp.extensions.Android.setElements import org.pacien.tincapp.intent.Actions -import org.pacien.tincapp.intent.SimpleBroadcastReceiver +import org.pacien.tincapp.intent.BroadcastMapper import org.pacien.tincapp.service.TincVpnService import org.pacien.tincapp.utils.TincKeyring @@ -116,8 +115,9 @@ class StartActivity : BaseActivity() { } } - private val startupBroadcastReceiver = SimpleBroadcastReceiver(IntentFilter(Actions.EVENT_CONNECTED), this::onVpnStart) - private val errorBroadcastReceiver = SimpleBroadcastReceiver(IntentFilter(Actions.EVENT_ABORTED), this::onVpnStartError) + private val broadcastMapper = BroadcastMapper(mapOf( + Actions.EVENT_CONNECTED to this::onVpnStart, + Actions.EVENT_ABORTED to this::onVpnStartError)) private var connectDialog: ProgressDialog? = null @@ -148,13 +148,11 @@ class StartActivity : BaseActivity() { override fun onResume() { super.onResume() if (TincVpnService.isConnected()) openStatusActivity() - startupBroadcastReceiver.register() - errorBroadcastReceiver.register() + broadcastMapper.register() } override fun onPause() { - startupBroadcastReceiver.unregister() - errorBroadcastReceiver.unregister() + broadcastMapper.unregister() super.onPause() } 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 import android.app.ProgressDialog import android.content.Intent -import android.content.IntentFilter import android.os.Bundle import android.support.v4.widget.SwipeRefreshLayout import android.support.v7.app.AlertDialog @@ -23,7 +22,7 @@ import org.pacien.tincapp.data.VpnInterfaceConfiguration import org.pacien.tincapp.extensions.Android.setElements import org.pacien.tincapp.extensions.Android.setText import org.pacien.tincapp.intent.Actions -import org.pacien.tincapp.intent.SimpleBroadcastReceiver +import org.pacien.tincapp.intent.BroadcastMapper import org.pacien.tincapp.service.TincVpnService import java.util.* import kotlin.concurrent.timerTask @@ -32,7 +31,7 @@ import kotlin.concurrent.timerTask * @author pacien */ class StatusActivity : BaseActivity(), AdapterView.OnItemClickListener, SwipeRefreshLayout.OnRefreshListener { - private val shutdownBroadcastReceiver = SimpleBroadcastReceiver(IntentFilter(Actions.EVENT_DISCONNECTED), this::onVpnShutdown) + private val broadcastMapper = BroadcastMapper(mapOf(Actions.EVENT_DISCONNECTED to this::onVpnShutdown)) private var shutdownDialog: ProgressDialog? = null private var nodeListAdapter: ArrayAdapter? = null private var refreshTimer: Timer? = null @@ -82,12 +81,12 @@ class StatusActivity : BaseActivity(), AdapterView.OnItemClickListener, SwipeRef override fun onResume() { super.onResume() - shutdownBroadcastReceiver.register() + broadcastMapper.register() updateView() } override fun onPause() { - shutdownBroadcastReceiver.unregister() + broadcastMapper.unregister() super.onPause() } diff --git a/app/src/main/java/org/pacien/tincapp/intent/BroadcastMapper.kt b/app/src/main/java/org/pacien/tincapp/intent/BroadcastMapper.kt new file mode 100644 index 0000000..1382f0a --- /dev/null +++ b/app/src/main/java/org/pacien/tincapp/intent/BroadcastMapper.kt @@ -0,0 +1,20 @@ +package org.pacien.tincapp.intent + +import android.content.BroadcastReceiver +import android.content.Context +import android.content.Intent +import android.content.IntentFilter +import android.support.v4.content.LocalBroadcastManager +import org.pacien.tincapp.context.App + +/** + * @author pacien + */ +class BroadcastMapper(private val actionHandlers: Map Unit>) : BroadcastReceiver() { + private val broadcastManager = LocalBroadcastManager.getInstance(App.getContext()) + private val intentFilter = actionHandlers.keys.fold(IntentFilter(), { filter, action -> filter.apply { addAction(action) } }) + + fun register() = broadcastManager.registerReceiver(this, intentFilter) + fun unregister() = broadcastManager.unregisterReceiver(this) + override fun onReceive(context: Context?, intent: Intent?) = actionHandlers[intent?.action]?.invoke() ?: Unit +} diff --git a/app/src/main/java/org/pacien/tincapp/intent/SimpleBroadcastReceiver.kt b/app/src/main/java/org/pacien/tincapp/intent/SimpleBroadcastReceiver.kt deleted file mode 100644 index fb77174..0000000 --- a/app/src/main/java/org/pacien/tincapp/intent/SimpleBroadcastReceiver.kt +++ /dev/null @@ -1,19 +0,0 @@ -package org.pacien.tincapp.intent - -import android.content.BroadcastReceiver -import android.content.Context -import android.content.Intent -import android.content.IntentFilter -import android.support.v4.content.LocalBroadcastManager -import org.pacien.tincapp.context.App - -/** - * @author pacien - */ -class SimpleBroadcastReceiver(private val intentFilter: IntentFilter, private val eventHandler: () -> Unit) : BroadcastReceiver() { - private val broadcastManager = LocalBroadcastManager.getInstance(App.getContext()) - - fun register() = broadcastManager.registerReceiver(this, intentFilter) - fun unregister() = broadcastManager.unregisterReceiver(this) - override fun onReceive(context: Context?, intent: Intent?) = eventHandler() -} -- cgit v1.2.3