Account for sorting that takes place when querying random songs

This commit is contained in:
Christopher Eby 2011-09-02 02:36:49 -05:00
parent d0ee5e8676
commit 605e14f930
2 changed files with 16 additions and 2 deletions

View File

@ -210,4 +210,15 @@ public class MediaUtils {
list[i] = tmp;
}
}
public static void shuffle(Song[] list)
{
Random random = ContextApplication.getRandom();
for (int i = list.length; --i != -1; ) {
int j = random.nextInt(i + 1);
Song tmp = list[j];
list[j] = list[i];
list[i] = tmp;
}
}
}

View File

@ -216,14 +216,14 @@ public class Song implements Parcelable {
int end = Math.min(mAllSongsIdx + RANDOM_POPULATE_SIZE, mAllSongs.length);
for (int i = mAllSongsIdx; i != end; ++i) {
if (!first)
selection.append(",");
selection.append(',');
first = false;
selection.append(mAllSongs[i]);
}
selection.append(")");
selection.append(')');
Cursor cursor = resolver.query(media, FILLED_PROJECTION, selection.toString(), null, null);
@ -247,6 +247,9 @@ public class Song implements Parcelable {
cursor.close();
// The query will return sorted results; undo that
MediaUtils.shuffle(mRandomCache);
mRandomCacheIdx = 0;
mRandomCacheEnd = mAllSongsIdx + count;
}