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
19 package sk.baka.ambient.activity.search;
20
21 import java.util.ArrayList;
22 import java.util.Collections;
23 import java.util.EnumMap;
24 import java.util.List;
25 import java.util.StringTokenizer;
26
27 import sk.baka.ambient.AmbientApplication;
28 import sk.baka.ambient.activity.main.StaticPlaylistController;
29 import sk.baka.ambient.collection.CategoryEnum;
30 import sk.baka.ambient.collection.CollectionUtils;
31 import sk.baka.ambient.collection.TrackMetadataBean;
32 import sk.baka.ambient.library.Library;
33 import sk.baka.ambient.library.LibraryUtils;
34 import android.database.Cursor;
35
36 /***
37 * Search the collection for given query.
38 *
39 * @author Martin Vysny
40 */
41 public final class LibraryEngine implements ISearchEngine {
42
43 private StaticPlaylistController controller = null;
44 private SearchActivity owner = null;
45 private int listViewId;
46
47 public boolean canSearchOffline() {
48 return true;
49 }
50
51 public void init(final SearchActivity owner, final int listViewId) {
52 this.owner = owner;
53 this.listViewId = listViewId;
54 }
55
56 public void passivate() {
57 controller = null;
58 }
59
60 public List<? extends Object> performSearch(String query) throws Exception {
61
62
63 final StringTokenizer words = new StringTokenizer(query, " \r\t\f\n");
64 if (!words.hasMoreTokens()) {
65
66 return Collections.emptyList();
67 }
68 final List<String> searchFor = new ArrayList<String>();
69 for (; words.hasMoreTokens();) {
70 searchFor.add(words.nextToken());
71 }
72
73 final EnumMap<CategoryEnum, List<String>> criteria = new EnumMap<CategoryEnum, List<String>>(
74 CategoryEnum.class);
75 criteria.put(CategoryEnum.Album, searchFor);
76 criteria.put(CategoryEnum.Artist, searchFor);
77 criteria.put(CategoryEnum.Title, searchFor);
78
79 final Library lib = AmbientApplication.getInstance().getLibrary();
80 final Cursor c = lib.getBackend().findByCriteria(criteria, true, false,
81 null);
82 final List<TrackMetadataBean> tracks = LibraryUtils.pollTracks(c);
83 CollectionUtils.sortByAlbumOrder(tracks);
84 return tracks;
85 }
86
87 @SuppressWarnings("unchecked")
88 public void update(List<? extends Object> model) {
89 if (controller == null) {
90 controller = new StaticPlaylistController(listViewId, listViewId,
91 owner, (List<TrackMetadataBean>) model, false);
92 } else {
93 controller.setTracks((List<TrackMetadataBean>) model);
94 }
95 }
96
97 public SearchType getType() {
98 return SearchType.Library;
99 }
100
101 public boolean canPerformSearch() {
102 return true;
103 }
104 }