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

Commit 145a2e58 authored by Taran Singh's avatar Taran Singh
Browse files

Use InputManagerInternal to report last toolType

In the past we've explored various ways to pipe last used tooltype,
however most effective way is to ask InputManager. This makes sure it is
always up-to-date and isn't restricted to a ViewRoot.

Fix: 336615195
Test: atest StylusHandwritingTest

Change-Id: I17b9d7d13e2fa265c0f9b04f1cf7193a1ce86638
parent d891a643
Loading
Loading
Loading
Loading
+2 −11
Original line number Diff line number Diff line
@@ -55,7 +55,6 @@ import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.OptionalInt;
import java.util.function.IntConsumer;

// TODO(b/210039666): See if we can make this class thread-safe.
final class HandwritingModeController {
@@ -91,7 +90,6 @@ final class HandwritingModeController {
    private boolean mDelegationConnectionlessFlow;
    private Runnable mDelegationIdleTimeoutRunnable;
    private Handler mDelegationIdleTimeoutHandler;
    private IntConsumer mPointerToolTypeConsumer;
    private final Runnable mDiscardDelegationTextRunnable;
    private HandwritingEventReceiverSurface mHandwritingSurface;

@@ -99,7 +97,7 @@ final class HandwritingModeController {

    @AnyThread
    HandwritingModeController(Context context, Looper uiThreadLooper,
            Runnable inkWindowInitRunnable, IntConsumer toolTypeConsumer,
            Runnable inkWindowInitRunnable,
            Runnable discardDelegationTextRunnable) {
        mContext = context;
        mLooper = uiThreadLooper;
@@ -109,7 +107,6 @@ final class HandwritingModeController {
        mPackageManagerInternal = LocalServices.getService(PackageManagerInternal.class);
        mCurrentRequestId = 0;
        mInkWindowInitRunnable = inkWindowInitRunnable;
        mPointerToolTypeConsumer = toolTypeConsumer;
        mDiscardDelegationTextRunnable = discardDelegationTextRunnable;
    }

@@ -389,16 +386,10 @@ final class HandwritingModeController {
                    "Input Event should not be processed when IME has the spy channel.");
        }

        if (!(ev instanceof MotionEvent)) {
        if (!(ev instanceof MotionEvent event)) {
            Slog.wtf(TAG, "Received non-motion event in stylus monitor.");
            return false;
        }
        final MotionEvent event = (MotionEvent) ev;
        if (mPointerToolTypeConsumer != null && event.getAction() == MotionEvent.ACTION_DOWN) {
            int toolType = event.getToolType(event.getActionIndex());
            // notify IME of change in tool type.
            mPointerToolTypeConsumer.accept(toolType);
        }
        if (!event.isStylusPointer()) {
            return false;
        }
+32 −7
Original line number Diff line number Diff line
@@ -204,7 +204,6 @@ import java.util.concurrent.CopyOnWriteArrayList;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;
import java.util.function.Consumer;
import java.util.function.IntConsumer;
import java.util.function.IntFunction;

/**
@@ -1301,12 +1300,9 @@ public final class InputMethodManagerService implements IInputMethodManagerImpl.
                    com.android.internal.R.bool.config_preventImeStartupUnlessTextEditor);
            mNonPreemptibleInputMethods = mRes.getStringArray(
                    com.android.internal.R.array.config_nonPreemptibleInputMethods);
            IntConsumer toolTypeConsumer =
                    Flags.useHandwritingListenerForTooltype()
                            ? toolType -> onUpdateEditorToolType(toolType) : null;
            Runnable discardDelegationTextRunnable = () -> discardHandwritingDelegationText();
            mHwController = new HandwritingModeController(mContext, thread.getLooper(),
                    new InkWindowInitializer(), toolTypeConsumer, discardDelegationTextRunnable);
                    new InkWindowInitializer(), discardDelegationTextRunnable);
            registerDeviceListenerAndCheckStylusSupport();
        }
    }
@@ -3372,8 +3368,10 @@ public final class InputMethodManagerService implements IInputMethodManagerImpl.
            ImeTracker.forLogging().onProgress(statsToken, ImeTracker.PHASE_SERVER_HAS_IME);
            mCurStatsToken = null;

            if (lastClickToolType != MotionEvent.TOOL_TYPE_UNKNOWN) {
                curMethod.updateEditorToolType(lastClickToolType);
            if (Flags.useHandwritingListenerForTooltype()) {
                maybeReportToolType();
            } else if (lastClickToolType != MotionEvent.TOOL_TYPE_UNKNOWN) {
                onUpdateEditorToolType(lastClickToolType);
            }
            mVisibilityApplier.performShowIme(windowToken, statsToken,
                    mVisibilityStateComputer.getShowFlagsForInputMethodServiceOnly(),
@@ -3387,6 +3385,29 @@ public final class InputMethodManagerService implements IInputMethodManagerImpl.
        return false;
    }

    @GuardedBy("ImfLock.class")
    private void maybeReportToolType() {
        int lastDeviceId = mInputManagerInternal.getLastUsedInputDeviceId();
        final InputManager im = mContext.getSystemService(InputManager.class);
        if (im == null) {
            return;
        }
        InputDevice device = im.getInputDevice(lastDeviceId);
        if (device == null) {
            return;
        }
        int toolType;
        if (isStylusDevice(device)) {
            toolType = MotionEvent.TOOL_TYPE_STYLUS;
        } else if (isFingerDevice(device)) {
            toolType = MotionEvent.TOOL_TYPE_FINGER;
        } else {
            // other toolTypes are irrelevant and reported as unknown.
            toolType = MotionEvent.TOOL_TYPE_UNKNOWN;
        }
        onUpdateEditorToolType(toolType);
    }

    @Override
    public boolean hideSoftInput(IInputMethodClient client, IBinder windowToken,
            @NonNull ImeTracker.Token statsToken, @InputMethodManager.HideFlags int flags,
@@ -4242,6 +4263,10 @@ public final class InputMethodManagerService implements IInputMethodManagerImpl.
                || inputDevice.supportsSource(InputDevice.SOURCE_BLUETOOTH_STYLUS);
    }

    private static boolean isFingerDevice(InputDevice inputDevice) {
        return inputDevice.supportsSource(InputDevice.SOURCE_TOUCHSCREEN);
    }

    @GuardedBy("ImfLock.class")
    private boolean hasSupportedStylusLocked() {
        return mStylusIds != null && mStylusIds.size() != 0;