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
|
2022-11-26 11:42:46 +05:30
|
|
|
import com.github.libretube.constants.PreferenceKeys
|
2022-11-25 16:26:45 +05:30
|
|
|
import com.github.libretube.extensions.normalize
|
|
|
|
|
2022-11-26 14:07:03 +05:30
|
|
|
class BrightnessHelper(private val 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) {
|
2022-11-25 16:26:45 +05:30
|
|
|
val lp = window.attributes
|
|
|
|
lp.screenBrightness = value
|
|
|
|
window.attributes = lp
|
|
|
|
}
|
|
|
|
|
2022-11-26 11:42:46 +05:30
|
|
|
/**
|
|
|
|
* Wrapper for the brightness persisted in the shared preferences.
|
|
|
|
*/
|
|
|
|
private var savedBrightness: Float
|
|
|
|
get() = PreferenceHelper.getFloat(PreferenceKeys.PLAYER_SCREEN_BRIGHTNESS, brightness)
|
|
|
|
set(value) = PreferenceHelper.putFloat(PreferenceKeys.PLAYER_SCREEN_BRIGHTNESS, value)
|
|
|
|
|
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
|
|
|
|
* ture value will be retrived from shared preferences.
|
|
|
|
*/
|
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
|
|
|
}
|
|
|
|
}
|