/
PerfectViewerPreferences.java
158 lines (134 loc) · 5.06 KB
/
PerfectViewerPreferences.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
package com.kaulahcintaku.comicshelf;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import android.content.Context;
import android.content.SharedPreferences;
public class PerfectViewerPreferences {
private static final String HISTORY_FILE = "perfect_viewer_history";
public File getSharedPrefsFile(String name) {
//return new File("mnt/sdcard/Android/data/shared_prefs/", name + ".xml");
//return new File("/data/data/com.example.coverflowv2/shared_prefs/", name + ".xml");
return new File("/data/data/com.rookiestudio.perfectviewer/shared_prefs/", name + ".xml");
}
public SharedPreferences getSharedPreferences(String name) throws Exception{
SharedPreferences sp = null;
File file = getSharedPrefsFile(name);
if(!file.exists())
throw new RuntimeException("file: "+file.getAbsolutePath()+" doesn't exists");
if(!file.canRead() && !makeFileReadable(file))
throw new RuntimeException("file is not readable");
FileInputStream str = null;
str = new FileInputStream(file);
Map map = XmlUtilsReadMapXml(str);
str.close();
sp = newPreferences(file, Context.MODE_PRIVATE, map);
return sp;
}
private boolean makeFileReadable(File file) throws Exception{
return executeSuCommand("chmod 664 "+file.getAbsolutePath());
}
public static boolean executeSuCommand(String command) throws Exception{
Process process = Runtime.getRuntime().exec("su -c sh");
DataOutputStream os = new DataOutputStream(process.getOutputStream());
os.writeBytes(command); os.flush();
os.writeBytes("\n"); os.flush();
os.writeBytes("exit\n"); os.flush();
if(process.waitFor() != 0)
throw new RuntimeException(command+" returns "+process.exitValue());
return true;
}
private Map XmlUtilsReadMapXml(FileInputStream fileInputStream) throws Exception{
Class clazz = Class.forName("com.android.internal.util.XmlUtils");
Method method = clazz.getMethod("readMapXml", InputStream.class);
return (Map) method.invoke(null, fileInputStream);
}
private SharedPreferences newPreferences(File file, int mode, final Map map) throws Exception{
return new SharedPreferencesImpl(file, mode, map);
}
public List<String> getLastReads() throws Exception{
List<String> results = new ArrayList<String>();
SharedPreferences sp = getSharedPreferences(HISTORY_FILE);
for(int i = 0; i < 10; i++){
String value = sp.getString("HistoryFolder"+i, null);
if(value != null)
results.add(value);
}
return results;
}
private static final class SharedPreferencesImpl implements SharedPreferences {
private Map mMap;
SharedPreferencesImpl(
File file, int mode, Map initialContents) {
mMap = initialContents != null ? initialContents : new HashMap();
}
public boolean hasFileChanged() {
synchronized (this) {
return false;
}
}
public void replace(Map newContents) {
if (newContents != null) {
synchronized (this) {
mMap = newContents;
}
}
}
public void registerOnSharedPreferenceChangeListener(OnSharedPreferenceChangeListener listener) {
throw new UnsupportedOperationException();
}
public void unregisterOnSharedPreferenceChangeListener(OnSharedPreferenceChangeListener listener) {
throw new UnsupportedOperationException();
}
public Map<String, ?> getAll() {
synchronized(this) {
return new HashMap(mMap);
}
}
public String getString(String key, String defValue) {
synchronized (this) {
String v = (String)mMap.get(key);
return v != null ? v : defValue;
}
}
public int getInt(String key, int defValue) {
synchronized (this) {
Integer v = (Integer)mMap.get(key);
return v != null ? v : defValue;
}
}
public long getLong(String key, long defValue) {
synchronized (this) {
Long v = (Long) mMap.get(key);
return v != null ? v : defValue;
}
}
public float getFloat(String key, float defValue) {
synchronized (this) {
Float v = (Float)mMap.get(key);
return v != null ? v : defValue;
}
}
public boolean getBoolean(String key, boolean defValue) {
synchronized (this) {
Boolean v = (Boolean)mMap.get(key);
return v != null ? v : defValue;
}
}
public boolean contains(String key) {
synchronized (this) {
return mMap.containsKey(key);
}
}
@Override
public Editor edit() {
throw new UnsupportedOperationException();
}
}
}