]>
Commit | Line | Data |
---|---|---|
0ae502f6 MG |
1 | package de.rmdir.ms2debounce; |
2 | ||
5738a32f MG |
3 | import java.io.InputStream; |
4 | import java.io.OutputStream; | |
226a7d4d | 5 | import java.io.File; |
d82ae589 MG |
6 | import java.io.FileReader; |
7 | import java.io.BufferedReader; | |
226a7d4d | 8 | |
5738a32f | 9 | import android.content.Context; |
08fec0be | 10 | import android.content.SharedPreferences; |
5738a32f | 11 | |
0ae502f6 MG |
12 | public class DebounceModuleHelper |
13 | { | |
5738a32f | 14 | private Context ctx; |
08fec0be | 15 | public static final String PREFS_NAME = "DebounceCfg"; |
226a7d4d | 16 | |
5738a32f MG |
17 | public DebounceModuleHelper(Context context) { |
18 | ctx = context; | |
226a7d4d MG |
19 | } |
20 | ||
21 | public void loadModule() { | |
dea0f4b0 | 22 | loadModule(getSavedDelay()); |
40697a47 MG |
23 | } |
24 | ||
08fec0be | 25 | public synchronized void loadModule(int delay) { |
40697a47 MG |
26 | File debounce_ko = new File(ctx.getFilesDir() + "/debounce.ko"); |
27 | ||
226a7d4d MG |
28 | extractModule(); |
29 | ||
d3e7b10c MG |
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 | } | |
40697a47 | 36 | |
0ae502f6 | 37 | try { |
40697a47 | 38 | Process insmod = Runtime.getRuntime().exec(new String[]{"su","-c","/system/bin/insmod " + debounce_ko + " debounce_delay=" + delay}); |
0ae502f6 | 39 | insmod.waitFor(); |
08fec0be MG |
40 | } catch (Exception e) { |
41 | return; | |
a6cf1017 MG |
42 | } |
43 | ||
44 | if (!isLoaded()) { | |
45 | return; | |
08fec0be | 46 | } |
dea0f4b0 | 47 | |
08fec0be MG |
48 | if (getDelay() <= 0) { |
49 | return; | |
50 | } | |
51 | ||
52 | /* Module was obviously loaded, so it is safe to load on boot */ | |
d3e7b10c MG |
53 | editor.putBoolean("safe_to_load", true); |
54 | editor.commit(); | |
0ae502f6 MG |
55 | } |
56 | ||
08fec0be | 57 | public synchronized void unloadModule() { |
40697a47 MG |
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 | ||
08fec0be | 64 | public synchronized boolean isLoaded() { |
d82ae589 MG |
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; | |
0ae502f6 | 83 | } |
226a7d4d | 84 | |
08fec0be | 85 | public synchronized int getDelay() { |
d82ae589 MG |
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; | |
ee6322a1 MG |
101 | } |
102 | ||
376c6ac7 MG |
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 | ||
dea0f4b0 MG |
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 | ||
08fec0be MG |
129 | public synchronized boolean is_safe_to_load() { |
130 | SharedPreferences settings = ctx.getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE); | |
c3053460 MG |
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 | } | |
08fec0be | 138 | |
c3053460 MG |
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(); | |
08fec0be MG |
145 | } |
146 | ||
5738a32f MG |
147 | private synchronized void extractModule() { |
148 | File debounce_ko = new File(ctx.getFilesDir() + "/debounce.ko"); | |
226a7d4d MG |
149 | |
150 | if (debounce_ko.exists()) { | |
151 | return; | |
152 | } | |
5738a32f MG |
153 | |
154 | try { | |
155 | InputStream apk = ctx.getAssets().open("debounce.ko"); | |
40697a47 MG |
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 | } | |
5738a32f MG |
165 | |
166 | apk.close(); | |
167 | mod.close(); | |
40697a47 MG |
168 | |
169 | File tmpfile = new File(debounce_ko + ".tmp"); | |
170 | tmpfile.renameTo(debounce_ko); | |
5738a32f | 171 | } catch (Exception e) {} |
226a7d4d | 172 | } |
0ae502f6 | 173 | } |