]>
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()); | |
40697a47 MG |
29 | } |
30 | ||
381027a8 MG |
31 | public void loadModule() { |
32 | _loadModule(); | |
33 | setAllValues(); | |
34 | } | |
35 | ||
6bb245ae MG |
36 | protected void runAsRoot(String command) throws java.io.IOException,java.lang.InterruptedException { |
37 | Process rootcmd = Runtime.getRuntime().exec(new String[]{"su","-c","sh"}); | |
38 | DataOutputStream sh = new DataOutputStream(rootcmd.getOutputStream()); | |
39 | sh.writeBytes(command + "\n"); | |
40 | sh.writeBytes("exit\n"); | |
41 | sh.flush(); | |
42 | sh.close(); | |
43 | ||
44 | rootcmd.waitFor(); | |
45 | } | |
46 | ||
0b9d6422 | 47 | public synchronized void _loadModule() { |
40697a47 MG |
48 | File debounce_ko = new File(ctx.getFilesDir() + "/debounce.ko"); |
49 | ||
226a7d4d MG |
50 | extractModule(); |
51 | ||
d3e7b10c MG |
52 | SharedPreferences settings = ctx.getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE); |
53 | SharedPreferences.Editor editor = settings.edit(); | |
54 | if (is_safe_to_load()) { | |
55 | editor.putBoolean("safe_to_load", false); | |
56 | editor.commit(); | |
57 | } | |
40697a47 | 58 | |
0ae502f6 | 59 | try { |
0b9d6422 | 60 | runAsRoot("/system/bin/insmod " + debounce_ko); |
08fec0be MG |
61 | } catch (Exception e) { |
62 | return; | |
a6cf1017 MG |
63 | } |
64 | ||
65 | if (!isLoaded()) { | |
66 | return; | |
08fec0be | 67 | } |
dea0f4b0 | 68 | |
818fb327 | 69 | if (getDelay() < 0) { |
08fec0be MG |
70 | return; |
71 | } | |
72 | ||
73 | /* Module was obviously loaded, so it is safe to load on boot */ | |
d3e7b10c MG |
74 | editor.putBoolean("safe_to_load", true); |
75 | editor.commit(); | |
0ae502f6 MG |
76 | } |
77 | ||
08fec0be | 78 | public synchronized void unloadModule() { |
40697a47 | 79 | try { |
6bb245ae | 80 | runAsRoot("/system/bin/rmmod debounce"); |
40697a47 MG |
81 | } catch (Exception e) {} |
82 | } | |
83 | ||
08fec0be | 84 | public synchronized boolean isLoaded() { |
d82ae589 MG |
85 | boolean loaded = false; |
86 | try { | |
87 | String read; | |
88 | ||
89 | FileReader modules = new FileReader("/proc/modules"); | |
90 | BufferedReader modules_buf = new BufferedReader(modules); | |
91 | ||
92 | while((read = modules_buf.readLine()) != null) { | |
93 | if (read.regionMatches(0, "debounce", 0, 8)) { | |
94 | loaded = true; | |
95 | } | |
96 | } | |
97 | ||
98 | } catch (Exception e) { | |
99 | loaded = false; | |
100 | } | |
101 | ||
102 | return loaded; | |
0ae502f6 | 103 | } |
226a7d4d | 104 | |
75fbc6ef MG |
105 | private synchronized int getValue(String parameter) { |
106 | int value = -1; | |
d82ae589 MG |
107 | |
108 | try { | |
109 | String read; | |
110 | ||
75fbc6ef MG |
111 | FileReader fr = new FileReader("/sys/devices/debounce/" + parameter); |
112 | BufferedReader fbuf = new BufferedReader(fr); | |
d82ae589 | 113 | |
75fbc6ef | 114 | read = fbuf.readLine(); |
d82ae589 | 115 | if (read != null) { |
75fbc6ef | 116 | value = Integer.parseInt(read.trim()); |
d82ae589 | 117 | } |
0b9d6422 | 118 | |
75fbc6ef | 119 | fbuf.close(); |
d82ae589 MG |
120 | } catch (Exception e) {} |
121 | ||
75fbc6ef | 122 | return value; |
ee6322a1 MG |
123 | } |
124 | ||
75fbc6ef | 125 | private synchronized void setValue(String parameter, int value) { |
0b9d6422 MG |
126 | if (!isLoaded()) { |
127 | return; | |
376c6ac7 MG |
128 | } |
129 | ||
0b9d6422 | 130 | try { |
75fbc6ef MG |
131 | FileWriter fw = new FileWriter("/sys/devices/debounce/" + parameter); |
132 | BufferedWriter fbuf = new BufferedWriter(fw); | |
0b9d6422 | 133 | |
75fbc6ef | 134 | fbuf.write((new Integer(value)).toString()); |
0b9d6422 | 135 | |
75fbc6ef | 136 | fbuf.close(); |
0b9d6422 | 137 | } catch (Exception e) {} |
376c6ac7 MG |
138 | } |
139 | ||
75fbc6ef MG |
140 | public synchronized int getDelay() { |
141 | return getValue("debounce_delay"); | |
142 | } | |
143 | ||
144 | public synchronized void setDelay(int debounce_delay) { | |
145 | setValue("debounce_delay", debounce_delay); | |
146 | } | |
147 | ||
148 | public synchronized int getSettle() { | |
149 | return getValue("settle_time"); | |
150 | } | |
151 | ||
152 | public synchronized void setSettle(int settle_time) { | |
153 | setValue("settle_time", settle_time); | |
154 | } | |
155 | ||
156 | public synchronized int getPoll() { | |
157 | return getValue("poll_time"); | |
158 | } | |
159 | ||
160 | public synchronized void setPoll(int poll_time) { | |
161 | setValue("poll_time", poll_time); | |
162 | } | |
163 | ||
dea0f4b0 MG |
164 | public synchronized int getSavedDelay() { |
165 | SharedPreferences settings = ctx.getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE); | |
166 | ||
131fed25 | 167 | return settings.getInt("debounce_delay", 15); |
dea0f4b0 MG |
168 | } |
169 | ||
170 | public synchronized void setSavedDelay(int delay) { | |
171 | SharedPreferences settings = ctx.getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE); | |
172 | SharedPreferences.Editor editor = settings.edit(); | |
173 | ||
174 | editor.putInt("debounce_delay", delay); | |
175 | editor.commit(); | |
176 | } | |
177 | ||
75fbc6ef MG |
178 | public synchronized int getSavedSettle() { |
179 | SharedPreferences settings = ctx.getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE); | |
180 | ||
882feaf4 | 181 | return settings.getInt("settle_time", 150); |
75fbc6ef MG |
182 | } |
183 | ||
184 | public synchronized void setSavedSettle(int settle) { | |
185 | SharedPreferences settings = ctx.getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE); | |
186 | SharedPreferences.Editor editor = settings.edit(); | |
187 | ||
188 | editor.putInt("settle_time", settle); | |
189 | editor.commit(); | |
190 | } | |
191 | ||
192 | public synchronized int getSavedPoll() { | |
193 | SharedPreferences settings = ctx.getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE); | |
194 | ||
131fed25 | 195 | return settings.getInt("poll_time", 2); |
75fbc6ef MG |
196 | } |
197 | ||
198 | public synchronized void setSavedPoll(int poll) { | |
199 | SharedPreferences settings = ctx.getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE); | |
200 | SharedPreferences.Editor editor = settings.edit(); | |
201 | ||
202 | editor.putInt("poll_time", poll); | |
203 | editor.commit(); | |
204 | } | |
205 | ||
08fec0be MG |
206 | public synchronized boolean is_safe_to_load() { |
207 | SharedPreferences settings = ctx.getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE); | |
c3053460 MG |
208 | return settings.getBoolean("safe_to_load", false); |
209 | } | |
210 | ||
211 | public synchronized boolean get_on_boot() { | |
212 | SharedPreferences settings = ctx.getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE); | |
213 | return settings.getBoolean("on_boot", false); | |
214 | } | |
08fec0be | 215 | |
c3053460 MG |
216 | public synchronized void set_on_boot(boolean on_boot) { |
217 | SharedPreferences settings = ctx.getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE); | |
218 | SharedPreferences.Editor editor = settings.edit(); | |
219 | ||
220 | editor.putBoolean("on_boot", on_boot); | |
221 | editor.commit(); | |
08fec0be MG |
222 | } |
223 | ||
5738a32f MG |
224 | private synchronized void extractModule() { |
225 | File debounce_ko = new File(ctx.getFilesDir() + "/debounce.ko"); | |
226a7d4d MG |
226 | |
227 | if (debounce_ko.exists()) { | |
228 | return; | |
229 | } | |
5738a32f MG |
230 | |
231 | try { | |
232 | InputStream apk = ctx.getAssets().open("debounce.ko"); | |
40697a47 MG |
233 | OutputStream mod = ctx.openFileOutput("debounce.ko.tmp", 0); |
234 | ||
235 | //I assume a page is 4k... | |
236 | byte buf[] = new byte[4096]; | |
237 | int bytes; | |
238 | ||
239 | while((bytes = apk.read(buf)) != -1) { | |
240 | mod.write(buf, 0, bytes); | |
241 | } | |
5738a32f MG |
242 | |
243 | apk.close(); | |
244 | mod.close(); | |
40697a47 MG |
245 | |
246 | File tmpfile = new File(debounce_ko + ".tmp"); | |
247 | tmpfile.renameTo(debounce_ko); | |
5738a32f | 248 | } catch (Exception e) {} |
226a7d4d | 249 | } |
0ae502f6 | 250 | } |