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

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

Change predefined additional subtype format in preference

This change also refactor StringUtils class

Change-Id: Ie0b4d169b21c260bf238d6fcc9ab0ee8bfd6b508
parent d43f7ec1
Loading
Loading
Loading
Loading
+2 −2
Original line number Diff line number Diff line
@@ -18,6 +18,6 @@
*/
-->
<resources xmlns:xliff="urn:oasis:names:tc:xliff:document:1.2">
    <!-- Predefined subtypes (language:layout) in CSV format -->
    <string name="predefined_subtypes" translatable="false">de:qwerty,fr:qwertz</string>
    <!-- Predefined subtypes (language:layout[:extraValue]) in semicolon separated format -->
    <string name="predefined_subtypes" translatable="false">de:qwerty:AsciiCapable;fr:qwertz:AsciiCapable</string>
</resources>
+5 −5
Original line number Diff line number Diff line
@@ -29,11 +29,11 @@ import android.view.inputmethod.InputMethodSubtype;

import com.android.inputmethod.compat.EditorInfoCompatUtils;
import com.android.inputmethod.keyboard.KeyboardLayoutSet.Params.ElementParams;
import com.android.inputmethod.latin.InputAttributes;
import com.android.inputmethod.latin.InputTypeUtils;
import com.android.inputmethod.latin.LatinIME;
import com.android.inputmethod.latin.LatinImeLogger;
import com.android.inputmethod.latin.R;
import com.android.inputmethod.latin.StringUtils;
import com.android.inputmethod.latin.SubtypeLocale;
import com.android.inputmethod.latin.SubtypeSwitcher;
import com.android.inputmethod.latin.XmlParseUtils;
@@ -229,7 +229,7 @@ public class KeyboardLayoutSet {

            params.mMode = getKeyboardMode(editorInfo);
            params.mEditorInfo = (editorInfo != null) ? editorInfo : EMPTY_EDITOR_INFO;
            params.mNoSettingsKey = StringUtils.inPrivateImeOptions(
            params.mNoSettingsKey = InputAttributes.inPrivateImeOptions(
                    mPackageName, LatinIME.IME_OPTION_NO_SETTINGS_KEY, mEditorInfo);
        }

@@ -242,7 +242,7 @@ public class KeyboardLayoutSet {
        public Builder setSubtype(InputMethodSubtype subtype) {
            final boolean asciiCapable = subtype.containsExtraValueKey(
                    LatinIME.SUBTYPE_EXTRA_VALUE_ASCII_CAPABLE);
            final boolean deprecatedForceAscii = StringUtils.inPrivateImeOptions(
            final boolean deprecatedForceAscii = InputAttributes.inPrivateImeOptions(
                    mPackageName, LatinIME.IME_OPTION_FORCE_ASCII, mEditorInfo);
            final boolean forceAscii = EditorInfoCompatUtils.hasFlagForceAscii(
                    mParams.mEditorInfo.imeOptions)
@@ -259,9 +259,9 @@ public class KeyboardLayoutSet {
        public Builder setOptions(boolean voiceKeyEnabled, boolean voiceKeyOnMain,
                boolean languageSwitchKeyEnabled) {
            @SuppressWarnings("deprecation")
            final boolean deprecatedNoMicrophone = StringUtils.inPrivateImeOptions(
            final boolean deprecatedNoMicrophone = InputAttributes.inPrivateImeOptions(
                    null, LatinIME.IME_OPTION_NO_MICROPHONE_COMPAT, mEditorInfo);
            final boolean noMicrophone = StringUtils.inPrivateImeOptions(
            final boolean noMicrophone = InputAttributes.inPrivateImeOptions(
                    mPackageName, LatinIME.IME_OPTION_NO_MICROPHONE, mEditorInfo)
                    || deprecatedNoMicrophone;
            mParams.mVoiceKeyEnabled = voiceKeyEnabled && !noMicrophone;
+20 −13
Original line number Diff line number Diff line
@@ -43,28 +43,35 @@ public class AdditionalSubtype {
    }

    public static InputMethodSubtype createAdditionalSubtype(
            String localeString, String keyboardLayoutSet) {
        final String extraValue = String.format(
                "%s=%s,%s", LatinIME.SUBTYPE_EXTRA_VALUE_KEYBOARD_LAYOUT_SET, keyboardLayoutSet,
                SUBTYPE_EXTRA_VALUE_IS_ADDITIONAL_SUBTYPE);
        Integer nameId = sKeyboardLayoutToNameIdsMap.get(keyboardLayoutSet);
            String localeString, String keyboardLayoutSetName, String extraValue) {
        final String layoutExtraValue = LatinIME.SUBTYPE_EXTRA_VALUE_KEYBOARD_LAYOUT_SET + "="
                + keyboardLayoutSetName;
        final String filteredExtraValue = StringUtils.appendToCsvIfNotExists(
                SUBTYPE_EXTRA_VALUE_IS_ADDITIONAL_SUBTYPE,
                StringUtils.removeFromCsvIfExists(layoutExtraValue, extraValue));
        Integer nameId = sKeyboardLayoutToNameIdsMap.get(keyboardLayoutSetName);
        if (nameId == null) nameId = R.string.subtype_generic;
        return new InputMethodSubtype(nameId, R.drawable.ic_subtype_keyboard,
                localeString, SUBTYPE_MODE_KEYBOARD, extraValue, false, false);
                localeString, SUBTYPE_MODE_KEYBOARD, filteredExtraValue, false, false);
    }

    private static final String LOCALE_AND_LAYOUT_SEPARATOR = ":";
    private static final String SUBTYPE_SEPARATOR = ",";
    private static final String PREF_SUBTYPE_SEPARATOR = ";";

    public static InputMethodSubtype[] createAdditionalSubtypesArray(String csvSubtypes) {
        final String[] subtypeSpecs = csvSubtypes.split(SUBTYPE_SEPARATOR);
        final InputMethodSubtype[] subtypesArray = new InputMethodSubtype[subtypeSpecs.length];
        for (int i = 0; i < subtypeSpecs.length; i++) {
            final String elems[] = subtypeSpecs[i].split(LOCALE_AND_LAYOUT_SEPARATOR);
    public static InputMethodSubtype[] createAdditionalSubtypesArray(String prefSubtypes) {
        final String[] prefSubtypeArray = prefSubtypes.split(PREF_SUBTYPE_SEPARATOR);
        final InputMethodSubtype[] subtypesArray = new InputMethodSubtype[prefSubtypeArray.length];
        for (int i = 0; i < prefSubtypeArray.length; i++) {
            final String prefSubtype = prefSubtypeArray[i];
            final String elems[] = prefSubtype.split(LOCALE_AND_LAYOUT_SEPARATOR);
            if (elems.length < 2 || elems.length > 3) {
                throw new RuntimeException("Unknown subtype found in preference: " + prefSubtype);
            }
            final String localeString = elems[0];
            final String keyboardLayoutSetName = elems[1];
            final String extraValue = (elems.length == 3) ? elems[2] : null;
            subtypesArray[i] = AdditionalSubtype.createAdditionalSubtype(
                    localeString, keyboardLayoutSetName);
                    localeString, keyboardLayoutSetName, extraValue);
        }
        return subtypesArray;
    }
+8 −0
Original line number Diff line number Diff line
@@ -162,4 +162,12 @@ public class InputAttributes {
                + "\n mIsSettingsSuggestionStripOn = " + mIsSettingsSuggestionStripOn
                + "\n mApplicationSpecifiedCompletionOn = " + mApplicationSpecifiedCompletionOn;
    }

    public static boolean inPrivateImeOptions(String packageName, String key,
            EditorInfo editorInfo) {
        if (editorInfo == null) return false;
        final String findingKey = (packageName != null) ? packageName + "." + key
                : key;
        return StringUtils.containsInCsv(findingKey, editorInfo.privateImeOptions);
    }
}
+17 −3
Original line number Diff line number Diff line
@@ -669,12 +669,14 @@ public class LatinIME extends InputMethodService implements KeyboardActionListen
        if (ProductionFlag.IS_EXPERIMENTAL) {
            ResearchLogger.latinIME_onStartInputViewInternal(editorInfo);
        }
        if (StringUtils.inPrivateImeOptions(null, IME_OPTION_NO_MICROPHONE_COMPAT, editorInfo)) {
        if (InputAttributes.inPrivateImeOptions(
                null, IME_OPTION_NO_MICROPHONE_COMPAT, editorInfo)) {
            Log.w(TAG, "Deprecated private IME option specified: "
                    + editorInfo.privateImeOptions);
            Log.w(TAG, "Use " + getPackageName() + "." + IME_OPTION_NO_MICROPHONE + " instead");
        }
        if (StringUtils.inPrivateImeOptions(getPackageName(), IME_OPTION_FORCE_ASCII, editorInfo)) {
        if (InputAttributes.inPrivateImeOptions(
                getPackageName(), IME_OPTION_FORCE_ASCII, editorInfo)) {
            Log.w(TAG, "Deprecated private IME option specified: "
                    + editorInfo.privateImeOptions);
            Log.w(TAG, "Use EditorInfo.IME_FLAG_FORCE_ASCII flag instead");
@@ -1077,7 +1079,7 @@ public class LatinIME extends InputMethodService implements KeyboardActionListen
        if (ic == null) return false;
        final CharSequence lastThree = ic.getTextBeforeCursor(3, 0);
        if (lastThree != null && lastThree.length() == 3
                && StringUtils.canBeFollowedByPeriod(lastThree.charAt(0))
                && canBeFollowedByPeriod(lastThree.charAt(0))
                && lastThree.charAt(1) == Keyboard.CODE_SPACE
                && lastThree.charAt(2) == Keyboard.CODE_SPACE
                && mHandler.isAcceptingDoubleSpaces()) {
@@ -1093,6 +1095,18 @@ public class LatinIME extends InputMethodService implements KeyboardActionListen
        return false;
    }

    private static boolean canBeFollowedByPeriod(final int codePoint) {
        // TODO: Check again whether there really ain't a better way to check this.
        // TODO: This should probably be language-dependant...
        return Character.isLetterOrDigit(codePoint)
                || codePoint == Keyboard.CODE_SINGLE_QUOTE
                || codePoint == Keyboard.CODE_DOUBLE_QUOTE
                || codePoint == Keyboard.CODE_CLOSING_PARENTHESIS
                || codePoint == Keyboard.CODE_CLOSING_SQUARE_BRACKET
                || codePoint == Keyboard.CODE_CLOSING_CURLY_BRACKET
                || codePoint == Keyboard.CODE_CLOSING_ANGLE_BRACKET;
    }

    // "ic" may be null
    private static void removeTrailingSpaceWhileInBatchEdit(final InputConnection ic) {
        if (ic == null) return;
Loading