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

Commit 3ed2996c authored by Omar Abdelmonem's avatar Omar Abdelmonem
Browse files

Change TouchpadDebugView color on touchpad button clicked

Send HardwareState of the touchpad to the TouchpadDebugView and
change the colour of the view each time the touchpad button is clicked

Bug: 363251709

Test: Manual testing by checking that the TouchpadDebugView changes
colour each time the Touchpad button is pressed and unit testing
to the verify the colour change by comparing the old colour of
the view with the new one.
flag: com.android.hardware.input.touchpad_visualizer

Change-Id: I54b9a627b67a2d674a12d34011deb1f8756ba2ca
parent c6146d09
Loading
Loading
Loading
Loading
+3 −1
Original line number Diff line number Diff line
@@ -2269,13 +2269,15 @@ public class InputManagerService extends IInputManager.Stub
    // Native callback.
    @SuppressWarnings("unused")
    private void notifyTouchpadHardwareState(TouchpadHardwareState hardwareStates, int deviceId) {
        // TODO(b/286551975): sent the touchpad hardware state data here to TouchpadDebugActivity
        Slog.d(TAG, "notifyTouchpadHardwareState: Time: "
                + hardwareStates.getTimestamp() + ", No. Buttons: "
                + hardwareStates.getButtonsDown() + ", No. Fingers: "
                + hardwareStates.getFingerCount() + ", No. Touch: "
                + hardwareStates.getTouchCount() + ", Id: "
                + deviceId);
        if (mTouchpadDebugViewController != null) {
            mTouchpadDebugViewController.updateTouchpadHardwareState(hardwareStates);
        }
    }

    // Native callback.
+44 −12
Original line number Diff line number Diff line
@@ -30,6 +30,9 @@ import android.view.WindowManager;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.android.server.input.TouchpadFingerState;
import com.android.server.input.TouchpadHardwareState;

import java.util.Objects;

