fix(android): get brightness of the current window on Android, closes #2389 (#2409)

This commit is contained in:
Huang Xin
2025-11-06 02:36:45 +08:00
committed by GitHub
parent b808fc5954
commit 5a3114de48
2 changed files with 15 additions and 8 deletions
@@ -457,12 +457,19 @@ class NativeBridgePlugin(private val activity: Activity): Plugin(activity) {
fun get_screen_brightness(invoke: Invoke) {
val ret = JSObject()
try {
val brightness = Settings.System.getInt(
activity.contentResolver,
Settings.System.SCREEN_BRIGHTNESS
)
val normalizedBrightness = brightness / 255.0
ret.put("brightness", normalizedBrightness)
val window = activity.window
val layoutParams = window.attributes
val brightness = layoutParams.screenBrightness
if (brightness >= 0.0f) {
ret.put("brightness", brightness.toDouble())
} else {
val systemBrightness = Settings.System.getInt(
activity.contentResolver,
Settings.System.SCREEN_BRIGHTNESS
)
ret.put("brightness", systemBrightness / 255.0)
}
} catch (e: Exception) {
ret.put("error", e.message)
ret.put("brightness", -1.0)
@@ -1,5 +1,5 @@
import clsx from 'clsx';
import React, { useCallback, useEffect, useMemo } from 'react';
import React, { useCallback, useEffect, useMemo, useState } from 'react';
import { PiSun, PiMoon } from 'react-icons/pi';
import { TbSunMoon } from 'react-icons/tb';
import { useEnv } from '@/context/EnvContext';
@@ -30,7 +30,7 @@ export const ColorPanel: React.FC<ColorPanelProps> = ({ actionTab, bottomOffset
const { getScreenBrightness, setScreenBrightness } = useDeviceControlStore();
const { themeMode, themeColor, isDarkMode, setThemeMode, setThemeColor } = useThemeStore();
const [screenBrightnessValue, setScreenBrightnessValue] = React.useState(
const [screenBrightnessValue, setScreenBrightnessValue] = useState(
settings.screenBrightness >= 0 ? settings.screenBrightness : SCREEN_BRIGHTNESS_LIMITS.DEFAULT,
);