blob: 9a510fb7af800126e42e87b13725d65c4cd80ea0 [file] [log] [blame]
Renato Manginic9063ac2014-09-04 12:21:05 -03001/*
2 * Copyright (C) 2014 Google Inc. All Rights Reserved.
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
17package com.example.android.musicservicedemo.utils;
18
19import android.media.MediaMetadata;
20import android.media.session.MediaSession;
21
22import com.example.android.musicservicedemo.model.MusicProvider;
23
24import java.util.ArrayList;
25import java.util.Iterator;
26import java.util.List;
27
28import static com.example.android.musicservicedemo.utils.MediaIDHelper.MEDIA_ID_MUSICS_BY_GENRE;
29
30/**
31 * Utility class to help on queue related tasks.
32 */
33public class QueueHelper {
34
35 private static final String TAG = "QueueHelper";
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 }
Nagesh Susarla372eb5f2014-11-07 16:18:29 -0800129}