]>
Commit | Line | Data |
---|---|---|
1 | package de.rmdir.ms2debounce; | |
2 | ||
3 | import java.io.InputStream; | |
4 | import java.io.OutputStream; | |
5 | import java.io.File; | |
6 | import java.io.FileReader; | |
7 | import java.io.FileWriter; | |
8 | import java.io.BufferedReader; | |
9 | import java.io.BufferedWriter; | |
10 | import java.io.DataOutputStream; | |
11 | ||
12 | import android.content.Context; | |
13 | import android.content.SharedPreferences; | |
14 | import android.util.Log; | |
15 | ||
16 | public class DebounceModuleHelper | |
17 | { | |
18 | private Context ctx; | |
19 | public static final String PREFS_NAME = "DebounceCfg"; | |
20 | final int SUPERUSER_REQUEST = 4223; | |
21 | ||
22 | private static final String TAG = "DebounceModuleHelper"; | |
23 | ||
24 | public DebounceModuleHelper(Context context) { | |
25 | ctx = context; | |
26 | } | |
27 | ||
28 | public void setAllValues() { | |
29 | setDelay(getSavedDelay()); | |
30 | setSettle(getSavedSettle()); | |
31 | setPoll(getSavedPoll()); | |
32 | setHwDebounce(getSavedHwDebounce()); | |
33 | setHwDebounceTime(getSavedHwDebounceTime()); | |
34 | //setDriveInactive(getSavedDriveInactive()); | |
35 | setActiveHigh(getSavedActiveHigh()); | |
36 | } | |
37 | ||
38 | public boolean loadModule() throws NotRootedException,ShellException { | |
39 | if (!_loadModule()) | |
40 | return false; | |
41 | ||
42 | setAllValues(); | |
43 | ||
44 | return true; | |
45 | } | |
46 | ||
47 | protected void runAsRoot(String command) throws NotRootedException,ShellException { | |
48 | Process rootcmd; | |
49 | ||
50 | Log.i(TAG, "Running as root: " + command); | |
51 | try { | |
52 | rootcmd = Runtime.getRuntime().exec(new String[]{"su","-c","sh"}); | |
53 | } catch (java.io.IOException e) { | |
54 | Log.e(TAG, "Got IOException: " + e.getMessage() + " (" + e.getCause() + ")"); | |
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) { | |
65 | Log.e(TAG, "Got IOException: " + e.getMessage() + " (" + e.getCause() + ")"); | |
66 | throw new ShellException(); | |
67 | } | |
68 | ||
69 | try { | |
70 | int r = rootcmd.waitFor(); | |
71 | ||
72 | if (r != 0) { | |
73 | Log.e(TAG, "Process returned: " + r); | |
74 | throw new ShellException(); | |
75 | } | |
76 | } catch (java.lang.InterruptedException e) { | |
77 | Log.e(TAG, "Got InterruptedException: " + e.getMessage() + " (" + e.getCause() + ")"); | |
78 | throw new ShellException(); | |
79 | } | |
80 | ||
81 | Log.i(TAG, "Process executed successfully"); | |
82 | } | |
83 | ||
84 | public synchronized boolean _loadModule() throws NotRootedException,ShellException { | |
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"); | |
96 | ||
97 | extractModule(); | |
98 | } | |
99 | ||
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 | } | |
106 | ||
107 | runAsRoot(insmod + " " + debounce_ko); | |
108 | ||
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 | ||
119 | if (!isLoaded()) { | |
120 | return false; | |
121 | } | |
122 | ||
123 | if (getDelay() < 0) { | |
124 | return false; | |
125 | } | |
126 | ||
127 | /* Module was obviously loaded, so it is safe to load on boot */ | |
128 | editor.putBoolean("safe_to_load", true); | |
129 | editor.commit(); | |
130 | ||
131 | return true; | |
132 | } | |
133 | ||
134 | public synchronized void unloadModule() throws NotRootedException,ShellException { | |
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"); | |
145 | } | |
146 | ||
147 | public synchronized boolean isLoaded() { | |
148 | boolean loaded = false; | |
149 | ||
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)) { | |
158 | File sysdir = new File("/sys/devices/debounce"); | |
159 | if (sysdir.exists() && sysdir.isDirectory()) { | |
160 | loaded = true; | |
161 | break; | |
162 | } | |
163 | } | |
164 | } | |
165 | } catch (Exception e) { | |
166 | loaded = false; | |
167 | } | |
168 | ||
169 | return loaded; | |
170 | } | |
171 | ||
172 | private synchronized int getValue(String parameter) { | |
173 | int value = -1; | |
174 | ||
175 | try { | |
176 | String read; | |
177 | ||
178 | FileReader fr = new FileReader("/sys/devices/debounce/" + parameter); | |
179 | BufferedReader fbuf = new BufferedReader(fr); | |
180 | ||
181 | read = fbuf.readLine(); | |
182 | if (read != null) { | |
183 | value = Integer.parseInt(read.trim()); | |
184 | } | |
185 | ||
186 | fbuf.close(); | |
187 | } catch (Exception e) {} | |
188 | ||
189 | return value; | |
190 | } | |
191 | ||
192 | private synchronized void setValue(String parameter, int value) { | |
193 | if (!isLoaded()) { | |
194 | return; | |
195 | } | |
196 | ||
197 | try { | |
198 | FileWriter fw = new FileWriter("/sys/devices/debounce/" + parameter); | |
199 | BufferedWriter fbuf = new BufferedWriter(fw); | |
200 | ||
201 | fbuf.write((new Integer(value)).toString()); | |
202 | ||
203 | fbuf.close(); | |
204 | } catch (Exception e) {} | |
205 | } | |
206 | ||
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 | ||
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 | ||
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 | ||
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 | ||
281 | public synchronized int getSavedDelay() { | |
282 | SharedPreferences settings = ctx.getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE); | |
283 | ||
284 | return settings.getInt("debounce_delay", 15); | |
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 | ||
295 | public synchronized int getSavedSettle() { | |
296 | SharedPreferences settings = ctx.getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE); | |
297 | ||
298 | return settings.getInt("settle_time", 40); | |
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 | ||
312 | return settings.getInt("poll_time", 20); | |
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 | ||
323 | public synchronized boolean getSavedHwDebounce() { | |
324 | SharedPreferences settings = ctx.getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE); | |
325 | ||
326 | return settings.getBoolean("hw_debounce", false); | |
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 | ||
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 | ||
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 | ||
379 | public synchronized boolean is_safe_to_load() { | |
380 | SharedPreferences settings = ctx.getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE); | |
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 | } | |
388 | ||
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(); | |
395 | } | |
396 | ||
397 | private synchronized void extractModule() { | |
398 | File debounce_ko = new File(ctx.getFilesDir() + "/debounce.ko"); | |
399 | ||
400 | if (debounce_ko.exists()) { | |
401 | return; | |
402 | } | |
403 | ||
404 | try { | |
405 | InputStream apk = ctx.getAssets().open("debounce.ko"); | |
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 | } | |
415 | ||
416 | apk.close(); | |
417 | mod.close(); | |
418 | ||
419 | File tmpfile = new File(debounce_ko + ".tmp"); | |
420 | tmpfile.renameTo(debounce_ko); | |
421 | } catch (Exception e) {} | |
422 | } | |
423 | } |