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
;
32 module
= new DebounceModuleHelper(this);
36 public void onCreate(Bundle savedInstanceState
)
38 super.onCreate(savedInstanceState
);
40 setContentView(R
.layout
.main
);
42 EditText textDelay
= (EditText
)findViewById(R
.id
.debounce_delay
);
43 textDelay
.addTextChangedListener(new TextWatcher() {
45 public void afterTextChanged(Editable delay
) {
46 if (delay
.toString().length() > 0) {
47 module
.setSavedDelay(Integer
.parseInt(delay
.toString()));
49 Button reload
= (Button
)findViewById(R
.id
.reload
);
50 if (loaded
&& module
.getSavedDelay() != debounce_delay
) {
51 reload
.setEnabled(true);
53 reload
.setEnabled(false);
59 public void beforeTextChanged(CharSequence s
, int start
, int count
, int after
) {
63 public void onTextChanged(CharSequence s
, int start
, int before
, int count
) {
70 private void updateUI() {
73 // Calling these is expensive, so cache the result...
74 loaded
= module
.isLoaded();
75 safe_to_load
= module
.is_safe_to_load();
76 debounce_delay
= module
.getDelay();
78 TextView text
= (TextView
)findViewById(R
.id
.text
);
79 text
.setText("Current status:\n\nModule loaded: " + loaded
+ "\ndebounce_delay: " + debounce_delay
+ "ms\nsafe_to_load: " + safe_to_load
);
81 EditText textDelay
= (EditText
)findViewById(R
.id
.debounce_delay
);
82 textDelay
.setText(Integer
.toString(module
.getSavedDelay()));
83 textDelay
.setEnabled(true);
85 Button reload
= (Button
)findViewById(R
.id
.reload
);
86 if (loaded
&& module
.getSavedDelay() != debounce_delay
) {
87 reload
.setEnabled(true);
89 reload
.setEnabled(false);
92 Button load
= (Button
)findViewById(R
.id
.load
);
94 load
.setEnabled(false);
96 load
.setEnabled(true);
99 Button unload
= (Button
)findViewById(R
.id
.unload
);
101 unload
.setEnabled(true);
103 unload
.setEnabled(false);
106 CheckBox on_boot
= (CheckBox
)findViewById(R
.id
.on_boot
);
107 on_boot
.setChecked(module
.get_on_boot());
109 on_boot
.setEnabled(true);
111 on_boot
.setEnabled(false);
115 private void disableUI() {
116 EditText textDelay
= (EditText
)findViewById(R
.id
.debounce_delay
);
117 textDelay
.setEnabled(false);
119 Button reload
= (Button
)findViewById(R
.id
.reload
);
120 reload
.setEnabled(false);
122 Button load
= (Button
)findViewById(R
.id
.load
);
123 load
.setEnabled(false);
125 Button unload
= (Button
)findViewById(R
.id
.unload
);
126 unload
.setEnabled(false);
128 CheckBox on_boot
= (CheckBox
)findViewById(R
.id
.on_boot
);
129 on_boot
.setEnabled(false);
132 public void loadModule(View view
) {
134 if (!module
.isLoaded()) {
140 public void unloadModule(View view
) {
142 if (module
.isLoaded()) {
143 module
.unloadModule();
148 public void reloadModule(View view
) {
150 if (module
.isLoaded()) {
151 module
.unloadModule();
153 if (!module
.isLoaded()) {
159 public void toggle_on_boot(View view
) {
160 CheckBox on_boot
= (CheckBox
)view
;
162 module
.set_on_boot(on_boot
.isChecked());
166 public boolean onCreateOptionsMenu(Menu menu
) {
167 MenuInflater inflater
= getMenuInflater();
168 inflater
.inflate(R
.menu
.main
, menu
);
173 public boolean onOptionsItemSelected(MenuItem item
) {
174 switch (item
.getItemId()) {
179 return super.onOptionsItemSelected(item
);
183 protected Dialog
onCreateDialog(int id
) {
186 AlertDialog
.Builder about
= new AlertDialog
.Builder(this);
187 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")
189 .setPositiveButton("Ok", new DialogInterface
.OnClickListener() {
190 public void onClick(DialogInterface dialog
, int id
) {
194 dlg
= about
.create();