From 0732c3ae8ef0a48f3a31da458e5d8d98848776d4 Mon Sep 17 00:00:00 2001 From: Hyundo Moon Date: Fri, 11 Sep 2020 17:55:23 +0900 Subject: [PATCH] Add a public method MediaMetadata.Builder#setBitmapDimensionLimit() This CL replaces the copy constructor MediaMetadata(MediaMetadata source, int maxBitmapSize) with the new public method Builder#setBitmapDimensionLimit(). Also added a getter getBitmapDimensionLimit(). Bug: 168215839 Test: m update-api -j; Ran MediaMetdataTest Change-Id: I6844fcf01c4ee98c99bd0dc880b96f73989dbd3c --- api/current.txt | 2 + api/module-lib-current.txt | 4 - media/java/android/media/MediaMetadata.java | 94 ++++++++++++------- .../android/media/session/MediaSession.java | 4 +- non-updatable-api/current.txt | 2 + non-updatable-api/module-lib-current.txt | 4 - 6 files changed, 67 insertions(+), 43 deletions(-) diff --git a/api/current.txt b/api/current.txt index b93ffe3bc745d..b5bdcf4c212a4 100644 --- a/api/current.txt +++ b/api/current.txt @@ -26359,6 +26359,7 @@ package android.media { method public boolean containsKey(String); method public int describeContents(); method public android.graphics.Bitmap getBitmap(String); + method @IntRange(from=0) public int getBitmapDimensionLimit(); method @NonNull public android.media.MediaDescription getDescription(); method public long getLong(String); method public android.media.Rating getRating(String); @@ -26408,6 +26409,7 @@ package android.media { method public android.media.MediaMetadata.Builder putRating(String, android.media.Rating); method public android.media.MediaMetadata.Builder putString(String, String); method public android.media.MediaMetadata.Builder putText(String, CharSequence); + method @NonNull public android.media.MediaMetadata.Builder setBitmapDimensionLimit(int); } @Deprecated public abstract class MediaMetadataEditor { diff --git a/api/module-lib-current.txt b/api/module-lib-current.txt index eb2f26448a699..4a7c1210267ef 100644 --- a/api/module-lib-current.txt +++ b/api/module-lib-current.txt @@ -53,10 +53,6 @@ package android.media { field public static final int FLAG_FROM_KEY = 4096; // 0x1000 } - public static final class MediaMetadata.Builder { - ctor public MediaMetadata.Builder(@NonNull android.media.MediaMetadata, @IntRange(from=1) int); - } - public final class MediaParceledListSlice implements android.os.Parcelable { ctor public MediaParceledListSlice(@NonNull java.util.List); method public int describeContents(); diff --git a/media/java/android/media/MediaMetadata.java b/media/java/android/media/MediaMetadata.java index 523a072b957aa..dbf4ad0b14f9b 100644 --- a/media/java/android/media/MediaMetadata.java +++ b/media/java/android/media/MediaMetadata.java @@ -18,13 +18,13 @@ package android.media; import android.annotation.IntRange; import android.annotation.NonNull; import android.annotation.StringDef; -import android.annotation.SystemApi; import android.compat.annotation.UnsupportedAppUsage; import android.content.ContentResolver; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.media.browse.MediaBrowser; import android.media.session.MediaController; +import android.media.session.MediaSession; import android.net.Uri; import android.os.Bundle; import android.os.Parcel; @@ -417,14 +417,17 @@ public final class MediaMetadata implements Parcelable { } private final Bundle mBundle; + private final int mBitmapDimensionLimit; private MediaDescription mDescription; - private MediaMetadata(Bundle bundle) { + private MediaMetadata(Bundle bundle, int bitmapDimensionLimit) { mBundle = new Bundle(bundle); + mBitmapDimensionLimit = bitmapDimensionLimit; } private MediaMetadata(Parcel in) { mBundle = in.readBundle(); + mBitmapDimensionLimit = Math.max(in.readInt(), 0); } /** @@ -513,6 +516,22 @@ public final class MediaMetadata implements Parcelable { return bmp; } + /** + * Gets the width/height limit (in pixels) for the bitmaps when this metadata was created. + * This method returns a positive value or zero. + *

+ * If it returns a positive value, then all the bitmaps in this metadata has width/height + * not greater than this limit. Bitmaps may have been scaled down according to the limit. + *

