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