Example usage for android.view.accessibility AccessibilityEvent obtain

List of usage examples for android.view.accessibility AccessibilityEvent obtain

Introduction

In this page you can find the example usage for android.view.accessibility AccessibilityEvent obtain.

Prototype

public static AccessibilityEvent obtain(AccessibilityEvent event) 

Source Link

Document

Returns a cached instance if such is available or a new one is created.

Usage

From source file:Main.java

public static void sendAccessibilityEvent(Context context) {
    AccessibilityManager am = (AccessibilityManager) context.getSystemService(Context.ACCESSIBILITY_SERVICE);
    if (am.isEnabled()) {
        am.sendAccessibilityEvent(AccessibilityEvent.obtain(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED));
    }/* ww w  .  ja va  2s . co m*/
}

From source file:Main.java

/**
 * Announce text through the AccessibilityManager for a view.
 *
 * @param text/*from  w w w . j a va2  s  .  c o  m*/
 * @param view
 * @param manager
 */
public static void announceText(String text, View view, AccessibilityManager manager) {
    // Only announce text if the accessibility service is enabled
    if (!manager.isEnabled()) {
        return;
    }
    AccessibilityEvent event = AccessibilityEvent.obtain(AccessibilityEventCompat.TYPE_ANNOUNCEMENT);
    event.getText().add(text);
    event.setEnabled(true);
    // Tie the event to the view
    event.setClassName(view.getClass().getName());
    event.setPackageName(view.getContext().getPackageName());
    AccessibilityEventCompat.asRecord(event).setSource(view);

    // Send the announcement
    manager.sendAccessibilityEvent(event);

}

From source file:Main.java

/**
 * Try to send an {@link AccessibilityEvent}
 * for a {@link View}.//from w w  w .  jav a  2 s .  c om
 *
 * @param view The View that will dispatch the AccessibilityEvent
 * @return true if the AccessibilityEvent was dispatched
 */
@SuppressWarnings("UnusedReturnValue")
public static boolean sendAccessibilityEvent(View view) {
    final AccessibilityManager accessibilityManager = (AccessibilityManager) view.getContext()
            .getSystemService(Context.ACCESSIBILITY_SERVICE);

    if (!accessibilityManager.isEnabled())
        return false;

    final AccessibilityEvent accessibilityEvent = AccessibilityEvent
            .obtain(AccessibilityEvent.TYPE_NOTIFICATION_STATE_CHANGED);
    accessibilityEvent.setClassName(view.getClass().getName());
    accessibilityEvent.setPackageName(view.getContext().getPackageName());

    view.dispatchPopulateAccessibilityEvent(accessibilityEvent);
    accessibilityManager.sendAccessibilityEvent(accessibilityEvent);

    return true;
}

From source file:com.android.gallery3d.util.AccessibilityUtils.java

public static void makeAnnouncement(View view, CharSequence announcement) {
    if (view == null)
        return;// ww w.j a  v  a 2s  .c om
    if (ApiHelper.HAS_ANNOUNCE_FOR_ACCESSIBILITY) {
        view.announceForAccessibility(announcement);
    } else {
        // For API 15 and earlier, we need to construct an accessibility event
        Context ctx = view.getContext();
        AccessibilityManager am = (AccessibilityManager) ctx.getSystemService(Context.ACCESSIBILITY_SERVICE);
        if (!am.isEnabled())
            return;
        AccessibilityEvent event = AccessibilityEvent
                .obtain(AccessibilityEvent.TYPE_NOTIFICATION_STATE_CHANGED);
        AccessibilityRecordCompat arc = new AccessibilityRecordCompat(event);
        arc.setSource(view);
        event.setClassName(view.getClass().getName());
        event.setPackageName(view.getContext().getPackageName());
        event.setEnabled(view.isEnabled());
        event.getText().add(announcement);
        am.sendAccessibilityEvent(event);
    }
}

From source file:com.android.fastergallery.util.AccessibilityUtils.java

public static void makeAnnouncement(View view, CharSequence announcement) {
    if (view == null)
        return;//from w  w  w  . ja  va  2  s.c o  m
    if (ApiHelper.HAS_ANNOUNCE_FOR_ACCESSIBILITY) {
        view.announceForAccessibility(announcement);
    } else {
        // For API 15 and earlier, we need to construct an accessibility
        // event
        Context ctx = view.getContext();
        AccessibilityManager am = (AccessibilityManager) ctx.getSystemService(Context.ACCESSIBILITY_SERVICE);
        if (!am.isEnabled())
            return;
        AccessibilityEvent event = AccessibilityEvent
                .obtain(AccessibilityEvent.TYPE_NOTIFICATION_STATE_CHANGED);
        AccessibilityRecordCompat arc = new AccessibilityRecordCompat(event);
        arc.setSource(view);
        event.setClassName(view.getClass().getName());
        event.setPackageName(view.getContext().getPackageName());
        event.setEnabled(view.isEnabled());
        event.getText().add(announcement);
        am.sendAccessibilityEvent(event);
    }
}

From source file:com.appeaser.sublimepickerlibrary.utilities.AccessibilityUtils.java

