OSDN Git Service

816233b4ad63cca647e143222070ea42b0383e71
[android-x86/packages-apps-Eleven.git] / src / com / andrew / apollo / loaders / PlaylistSongLoader.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 import android.provider.MediaStore;
17 import android.provider.MediaStore.Audio.AudioColumns;
18
19 import com.andrew.apollo.model.Song;
20 import com.andrew.apollo.utils.Lists;
21
22 import java.util.ArrayList;
23 import java.util.List;
24
25 /**
26  * Used to query {@link MediaStore.Audio.Playlists.EXTERNAL_CONTENT_URI} and
27  * return the songs for a particular playlist.
28  * 
29  * @author Andrew Neal (andrewdneal@gmail.com)
30  */
31 public class PlaylistSongLoader extends WrappedAsyncTaskLoader<List<Song>> {
32
33     /**
34      * The result
35      */
36     private final ArrayList<Song> mSongList = Lists.newArrayList();
37
38     /**
39      * The {@link Cursor} used to run the query.
40      */
41     private Cursor mCursor;
42
43     /**
44      * The Id of the playlist the songs belong to.
45      */
46     private final Long mPlaylistID;
47
48     /**
49      * Constructor of <code>SongLoader</code>
50      * 
51      * @param context The {@link Context} to use
52      * @param playlistID The Id of the playlist the songs belong to.
53      */
54     public PlaylistSongLoader(final Context context, final Long playlistId) {
55         super(context);
56         mPlaylistID = playlistId;
57     }
58
59     /**
60      * {@inheritDoc}
61      */
62     @Override
63     public List<Song> loadInBackground() {
64         // Create the Cursor
65         mCursor = makePlaylistSongCursor(getContext(), mPlaylistID);
66         // Gather the data
67         if (mCursor != null && mCursor.moveToFirst()) {
68             do {
69                 // Copy the song Id
70                 final long id = mCursor.getLong(mCursor
71                         .getColumnIndexOrThrow(MediaStore.Audio.Playlists.Members.AUDIO_ID));
72
73                 // Copy the song name
74                 final String songName = mCursor.getString(mCursor
75                         .getColumnIndexOrThrow(AudioColumns.TITLE));
76
77                 // Copy the artist name
78                 final String artist = mCursor.getString(mCursor
79                         .getColumnIndexOrThrow(AudioColumns.ARTIST));
80
81                 // Copy the album name
82                 final String album = mCursor.getString(mCursor
83                         .getColumnIndexOrThrow(AudioColumns.ALBUM));
84
85                 // Copy the duration
86                 final long duration = mCursor.getLong(mCursor
87                         .getColumnIndexOrThrow(AudioColumns.DURATION));
88
89                 // Convert the duration into seconds
90                 final int durationInSecs = (int) duration / 1000;
91
92                 // Create a new song
93                 final Song song = new Song(id, songName, artist, album, durationInSecs);
94
95                 // Add everything up
96                 mSongList.add(song);
97             } while (mCursor.moveToNext());
98         }
99         // Close the cursor
100         if (mCursor != null) {
101             mCursor.close();
102             mCursor = null;
103         }
104         return mSongList;
105     }
106
107     /**
108      * Creates the {@link Cursor} used to run the query.
109      * 
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.
113      */
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),
120                 new String[] {
121                         /* 0 */
122                         MediaStore.Audio.Playlists.Members._ID,
123                         /* 1 */
124                         MediaStore.Audio.Playlists.Members.AUDIO_ID,
125                         /* 2 */
126                         AudioColumns.TITLE,
127                         /* 3 */
128                         AudioColumns.ARTIST,
129                         /* 4 */
130                         AudioColumns.ALBUM,
131                         /* 5 */
132                         AudioColumns.DURATION
133                 }, mSelection.toString(), null,
134                 MediaStore.Audio.Playlists.Members.DEFAULT_SORT_ORDER);
135     }
136 }