Skip to content

Commit 960bb51

Browse files
committed
refactor(Config): reform how to apply sound package
1 parent 31b47ea commit 960bb51

File tree

1 file changed

+11
-32
lines changed
  • app/src/main/java/com/osfans/trime/data/theme

1 file changed

+11
-32
lines changed

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

+11-32
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
import android.graphics.drawable.NinePatchDrawable;
2929
import androidx.annotation.NonNull;
3030
import androidx.annotation.Nullable;
31+
import com.blankj.utilcode.util.FileUtils;
3132
import com.osfans.trime.core.Rime;
3233
import com.osfans.trime.data.AppPrefs;
3334
import com.osfans.trime.data.DataManager;
@@ -37,10 +38,6 @@
3738
import com.osfans.trime.ime.symbol.TabManager;
3839
import com.osfans.trime.util.DimensionsKt;
3940
import java.io.File;
40-
import java.io.FileInputStream;
41-
import java.io.FileOutputStream;
42-
import java.io.InputStream;
43-
import java.io.OutputStream;
4441
import java.util.ArrayList;
4542
import java.util.HashMap;
4643
import java.util.List;
@@ -118,36 +115,20 @@ public void setTheme(String theme) {
118115
// 设置音效包
119116
public void setSoundPackage(String name) {
120117
soundPackageName = name;
121-
String path = userDataDir + File.separator + "sound" + File.separator + name + ".sound.yaml";
122-
File file = new File(path);
123-
if (file.exists()) {
124-
applySoundPackage(file, name);
125-
}
118+
applySoundPackage(name);
126119
appPrefs.getKeyboard().setSoundPackage(soundPackageName);
127120
}
128121

129122
// 应用音效包
130-
private void applySoundPackage(File file, String name) {
131-
// copy soundpackage yaml file from sound folder to build folder
132-
try {
133-
InputStream in = new FileInputStream(file);
134-
OutputStream out =
135-
new FileOutputStream(
136-
userDataDir + File.separator + "build" + File.separator + name + ".sound.yaml");
137-
138-
byte[] buffer = new byte[1024];
139-
int len;
140-
while ((len = in.read(buffer)) > 0) {
141-
out.write(buffer, 0, len);
142-
}
143-
in.close();
144-
out.close();
145-
Timber.i("applySoundPackage=%s", name);
146-
} catch (Exception e) {
147-
e.printStackTrace();
123+
private boolean applySoundPackage(@NonNull String name) {
124+
final String src = userDataDir + "/sound/" + name + ".sound.yaml";
125+
final String dest = userDataDir + "/build/" + name + ".sound.yaml";
126+
boolean result = FileUtils.copy(src, dest);
127+
if (result) {
128+
Sound.get(name);
129+
currentSound = name;
148130
}
149-
Sound.get(name);
150-
currentSound = name;
131+
return result;
151132
}
152133

153134
// 配色指定音效时自动切换音效效果(不会自动修改设置)。
@@ -157,9 +138,7 @@ public void setSoundFromColor() {
157138
if (m.containsKey("sound")) {
158139
String sound = m.get("sound");
159140
if (!Objects.equals(sound, currentSound)) {
160-
File file = new File(userDataDir + "/sound/" + sound + ".sound.yaml");
161-
if (file.exists()) {
162-
applySoundPackage(file, sound);
141+
if (applySoundPackage(sound)) {
163142
return;
164143
}
165144
}

0 commit comments

Comments
 (0)