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());
32 setActiveHigh(getSavedActiveHigh());
35 public void loadModule() {
40 protected void runAsRoot(String command
) throws java
.io
.IOException
,java
.lang
.InterruptedException
{
41 Process rootcmd
= Runtime
.getRuntime().exec(new String
[]{"su","-c","sh"});
42 DataOutputStream sh
= new DataOutputStream(rootcmd
.getOutputStream());
43 sh
.writeBytes(command
+ "\n");
44 sh
.writeBytes("exit\n");
51 public synchronized void _loadModule() {
52 File debounce_ko
= new File(ctx
.getFilesDir() + "/debounce.ko");
56 SharedPreferences settings
= ctx
.getSharedPreferences(PREFS_NAME
, Context
.MODE_PRIVATE
);
57 SharedPreferences
.Editor editor
= settings
.edit();
58 if (is_safe_to_load()) {
59 editor
.putBoolean("safe_to_load", false);
64 runAsRoot("/system/bin/insmod " + debounce_ko
);
65 } catch (Exception e
) {
77 /* Module was obviously loaded, so it is safe to load on boot */
78 editor
.putBoolean("safe_to_load", true);
82 public synchronized void unloadModule() {
84 runAsRoot("/system/bin/rmmod debounce");
85 } catch (Exception e
) {}
88 public synchronized boolean isLoaded() {
89 boolean loaded
= false;
93 FileReader modules
= new FileReader("/proc/modules");
94 BufferedReader modules_buf
= new BufferedReader(modules
);
96 while((read
= modules_buf
.readLine()) != null) {
97 if (read
.regionMatches(0, "debounce", 0, 8)) {
102 } catch (Exception e
) {
109 private synchronized int getValue(String parameter
) {
115 FileReader fr
= new FileReader("/sys/devices/debounce/" + parameter
);
116 BufferedReader fbuf
= new BufferedReader(fr
);
118 read
= fbuf
.readLine();
120 value
= Integer
.parseInt(read
.trim());
124 } catch (Exception e
) {}
129 private synchronized void setValue(String parameter
, int value
) {
135 FileWriter fw
= new FileWriter("/sys/devices/debounce/" + parameter
);
136 BufferedWriter fbuf
= new BufferedWriter(fw
);
138 fbuf
.write((new Integer(value
)).toString());
141 } catch (Exception e
) {}
144 public synchronized int getDelay() {
145 return getValue("debounce_delay");
148 public synchronized void setDelay(int debounce_delay
) {
149 setValue("debounce_delay", debounce_delay
);
152 public synchronized int getSettle() {
153 return getValue("settle_time");
156 public synchronized void setSettle(int settle_time
) {
157 setValue("settle_time", settle_time
);
160 public synchronized int getPoll() {
161 return getValue("poll_time");
164 public synchronized void setPoll(int poll_time
) {
165 setValue("poll_time", poll_time
);
168 public synchronized boolean getHwDebounce() {
169 if (getValue("hw_debounce") == 1)
175 public synchronized void setHwDebounce(boolean enable
) {
177 setValue("hw_debounce", 1);
179 setValue("hw_debounce", 0);
182 public synchronized int getHwDebounceTime() {
183 return getValue("hw_debounce_time");
186 public synchronized void setHwDebounceTime(int time
) {
187 setValue("hw_debounce_time", time
);
190 public synchronized boolean getDriveInactive() {
191 if (getValue("drive_inactive_flag") == 1)
197 public synchronized void setDriveInactive(boolean enable
) {
199 setValue("drive_inactive_flag", 1);
201 setValue("drive_inactive_flag", 0);
204 public synchronized boolean getActiveHigh() {
205 if (getValue("active_high_flag") == 1)
211 public synchronized void setActiveHigh(boolean enable
) {
213 setValue("active_high_flag", 1);
215 setValue("active_high_flag", 0);
218 public synchronized int getSavedDelay() {
219 SharedPreferences settings
= ctx
.getSharedPreferences(PREFS_NAME
, Context
.MODE_PRIVATE
);
221 return settings
.getInt("debounce_delay", 15);
224 public synchronized void setSavedDelay(int delay
) {
225 SharedPreferences settings
= ctx
.getSharedPreferences(PREFS_NAME
, Context
.MODE_PRIVATE
);
226 SharedPreferences
.Editor editor
= settings
.edit();
228 editor
.putInt("debounce_delay", delay
);
232 public synchronized int getSavedSettle() {
233 SharedPreferences settings
= ctx
.getSharedPreferences(PREFS_NAME
, Context
.MODE_PRIVATE
);
235 return settings
.getInt("settle_time", 40);
238 public synchronized void setSavedSettle(int settle
) {
239 SharedPreferences settings
= ctx
.getSharedPreferences(PREFS_NAME
, Context
.MODE_PRIVATE
);
240 SharedPreferences
.Editor editor
= settings
.edit();
242 editor
.putInt("settle_time", settle
);
246 public synchronized int getSavedPoll() {
247 SharedPreferences settings
= ctx
.getSharedPreferences(PREFS_NAME
, Context
.MODE_PRIVATE
);
249 return settings
.getInt("poll_time", 20);
252 public synchronized void setSavedPoll(int poll
) {
253 SharedPreferences settings
= ctx
.getSharedPreferences(PREFS_NAME
, Context
.MODE_PRIVATE
);
254 SharedPreferences
.Editor editor
= settings
.edit();
256 editor
.putInt("poll_time", poll
);
260 public synchronized boolean getSavedHwDebounce() {
261 SharedPreferences settings
= ctx
.getSharedPreferences(PREFS_NAME
, Context
.MODE_PRIVATE
);
263 return settings
.getBoolean("hw_debounce", false);
266 public synchronized void setSavedHwDebounce(boolean enable
) {
267 SharedPreferences settings
= ctx
.getSharedPreferences(PREFS_NAME
, Context
.MODE_PRIVATE
);
268 SharedPreferences
.Editor editor
= settings
.edit();
270 editor
.putBoolean("hw_debounce", enable
);
274 public synchronized int getSavedHwDebounceTime() {
275 SharedPreferences settings
= ctx
.getSharedPreferences(PREFS_NAME
, Context
.MODE_PRIVATE
);
277 return settings
.getInt("hw_debounce_time", 1);
280 public synchronized void setSavedHwDebounceTime(int time
) {
281 SharedPreferences settings
= ctx
.getSharedPreferences(PREFS_NAME
, Context
.MODE_PRIVATE
);
282 SharedPreferences
.Editor editor
= settings
.edit();
284 editor
.putInt("hw_debounce_time", time
);
288 public synchronized boolean getSavedDriveInactive() {
289 SharedPreferences settings
= ctx
.getSharedPreferences(PREFS_NAME
, Context
.MODE_PRIVATE
);
291 return settings
.getBoolean("drive_inactive", false);
294 public synchronized void setSavedDriveInactive(boolean enable
) {
295 SharedPreferences settings
= ctx
.getSharedPreferences(PREFS_NAME
, Context
.MODE_PRIVATE
);
296 SharedPreferences
.Editor editor
= settings
.edit();
298 editor
.putBoolean("drive_inactive", enable
);
302 public synchronized boolean getSavedActiveHigh() {
303 SharedPreferences settings
= ctx
.getSharedPreferences(PREFS_NAME
, Context
.MODE_PRIVATE
);
305 return settings
.getBoolean("active_high", false);
308 public synchronized void setSavedActiveHigh(boolean enable
) {
309 SharedPreferences settings
= ctx
.getSharedPreferences(PREFS_NAME
, Context
.MODE_PRIVATE
);
310 SharedPreferences
.Editor editor
= settings
.edit();
312 editor
.putBoolean("active_high", enable
);
316 public synchronized boolean is_safe_to_load() {
317 SharedPreferences settings
= ctx
.getSharedPreferences(PREFS_NAME
, Context
.MODE_PRIVATE
);
318 return settings
.getBoolean("safe_to_load", false);
321 public synchronized boolean get_on_boot() {
322 SharedPreferences settings
= ctx
.getSharedPreferences(PREFS_NAME
, Context
.MODE_PRIVATE
);
323 return settings
.getBoolean("on_boot", false);
326 public synchronized void set_on_boot(boolean on_boot
) {
327 SharedPreferences settings
= ctx
.getSharedPreferences(PREFS_NAME
, Context
.MODE_PRIVATE
);
328 SharedPreferences
.Editor editor
= settings
.edit();
330 editor
.putBoolean("on_boot", on_boot
);
334 private synchronized void extractModule() {
335 File debounce_ko
= new File(ctx
.getFilesDir() + "/debounce.ko");
337 if (debounce_ko
.exists()) {
342 InputStream apk
= ctx
.getAssets().open("debounce.ko");
343 OutputStream mod
= ctx
.openFileOutput("debounce.ko.tmp", 0);
345 //I assume a page is 4k...
346 byte buf
[] = new byte[4096];
349 while((bytes
= apk
.read(buf
)) != -1) {
350 mod
.write(buf
, 0, bytes
);
356 File tmpfile
= new File(debounce_ko
+ ".tmp");
357 tmpfile
.renameTo(debounce_ko
);
358 } catch (Exception e
) {}