Skip to content

Commit

Permalink
[android] Loaders refactor.
Browse files Browse the repository at this point in the history
  • Loading branch information
gubatron committed Feb 4, 2016
1 parent 7036b04 commit 63a0a42
Show file tree
Hide file tree
Showing 11 changed files with 101 additions and 495 deletions.
63 changes: 7 additions & 56 deletions android/apollo/src/com/andrew/apollo/loaders/AlbumSongLoader.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
import android.provider.BaseColumns;
import android.provider.MediaStore;
import android.provider.MediaStore.Audio.AudioColumns;

import com.andrew.apollo.model.Song;
import com.andrew.apollo.utils.Lists;
import com.andrew.apollo.utils.PreferenceUtils;
Expand All @@ -25,22 +24,13 @@
import java.util.List;

/**
* Used to query {@link MediaStore.Audio.Media.EXTERNAL_CONTENT_URI} and return
* Used to query MediaStore.Audio.Media.EXTERNAL_CONTENT_URI and return
* the Song for a particular album.
*
* @author Andrew Neal (andrewdneal@gmail.com)
* @author Angel Leon (gubatron@gmail.com)
*/
public class AlbumSongLoader extends WrappedAsyncTaskLoader<List<Song>> {

/**
* The result
*/
private final ArrayList<Song> mSongList = Lists.newArrayList();

/**
* The {@link Cursor} used to run the query.
*/
private Cursor mCursor;
public class AlbumSongLoader extends SongLoader {

/**
* The Id of the album the songs belong to.
Expand All @@ -58,60 +48,22 @@ public AlbumSongLoader(final Context context, final Long albumId) {
mAlbumID = albumId;
}

/**
* {@inheritDoc}
*/
@Override
public List<Song> loadInBackground() {
// Create the Cursor
mCursor = makeAlbumSongCursor(getContext(), mAlbumID);
// Gather the data
if (mCursor != null && mCursor.moveToFirst()) {
do {
// Copy the song Id
final long id = mCursor.getLong(0);

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

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

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

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

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

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

// Add everything up
mSongList.add(song);
} while (mCursor.moveToNext());
}
// Close the cursor
if (mCursor != null) {
mCursor.close();
mCursor = null;
}
return mSongList;
public Cursor makeCursor(Context context) {
return makeAlbumSongCursor(context, mAlbumID);
}

/**
* @param context The {@link Context} to use.
* @param albumId The Id of the album the songs belong to.
* @return The {@link Cursor} used to run the query.
*/
public static final Cursor makeAlbumSongCursor(final Context context, final Long albumId) {
public static Cursor makeAlbumSongCursor(final Context context, final Long albumId) {
// Match the songs up with the artist
final StringBuilder selection = new StringBuilder();
selection.append(AudioColumns.IS_MUSIC + "=1");
selection.append(" AND " + AudioColumns.TITLE + " != ''");
selection.append(" AND " + AudioColumns.ALBUM_ID + "=" + albumId);
selection.append(" AND " + AudioColumns.ALBUM_ID + "=").append(albumId);
return context.getContentResolver().query(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI,
new String[] {
/* 0 */
Expand All @@ -127,5 +79,4 @@ public static final Cursor makeAlbumSongCursor(final Context context, final Long
}, selection.toString(), null,
PreferenceUtils.getInstance(context).getAlbumSongSortOrder());
}

}
26 changes: 9 additions & 17 deletions android/apollo/src/com/andrew/apollo/loaders/ArtistLoader.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@
import android.provider.BaseColumns;
import android.provider.MediaStore;
import android.provider.MediaStore.Audio.ArtistColumns;

import com.frostwire.android.R;
import com.andrew.apollo.model.Artist;
import com.andrew.apollo.utils.Lists;
import com.andrew.apollo.utils.PreferenceUtils;
Expand All @@ -26,23 +24,13 @@
import java.util.List;

/**
* Used to query {@link MediaStore.Audio.Artists.EXTERNAL_CONTENT_URI} and
* Used to query MediaStore.Audio.Artists.EXTERNAL_CONTENT_URI and
* return the artists on a user's device.
*
* @author Andrew Neal (andrewdneal@gmail.com)
* @author Angel Leon (gubatron@gmail.com)
*/
public class ArtistLoader extends WrappedAsyncTaskLoader<List<Artist>> {

/**
* The result
*/
private final ArrayList<Artist> mArtistsList = Lists.newArrayList();

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

/**
* Constructor of <code>ArtistLoader</code>
*
Expand All @@ -52,13 +40,18 @@ public ArtistLoader(final Context context) {
super(context);
}

public Cursor makeCursor(Context context) {
return makeArtistCursor(context);
}

/**
* {@inheritDoc}
*/
@Override
public List<Artist> loadInBackground() {
ArrayList<Artist> mArtistsList = Lists.newArrayList();
// Create the Cursor
mCursor = makeArtistCursor(getContext());
Cursor mCursor = makeCursor(getContext());
// Gather the data
if (mCursor != null && mCursor.moveToFirst()) {
do {
Expand All @@ -84,7 +77,6 @@ public List<Artist> loadInBackground() {
// Close the cursor
if (mCursor != null) {
mCursor.close();
mCursor = null;
}
return mArtistsList;
}
Expand All @@ -95,7 +87,7 @@ public List<Artist> loadInBackground() {
* @param context The {@link Context} to use.
* @return The {@link Cursor} used to run the artist query.
*/
public static final Cursor makeArtistCursor(final Context context) {
public static Cursor makeArtistCursor(final Context context) {
return context.getContentResolver().query(MediaStore.Audio.Artists.EXTERNAL_CONTENT_URI,
new String[] {
/* 0 */
Expand Down
64 changes: 4 additions & 60 deletions android/apollo/src/com/andrew/apollo/loaders/ArtistSongLoader.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,33 +16,16 @@
import android.provider.BaseColumns;
import android.provider.MediaStore;
import android.provider.MediaStore.Audio.AudioColumns;

import com.andrew.apollo.model.Song;
import com.andrew.apollo.utils.Lists;
import com.andrew.apollo.utils.PreferenceUtils;

import java.util.ArrayList;
import java.util.List;

/**
* Used to query {MediaStore.Audio.Media.EXTERNAL_CONTENT_URI} and return
* the songs for a particular artist.
*
* @author Andrew Neal (andrewdneal@gmail.com)
* @author Angel Leon (gubatron@gmail.com)
*/
public class ArtistSongLoader extends WrappedAsyncTaskLoader<List<Song>> {

/**
* The result
*/
private final ArrayList<Song> mSongList = Lists.newArrayList();

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

public class ArtistSongLoader extends SongLoader {
/**
* The Id of the artist the songs belong to.
*/
Expand All @@ -59,55 +42,17 @@ public ArtistSongLoader(final Context context, final Long artistId) {
mArtistID = artistId;
}

/**
* {@inheritDoc}
*/
@Override
public List<Song> loadInBackground() {
// Create the Cursor
mCursor = makeArtistSongCursor(getContext(), mArtistID);
// Gather the data
if (mCursor != null && mCursor.moveToFirst()) {
do {
// Copy the song Id
final long id = mCursor.getLong(0);

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

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

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

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

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

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

// Add everything up
mSongList.add(song);
} while (mCursor.moveToNext());
}
// Close the cursor
if (mCursor != null) {
mCursor.close();
mCursor = null;
}
return mSongList;
public Cursor makeCursor(Context context) {
return makeArtistSongCursor(getContext(), mArtistID);
}

/**
* @param context The {@link Context} to use.
* @param artistId The Id of the artist the songs belong to.
* @return The {@link Cursor} used to run the query.
*/
public static final Cursor makeArtistSongCursor(final Context context, final Long artistId) {
public static Cursor makeArtistSongCursor(final Context context, final Long artistId) {
// Match the songs up with the artist
final StringBuilder selection = new StringBuilder();
selection.append(AudioColumns.IS_MUSIC + "=1");
Expand All @@ -128,5 +73,4 @@ public static final Cursor makeArtistSongCursor(final Context context, final Lon
}, selection.toString(), null,
PreferenceUtils.getInstance(context).getArtistSongSortOrder());
}

}
63 changes: 5 additions & 58 deletions android/apollo/src/com/andrew/apollo/loaders/FavoritesLoader.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,32 +13,16 @@

import android.content.Context;
import android.database.Cursor;

import com.andrew.apollo.model.Song;
import com.andrew.apollo.provider.FavoritesStore;
import com.andrew.apollo.provider.FavoritesStore.FavoriteColumns;
import com.andrew.apollo.utils.Lists;

import java.util.ArrayList;
import java.util.List;

/**
* Used to query the {@link FavoritesStore} for the tracks marked as favorites.
*
* @author Andrew Neal (andrewdneal@gmail.com)
* @author Angel Leon (gubatron@gmail.com)
*/
public class FavoritesLoader extends WrappedAsyncTaskLoader<List<Song>> {

/**
* The result
*/
private final ArrayList<Song> mSongList = Lists.newArrayList();

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

public class FavoritesLoader extends SongLoader {
/**
* Constructor of <code>FavoritesHandler</code>
*
Expand All @@ -48,53 +32,16 @@ public FavoritesLoader(final Context context) {
super(context);
}

/**
* {@inheritDoc}
*/
@Override
public List<Song> loadInBackground() {
// Create the Cursor
mCursor = makeFavoritesCursor(getContext());
// Gather the data
if (mCursor != null && mCursor.moveToFirst()) {
do {

// Copy the song Id
final long id = mCursor.getLong(mCursor
.getColumnIndexOrThrow(FavoriteColumns.ID));

// Copy the song name
final String songName = mCursor.getString(mCursor
.getColumnIndexOrThrow(FavoriteColumns.SONGNAME));

// Copy the artist name
final String artist = mCursor.getString(mCursor
.getColumnIndexOrThrow(FavoriteColumns.ARTISTNAME));

// Copy the album name
final String album = mCursor.getString(mCursor
.getColumnIndexOrThrow(FavoriteColumns.ALBUMNAME));

// Create a new song
final Song song = new Song(id, songName, artist, album, -1);

// Add everything up
mSongList.add(song);
} while (mCursor.moveToNext());
}
// Close the cursor
if (mCursor != null) {
mCursor.close();
mCursor = null;
}
return mSongList;
public Cursor makeCursor(Context context) {
return makeFavoritesCursor(context);
}

/**
* @param context The {@link Context} to use.
* @return The {@link Cursor} used to run the favorites query.
*/
public static final Cursor makeFavoritesCursor(final Context context) {
public static Cursor makeFavoritesCursor(final Context context) {
return FavoritesStore
.getInstance(context)
.getReadableDatabase()
Expand Down

0 comments on commit 63a0a42

Please sign in to comment.