Skip to content

Commit ed2cab2

Browse files
hanakomisaUbuntu
authored andcommitted
Fixed inverted warmth slider for Vis6/Shine4, Added toast for permission requirement
1 parent 3aa73c9 commit ed2cab2

File tree

1 file changed

+46
-17
lines changed

1 file changed

+46
-17
lines changed

app/src/main/java/org/koreader/launcher/device/lights/TolinoB300Controller.kt

Lines changed: 46 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,14 @@ package org.koreader.launcher.device.lights
33
import android.app.Activity
44
import android.provider.Settings
55
import android.util.Log
6+
import android.widget.Toast
7+
import android.os.Looper
8+
import android.os.Handler
69
import org.koreader.launcher.device.Ioctl
710
import org.koreader.launcher.device.LightsInterface
11+
import org.koreader.launcher.device.DeviceInfo
812

913
// 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.
1214
class TolinoB300Controller : Ioctl(), LightsInterface {
1315

1416
companion object {
@@ -20,6 +22,29 @@ class TolinoB300Controller : Ioctl(), LightsInterface {
2022
private const val SCREEN_BRIGHTNESS_COLOR = "screen_brightness_color"
2123
}
2224

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+
2348
override fun getPlatform(): String {
2449
return "tolino"
2550
}
@@ -51,16 +76,19 @@ class TolinoB300Controller : Ioctl(), LightsInterface {
5176

5277
override fun getWarmth(activity: Activity): Int {
5378
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
5782
)
83+
if (needsInvertedWarmth()) WARMTH_MAX - raw else raw
5884
} catch (e: Exception) {
5985
Log.w(TAG, e.toString())
86+
0
6087
}
6188
}
6289

6390
override fun setBrightness(activity: Activity, brightness: Int) {
91+
if (!ensureWriteSettingsPermission(activity)) return
6492
if (brightness < MIN || brightness > BRIGHTNESS_MAX) {
6593
Log.w(TAG, "brightness value of of range: $brightness")
6694
return
@@ -78,29 +106,30 @@ class TolinoB300Controller : Ioctl(), LightsInterface {
78106
}
79107

80108
override fun setWarmth(activity: Activity, warmth: Int) {
109+
if (!ensureWriteSettingsPermission(activity)) return
81110
if (warmth < MIN || warmth > WARMTH_MAX) {
82111
Log.w(TAG, "warmth value of of range: $warmth")
83112
return
84113
}
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)")
86116
try {
87117
Settings.System.putInt(
88-
activity.applicationContext.contentResolver,
89-
SCREEN_BRIGHTNESS_COLOR,
90-
warmth
118+
activity.applicationContext.contentResolver,
119+
SCREEN_BRIGHTNESS_COLOR,
120+
warmthToSet
91121
)
92-
93-
// crappy toggle brightness to force warmth refresh
122+
// workaround, toggle brightness to force warmth refresh
94123
val currentBrightness: Int = getBrightness(activity)
95124
Settings.System.putInt(
96-
activity.applicationContext.contentResolver,
97-
SCREEN_BRIGHTNESS,
98-
currentBrightness + 1
125+
activity.applicationContext.contentResolver,
126+
SCREEN_BRIGHTNESS,
127+
currentBrightness + 1
99128
)
100129
Settings.System.putInt(
101-
activity.applicationContext.contentResolver,
102-
SCREEN_BRIGHTNESS,
103-
currentBrightness
130+
activity.applicationContext.contentResolver,
131+
SCREEN_BRIGHTNESS,
132+
currentBrightness
104133
)
105134
} catch (e: Exception) {
106135
Log.w(TAG, "$e")

0 commit comments

Comments
 (0)