@@ -767,30 +767,8 @@ public Builder() {
767767 * @param source
768768 */
769769 public Builder (MediaMetadata source ) {
770- mBundle = new Bundle ();
770+ mBundle = new Bundle (source . mBundle );
771771 mBitmapDimensionLimit = source .mBitmapDimensionLimit ;
772- for (String key : source .mBundle .keySet ()) {
773- // there is no non-deprecated way for doing this AFAIK
774- //noinspection deprecation
775- Object value = source .mBundle .get (key );
776- if (value == null ) {
777- Log .w (TAG , "Builder: discarding key with null value: " + key );
778- continue ;
779- }
780- if (value instanceof String s ) {
781- putString (key , s );
782- } else if (value instanceof CharSequence cs ) {
783- putText (key , cs );
784- } else if (value instanceof Long l ) {
785- putLong (key , l .longValue ());
786- } else if (value instanceof Rating r ) {
787- putRating (key , r );
788- } else if (value instanceof Bitmap bm ) {
789- putBitmap (key , bm );
790- } else {
791- throw new IllegalStateException ("unexpected value type: " + value .getClass ());
792- }
793- }
794772 }
795773
796774 /**
@@ -827,7 +805,6 @@ public Builder putText(@TextKey String key, CharSequence value) {
827805 }
828806 }
829807 mBundle .putCharSequence (key , value );
830- removeIfTooLarge (key );
831808 return this ;
832809 }
833810
@@ -870,7 +847,6 @@ public Builder putString(@TextKey String key, String value) {
870847 }
871848 }
872849 mBundle .putCharSequence (key , value );
873- removeIfTooLarge (key );
874850 return this ;
875851 }
876852
@@ -898,7 +874,6 @@ public Builder putLong(@LongKey String key, long value) {
898874 }
899875 }
900876 mBundle .putLong (key , value );
901- removeIfTooLarge (key );
902877 return this ;
903878 }
904879
@@ -923,7 +898,6 @@ public Builder putRating(@RatingKey String key, Rating value) {
923898 }
924899 }
925900 mBundle .putParcelable (key , value );
926- removeIfTooLarge (key );
927901 return this ;
928902 }
929903
@@ -953,8 +927,7 @@ public Builder putBitmap(@BitmapKey String key, Bitmap value) {
953927 + " key cannot be used to put a Bitmap" );
954928 }
955929 }
956- mBundle .putParcelable (key , prepareBitmap (value , key ));
957- removeIfTooLarge (key );
930+ mBundle .putParcelable (key , value );
958931 return this ;
959932 }
960933
@@ -991,44 +964,21 @@ public MediaMetadata build() {
991964 for (String key : mBundle .keySet ()) {
992965 Object value = mBundle .get (key );
993966 if (value instanceof Bitmap bmp ) {
994- Bitmap preparedBmp = prepareBitmap (bmp , key );
995- if (preparedBmp != bmp ) {
996- putBitmap (key , preparedBmp );
967+ final Bitmap orig = bmp ;
968+ if (bmp .getHeight () > mBitmapDimensionLimit
969+ || bmp .getWidth () > mBitmapDimensionLimit ) {
970+ bmp = scaleBitmap (bmp , mBitmapDimensionLimit );
971+ }
972+ Bitmap sharedBmp = bmp .asShared ();
973+ if (orig != sharedBmp ) {
974+ putBitmap (key , sharedBmp );
997975 }
998976 }
999977 }
1000978 }
1001979 return new MediaMetadata (mBundle , mBitmapDimensionLimit );
1002980 }
1003981
1004- private void removeIfTooLarge (String key ) {
1005- Parcel parcel = Parcel .obtain ();
1006- try {
1007- mBundle .writeToParcel (parcel , 0 );
1008- if (parcel .dataSize () > 500 * 1024 ) {
1009- Log .e (TAG , "Builder: bundle ran out of space, dropping " + key );
1010- mBundle .remove (key );
1011- }
1012- } finally {
1013- parcel .recycle ();
1014- }
1015- }
1016-
1017- private Bitmap prepareBitmap (Bitmap bmp , String key ) {
1018- int origW = bmp .getWidth ();
1019- int origH = bmp .getHeight ();
1020- if (origW > mBitmapDimensionLimit || origH > mBitmapDimensionLimit ) {
1021- bmp = scaleBitmap (bmp , mBitmapDimensionLimit );
1022- Log .d (TAG , "resized bitmap " + key + " from " + origW + "x" + origH
1023- + " to " + bmp .getWidth () + "x" + bmp .getHeight ());
1024- }
1025- Bitmap shared = bmp .asShared ();
1026- if (bmp != shared ) {
1027- Log .d (TAG , "converted bitmap " + key + " to a shared bitmap" );
1028- }
1029- return shared ;
1030- }
1031-
1032982 private Bitmap scaleBitmap (Bitmap bmp , int maxDimension ) {
1033983 float maxDimensionF = maxDimension ;
1034984 float widthScale = maxDimensionF / bmp .getWidth ();
0 commit comments