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

Commit 5e2d9f27 authored by Wilson Wu's avatar Wilson Wu
Browse files

Async startInputOrWindowGainedFocus

We ignore the result if startInputReason is
WINDOW_FOCUS_GAIN_REPORT_WITH_CONNECTION or
WINDOW_FOCUS_GAIN_REPORT_WITHOUT_CONNECTION
in startInputOrWindowGainedFocus.

Introduce USE_REPORT_WINDOW_GAINED_FOCUS_ASYNC
flag to control following behavior:

-. Don't wait the result if it only report the
   focus gain. Have another IPC for this case
   to prevent the UI thread be blocked.

-. Throw the exception to the client side if it
   happened.

Bug: 185928120
Test: atest CtsInputMethodTestCases
Change-Id: Ic584203c1221fbae17f5e2d8f09e3992df061646
parent f98e690f
Loading
Loading
Loading
Loading
+36 −14
Original line number Diff line number Diff line
@@ -265,6 +265,14 @@ public final class InputMethodManager {

    private static final int NOT_A_SUBTYPE_ID = -1;

    /**
     * {@code true} to try to avoid blocking apps' UI thread by sending
     * {@link StartInputReason#WINDOW_FOCUS_GAIN_REPORT_WITH_CONNECTION} and
     * {@link StartInputReason#WINDOW_FOCUS_GAIN_REPORT_WITHOUT_CONNECTION} in a truly asynchronous
     * way. {@code false} to go back to the previous synchronous semantics.
     */
    private static final boolean USE_REPORT_WINDOW_GAINED_FOCUS_ASYNC = false;

    /**
     * A constant that represents Voice IME.
     *
@@ -689,10 +697,18 @@ public final class InputMethodManager {
                        Log.v(TAG, "Reporting focus gain, without startInput"
                                + ", nextFocusIsServedView=" + nextFocusHasConnection);
                    }
                    final int startInputReason =
                            nextFocusHasConnection ? WINDOW_FOCUS_GAIN_REPORT_WITH_CONNECTION

                    if (USE_REPORT_WINDOW_GAINED_FOCUS_ASYNC) {
                        mService.reportWindowGainedFocusAsync(
                                nextFocusHasConnection, mClient, focusedView.getWindowToken(),
                                startInputFlags, softInputMode, windowFlags,
                                mCurRootView.mContext.getApplicationInfo().targetSdkVersion);
                    } else {
                        final int startInputReason = nextFocusHasConnection
                                ? WINDOW_FOCUS_GAIN_REPORT_WITH_CONNECTION
                                : WINDOW_FOCUS_GAIN_REPORT_WITHOUT_CONNECTION;
                    final Completable.InputBindResult value = Completable.createInputBindResult();
                        final Completable.InputBindResult value =
                                Completable.createInputBindResult();
                        mService.startInputOrWindowGainedFocus(
                                startInputReason, mClient,
                                focusedView.getWindowToken(), startInputFlags, softInputMode,
@@ -703,6 +719,7 @@ public final class InputMethodManager {
                                mCurRootView.mContext.getApplicationInfo().targetSdkVersion,
                                ResultCallbacks.of(value));
                        Completable.getResult(value); // ignore the result
                    }
                } catch (RemoteException e) {
                    throw e.rethrowFromSystemServer();
                }
@@ -1087,6 +1104,11 @@ public final class InputMethodManager {
        public void setImeTraceEnabled(boolean enabled) {
            ImeTracing.getInstance().setEnabled(enabled);
        }

        @Override
        public void throwExceptionFromSystem(String message) {
            throw new RuntimeException(message);
        }
    };

    final InputConnection mDummyInputConnection = new BaseInputConnection(this, false);
+1 −0
Original line number Diff line number Diff line
@@ -30,4 +30,5 @@ oneway interface IInputMethodClient {
    void reportFullscreenMode(boolean fullscreen);
    void updateActivityViewToScreenMatrix(int bindSequence, in float[] matrixValues);
    void setImeTraceEnabled(boolean enabled);
    void throwExceptionFromSystem(String message);
}
+6 −0
Original line number Diff line number Diff line
@@ -68,6 +68,12 @@ interface IInputMethodManager {
            int unverifiedTargetSdkVersion,
            in IInputBindResultResultCallback inputBindResult);

    oneway void reportWindowGainedFocusAsync(
            boolean nextFocusHasConnection, in IInputMethodClient client, in IBinder windowToken,
            /* @StartInputFlags */ int startInputFlags,
            /* @android.view.WindowManager.LayoutParams.SoftInputModeFlags */ int softInputMode,
            int windowFlags, int unverifiedTargetSdkVersion);

    oneway void showInputMethodPickerFromClient(in IInputMethodClient client,
            int auxiliarySubtypeMode, in IVoidResultCallback resultCallback);
    oneway void showInputMethodPickerFromSystem(in IInputMethodClient client,
+85 −53
Original line number Diff line number Diff line
@@ -3346,6 +3346,28 @@ public class InputMethodManagerService extends IInputMethodManager.Stub
        return res;
    }

    @NonNull
    @Override
    public void reportWindowGainedFocusAsync(
            boolean nextFocusHasConnection, IInputMethodClient client, IBinder windowToken,
            @StartInputFlags int startInputFlags, @SoftInputModeFlags int softInputMode,
            int windowFlags, int unverifiedTargetSdkVersion) {
        final int startInputReason = nextFocusHasConnection
                ? StartInputReason.WINDOW_FOCUS_GAIN_REPORT_WITH_CONNECTION
                : StartInputReason.WINDOW_FOCUS_GAIN_REPORT_WITHOUT_CONNECTION;
        try {
            startInputOrWindowGainedFocusInternal(startInputReason, client, windowToken,
                    startInputFlags, softInputMode, windowFlags, null /* attribute */,
                    null /* inputContext */, 0 /* missingMethods */, unverifiedTargetSdkVersion);
        } catch (Throwable t) {
            if (client != null) {
                try {
                    client.throwExceptionFromSystem(t.getMessage());
                } catch (RemoteException ignore) { }
            }
        }
    }

    @NonNull
    @Override
    public void startInputOrWindowGainedFocus(
@@ -3354,7 +3376,18 @@ public class InputMethodManagerService extends IInputMethodManager.Stub
            int windowFlags, @Nullable EditorInfo attribute, IInputContext inputContext,
            @MissingMethodFlags int missingMethods, int unverifiedTargetSdkVersion,
            IInputBindResultResultCallback resultCallback) {
        CallbackUtils.onResult(resultCallback, (Supplier<InputBindResult>) () -> {
        CallbackUtils.onResult(resultCallback, (Supplier<InputBindResult>) () ->
                startInputOrWindowGainedFocusInternal(startInputReason, client, windowToken,
                startInputFlags, softInputMode, windowFlags, attribute, inputContext,
                missingMethods, unverifiedTargetSdkVersion));
    }

    @NonNull
    private InputBindResult startInputOrWindowGainedFocusInternal(
            @StartInputReason int startInputReason, IInputMethodClient client, IBinder windowToken,
            @StartInputFlags int startInputFlags, @SoftInputModeFlags int softInputMode,
            int windowFlags, @Nullable EditorInfo attribute, @Nullable IInputContext inputContext,
            @MissingMethodFlags int missingMethods, int unverifiedTargetSdkVersion) {
        if (windowToken == null) {
            Slog.e(TAG, "windowToken cannot be null.");
            return InputBindResult.NULL;
@@ -3409,7 +3442,6 @@ public class InputMethodManagerService extends IInputMethodManager.Stub
        } finally {
            Trace.traceEnd(TRACE_TAG_WINDOW_MANAGER);
        }
        });
    }

    @NonNull
