package com.hikoncont.service.modules

import android.content.Context
import android.provider.Settings
import android.util.Log

/**
 * 屏幕亮度管理器
 * 用于在黑屏遮盖时调整屏幕亮度，增强用户体验
 */
class ScreenBrightnessManager(
    private val context: Context
) {
    companion object {
        private const val TAG = "ScreenBrightnessManager"
        private const val MIN_BRIGHTNESS = 1  // 最低亮度（0是真正的最低值，提供最佳黑屏效果）
        private const val DEFAULT_BRIGHTNESS = 128  // 默认亮度（中等亮度）
        private const val INVALID_BRIGHTNESS = -999  // 无效亮度标志
        
        // 华为设备特殊处理
        private const val HUAWEI_MIN_BRIGHTNESS = 1  // 华为设备最低亮度（某些华为设备不支持0亮度）
        // 🔧 新增：vivo设备特殊处理 - 使用深色遮盖替代亮度调节
        private const val VIVO_USE_DARK_OVERLAY = true  // vivo设备使用深色遮盖替代方案
        
        private const val RETRY_COUNT = 3  // 重试次数
        private const val RETRY_DELAY = 100L  // 重试间隔（毫秒）
    }
    
    // 保存原始亮度值，用于恢复
    private var originalBrightness: Int = INVALID_BRIGHTNESS
    private var originalBrightnessMode: Int = INVALID_BRIGHTNESS  // 保存原始亮度模式
    private var isManagingBrightness = false
    private var brightnessRecorded = false  // 标记是否已记录原始亮度
    
    // 设备信息
    private val isHuaweiDevice = isHuaweiDevice()
    // 🔧 新增：vivo设备检测
    private val isVivoDevice = isVivoDevice()
    private val deviceBrand = android.os.Build.BRAND?.lowercase() ?: ""
    private val deviceManufacturer = android.os.Build.MANUFACTURER?.lowercase() ?: ""
    
    /**
     * 检查是否为华为设备
     */
    private fun isHuaweiDevice(): Boolean {
        val brand = android.os.Build.BRAND?.lowercase() ?: ""
        val manufacturer = android.os.Build.MANUFACTURER?.lowercase() ?: ""
        val model = android.os.Build.MODEL?.lowercase() ?: ""
        
        val isHuawei = brand.contains("huawei") || 
                      manufacturer.contains("huawei") || 
                      brand.contains("honor") || 
                      manufacturer.contains("honor") ||
                      model.contains("honor")
        
        if (isHuawei) {
            Log.i(TAG, "🔍 检测到华为/荣耀设备: Brand=$brand, Manufacturer=$manufacturer, Model=$model")
        }
        
        return isHuawei
    }
    
    /**
     * 🔧 新增：检查是否为vivo设备
     */
    private fun isVivoDevice(): Boolean {
        val brand = android.os.Build.BRAND?.lowercase() ?: ""
        val manufacturer = android.os.Build.MANUFACTURER?.lowercase() ?: ""
        val model = android.os.Build.MODEL?.lowercase() ?: ""
        
        val isVivo = brand.contains("vivo") || 
                    manufacturer.contains("vivo") || 
                    brand.contains("iqoo") || 
                    manufacturer.contains("iqoo") ||
                    model.contains("vivo") ||
                    model.contains("iqoo")
        
        if (isVivo) {
            Log.i(TAG, "🔍 检测到vivo/iQOO设备: Brand=$brand, Manufacturer=$manufacturer, Model=$model")
            Log.i(TAG, "📱 vivo设备将使用深色遮盖替代亮度调节方案")
        }
        
        return isVivo
    }
    
    /**
     * 检查是否有WRITE_SETTINGS权限
     */
    fun hasWriteSettingsPermission(): Boolean {
        return try {
            if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.M) {
                val hasPermission = Settings.System.canWrite(context)
                Log.d(TAG, "🔍 WRITE_SETTINGS权限状态: $hasPermission")
                hasPermission
            } else {
                Log.d(TAG, "🔍 Android 6.0以下版本，WRITE_SETTINGS权限默认已授权")
                true
            }
        } catch (e: Exception) {
            Log.e(TAG, "❌ 检查WRITE_SETTINGS权限失败", e)
            false
        }
    }
    
    /**
     * 获取当前屏幕亮度
     */
    private fun getCurrentBrightness(): Int {
        return try {
            val brightness = Settings.System.getInt(
                context.contentResolver,
                Settings.System.SCREEN_BRIGHTNESS,
                DEFAULT_BRIGHTNESS
            )
            Log.d(TAG, "📱 当前屏幕亮度: $brightness")
            brightness
        } catch (e: Exception) {
            Log.e(TAG, "❌ 获取当前屏幕亮度失败", e)
            DEFAULT_BRIGHTNESS
        }
    }
    
    /**
     * 获取当前屏幕亮度模式
     */
    private fun getCurrentBrightnessMode(): Int {
        return try {
            val mode = Settings.System.getInt(
                context.contentResolver,
                Settings.System.SCREEN_BRIGHTNESS_MODE,
                Settings.System.SCREEN_BRIGHTNESS_MODE_MANUAL
            )
            Log.d(TAG, "📱 当前屏幕亮度模式: ${if (mode == Settings.System.SCREEN_BRIGHTNESS_MODE_AUTOMATIC) "自动" else "手动"}")
            mode
        } catch (e: Exception) {
            Log.e(TAG, "❌ 获取当前屏幕亮度模式失败", e)
            Settings.System.SCREEN_BRIGHTNESS_MODE_MANUAL
        }
    }
    
    /**
     * 设置屏幕亮度
     * @param brightness 亮度值 (0-255，其中0是最低亮度)
     */
    private fun setBrightness(brightness: Int): Boolean {
        return try {
            // 🔑 华为设备特殊处理：某些华为设备不支持0亮度
            val adjustedBrightness = if (isHuaweiDevice && brightness == 0) {
                Log.d(TAG, "🔧 华为设备特殊处理：将最低亮度从0调整为$HUAWEI_MIN_BRIGHTNESS")
                HUAWEI_MIN_BRIGHTNESS
            } else {
                brightness
            }
            
            // 🔑 确保亮度值在有效范围内，允许0-255
            val targetBrightness = adjustedBrightness.coerceIn(0, 255)
            
            Log.d(TAG, "🔧 准备设置屏幕亮度: 输入值=$brightness, 调整后=$adjustedBrightness, 最终值=$targetBrightness")
            
            // 🔑 华为设备特殊处理：先禁用自适应亮度，再设置手动模式
            if (isHuaweiDevice) {
                Log.d(TAG, "🔧 华为设备：先禁用自适应亮度")
                disableHuaweiAdaptiveBrightness()
            }
            
            // 🔑 关键修复：先设置为手动亮度模式，否则自动亮度会覆盖手动设置
            Log.d(TAG, "🔧 设置屏幕亮度模式为手动模式")
            val modeSuccess = setBrightnessMode(Settings.System.SCREEN_BRIGHTNESS_MODE_MANUAL)
            
            if (!modeSuccess) {
                Log.w(TAG, "⚠️ 设置手动亮度模式失败，但继续尝试设置亮度")
            } else {
                Log.d(TAG, "✅ 手动亮度模式设置成功")
            }
            
            // 🔑 华为设备使用重试机制设置亮度
            val success = if (isHuaweiDevice) {
                setBrightnessWithRetry(targetBrightness)
            } else {
                setBrightnessOnce(targetBrightness)
            }
            
            if (success) {
                Log.i(TAG, "✅ 屏幕亮度已设置为: $targetBrightness")
                
                // 🔑 验证设置是否生效（华为设备可能需要更长时间）
                val verifyDelay = if (isHuaweiDevice) 200L else 50L
                Thread.sleep(verifyDelay)
                
                val verifyBrightness = getCurrentBrightness()
                if (verifyBrightness == targetBrightness) {
                    Log.i(TAG, "✅ 亮度设置验证成功: $verifyBrightness")
                } else {
                    Log.w(TAG, "⚠️ 亮度设置验证失败: 期望=$targetBrightness, 实际=$verifyBrightness")
                    // 华为设备可能需要额外处理
                    if (isHuaweiDevice) {
                        Log.d(TAG, "🔧 华为设备：尝试额外的亮度设置方法")
                        val extraSuccess = setHuaweiBrightnessExtra(targetBrightness)
                        if (extraSuccess) {
                            Log.i(TAG, "✅ 华为设备额外亮度设置成功")
                        }
                    }
                }
            } else {
                Log.w(TAG, "⚠️ 设置屏幕亮度失败")
            }
            
            success
        } catch (e: Exception) {
            Log.e(TAG, "❌ 设置屏幕亮度异常", e)
            false
        }
    }
    
    /**
     * 设置亮度模式
     */
    private fun setBrightnessMode(mode: Int): Boolean {
        return try {
            Settings.System.putInt(
                context.contentResolver,
                Settings.System.SCREEN_BRIGHTNESS_MODE,
                mode
            )
        } catch (e: Exception) {
            Log.e(TAG, "❌ 设置亮度模式失败", e)
            false
        }
    }
    
    /**
     * 禁用华为设备的自适应亮度
     */
    private fun disableHuaweiAdaptiveBrightness() {
        try {
            // 华为设备可能有特殊的自适应亮度设置
            val contentResolver = context.contentResolver
            
            // 尝试禁用各种可能的自适应亮度设置
            val adaptiveSettings = listOf(
                "screen_auto_brightness",
                "adaptive_brightness",
                "auto_brightness",
                "screen_brightness_mode"
            )
            
            for (setting in adaptiveSettings) {
                try {
                    Settings.System.putInt(contentResolver, setting, 0)
                    Log.d(TAG, "🔧 华为设备：已尝试禁用 $setting")
                } catch (e: Exception) {
                    // 忽略单个设置的失败
                }
            }
        } catch (e: Exception) {
            Log.w(TAG, "⚠️ 禁用华为自适应亮度时出现异常", e)
        }
    }
    
    /**
     * 单次设置亮度
     */
    private fun setBrightnessOnce(brightness: Int): Boolean {
        return try {
            Settings.System.putInt(
                context.contentResolver,
                Settings.System.SCREEN_BRIGHTNESS,
                brightness
            )
        } catch (e: Exception) {
            Log.e(TAG, "❌ 单次设置亮度失败", e)
            false
        }
    }
    
    /**
     * 华为设备使用重试机制设置亮度
     */
    private fun setBrightnessWithRetry(brightness: Int): Boolean {
        for (attempt in 1..RETRY_COUNT) {
            try {
                Log.d(TAG, "🔧 华为设备亮度设置尝试 $attempt/$RETRY_COUNT")
                
                val success = Settings.System.putInt(
                    context.contentResolver,
                    Settings.System.SCREEN_BRIGHTNESS,
                    brightness
                )
                
                if (success) {
                    // 等待设置生效
                    Thread.sleep(RETRY_DELAY)
                    
                    // 验证设置是否成功
                    val currentBrightness = getCurrentBrightness()
                    if (currentBrightness == brightness || Math.abs(currentBrightness - brightness) <= 2) {
                        Log.i(TAG, "✅ 华为设备亮度设置成功，第 $attempt 次尝试")
                        return true
                    } else {
                        Log.w(TAG, "⚠️ 华为设备亮度设置验证失败，第 $attempt 次尝试: 期望=$brightness, 实际=$currentBrightness")
                    }
                } else {
                    Log.w(TAG, "⚠️ 华为设备亮度设置失败，第 $attempt 次尝试")
                }
                
                if (attempt < RETRY_COUNT) {
                    Thread.sleep(RETRY_DELAY)
                }
                
            } catch (e: Exception) {
                Log.e(TAG, "❌ 华为设备亮度设置异常，第 $attempt 次尝试", e)
                if (attempt < RETRY_COUNT) {
                    Thread.sleep(RETRY_DELAY)
                }
            }
        }
        
        Log.e(TAG, "❌ 华为设备亮度设置失败，所有重试均失败")
        return false
    }
    
    /**
     * 华为设备额外的亮度设置方法
     */
    private fun setHuaweiBrightnessExtra(brightness: Int): Boolean {
        return try {
            val contentResolver = context.contentResolver
            
            // 尝试华为设备特有的亮度设置方法
            val huaweiSettings = listOf(
                "screen_brightness",
                "screen_brightness_value",
                "brightness_level"
            )
            
            var success = false
            for (setting in huaweiSettings) {
                try {
                    val result = Settings.System.putInt(contentResolver, setting, brightness)
                    if (result) {
                        Log.d(TAG, "✅ 华为设备额外设置成功: $setting = $brightness")
                        success = true
                    }
                } catch (e: Exception) {
                    Log.d(TAG, "⚠️ 华为设备额外设置失败: $setting", e)
                }
            }
            
            return success
        } catch (e: Exception) {
            Log.e(TAG, "❌ 华为设备额外亮度设置异常", e)
            false
        }
    }
    
    /**
     * 华为设备恢复自动亮度的额外方法
     */
    private fun restoreHuaweiAutoBrightness(): Boolean {
        return try {
            val contentResolver = context.contentResolver
            
            // 尝试华为设备特有的自动亮度恢复方法
            val huaweiAutoSettings = listOf(
                "screen_auto_brightness" to 1,
                "adaptive_brightness" to 1,
                "auto_brightness" to 1,
                "screen_brightness_mode" to Settings.System.SCREEN_BRIGHTNESS_MODE_AUTOMATIC
            )
            
            var success = false
            for ((setting, value) in huaweiAutoSettings) {
                try {
                    val result = Settings.System.putInt(contentResolver, setting, value)
                    if (result) {
                        Log.d(TAG, "✅ 华为设备自动亮度恢复成功: $setting = $value")
                        success = true
                    }
                    Thread.sleep(50L) // 短暂等待
                } catch (e: Exception) {
                    Log.d(TAG, "⚠️ 华为设备自动亮度恢复失败: $setting", e)
                }
            }
            
            return success
        } catch (e: Exception) {
            Log.e(TAG, "❌ 华为设备自动亮度恢复异常", e)
            false
        }
    }
    
    /**
     * 启用低亮度模式（用于黑屏遮盖）
     * 只有在有WRITE_SETTINGS权限时才执行
     * 🔧 vivo设备：跳过亮度调节，使用深色遮盖替代方案
     */
    fun enableLowBrightnessMode(): Boolean {
        return try {
            // 🔧 vivo设备特殊处理：跳过亮度调节，使用深色遮盖替代方案
            if (isVivoDevice) {
                Log.i(TAG, "📱 vivo设备：跳过屏幕亮度调节，启用深色遮盖替代方案")
                isManagingBrightness = true  // 标记为已管理状态，确保恢复逻辑正常工作
                Log.i(TAG, "✅ vivo设备深色遮盖模式已启用（替代亮度调节）")
                return true
            }
            
            if (!hasWriteSettingsPermission()) {
                Log.w(TAG, "⚠️ 没有WRITE_SETTINGS权限，无法调整屏幕亮度")
                return false
            }
            
            if (isManagingBrightness) {
                Log.d(TAG, "🔧 亮度管理已启用，跳过重复操作")
                return true
            }
            
            Log.i(TAG, "🌙 启用低亮度模式")
            
            // 🔑 重要：首先记录当前亮度和亮度模式，确保能正确恢复
            if (!brightnessRecorded) {
                originalBrightness = getCurrentBrightness()
                originalBrightnessMode = getCurrentBrightnessMode()
                brightnessRecorded = true
                Log.i(TAG, "💾 记录原始亮度: $originalBrightness，原始亮度模式: ${if (originalBrightnessMode == Settings.System.SCREEN_BRIGHTNESS_MODE_AUTOMATIC) "自动" else "手动"}")
            } else {
                Log.d(TAG, "💾 使用已记录的原始亮度: $originalBrightness，原始亮度模式: ${if (originalBrightnessMode == Settings.System.SCREEN_BRIGHTNESS_MODE_AUTOMATIC) "自动" else "手动"}")
            }
            
            // 验证记录的亮度是否有效
            if (originalBrightness == INVALID_BRIGHTNESS) {
                Log.w(TAG, "⚠️ 记录的原始亮度无效，使用默认值")
                originalBrightness = DEFAULT_BRIGHTNESS
            }
            
            // 验证记录的亮度模式是否有效
            if (originalBrightnessMode == INVALID_BRIGHTNESS) {
                Log.w(TAG, "⚠️ 记录的原始亮度模式无效，使用手动模式")
                originalBrightnessMode = Settings.System.SCREEN_BRIGHTNESS_MODE_MANUAL
            }
            
            // 🔑 设置为最低亮度（华为设备特殊处理）
            val targetMinBrightness = if (isHuaweiDevice) HUAWEI_MIN_BRIGHTNESS else MIN_BRIGHTNESS
            Log.i(TAG, "🔧 设置屏幕亮度到最低值: $targetMinBrightness ${if (isHuaweiDevice) "(华为设备调整)" else ""}")
            val success = setBrightness(targetMinBrightness)
            
            if (success) {
                isManagingBrightness = true
                Log.i(TAG, "✅ 低亮度模式已启用，屏幕亮度设置为最低: $targetMinBrightness ${if (isHuaweiDevice) "(华为设备)" else ""}")
            } else {
                Log.w(TAG, "⚠️ 启用低亮度模式失败")
                // 失败时重置记录状态
                brightnessRecorded = false
                originalBrightness = INVALID_BRIGHTNESS
                originalBrightnessMode = INVALID_BRIGHTNESS
            }
            
            success
        } catch (e: Exception) {
            Log.e(TAG, "❌ 启用低亮度模式异常", e)
            // 异常时重置记录状态
            brightnessRecorded = false
            originalBrightness = INVALID_BRIGHTNESS
            originalBrightnessMode = INVALID_BRIGHTNESS
            false
        }
    }
    
    /**
     * 恢复原始亮度
     * 🔧 vivo设备：跳过亮度恢复，深色遮盖由遮盖管理器负责移除
     */
    fun restoreOriginalBrightness(): Boolean {
        return try {
            if (!isManagingBrightness) {
                Log.d(TAG, "🔧 亮度管理未启用，跳过恢复操作")
                return true
            }
            
            // 🔧 vivo设备特殊处理：跳过亮度恢复
            if (isVivoDevice) {
                Log.i(TAG, "📱 vivo设备：跳过屏幕亮度恢复，深色遮盖已由遮盖管理器移除")
                resetBrightnessState()  // 重置管理状态
                Log.i(TAG, "✅ vivo设备深色遮盖模式已关闭")
                return true
            }
            
            if (!hasWriteSettingsPermission()) {
                Log.w(TAG, "⚠️ 没有WRITE_SETTINGS权限，无法恢复屏幕亮度")
                resetBrightnessState()
                return false
            }
            
            Log.i(TAG, "🌅 恢复原始亮度")
            
            // 🔑 改进的亮度恢复逻辑：先恢复亮度模式，再恢复亮度值
            val targetBrightness = when {
                originalBrightness != INVALID_BRIGHTNESS && brightnessRecorded -> {
                    Log.d(TAG, "📱 使用记录的原始亮度: $originalBrightness")
                    originalBrightness
                }
                originalBrightness > 0 -> {
                    Log.d(TAG, "📱 使用有效的原始亮度: $originalBrightness")
                    originalBrightness
                }
                else -> {
                    Log.w(TAG, "⚠️ 原始亮度无效 ($originalBrightness)，使用默认亮度")
                    DEFAULT_BRIGHTNESS
                }
            }
            
            val targetBrightnessMode = when {
                originalBrightnessMode == Settings.System.SCREEN_BRIGHTNESS_MODE_AUTOMATIC -> {
                    Log.d(TAG, "📱 恢复自动亮度模式")
                    Settings.System.SCREEN_BRIGHTNESS_MODE_AUTOMATIC
                }
                else -> {
                    Log.d(TAG, "📱 恢复手动亮度模式")
                    Settings.System.SCREEN_BRIGHTNESS_MODE_MANUAL
                }
            }
            
            // 🔑 关键：先设置亮度值，再恢复亮度模式（如果是自动模式）
            Log.i(TAG, "🔧 恢复屏幕亮度到: $targetBrightness ${if (isHuaweiDevice) "(华为设备)" else ""}")
            val brightnessSuccess = setBrightness(targetBrightness)
            
            // 如果原来是自动模式，最后恢复自动模式
            var modeSuccess = true
            if (targetBrightnessMode == Settings.System.SCREEN_BRIGHTNESS_MODE_AUTOMATIC) {
                try {
                    // 🔑 华为设备特殊处理：先等待亮度设置生效
                    if (isHuaweiDevice) {
                        Thread.sleep(200L)
                        Log.d(TAG, "🔧 华为设备：等待亮度设置生效后恢复自动模式")
                    }
                    
                    modeSuccess = setBrightnessMode(Settings.System.SCREEN_BRIGHTNESS_MODE_AUTOMATIC)
                    if (modeSuccess) {
                        Log.i(TAG, "✅ 自动亮度模式已恢复")
                    } else {
                        Log.w(TAG, "⚠️ 恢复自动亮度模式失败")
                        // 🔑 华为设备的额外恢复尝试
                        if (isHuaweiDevice) {
                            Log.d(TAG, "🔧 华为设备：尝试额外的自动亮度恢复方法")
                            modeSuccess = restoreHuaweiAutoBrightness()
                        }
                    }
                } catch (e: Exception) {
                    Log.e(TAG, "❌ 恢复自动亮度模式异常", e)
                    modeSuccess = false
                }
            }
            
            val success = brightnessSuccess && modeSuccess
            
            if (success) {
                Log.i(TAG, "✅ 原始亮度已恢复: $targetBrightness")
            } else {
                Log.w(TAG, "⚠️ 恢复原始亮度失败")
            }
            
            // 重置状态
            resetBrightnessState()
            
            success
        } catch (e: Exception) {
            Log.e(TAG, "❌ 恢复原始亮度异常", e)
            // 确保重置状态
            resetBrightnessState()
            false
        }
    }
    
    /**
     * 重置亮度管理状态
     */
    private fun resetBrightnessState() {
        isManagingBrightness = false
        brightnessRecorded = false
        originalBrightness = INVALID_BRIGHTNESS
        originalBrightnessMode = INVALID_BRIGHTNESS
        Log.d(TAG, "🔄 亮度管理状态已重置")
    }
    
    /**
     * 强制恢复默认亮度（用于异常情况）
     */
    fun forceRestoreDefaultBrightness(): Boolean {
        return try {
            if (!hasWriteSettingsPermission()) {
                Log.w(TAG, "⚠️ 没有WRITE_SETTINGS权限，无法恢复默认屏幕亮度")
                return false
            }
            
            Log.i(TAG, "🔧 强制恢复默认亮度")
            
            val success = setBrightness(DEFAULT_BRIGHTNESS)
            
            if (success) {
                Log.i(TAG, "✅ 默认亮度已恢复: $DEFAULT_BRIGHTNESS")
            } else {
                Log.w(TAG, "⚠️ 强制恢复默认亮度失败")
            }
            
            // 重置状态
            resetBrightnessState()
            
            success
        } catch (e: Exception) {
            Log.e(TAG, "❌ 强制恢复默认亮度异常", e)
            // 确保重置状态
            resetBrightnessState()
            false
        }
    }
    
    /**
     * 获取亮度管理状态信息
     */
    fun getBrightnessStatus(): Map<String, Any> {
        return mapOf(
            "hasWriteSettingsPermission" to hasWriteSettingsPermission(),
            "isManagingBrightness" to isManagingBrightness,
            "brightnessRecorded" to brightnessRecorded,
            "originalBrightness" to originalBrightness,
            "originalBrightnessMode" to originalBrightnessMode,
            "currentBrightness" to getCurrentBrightness(),
            "currentBrightnessMode" to getCurrentBrightnessMode(),
            "minBrightness" to MIN_BRIGHTNESS,
            "defaultBrightness" to DEFAULT_BRIGHTNESS,
            "isHuaweiDevice" to isHuaweiDevice,
            "isVivoDevice" to isVivoDevice,
            "deviceBrand" to deviceBrand,
            "deviceManufacturer" to deviceManufacturer,
            "huaweiMinBrightness" to if (isHuaweiDevice) HUAWEI_MIN_BRIGHTNESS else "N/A",
            "vivoUseDarkOverlay" to if (isVivoDevice) VIVO_USE_DARK_OVERLAY else "N/A",
            "brightnessStrategy" to when {
                isVivoDevice -> "深色遮盖替代方案"
                isHuaweiDevice -> "华为设备重试机制"
                else -> "标准亮度调节"
            }
        )
    }
    
    /**
     * 清理资源
     */
    fun cleanup() {
        try {
            Log.d(TAG, "🧹 清理屏幕亮度管理器资源")
            
            // 如果正在管理亮度，尝试恢复
            if (isManagingBrightness) {
                restoreOriginalBrightness()
            }
            
            Log.i(TAG, "✅ 屏幕亮度管理器清理完成")
            
        } catch (e: Exception) {
            Log.e(TAG, "❌ 清理屏幕亮度管理器失败", e)
        }
    }
} 