Example usage for android.support.v4.media MediaBrowserCompat EXTRA_PAGE_SIZE

List of usage examples for android.support.v4.media MediaBrowserCompat EXTRA_PAGE_SIZE

Introduction

In this page you can find the example usage for android.support.v4.media MediaBrowserCompat EXTRA_PAGE_SIZE.

Prototype

String EXTRA_PAGE_SIZE

To view the source code for android.support.v4.media MediaBrowserCompat EXTRA_PAGE_SIZE.

Click Source Link

Document

Used as an int extra field to denote the number of media items in a page.

Usage

From source file:androidx.media.MediaBrowserCompatUtils.java

public static boolean areSameOptions(Bundle options1, Bundle options2) {
    if (options1 == options2) {
        return true;
    } else if (options1 == null) {
        return options2.getInt(MediaBrowserCompat.EXTRA_PAGE, -1) == -1
                && options2.getInt(MediaBrowserCompat.EXTRA_PAGE_SIZE, -1) == -1;
    } else if (options2 == null) {
        return options1.getInt(MediaBrowserCompat.EXTRA_PAGE, -1) == -1
                && options1.getInt(MediaBrowserCompat.EXTRA_PAGE_SIZE, -1) == -1;
    } else {/*from   www.j  av  a 2s.  c o  m*/
        return options1.getInt(MediaBrowserCompat.EXTRA_PAGE, -1) == options2
                .getInt(MediaBrowserCompat.EXTRA_PAGE, -1)
                && options1.getInt(MediaBrowserCompat.EXTRA_PAGE_SIZE, -1) == options2
                        .getInt(MediaBrowserCompat.EXTRA_PAGE_SIZE, -1);
    }
}

From source file:androidx.media.MediaBrowserCompatUtils.java

public static boolean hasDuplicatedItems(Bundle options1, Bundle options2) {
    int page1 = options1 == null ? -1 : options1.getInt(MediaBrowserCompat.EXTRA_PAGE, -1);
    int page2 = options2 == null ? -1 : options2.getInt(MediaBrowserCompat.EXTRA_PAGE, -1);
    int pageSize1 = options1 == null ? -1 : options1.getInt(MediaBrowserCompat.EXTRA_PAGE_SIZE, -1);
    int pageSize2 = options2 == null ? -1 : options2.getInt(MediaBrowserCompat.EXTRA_PAGE_SIZE, -1);

    int startIndex1, startIndex2, endIndex1, endIndex2;
    if (page1 == -1 || pageSize1 == -1) {
        startIndex1 = 0;/*w  w w. j a va 2s.  co m*/
        endIndex1 = Integer.MAX_VALUE;
    } else {
        startIndex1 = pageSize1 * page1;
        endIndex1 = startIndex1 + pageSize1 - 1;
    }

    if (page2 == -1 || pageSize2 == -1) {
        startIndex2 = 0;
        endIndex2 = Integer.MAX_VALUE;
    } else {
        startIndex2 = pageSize2 * page2;
        endIndex2 = startIndex2 + pageSize2 - 1;
    }

    if (startIndex1 <= startIndex2 && startIndex2 <= endIndex1) {
        return true;
    } else if (startIndex1 <= endIndex2 && endIndex2 <= endIndex1) {
        return true;
    }
    return false;
}

From source file:com.example.android.supportv4.media.BrowseFragment.java

private void loadPage(int page) {
    Bundle options = new Bundle();
    options.putInt(MediaBrowserCompat.EXTRA_PAGE, page);
    options.putInt(MediaBrowserCompat.EXTRA_PAGE_SIZE, PAGE_SIZE);
    mMediaBrowser.subscribe(mMediaId, options, mSubscriptionCallback);
}

From source file:androidx.media.MediaBrowser2.java

/**
 * Subscribe to a parent id for the change in its children. When there's a change,
 * {@link BrowserCallback#onChildrenChanged(MediaBrowser2, String, int, Bundle)} will be called
 * with the bundle that you've specified. You should call
 * {@link #getChildren(String, int, int, Bundle)} to get the actual contents for the parent.
 *
 * @param parentId parent id/*from w  ww .  ja  v a 2s  .  com*/
 * @param extras extra bundle
 */
