I'm developing application that views books. There is a screen (Activity) which shows a book. It has custom view, something similar to ViewSwitcher and every page is a bitmap that is rendered by a custom View.
Now I should implement accessibility function - book should be read by the phone (audio).
I've read Accessibility section here https://developer.android.com/guide/topics/ui/accessibility/index.html but it is not clear enough.
I use SupportLibrary for accessibility management and now I have this code in ViewGroup (which manages book pages). Code 1:
private class EditionPagesViewSwitcherAccessibilityDelegate extends AccessibilityDelegateCompat { private int mPageCount; private double[] mPageRange; @Override public void onInitializeAccessibilityEvent(final View host, final AccessibilityEvent event) { super.onInitializeAccessibilityEvent(host, event); event.setClassName(EditionPagesViewSwitcher.class.getName()); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH) { event.setScrollable(canScroll()); } if (event.getEventType() == AccessibilityEventCompat.TYPE_VIEW_SCROLLED && updatePageValues()) { event.setItemCount(mPageCount); // we use +1 because of user friendly numbers (from 1 not 0) event.setFromIndex((int) (mPageRange[0] + 1)); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH) { event.setToIndex((int) (mPageRange[1] + 1)); } } } @Override public void onInitializeAccessibilityNodeInfo(final View host, final AccessibilityNodeInfoCompat info) { super.onInitializeAccessibilityNodeInfo(host, info); info.setClassName(EditionPagesViewSwitcher.class.getName()); info.setScrollable(canScroll()); info.setLongClickable(true); if (canScrollForward()) { info.addAction(AccessibilityNodeInfoCompat.ACTION_SCROLL_FORWARD); } if (canScrollBackward()) { info.addAction(AccessibilityNodeInfoCompat.ACTION_SCROLL_BACKWARD); } } @Override public boolean performAccessibilityAction(final View host, final int action, final Bundle args) { if (super.performAccessibilityAction(host, action, args)) { return true; } switch (action) { case AccessibilityNodeInfoCompat.ACTION_SCROLL_FORWARD: { if (canScrollForward()) { showNext(); return true; } } return false; case AccessibilityNodeInfoCompat.ACTION_SCROLL_BACKWARD: { if (canScrollBackward()) { showPrevious(); return true; } } return false; } return false; }
Here is code from page view Code 2:
@Override public void onInitializeAccessibilityEvent(final View host, final AccessibilityEvent event) { super.onInitializeAccessibilityEvent(host, event); event.setClassName(EditionPageView.class.getName()); if (hasText()) { event.getText().add(getPageRangeText()); final String trimText = mSurfaceUpdateData.getPageText().trim(); if (trimText.length() > MAX_TEXT_LENGTH) { event.getText().add(trimText.substring(0, MAX_TEXT_LENGTH)); // event.getText().add(trimText.substring(MAX_TEXT_LENGTH, trimText.length())); } else { event.getText().add(trimText); } } } @Override public void onInitializeAccessibilityNodeInfo(final View host, final AccessibilityNodeInfoCompat info) { super.onInitializeAccessibilityNodeInfo(host, info); info.setClassName(EditionPageView.class.getName()); }
Because page text data loads asynchronous first time accessibility don't have any text while executes onInitializeAccessibilityEvent code. And then when data have been loaded I fire AccessibilityEvent.TYPE_VIEW_SELECTED and AccessibilityEvent.TYPE_VIEW_TEXT_CHANGED events. Then onInitializeAccessibilityEvent executes again and phone "read" book text.
So my questions:
UPDATED
Well. Here is some answers:
4.. According to the documentation of String.substring()
The first argument you pass is the start index in the original string, the second argument is the end index in the original string.
Example:
String text = "Hello"; partOfText = text.substring(2,text.length() - 1);
partOfText
equals to "llo" (the first char is index 0)
So by putting your constant MAX_TEXT_LENGTH
as a first argument, it would start at index 3800 to take out the substring.
http://developer.android.com/reference/java/lang/String.html#substring(int)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With