public class

MotionEventBuilder

extends java.lang.Object

 java.lang.Object

↳androidx.test.core.view.MotionEventBuilder

Gradle dependencies

compile group: 'androidx.test', name: 'core', version: '1.6.1'

  • groupId: androidx.test
  • artifactId: core
  • version: 1.6.1

Artifact androidx.test:core:1.6.1 it located at Google repository (https://maven.google.com/)

Overview

A helper builder for creating MotionEvent's.

Default values for unspecified attributes are 0 unless otherwise noted.

Summary

Methods
public MotionEventbuild()

Returns a MotionEvent with the provided data or reasonable defaults.

public static MotionEventBuildernewBuilder()

Start building a new MotionEvent.

public MotionEventBuildersetAction(int action)

Sets the action.

public MotionEventBuildersetActionIndex(int pointerIndex)

Sets the pointer index associated with the action.

public MotionEventBuildersetButtonState(int buttonState)

Sets the button state.

public MotionEventBuildersetDeviceId(int deviceId)

Sets the device id.

public MotionEventBuildersetDownTime(long downTime)

Sets the down time.

public MotionEventBuildersetEdgeFlags(int edgeFlags)

Sets the edge flags.

public MotionEventBuildersetEventTime(long eventTime)

Sets the event time.

public MotionEventBuildersetFlags(int flags)

Sets the flags.

public MotionEventBuildersetMetaState(int metastate)

Sets the metaState.

public MotionEventBuildersetPointer(float x, float y)

Simple mechanism to add a pointer to the MotionEvent.

public MotionEventBuildersetPointer(PointerProperties pointerProperties, PointerCoords pointerCoords)

An expanded variant of MotionEventBuilder.setPointer(float, float) that supports specifying all pointer properties and coords data.

public MotionEventBuildersetSource(int source)

Sets the source.

public MotionEventBuildersetXPrecision(float xPrecision)

Sets the x precision.

public MotionEventBuildersetYPrecision(float yPrecision)

Sets the y precision.

from java.lang.Objectclone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait

Methods

public static MotionEventBuilder newBuilder()

Start building a new MotionEvent.

Returns:

a new MotionEventBuilder.

public MotionEventBuilder setDownTime(long downTime)

Sets the down time.

See also: MotionEvent

public MotionEventBuilder setEventTime(long eventTime)

Sets the event time. Default is SystemClock.uptimeMillis().

See also: MotionEvent

public MotionEventBuilder setAction(int action)

Sets the action. Default is .

See also: MotionEvent

public MotionEventBuilder setActionIndex(int pointerIndex)

Sets the pointer index associated with the action.

See also: MotionEvent

public MotionEventBuilder setMetaState(int metastate)

Sets the metaState.

See also: MotionEvent

public MotionEventBuilder setButtonState(int buttonState)

Sets the button state.

See also: MotionEvent

public MotionEventBuilder setXPrecision(float xPrecision)

Sets the x precision.

See also: MotionEvent

public MotionEventBuilder setYPrecision(float yPrecision)

Sets the y precision.

See also: MotionEvent

public MotionEventBuilder setDeviceId(int deviceId)

Sets the device id.

See also: MotionEvent

public MotionEventBuilder setEdgeFlags(int edgeFlags)

Sets the edge flags.

See also: MotionEvent

public MotionEventBuilder setSource(int source)

Sets the source.

See also: MotionEvent

public MotionEventBuilder setFlags(int flags)

Sets the flags.

See also: MotionEvent

public MotionEventBuilder setPointer(float x, float y)

Simple mechanism to add a pointer to the MotionEvent.

Can be called multiple times to add multiple pointers to the event.

public MotionEventBuilder setPointer(PointerProperties pointerProperties, PointerCoords pointerCoords)

An expanded variant of MotionEventBuilder.setPointer(float, float) that supports specifying all pointer properties and coords data.

public MotionEvent build()

Returns a MotionEvent with the provided data or reasonable defaults.

Source

/*
 * Copyright (C) 2018 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package androidx.test.core.view;

import android.os.SystemClock;
import android.view.MotionEvent;
import android.view.MotionEvent.PointerCoords;
import android.view.MotionEvent.PointerProperties;
import java.util.ArrayList;
import java.util.List;

/**
 * A helper builder for creating {@link MotionEvent}'s.
 *
 * <p>Default values for unspecified attributes are 0 unless otherwise noted.
 */
public class MotionEventBuilder {

  private long downTime = 0;
  private long eventTime = SystemClock.uptimeMillis();
  private int action = MotionEvent.ACTION_DOWN;
  private int actionIndex = -1;
  private List<PointerProperties> pointerPropertiesList = new ArrayList<>();
  private List<PointerCoords> pointerCoordsList = new ArrayList<>();
  private int metaState = 0;
  private int buttonState = 0;
  private float xPrecision = 0f;
  private float yPrecision = 0f;
  private int deviceId = 0;
  private int edgeFlags = 0;
  private int source = 0;
  private int flags = 0;

  private MotionEventBuilder() {}

  /**
   * Start building a new MotionEvent.
   *
   * @return a new MotionEventBuilder.
   */
  public static MotionEventBuilder newBuilder() {
    return new MotionEventBuilder();
  }

  /**
   * Sets the down time.
   *
   * @see MotionEvent#getDownTime()
   */
  public MotionEventBuilder setDownTime(long downTime) {
    this.downTime = downTime;
    return this;
  }

  /**
   * Sets the event time. Default is SystemClock.uptimeMillis().
   *
   * @see MotionEvent#getEventTime()
   */
  public MotionEventBuilder setEventTime(long eventTime) {
    this.eventTime = eventTime;
    return this;
  }

  /**
   * Sets the action. Default is {@link MotionEvent.ACTION_DOWN}.
   *
   * @see MotionEvent#getAction()
   */
  public MotionEventBuilder setAction(int action) {
    this.action = action;
    return this;
  }

  /**
   * Sets the pointer index associated with the action.
   *
   * @see MotionEvent#getActionIndex()
   */
  public MotionEventBuilder setActionIndex(int pointerIndex) {
    checkState(pointerIndex <= 0xFF, "pointerIndex must be less than 0xff");
    this.actionIndex = pointerIndex;
    return this;
  }

  /**
   * Sets the metaState.
   *
   * @see MotionEvent#getMetaState()
   */
  public MotionEventBuilder setMetaState(int metastate) {
    this.metaState = metastate;
    return this;
  }

  /**
   * Sets the button state.
   *
   * @see MotionEvent#getButtonState()
   */
  public MotionEventBuilder setButtonState(int buttonState) {
    this.buttonState = buttonState;
    return this;
  }

  /**
   * Sets the x precision.
   *
   * @see MotionEvent#getXPrecision()
   */
  public MotionEventBuilder setXPrecision(float xPrecision) {
    this.xPrecision = xPrecision;
    return this;
  }

  /**
   * Sets the y precision.
   *
   * @see MotionEvent#getYPrecision()
   */
  public MotionEventBuilder setYPrecision(float yPrecision) {
    this.yPrecision = yPrecision;
    return this;
  }

  /**
   * Sets the device id.
   *
   * @see MotionEvent#getDeviceId()
   */
  public MotionEventBuilder setDeviceId(int deviceId) {
    this.deviceId = deviceId;
    return this;
  }

  /**
   * Sets the edge flags.
   *
   * @see MotionEvent#getEdgeFlags()
   */
  public MotionEventBuilder setEdgeFlags(int edgeFlags) {
    this.edgeFlags = edgeFlags;
    return this;
  }

  /**
   * Sets the source.
   *
   * @see MotionEvent#getSource()
   */
  public MotionEventBuilder setSource(int source) {
    this.source = source;
    return this;
  }

  /**
   * Sets the flags.
   *
   * @see MotionEvent#getFlags()
   */
  public MotionEventBuilder setFlags(int flags) {
    this.flags = flags;
    return this;
  }

  /**
   * Simple mechanism to add a pointer to the MotionEvent.
   *
   * <p>Can be called multiple times to add multiple pointers to the event.
   */
  public MotionEventBuilder setPointer(float x, float y) {
    PointerProperties pointerProperties = new PointerProperties();
    pointerProperties.id = pointerPropertiesList.size();
    PointerCoords pointerCoords = new PointerCoords();
    pointerCoords.x = x;
    pointerCoords.y = y;
    return setPointer(pointerProperties, pointerCoords);
  }

  /**
   * An expanded variant of {@link #setPointer(float, float)} that supports specifying all pointer
   * properties and coords data.
   */
  public MotionEventBuilder setPointer(
      PointerProperties pointerProperties, PointerCoords pointerCoords) {
    pointerPropertiesList.add(pointerProperties);
    pointerCoordsList.add(pointerCoords);
    return this;
  }

  /** Returns a MotionEvent with the provided data or reasonable defaults. */
  public MotionEvent build() {
    if (pointerPropertiesList.size() == 0) {
      setPointer(0, 0);
    }
    if (actionIndex != -1) {
      action = action | (actionIndex << MotionEvent.ACTION_POINTER_INDEX_SHIFT);
    }
    return MotionEvent.obtain(
        downTime,
        eventTime,
        action,
        pointerPropertiesList.size(),
        pointerPropertiesList.toArray(new PointerProperties[pointerPropertiesList.size()]),
        pointerCoordsList.toArray(new MotionEvent.PointerCoords[pointerCoordsList.size()]),
        metaState,
        buttonState,
        xPrecision,
        yPrecision,
        deviceId,
        edgeFlags,
        source,
        flags);
  }

  private static void checkState(boolean expression, String message) {
    if (!expression) {
      throw new IllegalStateException(message);
    }
  }
}