]>
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); | |
131 | boolean safe_to_load = settings.getBoolean("safe_to_load", false); | |
132 | ||
133 | return safe_to_load; | |
134 | } | |
135 | ||
5738a32f MG |
136 | private synchronized void extractModule() { |
137 | File debounce_ko = new File(ctx.getFilesDir() + "/debounce.ko"); | |
226a7d4d MG |
138 | |
139 | if (debounce_ko.exists()) { | |
140 | return; | |
141 | } | |
5738a32f MG |
142 | |
143 | try { | |
144 | InputStream apk = ctx.getAssets().open("debounce.ko"); | |
40697a47 MG |
145 | OutputStream mod = ctx.openFileOutput("debounce.ko.tmp", 0); |
146 | ||
147 | //I assume a page is 4k... | |
148 | byte buf[] = new byte[4096]; | |
149 | int bytes; | |
150 | ||
151 | while((bytes = apk.read(buf)) != -1) { | |
152 | mod.write(buf, 0, bytes); | |
153 | } | |
5738a32f MG |
154 | |
155 | apk.close(); | |
156 | mod.close(); | |
40697a47 MG |
157 | |
158 | File tmpfile = new File(debounce_ko + ".tmp"); | |
159 | tmpfile.renameTo(debounce_ko); | |
5738a32f | 160 | } catch (Exception e) {} |
226a7d4d | 161 | } |
0ae502f6 | 162 | } |