5252import java .util .concurrent .Executors ;
5353
5454public class SoftKeyboard extends InputMethodService
55- implements KeyboardView .OnKeyboardActionListener {
55+ implements KeyboardView .OnKeyboardActionListener {
5656
5757 private InputMethodManager mInputMethodManager ;
5858
@@ -61,11 +61,11 @@ public class SoftKeyboard extends InputMethodService
6161 private int mLastDisplayWidth ;
6262 private boolean mCapsLock ;
6363 private long mLastShiftTime ;
64-
64+
6565 private LatinKeyboard mSymbolsKeyboard ;
6666 private LatinKeyboard mSymbolsShiftedKeyboard ;
6767 private LatinKeyboard mQwertyKeyboard ;
68-
68+
6969 private LatinKeyboard mCurKeyboard ;
7070
7171 private ExecutorService executorService ;
@@ -75,9 +75,10 @@ public class SoftKeyboard extends InputMethodService
7575 private Map <String , List <String >> pinyinMap = new HashMap <>();
7676 private InputMode inputMode = InputMode .English ;
7777
78- @ Override public void onCreate () {
78+ @ Override
79+ public void onCreate () {
7980 super .onCreate ();
80- mInputMethodManager = (InputMethodManager )getSystemService (INPUT_METHOD_SERVICE );
81+ mInputMethodManager = (InputMethodManager ) getSystemService (INPUT_METHOD_SERVICE );
8182
8283 if (trie == null ) {
8384 executorService = Executors .newSingleThreadExecutor ();
@@ -89,7 +90,8 @@ private void loadDictionaryAsync() {
8990 executorService .execute (() -> {
9091 Gson gson = new Gson ();
9192 String jsonString = DictUtil .getContentFromAssets (getApplicationContext (), "google_227800_words.json" );
92- Type mapType = new TypeToken <Map <String , Long >>(){}.getType ();
93+ Type mapType = new TypeToken <Map <String , Long >>() {
94+ }.getType ();
9395 Map <String , Long > map = gson .fromJson (jsonString , mapType );
9496
9597 trie = new PatriciaTrie <>();
@@ -98,7 +100,8 @@ private void loadDictionaryAsync() {
98100 }
99101
100102 String pinyinJson = DictUtil .getContentFromAssets (getApplicationContext (), "cedict.json" );
101- Type pinyinType = new TypeToken <Map <String , List <String >>>(){}.getType ();
103+ Type pinyinType = new TypeToken <Map <String , List <String >>>() {
104+ }.getType ();
102105 pinyinMap = gson .fromJson (pinyinJson , pinyinType );
103106
104107 String pinyinTxt = DictUtil .getContentFromAssets (getApplicationContext (), "google_pinyin_rawdict_utf8_65105_freq.txt" );
@@ -163,7 +166,8 @@ Context getDisplayContext() {
163166 return createDisplayContext (wm .getDefaultDisplay ());
164167 }
165168
166- @ Override public void onInitializeInterface () {
169+ @ Override
170+ public void onInitializeInterface () {
167171 final Context displayContext = getDisplayContext ();
168172
169173 if (mQwertyKeyboard != null ) {
@@ -179,7 +183,8 @@ Context getDisplayContext() {
179183 mSymbolsShiftedKeyboard = new LatinKeyboard (displayContext , R .xml .symbols_shift );
180184 }
181185
182- @ Override public View onCreateInputView () {
186+ @ Override
187+ public View onCreateInputView () {
183188 mInputView = (KeyboardView ) getLayoutInflater ().inflate (R .layout .input , null );
184189 mInputView .setOnKeyboardActionListener (this );
185190 mInputView .setPreviewEnabled (false );
@@ -195,12 +200,13 @@ private void setLatinKeyboard(LatinKeyboard nextKeyboard) {
195200 mInputView .setKeyboard (nextKeyboard );
196201 }
197202
198- @ Override public void onStartInput (EditorInfo attribute , boolean restarting ) {
203+ @ Override
204+ public void onStartInput (EditorInfo attribute , boolean restarting ) {
199205 super .onStartInput (attribute , restarting );
200206
201207 // https://issuetracker.google.com/issues/246132117
202208 setCandidatesViewShown (true );
203-
209+
204210 // We are now going to initialize our state based on the type of
205211 // text being edited.
206212 switch (attribute .inputType & InputType .TYPE_MASK_CLASS ) {
@@ -211,30 +217,32 @@ private void setLatinKeyboard(LatinKeyboard nextKeyboard) {
211217 // no extra features.
212218 mCurKeyboard = mSymbolsKeyboard ;
213219 break ;
214-
220+
215221 default :
216222 // For all unknown input types, default to the alphabetic
217223 // keyboard with no special features.
218224 mCurKeyboard = mQwertyKeyboard ;
219225 updateShiftKeyState (attribute );
220226 }
221-
227+
222228 // Update the label on the enter key, depending on what the application
223229 // says it will do.
224230 mCurKeyboard .setImeOptions (getResources (), attribute .imeOptions );
225231 }
226232
227- @ Override public void onFinishInput () {
233+ @ Override
234+ public void onFinishInput () {
228235 super .onFinishInput ();
229236 compositionText = new StringBuilder ();
230-
237+
231238 mCurKeyboard = mQwertyKeyboard ;
232239 if (mInputView != null ) {
233240 mInputView .closing ();
234241 }
235242 }
236-
237- @ Override public void onStartInputView (EditorInfo attribute , boolean restarting ) {
243+
244+ @ Override
245+ public void onStartInputView (EditorInfo attribute , boolean restarting ) {
238246 super .onStartInputView (attribute , restarting );
239247 // Apply the selected keyboard to the input view.
240248 setLatinKeyboard (mCurKeyboard );
@@ -284,7 +292,7 @@ public void onKey(int primaryCode, int[] keyCodes) {
284292
285293 public void onText (CharSequence text ) {
286294 }
287-
295+
288296 private void handleBackspace () {
289297 keyDownUp (KeyEvent .KEYCODE_DEL );
290298 updateShiftKeyState (getCurrentInputEditorInfo ());
@@ -312,7 +320,7 @@ private void handleShift() {
312320 if (mInputView == null ) {
313321 return ;
314322 }
315-
323+
316324 Keyboard currentKeyboard = mInputView .getKeyboard ();
317325 if (mQwertyKeyboard == currentKeyboard ) {
318326 // Alphabet keyboard
@@ -328,7 +336,7 @@ private void handleShift() {
328336 mSymbolsKeyboard .setShifted (false );
329337 }
330338 }
331-
339+
332340 private void handleCharacter (int primaryCode ) {
333341 if (isInputViewShown ()) {
334342 if (mInputView .isShifted ()) {
@@ -368,7 +376,7 @@ private List<String> getCandidates() {
368376 }
369377 return sortedWords ;
370378 } else {
371- return PinyinDict .getCandidates (prefix );
379+ return PinyinDict .getCandidates (prefix );
372380 }
373381 }
374382
@@ -377,6 +385,7 @@ public void reset() {
377385 candidates = new ArrayList <>();
378386 updateCandidateViewAndComposingText ();
379387 }
388+
380389 private void commitInput () {
381390 getCurrentInputConnection ().commitText (compositionText .toString (), compositionText .length ());
382391 reset ();
@@ -396,7 +405,20 @@ private IBinder getToken() {
396405
397406
398407 private void handleLanguageSwitch () {
399- inputMode = inputMode == InputMode .English ? InputMode .Pinyin : InputMode .English ;
408+ List <Keyboard .Key > keys = mQwertyKeyboard .getKeys ();
409+ Keyboard .Key switchKey = keys .stream ()
410+ .filter (key -> key .codes != null && key .codes .length > 0 && key .codes [0 ] == LatinKeyboard .KEYCODE_LANGUAGE_SWITCH ).findFirst ().get ();
411+
412+ if (inputMode == InputMode .English ) {
413+ inputMode = InputMode .Pinyin ;
414+ switchKey .label = "\uD83C \uDF10 中文" ;
415+ } else {
416+ inputMode = InputMode .English ;
417+ switchKey .label = "\uD83C \uDF10 En" ;
418+ }
419+
420+ // Redraw the keyboard with updated labels
421+ mInputView .invalidateAllKeys ();
400422 }
401423
402424 private void checkToggleCapsLock () {
@@ -408,10 +430,10 @@ private void checkToggleCapsLock() {
408430 mLastShiftTime = now ;
409431 }
410432 }
411-
433+
412434 public void swipeRight () {
413435 }
414-
436+
415437 public void swipeLeft () {
416438 }
417439
@@ -420,10 +442,10 @@ public void swipeDown() {
420442
421443 public void swipeUp () {
422444 }
423-
445+
424446 public void onPress (int primaryCode ) {
425447 }
426-
448+
427449 public void onRelease (int primaryCode ) {
428450 }
429451}
0 commit comments