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