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.database.Cursor;
18 import android.provider.BaseColumns;
19 import android.provider.MediaStore;
20 import android.provider.MediaStore.Audio.AudioColumns;
22 import com.cyanogenmod.eleven.model.Song;
23 import com.cyanogenmod.eleven.sectionadapter.SectionCreator;
24 import com.cyanogenmod.eleven.utils.Lists;
25 import com.cyanogenmod.eleven.utils.PreferenceUtils;
27 import java.util.ArrayList;
28 import java.util.List;
31 * Used to query {@link MediaStore.Audio.Media.EXTERNAL_CONTENT_URI} and return
32 * the Song the user added over the past four of weeks.
34 * @author Andrew Neal (andrewdneal@gmail.com)
36 public class LastAddedLoader extends SectionCreator.SimpleListLoader<Song> {
40 private final ArrayList<Song> mSongList = Lists.newArrayList();
43 * The {@link Cursor} used to run the query.
45 private Cursor mCursor;
48 * Constructor of <code>LastAddedHandler</code>
50 * @param context The {@link Context} to use.
52 public LastAddedLoader(final Context context) {
60 public List<Song> loadInBackground() {
62 mCursor = makeLastAddedCursor(getContext());
64 if (mCursor != null && mCursor.moveToFirst()) {
67 final long id = mCursor.getLong(0);
70 final String songName = mCursor.getString(1);
72 // Copy the artist name
73 final String artist = mCursor.getString(2);
76 final long albumId = mCursor.getLong(3);
78 // Copy the album name
79 final String album = mCursor.getString(4);
82 final long duration = mCursor.getLong(5);
84 // Convert the duration into seconds
85 final int durationInSecs = (int) duration / 1000;
88 final int year = mCursor.getInt(6);
91 final Song song = new Song(id, songName, artist, album, albumId, durationInSecs, year);
95 } while (mCursor.moveToNext());
98 if (mCursor != null) {
106 * @param context The {@link Context} to use.
107 * @return The {@link Cursor} used to run the song query.
109 public static final Cursor makeLastAddedCursor(final Context context) {
110 // timestamp of four weeks ago
111 long fourWeeksAgo = (System.currentTimeMillis() / 1000) - (4 * 3600 * 24 * 7);
112 // possible saved timestamp caused by user "clearing" the last added playlist
113 long cutoff = PreferenceUtils.getInstance(context).getLastAddedCutoff() / 1000;
114 // use the most recent of the two timestamps
115 if(cutoff < fourWeeksAgo) { cutoff = fourWeeksAgo; }
117 String selection = (AudioColumns.IS_MUSIC + "=1") +
118 " AND " + AudioColumns.TITLE + " != ''" +
119 " AND " + MediaStore.Audio.Media.DATE_ADDED + ">" +
122 return context.getContentResolver().query(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI,
131 AudioColumns.ALBUM_ID,
135 AudioColumns.DURATION,
138 }, selection, null, MediaStore.Audio.Media.DATE_ADDED + " DESC");