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.io.IOException;
21 import java.io.Serializable;
22 import java.util.ArrayList;
23 import java.util.Arrays;
24 import java.util.Collections;
25 import java.util.List;
26
27 import sk.baka.ambient.ActionsEnum;
28 import sk.baka.ambient.AmbientApplication;
29 import sk.baka.ambient.AppState;
30 import sk.baka.ambient.R;
31 import sk.baka.ambient.commons.Interval;
32 import sk.baka.ambient.commons.ObjectStorage;
33 import sk.baka.ambient.playlist.IPlaylistStrategy;
34 import sk.baka.ambient.views.ViewUtils;
35 import sk.baka.ambient.views.gesturelist.GesturesListView;
36 import android.content.Context;
37 import android.content.DialogInterface;
38 import android.view.View;
39 import android.widget.TextView;
40
41 /***
42 * Controls the playlist manager.
43 *
44 * @author Martin Vysny
45 */
46 public final class PlaylistManagerController extends AbstractListController {
47
48 /***
49 * Creates new playlist manager controller instance.
50 *
51 * @param mainActivity
52 */
53 public PlaylistManagerController(MainActivity mainActivity) {
54 super(R.id.playlistManager, R.id.playlistManagerPlaylists, mainActivity);
55 initButtonBar(R.id.playlistManagerButtons, actions);
56 update(null);
57 }
58
59 private final List<ActionsEnum> actions = Arrays.asList(
60 ActionsEnum.PlaylistStatic, ActionsEnum.PlaylistDynamic,
61 ActionsEnum.PlaylistClear, ActionsEnum.PlaylistSortByAlbums,
62 ActionsEnum.PlaylistShuffle, ActionsEnum.PlaylistSave);
63
64 @Override
65 protected void onAction(ActionsEnum action) {
66 if (action == ActionsEnum.PlaylistSave) {
67 savePlaylistWizard(mainActivity);
68 return;
69 }
70 super.onAction(action);
71 }
72
73 /***
74 * Navigates user through a 'save playlist' wizard.
75 *
76 * @param context
77 * the context.
78 */
79 public void savePlaylistWizard(final Context context) {
80 final ViewUtils.OnTextSubmit listener = new ViewUtils.OnTextSubmit() {
81 public void submit(String text) {
82 savePlaylistAs(context, text, true);
83 update(null);
84 }
85
86 public String validate(String text) {
87 return text.length() == 0 ? context
88 .getString(R.string.playlistNameEmpty) : null;
89 }
90
91 public void cancel() {
92
93 }
94 };
95 ViewUtils.showTextEditor(context, context
96 .getString(R.string.lsplaylistSave), null, context
97 .getString(R.string.savePlaylist), context
98 .getString(R.string.savePlaylistAs), listener);
99 }
100
101 private void savePlaylistAs(final Context context, final String name,
102 final boolean askForOverwrite) {
103 final AmbientApplication app = AmbientApplication.getInstance();
104 try {
105 final ObjectStorage ps = app.getPlaylistStorage();
106 if (ps.contains(name) && askForOverwrite) {
107 final DialogInterface.OnClickListener listener = new DialogInterface.OnClickListener() {
108 public void onClick(DialogInterface dialog, int which) {
109 savePlaylistAs(context, name, false);
110 }
111 };
112 ViewUtils.showYesNoDialog(context, R.string.playlistOverwrite,
113 listener);
114 }
115 final AppState state = new AppState();
116 app.getPlaylist().storeState(state);
117 ps.saveObject(name, (Serializable) state.playlist);
118 } catch (IOException e) {
119 app.error(PlaylistManagerController.class, true, app.getResources()
120 .getString(R.string.lsplaylistErrSave), e);
121 }
122 }
123
124 @Override
125 protected void recomputeListItems() {
126 final List<String> sortedNames = new ArrayList<String>(app
127 .getPlaylistStorage().getNames());
128 Collections.sort(sortedNames);
129 final List<Object> playlistNames = listView.getModel().getModel();
130 playlistNames.clear();
131 playlistNames.addAll(sortedNames);
132 }
133
134 @Override
135 public boolean isReadOnly() {
136 return false;
137 }
138
139 public void itemActivated(int index, Object model) {
140 loadPlaylist((String) model);
141 }
142
143 /***
144 * Loads playlist with given name. Shows an error message when the playlist
145 * failed to be loaded.
146 *
147 * @param name
148 * the name of the playlist.
149 */
150 public void loadPlaylist(final String name) {
151 try {
152 final ObjectStorage ps = app.getPlaylistStorage();
153 final IPlaylistStrategy state = (IPlaylistStrategy) ps
154 .loadObject(name);
155 app.getPlaylist().reinit(state);
156 } catch (final Exception ex) {
157 app.error(PlaylistManagerController.class, true, mainActivity
158 .getString(R.string.lsplaylistErrLoad), ex);
159 }
160 }
161
162 @Override
163 public void removeItems(final Interval remove) {
164 if (remove.isEmpty())
165 return;
166 final DialogInterface.OnClickListener listener = new DialogInterface.OnClickListener() {
167 public void onClick(DialogInterface dialog, int which) {
168 final List<Object> toDelete = listView.getModel().getModel()
169 .subList(remove.start, remove.end + 1);
170 final ObjectStorage storage = AmbientApplication.getInstance()
171 .getPlaylistStorage();
172 for (final Object pname : toDelete) {
173 storage.delete((String) pname);
174 }
175 update(null);
176 }
177 };
178 ViewUtils.showYesNoDialog(mainActivity, R.string.playlistDelete,
179 listener);
180 }
181
182 public void update(GesturesListView listView, View itemView, int index,
183 Object model) {
184 final TextView view = (TextView) itemView;
185 if (listView.getHighlight().contains(index)) {
186 view.setBackgroundColor(highlightColor);
187 } else {
188 view.setBackgroundColor(0);
189 }
190 view.setText((String) model);
191 }
192
193 @Override
194 protected void performZoom(boolean zoom) {
195 super.performZoom(zoom);
196 initButtonBar(R.id.playlistManagerButtons, actions);
197 }
198 }