diff options
author | Scott Jackson <daneren2005@gmail.com> | 2015-04-27 17:39:30 -0700 |
---|---|---|
committer | Scott Jackson <daneren2005@gmail.com> | 2015-04-27 17:39:30 -0700 |
commit | 28a8dba5682a953570c04013b4acbd7b5dcab685 (patch) | |
tree | dab3f7c272f0267a27427ed7211cce1bc8ed5415 /app/src/main/java | |
parent | 9ab86aa9de58252f4355df5186c3f4b7f68c5fff (diff) | |
download | dsub-28a8dba5682a953570c04013b4acbd7b5dcab685.tar.gz dsub-28a8dba5682a953570c04013b4acbd7b5dcab685.tar.bz2 dsub-28a8dba5682a953570c04013b4acbd7b5dcab685.zip |
#496 Update cached metadata in background thread
Diffstat (limited to 'app/src/main/java')
4 files changed, 79 insertions, 3 deletions
diff --git a/app/src/main/java/github/daneren2005/dsub/domain/MusicDirectory.java b/app/src/main/java/github/daneren2005/dsub/domain/MusicDirectory.java index ad819763..3a2b9834 100644 --- a/app/src/main/java/github/daneren2005/dsub/domain/MusicDirectory.java +++ b/app/src/main/java/github/daneren2005/dsub/domain/MusicDirectory.java @@ -18,10 +18,13 @@ */ package github.daneren2005.dsub.domain; +import android.annotation.TargetApi; import android.content.Context; import android.media.MediaMetadataRetriever; +import android.os.Build; import android.util.Log; import java.util.ArrayList; +import java.util.Iterator; import java.util.List; import java.io.File; import java.io.Serializable; @@ -121,6 +124,46 @@ public class MusicDirectory implements Serializable { EntryComparator.sort(children, byYear); } + public void updateDifferences(Context context, int instance, MusicDirectory refreshedDirectory) { + Iterator<Entry> it = children.iterator(); + while(it.hasNext()) { + Entry entry = it.next(); + int index = refreshedDirectory.children.indexOf(entry); + if(index != -1) { + Entry refreshed = refreshedDirectory.children.get(index); + + entry.setTitle(refreshed.getTitle()); + entry.setAlbum(refreshed.getAlbum()); + entry.setArtist(refreshed.getArtist()); + entry.setTrack(refreshed.getTrack()); + entry.setYear(refreshed.getYear()); + entry.setGenre(refreshed.getGenre()); + entry.setTranscodedContentType(refreshed.getTranscodedContentType()); + entry.setTranscodedSuffix(refreshed.getTranscodedSuffix()); + entry.setDiscNumber(refreshed.getDiscNumber()); + entry.setStarred(refreshed.isStarred()); + entry.setRating(refreshed.getRating()); + entry.setType(refreshed.getType()); + } else { + // No longer exists in here + // it.remove(); + } + } + + // Make sure we contain all children from refreshed set + /*boolean resort = false; + for(Entry refreshed: refreshedDirectory.children) { + if(!this.children.contains(refreshed)) { + this.children.add(refreshed); + resort = true; + } + } + + if(resort) { + this.sortChildren(context, instance); + }*/ + } + public static class Entry implements Serializable { public static final int TYPE_SONG = 0; public static final int TYPE_PODCAST = 1; @@ -167,6 +210,7 @@ public class MusicDirectory implements Serializable { this.directory = true; } + @TargetApi(Build.VERSION_CODES.GINGERBREAD_MR1) public void loadMetadata(File file) { try { MediaMetadataRetriever metadata = new MediaMetadataRetriever(); 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 61d6205a..f161cac6 100644 --- a/app/src/main/java/github/daneren2005/dsub/service/CachedMusicService.java +++ b/app/src/main/java/github/daneren2005/dsub/service/CachedMusicService.java @@ -31,6 +31,7 @@ import org.apache.http.HttpResponse; import android.content.Context; import android.graphics.Bitmap; +import android.util.Log; import github.daneren2005.dsub.domain.Artist; import github.daneren2005.dsub.domain.ArtistInfo; @@ -50,6 +51,7 @@ import github.daneren2005.dsub.domain.SearchCritera; import github.daneren2005.dsub.domain.SearchResult; import github.daneren2005.dsub.domain.Share; import github.daneren2005.dsub.domain.User; +import github.daneren2005.dsub.util.Pair; import github.daneren2005.dsub.util.SilentBackgroundTask; import github.daneren2005.dsub.util.ProgressListener; import github.daneren2005.dsub.util.TimeLimitedCache; @@ -158,11 +160,35 @@ public class CachedMusicService implements MusicService { } @Override - public MusicDirectory getMusicDirectory(String id, String name, boolean refresh, Context context, ProgressListener progressListener) throws Exception { + public MusicDirectory getMusicDirectory(final String id, final String name, final boolean refresh, final Context context, final ProgressListener progressListener) throws Exception { MusicDirectory dir = null; - MusicDirectory cached = FileUtil.deserialize(context, getCacheName(context, "directory", id), MusicDirectory.class); - if(!refresh) { + final MusicDirectory cached = FileUtil.deserialize(context, getCacheName(context, "directory", id), MusicDirectory.class); + if(!refresh && cached != null) { dir = cached; + + new SilentBackgroundTask<Void>(context) { + @Override + protected Void doInBackground() throws Throwable { + Util.sleepQuietly(2000L); + MusicDirectory refreshed = musicService.getMusicDirectory(id, name, true, context, null); + cached.updateDifferences(context, musicService.getInstance(context), refreshed); + FileUtil.serialize(context, refreshed, getCacheName(context, "directory", id)); + return null; + } + + // TODO: When upgrading to RecyclerView, this should be usable since won't have split entry/album lists + /*@Override + public void done(Void result) { + if(progressListener != null) { + progressListener.updateCache(); + } + }*/ + + @Override + public void error(Throwable error) { + Log.e(TAG, "Failed to refresh music directory", error); + } + }.execute(); } if(dir == null) { diff --git a/app/src/main/java/github/daneren2005/dsub/util/BackgroundTask.java b/app/src/main/java/github/daneren2005/dsub/util/BackgroundTask.java index 9b39ac82..30081d7d 100644 --- a/app/src/main/java/github/daneren2005/dsub/util/BackgroundTask.java +++ b/app/src/main/java/github/daneren2005/dsub/util/BackgroundTask.java @@ -172,6 +172,11 @@ public abstract class BackgroundTask<T> implements ProgressListener { updateProgress(context.getResources().getString(messageId)); } + @Override + public void updateCache() { + + } + public void setOnCompletionListener(Runnable onCompletionListener) { this.onCompletionListener = onCompletionListener; } diff --git a/app/src/main/java/github/daneren2005/dsub/util/ProgressListener.java b/app/src/main/java/github/daneren2005/dsub/util/ProgressListener.java index c6d58f42..22f35efc 100644 --- a/app/src/main/java/github/daneren2005/dsub/util/ProgressListener.java +++ b/app/src/main/java/github/daneren2005/dsub/util/ProgressListener.java @@ -24,4 +24,5 @@ package github.daneren2005.dsub.util; public interface ProgressListener { void updateProgress(String message); void updateProgress(int messageId); + void updateCache(); } |