Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 58592051 authored by Tadashi G. Takaoka's avatar Tadashi G. Takaoka
Browse files

Implement logical or to Keyboard switch-case matching

Change-Id: Iab615459c21fa5799a7b30b8649d75347e46c804
parent c0fdabcd
Loading
Loading
Loading
Loading
+16 −15
Original line number Diff line number Diff line
@@ -70,7 +70,7 @@
        <attr name="shadowRadius" format="float" />
        <attr name="backgroundDimAmount" format="float" />

        <attr name="keyLetterStyle">
        <attr name="keyLetterStyle" format="enum">
            <!-- This should be aligned with Typeface.NORMAL etc. -->
            <enum name="normal" value="0" />
            <enum name="bold" value="1" />
@@ -78,7 +78,7 @@
            <enum name="boldItalic" value="3" />
        </attr>

        <attr name="colorScheme">
        <attr name="colorScheme" format="enum">
            <!-- This should be aligned with KeyboardView.COLOR_SCHEME_* -->
            <enum name="white" value="0" />
            <enum name="black" value="1" />
@@ -125,7 +125,7 @@
        <!-- Maximum column of popup keyboard -->
        <attr name="maxPopupKeyboardColumn" format="integer" />
        <!-- Key edge flags. -->
        <attr name="keyEdgeFlags">
        <attr name="keyEdgeFlags" format="integer">
            <!-- Key is anchored to the left of the keyboard. -->
            <flag name="left" value="1" />
            <!-- Key is anchored to the right of the keyboard. -->
@@ -144,7 +144,7 @@
        <!-- The label to display on the key. -->
        <attr name="keyLabel" format="string" />
        <!-- The key label option -->
        <attr name="keyLabelOption">
        <attr name="keyLabelOption" format="integer">
            <!-- This should be aligned with KeyboardView.KEY_LABEL_OPTION_* -->
            <flag name="alignLeft" value="1" />
            <flag name="alignRight" value="2" />
@@ -176,7 +176,7 @@

    <declare-styleable name="Keyboard_Row">
        <!-- Row edge flags. -->
        <attr name="rowEdgeFlags">
        <attr name="rowEdgeFlags" format="integer">
            <!-- Row is anchored to the top of the keyboard. -->
            <flag name="top" value="4" />
            <!-- Row is anchored to the bottom of the keyboard. -->
@@ -190,7 +190,7 @@

    <declare-styleable name="Keyboard_Case">
        <!-- This should be aligned with KeyboardId.MODE_* -->
        <attr name="mode">
        <attr name="mode" format="enum|string">
            <enum name="text" value="0" />
            <enum name="url" value="1" />
            <enum name="email" value="2" />
@@ -203,17 +203,18 @@
        <attr name="hasSettingsKey" format="string" />
        <attr name="voiceKeyEnabled" format="string" />
        <attr name="hasVoiceKey" format="string" />
        <attr name="imeAction">
        <attr name="imeAction" format="enum">
            <!-- This should be aligned with EditorInfo.IME_ACTION_* -->
            <flag name="actionUnspecified" value="0" />
            <flag name="actionNone" value="1" />
            <flag name="actionGo" value="2" />
            <flag name="actionSearch" value="3" />
            <flag name="actionSend" value="4" />
            <flag name="actionNext" value="5" />
            <flag name="actionDone" value="6" />
            <flag name="actionPrevious" value="7" />
            <enum name="actionUnspecified" value="0" />
            <enum name="actionNone" value="1" />
            <enum name="actionGo" value="2" />
            <enum name="actionSearch" value="3" />
            <enum name="actionSend" value="4" />
            <enum name="actionNext" value="5" />
            <enum name="actionDone" value="6" />
            <enum name="actionPrevious" value="7" />
        </attr>
        <attr name="localeCode" format="string" />
        <attr name="languageCode" format="string" />
        <attr name="countryCode" format="string" />
    </declare-styleable>
