OSDN Git Service

967c0a951ec7c0f6f61312667780e1b3a6e85756
[android-x86/packages-apps-Eleven.git] / src / com / cyanogenmod / eleven / loaders / LastAddedLoader.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.database.Cursor;
18 import android.provider.BaseColumns;
19 import android.provider.MediaStore;
20 import android.provider.MediaStore.Audio.AudioColumns;
21
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;
26
27 import java.util.ArrayList;
28 import java.util.List;
29
30 /**
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.
33  *
34  * @author Andrew Neal (andrewdneal@gmail.com)
35  */
36 public class LastAddedLoader extends SectionCreator.SimpleListLoader<Song> {
37     /**
38      * The result
39      */
40     private final ArrayList<Song> mSongList = Lists.newArrayList();
41
42     /**
43      * The {@link Cursor} used to run the query.
44      */
45     private Cursor mCursor;
46
47     /**
48      * Constructor of <code>LastAddedHandler</code>
49      *
50      * @param context The {@link Context} to use.
51      */
52     public LastAddedLoader(final Context context) {
53         super(context);
54     }
55
56     /**
57      * {@inheritDoc}
58      */
59     @Override
60     public List<Song> loadInBackground() {
61         // Create the xCursor
62         mCursor = makeLastAddedCursor(getContext());
63         // Gather the data
64         if (mCursor != null && mCursor.moveToFirst()) {
65             do {
66                 // Copy the song Id
67                 final long id = mCursor.getLong(0);
68
69                 // Copy the song name
70                 final String songName = mCursor.getString(1);
71
72                 // Copy the artist name
73                 final String artist = mCursor.getString(2);
74
75                 // Copy the album id
76                 final long albumId = mCursor.getLong(3);
77
78                 // Copy the album name
79                 final String album = mCursor.getString(4);
80
81                 // Copy the duration
82                 final long duration = mCursor.getLong(5);
83
84                 // Convert the duration into seconds
85                 final int durationInSecs = (int) duration / 1000;
86
87                 // Grab the Song Year
88                 final int year = mCursor.getInt(6);
89
90                 // Create a new song
91                 final Song song = new Song(id, songName, artist, album, albumId, durationInSecs, year);
92
93                 // Add everything up
94                 mSongList.add(song);
95             } while (mCursor.moveToNext());
96         }
97         // Close the cursor
98         if (mCursor != null) {
99             mCursor.close();
100             mCursor = null;
101         }
102         return mSongList;
103     }
104
105     /**
106      * @param context The {@link Context} to use.
107      * @return The {@link Cursor} used to run the song query.
108      */
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; }
116
117         String selection = (AudioColumns.IS_MUSIC + "=1") +
118                 " AND " + AudioColumns.TITLE + " != ''" +
119                 " AND " + MediaStore.Audio.Media.DATE_ADDED + ">" +
120                 cutoff;
121
122         return context.getContentResolver().query(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI,
123                 new String[] {
124                         /* 0 */
125                         BaseColumns._ID,
126                         /* 1 */
127                         AudioColumns.TITLE,
128                         /* 2 */
129                         AudioColumns.ARTIST,
130                         /* 3 */
131                         AudioColumns.ALBUM_ID,
132                         /* 4 */
133                         AudioColumns.ALBUM,
134                         /* 5 */
135                         AudioColumns.DURATION,
136                         /* 6 */
137                         AudioColumns.YEAR,
138                 }, selection, null, MediaStore.Audio.Media.DATE_ADDED + " DESC");
139     }
140 }