]>
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 | ||
e75481cd MG |
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 | ||
dea0f4b0 MG |
29 | public MS2Debounce() |
30 | { | |
31 | super(); | |
32 | module = new DebounceModuleHelper(this); | |
33 | } | |
34 | ||
226a7d4d MG |
35 | @Override |
36 | public void onCreate(Bundle savedInstanceState) | |
37 | { | |
38 | super.onCreate(savedInstanceState); | |
ae569a50 | 39 | |
dea0f4b0 | 40 | setContentView(R.layout.main); |
1559225a MG |
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); | |
e75481cd | 50 | if (loaded && module.getSavedDelay() != debounce_delay) { |
1559225a MG |
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 | ||
dea0f4b0 MG |
67 | updateUI(); |
68 | } | |
69 | ||
70 | private void updateUI() { | |
dea0f4b0 MG |
71 | disableUI(); |
72 | ||
01b288b5 | 73 | // Calling these is expensive, so cache the result... |
e75481cd MG |
74 | loaded = module.isLoaded(); |
75 | safe_to_load = module.is_safe_to_load(); | |
76 | debounce_delay = module.getDelay(); | |
dea0f4b0 | 77 | |
c51c7009 | 78 | TextView text = (TextView)findViewById(R.id.text); |
01b288b5 | 79 | text.setText("Current status:\n\nModule loaded: " + loaded + "\ndebounce_delay: " + debounce_delay + "ms\nsafe_to_load: " + safe_to_load); |
dea0f4b0 | 80 | |
1559225a MG |
81 | EditText textDelay = (EditText)findViewById(R.id.debounce_delay); |
82 | textDelay.setText(Integer.toString(module.getSavedDelay())); | |
83 | textDelay.setEnabled(true); | |
c51c7009 | 84 | |
dea0f4b0 | 85 | Button reload = (Button)findViewById(R.id.reload); |
01b288b5 | 86 | if (loaded && module.getSavedDelay() != debounce_delay) { |
dea0f4b0 MG |
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 | } | |
226a7d4d | 105 | |
dea0f4b0 | 106 | CheckBox on_boot = (CheckBox)findViewById(R.id.on_boot); |
c3053460 | 107 | on_boot.setChecked(module.get_on_boot()); |
dea0f4b0 MG |
108 | if (safe_to_load) { |
109 | on_boot.setEnabled(true); | |
110 | } else { | |
111 | on_boot.setEnabled(false); | |
112 | } | |
113 | } | |
114 | ||
115 | private void disableUI() { | |
1559225a MG |
116 | EditText textDelay = (EditText)findViewById(R.id.debounce_delay); |
117 | textDelay.setEnabled(false); | |
118 | ||
dea0f4b0 MG |
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(); | |
226a7d4d MG |
134 | if (!module.isLoaded()) { |
135 | module.loadModule(); | |
136 | } | |
dea0f4b0 MG |
137 | updateUI(); |
138 | } | |
ae569a50 | 139 | |
dea0f4b0 MG |
140 | public void unloadModule(View view) { |
141 | disableUI(); | |
142 | if (module.isLoaded()) { | |
143 | module.unloadModule(); | |
144 | } | |
145 | updateUI(); | |
146 | } | |
ee6322a1 | 147 | |
dea0f4b0 MG |
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(); | |
226a7d4d | 157 | } |
fd8f8652 | 158 | |
c3053460 MG |
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 | ||
fd8f8652 MG |
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 | } | |
7bcde8d7 | 198 | } |