Android Open Source - music-tag Artwork Service






From Project

Back to project page music-tag.

License

The source code is released under:

Apache License

If you think the Android project music-tag listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package binauld.pierre.musictag.service;
/*from w  w w  .j  a v a2s  . co  m*/
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.widget.ImageView;

import binauld.pierre.musictag.decoder.BitmapDecoder;
import binauld.pierre.musictag.io.ArtworkLoader;
import binauld.pierre.musictag.io.AsyncDrawable;
import binauld.pierre.musictag.io.DefaultArtworkLoader;
import binauld.pierre.musictag.item.LibraryItem;

/**
 * Help to build artwork from audio file.
 */
public class ArtworkService {

    private BitmapDecoder defaultArtworkDecoder;

    private CacheService<Bitmap> cacheService;

    public ArtworkService(BitmapDecoder defaultArtworkDecoder) {
        this.cacheService = Locator.getCacheService();
        this.defaultArtworkDecoder = defaultArtworkDecoder;
    }

    public void initDefaultArtwork(int artworkSize) {
        DefaultArtworkLoader loader = new DefaultArtworkLoader(this.cacheService, artworkSize);
        loader.execute(this.defaultArtworkDecoder);
    }

    /**
     * Set the Artwork associated to item to the imageView.
     * If the Artwork has not been yet loaded, then it is loaded a placeholder is put in the image view while loading.
     *
     * @param item      Current item.
     * @param imageView Associated image view.
     */
    public void setArtwork(LibraryItem item, ImageView imageView, int artworkSize) {

        final String key = item.getDecoder().getKey(artworkSize, artworkSize);

        final Bitmap bitmap = cacheService.get(key);

        if (bitmap != null) {
            imageView.setImageBitmap(bitmap);
        } else if (cancelPotentialWork(item, imageView)) {
            Resources res = imageView.getResources();
            final ArtworkLoader task = new ArtworkLoader(imageView, cacheService, defaultArtworkDecoder, artworkSize);
            Bitmap placeholder = cacheService.get(defaultArtworkDecoder.getKey(artworkSize, artworkSize));
            final AsyncDrawable asyncDrawable = new AsyncDrawable(res, placeholder, task);
            imageView.setImageDrawable(asyncDrawable);
            task.execute(item);
        }
    }

    /**
     * Cancel a potential AsyncTask from anAsyncDrawable of the ImageView if the AsyncTask is outdated.
     *
     * @param item      The item that the artwork must be load.
     * @param imageView The image view which has to display artwork thumbnail.
     * @return False if the same work is in progress.
     */
    private boolean cancelPotentialWork(LibraryItem item, ImageView imageView) {
        final ArtworkLoader artworkLoader = AsyncDrawable.retrieveBitmapLoader(imageView);

        if (artworkLoader != null) {
            final LibraryItem taskItem = artworkLoader.getWorkingItem();
            // If bitmapData is not yet set or it differs from the new data
            if (taskItem == null || taskItem != item) {
                // Cancel previous task
                artworkLoader.cancel(true);
            } else {
                // The same work is already in progress
                return false;
            }
        }
        // No task associated with the ImageView, or an existing task was cancelled
        return true;
    }
}




Java Source Code List

binauld.pierre.musictag.ApplicationTest.java
binauld.pierre.musictag.activities.MainActivity.java
binauld.pierre.musictag.activities.SettingsActivity.java
binauld.pierre.musictag.activities.TagFormActivity.java
binauld.pierre.musictag.activities.TagSuggestionActivity.java
binauld.pierre.musictag.adapter.LibraryItemAdapter.java
binauld.pierre.musictag.adapter.SuggestionItemAdapter.java
binauld.pierre.musictag.adapter.SuggestionViewHolder.java
binauld.pierre.musictag.collection.LibraryItemComparator.java
binauld.pierre.musictag.collection.MultipleBufferedList.java
binauld.pierre.musictag.decoder.AudioFileBitmapDecoder.java
binauld.pierre.musictag.decoder.BitmapDecoder.java
binauld.pierre.musictag.decoder.ResourceBitmapDecoder.java
binauld.pierre.musictag.factory.FileFilterFactory.java
binauld.pierre.musictag.factory.LibraryItemFactory.java
binauld.pierre.musictag.fragments.SettingsFragment.java
binauld.pierre.musictag.helper.LibraryItemFactoryHelper.java
binauld.pierre.musictag.helper.LoaderHelper.java
binauld.pierre.musictag.io.ArtworkLoader.java
binauld.pierre.musictag.io.AsyncDrawable.java
binauld.pierre.musictag.io.DefaultArtworkLoader.java
binauld.pierre.musictag.io.LibraryItemLoaderManager.java
binauld.pierre.musictag.io.LibraryItemLoader.java
binauld.pierre.musictag.io.SuggestionLoader.java
binauld.pierre.musictag.item.AudioItem.java
binauld.pierre.musictag.item.ChildItem.java
binauld.pierre.musictag.item.FolderItem.java
binauld.pierre.musictag.item.LibraryItem.java
binauld.pierre.musictag.item.LoadingState.java
binauld.pierre.musictag.item.NodeItem.java
binauld.pierre.musictag.item.SuggestionItem.java
binauld.pierre.musictag.service.ArtworkService.java
binauld.pierre.musictag.service.CacheService.java
binauld.pierre.musictag.service.Locator.java
binauld.pierre.musictag.tag.Id3TagParcelable.java
binauld.pierre.musictag.tag.Id3Tag.java
binauld.pierre.musictag.tag.SupportedTag.java
binauld.pierre.musictag.widget.AnimatedProgressBar.java