+1 −8
Original line number Diff line number Diff line
@@ -40,15 +40,8 @@
                latin:languageCode="ru"
            >
                <switch>
                    <!-- TODO: implement logical OR for <case> attribute -->
                    <case
                        latin:mode="email"
                    >
                        <Key
                            latin:keyStyle="comKeyStyle" />
                    </case>
                    <case
                        latin:mode="url"
                        latin:mode="email|url"
                    >
                        <Key
                            latin:keyStyle="comKeyStyle" />
+31 −201
Original line number Diff line number Diff line
@@ -27,229 +27,59 @@
        >
            <key-style
                latin:styleName="currencyKeyStyle"
                latin:keyLabel="$"
                latin:popupCharacters="@string/alternates_for_currency_dollar" />
                latin:keyLabel="$" />
        </case>
        <!-- Countries using Euro currency, 23 countries as for January 2011. -->
        <!-- 1. Andorra (ca_AD, ca_ES) -->
        <case
            latin:languageCode="ca"
            latin:countryCode=""
        >
            <key-style
                latin:styleName="currencyKeyStyle"
                latin:keyLabel="€"
                latin:popupCharacters="@string/alternates_for_currency_euro" />
        </case>
        <!-- 2. Austria (de_AT) -->
<!--        <case-->
<!--            latin:countryCode="AT"-->
<!--        >-->
<!--            <key-style-->
<!--                latin:styleName="currencyKeyStyle"-->
<!--                latin:keyLabel="€"-->
<!--                latin:popupCharacters="@string/alternates_for_currency_euro" />-->
<!--        </case>-->
        <!-- 3. Belgium (nl_BE, fr_BE, de_BE) -->
<!--        <case-->
<!--            latin:countryCode="BE"-->
<!--        >-->
<!--            <key-style-->
<!--                latin:styleName="currencyKeyStyle"-->
<!--                latin:keyLabel="€"-->
<!--                latin:popupCharacters="@string/alternates_for_currency_euro" />-->
<!--        </case>-->
        <!-- 4. Cyprus (el_CY, tr_CY) -->
        <case
            latin:countryCode="CY"
        >
            <key-style
                latin:styleName="currencyKeyStyle"
                latin:keyLabel="€"
                latin:popupCharacters="@string/alternates_for_currency_euro" />
        </case>
        <!-- 5. Estonia (et_EE) -->
<!--        <case-->
<!--            latin:languageCode="et"-->
<!--            latin:countryCode=""-->
<!--        >-->
<!--            <key-style-->
<!--                latin:styleName="currencyKeyStyle"-->
<!--                latin:keyLabel="€"-->
<!--                latin:popupCharacters="@string/alternates_for_currency_euro" />-->
<!--        </case>-->
        <!-- 6. Finland (fi_FI, sv_FI) -->
        <case
            latin:languageCode="fi"
            latin:countryCode=""
        >
            <key-style
                latin:styleName="currencyKeyStyle"
                latin:keyLabel="€"
                latin:popupCharacters="@string/alternates_for_currency_euro" />
        </case>
        <!-- 7. France (fr_FR) -->
        <case
            latin:languageCode="fr"
            latin:countryCode=""
        >
            <key-style
                latin:styleName="currencyKeyStyle"
                latin:keyLabel="€"
                latin:popupCharacters="@string/alternates_for_currency_euro" />
        </case>
        <!-- 8. Germany (de_DE) -->
        <case
            latin:languageCode="de"
            latin:countryCode=""
        >
            <key-style
                latin:styleName="currencyKeyStyle"
                latin:keyLabel="€"
                latin:popupCharacters="@string/alternates_for_currency_euro" />
        </case>
        <!-- 9. Greece (el_GR) -->
        <case
            latin:languageCode="el"
            latin:countryCode=""
        >
            <key-style
                latin:styleName="currencyKeyStyle"
                latin:keyLabel="€"
                latin:popupCharacters="@string/alternates_for_currency_euro" />
        </case>
        <!-- 10. Ireland (ga_IE, en_IE) -->
        <case
            latin:countryCode="IE"
        >
            <key-style
                latin:styleName="currencyKeyStyle"
                latin:keyLabel="€"
                latin:popupCharacters="@string/alternates_for_currency_euro" />
        </case>
        <!-- 11. Italy (it_IT) -->
        <case
            latin:languageCode="it"
            latin:countryCode=""
        >
            <key-style
                latin:styleName="currencyKeyStyle"
                latin:keyLabel="€"
                latin:popupCharacters="@string/alternates_for_currency_euro" />
        </case>
        <!-- 12. Kosovo -->
