Java Code Examples for android.view.accessibility.AccessibilityEvent#getMaxScrollY()

The following examples show how to use android.view.accessibility.AccessibilityEvent#getMaxScrollY() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: AccessibilityScrollData.java    From appium-uiautomator2-server with Apache License 2.0 5 votes vote down vote up
public AccessibilityScrollData(AccessibilityEvent event) {
    this.scrollX = event.getScrollX();
    this.scrollY = event.getScrollY();
    this.maxScrollX = event.getMaxScrollX();
    this.maxScrollY = event.getMaxScrollY();
    this.fromIndex = event.getFromIndex();
    this.toIndex = event.getToIndex();
    this.itemCount = event.getItemCount();
}
 
Example 2
Source File: EventFilter.java    From talkback with Apache License 2.0 5 votes vote down vote up
private boolean isValidScrollEvent(AccessibilityEvent event) {
  AccessibilityNodeInfo source = event.getSource();
  if (source == null) {
    return true; // Cannot check source validity, so assume that it's scrollable.
  }

  boolean valid =
      source.isScrollable() || event.getMaxScrollX() != -1 || event.getMaxScrollY() != -1;
  source.recycle();
  return valid;
}
 
Example 3
Source File: AccessibilityEventUtils.java    From talkback with Apache License 2.0 5 votes vote down vote up
/**
 * Returns a floating point value representing the scroll position of an {@link
 * AccessibilityEvent}. This value may be outside the range {0..1}. If there's no valid way to
 * obtain a position, this method returns the default value.
 *
 * @param event The event from which to obtain the scroll position.
 * @param defaultValue Value to return if there is no valid scroll position from the event.
 * @return A floating point value representing the scroll position.
 */
public static float getScrollPosition(AccessibilityEvent event, float defaultValue) {
  if (event == null) {
    return defaultValue;
  }

  final int itemCount = event.getItemCount();
  final int fromIndex = event.getFromIndex();

  // First, attempt to use (fromIndex / itemCount).
  if ((fromIndex >= 0) && (itemCount > 0)) {
    return (fromIndex / (float) itemCount);
  }

  final int scrollY = event.getScrollY();
  final int maxScrollY = event.getMaxScrollY();

  // Next, attempt to use (scrollY / maxScrollY). This will fail if the
  // getMaxScrollX() method is not available.
  if ((scrollY >= 0) && (maxScrollY > 0)) {
    return (scrollY / (float) maxScrollY);
  }

  // Finally, attempt to use (scrollY / itemCount).
  // TODO: Investigate if it is still needed.
  if ((scrollY >= 0) && (itemCount > 0) && (scrollY <= itemCount)) {
    return (scrollY / (float) itemCount);
  }

  return defaultValue;
}
 
Example 4
Source File: MergeNodeInfo.java    From PUMA with Apache License 2.0 5 votes vote down vote up
public MergeNodeInfo(AccessibilityEvent event) {
	this.source = event.getSource();

	this.fromIndex = event.getFromIndex();
	this.toIndex = event.getToIndex();

	this.scrollX = event.getScrollX();
	this.scrollY = event.getScrollY();

	this.maxScrollX = event.getMaxScrollX();
	this.maxScrollY = event.getMaxScrollY();
}
 
Example 5
Source File: InteractionController.java    From za-Farmer with MIT License 4 votes vote down vote up
/**
 * Handle swipes in any direction where the result is a scroll event. This call blocks
 * until the UI has fired a scroll event or timeout.
 * @param downX
 * @param downY
 * @param upX
 * @param upY
 * @param steps
 * @return true if we are not at the beginning or end of the scrollable view.
 */
public boolean scrollSwipe(final int downX, final int downY, final int upX, final int upY,
        final int steps) {
    Log.d(LOG_TAG, "scrollSwipe (" +  downX + ", " + downY + ", " + upX + ", "
            + upY + ", " + steps +")");

    Runnable command = new Runnable() {
        @Override
        public void run() {
            swipe(downX, downY, upX, upY, steps);
        }
    };

    // Collect all accessibility events generated during the swipe command and get the
    // last event
    ArrayList<AccessibilityEvent> events = new ArrayList<AccessibilityEvent>();
    runAndWaitForEvents(command,
            new EventCollectingPredicate(AccessibilityEvent.TYPE_VIEW_SCROLLED, events),
            Configurator.getInstance().getScrollAcknowledgmentTimeout());

    AccessibilityEvent event = getLastMatchingEvent(events,
            AccessibilityEvent.TYPE_VIEW_SCROLLED);

    if (event == null) {
        // end of scroll since no new scroll events received
        recycleAccessibilityEvents(events);
        return false;
    }

    // AdapterViews have indices we can use to check for the beginning.
    boolean foundEnd = false;
    if (event.getFromIndex() != -1 && event.getToIndex() != -1 && event.getItemCount() != -1) {
        foundEnd = event.getFromIndex() == 0 ||
                (event.getItemCount() - 1) == event.getToIndex();
        Log.d(LOG_TAG, "scrollSwipe reached scroll end: " + foundEnd);
    } else if (event.getScrollX() != -1 && event.getScrollY() != -1) {
        // Determine if we are scrolling vertically or horizontally.
        if (downX == upX) {
            // Vertical
            foundEnd = event.getScrollY() == 0 ||
                    event.getScrollY() == event.getMaxScrollY();
            Log.d(LOG_TAG, "Vertical scrollSwipe reached scroll end: " + foundEnd);
        } else if (downY == upY) {
            // Horizontal
            foundEnd = event.getScrollX() == 0 ||
                    event.getScrollX() == event.getMaxScrollX();
            Log.d(LOG_TAG, "Horizontal scrollSwipe reached scroll end: " + foundEnd);
        }
    }
    recycleAccessibilityEvents(events);
    return !foundEnd;
}
 
