Example usage for android.view.accessibility AccessibilityEvent getItemCount

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

Introduction

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

Prototype

public int getItemCount() 

Source Link

Document

Gets the number of items that can be visited.

Usage

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

private float getProgressPercent(AccessibilityEvent event) {
    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)));
}

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.
 *//*from   www  . ja  va2s.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.
 *//*w  w  w. j  ava 2 s  .co 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.
 *///from  w  ww  .  ja v a2  s.  c om
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.google.android.marvin.mytalkback.speechrules.RuleSeekBar.java

@Override
public CharSequence format(Context context, AccessibilityNodeInfoCompat node, AccessibilityEvent event) {
    final SpannableStringBuilder output = new SpannableStringBuilder();
    final CharSequence text = super.format(context, node, event);
    final CharSequence formattedText = context.getString(R.string.template_seek_bar, text);

    StringBuilderUtils.appendWithSeparator(output, formattedText);

    // TODO: We need to be getting this information from the node.
    if ((event != null) && (event.getItemCount() > 0)) {
        final int percent = (100 * event.getCurrentItemIndex()) / event.getItemCount();
        final CharSequence formattedPercent = context.getString(R.string.template_percent, percent);

        StringBuilderUtils.appendWithSeparator(output, formattedPercent);
    }//from w  ww .  ja v  a  2 s  .c o  m

    return output;
}

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   w  w  w.  ja  v a2s.  c om

    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;
    }/*from w  w w  .  j a v  a 2 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);
    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;
    }/*from  www.  j  a  v  a  2s  .  co m*/

    // 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.speechrules.RuleSeekBar.java

@Override
public CharSequence format(Context context, AccessibilityNodeInfoCompat node, AccessibilityEvent event) {
    if (node == null || !node.isAccessibilityFocused()) {
        return "";
    }//from ww  w . ja va  2  s. c  o m

    final SpannableStringBuilder output = new SpannableStringBuilder();

    final CharSequence text = AccessibilityNodeInfoUtils.getNodeText(node);
    final CharSequence roleText = Role.getRoleDescriptionOrDefault(context, node);

    StringBuilderUtils.append(output, text, roleText);

    // TODO: We need to be getting this information from the node.
    if ((event != null) && (event.getItemCount() > 0)) {
        final int percent = (100 * event.getCurrentItemIndex()) / event.getItemCount();
        final CharSequence formattedPercent = context.getString(R.string.template_percent, percent);

        StringBuilderUtils.appendWithSeparator(output, formattedPercent);
    }

    return output;
}

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 www. jav  a2  s  .  com*/

    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;
}