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

Commit b9c87fb4 authored by RoboErik's avatar RoboErik
Browse files

Scale bitmaps in metadata sent to the system

This scales down any MediaMetadata bitmaps that are sent to the system
through MediaSession.

bug:18114918
bug:18084448
Change-Id: Ib7b040cd8245108ad2dd56afe2499290d2b49f51
parent e6a1e993
Loading
Loading
Loading
Loading
+4 −0
Original line number Diff line number Diff line
@@ -1942,4 +1942,8 @@
    <string-array translatable="false" name="config_sms_convert_destination_number_support">
        <item>false</item>
    </string-array>

    <!-- The maximum bitmap size that can be written to a MediaMetadata object. This value
         is the max width/height allowed in dips.-->
    <dimen name="config_mediaMetadataBitmapMaxSize">320dp</dimen>
</resources>
+2 −0
Original line number Diff line number Diff line
@@ -1805,6 +1805,8 @@
  <java-symbol type="color" name="notification_progress_background_color" />
  <java-symbol type="id" name="media_actions" />

  <java-symbol type="dimen" name="config_mediaMetadataBitmapMaxSize" />

    <!-- From SystemUI -->
  <java-symbol type="anim" name="push_down_in" />
  <java-symbol type="anim" name="push_down_out" />
+38 −5
Original line number Diff line number Diff line
@@ -16,8 +16,6 @@
package android.media;

import android.annotation.NonNull;
import android.annotation.Nullable;
import android.content.ContentProviderClient;
import android.content.ContentResolver;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
@@ -25,16 +23,14 @@ import android.media.browse.MediaBrowser;
import android.media.session.MediaController;
import android.net.Uri;
import android.os.Bundle;
import android.os.CancellationSignal;
import android.os.OperationCanceledException;
import android.os.Parcel;
import android.os.Parcelable;
import android.text.TextUtils;
import android.util.ArrayMap;
import android.util.Log;
import android.util.Size;
import android.util.SparseArray;

import java.util.ArrayList;
import java.util.Set;

/**
@@ -568,6 +564,29 @@ public final class MediaMetadata implements Parcelable {
            mBundle = new Bundle(source.mBundle);
        }

        /**
         * Create a Builder using a {@link MediaMetadata} instance to set
         * initial values, but replace bitmaps with a scaled down copy if they
         * are larger than maxBitmapSize.
         *
         * @param source The original metadata to copy.
         * @param maxBitmapSize The maximum height/width for bitmaps contained
         *            in the metadata.
         * @hide
         */
        public Builder(MediaMetadata source, int maxBitmapSize) {
            this(source);
            for (String key : mBundle.keySet()) {
                Object value = mBundle.get(key);
                if (value != null && value instanceof Bitmap) {
                    Bitmap bmp = (Bitmap) value;
                    if (bmp.getHeight() > maxBitmapSize || bmp.getWidth() > maxBitmapSize) {
                        putBitmap(key, scaleBitmap(bmp, maxBitmapSize));
                    }
                }
            }
        }

        /**
         * Put a CharSequence value into the metadata. Custom keys may be used,
         * but if the METADATA_KEYs defined in this class are used they may only
@@ -707,6 +726,10 @@ public final class MediaMetadata implements Parcelable {
         * <li>{@link #METADATA_KEY_ALBUM_ART}</li>
         * <li>{@link #METADATA_KEY_DISPLAY_ICON}</li>
         * </ul>
         * <p>
         * Large bitmaps may be scaled down by the system. To pass full
         * resolution images {@link Uri Uris} should be used with
         * {@link #putString}.
         *
         * @param key The key for referencing this value
         * @param value The Bitmap to store
@@ -731,5 +754,15 @@ public final class MediaMetadata implements Parcelable {
        public MediaMetadata build() {
            return new MediaMetadata(mBundle);
        }

        private Bitmap scaleBitmap(Bitmap bmp, int maxSize) {
            float maxSizeF = maxSize;
            float widthScale = maxSizeF / bmp.getWidth();
            float heightScale = maxSizeF / bmp.getHeight();
            float scale = Math.min(widthScale, heightScale);
            int height = (int) (bmp.getHeight() * scale);
            int width = (int) (bmp.getWidth() * scale);
            return Bitmap.createScaledBitmap(bmp, width, height, true);
        }
    }
}
+4 −0
Original line number Diff line number Diff line
@@ -104,6 +104,7 @@ public final class MediaSession {
    public @interface SessionFlags { }

    private final Object mLock = new Object();
    private final int mMaxBitmapSize;

    private final MediaSession.Token mSessionToken;
    private final MediaController mController;
@@ -147,6 +148,8 @@ public final class MediaSession {
        if (TextUtils.isEmpty(tag)) {
            throw new IllegalArgumentException("tag cannot be null or empty");
        }
        mMaxBitmapSize = context.getResources().getDimensionPixelSize(
                com.android.internal.R.dimen.config_mediaMetadataBitmapMaxSize);
        mCbStub = new CallbackStub(this);
        MediaSessionManager manager = (MediaSessionManager) context
                .getSystemService(Context.MEDIA_SESSION_SERVICE);
@@ -409,6 +412,7 @@ public final class MediaSession {
     * @param metadata The new metadata
     */
    public void setMetadata(@Nullable MediaMetadata metadata) {
        metadata = (new MediaMetadata.Builder(metadata, mMaxBitmapSize)).build();
        try {
            mBinder.setMetadata(metadata);
        } catch (RemoteException e) {