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