Skip to content

Commit 843f52d

Browse files
WhiredPlanckBambooin
authored andcommitted
refactor(core): split more code from Trime service
1 parent bfa954a commit 843f52d

File tree

6 files changed

+170
-138
lines changed

6 files changed

+170
-138
lines changed

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -533,7 +533,7 @@ public static void onMessage(String message_type, String message_value) {
533533
getContexts(); // 切換中英文、簡繁體時更新候選
534534
final boolean value = !message_value.startsWith("!");
535535
final String option = message_value.substring(value ? 0 : 1);
536-
trime.onOptionChanged(option, value);
536+
trime.textInputManager.onOptionChanged(option, value);
537537
break;
538538
}
539539
mOnMessage = false;

app/src/main/java/com/osfans/trime/ime/core/EditorInstance.kt

+13-1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import android.view.inputmethod.InputConnection
1010
import com.osfans.trime.Rime
1111
import com.osfans.trime.ime.enums.InlineModeType
1212
import com.osfans.trime.ime.text.TextInputManager
13+
import timber.log.Timber
1314

1415
class EditorInstance(private val ims: InputMethodService) {
1516

@@ -18,6 +19,14 @@ class EditorInstance(private val ims: InputMethodService) {
1819
get() = ims.currentInputConnection
1920
val editorInfo: EditorInfo?
2021
get() = ims.currentInputEditorInfo
22+
val cursorCapsMode: Int
23+
get() {
24+
val ic = inputConnection ?: return 0
25+
val ei = editorInfo ?: return 0
26+
return if (ei.inputType != EditorInfo.TYPE_NULL) {
27+
ic.getCursorCapsMode(ei.inputType)
28+
} else 0
29+
}
2130
val textInputManager: TextInputManager
2231
get() = (ims as Trime).textInputManager
2332

@@ -38,7 +47,10 @@ class EditorInstance(private val ims: InputMethodService) {
3847
return true
3948
}
4049

41-
fun commitTextFromRime(): Boolean {
50+
/**
51+
* Commits the text got from Rime.
52+
*/
53+
fun commitRimeText(): Boolean {
4254
val ret = Rime.getCommit()
4355
if (ret) {
4456
commitText(Rime.getCommitText())

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

+32-126
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@
4040
import android.view.KeyEvent;
4141
import android.view.LayoutInflater;
4242
import android.view.View;
43-
import android.view.ViewGroup.LayoutParams;
43+
import android.view.ViewGroup;
4444
import android.view.Window;
4545
import android.view.WindowManager;
4646
import android.view.inputmethod.CursorAnchorInfo;
@@ -83,7 +83,6 @@
8383
import com.osfans.trime.setup.IntentReceiver;
8484
import com.osfans.trime.util.ShortcutUtils;
8585
import com.osfans.trime.util.StringUtils;
86-
import java.util.List;
8786
import java.util.Locale;
8887
import java.util.concurrent.CopyOnWriteArrayList;
8988
import kotlin.jvm.Synchronized;
@@ -214,31 +213,37 @@ public void run() {
214213
}
215214
};
216215

217-
public Trime() {
218-
try {
219-
self = this;
220-
textInputManager = TextInputManager.Companion.getInstance();
221-
} catch (Exception e) {
222-
e.fillInStackTrace();
223-
}
224-
}
225-
226216
@Synchronized
227217
@NonNull
228218
public static Trime getService() {
229219
assert self != null;
230220
return self;
231221
}
232222

223+
@Synchronized
224+
@Nullable
225+
public static Trime getServiceOrNull() {
226+
return self;
227+
}
228+
233229
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+
}
242247

243248
@Override
244249
public void onWindowShown() {
@@ -339,113 +344,25 @@ public void onCreate() {
339344
activeEditorInstance = new EditorInstance(this);
340345
imeManager = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
341346
inputFeedbackManager = new InputFeedbackManager(this);
342-
mIntentReceiver = new IntentReceiver();
343-
mIntentReceiver.registerReceiver(this);
344347

345-
final Config imeConfig = getImeConfig();
346-
loadConfig();
347348
keyboardSwitcher = new KeyboardSwitcher();
348349

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());
380351
clipBoardMonitor();
381352
} catch (Exception e) {
382353
super.onCreate();
383354
e.fillInStackTrace();
384355
return;
385356
}
386357
super.onCreate();
387-
for (EventListener listener : (List<EventListener>) eventListeners) {
358+
for (EventListener listener : eventListeners) {
388359
if (listener != null) listener.onCreate();
389360
}
390361
} catch (Exception e) {
391362
e.fillInStackTrace();
392363
}
393364
}
394365

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-
449366
public void selectLiquidKeyboard(final int tabIndex) {
450367
final LinearLayout symbolInputView =
451368
inputRootBinding != null ? inputRootBinding.symbol.symbolInput : null;
@@ -701,38 +618,27 @@ public View onCreateInputView() {
701618
super.onCreateInputView();
702619
inputRootBinding = InputRootBinding.inflate(LayoutInflater.from(this));
703620
mainKeyboardView = inputRootBinding.main.mainKeyboardView;
704-
// mainKeyboardView.setOnKeyboardActionListener(this);
705-
// mainKeyboardView.setShowHint(!Rime.getOption("_hide_key_hint"));
706621

707622
// 初始化候选栏
708-
mCandidateRoot = inputRootBinding.main.candidateView.getRoot();
709-
mTabRoot = inputRootBinding.symbol.tabView.getRoot();
623+
mCandidateRoot = inputRootBinding.main.candidateView.candidateRoot;
710624
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));
715625

716626
// 候选词悬浮窗的容器
717627
compositionRootBinding = CompositionRootBinding.inflate(LayoutInflater.from(this));
718-
hideCompositionView();
628+
mComposition = compositionRootBinding.compositions;
719629
mPopupWindow = new PopupWindow(compositionRootBinding.compositionRoot);
720630
mPopupWindow.setClippingEnabled(false);
721631
mPopupWindow.setInputMethodMode(PopupWindow.INPUT_METHOD_NOT_NEEDED);
722-
723-
mComposition = (Composition) compositionRootBinding.compositionRoot.getChildAt(0);
724-
725632
if (VERSION.SDK_INT >= VERSION_CODES.M) {
726633
mPopupWindow.setWindowLayoutType(dialogType);
727634
}
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;
731637

732638
liquidKeyboard.setView(inputRootBinding.symbol.liquidKeyboardView);
733-
tabView = inputRootBinding.symbol.tabView.tab;
639+
tabView = inputRootBinding.symbol.tabView.tabs;
734640

735-
for (EventListener listener : (List<EventListener>) eventListeners) {
641+
for (EventListener listener : eventListeners) {
736642
assert inputRootBinding != null;
737643
if (listener != null) listener.onInitializeInputUi(inputRootBinding);
738644
}

0 commit comments

Comments
 (0)