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

Commit 2ea54a5c authored by Josep del Rio's avatar Josep del Rio Committed by Josep del Río
Browse files

Reject touch when the stylus hovers

At the moment we disable touch when the stylus is in contact with the
screen, but we don't when the stylus is hovering, which is different
from how other platforms operate.

Bug: 301216095
Test: Flashed on real device
Change-Id: I481ec41a82d7de6da28c5b122450e0bd8faea66f
parent b53068f3
Loading
Loading
Loading
Loading
+7 −0
Original line number Diff line number Diff line
@@ -55,3 +55,10 @@ flag {
  description: "Remove the logic of dropping events due to pending app switch"
  bug: "284808102"
}

flag {
  name: "disable_reject_touch_on_stylus_hover"
  namespace: "input"
  description: "Disable touch rejection when the stylus hovers the screen"
  bug: "301216095"
}
+19 −8
Original line number Diff line number Diff line
@@ -15,10 +15,15 @@
 */

#include "PreferStylusOverTouchBlocker.h"
#include <com_android_input_flags.h>
#include <input/PrintTools.h>

namespace input_flags = com::android::input::flags;

namespace android {

const bool BLOCK_TOUCH_WHEN_STYLUS_HOVER = !input_flags::disable_reject_touch_on_stylus_hover();

static std::pair<bool, bool> checkToolType(const NotifyMotionArgs& args) {
    bool hasStylus = false;
    bool hasTouch = false;
@@ -96,8 +101,11 @@ static void intersectInPlace(std::map<K, V>& map, const std::set<K>& set2) {
std::vector<NotifyMotionArgs> PreferStylusOverTouchBlocker::processMotion(
        const NotifyMotionArgs& args) {
    const auto [hasTouch, hasStylus] = checkToolType(args);
    const bool isUpOrCancel =
            args.action == AMOTION_EVENT_ACTION_UP || args.action == AMOTION_EVENT_ACTION_CANCEL;
    const bool isDisengageOrCancel = BLOCK_TOUCH_WHEN_STYLUS_HOVER
            ? (args.action == AMOTION_EVENT_ACTION_HOVER_EXIT ||
               args.action == AMOTION_EVENT_ACTION_UP || args.action == AMOTION_EVENT_ACTION_CANCEL)
            : (args.action == AMOTION_EVENT_ACTION_UP ||
               args.action == AMOTION_EVENT_ACTION_CANCEL);

    if (hasTouch && hasStylus) {
        mDevicesWithMixedToolType.insert(args.deviceId);
@@ -109,7 +117,7 @@ std::vector<NotifyMotionArgs> PreferStylusOverTouchBlocker::processMotion(
        if (mCanceledDevices.find(args.deviceId) != mCanceledDevices.end()) {
            // If we started to cancel events from this device, continue to do so to keep
            // the stream consistent. It should happen at most once per "mixed" device.
            if (isUpOrCancel) {
            if (isDisengageOrCancel) {
                mCanceledDevices.erase(args.deviceId);
                mLastTouchEvents.erase(args.deviceId);
            }
@@ -119,10 +127,13 @@ std::vector<NotifyMotionArgs> PreferStylusOverTouchBlocker::processMotion(
    }

    const bool isStylusEvent = hasStylus;
    const bool isDown = args.action == AMOTION_EVENT_ACTION_DOWN;
    const bool isEngage = BLOCK_TOUCH_WHEN_STYLUS_HOVER
            ? (args.action == AMOTION_EVENT_ACTION_DOWN ||
               args.action == AMOTION_EVENT_ACTION_HOVER_ENTER)
            : (args.action == AMOTION_EVENT_ACTION_DOWN);

    if (isStylusEvent) {
        if (isDown) {
        if (isEngage) {
            // Reject all touch while stylus is down
            mActiveStyli.insert(args.deviceId);

@@ -143,7 +154,7 @@ std::vector<NotifyMotionArgs> PreferStylusOverTouchBlocker::processMotion(
            result.push_back(args);
            return result;
        }
        if (isUpOrCancel) {
        if (isDisengageOrCancel) {
            mActiveStyli.erase(args.deviceId);
        }
        // Never drop stylus events
@@ -158,7 +169,7 @@ std::vector<NotifyMotionArgs> PreferStylusOverTouchBlocker::processMotion(
        }

        const bool shouldDrop = mCanceledDevices.find(args.deviceId) != mCanceledDevices.end();
        if (isUpOrCancel) {
        if (isDisengageOrCancel) {
            mCanceledDevices.erase(args.deviceId);
            mLastTouchEvents.erase(args.deviceId);
        }
@@ -169,7 +180,7 @@ std::vector<NotifyMotionArgs> PreferStylusOverTouchBlocker::processMotion(
            return {};
        }

        if (!isUpOrCancel) {
        if (!isDisengageOrCancel) {
            mLastTouchEvents[args.deviceId] = args;
        }
        return {args};