Home | History | Annotate | Download | only in utils
      1 /*
      2  * Copyright (C) 2014 The Android Open Source Project
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  *      http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 
     17 package com.example.android.mediabrowserservice.utils;
     18 
     19 import android.media.MediaMetadata;
     20 import android.media.session.MediaSession;
     21 
     22 import com.example.android.mediabrowserservice.model.MusicProvider;
     23 
     24 import java.util.ArrayList;
     25 import java.util.Iterator;
     26 import java.util.List;
     27 
     28 import static com.example.android.mediabrowserservice.utils.MediaIDHelper.MEDIA_ID_MUSICS_BY_GENRE;
     29 
     30 /**
     31  * Utility class to help on queue related tasks.
     32  */
     33 public class QueueHelper {
     34 
     35     private static final String TAG = LogHelper.makeLogTag(QueueHelper.class.getSimpleName());
     36 
     37     public static final List<MediaSession.QueueItem> getPlayingQueue(String mediaId,
     38             MusicProvider musicProvider) {
     39 
     40         // extract the category and unique music ID from the media ID:
     41         String[] category = MediaIDHelper.extractBrowseCategoryFromMediaID(mediaId);
     42 
     43         // This sample only supports genre category.
     44         if (!category[0].equals(MEDIA_ID_MUSICS_BY_GENRE) || category.length != 2) {
     45             LogHelper.e(TAG, "Could not build a playing queue for this mediaId: ", mediaId);
     46             return null;
     47         }
     48 
     49         String categoryValue = category[1];
     50         LogHelper.e(TAG, "Creating playing queue for musics of genre ", categoryValue);
     51 
     52         List<MediaSession.QueueItem> queue = convertToQueue(
     53                 musicProvider.getMusicsByGenre(categoryValue));
     54 
     55         return queue;
     56     }
     57 
     58     public static final List<MediaSession.QueueItem> getPlayingQueueFromSearch(String query,
     59             MusicProvider musicProvider) {
     60 
     61         LogHelper.e(TAG, "Creating playing queue for musics from search ", query);
     62 
     63         return convertToQueue(musicProvider.searchMusics(query));
     64     }
     65 
     66 
     67     public static final int getMusicIndexOnQueue(Iterable<MediaSession.QueueItem> queue,
     68              String mediaId) {
     69         int index = 0;
     70         for (MediaSession.QueueItem item: queue) {
     71             if (mediaId.equals(item.getDescription().getMediaId())) {
     72                 return index;
     73             }
     74             index++;
     75         }
     76         return -1;
     77     }
     78 
     79     public static final int getMusicIndexOnQueue(Iterable<MediaSession.QueueItem> queue,
     80              long queueId) {
     81         int index = 0;
     82         for (MediaSession.QueueItem item: queue) {
     83             if (queueId == item.getQueueId()) {
     84                 return index;
     85             }
     86             index++;
     87         }
     88         return -1;
     89     }
     90 
     91     private static final List<MediaSession.QueueItem> convertToQueue(
     92             Iterable<MediaMetadata> tracks) {
     93         List<MediaSession.QueueItem> queue = new ArrayList<>();
     94         int count = 0;
     95         for (MediaMetadata track : tracks) {
     96             // We don't expect queues to change after created, so we use the item index as the
     97             // queueId. Any other number unique in the queue would work.
     98             MediaSession.QueueItem item = new MediaSession.QueueItem(
     99                     track.getDescription(), count++);
    100             queue.add(item);
    101         }
    102         return queue;
    103 
    104     }
    105 
    106     /**
    107      * Create a random queue. For simplicity sake, instead of a random queue, we create a
    108      * queue using the first genre,
    109      *
    110      * @param musicProvider
    111      * @return
    112      */
    113     public static final List<MediaSession.QueueItem> getRandomQueue(MusicProvider musicProvider) {
    114         Iterator<String> genres = musicProvider.getGenres().iterator();
    115         if (!genres.hasNext()) {
    116             return new ArrayList<>();
    117         }
    118         String genre = genres.next();
    119         Iterable<MediaMetadata> tracks = musicProvider.getMusicsByGenre(genre);
    120 
    121         return convertToQueue(tracks);
    122     }
    123 
    124 
    125 
    126     public static final boolean isIndexPlayable(int index, List<MediaSession.QueueItem> queue) {
    127         return (queue != null && index >= 0 && index < queue.size());
    128     }
    129 }
    130