/*
* 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.Context;
import android.database.Cursor;
import android.provider.MediaStore;
import com.naman14.timber.models.Album;
import com.naman14.timber.utils.PreferencesUtility;
import java.util.ArrayList;
import java.util.List;
public class AlbumLoader {
public static Album getAlbum(Cursor cursor) {
Album album = new Album();
if (cursor != null) {
if (cursor.moveToFirst())
album = new Album(cursor.getLong(0), cursor.getString(1), cursor.getString(2), cursor.getLong(3), cursor.getInt(4), cursor.getInt(5));
}
if (cursor != null)
cursor.close();
return album;
}
public static List<Album> getAlbumsForCursor(Cursor cursor) {
ArrayList arrayList = new ArrayList();
if ((cursor != null) && (cursor.moveToFirst()))
do {
arrayList.add(new Album(cursor.getLong(0), cursor.getString(1), cursor.getString(2), cursor.getLong(3), cursor.getInt(4), cursor.getInt(5)));
}
while (cursor.moveToNext());
if (cursor != null)
cursor.close();
return arrayList;
}
public static List<Album> getAllAlbums(Context context) {
return getAlbumsForCursor(makeAlbumCursor(context, null, null));
}
public static Album getAlbum(Context context, long id) {
return getAlbum(makeAlbumCursor(context, "_id=?", new String[]{String.valueOf(id)}));
}
public static List<Album> getAlbums(Context context, String paramString, int limit) {
List<Album> result = getAlbumsForCursor(makeAlbumCursor(context, "album LIKE ?", new String[]{paramString + "%"}));
if (result.size() < limit) {
result.addAll(getAlbumsForCursor(makeAlbumCursor(context, "album LIKE ?", new String[]{"%_" + paramString + "%"})));
}
return result.size() < limit ? result : result.subList(0, limit);
}
public static Cursor makeAlbumCursor(Context context, String selection, String[] paramArrayOfString) {
final String albumSortOrder = PreferencesUtility.getInstance(context).getAlbumSortOrder();
Cursor cursor = context.getContentResolver().query(MediaStore.Audio.Albums.EXTERNAL_CONTENT_URI, new String[]{"_id", "album", "artist", "artist_id", "numsongs", "minyear"}, selection, paramArrayOfString, albumSortOrder);
return cursor;
}
}