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 Original line Diff line number Diff line
@@ -55,7 +55,6 @@ import java.util.ArrayList;
import java.util.List;
import java.util.List;
import java.util.Objects;
import java.util.Objects;
import java.util.OptionalInt;
import java.util.OptionalInt;
import java.util.function.IntConsumer;


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


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


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


@@ -389,16 +386,10 @@ final class HandwritingModeController {
                    "Input Event should not be processed when IME has the spy channel.");
                    "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.");
            Slog.wtf(TAG, "Received non-motion event in stylus monitor.");
            return false;
            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()) {
        if (!event.isStylusPointer()) {
            return false;
            return false;
        }
        }
+32 −7
Original line number Original line Diff line number Diff line
@@ -204,7 +204,6 @@ import java.util.concurrent.CopyOnWriteArrayList;
import java.util.concurrent.Future;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeUnit;
import java.util.function.Consumer;
import java.util.function.Consumer;
import java.util.function.IntConsumer;
import java.util.function.IntFunction;
import java.util.function.IntFunction;


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


            if (lastClickToolType != MotionEvent.TOOL_TYPE_UNKNOWN) {
            if (Flags.useHandwritingListenerForTooltype()) {
                curMethod.updateEditorToolType(lastClickToolType);
                maybeReportToolType();
            } else if (lastClickToolType != MotionEvent.TOOL_TYPE_UNKNOWN) {
                onUpdateEditorToolType(lastClickToolType);
            }
            }
            mVisibilityApplier.performShowIme(windowToken, statsToken,
            mVisibilityApplier.performShowIme(windowToken, statsToken,
                    mVisibilityStateComputer.getShowFlagsForInputMethodServiceOnly(),
                    mVisibilityStateComputer.getShowFlagsForInputMethodServiceOnly(),
@@ -3387,6 +3385,29 @@ public final class InputMethodManagerService implements IInputMethodManagerImpl.
        return false;
        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
    @Override
    public boolean hideSoftInput(IInputMethodClient client, IBinder windowToken,
    public boolean hideSoftInput(IInputMethodClient client, IBinder windowToken,
            @NonNull ImeTracker.Token statsToken, @InputMethodManager.HideFlags int flags,
            @NonNull ImeTracker.Token statsToken, @InputMethodManager.HideFlags int flags,
@@ -4242,6 +4263,10 @@ public final class InputMethodManagerService implements IInputMethodManagerImpl.
                || inputDevice.supportsSource(InputDevice.SOURCE_BLUETOOTH_STYLUS);
                || inputDevice.supportsSource(InputDevice.SOURCE_BLUETOOTH_STYLUS);
    }
    }


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

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