From c7653c705150d1f0e1dbc2833687c9aea92e6644 Mon Sep 17 00:00:00 2001 From: Adrian Ulrich Date: Wed, 7 Oct 2015 16:20:43 +0200 Subject: [PATCH] get rid of sRandomCache --- .../android/vanilla/MediaUtils.java | 68 ++----------------- 1 file changed, 4 insertions(+), 64 deletions(-) diff --git a/src/ch/blinkenlights/android/vanilla/MediaUtils.java b/src/ch/blinkenlights/android/vanilla/MediaUtils.java index 418269c0..a1d08297 100644 --- a/src/ch/blinkenlights/android/vanilla/MediaUtils.java +++ b/src/ch/blinkenlights/android/vanilla/MediaUtils.java @@ -103,14 +103,6 @@ public class MediaUtils { private static long[] sAllSongs; private static int sAllSongsIdx; - /** - * Query this many songs at a time from sAllSongs. - */ - private static final int RANDOM_POPULATE_SIZE = 20; - private static final Song[] sRandomCache = new Song[RANDOM_POPULATE_SIZE]; - private static int sRandomCacheIdx; - private static int sRandomCacheEnd; - /** * Total number of songs in the music library, or -1 for uninitialized. */ @@ -370,11 +362,8 @@ public class MediaUtils { * * @param resolver A ContentResolver to use. */ - public static long[] queryAllSongs(ContentResolver resolver) + private static long[] queryAllSongs(ContentResolver resolver) { - sAllSongsIdx = 0; - sRandomCacheEnd = -1; - Uri media = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI; String selection = MediaStore.Audio.Media.IS_MUSIC; selection += " AND length(_data)"; @@ -441,63 +430,14 @@ public class MediaUtils { if (songs == null) return null; sAllSongs = songs; + sAllSongsIdx = 0; } else if (sAllSongsIdx == sAllSongs.length) { sAllSongsIdx = 0; - sRandomCacheEnd = -1; shuffle(sAllSongs); } - if (sAllSongsIdx >= sRandomCacheEnd) { - Uri media = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI; - - StringBuilder selection = new StringBuilder("_ID IN ("); - - boolean first = true; - int end = Math.min(sAllSongsIdx + RANDOM_POPULATE_SIZE, sAllSongs.length); - for (int i = sAllSongsIdx; i != end; ++i) { - if (!first) - selection.append(','); - - first = false; - - selection.append(sAllSongs[i]); - } - - selection.append(')'); - - Cursor cursor = resolver.query(media, Song.FILLED_PROJECTION, selection.toString(), null, null); - - if (cursor == null) { - sAllSongs = null; - return null; - } - - int count = cursor.getCount(); - if (count > 0) { - Assert.assertTrue(count <= RANDOM_POPULATE_SIZE); - - for (int i = 0; i != count; ++i) { - cursor.moveToNext(); - Song newSong = new Song(-1); - newSong.populate(cursor); - newSong.flags |= Song.FLAG_RANDOM; - sRandomCache[i] = newSong; - } - } - - cursor.close(); - - // The query will return sorted results; undo that - shuffle(sRandomCache, count); - - sRandomCacheIdx = 0; - sRandomCacheEnd = sAllSongsIdx + count; - } - - Song result = sRandomCache[sRandomCacheIdx]; - ++sRandomCacheIdx; - ++sAllSongsIdx; - + Song result = getSongByTypeId(resolver, MediaUtils.TYPE_SONG, sAllSongs[sAllSongsIdx]); + sAllSongsIdx++; return result; }