fix(player): Check for phone's maximum brightness value

This commit is contained in:
jmir1 2024-06-30 23:32:45 +02:00
parent 39ba4324e2
commit fb9f463316
No known key found for this signature in database
GPG key ID: 7B3B624787A072BD

View file

@ -18,6 +18,8 @@ import android.os.Environment
import android.os.Handler import android.os.Handler
import android.os.Looper import android.os.Looper
import android.os.ParcelFileDescriptor import android.os.ParcelFileDescriptor
import android.os.PowerManager
import android.provider.Settings
import android.support.v4.media.session.MediaControllerCompat import android.support.v4.media.session.MediaControllerCompat
import android.support.v4.media.session.MediaSessionCompat import android.support.v4.media.session.MediaSessionCompat
import android.support.v4.media.session.PlaybackStateCompat import android.support.v4.media.session.PlaybackStateCompat
@ -85,7 +87,6 @@ import eu.kanade.tachiyomi.util.system.toShareIntent
import eu.kanade.tachiyomi.util.system.toast import eu.kanade.tachiyomi.util.system.toast
import eu.kanade.tachiyomi.util.view.setComposeContent import eu.kanade.tachiyomi.util.view.setComposeContent
import `is`.xyz.mpv.MPVLib import `is`.xyz.mpv.MPVLib
import `is`.xyz.mpv.Utils
import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.flow.launchIn import kotlinx.coroutines.flow.launchIn
@ -748,13 +749,45 @@ class PlayerActivity : BaseActivity() {
!playerPreferences.rememberPlayerBrightness().get() !playerPreferences.rememberPlayerBrightness().get()
brightness = if (useDeviceBrightness) { brightness = if (useDeviceBrightness) {
Utils.getScreenBrightness(this) ?: 0.5F getCurrentBrightness()
} else { } else {
playerPreferences.playerBrightnessValue().get() playerPreferences.playerBrightnessValue().get()
} }
verticalScrollLeft(0F) verticalScrollLeft(0F)
} }
private fun getMaxBrightness(): Float {
val powerManager = getSystemService(POWER_SERVICE) as? PowerManager
if (powerManager != null) {
val fields = powerManager.javaClass.declaredFields
for (field in fields) {
// https://android.googlesource.com/platform/frameworks/base/+/refs/heads/master/core/java/android/os/PowerManager.java
if (field.getName().equals("BRIGHTNESS_ON")) {
field.isAccessible = true
return try {
(field.get(powerManager) as Int).toFloat()
} catch (e: IllegalAccessException) {
255F
}
}
}
}
return 255F
}
private fun getCurrentBrightness(): Float {
// check if window has brightness set
val lp = window.attributes
if (lp.screenBrightness >= 0f) return lp.screenBrightness
val resolver = contentResolver
return try {
Settings.System.getInt(resolver, Settings.System.SCREEN_BRIGHTNESS) / getMaxBrightness()
} catch (e: Settings.SettingNotFoundException) {
0.5F
}
}
@Suppress("DEPRECATION") @Suppress("DEPRECATION")
private fun setupMediaSession() { private fun setupMediaSession() {
mediaSession = MediaSessionCompat(this, "Aniyomi_Player_Session").apply { mediaSession = MediaSessionCompat(this, "Aniyomi_Player_Session").apply {