2 * Copyright (C) 2012 Andrew Neal Licensed under the Apache License, Version 2.0
3 * (the "License"); you may not use this file except in compliance with the
4 * License. You may obtain a copy of the License at
5 * http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law
6 * or agreed to in writing, software distributed under the License is
7 * distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
8 * KIND, either express or implied. See the License for the specific language
9 * governing permissions and limitations under the License.
12 package com.cyngn.eleven.cache;
14 import android.content.Context;
15 import android.graphics.Bitmap;
16 import android.widget.ImageView;
18 import com.cyngn.eleven.Config;
19 import com.cyngn.eleven.MusicPlaybackService;
20 import com.cyngn.eleven.cache.PlaylistWorkerTask.PlaylistWorkerType;
21 import com.cyngn.eleven.utils.MusicUtils;
22 import com.cyngn.eleven.widgets.BlurScrimImage;
25 * A subclass of {@link ImageWorker} that fetches images from a URL.
27 public class ImageFetcher extends ImageWorker {
29 private static ImageFetcher sInstance = null;
32 * Creates a new instance of {@link ImageFetcher}.
34 * @param context The {@link Context} to use.
36 public ImageFetcher(final Context context) {
41 * Used to create a singleton of the image fetcher
43 * @param context The {@link Context} to use
44 * @return A new instance of this class.
46 public static final ImageFetcher getInstance(final Context context) {
47 if (sInstance == null) {
48 sInstance = new ImageFetcher(context.getApplicationContext());
54 * Loads a playlist's most played song's artist image
55 * @param playlistId id of the playlist
56 * @param imageView imageview to load into
58 public void loadPlaylistArtistImage(final long playlistId, final ImageView imageView) {
59 loadPlaylistImage(playlistId, PlaylistWorkerType.Artist, imageView);
63 * Loads a playlist's most played songs into a combined image, or show 1 if not enough images
64 * @param playlistId id of the playlist
65 * @param imageView imageview to load into
67 public void loadPlaylistCoverArtImage(final long playlistId, final ImageView imageView) {
68 loadPlaylistImage(playlistId, PlaylistWorkerType.CoverArt, imageView);
72 * Used to fetch album images.
74 public void loadAlbumImage(final String artistName, final String albumName, final long albumId,
75 final ImageView imageView) {
76 loadImage(generateAlbumCacheKey(albumName, artistName), artistName, albumName, albumId, imageView,
81 * Used to fetch the current artwork.
83 public void loadCurrentArtwork(final ImageView imageView) {
84 loadImage(generateAlbumCacheKey(MusicUtils.getAlbumName(), MusicUtils.getArtistName()),
85 MusicUtils.getArtistName(), MusicUtils.getAlbumName(), MusicUtils.getCurrentAlbumId(),
86 imageView, ImageType.ALBUM);
90 * Used to fetch the current artwork blurred.
92 public void loadCurrentBlurredArtwork(final BlurScrimImage image) {
93 loadBlurImage(generateAlbumCacheKey(MusicUtils.getAlbumName(), MusicUtils.getArtistName()),
94 MusicUtils.getArtistName(), MusicUtils.getAlbumName(), MusicUtils.getCurrentAlbumId(),
95 image, ImageType.ALBUM);
99 * Used to fetch artist images.
101 public void loadArtistImage(final String key, final ImageView imageView) {
102 loadImage(key, key, null, -1, imageView, ImageType.ARTIST);
106 * Used to fetch the current artist image.
108 public void loadCurrentArtistImage(final ImageView imageView) {
109 loadImage(MusicUtils.getArtistName(), MusicUtils.getArtistName(), null, -1, imageView,
114 * @param pause True to temporarily pause the disk cache, false otherwise.
116 public void setPauseDiskCache(final boolean pause) {
117 if (mImageCache != null) {
118 mImageCache.setPauseDiskCache(pause);
123 * Clears the disk and memory caches
125 public void clearCaches() {
126 if (mImageCache != null) {
127 mImageCache.clearCaches();
132 * @param key The key used to find the image to remove
134 public void removeFromCache(final String key) {
135 if (mImageCache != null) {
136 mImageCache.removeFromCache(key);
141 * @param key The key used to find the image to return
143 public Bitmap getCachedBitmap(final String key) {
144 if (mImageCache != null) {
145 return mImageCache.getCachedBitmap(key);
147 return getDefaultArtwork();
151 * @param keyAlbum The key (album name) used to find the album art to return
152 * @param keyArtist The key (artist name) used to find the album art to return
154 public Bitmap getCachedArtwork(final String keyAlbum, final String keyArtist) {
155 return getCachedArtwork(keyAlbum, keyArtist,
156 MusicUtils.getIdForAlbum(mContext, keyAlbum, keyArtist));
160 * @param keyAlbum The key (album name) used to find the album art to return
161 * @param keyArtist The key (artist name) used to find the album art to return
162 * @param keyId The key (album id) used to find the album art to return
164 public Bitmap getCachedArtwork(final String keyAlbum, final String keyArtist,
166 if (mImageCache != null) {
167 return mImageCache.getCachedArtwork(mContext,
168 generateAlbumCacheKey(keyAlbum, keyArtist),
171 return getDefaultArtwork();
175 * Finds cached or downloads album art. Used in {@link MusicPlaybackService}
176 * to set the current album art in the notification and lock screen
178 * @param albumName The name of the current album
179 * @param albumId The ID of the current album
180 * @param artistName The album artist in case we should have to download
182 * @return The album art as an {@link Bitmap}
184 public Bitmap getArtwork(final String albumName, final long albumId, final String artistName) {
185 // Check the disk cache
186 Bitmap artwork = null;
188 if (artwork == null && albumName != null && mImageCache != null) {
189 artwork = mImageCache.getBitmapFromDiskCache(
190 generateAlbumCacheKey(albumName, artistName));
192 if (artwork == null && albumId >= 0 && mImageCache != null) {
193 // Check for local artwork
194 artwork = mImageCache.getArtworkFromFile(mContext, albumId);
196 if (artwork != null) {
199 return getDefaultArtwork();
203 * Generates key used by album art cache. It needs both album name and artist name
204 * to let to select correct image for the case when there are two albums with the
207 * @param albumName The album name the cache key needs to be generated.
208 * @param artistName The artist name the cache key needs to be generated.
211 public static String generateAlbumCacheKey(final String albumName, final String artistName) {
212 if (albumName == null || artistName == null) {
215 return new StringBuilder(albumName)
219 .append(Config.ALBUM_ART_SUFFIX)