From a7f7631c785ca44107d1c1a76fef610260e1ac1a Mon Sep 17 00:00:00 2001 From: jeffreyyan4 Date: Sun, 8 Nov 2026 01:51:12 +1100 Subject: [PATCH] perf(engine): stop logcat while precise input is live Once the Shizuku input stream is live, log lines are read and discarded without parsing, but the unfiltered logcat stream keeps running. That keeps logd, the shell logcat process and the app's reader thread busy with every line on the phone, around the clock. Stop the log reader while precise input is live and no teaching session needs the log. Restart it when teaching begins, or as the fallback when the input stream ends. The last access state is kept while paused, so the home status, listening state and paused notification are unchanged. Co-Authored-By: Claude Opus 5.5 --- CONTRIBUTING.md | 2 +- .../java/com/heykay/snapkeymap/SnapEngine.kt | 67 ++++++++++++++++++- .../com/heykay/snapkeymap/LogPauseTest.kt | 34 ++++++++++ docs/privacy.md | 2 +- 4 files changed, 100 insertions(+), 5 deletions(-) create mode 100644 app/src/test/java/com/heykay/snapkeymap/LogPauseTest.kt diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index 704fbe9..25c5760 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -42,7 +42,7 @@ These facts were checked on a Find X9 with ColorOS 16 (`CPH2797_16.0.10.600`). T - The Snap Key writes `KEYLOG_OplusKeyEventUtil: should not notify undefined keys in restrict listen mode` on every press and every release. Each edge appears two or three times within the same millisecond or the next. Real edges are at least 60 ms apart. - The line comes from UID 1000 (system). Run `adb logcat -v threadtime,uid -s KEYLOG_OplusKeyEventUtil` to see it. - The shell can also read the Snap Key on `/dev/input/event0` as `BTN_TRIGGER_HAPPY32` with `adb shell getevent -lt`. Normal apps cannot read these devices. The app learns the input code alongside the log signature through Shizuku, without pinning the device path. Confirm this on other firmware before relying on it. -- The home status says **Precise input via Shizuku** only after the input reader has received a valid key edge and a key was learned. **Logs via Shizuku** or **System logs** means log fallback is in use. Stopping Shizuku resets gestures and reconnects the local log reader; Android may require device-log consent again. +- The home status says **Precise input via Shizuku** only after the input reader has received a valid key edge and a key was learned. While it shows, logcat is stopped except during teaching. **Logs via Shizuku** or **System logs** means log fallback is in use. Stopping Shizuku resets gestures and reconnects the local log reader; Android may require device-log consent again. - With **Permission monitoring** on, ColorOS blocks `pm grant` and `pm revoke` from the shell, for both ADB and Shizuku. Turning on **Disable permission monitoring** takes effect only after a restart. - The shell cannot revoke `READ_LOGS`, so you can't reset the grant to test a first-time grant. Reinstalling the app resets it. - To count actions exactly, check `adb shell dumpsys media.camera`. Its event log records each flashlight on and off. diff --git a/app/src/main/java/com/heykay/snapkeymap/SnapEngine.kt b/app/src/main/java/com/heykay/snapkeymap/SnapEngine.kt index 36dadd2..9dc51c5 100644 --- a/app/src/main/java/com/heykay/snapkeymap/SnapEngine.kt +++ b/app/src/main/java/com/heykay/snapkeymap/SnapEngine.kt @@ -49,6 +49,31 @@ internal class LogRetryBackoff { } } +/** + * Live precise input makes the log stream redundant except while teaching, so logcat stops until it is needed again. + * An unfiltered logcat keeps logd and the shell busy with every line on the phone. + */ +internal class LogPause { + var paused = false + private set + + fun shouldRead(precise: Boolean, teaching: Boolean): Boolean = !precise || teaching + + /** Returns true only when the reader was running and should stop now. */ + fun pause(): Boolean { + if (paused) return false + paused = true + return true + } + + /** Returns true only when a paused reader should restart now. */ + fun resume(): Boolean { + if (!paused) return false + paused = false + return true + } +} + /** Connects the two ways of noticing the Snap Key (key events and the system log) to the actions. */ object SnapEngine { data class Detected(val gesture: Gesture, val atMs: Long, val source: DetectionSource) @@ -127,13 +152,15 @@ object SnapEngine { @Volatile private var logGeneration = 0 private val loggedKeys = LoggedKeyContext() private val logRetry = LogRetryBackoff() + private val logPause = LogPause() private var inputRetryDelayMs = 1000L private val reconnectInput = Runnable { if (wantsLog && readersDemanded() && canUseShizuku()) inputReader.start() } private val reconnect = Runnable { if (wantsLog && readersDemanded() && hasLogPermission()) { - if (uiVisible || !reader.needsForegroundConsent()) reader.start() + if (!logsWanted()) pauseLogs() + else if (uiVisible || !reader.needsForegroundConsent()) startLogs() else startBackgroundLogs() } } @@ -204,9 +231,37 @@ object SnapEngine { if (!readersDemanded() || !hasLogPermission()) return wantLogs() main.removeCallbacks(reconnect) + if (logsWanted()) startLogs(preferShizuku) else pauseLogs() + } + + /** Precise input replaces the log outside teaching. */ + private fun logsWanted(): Boolean = logPause.shouldRead(preciseState.value, teachingLogDemand) + + private fun startLogs(preferShizuku: Boolean = false) { + logPause.resume() reader.start(preferShizuku) } + /** An explicit stop sends no access report, so the last access state and its status stay as they were. */ + private fun pauseLogs() { + main.removeCallbacks(reconnect) + if (!logPause.pause()) return + logGeneration++ + reader.stop() + } + + private fun updateLogPause() { + if (!wantsLog || !readersDemanded()) return + if (!logsWanted()) { + pauseLogs() + } else if (logPause.resume()) { + // Same path as a lost stream: reset log edges, then reconnect now rather than after the backoff. + onAccessChanged(LogAccess.OFF) + main.removeCallbacks(reconnect) + if (hasLogPermission()) main.post(reconnect) + } + } + private fun wantLogs() { wantsLog = true inputReader.start() @@ -226,7 +281,7 @@ object SnapEngine { internal fun releaseTeachingLogs() { if (teachingSession != null) return teachingLogDemand = false - if (!readersDemanded()) stop() + if (!readersDemanded()) stop() else updateLogPause() } fun stop() { @@ -243,6 +298,7 @@ object SnapEngine { main.removeCallbacks(reconnectInput) inputReader.stop() reader.stop() + logPause.resume() onAccessChanged(LogAccess.OFF) resetGestures() } @@ -272,7 +328,7 @@ object SnapEngine { fun onConsentResumed(connectReader: Boolean) { consentVisible = true - if (!connectReader || accessState.value == LogAccess.FULL) return + if (!connectReader || accessState.value == LogAccess.FULL || !logsWanted()) return // A reader denied while we were backgrounded cannot prompt; replace it now we're on top. reader.stop() onAccessChanged(LogAccess.WAITING) @@ -289,6 +345,10 @@ object SnapEngine { if (!config.enabled || !config.needsLogAccess || !config.isTaught || !hasLogPermission() || accessState.value == LogAccess.FULL || uiVisible) return wantLogs() + if (!logsWanted()) { + pauseLogs() + return + } if (!reader.needsForegroundConsent()) { connect() return @@ -362,6 +422,7 @@ object SnapEngine { preciseState.value = precise } updateListeningSince() + updateLogPause() } private fun onInputEvent(event: InputKeyEvent, time: Long, stamp: TeachingDelivery.Stamp) { diff --git a/app/src/test/java/com/heykay/snapkeymap/LogPauseTest.kt b/app/src/test/java/com/heykay/snapkeymap/LogPauseTest.kt new file mode 100644 index 0000000..b33bf1b --- /dev/null +++ b/app/src/test/java/com/heykay/snapkeymap/LogPauseTest.kt @@ -0,0 +1,34 @@ +package com.heykay.snapkeymap + +import org.junit.Assert.assertEquals +import org.junit.Assert.assertFalse +import org.junit.Assert.assertTrue +import org.junit.Test + +class LogPauseTest { + @Test + fun readsLogsOnlyWithoutPreciseInputOrWhileTeaching() { + val pause = LogPause() + assertTrue(pause.shouldRead(precise = false, teaching = false)) + assertFalse(pause.shouldRead(precise = true, teaching = false)) + assertTrue(pause.shouldRead(precise = true, teaching = true)) + assertTrue(pause.shouldRead(precise = false, teaching = true)) + } + + @Test + fun pausesOnceUntilResumed() { + val pause = LogPause() + assertEquals(listOf(true, false, false), List(3) { pause.pause() }) + assertTrue(pause.paused) + assertTrue(pause.resume()) + assertFalse(pause.paused) + assertTrue(pause.pause()) + } + + @Test + fun resumeWithoutPauseDoesNothing() { + val pause = LogPause() + assertFalse(pause.resume()) + assertFalse(pause.paused) + } +} diff --git a/docs/privacy.md b/docs/privacy.md index c1ed980..59ac089 100644 --- a/docs/privacy.md +++ b/docs/privacy.md @@ -2,7 +2,7 @@ SnapKey Map reads the system log on the phone to detect the Snap Key. The log permission covers more than key events. Lines are checked in memory and discarded, never saved or uploaded. Teaching keeps observations in memory and saves a learned log signature and, when available, an input key code. The app accepts a key log line only from the app that wrote it while you taught the key, which on the Find X9 is the system, so other apps can't fake a press. -With Shizuku allowed, the app runs `logcat` and also reads raw key input events using `getevent` as the shell user through Shizuku. The shell service drops non-key events before passing input to the app. Input events are filtered in memory, never stored or uploaded. A learned input key uses explicit press and release events while the input stream is live; otherwise detection falls back to the system log. The log stream stays open as a fallback, but key parsing pauses while precise input is active. Ringer actions also use Shizuku to switch modes with `cmd audio set-ringer-mode`, including true Silent on Android 15 or later. You can also use it to allow Do Not Disturb access. +With Shizuku allowed, the app runs `logcat` and also reads raw key input events using `getevent` as the shell user through Shizuku. The shell service drops non-key events before passing input to the app. Input events are filtered in memory, never stored or uploaded. A learned input key uses explicit press and release events while the input stream is live; otherwise detection falls back to the system log. While precise input is active, the app stops reading the system log. It reads the log again while you teach a key, and restarts it as a fallback if the input stream ends. Ringer actions also use Shizuku to switch modes with `cmd audio set-ringer-mode`, including true Silent on Android 15 or later. You can also use it to allow Do Not Disturb access. Circle to Search uses Shizuku when available, then tries the system service directly if necessary. It opens Google's search interface over the current screen. Google handles that search under its own privacy policy. SnapKey Map does not capture or store the screen for this action. Other mapped actions do not use Shizuku. Apps and websites you open through an action have their own privacy policies.