Skip to content

Commit

Permalink
feat: add sound param in theme
Browse files Browse the repository at this point in the history
  • Loading branch information
tumuyan committed Dec 25, 2021
1 parent 4f9ea9e commit 9e4fc05
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 45 deletions.
2 changes: 2 additions & 0 deletions app/src/main/java/com/osfans/trime/ime/core/Trime.java
Original file line number Diff line number Diff line change
Expand Up @@ -518,6 +518,7 @@ private void reset() {
getImeConfig().reset();
loadConfig();
getImeConfig().initCurrentColors();
getImeConfig().setSoundFromColor();
if (keyboardSwitcher != null) keyboardSwitcher.newOrReset();
resetCandidate();
hideCompositionView();
Expand Down Expand Up @@ -944,6 +945,7 @@ public void showSchemaDialog() {
public void showThemeDialog() {
new ThemePickerDialog(this).show();
}

/** 彈出{@link SoundPickerDialog 音效包对话框} */
public void showSoundDialog() {
new SoundPickerDialog(this).show();
Expand Down
18 changes: 11 additions & 7 deletions app/src/main/java/com/osfans/trime/ime/keyboard/Sound.java
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,11 @@ public void resetProgress() {

@SuppressWarnings("unchecked")
public Sound(String soundPackageName) {
sp = new SoundPool(3, AudioManager.STREAM_MUSIC, 0);
sp = new SoundPool(3, AudioManager.STREAM_SYSTEM, 0);
keyset = new ArrayList<>();
melody = new int[1];
progress = -1;

Map<String, ?> m = YamlUtils.INSTANCE.loadMap(soundPackageName + ".sound", "");
if (m != null) {
String path = DataUtils.getUserDataDir() + File.separator + "sound" + File.separator;
Expand All @@ -69,7 +72,6 @@ public Sound(String soundPackageName) {
enable = true;
return;
} else if (m.containsKey("keyset")) {
progress = -1;
List<Map<String, ?>> n = (List<Map<String, ?>>) m.get("keyset");
for (Map<String, ?> o : n) {
int max = -1, min = -1;
Expand Down Expand Up @@ -111,9 +113,9 @@ public void play(Integer keycode, Integer volume) {
if (sound.length > 0) {
float soundVolume = volume / 100f;
if (progress >= 0) {
progress++;
if (progress >= melody.length) progress = 0;
currStreamId = sound[melody[progress]];
currStreamId = melody[progress];
progress++;
} else if (lastKeycode != keycode) {
lastKeycode = keycode;
for (Key key : keyset) {
Expand All @@ -138,9 +140,11 @@ public static int getKeycode(Object string) {

public int getSoundIndex(Object obj) {
String t = (String) obj;
if (t.matches("\\d+")) return Integer.parseInt(t);

int k = files.indexOf(t);
int k;
if (t.matches("\\d+")) {
k = Integer.parseInt(t);
if (k >= sound.length) return 0;
} else k = files.indexOf(t);
return Math.max(k, 0);
}

Expand Down
106 changes: 68 additions & 38 deletions app/src/main/java/com/osfans/trime/setup/Config.java
Original file line number Diff line number Diff line change
Expand Up @@ -81,9 +81,9 @@ public static Config get(Context context) {
}

private Map<?, ?> mStyle, mDefaultStyle;
private String themeName, soundPackageName;
private String themeName, soundPackageName, currentSound;
private static final String defaultName = "trime";
private String schema_id;
private String schema_id, colorID;

private Map<?, ?> fallbackColors;
private Map<String, ?> presetColorSchemes, presetKeyboards;
Expand All @@ -102,12 +102,12 @@ public Config(@NonNull Context context) {
self = this;
assetManager = context.getAssets();
themeName = getPrefs().getLooks().getSelectedTheme();
soundPackageName = getPrefs().getKeyboard().getSoundPackage();
prepareRime(context);
deployTheme();
init();
setSoundFromColor();
prepareCLipBoardRule();
soundPackageName = getPrefs().getKeyboard().getSoundPackage();
setSoundPackage(soundPackageName);
}

private void prepareCLipBoardRule() {
Expand Down Expand Up @@ -275,6 +275,7 @@ public void setTheme(String theme) {
init();
}

// 设置音效包
public void setSoundPackage(String name) {
soundPackageName = name;
String path =
Expand All @@ -286,31 +287,63 @@ public void setSoundPackage(String name) {
+ ".sound.yaml";
File file = new File(path);
if (file.exists()) {
// copy soundpackage yaml file from sound folder to build folder
try {
InputStream in = new FileInputStream(file);
OutputStream out =
new FileOutputStream(
DataUtils.getUserDataDir()
+ File.separator
+ "build"
+ File.separator
+ name
+ ".sound.yaml");

byte[] buffer = new byte[1024];
int len;
while ((len = in.read(buffer)) > 0) {
out.write(buffer, 0, len);
}
applySoundPackage(file, name);
}
getPrefs().getKeyboard().setSoundPackage(soundPackageName);
}

Timber.i("setSoundPackage = " + soundPackageName);
} catch (Exception e) {
e.printStackTrace();
// 应用音效包
private void applySoundPackage(File file, String name) {
// copy soundpackage yaml file from sound folder to build folder
try {
InputStream in = new FileInputStream(file);
OutputStream out =
new FileOutputStream(
DataUtils.getUserDataDir()
+ File.separator
+ "build"
+ File.separator
+ name
+ ".sound.yaml");

byte[] buffer = new byte[1024];
int len;
while ((len = in.read(buffer)) > 0) {
out.write(buffer, 0, len);
}

Timber.i("applySoundPackage = " + name);
} catch (Exception e) {
e.printStackTrace();
}
Sound.get(name);
currentSound = name;
}

// 配色指定音效时自动切换音效效果(不会自动修改设置)。
public void setSoundFromColor() {
final Map<String, ?> m = (Map<String, ?>) presetColorSchemes.get(colorID);
if (m.containsKey("sound")) {
String sound = (String) m.get("sound");
if (sound != currentSound) {
String path =
DataUtils.getUserDataDir()
+ File.separator
+ "sound"
+ File.separator
+ sound
+ ".sound.yaml";
File file = new File(path);
if (file.exists()) {
applySoundPackage(file, sound);
return;
}
}
}

if (currentSound != soundPackageName) {
setSoundPackage(soundPackageName);
}
getPrefs().getKeyboard().setSoundPackage(soundPackageName);
Sound.get(soundPackageName);
}

private void init() {
Expand Down Expand Up @@ -577,9 +610,7 @@ public static Integer getColor(Context context, @NonNull Map<?, ?> m, String k)
public Integer getColor(String key) {
Object o = getColorObject(key);
if (o == null) {
o =
((Map<?, ?>) Objects.requireNonNull(presetColorSchemes.get(getColorSchemeName())))
.get(key);
o = ((Map<?, ?>) Objects.requireNonNull(presetColorSchemes.get(colorID))).get(key);
}
if (o == null) return null;
return parseColor(o.toString());
Expand All @@ -589,7 +620,7 @@ public Integer getColor(String key) {
public Drawable getDrawable(@NonNull Map<?, ?> m, String k) {
if (m.containsKey(k)) {
final Object o = m.get(k);
Timber.d("getColorDrawable()" + k + " " + o);
// Timber.d("getColorDrawable()" + k + " " + o);
return drawableObject(o);
}
return null;
Expand Down Expand Up @@ -650,10 +681,9 @@ public String getString(String key) {
// 获取当前配色方案的key的value,或者从fallback获取值。
@Nullable
private Object getColorObject(String key) {
String scheme = getColorSchemeName();
final Map<?, ?> map = (Map<?, ?>) presetColorSchemes.get(scheme);
final Map<?, ?> map = (Map<?, ?>) presetColorSchemes.get(colorID);
if (map == null) return null;
getPrefs().getLooks().setSelectedColor(scheme);
getPrefs().getLooks().setSelectedColor(colorID);
Object o = map.get(key);
String fallbackKey = key;
while (o == null && fallbackColors.containsKey(fallbackKey)) {
Expand Down Expand Up @@ -922,16 +952,16 @@ public Drawable getDrawableBitmap_(String key) {
// 初始化当前配色 Config 2.0
public void initCurrentColors() {
curcentColors.clear();
String scheme = getColorSchemeName();
colorID = getColorSchemeName();
backgroundFolder = getString("background_folder");
Timber.d(
"initCurrentColors() scheme=%s themeName=%s schema_id=%s", scheme, themeName, schema_id);
final Map<?, ?> map = (Map<?, ?>) presetColorSchemes.get(scheme);
"initCurrentColors() colorID=%s themeName=%s schema_id=%s", colorID, themeName, schema_id);
final Map<?, ?> map = (Map<?, ?>) presetColorSchemes.get(colorID);
if (map == null) {
Timber.i("no scheme %s", scheme);
Timber.i("no colorID %s", colorID);
return;
}
getPrefs().getLooks().setSelectedColor(scheme);
getPrefs().getLooks().setSelectedColor(colorID);

for (Map.Entry<?, ?> entry : map.entrySet()) {
Object value = getColorRealValue(entry.getValue());
Expand Down

0 comments on commit 9e4fc05

Please sign in to comment.