Skip to content

Commit

Permalink
Load bitmaps for MediaBrowserCompat.
Browse files Browse the repository at this point in the history
* Transforms the `ListenableFuture<LibraryResult<MediaItem>>` and `ListenableFuture<LibraryResult<List<MediaItem>>>` to `ListenableFuture<MediaBrowserCompat.MediaItem>` and `ListenableFuture<List<MediaBrowserCompat.MediaItem>>`, and the result will be sent out when `ListenableFuture` the `MediaBrowserCompat.MediaItem` (or the list of it) is fulfilled.
* Add `artworkData` to the tests in `MediaBrowserCompatWithMediaLibraryServiceTest`.

PiperOrigin-RevId: 489205547
  • Loading branch information
tianyif authored and microkatz committed Nov 22, 2022
1 parent 6e73fc5 commit 4ce171a
Show file tree
Hide file tree
Showing 5 changed files with 225 additions and 76 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import static androidx.media3.session.MediaUtils.TRANSACTION_SIZE_LIMIT_IN_BYTES;

import android.annotation.SuppressLint;
import android.graphics.Bitmap;
import android.os.BadParcelableException;
import android.os.Bundle;
import android.os.RemoteException;
Expand All @@ -37,21 +38,27 @@
import androidx.media.MediaBrowserServiceCompat;
import androidx.media.MediaSessionManager.RemoteUserInfo;
import androidx.media3.common.MediaItem;
import androidx.media3.common.MediaMetadata;
import androidx.media3.common.util.ConditionVariable;
import androidx.media3.common.util.Log;
import androidx.media3.common.util.Util;
import androidx.media3.session.MediaLibraryService.LibraryParams;
import androidx.media3.session.MediaSession.ControllerCb;
import androidx.media3.session.MediaSession.ControllerInfo;
import com.google.common.collect.ImmutableList;
import com.google.common.util.concurrent.AsyncFunction;
import com.google.common.util.concurrent.Futures;
import com.google.common.util.concurrent.ListenableFuture;
import com.google.common.util.concurrent.MoreExecutors;
import com.google.common.util.concurrent.SettableFuture;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.CancellationException;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.atomic.AtomicReference;
import org.checkerframework.checker.nullness.compatqual.NullableType;

/**
* Implementation of {@link MediaBrowserServiceCompat} for interoperability between {@link
Expand Down Expand Up @@ -218,7 +225,11 @@ public void onLoadChildren(
ListenableFuture<LibraryResult<ImmutableList<MediaItem>>> future =
librarySessionImpl.onGetChildrenOnHandler(
controller, parentId, page, pageSize, params);
sendLibraryResultWithMediaItemsWhenReady(result, future);
ListenableFuture<@NullableType List<MediaBrowserCompat.MediaItem>>
browserItemsFuture =
Util.transformFutureAsync(
future, createMediaItemsToBrowserItemsAsyncFunction());
sendLibraryResultWithMediaItemsWhenReady(result, browserItemsFuture);
return;
}
// Cannot distinguish onLoadChildren() why it's called either by
Expand All @@ -236,7 +247,9 @@ public void onLoadChildren(
/* page= */ 0,
/* pageSize= */ Integer.MAX_VALUE,
/* params= */ null);
sendLibraryResultWithMediaItemsWhenReady(result, future);
ListenableFuture<@NullableType List<MediaBrowserCompat.MediaItem>> browserItemsFuture =
Util.transformFutureAsync(future, createMediaItemsToBrowserItemsAsyncFunction());
sendLibraryResultWithMediaItemsWhenReady(result, browserItemsFuture);
});
}

Expand Down Expand Up @@ -264,7 +277,9 @@ public void onLoadItem(String itemId, Result<MediaBrowserCompat.MediaItem> resul
}
ListenableFuture<LibraryResult<MediaItem>> future =
librarySessionImpl.onGetItemOnHandler(controller, itemId);
sendLibraryResultWithMediaItemWhenReady(result, future);
ListenableFuture<MediaBrowserCompat.@NullableType MediaItem> browserItemFuture =
Util.transformFutureAsync(future, createMediaItemToBrowserItemAsyncFunction());
sendLibraryResultWithMediaItemWhenReady(result, browserItemFuture);
});
}

