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

Commit 44c672ed authored by Yohei Yukawa's avatar Yohei Yukawa Committed by Android (Google) Code Review
Browse files

Merge "Make IAccessibilityInputMethodSession async even in in-proc cases"

parents 970df1db f11999b7
Loading
Loading
Loading
Loading
+70 −2
Original line number Diff line number Diff line
@@ -19,9 +19,13 @@ package android.view.inputmethod;
import android.annotation.AnyThread;
import android.annotation.NonNull;
import android.annotation.Nullable;
import android.os.Binder;
import android.os.Handler;
import android.os.HandlerThread;
import android.os.RemoteException;
import android.util.Log;

import com.android.internal.annotations.GuardedBy;
import com.android.internal.inputmethod.IAccessibilityInputMethodSession;
import com.android.internal.inputmethod.IRemoteAccessibilityInputConnection;

@@ -34,9 +38,26 @@ final class IAccessibilityInputMethodSessionInvoker {
    @NonNull
    private final IAccessibilityInputMethodSession mSession;

    /**
     * An optional {@link Handler} to dispatch {@link IAccessibilityInputMethodSession} method
     * invocations to a background thread to emulate async (one-way) {@link Binder} call.
     *
     * {@code null} if {@code Binder.isProxy(mSession)} is {@code true}.
     */
    @Nullable
    private final Handler mCustomHandler;

    private static final Object sAsyncBinderEmulationHandlerLock = new Object();

    @GuardedBy("sAsyncBinderEmulationHandlerLock")
    @Nullable
    private static Handler sAsyncBinderEmulationHandler;

    private IAccessibilityInputMethodSessionInvoker(
            @NonNull IAccessibilityInputMethodSession session) {
            @NonNull IAccessibilityInputMethodSession session,
            @Nullable Handler customHandler) {
        mSession = session;
        mCustomHandler = customHandler;
    }

    /**
@@ -49,11 +70,36 @@ final class IAccessibilityInputMethodSessionInvoker {
    @Nullable
    public static IAccessibilityInputMethodSessionInvoker createOrNull(
            @NonNull IAccessibilityInputMethodSession session) {
        return session == null ? null : new IAccessibilityInputMethodSessionInvoker(session);
        final Handler customHandler;
        if (session != null && !Binder.isProxy(session)) {
            synchronized (sAsyncBinderEmulationHandlerLock) {
                if (sAsyncBinderEmulationHandler == null) {
                    final HandlerThread thread = new HandlerThread("IMM.IAIMS");
                    thread.start();
                    // Use an async handler instead of Handler#getThreadHandler().
                    sAsyncBinderEmulationHandler = Handler.createAsync(thread.getLooper());
                }
                customHandler = sAsyncBinderEmulationHandler;
            }
        } else {
            customHandler = null;
        }

        return session == null
                ? null : new IAccessibilityInputMethodSessionInvoker(session, customHandler);
    }

    @AnyThread
    void finishInput() {
        if (mCustomHandler == null) {
            finishInputInternal();
        } else {
            mCustomHandler.post(this::finishInputInternal);
        }
    }

    @AnyThread
    private void finishInputInternal() {
        try {
            mSession.finishInput();
        } catch (RemoteException e) {
@@ -64,6 +110,18 @@ final class IAccessibilityInputMethodSessionInvoker {
    @AnyThread
    void updateSelection(int oldSelStart, int oldSelEnd, int selStart, int selEnd,
            int candidatesStart, int candidatesEnd) {
        if (mCustomHandler == null) {
            updateSelectionInternal(
                    oldSelStart, oldSelEnd, selStart, selEnd, candidatesStart, candidatesEnd);
        } else {
            mCustomHandler.post(() -> updateSelectionInternal(
                    oldSelStart, oldSelEnd, selStart, selEnd, candidatesStart, candidatesEnd));
        }
    }

    @AnyThread
    private void updateSelectionInternal(int oldSelStart, int oldSelEnd, int selStart, int selEnd,
            int candidatesStart, int candidatesEnd) {
        try {
            mSession.updateSelection(
                    oldSelStart, oldSelEnd, selStart, selEnd, candidatesStart, candidatesEnd);
@@ -75,6 +133,16 @@ final class IAccessibilityInputMethodSessionInvoker {
    @AnyThread
    void invalidateInput(EditorInfo editorInfo, IRemoteAccessibilityInputConnection connection,
            int sessionId) {
        if (mCustomHandler == null) {
            invalidateInputInternal(editorInfo, connection, sessionId);
        } else {
            mCustomHandler.post(() -> invalidateInputInternal(editorInfo, connection, sessionId));
        }
    }

    @AnyThread
    private void invalidateInputInternal(EditorInfo editorInfo,
            IRemoteAccessibilityInputConnection connection, int sessionId) {
        try {
            mSession.invalidateInput(editorInfo, connection, sessionId);
        } catch (RemoteException e) {