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

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

Add input method subtype selector and IME settings dialog

Bug: 3351762
Change-Id: Ic1767faac6d4470a89cacb851d449ac53b2f8205
parent fefda4e6
Loading
Loading
Loading
Loading
+54 −20
Original line number Diff line number Diff line
@@ -123,6 +123,9 @@ public class LatinIME extends InputMethodService implements KeyboardActionListen
    private AlertDialog mOptionsDialog;

    private InputMethodManager mImm;
    private Resources mResources;
    private SharedPreferences mPrefs;
    private String mInputMethodId;
    private KeyboardSwitcher mKeyboardSwitcher;
    private SubtypeSwitcher mSubtypeSwitcher;
    private VoiceIMEConnector mVoiceConnector;
@@ -132,9 +135,6 @@ public class LatinIME extends InputMethodService implements KeyboardActionListen
    private ContactsDictionary mContactsDictionary;
    private AutoDictionary mAutoDictionary;

    private Resources mResources;
    private SharedPreferences mPrefs;

    // These variables are initialized according to the {@link EditorInfo#inputType}.
    private boolean mAutoSpace;
    private boolean mInputTypeNoAutoCorrect;
@@ -156,6 +156,7 @@ public class LatinIME extends InputMethodService implements KeyboardActionListen
    private boolean mPopupOn;
    private boolean mAutoCap;
    private boolean mQuickFixes;
    private boolean mConfigEnableShowSubtypeSettings;
    private boolean mConfigSwipeDownDismissKeyboardEnabled;
    private int mConfigDelayBeforeFadeoutLanguageOnSpacebar;
    private int mConfigDurationOfFadeoutLanguageOnSpacebar;
@@ -350,6 +351,7 @@ public class LatinIME extends InputMethodService implements KeyboardActionListen
        super.onCreate();

        mImm = ((InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE));
        mInputMethodId = Utils.getInputMethodId(mImm, getApplicationInfo().packageName);
        mSubtypeSwitcher = SubtypeSwitcher.getInstance();
        mKeyboardSwitcher = KeyboardSwitcher.getInstance();

@@ -365,6 +367,8 @@ public class LatinIME extends InputMethodService implements KeyboardActionListen
            mReCorrectionEnabled = res.getBoolean(R.bool.default_recorrection_enabled);
        }

        mConfigEnableShowSubtypeSettings = res.getBoolean(
                R.bool.config_enable_show_subtype_settings);
        mConfigSwipeDownDismissKeyboardEnabled = res.getBoolean(
                R.bool.config_swipe_down_dismiss_keyboard_enabled);
        mConfigDelayBeforeFadeoutLanguageOnSpacebar = res.getInteger(
@@ -462,6 +466,8 @@ public class LatinIME extends InputMethodService implements KeyboardActionListen
            commitTyped(ic);
            if (ic != null) ic.finishComposingText(); // For voice input
            mOrientation = conf.orientation;
            if (isShowingOptionDialog())
                mOptionsDialog.dismiss();
        }

        mConfigurationChanging = true;
@@ -1044,7 +1050,9 @@ public class LatinIME extends InputMethodService implements KeyboardActionListen

    private void onSettingsKeyPressed() {
        if (!isShowingOptionDialog()) {
            if (Utils.hasMultipleEnabledIMEsOrSubtypes(mImm)) {
            if (!mConfigEnableShowSubtypeSettings) {
                showSubtypeSelectorAndSettings();
            } else if (Utils.hasMultipleEnabledIMEsOrSubtypes(mImm)) {
                showOptionsMenu();
            } else {
                launchSettings();
@@ -2183,17 +2191,32 @@ public class LatinIME extends InputMethodService implements KeyboardActionListen
        return mSuggestPuncs.contains(String.valueOf((char)code));
    }

    private void showOptionsMenu() {
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setCancelable(true);
        builder.setIcon(R.drawable.ic_dialog_keyboard);
        builder.setNegativeButton(android.R.string.cancel, null);
        CharSequence itemSettings = getString(R.string.english_ime_settings);
        CharSequence itemInputMethod = getString(R.string.selectInputMethod);
        builder.setItems(new CharSequence[] {
                itemInputMethod, itemSettings},
                new DialogInterface.OnClickListener() {
    private void showSubtypeSelectorAndSettings() {
        showOptionsMenuInternal(new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface di, int position) {
                di.dismiss();
                switch (position) {
                case POS_SETTINGS:
                    launchSettings();
                    break;
                case POS_METHOD:
                    Intent intent = new Intent(
                            android.provider.Settings.ACTION_INPUT_METHOD_SUBTYPE_SETTINGS);
                    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
                            | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED
                            | Intent.FLAG_ACTIVITY_CLEAR_TOP);
                    intent.putExtra(android.provider.Settings.EXTRA_INPUT_METHOD_ID,
                            mInputMethodId);
                    startActivity(intent);
                    break;
                }
            }
        });
    }

    private void showOptionsMenu() {
        showOptionsMenuInternal(new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface di, int position) {
                di.dismiss();
@@ -2207,6 +2230,17 @@ public class LatinIME extends InputMethodService implements KeyboardActionListen
                }
            }
        });
    }

    private void showOptionsMenuInternal(DialogInterface.OnClickListener listener) {
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setCancelable(true);
        builder.setIcon(R.drawable.ic_dialog_keyboard);
        builder.setNegativeButton(android.R.string.cancel, null);
        CharSequence itemSettings = getString(R.string.english_ime_settings);
        CharSequence itemInputMethod = getString(R.string.selectInputMethod);
        builder.setItems(new CharSequence[] {
                itemInputMethod, itemSettings}, listener);
        builder.setTitle(mResources.getString(R.string.english_ime_input_options));
        mOptionsDialog = builder.create();
        Window window = mOptionsDialog.getWindow();
+8 −0
Original line number Diff line number Diff line
@@ -23,6 +23,7 @@ import android.os.HandlerThread;
import android.os.Process;
import android.text.format.DateUtils;
import android.util.Log;
import android.view.inputmethod.InputMethodInfo;
import android.view.inputmethod.InputMethodManager;

import java.io.BufferedReader;
@@ -97,6 +98,13 @@ public class Utils {
                || imm.getEnabledInputMethodSubtypeList(null, false).size() > 1;
    }

    public static String getInputMethodId(InputMethodManager imm, String packageName) {
        for (final InputMethodInfo imi : imm.getEnabledInputMethodList()) {
            if (imi.getPackageName().equals(packageName))
                return imi.getId();
        }
        throw new RuntimeException("Can not find input method id for " + packageName);
    }

    public static boolean shouldBlockedBySafetyNetForAutoCorrection(SuggestedWords suggestions) {
        // Safety net for auto correction.