|
| 1 | +package org.koreader.launcher.device.lights |
| 2 | + |
| 3 | +import android.app.Activity |
| 4 | +import android.content.ComponentName |
| 5 | +import android.content.Intent |
| 6 | +import android.provider.Settings |
| 7 | +import android.util.Log |
| 8 | +import org.koreader.launcher.device.LightsInterface |
| 9 | + |
| 10 | +/* Controller for Nook Glowlight 4 Plus (bnrv1300) on Android 8.1. |
| 11 | + * Brightness via Settings.System. Requires "Modify system settings" special app permission; |
| 12 | + * grant with: adb shell appops set org.koreader.launcher WRITE_SETTINGS allow |
| 13 | + * Warmth via com.nook.partner GlowLightService (exported, no permission required, no root). |
| 14 | + * Sends action_set_color_temperature (0-100 scale); the service rescales to 0-10 for |
| 15 | + * the lm3630a_led hardware and calls PowerManager.setFrontlightBrightnessColor() using |
| 16 | + * its own DEVICE_POWER privilege. |
| 17 | + * see https://github.com/koreader/koreader/issues/14574 |
| 18 | + */ |
| 19 | +class NookGL4plusController : LightsInterface { |
| 20 | + |
| 21 | + companion object { |
| 22 | + private const val TAG = "Lights" |
| 23 | + private const val BRIGHTNESS_MAX = 100 |
| 24 | + private const val WARMTH_MAX = 10 |
| 25 | + private const val MIN = 0 |
| 26 | + private const val GLOWLIGHT_PACKAGE = "com.nook.partner" |
| 27 | + private const val GLOWLIGHT_SERVICE = "com.nook.partner.service.GlowLightService" |
| 28 | + private const val ACTION_SET_COLOR_TEMPERATURE = "action_set_color_temperature" |
| 29 | + private const val EXTRA_COLOR_TEMPERATURE = "extra_color_temperature" |
| 30 | + } |
| 31 | + |
| 32 | + @Volatile private var currentWarmth: Int = MIN |
| 33 | + |
| 34 | + override fun getPlatform(): String = "nook" |
| 35 | + override fun hasFallback(): Boolean = false |
| 36 | + override fun hasWarmth(): Boolean = true |
| 37 | + override fun needsPermission(): Boolean = false |
| 38 | + override fun hasStandaloneWarmth(): Boolean = false |
| 39 | + override fun enableFrontlightSwitch(activity: Activity): Int = 1 |
| 40 | + |
| 41 | + override fun getBrightness(activity: Activity): Int { |
| 42 | + return try { |
| 43 | + Settings.System.getInt(activity.applicationContext.contentResolver, |
| 44 | + Settings.System.SCREEN_BRIGHTNESS) |
| 45 | + } catch (e: Exception) { |
| 46 | + Log.w(TAG, e.toString()) |
| 47 | + MIN |
| 48 | + } |
| 49 | + } |
| 50 | + |
| 51 | + override fun getWarmth(activity: Activity): Int { |
| 52 | + return try { |
| 53 | + Settings.System.getInt(activity.applicationContext.contentResolver, |
| 54 | + "screen_brightness_color") |
| 55 | + } catch (e: Exception) { |
| 56 | + currentWarmth |
| 57 | + } |
| 58 | + } |
| 59 | + |
| 60 | + override fun setBrightness(activity: Activity, brightness: Int) { |
| 61 | + if (brightness < MIN || brightness > BRIGHTNESS_MAX) { |
| 62 | + Log.w(TAG, "brightness value out of range: $brightness") |
| 63 | + return |
| 64 | + } |
| 65 | + Log.v(TAG, "Setting brightness to $brightness") |
| 66 | + try { |
| 67 | + Settings.System.putInt(activity.applicationContext.contentResolver, |
| 68 | + Settings.System.SCREEN_BRIGHTNESS, brightness) |
| 69 | + } catch (e: Exception) { |
| 70 | + Log.w(TAG, "$e") |
| 71 | + } |
| 72 | + } |
| 73 | + |
| 74 | + override fun setWarmth(activity: Activity, warmth: Int) { |
| 75 | + if (warmth < MIN || warmth > WARMTH_MAX) { |
| 76 | + Log.w(TAG, "warmth value out of range: $warmth") |
| 77 | + return |
| 78 | + } |
| 79 | + if (warmth == getWarmth(activity)) return |
| 80 | + Log.v(TAG, "Setting warmth to $warmth of $WARMTH_MAX") |
| 81 | + setWarmthViaService(activity, warmth) |
| 82 | + } |
| 83 | + |
| 84 | + private fun setWarmthViaService(activity: Activity, warmth: Int): Boolean { |
| 85 | + return try { |
| 86 | + val intent = Intent(ACTION_SET_COLOR_TEMPERATURE).apply { |
| 87 | + component = ComponentName(GLOWLIGHT_PACKAGE, GLOWLIGHT_SERVICE) |
| 88 | + putExtra(EXTRA_COLOR_TEMPERATURE, warmth * 10) |
| 89 | + } |
| 90 | + val result = activity.startService(intent) |
| 91 | + if (result != null) { |
| 92 | + currentWarmth = warmth |
| 93 | + true |
| 94 | + } else { |
| 95 | + Log.w(TAG, "GlowLightService unavailable (com.nook.partner disabled?)") |
| 96 | + false |
| 97 | + } |
| 98 | + } catch (e: Exception) { |
| 99 | + Log.w(TAG, "setWarmth via service failed: $e") |
| 100 | + false |
| 101 | + } |
| 102 | + } |
| 103 | + |
| 104 | + override fun getMinBrightness(): Int = MIN |
| 105 | + override fun getMaxBrightness(): Int = BRIGHTNESS_MAX |
| 106 | + override fun getMinWarmth(): Int = MIN |
| 107 | + override fun getMaxWarmth(): Int = WARMTH_MAX |
| 108 | +} |
0 commit comments