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