Skip to content
This repository was archived by the owner on Jul 22, 2024. It is now read-only.

Commit 77deb30

Browse files
authored
Avoid showing Zh-TW and Zh-CN keyboard candidate view when typing symbols. (#3507)
* Avoid showing candidate view when clicking special symbols in Traditional Chinese keyboard. * Showing Simplified Chinese keyboard slash symbol correctly.
1 parent 82fda1e commit 77deb30

3 files changed

Lines changed: 35 additions & 23 deletions

File tree

app/src/common/shared/org/mozilla/vrbrowser/ui/keyboards/ChinesePinyinKeyboard.java

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,6 @@ public class ChinesePinyinKeyboard extends BaseKeyboard {
3636
private DBHelper mDB;
3737
private HashMap<String, KeyMap> mKeymaps = new HashMap<>();
3838
private HashMap<String, KeyMap> mExtraKeymaps = new HashMap<>();
39-
private List<Character> mAutocompleteEndings = Arrays.asList(
40-
' ', ',', '。','!','?','ー'
41-
);
4239

4340
public ChinesePinyinKeyboard(Context aContext) {
4441
super(aContext);
@@ -78,15 +75,8 @@ public CandidatesResult getCandidates(String aComposingText) {
7875
}
7976

8077
// Autocomplete when special characters are clicked
81-
final char kBackslashCode = 92;
82-
char lastChar = aComposingText.charAt(aComposingText.length() - 1);
83-
84-
// When using backslashes ({@code \}) in the replacement string
85-
// will cause crash at `replaceFirst()`, so we need to replace it first.
86-
if (lastChar == kBackslashCode) {
87-
aComposingText = aComposingText.replace("\\", "\\\\");
88-
}
89-
boolean autocomponse = mAutocompleteEndings.indexOf(lastChar) >= 0;
78+
final char lastChar = aComposingText.charAt(aComposingText.length() - 1);
79+
final boolean autocompose = ("" + lastChar).matches("[^a-z]");
9080

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

150140
CandidatesResult result = new CandidatesResult();
151141
result.words = words;
152-
result.action = autocomponse ? CandidatesResult.Action.AUTO_COMPOSE : CandidatesResult.Action.SHOW_CANDIDATES;
142+
result.action = autocompose ? CandidatesResult.Action.AUTO_COMPOSE : CandidatesResult.Action.SHOW_CANDIDATES;
153143
result.composing = aComposingText;
154144
if (result.words.size() > 0) {
155-
String codeWithoutSpaces = StringUtils.removeSpaces(result.words.get(0).code);
156-
result.composing = aComposingText.replaceFirst(Pattern.quote(codeWithoutSpaces), result.words.get(0).code);
145+
final char kBackslashCode = 92;
146+
String newCode = result.words.get(0).code;
147+
148+
// When using backslashes ({@code \}) in the replacement string
149+
// will cause crash at `replaceFirst()`, so we need to replace it first.
150+
if (result.words.get(0).code.charAt(result.words.get(0).code.length() - 1)
151+
== kBackslashCode) {
152+
newCode = result.words.get(0).code.replace("\\", "\\\\");
153+
aComposingText = aComposingText.replace("\\", "\\\\");
154+
}
155+
String codeWithoutSpaces = StringUtils.removeSpaces(newCode);
156+
result.composing = aComposingText.replaceFirst(Pattern.quote(codeWithoutSpaces), newCode);
157157
}
158158

159159
return result;

app/src/common/shared/org/mozilla/vrbrowser/ui/keyboards/ChineseZhuyinKeyboard.java

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,8 @@ public CandidatesResult getCandidates(String aComposingText) {
8383

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

227-
// private String getNonZhuyinReg() {
228-
// // For characters that not belong to Zhuyin input.
229-
// final String reg = "[^ㄅ-ㄩ˙ˊˇˋˉ]";
230-
// return reg;
231-
// }
232-
233228
private List<Words> getDisplays(String aKey) {
234229
// Allow completion of uppercase/lowercase letters numbers, and symbols
235230
// aKey.length() > 1 only happens when switching from other keyboard.
@@ -242,7 +237,22 @@ private List<Words> getDisplays(String aKey) {
242237
code = GetTransCode(code);
243238
loadKeymapIfNotLoaded(code);
244239
KeyMap map = mKeymaps.get(code);
245-
return map != null ? map.displays : null;
240+
241+
if (map == null) {
242+
return Collections.singletonList(new Words(1, aKey, aKey));
243+
}
244+
// When detecting special symbols at the last character, and
245+
// because special symbols are not defined in our code book. We
246+
// need to add it back to our generated word for doing following
247+
// AUTO_COMPOSE.
248+
final String lastChar = "" + aKey.charAt(aKey.length()-1);
249+
if (map != null && lastChar.matches(nonZhuyinReg))
250+
{
251+
Words word = map.displays.get(0);
252+
return Collections.singletonList(new Words(1,
253+
word.code + lastChar, word.value + lastChar));
254+
}
255+
return map.displays;
246256
}
247257

248258

app/src/common/shared/org/mozilla/vrbrowser/ui/widgets/KeyboardWidget.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1064,6 +1064,7 @@ private void updateCandidates() {
10641064
setAutoCompletionVisible(candidates != null && candidates.words.size() > 0);
10651065
mAutoCompletionView.setItems(candidates != null ? candidates.words : null);
10661066
if (candidates != null && candidates.action == KeyboardInterface.CandidatesResult.Action.AUTO_COMPOSE) {
1067+
setAutoCompletionVisible(false);
10671068
onAutoCompletionItemClick(candidates.words.get(0));
10681069
} else if (candidates != null) {
10691070
postInputCommand(() -> displayComposingText(candidates.composing, ComposingAction.DO_NOT_FINISH));
@@ -1141,6 +1142,7 @@ private void displayComposingText(String aText, ComposingAction aAction) {
11411142
mComposingDisplayText = aText;
11421143
if (aAction == ComposingAction.FINISH) {
11431144
mInputConnection.finishComposingText();
1145+
mComposingText = "";
11441146
}
11451147
}
11461148

0 commit comments

Comments
 (0)