Java Code Examples for android.view.accessibility.AccessibilityEvent#obtain()

The following examples show how to use android.view.accessibility.AccessibilityEvent#obtain() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: Event.java    From talkback with Apache License 2.0 6 votes vote down vote up
/**
 * Returns an Event instance, or null. Should only be called by this class and sub-classes.
 *
 * <p>Uses factory argument to create sub-class instances, without creating unnecessary instances
 * when result should be null. Method is protected so that it can be called by sub-classes without
 * duplicating null-checking logic.
 *
 * @param eventArg wrapped event info, which caller may need to recycle
 * @param copy flag whether to wrap a copy of eventArg, that caller must recycle
 * @param own flag whether wrapped event will be recycled
 * @param factory creates instances of Event or sub-classes
 */
@Nullable
protected static <T extends Event> T construct(
    @Nullable AccessibilityEvent eventArg, boolean copy, boolean own, Factory<T> factory) {

  if (copy && !own) {
    throw new IllegalStateException("Cannot create Event that wraps an un-owned copy.");
  }
  if (eventArg == null) {
    return null;
  }

  T instance = factory.create();
  Event instanceBase = instance;
  instanceBase.eventBare = copy ? AccessibilityEvent.obtain(eventArg) : eventArg;
  instanceBase.isEventOwner = own;
  return instance;
}
 
Example 2
Source File: BrowserAccessibilityManager.java    From android-chromium with BSD 2-Clause "Simplified" License 6 votes vote down vote up
private void sendAccessibilityEvent(int virtualViewId, int eventType) {
    if (!mAccessibilityManager.isEnabled() || mNativeObj == 0) return;

    final AccessibilityEvent event = AccessibilityEvent.obtain(eventType);
    event.setPackageName(mContentViewCore.getContext().getPackageName());
    int rootId = nativeGetRootId(mNativeObj);
    if (virtualViewId == rootId) {
        virtualViewId = View.NO_ID;
    }
    event.setSource(mView, virtualViewId);
    if (!nativePopulateAccessibilityEvent(mNativeObj, event, virtualViewId, eventType)) return;

    // This is currently needed if we want Android to draw the yellow box around
    // the item that has accessibility focus. In practice, this doesn't seem to slow
    // things down, because it's only called when the accessibility focus moves.
    // TODO(dmazzoni): remove this if/when Android framework fixes bug.
    mContentViewCore.getContainerView().postInvalidate();

    mContentViewCore.getContainerView().requestSendAccessibilityEvent(mView, event);
}
 
Example 3
Source File: ExploreByTouchHelper.java    From adt-leanback-support with Apache License 2.0 6 votes vote down vote up
/**
 * Constructs and returns an {@link AccessibilityEvent} populated with
 * information about the specified item.
 *
 * @param virtualViewId The virtual view id for the item for which to
 *            construct an event.
 * @param eventType The type of event to construct.
 * @return An {@link AccessibilityEvent} populated with information about
 *         the specified item.
 */
private AccessibilityEvent createEventForChild(int virtualViewId, int eventType) {
    final AccessibilityEvent event = AccessibilityEvent.obtain(eventType);
    event.setEnabled(true);
    event.setClassName(DEFAULT_CLASS_NAME);

    // Allow the client to populate the event.
    onPopulateEventForVirtualView(virtualViewId, event);

    // Make sure the developer is following the rules.
    if (event.getText().isEmpty() && (event.getContentDescription() == null)) {
        throw new RuntimeException("Callbacks must add text or a content description in "
                + "populateEventForVirtualViewId()");
    }

    // Don't allow the client to override these properties.
    event.setPackageName(mView.getContext().getPackageName());

    final AccessibilityRecordCompat record = AccessibilityEventCompat.asRecord(event);
    record.setSource(mView, virtualViewId);

    return event;
}
 
Example 4
Source File: BrowserAccessibilityManager.java    From 365browser with Apache License 2.0 6 votes vote down vote up
private AccessibilityEvent buildAccessibilityEvent(int virtualViewId, int eventType) {
    // If we don't have any frame info, then the virtual hierarchy
    // doesn't exist in the view of the Android framework, so should
    // never send any events.
    if (!mAccessibilityManager.isEnabled() || mNativeObj == 0
            || !isFrameInfoInitialized()) {
        return null;
    }

    // This is currently needed if we want Android to visually highlight
    // the item that has accessibility focus. In practice, this doesn't seem to slow
    // things down, because it's only called when the accessibility focus moves.
    // TODO(dmazzoni): remove this if/when Android framework fixes bug.
    mView.postInvalidate();

    final AccessibilityEvent event = AccessibilityEvent.obtain(eventType);
    event.setPackageName(mContentViewCore.getContext().getPackageName());
    event.setSource(mView, virtualViewId);
    if (!nativePopulateAccessibilityEvent(mNativeObj, event, virtualViewId, eventType)) {
        event.recycle();
        return null;
    }
    return event;
}
 
