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

Commit e21c63c6 authored by Yohei Yukawa's avatar Yohei Yukawa
Browse files

Extract out logic from IMMS#getCurrentInputMethodSubtypeLocked()

This is a mechanical refactoring CL to extract out a bit complicated
logic out of

  InputMethodManagerService#getCurrentInputMethodSubtypeLocked()

without changing any behavior.

Bug: 346658341
Test: presubmit
Flag: EXEMPT refactor
Change-Id: Ie876f94d3877370b88f2d605877d3b45c0a63e2b
parent 2d63f4a2
Loading
Loading
Loading
Loading
+2 −29
Original line number Diff line number Diff line
@@ -5513,35 +5513,8 @@ public final class InputMethodManagerService implements IInputMethodManagerImpl.
        if (imi == null || imi.getSubtypeCount() == 0) {
            return null;
        }
        final int currentSubtypeHashCode = SecureSettingsWrapper.getInt(
                Settings.Secure.SELECTED_INPUT_METHOD_SUBTYPE, NOT_A_SUBTYPE_ID, userId);
        if (currentSubtypeHashCode == NOT_A_SUBTYPE_ID || mCurrentSubtype == null
                || !SubtypeUtils.isValidSubtypeId(imi, mCurrentSubtype.hashCode())) {
            int subtypeId = settings.getSelectedInputMethodSubtypeId(selectedMethodId);
            if (subtypeId == NOT_A_SUBTYPE_ID) {
                // If there are no selected subtypes, the framework will try to find
                // the most applicable subtype from explicitly or implicitly enabled
                // subtypes.
                List<InputMethodSubtype> explicitlyOrImplicitlyEnabledSubtypes =
                        settings.getEnabledInputMethodSubtypeList(imi, true);
                // If there is only one explicitly or implicitly enabled subtype,
                // just returns it.
                if (explicitlyOrImplicitlyEnabledSubtypes.size() == 1) {
                    mCurrentSubtype = explicitlyOrImplicitlyEnabledSubtypes.get(0);
                } else if (explicitlyOrImplicitlyEnabledSubtypes.size() > 1) {
                    final String locale = SystemLocaleWrapper.get(userId).get(0).toString();
                    mCurrentSubtype = SubtypeUtils.findLastResortApplicableSubtype(
                            explicitlyOrImplicitlyEnabledSubtypes,
                            SubtypeUtils.SUBTYPE_MODE_KEYBOARD, locale, true);
                    if (mCurrentSubtype == null) {
                        mCurrentSubtype = SubtypeUtils.findLastResortApplicableSubtype(
                                explicitlyOrImplicitlyEnabledSubtypes, null, locale, true);
                    }
                }
            } else {
                mCurrentSubtype = SubtypeUtils.getSubtypes(imi).get(subtypeId);
            }
        }
        mCurrentSubtype = SubtypeUtils.getCurrentInputMethodSubtype(imi, settings,
                mCurrentSubtype);
        return mCurrentSubtype;
    }

+52 −0
Original line number Diff line number Diff line
@@ -16,9 +16,11 @@

package com.android.server.inputmethod;

import android.annotation.AnyThread;
import android.annotation.NonNull;
import android.annotation.Nullable;
import android.os.LocaleList;
import android.provider.Settings;
import android.text.TextUtils;
import android.util.ArrayMap;
import android.util.Slog;
@@ -289,4 +291,54 @@ final class SubtypeUtils {
        }
        return applicableSubtype;
    }

    /**
     * Returns a {@link InputMethodSubtype} available in {@code imi} based on
     * {@link Settings.Secure#SELECTED_INPUT_METHOD_SUBTYPE}.
     *
     * @param imi            {@link InputMethodInfo} to find out the current
     *                       {@link InputMethodSubtype}
     * @param settings       {@link InputMethodSettings} to be used to find out the current
     *                       {@link InputMethodSubtype}
     * @param currentSubtype the current value that will be used as fallback
     * @return {@link InputMethodSubtype} to be used as the current {@link InputMethodSubtype}
     */
    @AnyThread
    @Nullable
    static InputMethodSubtype getCurrentInputMethodSubtype(
            @NonNull InputMethodInfo imi, @NonNull InputMethodSettings settings,
            @Nullable InputMethodSubtype currentSubtype) {
        final int userId = settings.getUserId();
        final int selectedSubtypeHashCode = SecureSettingsWrapper.getInt(
                Settings.Secure.SELECTED_INPUT_METHOD_SUBTYPE, NOT_A_SUBTYPE_ID, userId);
        if (selectedSubtypeHashCode != NOT_A_SUBTYPE_ID && currentSubtype != null
                && isValidSubtypeId(imi, currentSubtype.hashCode())) {
            return currentSubtype;
        }

        final int subtypeId = settings.getSelectedInputMethodSubtypeId(imi.getId());
        if (subtypeId != NOT_A_SUBTYPE_ID) {
            return imi.getSubtypeAt(subtypeId);
        }

        // If there are no selected subtypes, the framework will try to find the most applicable
        // subtype from explicitly or implicitly enabled subtypes.
        final List<InputMethodSubtype> subtypes = settings.getEnabledInputMethodSubtypeList(imi,
                true);
        if (subtypes.isEmpty()) {
            return currentSubtype;
        }
        // If there is only one explicitly or implicitly enabled subtype,
        // just returns it.
        if (subtypes.size() == 1) {
            return subtypes.get(0);
        }
        final String locale = SystemLocaleWrapper.get(userId).get(0).toString();
        final var subtype = findLastResortApplicableSubtype(subtypes, SUBTYPE_MODE_KEYBOARD, locale,
                true);
        if (subtype != null) {
            return subtype;
        }
        return findLastResortApplicableSubtype(subtypes, null, locale, true);
    }
}