fix: accessbility isEnabled() false positive

This commit is contained in:
yzqzss 2024-11-27 23:46:08 +08:00
parent 3de4b7c56f
commit cc64745c0f
2 changed files with 21 additions and 9 deletions

View file

@ -5,6 +5,8 @@ import android.accessibilityservice.AccessibilityServiceInfo
import android.content.Context import android.content.Context
import android.content.Intent import android.content.Intent
import android.os.Build import android.os.Build
import android.provider.Settings
import android.util.Log
import android.view.accessibility.AccessibilityEvent import android.view.accessibility.AccessibilityEvent
import android.view.accessibility.AccessibilityManager import android.view.accessibility.AccessibilityManager
import android.widget.Toast import android.widget.Toast
@ -42,15 +44,18 @@ class LauncherAccessibilityService : AccessibilityService() {
} }
fun isEnabled(context: Context): Boolean { fun isEnabled(context: Context): Boolean {
val accessibilityManager = val enabledServices = Settings.Secure.getString(context.contentResolver, Settings.Secure.ENABLED_ACCESSIBILITY_SERVICES)
context.getSystemService<AccessibilityManager>() ?: return false for (service in enabledServices.split(":")) {
val enabledServices = val pkg = service.split("/")[0]
accessibilityManager.getEnabledAccessibilityServiceList(AccessibilityServiceInfo.FEEDBACK_ALL_MASK) val serviceClass = service.split("/")[1]
return enabledServices.any { if (pkg == context.packageName && serviceClass == LauncherAccessibilityService::class.java.name) {
it.id.startsWith(BuildConfig.APPLICATION_ID) Log.d("ServiceEnabled", "true")
return true
}
} }
Log.d("ServiceEnabled", "false")
return false
} }
} }
@ -87,6 +92,13 @@ class LauncherAccessibilityService : AccessibilityService() {
return return
} }
performGlobalAction(GLOBAL_ACTION_LOCK_SCREEN) val success = performGlobalAction(GLOBAL_ACTION_LOCK_SCREEN)
if (!success) {
Toast.makeText(
this,
getText(R.string.alert_lock_screen_failed),
Toast.LENGTH_LONG
).show()
}
} }
} }

View file

@ -247,7 +247,7 @@
<string name="device_admin_description">Enable the lock screen action</string> <string name="device_admin_description">Enable the lock screen action</string>
<string name="alert_no_torch_found">No camera with torch detected.</string> <string name="alert_no_torch_found">No camera with torch detected.</string>
<string name="alert_torch_access_exception">Error: Can\'t access torch.</string> <string name="alert_torch_access_exception">Error: Can\'t access torch.</string>
<string name="alert_lock_screen_failed">Error: Failed to lock screen.</string> <string name="alert_lock_screen_failed">Error: Failed to lock screen. (if you just upgraded/reinstalled the app, try to disable and re-enable the accessibility service in phone settings)</string>
<string name="toast_accessibility_service_not_enabled">μLauncher\'s accessibility service is not enabled. Please enable it in settings</string> <string name="toast_accessibility_service_not_enabled">μLauncher\'s accessibility service is not enabled. Please enable it in settings</string>
<string name="toast_lock_screen_not_supported">Error: Locking the screen using accessibility is not supported on this device. Please use device admin instead.</string> <string name="toast_lock_screen_not_supported">Error: Locking the screen using accessibility is not supported on this device. Please use device admin instead.</string>
<string name="accessibility_service_name">µLauncher - lock screen</string> <string name="accessibility_service_name">µLauncher - lock screen</string>