|
40 | 40 | import android.view.KeyEvent;
|
41 | 41 | import android.view.LayoutInflater;
|
42 | 42 | import android.view.View;
|
43 |
| -import android.view.ViewGroup.LayoutParams; |
| 43 | +import android.view.ViewGroup; |
44 | 44 | import android.view.Window;
|
45 | 45 | import android.view.WindowManager;
|
46 | 46 | import android.view.inputmethod.CursorAnchorInfo;
|
|
83 | 83 | import com.osfans.trime.setup.IntentReceiver;
|
84 | 84 | import com.osfans.trime.util.ShortcutUtils;
|
85 | 85 | import com.osfans.trime.util.StringUtils;
|
86 |
| -import java.util.List; |
87 | 86 | import java.util.Locale;
|
88 | 87 | import java.util.concurrent.CopyOnWriteArrayList;
|
89 | 88 | import kotlin.jvm.Synchronized;
|
@@ -214,31 +213,37 @@ public void run() {
|
214 | 213 | }
|
215 | 214 | };
|
216 | 215 |
|
217 |
| - public Trime() { |
218 |
| - try { |
219 |
| - self = this; |
220 |
| - textInputManager = TextInputManager.Companion.getInstance(); |
221 |
| - } catch (Exception e) { |
222 |
| - e.fillInStackTrace(); |
223 |
| - } |
224 |
| - } |
225 |
| - |
226 | 216 | @Synchronized
|
227 | 217 | @NonNull
|
228 | 218 | public static Trime getService() {
|
229 | 219 | assert self != null;
|
230 | 220 | return self;
|
231 | 221 | }
|
232 | 222 |
|
| 223 | + @Synchronized |
| 224 | + @Nullable |
| 225 | + public static Trime getServiceOrNull() { |
| 226 | + return self; |
| 227 | + } |
| 228 | + |
233 | 229 | private static final Handler syncBackgroundHandler =
|
234 |
| - new Handler( |
235 |
| - msg -> { |
236 |
| - if (!((Trime) msg.obj).isShowInputRequested()) { // 若当前没有输入面板,则后台同步。防止面板关闭后5秒内再次打开 |
237 |
| - ShortcutUtils.INSTANCE.syncInBackground((Trime) msg.obj); |
238 |
| - ((Trime) msg.obj).loadConfig(); |
239 |
| - } |
240 |
| - return false; |
241 |
| - }); |
| 230 | + new Handler( |
| 231 | + msg -> { |
| 232 | + if (!((Trime) msg.obj).isShowInputRequested()) { // 若当前没有输入面板,则后台同步。防止面板关闭后5秒内再次打开 |
| 233 | + ShortcutUtils.INSTANCE.syncInBackground((Trime) msg.obj); |
| 234 | + ((Trime) msg.obj).loadConfig(); |
| 235 | + } |
| 236 | + return false; |
| 237 | + }); |
| 238 | + |
| 239 | + public Trime() { |
| 240 | + try { |
| 241 | + self = this; |
| 242 | + textInputManager = TextInputManager.Companion.getInstance(); |
| 243 | + } catch (Exception e) { |
| 244 | + e.fillInStackTrace(); |
| 245 | + } |
| 246 | + } |
242 | 247 |
|
243 | 248 | @Override
|
244 | 249 | public void onWindowShown() {
|
@@ -339,113 +344,25 @@ public void onCreate() {
|
339 | 344 | activeEditorInstance = new EditorInstance(this);
|
340 | 345 | imeManager = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
|
341 | 346 | inputFeedbackManager = new InputFeedbackManager(this);
|
342 |
| - mIntentReceiver = new IntentReceiver(); |
343 |
| - mIntentReceiver.registerReceiver(this); |
344 | 347 |
|
345 |
| - final Config imeConfig = getImeConfig(); |
346 |
| - loadConfig(); |
347 | 348 | keyboardSwitcher = new KeyboardSwitcher();
|
348 | 349 |
|
349 |
| - String s = |
350 |
| - TextUtils.isEmpty(imeConfig.getString("locale")) ? imeConfig.getString("locale") : ""; |
351 |
| - final String DELIMITER = "[-_]"; |
352 |
| - if (s.contains(DELIMITER)) { |
353 |
| - final String[] lc = s.split(DELIMITER); |
354 |
| - if (lc.length == 3) { |
355 |
| - locales[0] = new Locale(lc[0], lc[1], lc[2]); |
356 |
| - } else { |
357 |
| - locales[0] = new Locale(lc[0], lc[1]); |
358 |
| - } |
359 |
| - } else { |
360 |
| - locales[0] = Locale.getDefault(); |
361 |
| - } |
362 |
| - |
363 |
| - s = |
364 |
| - TextUtils.isEmpty(imeConfig.getString("latin_locale")) |
365 |
| - ? imeConfig.getString("latin_locale") |
366 |
| - : "en_US"; |
367 |
| - if (s.contains(DELIMITER)) { |
368 |
| - final String[] lc = s.split(DELIMITER); |
369 |
| - if (lc.length == 3) { |
370 |
| - locales[1] = new Locale(lc[0], lc[1], lc[2]); |
371 |
| - } else { |
372 |
| - locales[1] = new Locale(lc[0], lc[1]); |
373 |
| - } |
374 |
| - } else { |
375 |
| - locales[0] = Locale.ENGLISH; |
376 |
| - locales[1] = new Locale(s); |
377 |
| - } |
378 |
| - |
379 |
| - liquidKeyboard = new LiquidKeyboard(this, imeConfig.getClipboardMaxSize()); |
| 350 | + liquidKeyboard = new LiquidKeyboard(this, getImeConfig().getClipboardMaxSize()); |
380 | 351 | clipBoardMonitor();
|
381 | 352 | } catch (Exception e) {
|
382 | 353 | super.onCreate();
|
383 | 354 | e.fillInStackTrace();
|
384 | 355 | return;
|
385 | 356 | }
|
386 | 357 | super.onCreate();
|
387 |
| - for (EventListener listener : (List<EventListener>) eventListeners) { |
| 358 | + for (EventListener listener : eventListeners) { |
388 | 359 | if (listener != null) listener.onCreate();
|
389 | 360 | }
|
390 | 361 | } catch (Exception e) {
|
391 | 362 | e.fillInStackTrace();
|
392 | 363 | }
|
393 | 364 | }
|
394 | 365 |
|
395 |
| - public void onOptionChanged(@NonNull String option, boolean value) { |
396 |
| - switch (option) { |
397 |
| - case "ascii_mode": |
398 |
| - if (!textInputManager.isTempAsciiMode()) { |
399 |
| - textInputManager.setAsciiMode(value); // 切換中西文時保存狀態 |
400 |
| - } |
401 |
| - if (inputFeedbackManager != null) |
402 |
| - inputFeedbackManager.setTtsLanguage(locales[value ? 1 : 0]); |
403 |
| - break; |
404 |
| - case "_hide_comment": |
405 |
| - setShowComment(!value); |
406 |
| - break; |
407 |
| - case "_hide_candidate": |
408 |
| - if (mCandidateRoot != null) mCandidateRoot.setVisibility(!value ? View.VISIBLE : View.GONE); |
409 |
| - setCandidatesViewShown(textInputManager.isComposable() && !value); |
410 |
| - break; |
411 |
| - case "_liquid_keyboard": |
412 |
| - selectLiquidKeyboard(0); |
413 |
| - break; |
414 |
| - case "_hide_key_hint": |
415 |
| - if (mainKeyboardView != null) mainKeyboardView.setShowHint(!value); |
416 |
| - break; |
417 |
| - default: |
418 |
| - if (option.startsWith("_keyboard_") |
419 |
| - && option.length() > 10 |
420 |
| - && value |
421 |
| - && (keyboardSwitcher != null)) { |
422 |
| - final String keyboard = option.substring(10); |
423 |
| - keyboardSwitcher.switchToKeyboard(keyboard); |
424 |
| - textInputManager.setTempAsciiMode(keyboardSwitcher.getAsciiMode()); |
425 |
| - bindKeyboardToInputView(); |
426 |
| - } else if (option.startsWith("_key_") && option.length() > 5 && value) { |
427 |
| - boolean needUpdate = textInputManager.getShouldUpdateRimeOption(); |
428 |
| - if (needUpdate) |
429 |
| - textInputManager.setShouldUpdateRimeOption(false); // 防止在 onMessage 中 setOption |
430 |
| - final String key = option.substring(5); |
431 |
| - textInputManager.onEvent(new Event(key)); |
432 |
| - if (needUpdate) textInputManager.setShouldUpdateRimeOption(true); |
433 |
| - } else if (option.startsWith("_one_hand_mode")) { |
434 |
| - char c = option.charAt(option.length() - 1); |
435 |
| - if (c == '1' && value) oneHandMode = 1; |
436 |
| - else if (c == '2' && value) oneHandMode = 2; |
437 |
| - else if (c == '3') oneHandMode = value ? 1 : 2; |
438 |
| - else oneHandMode = 0; |
439 |
| - |
440 |
| - loadBackground(); |
441 |
| - if (keyboardSwitcher != null) keyboardSwitcher.newOrReset(); |
442 |
| - resetKeyboard(); |
443 |
| - bindKeyboardToInputView(); |
444 |
| - } |
445 |
| - } |
446 |
| - if (mainKeyboardView != null) mainKeyboardView.invalidateAllKeys(); |
447 |
| - } |
448 |
| - |
449 | 366 | public void selectLiquidKeyboard(final int tabIndex) {
|
450 | 367 | final LinearLayout symbolInputView =
|
451 | 368 | inputRootBinding != null ? inputRootBinding.symbol.symbolInput : null;
|
@@ -701,38 +618,27 @@ public View onCreateInputView() {
|
701 | 618 | super.onCreateInputView();
|
702 | 619 | inputRootBinding = InputRootBinding.inflate(LayoutInflater.from(this));
|
703 | 620 | mainKeyboardView = inputRootBinding.main.mainKeyboardView;
|
704 |
| - // mainKeyboardView.setOnKeyboardActionListener(this); |
705 |
| - // mainKeyboardView.setShowHint(!Rime.getOption("_hide_key_hint")); |
706 | 621 |
|
707 | 622 | // 初始化候选栏
|
708 |
| - mCandidateRoot = inputRootBinding.main.candidateView.getRoot(); |
709 |
| - mTabRoot = inputRootBinding.symbol.tabView.getRoot(); |
| 623 | + mCandidateRoot = inputRootBinding.main.candidateView.candidateRoot; |
710 | 624 | mCandidate = inputRootBinding.main.candidateView.candidates;
|
711 |
| - // mCandidate.setCandidateListener(this); |
712 |
| - mCandidateRoot.setPageStr( |
713 |
| - () -> handleKey(KeyEvent.KEYCODE_PAGE_DOWN, 0), |
714 |
| - () -> handleKey(KeyEvent.KEYCODE_PAGE_UP, 0)); |
715 | 625 |
|
716 | 626 | // 候选词悬浮窗的容器
|
717 | 627 | compositionRootBinding = CompositionRootBinding.inflate(LayoutInflater.from(this));
|
718 |
| - hideCompositionView(); |
| 628 | + mComposition = compositionRootBinding.compositions; |
719 | 629 | mPopupWindow = new PopupWindow(compositionRootBinding.compositionRoot);
|
720 | 630 | mPopupWindow.setClippingEnabled(false);
|
721 | 631 | mPopupWindow.setInputMethodMode(PopupWindow.INPUT_METHOD_NOT_NEEDED);
|
722 |
| - |
723 |
| - mComposition = (Composition) compositionRootBinding.compositionRoot.getChildAt(0); |
724 |
| - |
725 | 632 | if (VERSION.SDK_INT >= VERSION_CODES.M) {
|
726 | 633 | mPopupWindow.setWindowLayoutType(dialogType);
|
727 | 634 | }
|
728 |
| - |
729 |
| - mComposition.setShowComment(!Rime.getOption("_hide_comment")); |
730 |
| - mCandidateRoot.setVisibility(!Rime.getOption("_hide_candidate") ? View.VISIBLE : View.GONE); |
| 635 | + hideCompositionView(); |
| 636 | + mTabRoot = inputRootBinding.symbol.tabView.tabRoot; |
731 | 637 |
|
732 | 638 | liquidKeyboard.setView(inputRootBinding.symbol.liquidKeyboardView);
|
733 |
| - tabView = inputRootBinding.symbol.tabView.tab; |
| 639 | + tabView = inputRootBinding.symbol.tabView.tabs; |
734 | 640 |
|
735 |
| - for (EventListener listener : (List<EventListener>) eventListeners) { |
| 641 | + for (EventListener listener : eventListeners) { |
736 | 642 | assert inputRootBinding != null;
|
737 | 643 | if (listener != null) listener.onInitializeInputUi(inputRootBinding);
|
738 | 644 | }
|
|
0 commit comments