-
Notifications
You must be signed in to change notification settings - Fork 8
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
base: dev
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -58,15 +58,14 @@ internal suspend fun buildMediaSource( | |
): MediaSource { | ||
when (streamSelection) { | ||
is SingleSelection -> { | ||
val mediaItem = toMediaItem(streamSelection.item, streamSelection.stream, uniqueId) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same here. |
||
it, | ||
uniqueId | ||
) | ||
|
@@ -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()) | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -23,7 +23,7 @@ package net.newpipe.newplayer.logic; | |
import net.newpipe.newplayer.data.StreamSelection | ||
import net.newpipe.newplayer.NewPlayer | ||
|
||
interface StreamExceptionResponse | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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,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 | ||
*/ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||
*/ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 */ | ||
|
@@ -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 | ||
} | ||
|
@@ -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 | ||
|
@@ -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, | ||
|
There was a problem hiding this comment.
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.