|
| 1 | +package org.mozilla.vrbrowser.ui.keyboards; |
| 2 | + |
| 3 | +import android.content.Context; |
| 4 | + |
| 5 | +import androidx.annotation.NonNull; |
| 6 | +import androidx.annotation.Nullable; |
| 7 | + |
| 8 | +import org.mozilla.vrbrowser.R; |
| 9 | +import org.mozilla.vrbrowser.input.CustomKeyboard; |
| 10 | +import org.mozilla.vrbrowser.utils.StringUtils; |
| 11 | + |
| 12 | +import java.util.ArrayList; |
| 13 | +import java.util.Arrays; |
| 14 | +import java.util.List; |
| 15 | +import java.util.Locale; |
| 16 | + |
| 17 | +import jp.co.omronsoft.openwnn.ComposingText; |
| 18 | +import jp.co.omronsoft.openwnn.JAJP.OpenWnnEngineJAJP; |
| 19 | +import jp.co.omronsoft.openwnn.JAJP.Romkan; |
| 20 | +import jp.co.omronsoft.openwnn.LetterConverter; |
| 21 | +import jp.co.omronsoft.openwnn.StrSegment; |
| 22 | +import jp.co.omronsoft.openwnn.SymbolList; |
| 23 | +import jp.co.omronsoft.openwnn.WnnEngine; |
| 24 | +import jp.co.omronsoft.openwnn.WnnWord; |
| 25 | + |
| 26 | +public class JapaneseKeyboard extends BaseKeyboard { |
| 27 | + |
| 28 | + private static final String LOGTAG = "VRB"; |
| 29 | + |
| 30 | + private CustomKeyboard mKeyboard; |
| 31 | + private CustomKeyboard mSymbolsKeyboard; |
| 32 | + private List<Character> mAutocompleteEndings = Arrays.asList( |
| 33 | + ' ', '、', '。','!','?','ー' |
| 34 | + ); |
| 35 | + |
| 36 | + private SymbolList mSymbolsConverter; |
| 37 | + |
| 38 | + /** OpenWnn dictionary */ |
| 39 | + private WnnEngine mConverter; |
| 40 | + |
| 41 | + /** Pre-converter (for Romaji-to-Kana input, Hangul input, etc.) */ |
| 42 | + protected LetterConverter mPreConverter; |
| 43 | + |
| 44 | + /** The inputing/editing string */ |
| 45 | + protected ComposingText mComposingText; |
| 46 | + |
| 47 | + |
| 48 | + public JapaneseKeyboard(Context aContext) { |
| 49 | + super(aContext); |
| 50 | + |
| 51 | + mConverter = new OpenWnnEngineJAJP(); |
| 52 | + ((OpenWnnEngineJAJP) mConverter).setKeyboardType(OpenWnnEngineJAJP.KEYBOARD_QWERTY); |
| 53 | + ((OpenWnnEngineJAJP) mConverter).setDictionary(OpenWnnEngineJAJP.DIC_LANG_JP); |
| 54 | + mConverter.init(); |
| 55 | + |
| 56 | + mPreConverter = new Romkan(); |
| 57 | + mComposingText = new ComposingText(); |
| 58 | + } |
| 59 | + |
| 60 | + @NonNull |
| 61 | + @Override |
| 62 | + public CustomKeyboard getAlphabeticKeyboard() { |
| 63 | + if (mKeyboard == null) { |
| 64 | + mKeyboard = new CustomKeyboard(mContext.getApplicationContext(), R.xml.keyboard_qwerty_japanese); |
| 65 | + } |
| 66 | + |
| 67 | + return mKeyboard; |
| 68 | + } |
| 69 | + |
| 70 | + @Nullable |
| 71 | + @Override |
| 72 | + public CustomKeyboard getSymbolsKeyboard() { |
| 73 | + if (mSymbolsKeyboard == null) { |
| 74 | + mSymbolsKeyboard = new CustomKeyboard(mContext.getApplicationContext(), R.xml.keyboard_symbols_japanese); |
| 75 | + |
| 76 | + mSymbolsConverter = new SymbolList(mContext, SymbolList.LANG_JA); |
| 77 | + } |
| 78 | + return mSymbolsKeyboard; |
| 79 | + } |
| 80 | + |
| 81 | + @Nullable |
| 82 | + @Override |
| 83 | + public CandidatesResult getCandidates(String aComposingText) { |
| 84 | + if (StringUtils.isEmpty(aComposingText)) { |
| 85 | + mComposingText.clear(); |
| 86 | + return null; |
| 87 | + } |
| 88 | + |
| 89 | + // Autocomplete when special characters are clicked |
| 90 | + char lastChar = aComposingText.charAt(aComposingText.length() - 1); |
| 91 | + boolean autocompose = mAutocompleteEndings.indexOf(lastChar) >= 0; |
| 92 | + |
| 93 | + aComposingText = aComposingText.replaceAll("\\s",""); |
| 94 | + if (aComposingText.isEmpty()) { |
| 95 | + return null; |
| 96 | + } |
| 97 | + |
| 98 | + initializeComposingText(aComposingText); |
| 99 | + |
| 100 | + List<Words> words = new ArrayList<>(); |
| 101 | + int candidates = mConverter.predict(mComposingText, 0, -1); |
| 102 | + if (candidates > 0) { |
| 103 | + WnnWord word; |
| 104 | + while ((word = mConverter.getNextCandidate()) != null) { |
| 105 | + words.add(new Words(1, word.stroke, word.candidate)); |
| 106 | + } |
| 107 | + } |
| 108 | + |
| 109 | + CandidatesResult result = new CandidatesResult(); |
| 110 | + result.words = words; |
| 111 | + |
| 112 | + if (autocompose) { |
| 113 | + result.action = CandidatesResult.Action.AUTO_COMPOSE; |
| 114 | + result.composing = aComposingText; |
| 115 | + |
| 116 | + mComposingText.clear(); |
| 117 | + |
| 118 | + } else { |
| 119 | + result.action = CandidatesResult.Action.SHOW_CANDIDATES; |
| 120 | + result.composing = mComposingText.toString(ComposingText.LAYER2); |
| 121 | + } |
| 122 | + |
| 123 | + return result; |
| 124 | + } |
| 125 | + |
| 126 | + @Override |
| 127 | + public CandidatesResult getEmojiCandidates(String aComposingText) { |
| 128 | + ComposingText text = new ComposingText(); |
| 129 | + mSymbolsConverter.convert(text); |
| 130 | + |
| 131 | + List<Words> words = new ArrayList<>(); |
| 132 | + int candidates = mSymbolsConverter.predict(mComposingText, 0, -1); |
| 133 | + if (candidates > 0) { |
| 134 | + WnnWord word; |
| 135 | + while ((word = mSymbolsConverter.getNextCandidate()) != null) { |
| 136 | + words.add(new Words(1, word.stroke, word.candidate)); |
| 137 | + } |
| 138 | + } |
| 139 | + |
| 140 | + CandidatesResult result = new CandidatesResult(); |
| 141 | + result.words = words; |
| 142 | + result.action = CandidatesResult.Action.SHOW_CANDIDATES; |
| 143 | + result.composing = aComposingText; |
| 144 | + |
| 145 | + return result; |
| 146 | + } |
| 147 | + |
| 148 | + @Override |
| 149 | + public String getComposingText(String aComposing, String aCode) { |
| 150 | + return ""; |
| 151 | + } |
| 152 | + |
| 153 | + private void initializeComposingText(String text) { |
| 154 | + mComposingText.clear(); |
| 155 | + for (int i=0; i<text.length(); i++) { |
| 156 | + mComposingText.insertStrSegment(ComposingText.LAYER0, ComposingText.LAYER1, new StrSegment(text.substring(i, i+1))); |
| 157 | + mPreConverter.convert(mComposingText); |
| 158 | + } |
| 159 | + mComposingText.debugout(); |
| 160 | + } |
| 161 | + |
| 162 | + @Override |
| 163 | + public boolean supportsAutoCompletion() { |
| 164 | + return true; |
| 165 | + } |
| 166 | + |
| 167 | + @Override |
| 168 | + public boolean usesComposingText() { |
| 169 | + return true; |
| 170 | + } |
| 171 | + |
| 172 | + @Override |
| 173 | + public String getKeyboardTitle() { |
| 174 | + return StringUtils.getStringByLocale(mContext, R.string.settings_language_japanese, getLocale()); |
| 175 | + } |
| 176 | + |
| 177 | + @Override |
| 178 | + public Locale getLocale() { |
| 179 | + return Locale.JAPAN; |
| 180 | + } |
| 181 | + |
| 182 | + @Override |
| 183 | + public String getSpaceKeyText(String aComposingText) { |
| 184 | + if (aComposingText == null || aComposingText.trim().isEmpty()) { |
| 185 | + return mContext.getString(R.string.japanese_spacebar_space); |
| 186 | + } else { |
| 187 | + return mContext.getString(R.string.japanese_spacebar_selection); |
| 188 | + } |
| 189 | + } |
| 190 | + |
| 191 | + @Override |
| 192 | + public String getEnterKeyText(int aIMEOptions, String aComposingText) { |
| 193 | + if (aComposingText == null || aComposingText.trim().isEmpty()) { |
| 194 | + return super.getEnterKeyText(aIMEOptions, aComposingText); |
| 195 | + } else { |
| 196 | + return mContext.getString(R.string.japanese_enter_completion); |
| 197 | + } |
| 198 | + } |
| 199 | + |
| 200 | + @Override |
| 201 | + public String getModeChangeKeyText() { |
| 202 | + return mContext.getString(R.string.japanese_keyboard_mode_change); |
| 203 | + } |
| 204 | + |
| 205 | + @Override |
| 206 | + public void clear() { |
| 207 | + mConverter.init(); |
| 208 | + } |
| 209 | + |
| 210 | +} |
0 commit comments