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