playback: unify notif and bar actions

Unify notif and bar actions under a new "ActionMode" enum.

This just makes things consistent.
This commit is contained in:
Alexander Capehart
2022-09-26 13:22:34 -06:00
parent 936f0ca167
commit 35e402b6b4
25 changed files with 142 additions and 90 deletions
@@ -143,12 +143,12 @@ object IntegerTable {
/** ReplayGainMode.Dynamic */
const val REPLAY_GAIN_MODE_DYNAMIC = 0xA113
/** BarAction.Next */
/** ActionMode.Next */
const val BAR_ACTION_NEXT = 0xA119
/** BarAction.Repeat */
/** ActionMode.Repeat */
const val BAR_ACTION_REPEAT = 0xA11A
/** BarAction.Shuffle */
/** ActionMode.Shuffle */
const val BAR_ACTION_SHUFFLE = 0xA11B
}
@@ -0,0 +1,45 @@
/*
* Copyright (c) 2022 Auxio Project
*
* 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.playback
import org.oxycblt.auxio.IntegerTable
/** Represents the action that should be shown on the playback bar. */
enum class ActionMode {
NEXT,
REPEAT,
SHUFFLE;
val intCode: Int
get() = when (this) {
NEXT -> IntegerTable.BAR_ACTION_NEXT
REPEAT -> IntegerTable.BAR_ACTION_REPEAT
SHUFFLE -> IntegerTable.BAR_ACTION_SHUFFLE
}
companion object {
/** Convert an int [code] into an instance, or null if it isn't valid. */
fun fromIntCode(code: Int) =
when (code) {
IntegerTable.BAR_ACTION_NEXT -> NEXT
IntegerTable.BAR_ACTION_REPEAT -> REPEAT
IntegerTable.BAR_ACTION_SHUFFLE -> SHUFFLE
else -> null
}
}
}
@@ -20,7 +20,6 @@ package org.oxycblt.auxio.playback
import android.os.Bundle
import android.view.LayoutInflater
import androidx.fragment.app.activityViewModels
import org.oxycblt.auxio.IntegerTable
import org.oxycblt.auxio.R
import org.oxycblt.auxio.databinding.FragmentPlaybackBarBinding
import org.oxycblt.auxio.music.Song
@@ -73,8 +72,8 @@ class PlaybackBarFragment : ViewBindingFragment<FragmentPlaybackBarBinding>() {
// Update the secondary action to match the setting.
when (Settings(context).barAction) {
BarAction.NEXT -> {
when (Settings(context).actionMode) {
ActionMode.NEXT -> {
binding.playbackSecondaryAction.apply {
setIconResource(R.drawable.ic_skip_next_24)
contentDescription = getString(R.string.desc_skip_next)
@@ -82,7 +81,7 @@ class PlaybackBarFragment : ViewBindingFragment<FragmentPlaybackBarBinding>() {
setOnClickListener { playbackModel.next() }
}
}
BarAction.REPEAT -> {
ActionMode.REPEAT -> {
binding.playbackSecondaryAction.apply {
contentDescription = getString(R.string.desc_change_repeat)
iconTint = context.getColorCompat(R.color.sel_accented)
@@ -90,7 +89,7 @@ class PlaybackBarFragment : ViewBindingFragment<FragmentPlaybackBarBinding>() {
collectImmediately(playbackModel.repeatMode, ::updateRepeat)
}
}
BarAction.SHUFFLE -> {
ActionMode.SHUFFLE -> {
binding.playbackSecondaryAction.apply {
setIconResource(R.drawable.sel_shuffle_state_24)
contentDescription = getString(R.string.desc_shuffle)
@@ -144,21 +143,3 @@ class PlaybackBarFragment : ViewBindingFragment<FragmentPlaybackBarBinding>() {
requireBinding().playbackProgressBar.progress = positionDs.toInt()
}
}
/** Represents the action that should be shown on the playback bar. */
enum class BarAction {
NEXT,
REPEAT,
SHUFFLE;
companion object {
/** Convert an int [code] into an instance, or null if it isn't valid. */
fun fromIntCode(code: Int) =
when (code) {
IntegerTable.BAR_ACTION_NEXT -> NEXT
IntegerTable.BAR_ACTION_REPEAT -> REPEAT
IntegerTable.BAR_ACTION_SHUFFLE -> SHUFFLE
else -> null
}
}
}
@@ -32,6 +32,7 @@ import org.oxycblt.auxio.R
import org.oxycblt.auxio.image.BitmapProvider
import org.oxycblt.auxio.music.MusicParent
import org.oxycblt.auxio.music.Song
import org.oxycblt.auxio.playback.ActionMode
import org.oxycblt.auxio.playback.state.InternalPlayer
import org.oxycblt.auxio.playback.state.PlaybackStateManager
import org.oxycblt.auxio.playback.state.RepeatMode
@@ -254,7 +255,7 @@ class MediaSessionComponent(private val context: Context, private val callback:
context.getString(R.string.set_key_show_covers),
context.getString(R.string.set_key_quality_covers) ->
updateMediaMetadata(playbackManager.song, playbackManager.parent)
context.getString(R.string.set_key_alt_notif_action) -> invalidateSecondaryAction()
context.getString(R.string.set_key_notif_action) -> invalidateSecondaryAction()
}
}
@@ -368,24 +369,23 @@ class MediaSessionComponent(private val context: Context, private val callback:
// Android 13+ leverages custom actions in the notification.
val extraAction =
if (settings.useAltNotifAction) {
PlaybackStateCompat.CustomAction.Builder(
PlaybackService.ACTION_INVERT_SHUFFLE,
context.getString(R.string.desc_shuffle),
if (playbackManager.isShuffled) {
R.drawable.ic_shuffle_on_24
} else {
R.drawable.ic_shuffle_off_24
}
)
} else {
PlaybackStateCompat.CustomAction.Builder(
PlaybackService.ACTION_INC_REPEAT_MODE,
context.getString(R.string.desc_change_repeat),
playbackManager.repeatMode.icon
)
}
val extraAction = when (settings.notifAction) {
ActionMode.SHUFFLE -> PlaybackStateCompat.CustomAction.Builder(
PlaybackService.ACTION_INVERT_SHUFFLE,
context.getString(R.string.desc_shuffle),
if (playbackManager.isShuffled) {
R.drawable.ic_shuffle_on_24
} else {
R.drawable.ic_shuffle_off_24
}
)
else -> PlaybackStateCompat.CustomAction.Builder(
PlaybackService.ACTION_INC_REPEAT_MODE,
context.getString(R.string.desc_change_repeat),
playbackManager.repeatMode.icon
)
}
val exitAction =
PlaybackStateCompat.CustomAction.Builder(
@@ -404,10 +404,9 @@ class MediaSessionComponent(private val context: Context, private val callback:
private fun invalidateSecondaryAction() {
invalidateSessionState()
if (settings.useAltNotifAction) {
notification.updateShuffled(playbackManager.isShuffled)
} else {
notification.updateRepeatMode(playbackManager.repeatMode)
when (settings.notifAction) {
ActionMode.SHUFFLE -> notification.updateShuffled(playbackManager.isShuffled)
else -> notification.updateRepeatMode(playbackManager.repeatMode)
}
if (!provider.isBusy) {
@@ -31,7 +31,7 @@ import org.oxycblt.auxio.music.MusicMode
import org.oxycblt.auxio.music.Sort
import org.oxycblt.auxio.music.storage.Directory
import org.oxycblt.auxio.music.storage.MusicDirs
import org.oxycblt.auxio.playback.BarAction
import org.oxycblt.auxio.playback.ActionMode
import org.oxycblt.auxio.playback.replaygain.ReplayGainMode
import org.oxycblt.auxio.playback.replaygain.ReplayGainPreAmp
import org.oxycblt.auxio.ui.accent.Accent
@@ -74,12 +74,28 @@ class Settings(private val context: Context, private val callback: Callback? = n
}
inner.edit {
putInt(context.getString(R.string.set_accent), accent)
putInt(context.getString(R.string.set_key_accent), accent)
remove(OldKeys.KEY_ACCENT3)
apply()
}
}
if (inner.contains(OldKeys.KEY_ALT_NOTIF_ACTION)) {
logD("Migrating ${OldKeys.KEY_ALT_NOTIF_ACTION}")
val mode = if (inner.getBoolean(OldKeys.KEY_ALT_NOTIF_ACTION, false)) {
ActionMode.SHUFFLE
} else {
ActionMode.REPEAT
}
inner.edit {
putInt(context.getString(R.string.set_key_notif_action), mode.intCode)
remove(OldKeys.KEY_ALT_NOTIF_ACTION)
apply()
}
}
fun Int.migratePlaybackMode() =
when (this) {
// Genre playback mode was retried in 3.0.0
@@ -100,6 +116,7 @@ class Settings(private val context: Context, private val callback: Callback? = n
inner.edit {
putInt(context.getString(R.string.set_key_library_song_playback_mode), mode.intCode)
remove(OldKeys.KEY_LIB_PLAYBACK_MODE)
apply()
}
}
@@ -114,6 +131,7 @@ class Settings(private val context: Context, private val callback: Callback? = n
mode?.intCode ?: Int.MIN_VALUE
)
remove(OldKeys.KEY_DETAIL_PLAYBACK_MODE)
apply()
}
}
}
@@ -182,19 +200,19 @@ class Settings(private val context: Context, private val callback: Callback? = n
get() = inner.getBoolean(context.getString(R.string.set_key_round_mode), false)
/** Which action to display on the playback bar. */
val barAction: BarAction
val actionMode: ActionMode
get() =
BarAction.fromIntCode(
ActionMode.fromIntCode(
inner.getInt(context.getString(R.string.set_key_bar_action), Int.MIN_VALUE)
)
?: BarAction.NEXT
?: ActionMode.NEXT
/**
* Whether to display the RepeatMode or the shuffle status on the notification. False if repeat,
* true if shuffle.
*/
val useAltNotifAction: Boolean
get() = inner.getBoolean(context.getString(R.string.set_key_alt_notif_action), false)
val notifAction: ActionMode
get() = ActionMode.fromIntCode(inner.getInt(context.getString(R.string.set_key_notif_action), Int.MIN_VALUE)) ?: ActionMode.REPEAT
/** Whether to resume playback when a headset is connected (may not work well in all cases) */
val headsetAutoplay: Boolean
@@ -439,6 +457,7 @@ class Settings(private val context: Context, private val callback: Callback? = n
/** Cache of the old keys used in Auxio. */
private object OldKeys {
const val KEY_ACCENT3 = "auxio_accent"
const val KEY_ALT_NOTIF_ACTION = "KEY_ALT_NOTIF_ACTION"
const val KEY_LIB_PLAYBACK_MODE = "KEY_SONG_PLAY_MODE2"
const val KEY_DETAIL_PLAYBACK_MODE = "auxio_detail_song_play_mode"
}