action: lock screen

This commit is contained in:
yzqzss 2024-10-26 22:32:15 +08:00
parent 6cabcf51bd
commit 9bc8d6bb6d
6 changed files with 110 additions and 5 deletions

View file

@ -52,6 +52,17 @@
<category android:name="android.intent.category.DEFAULT"/>
</intent-filter>
</activity>
<service android:name=".actions.LauncherAccessibilityService"
android:exported="true"
android:permission="android.permission.BIND_ACCESSIBILITY_SERVICE"
android:label="@string/app_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,51 @@
package de.jrpie.android.launcher.actions
import android.accessibilityservice.AccessibilityService
import android.accessibilityservice.AccessibilityServiceInfo
import android.content.Intent
import android.os.Build
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?) {
}
companion object {
const val ACTION_LOCK_SCREEN = "ACTION_LOCK_SCREEN"
}
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) {
performGlobalAction(GLOBAL_ACTION_LOCK_SCREEN)
} else {
Toast.makeText(this, getText(R.string.toast_lock_screen_not_supported), Toast.LENGTH_SHORT).show()
}
}
}

View file

@ -10,6 +10,7 @@ import android.os.SystemClock
import android.view.KeyEvent
import android.widget.Toast
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
@ -71,6 +72,12 @@ enum class LauncherAction(
R.drawable.baseline_settings_applications_24,
::expandSettingsPanel
),
LOCK_SCREEN(
"launcher:lockScreen",
R.string.list_other_lock_screen,
R.drawable.baseline_lock_24px,
::lockScreen
),
NOP("launcher:nop", R.string.list_other_nop, R.drawable.baseline_not_interested_24, {});
override fun invoke(context: Context, rect: Rect?): Boolean {
@ -199,6 +206,19 @@ private fun expandSettingsPanel(context: Context) {
}
}
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 openSettings(context: Context) {
context.startActivity(Intent(context, SettingsActivity::class.java))

View file

@ -0,0 +1,10 @@
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="24dp"
android:height="24dp"
android:viewportWidth="960"
android:viewportHeight="960"
android:tint="?attr/colorControlNormal">
<path
android:fillColor="@android:color/white"
android:pathData="M240,880Q207,880 183.5,856.5Q160,833 160,800L160,400Q160,367 183.5,343.5Q207,320 240,320L280,320L280,240Q280,157 338.5,98.5Q397,40 480,40Q563,40 621.5,98.5Q680,157 680,240L680,320L720,320Q753,320 776.5,343.5Q800,367 800,400L800,800Q800,833 776.5,856.5Q753,880 720,880L240,880ZM240,800L720,800Q720,800 720,800Q720,800 720,800L720,400Q720,400 720,400Q720,400 720,400L240,400Q240,400 240,400Q240,400 240,400L240,800Q240,800 240,800Q240,800 240,800ZM480,680Q513,680 536.5,656.5Q560,633 560,600Q560,567 536.5,543.5Q513,520 480,520Q447,520 423.5,543.5Q400,567 400,600Q400,633 423.5,656.5Q447,680 480,680ZM360,320L600,320L600,240Q600,190 565,155Q530,120 480,120Q430,120 395,155Q360,190 360,240L360,320ZM240,800Q240,800 240,800Q240,800 240,800L240,400Q240,400 240,400Q240,400 240,400L240,400Q240,400 240,400Q240,400 240,400L240,800Q240,800 240,800Q240,800 240,800Z"/>
</vector>

View file

@ -1,13 +1,18 @@
<resources>
<!--
-
- Home
-
-->
<string name="accessibility_service_description">Enable lock screen action</string>
<!--
-
- Home
-
-->
<string name="alert_cant_open_title">Can\'t open app</string>
<string name="alert_cant_open_message">Want to change its settings?</string>
<string name="alert_lock_screen_failed">Failed to lock screen</string>
<string name="toast_cant_open_message">Open settings to choose an action for this gesture</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">Sorry, lock screen is available on Android 9+</string>
<!--
-
@ -175,6 +180,7 @@
<string name="list_other_track_previous">Music: Previous</string>
<string name="list_other_expand_notifications_panel">Expand notifications panel</string>
<string name="list_other_nop">Do nothing</string>
<string name="list_other_lock_screen">Lock Screen</string>
<!--
-

View file

@ -0,0 +1,7 @@
<?xml version="1.0" encoding="utf-8"?>
<accessibility-service xmlns:android="http://schemas.android.com/apk/res/android"
android:accessibilityEventTypes="typeAllMask"
android:accessibilityFeedbackType="feedbackAllMask"
android:accessibilityFlags="flagDefault"
android:description="@string/accessibility_service_description"
/>