Skip to content

Commit

Permalink
fix(player-ui): fix brightness issue where it is initially set at max…
Browse files Browse the repository at this point in the history
… brightness [attempt #1]
  • Loading branch information
rhenwinch committed Jul 3, 2024
1 parent 80e40cb commit 1ae5671
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import android.content.pm.ActivityInfo
import android.media.AudioManager
import android.os.Build
import android.os.Build.VERSION.SDK_INT
import android.provider.Settings
import android.view.KeyEvent
import androidx.activity.ComponentActivity
import androidx.annotation.OptIn
Expand Down Expand Up @@ -65,6 +64,7 @@ import com.flixclusive.core.util.android.getActivity
import com.flixclusive.core.util.film.FilmType
import com.flixclusive.feature.mobile.player.controls.PlayerControls
import com.flixclusive.feature.mobile.player.util.PlayerPipReceiver
import com.flixclusive.feature.mobile.player.util.getBrightness
import com.flixclusive.feature.mobile.player.util.percentOfVolume
import com.flixclusive.feature.mobile.player.util.setBrightness
import com.flixclusive.model.provider.SourceData
Expand Down Expand Up @@ -277,12 +277,7 @@ fun PlayerScreen(
context.toggleSystemBars(isVisible = false)

// Initialize brightness
val userDefaultBrightnessLevel = Settings.System.getInt(
context.contentResolver,
Settings.System.SCREEN_BRIGHTNESS,
-1
) / 255F

val userDefaultBrightnessLevel = context.getBrightness()
viewModel.updateScreenBrightness(userDefaultBrightnessLevel)

// Initialize volume level
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package com.flixclusive.feature.mobile.player.util
import android.app.Activity
import android.content.Context
import android.media.AudioManager
import android.provider.Settings

internal fun Context.percentOfVolume(
volume: Int? = null
Expand All @@ -15,10 +16,56 @@ internal fun Context.percentOfVolume(
return ((volume ?: currentVolume) / maxVolume.toFloat()).coerceIn(0F, 1F)
}

internal fun Activity.setBrightness(strength: Float) {
window?.apply {
attributes = attributes?.apply {
screenBrightness = strength
internal fun Activity.setBrightness(
strength: Float,
useTrueSystemBrightness: Boolean = false
) {
if (useTrueSystemBrightness) {
try {
Settings.System.putInt(
contentResolver,
Settings.System.SCREEN_BRIGHTNESS_MODE,
Settings.System.SCREEN_BRIGHTNESS_MODE_MANUAL
)

Settings.System.putInt(
contentResolver,
Settings.System.SCREEN_BRIGHTNESS, (strength * 255).toInt()
)
} catch (e: Exception) {
setBrightness(
strength = strength,
useTrueSystemBrightness = false
)
}
} else {
window?.apply {
attributes = attributes?.apply {
screenBrightness = strength
}
}
}

}

internal fun Activity.getBrightness(useTrueSystemBrightness: Boolean = true): Float {
return if (useTrueSystemBrightness) {
try {
Settings.System.getInt(
contentResolver,
Settings.System.SCREEN_BRIGHTNESS
) / 255F
} catch (e: Exception) {
// because true system brightness requires
// permission, this is a lazy way to check
// as it will throw an error if we do not have it
return getBrightness(false)
}
} else {
try {
window?.attributes?.screenBrightness ?: 0F
} catch (e: Exception) {
0F
}
}
}

0 comments on commit 1ae5671

Please sign in to comment.