+29 −2
Original line number Diff line number Diff line
@@ -1621,6 +1621,33 @@ public final class MultiClientInputMethodManagerService {
            }
        }

        @BinderThread
        @Override
        public void reportWindowGainedFocusAsync(
                boolean nextFocusHasConnection,
                @Nullable IInputMethodClient client,
                @Nullable IBinder windowToken,
                @StartInputFlags int startInputFlags,
                @SoftInputModeFlags int softInputMode,
                int windowFlags,
                int unverifiedTargetSdkVersion) {
            final int startInputReason = nextFocusHasConnection
                    ? StartInputReason.WINDOW_FOCUS_GAIN_REPORT_WITH_CONNECTION
                    : StartInputReason.WINDOW_FOCUS_GAIN_REPORT_WITHOUT_CONNECTION;
            try {
                startInputOrWindowGainedFocusInternal(startInputReason, client, windowToken,
                        startInputFlags, softInputMode, windowFlags, null /* editorInfo */,
                        null /* inputContext */, 0 /* missingMethods */,
                        unverifiedTargetSdkVersion);
            } catch (Throwable t) {
                if (client != null) {
                    try {
                        client.throwExceptionFromSystem(t.getMessage());
                    } catch (RemoteException ignore) { }
                }
            }
        }

        @BinderThread
        @Override
        public void startInputOrWindowGainedFocus(