]>
Commit | Line | Data |
---|---|---|
0ae502f6 MG |
1 | package de.rmdir.ms2debounce; |
2 | ||
5738a32f MG |
3 | import java.io.InputStream; |
4 | import java.io.OutputStream; | |
226a7d4d | 5 | import java.io.File; |
d82ae589 | 6 | import java.io.FileReader; |
0b9d6422 | 7 | import java.io.FileWriter; |
d82ae589 | 8 | import java.io.BufferedReader; |
0b9d6422 | 9 | import java.io.BufferedWriter; |
6bb245ae | 10 | import java.io.DataOutputStream; |
226a7d4d | 11 | |
5738a32f | 12 | import android.content.Context; |
08fec0be | 13 | import android.content.SharedPreferences; |
5738a32f | 14 | |
0ae502f6 MG |
15 | public class DebounceModuleHelper |
16 | { | |
5738a32f | 17 | private Context ctx; |
08fec0be | 18 | public static final String PREFS_NAME = "DebounceCfg"; |
6bb245ae | 19 | final int SUPERUSER_REQUEST = 4223; |
226a7d4d | 20 | |
5738a32f MG |
21 | public DebounceModuleHelper(Context context) { |
22 | ctx = context; | |
226a7d4d MG |
23 | } |
24 | ||
381027a8 | 25 | public void setAllValues() { |
0b9d6422 | 26 | setDelay(getSavedDelay()); |
75fbc6ef MG |
27 | setSettle(getSavedSettle()); |
28 | setPoll(getSavedPoll()); | |
2bb83a0e MG |
29 | setHwDebounce(getSavedHwDebounce()); |
30 | setHwDebounceTime(getSavedHwDebounceTime()); | |
1786d191 MG |
31 | //setDriveInactive(getSavedDriveInactive()); |
32 | setActiveHigh(getSavedActiveHigh()); | |
40697a47 MG |
33 | } |
34 | ||
381027a8 MG |
35 | public void loadModule() { |
36 | _loadModule(); | |
37 | setAllValues(); | |
38 | } | |
39 | ||
6bb245ae MG |
40 | protected void runAsRoot(String command) throws java.io.IOException,java.lang.InterruptedException { |
41 | Process rootcmd = Runtime.getRuntime().exec(new String[]{"su","-c","sh"}); | |
42 | DataOutputStream sh = new DataOutputStream(rootcmd.getOutputStream()); | |
43 | sh.writeBytes(command + "\n"); | |
44 | sh.writeBytes("exit\n"); | |
45 | sh.flush(); | |
46 | sh.close(); | |
47 | ||
48 | rootcmd.waitFor(); | |
49 | } | |
50 | ||
0b9d6422 | 51 | public synchronized void _loadModule() { |
40697a47 MG |
52 | File debounce_ko = new File(ctx.getFilesDir() + "/debounce.ko"); |
53 | ||
226a7d4d MG |
54 | extractModule(); |
55 | ||
d3e7b10c MG |
56 | SharedPreferences settings = ctx.getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE); |
57 | SharedPreferences.Editor editor = settings.edit(); | |
58 | if (is_safe_to_load()) { | |
59 | editor.putBoolean("safe_to_load", false); | |
60 | editor.commit(); | |
61 | } | |
40697a47 | 62 | |
0ae502f6 | 63 | try { |
0b9d6422 | 64 | runAsRoot("/system/bin/insmod " + debounce_ko); |
08fec0be MG |
65 | } catch (Exception e) { |
66 | return; | |
a6cf1017 MG |
67 | } |
68 | ||
69 | if (!isLoaded()) { | |
70 | return; | |
08fec0be | 71 | } |
dea0f4b0 | 72 | |
818fb327 | 73 | if (getDelay() < 0) { |
08fec0be MG |
74 | return; |
75 | } | |
76 | ||
77 | /* Module was obviously loaded, so it is safe to load on boot */ | |
d3e7b10c MG |
78 | editor.putBoolean("safe_to_load", true); |
79 | editor.commit(); | |
0ae502f6 MG |
80 | } |
81 | ||
08fec0be | 82 | public synchronized void unloadModule() { |
40697a47 | 83 | try { |
6bb245ae | 84 | runAsRoot("/system/bin/rmmod debounce"); |
40697a47 MG |
85 | } catch (Exception e) {} |
86 | } | |
87 | ||
08fec0be | 88 | public synchronized boolean isLoaded() { |
d82ae589 MG |
89 | boolean loaded = false; |
90 | try { | |
91 | String read; | |
92 | ||
93 | FileReader modules = new FileReader("/proc/modules"); | |
94 | BufferedReader modules_buf = new BufferedReader(modules); | |
95 | ||
96 | while((read = modules_buf.readLine()) != null) { | |
97 | if (read.regionMatches(0, "debounce", 0, 8)) { | |
98 | loaded = true; | |
99 | } | |
100 | } | |
101 | ||
102 | } catch (Exception e) { | |
103 | loaded = false; | |
104 | } | |
105 | ||
106 | return loaded; | |
0ae502f6 | 107 | } |
226a7d4d | 108 | |
75fbc6ef MG |
109 | private synchronized int getValue(String parameter) { |
110 | int value = -1; | |
d82ae589 MG |
111 | |
112 | try { | |
113 | String read; | |
114 | ||
75fbc6ef MG |
115 | FileReader fr = new FileReader("/sys/devices/debounce/" + parameter); |
116 | BufferedReader fbuf = new BufferedReader(fr); | |
d82ae589 | 117 | |
75fbc6ef | 118 | read = fbuf.readLine(); |
d82ae589 | 119 | if (read != null) { |
75fbc6ef | 120 | value = Integer.parseInt(read.trim()); |
d82ae589 | 121 | } |
0b9d6422 | 122 | |
75fbc6ef | 123 | fbuf.close(); |
d82ae589 MG |
124 | } catch (Exception e) {} |
125 | ||
75fbc6ef | 126 | return value; |
ee6322a1 MG |
127 | } |
128 | ||
75fbc6ef | 129 | private synchronized void setValue(String parameter, int value) { |
0b9d6422 MG |
130 | if (!isLoaded()) { |
131 | return; | |
376c6ac7 MG |
132 | } |
133 | ||
0b9d6422 | 134 | try { |
75fbc6ef MG |
135 | FileWriter fw = new FileWriter("/sys/devices/debounce/" + parameter); |
136 | BufferedWriter fbuf = new BufferedWriter(fw); | |
0b9d6422 | 137 | |
75fbc6ef | 138 | fbuf.write((new Integer(value)).toString()); |
0b9d6422 | 139 | |
75fbc6ef | 140 | fbuf.close(); |
0b9d6422 | 141 | } catch (Exception e) {} |
376c6ac7 MG |
142 | } |
143 | ||
75fbc6ef MG |
144 | public synchronized int getDelay() { |
145 | return getValue("debounce_delay"); | |
146 | } | |
147 | ||
148 | public synchronized void setDelay(int debounce_delay) { | |
149 | setValue("debounce_delay", debounce_delay); | |
150 | } | |
151 | ||
152 | public synchronized int getSettle() { | |
153 | return getValue("settle_time"); | |
154 | } | |
155 | ||
156 | public synchronized void setSettle(int settle_time) { | |
157 | setValue("settle_time", settle_time); | |
158 | } | |
159 | ||
160 | public synchronized int getPoll() { | |
161 | return getValue("poll_time"); | |
162 | } | |
163 | ||
164 | public synchronized void setPoll(int poll_time) { | |
165 | setValue("poll_time", poll_time); | |
166 | } | |
167 | ||
2bb83a0e MG |
168 | public synchronized boolean getHwDebounce() { |
169 | if (getValue("hw_debounce") == 1) | |
170 | return true; | |
171 | ||
172 | return false; | |
173 | } | |
174 | ||
175 | public synchronized void setHwDebounce(boolean enable) { | |
176 | if (enable) | |
177 | setValue("hw_debounce", 1); | |
178 | else | |
179 | setValue("hw_debounce", 0); | |
180 | } | |
181 | ||
182 | public synchronized int getHwDebounceTime() { | |
183 | return getValue("hw_debounce_time"); | |
184 | } | |
185 | ||
186 | public synchronized void setHwDebounceTime(int time) { | |
187 | setValue("hw_debounce_time", time); | |
188 | } | |
189 | ||
d002e66d MG |
190 | public synchronized boolean getDriveInactive() { |
191 | if (getValue("drive_inactive_flag") == 1) | |
192 | return true; | |
193 | ||
194 | return false; | |
195 | } | |
196 | ||
197 | public synchronized void setDriveInactive(boolean enable) { | |
198 | if (enable) | |
199 | setValue("drive_inactive_flag", 1); | |
200 | else | |
201 | setValue("drive_inactive_flag", 0); | |
202 | } | |
203 | ||
1786d191 MG |
204 | public synchronized boolean getActiveHigh() { |
205 | if (getValue("active_high_flag") == 1) | |
206 | return true; | |
207 | ||
208 | return false; | |
209 | } | |
210 | ||
211 | public synchronized void setActiveHigh(boolean enable) { | |
212 | if (enable) | |
213 | setValue("active_high_flag", 1); | |
214 | else | |
215 | setValue("active_high_flag", 0); | |
216 | } | |
217 | ||
dea0f4b0 MG |
218 | public synchronized int getSavedDelay() { |
219 | SharedPreferences settings = ctx.getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE); | |
220 | ||
131fed25 | 221 | return settings.getInt("debounce_delay", 15); |
dea0f4b0 MG |
222 | } |
223 | ||
224 | public synchronized void setSavedDelay(int delay) { | |
225 | SharedPreferences settings = ctx.getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE); | |
226 | SharedPreferences.Editor editor = settings.edit(); | |
227 | ||
228 | editor.putInt("debounce_delay", delay); | |
229 | editor.commit(); | |
230 | } | |
231 | ||
75fbc6ef MG |
232 | public synchronized int getSavedSettle() { |
233 | SharedPreferences settings = ctx.getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE); | |
234 | ||
ceccb7e2 | 235 | return settings.getInt("settle_time", 40); |
75fbc6ef MG |
236 | } |
237 | ||
238 | public synchronized void setSavedSettle(int settle) { | |
239 | SharedPreferences settings = ctx.getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE); | |
240 | SharedPreferences.Editor editor = settings.edit(); | |
241 | ||
242 | editor.putInt("settle_time", settle); | |
243 | editor.commit(); | |
244 | } | |
245 | ||
246 | public synchronized int getSavedPoll() { | |
247 | SharedPreferences settings = ctx.getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE); | |
248 | ||
ceccb7e2 | 249 | return settings.getInt("poll_time", 20); |
75fbc6ef MG |
250 | } |
251 | ||
252 | public synchronized void setSavedPoll(int poll) { | |
253 | SharedPreferences settings = ctx.getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE); | |
254 | SharedPreferences.Editor editor = settings.edit(); | |
255 | ||
256 | editor.putInt("poll_time", poll); | |
257 | editor.commit(); | |
258 | } | |
259 | ||
2bb83a0e MG |
260 | public synchronized boolean getSavedHwDebounce() { |
261 | SharedPreferences settings = ctx.getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE); | |
262 | ||
d002e66d | 263 | return settings.getBoolean("hw_debounce", false); |
2bb83a0e MG |
264 | } |
265 | ||
266 | public synchronized void setSavedHwDebounce(boolean enable) { | |
267 | SharedPreferences settings = ctx.getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE); | |
268 | SharedPreferences.Editor editor = settings.edit(); | |
269 | ||
270 | editor.putBoolean("hw_debounce", enable); | |
271 | editor.commit(); | |
272 | } | |
273 | ||
274 | public synchronized int getSavedHwDebounceTime() { | |
275 | SharedPreferences settings = ctx.getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE); | |
276 | ||
277 | return settings.getInt("hw_debounce_time", 1); | |
278 | } | |
279 | ||
280 | public synchronized void setSavedHwDebounceTime(int time) { | |
281 | SharedPreferences settings = ctx.getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE); | |
282 | SharedPreferences.Editor editor = settings.edit(); | |
283 | ||
284 | editor.putInt("hw_debounce_time", time); | |
285 | editor.commit(); | |
286 | } | |
287 | ||
d002e66d MG |
288 | public synchronized boolean getSavedDriveInactive() { |
289 | SharedPreferences settings = ctx.getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE); | |
290 | ||
291 | return settings.getBoolean("drive_inactive", false); | |
292 | } | |
293 | ||
294 | public synchronized void setSavedDriveInactive(boolean enable) { | |
295 | SharedPreferences settings = ctx.getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE); | |
296 | SharedPreferences.Editor editor = settings.edit(); | |
297 | ||
298 | editor.putBoolean("drive_inactive", enable); | |
299 | editor.commit(); | |
300 | } | |
301 | ||
1786d191 MG |
302 | public synchronized boolean getSavedActiveHigh() { |
303 | SharedPreferences settings = ctx.getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE); | |
304 | ||
305 | return settings.getBoolean("active_high", false); | |
306 | } | |
307 | ||
308 | public synchronized void setSavedActiveHigh(boolean enable) { | |
309 | SharedPreferences settings = ctx.getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE); | |
310 | SharedPreferences.Editor editor = settings.edit(); | |
311 | ||
312 | editor.putBoolean("active_high", enable); | |
313 | editor.commit(); | |
314 | } | |
315 | ||
08fec0be MG |
316 | public synchronized boolean is_safe_to_load() { |
317 | SharedPreferences settings = ctx.getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE); | |
c3053460 MG |
318 | return settings.getBoolean("safe_to_load", false); |
319 | } | |
320 | ||
321 | public synchronized boolean get_on_boot() { | |
322 | SharedPreferences settings = ctx.getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE); | |
323 | return settings.getBoolean("on_boot", false); | |
324 | } | |
08fec0be | 325 | |
c3053460 MG |
326 | public synchronized void set_on_boot(boolean on_boot) { |
327 | SharedPreferences settings = ctx.getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE); | |
328 | SharedPreferences.Editor editor = settings.edit(); | |
329 | ||
330 | editor.putBoolean("on_boot", on_boot); | |
331 | editor.commit(); | |
08fec0be MG |
332 | } |
333 | ||
5738a32f MG |
334 | private synchronized void extractModule() { |
335 | File debounce_ko = new File(ctx.getFilesDir() + "/debounce.ko"); | |
226a7d4d MG |
336 | |
337 | if (debounce_ko.exists()) { | |
338 | return; | |
339 | } | |
5738a32f MG |
340 | |
341 | try { | |
342 | InputStream apk = ctx.getAssets().open("debounce.ko"); | |
40697a47 MG |
343 | OutputStream mod = ctx.openFileOutput("debounce.ko.tmp", 0); |
344 | ||
345 | //I assume a page is 4k... | |
346 | byte buf[] = new byte[4096]; | |
347 | int bytes; | |
348 | ||
349 | while((bytes = apk.read(buf)) != -1) { | |
350 | mod.write(buf, 0, bytes); | |
351 | } | |
5738a32f MG |
352 | |
353 | apk.close(); | |
354 | mod.close(); | |
40697a47 MG |
355 | |
356 | File tmpfile = new File(debounce_ko + ".tmp"); | |
357 | tmpfile.renameTo(debounce_ko); | |
5738a32f | 358 | } catch (Exception e) {} |
226a7d4d | 359 | } |
0ae502f6 | 360 | } |