Example 5
Source File: DragLayer.java    From Trebuchet with GNU General Public License v3.0 5 votes vote down vote up
private void sendTapOutsideFolderAccessibilityEvent(boolean isEditingName) {
    AccessibilityManager accessibilityManager = (AccessibilityManager)
            getContext().getSystemService(Context.ACCESSIBILITY_SERVICE);
    if (accessibilityManager.isEnabled()) {
        int stringId = isEditingName ? R.string.folder_tap_to_rename : R.string.folder_tap_to_close;
        AccessibilityEvent event = AccessibilityEvent.obtain(
                AccessibilityEvent.TYPE_VIEW_FOCUSED);
        onInitializeAccessibilityEvent(event);
        event.getText().add(getContext().getString(stringId));
        accessibilityManager.sendAccessibilityEvent(event);
    }
}
 
Example 6
Source File: AccessibilityEventProcessor.java    From talkback with Apache License 2.0 5 votes vote down vote up
public void postProcessEvent(AccessibilityEvent event, EventId eventId) {
  AccessibilityEvent eventCopy = AccessibilityEvent.obtain(event);
  EventIdAnd<AccessibilityEvent> eventAndId =
      new EventIdAnd<AccessibilityEvent>(eventCopy, eventId);
  Message msg = obtainMessage(MESSAGE_WHAT_PROCESS_EVENT, eventAndId);
  sendMessageDelayed(msg, EVENT_PROCESSING_DELAY);
}
 
Example 7
Source File: NumberPicker.java    From DateTimePicker with Apache License 2.0 5 votes vote down vote up
private void sendAccessibilityEventForVirtualText(int eventType) {
    //if (AccessibilityManager.getInstance(mContext).isEnabled()) {
    if (accessibilityManager != null && accessibilityManager.isEnabled()) {
        AccessibilityEvent event = AccessibilityEvent.obtain(eventType);
        mInputText.onInitializeAccessibilityEvent(event);
        mInputText.onPopulateAccessibilityEvent(event);
        AccessibilityRecordCompat.setSource(event, NumberPicker.this, VIRTUAL_VIEW_ID_INPUT);
        // event.setSource(NumberPicker.this, VIRTUAL_VIEW_ID_INPUT);
        requestSendAccessibilityEvent(NumberPicker.this, event);
    }
}
 
Example 8
Source File: AdapterView.java    From letv with Apache License 2.0 5 votes vote down vote up
@TargetApi(14)
public boolean onRequestSendAccessibilityEvent(View child, AccessibilityEvent event) {
    if (!super.onRequestSendAccessibilityEvent(child, event)) {
        return false;
    }
    AccessibilityEvent record = AccessibilityEvent.obtain();
    onInitializeAccessibilityEvent(record);
    child.dispatchPopulateAccessibilityEvent(record);
    event.appendRecord(record);
    return true;
}
 
Example 9
Source File: Folder.java    From TurboLauncher with Apache License 2.0 5 votes vote down vote up
private void sendCustomAccessibilityEvent(int type, String text) {
    AccessibilityManager accessibilityManager = (AccessibilityManager)
            getContext().getSystemService(Context.ACCESSIBILITY_SERVICE);
    if (accessibilityManager.isEnabled()) {
        AccessibilityEvent event = AccessibilityEvent.obtain(type);
        onInitializeAccessibilityEvent(event);
        event.getText().add(text);
        accessibilityManager.sendAccessibilityEvent(event);
    }
}
 
Example 10
Source File: DragLayer.java    From TurboLauncher with Apache License 2.0 5 votes vote down vote up
private void sendTapOutsideFolderAccessibilityEvent(boolean isEditingName) {
    AccessibilityManager accessibilityManager = (AccessibilityManager)
            getContext().getSystemService(Context.ACCESSIBILITY_SERVICE);
    if (accessibilityManager.isEnabled()) {
        int stringId = isEditingName ? R.string.folder_tap_to_rename : R.string.folder_tap_to_close;
        AccessibilityEvent event = AccessibilityEvent.obtain(
                AccessibilityEvent.TYPE_VIEW_FOCUSED);
        onInitializeAccessibilityEvent(event);
        event.getText().add(getContext().getString(stringId));
        accessibilityManager.sendAccessibilityEvent(event);
    }
}
 
