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

Commit 13e67d43 authored by Yohei Yukawa's avatar Yohei Yukawa
Browse files

Introduce InputMethodUtils#concatEnabledImeIds()

This CL introduces a stateless utility method

  InputMethodUtils#concatEnabledImeIds(),

which can be used as a replacement of

  InputMethodSettings#appendAndPutEnabledInputMethodLocked()

so that we can later remove

  InputMethodSettings#mEnabledInputMethodsStrCache

to make InputMethodSettings a thin wrapper of mMethodMap.

This is a mechanical cleanup with a new unit test. There must be no
observable behavior change in tihs CL.

Bug: 309837937
Test: atest InputMethodUtilsTest#testConcatEnabledImeIds
Change-Id: I01d13d1f46a5af19d6db18246f74a20a4fe14168
parent 21f4b36f
Loading
Loading
Loading
Loading
+6 −4
Original line number Diff line number Diff line
@@ -6498,10 +6498,12 @@ public final class InputMethodManagerService extends IInputMethodManager.Stub
                                nextEnabledImes).getId();

                        // Reset enabled IMEs.
                        settings.putEnabledInputMethodsStr("");
                        nextEnabledImes.forEach(
                                imi -> settings.appendAndPutEnabledInputMethodLocked(
                                        imi.getId()));
                        final String[] nextEnabledImeIds = new String[nextEnabledImes.size()];
                        for (int i = 0; i < nextEnabledImeIds.length; ++i) {
                            nextEnabledImeIds[i] = nextEnabledImes.get(i).getId();
                        }
                        settings.putEnabledInputMethodsStr(InputMethodUtils.concatEnabledImeIds(
                                settings.getEnabledInputMethodsStr(), nextEnabledImeIds));

                        // Reset selected IME.
                        settings.putSelectedInputMethod(nextIme);
+31 −0
Original line number Diff line number Diff line
@@ -33,6 +33,7 @@ import android.os.UserHandle;
import android.provider.Settings;
import android.text.TextUtils;
import android.util.ArrayMap;
import android.util.ArraySet;
import android.util.IntArray;
import android.util.Pair;
import android.util.Printer;
@@ -50,6 +51,7 @@ import com.android.server.textservices.TextServicesManagerInternal;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;
import java.util.StringJoiner;
import java.util.function.Consumer;
import java.util.function.Predicate;

@@ -984,6 +986,35 @@ final class InputMethodUtils {
        }
    }

    /**
     * Concat given IME IDs with an existing enabled IME
     * ({@link Settings.Secure#ENABLED_INPUT_METHODS}).
     *
     * @param existingEnabledImeId an existing {@link Settings.Secure#ENABLED_INPUT_METHODS} to
     *                             which {@code imeIDs} will be added.
     * @param imeIds an array of IME IDs to be added. For IME IDs that are already seen in
     *               {@code existingEnabledImeId} will be skipped.
     * @return a new enabled IME ID string that can be stored in
     *         {@link Settings.Secure#ENABLED_INPUT_METHODS}.
     */
    @NonNull
    static String concatEnabledImeIds(@NonNull String existingEnabledImeId,
            @NonNull String... imeIds) {
        final ArraySet<String> alreadyEnabledIds = new ArraySet<>();
        final StringJoiner joiner = new StringJoiner(Character.toString(INPUT_METHOD_SEPARATOR));
        if (!TextUtils.isEmpty(existingEnabledImeId)) {
            splitEnabledImeStr(existingEnabledImeId, alreadyEnabledIds::add);
            joiner.add(existingEnabledImeId);
        }
        for (String id : imeIds) {
            if (!alreadyEnabledIds.contains(id)) {
                joiner.add(id);
                alreadyEnabledIds.add(id);
            }
        }
        return joiner.toString();
    }

    /**
     * Convert the input method ID to a component name
     *
+22 −0
Original line number Diff line number Diff line
@@ -1364,6 +1364,28 @@ public class InputMethodUtilsTest {
                "com.android/.ime1", "com.android/.ime2", "com.android/.ime3");
    }

    @Test
    public void testConcatEnabledImeIds() {
        Truth.assertThat(InputMethodUtils.concatEnabledImeIds("")).isEmpty();
        Truth.assertThat(InputMethodUtils.concatEnabledImeIds("", "com.android/.ime1"))
                .isEqualTo("com.android/.ime1");
        Truth.assertThat(InputMethodUtils.concatEnabledImeIds(
                        "com.android/.ime1", "com.android/.ime1"))
                .isEqualTo("com.android/.ime1");
        Truth.assertThat(InputMethodUtils.concatEnabledImeIds(
                        "com.android/.ime1", "com.android/.ime2"))
                .isEqualTo("com.android/.ime1:com.android/.ime2");
        Truth.assertThat(InputMethodUtils.concatEnabledImeIds(
                        "com.android/.ime1", "com.android/.ime2", "com.android/.ime3"))
                .isEqualTo("com.android/.ime1:com.android/.ime2:com.android/.ime3");
        Truth.assertThat(InputMethodUtils.concatEnabledImeIds(
                        "com.android/.ime1:com.android/.ime2", "com.android/.ime1"))
                .isEqualTo("com.android/.ime1:com.android/.ime2");
        Truth.assertThat(InputMethodUtils.concatEnabledImeIds(
                        "com.android/.ime1:com.android/.ime2", "com.android/.ime3"))
                .isEqualTo("com.android/.ime1:com.android/.ime2:com.android/.ime3");
    }

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