all: optimize playback skip/next

GPT-6 Astra hillclimbing, reviewed by me for style consistency
This commit is contained in:
Alexander Capehart
2026-11-01 15:41:35 -07:00
parent c05cebc52f
commit 421a5c53e9
9 changed files with 189 additions and 55 deletions
@@ -561,6 +561,10 @@ constructor(
invalidate()
}
addEndListener { _, canceled, value, _ ->
// A finished spring must no longer keep the phase ticker running.
if (waveTransitionAnimation === this) {
waveTransitionAnimation = null
}
currentAmplitudeFraction = value
updateActiveTrackSuppression()
ensurePhaseTickerState()
@@ -96,6 +96,7 @@ class MainFragment :
private var lastInsets: WindowInsets? = null
private var elevationNormal = 0f
private var normalCornerSize = 0f
private var lastCornerSize = Float.NaN
private var maxScaleXDistance = 0f
private var sheetRising: Boolean? = null
@Inject lateinit var uiSettings: UISettings
@@ -182,6 +183,8 @@ class MainFragment :
}
normalCornerSize = playbackSheetBehavior.sheetBackgroundDrawable.topLeftCornerResolvedSize
// The new drawables must be initialized even if the previous view had the same corners.
lastCornerSize = Float.NaN
maxScaleXDistance =
context.getDimen(MR.dimen.m3_back_progress_bottom_container_max_scale_x_distance)
@@ -304,9 +307,20 @@ class MainFragment :
val playbackLastStretchRatio = min(playbackEdgeRatio * playbackBackRatio, 1f)
binding.mainSheetScrim.alpha = playbackLastStretchRatio
playbackSheetBehavior.sheetBackgroundDrawable.setCornerSize(
normalCornerSize * (1 - playbackLastStretchRatio)
)
val sheetCornerSize = normalCornerSize * (1 - playbackLastStretchRatio)
// MaterialShapeDrawable rebuilds its shape and invalidates even for an unchanged size.
// Keep reading sheet and back-gesture progress every frame, but only apply new shapes.
if (sheetCornerSize != lastCornerSize) {
playbackSheetBehavior.sheetBackgroundDrawable.setCornerSize(sheetCornerSize)
if (queueSheetBehavior == null) {
(binding.queueSheet.background as MaterialShapeDrawable).shapeAppearanceModel =
ShapeAppearanceModel.builder()
.setTopLeftCornerSize(normalCornerSize)
.setTopRightCornerSize(sheetCornerSize)
.build()
}
lastCornerSize = sheetCornerSize
}
binding.exploreNavHost.isInvisible = playbackLastStretchRatio == 1f
binding.playbackSheet.translationZ = (1 - playbackLastStretchRatio) * elevationNormal
@@ -342,11 +356,6 @@ class MainFragment :
// No queue sheet, fade normally based on the playback sheet
binding.playbackBarFragment.alpha = playbackOutRatio
binding.playbackPanelFragment.alpha = playbackInRatio
(binding.queueSheet.background as MaterialShapeDrawable).shapeAppearanceModel =
ShapeAppearanceModel.builder()
.setTopLeftCornerSize(normalCornerSize)
.setTopRightCornerSize(normalCornerSize * (1 - playbackLastStretchRatio))
.build()
}
// Fade out the playback bar as the panel expands.
binding.playbackBarFragment.apply {
@@ -23,7 +23,9 @@ import android.util.AttributeSet
import com.google.android.material.slider.Slider
import kotlin.math.max
import org.oxycblt.auxio.databinding.ViewSeekBarBinding
import org.oxycblt.auxio.playback.dsToSecs
import org.oxycblt.auxio.playback.formatDurationDs
import org.oxycblt.auxio.playback.formatDurationSecs
import org.oxycblt.auxio.util.inflater
import timber.log.Timber as L
@@ -40,6 +42,7 @@ constructor(context: Context, attrs: AttributeSet? = null, defStyleAttr: Int = 0
Slider.OnSliderTouchListener,
Slider.OnChangeListener {
private val binding = ViewSeekBarBinding.inflate(context.inflater, this, true)
private var displayedPositionSecs: Long? = null
init {
binding.seekBarSlider.addOnSliderTouchListener(this)
@@ -71,7 +74,7 @@ constructor(context: Context, attrs: AttributeSet? = null, defStyleAttr: Int = 0
binding.seekBarSlider.value = from.toFloat()
// We would want to keep this in the listener, but the listener only fires when
// a value changes completely, and sometimes that does not happen with this view.
binding.seekBarPosition.text = from.formatDurationDs(true)
updatePositionLabel(from)
}
}
@@ -112,7 +115,18 @@ constructor(context: Context, attrs: AttributeSet? = null, defStyleAttr: Int = 0
}
override fun onValueChange(slider: Slider, value: Float, fromUser: Boolean) {
binding.seekBarPosition.text = value.toLong().formatDurationDs(true)
updatePositionLabel(value.toLong())
}
private fun updatePositionLabel(positionDs: Long) {
val seconds = positionDs.dsToSecs()
if (displayedPositionSecs == seconds) {
return
}
// The slider changes every decisecond, but its label only displays whole seconds.
// Reassigning even identical text requests layout because the label wraps its content.
binding.seekBarPosition.text = seconds.formatDurationSecs(true)
displayedPositionSecs = seconds
}
/** A listener for SeekBar interactions. */
@@ -15,7 +15,7 @@
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package org.oxycblt.auxio.playback.ui.swiper
import android.graphics.RectF
@@ -41,7 +41,9 @@ class CarouselTransformer : ViewPager2.PageTransformer {
// like since it means we cant arrange them in the carousel
// so we use translationX to fix them to the same pos so we can remask them however
// we want to create our effect
page.translationX = -position * width
// Keep fully offscreen pages at their layout positions. Pinning them too makes
// LinearLayoutManager's transformed-bounds check prevent them from being recycled.
page.translationX = if (position < -1f || position > 1f) 0f else -position * width
page.alpha = 1f
@@ -126,7 +128,17 @@ class CarouselTransformer : ViewPager2.PageTransformer {
}
}
maskable.setMaskRectF(rect)
// Material rebuilds its shape path even for the same mask. Compare its live value
// without mutating it: the getter aliases the clipping delegate's rectangle.
val currentMask = maskable.maskRectF
if (
currentMask.left != rect.left ||
currentMask.top != rect.top ||
currentMask.right != rect.right ||
currentMask.bottom != rect.bottom
) {
maskable.setMaskRectF(rect)
}
// internal page parallax
// this does assume the cover item setup but thats fine
@@ -76,6 +76,13 @@ private constructor(
/** Update [WidgetProvider] with the current playback state. */
fun update() {
if (!widgetProvider.hasInstances(context)) {
// A widget added during playback requests a fresh update through onUpdate.
// Until then, avoid loading artwork and constructing all RemoteViews variants.
bitmapProvider.release()
return
}
val song = playbackManager.currentSong
if (song == null) {
L.d("No song, resetting widget")
@@ -124,7 +131,14 @@ private constructor(
}
override fun onCompleted(bitmap: Bitmap?) {
val state = PlaybackState(song, bitmap, isPlaying, repeatMode, isShuffled)
val state =
PlaybackState(
song,
prepareWidgetBitmap(bitmap),
isPlaying,
repeatMode,
isShuffled,
)
L.d("Bitmap loaded, uploading state $state")
widgetProvider.update(context, uiSettings, state)
}
@@ -0,0 +1,37 @@
/*
* Copyright (c) 2026 Auxio Project
* WidgetPendingIntents.kt is part of Auxio.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package org.oxycblt.auxio.widgets
import android.content.Context
import org.oxycblt.auxio.playback.service.PlaybackActions
import org.oxycblt.auxio.util.newBroadcastPendingIntent
import org.oxycblt.auxio.util.newMainPendingIntent
/**
* Retrieve each action once per widget update, then share its handle across the responsive layouts.
* Recreate this bundle on the next update so cancelled system tokens can be replaced normally.
*/
internal class WidgetPendingIntents(context: Context) {
val main = context.newMainPendingIntent()
val playPause = context.newBroadcastPendingIntent(PlaybackActions.ACTION_PLAY_PAUSE)
val skipPrevious = context.newBroadcastPendingIntent(PlaybackActions.ACTION_SKIP_PREV)
val skipNext = context.newBroadcastPendingIntent(PlaybackActions.ACTION_SKIP_NEXT)
val repeat = context.newBroadcastPendingIntent(PlaybackActions.ACTION_INC_REPEAT_MODE)
val shuffle = context.newBroadcastPendingIntent(PlaybackActions.ACTION_INVERT_SHUFFLE)
}
@@ -32,11 +32,9 @@ import org.oxycblt.auxio.BuildConfig
import org.oxycblt.auxio.R
import org.oxycblt.auxio.music.resolve
import org.oxycblt.auxio.music.resolveNames
import org.oxycblt.auxio.playback.service.PlaybackActions
import org.oxycblt.auxio.playback.state.RepeatMode
import org.oxycblt.auxio.ui.UISettings
import org.oxycblt.auxio.ui.UISettingsImpl
import org.oxycblt.auxio.util.newBroadcastPendingIntent
import timber.log.Timber as L
/**
@@ -46,6 +44,14 @@ import timber.log.Timber as L
* @author Alexander Capehart (OxygenCobalt)
*/
class WidgetProvider : AppWidgetProvider() {
/**
* Query current hosts instead of caching presence, so adding a widget takes effect immediately.
*/
fun hasInstances(context: Context): Boolean =
AppWidgetManager.getInstance(context)
.getAppWidgetIds(ComponentName(context, this::class.java))
.isNotEmpty()
override fun onUpdate(
context: Context,
appWidgetManager: AppWidgetManager,
@@ -88,21 +94,23 @@ class WidgetProvider : AppWidgetProvider() {
}
val awm = AppWidgetManager.getInstance(context)
val pendingIntents = WidgetPendingIntents(context)
// Create and configure each possible layout for the widget. These dimensions seem
// arbitrary, but they are actually the minimum dimensions required to fit all of
// the widget elements, plus some leeway for text sizing.
val defaultLayout = newThinDockedLayout(context, uiSettings, state)
val defaultLayout = newThinDockedLayout(context, uiSettings, state, pendingIntents)
val views =
mapOf(
SizeF(180f, 48f) to newThinStickLayout(context, state),
SizeF(304f, 48f) to newWideStickLayout(context, state),
SizeF(180f, 80f) to newThinWaferLayout(context, uiSettings, state),
SizeF(304f, 80f) to newWideWaferLayout(context, uiSettings, state),
SizeF(180f, 48f) to newThinStickLayout(context, state, pendingIntents),
SizeF(304f, 48f) to newWideStickLayout(context, state, pendingIntents),
SizeF(180f, 80f) to newThinWaferLayout(context, uiSettings, state, pendingIntents),
SizeF(304f, 80f) to newWideWaferLayout(context, uiSettings, state, pendingIntents),
SizeF(180f, 152f) to defaultLayout,
SizeF(304f, 152f) to newWideDockedLayout(context, uiSettings, state),
SizeF(180f, 272f) to newThinPaneLayout(context, uiSettings, state),
SizeF(304f, 272f) to newWidePaneLayout(context, uiSettings, state),
SizeF(304f, 152f) to
newWideDockedLayout(context, uiSettings, state, pendingIntents),
SizeF(180f, 272f) to newThinPaneLayout(context, uiSettings, state, pendingIntents),
SizeF(304f, 272f) to newWidePaneLayout(context, uiSettings, state, pendingIntents),
)
// This is the order in which we will disable cover art layouts if they exceed the
@@ -186,73 +194,89 @@ class WidgetProvider : AppWidgetProvider() {
.setupBackground(uiSettings)
.setupBackground(uiSettings)
private fun newThinStickLayout(context: Context, state: WidgetComponent.PlaybackState) =
newRemoteViews(context, R.layout.widget_stick_thin).setupTimelineControls(context, state)
private fun newThinStickLayout(
context: Context,
state: WidgetComponent.PlaybackState,
pendingIntents: WidgetPendingIntents,
) =
newRemoteViews(context, R.layout.widget_stick_thin, pendingIntents.main)
.setupTimelineControls(pendingIntents, state)
private fun newWideStickLayout(context: Context, state: WidgetComponent.PlaybackState) =
newRemoteViews(context, R.layout.widget_stick_wide).setupFullControls(context, state)
private fun newWideStickLayout(
context: Context,
state: WidgetComponent.PlaybackState,
pendingIntents: WidgetPendingIntents,
) =
newRemoteViews(context, R.layout.widget_stick_wide, pendingIntents.main)
.setupFullControls(pendingIntents, state)
private fun newThinWaferLayout(
context: Context,
uiSettings: UISettings,
state: WidgetComponent.PlaybackState,
pendingIntents: WidgetPendingIntents,
) =
newRemoteViews(context, R.layout.widget_wafer_thin)
newRemoteViews(context, R.layout.widget_wafer_thin, pendingIntents.main)
.setupBackground(uiSettings)
.setupCover(context, state.takeIf { canDisplayWaferCover(uiSettings) })
.setupFillingCover(uiSettings)
.setupTimelineControls(context, state)
.setupTimelineControls(pendingIntents, state)
private fun newWideWaferLayout(
context: Context,
uiSettings: UISettings,
state: WidgetComponent.PlaybackState,
pendingIntents: WidgetPendingIntents,
) =
newRemoteViews(context, R.layout.widget_wafer_wide)
newRemoteViews(context, R.layout.widget_wafer_wide, pendingIntents.main)
.setupBackground(uiSettings)
.setupCover(context, state.takeIf { canDisplayWaferCover(uiSettings) })
.setupFillingCover(uiSettings)
.setupFullControls(context, state)
.setupFullControls(pendingIntents, state)
private fun newThinDockedLayout(
context: Context,
uiSettings: UISettings,
state: WidgetComponent.PlaybackState,
pendingIntents: WidgetPendingIntents,
) =
newRemoteViews(context, R.layout.widget_docked_thin)
newRemoteViews(context, R.layout.widget_docked_thin, pendingIntents.main)
.setupBar(uiSettings)
.setupCover(context, state)
.setupTimelineControls(context, state)
.setupTimelineControls(pendingIntents, state)
private fun newWideDockedLayout(
context: Context,
uiSettings: UISettings,
state: WidgetComponent.PlaybackState,
pendingIntents: WidgetPendingIntents,
) =
newRemoteViews(context, R.layout.widget_docked_wide)
newRemoteViews(context, R.layout.widget_docked_wide, pendingIntents.main)
.setupBar(uiSettings)
.setupCover(context, state)
.setupFullControls(context, state)
.setupFullControls(pendingIntents, state)
private fun newThinPaneLayout(
context: Context,
uiSettings: UISettings,
state: WidgetComponent.PlaybackState,
pendingIntents: WidgetPendingIntents,
) =
newRemoteViews(context, R.layout.widget_pane_thin)
newRemoteViews(context, R.layout.widget_pane_thin, pendingIntents.main)
.setupBackground(uiSettings)
.setupPlaybackState(context, state)
.setupTimelineControls(context, state)
.setupTimelineControls(pendingIntents, state)
private fun newWidePaneLayout(
context: Context,
uiSettings: UISettings,
state: WidgetComponent.PlaybackState,
pendingIntents: WidgetPendingIntents,
) =
newRemoteViews(context, R.layout.widget_pane_wide)
newRemoteViews(context, R.layout.widget_pane_wide, pendingIntents.main)
.setupBackground(uiSettings)
.setupPlaybackState(context, state)
.setupFullControls(context, state)
.setupFullControls(pendingIntents, state)
/**
* Set up the control bar in a [RemoteViews] layout that contains one. This is a kind of
@@ -360,18 +384,18 @@ class WidgetProvider : AppWidgetProvider() {
/**
* Set up the play/pause button in a [RemoteViews] layout that contains one.
*
* @param context [Context] required to set up the view.
* @param pendingIntents Shared control actions for this widget update.
* @param state Current [WidgetComponent.PlaybackState] to display.
*/
private fun RemoteViews.setupBasicControls(
context: Context,
pendingIntents: WidgetPendingIntents,
state: WidgetComponent.PlaybackState,
): RemoteViews {
// Hook the play/pause button to the play/pause broadcast that will be recognized
// by PlaybackService.
setOnClickPendingIntent(
R.id.widget_play_pause,
context.newBroadcastPendingIntent(PlaybackActions.ACTION_PLAY_PAUSE),
pendingIntents.playPause,
)
// Set up the play/pause button appearance. Like the Android 13 media controls, use
@@ -398,26 +422,26 @@ class WidgetProvider : AppWidgetProvider() {
* Set up the play/pause and skip previous/next button in a [RemoteViews] layout that contains
* them.
*
* @param context [Context] required to set up the view.
* @param pendingIntents Shared control actions for this widget update.
* @param state Current [WidgetComponent.PlaybackState] to display.
*/
private fun RemoteViews.setupTimelineControls(
context: Context,
pendingIntents: WidgetPendingIntents,
state: WidgetComponent.PlaybackState,
): RemoteViews {
// Timeline controls contain the basic controls, set those up
setupBasicControls(context, state)
setupBasicControls(pendingIntents, state)
// Timeline elements should always be left-to-right.
setLayoutDirection(R.id.widget_controls, View.LAYOUT_DIRECTION_LTR)
// Hook the skip buttons to the respective broadcasts that can be recognized
// by PlaybackService.
setOnClickPendingIntent(
R.id.widget_skip_prev,
context.newBroadcastPendingIntent(PlaybackActions.ACTION_SKIP_PREV),
pendingIntents.skipPrevious,
)
setOnClickPendingIntent(
R.id.widget_skip_next,
context.newBroadcastPendingIntent(PlaybackActions.ACTION_SKIP_NEXT),
pendingIntents.skipNext,
)
return this
}
@@ -426,25 +450,25 @@ class WidgetProvider : AppWidgetProvider() {
* Set up the play/pause, skip previous/next, and repeat/shuffle buttons in a [RemoteViews] that
* contains them.
*
* @param context [Context] required to set up the view.
* @param pendingIntents Shared control actions for this widget update.
* @param state Current [WidgetComponent.PlaybackState] to display.
*/
private fun RemoteViews.setupFullControls(
context: Context,
pendingIntents: WidgetPendingIntents,
state: WidgetComponent.PlaybackState,
): RemoteViews {
// Full controls contain timeline controls, make are set those.
setupTimelineControls(context, state)
setupTimelineControls(pendingIntents, state)
// Hook the repeat/shuffle buttons to the respective broadcasts that can
// be recognized by PlaybackService.
setOnClickPendingIntent(
R.id.widget_repeat,
context.newBroadcastPendingIntent(PlaybackActions.ACTION_INC_REPEAT_MODE),
pendingIntents.repeat,
)
setOnClickPendingIntent(
R.id.widget_shuffle,
context.newBroadcastPendingIntent(PlaybackActions.ACTION_INVERT_SHUFFLE),
pendingIntents.shuffle,
)
// Set up the repeat/shuffle buttons. When working with RemoteViews, we will
@@ -18,9 +18,11 @@
package org.oxycblt.auxio.widgets
import android.app.PendingIntent
import android.appwidget.AppWidgetManager
import android.content.ComponentName
import android.content.Context
import android.graphics.Bitmap
import android.os.Build
import android.util.SizeF
import android.widget.RemoteViews
@@ -31,17 +33,32 @@ import org.oxycblt.auxio.util.isLandscape
import org.oxycblt.auxio.util.newMainPendingIntent
import timber.log.Timber as L
/** Share one immutable artwork snapshot across the RemoteViews in an update. */
internal fun prepareWidgetBitmap(bitmap: Bitmap?): Bitmap? =
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU && bitmap?.isMutable == true) {
// Android 13+ copies mutable artwork separately into each RemoteViews cache.
// Preparing it once also lets the responsive parent reuse the same snapshot.
bitmap.asShared()
} else {
bitmap
}
/**
* Create a [RemoteViews] instance with the specified layout and an automatic click handler to open
* the Auxio activity.
*
* @param context [Context] required to create [RemoteViews].
* @param layoutRes Resource ID of the layout to use. Must be compatible with [RemoteViews].
* @param mainPendingIntent App-opening action, optionally shared across layouts in one update.
* @return A new [RemoteViews] instance with the specified configuration.
*/
fun newRemoteViews(context: Context, @LayoutRes layoutRes: Int): RemoteViews {
fun newRemoteViews(
context: Context,
@LayoutRes layoutRes: Int,
mainPendingIntent: PendingIntent = context.newMainPendingIntent(),
): RemoteViews {
val views = RemoteViews(context.packageName, layoutRes)
views.setOnClickPendingIntent(android.R.id.background, context.newMainPendingIntent())
views.setOnClickPendingIntent(android.R.id.background, mainPendingIntent)
return views
}