<!--        <case-->
<!--            latin:countryCode="XK"-->
<!--        >-->
<!--            <key-style-->
<!--                latin:styleName="currencyKeyStyle"-->
<!--                latin:keyLabel="€"-->
<!--                latin:popupCharacters="@string/alternates_for_currency_euro" />-->
<!--        </case>-->
        <!-- 13. Luxembourg (lb_LU, fr_LU, de_LU) -->
        <case
            latin:countryCode="LU"
        >
            <key-style
                latin:styleName="currencyKeyStyle"
                latin:keyLabel="€"
                latin:popupCharacters="@string/alternates_for_currency_euro" />
        </case>
        <!-- 14. Malta (mt_MT, en_MT) -->
        <case
            latin:countryCode="MT"
        >
            <key-style
                latin:styleName="currencyKeyStyle"
                latin:keyLabel="€"
                latin:popupCharacters="@string/alternates_for_currency_euro" />
        </case>
        <!-- 15. Monaco (fr_MO) -->
<!--        <case-->
<!--            latin:countryCode="MO"-->
<!--        >-->
<!--            <key-style-->
<!--                latin:styleName="currencyKeyStyle"-->
<!--                latin:keyLabel="€"-->
<!--                latin:popupCharacters="@string/alternates_for_currency_euro" />-->
<!--        </case>-->
        <!-- 16. Montenegro (sla_ME) -->
        <case
            latin:countryCode="ME"
        >
            <key-style
                latin:styleName="currencyKeyStyle"
                latin:keyLabel="€"
                latin:popupCharacters="@string/alternates_for_currency_euro" />
        </case>
        <!-- 17. Netherlands (nl_NL) -->
        <case
            latin:languageCode="nl"
            latin:countryCode=""
        >
            <key-style
                latin:styleName="currencyKeyStyle"
                latin:keyLabel="€"
                latin:popupCharacters="@string/alternates_for_currency_euro" />
        </case>
        <!-- 18. Portugal (pt_PT) -->
        <case
            latin:languageCode="pt"
            latin:countryCode=""
        >
            <key-style
                latin:styleName="currencyKeyStyle"
                latin:keyLabel="€"
                latin:popupCharacters="@string/alternates_for_currency_euro" />
        </case>
        <!-- 19. San Marino (it_SM) -->
