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