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.main.cb;
20  
21  import java.text.ParseException;
22  import java.util.ArrayList;
23  import java.util.Collections;
24  import java.util.List;
25  import java.util.concurrent.CopyOnWriteArrayList;
26  
27  import sk.baka.ambient.AmbientApplication;
28  import sk.baka.ambient.R;
29  import sk.baka.ambient.collection.CategoryItem;
30  import sk.baka.ambient.collection.CollectionException;
31  import sk.baka.ambient.collection.CollectionUtils;
32  import sk.baka.ambient.collection.ICollection;
33  import sk.baka.ambient.collection.TrackMetadataBean;
34  import sk.baka.ambient.commons.Interval;
35  import sk.baka.ambient.commons.TagFormatter;
36  
37  /***
38   * Track list is being shown.
39   * 
40   * @author Martin Vysny
41   */
42  final class TrackManager implements IContentManager {
43  	/***
44  	 * The parent manager.
45  	 */
46  	private final CategoryManager parent;
47  	/***
48  	 * Show tracks for this item.
49  	 */
50  	private final CategoryItem categoryItem;
51  
52  	/***
53  	 * Formats track names.
54  	 */
55  	private final TagFormatter formatter;
56  
57  	/***
58  	 * Creates new track manager.
59  	 * 
60  	 * @param parent
61  	 *            parent manager
62  	 * @param categoryItem
63  	 *            Show tracks for this item.
64  	 */
65  	TrackManager(CategoryManager parent, CategoryItem categoryItem) {
66  		this.parent = parent;
67  		this.categoryItem = categoryItem;
68  		try {
69  			formatter = new TagFormatter("{%track }%title");
70  		} catch (ParseException e) {
71  			throw new RuntimeException(e);
72  		}
73  	}
74  
75  	public boolean canReturnTracks() {
76  		return true;
77  	}
78  
79  	public List<String> getDisplayableContent() {
80  		return trackNames;
81  	}
82  
83  	private volatile List<TrackMetadataBean> tracks = null;
84  	private volatile List<String> trackNames = null;
85  
86  	public List<TrackMetadataBean> getTracks(Interval selection) {
87  		return tracks.subList(selection.start, selection.end + 1);
88  	}
89  
90  	public IContentManager goBack() {
91  		return parent;
92  	}
93  
94  	public void uninitialize() {
95  		tracks = null;
96  		trackNames = null;
97  	}
98  
99  	public boolean initialize(boolean isYear, ICollection collection)
100 			throws CollectionException, InterruptedException {
101 		if (tracks != null) {
102 			return false;
103 		}
104 		final List<TrackMetadataBean> t = collection.getTracks(categoryItem);
105 		CollectionUtils.sortByAlbumOrder(t);
106 		tracks = new CopyOnWriteArrayList<TrackMetadataBean>(t);
107 		final List<String> names = new ArrayList<String>();
108 		for (final TrackMetadataBean track : tracks) {
109 			names.add(formatter.format(track));
110 		}
111 		trackNames = Collections
112 				.unmodifiableList(new CopyOnWriteArrayList<String>(names));
113 		return true;
114 	}
115 
116 	public IContentManager itemActivated(int i) {
117 		final TrackMetadataBean track = tracks.get(i);
118 		AmbientApplication.getInstance().getPlaylist().add(track);
119 		return null;
120 	}
121 
122 	public int getIndexOfPreviouslyActivatedItem(IContentManager contentManager) {
123 		return -1;
124 	}
125 
126 	public String getSelectedItemName() {
127 		return AmbientApplication.getInstance().getString(R.string.track);
128 	}
129 }