package de.rmdir.ms2debounce;
+import java.io.InputStream;
+import java.io.OutputStream;
+import java.io.File;
+import java.io.FileReader;
+import java.io.BufferedReader;
+
+import android.content.Context;
+import android.content.SharedPreferences;
+
public class DebounceModuleHelper
{
- static public void loadModule() {
+ private Context ctx;
+ public static final String PREFS_NAME = "DebounceCfg";
+
+ public DebounceModuleHelper(Context context) {
+ ctx = context;
+ }
+
+ public void loadModule() {
+ loadModule(getSavedDelay());
+ }
+
+ public synchronized void loadModule(int delay) {
+ File debounce_ko = new File(ctx.getFilesDir() + "/debounce.ko");
+
+ extractModule();
+
+ SharedPreferences settings = ctx.getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE);
+ SharedPreferences.Editor editor = settings.edit();
+ if (is_safe_to_load()) {
+ editor.putBoolean("safe_to_load", false);
+ editor.commit();
+ }
+
try {
- Process insmod = Runtime.getRuntime().exec(new String[]{"su","-c","/system/bin/insmod /system/lib/modules/debounce.ko"});
+ Process insmod = Runtime.getRuntime().exec(new String[]{"su","-c","/system/bin/insmod " + debounce_ko + " debounce_delay=" + delay});
insmod.waitFor();
+ } catch (Exception e) {
+ return;
+ }
+
+ if (!isLoaded()) {
+ return;
+ }
+
+ if (getDelay() <= 0) {
+ return;
+ }
+
+ /* Module was obviously loaded, so it is safe to load on boot */
+ editor.putBoolean("safe_to_load", true);
+ editor.commit();
+ }
+
+ public synchronized void unloadModule() {
+ try {
+ Process rmmod = Runtime.getRuntime().exec(new String[]{"su","-c","/system/bin/rmmod debounce"});
+ rmmod.waitFor();
+ } catch (Exception e) {}
+ }
+
+ public synchronized boolean isLoaded() {
+ boolean loaded = false;
+ try {
+ String read;
+
+ FileReader modules = new FileReader("/proc/modules");
+ BufferedReader modules_buf = new BufferedReader(modules);
+
+ while((read = modules_buf.readLine()) != null) {
+ if (read.regionMatches(0, "debounce", 0, 8)) {
+ loaded = true;
+ }
+ }
+
+ } catch (Exception e) {
+ loaded = false;
+ }
+
+ return loaded;
+ }
+
+ public synchronized int getDelay() {
+ int debounce_delay = -1;
+
+ try {
+ String read;
+
+ FileReader delay = new FileReader("/sys/module/debounce/parameters/debounce_delay");
+ BufferedReader delay_buf = new BufferedReader(delay);
+
+ read = delay_buf.readLine();
+ if (read != null) {
+ debounce_delay = Integer.parseInt(read.trim());
+ }
} catch (Exception e) {}
+
+ return debounce_delay;
+ }
+
+ public synchronized void setDelay(int delay) {
+ if (isLoaded()) {
+ if (getDelay() == delay) {
+ return;
+ }
+
+ unloadModule();
+ }
+
+ loadModule(delay);
+ }
+
+ public synchronized int getSavedDelay() {
+ SharedPreferences settings = ctx.getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE);
+
+ return settings.getInt("debounce_delay", 10);
+ }
+
+ public synchronized void setSavedDelay(int delay) {
+ SharedPreferences settings = ctx.getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE);
+ SharedPreferences.Editor editor = settings.edit();
+
+ editor.putInt("debounce_delay", delay);
+ editor.commit();
+ }
+
+ public synchronized boolean is_safe_to_load() {
+ SharedPreferences settings = ctx.getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE);
+ return settings.getBoolean("safe_to_load", false);
+ }
+
+ public synchronized boolean get_on_boot() {
+ SharedPreferences settings = ctx.getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE);
+ return settings.getBoolean("on_boot", false);
}
- static public boolean isLoaded() {
- return false;
+ public synchronized void set_on_boot(boolean on_boot) {
+ SharedPreferences settings = ctx.getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE);
+ SharedPreferences.Editor editor = settings.edit();
+
+ editor.putBoolean("on_boot", on_boot);
+ editor.commit();
+ }
+
+ private synchronized void extractModule() {
+ File debounce_ko = new File(ctx.getFilesDir() + "/debounce.ko");
+
+ if (debounce_ko.exists()) {
+ return;
+ }
+
+ try {
+ InputStream apk = ctx.getAssets().open("debounce.ko");
+ OutputStream mod = ctx.openFileOutput("debounce.ko.tmp", 0);
+
+ //I assume a page is 4k...
+ byte buf[] = new byte[4096];
+ int bytes;
+
+ while((bytes = apk.read(buf)) != -1) {
+ mod.write(buf, 0, bytes);
+ }
+
+ apk.close();
+ mod.close();
+
+ File tmpfile = new File(debounce_ko + ".tmp");
+ tmpfile.renameTo(debounce_ko);
+ } catch (Exception e) {}
}
}