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  package sk.baka.ambient.activity.main;
19  
20  import java.util.ArrayList;
21  import java.util.Collections;
22  import java.util.List;
23  
24  import sk.baka.ambient.AppState;
25  import sk.baka.ambient.R;
26  import sk.baka.ambient.activity.search.SearchType;
27  import sk.baka.ambient.collection.TrackMetadataBean;
28  import sk.baka.ambient.commons.Interval;
29  import android.app.Activity;
30  import android.widget.TextView;
31  
32  /***
33   * Manages provided track list. Uses a simple {@link List} of tracks as model.
34   * 
35   * @author Martin Vysny
36   */
37  public final class StaticPlaylistController extends AbstractPlaylistController {
38  	private List<TrackMetadataBean> tracks;
39  	private final boolean addActivated;
40  
41  	/***
42  	 * Creates new controller.
43  	 * 
44  	 * @param mainViewId
45  	 *            the view whose visibility is controlled.
46  	 * @param listviewId
47  	 *            the list view id
48  	 * @param activity
49  	 *            the parent activity.
50  	 * @param tracks
51  	 *            initial track list.
52  	 * @param addActivated
53  	 *            if <code>true</code> then the activated items are added at the
54  	 *            end of the playlist.
55  	 */
56  	public StaticPlaylistController(final int mainViewId, final int listviewId,
57  			final Activity activity, final List<TrackMetadataBean> tracks,
58  			final boolean addActivated) {
59  		super(mainViewId, listviewId, activity);
60  		this.tracks = tracks;
61  		this.addActivated = addActivated;
62  		initialize();
63  	}
64  
65  	public void itemActivated(final int index, final Object model) {
66  		if (addActivated) {
67  			app.getPlaylist().add(tracks.get(index));
68  		}
69  	}
70  
71  	@Override
72  	protected int getPlaylistSize() {
73  		return tracks.size();
74  	}
75  
76  	@Override
77  	protected TrackMetadataBean getTrack(int index) {
78  		return tracks.get(index);
79  	}
80  
81  	@Override
82  	protected boolean isCurrentlyPlayedTrack(int index) {
83  		return false;
84  	}
85  
86  	@Override
87  	public void removeItems(Interval remove) {
88  		tracks.subList(remove.start, remove.end + 1).clear();
89  		super.removeItems(remove);
90  	}
91  
92  	@Override
93  	public Interval moveItems(Interval highlight, int index) {
94  		final List<TrackMetadataBean> highlighted = tracks.subList(
95  				highlight.start, highlight.end + 1);
96  		final List<TrackMetadataBean> clone = new ArrayList<TrackMetadataBean>(
97  				highlighted);
98  		highlighted.clear();
99  		int target = index;
100 		if (target > highlight.end) {
101 			target -= highlight.length;
102 		}
103 		tracks.addAll(target, clone);
104 		recomputeListItems();
105 		return new Interval(target, highlight.length);
106 	}
107 
108 	@Override
109 	public Interval moveItemsByOne(Interval highlight, boolean down) {
110 		final int newIndex = down ? highlight.end + 1 : highlight.start - 2;
111 		if ((newIndex < 0) || (newIndex > tracks.size())) {
112 			return highlight;
113 		}
114 		return moveItems(highlight, newIndex);
115 	}
116 
117 	/***
118 	 * Returns an unmodifiable view on the track list.
119 	 * 
120 	 * @return list of tracks.
121 	 */
122 	public List<TrackMetadataBean> getTracks() {
123 		return Collections.unmodifiableList(tracks);
124 	}
125 
126 	@Override
127 	public String getHint(Interval highlight) {
128 		return listView.getResources().getString(R.string.numTracks,
129 				highlight.length);
130 	}
131 
132 	@Override
133 	protected int getQueueNumber(int index) {
134 		return 0;
135 	}
136 
137 	/***
138 	 * Shows given search result on the list view.
139 	 * 
140 	 * @param tracks
141 	 *            the model to show.
142 	 * @param query
143 	 *            the query
144 	 * @param type
145 	 *            hint on the type of data in the model.
146 	 */
147 	public void setResults(List<TrackMetadataBean> tracks, String query,
148 			SearchType type) {
149 		final TextView queryText = (TextView) mainView
150 				.findViewById(R.id.searchresultsQuery);
151 		queryText
152 				.setText(mainActivity.getString(R.string.searchResults, query));
153 		setTracks(tracks);
154 	}
155 
156 	/***
157 	 * Sets new track list.
158 	 * 
159 	 * @param tracks
160 	 */
161 	public void setTracks(final List<TrackMetadataBean> tracks) {
162 		this.tracks = tracks;
163 		update(null);
164 	}
165 
166 	public void stateChanged(AppState state) {
167 		// do nothing
168 	}
169 }