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.MediaStore;
17 import android.provider.MediaStore.Audio.AudioColumns;
19 import com.andrew.apollo.model.Song;
20 import com.andrew.apollo.utils.Lists;
22 import java.util.ArrayList;
23 import java.util.List;
26 * Used to query {@link MediaStore.Audio.Playlists.EXTERNAL_CONTENT_URI} and
27 * return the songs for a particular playlist.
29 * @author Andrew Neal (andrewdneal@gmail.com)
31 public class PlaylistSongLoader extends WrappedAsyncTaskLoader<List<Song>> {
36 private final ArrayList<Song> mSongList = Lists.newArrayList();
39 * The {@link Cursor} used to run the query.
41 private Cursor mCursor;
44 * The Id of the playlist the songs belong to.
46 private final Long mPlaylistID;
49 * Constructor of <code>SongLoader</code>
51 * @param context The {@link Context} to use
52 * @param playlistID The Id of the playlist the songs belong to.
54 public PlaylistSongLoader(final Context context, final Long playlistId) {
56 mPlaylistID = playlistId;
63 public List<Song> loadInBackground() {
65 mCursor = makePlaylistSongCursor(getContext(), mPlaylistID);
67 if (mCursor != null && mCursor.moveToFirst()) {
70 final long id = mCursor.getLong(mCursor
71 .getColumnIndexOrThrow(MediaStore.Audio.Playlists.Members.AUDIO_ID));
74 final String songName = mCursor.getString(mCursor
75 .getColumnIndexOrThrow(AudioColumns.TITLE));
77 // Copy the artist name
78 final String artist = mCursor.getString(mCursor
79 .getColumnIndexOrThrow(AudioColumns.ARTIST));
81 // Copy the album name
82 final String album = mCursor.getString(mCursor
83 .getColumnIndexOrThrow(AudioColumns.ALBUM));
86 final long duration = mCursor.getLong(mCursor
87 .getColumnIndexOrThrow(AudioColumns.DURATION));
89 // Convert the duration into seconds
90 final int durationInSecs = (int) duration / 1000;
93 final Song song = new Song(id, songName, artist, album, durationInSecs);
97 } while (mCursor.moveToNext());
100 if (mCursor != null) {
108 * Creates the {@link Cursor} used to run the query.
110 * @param context The {@link Context} to use.
111 * @param playlistID The playlist the songs belong to.
112 * @return The {@link Cursor} used to run the song query.
114 public static final Cursor makePlaylistSongCursor(final Context context, final Long playlistID) {
115 final StringBuilder mSelection = new StringBuilder();
116 mSelection.append(AudioColumns.IS_MUSIC + "=1");
117 mSelection.append(" AND " + AudioColumns.TITLE + " != ''"); //$NON-NLS-2$
118 return context.getContentResolver().query(
119 MediaStore.Audio.Playlists.Members.getContentUri("external", playlistID),
122 MediaStore.Audio.Playlists.Members._ID,
124 MediaStore.Audio.Playlists.Members.AUDIO_ID,
132 AudioColumns.DURATION
133 }, mSelection.toString(), null,
134 MediaStore.Audio.Playlists.Members.DEFAULT_SORT_ORDER);