Example 11
Source File: ScrollFeedbackManager.java    From talkback with Apache License 2.0 5 votes vote down vote up
/** Posts the delayed scroll position feedback. Call this for every VIEW_SCROLLED event. */
private void postScrollFeedback(
    AccessibilityEvent event, Performance.@Nullable EventId eventId) {
  cancelScrollFeedback();
  AccessibilityEvent eventClone;
  try {
    eventClone = AccessibilityEvent.obtain(event);
  } catch (NullPointerException e) {
    LogUtils.i(
        TAG,
        "A NullPointerException is expected to be thrown in the Robolectric tests when we try"
            + " to create a clone of the mocking AccessibilityEvent instance. This exception"
            + " should never occur when the program is running on actual Android devices.");
    eventClone = event;
  }

  final EventIdAnd<AccessibilityEvent> eventAndId =
      new EventIdAnd<AccessibilityEvent>(eventClone, eventId);
  final Message msg = obtainMessage(SCROLL_FEEDBACK, eventAndId);

  AccessibilityNodeInfo source = event.getSource();
  if (Role.getRole(source) == Role.ROLE_PAGER) {
    sendMessageDelayed(msg, DELAY_PAGE_FEEDBACK);
  } else {
    sendMessageDelayed(msg, DELAY_SCROLL_FEEDBACK);
  }
  if (source != null) {
    source.recycle();
  }
}
 
Example 12
Source File: AndroidUtilities.java    From Telegram-FOSS with GNU General Public License v2.0 5 votes vote down vote up
public static void makeAccessibilityAnnouncement(CharSequence what) {
    AccessibilityManager am = (AccessibilityManager) ApplicationLoader.applicationContext.getSystemService(Context.ACCESSIBILITY_SERVICE);
    if (am.isEnabled()) {
        AccessibilityEvent ev = AccessibilityEvent.obtain();
        ev.setEventType(AccessibilityEvent.TYPE_ANNOUNCEMENT);
        ev.getText().add(what);
        am.sendAccessibilityEvent(ev);
    }
}
 
Example 13
Source File: TextPicker.java    From GifAssistant with Apache License 2.0 5 votes vote down vote up
private void sendAccessibilityEventForVirtualButton(int virtualViewId, int eventType,
                                                    String text) {
    if (((AccessibilityManager) getContext().getSystemService(Context.ACCESSIBILITY_SERVICE)).isEnabled()) {
        AccessibilityEvent event = AccessibilityEvent.obtain(eventType);
        event.setClassName(Button.class.getName());
        event.setPackageName(getContext().getPackageName());
        event.getText().add(text);
        event.setEnabled(TextPicker.this.isEnabled());
        event.setSource(TextPicker.this, virtualViewId);
        requestSendAccessibilityEvent(TextPicker.this, event);
    }
}
 
Example 14
Source File: NumberPicker.java    From zen4android with MIT License 5 votes vote down vote up
private void sendAccessibilityEventForVirtualButton(int virtualViewId, int eventType,
        String text) {
    if (((AccessibilityManager) getContext().getSystemService(Context.ACCESSIBILITY_SERVICE)).isEnabled()) {
        AccessibilityEvent event = AccessibilityEvent.obtain(eventType);
        event.setClassName(Button.class.getName());
        event.setPackageName(getContext().getPackageName());
        event.getText().add(text);
        event.setEnabled(NumberPicker.this.isEnabled());
        event.setSource(NumberPicker.this, virtualViewId);
        requestSendAccessibilityEvent(NumberPicker.this, event);
    }
}
 
Example 15
Source File: Toast.java    From android_9.0.0_r45 with Apache License 2.0 5 votes vote down vote up
private void trySendAccessibilityEvent() {
    AccessibilityManager accessibilityManager =
            AccessibilityManager.getInstance(mView.getContext());
    if (!accessibilityManager.isEnabled()) {
        return;
    }
    // treat toasts as notifications since they are used to
    // announce a transient piece of information to the user
    AccessibilityEvent event = AccessibilityEvent.obtain(
            AccessibilityEvent.TYPE_NOTIFICATION_STATE_CHANGED);
    event.setClassName(getClass().getName());
    event.setPackageName(mView.getContext().getPackageName());
    mView.dispatchPopulateAccessibilityEvent(event);
    accessibilityManager.sendAccessibilityEvent(event);
}
 
Example 16
Source File: NumberPicker.java    From android_9.0.0_r45 with Apache License 2.0 5 votes vote down vote up
private void sendAccessibilityEventForVirtualButton(int virtualViewId, int eventType,
        String text) {
    if (AccessibilityManager.getInstance(mContext).isEnabled()) {
        AccessibilityEvent event = AccessibilityEvent.obtain(eventType);
        event.setClassName(Button.class.getName());
        event.setPackageName(mContext.getPackageName());
        event.getText().add(text);
        event.setEnabled(NumberPicker.this.isEnabled());
        event.setSource(NumberPicker.this, virtualViewId);
        requestSendAccessibilityEvent(NumberPicker.this, event);
    }
}
 
