OSDN Git Service

Update Eleven headers and namespace for open source
[android-x86/packages-apps-Eleven.git] / src / com / cyanogenmod / eleven / loaders / ArtistSongLoader.java
1 /*
2  * Copyright (C) 2012 Andrew Neal
3  * Copyright (C) 2014 The CyanogenMod Project
4  * Licensed under the Apache License, Version 2.0
5  * (the "License"); you may not use this file except in compliance with the
6  * License. You may obtain a copy of the License at
7  * http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law
8  * or agreed to in writing, software distributed under the License is
9  * distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
10  * KIND, either express or implied. See the License for the specific language
11  * governing permissions and limitations under the License.
12  */
13
14 package com.cyanogenmod.eleven.loaders;
15
16 import android.content.Context;
17 import android.database.Cursor;
18 import android.provider.BaseColumns;
19 import android.provider.MediaStore;
20 import android.provider.MediaStore.Audio.AudioColumns;
21
22 import com.cyanogenmod.eleven.model.Song;
23 import com.cyanogenmod.eleven.utils.Lists;
24 import com.cyanogenmod.eleven.utils.PreferenceUtils;
25
26 import java.util.ArrayList;
27 import java.util.List;
28
29 /**
30  * Used to query {@link MediaStore.Audio.Media.EXTERNAL_CONTENT_URI} and return
31  * the songs for a particular artist.
32  * 
33  * @author Andrew Neal (andrewdneal@gmail.com)
34  */
35 public class ArtistSongLoader extends WrappedAsyncTaskLoader<List<Song>> {
36
37     /**
38      * The result
39      */
40     private final ArrayList<Song> mSongList = Lists.newArrayList();
41
42     /**
43      * The {@link Cursor} used to run the query.
44      */
45     private Cursor mCursor;
46
47     /**
48      * The Id of the artist the songs belong to.
49      */
50     private final Long mArtistID;
51
52     /**
53      * Constructor of <code>ArtistSongLoader</code>
54      * 
55      * @param context The {@link Context} to use.
56      * @param artistId The Id of the artist the songs belong to.
57      */
58     public ArtistSongLoader(final Context context, final Long artistId) {
59         super(context);
60         mArtistID = artistId;
61     }
62
63     /**
64      * {@inheritDoc}
65      */
66     @Override
67     public List<Song> loadInBackground() {
68         // Create the Cursor
69         mCursor = makeArtistSongCursor(getContext(), mArtistID);
70         // Gather the data
71         if (mCursor != null && mCursor.moveToFirst()) {
72             do {
73                 // Copy the song Id
74                 final long id = mCursor.getLong(0);
75
76                 // Copy the song name
77                 final String songName = mCursor.getString(1);
78
79                 // Copy the artist name
80                 final String artist = mCursor.getString(2);
81
82                 // Copy the album id
83                 final long albumId = mCursor.getLong(3);
84
85                 // Copy the album name
86                 final String album = mCursor.getString(4);
87
88                 // Copy the duration
89                 final long duration = mCursor.getLong(5);
90
91                 // Convert the duration into seconds
92                 final int durationInSecs = (int) duration / 1000;
93
94                 // Grab the Song Year
95                 final int year = mCursor.getInt(6);
96
97                 // Create a new song
98                 final Song song = new Song(id, songName, artist, album, albumId, durationInSecs, year);
99
100                 // Add everything up
101                 mSongList.add(song);
102             } while (mCursor.moveToNext());
103         }
104         // Close the cursor
105         if (mCursor != null) {
106             mCursor.close();
107             mCursor = null;
108         }
109         return mSongList;
110     }
111
112     /**
113      * @param context The {@link Context} to use.
114      * @param artistId The Id of the artist the songs belong to.
115      * @return The {@link Cursor} used to run the query.
116      */
117     public static final Cursor makeArtistSongCursor(final Context context, final Long artistId) {
118         // Match the songs up with the artist
119         final StringBuilder selection = new StringBuilder();
120         selection.append(AudioColumns.IS_MUSIC + "=1");
121         selection.append(" AND " + AudioColumns.TITLE + " != ''");
122         selection.append(" AND " + AudioColumns.ARTIST_ID + "=" + artistId);
123         return context.getContentResolver().query(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI,
124                 new String[] {
125                         /* 0 */
126                         BaseColumns._ID,
127                         /* 1 */
128                         AudioColumns.TITLE,
129                         /* 2 */
130                         AudioColumns.ARTIST,
131                         /* 3 */
132                         AudioColumns.ALBUM_ID,
133                         /* 4 */
134                         AudioColumns.ALBUM,
135                         /* 5 */
136                         AudioColumns.DURATION,
137                         /* 6 */
138                         AudioColumns.YEAR,
139                 }, selection.toString(), null,
140                 PreferenceUtils.getInstance(context).getArtistSongSortOrder());
141     }
142
143 }