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.Arrays;
21 import java.util.Collections;
22 import java.util.List;
23 import java.util.Map;
24
25 import sk.baka.ambient.ActionsEnum;
26 import sk.baka.ambient.IBackgroundTask;
27 import sk.baka.ambient.R;
28 import sk.baka.ambient.ZoomEnum;
29 import sk.baka.ambient.commons.Interval;
30 import sk.baka.ambient.views.ButtonBar;
31 import sk.baka.ambient.views.TextProgressBar;
32 import android.view.View;
33 import android.view.View.OnClickListener;
34 import android.widget.ImageButton;
35
36 /***
37 * Controls trackbar and background tasks display window.
38 *
39 * @author Martin Vysny
40 */
41 public class TaskSwitcherController extends AbstractController implements
42 IBackgroundTask {
43
44 /***
45 * Creates the controller
46 *
47 * @param mainActivity
48 * the activity
49 */
50 public TaskSwitcherController(MainActivity mainActivity) {
51 super(R.id.taskSwitcherButtonBar, mainActivity);
52 initButtonBar(R.id.taskSwitcherButtonBar, actions);
53 final ImageButton cancelTasks = (ImageButton) mainActivity
54 .findViewById(R.id.backgroundTasksCancelAll);
55 cancelTasks.setBackgroundDrawable(null);
56 cancelTasks.setOnClickListener(new OnClickListener() {
57 public void onClick(View v) {
58 app.getBackgroundTasks().stopAllTasks();
59 }
60 });
61 }
62
63 /***
64 * The actions to display on the Task switcher.
65 */
66 public static final List<ActionsEnum> actions = Collections
67 .unmodifiableList(Arrays.asList(ActionsEnum.ShowPlayer,
68 ActionsEnum.PlaylistManagement, ActionsEnum.ShowCollection,
69 ActionsEnum.ShowFileBrowser, ActionsEnum.ShowContext,
70 ActionsEnum.ShowMagnatune, ActionsEnum.ShowShoutcast,
71 ActionsEnum.ShowAmpache, ActionsEnum.Configure));
72
73 /***
74 * Highlights given action.
75 *
76 * @param action
77 * the action to highlight, may be <code>null</code> - in such
78 * case all actions will be de-highlighted.
79 */
80 public void highlightAction(final ActionsEnum action) {
81 highlightAction(action, (ButtonBar) mainView);
82 }
83
84 /***
85 * Highlights given action on the button bar. The button bar must display
86 * {@link #actions} in order for this method to work correctly.
87 *
88 * @param action
89 * the action to highlight, may be <code>null</code> - in such
90 * case all actions will be de-highlighted.
91 * @param bar
92 * the button bar.
93 */
94 public static void highlightAction(final ActionsEnum action,
95 final ButtonBar bar) {
96 final int index = actions.indexOf(action);
97 final Interval highlight = index < 0 ? Interval.EMPTY : Interval
98 .fromItem(index);
99 bar.highlight(highlight, null);
100 }
101
102 public void backgroundTask(int taskCount, String arbitraryTaskName,
103 final int progress, final int maxProgress) {
104 final View tasks = mainActivity.findViewById(R.id.backgroundTasks);
105 if (taskCount == 0) {
106 tasks.setVisibility(View.GONE);
107 return;
108 }
109 tasks.setVisibility(View.VISIBLE);
110 final TextProgressBar tasksCaption = (TextProgressBar) tasks
111 .findViewById(R.id.backgroundTasksCaption);
112 if ((taskCount > 1) || (arbitraryTaskName == null)) {
113 tasksCaption.setText(R.string.backgroundTasksRunning);
114 } else {
115 tasksCaption.setText(arbitraryTaskName);
116 }
117 tasksCaption.setMax(maxProgress);
118 tasksCaption.setProgress(progress);
119 }
120
121 @Override
122 protected void performZoom(final Map<ZoomEnum, Integer> zoom) {
123 initButtonBar(R.id.taskSwitcherButtonBar, actions);
124 }
125 }