From: Kyrylo Mikos Date: Sun, 10 Nov 2013 17:37:41 +0000 (+0200) Subject: Apollo: update Last.fm api X-Git-Tag: android-x86-6.0-r1~309^2~7 X-Git-Url: http://git.osdn.net/view?a=commitdiff_plain;h=f19820982c7f6b1e601940f2757311c2f8f73f01;p=android-x86%2Fpackages-apps-Eleven.git Apollo: update Last.fm api 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 --- diff --git a/src/com/andrew/apollo/cache/ImageFetcher.java b/src/com/andrew/apollo/cache/ImageFetcher.java index e86234d..0c41482 100644 --- a/src/com/andrew/apollo/cache/ImageFetcher.java +++ b/src/com/andrew/apollo/cache/ImageFetcher.java @@ -21,10 +21,8 @@ import com.andrew.apollo.Config; 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; @@ -36,7 +34,6 @@ import java.io.IOException; 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. @@ -95,6 +92,18 @@ public class ImageFetcher extends ImageWorker { 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} */ @@ -105,17 +114,9 @@ public class ImageFetcher extends ImageWorker { case ARTIST: if (!TextUtils.isEmpty(artistName)) { if (PreferenceUtils.getInstance(mContext).downloadMissingArtistImages()) { - final PaginatedResult paginatedResult = Artist.getImages(mContext, - artistName); - if (paginatedResult != null) { - final Iterator 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); } } } @@ -128,10 +129,7 @@ public class ImageFetcher extends ImageWorker { 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); } } } diff --git a/src/com/andrew/apollo/lastfm/Artist.java b/src/com/andrew/apollo/lastfm/Artist.java index 6644db1..9f06705 100644 --- a/src/com/andrew/apollo/lastfm/Artist.java +++ b/src/com/andrew/apollo/lastfm/Artist.java @@ -49,12 +49,10 @@ public class Artist extends MusicEntry { * 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); } /** @@ -62,9 +60,6 @@ public class Artist extends MusicEntry { * * @param artistOrMbid Name of the artist or an mbid * @param locale The language to fetch info in, or null - * @param username The username for the context of the request, or - * null. If supplied, the user's playcount for this - * artist is included in the response * @param apiKey The API key * @return detailed artist info */ @@ -107,41 +102,6 @@ public class Artist extends MusicEntry { } } - /** - * 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 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 getImages(final Context context, - final String artistOrMbid, final int page, final int limit, final String apiKey) { - final Map params = new WeakHashMap(); - 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 { /** diff --git a/src/com/andrew/apollo/lastfm/Image.java b/src/com/andrew/apollo/lastfm/Image.java index b54aa7b..ec09701 100644 --- a/src/com/andrew/apollo/lastfm/Image.java +++ b/src/com/andrew/apollo/lastfm/Image.java @@ -50,24 +50,7 @@ public class Image extends ImageHolder { 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; } } diff --git a/src/com/andrew/apollo/lastfm/ImageHolder.java b/src/com/andrew/apollo/lastfm/ImageHolder.java index 5cb24b1..69c7c4d 100644 --- a/src/com/andrew/apollo/lastfm/ImageHolder.java +++ b/src/com/andrew/apollo/lastfm/ImageHolder.java @@ -67,7 +67,7 @@ public abstract class ImageHolder { 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)); diff --git a/src/com/andrew/apollo/lastfm/ImageSize.java b/src/com/andrew/apollo/lastfm/ImageSize.java index 927d75d..f568feb 100644 --- a/src/com/andrew/apollo/lastfm/ImageSize.java +++ b/src/com/andrew/apollo/lastfm/ImageSize.java @@ -26,6 +26,6 @@ package com.andrew.apollo.lastfm; */ public enum ImageSize { - LARGE, LARGESQUARE, ORIGINAL, EXTRALARGE + SMALL, MEDIUM, LARGE, EXTRALARGE, MEGA, UNKNOWN }