+ * If it returns zero, then no scaling down was applied to the bitmaps when this metadata + * was created. + * + * @see Builder#setBitmapDimensionLimit(int) + */ + public @IntRange(from = 0) int getBitmapDimensionLimit() { + return mBitmapDimensionLimit; + } + @Override public int describeContents() { return 0; @@ -521,6 +540,7 @@ public final class MediaMetadata implements Parcelable { @Override public void writeToParcel(Parcel dest, int flags) { dest.writeBundle(mBundle); + dest.writeInt(mBitmapDimensionLimit); } /** @@ -718,6 +738,7 @@ public final class MediaMetadata implements Parcelable { */ public static final class Builder { private final Bundle mBundle; + private int mBitmapDimensionLimit; /** * Create an empty Builder. Any field that should be included in the @@ -736,30 +757,7 @@ public final class MediaMetadata implements Parcelable { */ public Builder(MediaMetadata source) { 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 their width (or height) - * is larger than maxBitmapSize. - * - * @param source The original metadata to copy. - * @param maxBitmapSize The maximum height/width for bitmaps contained - * in the metadata. - * @hide - */ - @SystemApi(client = SystemApi.Client.MODULE_LIBRARIES) - public Builder(@NonNull MediaMetadata source, @IntRange(from = 1) 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)); - } - } - } + mBitmapDimensionLimit = source.mBitmapDimensionLimit; } /** @@ -902,9 +900,9 @@ public final class MediaMetadata implements Parcelable { *

  • {@link #METADATA_KEY_DISPLAY_ICON}
  • * *

    - * Large bitmaps may be scaled down by the system when - * {@link android.media.session.MediaSession#setMetadata} is called. - * To pass full resolution images {@link Uri Uris} should be used with + * Large bitmaps may be scaled down by the system with + * {@link Builder#setBitmapDimensionLimit(int)} when {@link MediaSession#setMetadata} + * is called. To pass full resolution images {@link Uri Uris} should be used with * {@link #putString}. * * @param key The key for referencing this value @@ -922,19 +920,47 @@ public final class MediaMetadata implements Parcelable { return this; } + /** + * Sets the maximum width/height (in pixels) for the bitmaps in the metadata. + * Bitmaps will be replaced with scaled down copies if their width (or height) is + * larger than {@code bitmapDimensionLimit}. + *

    + * In order to unset the limit, pass zero as {@code bitmapDimensionLimit}. + * + * @param bitmapDimensionLimit The maximum width/height (in pixels) for bitmaps + * contained in the metadata. Pass {@code 0} to unset the limit. + */ + @NonNull + public Builder setBitmapDimensionLimit(int bitmapDimensionLimit) { + mBitmapDimensionLimit = Math.max(bitmapDimensionLimit, 0); + return this; + } + /** * Creates a {@link MediaMetadata} instance with the specified fields. * * @return The new MediaMetadata instance */ public MediaMetadata build() { - return new MediaMetadata(mBundle); + if (mBitmapDimensionLimit > 0) { + for (String key : mBundle.keySet()) { + Object value = mBundle.get(key); + if (value instanceof Bitmap) { + Bitmap bmp = (Bitmap) value; + if (bmp.getHeight() > mBitmapDimensionLimit + || bmp.getWidth() > mBitmapDimensionLimit) { + putBitmap(key, scaleBitmap(bmp, mBitmapDimensionLimit)); + } + } + } + } + return new MediaMetadata(mBundle, mBitmapDimensionLimit); } - private Bitmap scaleBitmap(Bitmap bmp, int maxSize) { - float maxSizeF = maxSize; - float widthScale = maxSizeF / bmp.getWidth(); - float heightScale = maxSizeF / bmp.getHeight(); + private Bitmap scaleBitmap(Bitmap bmp, int maxDimension) { + float maxDimensionF = maxDimension; + float widthScale = maxDimensionF / bmp.getWidth(); + float heightScale = maxDimensionF / bmp.getHeight(); float scale = Math.min(widthScale, heightScale); int height = (int) (bmp.getHeight() * scale); int width = (int) (bmp.getWidth() * scale); diff --git a/media/java/android/media/session/MediaSession.java b/media/java/android/media/session/MediaSession.java index 624607b61b8f4..e17e069d61d0e 100644 --- a/media/java/android/media/session/MediaSession.java +++ b/media/java/android/media/session/MediaSession.java @@ -464,7 +464,9 @@ public final class MediaSession { int fields = 0; MediaDescription description = null; if (metadata != null) { - metadata = (new MediaMetadata.Builder(metadata, mMaxBitmapSize)).build(); + metadata = new MediaMetadata.Builder(metadata) + .setBitmapDimensionLimit(mMaxBitmapSize) + .build(); if (metadata.containsKey(MediaMetadata.METADATA_KEY_DURATION)) { duration = metadata.getLong(MediaMetadata.METADATA_KEY_DURATION); } diff --git a/non-updatable-api/current.txt b/non-updatable-api/current.txt index dd06ae032979c..34829564497d9 100644 --- a/non-updatable-api/current.txt +++ b/non-updatable-api/current.txt @@ -26317,6 +26317,7 @@ package android.media { method public boolean containsKey(String); method public int describeContents(); method public android.graphics.Bitmap getBitmap(String); + method @IntRange(from=0) public int getBitmapDimensionLimit(); method @NonNull public android.media.MediaDescription getDescription(); method public long getLong(String); method public android.media.Rating getRating(String); @@ -26366,6 +26367,7 @@ package android.media { method public android.media.MediaMetadata.Builder putRating(String, android.media.Rating); method public android.media.MediaMetadata.Builder putString(String, String); method public android.media.MediaMetadata.Builder putText(String, CharSequence); + method @NonNull public android.media.MediaMetadata.Builder setBitmapDimensionLimit(int); } @Deprecated public abstract class MediaMetadataEditor { diff --git a/non-updatable-api/module-lib-current.txt b/non-updatable-api/module-lib-current.txt index 44b0ab8de66aa..11cd6a93910e2 100644 --- a/non-updatable-api/module-lib-current.txt +++ b/non-updatable-api/module-lib-current.txt @@ -53,10 +53,6 @@ package android.media { field public static final int FLAG_FROM_KEY = 4096; // 0x1000 } - public static final class MediaMetadata.Builder { - ctor public MediaMetadata.Builder(@NonNull android.media.MediaMetadata, @IntRange(from=1) int); - } - } package android.media.session {