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