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.views.gesturelist.keypad;
20
21 import java.util.ArrayList;
22 import java.util.List;
23
24 import android.view.KeyEvent;
25
26 import sk.baka.ambient.views.KeyEventHandler;
27 import sk.baka.ambient.views.gesturelist.GesturesListView;
28
29 /***
30 * Manages the keypad.
31 *
32 * @author Martin Vysny
33 */
34 public final class KeypadManager extends KeyEventHandler {
35 private final GesturesListView owner;
36
37 /***
38 * Creates new manager instance.
39 *
40 * @param owner
41 * the owner list view.
42 */
43 public KeypadManager(final GesturesListView owner) {
44 this.owner = owner;
45 handlers.add(new KeypadSearch(owner));
46 handlers.add(new KeypadController(owner));
47 handlers.add(new KeypadDpadSearch(owner));
48 }
49
50 private final List<AbstractKeypadHandler> handlers = new ArrayList<AbstractKeypadHandler>();
51
52 /***
53 * Activates required handler. If <code>null</code> then all handlers are
54 * deactivated.
55 *
56 * @param handlerClass
57 * the handler to activate.
58 */
59 public void activate(
60 final Class<? extends AbstractKeypadHandler> handlerClass) {
61 activeHandler = null;
62 for (final AbstractKeypadHandler h : handlers) {
63 if (handlerClass != null && handlerClass.isInstance(h)) {
64 h.start();
65 activeHandler = h;
66 } else {
67 if (h.isStarted()) {
68 h.stop();
69 }
70 }
71 }
72 }
73
74 /***
75 * Currently active handler.
76 */
77 private AbstractKeypadHandler activeHandler = null;
78
79 @Override
80 protected boolean onKey(int keyCode, int count, KeyEvent event) {
81 if (activeHandler != null && !activeHandler.isStarted()) {
82 activeHandler = null;
83 }
84 if (activeHandler == null) {
85 for (final AbstractKeypadHandler h : handlers) {
86 if (h.isActivatedByKey(keyCode, event)) {
87 activeHandler = h;
88 h.start();
89 break;
90 }
91 }
92 }
93 if (activeHandler != null) {
94 return activeHandler.onKey(event);
95 }
96 return super.onKey(keyCode, count, event);
97 }
98
99 @Override
100 protected boolean onConfirm() {
101 final int index = owner.getSelectedItemPosition();
102 final boolean isEOP = owner.isEOP(index);
103 if ((index >= 0) && !isEOP) {
104 owner.listener.itemActivated(index, owner.getModel().getModel()
105 .get(index));
106 }
107 return true;
108 }
109
110 /***
111 * Invoked when the selection is changed in the underlying list view.
112 */
113 public void selectionChanged() {
114 if (activeHandler != null && !activeHandler.isStarted()) {
115 activeHandler = null;
116 }
117 if (activeHandler == null) {
118 return;
119 }
120 activeHandler.selectionChanged();
121 }
122 }