public class TouchpadDebugView extends LinearLayout {
@@ -52,6 +55,10 @@ public class TouchpadDebugView extends LinearLayout {
    private int mScreenHeight;
    private int mWindowLocationBeforeDragX;
    private int mWindowLocationBeforeDragY;
    @NonNull
    private TouchpadHardwareState mLastTouchpadState =
            new TouchpadHardwareState(0, 0 /* buttonsDown */, 0, 0,
                    new TouchpadFingerState[0]);

    public TouchpadDebugView(Context context, int touchpadId) {
        super(context);
@@ -83,14 +90,14 @@ public class TouchpadDebugView extends LinearLayout {

    private void init(Context context) {
        setOrientation(VERTICAL);
        setLayoutParams(new LinearLayout.LayoutParams(
                LinearLayout.LayoutParams.WRAP_CONTENT,
                LinearLayout.LayoutParams.WRAP_CONTENT));
        setBackgroundColor(Color.TRANSPARENT);
        setLayoutParams(new LayoutParams(
                LayoutParams.WRAP_CONTENT,
                LayoutParams.WRAP_CONTENT));
        setBackgroundColor(Color.RED);

        // TODO(b/286551975): Replace this content with the touchpad debug view.
        TextView textView1 = new TextView(context);
        textView1.setBackgroundColor(Color.parseColor("#FFFF0000"));
        textView1.setBackgroundColor(Color.TRANSPARENT);
        textView1.setTextSize(20);
        textView1.setText("Touchpad Debug View 1");
        textView1.setGravity(Gravity.CENTER);
@@ -98,7 +105,7 @@ public class TouchpadDebugView extends LinearLayout {
        textView1.setLayoutParams(new LayoutParams(1000, 200));

        TextView textView2 = new TextView(context);
        textView2.setBackgroundColor(Color.BLUE);
        textView2.setBackgroundColor(Color.TRANSPARENT);
        textView2.setTextSize(20);
        textView2.setText("Touchpad Debug View 2");
        textView2.setGravity(Gravity.CENTER);
@@ -126,9 +133,7 @@ public class TouchpadDebugView extends LinearLayout {
            case MotionEvent.ACTION_MOVE:
                deltaX = event.getRawX() - mWindowLayoutParams.x - mTouchDownX;
                deltaY = event.getRawY() - mWindowLayoutParams.y - mTouchDownY;
                Slog.d("TouchpadDebugView", "Slop = " + mTouchSlop);
                if (isSlopExceeded(deltaX, deltaY)) {
                    Slog.d("TouchpadDebugView", "Slop exceeded");
                    mWindowLayoutParams.x =
                            Math.max(0, Math.min((int) (event.getRawX() - mTouchDownX),
                                    mScreenWidth - this.getWidth()));
@@ -136,9 +141,6 @@ public class TouchpadDebugView extends LinearLayout {
                            Math.max(0, Math.min((int) (event.getRawY() - mTouchDownY),
                                    mScreenHeight - this.getHeight()));

                    Slog.d("TouchpadDebugView", "New position X: "
                            + mWindowLayoutParams.x + ", Y: " + mWindowLayoutParams.y);

                    mWindowManager.updateViewLayout(this, mWindowLayoutParams);
                }
                return true;
@@ -166,7 +168,7 @@ public class TouchpadDebugView extends LinearLayout {
    @Override
    public boolean performClick() {
        super.performClick();
        Slog.d("TouchpadDebugView", "You clicked me!");
        Slog.d("TouchpadDebugView", "You tapped the window!");
        return true;
    }

@@ -201,4 +203,34 @@ public class TouchpadDebugView extends LinearLayout {
    public WindowManager.LayoutParams getWindowLayoutParams() {
        return mWindowLayoutParams;
    }

    public void updateHardwareState(TouchpadHardwareState touchpadHardwareState) {
        if (mLastTouchpadState.getButtonsDown() == 0) {
            if (touchpadHardwareState.getButtonsDown() > 0) {
                onTouchpadButtonPress();
            }
        } else {
            if (touchpadHardwareState.getButtonsDown() == 0) {
                onTouchpadButtonRelease();
            }
        }
        mLastTouchpadState = touchpadHardwareState;
    }

    private void onTouchpadButtonPress() {
        Slog.d("TouchpadDebugView", "You clicked me!");

        // Iterate through all child views
        // Temporary demonstration for testing
        for (int i = 0; i < getChildCount(); i++) {
            getChildAt(i).setBackgroundColor(Color.BLUE);
        }
    }

    private void onTouchpadButtonRelease() {
        Slog.d("TouchpadDebugView", "You released the click");
        for (int i = 0; i < getChildCount(); i++) {
            getChildAt(i).setBackgroundColor(Color.RED);
        }
    }
}
+7 −0
Original line number Diff line number Diff line
@@ -27,6 +27,7 @@ import android.view.WindowManager;

import com.android.server.input.InputManagerService;
import com.android.server.input.TouchpadHardwareProperties;
import com.android.server.input.TouchpadHardwareState;

import java.util.Objects;

@@ -132,4 +133,10 @@ public class TouchpadDebugViewController implements InputManager.InputDeviceList
        mTouchpadDebugView = null;
        Slog.d(TAG, "Touchpad debug view removed.");
    }

    public void updateTouchpadHardwareState(TouchpadHardwareState touchpadHardwareState) {
        if (mTouchpadDebugView != null) {
            mTouchpadDebugView.updateHardwareState(touchpadHardwareState);
        }
    }
}
+36 −0
Original line number Diff line number Diff line
@@ -26,7 +26,9 @@ import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

import android.content.Context;
import android.graphics.Color;
import android.graphics.Rect;
import android.graphics.drawable.ColorDrawable;
import android.testing.TestableContext;
import android.view.MotionEvent;
import android.view.View;
@@ -40,6 +42,8 @@ import androidx.test.runner.AndroidJUnit4;

import com.android.cts.input.MotionEventBuilder;
import com.android.cts.input.PointerBuilder;
import com.android.server.input.TouchpadFingerState;
import com.android.server.input.TouchpadHardwareState;

import org.junit.Before;
import org.junit.Test;
@@ -289,4 +293,36 @@ public class TouchpadDebugViewTest {
        assertEquals(initialX, mWindowLayoutParamsCaptor.getValue().x);
        assertEquals(initialY, mWindowLayoutParamsCaptor.getValue().y);
    }

    @Test
    public void testTouchpadClick() {
        View child;

        mTouchpadDebugView.updateHardwareState(
                new TouchpadHardwareState(0, 1 /* buttonsDown */, 0, 0,
                        new TouchpadFingerState[0]));

        for (int i = 0; i < mTouchpadDebugView.getChildCount(); i++) {
            child = mTouchpadDebugView.getChildAt(i);
            assertEquals(((ColorDrawable) child.getBackground()).getColor(), Color.BLUE);
        }

        mTouchpadDebugView.updateHardwareState(
                new TouchpadHardwareState(0, 0 /* buttonsDown */, 0, 0,
                        new TouchpadFingerState[0]));

        for (int i = 0; i < mTouchpadDebugView.getChildCount(); i++) {
            child = mTouchpadDebugView.getChildAt(i);
            assertEquals(((ColorDrawable) child.getBackground()).getColor(), Color.RED);
        }

        mTouchpadDebugView.updateHardwareState(
                new TouchpadHardwareState(0, 1 /* buttonsDown */, 0, 0,
                        new TouchpadFingerState[0]));

        for (int i = 0; i < mTouchpadDebugView.getChildCount(); i++) {
            child = mTouchpadDebugView.getChildAt(i);
            assertEquals(((ColorDrawable) child.getBackground()).getColor(), Color.BLUE);
        }
    }
}