reintroduce AccessibilityService from 9bc8d6bb6d (cf. #65)

This commit is contained in:
Josia Pietsch 2024-11-08 04:07:33 +01:00
parent cd600af09f
commit 9848785b3e
Signed by: jrpie
GPG key ID: E70B571D66986A2D
5 changed files with 113 additions and 6 deletions

View file

@ -65,5 +65,17 @@
<action android:name="android.app.action.DEVICE_ADMIN_ENABLED" />
</intent-filter>
</receiver>
<service android:name=".actions.LauncherAccessibilityService"
android:exported="true"
android:permission="android.permission.BIND_ACCESSIBILITY_SERVICE"
android:label="@string/accessibility_service_name">
<intent-filter>
<action android:name="android.accessibilityservice.AccessibilityService" />
</intent-filter>
<meta-data
android:name="android.accessibilityservice"
android:resource="@xml/accessibility_service_config" />
</service>
</application>
</manifest>

View file

@ -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<AccessibilityManager>() ?: 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)
}
}

View file

@ -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)
}
}

View file

@ -243,4 +243,10 @@
<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_torch_access_exception">Error: Can\'t access torch.</string>
<string name="alert_lock_screen_failed">Error: Failed to lock screen.</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="accessibility_service_name">µLauncher - lock screen</string>
<string name="accessibility_service_description">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.</string>
</resources>

View file

@ -0,0 +1,19 @@
<?xml version="1.0" encoding="utf-8"?>
<!--
The accessibility service is only used for locking the screen.
* android:accessibilityEventTypes is unspecified intentionally, as we don't want to listen to any
events.
* android:packageNames is set to an empty list, as we don't want to interact or read data from
any app.
-->
<accessibility-service xmlns:android="http://schemas.android.com/apk/res/android"
android:accessibilityFlags="flagDefault"
android:canRequestEnhancedWebAccessibility="false"
android:canRequestFilterKeyEvents="false"
android:canRequestTouchExplorationMode="false"
android:canRetrieveWindowContent="false"
android:description="@string/accessibility_service_description"
android:packageNames=""
android:settingsActivity="de.jrpie.android.launcher.ui.settings.SettingsActivity" />