package com.jld.vod.utils

import android.app.Activity
import android.content.Context
import android.content.Intent
import android.net.Uri
import android.os.Build
import android.provider.Settings
import android.view.Window
import android.view.WindowManager

/**
 * @author ZhaoFuXin
 * @Email：18276061387@163.com
 * @description:
 * @date :2020/8/27 10:42
 */
object BrightnessUtils {

    /**
     * 返回背光值
     */
    fun adjustBrightnessNumber(brightness: Int): Int {
        var brightness = brightness
        if (brightness < 0) {
            brightness = 0
        } else if (brightness > 255) {
            brightness = 255
        }
        return brightness
    }

    /**
     * 设置系统亮度
     */
    fun setSystemBrightess(mContext:Context,brightness: Int)
    {
        //设置系统亮度

        //设置系统亮度
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            if (!Settings.System.canWrite(mContext)) {
                val intent = Intent(Settings.ACTION_MANAGE_WRITE_SETTINGS)
                intent.setData(Uri.parse("package:" + mContext.getPackageName()))
                intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
                mContext.startActivity(intent)
            } else {
                //有了权限，具体的动作
                Settings.System.putInt(mContext.contentResolver,
                    Settings.System.SCREEN_BRIGHTNESS,adjustBrightnessNumber(brightness));
            }
        }

    }

    /**
     * 设置应用内亮度
     */
    fun setBrightness(mContext: Activity, brightValue:Int)
    {
        val window: Window = mContext.getWindow()
        val lp: WindowManager.LayoutParams = mContext.getWindow ().getAttributes();
        lp.screenBrightness = adjustBrightnessNumber(brightValue).toFloat()
        mContext.getWindow().setAttributes(lp);
    }
    /**
     * 获取系统亮度
     */
    fun getSystemBrightness(mContext:Context): Int {
        return Settings.System.getInt(mContext.contentResolver, Settings.System.SCREEN_BRIGHTNESS, 255)
    }

    /**
     * 获取亮度
     */
    fun getAppSystemBrightness(mContext:Activity): Int {
        return Settings.System.getInt(mContext.contentResolver, Settings.System.SCREEN_BRIGHTNESS)
    }
}