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/SetupActivity.kt (2837 bytes)

1 package dev.equwal.assistkey.ui
2 
3 import android.app.Activity
4 import android.content.Intent
5 import android.widget.Toast
6 import dev.equwal.assistkey.bundle.Bundled
7 import dev.equwal.assistkey.channel.Channel
8 import dev.equwal.assistkey.channel.Channels
9 import dev.equwal.assistkey.license.License
10 import dev.equwal.assistkey.ui.Ui.button
11 import dev.equwal.assistkey.ui.Ui.check
12 import dev.equwal.assistkey.ui.Ui.note
13 import dev.equwal.assistkey.ui.Ui.row
14 
15 /**
16  * The master switch, what the app has been allowed to do, and the licence.
17  *
18  * Button remapping comes first because nothing else works without it.
19  */
20 class SetupActivity : Activity() {
21 
22     override fun onResume() {
23         super.onResume()
24         build()
25     }
26 
27     private fun build() {
28         val col = Ui.page(this, "Setup")
29 
30         val ch = Channel.ACCESSIBILITY
31         val on = Channels.isEnabled(this, ch)
32         val ok = Channels.isSatisfied(this, ch)
33         col.check(
34             "Button remapping",
35             if (ok) null else Channels.status(this, ch),
36             on
37         ) { checked ->
38             Channels.setEnabled(this, ch, checked)
39             if (checked && !Channels.isSatisfied(this, ch)) claim(ch)
40             build()
41         }
42         if (on && !ok) {
43             col.button("Turn it on") { claim(ch) }
44             col.note("If the switch turns itself off, allow restricted settings in App info.")
45             col.button("Open App info") {
46                 Channels.safeStart(this, Channels.appInfoIntent(this))
47             }
48         }
49 
50         val granted = PermissionsActivity.granted(this)
51         col.row(
52             "Permissions",
53             state = if (granted.all { it }) "All granted"
54             else (granted.size - granted.count { it }).toString() + " missing"
55         ) { startActivity(Intent(this, PermissionsActivity::class.java)) }
56 
57         val s = License.state(this)
58         col.row(
59             "Licence",
60             state = Summary.licenceChip(s.tier, s.trialDaysLeft)
61         ) { startActivity(Intent(this, LicenseActivity::class.java)) }
62 
63         // Rebind has no internet permission, so it cannot look for a new version.
64         // Ink Update, an app of its own, does that and tells the user.
65         BundledRows.add(this, col, Bundled.UPDATE)
66     }
67 
68     private fun claim(ch: Channel) {
69         if (ch != Channel.ACCESSIBILITY) return claimNow(ch)
70         AccessibilityDisclosure.show(this, onAgree = { claimNow(ch) })
71     }
72 
73     private fun claimNow(ch: Channel) {
74         if (ch == Channel.CAMERA) {
75             Toast.makeText(this, "Choose Camera app, then pick Rebind", Toast.LENGTH_LONG).show()
76         }
77         if (!Channels.safeStart(this, Channels.claimIntent(this, ch))) {
78             Toast.makeText(this, "This device has no screen for that", Toast.LENGTH_LONG)
79                 .show()
80         }
81     }
82 }