OSDN Git Service

3832c6eaf444beb9cf5c0610cedca33cfa4026de
[android-x86/packages-apps-Eleven.git] / src / com / cyngn / eleven / cache / ImageFetcher.java
1 /*
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.
10  */
11
12 package com.cyngn.eleven.cache;
13
14 import android.content.Context;
15 import android.graphics.Bitmap;
16 import android.widget.ImageView;
17
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;
23
24 /**
25  * A subclass of {@link ImageWorker} that fetches images from a URL.
26  */
27 public class ImageFetcher extends ImageWorker {
28
29     private static ImageFetcher sInstance = null;
30
31     /**
32      * Creates a new instance of {@link ImageFetcher}.
33      *
34      * @param context The {@link Context} to use.
35      */
36     public ImageFetcher(final Context context) {
37         super(context);
38     }
39
40     /**
41      * Used to create a singleton of the image fetcher
42      *
43      * @param context The {@link Context} to use
44      * @return A new instance of this class.
45      */
46     public static final ImageFetcher getInstance(final Context context) {
47         if (sInstance == null) {
48             sInstance = new ImageFetcher(context.getApplicationContext());
49         }
50         return sInstance;
51     }
52
53     /**
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
57      */
58     public void loadPlaylistArtistImage(final long playlistId, final ImageView imageView) {
59         loadPlaylistImage(playlistId, PlaylistWorkerType.Artist, imageView);
60     }
61
62     /**
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
66      */
67     public void loadPlaylistCoverArtImage(final long playlistId, final ImageView imageView) {
68         loadPlaylistImage(playlistId, PlaylistWorkerType.CoverArt, imageView);
69     }
70
71     /**
72      * Used to fetch album images.
73      */
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,
77                 ImageType.ALBUM);
78     }
79
80     /**
81      * Used to fetch the current artwork.
82      */
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);
87     }
88
89     /**
90      * Used to fetch the current artwork blurred.
91      */
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);
96     }
97
98     /**
99      * Used to fetch artist images.
100      */
101     public void loadArtistImage(final String key, final ImageView imageView) {
102         loadImage(key, key, null, -1, imageView, ImageType.ARTIST);
103     }
104
105     /**
106      * Used to fetch the current artist image.
107      */
108     public void loadCurrentArtistImage(final ImageView imageView) {
109         loadImage(MusicUtils.getArtistName(), MusicUtils.getArtistName(), null, -1, imageView,
110                 ImageType.ARTIST);
111     }
112
113     /**
114      * @param pause True to temporarily pause the disk cache, false otherwise.
115      */
116     public void setPauseDiskCache(final boolean pause) {
117         if (mImageCache != null) {
118             mImageCache.setPauseDiskCache(pause);
119         }
120     }
121
122     /**
123      * Clears the disk and memory caches
124      */
125     public void clearCaches() {
126         if (mImageCache != null) {
127             mImageCache.clearCaches();
128         }
129     }
130
131     /**
132      * @param key The key used to find the image to remove
133      */
134     public void removeFromCache(final String key) {
135         if (mImageCache != null) {
136             mImageCache.removeFromCache(key);
137         }
138     }
139
140     /**
141      * @param key The key used to find the image to return
142      */
143     public Bitmap getCachedBitmap(final String key) {
144         if (mImageCache != null) {
145             return mImageCache.getCachedBitmap(key);
146         }
147         return getDefaultArtwork();
148     }
149
150     /**
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
153      */
154     public Bitmap getCachedArtwork(final String keyAlbum, final String keyArtist) {
155         return getCachedArtwork(keyAlbum, keyArtist,
156                 MusicUtils.getIdForAlbum(mContext, keyAlbum, keyArtist));
157     }
158
159     /**
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
163      */
164     public Bitmap getCachedArtwork(final String keyAlbum, final String keyArtist,
165                                    final long keyId) {
166         if (mImageCache != null) {
167             return mImageCache.getCachedArtwork(mContext,
168                     generateAlbumCacheKey(keyAlbum, keyArtist),
169                     keyId);
170         }
171         return getDefaultArtwork();
172     }
173
174     /**
175      * Finds cached or downloads album art. Used in {@link MusicPlaybackService}
176      * to set the current album art in the notification and lock screen
177      *
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
181      *                   missing artwork
182      * @return The album art as an {@link Bitmap}
183      */
184     public Bitmap getArtwork(final String albumName, final long albumId, final String artistName) {
185         // Check the disk cache
186         Bitmap artwork = null;
187
188         if (artwork == null && albumName != null && mImageCache != null) {
189             artwork = mImageCache.getBitmapFromDiskCache(
190                     generateAlbumCacheKey(albumName, artistName));
191         }
192         if (artwork == null && albumId >= 0 && mImageCache != null) {
193             // Check for local artwork
194             artwork = mImageCache.getArtworkFromFile(mContext, albumId);
195         }
196         if (artwork != null) {
197             return artwork;
198         }
199         return getDefaultArtwork();
200     }
201
202     /**
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
205      * same artist.
206      *
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.
209      * @return
210      */
211     public static String generateAlbumCacheKey(final String albumName, final String artistName) {
212         if (albumName == null || artistName == null) {
213             return null;
214         }
215         return new StringBuilder(albumName)
216                 .append("_")
217                 .append(artistName)
218                 .append("_")
219                 .append(Config.ALBUM_ART_SUFFIX)
220                 .toString();
221     }
222 }