Пример #1
0
  /**
   * Formats an utterance from an {@link AccessibilityEvent}.
   *
   * @param event The event from which to format an utterance.
   * @return The formatted utterance.
   */
  private String formatUtterance(AccessibilityEvent event) {
    StringBuilder utterance = mUtterance;

    // clear the utterance before appending the formatted text
    utterance.delete(0, utterance.length());

    List<CharSequence> eventText = event.getText();

    // We try to get the event text if such
    if (!eventText.isEmpty()) {
      for (CharSequence subText : eventText) {
        utterance.append(subText);
        utterance.append(SPACE);
      }

      // here we do a bit of enhancement of the UI presentation by using the semantic
      // of the event source in the context of the Clock application
      if (CLASS_NAME_EDIT_TEXT.equals(event.getClassName())) {
        // if the source is an edit text box and we have a mapping based on
        // its position in the items of the container parent of the event source
        // we append that value as well. We say "XX hours" and "XX minutes".
        String resourceValue =
            getPositionMappedStringResource(event.getItemCount(), event.getCurrentItemIndex());
        if (resourceValue != null) {
          utterance.append(resourceValue);
        }
      }

      return utterance.toString();
    }

    // There is no event text but we try to get the content description which is
    // an optional attribute for describing a view (typically used with ImageView)
    CharSequence contentDescription = event.getContentDescription();
    if (contentDescription != null) {
      utterance.append(contentDescription);
      return utterance.toString();
    }

    // No text and content description for the plus and minus buttons, so we lookup
    // custom values based on the event's itemCount and currentItemIndex properties.
    CharSequence className = event.getClassName();
    if (CLASS_NAME_NUMBER_PICKER_BUTTON_ALARM_CLOCK.equals(className)
        || CLASS_NAME_NUMBER_PICKER_BUTTON_CLOCK.equals(className)) {
      String resourceValue =
          getPositionMappedStringResource(event.getItemCount(), event.getCurrentItemIndex());
      utterance.append(resourceValue);
    }

    return utterance.toString();
  }
 private AccessibilityEvent createEventForChild(int i, int j) {
   AccessibilityEvent accessibilityevent = AccessibilityEvent.obtain(j);
   accessibilityevent.setEnabled(true);
   accessibilityevent.setClassName(DEFAULT_CLASS_NAME);
   onPopulateEventForVirtualView(i, accessibilityevent);
   if (accessibilityevent.getText().isEmpty()
       && accessibilityevent.getContentDescription() == null) {
     throw new RuntimeException(
         "Callbacks must add text or a content description in populateEventForVirtualViewId()");
   } else {
     accessibilityevent.setPackageName(mView.getContext().getPackageName());
     AccessibilityEventCompat.asRecord(accessibilityevent).setSource(mView, i);
     return accessibilityevent;
   }
 }
  private AccessibilityEvent getEventForItem(T item, int eventType) {
    final AccessibilityEvent event = AccessibilityEvent.obtain(eventType);
    final AccessibilityRecordCompat record = new AccessibilityRecordCompat(event);
    final int virtualDescendantId = getIdForItem(item);

    // Ensure the client has good defaults.
    event.setEnabled(true);

    // Allow the client to populate the event.
    populateEventForItem(item, event);

    if (event.getText().isEmpty() && TextUtils.isEmpty(event.getContentDescription())) {
      throw new RuntimeException(
          "You must add text or a content description in populateEventForItem()");
    }

    // Don't allow the client to override these properties.
    event.setClassName(item.getClass().getName());
    event.setPackageName(mParentView.getContext().getPackageName());
    record.setSource(mParentView, virtualDescendantId);

    return event;
  }