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 boolean loadModule() {
44 protected void runAsRoot(String command
) throws java
.io
.IOException
,java
.lang
.InterruptedException
{
45 Process rootcmd
= Runtime
.getRuntime().exec(new String
[]{"su","-c","sh"});
46 DataOutputStream sh
= new DataOutputStream(rootcmd
.getOutputStream());
47 sh
.writeBytes(command
+ "\n");
48 sh
.writeBytes("exit\n");
55 public synchronized boolean _loadModule() {
56 File debounce_ko
= new File(ctx
.getFilesDir() + "/debounce.ko");
60 SharedPreferences settings
= ctx
.getSharedPreferences(PREFS_NAME
, Context
.MODE_PRIVATE
);
61 SharedPreferences
.Editor editor
= settings
.edit();
62 if (is_safe_to_load()) {
63 editor
.putBoolean("safe_to_load", false);
68 runAsRoot("/system/bin/insmod " + debounce_ko
+ " || /system/xbin/insmod " + debounce_ko
);
69 } catch (Exception e
) {
81 /* Module was obviously loaded, so it is safe to load on boot */
82 editor
.putBoolean("safe_to_load", true);
88 public synchronized boolean unloadModule() {
90 runAsRoot("/system/bin/rmmod debounce || /system/xbin/rmmod debounce");
91 } catch (Exception e
) {
98 public synchronized boolean isLoaded() {
99 boolean loaded
= false;
103 FileReader modules
= new FileReader("/proc/modules");
104 BufferedReader modules_buf
= new BufferedReader(modules
);
106 while((read
= modules_buf
.readLine()) != null) {
107 if (read
.regionMatches(0, "debounce", 0, 8)) {
112 } catch (Exception e
) {
119 private synchronized int getValue(String parameter
) {
125 FileReader fr
= new FileReader("/sys/devices/debounce/" + parameter
);
126 BufferedReader fbuf
= new BufferedReader(fr
);
128 read
= fbuf
.readLine();
130 value
= Integer
.parseInt(read
.trim());
134 } catch (Exception e
) {}
139 private synchronized void setValue(String parameter
, int value
) {
145 FileWriter fw
= new FileWriter("/sys/devices/debounce/" + parameter
);
146 BufferedWriter fbuf
= new BufferedWriter(fw
);
148 fbuf
.write((new Integer(value
)).toString());
151 } catch (Exception e
) {
155 public synchronized int getDelay() {
156 return getValue("debounce_delay");
159 public synchronized void setDelay(int debounce_delay
) {
160 setValue("debounce_delay", debounce_delay
);
163 public synchronized int getSettle() {
164 return getValue("settle_time");
167 public synchronized void setSettle(int settle_time
) {
168 setValue("settle_time", settle_time
);
171 public synchronized int getPoll() {
172 return getValue("poll_time");
175 public synchronized void setPoll(int poll_time
) {
176 setValue("poll_time", poll_time
);
179 public synchronized boolean getHwDebounce() {
180 if (getValue("hw_debounce") == 1)
186 public synchronized void setHwDebounce(boolean enable
) {
188 setValue("hw_debounce", 1);
190 setValue("hw_debounce", 0);
193 public synchronized int getHwDebounceTime() {
194 return getValue("hw_debounce_time");
197 public synchronized void setHwDebounceTime(int time
) {
198 setValue("hw_debounce_time", time
);
201 public synchronized boolean getDriveInactive() {
202 if (getValue("drive_inactive_flag") == 1)
208 public synchronized void setDriveInactive(boolean enable
) {
210 setValue("drive_inactive_flag", 1);
212 setValue("drive_inactive_flag", 0);
215 public synchronized boolean getActiveHigh() {
216 if (getValue("active_high_flag") == 1)
222 public synchronized void setActiveHigh(boolean enable
) {
224 setValue("active_high_flag", 1);
226 setValue("active_high_flag", 0);
229 public synchronized int getSavedDelay() {
230 SharedPreferences settings
= ctx
.getSharedPreferences(PREFS_NAME
, Context
.MODE_PRIVATE
);
232 return settings
.getInt("debounce_delay", 15);
235 public synchronized void setSavedDelay(int delay
) {
236 SharedPreferences settings
= ctx
.getSharedPreferences(PREFS_NAME
, Context
.MODE_PRIVATE
);
237 SharedPreferences
.Editor editor
= settings
.edit();
239 editor
.putInt("debounce_delay", delay
);
243 public synchronized int getSavedSettle() {
244 SharedPreferences settings
= ctx
.getSharedPreferences(PREFS_NAME
, Context
.MODE_PRIVATE
);
246 return settings
.getInt("settle_time", 40);
249 public synchronized void setSavedSettle(int settle
) {
250 SharedPreferences settings
= ctx
.getSharedPreferences(PREFS_NAME
, Context
.MODE_PRIVATE
);
251 SharedPreferences
.Editor editor
= settings
.edit();
253 editor
.putInt("settle_time", settle
);
257 public synchronized int getSavedPoll() {
258 SharedPreferences settings
= ctx
.getSharedPreferences(PREFS_NAME
, Context
.MODE_PRIVATE
);
260 return settings
.getInt("poll_time", 20);
263 public synchronized void setSavedPoll(int poll
) {
264 SharedPreferences settings
= ctx
.getSharedPreferences(PREFS_NAME
, Context
.MODE_PRIVATE
);
265 SharedPreferences
.Editor editor
= settings
.edit();
267 editor
.putInt("poll_time", poll
);
271 public synchronized boolean getSavedHwDebounce() {
272 SharedPreferences settings
= ctx
.getSharedPreferences(PREFS_NAME
, Context
.MODE_PRIVATE
);
274 return settings
.getBoolean("hw_debounce", false);
277 public synchronized void setSavedHwDebounce(boolean enable
) {
278 SharedPreferences settings
= ctx
.getSharedPreferences(PREFS_NAME
, Context
.MODE_PRIVATE
);
279 SharedPreferences
.Editor editor
= settings
.edit();
281 editor
.putBoolean("hw_debounce", enable
);
285 public synchronized int getSavedHwDebounceTime() {
286 SharedPreferences settings
= ctx
.getSharedPreferences(PREFS_NAME
, Context
.MODE_PRIVATE
);
288 return settings
.getInt("hw_debounce_time", 1);
291 public synchronized void setSavedHwDebounceTime(int time
) {
292 SharedPreferences settings
= ctx
.getSharedPreferences(PREFS_NAME
, Context
.MODE_PRIVATE
);
293 SharedPreferences
.Editor editor
= settings
.edit();
295 editor
.putInt("hw_debounce_time", time
);
299 public synchronized boolean getSavedDriveInactive() {
300 SharedPreferences settings
= ctx
.getSharedPreferences(PREFS_NAME
, Context
.MODE_PRIVATE
);
302 return settings
.getBoolean("drive_inactive", false);
305 public synchronized void setSavedDriveInactive(boolean enable
) {
306 SharedPreferences settings
= ctx
.getSharedPreferences(PREFS_NAME
, Context
.MODE_PRIVATE
);
307 SharedPreferences
.Editor editor
= settings
.edit();
309 editor
.putBoolean("drive_inactive", enable
);
313 public synchronized boolean getSavedActiveHigh() {
314 SharedPreferences settings
= ctx
.getSharedPreferences(PREFS_NAME
, Context
.MODE_PRIVATE
);
316 return settings
.getBoolean("active_high", false);
319 public synchronized void setSavedActiveHigh(boolean enable
) {
320 SharedPreferences settings
= ctx
.getSharedPreferences(PREFS_NAME
, Context
.MODE_PRIVATE
);
321 SharedPreferences
.Editor editor
= settings
.edit();
323 editor
.putBoolean("active_high", enable
);
327 public synchronized boolean is_safe_to_load() {
328 SharedPreferences settings
= ctx
.getSharedPreferences(PREFS_NAME
, Context
.MODE_PRIVATE
);
329 return settings
.getBoolean("safe_to_load", false);
332 public synchronized boolean get_on_boot() {
333 SharedPreferences settings
= ctx
.getSharedPreferences(PREFS_NAME
, Context
.MODE_PRIVATE
);
334 return settings
.getBoolean("on_boot", false);
337 public synchronized void set_on_boot(boolean on_boot
) {
338 SharedPreferences settings
= ctx
.getSharedPreferences(PREFS_NAME
, Context
.MODE_PRIVATE
);
339 SharedPreferences
.Editor editor
= settings
.edit();
341 editor
.putBoolean("on_boot", on_boot
);
345 private synchronized void extractModule() {
346 File debounce_ko
= new File(ctx
.getFilesDir() + "/debounce.ko");
348 if (debounce_ko
.exists()) {
353 InputStream apk
= ctx
.getAssets().open("debounce.ko");
354 OutputStream mod
= ctx
.openFileOutput("debounce.ko.tmp", 0);
356 //I assume a page is 4k...
357 byte buf
[] = new byte[4096];
360 while((bytes
= apk
.read(buf
)) != -1) {
361 mod
.write(buf
, 0, bytes
);
367 File tmpfile
= new File(debounce_ko
+ ".tmp");
368 tmpfile
.renameTo(debounce_ko
);
369 } catch (Exception e
) {}