NowPlayingNotification.kt 4.9KB

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