Skip to content

Commit 11b59b8

Browse files
committed
feat,refactor(core,ui,jni): reimplement setting integer in custom config
This can replace the huge, less robust schema overwriting in Rime.java
1 parent acf3c77 commit 11b59b8

6 files changed

Lines changed: 48 additions & 86 deletions

File tree

app/src/main/java/com/osfans/trime/core/Rime.java

Lines changed: 4 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -24,14 +24,8 @@
2424
import com.osfans.trime.data.DataManager;
2525
import com.osfans.trime.data.opencc.OpenCCDictManager;
2626
import com.osfans.trime.data.schema.SchemaManager;
27-
import java.io.BufferedReader;
28-
import java.io.CharArrayWriter;
29-
import java.io.File;
30-
import java.io.FileReader;
31-
import java.io.FileWriter;
32-
import java.io.IOException;
33-
import java.util.HashMap;
3427
import java.util.Map;
28+
import kotlin.Pair;
3529
import kotlinx.coroutines.channels.BufferOverflow;
3630
import kotlinx.coroutines.flow.FlowKt;
3731
import kotlinx.coroutines.flow.MutableSharedFlow;
@@ -354,71 +348,11 @@ public static String getSchemaName() {
354348

355349
public static boolean selectSchema(String schemaId) {
356350
Timber.d("Selecting schemaId=%s", schemaId);
357-
overWriteSchema(schemaId);
358351
boolean b = selectRimeSchema(schemaId);
359352
getContexts();
360353
return b;
361354
}
362355

363-
// 刷新当前输入方案
364-
public static void applySchemaChange() {
365-
String schema_id = getCurrentRimeSchema();
366-
// 实测直接select_schema(schema_id)方案没有重新载入,切换到不存在的方案,再切回去(会产生1秒的额外耗时).需要找到更好的方法
367-
// 不发生覆盖则不生效
368-
if (overWriteSchema(schema_id)) {
369-
selectRimeSchema("nill");
370-
selectRimeSchema(schema_id);
371-
}
372-
getContexts();
373-
}
374-
// 临时修改scheme文件参数
375-
// 临时修改build后的scheme可以避免build过程的耗时
376-
// 另外实际上jni读入yaml、修改、导出的效率并不高
377-
private static boolean overWriteSchema(String schema_id) {
378-
Map<String, String> map = new HashMap<>();
379-
String page_size = AppPrefs.defaultInstance().getKeyboard().getCandidatePageSize();
380-
Timber.d("overWriteSchema() page_size=" + page_size);
381-
if (!page_size.equals("0")) {
382-
map.put("page_size", page_size);
383-
}
384-
if (map.isEmpty()) return false;
385-
return overWriteSchema(schema_id, map);
386-
}
387-
388-
private static boolean overWriteSchema(String schema_id, Map<String, String> map) {
389-
if (schema_id == null) schema_id = getCurrentRimeSchema();
390-
File file =
391-
new File(Rime.getRimeUserDataDir() + File.separator + "build", schema_id + ".schema.yaml");
392-
try {
393-
FileReader in = new FileReader(file);
394-
BufferedReader bufIn = new BufferedReader(in);
395-
CharArrayWriter tempStream = new CharArrayWriter();
396-
String line = null;
397-
read:
398-
while ((line = bufIn.readLine()) != null) {
399-
for (String k : map.keySet()) {
400-
String key = k + ": ";
401-
if (line.contains(key)) {
402-
String value = ": " + map.get(k) + System.getProperty("line.separator");
403-
tempStream.write(line.replaceFirst(":.+", value));
404-
map.remove(k);
405-
continue read;
406-
}
407-
}
408-
tempStream.write(line);
409-
tempStream.append(System.getProperty("line.separator"));
410-
}
411-
bufIn.close();
412-
FileWriter out = new FileWriter(file);
413-
tempStream.writeTo(out);
414-
out.close();
415-
} catch (IOException e) {
416-
e.printStackTrace();
417-
return false;
418-
}
419-
return map.isEmpty();
420-
}
421-
422356
public static Rime get(boolean full_check) {
423357
if (self == null) {
424358
if (full_check) {
@@ -491,6 +425,9 @@ public static native boolean deployRimeConfigFile(
491425
public static native Map<String, Object> getRimeConfigMap(
492426
@NonNull String configId, @NonNull String key);
493427

428+
public static native void setRimeCustomConfigInt(
429+
@NonNull String configId, @NonNull Pair<String, Integer>[] keyValuePairs);
430+
494431
// testing
495432
public static native boolean simulateKeySequence(@NonNull String keySequence);
496433

app/src/main/java/com/osfans/trime/ui/fragments/KeyboardFragment.kt

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,15 @@ import androidx.lifecycle.lifecycleScope
77
import androidx.preference.Preference
88
import com.osfans.trime.R
99
import com.osfans.trime.core.Rime
10+
import com.osfans.trime.data.AppPrefs
11+
import com.osfans.trime.data.DataManager
1012
import com.osfans.trime.ime.core.Trime
1113
import com.osfans.trime.ui.components.PaddingPreferenceFragment
1214
import com.osfans.trime.ui.main.MainViewModel
1315
import com.osfans.trime.ui.main.soundPicker
16+
import kotlinx.coroutines.Dispatchers
1417
import kotlinx.coroutines.launch
18+
import kotlinx.coroutines.withContext
1519

1620
class KeyboardFragment :
1721
PaddingPreferenceFragment(),
@@ -41,7 +45,17 @@ class KeyboardFragment :
4145
trime?.loadConfig()
4246
}
4347
"keyboard__candidate_page_size" -> {
44-
Rime.applySchemaChange()
48+
val pageSize = AppPrefs.defaultInstance().keyboard.candidatePageSize.toInt()
49+
if (pageSize <= 0) return
50+
lifecycleScope.launch {
51+
withContext(Dispatchers.IO) {
52+
Rime.setRimeCustomConfigInt(
53+
"default",
54+
arrayOf("menu/page_size" to pageSize)
55+
)
56+
Rime.deployRimeConfigFile("${DataManager.userDataDir}/default.yaml", "")
57+
}
58+
}
4559
}
4660
}
4761
}

app/src/main/jni/librime_jni/jni-common.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
#include "jni-utils.h"
55

6-
static GlobalRefSingleton *GlobalRef;
6+
extern GlobalRefSingleton *GlobalRef;
77

88
#define TAG "rime.jni"
99
#ifdef ANDROID

app/src/main/jni/librime_jni/jni-utils.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,10 @@ class GlobalRefSingleton {
120120
jmethodID ArrayListInit;
121121
jmethodID ArrayListAdd;
122122

123+
jclass Pair;
124+
jmethodID PairFirst;
125+
jmethodID PairSecond;
126+
123127
jclass Rime;
124128
jmethodID HandleRimeNotification;
125129

@@ -186,6 +190,10 @@ class GlobalRefSingleton {
186190
ArrayListInit = env->GetMethodID(ArrayList, "<init>", "(I)V");
187191
ArrayListAdd = env->GetMethodID(ArrayList, "add", "(ILjava/lang/Object;)V");
188192

193+
Pair = reinterpret_cast<jclass>(env->NewGlobalRef(env->FindClass("kotlin/Pair")));
194+
PairFirst = env->GetMethodID(Pair, "getFirst", "()Ljava/lang/Object;");
195+
PairSecond = env->GetMethodID(Pair, "getSecond", "()Ljava/lang/Object;");
196+
189197
Rime = reinterpret_cast<jclass>(env->NewGlobalRef(env->FindClass("com/osfans/trime/core/Rime")));
190198
HandleRimeNotification = env->GetStaticMethodID(Rime, "handleRimeNotification", "(Ljava/lang/String;Ljava/lang/String;)V");
191199

app/src/main/jni/librime_jni/levers.cc

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,3 +56,21 @@ Java_com_osfans_trime_core_Rime_selectRimeSchemas(JNIEnv *env, jclass /* thiz */
5656
}
5757
return true;
5858
}
59+
60+
extern "C"
61+
JNIEXPORT void JNICALL
62+
Java_com_osfans_trime_core_Rime_setRimeCustomConfigInt(JNIEnv *env, jclass clazz, jstring config_id,
63+
jobjectArray key_value_pairs) {
64+
auto levers = get_levers();
65+
auto custom = levers->custom_settings_init(CString(env, config_id), "rime.trime");
66+
levers->load_settings(custom);
67+
int arrayLength = env->GetArrayLength(key_value_pairs);
68+
for (int i = 0; i < arrayLength; i++) {
69+
auto pair = JRef<>(env, env->GetObjectArrayElement(key_value_pairs, i));
70+
auto key = CString(env, (jstring) env->CallObjectMethod(pair, GlobalRef->PairFirst));
71+
auto value = (jint) (size_t) env->CallObjectMethod(pair, GlobalRef->PairSecond);
72+
levers->customize_int(custom, key, value);
73+
levers->save_settings(custom);
74+
}
75+
levers->custom_settings_destroy(custom);
76+
}

app/src/main/jni/librime_jni/rime_jni.cc

Lines changed: 2 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,8 @@ class Rime {
144144
#define RETURN_IF_NOT_RUNNING DO_IF_NOT_RUNNING(return)
145145
#define RETURN_VALUE_IF_NOT_RUNNING(v) DO_IF_NOT_RUNNING(return (v))
146146

147+
GlobalRefSingleton *GlobalRef;
148+
147149
JNIEXPORT jint JNICALL
148150
JNI_OnLoad(JavaVM* jvm, void* reserved)
149151
{
@@ -485,23 +487,6 @@ static jobject rimeConfigListToJObject(JNIEnv *env, RimeConfig* config, const st
485487
return obj;
486488
}
487489

488-
extern "C"
489-
JNIEXPORT jobject JNICALL
490-
Java_com_osfans_trime_core_Rime_config_1get_1list(JNIEnv *env, jclass /* thiz */, jstring name, jstring key) {
491-
const char* s = env->GetStringUTFChars(name, nullptr);
492-
RimeConfig config = {nullptr};
493-
Bool b = RimeConfigOpen(s, &config);
494-
env->ReleaseStringUTFChars(name, s);
495-
jobject value = nullptr;
496-
if (b) {
497-
s = env->GetStringUTFChars(key, nullptr);
498-
value = rimeConfigListToJObject(env, &config, s);
499-
env->ReleaseStringUTFChars(key, s);
500-
}
501-
RimeConfigClose(&config);
502-
return value;
503-
}
504-
505490
static jobject rimeConfigMapToJObject(JNIEnv *env, RimeConfig *config, const std::string &key) {
506491
auto rime = rime_get_api();
507492
RimeConfigIterator iter = {nullptr};

0 commit comments

Comments
 (0)