Example 6
Source File: Until.java    From za-Farmer with MIT License 4 votes vote down vote up
/**
 * Returns a condition that depends on a scroll having reached the end in the given
 * {@code direction}.
 *
 * @param direction The direction of the scroll.
 */
public static EventCondition<Boolean> scrollFinished(final Direction direction) {
    return new EventCondition<Boolean>() {
        private Direction mDirection = direction;
        private Boolean mResult = null;

        @Override
        Boolean apply(AccessibilityEvent event) {
            if (event.getFromIndex() != -1 && event.getToIndex() != -1 &&
                    event.getItemCount() != -1) {

                switch (mDirection) {
                    case UP:
                        mResult = (event.getFromIndex() == 0);
                        break;
                    case DOWN:
                        mResult = (event.getToIndex() == event.getItemCount() - 1);
                        break;
                    case LEFT:
                        mResult = (event.getFromIndex() == 0);
                        break;
                    case RIGHT:
                        mResult = (event.getToIndex() == event.getItemCount() - 1);
                        break;
                    default:
                        throw new IllegalArgumentException("Invalid Direction");
                }
            } else if (event.getScrollX() != -1 && event.getScrollY() != -1) {
                switch (mDirection) {
                    case UP:
                        mResult = (event.getScrollY() == 0);
                        break;
                    case DOWN:
                        mResult = (event.getScrollY() == event.getMaxScrollY());
                        break;
                    case LEFT:
                        mResult = (event.getScrollX() == 0);
                        break;
                    case RIGHT:
                        mResult = (event.getScrollX() == event.getMaxScrollX());
                        break;
                    default:
                        throw new IllegalArgumentException("Invalid Direction");
                }
            }

            // Keep listening for events until the result is set to true (we reached the end)
            return Boolean.TRUE.equals(mResult);
        }

        @Override
        Boolean getResult() {
            // If we didn't recieve any scroll events (mResult == null), assume we're already at
            // the end and return true.
            return mResult == null || mResult;
        }
    };
}
 
Example 7
Source File: InteractionController.java    From JsDroidCmd with Mozilla Public License 2.0 4 votes vote down vote up
/**
 * Handle swipes in any direction where the result is a scroll event. This
 * call blocks until the UI has fired a scroll event or timeout.
 * 
 * @param downX
 * @param downY
 * @param upX
 * @param upY
 * @param steps
 * @return true if we are not at the beginning or end of the scrollable
 *         view.
 */
public boolean scrollSwipe(final int downX, final int downY, final int upX,
		final int upY, final int steps) {
	Runnable command = new Runnable() {
		@Override
		public void run() {
			swipe(downX, downY, upX, upY, steps);
		}
	};

	// Collect all accessibility events generated during the swipe command
	// and get the
	// last event
	ArrayList<AccessibilityEvent> events = new ArrayList<AccessibilityEvent>();
	runAndWaitForEvents(command, new EventCollectingPredicate(
			AccessibilityEvent.TYPE_VIEW_SCROLLED, events), Configurator
			.getInstance().getScrollAcknowledgmentTimeout());

	AccessibilityEvent event = getLastMatchingEvent(events,
			AccessibilityEvent.TYPE_VIEW_SCROLLED);

	if (event == null) {
		// end of scroll since no new scroll events received
		recycleAccessibilityEvents(events);
		return false;
	}

	// AdapterViews have indices we can use to check for the beginning.
	boolean foundEnd = false;
	if (event.getFromIndex() != -1 && event.getToIndex() != -1
			&& event.getItemCount() != -1) {
		foundEnd = event.getFromIndex() == 0
				|| (event.getItemCount() - 1) == event.getToIndex();
		Log.d(LOG_TAG, "scrollSwipe reached scroll end: " + foundEnd);
	} else if (event.getScrollX() != -1 && event.getScrollY() != -1) {
		// Determine if we are scrolling vertically or horizontally.
		if (downX == upX) {
			// Vertical
			foundEnd = event.getScrollY() == 0
					|| event.getScrollY() == event.getMaxScrollY();
			Log.d(LOG_TAG, "Vertical scrollSwipe reached scroll end: "
					+ foundEnd);
		} else if (downY == upY) {
			// Horizontal
			foundEnd = event.getScrollX() == 0
					|| event.getScrollX() == event.getMaxScrollX();
			Log.d(LOG_TAG, "Horizontal scrollSwipe reached scroll end: "
					+ foundEnd);
		}
	}
	recycleAccessibilityEvents(events);
	return !foundEnd;
}
 
