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

Commit 814af359 authored by Abdelrahman Awadalla's avatar Abdelrahman Awadalla
Browse files

Auto resizing the touchpad visualizer

Resizing the touchpad visualizer dimensions based on the size of the touchpad connected putting in consideration the size of the screen the window is shown on and the orientation of the device.

Test: Manual Testing
Test: Build
Bug: 360137366
Flag: com.android.hardware.input.touchpad_visualizer
Change-Id: I6e9deaec75afbafee0077f35f56ea77f1419fb0d
parent 843b8537
Loading
Loading
Loading
Loading
+59 −13
Original line number Diff line number Diff line
@@ -16,6 +16,8 @@

package com.android.server.input.debug;

import static android.util.TypedValue.COMPLEX_UNIT_DIP;

import android.annotation.NonNull;
import android.content.Context;
import android.content.res.Configuration;
@@ -24,6 +26,7 @@ import android.graphics.PixelFormat;
import android.graphics.Rect;
import android.hardware.input.InputManager;
import android.util.Slog;
import android.util.TypedValue;
import android.view.Gravity;
import android.view.MotionEvent;
import android.view.ViewConfiguration;
@@ -38,6 +41,13 @@ import com.android.server.input.TouchpadHardwareState;
import java.util.Objects;

public class TouchpadDebugView extends LinearLayout {
    private static final float MAX_SCREEN_WIDTH_PROPORTION = 0.4f;
    private static final float MAX_SCREEN_HEIGHT_PROPORTION = 0.4f;
    private static final float MIN_SCALE_FACTOR = 10f;
    private static final float TEXT_SIZE_SP = 16.0f;
    private static final float DEFAULT_RES_X = 47f;
    private static final float DEFAULT_RES_Y = 45f;

