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 * The {@link Cursor} used to run the query.
47 private Cursor mCursor;
50 * Constructor of <code>PlaylistLoader</code>
52 * @param context The {@link Context} to use
54 public PlaylistLoader(final Context context) {
62 public List<Playlist> loadInBackground() {
63 // Add the deafult playlits to the adapter
64 makeDefaultPlaylists();
67 mCursor = makePlaylistCursor(getContext());
69 if (mCursor != null && mCursor.moveToFirst()) {
71 // Copy the playlist id
72 final long id = mCursor.getLong(0);
74 // Copy the playlist name
75 final String name = mCursor.getString(1);
77 final int songCount = MusicUtils.getSongCountForPlaylist(getContext(), id);
79 // Create a new playlist
80 final Playlist playlist = new Playlist(id, name, songCount);
83 mPlaylistList.add(playlist);
84 } while (mCursor.moveToNext());
87 if (mCursor != null) {
94 /* Adds the favorites and last added playlists */
95 private void makeDefaultPlaylists() {
96 final Resources resources = getContext().getResources();
99 final Playlist lastAdded = new Playlist(SmartPlaylistType.LastAdded.mId,
100 resources.getString(SmartPlaylistType.LastAdded.mTitleId), -1);
101 mPlaylistList.add(lastAdded);
103 /* Recently Played */
104 final Playlist recentlyPlayed = new Playlist(SmartPlaylistType.RecentlyPlayed.mId,
105 resources.getString(SmartPlaylistType.RecentlyPlayed.mTitleId), -1);
106 mPlaylistList.add(recentlyPlayed);
109 final Playlist topTracks = new Playlist(SmartPlaylistType.TopTracks.mId,
110 resources.getString(SmartPlaylistType.TopTracks.mTitleId), -1);
111 mPlaylistList.add(topTracks);
115 * Creates the {@link Cursor} used to run the query.
117 * @param context The {@link Context} to use.
118 * @return The {@link Cursor} used to run the playlist query.
120 public static final Cursor makePlaylistCursor(final Context context) {
121 return context.getContentResolver().query(MediaStore.Audio.Playlists.EXTERNAL_CONTENT_URI,
126 PlaylistsColumns.NAME
127 }, null, null, MediaStore.Audio.Playlists.DEFAULT_SORT_ORDER);