Recently Written · git

ink-dim

Ink Dim: tap to set the frontlight of a Viwoods AiPaper reader below the lowest system level. Tap again to go back.

git clone https://github.com/equwal/ink-dim

Log | Files | Refs


app/src/main/java/dev/equwal/inkdim/ToggleActivity.kt (2064 bytes)

1 // SPDX-License-Identifier: GPL-3.0-or-later
2 // Copyright (c) 2026 equwal
3 package dev.equwal.inkdim
4 
5 import android.app.Activity
6 import android.content.Intent
7 import android.os.Bundle
8 import android.os.Handler
9 import android.os.Looper
10 
11 /**
12  * The launcher icon. It has no screen.
13  *
14  * A tap toggles the light and the activity goes away. There is no toast: this
15  * device drops them, and the light itself is the answer.
16  *
17  * Without shell access, or without a light node, it opens the settings screen
18  * instead, which says what to do.
19  */
20 class ToggleActivity : Activity() {
21 
22     private val main = Handler(Looper.getMainLooper())
23     private var acted = false
24 
25     /** Shizuku hands over its binder a moment after the app starts. */
26     private val onShell: () -> Unit = { if (Shell.ready) act() }
27 
28     override fun onCreate(state: Bundle?) {
29         super.onCreate(state)
30         Shell.onChange(onShell)
31         Shell.connect(this)
32         if (Shell.ready) act() else main.postDelayed({ if (!acted) openSettings() }, WAIT_MS)
33     }
34 
35     override fun onDestroy() {
36         super.onDestroy()
37         Shell.removeOnChange(onShell)
38         main.removeCallbacksAndMessages(null)
39     }
40 
41     private fun act() {
42         if (acted) return
43         acted = true
44         val action = when (intent?.action) {
45             ACTION_ON -> Dim.Action.ON
46             ACTION_OFF -> Dim.Action.OFF
47             else -> Dim.Action.TOGGLE
48         }
49         Toggle.run(this, action) { result ->
50             if (result == null) openSettings() else close()
51         }
52     }
53 
54     private fun openSettings() {
55         acted = true
56         startActivity(Intent(this, SettingsActivity::class.java))
57         close()
58     }
59 
60     @Suppress("DEPRECATION")
61     private fun close() {
62         finish()
63         overridePendingTransition(0, 0)
64     }
65 
66     private companion object {
67         const val ACTION_ON = "dev.equwal.inkdim.ON"
68         const val ACTION_OFF = "dev.equwal.inkdim.OFF"
69 
70         /** How long to wait for Shizuku to hand over its binder. */
71         const val WAIT_MS = 1500L
72     }
73 }