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() throws NotRootedException
,ShellException
{
44 protected void runAsRoot(String command
) throws NotRootedException
,ShellException
{
48 rootcmd
= Runtime
.getRuntime().exec(new String
[]{"su","-c","sh"});
49 } catch (java
.io
.IOException e
) {
50 throw new NotRootedException();
54 DataOutputStream sh
= new DataOutputStream(rootcmd
.getOutputStream());
55 sh
.writeBytes(command
+ "\n");
56 sh
.writeBytes("exit\n");
59 } catch (java
.io
.IOException e
) {
60 throw new ShellException();
64 if (rootcmd
.waitFor() != 0)
65 throw new ShellException();
66 } catch (java
.lang
.InterruptedException e
) {
67 throw new ShellException();
71 public synchronized boolean _loadModule() throws NotRootedException
,ShellException
{
72 File debounce_ko
= new File(ctx
.getFilesDir() + "/debounce.ko");
76 SharedPreferences settings
= ctx
.getSharedPreferences(PREFS_NAME
, Context
.MODE_PRIVATE
);
77 SharedPreferences
.Editor editor
= settings
.edit();
78 if (is_safe_to_load()) {
79 editor
.putBoolean("safe_to_load", false);
83 runAsRoot("/system/bin/insmod " + debounce_ko
+ " || /system/xbin/insmod " + debounce_ko
);
93 /* Module was obviously loaded, so it is safe to load on boot */
94 editor
.putBoolean("safe_to_load", true);
100 public synchronized void unloadModule() throws NotRootedException
,ShellException
{
101 runAsRoot("/system/bin/rmmod debounce || /system/xbin/rmmod debounce");
104 public synchronized boolean isLoaded() {
105 boolean loaded
= false;
109 FileReader modules
= new FileReader("/proc/modules");
110 BufferedReader modules_buf
= new BufferedReader(modules
);
112 while((read
= modules_buf
.readLine()) != null) {
113 if (read
.regionMatches(0, "debounce", 0, 8)) {
118 } catch (Exception e
) {
125 private synchronized int getValue(String parameter
) {
131 FileReader fr
= new FileReader("/sys/devices/debounce/" + parameter
);
132 BufferedReader fbuf
= new BufferedReader(fr
);
134 read
= fbuf
.readLine();
136 value
= Integer
.parseInt(read
.trim());
140 } catch (Exception e
) {}
145 private synchronized void setValue(String parameter
, int value
) {
151 FileWriter fw
= new FileWriter("/sys/devices/debounce/" + parameter
);
152 BufferedWriter fbuf
= new BufferedWriter(fw
);
154 fbuf
.write((new Integer(value
)).toString());
157 } catch (Exception e
) {}
160 public synchronized int getDelay() {
161 return getValue("debounce_delay");
164 public synchronized void setDelay(int debounce_delay
) {
165 setValue("debounce_delay", debounce_delay
);
168 public synchronized int getSettle() {
169 return getValue("settle_time");
172 public synchronized void setSettle(int settle_time
) {
173 setValue("settle_time", settle_time
);
176 public synchronized int getPoll() {
177 return getValue("poll_time");
180 public synchronized void setPoll(int poll_time
) {
181 setValue("poll_time", poll_time
);
184 public synchronized boolean getHwDebounce() {
185 if (getValue("hw_debounce") == 1)
191 public synchronized void setHwDebounce(boolean enable
) {
193 setValue("hw_debounce", 1);
195 setValue("hw_debounce", 0);
198 public synchronized int getHwDebounceTime() {
199 return getValue("hw_debounce_time");
202 public synchronized void setHwDebounceTime(int time
) {
203 setValue("hw_debounce_time", time
);
206 public synchronized boolean getDriveInactive() {
207 if (getValue("drive_inactive_flag") == 1)
213 public synchronized void setDriveInactive(boolean enable
) {
215 setValue("drive_inactive_flag", 1);
217 setValue("drive_inactive_flag", 0);
220 public synchronized boolean getActiveHigh() {
221 if (getValue("active_high_flag") == 1)
227 public synchronized void setActiveHigh(boolean enable
) {
229 setValue("active_high_flag", 1);
231 setValue("active_high_flag", 0);
234 public synchronized int getSavedDelay() {
235 SharedPreferences settings
= ctx
.getSharedPreferences(PREFS_NAME
, Context
.MODE_PRIVATE
);
237 return settings
.getInt("debounce_delay", 15);
240 public synchronized void setSavedDelay(int delay
) {
241 SharedPreferences settings
= ctx
.getSharedPreferences(PREFS_NAME
, Context
.MODE_PRIVATE
);
242 SharedPreferences
.Editor editor
= settings
.edit();
244 editor
.putInt("debounce_delay", delay
);
248 public synchronized int getSavedSettle() {
249 SharedPreferences settings
= ctx
.getSharedPreferences(PREFS_NAME
, Context
.MODE_PRIVATE
);
251 return settings
.getInt("settle_time", 40);
254 public synchronized void setSavedSettle(int settle
) {
255 SharedPreferences settings
= ctx
.getSharedPreferences(PREFS_NAME
, Context
.MODE_PRIVATE
);
256 SharedPreferences
.Editor editor
= settings
.edit();
258 editor
.putInt("settle_time", settle
);
262 public synchronized int getSavedPoll() {
263 SharedPreferences settings
= ctx
.getSharedPreferences(PREFS_NAME
, Context
.MODE_PRIVATE
);
265 return settings
.getInt("poll_time", 20);
268 public synchronized void setSavedPoll(int poll
) {
269 SharedPreferences settings
= ctx
.getSharedPreferences(PREFS_NAME
, Context
.MODE_PRIVATE
);
270 SharedPreferences
.Editor editor
= settings
.edit();
272 editor
.putInt("poll_time", poll
);
276 public synchronized boolean getSavedHwDebounce() {
277 SharedPreferences settings
= ctx
.getSharedPreferences(PREFS_NAME
, Context
.MODE_PRIVATE
);
279 return settings
.getBoolean("hw_debounce", false);
282 public synchronized void setSavedHwDebounce(boolean enable
) {
283 SharedPreferences settings
= ctx
.getSharedPreferences(PREFS_NAME
, Context
.MODE_PRIVATE
);
284 SharedPreferences
.Editor editor
= settings
.edit();
286 editor
.putBoolean("hw_debounce", enable
);
290 public synchronized int getSavedHwDebounceTime() {
291 SharedPreferences settings
= ctx
.getSharedPreferences(PREFS_NAME
, Context
.MODE_PRIVATE
);
293 return settings
.getInt("hw_debounce_time", 1);
296 public synchronized void setSavedHwDebounceTime(int time
) {
297 SharedPreferences settings
= ctx
.getSharedPreferences(PREFS_NAME
, Context
.MODE_PRIVATE
);
298 SharedPreferences
.Editor editor
= settings
.edit();
300 editor
.putInt("hw_debounce_time", time
);
304 public synchronized boolean getSavedDriveInactive() {
305 SharedPreferences settings
= ctx
.getSharedPreferences(PREFS_NAME
, Context
.MODE_PRIVATE
);
307 return settings
.getBoolean("drive_inactive", false);
310 public synchronized void setSavedDriveInactive(boolean enable
) {
311 SharedPreferences settings
= ctx
.getSharedPreferences(PREFS_NAME
, Context
.MODE_PRIVATE
);
312 SharedPreferences
.Editor editor
= settings
.edit();
314 editor
.putBoolean("drive_inactive", enable
);
318 public synchronized boolean getSavedActiveHigh() {
319 SharedPreferences settings
= ctx
.getSharedPreferences(PREFS_NAME
, Context
.MODE_PRIVATE
);
321 return settings
.getBoolean("active_high", false);
324 public synchronized void setSavedActiveHigh(boolean enable
) {
325 SharedPreferences settings
= ctx
.getSharedPreferences(PREFS_NAME
, Context
.MODE_PRIVATE
);
326 SharedPreferences
.Editor editor
= settings
.edit();
328 editor
.putBoolean("active_high", enable
);
332 public synchronized boolean is_safe_to_load() {
333 SharedPreferences settings
= ctx
.getSharedPreferences(PREFS_NAME
, Context
.MODE_PRIVATE
);
334 return settings
.getBoolean("safe_to_load", false);
337 public synchronized boolean get_on_boot() {
338 SharedPreferences settings
= ctx
.getSharedPreferences(PREFS_NAME
, Context
.MODE_PRIVATE
);
339 return settings
.getBoolean("on_boot", false);
342 public synchronized void set_on_boot(boolean on_boot
) {
343 SharedPreferences settings
= ctx
.getSharedPreferences(PREFS_NAME
, Context
.MODE_PRIVATE
);
344 SharedPreferences
.Editor editor
= settings
.edit();
346 editor
.putBoolean("on_boot", on_boot
);
350 private synchronized void extractModule() {
351 File debounce_ko
= new File(ctx
.getFilesDir() + "/debounce.ko");
353 if (debounce_ko
.exists()) {
358 InputStream apk
= ctx
.getAssets().open("debounce.ko");
359 OutputStream mod
= ctx
.openFileOutput("debounce.ko.tmp", 0);
361 //I assume a page is 4k...
362 byte buf
[] = new byte[4096];
365 while((bytes
= apk
.read(buf
)) != -1) {
366 mod
.write(buf
, 0, bytes
);
372 File tmpfile
= new File(debounce_ko
+ ".tmp");
373 tmpfile
.renameTo(debounce_ko
);
374 } catch (Exception e
) {}