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