Example usage for android.view.accessibility AccessibilityEvent getFromIndex

List of usage examples for android.view.accessibility AccessibilityEvent getFromIndex

Introduction

In this page you can find the example usage for android.view.accessibility AccessibilityEvent getFromIndex.

Prototype

public int getFromIndex() 

Source Link

Document

Gets the index of the first character of the changed sequence, or the beginning of a text selection or the index of the first visible item when scrolling.

Usage

From source file:com.google.android.marvin.mytalkback.formatter.ScrollFormatter.java

/**
 * 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 0.5.
 *
 * @param event The event from which to obtain the scroll position.
 * @return A floating point value representing the scroll position.
 *///w w  w  . j a v a  2 s.  c  o m
private float getScrollPosition(AccessibilityEvent event) {
    final AccessibilityRecordCompat record = new AccessibilityRecordCompat(event);
    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 = record.getScrollY();
    final int maxScrollY = record.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(alanv): Hack from previous versions -- is it still needed?
    if ((scrollY >= 0) && (itemCount > 0) && (scrollY <= itemCount)) {
        return (scrollY / (float) itemCount);
    }

    return 0.5f;
}

From source file:com.android.screenspeak.formatter.ScrollFormatter.java

/**
 * 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 0.5.
 *
 * @param event The event from which to obtain the scroll position.
 * @return A floating point value representing the scroll position.
 *//*from w  w  w.j  ava 2s .  c o m*/
private float getScrollPosition(AccessibilityEvent event) {
    final AccessibilityRecordCompat record = AccessibilityEventCompat.asRecord(event);
    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 = record.getScrollY();
    final int maxScrollY = record.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(AV): Hack from previous versions -- is it still needed?
    if ((scrollY >= 0) && (itemCount > 0) && (scrollY <= itemCount)) {
        return (scrollY / (float) itemCount);
    }

    return 0.5f;
}

From source file:com.android.talkback.formatter.ScrollFormatter.java

/**
 * 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 0.5.
 *
 * @param event The event from which to obtain the scroll position.
 * @return A floating point value representing the scroll position.
 *///ww  w  .ja  v a  2s.  com
private float getScrollPosition(AccessibilityEvent event) {
    final AccessibilityRecordCompat record = AccessibilityEventCompat.asRecord(event);
    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 = record.getScrollY();
    final int maxScrollY = record.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: Hack from previous versions -- is it still needed?
    if ((scrollY >= 0) && (itemCount > 0) && (scrollY <= itemCount)) {
        return (scrollY / (float) itemCount);
    }

    return 0.5f;
}

From source file:com.android.talkback.eventprocessor.ProcessorScrollPosition.java

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;
    }//from ww  w .ja  va  2 s . c o m

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

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

    return mContext.getString(R.string.template_viewpager_index_count, fromIndex, itemCount);
}

From source file:com.android.screenspeak.eventprocessor.ProcessorScrollPosition.java

private boolean shouldIgnoreUpdateListEvent(AccessibilityEvent event) {
    // Don't speak during full-screen read.
    if (mFullScreenReadController.isActive()) {
        return true;
    }//  w  w w.  j av  a  2s .c om

    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 = mCachedFromValues.get(eventId);
    final Integer cachedItemCount = mCachedItemCounts.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.
    mCachedFromValues.put(eventId, fromIndex);
    mCachedItemCounts.put(eventId, itemCount);
    return false;
}

From source file:com.android.talkback.eventprocessor.ProcessorScrollPosition.java

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;
    }// ww  w. j  av  a 2 s .c om

    // 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 mContext.getString(R.string.template_scroll_from_count, fromIndex, itemCount);
    }

    // Announce the range of visible items.
    return mContext.getString(R.string.template_scroll_from_to_count, fromIndex, toIndex, itemCount);
}

From source file:com.android.talkback.eventprocessor.ProcessorScrollPosition.java

private boolean shouldIgnoreUpdateListEvent(AccessibilityEvent event) {
    // Don't speak during full-screen read.
    if (mFullScreenReadController.isActive()) {
        return true;
    }//from  w ww  .  j a v a2  s  .  c o  m

    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 = mCachedFromValues.get(eventId);
    final Integer cachedItemCount = mCachedItemCounts.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.
    mCachedFromValues.put(eventId, fromIndex);
    mCachedItemCounts.put(eventId, itemCount);

    // Allow the list indices to be cached, but don't actually speak after auto-scroll.
    if (mAutoScrollNode != null) {
        AccessibilityNodeInfo source = event.getSource();
        if (source != null) {
            try {
                if (source.equals(mAutoScrollNode.getInfo())) {
                    mAutoScrollNode.recycle();
                    mAutoScrollNode = null;
                    return true;
                }
            } finally {
                source.recycle();
            }
        }
    }

    return false;
}

From source file:com.android.screenspeak.eventprocessor.ProcessorScrollPosition.java

private CharSequence getDescriptionForScrollEvent(AccessibilityEvent event) {
    // If the event has text, use that by default.
    final CharSequence text = AccessibilityEventUtils.getEventTextOrDescription(event);
    if (!TextUtils.isEmpty(text)) {
        return text;
    }/*ww w  .  j  a va  2  s  . c om*/

    // 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 mContext.getString(R.string.template_scroll_from_count, fromIndex, itemCount);
    }

    // Announce the range of visible items.
    return mContext.getString(R.string.template_scroll_from_to_count, fromIndex, toIndex, itemCount);
}

From source file:com.android.screenspeak.SavedNode.java

@Override
public void onAccessibilityEvent(AccessibilityEvent event) {
    switch (event.getEventType()) {
    case AccessibilityEvent.TYPE_WINDOWS_CHANGED:
        clearCache();// w  ww.j  av a2s .co m
        break;
    case AccessibilityEvent.TYPE_VIEW_TEXT_SELECTION_CHANGED:
        AccessibilityNodeInfo source = event.getSource();
        if (source != null) {
            AccessibilityNodeInfoCompat copyNode = new AccessibilityNodeInfoCompat(
                    AccessibilityNodeInfo.obtain(source));
            Selection selection = new Selection(event.getFromIndex(), event.getToIndex());
            mSelectionCache.put(copyNode, selection);
        }
        break;
    }
}

From source file:com.google.android.marvin.mytalkback.ProcessorScrollPosition.java

private CharSequence getDescriptionForScrollEvent(AccessibilityEvent event) {
    // If the event has text, use that by default.
    final CharSequence text = AccessibilityEventUtils.getEventTextOrDescription(event);
    if (!TextUtils.isEmpty(text)) {
        return text;
    }// w  ww. j av a 2  s. c o  m

    // 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 = (AccessibilityEventCompatUtils.getToIndex(event) + 1);
    if ((fromIndex == toIndex) || (toIndex <= 0) || (toIndex > itemCount)) {
        return mContext.getString(R.string.template_scroll_from_count, fromIndex, itemCount);
    }

    // Announce the range of visible items.
    return mContext.getString(R.string.template_scroll_from_to_count, fromIndex, toIndex, itemCount);
}