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.andrew.apollo.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.ArtistColumns;
20 import com.andrew.apollo.R;
21 import com.andrew.apollo.model.Artist;
22 import com.andrew.apollo.utils.Lists;
23 import com.andrew.apollo.utils.PreferenceUtils;
25 import java.util.ArrayList;
26 import java.util.List;
29 * Used to query {@link MediaStore.Audio.Artists.EXTERNAL_CONTENT_URI} and
30 * return the artists on a user's device.
32 * @author Andrew Neal (andrewdneal@gmail.com)
34 public class ArtistLoader extends WrappedAsyncTaskLoader<List<Artist>> {
39 private final ArrayList<Artist> mArtistsList = Lists.newArrayList();
42 * The {@link Cursor} used to run the query.
44 private Cursor mCursor;
47 * Constructor of <code>ArtistLoader</code>
49 * @param context The {@link Context} to use
51 public ArtistLoader(final Context context) {
59 public List<Artist> loadInBackground() {
61 mCursor = makeArtistCursor(getContext());
63 if (mCursor != null && mCursor.moveToFirst()) {
66 final long id = mCursor.getLong(0);
68 // Copy the artist name
69 final String artistName = mCursor.getString(1);
71 // Copy the number of albums
72 final int albumCount = mCursor.getInt(2);
74 // Copy the number of songs
75 final int songCount = mCursor.getInt(3);
77 // Create a new artist
78 final Artist artist = new Artist(id, artistName, songCount, albumCount);
81 mArtistsList.add(artist);
82 } while (mCursor.moveToNext());
85 if (mCursor != null) {
93 * Creates the {@link Cursor} used to run the query.
95 * @param context The {@link Context} to use.
96 * @return The {@link Cursor} used to run the artist query.
98 public static final Cursor makeArtistCursor(final Context context) {
99 return context.getContentResolver().query(MediaStore.Audio.Artists.EXTERNAL_CONTENT_URI,
104 ArtistColumns.ARTIST,
106 ArtistColumns.NUMBER_OF_ALBUMS,
108 ArtistColumns.NUMBER_OF_TRACKS
109 }, null, null, PreferenceUtils.getInstance(context).getArtistSortOrder());