OSDN Git Service

Repackaged com.andrew.apollo to com.cyngn.eleven
[android-x86/packages-apps-Eleven.git] / src / com / cyngn / eleven / loaders / LastAddedLoader.java
1 /*
2  * Copyright (C) 2012 Andrew Neal Licensed under the Apache License, Version 2.0
3  * (the "License"); you may not use this file except in compliance with the
4  * License. You may obtain a copy of the License at
5  * http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law
6  * or agreed to in writing, software distributed under the License is
7  * distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
8  * KIND, either express or implied. See the License for the specific language
9  * governing permissions and limitations under the License.
10  */
11
12 package com.cyngn.eleven.loaders;
13
14 import android.content.Context;
15 import android.database.Cursor;
16 import android.provider.BaseColumns;
17 import android.provider.MediaStore;
18 import android.provider.MediaStore.Audio.AudioColumns;
19
20 import com.cyngn.eleven.model.Song;
21 import com.cyngn.eleven.utils.Lists;
22
23 import java.util.ArrayList;
24 import java.util.List;
25
26 /**
27  * Used to query {@link MediaStore.Audio.Media.EXTERNAL_CONTENT_URI} and return
28  * the Song the user added over the past four of weeks.
29  * 
30  * @author Andrew Neal (andrewdneal@gmail.com)
31  */
32 public class LastAddedLoader extends WrappedAsyncTaskLoader<List<Song>> {
33
34     /**
35      * The result
36      */
37     private final ArrayList<Song> mSongList = Lists.newArrayList();
38
39     /**
40      * The {@link Cursor} used to run the query.
41      */
42     private Cursor mCursor;
43
44     /**
45      * Constructor of <code>LastAddedHandler</code>
46      * 
47      * @param context The {@link Context} to use.
48      */
49     public LastAddedLoader(final Context context) {
50         super(context);
51     }
52
53     /**
54      * {@inheritDoc}
55      */
56     @Override
57     public List<Song> loadInBackground() {
58         // Create the Cursor
59         mCursor = makeLastAddedCursor(getContext());
60         // Gather the data
61         if (mCursor != null && mCursor.moveToFirst()) {
62             do {
63                 // Copy the song Id
64                 final long id = mCursor.getLong(0);
65
66                 // Copy the song name
67                 final String songName = mCursor.getString(1);
68
69                 // Copy the artist name
70                 final String artist = mCursor.getString(2);
71
72                 // Copy the album name
73                 final String album = mCursor.getString(3);
74
75                 // Copy the duration
76                 final long duration = mCursor.getLong(4);
77
78                 // Convert the duration into seconds
79                 final int durationInSecs = (int) duration / 1000;
80
81                 // Create a new song
82                 final Song song = new Song(id, songName, artist, album, durationInSecs);
83
84                 // Add everything up
85                 mSongList.add(song);
86             } while (mCursor.moveToNext());
87         }
88         // Close the cursor
89         if (mCursor != null) {
90             mCursor.close();
91             mCursor = null;
92         }
93         return mSongList;
94     }
95
96     /**
97      * @param context The {@link Context} to use.
98      * @return The {@link Cursor} used to run the song query.
99      */
100     public static final Cursor makeLastAddedCursor(final Context context) {
101         final int fourWeeks = 4 * 3600 * 24 * 7;
102         final StringBuilder selection = new StringBuilder();
103         selection.append(AudioColumns.IS_MUSIC + "=1");
104         selection.append(" AND " + AudioColumns.TITLE + " != ''"); //$NON-NLS-2$
105         selection.append(" AND " + MediaStore.Audio.Media.DATE_ADDED + ">"); //$NON-NLS-2$
106         selection.append(System.currentTimeMillis() / 1000 - fourWeeks);
107         return context.getContentResolver().query(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI,
108                 new String[] {
109                         /* 0 */
110                         BaseColumns._ID,
111                         /* 1 */
112                         AudioColumns.TITLE,
113                         /* 2 */
114                         AudioColumns.ARTIST,
115                         /* 3 */
116                         AudioColumns.ALBUM,
117                         /* 4 */
118                         AudioColumns.DURATION
119                 }, selection.toString(), null, MediaStore.Audio.Media.DATE_ADDED + " DESC");
120     }
121 }