Skip to content

Commit

Permalink
Fix WebViewLogin crash on A9 and below
Browse files Browse the repository at this point in the history
Fix WebViewLogin crash on Android 9 and below caused by setting forceDark on the WebView settings
Fix bottom padding on main page and overlay pages
  • Loading branch information
toasterofbread committed Jul 15, 2023
1 parent 3134aeb commit 999a277
Show file tree
Hide file tree
Showing 13 changed files with 63 additions and 51 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ import android.os.Build
import android.os.Looper
import android.os.VibrationEffect
import android.os.Vibrator
import android.os.VibratorManager
import android.view.Window
import android.view.WindowInsetsController.APPEARANCE_LIGHT_STATUS_BARS
import android.widget.Toast
Expand All @@ -35,7 +34,6 @@ import androidx.compose.ui.text.font.Font
import androidx.compose.ui.unit.Dp
import androidx.compose.ui.unit.dp
import androidx.core.app.NotificationManagerCompat
import androidx.core.content.ContextCompat.getSystemService
import com.toasterofbread.spmp.model.Settings
import com.toasterofbread.spmp.resources.getString
import com.toasterofbread.spmp.resources.getStringTODO
Expand Down Expand Up @@ -104,12 +102,11 @@ actual class PlatformContext(private val context: Context, onInit: ((PlatformCon
@Composable
actual fun getSystemInsets(): WindowInsets? = WindowInsets.systemGestures

@Composable
@SuppressLint("InternalInsetResource", "DiscouragedApi")
actual fun getNavigationBarHeight(): Dp = with(LocalDensity.current) {
actual fun getNavigationBarHeight(): Int {
val resources = SpMp.context.ctx.resources
val resource_id = resources.getIdentifier("navigation_bar_height", "dimen", "android")
if (resource_id > 0) resources.getDimensionPixelSize(resource_id).toDp() else 0.dp
return if (resource_id > 0) resources.getDimensionPixelSize(resource_id) else 0
}

actual fun getLightColorScheme(): ColorScheme =
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.toasterofbread.spmp.platform

import android.annotation.SuppressLint
import android.graphics.Bitmap
import android.os.Build
import android.view.ViewGroup
Expand Down Expand Up @@ -30,6 +31,7 @@ class WebResourceRequestReader(private val request: WebResourceRequest): WebView
get() = request.requestHeaders
}

@SuppressLint("SetJavaScriptEnabled")
@Composable
actual fun WebViewLogin(
initial_url: String,
Expand All @@ -52,7 +54,7 @@ actual fun WebViewLogin(
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
settings.isAlgorithmicDarkeningAllowed = is_dark
}
else {
else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
@Suppress("DEPRECATION")
settings.forceDark = if (is_dark) WebSettings.FORCE_DARK_ON else WebSettings.FORCE_DARK_OFF
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import androidx.compose.foundation.layout.WindowInsets
import androidx.compose.material3.ColorScheme
import androidx.compose.runtime.Composable
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.platform.LocalDensity
import androidx.compose.ui.text.font.Font
import androidx.compose.ui.unit.Dp
import androidx.compose.ui.unit.dp
Expand Down Expand Up @@ -44,6 +45,11 @@ fun PlatformContext.getDefaultVerticalPadding(): Dp = if (isScreenLarge()) 30.dp
@Composable
fun PlatformContext.getDefaultPaddingValues(): PaddingValues = PaddingValues(horizontal = getDefaultHorizontalPadding(), vertical = getDefaultVerticalPadding())

@Composable
fun PlatformContext.getNavigationBarHeightDp(): Dp = with(LocalDensity.current) {
getNavigationBarHeight().toDp()
}

expect class PlatformContext {
val player_state: PlayerStateImpl

Expand All @@ -62,8 +68,7 @@ expect class PlatformContext {
fun getImeInsets(): WindowInsets?
@Composable
fun getSystemInsets(): WindowInsets?
@Composable
fun getNavigationBarHeight(): Dp
fun getNavigationBarHeight(): Int

fun getLightColorScheme(): ColorScheme
fun getDarkColorScheme(): ColorScheme
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ fun MultiselectAndMusicTopBar(
modifier: Modifier = Modifier,
padding: PaddingValues = PaddingValues(),
show_wave_border: Boolean = true
): State<PaddingValues> {
): PaddingValues {
val top_padding = padding.calculateTopPadding()
var top_bar_showing by remember { mutableStateOf(false) }

Expand Down Expand Up @@ -58,12 +58,8 @@ fun MultiselectAndMusicTopBar(
val layout_direction = LocalLayoutDirection.current
val out_top_padding by animateDpAsState(if (!top_bar_showing && !multiselect_context.is_active) top_padding else 0.dp)

return remember {
derivedStateOf {
padding.copy(
layout_direction,
top = out_top_padding
)
}
}
return padding.copy(
layout_direction,
top = out_top_padding
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ import com.toasterofbread.spmp.model.mediaitem.Artist
import com.toasterofbread.spmp.model.mediaitem.MediaItemPreviewParams
import com.toasterofbread.spmp.model.mediaitem.MediaItemThumbnailProvider
import com.toasterofbread.spmp.platform.composable.platformClickable
import com.toasterofbread.spmp.platform.getNavigationBarHeightDp
import com.toasterofbread.spmp.platform.vibrateShort
import com.toasterofbread.spmp.ui.component.MediaItemTitleEditDialog
import com.toasterofbread.spmp.ui.layout.nowplaying.overlay.DEFAULT_THUMBNAIL_ROUNDING
Expand Down Expand Up @@ -85,7 +86,7 @@ internal fun LongPressMenuContent(

Box(
Modifier
.requiredHeight(SpMp.context.getScreenHeight() + SpMp.context.getNavigationBarHeight())
.requiredHeight(SpMp.context.getScreenHeight() + SpMp.context.getNavigationBarHeightDp())
.offset(y = -SpMp.context.getStatusBarHeight())
.background(Color.Black.setAlpha(0.5f))
) {
Expand All @@ -110,7 +111,7 @@ internal fun LongPressMenuContent(
Column(
modifier
.background(Theme.current.background, shape)
.padding(bottom = SpMp.context.getNavigationBarHeight())
.padding(bottom = SpMp.context.getNavigationBarHeightDp())
.fillMaxWidth()
.onSizeChanged {
if (show_info || !main_actions_showing) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ fun SearchPage(
Box(Modifier.fillMaxSize()) {
Column(Modifier.fillMaxSize()) {
val horizontal_padding = SpMp.context.getDefaultHorizontalPadding()
val padding by MultiselectAndMusicTopBar(
val padding = MultiselectAndMusicTopBar(
multiselect_context,
Modifier.fillMaxWidth().zIndex(1f),
padding = PaddingValues(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ fun MainPage(
Modifier.graphicsLayer { alpha = state_alpha.value },
state = scroll_state,
contentPadding = PaddingValues(
bottom = LocalPlayerState.current.bottom_padding,
bottom = LocalPlayerState.current.bottom_padding_dp,
top = top_padding
),
userScrollEnabled = !state_alpha.isRunning,
Expand Down Expand Up @@ -211,7 +211,7 @@ fun MainPage(

LibraryPage(
pill_menu,
LocalPlayerState.current.bottom_padding,
LocalPlayerState.current.bottom_padding_dp,
close = {},
inline = true,
outer_multiselect_context = LocalPlayerState.current.main_multiselect_context,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.setValue
import androidx.compose.ui.Modifier
import androidx.compose.ui.platform.LocalDensity
import androidx.compose.ui.unit.Dp
import com.toasterofbread.spmp.PlayerService
import com.toasterofbread.spmp.model.mediaitem.Artist
Expand Down Expand Up @@ -96,7 +97,7 @@ open class PlayerState protected constructor(
private val upstream: PlayerState? = null
) {
open val overlay_page: Pair<PlayerOverlayPage, MediaItem?>? get() = upstream!!.overlay_page
open val bottom_padding: Dp get() = upstream!!.bottom_padding
open val bottom_padding: Float get() = upstream!!.bottom_padding
open val pill_menu: PillMenu get() = upstream!!.pill_menu
open val main_multiselect_context: MediaItemMultiSelectContext get() = upstream!!.main_multiselect_context
open val np_theme_mode: ThemeMode get() = upstream!!.np_theme_mode
Expand All @@ -114,6 +115,11 @@ open class PlayerState protected constructor(
open fun interactService(action: (player: PlayerService) -> Unit) { upstream!!.interactService(action) }
open fun isRunningAndFocused(): Boolean = upstream!!.isRunningAndFocused()

val bottom_padding_dp: Dp
@Composable get() = with(LocalDensity.current) {
bottom_padding.toDp()
}

fun copy(
onClickedOverride: ((item: MediaItem, multiselect_key: Int?) -> Unit)? = null,
onLongClickedOverride: ((item: MediaItem, long_press_data: LongPressMenuData?) -> Unit)? = null
Expand Down Expand Up @@ -195,8 +201,4 @@ open class PlayerState protected constructor(
fun showLongPressMenu(item: MediaItem) { showLongPressMenu(LongPressMenuData(item)) }

open fun hideLongPressMenu() { upstream!!.hideLongPressMenu() }

// PlayerServiceHost


}
Original file line number Diff line number Diff line change
Expand Up @@ -183,9 +183,15 @@ class PlayerStateImpl(private val context: PlatformContext): PlayerState(null, n

private var now_playing_switch_page: Int by mutableStateOf(-1)
private val overlay_page_undo_stack: MutableList<Pair<PlayerOverlayPage, MediaItem?>?> = mutableListOf()
private val bottom_padding_anim = Animatable(session_started.toFloat() * MINIMISED_NOW_PLAYING_HEIGHT)
private var main_page_showing: Boolean by mutableStateOf(false)

@Composable
private fun getCurrentBottomPadding(): Float =
with(LocalDensity.current) {
(session_started.toFloat() * MINIMISED_NOW_PLAYING_HEIGHT_DP.dp.toPx()) + SpMp.context.getNavigationBarHeight()
}
private val bottom_padding_anim = Animatable(SpMp.context.getNavigationBarHeight().toFloat())

private val low_memory_listener: () -> Unit
private val prefs_listener: ProjectPreferences.Listener

Expand All @@ -207,7 +213,7 @@ class PlayerStateImpl(private val context: PlatformContext): PlayerState(null, n

override var overlay_page: Pair<PlayerOverlayPage, MediaItem?>? by mutableStateOf(null)
private set
override val bottom_padding: Dp get() = bottom_padding_anim.value.dp
override val bottom_padding: Float get() = bottom_padding_anim.value
override val pill_menu = PillMenu(
top = false,
left = false
Expand Down Expand Up @@ -297,7 +303,7 @@ class PlayerStateImpl(private val context: PlatformContext): PlayerState(null, n
override fun nowPlayingTopOffset(base: Modifier): Modifier {
val density = LocalDensity.current
val screen_height = context.getScreenHeight()
val bottom_padding = context.getNavigationBarHeight()
val bottom_padding = context.getNavigationBarHeightDp()
val keyboard_insets = SpMp.context.getImeInsets()

return base.offset {
Expand All @@ -314,10 +320,10 @@ class PlayerStateImpl(private val context: PlatformContext): PlayerState(null, n
@Composable
override fun nowPlayingBottomPadding(include_np: Boolean): Dp {
if (include_np) {
val np by animateDpAsState(if (session_started) MINIMISED_NOW_PLAYING_HEIGHT.dp else 0.dp)
return context.getNavigationBarHeight() + np
val np by animateDpAsState(if (session_started) MINIMISED_NOW_PLAYING_HEIGHT_DP.dp else 0.dp)
return context.getNavigationBarHeightDp() + np
}
return context.getNavigationBarHeight()
return context.getNavigationBarHeightDp()
}

override fun setOverlayPage(page: PlayerOverlayPage?, from_current: Boolean) {
Expand Down Expand Up @@ -434,8 +440,9 @@ class PlayerStateImpl(private val context: PlatformContext): PlayerState(null, n

@Composable
fun NowPlaying() {
OnChangedEffect(session_started) {
bottom_padding_anim.animateTo(session_started.toFloat() * MINIMISED_NOW_PLAYING_HEIGHT)
val bottom_padding = getCurrentBottomPadding()
OnChangedEffect(bottom_padding) {
bottom_padding_anim.animateTo(bottom_padding)
}

val screen_height = context.getScreenHeight()
Expand All @@ -445,7 +452,7 @@ class PlayerStateImpl(private val context: PlatformContext): PlayerState(null, n

np_swipe_anchors = (0..NOW_PLAYING_VERTICAL_PAGE_COUNT)
.associateBy { anchor ->
if (anchor == 0) MINIMISED_NOW_PLAYING_HEIGHT.toFloat() - half_screen_height
if (anchor == 0) MINIMISED_NOW_PLAYING_HEIGHT_DP.toFloat() - half_screen_height
else ((screen_height).value * anchor) - half_screen_height
}

Expand Down Expand Up @@ -628,7 +635,7 @@ class PlayerStateImpl(private val context: PlatformContext): PlayerState(null, n
page.first.getPage(
pill_menu,
page.second,
(if (session_started) MINIMISED_NOW_PLAYING_HEIGHT.dp else 0.dp) + 10.dp,
(if (session_started) MINIMISED_NOW_PLAYING_HEIGHT_DP.dp else 0.dp) + SpMp.context.getNavigationBarHeightDp(),
close
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ import androidx.compose.ui.Modifier
import com.toasterofbread.spmp.ui.theme.Theme
import com.toasterofbread.utils.modifier.background

const val MINIMISED_NOW_PLAYING_HEIGHT: Int = 64
const val MINIMISED_NOW_PLAYING_V_PADDING: Int = 7
const val MINIMISED_NOW_PLAYING_HEIGHT_DP: Int = 64
const val MINIMISED_NOW_PLAYING_V_PADDING_DP: Int = 7
const val MEDIAITEM_PREVIEW_SQUARE_SIZE_SMALL: Float = 100f
const val MEDIAITEM_PREVIEW_SQUARE_SIZE_LARGE: Float = 200f

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.times
import com.toasterofbread.spmp.model.mediaitem.Song
import com.toasterofbread.spmp.platform.composeScope
import com.toasterofbread.spmp.ui.layout.mainpage.MINIMISED_NOW_PLAYING_HEIGHT
import com.toasterofbread.spmp.ui.layout.mainpage.MINIMISED_NOW_PLAYING_V_PADDING
import com.toasterofbread.spmp.ui.layout.mainpage.MINIMISED_NOW_PLAYING_HEIGHT_DP
import com.toasterofbread.spmp.ui.layout.mainpage.MINIMISED_NOW_PLAYING_V_PADDING_DP
import com.toasterofbread.spmp.ui.layout.nowplaying.LocalNowPlayingExpansion
import com.toasterofbread.spmp.ui.layout.nowplaying.NOW_PLAYING_VERTICAL_PAGE_COUNT
import com.toasterofbread.spmp.ui.layout.nowplaying.ThumbnailRow
Expand Down Expand Up @@ -112,14 +112,14 @@ fun ColumnScope.NowPlayingMainTab() {
ThumbnailRow(
Modifier
.padding(
top = MINIMISED_NOW_PLAYING_V_PADDING.dp
top = MINIMISED_NOW_PLAYING_V_PADDING_DP.dp
* (1f - expansion.getBounded())
.coerceAtLeast(0f)
)
.height(
(expansion.getAbsolute() * (screen_width - (NOW_PLAYING_MAIN_PADDING.dp * 2)))
.coerceAtLeast(
MINIMISED_NOW_PLAYING_HEIGHT.dp - (MINIMISED_NOW_PLAYING_V_PADDING.dp * 2)
MINIMISED_NOW_PLAYING_HEIGHT_DP.dp - (MINIMISED_NOW_PLAYING_V_PADDING_DP.dp * 2)
)
)
.width(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,8 @@ import com.toasterofbread.spmp.model.mediaitem.Song
import com.toasterofbread.spmp.platform.MediaPlayerService
import com.toasterofbread.spmp.ui.component.WaveBorder
import com.toasterofbread.spmp.ui.component.multiselect.MediaItemMultiSelectContext
import com.toasterofbread.spmp.ui.layout.mainpage.MINIMISED_NOW_PLAYING_HEIGHT
import com.toasterofbread.spmp.ui.layout.mainpage.MINIMISED_NOW_PLAYING_V_PADDING
import com.toasterofbread.spmp.ui.layout.nowplaying.LocalNowPlayingExpansion
import com.toasterofbread.spmp.ui.layout.mainpage.MINIMISED_NOW_PLAYING_HEIGHT_DP
import com.toasterofbread.spmp.ui.layout.mainpage.MINIMISED_NOW_PLAYING_V_PADDING_DP
import com.toasterofbread.spmp.ui.layout.nowplaying.LocalNowPlayingExpansion
import com.toasterofbread.spmp.ui.layout.nowplaying.getNPAltOnBackground
import com.toasterofbread.spmp.ui.layout.nowplaying.getNPBackground
Expand Down Expand Up @@ -164,7 +163,7 @@ fun QueueTab(page_height: Dp, modifier: Modifier = Modifier) {
// Add extra height for overscroll
.requiredHeight(page_height + 200.dp)
.requiredWidth(SpMp.context.getScreenWidth())
.padding(top = MINIMISED_NOW_PLAYING_HEIGHT.dp + (SpMp.context.getStatusBarHeight() * 0.5f) + top_bar_height + MINIMISED_NOW_PLAYING_V_PADDING.dp)
.padding(top = MINIMISED_NOW_PLAYING_HEIGHT_DP.dp + (SpMp.context.getStatusBarHeight() * 0.5f) + top_bar_height + MINIMISED_NOW_PLAYING_V_PADDING_DP.dp)
.background(queue_background_colour, shape)
.clip(shape)
) {
Expand Down Expand Up @@ -229,7 +228,7 @@ fun QueueTab(page_height: Dp, modifier: Modifier = Modifier) {

item {
Spacer(Modifier.height(
NOW_PLAYING_TOP_BAR_HEIGHT.dp + MINIMISED_NOW_PLAYING_HEIGHT.dp + list_position + list_padding
NOW_PLAYING_TOP_BAR_HEIGHT.dp + MINIMISED_NOW_PLAYING_HEIGHT_DP.dp + list_position + list_padding
))
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import com.toasterofbread.spmp.api.getOrReport
import com.toasterofbread.spmp.model.*
import com.toasterofbread.spmp.model.mediaitem.*
import com.toasterofbread.spmp.platform.getDefaultHorizontalPadding
import com.toasterofbread.spmp.platform.getDefaultVerticalPadding
import com.toasterofbread.spmp.resources.getString
import com.toasterofbread.spmp.resources.uilocalisation.durationToString
import com.toasterofbread.spmp.ui.component.*
Expand Down Expand Up @@ -72,7 +73,8 @@ fun PlaylistPage(
var reorderable: Boolean by remember { mutableStateOf(false) }
var current_filter: String? by remember { mutableStateOf(null) }
var current_sort_option: SortOption by remember { mutableStateOf(SortOption.PLAYLIST) }
val top_padding = padding.calculateTopPadding()
val vertical_padding = SpMp.context.getDefaultVerticalPadding()
val top_padding = padding.calculateTopPadding() + vertical_padding

LaunchedEffect(playlist) {
accent_colour = null
Expand Down Expand Up @@ -150,14 +152,15 @@ fun PlaylistPage(
var editing_info by remember { mutableStateOf(false) }
val horizontal_padding = SpMp.context.getDefaultHorizontalPadding()

val final_padding by MultiselectAndMusicTopBar(
val final_padding = MultiselectAndMusicTopBar(
multiselect_context,
Modifier.fillMaxWidth(),
show_wave_border = false,
padding = padding.copy(
top = if (previous_item != null) 0.dp else top_padding,
start = horizontal_padding,
end = horizontal_padding
end = horizontal_padding,
bottom = padding.calculateBottomPadding() + vertical_padding
)
)

Expand Down

0 comments on commit 999a277

Please sign in to comment.