OSDN Git Service

68a1b1265e804b957dd592acebd15886008f92c7
[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      * Constructor of <code>PlaylistLoader</code>
46      *
47      * @param context The {@link Context} to use
48      */
49     public PlaylistLoader(final Context context) {
50         super(context);
51     }
52
53     /**
54      * {@inheritDoc}
55      */
56     @Override
57     public List<Playlist> loadInBackground() {
58         // Add the deafult playlits to the adapter
59         makeDefaultPlaylists();
60
61         // Create the Cursor
62         Cursor cursor = makePlaylistCursor(getContext());
63         // Gather the data
64         if (cursor != null && cursor.moveToFirst()) {
65             do {
66                 // Copy the playlist id
67                 final long id = cursor.getLong(0);
68
69                 // Copy the playlist name
70                 final String name = cursor.getString(1);
71
72                 final int songCount = MusicUtils.getSongCountForPlaylist(getContext(), id);
73
74                 // Create a new playlist
75                 final Playlist playlist = new Playlist(id, name, songCount);
76
77                 // Add everything up
78                 mPlaylistList.add(playlist);
79             } while (cursor.moveToNext());
80         }
81         // Close the cursor
82         if (cursor != null) {
83             cursor.close();
84             cursor = null;
85         }
86         return mPlaylistList;
87     }
88
89     /* Adds the favorites and last added playlists */
90     private void makeDefaultPlaylists() {
91         final Resources resources = getContext().getResources();
92
93         /* Last added list */
94         final Playlist lastAdded = new Playlist(SmartPlaylistType.LastAdded.mId,
95                 resources.getString(SmartPlaylistType.LastAdded.mTitleId), -1);
96         mPlaylistList.add(lastAdded);
97
98         /* Recently Played */
99         final Playlist recentlyPlayed = new Playlist(SmartPlaylistType.RecentlyPlayed.mId,
100                 resources.getString(SmartPlaylistType.RecentlyPlayed.mTitleId), -1);
101         mPlaylistList.add(recentlyPlayed);
102
103         /* Top Tracks */
104         final Playlist topTracks = new Playlist(SmartPlaylistType.TopTracks.mId,
105                 resources.getString(SmartPlaylistType.TopTracks.mTitleId), -1);
106         mPlaylistList.add(topTracks);
107     }
108
109     /**
110      * Creates the {@link Cursor} used to run the query.
111      *
112      * @param context The {@link Context} to use.
113      * @return The {@link Cursor} used to run the playlist query.
114      */
115     public static final Cursor makePlaylistCursor(final Context context) {
116         return context.getContentResolver().query(MediaStore.Audio.Playlists.EXTERNAL_CONTENT_URI,
117                 new String[] {
118                         /* 0 */
119                         BaseColumns._ID,
120                         /* 1 */
121                         PlaylistsColumns.NAME
122                 }, null, null, MediaStore.Audio.Playlists.DEFAULT_SORT_ORDER);
123     }
124 }