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.List;
22  
23  import sk.baka.ambient.AmbientApplication;
24  import sk.baka.ambient.AppState;
25  import sk.baka.ambient.activity.main.StaticPlaylistController;
26  import sk.baka.ambient.collection.CollectionUtils;
27  import sk.baka.ambient.collection.TrackMetadataBean;
28  import sk.baka.ambient.collection.ampache.CollectionImpl;
29  
30  /***
31   * Search the magnatune collection.
32   * 
33   * @author Martin Vysny
34   */
35  public final class AmpacheEngine implements ISearchEngine {
36  
37  	private StaticPlaylistController controller = null;
38  	private SearchActivity owner = null;
39  	private int listViewId;
40  
41  	public boolean canSearchOffline() {
42  		return false;
43  	}
44  
45  	public void init(final SearchActivity owner, final int listViewId) {
46  		this.owner = owner;
47  		this.listViewId = listViewId;
48  	}
49  
50  	public void passivate() {
51  		controller = null;
52  	}
53  
54  	public List<? extends Object> performSearch(String query) throws Exception {
55  		final AppState state = AmbientApplication.getInstance()
56  				.getStateHandler().getStartupState();
57  		final CollectionImpl collection = new CollectionImpl(null, null, null);
58  		collection.reset(state.ampacheClient);
59  		final List<TrackMetadataBean> tracks = collection.searchTracks(query);
60  		CollectionUtils.sortByAlbumOrder(tracks);
61  		return tracks;
62  	}
63  
64  	@SuppressWarnings("unchecked")
65  	public void update(List<? extends Object> model) {
66  		if (controller == null) {
67  			controller = new StaticPlaylistController(listViewId, listViewId,
68  					owner, (List<TrackMetadataBean>) model, false);
69  		} else {
70  			controller.setTracks((List<TrackMetadataBean>) model);
71  		}
72  	}
73  
74  	public SearchType getType() {
75  		return SearchType.Ampache;
76  	}
77  
78  	public boolean canPerformSearch() {
79  		final AppState state = AmbientApplication.getInstance()
80  				.getStateHandler().getStartupState();
81  		return state.ampacheClient != null && state.ampacheClient.isValid();
82  	}
83  }