Skip to content

Commit 5c1f7c4

Browse files
committed
refactor: wait for rime deployment completed before doing any work
1 parent 1b81b63 commit 5c1f7c4

1 file changed

Lines changed: 149 additions & 127 deletions

File tree

  • app/src/main/java/com/osfans/trime/ime/core

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

Lines changed: 149 additions & 127 deletions
Original file line numberDiff line numberDiff line change
@@ -253,12 +253,14 @@ public void onWindowShown() {
253253
} else {
254254
Timber.i("onWindowShown...");
255255
}
256-
isWindowShown = true;
257256

258-
updateComposing();
257+
if (RimeWrapper.INSTANCE.isReady() && activeEditorInstance != null) {
258+
isWindowShown = true;
259+
updateComposing();
259260

260-
for (EventListener listener : eventListeners) {
261-
listener.onWindowShown();
261+
for (EventListener listener : eventListeners) {
262+
listener.onWindowShown();
263+
}
262264
}
263265
}
264266

@@ -352,27 +354,33 @@ public void restartSystemStartTimingSync() { // 防止重启系统 强行停止
352354

353355
@Override
354356
public void onCreate() {
357+
super.onCreate();
355358
// MUST WRAP all code within Service onCreate() in try..catch to prevent any crash loops
356359
try {
357360
// Additional try..catch wrapper as the event listeners chain or the super.onCreate() method
358361
// could crash
359362
// and lead to a crash loop
360-
try {
361-
Timber.d("onCreate");
362-
textInputManager = TextInputManager.Companion.getInstance(UiUtil.INSTANCE.isDarkMode(this));
363-
activeEditorInstance = new EditorInstance(this);
364-
inputFeedbackManager = new InputFeedbackManager(this);
365-
liquidKeyboard = new LiquidKeyboard(this);
366-
restartSystemStartTimingSync();
367-
} catch (Exception e) {
368-
Timber.e(e);
369-
super.onCreate();
370-
return;
371-
}
372-
super.onCreate();
373-
for (EventListener listener : eventListeners) {
374-
listener.onCreate();
375-
}
363+
Timber.d("onCreate");
364+
final InputMethodService context = this;
365+
RimeWrapper.INSTANCE.startup(
366+
() -> {
367+
Timber.d("Back to Trime.onCreate");
368+
textInputManager =
369+
TextInputManager.Companion.getInstance(UiUtil.INSTANCE.isDarkMode(context));
370+
activeEditorInstance = new EditorInstance(context);
371+
inputFeedbackManager = new InputFeedbackManager(context);
372+
liquidKeyboard = new LiquidKeyboard(context);
373+
restartSystemStartTimingSync();
374+
375+
try {
376+
for (EventListener listener : eventListeners) {
377+
listener.onCreate();
378+
}
379+
} catch (Exception e) {
380+
Timber.e(e);
381+
}
382+
Timber.d("Trime.onCreate completed");
383+
});
376384
} catch (Exception e) {
377385
Timber.e(e);
378386
}
@@ -724,44 +732,51 @@ public void onComputeInsets(InputMethodService.Insets outInsets) {
724732

725733
@Override
726734
public View onCreateInputView() {
727-
Timber.e("onCreateInputView()");
735+
Timber.d("onCreateInputView()");
728736
// 初始化键盘布局
729737
super.onCreateInputView();
730-
updateDarkMode();
731-
Theme.get(darkMode).initCurrentColors(darkMode);
732-
733-
inputRootBinding = InputRootBinding.inflate(LayoutInflater.from(this));
734-
mainKeyboardView = inputRootBinding.main.mainKeyboardView;
738+
RimeWrapper.INSTANCE.runAfterStarted(
739+
() -> {
740+
inputRootBinding = InputRootBinding.inflate(LayoutInflater.from(this));
741+
742+
mainKeyboardView = inputRootBinding.main.mainKeyboardView;
743+
// 初始化候选栏
744+
mCandidateRoot = inputRootBinding.main.candidateView.candidateRoot;
745+
mCandidate = inputRootBinding.main.candidateView.candidates;
746+
747+
// 候选词悬浮窗的容器
748+
compositionRootBinding = CompositionRootBinding.inflate(LayoutInflater.from(this));
749+
mComposition = compositionRootBinding.compositions;
750+
mPopupWindow = new PopupWindow(compositionRootBinding.compositionRoot);
751+
mPopupWindow.setClippingEnabled(false);
752+
mPopupWindow.setInputMethodMode(PopupWindow.INPUT_METHOD_NOT_NEEDED);
753+
if (VERSION.SDK_INT >= VERSION_CODES.M) {
754+
mPopupWindow.setWindowLayoutType(
755+
WindowManager.LayoutParams.TYPE_APPLICATION_ATTACHED_DIALOG);
756+
}
757+
hideCompositionView();
758+
mTabRoot = inputRootBinding.symbol.tabView.tabRoot;
735759

736-
// 初始化候选栏
737-
mCandidateRoot = inputRootBinding.main.candidateView.candidateRoot;
738-
mCandidate = inputRootBinding.main.candidateView.candidates;
760+
updateDarkMode();
761+
Theme.get(darkMode).initCurrentColors(darkMode);
739762

740-
// 候选词悬浮窗的容器
741-
compositionRootBinding = CompositionRootBinding.inflate(LayoutInflater.from(this));
742-
mComposition = compositionRootBinding.compositions;
743-
mPopupWindow = new PopupWindow(compositionRootBinding.compositionRoot);
744-
mPopupWindow.setClippingEnabled(false);
745-
mPopupWindow.setInputMethodMode(PopupWindow.INPUT_METHOD_NOT_NEEDED);
746-
if (VERSION.SDK_INT >= VERSION_CODES.M) {
747-
mPopupWindow.setWindowLayoutType(WindowManager.LayoutParams.TYPE_APPLICATION_ATTACHED_DIALOG);
748-
}
749-
hideCompositionView();
750-
mTabRoot = inputRootBinding.symbol.tabView.tabRoot;
763+
liquidKeyboard.setKeyboardView(inputRootBinding.symbol.liquidKeyboardView);
764+
tabView = inputRootBinding.symbol.tabView.tabs;
751765

752-
liquidKeyboard.setKeyboardView(inputRootBinding.symbol.liquidKeyboardView);
753-
tabView = inputRootBinding.symbol.tabView.tabs;
766+
for (EventListener listener : eventListeners) {
767+
assert inputRootBinding != null;
768+
listener.onInitializeInputUi(inputRootBinding);
769+
}
770+
loadBackground();
754771

755-
for (EventListener listener : eventListeners) {
756-
assert inputRootBinding != null;
757-
listener.onInitializeInputUi(inputRootBinding);
758-
}
759-
loadBackground();
772+
KeyboardSwitcher.newOrReset();
760773

761-
KeyboardSwitcher.newOrReset();
774+
setInputView(inputRootBinding.inputRoot);
775+
Timber.d("onCreateInputView - really ended");
776+
});
762777
Timber.i("onCreateInputView() finish");
763778

764-
return inputRootBinding.inputRoot;
779+
return null;
765780
}
766781

767782
public void setShowComment(boolean show_comment) {
@@ -786,87 +801,94 @@ public void onStartInputView(EditorInfo attribute, boolean restarting) {
786801
Timber.d("onStartInputView: restarting=%s", restarting);
787802
editorInfo = attribute;
788803

789-
if (updateDarkMode()) {
790-
initKeyboardDarkMode(darkMode);
791-
}
792-
793-
inputFeedbackManager.resumeSoundPool();
794-
inputFeedbackManager.resetPlayProgress();
795-
for (EventListener listener : eventListeners) {
796-
listener.onStartInputView(activeEditorInstance, restarting);
797-
}
798-
if (getPrefs().getOther().getShowStatusBarIcon()) {
799-
showStatusIcon(R.drawable.ic_trime_status); // 狀態欄圖標
800-
}
801-
bindKeyboardToInputView();
802-
// if (!restarting) setNavBarColor();
803-
setCandidatesViewShown(!Rime.isEmpty()); // 軟鍵盤出現時顯示候選欄
804+
RimeWrapper.INSTANCE.runAfterStarted(
805+
() -> {
806+
if (updateDarkMode()) {
807+
initKeyboardDarkMode(darkMode);
808+
}
804809

805-
if ((attribute.imeOptions & EditorInfo.IME_FLAG_NO_ENTER_ACTION)
806-
== EditorInfo.IME_FLAG_NO_ENTER_ACTION) {
807-
mainKeyboardView.resetEnterLabel();
808-
} else {
809-
mainKeyboardView.setEnterLabel(
810-
attribute.imeOptions & EditorInfo.IME_MASK_ACTION, attribute.actionLabel);
811-
}
810+
inputFeedbackManager.resumeSoundPool();
811+
inputFeedbackManager.resetPlayProgress();
812+
for (EventListener listener : eventListeners) {
813+
listener.onStartInputView(activeEditorInstance, restarting);
814+
}
815+
if (getPrefs().getOther().getShowStatusBarIcon()) {
816+
showStatusIcon(R.drawable.ic_trime_status); // 狀態欄圖標
817+
}
818+
bindKeyboardToInputView();
819+
// if (!restarting) setNavBarColor();
820+
setCandidatesViewShown(!Rime.isEmpty()); // 軟鍵盤出現時顯示候選欄
812821

813-
switch (attribute.inputType & InputType.TYPE_MASK_VARIATION) {
814-
case InputType.TYPE_TEXT_VARIATION_EMAIL_ADDRESS:
815-
case InputType.TYPE_TEXT_VARIATION_PASSWORD:
816-
case InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD:
817-
case InputType.TYPE_TEXT_VARIATION_WEB_EMAIL_ADDRESS:
818-
case InputType.TYPE_TEXT_VARIATION_WEB_PASSWORD:
819-
Timber.i(
820-
"EditorInfo: private;"
821-
+ " packageName="
822-
+ attribute.packageName
823-
+ "; fieldName="
824-
+ attribute.fieldName
825-
+ "; actionLabel="
826-
+ attribute.actionLabel
827-
+ "; inputType="
828-
+ attribute.inputType
829-
+ "; VARIATION="
830-
+ (attribute.inputType & InputType.TYPE_MASK_VARIATION)
831-
+ "; CLASS="
832-
+ (attribute.inputType & InputType.TYPE_MASK_CLASS)
833-
+ "; ACTION="
834-
+ (attribute.imeOptions & EditorInfo.IME_MASK_ACTION));
835-
normalTextEditor = false;
836-
break;
822+
if ((attribute.imeOptions & EditorInfo.IME_FLAG_NO_ENTER_ACTION)
823+
== EditorInfo.IME_FLAG_NO_ENTER_ACTION) {
824+
mainKeyboardView.resetEnterLabel();
825+
} else {
826+
mainKeyboardView.setEnterLabel(
827+
attribute.imeOptions & EditorInfo.IME_MASK_ACTION, attribute.actionLabel);
828+
}
837829

838-
default:
839-
Timber.i(
840-
"EditorInfo: normal;"
841-
+ " packageName="
842-
+ attribute.packageName
843-
+ "; fieldName="
844-
+ attribute.fieldName
845-
+ "; actionLabel="
846-
+ attribute.actionLabel
847-
+ "; inputType="
848-
+ attribute.inputType
849-
+ "; VARIATION="
850-
+ (attribute.inputType & InputType.TYPE_MASK_VARIATION)
851-
+ "; CLASS="
852-
+ (attribute.inputType & InputType.TYPE_MASK_CLASS)
853-
+ "; ACTION="
854-
+ (attribute.imeOptions & EditorInfo.IME_MASK_ACTION));
855-
856-
if ((attribute.imeOptions & EditorInfo.IME_FLAG_NO_PERSONALIZED_LEARNING)
857-
== EditorInfo.IME_FLAG_NO_PERSONALIZED_LEARNING) {
858-
// 应用程求以隐身模式打开键盘应用程序
859-
normalTextEditor = false;
860-
Timber.d("EditorInfo: normal -> private, IME_FLAG_NO_PERSONALIZED_LEARNING");
861-
} else if (attribute.packageName.equals(BuildConfig.APPLICATION_ID)
862-
|| getPrefs().getClipboard().getDraftExcludeApp().contains(attribute.packageName)) {
863-
normalTextEditor = false;
864-
Timber.d("EditorInfo: normal -> exclude, packageName=%s", attribute.packageName);
865-
} else {
866-
normalTextEditor = true;
867-
DraftHelper.INSTANCE.onInputEventChanged();
868-
}
869-
}
830+
switch (attribute.inputType & InputType.TYPE_MASK_VARIATION) {
831+
case InputType.TYPE_TEXT_VARIATION_EMAIL_ADDRESS:
832+
case InputType.TYPE_TEXT_VARIATION_PASSWORD:
833+
case InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD:
834+
case InputType.TYPE_TEXT_VARIATION_WEB_EMAIL_ADDRESS:
835+
case InputType.TYPE_TEXT_VARIATION_WEB_PASSWORD:
836+
Timber.i(
837+
"EditorInfo: private;"
838+
+ " packageName="
839+
+ attribute.packageName
840+
+ "; fieldName="
841+
+ attribute.fieldName
842+
+ "; actionLabel="
843+
+ attribute.actionLabel
844+
+ "; inputType="
845+
+ attribute.inputType
846+
+ "; VARIATION="
847+
+ (attribute.inputType & InputType.TYPE_MASK_VARIATION)
848+
+ "; CLASS="
849+
+ (attribute.inputType & InputType.TYPE_MASK_CLASS)
850+
+ "; ACTION="
851+
+ (attribute.imeOptions & EditorInfo.IME_MASK_ACTION));
852+
normalTextEditor = false;
853+
break;
854+
855+
default:
856+
Timber.i(
857+
"EditorInfo: normal;"
858+
+ " packageName="
859+
+ attribute.packageName
860+
+ "; fieldName="
861+
+ attribute.fieldName
862+
+ "; actionLabel="
863+
+ attribute.actionLabel
864+
+ "; inputType="
865+
+ attribute.inputType
866+
+ "; VARIATION="
867+
+ (attribute.inputType & InputType.TYPE_MASK_VARIATION)
868+
+ "; CLASS="
869+
+ (attribute.inputType & InputType.TYPE_MASK_CLASS)
870+
+ "; ACTION="
871+
+ (attribute.imeOptions & EditorInfo.IME_MASK_ACTION));
872+
873+
if ((attribute.imeOptions & EditorInfo.IME_FLAG_NO_PERSONALIZED_LEARNING)
874+
== EditorInfo.IME_FLAG_NO_PERSONALIZED_LEARNING) {
875+
// 应用程求以隐身模式打开键盘应用程序
876+
normalTextEditor = false;
877+
Timber.d("EditorInfo: normal -> private, IME_FLAG_NO_PERSONALIZED_LEARNING");
878+
} else if (attribute.packageName.equals(BuildConfig.APPLICATION_ID)
879+
|| getPrefs()
880+
.getClipboard()
881+
.getDraftExcludeApp()
882+
.contains(attribute.packageName)) {
883+
normalTextEditor = false;
884+
Timber.d("EditorInfo: normal -> exclude, packageName=%s", attribute.packageName);
885+
} else {
886+
normalTextEditor = true;
887+
DraftHelper.INSTANCE.onInputEventChanged();
888+
}
889+
}
890+
});
891+
RimeWrapper.INSTANCE.runCheck();
870892
}
871893

872894
@Override

0 commit comments

Comments
 (0)