OSDN Git Service

ea3e9f09c6f7a1b63d0046c411b8ccc3c2edfbe3
[android-x86/packages-apps-Eleven.git] / src / com / andrew / apollo / 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.andrew.apollo.loaders;
13
14 import android.content.Context;
15 import android.database.Cursor;
16
17 import com.andrew.apollo.model.Song;
18 import com.andrew.apollo.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                 // Create a new song
78                 final Song song = new Song(id, songName, artist, album, durationInSecs);
79
80                 // Add everything up
81                 mSongList.add(song);
82             } while (mCursor.moveToNext());
83         }
84         // Close the cursor
85         if (mCursor != null) {
86             mCursor.close();
87             mCursor = null;
88         }
89         return mSongList;
90     }
91
92     /**
93      * Creates the {@link Cursor} used to run the query.
94      * 
95      * @param context The {@link Context} to use.
96      * @return The {@link Cursor} used to run the song query.
97      */
98     public static final Cursor makeQueueCursor(final Context context) {
99         final Cursor cursor = new NowPlayingCursor(context);
100         return cursor;
101     }
102 }