]> cvs.zerfleddert.de Git - ms2-fixes/blob - MS2Debounce/src/de/rmdir/ms2debounce/DebounceModuleHelper.java
add debounce_delay edittext
[ms2-fixes] / MS2Debounce / src / de / rmdir / ms2debounce / DebounceModuleHelper.java
1 package de.rmdir.ms2debounce;
2
3 import java.io.InputStream;
4 import java.io.OutputStream;
5 import java.io.File;
6 import java.io.FileReader;
7 import java.io.BufferedReader;
8
9 import android.content.Context;
10 import android.content.SharedPreferences;
11
12 public class DebounceModuleHelper
13 {
14 private Context ctx;
15 public static final String PREFS_NAME = "DebounceCfg";
16
17 public DebounceModuleHelper(Context context) {
18 ctx = context;
19 }
20
21 public void loadModule() {
22 loadModule(getSavedDelay());
23 }
24
25 public synchronized void loadModule(int delay) {
26 File debounce_ko = new File(ctx.getFilesDir() + "/debounce.ko");
27
28 extractModule();
29
30 SharedPreferences settings = ctx.getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE);
31 SharedPreferences.Editor editor = settings.edit();
32 if (is_safe_to_load()) {
33 editor.putBoolean("safe_to_load", false);
34 editor.commit();
35 }
36
37 try {
38 Process insmod = Runtime.getRuntime().exec(new String[]{"su","-c","/system/bin/insmod " + debounce_ko + " debounce_delay=" + delay});
39 insmod.waitFor();
40 } catch (Exception e) {
41 return;
42 }
43
44 if (!isLoaded()) {
45 return;
46 }
47
48 if (getDelay() <= 0) {
49 return;
50 }
51
52 /* Module was obviously loaded, so it is safe to load on boot */
53 editor.putBoolean("safe_to_load", true);
54 editor.commit();
55 }
56
57 public synchronized void unloadModule() {
58 try {
59 Process rmmod = Runtime.getRuntime().exec(new String[]{"su","-c","/system/bin/rmmod debounce"});
60 rmmod.waitFor();
61 } catch (Exception e) {}
62 }
63
64 public synchronized boolean isLoaded() {
65 boolean loaded = false;
66 try {
67 String read;
68
69 FileReader modules = new FileReader("/proc/modules");
70 BufferedReader modules_buf = new BufferedReader(modules);
71
72 while((read = modules_buf.readLine()) != null) {
73 if (read.regionMatches(0, "debounce", 0, 8)) {
74 loaded = true;
75 }
76 }
77
78 } catch (Exception e) {
79 loaded = false;
80 }
81
82 return loaded;
83 }
84
85 public synchronized int getDelay() {
86 int debounce_delay = -1;
87
88 try {
89 String read;
90
91 FileReader delay = new FileReader("/sys/module/debounce/parameters/debounce_delay");
92 BufferedReader delay_buf = new BufferedReader(delay);
93
94 read = delay_buf.readLine();
95 if (read != null) {
96 debounce_delay = Integer.parseInt(read.trim());
97 }
98 } catch (Exception e) {}
99
100 return debounce_delay;
101 }
102
103 public synchronized void setDelay(int delay) {
104 if (isLoaded()) {
105 if (getDelay() == delay) {
106 return;
107 }
108
109 unloadModule();
110 }
111
112 loadModule(delay);
113 }
114
115 public synchronized int getSavedDelay() {
116 SharedPreferences settings = ctx.getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE);
117
118 return settings.getInt("debounce_delay", 10);
119 }
120
121 public synchronized void setSavedDelay(int delay) {
122 SharedPreferences settings = ctx.getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE);
123 SharedPreferences.Editor editor = settings.edit();
124
125 editor.putInt("debounce_delay", delay);
126 editor.commit();
127 }
128
129 public synchronized boolean is_safe_to_load() {
130 SharedPreferences settings = ctx.getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE);
131 boolean safe_to_load = settings.getBoolean("safe_to_load", false);
132
133 return safe_to_load;
134 }
135
136 private synchronized void extractModule() {
137 File debounce_ko = new File(ctx.getFilesDir() + "/debounce.ko");
138
139 if (debounce_ko.exists()) {
140 return;
141 }
142
143 try {
144 InputStream apk = ctx.getAssets().open("debounce.ko");
145 OutputStream mod = ctx.openFileOutput("debounce.ko.tmp", 0);
146
147 //I assume a page is 4k...
148 byte buf[] = new byte[4096];
149 int bytes;
150
151 while((bytes = apk.read(buf)) != -1) {
152 mod.write(buf, 0, bytes);
153 }
154
155 apk.close();
156 mod.close();
157
158 File tmpfile = new File(debounce_ko + ".tmp");
159 tmpfile.renameTo(debounce_ko);
160 } catch (Exception e) {}
161 }
162 }
Impressum, Datenschutz