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.io.IOException;
22  import java.util.ArrayList;
23  import java.util.Iterator;
24  import java.util.List;
25  import java.util.concurrent.CopyOnWriteArrayList;
26  
27  import org.xml.sax.SAXException;
28  
29  import sk.baka.ambient.stream.shoutcast.ShoutcastUtils;
30  
31  /***
32   * Search in Shoutcast genres.
33   * 
34   * @author Martin Vysny
35   */
36  public final class ShoutcastEngine implements ISearchEngine {
37  	/***
38  	 * Lists all known genres.
39  	 */
40  	private final static List<String> genres = new CopyOnWriteArrayList<String>();
41  
42  	private static boolean areGenresDownloaded() {
43  		return !genres.isEmpty();
44  	}
45  
46  	private static synchronized List<String> getGenres() throws IOException,
47  			SAXException {
48  		if (areGenresDownloaded()) {
49  			return genres;
50  		}
51  		genres.addAll(ShoutcastUtils.parseGenres());
52  		return genres;
53  	}
54  
55  	public boolean canSearchOffline() {
56  		return areGenresDownloaded();
57  	}
58  
59  	public SearchType getType() {
60  		return SearchType.ShoutcastGenres;
61  	}
62  
63  	private int listViewId;
64  	private SearchActivity owner = null;
65  	private StringListController controller = null;
66  
67  	public void init(SearchActivity owner, final int listViewId) {
68  		this.owner = owner;
69  		this.listViewId = listViewId;
70  	}
71  
72  	public void passivate() {
73  		if (controller != null) {
74  			controller.destroy();
75  			controller = null;
76  		}
77  	}
78  
79  	public List<? extends Object> performSearch(final String query)
80  			throws Exception {
81  		getGenres();
82  		final List<String> result = new ArrayList<String>(genres);
83  		final String _query = query.toLowerCase();
84  		for (final Iterator<String> i = result.iterator(); i.hasNext();) {
85  			if (!i.next().toLowerCase().contains(_query)) {
86  				i.remove();
87  			}
88  		}
89  		return result;
90  	}
91  
92  	@SuppressWarnings("unchecked")
93  	public void update(List<? extends Object> model) {
94  		if (controller == null) {
95  			controller = new StringListController(listViewId, owner);
96  		}
97  		controller.setContent((List<String>) model);
98  	}
99  
100 	public boolean canPerformSearch() {
101 		return true;
102 	}
103 }