fix brightness and add audio focus listener

This commit is contained in:
jmir1 2022-03-30 23:11:19 +02:00
parent 38366ae669
commit 3ab104f78c
3 changed files with 74 additions and 7 deletions

View file

@ -65,11 +65,15 @@ class Gestures(
val dy = e1.y - e2.y val dy = e1.y - e2.y
when (scrollState) { when (scrollState) {
STATE_UP -> { STATE_UP -> {
if (abs(dx) > trigger) { if (abs(dx) >= trigger) {
scrollState = STATE_HORIZONTAL scrollState = STATE_HORIZONTAL
activity.initSeek() activity.initSeek()
} else if (abs(dy) > trigger) { } else if (abs(dy) > trigger) {
scrollState = if (e1.x > width / 2) STATE_VERTICAL_R else STATE_VERTICAL_L scrollState = when {
e1.x > width * 0.6F -> STATE_VERTICAL_R
e1.x < width * 0.4F -> STATE_VERTICAL_L
else -> STATE_UP
}
} }
} }
STATE_VERTICAL_L -> { STATE_VERTICAL_L -> {

View file

@ -5,6 +5,7 @@ import android.annotation.SuppressLint
import android.content.Context import android.content.Context
import android.content.Intent import android.content.Intent
import android.content.res.ColorStateList import android.content.res.ColorStateList
import android.graphics.Color
import android.media.AudioManager import android.media.AudioManager
import android.os.Build import android.os.Build
import android.os.Bundle import android.os.Bundle
@ -44,6 +45,7 @@ import `is`.xyz.mpv.Utils
import logcat.LogPriority import logcat.LogPriority
import nucleus.factory.RequiresPresenter import nucleus.factory.RequiresPresenter
import uy.kohesive.injekt.injectLazy import uy.kohesive.injekt.injectLazy
import kotlin.math.abs
import kotlin.math.roundToInt import kotlin.math.roundToInt
@RequiresPresenter(PlayerPresenter::class) @RequiresPresenter(PlayerPresenter::class)
@ -72,6 +74,36 @@ class PlayerActivity :
private var fineVolume = 0F private var fineVolume = 0F
private var maxVolume = 0 private var maxVolume = 0
private var brightness = 0F
private var audioFocusRestore: () -> Unit = {}
private val audioFocusChangeListener = AudioManager.OnAudioFocusChangeListener { type ->
when (type) {
AudioManager.AUDIOFOCUS_LOSS,
AudioManager.AUDIOFOCUS_LOSS_TRANSIENT -> {
// loss can occur in addition to ducking, so remember the old callback
val oldRestore = audioFocusRestore
val wasPlayerPaused = player.paused ?: false
player.paused = true
audioFocusRestore = {
oldRestore()
if (!wasPlayerPaused) player.paused = false
}
}
AudioManager.AUDIOFOCUS_LOSS_TRANSIENT_CAN_DUCK -> {
MPVLib.command(arrayOf("multiply", "volume", 0.5F.toString()))
audioFocusRestore = {
MPVLib.command(arrayOf("multiply", "volume", 2F.toString()))
}
}
AudioManager.AUDIOFOCUS_GAIN -> {
audioFocusRestore()
audioFocusRestore = {}
}
}
}
private var initialSeek = -1 private var initialSeek = -1
private var userIsOperatingSeekbar = false private var userIsOperatingSeekbar = false
@ -148,6 +180,14 @@ class PlayerActivity :
fineVolume = audioManager!!.getStreamVolume(AudioManager.STREAM_MUSIC).toFloat() fineVolume = audioManager!!.getStreamVolume(AudioManager.STREAM_MUSIC).toFloat()
maxVolume = audioManager!!.getStreamMaxVolume(AudioManager.STREAM_MUSIC) maxVolume = audioManager!!.getStreamMaxVolume(AudioManager.STREAM_MUSIC)
brightness = Utils.getScreenBrightness(this) ?: 0.5F
volumeControlStream = AudioManager.STREAM_MUSIC
audioManager!!.requestAudioFocus(
audioFocusChangeListener, AudioManager.STREAM_MUSIC, AudioManager.AUDIOFOCUS_GAIN
)
val dm = DisplayMetrics() val dm = DisplayMetrics()
windowManager.defaultDisplay.getRealMetrics(dm) windowManager.defaultDisplay.getRealMetrics(dm)
val width = dm.widthPixels.toFloat() val width = dm.widthPixels.toFloat()
@ -395,11 +435,20 @@ class PlayerActivity :
} }
fun verticalScrollLeft(diff: Float) { fun verticalScrollLeft(diff: Float) {
val initialBright = Utils.getScreenBrightness(this) ?: 0.5f brightness = (brightness + diff).coerceIn(-0.75F, 1F)
val newBright = (initialBright + diff).coerceIn(0f, 1f) window.attributes = window.attributes.apply {
window.attributes.screenBrightness = newBright // value of 0 and 0.01 is broken somehow
screenBrightness = brightness.coerceAtLeast(0.02F)
}
if (brightness < 0) {
binding.brightnessOverlay.isVisible = true
val alpha = (abs(brightness) * 256).toInt()
binding.brightnessOverlay.setBackgroundColor(Color.argb(alpha, 0, 0, 0))
} else {
binding.brightnessOverlay.isVisible = false
}
binding.gestureTextView.text = getString(R.string.ui_brightness, (newBright * 100).roundToInt()) binding.gestureTextView.text = getString(R.string.ui_brightness, (brightness * 100).roundToInt())
showGestureText() showGestureText()
} }
@ -579,6 +628,8 @@ class PlayerActivity :
playerIsDestroyed = true playerIsDestroyed = true
player.destroy() player.destroy()
} }
@Suppress("DEPRECATION")
audioManager?.abandonAudioFocus(audioFocusChangeListener)
super.onDestroy() super.onDestroy()
} }
@ -725,9 +776,15 @@ class PlayerActivity :
} }
} }
@Suppress("DEPRECATION")
private fun eventPropertyUi(property: String, value: Boolean) { private fun eventPropertyUi(property: String, value: Boolean) {
when (property) { when (property) {
"pause" -> updatePlaybackStatus(value) "pause" -> {
if (!value) audioManager!!.requestAudioFocus(
audioFocusChangeListener, AudioManager.STREAM_MUSIC, AudioManager.AUDIOFOCUS_GAIN
)
updatePlaybackStatus(value)
}
} }
} }

View file

@ -264,4 +264,10 @@
android:textStyle="bold" android:textStyle="bold"
android:visibility="gone" /> android:visibility="gone" />
<View
android:id="@+id/brightness_overlay"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:visibility="gone" />
</RelativeLayout> </RelativeLayout>