diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/player/Gestures.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/player/Gestures.kt index 0237d20aa..ef54578ee 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/player/Gestures.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/player/Gestures.kt @@ -65,11 +65,15 @@ class Gestures( val dy = e1.y - e2.y when (scrollState) { STATE_UP -> { - if (abs(dx) > trigger) { + if (abs(dx) >= trigger) { scrollState = STATE_HORIZONTAL activity.initSeek() } 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 -> { diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/player/PlayerActivity.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/player/PlayerActivity.kt index 7ae0757e1..c24493130 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/player/PlayerActivity.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/player/PlayerActivity.kt @@ -5,6 +5,7 @@ import android.annotation.SuppressLint import android.content.Context import android.content.Intent import android.content.res.ColorStateList +import android.graphics.Color import android.media.AudioManager import android.os.Build import android.os.Bundle @@ -44,6 +45,7 @@ import `is`.xyz.mpv.Utils import logcat.LogPriority import nucleus.factory.RequiresPresenter import uy.kohesive.injekt.injectLazy +import kotlin.math.abs import kotlin.math.roundToInt @RequiresPresenter(PlayerPresenter::class) @@ -72,6 +74,36 @@ class PlayerActivity : private var fineVolume = 0F 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 userIsOperatingSeekbar = false @@ -148,6 +180,14 @@ class PlayerActivity : fineVolume = audioManager!!.getStreamVolume(AudioManager.STREAM_MUSIC).toFloat() 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() windowManager.defaultDisplay.getRealMetrics(dm) val width = dm.widthPixels.toFloat() @@ -395,11 +435,20 @@ class PlayerActivity : } fun verticalScrollLeft(diff: Float) { - val initialBright = Utils.getScreenBrightness(this) ?: 0.5f - val newBright = (initialBright + diff).coerceIn(0f, 1f) - window.attributes.screenBrightness = newBright + brightness = (brightness + diff).coerceIn(-0.75F, 1F) + window.attributes = window.attributes.apply { + // 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() } @@ -579,6 +628,8 @@ class PlayerActivity : playerIsDestroyed = true player.destroy() } + @Suppress("DEPRECATION") + audioManager?.abandonAudioFocus(audioFocusChangeListener) super.onDestroy() } @@ -725,9 +776,15 @@ class PlayerActivity : } } + @Suppress("DEPRECATION") private fun eventPropertyUi(property: String, value: Boolean) { when (property) { - "pause" -> updatePlaybackStatus(value) + "pause" -> { + if (!value) audioManager!!.requestAudioFocus( + audioFocusChangeListener, AudioManager.STREAM_MUSIC, AudioManager.AUDIOFOCUS_GAIN + ) + updatePlaybackStatus(value) + } } } diff --git a/app/src/main/res/layout/player_activity.xml b/app/src/main/res/layout/player_activity.xml index 7474a7c15..a7d41a13f 100644 --- a/app/src/main/res/layout/player_activity.xml +++ b/app/src/main/res/layout/player_activity.xml @@ -264,4 +264,10 @@ android:textStyle="bold" android:visibility="gone" /> + + \ No newline at end of file