LibreTube/app/src/main/java/com/github/libretube/util/BrightnessHelper.kt
Krunal Patel 612a23c1dc Fix pip mode brightness issue
Check if activity is in pip mode then use system brightness.
Don't force to store brightness while returning from pip mode. Since it contains system brightness.
2022-11-26 14:17:02 +05:30

76 lines
2.6 KiB
Kotlin

package com.github.libretube.util
import android.app.Activity
import android.os.Build
import android.view.WindowManager
import com.github.libretube.constants.PreferenceKeys
import com.github.libretube.extensions.normalize
class BrightnessHelper(private val activity: Activity) {
private val window = activity.window
private val minBrightness = 0.0f
private val maxBrightness = 1.0f
/**
* Wrapper for the current screen brightness
*/
private var brightness: Float
get() = window.attributes.screenBrightness
set(value) {
val lp = window.attributes
lp.screenBrightness = value
window.attributes = lp
}
/**
* 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)
/**
* Restore screen brightness to device system brightness.
* if [forced] is false then value will be stored only if it's not
* [WindowManager.LayoutParams.BRIGHTNESS_OVERRIDE_NONE] value.
*/
fun resetToSystemBrightness(forced: Boolean = true) {
if (forced || brightness != WindowManager.LayoutParams.BRIGHTNESS_OVERRIDE_NONE) {
savedBrightness = brightness
}
brightness = WindowManager.LayoutParams.BRIGHTNESS_OVERRIDE_NONE
}
/**
* Set current screen brightness to saved brightness value.
*/
fun restoreSavedBrightness() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N && activity.isInPictureInPictureMode) {
return
}
brightness = savedBrightness
}
/**
* Set current brightness value with scaling to given range.
* [shouldSave] determines whether the value should be persisted.
*/
fun setBrightnessWithScale(value: Float, maxValue: Float, minValue: Float = 0.0f, shouldSave: Boolean = false) {
brightness = value.normalize(minValue, maxValue, minBrightness, maxBrightness)
if (shouldSave) savedBrightness = brightness
}
/**
* Get scaled brightness with given range. if [saved] is
* ture value will be retrived from shared preferences.
*/
fun getBrightnessWithScale(maxValue: Float, minValue: Float = 0.0f, saved: Boolean = false): Float {
return if (saved) {
savedBrightness.normalize(minBrightness, maxBrightness, minValue, maxValue)
} else {
brightness.normalize(minBrightness, maxBrightness, minValue, maxValue)
}
}
}