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