From f40aa50e7a18c6d594d2e2136b893354828d43a8 Mon Sep 17 00:00:00 2001 From: Profpatsch Date: Fri, 20 Dec 2024 19:09:37 +0100 Subject: [PATCH] =?UTF-8?q?fix:=20don=E2=80=99t=20cast=20LocalContext=20to?= =?UTF-8?q?=20Activity?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This only works if the surrounding Context is from an Activity, but when embedding in NewPipe proper, we are in a Fragment. So the cast will fail. Instead, for now we inject the Activity in the model. But what we should actually do is forward the data we need from outside. --- .../net/newpipe/newplayer/ui/NewPlayerUI.kt | 11 +++++----- .../net/newpipe/newplayer/ui/common/utils.kt | 22 ++++++++++++++----- .../newplayer/ui/videoplayer/PlaySurface.kt | 4 ++-- .../newplayer/ui/videoplayer/VideoPlayerUI.kt | 9 ++++---- .../newplayer/uiModel/NewPlayerViewModel.kt | 1 + .../uiModel/NewPlayerViewModelDummy.kt | 1 + 6 files changed, 30 insertions(+), 18 deletions(-) diff --git a/new-player/src/main/java/net/newpipe/newplayer/ui/NewPlayerUI.kt b/new-player/src/main/java/net/newpipe/newplayer/ui/NewPlayerUI.kt index 2a101fa8..810cffca 100644 --- a/new-player/src/main/java/net/newpipe/newplayer/ui/NewPlayerUI.kt +++ b/new-player/src/main/java/net/newpipe/newplayer/ui/NewPlayerUI.kt @@ -20,7 +20,6 @@ package net.newpipe.newplayer.ui -import android.app.Activity import android.content.pm.ActivityInfo import android.os.Build import android.util.Log @@ -31,7 +30,6 @@ import androidx.compose.runtime.Composable import androidx.compose.runtime.LaunchedEffect import androidx.compose.runtime.collectAsState import androidx.compose.runtime.getValue -import androidx.compose.ui.platform.LocalContext import androidx.compose.ui.platform.LocalView import androidx.compose.ui.tooling.preview.Preview import androidx.core.view.WindowCompat @@ -48,9 +46,12 @@ import net.newpipe.newplayer.ui.audioplayer.AudioPlayerUI import net.newpipe.newplayer.ui.theme.VideoPlayerTheme import net.newpipe.newplayer.ui.videoplayer.VideoPlayerUi import net.newpipe.newplayer.ui.common.LockScreenOrientation +import net.newpipe.newplayer.ui.common.activity +import net.newpipe.newplayer.ui.common.findActivity import net.newpipe.newplayer.ui.common.getDefaultBrightness import net.newpipe.newplayer.ui.common.isInPowerSaveMode import net.newpipe.newplayer.ui.common.setScreenBrightness +import net.newpipe.newplayer.ui.common.window private const val TAG = "VideoPlayerUI" @@ -84,9 +85,9 @@ fun NewPlayerUI( } else { val uiState by viewModel.uiState.collectAsState() - val activity = LocalContext.current as Activity + // find out whether application is light or dark mode from LocalContext.current val view = LocalView.current - + val activity = activity() val window = activity.window // Setup fullscreen @@ -145,7 +146,7 @@ fun NewPlayerUI( } LaunchedEffect(key1 = uiState.brightness) { - Log.d(TAG, "New Brightnes: ${uiState.brightness}") + Log.d(TAG, "New Brightness: ${uiState.brightness}") val defaultBrightness = getDefaultBrightness(activity) setScreenBrightness( diff --git a/new-player/src/main/java/net/newpipe/newplayer/ui/common/utils.kt b/new-player/src/main/java/net/newpipe/newplayer/ui/common/utils.kt index 0374fa8f..bcd967de 100644 --- a/new-player/src/main/java/net/newpipe/newplayer/ui/common/utils.kt +++ b/new-player/src/main/java/net/newpipe/newplayer/ui/common/utils.kt @@ -27,6 +27,7 @@ import android.content.ContextWrapper import android.net.Uri import android.os.Build import android.os.PowerManager +import android.view.Window import android.view.WindowManager import androidx.annotation.OptIn import androidx.annotation.RequiresApi @@ -55,6 +56,21 @@ import net.newpipe.newplayer.R import net.newpipe.newplayer.uiModel.EmbeddedUiConfig import java.util.Locale +@Composable +internal fun activity(): Activity + = LocalContext.current.findActivity()!! + +@Composable +internal fun window(): Window + = activity().window + +/** @hide */ +internal fun Context.findActivity(): Activity? = when (this) { + is Activity -> this + is ContextWrapper -> baseContext.findActivity() + else -> null +} + @Composable /** @hide */ @@ -86,12 +102,6 @@ internal fun setScreenBrightness(value: Float, activity: Activity) { } -/** @hide */ -internal fun Context.findActivity(): Activity? = when (this) { - is Activity -> this - is ContextWrapper -> baseContext.findActivity() - else -> null -} @Composable diff --git a/new-player/src/main/java/net/newpipe/newplayer/ui/videoplayer/PlaySurface.kt b/new-player/src/main/java/net/newpipe/newplayer/ui/videoplayer/PlaySurface.kt index ec9e9b77..9b53e0ee 100644 --- a/new-player/src/main/java/net/newpipe/newplayer/ui/videoplayer/PlaySurface.kt +++ b/new-player/src/main/java/net/newpipe/newplayer/ui/videoplayer/PlaySurface.kt @@ -60,9 +60,9 @@ internal fun PlaySurface( // Preparation - val activity = LocalContext.current as Activity + val ctx = LocalContext.current - val displayMetrics = activity.resources.displayMetrics + val displayMetrics = ctx.resources.displayMetrics val screenRatio = displayMetrics.widthPixels.toFloat() / displayMetrics.heightPixels.toFloat() diff --git a/new-player/src/main/java/net/newpipe/newplayer/ui/videoplayer/VideoPlayerUI.kt b/new-player/src/main/java/net/newpipe/newplayer/ui/videoplayer/VideoPlayerUI.kt index 0270d8cf..1140cbc4 100644 --- a/new-player/src/main/java/net/newpipe/newplayer/ui/videoplayer/VideoPlayerUI.kt +++ b/new-player/src/main/java/net/newpipe/newplayer/ui/videoplayer/VideoPlayerUI.kt @@ -52,6 +52,7 @@ import net.newpipe.newplayer.uiModel.InternalNewPlayerViewModel import net.newpipe.newplayer.ui.selection_ui.StreamSelectUI import androidx.lifecycle.LifecycleEventObserver import net.newpipe.newplayer.data.NewPlayerException +import net.newpipe.newplayer.ui.common.activity import net.newpipe.newplayer.uiModel.EmbeddedUiConfig import net.newpipe.newplayer.ui.selection_ui.ChapterSelectUI import net.newpipe.newplayer.ui.videoplayer.pip.getPipParams @@ -64,14 +65,12 @@ import net.newpipe.newplayer.uiModel.UIModeState /** @hide */ internal fun VideoPlayerUi(viewModel: InternalNewPlayerViewModel, uiState: NewPlayerUIState) { - val embeddedUiConfig = if (LocalContext.current is Activity) - getEmbeddedUiConfig(activity = LocalContext.current as Activity) - else - EmbeddedUiConfig.DUMMY + val activity = activity() + + getEmbeddedUiConfig(activity = LocalContext.current as Activity) val exoPlayer by viewModel.newPlayer?.exoPlayer!!.collectAsState() - val activity = LocalContext.current as Activity var videoViewBounds by remember { mutableStateOf(android.graphics.Rect()) diff --git a/new-player/src/main/java/net/newpipe/newplayer/uiModel/NewPlayerViewModel.kt b/new-player/src/main/java/net/newpipe/newplayer/uiModel/NewPlayerViewModel.kt index 5b888ec0..9e9d6c5b 100644 --- a/new-player/src/main/java/net/newpipe/newplayer/uiModel/NewPlayerViewModel.kt +++ b/new-player/src/main/java/net/newpipe/newplayer/uiModel/NewPlayerViewModel.kt @@ -1,5 +1,6 @@ package net.newpipe.newplayer.uiModel +import android.app.Activity import androidx.annotation.OptIn import androidx.media3.common.util.UnstableApi import kotlinx.coroutines.flow.SharedFlow diff --git a/new-player/src/main/java/net/newpipe/newplayer/uiModel/NewPlayerViewModelDummy.kt b/new-player/src/main/java/net/newpipe/newplayer/uiModel/NewPlayerViewModelDummy.kt index 3eeec933..cd2fac5e 100644 --- a/new-player/src/main/java/net/newpipe/newplayer/uiModel/NewPlayerViewModelDummy.kt +++ b/new-player/src/main/java/net/newpipe/newplayer/uiModel/NewPlayerViewModelDummy.kt @@ -1,5 +1,6 @@ package net.newpipe.newplayer.uiModel +import android.app.Activity import android.os.Bundle import androidx.media3.common.util.UnstableApi import kotlinx.coroutines.flow.MutableSharedFlow