1 package de
.rmdir
.ms2debounce
;
3 import java
.io
.InputStream
;
4 import java
.io
.OutputStream
;
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
;
12 import android
.content
.Context
;
13 import android
.content
.SharedPreferences
;
15 public class DebounceModuleHelper
18 public static final String PREFS_NAME
= "DebounceCfg";
19 final int SUPERUSER_REQUEST
= 4223;
21 public DebounceModuleHelper(Context context
) {
25 public void setAllValues() {
26 setDelay(getSavedDelay());
27 setSettle(getSavedSettle());
28 setPoll(getSavedPoll());
29 setHwDebounce(getSavedHwDebounce());
30 setHwDebounceTime(getSavedHwDebounceTime());
33 public void loadModule() {
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");
49 public synchronized void _loadModule() {
50 File debounce_ko
= new File(ctx
.getFilesDir() + "/debounce.ko");
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);
62 runAsRoot("/system/bin/insmod " + debounce_ko
);
63 } catch (Exception e
) {
75 /* Module was obviously loaded, so it is safe to load on boot */
76 editor
.putBoolean("safe_to_load", true);
80 public synchronized void unloadModule() {
82 runAsRoot("/system/bin/rmmod debounce");
83 } catch (Exception e
) {}
86 public synchronized boolean isLoaded() {
87 boolean loaded
= false;
91 FileReader modules
= new FileReader("/proc/modules");
92 BufferedReader modules_buf
= new BufferedReader(modules
);
94 while((read
= modules_buf
.readLine()) != null) {
95 if (read
.regionMatches(0, "debounce", 0, 8)) {
100 } catch (Exception e
) {
107 private synchronized int getValue(String parameter
) {
113 FileReader fr
= new FileReader("/sys/devices/debounce/" + parameter
);
114 BufferedReader fbuf
= new BufferedReader(fr
);
116 read
= fbuf
.readLine();
118 value
= Integer
.parseInt(read
.trim());
122 } catch (Exception e
) {}
127 private synchronized void setValue(String parameter
, int value
) {
133 FileWriter fw
= new FileWriter("/sys/devices/debounce/" + parameter
);
134 BufferedWriter fbuf
= new BufferedWriter(fw
);
136 fbuf
.write((new Integer(value
)).toString());
139 } catch (Exception e
) {}
142 public synchronized int getDelay() {
143 return getValue("debounce_delay");
146 public synchronized void setDelay(int debounce_delay
) {
147 setValue("debounce_delay", debounce_delay
);
150 public synchronized int getSettle() {
151 return getValue("settle_time");
154 public synchronized void setSettle(int settle_time
) {
155 setValue("settle_time", settle_time
);
158 public synchronized int getPoll() {
159 return getValue("poll_time");
162 public synchronized void setPoll(int poll_time
) {
163 setValue("poll_time", poll_time
);
166 public synchronized boolean getHwDebounce() {
167 if (getValue("hw_debounce") == 1)
173 public synchronized void setHwDebounce(boolean enable
) {
175 setValue("hw_debounce", 1);
177 setValue("hw_debounce", 0);
180 public synchronized int getHwDebounceTime() {
181 return getValue("hw_debounce_time");
184 public synchronized void setHwDebounceTime(int time
) {
185 setValue("hw_debounce_time", time
);
188 public synchronized int getSavedDelay() {
189 SharedPreferences settings
= ctx
.getSharedPreferences(PREFS_NAME
, Context
.MODE_PRIVATE
);
191 return settings
.getInt("debounce_delay", 15);
194 public synchronized void setSavedDelay(int delay
) {
195 SharedPreferences settings
= ctx
.getSharedPreferences(PREFS_NAME
, Context
.MODE_PRIVATE
);
196 SharedPreferences
.Editor editor
= settings
.edit();
198 editor
.putInt("debounce_delay", delay
);
202 public synchronized int getSavedSettle() {
203 SharedPreferences settings
= ctx
.getSharedPreferences(PREFS_NAME
, Context
.MODE_PRIVATE
);
205 return settings
.getInt("settle_time", 40);
208 public synchronized void setSavedSettle(int settle
) {
209 SharedPreferences settings
= ctx
.getSharedPreferences(PREFS_NAME
, Context
.MODE_PRIVATE
);
210 SharedPreferences
.Editor editor
= settings
.edit();
212 editor
.putInt("settle_time", settle
);
216 public synchronized int getSavedPoll() {
217 SharedPreferences settings
= ctx
.getSharedPreferences(PREFS_NAME
, Context
.MODE_PRIVATE
);
219 return settings
.getInt("poll_time", 20);
222 public synchronized void setSavedPoll(int poll
) {
223 SharedPreferences settings
= ctx
.getSharedPreferences(PREFS_NAME
, Context
.MODE_PRIVATE
);
224 SharedPreferences
.Editor editor
= settings
.edit();
226 editor
.putInt("poll_time", poll
);
230 public synchronized boolean getSavedHwDebounce() {
231 SharedPreferences settings
= ctx
.getSharedPreferences(PREFS_NAME
, Context
.MODE_PRIVATE
);
233 return settings
.getBoolean("hw_debounce", true);
236 public synchronized void setSavedHwDebounce(boolean enable
) {
237 SharedPreferences settings
= ctx
.getSharedPreferences(PREFS_NAME
, Context
.MODE_PRIVATE
);
238 SharedPreferences
.Editor editor
= settings
.edit();
240 editor
.putBoolean("hw_debounce", enable
);
244 public synchronized int getSavedHwDebounceTime() {
245 SharedPreferences settings
= ctx
.getSharedPreferences(PREFS_NAME
, Context
.MODE_PRIVATE
);
247 return settings
.getInt("hw_debounce_time", 1);
250 public synchronized void setSavedHwDebounceTime(int time
) {
251 SharedPreferences settings
= ctx
.getSharedPreferences(PREFS_NAME
, Context
.MODE_PRIVATE
);
252 SharedPreferences
.Editor editor
= settings
.edit();
254 editor
.putInt("hw_debounce_time", time
);
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);
263 public synchronized boolean get_on_boot() {
264 SharedPreferences settings
= ctx
.getSharedPreferences(PREFS_NAME
, Context
.MODE_PRIVATE
);
265 return settings
.getBoolean("on_boot", false);
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();
272 editor
.putBoolean("on_boot", on_boot
);
276 private synchronized void extractModule() {
277 File debounce_ko
= new File(ctx
.getFilesDir() + "/debounce.ko");
279 if (debounce_ko
.exists()) {
284 InputStream apk
= ctx
.getAssets().open("debounce.ko");
285 OutputStream mod
= ctx
.openFileOutput("debounce.ko.tmp", 0);
287 //I assume a page is 4k...
288 byte buf
[] = new byte[4096];
291 while((bytes
= apk
.read(buf
)) != -1) {
292 mod
.write(buf
, 0, bytes
);
298 File tmpfile
= new File(debounce_ko
+ ".tmp");
299 tmpfile
.renameTo(debounce_ko
);
300 } catch (Exception e
) {}