1 /***
2 * Ambient - A music player for the Android platform
3 Copyright (C) 2007 Martin Vysny
4
5 This program is free software: you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation, either version 3 of the License, or
8 (at your option) any later version.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>.
17 */
18 package sk.baka.ambient.collection.local;
19
20 import java.util.List;
21
22 import sk.baka.ambient.AmbientApplication;
23 import sk.baka.ambient.collection.TrackMetadataBean;
24 import sk.baka.ambient.playlist.Random;
25 import android.content.ContentResolver;
26 import android.database.Cursor;
27 import android.provider.MediaStore;
28 import android.provider.MediaStore.Audio.AlbumColumns;
29 import android.provider.MediaStore.Audio.Albums;
30 import android.provider.MediaStore.Audio.AudioColumns;
31 import android.provider.MediaStore.Audio.Media;
32
33 /***
34 * Provides tracks from a {@link MediaStore}.
35 *
36 * @author mvy
37 */
38 public final class MediaStoreTrackProvider extends AbstractTrackProvider {
39 private static final long serialVersionUID = 1L;
40
41 /***
42 * Creates new provider.
43 *
44 * @param random
45 * the initial random value.
46 */
47 public MediaStoreTrackProvider(final Random random) {
48 super(random);
49 }
50
51 @Override
52 protected List<TrackMetadataBean> getAlbumTracks(String album) {
53 final String selection = AudioColumns.ALBUM + "=?";
54 final String[] selectionArgs = new String[] { album };
55 final ContentResolver resolver = AmbientApplication.getInstance()
56 .getContentResolver();
57 final Cursor c = resolver.query(Media.EXTERNAL_CONTENT_URI,
58 MediaStoreCollection.TRACK_COLUMNS, selection, selectionArgs,
59 null);
60 try {
61 return MediaStoreCollection.readTracks(c,
62 Media.EXTERNAL_CONTENT_URI);
63 } catch (final InterruptedException e) {
64 throw new RuntimeException(e);
65 }
66 }
67
68 @Override
69 protected TrackMetadataBean getTrackFromCursor(Cursor c) {
70 return MediaStoreCollection.readTrack(c, Media.EXTERNAL_CONTENT_URI);
71 }
72
73 @Override
74 protected Cursor newCursor(boolean albums) {
75 final ContentResolver resolver = AmbientApplication.getInstance()
76 .getContentResolver();
77 if (!albums) {
78 return resolver.query(Media.EXTERNAL_CONTENT_URI,
79 MediaStoreCollection.TRACK_COLUMNS, null, null, "random()");
80 }
81 return resolver.query(Albums.EXTERNAL_CONTENT_URI,
82 new String[] { AlbumColumns.ALBUM }, null, null, "random()");
83 }
84 }