    /**
     * Input device ID for the touchpad that this debug view is displaying.
     */
@@ -62,6 +72,7 @@ public class TouchpadDebugView extends LinearLayout {
            new TouchpadHardwareState(0, 0 /* buttonsDown */, 0, 0,
                    new TouchpadFingerState[0]);
    private TouchpadVisualizationView mTouchpadVisualizationView;
    private final TouchpadHardwareProperties mTouchpadHardwareProperties;

    public TouchpadDebugView(Context context, int touchpadId,
            TouchpadHardwareProperties touchpadHardwareProperties) {
@@ -69,10 +80,10 @@ public class TouchpadDebugView extends LinearLayout {
        mTouchpadId = touchpadId;
        mWindowManager =
                Objects.requireNonNull(getContext().getSystemService(WindowManager.class));
        init(context, touchpadHardwareProperties, touchpadId);
        mTouchpadHardwareProperties = touchpadHardwareProperties;
        init(context, touchpadId);
        mTouchSlop = ViewConfiguration.get(context).getScaledTouchSlop();

        // TODO(b/360137366): Use the hardware properties to initialise layout parameters.
        mWindowLayoutParams = new WindowManager.LayoutParams();
        mWindowLayoutParams.type = WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY;
        mWindowLayoutParams.flags = WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE
@@ -92,8 +103,8 @@ public class TouchpadDebugView extends LinearLayout {
        mWindowLayoutParams.gravity = Gravity.TOP | Gravity.LEFT;
    }

    private void init(Context context, TouchpadHardwareProperties touchpadHardwareProperties,
            int touchpadId) {
    private void init(Context context, int touchpadId) {
        updateScreenDimensions();
        setOrientation(VERTICAL);
        setLayoutParams(new LayoutParams(
                LayoutParams.WRAP_CONTENT,
@@ -102,35 +113,34 @@ public class TouchpadDebugView extends LinearLayout {

        TextView nameView = new TextView(context);
        nameView.setBackgroundColor(Color.RED);
        nameView.setTextSize(20);
        nameView.setTextSize(TEXT_SIZE_SP);
        nameView.setText(Objects.requireNonNull(Objects.requireNonNull(
                        mContext.getSystemService(InputManager.class))
                .getInputDevice(touchpadId)).getName());
        nameView.setGravity(Gravity.CENTER);
        nameView.setTextColor(Color.WHITE);
        nameView.setLayoutParams(new LayoutParams(1000, 200));
        nameView.setLayoutParams(
                new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));

        mTouchpadVisualizationView = new TouchpadVisualizationView(context,
                touchpadHardwareProperties);
                mTouchpadHardwareProperties);
        mTouchpadVisualizationView.setBackgroundColor(Color.WHITE);
        //TODO(b/365568238): set the view size according to the touchpad size from the
        // TouchpadHardwareProperties
        mTouchpadVisualizationView.setLayoutParams(new LayoutParams(778, 500));

        //TODO(b/365562952): Add a display for recognized gesture info here
        TextView gestureInfoView = new TextView(context);
        gestureInfoView.setBackgroundColor(Color.GRAY);
        gestureInfoView.setTextSize(20);
        gestureInfoView.setTextSize(TEXT_SIZE_SP);
        gestureInfoView.setText("Touchpad Debug View 3");
        gestureInfoView.setGravity(Gravity.CENTER);
        gestureInfoView.setTextColor(Color.BLACK);
        gestureInfoView.setLayoutParams(new LayoutParams(1000, 200));
        gestureInfoView.setLayoutParams(
                new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));

        addView(nameView);
        addView(mTouchpadVisualizationView);
        addView(gestureInfoView);

        updateScreenDimensions();
        updateViewsDimensions();
    }

    @Override
@@ -191,6 +201,7 @@ public class TouchpadDebugView extends LinearLayout {
    protected void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);
        updateScreenDimensions();
        updateViewsDimensions();

        // Adjust view position to stay within screen bounds after rotation
        mWindowLayoutParams.x =
@@ -204,6 +215,41 @@ public class TouchpadDebugView extends LinearLayout {
        return deltaX * deltaX + deltaY * deltaY >= mTouchSlop * mTouchSlop;
    }

    private void updateViewsDimensions() {
        float resX = mTouchpadHardwareProperties.getResX() == 0f ? DEFAULT_RES_X
                : mTouchpadHardwareProperties.getResX();
        float resY = mTouchpadHardwareProperties.getResY() == 0f ? DEFAULT_RES_Y
                : mTouchpadHardwareProperties.getResY();

        float touchpadHeightMm = Math.abs(
                mTouchpadHardwareProperties.getBottom() - mTouchpadHardwareProperties.getTop())
                / resY;
        float touchpadWidthMm = Math.abs(
                mTouchpadHardwareProperties.getLeft() - mTouchpadHardwareProperties.getRight())
                / resX;

        float maxViewWidthPx = mScreenWidth * MAX_SCREEN_WIDTH_PROPORTION;
        float maxViewHeightPx = mScreenHeight * MAX_SCREEN_HEIGHT_PROPORTION;

        float minScaleFactorPx = TypedValue.applyDimension(COMPLEX_UNIT_DIP, MIN_SCALE_FACTOR,
                getResources().getDisplayMetrics());

        float scaleFactorBasedOnWidth =
                touchpadWidthMm * minScaleFactorPx > maxViewWidthPx ? maxViewWidthPx
                        / touchpadWidthMm : minScaleFactorPx;
        float scaleFactorBasedOnHeight =
                touchpadHeightMm * minScaleFactorPx > maxViewHeightPx ? maxViewHeightPx
                        / touchpadHeightMm : minScaleFactorPx;
        float scaleFactorUsed = Math.min(scaleFactorBasedOnHeight, scaleFactorBasedOnWidth);

        mTouchpadVisualizationView.setLayoutParams(
                new LayoutParams((int) (touchpadWidthMm * scaleFactorUsed),
                        (int) (touchpadHeightMm * scaleFactorUsed)));

        mTouchpadVisualizationView.updateScaleFactor(scaleFactorUsed);
        mTouchpadVisualizationView.invalidate();
    }

    private void updateScreenDimensions() {
        Rect windowBounds =
                mWindowManager.getCurrentWindowMetrics().getBounds();
+20 −4
Original line number Diff line number Diff line
@@ -30,8 +30,11 @@ import com.android.server.input.TouchpadHardwareState;
public class TouchpadVisualizationView extends View {
    private static final String TAG = "TouchpadVizMain";
    private static final boolean DEBUG = true;
    private static final float DEFAULT_RES_X = 47f;
    private static final float DEFAULT_RES_Y = 45f;

    private final TouchpadHardwareProperties mTouchpadHardwareProperties;
    private float mScaleFactor;

    TouchpadHardwareState mLatestHardwareState = new TouchpadHardwareState(0, 0, 0, 0,
            new TouchpadFingerState[]{});
@@ -42,6 +45,7 @@ public class TouchpadVisualizationView extends View {
            TouchpadHardwareProperties touchpadHardwareProperties) {
        super(context);
        mTouchpadHardwareProperties = touchpadHardwareProperties;
        mScaleFactor = 1;
        mOvalPaint = new Paint();
        mOvalPaint.setAntiAlias(true);
        mOvalPaint.setARGB(255, 0, 0, 0);
@@ -77,10 +81,13 @@ public class TouchpadVisualizationView extends View {
                    mTouchpadHardwareProperties.getOrientationMaximum(), 0, 360,
                    touchpadFingerState.getOrientation());

            float newTouchMajor =
                    touchpadFingerState.getTouchMajor() / mTouchpadHardwareProperties.getResX();
            float newTouchMinor =
                    touchpadFingerState.getTouchMinor() / mTouchpadHardwareProperties.getResY();
            float resX = mTouchpadHardwareProperties.getResX() == 0f ? DEFAULT_RES_X
                    : mTouchpadHardwareProperties.getResX();
            float resY = mTouchpadHardwareProperties.getResY() == 0f ? DEFAULT_RES_Y
                    : mTouchpadHardwareProperties.getResY();

            float newTouchMajor = touchpadFingerState.getTouchMajor() * mScaleFactor / resY;
            float newTouchMinor = touchpadFingerState.getTouchMinor() * mScaleFactor / resX;

            drawOval(canvas, newX, newY, newTouchMajor, newTouchMinor, newAngle, mOvalPaint);
        }
@@ -101,6 +108,15 @@ public class TouchpadVisualizationView extends View {
        invalidate();
    }

    /**
     * Update the scale factor of the drawings in the view.
     *
     * @param scaleFactor the new scale factor
     */
    public void updateScaleFactor(float scaleFactor) {
        mScaleFactor = scaleFactor;
    }

    private float translateRange(float rangeBeforeMin, float rangeBeforeMax,
            float rangeAfterMin, float rangeAfterMax, float value) {
        return rangeAfterMin + (value - rangeBeforeMin) / (rangeBeforeMax - rangeBeforeMin) * (
+2 −2
Original line number Diff line number Diff line
@@ -85,8 +85,8 @@ public class TouchpadDebugViewTest {
        when(mWindowManager.getCurrentWindowMetrics()).thenReturn(mWindowMetrics);

        mTouchpadDebugView = new TouchpadDebugView(mTestableContext, TOUCHPAD_DEVICE_ID,
                new TouchpadHardwareProperties.Builder(500f, 500f, 500f,
                        500f, 0f, 0f, -5f, 5f, (short) 10, true,
                new TouchpadHardwareProperties.Builder(0f, 0f, 500f,
                        500f, 45f, 47f, -4f, 5f, (short) 10, true,
                        true).build());

        mTouchpadDebugView.measure(