1 package de
.rmdir
.ms2debounce
;
3 import java
.io
.InputStream
;
4 import java
.io
.OutputStream
;
6 import java
.io
.FileReader
;
7 import java
.io
.BufferedReader
;
9 import android
.content
.Context
;
10 import android
.content
.SharedPreferences
;
12 public class DebounceModuleHelper
15 public static final String PREFS_NAME
= "DebounceCfg";
17 public DebounceModuleHelper(Context context
) {
21 public void loadModule() {
22 loadModule(getSavedDelay());
25 public synchronized void loadModule(int delay
) {
26 File debounce_ko
= new File(ctx
.getFilesDir() + "/debounce.ko");
30 SharedPreferences settings
= ctx
.getSharedPreferences(PREFS_NAME
, Context
.MODE_PRIVATE
);
31 SharedPreferences
.Editor editor
= settings
.edit();
32 if (is_safe_to_load()) {
33 editor
.putBoolean("safe_to_load", false);
38 Process insmod
= Runtime
.getRuntime().exec(new String
[]{"su","-c","/system/bin/insmod " + debounce_ko
+ " debounce_delay=" + delay
});
40 } catch (Exception e
) {
48 if (getDelay() <= 0) {
52 /* Module was obviously loaded, so it is safe to load on boot */
53 editor
.putBoolean("safe_to_load", true);
57 public synchronized void unloadModule() {
59 Process rmmod
= Runtime
.getRuntime().exec(new String
[]{"su","-c","/system/bin/rmmod debounce"});
61 } catch (Exception e
) {}
64 public synchronized boolean isLoaded() {
65 boolean loaded
= false;
69 FileReader modules
= new FileReader("/proc/modules");
70 BufferedReader modules_buf
= new BufferedReader(modules
);
72 while((read
= modules_buf
.readLine()) != null) {
73 if (read
.regionMatches(0, "debounce", 0, 8)) {
78 } catch (Exception e
) {
85 public synchronized int getDelay() {
86 int debounce_delay
= -1;
91 FileReader delay
= new FileReader("/sys/module/debounce/parameters/debounce_delay");
92 BufferedReader delay_buf
= new BufferedReader(delay
);
94 read
= delay_buf
.readLine();
96 debounce_delay
= Integer
.parseInt(read
.trim());
98 } catch (Exception e
) {}
100 return debounce_delay
;
103 public synchronized void setDelay(int delay
) {
105 if (getDelay() == delay
) {
115 public synchronized int getSavedDelay() {
116 SharedPreferences settings
= ctx
.getSharedPreferences(PREFS_NAME
, Context
.MODE_PRIVATE
);
118 return settings
.getInt("debounce_delay", 10);
121 public synchronized void setSavedDelay(int delay
) {
122 SharedPreferences settings
= ctx
.getSharedPreferences(PREFS_NAME
, Context
.MODE_PRIVATE
);
123 SharedPreferences
.Editor editor
= settings
.edit();
125 editor
.putInt("debounce_delay", delay
);
129 public synchronized boolean is_safe_to_load() {
130 SharedPreferences settings
= ctx
.getSharedPreferences(PREFS_NAME
, Context
.MODE_PRIVATE
);
131 boolean safe_to_load
= settings
.getBoolean("safe_to_load", false);
136 private synchronized void extractModule() {
137 File debounce_ko
= new File(ctx
.getFilesDir() + "/debounce.ko");
139 if (debounce_ko
.exists()) {
144 InputStream apk
= ctx
.getAssets().open("debounce.ko");
145 OutputStream mod
= ctx
.openFileOutput("debounce.ko.tmp", 0);
147 //I assume a page is 4k...
148 byte buf
[] = new byte[4096];
151 while((bytes
= apk
.read(buf
)) != -1) {
152 mod
.write(buf
, 0, bytes
);
158 File tmpfile
= new File(debounce_ko
+ ".tmp");
159 tmpfile
.renameTo(debounce_ko
);
160 } catch (Exception e
) {}