Use SOURCE_CLASS_POINTER as the default source for MotionEvent

This CL also adds a test to ensure that a pointer source is used by
default when obtaining a MotionEvent, and that offsets are only applied
to pointer sources.

Bug: 207251886
Test: atest MotionEventTests
Change-Id: Id40accc6d1238c940f139a18b26fa277dd6e11f9
This commit is contained in:
Prabir Pradhan
2021-12-09 23:51:52 -08:00
parent 6dbbb8557f
commit b1d3e076af
2 changed files with 25 additions and 1 deletions

View File

@@ -1860,7 +1860,7 @@ public final class MotionEvent extends InputEvent implements Parcelable {
float x, float y, float pressure, float size, int metaState,
float xPrecision, float yPrecision, int deviceId, int edgeFlags) {
return obtain(downTime, eventTime, action, x, y, pressure, size, metaState,
xPrecision, yPrecision, deviceId, edgeFlags, InputDevice.SOURCE_UNKNOWN,
xPrecision, yPrecision, deviceId, edgeFlags, InputDevice.SOURCE_CLASS_POINTER,
DEFAULT_DISPLAY);
}

View File

@@ -16,6 +16,7 @@
package android.view;
import static android.view.InputDevice.SOURCE_CLASS_POINTER;
import static android.view.MotionEvent.ACTION_DOWN;
import static android.view.MotionEvent.ACTION_POINTER_DOWN;
import static android.view.MotionEvent.TOOL_TYPE_FINGER;
@@ -189,4 +190,27 @@ public class MotionEventTest {
assertEquals(950, (int) rot270.getX());
assertEquals(30, (int) rot270.getY());
}
@Test
public void testUsesPointerSourceByDefault() {
final MotionEvent event = MotionEvent.obtain(0 /* downTime */, 0 /* eventTime */,
ACTION_DOWN, 0 /* x */, 0 /* y */, 0 /* metaState */);
assertTrue(event.isFromSource(SOURCE_CLASS_POINTER));
}
@Test
public void testLocationOffsetOnlyAppliedToNonPointerSources() {
final MotionEvent event = MotionEvent.obtain(0 /* downTime */, 0 /* eventTime */,
ACTION_DOWN, 10 /* x */, 20 /* y */, 0 /* metaState */);
event.offsetLocation(40, 50);
// The offset should be applied since a pointer source is used by default.
assertEquals(50, (int) event.getX());
assertEquals(70, (int) event.getY());
// The offset should not be applied if the source is changed to a non-pointer source.
event.setSource(InputDevice.SOURCE_JOYSTICK);
assertEquals(10, (int) event.getX());
assertEquals(20, (int) event.getY());
}
}