From 9d4c7d79dc8b2dec59b49452d60bc4ab7877d6e5 Mon Sep 17 00:00:00 2001 From: Yuncheol Heo Date: Thu, 16 May 2019 13:56:22 -0700 Subject: [PATCH] Handle a shift key. - Add a symbol shift keylayout. - Apply a shifted state (upper case) on the next key input. Bug: 132636760 Test: test manually. Change-Id: I7447b9df47a953dad7216977e0b75ab801d863e4 --- .../res/xml/symbols_shift.xml | 79 +++++++++++++++++++ .../SoftInputWindow.java | 43 +++++++++- 2 files changed, 119 insertions(+), 3 deletions(-) create mode 100755 samples/MultiClientInputMethod/res/xml/symbols_shift.xml diff --git a/samples/MultiClientInputMethod/res/xml/symbols_shift.xml b/samples/MultiClientInputMethod/res/xml/symbols_shift.xml new file mode 100755 index 000000000..fe771b408 --- /dev/null +++ b/samples/MultiClientInputMethod/res/xml/symbols_shift.xml @@ -0,0 +1,79 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/samples/MultiClientInputMethod/src/com/example/android/multiclientinputmethod/SoftInputWindow.java b/samples/MultiClientInputMethod/src/com/example/android/multiclientinputmethod/SoftInputWindow.java index 0cc872a6c..afc66a413 100644 --- a/samples/MultiClientInputMethod/src/com/example/android/multiclientinputmethod/SoftInputWindow.java +++ b/samples/MultiClientInputMethod/src/com/example/android/multiclientinputmethod/SoftInputWindow.java @@ -40,6 +40,7 @@ final class SoftInputWindow extends Dialog { private final Keyboard mQwertygKeyboard; private final Keyboard mSymbolKeyboard; + private final Keyboard mSymbolShiftKeyboard; private int mClientId = MultiClientInputMethodServiceDelegate.INVALID_CLIENT_ID; private int mTargetWindowHandle = MultiClientInputMethodServiceDelegate.INVALID_WINDOW_HANDLE; @@ -74,6 +75,7 @@ final class SoftInputWindow extends Dialog { mKeyboardView = (KeyboardView) getLayoutInflater().inflate(R.layout.input, null); mQwertygKeyboard = new Keyboard(context, R.xml.qwerty); mSymbolKeyboard = new Keyboard(context, R.xml.symbols); + mSymbolShiftKeyboard = new Keyboard(context, R.xml.symbols_shift); mKeyboardView.setKeyboard(mQwertygKeyboard); mKeyboardView.setOnKeyboardActionListener(sNoopListener); layout.addView(mKeyboardView); @@ -93,10 +95,15 @@ final class SoftInputWindow extends Dialog { return mTargetWindowHandle; } - boolean isQwerty() { + boolean isQwertyKeyboard() { return mKeyboardView.getKeyboard() == mQwertygKeyboard; } + boolean isSymbolKeyboard() { + Keyboard keyboard = mKeyboardView.getKeyboard(); + return keyboard == mSymbolKeyboard || keyboard == mSymbolShiftKeyboard; + } + void onFinishClient() { mKeyboardView.setOnKeyboardActionListener(sNoopListener); mClientId = MultiClientInputMethodServiceDelegate.INVALID_CLIENT_ID; @@ -127,6 +134,8 @@ final class SoftInputWindow extends Dialog { Log.v(TAG, "onKey clientId=" + clientId + " primaryCode=" + primaryCode + " keyCodes=" + Arrays.toString(keyCodes)); } + boolean isShifted = isShifted(); // Store the current state before resetting it. + resetShift(); switch (primaryCode) { case Keyboard.KEYCODE_CANCEL: hide(); @@ -140,8 +149,11 @@ final class SoftInputWindow extends Dialog { case Keyboard.KEYCODE_MODE_CHANGE: handleSwitchKeyboard(); break; + case Keyboard.KEYCODE_SHIFT: + handleShift(isShifted); + break; default: - inputConnection.commitText(String.valueOf((char) primaryCode), 1); + handleCharacter(inputConnection, primaryCode, isShifted); break; } } @@ -160,11 +172,36 @@ final class SoftInputWindow extends Dialog { } void handleSwitchKeyboard() { - if (isQwerty()) { + if (isQwertyKeyboard()) { mKeyboardView.setKeyboard(mSymbolKeyboard); } else { mKeyboardView.setKeyboard(mQwertygKeyboard); } } + + boolean isShifted() { + return mKeyboardView.isShifted(); + } + + void resetShift() { + if (isSymbolKeyboard() && isShifted()) { + mKeyboardView.setKeyboard(mSymbolKeyboard); + } + mKeyboardView.setShifted(false); + } + + void handleShift(boolean isShifted) { + if (isSymbolKeyboard()) { + mKeyboardView.setKeyboard(isShifted ? mSymbolKeyboard : mSymbolShiftKeyboard); + } + mKeyboardView.setShifted(!isShifted); + } + + void handleCharacter(InputConnection inputConnection, int primaryCode, boolean isShifted) { + if (isQwertyKeyboard() && isShifted) { + primaryCode = Character.toUpperCase(primaryCode); + } + inputConnection.commitText(String.valueOf((char) primaryCode), 1); + } }