OSDN Git Service

Eleven: Cleanup all the whitespace
[android-x86/packages-apps-Eleven.git] / src / com / cyanogenmod / eleven / loaders / PlaylistLoader.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.content.res.Resources;
18 import android.database.Cursor;
19 import android.provider.BaseColumns;
20 import android.provider.MediaStore;
21 import android.provider.MediaStore.Audio.PlaylistsColumns;
22
23 import com.cyanogenmod.eleven.Config.SmartPlaylistType;
24 import com.cyanogenmod.eleven.model.Playlist;
25 import com.cyanogenmod.eleven.utils.Lists;
26 import com.cyanogenmod.eleven.utils.MusicUtils;
27
28 import java.util.ArrayList;
29 import java.util.List;
30
31 /**
32  * Used to query {@link MediaStore.Audio.Playlists.EXTERNAL_CONTENT_URI} and
33  * return the playlists on a user's device.
34  *
35  * @author Andrew Neal (andrewdneal@gmail.com)
36  */
37 public class PlaylistLoader extends WrappedAsyncTaskLoader<List<Playlist>> {
38
39     /**
40      * The result
41      */
42     private final ArrayList<Playlist> mPlaylistList = Lists.newArrayList();
43
44     /**
45      * The {@link Cursor} used to run the query.
46      */
47     private Cursor mCursor;
48
49     /**
50      * Constructor of <code>PlaylistLoader</code>
51      *
52      * @param context The {@link Context} to use
53      */
54     public PlaylistLoader(final Context context) {
55         super(context);
56     }
57
58     /**
59      * {@inheritDoc}
60      */
61     @Override
62     public List<Playlist> loadInBackground() {
63         // Add the deafult playlits to the adapter
64         makeDefaultPlaylists();
65
66         // Create the Cursor
67         mCursor = makePlaylistCursor(getContext());
68         // Gather the data
69         if (mCursor != null && mCursor.moveToFirst()) {
70             do {
71                 // Copy the playlist id
72                 final long id = mCursor.getLong(0);
73
74                 // Copy the playlist name
75                 final String name = mCursor.getString(1);
76
77                 final int songCount = MusicUtils.getSongCountForPlaylist(getContext(), id);
78
79                 // Create a new playlist
80                 final Playlist playlist = new Playlist(id, name, songCount);
81
82                 // Add everything up
83                 mPlaylistList.add(playlist);
84             } while (mCursor.moveToNext());
85         }
86         // Close the cursor
87         if (mCursor != null) {
88             mCursor.close();
89             mCursor = null;
90         }
91         return mPlaylistList;
92     }
93
94     /* Adds the favorites and last added playlists */
95     private void makeDefaultPlaylists() {
96         final Resources resources = getContext().getResources();
97
98         /* Last added list */
99         final Playlist lastAdded = new Playlist(SmartPlaylistType.LastAdded.mId,
100                 resources.getString(SmartPlaylistType.LastAdded.mTitleId), -1);
101         mPlaylistList.add(lastAdded);
102
103         /* Recently Played */
104         final Playlist recentlyPlayed = new Playlist(SmartPlaylistType.RecentlyPlayed.mId,
105                 resources.getString(SmartPlaylistType.RecentlyPlayed.mTitleId), -1);
106         mPlaylistList.add(recentlyPlayed);
107
108         /* Top Tracks */
109         final Playlist topTracks = new Playlist(SmartPlaylistType.TopTracks.mId,
110                 resources.getString(SmartPlaylistType.TopTracks.mTitleId), -1);
111         mPlaylistList.add(topTracks);
112     }
113
114     /**
115      * Creates the {@link Cursor} used to run the query.
116      *
117      * @param context The {@link Context} to use.
118      * @return The {@link Cursor} used to run the playlist query.
119      */
120     public static final Cursor makePlaylistCursor(final Context context) {
121         return context.getContentResolver().query(MediaStore.Audio.Playlists.EXTERNAL_CONTENT_URI,
122                 new String[] {
123                         /* 0 */
124                         BaseColumns._ID,
125                         /* 1 */
126                         PlaylistsColumns.NAME
127                 }, null, null, MediaStore.Audio.Playlists.DEFAULT_SORT_ORDER);
128     }
129 }