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 loadModule() {
27 setDelay(getSavedDelay());
28 setSettle(getSavedSettle());
29 setPoll(getSavedPoll());
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");
43 public synchronized void _loadModule() {
44 File debounce_ko
= new File(ctx
.getFilesDir() + "/debounce.ko");
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);
56 runAsRoot("/system/bin/insmod " + debounce_ko
);
57 } catch (Exception e
) {
69 /* Module was obviously loaded, so it is safe to load on boot */
70 editor
.putBoolean("safe_to_load", true);
74 public synchronized void unloadModule() {
76 runAsRoot("/system/bin/rmmod debounce");
77 } catch (Exception e
) {}
80 public synchronized boolean isLoaded() {
81 boolean loaded
= false;
85 FileReader modules
= new FileReader("/proc/modules");
86 BufferedReader modules_buf
= new BufferedReader(modules
);
88 while((read
= modules_buf
.readLine()) != null) {
89 if (read
.regionMatches(0, "debounce", 0, 8)) {
94 } catch (Exception e
) {
101 private synchronized int getValue(String parameter
) {
107 FileReader fr
= new FileReader("/sys/devices/debounce/" + parameter
);
108 BufferedReader fbuf
= new BufferedReader(fr
);
110 read
= fbuf
.readLine();
112 value
= Integer
.parseInt(read
.trim());
116 } catch (Exception e
) {}
121 private synchronized void setValue(String parameter
, int value
) {
127 FileWriter fw
= new FileWriter("/sys/devices/debounce/" + parameter
);
128 BufferedWriter fbuf
= new BufferedWriter(fw
);
130 fbuf
.write((new Integer(value
)).toString());
133 } catch (Exception e
) {}
136 public synchronized int getDelay() {
137 return getValue("debounce_delay");
140 public synchronized void setDelay(int debounce_delay
) {
141 setValue("debounce_delay", debounce_delay
);
144 public synchronized int getSettle() {
145 return getValue("settle_time");
148 public synchronized void setSettle(int settle_time
) {
149 setValue("settle_time", settle_time
);
152 public synchronized int getPoll() {
153 return getValue("poll_time");
156 public synchronized void setPoll(int poll_time
) {
157 setValue("poll_time", poll_time
);
160 public synchronized int getSavedDelay() {
161 SharedPreferences settings
= ctx
.getSharedPreferences(PREFS_NAME
, Context
.MODE_PRIVATE
);
163 return settings
.getInt("debounce_delay", 8);
166 public synchronized void setSavedDelay(int delay
) {
167 SharedPreferences settings
= ctx
.getSharedPreferences(PREFS_NAME
, Context
.MODE_PRIVATE
);
168 SharedPreferences
.Editor editor
= settings
.edit();
170 editor
.putInt("debounce_delay", delay
);
174 public synchronized int getSavedSettle() {
175 SharedPreferences settings
= ctx
.getSharedPreferences(PREFS_NAME
, Context
.MODE_PRIVATE
);
177 return settings
.getInt("settle_time", 100);
180 public synchronized void setSavedSettle(int settle
) {
181 SharedPreferences settings
= ctx
.getSharedPreferences(PREFS_NAME
, Context
.MODE_PRIVATE
);
182 SharedPreferences
.Editor editor
= settings
.edit();
184 editor
.putInt("settle_time", settle
);
188 public synchronized int getSavedPoll() {
189 SharedPreferences settings
= ctx
.getSharedPreferences(PREFS_NAME
, Context
.MODE_PRIVATE
);
191 return settings
.getInt("poll_time", 10);
194 public synchronized void setSavedPoll(int poll
) {
195 SharedPreferences settings
= ctx
.getSharedPreferences(PREFS_NAME
, Context
.MODE_PRIVATE
);
196 SharedPreferences
.Editor editor
= settings
.edit();
198 editor
.putInt("poll_time", poll
);
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);
207 public synchronized boolean get_on_boot() {
208 SharedPreferences settings
= ctx
.getSharedPreferences(PREFS_NAME
, Context
.MODE_PRIVATE
);
209 return settings
.getBoolean("on_boot", false);
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();
216 editor
.putBoolean("on_boot", on_boot
);
220 private synchronized void extractModule() {
221 File debounce_ko
= new File(ctx
.getFilesDir() + "/debounce.ko");
223 if (debounce_ko
.exists()) {
228 InputStream apk
= ctx
.getAssets().open("debounce.ko");
229 OutputStream mod
= ctx
.openFileOutput("debounce.ko.tmp", 0);
231 //I assume a page is 4k...
232 byte buf
[] = new byte[4096];
235 while((bytes
= apk
.read(buf
)) != -1) {
236 mod
.write(buf
, 0, bytes
);
242 File tmpfile
= new File(debounce_ko
+ ".tmp");
243 tmpfile
.renameTo(debounce_ko
);
244 } catch (Exception e
) {}