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.android.music.utils; 18 19 import java.util.Arrays; 20 21 /** 22 * Utility class to help on queue related tasks. 23 */ 24 public class MediaIDHelper { 25 private static final String TAG = "MediaIDHelper"; 26 27 // Media IDs used on browseable items of MediaBrowser 28 public static final String MEDIA_ID_ROOT = "__ROOT__"; 29 public static final String MEDIA_ID_MUSICS_BY_ARTIST = "__BY_ARTIST__"; 30 public static final String MEDIA_ID_MUSICS_BY_ALBUM = "__BY_ALBUM__"; 31 public static final String MEDIA_ID_MUSICS_BY_SONG = "__BY_SONG__"; 32 public static final String MEDIA_ID_MUSICS_BY_PLAYLIST = "__BY_PLAYLIST__"; 33 public static final String MEDIA_ID_MUSICS_BY_SEARCH = "__BY_SEARCH__"; 34 public static final String MEDIA_ID_NOW_PLAYING = "__NOW_PLAYING__"; 35 36 private static final char CATEGORY_SEPARATOR = 31; 37 private static final char LEAF_SEPARATOR = 30; 38 createMediaID(String musicID, String... categories)39 public static String createMediaID(String musicID, String... categories) { 40 // MediaIDs are of the form <categoryType>/<categoryValue>|<musicUniqueId>, to make it easy 41 // to find the category (like genre) that a music was selected from, so we 42 // can correctly build the playing queue. This is specially useful when 43 // one music can appear in more than one list, like "by genre -> genre_1" 44 // and "by artist -> artist_1". 45 StringBuilder sb = new StringBuilder(); 46 if (categories != null && categories.length > 0) { 47 sb.append(categories[0]); 48 for (int i = 1; i < categories.length; i++) { 49 sb.append(CATEGORY_SEPARATOR).append(categories[i]); 50 } 51 } 52 if (musicID != null) { 53 sb.append(LEAF_SEPARATOR).append(musicID); 54 } 55 return sb.toString(); 56 } 57 createBrowseCategoryMediaID(String categoryType, String categoryValue)58 public static String createBrowseCategoryMediaID(String categoryType, String categoryValue) { 59 return categoryType + CATEGORY_SEPARATOR + categoryValue; 60 } 61 62 /** 63 * Extracts unique musicID from the mediaID. mediaID is, by this sample's convention, a 64 * concatenation of category (eg "by_genre"), categoryValue (eg "Classical") and unique 65 * musicID. This is necessary so we know where the user selected the music from, when the music 66 * exists in more than one music list, and thus we are able to correctly build the playing 67 * queue. 68 * 69 * @param mediaID that contains the musicID 70 * @return musicID 71 */ extractMusicIDFromMediaID(String mediaID)72 public static String extractMusicIDFromMediaID(String mediaID) { 73 int pos = mediaID.indexOf(LEAF_SEPARATOR); 74 if (pos >= 0) { 75 return mediaID.substring(pos + 1); 76 } 77 return null; 78 } 79 80 /** 81 * Extracts category and categoryValue from the mediaID. mediaID is, by this sample's 82 * convention, a concatenation of category (eg "by_genre"), categoryValue (eg "Classical") and 83 * mediaID. This is necessary so we know where the user selected the music from, when the music 84 * exists in more than one music list, and thus we are able to correctly build the playing 85 * queue. 86 * 87 * @param mediaID that contains a category and categoryValue. 88 */ getHierarchy(String mediaID)89 public static String[] getHierarchy(String mediaID) { 90 int pos = mediaID.indexOf(LEAF_SEPARATOR); 91 if (pos >= 0) { 92 mediaID = mediaID.substring(0, pos); 93 } 94 return mediaID.split(String.valueOf(CATEGORY_SEPARATOR)); 95 } 96 extractBrowseCategoryValueFromMediaID(String mediaID)97 public static String extractBrowseCategoryValueFromMediaID(String mediaID) { 98 String[] hierarchy = getHierarchy(mediaID); 99 if (hierarchy != null && hierarchy.length == 2) { 100 return hierarchy[1]; 101 } 102 return null; 103 } 104 isBrowseable(String mediaID)105 private static boolean isBrowseable(String mediaID) { 106 return mediaID.indexOf(LEAF_SEPARATOR) < 0; 107 } 108 getParentMediaID(String mediaID)109 public static String getParentMediaID(String mediaID) { 110 String[] hierarchy = getHierarchy(mediaID); 111 if (!isBrowseable(mediaID)) { 112 return createMediaID(null, hierarchy); 113 } 114 if (hierarchy == null || hierarchy.length <= 1) { 115 return MEDIA_ID_ROOT; 116 } 117 String[] parentHierarchy = Arrays.copyOf(hierarchy, hierarchy.length - 1); 118 return createMediaID(null, parentHierarchy); 119 } 120 } 121