content: package com.alium.orin.ui.activities.tageditor;
  
  import android.graphics.Bitmap;
  import android.graphics.BitmapFactory;
  import android.graphics.Color;
  import android.graphics.drawable.Drawable;
  import android.net.Uri;
  import android.os.Bundle;
  import android.support.annotation.NonNull;
  import android.text.Editable;
  import android.text.TextUtils;
  import android.text.TextWatcher;
  import android.widget.EditText;
  import android.widget.Toast;
  
  import com.alium.orin.glide.palette.BitmapPaletteWrapper;
  import com.alium.orin.lastfm.rest.model.LastFmAlbum;
  import com.alium.orin.loader.AlbumLoader;
  import com.alium.orin.model.Song;
  import com.alium.orin.util.LastFMUtil;
  import com.alium.orin.util.PhonographColorUtil;
  import com.bumptech.glide.Glide;
  import com.bumptech.glide.load.engine.DiskCacheStrategy;
  import com.bumptech.glide.request.animation.GlideAnimation;
  import com.bumptech.glide.request.target.SimpleTarget;
  import com.kabouzeid.appthemehelper.util.ATHUtil;
  import com.kabouzeid.appthemehelper.util.ToolbarContentTintHelper;
  import com.alium.orin.R;
  import com.alium.orin.glide.palette.BitmapPaletteTranscoder;
  import com.alium.orin.lastfm.rest.LastFMRestClient;
  
  import org.jaudiotagger.tag.FieldKey;
  
  import java.util.ArrayList;
  import java.util.EnumMap;
  import java.util.List;
  import java.util.Map;
  
  import butterknife.BindView;
  import butterknife.ButterKnife;
  import retrofit2.Call;
  import retrofit2.Callback;
  import retrofit2.Response;
  
  public class AlbumTagEditorActivity extends AbsTagEditorActivity implements TextWatcher {
  
      public static final String TAG = AlbumTagEditorActivity.class.getSimpleName();
  
      @BindView(R.id.title)
      EditText albumTitle;
      @BindView(R.id.album_artist)
      EditText albumArtist;
      @BindView(R.id.genre)
      EditText genre;
      @BindView(R.id.year)
      EditText year;
  
      private Bitmap albumArtBitmap;
      private boolean deleteAlbumArt;
      private LastFMRestClient lastFMRestClient;
  
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          ButterKnife.bind(this);
  
          lastFMRestClient = new LastFMRestClient(this);
  
          setUpViews();
      }
  
      private void setUpViews() {
          fillViewsWithFileTags();
          albumTitle.addTextChangedListener(this);
          albumArtist.addTextChangedListener(this);
          genre.addTextChangedListener(this);
          year.addTextChangedListener(this);
      }
  
  
      private void fillViewsWithFileTags() {
          albumTitle.setText(getAlbumTitle());
          albumArtist.setText(getAlbumArtistName());
          genre.setText(getGenreName());
          year.setText(getSongYear());
      }
  
      @Override
      protected void loadCurrentImage() {
          Bitmap bitmap = getAlbumArt();
          setImageBitmap(bitmap, PhonographColorUtil.getColor(PhonographColorUtil.generatePalette(bitmap), ATHUtil.resolveColor(this, R.attr.defaultFooterColor)));
          deleteAlbumArt = false;
      }
  
      @Override
      protected void getImageFromLastFM() {
          String albumTitleStr = albumTitle.getText().toString();
          String albumArtistNameStr = albumArtist.getText().toString();
          if (albumArtistNameStr.trim().equals("") || albumTitleStr.trim().equals("")) {
              Toast.makeText(this, getResources().getString(R.string.album_or_artist_empty), Toast.LENGTH_SHORT).show();
              return;
          }
          lastFMRestClient.getApiService().getAlbumInfo(albumTitleStr, albumArtistNameStr, null).enqueue(new Callback<LastFmAlbum>() {
              @Override
              public void onResponse(Call<LastFmAlbum> call, Response<LastFmAlbum> response) {
                  LastFmAlbum lastFmAlbum = response.body();
                  if (lastFmAlbum.getAlbum() != null) {
                      String url = LastFMUtil.getLargestAlbumImageUrl(lastFmAlbum.getAlbum().getImage());
                      if (!TextUtils.isEmpty(url) && url.trim().length() > 0) {
                          Glide.with(AlbumTagEditorActivity.this)
                                  .load(url)
                                  .asBitmap()
                                  .transcode(new BitmapPaletteTranscoder(AlbumTagEditorActivity.this), BitmapPaletteWrapper.class)
                                  .diskCacheStrategy(DiskCacheStrategy.SOURCE)
                                  .error(R.drawable.default_album_art)
                                  .into(new SimpleTarget<BitmapPaletteWrapper>() {
                                      @Override
                                      public void onLoadFailed(Exception e, Drawable errorDrawable) {
                                          super.onLoadFailed(e, errorDrawable);
                                          e.printStackTrace();
                                          Toast.makeText(AlbumTagEditorActivity.this, e.toString(), Toast.LENGTH_LONG).show();
                                      }
  
                                      @Override
                                      public void onResourceReady(BitmapPaletteWrapper resource, GlideAnimation<? super BitmapPaletteWrapper> glideAnimation) {
                                          albumArtBitmap = getResizedAlbumCover(resource.getBitmap(), 2048);
                                          setImageBitmap(albumArtBitmap, PhonographColorUtil.getColor(resource.getPalette(), ATHUtil.resolveColor(AlbumTagEditorActivity.this, R.attr.defaultFooterColor)));
                                          deleteAlbumArt = false;
                                          dataChanged();
                                          setResult(RESULT_OK);
                                      }
                                  });
                          return;
                      }
                  }
                  toastLoadingFailed();
              }
  
              @Override
              public void onFailure(Call<LastFmAlbum> call, Throwable t) {
                  toastLoadingFailed();
              }
  
              private void toastLoadingFailed() {
                  Toast.makeText(AlbumTagEditorActivity.this,
                          R.string.could_not_download_album_cover, Toast.LENGTH_SHORT).show();
              }
          });
      }
  
      @Override
      protected void searchImageOnWeb() {
          searchWebFor(albumTitle.getText().toString(), albumArtist.getText().toString());
      }
  
      @Override
      protected void deleteImage() {
          setImageBitmap(BitmapFactory.decodeResource(getResources(), R.drawable.default_album_art), ATHUtil.resolveColor(this, R.attr.defaultFooterColor));
          deleteAlbumArt = true;
          dataChanged();
      }
  
      @Override
      protected void save() {
          Map<FieldKey, String> fieldKeyValueMap = new EnumMap<>(FieldKey.class);
          fieldKeyValueMap.put(FieldKey.ALBUM, albumTitle.getText().toString());
          //android seems not to recognize album_artist field so we additionally write the normal artist field
          fieldKeyValueMap.put(FieldKey.ARTIST, albumArtist.getText().toString());
          fieldKeyValueMap.put(FieldKey.ALBUM_ARTIST, albumArtist.getText().toString());
          fieldKeyValueMap.put(FieldKey.GENRE, genre.getText().toString());
          fieldKeyValueMap.put(FieldKey.YEAR, year.getText().toString());
  
          writeValuesToFiles(fieldKeyValueMap, deleteAlbumArt ? new ArtworkInfo(getId(), null) : albumArtBitmap == null ? null : new ArtworkInfo(getId(), albumArtBitmap));
      }
  
      @Override
      protected int getContentViewLayout() {
          return R.layout.activity_album_tag_editor;
      }
  
      @NonNull
      @Override
      protected List<String> getSongPaths() {
          ArrayList<Song> songs = AlbumLoader.getAlbum(this, getId()).songs;
          ArrayList<String> paths = new ArrayList<>(songs.size());
          for (Song song : songs) {
              paths.add(song.data);
          }
          return paths;
      }
  
      @Override
      protected void loadImageFromFile(@NonNull final Uri selectedFileUri) {
          Glide.with(AlbumTagEditorActivity.this)
                  .load(selectedFileUri)
                  .asBitmap()
                  .transcode(new BitmapPaletteTranscoder(AlbumTagEditorActivity.this), BitmapPaletteWrapper.class)
                  .diskCacheStrategy(DiskCacheStrategy.NONE)
                  .skipMemoryCache(true)
                  .into(new SimpleTarget<BitmapPaletteWrapper>() {
                      @Override
                      public void onLoadFailed(Exception e, Drawable errorDrawable) {
                          super.onLoadFailed(e, errorDrawable);
                          e.printStackTrace();
                          Toast.makeText(AlbumTagEditorActivity.this, e.toString(), Toast.LENGTH_LONG).show();
                      }
  
                      @Override
                      public void onResourceReady(BitmapPaletteWrapper resource, GlideAnimation<? super BitmapPaletteWrapper> glideAnimation) {
                          PhonographColorUtil.getColor(resource.getPalette(), Color.TRANSPARENT);
                          albumArtBitmap = getResizedAlbumCover(resource.getBitmap(), 2048);
                          setImageBitmap(albumArtBitmap, PhonographColorUtil.getColor(resource.getPalette(), ATHUtil.resolveColor(AlbumTagEditorActivity.this, R.attr.defaultFooterColor)));
                          deleteAlbumArt = false;
                          dataChanged();
                          setResult(RESULT_OK);
                      }
                  });
      }
  
      @Override
      public void beforeTextChanged(CharSequence s, int start, int count, int after) {
  
      }
  
      @Override
      public void onTextChanged(CharSequence s, int start, int before, int count) {
  
      }
  
      @Override
      public void afterTextChanged(Editable s) {
          dataChanged();
      }
  
      private static Bitmap getResizedAlbumCover(@NonNull Bitmap src, int maxForSmallerSize) {
          int width = src.getWidth();
          int height = src.getHeight();
  
          final int dstWidth;
          final int dstHeight;
  
          if (width < height) {
              if (maxForSmallerSize >= width) {
                  return src;
              }
              float ratio = (float) height / width;
              dstWidth = maxForSmallerSize;
              dstHeight = Math.round(maxForSmallerSize * ratio);
          } else {
              if (maxForSmallerSize >= height) {
                  return src;
              }
              float ratio = (float) width / height;
              dstWidth = Math.round(maxForSmallerSize * ratio);
              dstHeight = maxForSmallerSize;
          }
  
          return Bitmap.createScaledBitmap(src, dstWidth, dstHeight, false);
      }
  
      @Override
      protected void setColors(int color) {
          super.setColors(color);
          albumTitle.setTextColor(ToolbarContentTintHelper.toolbarTitleColor(this, color));
      }
  }
  