public int createPlaylist(Playlist playlist, boolean overwrite) { String[] artistNameVariations = playlist.getArtistsVariations(); Cursor cursor = findSongs(artistNameVariations); int order = 1; if (cursor != null) { // if any artists matching the playlist artist was found if (anySongsFound(cursor, playlist)) { // if any of the songs selected were found Uri insertUri = setupPlaylist(playlist.getPlaylistName(), overwrite); if (insertUri != null) { // if the playlist was setup correctly order = addToPlaylist(cursor, insertUri, order, playlist); cursor.close(); return (order - 1); // return the number of songs that were found and added to the playlist } else { cursor.close(); return ERROR; } } else { cursor.close(); return NO_SONGS; } } else { return NO_ARTIST; } }
private boolean anySongsFound(Cursor cursor, Playlist playlist) { cursor.moveToFirst(); do { String songName = cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.TITLE)); if (playlist.containsSong(songName)) { return true; } } while (cursor.moveToNext()); return false; }
private int addToPlaylist(Cursor cursor, Uri insertUri, int order, Playlist playlist) { ContentResolver resolver = mContext.getContentResolver(); do { String songName = cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.TITLE)); if (playlist.containsSong(songName)) { long id = cursor.getLong(cursor.getColumnIndex(MediaStore.Audio.Media._ID)); if (!playlistContains(id, insertUri)) { ContentValues values = new ContentValues(); values.put(MediaStore.Audio.Playlists.Members.PLAY_ORDER, order++); values.put(MediaStore.Audio.Playlists.Members.AUDIO_ID, id); resolver.insert(insertUri, values); } } } while (cursor.moveToNext()); cursor.close(); return order; }