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

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

Refactor keyboard mode holding variables

Change-Id: Id8d4e780a98292fda63b1e5bbf739eda78935a3b
parent f5f834af
Loading
Loading
Loading
Loading
+11 −15
Original line number Diff line number Diff line
@@ -67,8 +67,6 @@ public class KeyboardSwitcher implements SharedPreferences.OnSharedPreferenceCha
    private final HashMap<KeyboardId, SoftReference<LatinKeyboard>> mKeyboardCache =
            new HashMap<KeyboardId, SoftReference<LatinKeyboard>>();

    // TODO: clean mMode up and use mAttribute instead.
    private int mMode = KeyboardId.MODE_TEXT; /* default value */
    private EditorInfo mAttribute;
    private boolean mIsSymbols;
    /** mIsAutoCorrectionActive indicates that auto corrected word will be input instead of
@@ -124,11 +122,10 @@ public class KeyboardSwitcher implements SharedPreferences.OnSharedPreferenceCha
        prefs.registerOnSharedPreferenceChangeListener(sInstance);
    }

    private void makeSymbolsKeyboardIds() {
    private void makeSymbolsKeyboardIds(final int mode) {
        final Locale locale = mSubtypeSwitcher.getInputLocale();
        final Resources res = mInputMethodService.getResources();
        final int orientation = res.getConfiguration().orientation;
        final int mode = mMode;
        final int colorScheme = getColorScheme();
        final boolean hasVoiceKey = mVoiceKeyEnabled && !mVoiceButtonOnPrimary;
        // Note: This comment is only applied for phone number keyboard layout.
@@ -151,37 +148,36 @@ public class KeyboardSwitcher implements SharedPreferences.OnSharedPreferenceCha
        return mVoiceKeyEnabled && (isSymbols != mVoiceButtonOnPrimary);
    }

    public void loadKeyboard(int mode, EditorInfo attribute, boolean voiceKeyEnabled,
    public void loadKeyboard(EditorInfo attribute, boolean voiceKeyEnabled,
            boolean voiceButtonOnPrimary) {
        mAutoModeSwitchState = AUTO_MODE_SWITCH_STATE_ALPHA;
        try {
            loadKeyboardInternal(mode, attribute, voiceKeyEnabled, voiceButtonOnPrimary, false);
            loadKeyboardInternal(attribute, voiceKeyEnabled, voiceButtonOnPrimary, false);
        } catch (RuntimeException e) {
            // Get KeyboardId to record which keyboard has been failed to load.
            final KeyboardId id = getKeyboardId(mode, attribute, false);
            final KeyboardId id = getKeyboardId(attribute, false);
            Log.w(TAG, "loading keyboard failed: " + id, e);
            LatinImeLogger.logOnException(id.toString(), e);
        }
    }

    private void loadKeyboardInternal(int mode, EditorInfo attribute, boolean voiceButtonEnabled,
    private void loadKeyboardInternal(EditorInfo attribute, boolean voiceButtonEnabled,
            boolean voiceButtonOnPrimary, boolean isSymbols) {
        if (mInputView == null) return;

        mMode = mode;
        mAttribute = attribute;
        mVoiceKeyEnabled = voiceButtonEnabled;
        mVoiceButtonOnPrimary = voiceButtonOnPrimary;
        mIsSymbols = isSymbols;
        // Update the settings key state because number of enabled IMEs could have been changed
        mHasSettingsKey = getSettingsKeyMode(mPrefs, mInputMethodService);
        final KeyboardId id = getKeyboardId(mode, attribute, isSymbols);
        final KeyboardId id = getKeyboardId(attribute, isSymbols);

        final Keyboard oldKeyboard = mInputView.getKeyboard();
        if (oldKeyboard != null && oldKeyboard.mId.equals(id))
            return;

        makeSymbolsKeyboardIds();
        makeSymbolsKeyboardIds(id.mMode);
        mCurrentId = id;
        mInputView.setPreviewEnabled(mInputMethodService.getPopupOn());
        setKeyboard(getKeyboard(id));
@@ -228,7 +224,8 @@ public class KeyboardSwitcher implements SharedPreferences.OnSharedPreferenceCha
        return keyboard;
    }

    private KeyboardId getKeyboardId(int mode, EditorInfo attribute, boolean isSymbols) {
    private KeyboardId getKeyboardId(EditorInfo attribute, boolean isSymbols) {
        final int mode = Utils.getKeyboardMode(attribute);
        final boolean hasVoiceKey = hasVoiceKey(isSymbols);
        final int charColorId = getColorScheme();
        final int xmlId;
@@ -265,7 +262,7 @@ public class KeyboardSwitcher implements SharedPreferences.OnSharedPreferenceCha
    }

    public int getKeyboardMode() {
        return mMode;
        return mCurrentId != null ? mCurrentId.mMode : KeyboardId.MODE_TEXT;
    }

    public boolean isAlphabetMode() {
@@ -569,8 +566,7 @@ public class KeyboardSwitcher implements SharedPreferences.OnSharedPreferenceCha
    }

    private void toggleKeyboardMode() {
        loadKeyboardInternal(mMode, mAttribute, mVoiceKeyEnabled, mVoiceButtonOnPrimary,
                !mIsSymbols);
        loadKeyboardInternal(mAttribute, mVoiceKeyEnabled, mVoiceButtonOnPrimary, !mIsSymbols);
        if (mIsSymbols) {
            mAutoModeSwitchState = AUTO_MODE_SWITCH_STATE_SYMBOL_BEGIN;
        } else {
+45 −76
Original line number Diff line number Diff line
@@ -18,7 +18,6 @@ package com.android.inputmethod.latin;

import com.android.inputmethod.keyboard.Keyboard;
import com.android.inputmethod.keyboard.KeyboardActionListener;
import com.android.inputmethod.keyboard.KeyboardId;
import com.android.inputmethod.keyboard.KeyboardSwitcher;
import com.android.inputmethod.keyboard.KeyboardView;
import com.android.inputmethod.keyboard.LatinKeyboard;
@@ -494,11 +493,6 @@ public class LatinIME extends InputMethodService implements KeyboardActionListen
        return container;
    }

    private static boolean isEmailVariation(int variation) {
        return variation == InputType.TYPE_TEXT_VARIATION_EMAIL_ADDRESS
                || variation == InputType.TYPE_TEXT_VARIATION_WEB_EMAIL_ADDRESS;
    }

    @Override
    public void onStartInputView(EditorInfo attribute, boolean restarting) {
        final KeyboardSwitcher switcher = mKeyboardSwitcher;
@@ -522,7 +516,7 @@ public class LatinIME extends InputMethodService implements KeyboardActionListen
        mVoiceConnector.resetVoiceStates(Utils.isPasswordInputType(attribute.inputType)
                || Utils.isVisiblePasswordInputType(attribute.inputType));

        final int mode = initializeInputAttributesAndGetMode(attribute);
        initializeInputAttributes(attribute);

        inputView.closing();
        mEnteredText = null;
@@ -533,7 +527,7 @@ public class LatinIME extends InputMethodService implements KeyboardActionListen

        loadSettings(attribute);
        if (mSubtypeSwitcher.isKeyboardMode()) {
            switcher.loadKeyboard(mode, attribute,
            switcher.loadKeyboard(attribute,
                    mVoiceConnector.isVoiceButtonEnabled(),
                    mVoiceConnector.isVoiceButtonOnPrimary());
            switcher.updateShiftState();
@@ -557,10 +551,9 @@ public class LatinIME extends InputMethodService implements KeyboardActionListen
        if (TRACE) Debug.startMethodTracing("/data/trace/latinime");
    }

    // TODO: Separate calculating keyboard mode from initializing attributes, and make it an
    // utility method in {@link Utils}.
    private int initializeInputAttributesAndGetMode(EditorInfo attribute) {
        if (attribute == null) return KeyboardId.MODE_TEXT;
    private void initializeInputAttributes(EditorInfo attribute) {
        if (attribute == null)
            return;
        final int inputType = attribute.inputType;
        final int variation = inputType & InputType.TYPE_MASK_VARIATION;
        mAutoSpace = false;
@@ -569,48 +562,31 @@ public class LatinIME extends InputMethodService implements KeyboardActionListen
        mApplicationSpecifiedCompletionOn = false;
        mApplicationSpecifiedCompletions = null;

        final int mode;
        switch (inputType & InputType.TYPE_MASK_CLASS) {
            case InputType.TYPE_CLASS_NUMBER:
            case InputType.TYPE_CLASS_DATETIME:
                mode = KeyboardId.MODE_NUMBER;
                break;
            case InputType.TYPE_CLASS_PHONE:
                mode = KeyboardId.MODE_PHONE;
                break;
            case InputType.TYPE_CLASS_TEXT:
        if ((inputType & InputType.TYPE_MASK_CLASS) == InputType.TYPE_CLASS_TEXT) {
            mIsSettingsSuggestionStripOn = true;
            // Make sure that passwords are not displayed in candidate view
            if (Utils.isPasswordInputType(inputType)
                    || Utils.isVisiblePasswordInputType(inputType)) {
                mIsSettingsSuggestionStripOn = false;
            }
                if (LatinIME.isEmailVariation(variation)
            if (Utils.isEmailVariation(variation)
                    || variation == InputType.TYPE_TEXT_VARIATION_PERSON_NAME) {
                mAutoSpace = false;
            } else {
                mAutoSpace = true;
            }
                if (LatinIME.isEmailVariation(variation)) {
            if (Utils.isEmailVariation(variation)) {
                mIsSettingsSuggestionStripOn = false;
                    mode = KeyboardId.MODE_EMAIL;
            } else if (variation == InputType.TYPE_TEXT_VARIATION_URI) {
                mIsSettingsSuggestionStripOn = false;
                    mode = KeyboardId.MODE_URL;
                } else if (variation == InputType.TYPE_TEXT_VARIATION_SHORT_MESSAGE) {
                    mode = KeyboardId.MODE_IM;
            } else if (variation == InputType.TYPE_TEXT_VARIATION_FILTER) {
                mIsSettingsSuggestionStripOn = false;
                    mode = KeyboardId.MODE_TEXT;
            } else if (variation == InputType.TYPE_TEXT_VARIATION_WEB_EDIT_TEXT) {
                    mode = KeyboardId.MODE_WEB;
                // If it's a browser edit field and auto correct is not ON explicitly, then
                // disable auto correction, but keep suggestions on.
                if ((inputType & InputType.TYPE_TEXT_FLAG_AUTO_CORRECT) == 0) {
                    mInputTypeNoAutoCorrect = true;
                }
                } else {
                    mode = KeyboardId.MODE_TEXT;
            }

            // If NO_SUGGESTIONS is set, don't do prediction.
@@ -619,20 +595,15 @@ public class LatinIME extends InputMethodService implements KeyboardActionListen
                mInputTypeNoAutoCorrect = true;
            }
            // If it's not multiline and the autoCorrect flag is not set, then don't correct
                if ((inputType & InputType.TYPE_TEXT_FLAG_AUTO_CORRECT) == 0 &&
                        (inputType & InputType.TYPE_TEXT_FLAG_MULTI_LINE) == 0) {
            if ((inputType & InputType.TYPE_TEXT_FLAG_AUTO_CORRECT) == 0
                    && (inputType & InputType.TYPE_TEXT_FLAG_MULTI_LINE) == 0) {
                mInputTypeNoAutoCorrect = true;
            }
            if ((inputType & InputType.TYPE_TEXT_FLAG_AUTO_COMPLETE) != 0) {
                mIsSettingsSuggestionStripOn = false;
                mApplicationSpecifiedCompletionOn = isFullscreenMode();
            }
                break;
            default:
                mode = KeyboardId.MODE_TEXT;
                break;
        }
        return mode;
    }

    private void checkReCorrectionOnStart() {
@@ -1909,9 +1880,7 @@ public class LatinIME extends InputMethodService implements KeyboardActionListen
            mSubtypeSwitcher.toggleLanguage(reset, next);
        }
        // Reload keyboard because the current language has been changed.
        final EditorInfo attribute = getCurrentInputEditorInfo();
        final int mode = initializeInputAttributesAndGetMode(attribute);
        mKeyboardSwitcher.loadKeyboard(mode, attribute,
        mKeyboardSwitcher.loadKeyboard(getCurrentInputEditorInfo(),
                mVoiceConnector.isVoiceButtonEnabled(), mVoiceConnector.isVoiceButtonOnPrimary());
        initSuggest();
        mKeyboardSwitcher.updateShiftState();
+0 −1
Original line number Diff line number Diff line
@@ -18,7 +18,6 @@ package com.android.inputmethod.latin;

import com.android.inputmethod.keyboard.KeyboardSwitcher;
import com.android.inputmethod.keyboard.LatinKeyboard;
import com.android.inputmethod.keyboard.LatinKeyboardView;
import com.android.inputmethod.voice.SettingsUtil;
import com.android.inputmethod.voice.VoiceIMEConnector;
import com.android.inputmethod.voice.VoiceInput;
+47 −6
Original line number Diff line number Diff line
@@ -45,6 +45,10 @@ public class Utils {
    private static final int MINIMUM_SAFETY_NET_CHAR_LENGTH = 4;
    private static boolean DBG = LatinImeLogger.sDBG;

    private Utils() {
        // Intentional empty constructor for utility class.
    }

    /**
     * Cancel an {@link AsyncTask}.
     *
@@ -319,7 +323,7 @@ public class Utils {
    }

    public static class UsabilityStudyLogUtils {
        private static final String TAG = "UsabilityStudyLogUtils";
        private static final String USABILITY_TAG = UsabilityStudyLogUtils.class.getSimpleName();
        private static final String FILENAME = "log.txt";
        private static final UsabilityStudyLogUtils sInstance =
                new UsabilityStudyLogUtils();
@@ -356,7 +360,7 @@ public class Utils {
                try {
                    mWriter = getPrintWriter(mDirectory, FILENAME, false);
                } catch (IOException e) {
                    Log.e(TAG, "Can't create log file.");
                    Log.e(USABILITY_TAG, "Can't create log file.");
                }
            }
        }
@@ -393,7 +397,7 @@ public class Utils {
                    final String printString = String.format("%s\t%d\t%s\n",
                            mDateFormat.format(mDate), currentTime, log);
                    if (LatinImeLogger.sDBG) {
                        Log.d(TAG, "Write: " + log);
                        Log.d(USABILITY_TAG, "Write: " + log);
                    }
                    mWriter.print(printString);
                }
@@ -414,10 +418,10 @@ public class Utils {
                            sb.append(line);
                        }
                    } catch (IOException e) {
                        Log.e(TAG, "Can't read log file.");
                        Log.e(USABILITY_TAG, "Can't read log file.");
                    } finally {
                        if (LatinImeLogger.sDBG) {
                            Log.d(TAG, "output all logs\n" + sb.toString());
                            Log.d(USABILITY_TAG, "output all logs\n" + sb.toString());
                        }
                        mIms.getCurrentInputConnection().commitText(sb.toString(), 0);
                        try {
@@ -436,7 +440,7 @@ public class Utils {
                public void run() {
                    if (mFile != null && mFile.exists()) {
                        if (LatinImeLogger.sDBG) {
                            Log.d(TAG, "Delete log file.");
                            Log.d(USABILITY_TAG, "Delete log file.");
                        }
                        mFile.delete();
                        mWriter.close();
@@ -466,6 +470,43 @@ public class Utils {
        }
    }

    public static int getKeyboardMode(EditorInfo attribute) {
        if (attribute == null)
            return KeyboardId.MODE_TEXT;

        final int inputType = attribute.inputType;
        final int variation = inputType & InputType.TYPE_MASK_VARIATION;

        switch (inputType & InputType.TYPE_MASK_CLASS) {
        case InputType.TYPE_CLASS_NUMBER:
        case InputType.TYPE_CLASS_DATETIME:
            return KeyboardId.MODE_NUMBER;
        case InputType.TYPE_CLASS_PHONE:
            return KeyboardId.MODE_PHONE;
        case InputType.TYPE_CLASS_TEXT:
            if (Utils.isEmailVariation(variation)) {
                return KeyboardId.MODE_EMAIL;
            } else if (variation == InputType.TYPE_TEXT_VARIATION_URI) {
                return KeyboardId.MODE_URL;
            } else if (variation == InputType.TYPE_TEXT_VARIATION_SHORT_MESSAGE) {
                return KeyboardId.MODE_IM;
            } else if (variation == InputType.TYPE_TEXT_VARIATION_FILTER) {
                return KeyboardId.MODE_TEXT;
            } else if (variation == InputType.TYPE_TEXT_VARIATION_WEB_EDIT_TEXT) {
                return KeyboardId.MODE_WEB;
            } else {
                return KeyboardId.MODE_TEXT;
            }
        default:
            return KeyboardId.MODE_TEXT;
        }
    }

    public static boolean isEmailVariation(int variation) {
        return variation == InputType.TYPE_TEXT_VARIATION_EMAIL_ADDRESS
                || variation == InputType.TYPE_TEXT_VARIATION_WEB_EMAIL_ADDRESS;
    }

    // Please refer to TextView.isPasswordInputType
    public static boolean isPasswordInputType(int inputType) {
        final int variation =