/*
 * Copyright (c) 2017 Reto Inderbitzin (mail@indr.ch)
 *
 * For the full copyright and license information, please view
 * the LICENSE file that was distributed with this source code.
 */

package ch.indr.threethreefive.libs;

import android.graphics.Bitmap;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.media.MediaDescriptionCompat;
import android.support.v4.media.MediaMetadataCompat;

import ch.indr.threethreefive.libs.utils.StringUtils;

public class MediaItem {

  private MediaMetadataCompat metadata;

  private final String title;
  private final String subtitle;
  private final String pageUri;
  private final String mediaUri;

  public MediaItem(final @NonNull MediaMetadataCompat metadata, final @NonNull String pageUri) {
    this.metadata = metadata;

    String title = metadata.getString(MediaMetadataCompat.METADATA_KEY_DISPLAY_TITLE);
    if (StringUtils.isEmpty(title)) {
      title = metadata.getString(MediaMetadataCompat.METADATA_KEY_TITLE);
    }
    this.title = title;
    this.subtitle = metadata.getString(MediaMetadataCompat.METADATA_KEY_DISPLAY_SUBTITLE);
    this.pageUri = pageUri;
    this.mediaUri = metadata.getString(MediaMetadataCompat.METADATA_KEY_MEDIA_URI);
  }

  @Nullable public String getTitle() {
    return title;
  }

  @Nullable public String getSubtitle() {
    return subtitle;
  }

  @NonNull public String getPageUri() {
    return pageUri;
  }

  @Nullable public String getMediaUri() {
    return mediaUri;
  }

  @NonNull public MediaMetadataCompat getMediaMetadata() {
    return metadata;
  }

  @NonNull public MediaDescriptionCompat getMediaDescription() {
    return getMediaMetadata().getDescription();
  }

  public void setAlbumArt(final @Nullable Bitmap bitmap) {
    metadata = new MediaMetadataCompat.Builder(metadata)
        .putBitmap(MediaMetadataCompat.METADATA_KEY_ALBUM_ART, bitmap)
        .build();
  }

  public void setDisplayIcon(final @Nullable Bitmap bitmap) {
    metadata = new MediaMetadataCompat.Builder(metadata)
        .putBitmap(MediaMetadataCompat.METADATA_KEY_DISPLAY_ICON, bitmap)
        .build();
  }
}

@@ -255,16 +255,40 @@
 Bitmap;%0A
+import android.net.Uri;%0A
 import a
@@ -1288,24 +1288,658 @@
 _URI);%0A  %7D%0A%0A
+  public static MediaItem make(final @NonNull String mediaUri, final @NonNull Uri pageUri,%0A                               final @NonNull String title, final @Nullable String subtitle) %7B%0A    final MediaMetadataCompat metadata = new MediaMetadataCompat.Builder()%0A        .putString(MediaMetadataCompat.METADATA_KEY_TITLE, title)%0A        .putString(MediaMetadataCompat.METADATA_KEY_DISPLAY_TITLE, title)%0A        .putString(MediaMetadataCompat.METADATA_KEY_DISPLAY_SUBTITLE, subtitle)%0A        .putString(MediaMetadataCompat.METADATA_KEY_MEDIA_URI, mediaUri)%0A        .build();%0A%0A    return new MediaItem(metadata, pageUri.toString());%0A  %7D%0A%0A
   @Nullable 

Add static factory method to quickly create a MediaItem