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