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 loadModule() {
27 setDelay(getSavedDelay());
30 protected void runAsRoot(String command
) throws java
.io
.IOException
,java
.lang
.InterruptedException
{
31 Process rootcmd
= Runtime
.getRuntime().exec(new String
[]{"su","-c","sh"});
32 DataOutputStream sh
= new DataOutputStream(rootcmd
.getOutputStream());
33 sh
.writeBytes(command
+ "\n");
34 sh
.writeBytes("exit\n");
41 public synchronized void _loadModule() {
42 File debounce_ko
= new File(ctx
.getFilesDir() + "/debounce.ko");
46 SharedPreferences settings
= ctx
.getSharedPreferences(PREFS_NAME
, Context
.MODE_PRIVATE
);
47 SharedPreferences
.Editor editor
= settings
.edit();
48 if (is_safe_to_load()) {
49 editor
.putBoolean("safe_to_load", false);
54 runAsRoot("/system/bin/insmod " + debounce_ko
);
55 } catch (Exception e
) {
67 /* Module was obviously loaded, so it is safe to load on boot */
68 editor
.putBoolean("safe_to_load", true);
72 public synchronized void unloadModule() {
74 runAsRoot("/system/bin/rmmod debounce");
75 } catch (Exception e
) {}
78 public synchronized boolean isLoaded() {
79 boolean loaded
= false;
83 FileReader modules
= new FileReader("/proc/modules");
84 BufferedReader modules_buf
= new BufferedReader(modules
);
86 while((read
= modules_buf
.readLine()) != null) {
87 if (read
.regionMatches(0, "debounce", 0, 8)) {
92 } catch (Exception e
) {
99 public synchronized int getDelay() {
100 int debounce_delay
= -1;
105 FileReader delay
= new FileReader("/sys/devices/debounce/debounce_delay");
106 BufferedReader delay_buf
= new BufferedReader(delay
);
108 read
= delay_buf
.readLine();
110 debounce_delay
= Integer
.parseInt(read
.trim());
114 } catch (Exception e
) {}
116 return debounce_delay
;
119 public synchronized void setDelay(int debounce_delay
) {
125 FileWriter delay
= new FileWriter("/sys/devices/debounce/debounce_delay");
126 BufferedWriter delay_buf
= new BufferedWriter(delay
);
128 delay_buf
.write((new Integer(debounce_delay
)).toString());
131 } catch (Exception e
) {}
134 public synchronized int getSavedDelay() {
135 SharedPreferences settings
= ctx
.getSharedPreferences(PREFS_NAME
, Context
.MODE_PRIVATE
);
137 return settings
.getInt("debounce_delay", 8);
140 public synchronized void setSavedDelay(int delay
) {
141 SharedPreferences settings
= ctx
.getSharedPreferences(PREFS_NAME
, Context
.MODE_PRIVATE
);
142 SharedPreferences
.Editor editor
= settings
.edit();
144 editor
.putInt("debounce_delay", delay
);
148 public synchronized boolean is_safe_to_load() {
149 SharedPreferences settings
= ctx
.getSharedPreferences(PREFS_NAME
, Context
.MODE_PRIVATE
);
150 return settings
.getBoolean("safe_to_load", false);
153 public synchronized boolean get_on_boot() {
154 SharedPreferences settings
= ctx
.getSharedPreferences(PREFS_NAME
, Context
.MODE_PRIVATE
);
155 return settings
.getBoolean("on_boot", false);
158 public synchronized void set_on_boot(boolean on_boot
) {
159 SharedPreferences settings
= ctx
.getSharedPreferences(PREFS_NAME
, Context
.MODE_PRIVATE
);
160 SharedPreferences
.Editor editor
= settings
.edit();
162 editor
.putBoolean("on_boot", on_boot
);
166 private synchronized void extractModule() {
167 File debounce_ko
= new File(ctx
.getFilesDir() + "/debounce.ko");
169 if (debounce_ko
.exists()) {
174 InputStream apk
= ctx
.getAssets().open("debounce.ko");
175 OutputStream mod
= ctx
.openFileOutput("debounce.ko.tmp", 0);
177 //I assume a page is 4k...
178 byte buf
[] = new byte[4096];
181 while((bytes
= apk
.read(buf
)) != -1) {
182 mod
.write(buf
, 0, bytes
);
188 File tmpfile
= new File(debounce_ko
+ ".tmp");
189 tmpfile
.renameTo(debounce_ko
);
190 } catch (Exception e
) {}