]>
Commit | Line | Data |
---|---|---|
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 | } | |
30 | ||
31 | public void loadModule() { | |
32 | _loadModule(); | |
33 | setAllValues(); | |
34 | } | |
35 | ||
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 | ||
47 | public synchronized void _loadModule() { | |
48 | File debounce_ko = new File(ctx.getFilesDir() + "/debounce.ko"); | |
49 | ||
50 | extractModule(); | |
51 | ||
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 | } | |
58 | ||
59 | try { | |
60 | runAsRoot("/system/bin/insmod " + debounce_ko); | |
61 | } catch (Exception e) { | |
62 | return; | |
63 | } | |
64 | ||
65 | if (!isLoaded()) { | |
66 | return; | |
67 | } | |
68 | ||
69 | if (getDelay() < 0) { | |
70 | return; | |
71 | } | |
72 | ||
73 | /* Module was obviously loaded, so it is safe to load on boot */ | |
74 | editor.putBoolean("safe_to_load", true); | |
75 | editor.commit(); | |
76 | } | |
77 | ||
78 | public synchronized void unloadModule() { | |
79 | try { | |
80 | runAsRoot("/system/bin/rmmod debounce"); | |
81 | } catch (Exception e) {} | |
82 | } | |
83 | ||
84 | public synchronized boolean isLoaded() { | |
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; | |
103 | } | |
104 | ||
105 | private synchronized int getValue(String parameter) { | |
106 | int value = -1; | |
107 | ||
108 | try { | |
109 | String read; | |
110 | ||
111 | FileReader fr = new FileReader("/sys/devices/debounce/" + parameter); | |
112 | BufferedReader fbuf = new BufferedReader(fr); | |
113 | ||
114 | read = fbuf.readLine(); | |
115 | if (read != null) { | |
116 | value = Integer.parseInt(read.trim()); | |
117 | } | |
118 | ||
119 | fbuf.close(); | |
120 | } catch (Exception e) {} | |
121 | ||
122 | return value; | |
123 | } | |
124 | ||
125 | private synchronized void setValue(String parameter, int value) { | |
126 | if (!isLoaded()) { | |
127 | return; | |
128 | } | |
129 | ||
130 | try { | |
131 | FileWriter fw = new FileWriter("/sys/devices/debounce/" + parameter); | |
132 | BufferedWriter fbuf = new BufferedWriter(fw); | |
133 | ||
134 | fbuf.write((new Integer(value)).toString()); | |
135 | ||
136 | fbuf.close(); | |
137 | } catch (Exception e) {} | |
138 | } | |
139 | ||
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 | ||
164 | public synchronized int getSavedDelay() { | |
165 | SharedPreferences settings = ctx.getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE); | |
166 | ||
167 | return settings.getInt("debounce_delay", 15); | |
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 | ||
178 | public synchronized int getSavedSettle() { | |
179 | SharedPreferences settings = ctx.getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE); | |
180 | ||
181 | return settings.getInt("settle_time", 40); | |
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 | ||
195 | return settings.getInt("poll_time", 20); | |
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 | ||
206 | public synchronized boolean is_safe_to_load() { | |
207 | SharedPreferences settings = ctx.getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE); | |
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 | } | |
215 | ||
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(); | |
222 | } | |
223 | ||
224 | private synchronized void extractModule() { | |
225 | File debounce_ko = new File(ctx.getFilesDir() + "/debounce.ko"); | |
226 | ||
227 | if (debounce_ko.exists()) { | |
228 | return; | |
229 | } | |
230 | ||
231 | try { | |
232 | InputStream apk = ctx.getAssets().open("debounce.ko"); | |
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 | } | |
242 | ||
243 | apk.close(); | |
244 | mod.close(); | |
245 | ||
246 | File tmpfile = new File(debounce_ko + ".tmp"); | |
247 | tmpfile.renameTo(debounce_ko); | |
248 | } catch (Exception e) {} | |
249 | } | |
250 | } |