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