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

Commit 7debcf9d authored by Eric Miao's avatar Eric Miao
Browse files

Get metadata description without creating MediaDescription

In MediaSession#setMetadata(), MediaMetadata#getDescription() is
called to get a MediaDescription object from the metadata, and
MediaDescription#toString() is implicitly called to get a string
description with title, subtitle and description.

This is unnecessary esp. because Bitmaps could be involved in
creating the temporary MediaDescription.

This change adds an API `MediaMetadata#getDescriptionString()`
with the same behavior without creating an intermediate object.

Bug: 400807118
Flag: EXEMPT refactor
Test: local test
Change-Id: I653fdd7a6fe087ccb4cfd47931b893c66a68fa40
parent c4e5377b
Loading
Loading
Loading
Loading
+44 −22
Original line number Diff line number Diff line
@@ -18,6 +18,7 @@ package android.media;
import android.annotation.IntRange;
import android.annotation.NonNull;
import android.annotation.Nullable;
import android.annotation.Size;
import android.annotation.StringDef;
import android.compat.annotation.UnsupportedAppUsage;
import android.content.ContentResolver;
@@ -590,31 +591,10 @@ public final class MediaMetadata implements Parcelable {

        String mediaId = getString(METADATA_KEY_MEDIA_ID);

        CharSequence[] text = new CharSequence[3];
        CharSequence[] text = getTitleSubtitleAndDescription();
        Bitmap icon = null;
        Uri iconUri = null;

        // First handle the case where display data is set already
        CharSequence displayText = getText(METADATA_KEY_DISPLAY_TITLE);
        if (!TextUtils.isEmpty(displayText)) {
            // If they have a display title use only display data, otherwise use
            // our best bets
            text[0] = displayText;
            text[1] = getText(METADATA_KEY_DISPLAY_SUBTITLE);
            text[2] = getText(METADATA_KEY_DISPLAY_DESCRIPTION);
        } else {
            // Use whatever fields we can
            int textIndex = 0;
            int keyIndex = 0;
            while (textIndex < text.length && keyIndex < PREFERRED_DESCRIPTION_ORDER.length) {
                CharSequence next = getText(PREFERRED_DESCRIPTION_ORDER[keyIndex++]);
                if (!TextUtils.isEmpty(next)) {
                    // Fill in the next empty bit of text
                    text[textIndex++] = next;
                }
            }
        }

        // Get the best art bitmap we can find
        for (int i = 0; i < PREFERRED_BITMAP_ORDER.length; i++) {
            Bitmap next = getBitmap(PREFERRED_BITMAP_ORDER[i]);
@@ -658,6 +638,19 @@ public final class MediaMetadata implements Parcelable {
        return mDescription;
    }

    /**
     * Returns a description string of this metadata.
     *
     * Note: this is for MediaSession.setMetadata() to get the description of the
     * metadata, and it matches the previous beahvior as MediaDescription#toString()
     *
     * @hide
     */
    public String getDescriptionString() {
        CharSequence[] text = getTitleSubtitleAndDescription();
        return text[0] + ", " + text[1] + ", " + text[2];
    }

    /**
     * Helper for getting the String key used by {@link MediaMetadata} from the
     * integer key that {@link MediaMetadataEditor} uses.
@@ -750,6 +743,35 @@ public final class MediaMetadata implements Parcelable {
        return hashCode;
    }

    /**
     * Returns title, subtitle, description in this metadata
     */
    @NonNull @Size(3)
    private CharSequence[] getTitleSubtitleAndDescription() {
        CharSequence[] text = new CharSequence[3];
        // First handle the case where display data is set already
        CharSequence displayText = getText(METADATA_KEY_DISPLAY_TITLE);
        if (!TextUtils.isEmpty(displayText)) {
            // If they have a display title use only display data, otherwise use
            // our best bets
            text[0] = displayText;
            text[1] = getText(METADATA_KEY_DISPLAY_SUBTITLE);
            text[2] = getText(METADATA_KEY_DISPLAY_DESCRIPTION);
        } else {
            // Use whatever fields we can
            int textIndex = 0;
            int keyIndex = 0;
            while (textIndex < text.length && keyIndex < PREFERRED_DESCRIPTION_ORDER.length) {
                CharSequence next = getText(PREFERRED_DESCRIPTION_ORDER[keyIndex++]);
                if (!TextUtils.isEmpty(next)) {
                    // Fill in the next empty bit of text
                    text[textIndex++] = next;
                }
            }
        }
        return text;
    }

    /**
     * Use to build MediaMetadata objects. The system defined metadata keys must
     * use the appropriate data type.
+2 −2
Original line number Diff line number Diff line
@@ -506,7 +506,7 @@ public final class MediaSession {
    public void setMetadata(@Nullable MediaMetadata metadata) {
        long duration = -1;
        int fields = 0;
        MediaDescription description = null;
        String description = null;
        if (metadata != null) {
            metadata = new MediaMetadata.Builder(metadata)
                    .setBitmapDimensionLimit(mMaxBitmapSize)
@@ -515,7 +515,7 @@ public final class MediaSession {
                duration = metadata.getLong(MediaMetadata.METADATA_KEY_DURATION);
            }
            fields = metadata.size();
            description = metadata.getDescription();
            description = metadata.getDescriptionString();
        }
        String metadataDescription = "size=" + fields + ", description=" + description;