Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

use findActivity everywhere #15

Open
wants to merge 4 commits into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -298,10 +298,6 @@ class NewPlayerImpl(
mutableErrorFlow.emit(e)
}
}

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Useful cleanup, but if I'm nitpicky this code change should not be part of this PR.
I don't care too much about this though :P. However, I don't know how other devs feel about this. I'd say do it as you feel best with.

else -> {
throw NewPlayerException("Unknwon exception response ${response.javaClass}")
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,15 +58,14 @@ internal suspend fun buildMediaSource(
): MediaSource {
when (streamSelection) {
is SingleSelection -> {
val mediaItem = toMediaItem(streamSelection.item, streamSelection.stream, uniqueId)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same here.

val mediaItem = toMediaItem(streamSelection.stream, uniqueId)
val mediaItemWithMetadata = addMetadata(mediaItem, streamSelection.item)
return toMediaSource(mediaItemWithMetadata, streamSelection.stream)
}

is MultiSelection -> {
val mediaItems = ArrayList(streamSelection.streams.map {
toMediaItem(
streamSelection.item,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same here.

it,
uniqueId
)
Expand All @@ -88,7 +87,7 @@ internal suspend fun buildMediaSource(

@OptIn(UnstableApi::class)
private
fun toMediaItem(item: String, stream: Stream, uniqueId: Long): MediaItem {
fun toMediaItem(stream: Stream, uniqueId: Long): MediaItem {

val mediaItemBuilder = MediaItem.Builder()
.setMediaId(uniqueId.toString())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ package net.newpipe.newplayer.logic;
import net.newpipe.newplayer.data.StreamSelection
import net.newpipe.newplayer.NewPlayer

interface StreamExceptionResponse
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same here.

sealed interface StreamExceptionResponse

/***
* Perform a specific action, like halting the playback or etc.
Expand Down
13 changes: 7 additions & 6 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 @@ -144,9 +145,9 @@ fun NewPlayerUI(
}
}

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

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)
}
}
}
}
58 changes: 45 additions & 13 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,38 @@ 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
*/
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Consider adding @hide here, so this function does not show up on the KDoc documentation.

@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
*/
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Consider adding @hide here, so this function does not show up on the KDoc documentation.

@Composable
internal fun <T>activity(default: T, block: @Composable Activity.() -> T): T =
when (val a = LocalContext.current.findActivity()) {
null -> default
else -> block(a)
}


@Composable
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Consider adding @hide here, so this function does not show up on the KDoc documentation.

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 All @@ -68,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 All @@ -86,12 +119,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 All @@ -104,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 @@ -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
Loading
Loading