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

The following examples show how to use android.view.accessibility.AccessibilityEvent#getItemCount() . 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: ScrollFeedbackManager.java    From talkback with Apache License 2.0 6 votes vote down vote up
private CharSequence getDescriptionForPageEvent(
    AccessibilityEvent event, AccessibilityNodeInfo source) {
  final int fromIndex = (event.getFromIndex() + 1);
  final int itemCount = event.getItemCount();
  if ((fromIndex <= 0) || (itemCount <= 0)) {
    return null;
  }

  CharSequence pageTitle = getSelectedPageTitle(source);
  if (!TextUtils.isEmpty(pageTitle)) {
    CharSequence count =
        context.getString(R.string.template_viewpager_index_count_short, fromIndex, itemCount);

    SpannableStringBuilder output = new SpannableStringBuilder();
    StringBuilderUtils.appendWithSeparator(output, pageTitle, count);
    return output;
  }

  return context.getString(R.string.template_viewpager_index_count, fromIndex, itemCount);
}
 
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: ScrollFeedbackManager.java    From talkback with Apache License 2.0 5 votes vote down vote up
/**
 * Returns whether the event is a duplicate of the previous event, or the event is triggered by
 * auto-scroll.
 *
 * @param event The event from which information about the scroll position will be retrieved
 * @return {@code true} if the event is a duplicate of the previous event, or triggered by
 *     auto-scroll
 */
protected boolean isDuplicateScrollEventOrAutoScroll(AccessibilityEvent event) {
  int eventType = event.getEventType();
  if ((eventType != AccessibilityEventCompat.TYPE_WINDOW_CONTENT_CHANGED)
      && (eventType != AccessibilityEventCompat.TYPE_VIEW_SCROLLED)) {
    return false;
  }

  final int fromIndex = event.getFromIndex() + 1;
  final int itemCount = event.getItemCount();
  if (itemCount <= 0 || fromIndex <= 0) {
    return true;
  }

  EventId eventId;
  try {
    eventId = new EventId(event);
  } catch (Exception e) {
    return true;
  }

  final Integer cachedFromIndex = cachedFromValues.get(eventId);
  final Integer cachedItemCount = cachedItemCounts.get(eventId);

  if ((cachedFromIndex != null)
      && (cachedFromIndex == fromIndex)
      && (cachedItemCount != null)
      && (cachedItemCount == itemCount)) {
    // The from index hasn't changed, which means the event is coming
    // from a re-layout or resize and should not be spoken.
    return true;
  }

  // The behavior of put() for an existing key is unspecified, so we can't
  // recycle the old or new key nodes.
  cachedFromValues.put(eventId, fromIndex);
  cachedItemCounts.put(eventId, itemCount);

  return false;
}
 
Example 7
Source File: AccessibilityEventUtils.java    From talkback with Apache License 2.0 5 votes vote down vote up
/**
 * Returns the progress percentage from the event. The value will be in the range [0, 100], where
 * 100 is the maximum scroll amount.
 *
 * @param event The event from which to obtain the progress percentage.
 * @return The progress percentage.
 */
public static float getProgressPercent(AccessibilityEvent event) {
  if (event == null) {
    return 0.0f;
  }
  final int maxProgress = event.getItemCount();
  final int progress = event.getCurrentItemIndex();
  final float percent = (progress / (float) maxProgress);

  return (100.0f * Math.max(0.0f, Math.min(1.0f, percent)));
}
 
Example 8
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 9
Source File: InputFocusInterpreter.java    From talkback with Apache License 2.0 5 votes vote down vote up
/**
 * Gets target child node from the source AdapterView node.
 *
 * <p><strong>Note:</strong> Caller is responsible for recycling the returned node.
 */
private static @Nullable AccessibilityNodeInfoCompat getTargetChildFromAdapterView(
    AccessibilityEvent event) {
  AccessibilityNodeInfoCompat sourceNode = null;
  try {
    sourceNode = AccessibilityNodeInfoUtils.toCompat(event.getSource());
    if (sourceNode == null) {
      return null;
    }

    if (event.getItemCount() <= 0
        || event.getFromIndex() < 0
        || event.getCurrentItemIndex() < 0) {
      return null;
    }
    int index = event.getCurrentItemIndex() - event.getFromIndex();
    if (index < 0 || index >= sourceNode.getChildCount()) {
      return null;
    }
    AccessibilityNodeInfoCompat targetChildNode = sourceNode.getChild(index);

    // TODO: Think about to replace childNode check with sourceNode check.
    if ((targetChildNode == null)
        || !AccessibilityNodeInfoUtils.isTopLevelScrollItem(targetChildNode)) {
      AccessibilityNodeInfoUtils.recycleNodes(targetChildNode);
      return null;
    } else {
      return targetChildNode;
    }
  } finally {
    AccessibilityNodeInfoUtils.recycleNodes(sourceNode);
  }
}
 
Example 10
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 11
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 12
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 13
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 14
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 15
Source File: AccessibilityEventUtils.java    From brailleback with Apache License 2.0 4 votes vote down vote up
/**
 * @return If the <code>first</code> event is equal to the <code>second</code>.
 */
public static boolean eventEquals(AccessibilityEvent first, AccessibilityEvent second) {
    // TODO: The framework should implement AccessibilityEvent#equals()
    if (first == null || second == null) {
        return false;
    }
    if (first.getEventType() != second.getEventType()) {
        return false;
    }
    if (first.getPackageName() == null) {
        if (second.getPackageName() != null) {
            return false;
        }
    } else if (!first.getPackageName().equals(second.getPackageName())) {
        return false;
    }
    if (first.getClassName() == null) {
        if (second.getClassName() != null) {
            return false;
        }
    } else if (!first.getClassName().equals(second.getClassName())) {
        return false;
    }
    if (!first.getText().equals(second.getText())) {
        // The result of getText() is never null.
        return false;
    }
    if (first.getContentDescription() == null) {
        if (second.getContentDescription() != null) {
            return false;
        }
    } else if (!first.getContentDescription().equals(second.getContentDescription())) {
        return false;
    }
    if (first.getBeforeText() == null) {
        if (second.getBeforeText() != null) {
            return false;
        }
    } else if (!first.getBeforeText().equals(second.getBeforeText())) {
        return false;
    }
    if (first.getParcelableData() != null) {
        // Parcelable data may not implement equals() correctly.
        return false;
    }
    if (first.getAddedCount() != second.getAddedCount()) {
        return false;
    }
    if (first.isChecked() != second.isChecked()) {
        return false;
    }
    if (first.isEnabled() != second.isEnabled()) {
        return false;
    }
    if (first.getFromIndex() != second.getFromIndex()) {
        return false;
    }
    if (first.isFullScreen() != second.isFullScreen()) {
        return false;
    }
    if (first.getCurrentItemIndex() != second.getCurrentItemIndex()) {
        return false;
    }
    if (first.getItemCount() != second.getItemCount()) {
        return false;
    }
    if (first.isPassword() != second.isPassword()) {
        return false;
    }
    if (first.getRemovedCount() != second.getRemovedCount()) {
        return false;
    }
    if (first.getEventTime() != second.getEventTime()) {
        return false;
    }
    return true;
}
 
Example 16
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;
}