Double Tap to Lock Screen gesture implemented

This commit is contained in:
ottoptj 2024-08-06 20:27:15 +03:00
commit c6f3d537f6
6 changed files with 118 additions and 3 deletions

View file

@ -35,7 +35,8 @@
android:launchMode="singleTask" android:launchMode="singleTask"
android:screenOrientation="portrait" android:screenOrientation="portrait"
android:theme="@style/Theme.YamLauncher" android:theme="@style/Theme.YamLauncher"
android:windowSoftInputMode="adjustResize"> android:windowSoftInputMode="adjustResize"
tools:ignore="DiscouragedApi,LockedOrientationActivity">
<intent-filter> <intent-filter>
<action android:name="android.intent.action.MAIN" /> <action android:name="android.intent.action.MAIN" />
@ -44,6 +45,18 @@
<category android:name="android.intent.category.DEFAULT" /> <category android:name="android.intent.category.DEFAULT" />
</intent-filter> </intent-filter>
</activity> </activity>
<service
android:name=".ScreenLockService"
android:exported="false"
android:permission="android.permission.BIND_ACCESSIBILITY_SERVICE">
<intent-filter>
<action android:name="android.accessibilityservice.AccessibilityService" />
</intent-filter>
<meta-data
android:name="android.screenLockService"
android:resource="@xml/screenlock_service_config" />
</service>
</application> </application>
</manifest> </manifest>

View file

