]> git.zerfleddert.de Git - ms2-fixes/blob - MS2Debounce/src/de/rmdir/ms2debounce/DebounceModuleHelper.java
80dc5ff134745fabb87e4ac221252d180ff7da73
[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 import java.io.DataOutputStream;
9
10 import android.content.Context;
11 import android.content.SharedPreferences;
12
13 public class DebounceModuleHelper
14 {
15 private Context ctx;
16 public static final String PREFS_NAME = "DebounceCfg";
17 final int SUPERUSER_REQUEST = 4223;
18
19 public DebounceModuleHelper(Context context) {
20 ctx = context;
21 }
22
23 public void loadModule() {
24 loadModule(getSavedDelay());
25 }
26
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
38 public synchronized void loadModule(int delay) {
39 File debounce_ko = new File(ctx.getFilesDir() + "/debounce.ko");
40
41 extractModule();
42
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 }
49
50 try {
51 runAsRoot("/system/bin/insmod " + debounce_ko + " debounce_delay=" + delay);
52 } catch (Exception e) {
53 return;
54 }
55
56 if (!isLoaded()) {
57 return;
58 }
59
60 if (getDelay() <= 0) {
61 return;
62 }
63
64 /* Module was obviously loaded, so it is safe to load on boot */
65 editor.putBoolean("safe_to_load", true);
66 editor.commit();
67 }
68
69 public synchronized void unloadModule() {
70 try {
71 runAsRoot("/system/bin/rmmod debounce");
72 } catch (Exception e) {}
73 }
74
75 public synchronized boolean isLoaded() {
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;
94 }
95
96 public synchronized int getDelay() {
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;
112 }
113
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
126 public synchronized int getSavedDelay() {
127 SharedPreferences settings = ctx.getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE);
128
129 return settings.getInt("debounce_delay", 10);
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
140 public synchronized boolean is_safe_to_load() {
141 SharedPreferences settings = ctx.getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE);
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 }
149
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();
156 }
157
158 private synchronized void extractModule() {
159 File debounce_ko = new File(ctx.getFilesDir() + "/debounce.ko");
160
161 if (debounce_ko.exists()) {
162 return;
163 }
164
165 try {
166 InputStream apk = ctx.getAssets().open("debounce.ko");
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 }
176
177 apk.close();
178 mod.close();
179
180 File tmpfile = new File(debounce_ko + ".tmp");
181 tmpfile.renameTo(debounce_ko);
182 } catch (Exception e) {}
183 }
184 }
Impressum, Datenschutz