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