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

Commit 75a92392 authored by Tarandeep Singh's avatar Tarandeep Singh
Browse files

Optimize calls to IME's onStartInput

As of today onStartInput is called for every window which gains focus. We
can significantly optimize this by restricting calls only to windows
that have a text editor. However, if input was already started based on
window flags, behavior is unchanged.

This behavior is guarded by debug flag "debug.optimize_startinput" and
is defaulted to true.

Bug: 37617707
Test: atest CtsInputMethodTestCases
Test: Verify following command reverts the behavior: adb root && adb
      shell setprop debug.optimize_startinput false &&
      adb shell cmd input_method refresh_debug_options
Change-Id: Ibf9dab3d9c138b5f04e053d41ee4fd248c78e4da
parent 4c391968
Loading
Loading
Loading
Loading
+55 −2
Original line number Diff line number Diff line
@@ -51,6 +51,7 @@ import org.xmlpull.v1.XmlPullParserException;
import org.xmlpull.v1.XmlSerializer;

import android.Manifest;
import android.annotation.AnyThread;
import android.annotation.BinderThread;
import android.annotation.ColorInt;
import android.annotation.IntDef;
@@ -110,6 +111,7 @@ import android.os.ServiceManager;
import android.os.ShellCallback;
import android.os.ShellCommand;
import android.os.SystemClock;
import android.os.SystemProperties;
import android.os.UserHandle;
import android.os.UserManager;
import android.provider.Settings;
@@ -256,6 +258,44 @@ public class InputMethodManagerService extends IInputMethodManager.Stub
    private static final String ACTION_SHOW_INPUT_METHOD_PICKER =
            "com.android.server.InputMethodManagerService.SHOW_INPUT_METHOD_PICKER";

    /**
     * Debug flag for overriding runtime {@link SystemProperties}.
     */
    @AnyThread
    private static final class DebugFlag {
        private static final Object LOCK = new Object();
        private final String mKey;
        @GuardedBy("LOCK")
        private boolean mValue;

        public DebugFlag(String key) {
            mKey = key;
            refresh();
        }

        void refresh() {
            synchronized (LOCK) {
                mValue = SystemProperties.getBoolean(mKey, true);
            }
        }

        boolean value() {
            synchronized (LOCK) {
                return mValue;
            }
        }
    }

    /**
     * Debug flags that can be overridden using "adb shell setprop <key>"
     * Note: These flags are cached. To refresh, run "adb shell ime refresh_debug_properties".
     */
    private static final class DebugFlags {
        static final DebugFlag FLAG_OPTIMIZE_START_INPUT =
                new DebugFlag("debug.optimize_startinput");
    }


    final Context mContext;
    final Resources mRes;
    final Handler mHandler;
@@ -2930,10 +2970,14 @@ public class InputMethodManagerService extends IInputMethodManager.Stub
                }

                if (!didStart && attribute != null) {
                    if (!DebugFlags.FLAG_OPTIMIZE_START_INPUT.value()
                            || (controlFlags
                                    & InputMethodManager.CONTROL_WINDOW_IS_TEXT_EDITOR) != 0) {
                        res = startInputUncheckedLocked(cs, inputContext, missingMethods, attribute,
                                controlFlags, startInputReason);
                    }
                }
            }
        } finally {
            Binder.restoreCallingIdentity(ident);
        }
@@ -4703,6 +4747,8 @@ public class InputMethodManagerService extends IInputMethodManager.Stub
                        return mService.handleShellCommandSetInputMethod(this);
                    case "reset":
                        return mService.handleShellCommandResetInputMethod(this);
                    case "refresh_debug_properties":
                        return refreshDebugProperties();
                    default:
                        getOutPrintWriter().println("Unknown command: " + imeCommand);
                        return ShellCommandResult.FAILURE;
@@ -4712,6 +4758,13 @@ public class InputMethodManagerService extends IInputMethodManager.Stub
            return handleDefaultCommands(cmd);
        }

        @BinderThread
        @ShellCommandResult
        private int refreshDebugProperties() {
            DebugFlags.FLAG_OPTIMIZE_START_INPUT.refresh();
            return ShellCommandResult.SUCCESS;
        }

        @BinderThread
        @Override
        public void onHelp() {