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