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