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

Commit 1862eb63 authored by Hiroki Sato's avatar Hiroki Sato
Browse files

Add getCursorPositionInLogicalDisplay

This change adds InputManagerService#getCursorPositionInLogicalDisplay
and related APIs to be able to return logical display coordinates if
needed.
* The VirtualMouse API behavior change is gated by CompatChange target
  SDK version check.
* InputManager API is a test API only used by a limited consumer. The
  behavior is changed to return in logical display coordinates.

Bug: 431622043
Test: InputManagerServiceTests
Test: VirtualInputDeviceControllerTest
Test: MouseToTouchCompatTest
Flag: EXEMPT bugfix
Change-Id: Ida264cbe4ea33cc08673ba15b21d10a4d5efb4ff
parent 66e121aa
Loading
Loading
Loading
Loading
+3 −1
Original line number Diff line number Diff line
@@ -309,5 +309,7 @@ interface IInputManager {

    void setMouseScalingEnabled(boolean enabled, int displayId);

    PointF getCursorPosition(int displayId);
    PointF getCursorPositionInPhysicalDisplay(int displayId);

    PointF getCursorPositionInLogicalDisplay(int displayId);
}
+9 −3
Original line number Diff line number Diff line
@@ -98,8 +98,14 @@ interface IVirtualInputDevice {
    boolean sendRotaryEncoderScrollEvent(in VirtualRotaryEncoderScrollEvent event);

    /**
     * Returns the current cursor position of the mouse corresponding to this device, in x and y
     * coordinates.
     * Returns the current cursor position of the mouse corresponding to this device, in the
     * physical display coordinates.
     */
    PointF getCursorPosition();
    PointF getCursorPositionInPhysicalDisplay();

    /**
     * Returns the current cursor position of the mouse corresponding to this device, in the
     * logical display coordinates.
     */
    PointF getCursorPositionInLogicalDisplay();
}
+3 −1
Original line number Diff line number Diff line
@@ -1623,6 +1623,8 @@ public final class InputManager {
    /**
     * Gets the current position of the mouse cursor on the specified display.
     *
     * <p>Returned values are in logical display coordinates in pixels.
     *
     * <p>Returns null if no cursor is available, or if existing cursor is not on the supplied
     * `displayId`.
     *
@@ -1635,7 +1637,7 @@ public final class InputManager {
    @Nullable
    public PointF getCursorPosition(int displayId) {
        try {
            return mIm.getCursorPosition(displayId);
            return mIm.getCursorPositionInLogicalDisplay(displayId);
        } catch (RemoteException e) {
            throw e.rethrowFromSystemServer();
        }
+23 −2
Original line number Diff line number Diff line
@@ -18,7 +18,11 @@ package android.hardware.input;

import android.annotation.NonNull;
import android.annotation.SystemApi;
import android.app.compat.CompatChanges;
import android.compat.annotation.ChangeId;
import android.compat.annotation.EnabledSince;
import android.graphics.PointF;
import android.os.Build;
import android.os.RemoteException;
import android.util.Log;
import android.view.MotionEvent;
@@ -35,6 +39,14 @@ import android.view.MotionEvent;
@SystemApi
public class VirtualMouse extends VirtualInputDevice {

    /**
     * If enabled, the {@link #getCursorPosition()} API now returns in logical display coordinates
     * instead of in physical display coordinates, which is an old behavior.
     */
    @ChangeId
    @EnabledSince(targetSdkVersion = Build.VERSION_CODES.CUR_DEVELOPMENT)
    static final long VIRTUAL_MOUSE_CURSOR_POTION_IN_LOGICAL_COORDINATES = 431622043;

    /** @hide */
    public VirtualMouse(VirtualMouseConfig config, IVirtualInputDevice virtualInputDevice) {
        super(config, virtualInputDevice);
@@ -96,7 +108,10 @@ public class VirtualMouse extends VirtualInputDevice {
    }

    /**
     * Gets the current cursor position.
     * Gets the current cursor position in logical display coordinates in pixels.
     *
     * <p>Note that if {@code VIRTUAL_MOUSE_CURSOR_POTION_IN_LOGICAL_COORDINATES} is disabled,
     * this returns a position in the physical display coordinates instead.
     *
     * @return the position, expressed as x and y coordinates
     * @throws IllegalStateException if the display this mouse is associated with is not currently
@@ -104,7 +119,13 @@ public class VirtualMouse extends VirtualInputDevice {
     */
    public @NonNull PointF getCursorPosition() {
        try {
            PointF cursorPosition = mVirtualInputDevice.getCursorPosition();
            final PointF cursorPosition;
            if (CompatChanges.isChangeEnabled(
                    VIRTUAL_MOUSE_CURSOR_POTION_IN_LOGICAL_COORDINATES)) {
                cursorPosition = mVirtualInputDevice.getCursorPositionInLogicalDisplay();
            } else {
                cursorPosition = mVirtualInputDevice.getCursorPositionInPhysicalDisplay();
            }
            // TODO(b/410677781): Returning PointF(NaN, NaN) on invalid displayId is different with
            // what the javadoc states, consider updating this (or the javadoc).
            return cursorPosition != null ? cursorPosition : new PointF(Float.NaN, Float.NaN);
+20 −2
Original line number Diff line number Diff line
@@ -3225,7 +3225,7 @@ public class InputManagerService extends IInputManager.Stub

    @Override // Binder call
    @Nullable
    public PointF getCursorPosition(int displayId) {
    public PointF getCursorPositionInPhysicalDisplay(int displayId) {
        if (!checkCallingPermission(
                Manifest.permission.INJECT_EVENTS,
                "getCursorPosition()",
@@ -3234,7 +3234,25 @@ public class InputManagerService extends IInputManager.Stub
                    "The INJECT_EVENTS permission is required to access cursor outside the "
                            + "intermediate window / display.");
        }
        final float[] p = mNative.getMouseCursorPosition(displayId);
        final float[] p = mNative.getMouseCursorPositionInPhysicalDisplay(displayId);
        if (p == null || p.length != 2) {
            return null;
        }
        return new PointF(p[0], p[1]);
    }

    @Override // Binder call
    @Nullable
    public PointF getCursorPositionInLogicalDisplay(int displayId) {
        if (!checkCallingPermission(
                Manifest.permission.INJECT_EVENTS,
                "getCursorPositionInLogicalDisplay()",
                true /*checkInstrumentationSource*/)) {
            throw new SecurityException(
                    "The INJECT_EVENTS permission is required to access cursor outside the "
                            + "intermediate window / display.");
        }
        final float[] p = mNative.getMouseCursorPositionInLogicalDisplay(displayId);
        if (p == null || p.length != 2) {
            return null;
        }
Loading