Skip to content

Commit

Permalink
#1052 fix: Crash when disabling accessibility service through notific…
Browse files Browse the repository at this point in the history
…ation on Lollipop/Marshmallow
  • Loading branch information
sds100 committed Jun 21, 2022
1 parent 39696a9 commit bc430f1
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ class AccessibilityServiceAdapter(
} else {
Timber.i("Enable service by opening accessibility settings")

return launchSettingsScreen()
return launchAccessibilitySettings()
}
}

Expand All @@ -146,7 +146,7 @@ class AccessibilityServiceAdapter(
} else {
Timber.i("Restarting service by opening accessibility settings")

return launchSettingsScreen()
return launchAccessibilitySettings()
}
}

Expand All @@ -158,14 +158,14 @@ class AccessibilityServiceAdapter(
return true
}

private fun launchSettingsScreen(): Boolean {
private fun launchAccessibilitySettings(): Boolean {
try {
val settingsIntent = Intent(Settings.ACTION_ACCESSIBILITY_SETTINGS)

settingsIntent.addFlags(
Intent.FLAG_ACTIVITY_NEW_TASK
or Intent.FLAG_ACTIVITY_CLEAR_TASK
or Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS
or Intent.FLAG_ACTIVITY_CLEAR_TASK
or Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS
)

ctx.startActivity(settingsIntent)
Expand All @@ -178,12 +178,15 @@ class AccessibilityServiceAdapter(
}

private suspend fun disableServiceSuspend() {
send(Event.DisableService).onSuccess {
Timber.i("Disabling service by calling disableSelf()")
// disableSelf method only exists in 7.0.0+
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
send(Event.DisableService).onSuccess {
Timber.i("Disabling service by calling disableSelf()")

return
}.onFailure {
Timber.i("Failed to disable service by calling disableSelf()")
return
}.onFailure {
Timber.i("Failed to disable service by calling disableSelf()")
}
}

if (permissionAdapter.isGranted(Permission.WRITE_SECURE_SETTINGS)) {
Expand Down Expand Up @@ -214,7 +217,11 @@ class AccessibilityServiceAdapter(
Settings.Secure.ENABLED_ACCESSIBILITY_SERVICES,
newEnabledServices
)

return
}

launchAccessibilitySettings()
}

override suspend fun isCrashed(): Boolean {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package io.github.sds100.keymapper.system.accessibility

import android.os.Build
import androidx.annotation.RequiresApi
import io.github.sds100.keymapper.util.InputEventType
import io.github.sds100.keymapper.util.Result
import kotlinx.coroutines.flow.Flow
Expand Down Expand Up @@ -31,6 +33,7 @@ interface IAccessibilityService {

fun switchIme(imeId: String)

@RequiresApi(Build.VERSION_CODES.N)
fun disableSelf()

fun findFocussedNode(focus: Int): AccessibilityNodeModel?
Expand Down

0 comments on commit bc430f1

Please sign in to comment.