Cleaned up MediaStore loaders, preparing for album art loading directly from the file
This commit is contained in:
parent
1edd0742fc
commit
da92636180
23 changed files with 271 additions and 374 deletions
|
|
@ -2,48 +2,42 @@ package com.kabouzeid.gramophone.loader;
|
|||
|
||||
import android.content.Context;
|
||||
import android.database.Cursor;
|
||||
import android.net.Uri;
|
||||
import android.provider.BaseColumns;
|
||||
import android.provider.MediaStore;
|
||||
import android.provider.MediaStore.Audio.AlbumColumns;
|
||||
import android.provider.MediaStore.Audio.AudioColumns;
|
||||
|
||||
import com.kabouzeid.gramophone.model.Album;
|
||||
import com.kabouzeid.gramophone.util.PreferenceUtils;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* @author Karim Abou Zeid (kabouzeid)
|
||||
*/
|
||||
public class AlbumLoader {
|
||||
|
||||
public static List<Album> getAllAlbums(Context context) {
|
||||
public static ArrayList<Album> getAllAlbums(final Context context) {
|
||||
Cursor cursor = makeAlbumCursor(context, null, null);
|
||||
return getAlbums(cursor);
|
||||
}
|
||||
|
||||
public static List<Album> getAlbums(Context context, String query) {
|
||||
Cursor cursor = makeAlbumCursor(context, MediaStore.Audio.AlbumColumns.ALBUM + " LIKE ?", new String[]{"%" + query + "%"});
|
||||
public static ArrayList<Album> getAlbums(final Context context, String query) {
|
||||
Cursor cursor = makeAlbumCursor(context, AlbumColumns.ALBUM + " LIKE ?", new String[]{"%" + query + "%"});
|
||||
return getAlbums(cursor);
|
||||
}
|
||||
|
||||
public static Album getAlbum(Context context, int albumId) {
|
||||
public static Album getAlbum(final Context context, int albumId) {
|
||||
Cursor cursor = makeAlbumCursor(context, BaseColumns._ID + "=?", new String[]{String.valueOf(albumId)});
|
||||
return getAlbum(cursor);
|
||||
}
|
||||
|
||||
public static List<Album> getAlbums(Cursor cursor) {
|
||||
List<Album> albums = new ArrayList<>();
|
||||
public static ArrayList<Album> getAlbums(final Cursor cursor) {
|
||||
ArrayList<Album> albums = new ArrayList<>();
|
||||
if (cursor != null && cursor.moveToFirst()) {
|
||||
do {
|
||||
final String albumName = cursor.getString(1);
|
||||
final int id = cursor.getInt(0);
|
||||
final String artist = cursor.getString(2);
|
||||
final int artistId = cursor.getInt(3);
|
||||
final int songCount = cursor.getInt(4);
|
||||
final int year = cursor.getInt(5);
|
||||
|
||||
final Album album = new Album(id, albumName, artist, artistId, songCount, year);
|
||||
albums.add(album);
|
||||
albums.add(getAlbumFromCursorImpl(cursor));
|
||||
} while (cursor.moveToNext());
|
||||
}
|
||||
if (cursor != null) {
|
||||
|
|
@ -52,17 +46,10 @@ public class AlbumLoader {
|
|||
return albums;
|
||||
}
|
||||
|
||||
public static Album getAlbum(Cursor cursor) {
|
||||
public static Album getAlbum(final Cursor cursor) {
|
||||
Album album = new Album();
|
||||
if (cursor != null && cursor.moveToFirst()) {
|
||||
final int id = cursor.getInt(0);
|
||||
final String albumName = cursor.getString(1);
|
||||
final String artist = cursor.getString(2);
|
||||
final int artistId = cursor.getInt(3);
|
||||
final int songCount = cursor.getInt(4);
|
||||
final int year = cursor.getInt(5);
|
||||
|
||||
album = new Album(id, albumName, artist, artistId, songCount, year);
|
||||
album = getAlbumFromCursorImpl(cursor);
|
||||
}
|
||||
|
||||
if (cursor != null) {
|
||||
|
|
@ -71,21 +58,40 @@ public class AlbumLoader {
|
|||
return album;
|
||||
}
|
||||
|
||||
private static Album getAlbumFromCursorImpl(final Cursor cursor) {
|
||||
final int id = cursor.getInt(0);
|
||||
final String albumName = cursor.getString(1);
|
||||
final String artist = cursor.getString(2);
|
||||
final int artistId = cursor.getInt(3);
|
||||
final int songCount = cursor.getInt(4);
|
||||
final int year = cursor.getInt(5);
|
||||
|
||||
return new Album(id, albumName, artist, artistId, songCount, year);
|
||||
}
|
||||
|
||||
public static Cursor makeAlbumCursor(final Context context, final String selection, final String[] values) {
|
||||
return context.getContentResolver().query(MediaStore.Audio.Albums.EXTERNAL_CONTENT_URI,
|
||||
return makeAlbumCursor(context, selection, values, PreferenceUtils.getInstance(context).getAlbumSortOrder());
|
||||
}
|
||||
|
||||
public static Cursor makeAlbumCursor(final Context context, final String selection, final String[] values, final String sortOrder) {
|
||||
return makeAlbumCursor(context, MediaStore.Audio.Albums.EXTERNAL_CONTENT_URI, selection, values, sortOrder);
|
||||
}
|
||||
|
||||
public static Cursor makeAlbumCursor(final Context context, final Uri contentUri, final String selection, final String[] values, final String sortOrder) {
|
||||
return context.getContentResolver().query(contentUri,
|
||||
new String[]{
|
||||
/* 0 */
|
||||
BaseColumns._ID,
|
||||
/* 1 */
|
||||
MediaStore.Audio.AlbumColumns.ALBUM,
|
||||
AlbumColumns.ALBUM,
|
||||
/* 2 */
|
||||
MediaStore.Audio.AlbumColumns.ARTIST,
|
||||
AlbumColumns.ARTIST,
|
||||
/* 3 */
|
||||
MediaStore.Audio.Media.ARTIST_ID,
|
||||
AudioColumns.ARTIST_ID,
|
||||
/* 4 */
|
||||
MediaStore.Audio.AlbumColumns.NUMBER_OF_SONGS,
|
||||
AlbumColumns.NUMBER_OF_SONGS,
|
||||
/* 5 */
|
||||
MediaStore.Audio.AlbumColumns.FIRST_YEAR,
|
||||
}, selection, values, PreferenceUtils.getInstance(context).getAlbumSortOrder());
|
||||
AlbumColumns.FIRST_YEAR,
|
||||
}, selection, values, sortOrder);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -2,7 +2,6 @@ package com.kabouzeid.gramophone.loader;
|
|||
|
||||
import android.content.Context;
|
||||
import android.database.Cursor;
|
||||
import android.provider.BaseColumns;
|
||||
import android.provider.MediaStore;
|
||||
|
||||
import com.kabouzeid.gramophone.model.Song;
|
||||
|
|
@ -16,47 +15,17 @@ import java.util.ArrayList;
|
|||
public class AlbumSongLoader {
|
||||
|
||||
public static ArrayList<Song> getAlbumSongList(final Context context, final int albumId) {
|
||||
Cursor cursor = makeAlbumSongCursor(context, albumId);
|
||||
ArrayList<Song> songs = new ArrayList<>();
|
||||
if (cursor != null && cursor.moveToFirst()) {
|
||||
do {
|
||||
final int id = cursor.getInt(0);
|
||||
final String songName = cursor.getString(1);
|
||||
final String artist = cursor.getString(2);
|
||||
final String album = cursor.getString(3);
|
||||
final long duration = cursor.getLong(4);
|
||||
final int trackNumber = cursor.getInt(5);
|
||||
final int artistId = cursor.getInt(6);
|
||||
|
||||
final Song song = new Song(id, albumId, artistId, songName, artist, album, duration, trackNumber);
|
||||
songs.add(song);
|
||||
} while (cursor.moveToNext());
|
||||
}
|
||||
if (cursor != null)
|
||||
cursor.close();
|
||||
return songs;
|
||||
return SongLoader.getSongs(makeAlbumSongCursor(context, albumId));
|
||||
}
|
||||
|
||||
public static Cursor makeAlbumSongCursor(final Context context, final int albumId) {
|
||||
return context.getContentResolver().query(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI,
|
||||
return SongLoader.makeSongCursor(
|
||||
context,
|
||||
MediaStore.Audio.AudioColumns.ALBUM_ID + "=?",
|
||||
new String[]{
|
||||
/* 0 */
|
||||
BaseColumns._ID,
|
||||
/* 1 */
|
||||
MediaStore.Audio.AudioColumns.TITLE,
|
||||
/* 2 */
|
||||
MediaStore.Audio.AudioColumns.ARTIST,
|
||||
/* 3 */
|
||||
MediaStore.Audio.AudioColumns.ALBUM,
|
||||
/* 4 */
|
||||
MediaStore.Audio.AudioColumns.DURATION,
|
||||
/* 5 */
|
||||
MediaStore.Audio.AudioColumns.TRACK,
|
||||
/* 6 */
|
||||
MediaStore.Audio.AudioColumns.ARTIST_ID
|
||||
}, (MediaStore.Audio.AudioColumns.IS_MUSIC + "=1") + " AND " +
|
||||
MediaStore.Audio.AudioColumns.TITLE + " != ''" + " AND " +
|
||||
MediaStore.Audio.AudioColumns.ALBUM_ID + "=" + albumId, null,
|
||||
PreferenceUtils.getInstance(context).getAlbumSongSortOrder());
|
||||
String.valueOf(albumId)
|
||||
},
|
||||
PreferenceUtils.getInstance(context).getAlbumSongSortOrder()
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -2,7 +2,6 @@ package com.kabouzeid.gramophone.loader;
|
|||
|
||||
import android.content.Context;
|
||||
import android.database.Cursor;
|
||||
import android.provider.BaseColumns;
|
||||
import android.provider.MediaStore;
|
||||
|
||||
import com.kabouzeid.gramophone.model.Album;
|
||||
|
|
@ -16,39 +15,15 @@ import java.util.ArrayList;
|
|||
public class ArtistAlbumLoader {
|
||||
|
||||
public static ArrayList<Album> getArtistAlbumList(final Context context, final int artistId) {
|
||||
Cursor cursor = makeArtistAlbumCursor(context, artistId);
|
||||
ArrayList<Album> albums = new ArrayList<>();
|
||||
if (cursor != null && cursor.moveToFirst()) {
|
||||
do {
|
||||
final int id = cursor.getInt(0);
|
||||
final String albumName = cursor.getString(1);
|
||||
final String artist = cursor.getString(2);
|
||||
final int songCount = cursor.getInt(3);
|
||||
final int year = cursor.getInt(4);
|
||||
|
||||
final Album album = new Album(id, albumName, artist, artistId, songCount, year);
|
||||
albums.add(album);
|
||||
} while (cursor.moveToNext());
|
||||
}
|
||||
if (cursor != null)
|
||||
cursor.close();
|
||||
return albums;
|
||||
return AlbumLoader.getAlbums(makeArtistAlbumCursor(context, artistId));
|
||||
}
|
||||
|
||||
public static Cursor makeArtistAlbumCursor(final Context context, final int artistId) {
|
||||
if (artistId == -1) return null;
|
||||
return context.getContentResolver().query(
|
||||
MediaStore.Audio.Artists.Albums.getContentUri("external", artistId), new String[]{
|
||||
/* 0 */
|
||||
BaseColumns._ID,
|
||||
/* 1 */
|
||||
MediaStore.Audio.AlbumColumns.ALBUM,
|
||||
/* 2 */
|
||||
MediaStore.Audio.AlbumColumns.ARTIST,
|
||||
/* 3 */
|
||||
MediaStore.Audio.AlbumColumns.NUMBER_OF_SONGS,
|
||||
/* 4 */
|
||||
MediaStore.Audio.AlbumColumns.FIRST_YEAR,
|
||||
}, null, null, PreferenceUtils.getInstance(context).getArtistAlbumSortOrder());
|
||||
return AlbumLoader.makeAlbumCursor(context,
|
||||
MediaStore.Audio.Artists.Albums.getContentUri("external", artistId),
|
||||
null,
|
||||
null,
|
||||
PreferenceUtils.getInstance(context).getArtistAlbumSortOrder()
|
||||
);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -4,25 +4,25 @@ import android.content.Context;
|
|||
import android.database.Cursor;
|
||||
import android.provider.BaseColumns;
|
||||
import android.provider.MediaStore;
|
||||
import android.provider.MediaStore.Audio.ArtistColumns;
|
||||
|
||||
import com.kabouzeid.gramophone.model.Artist;
|
||||
import com.kabouzeid.gramophone.util.PreferenceUtils;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* @author Karim Abou Zeid (kabouzeid)
|
||||
*/
|
||||
public class ArtistLoader {
|
||||
|
||||
public static List<Artist> getAllArtists(Context context) {
|
||||
public static ArrayList<Artist> getAllArtists(Context context) {
|
||||
Cursor cursor = makeArtistCursor(context, null, null);
|
||||
return getArtists(cursor);
|
||||
}
|
||||
|
||||
public static List<Artist> getArtists(Context context, String query) {
|
||||
Cursor cursor = makeArtistCursor(context, MediaStore.Audio.ArtistColumns.ARTIST + " LIKE ?", new String[]{"%" + query + "%"});
|
||||
public static ArrayList<Artist> getArtists(Context context, String query) {
|
||||
Cursor cursor = makeArtistCursor(context, ArtistColumns.ARTIST + " LIKE ?", new String[]{"%" + query + "%"});
|
||||
return getArtists(cursor);
|
||||
}
|
||||
|
||||
|
|
@ -31,17 +31,11 @@ public class ArtistLoader {
|
|||
return getArtist(cursor);
|
||||
}
|
||||
|
||||
public static List<Artist> getArtists(Cursor cursor) {
|
||||
List<Artist> artists = new ArrayList<>();
|
||||
public static ArrayList<Artist> getArtists(Cursor cursor) {
|
||||
ArrayList<Artist> artists = new ArrayList<>();
|
||||
if (cursor != null && cursor.moveToFirst()) {
|
||||
do {
|
||||
final String artistName = cursor.getString(1);
|
||||
final int id = cursor.getInt(0);
|
||||
final int albumCount = cursor.getInt(2);
|
||||
final int songCount = cursor.getInt(3);
|
||||
|
||||
final Artist artist = new Artist(id, artistName, albumCount, songCount);
|
||||
artists.add(artist);
|
||||
artists.add(getArtistFromCursorImpl(cursor));
|
||||
} while (cursor.moveToNext());
|
||||
}
|
||||
|
||||
|
|
@ -54,12 +48,7 @@ public class ArtistLoader {
|
|||
public static Artist getArtist(Cursor cursor) {
|
||||
Artist artist = new Artist();
|
||||
if (cursor != null && cursor.moveToFirst()) {
|
||||
final int id = cursor.getInt(0);
|
||||
final String artistName = cursor.getString(1);
|
||||
final int albumCount = cursor.getInt(2);
|
||||
final int songCount = cursor.getInt(3);
|
||||
|
||||
artist = new Artist(id, artistName, albumCount, songCount);
|
||||
artist = getArtistFromCursorImpl(cursor);
|
||||
}
|
||||
|
||||
if (cursor != null) {
|
||||
|
|
@ -68,17 +57,26 @@ public class ArtistLoader {
|
|||
return artist;
|
||||
}
|
||||
|
||||
private static Artist getArtistFromCursorImpl(Cursor cursor) {
|
||||
final int id = cursor.getInt(0);
|
||||
final String artistName = cursor.getString(1);
|
||||
final int albumCount = cursor.getInt(2);
|
||||
final int songCount = cursor.getInt(3);
|
||||
|
||||
return new Artist(id, artistName, albumCount, songCount);
|
||||
}
|
||||
|
||||
public static Cursor makeArtistCursor(final Context context, final String selection, final String[] values) {
|
||||
return context.getContentResolver().query(MediaStore.Audio.Artists.EXTERNAL_CONTENT_URI,
|
||||
new String[]{
|
||||
/* 0 */
|
||||
BaseColumns._ID,
|
||||
/* 1 */
|
||||
MediaStore.Audio.ArtistColumns.ARTIST,
|
||||
ArtistColumns.ARTIST,
|
||||
/* 2 */
|
||||
MediaStore.Audio.ArtistColumns.NUMBER_OF_ALBUMS,
|
||||
ArtistColumns.NUMBER_OF_ALBUMS,
|
||||
/* 3 */
|
||||
MediaStore.Audio.ArtistColumns.NUMBER_OF_TRACKS
|
||||
ArtistColumns.NUMBER_OF_TRACKS
|
||||
}, selection, values, PreferenceUtils.getInstance(context).getArtistSortOrder());
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -2,7 +2,6 @@ package com.kabouzeid.gramophone.loader;
|
|||
|
||||
import android.content.Context;
|
||||
import android.database.Cursor;
|
||||
import android.provider.BaseColumns;
|
||||
import android.provider.MediaStore;
|
||||
|
||||
import com.kabouzeid.gramophone.model.Song;
|
||||
|
|
@ -16,47 +15,17 @@ import java.util.ArrayList;
|
|||
public class ArtistSongLoader {
|
||||
|
||||
public static ArrayList<Song> getArtistSongList(final Context context, final int artistId) {
|
||||
Cursor cursor = makeArtistSongCursor(context, artistId);
|
||||
ArrayList<Song> songs = new ArrayList<>();
|
||||
if (cursor != null && cursor.moveToFirst()) {
|
||||
do {
|
||||
final int id = cursor.getInt(0);
|
||||
final String songName = cursor.getString(1);
|
||||
final String artist = cursor.getString(2);
|
||||
final String album = cursor.getString(3);
|
||||
final long duration = cursor.getLong(4);
|
||||
final int trackNumber = cursor.getInt(5);
|
||||
final int albumId = cursor.getInt(6);
|
||||
|
||||
final Song song = new Song(id, albumId, artistId, songName, artist, album, duration, trackNumber);
|
||||
songs.add(song);
|
||||
} while (cursor.moveToNext());
|
||||
}
|
||||
if (cursor != null)
|
||||
cursor.close();
|
||||
return songs;
|
||||
return SongLoader.getSongs(makeArtistSongCursor(context, artistId));
|
||||
}
|
||||
|
||||
public static Cursor makeArtistSongCursor(final Context context, final int artistId) {
|
||||
return context.getContentResolver().query(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI,
|
||||
return SongLoader.makeSongCursor(
|
||||
context,
|
||||
MediaStore.Audio.AudioColumns.ARTIST_ID + "=?",
|
||||
new String[]{
|
||||
/* 0 */
|
||||
BaseColumns._ID,
|
||||
/* 1 */
|
||||
MediaStore.Audio.AudioColumns.TITLE,
|
||||
/* 2 */
|
||||
MediaStore.Audio.AudioColumns.ARTIST,
|
||||
/* 3 */
|
||||
MediaStore.Audio.AudioColumns.ALBUM,
|
||||
/* 4 */
|
||||
MediaStore.Audio.AudioColumns.DURATION,
|
||||
/* 5 */
|
||||
MediaStore.Audio.AudioColumns.TRACK,
|
||||
/* 6 */
|
||||
MediaStore.Audio.AudioColumns.ALBUM_ID
|
||||
}, (MediaStore.Audio.AudioColumns.IS_MUSIC + "=1") + " AND " +
|
||||
MediaStore.Audio.AudioColumns.TITLE + " != ''" + " AND " +
|
||||
MediaStore.Audio.AudioColumns.ARTIST_ID + "=" + artistId, null,
|
||||
PreferenceUtils.getInstance(context).getArtistSongSortOrder());
|
||||
String.valueOf(artistId)
|
||||
},
|
||||
PreferenceUtils.getInstance(context).getArtistSongSortOrder()
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -2,9 +2,7 @@ package com.kabouzeid.gramophone.loader;
|
|||
|
||||
import android.content.Context;
|
||||
import android.database.Cursor;
|
||||
import android.provider.BaseColumns;
|
||||
import android.provider.MediaStore;
|
||||
import android.provider.MediaStore.Audio.AudioColumns;
|
||||
|
||||
import com.kabouzeid.gramophone.model.Song;
|
||||
import com.kabouzeid.gramophone.util.PreferenceUtils;
|
||||
|
|
@ -25,31 +23,10 @@ public class LastAddedLoader {
|
|||
cutoff = fourWeeksAgo;
|
||||
}
|
||||
|
||||
//noinspection StringBufferReplaceableByString
|
||||
final StringBuilder selection = new StringBuilder();
|
||||
selection.append(AudioColumns.IS_MUSIC + "=1");
|
||||
selection.append(" AND " + AudioColumns.TITLE + " != ''");
|
||||
selection.append(" AND " + MediaStore.Audio.Media.DATE_ADDED + ">");
|
||||
selection.append(cutoff);
|
||||
|
||||
return context.getContentResolver().query(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI,
|
||||
new String[]{
|
||||
/* 0 */
|
||||
BaseColumns._ID,
|
||||
/* 1 */
|
||||
MediaStore.Audio.AudioColumns.TITLE,
|
||||
/* 2 */
|
||||
MediaStore.Audio.AudioColumns.ARTIST,
|
||||
/* 3 */
|
||||
MediaStore.Audio.AudioColumns.ALBUM,
|
||||
/* 4 */
|
||||
MediaStore.Audio.AudioColumns.DURATION,
|
||||
/* 5 */
|
||||
MediaStore.Audio.AudioColumns.TRACK,
|
||||
/* 6 */
|
||||
MediaStore.Audio.AudioColumns.ARTIST_ID,
|
||||
/* 7 */
|
||||
MediaStore.Audio.AudioColumns.ALBUM_ID
|
||||
}, selection.toString(), null, MediaStore.Audio.Media.DATE_ADDED + " DESC");
|
||||
return SongLoader.makeSongCursor(
|
||||
context,
|
||||
MediaStore.Audio.Media.DATE_ADDED + ">",
|
||||
new String[]{String.valueOf(cutoff)},
|
||||
MediaStore.Audio.Media.DATE_ADDED + " DESC");
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -13,44 +13,47 @@ import java.util.List;
|
|||
|
||||
public class PlaylistLoader {
|
||||
|
||||
public static Playlist getPlaylist(final Context context, final int playlistId) {
|
||||
Playlist playlist = new Playlist();
|
||||
Cursor cursor = makePlaylistCursor(context, BaseColumns._ID + "=?", new String[]{String.valueOf(playlistId)});
|
||||
public static List<Playlist> getAllPlaylists(final Context context) {
|
||||
return getAllPlaylists(makePlaylistCursor(context, null, null));
|
||||
}
|
||||
|
||||
if (cursor != null && cursor.moveToFirst()) {
|
||||
final int id = cursor.getInt(0);
|
||||
final String name = cursor.getString(1);
|
||||
playlist = new Playlist(id, name);
|
||||
}
|
||||
if (cursor != null)
|
||||
cursor.close();
|
||||
return playlist;
|
||||
public static Playlist getPlaylist(final Context context, final int playlistId) {
|
||||
return getPlaylist(makePlaylistCursor(
|
||||
context,
|
||||
BaseColumns._ID + "=?",
|
||||
new String[]{
|
||||
String.valueOf(playlistId)
|
||||
}
|
||||
));
|
||||
}
|
||||
|
||||
public static Playlist getPlaylist(final Context context, final String playlistName) {
|
||||
return getPlaylist(makePlaylistCursor(
|
||||
context,
|
||||
PlaylistsColumns.NAME + "=?",
|
||||
new String[]{
|
||||
playlistName
|
||||
}
|
||||
));
|
||||
}
|
||||
|
||||
public static Playlist getPlaylist(final Cursor cursor) {
|
||||
Playlist playlist = new Playlist();
|
||||
Cursor cursor = makePlaylistCursor(context, PlaylistsColumns.NAME + "=?", new String[]{playlistName});
|
||||
|
||||
if (cursor != null && cursor.moveToFirst()) {
|
||||
final int id = cursor.getInt(0);
|
||||
final String name = cursor.getString(1);
|
||||
playlist = new Playlist(id, name);
|
||||
playlist = getPlaylistFromCursorImpl(cursor);
|
||||
}
|
||||
if (cursor != null)
|
||||
cursor.close();
|
||||
return playlist;
|
||||
}
|
||||
|
||||
public static List<Playlist> getAllPlaylists(final Context context) {
|
||||
public static List<Playlist> getAllPlaylists(final Cursor cursor) {
|
||||
List<Playlist> playlists = new ArrayList<>();
|
||||
Cursor cursor = makePlaylistCursor(context, null, null);
|
||||
|
||||
if (cursor != null && cursor.moveToFirst()) {
|
||||
do {
|
||||
final int id = cursor.getInt(0);
|
||||
final String name = cursor.getString(1);
|
||||
final Playlist playlist = new Playlist(id, name);
|
||||
playlists.add(playlist);
|
||||
playlists.add(getPlaylistFromCursorImpl(cursor));
|
||||
} while (cursor.moveToNext());
|
||||
}
|
||||
if (cursor != null)
|
||||
|
|
@ -58,6 +61,12 @@ public class PlaylistLoader {
|
|||
return playlists;
|
||||
}
|
||||
|
||||
private static Playlist getPlaylistFromCursorImpl(final Cursor cursor) {
|
||||
final int id = cursor.getInt(0);
|
||||
final String name = cursor.getString(1);
|
||||
return new Playlist(id, name);
|
||||
}
|
||||
|
||||
public static Cursor makePlaylistCursor(final Context context, final String selection, final String[] values) {
|
||||
return context.getContentResolver().query(MediaStore.Audio.Playlists.EXTERNAL_CONTENT_URI,
|
||||
new String[]{
|
||||
|
|
@ -67,4 +76,4 @@ public class PlaylistLoader {
|
|||
PlaylistsColumns.NAME
|
||||
}, selection, values, MediaStore.Audio.Playlists.DEFAULT_SORT_ORDER);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -11,25 +11,13 @@ import java.util.ArrayList;
|
|||
|
||||
public class PlaylistSongLoader {
|
||||
|
||||
public static ArrayList<PlaylistSong> getPlaylistSongList(final Context context, final int playlistID) {
|
||||
public static ArrayList<PlaylistSong> getPlaylistSongList(final Context context, final int playlistId) {
|
||||
ArrayList<PlaylistSong> songs = new ArrayList<>();
|
||||
Cursor cursor = makePlaylistSongCursor(context, playlistID);
|
||||
Cursor cursor = makePlaylistSongCursor(context, playlistId);
|
||||
|
||||
if (cursor != null && cursor.moveToFirst()) {
|
||||
do {
|
||||
final int id = cursor.getInt(0);
|
||||
final String songName = cursor.getString(1);
|
||||
final String artist = cursor.getString(2);
|
||||
final String album = cursor.getString(3);
|
||||
final long duration = cursor.getLong(4);
|
||||
final int trackNumber = cursor.getInt(5);
|
||||
final int albumId = cursor.getInt(6);
|
||||
final int artistId = cursor.getInt(7);
|
||||
final int idInPlaylist = cursor.getInt(8);
|
||||
|
||||
final PlaylistSong song = new PlaylistSong(id, albumId, artistId, songName, artist, album, duration, trackNumber, playlistID, idInPlaylist);
|
||||
|
||||
songs.add(song);
|
||||
songs.add(getPlaylistSongFromCursorImpl(cursor));
|
||||
} while (cursor.moveToNext());
|
||||
}
|
||||
if (cursor != null) {
|
||||
|
|
@ -38,9 +26,25 @@ public class PlaylistSongLoader {
|
|||
return songs;
|
||||
}
|
||||
|
||||
public static Cursor makePlaylistSongCursor(final Context context, final int playlistID) {
|
||||
private static PlaylistSong getPlaylistSongFromCursorImpl(Cursor cursor) {
|
||||
final int id = cursor.getInt(0);
|
||||
final String songName = cursor.getString(1);
|
||||
final String artist = cursor.getString(2);
|
||||
final String album = cursor.getString(3);
|
||||
final long duration = cursor.getLong(4);
|
||||
final int trackNumber = cursor.getInt(5);
|
||||
final int albumId = cursor.getInt(6);
|
||||
final int artistId = cursor.getInt(7);
|
||||
final String data = cursor.getString(8);
|
||||
final int idInPlaylist = cursor.getInt(9);
|
||||
final int playlistId = cursor.getInt(10);
|
||||
|
||||
return new PlaylistSong(id, albumId, artistId, songName, artist, album, duration, trackNumber, data, playlistId, idInPlaylist);
|
||||
}
|
||||
|
||||
public static Cursor makePlaylistSongCursor(final Context context, final int playlistId) {
|
||||
return context.getContentResolver().query(
|
||||
MediaStore.Audio.Playlists.Members.getContentUri("external", playlistID),
|
||||
MediaStore.Audio.Playlists.Members.getContentUri("external", playlistId),
|
||||
new String[]{
|
||||
/* 0 */
|
||||
MediaStore.Audio.Playlists.Members.AUDIO_ID,
|
||||
|
|
@ -59,8 +63,12 @@ public class PlaylistSongLoader {
|
|||
/* 7 */
|
||||
AudioColumns.ARTIST_ID,
|
||||
/* 8 */
|
||||
AudioColumns.DATA,
|
||||
/* 9 */
|
||||
MediaStore.Audio.Playlists.Members.PLAYLIST_ID,
|
||||
/* 10 */
|
||||
MediaStore.Audio.Playlists.Members._ID
|
||||
}, (AudioColumns.IS_MUSIC + "=1") + " AND " + AudioColumns.TITLE + " != ''", null,
|
||||
}, SongLoader.BASE_SELECTION, null,
|
||||
MediaStore.Audio.Playlists.Members.DEFAULT_SORT_ORDER);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,60 +0,0 @@
|
|||
package com.kabouzeid.gramophone.loader;
|
||||
|
||||
import android.content.Context;
|
||||
import android.database.Cursor;
|
||||
import android.provider.BaseColumns;
|
||||
import android.provider.MediaStore;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* @author Karim Abou Zeid (kabouzeid)
|
||||
*/
|
||||
public class SongFilePathLoader {
|
||||
public static final String TAG = SongFilePathLoader.class.getSimpleName();
|
||||
|
||||
public static List<String> getSongFilePaths(Context context, int[] queryIds) {
|
||||
StringBuilder selectionBuilder = new StringBuilder();
|
||||
selectionBuilder.append(BaseColumns._ID).append(" IN(");
|
||||
for (int i = 0; i < queryIds.length; i++) {
|
||||
selectionBuilder.append(queryIds[i]);
|
||||
if (i < queryIds.length - 1) {
|
||||
selectionBuilder.append(",");
|
||||
}
|
||||
}
|
||||
selectionBuilder.append(")");
|
||||
Cursor cursor = makeSongFilePathCursor(context, selectionBuilder.toString());
|
||||
List<String> songFiles = new ArrayList<>();
|
||||
if (cursor != null && cursor.moveToFirst()) {
|
||||
do {
|
||||
songFiles.add(cursor.getString(0));
|
||||
} while (cursor.moveToNext());
|
||||
}
|
||||
|
||||
if (cursor != null) {
|
||||
cursor.close();
|
||||
}
|
||||
return songFiles;
|
||||
}
|
||||
|
||||
public static String getSongFilePath(Context context, int queryId) {
|
||||
try {
|
||||
return getSongFilePaths(context, new int[]{queryId}).get(0);
|
||||
} catch (Exception e) {
|
||||
return "";
|
||||
}
|
||||
}
|
||||
|
||||
public static Cursor makeSongFilePathCursor(final Context context) {
|
||||
return makeSongFilePathCursor(context, null);
|
||||
}
|
||||
|
||||
public static Cursor makeSongFilePathCursor(final Context context, String selection) {
|
||||
return context.getContentResolver().query(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI,
|
||||
new String[]{
|
||||
/* 0 */
|
||||
MediaStore.Audio.AudioColumns.DATA,
|
||||
}, selection, null, null);
|
||||
}
|
||||
}
|
||||
|
|
@ -4,6 +4,7 @@ import android.content.Context;
|
|||
import android.database.Cursor;
|
||||
import android.provider.BaseColumns;
|
||||
import android.provider.MediaStore;
|
||||
import android.provider.MediaStore.Audio.AudioColumns;
|
||||
|
||||
import com.kabouzeid.gramophone.model.Song;
|
||||
import com.kabouzeid.gramophone.util.PreferenceUtils;
|
||||
|
|
@ -14,7 +15,7 @@ import java.util.ArrayList;
|
|||
* @author Karim Abou Zeid (kabouzeid)
|
||||
*/
|
||||
public class SongLoader {
|
||||
private static final String BASE_SELECTION = MediaStore.Audio.AudioColumns.IS_MUSIC + "=1" + " AND " + MediaStore.Audio.AudioColumns.TITLE + " != ''";
|
||||
protected static final String BASE_SELECTION = AudioColumns.IS_MUSIC + "=1" + " AND " + AudioColumns.TITLE + " != ''";
|
||||
|
||||
public static ArrayList<Song> getAllSongs(Context context) {
|
||||
Cursor cursor = makeSongCursor(context, null, null);
|
||||
|
|
@ -22,12 +23,12 @@ public class SongLoader {
|
|||
}
|
||||
|
||||
public static ArrayList<Song> getSongs(final Context context, final String query) {
|
||||
Cursor cursor = makeSongCursor(context, MediaStore.Audio.AudioColumns.TITLE + " LIKE ?", new String[]{"%" + query + "%"});
|
||||
Cursor cursor = makeSongCursor(context, AudioColumns.TITLE + " LIKE ?", new String[]{"%" + query + "%"});
|
||||
return getSongs(cursor);
|
||||
}
|
||||
|
||||
public static Song getSong(final Context context, final int queryId) {
|
||||
Cursor cursor = makeSongCursor(context, MediaStore.Audio.AudioColumns._ID + "=?", new String[]{String.valueOf(queryId)});
|
||||
Cursor cursor = makeSongCursor(context, AudioColumns._ID + "=?", new String[]{String.valueOf(queryId)});
|
||||
return getSong(cursor);
|
||||
}
|
||||
|
||||
|
|
@ -35,17 +36,7 @@ public class SongLoader {
|
|||
ArrayList<Song> songs = new ArrayList<>();
|
||||
if (cursor != null && cursor.moveToFirst()) {
|
||||
do {
|
||||
final String songName = cursor.getString(1);
|
||||
final int id = cursor.getInt(0);
|
||||
final String artist = cursor.getString(2);
|
||||
final String album = cursor.getString(3);
|
||||
final long duration = cursor.getLong(4);
|
||||
final int trackNumber = cursor.getInt(5);
|
||||
final int artistId = cursor.getInt(6);
|
||||
final int albumId = cursor.getInt(7);
|
||||
|
||||
final Song song = new Song(id, albumId, artistId, songName, artist, album, duration, trackNumber);
|
||||
songs.add(song);
|
||||
songs.add(getSongFromCursorImpl(cursor));
|
||||
} while (cursor.moveToNext());
|
||||
}
|
||||
|
||||
|
|
@ -57,15 +48,7 @@ public class SongLoader {
|
|||
public static Song getSong(Cursor cursor) {
|
||||
Song song = new Song();
|
||||
if (cursor != null && cursor.moveToFirst()) {
|
||||
final int id = cursor.getInt(0);
|
||||
final String songName = cursor.getString(1);
|
||||
final String artist = cursor.getString(2);
|
||||
final String album = cursor.getString(3);
|
||||
final long duration = cursor.getLong(4);
|
||||
final int trackNumber = cursor.getInt(5);
|
||||
final int artistId = cursor.getInt(6);
|
||||
final int albumId = cursor.getInt(7);
|
||||
song = new Song(id, albumId, artistId, songName, artist, album, duration, trackNumber);
|
||||
song = getSongFromCursorImpl(cursor);
|
||||
}
|
||||
if (cursor != null) {
|
||||
cursor.close();
|
||||
|
|
@ -73,7 +56,24 @@ public class SongLoader {
|
|||
return song;
|
||||
}
|
||||
|
||||
private static Song getSongFromCursorImpl(Cursor cursor) {
|
||||
final int id = cursor.getInt(0);
|
||||
final String songName = cursor.getString(1);
|
||||
final String artist = cursor.getString(2);
|
||||
final String album = cursor.getString(3);
|
||||
final long duration = cursor.getLong(4);
|
||||
final int trackNumber = cursor.getInt(5);
|
||||
final int artistId = cursor.getInt(6);
|
||||
final int albumId = cursor.getInt(7);
|
||||
final String data = cursor.getString(8);
|
||||
return new Song(id, albumId, artistId, songName, artist, album, duration, trackNumber, data);
|
||||
}
|
||||
|
||||
public static Cursor makeSongCursor(final Context context, final String selection, final String[] values) {
|
||||
return makeSongCursor(context, selection, values, PreferenceUtils.getInstance(context).getSongSortOrder());
|
||||
}
|
||||
|
||||
public static Cursor makeSongCursor(final Context context, final String selection, final String[] values, final String sortOrder) {
|
||||
String baseSelection = BASE_SELECTION;
|
||||
if (selection != null && !selection.trim().equals("")) {
|
||||
baseSelection += " AND " + selection;
|
||||
|
|
@ -84,19 +84,21 @@ public class SongLoader {
|
|||
/* 0 */
|
||||
BaseColumns._ID,
|
||||
/* 1 */
|
||||
MediaStore.Audio.AudioColumns.TITLE,
|
||||
AudioColumns.TITLE,
|
||||
/* 2 */
|
||||
MediaStore.Audio.AudioColumns.ARTIST,
|
||||
AudioColumns.ARTIST,
|
||||
/* 3 */
|
||||
MediaStore.Audio.AudioColumns.ALBUM,
|
||||
AudioColumns.ALBUM,
|
||||
/* 4 */
|
||||
MediaStore.Audio.AudioColumns.DURATION,
|
||||
AudioColumns.DURATION,
|
||||
/* 5 */
|
||||
MediaStore.Audio.AudioColumns.TRACK,
|
||||
AudioColumns.TRACK,
|
||||
/* 6 */
|
||||
MediaStore.Audio.AudioColumns.ARTIST_ID,
|
||||
AudioColumns.ARTIST_ID,
|
||||
/* 7 */
|
||||
MediaStore.Audio.AudioColumns.ALBUM_ID
|
||||
}, baseSelection, values, PreferenceUtils.getInstance(context).getSongSortOrder());
|
||||
AudioColumns.ALBUM_ID,
|
||||
/* 8 */
|
||||
AudioColumns.DATA
|
||||
}, baseSelection, values, sortOrder);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue