OSDN Git Service

7139d0572cf843fe9ddd34601a98b371af635beb
[android-x86/packages-apps-Eleven.git] / src / com / cyngn / eleven / loaders / QueueLoader.java
1 /*
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.
10  */
11
12 package com.cyngn.eleven.loaders;
13
14 import android.content.Context;
15 import android.database.Cursor;
16
17 import com.cyngn.eleven.model.Song;
18 import com.cyngn.eleven.utils.Lists;
19
20 import java.util.ArrayList;
21 import java.util.List;
22
23 /**
24  * Used to return the current playlist or queue.
25  * 
26  * @author Andrew Neal (andrewdneal@gmail.com)
27  */
28 public class QueueLoader extends WrappedAsyncTaskLoader<List<Song>> {
29
30     /**
31      * The result
32      */
33     private final ArrayList<Song> mSongList = Lists.newArrayList();
34
35     /**
36      * The {@link Cursor} used to run the query.
37      */
38     private NowPlayingCursor mCursor;
39
40     /**
41      * Constructor of <code>QueueLoader</code>
42      * 
43      * @param context The {@link Context} to use
44      */
45     public QueueLoader(final Context context) {
46         super(context);
47     }
48
49     /**
50      * {@inheritDoc}
51      */
52     @Override
53     public List<Song> loadInBackground() {
54         // Create the Cursor
55         mCursor = new NowPlayingCursor(getContext());
56         // Gather the data
57         if (mCursor != null && mCursor.moveToFirst()) {
58             do {
59                 // Copy the song Id
60                 final long id = mCursor.getLong(0);
61
62                 // Copy the song name
63                 final String songName = mCursor.getString(1);
64
65                 // Copy the artist name
66                 final String artist = mCursor.getString(2);
67
68                 // Copy the album name
69                 final String album = mCursor.getString(3);
70
71                 // Copy the duration
72                 final long duration = mCursor.getLong(4);
73
74                 // Convert the duration into seconds
75                 final int durationInSecs = (int) duration / 1000;
76
77                 // Copy the year
78                 final int year = mCursor.getInt(5);
79
80                 // Create a new song
81                 final Song song = new Song(id, songName, artist, album, durationInSecs, year);
82
83                 // Add everything up
84                 mSongList.add(song);
85             } while (mCursor.moveToNext());
86         }
87         // Close the cursor
88         if (mCursor != null) {
89             mCursor.close();
90             mCursor = null;
91         }
92         return mSongList;
93     }
94
95     /**
96      * Creates the {@link Cursor} used to run the query.
97      * 
98      * @param context The {@link Context} to use.
99      * @return The {@link Cursor} used to run the song query.
100      */
101     public static final Cursor makeQueueCursor(final Context context) {
102         final Cursor cursor = new NowPlayingCursor(context);
103         return cursor;
104     }
105 }