|
26 | 26 | import com.osfans.trime.data.DataManager;
|
27 | 27 | import com.osfans.trime.data.opencc.OpenCCDictManager;
|
28 | 28 | import com.osfans.trime.ime.core.Trime;
|
| 29 | +import java.io.BufferedReader; |
| 30 | +import java.io.CharArrayWriter; |
29 | 31 | import java.io.File;
|
| 32 | +import java.io.FileReader; |
| 33 | +import java.io.FileWriter; |
| 34 | +import java.io.IOException; |
30 | 35 | import java.util.ArrayList;
|
31 | 36 | import java.util.HashMap;
|
32 | 37 | import java.util.Iterator;
|
@@ -107,6 +112,7 @@ public int size() {
|
107 | 112 | }
|
108 | 113 |
|
109 | 114 | public RimeCandidate[] getCandidates() {
|
| 115 | + Timber.d("setWindow getCandidates() size()=" + size()); |
110 | 116 | return size() == 0 ? null : menu.candidates;
|
111 | 117 | }
|
112 | 118 | }
|
@@ -134,14 +140,20 @@ public static class RimeSchema {
|
134 | 140 | List<Map<String, Object>> switches = new ArrayList<Map<String, Object>>();
|
135 | 141 |
|
136 | 142 | public RimeSchema(String schema_id) {
|
| 143 | + Timber.d("RimeSchema() start"); |
137 | 144 | Object o;
|
138 | 145 | o = schema_get_value(schema_id, "schema");
|
139 | 146 | if (o == null || !(o instanceof Map)) return;
|
| 147 | + Timber.d("RimeSchema() switch"); |
140 | 148 | schema = (Map<String, Object>) o;
|
141 | 149 | o = schema_get_value(schema_id, "switches");
|
142 | 150 | if (o == null || !(o instanceof List)) return;
|
143 | 151 | switches = (List<Map<String, Object>>) o;
|
144 | 152 | check(); // 檢查不在選單中顯示的選項
|
| 153 | + Timber.d("RimeSchema() menu"); |
| 154 | + o = schema_get_value(schema_id, "menu"); |
| 155 | + if (o == null || !(o instanceof HashMap)) return; |
| 156 | + Timber.d("RimeSchema() menu.page_size=" + ((Map<Object, Object>) o).get("page_size")); |
145 | 157 | }
|
146 | 158 |
|
147 | 159 | public void check() {
|
@@ -296,8 +308,11 @@ public Rime(Context context, boolean full_check) {
|
296 | 308 | private static void initSchema() {
|
297 | 309 | mSchemaList = get_schema_list();
|
298 | 310 | String schema_id = getSchemaId();
|
| 311 | + Timber.d("initSchema() RimeSchema"); |
299 | 312 | mSchema = new RimeSchema(schema_id);
|
| 313 | + Timber.d("initSchema() getStatus"); |
300 | 314 | getStatus();
|
| 315 | + Timber.d("initSchema() done"); |
301 | 316 | }
|
302 | 317 |
|
303 | 318 | @SuppressWarnings("UnusedReturnValue")
|
@@ -367,6 +382,7 @@ public static boolean isVoidKeycode(int keycode) {
|
367 | 382 | private static boolean onKey(int keycode, int mask) {
|
368 | 383 | Timber.i("\t<TrimeInput>\tonkey()\tkeycode=%s, mask=%s", keycode, mask);
|
369 | 384 | if (isVoidKeycode(keycode)) return false;
|
| 385 | + // 此处调用native方法是耗时操作 |
370 | 386 | final boolean b = process_key(keycode, mask);
|
371 | 387 | Timber.i(
|
372 | 388 | "\t<TrimeInput>\tonkey()\tkeycode=%s, mask=%s, process_key result=%s", keycode, mask, b);
|
@@ -500,11 +516,72 @@ public static String getSchemaName() {
|
500 | 516 | }
|
501 | 517 |
|
502 | 518 | private static boolean selectSchema(String schema_id) {
|
| 519 | + Timber.d("selectSchema() schema_id=" + schema_id); |
| 520 | + overWriteSchema(schema_id); |
503 | 521 | boolean b = select_schema(schema_id);
|
504 | 522 | getContexts();
|
505 | 523 | return b;
|
506 | 524 | }
|
507 | 525 |
|
| 526 | + // 刷新当前输入方案 |
| 527 | + public static void applySchemaChange() { |
| 528 | + String schema_id = getSchemaId(); |
| 529 | + // 实测直接select_schema(schema_id)方案没有重新载入,切换到不存在的方案,再切回去(会产生1秒的额外耗时).需要找到更好的方法 |
| 530 | + // 不发生覆盖则不生效 |
| 531 | + if (overWriteSchema(schema_id)) { |
| 532 | + select_schema("nill"); |
| 533 | + select_schema(schema_id); |
| 534 | + } |
| 535 | + getContexts(); |
| 536 | + } |
| 537 | + // 临时修改scheme文件参数 |
| 538 | + // 临时修改build后的scheme可以避免build过程的耗时 |
| 539 | + // 另外实际上jni读入yaml、修改、导出的效率并不高 |
| 540 | + private static boolean overWriteSchema(String schema_id) { |
| 541 | + Map<String, String> map = new HashMap<>(); |
| 542 | + String page_size = AppPrefs.defaultInstance().getKeyboard().getCandidatePageSize(); |
| 543 | + Timber.d("overWriteSchema() page_size=" + page_size); |
| 544 | + if (!page_size.equals("0")) { |
| 545 | + map.put("page_size", page_size); |
| 546 | + } |
| 547 | + if (map.isEmpty()) return false; |
| 548 | + return overWriteSchema(schema_id, map); |
| 549 | + } |
| 550 | + |
| 551 | + private static boolean overWriteSchema(String schema_id, Map<String, String> map) { |
| 552 | + if (schema_id == null) schema_id = getSchemaId(); |
| 553 | + File file = |
| 554 | + new File(Rime.get_user_data_dir() + File.separator + "build", schema_id + ".schema.yaml"); |
| 555 | + try { |
| 556 | + FileReader in = new FileReader(file); |
| 557 | + BufferedReader bufIn = new BufferedReader(in); |
| 558 | + CharArrayWriter tempStream = new CharArrayWriter(); |
| 559 | + String line = null; |
| 560 | + read: |
| 561 | + while ((line = bufIn.readLine()) != null) { |
| 562 | + for (String k : map.keySet()) { |
| 563 | + String key = k + ": "; |
| 564 | + if (line.contains(key)) { |
| 565 | + String value = ": " + map.get(k) + System.getProperty("line.separator"); |
| 566 | + tempStream.write(line.replaceFirst(":.+", value)); |
| 567 | + map.remove(k); |
| 568 | + continue read; |
| 569 | + } |
| 570 | + } |
| 571 | + tempStream.write(line); |
| 572 | + tempStream.append(System.getProperty("line.separator")); |
| 573 | + } |
| 574 | + bufIn.close(); |
| 575 | + FileWriter out = new FileWriter(file); |
| 576 | + tempStream.writeTo(out); |
| 577 | + out.close(); |
| 578 | + } catch (IOException e) { |
| 579 | + e.printStackTrace(); |
| 580 | + return false; |
| 581 | + } |
| 582 | + return map.isEmpty(); |
| 583 | + } |
| 584 | + |
508 | 585 | public static boolean selectSchema(int id) {
|
509 | 586 | int n = mSchemaList.size();
|
510 | 587 | if (id < 0 || id >= n) return false;
|
@@ -549,9 +626,11 @@ public static void handleRimeNotification(String message_type, String message_va
|
549 | 626 | // Timber.i("message: [%s] %s", message_type, message_value);
|
550 | 627 | Timber.i("Notification: %s", event);
|
551 | 628 | final Trime trime = Trime.getService();
|
| 629 | + Timber.i("Notification: getService done, before det SchemaEvent"); |
552 | 630 | if (event instanceof RimeEvent.SchemaEvent) {
|
553 | 631 | initSchema();
|
554 | 632 | trime.initKeyboard();
|
| 633 | + Timber.i("Notification: solve SchemaEvent"); |
555 | 634 | } else if (event instanceof RimeEvent.OptionEvent) {
|
556 | 635 | getStatus();
|
557 | 636 | getContexts(); // 切換中英文、簡繁體時更新候選
|
|
0 commit comments