Skip to content

Commit 3ffef79

Browse files
committed
fix(Sound): crash when soundpackage missing
1 parent 0252b6f commit 3ffef79

File tree

4 files changed

+40
-31
lines changed

4 files changed

+40
-31
lines changed

app/src/main/java/com/osfans/trime/ime/core/Trime.java

+5
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@
7878
import com.osfans.trime.settings.PrefMainActivity;
7979
import com.osfans.trime.settings.components.ColorPickerDialog;
8080
import com.osfans.trime.settings.components.SchemaPickerDialog;
81+
import com.osfans.trime.settings.components.SoundPickerDialog;
8182
import com.osfans.trime.settings.components.ThemePickerDialog;
8283
import com.osfans.trime.setup.Config;
8384
import com.osfans.trime.setup.IntentReceiver;
@@ -941,6 +942,10 @@ public void showSchemaDialog() {
941942
public void showThemeDialog() {
942943
new ThemePickerDialog(this).show();
943944
}
945+
/** 彈出{@link SoundPickerDialog 音效包对话框} */
946+
public void showSoundDialog() {
947+
new SoundPickerDialog(this).show();
948+
}
944949

945950
/** Hides the IME and launches {@link PrefMainActivity}. */
946951
public void launchSettings() {

app/src/main/java/com/osfans/trime/ime/text/TextInputManager.kt

+1
Original file line numberDiff line numberDiff line change
@@ -347,6 +347,7 @@ class TextInputManager private constructor() :
347347
"theme" -> trime.showThemeDialog()
348348
"color" -> trime.showColorDialog()
349349
"schema" -> trime.showSchemaDialog()
350+
"sound" -> trime.showSoundDialog()
350351
else -> trime.launchSettings()
351352
}
352353
}

app/src/main/java/com/osfans/trime/settings/components/SoundPickerDialog.kt

+2-1
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,8 @@ class SoundPickerDialog(
7070
}
7171

7272
private fun setSound() {
73-
config.soundPackage = themeKeys[checkedId]?.replace(".sound.yaml", "")
73+
if (checkedId >= 0 && checkedId <themeKeys.size)
74+
config.soundPackage = themeKeys[checkedId]?.replace(".sound.yaml", "")
7475
}
7576

7677
/** 调用该方法显示对话框 **/

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

+32-30
Original file line numberDiff line numberDiff line change
@@ -277,38 +277,40 @@ public void setTheme(String theme) {
277277

278278
public void setSoundPackage(String name) {
279279
soundPackageName = name;
280-
// copy soundpackage yaml file from sound folder to build folder
281-
try {
282-
InputStream in =
283-
new FileInputStream(
284-
DataUtils.getUserDataDir()
285-
+ File.separator
286-
+ "sound"
287-
+ File.separator
288-
+ name
289-
+ ".sound.yaml");
290-
OutputStream out =
291-
new FileOutputStream(
292-
DataUtils.getUserDataDir()
293-
+ File.separator
294-
+ "build"
295-
+ File.separator
296-
+ name
297-
+ ".sound.yaml");
298-
299-
byte[] buffer = new byte[1024];
300-
int len;
301-
while ((len = in.read(buffer)) > 0) {
302-
out.write(buffer, 0, len);
303-
}
304-
305-
getPrefs().getKeyboard().setSoundPackage(soundPackageName);
306-
Sound.get(soundPackageName);
280+
String path =
281+
DataUtils.getUserDataDir()
282+
+ File.separator
283+
+ "sound"
284+
+ File.separator
285+
+ name
286+
+ ".sound.yaml";
287+
File file = new File(path);
288+
if (file.exists()) {
289+
// copy soundpackage yaml file from sound folder to build folder
290+
try {
291+
InputStream in = new FileInputStream(file);
292+
OutputStream out =
293+
new FileOutputStream(
294+
DataUtils.getUserDataDir()
295+
+ File.separator
296+
+ "build"
297+
+ File.separator
298+
+ name
299+
+ ".sound.yaml");
300+
301+
byte[] buffer = new byte[1024];
302+
int len;
303+
while ((len = in.read(buffer)) > 0) {
304+
out.write(buffer, 0, len);
305+
}
307306

308-
Timber.i("setSoundPackage = " + soundPackageName);
309-
} catch (Exception e) {
310-
e.printStackTrace();
307+
Timber.i("setSoundPackage = " + soundPackageName);
308+
} catch (Exception e) {
309+
e.printStackTrace();
310+
}
311311
}
312+
getPrefs().getKeyboard().setSoundPackage(soundPackageName);
313+
Sound.get(soundPackageName);
312314
}
313315

314316
private void init() {

0 commit comments

Comments
 (0)