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