Update ImageSize class.
Artist.getImages is deprecated now. Remove it
and use Artist.getInfo to load images for Artist.
Use default album size to EXTRALARGE.
Add a function to check album size and fallback to smaller
one if does not exists,
Change-Id: I40e3d5c60397a620057619dca1a7ea3e5839c7de
import com.andrew.apollo.MusicPlaybackService;
import com.andrew.apollo.lastfm.Album;
import com.andrew.apollo.lastfm.Artist;
-import com.andrew.apollo.lastfm.Image;
+import com.andrew.apollo.lastfm.MusicEntry;
import com.andrew.apollo.lastfm.ImageSize;
-import com.andrew.apollo.lastfm.PaginatedResult;
-import com.andrew.apollo.utils.ApolloUtils;
import com.andrew.apollo.utils.MusicUtils;
import com.andrew.apollo.utils.PreferenceUtils;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
-import java.util.Iterator;
/**
* A subclass of {@link ImageWorker} that fetches images from a URL.
return null;
}
+ private static String getBestImage(MusicEntry e) {
+ final ImageSize[] QUALITY = {ImageSize.EXTRALARGE, ImageSize.LARGE, ImageSize.MEDIUM,
+ ImageSize.SMALL, ImageSize.UNKNOWN};
+ for(ImageSize q : QUALITY) {
+ String url = e.getImageURL(q);
+ if (url != null) {
+ return url;
+ }
+ }
+ return null;
+ }
+
/**
* {@inheritDoc}
*/
case ARTIST:
if (!TextUtils.isEmpty(artistName)) {
if (PreferenceUtils.getInstance(mContext).downloadMissingArtistImages()) {
- final PaginatedResult<Image> paginatedResult = Artist.getImages(mContext,
- artistName);
- if (paginatedResult != null) {
- final Iterator<Image> iterator = paginatedResult.pageResults.iterator();
- while (iterator.hasNext()) {
- final Image temp = iterator.next();
- final String url = temp.getImageURL(ImageSize.EXTRALARGE);
- if (url != null) {
- return url;
- }
- }
+ final Artist artist = Artist.getInfo(mContext, artistName);
+ if (artist != null) {
+ return getBestImage(artist);
}
}
}
final Album album = Album.getInfo(mContext, correction.getName(),
albumName);
if (album != null) {
- final String url = album.getImageURL(ImageSize.LARGE);
- if (url != null) {
- return url;
- }
+ return getBestImage(album);
}
}
}
* Retrieves detailed artist info for the given artist or mbid entry.
*
* @param artistOrMbid Name of the artist or an mbid
- * @param apiKey The API key
* @return detailed artist info
*/
- public final static Artist getInfo(final Context context, final String artistOrMbid,
- final String apiKey) {
- return getInfo(context, artistOrMbid, Locale.getDefault(), apiKey);
+ public final static Artist getInfo(final Context context, final String artistOrMbid) {
+ return getInfo(context, artistOrMbid, Locale.getDefault(), Config.LASTFM_API_KEY);
}
/**
*
* @param artistOrMbid Name of the artist or an mbid
* @param locale The language to fetch info in, or <code>null</code>
- * @param username The username for the context of the request, or
- * <code>null</code>. If supplied, the user's playcount for this
- * artist is included in the response
* @param apiKey The API key
* @return detailed artist info
*/
}
}
- /**
- * Get {@link Image}s for this artist in a variety of sizes.
- *
- * @param artistOrMbid The artist name in question
- * @return a list of {@link Image}s
- */
- public final static PaginatedResult<Image> getImages(final Context context,
- final String artistOrMbid) {
- return getImages(context, artistOrMbid, -1, -1, Config.LASTFM_API_KEY);
- }
-
- /**
- * Get {@link Image}s for this artist in a variety of sizes.
- *
- * @param artistOrMbid The artist name in question
- * @param page Which page of limit amount to display
- * @param limit How many to return. Defaults and maxes out at 50
- * @param apiKey A Last.fm API key
- * @return a list of {@link Image}s
- */
- public final static PaginatedResult<Image> getImages(final Context context,
- final String artistOrMbid, final int page, final int limit, final String apiKey) {
- final Map<String, String> params = new WeakHashMap<String, String>();
- params.put("artist", artistOrMbid);
- MapUtilities.nullSafePut(params, "page", page);
- MapUtilities.nullSafePut(params, "limit", limit);
- Result result = null;
- try {
- result = Caller.getInstance(context).call("artist.getImages", apiKey, params);
- } catch (final Exception ignored) {
- return null;
- }
- return ResponseBuilder.buildPaginatedResult(result, Image.class);
- }
-
private final static class ArtistFactory implements ItemFactory<Artist> {
/**
public Image createItemFromElement(final DomElement element) {
final Image i = new Image();
i.url = element.getChildText("url");
- final DomElement sizes = element.getChild("sizes");
- for (final DomElement image : sizes.getChildren("size")) {
- // code copied from ImageHolder.loadImages
- final String attribute = image.getAttribute("name");
- ImageSize size = null;
- if (attribute == null) {
- size = ImageSize.LARGESQUARE;
- } else {
- try {
- size = ImageSize.valueOf(attribute.toUpperCase(Locale.ENGLISH));
- } catch (final IllegalArgumentException e) {
- // if they suddenly again introduce a new image size
- }
- }
- if (size != null) {
- i.imageUrls.put(size, image.getText());
- }
- }
+ loadImages(i, element);
return i;
}
}
final String attribute = image.getAttribute("size");
ImageSize size = null;
if (attribute == null) {
- size = ImageSize.LARGESQUARE;
+ size = ImageSize.UNKNOWN;
} else {
try {
size = ImageSize.valueOf(attribute.toUpperCase(Locale.ENGLISH));
*/
public enum ImageSize {
- LARGE, LARGESQUARE, ORIGINAL, EXTRALARGE
+ SMALL, MEDIUM, LARGE, EXTRALARGE, MEGA, UNKNOWN
}