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