]> git.zerfleddert.de Git - ms2-fixes/blob - MS2Debounce/src/de/rmdir/ms2debounce/MS2Debounce.java
87b53694caa973170ae30300f1eba6aba7546395
[ms2-fixes] / MS2Debounce / src / de / rmdir / ms2debounce / MS2Debounce.java
1 package de.rmdir.ms2debounce;
2
3 import android.app.Activity;
4 import android.app.Dialog;
5 import android.app.AlertDialog;
6 import android.os.Bundle;
7 import android.content.Intent;
8 import android.content.DialogInterface;
9 import android.widget.TextView;
10 import android.widget.EditText;
11 import android.widget.Button;
12 import android.widget.CheckBox;
13 import android.view.View;
14 import android.view.Menu;
15 import android.view.MenuInflater;
16 import android.view.MenuItem;
17 import android.text.TextWatcher;
18 import android.text.Editable;
19
20 public class MS2Debounce extends Activity
21 {
22 private DebounceModuleHelper module;
23
24 // Calling these is expensive, so cache the result...
25 private boolean loaded;
26 private boolean safe_to_load;
27 private int debounce_delay;
28 private int settle_time;
29 private int poll_time;
30
31 public MS2Debounce()
32 {
33 super();
34 module = new DebounceModuleHelper(this);
35 }
36
37 @Override
38 public void onCreate(Bundle savedInstanceState)
39 {
40 super.onCreate(savedInstanceState);
41
42 setContentView(R.layout.main);
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()));
50 }
51 }
52
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()));
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
98 updateUI();
99 }
100
101 private void updateUI() {
102 disableUI();
103
104 // Calling these is expensive, so cache the result...
105 loaded = module.isLoaded();
106 safe_to_load = module.is_safe_to_load();
107 debounce_delay = module.getDelay();
108 settle_time = module.getSettle();
109 poll_time = module.getPoll();
110
111 TextView text = (TextView)findViewById(R.id.text);
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 + "ms\n" +
116 "safe_to_load: " + safe_to_load + " (module loaded by this app)");
117
118 EditText textDelay = (EditText)findViewById(R.id.debounce_delay);
119 textDelay.setText(Integer.toString(module.getSavedDelay()));
120 textDelay.setEnabled(true);
121
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);
133 } else {
134 set.setEnabled(false);
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 }
150
151 CheckBox on_boot = (CheckBox)findViewById(R.id.on_boot);
152 on_boot.setChecked(module.get_on_boot());
153 if (safe_to_load) {
154 on_boot.setEnabled(true);
155 } else {
156 on_boot.setEnabled(false);
157 }
158 }
159
160 private void disableUI() {
161 EditText textDelay = (EditText)findViewById(R.id.debounce_delay);
162 textDelay.setEnabled(false);
163
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);
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();
185 if (!module.isLoaded()) {
186 module.loadModule();
187 }
188 updateUI();
189 }
190
191 public void unloadModule(View view) {
192 disableUI();
193 if (module.isLoaded()) {
194 module.unloadModule();
195 }
196 updateUI();
197 }
198
199 public void setValues(View view) {
200 disableUI();
201 if (!module.isLoaded()) {
202 module.loadModule();
203 }
204 module.setAllValues();
205 updateUI();
206 }
207
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
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 }
247 }
Impressum, Datenschutz