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