2023-01-31 21:13:39 +05:30
|
|
|
package com.github.libretube.helpers
|
2022-11-25 16:26:45 +05:30
|
|
|
|
|
|
|
import android.app.Activity
|
|
|
|
import android.view.WindowManager
|
|
|
|
import com.github.libretube.extensions.normalize
|
|
|
|
|
2023-05-21 22:41:14 +05:30
|
|
|
class BrightnessHelper(activity: Activity) {
|
2022-11-25 16:26:45 +05:30
|
|
|
private val window = activity.window
|
|
|
|
private val minBrightness = 0.0f
|
|
|
|
private val maxBrightness = 1.0f
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Wrapper for the current screen brightness
|
|
|
|
*/
|
2022-11-26 11:42:46 +05:30
|
|
|
private var brightness: Float
|
2022-11-25 16:26:45 +05:30
|
|
|
get() = window.attributes.screenBrightness
|
2022-11-26 11:42:46 +05:30
|
|
|
set(value) {
|
2023-05-21 22:41:14 +05:30
|
|
|
window.attributes = window.attributes.apply {
|
|
|
|
screenBrightness = value
|
|
|
|
}
|
2022-11-25 16:26:45 +05:30
|
|
|
}
|
|
|
|
|
2022-11-26 11:42:46 +05:30
|
|
|
/**
|
2023-05-21 22:41:14 +05:30
|
|
|
* Wrapper for the brightness saved per session, set to the current screen brightness of the
|
|
|
|
* beginning of each session / player creation.
|
2022-11-26 11:42:46 +05:30
|
|
|
*/
|
2023-05-21 22:41:14 +05:30
|
|
|
private var savedBrightness = window.attributes.screenBrightness
|
2022-11-26 11:42:46 +05:30
|
|
|
|
2022-11-25 16:26:45 +05:30
|
|
|
/**
|
|
|
|
* Restore screen brightness to device system brightness.
|
2022-11-26 14:07:03 +05:30
|
|
|
* if [forced] is false then value will be stored only if it's not
|
|
|
|
* [WindowManager.LayoutParams.BRIGHTNESS_OVERRIDE_NONE] value.
|
2022-11-25 16:26:45 +05:30
|
|
|
*/
|
2022-11-26 14:07:03 +05:30
|
|
|
fun resetToSystemBrightness(forced: Boolean = true) {
|
|
|
|
if (forced || brightness != WindowManager.LayoutParams.BRIGHTNESS_OVERRIDE_NONE) {
|
|
|
|
savedBrightness = brightness
|
2022-11-26 11:42:46 +05:30
|
|
|
}
|
2022-11-25 16:26:45 +05:30
|
|
|
brightness = WindowManager.LayoutParams.BRIGHTNESS_OVERRIDE_NONE
|
|
|
|
}
|
|
|
|
|
2022-11-26 11:42:46 +05:30
|
|
|
/**
|
|
|
|
* Set current screen brightness to saved brightness value.
|
|
|
|
*/
|
|
|
|
fun restoreSavedBrightness() {
|
|
|
|
brightness = savedBrightness
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set current brightness value with scaling to given range.
|
|
|
|
* [shouldSave] determines whether the value should be persisted.
|
|
|
|
*/
|
2022-12-19 21:28:34 +05:30
|
|
|
fun setBrightnessWithScale(
|
|
|
|
value: Float,
|
|
|
|
maxValue: Float,
|
|
|
|
minValue: Float = 0.0f,
|
2023-05-09 22:11:05 +05:30
|
|
|
shouldSave: Boolean = false,
|
2022-12-19 21:28:34 +05:30
|
|
|
) {
|
2022-11-25 16:26:45 +05:30
|
|
|
brightness = value.normalize(minValue, maxValue, minBrightness, maxBrightness)
|
2022-11-26 11:42:46 +05:30
|
|
|
if (shouldSave) savedBrightness = brightness
|
2022-11-25 16:26:45 +05:30
|
|
|
}
|
|
|
|
|
2022-11-26 11:42:46 +05:30
|
|
|
/**
|
|
|
|
* Get scaled brightness with given range. if [saved] is
|
2023-05-21 22:41:14 +05:30
|
|
|
* true value will be restored from the session (per played queue)
|
2022-11-26 11:42:46 +05:30
|
|
|
*/
|
2022-12-23 19:27:10 +05:30
|
|
|
fun getBrightnessWithScale(
|
|
|
|
maxValue: Float,
|
|
|
|
minValue: Float = 0.0f,
|
2023-05-09 22:11:05 +05:30
|
|
|
saved: Boolean = false,
|
2022-12-23 19:27:10 +05:30
|
|
|
): Float {
|
2022-11-26 11:42:46 +05:30
|
|
|
return if (saved) {
|
|
|
|
savedBrightness.normalize(minBrightness, maxBrightness, minValue, maxValue)
|
|
|
|
} else {
|
|
|
|
brightness.normalize(minBrightness, maxBrightness, minValue, maxValue)
|
|
|
|
}
|
2022-11-25 16:26:45 +05:30
|
|
|
}
|
|
|
|
}
|