Renamed package to gramophone
This commit is contained in:
parent
200c50babf
commit
c28a75c61a
95 changed files with 412 additions and 415 deletions
|
|
@ -0,0 +1,9 @@
|
|||
package com.kabouzeid.gramophone.lastfm;
|
||||
|
||||
/**
|
||||
* Created by karim on 15.01.15.
|
||||
*/
|
||||
public class LastFMUtil {
|
||||
public static String BASE_URL = "ws.audioscrobbler.com";
|
||||
public static String API_KEY = "bd9c6ea4d55ec9ed3af7d276e5ece304";
|
||||
}
|
||||
|
|
@ -0,0 +1,110 @@
|
|||
package com.kabouzeid.gramophone.lastfm.album;
|
||||
|
||||
import android.content.Context;
|
||||
import android.graphics.Bitmap;
|
||||
import android.util.Log;
|
||||
import android.view.View;
|
||||
|
||||
import com.android.volley.Response;
|
||||
import com.android.volley.VolleyError;
|
||||
import com.android.volley.toolbox.JsonObjectRequest;
|
||||
import com.kabouzeid.gramophone.App;
|
||||
import com.kabouzeid.gramophone.provider.AlbumJSONStore;
|
||||
import com.kabouzeid.gramophone.util.Util;
|
||||
import com.nostra13.universalimageloader.core.DisplayImageOptions;
|
||||
import com.nostra13.universalimageloader.core.ImageLoader;
|
||||
import com.nostra13.universalimageloader.core.assist.FailReason;
|
||||
import com.nostra13.universalimageloader.core.listener.ImageLoadingListener;
|
||||
import com.nostra13.universalimageloader.core.process.BitmapProcessor;
|
||||
|
||||
import org.json.JSONException;
|
||||
import org.json.JSONObject;
|
||||
|
||||
/**
|
||||
* Created by karim on 01.01.15.
|
||||
*/
|
||||
public class LastFMAlbumImageLoader {
|
||||
public static final String TAG = LastFMAlbumImageLoader.class.getSimpleName();
|
||||
|
||||
public static void loadAlbumImage(Context context, String queryAlbum, String queryArtist, AlbumImageLoaderCallback callback) {
|
||||
if (queryAlbum != null) {
|
||||
String albumJSON = AlbumJSONStore.getInstance(context).getAlbumJSON(queryAlbum + queryArtist);
|
||||
if (albumJSON != null) {
|
||||
Log.i(TAG, queryAlbum + " by " + queryArtist + " is in cache.");
|
||||
try {
|
||||
loadAlbumImageFromJSON(new JSONObject(albumJSON), callback);
|
||||
} catch (JSONException e) {
|
||||
Log.e(TAG, "Error while parsing string from cache to JSONObject", e);
|
||||
}
|
||||
} else {
|
||||
Log.i(TAG, queryAlbum + " is not in cache.");
|
||||
downloadAlbumImage(context, queryAlbum, queryArtist, callback);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private static void loadAlbumImageFromJSON(JSONObject jsonObject, final AlbumImageLoaderCallback callback) {
|
||||
Log.i(TAG, "Applying album art...");
|
||||
String url = LastFMAlbumInfoUtil.getAlbumImageUrlFromJSON(jsonObject);
|
||||
if (!url.trim().equals("")) {
|
||||
DisplayImageOptions options = new DisplayImageOptions.Builder()
|
||||
.cacheInMemory(true)
|
||||
.cacheOnDisk(false)
|
||||
.postProcessor(new BitmapProcessor() {
|
||||
@Override
|
||||
public Bitmap process(Bitmap bmp) {
|
||||
return Util.getAlbumArtScaledBitmap(bmp, true);
|
||||
}
|
||||
})
|
||||
.build();
|
||||
ImageLoader.getInstance().loadImage(url, options, new ImageLoadingListener() {
|
||||
@Override
|
||||
public void onLoadingStarted(String imageUri, View view) {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onLoadingFailed(String imageUri, View view, FailReason failReason) {
|
||||
callback.onAlbumImageLoaded(null, null);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onLoadingComplete(String imageUri, View view, Bitmap loadedImage) {
|
||||
callback.onAlbumImageLoaded(loadedImage, imageUri);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onLoadingCancelled(String imageUri, View view) {
|
||||
callback.onAlbumImageLoaded(null, null);
|
||||
}
|
||||
});
|
||||
} else {
|
||||
callback.onAlbumImageLoaded(null, null);
|
||||
}
|
||||
}
|
||||
|
||||
private static void downloadAlbumImage(final Context context, final String album, final String artist, final AlbumImageLoaderCallback callback) {
|
||||
Log.i(TAG, "Downloading details for " + album);
|
||||
App app = (App) context.getApplicationContext();
|
||||
String albumUrl = LastFMAlbumInfoUtil.getAlbumUrl(album, artist);
|
||||
JsonObjectRequest albumInfoJSONRequest = new JsonObjectRequest(0, albumUrl, null, new Response.Listener<JSONObject>() {
|
||||
@Override
|
||||
public void onResponse(JSONObject response) {
|
||||
Log.i(TAG, "Download was successful!");
|
||||
LastFMAlbumInfoUtil.saveAlbumJSONDataToCacheAndDisk(context, album, artist, response);
|
||||
loadAlbumImageFromJSON(response, callback);
|
||||
}
|
||||
}, new Response.ErrorListener() {
|
||||
@Override
|
||||
public void onErrorResponse(VolleyError error) {
|
||||
Log.e(TAG, "Download failed!", error);
|
||||
callback.onAlbumImageLoaded(null, null);
|
||||
}
|
||||
});
|
||||
app.addToVolleyRequestQueue(albumInfoJSONRequest);
|
||||
}
|
||||
|
||||
public static interface AlbumImageLoaderCallback {
|
||||
public void onAlbumImageLoaded(Bitmap albumImage, String uri);
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,87 @@
|
|||
package com.kabouzeid.gramophone.lastfm.album;
|
||||
|
||||
import android.content.Context;
|
||||
import android.net.Uri;
|
||||
import android.util.Log;
|
||||
|
||||
import com.kabouzeid.gramophone.lastfm.LastFMUtil;
|
||||
import com.kabouzeid.gramophone.provider.AlbumJSONStore;
|
||||
|
||||
import org.json.JSONArray;
|
||||
import org.json.JSONException;
|
||||
import org.json.JSONObject;
|
||||
|
||||
/**
|
||||
* Created by karim on 24.12.14.
|
||||
*/
|
||||
public class LastFMAlbumInfoUtil {
|
||||
public static final String TAG = LastFMAlbumInfoUtil.class.getSimpleName();
|
||||
|
||||
private static String AUTO_CORRECT = "1";
|
||||
|
||||
public static String getAlbumUrl(String album, String artist) {
|
||||
if (album != null) {
|
||||
Uri.Builder builder = new Uri.Builder();
|
||||
builder.scheme("http")
|
||||
.authority(LastFMUtil.BASE_URL)
|
||||
.appendPath("2.0")
|
||||
.appendQueryParameter("method", "album.getinfo")
|
||||
.appendQueryParameter("album", album)
|
||||
.appendQueryParameter("artist", artist)
|
||||
//.appendQueryParameter("lang", "de")
|
||||
.appendQueryParameter("autocorrect", AUTO_CORRECT)
|
||||
.appendQueryParameter("api_key", LastFMUtil.API_KEY)
|
||||
.appendQueryParameter("format", "json");
|
||||
return builder.build().toString();
|
||||
}
|
||||
return "";
|
||||
}
|
||||
|
||||
public static String getAlbumNameFromJSON(JSONObject rootJSON) {
|
||||
try {
|
||||
return rootJSON.getJSONObject("album").getString("name");
|
||||
} catch (JSONException e) {
|
||||
//Log.e(TAG, "Error while getting album name from JSON parameter!", e);
|
||||
return "";
|
||||
}
|
||||
}
|
||||
|
||||
public static String getAlbumThumbnailUrlFromJSON(JSONObject rootJSON) {
|
||||
try {
|
||||
JSONArray images = getAlbumImageArrayFromJSON(rootJSON);
|
||||
if (images.length() > 2) {
|
||||
return images.getJSONObject(2).getString("#text");
|
||||
} else if (images.length() > 1) {
|
||||
return images.getJSONObject(1).getString("#text");
|
||||
}
|
||||
return images.getJSONObject(0).getString("#text");
|
||||
} catch (JSONException | NullPointerException e) {
|
||||
//Log.e(TAG, "Error while getting album thumbnail image from JSON parameter!", e);
|
||||
return "";
|
||||
}
|
||||
}
|
||||
|
||||
public static JSONArray getAlbumImageArrayFromJSON(JSONObject rootJSON) {
|
||||
try {
|
||||
return rootJSON.getJSONObject("album").getJSONArray("image");
|
||||
} catch (JSONException e) {
|
||||
//Log.e(TAG, "Error while getting album image array from JSON parameter!", e);
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
public static String getAlbumImageUrlFromJSON(JSONObject rootJSON) {
|
||||
try {
|
||||
JSONArray images = getAlbumImageArrayFromJSON(rootJSON);
|
||||
return images.getJSONObject(images.length() - 1).getString("#text");
|
||||
} catch (JSONException | NullPointerException e) {
|
||||
//Log.e(TAG, "Error while getting album image from JSON parameter!", e);
|
||||
return "";
|
||||
}
|
||||
}
|
||||
|
||||
public static void saveAlbumJSONDataToCacheAndDisk(Context context, String album, String artist, JSONObject jsonObject) {
|
||||
Log.i(TAG, "Saving new JSON album data for " + album + "...");
|
||||
AlbumJSONStore.getInstance(context).addAlbumJSON(album + artist, jsonObject.toString());
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,65 @@
|
|||
package com.kabouzeid.gramophone.lastfm.artist;
|
||||
|
||||
import android.content.Context;
|
||||
import android.util.Log;
|
||||
|
||||
import com.android.volley.Response;
|
||||
import com.android.volley.VolleyError;
|
||||
import com.android.volley.toolbox.JsonObjectRequest;
|
||||
import com.kabouzeid.gramophone.App;
|
||||
import com.kabouzeid.gramophone.provider.ArtistJSONStore;
|
||||
|
||||
import org.json.JSONException;
|
||||
import org.json.JSONObject;
|
||||
|
||||
/**
|
||||
* Created by karim on 01.01.15.
|
||||
*/
|
||||
public class LastFMArtistBiographyLoader {
|
||||
public static final String TAG = LastFMArtistBiographyLoader.class.getSimpleName();
|
||||
|
||||
public static void loadArtistBio(Context context, String queryArtist, ArtistBioLoaderCallback callback) {
|
||||
if (queryArtist != null) {
|
||||
String artistJSON = ArtistJSONStore.getInstance(context).getArtistJSON(queryArtist);
|
||||
if (artistJSON != null) {
|
||||
Log.i(TAG, queryArtist + " is in cache.");
|
||||
try {
|
||||
JSONObject json = new JSONObject(artistJSON);
|
||||
String bio = LastFMArtistInfoUtil.getArtistBiographyFromJSON(json);
|
||||
callback.onArtistBioLoaded(bio);
|
||||
} catch (JSONException e) {
|
||||
Log.e(TAG, "Error while parsing bio from cache to JSONObject", e);
|
||||
}
|
||||
} else {
|
||||
Log.i(TAG, queryArtist + " is not in cache.");
|
||||
downloadArtistBio(context, queryArtist, callback);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private static void downloadArtistBio(final Context context, final String artist, final ArtistBioLoaderCallback callback) {
|
||||
Log.i(TAG, "Downloading details for " + artist);
|
||||
App app = (App) context.getApplicationContext();
|
||||
String artistUrl = LastFMArtistInfoUtil.getArtistUrl(artist);
|
||||
JsonObjectRequest artistInfoJSONRequest = new JsonObjectRequest(0, artistUrl, null, new Response.Listener<JSONObject>() {
|
||||
@Override
|
||||
public void onResponse(JSONObject response) {
|
||||
Log.i(TAG, "Download was successful!");
|
||||
LastFMArtistInfoUtil.saveArtistJSONDataToCacheAndDisk(context, artist, response);
|
||||
String bio = LastFMArtistInfoUtil.getArtistBiographyFromJSON(response);
|
||||
callback.onArtistBioLoaded(bio);
|
||||
}
|
||||
}, new Response.ErrorListener() {
|
||||
@Override
|
||||
public void onErrorResponse(VolleyError error) {
|
||||
Log.e(TAG, "Download failed!", error);
|
||||
callback.onArtistBioLoaded("");
|
||||
}
|
||||
});
|
||||
app.addToVolleyRequestQueue(artistInfoJSONRequest);
|
||||
}
|
||||
|
||||
public static interface ArtistBioLoaderCallback {
|
||||
public void onArtistBioLoaded(String bio);
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,107 @@
|
|||
package com.kabouzeid.gramophone.lastfm.artist;
|
||||
|
||||
import android.content.Context;
|
||||
import android.graphics.Bitmap;
|
||||
import android.util.Log;
|
||||
import android.view.View;
|
||||
|
||||
import com.android.volley.Response;
|
||||
import com.android.volley.VolleyError;
|
||||
import com.android.volley.toolbox.JsonObjectRequest;
|
||||
import com.kabouzeid.gramophone.App;
|
||||
import com.kabouzeid.gramophone.provider.ArtistJSONStore;
|
||||
import com.kabouzeid.gramophone.util.ImageLoaderUtil;
|
||||
import com.nostra13.universalimageloader.core.ImageLoader;
|
||||
import com.nostra13.universalimageloader.core.assist.FailReason;
|
||||
import com.nostra13.universalimageloader.core.listener.ImageLoadingListener;
|
||||
|
||||
import org.json.JSONException;
|
||||
import org.json.JSONObject;
|
||||
|
||||
/**
|
||||
* Created by karim on 01.01.15.
|
||||
*/
|
||||
public class LastFMArtistImageLoader {
|
||||
public static final String TAG = LastFMArtistImageLoader.class.getSimpleName();
|
||||
|
||||
@Deprecated
|
||||
public static void loadArtistImage(Context context, String queryArtist, ArtistImageLoaderCallback callback) {
|
||||
loadArtistImage(context, queryArtist, false, callback);
|
||||
}
|
||||
|
||||
public static void loadArtistImage(Context context, String queryArtist, boolean forceDownload, ArtistImageLoaderCallback callback) {
|
||||
if (queryArtist != null) {
|
||||
String artistJSON = ArtistJSONStore.getInstance(context).getArtistJSON(queryArtist);
|
||||
if (artistJSON != null && !forceDownload) {
|
||||
Log.i(TAG, queryArtist + " is in cache.");
|
||||
try {
|
||||
loadArtistImageFromJSON(new JSONObject(artistJSON), callback);
|
||||
} catch (JSONException e) {
|
||||
Log.e(TAG, "Error while parsing string from cache to JSONObject", e);
|
||||
}
|
||||
} else {
|
||||
if(forceDownload){
|
||||
Log.i(TAG, queryArtist + " force re-download");
|
||||
} else {
|
||||
Log.i(TAG, queryArtist + " is not in cache.");
|
||||
}
|
||||
downloadArtistJSONAndStartImageDownload(context, queryArtist, callback);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private static void loadArtistImageFromJSON(JSONObject jsonObject, final ArtistImageLoaderCallback callback) {
|
||||
Log.i(TAG, "Applying artist art...");
|
||||
String url = LastFMArtistInfoUtil.getArtistImageUrlFromJSON(jsonObject);
|
||||
if (!url.trim().equals("")) {
|
||||
ImageLoader.getInstance().loadImage(url, ImageLoaderUtil.getCacheOnDiskOptions(), new ImageLoadingListener() {
|
||||
@Override
|
||||
public void onLoadingStarted(String imageUri, View view) {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onLoadingFailed(String imageUri, View view, FailReason failReason) {
|
||||
callback.onArtistImageLoaded(null);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onLoadingComplete(String imageUri, View view, Bitmap loadedImage) {
|
||||
callback.onArtistImageLoaded(loadedImage);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onLoadingCancelled(String imageUri, View view) {
|
||||
callback.onArtistImageLoaded(null);
|
||||
}
|
||||
});
|
||||
} else {
|
||||
callback.onArtistImageLoaded(null);
|
||||
}
|
||||
}
|
||||
|
||||
private static void downloadArtistJSONAndStartImageDownload(final Context context, final String artist, final ArtistImageLoaderCallback callback) {
|
||||
Log.i(TAG, "Downloading details for " + artist);
|
||||
App app = (App) context.getApplicationContext();
|
||||
String artistUrl = LastFMArtistInfoUtil.getArtistUrl(artist);
|
||||
JsonObjectRequest artistInfoJSONRequest = new JsonObjectRequest(0, artistUrl, null, new Response.Listener<JSONObject>() {
|
||||
@Override
|
||||
public void onResponse(JSONObject response) {
|
||||
Log.i(TAG, "Download was successful!");
|
||||
LastFMArtistInfoUtil.saveArtistJSONDataToCacheAndDisk(context, artist, response);
|
||||
loadArtistImageFromJSON(response, callback);
|
||||
}
|
||||
}, new Response.ErrorListener() {
|
||||
@Override
|
||||
public void onErrorResponse(VolleyError error) {
|
||||
Log.e(TAG, "Download failed!", error);
|
||||
callback.onArtistImageLoaded(null);
|
||||
}
|
||||
});
|
||||
app.addToVolleyRequestQueue(artistInfoJSONRequest);
|
||||
}
|
||||
|
||||
public static interface ArtistImageLoaderCallback {
|
||||
public void onArtistImageLoaded(Bitmap artistImage);
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,96 @@
|
|||
package com.kabouzeid.gramophone.lastfm.artist;
|
||||
|
||||
import android.content.Context;
|
||||
import android.net.Uri;
|
||||
import android.util.Log;
|
||||
|
||||
import com.kabouzeid.gramophone.lastfm.LastFMUtil;
|
||||
import com.kabouzeid.gramophone.provider.ArtistJSONStore;
|
||||
|
||||
import org.json.JSONArray;
|
||||
import org.json.JSONException;
|
||||
import org.json.JSONObject;
|
||||
|
||||
/**
|
||||
* Created by karim on 24.12.14.
|
||||
*/
|
||||
public class LastFMArtistInfoUtil {
|
||||
public static final String TAG = LastFMArtistInfoUtil.class.getSimpleName();
|
||||
|
||||
private static String AUTO_CORRECT = "1";
|
||||
|
||||
public static String getArtistUrl(String artist) {
|
||||
if (artist != null) {
|
||||
Uri.Builder builder = new Uri.Builder();
|
||||
builder.scheme("http")
|
||||
.authority(LastFMUtil.BASE_URL)
|
||||
.appendPath("2.0")
|
||||
.appendQueryParameter("method", "artist.getinfo")
|
||||
.appendQueryParameter("artist", artist)
|
||||
//.appendQueryParameter("lang", "de")
|
||||
.appendQueryParameter("autocorrect", AUTO_CORRECT)
|
||||
.appendQueryParameter("api_key", LastFMUtil.API_KEY)
|
||||
.appendQueryParameter("format", "json");
|
||||
return builder.build().toString();
|
||||
}
|
||||
return "";
|
||||
}
|
||||
|
||||
public static String getArtistNameFromJSON(JSONObject rootJSON) {
|
||||
try {
|
||||
return rootJSON.getJSONObject("artist").getString("name");
|
||||
} catch (JSONException e) {
|
||||
//Log.e(TAG, "Error while getting artist name from JSON parameter!", e);
|
||||
return "";
|
||||
}
|
||||
}
|
||||
|
||||
public static String getArtistThumbnailUrlFromJSON(JSONObject rootJSON) {
|
||||
try {
|
||||
JSONArray images = getArtistImageArrayFromJSON(rootJSON);
|
||||
if (images.length() > 2) {
|
||||
return images.getJSONObject(2).getString("#text");
|
||||
} else if (images.length() > 1) {
|
||||
return images.getJSONObject(1).getString("#text");
|
||||
}
|
||||
return images.getJSONObject(0).getString("#text");
|
||||
} catch (JSONException | NullPointerException e) {
|
||||
//Log.e(TAG, "Error while getting artist thumbnail image from JSON parameter!", e);
|
||||
return "";
|
||||
}
|
||||
}
|
||||
|
||||
public static JSONArray getArtistImageArrayFromJSON(JSONObject rootJSON) {
|
||||
try {
|
||||
return rootJSON.getJSONObject("artist").getJSONArray("image");
|
||||
} catch (JSONException e) {
|
||||
//Log.e(TAG, "Error while getting artist image array from JSON parameter!", e);
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
public static String getArtistImageUrlFromJSON(JSONObject rootJSON) {
|
||||
try {
|
||||
JSONArray images = getArtistImageArrayFromJSON(rootJSON);
|
||||
return images.getJSONObject(images.length() - 1).getString("#text");
|
||||
} catch (JSONException | NullPointerException e) {
|
||||
//Log.e(TAG, "Error while getting artist image from JSON parameter!", e);
|
||||
return "";
|
||||
}
|
||||
}
|
||||
|
||||
public static String getArtistBiographyFromJSON(JSONObject rootJSON) {
|
||||
try {
|
||||
return rootJSON.getJSONObject("artist").getJSONObject("bio").getString("content");
|
||||
} catch (JSONException e) {
|
||||
//Log.e(TAG, "Error while getting artist biografie from JSON parameter!", e);
|
||||
return "";
|
||||
}
|
||||
}
|
||||
|
||||
public static void saveArtistJSONDataToCacheAndDisk(Context context, String artist, JSONObject jsonObject) {
|
||||
Log.i(TAG, "Saving new JSON artist data for " + artist + "...");
|
||||
ArtistJSONStore.getInstance(context).removeItem(artist);
|
||||
ArtistJSONStore.getInstance(context).addArtistJSON(artist, jsonObject.toString());
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,107 @@
|
|||
package com.kabouzeid.gramophone.lastfm.artist;
|
||||
|
||||
import android.content.Context;
|
||||
import android.graphics.Bitmap;
|
||||
import android.util.Log;
|
||||
import android.view.View;
|
||||
|
||||
import com.android.volley.Response;
|
||||
import com.android.volley.VolleyError;
|
||||
import com.android.volley.toolbox.JsonObjectRequest;
|
||||
import com.kabouzeid.gramophone.App;
|
||||
import com.kabouzeid.gramophone.provider.ArtistJSONStore;
|
||||
import com.kabouzeid.gramophone.util.ImageLoaderUtil;
|
||||
import com.nostra13.universalimageloader.core.ImageLoader;
|
||||
import com.nostra13.universalimageloader.core.assist.FailReason;
|
||||
import com.nostra13.universalimageloader.core.listener.ImageLoadingListener;
|
||||
|
||||
import org.json.JSONException;
|
||||
import org.json.JSONObject;
|
||||
|
||||
/**
|
||||
* Created by karim on 01.01.15.
|
||||
*/
|
||||
public class LastFMArtistThumbnailLoader {
|
||||
public static final String TAG = LastFMArtistThumbnailLoader.class.getSimpleName();
|
||||
|
||||
@Deprecated
|
||||
public static void loadArtistThumbnail(Context context, String queryArtist, ArtistThumbnailLoaderCallback callback) {
|
||||
loadArtistThumbnail(context, queryArtist, false, callback);
|
||||
}
|
||||
|
||||
public static void loadArtistThumbnail(Context context, String queryArtist, boolean forceDownload, ArtistThumbnailLoaderCallback callback) {
|
||||
if (queryArtist != null) {
|
||||
String artistJSON = ArtistJSONStore.getInstance(context).getArtistJSON(queryArtist);
|
||||
if (artistJSON != null && !forceDownload) {
|
||||
Log.i(TAG, queryArtist + " is in cache.");
|
||||
try {
|
||||
loadArtistThumbnailFromJSON(new JSONObject(artistJSON), callback);
|
||||
} catch (JSONException e) {
|
||||
Log.e(TAG, "Error while parsing string from cache to JSONObject", e);
|
||||
}
|
||||
} else {
|
||||
if(forceDownload){
|
||||
Log.i(TAG, queryArtist + " force re-download");
|
||||
} else {
|
||||
Log.i(TAG, queryArtist + " is not in cache.");
|
||||
}
|
||||
downloadArtistThumbnail(context, queryArtist, callback);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private static void loadArtistThumbnailFromJSON(JSONObject jsonObject, final ArtistThumbnailLoaderCallback callback) {
|
||||
Log.i(TAG, "Applying artist thumbnail...");
|
||||
String url = LastFMArtistInfoUtil.getArtistThumbnailUrlFromJSON(jsonObject);
|
||||
if (!url.trim().equals("")) {
|
||||
ImageLoader.getInstance().loadImage(url, ImageLoaderUtil.getCacheOnDiskOptions(), new ImageLoadingListener() {
|
||||
@Override
|
||||
public void onLoadingStarted(String imageUri, View view) {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onLoadingFailed(String imageUri, View view, FailReason failReason) {
|
||||
callback.onArtistThumbnailLoaded(null);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onLoadingComplete(String imageUri, View view, Bitmap loadedImage) {
|
||||
callback.onArtistThumbnailLoaded(loadedImage);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onLoadingCancelled(String imageUri, View view) {
|
||||
callback.onArtistThumbnailLoaded(null);
|
||||
}
|
||||
});
|
||||
} else {
|
||||
callback.onArtistThumbnailLoaded(null);
|
||||
}
|
||||
}
|
||||
|
||||
private static void downloadArtistThumbnail(final Context context, final String artist, final ArtistThumbnailLoaderCallback callback) {
|
||||
Log.i(TAG, "Downloading details for " + artist);
|
||||
App app = (App) context.getApplicationContext();
|
||||
String artistUrl = LastFMArtistInfoUtil.getArtistUrl(artist);
|
||||
JsonObjectRequest artistInfoJSONRequest = new JsonObjectRequest(0, artistUrl, null, new Response.Listener<JSONObject>() {
|
||||
@Override
|
||||
public void onResponse(JSONObject response) {
|
||||
Log.i(TAG, "Download was successful!");
|
||||
LastFMArtistInfoUtil.saveArtistJSONDataToCacheAndDisk(context, artist, response);
|
||||
loadArtistThumbnailFromJSON(response, callback);
|
||||
}
|
||||
}, new Response.ErrorListener() {
|
||||
@Override
|
||||
public void onErrorResponse(VolleyError error) {
|
||||
Log.e(TAG, "Download failed!", error);
|
||||
callback.onArtistThumbnailLoaded(null);
|
||||
}
|
||||
});
|
||||
app.addToVolleyRequestQueue(artistInfoJSONRequest);
|
||||
}
|
||||
|
||||
public static interface ArtistThumbnailLoaderCallback {
|
||||
public void onArtistThumbnailLoaded(Bitmap thumbnail);
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue