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

Commit 34388c1c authored by Antonio Kantek's avatar Antonio Kantek
Browse files

Close opened streams in inputmethod

Closing streams + removing some unused code.

Bug: 238470908
Test: atest CtsInputMethodTestCases InputMethodUtilTests
Change-Id: I69059bb31baa64935e52f41c06ef01b778ae9abd
parent 0a0cb46b
Loading
Loading
Loading
Loading
+9 −5
Original line number Diff line number Diff line
@@ -31,6 +31,8 @@ import android.util.Xml;
import android.view.inputmethod.InputMethodInfo;
import android.view.inputmethod.InputMethodSubtype;

import libcore.io.IoUtils;

import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;

@@ -172,11 +174,13 @@ final class AdditionalSubtypeUtils {
            out.endTag(null, NODE_SUBTYPES);
            out.endDocument();
            subtypesFile.finishWrite(fos);
        } catch (java.io.IOException e) {
        } catch (IOException e) {
            Slog.w(TAG, "Error writing subtypes", e);
            if (fos != null) {
                subtypesFile.failWrite(fos);
            }
        } finally {
            IoUtils.closeQuietly(fos);
        }
    }

+140 −133
Original line number Diff line number Diff line
@@ -3169,7 +3169,7 @@ public final class InputMethodManagerService extends IInputMethodManager.Stub
        }

        // TODO: Move this clearing calling identity block to setImeWindowStatus after making sure
        // all updateSystemUi happens on system previlege.
        // all updateSystemUi happens on system privilege.
        final long ident = Binder.clearCallingIdentity();
        try {
            if (!mCurPerceptible) {
@@ -4180,10 +4180,7 @@ public final class InputMethodManagerService extends IInputMethodManager.Stub
            final ImeSubtypeListItem nextSubtype = mSwitchingController.getNextInputMethodLocked(
                    false /* onlyCurrentIme */, mMethodMap.get(getSelectedMethodIdLocked()),
                    mCurrentSubtype);
            if (nextSubtype == null) {
                return false;
            }
            return true;
            return nextSubtype != null;
        }
    }

@@ -6110,9 +6107,9 @@ public final class InputMethodManagerService extends IInputMethodManager.Stub
            }
        }
        synchronized (ImfLock.class) {
            final PrintWriter pr = shellCommand.getOutPrintWriter();
            final int[] userIds = InputMethodUtils.resolveUserId(userIdToBeResolved,
                    mSettings.getCurrentUserId(), shellCommand.getErrPrintWriter());
            try (PrintWriter pr = shellCommand.getOutPrintWriter()) {
                for (int userId : userIds) {
                    final List<InputMethodInfo> methods = all
                            ? getInputMethodListLocked(userId, DirectBootAwareness.AUTO)
@@ -6133,11 +6130,13 @@ public final class InputMethodManagerService extends IInputMethodManager.Stub
                    }
                }
            }
        }
        return ShellCommandResult.SUCCESS;
    }

    /**
     * Handles {@code adb shell ime enable} and {@code adb shell ime disable}.
     *
     * @param shellCommand {@link ShellCommand} object that is handling this command.
     * @param enabled      {@code true} if the command was {@code adb shell ime enable}.
     * @return Exit code of the command.
@@ -6148,9 +6147,9 @@ public final class InputMethodManagerService extends IInputMethodManager.Stub
            @NonNull ShellCommand shellCommand, boolean enabled) {
        final int userIdToBeResolved = handleOptionsForCommandsThatOnlyHaveUserOption(shellCommand);
        final String imeId = shellCommand.getNextArgRequired();
        final PrintWriter out = shellCommand.getOutPrintWriter();
        final PrintWriter error = shellCommand.getErrPrintWriter();
        boolean hasFailed = false;
        try (PrintWriter out = shellCommand.getOutPrintWriter();
             PrintWriter error = shellCommand.getErrPrintWriter()) {
            synchronized (ImfLock.class) {
                final int[] userIds = InputMethodUtils.resolveUserId(userIdToBeResolved,
                        mSettings.getCurrentUserId(), shellCommand.getErrPrintWriter());
@@ -6162,6 +6161,7 @@ public final class InputMethodManagerService extends IInputMethodManager.Stub
                            userId, imeId, enabled, out, error);
                }
            }
        }
        return hasFailed ? ShellCommandResult.FAILURE : ShellCommandResult.SUCCESS;
    }

@@ -6251,7 +6251,6 @@ public final class InputMethodManagerService extends IInputMethodManager.Stub
                    + " failed due to its unrecognized IME ID.");
            return false;
        }

        out.print("Input method ");
        out.print(imeId);
        out.print(": ");
@@ -6264,6 +6263,7 @@ public final class InputMethodManagerService extends IInputMethodManager.Stub

    /**
     * Handles {@code adb shell ime set}.
     *
     * @param shellCommand {@link ShellCommand} object that is handling this command.
     * @return Exit code of the command.
     */
@@ -6272,9 +6272,9 @@ public final class InputMethodManagerService extends IInputMethodManager.Stub
    private int handleShellCommandSetInputMethod(@NonNull ShellCommand shellCommand) {
        final int userIdToBeResolved = handleOptionsForCommandsThatOnlyHaveUserOption(shellCommand);
        final String imeId = shellCommand.getNextArgRequired();
        final PrintWriter out = shellCommand.getOutPrintWriter();
        final PrintWriter error = shellCommand.getErrPrintWriter();
        boolean hasFailed = false;
        try (PrintWriter out = shellCommand.getOutPrintWriter();
             PrintWriter error = shellCommand.getErrPrintWriter()) {
            synchronized (ImfLock.class) {
                final int[] userIds = InputMethodUtils.resolveUserId(userIdToBeResolved,
                        mSettings.getCurrentUserId(), shellCommand.getErrPrintWriter());
@@ -6282,7 +6282,8 @@ public final class InputMethodManagerService extends IInputMethodManager.Stub
                    if (!userHasDebugPriv(userId, shellCommand)) {
                        continue;
                    }
                boolean failedToSelectUnknownIme = !switchToInputMethodLocked(imeId, userId);
                    boolean failedToSelectUnknownIme = !switchToInputMethodLocked(imeId,
                            userId);
                    if (failedToSelectUnknownIme) {
                        error.print("Unknown input method ");
                        error.print(imeId);
@@ -6300,6 +6301,7 @@ public final class InputMethodManagerService extends IInputMethodManager.Stub
                    hasFailed |= failedToSelectUnknownIme;
                }
            }
        }
        return hasFailed ? ShellCommandResult.FAILURE : ShellCommandResult.SUCCESS;
    }

