@@ -3,12 +3,14 @@ package org.koreader.launcher.device.lights
3
3
import android.app.Activity
4
4
import android.provider.Settings
5
5
import android.util.Log
6
+ import android.widget.Toast
7
+ import android.os.Looper
8
+ import android.os.Handler
6
9
import org.koreader.launcher.device.Ioctl
7
10
import org.koreader.launcher.device.LightsInterface
11
+ import org.koreader.launcher.device.DeviceInfo
8
12
9
13
// Light and warmth controller for B300 Tolino devices (Epos 3, Vision 6, Shine 4)
10
- // Need testers for Shine 4, I'm operating under the assumption that this works.
11
- // Vision 6 has inverted warmth from personal testing.
12
14
class TolinoB300Controller : Ioctl (), LightsInterface {
13
15
14
16
companion object {
@@ -20,6 +22,29 @@ class TolinoB300Controller : Ioctl(), LightsInterface {
20
22
private const val SCREEN_BRIGHTNESS_COLOR = " screen_brightness_color"
21
23
}
22
24
25
+ private fun needsInvertedWarmth (): Boolean {
26
+ return DeviceInfo .ID == DeviceInfo .Id .TOLINO_VISION6 ||
27
+ DeviceInfo .ID == DeviceInfo .Id .TOLINO_SHINE4
28
+ }
29
+
30
+ private fun showToastOnUiThread (activity : Activity , message : String ) {
31
+ Handler (Looper .getMainLooper()).post {
32
+ Toast .makeText(activity.applicationContext, message, Toast .LENGTH_LONG ).show()
33
+ }
34
+ }
35
+
36
+ private fun ensureWriteSettingsPermission (activity : Activity ): Boolean {
37
+ if (! Settings .System .canWrite(activity.applicationContext)) {
38
+ showToastOnUiThread(
39
+ activity,
40
+ " Please enable 'Modify system settings' for KOReader in Android settings."
41
+ )
42
+ Log .w(TAG , " WRITE_SETTINGS permission not granted." )
43
+ return false
44
+ }
45
+ return true
46
+ }
47
+
23
48
override fun getPlatform (): String {
24
49
return " tolino"
25
50
}
@@ -51,16 +76,19 @@ class TolinoB300Controller : Ioctl(), LightsInterface {
51
76
52
77
override fun getWarmth (activity : Activity ): Int {
53
78
return try {
54
- Settings .System .getInt(
55
- activity.applicationContext.contentResolver,
56
- SCREEN_BRIGHTNESS_COLOR
79
+ val raw = Settings .System .getInt(
80
+ activity.applicationContext.contentResolver,
81
+ SCREEN_BRIGHTNESS_COLOR
57
82
)
83
+ if (needsInvertedWarmth()) WARMTH_MAX - raw else raw
58
84
} catch (e: Exception ) {
59
85
Log .w(TAG , e.toString())
86
+ 0
60
87
}
61
88
}
62
89
63
90
override fun setBrightness (activity : Activity , brightness : Int ) {
91
+ if (! ensureWriteSettingsPermission(activity)) return
64
92
if (brightness < MIN || brightness > BRIGHTNESS_MAX ) {
65
93
Log .w(TAG , " brightness value of of range: $brightness " )
66
94
return
@@ -78,29 +106,30 @@ class TolinoB300Controller : Ioctl(), LightsInterface {
78
106
}
79
107
80
108
override fun setWarmth (activity : Activity , warmth : Int ) {
109
+ if (! ensureWriteSettingsPermission(activity)) return
81
110
if (warmth < MIN || warmth > WARMTH_MAX ) {
82
111
Log .w(TAG , " warmth value of of range: $warmth " )
83
112
return
84
113
}
85
- Log .v(TAG , " Setting warmth to $warmth " )
114
+ val warmthToSet = if (needsInvertedWarmth()) WARMTH_MAX - warmth else warmth
115
+ Log .v(TAG , " Setting warmth to $warmth (actual: $warmthToSet )" )
86
116
try {
87
117
Settings .System .putInt(
88
- activity.applicationContext.contentResolver,
89
- SCREEN_BRIGHTNESS_COLOR ,
90
- warmth
118
+ activity.applicationContext.contentResolver,
119
+ SCREEN_BRIGHTNESS_COLOR ,
120
+ warmthToSet
91
121
)
92
-
93
- // crappy toggle brightness to force warmth refresh
122
+ // workaround, toggle brightness to force warmth refresh
94
123
val currentBrightness: Int = getBrightness(activity)
95
124
Settings .System .putInt(
96
- activity.applicationContext.contentResolver,
97
- SCREEN_BRIGHTNESS ,
98
- currentBrightness + 1
125
+ activity.applicationContext.contentResolver,
126
+ SCREEN_BRIGHTNESS ,
127
+ currentBrightness + 1
99
128
)
100
129
Settings .System .putInt(
101
- activity.applicationContext.contentResolver,
102
- SCREEN_BRIGHTNESS ,
103
- currentBrightness
130
+ activity.applicationContext.contentResolver,
131
+ SCREEN_BRIGHTNESS ,
132
+ currentBrightness
104
133
)
105
134
} catch (e: Exception ) {
106
135
Log .w(TAG , " $e " )
0 commit comments