]> git.zerfleddert.de Git - ms2-fixes/blob - MS2Debounce/src/de/rmdir/ms2debounce/MS2Debounce.java
2771ef2a11669ed6149497e2975cb2910cd56d78
[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 public MS2Debounce()
25 {
26 super();
27 module = new DebounceModuleHelper(this);
28 }
29
30 @Override
31 public void onCreate(Bundle savedInstanceState)
32 {
33 super.onCreate(savedInstanceState);
34
35 setContentView(R.layout.main);
36
37 EditText textDelay = (EditText)findViewById(R.id.debounce_delay);
38 textDelay.addTextChangedListener(new TextWatcher() {
39 @Override
40 public void afterTextChanged(Editable delay) {
41 if (delay.toString().length() > 0) {
42 module.setSavedDelay(Integer.parseInt(delay.toString()));
43
44 Button reload = (Button)findViewById(R.id.reload);
45 if (module.isLoaded() && module.getSavedDelay() != module.getDelay()) {
46 reload.setEnabled(true);
47 } else {
48 reload.setEnabled(false);
49 }
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 updateUI();
63 }
64
65 private void updateUI() {
66 disableUI();
67
68 // Calling these is expensive, so cache the result...
69 boolean loaded = module.isLoaded();
70 boolean safe_to_load = module.is_safe_to_load();
71 int debounce_delay = module.getDelay();
72
73 TextView text = (TextView)findViewById(R.id.text);
74 text.setText("Current status:\n\nModule loaded: " + loaded + "\ndebounce_delay: " + debounce_delay + "ms\nsafe_to_load: " + safe_to_load);
75
76 EditText textDelay = (EditText)findViewById(R.id.debounce_delay);
77 textDelay.setText(Integer.toString(module.getSavedDelay()));
78 textDelay.setEnabled(true);
79
80 Button reload = (Button)findViewById(R.id.reload);
81 if (loaded && module.getSavedDelay() != debounce_delay) {
82 reload.setEnabled(true);
83 } else {
84 reload.setEnabled(false);
85 }
86
87 Button load = (Button)findViewById(R.id.load);
88 if (loaded) {
89 load.setEnabled(false);
90 } else {
91 load.setEnabled(true);
92 }
93
94 Button unload = (Button)findViewById(R.id.unload);
95 if (loaded) {
96 unload.setEnabled(true);
97 } else {
98 unload.setEnabled(false);
99 }
100
101 CheckBox on_boot = (CheckBox)findViewById(R.id.on_boot);
102 on_boot.setChecked(module.get_on_boot());
103 if (safe_to_load) {
104 on_boot.setEnabled(true);
105 } else {
106 on_boot.setEnabled(false);
107 }
108 }
109
110 private void disableUI() {
111 EditText textDelay = (EditText)findViewById(R.id.debounce_delay);
112 textDelay.setEnabled(false);
113
114 Button reload = (Button)findViewById(R.id.reload);
115 reload.setEnabled(false);
116
117 Button load = (Button)findViewById(R.id.load);
118 load.setEnabled(false);
119
120 Button unload = (Button)findViewById(R.id.unload);
121 unload.setEnabled(false);
122
123 CheckBox on_boot = (CheckBox)findViewById(R.id.on_boot);
124 on_boot.setEnabled(false);
125 }
126
127 public void loadModule(View view) {
128 disableUI();
129 if (!module.isLoaded()) {
130 module.loadModule();
131 }
132 updateUI();
133 }
134
135 public void unloadModule(View view) {
136 disableUI();
137 if (module.isLoaded()) {
138 module.unloadModule();
139 }
140 updateUI();
141 }
142
143 public void reloadModule(View view) {
144 disableUI();
145 if (module.isLoaded()) {
146 module.unloadModule();
147 }
148 if (!module.isLoaded()) {
149 module.loadModule();
150 }
151 updateUI();
152 }
153
154 public void toggle_on_boot(View view) {
155 CheckBox on_boot = (CheckBox)view;
156
157 module.set_on_boot(on_boot.isChecked());
158 }
159
160 @Override
161 public boolean onCreateOptionsMenu(Menu menu) {
162 MenuInflater inflater = getMenuInflater();
163 inflater.inflate(R.menu.main, menu);
164 return true;
165 }
166
167 @Override
168 public boolean onOptionsItemSelected(MenuItem item) {
169 switch (item.getItemId()) {
170 case R.id.about:
171 showDialog(42);
172 return true;
173 default:
174 return super.onOptionsItemSelected(item);
175 }
176 }
177
178 protected Dialog onCreateDialog(int id) {
179 Dialog dlg = null;
180
181 AlertDialog.Builder about = new AlertDialog.Builder(this);
182 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")
183 .setCancelable(true)
184 .setPositiveButton("Ok", new DialogInterface.OnClickListener() {
185 public void onClick(DialogInterface dialog, int id) {
186 dialog.cancel();
187 }
188 });
189 dlg = about.create();
190
191 return dlg;
192 }
193 }
Impressum, Datenschutz