Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 49b8b9ca authored by Gabriele M's avatar Gabriele M
Browse files

Eleven: Create one cursor per background task

If we call loadInBackground() twice we will have conflicts since
the two threads will share the cursor. The cursor is only used by
loadInBackground(), there's no need to have a class member for it,
so just keep it in a local variable to prevent conflicts.

BUGBASH-1045

Change-Id: Id484bee852d886c3a49ec8c84ef821f969db9a54
parent 943d43e8
Loading
Loading
Loading
Loading
+13 −18
Original line number Diff line number Diff line
@@ -45,11 +45,6 @@ public class AlbumLoader extends SectionCreator.SimpleListLoader<Album> {
     */
    private ArrayList<Album> mAlbumsList = Lists.newArrayList();

    /**
     * The {@link Cursor} used to run the query.
     */
    private Cursor mCursor;

    /**
     * Additional selection filter
     */
@@ -78,24 +73,24 @@ public class AlbumLoader extends SectionCreator.SimpleListLoader<Album> {
    @Override
    public List<Album> loadInBackground() {
        // Create the Cursor
        mCursor = makeAlbumCursor(getContext(), mArtistId);
        Cursor cursor = makeAlbumCursor(getContext(), mArtistId);
        // Gather the data
        if (mCursor != null && mCursor.moveToFirst()) {
        if (cursor != null && cursor.moveToFirst()) {
            do {
                // Copy the album id
                final long id = mCursor.getLong(0);
                final long id = cursor.getLong(0);

                // Copy the album name
                final String albumName = mCursor.getString(1);
                final String albumName = cursor.getString(1);

                // Copy the artist name
                final String artist = mCursor.getString(2);
                final String artist = cursor.getString(2);

                // Copy the number of songs
                final int songCount = mCursor.getInt(3);
                final int songCount = cursor.getInt(3);

                // Copy the release year
                final String year = mCursor.getString(4);
                final String year = cursor.getString(4);

                // as per designer's request, don't show unknown albums
                if (MediaStore.UNKNOWN_STRING.equals(albumName)) {
@@ -105,18 +100,18 @@ public class AlbumLoader extends SectionCreator.SimpleListLoader<Album> {
                // Create a new album
                final Album album = new Album(id, albumName, artist, songCount, year);

                if (mCursor instanceof SortedCursor) {
                    album.mBucketLabel = (String)((SortedCursor)mCursor).getExtraData();
                if (cursor instanceof SortedCursor) {
                    album.mBucketLabel = (String)((SortedCursor) cursor).getExtraData();
                }

                // Add everything up
                mAlbumsList.add(album);
            } while (mCursor.moveToNext());
            } while (cursor.moveToNext());
        }
        // Close the cursor
        if (mCursor != null) {
            mCursor.close();
            mCursor = null;
        if (cursor != null) {
            cursor.close();
            cursor = null;
        }

        return mAlbumsList;
+12 −17
Original line number Diff line number Diff line
@@ -39,11 +39,6 @@ public class AlbumSongLoader extends WrappedAsyncTaskLoader<List<Song>> {
     */
    private final ArrayList<Song> mSongList = Lists.newArrayList();

    /**
     * The {@link Cursor} used to run the query.
     */
    private Cursor mCursor;

    /**
     * The Id of the album the songs belong to.
     */
@@ -66,42 +61,42 @@ public class AlbumSongLoader extends WrappedAsyncTaskLoader<List<Song>> {
    @Override
    public List<Song> loadInBackground() {
        // Create the Cursor
        mCursor = makeAlbumSongCursor(getContext(), mAlbumID);
        Cursor cursor = makeAlbumSongCursor(getContext(), mAlbumID);
        // Gather the data
        if (mCursor != null && mCursor.moveToFirst()) {
        if (cursor != null && cursor.moveToFirst()) {
            do {
                // Copy the song Id
                final long id = mCursor.getLong(0);
                final long id = cursor.getLong(0);

                // Copy the song name
                final String songName = mCursor.getString(1);
                final String songName = cursor.getString(1);

                // Copy the artist name
                final String artist = mCursor.getString(2);
                final String artist = cursor.getString(2);

                // Copy the album name
                final String album = mCursor.getString(3);
                final String album = cursor.getString(3);

                // Copy the duration
                final long duration = mCursor.getLong(4);
                final long duration = cursor.getLong(4);

                // Make the duration label
                final int seconds = (int) (duration / 1000);

                // Grab the Song Year
                final int year = mCursor.getInt(5);
                final int year = cursor.getInt(5);

                // Create a new song
                final Song song = new Song(id, songName, artist, album, mAlbumID, seconds, year);

                // Add everything up
                mSongList.add(song);
            } while (mCursor.moveToNext());
            } while (cursor.moveToNext());
        }
        // Close the cursor
        if (mCursor != null) {
            mCursor.close();
            mCursor = null;
        if (cursor != null) {
            cursor.close();
            cursor = null;
        }
        return mSongList;
    }
+12 −17
Original line number Diff line number Diff line
@@ -43,11 +43,6 @@ public class ArtistLoader extends SectionCreator.SimpleListLoader<Artist> {
     */
    private ArrayList<Artist> mArtistsList = Lists.newArrayList();

    /**
     * The {@link Cursor} used to run the query.
     */
    private Cursor mCursor;

    /**
     * Constructor of <code>ArtistLoader</code>
     *
@@ -63,21 +58,21 @@ public class ArtistLoader extends SectionCreator.SimpleListLoader<Artist> {
    @Override
    public List<Artist> loadInBackground() {
        // Create the Cursor
        mCursor = makeArtistCursor(getContext());
        Cursor cursor = makeArtistCursor(getContext());
        // Gather the data
        if (mCursor != null && mCursor.moveToFirst()) {
        if (cursor != null && cursor.moveToFirst()) {
            do {
                // Copy the artist id
                final long id = mCursor.getLong(0);
                final long id = cursor.getLong(0);

                // Copy the artist name
                final String artistName = mCursor.getString(1);
                final String artistName = cursor.getString(1);

                // Copy the number of albums
                final int albumCount = mCursor.getInt(2);
                final int albumCount = cursor.getInt(2);

                // Copy the number of songs
                final int songCount = mCursor.getInt(3);
                final int songCount = cursor.getInt(3);

                // as per designer's request, don't show unknown artist
                if (MediaStore.UNKNOWN_STRING.equals(artistName)) {
@@ -87,17 +82,17 @@ public class ArtistLoader extends SectionCreator.SimpleListLoader<Artist> {
                // Create a new artist
                final Artist artist = new Artist(id, artistName, songCount, albumCount);

                if (mCursor instanceof SortedCursor) {
                    artist.mBucketLabel = (String)((SortedCursor)mCursor).getExtraData();
                if (cursor instanceof SortedCursor) {
                    artist.mBucketLabel = (String)((SortedCursor) cursor).getExtraData();
                }

                mArtistsList.add(artist);
            } while (mCursor.moveToNext());
            } while (cursor.moveToNext());
        }
        // Close the cursor
        if (mCursor != null) {
            mCursor.close();
            mCursor = null;
        if (cursor != null) {
            cursor.close();
            cursor = null;
        }

        return mArtistsList;
+13 −18
Original line number Diff line number Diff line
@@ -39,11 +39,6 @@ public class LastAddedLoader extends SectionCreator.SimpleListLoader<Song> {
     */
    private final ArrayList<Song> mSongList = Lists.newArrayList();

    /**
     * The {@link Cursor} used to run the query.
     */
    private Cursor mCursor;

    /**
     * Constructor of <code>LastAddedHandler</code>
     *
@@ -59,45 +54,45 @@ public class LastAddedLoader extends SectionCreator.SimpleListLoader<Song> {
    @Override
    public List<Song> loadInBackground() {
        // Create the xCursor
        mCursor = makeLastAddedCursor(getContext());
        Cursor cursor = makeLastAddedCursor(getContext());
        // Gather the data
        if (mCursor != null && mCursor.moveToFirst()) {
        if (cursor != null && cursor.moveToFirst()) {
            do {
                // Copy the song Id
                final long id = mCursor.getLong(0);
                final long id = cursor.getLong(0);

                // Copy the song name
                final String songName = mCursor.getString(1);
                final String songName = cursor.getString(1);

                // Copy the artist name
                final String artist = mCursor.getString(2);
                final String artist = cursor.getString(2);

                // Copy the album id
                final long albumId = mCursor.getLong(3);
                final long albumId = cursor.getLong(3);

                // Copy the album name
                final String album = mCursor.getString(4);
                final String album = cursor.getString(4);

                // Copy the duration
                final long duration = mCursor.getLong(5);
                final long duration = cursor.getLong(5);

                // Convert the duration into seconds
                final int durationInSecs = (int) duration / 1000;

                // Grab the Song Year
                final int year = mCursor.getInt(6);
                final int year = cursor.getInt(6);

                // Create a new song
                final Song song = new Song(id, songName, artist, album, albumId, durationInSecs, year);

                // Add everything up
                mSongList.add(song);
            } while (mCursor.moveToNext());
            } while (cursor.moveToNext());
        }
        // Close the cursor
        if (mCursor != null) {
            mCursor.close();
            mCursor = null;
        if (cursor != null) {
            cursor.close();
            cursor = null;
        }
        return mSongList;
    }
+8 −13
Original line number Diff line number Diff line
@@ -41,11 +41,6 @@ public class PlaylistLoader extends WrappedAsyncTaskLoader<List<Playlist>> {
     */
    private final ArrayList<Playlist> mPlaylistList = Lists.newArrayList();

    /**
     * The {@link Cursor} used to run the query.
     */
    private Cursor mCursor;

    /**
     * Constructor of <code>PlaylistLoader</code>
     *
@@ -64,15 +59,15 @@ public class PlaylistLoader extends WrappedAsyncTaskLoader<List<Playlist>> {
        makeDefaultPlaylists();

        // Create the Cursor
        mCursor = makePlaylistCursor(getContext());
        Cursor cursor = makePlaylistCursor(getContext());
        // Gather the data
        if (mCursor != null && mCursor.moveToFirst()) {
        if (cursor != null && cursor.moveToFirst()) {
            do {
                // Copy the playlist id
                final long id = mCursor.getLong(0);
                final long id = cursor.getLong(0);

                // Copy the playlist name
                final String name = mCursor.getString(1);
                final String name = cursor.getString(1);

                final int songCount = MusicUtils.getSongCountForPlaylist(getContext(), id);

@@ -81,12 +76,12 @@ public class PlaylistLoader extends WrappedAsyncTaskLoader<List<Playlist>> {

                // Add everything up
                mPlaylistList.add(playlist);
            } while (mCursor.moveToNext());
            } while (cursor.moveToNext());
        }
        // Close the cursor
        if (mCursor != null) {
            mCursor.close();
            mCursor = null;
        if (cursor != null) {
            cursor.close();
            cursor = null;
        }
        return mPlaylistList;
    }
Loading