@ -1,11 +1,15 @@
package eu.ottop.yamlauncher package eu.ottop.yamlauncher
import android.accessibilityservice.AccessibilityService
import android.accessibilityservice.AccessibilityServiceInfo
import android.annotation.SuppressLint import android.annotation.SuppressLint
import android.app.AlertDialog
import android.content.Context import android.content.Context
import android.content.Intent import android.content.Intent
import android.content.SharedPreferences import android.content.SharedPreferences
import android.content.pm.LauncherActivityInfo import android.content.pm.LauncherActivityInfo
import android.content.pm.LauncherApps import android.content.pm.LauncherApps
import android.content.pm.ServiceInfo
import android.graphics.BlendMode import android.graphics.BlendMode
import android.graphics.BlendModeColorFilter import android.graphics.BlendModeColorFilter
import android.graphics.Color import android.graphics.Color
@ -15,6 +19,7 @@ import android.os.Bundle
import android.os.Handler import android.os.Handler
import android.os.Looper import android.os.Looper
import android.os.UserHandle import android.os.UserHandle
import android.provider.Settings
import android.text.Editable import android.text.Editable
import android.text.TextWatcher import android.text.TextWatcher
import android.view.GestureDetector import android.view.GestureDetector
@ -27,7 +32,7 @@ import android.view.View.TEXT_ALIGNMENT_TEXT_START
import android.view.ViewTreeObserver import android.view.ViewTreeObserver
import android.view.WindowInsets import android.view.WindowInsets
import android.view.WindowInsetsController import android.view.WindowInsetsController
import android.view.WindowManager import android.view.accessibility.AccessibilityManager
import android.view.inputmethod.InputMethodManager import android.view.inputmethod.InputMethodManager
import android.widget.LinearLayout import android.widget.LinearLayout
import android.widget.TextClock import android.widget.TextClock
@ -49,8 +54,8 @@ import kotlinx.coroutines.Job
import kotlinx.coroutines.delay import kotlinx.coroutines.delay
import kotlinx.coroutines.launch import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext import kotlinx.coroutines.withContext
import kotlin.math.abs
import java.lang.reflect.Method import java.lang.reflect.Method
import kotlin.math.abs
class MainActivity : AppCompatActivity(), SharedPreferences.OnSharedPreferenceChangeListener, AppMenuAdapter.OnItemClickListener, AppMenuAdapter.OnShortcutListener, AppMenuAdapter.OnItemLongClickListener { class MainActivity : AppCompatActivity(), SharedPreferences.OnSharedPreferenceChangeListener, AppMenuAdapter.OnItemClickListener, AppMenuAdapter.OnShortcutListener, AppMenuAdapter.OnItemLongClickListener {
@ -427,6 +432,26 @@ class MainActivity : AppCompatActivity(), SharedPreferences.OnSharedPreferenceCh
startActivity(Intent(this@MainActivity, SettingsActivity::class.java)) startActivity(Intent(this@MainActivity, SettingsActivity::class.java))
} }
override fun onDoubleTap(e: MotionEvent): Boolean {
if (preferences.getBoolean("doubleTap", false)) {
if (isAccessibilityServiceEnabled(
this@MainActivity,
ScreenLockService::class.java
)
) {
println("enabled")
val intent = Intent(this@MainActivity, ScreenLockService::class.java)
intent.action = "LOCK_SCREEN"
startService(intent)
} else {
promptEnableAccessibility()
}
}
return super.onDoubleTap(e)
}
} }
inner class TextGestureListener : GestureListener() { inner class TextGestureListener : GestureListener() {
@ -435,6 +460,40 @@ class MainActivity : AppCompatActivity(), SharedPreferences.OnSharedPreferenceCh
} }
} }
fun isAccessibilityServiceEnabled(context: Context, service: Class<out AccessibilityService>): Boolean {
val am = context.getSystemService(ACCESSIBILITY_SERVICE) as AccessibilityManager
val enabledServices =
am.getEnabledAccessibilityServiceList(AccessibilityServiceInfo.FEEDBACK_ALL_MASK)
for (enabledService in enabledServices) {
val enabledServiceInfo: ServiceInfo = enabledService.resolveInfo.serviceInfo
if (enabledServiceInfo.packageName.equals(context.packageName) && enabledServiceInfo.name.equals(
service.name
)
) return true
}
return false
}
private fun promptEnableAccessibility() {
AlertDialog.Builder(this@MainActivity).apply {
setTitle("Confirmation")
setMessage("To lock with double tap, enable YAM Launcher in accessibility settings.")
setPositiveButton("Yes") { _, _ ->
// Perform action on confirmation
val intent = Intent(Settings.ACTION_ACCESSIBILITY_SETTINGS)
intent.flags = Intent.FLAG_ACTIVITY_NEW_TASK
startActivity(intent)
}
setNegativeButton("Cancel") { _, _ ->
}
}.create().show()
}
private fun getSwipeInfo(direction: String): Pair<LauncherActivityInfo?, Int?> { private fun getSwipeInfo(direction: String): Pair<LauncherActivityInfo?, Int?> {
val app = preferences.getString("${direction}SwipeApp", "")?.split("§splitter§") val app = preferences.getString("${direction}SwipeApp", "")?.split("§splitter§")

View file

@ -0,0 +1,28 @@
package eu.ottop.yamlauncher
import android.accessibilityservice.AccessibilityService
import android.content.Intent
import android.view.accessibility.AccessibilityEvent
class ScreenLockService : AccessibilityService() {
override fun onAccessibilityEvent(event: AccessibilityEvent?) {
// Handle accessibility events if needed
}
override fun onInterrupt() {
// Handle interrupt
}
override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
if (intent != null && intent.action == "LOCK_SCREEN") {
performLockScreen()
}
stopSelf()
return super.onStartCommand(intent, flags, startId)
}
private fun performLockScreen() {
performGlobalAction(GLOBAL_ACTION_LOCK_SCREEN)
}
}

View file

@ -75,5 +75,6 @@
<string name="weather_link"><![CDATA[Weather data by <a href="https://open-meteo.com/">Open-Meteo.com</a><br>(<a href="https://creativecommons.org/licenses/by/4.0/">CC BY 4.0</a>)]]></string> <string name="weather_link"><![CDATA[Weather data by <a href="https://open-meteo.com/">Open-Meteo.com</a><br>(<a href="https://creativecommons.org/licenses/by/4.0/">CC BY 4.0</a>)]]></string>
<string name="location_link"><![CDATA[Location data by <a href="https://open-meteo.com/">Open-Meteo.com</a> (<a href="https://creativecommons.org/licenses/by/4.0/">CC BY 4.0</a>)]]></string> <string name="location_link"><![CDATA[Location data by <a href="https://open-meteo.com/">Open-Meteo.com</a> (<a href="https://creativecommons.org/licenses/by/4.0/">CC BY 4.0</a>)]]></string>
<string name="accessibility_service_description">Idk this is my service</string>
</resources> </resources>

View file

@ -173,6 +173,12 @@
app:key="rightSwipeApp" app:key="rightSwipeApp"
app:selectable="true" app:selectable="true"
app:title="Right Swipe App" /> app:title="Right Swipe App" />
<SwitchPreference
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:defaultValue="false"
android:title="Double Tap to Lock Screen"
app:key="doubleTap" />
<SwitchPreference <SwitchPreference
android:layout_width="wrap_content" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_height="wrap_content"

View file

@ -0,0 +1,8 @@
<?xml version="1.0" encoding="utf-8"?>
<accessibility-service
xmlns:android="http://schemas.android.com/apk/res/android"
android:accessibilityEventTypes="typeViewClicked"
android:accessibilityFeedbackType="feedbackGeneric"
android:notificationTimeout="100"
android:canRetrieveWindowContent="true"
android:description="@string/accessibility_service_description" />