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

Commit d9435bce authored by satok's avatar satok Committed by Android Git Automerger
Browse files

am b4788fdb: Do not merge. Backport two fixes for InputMethethodFramework

* commit 'b4788fdb':
  Do not merge. Backport two fixes for InputMethethodFramework
parents c851ea56 b4788fdb
Loading
Loading
Loading
Loading
+4 −7
Original line number Diff line number Diff line
@@ -60,8 +60,7 @@ public final class InputMethodSubtype implements Parcelable {
        mSubtypeLocale = locale != null ? locale : "";
        mSubtypeMode = mode != null ? mode : "";
        mSubtypeExtraValue = extraValue != null ? extraValue : "";
        mSubtypeHashCode = hashCodeInternal(mSubtypeNameResId, mSubtypeIconResId, mSubtypeLocale,
                mSubtypeMode, mSubtypeExtraValue);
        mSubtypeHashCode = hashCodeInternal(mSubtypeLocale, mSubtypeMode, mSubtypeExtraValue);
    }

    InputMethodSubtype(Parcel source) {
@@ -74,8 +73,7 @@ public final class InputMethodSubtype implements Parcelable {
        mSubtypeMode = s != null ? s : "";
        s = source.readString();
        mSubtypeExtraValue = s != null ? s : "";
        mSubtypeHashCode = hashCodeInternal(mSubtypeNameResId, mSubtypeIconResId, mSubtypeLocale,
                mSubtypeMode, mSubtypeExtraValue);
        mSubtypeHashCode = hashCodeInternal(mSubtypeLocale, mSubtypeMode, mSubtypeExtraValue);
    }

    /**
@@ -195,9 +193,8 @@ public final class InputMethodSubtype implements Parcelable {
        }
    };

    private static int hashCodeInternal(int nameResId, int iconResId, String locale,
            String mode, String extraValue) {
        return Arrays.hashCode(new Object[] {nameResId, iconResId, locale, mode, extraValue});
    private static int hashCodeInternal(String locale, String mode, String extraValue) {
        return Arrays.hashCode(new Object[] {locale, mode, extraValue});
    }

    /**
+58 −19
Original line number Diff line number Diff line
@@ -1371,33 +1371,72 @@ public class InputMethodManagerService extends IInputMethodManager.Stub
        }
    }

    @Override
    public boolean switchToLastInputMethod(IBinder token) {
        synchronized (mMethodMap) {
            final Pair<String, String> lastIme = mSettings.getLastInputMethodAndSubtypeLocked();
            if (lastIme == null) return false;
            final InputMethodInfo lastImi = mMethodMap.get(lastIme.first);
            if (lastImi == null) return false;

            final InputMethodInfo lastImi;
            if (lastIme != null) {
                lastImi = mMethodMap.get(lastIme.first);
            } else {
                lastImi = null;
            }
            String targetLastImiId = null;
            int subtypeId = NOT_A_SUBTYPE_ID;
            if (lastIme != null && lastImi != null) {
                final boolean imiIdIsSame = lastImi.getId().equals(mCurMethodId);
                final int lastSubtypeHash = Integer.valueOf(lastIme.second);
            // If the last IME is the same as the current IME and the last subtype is not defined,
            // there is no need to switch to the last IME.
            if (imiIdIsSame && lastSubtypeHash == NOT_A_SUBTYPE_ID) return false;

            int currentSubtypeHash = mCurrentSubtype == null ? NOT_A_SUBTYPE_ID
                final int currentSubtypeHash = mCurrentSubtype == null ? NOT_A_SUBTYPE_ID
                        : mCurrentSubtype.hashCode();
                // If the last IME is the same as the current IME and the last subtype is not
                // defined, there is no need to switch to the last IME.
                if (!imiIdIsSame || lastSubtypeHash != currentSubtypeHash) {
                    targetLastImiId = lastIme.first;
                    subtypeId = getSubtypeIdFromHashCode(lastImi, lastSubtypeHash);
                }
            }

            if (TextUtils.isEmpty(targetLastImiId) && !canAddToLastInputMethod(mCurrentSubtype)) {
                // This is a safety net. If the currentSubtype can't be added to the history
                // and the framework couldn't find the last ime, we will make the last ime be
                // the most applicable enabled keyboard subtype of the system imes.
                final List<InputMethodInfo> enabled = mSettings.getEnabledInputMethodListLocked();
                if (enabled != null) {
                    final int N = enabled.size();
                    final String locale = mCurrentSubtype == null
                            ? mRes.getConfiguration().locale.toString()
                            : mCurrentSubtype.getLocale();
                    for (int i = 0; i < N; ++i) {
                        final InputMethodInfo imi = enabled.get(i);
                        if (imi.getSubtypeCount() > 0 && isSystemIme(imi)) {
                            InputMethodSubtype keyboardSubtype =
                                    findLastResortApplicableSubtypeLocked(mRes, getSubtypes(imi),
                                            SUBTYPE_MODE_KEYBOARD, locale, true);
                            if (keyboardSubtype != null) {
                                targetLastImiId = imi.getId();
                                subtypeId = getSubtypeIdFromHashCode(
                                        imi, keyboardSubtype.hashCode());
                                if(keyboardSubtype.getLocale().equals(locale)) {
                                    break;
                                }
                            }
                        }
                    }
                }
            }

            if (!TextUtils.isEmpty(targetLastImiId)) {
                if (DEBUG) {
                    Slog.d(TAG, "Switch to: " + lastImi.getId() + ", " + lastIme.second + ", from: "
                            + mCurMethodId + ", " + currentSubtypeHash);
                    Slog.d(TAG, "Switch to: " + lastImi.getId() + ", " + lastIme.second
                            + ", from: " + mCurMethodId + ", " + subtypeId);
                }
                setInputMethodWithSubtypeId(token, lastIme.first, getSubtypeIdFromHashCode(
                        lastImi, lastSubtypeHash));
                setInputMethodWithSubtypeId(token, targetLastImiId, subtypeId);
                return true;
            }
            } else {
                return false;
            }
        }
    }

    private void setInputMethodWithSubtypeId(IBinder token, String id, int subtypeId) {
        synchronized (mMethodMap) {