/* * Copyright (C) 2015 Naman Dwivedi * * Licensed under the GNU General Public License v3 * * This is free software: you can redistribute it and/or modify it * under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or (at your option) any later version. * * This software is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. * See the GNU General Public License for more details. */ package com.naman14.timber.dataloaders; import android.content.ContentResolver; import android.content.Context; import android.database.Cursor; import android.net.Uri; import android.provider.MediaStore; import com.naman14.timber.models.Song; import com.naman14.timber.utils.PreferencesUtility; import java.util.ArrayList; public class ArtistSongLoader { public static ArrayList<Song> getSongsForArtist(Context context, long artistID) { Cursor cursor = makeArtistSongCursor(context, artistID); ArrayList songsList = new ArrayList(); if ((cursor != null) && (cursor.moveToFirst())) do { long id = cursor.getLong(0); String title = cursor.getString(1); String artist = cursor.getString(2); String album = cursor.getString(3); int duration = cursor.getInt(4); int trackNumber = cursor.getInt(5); long albumId = cursor.getInt(6); long artistId = artistID; songsList.add(new Song(id, albumId, artistID, title, artist, album, duration, trackNumber)); } while (cursor.moveToNext()); if (cursor != null) cursor.close(); return songsList; } public static Cursor makeArtistSongCursor(Context context, long artistID) { ContentResolver contentResolver = context.getContentResolver(); final String artistSongSortOrder = PreferencesUtility.getInstance(context).getArtistSongSortOrder(); Uri uri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI; String string = "is_music=1 AND title != '' AND artist_id=" + artistID; return contentResolver.query(uri, new String[]{"_id", "title", "artist", "album", "duration", "track", "album_id"}, string, null, artistSongSortOrder); } }