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 Song for a particular album.
33 * @author Andrew Neal (andrewdneal@gmail.com)
35 public class AlbumSongLoader 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 album the songs belong to.
50 private final Long mAlbumID;
53 * Constructor of <code>AlbumSongHandler</code>
55 * @param context The {@link Context} to use.
56 * @param albumId The Id of the album the songs belong to.
58 public AlbumSongLoader(final Context context, final Long albumId) {
67 public List<Song> loadInBackground() {
69 mCursor = makeAlbumSongCursor(getContext(), mAlbumID);
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);
82 // Copy the album name
83 final String album = mCursor.getString(3);
86 final long duration = mCursor.getLong(4);
88 // Make the duration label
89 final int seconds = (int) (duration / 1000);
92 final int year = mCursor.getInt(5);
95 final Song song = new Song(id, songName, artist, album, mAlbumID, seconds, year);
99 } while (mCursor.moveToNext());
102 if (mCursor != null) {
110 * @param context The {@link Context} to use.
111 * @param albumId The Id of the album the songs belong to.
112 * @return The {@link Cursor} used to run the query.
114 public static final Cursor makeAlbumSongCursor(final Context context, final Long albumId) {
115 // Match the songs up with the artist
116 String selection = (AudioColumns.IS_MUSIC + "=1") +
117 " AND " + AudioColumns.TITLE + " != ''" +
118 " AND " + AudioColumns.ALBUM_ID + "=" + albumId;
119 return context.getContentResolver().query(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI,
130 AudioColumns.DURATION,
134 PreferenceUtils.getInstance(context).getAlbumSongSortOrder());