@SuppressLint("NewApi")
 void sendAccessibilityEvent(View view) {
   try {
     AccessibilityManager accessibilityManager =
         (AccessibilityManager) getContext().getSystemService(Context.ACCESSIBILITY_SERVICE);
     if (mSendClickAccessibilityEvent && accessibilityManager.isEnabled()) {
       AccessibilityEvent event = AccessibilityEvent.obtain();
       event.setEventType(AccessibilityEvent.TYPE_VIEW_CLICKED);
       if (VERSION.SDK_INT >= 14) {
         view.onInitializeAccessibilityEvent(event);
       }
       view.dispatchPopulateAccessibilityEvent(event);
       accessibilityManager.sendAccessibilityEvent(event);
     }
   } catch (Exception e) {
   }
   mSendClickAccessibilityEvent = false;
 }
  /**
   * Sends the specified text to the {@link AccessibilityManager} to be spoken.
   *
   * @param view The source view.
   * @param text The text to speak.
   */
  public void announceForAccessibility(final View view, final CharSequence text) {
    if (!mAccessibilityManager.isEnabled()) {
      Log.e(TAG, "Attempted to speak when accessibility was disabled!");
      return;
    }

    // The following is a hack to avoid using the heavy-weight TextToSpeech
    // class. Instead, we're just forcing a fake AccessibilityEvent into
    // the screen reader to make it speak.
    final AccessibilityEvent event = AccessibilityEvent.obtain();

    event.setPackageName(PACKAGE);
    event.setClassName(CLASS);
    event.setEventTime(SystemClock.uptimeMillis());
    event.setEnabled(true);
    event.getText().add(text);

    // Platforms starting at SDK version 16 (Build.VERSION_CODES.JELLY_BEAN) should use
    // announce events.
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
      event.setEventType(AccessibilityEventCompat.TYPE_ANNOUNCEMENT);
    } else {
      event.setEventType(AccessibilityEvent.TYPE_VIEW_FOCUSED);
    }

    final ViewParent viewParent = view.getParent();
    if ((viewParent == null) || !(viewParent instanceof ViewGroup)) {
      Log.e(TAG, "Failed to obtain ViewParent in announceForAccessibility");
      return;
    }

    viewParent.requestSendAccessibilityEvent(view, event);
  }
Exemplo n.º 3
0
  @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;
  }