1a907614755847b2630561a1e5949b2b416600d97Sungsoo Lim/* 2a907614755847b2630561a1e5949b2b416600d97Sungsoo Lim * Copyright (C) 2015 The Android Open Source Project 3a907614755847b2630561a1e5949b2b416600d97Sungsoo Lim * 4a907614755847b2630561a1e5949b2b416600d97Sungsoo Lim * Licensed under the Apache License, Version 2.0 (the "License"); 5a907614755847b2630561a1e5949b2b416600d97Sungsoo Lim * you may not use this file except in compliance with the License. 6a907614755847b2630561a1e5949b2b416600d97Sungsoo Lim * You may obtain a copy of the License at 7a907614755847b2630561a1e5949b2b416600d97Sungsoo Lim * 8a907614755847b2630561a1e5949b2b416600d97Sungsoo Lim * http://www.apache.org/licenses/LICENSE-2.0 9a907614755847b2630561a1e5949b2b416600d97Sungsoo Lim * 10a907614755847b2630561a1e5949b2b416600d97Sungsoo Lim * Unless required by applicable law or agreed to in writing, software 11a907614755847b2630561a1e5949b2b416600d97Sungsoo Lim * distributed under the License is distributed on an "AS IS" BASIS, 12a907614755847b2630561a1e5949b2b416600d97Sungsoo Lim * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13a907614755847b2630561a1e5949b2b416600d97Sungsoo Lim * See the License for the specific language governing permissions and 14a907614755847b2630561a1e5949b2b416600d97Sungsoo Lim * limitations under the License. 15a907614755847b2630561a1e5949b2b416600d97Sungsoo Lim */ 16a907614755847b2630561a1e5949b2b416600d97Sungsoo Lim 17a907614755847b2630561a1e5949b2b416600d97Sungsoo Limpackage com.example.android.supportv4.media.utils; 18a907614755847b2630561a1e5949b2b416600d97Sungsoo Lim 19a907614755847b2630561a1e5949b2b416600d97Sungsoo Limimport java.util.Arrays; 20a907614755847b2630561a1e5949b2b416600d97Sungsoo Lim 21a907614755847b2630561a1e5949b2b416600d97Sungsoo Lim/** 22a907614755847b2630561a1e5949b2b416600d97Sungsoo Lim * Utility class to help on queue related tasks. 23a907614755847b2630561a1e5949b2b416600d97Sungsoo Lim */ 24a907614755847b2630561a1e5949b2b416600d97Sungsoo Limpublic class MediaIDHelper { 25a907614755847b2630561a1e5949b2b416600d97Sungsoo Lim 26a907614755847b2630561a1e5949b2b416600d97Sungsoo Lim private static final String TAG = "MediaIDHelper"; 27a907614755847b2630561a1e5949b2b416600d97Sungsoo Lim 28a907614755847b2630561a1e5949b2b416600d97Sungsoo Lim // Media IDs used on browseable items of MediaBrowser 29a907614755847b2630561a1e5949b2b416600d97Sungsoo Lim public static final String MEDIA_ID_ROOT = "__ROOT__"; 30a907614755847b2630561a1e5949b2b416600d97Sungsoo Lim public static final String MEDIA_ID_MUSICS_BY_GENRE = "__BY_GENRE__"; 31a907614755847b2630561a1e5949b2b416600d97Sungsoo Lim public static final String MEDIA_ID_MUSICS_BY_SEARCH = "__BY_SEARCH__"; 32a907614755847b2630561a1e5949b2b416600d97Sungsoo Lim 33a907614755847b2630561a1e5949b2b416600d97Sungsoo Lim private static final char CATEGORY_SEPARATOR = '/'; 34a907614755847b2630561a1e5949b2b416600d97Sungsoo Lim private static final char LEAF_SEPARATOR = '|'; 35a907614755847b2630561a1e5949b2b416600d97Sungsoo Lim 36a907614755847b2630561a1e5949b2b416600d97Sungsoo Lim public static String createMediaID(String musicID, String... categories) { 37a907614755847b2630561a1e5949b2b416600d97Sungsoo Lim // MediaIDs are of the form <categoryType>/<categoryValue>|<musicUniqueId>, to make it easy 38a907614755847b2630561a1e5949b2b416600d97Sungsoo Lim // to find the category (like genre) that a music was selected from, so we 39a907614755847b2630561a1e5949b2b416600d97Sungsoo Lim // can correctly build the playing queue. This is specially useful when 40a907614755847b2630561a1e5949b2b416600d97Sungsoo Lim // one music can appear in more than one list, like "by genre -> genre_1" 41a907614755847b2630561a1e5949b2b416600d97Sungsoo Lim // and "by artist -> artist_1". 42a907614755847b2630561a1e5949b2b416600d97Sungsoo Lim StringBuilder sb = new StringBuilder(); 43a907614755847b2630561a1e5949b2b416600d97Sungsoo Lim if (categories != null && categories.length > 0) { 44a907614755847b2630561a1e5949b2b416600d97Sungsoo Lim sb.append(categories[0]); 45a907614755847b2630561a1e5949b2b416600d97Sungsoo Lim for (int i=1; i < categories.length; i++) { 46a907614755847b2630561a1e5949b2b416600d97Sungsoo Lim sb.append(CATEGORY_SEPARATOR).append(categories[i]); 47a907614755847b2630561a1e5949b2b416600d97Sungsoo Lim } 48a907614755847b2630561a1e5949b2b416600d97Sungsoo Lim } 49a907614755847b2630561a1e5949b2b416600d97Sungsoo Lim if (musicID != null) { 50a907614755847b2630561a1e5949b2b416600d97Sungsoo Lim sb.append(LEAF_SEPARATOR).append(musicID); 51a907614755847b2630561a1e5949b2b416600d97Sungsoo Lim } 52a907614755847b2630561a1e5949b2b416600d97Sungsoo Lim return sb.toString(); 53a907614755847b2630561a1e5949b2b416600d97Sungsoo Lim } 54a907614755847b2630561a1e5949b2b416600d97Sungsoo Lim 55a907614755847b2630561a1e5949b2b416600d97Sungsoo Lim public static String createBrowseCategoryMediaID(String categoryType, String categoryValue) { 56a907614755847b2630561a1e5949b2b416600d97Sungsoo Lim return categoryType + CATEGORY_SEPARATOR + categoryValue; 57a907614755847b2630561a1e5949b2b416600d97Sungsoo Lim } 58a907614755847b2630561a1e5949b2b416600d97Sungsoo Lim 59a907614755847b2630561a1e5949b2b416600d97Sungsoo Lim /** 60a907614755847b2630561a1e5949b2b416600d97Sungsoo Lim * Extracts unique musicID from the mediaID. mediaID is, by this sample's convention, a 61a907614755847b2630561a1e5949b2b416600d97Sungsoo Lim * concatenation of category (eg "by_genre"), categoryValue (eg "Classical") and unique 62a907614755847b2630561a1e5949b2b416600d97Sungsoo Lim * musicID. This is necessary so we know where the user selected the music from, when the music 63a907614755847b2630561a1e5949b2b416600d97Sungsoo Lim * exists in more than one music list, and thus we are able to correctly build the playing queue. 64a907614755847b2630561a1e5949b2b416600d97Sungsoo Lim * 65a907614755847b2630561a1e5949b2b416600d97Sungsoo Lim * @param mediaID that contains the musicID 66a907614755847b2630561a1e5949b2b416600d97Sungsoo Lim * @return musicID 67a907614755847b2630561a1e5949b2b416600d97Sungsoo Lim */ 68a907614755847b2630561a1e5949b2b416600d97Sungsoo Lim public static String extractMusicIDFromMediaID(String mediaID) { 69a907614755847b2630561a1e5949b2b416600d97Sungsoo Lim int pos = mediaID.indexOf(LEAF_SEPARATOR); 70a907614755847b2630561a1e5949b2b416600d97Sungsoo Lim if (pos >= 0) { 71a907614755847b2630561a1e5949b2b416600d97Sungsoo Lim return mediaID.substring(pos+1); 72a907614755847b2630561a1e5949b2b416600d97Sungsoo Lim } 73a907614755847b2630561a1e5949b2b416600d97Sungsoo Lim return null; 74a907614755847b2630561a1e5949b2b416600d97Sungsoo Lim } 75a907614755847b2630561a1e5949b2b416600d97Sungsoo Lim 76a907614755847b2630561a1e5949b2b416600d97Sungsoo Lim /** 77a907614755847b2630561a1e5949b2b416600d97Sungsoo Lim * Extracts category and categoryValue from the mediaID. mediaID is, by this sample's 78a907614755847b2630561a1e5949b2b416600d97Sungsoo Lim * convention, a concatenation of category (eg "by_genre"), categoryValue (eg "Classical") and 79a907614755847b2630561a1e5949b2b416600d97Sungsoo Lim * mediaID. This is necessary so we know where the user selected the music from, when the music 80a907614755847b2630561a1e5949b2b416600d97Sungsoo Lim * exists in more than one music list, and thus we are able to correctly build the playing queue. 81a907614755847b2630561a1e5949b2b416600d97Sungsoo Lim * 82a907614755847b2630561a1e5949b2b416600d97Sungsoo Lim * @param mediaID that contains a category and categoryValue. 83a907614755847b2630561a1e5949b2b416600d97Sungsoo Lim */ 84a907614755847b2630561a1e5949b2b416600d97Sungsoo Lim public static String[] getHierarchy(String mediaID) { 85a907614755847b2630561a1e5949b2b416600d97Sungsoo Lim int pos = mediaID.indexOf(LEAF_SEPARATOR); 86a907614755847b2630561a1e5949b2b416600d97Sungsoo Lim if (pos >= 0) { 87a907614755847b2630561a1e5949b2b416600d97Sungsoo Lim mediaID = mediaID.substring(0, pos); 88a907614755847b2630561a1e5949b2b416600d97Sungsoo Lim } 89a907614755847b2630561a1e5949b2b416600d97Sungsoo Lim return mediaID.split(String.valueOf(CATEGORY_SEPARATOR)); 90a907614755847b2630561a1e5949b2b416600d97Sungsoo Lim } 91a907614755847b2630561a1e5949b2b416600d97Sungsoo Lim 92a907614755847b2630561a1e5949b2b416600d97Sungsoo Lim public static String extractBrowseCategoryValueFromMediaID(String mediaID) { 93a907614755847b2630561a1e5949b2b416600d97Sungsoo Lim String[] hierarchy = getHierarchy(mediaID); 94a907614755847b2630561a1e5949b2b416600d97Sungsoo Lim if (hierarchy != null && hierarchy.length == 2) { 95a907614755847b2630561a1e5949b2b416600d97Sungsoo Lim return hierarchy[1]; 96a907614755847b2630561a1e5949b2b416600d97Sungsoo Lim } 97a907614755847b2630561a1e5949b2b416600d97Sungsoo Lim return null; 98a907614755847b2630561a1e5949b2b416600d97Sungsoo Lim } 99a907614755847b2630561a1e5949b2b416600d97Sungsoo Lim 100a907614755847b2630561a1e5949b2b416600d97Sungsoo Lim private static boolean isBrowseable(String mediaID) { 101a907614755847b2630561a1e5949b2b416600d97Sungsoo Lim return mediaID.indexOf(LEAF_SEPARATOR) < 0; 102a907614755847b2630561a1e5949b2b416600d97Sungsoo Lim } 103a907614755847b2630561a1e5949b2b416600d97Sungsoo Lim 104a907614755847b2630561a1e5949b2b416600d97Sungsoo Lim public static String getParentMediaID(String mediaID) { 105a907614755847b2630561a1e5949b2b416600d97Sungsoo Lim String[] hierarchy = getHierarchy(mediaID); 106a907614755847b2630561a1e5949b2b416600d97Sungsoo Lim if (!isBrowseable(mediaID)) { 107a907614755847b2630561a1e5949b2b416600d97Sungsoo Lim return createMediaID(null, hierarchy); 108a907614755847b2630561a1e5949b2b416600d97Sungsoo Lim } 109a907614755847b2630561a1e5949b2b416600d97Sungsoo Lim if (hierarchy == null || hierarchy.length <= 1) { 110a907614755847b2630561a1e5949b2b416600d97Sungsoo Lim return MEDIA_ID_ROOT; 111a907614755847b2630561a1e5949b2b416600d97Sungsoo Lim } 112a907614755847b2630561a1e5949b2b416600d97Sungsoo Lim String[] parentHierarchy = Arrays.copyOf(hierarchy, hierarchy.length-1); 113a907614755847b2630561a1e5949b2b416600d97Sungsoo Lim return createMediaID(null, parentHierarchy); 114a907614755847b2630561a1e5949b2b416600d97Sungsoo Lim } 115a907614755847b2630561a1e5949b2b416600d97Sungsoo Lim} 116