From 9848785b3e1e42bdd83eb311364b381d30c274af Mon Sep 17 00:00:00 2001 From: Josia Pietsch Date: Fri, 8 Nov 2024 04:07:33 +0100 Subject: [PATCH] reintroduce AccessibilityService from 9bc8d6bb6d700ed6dd40a934e87b250ba3839f3a (cf. #65) --- app/src/main/AndroidManifest.xml | 12 +++ .../actions/LauncherAccessibilityService.kt | 74 +++++++++++++++++++ .../launcher/actions/LauncherAction.kt | 8 +- app/src/main/res/values/strings.xml | 6 ++ .../res/xml/accessibility_service_config.xml | 19 +++++ 5 files changed, 113 insertions(+), 6 deletions(-) create mode 100644 app/src/main/java/de/jrpie/android/launcher/actions/LauncherAccessibilityService.kt create mode 100644 app/src/main/res/xml/accessibility_service_config.xml diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index b06b1ea..a4e5122 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -65,5 +65,17 @@ + + + + + + + diff --git a/app/src/main/java/de/jrpie/android/launcher/actions/LauncherAccessibilityService.kt b/app/src/main/java/de/jrpie/android/launcher/actions/LauncherAccessibilityService.kt new file mode 100644 index 0000000..bd46dab --- /dev/null +++ b/app/src/main/java/de/jrpie/android/launcher/actions/LauncherAccessibilityService.kt @@ -0,0 +1,74 @@ +package de.jrpie.android.launcher.actions + +import android.accessibilityservice.AccessibilityService +import android.accessibilityservice.AccessibilityServiceInfo +import android.content.Context +import android.content.Intent +import android.os.Build +import android.util.Log +import android.view.accessibility.AccessibilityEvent +import android.view.accessibility.AccessibilityManager +import android.widget.Toast +import androidx.core.content.getSystemService +import de.jrpie.android.launcher.R + +class LauncherAccessibilityService : AccessibilityService() { + override fun onInterrupt() { } + + override fun onAccessibilityEvent(event: AccessibilityEvent?) { + // Intentionally left blank, we are not interested in any AccessibilityEvents. + // DO NOT ADD ANY CODE HERE! + } + + companion object { + const val ACTION_LOCK_SCREEN = "ACTION_LOCK_SCREEN" + + private fun lockScreen(context: Context){ + try { + context.startService(Intent(context, LauncherAccessibilityService::class.java).apply { + action = ACTION_LOCK_SCREEN + }) + } catch (e: Exception) { + Toast.makeText( + context, + context.getString(R.string.alert_lock_screen_failed), + Toast.LENGTH_LONG + ).show() + } + } + } + + private fun isServiceEnabled(): Boolean { + val accessibilityManager = getSystemService() ?: return false + val enabledServices = accessibilityManager.getEnabledAccessibilityServiceList(AccessibilityServiceInfo.FEEDBACK_ALL_MASK) + return enabledServices.any { it.id.contains(packageName) } + } + + override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int { + intent?.action?.let { action -> + if (!isServiceEnabled()) { + Toast.makeText(this, getString(R.string.toast_accessibility_service_not_enabled), Toast.LENGTH_LONG).show() + startActivity(Intent(android.provider.Settings.ACTION_ACCESSIBILITY_SETTINGS).addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)) + return START_NOT_STICKY + } + + when (action) { + ACTION_LOCK_SCREEN -> handleLockScreen() + } + } + return super.onStartCommand(intent, flags, startId) + } + + private fun handleLockScreen(){ + if (Build.VERSION.SDK_INT < Build.VERSION_CODES.P) { + Toast.makeText( + this, + getText(R.string.toast_lock_screen_not_supported), + Toast.LENGTH_SHORT + ).show() + return + } + + performGlobalAction(GLOBAL_ACTION_LOCK_SCREEN) + } +} \ No newline at end of file diff --git a/app/src/main/java/de/jrpie/android/launcher/actions/LauncherAction.kt b/app/src/main/java/de/jrpie/android/launcher/actions/LauncherAction.kt index 3f0821d..8848e3a 100644 --- a/app/src/main/java/de/jrpie/android/launcher/actions/LauncherAction.kt +++ b/app/src/main/java/de/jrpie/android/launcher/actions/LauncherAction.kt @@ -12,6 +12,7 @@ import android.view.KeyEvent import android.widget.Toast import de.jrpie.android.launcher.Application import de.jrpie.android.launcher.R +import de.jrpie.android.launcher.actions.LauncherAccessibilityService.Companion.ACTION_LOCK_SCREEN import de.jrpie.android.launcher.apps.AppFilter import de.jrpie.android.launcher.apps.AppInfo.Companion.INVALID_USER import de.jrpie.android.launcher.ui.list.ListActivity @@ -251,9 +252,4 @@ fun openAppsList(context: Context, favorite: Boolean = false, hidden: Boolean = ) context.startActivity(intent) -} - - - - - +} \ No newline at end of file diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index cdb10c0..2bc5c59 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -243,4 +243,10 @@ Enable the lock screen action No camera with torch detected. Error: Can\'t access torch. + Error: Failed to lock screen. + μLauncher\'s accessibility service is not enabled. Please enable it in settings + Error: Locking the screen using accessibility is not supported on this device. Please use device admin instead. + µLauncher - lock screen + Setting µLauncher as an accessibility service allows it to lock the screen. Note that excessive permissions are required, you should never grant such permissions lightly to any app. µLauncher will use the accessibility service only for locking the screen. You should check the source code to make sure. Note that locking the screen can also be accomplished by granting µLauncher device administrator permissions. Those are more fine grained. However that method doesn\'t work with fingerprint and facial recognition. + diff --git a/app/src/main/res/xml/accessibility_service_config.xml b/app/src/main/res/xml/accessibility_service_config.xml new file mode 100644 index 0000000..8e64bfc --- /dev/null +++ b/app/src/main/res/xml/accessibility_service_config.xml @@ -0,0 +1,19 @@ + + + +