NowPlayingNotification.kt 4.9KB

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