]>
Commit | Line | Data |
---|---|---|
7bcde8d7 MG |
1 | package de.rmdir.ms2debounce; |
2 | ||
3 | import android.app.Activity; | |
fd8f8652 MG |
4 | import android.app.Dialog; |
5 | import android.app.AlertDialog; | |
7bcde8d7 | 6 | import android.os.Bundle; |
ae569a50 | 7 | import android.content.Intent; |
fd8f8652 | 8 | import android.content.DialogInterface; |
ee6322a1 | 9 | import android.widget.TextView; |
c51c7009 | 10 | import android.widget.EditText; |
dea0f4b0 MG |
11 | import android.widget.Button; |
12 | import android.widget.CheckBox; | |
13 | import android.view.View; | |
fd8f8652 MG |
14 | import android.view.Menu; |
15 | import android.view.MenuInflater; | |
16 | import android.view.MenuItem; | |
1559225a MG |
17 | import android.text.TextWatcher; |
18 | import android.text.Editable; | |
7bcde8d7 MG |
19 | |
20 | public class MS2Debounce extends Activity | |
21 | { | |
dea0f4b0 MG |
22 | private DebounceModuleHelper module; |
23 | ||
24 | public MS2Debounce() | |
25 | { | |
26 | super(); | |
27 | module = new DebounceModuleHelper(this); | |
28 | } | |
29 | ||
226a7d4d MG |
30 | @Override |
31 | public void onCreate(Bundle savedInstanceState) | |
32 | { | |
33 | super.onCreate(savedInstanceState); | |
ae569a50 | 34 | |
dea0f4b0 | 35 | setContentView(R.layout.main); |
1559225a MG |
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 | ||
dea0f4b0 MG |
62 | updateUI(); |
63 | } | |
64 | ||
65 | private void updateUI() { | |
dea0f4b0 MG |
66 | disableUI(); |
67 | ||
01b288b5 | 68 | // Calling these is expensive, so cache the result... |
dea0f4b0 MG |
69 | boolean loaded = module.isLoaded(); |
70 | boolean safe_to_load = module.is_safe_to_load(); | |
01b288b5 | 71 | int debounce_delay = module.getDelay(); |
dea0f4b0 | 72 | |
c51c7009 | 73 | TextView text = (TextView)findViewById(R.id.text); |
01b288b5 | 74 | text.setText("Current status:\n\nModule loaded: " + loaded + "\ndebounce_delay: " + debounce_delay + "ms\nsafe_to_load: " + safe_to_load); |
dea0f4b0 | 75 | |
1559225a MG |
76 | EditText textDelay = (EditText)findViewById(R.id.debounce_delay); |
77 | textDelay.setText(Integer.toString(module.getSavedDelay())); | |
78 | textDelay.setEnabled(true); | |
c51c7009 | 79 | |
dea0f4b0 | 80 | Button reload = (Button)findViewById(R.id.reload); |
01b288b5 | 81 | if (loaded && module.getSavedDelay() != debounce_delay) { |
dea0f4b0 MG |
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 | } | |
226a7d4d | 100 | |
dea0f4b0 | 101 | CheckBox on_boot = (CheckBox)findViewById(R.id.on_boot); |
c3053460 | 102 | on_boot.setChecked(module.get_on_boot()); |
dea0f4b0 MG |
103 | if (safe_to_load) { |
104 | on_boot.setEnabled(true); | |
105 | } else { | |
106 | on_boot.setEnabled(false); | |
107 | } | |
108 | } | |
109 | ||
110 | private void disableUI() { | |
1559225a MG |
111 | EditText textDelay = (EditText)findViewById(R.id.debounce_delay); |
112 | textDelay.setEnabled(false); | |
113 | ||
dea0f4b0 MG |
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(); | |
226a7d4d MG |
129 | if (!module.isLoaded()) { |
130 | module.loadModule(); | |
131 | } | |
dea0f4b0 MG |
132 | updateUI(); |
133 | } | |
ae569a50 | 134 | |
dea0f4b0 MG |
135 | public void unloadModule(View view) { |
136 | disableUI(); | |
137 | if (module.isLoaded()) { | |
138 | module.unloadModule(); | |
139 | } | |
140 | updateUI(); | |
141 | } | |
ee6322a1 | 142 | |
dea0f4b0 MG |
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(); | |
226a7d4d | 152 | } |
fd8f8652 | 153 | |
c3053460 MG |
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 | ||
fd8f8652 MG |
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 | } | |
7bcde8d7 | 193 | } |