diff --git a/changelog.d/1519.bugfix b/changelog.d/1519.bugfix new file mode 100644 index 0000000000..35de17de6b --- /dev/null +++ b/changelog.d/1519.bugfix @@ -0,0 +1 @@ +Ensure screen does not turn off when playing a video diff --git a/features/messages/impl/src/main/kotlin/io/element/android/features/messages/impl/media/local/LocalMediaView.kt b/features/messages/impl/src/main/kotlin/io/element/android/features/messages/impl/media/local/LocalMediaView.kt index 3ca534c310..4348b83a95 100644 --- a/features/messages/impl/src/main/kotlin/io/element/android/features/messages/impl/media/local/LocalMediaView.kt +++ b/features/messages/impl/src/main/kotlin/io/element/android/features/messages/impl/media/local/LocalMediaView.kt @@ -66,6 +66,7 @@ import io.element.android.libraries.core.mimetype.MimeTypes.isMimeTypeVideo import io.element.android.libraries.designsystem.theme.components.Icon import io.element.android.libraries.designsystem.theme.components.Text import io.element.android.libraries.designsystem.utils.CommonDrawables +import io.element.android.libraries.designsystem.utils.KeepScreenOn import io.element.android.libraries.designsystem.utils.OnLifecycleEvent import io.element.android.libraries.theme.ElementTheme import me.saket.telephoto.zoomable.ZoomSpec @@ -152,6 +153,10 @@ private fun MediaVideoView( override fun onRenderedFirstFrame() { localMediaViewState.isReady = true } + + override fun onIsPlayingChanged(isPlaying: Boolean) { + localMediaViewState.isPlaying = isPlaying + } } val exoPlayer = remember { ExoPlayerWrapper.create(context) @@ -168,6 +173,7 @@ private fun MediaVideoView( } else { exoPlayer.setMediaItems(emptyList()) } + KeepScreenOn(localMediaViewState.isPlaying) AndroidView( factory = { PlayerView(context).apply { diff --git a/features/messages/impl/src/main/kotlin/io/element/android/features/messages/impl/media/local/LocalMediaViewState.kt b/features/messages/impl/src/main/kotlin/io/element/android/features/messages/impl/media/local/LocalMediaViewState.kt index e009c3f6cc..d5af7a78e1 100644 --- a/features/messages/impl/src/main/kotlin/io/element/android/features/messages/impl/media/local/LocalMediaViewState.kt +++ b/features/messages/impl/src/main/kotlin/io/element/android/features/messages/impl/media/local/LocalMediaViewState.kt @@ -26,6 +26,7 @@ import androidx.compose.runtime.setValue @Stable class LocalMediaViewState { var isReady: Boolean by mutableStateOf(false) + var isPlaying: Boolean by mutableStateOf(false) } @Composable diff --git a/libraries/designsystem/src/main/kotlin/io/element/android/libraries/designsystem/utils/KeepScreenOn.kt b/libraries/designsystem/src/main/kotlin/io/element/android/libraries/designsystem/utils/KeepScreenOn.kt new file mode 100644 index 0000000000..a9cf913e2a --- /dev/null +++ b/libraries/designsystem/src/main/kotlin/io/element/android/libraries/designsystem/utils/KeepScreenOn.kt @@ -0,0 +1,36 @@ +/* + * Copyright (c) 2023 New Vector Ltd + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package io.element.android.libraries.designsystem.utils + +import androidx.compose.runtime.Composable +import androidx.compose.runtime.DisposableEffect +import androidx.compose.ui.platform.LocalView + +@Composable +fun KeepScreenOn( + keepScreenOn: Boolean = true +) { + if (keepScreenOn) { + val currentView = LocalView.current + DisposableEffect(Unit) { + currentView.keepScreenOn = true + onDispose { + currentView.keepScreenOn = false + } + } + } +}