Skip to content

Commit 59a0990

Browse files
neobuddy89joeyhuab
authored andcommitted
SystemUI: Color mode perf improvements for music chip bg
Signed-off-by: Pranav Vashi <neobuddy89@gmail.com>
1 parent 3142889 commit 59a0990

2 files changed

Lines changed: 37 additions & 55 deletions

File tree

packages/SystemUI/src/com/android/systemui/statusbar/OnGoingActionProgressController.kt

Lines changed: 35 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,8 @@ class OnGoingActionProgressController(
9292
private var currentChipBgColor: Int? = null
9393
private var lastColorExtractedIcon: Drawable? = null
9494
private var lastColorExtractedAlbumArt: Bitmap? = null
95+
private var chipIconColor: Int? = null
96+
private var chipAlbumColor: Int? = null
9597

9698
private var lastObservedTitle: String? = null
9799

@@ -210,7 +212,6 @@ class OnGoingActionProgressController(
210212
private fun onTrackChanged() {
211213
needsFullUiUpdate = true
212214
currentAlbumArt = null
213-
invalidateChipBgColor()
214215
scheduleAlbumArtRetry()
215216
}
216217

@@ -226,7 +227,10 @@ class OnGoingActionProgressController(
226227

227228
if (art != null) {
228229
currentAlbumArt = art
229-
if (chipColorMode == CHIP_COLOR_MODE_ALBUM_ART) invalidateChipBgColor()
230+
if (chipColorMode == CHIP_COLOR_MODE_ALBUM_ART) {
231+
invalidateChipBgColor()
232+
currentAlbumArt?.let { extractAndApplyChipBgColorFromAlbumArt(it) }
233+
}
230234
requestUiUpdate()
231235
return@launch
232236
}
@@ -332,7 +336,8 @@ class OnGoingActionProgressController(
332336
}
333337

334338
private fun extractAndApplyChipBgColorFromIcon(icon: Drawable) {
335-
if (icon === lastColorExtractedIcon) return
339+
if (!isMediaSessionActiveForChip()) return
340+
if (icon === lastColorExtractedIcon && chipIconColor != null) return
336341

337342
mainScope.launch {
338343
val size = (48f * context.resources.displayMetrics.density).toInt().coerceAtLeast(1)
@@ -346,45 +351,18 @@ class OnGoingActionProgressController(
346351
} ?: return@launch
347352

348353
lastColorExtractedIcon = icon
349-
currentChipBgColor = extractDominantColorFromBitmap(bitmap)
354+
chipIconColor = extractDominantColorFromBitmap(bitmap)
350355
updateProgressState()
351356
}
352357
}
353358

354-
private fun extractAndApplyChipBgColorFromAlbumArt(albumArt: Bitmap, iconFallback: Drawable?) {
355-
if (albumArt === lastColorExtractedAlbumArt) return
359+
private fun extractAndApplyChipBgColorFromAlbumArt(albumArt: Bitmap) {
360+
if (!isMediaSessionActiveForChip()) return
361+
if (albumArt === lastColorExtractedAlbumArt && chipAlbumColor != null) return
356362

357363
mainScope.launch {
358-
val color = extractDominantColorFromBitmap(albumArt)
359-
360-
if (color != null) {
361-
lastColorExtractedAlbumArt = albumArt
362-
currentChipBgColor = color
363-
updateProgressState()
364-
return@launch
365-
}
366-
367-
Log.d(TAG, "Album art color extraction failed, falling back to icon color")
368-
if (iconFallback == null) {
369-
lastColorExtractedAlbumArt = albumArt
370-
currentChipBgColor = null
371-
updateProgressState()
372-
return@launch
373-
}
374-
375-
val size = (48f * context.resources.displayMetrics.density).toInt().coerceAtLeast(1)
376-
val iconBitmap = try {
377-
withContext(bgDispatcher) {
378-
iconFallback.toBitmap(width = size, height = size,
379-
config = Bitmap.Config.ARGB_8888)
380-
}
381-
} catch (e: Exception) {
382-
Log.w(TAG, "Failed to rasterize icon fallback for chip color extraction", e)
383-
null
384-
}
385-
386364
lastColorExtractedAlbumArt = albumArt
387-
currentChipBgColor = iconBitmap?.let { extractDominantColorFromBitmap(it) }
365+
chipAlbumColor = extractDominantColorFromBitmap(albumArt)
388366
updateProgressState()
389367
}
390368
}
@@ -449,24 +427,15 @@ class OnGoingActionProgressController(
449427
val artistName = if (hasMediaSession) currentArtistName else null
450428

451429
if (hasMediaSession) {
452-
when (chipColorMode) {
453-
CHIP_COLOR_MODE_ICON -> {
454-
currentIcon?.let { extractAndApplyChipBgColorFromIcon(it) }
455-
}
456-
CHIP_COLOR_MODE_ALBUM_ART -> {
457-
val art = currentAlbumArt
458-
if (art != null) {
459-
extractAndApplyChipBgColorFromAlbumArt(art, currentIcon)
460-
} else {
461-
currentIcon?.let { extractAndApplyChipBgColorFromIcon(it) }
462-
}
463-
}
430+
if (chipColorMode == CHIP_COLOR_MODE_ICON &&
431+
chipIconColor != null) {
432+
currentChipBgColor = chipIconColor
433+
} else if (chipColorMode == CHIP_COLOR_MODE_ALBUM_ART &&
434+
chipAlbumColor != null) {
435+
currentChipBgColor = chipAlbumColor
464436
}
465437
}
466438

467-
val resolvedChipBgColor = if (chipColorMode != CHIP_COLOR_MODE_DEFAULT) currentChipBgColor
468-
else null
469-
470439
publish(
471440
ProgressState(
472441
isVisible = true,
@@ -480,7 +449,7 @@ class OnGoingActionProgressController(
480449
isMediaPlaying = isMediaPlaying,
481450
trackTitle = trackTitle,
482451
artistName = artistName,
483-
chipBgColor = resolvedChipBgColor,
452+
chipBgColor = currentChipBgColor,
484453
)
485454
)
486455
}
@@ -586,31 +555,37 @@ class OnGoingActionProgressController(
586555
val mediaAppIcon = mediaSessionHelper.getMediaAppIcon()
587556
if (mediaAppIcon != null) {
588557
currentIcon = mediaAppIcon
558+
updateChipIconColor()
589559
return
590560
}
591561

592562
val pkg = playbackState?.extras?.getString("package")
593563
if (pkg.isNullOrEmpty()) {
594-
setDefaultMediaIconCompact()
564+
setDefaultMediaIcon()
595565
return
596566
}
597567

598568
loadIcon(pkg) { drawable ->
599569
if (drawable != null) {
600570
currentIcon = drawable
571+
updateChipIconColor()
601572
} else {
602-
setDefaultMediaIconCompact()
573+
setDefaultMediaIcon()
603574
}
604575
updateProgressState()
605576
}
606577
}
607578

608579
private fun setDefaultMediaIcon() {
609580
currentIcon = context.resources.getDrawable(R.drawable.ic_default_music_icon, context.theme)
581+
updateChipIconColor()
610582
}
611583

612-
private fun setDefaultMediaIconCompact() {
613-
currentIcon = context.resources.getDrawable(R.drawable.ic_default_music_icon, context.theme)
584+
private fun updateChipIconColor() {
585+
if (chipColorMode == CHIP_COLOR_MODE_ICON && currentIcon != null) {
586+
invalidateChipBgColor()
587+
currentIcon?.let { extractAndApplyChipBgColorFromIcon(it) }
588+
}
614589
}
615590

616591
private fun updateNotificationProgress() {
@@ -989,6 +964,11 @@ class OnGoingActionProgressController(
989964

990965
if (wasChipColorMode != chipColorMode) {
991966
invalidateChipBgColor()
967+
if (chipColorMode == CHIP_COLOR_MODE_ALBUM_ART && currentAlbumArt != null) {
968+
currentAlbumArt?.let { extractAndApplyChipBgColorFromAlbumArt(it) }
969+
} else if (chipColorMode == CHIP_COLOR_MODE_ICON && currentIcon != null) {
970+
currentIcon?.let { extractAndApplyChipBgColorFromIcon(it) }
971+
}
992972
needsFullUiUpdate = true
993973
}
994974

packages/SystemUI/src/com/android/systemui/statusbar/OngoingActionProgressCompose.kt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -555,6 +555,8 @@ private fun MusicChip(
555555
val text = if (state.chipBgColor != null &&
556556
ColorUtils.calculateLuminance(state.chipBgColor) >= CHIP_TEXT_LUMINANCE_THRESHOLD)
557557
Color.Black
558+
else if (state.chipBgColor != null)
559+
Color.White
558560
else
559561
colorResource(android.R.color.system_accent1_100)
560562

0 commit comments

Comments
 (0)