2 * Copyright (C) 2012 Andrew Neal
3 * Copyright (C) 2014 The CyanogenMod Project
4 * Licensed under the Apache License, Version 2.0
5 * (the "License"); you may not use this file except in compliance with the
6 * License. You may obtain a copy of the License at
7 * http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law
8 * or agreed to in writing, software distributed under the License is
9 * distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
10 * KIND, either express or implied. See the License for the specific language
11 * governing permissions and limitations under the License.
14 package com.cyanogenmod.eleven.loaders;
16 import android.content.Context;
17 import android.database.Cursor;
18 import android.net.Uri;
19 import android.provider.BaseColumns;
20 import android.provider.MediaStore;
21 import android.provider.MediaStore.Audio.AlbumColumns;
23 import com.cyanogenmod.eleven.model.Album;
24 import com.cyanogenmod.eleven.provider.LocalizedStore;
25 import com.cyanogenmod.eleven.provider.LocalizedStore.SortParameter;
26 import com.cyanogenmod.eleven.sectionadapter.SectionCreator;
27 import com.cyanogenmod.eleven.utils.Lists;
28 import com.cyanogenmod.eleven.utils.MusicUtils;
29 import com.cyanogenmod.eleven.utils.PreferenceUtils;
30 import com.cyanogenmod.eleven.utils.SortOrder;
32 import java.util.ArrayList;
33 import java.util.List;
36 * Used to query {@link MediaStore.Audio.Albums.EXTERNAL_CONTENT_URI} and return
37 * the albums on a user's device.
39 * @author Andrew Neal (andrewdneal@gmail.com)
41 public class AlbumLoader extends SectionCreator.SimpleListLoader<Album> {
46 private ArrayList<Album> mAlbumsList = Lists.newArrayList();
49 * Additional selection filter
51 protected Long mArtistId;
54 * @param context The {@link Context} to use
56 public AlbumLoader(final Context context) {
61 * @param context The {@link Context} to use
62 * @param artistId The artistId to filter against or null if none
64 public AlbumLoader(final Context context, final Long artistId) {
74 public List<Album> loadInBackground() {
76 Cursor cursor = makeAlbumCursor(getContext(), mArtistId);
78 if (cursor != null && cursor.moveToFirst()) {
81 final long id = cursor.getLong(0);
83 // Copy the album name
84 final String albumName = cursor.getString(1);
86 // Copy the artist name
87 final String artist = cursor.getString(2);
89 // Copy the number of songs
90 final int songCount = cursor.getInt(3);
92 // Copy the release year
93 final String year = cursor.getString(4);
95 // as per designer's request, don't show unknown albums
96 if (MediaStore.UNKNOWN_STRING.equals(albumName)) {
100 // Create a new album
101 final Album album = new Album(id, albumName, artist, songCount, year);
103 if (cursor instanceof SortedCursor) {
104 album.mBucketLabel = (String)((SortedCursor) cursor).getExtraData();
108 mAlbumsList.add(album);
109 } while (cursor.moveToNext());
112 if (cursor != null) {
121 * For string-based sorts, return the localized store sort parameter, otherwise return null
122 * @param sortOrder the song ordering preference selected by the user
124 private static LocalizedStore.SortParameter getSortParameter(String sortOrder) {
125 if (sortOrder.equals(SortOrder.AlbumSortOrder.ALBUM_A_Z) ||
126 sortOrder.equals(SortOrder.AlbumSortOrder.ALBUM_Z_A)) {
127 return LocalizedStore.SortParameter.Album;
128 } else if (sortOrder.equals(SortOrder.AlbumSortOrder.ALBUM_ARTIST)) {
129 return LocalizedStore.SortParameter.Artist;
136 * Creates the {@link Cursor} used to run the query.
138 * @param context The {@link Context} to use.
139 * @param artistId The artistId we want to find albums for or null if we want all albums
140 * @return The {@link Cursor} used to run the album query.
142 public static final Cursor makeAlbumCursor(final Context context, final Long artistId) {
143 // requested album ordering
144 final String albumSortOrder = PreferenceUtils.getInstance(context).getAlbumSortOrder();
145 Uri uri = MediaStore.Audio.Albums.EXTERNAL_CONTENT_URI;
146 if (artistId != null) {
147 uri = MediaStore.Audio.Artists.Albums.getContentUri("external", artistId);
150 Cursor cursor = context.getContentResolver().query(uri,
159 AlbumColumns.NUMBER_OF_SONGS,
161 AlbumColumns.FIRST_YEAR
162 }, null, null, albumSortOrder);
164 // if our sort is a localized-based sort, grab localized data from the store
165 final SortParameter sortParameter = getSortParameter(albumSortOrder);
166 if (sortParameter != null && cursor != null) {
167 final boolean descending = MusicUtils.isSortOrderDesending(albumSortOrder);
168 return LocalizedStore.getInstance(context).getLocalizedSort(cursor, BaseColumns._ID,
169 SortParameter.Album, sortParameter, descending, artistId == null);