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());
31 setDriveInactive(getSavedDriveInactive());
34 public void loadModule() {
39 protected void runAsRoot(String command
) throws java
.io
.IOException
,java
.lang
.InterruptedException
{
40 Process rootcmd
= Runtime
.getRuntime().exec(new String
[]{"su","-c","sh"});
41 DataOutputStream sh
= new DataOutputStream(rootcmd
.getOutputStream());
42 sh
.writeBytes(command
+ "\n");
43 sh
.writeBytes("exit\n");
50 public synchronized void _loadModule() {
51 File debounce_ko
= new File(ctx
.getFilesDir() + "/debounce.ko");
55 SharedPreferences settings
= ctx
.getSharedPreferences(PREFS_NAME
, Context
.MODE_PRIVATE
);
56 SharedPreferences
.Editor editor
= settings
.edit();
57 if (is_safe_to_load()) {
58 editor
.putBoolean("safe_to_load", false);
63 runAsRoot("/system/bin/insmod " + debounce_ko
);
64 } catch (Exception e
) {
76 /* Module was obviously loaded, so it is safe to load on boot */
77 editor
.putBoolean("safe_to_load", true);
81 public synchronized void unloadModule() {
83 runAsRoot("/system/bin/rmmod debounce");
84 } catch (Exception e
) {}
87 public synchronized boolean isLoaded() {
88 boolean loaded
= false;
92 FileReader modules
= new FileReader("/proc/modules");
93 BufferedReader modules_buf
= new BufferedReader(modules
);
95 while((read
= modules_buf
.readLine()) != null) {
96 if (read
.regionMatches(0, "debounce", 0, 8)) {
101 } catch (Exception e
) {
108 private synchronized int getValue(String parameter
) {
114 FileReader fr
= new FileReader("/sys/devices/debounce/" + parameter
);
115 BufferedReader fbuf
= new BufferedReader(fr
);
117 read
= fbuf
.readLine();
119 value
= Integer
.parseInt(read
.trim());
123 } catch (Exception e
) {}
128 private synchronized void setValue(String parameter
, int value
) {
134 FileWriter fw
= new FileWriter("/sys/devices/debounce/" + parameter
);
135 BufferedWriter fbuf
= new BufferedWriter(fw
);
137 fbuf
.write((new Integer(value
)).toString());
140 } catch (Exception e
) {}
143 public synchronized int getDelay() {
144 return getValue("debounce_delay");
147 public synchronized void setDelay(int debounce_delay
) {
148 setValue("debounce_delay", debounce_delay
);
151 public synchronized int getSettle() {
152 return getValue("settle_time");
155 public synchronized void setSettle(int settle_time
) {
156 setValue("settle_time", settle_time
);
159 public synchronized int getPoll() {
160 return getValue("poll_time");
163 public synchronized void setPoll(int poll_time
) {
164 setValue("poll_time", poll_time
);
167 public synchronized boolean getHwDebounce() {
168 if (getValue("hw_debounce") == 1)
174 public synchronized void setHwDebounce(boolean enable
) {
176 setValue("hw_debounce", 1);
178 setValue("hw_debounce", 0);
181 public synchronized int getHwDebounceTime() {
182 return getValue("hw_debounce_time");
185 public synchronized void setHwDebounceTime(int time
) {
186 setValue("hw_debounce_time", time
);
189 public synchronized boolean getDriveInactive() {
190 if (getValue("drive_inactive_flag") == 1)
196 public synchronized void setDriveInactive(boolean enable
) {
198 setValue("drive_inactive_flag", 1);
200 setValue("drive_inactive_flag", 0);
203 public synchronized int getSavedDelay() {
204 SharedPreferences settings
= ctx
.getSharedPreferences(PREFS_NAME
, Context
.MODE_PRIVATE
);
206 return settings
.getInt("debounce_delay", 15);
209 public synchronized void setSavedDelay(int delay
) {
210 SharedPreferences settings
= ctx
.getSharedPreferences(PREFS_NAME
, Context
.MODE_PRIVATE
);
211 SharedPreferences
.Editor editor
= settings
.edit();
213 editor
.putInt("debounce_delay", delay
);
217 public synchronized int getSavedSettle() {
218 SharedPreferences settings
= ctx
.getSharedPreferences(PREFS_NAME
, Context
.MODE_PRIVATE
);
220 return settings
.getInt("settle_time", 40);
223 public synchronized void setSavedSettle(int settle
) {
224 SharedPreferences settings
= ctx
.getSharedPreferences(PREFS_NAME
, Context
.MODE_PRIVATE
);
225 SharedPreferences
.Editor editor
= settings
.edit();
227 editor
.putInt("settle_time", settle
);
231 public synchronized int getSavedPoll() {
232 SharedPreferences settings
= ctx
.getSharedPreferences(PREFS_NAME
, Context
.MODE_PRIVATE
);
234 return settings
.getInt("poll_time", 20);
237 public synchronized void setSavedPoll(int poll
) {
238 SharedPreferences settings
= ctx
.getSharedPreferences(PREFS_NAME
, Context
.MODE_PRIVATE
);
239 SharedPreferences
.Editor editor
= settings
.edit();
241 editor
.putInt("poll_time", poll
);
245 public synchronized boolean getSavedHwDebounce() {
246 SharedPreferences settings
= ctx
.getSharedPreferences(PREFS_NAME
, Context
.MODE_PRIVATE
);
248 return settings
.getBoolean("hw_debounce", false);
251 public synchronized void setSavedHwDebounce(boolean enable
) {
252 SharedPreferences settings
= ctx
.getSharedPreferences(PREFS_NAME
, Context
.MODE_PRIVATE
);
253 SharedPreferences
.Editor editor
= settings
.edit();
255 editor
.putBoolean("hw_debounce", enable
);
259 public synchronized int getSavedHwDebounceTime() {
260 SharedPreferences settings
= ctx
.getSharedPreferences(PREFS_NAME
, Context
.MODE_PRIVATE
);
262 return settings
.getInt("hw_debounce_time", 1);
265 public synchronized void setSavedHwDebounceTime(int time
) {
266 SharedPreferences settings
= ctx
.getSharedPreferences(PREFS_NAME
, Context
.MODE_PRIVATE
);
267 SharedPreferences
.Editor editor
= settings
.edit();
269 editor
.putInt("hw_debounce_time", time
);
273 public synchronized boolean getSavedDriveInactive() {
274 SharedPreferences settings
= ctx
.getSharedPreferences(PREFS_NAME
, Context
.MODE_PRIVATE
);
276 return settings
.getBoolean("drive_inactive", false);
279 public synchronized void setSavedDriveInactive(boolean enable
) {
280 SharedPreferences settings
= ctx
.getSharedPreferences(PREFS_NAME
, Context
.MODE_PRIVATE
);
281 SharedPreferences
.Editor editor
= settings
.edit();
283 editor
.putBoolean("drive_inactive", enable
);
287 public synchronized boolean is_safe_to_load() {
288 SharedPreferences settings
= ctx
.getSharedPreferences(PREFS_NAME
, Context
.MODE_PRIVATE
);
289 return settings
.getBoolean("safe_to_load", false);
292 public synchronized boolean get_on_boot() {
293 SharedPreferences settings
= ctx
.getSharedPreferences(PREFS_NAME
, Context
.MODE_PRIVATE
);
294 return settings
.getBoolean("on_boot", false);
297 public synchronized void set_on_boot(boolean on_boot
) {
298 SharedPreferences settings
= ctx
.getSharedPreferences(PREFS_NAME
, Context
.MODE_PRIVATE
);
299 SharedPreferences
.Editor editor
= settings
.edit();
301 editor
.putBoolean("on_boot", on_boot
);
305 private synchronized void extractModule() {
306 File debounce_ko
= new File(ctx
.getFilesDir() + "/debounce.ko");
308 if (debounce_ko
.exists()) {
313 InputStream apk
= ctx
.getAssets().open("debounce.ko");
314 OutputStream mod
= ctx
.openFileOutput("debounce.ko.tmp", 0);
316 //I assume a page is 4k...
317 byte buf
[] = new byte[4096];
320 while((bytes
= apk
.read(buf
)) != -1) {
321 mod
.write(buf
, 0, bytes
);
327 File tmpfile
= new File(debounce_ko
+ ".tmp");
328 tmpfile
.renameTo(debounce_ko
);
329 } catch (Exception e
) {}