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.provider.BaseColumns;
19 import android.provider.MediaStore;
20 import android.provider.MediaStore.Audio.AudioColumns;
22 import com.cyanogenmod.eleven.model.Song;
23 import com.cyanogenmod.eleven.utils.Lists;
24 import com.cyanogenmod.eleven.utils.PreferenceUtils;
26 import java.util.ArrayList;
27 import java.util.List;
30 * Used to query {@link MediaStore.Audio.Media.EXTERNAL_CONTENT_URI} and return
31 * the songs for a particular artist.
33 * @author Andrew Neal (andrewdneal@gmail.com)
35 public class ArtistSongLoader extends WrappedAsyncTaskLoader<List<Song>> {
40 private final ArrayList<Song> mSongList = Lists.newArrayList();
43 * The {@link Cursor} used to run the query.
45 private Cursor mCursor;
48 * The Id of the artist the songs belong to.
50 private final Long mArtistID;
53 * Constructor of <code>ArtistSongLoader</code>
55 * @param context The {@link Context} to use.
56 * @param artistId The Id of the artist the songs belong to.
58 public ArtistSongLoader(final Context context, final Long artistId) {
67 public List<Song> loadInBackground() {
69 mCursor = makeArtistSongCursor(getContext(), mArtistID);
71 if (mCursor != null && mCursor.moveToFirst()) {
74 final long id = mCursor.getLong(0);
77 final String songName = mCursor.getString(1);
79 // Copy the artist name
80 final String artist = mCursor.getString(2);
83 final long albumId = mCursor.getLong(3);
85 // Copy the album name
86 final String album = mCursor.getString(4);
89 final long duration = mCursor.getLong(5);
91 // Convert the duration into seconds
92 final int durationInSecs = (int) duration / 1000;
95 final int year = mCursor.getInt(6);
98 final Song song = new Song(id, songName, artist, album, albumId, durationInSecs, year);
102 } while (mCursor.moveToNext());
105 if (mCursor != null) {
113 * @param context The {@link Context} to use.
114 * @param artistId The Id of the artist the songs belong to.
115 * @return The {@link Cursor} used to run the query.
117 public static final Cursor makeArtistSongCursor(final Context context, final Long artistId) {
118 // Match the songs up with the artist
119 final StringBuilder selection = new StringBuilder();
120 selection.append(AudioColumns.IS_MUSIC + "=1");
121 selection.append(" AND " + AudioColumns.TITLE + " != ''");
122 selection.append(" AND " + AudioColumns.ARTIST_ID + "=" + artistId);
123 return context.getContentResolver().query(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI,
132 AudioColumns.ALBUM_ID,
136 AudioColumns.DURATION,
139 }, selection.toString(), null,
140 PreferenceUtils.getInstance(context).getArtistSongSortOrder());