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