Skip to content
This repository was archived by the owner on Dec 18, 2022. It is now read-only.

Commit 4b50f28

Browse files
committed
optimize when statement for more utilized intent actions
1 parent 03143b6 commit 4b50f28

File tree

1 file changed

+33
-31
lines changed
  • topl-service/src/main/java/io/matthewnelson/topl_service/service/components/receiver

1 file changed

+33
-31
lines changed

topl-service/src/main/java/io/matthewnelson/topl_service/service/components/receiver/TorServiceReceiver.kt

Lines changed: 33 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -149,40 +149,42 @@ internal class TorServiceReceiver(private val torService: BaseService): Broadcas
149149

150150
override fun onReceive(context: Context?, intent: Intent?) {
151151
if (context != null && intent != null) {
152-
when (val serviceAction = intent.getStringExtra(SERVICE_INTENT_FILTER)) {
153-
ServiceActionName.NEW_ID -> {
154-
torService.processServiceAction(ServiceActions.NewId())
155-
}
156-
ServiceActionName.RESTART_TOR -> {
157-
torService.processServiceAction(ServiceActions.RestartTor())
152+
when (val action = intent.action) {
153+
@Suppress("DEPRECATION")
154+
ConnectivityManager.CONNECTIVITY_ACTION -> {
155+
if (!torService.isTorOn()) return
156+
157+
val connectivity = torService.hasNetworkConnectivity()
158+
broadcastLogger.notice("Network connectivity: $connectivity")
159+
160+
val actionObject = if (connectivity)
161+
ServiceActions.SetDisableNetwork(ServiceActionName.ENABLE_NETWORK)
162+
else
163+
ServiceActions.SetDisableNetwork(ServiceActionName.DISABLE_NETWORK)
164+
165+
torService.processServiceAction(actionObject)
158166
}
159-
ServiceActionName.STOP -> {
160-
torService.processServiceAction(ServiceActions.Stop())
167+
Intent.ACTION_SCREEN_OFF,
168+
Intent.ACTION_SCREEN_ON,
169+
Intent.ACTION_USER_PRESENT -> {
170+
val locked = checkIfDeviceIsLocked()
171+
if (locked != deviceIsLocked) {
172+
setDeviceIsLocked(locked)
173+
broadcastLogger.notice("Device is locked: $deviceIsLocked")
174+
torService.refreshNotificationActions()
175+
}
161176
}
162-
else -> {
163-
164-
when (intent.action) {
165-
@Suppress("DEPRECATION")
166-
ConnectivityManager.CONNECTIVITY_ACTION -> {
167-
if (!torService.isTorOn()) return
168-
169-
val connectivity = torService.hasNetworkConnectivity()
170-
broadcastLogger.notice("Network connectivity: $connectivity")
171-
val actionObject = if (connectivity)
172-
ServiceActions.SetDisableNetwork(ServiceActionName.ENABLE_NETWORK)
173-
else
174-
ServiceActions.SetDisableNetwork(ServiceActionName.DISABLE_NETWORK)
175-
torService.processServiceAction(actionObject)
177+
SERVICE_INTENT_FILTER -> {
178+
179+
when (val serviceAction = intent.getStringExtra(SERVICE_INTENT_FILTER)) {
180+
ServiceActionName.NEW_ID -> {
181+
torService.processServiceAction(ServiceActions.NewId())
182+
}
183+
ServiceActionName.RESTART_TOR -> {
184+
torService.processServiceAction(ServiceActions.RestartTor())
176185
}
177-
Intent.ACTION_SCREEN_OFF,
178-
Intent.ACTION_SCREEN_ON,
179-
Intent.ACTION_USER_PRESENT -> {
180-
val locked = checkIfDeviceIsLocked()
181-
if (locked != deviceIsLocked) {
182-
setDeviceIsLocked(locked)
183-
broadcastLogger.notice("Device is locked: $deviceIsLocked")
184-
torService.refreshNotificationActions()
185-
}
186+
ServiceActionName.STOP -> {
187+
torService.processServiceAction(ServiceActions.Stop())
186188
}
187189
else -> {
188190
broadcastLogger.warn(

0 commit comments

Comments
 (0)