Skip to content

Commit

Permalink
Resize keyboard dimensions (#238)
Browse files Browse the repository at this point in the history
* support a switch tag around the root keyboard tag

* make the number row the same height as the other rows (still need to add numbers to support large devices)

* add numbers to support large devices

* add extra characters to the number row

* reduced some duplicate code and removed more keys on the number row

* removed special characters on the number row
  • Loading branch information
wittmane authored Jan 24, 2021
1 parent 3b2beee commit 79e36db
Show file tree
Hide file tree
Showing 52 changed files with 629 additions and 590 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ public final class KeyboardId {
public static final int ELEMENT_NUMBER = 9;

public final RichInputMethodSubtype mSubtype;
public final int mThemeId;
public final int mWidth;
public final int mHeight;
public final int mMode;
Expand All @@ -68,6 +69,7 @@ public final class KeyboardId {

public KeyboardId(final int elementId, final KeyboardLayoutSet.Params params) {
mSubtype = params.mSubtype;
mThemeId = params.mKeyboardThemeId;
mWidth = params.mKeyboardWidth;
mHeight = params.mKeyboardHeight;
mMode = params.mMode;
Expand Down Expand Up @@ -96,6 +98,7 @@ private static int computeHashCode(final KeyboardId id) {
id.navigateNext(),
id.navigatePrevious(),
id.mSubtype,
id.mThemeId
});
}

Expand All @@ -113,7 +116,8 @@ private boolean equals(final KeyboardId other) {
&& TextUtils.equals(other.mCustomActionLabel, mCustomActionLabel)
&& other.navigateNext() == navigateNext()
&& other.navigatePrevious() == navigatePrevious()
&& other.mSubtype.equals(mSubtype);
&& other.mSubtype.equals(mSubtype)
&& other.mThemeId == mThemeId;
}

private static boolean isAlphabetKeyboard(final int elementId) {
Expand Down Expand Up @@ -164,7 +168,7 @@ public int hashCode() {

@Override
public String toString() {
return String.format(Locale.ROOT, "[%s %s:%s %dx%d %s %s%s%s%s%s%s%s%s%s]",
return String.format(Locale.ROOT, "[%s %s:%s %dx%d %s %s%s%s%s%s%s %s]",
elementIdToName(mElementId),
mSubtype.getLocale(),
mSubtype.getExtraValueOf(KEYBOARD_LAYOUT_SET),
Expand All @@ -175,7 +179,8 @@ public String toString() {
(navigatePrevious() ? " navigatePrevious" : ""),
(passwordInput() ? " passwordInput" : ""),
(mLanguageSwitchKeyEnabled ? " languageSwitchKeyEnabled" : ""),
(isMultiLine() ? " isMultiLine" : "")
(isMultiLine() ? " isMultiLine" : ""),
KeyboardTheme.getKeyboardThemeName(mThemeId)
);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ public static final class Params {
EditorInfo mEditorInfo;
boolean mLanguageSwitchKeyEnabled;
RichInputMethodSubtype mSubtype;
int mKeyboardThemeId;
int mKeyboardWidth;
int mKeyboardHeight;
boolean mShowMoreKeys;
Expand Down Expand Up @@ -213,6 +214,11 @@ public Builder(final Context context, final EditorInfo ei) {
params.mEditorInfo = editorInfo;
}

public Builder setKeyboardTheme(final int themeId) {
mParams.mKeyboardThemeId = themeId;
return this;
}

public Builder setKeyboardGeometry(final int keyboardWidth, final int keyboardHeight) {
mParams.mKeyboardWidth = keyboardWidth;
mParams.mKeyboardHeight = keyboardHeight;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ public void loadKeyboard(final EditorInfo editorInfo, final SettingsValues setti
final Resources res = mThemeContext.getResources();
final int keyboardWidth = mLatinIME.getMaxWidth();
final int keyboardHeight = ResourceUtils.getKeyboardHeight(res, settingsValues);
builder.setKeyboardTheme(mKeyboardTheme.mThemeId);
builder.setKeyboardGeometry(keyboardWidth, keyboardHeight);
builder.setSubtype(mRichImm.getCurrentSubtype());
builder.setLanguageSwitchKeyEnabled(mLatinIME.shouldShowLanguageSwitchKey());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,7 @@ public class KeyboardBuilder<KP extends KeyboardParams> {
private float mCurrentY = 0;
private KeyboardRow mCurrentRow = null;
private Key mPreviousKeyInRow = null;
private boolean mKeyboardDefined = false;

public KeyboardBuilder(final Context context, final KP params) {
mContext = context;
Expand All @@ -160,7 +161,10 @@ public KeyboardBuilder<KP> load(final int xmlId, final KeyboardId id) {
mParams.mId = id;
final XmlResourceParser parser = mResources.getXml(xmlId);
try {
parseKeyboard(parser);
parseKeyboard(parser, false);
if (!mKeyboardDefined) {
throw new XmlParseUtils.ParseException("No " + TAG_KEYBOARD + " tag was found");
}
} catch (XmlPullParserException e) {
Log.w(BUILDER_TAG, "keyboard XML parse error", e);
throw new IllegalArgumentException(e.getMessage(), e);
Expand Down Expand Up @@ -197,20 +201,37 @@ private void startEndTag(final String format, final Object ... args) {
mIndent--;
}

private void parseKeyboard(final XmlPullParser parser)
private void parseKeyboard(final XmlPullParser parser, final boolean skip)
throws XmlPullParserException, IOException {
if (DEBUG) startTag("<%s> %s", TAG_KEYBOARD, mParams.mId);
while (parser.getEventType() != XmlPullParser.END_DOCUMENT) {
final int event = parser.next();
if (event == XmlPullParser.START_TAG) {
final String tag = parser.getName();
if (TAG_KEYBOARD.equals(tag)) {
parseKeyboardAttributes(parser);
startKeyboard();
parseKeyboardContent(parser, false);
if (DEBUG) startTag("<%s> %s%s", TAG_KEYBOARD, mParams.mId,
skip ? " skipped" : "");
if (!skip) {
if (mKeyboardDefined) {
throw new XmlParseUtils.ParseException("Only one " + TAG_KEYBOARD
+ " tag can be defined", parser);
}
mKeyboardDefined = true;
parseKeyboardAttributes(parser);
startKeyboard();
}
parseKeyboardContent(parser, skip);
} else if (TAG_SWITCH.equals(tag)) {
parseSwitchKeyboard(parser, skip);
} else {
throw new XmlParseUtils.IllegalStartTag(parser, tag, TAG_KEYBOARD);
}
} else if (event == XmlPullParser.END_TAG) {
final String tag = parser.getName();
if (DEBUG) endTag("</%s>", tag);
if (TAG_CASE.equals(tag) || TAG_DEFAULT.equals(tag)) {
return;
}
throw new XmlParseUtils.IllegalStartTag(parser, tag, TAG_KEYBOARD);
throw new XmlParseUtils.IllegalEndTag(parser, tag, TAG_ROW);
}
}
}
Expand All @@ -224,7 +245,11 @@ private void parseKeyboardAttributes(final XmlPullParser parser) {
final KeyboardParams params = mParams;
final int height = params.mId.mHeight;
final int width = params.mId.mWidth;
params.mOccupiedHeight = height;
// The bonus height isn't used to determine the other dimensions (gap/padding) to allow
// those to stay consistent between layouts with and without the bonus height added.
final int bonusHeight = mParams.mId.mShowNumberRow ? Math.round(mResources.getFraction(
R.fraction.config_key_bonus_height_5row, height, height)) : 0;
params.mOccupiedHeight = height + bonusHeight;
params.mOccupiedWidth = width;
params.mTopPadding = ResourceUtils.getDimensionOrFraction(keyboardAttr,
R.styleable.Keyboard_keyboardTopPadding, height, 0);
Expand Down Expand Up @@ -261,7 +286,6 @@ private void parseKeyboardAttributes(final XmlPullParser parser) {
params.mMaxMoreKeysKeyboardColumn = keyAttr.getInt(
R.styleable.Keyboard_Key_maxMoreKeysColumn, 5);

params.mThemeId = keyboardAttr.getInt(R.styleable.Keyboard_themeId, 0);
params.mIconsSet.loadIcons(keyboardAttr);
params.mTextsSet.setLocale(params.mId.getLocale(), mContext);
} finally {
Expand Down Expand Up @@ -484,28 +508,33 @@ private void parseMerge(final XmlPullParser parser, final KeyboardRow row, final
}
}

private void parseSwitchKeyboard(final XmlPullParser parser, final boolean skip)
throws XmlPullParserException, IOException {
parseSwitchInternal(parser, true, null, skip);
}

private void parseSwitchKeyboardContent(final XmlPullParser parser, final boolean skip)
throws XmlPullParserException, IOException {
parseSwitchInternal(parser, null, skip);
parseSwitchInternal(parser, false, null, skip);
}

private void parseSwitchRowContent(final XmlPullParser parser, final KeyboardRow row,
final boolean skip) throws XmlPullParserException, IOException {
parseSwitchInternal(parser, row, skip);
parseSwitchInternal(parser, false, row, skip);
}

private void parseSwitchInternal(final XmlPullParser parser, final KeyboardRow row,
final boolean skip) throws XmlPullParserException, IOException {
private void parseSwitchInternal(final XmlPullParser parser, final boolean parseKeyboard,
final KeyboardRow row, final boolean skip) throws XmlPullParserException, IOException {
if (DEBUG) startTag("<%s> %s", TAG_SWITCH, mParams.mId);
boolean selected = false;
while (parser.getEventType() != XmlPullParser.END_DOCUMENT) {
final int event = parser.next();
if (event == XmlPullParser.START_TAG) {
final String tag = parser.getName();
if (TAG_CASE.equals(tag)) {
selected |= parseCase(parser, row, selected || skip);
selected |= parseCase(parser, parseKeyboard, row, selected || skip);
} else if (TAG_DEFAULT.equals(tag)) {
selected |= parseDefault(parser, row, selected || skip);
selected |= parseDefault(parser, parseKeyboard, row, selected || skip);
} else {
throw new XmlParseUtils.IllegalStartTag(parser, tag, TAG_SWITCH);
}
Expand All @@ -520,10 +549,13 @@ private void parseSwitchInternal(final XmlPullParser parser, final KeyboardRow r
}
}

private boolean parseCase(final XmlPullParser parser, final KeyboardRow row, final boolean skip)
throws XmlPullParserException, IOException {
private boolean parseCase(final XmlPullParser parser, final boolean parseKeyboard,
final KeyboardRow row, final boolean skip) throws XmlPullParserException, IOException {
final boolean selected = parseCaseCondition(parser);
if (row == null) {
if (parseKeyboard) {
// Processing Keyboard root.
parseKeyboard(parser, !selected || skip);
} else if (row == null) {
// Processing Rows.
parseKeyboardContent(parser, !selected || skip);
} else {
Expand All @@ -540,6 +572,7 @@ private boolean parseCaseCondition(final XmlPullParser parser) {
}
final AttributeSet attr = Xml.asAttributeSet(parser);
final TypedArray caseAttr = mResources.obtainAttributes(attr, R.styleable.Keyboard_Case);
if (DEBUG) startTag("<%s>", TAG_CASE);
try {
final boolean keyboardLayoutSetMatched = matchString(caseAttr,
R.styleable.Keyboard_Case_keyboardLayoutSet,
Expand All @@ -548,8 +581,8 @@ private boolean parseCaseCondition(final XmlPullParser parser) {
R.styleable.Keyboard_Case_keyboardLayoutSetElement, id.mElementId,
KeyboardId.elementIdToName(id.mElementId));
final boolean keyboardThemeMatched = matchTypedValue(caseAttr,
R.styleable.Keyboard_Case_keyboardTheme, mParams.mThemeId,
KeyboardTheme.getKeyboardThemeName(mParams.mThemeId));
R.styleable.Keyboard_Case_keyboardTheme, id.mThemeId,
KeyboardTheme.getKeyboardThemeName(id.mThemeId));
final boolean modeMatched = matchTypedValue(caseAttr,
R.styleable.Keyboard_Case_mode, id.mMode, KeyboardId.modeName(id.mMode));
final boolean navigateNextMatched = matchBoolean(caseAttr,
Expand Down Expand Up @@ -647,10 +680,12 @@ private static boolean isIconDefined(final TypedArray a, final int index,
return iconsSet.getIconDrawable(iconId) != null;
}

private boolean parseDefault(final XmlPullParser parser, final KeyboardRow row,
final boolean skip) throws XmlPullParserException, IOException {
private boolean parseDefault(final XmlPullParser parser, final boolean parseKeyboard,
final KeyboardRow row, final boolean skip) throws XmlPullParserException, IOException {
if (DEBUG) startTag("<%s>", TAG_DEFAULT);
if (row == null) {
if (parseKeyboard) {
parseKeyboard(parser, skip);
} else if (row == null) {
parseKeyboardContent(parser, skip);
} else {
parseRowContent(parser, row, skip);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@

public class KeyboardParams {
public KeyboardId mId;
public int mThemeId;

/** Total height and width of the keyboard, including the paddings and keys */
public int mOccupiedHeight;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -183,8 +183,6 @@ private static boolean fulfillsCondition(final HashMap<String,String> keyValuePa
public static int getKeyboardHeight(final Resources res, final SettingsValues settingsValues) {
final int defaultKeyboardHeight = getDefaultKeyboardHeight(res);
float scale = settingsValues.mKeyboardHeightScale;
if (settingsValues.mShowNumberRow)
scale += 0.1;
return (int)(defaultKeyboardHeight * scale);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@ private XmlParseUtils() {

@SuppressWarnings("serial")
public static class ParseException extends XmlPullParserException {
public ParseException(final String msg) {
super(msg);
}
public ParseException(final String msg, final XmlPullParser parser) {
super(msg + " at " + parser.getPositionDescription());
}
Expand Down
10 changes: 10 additions & 0 deletions app/src/main/res/values-land/config.xml
Original file line number Diff line number Diff line change
Expand Up @@ -54,4 +54,14 @@
<fraction name="config_language_on_spacebar_text_ratio">40.000%</fraction>

<!-- For 5-row keyboard -->
<!-- config_key_vertical_gap_5row is calculated to match config_key_horizontal_gap from the
portrait layout (assuming the height is based on config_min_keyboard_height) to have a
general guideline for a minimum gap between keys. For a 5 row keyboard, we shouldn't waste
unnecessary space on the gap. -->
<fraction name="config_key_vertical_gap_5row">3.864%p</fraction>
<!-- config_key_bonus_height_5row ideally would be calculated to be the extra height necessary
to keep the 5 and 4 row keyboards' keys the same height, but there isn't enough space
solely from reducing the vertical gap, so it's just capped to prevent taking up too much
space. -->
<fraction name="config_key_bonus_height_5row">10%p</fraction>
</resources>
6 changes: 6 additions & 0 deletions app/src/main/res/values-sw600dp-land/config.xml
Original file line number Diff line number Diff line change
Expand Up @@ -45,4 +45,10 @@
<dimen name="config_key_shifted_letter_hint_padding">4dp</dimen>

<!-- For 5-row keyboard -->
<!-- config_key_vertical_gap_5row should match config_key_vertical_gap -->
<fraction name="config_key_vertical_gap_5row">4.5%p</fraction>
<!-- config_key_bonus_height_5row ideally would be calculated to be the extra height necessary
to keep the 5 and 4 row keyboards' keys the same height, but it needs to be capped to
prevent taking up too much space. -->
<fraction name="config_key_bonus_height_5row">15%p</fraction>
</resources>
5 changes: 5 additions & 0 deletions app/src/main/res/values-sw600dp/config.xml
Original file line number Diff line number Diff line change
Expand Up @@ -54,4 +54,9 @@
<dimen name="config_key_shifted_letter_hint_padding">3dp</dimen>

<!-- For 5-row keyboard -->
<!-- config_key_vertical_gap_5row should match config_key_vertical_gap -->
<fraction name="config_key_vertical_gap_5row">4.5%p</fraction>
<!-- config_key_bonus_height_5row is calculated to be the extra height necessary to keep the 5
and 4 row keyboards' keys the same height. -->
<fraction name="config_key_bonus_height_5row">25.096%p</fraction>
</resources>
6 changes: 6 additions & 0 deletions app/src/main/res/values-sw768dp-land/config.xml
Original file line number Diff line number Diff line change
Expand Up @@ -45,4 +45,10 @@
<fraction name="config_language_on_spacebar_text_ratio">24.00%</fraction>

<!-- For 5-row keyboard -->
<!-- config_key_vertical_gap_5row should match config_key_vertical_gap -->
<fraction name="config_key_vertical_gap_5row">3.690%p</fraction>
<!-- config_key_bonus_height_5row ideally would be calculated to be the extra height necessary
to keep the 5 and 4 row keyboards' keys the same height, but it needs to be capped to
prevent taking up too much space. -->
<fraction name="config_key_bonus_height_5row">15%p</fraction>
</resources>
5 changes: 5 additions & 0 deletions app/src/main/res/values-sw768dp/config.xml
Original file line number Diff line number Diff line change
Expand Up @@ -53,4 +53,9 @@
<dimen name="config_key_shifted_letter_hint_padding">3dp</dimen>

<!-- For 5-row keyboard -->
<!-- config_key_vertical_gap_5row should match config_key_vertical_gap -->
<fraction name="config_key_vertical_gap_5row">3.312%p</fraction>
<!-- config_key_bonus_height_5row is calculated to be the extra height necessary to keep the 5
and 4 row keyboards' keys the same height. -->
<fraction name="config_key_bonus_height_5row">24.466%p</fraction>
</resources>
2 changes: 0 additions & 2 deletions app/src/main/res/values-v28/themes-lxx-system-border.xml
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,6 @@
name="Keyboard.LXX_SystemBorder"
parent="Keyboard"
>
<!-- This should be aligned with KeyboardTheme.THEME_ID_* -->
<item name="themeId">LXXSystemBorder</item>
</style>
<style
name="KeyboardView.LXX_SystemBorder"
Expand Down
2 changes: 0 additions & 2 deletions app/src/main/res/values-v28/themes-lxx-system.xml
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,6 @@
name="Keyboard.LXX_System"
parent="Keyboard"
>
<!-- This should be aligned with KeyboardTheme.THEME_ID_* -->
<item name="themeId">LXXSystem</item>
</style>
<style
name="KeyboardView.LXX_System"
Expand Down
12 changes: 1 addition & 11 deletions app/src/main/res/values/attrs.xml
Original file line number Diff line number Diff line change
Expand Up @@ -116,16 +116,6 @@
</declare-styleable>

<declare-styleable name="Keyboard">
<attr name="themeId" format="enum">
<!-- This should be aligned with
{@link rkr.simplekeyboard.inputmethod.keyboard.KeyboardTheme#THEME_ID_ICS} etc. -->
<enum name="LXXLightBorder" value="1" />
<enum name="LXXDarkBorder" value="2" />
<enum name="LXXLight" value="3" />
<enum name="LXXDark" value="4" />
<enum name="LXXSystem" value="5" />
<enum name="LXXSystemBorder" value="6" />
</attr>
<!-- Keyboard top, bottom, left, right edges paddings, in pixels or in the propotion of
keyboard height. -->
<attr name="keyboardTopPadding" format="dimension|fraction" />
Expand Down Expand Up @@ -328,7 +318,7 @@
<enum name="phoneSymbols" value="8" />
<enum name="number" value="9" />
</attr>
<!-- This should be aligned with Keyboard.themeId and
<!-- This should be aligned with
{@link rkr.simplekeyboard.inputmethod.keyboard.KeyboardTheme#THEME_ID_ICS} etc. -->
<attr name="keyboardTheme" format="enum|string">
<enum name="LXXLightBorder" value="1" />
Expand Down
Loading

0 comments on commit 79e36db

Please sign in to comment.