]>
Commit | Line | Data |
---|---|---|
0ae502f6 MG |
1 | package de.rmdir.ms2debounce; |
2 | ||
5738a32f MG |
3 | import java.io.InputStream; |
4 | import java.io.OutputStream; | |
226a7d4d | 5 | import java.io.File; |
d82ae589 | 6 | import java.io.FileReader; |
0b9d6422 | 7 | import java.io.FileWriter; |
d82ae589 | 8 | import java.io.BufferedReader; |
0b9d6422 | 9 | import java.io.BufferedWriter; |
6bb245ae | 10 | import java.io.DataOutputStream; |
226a7d4d | 11 | |
5738a32f | 12 | import android.content.Context; |
08fec0be | 13 | import android.content.SharedPreferences; |
5738a32f | 14 | |
0ae502f6 MG |
15 | public class DebounceModuleHelper |
16 | { | |
5738a32f | 17 | private Context ctx; |
08fec0be | 18 | public static final String PREFS_NAME = "DebounceCfg"; |
6bb245ae | 19 | final int SUPERUSER_REQUEST = 4223; |
226a7d4d | 20 | |
5738a32f MG |
21 | public DebounceModuleHelper(Context context) { |
22 | ctx = context; | |
226a7d4d MG |
23 | } |
24 | ||
381027a8 | 25 | public void setAllValues() { |
0b9d6422 | 26 | setDelay(getSavedDelay()); |
75fbc6ef MG |
27 | setSettle(getSavedSettle()); |
28 | setPoll(getSavedPoll()); | |
2bb83a0e MG |
29 | setHwDebounce(getSavedHwDebounce()); |
30 | setHwDebounceTime(getSavedHwDebounceTime()); | |
40697a47 MG |
31 | } |
32 | ||
381027a8 MG |
33 | public void loadModule() { |
34 | _loadModule(); | |
35 | setAllValues(); | |
36 | } | |
37 | ||
6bb245ae MG |
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 | ||
0b9d6422 | 49 | public synchronized void _loadModule() { |
40697a47 MG |
50 | File debounce_ko = new File(ctx.getFilesDir() + "/debounce.ko"); |
51 | ||
226a7d4d MG |
52 | extractModule(); |
53 | ||
d3e7b10c MG |
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 | } | |
40697a47 | 60 | |
0ae502f6 | 61 | try { |
0b9d6422 | 62 | runAsRoot("/system/bin/insmod " + debounce_ko); |
08fec0be MG |
63 | } catch (Exception e) { |
64 | return; | |
a6cf1017 MG |
65 | } |
66 | ||
67 | if (!isLoaded()) { | |
68 | return; | |
08fec0be | 69 | } |
dea0f4b0 | 70 | |
818fb327 | 71 | if (getDelay() < 0) { |
08fec0be MG |
72 | return; |
73 | } | |
74 | ||
75 | /* Module was obviously loaded, so it is safe to load on boot */ | |
d3e7b10c MG |
76 | editor.putBoolean("safe_to_load", true); |
77 | editor.commit(); | |
0ae502f6 MG |
78 | } |
79 | ||
08fec0be | 80 | public synchronized void unloadModule() { |
40697a47 | 81 | try { |
6bb245ae | 82 | runAsRoot("/system/bin/rmmod debounce"); |
40697a47 MG |
83 | } catch (Exception e) {} |
84 | } | |
85 | ||
08fec0be | 86 | public synchronized boolean isLoaded() { |
d82ae589 MG |
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; | |
0ae502f6 | 105 | } |
226a7d4d | 106 | |
75fbc6ef MG |
107 | private synchronized int getValue(String parameter) { |
108 | int value = -1; | |
d82ae589 MG |
109 | |
110 | try { | |
111 | String read; | |
112 | ||
75fbc6ef MG |
113 | FileReader fr = new FileReader("/sys/devices/debounce/" + parameter); |
114 | BufferedReader fbuf = new BufferedReader(fr); | |
d82ae589 | 115 | |
75fbc6ef | 116 | read = fbuf.readLine(); |
d82ae589 | 117 | if (read != null) { |
75fbc6ef | 118 | value = Integer.parseInt(read.trim()); |
d82ae589 | 119 | } |
0b9d6422 | 120 | |
75fbc6ef | 121 | fbuf.close(); |
d82ae589 MG |
122 | } catch (Exception e) {} |
123 | ||
75fbc6ef | 124 | return value; |
ee6322a1 MG |
125 | } |
126 | ||
75fbc6ef | 127 | private synchronized void setValue(String parameter, int value) { |
0b9d6422 MG |
128 | if (!isLoaded()) { |
129 | return; | |
376c6ac7 MG |
130 | } |
131 | ||
0b9d6422 | 132 | try { |
75fbc6ef MG |
133 | FileWriter fw = new FileWriter("/sys/devices/debounce/" + parameter); |
134 | BufferedWriter fbuf = new BufferedWriter(fw); | |
0b9d6422 | 135 | |
75fbc6ef | 136 | fbuf.write((new Integer(value)).toString()); |
0b9d6422 | 137 | |
75fbc6ef | 138 | fbuf.close(); |
0b9d6422 | 139 | } catch (Exception e) {} |
376c6ac7 MG |
140 | } |
141 | ||
75fbc6ef MG |
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 | ||
2bb83a0e MG |
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 | ||
dea0f4b0 MG |
188 | public synchronized int getSavedDelay() { |
189 | SharedPreferences settings = ctx.getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE); | |
190 | ||
131fed25 | 191 | return settings.getInt("debounce_delay", 15); |
dea0f4b0 MG |
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 | ||
75fbc6ef MG |
202 | public synchronized int getSavedSettle() { |
203 | SharedPreferences settings = ctx.getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE); | |
204 | ||
ceccb7e2 | 205 | return settings.getInt("settle_time", 40); |
75fbc6ef MG |
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 | ||
ceccb7e2 | 219 | return settings.getInt("poll_time", 20); |
75fbc6ef MG |
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 | ||
2bb83a0e MG |
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 | ||
08fec0be MG |
258 | public synchronized boolean is_safe_to_load() { |
259 | SharedPreferences settings = ctx.getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE); | |
c3053460 MG |
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 | } | |
08fec0be | 267 | |
c3053460 MG |
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(); | |
08fec0be MG |
274 | } |
275 | ||
5738a32f MG |
276 | private synchronized void extractModule() { |
277 | File debounce_ko = new File(ctx.getFilesDir() + "/debounce.ko"); | |
226a7d4d MG |
278 | |
279 | if (debounce_ko.exists()) { | |
280 | return; | |
281 | } | |
5738a32f MG |
282 | |
283 | try { | |
284 | InputStream apk = ctx.getAssets().open("debounce.ko"); | |
40697a47 MG |
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 | } | |
5738a32f MG |
294 | |
295 | apk.close(); | |
296 | mod.close(); | |
40697a47 MG |
297 | |
298 | File tmpfile = new File(debounce_ko + ".tmp"); | |
299 | tmpfile.renameTo(debounce_ko); | |
5738a32f | 300 | } catch (Exception e) {} |
226a7d4d | 301 | } |
0ae502f6 | 302 | } |