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

The following examples show how to use android.view.accessibility.AccessibilityEvent#getScrollY() . 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: ScrollEventInterpreter.java    From talkback with Apache License 2.0 6 votes vote down vote up
private boolean hasValidIndex(AccessibilityEvent event) {
  switch (event.getEventType()) {
    case AccessibilityEvent.TYPE_WINDOW_CONTENT_CHANGED:
      return (event.getFromIndex() != INDEX_UNDEFINED)
          && (event.getToIndex() != INDEX_UNDEFINED)
          && (event.getItemCount() > 0);
    case AccessibilityEvent.TYPE_VIEW_SCROLLED:
      boolean validScrollDelta = AccessibilityEventUtils.hasValidScrollDelta(event);
      return (event.getFromIndex() != INDEX_UNDEFINED)
          || (event.getToIndex() != INDEX_UNDEFINED)
          || (event.getScrollX() != INDEX_UNDEFINED)
          || (event.getScrollY() != INDEX_UNDEFINED)
          || validScrollDelta;
    default:
      return false;
  }
}
 
Example 2
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 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: ScrollEventInterpreter.java    From talkback with Apache License 2.0 5 votes vote down vote up
@SearchDirectionOrUnknown
private int getScrollDirection(NodeIdentifier sourceNodeIdentifier, AccessibilityEvent event) {
  PositionInfo positionInfo = cachedPositionInfo.get(sourceNodeIdentifier);
  if (positionInfo == null) {
    if (BuildVersionUtils.isAtLeastR()) {
      return getScrollDirectionFromDeltas(event);
    }
    return TraversalStrategy.SEARCH_FOCUS_UNKNOWN;
  }

  // Check scroll of AdapterViews
  if (event.getFromIndex() > positionInfo.fromIndex
      || event.getToIndex() > positionInfo.toIndex) {
    return TraversalStrategy.SEARCH_FOCUS_FORWARD;
  } else if (event.getFromIndex() < positionInfo.fromIndex
      || event.getToIndex() < positionInfo.toIndex) {
    return TraversalStrategy.SEARCH_FOCUS_BACKWARD;
  }

  // Check scroll of ScrollViews.
  if (event.getScrollX() > positionInfo.scrollX || event.getScrollY() > positionInfo.scrollY) {
    return TraversalStrategy.SEARCH_FOCUS_FORWARD;
  } else if (event.getScrollX() < positionInfo.scrollX
      || event.getScrollY() < positionInfo.scrollY) {
    return TraversalStrategy.SEARCH_FOCUS_BACKWARD;
  }

  if (BuildVersionUtils.isAtLeastR()) {
    return getScrollDirectionFromDeltas(event);
  }

  return TraversalStrategy.SEARCH_FOCUS_UNKNOWN;
}
 
Example 5
Source File: ScrollEventInterpreter.java    From talkback with Apache License 2.0 5 votes vote down vote up
PositionInfo(AccessibilityEvent event) {
  fromIndex = event.getFromIndex();
  toIndex = event.getToIndex();
  scrollX = event.getScrollX();
  scrollY = event.getScrollY();
  itemCount = event.getItemCount();
  scrollDeltaX = AccessibilityEventUtils.getScrollDeltaX(event);
  scrollDeltaY = AccessibilityEventUtils.getScrollDeltaY(event);
}
 
Example 6
Source File: LaunchApp.java    From PUMA with Apache License 2.0 5 votes vote down vote up
private ScrollDirection findScrollDirection() {
	AccessibilityNodeInfo localRoot = getRootNode();
	AccessibilityNodeInfo toScroll = getScrollableNode(localRoot);
	ScrollDirection sDir = new ScrollDirection();
	AccessibilityEvent event;
	boolean atBeginning;

	// Util.log(toScroll);

	// Test Up --> Down
	event = scrollAndWaitForLastEvent(toScroll, true, true);
	sDir.vertical = checkScrollEvent(event);

	if (!toScroll.getClassName().equals(ListView.class.getCanonicalName())) {
		// Test Left --> Right
		event = scrollAndWaitForLastEvent(toScroll, false, true);
		sDir.horizontal = checkScrollEvent(event);

		// Restore view position
		do {
			event = scrollAndWaitForLastEvent(toScroll, false, false);
			atBeginning = (event == null) || (event.getFromIndex() == 0) || (event.getScrollX() == 0);
		} while (!atBeginning);
	}

	// Restore view position
	do {
		event = scrollAndWaitForLastEvent(toScroll, true, false);
		atBeginning = (event == null) || (event.getFromIndex() == 0) || (event.getScrollY() == 0);
	} while (!atBeginning);

	return sDir;
}
 
Example 7
Source File: LaunchApp.java    From PUMA with Apache License 2.0 5 votes vote down vote up
private boolean compareScrollEvent(AccessibilityEvent event1, AccessibilityEvent event2) {
	boolean same = false;

	if (event1 == null && event2 == null) {
		same = true;
	} else if (event1 != null && event2 != null) {
		String s1 = event1.getFromIndex() + "," + event1.getToIndex() + "," + event1.getScrollX() + "," + event1.getScrollY();
		String s2 = event2.getFromIndex() + "," + event2.getToIndex() + "," + event2.getScrollX() + "," + event2.getScrollY();
		same = s1.equals(s2);
	}

	return same;
}
 
Example 8
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 9
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 10
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 11
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 12
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 13
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;
}
 
Example 14
Source File: LaunchApp.java    From PUMA with Apache License 2.0 4 votes vote down vote up
private boolean checkScrollEvent(AccessibilityEvent event) {
	return ((event != null) && (event.getFromIndex() > -1 || event.getScrollX() > -1 || event.getScrollY() > -1));
}