Skip to content

Commit

Permalink
Revamp capabilities setup
Browse files Browse the repository at this point in the history
  • Loading branch information
dcvz committed Jul 27, 2023
1 parent 4caf58f commit 1cb7bd2
Show file tree
Hide file tree
Showing 4 changed files with 209 additions and 222 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -136,17 +136,21 @@ class FirstFragment : Fragment() {
}

private fun setupNotification() {
val notificationConfig = NotificationConfig(
listOf(
NotificationButton.PLAY_PAUSE(),
NotificationButton.NEXT(isCompact = true),
NotificationButton.PREVIOUS(isCompact = true),
NotificationButton.BACKWARD(isCompact = true),
NotificationButton.FORWARD(isCompact = true, icon = com.google.android.exoplayer2.ui.R.drawable.exo_icon_circular_play),
NotificationButton.SEEK_TO
), accentColor = null, smallIcon = null, pendingIntent = null
val capabilitiesConfig = CapabilitiesConfig(
capabilities = listOf(
Capability.PLAY_PAUSE(),
Capability.NEXT(notificationConfig = NofiticationActionConfig(isCompact = true)),
Capability.PREVIOUS(notificationConfig = NofiticationActionConfig(isCompact = true)),
Capability.FORWARD(notificationConfig = NofiticationActionConfig(isCompact = true)),
Capability.BACKWARD(notificationConfig = NofiticationActionConfig(isCompact = true, icon = com.google.android.exoplayer2.ui.R.drawable.exo_icon_circular_play)),
Capability.SEEK_TO
),
notificationConfig = NotificationConfig(
accentColor = null, smallIcon = null, pendingIntent = null
)
)
player.notificationManager.createNotification(notificationConfig)

player.notificationManager.createNotification(capabilitiesConfig)
}

override fun onDestroyView() {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,19 +1,83 @@
package com.doublesymmetry.kotlinaudio.models

enum class Capability {
PLAY,
PLAY_FROM_ID,
PLAY_FROM_SEARCH,
PAUSE,
STOP,
SEEK_TO,
SKIP,
SKIP_TO_NEXT,
SKIP_TO_PREVIOUS,
JUMP_FORWARD,
JUMP_BACKWARD,
SET_RATING,
LIKE,
DISLIKE,
BOOKMARK
import android.support.v4.media.RatingCompat

/**
* Defines the capabilities supported by the media session and whether they're also supported by the notification.
* @see [com.doublesymmetry.kotlinaudio.notification.NotificationManager.showPlayPauseButton]
* @see [com.doublesymmetry.kotlinaudio.notification.NotificationManager.showStopButton]
* @see [com.doublesymmetry.kotlinaudio.notification.NotificationManager.showRewindButton]
* @see [com.doublesymmetry.kotlinaudio.notification.NotificationManager.showRewindButtonCompact]
* @see [com.doublesymmetry.kotlinaudio.notification.NotificationManager.showForwardButton]
* @see [com.doublesymmetry.kotlinaudio.notification.NotificationManager.showForwardButtonCompact]
* @see [com.doublesymmetry.kotlinaudio.notification.NotificationManager.showNextButton]
* @see [com.doublesymmetry.kotlinaudio.notification.NotificationManager.showNextButtonCompact]
* @see [com.doublesymmetry.kotlinaudio.notification.NotificationManager.showPreviousButton]
* @see [com.doublesymmetry.kotlinaudio.notification.NotificationManager.showPreviousButtonCompact]
*/
sealed class Capability {
/**
* Play and pause capability with optional configuration for the notification.
* @param showInNotification Whether to show the button in the notification. Defaults to true.
* @param notificationConfig The configuration for the notification button.
*/
data class PLAY_PAUSE(val showInNotification: Boolean = true, val notificationConfig: NofiticationPlayPauseActionConfig? = null): Capability()

/** Play from a media id capability. Used in the media session. */
object PLAY_FROM_ID: Capability()
/** Play from search capability. Used in the media session. */
object PLAY_FROM_SEARCH: Capability()
/**
* Stop capability with optional configuration for the notification.
* @param showInNotification Whether to show the button in the notification. Defaults to true.
* @param notificationConfig The configuration for the notification button.
*/
data class STOP(val showInNotification: Boolean = true, val notificationConfig: NofiticationIconActionConfig? = null): Capability()
/** Seek to capability. Used in the media session. */
object SEEK_TO: Capability()
/** Skip to queue item capability. Used in the media session. */
object SKIP: Capability()
/**
* Skip to next item capability with optional configuration for the notification.
* @param showInNotification Whether to show the button in the notification. Defaults to true.
* @param notificationConfig The configuration for the notification button.
*/
data class NEXT(val showInNotification: Boolean = true, val notificationConfig: NofiticationActionConfig? = null): Capability()
/**
* Skip to previous item capability with optional configuration for the notification.
* @param showInNotification Whether to show the button in the notification. Defaults to true.
* @param notificationConfig The configuration for the notification button.
*/
data class PREVIOUS(val showInNotification: Boolean = true, val notificationConfig: NofiticationActionConfig? = null): Capability()
/**
* Skip forward by interval capability with optional configuration for the notification.
* @param showInNotification Whether to show the button in the notification. Defaults to true.
* @param notificationConfig The configuration for the notification button.
*/
data class FORWARD(val showInNotification: Boolean = true, val notificationConfig: NofiticationActionConfig? = null): Capability()
/**
* Skip backward by interval capability with optional configuration for the notification.
* @param showInNotification Whether to show the button in the notification. Defaults to true.
* @param notificationConfig The configuration for the notification button.
*/
data class BACKWARD(val showInNotification: Boolean = true, val notificationConfig: NofiticationActionConfig? = null): Capability()
/** Set rating capability. Used in the media session. */
data class SET_RATING(val type: Int): Capability()
}

data class CapabilitiesConfig(
val capabilities: List<Capability>,
val notificationConfig: NotificationConfig,
)

/** Custom extension to filter out only those capabilities that are supported by the notification */
fun List<Capability>.filterForNotification(): List<Capability> {
return this.filter {
(it is Capability.PLAY_PAUSE && it.showInNotification)
|| (it is Capability.STOP && it.showInNotification)
|| (it is Capability.NEXT && it.showInNotification)
|| (it is Capability.PREVIOUS && it.showInNotification)
|| (it is Capability.FORWARD && it.showInNotification)
|| (it is Capability.BACKWARD && it.showInNotification)
} ?: emptyList()
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,38 +5,30 @@ import androidx.annotation.DrawableRes

/**
* Used to configure the player notification.
* @param buttons Provide customized notification buttons. They will be shown by default. Note that buttons can still be shown and hidden at runtime by using the functions in [NotificationManager][com.doublesymmetry.kotlinaudio.notification.NotificationManager], but they will have the default icon if not set explicitly here.
* @param accentColor The accent color of the notification.
* @param smallIcon The small icon of the notification which is also shown in the system status bar.
* @param pendingIntent The [PendingIntent] that would be called when tapping on the notification itself.
*/
data class NotificationConfig(
val buttons: List<NotificationButton>,
val accentColor: Int? = null,
@DrawableRes val smallIcon: Int? = null,
val pendingIntent: PendingIntent? = null
)

/**
* Provide customized notification buttons. They will be shown by default. Note that buttons can still be shown and hidden at runtime by using the functions in [NotificationManager][com.doublesymmetry.kotlinaudio.notification.NotificationManager], but they will have the default icon if not set explicitly here.
* @see [com.doublesymmetry.kotlinaudio.notification.NotificationManager.showPlayPauseButton]
* @see [com.doublesymmetry.kotlinaudio.notification.NotificationManager.showStopButton]
* @see [com.doublesymmetry.kotlinaudio.notification.NotificationManager.showRewindButton]
* @see [com.doublesymmetry.kotlinaudio.notification.NotificationManager.showRewindButtonCompact]
* @see [com.doublesymmetry.kotlinaudio.notification.NotificationManager.showForwardButton]
* @see [com.doublesymmetry.kotlinaudio.notification.NotificationManager.showForwardButtonCompact]
* @see [com.doublesymmetry.kotlinaudio.notification.NotificationManager.showNextButton]
* @see [com.doublesymmetry.kotlinaudio.notification.NotificationManager.showNextButtonCompact]
* @see [com.doublesymmetry.kotlinaudio.notification.NotificationManager.showPreviousButton]
* @see [com.doublesymmetry.kotlinaudio.notification.NotificationManager.showPreviousButtonCompact]
*/
@Suppress("ClassName")
sealed class NotificationButton {
class PLAY_PAUSE(@DrawableRes val playIcon: Int? = null, @DrawableRes val pauseIcon: Int? = null): NotificationButton()
class STOP(@DrawableRes val icon: Int? = null): NotificationButton()
class FORWARD(@DrawableRes val icon: Int? = null, val isCompact: Boolean = false): NotificationButton()
class BACKWARD(@DrawableRes val icon: Int? = null, val isCompact: Boolean = false): NotificationButton()
class NEXT(@DrawableRes val icon: Int? = null, val isCompact: Boolean = false): NotificationButton()
class PREVIOUS(@DrawableRes val icon: Int? = null, val isCompact: Boolean = false): NotificationButton()
object SEEK_TO : NotificationButton()
}
/** Used to configure the properties of a standard notification button */
data class NofiticationActionConfig(
@DrawableRes val icon: Int? = null,
val isCompact: Boolean = false
)

/** Used to configure the properties of a standard notification button */
data class NofiticationIconActionConfig(
@DrawableRes val icon: Int? = null,
)

/** Used to configure the properties of a standard notification button */
data class NofiticationPlayPauseActionConfig(
@DrawableRes val playIcon: Int? = null,
@DrawableRes val pauseIcon: Int? = null,
val isCompact: Boolean = false
)
Loading

0 comments on commit 1cb7bd2

Please sign in to comment.