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