Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
67 changes: 64 additions & 3 deletions app/src/main/java/com/heykay/snapkeymap/SnapEngine.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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()
}
}
Expand Down Expand Up @@ -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()
Expand All @@ -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() {
Expand All @@ -243,6 +298,7 @@ object SnapEngine {
main.removeCallbacks(reconnectInput)
inputReader.stop()
reader.stop()
logPause.resume()
onAccessChanged(LogAccess.OFF)
resetGestures()
}
Expand Down Expand Up @@ -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)
Expand All @@ -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
Expand Down Expand Up @@ -362,6 +422,7 @@ object SnapEngine {
preciseState.value = precise
}
updateListeningSince()
updateLogPause()
}

private fun onInputEvent(event: InputKeyEvent, time: Long, stamp: TeachingDelivery.Stamp) {
Expand Down
34 changes: 34 additions & 0 deletions app/src/test/java/com/heykay/snapkeymap/LogPauseTest.kt
Original file line number Diff line number Diff line change
@@ -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)
}
}
2 changes: 1 addition & 1 deletion docs/privacy.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand Down