Skip to content

Commit

Permalink
Avoid showing Zh-TW and Zh-CN keyboard candidate view when typing sym…
Browse files Browse the repository at this point in the history
…bols. (#3507)

* Avoid showing candidate view when clicking special symbols in Traditional Chinese keyboard.

* Showing Simplified Chinese keyboard slash symbol correctly.
  • Loading branch information
daoshengmu committed Jun 22, 2020
1 parent 82fda1e commit 77deb30
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,6 @@ public class ChinesePinyinKeyboard extends BaseKeyboard {
private DBHelper mDB;
private HashMap<String, KeyMap> mKeymaps = new HashMap<>();
private HashMap<String, KeyMap> mExtraKeymaps = new HashMap<>();
private List<Character> mAutocompleteEndings = Arrays.asList(
' ', ',', '。','!','?','ー'
);

public ChinesePinyinKeyboard(Context aContext) {
super(aContext);
Expand Down Expand Up @@ -78,15 +75,8 @@ public CandidatesResult getCandidates(String aComposingText) {
}

// Autocomplete when special characters are clicked
final char kBackslashCode = 92;
char lastChar = aComposingText.charAt(aComposingText.length() - 1);

// When using backslashes ({@code \}) in the replacement string
// will cause crash at `replaceFirst()`, so we need to replace it first.
if (lastChar == kBackslashCode) {
aComposingText = aComposingText.replace("\\", "\\\\");
}
boolean autocomponse = mAutocompleteEndings.indexOf(lastChar) >= 0;
final char lastChar = aComposingText.charAt(aComposingText.length() - 1);
final boolean autocompose = ("" + lastChar).matches("[^a-z]");

aComposingText = aComposingText.replaceAll("\\s","");
if (aComposingText.isEmpty()) {
Expand Down Expand Up @@ -149,11 +139,21 @@ public CandidatesResult getCandidates(String aComposingText) {

CandidatesResult result = new CandidatesResult();
result.words = words;
result.action = autocomponse ? CandidatesResult.Action.AUTO_COMPOSE : CandidatesResult.Action.SHOW_CANDIDATES;
result.action = autocompose ? CandidatesResult.Action.AUTO_COMPOSE : CandidatesResult.Action.SHOW_CANDIDATES;
result.composing = aComposingText;
if (result.words.size() > 0) {
String codeWithoutSpaces = StringUtils.removeSpaces(result.words.get(0).code);
result.composing = aComposingText.replaceFirst(Pattern.quote(codeWithoutSpaces), result.words.get(0).code);
final char kBackslashCode = 92;
String newCode = result.words.get(0).code;

// When using backslashes ({@code \}) in the replacement string
// will cause crash at `replaceFirst()`, so we need to replace it first.
if (result.words.get(0).code.charAt(result.words.get(0).code.length() - 1)
== kBackslashCode) {
newCode = result.words.get(0).code.replace("\\", "\\\\");
aComposingText = aComposingText.replace("\\", "\\\\");
}
String codeWithoutSpaces = StringUtils.removeSpaces(newCode);
result.composing = aComposingText.replaceFirst(Pattern.quote(codeWithoutSpaces), newCode);
}

return result;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,8 @@ public CandidatesResult getCandidates(String aComposingText) {

// If using non-Zhuyin symbols like numeric, abc, special symbols,
// we just need to compose them.
if (aComposingText.matches(nonZhuyinReg)) {
String lastChar = "" + aComposingText.charAt(aComposingText.length() - 1);
if (lastChar.matches(nonZhuyinReg)) {
CandidatesResult result = new CandidatesResult();
result.words = getDisplays(aComposingText);
result.action = CandidatesResult.Action.AUTO_COMPOSE;
Expand Down Expand Up @@ -224,12 +225,6 @@ public String getModeChangeKeyText() {
return mContext.getString(R.string.zhuyin_keyboard_mode_change);
}

// private String getNonZhuyinReg() {
// // For characters that not belong to Zhuyin input.
// final String reg = "[^ㄅ-ㄩ˙ˊˇˋˉ]";
// return reg;
// }

private List<Words> getDisplays(String aKey) {
// Allow completion of uppercase/lowercase letters numbers, and symbols
// aKey.length() > 1 only happens when switching from other keyboard.
Expand All @@ -242,7 +237,22 @@ private List<Words> getDisplays(String aKey) {
code = GetTransCode(code);
loadKeymapIfNotLoaded(code);
KeyMap map = mKeymaps.get(code);
return map != null ? map.displays : null;

if (map == null) {
return Collections.singletonList(new Words(1, aKey, aKey));
}
// When detecting special symbols at the last character, and
// because special symbols are not defined in our code book. We
// need to add it back to our generated word for doing following
// AUTO_COMPOSE.
final String lastChar = "" + aKey.charAt(aKey.length()-1);
if (map != null && lastChar.matches(nonZhuyinReg))
{
Words word = map.displays.get(0);
return Collections.singletonList(new Words(1,
word.code + lastChar, word.value + lastChar));
}
return map.displays;
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1064,6 +1064,7 @@ private void updateCandidates() {
setAutoCompletionVisible(candidates != null && candidates.words.size() > 0);
mAutoCompletionView.setItems(candidates != null ? candidates.words : null);
if (candidates != null && candidates.action == KeyboardInterface.CandidatesResult.Action.AUTO_COMPOSE) {
setAutoCompletionVisible(false);
onAutoCompletionItemClick(candidates.words.get(0));
} else if (candidates != null) {
postInputCommand(() -> displayComposingText(candidates.composing, ComposingAction.DO_NOT_FINISH));
Expand Down Expand Up @@ -1141,6 +1142,7 @@ private void displayComposingText(String aText, ComposingAction aAction) {
mComposingDisplayText = aText;
if (aAction == ComposingAction.FINISH) {
mInputConnection.finishComposingText();
mComposingText = "";
}
}

Expand Down

0 comments on commit 77deb30

Please sign in to comment.