Search in sources :

Example 1 with ItemCallback

use of android.support.v4.media.MediaBrowserCompat.ItemCallback in project media by androidx.

the class MediaBrowserImplLegacy method getItem.

@Override
public ListenableFuture<LibraryResult<MediaItem>> getItem(String mediaId) {
    if (!instance.isSessionCommandAvailable(SessionCommand.COMMAND_CODE_LIBRARY_GET_ITEM)) {
        return Futures.immediateFuture(LibraryResult.ofError(RESULT_ERROR_PERMISSION_DENIED));
    }
    MediaBrowserCompat browserCompat = getBrowserCompat();
    if (browserCompat == null) {
        return Futures.immediateFuture(LibraryResult.ofError(RESULT_ERROR_SESSION_DISCONNECTED));
    }
    SettableFuture<LibraryResult<MediaItem>> result = SettableFuture.create();
    browserCompat.getItem(mediaId, new ItemCallback() {

        @Override
        public void onItemLoaded(MediaBrowserCompat.MediaItem item) {
            if (item != null) {
                result.set(LibraryResult.ofItem(MediaUtils.convertToMediaItem(item), /* params= */
                null));
            } else {
                result.set(LibraryResult.ofError(RESULT_ERROR_BAD_VALUE));
            }
        }

        @Override
        public void onError(String itemId) {
            result.set(LibraryResult.ofError(RESULT_ERROR_UNKNOWN));
        }
    });
    return result;
}
Also used : MediaBrowserCompat(android.support.v4.media.MediaBrowserCompat) ItemCallback(android.support.v4.media.MediaBrowserCompat.ItemCallback)

Example 2 with ItemCallback

use of android.support.v4.media.MediaBrowserCompat.ItemCallback in project media by androidx.

the class MediaBrowserCompatWithMediaLibraryServiceTest method getItem_nullResult.

@Test
public void getItem_nullResult() throws InterruptedException {
    String mediaId = "random_media_id";
    connectAndWait();
    CountDownLatch latch = new CountDownLatch(1);
    browserCompat.getItem(mediaId, new ItemCallback() {

        @Override
        public void onItemLoaded(MediaItem item) {
            assertThat(item).isNull();
            latch.countDown();
        }

        @Override
        public void onError(String itemId) {
            assertWithMessage("").fail();
        }
    });
    assertThat(latch.await(TIMEOUT_MS, MILLISECONDS)).isTrue();
}
Also used : ItemCallback(android.support.v4.media.MediaBrowserCompat.ItemCallback) MediaItem(android.support.v4.media.MediaBrowserCompat.MediaItem) CountDownLatch(java.util.concurrent.CountDownLatch) LargeTest(androidx.test.filters.LargeTest) Test(org.junit.Test)

Example 3 with ItemCallback

use of android.support.v4.media.MediaBrowserCompat.ItemCallback in project media by androidx.

the class MediaBrowserCompatWithMediaLibraryServiceTest method getItem_playable.

@Test
public void getItem_playable() throws InterruptedException {
    String mediaId = MEDIA_ID_GET_PLAYABLE_ITEM;
    connectAndWait();
    CountDownLatch latch = new CountDownLatch(1);
    AtomicReference<MediaItem> itemRef = new AtomicReference<>();
    browserCompat.getItem(mediaId, new ItemCallback() {

        @Override
        public void onItemLoaded(MediaItem item) {
            itemRef.set(item);
            latch.countDown();
        }
    });
    assertThat(latch.await(TIMEOUT_MS, MILLISECONDS)).isTrue();
    assertThat(itemRef.get().getMediaId()).isEqualTo(mediaId);
    assertThat(itemRef.get().isPlayable()).isTrue();
}
Also used : ItemCallback(android.support.v4.media.MediaBrowserCompat.ItemCallback) MediaItem(android.support.v4.media.MediaBrowserCompat.MediaItem) AtomicReference(java.util.concurrent.atomic.AtomicReference) CountDownLatch(java.util.concurrent.CountDownLatch) LargeTest(androidx.test.filters.LargeTest) Test(org.junit.Test)