@@ -6311,9 +6313,9 @@ public final class InputMethodManagerService extends IInputMethodManager.Stub
    @BinderThread
    @ShellCommandResult
    private int handleShellCommandResetInputMethod(@NonNull ShellCommand shellCommand) {
        final PrintWriter out = shellCommand.getOutPrintWriter();
        final int userIdToBeResolved = handleOptionsForCommandsThatOnlyHaveUserOption(shellCommand);
        synchronized (ImfLock.class) {
            try (PrintWriter out = shellCommand.getOutPrintWriter()) {
                final int[] userIds = InputMethodUtils.resolveUserId(userIdToBeResolved,
                        mSettings.getCurrentUserId(), shellCommand.getErrPrintWriter());
                for (int userId : userIds) {
@@ -6365,7 +6367,8 @@ public final class InputMethodManagerService extends IInputMethodManager.Stub

                        // Reset enabled IMEs.
                        settings.putEnabledInputMethodsStr("");
                    nextEnabledImes.forEach(imi -> settings.appendAndPutEnabledInputMethodLocked(
                        nextEnabledImes.forEach(
                                imi -> settings.appendAndPutEnabledInputMethodLocked(
                                        imi.getId(), false));

                        // Reset selected IME.
@@ -6377,6 +6380,7 @@ public final class InputMethodManagerService extends IInputMethodManager.Stub
                    nextEnabledImes.forEach(ime -> out.println("   Enabled: " + ime.getId()));
                }
            }
        }
        return ShellCommandResult.SUCCESS;
    }

@@ -6389,7 +6393,7 @@ public final class InputMethodManagerService extends IInputMethodManager.Stub
    @ShellCommandResult
    private int handleShellCommandTraceInputMethod(@NonNull ShellCommand shellCommand) {
        final String cmd = shellCommand.getNextArgRequired();
        final PrintWriter pw = shellCommand.getOutPrintWriter();
        try (PrintWriter pw = shellCommand.getOutPrintWriter()) {
            switch (cmd) {
                case "start":
                    ImeTracing.getInstance().startTrace(pw);
@@ -6399,13 +6403,16 @@ public final class InputMethodManagerService extends IInputMethodManager.Stub
                    break;  // proceed to the next step to update the IME client processes.
                case "save-for-bugreport":
                    ImeTracing.getInstance().saveForBugreport(pw);
                return ShellCommandResult.SUCCESS;  // no need to update the IME client processes.
                    // no need to update the IME client processes.
                    return ShellCommandResult.SUCCESS;
                default:
                    pw.println("Unknown command: " + cmd);
                    pw.println("Input method trace options:");
                    pw.println("  start: Start tracing");
                    pw.println("  stop: Stop tracing");
                return ShellCommandResult.FAILURE;  // no need to update the IME client processes.
                    // no need to update the IME client processes.
                    return ShellCommandResult.FAILURE;
            }
        }
        boolean isImeTraceEnabled = ImeTracing.getInstance().isEnabled();
        ArrayMap<IBinder, ClientState> clients;
+0 −6
Original line number Diff line number Diff line
@@ -199,9 +199,6 @@ public class InputMethodUtilsTest {
        final InputMethodSubtype nonAutoEnGB = createFakeInputMethodSubtype("en_GB",
                SUBTYPE_MODE_KEYBOARD, !IS_AUX, !IS_OVERRIDES_IMPLICITLY_ENABLED_SUBTYPE,
                IS_ASCII_CAPABLE, IS_ENABLED_WHEN_DEFAULT_IS_NOT_ASCII_CAPABLE);
        final InputMethodSubtype nonAutoEnIN = createFakeInputMethodSubtype("en_IN",
                SUBTYPE_MODE_KEYBOARD, !IS_AUX, !IS_OVERRIDES_IMPLICITLY_ENABLED_SUBTYPE,
                IS_ASCII_CAPABLE, IS_ENABLED_WHEN_DEFAULT_IS_NOT_ASCII_CAPABLE);
        final InputMethodSubtype nonAutoFrCA = createFakeInputMethodSubtype("fr_CA",
                SUBTYPE_MODE_KEYBOARD, !IS_AUX, !IS_OVERRIDES_IMPLICITLY_ENABLED_SUBTYPE,
                IS_ASCII_CAPABLE, IS_ENABLED_WHEN_DEFAULT_IS_NOT_ASCII_CAPABLE);
@@ -656,9 +653,6 @@ public class InputMethodUtilsTest {
        final InputMethodSubtype nonAutoEnUS = createFakeInputMethodSubtype("en_US",
                SUBTYPE_MODE_KEYBOARD, !IS_AUX, !IS_OVERRIDES_IMPLICITLY_ENABLED_SUBTYPE,
                IS_ASCII_CAPABLE, !IS_ENABLED_WHEN_DEFAULT_IS_NOT_ASCII_CAPABLE);
        final InputMethodSubtype nonAutoEnGB = createFakeInputMethodSubtype("en_GB",
                SUBTYPE_MODE_KEYBOARD, !IS_AUX, !IS_OVERRIDES_IMPLICITLY_ENABLED_SUBTYPE,
                IS_ASCII_CAPABLE, IS_ENABLED_WHEN_DEFAULT_IS_NOT_ASCII_CAPABLE);
        final InputMethodSubtype nonAutoFil = createFakeInputMethodSubtype("fil",
                SUBTYPE_MODE_KEYBOARD, !IS_AUX, !IS_OVERRIDES_IMPLICITLY_ENABLED_SUBTYPE,
                IS_ASCII_CAPABLE, !IS_ENABLED_WHEN_DEFAULT_IS_NOT_ASCII_CAPABLE);