OSDN Git Service

Eleven: Cleanup all the whitespace
[android-x86/packages-apps-Eleven.git] / src / com / cyanogenmod / eleven / loaders / AlbumSongLoader.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 Song for a particular album.
32  *
33  * @author Andrew Neal (andrewdneal@gmail.com)
34  */
35 public class AlbumSongLoader 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 album the songs belong to.
49      */
50     private final Long mAlbumID;
51
52     /**
53      * Constructor of <code>AlbumSongHandler</code>
54      *
55      * @param context The {@link Context} to use.
56      * @param albumId The Id of the album the songs belong to.
57      */
58     public AlbumSongLoader(final Context context, final Long albumId) {
59         super(context);
60         mAlbumID = albumId;
61     }
62
63     /**
64      * {@inheritDoc}
65      */
66     @Override
67     public List<Song> loadInBackground() {
68         // Create the Cursor
69         mCursor = makeAlbumSongCursor(getContext(), mAlbumID);
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 name
83                 final String album = mCursor.getString(3);
84
85                 // Copy the duration
86                 final long duration = mCursor.getLong(4);
87
88                 // Make the duration label
89                 final int seconds = (int) (duration / 1000);
90
91                 // Grab the Song Year
92                 final int year = mCursor.getInt(5);
93
94                 // Create a new song
95                 final Song song = new Song(id, songName, artist, album, mAlbumID, seconds, year);
96
97                 // Add everything up
98                 mSongList.add(song);
99             } while (mCursor.moveToNext());
100         }
101         // Close the cursor
102         if (mCursor != null) {
103             mCursor.close();
104             mCursor = null;
105         }
106         return mSongList;
107     }
108
109     /**
110      * @param context The {@link Context} to use.
111      * @param albumId The Id of the album the songs belong to.
112      * @return The {@link Cursor} used to run the query.
113      */
114     public static final Cursor makeAlbumSongCursor(final Context context, final Long albumId) {
115         // Match the songs up with the artist
116         String selection = (AudioColumns.IS_MUSIC + "=1") +
117                 " AND " + AudioColumns.TITLE + " != ''" +
118                 " AND " + AudioColumns.ALBUM_ID + "=" + albumId;
119         return context.getContentResolver().query(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI,
120                 new String[] {
121                         /* 0 */
122                         BaseColumns._ID,
123                         /* 1 */
124                         AudioColumns.TITLE,
125                         /* 2 */
126                         AudioColumns.ARTIST,
127                         /* 3 */
128                         AudioColumns.ALBUM,
129                         /* 4 */
130                         AudioColumns.DURATION,
131                         /* 5 */
132                         AudioColumns.YEAR,
133                 }, selection, null,
134                 PreferenceUtils.getInstance(context).getAlbumSongSortOrder());
135     }
136
137 }