]>
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; | |
2bb83a0e MG |
30 | private boolean hw_debounce_en; |
31 | private int hw_debounce_time; | |
d002e66d | 32 | private boolean drive_inactive_en; |
1786d191 | 33 | private boolean active_high_en; |
e75481cd | 34 | |
dea0f4b0 MG |
35 | public MS2Debounce() |
36 | { | |
37 | super(); | |
38 | module = new DebounceModuleHelper(this); | |
39 | } | |
40 | ||
226a7d4d MG |
41 | @Override |
42 | public void onCreate(Bundle savedInstanceState) | |
43 | { | |
44 | super.onCreate(savedInstanceState); | |
ae569a50 | 45 | |
dea0f4b0 | 46 | setContentView(R.layout.main); |
1559225a MG |
47 | |
48 | EditText textDelay = (EditText)findViewById(R.id.debounce_delay); | |
49 | textDelay.addTextChangedListener(new TextWatcher() { | |
50 | @Override | |
51 | public void afterTextChanged(Editable delay) { | |
52 | if (delay.toString().length() > 0) { | |
53 | module.setSavedDelay(Integer.parseInt(delay.toString())); | |
381027a8 MG |
54 | } |
55 | } | |
1559225a | 56 | |
381027a8 MG |
57 | @Override |
58 | public void beforeTextChanged(CharSequence s, int start, int count, int after) { | |
59 | } | |
60 | ||
61 | @Override | |
62 | public void onTextChanged(CharSequence s, int start, int before, int count) { | |
63 | } | |
64 | }); | |
65 | ||
66 | EditText textSettle = (EditText)findViewById(R.id.settle_time); | |
67 | textSettle.addTextChangedListener(new TextWatcher() { | |
68 | @Override | |
69 | public void afterTextChanged(Editable settle_time) { | |
70 | if (settle_time.toString().length() > 0) { | |
71 | module.setSavedSettle(Integer.parseInt(settle_time.toString())); | |
72 | } | |
73 | } | |
74 | ||
75 | @Override | |
76 | public void beforeTextChanged(CharSequence s, int start, int count, int after) { | |
77 | } | |
78 | ||
79 | @Override | |
80 | public void onTextChanged(CharSequence s, int start, int before, int count) { | |
81 | } | |
82 | }); | |
83 | ||
84 | EditText textPoll = (EditText)findViewById(R.id.poll_time); | |
85 | textPoll.addTextChangedListener(new TextWatcher() { | |
86 | @Override | |
87 | public void afterTextChanged(Editable poll_time) { | |
88 | if (poll_time.toString().length() > 0) { | |
89 | module.setSavedPoll(Integer.parseInt(poll_time.toString())); | |
1559225a MG |
90 | } |
91 | } | |
92 | ||
93 | @Override | |
94 | public void beforeTextChanged(CharSequence s, int start, int count, int after) { | |
95 | } | |
96 | ||
97 | @Override | |
98 | public void onTextChanged(CharSequence s, int start, int before, int count) { | |
99 | } | |
100 | }); | |
101 | ||
2bb83a0e MG |
102 | EditText textHwDebounceTime = (EditText)findViewById(R.id.hw_debounce_time); |
103 | textHwDebounceTime.addTextChangedListener(new TextWatcher() { | |
104 | @Override | |
105 | public void afterTextChanged(Editable hw_debounce_time) { | |
106 | if (hw_debounce_time.toString().length() > 0) { | |
107 | module.setSavedHwDebounceTime(Integer.parseInt(hw_debounce_time.toString())); | |
108 | } | |
109 | } | |
110 | ||
111 | @Override | |
112 | public void beforeTextChanged(CharSequence s, int start, int count, int after) { | |
113 | } | |
114 | ||
115 | @Override | |
116 | public void onTextChanged(CharSequence s, int start, int before, int count) { | |
117 | } | |
118 | }); | |
119 | ||
dea0f4b0 MG |
120 | updateUI(); |
121 | } | |
122 | ||
123 | private void updateUI() { | |
dea0f4b0 MG |
124 | disableUI(); |
125 | ||
01b288b5 | 126 | // Calling these is expensive, so cache the result... |
e75481cd MG |
127 | loaded = module.isLoaded(); |
128 | safe_to_load = module.is_safe_to_load(); | |
129 | debounce_delay = module.getDelay(); | |
381027a8 MG |
130 | settle_time = module.getSettle(); |
131 | poll_time = module.getPoll(); | |
2bb83a0e MG |
132 | hw_debounce_en = module.getHwDebounce(); |
133 | hw_debounce_time = module.getHwDebounceTime(); | |
1786d191 MG |
134 | //drive_inactive_en = module.getDriveInactive(); |
135 | active_high_en = module.getActiveHigh(); | |
dea0f4b0 | 136 | |
c51c7009 | 137 | TextView text = (TextView)findViewById(R.id.text); |
882feaf4 | 138 | text.setText("Module loaded: " + loaded + "\n" + |
381027a8 MG |
139 | "debounce_delay: " + debounce_delay + "ms\n" + |
140 | "settle_time: " + settle_time + "us\n" + | |
722ab95d | 141 | "poll_time: " + poll_time + "ms\n" + |
2bb83a0e | 142 | "safe_to_load: " + safe_to_load + " (module loaded by this app)\n" + |
1786d191 | 143 | "hw_debounce: " + (hw_debounce_en?"en":"dis") + "abled, " + ((hw_debounce_time+1)*31) + "us (" + hw_debounce_time + "), active high: " + (active_high_en?"en":"dis") + "abled"); |
dea0f4b0 | 144 | |
1559225a MG |
145 | EditText textDelay = (EditText)findViewById(R.id.debounce_delay); |
146 | textDelay.setText(Integer.toString(module.getSavedDelay())); | |
147 | textDelay.setEnabled(true); | |
c51c7009 | 148 | |
381027a8 MG |
149 | EditText textSettle = (EditText)findViewById(R.id.settle_time); |
150 | textSettle.setText(Integer.toString(module.getSavedSettle())); | |
151 | textSettle.setEnabled(true); | |
152 | ||
153 | EditText textPoll = (EditText)findViewById(R.id.poll_time); | |
154 | textPoll.setText(Integer.toString(module.getSavedPoll())); | |
155 | textPoll.setEnabled(true); | |
156 | ||
2bb83a0e MG |
157 | EditText textHwDebounceTime = (EditText)findViewById(R.id.hw_debounce_time); |
158 | textHwDebounceTime.setText(Integer.toString(module.getSavedHwDebounceTime())); | |
159 | textHwDebounceTime.setEnabled(true); | |
160 | ||
381027a8 MG |
161 | Button set = (Button)findViewById(R.id.set); |
162 | if (loaded) { | |
163 | set.setEnabled(true); | |
dea0f4b0 | 164 | } else { |
381027a8 | 165 | set.setEnabled(false); |
dea0f4b0 MG |
166 | } |
167 | ||
168 | Button load = (Button)findViewById(R.id.load); | |
169 | if (loaded) { | |
170 | load.setEnabled(false); | |
171 | } else { | |
172 | load.setEnabled(true); | |
173 | } | |
174 | ||
175 | Button unload = (Button)findViewById(R.id.unload); | |
176 | if (loaded) { | |
177 | unload.setEnabled(true); | |
178 | } else { | |
179 | unload.setEnabled(false); | |
180 | } | |
226a7d4d | 181 | |
dea0f4b0 | 182 | CheckBox on_boot = (CheckBox)findViewById(R.id.on_boot); |
c3053460 | 183 | on_boot.setChecked(module.get_on_boot()); |
dea0f4b0 MG |
184 | if (safe_to_load) { |
185 | on_boot.setEnabled(true); | |
186 | } else { | |
187 | on_boot.setEnabled(false); | |
188 | } | |
2bb83a0e MG |
189 | |
190 | CheckBox hw_debounce = (CheckBox)findViewById(R.id.hw_debounce); | |
191 | hw_debounce.setChecked(module.getSavedHwDebounce()); | |
192 | hw_debounce.setEnabled(true); | |
d002e66d | 193 | |
1786d191 MG |
194 | //CheckBox drive_inactive = (CheckBox)findViewById(R.id.drive_inactive); |
195 | //drive_inactive.setChecked(module.getSavedDriveInactive()); | |
196 | //drive_inactive.setEnabled(true); | |
197 | ||
198 | CheckBox active_high = (CheckBox)findViewById(R.id.active_high); | |
199 | active_high.setChecked(module.getSavedActiveHigh()); | |
200 | active_high.setEnabled(true); | |
dea0f4b0 MG |
201 | } |
202 | ||
203 | private void disableUI() { | |
1559225a MG |
204 | EditText textDelay = (EditText)findViewById(R.id.debounce_delay); |
205 | textDelay.setEnabled(false); | |
206 | ||
381027a8 MG |
207 | EditText textSettle = (EditText)findViewById(R.id.settle_time); |
208 | textSettle.setEnabled(false); | |
209 | ||
210 | EditText textPoll = (EditText)findViewById(R.id.poll_time); | |
211 | textPoll.setEnabled(false); | |
212 | ||
2bb83a0e MG |
213 | EditText textHwDebounceTime = (EditText)findViewById(R.id.hw_debounce_time); |
214 | textHwDebounceTime.setEnabled(false); | |
215 | ||
381027a8 MG |
216 | Button set = (Button)findViewById(R.id.set); |
217 | set.setEnabled(false); | |
dea0f4b0 MG |
218 | |
219 | Button load = (Button)findViewById(R.id.load); | |
220 | load.setEnabled(false); | |
221 | ||
222 | Button unload = (Button)findViewById(R.id.unload); | |
223 | unload.setEnabled(false); | |
224 | ||
225 | CheckBox on_boot = (CheckBox)findViewById(R.id.on_boot); | |
226 | on_boot.setEnabled(false); | |
2bb83a0e MG |
227 | |
228 | CheckBox hw_debounce = (CheckBox)findViewById(R.id.hw_debounce); | |
229 | hw_debounce.setEnabled(false); | |
d002e66d | 230 | |
1786d191 MG |
231 | //CheckBox drive_inactive = (CheckBox)findViewById(R.id.drive_inactive); |
232 | //drive_inactive.setEnabled(false); | |
233 | ||
234 | CheckBox active_high = (CheckBox)findViewById(R.id.active_high); | |
235 | active_high.setEnabled(false); | |
dea0f4b0 MG |
236 | } |
237 | ||
238 | public void loadModule(View view) { | |
239 | disableUI(); | |
226a7d4d | 240 | if (!module.isLoaded()) { |
0c4d887c MG |
241 | try { |
242 | if (!module.loadModule()) | |
243 | showDialog(22); | |
244 | } catch (NotRootedException e) { | |
245 | showDialog(23); | |
246 | } catch (ShellException e) { | |
247 | showDialog(24); | |
248 | } | |
226a7d4d | 249 | } |
dea0f4b0 MG |
250 | updateUI(); |
251 | } | |
ae569a50 | 252 | |
dea0f4b0 MG |
253 | public void unloadModule(View view) { |
254 | disableUI(); | |
255 | if (module.isLoaded()) { | |
0c4d887c MG |
256 | try { |
257 | module.unloadModule(); | |
258 | } catch (NotRootedException e) { | |
259 | showDialog(23); | |
260 | } catch (ShellException e) { | |
261 | showDialog(24); | |
262 | } | |
dea0f4b0 MG |
263 | } |
264 | updateUI(); | |
265 | } | |
ee6322a1 | 266 | |
381027a8 | 267 | public void setValues(View view) { |
dea0f4b0 | 268 | disableUI(); |
dea0f4b0 | 269 | if (!module.isLoaded()) { |
0c4d887c MG |
270 | try { |
271 | if (!module.loadModule()) | |
272 | showDialog(22); | |
273 | } catch (NotRootedException e) { | |
a7c1cd77 | 274 | showDialog(23); |
0c4d887c MG |
275 | } catch (ShellException e) { |
276 | showDialog(24); | |
277 | } | |
dea0f4b0 | 278 | } |
381027a8 | 279 | module.setAllValues(); |
dea0f4b0 | 280 | updateUI(); |
226a7d4d | 281 | } |
fd8f8652 | 282 | |
c3053460 MG |
283 | public void toggle_on_boot(View view) { |
284 | CheckBox on_boot = (CheckBox)view; | |
285 | ||
286 | module.set_on_boot(on_boot.isChecked()); | |
287 | } | |
288 | ||
2bb83a0e MG |
289 | public void toggle_hw_debounce(View view) { |
290 | CheckBox hw_debounce = (CheckBox)view; | |
291 | ||
292 | module.setSavedHwDebounce(hw_debounce.isChecked()); | |
293 | } | |
294 | ||
1786d191 MG |
295 | //public void toggle_drive_inactive(View view) { |
296 | // CheckBox drive_inactive = (CheckBox)view; | |
297 | ||
298 | // module.setSavedDriveInactive(drive_inactive.isChecked()); | |
299 | //} | |
300 | ||
301 | public void toggle_active_high(View view) { | |
302 | CheckBox active_high = (CheckBox)view; | |
d002e66d | 303 | |
1786d191 | 304 | module.setSavedActiveHigh(active_high.isChecked()); |
d002e66d MG |
305 | } |
306 | ||
fd8f8652 MG |
307 | @Override |
308 | public boolean onCreateOptionsMenu(Menu menu) { | |
309 | MenuInflater inflater = getMenuInflater(); | |
310 | inflater.inflate(R.menu.main, menu); | |
311 | return true; | |
312 | } | |
313 | ||
314 | @Override | |
315 | public boolean onOptionsItemSelected(MenuItem item) { | |
316 | switch (item.getItemId()) { | |
317 | case R.id.about: | |
318 | showDialog(42); | |
319 | return true; | |
320 | default: | |
321 | return super.onOptionsItemSelected(item); | |
322 | } | |
323 | } | |
324 | ||
325 | protected Dialog onCreateDialog(int id) { | |
326 | Dialog dlg = null; | |
327 | ||
a7c1cd77 | 328 | switch(id) { |
0c4d887c MG |
329 | case 22: |
330 | AlertDialog.Builder noload = new AlertDialog.Builder(this); | |
331 | noload.setMessage("Could not load/unload the module! Do you have a MS2/Droid2 with kernel 2.6.32?") | |
332 | .setCancelable(true) | |
333 | .setPositiveButton("Ok", new DialogInterface.OnClickListener() { | |
334 | public void onClick(DialogInterface dialog, int id) { | |
335 | dialog.cancel(); | |
336 | } | |
337 | }); | |
338 | dlg = noload.create(); | |
339 | break; | |
a7c1cd77 MG |
340 | case 23: |
341 | AlertDialog.Builder noroot = new AlertDialog.Builder(this); | |
0c4d887c | 342 | noroot.setMessage("Could not get root access! Is this device rooted and have you granted Superuser privileges?") |
a7c1cd77 MG |
343 | .setCancelable(true) |
344 | .setPositiveButton("Ok", new DialogInterface.OnClickListener() { | |
345 | public void onClick(DialogInterface dialog, int id) { | |
346 | dialog.cancel(); | |
347 | } | |
348 | }); | |
349 | dlg = noroot.create(); | |
350 | break; | |
0c4d887c MG |
351 | case 24: |
352 | AlertDialog.Builder shellexec = new AlertDialog.Builder(this); | |
6bc2dff5 | 353 | shellexec.setMessage("Problems executing shell commands as root! Is this device rooted, are insmod/rmmod binaries available and do you have a MS2/Droid2 with kernel 2.6.32 (or matching debounce-module for a custom kernel)?") |
0c4d887c MG |
354 | .setCancelable(true) |
355 | .setPositiveButton("Ok", new DialogInterface.OnClickListener() { | |
356 | public void onClick(DialogInterface dialog, int id) { | |
357 | dialog.cancel(); | |
358 | } | |
359 | }); | |
360 | dlg = shellexec.create(); | |
361 | break; | |
a7c1cd77 | 362 | case 42: |
2f680ae8 MG |
363 | String version; |
364 | ||
365 | try { | |
366 | version = getPackageManager().getPackageInfo(getPackageName(), 0).versionName; | |
367 | } catch (Exception e) { | |
368 | version = "?"; | |
369 | } | |
370 | ||
a7c1cd77 | 371 | AlertDialog.Builder about = new AlertDialog.Builder(this); |
2f680ae8 | 372 | about.setMessage("Milestone 2 Debounce " + version + "\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") |
a7c1cd77 MG |
373 | .setCancelable(true) |
374 | .setPositiveButton("Ok", new DialogInterface.OnClickListener() { | |
375 | public void onClick(DialogInterface dialog, int id) { | |
376 | dialog.cancel(); | |
377 | } | |
378 | }); | |
379 | dlg = about.create(); | |
380 | break; | |
381 | default: | |
382 | dlg = null; | |
383 | break; | |
384 | } | |
fd8f8652 MG |
385 | |
386 | return dlg; | |
387 | } | |
7bcde8d7 | 388 | } |