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
;
8 import java
.io
.DataOutputStream
;
10 import android
.content
.Context
;
11 import android
.content
.SharedPreferences
;
13 public class DebounceModuleHelper
16 public static final String PREFS_NAME
= "DebounceCfg";
17 final int SUPERUSER_REQUEST
= 4223;
19 public DebounceModuleHelper(Context context
) {
23 public void loadModule() {
24 loadModule(getSavedDelay());
27 protected void runAsRoot(String command
) throws java
.io
.IOException
,java
.lang
.InterruptedException
{
28 Process rootcmd
= Runtime
.getRuntime().exec(new String
[]{"su","-c","sh"});
29 DataOutputStream sh
= new DataOutputStream(rootcmd
.getOutputStream());
30 sh
.writeBytes(command
+ "\n");
31 sh
.writeBytes("exit\n");
38 public synchronized void loadModule(int delay
) {
39 File debounce_ko
= new File(ctx
.getFilesDir() + "/debounce.ko");
43 SharedPreferences settings
= ctx
.getSharedPreferences(PREFS_NAME
, Context
.MODE_PRIVATE
);
44 SharedPreferences
.Editor editor
= settings
.edit();
45 if (is_safe_to_load()) {
46 editor
.putBoolean("safe_to_load", false);
51 runAsRoot("/system/bin/insmod " + debounce_ko
+ " debounce_delay=" + delay
);
52 } catch (Exception e
) {
64 /* Module was obviously loaded, so it is safe to load on boot */
65 editor
.putBoolean("safe_to_load", true);
69 public synchronized void unloadModule() {
71 runAsRoot("/system/bin/rmmod debounce");
72 } catch (Exception e
) {}
75 public synchronized boolean isLoaded() {
76 boolean loaded
= false;
80 FileReader modules
= new FileReader("/proc/modules");
81 BufferedReader modules_buf
= new BufferedReader(modules
);
83 while((read
= modules_buf
.readLine()) != null) {
84 if (read
.regionMatches(0, "debounce", 0, 8)) {
89 } catch (Exception e
) {
96 public synchronized int getDelay() {
97 int debounce_delay
= -1;
102 FileReader delay
= new FileReader("/sys/module/debounce/parameters/debounce_delay");
103 BufferedReader delay_buf
= new BufferedReader(delay
);
105 read
= delay_buf
.readLine();
107 debounce_delay
= Integer
.parseInt(read
.trim());
109 } catch (Exception e
) {}
111 return debounce_delay
;
114 public synchronized void setDelay(int delay
) {
116 if (getDelay() == delay
) {
126 public synchronized int getSavedDelay() {
127 SharedPreferences settings
= ctx
.getSharedPreferences(PREFS_NAME
, Context
.MODE_PRIVATE
);
129 return settings
.getInt("debounce_delay", 15);
132 public synchronized void setSavedDelay(int delay
) {
133 SharedPreferences settings
= ctx
.getSharedPreferences(PREFS_NAME
, Context
.MODE_PRIVATE
);
134 SharedPreferences
.Editor editor
= settings
.edit();
136 editor
.putInt("debounce_delay", delay
);
140 public synchronized boolean is_safe_to_load() {
141 SharedPreferences settings
= ctx
.getSharedPreferences(PREFS_NAME
, Context
.MODE_PRIVATE
);
142 return settings
.getBoolean("safe_to_load", false);
145 public synchronized boolean get_on_boot() {
146 SharedPreferences settings
= ctx
.getSharedPreferences(PREFS_NAME
, Context
.MODE_PRIVATE
);
147 return settings
.getBoolean("on_boot", false);
150 public synchronized void set_on_boot(boolean on_boot
) {
151 SharedPreferences settings
= ctx
.getSharedPreferences(PREFS_NAME
, Context
.MODE_PRIVATE
);
152 SharedPreferences
.Editor editor
= settings
.edit();
154 editor
.putBoolean("on_boot", on_boot
);
158 private synchronized void extractModule() {
159 File debounce_ko
= new File(ctx
.getFilesDir() + "/debounce.ko");
161 if (debounce_ko
.exists()) {
166 InputStream apk
= ctx
.getAssets().open("debounce.ko");
167 OutputStream mod
= ctx
.openFileOutput("debounce.ko.tmp", 0);
169 //I assume a page is 4k...
170 byte buf
[] = new byte[4096];
173 while((bytes
= apk
.read(buf
)) != -1) {
174 mod
.write(buf
, 0, bytes
);
180 File tmpfile
= new File(debounce_ko
+ ".tmp");
181 tmpfile
.renameTo(debounce_ko
);
182 } catch (Exception e
) {}