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

The following examples show how to use android.view.accessibility.AccessibilityEvent#getToIndex() . 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: ScrollFeedbackManager.java    From talkback with Apache License 2.0 6 votes vote down vote up
private CharSequence getDescriptionForScrollEvent(AccessibilityEvent event) {
  // If the from index or item count are invalid, don't announce anything.
  final int fromIndex = (event.getFromIndex() + 1);
  final int itemCount = event.getItemCount();
  if ((fromIndex <= 0) || (itemCount <= 0)) {
    return null;
  }

  // If the to and from indices are the same, or if the to index is
  // invalid, only announce the item at the from index.
  final int toIndex = event.getToIndex() + 1;
  if ((fromIndex == toIndex) || (toIndex <= 0) || (toIndex > itemCount)) {
    return context.getString(R.string.template_scroll_from_count, fromIndex, itemCount);
  }

  // Announce the range of visible items.
  return context.getString(R.string.template_scroll_from_to_count, fromIndex, toIndex, itemCount);
}
 
Example 2
Source File: TextCursorManager.java    From talkback with Apache License 2.0 6 votes vote down vote up
private void processTextSelectionChange(AccessibilityEvent event) {
  AccessibilityNodeInfo node = event.getSource();
  if (node == null) {
    clear();
    return;
  }

  AccessibilityNodeInfoCompat compat = AccessibilityNodeInfoUtils.toCompat(node);
  if (compat.equals(mNode)) {
    compat.recycle();
    mPreviousCursorPosition = mCurrentCursorPosition;
    mCurrentCursorPosition = event.getToIndex();
  } else {
    clear();
    mNode = compat;
    mCurrentCursorPosition = event.getToIndex();
  }
}
 
Example 3
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 4
Source File: AccessibilityEventProcessor.java    From PUMA with Apache License 2.0 6 votes vote down vote up
private static void dump(AccessibilityEvent event) {
	AccessibilityNodeInfo source = event.getSource();
	if (source == null) {
		Util.err("event source: NULL");
		return;
	}

	Rect bounds = new Rect();
	source.getBoundsInScreen(bounds);
	int cnt = -1;
	if (source.getClassName().equals(ListView.class.getCanonicalName())) {
		cnt = event.getItemCount();
		int from = event.getFromIndex();
		int to = event.getToIndex();
		Util.log(event.getEventTime() + ": " + AccessibilityEvent.eventTypeToString(event.getEventType()) + "," + source.getClassName() + "," + cnt + ", [" + from + " --> " + to + "], "
				+ bounds.toShortString());
	} else {
		Util.log(event.getEventTime() + ": " + AccessibilityEvent.eventTypeToString(event.getEventType()) + "," + source.getClassName() + "," + bounds.toShortString());
	}
}
 
Example 5
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 6
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 7
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 8
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 9
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 10
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 11
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 12
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 13
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 14
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;
}