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.List;
22
23 import sk.baka.ambient.activity.main.StaticPlaylistController;
24 import sk.baka.ambient.collection.CollectionUtils;
25 import sk.baka.ambient.collection.ICollection;
26 import sk.baka.ambient.collection.TrackMetadataBean;
27
28 /***
29 * Search the collection for given query.
30 *
31 * @author Martin Vysny
32 */
33 public final class CollectionEngine implements ISearchEngine {
34
35 private StaticPlaylistController controller = null;
36 private SearchActivity owner = null;
37 private int listViewId;
38 private final ICollection collection;
39
40 /***
41 * Creates new collection engine which performs search on an arbitrary
42 * {@link ICollection}.
43 *
44 * @param collection
45 * the collection strategy.
46 */
47 CollectionEngine(final ICollection collection) {
48 this.collection = collection;
49 }
50
51 public boolean canSearchOffline() {
52 return collection.isLocal();
53 }
54
55 public void init(final SearchActivity owner, final int listViewId) {
56 this.owner = owner;
57 this.listViewId = listViewId;
58 }
59
60 public void passivate() {
61 controller = null;
62 }
63
64 public List<? extends Object> performSearch(String query) throws Exception {
65 final List<TrackMetadataBean> result = collection.searchTracks(query);
66 CollectionUtils.sortByAlbumOrder(result);
67 return result;
68 }
69
70 @SuppressWarnings("unchecked")
71 public void update(List<? extends Object> model) {
72 if (controller == null) {
73 controller = new StaticPlaylistController(listViewId, listViewId,
74 owner, (List<TrackMetadataBean>) model, false);
75 } else {
76 controller.setTracks((List<TrackMetadataBean>) model);
77 }
78 }
79
80 public SearchType getType() {
81 return SearchType.Collection;
82 }
83
84 public boolean canPerformSearch() {
85 return true;
86 }
87 }