NowPlayingNotification.kt 5.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. package fr.riff_app.riff
  2. import android.app.PendingIntent
  3. import android.content.Context
  4. import android.content.Intent
  5. import android.os.Build
  6. import android.support.v4.media.session.MediaSessionCompat
  7. import android.support.v4.media.session.PlaybackStateCompat
  8. import androidx.annotation.RequiresApi
  9. import androidx.core.app.NotificationCompat
  10. import fr.riff_app.riff.playerstore.PlayerStore
  11. class NowPlayingNotification(
  12. notificationChannelId: String,
  13. notificationChannel : Int,
  14. notificationId: Int,
  15. notificationImportance: Int
  16. ) : BaseNotification
  17. (
  18. notificationChannelId,
  19. notificationChannel,
  20. notificationId,
  21. notificationImportance
  22. ){
  23. // ########################################
  24. // ###### NOW PLAYING NOTIFICATION ########
  25. // ########################################
  26. private lateinit var mediaStyle: androidx.media.app.NotificationCompat.DecoratedMediaCustomViewStyle
  27. fun create(c: Context, m: MediaSessionCompat) {
  28. super.create(c)
  29. // got it right
  30. val delIntent = Intent(c, RadioService::class.java)
  31. delIntent.putExtra("action", Actions.KILL.name)
  32. val deleteIntent = PendingIntent.getService(c, 0, delIntent, PendingIntent.FLAG_NO_CREATE + PendingIntent.FLAG_IMMUTABLE)
  33. builder.setDeleteIntent(deleteIntent)
  34. mediaStyle = androidx.media.app.NotificationCompat.DecoratedMediaCustomViewStyle().also {
  35. it.setMediaSession(m.sessionToken)
  36. it.setShowActionsInCompactView(0, 1) // index 0 = show actions 0 and 1 (show action #0 (play/pause))
  37. it.setCancelButtonIntent(deleteIntent)
  38. }
  39. builder.setStyle(mediaStyle)
  40. update(c)
  41. }
  42. @RequiresApi(Build.VERSION_CODES.M)
  43. fun update(c: Context, isUpdatingNotificationButton: Boolean = false, isRinging: Boolean = false) {
  44. if (isUpdatingNotificationButton)
  45. builder.mActions.clear()
  46. // Title : Title of notification (usu. songArtist is first)
  47. // Text : Text of the notification (usu. songTitle is second)
  48. builder.setContentTitle(PlayerStore.instance.currentSong.artist.value)
  49. builder.setContentText(PlayerStore.instance.currentSong.title.value)
  50. // As subText, we show when the player is stopped. This is a friendly reminder that the metadata won't get updated.
  51. // Maybe later we could replace it by a nice progressBar? Would it be interesting to have one here? I don't know.
  52. if (PlayerStore.instance.playbackState.value == PlaybackStateCompat.STATE_STOPPED) {
  53. builder.setSubText("Stopped")
  54. builder.setShowWhen(false)
  55. }
  56. else {
  57. builder.setSubText(null)
  58. builder.setShowWhen(true)
  59. }
  60. if (builder.mActions.isEmpty()) {
  61. val intent = Intent(c, RadioService::class.java)
  62. val playPauseAction: NotificationCompat.Action
  63. playPauseAction = if (PlayerStore.instance.playbackState.value == PlaybackStateCompat.STATE_PLAYING) {
  64. intent.putExtra("action", Actions.PAUSE.name)
  65. val pendingButtonIntent = PendingIntent.getService(c, 1, intent, PendingIntent.FLAG_UPDATE_CURRENT + PendingIntent.FLAG_IMMUTABLE)
  66. NotificationCompat.Action.Builder(R.drawable.ic_pause, "Pause", pendingButtonIntent).build()
  67. } else {
  68. intent.putExtra("action", Actions.PLAY.name)
  69. val pendingButtonIntent = PendingIntent.getService(c, 1, intent, PendingIntent.FLAG_UPDATE_CURRENT + PendingIntent.FLAG_IMMUTABLE)
  70. NotificationCompat.Action.Builder(R.drawable.ic_play,"Play", pendingButtonIntent).build()
  71. }
  72. builder.addAction(playPauseAction)
  73. val intent2 = Intent(c, RadioService::class.java)
  74. intent2.putExtra("action", Actions.KILL.name)
  75. val pendingButtonIntent = PendingIntent.getService(c, 2, intent2, PendingIntent.FLAG_UPDATE_CURRENT + PendingIntent.FLAG_IMMUTABLE)
  76. val stopAction = NotificationCompat.Action.Builder(R.drawable.ic_close,"Stop", pendingButtonIntent).build()
  77. builder.addAction(stopAction)
  78. if (isRinging) {
  79. val snoozeString = preferenceStore.getString("snoozeDuration", "10") ?: "10"
  80. val snoozeMinutes = if (snoozeString == c.getString(R.string.disable)) 0 else Integer.parseInt(snoozeString)
  81. val snoozeIntent = Intent(c, RadioService::class.java)
  82. snoozeIntent.putExtra("action", Actions.SNOOZE.name)
  83. val pendingSnoozeIntent = PendingIntent.getService(c, 5, snoozeIntent, PendingIntent.FLAG_UPDATE_CURRENT + PendingIntent.FLAG_IMMUTABLE)
  84. val snoozeAction = NotificationCompat.Action.Builder(R.drawable.ic_alarm, "Snooze ($snoozeMinutes min.)", pendingSnoozeIntent ).build()
  85. if (snoozeMinutes > 0)
  86. builder.addAction(snoozeAction)
  87. builder.setStyle(NotificationCompat.DecoratedCustomViewStyle())
  88. } else {
  89. builder.setStyle(mediaStyle)
  90. }
  91. }
  92. builder.setLargeIcon(PlayerStore.instance.streamerPicture.value)
  93. super.show()
  94. }
  95. }