<!--        <case-->
<!--            latin:countryCode="SM"-->
<!--        >-->
<!--            <key-style-->
<!--                latin:styleName="currencyKeyStyle"-->
<!--                latin:keyLabel="€"-->
<!--                latin:popupCharacters="@string/alternates_for_currency_euro" />-->
<!--        </case>-->
        <!-- 20. Slovakia (sk_SK) -->
              1. Andorra (ca_AD, ca_ES)
              2. Austria (de_AT)
              3. Belgium (nl_BE, fr_BE, de_BE)
              4. Cyprus (el_CY, tr_CY)
              5. Estonia (et_EE)
              6. Finland (fi_FI, sv_FI)
              7. France (fr_FR)
              8. Germany (de_DE)
              9. Greece (el_GR)
             10. Ireland (ga_IE, en_IE)
             11. Italy (it_IT)
             12. Kosovo (??_XK) ??
             13. Luxembourg (lb_LU, fr_LU, de_LU)
             14. Malta (mt_MT, en_MT)
             15. Monaco (fr_MO)
             16. Montenegro (sla_ME)
             17. Netherlands (nl_NL)
             18. Portugal (pt_PT)
             19. San Marino (it_SM)
             20. Slovakia (sk_SK)
             21. Slovenia (sl_SI)
             22. Spain (es_ES, ca_ES)
             23. Vatican City (it_VA)
        -->
        <!-- Note: Some locales may not have country code, and it it supposed to indicate the
             country where the language originally/mainly spoken. -->
        <case
            latin:languageCode="sk"
            latin:countryCode=""
            latin:localeCode="de|es|el|fi|fr|it|nl|sk|sl|pt_PT"
        >
            <key-style
                latin:styleName="currencyKeyStyle"
                latin:keyLabel="€"
                latin:popupCharacters="@string/alternates_for_currency_euro" />
        </case>
        <!-- 21. Slovenia (sl_SI) -->
        <case
            latin:languageCode="sl"
            latin:countryCode=""
            latin:languageCode="ca|et|lb|mt|sla"
        >
            <key-style
                latin:styleName="currencyKeyStyle"
                latin:keyLabel="€"
                latin:popupCharacters="@string/alternates_for_currency_euro" />
        </case>
        <!-- 22. Spain (es_ES, ca_ES) -->
        <case
            latin:languageCode="es"
            latin:countryCode=""
            latin:countryCode="AD|AT|BE|CY|EE|FI|FR|DE|GR|IE|IT|XK|LU|MT|MO|ME|NL|PT|SM|SK|SI|ES|VA"
        >
            <key-style
                latin:styleName="currencyKeyStyle"
                latin:keyLabel="€"
                latin:popupCharacters="@string/alternates_for_currency_euro" />
        </case>
        <!-- 23. Vatican City (it_VA) -->
<!--        <case-->
<!--            latin:countryCode="VA"-->
<!--        >-->
<!--            <key-style-->
<!--                latin:styleName="currencyKeyStyle"-->
<!--                latin:keyLabel="€"-->
<!--                latin:popupCharacters="@string/alternates_for_currency_euro" />-->
<!--        </case>-->
        <!-- United Kingdom -->
        <case
            latin:countryCode="GB"
