diff --git a/app/src/common/shared/org/mozilla/vrbrowser/ui/keyboards/ChinesePinyinKeyboard.java b/app/src/common/shared/org/mozilla/vrbrowser/ui/keyboards/ChinesePinyinKeyboard.java index f171f6804..74d0e6e3b 100644 --- a/app/src/common/shared/org/mozilla/vrbrowser/ui/keyboards/ChinesePinyinKeyboard.java +++ b/app/src/common/shared/org/mozilla/vrbrowser/ui/keyboards/ChinesePinyinKeyboard.java @@ -23,9 +23,16 @@ import androidx.annotation.NonNull; import androidx.annotation.Nullable; +import jp.co.omronsoft.openwnn.ComposingText; +import jp.co.omronsoft.openwnn.SymbolList; +import jp.co.omronsoft.openwnn.WnnWord; + public class ChinesePinyinKeyboard extends BaseKeyboard { private static final String LOGTAG = SystemUtils.createLogtag(ChinesePinyinKeyboard.class); private CustomKeyboard mKeyboard; + private CustomKeyboard mSymbolsKeyboard; + private SymbolList mSymbolsConverter; // For Emoji characters. + private List mEmojiList = null; private DBHelper mDB; private HashMap mKeymaps = new HashMap<>(); private HashMap mExtraKeymaps = new HashMap<>(); @@ -47,6 +54,22 @@ public CustomKeyboard getAlphabeticKeyboard() { return mKeyboard; } + @Nullable + @Override + public CustomKeyboard getSymbolsKeyboard() { + if (mSymbolsKeyboard == null) { + mSymbolsKeyboard = new CustomKeyboard(mContext.getApplicationContext(), R.xml.keyboard_symbols_pinyin); + // We use openwnn to provide us Emoji character although we are not using JPN keyboard. + mSymbolsConverter = new SymbolList(mContext, SymbolList.LANG_JA); + } + return mSymbolsKeyboard; + } + + @Override + public String getModeChangeKeyText() { + return mContext.getString(R.string.pinyin_keyboard_mode_change); + } + @Nullable @Override public CandidatesResult getCandidates(String aComposingText) { @@ -55,7 +78,14 @@ 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; aComposingText = aComposingText.replaceAll("\\s",""); @@ -123,6 +153,43 @@ public CandidatesResult getCandidates(String aComposingText) { return result; } + @Override + public CandidatesResult getEmojiCandidates(String aComposingText) { + if (mEmojiList == null) { + List words = new ArrayList<>(); + ComposingText text = new ComposingText(); + mSymbolsConverter.convert(text); + + int candidates = mSymbolsConverter.predict(text, 0, -1); + if (candidates > 0) { + WnnWord word; + while ((word = mSymbolsConverter.getNextCandidate()) != null) { + words.add(new Words(1, word.stroke, word.candidate)); + } + mEmojiList = words; + } + } + + CandidatesResult result = new CandidatesResult(); + result.words = mEmojiList; + result.action = CandidatesResult.Action.SHOW_CANDIDATES; + result.composing = aComposingText; + + return result; + } + + @Override + public String getComposingText(String aComposing, String aCode) { + if (mEmojiList != null) { + for (Words word : mEmojiList) { + if (word.code.equals(aCode)) { + return ""; + } + } + } + return aComposing.replaceFirst(Pattern.quote(aCode), ""); + } + private ArrayList getDisplayCode(String aKey) { ArrayList result = new ArrayList<>(); String remain = ""; diff --git a/app/src/common/shared/org/mozilla/vrbrowser/ui/keyboards/ChineseZhuyinKeyboard.java b/app/src/common/shared/org/mozilla/vrbrowser/ui/keyboards/ChineseZhuyinKeyboard.java index adcc105f2..19ab4841d 100644 --- a/app/src/common/shared/org/mozilla/vrbrowser/ui/keyboards/ChineseZhuyinKeyboard.java +++ b/app/src/common/shared/org/mozilla/vrbrowser/ui/keyboards/ChineseZhuyinKeyboard.java @@ -449,12 +449,10 @@ private String getString(Cursor aCursor, int aIndex) { return aCursor.getString(aIndex); } - class KeyMap { ArrayList displays = new ArrayList<>(); } - class DBWordHelper extends SQLiteAssetHelper { private static final String DATABASE_NAME = "zhuyin_words.db"; private static final int DATABASE_VERSION = 1; diff --git a/app/src/common/shared/org/mozilla/vrbrowser/ui/widgets/KeyboardWidget.java b/app/src/common/shared/org/mozilla/vrbrowser/ui/widgets/KeyboardWidget.java index 3e6dc2c7f..5545b095e 100644 --- a/app/src/common/shared/org/mozilla/vrbrowser/ui/widgets/KeyboardWidget.java +++ b/app/src/common/shared/org/mozilla/vrbrowser/ui/widgets/KeyboardWidget.java @@ -578,10 +578,11 @@ public void onLongPress(Keyboard.Key popupKey) { mPopUpHoverDeviceId = -1; mIsLongPress = true; - } else if (popupKey.codes[0] == CustomKeyboard.KEYCODE_SHIFT) { mIsLongPress = !mIsCapsLock; - + } else if (popupKey.codes[0] == CustomKeyboard.KEYCODE_DOMAIN) { + handleDomainLongPress(); + mIsLongPress = true; } } @@ -824,6 +825,12 @@ private void handleEmojiInput() { } private void handleDomain() { + if (!mIsLongPress) { + handleText(mCurrentKeyboard.getDomains()[0]); + } + } + + private void handleDomainLongPress() { ArrayList items = new ArrayList<>(); for (String item: mCurrentKeyboard.getDomains()) { items.add(new KeyboardSelectorView.Item(item, item)); @@ -837,6 +844,15 @@ private void handleDomain() { mDomainHoverDeviceId = -1; } + private void handleDomainChange(KeyboardSelectorView.Item aItem) { + handleText(aItem.title, true); + + disableShift(getSymbolsKeyboard()); + handleShift(false); + hideOverlays(); + updateCandidates(); + } + private void handleLanguageChange(KeyboardInterface aKeyboard) { cleanComposingText(); @@ -869,15 +885,6 @@ private void handleLanguageChange(KeyboardInterface aKeyboard) { mCurrentKeyboard.getAlphabeticKeyboard().setSpaceKeyLabel(spaceText); } - private void handleDomainChange(KeyboardSelectorView.Item aItem) { - handleText(aItem.title, true); - - disableShift(getSymbolsKeyboard()); - handleShift(false); - hideOverlays(); - updateCandidates(); - } - private void disableShift(@NonNull CustomKeyboard keyboard) { int[] shiftIndices = keyboard.getShiftKeyIndices(); for (int shiftIndex: shiftIndices) { diff --git a/app/src/main/res/values/non_L10n.xml b/app/src/main/res/values/non_L10n.xml index 58c47b506..ccfcbb4af 100644 --- a/app/src/main/res/values/non_L10n.xml +++ b/app/src/main/res/values/non_L10n.xml @@ -81,6 +81,7 @@ 选定 空格 确认 + 拼音 一聲 空格 diff --git a/app/src/main/res/xml/keyboard_symbols_pinyin.xml b/app/src/main/res/xml/keyboard_symbols_pinyin.xml new file mode 100644 index 000000000..e0510d9c5 --- /dev/null +++ b/app/src/main/res/xml/keyboard_symbols_pinyin.xml @@ -0,0 +1,62 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file