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.List; 25 import java.util.Map; 26 import java.util.HashMap; 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 DBG = true; 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 public TrackInfo() { 66 this(new ArrayList<Integer>(), new ArrayList<String>()); 67 } 68 TrackInfo(List<Integer> attrIds, List<String> attrMap)69 public 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) : TRACK_NUM_INVALID; 84 85 attribute = attributeMap.get(MEDIA_ATTRIBUTE_TOTAL_TRACK_NUMBER); 86 mTotalTracks = (attribute != null && !attribute.isEmpty()) ? Long.valueOf(attribute) : TOTAL_TRACKS_INVALID; 87 88 mGenre = attributeMap.getOrDefault(MEDIA_ATTRIBUTE_GENRE, UNPOPULATED_ATTRIBUTE); 89 90 attribute = attributeMap.get(MEDIA_ATTRIBUTE_PLAYING_TIME); 91 mTrackLen = (attribute != null && !attribute.isEmpty()) ? Long.valueOf(attribute) : TOTAL_TRACK_TIME_INVALID; 92 } 93 toString()94 public String toString() { 95 return "Metadata [artist=" + mArtistName + " trackTitle= " + mTrackTitle + 96 " albumTitle= " + mAlbumTitle + " genre= " +mGenre+" trackNum= "+ 97 Long.toString(mTrackNum) + " track_len : "+ Long.toString(mTrackLen) + 98 " TotalTracks " + Long.toString(mTotalTracks) + "]"; 99 } 100 getMediaMetaData()101 public MediaMetadata getMediaMetaData() { 102 if (DBG) Log.d(TAG, " TrackInfo " + toString()); 103 MediaMetadata.Builder mMetaDataBuilder = new MediaMetadata.Builder(); 104 mMetaDataBuilder.putString(MediaMetadata.METADATA_KEY_ARTIST, 105 mArtistName); 106 mMetaDataBuilder.putString(MediaMetadata.METADATA_KEY_TITLE, 107 mTrackTitle); 108 mMetaDataBuilder.putString(MediaMetadata.METADATA_KEY_ALBUM, 109 mAlbumTitle); 110 mMetaDataBuilder.putString(MediaMetadata.METADATA_KEY_GENRE, 111 mGenre); 112 mMetaDataBuilder.putLong(MediaMetadata.METADATA_KEY_TRACK_NUMBER, 113 mTrackNum); 114 mMetaDataBuilder.putLong(MediaMetadata.METADATA_KEY_NUM_TRACKS, 115 mTotalTracks); 116 mMetaDataBuilder.putLong(MediaMetadata.METADATA_KEY_DURATION, 117 mTrackLen); 118 return mMetaDataBuilder.build(); 119 } 120 121 displayMetaData()122 public String displayMetaData() { 123 MediaMetadata metaData = getMediaMetaData(); 124 StringBuffer sb = new StringBuffer(); 125 /* getDescription only contains artist, title and album */ 126 sb.append(metaData.getDescription().toString() + " "); 127 if(metaData.containsKey(MediaMetadata.METADATA_KEY_GENRE)) 128 sb.append(metaData.getString(MediaMetadata.METADATA_KEY_GENRE) + " "); 129 if(metaData.containsKey(MediaMetadata.METADATA_KEY_MEDIA_ID)) 130 sb.append(metaData.getString(MediaMetadata.METADATA_KEY_MEDIA_ID) + " "); 131 if(metaData.containsKey(MediaMetadata.METADATA_KEY_TRACK_NUMBER)) 132 sb.append(Long.toString(metaData.getLong(MediaMetadata.METADATA_KEY_TRACK_NUMBER)) + " "); 133 if(metaData.containsKey(MediaMetadata.METADATA_KEY_NUM_TRACKS)) 134 sb.append(Long.toString(metaData.getLong(MediaMetadata.METADATA_KEY_NUM_TRACKS)) + " "); 135 if(metaData.containsKey(MediaMetadata.METADATA_KEY_TRACK_NUMBER)) 136 sb.append(Long.toString(metaData.getLong(MediaMetadata.METADATA_KEY_DURATION)) + " "); 137 if(metaData.containsKey(MediaMetadata.METADATA_KEY_TRACK_NUMBER)) 138 sb.append(Long.toString(metaData.getLong(MediaMetadata.METADATA_KEY_DURATION)) + " "); 139 return sb.toString(); 140 } 141 } 142