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;
17 import com.andrew.apollo.model.Song;
18 import com.andrew.apollo.utils.Lists;
20 import java.util.ArrayList;
21 import java.util.List;
24 * Used to return the current playlist or queue.
26 * @author Andrew Neal (andrewdneal@gmail.com)
28 public class QueueLoader extends WrappedAsyncTaskLoader<List<Song>> {
33 private final ArrayList<Song> mSongList = Lists.newArrayList();
36 * The {@link Cursor} used to run the query.
38 private NowPlayingCursor mCursor;
41 * Constructor of <code>QueueLoader</code>
43 * @param context The {@link Context} to use
45 public QueueLoader(final Context context) {
53 public List<Song> loadInBackground() {
55 mCursor = new NowPlayingCursor(getContext());
57 if (mCursor != null && mCursor.moveToFirst()) {
60 final long id = mCursor.getLong(0);
63 final String songName = mCursor.getString(1);
65 // Copy the artist name
66 final String artist = mCursor.getString(2);
68 // Copy the album name
69 final String album = mCursor.getString(3);
72 final long duration = mCursor.getLong(4);
74 // Convert the duration into seconds
75 final int durationInSecs = (int) duration / 1000;
78 final Song song = new Song(id, songName, artist, album, durationInSecs);
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 song query.
98 public static final Cursor makeQueueCursor(final Context context) {
99 final Cursor cursor = new NowPlayingCursor(context);