OSDN Git Service

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