playback: add pre-R inset modification

Add the ability for PlaybackBarLayout to modify window insets on
android versions before R.
This commit is contained in:
OxygenCobalt 2021-10-31 14:05:16 -06:00
parent c1e1329c21
commit 7c1382db49
No known key found for this signature in database
GPG key ID: 37DBE3621FE9AD47
2 changed files with 23 additions and 25 deletions

View file

@ -28,7 +28,6 @@ import android.view.WindowInsets
import androidx.annotation.AttrRes import androidx.annotation.AttrRes
import androidx.annotation.StyleRes import androidx.annotation.StyleRes
import androidx.core.view.children import androidx.core.view.children
import androidx.core.view.isVisible
import androidx.core.view.updatePadding import androidx.core.view.updatePadding
import org.oxycblt.auxio.music.Song import org.oxycblt.auxio.music.Song
import org.oxycblt.auxio.util.systemBarsCompat import org.oxycblt.auxio.util.systemBarsCompat
@ -55,14 +54,12 @@ class PlaybackBarLayout @JvmOverloads constructor(
init { init {
addView(playbackView) addView(playbackView)
playbackView.apply { (playbackView.layoutParams as LayoutParams).apply {
(layoutParams as LayoutParams).apply {
width = ViewGroup.LayoutParams.MATCH_PARENT width = ViewGroup.LayoutParams.MATCH_PARENT
height = ViewGroup.LayoutParams.WRAP_CONTENT height = ViewGroup.LayoutParams.WRAP_CONTENT
isBar = true isBar = true
} }
} }
}
override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) { override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec) super.onMeasure(widthMeasureSpec, heightMeasureSpec)
@ -100,12 +97,7 @@ class PlaybackBarLayout @JvmOverloads constructor(
} }
override fun onLayout(changed: Boolean, l: Int, t: Int, r: Int, b: Int) { override fun onLayout(changed: Boolean, l: Int, t: Int, r: Int, b: Int) {
val barHeight = if (playbackView.isVisible) { val barHeight = playbackView.measuredHeight
playbackView.measuredHeight
} else {
0
}
val barHeightAdjusted = (barHeight * (playbackView.layoutParams as LayoutParams).offset).toInt() val barHeightAdjusted = (barHeight * (playbackView.layoutParams as LayoutParams).offset).toInt()
for (child in children) { for (child in children) {
@ -143,20 +135,23 @@ class PlaybackBarLayout @JvmOverloads constructor(
val insets = lastInsets val insets = lastInsets
if (insets != null) { if (insets != null) {
super.dispatchApplyWindowInsets(mutateInsets(insets)) val adjustedInsets = adjustInsets(insets)
for (child in children) {
child.dispatchApplyWindowInsets(adjustedInsets)
}
} }
} }
private fun mutateInsets(insets: WindowInsets): WindowInsets { private fun adjustInsets(insets: WindowInsets): WindowInsets {
val barParams = playbackView.layoutParams as LayoutParams val barParams = playbackView.layoutParams as LayoutParams
val childConsumedInset = (playbackView.measuredHeight * barParams.offset).toInt() val childConsumedInset = (playbackView.measuredHeight * barParams.offset).toInt()
val bars = insets.systemBarsCompat val bars = insets.systemBarsCompat
// TODO: Q support return when {
when {
Build.VERSION.SDK_INT >= Build.VERSION_CODES.R -> { Build.VERSION.SDK_INT >= Build.VERSION_CODES.R -> {
return WindowInsets.Builder(insets) WindowInsets.Builder(insets)
.setInsets( .setInsets(
WindowInsets.Type.systemBars(), WindowInsets.Type.systemBars(),
Insets.of( Insets.of(
@ -166,9 +161,17 @@ class PlaybackBarLayout @JvmOverloads constructor(
) )
.build() .build()
} }
Build.VERSION.SDK_INT >= Build.VERSION_CODES.O_MR1 -> {
@Suppress("DEPRECATION")
insets.replaceSystemWindowInsets(
bars.left, bars.top,
bars.right, (bars.bottom - childConsumedInset).coerceAtLeast(0)
)
} }
return insets else -> insets
}
} }
fun setSong(song: Song?) { fun setSong(song: Song?) {

View file

@ -13,13 +13,8 @@
</data> </data>
<merge <merge
android:id="@+id/playback_layout"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:animateLayoutChanges="true"
android:background="@drawable/ui_background_ripple"
android:clickable="true"
android:focusable="true"
tools:parentTag="androidx.constraintlayout.widget.ConstraintLayout"> tools:parentTag="androidx.constraintlayout.widget.ConstraintLayout">
<ImageView <ImageView