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.EnumMap;
21 import java.util.List;
22
23 import android.database.Cursor;
24
25 import sk.baka.ambient.AmbientApplication;
26 import sk.baka.ambient.collection.CategoryEnum;
27 import sk.baka.ambient.collection.TrackMetadataBean;
28 import sk.baka.ambient.library.DBStrategy;
29 import sk.baka.ambient.library.LibraryUtils;
30 import sk.baka.ambient.playlist.Random;
31
32 /***
33 * Provides tracks from the library.
34 *
35 * @author Martin Vysny
36 */
37 public final class LibraryTrackProvider extends AbstractTrackProvider {
38
39 private static final long serialVersionUID = 1L;
40
41 /***
42 * Creates new library track provider.
43 *
44 * @param random
45 * initial random value.
46 */
47 public LibraryTrackProvider(Random random) {
48 super(random);
49 }
50
51 @Override
52 protected List<TrackMetadataBean> getAlbumTracks(String album) {
53 final DBStrategy strategy = AmbientApplication.getInstance()
54 .getLibrary().getBackend();
55 final EnumMap<CategoryEnum, String> map = new EnumMap<CategoryEnum, String>(
56 CategoryEnum.class);
57 map.put(CategoryEnum.Album, album);
58 final Cursor albumTracks = strategy.findByCriteria(map, false, null);
59 final List<TrackMetadataBean> result = LibraryUtils
60 .pollTracks(albumTracks);
61 return result;
62 }
63
64 @Override
65 protected TrackMetadataBean getTrackFromCursor(Cursor c) {
66 return LibraryUtils.cursorToTrackBean(c);
67 }
68
69 @Override
70 protected Cursor newCursor(boolean albums) {
71 final String sql;
72 if (!albums) {
73 sql = "select * from tracks order by random()";
74 } else {
75 sql = "select distinct album from tracks order by random()";
76 }
77 final DBStrategy strategy = AmbientApplication.getInstance()
78 .getLibrary().getBackend();
79 return strategy.rawQuery(sql, null);
80 }
81 }