diff --git a/feature/mobile/player/src/main/java/com/flixclusive/feature/mobile/player/PlayerScreen.kt b/feature/mobile/player/src/main/java/com/flixclusive/feature/mobile/player/PlayerScreen.kt index 4bba72eb8..50b8e62cc 100644 --- a/feature/mobile/player/src/main/java/com/flixclusive/feature/mobile/player/PlayerScreen.kt +++ b/feature/mobile/player/src/main/java/com/flixclusive/feature/mobile/player/PlayerScreen.kt @@ -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 @@ -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 @@ -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 diff --git a/feature/mobile/player/src/main/java/com/flixclusive/feature/mobile/player/util/PlayerUiHelper.kt b/feature/mobile/player/src/main/java/com/flixclusive/feature/mobile/player/util/PlayerUiHelper.kt index 9856a6a49..59c58acd4 100644 --- a/feature/mobile/player/src/main/java/com/flixclusive/feature/mobile/player/util/PlayerUiHelper.kt +++ b/feature/mobile/player/src/main/java/com/flixclusive/feature/mobile/player/util/PlayerUiHelper.kt @@ -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 @@ -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 } } } \ No newline at end of file