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

Commit 2c914120 authored by Ben Pietrzak's avatar Ben Pietrzak
Browse files

Send Trackball clicks instead of Enter Key for taps

Change-Id: Ib1e77a1bdad81db18f84bb33402e9144517a0f4c
parent f3df6b08
Loading
Loading
Loading
Loading
+52 −4
Original line number Diff line number Diff line
@@ -25,30 +25,78 @@ class SimulatedTrackball {
    //The position of the previous touchpad event
    private float mLastTouchpadXPosition;
    private float mLastTouchpadYPosition;
    //Where the touchpad was initially pressed
    private float mTouchpadEnterXPosition;
    private float mTouchpadEnterYPosition;
    //When the last touchpad event occurred
    private long mLastTouchPadStartTimeMs = 0;

    //Change in position allowed during tap events
    private float mTouchSlop;
    private float mTouchSlopSquared;
    //Has the TouchSlop constraint been invalidated
    private boolean mAlwaysInTapRegion = true;

    //Maximum difference in milliseconds between the down and up of a touch event
    //for it to be considered a tap
    //TODO:Read this value from a config file
    private static final int MAX_TAP_TIME = 250;

    public SimulatedTrackball(){
        mTouchSlop = ViewConfiguration.getTouchSlop();
        mTouchSlopSquared = mTouchSlop * mTouchSlop;
    }

    public void updateTrackballDirection(ViewRootImpl viewroot, MotionEvent event){
        //Store what time the touchpad event occurred
        final long time = event.getEventTime();
        MotionEvent trackballEvent;
        switch (event.getAction()) {
            case MotionEvent.ACTION_HOVER_ENTER:
                mLastTouchPadStartTimeMs = time;
                mAlwaysInTapRegion = true;
                mTouchpadEnterXPosition = event.getX();
                mTouchpadEnterYPosition = event.getY();
                break;
            case MotionEvent.ACTION_HOVER_MOVE:
                //Find the difference in position between the two most recent touchpad events
                float deltaX = event.getX() - mLastTouchpadXPosition;
                float deltaY = event.getY() - mLastTouchpadYPosition;

                //TODO: Get simulated trackball configuration parameters
                //Create a trackball event from recorded touchpad event data
                MotionEvent trackballEvent = MotionEvent.obtain(mLastTouchPadStartTimeMs, time,
                        MotionEvent.ACTION_MOVE, deltaX/50, deltaY/50, 0, 0, event.getMetaState(),
                trackballEvent = MotionEvent.obtain(mLastTouchPadStartTimeMs, time,
                        MotionEvent.ACTION_MOVE, deltaX / 50,
                        deltaY / 50, 0, 0, event.getMetaState(),
                        10f, 10f, event.getDeviceId(), 0);
                trackballEvent.setSource(InputDevice.SOURCE_CLASS_TRACKBALL);
                //Add the new event to event queue
                viewroot.enqueueInputEvent(trackballEvent);

                deltaX = event.getX() - mTouchpadEnterXPosition;
                deltaY = event.getY() - mTouchpadEnterYPosition;
                if (mTouchSlopSquared < deltaX * deltaX + deltaY * deltaY){
                    mAlwaysInTapRegion = false;
                }
                break;
            case MotionEvent.ACTION_HOVER_EXIT:
                if (time-mLastTouchPadStartTimeMs<MAX_TAP_TIME && mAlwaysInTapRegion){
                    //Trackball Down
                    trackballEvent = MotionEvent.obtain(mLastTouchPadStartTimeMs, time,
                        MotionEvent.ACTION_DOWN, 0, 0, 0, 0, event.getMetaState(),
                        10f, 10f, event.getDeviceId(), 0);
                    trackballEvent.setSource(InputDevice.SOURCE_CLASS_TRACKBALL);
                    //Add the new event to event queue
                    viewroot.enqueueInputEvent(trackballEvent);

                    //Trackball Release
                    trackballEvent = MotionEvent.obtain(mLastTouchPadStartTimeMs, time,
                        MotionEvent.ACTION_UP, 0, 0, 0, 0, event.getMetaState(),
                        10f, 10f, event.getDeviceId(), 0);
                    trackballEvent.setSource(InputDevice.SOURCE_CLASS_TRACKBALL);
                    //Add the new event to event queue
                    viewroot.enqueueInputEvent(trackballEvent);
                }
                break;
        }
        //Store touch event position
+11 −4
Original line number Diff line number Diff line
@@ -3410,14 +3410,14 @@ public final class ViewRootImpl implements ViewParent,
        final int source = event.getSource();
        final boolean isJoystick = (source & InputDevice.SOURCE_CLASS_JOYSTICK) != 0;
        final boolean isTouchPad = (source & InputDevice.SOURCE_CLASS_POSITION) != 0;
        if (isTouchPad) {
            //Convert TouchPad motion into a TrackBall event
            mSimulatedTrackball.updateTrackballDirection(this, event);
        }

        // If there is no view, then the event will not be handled.
        if (mView == null || !mAdded) {
            if (isJoystick) {
                updateJoystickDirection(event, false);
            } else if (isTouchPad) {
              //Convert TouchPad motion into a TrackBall event
              mSimulatedTrackball.updateTrackballDirection(this, event);
            }
            finishInputEvent(q, false);
            return;
@@ -3427,6 +3427,9 @@ public final class ViewRootImpl implements ViewParent,
        if (mView.dispatchGenericMotionEvent(event)) {
            if (isJoystick) {
                updateJoystickDirection(event, false);
            } else if (isTouchPad) {
              //Convert TouchPad motion into a TrackBall event
              mSimulatedTrackball.updateTrackballDirection(this, event);
            }
            finishInputEvent(q, true);
            return;
@@ -3437,6 +3440,10 @@ public final class ViewRootImpl implements ViewParent,
            // those.
            updateJoystickDirection(event, true);
            finishInputEvent(q, true);
        } else if (isTouchPad) {
            //Convert TouchPad motion into a TrackBall event
            mSimulatedTrackball.updateTrackballDirection(this, event);
            finishInputEvent(q, true);
        } else {
            finishInputEvent(q, false);
        }