2 * Copyright (C) 2012 Andrew Neal Licensed under the Apache License, Version 2.0
3 * (the "License"); you may not use this file except in compliance with the
4 * License. You may obtain a copy of the License at
5 * http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law
6 * or agreed to in writing, software distributed under the License is
7 * distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
8 * KIND, either express or implied. See the License for the specific language
9 * governing permissions and limitations under the License.
12 package com.cyngn.eleven.loaders;
14 import android.content.Context;
15 import android.database.Cursor;
16 import android.provider.BaseColumns;
17 import android.provider.MediaStore;
18 import android.provider.MediaStore.Audio.AlbumColumns;
20 import com.cyngn.eleven.R;
21 import com.cyngn.eleven.model.Album;
22 import com.cyngn.eleven.utils.Lists;
23 import com.cyngn.eleven.utils.PreferenceUtils;
25 import java.util.ArrayList;
26 import java.util.List;
29 * Used to query {@link MediaStore.Audio.Albums.EXTERNAL_CONTENT_URI} and return
30 * the albums on a user's device.
32 * @author Andrew Neal (andrewdneal@gmail.com)
34 public class AlbumLoader extends WrappedAsyncTaskLoader<List<Album>> {
39 private final ArrayList<Album> mAlbumsList = Lists.newArrayList();
42 * The {@link Cursor} used to run the query.
44 private Cursor mCursor;
47 * Constructor of <code>AlbumLoader</code>
49 * @param context The {@link Context} to use
51 public AlbumLoader(final Context context) {
59 public List<Album> loadInBackground() {
61 mCursor = makeAlbumCursor(getContext());
63 if (mCursor != null && mCursor.moveToFirst()) {
66 final long id = mCursor.getLong(0);
68 // Copy the album name
69 final String albumName = mCursor.getString(1);
71 // Copy the artist name
72 final String artist = mCursor.getString(2);
74 // Copy the number of songs
75 final int songCount = mCursor.getInt(3);
77 // Copy the release year
78 final String year = mCursor.getString(4);
81 final Album album = new Album(id, albumName, artist, songCount, year);
84 mAlbumsList.add(album);
85 } while (mCursor.moveToNext());
88 if (mCursor != null) {
96 * Creates the {@link Cursor} used to run the query.
98 * @param context The {@link Context} to use.
99 * @return The {@link Cursor} used to run the album query.
101 public static final Cursor makeAlbumCursor(final Context context) {
102 return context.getContentResolver().query(MediaStore.Audio.Albums.EXTERNAL_CONTENT_URI,
111 AlbumColumns.NUMBER_OF_SONGS,
113 AlbumColumns.FIRST_YEAR
114 }, null, null, PreferenceUtils.getInstance(context).getAlbumSortOrder());