Track implements Serializable { public static Track fromMediaMetadata(MediaMetadata metadata) { String title = metadata.getString(MediaMetadata.METADATA_KEY_TITLE); String artist = metadata.getString(MediaMetadata.METADATA_KEY_ARTIST); String composer = metadata.getString(MediaMetadata.METADATA_KEY_COMPOSER); String album = metadata.getString(MediaMetadata.METADATA_KEY_ALBUM); String albumArtist = metadata.getString(MediaMetadata.METADATA_KEY_ALBUM_ARTIST); Bitmap art = metadata.getBitmap(MediaMetadata.METADATA_KEY_ART); long duration = metadata.getLong(MediaMetadata.METADATA_KEY_DURATION); if (title == null) { title = metadata.getString(MediaMetadata.METADATA_KEY_DISPLAY_TITLE); if (title == null) { title = ""; } } if (art == null) { art = metadata.getBitmap(MediaMetadata.METADATA_KEY_ALBUM_ART); } Track.Builder builder = Track.builder().track(title); if (duration < 1000) { duration *= 1000; } if (duration > 0) { builder.duration(duration); } if (album != null && !album.isEmpty()) { builder.album(album); } if (albumArtist != null && !albumArtist.isEmpty()) { builder.albumArtist(albumArtist); } if (art != null) { builder.art(art); } if (artist != null) { builder.artist(artist); } else if (albumArtist != null) { builder.artist(albumArtist); } else { return new TitleExtractor().transform(builder.artist("").build()); } if (composer != null) { builder.composer(composer); } return builder.build(); }  abstract String track(); abstract String artist(); abstract Optional<String> composer(); abstract Optional<String> album(); abstract Optional<String> albumArtist(); abstract Optional<Long> duration(); abstract Optional<Bitmap> art(); abstract Builder toBuilder(); boolean isValid(); static Track fromMediaMetadata(MediaMetadata metadata); boolean isSameTrack(Track track); static Track empty(); static Builder builder();  }
@Test public void fromMediaMetadata() { MediaMetadata input = new MediaMetadata.Builder() .putString(MediaMetadata.METADATA_KEY_TITLE, "Title") .putString(MediaMetadata.METADATA_KEY_ARTIST, "Artist") .putString(MediaMetadata.METADATA_KEY_ALBUM, "Album") .putString(MediaMetadata.METADATA_KEY_ALBUM_ARTIST, "Album Artist") .putBitmap(MediaMetadata.METADATA_KEY_ART, EMPTY_BITMAP) .putLong(MediaMetadata.METADATA_KEY_DURATION, 10000) .build(); Track output = Track.fromMediaMetadata(input); assertThat(output.track()).isEqualTo("Title"); assertThat(output.artist()).isEqualTo("Artist"); assertThat(output.albumArtist()).hasValue("Album Artist"); assertThat(output.art()).isPresent(); assertThat(output.duration()).hasValue((long) 10000); }