1 package de
.rmdir
.ms2debounce
;
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
;
20 public class MS2Debounce
extends Activity
22 private DebounceModuleHelper module
;
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
;
34 module
= new DebounceModuleHelper(this);
38 public void onCreate(Bundle savedInstanceState
)
40 super.onCreate(savedInstanceState
);
42 setContentView(R
.layout
.main
);
44 EditText textDelay
= (EditText
)findViewById(R
.id
.debounce_delay
);
45 textDelay
.addTextChangedListener(new TextWatcher() {
47 public void afterTextChanged(Editable delay
) {
48 if (delay
.toString().length() > 0) {
49 module
.setSavedDelay(Integer
.parseInt(delay
.toString()));
54 public void beforeTextChanged(CharSequence s
, int start
, int count
, int after
) {
58 public void onTextChanged(CharSequence s
, int start
, int before
, int count
) {
62 EditText textSettle
= (EditText
)findViewById(R
.id
.settle_time
);
63 textSettle
.addTextChangedListener(new TextWatcher() {
65 public void afterTextChanged(Editable settle_time
) {
66 if (settle_time
.toString().length() > 0) {
67 module
.setSavedSettle(Integer
.parseInt(settle_time
.toString()));
72 public void beforeTextChanged(CharSequence s
, int start
, int count
, int after
) {
76 public void onTextChanged(CharSequence s
, int start
, int before
, int count
) {
80 EditText textPoll
= (EditText
)findViewById(R
.id
.poll_time
);
81 textPoll
.addTextChangedListener(new TextWatcher() {
83 public void afterTextChanged(Editable poll_time
) {
84 if (poll_time
.toString().length() > 0) {
85 module
.setSavedPoll(Integer
.parseInt(poll_time
.toString()));
90 public void beforeTextChanged(CharSequence s
, int start
, int count
, int after
) {
94 public void onTextChanged(CharSequence s
, int start
, int before
, int count
) {
101 private void updateUI() {
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();
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
+ "us\n" +
116 "safe_to_load: " + safe_to_load
+ " (module loaded by this app)");
118 EditText textDelay
= (EditText
)findViewById(R
.id
.debounce_delay
);
119 textDelay
.setText(Integer
.toString(module
.getSavedDelay()));
120 textDelay
.setEnabled(true);
122 EditText textSettle
= (EditText
)findViewById(R
.id
.settle_time
);
123 textSettle
.setText(Integer
.toString(module
.getSavedSettle()));
124 textSettle
.setEnabled(true);
126 EditText textPoll
= (EditText
)findViewById(R
.id
.poll_time
);
127 textPoll
.setText(Integer
.toString(module
.getSavedPoll()));
128 textPoll
.setEnabled(true);
130 Button set
= (Button
)findViewById(R
.id
.set
);
132 set
.setEnabled(true);
134 set
.setEnabled(false);
137 Button load
= (Button
)findViewById(R
.id
.load
);
139 load
.setEnabled(false);
141 load
.setEnabled(true);
144 Button unload
= (Button
)findViewById(R
.id
.unload
);
146 unload
.setEnabled(true);
148 unload
.setEnabled(false);
151 CheckBox on_boot
= (CheckBox
)findViewById(R
.id
.on_boot
);
152 on_boot
.setChecked(module
.get_on_boot());
154 on_boot
.setEnabled(true);
156 on_boot
.setEnabled(false);
160 private void disableUI() {
161 EditText textDelay
= (EditText
)findViewById(R
.id
.debounce_delay
);
162 textDelay
.setEnabled(false);
164 EditText textSettle
= (EditText
)findViewById(R
.id
.settle_time
);
165 textSettle
.setEnabled(false);
167 EditText textPoll
= (EditText
)findViewById(R
.id
.poll_time
);
168 textPoll
.setEnabled(false);
170 Button set
= (Button
)findViewById(R
.id
.set
);
171 set
.setEnabled(false);
173 Button load
= (Button
)findViewById(R
.id
.load
);
174 load
.setEnabled(false);
176 Button unload
= (Button
)findViewById(R
.id
.unload
);
177 unload
.setEnabled(false);
179 CheckBox on_boot
= (CheckBox
)findViewById(R
.id
.on_boot
);
180 on_boot
.setEnabled(false);
183 public void loadModule(View view
) {
185 if (!module
.isLoaded()) {
191 public void unloadModule(View view
) {
193 if (module
.isLoaded()) {
194 module
.unloadModule();
199 public void setValues(View view
) {
201 if (!module
.isLoaded()) {
204 module
.setAllValues();
208 public void toggle_on_boot(View view
) {
209 CheckBox on_boot
= (CheckBox
)view
;
211 module
.set_on_boot(on_boot
.isChecked());
215 public boolean onCreateOptionsMenu(Menu menu
) {
216 MenuInflater inflater
= getMenuInflater();
217 inflater
.inflate(R
.menu
.main
, menu
);
222 public boolean onOptionsItemSelected(MenuItem item
) {
223 switch (item
.getItemId()) {
228 return super.onOptionsItemSelected(item
);
232 protected Dialog
onCreateDialog(int id
) {
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")
238 .setPositiveButton("Ok", new DialogInterface
.OnClickListener() {
239 public void onClick(DialogInterface dialog
, int id
) {
243 dlg
= about
.create();