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.util.ArrayList;
22  import java.util.Collections;
23  import java.util.List;
24  import java.util.concurrent.CopyOnWriteArrayList;
25  
26  import sk.baka.ambient.AmbientApplication;
27  import sk.baka.ambient.R;
28  import sk.baka.ambient.collection.CategoryEnum;
29  import sk.baka.ambient.collection.ICollection;
30  import sk.baka.ambient.collection.TrackMetadataBean;
31  import sk.baka.ambient.commons.Interval;
32  import android.content.Context;
33  
34  /***
35   * Handles the first menu, the grouping menu.
36   * 
37   * @author Martin Vysny
38   */
39  final class GroupingManager implements IContentManager {
40  	public IContentManager goBack() {
41  		return null;
42  	}
43  
44  	/***
45  	 * Enumeration of all grouping types.
46  	 * 
47  	 * @author Martin Vysny
48  	 */
49  	private static enum GroupingEnum {
50  		/***
51  		 * Artist.
52  		 */
53  		Artist(new CategoryEnum[] { CategoryEnum.Artist }),
54  		/***
55  		 * Artist/album.
56  		 */
57  		ArtistAlbum(new CategoryEnum[] { CategoryEnum.Artist,
58  				CategoryEnum.Album }),
59  		/***
60  		 * Album.
61  		 */
62  		Album(new CategoryEnum[] { CategoryEnum.Album }),
63  		/***
64  		 * Genre / Artist
65  		 */
66  		GenreArtist(new CategoryEnum[] { CategoryEnum.Genre,
67  				CategoryEnum.Artist }),
68  		/***
69  		 * Genre / Artist / Album
70  		 */
71  		GenreArtistAlbum(new CategoryEnum[] { CategoryEnum.Genre,
72  				CategoryEnum.Artist, CategoryEnum.Album }),
73  		/***
74  		 * Genre / Album
75  		 */
76  		GenreAlbum(
77  				new CategoryEnum[] { CategoryEnum.Genre, CategoryEnum.Album });
78  		/***
79  		 * Criteria ordering for a group.
80  		 */
81  		private final CategoryEnum[] groupOrder;
82  
83  		private GroupingEnum(final CategoryEnum[] groupOrder) {
84  			this.groupOrder = groupOrder;
85  		}
86  
87  		/***
88  		 * Returns criteria ordering for this group.
89  		 * 
90  		 * @return criteria ordering. The array must not be modified.
91  		 */
92  		public CategoryEnum[] getGroupOrder() {
93  			return groupOrder;
94  		}
95  
96  		/***
97  		 * Returns displayable name of this group.
98  		 * 
99  		 * @param context
100 		 *            used to resolve strings
101 		 * @param year
102 		 *            if <code>true</code> then we are searching by year-album.
103 		 * @return displayable name.
104 		 */
105 		public final String getDisplayableName(final Context context,
106 				final boolean year) {
107 			return getPathName(context, groupOrder, groupOrder.length - 1, year);
108 		}
109 
110 		/***
111 		 * Returns displayable name of this group.
112 		 * 
113 		 * @param context
114 		 *            used to resolve strings
115 		 * @param order
116 		 *            the criteria ordering
117 		 * @param end
118 		 *            process the <code>order</code> array until this item
119 		 *            (including).
120 		 * @param year
121 		 *            if <code>true</code> then we are searching by year-album.
122 		 * @return displayable name.
123 		 */
124 		public static String getPathName(final Context context,
125 				final CategoryEnum[] order, final int end, final boolean year) {
126 			final StringBuilder builder = new StringBuilder();
127 			for (int i = 0; i <= end; i++) {
128 				final CategoryEnum crit = order[i];
129 				if ((crit == CategoryEnum.Album) && year) {
130 					builder.append(context.getString(R.string.year));
131 					builder.append('-');
132 				}
133 				builder.append(context.getString(crit.displayableNameId));
134 				if (i < end) {
135 					builder.append(" / ");
136 				}
137 			}
138 			return builder.toString();
139 		}
140 	}
141 
142 	private volatile List<String> contents = null;
143 
144 	public boolean initialize(boolean isYear, final ICollection collection) {
145 		previouslyActivated = null;
146 		final List<String> list = new ArrayList<String>();
147 		for (final GroupingEnum e : GroupingEnum.values()) {
148 			list.add(e.getDisplayableName(AmbientApplication.getInstance(),
149 					isYear));
150 		}
151 		contents = Collections
152 				.unmodifiableList(new CopyOnWriteArrayList<String>(list));
153 		return true;
154 	}
155 
156 	public void uninitialize() {
157 		contents = null;
158 	}
159 
160 	private GroupingEnum previouslyActivated = null;
161 
162 	public IContentManager itemActivated(int i) {
163 		previouslyActivated = GroupingEnum.values()[i];
164 		return new CategoryManager(previouslyActivated.groupOrder);
165 	}
166 
167 	public List<String> getDisplayableContent() {
168 		return contents;
169 	}
170 
171 	public boolean canReturnTracks() {
172 		return false;
173 	}
174 
175 	public List<TrackMetadataBean> getTracks(Interval selection) {
176 		throw new UnsupportedOperationException();
177 	}
178 
179 	public int getIndexOfPreviouslyActivatedItem(IContentManager contentManager) {
180 		final GroupingEnum prev = ((GroupingManager) contentManager).previouslyActivated;
181 		if (prev == null) {
182 			return -1;
183 		}
184 		return prev.ordinal();
185 	}
186 
187 	public String getSelectedItemName() {
188 		return AmbientApplication.getInstance().getString(R.string.categories);
189 	}
190 }