public static void makeAnnouncement(View view, CharSequence announcement) {
    if (view == null)
        return;//from  w w w . j av  a  2 s .  co m
    if (SUtils.isApi_16_OrHigher()) {
        view.announceForAccessibility(announcement);
    } else {
        // For API 15 and earlier, we need to construct an accessibility event
        Context ctx = view.getContext();
        AccessibilityManager am = (AccessibilityManager) ctx.getSystemService(Context.ACCESSIBILITY_SERVICE);
        if (!am.isEnabled())
            return;

        AccessibilityEvent event = AccessibilityEvent
                .obtain(AccessibilityEvent.TYPE_NOTIFICATION_STATE_CHANGED);
        AccessibilityRecordCompat arc = AccessibilityEventCompat.asRecord(event);
        arc.setSource(view);
        event.setClassName(view.getClass().getName());
        event.setPackageName(view.getContext().getPackageName());
        event.setEnabled(view.isEnabled());
        event.getText().add(announcement);
        am.sendAccessibilityEvent(event);
    }
}

From source file:com.android.talkback.TalkBackKeyboardShortcutPreferencesActivity.java

/**
 * Utility method for announcing text via accessibility event.
 *//*  w  w  w  .j a v a  2s .c  om*/
public static void announceText(String text, Context context) {
    AccessibilityManager accessibilityManager = (AccessibilityManager) context
            .getSystemService(Context.ACCESSIBILITY_SERVICE);
    if (accessibilityManager.isEnabled()) {
        AccessibilityEvent event = AccessibilityEvent.obtain(AccessibilityEvent.TYPE_ANNOUNCEMENT);
        event.setContentDescription(text);
        accessibilityManager.sendAccessibilityEvent(event);
    }
}

From source file:com.android.mail.utils.ViewUtils.java

/**
 * Convenience method for sending a {@link android.view.accessibility.AccessibilityEvent#TYPE_ANNOUNCEMENT}
 * {@link android.view.accessibility.AccessibilityEvent} to make an announcement which is related to some
 * sort of a context change for which none of the events representing UI transitions
 * is a good fit. For example, announcing a new page in a book. If accessibility
 * is not enabled this method does nothing.
 *
 * @param view view to perform the accessibility announcement
 * @param text The announcement text.//  w w  w  .j a v  a 2 s .  co  m
 */
public static void announceForAccessibility(View view, CharSequence text) {
    final AccessibilityManager accessibilityManager = (AccessibilityManager) view.getContext()
            .getSystemService(Context.ACCESSIBILITY_SERVICE);
    final ViewParent parent = view.getParent();
    if (accessibilityManager.isEnabled() && parent != null) {
        AccessibilityEvent event = AccessibilityEvent.obtain(AccessibilityEvent.TYPE_ANNOUNCEMENT);
        view.onInitializeAccessibilityEvent(event);
        event.getText().add(text);
        event.setContentDescription(null);
        parent.requestSendAccessibilityEvent(view, event);
    }
}

From source file:com.android.messaging.util.AccessibilityUtil.java

public static void announceForAccessibilityCompat(final View view,
        @Nullable AccessibilityManager accessibilityManager, final CharSequence text) {
    final Context context = view.getContext().getApplicationContext();
    if (accessibilityManager == null) {
        accessibilityManager = (AccessibilityManager) context.getSystemService(Context.ACCESSIBILITY_SERVICE);
    }/*from   w w w.j  av a  2 s . com*/

    if (!accessibilityManager.isEnabled()) {
        return;
    }

    // Jelly Bean added support for speaking text verbatim
    final int eventType = OsUtil.isAtLeastJB() ? AccessibilityEvent.TYPE_ANNOUNCEMENT
            : AccessibilityEvent.TYPE_VIEW_FOCUSED;

    // Construct an accessibility event with the minimum recommended
    // attributes. An event without a class name or package may be dropped.
    final AccessibilityEvent event = AccessibilityEvent.obtain(eventType);
    event.getText().add(text);
    event.setEnabled(view.isEnabled());
    event.setClassName(view.getClass().getName());
    event.setPackageName(context.getPackageName());

    // JellyBean MR1 requires a source view to set the window ID.
    final AccessibilityRecordCompat record = AccessibilityEventCompat.asRecord(event);
    record.setSource(view);

    // Sends the event directly through the accessibility manager. If we only supported SDK 14+
    // we could have done:
    // getParent().requestSendAccessibilityEvent(this, event);
    accessibilityManager.sendAccessibilityEvent(event);
}

From source file:com.android.screenspeak.eventprocessor.EventQueue.java

/**
 * Adds an {@link AccessibilityEvent} to the queue for processing. If this
 * addition causes the queue to exceed the maximum allowable events for an
 * event's type, earlier events of this type will be pruned from the queue.
 *
 * @param event The event to add to the queue
 *///  w  w  w . j av  a  2  s .c om
public void enqueue(AccessibilityEvent event) {
    final AccessibilityEvent clone = AccessibilityEvent.obtain(event);
    final int eventType = clone.getEventType();

    if (AccessibilityEventUtils.eventMatchesAnyType(clone, MASK_LIMITED_EVENT_TYPES)) {
        final int eventCountOfType = mQualifyingEvents.get(eventType, 0);
        mQualifyingEvents.put(eventType, (eventCountOfType + 1));
    }

    mEventQueue.add(clone);
    enforceEventLimits();
}