Skip to content

Commit bfc4ed4

Browse files
marcbaechingerchristosts
authored andcommitted
Inline method in PlayerService that is used from on call site only
#cleanup #minor-release PiperOrigin-RevId: 505146915 (cherry picked from commit d7ef1ab)
1 parent 631ff80 commit bfc4ed4

File tree

1 file changed

+20
-26
lines changed

1 file changed

+20
-26
lines changed

demos/session/src/main/java/androidx/media3/demo/session/PlaybackService.kt

Lines changed: 20 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
*/
1616
package androidx.media3.demo.session
1717

18-
import android.app.Notification.BigTextStyle
1918
import android.app.NotificationChannel
2019
import android.app.NotificationManager
2120
import android.app.PendingIntent.*
@@ -272,34 +271,29 @@ class PlaybackService : MediaLibraryService() {
272271
* background.
273272
*/
274273
override fun onForegroundServiceStartNotAllowedException() {
275-
createNotificationAndNotify()
274+
val notificationManagerCompat = NotificationManagerCompat.from(this@PlaybackService)
275+
ensureNotificationChannel(notificationManagerCompat)
276+
val pendingIntent =
277+
TaskStackBuilder.create(this@PlaybackService).run {
278+
addNextIntent(Intent(this@PlaybackService, MainActivity::class.java))
279+
280+
val immutableFlag = if (Build.VERSION.SDK_INT >= 23) FLAG_IMMUTABLE else 0
281+
getPendingIntent(0, immutableFlag or FLAG_UPDATE_CURRENT)
282+
}
283+
val builder =
284+
NotificationCompat.Builder(this@PlaybackService, CHANNEL_ID)
285+
.setContentIntent(pendingIntent)
286+
.setSmallIcon(R.drawable.media3_notification_small_icon)
287+
.setContentTitle(getString(R.string.notification_content_title))
288+
.setStyle(
289+
NotificationCompat.BigTextStyle().bigText(getString(R.string.notification_content_text))
290+
)
291+
.setPriority(NotificationCompat.PRIORITY_DEFAULT)
292+
.setAutoCancel(true)
293+
notificationManagerCompat.notify(NOTIFICATION_ID, builder.build())
276294
}
277295
}
278296

279-
private fun createNotificationAndNotify() {
280-
var notificationManagerCompat = NotificationManagerCompat.from(this)
281-
ensureNotificationChannel(notificationManagerCompat)
282-
var pendingIntent =
283-
TaskStackBuilder.create(this).run {
284-
addNextIntent(Intent(this@PlaybackService, MainActivity::class.java))
285-
286-
val immutableFlag = if (Build.VERSION.SDK_INT >= 23) FLAG_IMMUTABLE else 0
287-
getPendingIntent(0, immutableFlag or FLAG_UPDATE_CURRENT)
288-
}
289-
290-
var builder =
291-
NotificationCompat.Builder(this, CHANNEL_ID)
292-
.setContentIntent(pendingIntent)
293-
.setSmallIcon(R.drawable.media3_notification_small_icon)
294-
.setContentTitle(getString(R.string.notification_content_title))
295-
.setStyle(
296-
NotificationCompat.BigTextStyle().bigText(getString(R.string.notification_content_text))
297-
)
298-
.setPriority(NotificationCompat.PRIORITY_DEFAULT)
299-
.setAutoCancel(true)
300-
notificationManagerCompat.notify(NOTIFICATION_ID, builder.build())
301-
}
302-
303297
private fun ensureNotificationChannel(notificationManagerCompat: NotificationManagerCompat) {
304298
if (Util.SDK_INT < 26 || notificationManagerCompat.getNotificationChannel(CHANNEL_ID) != null) {
305299
return

0 commit comments

Comments
 (0)