public void subscribe(@NonNull String parentId, @Nullable Bundle extras) {
    if (parentId == null) {
        throw new IllegalArgumentException("parentId shouldn't be null");
    }
    // TODO: Document this behavior
    Bundle option;
    if (extras != null && (extras.containsKey(MediaBrowserCompat.EXTRA_PAGE)
            || extras.containsKey(MediaBrowserCompat.EXTRA_PAGE_SIZE))) {
        option = new Bundle(extras);
        option.remove(MediaBrowserCompat.EXTRA_PAGE);
        option.remove(MediaBrowserCompat.EXTRA_PAGE_SIZE);
    } else {
        option = extras;
    }
    SubscribeCallback callback = new SubscribeCallback();
    synchronized (mLock) {
        List<SubscribeCallback> list = mSubscribeCallbacks.get(parentId);
        if (list == null) {
            list = new ArrayList<>();
            mSubscribeCallbacks.put(parentId, list);
        }
        list.add(callback);
    }
    // TODO: Revisit using default browser is OK. Here's my concern.
    //       Assume that MediaBrowser2 is connected with the MediaBrowserServiceCompat.
    //       Since MediaBrowserServiceCompat can call MediaBrowserServiceCompat#
    //       getBrowserRootHints(), the service may refuse calls from MediaBrowser2
    getBrowserCompat().subscribe(parentId, option, callback);
}

From source file:com.example.android.supportv4.media.MediaBrowserServiceSupport.java

/**
 * Actual implementation of onLoadChildren that assumes that MusicProvider is already
 * initialized.//from www  .  j  a v a  2 s  . c o  m
 */
private void loadChildrenImpl(final String parentMediaId, final Result<List<MediaItem>> result,
        final Bundle options) {
    Log.d(TAG, "OnLoadChildren: parentMediaId=" + parentMediaId + ", options=" + options);

    int page = -1;
    int pageSize = -1;

    if (options != null && (options.containsKey(MediaBrowserCompat.EXTRA_PAGE)
            || options.containsKey(MediaBrowserCompat.EXTRA_PAGE_SIZE))) {
        page = options.getInt(MediaBrowserCompat.EXTRA_PAGE, -1);
        pageSize = options.getInt(MediaBrowserCompat.EXTRA_PAGE_SIZE, -1);

        if (page < 0 || pageSize < 1) {
            result.sendResult(new ArrayList<MediaItem>());
            return;
        }
    }

    int fromIndex = page == -1 ? 0 : page * pageSize;
    int toIndex = 0;

    List<MediaItem> mediaItems = new ArrayList<>();

    if (MEDIA_ID_ROOT.equals(parentMediaId)) {
        Log.d(TAG, "OnLoadChildren.ROOT");
        if (page <= 0) {
            mediaItems.add(new MediaItem(
                    new MediaDescriptionCompat.Builder().setMediaId(MEDIA_ID_MUSICS_BY_GENRE)
                            .setTitle(getString(R.string.browse_genres))
                            .setIconUri(Uri.parse("android.resource://"
                                    + "com.example.android.supportv4.media/drawable/ic_by_genre"))
                            .setSubtitle(getString(R.string.browse_genre_subtitle)).build(),
                    MediaItem.FLAG_BROWSABLE));
        }

    } else if (MEDIA_ID_MUSICS_BY_GENRE.equals(parentMediaId)) {
        Log.d(TAG, "OnLoadChildren.GENRES");

        List<String> genres = mMusicProvider.getGenres();
        toIndex = page == -1 ? genres.size() : Math.min(fromIndex + pageSize, genres.size());

        for (int i = fromIndex; i < toIndex; i++) {
            String genre = genres.get(i);
            MediaItem item = new MediaItem(new MediaDescriptionCompat.Builder()
                    .setMediaId(createBrowseCategoryMediaID(MEDIA_ID_MUSICS_BY_GENRE, genre)).setTitle(genre)
                    .setSubtitle(getString(R.string.browse_musics_by_genre_subtitle, genre)).build(),
                    MediaItem.FLAG_BROWSABLE);
            mediaItems.add(item);
        }

    } else if (parentMediaId.startsWith(MEDIA_ID_MUSICS_BY_GENRE)) {
        String genre = MediaIDHelper.getHierarchy(parentMediaId)[1];
        Log.d(TAG, "OnLoadChildren.SONGS_BY_GENRE  genre=" + genre);

        List<MediaMetadataCompat> tracks = mMusicProvider.getMusicsByGenre(genre);
        toIndex = page == -1 ? tracks.size() : Math.min(fromIndex + pageSize, tracks.size());

        for (int i = fromIndex; i < toIndex; i++) {
            MediaMetadataCompat track = tracks.get(i);

            // Since mediaMetadata fields are immutable, we need to create a copy, so we
            // can set a hierarchy-aware mediaID. We will need to know the media hierarchy
            // when we get a onPlayFromMusicID call, so we can create the proper queue based
            // on where the music was selected from (by artist, by genre, random, etc)
            String hierarchyAwareMediaID = MediaIDHelper.createMediaID(track.getDescription().getMediaId(),
                    MEDIA_ID_MUSICS_BY_GENRE, genre);
            MediaMetadataCompat trackCopy = new MediaMetadataCompat.Builder(track)
                    .putString(MediaMetadataCompat.METADATA_KEY_MEDIA_ID, hierarchyAwareMediaID).build();
            MediaItem bItem = new MediaItem(trackCopy.getDescription(), MediaItem.FLAG_PLAYABLE);
            mediaItems.add(bItem);
        }
    } else {
        Log.w(TAG, "Skipping unmatched parentMediaId: " + parentMediaId);
    }
    Log.d(TAG, "OnLoadChildren sending " + mediaItems.size() + " results for " + parentMediaId);
    result.sendResult(mediaItems);
}

