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
diff --git a/core/res/res/values/config.xml b/core/res/res/values/config.xml
index cbdb256..6e635f3 100644
--- a/core/res/res/values/config.xml
+++ b/core/res/res/values/config.xml
@@ -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>
diff --git a/core/res/res/values/symbols.xml b/core/res/res/values/symbols.xml
index b6cf81e..e3ea2d2 100644
--- a/core/res/res/values/symbols.xml
+++ b/core/res/res/values/symbols.xml
@@ -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" />
diff --git a/media/java/android/media/MediaMetadata.java b/media/java/android/media/MediaMetadata.java
index 924078c..754da0e 100644
--- a/media/java/android/media/MediaMetadata.java
+++ b/media/java/android/media/MediaMetadata.java
@@ -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.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;
/**
@@ -569,6 +565,29 @@
}
/**
+ * 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
* be one of the following:
@@ -707,6 +726,10 @@
* <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 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);
+ }
}
}
diff --git a/media/java/android/media/session/MediaSession.java b/media/java/android/media/session/MediaSession.java
index 973527f..e13f008 100644
--- a/media/java/android/media/session/MediaSession.java
+++ b/media/java/android/media/session/MediaSession.java
@@ -104,6 +104,7 @@
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 @@
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 @@
* @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) {