Skip to content

Commit

Permalink
fix: Use findActivity() instead of checking is Activity
Browse files Browse the repository at this point in the history
This works better with `ContextWrapper`s.
I moved the relevant static functions into extension functions.

We should think about whether we should just require `Activity` to be
set on `Context` everywhere, because right now we require it on some
of the code, but have defaults for other parts.
  • Loading branch information
Profpatsch committed Dec 22, 2024
1 parent f40aa50 commit 375ab60
Show file tree
Hide file tree
Showing 12 changed files with 46 additions and 93 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -145,9 +145,9 @@ fun NewPlayerUI(
}
}

val defaultBrightness = activity.getDefaultBrightness()
LaunchedEffect(key1 = uiState.brightness) {
Log.d(TAG, "New Brightness: ${uiState.brightness}")
val defaultBrightness = getDefaultBrightness(activity)

setScreenBrightness(
uiState.brightness ?: defaultBrightness, activity
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@

package net.newpipe.newplayer.ui.audioplayer

import android.app.Activity
import androidx.annotation.OptIn
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.Box
Expand All @@ -37,14 +36,12 @@ 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.res.stringResource
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import androidx.media3.common.util.UnstableApi
import net.newpipe.newplayer.R
import net.newpipe.newplayer.uiModel.EmbeddedUiConfig
import net.newpipe.newplayer.uiModel.NewPlayerUIState
import net.newpipe.newplayer.uiModel.InternalNewPlayerViewModel
import net.newpipe.newplayer.uiModel.NewPlayerViewModelDummy
Expand All @@ -57,17 +54,14 @@ import net.newpipe.newplayer.ui.common.getEmbeddedUiConfig
import net.newpipe.newplayer.ui.common.getLocale
import net.newpipe.newplayer.ui.common.getTimeStringFromMs

@OptIn(androidx.media3.common.util.UnstableApi::class)
@OptIn(UnstableApi::class)
@Composable

/** @hide */
internal fun AudioPlayerEmbeddedUI(viewModel: InternalNewPlayerViewModel, uiState: NewPlayerUIState) {
val locale = getLocale()!!

val embeddedUIConfig = if (LocalContext.current is Activity)
getEmbeddedUiConfig(activity = LocalContext.current as Activity)
else
EmbeddedUiConfig.DUMMY
val embeddedUIConfig = getEmbeddedUiConfig()

Box(modifier = Modifier.wrapContentSize()) {
Thumbnail(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@

package net.newpipe.newplayer.ui.audioplayer

import android.app.Activity
import androidx.annotation.OptIn
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Box
Expand All @@ -36,7 +35,6 @@ import androidx.compose.material.icons.filled.MoreVert
import androidx.compose.material.icons.filled.PictureInPicture
import androidx.compose.material.icons.filled.Share
import androidx.compose.material.icons.filled.Speed
import androidx.compose.material.icons.filled.Translate
import androidx.compose.material3.Button
import androidx.compose.material3.DropdownMenu
import androidx.compose.material3.DropdownMenuItem
Expand All @@ -56,7 +54,6 @@ import androidx.media3.common.util.UnstableApi
import net.newpipe.newplayer.R
import net.newpipe.newplayer.ui.common.LanguageMenu
import net.newpipe.newplayer.ui.common.LanguageMenuItem
import net.newpipe.newplayer.uiModel.EmbeddedUiConfig
import net.newpipe.newplayer.uiModel.NewPlayerUIState
import net.newpipe.newplayer.uiModel.InternalNewPlayerViewModel
import net.newpipe.newplayer.uiModel.NewPlayerViewModelDummy
Expand All @@ -72,10 +69,7 @@ import net.newpipe.newplayer.ui.common.showNotYetImplementedToast
/** @hide */
internal fun AudioBottomUI(viewModel: InternalNewPlayerViewModel, uiState: NewPlayerUIState) {

val embeddedUiConfig = if (LocalContext.current is Activity)
getEmbeddedUiConfig(activity = LocalContext.current as Activity)
else
EmbeddedUiConfig.DUMMY
val embeddedUiConfig = getEmbeddedUiConfig()

Row(modifier = Modifier.fillMaxWidth(), horizontalArrangement = Arrangement.SpaceBetween) {
Row(
Expand Down Expand Up @@ -142,10 +136,7 @@ private fun Menu(viewModel: InternalNewPlayerViewModel, uiState: NewPlayerUIStat
var showMenu: Boolean by remember { mutableStateOf(false) }
var showLanguageMenu: Boolean by remember { mutableStateOf(false) }

val embeddedUiConfig = if (LocalContext.current is Activity)
getEmbeddedUiConfig(activity = LocalContext.current as Activity)
else
EmbeddedUiConfig.DUMMY
val embeddedUiConfig = getEmbeddedUiConfig()

Box {
IconButton(onClick = {
Expand Down Expand Up @@ -229,4 +220,4 @@ private fun AudioBottomUIPreview() {
AudioBottomUI(viewModel = NewPlayerViewModelDummy(), uiState = NewPlayerUIState.DUMMY)
}
}
}
}
36 changes: 29 additions & 7 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 @@ -56,10 +56,27 @@ import net.newpipe.newplayer.R
import net.newpipe.newplayer.uiModel.EmbeddedUiConfig
import java.util.Locale

/** Get the [Activity] from local context. Assumes the activity exists!
* @return the activity
* @throws NullPointerException if there is no Activity
*/
@Composable
internal fun activity(): Activity
= LocalContext.current.findActivity()!!

/** Call block with the [Activity] from current context, if there is an activity.
*
* @param default: the default value if there is no activity
* @param block: the block to call with the activity
*/
@Composable
internal fun <T>activity(default: T, block: @Composable Activity.() -> T): T =
when (val a = LocalContext.current.findActivity()) {
null -> default
else -> block(a)
}


@Composable
internal fun window(): Window
= activity().window
Expand All @@ -84,9 +101,9 @@ internal fun LockScreenOrientation(orientation: Int) {

@SuppressLint("NewApi")

/** @return the default brightness of the screen, via window attributes */
/** @hide */
internal fun getDefaultBrightness(activity: Activity): Float {
val window = activity.window
internal fun Activity.getDefaultBrightness(): Float {
val layout = window.attributes as WindowManager.LayoutParams
return if (layout.screenBrightness < 0) 0.5f else layout.screenBrightness
}
Expand Down Expand Up @@ -114,19 +131,24 @@ internal fun getLocale(): Locale? {
}

@Composable
@ReadOnlyComposable
/** @return A collection of current activity/window configurations */
/** @hide */
internal fun getEmbeddedUiConfig()
= activity(EmbeddedUiConfig.DUMMY) { getEmbeddedUiConfig() }

@Composable
@ReadOnlyComposable
/** @return A collection of current activity/window configurations */
/** @hide */
internal fun getEmbeddedUiConfig(activity: Activity): EmbeddedUiConfig {
val window = activity.window
internal fun Activity.getEmbeddedUiConfig(): EmbeddedUiConfig {
val view = LocalView.current

val isLightStatusBar = WindowCompat.getInsetsController(
window,
view
).isAppearanceLightStatusBars
val screenOrientation = activity.requestedOrientation
val defaultBrightness = getDefaultBrightness(activity)
val screenOrientation = requestedOrientation
val defaultBrightness = getDefaultBrightness()
return EmbeddedUiConfig(
systemBarInLightMode = isLightStatusBar,
brightness = defaultBrightness,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@

package net.newpipe.newplayer.ui.selection_ui

import android.app.Activity
import androidx.annotation.OptIn
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Box
Expand All @@ -34,11 +33,9 @@ import androidx.compose.material3.Surface
import androidx.compose.runtime.Composable
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 androidx.media3.common.util.UnstableApi
import net.newpipe.newplayer.uiModel.EmbeddedUiConfig
import net.newpipe.newplayer.uiModel.NewPlayerUIState
import net.newpipe.newplayer.uiModel.InternalNewPlayerViewModel
import net.newpipe.newplayer.uiModel.NewPlayerViewModelDummy
Expand All @@ -57,10 +54,7 @@ internal fun ChapterSelectUI(
) {
val insets = getInsets()

val embeddedUiConfig = if (LocalContext.current is Activity)
getEmbeddedUiConfig(activity = LocalContext.current as Activity)
else
EmbeddedUiConfig.DUMMY
val embeddedUiConfig = getEmbeddedUiConfig()

Scaffold(
modifier = Modifier
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@

package net.newpipe.newplayer.ui.selection_ui

import android.app.Activity
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.automirrored.filled.PlaylistAdd
Expand All @@ -41,7 +40,6 @@ import androidx.compose.ui.text.style.TextOverflow
import androidx.compose.ui.tooling.preview.Preview
import androidx.media3.common.util.UnstableApi
import net.newpipe.newplayer.R
import net.newpipe.newplayer.uiModel.EmbeddedUiConfig
import net.newpipe.newplayer.uiModel.NewPlayerUIState
import net.newpipe.newplayer.uiModel.InternalNewPlayerViewModel
import net.newpipe.newplayer.uiModel.NewPlayerViewModelDummy
Expand All @@ -65,11 +63,7 @@ internal fun StreamSelectTopBar(
uiState: NewPlayerUIState
) {

val embeddedUiConfig =
if (LocalContext.current is Activity)
getEmbeddedUiConfig(activity = LocalContext.current as Activity)
else
EmbeddedUiConfig.DUMMY
val embeddedUiConfig = getEmbeddedUiConfig()

TopAppBar(modifier = modifier,
colors = topAppBarColors(containerColor = Color.Transparent),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,17 +20,14 @@

package net.newpipe.newplayer.ui.videoplayer

import android.app.Activity
import android.os.Build
import androidx.annotation.OptIn
import androidx.compose.animation.AnimatedVisibility
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.aspectRatio
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.material3.Surface
import androidx.compose.runtime.Composable
import androidx.compose.runtime.DisposableEffect
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.collectAsState
import androidx.compose.runtime.getValue
Expand All @@ -43,21 +40,16 @@ import androidx.compose.ui.graphics.Color
import androidx.compose.ui.graphics.toAndroidRectF
import androidx.compose.ui.layout.boundsInWindow
import androidx.compose.ui.layout.onGloballyPositioned
import androidx.compose.ui.platform.LocalContext
import androidx.core.graphics.toRect
import androidx.lifecycle.Lifecycle
import androidx.media3.common.util.UnstableApi
import net.newpipe.newplayer.uiModel.NewPlayerUIState
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
import net.newpipe.newplayer.ui.videoplayer.pip.supportsPip
import net.newpipe.newplayer.ui.common.getEmbeddedUiConfig
import net.newpipe.newplayer.uiModel.UIModeState

@OptIn(UnstableApi::class)
Expand All @@ -67,8 +59,6 @@ import net.newpipe.newplayer.uiModel.UIModeState
internal fun VideoPlayerUi(viewModel: InternalNewPlayerViewModel, uiState: NewPlayerUIState) {
val activity = activity()

getEmbeddedUiConfig(activity = LocalContext.current as Activity)

val exoPlayer by viewModel.newPlayer?.exoPlayer!!.collectAsState()


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -130,10 +130,7 @@ internal fun BottomUI(
) {
Text(getTimeStringFromMs(uiState.durationInMs, getLocale() ?: locale))

val embeddedUiConfig = when (LocalContext.current) {
is Activity -> getEmbeddedUiConfig(LocalContext.current as Activity)
else -> EmbeddedUiConfig.DUMMY
}
val embeddedUiConfig = getEmbeddedUiConfig()

IconButton(
onClick = if (uiState.uiMode.fullscreen) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,8 @@

package net.newpipe.newplayer.ui.videoplayer.controller

import android.app.Activity
import androidx.annotation.OptIn
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.fillMaxHeight
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.Crop
Expand All @@ -34,7 +32,6 @@ import androidx.compose.material.icons.filled.MoreVert
import androidx.compose.material.icons.filled.PictureInPicture
import androidx.compose.material.icons.filled.Share
import androidx.compose.material.icons.filled.Subtitles
import androidx.compose.material.icons.filled.Translate
import androidx.compose.material.icons.filled.ZoomOutMap
import androidx.compose.material3.DropdownMenu
import androidx.compose.material3.DropdownMenuItem
Expand All @@ -58,11 +55,9 @@ import androidx.compose.ui.unit.DpOffset
import androidx.compose.ui.unit.dp
import androidx.media3.common.util.UnstableApi
import net.newpipe.newplayer.R
import net.newpipe.newplayer.logic.TrackUtils
import net.newpipe.newplayer.ui.ContentScale
import net.newpipe.newplayer.ui.common.LanguageMenu
import net.newpipe.newplayer.ui.common.LanguageMenuItem
import net.newpipe.newplayer.uiModel.EmbeddedUiConfig
import net.newpipe.newplayer.uiModel.NewPlayerUIState
import net.newpipe.newplayer.uiModel.InternalNewPlayerViewModel
import net.newpipe.newplayer.uiModel.NewPlayerViewModelDummy
Expand All @@ -71,7 +66,6 @@ import net.newpipe.newplayer.ui.theme.VideoPlayerTheme
import net.newpipe.newplayer.ui.videoplayer.pip.supportsPip
import net.newpipe.newplayer.ui.common.getEmbeddedUiConfig
import net.newpipe.newplayer.ui.common.showNotYetImplementedToast
import java.util.Locale

@OptIn(UnstableApi::class)
@Composable
Expand All @@ -91,11 +85,7 @@ internal fun VideoPlayerMenu(
mutableStateOf(0.dp)
}

val embeddedUiConfig = if (LocalContext.current is Activity)
getEmbeddedUiConfig(activity = LocalContext.current as Activity)
else
EmbeddedUiConfig.DUMMY

val embeddedUiConfig = getEmbeddedUiConfig()


Box {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@

package net.newpipe.newplayer.ui.videoplayer.controller

import android.app.Activity
import android.widget.Toast
import androidx.annotation.OptIn
import androidx.compose.animation.AnimatedVisibility
Expand Down Expand Up @@ -62,8 +61,6 @@ import androidx.compose.ui.unit.sp
import androidx.media3.common.util.UnstableApi
import net.newpipe.newplayer.R
import net.newpipe.newplayer.data.VideoStreamTrack
import net.newpipe.newplayer.logic.TrackUtils
import net.newpipe.newplayer.uiModel.EmbeddedUiConfig
import net.newpipe.newplayer.uiModel.NewPlayerUIState
import net.newpipe.newplayer.uiModel.InternalNewPlayerViewModel
import net.newpipe.newplayer.uiModel.NewPlayerViewModelDummy
Expand All @@ -80,12 +77,7 @@ import net.newpipe.newplayer.ui.common.showNotYetImplementedToast
internal fun TopUI(
modifier: Modifier, viewModel: InternalNewPlayerViewModel, uiState: NewPlayerUIState
) {
val embeddedUiConfig =
if (LocalContext.current is Activity)
getEmbeddedUiConfig(activity = LocalContext.current as Activity)
else
EmbeddedUiConfig.DUMMY

val embeddedUiConfig = getEmbeddedUiConfig()
Row(
// the default height for an app bar is 64.dp according to this source:
// https://cs.android.com/androidx/platform/frameworks/support/+/7b27816c561b8f271d79d24ab21ba7d08aaad031:compose/material3/material3/src/commonMain/kotlin/androidx/compose/material3/tokens/TopAppBarSmallTokens.kt;l=26
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,10 +66,7 @@ internal fun EmbeddedGestureUI(
mutableFloatStateOf(0f)
}

val embeddedUiConfig = if (LocalContext.current is Activity)
getEmbeddedUiConfig(LocalContext.current as Activity)
else
EmbeddedUiConfig.DUMMY
val embeddedUiConfig = getEmbeddedUiConfig()

val handleMovement = { movement: TouchedPosition ->
//Log.d(TAG, "${movement.x}:${movement.y}")
Expand Down
Loading

0 comments on commit 375ab60

Please sign in to comment.