Merge "Add a public method MediaMetadata.Builder#setBitmapDimensionLimit()"

This commit is contained in:
Hyundo Moon
2020-09-28 16:51:49 +00:00
committed by Android (Google) Code Review
6 changed files with 67 additions and 43 deletions

View File

@@ -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 {

View File

@@ -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<T extends android.os.Parcelable> implements android.os.Parcelable {
ctor public MediaParceledListSlice(@NonNull java.util.List<T>);
method public int describeContents();

View File

@@ -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.
* <p>
* 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.
* <p>
* 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 {
* <li>{@link #METADATA_KEY_DISPLAY_ICON}</li>
* </ul>
* <p>
* 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}.
* <p>
* 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);

View File

@@ -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);
}

View File

@@ -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 {

View File

@@ -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 {