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
This commit is contained in:
Yuncheol Heo
2019-05-16 13:56:22 -07:00
parent 94376a0ffe
commit 9d4c7d79dc
2 changed files with 119 additions and 3 deletions

View File

@@ -0,0 +1,79 @@
<?xml version="1.0" encoding="utf-8"?>
<!--
Copyright (C) 2019 The Android Open Source Project
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->
<Keyboard xmlns:android="http://schemas.android.com/apk/res/android"
android:keyWidth="10%p"
android:horizontalGap="0px"
android:verticalGap="0px"
android:keyHeight="50dip"
>
<Row>
<Key android:codes="126" android:keyLabel="~" android:keyEdgeFlags="left"/>
<Key android:codes="177" android:keyLabel="±"/>
<Key android:codes="215" android:keyLabel="×"/>
<Key android:codes="247" android:keyLabel="÷"/>
<Key android:codes="8226" android:keyLabel="•"/>
<Key android:codes="176" android:keyLabel="°"/>
<Key android:codes="96" android:keyLabel="`"/>
<Key android:codes="180" android:keyLabel="´"/>
<Key android:codes="123" android:keyLabel="{"/>
<Key android:codes="125" android:keyLabel="}" android:keyEdgeFlags="right"/>
</Row>
<Row>
<Key android:codes="169" android:keyLabel="©" android:keyEdgeFlags="left"/>
<Key android:codes="163" android:keyLabel="£"/>
<Key android:codes="8364" android:keyLabel="€"/>
<Key android:codes="94" android:keyLabel="^"/>
<Key android:codes="174" android:keyLabel="®"/>
<Key android:codes="165" android:keyLabel="¥"/>
<Key android:codes="95" android:keyLabel="_"/>
<Key android:codes="43" android:keyLabel="+"/>
<Key android:codes="91" android:keyLabel="["/>
<Key android:codes="93" android:keyLabel="]" android:keyEdgeFlags="right"/>
</Row>
<Row>
<Key android:codes="-1" android:keyIcon="@drawable/sym_keyboard_shift"
android:keyWidth="15%p" android:isModifier="true"
android:isSticky="true" android:keyEdgeFlags="left"/>
<Key android:codes="161" android:keyLabel="¡" />
<Key android:codes="60" android:keyLabel="&lt;"/>
<Key android:codes="62" android:keyLabel="&gt;"/>
<Key android:codes="162" android:keyLabel="¢"/>
<Key android:codes="124" android:keyLabel="|"/>
<Key android:codes="92" android:keyLabel="\\" />
<Key android:codes="191" android:keyLabel="¿"/>
<Key android:codes="-5" android:keyIcon="@drawable/sym_keyboard_delete"
android:keyWidth="15%p" android:keyEdgeFlags="right"
android:isRepeatable="true"/>
</Row>
<Row android:rowEdgeFlags="bottom">
<Key android:codes="-3" android:keyIcon="@drawable/sym_keyboard_done"
android:keyWidth="15%p" android:keyEdgeFlags="left"/>
<Key android:codes="-2" android:keyLabel="ABC" android:keyWidth="10%p"/>
<Key android:codes="32" android:keyIcon="@drawable/sym_keyboard_space"
android:keyWidth="40%p" android:isRepeatable="true"/>
<Key android:codes="46,44" android:keyLabel=". ,"
android:keyWidth="15%p"/>
<Key android:codes="10" android:keyIcon="@drawable/sym_keyboard_return"
android:keyWidth="20%p" android:keyEdgeFlags="right"/>
</Row>
</Keyboard>

View File

@@ -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);
}
}