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