]> cvs.zerfleddert.de Git - ms2-fixes/blob - MS2Debounce/src/de/rmdir/ms2debounce/DebounceModuleHelper.java
improve error message when module-loading fails
[ms2-fixes] / MS2Debounce / src / de / rmdir / ms2debounce / DebounceModuleHelper.java
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 import android.util.Log;
15
16 public class DebounceModuleHelper
17 {
18 private Context ctx;
19 public static final String PREFS_NAME = "DebounceCfg";
20 final int SUPERUSER_REQUEST = 4223;
21
22 private static final String TAG = "DebounceModuleHelper";
23
24 public DebounceModuleHelper(Context context) {
25 ctx = context;
26 }
27
28 public void setAllValues() {
29 setDelay(getSavedDelay());
30 setSettle(getSavedSettle());
31 setPoll(getSavedPoll());
32 setHwDebounce(getSavedHwDebounce());
33 setHwDebounceTime(getSavedHwDebounceTime());
34 //setDriveInactive(getSavedDriveInactive());
35 setActiveHigh(getSavedActiveHigh());
36 }
37
38 public boolean loadModule() throws NotRootedException,ShellException {
39 if (!_loadModule())
40 return false;
41
42 setAllValues();
43
44 return true;
45 }
46
47 protected void runAsRoot(String command) throws NotRootedException,ShellException {
48 Process rootcmd;
49
50 Log.i(TAG, "Running as root: " + command);
51 try {
52 rootcmd = Runtime.getRuntime().exec(new String[]{"su","-c","sh"});
53 } catch (java.io.IOException e) {
54 Log.e(TAG, "Got IOException: " + e.getMessage() + " (" + e.getCause() + ")");
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) {
65 Log.e(TAG, "Got IOException: " + e.getMessage() + " (" + e.getCause() + ")");
66 throw new ShellException();
67 }
68
69 try {
70 int r = rootcmd.waitFor();
71
72 if (r != 0) {
73 Log.e(TAG, "Process returned: " + r);
74 throw new ShellException();
75 }
76 } catch (java.lang.InterruptedException e) {
77 Log.e(TAG, "Got InterruptedException: " + e.getMessage() + " (" + e.getCause() + ")");
78 throw new ShellException();
79 }
80
81 Log.i(TAG, "Process executed successfully");
82 }
83
84 public synchronized boolean _loadModule() throws NotRootedException,ShellException {
85 Log.i(TAG, "Loading module");
86
87 if (isLoaded()) {
88 Log.i(TAG, "Module already loaded");
89 return true;
90 }
91
92 File insmod = new File("/system/bin/insmod");
93 if (!insmod.exists()) {
94 insmod = new File("/system/xbin/insmod");
95 if (!insmod.exists()) {
96 Log.e(TAG, "insmod not found");
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");
104
105 extractModule();
106 }
107
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 }
114
115 runAsRoot(insmod + " " + debounce_ko);
116
117 int cnt = 10;
118 while ((!isLoaded()) && (cnt > 0)) {
119 try {
120 Thread.sleep(100);
121 } catch (Exception e) {
122 Log.e(TAG, "Got Exception: " + e.getMessage() + " (" + e.getCause() + ")");
123 return false;
124 }
125 cnt--;
126 }
127
128 if (!isLoaded()) {
129 Log.e(TAG, "Module doesn't appear to be correctly loaded");
130 return false;
131 }
132
133 if (getDelay() < 0) {
134 return false;
135 }
136
137 /* Module was obviously loaded, so it is safe to load on boot */
138 editor.putBoolean("safe_to_load", true);
139 editor.commit();
140
141 Log.i(TAG, "Module loaded successfully");
142 return true;
143 }
144
145 public synchronized void unloadModule() throws NotRootedException,ShellException {
146 Log.i(TAG, "Unloading module");
147
148 if (!isLoaded()) {
149 Log.i(TAG, "Module not loaded");
150 return;
151 }
152
153 File rmmod = new File("/system/bin/rmmod");
154
155 if (!rmmod.exists()) {
156 rmmod = new File("/system/xbin/rmmod");
157 if (!rmmod.exists()) {
158 Log.e(TAG, "rmmod not found");
159 return;
160 }
161 }
162
163 runAsRoot(rmmod + " debounce");
164 }
165
166 public synchronized boolean isLoaded() {
167 boolean loaded = false;
168
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)) {
177 File sysdir = new File("/sys/devices/debounce");
178 if (sysdir.exists() && sysdir.isDirectory()) {
179 loaded = true;
180 break;
181 }
182 }
183 }
184 } catch (Exception e) {
185 loaded = false;
186 }
187
188 return loaded;
189 }
190
191 private synchronized int getValue(String parameter) {
192 int value = -1;
193
194 try {
195 String read;
196
197 FileReader fr = new FileReader("/sys/devices/debounce/" + parameter);
198 BufferedReader fbuf = new BufferedReader(fr);
199
200 read = fbuf.readLine();
201 if (read != null) {
202 value = Integer.parseInt(read.trim());
203 }
204
205 fbuf.close();
206 } catch (Exception e) {}
207
208 return value;
209 }
210
211 private synchronized void setValue(String parameter, int value) {
212 if (!isLoaded()) {
213 return;
214 }
215
216 try {
217 FileWriter fw = new FileWriter("/sys/devices/debounce/" + parameter);
218 BufferedWriter fbuf = new BufferedWriter(fw);
219
220 fbuf.write((new Integer(value)).toString());
221
222 fbuf.close();
223 } catch (Exception e) {}
224 }
225
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
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
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
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
300 public synchronized int getSavedDelay() {
301 SharedPreferences settings = ctx.getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE);
302
303 return settings.getInt("debounce_delay", 15);
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
314 public synchronized int getSavedSettle() {
315 SharedPreferences settings = ctx.getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE);
316
317 return settings.getInt("settle_time", 40);
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
331 return settings.getInt("poll_time", 20);
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
342 public synchronized boolean getSavedHwDebounce() {
343 SharedPreferences settings = ctx.getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE);
344
345 return settings.getBoolean("hw_debounce", false);
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
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
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
398 public synchronized boolean is_safe_to_load() {
399 SharedPreferences settings = ctx.getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE);
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 }
407
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();
414 }
415
416 private synchronized void extractModule() {
417 File debounce_ko = new File(ctx.getFilesDir() + "/debounce.ko");
418
419 if (debounce_ko.exists()) {
420 return;
421 }
422
423 try {
424 InputStream apk = ctx.getAssets().open("debounce.ko");
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 }
434
435 apk.close();
436 mod.close();
437
438 File tmpfile = new File(debounce_ko + ".tmp");
439 tmpfile.renameTo(debounce_ko);
440 } catch (Exception e) {}
441 }
442 }
Impressum, Datenschutz