+53 −13
Original line number Diff line number Diff line
@@ -467,8 +467,8 @@ public class KeyboardParser {
        final TypedArray viewAttr = mResources.obtainAttributes(Xml.asAttributeSet(parser),
                R.styleable.KeyboardView);
        try {
            final boolean modeMatched = matchInteger(a,
                    R.styleable.Keyboard_Case_mode, id.mMode);
            final boolean modeMatched = matchTypedValue(a,
                    R.styleable.Keyboard_Case_mode, id.mMode, KeyboardId.modeName(id.mMode));
            final boolean webInputMatched = matchBoolean(a,
                    R.styleable.Keyboard_Case_webInput, id.mWebInput);
            final boolean passwordInputMatched = matchBoolean(a,
@@ -487,21 +487,22 @@ public class KeyboardParser {
            // this attribute with id.mImeOptions as integer value is enough for our purpose.
            final boolean imeActionMatched = matchInteger(a,
                    R.styleable.Keyboard_Case_imeAction, id.mImeAction);
            final boolean localeCodeMatched = matchString(a,
                    R.styleable.Keyboard_Case_localeCode, id.mLocale.toString());
            final boolean languageCodeMatched = matchString(a,
                    R.styleable.Keyboard_Case_languageCode, id.mLocale.getLanguage());
            final boolean countryCodeMatched = matchString(a,
                    R.styleable.Keyboard_Case_countryCode, id.mLocale.getCountry());
            final boolean selected = modeMatched && webInputMatched && passwordInputMatched
                    && settingsKeyMatched && voiceEnabledMatched && voiceKeyMatched
                    && colorSchemeMatched && imeActionMatched && languageCodeMatched
                    && countryCodeMatched;
                    && colorSchemeMatched && imeActionMatched && localeCodeMatched
                    && languageCodeMatched && countryCodeMatched;

            if (DEBUG) Log.d(TAG, String.format("<%s%s%s%s%s%s%s%s%s%s%s> %s", TAG_CASE,
                    textAttr(KeyboardId.modeName(
                            a.getInt(R.styleable.Keyboard_Case_mode, -1)), "mode"),
            if (DEBUG) Log.d(TAG, String.format("<%s%s%s%s%s%s%s%s%s%s%s%s> %s", TAG_CASE,
                    textAttr(a.getString(R.styleable.Keyboard_Case_mode), "mode"),
                    textAttr(KeyboardId.colorSchemeName(
                            viewAttr.getInt(
                                    R.styleable.KeyboardView_colorScheme, -1)), "colorSchemeName"),
                                    R.styleable.KeyboardView_colorScheme, -1)), "colorScheme"),
                    booleanAttr(a, R.styleable.Keyboard_Case_webInput, "webInput"),
                    booleanAttr(a, R.styleable.Keyboard_Case_passwordInput, "passwordInput"),
                    booleanAttr(a, R.styleable.Keyboard_Case_hasSettingsKey, "hasSettingsKey"),
@@ -509,6 +510,7 @@ public class KeyboardParser {
                    booleanAttr(a, R.styleable.Keyboard_Case_hasVoiceKey, "hasVoiceKey"),
                    textAttr(EditorInfoCompatUtils.imeOptionsName(
                            a.getInt(R.styleable.Keyboard_Case_imeAction, -1)), "imeAction"),
                    textAttr(a.getString(R.styleable.Keyboard_Case_localeCode), "localeCode"),
                    textAttr(a.getString(R.styleable.Keyboard_Case_languageCode), "languageCode"),
                    textAttr(a.getString(R.styleable.Keyboard_Case_countryCode), "countryCode"),
                    Boolean.toString(selected)));
@@ -535,7 +537,30 @@ public class KeyboardParser {
    private static boolean matchString(TypedArray a, int index, String value) {
        // If <case> does not have "index" attribute, that means this <case> is wild-card for the
        // attribute.
        return !a.hasValue(index) || a.getString(index).equals(value);
        return !a.hasValue(index) || stringArrayContains(a.getString(index).split("\\|"), value);
    }

    private static boolean matchTypedValue(TypedArray a, int index, int intValue, String strValue) {
        // If <case> does not have "index" attribute, that means this <case> is wild-card for the
        // attribute.
        final TypedValue v = a.peekValue(index);
        if (v == null)
            return true;

        if (isIntegerValue(v)) {
            return intValue == a.getInt(index, 0);
        } else if (isStringValue(v)) {
            return stringArrayContains(a.getString(index).split("\\|"), strValue);
        }
        return false;
    }

    private static boolean stringArrayContains(String[] array, String value) {
        for (final String elem : array) {
            if (elem.equals(value))
                return true;
        }
        return false;
    }

    private boolean parseDefault(XmlResourceParser parser, Row row, List<Key> keys)
@@ -610,19 +635,34 @@ public class KeyboardParser {
        final TypedValue value = a.peekValue(index);
        if (value == null)
            return defValue;
        if (value.type == TypedValue.TYPE_FRACTION) {
        if (isFractionValue(value)) {
            // Round it to avoid values like 47.9999 from getting truncated
            return Math.round(a.getFraction(index, base, base, defValue));
        } else if (value.type == TypedValue.TYPE_DIMENSION) {
        } else if (isDimensionValue(value)) {
            return a.getDimensionPixelOffset(index, defValue);
        } else if (value.type >= TypedValue.TYPE_FIRST_INT
                && value.type <= TypedValue.TYPE_LAST_INT) {
        } else if (isIntegerValue(value)) {
            // For enum value.
            return a.getInt(index, defValue);
        }
        return defValue;
    }

    private static boolean isFractionValue(TypedValue v) {
        return v.type == TypedValue.TYPE_FRACTION;
    }

    private static boolean isDimensionValue(TypedValue v) {
        return v.type == TypedValue.TYPE_DIMENSION;
    }

    private static boolean isIntegerValue(TypedValue v) {
        return v.type >= TypedValue.TYPE_FIRST_INT && v.type <= TypedValue.TYPE_LAST_INT;
    }

    private static boolean isStringValue(TypedValue v) {
        return v.type == TypedValue.TYPE_STRING;
    }

    @SuppressWarnings("serial")
    public static class ParseException extends InflateException {
        public ParseException(String msg, XmlResourceParser parser) {