OSDN Git Service

Eleven: Create one cursor per background task
[android-x86/packages-apps-Eleven.git] / src / com / cyanogenmod / eleven / loaders / AlbumLoader.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.net.Uri;
19 import android.provider.BaseColumns;
20 import android.provider.MediaStore;
21 import android.provider.MediaStore.Audio.AlbumColumns;
22
23 import com.cyanogenmod.eleven.model.Album;
24 import com.cyanogenmod.eleven.provider.LocalizedStore;
25 import com.cyanogenmod.eleven.provider.LocalizedStore.SortParameter;
26 import com.cyanogenmod.eleven.sectionadapter.SectionCreator;
27 import com.cyanogenmod.eleven.utils.Lists;
28 import com.cyanogenmod.eleven.utils.MusicUtils;
29 import com.cyanogenmod.eleven.utils.PreferenceUtils;
30 import com.cyanogenmod.eleven.utils.SortOrder;
31
32 import java.util.ArrayList;
33 import java.util.List;
34
35 /**
36  * Used to query {@link MediaStore.Audio.Albums.EXTERNAL_CONTENT_URI} and return
37  * the albums on a user's device.
38  *
39  * @author Andrew Neal (andrewdneal@gmail.com)
40  */
41 public class AlbumLoader extends SectionCreator.SimpleListLoader<Album> {
42
43     /**
44      * The result
45      */
46     private ArrayList<Album> mAlbumsList = Lists.newArrayList();
47
48     /**
49      * Additional selection filter
50      */
51     protected Long mArtistId;
52
53     /**
54      * @param context The {@link Context} to use
55      */
56     public AlbumLoader(final Context context) {
57         this(context, null);
58     }
59
60     /**
61      * @param context The {@link Context} to use
62      * @param artistId The artistId to filter against or null if none
63      */
64     public AlbumLoader(final Context context, final Long artistId) {
65         super(context);
66
67         mArtistId = artistId;
68     }
69
70     /**
71      * {@inheritDoc}
72      */
73     @Override
74     public List<Album> loadInBackground() {
75         // Create the Cursor
76         Cursor cursor = makeAlbumCursor(getContext(), mArtistId);
77         // Gather the data
78         if (cursor != null && cursor.moveToFirst()) {
79             do {
80                 // Copy the album id
81                 final long id = cursor.getLong(0);
82
83                 // Copy the album name
84                 final String albumName = cursor.getString(1);
85
86                 // Copy the artist name
87                 final String artist = cursor.getString(2);
88
89                 // Copy the number of songs
90                 final int songCount = cursor.getInt(3);
91
92                 // Copy the release year
93                 final String year = cursor.getString(4);
94
95                 // as per designer's request, don't show unknown albums
96                 if (MediaStore.UNKNOWN_STRING.equals(albumName)) {
97                     continue;
98                 }
99
100                 // Create a new album
101                 final Album album = new Album(id, albumName, artist, songCount, year);
102
103                 if (cursor instanceof SortedCursor) {
104                     album.mBucketLabel = (String)((SortedCursor) cursor).getExtraData();
105                 }
106
107                 // Add everything up
108                 mAlbumsList.add(album);
109             } while (cursor.moveToNext());
110         }
111         // Close the cursor
112         if (cursor != null) {
113             cursor.close();
114             cursor = null;
115         }
116
117         return mAlbumsList;
118     }
119
120     /**
121      * For string-based sorts, return the localized store sort parameter, otherwise return null
122      * @param sortOrder the song ordering preference selected by the user
123      */
124     private static LocalizedStore.SortParameter getSortParameter(String sortOrder) {
125         if (sortOrder.equals(SortOrder.AlbumSortOrder.ALBUM_A_Z) ||
126                 sortOrder.equals(SortOrder.AlbumSortOrder.ALBUM_Z_A)) {
127             return LocalizedStore.SortParameter.Album;
128         } else if (sortOrder.equals(SortOrder.AlbumSortOrder.ALBUM_ARTIST)) {
129             return LocalizedStore.SortParameter.Artist;
130         }
131
132         return null;
133     }
134
135     /**
136      * Creates the {@link Cursor} used to run the query.
137      *
138      * @param context The {@link Context} to use.
139      * @param artistId The artistId we want to find albums for or null if we want all albums
140      * @return The {@link Cursor} used to run the album query.
141      */
142     public static final Cursor makeAlbumCursor(final Context context, final Long artistId) {
143         // requested album ordering
144         final String albumSortOrder = PreferenceUtils.getInstance(context).getAlbumSortOrder();
145         Uri uri = MediaStore.Audio.Albums.EXTERNAL_CONTENT_URI;
146         if (artistId != null) {
147             uri = MediaStore.Audio.Artists.Albums.getContentUri("external", artistId);
148         }
149
150         Cursor cursor = context.getContentResolver().query(uri,
151                 new String[] {
152                         /* 0 */
153                         BaseColumns._ID,
154                         /* 1 */
155                         AlbumColumns.ALBUM,
156                         /* 2 */
157                         AlbumColumns.ARTIST,
158                         /* 3 */
159                         AlbumColumns.NUMBER_OF_SONGS,
160                         /* 4 */
161                         AlbumColumns.FIRST_YEAR
162                 }, null, null, albumSortOrder);
163
164         // if our sort is a localized-based sort, grab localized data from the store
165         final SortParameter sortParameter = getSortParameter(albumSortOrder);
166         if (sortParameter != null && cursor != null) {
167             final boolean descending = MusicUtils.isSortOrderDesending(albumSortOrder);
168             return LocalizedStore.getInstance(context).getLocalizedSort(cursor, BaseColumns._ID,
169                     SortParameter.Album, sortParameter, descending, artistId == null);
170         }
171
172         return cursor;
173     }
174 }