/*
* 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 AlbumSongLoader {
private static final long[] sEmptyList = new long[0];
public static ArrayList<Song> getSongsForAlbum(Context context, long albumID) {
Cursor cursor = makeAlbumSongCursor(context, albumID);
ArrayList arrayList = 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);
/*This fixes bug where some track numbers displayed as 100 or 200*/
while (trackNumber >= 1000) {
trackNumber -= 1000; //When error occurs the track numbers have an extra 1000 or 2000 added, so decrease till normal.
}
long artistId = cursor.getInt(6);
long albumId = albumID;
arrayList.add(new Song(id, albumId, artistId, title, artist, album, duration, trackNumber));
}
while (cursor.moveToNext());
if (cursor != null)
cursor.close();
return arrayList;
}
public static Cursor makeAlbumSongCursor(Context context, long albumID) {
ContentResolver contentResolver = context.getContentResolver();
final String albumSongSortOrder = PreferencesUtility.getInstance(context).getAlbumSongSortOrder();
Uri uri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
String string = "is_music=1 AND title != '' AND album_id=" + albumID;
Cursor cursor = contentResolver.query(uri, new String[]{"_id", "title", "artist", "album", "duration", "track", "artist_id"}, string, null, albumSongSortOrder);
return cursor;
}
}