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());
31 public void loadModule() {
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");
47 public synchronized void _loadModule() {
48 File debounce_ko
= new File(ctx
.getFilesDir() + "/debounce.ko");
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);
60 runAsRoot("/system/bin/insmod " + debounce_ko
);
61 } catch (Exception e
) {
73 /* Module was obviously loaded, so it is safe to load on boot */
74 editor
.putBoolean("safe_to_load", true);
78 public synchronized void unloadModule() {
80 runAsRoot("/system/bin/rmmod debounce");
81 } catch (Exception e
) {}
84 public synchronized boolean isLoaded() {
85 boolean loaded
= false;
89 FileReader modules
= new FileReader("/proc/modules");
90 BufferedReader modules_buf
= new BufferedReader(modules
);
92 while((read
= modules_buf
.readLine()) != null) {
93 if (read
.regionMatches(0, "debounce", 0, 8)) {
98 } catch (Exception e
) {
105 private synchronized int getValue(String parameter
) {
111 FileReader fr
= new FileReader("/sys/devices/debounce/" + parameter
);
112 BufferedReader fbuf
= new BufferedReader(fr
);
114 read
= fbuf
.readLine();
116 value
= Integer
.parseInt(read
.trim());
120 } catch (Exception e
) {}
125 private synchronized void setValue(String parameter
, int value
) {
131 FileWriter fw
= new FileWriter("/sys/devices/debounce/" + parameter
);
132 BufferedWriter fbuf
= new BufferedWriter(fw
);
134 fbuf
.write((new Integer(value
)).toString());
137 } catch (Exception e
) {}
140 public synchronized int getDelay() {
141 return getValue("debounce_delay");
144 public synchronized void setDelay(int debounce_delay
) {
145 setValue("debounce_delay", debounce_delay
);
148 public synchronized int getSettle() {
149 return getValue("settle_time");
152 public synchronized void setSettle(int settle_time
) {
153 setValue("settle_time", settle_time
);
156 public synchronized int getPoll() {
157 return getValue("poll_time");
160 public synchronized void setPoll(int poll_time
) {
161 setValue("poll_time", poll_time
);
164 public synchronized int getSavedDelay() {
165 SharedPreferences settings
= ctx
.getSharedPreferences(PREFS_NAME
, Context
.MODE_PRIVATE
);
167 return settings
.getInt("debounce_delay", 15);
170 public synchronized void setSavedDelay(int delay
) {
171 SharedPreferences settings
= ctx
.getSharedPreferences(PREFS_NAME
, Context
.MODE_PRIVATE
);
172 SharedPreferences
.Editor editor
= settings
.edit();
174 editor
.putInt("debounce_delay", delay
);
178 public synchronized int getSavedSettle() {
179 SharedPreferences settings
= ctx
.getSharedPreferences(PREFS_NAME
, Context
.MODE_PRIVATE
);
181 return settings
.getInt("settle_time", 150);
184 public synchronized void setSavedSettle(int settle
) {
185 SharedPreferences settings
= ctx
.getSharedPreferences(PREFS_NAME
, Context
.MODE_PRIVATE
);
186 SharedPreferences
.Editor editor
= settings
.edit();
188 editor
.putInt("settle_time", settle
);
192 public synchronized int getSavedPoll() {
193 SharedPreferences settings
= ctx
.getSharedPreferences(PREFS_NAME
, Context
.MODE_PRIVATE
);
195 return settings
.getInt("poll_time", 2);
198 public synchronized void setSavedPoll(int poll
) {
199 SharedPreferences settings
= ctx
.getSharedPreferences(PREFS_NAME
, Context
.MODE_PRIVATE
);
200 SharedPreferences
.Editor editor
= settings
.edit();
202 editor
.putInt("poll_time", poll
);
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);
211 public synchronized boolean get_on_boot() {
212 SharedPreferences settings
= ctx
.getSharedPreferences(PREFS_NAME
, Context
.MODE_PRIVATE
);
213 return settings
.getBoolean("on_boot", false);
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();
220 editor
.putBoolean("on_boot", on_boot
);
224 private synchronized void extractModule() {
225 File debounce_ko
= new File(ctx
.getFilesDir() + "/debounce.ko");
227 if (debounce_ko
.exists()) {
232 InputStream apk
= ctx
.getAssets().open("debounce.ko");
233 OutputStream mod
= ctx
.openFileOutput("debounce.ko.tmp", 0);
235 //I assume a page is 4k...
236 byte buf
[] = new byte[4096];
239 while((bytes
= apk
.read(buf
)) != -1) {
240 mod
.write(buf
, 0, bytes
);
246 File tmpfile
= new File(debounce_ko
+ ".tmp");
247 tmpfile
.renameTo(debounce_ko
);
248 } catch (Exception e
) {}