Java tutorial
/* * Copyright (C) 2014 The Android Open Source Project * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package com.appdevper.mediaplayer.util; import android.media.MediaMetadata; import android.media.session.MediaSession; import android.os.Bundle; import android.support.v4.media.MediaMetadataCompat; import android.support.v4.media.session.MediaSessionCompat; import com.appdevper.mediaplayer.model.MusicProvider; import java.util.ArrayList; import java.util.Collections; import java.util.List; import java.util.concurrent.ThreadLocalRandom; /** * Utility class to help on queue related tasks. */ public class QueueHelper { private static final String TAG = LogHelper.makeLogTag(QueueHelper.class); public static List<MediaSessionCompat.QueueItem> getPlayingQueue(String mediaId, MusicProvider musicProvider) { Iterable<MediaMetadataCompat> tracks = musicProvider.getAllMusic(); if (tracks == null) { //LogHelper.e(TAG, "Unrecognized category type: ", categoryType, " for media ", mediaId); return null; } return convertToQueue(tracks); } public static int getMusicIndexOnQueue(Iterable<MediaSessionCompat.QueueItem> queue, String mediaId) { int index = 0; for (MediaSessionCompat.QueueItem item : queue) { if (mediaId.equals(item.getDescription().getMediaId())) { return index; } index++; } return -1; } public static int getMusicIndexOnQueue(Iterable<MediaSessionCompat.QueueItem> queue, long queueId) { int index = 0; for (MediaSessionCompat.QueueItem item : queue) { if (queueId == item.getQueueId()) { return index; } index++; } return -1; } private static List<MediaSessionCompat.QueueItem> convertToQueue(Iterable<MediaMetadataCompat> tracks, String... categories) { List<MediaSessionCompat.QueueItem> queue = new ArrayList<>(); int count = 0; for (MediaMetadataCompat track : tracks) { // We create a hierarchy-aware mediaID, so we know what the queue is about by looking // at the QueueItem media IDs. String hierarchyAwareMediaID = MediaIDHelper.createMediaID(track.getDescription().getMediaId(), categories); MediaMetadataCompat trackCopy = new MediaMetadataCompat.Builder(track) .putString(MediaMetadataCompat.METADATA_KEY_MEDIA_ID, hierarchyAwareMediaID).build(); // We don't expect queues to change after created, so we use the item index as the // queueId. Any other number unique in the queue would work. MediaSessionCompat.QueueItem item = new MediaSessionCompat.QueueItem(trackCopy.getDescription(), count++); queue.add(item); } return queue; } private static List<MediaSessionCompat.QueueItem> convertToQueue(Iterable<MediaMetadataCompat> tracks) { List<MediaSessionCompat.QueueItem> queue = new ArrayList<>(); int count = 0; for (MediaMetadataCompat track : tracks) { MediaMetadataCompat trackCopy = new MediaMetadataCompat.Builder(track) .putString(MediaMetadataCompat.METADATA_KEY_MEDIA_ID, track.getDescription().getMediaId()) .build(); MediaSessionCompat.QueueItem item = new MediaSessionCompat.QueueItem(trackCopy.getDescription(), count++); queue.add(item); } return queue; } /** * Create a random queue. * * @param musicProvider the provider used for fetching music. * @return list containing {@link MediaSession.QueueItem}'s */ public static List<MediaSessionCompat.QueueItem> getRandomQueue(MusicProvider musicProvider) { List<MediaMetadataCompat> result = new ArrayList<>(); for (String genre : musicProvider.getGenres()) { Iterable<MediaMetadataCompat> tracks = musicProvider.getMusicsByGenre(genre); for (MediaMetadataCompat track : tracks) { if (ThreadLocalRandom.current().nextBoolean()) { result.add(track); } } } LogHelper.d(TAG, "getRandomQueue: result.size=", result.size()); Collections.shuffle(result); return convertToQueue(result, MediaIDHelper.MEDIA_ID_MUSICS_BY_SEARCH, "random"); } public static boolean isIndexPlayable(int index, List<MediaSessionCompat.QueueItem> queue) { return (queue != null && index >= 0 && index < queue.size()); } }