private static void populateEventFromJSON(AccessibilityEvent event, JSONObject message) { final JSONArray textArray = message.optJSONArray("text"); if (textArray != null) { for (int i = 0; i < textArray.length(); i++) event.getText().add(textArray.optString(i)); } event.setContentDescription(message.optString("description")); event.setEnabled(message.optBoolean("enabled", true)); event.setChecked(message.optBoolean("checked")); event.setPassword(message.optBoolean("password")); event.setAddedCount(message.optInt("addedCount", -1)); event.setRemovedCount(message.optInt("removedCount", -1)); event.setFromIndex(message.optInt("fromIndex", -1)); event.setItemCount(message.optInt("itemCount", -1)); event.setCurrentItemIndex(message.optInt("currentItemIndex", -1)); event.setBeforeText(message.optString("beforeText")); if (Versions.feature14Plus) { event.setToIndex(message.optInt("toIndex", -1)); event.setScrollable(message.optBoolean("scrollable")); event.setScrollX(message.optInt("scrollX", -1)); event.setScrollY(message.optInt("scrollY", -1)); } if (Versions.feature15Plus) { event.setMaxScrollX(message.optInt("maxScrollX", -1)); event.setMaxScrollY(message.optInt("maxScrollY", -1)); } }
@Override public boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event) { boolean populated = false; // This is an exceptional case which occurs when a window gets the // focus and sends a focus event via its focused child to announce // current focus/selection. CarouselAdapter fires selection but not focus // events so we change the event type here. if (event.getEventType() == AccessibilityEvent.TYPE_VIEW_FOCUSED) { event.setEventType(AccessibilityEvent.TYPE_VIEW_SELECTED); } // we send selection events only from CarouselAdapter to avoid // generation of such event for each child View selectedView = getSelectedView(); if (selectedView != null) { populated = selectedView.dispatchPopulateAccessibilityEvent(event); } if (!populated) { if (selectedView != null) { event.setEnabled(selectedView.isEnabled()); } event.setItemCount(getCount()); event.setCurrentItemIndex(getSelectedItemPosition()); } return populated; }
// BEGIN_INCLUDE (on_populate_accessibility_event) @Override @TargetApi(Build.VERSION_CODES.ICE_CREAM_SANDWICH) public void onPopulateAccessibilityEvent(AccessibilityEvent event) { super.onPopulateAccessibilityEvent(event); // Detect what type of accessibility event is being passed in. int eventType = event.getEventType(); // Common case: The user has interacted with our view in some way. State may or may not // have been changed. Read out the current status of the view. // // We also set some other metadata which is not used by TalkBack, but could be used by // other TTS engines. if (eventType == AccessibilityEvent.TYPE_VIEW_SELECTED || eventType == AccessibilityEvent.TYPE_VIEW_ACCESSIBILITY_FOCUSED) { event.getText().add("Mode selected: " + Integer.toString(mActiveSelection + 1) + "."); event.setItemCount(SELECTION_COUNT); event.setCurrentItemIndex(mActiveSelection); } // When a user first focuses on our view, we'll also read out some simple instructions to // make it clear that this is an interactive element. if (eventType == AccessibilityEvent.TYPE_VIEW_ACCESSIBILITY_FOCUSED) { event.getText().add("Tap to change."); } }
@Override public void onInitializeAccessibilityEvent(AccessibilityEvent event) { super.onInitializeAccessibilityEvent(event); event.setScrollable(isScrollableForAccessibility()); View selectedView = getSelectedView(); if (selectedView != null) { event.setEnabled(selectedView.isEnabled()); } event.setCurrentItemIndex(getSelectedItemPosition()); event.setFromIndex(getFirstVisiblePosition()); event.setToIndex(getLastVisiblePosition()); event.setItemCount(getCount()); }