diff options
Diffstat (limited to 'app/src/main/java/github/daneren2005/dsub/service/CachedMusicService.java')
-rw-r--r-- | app/src/main/java/github/daneren2005/dsub/service/CachedMusicService.java | 107 |
1 files changed, 88 insertions, 19 deletions
diff --git a/app/src/main/java/github/daneren2005/dsub/service/CachedMusicService.java b/app/src/main/java/github/daneren2005/dsub/service/CachedMusicService.java index 53433f5c..9fd26fe5 100644 --- a/app/src/main/java/github/daneren2005/dsub/service/CachedMusicService.java +++ b/app/src/main/java/github/daneren2005/dsub/service/CachedMusicService.java @@ -20,6 +20,7 @@ package github.daneren2005.dsub.service; import java.io.File; import java.io.IOException; +import java.net.HttpURLConnection; import java.util.ArrayList; import java.util.Arrays; import java.util.Iterator; @@ -27,8 +28,6 @@ import java.util.List; import java.util.ListIterator; import java.util.concurrent.TimeUnit; -import org.apache.http.HttpResponse; - import android.content.Context; import android.graphics.Bitmap; import android.util.Log; @@ -39,6 +38,7 @@ import github.daneren2005.dsub.domain.Bookmark; import github.daneren2005.dsub.domain.ChatMessage; import github.daneren2005.dsub.domain.Genre; import github.daneren2005.dsub.domain.Indexes; +import github.daneren2005.dsub.domain.InternetRadioStation; import github.daneren2005.dsub.domain.PlayerQueue; import github.daneren2005.dsub.domain.PodcastEpisode; import github.daneren2005.dsub.domain.RemoteStatus; @@ -69,6 +69,9 @@ public class CachedMusicService implements MusicService { private static final int MUSIC_DIR_CACHE_SIZE = 20; private static final int TTL_MUSIC_DIR = 5 * 60; // Five minutes + public static final int CACHE_UPDATE_LIST = 1; + public static final int CACHE_UPDATE_METADATA = 2; + private static final int CACHED_LAST_FM = 24 * 60; private final RESTMusicService musicService; private final TimeLimitedCache<Boolean> cachedLicenseValid = new TimeLimitedCache<Boolean>(120, TimeUnit.SECONDS); @@ -121,11 +124,13 @@ public class CachedMusicService implements MusicService { if(!refresh) { result = FileUtil.deserialize(context, getCacheName(context, "musicFolders"), ArrayList.class); } - + if(result == null) { result = musicService.getMusicFolders(refresh, context, progressListener); FileUtil.serialize(context, new ArrayList<MusicFolder>(result), getCacheName(context, "musicFolders")); } + + MusicFolder.sort(result); cachedMusicFolders.set(result); } return result; @@ -150,7 +155,7 @@ public class CachedMusicService implements MusicService { if(!refresh) { result = FileUtil.deserialize(context, name, Indexes.class); } - + if(result == null) { result = musicService.getIndexes(musicFolderId, refresh, context, progressListener); FileUtil.serialize(context, result, name); @@ -169,12 +174,13 @@ public class CachedMusicService implements MusicService { new SilentBackgroundTask<Void>(context) { MusicDirectory refreshed; + private boolean metadataUpdated; @Override protected Void doInBackground() throws Throwable { refreshed = musicService.getMusicDirectory(id, name, true, context, null); updateAllSongs(context, refreshed); - cached.updateMetadata(refreshed); + metadataUpdated = cached.updateMetadata(refreshed); deleteRemovedEntries(context, refreshed, cached); FileUtil.serialize(context, refreshed, getCacheName(context, "directory", id)); return null; @@ -185,7 +191,10 @@ public class CachedMusicService implements MusicService { public void done(Void result) { if(progressListener != null) { if(cached.updateEntriesList(context, musicService.getInstance(context), refreshed)) { - progressListener.updateCache(); + progressListener.updateCache(CACHE_UPDATE_LIST); + } + if(metadataUpdated) { + progressListener.updateCache(CACHE_UPDATE_METADATA); } } } @@ -201,7 +210,7 @@ public class CachedMusicService implements MusicService { dir = musicService.getMusicDirectory(id, name, refresh, context, progressListener); updateAllSongs(context, dir); FileUtil.serialize(context, dir, getCacheName(context, "directory", id)); - + // If a cached copy exists to check against, look for removes deleteRemovedEntries(context, dir, cached); } @@ -234,7 +243,7 @@ public class CachedMusicService implements MusicService { public void done(Void result) { if(progressListener != null) { if(cached.updateEntriesList(context, musicService.getInstance(context), refreshed)) { - progressListener.updateCache(); + progressListener.updateCache(CACHE_UPDATE_LIST); } } } @@ -267,12 +276,13 @@ public class CachedMusicService implements MusicService { new SilentBackgroundTask<Void>(context) { MusicDirectory refreshed; + private boolean metadataUpdated; @Override protected Void doInBackground() throws Throwable { refreshed = musicService.getAlbum(id, name, refresh, context, null); updateAllSongs(context, refreshed); - cached.updateMetadata(refreshed); + metadataUpdated = cached.updateMetadata(refreshed); deleteRemovedEntries(context, refreshed, cached); FileUtil.serialize(context, refreshed, getCacheName(context, "album", id)); return null; @@ -283,7 +293,10 @@ public class CachedMusicService implements MusicService { public void done(Void result) { if(progressListener != null) { if(cached.updateEntriesList(context, musicService.getInstance(context), refreshed)) { - progressListener.updateCache(); + progressListener.updateCache(CACHE_UPDATE_LIST); + } + if(metadataUpdated) { + progressListener.updateCache(CACHE_UPDATE_METADATA); } } } @@ -656,6 +669,11 @@ public class CachedMusicService implements MusicService { } @Override + public MusicDirectory getSongList(String type, int size, int offset, Context context, ProgressListener progressListener) throws Exception { + return musicService.getSongList(type, size, offset, context, progressListener); + } + + @Override public MusicDirectory getRandomSongs(int size, String artistId, Context context, ProgressListener progressListener) throws Exception { return musicService.getRandomSongs(size, artistId, context, progressListener); } @@ -714,11 +732,16 @@ public class CachedMusicService implements MusicService { @Override public Bitmap getCoverArt(Context context, Entry entry, int size, ProgressListener progressListener, SilentBackgroundTask task) throws Exception { - return musicService.getCoverArt(context, entry, size, progressListener, task); + Bitmap bitmap = FileUtil.getAlbumArtBitmap(context, entry, size); + if (bitmap != null) { + return bitmap; + } else { + return musicService.getCoverArt(context, entry, size, progressListener, task); + } } @Override - public HttpResponse getDownloadInputStream(Context context, Entry song, long offset, int maxBitrate, SilentBackgroundTask task) throws Exception { + public HttpURLConnection getDownloadInputStream(Context context, Entry song, long offset, int maxBitrate, SilentBackgroundTask task) throws Exception { return musicService.getDownloadInputStream(context, song, offset, maxBitrate, task); } @@ -905,8 +928,18 @@ public class CachedMusicService implements MusicService { } @Override - public MusicDirectory getNewestPodcastEpisodes(int count, Context context, ProgressListener progressListener) throws Exception { - return musicService.getNewestPodcastEpisodes(count, context, progressListener); + public MusicDirectory getNewestPodcastEpisodes(boolean refresh, Context context, ProgressListener progressListener, int count) throws Exception { + MusicDirectory result = null; + + String cacheName = getCacheName(context, "newestPodcastEpisodes"); + try { + result = musicService.getNewestPodcastEpisodes(refresh, context, progressListener, count); + FileUtil.serialize(context, result, cacheName); + } catch(IOException e) { + result = FileUtil.deserialize(context, cacheName, MusicDirectory.class, 24); + } finally { + return result; + } } @Override @@ -1128,7 +1161,12 @@ public class CachedMusicService implements MusicService { @Override public Bitmap getAvatar(String username, int size, Context context, ProgressListener progressListener, SilentBackgroundTask task) throws Exception { - return musicService.getAvatar(username, size, context, progressListener, task); + Bitmap bitmap = FileUtil.getAvatarBitmap(context, username, size); + if(bitmap != null) { + return bitmap; + } else { + return musicService.getAvatar(username, size, context, progressListener, task); + } } @Override @@ -1136,12 +1174,22 @@ public class CachedMusicService implements MusicService { String cacheName = getCacheName(context, "artistInfo", id); ArtistInfo info = null; if(!refresh) { - info = FileUtil.deserialize(context, cacheName, ArtistInfo.class); + info = FileUtil.deserialize(context, cacheName, ArtistInfo.class, CACHED_LAST_FM); } if(info == null && allowNetwork) { - info = musicService.getArtistInfo(id, refresh, allowNetwork, context, progressListener); - FileUtil.serialize(context, info, cacheName); + try { + info = musicService.getArtistInfo(id, refresh, allowNetwork, context, progressListener); + FileUtil.serialize(context, info, cacheName); + } catch(Exception e) { + Log.w(TAG, "Failed to refresh Artist Info"); + info = FileUtil.deserialize(context, cacheName, ArtistInfo.class); + + // Nothing ever cached, throw error further upstream + if(info == null) { + throw e; + } + } } return info; @@ -1149,7 +1197,12 @@ public class CachedMusicService implements MusicService { @Override public Bitmap getBitmap(String url, int size, Context context, ProgressListener progressListener, SilentBackgroundTask task) throws Exception { - return musicService.getBitmap(url, size, context, progressListener, task); + Bitmap bitmap = FileUtil.getMiscBitmap(context, url, size); + if(bitmap != null) { + return bitmap; + } else { + return musicService.getBitmap(url, size, context, progressListener, task); + } } @Override @@ -1180,6 +1233,22 @@ public class CachedMusicService implements MusicService { } @Override + public List<InternetRadioStation> getInternetRadioStations(boolean refresh, Context context, ProgressListener progressListener) throws Exception { + List<InternetRadioStation> result = null; + + if(!refresh) { + result = FileUtil.deserialize(context, getCacheName(context, "internetRadioStations"), ArrayList.class); + } + + if(result == null) { + result = musicService.getInternetRadioStations(refresh, context, progressListener); + FileUtil.serialize(context, new ArrayList<>(result), getCacheName(context, "internetRadioStations")); + } + + return result; + } + + @Override public int processOfflineSyncs(final Context context, final ProgressListener progressListener) throws Exception{ return musicService.processOfflineSyncs(context, progressListener); } |