93fd221a54fe3aa85961cfd60823b58d12cfc679
[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 // FIXME: Read settings from database...
34
35 try {
36 Process insmod = Runtime.getRuntime().exec(new String[]{"su","-c","/system/bin/insmod " + debounce_ko + " debounce_delay=" + delay});
37 insmod.waitFor();
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 }
53 }
54
55 public synchronized void unloadModule() {
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
62 public synchronized boolean isLoaded() {
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;
81 }
82
83 public synchronized int getDelay() {
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;
99 }
100
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
108 private synchronized void extractModule() {
109 File debounce_ko = new File(ctx.getFilesDir() + "/debounce.ko");
110
111 if (debounce_ko.exists()) {
112 return;
113 }
114
115 try {
116 InputStream apk = ctx.getAssets().open("debounce.ko");
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 }
126
127 apk.close();
128 mod.close();
129
130 File tmpfile = new File(debounce_ko + ".tmp");
131 tmpfile.renameTo(debounce_ko);
132 } catch (Exception e) {}
133 }
134 }
Impressum, Datenschutz