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 SharedPreferences settings
= ctx
.getSharedPreferences(PREFS_NAME
, Context
.MODE_PRIVATE
);
24 int delay
= settings
.getInt("debounce_delay", 10);
28 public synchronized void loadModule(int delay
) {
29 File debounce_ko
= new File(ctx
.getFilesDir() + "/debounce.ko");
33 SharedPreferences settings
= ctx
.getSharedPreferences(PREFS_NAME
, Context
.MODE_PRIVATE
);
34 SharedPreferences
.Editor editor
= settings
.edit();
35 if (is_safe_to_load()) {
36 editor
.putBoolean("safe_to_load", false);
41 Process insmod
= Runtime
.getRuntime().exec(new String
[]{"su","-c","/system/bin/insmod " + debounce_ko
+ " debounce_delay=" + delay
});
43 } catch (Exception e
) {
51 if (getDelay() <= 0) {
55 /* Module was obviously loaded, so it is safe to load on boot */
56 editor
.putBoolean("safe_to_load", true);
60 public synchronized void unloadModule() {
62 Process rmmod
= Runtime
.getRuntime().exec(new String
[]{"su","-c","/system/bin/rmmod debounce"});
64 } catch (Exception e
) {}
67 public synchronized boolean isLoaded() {
68 boolean loaded
= false;
72 FileReader modules
= new FileReader("/proc/modules");
73 BufferedReader modules_buf
= new BufferedReader(modules
);
75 while((read
= modules_buf
.readLine()) != null) {
76 if (read
.regionMatches(0, "debounce", 0, 8)) {
81 } catch (Exception e
) {
88 public synchronized int getDelay() {
89 int debounce_delay
= -1;
94 FileReader delay
= new FileReader("/sys/module/debounce/parameters/debounce_delay");
95 BufferedReader delay_buf
= new BufferedReader(delay
);
97 read
= delay_buf
.readLine();
99 debounce_delay
= Integer
.parseInt(read
.trim());
101 } catch (Exception e
) {}
103 return debounce_delay
;
106 public synchronized void setDelay(int delay
) {
108 if (getDelay() == delay
) {
118 public synchronized boolean is_safe_to_load() {
119 SharedPreferences settings
= ctx
.getSharedPreferences(PREFS_NAME
, Context
.MODE_PRIVATE
);
120 boolean safe_to_load
= settings
.getBoolean("safe_to_load", false);
125 private synchronized void extractModule() {
126 File debounce_ko
= new File(ctx
.getFilesDir() + "/debounce.ko");
128 if (debounce_ko
.exists()) {
133 InputStream apk
= ctx
.getAssets().open("debounce.ko");
134 OutputStream mod
= ctx
.openFileOutput("debounce.ko.tmp", 0);
136 //I assume a page is 4k...
137 byte buf
[] = new byte[4096];
140 while((bytes
= apk
.read(buf
)) != -1) {
141 mod
.write(buf
, 0, bytes
);
147 File tmpfile
= new File(debounce_ko
+ ".tmp");
148 tmpfile
.renameTo(debounce_ko
);
149 } catch (Exception e
) {}