Example 4 with ItemCallback

use of android.support.v4.media.MediaBrowserCompat.ItemCallback in project media by androidx.

the class MediaBrowserCompatWithMediaLibraryServiceTest method getItem_metadata.

@Test
public void getItem_metadata() throws InterruptedException {
    String mediaId = MEDIA_ID_GET_ITEM_WITH_METADATA;
    connectAndWait();
    CountDownLatch latch = new CountDownLatch(1);
    AtomicReference<MediaItem> itemRef = new AtomicReference<>();
    browserCompat.getItem(mediaId, new ItemCallback() {

        @Override
        public void onItemLoaded(MediaItem item) {
            itemRef.set(item);
            latch.countDown();
        }
    });
    assertThat(latch.await(TIMEOUT_MS, MILLISECONDS)).isTrue();
    assertThat(itemRef.get().getMediaId()).isEqualTo(mediaId);
    MediaDescriptionCompat description = itemRef.get().getDescription();
    assertThat(TextUtils.equals(description.getTitle(), METADATA_TITLE)).isTrue();
    assertThat(TextUtils.equals(description.getSubtitle(), METADATA_SUBTITLE)).isTrue();
    assertThat(TextUtils.equals(description.getDescription(), METADATA_DESCRIPTION)).isTrue();
    assertThat(description.getIconUri()).isEqualTo(METADATA_ARTWORK_URI);
    assertThat(description.getMediaUri()).isEqualTo(METADATA_MEDIA_URI);
    BundleSubject.assertThat(description.getExtras()).string(METADATA_EXTRA_KEY).isEqualTo(METADATA_EXTRA_VALUE);
}
Also used : ItemCallback(android.support.v4.media.MediaBrowserCompat.ItemCallback) MediaItem(android.support.v4.media.MediaBrowserCompat.MediaItem) AtomicReference(java.util.concurrent.atomic.AtomicReference) CountDownLatch(java.util.concurrent.CountDownLatch) MediaDescriptionCompat(android.support.v4.media.MediaDescriptionCompat) LargeTest(androidx.test.filters.LargeTest) Test(org.junit.Test)

Example 5 with ItemCallback

use of android.support.v4.media.MediaBrowserCompat.ItemCallback in project media by androidx.

the class MediaBrowserCompatWithMediaLibraryServiceTest method getItem_browsable.

@Test
public void getItem_browsable() throws InterruptedException {
    String mediaId = MEDIA_ID_GET_BROWSABLE_ITEM;
    connectAndWait();
    CountDownLatch latch = new CountDownLatch(1);
    AtomicReference<MediaItem> itemRef = new AtomicReference<>();
    browserCompat.getItem(mediaId, new ItemCallback() {

        @Override
        public void onItemLoaded(MediaItem item) {
            itemRef.set(item);
            latch.countDown();
        }
    });
    assertThat(latch.await(TIMEOUT_MS, MILLISECONDS)).isTrue();
    assertThat(itemRef.get().getMediaId()).isEqualTo(mediaId);
    assertThat(itemRef.get().isBrowsable()).isTrue();
}
Also used : ItemCallback(android.support.v4.media.MediaBrowserCompat.ItemCallback) MediaItem(android.support.v4.media.MediaBrowserCompat.MediaItem) AtomicReference(java.util.concurrent.atomic.AtomicReference) CountDownLatch(java.util.concurrent.CountDownLatch) LargeTest(androidx.test.filters.LargeTest) Test(org.junit.Test)

Aggregations

ItemCallback (android.support.v4.media.MediaBrowserCompat.ItemCallback)5 MediaItem (android.support.v4.media.MediaBrowserCompat.MediaItem)4 LargeTest (androidx.test.filters.LargeTest)4 CountDownLatch (java.util.concurrent.CountDownLatch)4 Test (org.junit.Test)4 AtomicReference (java.util.concurrent.atomic.AtomicReference)3 MediaBrowserCompat (android.support.v4.media.MediaBrowserCompat)1 MediaDescriptionCompat (android.support.v4.media.MediaDescriptionCompat)1