Skip to content

Commit

Permalink
fix: don’t cast LocalContext to Activity
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
Profpatsch committed Dec 22, 2024
1 parent 2e2fd9b commit f40aa50
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 18 deletions.
11 changes: 6 additions & 5 deletions new-player/src/main/java/net/newpipe/newplayer/ui/NewPlayerUI.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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"

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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(
Expand Down
22 changes: 16 additions & 6 deletions new-player/src/main/java/net/newpipe/newplayer/ui/common/utils.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 */
Expand Down Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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())
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand Down

0 comments on commit f40aa50

Please sign in to comment.