From 3a3530fc93176a88bff7041644b43957fb32c7f9 Mon Sep 17 00:00:00 2001 From: Adrian Ulrich Date: Mon, 20 Oct 2014 22:29:11 +0200 Subject: [PATCH] implement addToPlaylist() variant accepting an ArrayList --- .../android/vanilla/Playlist.java | 37 +++++++++++++------ 1 file changed, 26 insertions(+), 11 deletions(-) diff --git a/src/ch/blinkenlights/android/vanilla/Playlist.java b/src/ch/blinkenlights/android/vanilla/Playlist.java index 600fc2d2..04f307d0 100644 --- a/src/ch/blinkenlights/android/vanilla/Playlist.java +++ b/src/ch/blinkenlights/android/vanilla/Playlist.java @@ -23,6 +23,8 @@ package ch.blinkenlights.android.vanilla; +import java.util.ArrayList; + import android.content.ContentResolver; import android.content.ContentUris; import android.content.ContentValues; @@ -133,8 +135,28 @@ public class Playlist { * @param query The query to run. The audio id should be the first column. * @return The number of songs that were added to the playlist. */ - public static int addToPlaylist(ContentResolver resolver, long playlistId, QueryTask query) - { + public static int addToPlaylist(ContentResolver resolver, long playlistId, QueryTask query) { + ArrayList result = new ArrayList(); + Cursor cursor = query.runQuery(resolver); + if (cursor != null) { + while (cursor.moveToNext()) { + result.add(cursor.getLong(0)); + } + } + return addToPlaylist(resolver, playlistId, result); + } + + /** + * Run the given query and add the results to the given playlist. Should be + * run on a background thread. + * + * @param resolver A ContentResolver to use. + * @param playlistId The MediaStore.Audio.Playlist id of the playlist to + * modify. + * @param audioIds An ArrayList with all IDs to add + * @return The number of songs that were added to the playlist. + */ + public static int addToPlaylist(ContentResolver resolver, long playlistId, ArrayList audioIds) { if (playlistId == -1) return 0; @@ -147,25 +169,18 @@ public class Playlist { base = cursor.getInt(0) + 1; cursor.close(); - Cursor from = query.runQuery(resolver); - if (from == null) - return 0; - - int count = from.getCount(); + int count = audioIds.size(); if (count > 0) { ContentValues[] values = new ContentValues[count]; for (int i = 0; i != count; ++i) { - from.moveToPosition(i); ContentValues value = new ContentValues(2); value.put(MediaStore.Audio.Playlists.Members.PLAY_ORDER, Integer.valueOf(base + i)); - value.put(MediaStore.Audio.Playlists.Members.AUDIO_ID, from.getLong(0)); + value.put(MediaStore.Audio.Playlists.Members.AUDIO_ID, audioIds.get(i)); values[i] = value; } resolver.bulkInsert(uri, values); } - from.close(); - return count; }