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

Commit 21f4b36f authored by Yohei Yukawa's avatar Yohei Yukawa
Browse files

Introduce InputMethodUtils#splitEnabledImeStr()

This is a follow up CL to my previous CL, which added

  InputMethodUtils#getEnabledInputMethodIdsForFiltering() [1]

as an internal utility method.  This CL extracts out its main logic as

  InputMethodUtils#splitEnabledImeStr()

with a unit test so that we can reuse the same functionality in
subsequent CLs.

This is a mechanical cleanup. There must be no observable behavior
change.

 [1]: Iac98fdbb2758f4d9c68930f49d219eb83e54a3d0
      6c984cca

Bug: 261723412
Bug: 309837937
Test: atest InputMethodUtilsTest#testSplitEnabledImeStr
Change-Id: I503c0023b62bec949d1681204cc1ad4481675b00
parent c9856234
Loading
Loading
Loading
Loading
+18 −6
Original line number Diff line number Diff line
@@ -50,6 +50,7 @@ import com.android.server.textservices.TextServicesManagerInternal;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;
import java.util.function.Consumer;
import java.util.function.Predicate;

/**
@@ -952,24 +953,35 @@ final class InputMethodUtils {
        final String enabledInputMethodsStr = TextUtils.nullIfEmpty(
                SecureSettingsWrapper.getString(Settings.Secure.ENABLED_INPUT_METHODS, null,
                        userId));
        if (enabledInputMethodsStr == null) {
            return List.of();
        final ArrayList<String> result = new ArrayList<>();
        splitEnabledImeStr(enabledInputMethodsStr, result::add);
        return result;
    }

    /**
     * Split enabled IME string ({@link Settings.Secure#ENABLED_INPUT_METHODS}) into IME IDs.
     *
     * @param text a text formatted with {@link Settings.Secure#ENABLED_INPUT_METHODS}.
     * @param consumer {@link Consumer} called back when a new IME ID is found.
     */
    @VisibleForTesting(visibility = VisibleForTesting.Visibility.PRIVATE)
    static void splitEnabledImeStr(@Nullable String text, @NonNull Consumer<String> consumer) {
        if (TextUtils.isEmpty(text)) {
            return;
        }
        final TextUtils.SimpleStringSplitter inputMethodSplitter =
                new TextUtils.SimpleStringSplitter(INPUT_METHOD_SEPARATOR);
        final TextUtils.SimpleStringSplitter subtypeSplitter =
                new TextUtils.SimpleStringSplitter(INPUT_METHOD_SUBTYPE_SEPARATOR);
        inputMethodSplitter.setString(enabledInputMethodsStr);
        final ArrayList<String> result = new ArrayList<>();
        inputMethodSplitter.setString(text);
        while (inputMethodSplitter.hasNext()) {
            String nextImsStr = inputMethodSplitter.next();
            subtypeSplitter.setString(nextImsStr);
            if (subtypeSplitter.hasNext()) {
                // The first element is ime id.
                result.add(subtypeSplitter.next());
                consumer.accept(subtypeSplitter.next());
            }
        }
        return result;
    }

    /**
+28 −0
Original line number Diff line number Diff line
@@ -60,6 +60,8 @@ import androidx.test.runner.AndroidJUnit4;

import com.android.internal.inputmethod.StartInputFlags;

import com.google.common.truth.Truth;

import org.junit.Test;
import org.junit.runner.RunWith;

@@ -1336,6 +1338,32 @@ public class InputMethodUtilsTest {
        }
    }

    private static void verifySplitEnabledImeStr(@NonNull String enabledImeStr,
            @NonNull String... expected) {
        final ArrayList<String> actual = new ArrayList<>();
        InputMethodUtils.splitEnabledImeStr(enabledImeStr, actual::add);
        if (expected.length == 0) {
            Truth.assertThat(actual).isEmpty();
        } else {
            Truth.assertThat(actual).containsExactlyElementsIn(expected);
        }
    }

    @Test
    public void testSplitEnabledImeStr() {
        verifySplitEnabledImeStr("");
        verifySplitEnabledImeStr("com.android/.ime1", "com.android/.ime1");
        verifySplitEnabledImeStr("com.android/.ime1;1;2;3", "com.android/.ime1");
        verifySplitEnabledImeStr("com.android/.ime1;1;2;3:com.android/.ime2",
                "com.android/.ime1", "com.android/.ime2");
        verifySplitEnabledImeStr("com.android/.ime1:com.android/.ime2",
                "com.android/.ime1", "com.android/.ime2");
        verifySplitEnabledImeStr("com.android/.ime1:com.android/.ime2:com.android/.ime3",
                "com.android/.ime1", "com.android/.ime2", "com.android/.ime3");
        verifySplitEnabledImeStr("com.android/.ime1;1:com.android/.ime2;1:com.android/.ime3;1",
                "com.android/.ime1", "com.android/.ime2", "com.android/.ime3");
    }

    @Test
    public void updateEnabledImeStringTest() {
        // No change cases