Skip to content

Commit 26849eb

Browse files
committed
refactor(data): replace prepareRime with sync in Config
prepareRime is adapted and migrated to DataManager and called sync now.
1 parent 1781a59 commit 26849eb

File tree

1 file changed

+3
-74
lines changed

1 file changed

+3
-74
lines changed

app/src/main/java/com/osfans/trime/data/Config.java

+3-74
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919
package com.osfans.trime.data;
2020

2121
import android.content.Context;
22-
import android.content.res.AssetManager;
2322
import android.content.res.Resources;
2423
import android.graphics.Bitmap;
2524
import android.graphics.BitmapFactory;
@@ -39,13 +38,11 @@
3938
import com.osfans.trime.ime.keyboard.Key;
4039
import com.osfans.trime.ime.keyboard.Sound;
4140
import com.osfans.trime.ime.symbol.TabManager;
42-
import com.osfans.trime.util.AppVersionUtils;
4341
import com.osfans.trime.util.ConfigGetter;
4442
import java.io.File;
4543
import java.io.FileInputStream;
4644
import java.io.FileOutputStream;
4745
import java.io.FilenameFilter;
48-
import java.io.IOException;
4946
import java.io.InputStream;
5047
import java.io.OutputStream;
5148
import java.util.ArrayList;
@@ -64,7 +61,6 @@ public class Config {
6461
// private static final String TAG = "Config";
6562

6663
private static Config self = null;
67-
private static AssetManager assetManager = null;
6864

6965
private static final AppPrefs appPrefs = AppPrefs.defaultInstance();
7066

@@ -99,12 +95,12 @@ public Config(@NonNull Context context, boolean skipDeploy) {
9995
"\t<TrimeInit>\t" + Thread.currentThread().getStackTrace()[2].getMethodName() + "\t";
10096
Timber.d(methodName);
10197
self = this;
102-
assetManager = context.getAssets();
10398
themeName = appPrefs.getLooks().getSelectedTheme();
10499
soundPackageName = appPrefs.getKeyboard().getSoundPackage();
105100

106-
Timber.d(methodName + "prepareRime");
107-
prepareRime(context);
101+
Timber.d(methodName + "sync");
102+
DataManager.sync();
103+
Rime.get(context, !DataManager.INSTANCE.getSharedDataDir().exists());
108104

109105
// 正常逻辑不应该部署全部主题,init()方法已经做过当前主题的部署
110106
// Timber.d(methodName + "deployTheme");
@@ -173,30 +169,6 @@ public String getSoundPackage() {
173169
return soundPackageName;
174170
}
175171

176-
public void prepareRime(Context context) {
177-
String methodName =
178-
"\t<TrimeInit>\t" + Thread.currentThread().getStackTrace()[2].getMethodName() + "\t";
179-
Timber.d(methodName);
180-
boolean isExist = new File(sharedDataDir).exists();
181-
boolean isOverwrite = AppVersionUtils.INSTANCE.isDifferentVersion(appPrefs);
182-
String defaultFile = "trime.yaml";
183-
Timber.d(methodName + "copy");
184-
copyFileOrDir("", isOverwrite);
185-
// 缺失导致获取方案列表为空
186-
Timber.d(methodName + "copy default.custom.yaml");
187-
final String defaultCustom = "default.custom.yaml";
188-
if (!new File(userDataDir, defaultCustom).exists()) {
189-
try {
190-
new File(userDataDir, defaultCustom).createNewFile();
191-
} catch (IOException e) {
192-
e.printStackTrace();
193-
}
194-
}
195-
Timber.d(methodName + "Rime.get");
196-
Rime.get(context, !isExist); // 覆蓋時不強制部署
197-
Timber.d(methodName + "finish");
198-
}
199-
200172
public static String[] getThemeKeys(boolean isUser) {
201173
File d = new File(isUser ? userDataDir : sharedDataDir);
202174
FilenameFilter trimeFilter = (dir, filename) -> filename.endsWith("trime.yaml");
@@ -225,49 +197,6 @@ public static String[] getYamlFileNames(String[] keys) {
225197
return names;
226198
}
227199

228-
public boolean copyFileOrDir(String path, boolean overwrite) {
229-
try {
230-
final String assetPath = new File(RIME, path).getPath();
231-
final String[] assets = assetManager.list(assetPath);
232-
if (assets.length == 0) {
233-
// Files
234-
copyFile(path, overwrite);
235-
} else {
236-
// Dirs
237-
final File dir = new File(sharedDataDir, path);
238-
if (!dir.exists()) // noinspection ResultOfMethodCallIgnored
239-
dir.mkdir();
240-
for (String asset : assets) {
241-
final String subPath = new File(path, asset).getPath();
242-
copyFileOrDir(subPath, overwrite);
243-
}
244-
}
245-
} catch (Exception e) {
246-
e.printStackTrace();
247-
return false;
248-
}
249-
return true;
250-
}
251-
252-
private void copyFile(String fileName, boolean overwrite) {
253-
if (fileName == null) return;
254-
255-
final String targetFileName = new File(sharedDataDir, fileName).getPath();
256-
if (new File(targetFileName).exists() && !overwrite) return;
257-
final String sourceFileName = new File(RIME, fileName).getPath();
258-
try (InputStream in = assetManager.open(sourceFileName);
259-
final FileOutputStream out = new FileOutputStream(targetFileName)) {
260-
final byte[] buffer = new byte[1024];
261-
int read;
262-
while ((read = in.read(buffer)) != -1) {
263-
out.write(buffer, 0, read);
264-
}
265-
out.flush();
266-
} catch (IOException e) {
267-
e.printStackTrace();
268-
}
269-
}
270-
271200
private void deployTheme() {
272201
if (userDataDir.contentEquals(sharedDataDir)) return; // 相同文件夾不部署主題
273202
final String[] configs = getThemeKeys(false);

0 commit comments

Comments
 (0)