1 /* 2 * Copyright (C) 2016 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 18 package com.android.bluetooth.avrcpcontroller; 19 20 import android.media.MediaMetadata; 21 import android.util.Log; 22 23 import java.util.ArrayList; 24 import java.util.HashMap; 25 import java.util.List; 26 import java.util.Map; 27 28 /* 29 * Contains information about tracks that either currently playing or maintained in playlist 30 * This is used as a local repository for information that will be passed on as MediaMetadata to the 31 * MediaSessionServicve 32 */ 33 class TrackInfo { 34 private static final String TAG = "AvrcpTrackInfo"; 35 private static final boolean VDBG = false; 36 37 /* 38 * Default values for each of the items from JNI 39 */ 40 private static final int TRACK_NUM_INVALID = -1; 41 private static final int TOTAL_TRACKS_INVALID = -1; 42 private static final int TOTAL_TRACK_TIME_INVALID = -1; 43 private static final String UNPOPULATED_ATTRIBUTE = ""; 44 45 /* 46 *Element Id Values for GetMetaData from JNI 47 */ 48 private static final int MEDIA_ATTRIBUTE_TITLE = 0x01; 49 private static final int MEDIA_ATTRIBUTE_ARTIST_NAME = 0x02; 50 private static final int MEDIA_ATTRIBUTE_ALBUM_NAME = 0x03; 51 private static final int MEDIA_ATTRIBUTE_TRACK_NUMBER = 0x04; 52 private static final int MEDIA_ATTRIBUTE_TOTAL_TRACK_NUMBER = 0x05; 53 private static final int MEDIA_ATTRIBUTE_GENRE = 0x06; 54 private static final int MEDIA_ATTRIBUTE_PLAYING_TIME = 0x07; 55 56 57 private final String mArtistName; 58 private final String mTrackTitle; 59 private final String mAlbumTitle; 60 private final String mGenre; 61 private final long mTrackNum; // number of audio file on original recording. 62 private final long mTotalTracks; // total number of tracks on original recording 63 private final long mTrackLen; // full length of AudioFile. 64 TrackInfo()65 TrackInfo() { 66 this(new ArrayList<Integer>(), new ArrayList<String>()); 67 } 68 TrackInfo(List<Integer> attrIds, List<String> attrMap)69 TrackInfo(List<Integer> attrIds, List<String> attrMap) { 70 Map<Integer, String> attributeMap = new HashMap<>(); 71 for (int i = 0; i < attrIds.size(); i++) { 72 attributeMap.put(attrIds.get(i), attrMap.get(i)); 73 } 74 75 String attribute; 76 mTrackTitle = attributeMap.getOrDefault(MEDIA_ATTRIBUTE_TITLE, UNPOPULATED_ATTRIBUTE); 77 78 mArtistName = attributeMap.getOrDefault(MEDIA_ATTRIBUTE_ARTIST_NAME, UNPOPULATED_ATTRIBUTE); 79 80 mAlbumTitle = attributeMap.getOrDefault(MEDIA_ATTRIBUTE_ALBUM_NAME, UNPOPULATED_ATTRIBUTE); 81 82 attribute = attributeMap.get(MEDIA_ATTRIBUTE_TRACK_NUMBER); 83 mTrackNum = (attribute != null && !attribute.isEmpty()) ? Long.valueOf(attribute) 84 : TRACK_NUM_INVALID; 85 86 attribute = attributeMap.get(MEDIA_ATTRIBUTE_TOTAL_TRACK_NUMBER); 87 mTotalTracks = (attribute != null && !attribute.isEmpty()) ? Long.valueOf(attribute) 88 : TOTAL_TRACKS_INVALID; 89 90 mGenre = attributeMap.getOrDefault(MEDIA_ATTRIBUTE_GENRE, UNPOPULATED_ATTRIBUTE); 91 92 attribute = attributeMap.get(MEDIA_ATTRIBUTE_PLAYING_TIME); 93 mTrackLen = (attribute != null && !attribute.isEmpty()) ? Long.valueOf(attribute) 94 : TOTAL_TRACK_TIME_INVALID; 95 } 96 97 @Override toString()98 public String toString() { 99 return "Metadata [artist=" + mArtistName + " trackTitle= " + mTrackTitle + " albumTitle= " 100 + mAlbumTitle + " genre= " + mGenre + " trackNum= " + Long.toString(mTrackNum) 101 + " track_len : " + Long.toString(mTrackLen) + " TotalTracks " + Long.toString( 102 mTotalTracks) + "]"; 103 } 104 getMediaMetaData()105 public MediaMetadata getMediaMetaData() { 106 if (VDBG) { 107 Log.d(TAG, " TrackInfo " + toString()); 108 } 109 MediaMetadata.Builder mMetaDataBuilder = new MediaMetadata.Builder(); 110 mMetaDataBuilder.putString(MediaMetadata.METADATA_KEY_ARTIST, mArtistName); 111 mMetaDataBuilder.putString(MediaMetadata.METADATA_KEY_TITLE, mTrackTitle); 112 mMetaDataBuilder.putString(MediaMetadata.METADATA_KEY_ALBUM, mAlbumTitle); 113 mMetaDataBuilder.putString(MediaMetadata.METADATA_KEY_GENRE, mGenre); 114 mMetaDataBuilder.putLong(MediaMetadata.METADATA_KEY_TRACK_NUMBER, mTrackNum); 115 mMetaDataBuilder.putLong(MediaMetadata.METADATA_KEY_NUM_TRACKS, mTotalTracks); 116 mMetaDataBuilder.putLong(MediaMetadata.METADATA_KEY_DURATION, mTrackLen); 117 return mMetaDataBuilder.build(); 118 } 119 120 displayMetaData()121 public String displayMetaData() { 122 MediaMetadata metaData = getMediaMetaData(); 123 StringBuffer sb = new StringBuffer(); 124 /* getDescription only contains artist, title and album */ 125 sb.append(metaData.getDescription().toString() + " "); 126 if (metaData.containsKey(MediaMetadata.METADATA_KEY_GENRE)) { 127 sb.append(metaData.getString(MediaMetadata.METADATA_KEY_GENRE) + " "); 128 } 129 if (metaData.containsKey(MediaMetadata.METADATA_KEY_MEDIA_ID)) { 130 sb.append(metaData.getString(MediaMetadata.METADATA_KEY_MEDIA_ID) + " "); 131 } 132 if (metaData.containsKey(MediaMetadata.METADATA_KEY_TRACK_NUMBER)) { 133 sb.append( 134 Long.toString(metaData.getLong(MediaMetadata.METADATA_KEY_TRACK_NUMBER)) + " "); 135 } 136 if (metaData.containsKey(MediaMetadata.METADATA_KEY_NUM_TRACKS)) { 137 sb.append(Long.toString(metaData.getLong(MediaMetadata.METADATA_KEY_NUM_TRACKS)) + " "); 138 } 139 if (metaData.containsKey(MediaMetadata.METADATA_KEY_TRACK_NUMBER)) { 140 sb.append(Long.toString(metaData.getLong(MediaMetadata.METADATA_KEY_DURATION)) + " "); 141 } 142 if (metaData.containsKey(MediaMetadata.METADATA_KEY_TRACK_NUMBER)) { 143 sb.append(Long.toString(metaData.getLong(MediaMetadata.METADATA_KEY_DURATION)) + " "); 144 } 145 return sb.toString(); 146 } 147 } 148