From source file:androidx.media.MediaBrowser2.java

/**
 * Get list of children under the parent. Result would be sent back asynchronously with the
 * {@link BrowserCallback#onGetChildrenDone(MediaBrowser2, String, int, int, List, Bundle)}.
 *
 * @param parentId parent id for getting the children.
 * @param page page number to get the result. Starts from {@code 1}
 * @param pageSize page size. Should be greater or equal to {@code 1}
 * @param extras extra bundle/*from   w ww  .j  a  v a  2 s.  c om*/
 */
public void getChildren(@NonNull String parentId, int page, int pageSize, @Nullable Bundle extras) {
    if (parentId == null) {
        throw new IllegalArgumentException("parentId shouldn't be null");
    }
    if (page < 1 || pageSize < 1) {
        throw new IllegalArgumentException("Neither page nor pageSize should be less than 1");
    }
    Bundle options = new Bundle(extras);
    options.putInt(MediaBrowserCompat.EXTRA_PAGE, page);
    options.putInt(MediaBrowserCompat.EXTRA_PAGE_SIZE, pageSize);
    // TODO: Revisit using default browser is OK. See TODO in subscribe
    getBrowserCompat().subscribe(parentId, options, new GetChildrenCallback(parentId, page, pageSize));
}

From source file:androidx.media.MediaBrowserServiceCompat.java

List<MediaBrowserCompat.MediaItem> applyOptions(List<MediaBrowserCompat.MediaItem> list, final Bundle options) {
    if (list == null) {
        return null;
    }/*from w w  w. j  a  v  a 2 s .  com*/
    int page = options.getInt(MediaBrowserCompat.EXTRA_PAGE, -1);
    int pageSize = options.getInt(MediaBrowserCompat.EXTRA_PAGE_SIZE, -1);
    if (page == -1 && pageSize == -1) {
        return list;
    }
    int fromIndex = pageSize * page;
    int toIndex = fromIndex + pageSize;
    if (page < 0 || pageSize < 1 || fromIndex >= list.size()) {
        return Collections.EMPTY_LIST;
    }
    if (toIndex > list.size()) {
        toIndex = list.size();
    }
    return list.subList(fromIndex, toIndex);
}