Skip to content

Commit

Permalink
Fix crash on Xiaomi devices due to their service lifecycle management
Browse files Browse the repository at this point in the history
  • Loading branch information
Nain57 committed Jun 1, 2024
1 parent 632af16 commit b69110b
Showing 1 changed file with 17 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -136,17 +136,7 @@ class SmartAutoClickerService : AccessibilityService(), AndroidExecutor {
private var currentScenarioName: String? = null

/** Receives commands from the notification. */
private val notificationActionsReceiver = object : BroadcastReceiver() {
override fun onReceive(context: Context?, intent: Intent?) {
intent ?: return
val service = localService ?: return

when (intent.action) {
INTENT_ACTION_TOGGLE_OVERLAY -> service.toggleOverlaysVisibility()
INTENT_ACTION_STOP_SCENARIO -> service.stop()
}
}
}
private var notificationActionsReceiver : BroadcastReceiver? = null

override fun onServiceConnected() {
super.onServiceConnected()
Expand Down Expand Up @@ -177,6 +167,7 @@ class SmartAutoClickerService : AccessibilityService(), AndroidExecutor {
},
)

notificationActionsReceiver = createNotificationActionReceiver()
ContextCompat.registerReceiver(
this,
notificationActionsReceiver,
Expand All @@ -189,7 +180,8 @@ class SmartAutoClickerService : AccessibilityService(), AndroidExecutor {
}

override fun onUnbind(intent: Intent?): Boolean {
unregisterReceiver(notificationActionsReceiver)
notificationActionsReceiver?.let { unregisterReceiver(it) }
notificationActionsReceiver = null

LOCAL_SERVICE_INSTANCE?.stop()
LOCAL_SERVICE_INSTANCE?.release()
Expand Down Expand Up @@ -250,6 +242,19 @@ class SmartAutoClickerService : AccessibilityService(), AndroidExecutor {
return builder.build()
}

private fun createNotificationActionReceiver() : BroadcastReceiver =
object : BroadcastReceiver() {
override fun onReceive(context: Context?, intent: Intent?) {
intent ?: return
val service = localService ?: return

when (intent.action) {
INTENT_ACTION_TOGGLE_OVERLAY -> service.toggleOverlaysVisibility()
INTENT_ACTION_STOP_SCENARIO -> service.stop()
}
}
}

override suspend fun executeGesture(gestureDescription: GestureDescription) {
suspendCoroutine<Unit?> { continuation ->
dispatchGesture(
Expand Down

0 comments on commit b69110b

Please sign in to comment.