Skip to content

Commit 0ce0156

Browse files
committed
migrate to patched notif api for now
1 parent 1cacb25 commit 0ce0156

4 files changed

Lines changed: 27 additions & 68 deletions

File tree

app/src/main/java/androidx/media3/session/ext.kt

Lines changed: 0 additions & 17 deletions
This file was deleted.

app/src/main/java/org/akanework/gramophone/logic/GramophonePlaybackService.kt

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,6 @@ import androidx.media3.session.MediaSessionService
7575
import androidx.media3.session.SessionCommand
7676
import androidx.media3.session.SessionError
7777
import androidx.media3.session.SessionResult
78-
import androidx.media3.session.doUpdateNotification
7978
import androidx.preference.PreferenceManager
8079
import coil3.BitmapImage
8180
import coil3.imageLoader
@@ -972,7 +971,7 @@ class GramophonePlaybackService : MediaLibraryService(), MediaSessionService.Lis
972971
else null
973972
if (lastSentHighlightedLyric != highlightedLyric) {
974973
lastSentHighlightedLyric = highlightedLyric
975-
doUpdateNotification(mediaSession!!)
974+
manuallyUpdateMediaNotification(mediaSession!!)
976975
}
977976
}
978977

Lines changed: 25 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
package org.akanework.gramophone.logic.ui
22

3-
import android.content.Context
3+
import android.app.Notification
44
import android.os.Bundle
55
import androidx.annotation.OptIn
66
import androidx.core.app.NotificationCompat
@@ -10,25 +10,29 @@ import androidx.media3.session.DefaultMediaNotificationProvider
1010
import androidx.media3.session.MediaNotification
1111
import androidx.media3.session.MediaSession
1212
import androidx.media3.session.MediaSessionService
13-
import androidx.media3.session.isManualNotificationUpdate
1413
import com.google.common.collect.ImmutableList
1514
import org.akanework.gramophone.R
1615

1716
private const val FLAG_ALWAYS_SHOW_TICKER = 0x01000000
1817
private const val FLAG_ONLY_UPDATE_TICKER = 0x02000000
1918

2019
@OptIn(UnstableApi::class)
21-
private class InnerMeiZuLyricsMediaNotificationProvider(
22-
context: Context,
20+
class MeiZuLyricsMediaNotificationProvider(
21+
context: MediaSessionService,
2322
private val tickerProvider: () -> CharSequence?
2423
) : DefaultMediaNotificationProvider(context) {
24+
private var ticker: CharSequence? = null
25+
init {
26+
setSmallIcon(R.drawable.ic_gramophone_monochrome)
27+
}
28+
2529
override fun addNotificationActions(
2630
mediaSession: MediaSession,
2731
mediaButtons: ImmutableList<CommandButton>,
2832
builder: NotificationCompat.Builder,
2933
actionFactory: MediaNotification.ActionFactory
3034
): IntArray {
31-
val ticker = tickerProvider()
35+
ticker = tickerProvider()
3236
builder.setTicker(ticker)
3337
if (ticker != null) {
3438
builder.addExtras(Bundle().apply {
@@ -39,52 +43,25 @@ private class InnerMeiZuLyricsMediaNotificationProvider(
3943
}
4044
return super.addNotificationActions(mediaSession, mediaButtons, builder, actionFactory)
4145
}
42-
}
4346

44-
@OptIn(UnstableApi::class)
45-
class MeiZuLyricsMediaNotificationProvider(
46-
context: MediaSessionService,
47-
private val tickerProvider: () -> CharSequence?
48-
) : MediaNotification.Provider {
49-
private val inner = InnerMeiZuLyricsMediaNotificationProvider(context, tickerProvider).apply {
50-
setSmallIcon(R.drawable.ic_gramophone_monochrome)
51-
}
52-
53-
override fun createNotification(
54-
mediaSession: MediaSession,
55-
customLayout: ImmutableList<CommandButton>,
56-
actionFactory: MediaNotification.ActionFactory,
57-
onNotificationChangedCallback: MediaNotification.Provider.Callback
47+
override fun createMediaNotification(
48+
notificationId: Int,
49+
notification: Notification,
50+
reason: @MediaSessionService.NotificationUpdate Int,
51+
isRebuild: Boolean
5852
): MediaNotification {
59-
val ticker = tickerProvider()
60-
return inner.createNotification(
61-
mediaSession, customLayout, actionFactory
62-
) {
63-
onNotificationChangedCallback.onNotificationChanged(it.also {
53+
val notification = super.createMediaNotification(notificationId, notification, reason, isRebuild)
54+
val isManual = !isRebuild && reason == MediaSessionService.NOTIFICATION_UPDATE_MANUAL
55+
if (ticker != null || isManual)
56+
notification.notification.apply {
57+
// Keep the status bar lyrics scrolling
6458
if (ticker != null)
65-
it.applyNotificationFlags(true, false)
66-
})
67-
}.also {
68-
if (ticker != null || isManualNotificationUpdate)
69-
it.applyNotificationFlags(ticker != null, isManualNotificationUpdate)
70-
}
71-
}
72-
73-
override fun handleCustomCommand(
74-
session: MediaSession,
75-
action: String,
76-
extras: Bundle
77-
) = inner.handleCustomCommand(session, action, extras)
78-
79-
private fun MediaNotification.applyNotificationFlags(alwaysShowTicker: Boolean, onlyUpdateTicker: Boolean) {
80-
notification.apply {
81-
// Keep the status bar lyrics scrolling
82-
if (alwaysShowTicker)
83-
flags = flags.or(FLAG_ALWAYS_SHOW_TICKER)
84-
// Only update the ticker (lyrics), and do not update other properties
85-
if (onlyUpdateTicker)
86-
flags = flags.or(FLAG_ONLY_UPDATE_TICKER)
87-
}
59+
flags = flags.or(FLAG_ALWAYS_SHOW_TICKER)
60+
// Only update the ticker (lyrics), and do not update other properties
61+
if (isManual)
62+
flags = flags.or(FLAG_ONLY_UPDATE_TICKER)
63+
}
64+
return notification
8865
}
8966

9067
}

media3

Submodule media3 updated 92 files

0 commit comments

Comments
 (0)