-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSettingsManager.java
More file actions
230 lines (192 loc) · 7.94 KB
/
Copy pathSettingsManager.java
File metadata and controls
230 lines (192 loc) · 7.94 KB
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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
import javax.microedition.rms.*;
import java.io.*;
public class SettingsManager {
private static SettingsManager instance;
private RecordStore settingsStore;
// ✅ CHANGED: Default path uses VidmateME (will be auto-detected)
private String storagePath = ""; // Empty = auto-detect on first access
private String currentProxy = "Direct";
private String currentApi = "Dashtube";
private boolean showThumbnails = true;
private boolean downloadAudio = false;
private String defaultQuality = "360p";
private int speedLimitKBps = 0;
private static final int SETTINGS_VERSION = 2;
private SettingsManager() {
loadSettings();
// ✅ NEW: Auto-detect storage if empty
if (storagePath == null || storagePath.length() == 0) {
storagePath = StorageManager.getDownloadPath();
saveSettings();
}
}
public static synchronized SettingsManager getInstance() {
if (instance == null) {
instance = new SettingsManager();
}
return instance;
}
private void loadSettings() {
try {
settingsStore = RecordStore.openRecordStore("UniMediaSettings", true);
if (settingsStore.getNumRecords() > 0) {
byte[] data = settingsStore.getRecord(1);
DataInputStream dis = new DataInputStream(new ByteArrayInputStream(data));
int version = 1;
try {
version = dis.readInt();
} catch (Exception e) {
dis.close();
dis = new DataInputStream(new ByteArrayInputStream(data));
}
if (version == 1) {
loadV1Settings(dis);
saveSettings();
} else if (version >= 2) {
loadV2Settings(dis);
}
dis.close();
} else {
migrateOldSettings();
}
} catch (Exception e) {
}
}
private void loadV1Settings(DataInputStream dis) throws IOException {
storagePath = dis.readUTF();
currentProxy = dis.readUTF();
currentApi = dis.readUTF();
showThumbnails = dis.readBoolean();
downloadAudio = dis.readBoolean();
defaultQuality = dis.readUTF();
// ✅ CHANGED: Don't change UniMedia to VidmateME
// Let auto-detection handle it
if (currentApi.equals("Asepharyana")) {
currentApi = "Dashtube";
}
}
private void loadV2Settings(DataInputStream dis) throws IOException {
storagePath = dis.readUTF();
currentProxy = dis.readUTF();
currentApi = dis.readUTF();
showThumbnails = dis.readBoolean();
downloadAudio = dis.readBoolean();
defaultQuality = dis.readUTF();
speedLimitKBps = dis.readInt();
}
private void migrateOldSettings() {
RecordStore oldStore = null;
try {
oldStore = RecordStore.openRecordStore("VidmateSettings", false);
if (oldStore.getNumRecords() > 0) {
byte[] data = oldStore.getRecord(1);
DataInputStream dis = new DataInputStream(new ByteArrayInputStream(data));
loadV1Settings(dis);
dis.close();
saveSettings();
oldStore.closeRecordStore();
RecordStore.deleteRecordStore("VidmateSettings");
}
} catch (RecordStoreNotFoundException e) {
} catch (Exception e) {
} finally {
try {
if (oldStore != null) oldStore.closeRecordStore();
} catch (Exception e) {}
}
}
public void saveSettings() {
try {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
DataOutputStream dos = new DataOutputStream(baos);
dos.writeInt(SETTINGS_VERSION);
dos.writeUTF(storagePath);
dos.writeUTF(currentProxy);
dos.writeUTF(currentApi);
dos.writeBoolean(showThumbnails);
dos.writeBoolean(downloadAudio);
dos.writeUTF(defaultQuality);
dos.writeInt(speedLimitKBps);
dos.close();
byte[] data = baos.toByteArray();
if (settingsStore.getNumRecords() == 0) {
settingsStore.addRecord(data, 0, data.length);
} else {
settingsStore.setRecord(1, data, 0, data.length);
}
} catch (Exception e) {
}
}
private static String replaceString(String str, String pattern, String replacement) {
if (str == null || pattern == null || replacement == null) return str;
StringBuffer result = new StringBuffer();
int s = 0, e;
while ((e = str.indexOf(pattern, s)) >= 0) {
result.append(str.substring(s, e));
result.append(replacement);
s = e + pattern.length();
}
result.append(str.substring(s));
return result.toString();
}
public String getStoragePath() {
if (storagePath == null || storagePath.length() == 0) {
storagePath = StorageManager.getDownloadPath();
}
return storagePath;
}
public void setStoragePath(String path) {
storagePath = path;
}
public String getCurrentProxy() { return currentProxy; }
public void setCurrentProxy(String proxy) { currentProxy = proxy; }
public String getCurrentApi() { return currentApi; }
public void setCurrentApi(String api) { currentApi = api; }
public boolean isShowThumbnails() { return showThumbnails; }
public void setShowThumbnails(boolean show) { showThumbnails = show; }
public boolean isDownloadAudio() { return downloadAudio; }
public void setDownloadAudio(boolean audio) { downloadAudio = audio; }
public String getDefaultQuality() { return defaultQuality; }
public void setDefaultQuality(String quality) { defaultQuality = quality; }
public int getSpeedLimitKBps() { return speedLimitKBps; }
public void setSpeedLimitKBps(int kbps) {
if (kbps < 0) kbps = 0;
this.speedLimitKBps = kbps;
}
public String getSpeedLimitDisplay() {
if (speedLimitKBps == 0) {
return "Illimitee";
} else {
return speedLimitKBps + " KB/s";
}
}
public void resetToDefaults() {
storagePath = StorageManager.getDownloadPath(); // ✅ Auto-detect
currentProxy = "Direct";
currentApi = "Dashtube";
showThumbnails = true;
downloadAudio = false;
defaultQuality = "360p";
speedLimitKBps = 0;
saveSettings();
}
public String getSettingsSummary() {
StringBuffer sb = new StringBuffer();
sb.append("Storage: ").append(getStoragePath()).append("\n");
sb.append("Proxy: ").append(currentProxy).append("\n");
sb.append("API: ").append(currentApi).append("\n");
sb.append("Quality: ").append(defaultQuality).append("\n");
sb.append("Speed Limit: ").append(getSpeedLimitDisplay()).append("\n");
sb.append("Thumbnails: ").append(showThumbnails ? "Yes" : "No").append("\n");
sb.append("Audio Mode: ").append(downloadAudio ? "Yes" : "No").append("\n");
return sb.toString();
}
public void close() {
try {
if (settingsStore != null) {
settingsStore.closeRecordStore();
}
} catch (Exception e) {
}
}
}