]>
Commit | Line | Data |
---|---|---|
7bcde8d7 MG |
1 | package de.rmdir.ms2debounce; |
2 | ||
3 | import android.app.Activity; | |
fd8f8652 MG |
4 | import android.app.Dialog; |
5 | import android.app.AlertDialog; | |
7bcde8d7 | 6 | import android.os.Bundle; |
ae569a50 | 7 | import android.content.Intent; |
fd8f8652 | 8 | import android.content.DialogInterface; |
ee6322a1 | 9 | import android.widget.TextView; |
c51c7009 | 10 | import android.widget.EditText; |
dea0f4b0 MG |
11 | import android.widget.Button; |
12 | import android.widget.CheckBox; | |
13 | import android.view.View; | |
fd8f8652 MG |
14 | import android.view.Menu; |
15 | import android.view.MenuInflater; | |
16 | import android.view.MenuItem; | |
1559225a MG |
17 | import android.text.TextWatcher; |
18 | import android.text.Editable; | |
7bcde8d7 MG |
19 | |
20 | public class MS2Debounce extends Activity | |
21 | { | |
dea0f4b0 MG |
22 | private DebounceModuleHelper module; |
23 | ||
e75481cd MG |
24 | // Calling these is expensive, so cache the result... |
25 | private boolean loaded; | |
26 | private boolean safe_to_load; | |
27 | private int debounce_delay; | |
381027a8 MG |
28 | private int settle_time; |
29 | private int poll_time; | |
e75481cd | 30 | |
dea0f4b0 MG |
31 | public MS2Debounce() |
32 | { | |
33 | super(); | |
34 | module = new DebounceModuleHelper(this); | |
35 | } | |
36 | ||
226a7d4d MG |
37 | @Override |
38 | public void onCreate(Bundle savedInstanceState) | |
39 | { | |
40 | super.onCreate(savedInstanceState); | |
ae569a50 | 41 | |
dea0f4b0 | 42 | setContentView(R.layout.main); |
1559225a MG |
43 | |
44 | EditText textDelay = (EditText)findViewById(R.id.debounce_delay); | |
45 | textDelay.addTextChangedListener(new TextWatcher() { | |
46 | @Override | |
47 | public void afterTextChanged(Editable delay) { | |
48 | if (delay.toString().length() > 0) { | |
49 | module.setSavedDelay(Integer.parseInt(delay.toString())); | |
381027a8 MG |
50 | } |
51 | } | |
1559225a | 52 | |
381027a8 MG |
53 | @Override |
54 | public void beforeTextChanged(CharSequence s, int start, int count, int after) { | |
55 | } | |
56 | ||
57 | @Override | |
58 | public void onTextChanged(CharSequence s, int start, int before, int count) { | |
59 | } | |
60 | }); | |
61 | ||
62 | EditText textSettle = (EditText)findViewById(R.id.settle_time); | |
63 | textSettle.addTextChangedListener(new TextWatcher() { | |
64 | @Override | |
65 | public void afterTextChanged(Editable settle_time) { | |
66 | if (settle_time.toString().length() > 0) { | |
67 | module.setSavedSettle(Integer.parseInt(settle_time.toString())); | |
68 | } | |
69 | } | |
70 | ||
71 | @Override | |
72 | public void beforeTextChanged(CharSequence s, int start, int count, int after) { | |
73 | } | |
74 | ||
75 | @Override | |
76 | public void onTextChanged(CharSequence s, int start, int before, int count) { | |
77 | } | |
78 | }); | |
79 | ||
80 | EditText textPoll = (EditText)findViewById(R.id.poll_time); | |
81 | textPoll.addTextChangedListener(new TextWatcher() { | |
82 | @Override | |
83 | public void afterTextChanged(Editable poll_time) { | |
84 | if (poll_time.toString().length() > 0) { | |
85 | module.setSavedPoll(Integer.parseInt(poll_time.toString())); | |
1559225a MG |
86 | } |
87 | } | |
88 | ||
89 | @Override | |
90 | public void beforeTextChanged(CharSequence s, int start, int count, int after) { | |
91 | } | |
92 | ||
93 | @Override | |
94 | public void onTextChanged(CharSequence s, int start, int before, int count) { | |
95 | } | |
96 | }); | |
97 | ||
dea0f4b0 MG |
98 | updateUI(); |
99 | } | |
100 | ||
101 | private void updateUI() { | |
dea0f4b0 MG |
102 | disableUI(); |
103 | ||
01b288b5 | 104 | // Calling these is expensive, so cache the result... |
e75481cd MG |
105 | loaded = module.isLoaded(); |
106 | safe_to_load = module.is_safe_to_load(); | |
107 | debounce_delay = module.getDelay(); | |
381027a8 MG |
108 | settle_time = module.getSettle(); |
109 | poll_time = module.getPoll(); | |
dea0f4b0 | 110 | |
c51c7009 | 111 | TextView text = (TextView)findViewById(R.id.text); |
381027a8 MG |
112 | text.setText("Current status:\n\nModule loaded: " + loaded + "\n" + |
113 | "debounce_delay: " + debounce_delay + "ms\n" + | |
114 | "settle_time: " + settle_time + "us\n" + | |
115 | "poll_time: " + poll_time + "us\n" + | |
116 | "safe_to_load: " + safe_to_load + " (module loaded by this app)"); | |
dea0f4b0 | 117 | |
1559225a MG |
118 | EditText textDelay = (EditText)findViewById(R.id.debounce_delay); |
119 | textDelay.setText(Integer.toString(module.getSavedDelay())); | |
120 | textDelay.setEnabled(true); | |
c51c7009 | 121 | |
381027a8 MG |
122 | EditText textSettle = (EditText)findViewById(R.id.settle_time); |
123 | textSettle.setText(Integer.toString(module.getSavedSettle())); | |
124 | textSettle.setEnabled(true); | |
125 | ||
126 | EditText textPoll = (EditText)findViewById(R.id.poll_time); | |
127 | textPoll.setText(Integer.toString(module.getSavedPoll())); | |
128 | textPoll.setEnabled(true); | |
129 | ||
130 | Button set = (Button)findViewById(R.id.set); | |
131 | if (loaded) { | |
132 | set.setEnabled(true); | |
dea0f4b0 | 133 | } else { |
381027a8 | 134 | set.setEnabled(false); |
dea0f4b0 MG |
135 | } |
136 | ||
137 | Button load = (Button)findViewById(R.id.load); | |
138 | if (loaded) { | |
139 | load.setEnabled(false); | |
140 | } else { | |
141 | load.setEnabled(true); | |
142 | } | |
143 | ||
144 | Button unload = (Button)findViewById(R.id.unload); | |
145 | if (loaded) { | |
146 | unload.setEnabled(true); | |
147 | } else { | |
148 | unload.setEnabled(false); | |
149 | } | |
226a7d4d | 150 | |
dea0f4b0 | 151 | CheckBox on_boot = (CheckBox)findViewById(R.id.on_boot); |
c3053460 | 152 | on_boot.setChecked(module.get_on_boot()); |
dea0f4b0 MG |
153 | if (safe_to_load) { |
154 | on_boot.setEnabled(true); | |
155 | } else { | |
156 | on_boot.setEnabled(false); | |
157 | } | |
158 | } | |
159 | ||
160 | private void disableUI() { | |
1559225a MG |
161 | EditText textDelay = (EditText)findViewById(R.id.debounce_delay); |
162 | textDelay.setEnabled(false); | |
163 | ||
381027a8 MG |
164 | EditText textSettle = (EditText)findViewById(R.id.settle_time); |
165 | textSettle.setEnabled(false); | |
166 | ||
167 | EditText textPoll = (EditText)findViewById(R.id.poll_time); | |
168 | textPoll.setEnabled(false); | |
169 | ||
170 | Button set = (Button)findViewById(R.id.set); | |
171 | set.setEnabled(false); | |
dea0f4b0 MG |
172 | |
173 | Button load = (Button)findViewById(R.id.load); | |
174 | load.setEnabled(false); | |
175 | ||
176 | Button unload = (Button)findViewById(R.id.unload); | |
177 | unload.setEnabled(false); | |
178 | ||
179 | CheckBox on_boot = (CheckBox)findViewById(R.id.on_boot); | |
180 | on_boot.setEnabled(false); | |
181 | } | |
182 | ||
183 | public void loadModule(View view) { | |
184 | disableUI(); | |
226a7d4d MG |
185 | if (!module.isLoaded()) { |
186 | module.loadModule(); | |
187 | } | |
dea0f4b0 MG |
188 | updateUI(); |
189 | } | |
ae569a50 | 190 | |
dea0f4b0 MG |
191 | public void unloadModule(View view) { |
192 | disableUI(); | |
193 | if (module.isLoaded()) { | |
194 | module.unloadModule(); | |
195 | } | |
196 | updateUI(); | |
197 | } | |
ee6322a1 | 198 | |
381027a8 | 199 | public void setValues(View view) { |
dea0f4b0 | 200 | disableUI(); |
dea0f4b0 MG |
201 | if (!module.isLoaded()) { |
202 | module.loadModule(); | |
203 | } | |
381027a8 | 204 | module.setAllValues(); |
dea0f4b0 | 205 | updateUI(); |
226a7d4d | 206 | } |
fd8f8652 | 207 | |
c3053460 MG |
208 | public void toggle_on_boot(View view) { |
209 | CheckBox on_boot = (CheckBox)view; | |
210 | ||
211 | module.set_on_boot(on_boot.isChecked()); | |
212 | } | |
213 | ||
fd8f8652 MG |
214 | @Override |
215 | public boolean onCreateOptionsMenu(Menu menu) { | |
216 | MenuInflater inflater = getMenuInflater(); | |
217 | inflater.inflate(R.menu.main, menu); | |
218 | return true; | |
219 | } | |
220 | ||
221 | @Override | |
222 | public boolean onOptionsItemSelected(MenuItem item) { | |
223 | switch (item.getItemId()) { | |
224 | case R.id.about: | |
225 | showDialog(42); | |
226 | return true; | |
227 | default: | |
228 | return super.onOptionsItemSelected(item); | |
229 | } | |
230 | } | |
231 | ||
232 | protected Dialog onCreateDialog(int id) { | |
233 | Dialog dlg = null; | |
234 | ||
235 | AlertDialog.Builder about = new AlertDialog.Builder(this); | |
236 | about.setMessage("Milestone 2 Debounce\n\n(C) 2011 Michael Gernoth <michael@gernoth.net>\n\nThis program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation version 2 of the License.\n\nThis program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.\n\nYou should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA") | |
237 | .setCancelable(true) | |
238 | .setPositiveButton("Ok", new DialogInterface.OnClickListener() { | |
239 | public void onClick(DialogInterface dialog, int id) { | |
240 | dialog.cancel(); | |
241 | } | |
242 | }); | |
243 | dlg = about.create(); | |
244 | ||
245 | return dlg; | |
246 | } | |
7bcde8d7 | 247 | } |