View Javadoc

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.collection.TrackOriginEnum;
33  import sk.baka.ambient.library.Library;
34  import sk.baka.ambient.library.LibraryUtils;
35  import android.database.Cursor;
36  
37  /***
38   * Search the magnatune collection.
39   * 
40   * @author Martin Vysny
41   */
42  public final class MagnatuneEngine implements ISearchEngine {
43  
44  	private StaticPlaylistController controller = null;
45  	private SearchActivity owner = null;
46  	private int listViewId;
47  
48  	public boolean canSearchOffline() {
49  		return true;
50  	}
51  
52  	public void init(final SearchActivity owner, final int listViewId) {
53  		this.owner = owner;
54  		this.listViewId = listViewId;
55  	}
56  
57  	public void passivate() {
58  		controller = null;
59  	}
60  
61  	public List<? extends Object> performSearch(String query) throws Exception {
62  		// search as substring in the following categories: artist, album, track
63  		// name. Use the OR operator for all words present in the query.
64  		final StringTokenizer words = new StringTokenizer(query, " \r\t\f\n");
65  		if (!words.hasMoreTokens()) {
66  			// no words to search for? return empty list
67  			return Collections.emptyList();
68  		}
69  		final List<String> searchFor = new ArrayList<String>();
70  		for (; words.hasMoreTokens();) {
71  			searchFor.add(words.nextToken());
72  		}
73  		// construct the criteria map
74  		final EnumMap<CategoryEnum, List<String>> criteria = new EnumMap<CategoryEnum, List<String>>(
75  				CategoryEnum.class);
76  		criteria.put(CategoryEnum.Album, searchFor);
77  		criteria.put(CategoryEnum.Artist, searchFor);
78  		criteria.put(CategoryEnum.Title, searchFor);
79  		criteria.put(CategoryEnum.Origin, Collections
80  				.singletonList(TrackOriginEnum.Magnatune.toDBString()));
81  		// retrieve the track list
82  		final Library lib = AmbientApplication.getInstance().getLibrary();
83  		final Cursor c = lib.getBackend().findByCriteria(criteria, true, false,
84  				null);
85  		final List<TrackMetadataBean> tracks = LibraryUtils.pollTracks(c);
86  		CollectionUtils.sortByAlbumOrder(tracks);
87  		return tracks;
88  	}
89  
90  	@SuppressWarnings("unchecked")
91  	public void update(List<? extends Object> model) {
92  		if (controller == null) {
93  			controller = new StaticPlaylistController(listViewId, listViewId,
94  					owner, (List<TrackMetadataBean>) model, false);
95  		} else {
96  			controller.setTracks((List<TrackMetadataBean>) model);
97  		}
98  	}
99  
100 	public SearchType getType() {
101 		return SearchType.Magnatune;
102 	}
103 
104 	public boolean canPerformSearch() {
105 		return true;
106 	}
107 }