Recently Written · git

assistkey

Hardware key remapper for the Viwoods AiPaper Reader: AI key, volume keys and power, with multi-tap, hold and chords

git clone https://github.com/equwal/assistkey

Log | Files | Refs


app/src/main/java/dev/equwal/assistkey/ui/KeyTesterActivity.kt (2437 bytes)

1 package dev.equwal.assistkey.ui
2 
3 import android.app.Activity
4 import android.os.Bundle
5 import android.os.Handler
6 import android.os.Looper
7 import android.widget.LinearLayout
8 import dev.equwal.assistkey.channel.Channel
9 import dev.equwal.assistkey.channel.Channels
10 import dev.equwal.assistkey.engine.KeyLog
11 import dev.equwal.assistkey.ui.Ui.button
12 import dev.equwal.assistkey.ui.Ui.header
13 import dev.equwal.assistkey.ui.Ui.note
14 import dev.equwal.assistkey.ui.Ui.row
15 
16 /**
17  * Shows every key event the filter receives.
18  *
19  * This is the only honest way to answer "can this key be remapped at all?" on a
20  * given device. A key the window manager consumes upstream never reaches any
21  * input filter and simply will not appear - that silence is the answer, and it
22  * is what Power looks like on every Android device.
23  */
24 class KeyTesterActivity : Activity() {
25 
26     private val handler = Handler(Looper.getMainLooper())
27     private var lastCount = -1
28 
29     private val tick = object : Runnable {
30         override fun run() {
31             if (KeyLog.count() != lastCount) build()
32             handler.postDelayed(this, 400L)
33         }
34     }
35 
36     override fun onCreate(savedInstanceState: Bundle?) {
37         super.onCreate(savedInstanceState)
38         KeyLog.clear()
39     }
40 
41     override fun onResume() {
42         super.onResume()
43         KeyLog.recording = true
44         build()
45         handler.postDelayed(tick, 400L)
46     }
47 
48     override fun onPause() {
49         super.onPause()
50         KeyLog.recording = false
51         handler.removeCallbacks(tick)
52     }
53 
54     private fun build() {
55         lastCount = KeyLog.count()
56         val col = Ui.page(this, "Button tester")
57 
58         if (!Channels.isSatisfied(this, Channel.ACCESSIBILITY)) {
59             col.row(
60                 "Button remapping",
61                 "Nothing will appear here",
62                 state = "Off"
63             ) { startActivity(android.content.Intent(this, SetupActivity::class.java)) }
64             return
65         }
66 
67         col.note("Press a button.")
68 
69         col.button("Clear") { KeyLog.clear(); build() }
70 
71         val recent = KeyLog.recent()
72         col.header(if (recent.isEmpty()) "Nothing yet" else "Most recent first")
73         if (recent.isEmpty()) {
74             col.note("A button that never appears is kept by the device.")
75             return
76         }
77         recent.forEach { e ->
78             col.row(e.describe(), "keycode " + e.keyCode, enabled = false)
79         }
80     }
81 }