Example 8
Source File: Until.java    From JsDroidCmd with Mozilla Public License 2.0 4 votes vote down vote up
/**
 * Returns a condition that depends on a scroll having reached the end in the given
 * {@code direction}.
 *
 * @param direction The direction of the scroll.
 */
public static EventCondition<Boolean> scrollFinished(final Direction direction) {
    return new EventCondition<Boolean>() {
        private Direction mDirection = direction;
        private Boolean mResult = null;

        @Override
        Boolean apply(AccessibilityEvent event) {
            if (event.getFromIndex() != -1 && event.getToIndex() != -1 &&
                    event.getItemCount() != -1) {

                switch (mDirection) {
                    case UP:
                        mResult = (event.getFromIndex() == 0);
                        break;
                    case DOWN:
                        mResult = (event.getToIndex() == event.getItemCount() - 1);
                        break;
                    case LEFT:
                        mResult = (event.getFromIndex() == 0);
                        break;
                    case RIGHT:
                        mResult = (event.getToIndex() == event.getItemCount() - 1);
                        break;
                    default:
                        throw new IllegalArgumentException("Invalid Direction");
                }
            } else if (event.getScrollX() != -1 && event.getScrollY() != -1) {
                switch (mDirection) {
                    case UP:
                        mResult = (event.getScrollY() == 0);
                        break;
                    case DOWN:
                        mResult = (event.getScrollY() == event.getMaxScrollY());
                        break;
                    case LEFT:
                        mResult = (event.getScrollX() == 0);
                        break;
                    case RIGHT:
                        mResult = (event.getScrollX() == event.getMaxScrollX());
                        break;
                    default:
                        throw new IllegalArgumentException("Invalid Direction");
                }
            }

            // Keep listening for events until the result is set to true (we reached the end)
            return Boolean.TRUE.equals(mResult);
        }

        @Override
        Boolean getResult() {
            // If we didn't recieve any scroll events (mResult == null), assume we're already at
            // the end and return true.
            return mResult == null || mResult;
        }
    };
}
 
Example 9
Source File: MyInteractionController.java    From PUMA with Apache License 2.0 4 votes vote down vote up
/**
 * Handle swipes in any direction where the result is a scroll event. This call blocks
 * until the UI has fired a scroll event or timeout.
 * @param downX
 * @param downY
 * @param upX
 * @param upY
 * @param steps
 * @return true if we are not at the beginning or end of the scrollable view.
 */
public boolean scrollSwipe(final int downX, final int downY, final int upX, final int upY, final int steps) {
	Log.d(LOG_TAG, "scrollSwipe (" + downX + ", " + downY + ", " + upX + ", " + upY + ", " + steps + ")");

	Runnable command = new Runnable() {
		@Override
		public void run() {
			swipe(downX, downY, upX, upY, steps);
		}
	};

	// Collect all accessibility events generated during the swipe command and get the
	// last event
	ArrayList<AccessibilityEvent> events = new ArrayList<AccessibilityEvent>();
	runAndWaitForEvents(command, new EventCollectingPredicate(AccessibilityEvent.TYPE_VIEW_SCROLLED, events), Configurator.getInstance().getScrollAcknowledgmentTimeout());

	AccessibilityEvent event = getLastMatchingEvent(events, AccessibilityEvent.TYPE_VIEW_SCROLLED);

	if (event == null) {
		// end of scroll since no new scroll events received
		recycleAccessibilityEvents(events);
		return false;
	}

	// AdapterViews have indices we can use to check for the beginning.
	boolean foundEnd = false;
	if (event.getFromIndex() != -1 && event.getToIndex() != -1 && event.getItemCount() != -1) {
		foundEnd = event.getFromIndex() == 0 || (event.getItemCount() - 1) == event.getToIndex();
		Log.d(LOG_TAG, "scrollSwipe reached scroll end: " + foundEnd);
	} else if (event.getScrollX() != -1 && event.getScrollY() != -1) {
		// Determine if we are scrolling vertically or horizontally.
		if (downX == upX) {
			// Vertical
			foundEnd = event.getScrollY() == 0 || event.getScrollY() == event.getMaxScrollY();
			Log.d(LOG_TAG, "Vertical scrollSwipe reached scroll end: " + foundEnd);
		} else if (downY == upY) {
			// Horizontal
			foundEnd = event.getScrollX() == 0 || event.getScrollX() == event.getMaxScrollX();
			Log.d(LOG_TAG, "Horizontal scrollSwipe reached scroll end: " + foundEnd);
		}
	}
	recycleAccessibilityEvents(events);
	return !foundEnd;
}