Skip to content
This repository has been archived by the owner on Jul 22, 2024. It is now read-only.

Refine Pinyin symbol keyboard and add Emoji support #3056

Merged
merged 3 commits into from
Mar 31, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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<Words> mEmojiList = null;
private DBHelper mDB;
private HashMap<String, KeyMap> mKeymaps = new HashMap<>();
private HashMap<String, KeyMap> mExtraKeymaps = new HashMap<>();
Expand All @@ -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) {
Expand All @@ -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","");
Expand Down Expand Up @@ -123,6 +153,43 @@ public CandidatesResult getCandidates(String aComposingText) {
return result;
}

@Override
public CandidatesResult getEmojiCandidates(String aComposingText) {
if (mEmojiList == null) {
List<Words> 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<String> getDisplayCode(String aKey) {
ArrayList<String> result = new ArrayList<>();
String remain = "";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -449,12 +449,10 @@ private String getString(Cursor aCursor, int aIndex) {
return aCursor.getString(aIndex);
}


class KeyMap {
ArrayList<Words> displays = new ArrayList<>();
}


class DBWordHelper extends SQLiteAssetHelper {
private static final String DATABASE_NAME = "zhuyin_words.db";
private static final int DATABASE_VERSION = 1;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}

Expand Down Expand Up @@ -824,6 +825,12 @@ private void handleEmojiInput() {
}

private void handleDomain() {
if (!mIsLongPress) {
handleText(mCurrentKeyboard.getDomains()[0]);
}
}

private void handleDomainLongPress() {
ArrayList<KeyboardSelectorView.Item> items = new ArrayList<>();
for (String item: mCurrentKeyboard.getDomains()) {
items.add(new KeyboardSelectorView.Item(item, item));
Expand All @@ -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();

Expand Down Expand Up @@ -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) {
Expand Down
1 change: 1 addition & 0 deletions app/src/main/res/values/non_L10n.xml
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@
<string name="pinyin_spacebar_selection" translatable="false">选定</string>
<string name="pinyin_spacebar_space" translatable="false">空格</string>
<string name="pinyin_enter_completion" translatable="false">确认</string>
<string name="pinyin_keyboard_mode_change" translatable="false">拼音</string>

<string name="zhuyin_spacebar_selection" translatable="false">一聲</string>
<string name="zhuyin_spacebar_space" translatable="false">空格</string>
Expand Down
62 changes: 62 additions & 0 deletions app/src/main/res/xml/keyboard_symbols_pinyin.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
<?xml version="1.0" encoding="utf-8"?>
<Keyboard xmlns:android="http://schemas.android.com/apk/res/android"
android:horizontalGap="@dimen/keyboard_horizontal_gap"
android:verticalGap="@dimen/keyboard_vertical_gap"
android:keyWidth="@dimen/keyboard_key_width"
android:keyHeight="@dimen/keyboard_key_height">
<Row>
<Key android:codes="8364" android:keyLabel="€" android:keyEdgeFlags="left"/>
<Key android:codes="163" android:keyLabel="£"/>
<Key android:codes="165" android:keyLabel="¥"/>
<Key android:codes="36" android:keyLabel="$"/>
<Key android:codes="199" android:keyLabel="ç"/>
<Key android:codes="37" android:keyLabel="%"/>
<Key android:codes="38" android:keyLabel="&amp;"/>
<Key android:codes="40" android:keyLabel="("/>
<Key android:codes="41" android:keyLabel=")"/>
<Key android:codes="61" android:keyLabel="="/>
<Key android:codes="95" android:keyLabel="_"/>
<Key android:codes="-5" android:keyIcon="@drawable/ic_icon_keyboard_backspace" android:isRepeatable="true" android:keyWidth="@dimen/keyboard_key_backspace_width"/>
</Row>

<Row>
<Key android:codes="186" android:keyLabel="º" android:keyEdgeFlags="left" android:horizontalGap="@dimen/keyboard_left_margin"/>
<Key android:codes="94" android:keyLabel="^" />
<Key android:codes="0x2026" android:keyLabel="…" />
<Key android:codes="123" android:keyLabel="{"/>
<Key android:codes="125" android:keyLabel="}"/>
<Key android:codes="0x3010" android:keyLabel="【"/>
<Key android:codes="0x3011" android:keyLabel="】"/>
<Key android:codes="124" android:keyLabel="|"/>
<Key android:codes="59" android:keyLabel=";"/>
<Key android:codes="58" android:keyLabel=":"/>
<Key android:codes="-4" android:keyLabel="@string/keyboard_enter_label" android:keyWidth="@dimen/keyboard_key_enter_width" />
</Row>

<Row>
<Key android:codes="-1" android:keyIcon="@drawable/ic_icon_keyboard_shift_on" android:keyEdgeFlags="left"/>
<Key android:codes="0x300a" android:keyLabel="《"/>
<Key android:codes="0x300b" android:keyLabel="》"/>
<Key android:codes="96" android:keyLabel="`"/>
<Key android:codes="126" android:keyLabel="~"/>
<Key android:codes="39" android:keyLabel="'"/>
<Key android:codes="34" android:keyLabel="&quot;"/>
<Key android:codes="92" android:keyLabel="\\" />
<Key android:codes="45" android:keyLabel="-" />
<Key android:codes="43" android:keyLabel="+" />
<Key android:codes="47" android:keyLabel="/" />
<Key android:codes="-1" android:keyIcon="@drawable/ic_icon_keyboard_shift_on" />
</Row>

<Row>
<Key android:codes="-2" android:keyLabel="@string/keyboard_mode_change" android:keyEdgeFlags="left" />
<Key android:codes="-12" android:keyIcon="@drawable/ic_icon_keyboard_globe" />
<Key android:codes="32" android:keyLabel="" android:keyWidth="@dimen/keyboard_key_space_width" android:isRepeatable="true"/>
<Key android:codes="0x3001" android:keyLabel="、"/>
<Key android:codes="46" android:keyLabel="."/>
<Key android:codes="33" android:keyLabel="!" android:popupCharacters="!¡" android:popupKeyboard="@xml/keyboard_popup" />
<Key android:codes="63" android:keyLabel="\?" android:popupCharacters="\?¿" android:popupKeyboard="@xml/keyboard_popup" />
<Key android:codes="-14" android:keyLabel="\.com" />
<Key android:codes="-13" android:keyLabel="^.^"/>
</Row>
</Keyboard>