package com.base.browserwhite.help

import android.content.Context
import android.content.Intent
import android.net.Uri
import android.provider.Settings

object BatteryOptimizationIntentHelper {


        // 获取请求忽略电池优化的 Intent
        fun getIgnoreBatteryOptimizationIntent(context: Context): Intent {
            val intent = Intent("android.settings.REQUEST_IGNORE_BATTERY_OPTIMIZATIONS")
            intent.data = Uri.parse("package:" + context.packageName)
            return if (areActivityIntent(context, intent)) {
                intent
            } else {
                getBatteryOptimizationSettingsIntent(context)
            }
        }

        // 获取电池优化设置的 Intent
        fun getBatteryOptimizationSettingsIntent(context: Context): Intent {
            val intent = Intent("android.settings.IGNORE_BATTERY_OPTIMIZATION_SETTINGS")
            return if (areActivityIntent(context, intent)) {
                intent
            } else {
                getApplicationDetailsIntent(context)
            }
        }

        // 获取应用详情页面的 Intent
        fun getApplicationDetailsIntent(context: Context): Intent {
            val intent = Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS)
            intent.data = Uri.parse("package:" + context.packageName)
            return intent
        }

        // 检查是否存在可以处理给定 Intent 的 Activity
        fun areActivityIntent(context: Context, intent: Intent): Boolean {
            return context.packageManager.queryIntentActivities(intent, 0).size > 0
        }
}