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.Iterator;
24  import java.util.List;
25  
26  import sk.baka.ambient.AmbientApplication;
27  
28  /***
29   * Search in playlist names.
30   * 
31   * @author Martin Vysny
32   */
33  public final class PlaylistEngine implements ISearchEngine {
34  
35  	public boolean canSearchOffline() {
36  		return true;
37  	}
38  
39  	private StringListController controller = null;
40  	private SearchActivity owner = null;
41  	private int listViewId;
42  
43  	public void init(final SearchActivity owner, final int listViewId) {
44  		this.owner = owner;
45  		this.listViewId = listViewId;
46  	}
47  
48  	public void passivate() {
49  		if (controller != null) {
50  			controller.destroy();
51  			controller = null;
52  		}
53  	}
54  
55  	public List<? extends Object> performSearch(String query) throws Exception {
56  		final List<String> playlists = new ArrayList<String>(AmbientApplication
57  				.getInstance().getPlaylistStorage().getNames());
58  		final String _query = query.toLowerCase();
59  		for (final Iterator<String> i = playlists.iterator(); i.hasNext();) {
60  			final String playlist = i.next();
61  			if (!playlist.toLowerCase().contains(_query)) {
62  				i.remove();
63  			}
64  		}
65  		Collections.sort(playlists);
66  		return playlists;
67  	}
68  
69  	@SuppressWarnings("unchecked")
70  	public void update(List<? extends Object> model) {
71  		if (controller == null) {
72  			controller = new StringListController(listViewId, owner);
73  		}
74  		controller.setContent((List<String>) model);
75  	}
76  
77  	public SearchType getType() {
78  		return SearchType.StoredPlaylists;
79  	}
80  
81  	public boolean canPerformSearch() {
82  		return true;
83  	}
84  }