diff options
author | Scott Jackson <daneren2005@gmail.com> | 2014-08-27 15:00:46 -0700 |
---|---|---|
committer | Scott Jackson <daneren2005@gmail.com> | 2014-08-27 15:00:46 -0700 |
commit | 442e2f2f66271edd1b28e26c5fac8ecc09b73d30 (patch) | |
tree | 4f10f78448d3f19aa5f466af84cadd9607c44831 /src | |
parent | 3b07693d398de0dac28d6bae7125892815c2562a (diff) | |
download | dsub-442e2f2f66271edd1b28e26c5fac8ecc09b73d30.tar.gz dsub-442e2f2f66271edd1b28e26c5fac8ecc09b73d30.tar.bz2 dsub-442e2f2f66271edd1b28e26c5fac8ecc09b73d30.zip |
#385 Optimize shuffle buffer logic for large shuffle size preferences. Fewer round trips.
Diffstat (limited to 'src')
-rw-r--r-- | src/github/daneren2005/dsub/util/ShufflePlayBuffer.java | 21 |
1 files changed, 16 insertions, 5 deletions
diff --git a/src/github/daneren2005/dsub/util/ShufflePlayBuffer.java b/src/github/daneren2005/dsub/util/ShufflePlayBuffer.java index 5a728334..ab32af97 100644 --- a/src/github/daneren2005/dsub/util/ShufflePlayBuffer.java +++ b/src/github/daneren2005/dsub/util/ShufflePlayBuffer.java @@ -42,8 +42,6 @@ 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 ScheduledExecutorService executorService; private Runnable runnable; @@ -52,6 +50,8 @@ public class ShufflePlayBuffer { private int lastCount = -1; private DownloadService context; private boolean awaitingResults = false; + private int capacity; + private int refillThreshold; private SharedPreferences.OnSharedPreferenceChangeListener listener; private int currentServer; @@ -71,6 +71,15 @@ public class ShufflePlayBuffer { } }; executorService.scheduleWithFixedDelay(runnable, 1, 10, TimeUnit.SECONDS); + + // Calculate out the capacity and refill threshold based on the user's random size preference + int shuffleListSize = Integer.parseInt(Util.getPreferences(context).getString(Constants.PREFERENCES_KEY_RANDOM_SIZE, "20")); + // ex: default 20 -> 50 + capacity = shuffleListSize * 5 / 2; + capacity = Math.min(500, capacity); + + // ex: default 20 -> 40 + refillThreshold = capacity * 4 / 5; } public List<MusicDirectory.Entry> get(int size) { @@ -105,7 +114,7 @@ public class ShufflePlayBuffer { private void restart() { synchronized(buffer) { - if(buffer.size() <= REFILL_THRESHOLD && lastCount != 0 && executorService.isShutdown()) { + if(buffer.size() <= refillThreshold && lastCount != 0 && executorService.isShutdown()) { executorService = Executors.newSingleThreadScheduledExecutor(); executorService.scheduleWithFixedDelay(runnable, 0, 10, TimeUnit.SECONDS); } @@ -116,14 +125,16 @@ public class ShufflePlayBuffer { // Check if active server has changed. clearBufferIfnecessary(); - if (buffer != null && (buffer.size() > REFILL_THRESHOLD || (!Util.isNetworkConnected(context) && !Util.isOffline(context)) || lastCount == 0)) { + if (buffer != null && (buffer.size() > refillThreshold || (!Util.isNetworkConnected(context) && !Util.isOffline(context)) || lastCount == 0)) { executorService.shutdown(); return; } try { MusicService service = MusicServiceFactory.getMusicService(context); - int n = CAPACITY - buffer.size(); + + // Get capacity based + int n = capacity - buffer.size(); String folder = null; if(!Util.isTagBrowsing(context)) { folder = Util.getSelectedMusicFolderId(context); |