Skip to content

Commit

Permalink
GBoard CJK support: recheck workaround, and fix blank langTag bug
Browse files Browse the repository at this point in the history
  • Loading branch information
knyipab committed May 4, 2024
1 parent faf8b9d commit da4fb84
Showing 1 changed file with 21 additions and 5 deletions.
26 changes: 21 additions & 5 deletions app/src/main/java/com/termux/x11/LorieView.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@
import com.termux.x11.input.InputStub;

import java.nio.charset.StandardCharsets;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.TimeUnit;
import java.util.regex.PatternSyntaxException;

@Keep @SuppressLint("WrongConstant")
Expand Down Expand Up @@ -276,8 +278,8 @@ public void onWindowFocusChanged(boolean hasFocus) {
clipboard.removePrimaryClipChangedListener(clipboardListener);
}
private InputMethodManager mIMM = (InputMethodManager)getContext().getSystemService( Context.INPUT_METHOD_SERVICE);
private String mImeLang = mIMM.getCurrentInputMethodSubtype().getLanguageTag().substring(0, 2);
private boolean mImeCJK = mImeLang.equals("zh") || mImeLang.equals("ko") || mImeLang.equals("ja");
private String mImeLang;
private boolean mImeCJK;
private boolean enableGBoardCJK;
void setWorkaroundGBoardCJK(boolean enabled) {
enableGBoardCJK = enabled;
Expand All @@ -286,18 +288,32 @@ void setWorkaroundGBoardCJK(boolean enabled) {
@Override
public InputConnection onCreateInputConnection(EditorInfo outAttrs) {
if (enableGBoardCJK) {
mImeLang = mIMM.getCurrentInputMethodSubtype().getLanguageTag().substring(0, 2);
mImeLang = mIMM.getCurrentInputMethodSubtype().getLanguageTag();
if (mImeLang.length() > 2)
mImeLang = mImeLang.substring(0, 2);
mImeCJK = mImeLang.equals("zh") || mImeLang.equals("ko") || mImeLang.equals("ja");
outAttrs.inputType = InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_FLAG_NO_SUGGESTIONS |
(mImeCJK ? InputType.TYPE_TEXT_VARIATION_NORMAL : InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD);
return new BaseInputConnection(this, false) {
private void checkRestartInput(boolean recheck) {
if (mIMM.getCurrentInputMethodSubtype().getLanguageTag().length() >= 2 && !mIMM.getCurrentInputMethodSubtype().getLanguageTag().substring(0, 2).equals(mImeLang))
mIMM.restartInput(LorieView.this);
else if (recheck) { // recheck needed because sometimes requestCursorUpdates() is called too fast, before InputMethodManager detect change in IM subtype
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S)
CompletableFuture.delayedExecutor(40, TimeUnit.MILLISECONDS).execute(() -> { checkRestartInput(false); });
else
new Thread(() -> { try {
Thread.sleep(40);
checkRestartInput(false);
} catch (Exception e) { System.err.println(e); } }).start();
}
}
@Override
public boolean requestCursorUpdates(int cursorUpdateMode) {
// workaround for Gboard
// Gboard calls requestCursorUpdates() whenever switching language
// check and then restart keyboard in different inputtype when needed
if (!mIMM.getCurrentInputMethodSubtype().getLanguageTag().substring(0, 2).equals(mImeLang))
mIMM.restartInput(LorieView.this);
checkRestartInput(true);
return super.requestCursorUpdates(cursorUpdateMode);
}

Expand Down

0 comments on commit da4fb84

Please sign in to comment.