OSDN Git Service

Repackaged com.andrew.apollo to com.cyngn.eleven
[android-x86/packages-apps-Eleven.git] / src / com / cyngn / eleven / loaders / AlbumLoader.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 import android.provider.BaseColumns;
17 import android.provider.MediaStore;
18 import android.provider.MediaStore.Audio.AlbumColumns;
19
20 import com.cyngn.eleven.R;
21 import com.cyngn.eleven.model.Album;
22 import com.cyngn.eleven.utils.Lists;
23 import com.cyngn.eleven.utils.PreferenceUtils;
24
25 import java.util.ArrayList;
26 import java.util.List;
27
28 /**
29  * Used to query {@link MediaStore.Audio.Albums.EXTERNAL_CONTENT_URI} and return
30  * the albums on a user's device.
31  * 
32  * @author Andrew Neal (andrewdneal@gmail.com)
33  */
34 public class AlbumLoader extends WrappedAsyncTaskLoader<List<Album>> {
35
36     /**
37      * The result
38      */
39     private final ArrayList<Album> mAlbumsList = Lists.newArrayList();
40
41     /**
42      * The {@link Cursor} used to run the query.
43      */
44     private Cursor mCursor;
45
46     /**
47      * Constructor of <code>AlbumLoader</code>
48      * 
49      * @param context The {@link Context} to use
50      */
51     public AlbumLoader(final Context context) {
52         super(context);
53     }
54
55     /**
56      * {@inheritDoc}
57      */
58     @Override
59     public List<Album> loadInBackground() {
60         // Create the Cursor
61         mCursor = makeAlbumCursor(getContext());
62         // Gather the data
63         if (mCursor != null && mCursor.moveToFirst()) {
64             do {
65                 // Copy the album id
66                 final long id = mCursor.getLong(0);
67
68                 // Copy the album name
69                 final String albumName = mCursor.getString(1);
70
71                 // Copy the artist name
72                 final String artist = mCursor.getString(2);
73
74                 // Copy the number of songs
75                 final int songCount = mCursor.getInt(3);
76
77                 // Copy the release year
78                 final String year = mCursor.getString(4);
79
80                 // Create a new album
81                 final Album album = new Album(id, albumName, artist, songCount, year);
82
83                 // Add everything up
84                 mAlbumsList.add(album);
85             } while (mCursor.moveToNext());
86         }
87         // Close the cursor
88         if (mCursor != null) {
89             mCursor.close();
90             mCursor = null;
91         }
92         return mAlbumsList;
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 album query.
100      */
101     public static final Cursor makeAlbumCursor(final Context context) {
102         return context.getContentResolver().query(MediaStore.Audio.Albums.EXTERNAL_CONTENT_URI,
103                 new String[] {
104                         /* 0 */
105                         BaseColumns._ID,
106                         /* 1 */
107                         AlbumColumns.ALBUM,
108                         /* 2 */
109                         AlbumColumns.ARTIST,
110                         /* 3 */
111                         AlbumColumns.NUMBER_OF_SONGS,
112                         /* 4 */
113                         AlbumColumns.FIRST_YEAR
114                 }, null, null, PreferenceUtils.getInstance(context).getAlbumSortOrder());
115     }
116 }