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.
14 package com.cyanogenmod.eleven.loaders;
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;
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;
28 import java.util.ArrayList;
29 import java.util.List;
32 * Used to query {@link MediaStore.Audio.Playlists.EXTERNAL_CONTENT_URI} and
33 * return the playlists on a user's device.
35 * @author Andrew Neal (andrewdneal@gmail.com)
37 public class PlaylistLoader extends WrappedAsyncTaskLoader<List<Playlist>> {
42 private final ArrayList<Playlist> mPlaylistList = Lists.newArrayList();
45 * Constructor of <code>PlaylistLoader</code>
47 * @param context The {@link Context} to use
49 public PlaylistLoader(final Context context) {
57 public List<Playlist> loadInBackground() {
58 // Add the deafult playlits to the adapter
59 makeDefaultPlaylists();
62 Cursor cursor = makePlaylistCursor(getContext());
64 if (cursor != null && cursor.moveToFirst()) {
66 // Copy the playlist id
67 final long id = cursor.getLong(0);
69 // Copy the playlist name
70 final String name = cursor.getString(1);
72 final int songCount = MusicUtils.getSongCountForPlaylist(getContext(), id);
74 // Create a new playlist
75 final Playlist playlist = new Playlist(id, name, songCount);
78 mPlaylistList.add(playlist);
79 } while (cursor.moveToNext());
89 /* Adds the favorites and last added playlists */
90 private void makeDefaultPlaylists() {
91 final Resources resources = getContext().getResources();
94 final Playlist lastAdded = new Playlist(SmartPlaylistType.LastAdded.mId,
95 resources.getString(SmartPlaylistType.LastAdded.mTitleId), -1);
96 mPlaylistList.add(lastAdded);
99 final Playlist recentlyPlayed = new Playlist(SmartPlaylistType.RecentlyPlayed.mId,
100 resources.getString(SmartPlaylistType.RecentlyPlayed.mTitleId), -1);
101 mPlaylistList.add(recentlyPlayed);
104 final Playlist topTracks = new Playlist(SmartPlaylistType.TopTracks.mId,
105 resources.getString(SmartPlaylistType.TopTracks.mTitleId), -1);
106 mPlaylistList.add(topTracks);
110 * Creates the {@link Cursor} used to run the query.
112 * @param context The {@link Context} to use.
113 * @return The {@link Cursor} used to run the playlist query.
115 public static final Cursor makePlaylistCursor(final Context context) {
116 return context.getContentResolver().query(MediaStore.Audio.Playlists.EXTERNAL_CONTENT_URI,
121 PlaylistsColumns.NAME
122 }, null, null, MediaStore.Audio.Playlists.DEFAULT_SORT_ORDER);