Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit c57e0699 authored by Robert Snoeberger's avatar Robert Snoeberger
Browse files

Resumption player colorized from the album art

Bug: 159229758
Test: manual - Play Spotify, reboot, and check player background color
to match album artwork.

Change-Id: I04da95ed541ff4ff8aae3a87146bd7f4954da0cf
parent f769298b
Loading
Loading
Loading
Loading
+30 −21
Original line number Diff line number Diff line
@@ -293,10 +293,11 @@ class MediaDataManager @Inject constructor(
        } else {
            null
        }
        val bgColor = artworkBitmap?.let { computeBackgroundColor(it) } ?: Color.DKGRAY

        val mediaAction = getResumeMediaAction(resumeAction)
        foregroundExecutor.execute {
            onMediaDataLoaded(packageName, null, MediaData(true, Color.DKGRAY, appName,
            onMediaDataLoaded(packageName, null, MediaData(true, bgColor, appName,
                    null, desc.subtitle, desc.title, artworkIcon, listOf(mediaAction), listOf(0),
                    packageName, token, appIntent, device = null, active = false,
                    resumeAction = resumeAction))
@@ -319,7 +320,6 @@ class MediaDataManager @Inject constructor(

        // Foreground and Background colors computed from album art
        val notif: Notification = sbn.notification
        var bgColor = Color.WHITE
        var artworkBitmap = metadata.getBitmap(MediaMetadata.METADATA_KEY_ART)
        if (artworkBitmap == null) {
            artworkBitmap = metadata.getBitmap(MediaMetadata.METADATA_KEY_ALBUM_ART)
@@ -349,26 +349,8 @@ class MediaDataManager @Inject constructor(
                    drawable.draw(canvas)
                }
            }
            val p = MediaNotificationProcessor.generateArtworkPaletteBuilder(artworkBitmap)
                    .generate()
            val swatch = MediaNotificationProcessor.findBackgroundSwatch(p)
            bgColor = swatch.rgb
        }
        // Adapt background color, so it's always subdued and text is legible
        val tmpHsl = floatArrayOf(0f, 0f, 0f)
        ColorUtils.colorToHSL(bgColor, tmpHsl)

        val l = tmpHsl[2]
        // Colors with very low luminosity can have any saturation. This means that changing the
        // luminosity can make a black become red. Let's remove the saturation of very light or
        // very dark colors to avoid this issue.
        if (l < LUMINOSITY_THRESHOLD || l > 1f - LUMINOSITY_THRESHOLD) {
            tmpHsl[1] = 0f
        }
        tmpHsl[1] *= SATURATION_MULTIPLIER
        tmpHsl[2] = DEFAULT_LUMINOSITY

        bgColor = ColorUtils.HSLToColor(tmpHsl)
        val bgColor = computeBackgroundColor(artworkBitmap)

        // App name
        val builder = Notification.Builder.recoverBuilder(context, notif)
@@ -480,6 +462,33 @@ class MediaDataManager @Inject constructor(
        }
    }

    private fun computeBackgroundColor(artworkBitmap: Bitmap?): Int {
        var color = Color.WHITE
        if (artworkBitmap != null) {
            // If we have art, get colors from that
            val p = MediaNotificationProcessor.generateArtworkPaletteBuilder(artworkBitmap)
                    .generate()
            val swatch = MediaNotificationProcessor.findBackgroundSwatch(p)
            color = swatch.rgb
        }
        // Adapt background color, so it's always subdued and text is legible
        val tmpHsl = floatArrayOf(0f, 0f, 0f)
        ColorUtils.colorToHSL(color, tmpHsl)

        val l = tmpHsl[2]
        // Colors with very low luminosity can have any saturation. This means that changing the
        // luminosity can make a black become red. Let's remove the saturation of very light or
        // very dark colors to avoid this issue.
        if (l < LUMINOSITY_THRESHOLD || l > 1f - LUMINOSITY_THRESHOLD) {
            tmpHsl[1] = 0f
        }
        tmpHsl[1] *= SATURATION_MULTIPLIER
        tmpHsl[2] = DEFAULT_LUMINOSITY

        color = ColorUtils.HSLToColor(tmpHsl)
        return color
    }

    private fun getResumeMediaAction(action: Runnable): MediaAction {
        return MediaAction(
            context.getDrawable(R.drawable.lb_ic_play),