Пример #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();
  }