Improved the artist loading mechanism.
This commit is contained in:
parent
ca238fc69a
commit
8a4d364c14
19 changed files with 160 additions and 240 deletions
|
|
@ -53,23 +53,20 @@ public class AlbumLoader {
|
|||
ArrayList<Album> albums = new ArrayList<>();
|
||||
if (songs != null) {
|
||||
for (Song song : songs) {
|
||||
Album album = get(albums, song.albumId);
|
||||
if (album == null) {
|
||||
album = new Album();
|
||||
albums.add(album);
|
||||
}
|
||||
album.songs.add(song);
|
||||
getOrCreateAlbum(albums, song.albumId).songs.add(song);
|
||||
}
|
||||
}
|
||||
return albums;
|
||||
}
|
||||
|
||||
private static Album get(ArrayList<Album> albums, int albumId) {
|
||||
private static Album getOrCreateAlbum(ArrayList<Album> albums, int albumId) {
|
||||
for (Album album : albums) {
|
||||
if (!album.songs.isEmpty() && album.songs.get(0).albumId == albumId) {
|
||||
return album;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
Album album = new Album();
|
||||
albums.add(album);
|
||||
return album;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,29 +0,0 @@
|
|||
package com.kabouzeid.gramophone.loader;
|
||||
|
||||
import android.content.Context;
|
||||
import android.provider.MediaStore;
|
||||
import android.support.annotation.NonNull;
|
||||
|
||||
import com.kabouzeid.gramophone.helper.SortOrder;
|
||||
import com.kabouzeid.gramophone.model.Album;
|
||||
import com.kabouzeid.gramophone.model.Song;
|
||||
import com.kabouzeid.gramophone.util.PreferenceUtil;
|
||||
|
||||
import java.util.ArrayList;
|
||||
|
||||
/**
|
||||
* @author Karim Abou Zeid (kabouzeid)
|
||||
*/
|
||||
public class ArtistAlbumLoader extends AlbumLoader {
|
||||
|
||||
@NonNull
|
||||
public static ArrayList<Album> getAlbums(@NonNull final Context context, final int artistId) {
|
||||
ArrayList<Song> songs = SongLoader.getSongs(SongLoader.makeSongCursor(
|
||||
context,
|
||||
MediaStore.Audio.AudioColumns.ARTIST_ID + "=?",
|
||||
new String[]{String.valueOf(artistId)},
|
||||
SortOrder.SongSortOrder.SONG_YEAR + ", " + PreferenceUtil.getInstance(context).getAlbumSongSortOrder()
|
||||
));
|
||||
return splitIntoAlbums(songs);
|
||||
}
|
||||
}
|
||||
|
|
@ -1,14 +1,13 @@
|
|||
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.ArtistColumns;
|
||||
import android.provider.MediaStore.Audio.AudioColumns;
|
||||
import android.support.annotation.NonNull;
|
||||
import android.support.annotation.Nullable;
|
||||
|
||||
import com.kabouzeid.gramophone.model.Album;
|
||||
import com.kabouzeid.gramophone.model.Artist;
|
||||
import com.kabouzeid.gramophone.model.Song;
|
||||
import com.kabouzeid.gramophone.util.PreferenceUtil;
|
||||
|
||||
import java.util.ArrayList;
|
||||
|
|
@ -17,79 +16,62 @@ import java.util.ArrayList;
|
|||
* @author Karim Abou Zeid (kabouzeid)
|
||||
*/
|
||||
public class ArtistLoader {
|
||||
|
||||
@NonNull
|
||||
public static ArrayList<Artist> getAllArtists(@NonNull Context context) {
|
||||
Cursor cursor = makeArtistCursor(context, null, null);
|
||||
return getArtists(cursor);
|
||||
public static String getSongLoaderSortOrder(Context context) {
|
||||
return PreferenceUtil.getInstance(context).getArtistSortOrder() + ", " + PreferenceUtil.getInstance(context).getArtistAlbumSortOrder() + ", " + PreferenceUtil.getInstance(context).getAlbumSongSortOrder();
|
||||
}
|
||||
|
||||
@NonNull
|
||||
public static ArrayList<Artist> getArtists(@NonNull Context context, String query) {
|
||||
Cursor cursor = makeArtistCursor(context, ArtistColumns.ARTIST + " LIKE ?", new String[]{"%" + query + "%"});
|
||||
return getArtists(cursor);
|
||||
public static ArrayList<Artist> getAllArtists(@NonNull final Context context) {
|
||||
ArrayList<Song> songs = SongLoader.getSongs(SongLoader.makeSongCursor(
|
||||
context,
|
||||
null,
|
||||
null,
|
||||
getSongLoaderSortOrder(context))
|
||||
);
|
||||
return splitIntoArtists(AlbumLoader.splitIntoAlbums(songs));
|
||||
}
|
||||
|
||||
@NonNull
|
||||
public static Artist getArtist(@NonNull Context context, int artistId) {
|
||||
Cursor cursor = makeArtistCursor(context, BaseColumns._ID + "=?", new String[]{String.valueOf(artistId)});
|
||||
return getArtist(cursor);
|
||||
public static ArrayList<Artist> getArtists(@NonNull final Context context, String query) {
|
||||
ArrayList<Song> songs = SongLoader.getSongs(SongLoader.makeSongCursor(
|
||||
context,
|
||||
AudioColumns.ARTIST + " LIKE ?",
|
||||
new String[]{"%" + query + "%"},
|
||||
getSongLoaderSortOrder(context))
|
||||
);
|
||||
return splitIntoArtists(AlbumLoader.splitIntoAlbums(songs));
|
||||
}
|
||||
|
||||
@NonNull
|
||||
public static ArrayList<Artist> getArtists(@Nullable Cursor cursor) {
|
||||
public static Artist getArtist(@NonNull final Context context, int artistId) {
|
||||
ArrayList<Song> songs = SongLoader.getSongs(SongLoader.makeSongCursor(
|
||||
context,
|
||||
AudioColumns.ARTIST_ID + "=?",
|
||||
new String[]{String.valueOf(artistId)},
|
||||
getSongLoaderSortOrder(context))
|
||||
);
|
||||
return new Artist(AlbumLoader.splitIntoAlbums(songs));
|
||||
}
|
||||
|
||||
@NonNull
|
||||
public static ArrayList<Artist> splitIntoArtists(@Nullable final ArrayList<Album> albums) {
|
||||
ArrayList<Artist> artists = new ArrayList<>();
|
||||
if (cursor != null && cursor.moveToFirst()) {
|
||||
do {
|
||||
artists.add(getArtistFromCursorImpl(cursor));
|
||||
} while (cursor.moveToNext());
|
||||
}
|
||||
|
||||
if (cursor != null) {
|
||||
cursor.close();
|
||||
if (albums != null) {
|
||||
for (Album album : albums) {
|
||||
getOrCreateArtist(artists, album.getArtistId()).albums.add(album);
|
||||
}
|
||||
}
|
||||
return artists;
|
||||
}
|
||||
|
||||
@NonNull
|
||||
public static Artist getArtist(@Nullable Cursor cursor) {
|
||||
Artist artist = new Artist();
|
||||
if (cursor != null && cursor.moveToFirst()) {
|
||||
artist = getArtistFromCursorImpl(cursor);
|
||||
}
|
||||
|
||||
if (cursor != null) {
|
||||
cursor.close();
|
||||
}
|
||||
return artist;
|
||||
}
|
||||
|
||||
@NonNull
|
||||
private static Artist getArtistFromCursorImpl(@NonNull 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);
|
||||
}
|
||||
|
||||
@Nullable
|
||||
public static Cursor makeArtistCursor(@NonNull final Context context, final String selection, final String[] values) {
|
||||
try {
|
||||
return context.getContentResolver().query(MediaStore.Audio.Artists.EXTERNAL_CONTENT_URI,
|
||||
new String[]{
|
||||
/* 0 */
|
||||
BaseColumns._ID,
|
||||
/* 1 */
|
||||
ArtistColumns.ARTIST,
|
||||
/* 2 */
|
||||
ArtistColumns.NUMBER_OF_ALBUMS,
|
||||
/* 3 */
|
||||
ArtistColumns.NUMBER_OF_TRACKS
|
||||
}, selection, values, PreferenceUtil.getInstance(context).getArtistSortOrder());
|
||||
} catch (SecurityException e) {
|
||||
return null;
|
||||
private static Artist getOrCreateArtist(ArrayList<Artist> artists, int artistId) {
|
||||
for (Artist artist : artists) {
|
||||
if (!artist.albums.isEmpty() && !artist.albums.get(0).songs.isEmpty() && artist.albums.get(0).songs.get(0).artistId == artistId) {
|
||||
return artist;
|
||||
}
|
||||
}
|
||||
Artist album = new Artist();
|
||||
artists.add(album);
|
||||
return album;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -57,7 +57,7 @@ public class SongLoader {
|
|||
if (cursor != null && cursor.moveToFirst()) {
|
||||
song = getSongFromCursorImpl(cursor);
|
||||
} else {
|
||||
song = new Song();
|
||||
song = Song.EMPTY_SONG;
|
||||
}
|
||||
if (cursor != null) {
|
||||
cursor.close();
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue