make brightnes change work

This commit is contained in:
Christian Schabesberger 2024-08-09 11:37:45 +02:00
parent cb39937594
commit 1fde8569ce
10 changed files with 63 additions and 61 deletions

View File

@ -42,7 +42,7 @@ data class VideoPlayerUIState(
val soundVolume: Float,
// when null use system value
val brightnes: Float?
val brightness: Float?
) : Parcelable {
companion object {
val DEFAULT = VideoPlayerUIState(
@ -60,7 +60,7 @@ data class VideoPlayerUIState(
playbackPositionInMs = 0,
fastseekSeconds = 0,
soundVolume = 0f,
brightnes = null
brightness = null
)
}
}

View File

@ -50,7 +50,7 @@ interface VideoPlayerViewModel {
fun embeddedDraggedDown(offset: Float)
fun fastSeek(count: Int)
fun finishFastSeek()
fun brightnesChange(changeRate: Float, currentValue: Float)
fun brightnessChange(changeRate: Float, systemBrightness: Float)
fun volumeChange(changeRate: Float)
interface Listener {

View File

@ -21,13 +21,11 @@
package net.newpipe.newplayer.model
import android.app.Application
import android.content.Context.AUDIO_SERVICE
import android.media.AudioManager
import android.os.Build
import android.os.Bundle
import android.util.Log
import androidx.annotation.RequiresApi
import androidx.core.content.ContextCompat
import androidx.core.content.ContextCompat.getSystemService
import androidx.lifecycle.AndroidViewModel
import androidx.lifecycle.SavedStateHandle
@ -44,7 +42,6 @@ import kotlinx.coroutines.launch
import net.newpipe.newplayer.utils.VideoSize
import net.newpipe.newplayer.NewPlayer
import net.newpipe.newplayer.ui.ContentScale
import kotlin.math.max
val VIDEOPLAYER_UI_STATE = "video_player_ui_state"
@ -303,8 +300,20 @@ class VideoPlayerViewModelImpl @Inject constructor(
}
}
override fun brightnesChange(changeRate: Float, currentValue: Float) {
TODO("Not yet implemented")
override fun brightnessChange(changeRate: Float, systemBrightness: Float) {
if (mutableUiState.value.fullscreen) {
val currentBrightness = mutableUiState.value.brightness
?: if (systemBrightness < 0f) 0.5f else systemBrightness
Log.d(
TAG,
"currentBrightnes: $currentBrightness, sytemBrightness: $systemBrightness, changeRate: $changeRate"
)
val newBrightness = (currentBrightness + changeRate).coerceIn(0f, 1f)
mutableUiState.update {
it.copy(brightness = newBrightness)
}
}
}
override fun volumeChange(changeRate: Float) {
@ -406,7 +415,7 @@ class VideoPlayerViewModelImpl @Inject constructor(
println("dummy impl")
}
override fun brightnesChange(changeRate: Float, currentValue: Float) {
override fun brightnessChange(changeRate: Float, currentValue: Float) {
println("dummy impl")
}

View File

@ -21,6 +21,7 @@
package net.newpipe.newplayer.ui
import android.app.Activity
import androidx.activity.compose.BackHandler
import androidx.compose.animation.AnimatedVisibility
import androidx.compose.foundation.layout.Box
@ -44,6 +45,7 @@ import androidx.compose.runtime.Composable
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import net.newpipe.newplayer.ui.theme.VideoPlayerTheme
@ -51,6 +53,7 @@ import net.newpipe.newplayer.ui.videoplayer.BottomUI
import net.newpipe.newplayer.ui.videoplayer.CenterUI
import net.newpipe.newplayer.ui.videoplayer.TopUI
import net.newpipe.newplayer.ui.videoplayer.GestureUI
import net.newpipe.newplayer.utils.getScreenBrightnes
@Composable
fun VideoPlayerControllerUI(
@ -78,18 +81,21 @@ fun VideoPlayerControllerUI(
embeddedDraggedDownBy: (Float) -> Unit,
fastSeek: (Int) -> Unit,
finishFastSeek: () -> Unit,
brightnesChange: (Float, Float) -> Unit,
brightnessChange: (Float, Float) -> Unit,
volumeChange: (Float) -> Unit
) {
val context = LocalContext.current
if (fullscreen) {
BackHandler {
switchToEmbeddedView()
}
}
val internalBrightnesChange = { rateChange: Float ->
val internalBrightnessChange = { rateChange: Float ->
val systemBrightness = getScreenBrightnes(context as Activity)
brightnessChange(rateChange, systemBrightness)
}
val insets =
@ -114,7 +120,7 @@ fun VideoPlayerControllerUI(
embeddedDraggedDownBy = embeddedDraggedDownBy,
fastSeek = fastSeek,
fastSeekFinished = finishFastSeek,
brightnesChange = internalBrightnesChange,
brightnessChange = internalBrightnessChange,
volumeChange = volumeChange
)
}
@ -155,7 +161,7 @@ fun VideoPlayerControllerUI(
fastSeek = fastSeek,
fastSeekFinished = finishFastSeek,
volumeChange = volumeChange,
brightnesChange = internalBrightnesChange
brightnessChange = internalBrightnessChange
)
Box(modifier = Modifier.fillMaxSize()) {
@ -254,7 +260,7 @@ fun VideoPlayerControllerUIPreviewEmbedded() {
embeddedDraggedDownBy = {},
fastSeek = {},
finishFastSeek = {},
internalBrightnesChange = { a, b ->},
brightnessChange = { a, b ->},
volumeChange = {})
}
}
@ -289,7 +295,7 @@ fun VideoPlayerControllerUIPreviewLandscape() {
embeddedDraggedDownBy = {},
fastSeek = {},
finishFastSeek = {},
internalBrightnesChange = { a, b -> },
brightnessChange = { a, b -> },
volumeChange = {})
}
}
@ -325,7 +331,7 @@ fun VideoPlayerControllerUIPreviewPortrait() {
embeddedDraggedDownBy = {},
fastSeek = {},
finishFastSeek = {},
internalBrightnesChange = { a, b -> },
brightnessChange = { a, b -> },
volumeChange = {})
}
}

View File

@ -22,6 +22,7 @@ package net.newpipe.newplayer.ui
import android.app.Activity
import android.content.pm.ActivityInfo
import android.util.Log
import android.view.SurfaceView
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.aspectRatio
@ -55,6 +56,10 @@ import net.newpipe.newplayer.model.VideoPlayerViewModel
import net.newpipe.newplayer.model.VideoPlayerViewModelImpl
import net.newpipe.newplayer.ui.theme.VideoPlayerTheme
import net.newpipe.newplayer.utils.LockScreenOrientation
import net.newpipe.newplayer.utils.getScreenBrightnes
import net.newpipe.newplayer.utils.setScreenBrightnes
private const val TAG = "VideoPlayerUI"
@Composable
fun VideoPlayerUI(
@ -90,7 +95,7 @@ fun VideoPlayerUI(
}
// Setup immersive mode
if (uiState.fullscreen && ! uiState.uiVissible) {
if (uiState.fullscreen && !uiState.uiVissible) {
LaunchedEffect(key1 = true) {
windowInsetsController.hide(WindowInsetsCompat.Type.systemBars())
}
@ -125,6 +130,16 @@ fun VideoPlayerUI(
val screenRatio =
displayMetrics.widthPixels.toFloat() / displayMetrics.heightPixels.toFloat()
val systemScreenBrightnes = getScreenBrightnes(activity)
LaunchedEffect(key1 = uiState.brightness) {
Log.d(TAG, "New Brightnes: ${uiState.brightness}")
setScreenBrightnes(
uiState.brightness
?: if (systemScreenBrightnes < 0f) 0.5f else systemScreenBrightnes, activity
)
}
// Set UI
Surface(
modifier = Modifier.then(
@ -143,6 +158,7 @@ fun VideoPlayerUI(
contentRatio = uiState.contentRatio
)
}
VideoPlayerControllerUI(
isPlaying = uiState.playing,
fullscreen = uiState.fullscreen,
@ -153,7 +169,8 @@ fun VideoPlayerUI(
playbackPositionInMs = uiState.playbackPositionInMs,
bufferedPercentage = uiState.bufferedPercentage,
fastSeekSeconds = uiState.fastseekSeconds,
brightnes = uiState.brightnes,
brightnes = uiState.brightness
?: if (systemScreenBrightnes < 0f) 0.5f else systemScreenBrightnes,
soundVolume = uiState.soundVolume,
play = viewModel::play,
pause = viewModel::pause,
@ -169,7 +186,7 @@ fun VideoPlayerUI(
fastSeek = viewModel::fastSeek,
finishFastSeek = viewModel::finishFastSeek,
volumeChange = viewModel::volumeChange,
internalBrightnesChange = viewModel::brightnesChange
brightnessChange = viewModel::brightnessChange
)
}
}

View File

@ -96,7 +96,7 @@ fun VideoPlayerControllerUIPreviewEmbeddedColorPreview() {
embeddedDraggedDownBy = {},
fastSeek = {},
finishFastSeek = {},
internalBrightnesChange = {},
brightnessChange = {a, b ->},
volumeChange = {})
}
}

View File

@ -20,32 +20,10 @@
package net.newpipe.newplayer.ui.videoplayer
import android.util.Log
import androidx.compose.animation.AnimatedVisibility
import androidx.compose.animation.core.tween
import androidx.compose.animation.fadeIn
import androidx.compose.animation.fadeOut
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.wrapContentSize
import androidx.compose.material3.Surface
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.tooling.preview.Preview
import net.newpipe.newplayer.ui.theme.VideoPlayerTheme
import net.newpipe.newplayer.ui.videoplayer.gesture_ui.EmbeddedGestureUI
import net.newpipe.newplayer.ui.videoplayer.gesture_ui.FastSeekVisualFeedback
import net.newpipe.newplayer.ui.videoplayer.gesture_ui.FullscreenGestureUI
import net.newpipe.newplayer.ui.videoplayer.gesture_ui.FullscreenGestureUIPreview
import net.newpipe.newplayer.ui.videoplayer.gesture_ui.TouchSurface
import net.newpipe.newplayer.ui.videoplayer.gesture_ui.TouchedPosition
private const val TAG = "TouchUi"
@ -72,7 +50,7 @@ fun GestureUI(
fastSeek: (Int) -> Unit,
fastSeekFinished: () -> Unit,
volumeChange: (Float) -> Unit,
brightnesChange: (Float) -> Unit,
brightnessChange: (Float) -> Unit,
) {
val defaultOnRegularTap = {
if (uiVissible) {
@ -94,7 +72,7 @@ fun GestureUI(
switchToEmbeddedView = switchToEmbeddedView,
fastSeekFinished = fastSeekFinished,
volumeChange = volumeChange,
brightnesChange = brightnesChange)
brightnesChange = brightnessChange)
} else {
EmbeddedGestureUI(
fastSeekSeconds = fastSeekSeconds,

View File

@ -23,8 +23,6 @@ package net.newpipe.newplayer.ui.videoplayer.gesture_ui
import androidx.compose.animation.AnimatedVisibility
import androidx.compose.animation.core.tween
import androidx.compose.animation.fadeIn
import androidx.compose.animation.fadeOut
import androidx.compose.animation.scaleIn
import androidx.compose.animation.scaleOut
import androidx.compose.foundation.layout.Box
@ -170,7 +168,7 @@ fun FullscreenGestureUI(
VolumeCircle(
volumeFraction = brightnes,
modifier = Modifier.align(Alignment.Center),
isBrightnes = true
isBrightness = true
)
}
}

View File

@ -23,9 +23,7 @@ package net.newpipe.newplayer.ui.videoplayer.gesture_ui
import android.util.Log
import androidx.compose.foundation.Canvas
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.shape.CircleShape
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.automirrored.filled.VolumeDown
import androidx.compose.material.icons.automirrored.filled.VolumeMute
@ -40,7 +38,6 @@ import androidx.compose.ui.graphics.drawscope.Stroke
import androidx.compose.runtime.Composable
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.shadow
import androidx.compose.ui.geometry.Offset
import androidx.compose.ui.geometry.Size
import androidx.compose.ui.graphics.Color
@ -60,7 +57,7 @@ private const val CIRCLE_SIZE = 100
fun VolumeCircle(
modifier: Modifier = Modifier,
volumeFraction: Float,
isBrightnes: Boolean = false
isBrightness: Boolean = false
) {
assert(0f <= volumeFraction && volumeFraction <= 1f) {
Log.e(TAG, "Volume fraction must be in ragne [0;1]. It was $volumeFraction")
@ -87,10 +84,10 @@ fun VolumeCircle(
modifier = Modifier
.align(Alignment.Center)
.size(60.dp),
imageVector = (if (isBrightnes) getBrightnesIcon(volumeFraction = volumeFraction)
imageVector = (if (isBrightness) getBrightnesIcon(volumeFraction = volumeFraction)
else getVolumeIcon(volumeFraction = volumeFraction)),
contentDescription = stringResource(
id = if (isBrightnes) R.string.brightness_indicator
id = if (isBrightness) R.string.brightness_indicator
else R.string.volume_indicator
)
)
@ -115,7 +112,7 @@ private fun getBrightnesIcon(volumeFraction: Float) =
fun VolumeCirclePreview() {
VideoPlayerTheme {
Surface(color = Color.White) {
VolumeCircle(volumeFraction = 0.3f, isBrightnes = false)
VolumeCircle(volumeFraction = 0.3f, isBrightness = false)
}
}
}

View File

@ -24,6 +24,7 @@ import android.annotation.SuppressLint
import android.app.Activity
import android.content.Context
import android.content.ContextWrapper
import android.view.Window
import android.view.WindowManager
import androidx.compose.runtime.Composable
import androidx.compose.runtime.DisposableEffect
@ -44,18 +45,14 @@ fun LockScreenOrientation(orientation: Int) {
}
@SuppressLint("NewApi")
@Composable
fun getScreenBrightnes() : Float {
val activity = LocalContext.current.findActivity()!!
fun getScreenBrightnes(activity: Activity) : Float {
val window = activity.window
val layout = window.attributes as WindowManager.LayoutParams
return layout.screenBrightness
}
@SuppressLint("NewApi")
@Composable
fun SetScreenBrightnes(value:Float) {
val activity = LocalContext.current.findActivity()!!
fun setScreenBrightnes(value:Float, activity: Activity) {
val window = activity.window
val layout = window.attributes as WindowManager.LayoutParams
layout.screenBrightness = value