From a6763465b4e2384f6e03fc789e1d167a1494f970 Mon Sep 17 00:00:00 2001 From: zwim <36999612+zwim@users.noreply.github.com> Date: Sun, 2 Aug 2020 22:27:49 +0200 Subject: [PATCH] [Android]: Native light dialog (#6426) * Adds support for Tolino Epos 2 warmth light --- .../apps/reader/modules/readergesture.lua | 2 +- frontend/device/android/device.lua | 36 ++++++++++++++++--- frontend/device/android/powerd.lua | 26 +++++++++++--- .../elements/common_settings_menu_table.lua | 8 +++++ platform/android/luajit-launcher | 2 +- 5 files changed, 64 insertions(+), 10 deletions(-) diff --git a/frontend/apps/reader/modules/readergesture.lua b/frontend/apps/reader/modules/readergesture.lua index 45964ef26..7f3f08b43 100644 --- a/frontend/apps/reader/modules/readergesture.lua +++ b/frontend/apps/reader/modules/readergesture.lua @@ -184,7 +184,7 @@ function ReaderGesture:init() hold_bottom_right_corner = "ignore", one_finger_swipe_left_edge_down = Device:hasFrontlight() and "decrease_frontlight" or "ignore", one_finger_swipe_left_edge_up = Device:hasFrontlight() and "increase_frontlight" or "ignore", - one_finger_swipe_right_edge_down = Device:hasNaturalLight() and "decrease_frontlight_warmth" or "ignore", + one_finger_swipe_right_edge_down = Device:hasNaturalLight() and "decrease_frontlight_warmth" or "ignore", one_finger_swipe_right_edge_up = Device:hasNaturalLight() and "increase_frontlight_warmth" or "ignore", one_finger_swipe_top_edge_right = "ignore", one_finger_swipe_top_edge_left = "ignore", diff --git a/frontend/device/android/device.lua b/frontend/device/android/device.lua index 7af379f75..c5f932b65 100644 --- a/frontend/device/android/device.lua +++ b/frontend/device/android/device.lua @@ -72,6 +72,7 @@ local Device = Generic:new{ hasEinkScreen = function() return android.isEink() end, hasColorScreen = function() return not android.isEink() end, hasFrontlight = yes, + hasNaturalLight = android.isWarmthDevice, hasLightLevelFallback = yes, canRestart = no, canSuspend = no, @@ -244,11 +245,13 @@ function Device:init() android.setBackButtonIgnored(true) end + --- @todo remove fl_last_level and revert hasLightFallback if frontlightwidget + -- check if we enable a custom light level for this activity - local last_value = G_reader_settings:readSetting("fl_last_level") - if type(last_value) == "number" and last_value >= 0 then - Device:setScreenBrightness(last_value) - end + --local last_value = G_reader_settings:readSetting("fl_last_level") + --if type(last_value) == "number" and last_value >= 0 then + -- Device:setScreenBrightness(last_value) + --end Generic.init(self) end @@ -375,6 +378,31 @@ function Device:canExecuteScript(file) end end +function Device:showLightDialog() + local usleep = require("ffi/util").usleep + local title = android.isEink() and _("Frontlight settings") or _("Light settings") + android.lights.showDialog(title, _("Brightness"), _("Warmth"), _("OK"), _("Cancel")) + repeat + usleep(25000) -- sleep 25ms before next check if dialog was quit + until (android.lights.dialogState() ~= C.ALIGHTS_DIALOG_OPENED) + local action = android.lights.dialogState() + if action == C.ALIGHTS_DIALOG_OK then + self.powerd.fl_intensity = self.powerd:frontlightIntensityHW() + logger.dbg("Dialog OK, brightness: " .. self.powerd.fl_intensity) + if android.isWarmthDevice() then + self.powerd.fl_warmth = self.powerd:getWarmth() + logger.dbg("Dialog OK, warmth: " .. self.powerd.fl_warmth) + end + elseif action == C.ALIGHTS_DIALOG_CANCEL then + logger.dbg("Dialog Cancel, brightness: " .. self.powerd.fl_intensity) + self.powerd:setIntensityHW(self.powerd.fl_intensity) + if android.isWarmthDevice() then + logger.dbg("Dialog Cancel, warmth: " .. self.powerd.fl_warmth) + self.powerd:setWarmth(self.powerd.fl_warmth) + end + end +end + android.LOGI(string.format("Android %s - %s (API %d) - flavor: %s", android.prop.version, getCodename(), Device.firmware_rev, android.prop.flavor)) diff --git a/frontend/device/android/powerd.lua b/frontend/device/android/powerd.lua index 2619a2399..c1adb4d43 100644 --- a/frontend/device/android/powerd.lua +++ b/frontend/device/android/powerd.lua @@ -2,16 +2,34 @@ local BasePowerD = require("device/generic/powerd") local _, android = pcall(require, "android") local AndroidPowerD = BasePowerD:new{ - fl_min = 0, fl_max = 25, - fl_intensity = 10, + fl_min = 0, + fl_max = 100, } function AndroidPowerD:frontlightIntensityHW() - return math.floor(android.getScreenBrightness() / 255 * self.fl_max) + return math.floor(android.getScreenBrightness() / self.bright_diff * self.fl_max) end function AndroidPowerD:setIntensityHW(intensity) - android.setScreenBrightness(math.floor(255 * intensity / self.fl_max)) + self.fl_intensity = intensity + android.setScreenBrightness(math.floor(intensity * self.bright_diff / self.fl_max)) +end + +function AndroidPowerD:init() + self.bright_diff = android:getScreenMaxBrightness() - android:getScreenMinBrightness() + if self.device:hasNaturalLight() then + self.warm_diff = android:getScreenMaxWarmth() - android:getScreenMinWarmth() + self.fl_warmth = self:getWarmth() + end +end + +function AndroidPowerD:setWarmth(warmth) + self.fl_warmth = warmth + android.setScreenWarmth(warmth / self.warm_diff) +end + +function AndroidPowerD:getWarmth() + return android.getScreenWarmth() * self.warm_diff end function AndroidPowerD:getCapacityHW() diff --git a/frontend/ui/elements/common_settings_menu_table.lua b/frontend/ui/elements/common_settings_menu_table.lua index bb2be52cf..fca66bb47 100644 --- a/frontend/ui/elements/common_settings_menu_table.lua +++ b/frontend/ui/elements/common_settings_menu_table.lua @@ -201,6 +201,14 @@ if Device:isAndroid() then local isAndroid, android = pcall(require, "android") if not isAndroid then return end + -- overwrite generic frontlight with a native Dialog + common_settings.frontlight = { + text = _("Frontlight"), + callback = function() + Device:showLightDialog() + end, + } + -- screen timeout options, disabled if device needs wakelocks. common_settings.screen_timeout = require("ui/elements/screen_android"):getTimeoutMenuTable() diff --git a/platform/android/luajit-launcher b/platform/android/luajit-launcher index ae9338224..e3d58c643 160000 --- a/platform/android/luajit-launcher +++ b/platform/android/luajit-launcher @@ -1 +1 @@ -Subproject commit ae933822402a5300c927757db410cb0faf098021 +Subproject commit e3d58c6432ba06d43bdd609c38e6315367825d7e