39a29a8287d145de9032fad87a8da35218ab529b
[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 return settings.getBoolean("safe_to_load", false);
132 }
133
134 public synchronized boolean get_on_boot() {
135 SharedPreferences settings = ctx.getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE);
136 return settings.getBoolean("on_boot", false);
137 }
138
139 public synchronized void set_on_boot(boolean on_boot) {
140 SharedPreferences settings = ctx.getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE);
141 SharedPreferences.Editor editor = settings.edit();
142
143 editor.putBoolean("on_boot", on_boot);
144 editor.commit();
145 }
146
147 private synchronized void extractModule() {
148 File debounce_ko = new File(ctx.getFilesDir() + "/debounce.ko");
149
150 if (debounce_ko.exists()) {
151 return;
152 }
153
154 try {
155 InputStream apk = ctx.getAssets().open("debounce.ko");
156 OutputStream mod = ctx.openFileOutput("debounce.ko.tmp", 0);
157
158 //I assume a page is 4k...
159 byte buf[] = new byte[4096];
160 int bytes;
161
162 while((bytes = apk.read(buf)) != -1) {
163 mod.write(buf, 0, bytes);
164 }
165
166 apk.close();
167 mod.close();
168
169 File tmpfile = new File(debounce_ko + ".tmp");
170 tmpfile.renameTo(debounce_ko);
171 } catch (Exception e) {}
172 }
173 }
Impressum, Datenschutz