Example 17
Source File: NumberPicker.java    From ticdesign with Apache License 2.0 5 votes vote down vote up
private void sendAccessibilityEventForVirtualText(int eventType) {
    if (((AccessibilityManager) getContext().getSystemService(Context.ACCESSIBILITY_SERVICE)).isEnabled()) {
        AccessibilityEvent event = AccessibilityEvent.obtain(eventType);
        mInputText.onInitializeAccessibilityEvent(event);
        mInputText.onPopulateAccessibilityEvent(event);
        event.setSource(NumberPicker.this, VIRTUAL_VIEW_ID_INPUT);
        requestSendAccessibilityEvent(NumberPicker.this, event);
    }
}
 
Example 18
Source File: AndroidUtilities.java    From Telegram with GNU General Public License v2.0 5 votes vote down vote up
public static void makeAccessibilityAnnouncement(CharSequence what) {
    AccessibilityManager am = (AccessibilityManager) ApplicationLoader.applicationContext.getSystemService(Context.ACCESSIBILITY_SERVICE);
    if (am.isEnabled()) {
        AccessibilityEvent ev = AccessibilityEvent.obtain();
        ev.setEventType(AccessibilityEvent.TYPE_ANNOUNCEMENT);
        ev.getText().add(what);
        am.sendAccessibilityEvent(ev);
    }
}
 
Example 19
Source File: UrlBar.java    From AndroidChromium with Apache License 2.0 4 votes vote down vote up
@Override
public boolean commitText(CharSequence text, int newCursorPosition) {
    Editable currentText = getText();
    if (currentText == null) return super.commitText(text, newCursorPosition);

    int selectionStart = Selection.getSelectionStart(currentText);
    int selectionEnd = Selection.getSelectionEnd(currentText);
    int autocompleteIndex = currentText.getSpanStart(mAutocompleteSpan);
    // If the text being committed is a single character that matches the next character
    // in the selection (assumed to be the autocomplete text), we only move the text
    // selection instead clearing the autocomplete text causing flickering as the
    // autocomplete text will appear once the next suggestions are received.
    //
    // To be confident that the selection is an autocomplete, we ensure the selection
    // is at least one character and the end of the selection is the end of the
    // currently entered text.
    if (newCursorPosition == 1 && selectionStart > 0 && selectionStart != selectionEnd
            && selectionEnd >= currentText.length()
            && autocompleteIndex == selectionStart
            && text.length() == 1) {
        currentText.getChars(selectionStart, selectionStart + 1, mTempSelectionChar, 0);
        if (mTempSelectionChar[0] == text.charAt(0)) {

            // Since the text isn't changing, TalkBack won't read out the typed characters.
            // To work around this, explicitly send an accessibility event. crbug.com/416595
            if (mAccessibilityManager != null && mAccessibilityManager.isEnabled()) {
                AccessibilityEvent event = AccessibilityEvent.obtain(
                        AccessibilityEvent.TYPE_VIEW_TEXT_CHANGED);
                event.setFromIndex(selectionStart);
                event.setRemovedCount(0);
                event.setAddedCount(1);
                event.setBeforeText(currentText.toString().substring(0, selectionStart));
                sendAccessibilityEventUnchecked(event);
            }

            setAutocompleteText(
                    currentText.subSequence(0, selectionStart + 1),
                    currentText.subSequence(selectionStart + 1, selectionEnd));
            if (!mInBatchEditMode) {
                notifyAutocompleteTextStateChanged(false);
            }
            return true;
        }
    }

    boolean retVal = super.commitText(text, newCursorPosition);

    // Ensure the autocomplete span is removed if it is no longer valid after committing the
    // text.
    if (getText().getSpanStart(mAutocompleteSpan) >= 0) clearAutocompleteSpanIfInvalid();

    return retVal;
}
 
Example 20
Source File: ExploreByTouchHelper.java    From android-recipes-app with Apache License 2.0 2 votes vote down vote up
/**
 * Constructs and returns an {@link AccessibilityEvent} for the host node.
 *
 * @param eventType The type of event to construct.
 * @return An {@link AccessibilityEvent} populated with information about
 *         the specified item.
 */
private AccessibilityEvent createEventForHost(int eventType) {
    final AccessibilityEvent event = AccessibilityEvent.obtain(eventType);
    ViewCompat.onInitializeAccessibilityEvent(mView, event);
    return event;
}