Expand Down Expand Up @@ -362,17 +377,12 @@ private static void sendCustomActionResultWhenReady(

private static void sendLibraryResultWithMediaItemWhenReady(
Result<MediaBrowserCompat.MediaItem> result,
ListenableFuture<LibraryResult<MediaItem>> future) {
ListenableFuture<MediaBrowserCompat.@NullableType MediaItem> future) {
future.addListener(
() -> {
try {
LibraryResult<MediaItem> libraryResult =
checkNotNull(future.get(), "LibraryResult must not be null");
if (libraryResult.resultCode != RESULT_SUCCESS || libraryResult.value == null) {
result.sendResult(/* result= */ null);
} else {
result.sendResult(MediaUtils.convertToBrowserItem(libraryResult.value));
}
MediaBrowserCompat.MediaItem mediaItem = future.get();
result.sendResult(mediaItem);
} catch (CancellationException | ExecutionException | InterruptedException unused) {
result.sendError(/* extras= */ null);
}
Expand All @@ -382,27 +392,146 @@ private static void sendLibraryResultWithMediaItemWhenReady(

private static void sendLibraryResultWithMediaItemsWhenReady(
Result<List<MediaBrowserCompat.MediaItem>> result,
ListenableFuture<LibraryResult<ImmutableList<MediaItem>>> future) {
ListenableFuture<@NullableType List<MediaBrowserCompat.MediaItem>> future) {
future.addListener(
() -> {
try {
LibraryResult<ImmutableList<MediaItem>> libraryResult =
checkNotNull(future.get(), "LibraryResult must not be null");
if (libraryResult.resultCode != RESULT_SUCCESS || libraryResult.value == null) {
result.sendResult(/* result= */ null);
} else {
result.sendResult(
MediaUtils.truncateListBySize(
MediaUtils.convertToBrowserItemList(libraryResult.value),
TRANSACTION_SIZE_LIMIT_IN_BYTES));
}
List<MediaBrowserCompat.MediaItem> mediaItems = future.get();
result.sendResult(
(mediaItems == null)
? null
: MediaUtils.truncateListBySize(mediaItems, TRANSACTION_SIZE_LIMIT_IN_BYTES));
} catch (CancellationException | ExecutionException | InterruptedException unused) {
result.sendError(/* extras= */ null);
}
},
MoreExecutors.directExecutor());
}

private AsyncFunction<
LibraryResult<ImmutableList<MediaItem>>, @NullableType List<MediaBrowserCompat.MediaItem>>
createMediaItemsToBrowserItemsAsyncFunction() {
return result -> {
checkNotNull(result, "LibraryResult must not be null");
SettableFuture<@NullableType List<MediaBrowserCompat.MediaItem>> outputFuture =
SettableFuture.create();
if (result.resultCode != RESULT_SUCCESS || result.value == null) {
outputFuture.set(null);
return outputFuture;
}

ImmutableList<MediaItem> mediaItems = result.value;
if (mediaItems.isEmpty()) {
outputFuture.set(new ArrayList<>());
return outputFuture;
}

List<@NullableType ListenableFuture<Bitmap>> bitmapFutures = new ArrayList<>();
outputFuture.addListener(
() -> {
if (outputFuture.isCancelled()) {
cancelAllFutures(bitmapFutures);
}
},
MoreExecutors.directExecutor());

final AtomicInteger resultCount = new AtomicInteger(0);
Runnable handleBitmapFuturesTask =
() -> {
int completedBitmapFutureCount = resultCount.incrementAndGet();
if (completedBitmapFutureCount == mediaItems.size()) {
handleBitmapFuturesAllCompletedAndSetOutputFuture(
bitmapFutures, mediaItems, outputFuture);
}
};

for (int i = 0; i < mediaItems.size(); i++) {
MediaItem mediaItem = mediaItems.get(i);
MediaMetadata metadata = mediaItem.mediaMetadata;
if (metadata.artworkData == null) {
bitmapFutures.add(null);
handleBitmapFuturesTask.run();
} else {
ListenableFuture<Bitmap> bitmapFuture =
librarySessionImpl.getBitmapLoader().decodeBitmap(metadata.artworkData);
bitmapFutures.add(bitmapFuture);
bitmapFuture.addListener(handleBitmapFuturesTask, MoreExecutors.directExecutor());
}
}
return outputFuture;
};
}

private void handleBitmapFuturesAllCompletedAndSetOutputFuture(
List<@NullableType ListenableFuture<Bitmap>> bitmapFutures,
List<MediaItem> mediaItems,
SettableFuture<@NullableType List<MediaBrowserCompat.MediaItem>> outputFuture) {
List<MediaBrowserCompat.MediaItem> outputMediaItems = new ArrayList<>();
for (int i = 0; i < bitmapFutures.size(); i++) {
@Nullable ListenableFuture<Bitmap> future = bitmapFutures.get(i);
@Nullable Bitmap bitmap = null;
if (future != null) {
try {
bitmap = Futures.getDone(future);
} catch (CancellationException | ExecutionException e) {
Log.d(TAG, "Failed to get bitmap");
}
}
outputMediaItems.add(MediaUtils.convertToBrowserItem(mediaItems.get(i), bitmap));
}
outputFuture.set(outputMediaItems);
}

private static <T> void cancelAllFutures(List<@NullableType ListenableFuture<T>> futures) {
for (int i = 0; i < futures.size(); i++) {
if (futures.get(i) != null) {
futures.get(i).cancel(/* mayInterruptIfRunning= */ false);
}
}
}

private AsyncFunction<LibraryResult<MediaItem>, MediaBrowserCompat.@NullableType MediaItem>
createMediaItemToBrowserItemAsyncFunction() {
return result -> {
checkNotNull(result, "LibraryResult must not be null");
SettableFuture<MediaBrowserCompat.@NullableType MediaItem> outputFuture =
SettableFuture.create();
if (result.resultCode != RESULT_SUCCESS || result.value == null) {
outputFuture.set(null);
return outputFuture;
}

MediaItem mediaItem = result.value;
MediaMetadata metadata = mediaItem.mediaMetadata;
if (metadata.artworkData == null) {
outputFuture.set(MediaUtils.convertToBrowserItem(mediaItem, /* artworkBitmap= */ null));
return outputFuture;
}

ListenableFuture<Bitmap> bitmapFuture =
librarySessionImpl.getBitmapLoader().decodeBitmap(metadata.artworkData);
outputFuture.addListener(
() -> {
if (outputFuture.isCancelled()) {
bitmapFuture.cancel(/* mayInterruptIfRunning= */ false);
}
},
MoreExecutors.directExecutor());
bitmapFuture.addListener(
() -> {
@Nullable Bitmap bitmap = null;
try {
bitmap = Futures.getDone(bitmapFuture);
} catch (CancellationException | ExecutionException e) {
Log.d(TAG, "failed to get bitmap");
}
outputFuture.set(MediaUtils.convertToBrowserItem(mediaItem, bitmap));
},
MoreExecutors.directExecutor());
return outputFuture;
};
}

private static <T> void ignoreFuture(Future<T> unused) {
// no-op
}
Expand Down Expand Up @@ -504,7 +633,9 @@ public void onSearchResultChanged(
ListenableFuture<LibraryResult<ImmutableList<MediaItem>>> future =
librarySessionImpl.onGetSearchResultOnHandler(
request.controller, request.query, page, pageSize, libraryParams);
sendLibraryResultWithMediaItemsWhenReady(request.result, future);
ListenableFuture<@NullableType List<MediaBrowserCompat.MediaItem>> mediaItemsFuture =
Util.transformFutureAsync(future, createMediaItemsToBrowserItemsAsyncFunction());
sendLibraryResultWithMediaItemsWhenReady(request.result, mediaItemsFuture);
}
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -136,9 +136,9 @@ public static PlaybackException convertToPlaybackException(
errorMessage, /* cause= */ null, PlaybackException.ERROR_CODE_REMOTE_ERROR);
}

/** Converts a {@link MediaItem} to a {@link MediaBrowserCompat.MediaItem}. */
public static MediaBrowserCompat.MediaItem convertToBrowserItem(MediaItem item) {
MediaDescriptionCompat description = convertToMediaDescriptionCompat(item);
public static MediaBrowserCompat.MediaItem convertToBrowserItem(
MediaItem item, @Nullable Bitmap artworkBitmap) {
MediaDescriptionCompat description = convertToMediaDescriptionCompat(item, artworkBitmap);
MediaMetadata metadata = item.mediaMetadata;
int flags = 0;
if (metadata.folderType != null && metadata.folderType != MediaMetadata.FOLDER_TYPE_NONE) {
Expand All @@ -150,15 +150,6 @@ public static MediaBrowserCompat.MediaItem convertToBrowserItem(MediaItem item)
return new MediaBrowserCompat.MediaItem(description, flags);
}

/** Converts a list of {@link MediaItem} to a list of {@link MediaBrowserCompat.MediaItem}. */
public static List<MediaBrowserCompat.MediaItem> convertToBrowserItemList(List<MediaItem> items) {
List<MediaBrowserCompat.MediaItem> result = new ArrayList<>();
for (int i = 0; i < items.size(); i++) {
result.add(convertToBrowserItem(items.get(i)));
}
return result;
}

/** Converts a {@link MediaBrowserCompat.MediaItem} to a {@link MediaItem}. */
public static MediaItem convertToMediaItem(MediaBrowserCompat.MediaItem item) {
return convertToMediaItem(item.getDescription(), item.isBrowsable(), item.isPlayable());
Expand Down Expand Up @@ -320,16 +311,32 @@ public static <T extends Parcelable> List<T> truncateListBySize(
return result;
}

/** Converts a {@link MediaItem} to a {@link MediaDescriptionCompat}. */
/**
* Converts a {@link MediaItem} to a {@link MediaDescriptionCompat}.
*
* @deprecated Use {@link #convertToMediaDescriptionCompat(MediaItem, Bitmap)} instead.
*/
@Deprecated
public static MediaDescriptionCompat convertToMediaDescriptionCompat(MediaItem item) {
MediaMetadata metadata = item.mediaMetadata;
@Nullable Bitmap artworkBitmap = null;
if (metadata.artworkData != null) {
artworkBitmap =
BitmapFactory.decodeByteArray(metadata.artworkData, 0, metadata.artworkData.length);
}

return convertToMediaDescriptionCompat(item, artworkBitmap);
}

/** Converts a {@link MediaItem} to a {@link MediaDescriptionCompat} */
public static MediaDescriptionCompat convertToMediaDescriptionCompat(
MediaItem item, @Nullable Bitmap artworkBitmap) {
MediaDescriptionCompat.Builder builder =
new MediaDescriptionCompat.Builder()
.setMediaId(item.mediaId.equals(MediaItem.DEFAULT_MEDIA_ID) ? null : item.mediaId);
MediaMetadata metadata = item.mediaMetadata;
if (metadata.artworkData != null) {
Bitmap artwork =
BitmapFactory.decodeByteArray(metadata.artworkData, 0, metadata.artworkData.length);
builder.setIconBitmap(artwork);
if (artworkBitmap != null) {
builder.setIconBitmap(artworkBitmap);
}
@Nullable Bundle extras = metadata.extras;
if (metadata.folderType != null && metadata.folderType != MediaMetadata.FOLDER_TYPE_NONE) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,7 @@ public void onItemLoaded(MediaItem item) {
assertThat(latch.await(TIMEOUT_MS, MILLISECONDS)).isTrue();
assertThat(itemRef.get().getMediaId()).isEqualTo(mediaId);
assertThat(itemRef.get().isBrowsable()).isTrue();
assertThat(itemRef.get().getDescription().getIconBitmap()).isNotNull();
}

@Test
Expand All @@ -151,6 +152,7 @@ public void onItemLoaded(MediaItem item) {
assertThat(latch.await(TIMEOUT_MS, MILLISECONDS)).isTrue();
assertThat(itemRef.get().getMediaId()).isEqualTo(mediaId);
assertThat(itemRef.get().isPlayable()).isTrue();
assertThat(itemRef.get().getDescription().getIconBitmap()).isNotNull();
}

@Test
Expand Down Expand Up @@ -181,6 +183,7 @@ public void onItemLoaded(MediaItem item) {
BundleSubject.assertThat(description.getExtras())
.string(METADATA_EXTRA_KEY)
.isEqualTo(METADATA_EXTRA_VALUE);
assertThat(description.getIconBitmap()).isNotNull();
}

@Test
Expand Down Expand Up @@ -245,6 +248,7 @@ public void onChildrenLoaded(String parentId, List<MediaItem> children, Bundle o
EXTRAS_KEY_COMPLETION_STATUS,
/* defaultValue= */ EXTRAS_VALUE_COMPLETION_STATUS_PARTIALLY_PLAYED + 1))
.isEqualTo(EXTRAS_VALUE_COMPLETION_STATUS_PARTIALLY_PLAYED);
assertThat(mediaItem.getDescription().getIconBitmap()).isNotNull();
}
}

Expand Down Expand Up @@ -311,6 +315,7 @@ public void onChildrenLoaded(String parentId, List<MediaItem> children, Bundle o
int relativeIndex = originalIndex - fromIndex;
assertThat(children.get(relativeIndex).getMediaId())
.isEqualTo(GET_CHILDREN_RESULT.get(originalIndex));
assertThat(children.get(relativeIndex).getDescription().getIconBitmap()).isNotNull();
}
latch.countDown();
}
Expand Down
Loading

0 comments on commit 4ce171a

Please sign in to comment.