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 insmod
= new File("/system/bin/insmod");
73 if (!insmod
.exists()) {
74 insmod
= new File("/system/xbin/insmod");
75 if (!insmod
.exists()) {
80 File debounce_ko
= new File("/system/lib/modules/debounce.ko");
81 if (!debounce_ko
.exists()) {
82 debounce_ko
= new File(ctx
.getFilesDir() + "/debounce.ko");
87 SharedPreferences settings
= ctx
.getSharedPreferences(PREFS_NAME
, Context
.MODE_PRIVATE
);
88 SharedPreferences
.Editor editor
= settings
.edit();
89 if (is_safe_to_load()) {
90 editor
.putBoolean("safe_to_load", false);
94 runAsRoot(insmod
+ " " + debounce_ko
);
97 while ((!isLoaded()) && (cnt
> 0)) {
100 } catch (Exception e
) {
110 if (getDelay() < 0) {
114 /* Module was obviously loaded, so it is safe to load on boot */
115 editor
.putBoolean("safe_to_load", true);
121 public synchronized void unloadModule() throws NotRootedException
,ShellException
{
122 File rmmod
= new File("/system/bin/rmmod");
124 if (!rmmod
.exists()) {
125 rmmod
= new File("/system/xbin/rmmod");
126 if (!rmmod
.exists()) {
131 runAsRoot(rmmod
+ " debounce");
134 public synchronized boolean isLoaded() {
135 boolean loaded
= false;
140 FileReader modules
= new FileReader("/proc/modules");
141 BufferedReader modules_buf
= new BufferedReader(modules
);
143 while((read
= modules_buf
.readLine()) != null) {
144 if (read
.regionMatches(0, "debounce", 0, 8)) {
145 File sysdir
= new File("/sys/devices/debounce");
146 if (sysdir
.exists() && sysdir
.isDirectory()) {
152 } catch (Exception e
) {
159 private synchronized int getValue(String parameter
) {
165 FileReader fr
= new FileReader("/sys/devices/debounce/" + parameter
);
166 BufferedReader fbuf
= new BufferedReader(fr
);
168 read
= fbuf
.readLine();
170 value
= Integer
.parseInt(read
.trim());
174 } catch (Exception e
) {}
179 private synchronized void setValue(String parameter
, int value
) {
185 FileWriter fw
= new FileWriter("/sys/devices/debounce/" + parameter
);
186 BufferedWriter fbuf
= new BufferedWriter(fw
);
188 fbuf
.write((new Integer(value
)).toString());
191 } catch (Exception e
) {}
194 public synchronized int getDelay() {
195 return getValue("debounce_delay");
198 public synchronized void setDelay(int debounce_delay
) {
199 setValue("debounce_delay", debounce_delay
);
202 public synchronized int getSettle() {
203 return getValue("settle_time");
206 public synchronized void setSettle(int settle_time
) {
207 setValue("settle_time", settle_time
);
210 public synchronized int getPoll() {
211 return getValue("poll_time");
214 public synchronized void setPoll(int poll_time
) {
215 setValue("poll_time", poll_time
);
218 public synchronized boolean getHwDebounce() {
219 if (getValue("hw_debounce") == 1)
225 public synchronized void setHwDebounce(boolean enable
) {
227 setValue("hw_debounce", 1);
229 setValue("hw_debounce", 0);
232 public synchronized int getHwDebounceTime() {
233 return getValue("hw_debounce_time");
236 public synchronized void setHwDebounceTime(int time
) {
237 setValue("hw_debounce_time", time
);
240 public synchronized boolean getDriveInactive() {
241 if (getValue("drive_inactive_flag") == 1)
247 public synchronized void setDriveInactive(boolean enable
) {
249 setValue("drive_inactive_flag", 1);
251 setValue("drive_inactive_flag", 0);
254 public synchronized boolean getActiveHigh() {
255 if (getValue("active_high_flag") == 1)
261 public synchronized void setActiveHigh(boolean enable
) {
263 setValue("active_high_flag", 1);
265 setValue("active_high_flag", 0);
268 public synchronized int getSavedDelay() {
269 SharedPreferences settings
= ctx
.getSharedPreferences(PREFS_NAME
, Context
.MODE_PRIVATE
);
271 return settings
.getInt("debounce_delay", 15);
274 public synchronized void setSavedDelay(int delay
) {
275 SharedPreferences settings
= ctx
.getSharedPreferences(PREFS_NAME
, Context
.MODE_PRIVATE
);
276 SharedPreferences
.Editor editor
= settings
.edit();
278 editor
.putInt("debounce_delay", delay
);
282 public synchronized int getSavedSettle() {
283 SharedPreferences settings
= ctx
.getSharedPreferences(PREFS_NAME
, Context
.MODE_PRIVATE
);
285 return settings
.getInt("settle_time", 40);
288 public synchronized void setSavedSettle(int settle
) {
289 SharedPreferences settings
= ctx
.getSharedPreferences(PREFS_NAME
, Context
.MODE_PRIVATE
);
290 SharedPreferences
.Editor editor
= settings
.edit();
292 editor
.putInt("settle_time", settle
);
296 public synchronized int getSavedPoll() {
297 SharedPreferences settings
= ctx
.getSharedPreferences(PREFS_NAME
, Context
.MODE_PRIVATE
);
299 return settings
.getInt("poll_time", 20);
302 public synchronized void setSavedPoll(int poll
) {
303 SharedPreferences settings
= ctx
.getSharedPreferences(PREFS_NAME
, Context
.MODE_PRIVATE
);
304 SharedPreferences
.Editor editor
= settings
.edit();
306 editor
.putInt("poll_time", poll
);
310 public synchronized boolean getSavedHwDebounce() {
311 SharedPreferences settings
= ctx
.getSharedPreferences(PREFS_NAME
, Context
.MODE_PRIVATE
);
313 return settings
.getBoolean("hw_debounce", false);
316 public synchronized void setSavedHwDebounce(boolean enable
) {
317 SharedPreferences settings
= ctx
.getSharedPreferences(PREFS_NAME
, Context
.MODE_PRIVATE
);
318 SharedPreferences
.Editor editor
= settings
.edit();
320 editor
.putBoolean("hw_debounce", enable
);
324 public synchronized int getSavedHwDebounceTime() {
325 SharedPreferences settings
= ctx
.getSharedPreferences(PREFS_NAME
, Context
.MODE_PRIVATE
);
327 return settings
.getInt("hw_debounce_time", 1);
330 public synchronized void setSavedHwDebounceTime(int time
) {
331 SharedPreferences settings
= ctx
.getSharedPreferences(PREFS_NAME
, Context
.MODE_PRIVATE
);
332 SharedPreferences
.Editor editor
= settings
.edit();
334 editor
.putInt("hw_debounce_time", time
);
338 public synchronized boolean getSavedDriveInactive() {
339 SharedPreferences settings
= ctx
.getSharedPreferences(PREFS_NAME
, Context
.MODE_PRIVATE
);
341 return settings
.getBoolean("drive_inactive", false);
344 public synchronized void setSavedDriveInactive(boolean enable
) {
345 SharedPreferences settings
= ctx
.getSharedPreferences(PREFS_NAME
, Context
.MODE_PRIVATE
);
346 SharedPreferences
.Editor editor
= settings
.edit();
348 editor
.putBoolean("drive_inactive", enable
);
352 public synchronized boolean getSavedActiveHigh() {
353 SharedPreferences settings
= ctx
.getSharedPreferences(PREFS_NAME
, Context
.MODE_PRIVATE
);
355 return settings
.getBoolean("active_high", false);
358 public synchronized void setSavedActiveHigh(boolean enable
) {
359 SharedPreferences settings
= ctx
.getSharedPreferences(PREFS_NAME
, Context
.MODE_PRIVATE
);
360 SharedPreferences
.Editor editor
= settings
.edit();
362 editor
.putBoolean("active_high", enable
);
366 public synchronized boolean is_safe_to_load() {
367 SharedPreferences settings
= ctx
.getSharedPreferences(PREFS_NAME
, Context
.MODE_PRIVATE
);
368 return settings
.getBoolean("safe_to_load", false);
371 public synchronized boolean get_on_boot() {
372 SharedPreferences settings
= ctx
.getSharedPreferences(PREFS_NAME
, Context
.MODE_PRIVATE
);
373 return settings
.getBoolean("on_boot", false);
376 public synchronized void set_on_boot(boolean on_boot
) {
377 SharedPreferences settings
= ctx
.getSharedPreferences(PREFS_NAME
, Context
.MODE_PRIVATE
);
378 SharedPreferences
.Editor editor
= settings
.edit();
380 editor
.putBoolean("on_boot", on_boot
);
384 private synchronized void extractModule() {
385 File debounce_ko
= new File(ctx
.getFilesDir() + "/debounce.ko");
387 if (debounce_ko
.exists()) {
392 InputStream apk
= ctx
.getAssets().open("debounce.ko");
393 OutputStream mod
= ctx
.openFileOutput("debounce.ko.tmp", 0);
395 //I assume a page is 4k...
396 byte buf
[] = new byte[4096];
399 while((bytes
= apk
.read(buf
)) != -1) {
400 mod
.write(buf
, 0, bytes
);
406 File tmpfile
= new File(debounce_ko
+ ".tmp");
407 tmpfile
.renameTo(debounce_ko
);
408 } catch (Exception e
) {}