]>
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() { | |
08fec0be MG |
22 | SharedPreferences settings = ctx.getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE); |
23 | ||
24 | int delay = settings.getInt("debounce_delay", 10); | |
25 | loadModule(delay); | |
40697a47 MG |
26 | } |
27 | ||
08fec0be | 28 | public synchronized void loadModule(int delay) { |
40697a47 MG |
29 | File debounce_ko = new File(ctx.getFilesDir() + "/debounce.ko"); |
30 | ||
226a7d4d MG |
31 | extractModule(); |
32 | ||
d3e7b10c MG |
33 | SharedPreferences settings = ctx.getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE); |
34 | SharedPreferences.Editor editor = settings.edit(); | |
35 | if (is_safe_to_load()) { | |
36 | editor.putBoolean("safe_to_load", false); | |
37 | editor.commit(); | |
38 | } | |
40697a47 | 39 | |
0ae502f6 | 40 | try { |
40697a47 | 41 | Process insmod = Runtime.getRuntime().exec(new String[]{"su","-c","/system/bin/insmod " + debounce_ko + " debounce_delay=" + delay}); |
0ae502f6 | 42 | insmod.waitFor(); |
08fec0be MG |
43 | } catch (Exception e) { |
44 | return; | |
a6cf1017 MG |
45 | } |
46 | ||
47 | if (!isLoaded()) { | |
48 | return; | |
08fec0be MG |
49 | } |
50 | ||
51 | if (getDelay() <= 0) { | |
52 | return; | |
53 | } | |
54 | ||
55 | /* Module was obviously loaded, so it is safe to load on boot */ | |
d3e7b10c MG |
56 | editor.putBoolean("safe_to_load", true); |
57 | editor.commit(); | |
0ae502f6 MG |
58 | } |
59 | ||
08fec0be | 60 | public synchronized void unloadModule() { |
40697a47 MG |
61 | try { |
62 | Process rmmod = Runtime.getRuntime().exec(new String[]{"su","-c","/system/bin/rmmod debounce"}); | |
63 | rmmod.waitFor(); | |
64 | } catch (Exception e) {} | |
65 | } | |
66 | ||
08fec0be | 67 | public synchronized boolean isLoaded() { |
d82ae589 MG |
68 | boolean loaded = false; |
69 | try { | |
70 | String read; | |
71 | ||
72 | FileReader modules = new FileReader("/proc/modules"); | |
73 | BufferedReader modules_buf = new BufferedReader(modules); | |
74 | ||
75 | while((read = modules_buf.readLine()) != null) { | |
76 | if (read.regionMatches(0, "debounce", 0, 8)) { | |
77 | loaded = true; | |
78 | } | |
79 | } | |
80 | ||
81 | } catch (Exception e) { | |
82 | loaded = false; | |
83 | } | |
84 | ||
85 | return loaded; | |
0ae502f6 | 86 | } |
226a7d4d | 87 | |
08fec0be | 88 | public synchronized int getDelay() { |
d82ae589 MG |
89 | int debounce_delay = -1; |
90 | ||
91 | try { | |
92 | String read; | |
93 | ||
94 | FileReader delay = new FileReader("/sys/module/debounce/parameters/debounce_delay"); | |
95 | BufferedReader delay_buf = new BufferedReader(delay); | |
96 | ||
97 | read = delay_buf.readLine(); | |
98 | if (read != null) { | |
99 | debounce_delay = Integer.parseInt(read.trim()); | |
100 | } | |
101 | } catch (Exception e) {} | |
102 | ||
103 | return debounce_delay; | |
ee6322a1 MG |
104 | } |
105 | ||
376c6ac7 MG |
106 | public synchronized void setDelay(int delay) { |
107 | if (isLoaded()) { | |
108 | if (getDelay() == delay) { | |
109 | return; | |
110 | } | |
111 | ||
112 | unloadModule(); | |
113 | } | |
114 | ||
115 | loadModule(delay); | |
116 | } | |
117 | ||
08fec0be MG |
118 | public synchronized boolean is_safe_to_load() { |
119 | SharedPreferences settings = ctx.getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE); | |
120 | boolean safe_to_load = settings.getBoolean("safe_to_load", false); | |
121 | ||
122 | return safe_to_load; | |
123 | } | |
124 | ||
5738a32f MG |
125 | private synchronized void extractModule() { |
126 | File debounce_ko = new File(ctx.getFilesDir() + "/debounce.ko"); | |
226a7d4d MG |
127 | |
128 | if (debounce_ko.exists()) { | |
129 | return; | |
130 | } | |
5738a32f MG |
131 | |
132 | try { | |
133 | InputStream apk = ctx.getAssets().open("debounce.ko"); | |
40697a47 MG |
134 | OutputStream mod = ctx.openFileOutput("debounce.ko.tmp", 0); |
135 | ||
136 | //I assume a page is 4k... | |
137 | byte buf[] = new byte[4096]; | |
138 | int bytes; | |
139 | ||
140 | while((bytes = apk.read(buf)) != -1) { | |
141 | mod.write(buf, 0, bytes); | |
142 | } | |
5738a32f MG |
143 | |
144 | apk.close(); | |
145 | mod.close(); | |
40697a47 MG |
146 | |
147 | File tmpfile = new File(debounce_ko + ".tmp"); | |
148 | tmpfile.renameTo(debounce_ko); | |
5738a32f | 149 | } catch (Exception e) {} |
226a7d4d | 150 | } |
0ae502f6 | 151 | } |