aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authordaneren2005 <daneren2005@gmail.com>2013-11-22 16:10:50 -0800
committerdaneren2005 <daneren2005@gmail.com>2013-11-22 16:10:50 -0800
commit8a3b127be0531ef0fe7174c84ffe19a2a2659ba1 (patch)
tree505009e8180109cc009df74c2f8d773d6528a2f3 /src
parent4a78f5cdd8b54e5c834aa7e5ccf290d46bc349a7 (diff)
downloaddsub-8a3b127be0531ef0fe7174c84ffe19a2a2659ba1.tar.gz
dsub-8a3b127be0531ef0fe7174c84ffe19a2a2659ba1.tar.bz2
dsub-8a3b127be0531ef0fe7174c84ffe19a2a2659ba1.zip
#195 Cache shuffle buffer to avoid repeat gets
Diffstat (limited to 'src')
-rw-r--r--src/github/daneren2005/dsub/util/ShufflePlayBuffer.java26
1 files changed, 24 insertions, 2 deletions
diff --git a/src/github/daneren2005/dsub/util/ShufflePlayBuffer.java b/src/github/daneren2005/dsub/util/ShufflePlayBuffer.java
index 195fe913..d5f735ab 100644
--- a/src/github/daneren2005/dsub/util/ShufflePlayBuffer.java
+++ b/src/github/daneren2005/dsub/util/ShufflePlayBuffer.java
@@ -30,6 +30,7 @@ import android.util.Log;
import github.daneren2005.dsub.domain.MusicDirectory;
import github.daneren2005.dsub.service.MusicService;
import github.daneren2005.dsub.service.MusicServiceFactory;
+import github.daneren2005.dsub.util.FileUtil;
/**
* @author Sindre Mehus
@@ -38,11 +39,12 @@ import github.daneren2005.dsub.service.MusicServiceFactory;
public class ShufflePlayBuffer {
private static final String TAG = ShufflePlayBuffer.class.getSimpleName();
+ private static final String CACHE_FILENAME = "shuffleBuffer.ser";
private static final int CAPACITY = 50;
private static final int REFILL_THRESHOLD = 40;
private final ScheduledExecutorService executorService;
- private final List<MusicDirectory.Entry> buffer = new ArrayList<MusicDirectory.Entry>();
+ private List<MusicDirectory.Entry> buffer;
private int lastCount = -1;
private Context context;
private int currentServer;
@@ -53,7 +55,13 @@ public class ShufflePlayBuffer {
private String endYear = "";
public ShufflePlayBuffer(Context context) {
- this.context = context;
+ this.context = context;
+
+ buffer = FileUtil.deserialize(context, CACHE_FILENAME, ArrayList.class);
+ if(buffer == null) {
+ buffer = new ArrayList<MusicDirectory.Entry>();
+ }
+
executorService = Executors.newSingleThreadScheduledExecutor();
Runnable runnable = new Runnable() {
@Override
@@ -69,8 +77,15 @@ public class ShufflePlayBuffer {
List<MusicDirectory.Entry> result = new ArrayList<MusicDirectory.Entry>(size);
synchronized (buffer) {
+ boolean removed = false;
while (!buffer.isEmpty() && result.size() < size) {
result.add(buffer.remove(buffer.size() - 1));
+ removed = true;
+ }
+
+ // Re-cache if anything is taken out
+ if(removed) {
+ FileUtil.serialize(context, buffer, "shuffle.ser");
}
}
Log.i(TAG, "Taking " + result.size() + " songs from shuffle play buffer. " + buffer.size() + " remaining.");
@@ -100,6 +115,9 @@ public class ShufflePlayBuffer {
buffer.addAll(songs.getChildren());
Log.i(TAG, "Refilled shuffle play buffer with " + songs.getChildrenSize() + " songs.");
lastCount = songs.getChildrenSize();
+
+ // Cache buffer
+ FileUtil.serialize(context, buffer, "shuffle.ser");
}
} catch (Exception x) {
Log.w(TAG, "Failed to refill shuffle play buffer.", x);
@@ -121,6 +139,10 @@ public class ShufflePlayBuffer {
startYear = prefs.getString(Constants.PREFERENCES_KEY_SHUFFLE_START_YEAR, "");
endYear = prefs.getString(Constants.PREFERENCES_KEY_SHUFFLE_END_YEAR, "");
buffer.clear();
+
+ // Clear cache
+ File file = new File(context.getCacheDir(), CACHE_FILENAME);
+ file.delete();
}
}
}