Android 11 - Mediastore playlist remove multiple tracks from Playlist

Viewed 426

On Android 11, when trying to remove multiple tracks from a Playlist, all the tracks are removed instead of just the selected ones. This used to work fine till Android 10.
However, if only 1 track is removed, no other tracks are removed. Is this a bug or an expected behavior going forward from Android 11? Is this a side-effect of scoped storage?
Tested on : Pixel 3, Android 11 Official release build

 public void removeTracksFromPlaylist(long playlistId, String[] ids) {
        Log.d(TAG, "removeTracksFromPlaylist() called with: playlistId = [" + playlistId + "], ids = [" + Arrays.toString(ids) + "]");
        Uri uri = MediaStore.Audio.Playlists.Members.getContentUri(MediaStore.VOLUME_EXTERNAL, playlistId);
        StringBuilder stringBuilder = new StringBuilder();
        for (int index = 0; index < ids.length; index++) {
            if (index > 0) {
                stringBuilder.append(",");
            }
            stringBuilder.append("?");
        }
        String selection = MediaStore.Audio.Playlists.Members._ID + " IN (" + stringBuilder.toString() + ")";
        contentResolver.delete(uri, selection, ids); // Returns count of all files removed from playlist
    }

Edit: Some information from source : (https://android.googlesource.com/platform/packages/providers/MediaProvider/+blame/refs/tags/android-11.0.0_r3/src/com/android/providers/media/MediaProvider.java#5610)

From the source, I see that if the count of playlist items are more than 1, the whole playlist is cleared.

try (Cursor c = qb.query(helper,
                new String[] { Playlists.Members.PLAY_ORDER }, queryArgs, null)) {
            if ((c.getCount() == 1) && c.moveToFirst()) {  // This condition fails if more than 1 item to delete
                return c.getInt(0) - 1;
            } else {
                return -1;
            }
}
 

The index is used here which clears the playlist:(https://android.googlesource.com/platform/packages/providers/MediaProvider/+blame/refs/tags/android-11.0.0_r3/src/com/android/providers/media/MediaProvider.java#5566)

 private int removePlaylistMembers(@NonNull Uri playlistUri, @NonNull Bundle queryArgs)
            throws FallbackException {
        final int index = resolvePlaylistIndex(playlistUri, queryArgs);
        try {
            final File playlistFile = queryForDataFile(playlistUri, null);
            final Playlist playlist = new Playlist();
            playlist.read(playlistFile);
            final int count;
            if (index == -1) {      
                count = playlist.asList().size();
                playlist.clear();  //Clears whole playlist
            } else {
                count = 1;
                playlist.remove(index);
            }
            playlist.write(playlistFile);
            resolvePlaylistMembers(playlistUri);
            return count;
        } catch (IOException e) {
            throw new FallbackException("Failed to update playlist", e,
                    android.os.Build.VERSION_CODES.R);
        }
    }
0 Answers
Related