1 /* 2 * Copyright 2019 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.car.media.testmediaapp.loader; 18 19 import static android.support.v4.media.MediaMetadataCompat.METADATA_KEY_ADVERTISEMENT; 20 import static android.support.v4.media.MediaMetadataCompat.METADATA_KEY_ALBUM; 21 import static android.support.v4.media.MediaMetadataCompat.METADATA_KEY_ALBUM_ART; 22 import static android.support.v4.media.MediaMetadataCompat.METADATA_KEY_ALBUM_ARTIST; 23 import static android.support.v4.media.MediaMetadataCompat.METADATA_KEY_ALBUM_ART_URI; 24 import static android.support.v4.media.MediaMetadataCompat.METADATA_KEY_ART; 25 import static android.support.v4.media.MediaMetadataCompat.METADATA_KEY_ARTIST; 26 import static android.support.v4.media.MediaMetadataCompat.METADATA_KEY_ART_URI; 27 import static android.support.v4.media.MediaMetadataCompat.METADATA_KEY_AUTHOR; 28 import static android.support.v4.media.MediaMetadataCompat.METADATA_KEY_BT_FOLDER_TYPE; 29 import static android.support.v4.media.MediaMetadataCompat.METADATA_KEY_COMPILATION; 30 import static android.support.v4.media.MediaMetadataCompat.METADATA_KEY_COMPOSER; 31 import static android.support.v4.media.MediaMetadataCompat.METADATA_KEY_DATE; 32 import static android.support.v4.media.MediaMetadataCompat.METADATA_KEY_DISC_NUMBER; 33 import static android.support.v4.media.MediaMetadataCompat.METADATA_KEY_DISPLAY_DESCRIPTION; 34 import static android.support.v4.media.MediaMetadataCompat.METADATA_KEY_DISPLAY_ICON; 35 import static android.support.v4.media.MediaMetadataCompat.METADATA_KEY_DISPLAY_ICON_URI; 36 import static android.support.v4.media.MediaMetadataCompat.METADATA_KEY_DISPLAY_SUBTITLE; 37 import static android.support.v4.media.MediaMetadataCompat.METADATA_KEY_DISPLAY_TITLE; 38 import static android.support.v4.media.MediaMetadataCompat.METADATA_KEY_DOWNLOAD_STATUS; 39 import static android.support.v4.media.MediaMetadataCompat.METADATA_KEY_DURATION; 40 import static android.support.v4.media.MediaMetadataCompat.METADATA_KEY_GENRE; 41 import static android.support.v4.media.MediaMetadataCompat.METADATA_KEY_MEDIA_ID; 42 import static android.support.v4.media.MediaMetadataCompat.METADATA_KEY_MEDIA_URI; 43 import static android.support.v4.media.MediaMetadataCompat.METADATA_KEY_NUM_TRACKS; 44 import static android.support.v4.media.MediaMetadataCompat.METADATA_KEY_RATING; 45 import static android.support.v4.media.MediaMetadataCompat.METADATA_KEY_TITLE; 46 import static android.support.v4.media.MediaMetadataCompat.METADATA_KEY_TRACK_NUMBER; 47 import static android.support.v4.media.MediaMetadataCompat.METADATA_KEY_USER_RATING; 48 import static android.support.v4.media.MediaMetadataCompat.METADATA_KEY_WRITER; 49 import static android.support.v4.media.MediaMetadataCompat.METADATA_KEY_YEAR; 50 51 import static com.android.car.media.testmediaapp.loader.TmaLoaderUtils.enumNamesToValues; 52 53 import android.support.v4.media.MediaMetadataCompat; 54 import android.util.Log; 55 56 import com.android.car.media.testmediaapp.TmaAssetProvider; 57 58 import org.json.JSONException; 59 import org.json.JSONObject; 60 61 import java.util.EnumSet; 62 import java.util.Map; 63 import java.util.Set; 64 65 66 class TmaMediaMetadataReader { 67 68 private final static String TAG = "TmaMetadataReader"; 69 70 private enum ValueType { 71 LONG, 72 TEXT, 73 BITMAP, 74 RATING 75 } 76 77 /** The name of each entry is the key used in the json file. */ 78 private enum MetadataKey { 79 TITLE (METADATA_KEY_TITLE, ValueType.TEXT), 80 ARTIST (METADATA_KEY_ARTIST, ValueType.TEXT), 81 DURATION (METADATA_KEY_DURATION, ValueType.LONG), 82 ALBUM (METADATA_KEY_ALBUM, ValueType.TEXT), 83 AUTHOR (METADATA_KEY_AUTHOR, ValueType.TEXT), 84 WRITER (METADATA_KEY_WRITER, ValueType.TEXT), 85 COMPOSER (METADATA_KEY_COMPOSER, ValueType.TEXT), 86 COMPILATION (METADATA_KEY_COMPILATION, ValueType.TEXT), 87 DATE (METADATA_KEY_DATE, ValueType.TEXT), 88 YEAR (METADATA_KEY_YEAR, ValueType.LONG), 89 GENRE (METADATA_KEY_GENRE, ValueType.TEXT), 90 TRACK_NUMBER (METADATA_KEY_TRACK_NUMBER, ValueType.LONG), 91 NUM_TRACKS (METADATA_KEY_NUM_TRACKS, ValueType.LONG), 92 DISC_NUMBER (METADATA_KEY_DISC_NUMBER, ValueType.LONG), 93 ALBUM_ARTIST (METADATA_KEY_ALBUM_ARTIST, ValueType.TEXT), 94 ART (METADATA_KEY_ART, ValueType.BITMAP), 95 ART_URI (METADATA_KEY_ART_URI, ValueType.TEXT), 96 ALBUM_ART (METADATA_KEY_ALBUM_ART, ValueType.BITMAP), 97 ALBUM_ART_URI (METADATA_KEY_ALBUM_ART_URI, ValueType.TEXT), 98 USER_RATING (METADATA_KEY_USER_RATING, ValueType.RATING), 99 RATING (METADATA_KEY_RATING, ValueType.RATING), 100 DISPLAY_TITLE (METADATA_KEY_DISPLAY_TITLE, ValueType.TEXT), 101 DISPLAY_SUBTITLE (METADATA_KEY_DISPLAY_SUBTITLE, ValueType.TEXT), 102 DISPLAY_DESCRIPTION (METADATA_KEY_DISPLAY_DESCRIPTION, ValueType.TEXT), 103 DISPLAY_ICON (METADATA_KEY_DISPLAY_ICON, ValueType.BITMAP), 104 DISPLAY_ICON_URI (METADATA_KEY_DISPLAY_ICON_URI, ValueType.TEXT), 105 MEDIA_ID (METADATA_KEY_MEDIA_ID, ValueType.TEXT), 106 BT_FOLDER_TYPE (METADATA_KEY_BT_FOLDER_TYPE, ValueType.LONG), 107 MEDIA_URI (METADATA_KEY_MEDIA_URI, ValueType.TEXT), 108 ADVERTISEMENT (METADATA_KEY_ADVERTISEMENT, ValueType.LONG), 109 DOWNLOAD_STATUS (METADATA_KEY_DOWNLOAD_STATUS, ValueType.LONG); 110 111 /** The full name of the key in {@link MediaMetadataCompat}. */ 112 final String mLongName; 113 /** The type of the key's value in {@link MediaMetadataCompat}. */ 114 final ValueType mKeyType; 115 MetadataKey(String longName, ValueType valueType)116 MetadataKey(String longName, ValueType valueType) { 117 mLongName = longName; 118 mKeyType = valueType; 119 } 120 } 121 122 private static TmaMediaMetadataReader sInstance; 123 getInstance()124 synchronized static TmaMediaMetadataReader getInstance() { 125 if (sInstance == null) { 126 sInstance = new TmaMediaMetadataReader(); 127 } 128 return sInstance; 129 } 130 131 private final Map<String, MetadataKey> mMetadataKeys; 132 private final Set<MetadataKey> mUriKeys; 133 TmaMediaMetadataReader()134 private TmaMediaMetadataReader() { 135 mMetadataKeys = enumNamesToValues(MetadataKey.values()); 136 mUriKeys = EnumSet.of(MetadataKey.ART_URI, MetadataKey.ALBUM_ART_URI, 137 MetadataKey.DISPLAY_ICON_URI, MetadataKey.MEDIA_URI); 138 } 139 140 fromJson(JSONObject object)141 MediaMetadataCompat fromJson(JSONObject object) throws JSONException { 142 MediaMetadataCompat.Builder builder = new MediaMetadataCompat.Builder(); 143 for (String jsonKey : object.keySet()) { 144 MetadataKey key = mMetadataKeys.get(jsonKey); 145 if (key != null) { 146 switch (key.mKeyType) { 147 case LONG: 148 builder.putLong(key.mLongName, object.getLong(jsonKey)); 149 break; 150 case TEXT: 151 String value = object.getString(jsonKey); 152 if (mUriKeys.contains(key)) { 153 value = TmaAssetProvider.buildUriString(value); 154 } 155 builder.putString(key.mLongName, value); 156 break; 157 case BITMAP: 158 case RATING: 159 Log.e(TAG, "Ignoring unsupported type: " + key.mKeyType + " for key: " 160 + jsonKey + " / " + key.mLongName); 161 } 162 } else { 163 Log.e(TAG, "Ignoring unsupported key: " + jsonKey); 164 } 165 } 166 return builder.build(); 167 } 168 } 169