|
| 1 | +package org.koreader.launcher.device.lights |
| 2 | + |
| 3 | +import android.app.Activity |
| 4 | +import android.provider.Settings |
| 5 | +import android.util.Log |
| 6 | +import android.widget.Toast |
| 7 | +import android.os.Looper |
| 8 | +import android.os.Handler |
| 9 | +import org.koreader.launcher.device.Ioctl |
| 10 | +import org.koreader.launcher.device.LightsInterface |
| 11 | +import org.koreader.launcher.device.DeviceInfo |
| 12 | + |
| 13 | +// Light and warmth controller for B300 Tolino devices (Epos 3, Vision 6, Shine 4) |
| 14 | +class TolinoB300Controller : Ioctl(), LightsInterface { |
| 15 | + |
| 16 | + companion object { |
| 17 | + private const val TAG = "Lights" |
| 18 | + private const val BRIGHTNESS_MAX = 100 |
| 19 | + private const val WARMTH_MAX = 10 |
| 20 | + private const val MIN = 0 |
| 21 | + private const val SCREEN_BRIGHTNESS = "screen_brightness" |
| 22 | + private const val SCREEN_BRIGHTNESS_COLOR = "screen_brightness_color" |
| 23 | + } |
| 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 | + |
| 48 | + override fun getPlatform(): String { |
| 49 | + return "tolino" |
| 50 | + } |
| 51 | + |
| 52 | + override fun hasFallback(): Boolean { |
| 53 | + return false |
| 54 | + } |
| 55 | + |
| 56 | + override fun hasWarmth(): Boolean { |
| 57 | + return true |
| 58 | + } |
| 59 | + |
| 60 | + override fun needsPermission(): Boolean { |
| 61 | + return true |
| 62 | + } |
| 63 | + |
| 64 | + override fun enableFrontlightSwitch(activity: Activity): Int { |
| 65 | + return 1 |
| 66 | + } |
| 67 | + |
| 68 | + override fun getBrightness(activity: Activity): Int { |
| 69 | + return try { |
| 70 | + Settings.System.getInt(activity.applicationContext.contentResolver, SCREEN_BRIGHTNESS) |
| 71 | + } catch (e: Exception) { |
| 72 | + Log.w(TAG, e.toString()) |
| 73 | + 0 |
| 74 | + } |
| 75 | + } |
| 76 | + |
| 77 | + override fun getWarmth(activity: Activity): Int { |
| 78 | + return try { |
| 79 | + val raw = Settings.System.getInt( |
| 80 | + activity.applicationContext.contentResolver, |
| 81 | + SCREEN_BRIGHTNESS_COLOR |
| 82 | + ) |
| 83 | + if (needsInvertedWarmth()) WARMTH_MAX - raw else raw |
| 84 | + } catch (e: Exception) { |
| 85 | + Log.w(TAG, e.toString()) |
| 86 | + 0 |
| 87 | + } |
| 88 | + } |
| 89 | + |
| 90 | + override fun setBrightness(activity: Activity, brightness: Int) { |
| 91 | + if (!ensureWriteSettingsPermission(activity)) return |
| 92 | + if (brightness < MIN || brightness > BRIGHTNESS_MAX) { |
| 93 | + Log.w(TAG, "brightness value of of range: $brightness") |
| 94 | + return |
| 95 | + } |
| 96 | + Log.v(TAG, "Setting brightness to $brightness") |
| 97 | + try { |
| 98 | + Settings.System.putInt( |
| 99 | + activity.applicationContext.contentResolver, |
| 100 | + SCREEN_BRIGHTNESS, |
| 101 | + brightness |
| 102 | + ) |
| 103 | + } catch (e: Exception) { |
| 104 | + Log.w(TAG, "$e") |
| 105 | + } |
| 106 | + } |
| 107 | + |
| 108 | + override fun setWarmth(activity: Activity, warmth: Int) { |
| 109 | + if (!ensureWriteSettingsPermission(activity)) return |
| 110 | + if (warmth < MIN || warmth > WARMTH_MAX) { |
| 111 | + Log.w(TAG, "warmth value of of range: $warmth") |
| 112 | + return |
| 113 | + } |
| 114 | + val warmthToSet = if (needsInvertedWarmth()) WARMTH_MAX - warmth else warmth |
| 115 | + Log.v(TAG, "Setting warmth to $warmth (actual: $warmthToSet)") |
| 116 | + try { |
| 117 | + Settings.System.putInt( |
| 118 | + activity.applicationContext.contentResolver, |
| 119 | + SCREEN_BRIGHTNESS_COLOR, |
| 120 | + warmthToSet |
| 121 | + ) |
| 122 | + // workaround, toggle brightness to force warmth refresh |
| 123 | + val currentBrightness: Int = getBrightness(activity) |
| 124 | + Settings.System.putInt( |
| 125 | + activity.applicationContext.contentResolver, |
| 126 | + SCREEN_BRIGHTNESS, |
| 127 | + currentBrightness + 1 |
| 128 | + ) |
| 129 | + Settings.System.putInt( |
| 130 | + activity.applicationContext.contentResolver, |
| 131 | + SCREEN_BRIGHTNESS, |
| 132 | + currentBrightness |
| 133 | + ) |
| 134 | + } catch (e: Exception) { |
| 135 | + Log.w(TAG, "$e") |
| 136 | + } |
| 137 | + } |
| 138 | + |
| 139 | + override fun getMinWarmth(): Int { |
| 140 | + return MIN |
| 141 | + } |
| 142 | + |
| 143 | + override fun getMaxWarmth(): Int { |
| 144 | + return WARMTH_MAX |
| 145 | + } |
| 146 | + |
| 147 | + override fun getMinBrightness(): Int { |
| 148 | + return MIN |
| 149 | + } |
| 150 | + |
| 151 | + override fun getMaxBrightness(): Int { |
| 152 | + return BRIGHTNESS_MAX |
| 153 | + } |
| 154 | + |
| 155 | + override fun hasStandaloneWarmth(): Boolean { |
| 156 | + return false |
| 157 | + } |
| 158 | +} |
0 commit comments